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)
47 /* ACPI PLD (physical location of device) definitions, as used with sensors */
52 /* We equate sensor handles to indices in these tables */
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 */
59 static void setup_properties_from_pld(int s, int panel, int rotation,
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.
73 int angle = rotation * 45;
75 /* Only deal with 3 axis chips for now */
79 if (panel == PANEL_BACK) {
80 /* Chip placed on the back panel ; negate x and z */
86 case 90: /* 90° clockwise: negate y then swap x,y */
91 case 180: /* Upside down: negate x and y */
96 case 270: /* 90° counter clockwise: negate x then swap x,y */
103 sensor_info[s].order[0] = 1;
104 sensor_info[s].order[1] = 0;
105 sensor_info[s].order[2] = 2;
106 sensor_info[s].quirks |= QUIRK_FIELD_ORDERING;
109 sensor_info[s].channel[0].opt_scale = x;
110 sensor_info[s].channel[1].opt_scale = y;
111 sensor_info[s].channel[2].opt_scale = z;
115 static int is_valid_pld (int panel, int rotation)
117 if (panel != PANEL_FRONT && panel != PANEL_BACK) {
118 ALOGW("Unhandled PLD panel spec: %d\n", panel);
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);
132 static int read_pld_from_properties (int s, int* panel, int* rotation)
136 if (sensor_get_prop(s, "panel", &p))
139 if (sensor_get_prop(s, "rotation", &r))
142 if (!is_valid_pld(p, r))
148 ALOGI("S%d PLD from properties: panel=%d, rotation=%d\n", s, p, r);
154 static int read_pld_from_sysfs (int s, int dev_num, int* panel, int* rotation)
156 char sysfs_path[PATH_MAX];
159 sprintf(sysfs_path, BASE_PATH "../firmware_node/pld/panel", dev_num);
161 if (sysfs_read_int(sysfs_path, &p))
164 sprintf(sysfs_path, BASE_PATH "../firmware_node/pld/rotation", dev_num);
166 if (sysfs_read_int(sysfs_path, &r))
169 if (!is_valid_pld(p, r))
175 ALOGI("S%d PLD from sysfs: panel=%d, rotation=%d\n", s, p, r);
181 static void decode_placement_information (int dev_num, int num_channels, int s)
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.
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 */
197 /* Map that to field ordering and scaling mechanisms */
198 setup_properties_from_pld(s, panel, rotation, num_channels);
202 static void add_sensor (int dev_num, int catalog_index, int use_polling)
207 char sysfs_path[PATH_MAX];
214 char suffix[MAX_NAME_SIZE + 8];
216 if (sensor_count == MAX_SENSORS) {
217 ALOGE("Too many sensors!\n");
221 sensor_type = sensor_catalog[catalog_index].type;
224 * At this point we could check that the expected sysfs attributes are
225 * present ; that would enable having multiple catalog entries with the
226 * same sensor type, accomodating different sets of sysfs attributes.
231 sensor_info[s].dev_num = dev_num;
232 sensor_info[s].catalog_index = catalog_index;
234 num_channels = sensor_catalog[catalog_index].num_channels;
237 sensor_info[s].num_channels = 0;
239 sensor_info[s].num_channels = num_channels;
241 prefix = sensor_catalog[catalog_index].tag;
244 * receiving the illumination sensor calibration inputs from
245 * the Android properties and setting it within sysfs
247 if (sensor_catalog[catalog_index].type == SENSOR_TYPE_LIGHT) {
248 retval = sensor_get_illumincalib(s);
250 sprintf(sysfs_path, ILLUMINATION_CALIBPATH, dev_num);
251 sysfs_write_int(sysfs_path, retval);
255 /* Read name attribute, if available */
256 sprintf(sysfs_path, NAME_PATH, dev_num);
257 sysfs_read_str(sysfs_path, sensor_info[s].internal_name, MAX_NAME_SIZE);
259 /* See if we have general offsets and scale values for this sensor */
261 sprintf(sysfs_path, SENSOR_OFFSET_PATH, dev_num, prefix);
262 sysfs_read_float(sysfs_path, &sensor_info[s].offset);
264 sprintf(sysfs_path, SENSOR_SCALE_PATH, dev_num, prefix);
265 if (!sysfs_read_float(sysfs_path, &scale)) {
266 sensor_info[s].scale = scale;
267 ALOGI("Scale path:%s scale:%f dev_num:%d\n",
268 sysfs_path, scale, dev_num);
270 sensor_info[s].scale = 1;
272 /* Read channel specific scale if any*/
273 for (c = 0; c < num_channels; c++)
275 sprintf(sysfs_path, BASE_PATH "%s", dev_num,
276 sensor_catalog[catalog_index].channel[c].scale_path);
278 if (!sysfs_read_float(sysfs_path, &scale)) {
279 sensor_info[s].channel[c].scale = scale;
280 sensor_info[s].scale = 0;
282 ALOGI( "Scale path:%s "
283 "channel scale:%f dev_num:%d\n",
284 sysfs_path, scale, dev_num);
289 /* Set default scaling - if num_channels is zero, we have one channel */
291 sensor_info[s].channel[0].opt_scale = 1;
293 for (c = 1; c < num_channels; c++)
294 sensor_info[s].channel[c].opt_scale = 1;
296 /* Read ACPI _PLD attributes for this sensor, if there are any */
297 decode_placement_information(dev_num, num_channels, s);
300 * See if we have optional correction scaling factors for each of the
301 * channels of this sensor. These would be expressed using properties
302 * like iio.accel.y.opt_scale = -1. In case of a single channel we also
303 * support things such as iio.temp.opt_scale = -1. Note that this works
304 * for all types of sensors, and whatever transform is selected, on top
305 * of any previous conversions.
309 for (c = 0; c < num_channels; c++) {
310 ch_name = sensor_catalog[catalog_index].channel[c].name;
311 sprintf(suffix, "%s.opt_scale", ch_name);
312 if (!sensor_get_fl_prop(s, suffix, &opt_scale))
313 sensor_info[s].channel[c].opt_scale = opt_scale;
316 if (!sensor_get_fl_prop(s, "opt_scale", &opt_scale))
317 sensor_info[s].channel[0].opt_scale = opt_scale;
319 /* Initialize Android-visible descriptor */
320 sensor_desc[s].name = sensor_get_name(s);
321 sensor_desc[s].vendor = sensor_get_vendor(s);
322 sensor_desc[s].version = sensor_get_version(s);
323 sensor_desc[s].handle = s;
324 sensor_desc[s].type = sensor_type;
325 sensor_desc[s].maxRange = sensor_get_max_range(s);
326 sensor_desc[s].resolution = sensor_get_resolution(s);
327 sensor_desc[s].power = sensor_get_power(s);
328 sensor_desc[s].stringType = sensor_get_string_type(s);
330 /* None of our supported sensors requires a special permission.
331 * If this will be the case we should implement a sensor_get_perm
333 sensor_desc[s].requiredPermission = "";
334 sensor_desc[s].flags = sensor_get_flags(s);
335 sensor_desc[s].maxDelay = sensor_get_max_delay(s);
336 sensor_desc[s].minDelay = sensor_get_min_delay(s);
338 if (sensor_info[s].internal_name[0] == '\0') {
340 * In case the kernel-mode driver doesn't expose a name for
341 * the iio device, use (null)-dev%d as the trigger name...
342 * This can be considered a kernel-mode iio driver bug.
344 ALOGW("Using null trigger on sensor %d (dev %d)\n", s, dev_num);
345 strcpy(sensor_info[s].internal_name, "(null)");
348 if (sensor_type == SENSOR_TYPE_GYROSCOPE ||
349 sensor_type == SENSOR_TYPE_GYROSCOPE_UNCALIBRATED) {
350 struct gyro_cal* calibration_data = calloc(1, sizeof(struct gyro_cal));
351 sensor_info[s].cal_data = calibration_data;
354 if (sensor_type == SENSOR_TYPE_MAGNETIC_FIELD) {
355 struct compass_cal* calibration_data = calloc(1, sizeof(struct compass_cal));
356 sensor_info[s].cal_data = calibration_data;
359 /* Select one of the available sensor sample processing styles */
362 /* Initialize fields related to sysfs reads offloading */
363 sensor_info[s].thread_data_fd[0] = -1;
364 sensor_info[s].thread_data_fd[1] = -1;
365 sensor_info[s].acquisition_thread = -1;
367 /* Check if we have a special ordering property on this sensor */
368 if (sensor_get_order(s, sensor_info[s].order))
369 sensor_info[s].quirks |= QUIRK_FIELD_ORDERING;
375 static void discover_poll_sensors (int dev_num, char map[CATALOG_SIZE])
377 char base_dir[PATH_MAX];
383 memset(map, 0, CATALOG_SIZE);
385 snprintf(base_dir, sizeof(base_dir), BASE_PATH, dev_num);
387 dir = opendir(base_dir);
392 /* Enumerate entries in this iio device's base folder */
394 while ((d = readdir(dir))) {
395 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
398 /* If the name matches a catalog entry, flag it */
399 for (i = 0; i<CATALOG_SIZE; i++) {
400 /* This will be added separately later */
401 if (sensor_catalog[i].type == SENSOR_TYPE_GYROSCOPE_UNCALIBRATED)
403 for (c=0; c<sensor_catalog[i].num_channels; c++)
404 if (!strcmp(d->d_name,sensor_catalog[i].channel[c].raw_path) ||
405 !strcmp(d->d_name, sensor_catalog[i].channel[c].input_path)) {
416 static void discover_trig_sensors (int dev_num, char map[CATALOG_SIZE])
418 char scan_elem_dir[PATH_MAX];
423 memset(map, 0, CATALOG_SIZE);
425 /* Enumerate entries in this iio device's scan_elements folder */
427 snprintf(scan_elem_dir, sizeof(scan_elem_dir), CHANNEL_PATH, dev_num);
429 dir = opendir(scan_elem_dir);
434 while ((d = readdir(dir))) {
435 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
438 /* Compare en entry to known ones and create matching sensors */
440 for (i = 0; i<CATALOG_SIZE; i++) {
441 if (sensor_catalog[i].type == SENSOR_TYPE_GYROSCOPE_UNCALIBRATED)
443 if (!strcmp(d->d_name,
444 sensor_catalog[i].channel[0].en_path)) {
455 static void orientation_sensor_check(void)
458 * If we have accel + gyro + magn but no rotation vector sensor,
459 * SensorService replaces the HAL provided orientation sensor by the
460 * AOSP version... provided we report one. So initialize a virtual
461 * orientation sensor with zero values, which will get replaced. See:
462 * frameworks/native/services/sensorservice/SensorService.cpp, looking
463 * for SENSOR_TYPE_ROTATION_VECTOR; that code should presumably fall
464 * back to mUserSensorList.add instead of replaceAt, but accommodate it.
473 int catalog_size = CATALOG_SIZE;
475 for (i=0; i<sensor_count; i++)
476 switch (sensor_catalog[sensor_info[i].catalog_index].type) {
477 case SENSOR_TYPE_ACCELEROMETER:
480 case SENSOR_TYPE_GYROSCOPE:
483 case SENSOR_TYPE_MAGNETIC_FIELD:
486 case SENSOR_TYPE_ORIENTATION:
489 case SENSOR_TYPE_ROTATION_VECTOR:
494 if (has_acc && has_gyr && has_mag && !has_rot && !has_ori)
495 for (i=0; i<catalog_size; i++)
496 if (sensor_catalog[i].type == SENSOR_TYPE_ORIENTATION) {
497 ALOGI("Adding placeholder orientation sensor");
503 static int is_continuous (int s)
505 /* Is sensor s of the continous trigger type kind? */
507 int catalog_index = sensor_info[s].catalog_index;
508 int sensor_type = sensor_catalog[catalog_index].type;
510 switch (sensor_type) {
511 case SENSOR_TYPE_ACCELEROMETER:
512 case SENSOR_TYPE_MAGNETIC_FIELD:
513 case SENSOR_TYPE_ORIENTATION:
514 case SENSOR_TYPE_GYROSCOPE:
515 case SENSOR_TYPE_PRESSURE:
516 case SENSOR_TYPE_GRAVITY:
517 case SENSOR_TYPE_LINEAR_ACCELERATION:
518 case SENSOR_TYPE_ROTATION_VECTOR:
519 case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED:
520 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
521 case SENSOR_TYPE_GEOMAGNETIC_ROTATION_VECTOR:
530 static void propose_new_trigger (int s, char trigger_name[MAX_NAME_SIZE],
534 * A new trigger has been enumerated for this sensor. Check if it makes
535 * sense to use it over the currently selected one, and select it if it
536 * is so. The format is something like sensor_name-dev0.
539 const char *suffix = trigger_name + sensor_name_len + 1;
541 /* dev is the default, and lowest priority; no need to update */
542 if (!memcmp(suffix, "dev", 3))
546 * If we found any-motion trigger, record it and force the sensor to
547 * automatic intermediate event generation mode, at least if it is of a
548 * continuously firing sensor type.
551 if (!memcmp(suffix, "any-motion-", 11) && is_continuous(s)) {
552 /* Update the any-motion trigger name to use for this sensor */
553 strcpy(sensor_info[s].motion_trigger_name, trigger_name);
557 /* Update the initial trigger name to use for this sensor */
558 strcpy(sensor_info[s].init_trigger_name, trigger_name);
562 static void update_sensor_matching_trigger_name (char name[MAX_NAME_SIZE])
565 * Check if we have a sensor matching the specified trigger name,
566 * which should then begin with the sensor name, and end with a number
567 * equal to the iio device number the sensor is associated to. If so,
568 * update the string we're going to write to trigger/current_trigger
569 * when enabling this sensor.
579 * First determine the iio device number this trigger refers to. We
580 * expect the last few characters (typically one) of the trigger name
581 * to be this number, so perform a few checks.
583 len = strnlen(name, MAX_NAME_SIZE);
588 cursor = name + len - 1;
590 if (!isdigit(*cursor))
593 while (len && isdigit(*cursor)) {
598 dev_num = atoi(cursor+1);
600 /* See if that matches a sensor */
601 for (s=0; s<sensor_count; s++)
602 if (sensor_info[s].dev_num == dev_num) {
604 sensor_name_len = strlen(sensor_info[s].internal_name);
607 sensor_info[s].internal_name,
609 /* Switch to new trigger if appropriate */
610 propose_new_trigger(s, name, sensor_name_len);
615 static void setup_trigger_names (void)
617 char filename[PATH_MAX];
618 char buf[MAX_NAME_SIZE];
624 /* By default, use the name-dev convention that most drivers use */
625 for (s=0; s<sensor_count; s++)
626 snprintf(sensor_info[s].init_trigger_name,
627 MAX_NAME_SIZE, "%s-dev%d",
628 sensor_info[s].internal_name, sensor_info[s].dev_num);
630 /* Now have a look to /sys/bus/iio/devices/triggerX entries */
632 for (trigger=0; trigger<MAX_TRIGGERS; trigger++) {
634 snprintf(filename, sizeof(filename), TRIGGER_FILE_PATH,trigger);
636 ret = sysfs_read_str(filename, buf, sizeof(buf));
641 /* Record initial and any-motion triggers names */
642 update_sensor_matching_trigger_name(buf);
645 for (s=0; s<sensor_count; s++)
646 if (sensor_info[s].num_channels) {
647 ALOGI( "Sensor %d (%s) default trigger: %s\n", s,
648 sensor_info[s].friendly_name,
649 sensor_info[s].init_trigger_name);
650 if (sensor_info[s].motion_trigger_name[0])
651 ALOGI( "Sensor %d (%s) motion trigger: %s\n",
652 s, sensor_info[s].friendly_name,
653 sensor_info[s].motion_trigger_name);
657 static void uncalibrated_gyro_check (void)
659 unsigned int has_gyr = 0;
660 unsigned int dev_num;
662 unsigned int is_poll_sensor;
663 char buf[MAX_NAME_SIZE];
667 int catalog_size = CATALOG_SIZE; /* Avoid GCC sign comparison warning */
669 /* Checking to see if we have a gyroscope - we can only have uncal if we have the base sensor */
670 for (i=0; i < sensor_count; i++)
671 if(sensor_catalog[sensor_info[i].catalog_index].type == SENSOR_TYPE_GYROSCOPE)
674 dev_num = sensor_info[i].dev_num;
675 is_poll_sensor = !sensor_info[i].num_channels;
681 * If we have a gyro we can add the uncalibrated sensor of the same type and
682 * on the same dev_num. We will save indexes for easy finding and also save the
683 * channel specific information.
686 for (i=0; i<catalog_size; i++)
687 if (sensor_catalog[i].type == SENSOR_TYPE_GYROSCOPE_UNCALIBRATED) {
688 add_sensor(dev_num, i, is_poll_sensor);
690 uncal_idx = sensor_count - 1; /* Just added uncalibrated sensor */
692 /* Similar to build_sensor_report_maps */
693 for (c = 0; c < sensor_info[uncal_idx].num_channels; c++)
695 memcpy( &(sensor_info[uncal_idx].channel[c].type_spec),
696 &(sensor_info[cal_idx].channel[c].type_spec),
697 sizeof(sensor_info[uncal_idx].channel[c].type_spec));
698 sensor_info[uncal_idx].channel[c].type_info = sensor_info[cal_idx].channel[c].type_info;
699 sensor_info[uncal_idx].channel[c].offset = sensor_info[cal_idx].channel[c].offset;
700 sensor_info[uncal_idx].channel[c].size = sensor_info[cal_idx].channel[c].size;
702 sensor_info[uncal_idx].pair_idx = cal_idx;
703 sensor_info[cal_idx].pair_idx = uncal_idx;
704 strncpy(sensor_info[uncal_idx].init_trigger_name,
705 sensor_info[cal_idx].init_trigger_name,
707 strncpy(sensor_info[uncal_idx].motion_trigger_name,
708 sensor_info[cal_idx].motion_trigger_name,
711 /* Add "Uncalibrated " prefix to sensor name */
712 strcpy(buf, sensor_info[cal_idx].friendly_name);
713 snprintf(sensor_info[uncal_idx].friendly_name,
715 "%s %s", "Uncalibrated", buf);
720 void enumerate_sensors (void)
723 * Discover supported sensors and allocate control structures for them.
724 * Multiple sensors can potentially rely on a single iio device (each
725 * using their own channels). We can't have multiple sensors of the same
726 * type on the same device. In case of detection as both a poll-mode
727 * and trigger-based sensor, use the trigger usage mode.
729 char poll_sensors[CATALOG_SIZE];
730 char trig_sensors[CATALOG_SIZE];
735 for (dev_num=0; dev_num<MAX_DEVICES; dev_num++) {
738 discover_poll_sensors(dev_num, poll_sensors);
739 discover_trig_sensors(dev_num, trig_sensors);
741 for (i=0; i<CATALOG_SIZE; i++)
742 if (trig_sensors[i]) {
743 add_sensor(dev_num, i, 0);
748 add_sensor(dev_num, i, 1);
751 build_sensor_report_maps(dev_num);
755 ALOGI("Discovered %d sensors\n", sensor_count);
757 /* Set up default - as well as custom - trigger names */
758 setup_trigger_names();
760 /* Make sure Android fall backs to its own orientation sensor */
761 orientation_sensor_check();
764 * Create the uncalibrated counterpart to the compensated gyroscope.
765 * This is is a new sensor type in Android 4.4.
767 uncalibrated_gyro_check();
771 void delete_enumeration_data (void)
775 for (i = 0; i < sensor_count; i++)
776 switch (sensor_catalog[sensor_info[i].catalog_index].type) {
777 case SENSOR_TYPE_MAGNETIC_FIELD:
778 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
779 case SENSOR_TYPE_GYROSCOPE:
780 if (sensor_info[i].cal_data != NULL) {
781 free(sensor_info[i].cal_data);
782 sensor_info[i].cal_data = NULL;
783 sensor_info[i].cal_level = 0;
789 /* Reset sensor count */
794 int get_sensors_list( struct sensors_module_t* module,
795 struct sensor_t const** list)