/*
- * Copyright (C) 2014 Intel Corporation.
+ * Copyright (C) 2014-2015 Intel Corporation.
*/
#include <ctype.h>
},
{
.tag = "intensity",
- .type = SENSOR_TYPE_LIGHT,
+ .type = SENSOR_TYPE_INTERNAL_INTENSITY,
.num_channels = 1,
.is_virtual = 0,
.channel = {
},
{
.tag = "illuminance",
- .type = SENSOR_TYPE_LIGHT,
+ .type = SENSOR_TYPE_INTERNAL_ILLUMINANCE,
.num_channels = 1,
.is_virtual = 0,
.channel = {
},
},
},
+ {
+ .tag = "proximity",
+ .type = SENSOR_TYPE_PROXIMITY,
+ .num_channels = 4,
+ .is_virtual = 0,
+ .channel = {
+ {
+ DECLARE_VOID_CHANNEL("proximity0")
+ .num_events = 1,
+ .event = {
+ { DECLARE_EVENT("proximity0", "_", "", "", "thresh", "_", "either") },
+ },
+ },
+ {
+ DECLARE_VOID_CHANNEL("proximity1")
+ .num_events = 1,
+ .event = {
+ { DECLARE_EVENT("proximity1", "_", "", "", "thresh", "_", "either") },
+ },
+ },
+ {
+ DECLARE_VOID_CHANNEL("proximity2")
+ .num_events = 1,
+ .event = {
+ { DECLARE_EVENT("proximity2", "_", "", "", "thresh", "_", "either") },
+ },
+ },
+ {
+ DECLARE_VOID_CHANNEL("proximity3")
+ .num_events = 1,
+ .event = {
+ { DECLARE_EVENT("proximity3", "_", "", "", "thresh", "_", "either") },
+ },
+ },
+ },
+ },
};
-#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 */
}
+static int map_internal_to_external_type (int sensor_type)
+{
+ /* Most sensors are internally identified using the Android type, but for some we use a different type specification internally */
+
+ switch (sensor_type) {
+ case SENSOR_TYPE_INTERNAL_ILLUMINANCE:
+ case SENSOR_TYPE_INTERNAL_INTENSITY:
+ return SENSOR_TYPE_LIGHT;
+
+ default:
+ return sensor_type;
+ }
+}
+
static void populate_descriptors (int s, int sensor_type)
{
int32_t min_delay_us;
sensor_desc[s].vendor = sensor_get_vendor(s);
sensor_desc[s].version = sensor_get_version(s);
sensor_desc[s].handle = s;
- sensor_desc[s].type = sensor_type;
+ sensor_desc[s].type = map_internal_to_external_type(sensor_type);
sensor_desc[s].maxRange = sensor_get_max_range(s);
sensor_desc[s].resolution = sensor_get_resolution(s);
}
-static void add_sensor (int dev_num, int catalog_index, int mode)
+static int add_sensor (int dev_num, int catalog_index, int mode)
{
int s;
int sensor_type;
if (sensor_count == MAX_SENSORS) {
ALOGE("Too many sensors!\n");
- return;
+ return -1;
}
sensor_type = sensor_catalog[catalog_index].type;
else
sensor[s].num_channels = num_channels;
+ /* Populate the quirks array */
+ sensor_get_quirks(s);
+
+ /* Reject interfaces that may have been disabled through a quirk for this driver */
+ if ((mode == MODE_EVENT && (sensor[s].quirks & QUIRK_NO_EVENT_MODE)) ||
+ (mode == MODE_TRIGGER && (sensor[s].quirks & QUIRK_NO_TRIG_MODE )) ||
+ (mode == MODE_POLL && (sensor[s].quirks & QUIRK_NO_POLL_MODE ))) {
+ memset(&sensor[s], 0, sizeof(sensor[0]));
+ return -1;
+ }
+
prefix = sensor_catalog[catalog_index].tag;
/*
* receiving the illumination sensor calibration inputs from
* the Android properties and setting it within sysfs
*/
- if (sensor_type == SENSOR_TYPE_LIGHT) {
+ if (sensor_type == SENSOR_TYPE_INTERNAL_ILLUMINANCE) {
retval = sensor_get_illumincalib(s);
if (retval > 0) {
sprintf(sysfs_path, ILLUMINATION_CALIBPATH, dev_num);
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);
- /*
- * See if we have optional correction scaling factors for each of the
- * channels of this sensor. These would be expressed using properties
- * like iio.accel.y.opt_scale = -1. In case of a single channel we also
- * support things such as iio.temp.opt_scale = -1. Note that this works
- * for all types of sensors, and whatever transform is selected, on top
- * of any previous conversions.
- */
+ /*
+ * See if we have optional correction scaling factors for each of the
+ * channels of this sensor. These would be expressed using properties
+ * like iio.accel.y.opt_scale = -1. In case of a single channel we also
+ * support things such as iio.temp.opt_scale = -1. Note that this works
+ * for all types of sensors, and whatever transform is selected, on top
+ * of any previous conversions.
+ */
- if (num_channels) {
+ if (num_channels) {
for (c = 0; c < num_channels; c++) {
ch_name = sensor_catalog[catalog_index].channel[c].name;
sprintf(suffix, "%s.opt_scale", ch_name);
if (!sensor_get_fl_prop(s, suffix, &opt_scale))
sensor[s].channel[c].opt_scale = opt_scale;
}
- } else
+ } else {
if (!sensor_get_fl_prop(s, "opt_scale", &opt_scale))
sensor[s].channel[0].opt_scale = opt_scale;
+ }
populate_descriptors(s, sensor_type);
- /* Populate the quirks array */
- sensor_get_quirks(s);
-
if (sensor[s].internal_name[0] == '\0') {
/*
* In case the kernel-mode driver doesn't expose a name for
}
switch (sensor_type) {
- case SENSOR_TYPE_GYROSCOPE:
+ case SENSOR_TYPE_ACCELEROMETER:
+ /* Only engage accelerometer bias compensation if really needed */
+ if (sensor_get_quirks(s) & QUIRK_BIASED)
+ sensor[s].cal_data = calloc(1, sizeof(accel_cal_t));
+ break;
+
+ case SENSOR_TYPE_GYROSCOPE:
sensor[s].cal_data = malloc(sizeof(gyro_cal_t));
break;
sensor[s].needs_enable = get_needs_enable(dev_num, sensor_catalog[catalog_index].tag);
sensor_count++;
-}
-
-
-static void discover_sensors (int dev_num, char *sysfs_base_path, char map[CATALOG_SIZE],
- void (*discover_sensor)(int, char*, char*))
-{
- char sysfs_dir[PATH_MAX];
- DIR *dir;
- struct dirent *d;
- unsigned int i;
-
- memset(map, 0, CATALOG_SIZE);
-
- snprintf(sysfs_dir, sizeof(sysfs_dir), sysfs_base_path, dev_num);
-
- dir = opendir(sysfs_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;
- discover_sensor(i, d->d_name, map);
- }
- }
-
- closedir(dir);
-}
-
-static void check_poll_sensors (int i, char *sysfs_file, char map[CATALOG_SIZE])
-{
- int c;
-
- for (c = 0; c < sensor_catalog[i].num_channels; c++)
- if (!strcmp(sysfs_file, sensor_catalog[i].channel[c].raw_path) ||
- !strcmp(sysfs_file, sensor_catalog[i].channel[c].input_path)) {
- map[i] = 1;
- break;
- }
-}
-static void check_trig_sensors (int i, char *sysfs_file, char map[CATALOG_SIZE])
-{
-
- if (!strcmp(sysfs_file, sensor_catalog[i].channel[0].en_path)) {
- map[i] = 1;
- return;
- }
-}
-
-static void check_event_sensors(int i, char *sysfs_file, char map[CATALOG_SIZE])
-{
- int j, k;
-
- for (j = 0; j < sensor_catalog[i].num_channels; j++)
- for (k = 0; k < sensor_catalog[i].channel[j].num_events; k++)
- if (!strcmp(sysfs_file, sensor_catalog[i].channel[j].event[k].ev_en_path)) {
- map[i] = 1;
- return ;
- }
+ return 0;
}
static void virtual_sensors_check (void)
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, MODE_POLL);
+ 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;
}
}
* 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 event_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;
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++) {
- if (event_sensors[i]) {
- add_sensor(dev_num, i, MODE_EVENT);
+ for (i=0; i<catalog_size; i++) {
+ /* Try using events interface */
+ if (event_sensors[i] && !add_sensor(dev_num, i, MODE_EVENT))
continue;
- }
- if (trig_sensors[i]) {
- add_sensor(dev_num, i, MODE_TRIGGER);
+
+ /* Then trigger */
+ if (trig_sensors[i] && !add_sensor(dev_num, i, MODE_TRIGGER)) {
trig_found = 1;
continue;
}
- if (poll_sensors[i]) {
+
+ /* Try polling otherwise */
+ if (poll_sensors[i])
add_sensor(dev_num, i, MODE_POLL);
- continue;
- }
}
if (trig_found)