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 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 DECLARE_VIRTUAL(SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED )
46 #define CATALOG_SIZE ARRAY_SIZE(sensor_catalog)
48 /* ACPI PLD (physical location of device) definitions, as used with sensors */
53 /* We equate sensor handles to indices in these tables */
55 struct sensor_t sensor_desc[MAX_SENSORS]; /* Android-level descriptors */
56 sensor_info_t sensor[MAX_SENSORS]; /* Internal descriptors */
57 int sensor_count; /* Detected sensors */
60 static void setup_properties_from_pld (int s, int panel, int rotation,
64 * Generate suitable order and opt_scale directives from the PLD panel
65 * and rotation codes we got. This can later be superseded by the usual
66 * properties if necessary. Eventually we'll need to replace these
67 * mechanisms by a less convoluted one, such as a 3x3 placement matrix.
74 int angle = rotation * 45;
76 /* Only deal with 3 axis chips for now */
80 if (panel == PANEL_BACK) {
81 /* Chip placed on the back panel ; negate x and z */
87 case 90: /* 90° clockwise: negate y then swap x,y */
92 case 180: /* Upside down: negate x and y */
97 case 270: /* 90° counter clockwise: negate x then swap x,y */
104 sensor[s].order[0] = 1;
105 sensor[s].order[1] = 0;
106 sensor[s].order[2] = 2;
107 sensor[s].quirks |= QUIRK_FIELD_ORDERING;
110 sensor[s].channel[0].opt_scale = x;
111 sensor[s].channel[1].opt_scale = y;
112 sensor[s].channel[2].opt_scale = z;
116 static int is_valid_pld (int panel, int rotation)
118 if (panel != PANEL_FRONT && panel != PANEL_BACK) {
119 ALOGW("Unhandled PLD panel spec: %d\n", panel);
123 /* Only deal with 90° rotations for now */
124 if (rotation < 0 || rotation > 7 || (rotation & 1)) {
125 ALOGW("Unhandled PLD rotation spec: %d\n", rotation);
133 static int read_pld_from_properties (int s, int* panel, int* rotation)
137 if (sensor_get_prop(s, "panel", &p))
140 if (sensor_get_prop(s, "rotation", &r))
143 if (!is_valid_pld(p, r))
149 ALOGI("S%d PLD from properties: panel=%d, rotation=%d\n", s, p, r);
155 static int read_pld_from_sysfs (int s, int dev_num, int* panel, int* rotation)
157 char sysfs_path[PATH_MAX];
160 sprintf(sysfs_path, BASE_PATH "../firmware_node/pld/panel", dev_num);
162 if (sysfs_read_int(sysfs_path, &p))
165 sprintf(sysfs_path, BASE_PATH "../firmware_node/pld/rotation", dev_num);
167 if (sysfs_read_int(sysfs_path, &r))
170 if (!is_valid_pld(p, r))
176 ALOGI("S%d PLD from sysfs: panel=%d, rotation=%d\n", s, p, r);
182 static void decode_placement_information (int dev_num, int num_channels, int s)
185 * See if we have optional "physical location of device" ACPI tags.
186 * We're only interested in panel and rotation specifiers. Use the
187 * .panel and .rotation properties in priority, and the actual ACPI
188 * values as a second source.
194 if (read_pld_from_properties(s, &panel, &rotation) &&
195 read_pld_from_sysfs(s, dev_num, &panel, &rotation))
196 return; /* No PLD data available */
198 /* Map that to field ordering and scaling mechanisms */
199 setup_properties_from_pld(s, panel, rotation, num_channels);
203 static void populate_descriptors (int s, int sensor_type)
205 int32_t min_delay_us;
206 max_delay_t max_delay_us;
208 /* Initialize Android-visible descriptor */
209 sensor_desc[s].name = sensor_get_name(s);
210 sensor_desc[s].vendor = sensor_get_vendor(s);
211 sensor_desc[s].version = sensor_get_version(s);
212 sensor_desc[s].handle = s;
213 sensor_desc[s].type = sensor_type;
215 sensor_desc[s].maxRange = sensor_get_max_range(s);
216 sensor_desc[s].resolution = sensor_get_resolution(s);
217 sensor_desc[s].power = sensor_get_power(s);
218 sensor_desc[s].stringType = sensor_get_string_type(s);
220 /* None of our supported sensors requires a special permission */
221 sensor_desc[s].requiredPermission = "";
223 sensor_desc[s].flags = sensor_get_flags(s);
224 sensor_desc[s].minDelay = sensor_get_min_delay(s);
225 sensor_desc[s].maxDelay = sensor_get_max_delay(s);
227 ALOGV("Sensor %d (%s) type(%d) minD(%d) maxD(%d) flags(%2.2x)\n",
228 s, sensor[s].friendly_name, sensor_desc[s].type,
229 sensor_desc[s].minDelay, sensor_desc[s].maxDelay,
230 sensor_desc[s].flags);
232 /* We currently do not implement batching */
233 sensor_desc[s].fifoReservedEventCount = 0;
234 sensor_desc[s].fifoMaxEventCount = 0;
236 min_delay_us = sensor_desc[s].minDelay;
237 max_delay_us = sensor_desc[s].maxDelay;
239 sensor[s].min_supported_rate = max_delay_us ? 1000000.0 / max_delay_us : 1;
240 sensor[s].max_supported_rate = min_delay_us && min_delay_us != -1 ? 1000000.0 / min_delay_us : 0;
244 static void add_virtual_sensor (int catalog_index)
249 if (sensor_count == MAX_SENSORS) {
250 ALOGE("Too many sensors!\n");
254 sensor_type = sensor_catalog[catalog_index].type;
258 sensor[s].is_virtual = 1;
259 sensor[s].catalog_index = catalog_index;
260 sensor[s].type = sensor_type;
262 populate_descriptors(s, sensor_type);
264 /* Initialize fields related to sysfs reads offloading */
265 sensor[s].thread_data_fd[0] = -1;
266 sensor[s].thread_data_fd[1] = -1;
267 sensor[s].acquisition_thread = -1;
273 static void add_sensor (int dev_num, int catalog_index, int use_polling)
278 char sysfs_path[PATH_MAX];
285 char suffix[MAX_NAME_SIZE + 8];
287 if (sensor_count == MAX_SENSORS) {
288 ALOGE("Too many sensors!\n");
292 sensor_type = sensor_catalog[catalog_index].type;
295 * At this point we could check that the expected sysfs attributes are
296 * present ; that would enable having multiple catalog entries with the
297 * same sensor type, accomodating different sets of sysfs attributes.
302 sensor[s].dev_num = dev_num;
303 sensor[s].catalog_index = catalog_index;
304 sensor[s].type = sensor_type;
305 sensor[s].is_polling = use_polling;
307 num_channels = sensor_catalog[catalog_index].num_channels;
310 sensor[s].num_channels = 0;
312 sensor[s].num_channels = num_channels;
314 prefix = sensor_catalog[catalog_index].tag;
317 * receiving the illumination sensor calibration inputs from
318 * the Android properties and setting it within sysfs
320 if (sensor_type == SENSOR_TYPE_LIGHT) {
321 retval = sensor_get_illumincalib(s);
323 sprintf(sysfs_path, ILLUMINATION_CALIBPATH, dev_num);
324 sysfs_write_int(sysfs_path, retval);
328 /* Read name attribute, if available */
329 sprintf(sysfs_path, NAME_PATH, dev_num);
330 sysfs_read_str(sysfs_path, sensor[s].internal_name, MAX_NAME_SIZE);
332 /* See if we have general offsets and scale values for this sensor */
334 sprintf(sysfs_path, SENSOR_OFFSET_PATH, dev_num, prefix);
335 sysfs_read_float(sysfs_path, &sensor[s].offset);
337 sprintf(sysfs_path, SENSOR_SCALE_PATH, dev_num, prefix);
338 if (!sensor_get_fl_prop(s, "scale", &scale)) {
340 * There is a chip preferred scale specified,
341 * so try to store it in sensor's scale file
343 if (sysfs_write_float(sysfs_path, scale) == -1 && errno == ENOENT) {
344 ALOGE("Failed to store scale[%g] into %s - file is missing", scale, sysfs_path);
345 /* Store failed, try to store the scale into channel specific file */
346 for (c = 0; c < num_channels; c++)
348 sprintf(sysfs_path, BASE_PATH "%s", dev_num,
349 sensor_catalog[catalog_index].channel[c].scale_path);
350 if (sysfs_write_float(sysfs_path, scale) == -1)
351 ALOGE("Failed to store scale[%g] into %s", scale, sysfs_path);
356 sprintf(sysfs_path, SENSOR_SCALE_PATH, dev_num, prefix);
357 if (!sysfs_read_float(sysfs_path, &scale)) {
358 sensor[s].scale = scale;
359 ALOGV("Scale path:%s scale:%g dev_num:%d\n",
360 sysfs_path, scale, dev_num);
364 /* Read channel specific scale if any*/
365 for (c = 0; c < num_channels; c++)
367 sprintf(sysfs_path, BASE_PATH "%s", dev_num,
368 sensor_catalog[catalog_index].channel[c].scale_path);
370 if (!sysfs_read_float(sysfs_path, &scale)) {
371 sensor[s].channel[c].scale = scale;
374 ALOGV( "Scale path:%s "
375 "channel scale:%g dev_num:%d\n",
376 sysfs_path, scale, dev_num);
381 /* Set default scaling - if num_channels is zero, we have one channel */
383 sensor[s].channel[0].opt_scale = 1;
385 for (c = 1; c < num_channels; c++)
386 sensor[s].channel[c].opt_scale = 1;
388 /* Read ACPI _PLD attributes for this sensor, if there are any */
389 decode_placement_information(dev_num, num_channels, s);
392 * See if we have optional correction scaling factors for each of the
393 * channels of this sensor. These would be expressed using properties
394 * like iio.accel.y.opt_scale = -1. In case of a single channel we also
395 * support things such as iio.temp.opt_scale = -1. Note that this works
396 * for all types of sensors, and whatever transform is selected, on top
397 * of any previous conversions.
401 for (c = 0; c < num_channels; c++) {
402 ch_name = sensor_catalog[catalog_index].channel[c].name;
403 sprintf(suffix, "%s.opt_scale", ch_name);
404 if (!sensor_get_fl_prop(s, suffix, &opt_scale))
405 sensor[s].channel[c].opt_scale = opt_scale;
408 if (!sensor_get_fl_prop(s, "opt_scale", &opt_scale))
409 sensor[s].channel[0].opt_scale = opt_scale;
411 populate_descriptors(s, sensor_type);
413 /* Populate the quirks array */
414 sensor_get_quirks(s);
416 if (sensor[s].internal_name[0] == '\0') {
418 * In case the kernel-mode driver doesn't expose a name for
419 * the iio device, use (null)-dev%d as the trigger name...
420 * This can be considered a kernel-mode iio driver bug.
422 ALOGW("Using null trigger on sensor %d (dev %d)\n", s, dev_num);
423 strcpy(sensor[s].internal_name, "(null)");
426 switch (sensor_type) {
427 case SENSOR_TYPE_GYROSCOPE:
428 sensor[s].cal_data = malloc(sizeof(gyro_cal_t));
431 case SENSOR_TYPE_MAGNETIC_FIELD:
432 sensor[s].cal_data = malloc(sizeof(compass_cal_t));
436 sensor[s].max_cal_level = sensor_get_cal_steps(s);
438 /* Select one of the available sensor sample processing styles */
441 /* Initialize fields related to sysfs reads offloading */
442 sensor[s].thread_data_fd[0] = -1;
443 sensor[s].thread_data_fd[1] = -1;
444 sensor[s].acquisition_thread = -1;
446 /* Check if we have a special ordering property on this sensor */
447 if (sensor_get_order(s, sensor[s].order))
448 sensor[s].quirks |= QUIRK_FIELD_ORDERING;
454 static void discover_poll_sensors (int dev_num, char map[CATALOG_SIZE])
456 char base_dir[PATH_MAX];
462 memset(map, 0, CATALOG_SIZE);
464 snprintf(base_dir, sizeof(base_dir), BASE_PATH, dev_num);
466 dir = opendir(base_dir);
471 /* Enumerate entries in this iio device's base folder */
473 while ((d = readdir(dir))) {
474 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
477 /* If the name matches a catalog entry, flag it */
478 for (i = 0; i < CATALOG_SIZE; i++) {
480 /* No discovery for virtual sensors */
481 if (sensor_catalog[i].is_virtual)
484 for (c=0; c<sensor_catalog[i].num_channels; c++)
485 if (!strcmp(d->d_name,sensor_catalog[i].channel[c].raw_path) || !strcmp(d->d_name, sensor_catalog[i].channel[c].input_path)) {
496 static void discover_trig_sensors (int dev_num, char map[CATALOG_SIZE])
498 char scan_elem_dir[PATH_MAX];
503 memset(map, 0, CATALOG_SIZE);
505 /* Enumerate entries in this iio device's scan_elements folder */
507 snprintf(scan_elem_dir, sizeof(scan_elem_dir), CHANNEL_PATH, dev_num);
509 dir = opendir(scan_elem_dir);
514 while ((d = readdir(dir))) {
515 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
518 /* Compare en entry to known ones and create matching sensors */
520 for (i = 0; i<CATALOG_SIZE; i++) {
522 /* No discovery for virtual sensors */
523 if (sensor_catalog[i].is_virtual)
526 if (!strcmp(d->d_name, sensor_catalog[i].channel[0].en_path)) {
537 static void virtual_sensors_check (void)
545 int catalog_size = CATALOG_SIZE;
546 int gyro_cal_idx = 0;
547 int magn_cal_idx = 0;
549 for (i=0; i<sensor_count; i++)
550 switch (sensor[i].type) {
551 case SENSOR_TYPE_ACCELEROMETER:
554 case SENSOR_TYPE_GYROSCOPE:
558 case SENSOR_TYPE_MAGNETIC_FIELD:
562 case SENSOR_TYPE_ORIENTATION:
565 case SENSOR_TYPE_ROTATION_VECTOR:
570 for (i=0; i<catalog_size; i++)
571 switch (sensor_catalog[i].type) {
573 * If we have accel + gyro + magn but no rotation vector sensor,
574 * SensorService replaces the HAL provided orientation sensor by the
575 * AOSP version... provided we report one. So initialize a virtual
576 * orientation sensor with zero values, which will get replaced. See:
577 * frameworks/native/services/sensorservice/SensorService.cpp, looking
578 * for SENSOR_TYPE_ROTATION_VECTOR; that code should presumably fall
579 * back to mUserSensorList.add instead of replaceAt, but accommodate it.
582 case SENSOR_TYPE_ORIENTATION:
583 if (has_acc && has_gyr && has_mag && !has_rot && !has_ori)
586 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
588 sensor[sensor_count].base_count = 1;
589 sensor[sensor_count].base[0] = gyro_cal_idx;
590 add_virtual_sensor(i);
593 case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED:
595 sensor[sensor_count].base_count = 1;
596 sensor[sensor_count].base[0] = magn_cal_idx;
597 add_virtual_sensor(i);
606 static void propose_new_trigger (int s, char trigger_name[MAX_NAME_SIZE],
610 * A new trigger has been enumerated for this sensor. Check if it makes sense to use it over the currently selected one,
611 * and select it if it is so. The format is something like sensor_name-dev0.
614 const char *suffix = trigger_name + sensor_name_len + 1;
616 /* dev is the default, and lowest priority; no need to update */
617 if (!memcmp(suffix, "dev", 3))
620 /* If we found any-motion trigger, record it */
622 if (!memcmp(suffix, "any-motion-", 11)) {
623 strcpy(sensor[s].motion_trigger_name, trigger_name);
628 * It's neither the default "dev" nor an "any-motion" one. Make sure we use this though, as we may not have any other indication of the name
629 * of the trigger to use with this sensor.
631 strcpy(sensor[s].init_trigger_name, trigger_name);
635 static void update_sensor_matching_trigger_name (char name[MAX_NAME_SIZE])
638 * Check if we have a sensor matching the specified trigger name, which should then begin with the sensor name, and end with a number
639 * equal to the iio device number the sensor is associated to. If so, update the string we're going to write to trigger/current_trigger
640 * when enabling this sensor.
650 * First determine the iio device number this trigger refers to. We expect the last few characters (typically one) of the trigger name
651 * to be this number, so perform a few checks.
653 len = strnlen(name, MAX_NAME_SIZE);
658 cursor = name + len - 1;
660 if (!isdigit(*cursor))
663 while (len && isdigit(*cursor)) {
668 dev_num = atoi(cursor+1);
670 /* See if that matches a sensor */
671 for (s=0; s<sensor_count; s++)
672 if (sensor[s].dev_num == dev_num) {
674 sensor_name_len = strlen(sensor[s].internal_name);
676 if (!strncmp(name, sensor[s].internal_name, sensor_name_len))
677 /* Switch to new trigger if appropriate */
678 propose_new_trigger(s, name, sensor_name_len);
683 static void setup_trigger_names (void)
685 char filename[PATH_MAX];
686 char buf[MAX_NAME_SIZE];
692 /* By default, use the name-dev convention that most drivers use */
693 for (s=0; s<sensor_count; s++)
694 snprintf(sensor[s].init_trigger_name, MAX_NAME_SIZE, "%s-dev%d", sensor[s].internal_name, sensor[s].dev_num);
696 /* Now have a look to /sys/bus/iio/devices/triggerX entries */
698 for (trigger=0; trigger<MAX_TRIGGERS; trigger++) {
700 snprintf(filename, sizeof(filename), TRIGGER_FILE_PATH,trigger);
702 ret = sysfs_read_str(filename, buf, sizeof(buf));
707 /* Record initial and any-motion triggers names */
708 update_sensor_matching_trigger_name(buf);
712 * Certain drivers expose only motion triggers even though they should be continous. For these, use the default trigger name as the motion
713 * trigger. The code generating intermediate events is dependent on motion_trigger_name being set to a non empty string.
716 for (s=0; s<sensor_count; s++)
717 if ((sensor[s].quirks & QUIRK_TERSE_DRIVER) && sensor[s].motion_trigger_name[0] == '\0')
718 strcpy(sensor[s].motion_trigger_name, sensor[s].init_trigger_name);
720 for (s=0; s<sensor_count; s++)
721 if (!sensor[s].is_polling) {
722 ALOGI("Sensor %d (%s) default trigger: %s\n", s, sensor[s].friendly_name, sensor[s].init_trigger_name);
723 if (sensor[s].motion_trigger_name[0])
724 ALOGI("Sensor %d (%s) motion trigger: %s\n", s, sensor[s].friendly_name, sensor[s].motion_trigger_name);
728 void enumerate_sensors (void)
731 * Discover supported sensors and allocate control structures for them. Multiple sensors can potentially rely on a single iio device (each
732 * using their own channels). We can't have multiple sensors of the same type on the same device. In case of detection as both a poll-mode
733 * and trigger-based sensor, use the trigger usage mode.
735 char poll_sensors[CATALOG_SIZE];
736 char trig_sensors[CATALOG_SIZE];
741 for (dev_num=0; dev_num<MAX_DEVICES; dev_num++) {
744 discover_poll_sensors(dev_num, poll_sensors);
745 discover_trig_sensors(dev_num, trig_sensors);
747 for (i=0; i<CATALOG_SIZE; i++)
748 if (trig_sensors[i]) {
749 add_sensor(dev_num, i, 0);
754 add_sensor(dev_num, i, 1);
757 build_sensor_report_maps(dev_num);
760 ALOGI("Discovered %d sensors\n", sensor_count);
762 /* Set up default - as well as custom - trigger names */
763 setup_trigger_names();
765 virtual_sensors_check();
769 void delete_enumeration_data (void)
772 for (i = 0; i < sensor_count; i++)
773 if (sensor[i].cal_data) {
774 free(sensor[i].cal_data);
775 sensor[i].cal_data = NULL;
776 sensor[i].cal_level = 0;
779 /* Reset sensor count */
784 int get_sensors_list (__attribute__((unused)) struct sensors_module_t* module,
785 struct sensor_t const** list)