OSDN Git Service

Populate maxDelay field
[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
48 /* We equate sensor handles to indices in these tables */
49
50 struct sensor_t      sensor_desc[MAX_SENSORS];  /* Android-level descriptors */
51 struct sensor_info_t sensor_info[MAX_SENSORS];  /* Internal descriptors      */
52 int sensor_count;                               /* Detected sensors          */
53
54
55 static void add_sensor (int dev_num, int catalog_index, int use_polling)
56 {
57         int s;
58         int sensor_type;
59         int retval;
60         char sysfs_path[PATH_MAX];
61         const char* prefix;
62         float scale;
63         int c;
64         float opt_scale;
65         const char* ch_name;
66         int num_channels;
67         char suffix[MAX_NAME_SIZE + 8];
68
69         if (sensor_count == MAX_SENSORS) {
70                 ALOGE("Too many sensors!\n");
71                 return;
72         }
73
74         sensor_type = sensor_catalog[catalog_index].type;
75
76         /*
77          * At this point we could check that the expected sysfs attributes are
78          * present ; that would enable having multiple catalog entries with the
79          * same sensor type, accomodating different sets of sysfs attributes.
80          */
81
82         s = sensor_count;
83
84         sensor_info[s].dev_num          = dev_num;
85         sensor_info[s].catalog_index    = catalog_index;
86
87         if (use_polling)
88                 sensor_info[s].num_channels = 0;
89         else
90                 sensor_info[s].num_channels =
91                                 sensor_catalog[catalog_index].num_channels;
92
93         prefix = sensor_catalog[catalog_index].tag;
94
95         /*
96          * receiving the illumination sensor calibration inputs from
97          * the Android properties and setting it within sysfs
98          */
99         if (sensor_catalog[catalog_index].type == SENSOR_TYPE_LIGHT) {
100                 retval = sensor_get_illumincalib(s);
101                 if (retval > 0) {
102                         sprintf(sysfs_path, ILLUMINATION_CALIBPATH, dev_num);
103                         sysfs_write_int(sysfs_path, retval);
104                 }
105         }
106
107         /* Read name attribute, if available */
108         sprintf(sysfs_path, NAME_PATH, dev_num);
109         sysfs_read_str(sysfs_path, sensor_info[s].internal_name, MAX_NAME_SIZE);
110
111         /* See if we have general offsets and scale values for this sensor */
112
113         sprintf(sysfs_path, SENSOR_OFFSET_PATH, dev_num, prefix);
114         sysfs_read_float(sysfs_path, &sensor_info[s].offset);
115
116         sprintf(sysfs_path, SENSOR_SCALE_PATH, dev_num, prefix);
117         if (!sysfs_read_float(sysfs_path, &scale)) {
118                 sensor_info[s].scale = scale;
119                 ALOGI("Scale path:%s scale:%f dev_num:%d\n",
120                                         sysfs_path, scale, dev_num);
121         } else {
122                 sensor_info[s].scale = 1;
123
124                 /* Read channel specific scale if any*/
125                 for (c = 0; c < sensor_catalog[catalog_index].num_channels; c++)
126                 {
127                         sprintf(sysfs_path, BASE_PATH "%s", dev_num,
128                            sensor_catalog[catalog_index].channel[c].scale_path);
129
130                         if (!sysfs_read_float(sysfs_path, &scale)) {
131                                 sensor_info[s].channel[c].scale = scale;
132                                 sensor_info[s].scale = 0;
133
134                                 ALOGI(  "Scale path:%s "
135                                         "channel scale:%f dev_num:%d\n",
136                                         sysfs_path, scale, dev_num);
137                         }
138                 }
139         }
140
141         /*
142          * See if we have optional correction scaling factors for each of the
143          * channels of this sensor. These would be expressed using properties
144          * like iio.accel.y.opt_scale = -1. In case of a single channel we also
145          * support things such as iio.temp.opt_scale = -1. Note that this works
146          * for all types of sensors, and whatever transform is selected, on top
147          * of any previous conversions.
148          */
149         num_channels = sensor_catalog[catalog_index].num_channels;
150
151         if (num_channels) {
152                 for (c = 0; c < num_channels; c++) {
153                         opt_scale = 1;
154
155                         ch_name = sensor_catalog[catalog_index].channel[c].name;
156                         sprintf(suffix, "%s.opt_scale", ch_name);
157                         sensor_get_fl_prop(s, suffix, &opt_scale);
158
159                         sensor_info[s].channel[c].opt_scale = opt_scale;
160                 }
161         } else {
162                 opt_scale = 1;
163                 sensor_get_fl_prop(s, "opt_scale", &opt_scale);
164                 sensor_info[s].channel[0].opt_scale = opt_scale;
165         }
166
167         /* Initialize Android-visible descriptor */
168         sensor_desc[s].name             = sensor_get_name(s);
169         sensor_desc[s].vendor           = sensor_get_vendor(s);
170         sensor_desc[s].version          = sensor_get_version(s);
171         sensor_desc[s].handle           = s;
172         sensor_desc[s].type             = sensor_type;
173         sensor_desc[s].maxRange         = sensor_get_max_range(s);
174         sensor_desc[s].resolution       = sensor_get_resolution(s);
175         sensor_desc[s].power            = sensor_get_power(s);
176         sensor_desc[s].stringType = sensor_get_string_type(s);
177
178         /* None of our supported sensors requires a special permission.
179         *  If this will be the case we should implement a sensor_get_perm
180         */
181         sensor_desc[s].requiredPermission = "";
182         sensor_desc[s].flags = sensor_get_flags(s);
183         sensor_desc[s].maxDelay = sensor_get_max_delay(s);
184
185         if (sensor_info[s].internal_name[0] == '\0') {
186                 /*
187                  * In case the kernel-mode driver doesn't expose a name for
188                  * the iio device, use (null)-dev%d as the trigger name...
189                  * This can be considered a kernel-mode iio driver bug.
190                  */
191                 ALOGW("Using null trigger on sensor %d (dev %d)\n", s, dev_num);
192                 strcpy(sensor_info[s].internal_name, "(null)");
193         }
194
195         if (sensor_catalog[catalog_index].type == SENSOR_TYPE_GYROSCOPE ||
196                 sensor_catalog[catalog_index].type == SENSOR_TYPE_GYROSCOPE_UNCALIBRATED) {
197                 struct gyro_cal* calibration_data = calloc(1, sizeof(struct gyro_cal));
198                 sensor_info[s].cal_data = calibration_data;
199         }
200
201         if (sensor_catalog[catalog_index].type == SENSOR_TYPE_MAGNETIC_FIELD) {
202                 struct compass_cal* calibration_data = calloc(1, sizeof(struct compass_cal));
203                 sensor_info[s].cal_data = calibration_data;
204         }
205
206         /* Select one of the available sensor sample processing styles */
207         select_transform(s);
208
209         /* Initialize fields related to sysfs reads offloading */
210         sensor_info[s].thread_data_fd[0]  = -1;
211         sensor_info[s].thread_data_fd[1]  = -1;
212         sensor_info[s].acquisition_thread = -1;
213
214         /* Check if we have a special ordering property on this sensor */
215         if (sensor_get_order(s, sensor_info[s].order))
216                 sensor_info[s].quirks |= QUIRK_FIELD_ORDERING;
217
218         sensor_count++;
219 }
220
221
222 static void discover_poll_sensors (int dev_num, char map[CATALOG_SIZE])
223 {
224         char base_dir[PATH_MAX];
225         DIR *dir;
226         struct dirent *d;
227         unsigned int i;
228         int c;
229
230         memset(map, 0, CATALOG_SIZE);
231
232         snprintf(base_dir, sizeof(base_dir), BASE_PATH, dev_num);
233
234         dir = opendir(base_dir);
235         if (!dir) {
236                 return;
237         }
238
239         /* Enumerate entries in this iio device's base folder */
240
241         while ((d = readdir(dir))) {
242                 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
243                         continue;
244
245                 /* If the name matches a catalog entry, flag it */
246                 for (i = 0; i<CATALOG_SIZE; i++) {
247                 /* This will be added separately later */
248                 if (sensor_catalog[i].type == SENSOR_TYPE_GYROSCOPE_UNCALIBRATED)
249                         continue;
250                 for (c=0; c<sensor_catalog[i].num_channels; c++)
251                         if (!strcmp(d->d_name,sensor_catalog[i].channel[c].raw_path) ||
252                                 !strcmp(d->d_name, sensor_catalog[i].channel[c].input_path)) {
253                                         map[i] = 1;
254                                         break;
255                         }
256                 }
257         }
258
259         closedir(dir);
260 }
261
262
263 static void discover_trig_sensors (int dev_num, char map[CATALOG_SIZE])
264 {
265         char scan_elem_dir[PATH_MAX];
266         DIR *dir;
267         struct dirent *d;
268         unsigned int i;
269
270         memset(map, 0, CATALOG_SIZE);
271
272         /* Enumerate entries in this iio device's scan_elements folder */
273
274         snprintf(scan_elem_dir, sizeof(scan_elem_dir), CHANNEL_PATH, dev_num);
275
276         dir = opendir(scan_elem_dir);
277         if (!dir) {
278                 return;
279         }
280
281         while ((d = readdir(dir))) {
282                 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
283                         continue;
284
285                 /* Compare en entry to known ones and create matching sensors */
286
287                 for (i = 0; i<CATALOG_SIZE; i++) {
288                         if (sensor_catalog[i].type == SENSOR_TYPE_GYROSCOPE_UNCALIBRATED)
289                                 continue;
290                         if (!strcmp(d->d_name,
291                                         sensor_catalog[i].channel[0].en_path)) {
292                                         map[i] = 1;
293                                         break;
294                         }
295                 }
296         }
297
298         closedir(dir);
299 }
300
301
302 static void orientation_sensor_check(void)
303 {
304         /*
305          * If we have accel + gyro + magn but no rotation vector sensor,
306          * SensorService replaces the HAL provided orientation sensor by the
307          * AOSP version... provided we report one. So initialize a virtual
308          * orientation sensor with zero values, which will get replaced. See:
309          * frameworks/native/services/sensorservice/SensorService.cpp, looking
310          * for SENSOR_TYPE_ROTATION_VECTOR; that code should presumably fall
311          * back to mUserSensorList.add instead of replaceAt, but accommodate it.
312          */
313
314         int i;
315         int has_acc = 0;
316         int has_gyr = 0;
317         int has_mag = 0;
318         int has_rot = 0;
319         int has_ori = 0;
320         int catalog_size = CATALOG_SIZE;
321
322         for (i=0; i<sensor_count; i++)
323                 switch (sensor_catalog[sensor_info[i].catalog_index].type) {
324                         case SENSOR_TYPE_ACCELEROMETER:
325                                 has_acc = 1;
326                                 break;
327                         case SENSOR_TYPE_GYROSCOPE:
328                                 has_gyr = 1;
329                                 break;
330                         case SENSOR_TYPE_MAGNETIC_FIELD:
331                                 has_mag = 1;
332                                 break;
333                         case SENSOR_TYPE_ORIENTATION:
334                                 has_ori = 1;
335                                 break;
336                         case SENSOR_TYPE_ROTATION_VECTOR:
337                                 has_rot = 1;
338                                 break;
339                 }
340
341         if (has_acc && has_gyr && has_mag && !has_rot && !has_ori)
342                 for (i=0; i<catalog_size; i++)
343                         if (sensor_catalog[i].type == SENSOR_TYPE_ORIENTATION) {
344                                 ALOGI("Adding placeholder orientation sensor");
345                                 add_sensor(0, i, 1);
346                                 break;
347                         }
348 }
349
350 static void uncalibrated_gyro_check (void)
351 {
352         unsigned int has_gyr = 0;
353         unsigned int dev_num;
354         int i, c;
355         unsigned int is_poll_sensor;
356
357         int cal_idx = 0;
358         int uncal_idx = 0;
359
360         /* Checking to see if we have a gyroscope - we can only have uncal if we have the base sensor */
361         for (i=0; i < sensor_count; i++)
362                 if(sensor_catalog[sensor_info[i].catalog_index].type == SENSOR_TYPE_GYROSCOPE)
363                 {
364                         has_gyr=1;
365                         dev_num = sensor_info[i].dev_num;
366                         is_poll_sensor = !sensor_info[i].num_channels;
367                         cal_idx = i;
368                         break;
369                 }
370
371         /*
372          * If we have a gyro we can add the uncalibrated sensor of the same type and
373          * on the same dev_num. We will save indexes for easy finding and also save the
374          * channel specific information.
375          */
376         if (has_gyr)
377                 for (i=0; i<CATALOG_SIZE; i++)
378                         if (sensor_catalog[i].type == SENSOR_TYPE_GYROSCOPE_UNCALIBRATED) {
379                                 add_sensor(dev_num, i, is_poll_sensor);
380
381                                 uncal_idx = sensor_count - 1; /* Just added uncalibrated sensor */
382
383                                 /* Similar to build_sensor_report_maps */
384                                 for (c = 0; c < sensor_info[uncal_idx].num_channels; c++)
385                                 {
386                                         memcpy( &(sensor_info[uncal_idx].channel[c].type_spec),
387                                                 &(sensor_info[cal_idx].channel[c].type_spec),
388                                                 sizeof(sensor_info[uncal_idx].channel[c].type_spec));
389                                         sensor_info[uncal_idx].channel[c].type_info = sensor_info[cal_idx].channel[c].type_info;
390                                         sensor_info[uncal_idx].channel[c].offset    = sensor_info[cal_idx].channel[c].offset;
391                                         sensor_info[uncal_idx].channel[c].size      = sensor_info[cal_idx].channel[c].size;
392                                 }
393                                 sensor_info[uncal_idx].pair_idx = cal_idx;
394                                 sensor_info[cal_idx].pair_idx = uncal_idx;
395                                 break;
396                         }
397 }
398
399 static void update_sensor_matching_trigger_name (char name[MAX_NAME_SIZE])
400 {
401         /*
402          * Check if we have a sensor matching the specified trigger name,
403          * which should then begin with the sensor name, and end with a number
404          * equal to the iio device number the sensor is associated to. If so,
405          * update the string we're going to write to trigger/current_trigger
406          * when enabling this sensor.
407          */
408
409         int s;
410         int dev_num;
411         int len;
412         char* cursor;
413
414         /*
415          * First determine the iio device number this trigger refers to. We
416          * expect the last few characters (typically one) of the trigger name
417          * to be this number, so perform a few checks.
418          */
419         len = strnlen(name, MAX_NAME_SIZE);
420
421         if (len < 2)
422                 return;
423
424         cursor = name + len - 1;
425
426         if (!isdigit(*cursor))
427                 return;
428
429         while (len && isdigit(*cursor)) {
430                 len--;
431                 cursor--;
432         }
433
434         dev_num = atoi(cursor+1);
435
436         /* See if that matches a sensor */
437         for (s=0; s<sensor_count; s++)
438                 if (sensor_info[s].dev_num == dev_num &&
439                         !strncmp(name, sensor_info[s].internal_name,
440                                 strlen(sensor_info[s].internal_name))) {
441                                 /* Update sensor structure and return */
442                                 strcpy(sensor_info[s].trigger_name, name);
443                                 return;
444                         }
445 }
446
447
448 static void setup_trigger_names (void)
449 {
450         char filename[PATH_MAX];
451         char buf[MAX_NAME_SIZE];
452         int len;
453         int s;
454         int trigger;
455         int ret;
456
457         /* By default, use the name-dev convention that most drivers use */
458         for (s=0; s<sensor_count; s++)
459                 snprintf(sensor_info[s].trigger_name, MAX_NAME_SIZE, "%s-dev%d",
460                         sensor_info[s].internal_name, sensor_info[s].dev_num);
461
462         /* Now have a look to /sys/bus/iio/devices/triggerX entries */
463
464         for (trigger=0; trigger<MAX_TRIGGERS; trigger++) {
465
466                 snprintf(filename, sizeof(filename), TRIGGER_FILE_PATH,trigger);
467
468                 ret = sysfs_read_str(filename, buf, sizeof(buf));
469
470                 if (ret < 0)
471                         break;
472
473                 update_sensor_matching_trigger_name(buf);
474         }
475
476         for (s=0; s<sensor_count; s++)
477                 if (sensor_info[s].num_channels) {
478                         ALOGI(  "Sensor %d (%s) using iio trigger %s\n", s,
479                                 sensor_info[s].friendly_name,
480                                 sensor_info[s].trigger_name);
481                 }
482 }
483
484
485 void enumerate_sensors (void)
486 {
487         /*
488          * Discover supported sensors and allocate control structures for them.
489          * Multiple sensors can potentially rely on a single iio device (each
490          * using their own channels). We can't have multiple sensors of the same
491          * type on the same device. In case of detection as both a poll-mode
492          * and trigger-based sensor, use the trigger usage mode.
493          */
494         char poll_sensors[CATALOG_SIZE];
495         char trig_sensors[CATALOG_SIZE];
496         int dev_num;
497         unsigned int i;
498         int trig_found;
499
500         for (dev_num=0; dev_num<MAX_DEVICES; dev_num++) {
501                 trig_found = 0;
502
503                 discover_poll_sensors(dev_num, poll_sensors);
504                 discover_trig_sensors(dev_num, trig_sensors);
505
506                 for (i=0; i<CATALOG_SIZE; i++)
507                         if (trig_sensors[i]) {
508                                 add_sensor(dev_num, i, 0);
509                                 trig_found = 1;
510                         }
511                         else
512                                 if (poll_sensors[i])
513                                         add_sensor(dev_num, i, 1);
514
515                 if (trig_found) {
516                         build_sensor_report_maps(dev_num);
517                 }
518         }
519
520         ALOGI("Discovered %d sensors\n", sensor_count);
521
522         /* Set up default - as well as custom - trigger names */
523         setup_trigger_names();
524
525         /* Make sure Android fall backs to its own orientation sensor */
526         orientation_sensor_check();
527
528         /* Create the uncalibrated counterpart to the compensated gyroscope;
529          * This is is a new sensor type in Android 4.4 */
530         uncalibrated_gyro_check();
531 }
532
533
534 void delete_enumeration_data (void)
535 {
536
537         int i;
538         for (i = 0; i < sensor_count; i++)
539         switch (sensor_catalog[sensor_info[i].catalog_index].type) {
540                 case SENSOR_TYPE_MAGNETIC_FIELD:
541                 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
542                 case SENSOR_TYPE_GYROSCOPE:
543                         if (sensor_info[i].cal_data != NULL) {
544                                 free(sensor_info[i].cal_data);
545                                 sensor_info[i].cal_data = NULL;
546                                 sensor_info[i].calibrated = 0;
547                         }
548                         break;
549                 default:
550                         break;
551         }
552         /* Reset sensor count */
553         sensor_count = 0;
554 }
555
556
557 int get_sensors_list(   struct sensors_module_t* module,
558                         struct sensor_t const** list)
559 {
560         *list = sensor_desc;
561         return sensor_count;
562 }
563