X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=enumeration.c;h=eda8845342a0fa7f682927fefee16237ea47c964;hb=b24d6584ce3c44cdb5ba9ebe6aa525ff7ecd477c;hp=335569c32798b302c60c50ec2bf166a89a11da6e;hpb=816bba686f5c70100cf825b3894e808f58e30ab8;p=android-x86%2Fhardware-intel-libsensors.git diff --git a/enumeration.c b/enumeration.c index 335569c..eda8845 100644 --- a/enumeration.c +++ b/enumeration.c @@ -2,12 +2,19 @@ * Copyright (C) 2014 Intel Corporation. */ +#include #include +#include #include #include #include "enumeration.h" #include "description.h" #include "utils.h" +#include "transform.h" +#include "description.h" +#include "control.h" +#include "calibration.h" +#include "filtering.h" /* * This table maps syfs entries in scan_elements directories to sensor types, @@ -15,20 +22,33 @@ * device number associated to a specific sensor. */ + /* + * We duplicate entries for the uncalibrated types after their respective base + * sensor. This is because all sensor entries must have an associated catalog entry + * and also because when only the uncal sensor is active it needs to take it's data + * from the same iio device as the base one. + */ + struct 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_TEMPERATURE ) - DECLARE_SENSOR0("timestamp", SENSOR_TYPE_DEVICE_PRIVATE_BASE ) + DECLARE_SENSOR0("temp", SENSOR_TYPE_AMBIENT_TEMPERATURE ) + DECLARE_SENSOR0("proximity", SENSOR_TYPE_PROXIMITY ) + DECLARE_SENSOR3("anglvel", SENSOR_TYPE_GYROSCOPE_UNCALIBRATED, "x", "y", "z") }; #define CATALOG_SIZE ARRAY_SIZE(sensor_catalog) +/* ACPI PLD (physical location of device) definitions, as used with sensors */ + +#define PANEL_FRONT 4 +#define PANEL_BACK 5 /* We equate sensor handles to indices in these tables */ @@ -37,13 +57,162 @@ struct sensor_info_t sensor_info[MAX_SENSORS]; /* Internal descriptors */ int sensor_count; /* Detected sensors */ +static void setup_properties_from_pld(int s, int panel, int rotation, + int num_channels) +{ + /* + * Generate suitable order and opt_scale directives from the PLD panel + * and rotation codes we got. This can later be superseded by the usual + * properties if necessary. Eventually we'll need to replace these + * mechanisms by a less convoluted one, such as a 3x3 placement matrix. + */ + + int x = 1; + int y = 1; + int z = 1; + int xy_swap = 0; + int angle = rotation * 45; + + /* Only deal with 3 axis chips for now */ + if (num_channels < 3) + return; + + if (panel == PANEL_BACK) { + /* Chip placed on the back panel ; negate x and z */ + x = -x; + z = -z; + } + + switch (angle) { + case 90: /* 90° clockwise: negate y then swap x,y */ + xy_swap = 1; + y = -y; + break; + + case 180: /* Upside down: negate x and y */ + x = -x; + y = -y; + break; + + case 270: /* 90° counter clockwise: negate x then swap x,y */ + x = -x; + xy_swap = 1; + break; + } + + if (xy_swap) { + sensor_info[s].order[0] = 1; + sensor_info[s].order[1] = 0; + sensor_info[s].order[2] = 2; + sensor_info[s].quirks |= QUIRK_FIELD_ORDERING; + } + + sensor_info[s].channel[0].opt_scale = x; + sensor_info[s].channel[1].opt_scale = y; + sensor_info[s].channel[2].opt_scale = z; +} + + +static int is_valid_pld (int panel, int rotation) +{ + if (panel != PANEL_FRONT && panel != PANEL_BACK) { + ALOGW("Unhandled PLD panel spec: %d\n", panel); + return 0; + } + + /* Only deal with 90° rotations for now */ + if (rotation < 0 || rotation > 7 || (rotation & 1)) { + ALOGW("Unhandled PLD rotation spec: %d\n", rotation); + return 0; + } + + return 1; +} + + +static int read_pld_from_properties (int s, int* panel, int* rotation) +{ + int p, r; + + if (sensor_get_prop(s, "panel", &p)) + return -1; + + if (sensor_get_prop(s, "rotation", &r)) + return -1; + + if (!is_valid_pld(p, r)) + return -1; + + *panel = p; + *rotation = r; + + ALOGI("S%d PLD from properties: panel=%d, rotation=%d\n", s, p, r); + + return 0; +} + + +static int read_pld_from_sysfs (int s, int dev_num, int* panel, int* rotation) +{ + char sysfs_path[PATH_MAX]; + int p,r; + + sprintf(sysfs_path, BASE_PATH "../firmware_node/pld/panel", dev_num); + + if (sysfs_read_int(sysfs_path, &p)) + return -1; + + sprintf(sysfs_path, BASE_PATH "../firmware_node/pld/rotation", dev_num); + + if (sysfs_read_int(sysfs_path, &r)) + return -1; + + if (!is_valid_pld(p, r)) + return -1; + + *panel = p; + *rotation = r; + + ALOGI("S%d PLD from sysfs: panel=%d, rotation=%d\n", s, p, r); + + return 0; +} + + +static void decode_placement_information (int dev_num, int num_channels, int s) +{ + /* + * See if we have optional "physical location of device" ACPI tags. + * We're only interested in panel and rotation specifiers. Use the + * .panel and .rotation properties in priority, and the actual ACPI + * values as a second source. + */ + + int panel; + int rotation; + + if (read_pld_from_properties(s, &panel, &rotation) && + read_pld_from_sysfs(s, dev_num, &panel, &rotation)) + return; /* No PLD data available */ + + /* Map that to field ordering and scaling mechanisms */ + setup_properties_from_pld(s, panel, rotation, num_channels); +} + + static void add_sensor (int dev_num, int catalog_index, int use_polling) { int s; int sensor_type; + int retval; char sysfs_path[PATH_MAX]; const char* prefix; float scale; + int c; + float opt_scale; + const char* ch_name; + int num_channels; + char suffix[MAX_NAME_SIZE + 8]; if (sensor_count == MAX_SENSORS) { ALOGE("Too many sensors!\n"); @@ -63,29 +232,91 @@ static void add_sensor (int dev_num, int catalog_index, int use_polling) sensor_info[s].dev_num = dev_num; sensor_info[s].catalog_index = catalog_index; + num_channels = sensor_catalog[catalog_index].num_channels; + if (use_polling) sensor_info[s].num_channels = 0; else - sensor_info[s].num_channels = - sensor_catalog[catalog_index].num_channels; + sensor_info[s].num_channels = num_channels; prefix = sensor_catalog[catalog_index].tag; + /* + * receiving the illumination sensor calibration inputs from + * the Android properties and setting it within sysfs + */ + if (sensor_catalog[catalog_index].type == SENSOR_TYPE_LIGHT) { + retval = sensor_get_illumincalib(s); + if (retval > 0) { + sprintf(sysfs_path, ILLUMINATION_CALIBPATH, dev_num); + sysfs_write_int(sysfs_path, retval); + } + } + /* Read name attribute, if available */ sprintf(sysfs_path, NAME_PATH, dev_num); sysfs_read_str(sysfs_path, sensor_info[s].internal_name, MAX_NAME_SIZE); /* See if we have general offsets and scale values for this sensor */ - sprintf(sysfs_path, COMMON_OFFSET_PATH, dev_num, prefix); + sprintf(sysfs_path, SENSOR_OFFSET_PATH, dev_num, prefix); sysfs_read_float(sysfs_path, &sensor_info[s].offset); - sprintf(sysfs_path, COMMON_SCALE_PATH, dev_num, prefix); - if (!sysfs_read_float(sysfs_path, &scale)) + sprintf(sysfs_path, SENSOR_SCALE_PATH, dev_num, prefix); + if (!sysfs_read_float(sysfs_path, &scale)) { sensor_info[s].scale = scale; - else + ALOGI("Scale path:%s scale:%f dev_num:%d\n", + sysfs_path, scale, dev_num); + } else { sensor_info[s].scale = 1; + /* Read channel specific scale if any*/ + for (c = 0; c < num_channels; c++) + { + sprintf(sysfs_path, BASE_PATH "%s", dev_num, + sensor_catalog[catalog_index].channel[c].scale_path); + + if (!sysfs_read_float(sysfs_path, &scale)) { + sensor_info[s].channel[c].scale = scale; + sensor_info[s].scale = 0; + + ALOGI( "Scale path:%s " + "channel scale:%f dev_num:%d\n", + sysfs_path, scale, dev_num); + } + } + } + + /* Set default scaling - if num_channels is zero, we have one channel */ + + sensor_info[s].channel[0].opt_scale = 1; + + for (c = 1; c < num_channels; c++) + sensor_info[s].channel[c].opt_scale = 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. + */ + + 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_info[s].channel[c].opt_scale = opt_scale; + } + } else + if (!sensor_get_fl_prop(s, "opt_scale", &opt_scale)) + sensor_info[s].channel[0].opt_scale = opt_scale; + /* Initialize Android-visible descriptor */ sensor_desc[s].name = sensor_get_name(s); sensor_desc[s].vendor = sensor_get_vendor(s); @@ -95,6 +326,24 @@ static void add_sensor (int dev_num, int catalog_index, int use_polling) sensor_desc[s].maxRange = sensor_get_max_range(s); sensor_desc[s].resolution = sensor_get_resolution(s); sensor_desc[s].power = sensor_get_power(s); + sensor_desc[s].stringType = sensor_get_string_type(s); + + /* None of our supported sensors requires a special permission. + * If this will be the case we should implement a sensor_get_perm + */ + sensor_desc[s].requiredPermission = ""; + sensor_desc[s].flags = sensor_get_flags(s); + sensor_desc[s].minDelay = sensor_get_min_delay(s); + sensor_desc[s].maxDelay = sensor_get_max_delay(s); + ALOGI("Sensor %d (%s) type(%d) minD(%ld) maxD(%ld) flags(%2.2x)\n", + s, sensor_info[s].friendly_name, sensor_desc[s].type, + sensor_desc[s].minDelay, sensor_desc[s].maxDelay, sensor_desc[s].flags); + + /* We currently do not implement batching when we'll so + * these should be overriden appropriately + */ + sensor_desc[s].fifoReservedEventCount = 0; + sensor_desc[s].fifoMaxEventCount = 0; if (sensor_info[s].internal_name[0] == '\0') { /* @@ -106,6 +355,40 @@ static void add_sensor (int dev_num, int catalog_index, int use_polling) strcpy(sensor_info[s].internal_name, "(null)"); } + if (sensor_type == SENSOR_TYPE_GYROSCOPE || + sensor_type == SENSOR_TYPE_GYROSCOPE_UNCALIBRATED) { + struct gyro_cal* calibration_data = calloc(1, sizeof(struct gyro_cal)); + sensor_info[s].cal_data = calibration_data; + struct filter* f_data = (struct filter*) calloc(1, sizeof(struct filter)); + f_data->x_buff = (struct circ_buff*) calloc(1, sizeof (struct circ_buff)); + f_data->y_buff = (struct circ_buff*) calloc(1, sizeof (struct circ_buff)); + f_data->z_buff = (struct circ_buff*) calloc(1, sizeof (struct circ_buff)); + f_data->x_buff->buff = (float*)calloc(SAMPLE_SIZE, sizeof(float)); + f_data->y_buff->buff = (float*)calloc(SAMPLE_SIZE, sizeof(float)); + f_data->z_buff->buff = (float*)calloc(SAMPLE_SIZE, sizeof(float)); + f_data->x_buff->size = SAMPLE_SIZE; + f_data->y_buff->size = SAMPLE_SIZE; + f_data->z_buff->size = SAMPLE_SIZE; + sensor_info[s].filter = f_data; + } + + if (sensor_type == SENSOR_TYPE_MAGNETIC_FIELD) { + struct compass_cal* calibration_data = calloc(1, sizeof(struct compass_cal)); + sensor_info[s].cal_data = calibration_data; + } + + /* Select one of the available sensor sample processing styles */ + select_transform(s); + + /* Initialize fields related to sysfs reads offloading */ + sensor_info[s].thread_data_fd[0] = -1; + sensor_info[s].thread_data_fd[1] = -1; + sensor_info[s].acquisition_thread = -1; + + /* Check if we have a special ordering property on this sensor */ + if (sensor_get_order(s, sensor_info[s].order)) + sensor_info[s].quirks |= QUIRK_FIELD_ORDERING; + sensor_count++; } @@ -114,8 +397,6 @@ static void discover_poll_sensors (int dev_num, char map[CATALOG_SIZE]) { char base_dir[PATH_MAX]; DIR *dir; - char sysfs_dir[PATH_MAX]; - struct sensor *sensor; struct dirent *d; unsigned int i; int c; @@ -126,7 +407,7 @@ static void discover_poll_sensors (int dev_num, char map[CATALOG_SIZE]) dir = opendir(base_dir); if (!dir) { - return; + return; } /* Enumerate entries in this iio device's base folder */ @@ -135,16 +416,18 @@ static void discover_poll_sensors (int dev_num, char map[CATALOG_SIZE]) if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, "..")) continue; - /* If the name matches a catalog entry, flag it */ - for (i = 0; id_name, - sensor_catalog[i].channel[c].raw_path) || - !strcmp(d->d_name, - sensor_catalog[i].channel[c].input_path)) { - map[i] = 1; - break; - } + /* If the name matches a catalog entry, flag it */ + for (i = 0; id_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); @@ -155,12 +438,10 @@ static void discover_trig_sensors (int dev_num, char map[CATALOG_SIZE]) { char scan_elem_dir[PATH_MAX]; DIR *dir; - char sysfs_dir[PATH_MAX]; - struct sensor *sensor; struct dirent *d; unsigned int i; - memset(map, 0, CATALOG_SIZE); + memset(map, 0, CATALOG_SIZE); /* Enumerate entries in this iio device's scan_elements folder */ @@ -168,7 +449,7 @@ static void discover_trig_sensors (int dev_num, char map[CATALOG_SIZE]) dir = opendir(scan_elem_dir); if (!dir) { - return; + return; } while ((d = readdir(dir))) { @@ -177,18 +458,286 @@ static void discover_trig_sensors (int dev_num, char map[CATALOG_SIZE]) /* Compare en entry to known ones and create matching sensors */ - for (i = 0; id_name, - sensor_catalog[i].channel[0].en_path)) { - map[i] = 1; - break; - } + sensor_catalog[i].channel[0].en_path)) { + map[i] = 1; + break; + } + } } closedir(dir); } +static void orientation_sensor_check(void) +{ + /* + * 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. + */ + + int i; + int has_acc = 0; + int has_gyr = 0; + int has_mag = 0; + int has_rot = 0; + int has_ori = 0; + int catalog_size = CATALOG_SIZE; + + for (i=0; ix_buff->buff); + free(((struct filter*)sensor_info[i].filter)->y_buff->buff); + free(((struct filter*)sensor_info[i].filter)->z_buff->buff); + free(((struct filter*)sensor_info[i].filter)->x_buff); + free(((struct filter*)sensor_info[i].filter)->y_buff); + free(((struct filter*)sensor_info[i].filter)->z_buff); + free(sensor_info[i].filter); + sensor_info[i].filter = NULL; + } + default: + break; + } /* Reset sensor count */ sensor_count = 0; }