OSDN Git Service

Merge branch ab9bfb48e9c9aa0d4d0e182a8b7f78f8cae27e42 into android/master
[android-x86/hardware-intel-libsensors.git] / enumeration.c
index e8219cc..25b07e7 100644 (file)
@@ -17,6 +17,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
@@ -225,6 +227,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 */
@@ -489,6 +494,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");
@@ -509,6 +515,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;
 
@@ -564,6 +571,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);
@@ -813,7 +833,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], int* updated)
+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
@@ -858,10 +878,11 @@ static void update_sensor_matching_trigger_name (char name[MAX_NAME_SIZE], int*
                                /* 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)
+static int create_hrtimer_trigger(int s, int trigger)
 {
        struct stat dir_status;
        char buf[MAX_NAME_SIZE];
@@ -882,6 +903,7 @@ static int create_hrtimer_trigger(int s)
                        return -1;
 
        strncpy (sensor[s].hrtimer_trigger_name, hrtimer_name, MAX_NAME_SIZE);
+       sensor[s].trigger_nr = trigger;
        return 0;
 }
 
@@ -902,7 +924,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));
 
@@ -910,14 +932,16 @@ static void setup_trigger_names (void)
                        break;
 
                /* Record initial and any-motion triggers names */
-               update_sensor_matching_trigger_name(buf, updated);
+               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; s++) {
-               if ((sensor[s].quirks & QUIRK_HRTIMER) && !updated[s])
-                       create_hrtimer_trigger(s);
+       for (s=0; s<sensor_count && trigger<MAX_TRIGGERS; s++) {
+               if ((sensor[s].quirks & QUIRK_HRTIMER) && !updated[s]) {
+                       create_hrtimer_trigger(s, trigger);
+                       trigger++;
+               }
        }
 
        /*
@@ -970,6 +994,53 @@ static void post_process_sensor_list (char poll_map[catalog_size], char trig_map
 }
 
 
+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)
 {
        /*
@@ -983,6 +1054,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;
@@ -1014,12 +1086,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);
+       }
 }