/*
- * Copyright (C) 2014 Intel Corporation.
+ * Copyright (C) 2014-2015 Intel Corporation.
*/
#include <ctype.h>
#include <dirent.h>
#include <stdlib.h>
+#include <fcntl.h>
#include <utils/Log.h>
#include <hardware/sensors.h>
#include "enumeration.h"
*/
sensor_catalog_entry_t sensor_catalog[] = {
- DECLARE_SENSOR3("accel", SENSOR_TYPE_ACCELEROMETER, "x", "y", "z")
- DECLARE_SENSOR3("anglvel", SENSOR_TYPE_GYROSCOPE, "x", "y", "z")
- DECLARE_SENSOR3("magn", SENSOR_TYPE_MAGNETIC_FIELD, "x", "y", "z")
- DECLARE_SENSOR1("intensity", SENSOR_TYPE_LIGHT, "both" )
- DECLARE_SENSOR0("illuminance",SENSOR_TYPE_LIGHT )
- DECLARE_SENSOR3("incli", SENSOR_TYPE_ORIENTATION, "x", "y", "z")
- DECLARE_SENSOR4("rot", SENSOR_TYPE_ROTATION_VECTOR,
- "quat_x", "quat_y", "quat_z", "quat_w")
- DECLARE_SENSOR0("temp", SENSOR_TYPE_AMBIENT_TEMPERATURE )
- DECLARE_SENSOR0("proximity", SENSOR_TYPE_PROXIMITY )
- DECLARE_VIRTUAL(SENSOR_TYPE_GYROSCOPE_UNCALIBRATED )
- DECLARE_VIRTUAL(SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED )
+ {
+ .tag = "accel",
+ .type = SENSOR_TYPE_ACCELEROMETER,
+ .num_channels = 3,
+ .is_virtual = 0,
+ .channel = {
+ { DECLARE_NAMED_CHANNEL("accel", "x") },
+ { DECLARE_NAMED_CHANNEL("accel", "y") },
+ { DECLARE_NAMED_CHANNEL("accel", "z") },
+ },
+ },
+ {
+ .tag = "anglvel",
+ .type = SENSOR_TYPE_GYROSCOPE,
+ .num_channels = 3,
+ .is_virtual = 0,
+ .channel = {
+ { DECLARE_NAMED_CHANNEL("anglvel", "x") },
+ { DECLARE_NAMED_CHANNEL("anglvel", "y") },
+ { DECLARE_NAMED_CHANNEL("anglvel", "z") },
+ },
+ },
+ {
+ .tag = "magn",
+ .type = SENSOR_TYPE_MAGNETIC_FIELD,
+ .num_channels = 3,
+ .is_virtual = 0,
+ .channel = {
+ { DECLARE_NAMED_CHANNEL("magn", "x") },
+ { DECLARE_NAMED_CHANNEL("magn", "y") },
+ { DECLARE_NAMED_CHANNEL("magn", "z") },
+ },
+ },
+ {
+ .tag = "intensity",
+ .type = SENSOR_TYPE_LIGHT,
+ .num_channels = 1,
+ .is_virtual = 0,
+ .channel = {
+ { DECLARE_NAMED_CHANNEL("intensity", "both") },
+ },
+ },
+ {
+ .tag = "illuminance",
+ .type = SENSOR_TYPE_LIGHT,
+ .num_channels = 1,
+ .is_virtual = 0,
+ .channel = {
+ { DECLARE_GENERIC_CHANNEL("illuminance") },
+ },
+ },
+ {
+ .tag = "incli",
+ .type = SENSOR_TYPE_ORIENTATION,
+ .num_channels = 3,
+ .is_virtual = 0,
+ .channel = {
+ { DECLARE_NAMED_CHANNEL("incli", "x") },
+ { DECLARE_NAMED_CHANNEL("incli", "y") },
+ { DECLARE_NAMED_CHANNEL("incli", "z") },
+ },
+ },
+ {
+ .tag = "rot",
+ .type = SENSOR_TYPE_ROTATION_VECTOR,
+ .num_channels = 4,
+ .is_virtual = 0,
+ .channel = {
+ { DECLARE_NAMED_CHANNEL("rot", "quat_x") },
+ { DECLARE_NAMED_CHANNEL("rot", "quat_y") },
+ { DECLARE_NAMED_CHANNEL("rot", "quat_z") },
+ { DECLARE_NAMED_CHANNEL("rot", "quat_w") },
+ },
+ },
+ {
+ .tag = "temp",
+ .type = SENSOR_TYPE_AMBIENT_TEMPERATURE,
+ .num_channels = 1,
+ .is_virtual = 0,
+ .channel = {
+ { DECLARE_GENERIC_CHANNEL("temp") },
+ },
+ },
+ {
+ .tag = "proximity",
+ .type = SENSOR_TYPE_PROXIMITY,
+ .num_channels = 1,
+ .is_virtual = 0,
+ .channel = {
+ { DECLARE_GENERIC_CHANNEL("proximity") },
+ },
+ },
+ {
+ .tag = "",
+ .type = SENSOR_TYPE_GYROSCOPE_UNCALIBRATED,
+ .num_channels = 0,
+ .is_virtual = 1,
+ .channel = {
+ { DECLARE_GENERIC_CHANNEL("") },
+ },
+
+ },
+ {
+ .tag = "",
+ .type = SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED,
+ .num_channels = 0,
+ .is_virtual = 1,
+ .channel = {
+ { DECLARE_GENERIC_CHANNEL("") },
+ },
+ },
+ {
+ .tag = "steps",
+ .type = SENSOR_TYPE_STEP_COUNTER,
+ .num_channels = 1,
+ .is_virtual = 0,
+ .channel = {
+ { DECLARE_GENERIC_CHANNEL("steps") },
+ },
+ },
+ {
+ .tag = "steps",
+ .type = SENSOR_TYPE_STEP_DETECTOR,
+ .num_channels = 1,
+ .is_virtual = 0,
+ .channel = {
+ {
+ DECLARE_VOID_CHANNEL("steps")
+ .num_events = 1,
+ .event = {
+ { DECLARE_NAMED_EVENT("steps", "change") },
+ },
+ },
+ },
+ },
};
-#define CATALOG_SIZE ARRAY_SIZE(sensor_catalog)
+unsigned int catalog_size = ARRAY_SIZE(sensor_catalog);
/* ACPI PLD (physical location of device) definitions, as used with sensors */
int sensor_count; /* Detected sensors */
+/* if the sensor has an _en attribute, we need to enable it */
+int get_needs_enable(int dev_num, const char *tag)
+{
+ char sysfs_path[PATH_MAX];
+ int fd;
+
+ sprintf(sysfs_path, SENSOR_ENABLE_PATH, dev_num, tag);
+
+ fd = open(sysfs_path, O_RDWR);
+ if (fd == -1)
+ return 0;
+
+ close(fd);
+ return 1;
+}
+
static void setup_properties_from_pld (int s, int panel, int rotation,
int num_channels)
{
}
-static void add_sensor (int dev_num, int catalog_index, int use_polling)
+static void add_sensor (int dev_num, int catalog_index, int mode)
{
int s;
int sensor_type;
const char* ch_name;
int num_channels;
char suffix[MAX_NAME_SIZE + 8];
+ int calib_bias;
if (sensor_count == MAX_SENSORS) {
ALOGE("Too many sensors!\n");
sensor[s].dev_num = dev_num;
sensor[s].catalog_index = catalog_index;
sensor[s].type = sensor_type;
- sensor[s].is_polling = use_polling;
+ sensor[s].mode = mode;
num_channels = sensor_catalog[catalog_index].num_channels;
- if (use_polling)
+ if (mode == MODE_POLL)
sensor[s].num_channels = 0;
else
sensor[s].num_channels = num_channels;
}
}
+ /*
+ * See if we have optional calibration biases for each of the channels of this sensor. These would be expressed using properties like
+ * 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
+ * relevant calibbias sysfs file if that file can be located and then used internally by the iio sensor driver.
+ */
+
+ if (num_channels) {
+ for (c = 0; c < num_channels; c++) {
+ ch_name = sensor_catalog[catalog_index].channel[c].name;
+ sprintf(suffix, "%s.calib_bias", ch_name);
+ if (!sensor_get_prop(s, suffix, &calib_bias) && calib_bias) {
+ sprintf(suffix, "%s_%s", prefix, sensor_catalog[catalog_index].channel[c].name);
+ sprintf(sysfs_path, SENSOR_CALIB_BIAS_PATH, dev_num, suffix);
+ sysfs_write_int(sysfs_path, calib_bias);
+ }
+ }
+ } else
+ if (!sensor_get_prop(s, "calib_bias", &calib_bias) && calib_bias) {
+ sprintf(sysfs_path, SENSOR_CALIB_BIAS_PATH, dev_num, prefix);
+ sysfs_write_int(sysfs_path, calib_bias);
+ }
+
/* Read name attribute, if available */
sprintf(sysfs_path, NAME_PATH, dev_num);
sysfs_read_str(sysfs_path, sensor[s].internal_name, MAX_NAME_SIZE);
for (c = 1; c < num_channels; c++)
sensor[s].channel[c].opt_scale = 1;
+ for (c = 0; c < num_channels; c++) {
+ /* Check the presence of the channel's input_path */
+ sprintf(sysfs_path, BASE_PATH "%s", dev_num,
+ sensor_catalog[catalog_index].channel[c].input_path);
+ sensor[s].channel[c].input_path_present = (access(sysfs_path, R_OK) != -1);
+ /* Check the presence of the channel's raw_path */
+ sprintf(sysfs_path, BASE_PATH "%s", dev_num,
+ sensor_catalog[catalog_index].channel[c].raw_path);
+ sensor[s].channel[c].raw_path_present = (access(sysfs_path, R_OK) != -1);
+ }
+
/* Read ACPI _PLD attributes for this sensor, if there are any */
decode_placement_information(dev_num, num_channels, s);
if (sensor_get_order(s, sensor[s].order))
sensor[s].quirks |= QUIRK_FIELD_ORDERING;
- sensor_count++;
-}
-
-
-static void discover_poll_sensors (int dev_num, char map[CATALOG_SIZE])
-{
- char base_dir[PATH_MAX];
- DIR *dir;
- struct dirent *d;
- unsigned int i;
- int c;
-
- memset(map, 0, CATALOG_SIZE);
-
- snprintf(base_dir, sizeof(base_dir), BASE_PATH, dev_num);
-
- dir = opendir(base_dir);
- if (!dir) {
- return;
- }
-
- /* Enumerate entries in this iio device's base folder */
-
- while ((d = readdir(dir))) {
- if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
- continue;
-
- /* If the name matches a catalog entry, flag it */
- for (i = 0; i < CATALOG_SIZE; i++) {
-
- /* No discovery for virtual sensors */
- if (sensor_catalog[i].is_virtual)
- continue;
-
- for (c=0; c<sensor_catalog[i].num_channels; c++)
- if (!strcmp(d->d_name,sensor_catalog[i].channel[c].raw_path) || !strcmp(d->d_name, sensor_catalog[i].channel[c].input_path)) {
- map[i] = 1;
- break;
- }
- }
- }
-
- closedir(dir);
-}
-
-
-static void discover_trig_sensors (int dev_num, char map[CATALOG_SIZE])
-{
- char scan_elem_dir[PATH_MAX];
- DIR *dir;
- struct dirent *d;
- unsigned int i;
-
- memset(map, 0, CATALOG_SIZE);
-
- /* Enumerate entries in this iio device's scan_elements folder */
-
- snprintf(scan_elem_dir, sizeof(scan_elem_dir), CHANNEL_PATH, dev_num);
-
- dir = opendir(scan_elem_dir);
- if (!dir) {
- return;
- }
-
- while ((d = readdir(dir))) {
- if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
- continue;
-
- /* Compare en entry to known ones and create matching sensors */
+ sensor[s].needs_enable = get_needs_enable(dev_num, sensor_catalog[catalog_index].tag);
- for (i = 0; i<CATALOG_SIZE; i++) {
-
- /* No discovery for virtual sensors */
- if (sensor_catalog[i].is_virtual)
- continue;
-
- if (!strcmp(d->d_name, sensor_catalog[i].channel[0].en_path)) {
- map[i] = 1;
- break;
- }
- }
- }
-
- closedir(dir);
+ sensor_count++;
}
-
static void virtual_sensors_check (void)
{
int i;
int has_mag = 0;
int has_rot = 0;
int has_ori = 0;
- int catalog_size = CATALOG_SIZE;
int gyro_cal_idx = 0;
int magn_cal_idx = 0;
+ unsigned int j;
for (i=0; i<sensor_count; i++)
switch (sensor[i].type) {
break;
}
- for (i=0; i<catalog_size; i++)
- switch (sensor_catalog[i].type) {
+ for (j=0; j<catalog_size; j++)
+ switch (sensor_catalog[j].type) {
/*
- * If we have accel + gyro + magn but no rotation vector sensor,
- * SensorService replaces the HAL provided orientation sensor by the
- * AOSP version... provided we report one. So initialize a virtual
- * orientation sensor with zero values, which will get replaced. See:
- * frameworks/native/services/sensorservice/SensorService.cpp, looking
- * for SENSOR_TYPE_ROTATION_VECTOR; that code should presumably fall
- * back to mUserSensorList.add instead of replaceAt, but accommodate it.
- */
+ * If we have accel + gyro + magn but no rotation vector sensor,
+ * SensorService replaces the HAL provided orientation sensor by the
+ * AOSP version... provided we report one. So initialize a virtual
+ * orientation sensor with zero values, which will get replaced. See:
+ * frameworks/native/services/sensorservice/SensorService.cpp, looking
+ * for SENSOR_TYPE_ROTATION_VECTOR; that code should presumably fall
+ * back to mUserSensorList.add instead of replaceAt, but accommodate it.
+ */
case SENSOR_TYPE_ORIENTATION:
if (has_acc && has_gyr && has_mag && !has_rot && !has_ori)
- add_sensor(0, i, 1);
+ add_sensor(0, j, MODE_POLL);
break;
case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
if (has_gyr) {
sensor[sensor_count].base_count = 1;
sensor[sensor_count].base[0] = gyro_cal_idx;
- add_virtual_sensor(i);
+ add_virtual_sensor(j);
}
break;
case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED:
if (has_mag) {
sensor[sensor_count].base_count = 1;
sensor[sensor_count].base[0] = magn_cal_idx;
- add_virtual_sensor(i);
+ add_virtual_sensor(j);
}
break;
default:
- break;
+ break;
}
}
{
char filename[PATH_MAX];
char buf[MAX_NAME_SIZE];
- int len;
int s;
int trigger;
int ret;
strcpy(sensor[s].motion_trigger_name, sensor[s].init_trigger_name);
for (s=0; s<sensor_count; s++)
- if (!sensor[s].is_polling) {
+ if (sensor[s].mode == MODE_TRIGGER) {
ALOGI("Sensor %d (%s) default trigger: %s\n", s, sensor[s].friendly_name, sensor[s].init_trigger_name);
if (sensor[s].motion_trigger_name[0])
ALOGI("Sensor %d (%s) motion trigger: %s\n", s, sensor[s].friendly_name, sensor[s].motion_trigger_name);
* 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
* and trigger-based sensor, use the trigger usage mode.
*/
- char poll_sensors[CATALOG_SIZE];
- char trig_sensors[CATALOG_SIZE];
+ char poll_sensors[catalog_size];
+ char trig_sensors[catalog_size];
+ char event_sensors[catalog_size];
int dev_num;
unsigned int i;
int trig_found;
for (dev_num=0; dev_num<MAX_DEVICES; dev_num++) {
trig_found = 0;
- discover_poll_sensors(dev_num, poll_sensors);
- discover_trig_sensors(dev_num, trig_sensors);
+ discover_sensors(dev_num, BASE_PATH, poll_sensors, check_poll_sensors);
+ discover_sensors(dev_num, CHANNEL_PATH, trig_sensors, check_trig_sensors);
+ discover_sensors(dev_num, EVENTS_PATH, event_sensors, check_event_sensors);
- for (i=0; i<CATALOG_SIZE; i++)
+ for (i=0; i<catalog_size; i++) {
+ if (event_sensors[i]) {
+ add_sensor(dev_num, i, MODE_EVENT);
+ continue;
+ }
if (trig_sensors[i]) {
- add_sensor(dev_num, i, 0);
+ add_sensor(dev_num, i, MODE_TRIGGER);
trig_found = 1;
+ continue;
+ }
+ if (poll_sensors[i]) {
+ add_sensor(dev_num, i, MODE_POLL);
+ continue;
}
- else
- if (poll_sensors[i])
- add_sensor(dev_num, i, 1);
+ }
if (trig_found)
build_sensor_report_maps(dev_num);