OSDN Git Service

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