OSDN Git Service

b5464f7404a7f28ee2146c969a9817743acb777a
[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 struct 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_SENSOR3("anglvel",      SENSOR_TYPE_GYROSCOPE_UNCALIBRATED, "x", "y", "z")
43 };
44
45 #define CATALOG_SIZE    ARRAY_SIZE(sensor_catalog)
46
47 /* ACPI PLD (physical location of device) definitions, as used with sensors */
48
49 #define PANEL_FRONT     4
50 #define PANEL_BACK      5
51
52 /* We equate sensor handles to indices in these tables */
53
54 struct sensor_t      sensor_desc[MAX_SENSORS];  /* Android-level descriptors */
55 struct sensor_info_t sensor_info[MAX_SENSORS];  /* Internal descriptors      */
56 int sensor_count;                               /* Detected sensors          */
57
58
59 static void setup_properties_from_pld(int s, int panel, int rotation,
60                                       int num_channels)
61 {
62         /*
63          * Generate suitable order and opt_scale directives from the PLD panel
64          * and rotation codes we got. This can later be superseded by the usual
65          * properties if necessary. Eventually we'll need to replace these
66          * mechanisms by a less convoluted one, such as a 3x3 placement matrix.
67          */
68
69         int x = 1;
70         int y = 1;
71         int z = 1;
72         int xy_swap = 0;
73         int angle = rotation * 45;
74
75         /* Only deal with 3 axis chips for now */
76         if (num_channels < 3)
77                 return;
78
79         if (panel == PANEL_BACK) {
80                 /* Chip placed on the back panel ; negate x and z */
81                 x = -x;
82                 z = -z;
83         }
84
85         switch (angle) {
86                 case 90: /* 90° clockwise: negate y then swap x,y */
87                         xy_swap = 1;
88                         y = -y;
89                         break;
90
91                 case 180: /* Upside down: negate x and y */
92                         x = -x;
93                         y = -y;
94                         break;
95
96                 case 270: /* 90° counter clockwise: negate x then swap x,y */
97                         x = -x;
98                         xy_swap = 1;
99                         break;
100         }
101
102         if (xy_swap) {
103                 sensor_info[s].order[0] = 1;
104                 sensor_info[s].order[1] = 0;
105                 sensor_info[s].order[2] = 2;
106                 sensor_info[s].quirks |= QUIRK_FIELD_ORDERING;
107         }
108
109         sensor_info[s].channel[0].opt_scale = x;
110         sensor_info[s].channel[1].opt_scale = y;
111         sensor_info[s].channel[2].opt_scale = z;
112 }
113
114
115 static int is_valid_pld (int panel, int rotation)
116 {
117         if (panel != PANEL_FRONT && panel != PANEL_BACK) {
118                 ALOGW("Unhandled PLD panel spec: %d\n", panel);
119                 return 0;
120         }
121
122         /* Only deal with 90° rotations for now */
123         if (rotation < 0 || rotation > 7 || (rotation & 1)) {
124                 ALOGW("Unhandled PLD rotation spec: %d\n", rotation);
125                 return 0;
126         }
127
128         return 1;
129 }
130
131
132 static int read_pld_from_properties (int s, int* panel, int* rotation)
133 {
134         int p, r;
135
136         if (sensor_get_prop(s, "panel", &p))
137                 return -1;
138
139         if (sensor_get_prop(s, "rotation", &r))
140                 return -1;
141
142         if (!is_valid_pld(p, r))
143                 return -1;
144
145         *panel = p;
146         *rotation = r;
147
148         ALOGI("S%d PLD from properties: panel=%d, rotation=%d\n", s, p, r);
149
150         return 0;
151 }
152
153
154 static int read_pld_from_sysfs (int s, int dev_num, int* panel, int* rotation)
155 {
156         char sysfs_path[PATH_MAX];
157         int p,r;
158
159         sprintf(sysfs_path, BASE_PATH "../firmware_node/pld/panel", dev_num);
160
161         if (sysfs_read_int(sysfs_path, &p))
162                 return -1;
163
164         sprintf(sysfs_path, BASE_PATH "../firmware_node/pld/rotation", dev_num);
165
166         if (sysfs_read_int(sysfs_path, &r))
167                 return -1;
168
169         if (!is_valid_pld(p, r))
170                 return -1;
171
172         *panel = p;
173         *rotation = r;
174
175         ALOGI("S%d PLD from sysfs: panel=%d, rotation=%d\n", s, p, r);
176
177         return 0;
178 }
179
180
181 static void decode_placement_information (int dev_num, int num_channels, int s)
182 {
183         /*
184          * See if we have optional "physical location of device" ACPI tags.
185          * We're only interested in panel and rotation specifiers. Use the
186          * .panel and .rotation properties in priority, and the actual ACPI
187          * values as a second source.
188          */
189
190         int panel;
191         int rotation;
192
193         if (read_pld_from_properties(s, &panel, &rotation) &&
194                 read_pld_from_sysfs(s, dev_num, &panel, &rotation))
195                         return; /* No PLD data available */
196
197         /* Map that to field ordering and scaling mechanisms */
198         setup_properties_from_pld(s, panel, rotation, num_channels);
199 }
200
201
202 static void add_sensor (int dev_num, int catalog_index, int use_polling)
203 {
204         int s;
205         int sensor_type;
206         int retval;
207         char sysfs_path[PATH_MAX];
208         const char* prefix;
209         float scale;
210         int c;
211         float opt_scale;
212         const char* ch_name;
213         int num_channels;
214         char suffix[MAX_NAME_SIZE + 8];
215
216         if (sensor_count == MAX_SENSORS) {
217                 ALOGE("Too many sensors!\n");
218                 return;
219         }
220
221         sensor_type = sensor_catalog[catalog_index].type;
222
223         /*
224          * At this point we could check that the expected sysfs attributes are
225          * present ; that would enable having multiple catalog entries with the
226          * same sensor type, accomodating different sets of sysfs attributes.
227          */
228
229         s = sensor_count;
230
231         sensor_info[s].dev_num          = dev_num;
232         sensor_info[s].catalog_index    = catalog_index;
233
234         num_channels = sensor_catalog[catalog_index].num_channels;
235
236         if (use_polling)
237                 sensor_info[s].num_channels = 0;
238         else
239                 sensor_info[s].num_channels = num_channels;
240
241         prefix = sensor_catalog[catalog_index].tag;
242
243         /*
244          * receiving the illumination sensor calibration inputs from
245          * the Android properties and setting it within sysfs
246          */
247         if (sensor_catalog[catalog_index].type == SENSOR_TYPE_LIGHT) {
248                 retval = sensor_get_illumincalib(s);
249                 if (retval > 0) {
250                         sprintf(sysfs_path, ILLUMINATION_CALIBPATH, dev_num);
251                         sysfs_write_int(sysfs_path, retval);
252                 }
253         }
254
255         /* Read name attribute, if available */
256         sprintf(sysfs_path, NAME_PATH, dev_num);
257         sysfs_read_str(sysfs_path, sensor_info[s].internal_name, MAX_NAME_SIZE);
258
259         /* See if we have general offsets and scale values for this sensor */
260
261         sprintf(sysfs_path, SENSOR_OFFSET_PATH, dev_num, prefix);
262         sysfs_read_float(sysfs_path, &sensor_info[s].offset);
263
264         sprintf(sysfs_path, SENSOR_SCALE_PATH, dev_num, prefix);
265         if (!sysfs_read_float(sysfs_path, &scale)) {
266                 sensor_info[s].scale = scale;
267                 ALOGI("Scale path:%s scale:%f dev_num:%d\n",
268                                         sysfs_path, scale, dev_num);
269         } else {
270                 sensor_info[s].scale = 1;
271
272                 /* Read channel specific scale if any*/
273                 for (c = 0; c < num_channels; c++)
274                 {
275                         sprintf(sysfs_path, BASE_PATH "%s", dev_num,
276                            sensor_catalog[catalog_index].channel[c].scale_path);
277
278                         if (!sysfs_read_float(sysfs_path, &scale)) {
279                                 sensor_info[s].channel[c].scale = scale;
280                                 sensor_info[s].scale = 0;
281
282                                 ALOGI(  "Scale path:%s "
283                                         "channel scale:%f dev_num:%d\n",
284                                         sysfs_path, scale, dev_num);
285                         }
286                 }
287         }
288
289         /* Set default scaling - if num_channels is zero, we have one channel */
290
291         sensor_info[s].channel[0].opt_scale = 1;
292
293         for (c = 1; c < num_channels; c++)
294                 sensor_info[s].channel[c].opt_scale = 1;
295
296         /* Read ACPI _PLD attributes for this sensor, if there are any */
297         decode_placement_information(dev_num, num_channels, s);
298
299         /*
300          * See if we have optional correction scaling factors for each of the
301          * channels of this sensor. These would be expressed using properties
302          * like iio.accel.y.opt_scale = -1. In case of a single channel we also
303          * support things such as iio.temp.opt_scale = -1. Note that this works
304          * for all types of sensors, and whatever transform is selected, on top
305          * of any previous conversions.
306          */
307
308         if (num_channels) {
309                 for (c = 0; c < num_channels; c++) {
310                         ch_name = sensor_catalog[catalog_index].channel[c].name;
311                         sprintf(suffix, "%s.opt_scale", ch_name);
312                         if (!sensor_get_fl_prop(s, suffix, &opt_scale))
313                                 sensor_info[s].channel[c].opt_scale = opt_scale;
314                 }
315         } else
316                 if (!sensor_get_fl_prop(s, "opt_scale", &opt_scale))
317                         sensor_info[s].channel[0].opt_scale = opt_scale;
318
319         /* Initialize Android-visible descriptor */
320         sensor_desc[s].name             = sensor_get_name(s);
321         sensor_desc[s].vendor           = sensor_get_vendor(s);
322         sensor_desc[s].version          = sensor_get_version(s);
323         sensor_desc[s].handle           = s;
324         sensor_desc[s].type             = sensor_type;
325         sensor_desc[s].maxRange         = sensor_get_max_range(s);
326         sensor_desc[s].resolution       = sensor_get_resolution(s);
327         sensor_desc[s].power            = sensor_get_power(s);
328         sensor_desc[s].stringType = sensor_get_string_type(s);
329
330         /* None of our supported sensors requires a special permission.
331         *  If this will be the case we should implement a sensor_get_perm
332         */
333         sensor_desc[s].requiredPermission = "";
334         sensor_desc[s].flags = sensor_get_flags(s);
335         sensor_desc[s].maxDelay = sensor_get_max_delay(s);
336
337         if (sensor_info[s].internal_name[0] == '\0') {
338                 /*
339                  * In case the kernel-mode driver doesn't expose a name for
340                  * the iio device, use (null)-dev%d as the trigger name...
341                  * This can be considered a kernel-mode iio driver bug.
342                  */
343                 ALOGW("Using null trigger on sensor %d (dev %d)\n", s, dev_num);
344                 strcpy(sensor_info[s].internal_name, "(null)");
345         }
346
347         if (sensor_type == SENSOR_TYPE_GYROSCOPE ||
348                 sensor_type == SENSOR_TYPE_GYROSCOPE_UNCALIBRATED) {
349                 struct gyro_cal* calibration_data = calloc(1, sizeof(struct gyro_cal));
350                 sensor_info[s].cal_data = calibration_data;
351         }
352
353         if (sensor_type == SENSOR_TYPE_MAGNETIC_FIELD) {
354                 struct compass_cal* calibration_data = calloc(1, sizeof(struct compass_cal));
355                 sensor_info[s].cal_data = calibration_data;
356         }
357
358         /* Select one of the available sensor sample processing styles */
359         select_transform(s);
360
361         /* Initialize fields related to sysfs reads offloading */
362         sensor_info[s].thread_data_fd[0]  = -1;
363         sensor_info[s].thread_data_fd[1]  = -1;
364         sensor_info[s].acquisition_thread = -1;
365
366         /* Check if we have a special ordering property on this sensor */
367         if (sensor_get_order(s, sensor_info[s].order))
368                 sensor_info[s].quirks |= QUIRK_FIELD_ORDERING;
369
370         sensor_count++;
371 }
372
373
374 static void discover_poll_sensors (int dev_num, char map[CATALOG_SIZE])
375 {
376         char base_dir[PATH_MAX];
377         DIR *dir;
378         struct dirent *d;
379         unsigned int i;
380         int c;
381
382         memset(map, 0, CATALOG_SIZE);
383
384         snprintf(base_dir, sizeof(base_dir), BASE_PATH, dev_num);
385
386         dir = opendir(base_dir);
387         if (!dir) {
388                 return;
389         }
390
391         /* Enumerate entries in this iio device's base folder */
392
393         while ((d = readdir(dir))) {
394                 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
395                         continue;
396
397                 /* If the name matches a catalog entry, flag it */
398                 for (i = 0; i<CATALOG_SIZE; i++) {
399                 /* This will be added separately later */
400                 if (sensor_catalog[i].type == SENSOR_TYPE_GYROSCOPE_UNCALIBRATED)
401                         continue;
402                 for (c=0; c<sensor_catalog[i].num_channels; c++)
403                         if (!strcmp(d->d_name,sensor_catalog[i].channel[c].raw_path) ||
404                                 !strcmp(d->d_name, sensor_catalog[i].channel[c].input_path)) {
405                                         map[i] = 1;
406                                         break;
407                         }
408                 }
409         }
410
411         closedir(dir);
412 }
413
414
415 static void discover_trig_sensors (int dev_num, char map[CATALOG_SIZE])
416 {
417         char scan_elem_dir[PATH_MAX];
418         DIR *dir;
419         struct dirent *d;
420         unsigned int i;
421
422         memset(map, 0, CATALOG_SIZE);
423
424         /* Enumerate entries in this iio device's scan_elements folder */
425
426         snprintf(scan_elem_dir, sizeof(scan_elem_dir), CHANNEL_PATH, dev_num);
427
428         dir = opendir(scan_elem_dir);
429         if (!dir) {
430                 return;
431         }
432
433         while ((d = readdir(dir))) {
434                 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
435                         continue;
436
437                 /* Compare en entry to known ones and create matching sensors */
438
439                 for (i = 0; i<CATALOG_SIZE; i++) {
440                         if (sensor_catalog[i].type == SENSOR_TYPE_GYROSCOPE_UNCALIBRATED)
441                                 continue;
442                         if (!strcmp(d->d_name,
443                                         sensor_catalog[i].channel[0].en_path)) {
444                                         map[i] = 1;
445                                         break;
446                         }
447                 }
448         }
449
450         closedir(dir);
451 }
452
453
454 static void orientation_sensor_check(void)
455 {
456         /*
457          * If we have accel + gyro + magn but no rotation vector sensor,
458          * SensorService replaces the HAL provided orientation sensor by the
459          * AOSP version... provided we report one. So initialize a virtual
460          * orientation sensor with zero values, which will get replaced. See:
461          * frameworks/native/services/sensorservice/SensorService.cpp, looking
462          * for SENSOR_TYPE_ROTATION_VECTOR; that code should presumably fall
463          * back to mUserSensorList.add instead of replaceAt, but accommodate it.
464          */
465
466         int i;
467         int has_acc = 0;
468         int has_gyr = 0;
469         int has_mag = 0;
470         int has_rot = 0;
471         int has_ori = 0;
472         int catalog_size = CATALOG_SIZE;
473
474         for (i=0; i<sensor_count; i++)
475                 switch (sensor_catalog[sensor_info[i].catalog_index].type) {
476                         case SENSOR_TYPE_ACCELEROMETER:
477                                 has_acc = 1;
478                                 break;
479                         case SENSOR_TYPE_GYROSCOPE:
480                                 has_gyr = 1;
481                                 break;
482                         case SENSOR_TYPE_MAGNETIC_FIELD:
483                                 has_mag = 1;
484                                 break;
485                         case SENSOR_TYPE_ORIENTATION:
486                                 has_ori = 1;
487                                 break;
488                         case SENSOR_TYPE_ROTATION_VECTOR:
489                                 has_rot = 1;
490                                 break;
491                 }
492
493         if (has_acc && has_gyr && has_mag && !has_rot && !has_ori)
494                 for (i=0; i<catalog_size; i++)
495                         if (sensor_catalog[i].type == SENSOR_TYPE_ORIENTATION) {
496                                 ALOGI("Adding placeholder orientation sensor");
497                                 add_sensor(0, i, 1);
498                                 break;
499                         }
500 }
501
502 static void uncalibrated_gyro_check (void)
503 {
504         unsigned int has_gyr = 0;
505         unsigned int dev_num;
506         int i, c;
507         unsigned int is_poll_sensor;
508
509         int cal_idx = 0;
510         int uncal_idx = 0;
511
512         /* Checking to see if we have a gyroscope - we can only have uncal if we have the base sensor */
513         for (i=0; i < sensor_count; i++)
514                 if(sensor_catalog[sensor_info[i].catalog_index].type == SENSOR_TYPE_GYROSCOPE)
515                 {
516                         has_gyr=1;
517                         dev_num = sensor_info[i].dev_num;
518                         is_poll_sensor = !sensor_info[i].num_channels;
519                         cal_idx = i;
520                         break;
521                 }
522
523         /*
524          * If we have a gyro we can add the uncalibrated sensor of the same type and
525          * on the same dev_num. We will save indexes for easy finding and also save the
526          * channel specific information.
527          */
528         if (has_gyr)
529                 for (i=0; i<CATALOG_SIZE; i++)
530                         if (sensor_catalog[i].type == SENSOR_TYPE_GYROSCOPE_UNCALIBRATED) {
531                                 add_sensor(dev_num, i, is_poll_sensor);
532
533                                 uncal_idx = sensor_count - 1; /* Just added uncalibrated sensor */
534
535                                 /* Similar to build_sensor_report_maps */
536                                 for (c = 0; c < sensor_info[uncal_idx].num_channels; c++)
537                                 {
538                                         memcpy( &(sensor_info[uncal_idx].channel[c].type_spec),
539                                                 &(sensor_info[cal_idx].channel[c].type_spec),
540                                                 sizeof(sensor_info[uncal_idx].channel[c].type_spec));
541                                         sensor_info[uncal_idx].channel[c].type_info = sensor_info[cal_idx].channel[c].type_info;
542                                         sensor_info[uncal_idx].channel[c].offset    = sensor_info[cal_idx].channel[c].offset;
543                                         sensor_info[uncal_idx].channel[c].size      = sensor_info[cal_idx].channel[c].size;
544                                 }
545                                 strncpy(sensor_info[uncal_idx].trigger_name,
546                                         sensor_info[cal_idx].trigger_name,
547                                         MAX_NAME_SIZE);
548                                 sensor_info[uncal_idx].pair_idx = cal_idx;
549                                 sensor_info[cal_idx].pair_idx = uncal_idx;
550                                 break;
551                         }
552 }
553
554 static int is_continuous (int s)
555 {
556         /* Is sensor s of the continous trigger type kind? */
557
558         int catalog_index = sensor_info[s].catalog_index;
559         int sensor_type = sensor_catalog[catalog_index].type;
560
561         switch (sensor_type) {
562                 case SENSOR_TYPE_ACCELEROMETER:
563                 case SENSOR_TYPE_MAGNETIC_FIELD:
564                 case SENSOR_TYPE_ORIENTATION:
565                 case SENSOR_TYPE_GYROSCOPE:
566                 case SENSOR_TYPE_PRESSURE:
567                 case SENSOR_TYPE_GRAVITY:
568                 case SENSOR_TYPE_LINEAR_ACCELERATION:
569                 case SENSOR_TYPE_ROTATION_VECTOR:
570                 case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED:
571                 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
572                 case SENSOR_TYPE_GEOMAGNETIC_ROTATION_VECTOR:
573                         return 1;
574
575                 default:
576                         return 0;
577         }
578 }
579
580
581 static void propose_new_trigger (int s, char trigger_name[MAX_NAME_SIZE],
582                                  int sensor_name_len)
583 {
584         /*
585          * A new trigger has been enumerated for this sensor. Check if it makes
586          * sense to use it over the currently selected one, and select it if it
587          * is so. The format is something like sensor_name-dev0.
588          */
589
590         const char *suffix = trigger_name + sensor_name_len + 1;
591
592         /* dev is the default, and lowest priority; no need to update */
593         if (!memcmp(suffix, "dev", 3))
594                 return;
595
596         /*
597          * Anything else is higher priority. However if we already found an
598          * any-motion trigger, don't select anything else.
599          */
600
601         if (!memcmp(sensor_info[s].trigger_name + sensor_name_len + 1,
602                     "any-motion-", 11))
603                 return;
604
605         /*
606          * If we're switching to an any-motion trigger, force the sensor to
607          * automatic intermediate event generation mode, at least if it is of a
608          * continuously firing sensor type.
609          */
610
611         if (!memcmp(suffix, "any-motion-", 11) && is_continuous(s))
612                 sensor_info[s].quirks |= QUIRK_TERSE_DRIVER;
613
614         /* Update the trigger name to use for this sensor */
615         strcpy(sensor_info[s].trigger_name, trigger_name);
616 }
617
618
619 static void update_sensor_matching_trigger_name (char name[MAX_NAME_SIZE])
620 {
621         /*
622          * Check if we have a sensor matching the specified trigger name,
623          * which should then begin with the sensor name, and end with a number
624          * equal to the iio device number the sensor is associated to. If so,
625          * update the string we're going to write to trigger/current_trigger
626          * when enabling this sensor.
627          */
628
629         int s;
630         int dev_num;
631         int len;
632         char* cursor;
633         int sensor_name_len;
634
635         /*
636          * First determine the iio device number this trigger refers to. We
637          * expect the last few characters (typically one) of the trigger name
638          * to be this number, so perform a few checks.
639          */
640         len = strnlen(name, MAX_NAME_SIZE);
641
642         if (len < 2)
643                 return;
644
645         cursor = name + len - 1;
646
647         if (!isdigit(*cursor))
648                 return;
649
650         while (len && isdigit(*cursor)) {
651                 len--;
652                 cursor--;
653         }
654
655         dev_num = atoi(cursor+1);
656
657         /* See if that matches a sensor */
658         for (s=0; s<sensor_count; s++)
659                 if (sensor_info[s].dev_num == dev_num) {
660
661                         sensor_name_len = strlen(sensor_info[s].internal_name);
662
663                         if (!strncmp(name,
664                                      sensor_info[s].internal_name,
665                                      sensor_name_len))
666                                 /* Switch to new trigger if appropriate */
667                                 propose_new_trigger(s, name, sensor_name_len);
668                 }
669 }
670
671
672 static void setup_trigger_names (void)
673 {
674         char filename[PATH_MAX];
675         char buf[MAX_NAME_SIZE];
676         int len;
677         int s;
678         int trigger;
679         int ret;
680
681         /* By default, use the name-dev convention that most drivers use */
682         for (s=0; s<sensor_count; s++)
683                 snprintf(sensor_info[s].trigger_name, MAX_NAME_SIZE, "%s-dev%d",
684                         sensor_info[s].internal_name, sensor_info[s].dev_num);
685
686         /* Now have a look to /sys/bus/iio/devices/triggerX entries */
687
688         for (trigger=0; trigger<MAX_TRIGGERS; trigger++) {
689
690                 snprintf(filename, sizeof(filename), TRIGGER_FILE_PATH,trigger);
691
692                 ret = sysfs_read_str(filename, buf, sizeof(buf));
693
694                 if (ret < 0)
695                         break;
696
697                 update_sensor_matching_trigger_name(buf);
698         }
699
700         for (s=0; s<sensor_count; s++)
701                 if (sensor_info[s].num_channels) {
702                         ALOGI(  "Sensor %d (%s) using iio trigger %s\n", s,
703                                 sensor_info[s].friendly_name,
704                                 sensor_info[s].trigger_name);
705                 }
706 }
707
708
709 void enumerate_sensors (void)
710 {
711         /*
712          * Discover supported sensors and allocate control structures for them.
713          * Multiple sensors can potentially rely on a single iio device (each
714          * using their own channels). We can't have multiple sensors of the same
715          * type on the same device. In case of detection as both a poll-mode
716          * and trigger-based sensor, use the trigger usage mode.
717          */
718         char poll_sensors[CATALOG_SIZE];
719         char trig_sensors[CATALOG_SIZE];
720         int dev_num;
721         unsigned int i;
722         int trig_found;
723
724         for (dev_num=0; dev_num<MAX_DEVICES; dev_num++) {
725                 trig_found = 0;
726
727                 discover_poll_sensors(dev_num, poll_sensors);
728                 discover_trig_sensors(dev_num, trig_sensors);
729
730                 for (i=0; i<CATALOG_SIZE; i++)
731                         if (trig_sensors[i]) {
732                                 add_sensor(dev_num, i, 0);
733                                 trig_found = 1;
734                         }
735                         else
736                                 if (poll_sensors[i])
737                                         add_sensor(dev_num, i, 1);
738
739                 if (trig_found) {
740                         build_sensor_report_maps(dev_num);
741                 }
742         }
743
744         ALOGI("Discovered %d sensors\n", sensor_count);
745
746         /* Set up default - as well as custom - trigger names */
747         setup_trigger_names();
748
749         /* Make sure Android fall backs to its own orientation sensor */
750         orientation_sensor_check();
751
752         /* Create the uncalibrated counterpart to the compensated gyroscope;
753          * This is is a new sensor type in Android 4.4 */
754         uncalibrated_gyro_check();
755 }
756
757
758 void delete_enumeration_data (void)
759 {
760
761         int i;
762         for (i = 0; i < sensor_count; i++)
763         switch (sensor_catalog[sensor_info[i].catalog_index].type) {
764                 case SENSOR_TYPE_MAGNETIC_FIELD:
765                 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
766                 case SENSOR_TYPE_GYROSCOPE:
767                         if (sensor_info[i].cal_data != NULL) {
768                                 free(sensor_info[i].cal_data);
769                                 sensor_info[i].cal_data = NULL;
770                                 sensor_info[i].cal_level = 0;
771                         }
772                         break;
773                 default:
774                         break;
775         }
776         /* Reset sensor count */
777         sensor_count = 0;
778 }
779
780
781 int get_sensors_list(   struct sensors_module_t* module,
782                         struct sensor_t const** list)
783 {
784         *list = sensor_desc;
785         return sensor_count;
786 }
787