OSDN Git Service

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