OSDN Git Service

Only signal an acquisition thread if the target sensor is enabled
[android-x86/hardware-intel-libsensors.git] / control.c
index 28b6dc6..917c32d 100644 (file)
--- a/control.c
+++ b/control.c
@@ -7,6 +7,7 @@
 #include <fcntl.h>
 #include <pthread.h>
 #include <time.h>
+#include <math.h>
 #include <sys/epoll.h>
 #include <sys/socket.h>
 #include <utils/Log.h>
@@ -41,57 +42,61 @@ static pthread_mutex_t    thread_release_mutex      [MAX_SENSORS];
  * - 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
 
 #define ENABLE_BUFFER_RETRIES 10
 #define ENABLE_BUFFER_RETRY_DELAY_MS 10
 
-inline int is_enabled(int s)
+
+inline int is_enabled (int s)
 {
-       return (sensor_info[s].directly_enabled || sensor_info[s].ref_count);
+       return sensor[s].directly_enabled || sensor[s].ref_count;
 }
 
-static int check_state_change(int s, int enabled, int from_virtual)
+
+static int check_state_change (int s, int enabled, int from_virtual)
 {
-       if(enabled) {
-               if (sensor_info[s].directly_enabled)
-                       return 0;
+       if (enabled) {
+               if (sensor[s].directly_enabled)
+                                       /*
+                                        * We're being enabled but already were
+                                        * directly activated: no change.
+                                        */
+                                       return 0;
 
-               /* If we were enabled by Android no sample drops */
                if (!from_virtual)
-                       sensor_info[s].directly_enabled = 1;
+                       /* We're being directly enabled */
+                       sensor[s].directly_enabled = 1;
 
-               /*
-               * If we got here it means we were not previously directly enabled - we may
-               * or may not be now, whatever the case if we already had references we
-               * were already in use
-               */
-               if (sensor_info[s].ref_count)
+               if (sensor[s].ref_count)
+                       /* We were already indirectly enabled */
                        return 0;
 
-               return 1;
-
+               return 1; /* Do continue enabling this sensor */
        }
-       /* Spurious disable call */
+
        if (!is_enabled(s))
+               /* We are being disabled but already were: no change */
                return 0;
 
-       /* We're requesting disable for a virtual sensor but the base is still active */
-       if (from_virtual && sensor_info[s].directly_enabled)
+       if (from_virtual && sensor[s].directly_enabled)
+               /* We're indirectly disabled but the base is still active */
                return 0;
 
        /* If it's disable, and it's from Android, and we still have ref counts */
-       if (!from_virtual && sensor_info[s].ref_count) {
-               sensor_info[s].directly_enabled = 0;
+       if (!from_virtual && sensor[s].ref_count) {
+               sensor[s].directly_enabled = 0;
                return 0;
        }
 
        /*If perhaps we are from virtual but we're disabling it*/
-       sensor_info[s].directly_enabled = 0;
+       sensor[s].directly_enabled = 0;
 
-       return 1;
+       return 1; /* Do continue disabling this sensor */
 }
+
+
 static int enable_buffer(int dev_num, int enabled)
 {
        char sysfs_path[PATH_MAX];
@@ -129,11 +134,11 @@ static int setup_trigger (int s, const char* trigger_val)
        char sysfs_path[PATH_MAX];
        int ret = -1, attempts = 5;
 
-       sprintf(sysfs_path, TRIGGER_PATH, sensor_info[s].dev_num);
+       sprintf(sysfs_path, TRIGGER_PATH, sensor[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);
+                       sensor[s].friendly_name, trigger_val);
 
        while (ret == -1 && attempts) {
                ret = sysfs_write_str(sysfs_path, trigger_val);
@@ -141,10 +146,10 @@ static int setup_trigger (int s, const char* trigger_val)
        }
 
        if (ret != -1)
-               sensor_info[s].selected_trigger = trigger_val;
+               sensor[s].selected_trigger = trigger_val;
        else
                ALOGE("Setting S%d (%s) trigger to %s FAILED.\n", s,
-                       sensor_info[s].friendly_name, trigger_val);
+                       sensor[s].friendly_name, trigger_val);
        return ret;
 }
 
@@ -193,6 +198,39 @@ static void enable_iio_timestamp (int dev_num, int known_channels)
 }
 
 
+static int decode_type_spec (const char type_buf[MAX_TYPE_SPEC_LEN],
+                            struct datum_info_t *type_info)
+{
+       /* Return size in bytes for this type specification, or -1 in error */
+       char sign;
+       char endianness;
+       unsigned int realbits, storagebits, shift;
+       int tokens;
+
+       /* Valid specs: "le:u10/16>>0", "le:s16/32>>0" or "le:s32/32>>0" */
+
+       tokens = sscanf(type_buf, "%ce:%c%u/%u>>%u",
+                       &endianness, &sign, &realbits, &storagebits, &shift);
+
+       if     (tokens != 5 ||
+               (endianness != 'b' && endianness != 'l') ||
+               (sign != 'u' && sign != 's') ||
+               realbits > storagebits ||
+               (storagebits != 16 && storagebits != 32 && storagebits != 64)) {
+                       ALOGE("Invalid iio channel type spec: %s\n", type_buf);
+                       return -1;
+               }
+
+       type_info->endianness   =               endianness;
+       type_info->sign         =               sign;
+       type_info->realbits     =       (short) realbits;
+       type_info->storagebits  =       (short) storagebits;
+       type_info->shift        =       (short) shift;
+
+       return storagebits / 8;
+}
+
+
 void build_sensor_report_maps (int dev_num)
 {
        /*
@@ -229,17 +267,17 @@ void build_sensor_report_maps (int dev_num)
 
        /* For each sensor that is linked to this device */
        for (s=0; s<sensor_count; s++) {
-               if (sensor_info[s].dev_num != dev_num)
+               if (sensor[s].dev_num != dev_num)
                        continue;
 
-               i = sensor_info[s].catalog_index;
+               i = sensor[s].catalog_index;
 
                /* Read channel details through sysfs attributes */
-               for (c=0; c<sensor_info[s].num_channels; c++) {
+               for (c=0; c<sensor[s].num_channels; c++) {
 
                        /* Read _type file */
                        sprintf(sysfs_path, CHANNEL_PATH "%s",
-                               sensor_info[s].dev_num,
+                               sensor[s].dev_num,
                                sensor_catalog[i].channel[c].type_path);
 
                        n = sysfs_read_str(sysfs_path, spec_buf, 
@@ -251,17 +289,17 @@ void build_sensor_report_maps (int dev_num)
                                        continue;
                                }
 
-                       ch_spec = sensor_info[s].channel[c].type_spec;
+                       ch_spec = sensor[s].channel[c].type_spec;
 
                        memcpy(ch_spec, spec_buf, sizeof(spec_buf));
 
-                       ch_info = &sensor_info[s].channel[c].type_info;
+                       ch_info = &sensor[s].channel[c].type_info;
 
                        size = decode_type_spec(ch_spec, ch_info);
 
                        /* Read _index file */
                        sprintf(sysfs_path, CHANNEL_PATH "%s",
-                               sensor_info[s].dev_num,
+                               sensor[s].dev_num,
                                sensor_catalog[i].channel[c].index_path);
 
                        n = sysfs_read_int(sysfs_path, &ch_index);
@@ -291,9 +329,9 @@ void build_sensor_report_maps (int dev_num)
                 setup_trigger(s, "\n");
 
                /* Turn on channels we're aware of */
-               for (c=0;c<sensor_info[s].num_channels; c++) {
+               for (c=0;c<sensor[s].num_channels; c++) {
                        sprintf(sysfs_path, CHANNEL_PATH "%s",
-                               sensor_info[s].dev_num,
+                               sensor[s].dev_num,
                                sensor_catalog[i].channel[c].en_path);
                        sysfs_write_int(sysfs_path, 1);
                }
@@ -321,10 +359,10 @@ void build_sensor_report_maps (int dev_num)
                        continue;
 
                ALOGI("S%d C%d : offset %d, size %d, type %s\n",
-                     s, c, offset, size, sensor_info[s].channel[c].type_spec);
+                     s, c, offset, size, sensor[s].channel[c].type_spec);
 
-               sensor_info[s].channel[c].offset        = offset;
-               sensor_info[s].channel[c].size          = size;
+               sensor[s].channel[c].offset     = offset;
+               sensor[s].channel[c].size               = size;
 
                offset += size;
         }
@@ -352,46 +390,46 @@ int adjust_counters (int s, int enabled, int from_virtual)
 {
        /*
         * Adjust counters based on sensor enable action. Return values are:
-        * -1 if there's an inconsistency: abort action in this case
         *  0 if the operation was completed and we're all set
         *  1 if we toggled the state of the sensor and there's work left
         */
 
-       int dev_num = sensor_info[s].dev_num;
+       int dev_num = sensor[s].dev_num;
 
        if (!check_state_change(s, enabled, from_virtual))
+               /* The state of the sensor remains the same: we're done */
                return 0;
 
        if (enabled) {
                ALOGI("Enabling sensor %d (iio device %d: %s)\n",
-                       s, dev_num, sensor_info[s].friendly_name);
+                       s, dev_num, sensor[s].friendly_name);
 
-               switch (sensor_info[s].type) {
+               switch (sensor[s].type) {
                        case SENSOR_TYPE_MAGNETIC_FIELD:
-                               compass_read_data(&sensor_info[s]);
+                               compass_read_data(&sensor[s]);
                                break;
 
                        case SENSOR_TYPE_GYROSCOPE:
-                               gyro_cal_init(&sensor_info[s]);
+                               gyro_cal_init(&sensor[s]);
                                break;
                }
        } else {
                ALOGI("Disabling sensor %d (iio device %d: %s)\n", s, dev_num,
-                     sensor_info[s].friendly_name);
+                     sensor[s].friendly_name);
 
                /* Sensor disabled, lower report available flag */
-               sensor_info[s].report_pending = 0;
+               sensor[s].report_pending = 0;
 
-               if (sensor_info[s].type == SENSOR_TYPE_MAGNETIC_FIELD)
-                       compass_store_data(&sensor_info[s]);
+               if (sensor[s].type == SENSOR_TYPE_MAGNETIC_FIELD)
+                       compass_store_data(&sensor[s]);
 
-               if(sensor_info[s].type == SENSOR_TYPE_GYROSCOPE)
-                       gyro_store_data(&sensor_info[s]);
+               if (sensor[s].type == SENSOR_TYPE_GYROSCOPE)
+                       gyro_store_data(&sensor[s]);
        }
 
-       /* We changed the state of a sensor - adjust per iio device counters */
-       /* If this is a regular event-driven sensor */
-       if (sensor_info[s].num_channels) {
+       /* We changed the state of a sensor: adjust device ref counts */
+
+       if (sensor[s].num_channels) {
 
                        if (enabled)
                                trig_sensors_per_dev[dev_num]++;
@@ -399,7 +437,7 @@ int adjust_counters (int s, int enabled, int from_virtual)
                                trig_sensors_per_dev[dev_num]--;
 
                        return 1;
-               }
+       }
 
        if (enabled) {
                active_poll_sensors++;
@@ -415,7 +453,7 @@ int adjust_counters (int s, int enabled, int from_virtual)
 
 static int get_field_count (int s)
 {
-       switch (sensor_info[s].type) {
+       switch (sensor[s].type) {
                case SENSOR_TYPE_ACCELEROMETER:         /* m/s^2        */
                case SENSOR_TYPE_MAGNETIC_FIELD:        /* micro-tesla  */
                case SENSOR_TYPE_ORIENTATION:           /* degrees      */
@@ -432,7 +470,7 @@ static int get_field_count (int s)
                        return 1;
 
                case SENSOR_TYPE_ROTATION_VECTOR:
-                       return  4;
+                       return 4;
 
                default:
                        ALOGE("Unknown sensor type!\n");
@@ -465,12 +503,12 @@ static void* acquisition_routine (void* param)
                return NULL;
        }
 
-       ALOGI("Entering data acquisition thread S%d (%s): rate(%f), ts(%lld)\n", s,
-               sensor_info[s].friendly_name, sensor_info[s].sampling_rate, sensor_info[s].report_ts);
+       ALOGI("Entering data acquisition thread S%d (%s), rate:%g\n",
+             s, sensor[s].friendly_name, sensor[s].sampling_rate);
 
-       if (sensor_info[s].sampling_rate <= 0) {
-               ALOGE("Non-positive rate in acquisition routine for sensor %d: %f\n",
-                       s, sensor_info[s].sampling_rate);
+       if (sensor[s].sampling_rate <= 0) {
+               ALOGE("Invalid rate in acquisition routine for sensor %d: %g\n",
+                       s, sensor[s].sampling_rate);
                return NULL;
        }
 
@@ -489,43 +527,43 @@ static void* acquisition_routine (void* param)
        timestamp = get_timestamp_monotonic();
 
        /* Check and honor termination requests */
-       while (sensor_info[s].thread_data_fd[1] != -1) {
+       while (sensor[s].thread_data_fd[1] != -1) {
                start = get_timestamp_boot();
                /* 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)
+                       if (sensor[s].thread_data_fd[1] == -1)
                                goto exit;
                }
                stop = get_timestamp_boot();
                data.timestamp = start/2 + stop/2;
 
                /* If the sample looks good */
-               if (sensor_info[s].ops.finalize(s, &data)) {
+               if (sensor[s].ops.finalize(s, &data)) {
 
                        /* Pipe it for transmission to poll loop */
-                       ret = write(    sensor_info[s].thread_data_fd[1],
+                       ret = write(    sensor[s].thread_data_fd[1],
                                        &data.timestamp, sample_size);
 
                        if (ret != sample_size)
-                               ALOGE("S%d acquisition thread: tried to write %d, ret: %d\n",
-                                       s, sample_size, ret);
+                               ALOGE("S%d write failure: wrote %d, got %d\n",
+                                     s, sample_size, ret);
                }
 
                /* Check and honor termination requests */
-               if (sensor_info[s].thread_data_fd[1] == -1)
+               if (sensor[s].thread_data_fd[1] == -1)
                        goto exit;
 
-               /* Recalculate period asumming sensor_info[s].sampling_rate
+               /* Recalculate period asumming sensor[s].sampling_rate
                 * can be changed dynamically during the thread run */
-               if (sensor_info[s].sampling_rate <= 0) {
-                       ALOGE("Non-positive rate in acquisition routine for sensor %d: %f\n",
-                               s, sensor_info[s].sampling_rate);
+               if (sensor[s].sampling_rate <= 0) {
+                       ALOGE("Unexpected sampling rate for sensor %d: %g\n",
+                               s, sensor[s].sampling_rate);
                        goto exit;
                }
 
-               period = (int64_t) (1000000000LL / sensor_info[s].sampling_rate);
+               period = (int64_t) (1000000000LL / sensor[s].sampling_rate);
                timestamp += period;
                set_timestamp(&target_time, timestamp);
 
@@ -562,9 +600,9 @@ static void start_acquisition_thread (int s)
        ret = pthread_mutex_init(&thread_release_mutex[s], NULL);
 
        /* Create a pipe for inter thread communication */
-       ret = pipe(sensor_info[s].thread_data_fd);
+       ret = pipe(sensor[s].thread_data_fd);
 
-       incoming_data_fd = sensor_info[s].thread_data_fd[0];
+       incoming_data_fd = sensor[s].thread_data_fd[0];
 
        ev.events = EPOLLIN;
        ev.data.u32 = THREAD_REPORT_TAG_BASE + s;
@@ -573,7 +611,7 @@ static void start_acquisition_thread (int s)
        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,
+       ret = pthread_create(   &sensor[s].acquisition_thread,
                                NULL,
                                acquisition_routine,
                                (void*) (size_t) s);
@@ -582,8 +620,8 @@ static void start_acquisition_thread (int 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];
+       int incoming_data_fd = sensor[s].thread_data_fd[0];
+       int outgoing_data_fd = sensor[s].thread_data_fd[1];
 
        ALOGV("Tearing down acquisition context for sensor %d\n", s);
 
@@ -591,8 +629,8 @@ static void stop_acquisition_thread (int s)
        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;
+       sensor[s].thread_data_fd[0] = -1;
+       sensor[s].thread_data_fd[1] = -1;
 
        /* Close both sides of our pipe */
        close(incoming_data_fd);
@@ -600,42 +638,43 @@ static void stop_acquisition_thread (int s)
 
        /* Stop acquisition thread and clean up thread handle */
        pthread_cond_signal(&thread_release_cond[s]);
-       pthread_join(sensor_info[s].acquisition_thread, NULL);
+       pthread_join(sensor[s].acquisition_thread, NULL);
 
        /* Clean up our sensor descriptor */
-       sensor_info[s].acquisition_thread = -1;
+       sensor[s].acquisition_thread = -1;
 
        /* Delete condition variable and mutex */
        pthread_cond_destroy(&thread_release_cond[s]);
        pthread_mutex_destroy(&thread_release_mutex[s]);
 }
 
-static void sensor_activate_virtual(int s, int enabled, int from_virtual)
+
+static void sensor_activate_virtual (int s, int enabled, int from_virtual)
 {
        int i, base;
 
-       sensor_info[s].event_count = 0;
-       sensor_info[s].meta_data_pending = 0;
+       sensor[s].event_count = 0;
+       sensor[s].meta_data_pending = 0;
 
        if (!check_state_change(s, enabled, from_virtual))
                return;
        if (enabled) {
                /* Enable all the base sensors for this virtual one */
-               for (i = 0; i < sensor_info[s].base_count; i++) {
-                       base = sensor_info[s].base_idx[i];
+               for (i = 0; i < sensor[s].base_count; i++) {
+                       base = sensor[s].base[i];
                        sensor_activate(base, enabled, 1);
-                       sensor_info[base].ref_count++;
+                       sensor[base].ref_count++;
                }
                return;
        }
 
        /* Sensor disabled, lower report available flag */
-       sensor_info[s].report_pending = 0;
+       sensor[s].report_pending = 0;
 
-       for (i = 0; i < sensor_info[s].base_count; i++) {
-               base = sensor_info[s].base_idx[i];
+       for (i = 0; i < sensor[s].base_count; i++) {
+               base = sensor[s].base[i];
                sensor_activate(base, enabled, 1);
-               sensor_info[base].ref_count--;
+               sensor[base].ref_count--;
        }
 
 }
@@ -650,15 +689,16 @@ static int is_fast_accelerometer (int s)
         * sensor is an accelerometer and uses a sampling rate of at least 25.
         */
 
-       if (sensor_info[s].type != SENSOR_TYPE_ACCELEROMETER)
+       if (sensor[s].type != SENSOR_TYPE_ACCELEROMETER)
                return 0;
 
-       if (sensor_info[s].sampling_rate < 25)
+       if (sensor[s].sampling_rate < 25)
                return 0;
 
        return 1;
 }
 
+
 static void tentative_switch_trigger (int s)
 {
        /*
@@ -671,50 +711,65 @@ static void tentative_switch_trigger (int s)
         */
 
        if (is_fast_accelerometer(s) &&
-               !(sensor_info[s].quirks & QUIRK_TERSE_DRIVER) &&
-                       sensor_info[s].selected_trigger ==
-                               sensor_info[s].motion_trigger_name)
-               setup_trigger(s, sensor_info[s].init_trigger_name);
+               !(sensor[s].quirks & QUIRK_TERSE_DRIVER) &&
+                       sensor[s].selected_trigger ==
+                               sensor[s].motion_trigger_name)
+               setup_trigger(s, sensor[s].init_trigger_name);
 }
 
-static int setup_delay_sysfs(int s, float new_sampling_rate)
+
+static int setup_delay_sysfs (int s, float requested_rate)
 {
        /* Set the rate at which a specific sensor should report events */
-
        /* See Android sensors.h for indication on sensor trigger modes */
 
        char sysfs_path[PATH_MAX];
        char avail_sysfs_path[PATH_MAX];
-       float cur_sampling_rate; /* Currently used sampling rate              */
-       int dev_num             =       sensor_info[s].dev_num;
-       int i                   =       sensor_info[s].catalog_index;
+       int dev_num             =       sensor[s].dev_num;
+       int i                   =       sensor[s].catalog_index;
        const char *prefix      =       sensor_catalog[i].tag;
        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.0 / max_delay_us) : 1;
+       float min_supported_rate = max_delay_us ? 1000000.0/max_delay_us : 1;
        float max_supported_rate =
-               (min_delay_us && min_delay_us != -1) ? (1000000.0 / min_delay_us) : 0;
+               min_delay_us && min_delay_us != -1 ? 1000000.0/min_delay_us : 0;
        char freqs_buf[100];
        char* cursor;
        int n;
        float sr;
+       float cur_sampling_rate; /* Currently used sampling rate              */
+       float arb_sampling_rate; /* Granted sampling rate after arbitration   */
 
-       if (new_sampling_rate < min_supported_rate)
-               new_sampling_rate = min_supported_rate;
+       ALOGV("Sampling rate %g requested on sensor %d (%s)\n", requested_rate,
+             s, sensor[s].friendly_name);
 
-       if (max_supported_rate &&
-               new_sampling_rate > max_supported_rate) {
-               new_sampling_rate = max_supported_rate;
+       sensor[s].requested_rate = requested_rate;
+
+       arb_sampling_rate = requested_rate;
+
+       if (arb_sampling_rate < min_supported_rate) {
+               ALOGV("Sampling rate %g too low for %s, using %g instead\n",
+                      arb_sampling_rate, sensor[s].friendly_name,
+                      min_supported_rate);
+
+               arb_sampling_rate = min_supported_rate;
        }
 
-       sensor_info[s].sampling_rate = new_sampling_rate;
+       if (max_supported_rate && arb_sampling_rate > max_supported_rate) {
+               ALOGV("Sampling rate %g too high for %s, using %g instead\n",
+               arb_sampling_rate, sensor[s].friendly_name, max_supported_rate);
+               arb_sampling_rate = max_supported_rate;
+       }
+
+       sensor[s].sampling_rate = arb_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]);
+       if (!sensor[s].num_channels) {
+               if (is_enabled(s))
+                       /* Wake up thread so the new sampling rate gets used */
+                       pthread_cond_signal(&thread_release_cond[s]);
                return 0;
        }
 
@@ -742,11 +797,11 @@ static int setup_delay_sysfs(int s, float 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 &&
+                       if (n != s && sensor[n].dev_num == dev_num &&
+                           sensor[n].num_channels &&
                            is_enabled(s) &&
-                           sensor_info[n].sampling_rate > new_sampling_rate)
-                               new_sampling_rate= sensor_info[n].sampling_rate;
+                           sensor[n].sampling_rate > arb_sampling_rate)
+                               arb_sampling_rate = sensor[n].sampling_rate;
 
        /* Check if we have contraints on allowed sampling rates */
 
@@ -763,9 +818,15 @@ static int setup_delay_sysfs(int s, float new_sampling_rate)
                        /* Decode a single value */
                        sr = strtod(cursor, NULL);
 
-                       /* If this matches the selected rate, we're happy */
-                       if (new_sampling_rate == sr)
+                       /*
+                        * If this matches the selected rate, we're happy.
+                        * Have some tolerance to counter rounding errors and
+                        * avoid needless jumps to higher rates.
+                        */
+                       if (fabs(arb_sampling_rate - sr) <= 0.001) {
+                               arb_sampling_rate = sr;
                                break;
+                       }
 
                        /*
                         * If we reached a higher value than the desired rate,
@@ -774,8 +835,8 @@ static int setup_delay_sysfs(int s, float new_sampling_rate)
                         * assumption that rates are sorted by increasing value
                         * in the allowed frequencies string.
                         */
-                       if (sr > new_sampling_rate) {
-                               new_sampling_rate = sr;
+                       if (sr > arb_sampling_rate) {
+                               arb_sampling_rate = sr;
                                break;
                        }
 
@@ -790,20 +851,21 @@ static int setup_delay_sysfs(int s, float new_sampling_rate)
        }
 
        if (max_supported_rate &&
-               new_sampling_rate > max_supported_rate) {
-               new_sampling_rate = max_supported_rate;
+               arb_sampling_rate > max_supported_rate) {
+               arb_sampling_rate = max_supported_rate;
        }
 
        /* If the desired rate is already active we're all set */
-       if (new_sampling_rate == cur_sampling_rate)
+       if (arb_sampling_rate == cur_sampling_rate)
                return 0;
 
-       ALOGI("Sensor %d sampling rate set to %g\n", s, new_sampling_rate);
+       ALOGI("Sensor %d (%s) sampling rate set to %g\n",
+             s, sensor[s].friendly_name, arb_sampling_rate);
 
        if (trig_sensors_per_dev[dev_num])
                enable_buffer(dev_num, 0);
 
-       sysfs_write_float(sysfs_path, new_sampling_rate);
+       sysfs_write_float(sysfs_path, arb_sampling_rate);
 
        /* Check if it makes sense to use an alternate trigger */
        tentative_switch_trigger(s);
@@ -813,6 +875,8 @@ static int setup_delay_sysfs(int s, float new_sampling_rate)
 
        return 0;
 }
+
+
 /*
  * We go through all the virtual sensors of the base - and the base itself
  * in order to recompute the maximum requested delay of the group and setup the base
@@ -824,55 +888,59 @@ static int arbitrate_bases (int s)
 
        float arbitrated_rate = 0;
 
-       if (sensor_info[s].directly_enabled)
-               arbitrated_rate = sensor_info[s].requested_rate;
+       if (sensor[s].directly_enabled)
+               arbitrated_rate = sensor[s].requested_rate;
 
         for (i = 0; i < sensor_count; i++) {
-                       for (vidx = 0; vidx < sensor_info[i].base_count; vidx++)
+                       for (vidx = 0; vidx < sensor[i].base_count; vidx++)
                        /* If we have a virtual sensor depending on this one - handle it */
-                               if (sensor_info[i].base_idx[vidx] == s &&
-                                       sensor_info[i].directly_enabled &&
-                                       sensor_info[i].requested_rate > arbitrated_rate)
-                                               arbitrated_rate = sensor_info[i].requested_rate;
+                               if (sensor[i].base[vidx] == s &&
+                                       sensor[i].directly_enabled &&
+                                       sensor[i].requested_rate > arbitrated_rate)
+                                               arbitrated_rate = sensor[i].requested_rate;
                }
 
        return setup_delay_sysfs(s, arbitrated_rate);
 }
 
+
 /*
  * Re-assesment for delays. We need to re-asses delays for all related groups
  * of sensors everytime a sensor enables / disables / changes frequency.
-*/
+ */
 int arbitrate_delays (int s)
 {
        int i;
 
-       if (!sensor_info[s].is_virtual) {
+       if (!sensor[s].is_virtual) {
                return arbitrate_bases(s);
        }
        /* Is virtual sensor - go through bases */
-       for (i = 0; i < sensor_info[s].base_count; i++)
-               arbitrate_bases(sensor_info[s].base_idx[i]);
+       for (i = 0; i < sensor[s].base_count; i++)
+               arbitrate_bases(sensor[s].base[i]);
 
        return 0;
 }
-int sensor_activate(int s, int enabled, int from_virtual)
+
+
+int sensor_activate (int s, int enabled, int from_virtual)
 {
        char device_name[PATH_MAX];
        struct epoll_event ev = {0};
        int dev_fd;
        int ret;
-       int dev_num = sensor_info[s].dev_num;
-       int is_poll_sensor = !sensor_info[s].num_channels;
+       int dev_num = sensor[s].dev_num;
+       int is_poll_sensor = !sensor[s].num_channels;
 
-       if (sensor_info[s].is_virtual) {
+       if (sensor[s].is_virtual) {
                sensor_activate_virtual(s, enabled, from_virtual);
                arbitrate_delays(s);
                return 0;
        }
 
        /* Prepare the report timestamp field for the first event, see set_report_ts method */
-       sensor_info[s].report_ts = 0;
+       sensor[s].report_ts = 0;
+
        ret = adjust_counters(s, enabled, from_virtual);
 
        /* If the operation was neutral in terms of state, we're done */
@@ -881,10 +949,10 @@ int sensor_activate(int s, int enabled, int from_virtual)
 
        arbitrate_delays(s);
 
-       sensor_info[s].event_count = 0;
-       sensor_info[s].meta_data_pending = 0;
+       sensor[s].event_count = 0;
+       sensor[s].meta_data_pending = 0;
 
-       if (enabled && (sensor_info[s].quirks & QUIRK_NOISY))
+       if (enabled && (sensor[s].quirks & QUIRK_NOISY))
                /* Initialize filtering data if required */
                setup_noise_filtering(s);
 
@@ -898,7 +966,7 @@ int sensor_activate(int s, int enabled, int from_virtual)
                if (trig_sensors_per_dev[dev_num]) {
 
                        /* Start sampling */
-                       setup_trigger(s, sensor_info[s].init_trigger_name);
+                       setup_trigger(s, sensor[s].init_trigger_name);
                        enable_buffer(dev_num, 1);
                }
        }
@@ -968,7 +1036,7 @@ int sensor_activate(int s, int enabled, int from_virtual)
        }
 
        /* Ensure that on-change sensors send at least one event after enable */
-       sensor_info[s].prev_val = -1;
+       sensor[s].prev_val = -1;
 
        if (is_poll_sensor)
                start_acquisition_thread(s);
@@ -1007,24 +1075,24 @@ static void enable_motion_trigger (int dev_num)
        /* Check that all active sensors are ready to switch */
 
        for (s=0; s<MAX_SENSORS; s++)
-               if (sensor_info[s].dev_num == dev_num &&
+               if (sensor[s].dev_num == dev_num &&
                    is_enabled(s) &&
-                   sensor_info[s].num_channels &&
-                   (!sensor_info[s].motion_trigger_name[0] ||
-                    !sensor_info[s].report_initialized ||
+                   sensor[s].num_channels &&
+                   (!sensor[s].motion_trigger_name[0] ||
+                    !sensor[s].report_initialized ||
                     is_fast_accelerometer(s) ||
-                    (sensor_info[s].quirks & QUIRK_FORCE_CONTINUOUS))
+                    (sensor[s].quirks & QUIRK_FORCE_CONTINUOUS))
                    )
                        return; /* Nope */
 
        /* Record which particular sensors need to switch */
 
        for (s=0; s<MAX_SENSORS; s++)
-               if (sensor_info[s].dev_num == dev_num &&
+               if (sensor[s].dev_num == dev_num &&
                    is_enabled(s) &&
-                   sensor_info[s].num_channels &&
-                   sensor_info[s].selected_trigger !=
-                       sensor_info[s].motion_trigger_name)
+                   sensor[s].num_channels &&
+                   sensor[s].selected_trigger !=
+                       sensor[s].motion_trigger_name)
                                candidate[candidate_count++] = s;
 
        if (!candidate_count)
@@ -1036,13 +1104,15 @@ static void enable_motion_trigger (int dev_num)
 
        for (i=0; i<candidate_count; i++) {
                s = candidate[i];
-               setup_trigger(s, sensor_info[s].motion_trigger_name);
+               setup_trigger(s, sensor[s].motion_trigger_name);
        }
 
        enable_buffer(dev_num, 1);
 }
 
-/* CTS acceptable thresholds:
+
+/*
+ *  CTS acceptable thresholds:
  *     EventGapVerification.java: (th <= 1.8)
  *     FrequencyVerification.java: (0.9)*(expected freq) => (th <= 1.1111)
  */
@@ -1060,17 +1130,17 @@ void set_report_ts(int s, int64_t ts)
        *  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 &&
+       if (sensor[s].report_ts && sensor[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 + THRESHOLD * period;
+               period = (int64_t) (1000000000LL / sensor[s].sampling_rate);
+               maxTs = sensor[s].report_ts + THRESHOLD * period;
                /* If we're too far behind get back on track */
                if (ts - maxTs >= MAX_DELAY)
                        maxTs = ts;
-               sensor_info[s].report_ts = (ts < maxTs ? ts : maxTs);
+               sensor[s].report_ts = (ts < maxTs ? ts : maxTs);
        } else {
-               sensor_info[s].report_ts = ts;
+               sensor[s].report_ts = ts;
        }
 }
 
@@ -1080,7 +1150,7 @@ static void stamp_reports (int dev_num, int64_t ts)
        int s;
 
        for (s=0; s<MAX_SENSORS; s++)
-                       if (sensor_info[s].dev_num == dev_num &&
+                       if (sensor[s].dev_num == dev_num &&
                                is_enabled(s))
                                        set_report_ts(s, ts);
 }
@@ -1124,20 +1194,20 @@ static int integrate_device_report (int 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[s].dev_num == dev_num &&
                    is_enabled(s)) {
 
                        sr_offset = 0;
 
                        /* Copy data from device to sensor report buffer */
-                       for (c=0; c<sensor_info[s].num_channels; c++) {
+                       for (c=0; c<sensor[s].num_channels; c++) {
 
-                               target = sensor_info[s].report_buffer +
+                               target = sensor[s].report_buffer +
                                        sr_offset;
 
-                               source = buf + sensor_info[s].channel[c].offset;
+                               source = buf + sensor[s].channel[c].offset;
 
-                               size = sensor_info[s].channel[c].size;
+                               size = sensor[s].channel[c].size;
 
                                memcpy(target, source, size);
 
@@ -1147,8 +1217,8 @@ static int integrate_device_report (int dev_num)
                        ALOGV("Sensor %d report available (%d bytes)\n", s,
                              sr_offset);
 
-                       sensor_info[s].report_pending = DATA_TRIGGER;
-                       sensor_info[s].report_initialized = 1;
+                       sensor[s].report_pending = DATA_TRIGGER;
+                       sensor[s].report_initialized = 1;
 
                        ts_offset += sr_offset;
                }
@@ -1164,10 +1234,10 @@ static int integrate_device_report (int dev_num)
 
        /* Don't trust the timestamp channel in any-motion mode */
        for (s=0; s<MAX_SENSORS; s++)
-               if (sensor_info[s].dev_num == dev_num &&
+               if (sensor[s].dev_num == dev_num &&
                    is_enabled(s) &&
-                   sensor_info[s].selected_trigger ==
-                                       sensor_info[s].motion_trigger_name) {
+                   sensor[s].selected_trigger ==
+                                       sensor[s].motion_trigger_name) {
                stamp_reports(dev_num, get_timestamp_boot());
                return 0;
        }
@@ -1194,17 +1264,19 @@ static int integrate_device_report (int dev_num)
        return 0;
 }
 
+
 static int propagate_vsensor_report (int s, struct sensors_event_t  *data)
 {
-       /* There's a new report stored in sensor_info.sample for this sensor; transmit it */
+       /* There's a new report stored in sensor.sample for this sensor; transmit it */
 
-       memcpy(data, &sensor_info[s].sample, sizeof(struct sensors_event_t));
+       memcpy(data, &sensor[s].sample, sizeof(struct sensors_event_t));
 
        data->sensor    = s;
-       data->type      = sensor_info[s].type;
+       data->type      = sensor[s].type;
        return 1;
 }
 
+
 static int propagate_sensor_report (int s, struct sensors_event_t  *data)
 {
        /* There's a sensor report pending for this sensor ; transmit it */
@@ -1221,15 +1293,15 @@ static int propagate_sensor_report (int s, struct sensors_event_t  *data)
 
        data->version   = sizeof(sensors_event_t);
        data->sensor    = s;
-       data->type      = sensor_info[s].type;
-       data->timestamp = sensor_info[s].report_ts;
+       data->type      = sensor[s].type;
+       data->timestamp = sensor[s].report_ts;
 
-       ALOGV("Sample on sensor %d (type %d):\n", s, sensor_info[s].type);
+       ALOGV("Sample on sensor %d (type %d):\n", s, sensor[s].type);
 
-       current_sample = sensor_info[s].report_buffer;
+       current_sample = sensor[s].report_buffer;
 
        /* If this is a poll sensor */
-       if (!sensor_info[s].num_channels) {
+       if (!sensor[s].num_channels) {
                /* 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));
@@ -1239,18 +1311,18 @@ static int propagate_sensor_report (int s, struct sensors_event_t  *data)
        /* Convert the data into the expected Android-level format */
        for (c=0; c<num_fields; c++) {
 
-               data->data[c] = sensor_info[s].ops.transform
+               data->data[c] = sensor[s].ops.transform
                                                        (s, c, current_sample);
 
                ALOGV("\tfield %d: %f\n", c, data->data[c]);
-               current_sample += sensor_info[s].channel[c].size;
+               current_sample += sensor[s].channel[c].size;
        }
 
        /*
         * 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);
+       return sensor[s].ops.finalize(s, data);
 }
 
 
@@ -1276,31 +1348,31 @@ static void synthetize_duplicate_samples (void)
                        continue;
 
                /* If the sensor is continuously firing, leave it alone */
-               if (sensor_info[s].selected_trigger !=
-                   sensor_info[s].motion_trigger_name)
+               if (sensor[s].selected_trigger !=
+                   sensor[s].motion_trigger_name)
                        continue;
 
                /* If we haven't seen a sample, there's nothing to duplicate */
-               if (!sensor_info[s].report_initialized)
+               if (!sensor[s].report_initialized)
                        continue;
 
                /* If a sample was recently buffered, leave it alone too */
-               if (sensor_info[s].report_pending)
+               if (sensor[s].report_pending)
                        continue;
 
                /* We also need a valid sampling rate to be configured */
-               if (!sensor_info[s].sampling_rate)
+               if (!sensor[s].sampling_rate)
                        continue;
 
-               period = (int64_t) (1000000000.0/ sensor_info[s].sampling_rate);
+               period = (int64_t) (1000000000.0/ sensor[s].sampling_rate);
 
                current_ts = get_timestamp_boot();
-               target_ts = sensor_info[s].report_ts + period;
+               target_ts = sensor[s].report_ts + period;
 
                if (target_ts <= current_ts) {
                        /* Mark the sensor for event generation */
                        set_report_ts(s, current_ts);
-                       sensor_info[s].report_pending = DATA_DUPLICATE;
+                       sensor[s].report_pending = DATA_DUPLICATE;
                }
        }
 }
@@ -1316,17 +1388,17 @@ static void integrate_thread_report (uint32_t tag)
 
        expected_len = sizeof(int64_t) + get_field_count(s) * sizeof(float);
 
-       len = read(sensor_info[s].thread_data_fd[0],
+       len = read(sensor[s].thread_data_fd[0],
                   current_sample,
                   expected_len);
 
        memcpy(&timestamp, current_sample, sizeof(int64_t));
-       memcpy(sensor_info[s].report_buffer, sizeof(int64_t) + current_sample,
+       memcpy(sensor[s].report_buffer, sizeof(int64_t) + current_sample,
                        expected_len - sizeof(int64_t));
 
        if (len == expected_len) {
                set_report_ts(s, timestamp);
-               sensor_info[s].report_pending = DATA_SYSFS;
+               sensor[s].report_pending = DATA_SYSFS;
        }
 }
 
@@ -1352,14 +1424,14 @@ static int get_poll_wait_timeout (void)
         */
        for (s=0; s<sensor_count; s++)
                if (is_enabled(s) &&
-                   sensor_info[s].selected_trigger ==
-                   sensor_info[s].motion_trigger_name &&
-                   sensor_info[s].sampling_rate) {
+                   sensor[s].selected_trigger ==
+                   sensor[s].motion_trigger_name &&
+                   sensor[s].sampling_rate) {
                        period = (int64_t) (1000000000.0 /
-                                               sensor_info[s].sampling_rate);
+                                               sensor[s].sampling_rate);
 
-                       if (sensor_info[s].report_ts + period < target_ts)
-                               target_ts = sensor_info[s].report_ts + period;
+                       if (sensor[s].report_ts + period < target_ts)
+                               target_ts = sensor[s].report_ts + period;
                }
 
        /* If we don't have such a driver to deal with */
@@ -1376,7 +1448,7 @@ static int get_poll_wait_timeout (void)
 }
 
 
-int sensor_poll(struct sensors_event_t* data, int count)
+int sensor_poll (struct sensors_event_t* data, int count)
 {
        int s;
        int i;
@@ -1395,10 +1467,10 @@ return_available_sensor_reports:
        returned_events = 0;
        /* Check our sensor collection for available reports */
        for (s=0; s<sensor_count && returned_events < count; s++) {
-               if (sensor_info[s].report_pending) {
+               if (sensor[s].report_pending) {
                        event_count = 0;
 
-                       if (sensor_info[s].is_virtual)
+                       if (sensor[s].is_virtual)
                                event_count = propagate_vsensor_report(s, &data[returned_events]);
                        else {
                                /* Report this event if it looks OK */
@@ -1406,7 +1478,7 @@ return_available_sensor_reports:
                        }
 
                        /* Lower flag */
-                       sensor_info[s].report_pending = 0;
+                       sensor[s].report_pending = 0;
                        returned_events += event_count;
                        /*
                         * If the sample was deemed invalid or unreportable,
@@ -1414,7 +1486,7 @@ return_available_sensor_reports:
                         * value for a 'on change' sensor, silently drop it.
                         */
                }
-               while (sensor_info[s].meta_data_pending) {
+               while (sensor[s].meta_data_pending) {
                        /* See sensors.h on these */
                        data[returned_events].version = META_DATA_VERSION;
                        data[returned_events].sensor = 0;
@@ -1424,7 +1496,7 @@ return_available_sensor_reports:
                        data[returned_events].meta_data.sensor = s;
                        data[returned_events].meta_data.what = META_DATA_FLUSH_COMPLETE;
                        returned_events++;
-                       sensor_info[s].meta_data_pending--;
+                       sensor[s].meta_data_pending--;
                }
        }
        if (returned_events)
@@ -1466,36 +1538,39 @@ await_event:
        goto return_available_sensor_reports;
 }
 
-int sensor_set_delay(int s, int64_t ns)
+
+int sensor_set_delay (int s, int64_t ns)
 {
-       float new_sampling_rate; /* Granted sampling rate after arbitration   */
+       float requested_sampling_rate;
 
        if (ns <= 0) {
-               ALOGE("Rejecting non-positive delay request on sensor %d,required delay: %lld\n", s, ns);
+               ALOGE("Invalid delay requested on sensor %d: %lld\n", s, ns);
                return -EINVAL;
        }
 
-       new_sampling_rate = 1000000000LL/ns;
+       requested_sampling_rate = 1000000000.0/ns;
 
-       ALOGV("Entering set delay S%d (%s): old rate(%f), new rate(%f)\n",
-               s, sensor_info[s].friendly_name, sensor_info[s].sampling_rate,
-               new_sampling_rate);
+       ALOGV("Entering set delay S%d (%s): current rate: %f, requested: %f\n",
+               s, sensor[s].friendly_name, sensor[s].sampling_rate,
+               requested_sampling_rate);
 
-       sensor_info[s].requested_rate = new_sampling_rate;
+       sensor[s].requested_rate = requested_sampling_rate;
 
        return arbitrate_delays(s);
 }
 
+
 int sensor_flush (int s)
 {
        /* If one shot or not enabled return -EINVAL */
        if (sensor_desc[s].flags & SENSOR_FLAG_ONE_SHOT_MODE || !is_enabled(s))
                return -EINVAL;
 
-       sensor_info[s].meta_data_pending++;
+       sensor[s].meta_data_pending++;
        return 0;
 }
 
+
 int allocate_control_data (void)
 {
        int i;