2 * Copyright (C) 2014-2015 Intel Corporation.
11 #include <hardware/sensors.h>
12 #include "enumeration.h"
13 #include "description.h"
15 #include "transform.h"
16 #include "description.h"
18 #include "calibration.h"
21 * This table maps syfs entries in scan_elements directories to sensor types,
22 * and will also be used to determine other sysfs names as well as the iio
23 * device number associated to a specific sensor.
27 * We duplicate entries for the uncalibrated types after their respective base
28 * sensor. This is because all sensor entries must have an associated catalog entry
29 * and also because when only the uncal sensor is active it needs to take it's data
30 * from the same iio device as the base one.
33 sensor_catalog_entry_t sensor_catalog[] = {
37 .type = SENSOR_TYPE_ACCELEROMETER,
41 { DECLARE_NAMED_CHANNEL("accel", "x") },
42 { DECLARE_NAMED_CHANNEL("accel", "y") },
43 { DECLARE_NAMED_CHANNEL("accel", "z") },
49 .type = SENSOR_TYPE_GYROSCOPE,
53 { DECLARE_NAMED_CHANNEL("anglvel", "x") },
54 { DECLARE_NAMED_CHANNEL("anglvel", "y") },
55 { DECLARE_NAMED_CHANNEL("anglvel", "z") },
61 .type = SENSOR_TYPE_MAGNETIC_FIELD,
65 { DECLARE_NAMED_CHANNEL("magn", "x") },
66 { DECLARE_NAMED_CHANNEL("magn", "y") },
67 { DECLARE_NAMED_CHANNEL("magn", "z") },
73 .type = SENSOR_TYPE_INTERNAL_INTENSITY,
77 { DECLARE_NAMED_CHANNEL("intensity", "both") },
83 .type = SENSOR_TYPE_INTERNAL_ILLUMINANCE,
87 { DECLARE_GENERIC_CHANNEL("illuminance") },
93 .type = SENSOR_TYPE_ORIENTATION,
97 { DECLARE_NAMED_CHANNEL("incli", "x") },
98 { DECLARE_NAMED_CHANNEL("incli", "y") },
99 { DECLARE_NAMED_CHANNEL("incli", "z") },
105 .type = SENSOR_TYPE_ROTATION_VECTOR,
109 { DECLARE_NAMED_CHANNEL("rot", "quat_x") },
110 { DECLARE_NAMED_CHANNEL("rot", "quat_y") },
111 { DECLARE_NAMED_CHANNEL("rot", "quat_z") },
112 { DECLARE_NAMED_CHANNEL("rot", "quat_w") },
118 .type = SENSOR_TYPE_AMBIENT_TEMPERATURE,
122 { DECLARE_GENERIC_CHANNEL("temp") },
128 .type = SENSOR_TYPE_PROXIMITY,
132 { DECLARE_GENERIC_CHANNEL("proximity") },
138 .type = SENSOR_TYPE_GYROSCOPE_UNCALIBRATED,
142 { DECLARE_GENERIC_CHANNEL("") },
149 .type = SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED,
153 { DECLARE_GENERIC_CHANNEL("") },
159 .type = SENSOR_TYPE_STEP_COUNTER,
163 { DECLARE_GENERIC_CHANNEL("steps") },
169 .type = SENSOR_TYPE_STEP_DETECTOR,
174 DECLARE_VOID_CHANNEL("steps")
177 { DECLARE_NAMED_EVENT("steps", "change") },
185 .type = SENSOR_TYPE_PROXIMITY,
190 DECLARE_VOID_CHANNEL("proximity0")
193 { DECLARE_EVENT("proximity0", "_", "", "", "thresh", "_", "either") },
197 DECLARE_VOID_CHANNEL("proximity1")
200 { DECLARE_EVENT("proximity1", "_", "", "", "thresh", "_", "either") },
204 DECLARE_VOID_CHANNEL("proximity2")
207 { DECLARE_EVENT("proximity2", "_", "", "", "thresh", "_", "either") },
211 DECLARE_VOID_CHANNEL("proximity3")
214 { DECLARE_EVENT("proximity3", "_", "", "", "thresh", "_", "either") },
221 unsigned int catalog_size = ARRAY_SIZE(sensor_catalog);
223 /* ACPI PLD (physical location of device) definitions, as used with sensors */
225 #define PANEL_FRONT 4
228 /* Buffer default length */
229 #define BUFFER_LENGTH 16
231 /* We equate sensor handles to indices in these tables */
233 struct sensor_t sensor_desc[MAX_SENSORS]; /* Android-level descriptors */
234 sensor_info_t sensor[MAX_SENSORS]; /* Internal descriptors */
235 int sensor_count; /* Detected sensors */
238 /* if the sensor has an _en attribute, we need to enable it */
239 int get_needs_enable(int dev_num, const char *tag)
241 char sysfs_path[PATH_MAX];
244 sprintf(sysfs_path, SENSOR_ENABLE_PATH, dev_num, tag);
246 fd = open(sysfs_path, O_RDWR);
254 static void setup_properties_from_pld (int s, int panel, int rotation,
258 * Generate suitable order and opt_scale directives from the PLD panel
259 * and rotation codes we got. This can later be superseded by the usual
260 * properties if necessary. Eventually we'll need to replace these
261 * mechanisms by a less convoluted one, such as a 3x3 placement matrix.
268 int angle = rotation * 45;
270 /* Only deal with 3 axis chips for now */
271 if (num_channels < 3)
274 if (panel == PANEL_BACK) {
275 /* Chip placed on the back panel ; negate x and z */
281 case 90: /* 90° clockwise: negate y then swap x,y */
286 case 180: /* Upside down: negate x and y */
291 case 270: /* 90° counter clockwise: negate x then swap x,y */
298 sensor[s].order[0] = 1;
299 sensor[s].order[1] = 0;
300 sensor[s].order[2] = 2;
301 sensor[s].quirks |= QUIRK_FIELD_ORDERING;
304 sensor[s].channel[0].opt_scale = x;
305 sensor[s].channel[1].opt_scale = y;
306 sensor[s].channel[2].opt_scale = z;
310 static int is_valid_pld (int panel, int rotation)
312 if (panel != PANEL_FRONT && panel != PANEL_BACK) {
313 ALOGW("Unhandled PLD panel spec: %d\n", panel);
317 /* Only deal with 90° rotations for now */
318 if (rotation < 0 || rotation > 7 || (rotation & 1)) {
319 ALOGW("Unhandled PLD rotation spec: %d\n", rotation);
327 static int read_pld_from_properties (int s, int* panel, int* rotation)
331 if (sensor_get_prop(s, "panel", &p))
334 if (sensor_get_prop(s, "rotation", &r))
337 if (!is_valid_pld(p, r))
343 ALOGI("S%d PLD from properties: panel=%d, rotation=%d\n", s, p, r);
349 static int read_pld_from_sysfs (int s, int dev_num, int* panel, int* rotation)
351 char sysfs_path[PATH_MAX];
354 sprintf(sysfs_path, BASE_PATH "../firmware_node/pld/panel", dev_num);
356 if (sysfs_read_int(sysfs_path, &p))
359 sprintf(sysfs_path, BASE_PATH "../firmware_node/pld/rotation", dev_num);
361 if (sysfs_read_int(sysfs_path, &r))
364 if (!is_valid_pld(p, r))
370 ALOGI("S%d PLD from sysfs: panel=%d, rotation=%d\n", s, p, r);
376 static void decode_placement_information (int dev_num, int num_channels, int s)
379 * See if we have optional "physical location of device" ACPI tags.
380 * We're only interested in panel and rotation specifiers. Use the
381 * .panel and .rotation properties in priority, and the actual ACPI
382 * values as a second source.
388 if (read_pld_from_properties(s, &panel, &rotation) &&
389 read_pld_from_sysfs(s, dev_num, &panel, &rotation))
390 return; /* No PLD data available */
392 /* Map that to field ordering and scaling mechanisms */
393 setup_properties_from_pld(s, panel, rotation, num_channels);
397 static int map_internal_to_external_type (int sensor_type)
399 /* Most sensors are internally identified using the Android type, but for some we use a different type specification internally */
401 switch (sensor_type) {
402 case SENSOR_TYPE_INTERNAL_ILLUMINANCE:
403 case SENSOR_TYPE_INTERNAL_INTENSITY:
404 return SENSOR_TYPE_LIGHT;
411 static void populate_descriptors (int s, int sensor_type)
413 int32_t min_delay_us;
414 max_delay_t max_delay_us;
416 /* Initialize Android-visible descriptor */
417 sensor_desc[s].name = sensor_get_name(s);
418 sensor_desc[s].vendor = sensor_get_vendor(s);
419 sensor_desc[s].version = sensor_get_version(s);
420 sensor_desc[s].handle = s;
421 sensor_desc[s].type = map_internal_to_external_type(sensor_type);
423 sensor_desc[s].maxRange = sensor_get_max_range(s);
424 sensor_desc[s].resolution = sensor_get_resolution(s);
425 sensor_desc[s].power = sensor_get_power(s);
426 sensor_desc[s].stringType = sensor_get_string_type(s);
428 /* None of our supported sensors requires a special permission */
429 sensor_desc[s].requiredPermission = "";
431 sensor_desc[s].flags = sensor_get_flags(s);
432 sensor_desc[s].minDelay = sensor_get_min_delay(s);
433 sensor_desc[s].maxDelay = sensor_get_max_delay(s);
435 ALOGV("Sensor %d (%s) type(%d) minD(%d) maxD(%d) flags(%2.2x)\n",
436 s, sensor[s].friendly_name, sensor_desc[s].type,
437 sensor_desc[s].minDelay, sensor_desc[s].maxDelay,
438 sensor_desc[s].flags);
440 /* We currently do not implement batching */
441 sensor_desc[s].fifoReservedEventCount = 0;
442 sensor_desc[s].fifoMaxEventCount = 0;
444 min_delay_us = sensor_desc[s].minDelay;
445 max_delay_us = sensor_desc[s].maxDelay;
447 sensor[s].min_supported_rate = max_delay_us ? 1000000.0 / max_delay_us : 1;
448 sensor[s].max_supported_rate = min_delay_us && min_delay_us != -1 ? 1000000.0 / min_delay_us : 0;
452 static void add_virtual_sensor (int catalog_index)
457 if (sensor_count == MAX_SENSORS) {
458 ALOGE("Too many sensors!\n");
462 sensor_type = sensor_catalog[catalog_index].type;
466 sensor[s].is_virtual = 1;
467 sensor[s].catalog_index = catalog_index;
468 sensor[s].type = sensor_type;
470 populate_descriptors(s, sensor_type);
472 /* Initialize fields related to sysfs reads offloading */
473 sensor[s].thread_data_fd[0] = -1;
474 sensor[s].thread_data_fd[1] = -1;
475 sensor[s].acquisition_thread = -1;
481 static int add_sensor (int dev_num, int catalog_index, int mode)
486 char sysfs_path[PATH_MAX];
493 char suffix[MAX_NAME_SIZE + 8];
497 if (sensor_count == MAX_SENSORS) {
498 ALOGE("Too many sensors!\n");
502 sensor_type = sensor_catalog[catalog_index].type;
505 * At this point we could check that the expected sysfs attributes are
506 * present ; that would enable having multiple catalog entries with the
507 * same sensor type, accomodating different sets of sysfs attributes.
512 sensor[s].dev_num = dev_num;
513 sensor[s].catalog_index = catalog_index;
514 sensor[s].type = sensor_type;
515 sensor[s].mode = mode;
516 sensor[s].trigger_nr = -1; /* -1 means no trigger - we'll populate these at a later time */
518 num_channels = sensor_catalog[catalog_index].num_channels;
520 if (mode == MODE_POLL)
521 sensor[s].num_channels = 0;
523 sensor[s].num_channels = num_channels;
525 /* Populate the quirks array */
526 sensor_get_quirks(s);
528 /* Reject interfaces that may have been disabled through a quirk for this driver */
529 if ((mode == MODE_EVENT && (sensor[s].quirks & QUIRK_NO_EVENT_MODE)) ||
530 (mode == MODE_TRIGGER && (sensor[s].quirks & QUIRK_NO_TRIG_MODE )) ||
531 (mode == MODE_POLL && (sensor[s].quirks & QUIRK_NO_POLL_MODE ))) {
532 memset(&sensor[s], 0, sizeof(sensor[0]));
536 prefix = sensor_catalog[catalog_index].tag;
539 * receiving the illumination sensor calibration inputs from
540 * the Android properties and setting it within sysfs
542 if (sensor_type == SENSOR_TYPE_INTERNAL_ILLUMINANCE) {
543 retval = sensor_get_illumincalib(s);
545 sprintf(sysfs_path, ILLUMINATION_CALIBPATH, dev_num);
546 sysfs_write_int(sysfs_path, retval);
551 * See if we have optional calibration biases for each of the channels of this sensor. These would be expressed using properties like
552 * iio.accel.y.calib_bias = -1, or possibly something like iio.temp.calib_bias if the sensor has a single channel. This value gets stored in the
553 * relevant calibbias sysfs file if that file can be located and then used internally by the iio sensor driver.
557 for (c = 0; c < num_channels; c++) {
558 ch_name = sensor_catalog[catalog_index].channel[c].name;
559 sprintf(suffix, "%s.calib_bias", ch_name);
560 if (!sensor_get_prop(s, suffix, &calib_bias) && calib_bias) {
561 sprintf(suffix, "%s_%s", prefix, sensor_catalog[catalog_index].channel[c].name);
562 sprintf(sysfs_path, SENSOR_CALIB_BIAS_PATH, dev_num, suffix);
563 sysfs_write_int(sysfs_path, calib_bias);
567 if (!sensor_get_prop(s, "calib_bias", &calib_bias) && calib_bias) {
568 sprintf(sysfs_path, SENSOR_CALIB_BIAS_PATH, dev_num, prefix);
569 sysfs_write_int(sysfs_path, calib_bias);
572 /* Change buffer length according to the property or use default value */
573 if (mode == MODE_TRIGGER) {
574 if (sensor_get_prop(s, "buffer_length", &buffer_length)) {
575 buffer_length = BUFFER_LENGTH;
578 sprintf(sysfs_path, BUFFER_LENGTH_PATH, dev_num);
580 if (sysfs_write_int(sysfs_path, buffer_length) <= 0) {
581 ALOGE("Failed to set buffer length on dev%d", dev_num);
585 /* Read name attribute, if available */
586 sprintf(sysfs_path, NAME_PATH, dev_num);
587 sysfs_read_str(sysfs_path, sensor[s].internal_name, MAX_NAME_SIZE);
589 /* See if we have general offsets and scale values for this sensor */
591 sprintf(sysfs_path, SENSOR_OFFSET_PATH, dev_num, prefix);
592 sysfs_read_float(sysfs_path, &sensor[s].offset);
594 sprintf(sysfs_path, SENSOR_SCALE_PATH, dev_num, prefix);
595 if (!sensor_get_fl_prop(s, "scale", &scale)) {
597 * There is a chip preferred scale specified,
598 * so try to store it in sensor's scale file
600 if (sysfs_write_float(sysfs_path, scale) == -1 && errno == ENOENT) {
601 ALOGE("Failed to store scale[%g] into %s - file is missing", scale, sysfs_path);
602 /* Store failed, try to store the scale into channel specific file */
603 for (c = 0; c < num_channels; c++)
605 sprintf(sysfs_path, BASE_PATH "%s", dev_num,
606 sensor_catalog[catalog_index].channel[c].scale_path);
607 if (sysfs_write_float(sysfs_path, scale) == -1)
608 ALOGE("Failed to store scale[%g] into %s", scale, sysfs_path);
613 sprintf(sysfs_path, SENSOR_SCALE_PATH, dev_num, prefix);
614 if (!sysfs_read_float(sysfs_path, &scale)) {
615 sensor[s].scale = scale;
616 ALOGV("Scale path:%s scale:%g dev_num:%d\n",
617 sysfs_path, scale, dev_num);
621 /* Read channel specific scale if any*/
622 for (c = 0; c < num_channels; c++)
624 sprintf(sysfs_path, BASE_PATH "%s", dev_num,
625 sensor_catalog[catalog_index].channel[c].scale_path);
627 if (!sysfs_read_float(sysfs_path, &scale)) {
628 sensor[s].channel[c].scale = scale;
631 ALOGV( "Scale path:%s "
632 "channel scale:%g dev_num:%d\n",
633 sysfs_path, scale, dev_num);
638 /* Set default scaling - if num_channels is zero, we have one channel */
640 sensor[s].channel[0].opt_scale = 1;
642 for (c = 1; c < num_channels; c++)
643 sensor[s].channel[c].opt_scale = 1;
645 for (c = 0; c < num_channels; c++) {
646 /* Check the presence of the channel's input_path */
647 sprintf(sysfs_path, BASE_PATH "%s", dev_num,
648 sensor_catalog[catalog_index].channel[c].input_path);
649 sensor[s].channel[c].input_path_present = (access(sysfs_path, R_OK) != -1);
650 /* Check the presence of the channel's raw_path */
651 sprintf(sysfs_path, BASE_PATH "%s", dev_num,
652 sensor_catalog[catalog_index].channel[c].raw_path);
653 sensor[s].channel[c].raw_path_present = (access(sysfs_path, R_OK) != -1);
656 sensor_get_available_frequencies(s);
658 if (sensor_get_mounting_matrix(s, sensor[s].mounting_matrix))
659 sensor[s].quirks |= QUIRK_MOUNTING_MATRIX;
661 /* Read ACPI _PLD attributes for this sensor, if there are any */
662 decode_placement_information(dev_num, num_channels, s);
665 * See if we have optional correction scaling factors for each of the
666 * channels of this sensor. These would be expressed using properties
667 * like iio.accel.y.opt_scale = -1. In case of a single channel we also
668 * support things such as iio.temp.opt_scale = -1. Note that this works
669 * for all types of sensors, and whatever transform is selected, on top
670 * of any previous conversions.
674 for (c = 0; c < num_channels; c++) {
675 ch_name = sensor_catalog[catalog_index].channel[c].name;
676 sprintf(suffix, "%s.opt_scale", ch_name);
677 if (!sensor_get_fl_prop(s, suffix, &opt_scale))
678 sensor[s].channel[c].opt_scale = opt_scale;
681 if (!sensor_get_fl_prop(s, "opt_scale", &opt_scale))
682 sensor[s].channel[0].opt_scale = opt_scale;
685 populate_descriptors(s, sensor_type);
687 if (sensor[s].internal_name[0] == '\0') {
689 * In case the kernel-mode driver doesn't expose a name for
690 * the iio device, use (null)-dev%d as the trigger name...
691 * This can be considered a kernel-mode iio driver bug.
693 ALOGW("Using null trigger on sensor %d (dev %d)\n", s, dev_num);
694 strcpy(sensor[s].internal_name, "(null)");
697 switch (sensor_type) {
698 case SENSOR_TYPE_ACCELEROMETER:
699 /* Only engage accelerometer bias compensation if really needed */
700 if (sensor_get_quirks(s) & QUIRK_BIASED)
701 sensor[s].cal_data = calloc(1, sizeof(accel_cal_t));
704 case SENSOR_TYPE_GYROSCOPE:
705 sensor[s].cal_data = malloc(sizeof(gyro_cal_t));
708 case SENSOR_TYPE_MAGNETIC_FIELD:
709 sensor[s].cal_data = malloc(sizeof(compass_cal_t));
713 sensor[s].max_cal_level = sensor_get_cal_steps(s);
715 /* Select one of the available sensor sample processing styles */
718 /* Initialize fields related to sysfs reads offloading */
719 sensor[s].thread_data_fd[0] = -1;
720 sensor[s].thread_data_fd[1] = -1;
721 sensor[s].acquisition_thread = -1;
723 /* Check if we have a special ordering property on this sensor */
724 if (sensor_get_order(s, sensor[s].order))
725 sensor[s].quirks |= QUIRK_FIELD_ORDERING;
727 sensor[s].needs_enable = get_needs_enable(dev_num, sensor_catalog[catalog_index].tag);
733 static void virtual_sensors_check (void)
741 int gyro_cal_idx = 0;
742 int magn_cal_idx = 0;
745 for (i=0; i<sensor_count; i++)
746 switch (sensor[i].type) {
747 case SENSOR_TYPE_ACCELEROMETER:
750 case SENSOR_TYPE_GYROSCOPE:
754 case SENSOR_TYPE_MAGNETIC_FIELD:
758 case SENSOR_TYPE_ORIENTATION:
761 case SENSOR_TYPE_ROTATION_VECTOR:
766 for (j=0; j<catalog_size; j++)
767 switch (sensor_catalog[j].type) {
769 * If we have accel + gyro + magn but no rotation vector sensor,
770 * SensorService replaces the HAL provided orientation sensor by the
771 * AOSP version... provided we report one. So initialize a virtual
772 * orientation sensor with zero values, which will get replaced. See:
773 * frameworks/native/services/sensorservice/SensorService.cpp, looking
774 * for SENSOR_TYPE_ROTATION_VECTOR; that code should presumably fall
775 * back to mUserSensorList.add instead of replaceAt, but accommodate it.
778 case SENSOR_TYPE_ORIENTATION:
779 if (has_acc && has_gyr && has_mag && !has_rot && !has_ori)
780 add_sensor(0, j, MODE_POLL);
782 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
784 sensor[sensor_count].base_count = 1;
785 sensor[sensor_count].base[0] = gyro_cal_idx;
786 add_virtual_sensor(j);
789 case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED:
791 sensor[sensor_count].base_count = 1;
792 sensor[sensor_count].base[0] = magn_cal_idx;
793 add_virtual_sensor(j);
802 static void propose_new_trigger (int s, char trigger_name[MAX_NAME_SIZE],
806 * A new trigger has been enumerated for this sensor. Check if it makes sense to use it over the currently selected one,
807 * and select it if it is so. The format is something like sensor_name-dev0.
810 const char *suffix = trigger_name + sensor_name_len + 1;
812 /* dev is the default, and lowest priority; no need to update */
813 if (!memcmp(suffix, "dev", 3))
816 /* If we found any-motion trigger, record it */
818 if (!memcmp(suffix, "any-motion-", 11)) {
819 strcpy(sensor[s].motion_trigger_name, trigger_name);
823 /* If we found a hrtimer trigger, record it */
824 if (!memcmp(suffix, "hr-dev", 6)) {
825 strcpy(sensor[s].hrtimer_trigger_name, trigger_name);
829 * 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
830 * of the trigger to use with this sensor.
832 strcpy(sensor[s].init_trigger_name, trigger_name);
836 static void update_sensor_matching_trigger_name (char name[MAX_NAME_SIZE], int* updated, int trigger)
839 * Check if we have a sensor matching the specified trigger name, which should then begin with the sensor name, and end with a number
840 * 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
841 * when enabling this sensor.
851 * First determine the iio device number this trigger refers to. We expect the last few characters (typically one) of the trigger name
852 * to be this number, so perform a few checks.
854 len = strnlen(name, MAX_NAME_SIZE);
859 cursor = name + len - 1;
861 if (!isdigit(*cursor))
864 while (len && isdigit(*cursor)) {
869 dev_num = atoi(cursor+1);
871 /* See if that matches a sensor */
872 for (s=0; s<sensor_count; s++)
873 if (sensor[s].dev_num == dev_num) {
875 sensor_name_len = strlen(sensor[s].internal_name);
877 if (!strncmp(name, sensor[s].internal_name, sensor_name_len))
878 /* Switch to new trigger if appropriate */
879 propose_new_trigger(s, name, sensor_name_len);
881 sensor[s].trigger_nr = trigger;
885 static int create_hrtimer_trigger(int s, int trigger)
887 struct stat dir_status;
888 char buf[MAX_NAME_SIZE];
889 char hrtimer_path[PATH_MAX];
890 char hrtimer_name[MAX_NAME_SIZE];
892 snprintf(buf, MAX_NAME_SIZE, "hrtimer-%s-hr-dev%d", sensor[s].internal_name, sensor[s].dev_num);
893 snprintf(hrtimer_name, MAX_NAME_SIZE, "%s-hr-dev%d", sensor[s].internal_name, sensor[s].dev_num);
894 snprintf(hrtimer_path, PATH_MAX, "%s%s", CONFIGFS_TRIGGER_PATH, buf);
896 /* Get parent dir status */
897 if (stat(CONFIGFS_TRIGGER_PATH, &dir_status))
900 /* Create hrtimer with the same access rights as it's parent */
901 if (mkdir(hrtimer_path, dir_status.st_mode))
905 strncpy (sensor[s].hrtimer_trigger_name, hrtimer_name, MAX_NAME_SIZE);
906 sensor[s].trigger_nr = trigger;
910 static void setup_trigger_names (void)
912 char filename[PATH_MAX];
913 char buf[MAX_NAME_SIZE];
917 int updated[MAX_SENSORS] = {0};
919 /* By default, use the name-dev convention that most drivers use */
920 for (s=0; s<sensor_count; s++)
921 snprintf(sensor[s].init_trigger_name, MAX_NAME_SIZE, "%s-dev%d", sensor[s].internal_name, sensor[s].dev_num);
923 /* Now have a look to /sys/bus/iio/devices/triggerX entries */
925 for (trigger=0; trigger<MAX_TRIGGERS; trigger++) {
927 snprintf(filename, sizeof(filename), TRIGGER_FILE_PATH, trigger);
929 ret = sysfs_read_str(filename, buf, sizeof(buf));
934 /* Record initial and any-motion triggers names */
935 update_sensor_matching_trigger_name(buf, updated, trigger);
939 /* If we don't have any other trigger exposed and quirk hrtimer is set setup the hrtimer name here - and create it also */
940 for (s=0; s<sensor_count && trigger<MAX_TRIGGERS; s++) {
941 if ((sensor[s].quirks & QUIRK_HRTIMER) && !updated[s]) {
942 create_hrtimer_trigger(s, trigger);
948 * Certain drivers expose only motion triggers even though they should be continous. For these, use the default trigger name as the motion
949 * trigger. The code generating intermediate events is dependent on motion_trigger_name being set to a non empty string.
952 for (s=0; s<sensor_count; s++)
953 if ((sensor[s].quirks & QUIRK_TERSE_DRIVER) && sensor[s].motion_trigger_name[0] == '\0')
954 strcpy(sensor[s].motion_trigger_name, sensor[s].init_trigger_name);
956 for (s=0; s<sensor_count; s++)
957 if (sensor[s].mode == MODE_TRIGGER) {
958 ALOGI("Sensor %d (%s) default trigger: %s\n", s, sensor[s].friendly_name, sensor[s].init_trigger_name);
959 if (sensor[s].motion_trigger_name[0])
960 ALOGI("Sensor %d (%s) motion trigger: %s\n", s, sensor[s].friendly_name, sensor[s].motion_trigger_name);
961 if (sensor[s].hrtimer_trigger_name[0])
962 ALOGI("Sensor %d (%s) hrtimer trigger: %s\n", s, sensor[s].friendly_name, sensor[s].hrtimer_trigger_name);
967 static int catalog_index_from_sensor_type (int type)
969 /* Return first matching catalog entry index for selected type */
972 for (i=0; i<catalog_size; i++)
973 if (sensor_catalog[i].type == type)
980 static void post_process_sensor_list (char poll_map[catalog_size], char trig_map[catalog_size], char event_map[catalog_size])
982 int illuminance_cat_index = catalog_index_from_sensor_type(SENSOR_TYPE_INTERNAL_ILLUMINANCE);
983 int intensity_cat_index = catalog_index_from_sensor_type(SENSOR_TYPE_INTERNAL_INTENSITY);
984 int illuminance_found = poll_map[illuminance_cat_index] || trig_map[illuminance_cat_index] || event_map[illuminance_cat_index];
986 /* If an illumimance sensor has been reported */
987 if (illuminance_found) {
988 /* Hide any intensity sensors we can have for the same iio device */
989 poll_map [intensity_cat_index ] = 0;
990 trig_map [intensity_cat_index ] = 0;
991 event_map[intensity_cat_index ] = 0;
997 static void swap_sensors (int s1, int s2)
999 struct sensor_t temp_sensor_desc;
1000 sensor_info_t temp_sensor;
1003 memcpy(&temp_sensor, &sensor[s1], sizeof(sensor_info_t));
1004 memcpy(&temp_sensor_desc, &sensor_desc[s1], sizeof(struct sensor_t));
1007 memcpy(&sensor[s1], &sensor[s2], sizeof(sensor_info_t));
1008 memcpy(&sensor_desc[s1], &sensor_desc[s2], sizeof(struct sensor_t));
1011 memcpy(&sensor[s2], &temp_sensor, sizeof(sensor_info_t));
1012 memcpy(&sensor_desc[s2], &temp_sensor_desc, sizeof(struct sensor_t));
1014 /* Fix-up sensor id mapping, which is stale */
1015 sensor_desc[s1].handle = s1;
1016 sensor_desc[s2].handle = s2;
1018 /* Fix up name and vendor buffer pointers, which are potentially stale pointers */
1019 sensor_desc[s1].name = sensor_get_name(s1);
1020 sensor_desc[s1].vendor = sensor_get_vendor(s1);
1021 sensor_desc[s2].name = sensor_get_name(s2);
1022 sensor_desc[s2].vendor = sensor_get_vendor(s2);
1026 static void reorder_sensors (void)
1028 /* Some sensors may be marked as secondary - these need to be listed after other sensors of the same type */
1031 for (s1=0; s1<sensor_count-1; s1++)
1032 if (sensor[s1].quirks & QUIRK_SECONDARY) {
1033 /* Search for subsequent sensors of same type */
1034 for (s2 = s1+1; s2<sensor_count; s2++)
1035 if (sensor[s2].type == sensor[s1].type && !(sensor[s2].quirks & QUIRK_SECONDARY)) {
1036 ALOGI("Sensor S%d has higher priority than S%d, swapping\n", s2, s1);
1037 swap_sensors(s1, s2);
1044 void enumerate_sensors (void)
1047 * Discover supported sensors and allocate control structures for them. Multiple sensors can potentially rely on a single iio device (each
1048 * 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
1049 * and trigger-based sensor, use the trigger usage mode.
1051 char poll_sensors[catalog_size];
1052 char trig_sensors[catalog_size];
1053 char event_sensors[catalog_size];
1059 for (dev_num=0; dev_num<MAX_DEVICES; dev_num++) {
1062 discover_sensors(dev_num, BASE_PATH, poll_sensors, check_poll_sensors);
1063 discover_sensors(dev_num, CHANNEL_PATH, trig_sensors, check_trig_sensors);
1064 discover_sensors(dev_num, EVENTS_PATH, event_sensors, check_event_sensors);
1066 /* Hide specific sensor types if appropriate */
1067 post_process_sensor_list(poll_sensors, trig_sensors, event_sensors);
1069 for (i=0; i<catalog_size; i++) {
1070 /* Try using events interface */
1071 if (event_sensors[i] && !add_sensor(dev_num, i, MODE_EVENT))
1075 if (trig_sensors[i] && !add_sensor(dev_num, i, MODE_TRIGGER)) {
1080 /* Try polling otherwise */
1081 if (poll_sensors[i])
1082 add_sensor(dev_num, i, MODE_POLL);
1086 build_sensor_report_maps(dev_num);
1089 /* Make sure secondary sensors appear after primary ones */
1092 ALOGI("Discovered %d sensors\n", sensor_count);
1094 /* Set up default - as well as custom - trigger names */
1095 setup_trigger_names();
1097 ALOGI("Discovered %d sensors\n", sensor_count);
1099 virtual_sensors_check();
1101 for (s=0; s<sensor_count; s++) {
1102 ALOGI("S%d: %s\n", s, sensor[s].friendly_name);
1107 void delete_enumeration_data (void)
1110 for (i = 0; i < sensor_count; i++)
1111 if (sensor[i].cal_data) {
1112 free(sensor[i].cal_data);
1113 sensor[i].cal_data = NULL;
1114 sensor[i].cal_level = 0;
1117 /* Reset sensor count */
1122 int get_sensors_list (__attribute__((unused)) struct sensors_module_t* module,
1123 struct sensor_t const** list)
1125 *list = sensor_desc;
1126 return sensor_count;