OSDN Git Service

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