OSDN Git Service

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