OSDN Git Service

Winter cleanup: remove superfluous volatile specifier
[android-x86/hardware-intel-libsensors.git] / enumeration.c
index 80cdb51..406c9ee 100644 (file)
@@ -14,7 +14,6 @@
 #include "description.h"
 #include "control.h"
 #include "calibration.h"
-#include "filtering.h"
 
 /*
  * This table maps syfs entries in scan_elements directories to sensor types,
@@ -40,7 +39,7 @@ struct sensor_catalog_entry_t sensor_catalog[] = {
                                         "quat_x", "quat_y", "quat_z", "quat_w")
        DECLARE_SENSOR0("temp",       SENSOR_TYPE_AMBIENT_TEMPERATURE          )
        DECLARE_SENSOR0("proximity",  SENSOR_TYPE_PROXIMITY                    )
-       DECLARE_SENSOR3("anglvel",      SENSOR_TYPE_GYROSCOPE_UNCALIBRATED, "x", "y", "z")
+       DECLARE_VIRTUAL(SENSOR_TYPE_GYROSCOPE_UNCALIBRATED)
 };
 
 #define CATALOG_SIZE   ARRAY_SIZE(sensor_catalog)
@@ -53,10 +52,9 @@ struct sensor_catalog_entry_t sensor_catalog[] = {
 /* We equate sensor handles to indices in these tables */
 
 struct sensor_t      sensor_desc[MAX_SENSORS]; /* Android-level descriptors */
-struct sensor_info_t sensor_info[MAX_SENSORS]; /* Internal descriptors      */
+struct sensor_info_t sensor[MAX_SENSORS];      /* Internal descriptors      */
 int sensor_count;                              /* Detected sensors          */
 
-
 static void setup_properties_from_pld(int s, int panel, int rotation,
                                      int num_channels)
 {
@@ -101,15 +99,15 @@ static void setup_properties_from_pld(int s, int panel, int rotation,
        }
 
        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[s].order[0] = 1;
+               sensor[s].order[1] = 0;
+               sensor[s].order[2] = 2;
+               sensor[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;
+       sensor[s].channel[0].opt_scale = x;
+       sensor[s].channel[1].opt_scale = y;
+       sensor[s].channel[2].opt_scale = z;
 }
 
 
@@ -199,6 +197,66 @@ static void decode_placement_information (int dev_num, int num_channels, int s)
        setup_properties_from_pld(s, panel, rotation, num_channels);
 }
 
+static void populate_descriptors(int s, int sensor_type)
+{
+       /* Initialize Android-visible descriptor */
+       sensor_desc[s].name             = sensor_get_name(s);
+       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].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(%d) maxD(%d) flags(%2.2x)\n",
+               s, sensor[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;
+}
+
+static void add_virtual_sensor (int catalog_index)
+{
+       int s;
+       int sensor_type;
+
+       if (sensor_count == MAX_SENSORS) {
+               ALOGE("Too many sensors!\n");
+               return;
+       }
+
+       sensor_type = sensor_catalog[catalog_index].type;
+
+       s = sensor_count;
+
+       sensor[s].is_virtual = 1;
+       sensor[s].catalog_index = catalog_index;
+       sensor[s].type          = sensor_type;
+
+       populate_descriptors(s, sensor_type);
+
+       /* Initialize fields related to sysfs reads offloading */
+       sensor[s].thread_data_fd[0]  = -1;
+       sensor[s].thread_data_fd[1]  = -1;
+       sensor[s].acquisition_thread = -1;
+
+       sensor_count++;
+}
 
 static void add_sensor (int dev_num, int catalog_index, int use_polling)
 {
@@ -229,16 +287,16 @@ static void add_sensor (int dev_num, int catalog_index, int use_polling)
 
        s = sensor_count;
 
-       sensor_info[s].dev_num          = dev_num;
-       sensor_info[s].catalog_index    = catalog_index;
-       sensor_info[s].type             = sensor_type;
+       sensor[s].dev_num               = dev_num;
+       sensor[s].catalog_index = catalog_index;
+       sensor[s].type          = sensor_type;
 
         num_channels = sensor_catalog[catalog_index].num_channels;
 
         if (use_polling)
-                sensor_info[s].num_channels = 0;
+                sensor[s].num_channels = 0;
         else
-                sensor_info[s].num_channels = num_channels;
+                sensor[s].num_channels = num_channels;
 
        prefix = sensor_catalog[catalog_index].tag;
 
@@ -256,12 +314,12 @@ static void add_sensor (int dev_num, int catalog_index, int use_polling)
 
        /* 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);
+       sysfs_read_str(sysfs_path, sensor[s].internal_name, MAX_NAME_SIZE);
 
        /* See if we have general offsets and scale values for this sensor */
 
        sprintf(sysfs_path, SENSOR_OFFSET_PATH, dev_num, prefix);
-       sysfs_read_float(sysfs_path, &sensor_info[s].offset);
+       sysfs_read_float(sysfs_path, &sensor[s].offset);
 
        sprintf(sysfs_path, SENSOR_SCALE_PATH, dev_num, prefix);
        if (!sensor_get_fl_prop(s, "scale", &scale)) {
@@ -284,11 +342,11 @@ static void add_sensor (int dev_num, int catalog_index, int use_polling)
 
        sprintf(sysfs_path, SENSOR_SCALE_PATH, dev_num, prefix);
        if (!sysfs_read_float(sysfs_path, &scale)) {
-                sensor_info[s].scale = scale;
+                sensor[s].scale = scale;
                ALOGI("Scale path:%s scale:%f dev_num:%d\n",
                                         sysfs_path, scale, dev_num);
        } else {
-                sensor_info[s].scale = 1;
+                sensor[s].scale = 1;
 
                 /* Read channel specific scale if any*/
                 for (c = 0; c < num_channels; c++)
@@ -297,8 +355,8 @@ static void add_sensor (int dev_num, int catalog_index, int use_polling)
                            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;
+                                sensor[s].channel[c].scale = scale;
+                               sensor[s].scale = 0;
 
                                ALOGI(  "Scale path:%s "
                                        "channel scale:%f dev_num:%d\n",
@@ -309,10 +367,10 @@ static void add_sensor (int dev_num, int catalog_index, int use_polling)
 
         /* Set default scaling - if num_channels is zero, we have one channel */
 
-       sensor_info[s].channel[0].opt_scale = 1;
+       sensor[s].channel[0].opt_scale = 1;
 
        for (c = 1; c < num_channels; c++)
-               sensor_info[s].channel[c].opt_scale = 1;
+               sensor[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);
@@ -331,73 +389,48 @@ static void add_sensor (int dev_num, int catalog_index, int use_polling)
                        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;
+                               sensor[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);
-       sensor_desc[s].version          = sensor_get_version(s);
-       sensor_desc[s].handle           = s;
-       sensor_desc[s].type             = sensor_type;
-       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);
+                       sensor[s].channel[0].opt_scale = opt_scale;
 
-       /* 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(%d) maxD(%d) 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);
+       populate_descriptors(s, sensor_type);
 
-       /* 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;
+       /* Populate the quirks array */
+       sensor_get_quirks(s);
 
-       if (sensor_info[s].internal_name[0] == '\0') {
+       if (sensor[s].internal_name[0] == '\0') {
                /*
                 * In case the kernel-mode driver doesn't expose a name for
                 * the iio device, use (null)-dev%d as the trigger name...
                 * This can be considered a kernel-mode iio driver bug.
                 */
                ALOGW("Using null trigger on sensor %d (dev %d)\n", s, dev_num);
-               strcpy(sensor_info[s].internal_name, "(null)");
+               strcpy(sensor[s].internal_name, "(null)");
        }
 
-       if (sensor_type == SENSOR_TYPE_GYROSCOPE ||
-               sensor_type == SENSOR_TYPE_GYROSCOPE_UNCALIBRATED) {
+       if (sensor_type == SENSOR_TYPE_GYROSCOPE) {
                struct gyro_cal* calibration_data = calloc(1, sizeof(struct gyro_cal));
-               sensor_info[s].cal_data = calibration_data;
-               denoise_median_init(s, 3, 7);
+               sensor[s].cal_data = calibration_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;
+               sensor[s].cal_data = calibration_data;
        }
-
+       sensor[s].max_cal_level = sensor_get_cal_steps(s);
        /* 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;
+       sensor[s].thread_data_fd[0]  = -1;
+       sensor[s].thread_data_fd[1]  = -1;
+       sensor[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;
+       if (sensor_get_order(s, sensor[s].order))
+               sensor[s].quirks |= QUIRK_FIELD_ORDERING;
 
        sensor_count++;
 }
@@ -427,9 +460,9 @@ static void discover_poll_sensors (int dev_num, char map[CATALOG_SIZE])
                        continue;
 
                /* If the name matches a catalog entry, flag it */
-               for (i = 0; i<CATALOG_SIZE; i++) {
-               /* This will be added separately later */
-               if (sensor_catalog[i].type == SENSOR_TYPE_GYROSCOPE_UNCALIBRATED)
+               for (i = 0; i < CATALOG_SIZE; i++) {
+               /* No discovery for virtual sensors */
+               if (sensor_catalog[i].is_virtual)
                        continue;
                for (c=0; c<sensor_catalog[i].num_channels; c++)
                        if (!strcmp(d->d_name,sensor_catalog[i].channel[c].raw_path) ||
@@ -469,7 +502,8 @@ 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; i<CATALOG_SIZE; i++) {
-                       if (sensor_catalog[i].type == SENSOR_TYPE_GYROSCOPE_UNCALIBRATED)
+                       /* No discovery for virtual sensors */
+                       if (sensor_catalog[i].is_virtual)
                                continue;
                        if (!strcmp(d->d_name,
                                        sensor_catalog[i].channel[0].en_path)) {
@@ -504,7 +538,7 @@ static void orientation_sensor_check(void)
        int catalog_size = CATALOG_SIZE;
 
        for (i=0; i<sensor_count; i++)
-               switch (sensor_info[i].type) {
+               switch (sensor[i].type) {
                        case SENSOR_TYPE_ACCELEROMETER:
                                has_acc = 1;
                                break;
@@ -549,7 +583,7 @@ static void propose_new_trigger (int s, char trigger_name[MAX_NAME_SIZE],
        /* If we found any-motion trigger, record it */
 
        if (!memcmp(suffix, "any-motion-", 11)) {
-               strcpy(sensor_info[s].motion_trigger_name, trigger_name);
+               strcpy(sensor[s].motion_trigger_name, trigger_name);
                return;
        }
 
@@ -558,7 +592,7 @@ static void propose_new_trigger (int s, char trigger_name[MAX_NAME_SIZE],
         * use this though, as we may not have any other indication of the name
         * of the trigger to use with this sensor.
         */
-       strcpy(sensor_info[s].init_trigger_name, trigger_name);
+       strcpy(sensor[s].init_trigger_name, trigger_name);
 }
 
 
@@ -602,12 +636,12 @@ static void update_sensor_matching_trigger_name (char name[MAX_NAME_SIZE])
 
        /* See if that matches a sensor */
        for (s=0; s<sensor_count; s++)
-               if (sensor_info[s].dev_num == dev_num) {
+               if (sensor[s].dev_num == dev_num) {
 
-                       sensor_name_len = strlen(sensor_info[s].internal_name);
+                       sensor_name_len = strlen(sensor[s].internal_name);
 
                        if (!strncmp(name,
-                                    sensor_info[s].internal_name,
+                                    sensor[s].internal_name,
                                     sensor_name_len))
                                /* Switch to new trigger if appropriate */
                                propose_new_trigger(s, name, sensor_name_len);
@@ -626,9 +660,9 @@ static void setup_trigger_names (void)
 
        /* By default, use the name-dev convention that most drivers use */
        for (s=0; s<sensor_count; s++)
-               snprintf(sensor_info[s].init_trigger_name,
+               snprintf(sensor[s].init_trigger_name,
                         MAX_NAME_SIZE, "%s-dev%d",
-                        sensor_info[s].internal_name, sensor_info[s].dev_num);
+                        sensor[s].internal_name, sensor[s].dev_num);
 
        /* Now have a look to /sys/bus/iio/devices/triggerX entries */
 
@@ -653,20 +687,20 @@ static void setup_trigger_names (void)
         */
 
        for (s=0; s<sensor_count; s++)
-               if ((sensor_info[s].quirks & QUIRK_TERSE_DRIVER) &&
-                   sensor_info[s].motion_trigger_name[0] == '\0')
-                       strcpy( sensor_info[s].motion_trigger_name,
-                               sensor_info[s].init_trigger_name);
+               if ((sensor[s].quirks & QUIRK_TERSE_DRIVER) &&
+                   sensor[s].motion_trigger_name[0] == '\0')
+                       strcpy( sensor[s].motion_trigger_name,
+                               sensor[s].init_trigger_name);
 
        for (s=0; s<sensor_count; s++)
-               if (sensor_info[s].num_channels) {
+               if (sensor[s].num_channels) {
                        ALOGI("Sensor %d (%s) default trigger: %s\n", s,
-                               sensor_info[s].friendly_name,
-                               sensor_info[s].init_trigger_name);
-                       if (sensor_info[s].motion_trigger_name[0])
+                               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_info[s].friendly_name,
-                               sensor_info[s].motion_trigger_name);
+                               s, sensor[s].friendly_name,
+                               sensor[s].motion_trigger_name);
                }
 }
 
@@ -674,62 +708,33 @@ static void uncalibrated_gyro_check (void)
 {
        unsigned int has_gyr = 0;
        unsigned int dev_num;
-       int i, c;
-       unsigned int is_poll_sensor;
-       char buf[MAX_NAME_SIZE];
+       int i;
 
        int cal_idx = 0;
        int uncal_idx = 0;
        int catalog_size = CATALOG_SIZE; /* Avoid GCC sign comparison warning */
 
+       if (sensor_count == MAX_SENSORS)
+               return;
        /* Checking to see if we have a gyroscope - we can only have uncal if we have the base sensor */
        for (i=0; i < sensor_count; i++)
-               if (sensor_info[i].type == SENSOR_TYPE_GYROSCOPE) {
+               if (sensor[i].type == SENSOR_TYPE_GYROSCOPE) {
                        has_gyr=1;
-                       dev_num = sensor_info[i].dev_num;
-                       is_poll_sensor = !sensor_info[i].num_channels;
                        cal_idx = i;
                        break;
                }
 
-       /*
-        * If we have a gyro we can add the uncalibrated sensor of the same type and
-        * on the same dev_num. We will save indexes for easy finding and also save the
-        * channel specific information.
-        */
-       if (has_gyr)
+       if (has_gyr) {
+               uncal_idx = sensor_count;
+               sensor[uncal_idx].base_count = 1;
+               sensor[uncal_idx].base_idx[0] = cal_idx;
+
                for (i=0; i<catalog_size; i++)
                        if (sensor_catalog[i].type == SENSOR_TYPE_GYROSCOPE_UNCALIBRATED) {
-                               add_sensor(dev_num, i, is_poll_sensor);
-
-                               uncal_idx = sensor_count - 1; /* Just added uncalibrated sensor */
-
-                               /* Similar to build_sensor_report_maps */
-                               for (c = 0; c < sensor_info[uncal_idx].num_channels; c++)
-                               {
-                                       memcpy( &(sensor_info[uncal_idx].channel[c].type_spec),
-                                               &(sensor_info[cal_idx].channel[c].type_spec),
-                                               sizeof(sensor_info[uncal_idx].channel[c].type_spec));
-                                       sensor_info[uncal_idx].channel[c].type_info = sensor_info[cal_idx].channel[c].type_info;
-                                       sensor_info[uncal_idx].channel[c].offset    = sensor_info[cal_idx].channel[c].offset;
-                                       sensor_info[uncal_idx].channel[c].size      = sensor_info[cal_idx].channel[c].size;
-                               }
-                               sensor_info[uncal_idx].pair_idx = cal_idx;
-                               sensor_info[cal_idx].pair_idx = uncal_idx;
-                               strncpy(sensor_info[uncal_idx].init_trigger_name,
-                                       sensor_info[cal_idx].init_trigger_name,
-                                       MAX_NAME_SIZE);
-                               strncpy(sensor_info[uncal_idx].motion_trigger_name,
-                                       sensor_info[cal_idx].motion_trigger_name,
-                                       MAX_NAME_SIZE);
-
-                               /* Add "Uncalibrated " prefix to sensor name */
-                               strcpy(buf, sensor_info[cal_idx].friendly_name);
-                               snprintf(sensor_info[uncal_idx].friendly_name,
-                                        MAX_NAME_SIZE,
-                                        "%s %s", "Uncalibrated", buf);
+                               add_virtual_sensor(i);
                                break;
                        }
+       }
 }
 
 void enumerate_sensors (void)
@@ -779,7 +784,8 @@ void enumerate_sensors (void)
         * Create the uncalibrated counterpart to the compensated gyroscope.
         * This is is a new sensor type in Android 4.4.
         */
-       uncalibrated_gyro_check();
+
+         uncalibrated_gyro_check();
 }
 
 
@@ -787,25 +793,23 @@ void delete_enumeration_data (void)
 {
        int i;
        for (i = 0; i < sensor_count; i++)
-       switch (sensor_info[i].type) {
+       switch (sensor[i].type) {
                case SENSOR_TYPE_MAGNETIC_FIELD:
-                       if (sensor_info[i].cal_data != NULL) {
-                               free(sensor_info[i].cal_data);
-                               sensor_info[i].cal_data = NULL;
-                               sensor_info[i].cal_level = 0;
+                       if (sensor[i].cal_data != NULL) {
+                               free(sensor[i].cal_data);
+                               sensor[i].cal_data = NULL;
+                               sensor[i].cal_level = 0;
                        }
                        break;
-               case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
+
                case SENSOR_TYPE_GYROSCOPE:
-                       if (sensor_info[i].cal_data != NULL) {
-                               free(sensor_info[i].cal_data);
-                               sensor_info[i].cal_data = NULL;
-                               sensor_info[i].cal_level = 0;
+                       if (sensor[i].cal_data != NULL) {
+                               free(sensor[i].cal_data);
+                               sensor[i].cal_data = NULL;
+                               sensor[i].cal_level = 0;
                        }
                        break;
-                       if (sensor_info[i].filter != NULL) {
-                               denoise_median_release(i);
-                       }
+
                default:
                        break;
        }
@@ -814,8 +818,8 @@ void delete_enumeration_data (void)
 }
 
 
-int get_sensors_list struct sensors_module_t* module,
-                       struct sensor_t const** list)
+int get_sensors_list (__attribute__((unused)) struct sensors_module_t* module,
+                     struct sensor_t const** list)
 {
        *list = sensor_desc;
        return sensor_count;