OSDN Git Service

Merge remote-tracking branch 'origin/abt/topic/gmin/l-dev/mr1/sensors/master' into...
[android-x86/hardware-intel-libsensors.git] / enumeration.c
index 0d744a9..c8fa34a 100644 (file)
@@ -7,6 +7,7 @@
 #include <stdlib.h>
 #include <fcntl.h>
 #include <utils/Log.h>
+#include <sys/stat.h>
 #include <hardware/sensors.h>
 #include "enumeration.h"
 #include "description.h"
@@ -32,6 +33,7 @@
 sensor_catalog_entry_t sensor_catalog[] = {
        {
                .tag            = "accel",
+               .shorthand      = "",
                .type           = SENSOR_TYPE_ACCELEROMETER,
                .num_channels   = 3,
                .is_virtual     = 0,
@@ -43,6 +45,7 @@ sensor_catalog_entry_t sensor_catalog[] = {
        },
        {
                .tag            = "anglvel",
+               .shorthand      = "",
                .type           = SENSOR_TYPE_GYROSCOPE,
                .num_channels   = 3,
                .is_virtual     = 0,
@@ -54,6 +57,7 @@ sensor_catalog_entry_t sensor_catalog[] = {
        },
        {
                .tag            = "magn",
+               .shorthand      = "",
                .type           = SENSOR_TYPE_MAGNETIC_FIELD,
                .num_channels   = 3,
                .is_virtual     = 0,
@@ -65,7 +69,8 @@ sensor_catalog_entry_t sensor_catalog[] = {
        },
        {
                .tag            = "intensity",
-               .type           = SENSOR_TYPE_LIGHT,
+               .shorthand      = "",
+               .type           = SENSOR_TYPE_INTERNAL_INTENSITY,
                .num_channels   = 1,
                .is_virtual     = 0,
                .channel = {
@@ -74,7 +79,8 @@ sensor_catalog_entry_t sensor_catalog[] = {
        },
        {
                .tag            = "illuminance",
-               .type           = SENSOR_TYPE_LIGHT,
+               .shorthand      = "",
+               .type           = SENSOR_TYPE_INTERNAL_ILLUMINANCE,
                .num_channels   = 1,
                .is_virtual     = 0,
                .channel = {
@@ -83,6 +89,7 @@ sensor_catalog_entry_t sensor_catalog[] = {
        },
        {
                .tag            = "incli",
+               .shorthand      = "",
                .type           = SENSOR_TYPE_ORIENTATION,
                .num_channels   = 3,
                .is_virtual     = 0,
@@ -94,6 +101,7 @@ sensor_catalog_entry_t sensor_catalog[] = {
        },
        {
                .tag            = "rot",
+               .shorthand      = "",
                .type           = SENSOR_TYPE_ROTATION_VECTOR,
                .num_channels   = 4,
                .is_virtual     = 0,
@@ -106,6 +114,7 @@ sensor_catalog_entry_t sensor_catalog[] = {
        },
        {
                .tag            = "temp",
+               .shorthand      = "",
                .type           = SENSOR_TYPE_AMBIENT_TEMPERATURE,
                .num_channels   = 1,
                .is_virtual     = 0,
@@ -115,6 +124,7 @@ sensor_catalog_entry_t sensor_catalog[] = {
        },
        {
                .tag            = "proximity",
+               .shorthand      = "prox",
                .type           = SENSOR_TYPE_PROXIMITY,
                .num_channels   = 1,
                .is_virtual     = 0,
@@ -124,6 +134,7 @@ sensor_catalog_entry_t sensor_catalog[] = {
        },
        {
                .tag            = "",
+               .shorthand      = "",
                .type           = SENSOR_TYPE_GYROSCOPE_UNCALIBRATED,
                .num_channels   = 0,
                .is_virtual     = 1,
@@ -134,6 +145,7 @@ sensor_catalog_entry_t sensor_catalog[] = {
        },
        {
                .tag            = "",
+               .shorthand      = "",
                .type           = SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED,
                .num_channels   = 0,
                .is_virtual     = 1,
@@ -143,6 +155,7 @@ sensor_catalog_entry_t sensor_catalog[] = {
        },
        {
                .tag            = "steps",
+               .shorthand      = "",
                .type           = SENSOR_TYPE_STEP_COUNTER,
                .num_channels   = 1,
                .is_virtual     = 0,
@@ -152,6 +165,7 @@ sensor_catalog_entry_t sensor_catalog[] = {
        },
        {
                .tag            = "steps",
+               .shorthand      = "",
                .type           = SENSOR_TYPE_STEP_DETECTOR,
                .num_channels   = 1,
                .is_virtual     = 0,
@@ -167,6 +181,7 @@ sensor_catalog_entry_t sensor_catalog[] = {
        },
        {
                .tag            = "proximity",
+               .shorthand      = "prox",
                .type           = SENSOR_TYPE_PROXIMITY,
                .num_channels   = 4,
                .is_virtual     = 0,
@@ -376,6 +391,20 @@ static void decode_placement_information (int dev_num, int num_channels, int s)
 }
 
 
+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;
@@ -386,7 +415,7 @@ static void populate_descriptors (int s, int sensor_type)
        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);
@@ -446,7 +475,7 @@ static void add_virtual_sensor (int catalog_index)
 }
 
 
-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;
@@ -463,7 +492,7 @@ static void add_sensor (int dev_num, int catalog_index, int mode)
 
        if (sensor_count == MAX_SENSORS) {
                ALOGE("Too many sensors!\n");
-               return;
+               return -1;
        }
 
        sensor_type = sensor_catalog[catalog_index].type;
@@ -480,6 +509,7 @@ static void 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;
 
@@ -488,13 +518,24 @@ static void add_sensor (int dev_num, int catalog_index, int mode)
         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);
@@ -595,8 +636,11 @@ static void 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);
+       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
@@ -621,9 +665,6 @@ static void add_sensor (int dev_num, int catalog_index, int mode)
 
        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
@@ -667,6 +708,7 @@ static void add_sensor (int dev_num, int catalog_index, int mode)
        sensor[s].needs_enable = get_needs_enable(dev_num, sensor_catalog[catalog_index].tag);
 
        sensor_count++;
+       return 0;
 }
 
 static void virtual_sensors_check (void)
@@ -759,6 +801,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.
@@ -767,7 +814,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
@@ -811,9 +858,35 @@ 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;
                }
 }
 
+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];
+
+       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;
+       return 0;
+}
 
 static void setup_trigger_names (void)
 {
@@ -822,6 +895,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++)
@@ -831,7 +905,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));
 
@@ -839,7 +913,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++;
+               }
        }
 
        /*
@@ -856,9 +939,42 @@ 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;
+       }
+}
+
+
 void enumerate_sensors (void)
 {
        /*
@@ -880,20 +996,23 @@ 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++) {
-                       if (event_sensors[i]) {
-                               add_sensor(dev_num, i, MODE_EVENT);
+                       /* 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)