OSDN Git Service

Merge branch 'lineage-16.0' of https://github.com/me176c-dev/android_hardware_iio...
[android-x86/hardware-intel-libsensors.git] / enumeration.c
index fa02c28..359fa90 100644 (file)
@@ -1,12 +1,25 @@
 /*
- * Copyright (C) 2014-2015 Intel Corporation.
- */
+// Copyright (c) 2015 Intel Corporation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+*/
 
 #include <ctype.h>
 #include <dirent.h>
 #include <stdlib.h>
 #include <fcntl.h>
 #include <utils/Log.h>
+#include <sys/stat.h>
 #include <hardware/sensors.h>
 #include "enumeration.h"
 #include "description.h"
@@ -16,6 +29,8 @@
 #include "control.h"
 #include "calibration.h"
 
+#include <errno.h>
+
 /*
  * This table maps syfs entries in scan_elements directories to sensor types,
  * and will also be used to determine other sysfs names as well as the iio
@@ -32,6 +47,7 @@
 sensor_catalog_entry_t sensor_catalog[] = {
        {
                .tag            = "accel",
+               .shorthand      = "",
                .type           = SENSOR_TYPE_ACCELEROMETER,
                .num_channels   = 3,
                .is_virtual     = 0,
@@ -43,6 +59,7 @@ sensor_catalog_entry_t sensor_catalog[] = {
        },
        {
                .tag            = "anglvel",
+               .shorthand      = "",
                .type           = SENSOR_TYPE_GYROSCOPE,
                .num_channels   = 3,
                .is_virtual     = 0,
@@ -54,6 +71,7 @@ sensor_catalog_entry_t sensor_catalog[] = {
        },
        {
                .tag            = "magn",
+               .shorthand      = "",
                .type           = SENSOR_TYPE_MAGNETIC_FIELD,
                .num_channels   = 3,
                .is_virtual     = 0,
@@ -65,6 +83,7 @@ sensor_catalog_entry_t sensor_catalog[] = {
        },
        {
                .tag            = "intensity",
+               .shorthand      = "",
                .type           = SENSOR_TYPE_INTERNAL_INTENSITY,
                .num_channels   = 1,
                .is_virtual     = 0,
@@ -74,6 +93,7 @@ sensor_catalog_entry_t sensor_catalog[] = {
        },
        {
                .tag            = "illuminance",
+               .shorthand      = "",
                .type           = SENSOR_TYPE_INTERNAL_ILLUMINANCE,
                .num_channels   = 1,
                .is_virtual     = 0,
@@ -83,6 +103,7 @@ sensor_catalog_entry_t sensor_catalog[] = {
        },
        {
                .tag            = "incli",
+               .shorthand      = "",
                .type           = SENSOR_TYPE_ORIENTATION,
                .num_channels   = 3,
                .is_virtual     = 0,
@@ -94,6 +115,7 @@ sensor_catalog_entry_t sensor_catalog[] = {
        },
        {
                .tag            = "rot",
+               .shorthand      = "",
                .type           = SENSOR_TYPE_ROTATION_VECTOR,
                .num_channels   = 4,
                .is_virtual     = 0,
@@ -106,6 +128,7 @@ sensor_catalog_entry_t sensor_catalog[] = {
        },
        {
                .tag            = "temp",
+               .shorthand      = "",
                .type           = SENSOR_TYPE_AMBIENT_TEMPERATURE,
                .num_channels   = 1,
                .is_virtual     = 0,
@@ -115,6 +138,7 @@ sensor_catalog_entry_t sensor_catalog[] = {
        },
        {
                .tag            = "proximity",
+               .shorthand      = "prox",
                .type           = SENSOR_TYPE_PROXIMITY,
                .num_channels   = 1,
                .is_virtual     = 0,
@@ -124,6 +148,7 @@ sensor_catalog_entry_t sensor_catalog[] = {
        },
        {
                .tag            = "",
+               .shorthand      = "",
                .type           = SENSOR_TYPE_GYROSCOPE_UNCALIBRATED,
                .num_channels   = 0,
                .is_virtual     = 1,
@@ -134,6 +159,7 @@ sensor_catalog_entry_t sensor_catalog[] = {
        },
        {
                .tag            = "",
+               .shorthand      = "",
                .type           = SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED,
                .num_channels   = 0,
                .is_virtual     = 1,
@@ -143,6 +169,7 @@ sensor_catalog_entry_t sensor_catalog[] = {
        },
        {
                .tag            = "steps",
+               .shorthand      = "",
                .type           = SENSOR_TYPE_STEP_COUNTER,
                .num_channels   = 1,
                .is_virtual     = 0,
@@ -152,6 +179,7 @@ sensor_catalog_entry_t sensor_catalog[] = {
        },
        {
                .tag            = "steps",
+               .shorthand      = "",
                .type           = SENSOR_TYPE_STEP_DETECTOR,
                .num_channels   = 1,
                .is_virtual     = 0,
@@ -167,6 +195,7 @@ sensor_catalog_entry_t sensor_catalog[] = {
        },
        {
                .tag            = "proximity",
+               .shorthand      = "prox",
                .type           = SENSOR_TYPE_PROXIMITY,
                .num_channels   = 4,
                .is_virtual     = 0,
@@ -210,6 +239,9 @@ unsigned int catalog_size = ARRAY_SIZE(sensor_catalog);
 #define PANEL_FRONT    4
 #define PANEL_BACK     5
 
+/* Buffer default length */
+#define BUFFER_LENGTH  16
+
 /* We equate sensor handles to indices in these tables */
 
 struct sensor_t        sensor_desc[MAX_SENSORS];       /* Android-level descriptors */
@@ -414,7 +446,7 @@ static void populate_descriptors (int s, int sensor_type)
        sensor_desc[s].minDelay = sensor_get_min_delay(s);
        sensor_desc[s].maxDelay = sensor_get_max_delay(s);
 
-       ALOGV("Sensor %d (%s) type(%d) minD(%d) maxD(%d) flags(%2.2x)\n",
+       ALOGV("Sensor %d (%s) type(%d) minD(%d) maxD(%zd) flags(%2.2zx)\n",
                s, sensor[s].friendly_name, sensor_desc[s].type,
                sensor_desc[s].minDelay, sensor_desc[s].maxDelay,
                sensor_desc[s].flags);
@@ -474,6 +506,7 @@ static int add_sensor (int dev_num, int catalog_index, int mode)
        int num_channels;
        char suffix[MAX_NAME_SIZE + 8];
        int calib_bias;
+       int buffer_length;
 
        if (sensor_count == MAX_SENSORS) {
                ALOGE("Too many sensors!\n");
@@ -494,6 +527,7 @@ static int add_sensor (int dev_num, int catalog_index, int mode)
        sensor[s].catalog_index = catalog_index;
        sensor[s].type          = sensor_type;
        sensor[s].mode          = mode;
+       sensor[s].trigger_nr = -1;      /* -1 means no trigger - we'll populate these at a later time */
 
         num_channels = sensor_catalog[catalog_index].num_channels;
 
@@ -549,6 +583,19 @@ static int add_sensor (int dev_num, int catalog_index, int mode)
                                sysfs_write_int(sysfs_path, calib_bias);
                        }
 
+       /* Change buffer length according to the property or use default value */
+       if (mode == MODE_TRIGGER) {
+                if (sensor_get_prop(s, "buffer_length", &buffer_length)) {
+                        buffer_length = BUFFER_LENGTH;
+                }
+
+                sprintf(sysfs_path, BUFFER_LENGTH_PATH, dev_num);
+
+                if (sysfs_write_int(sysfs_path, buffer_length) <= 0) {
+                        ALOGE("Failed to set buffer length on dev%d", dev_num);
+                }
+        }
+
        /* Read name attribute, if available */
        sprintf(sysfs_path, NAME_PATH, dev_num);
        sysfs_read_str(sysfs_path, sensor[s].internal_name, MAX_NAME_SIZE);
@@ -604,10 +651,10 @@ static int add_sensor (int dev_num, int catalog_index, int mode)
 
         /* Set default scaling - if num_channels is zero, we have one channel */
 
-       sensor[s].channel[0].opt_scale = 1;
+       sensor[s].channel[0].opt_scale = (sensor_type == SENSOR_TYPE_ACCELEROMETER) ? -1 : 1;
 
        for (c = 1; c < num_channels; c++)
-               sensor[s].channel[c].opt_scale = 1;
+               sensor[s].channel[c].opt_scale = sensor[s].channel[0].opt_scale;
 
        for (c = 0; c < num_channels; c++) {
                /* Check the presence of the channel's input_path */
@@ -620,8 +667,13 @@ static int add_sensor (int dev_num, int catalog_index, int mode)
                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);
+       sensor_get_available_frequencies(s);
+
+       if (sensor_get_mounting_matrix(s, sensor[s].mounting_matrix))
+               sensor[s].quirks |= QUIRK_MOUNTING_MATRIX;
+       else
+               /* 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
@@ -637,7 +689,7 @@ static int add_sensor (int dev_num, int catalog_index, int mode)
                        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;
+                               sensor[s].channel[c].opt_scale *= opt_scale;
                }
         } else {
                if (!sensor_get_fl_prop(s, "opt_scale", &opt_scale))
@@ -782,6 +834,11 @@ static void propose_new_trigger (int s, char trigger_name[MAX_NAME_SIZE],
                return;
        }
 
+       /* If we found a hrtimer trigger, record it */
+       if (!memcmp(suffix, "hr-dev", 6)) {
+               strcpy(sensor[s].hrtimer_trigger_name, trigger_name);
+               return;
+       }
        /*
         * 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
         * of the trigger to use with this sensor.
@@ -790,7 +847,7 @@ static void propose_new_trigger (int s, char trigger_name[MAX_NAME_SIZE],
 }
 
 
-static void update_sensor_matching_trigger_name (char name[MAX_NAME_SIZE])
+static void update_sensor_matching_trigger_name (char name[MAX_NAME_SIZE], int* updated, int trigger)
 {
        /*
         * Check if we have a sensor matching the specified trigger name, which should then begin with the sensor name, and end with a number
@@ -834,9 +891,60 @@ static void update_sensor_matching_trigger_name (char name[MAX_NAME_SIZE])
                        if (!strncmp(name, sensor[s].internal_name, sensor_name_len))
                                /* Switch to new trigger if appropriate */
                                propose_new_trigger(s, name, sensor_name_len);
+                               updated[s] = 1;
+                               sensor[s].trigger_nr = trigger;
                }
 }
 
+extern float sensor_get_max_static_freq(int s);
+extern float sensor_get_min_freq (int s);
+
+static int create_hrtimer_trigger(int s, int trigger)
+{
+       struct stat dir_status;
+       char buf[MAX_NAME_SIZE];
+       char hrtimer_path[PATH_MAX];
+       char hrtimer_name[MAX_NAME_SIZE];
+       float min_supported_rate = 1, min_rate_cap, max_supported_rate;
+
+       snprintf(buf, MAX_NAME_SIZE, "hrtimer-%s-hr-dev%d", sensor[s].internal_name, sensor[s].dev_num);
+       snprintf(hrtimer_name, MAX_NAME_SIZE, "%s-hr-dev%d", sensor[s].internal_name, sensor[s].dev_num);
+       snprintf(hrtimer_path, PATH_MAX, "%s%s", CONFIGFS_TRIGGER_PATH, buf);
+
+       /* Get parent dir status */
+       if (stat(CONFIGFS_TRIGGER_PATH, &dir_status))
+               return -1;
+
+       /* Create hrtimer with the same access rights as it's parent */
+       if (mkdir(hrtimer_path, dir_status.st_mode))
+               if (errno != EEXIST)
+                       return -1;
+
+       strncpy (sensor[s].hrtimer_trigger_name, hrtimer_name, MAX_NAME_SIZE);
+       sensor[s].trigger_nr = trigger;
+
+       max_supported_rate = sensor_get_max_static_freq(s);
+
+       /* set 0 for wrong values */
+       if (max_supported_rate < 0.1) {
+               max_supported_rate = 0;
+       }
+
+       sensor[s].max_supported_rate = max_supported_rate;
+       sensor_desc[s].minDelay = max_supported_rate ? (int32_t) (1000000.0 / max_supported_rate) : 0;
+
+       /* Check if a minimum rate was specified for this sensor */
+       min_rate_cap = sensor_get_min_freq(s);
+
+       if (min_supported_rate < min_rate_cap) {
+               min_supported_rate = min_rate_cap;
+       }
+
+       sensor[s].min_supported_rate = min_supported_rate;
+       sensor_desc[s].maxDelay = (max_delay_t) (1000000.0 / min_supported_rate);
+
+       return 0;
+}
 
 static void setup_trigger_names (void)
 {
@@ -845,6 +953,7 @@ static void setup_trigger_names (void)
        int s;
        int trigger;
        int ret;
+       int updated[MAX_SENSORS] = {0};
 
        /* By default, use the name-dev convention that most drivers use */
        for (s=0; s<sensor_count; s++)
@@ -854,7 +963,7 @@ static void setup_trigger_names (void)
 
        for (trigger=0; trigger<MAX_TRIGGERS; trigger++) {
 
-               snprintf(filename, sizeof(filename), TRIGGER_FILE_PATH,trigger);
+               snprintf(filename, sizeof(filename), TRIGGER_FILE_PATH, trigger);
 
                ret = sysfs_read_str(filename, buf, sizeof(buf));
 
@@ -862,7 +971,16 @@ static void setup_trigger_names (void)
                        break;
 
                /* Record initial and any-motion triggers names */
-               update_sensor_matching_trigger_name(buf);
+               update_sensor_matching_trigger_name(buf, updated, trigger);
+       }
+
+
+       /* If we don't have any other trigger exposed and quirk hrtimer is set setup the hrtimer name here  - and create it also */
+       for (s=0; s<sensor_count && trigger<MAX_TRIGGERS; s++) {
+               if ((sensor[s].quirks & QUIRK_HRTIMER) && !updated[s]) {
+                       create_hrtimer_trigger(s, trigger);
+                       trigger++;
+               }
        }
 
        /*
@@ -879,9 +997,89 @@ static void setup_trigger_names (void)
                        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);
+                       if (sensor[s].hrtimer_trigger_name[0])
+                               ALOGI("Sensor %d (%s) hrtimer trigger: %s\n", s, sensor[s].friendly_name, sensor[s].hrtimer_trigger_name);
                }
 }
 
+
+static int catalog_index_from_sensor_type (int type)
+{
+       /* Return first matching catalog entry index for selected type */
+       unsigned int i;
+
+       for (i=0; i<catalog_size; i++)
+               if (sensor_catalog[i].type == type)
+                       return i;
+
+       return -1;
+}
+
+
+static void post_process_sensor_list (char poll_map[catalog_size], char trig_map[catalog_size], char event_map[catalog_size])
+{
+       int illuminance_cat_index = catalog_index_from_sensor_type(SENSOR_TYPE_INTERNAL_ILLUMINANCE);
+       int intensity_cat_index   = catalog_index_from_sensor_type(SENSOR_TYPE_INTERNAL_INTENSITY);
+       int illuminance_found     = poll_map[illuminance_cat_index] || trig_map[illuminance_cat_index] || event_map[illuminance_cat_index];
+
+       /* If an illumimance sensor has been reported */
+       if (illuminance_found) {
+               /* Hide any intensity sensors we can have for the same iio device */
+               poll_map [intensity_cat_index     ] = 0;
+               trig_map [intensity_cat_index     ] = 0;
+               event_map[intensity_cat_index     ] = 0;
+               return;
+       }
+}
+
+
+static void swap_sensors (int s1, int s2)
+{
+       struct sensor_t temp_sensor_desc;
+       sensor_info_t   temp_sensor;
+
+       /* S1 -> temp */
+       memcpy(&temp_sensor, &sensor[s1], sizeof(sensor_info_t));
+       memcpy(&temp_sensor_desc, &sensor_desc[s1], sizeof(struct sensor_t));
+
+       /* S2 -> S1 */
+       memcpy(&sensor[s1], &sensor[s2], sizeof(sensor_info_t));
+       memcpy(&sensor_desc[s1], &sensor_desc[s2], sizeof(struct sensor_t));
+
+       /* temp -> S2 */
+       memcpy(&sensor[s2], &temp_sensor, sizeof(sensor_info_t));
+       memcpy(&sensor_desc[s2], &temp_sensor_desc,  sizeof(struct sensor_t));
+
+       /* Fix-up sensor id mapping, which is stale */
+       sensor_desc[s1].handle  = s1;
+       sensor_desc[s2].handle  = s2;
+
+       /* Fix up name and vendor buffer pointers, which are potentially stale pointers */
+       sensor_desc[s1].name            = sensor_get_name(s1);
+       sensor_desc[s1].vendor          = sensor_get_vendor(s1);
+       sensor_desc[s2].name            = sensor_get_name(s2);
+       sensor_desc[s2].vendor          = sensor_get_vendor(s2);
+}
+
+
+static void reorder_sensors (void)
+{
+       /* Some sensors may be marked as secondary - these need to be listed after other sensors of the same type */
+       int s1, s2;
+
+       for (s1=0; s1<sensor_count-1; s1++)
+               if (sensor[s1].quirks & QUIRK_SECONDARY) {
+                       /* Search for subsequent sensors of same type */
+                       for (s2 = s1+1; s2<sensor_count; s2++)
+                               if (sensor[s2].type == sensor[s1].type && !(sensor[s2].quirks & QUIRK_SECONDARY)) {
+                                       ALOGI("Sensor S%d has higher priority than S%d, swapping\n", s2, s1);
+                                       swap_sensors(s1, s2);
+                                       break;
+                               }
+               }
+}
+
+
 void enumerate_sensors (void)
 {
        /*
@@ -895,6 +1093,7 @@ void enumerate_sensors (void)
        int dev_num;
        unsigned int i;
        int trig_found;
+       int s;
 
        for (dev_num=0; dev_num<MAX_DEVICES; dev_num++) {
                trig_found = 0;
@@ -903,6 +1102,9 @@ void enumerate_sensors (void)
                discover_sensors(dev_num, CHANNEL_PATH, trig_sensors, check_trig_sensors);
                discover_sensors(dev_num, EVENTS_PATH, event_sensors, check_event_sensors);
 
+               /* Hide specific sensor types if appropriate */
+               post_process_sensor_list(poll_sensors, trig_sensors, event_sensors);
+
                for (i=0; i<catalog_size; i++) {
                        /* Try using events interface */
                        if (event_sensors[i] && !add_sensor(dev_num, i, MODE_EVENT))
@@ -923,12 +1125,21 @@ void enumerate_sensors (void)
                        build_sensor_report_maps(dev_num);
        }
 
+       /* Make sure secondary sensors appear after primary ones */
+       reorder_sensors();
+
        ALOGI("Discovered %d sensors\n", sensor_count);
 
        /* Set up default - as well as custom - trigger names */
        setup_trigger_names();
 
+       ALOGI("Discovered %d sensors\n", sensor_count);
+
        virtual_sensors_check();
+
+       for (s=0; s<sensor_count; s++) {
+               ALOGI("S%d: %s\n", s, sensor[s].friendly_name);
+       }
 }