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
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_catalog[catalog_index].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 (!sysfs_read_float(sysfs_path, &scale)) {
267                 sensor_info[s].scale = scale;
268                 ALOGI("Scale path:%s scale:%f dev_num:%d\n",
269                                         sysfs_path, scale, dev_num);
270         } else {
271                 sensor_info[s].scale = 1;
272
273                 /* Read channel specific scale if any*/
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
279                         if (!sysfs_read_float(sysfs_path, &scale)) {
280                                 sensor_info[s].channel[c].scale = scale;
281                                 sensor_info[s].scale = 0;
282
283                                 ALOGI(  "Scale path:%s "
284                                         "channel scale:%f dev_num:%d\n",
285                                         sysfs_path, scale, dev_num);
286                         }
287                 }
288         }
289
290         /* Set default scaling - if num_channels is zero, we have one channel */
291
292         sensor_info[s].channel[0].opt_scale = 1;
293
294         for (c = 1; c < num_channels; c++)
295                 sensor_info[s].channel[c].opt_scale = 1;
296
297         /* Read ACPI _PLD attributes for this sensor, if there are any */
298         decode_placement_information(dev_num, num_channels, s);
299
300         /*
301          * See if we have optional correction scaling factors for each of the
302          * channels of this sensor. These would be expressed using properties
303          * like iio.accel.y.opt_scale = -1. In case of a single channel we also
304          * support things such as iio.temp.opt_scale = -1. Note that this works
305          * for all types of sensors, and whatever transform is selected, on top
306          * of any previous conversions.
307          */
308
309         if (num_channels) {
310                 for (c = 0; c < num_channels; c++) {
311                         ch_name = sensor_catalog[catalog_index].channel[c].name;
312                         sprintf(suffix, "%s.opt_scale", ch_name);
313                         if (!sensor_get_fl_prop(s, suffix, &opt_scale))
314                                 sensor_info[s].channel[c].opt_scale = opt_scale;
315                 }
316         } else
317                 if (!sensor_get_fl_prop(s, "opt_scale", &opt_scale))
318                         sensor_info[s].channel[0].opt_scale = opt_scale;
319
320         /* Initialize Android-visible descriptor */
321         sensor_desc[s].name             = sensor_get_name(s);
322         sensor_desc[s].vendor           = sensor_get_vendor(s);
323         sensor_desc[s].version          = sensor_get_version(s);
324         sensor_desc[s].handle           = s;
325         sensor_desc[s].type             = sensor_type;
326         sensor_desc[s].maxRange         = sensor_get_max_range(s);
327         sensor_desc[s].resolution       = sensor_get_resolution(s);
328         sensor_desc[s].power            = sensor_get_power(s);
329         sensor_desc[s].stringType = sensor_get_string_type(s);
330
331         /* None of our supported sensors requires a special permission.
332         *  If this will be the case we should implement a sensor_get_perm
333         */
334         sensor_desc[s].requiredPermission = "";
335         sensor_desc[s].flags = sensor_get_flags(s);
336         sensor_desc[s].maxDelay = sensor_get_max_delay(s);
337         sensor_desc[s].minDelay = sensor_get_min_delay(s);
338
339         /* We currently do not implement batching when we'll so
340          * these should be overriden appropriately
341          */
342         sensor_desc[s].fifoReservedEventCount = 0;
343         sensor_desc[s].fifoMaxEventCount = 0;
344
345         if (sensor_info[s].internal_name[0] == '\0') {
346                 /*
347                  * In case the kernel-mode driver doesn't expose a name for
348                  * the iio device, use (null)-dev%d as the trigger name...
349                  * This can be considered a kernel-mode iio driver bug.
350                  */
351                 ALOGW("Using null trigger on sensor %d (dev %d)\n", s, dev_num);
352                 strcpy(sensor_info[s].internal_name, "(null)");
353         }
354
355         if (sensor_type == SENSOR_TYPE_GYROSCOPE ||
356                 sensor_type == SENSOR_TYPE_GYROSCOPE_UNCALIBRATED) {
357                 struct gyro_cal* calibration_data = calloc(1, sizeof(struct gyro_cal));
358                 sensor_info[s].cal_data = calibration_data;
359                 struct filter* f_data = (struct filter*) calloc(1, sizeof(struct filter));
360                 f_data->x_buff = (struct circ_buff*) calloc(1, sizeof (struct circ_buff));
361                 f_data->y_buff = (struct circ_buff*) calloc(1, sizeof (struct circ_buff));
362                 f_data->z_buff = (struct circ_buff*) calloc(1, sizeof (struct circ_buff));
363                 f_data->x_buff->buff = (float*)calloc(SAMPLE_SIZE, sizeof(float));
364                 f_data->y_buff->buff = (float*)calloc(SAMPLE_SIZE, sizeof(float));
365                 f_data->z_buff->buff = (float*)calloc(SAMPLE_SIZE, sizeof(float));
366                 f_data->x_buff->size = SAMPLE_SIZE;
367                 f_data->y_buff->size = SAMPLE_SIZE;
368                 f_data->z_buff->size = SAMPLE_SIZE;
369                 sensor_info[s].filter = f_data;
370         }
371
372         if (sensor_type == SENSOR_TYPE_MAGNETIC_FIELD) {
373                 struct compass_cal* calibration_data = calloc(1, sizeof(struct compass_cal));
374                 sensor_info[s].cal_data = calibration_data;
375         }
376
377         /* Select one of the available sensor sample processing styles */
378         select_transform(s);
379
380         /* Initialize fields related to sysfs reads offloading */
381         sensor_info[s].thread_data_fd[0]  = -1;
382         sensor_info[s].thread_data_fd[1]  = -1;
383         sensor_info[s].acquisition_thread = -1;
384
385         /* Check if we have a special ordering property on this sensor */
386         if (sensor_get_order(s, sensor_info[s].order))
387                 sensor_info[s].quirks |= QUIRK_FIELD_ORDERING;
388
389         sensor_count++;
390 }
391
392
393 static void discover_poll_sensors (int dev_num, char map[CATALOG_SIZE])
394 {
395         char base_dir[PATH_MAX];
396         DIR *dir;
397         struct dirent *d;
398         unsigned int i;
399         int c;
400
401         memset(map, 0, CATALOG_SIZE);
402
403         snprintf(base_dir, sizeof(base_dir), BASE_PATH, dev_num);
404
405         dir = opendir(base_dir);
406         if (!dir) {
407                 return;
408         }
409
410         /* Enumerate entries in this iio device's base folder */
411
412         while ((d = readdir(dir))) {
413                 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
414                         continue;
415
416                 /* If the name matches a catalog entry, flag it */
417                 for (i = 0; i<CATALOG_SIZE; i++) {
418                 /* This will be added separately later */
419                 if (sensor_catalog[i].type == SENSOR_TYPE_GYROSCOPE_UNCALIBRATED)
420                         continue;
421                 for (c=0; c<sensor_catalog[i].num_channels; c++)
422                         if (!strcmp(d->d_name,sensor_catalog[i].channel[c].raw_path) ||
423                                 !strcmp(d->d_name, sensor_catalog[i].channel[c].input_path)) {
424                                         map[i] = 1;
425                                         break;
426                         }
427                 }
428         }
429
430         closedir(dir);
431 }
432
433
434 static void discover_trig_sensors (int dev_num, char map[CATALOG_SIZE])
435 {
436         char scan_elem_dir[PATH_MAX];
437         DIR *dir;
438         struct dirent *d;
439         unsigned int i;
440
441         memset(map, 0, CATALOG_SIZE);
442
443         /* Enumerate entries in this iio device's scan_elements folder */
444
445         snprintf(scan_elem_dir, sizeof(scan_elem_dir), CHANNEL_PATH, dev_num);
446
447         dir = opendir(scan_elem_dir);
448         if (!dir) {
449                 return;
450         }
451
452         while ((d = readdir(dir))) {
453                 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
454                         continue;
455
456                 /* Compare en entry to known ones and create matching sensors */
457
458                 for (i = 0; i<CATALOG_SIZE; i++) {
459                         if (sensor_catalog[i].type == SENSOR_TYPE_GYROSCOPE_UNCALIBRATED)
460                                 continue;
461                         if (!strcmp(d->d_name,
462                                         sensor_catalog[i].channel[0].en_path)) {
463                                         map[i] = 1;
464                                         break;
465                         }
466                 }
467         }
468
469         closedir(dir);
470 }
471
472
473 static void orientation_sensor_check(void)
474 {
475         /*
476          * If we have accel + gyro + magn but no rotation vector sensor,
477          * SensorService replaces the HAL provided orientation sensor by the
478          * AOSP version... provided we report one. So initialize a virtual
479          * orientation sensor with zero values, which will get replaced. See:
480          * frameworks/native/services/sensorservice/SensorService.cpp, looking
481          * for SENSOR_TYPE_ROTATION_VECTOR; that code should presumably fall
482          * back to mUserSensorList.add instead of replaceAt, but accommodate it.
483          */
484
485         int i;
486         int has_acc = 0;
487         int has_gyr = 0;
488         int has_mag = 0;
489         int has_rot = 0;
490         int has_ori = 0;
491         int catalog_size = CATALOG_SIZE;
492
493         for (i=0; i<sensor_count; i++)
494                 switch (sensor_catalog[sensor_info[i].catalog_index].type) {
495                         case SENSOR_TYPE_ACCELEROMETER:
496                                 has_acc = 1;
497                                 break;
498                         case SENSOR_TYPE_GYROSCOPE:
499                                 has_gyr = 1;
500                                 break;
501                         case SENSOR_TYPE_MAGNETIC_FIELD:
502                                 has_mag = 1;
503                                 break;
504                         case SENSOR_TYPE_ORIENTATION:
505                                 has_ori = 1;
506                                 break;
507                         case SENSOR_TYPE_ROTATION_VECTOR:
508                                 has_rot = 1;
509                                 break;
510                 }
511
512         if (has_acc && has_gyr && has_mag && !has_rot && !has_ori)
513                 for (i=0; i<catalog_size; i++)
514                         if (sensor_catalog[i].type == SENSOR_TYPE_ORIENTATION) {
515                                 ALOGI("Adding placeholder orientation sensor");
516                                 add_sensor(0, i, 1);
517                                 break;
518                         }
519 }
520
521 static int is_continuous (int s)
522 {
523         /* Is sensor s of the continous trigger type kind? */
524
525         int catalog_index = sensor_info[s].catalog_index;
526         int sensor_type = sensor_catalog[catalog_index].type;
527
528         switch (sensor_type) {
529                 case SENSOR_TYPE_ACCELEROMETER:
530                 case SENSOR_TYPE_MAGNETIC_FIELD:
531                 case SENSOR_TYPE_ORIENTATION:
532                 case SENSOR_TYPE_GYROSCOPE:
533                 case SENSOR_TYPE_PRESSURE:
534                 case SENSOR_TYPE_GRAVITY:
535                 case SENSOR_TYPE_LINEAR_ACCELERATION:
536                 case SENSOR_TYPE_ROTATION_VECTOR:
537                 case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED:
538                 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
539                 case SENSOR_TYPE_GEOMAGNETIC_ROTATION_VECTOR:
540                         return 1;
541
542                 default:
543                         return 0;
544         }
545 }
546
547
548 static void propose_new_trigger (int s, char trigger_name[MAX_NAME_SIZE],
549                                  int sensor_name_len)
550 {
551         /*
552          * A new trigger has been enumerated for this sensor. Check if it makes
553          * sense to use it over the currently selected one, and select it if it
554          * is so. The format is something like sensor_name-dev0.
555          */
556
557         const char *suffix = trigger_name + sensor_name_len + 1;
558
559         /* dev is the default, and lowest priority; no need to update */
560         if (!memcmp(suffix, "dev", 3))
561                 return;
562
563         /*
564          * If we found any-motion trigger, record it and force the sensor to
565          * automatic intermediate event generation mode, at least if it is of a
566          * continuously firing sensor type.
567          */
568
569         if (!memcmp(suffix, "any-motion-", 11) && is_continuous(s)) {
570                 /* Update the any-motion trigger name to use for this sensor */
571                 strcpy(sensor_info[s].motion_trigger_name, trigger_name);
572                 return;
573         }
574
575         /* Update the initial trigger name to use for this sensor */
576         strcpy(sensor_info[s].init_trigger_name, trigger_name);
577 }
578
579
580 static void update_sensor_matching_trigger_name (char name[MAX_NAME_SIZE])
581 {
582         /*
583          * Check if we have a sensor matching the specified trigger name,
584          * which should then begin with the sensor name, and end with a number
585          * equal to the iio device number the sensor is associated to. If so,
586          * update the string we're going to write to trigger/current_trigger
587          * when enabling this sensor.
588          */
589
590         int s;
591         int dev_num;
592         int len;
593         char* cursor;
594         int sensor_name_len;
595
596         /*
597          * First determine the iio device number this trigger refers to. We
598          * expect the last few characters (typically one) of the trigger name
599          * to be this number, so perform a few checks.
600          */
601         len = strnlen(name, MAX_NAME_SIZE);
602
603         if (len < 2)
604                 return;
605
606         cursor = name + len - 1;
607
608         if (!isdigit(*cursor))
609                 return;
610
611         while (len && isdigit(*cursor)) {
612                 len--;
613                 cursor--;
614         }
615
616         dev_num = atoi(cursor+1);
617
618         /* See if that matches a sensor */
619         for (s=0; s<sensor_count; s++)
620                 if (sensor_info[s].dev_num == dev_num) {
621
622                         sensor_name_len = strlen(sensor_info[s].internal_name);
623
624                         if (!strncmp(name,
625                                      sensor_info[s].internal_name,
626                                      sensor_name_len))
627                                 /* Switch to new trigger if appropriate */
628                                 propose_new_trigger(s, name, sensor_name_len);
629                 }
630 }
631
632
633 static void setup_trigger_names (void)
634 {
635         char filename[PATH_MAX];
636         char buf[MAX_NAME_SIZE];
637         int len;
638         int s;
639         int trigger;
640         int ret;
641
642         /* By default, use the name-dev convention that most drivers use */
643         for (s=0; s<sensor_count; s++)
644                 snprintf(sensor_info[s].init_trigger_name,
645                          MAX_NAME_SIZE, "%s-dev%d",
646                          sensor_info[s].internal_name, sensor_info[s].dev_num);
647
648         /* Now have a look to /sys/bus/iio/devices/triggerX entries */
649
650         for (trigger=0; trigger<MAX_TRIGGERS; trigger++) {
651
652                 snprintf(filename, sizeof(filename), TRIGGER_FILE_PATH,trigger);
653
654                 ret = sysfs_read_str(filename, buf, sizeof(buf));
655
656                 if (ret < 0)
657                         break;
658
659                 /* Record initial and any-motion triggers names */
660                 update_sensor_matching_trigger_name(buf);
661         }
662
663         for (s=0; s<sensor_count; s++)
664                 if (sensor_info[s].num_channels) {
665                         ALOGI(  "Sensor %d (%s) default trigger: %s\n", s,
666                                 sensor_info[s].friendly_name,
667                                 sensor_info[s].init_trigger_name);
668                         if (sensor_info[s].motion_trigger_name[0])
669                                 ALOGI(  "Sensor %d (%s) motion trigger: %s\n",
670                                 s, sensor_info[s].friendly_name,
671                                 sensor_info[s].motion_trigger_name);
672                 }
673 }
674
675 static void uncalibrated_gyro_check (void)
676 {
677         unsigned int has_gyr = 0;
678         unsigned int dev_num;
679         int i, c;
680         unsigned int is_poll_sensor;
681         char buf[MAX_NAME_SIZE];
682
683         int cal_idx = 0;
684         int uncal_idx = 0;
685         int catalog_size = CATALOG_SIZE; /* Avoid GCC sign comparison warning */
686
687         /* Checking to see if we have a gyroscope - we can only have uncal if we have the base sensor */
688         for (i=0; i < sensor_count; i++)
689                 if(sensor_catalog[sensor_info[i].catalog_index].type == SENSOR_TYPE_GYROSCOPE)
690                 {
691                         has_gyr=1;
692                         dev_num = sensor_info[i].dev_num;
693                         is_poll_sensor = !sensor_info[i].num_channels;
694                         cal_idx = i;
695                         break;
696                 }
697
698         /*
699          * If we have a gyro we can add the uncalibrated sensor of the same type and
700          * on the same dev_num. We will save indexes for easy finding and also save the
701          * channel specific information.
702          */
703         if (has_gyr)
704                 for (i=0; i<catalog_size; i++)
705                         if (sensor_catalog[i].type == SENSOR_TYPE_GYROSCOPE_UNCALIBRATED) {
706                                 add_sensor(dev_num, i, is_poll_sensor);
707
708                                 uncal_idx = sensor_count - 1; /* Just added uncalibrated sensor */
709
710                                 /* Similar to build_sensor_report_maps */
711                                 for (c = 0; c < sensor_info[uncal_idx].num_channels; c++)
712                                 {
713                                         memcpy( &(sensor_info[uncal_idx].channel[c].type_spec),
714                                                 &(sensor_info[cal_idx].channel[c].type_spec),
715                                                 sizeof(sensor_info[uncal_idx].channel[c].type_spec));
716                                         sensor_info[uncal_idx].channel[c].type_info = sensor_info[cal_idx].channel[c].type_info;
717                                         sensor_info[uncal_idx].channel[c].offset    = sensor_info[cal_idx].channel[c].offset;
718                                         sensor_info[uncal_idx].channel[c].size      = sensor_info[cal_idx].channel[c].size;
719                                 }
720                                 sensor_info[uncal_idx].pair_idx = cal_idx;
721                                 sensor_info[cal_idx].pair_idx = uncal_idx;
722                                 strncpy(sensor_info[uncal_idx].init_trigger_name,
723                                         sensor_info[cal_idx].init_trigger_name,
724                                         MAX_NAME_SIZE);
725                                 strncpy(sensor_info[uncal_idx].motion_trigger_name,
726                                         sensor_info[cal_idx].motion_trigger_name,
727                                         MAX_NAME_SIZE);
728
729                                 /* Add "Uncalibrated " prefix to sensor name */
730                                 strcpy(buf, sensor_info[cal_idx].friendly_name);
731                                 snprintf(sensor_info[uncal_idx].friendly_name,
732                                          MAX_NAME_SIZE,
733                                          "%s %s", "Uncalibrated", buf);
734                                 break;
735                         }
736 }
737
738 void enumerate_sensors (void)
739 {
740         /*
741          * Discover supported sensors and allocate control structures for them.
742          * Multiple sensors can potentially rely on a single iio device (each
743          * using their own channels). We can't have multiple sensors of the same
744          * type on the same device. In case of detection as both a poll-mode
745          * and trigger-based sensor, use the trigger usage mode.
746          */
747         char poll_sensors[CATALOG_SIZE];
748         char trig_sensors[CATALOG_SIZE];
749         int dev_num;
750         unsigned int i;
751         int trig_found;
752
753         for (dev_num=0; dev_num<MAX_DEVICES; dev_num++) {
754                 trig_found = 0;
755
756                 discover_poll_sensors(dev_num, poll_sensors);
757                 discover_trig_sensors(dev_num, trig_sensors);
758
759                 for (i=0; i<CATALOG_SIZE; i++)
760                         if (trig_sensors[i]) {
761                                 add_sensor(dev_num, i, 0);
762                                 trig_found = 1;
763                         }
764                         else
765                                 if (poll_sensors[i])
766                                         add_sensor(dev_num, i, 1);
767
768                 if (trig_found) {
769                         build_sensor_report_maps(dev_num);
770                 }
771         }
772
773         ALOGI("Discovered %d sensors\n", sensor_count);
774
775         /* Set up default - as well as custom - trigger names */
776         setup_trigger_names();
777
778         /* Make sure Android fall backs to its own orientation sensor */
779         orientation_sensor_check();
780
781         /*
782          * Create the uncalibrated counterpart to the compensated gyroscope.
783          * This is is a new sensor type in Android 4.4.
784          */
785         uncalibrated_gyro_check();
786 }
787
788
789 void delete_enumeration_data (void)
790 {
791
792         int i;
793         for (i = 0; i < sensor_count; i++)
794         switch (sensor_catalog[sensor_info[i].catalog_index].type) {
795                 case SENSOR_TYPE_MAGNETIC_FIELD:
796                         if (sensor_info[i].cal_data != NULL) {
797                                 free(sensor_info[i].cal_data);
798                                 sensor_info[i].cal_data = NULL;
799                                 sensor_info[i].cal_level = 0;
800                         }
801                         break;
802                 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
803                 case SENSOR_TYPE_GYROSCOPE:
804                         if (sensor_info[i].cal_data != NULL) {
805                                 free(sensor_info[i].cal_data);
806                                 sensor_info[i].cal_data = NULL;
807                                 sensor_info[i].cal_level = 0;
808                         }
809                         break;
810                         if (sensor_info[i].filter != NULL) {
811                                 free(((struct filter*)sensor_info[i].filter)->x_buff->buff);
812                                 free(((struct filter*)sensor_info[i].filter)->y_buff->buff);
813                                 free(((struct filter*)sensor_info[i].filter)->z_buff->buff);
814                                 free(((struct filter*)sensor_info[i].filter)->x_buff);
815                                 free(((struct filter*)sensor_info[i].filter)->y_buff);
816                                 free(((struct filter*)sensor_info[i].filter)->z_buff);
817                                 free(sensor_info[i].filter);
818                                 sensor_info[i].filter = NULL;
819                         }
820                 default:
821                         break;
822         }
823         /* Reset sensor count */
824         sensor_count = 0;
825 }
826
827
828 int get_sensors_list(   struct sensors_module_t* module,
829                         struct sensor_t const** list)
830 {
831         *list = sensor_desc;
832         return sensor_count;
833 }
834