OSDN Git Service

Min/Max delay for Light and Temperature
[android-x86/hardware-intel-libsensors.git] / control.c
index 71aefca..7d942fd 100644 (file)
--- a/control.c
+++ b/control.c
@@ -2,7 +2,11 @@
  * Copyright (C) 2014 Intel Corporation.
  */
 
+#include <stdlib.h>
+#include <ctype.h>
 #include <fcntl.h>
+#include <pthread.h>
+#include <time.h>
 #include <sys/epoll.h>
 #include <sys/socket.h>
 #include <utils/Log.h>
@@ -11,6 +15,8 @@
 #include "enumeration.h"
 #include "utils.h"
 #include "transform.h"
+#include "calibration.h"
+#include "description.h"
 
 /* Currently active sensors count, per device */
 static int poll_sensors_per_dev[MAX_DEVICES];  /* poll-mode sensors */
@@ -20,41 +26,77 @@ static int device_fd[MAX_DEVICES];   /* fd on the /dev/iio:deviceX file */
 
 static int poll_fd; /* epoll instance covering all enabled sensors */
 
-static int poll_socket_pair[2];        /* used to unblock the poll loop */
-
-/* Timestamp for the moment when we last exited a poll operation */
-static int64_t last_poll_exit_ts;
-
 static int active_poll_sensors; /* Number of enabled poll-mode sensors */
 
-/* Cap the time between poll operations to this, to counter runaway polls */
-#define POLL_MIN_INTERVAL 10000 /* uS */
-
-#define INVALID_DEV_NUM ((uint32_t) -1)
+/* We use pthread condition variables to get worker threads out of sleep */
+static pthread_cond_t  thread_release_cond     [MAX_SENSORS];
+static pthread_mutex_t thread_release_mutex    [MAX_SENSORS];
 
+/*
+ * We associate tags to each of our poll set entries. These tags have the
+ * following values:
+ * - a iio device number if the fd is a iio character device fd
+ * - THREAD_REPORT_TAG_BASE + sensor handle if the fd is the receiving end of a
+ *   pipe used by a sysfs data acquisition thread
+ *  */
+#define THREAD_REPORT_TAG_BASE 0x00010000
+
+/* When polling try to compensate for the iio overhead in
+ * order to try to get a frequency closer to the advertised one
+ */
+#define OVERHEAD_THRESHOLD 0.97
+#define ENABLE_BUFFER_RETRIES 10
+#define ENABLE_BUFFER_RETRY_DELAY_MS 10
 
 static int enable_buffer(int dev_num, int enabled)
 {
        char sysfs_path[PATH_MAX];
+       int ret, retries, millisec;
+       struct timespec req = {0};
+
+       retries = ENABLE_BUFFER_RETRIES;
+       millisec = ENABLE_BUFFER_RETRY_DELAY_MS;
+       req.tv_sec = 0;
+       req.tv_nsec = millisec * 1000000L;
 
        sprintf(sysfs_path, ENABLE_PATH, dev_num);
 
-       /* Low level, non-multiplexed, enable/disable routine */
-       return sysfs_write_int(sysfs_path, enabled);
+       while (retries--) {
+               /* Low level, non-multiplexed, enable/disable routine */
+               ret = sysfs_write_int(sysfs_path, enabled);
+               if (ret > 0)
+                       break;
+
+               ALOGE("Failed enabling buffer, retrying");
+               nanosleep(&req, (struct timespec *)NULL);
+       }
+
+       if (ret < 0) {
+               ALOGE("Could not enable buffer\n");
+               return -EIO;
+       }
+
+       return 0;
 }
 
 
-static int setup_trigger(int dev_num, const char* trigger_val)
+static void setup_trigger (int s, const char* trigger_val)
 {
        char sysfs_path[PATH_MAX];
 
-       sprintf(sysfs_path, TRIGGER_PATH, dev_num);
+       sprintf(sysfs_path, TRIGGER_PATH, sensor_info[s].dev_num);
+
+       if (trigger_val[0] != '\n')
+               ALOGI("Setting S%d (%s) trigger to %s\n", s,
+                       sensor_info[s].friendly_name, trigger_val);
+
+       sysfs_write_str(sysfs_path, trigger_val);
 
-       return sysfs_write_str(sysfs_path, trigger_val);
+       sensor_info[s].selected_trigger = trigger_val;
 }
 
 
-static void refresh_sensor_report_maps(int dev_num)
+void build_sensor_report_maps(int dev_num)
 {
        /*
         * Read sysfs files from a iio device's scan_element directory, and
@@ -74,21 +116,19 @@ static void refresh_sensor_report_maps(int dev_num)
        int c;
        int n;
        int i;
-       int ch_enabled;
        int ch_index;
        char* ch_spec;
        char spec_buf[MAX_TYPE_SPEC_LEN];
        struct datum_info_t* ch_info;
        int size;
        char sysfs_path[PATH_MAX];
-       int active_channels;
+       int known_channels;
        int offset;
-       int channel_count;
        int channel_size_from_index[MAX_SENSORS * MAX_CHANNELS] = { 0 };
        int sensor_handle_from_index[MAX_SENSORS * MAX_CHANNELS] = { 0 };
        int channel_number_from_index[MAX_SENSORS * MAX_CHANNELS] = { 0 };
 
-       active_channels = 0;
+       known_channels = 0;
 
        /* For each sensor that is linked to this device */
        for (s=0; s<sensor_count; s++) {
@@ -97,26 +137,9 @@ static void refresh_sensor_report_maps(int dev_num)
 
                i = sensor_info[s].catalog_index;
 
-               /* Read channel status through syfs attributes */
+               /* Read channel details through sysfs attributes */
                for (c=0; c<sensor_info[s].num_channels; c++) {
 
-                       /* Read _en file */
-                       sprintf(sysfs_path, CHANNEL_PATH "%s",
-                               sensor_info[s].dev_num,
-                               sensor_catalog[i].channel[c].en_path);
-
-                       n = sysfs_read_int(sysfs_path, &ch_enabled);
-
-                       if (n == -1) {
-                               ALOGW(  "Failed to read _en flag: %s\n",
-                               sysfs_path);
-                               continue;
-                       }
-
-                       if (!ch_enabled != 1) {
-                               sensor_info[s].channel[c].size = 0;
-                       }
-
                        /* Read _type file */
                        sprintf(sysfs_path, CHANNEL_PATH "%s",
                                sensor_info[s].dev_num,
@@ -163,15 +186,26 @@ static void refresh_sensor_report_maps(int dev_num)
                        channel_number_from_index[ch_index] = c;
                        channel_size_from_index  [ch_index] = size;
 
-                       active_channels++;
+                       known_channels++;
+               }
+
+               /* Stop sampling - if we are recovering from hal restart */
+                enable_buffer(dev_num, 0);
+                setup_trigger(s, "\n");
+
+               /* Turn on channels we're aware of */
+               for (c=0;c<sensor_info[s].num_channels; c++) {
+                       sprintf(sysfs_path, CHANNEL_PATH "%s",
+                               sensor_info[s].dev_num,
+                               sensor_catalog[i].channel[c].en_path);
+                       sysfs_write_int(sysfs_path, 1);
                }
        }
 
-       ALOGI("Found %d enabled channels for iio device %d\n", active_channels,
-               dev_num);
+       ALOGI("Found %d channels on iio device %d\n", known_channels, dev_num);
 
        /*
-        * Now that we know which channels are enabled, their sizes and their
+        * Now that we know which channels are defined, their sizes and their
         * ordering, update channels offsets within device report. Note: there
         * is a possibility that several sensors share the same index, with
         * their data fields being isolated by masking and shifting as specified
@@ -210,6 +244,8 @@ int adjust_counters (int s, int enabled)
         */
 
        int dev_num = sensor_info[s].dev_num;
+       int catalog_index = sensor_info[s].catalog_index;
+       int sensor_type = sensor_catalog[catalog_index].type;
 
        /* Refcount per sensor, in terms of enable count */
        if (enabled) {
@@ -218,8 +254,19 @@ int adjust_counters (int s, int enabled)
 
                sensor_info[s].enable_count++;
 
-               if (sensor_info[s].enable_count != 1)
+               if (sensor_info[s].enable_count > 1)
                        return 0; /* The sensor was, and remains, in use */
+
+               switch (sensor_type) {
+                       case SENSOR_TYPE_MAGNETIC_FIELD:
+                               compass_read_data(&sensor_info[s]);
+                               break;
+
+                       case SENSOR_TYPE_GYROSCOPE:
+                       case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
+                               gyro_cal_init(&sensor_info[s]);
+                               break;
+               }
        } else {
                if (sensor_info[s].enable_count == 0)
                        return -1; /* Spurious disable call */
@@ -232,12 +279,19 @@ int adjust_counters (int s, int enabled)
                if (sensor_info[s].enable_count > 0)
                        return 0; /* The sensor was, and remains, in use */
 
-               /* Sensor disabled, clear up pending data */
-
+               /* Sensor disabled, lower report available flag */
                sensor_info[s].report_pending = 0;
-               memset(sensor_info[s].report_buffer, 0, MAX_SENSOR_REPORT_SIZE);
+
+               if (sensor_type == SENSOR_TYPE_MAGNETIC_FIELD)
+                       compass_store_data(&sensor_info[s]);
        }
 
+
+       /* If uncalibrated type and pair is already active don't adjust counters */
+       if (sensor_type == SENSOR_TYPE_GYROSCOPE_UNCALIBRATED &&
+               sensor_info[sensor_info[s].pair_idx].enable_count != 0)
+                       return 0;
+
        /* We changed the state of a sensor - adjust per iio device counters */
 
        /* If this is a regular event-driven sensor */
@@ -263,75 +317,275 @@ int adjust_counters (int s, int enabled)
 }
 
 
+static int get_field_count (int s)
+{
+       int catalog_index = sensor_info[s].catalog_index;
+       int sensor_type   = sensor_catalog[catalog_index].type;
+
+       switch (sensor_type) {
+               case SENSOR_TYPE_ACCELEROMETER:         /* m/s^2        */
+               case SENSOR_TYPE_MAGNETIC_FIELD:        /* micro-tesla  */
+               case SENSOR_TYPE_ORIENTATION:           /* degrees      */
+               case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
+               case SENSOR_TYPE_GYROSCOPE:             /* radians/s    */
+                       return 3;
+
+               case SENSOR_TYPE_LIGHT:                 /* SI lux units */
+               case SENSOR_TYPE_AMBIENT_TEMPERATURE:   /* °C          */
+               case SENSOR_TYPE_TEMPERATURE:           /* °C          */
+               case SENSOR_TYPE_PROXIMITY:             /* centimeters  */
+               case SENSOR_TYPE_PRESSURE:              /* hecto-pascal */
+               case SENSOR_TYPE_RELATIVE_HUMIDITY:     /* percent */
+                       return 1;
+
+               case SENSOR_TYPE_ROTATION_VECTOR:
+                       return  4;
+
+               default:
+                       ALOGE("Unknown sensor type!\n");
+                       return 0;                       /* Drop sample */
+       }
+}
+
+
+static void time_add(struct timespec *out, struct timespec *in, int64_t ns)
+{
+       int64_t target_ts = 1000000000LL * in->tv_sec + in->tv_nsec + ns;
+
+       out->tv_sec = target_ts / 1000000000;
+       out->tv_nsec = target_ts % 1000000000;
+}
+
+
+static void* acquisition_routine (void* param)
+{
+       /*
+        * Data acquisition routine run in a dedicated thread, covering a single
+        * sensor. This loop will periodically retrieve sampling data through
+        * sysfs, then package it as a sample and transfer it to our master poll
+        * loop through a report fd. Checks for a cancellation signal quite
+        * frequently, as the thread may be disposed of at any time. Note that
+        * Bionic does not provide pthread_cancel / pthread_testcancel...
+        */
+
+       int s = (int) (size_t) param;
+       int num_fields;
+       struct sensors_event_t data = {0};
+       int c;
+       int ret;
+       struct timespec entry_time;
+       struct timespec target_time;
+       int64_t period;
+
+       ALOGI("Entering data acquisition thread S%d (%s): rate(%f), minDelay(%ld), maxDelay(%ld)\n",
+               s, sensor_info[s].friendly_name, sensor_info[s].sampling_rate,
+               sensor_desc[s].minDelay, sensor_desc[s].maxDelay);
+
+       if (s < 0 || s >= sensor_count) {
+               ALOGE("Invalid sensor handle!\n");
+               return NULL;
+       }
+
+       if (!sensor_info[s].sampling_rate) {
+               ALOGE("Zero rate in acquisition routine for sensor %d\n", s);
+               return NULL;
+       }
+
+       num_fields = get_field_count(s);
+
+       /*
+        * Each condition variable is associated to a mutex that has to be
+        * locked by the thread that's waiting on it. We use these condition
+        * variables to get the acquisition threads out of sleep quickly after
+        * the sampling rate is adjusted, or the sensor is disabled.
+        */
+       pthread_mutex_lock(&thread_release_mutex[s]);
+
+       while (1) {
+               /* Pinpoint the moment we start sampling */
+               clock_gettime(CLOCK_REALTIME, &entry_time);
+
+               ALOGV("Acquiring sample data for sensor %d through sysfs\n", s);
+
+               /* Read values through sysfs */
+               for (c=0; c<num_fields; c++) {
+                       data.data[c] = acquire_immediate_value(s, c);
+
+                       /* Check and honor termination requests */
+                       if (sensor_info[s].thread_data_fd[1] == -1)
+                               goto exit;
+
+                       ALOGV("\tfield %d: %f\n", c, data.data[c]);
+
+               }
+
+               /* If the sample looks good */
+               if (sensor_info[s].ops.finalize(s, &data)) {
+
+                       /* Pipe it for transmission to poll loop */
+                       ret = write(    sensor_info[s].thread_data_fd[1],
+                                       data.data,
+                                       num_fields * sizeof(float));
+               }
+
+               /* Check and honor termination requests */
+               if (sensor_info[s].thread_data_fd[1] == -1)
+                       goto exit;
+
+
+               period = (int64_t) (1000000000.0/ sensor_info[s].sampling_rate);
+               period = period * OVERHEAD_THRESHOLD;
+               time_add(&target_time, &entry_time, period);
+
+               /*
+                * Wait until the sampling time elapses, or a rate change is
+                * signaled, or a thread exit is requested.
+                */
+               ret = pthread_cond_timedwait(   &thread_release_cond[s],
+                                               &thread_release_mutex[s],
+                                               &target_time);
+
+               /* Check and honor termination requests */
+               if (sensor_info[s].thread_data_fd[1] == -1)
+                               goto exit;
+       }
+
+exit:
+       ALOGV("Acquisition thread for S%d exiting\n", s);
+       pthread_mutex_unlock(&thread_release_mutex[s]);
+       pthread_exit(0);
+       return NULL;
+}
+
+
+static void start_acquisition_thread (int s)
+{
+       int incoming_data_fd;
+       int ret;
+
+       struct epoll_event ev = {0};
+
+       ALOGV("Initializing acquisition context for sensor %d\n", s);
+
+       /* Create condition variable and mutex for quick thread release */
+       ret = pthread_cond_init(&thread_release_cond[s], NULL);
+       ret = pthread_mutex_init(&thread_release_mutex[s], NULL);
+
+       /* Create a pipe for inter thread communication */
+       ret = pipe(sensor_info[s].thread_data_fd);
+
+       incoming_data_fd = sensor_info[s].thread_data_fd[0];
+
+       ev.events = EPOLLIN;
+       ev.data.u32 = THREAD_REPORT_TAG_BASE + s;
+
+       /* Add incoming side of pipe to our poll set, with a suitable tag */
+       ret = epoll_ctl(poll_fd, EPOLL_CTL_ADD, incoming_data_fd , &ev);
+
+       /* Create and start worker thread */
+       ret = pthread_create(   &sensor_info[s].acquisition_thread,
+                               NULL,
+                               acquisition_routine,
+                               (void*) (size_t) s);
+}
+
+
+static void stop_acquisition_thread (int s)
+{
+       int incoming_data_fd = sensor_info[s].thread_data_fd[0];
+       int outgoing_data_fd = sensor_info[s].thread_data_fd[1];
+
+       ALOGV("Tearing down acquisition context for sensor %d\n", s);
+
+       /* Delete the incoming side of the pipe from our poll set */
+       epoll_ctl(poll_fd, EPOLL_CTL_DEL, incoming_data_fd, NULL);
+
+       /* Mark the pipe ends as invalid ; that's a cheap exit flag */
+       sensor_info[s].thread_data_fd[0] = -1;
+       sensor_info[s].thread_data_fd[1] = -1;
+
+       /* Close both sides of our pipe */
+       close(incoming_data_fd);
+       close(outgoing_data_fd);
+
+       /* Stop acquisition thread and clean up thread handle */
+       pthread_cond_signal(&thread_release_cond[s]);
+       pthread_join(sensor_info[s].acquisition_thread, NULL);
+
+       /* Clean up our sensor descriptor */
+       sensor_info[s].acquisition_thread = -1;
+
+       /* Delete condition variable and mutex */
+       pthread_cond_destroy(&thread_release_cond[s]);
+       pthread_mutex_destroy(&thread_release_mutex[s]);
+}
+
+
 int sensor_activate(int s, int enabled)
 {
-       char sysfs_path[PATH_MAX];
        char device_name[PATH_MAX];
-       char trigger_name[MAX_NAME_SIZE + 16];
-       int c;
        struct epoll_event ev = {0};
        int dev_fd;
        int ret;
        int dev_num = sensor_info[s].dev_num;
-       int i = sensor_info[s].catalog_index;
        int is_poll_sensor = !sensor_info[s].num_channels;
 
-       ret = adjust_counters(s, enabled);
+       /* Prepare the report timestamp field for the first event, see set_report_ts method */
+       sensor_info[s].report_ts = 0;
 
-       /* If the operation was neutral in terms of state, we're done */
-       if (ret <= 0)
-               return ret;
+       /* If we want to activate gyro calibrated and gyro uncalibrated is activated
+        * Deactivate gyro uncalibrated - Uncalibrated releases handler
+        * Activate gyro calibrated     - Calibrated has handler
+        * Reactivate gyro uncalibrated - Uncalibrated gets data from calibrated */
 
-       if (!is_poll_sensor) {
-               /* Changes have to be made while the buffer is turned off */
-               enable_buffer(dev_num, 0);
+       /* If we want to deactivate gyro calibrated and gyro uncalibrated is active
+        * Deactivate gyro uncalibrated - Uncalibrated no longer gets data from handler
+        * Deactivate gyro calibrated   - Calibrated releases handler
+        * Reactivate gyro uncalibrated - Uncalibrated has handler */
 
-               /* Configure trigger */
-               switch (trig_sensors_per_dev[dev_num]) {
-                       case 0:
-                               setup_trigger(dev_num, "none");
-                               break;
+       if (sensor_catalog[sensor_info[s].catalog_index].type == SENSOR_TYPE_GYROSCOPE &&
+               sensor_info[s].pair_idx && sensor_info[sensor_info[s].pair_idx].enable_count != 0) {
 
-                       case 1:
-                               sprintf(trigger_name, "%s-dev%d",
-                                       sensor_info[s].internal_name, dev_num);
+                               sensor_activate(sensor_info[s].pair_idx, 0);
+                               ret = sensor_activate(s, enabled);
+                               sensor_activate(sensor_info[s].pair_idx, 1);
+                               return ret;
+       }
 
-                               setup_trigger(dev_num, trigger_name);
-                               break;
+       ret = adjust_counters(s, enabled);
 
-                       default:
-                               /* The trigger is already set */
-                               break;
-               }
+       /* If the operation was neutral in terms of state, we're done */
+       if (ret <= 0)
+               return ret;
 
-               /*
-                * Turn channels associated to this sensor on or off, and update
-                * the channels maps for all sensors associated to this device.
-                */
-               for (c=0;c<sensor_info[s].num_channels; c++) {
-                       sprintf(sysfs_path, CHANNEL_PATH "%s",
-                               sensor_info[s].dev_num,
-                               sensor_catalog[i].channel[c].en_path);
 
-                       sysfs_write_int(sysfs_path, enabled);
-               }
+       if (!is_poll_sensor) {
 
-               /* If there's at least one sensor left */
+               /* Stop sampling */
+               enable_buffer(dev_num, 0);
+               setup_trigger(s, "\n");
+
+               /* If there's at least one sensor enabled on this iio device */
                if (trig_sensors_per_dev[dev_num]) {
-                       refresh_sensor_report_maps(dev_num);
+
+                       /* Start sampling */
+                       setup_trigger(s, sensor_info[s].init_trigger_name);
                        enable_buffer(dev_num, 1);
                }
        }
 
        /*
         * Make sure we have a fd on the character device ; conversely, close
-        * the fd if no one is using associated sensor anymore. The assumption
+        * the fd if no one is using associated sensors anymore. The assumption
         * here is that the underlying driver will power on the relevant
-        * hardware block while someone hold a fd on the device.
+        * hardware block while someone holds a fd on the device.
         */
        dev_fd = device_fd[dev_num];
 
        if (!enabled) {
+               if (is_poll_sensor)
+                       stop_acquisition_thread(s);
+
                if (dev_fd != -1 && !poll_sensors_per_dev[dev_num] &&
                        !trig_sensors_per_dev[dev_num]) {
                                /*
@@ -343,6 +597,18 @@ int sensor_activate(int s, int enabled)
                                close(dev_fd);
                                device_fd[dev_num] = -1;
                        }
+
+               /* If we recorded a trail of samples for filtering, delete it */
+               if (sensor_info[s].history) {
+                       free(sensor_info[s].history);
+                       sensor_info[s].history = NULL;
+                       sensor_info[s].history_size = 0;
+                       if (sensor_info[s].history_sum) {
+                               free(sensor_info[s].history_sum);
+                               sensor_info[s].history_sum = NULL;
+                       }
+               }
+
                return 0;
        }
 
@@ -380,38 +646,149 @@ int sensor_activate(int s, int enabled)
                }
        }
 
-       /* Release the polling loop so an updated timeout gets used */
-       write(poll_socket_pair[1], "", 1);
+       /* Ensure that on-change sensors send at least one event after enable */
+       sensor_info[s].prev_val = -1;
+
+       if (is_poll_sensor)
+               start_acquisition_thread(s);
 
        return 0;
 }
 
 
+static int is_fast_accelerometer (int s)
+{
+       /*
+        * Some games don't react well to accelerometers using any-motion
+        * triggers. Even very low thresholds seem to trip them, and they tend
+        * to request fairly high event rates. Favor continuous triggers if the
+        * sensor is an accelerometer and uses a sampling rate of at least 25.
+        */
+       int catalog_index = sensor_info[s].catalog_index;
+
+       if (sensor_catalog[catalog_index].type != SENSOR_TYPE_ACCELEROMETER)
+               return 0;
+
+       if (sensor_info[s].sampling_rate < 25)
+               return 0;
+
+       return 1;
+}
+
+
+static void enable_motion_trigger (int dev_num)
+{
+       /*
+        * In the ideal case, we enumerate two triggers per iio device ; the
+        * default (periodically firing) trigger, and another one (the motion
+        * trigger) that only fires up when motion is detected. This second one
+        * allows for lesser energy consumption, but requires periodic sample
+        * duplication at the HAL level for sensors that Android defines as
+        * continuous. This "duplicate last sample" logic can only be engaged
+        * once we got a first sample for the driver, so we start with the
+        * default trigger when an iio device is first opened, then adjust the
+        * trigger when we got events for all active sensors. Unfortunately in
+        * the general case several sensors can be associated to a given iio
+        * device, they can independently be controlled, and we have to adjust
+        * the trigger in use at the iio device level depending on whether or
+        * not appropriate conditions are met at the sensor level.
+        */
+
+       int s;
+       int i;
+       int active_sensors = trig_sensors_per_dev[dev_num];
+       int candidate[MAX_SENSORS];
+       int candidate_count = 0;
+
+       if  (!active_sensors)
+               return;
+
+       /* Check that all active sensors are ready to switch */
+
+       for (s=0; s<MAX_SENSORS; s++)
+               if (sensor_info[s].dev_num == dev_num &&
+                   sensor_info[s].enable_count &&
+                   sensor_info[s].num_channels &&
+                   (!sensor_info[s].motion_trigger_name[0] ||
+                    !sensor_info[s].report_initialized ||
+                    is_fast_accelerometer(s))
+                   )
+                       return; /* Nope */
+
+       /* Record which particular sensors need to switch */
+
+       for (s=0; s<MAX_SENSORS; s++)
+               if (sensor_info[s].dev_num == dev_num &&
+                   sensor_info[s].enable_count &&
+                   sensor_info[s].num_channels &&
+                   !(sensor_info[s].quirks & QUIRK_CONTINUOUS_DRIVER) &&
+                   sensor_info[s].selected_trigger !=
+                       sensor_info[s].motion_trigger_name)
+                               candidate[candidate_count++] = s;
+
+       if (!candidate_count)
+               return;
+
+       /* Now engage the motion trigger for sensors which aren't using it */
+
+       enable_buffer(dev_num, 0);
+
+       for (i=0; i<candidate_count; i++) {
+               s = candidate[i];
+               setup_trigger(s, sensor_info[s].motion_trigger_name);
+       }
+
+       enable_buffer(dev_num, 1);
+}
+
+void set_report_ts(int s, int64_t ts)
+{
+       int64_t maxTs, period;
+
+       /*
+       *  A bit of a hack to please a bunch of cts tests. They
+       *  expect the timestamp to be exacly according to the set-up
+       *  frequency but if we're simply getting the timestamp at hal level
+       *  this may not be the case. Perhaps we'll get rid of this when
+       *  we'll be reading the timestamp from the iio channel for all sensors
+       */
+       if (sensor_info[s].report_ts && sensor_info[s].sampling_rate &&
+               REPORTING_MODE(sensor_desc[s].flags) == SENSOR_FLAG_CONTINUOUS_MODE)
+       {
+               period = (int64_t) (1000000000LL / sensor_info[s].sampling_rate);
+               maxTs = sensor_info[s].report_ts + period;
+               sensor_info[s].report_ts = (ts < maxTs ? ts : maxTs);
+       } else {
+               sensor_info[s].report_ts = ts;
+       }
+}
+
 static int integrate_device_report(int dev_num)
 {
        int len;
        int s,c;
-       unsigned char buf[MAX_SENSOR_REPORT_SIZE * MAX_SENSORS] = { 0 };
+       unsigned char buf[MAX_SENSOR_REPORT_SIZE] = { 0 };
        int sr_offset;
        unsigned char *target;
        unsigned char *source;
        int size;
-       int expected_size = 0;
+       int64_t ts;
 
-       /* There's an incoming report on the specified fd */
+       /* There's an incoming report on the specified iio device char dev fd */
 
-       if (dev_num < 0 || dev_num >= MAX_DEVICES ||
-               !trig_sensors_per_dev[dev_num]) {
+       if (dev_num < 0 || dev_num >= MAX_DEVICES) {
                ALOGE("Event reported on unexpected iio device %d\n", dev_num);
                return -1;
        }
 
-       for (s=0; s<MAX_SENSORS; s++)
-               if (sensor_info[s].dev_num == dev_num)
-                       for (c=0; c<sensor_info[s].num_channels; c++)
-                               expected_size += sensor_info[s].channel[c].size;
+       if (device_fd[dev_num] == -1) {
+               ALOGE("Ignoring stale report on iio device %d\n", dev_num);
+               return -1;
+       }
 
-       len = read(device_fd[dev_num], buf, expected_size);
+       ts = get_timestamp();
+
+       len = read(device_fd[dev_num], buf, MAX_SENSOR_REPORT_SIZE);
 
        if (len == -1) {
                ALOGE("Could not read report from iio device %d (%s)\n",
@@ -421,8 +798,12 @@ static int integrate_device_report(int dev_num)
 
        ALOGV("Read %d bytes from iio device %d\n", len, dev_num);
 
+       /* Map device report to sensor reports */
+
        for (s=0; s<MAX_SENSORS; s++)
-               if (sensor_info[s].dev_num == dev_num) {
+               if (sensor_info[s].dev_num == dev_num &&
+                   sensor_info[s].enable_count) {
+
                        sr_offset = 0;
 
                        /* Copy data from device to sensor report buffer */
@@ -440,90 +821,61 @@ static int integrate_device_report(int dev_num)
                                sr_offset += size;
                        }
 
-                       if (sensor_info[s].enable_count) {
-                               ALOGV("Sensor %d report available (%d bytes)\n",
-                                     s, sr_offset);
+                       ALOGV("Sensor %d report available (%d bytes)\n", s,
+                             sr_offset);
 
-                               sensor_info[s].report_pending = 1;
-                       }
+                       set_report_ts(s, ts);
+                       sensor_info[s].report_pending = 1;
+                       sensor_info[s].report_initialized = 1;
                }
 
+       /* Tentatively switch to an any-motion trigger if conditions are met */
+       enable_motion_trigger(dev_num);
+
        return 0;
 }
 
 
-static void propagate_sensor_report(int s, struct sensors_event_t* data)
+static int propagate_sensor_report(int s, struct sensors_event_t  *data)
 {
        /* There's a sensor report pending for this sensor ; transmit it */
 
        int catalog_index = sensor_info[s].catalog_index;
-       int sensor_type = sensor_catalog[catalog_index].type;
-       int num_fields;
+       int sensor_type   = sensor_catalog[catalog_index].type;
+       int num_fields    = get_field_count(s);
        int c;
        unsigned char* current_sample;
 
-       memset(data, 0, sizeof(sensors_event_t));
-
-       data->version = sizeof(sensors_event_t);
-       data->sensor = s;
-       data->type = sensor_type;
-       data->timestamp = get_timestamp();
-
-       switch (sensor_type) {
-               case SENSOR_TYPE_ACCELEROMETER:         /* m/s^2        */
-               case SENSOR_TYPE_MAGNETIC_FIELD:        /* micro-tesla  */
-               case SENSOR_TYPE_ORIENTATION:           /* degrees      */
-               case SENSOR_TYPE_GYROSCOPE:             /* radians/s    */
-                       num_fields = 3;
-                       break;
+       /* If there's nothing to return... we're done */
+       if (!num_fields)
+               return 0;
 
-               case SENSOR_TYPE_LIGHT:                 /* SI lux units */
-               case SENSOR_TYPE_AMBIENT_TEMPERATURE:   /* °C          */
-               case SENSOR_TYPE_TEMPERATURE:           /* °C          */
-               case SENSOR_TYPE_PROXIMITY:             /* centimeters  */
-               case SENSOR_TYPE_PRESSURE:              /* hecto-pascal */
-               case SENSOR_TYPE_RELATIVE_HUMIDITY:     /* percent */
-                       num_fields = 1;
-                       break;
 
-               case SENSOR_TYPE_ROTATION_VECTOR:
-                       num_fields = 4;
-                       break;
+       /* Only return uncalibrated event if also gyro active */
+       if (sensor_type == SENSOR_TYPE_GYROSCOPE_UNCALIBRATED &&
+               sensor_info[sensor_info[s].pair_idx].enable_count != 0)
+                       return 0;
 
-               case SENSOR_TYPE_DEVICE_PRIVATE_BASE:   /* hidden for now */
-                       num_fields = 0;
-                       break;
+       memset(data, 0, sizeof(sensors_event_t));
 
-               default:
-                       ALOGE("Unknown sensor type!\n");
-                       num_fields = 0;
-                       break;
-       }
+       data->version   = sizeof(sensors_event_t);
+       data->sensor    = s;
+       data->type      = sensor_type;
+       data->timestamp = sensor_info[s].report_ts;
 
        ALOGV("Sample on sensor %d (type %d):\n", s, sensor_type);
 
-       /* Take note of current time counter value for rate control purposes */
-       sensor_info[s].last_integration_ts = get_timestamp();
+       current_sample = sensor_info[s].report_buffer;
 
-       /* If we're dealing with a poll-mode sensor */
+       /* If this is a poll sensor */
        if (!sensor_info[s].num_channels) {
-
-               /* Read values through sysfs rather than from a report buffer */
-               for (c=0; c<num_fields; c++) {
-
-                       data->data[c] = acquire_immediate_value(s, c);
-
-                       ALOGV("\tfield %d: %f\n", c, data->data[c]);
-               }
-
-               sensor_info[s].ops.finalize(s, data);
-               return;
+               /* Use the data provided by the acquisition thread */
+               ALOGV("Reporting data from worker thread for S%d\n", s);
+               memcpy(data->data, current_sample, num_fields * sizeof(float));
+               return 1;
        }
 
        /* Convert the data into the expected Android-level format */
-
-       current_sample = sensor_info[s].report_buffer;
-
        for (c=0; c<num_fields; c++) {
 
                data->data[c] = sensor_info[s].ops.transform
@@ -533,52 +885,127 @@ static void propagate_sensor_report(int s, struct sensors_event_t* data)
                current_sample += sensor_info[s].channel[c].size;
        }
 
-       sensor_info[s].ops.finalize(s, data);
+       /*
+        * The finalize routine, in addition to its late sample processing duty,
+        * has the final say on whether or not the sample gets sent to Android.
+        */
+       return sensor_info[s].ops.finalize(s, data);
 }
 
 
-static int get_poll_time (void)
+static void synthetize_duplicate_samples (void)
 {
-       int64_t target_ts;
-       int64_t lowest_target_ts;
-       int64_t current_ts;
+       /*
+        * Some sensor types (ex: gyroscope) are defined as continuously firing
+        * by Android, despite the fact that we can be dealing with iio drivers
+        * that only report events for new samples. For these we generate
+        * reports periodically, duplicating the last data we got from the
+        * driver. This is not necessary for polling sensors.
+        */
+
        int s;
+       int64_t current_ts;
+       int64_t target_ts;
+       int64_t period;
 
-       if (!active_poll_sensors)
-               return -1;      /* Infinite wait */
+       for (s=0; s<sensor_count; s++) {
 
-       /* Check if we should schedule a poll-mode sensor event delivery */
+               /* Ignore disabled sensors */
+               if (!sensor_info[s].enable_count)
+                       continue;
 
-       lowest_target_ts = INT64_MAX;
+               /* If the sensor is continuously firing, leave it alone */
+               if (    sensor_info[s].selected_trigger !=
+                       sensor_info[s].motion_trigger_name)
+                       continue;
 
-       for (s=0; s<sensor_count; s++)
-               if (sensor_info[s].enable_count &&
-                   sensor_info[s].sampling_rate &&
-                   !sensor_info[s].num_channels) {
-                               target_ts = sensor_info[s].last_integration_ts +
-                                     1000000000LL/sensor_info[s].sampling_rate;
+               /* If we haven't seen a sample, there's nothing to duplicate */
+               if (!sensor_info[s].report_initialized)
+                       continue;
 
-                               if (target_ts < lowest_target_ts)
-                                       lowest_target_ts = target_ts;
-                       }
+               /* If a sample was recently buffered, leave it alone too */
+               if (sensor_info[s].report_pending)
+                       continue;
 
-       if (lowest_target_ts == INT64_MAX)
-               return -1;
+               /* We also need a valid sampling rate to be configured */
+               if (!sensor_info[s].sampling_rate)
+                       continue;
 
-       current_ts = get_timestamp();
+               period = (int64_t) (1000000000.0/ sensor_info[s].sampling_rate);
 
-       if (lowest_target_ts <= current_ts)
-               return 0;
+               current_ts = get_timestamp();
+               target_ts = sensor_info[s].report_ts + period;
 
-       return (lowest_target_ts - current_ts)/1000000; /* ms */
+               if (target_ts <= current_ts) {
+                       /* Mark the sensor for event generation */
+                       set_report_ts(s, current_ts);
+                       sensor_info[s].report_pending = 1;
+               }
+       }
 }
 
 
-static void acknowledge_release (void)
+static void integrate_thread_report (uint32_t tag)
 {
-       /* A write to our socket circuit was performed to release epoll */
-       char buf;
-       read(poll_socket_pair[0], &buf, 1);
+       int s = tag - THREAD_REPORT_TAG_BASE;
+       int len;
+       int expected_len;
+
+       expected_len = get_field_count(s) * sizeof(float);
+
+       len = read(sensor_info[s].thread_data_fd[0],
+                  sensor_info[s].report_buffer,
+                  expected_len);
+
+       if (len == expected_len) {
+               set_report_ts(s, get_timestamp());
+               sensor_info[s].report_pending = 1;
+       }
+}
+
+
+static int get_poll_wait_timeout (void)
+{
+       /*
+        * Compute an appropriate timeout value, in ms, for the epoll_wait
+        * call that's going to await for iio device reports and incoming
+        * reports from our sensor sysfs data reader threads.
+        */
+
+       int s;
+       int64_t target_ts = INT64_MAX;
+       int64_t ms_to_wait;
+       int64_t period;
+
+       /*
+        * Check if have have to deal with "terse" drivers that only send events
+        * when there is motion, despite the fact that the associated Android
+        * sensor type is continuous rather than on-change. In that case we have
+        * to duplicate events. Check deadline for the nearest upcoming event.
+        */
+       for (s=0; s<sensor_count; s++)
+               if (sensor_info[s].enable_count &&
+                   sensor_info[s].selected_trigger ==
+                   sensor_info[s].motion_trigger_name &&
+                   sensor_info[s].sampling_rate) {
+                       period = (int64_t) (1000000000.0 /
+                                               sensor_info[s].sampling_rate);
+
+                       if (sensor_info[s].report_ts + period < target_ts)
+                               target_ts = sensor_info[s].report_ts + period;
+               }
+
+       /* If we don't have such a driver to deal with */
+       if (target_ts == INT64_MAX)
+               return -1; /* Infinite wait */
+
+       ms_to_wait = (target_ts - get_timestamp()) / 1000000;
+
+       /* If the target timestamp is already behind us, don't wait */
+       if (ms_to_wait < 1)
+               return 0;
+
+       return ms_to_wait;
 }
 
 
@@ -587,63 +1014,99 @@ int sensor_poll(struct sensors_event_t* data, int count)
        int s;
        int i;
        int nfds;
-       int delta;
        struct epoll_event ev[MAX_DEVICES];
+       int returned_events;
+       int event_count;
+       int uncal_start;
 
        /* Get one or more events from our collection of sensors */
 
-return_first_available_sensor_report:
+return_available_sensor_reports:
 
-       /* If there's at least one available report */
-       for (s=0; s<sensor_count; s++)
-               if (sensor_info[s].report_pending) {
+       /* Synthetize duplicate samples if needed */
+       synthetize_duplicate_samples();
+
+       returned_events = 0;
 
-                       /* Return that up */
-                       propagate_sensor_report(s, data);
+       /* Check our sensor collection for available reports */
+       for (s=0; s<sensor_count && returned_events < count; s++)
+               if (sensor_info[s].report_pending) {
+                       event_count = 0;
+                       /* Lower flag */
                        sensor_info[s].report_pending = 0;
-                       ALOGV("Report on sensor %d\n", s);
-                       return 1;
+
+                       /* Report this event if it looks OK */
+                       event_count = propagate_sensor_report(s, &data[returned_events]);
+
+                       /* Duplicate only if both cal & uncal are active */
+                       if (sensor_catalog[sensor_info[s].catalog_index].type == SENSOR_TYPE_GYROSCOPE &&
+                                       sensor_info[s].pair_idx && sensor_info[sensor_info[s].pair_idx].enable_count != 0) {
+                                       struct gyro_cal* gyro_data = (struct gyro_cal*) sensor_info[s].cal_data;
+
+                                       memcpy(&data[returned_events + event_count], &data[returned_events],
+                                                       sizeof(struct sensors_event_t) * event_count);
+
+                                       uncal_start = returned_events + event_count;
+                                       for (i = 0; i < event_count; i++) {
+                                               data[uncal_start + i].type = SENSOR_TYPE_GYROSCOPE_UNCALIBRATED;
+                                               data[uncal_start + i].sensor = sensor_info[s].pair_idx;
+
+                                               data[uncal_start + i].data[0] = data[returned_events + i].data[0] + gyro_data->bias_x;
+                                               data[uncal_start + i].data[1] = data[returned_events + i].data[1] + gyro_data->bias_y;
+                                               data[uncal_start + i].data[2] = data[returned_events + i].data[2] + gyro_data->bias_z;
+
+                                               data[uncal_start + i].uncalibrated_gyro.bias[0] = gyro_data->bias_x;
+                                               data[uncal_start + i].uncalibrated_gyro.bias[1] = gyro_data->bias_y;
+                                               data[uncal_start + i].uncalibrated_gyro.bias[2] = gyro_data->bias_z;
+                                       }
+                                       event_count <<= 1;
+                       }
+                       sensor_info[sensor_info[s].pair_idx].report_pending = 0;
+                       returned_events += event_count;
+                       /*
+                        * If the sample was deemed invalid or unreportable,
+                        * e.g. had the same value as the previously reported
+                        * value for a 'on change' sensor, silently drop it.
+                        */
                }
-await_event:
 
-       /* Keep a minimum time interval between poll operations */
-       delta = (get_timestamp() - last_poll_exit_ts)/1000;
+       if (returned_events)
+               return returned_events;
 
-       if (delta > 0 && delta < POLL_MIN_INTERVAL)
-               usleep(POLL_MIN_INTERVAL - delta);
+await_event:
 
        ALOGV("Awaiting sensor data\n");
 
-       nfds = epoll_wait(poll_fd, ev, MAX_DEVICES, get_poll_time());
-
-       last_poll_exit_ts = get_timestamp();
+       nfds = epoll_wait(poll_fd, ev, MAX_DEVICES, get_poll_wait_timeout());
 
        if (nfds == -1) {
-               ALOGI("epoll_wait returned -1 (%s)\n", strerror(errno));
+               ALOGE("epoll_wait returned -1 (%s)\n", strerror(errno));
                goto await_event;
        }
 
        ALOGV("%d fds signalled\n", nfds);
 
-       /* For each of the devices for which a report is available */
+       /* For each of the signalled sources */
        for (i=0; i<nfds; i++)
-               if (ev[i].events == EPOLLIN) {
-                       if (ev[i].data.u32 == INVALID_DEV_NUM) {
-                               acknowledge_release();
-                               goto await_event;
-                       } else
-                               /* Read report */
-                               integrate_device_report(ev[i].data.u32);
-               }
-
-       /* It's a good time to invalidate poll-mode sensor values */
-       if (active_poll_sensors)
-               for (s=0; s<sensor_count; s++)
-                       if (sensor_info[s].enable_count &&
-                               !sensor_info[s].num_channels)
-                                       sensor_info[s].report_pending = 1;
+               if (ev[i].events == EPOLLIN)
+                       switch (ev[i].data.u32) {
+                               case 0 ... MAX_DEVICES-1:
+                                       /* Read report from iio char dev fd */
+                                       integrate_device_report(ev[i].data.u32);
+                                       break;
+
+                               case THREAD_REPORT_TAG_BASE ...
+                                    THREAD_REPORT_TAG_BASE + MAX_SENSORS-1:
+                                       /* Get report from acquisition thread */
+                                       integrate_thread_report(ev[i].data.u32);
+                                       break;
+
+                               default:
+                                       ALOGW("Unexpected event source!\n");
+                                       break;
+                       }
 
-       goto return_first_available_sensor_report;
+       goto return_available_sensor_reports;
 }
 
 
@@ -654,44 +1117,148 @@ int sensor_set_delay(int s, int64_t ns)
        /* See Android sensors.h for indication on sensor trigger modes */
 
        char sysfs_path[PATH_MAX];
+       char avail_sysfs_path[PATH_MAX];
        int dev_num             =       sensor_info[s].dev_num;
        int i                   =       sensor_info[s].catalog_index;
        const char *prefix      =       sensor_catalog[i].tag;
-       int new_sampling_rate;
-       int cur_sampling_rate;
+       float new_sampling_rate; /* Granted sampling rate after arbitration   */
+       float cur_sampling_rate; /* Currently used sampling rate              */
+       int per_sensor_sampling_rate;
+       int per_device_sampling_rate;
+       int32_t min_delay_us = sensor_desc[s].minDelay;
+       max_delay_t max_delay_us = sensor_desc[s].maxDelay;
+       float min_supported_rate = max_delay_us ? (1000000.0f / max_delay_us) : 1;
+       float max_supported_rate = 
+               (min_delay_us && min_delay_us != -1) ? (1000000.0f / min_delay_us) : 0;
+       char freqs_buf[100];
+       char* cursor;
+       int n;
+       float sr;
 
        if (!ns) {
                ALOGE("Rejecting zero delay request on sensor %d\n", s);
                return -EINVAL;
        }
 
-       new_sampling_rate = (int) (1000000000L/ns);
+       new_sampling_rate = 1000000000LL/ns;
 
-       if (!new_sampling_rate) {
-               ALOGI("Sub-HZ sampling rate requested on on sensor %d\n", s);
-               new_sampling_rate = 1;
+       /*
+        * Artificially limit ourselves to 1 Hz or higher. This is mostly to
+        * avoid setting up the stage for divisions by zero.
+        */
+       if (new_sampling_rate < min_supported_rate)
+               new_sampling_rate = min_supported_rate;
+
+       if (max_supported_rate &&
+               new_sampling_rate > max_supported_rate) {
+               new_sampling_rate = max_supported_rate;
+       }
+
+       sensor_info[s].sampling_rate = new_sampling_rate;
+
+       /* If we're dealing with a poll-mode sensor */
+       if (!sensor_info[s].num_channels) {
+               /* Interrupt current sleep so the new sampling gets used */
+               pthread_cond_signal(&thread_release_cond[s]);
+               return 0;
        }
 
        sprintf(sysfs_path, SENSOR_SAMPLING_PATH, dev_num, prefix);
 
-       if (sysfs_read_int(sysfs_path, &cur_sampling_rate) != -1)
-               if (new_sampling_rate != cur_sampling_rate) {
-                       ALOGI(  "Sensor %d sampling rate set to %d\n",
-                               s, new_sampling_rate);
+       if (sysfs_read_float(sysfs_path, &cur_sampling_rate) != -1) {
+               per_sensor_sampling_rate = 1;
+               per_device_sampling_rate = 0;
+       } else {
+               per_sensor_sampling_rate = 0;
 
-                       if (trig_sensors_per_dev[dev_num])
-                               enable_buffer(dev_num, 0);
+               sprintf(sysfs_path, DEVICE_SAMPLING_PATH, dev_num);
 
-                       sysfs_write_int(sysfs_path, new_sampling_rate);
+               if (sysfs_read_float(sysfs_path, &cur_sampling_rate) != -1)
+                       per_device_sampling_rate = 1;
+               else
+                       per_device_sampling_rate = 0;
+       }
 
-                       if (trig_sensors_per_dev[dev_num])
-                               enable_buffer(dev_num, 1);
+       if (!per_sensor_sampling_rate && !per_device_sampling_rate) {
+               ALOGE("No way to adjust sampling rate on sensor %d\n", s);
+               return -ENOSYS;
        }
 
-       sensor_info[s].sampling_rate = new_sampling_rate;
+       /* Coordinate with others active sensors on the same device, if any */
+       if (per_device_sampling_rate)
+               for (n=0; n<sensor_count; n++)
+                       if (n != s && sensor_info[n].dev_num == dev_num &&
+                           sensor_info[n].num_channels &&
+                           sensor_info[n].enable_count &&
+                           sensor_info[n].sampling_rate > new_sampling_rate)
+                               new_sampling_rate= sensor_info[n].sampling_rate;
+
+       /* Check if we have contraints on allowed sampling rates */
+
+       sprintf(avail_sysfs_path, DEVICE_AVAIL_FREQ_PATH, dev_num);
+
+       if (sysfs_read_str(avail_sysfs_path, freqs_buf, sizeof(freqs_buf)) > 0){
+               cursor = freqs_buf;
+
+               /* Decode allowed sampling rates string, ex: "10 20 50 100" */
+
+               /* While we're not at the end of the string */
+               while (*cursor && cursor[0]) {
+
+                       /* Decode a single value */
+                       sr = strtod(cursor, NULL);
+
+                       /* If this matches the selected rate, we're happy */
+                       if (new_sampling_rate == sr)
+                               break;
+
+                       /*
+                        * If we reached a higher value than the desired rate,
+                        * adjust selected rate so it matches the first higher
+                        * available one and stop parsing - this makes the
+                        * assumption that rates are sorted by increasing value
+                        * in the allowed frequencies string.
+                        */
+                       if (sr > new_sampling_rate) {
+                               new_sampling_rate = sr;
+                               break;
+                       }
 
-       /* Release the polling loop so an updated timeout value gets used */
-       write(poll_socket_pair[1], "", 1);
+                       /* Skip digits */
+                       while (cursor[0] && !isspace(cursor[0]))
+                               cursor++;
+
+                       /* Skip spaces */
+                       while (cursor[0] && isspace(cursor[0]))
+                                       cursor++;
+               }
+       }
+
+
+       if (max_supported_rate &&
+               new_sampling_rate > max_supported_rate) {
+               new_sampling_rate = max_supported_rate;
+       }
+
+
+       /* If the desired rate is already active we're all set */
+       if (new_sampling_rate == cur_sampling_rate)
+               return 0;
+
+       ALOGI("Sensor %d sampling rate set to %g\n", s, new_sampling_rate);
+
+       if (trig_sensors_per_dev[dev_num])
+               enable_buffer(dev_num, 0);
+
+       sysfs_write_float(sysfs_path, new_sampling_rate);
+
+       /* Switch back to continuous sampling for accelerometer based games */
+       if (is_fast_accelerometer(s) && sensor_info[s].selected_trigger !=
+                                       sensor_info[s].init_trigger_name)
+               setup_trigger(s, sensor_info[s].init_trigger_name);
+
+       if (trig_sensors_per_dev[dev_num])
+               enable_buffer(dev_num, 1);
 
        return 0;
 }
@@ -700,7 +1267,6 @@ int sensor_set_delay(int s, int64_t ns)
 int allocate_control_data (void)
 {
        int i;
-       struct epoll_event ev = {0};
 
        for (i=0; i<MAX_DEVICES; i++)
                device_fd[i] = -1;
@@ -712,19 +1278,6 @@ int allocate_control_data (void)
                return -1;
        }
 
-       /* Create and add "unblocking" fd to the set of watched fds */
-
-       if (socketpair(AF_UNIX, SOCK_STREAM, 0, poll_socket_pair) == -1) {
-               ALOGE("Can't create socket pair for iio sensors!\n");
-               close(poll_fd);
-               return -1;
-       }
-
-       ev.events = EPOLLIN;
-       ev.data.u32 = INVALID_DEV_NUM;
-
-       epoll_ctl(poll_fd, EPOLL_CTL_ADD, poll_socket_pair[0], &ev);
-
        return poll_fd;
 }