OSDN Git Service

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