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 54467f2..359fa90 100644 (file)
@@ -1,11 +1,25 @@
 /*
- * Copyright (C) 2014 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"
@@ -15,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
   * 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_AMBIENT_TEMPERATURE          )
-       DECLARE_SENSOR0("proximity",  SENSOR_TYPE_PROXIMITY                    )
-       DECLARE_VIRTUAL(SENSOR_TYPE_GYROSCOPE_UNCALIBRATED                     )
+sensor_catalog_entry_t sensor_catalog[] = {
+       {
+               .tag            = "accel",
+               .shorthand      = "",
+               .type           = SENSOR_TYPE_ACCELEROMETER,
+               .num_channels   = 3,
+               .is_virtual     = 0,
+               .channel = {
+                       { DECLARE_NAMED_CHANNEL("accel", "x") },
+                       { DECLARE_NAMED_CHANNEL("accel", "y") },
+                       { DECLARE_NAMED_CHANNEL("accel", "z") },
+               },
+       },
+       {
+               .tag            = "anglvel",
+               .shorthand      = "",
+               .type           = SENSOR_TYPE_GYROSCOPE,
+               .num_channels   = 3,
+               .is_virtual     = 0,
+               .channel = {
+                       { DECLARE_NAMED_CHANNEL("anglvel", "x") },
+                       { DECLARE_NAMED_CHANNEL("anglvel", "y") },
+                       { DECLARE_NAMED_CHANNEL("anglvel", "z") },
+               },
+       },
+       {
+               .tag            = "magn",
+               .shorthand      = "",
+               .type           = SENSOR_TYPE_MAGNETIC_FIELD,
+               .num_channels   = 3,
+               .is_virtual     = 0,
+               .channel = {
+                       { DECLARE_NAMED_CHANNEL("magn", "x") },
+                       { DECLARE_NAMED_CHANNEL("magn", "y") },
+                       { DECLARE_NAMED_CHANNEL("magn", "z") },
+               },
+       },
+       {
+               .tag            = "intensity",
+               .shorthand      = "",
+               .type           = SENSOR_TYPE_INTERNAL_INTENSITY,
+               .num_channels   = 1,
+               .is_virtual     = 0,
+               .channel = {
+                       { DECLARE_NAMED_CHANNEL("intensity", "both") },
+               },
+       },
+       {
+               .tag            = "illuminance",
+               .shorthand      = "",
+               .type           = SENSOR_TYPE_INTERNAL_ILLUMINANCE,
+               .num_channels   = 1,
+               .is_virtual     = 0,
+               .channel = {
+                       { DECLARE_GENERIC_CHANNEL("illuminance") },
+               },
+       },
+       {
+               .tag            = "incli",
+               .shorthand      = "",
+               .type           = SENSOR_TYPE_ORIENTATION,
+               .num_channels   = 3,
+               .is_virtual     = 0,
+               .channel = {
+                       { DECLARE_NAMED_CHANNEL("incli", "x") },
+                       { DECLARE_NAMED_CHANNEL("incli", "y") },
+                       { DECLARE_NAMED_CHANNEL("incli", "z") },
+               },
+       },
+       {
+               .tag            = "rot",
+               .shorthand      = "",
+               .type           = SENSOR_TYPE_ROTATION_VECTOR,
+               .num_channels   = 4,
+               .is_virtual     = 0,
+               .channel = {
+                       { DECLARE_NAMED_CHANNEL("rot", "quat_x") },
+                       { DECLARE_NAMED_CHANNEL("rot", "quat_y") },
+                       { DECLARE_NAMED_CHANNEL("rot", "quat_z") },
+                       { DECLARE_NAMED_CHANNEL("rot", "quat_w") },
+               },
+       },
+       {
+               .tag            = "temp",
+               .shorthand      = "",
+               .type           = SENSOR_TYPE_AMBIENT_TEMPERATURE,
+               .num_channels   = 1,
+               .is_virtual     = 0,
+               .channel = {
+                       { DECLARE_GENERIC_CHANNEL("temp") },
+               },
+       },
+       {
+               .tag            = "proximity",
+               .shorthand      = "prox",
+               .type           = SENSOR_TYPE_PROXIMITY,
+               .num_channels   = 1,
+               .is_virtual     = 0,
+               .channel = {
+                       { DECLARE_GENERIC_CHANNEL("proximity") },
+               },
+       },
+       {
+               .tag            = "",
+               .shorthand      = "",
+               .type           = SENSOR_TYPE_GYROSCOPE_UNCALIBRATED,
+               .num_channels   = 0,
+               .is_virtual     = 1,
+               .channel = {
+                       { DECLARE_GENERIC_CHANNEL("") },
+               },
+
+       },
+       {
+               .tag            = "",
+               .shorthand      = "",
+               .type           = SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED,
+               .num_channels   = 0,
+               .is_virtual     = 1,
+               .channel = {
+                       { DECLARE_GENERIC_CHANNEL("") },
+               },
+       },
+       {
+               .tag            = "steps",
+               .shorthand      = "",
+               .type           = SENSOR_TYPE_STEP_COUNTER,
+               .num_channels   = 1,
+               .is_virtual     = 0,
+               .channel = {
+                       { DECLARE_GENERIC_CHANNEL("steps") },
+               },
+       },
+       {
+               .tag            = "steps",
+               .shorthand      = "",
+               .type           = SENSOR_TYPE_STEP_DETECTOR,
+               .num_channels   = 1,
+               .is_virtual     = 0,
+               .channel = {
+                       {
+                               DECLARE_VOID_CHANNEL("steps")
+                               .num_events = 1,
+                               .event = {
+                                       { DECLARE_NAMED_EVENT("steps", "change") },
+                               },
+                       },
+               },
+       },
+       {
+               .tag            = "proximity",
+               .shorthand      = "prox",
+               .type           = SENSOR_TYPE_PROXIMITY,
+               .num_channels   = 4,
+               .is_virtual     = 0,
+               .channel = {
+                       {
+                               DECLARE_VOID_CHANNEL("proximity0")
+                               .num_events = 1,
+                               .event = {
+                                       { DECLARE_EVENT("proximity0", "_", "", "", "thresh", "_", "either") },
+                               },
+                       },
+                       {
+                               DECLARE_VOID_CHANNEL("proximity1")
+                               .num_events = 1,
+                               .event = {
+                                       { DECLARE_EVENT("proximity1", "_", "", "", "thresh", "_", "either") },
+                               },
+                       },
+                       {
+                               DECLARE_VOID_CHANNEL("proximity2")
+                               .num_events = 1,
+                               .event = {
+                                       { DECLARE_EVENT("proximity2", "_", "", "", "thresh", "_", "either") },
+                               },
+                       },
+                       {
+                               DECLARE_VOID_CHANNEL("proximity3")
+                               .num_events = 1,
+                               .event = {
+                                       { DECLARE_EVENT("proximity3", "_", "", "", "thresh", "_", "either") },
+                               },
+                       },
+               },
+       },
 };
 
-#define CATALOG_SIZE   ARRAY_SIZE(sensor_catalog)
+unsigned int 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
 
+/* 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 */
-struct sensor_info_t sensor[MAX_SENSORS];      /* Internal descriptors      */
-int sensor_count;                              /* Detected sensors          */
+struct sensor_t        sensor_desc[MAX_SENSORS];       /* Android-level descriptors */
+sensor_info_t  sensor[MAX_SENSORS];            /* Internal descriptors      */
+int            sensor_count;                   /* Detected sensors          */
 
 
+/* if the sensor has an _en attribute, we need to enable it */
+int get_needs_enable(int dev_num, const char *tag)
+{
+       char sysfs_path[PATH_MAX];
+       int fd;
+
+       sprintf(sysfs_path, SENSOR_ENABLE_PATH, dev_num, tag);
+
+       fd = open(sysfs_path, O_RDWR);
+       if (fd == -1)
+               return 0;
+
+       close(fd);
+       return 1;
+}
+
 static void setup_properties_from_pld (int s, int panel, int rotation,
                                       int num_channels)
 {
@@ -199,14 +408,31 @@ 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;
+       max_delay_t     max_delay_us;
+
        /* 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].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);
@@ -220,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);
@@ -228,6 +454,12 @@ static void populate_descriptors (int s, int sensor_type)
        /* We currently do not implement batching */
        sensor_desc[s].fifoReservedEventCount = 0;
        sensor_desc[s].fifoMaxEventCount = 0;
+
+       min_delay_us = sensor_desc[s].minDelay;
+       max_delay_us = sensor_desc[s].maxDelay;
+
+       sensor[s].min_supported_rate = max_delay_us ? 1000000.0 / max_delay_us : 1;
+       sensor[s].max_supported_rate = min_delay_us && min_delay_us != -1 ? 1000000.0 / min_delay_us : 0;
 }
 
 
@@ -260,7 +492,7 @@ static void add_virtual_sensor (int catalog_index)
 }
 
 
-static void add_sensor (int dev_num, int catalog_index, int use_polling)
+static int add_sensor (int dev_num, int catalog_index, int mode)
 {
        int s;
        int sensor_type;
@@ -273,10 +505,12 @@ static void add_sensor (int dev_num, int catalog_index, int use_polling)
        const char* ch_name;
        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");
-               return;
+               return -1;
        }
 
        sensor_type = sensor_catalog[catalog_index].type;
@@ -289,24 +523,37 @@ static void add_sensor (int dev_num, int catalog_index, int use_polling)
 
        s = sensor_count;
 
-       sensor[s].dev_num               = dev_num;
+       sensor[s].dev_num       = dev_num;
        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;
 
-        if (use_polling)
+        if (mode == MODE_POLL)
                 sensor[s].num_channels = 0;
         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);
@@ -314,6 +561,41 @@ static void add_sensor (int dev_num, int catalog_index, int use_polling)
                 }
        }
 
+        /*
+         * See if we have optional calibration biases for each of the channels of this sensor. These would be expressed using properties like
+         * iio.accel.y.calib_bias = -1, or possibly something like iio.temp.calib_bias if the sensor has a single channel. This value gets stored in the
+         * relevant calibbias sysfs file if that file can be located and then used internally by the iio sensor driver.
+         */
+
+        if (num_channels) {
+               for (c = 0; c < num_channels; c++) {
+                       ch_name = sensor_catalog[catalog_index].channel[c].name;
+                       sprintf(suffix, "%s.calib_bias", ch_name);
+                       if (!sensor_get_prop(s, suffix, &calib_bias) && calib_bias) {
+                               sprintf(suffix, "%s_%s", prefix, sensor_catalog[catalog_index].channel[c].name);
+                               sprintf(sysfs_path, SENSOR_CALIB_BIAS_PATH, dev_num, suffix);
+                               sysfs_write_int(sysfs_path, calib_bias);
+                       }
+               }
+        } else
+               if (!sensor_get_prop(s, "calib_bias", &calib_bias) && calib_bias) {
+                               sprintf(sysfs_path, SENSOR_CALIB_BIAS_PATH, dev_num, prefix);
+                               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);
@@ -369,39 +651,53 @@ 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[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 */
+               sprintf(sysfs_path, BASE_PATH "%s", dev_num,
+                       sensor_catalog[catalog_index].channel[c].input_path);
+               sensor[s].channel[c].input_path_present = (access(sysfs_path, R_OK) != -1);
+               /* Check the presence of the channel's raw_path */
+               sprintf(sysfs_path, BASE_PATH "%s", dev_num,
+                       sensor_catalog[catalog_index].channel[c].raw_path);
+               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);
 
-        /*
-         * 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 (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);
 
-        if (num_channels) {
+       /*
+        * 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[s].channel[c].opt_scale = opt_scale;
+                               sensor[s].channel[c].opt_scale *= opt_scale;
                }
-        } else
+        } else {
                if (!sensor_get_fl_prop(s, "opt_scale", &opt_scale))
                        sensor[s].channel[0].opt_scale = opt_scale;
+        }
 
        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
@@ -413,12 +709,18 @@ static void add_sensor (int dev_num, int catalog_index, int use_polling)
        }
 
        switch (sensor_type) {
-               case SENSOR_TYPE_GYROSCOPE:
-                       sensor[s].cal_data = malloc(sizeof(struct gyro_cal_t));
+               case SENSOR_TYPE_ACCELEROMETER:
+                       /* Only engage accelerometer bias compensation if really needed */
+                       if (sensor_get_quirks(s) & QUIRK_BIASED)
+                               sensor[s].cal_data = calloc(1, sizeof(accel_cal_t));
+                       break;
+
+                       case SENSOR_TYPE_GYROSCOPE:
+                       sensor[s].cal_data = malloc(sizeof(gyro_cal_t));
                        break;
 
                case SENSOR_TYPE_MAGNETIC_FIELD:
-                       sensor[s].cal_data = malloc(sizeof(struct compass_cal_t));
+                       sensor[s].cal_data = malloc(sizeof(compass_cal_t));
                        break;
        }
 
@@ -436,110 +738,23 @@ static void add_sensor (int dev_num, int catalog_index, int use_polling)
        if (sensor_get_order(s, sensor[s].order))
                sensor[s].quirks |= QUIRK_FIELD_ORDERING;
 
-       sensor_count++;
-}
-
-
-static void discover_poll_sensors (int dev_num, char map[CATALOG_SIZE])
-{
-       char base_dir[PATH_MAX];
-       DIR *dir;
-       struct dirent *d;
-       unsigned int i;
-        int c;
-
-       memset(map, 0, CATALOG_SIZE);
-
-       snprintf(base_dir, sizeof(base_dir), BASE_PATH, dev_num);
-
-       dir = opendir(base_dir);
-       if (!dir) {
-               return;
-       }
-
-       /* Enumerate entries in this iio device's base folder */
-
-       while ((d = readdir(dir))) {
-               if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
-                       continue;
-
-               /* If the name matches a catalog entry, flag it */
-               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) ||
-                               !strcmp(d->d_name, sensor_catalog[i].channel[c].input_path)) {
-                                       map[i] = 1;
-                                       break;
-                       }
-               }
-       }
-
-       closedir(dir);
-}
-
-
-static void discover_trig_sensors (int dev_num, char map[CATALOG_SIZE])
-{
-       char scan_elem_dir[PATH_MAX];
-       DIR *dir;
-       struct dirent *d;
-       unsigned int i;
-
-       memset(map, 0, CATALOG_SIZE);
+       sensor[s].needs_enable = get_needs_enable(dev_num, sensor_catalog[catalog_index].tag);
 
-       /* Enumerate entries in this iio device's scan_elements folder */
-
-       snprintf(scan_elem_dir, sizeof(scan_elem_dir), CHANNEL_PATH, dev_num);
-
-       dir = opendir(scan_elem_dir);
-       if (!dir) {
-               return;
-       }
-
-       while ((d = readdir(dir))) {
-               if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
-                       continue;
-
-               /* Compare en entry to known ones and create matching sensors */
-
-               for (i = 0; i<CATALOG_SIZE; i++) {
-                       /* No discovery for virtual sensors */
-                       if (sensor_catalog[i].is_virtual)
-                               continue;
-                       if (!strcmp(d->d_name,
-                                       sensor_catalog[i].channel[0].en_path)) {
-                                       map[i] = 1;
-                                       break;
-                       }
-               }
-       }
-
-       closedir(dir);
+       sensor_count++;
+       return 0;
 }
 
-
-static void orientation_sensor_check (void)
+static void virtual_sensors_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;
+       int gyro_cal_idx = 0;
+       int magn_cal_idx = 0;
+       unsigned int j;
 
        for (i=0; i<sensor_count; i++)
                switch (sensor[i].type) {
@@ -548,9 +763,11 @@ static void orientation_sensor_check (void)
                                break;
                        case SENSOR_TYPE_GYROSCOPE:
                                has_gyr = 1;
+                               gyro_cal_idx = i;
                                break;
                        case SENSOR_TYPE_MAGNETIC_FIELD:
                                has_mag = 1;
+                               magn_cal_idx = i;
                                break;
                        case SENSOR_TYPE_ORIENTATION:
                                has_ori = 1;
@@ -560,13 +777,39 @@ static void orientation_sensor_check (void)
                                break;
                }
 
-       if (has_acc && has_gyr && has_mag && !has_rot && !has_ori)
-               for (i=0; i<catalog_size; i++)
-                       if (sensor_catalog[i].type == SENSOR_TYPE_ORIENTATION) {
-                               ALOGI("Adding placeholder orientation sensor");
-                               add_sensor(0, i, 1);
+       for (j=0; j<catalog_size; j++)
+               switch (sensor_catalog[j].type) {
+                       /*
+                        * 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.
+                        */
+
+                       case SENSOR_TYPE_ORIENTATION:
+                               if (has_acc && has_gyr && has_mag && !has_rot && !has_ori)
+                                       add_sensor(0, j, MODE_POLL);
                                break;
-                       }
+                       case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
+                               if (has_gyr) {
+                                       sensor[sensor_count].base_count = 1;
+                                       sensor[sensor_count].base[0] = gyro_cal_idx;
+                                       add_virtual_sensor(j);
+                               }
+                               break;
+                       case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED:
+                               if (has_mag) {
+                                       sensor[sensor_count].base_count = 1;
+                                       sensor[sensor_count].base[0] = magn_cal_idx;
+                                       add_virtual_sensor(j);
+                               }
+                               break;
+                       default:
+                               break;
+               }
 }
 
 
@@ -574,9 +817,8 @@ static void propose_new_trigger (int s, char trigger_name[MAX_NAME_SIZE],
                                 int sensor_name_len)
 {
        /*
-        * A new trigger has been enumerated for this sensor. Check if it makes
-        * sense to use it over the currently selected one, and select it if it
-        * is so. The format is something like sensor_name-dev0.
+        * A new trigger has been enumerated for this sensor. Check if it makes sense to use it over the currently selected one,
+        *  and select it if it is so. The format is something like sensor_name-dev0.
         */
 
        const char *suffix = trigger_name + sensor_name_len + 1;
@@ -592,22 +834,24 @@ 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
+        * 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.
         */
        strcpy(sensor[s].init_trigger_name, trigger_name);
 }
 
 
-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
-        * equal to the iio device number the sensor is associated to. If so,
-        * update the string we're going to write to trigger/current_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
+        * equal to the iio device number the sensor is associated to. If so, update the string we're going to write to trigger/current_trigger
         * when enabling this sensor.
         */
 
@@ -618,8 +862,7 @@ static void update_sensor_matching_trigger_name (char name[MAX_NAME_SIZE])
        int sensor_name_len;
 
        /*
-        * First determine the iio device number this trigger refers to. We
-        * expect the last few characters (typically one) of the trigger name
+        * First determine the iio device number this trigger refers to. We expect the last few characters (typically one) of the trigger name
         * to be this number, so perform a few checks.
         */
        len = strnlen(name, MAX_NAME_SIZE);
@@ -645,35 +888,82 @@ static void update_sensor_matching_trigger_name (char name[MAX_NAME_SIZE])
 
                        sensor_name_len = strlen(sensor[s].internal_name);
 
-                       if (!strncmp(name,
-                                    sensor[s].internal_name,
-                                    sensor_name_len))
+                       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)
 {
        char filename[PATH_MAX];
        char buf[MAX_NAME_SIZE];
-       int len;
        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++)
-               snprintf(sensor[s].init_trigger_name,
-                        MAX_NAME_SIZE, "%s-dev%d",
-                        sensor[s].internal_name, sensor[s].dev_num);
+               snprintf(sensor[s].init_trigger_name, MAX_NAME_SIZE, "%s-dev%d", sensor[s].internal_name, sensor[s].dev_num);
 
        /* Now have a look to /sys/bus/iio/devices/triggerX entries */
 
        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));
 
@@ -681,118 +971,175 @@ 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++;
+               }
        }
 
        /*
-        * Certain drivers expose only motion triggers even though they should
-        * be continous. For these, use the default trigger name as the motion
-        * trigger. The code generating intermediate events is dependent on
-        * motion_trigger_name being set to a non empty string.
+        * Certain drivers expose only motion triggers even though they should be continous. For these, use the default trigger name as the motion
+        * trigger. The code generating intermediate events is dependent on motion_trigger_name being set to a non empty string.
         */
 
        for (s=0; s<sensor_count; s++)
-               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);
+               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[s].num_channels) {
-                       ALOGI("Sensor %d (%s) default trigger: %s\n", s,
-                               sensor[s].friendly_name,
-                               sensor[s].init_trigger_name);
+               if (sensor[s].mode == MODE_TRIGGER) {
+                       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);
+                               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 void uncalibrated_gyro_check (void)
+static int catalog_index_from_sensor_type (int type)
 {
-       unsigned int has_gyr = 0;
-       unsigned int dev_num;
-       int i;
+       /* Return first matching catalog entry index for selected type */
+       unsigned int i;
 
-       int cal_idx = 0;
-       int uncal_idx = 0;
-       int catalog_size = CATALOG_SIZE; /* Avoid GCC sign comparison warning */
+       for (i=0; i<catalog_size; i++)
+               if (sensor_catalog[i].type == type)
+                       return i;
 
-       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[i].type == SENSOR_TYPE_GYROSCOPE) {
-                       has_gyr=1;
-                       cal_idx = i;
-                       break;
-               }
+       return -1;
+}
 
-       if (has_gyr) {
-               uncal_idx = sensor_count;
-               sensor[uncal_idx].base_count = 1;
-               sensor[uncal_idx].base[0] = cal_idx;
 
-               for (i=0; i<catalog_size; i++)
-                       if (sensor_catalog[i].type == SENSOR_TYPE_GYROSCOPE_UNCALIBRATED) {
-                               add_virtual_sensor(i);
-                               break;
-                       }
+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)
 {
        /*
-        * Discover supported sensors and allocate control structures for them.
-        * Multiple sensors can potentially rely on a single iio device (each
-        * using their own channels). We can't have multiple sensors of the same
-        * type on the same device. In case of detection as both a poll-mode
+        * Discover supported sensors and allocate control structures for them. Multiple sensors can potentially rely on a single iio device (each
+        * using their own channels). We can't have multiple sensors of the same type on the same device. In case of detection as both a poll-mode
         * and trigger-based sensor, use the trigger usage mode.
         */
-       char poll_sensors[CATALOG_SIZE];
-       char trig_sensors[CATALOG_SIZE];
+       char poll_sensors[catalog_size];
+       char trig_sensors[catalog_size];
+       char event_sensors[catalog_size];
        int dev_num;
        unsigned int i;
        int trig_found;
+       int s;
 
        for (dev_num=0; dev_num<MAX_DEVICES; dev_num++) {
                trig_found = 0;
 
-               discover_poll_sensors(dev_num, poll_sensors);
-               discover_trig_sensors(dev_num, trig_sensors);
+               discover_sensors(dev_num, BASE_PATH, poll_sensors, check_poll_sensors);
+               discover_sensors(dev_num, CHANNEL_PATH, trig_sensors, check_trig_sensors);
+               discover_sensors(dev_num, EVENTS_PATH, event_sensors, check_event_sensors);
 
-               for (i=0; i<CATALOG_SIZE; i++)
-                       if (trig_sensors[i]) {
-                               add_sensor(dev_num, i, 0);
+               /* 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))
+                               continue;
+
+                       /* Then trigger */
+                       if (trig_sensors[i] && !add_sensor(dev_num, i, MODE_TRIGGER)) {
                                trig_found = 1;
+                               continue;
                        }
-                       else
-                               if (poll_sensors[i])
-                                       add_sensor(dev_num, i, 1);
 
-               if (trig_found) {
-                       build_sensor_report_maps(dev_num);
+                       /* Try polling otherwise */
+                       if (poll_sensors[i])
+                               add_sensor(dev_num, i, MODE_POLL);
                }
+
+               if (trig_found)
+                       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();
 
-       /* Make sure Android fall backs to its own orientation sensor */
-       orientation_sensor_check();
+       ALOGI("Discovered %d sensors\n", sensor_count);
 
-       /*
-        * Create the uncalibrated counterpart to the compensated gyroscope.
-        * This is is a new sensor type in Android 4.4.
-        */
+       virtual_sensors_check();
 
-         uncalibrated_gyro_check();
+       for (s=0; s<sensor_count; s++) {
+               ALOGI("S%d: %s\n", s, sensor[s].friendly_name);
+       }
 }