OSDN Git Service

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