OSDN Git Service

IRDA-2849: Add support for optional calibscale properties
[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         DECLARE_SENSOR3("accel",      SENSOR_TYPE_ACCELEROMETER,  "x", "y", "z")
33         DECLARE_SENSOR3("anglvel",    SENSOR_TYPE_GYROSCOPE,      "x", "y", "z")
34         DECLARE_SENSOR3("magn",       SENSOR_TYPE_MAGNETIC_FIELD, "x", "y", "z")
35         DECLARE_SENSOR1("intensity",  SENSOR_TYPE_LIGHT,          "both"       )
36         DECLARE_SENSOR0("illuminance",SENSOR_TYPE_LIGHT                        )
37         DECLARE_SENSOR3("incli",      SENSOR_TYPE_ORIENTATION,    "x", "y", "z")
38         DECLARE_SENSOR4("rot",        SENSOR_TYPE_ROTATION_VECTOR,
39                                          "quat_x", "quat_y", "quat_z", "quat_w")
40         DECLARE_SENSOR0("temp",       SENSOR_TYPE_AMBIENT_TEMPERATURE          )
41         DECLARE_SENSOR0("proximity",  SENSOR_TYPE_PROXIMITY                    )
42         DECLARE_VIRTUAL(SENSOR_TYPE_GYROSCOPE_UNCALIBRATED                     )
43         DECLARE_VIRTUAL(SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED                 )
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 sensor_info_t   sensor[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[s].order[0] = 1;
105                 sensor[s].order[1] = 0;
106                 sensor[s].order[2] = 2;
107                 sensor[s].quirks |= QUIRK_FIELD_ORDERING;
108         }
109
110         sensor[s].channel[0].opt_scale = x;
111         sensor[s].channel[1].opt_scale = y;
112         sensor[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 populate_descriptors (int s, int sensor_type)
204 {
205         int32_t         min_delay_us;
206         max_delay_t     max_delay_us;
207
208         /* Initialize Android-visible descriptor */
209         sensor_desc[s].name             = sensor_get_name(s);
210         sensor_desc[s].vendor           = sensor_get_vendor(s);
211         sensor_desc[s].version          = sensor_get_version(s);
212         sensor_desc[s].handle           = s;
213         sensor_desc[s].type             = sensor_type;
214
215         sensor_desc[s].maxRange         = sensor_get_max_range(s);
216         sensor_desc[s].resolution       = sensor_get_resolution(s);
217         sensor_desc[s].power            = sensor_get_power(s);
218         sensor_desc[s].stringType       = sensor_get_string_type(s);
219
220         /* None of our supported sensors requires a special permission */
221         sensor_desc[s].requiredPermission = "";
222
223         sensor_desc[s].flags = sensor_get_flags(s);
224         sensor_desc[s].minDelay = sensor_get_min_delay(s);
225         sensor_desc[s].maxDelay = sensor_get_max_delay(s);
226
227         ALOGV("Sensor %d (%s) type(%d) minD(%d) maxD(%d) flags(%2.2x)\n",
228                 s, sensor[s].friendly_name, sensor_desc[s].type,
229                 sensor_desc[s].minDelay, sensor_desc[s].maxDelay,
230                 sensor_desc[s].flags);
231
232         /* We currently do not implement batching */
233         sensor_desc[s].fifoReservedEventCount = 0;
234         sensor_desc[s].fifoMaxEventCount = 0;
235
236         min_delay_us = sensor_desc[s].minDelay;
237         max_delay_us = sensor_desc[s].maxDelay;
238
239         sensor[s].min_supported_rate = max_delay_us ? 1000000.0 / max_delay_us : 1;
240         sensor[s].max_supported_rate = min_delay_us && min_delay_us != -1 ? 1000000.0 / min_delay_us : 0;
241 }
242
243
244 static void add_virtual_sensor (int catalog_index)
245 {
246         int s;
247         int sensor_type;
248
249         if (sensor_count == MAX_SENSORS) {
250                 ALOGE("Too many sensors!\n");
251                 return;
252         }
253
254         sensor_type = sensor_catalog[catalog_index].type;
255
256         s = sensor_count;
257
258         sensor[s].is_virtual = 1;
259         sensor[s].catalog_index = catalog_index;
260         sensor[s].type          = sensor_type;
261
262         populate_descriptors(s, sensor_type);
263
264         /* Initialize fields related to sysfs reads offloading */
265         sensor[s].thread_data_fd[0]  = -1;
266         sensor[s].thread_data_fd[1]  = -1;
267         sensor[s].acquisition_thread = -1;
268
269         sensor_count++;
270 }
271
272
273 static void add_sensor (int dev_num, int catalog_index, int use_polling)
274 {
275         int s;
276         int sensor_type;
277         int retval;
278         char sysfs_path[PATH_MAX];
279         const char* prefix;
280         float scale;
281         int c;
282         float opt_scale;
283         const char* ch_name;
284         int num_channels;
285         char suffix[MAX_NAME_SIZE + 8];
286         int calib_bias;
287
288         if (sensor_count == MAX_SENSORS) {
289                 ALOGE("Too many sensors!\n");
290                 return;
291         }
292
293         sensor_type = sensor_catalog[catalog_index].type;
294
295         /*
296          * At this point we could check that the expected sysfs attributes are
297          * present ; that would enable having multiple catalog entries with the
298          * same sensor type, accomodating different sets of sysfs attributes.
299          */
300
301         s = sensor_count;
302
303         sensor[s].dev_num       = dev_num;
304         sensor[s].catalog_index = catalog_index;
305         sensor[s].type          = sensor_type;
306         sensor[s].is_polling    = use_polling;
307
308         num_channels = sensor_catalog[catalog_index].num_channels;
309
310         if (use_polling)
311                 sensor[s].num_channels = 0;
312         else
313                 sensor[s].num_channels = num_channels;
314
315         prefix = sensor_catalog[catalog_index].tag;
316
317         /*
318          * receiving the illumination sensor calibration inputs from
319          * the Android properties and setting it within sysfs
320          */
321         if (sensor_type == SENSOR_TYPE_LIGHT) {
322                 retval = sensor_get_illumincalib(s);
323                 if (retval > 0) {
324                         sprintf(sysfs_path, ILLUMINATION_CALIBPATH, dev_num);
325                         sysfs_write_int(sysfs_path, retval);
326                 }
327         }
328
329         /*
330          * See if we have optional calibration biases for each of the channels of this sensor. These would be expressed using properties like
331          * 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
332          * relevant calibbias sysfs file if that file can be located and then used internally by the iio sensor driver.
333          */
334
335         if (num_channels) {
336                 for (c = 0; c < num_channels; c++) {
337                         ch_name = sensor_catalog[catalog_index].channel[c].name;
338                         sprintf(suffix, "%s.calib_bias", ch_name);
339                         if (!sensor_get_prop(s, suffix, &calib_bias) && calib_bias) {
340                                 sprintf(suffix, "%s_%s", prefix, sensor_catalog[catalog_index].channel[c].name);
341                                 sprintf(sysfs_path, SENSOR_CALIB_BIAS_PATH, dev_num, suffix);
342                                 sysfs_write_int(sysfs_path, calib_bias);
343                         }
344                 }
345         } else
346                 if (!sensor_get_prop(s, "calib_bias", &calib_bias) && calib_bias) {
347                                 sprintf(sysfs_path, SENSOR_CALIB_BIAS_PATH, dev_num, prefix);
348                                 sysfs_write_int(sysfs_path, calib_bias);
349                         }
350
351         /* Read name attribute, if available */
352         sprintf(sysfs_path, NAME_PATH, dev_num);
353         sysfs_read_str(sysfs_path, sensor[s].internal_name, MAX_NAME_SIZE);
354
355         /* See if we have general offsets and scale values for this sensor */
356
357         sprintf(sysfs_path, SENSOR_OFFSET_PATH, dev_num, prefix);
358         sysfs_read_float(sysfs_path, &sensor[s].offset);
359
360         sprintf(sysfs_path, SENSOR_SCALE_PATH, dev_num, prefix);
361         if (!sensor_get_fl_prop(s, "scale", &scale)) {
362                 /*
363                  * There is a chip preferred scale specified,
364                  * so try to store it in sensor's scale file
365                  */
366                 if (sysfs_write_float(sysfs_path, scale) == -1 && errno == ENOENT) {
367                         ALOGE("Failed to store scale[%g] into %s - file is missing", scale, sysfs_path);
368                         /* Store failed, try to store the scale into channel specific file */
369                         for (c = 0; c < num_channels; c++)
370                         {
371                                 sprintf(sysfs_path, BASE_PATH "%s", dev_num,
372                                         sensor_catalog[catalog_index].channel[c].scale_path);
373                                 if (sysfs_write_float(sysfs_path, scale) == -1)
374                                         ALOGE("Failed to store scale[%g] into %s", scale, sysfs_path);
375                         }
376                 }
377         }
378
379         sprintf(sysfs_path, SENSOR_SCALE_PATH, dev_num, prefix);
380         if (!sysfs_read_float(sysfs_path, &scale)) {
381                 sensor[s].scale = scale;
382                 ALOGV("Scale path:%s scale:%g dev_num:%d\n",
383                                         sysfs_path, scale, dev_num);
384         } else {
385                 sensor[s].scale = 1;
386
387                 /* Read channel specific scale if any*/
388                 for (c = 0; c < num_channels; c++)
389                 {
390                         sprintf(sysfs_path, BASE_PATH "%s", dev_num,
391                            sensor_catalog[catalog_index].channel[c].scale_path);
392
393                         if (!sysfs_read_float(sysfs_path, &scale)) {
394                                 sensor[s].channel[c].scale = scale;
395                                 sensor[s].scale = 0;
396
397                                 ALOGV(  "Scale path:%s "
398                                         "channel scale:%g dev_num:%d\n",
399                                         sysfs_path, scale, dev_num);
400                         }
401                 }
402         }
403
404         /* Set default scaling - if num_channels is zero, we have one channel */
405
406         sensor[s].channel[0].opt_scale = 1;
407
408         for (c = 1; c < num_channels; c++)
409                 sensor[s].channel[c].opt_scale = 1;
410
411         /* Read ACPI _PLD attributes for this sensor, if there are any */
412         decode_placement_information(dev_num, num_channels, s);
413
414         /*
415          * See if we have optional correction scaling factors for each of the
416          * channels of this sensor. These would be expressed using properties
417          * like iio.accel.y.opt_scale = -1. In case of a single channel we also
418          * support things such as iio.temp.opt_scale = -1. Note that this works
419          * for all types of sensors, and whatever transform is selected, on top
420          * of any previous conversions.
421          */
422
423         if (num_channels) {
424                 for (c = 0; c < num_channels; c++) {
425                         ch_name = sensor_catalog[catalog_index].channel[c].name;
426                         sprintf(suffix, "%s.opt_scale", ch_name);
427                         if (!sensor_get_fl_prop(s, suffix, &opt_scale))
428                                 sensor[s].channel[c].opt_scale = opt_scale;
429                 }
430         } else
431                 if (!sensor_get_fl_prop(s, "opt_scale", &opt_scale))
432                         sensor[s].channel[0].opt_scale = opt_scale;
433
434         populate_descriptors(s, sensor_type);
435
436         /* Populate the quirks array */
437         sensor_get_quirks(s);
438
439         if (sensor[s].internal_name[0] == '\0') {
440                 /*
441                  * In case the kernel-mode driver doesn't expose a name for
442                  * the iio device, use (null)-dev%d as the trigger name...
443                  * This can be considered a kernel-mode iio driver bug.
444                  */
445                 ALOGW("Using null trigger on sensor %d (dev %d)\n", s, dev_num);
446                 strcpy(sensor[s].internal_name, "(null)");
447         }
448
449         switch (sensor_type) {
450                 case SENSOR_TYPE_GYROSCOPE:
451                         sensor[s].cal_data = malloc(sizeof(gyro_cal_t));
452                         break;
453
454                 case SENSOR_TYPE_MAGNETIC_FIELD:
455                         sensor[s].cal_data = malloc(sizeof(compass_cal_t));
456                         break;
457         }
458
459         sensor[s].max_cal_level = sensor_get_cal_steps(s);
460
461         /* Select one of the available sensor sample processing styles */
462         select_transform(s);
463
464         /* Initialize fields related to sysfs reads offloading */
465         sensor[s].thread_data_fd[0]  = -1;
466         sensor[s].thread_data_fd[1]  = -1;
467         sensor[s].acquisition_thread = -1;
468
469         /* Check if we have a special ordering property on this sensor */
470         if (sensor_get_order(s, sensor[s].order))
471                 sensor[s].quirks |= QUIRK_FIELD_ORDERING;
472
473         sensor_count++;
474 }
475
476
477 static void discover_poll_sensors (int dev_num, char map[CATALOG_SIZE])
478 {
479         char base_dir[PATH_MAX];
480         DIR *dir;
481         struct dirent *d;
482         unsigned int i;
483         int c;
484
485         memset(map, 0, CATALOG_SIZE);
486
487         snprintf(base_dir, sizeof(base_dir), BASE_PATH, dev_num);
488
489         dir = opendir(base_dir);
490         if (!dir) {
491                 return;
492         }
493
494         /* Enumerate entries in this iio device's base folder */
495
496         while ((d = readdir(dir))) {
497                 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
498                         continue;
499
500                 /* If the name matches a catalog entry, flag it */
501                 for (i = 0; i < CATALOG_SIZE; i++) {
502
503                         /* No discovery for virtual sensors */
504                         if (sensor_catalog[i].is_virtual)
505                                 continue;
506
507                         for (c=0; c<sensor_catalog[i].num_channels; c++)
508                                 if (!strcmp(d->d_name,sensor_catalog[i].channel[c].raw_path) || !strcmp(d->d_name, sensor_catalog[i].channel[c].input_path)) {
509                                         map[i] = 1;
510                                         break;
511                         }
512                 }
513         }
514
515         closedir(dir);
516 }
517
518
519 static void discover_trig_sensors (int dev_num, char map[CATALOG_SIZE])
520 {
521         char scan_elem_dir[PATH_MAX];
522         DIR *dir;
523         struct dirent *d;
524         unsigned int i;
525
526         memset(map, 0, CATALOG_SIZE);
527
528         /* Enumerate entries in this iio device's scan_elements folder */
529
530         snprintf(scan_elem_dir, sizeof(scan_elem_dir), CHANNEL_PATH, dev_num);
531
532         dir = opendir(scan_elem_dir);
533         if (!dir) {
534                 return;
535         }
536
537         while ((d = readdir(dir))) {
538                 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
539                         continue;
540
541                 /* Compare en entry to known ones and create matching sensors */
542
543                 for (i = 0; i<CATALOG_SIZE; i++) {
544
545                         /* No discovery for virtual sensors */
546                         if (sensor_catalog[i].is_virtual)
547                                 continue;
548
549                         if (!strcmp(d->d_name, sensor_catalog[i].channel[0].en_path)) {
550                                         map[i] = 1;
551                                         break;
552                         }
553                 }
554         }
555
556         closedir(dir);
557 }
558
559
560 static void virtual_sensors_check (void)
561 {
562         int i;
563         int has_acc = 0;
564         int has_gyr = 0;
565         int has_mag = 0;
566         int has_rot = 0;
567         int has_ori = 0;
568         int catalog_size = CATALOG_SIZE;
569         int gyro_cal_idx = 0;
570         int magn_cal_idx = 0;
571
572         for (i=0; i<sensor_count; i++)
573                 switch (sensor[i].type) {
574                         case SENSOR_TYPE_ACCELEROMETER:
575                                 has_acc = 1;
576                                 break;
577                         case SENSOR_TYPE_GYROSCOPE:
578                                 has_gyr = 1;
579                                 gyro_cal_idx = i;
580                                 break;
581                         case SENSOR_TYPE_MAGNETIC_FIELD:
582                                 has_mag = 1;
583                                 magn_cal_idx = i;
584                                 break;
585                         case SENSOR_TYPE_ORIENTATION:
586                                 has_ori = 1;
587                                 break;
588                         case SENSOR_TYPE_ROTATION_VECTOR:
589                                 has_rot = 1;
590                                 break;
591                 }
592
593         for (i=0; i<catalog_size; i++)
594                 switch (sensor_catalog[i].type) {
595                         /*
596                         * If we have accel + gyro + magn but no rotation vector sensor,
597                         * SensorService replaces the HAL provided orientation sensor by the
598                         * AOSP version... provided we report one. So initialize a virtual
599                         * orientation sensor with zero values, which will get replaced. See:
600                         * frameworks/native/services/sensorservice/SensorService.cpp, looking
601                         * for SENSOR_TYPE_ROTATION_VECTOR; that code should presumably fall
602                         * back to mUserSensorList.add instead of replaceAt, but accommodate it.
603                         */
604
605                         case SENSOR_TYPE_ORIENTATION:
606                                 if (has_acc && has_gyr && has_mag && !has_rot && !has_ori)
607                                         add_sensor(0, i, 1);
608                                 break;
609                         case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
610                                 if (has_gyr) {
611                                         sensor[sensor_count].base_count = 1;
612                                         sensor[sensor_count].base[0] = gyro_cal_idx;
613                                         add_virtual_sensor(i);
614                                 }
615                                 break;
616                         case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED:
617                                 if (has_mag) {
618                                         sensor[sensor_count].base_count = 1;
619                                         sensor[sensor_count].base[0] = magn_cal_idx;
620                                         add_virtual_sensor(i);
621                                 }
622                                 break;
623                         default:
624                         break;
625                 }
626 }
627
628
629 static void propose_new_trigger (int s, char trigger_name[MAX_NAME_SIZE],
630                                  int sensor_name_len)
631 {
632         /*
633          * A new trigger has been enumerated for this sensor. Check if it makes sense to use it over the currently selected one,
634          *  and select it if it is so. The format is something like sensor_name-dev0.
635          */
636
637         const char *suffix = trigger_name + sensor_name_len + 1;
638
639         /* dev is the default, and lowest priority; no need to update */
640         if (!memcmp(suffix, "dev", 3))
641                 return;
642
643         /* If we found any-motion trigger, record it */
644
645         if (!memcmp(suffix, "any-motion-", 11)) {
646                 strcpy(sensor[s].motion_trigger_name, trigger_name);
647                 return;
648         }
649
650         /*
651          * 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
652          * of the trigger to use with this sensor.
653          */
654         strcpy(sensor[s].init_trigger_name, trigger_name);
655 }
656
657
658 static void update_sensor_matching_trigger_name (char name[MAX_NAME_SIZE])
659 {
660         /*
661          * Check if we have a sensor matching the specified trigger name, which should then begin with the sensor name, and end with a number
662          * 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
663          * when enabling this sensor.
664          */
665
666         int s;
667         int dev_num;
668         int len;
669         char* cursor;
670         int sensor_name_len;
671
672         /*
673          * First determine the iio device number this trigger refers to. We expect the last few characters (typically one) of the trigger name
674          * to be this number, so perform a few checks.
675          */
676         len = strnlen(name, MAX_NAME_SIZE);
677
678         if (len < 2)
679                 return;
680
681         cursor = name + len - 1;
682
683         if (!isdigit(*cursor))
684                 return;
685
686         while (len && isdigit(*cursor)) {
687                 len--;
688                 cursor--;
689         }
690
691         dev_num = atoi(cursor+1);
692
693         /* See if that matches a sensor */
694         for (s=0; s<sensor_count; s++)
695                 if (sensor[s].dev_num == dev_num) {
696
697                         sensor_name_len = strlen(sensor[s].internal_name);
698
699                         if (!strncmp(name, sensor[s].internal_name, sensor_name_len))
700                                 /* Switch to new trigger if appropriate */
701                                 propose_new_trigger(s, name, sensor_name_len);
702                 }
703 }
704
705
706 static void setup_trigger_names (void)
707 {
708         char filename[PATH_MAX];
709         char buf[MAX_NAME_SIZE];
710         int len;
711         int s;
712         int trigger;
713         int ret;
714
715         /* By default, use the name-dev convention that most drivers use */
716         for (s=0; s<sensor_count; s++)
717                 snprintf(sensor[s].init_trigger_name, MAX_NAME_SIZE, "%s-dev%d", sensor[s].internal_name, sensor[s].dev_num);
718
719         /* Now have a look to /sys/bus/iio/devices/triggerX entries */
720
721         for (trigger=0; trigger<MAX_TRIGGERS; trigger++) {
722
723                 snprintf(filename, sizeof(filename), TRIGGER_FILE_PATH,trigger);
724
725                 ret = sysfs_read_str(filename, buf, sizeof(buf));
726
727                 if (ret < 0)
728                         break;
729
730                 /* Record initial and any-motion triggers names */
731                 update_sensor_matching_trigger_name(buf);
732         }
733
734         /*
735          * Certain drivers expose only motion triggers even though they should be continous. For these, use the default trigger name as the motion
736          * trigger. The code generating intermediate events is dependent on motion_trigger_name being set to a non empty string.
737          */
738
739         for (s=0; s<sensor_count; s++)
740                 if ((sensor[s].quirks & QUIRK_TERSE_DRIVER) && sensor[s].motion_trigger_name[0] == '\0')
741                         strcpy(sensor[s].motion_trigger_name, sensor[s].init_trigger_name);
742
743         for (s=0; s<sensor_count; s++)
744                 if (!sensor[s].is_polling) {
745                         ALOGI("Sensor %d (%s) default trigger: %s\n", s, sensor[s].friendly_name, sensor[s].init_trigger_name);
746                         if (sensor[s].motion_trigger_name[0])
747                                 ALOGI("Sensor %d (%s) motion trigger: %s\n", s, sensor[s].friendly_name, sensor[s].motion_trigger_name);
748                 }
749 }
750
751 void enumerate_sensors (void)
752 {
753         /*
754          * Discover supported sensors and allocate control structures for them. Multiple sensors can potentially rely on a single iio device (each
755          * 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
756          * and trigger-based sensor, use the trigger usage mode.
757          */
758         char poll_sensors[CATALOG_SIZE];
759         char trig_sensors[CATALOG_SIZE];
760         int dev_num;
761         unsigned int i;
762         int trig_found;
763
764         for (dev_num=0; dev_num<MAX_DEVICES; dev_num++) {
765                 trig_found = 0;
766
767                 discover_poll_sensors(dev_num, poll_sensors);
768                 discover_trig_sensors(dev_num, trig_sensors);
769
770                 for (i=0; i<CATALOG_SIZE; i++)
771                         if (trig_sensors[i]) {
772                                 add_sensor(dev_num, i, 0);
773                                 trig_found = 1;
774                         }
775                         else
776                                 if (poll_sensors[i])
777                                         add_sensor(dev_num, i, 1);
778
779                 if (trig_found)
780                         build_sensor_report_maps(dev_num);
781         }
782
783         ALOGI("Discovered %d sensors\n", sensor_count);
784
785         /* Set up default - as well as custom - trigger names */
786         setup_trigger_names();
787
788         virtual_sensors_check();
789 }
790
791
792 void delete_enumeration_data (void)
793 {
794         int i;
795         for (i = 0; i < sensor_count; i++)
796                 if (sensor[i].cal_data) {
797                         free(sensor[i].cal_data);
798                         sensor[i].cal_data = NULL;
799                         sensor[i].cal_level = 0;
800                 }
801
802         /* Reset sensor count */
803         sensor_count = 0;
804 }
805
806
807 int get_sensors_list (__attribute__((unused)) struct sensors_module_t* module,
808                       struct sensor_t const** list)
809 {
810         *list = sensor_desc;
811         return sensor_count;
812 }
813