OSDN Git Service

sens: fix server crash after stopping the poll client
[android-x86/hardware-intel-libsensors.git] / enumeration.c
1 /*
2  * Copyright (C) 2014 Intel Corporation.
3  */
4
5 #include <ctype.h>
6 #include <dirent.h>
7 #include <stdlib.h>
8 #include <utils/Log.h>
9 #include <hardware/sensors.h>
10 #include "enumeration.h"
11 #include "description.h"
12 #include "utils.h"
13 #include "transform.h"
14 #include "description.h"
15 #include "control.h"
16 #include "calibration.h"
17
18 /*
19  * This table maps syfs entries in scan_elements directories to sensor types,
20  * and will also be used to determine other sysfs names as well as the iio
21  * device number associated to a specific sensor.
22  */
23
24  /*
25   * We duplicate entries for the uncalibrated types after their respective base
26   * sensor. This is because all sensor entries must have an associated catalog entry
27   * and also because when only the uncal sensor is active it needs to take it's data
28   * from the same iio device as the base one.
29   */
30
31 struct sensor_catalog_entry_t sensor_catalog[] = {
32         DECLARE_SENSOR3("accel",      SENSOR_TYPE_ACCELEROMETER,  "x", "y", "z")
33         DECLARE_SENSOR3("anglvel",    SENSOR_TYPE_GYROSCOPE,      "x", "y", "z")
34         DECLARE_SENSOR3("magn",       SENSOR_TYPE_MAGNETIC_FIELD, "x", "y", "z")
35         DECLARE_SENSOR1("intensity",  SENSOR_TYPE_LIGHT,          "both"       )
36         DECLARE_SENSOR0("illuminance",SENSOR_TYPE_LIGHT                        )
37         DECLARE_SENSOR3("incli",      SENSOR_TYPE_ORIENTATION,    "x", "y", "z")
38         DECLARE_SENSOR4("rot",        SENSOR_TYPE_ROTATION_VECTOR,
39                                          "quat_x", "quat_y", "quat_z", "quat_w")
40         DECLARE_SENSOR0("temp",       SENSOR_TYPE_AMBIENT_TEMPERATURE          )
41         DECLARE_SENSOR0("proximity",  SENSOR_TYPE_PROXIMITY                    )
42         DECLARE_SENSOR3("anglvel",      SENSOR_TYPE_GYROSCOPE_UNCALIBRATED, "x", "y", "z")
43 };
44
45 #define CATALOG_SIZE    ARRAY_SIZE(sensor_catalog)
46
47 /* ACPI PLD (physical location of device) definitions, as used with sensors */
48
49 #define PANEL_FRONT     4
50 #define PANEL_BACK      5
51
52 /* We equate sensor handles to indices in these tables */
53
54 struct sensor_t      sensor_desc[MAX_SENSORS];  /* Android-level descriptors */
55 struct sensor_info_t sensor_info[MAX_SENSORS];  /* Internal descriptors      */
56 int sensor_count;                               /* Detected sensors          */
57
58
59 static void setup_properties_from_pld(int s, int panel, int rotation,
60                                       int num_channels)
61 {
62         /*
63          * Generate suitable order and opt_scale directives from the PLD panel
64          * and rotation codes we got. This can later be superseded by the usual
65          * properties if necessary. Eventually we'll need to replace these
66          * mechanisms by a less convoluted one, such as a 3x3 placement matrix.
67          */
68
69         int x = 1;
70         int y = 1;
71         int z = 1;
72         int xy_swap = 0;
73         int angle = rotation * 45;
74
75         /* Only deal with 3 axis chips for now */
76         if (num_channels < 3)
77                 return;
78
79         if (panel == PANEL_BACK) {
80                 /* Chip placed on the back panel ; negate x and z */
81                 x = -x;
82                 z = -z;
83         }
84
85         switch (angle) {
86                 case 90: /* 90° clockwise: negate y then swap x,y */
87                         xy_swap = 1;
88                         y = -y;
89                         break;
90
91                 case 180: /* Upside down: negate x and y */
92                         x = -x;
93                         y = -y;
94                         break;
95
96                 case 270: /* 90° counter clockwise: negate x then swap x,y */
97                         x = -x;
98                         xy_swap = 1;
99                         break;
100         }
101
102         if (xy_swap) {
103                 sensor_info[s].order[0] = 1;
104                 sensor_info[s].order[1] = 0;
105                 sensor_info[s].order[2] = 2;
106                 sensor_info[s].quirks |= QUIRK_FIELD_ORDERING;
107         }
108
109         sensor_info[s].channel[0].opt_scale = x;
110         sensor_info[s].channel[1].opt_scale = y;
111         sensor_info[s].channel[2].opt_scale = z;
112 }
113
114
115 static int is_valid_pld (int panel, int rotation)
116 {
117         if (panel != PANEL_FRONT && panel != PANEL_BACK) {
118                 ALOGW("Unhandled PLD panel spec: %d\n", panel);
119                 return 0;
120         }
121
122         /* Only deal with 90° rotations for now */
123         if (rotation < 0 || rotation > 7 || (rotation & 1)) {
124                 ALOGW("Unhandled PLD rotation spec: %d\n", rotation);
125                 return 0;
126         }
127
128         return 1;
129 }
130
131
132 static int read_pld_from_properties (int s, int* panel, int* rotation)
133 {
134         int p, r;
135
136         if (sensor_get_prop(s, "panel", &p))
137                 return -1;
138
139         if (sensor_get_prop(s, "rotation", &r))
140                 return -1;
141
142         if (!is_valid_pld(p, r))
143                 return -1;
144
145         *panel = p;
146         *rotation = r;
147
148         ALOGI("S%d PLD from properties: panel=%d, rotation=%d\n", s, p, r);
149
150         return 0;
151 }
152
153
154 static int read_pld_from_sysfs (int s, int dev_num, int* panel, int* rotation)
155 {
156         char sysfs_path[PATH_MAX];
157         int p,r;
158
159         sprintf(sysfs_path, BASE_PATH "../firmware_node/pld/panel", dev_num);
160
161         if (sysfs_read_int(sysfs_path, &p))
162                 return -1;
163
164         sprintf(sysfs_path, BASE_PATH "../firmware_node/pld/rotation", dev_num);
165
166         if (sysfs_read_int(sysfs_path, &r))
167                 return -1;
168
169         if (!is_valid_pld(p, r))
170                 return -1;
171
172         *panel = p;
173         *rotation = r;
174
175         ALOGI("S%d PLD from sysfs: panel=%d, rotation=%d\n", s, p, r);
176
177         return 0;
178 }
179
180
181 static void decode_placement_information (int dev_num, int num_channels, int s)
182 {
183         /*
184          * See if we have optional "physical location of device" ACPI tags.
185          * We're only interested in panel and rotation specifiers. Use the
186          * .panel and .rotation properties in priority, and the actual ACPI
187          * values as a second source.
188          */
189
190         int panel;
191         int rotation;
192
193         if (read_pld_from_properties(s, &panel, &rotation) &&
194                 read_pld_from_sysfs(s, dev_num, &panel, &rotation))
195                         return; /* No PLD data available */
196
197         /* Map that to field ordering and scaling mechanisms */
198         setup_properties_from_pld(s, panel, rotation, num_channels);
199 }
200
201
202 static void add_sensor (int dev_num, int catalog_index, int use_polling)
203 {
204         int s;
205         int sensor_type;
206         int retval;
207         char sysfs_path[PATH_MAX];
208         const char* prefix;
209         float scale;
210         int c;
211         float opt_scale;
212         const char* ch_name;
213         int num_channels;
214         char suffix[MAX_NAME_SIZE + 8];
215
216         if (sensor_count == MAX_SENSORS) {
217                 ALOGE("Too many sensors!\n");
218                 return;
219         }
220
221         sensor_type = sensor_catalog[catalog_index].type;
222
223         /*
224          * At this point we could check that the expected sysfs attributes are
225          * present ; that would enable having multiple catalog entries with the
226          * same sensor type, accomodating different sets of sysfs attributes.
227          */
228
229         s = sensor_count;
230
231         sensor_info[s].dev_num          = dev_num;
232         sensor_info[s].catalog_index    = catalog_index;
233         sensor_info[s].type             = sensor_type;
234
235         num_channels = sensor_catalog[catalog_index].num_channels;
236
237         if (use_polling)
238                 sensor_info[s].num_channels = 0;
239         else
240                 sensor_info[s].num_channels = num_channels;
241
242         prefix = sensor_catalog[catalog_index].tag;
243
244         /*
245          * receiving the illumination sensor calibration inputs from
246          * the Android properties and setting it within sysfs
247          */
248         if (sensor_type == SENSOR_TYPE_LIGHT) {
249                 retval = sensor_get_illumincalib(s);
250                 if (retval > 0) {
251                         sprintf(sysfs_path, ILLUMINATION_CALIBPATH, dev_num);
252                         sysfs_write_int(sysfs_path, retval);
253                 }
254         }
255
256         /* Read name attribute, if available */
257         sprintf(sysfs_path, NAME_PATH, dev_num);
258         sysfs_read_str(sysfs_path, sensor_info[s].internal_name, MAX_NAME_SIZE);
259
260         /* See if we have general offsets and scale values for this sensor */
261
262         sprintf(sysfs_path, SENSOR_OFFSET_PATH, dev_num, prefix);
263         sysfs_read_float(sysfs_path, &sensor_info[s].offset);
264
265         sprintf(sysfs_path, SENSOR_SCALE_PATH, dev_num, prefix);
266         if (!sensor_get_fl_prop(s, "scale", &scale)) {
267                 /*
268                  * There is a chip preferred scale specified,
269                  * so try to store it in sensor's scale file
270                  */
271                 if (sysfs_write_float(sysfs_path, scale) == -1 && errno == ENOENT) {
272                         ALOGE("Failed to store scale[%f] into %s - file is missing", scale, sysfs_path);
273                         /* Store failed, try to store the scale into channel specific file */
274                         for (c = 0; c < num_channels; c++)
275                         {
276                                 sprintf(sysfs_path, BASE_PATH "%s", dev_num,
277                                         sensor_catalog[catalog_index].channel[c].scale_path);
278                                 if (sysfs_write_float(sysfs_path, scale) == -1)
279                                         ALOGE("Failed to store scale[%f] into %s", scale, sysfs_path);
280                         }
281                 }
282         }
283
284         sprintf(sysfs_path, SENSOR_SCALE_PATH, dev_num, prefix);
285         if (!sysfs_read_float(sysfs_path, &scale)) {
286                 sensor_info[s].scale = scale;
287                 ALOGI("Scale path:%s scale:%f dev_num:%d\n",
288                                         sysfs_path, scale, dev_num);
289         } else {
290                 sensor_info[s].scale = 1;
291
292                 /* Read channel specific scale if any*/
293                 for (c = 0; c < num_channels; c++)
294                 {
295                         sprintf(sysfs_path, BASE_PATH "%s", dev_num,
296                            sensor_catalog[catalog_index].channel[c].scale_path);
297
298                         if (!sysfs_read_float(sysfs_path, &scale)) {
299                                 sensor_info[s].channel[c].scale = scale;
300                                 sensor_info[s].scale = 0;
301
302                                 ALOGI(  "Scale path:%s "
303                                         "channel scale:%f dev_num:%d\n",
304                                         sysfs_path, scale, dev_num);
305                         }
306                 }
307         }
308
309         /* Set default scaling - if num_channels is zero, we have one channel */
310
311         sensor_info[s].channel[0].opt_scale = 1;
312
313         for (c = 1; c < num_channels; c++)
314                 sensor_info[s].channel[c].opt_scale = 1;
315
316         /* Read ACPI _PLD attributes for this sensor, if there are any */
317         decode_placement_information(dev_num, num_channels, s);
318
319         /*
320          * See if we have optional correction scaling factors for each of the
321          * channels of this sensor. These would be expressed using properties
322          * like iio.accel.y.opt_scale = -1. In case of a single channel we also
323          * support things such as iio.temp.opt_scale = -1. Note that this works
324          * for all types of sensors, and whatever transform is selected, on top
325          * of any previous conversions.
326          */
327
328         if (num_channels) {
329                 for (c = 0; c < num_channels; c++) {
330                         ch_name = sensor_catalog[catalog_index].channel[c].name;
331                         sprintf(suffix, "%s.opt_scale", ch_name);
332                         if (!sensor_get_fl_prop(s, suffix, &opt_scale))
333                                 sensor_info[s].channel[c].opt_scale = opt_scale;
334                 }
335         } else
336                 if (!sensor_get_fl_prop(s, "opt_scale", &opt_scale))
337                         sensor_info[s].channel[0].opt_scale = opt_scale;
338
339         /* Initialize Android-visible descriptor */
340         sensor_desc[s].name             = sensor_get_name(s);
341         sensor_desc[s].vendor           = sensor_get_vendor(s);
342         sensor_desc[s].version          = sensor_get_version(s);
343         sensor_desc[s].handle           = s;
344         sensor_desc[s].type             = sensor_type;
345         sensor_desc[s].maxRange         = sensor_get_max_range(s);
346         sensor_desc[s].resolution       = sensor_get_resolution(s);
347         sensor_desc[s].power            = sensor_get_power(s);
348         sensor_desc[s].stringType = sensor_get_string_type(s);
349
350         /* None of our supported sensors requires a special permission.
351         *  If this will be the case we should implement a sensor_get_perm
352         */
353         sensor_desc[s].requiredPermission = "";
354         sensor_desc[s].flags = sensor_get_flags(s);
355         sensor_desc[s].minDelay = sensor_get_min_delay(s);
356         sensor_desc[s].maxDelay = sensor_get_max_delay(s);
357         ALOGI("Sensor %d (%s) type(%d) minD(%d) maxD(%d) flags(%2.2x)\n",
358                 s, sensor_info[s].friendly_name, sensor_desc[s].type,
359                 sensor_desc[s].minDelay, sensor_desc[s].maxDelay, sensor_desc[s].flags);
360
361         /* We currently do not implement batching when we'll so
362          * these should be overriden appropriately
363          */
364         sensor_desc[s].fifoReservedEventCount = 0;
365         sensor_desc[s].fifoMaxEventCount = 0;
366
367         /* Populate the quirks array */
368         sensor_get_quirks(s);
369
370         if (sensor_info[s].internal_name[0] == '\0') {
371                 /*
372                  * In case the kernel-mode driver doesn't expose a name for
373                  * the iio device, use (null)-dev%d as the trigger name...
374                  * This can be considered a kernel-mode iio driver bug.
375                  */
376                 ALOGW("Using null trigger on sensor %d (dev %d)\n", s, dev_num);
377                 strcpy(sensor_info[s].internal_name, "(null)");
378         }
379
380         if (sensor_type == SENSOR_TYPE_GYROSCOPE ||
381                 sensor_type == SENSOR_TYPE_GYROSCOPE_UNCALIBRATED) {
382                 struct gyro_cal* calibration_data = calloc(1, sizeof(struct gyro_cal));
383                 sensor_info[s].cal_data = calibration_data;
384         }
385
386         if (sensor_type == SENSOR_TYPE_MAGNETIC_FIELD) {
387                 struct compass_cal* calibration_data = calloc(1, sizeof(struct compass_cal));
388                 sensor_info[s].cal_data = calibration_data;
389         }
390         sensor_info[s].max_cal_level = sensor_get_cal_steps(s);
391         /* Select one of the available sensor sample processing styles */
392         select_transform(s);
393
394         /* Initialize fields related to sysfs reads offloading */
395         sensor_info[s].thread_data_fd[0]  = -1;
396         sensor_info[s].thread_data_fd[1]  = -1;
397         sensor_info[s].acquisition_thread = -1;
398
399         /* Check if we have a special ordering property on this sensor */
400         if (sensor_get_order(s, sensor_info[s].order))
401                 sensor_info[s].quirks |= QUIRK_FIELD_ORDERING;
402
403         sensor_count++;
404 }
405
406
407 static void discover_poll_sensors (int dev_num, char map[CATALOG_SIZE])
408 {
409         char base_dir[PATH_MAX];
410         DIR *dir;
411         struct dirent *d;
412         unsigned int i;
413         int c;
414
415         memset(map, 0, CATALOG_SIZE);
416
417         snprintf(base_dir, sizeof(base_dir), BASE_PATH, dev_num);
418
419         dir = opendir(base_dir);
420         if (!dir) {
421                 return;
422         }
423
424         /* Enumerate entries in this iio device's base folder */
425
426         while ((d = readdir(dir))) {
427                 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
428                         continue;
429
430                 /* If the name matches a catalog entry, flag it */
431                 for (i = 0; i<CATALOG_SIZE; i++) {
432                 /* This will be added separately later */
433                 if (sensor_catalog[i].type == SENSOR_TYPE_GYROSCOPE_UNCALIBRATED)
434                         continue;
435                 for (c=0; c<sensor_catalog[i].num_channels; c++)
436                         if (!strcmp(d->d_name,sensor_catalog[i].channel[c].raw_path) ||
437                                 !strcmp(d->d_name, sensor_catalog[i].channel[c].input_path)) {
438                                         map[i] = 1;
439                                         break;
440                         }
441                 }
442         }
443
444         closedir(dir);
445 }
446
447
448 static void discover_trig_sensors (int dev_num, char map[CATALOG_SIZE])
449 {
450         char scan_elem_dir[PATH_MAX];
451         DIR *dir;
452         struct dirent *d;
453         unsigned int i;
454
455         memset(map, 0, CATALOG_SIZE);
456
457         /* Enumerate entries in this iio device's scan_elements folder */
458
459         snprintf(scan_elem_dir, sizeof(scan_elem_dir), CHANNEL_PATH, dev_num);
460
461         dir = opendir(scan_elem_dir);
462         if (!dir) {
463                 return;
464         }
465
466         while ((d = readdir(dir))) {
467                 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
468                         continue;
469
470                 /* Compare en entry to known ones and create matching sensors */
471
472                 for (i = 0; i<CATALOG_SIZE; i++) {
473                         if (sensor_catalog[i].type == SENSOR_TYPE_GYROSCOPE_UNCALIBRATED)
474                                 continue;
475                         if (!strcmp(d->d_name,
476                                         sensor_catalog[i].channel[0].en_path)) {
477                                         map[i] = 1;
478                                         break;
479                         }
480                 }
481         }
482
483         closedir(dir);
484 }
485
486
487 static void orientation_sensor_check(void)
488 {
489         /*
490          * If we have accel + gyro + magn but no rotation vector sensor,
491          * SensorService replaces the HAL provided orientation sensor by the
492          * AOSP version... provided we report one. So initialize a virtual
493          * orientation sensor with zero values, which will get replaced. See:
494          * frameworks/native/services/sensorservice/SensorService.cpp, looking
495          * for SENSOR_TYPE_ROTATION_VECTOR; that code should presumably fall
496          * back to mUserSensorList.add instead of replaceAt, but accommodate it.
497          */
498
499         int i;
500         int has_acc = 0;
501         int has_gyr = 0;
502         int has_mag = 0;
503         int has_rot = 0;
504         int has_ori = 0;
505         int catalog_size = CATALOG_SIZE;
506
507         for (i=0; i<sensor_count; i++)
508                 switch (sensor_info[i].type) {
509                         case SENSOR_TYPE_ACCELEROMETER:
510                                 has_acc = 1;
511                                 break;
512                         case SENSOR_TYPE_GYROSCOPE:
513                                 has_gyr = 1;
514                                 break;
515                         case SENSOR_TYPE_MAGNETIC_FIELD:
516                                 has_mag = 1;
517                                 break;
518                         case SENSOR_TYPE_ORIENTATION:
519                                 has_ori = 1;
520                                 break;
521                         case SENSOR_TYPE_ROTATION_VECTOR:
522                                 has_rot = 1;
523                                 break;
524                 }
525
526         if (has_acc && has_gyr && has_mag && !has_rot && !has_ori)
527                 for (i=0; i<catalog_size; i++)
528                         if (sensor_catalog[i].type == SENSOR_TYPE_ORIENTATION) {
529                                 ALOGI("Adding placeholder orientation sensor");
530                                 add_sensor(0, i, 1);
531                                 break;
532                         }
533 }
534
535 static void propose_new_trigger (int s, char trigger_name[MAX_NAME_SIZE],
536                                  int sensor_name_len)
537 {
538         /*
539          * A new trigger has been enumerated for this sensor. Check if it makes
540          * sense to use it over the currently selected one, and select it if it
541          * is so. The format is something like sensor_name-dev0.
542          */
543
544         const char *suffix = trigger_name + sensor_name_len + 1;
545
546         /* dev is the default, and lowest priority; no need to update */
547         if (!memcmp(suffix, "dev", 3))
548                 return;
549
550         /* If we found any-motion trigger, record it */
551
552         if (!memcmp(suffix, "any-motion-", 11)) {
553                 strcpy(sensor_info[s].motion_trigger_name, trigger_name);
554                 return;
555         }
556
557         /*
558          * It's neither the default "dev" nor an "any-motion" one. Make sure we
559          * use this though, as we may not have any other indication of the name
560          * of the trigger to use with this sensor.
561          */
562         strcpy(sensor_info[s].init_trigger_name, trigger_name);
563 }
564
565
566 static void update_sensor_matching_trigger_name (char name[MAX_NAME_SIZE])
567 {
568         /*
569          * Check if we have a sensor matching the specified trigger name,
570          * which should then begin with the sensor name, and end with a number
571          * equal to the iio device number the sensor is associated to. If so,
572          * update the string we're going to write to trigger/current_trigger
573          * when enabling this sensor.
574          */
575
576         int s;
577         int dev_num;
578         int len;
579         char* cursor;
580         int sensor_name_len;
581
582         /*
583          * First determine the iio device number this trigger refers to. We
584          * expect the last few characters (typically one) of the trigger name
585          * to be this number, so perform a few checks.
586          */
587         len = strnlen(name, MAX_NAME_SIZE);
588
589         if (len < 2)
590                 return;
591
592         cursor = name + len - 1;
593
594         if (!isdigit(*cursor))
595                 return;
596
597         while (len && isdigit(*cursor)) {
598                 len--;
599                 cursor--;
600         }
601
602         dev_num = atoi(cursor+1);
603
604         /* See if that matches a sensor */
605         for (s=0; s<sensor_count; s++)
606                 if (sensor_info[s].dev_num == dev_num) {
607
608                         sensor_name_len = strlen(sensor_info[s].internal_name);
609
610                         if (!strncmp(name,
611                                      sensor_info[s].internal_name,
612                                      sensor_name_len))
613                                 /* Switch to new trigger if appropriate */
614                                 propose_new_trigger(s, name, sensor_name_len);
615                 }
616 }
617
618
619 static void setup_trigger_names (void)
620 {
621         char filename[PATH_MAX];
622         char buf[MAX_NAME_SIZE];
623         int len;
624         int s;
625         int trigger;
626         int ret;
627
628         /* By default, use the name-dev convention that most drivers use */
629         for (s=0; s<sensor_count; s++)
630                 snprintf(sensor_info[s].init_trigger_name,
631                          MAX_NAME_SIZE, "%s-dev%d",
632                          sensor_info[s].internal_name, sensor_info[s].dev_num);
633
634         /* Now have a look to /sys/bus/iio/devices/triggerX entries */
635
636         for (trigger=0; trigger<MAX_TRIGGERS; trigger++) {
637
638                 snprintf(filename, sizeof(filename), TRIGGER_FILE_PATH,trigger);
639
640                 ret = sysfs_read_str(filename, buf, sizeof(buf));
641
642                 if (ret < 0)
643                         break;
644
645                 /* Record initial and any-motion triggers names */
646                 update_sensor_matching_trigger_name(buf);
647         }
648
649         /*
650          * Certain drivers expose only motion triggers even though they should
651          * be continous. For these, use the default trigger name as the motion
652          * trigger. The code generating intermediate events is dependent on
653          * motion_trigger_name being set to a non empty string.
654          */
655
656         for (s=0; s<sensor_count; s++)
657                 if ((sensor_info[s].quirks & QUIRK_TERSE_DRIVER) &&
658                     sensor_info[s].motion_trigger_name[0] == '\0')
659                         strcpy( sensor_info[s].motion_trigger_name,
660                                 sensor_info[s].init_trigger_name);
661
662         for (s=0; s<sensor_count; s++)
663                 if (sensor_info[s].num_channels) {
664                         ALOGI("Sensor %d (%s) default trigger: %s\n", s,
665                                 sensor_info[s].friendly_name,
666                                 sensor_info[s].init_trigger_name);
667                         if (sensor_info[s].motion_trigger_name[0])
668                                 ALOGI("Sensor %d (%s) motion trigger: %s\n",
669                                 s, sensor_info[s].friendly_name,
670                                 sensor_info[s].motion_trigger_name);
671                 }
672 }
673
674 static void uncalibrated_gyro_check (void)
675 {
676         unsigned int has_gyr = 0;
677         unsigned int dev_num;
678         int i, c;
679         unsigned int is_poll_sensor;
680         char buf[MAX_NAME_SIZE];
681
682         int cal_idx = 0;
683         int uncal_idx = 0;
684         int catalog_size = CATALOG_SIZE; /* Avoid GCC sign comparison warning */
685
686         /* Checking to see if we have a gyroscope - we can only have uncal if we have the base sensor */
687         for (i=0; i < sensor_count; i++)
688                 if (sensor_info[i].type == SENSOR_TYPE_GYROSCOPE) {
689                         has_gyr=1;
690                         dev_num = sensor_info[i].dev_num;
691                         is_poll_sensor = !sensor_info[i].num_channels;
692                         cal_idx = i;
693                         break;
694                 }
695
696         /*
697          * If we have a gyro we can add the uncalibrated sensor of the same type and
698          * on the same dev_num. We will save indexes for easy finding and also save the
699          * channel specific information.
700          */
701         if (has_gyr)
702                 for (i=0; i<catalog_size; i++)
703                         if (sensor_catalog[i].type == SENSOR_TYPE_GYROSCOPE_UNCALIBRATED) {
704                                 add_sensor(dev_num, i, is_poll_sensor);
705
706                                 uncal_idx = sensor_count - 1; /* Just added uncalibrated sensor */
707
708                                 /* Similar to build_sensor_report_maps */
709                                 for (c = 0; c < sensor_info[uncal_idx].num_channels; c++)
710                                 {
711                                         memcpy( &(sensor_info[uncal_idx].channel[c].type_spec),
712                                                 &(sensor_info[cal_idx].channel[c].type_spec),
713                                                 sizeof(sensor_info[uncal_idx].channel[c].type_spec));
714                                         sensor_info[uncal_idx].channel[c].type_info = sensor_info[cal_idx].channel[c].type_info;
715                                         sensor_info[uncal_idx].channel[c].offset    = sensor_info[cal_idx].channel[c].offset;
716                                         sensor_info[uncal_idx].channel[c].size      = sensor_info[cal_idx].channel[c].size;
717                                 }
718                                 sensor_info[uncal_idx].pair_idx = cal_idx;
719                                 sensor_info[cal_idx].pair_idx = uncal_idx;
720                                 strncpy(sensor_info[uncal_idx].init_trigger_name,
721                                         sensor_info[cal_idx].init_trigger_name,
722                                         MAX_NAME_SIZE);
723                                 strncpy(sensor_info[uncal_idx].motion_trigger_name,
724                                         sensor_info[cal_idx].motion_trigger_name,
725                                         MAX_NAME_SIZE);
726
727                                 /* Add "Uncalibrated " prefix to sensor name */
728                                 strcpy(buf, sensor_info[cal_idx].friendly_name);
729                                 snprintf(sensor_info[uncal_idx].friendly_name,
730                                          MAX_NAME_SIZE,
731                                          "%s %s", "Uncalibrated", buf);
732                                 break;
733                         }
734 }
735
736 void enumerate_sensors (void)
737 {
738         /*
739          * Discover supported sensors and allocate control structures for them.
740          * Multiple sensors can potentially rely on a single iio device (each
741          * using their own channels). We can't have multiple sensors of the same
742          * type on the same device. In case of detection as both a poll-mode
743          * and trigger-based sensor, use the trigger usage mode.
744          */
745         char poll_sensors[CATALOG_SIZE];
746         char trig_sensors[CATALOG_SIZE];
747         int dev_num;
748         unsigned int i;
749         int trig_found;
750
751         for (dev_num=0; dev_num<MAX_DEVICES; dev_num++) {
752                 trig_found = 0;
753
754                 discover_poll_sensors(dev_num, poll_sensors);
755                 discover_trig_sensors(dev_num, trig_sensors);
756
757                 for (i=0; i<CATALOG_SIZE; i++)
758                         if (trig_sensors[i]) {
759                                 add_sensor(dev_num, i, 0);
760                                 trig_found = 1;
761                         }
762                         else
763                                 if (poll_sensors[i])
764                                         add_sensor(dev_num, i, 1);
765
766                 if (trig_found) {
767                         build_sensor_report_maps(dev_num);
768                 }
769         }
770
771         ALOGI("Discovered %d sensors\n", sensor_count);
772
773         /* Set up default - as well as custom - trigger names */
774         setup_trigger_names();
775
776         /* Make sure Android fall backs to its own orientation sensor */
777         orientation_sensor_check();
778
779         /*
780          * Create the uncalibrated counterpart to the compensated gyroscope.
781          * This is is a new sensor type in Android 4.4.
782          */
783
784         /*
785          * Patrick Porlan 11/12/2014 - Disabled for now due to a possible
786          * relation with GMINL-3234 Panorama Drift. I take full responsability
787          * for this.
788          *
789          * uncalibrated_gyro_check(); */
790 }
791
792
793 void delete_enumeration_data (void)
794 {
795         int i;
796         for (i = 0; i < sensor_count; i++)
797         switch (sensor_info[i].type) {
798                 case SENSOR_TYPE_MAGNETIC_FIELD:
799                         if (sensor_info[i].cal_data != NULL) {
800                                 free(sensor_info[i].cal_data);
801                                 sensor_info[i].cal_data = NULL;
802                                 sensor_info[i].cal_level = 0;
803                         }
804                         break;
805
806                 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
807                 case SENSOR_TYPE_GYROSCOPE:
808                         if (sensor_info[i].cal_data != NULL) {
809                                 free(sensor_info[i].cal_data);
810                                 sensor_info[i].cal_data = NULL;
811                                 sensor_info[i].cal_level = 0;
812                         }
813                         break;
814
815                 default:
816                         break;
817         }
818         /* Reset sensor count */
819         sensor_count = 0;
820 }
821
822
823 int get_sensors_list (__attribute__((unused)) struct sensors_module_t* module,
824                       struct sensor_t const** list)
825 {
826         *list = sensor_desc;
827         return sensor_count;
828 }
829