2 * Copyright (C) 2014 Intel Corporation.
9 #include <hardware/sensors.h>
10 #include "enumeration.h"
11 #include "description.h"
13 #include "transform.h"
14 #include "description.h"
16 #include "calibration.h"
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.
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.
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")
45 #define CATALOG_SIZE ARRAY_SIZE(sensor_catalog)
48 /* We equate sensor handles to indices in these tables */
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 */
55 static void add_sensor (int dev_num, int catalog_index, int use_polling)
60 char sysfs_path[PATH_MAX];
67 char suffix[MAX_NAME_SIZE + 8];
69 if (sensor_count == MAX_SENSORS) {
70 ALOGE("Too many sensors!\n");
74 sensor_type = sensor_catalog[catalog_index].type;
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.
84 sensor_info[s].dev_num = dev_num;
85 sensor_info[s].catalog_index = catalog_index;
88 sensor_info[s].num_channels = 0;
90 sensor_info[s].num_channels =
91 sensor_catalog[catalog_index].num_channels;
93 prefix = sensor_catalog[catalog_index].tag;
96 * receiving the illumination sensor calibration inputs from
97 * the Android properties and setting it within sysfs
99 if (sensor_catalog[catalog_index].type == SENSOR_TYPE_LIGHT) {
100 retval = sensor_get_illumincalib(s);
102 sprintf(sysfs_path, ILLUMINATION_CALIBPATH, dev_num);
103 sysfs_write_int(sysfs_path, retval);
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);
111 /* See if we have general offsets and scale values for this sensor */
113 sprintf(sysfs_path, SENSOR_OFFSET_PATH, dev_num, prefix);
114 sysfs_read_float(sysfs_path, &sensor_info[s].offset);
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);
122 sensor_info[s].scale = 1;
124 /* Read channel specific scale if any*/
125 for (c = 0; c < sensor_catalog[catalog_index].num_channels; c++)
127 sprintf(sysfs_path, BASE_PATH "%s", dev_num,
128 sensor_catalog[catalog_index].channel[c].scale_path);
130 if (!sysfs_read_float(sysfs_path, &scale)) {
131 sensor_info[s].channel[c].scale = scale;
132 sensor_info[s].scale = 0;
134 ALOGI( "Scale path:%s "
135 "channel scale:%f dev_num:%d\n",
136 sysfs_path, scale, dev_num);
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.
149 num_channels = sensor_catalog[catalog_index].num_channels;
152 for (c = 0; c < num_channels; c++) {
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);
159 sensor_info[s].channel[c].opt_scale = opt_scale;
163 sensor_get_fl_prop(s, "opt_scale", &opt_scale);
164 sensor_info[s].channel[0].opt_scale = opt_scale;
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);
178 /* None of our supported sensors requires a special permission.
179 * If this will be the case we should implement a sensor_get_perm
181 sensor_desc[s].requiredPermission = "";
182 sensor_desc[s].flags = sensor_get_flags(s);
183 sensor_desc[s].maxDelay = sensor_get_max_delay(s);
185 if (sensor_info[s].internal_name[0] == '\0') {
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.
191 ALOGW("Using null trigger on sensor %d (dev %d)\n", s, dev_num);
192 strcpy(sensor_info[s].internal_name, "(null)");
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;
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;
206 /* Select one of the available sensor sample processing styles */
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;
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;
222 static void discover_poll_sensors (int dev_num, char map[CATALOG_SIZE])
224 char base_dir[PATH_MAX];
230 memset(map, 0, CATALOG_SIZE);
232 snprintf(base_dir, sizeof(base_dir), BASE_PATH, dev_num);
234 dir = opendir(base_dir);
239 /* Enumerate entries in this iio device's base folder */
241 while ((d = readdir(dir))) {
242 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
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)
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)) {
263 static void discover_trig_sensors (int dev_num, char map[CATALOG_SIZE])
265 char scan_elem_dir[PATH_MAX];
270 memset(map, 0, CATALOG_SIZE);
272 /* Enumerate entries in this iio device's scan_elements folder */
274 snprintf(scan_elem_dir, sizeof(scan_elem_dir), CHANNEL_PATH, dev_num);
276 dir = opendir(scan_elem_dir);
281 while ((d = readdir(dir))) {
282 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
285 /* Compare en entry to known ones and create matching sensors */
287 for (i = 0; i<CATALOG_SIZE; i++) {
288 if (sensor_catalog[i].type == SENSOR_TYPE_GYROSCOPE_UNCALIBRATED)
290 if (!strcmp(d->d_name,
291 sensor_catalog[i].channel[0].en_path)) {
302 static void orientation_sensor_check(void)
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.
320 int catalog_size = CATALOG_SIZE;
322 for (i=0; i<sensor_count; i++)
323 switch (sensor_catalog[sensor_info[i].catalog_index].type) {
324 case SENSOR_TYPE_ACCELEROMETER:
327 case SENSOR_TYPE_GYROSCOPE:
330 case SENSOR_TYPE_MAGNETIC_FIELD:
333 case SENSOR_TYPE_ORIENTATION:
336 case SENSOR_TYPE_ROTATION_VECTOR:
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");
350 static void uncalibrated_gyro_check (void)
352 unsigned int has_gyr = 0;
353 unsigned int dev_num;
355 unsigned int is_poll_sensor;
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)
365 dev_num = sensor_info[i].dev_num;
366 is_poll_sensor = !sensor_info[i].num_channels;
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.
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);
381 uncal_idx = sensor_count - 1; /* Just added uncalibrated sensor */
383 /* Similar to build_sensor_report_maps */
384 for (c = 0; c < sensor_info[uncal_idx].num_channels; c++)
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;
393 strncpy(sensor_info[uncal_idx].trigger_name,
394 sensor_info[cal_idx].trigger_name,
396 sensor_info[uncal_idx].pair_idx = cal_idx;
397 sensor_info[cal_idx].pair_idx = uncal_idx;
402 static int is_continuous (int s)
404 /* Is sensor s of the continous trigger type kind? */
406 int catalog_index = sensor_info[s].catalog_index;
407 int sensor_type = sensor_catalog[catalog_index].type;
409 switch (sensor_type) {
410 case SENSOR_TYPE_ACCELEROMETER:
411 case SENSOR_TYPE_MAGNETIC_FIELD:
412 case SENSOR_TYPE_ORIENTATION:
413 case SENSOR_TYPE_GYROSCOPE:
414 case SENSOR_TYPE_PRESSURE:
415 case SENSOR_TYPE_GRAVITY:
416 case SENSOR_TYPE_LINEAR_ACCELERATION:
417 case SENSOR_TYPE_ROTATION_VECTOR:
418 case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED:
419 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
420 case SENSOR_TYPE_GEOMAGNETIC_ROTATION_VECTOR:
429 static void propose_new_trigger (int s, char trigger_name[MAX_NAME_SIZE],
433 * A new trigger has been enumerated for this sensor. Check if it makes
434 * sense to use it over the currently selected one, and select it if it
435 * is so. The format is something like sensor_name-dev0.
438 const char *suffix = trigger_name + sensor_name_len + 1;
440 /* dev is the default, and lowest priority; no need to update */
441 if (!memcmp(suffix, "dev", 3))
445 * Anything else is higher priority. However if we already found an
446 * any-motion trigger, don't select anything else.
449 if (!memcmp(sensor_info[s].trigger_name + sensor_name_len + 1,
454 * If we're switching to an any-motion trigger, force the sensor to
455 * automatic intermediate event generation mode, at least if it is of a
456 * continuously firing sensor type.
459 if (!memcmp(suffix, "any-motion-", 11) && is_continuous(s))
460 sensor_info[s].quirks |= QUIRK_TERSE_DRIVER;
462 /* Update the trigger name to use for this sensor */
463 strcpy(sensor_info[s].trigger_name, trigger_name);
467 static void update_sensor_matching_trigger_name (char name[MAX_NAME_SIZE])
470 * Check if we have a sensor matching the specified trigger name,
471 * which should then begin with the sensor name, and end with a number
472 * equal to the iio device number the sensor is associated to. If so,
473 * update the string we're going to write to trigger/current_trigger
474 * when enabling this sensor.
484 * First determine the iio device number this trigger refers to. We
485 * expect the last few characters (typically one) of the trigger name
486 * to be this number, so perform a few checks.
488 len = strnlen(name, MAX_NAME_SIZE);
493 cursor = name + len - 1;
495 if (!isdigit(*cursor))
498 while (len && isdigit(*cursor)) {
503 dev_num = atoi(cursor+1);
505 /* See if that matches a sensor */
506 for (s=0; s<sensor_count; s++)
507 if (sensor_info[s].dev_num == dev_num) {
509 sensor_name_len = strlen(sensor_info[s].internal_name);
512 sensor_info[s].internal_name,
514 /* Switch to new trigger if appropriate */
515 propose_new_trigger(s, name, sensor_name_len);
520 static void setup_trigger_names (void)
522 char filename[PATH_MAX];
523 char buf[MAX_NAME_SIZE];
529 /* By default, use the name-dev convention that most drivers use */
530 for (s=0; s<sensor_count; s++)
531 snprintf(sensor_info[s].trigger_name, MAX_NAME_SIZE, "%s-dev%d",
532 sensor_info[s].internal_name, sensor_info[s].dev_num);
534 /* Now have a look to /sys/bus/iio/devices/triggerX entries */
536 for (trigger=0; trigger<MAX_TRIGGERS; trigger++) {
538 snprintf(filename, sizeof(filename), TRIGGER_FILE_PATH,trigger);
540 ret = sysfs_read_str(filename, buf, sizeof(buf));
545 update_sensor_matching_trigger_name(buf);
548 for (s=0; s<sensor_count; s++)
549 if (sensor_info[s].num_channels) {
550 ALOGI( "Sensor %d (%s) using iio trigger %s\n", s,
551 sensor_info[s].friendly_name,
552 sensor_info[s].trigger_name);
557 void enumerate_sensors (void)
560 * Discover supported sensors and allocate control structures for them.
561 * Multiple sensors can potentially rely on a single iio device (each
562 * using their own channels). We can't have multiple sensors of the same
563 * type on the same device. In case of detection as both a poll-mode
564 * and trigger-based sensor, use the trigger usage mode.
566 char poll_sensors[CATALOG_SIZE];
567 char trig_sensors[CATALOG_SIZE];
572 for (dev_num=0; dev_num<MAX_DEVICES; dev_num++) {
575 discover_poll_sensors(dev_num, poll_sensors);
576 discover_trig_sensors(dev_num, trig_sensors);
578 for (i=0; i<CATALOG_SIZE; i++)
579 if (trig_sensors[i]) {
580 add_sensor(dev_num, i, 0);
585 add_sensor(dev_num, i, 1);
588 build_sensor_report_maps(dev_num);
592 ALOGI("Discovered %d sensors\n", sensor_count);
594 /* Set up default - as well as custom - trigger names */
595 setup_trigger_names();
597 /* Make sure Android fall backs to its own orientation sensor */
598 orientation_sensor_check();
600 /* Create the uncalibrated counterpart to the compensated gyroscope;
601 * This is is a new sensor type in Android 4.4 */
602 uncalibrated_gyro_check();
606 void delete_enumeration_data (void)
610 for (i = 0; i < sensor_count; i++)
611 switch (sensor_catalog[sensor_info[i].catalog_index].type) {
612 case SENSOR_TYPE_MAGNETIC_FIELD:
613 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
614 case SENSOR_TYPE_GYROSCOPE:
615 if (sensor_info[i].cal_data != NULL) {
616 free(sensor_info[i].cal_data);
617 sensor_info[i].cal_data = NULL;
618 sensor_info[i].cal_level = 0;
624 /* Reset sensor count */
629 int get_sensors_list( struct sensors_module_t* module,
630 struct sensor_t const** list)