OSDN Git Service

STPK-1429 Slight improvement to timestamp accuracy
[android-x86/hardware-intel-libsensors.git] / control.c
1 /*
2  * Copyright (C) 2014 Intel Corporation.
3  */
4
5 #include <stdlib.h>
6 #include <ctype.h>
7 #include <fcntl.h>
8 #include <sys/epoll.h>
9 #include <sys/socket.h>
10 #include <utils/Log.h>
11 #include <hardware/sensors.h>
12 #include "control.h"
13 #include "enumeration.h"
14 #include "utils.h"
15 #include "transform.h"
16 #include "calibration.h"
17
18 /* Currently active sensors count, per device */
19 static int poll_sensors_per_dev[MAX_DEVICES];   /* poll-mode sensors */
20 static int trig_sensors_per_dev[MAX_DEVICES];   /* trigger, event based */
21
22 static int device_fd[MAX_DEVICES];   /* fd on the /dev/iio:deviceX file */
23
24 static int poll_fd; /* epoll instance covering all enabled sensors */
25
26 static int poll_socket_pair[2]; /* used to unblock the poll loop */
27
28 /* Timestamp for the moment when we last exited a poll operation */
29 static int64_t last_poll_exit_ts;
30
31 static int active_poll_sensors; /* Number of enabled poll-mode sensors */
32
33 #define INVALID_DEV_NUM ((uint32_t) -1)
34
35
36 static int enable_buffer(int dev_num, int enabled)
37 {
38         char sysfs_path[PATH_MAX];
39
40         sprintf(sysfs_path, ENABLE_PATH, dev_num);
41
42         /* Low level, non-multiplexed, enable/disable routine */
43         return sysfs_write_int(sysfs_path, enabled);
44 }
45
46
47 static int setup_trigger(int dev_num, const char* trigger_val)
48 {
49         char sysfs_path[PATH_MAX];
50
51         sprintf(sysfs_path, TRIGGER_PATH, dev_num);
52
53         return sysfs_write_str(sysfs_path, trigger_val);
54 }
55
56
57 void build_sensor_report_maps(int dev_num)
58 {
59         /*
60          * Read sysfs files from a iio device's scan_element directory, and
61          * build a couple of tables from that data. These tables will tell, for
62          * each sensor, where to gather relevant data in a device report, i.e.
63          * the structure that we read from the /dev/iio:deviceX file in order to
64          * sensor report, itself being the data that we return to Android when a
65          * sensor poll completes. The mapping should be straightforward in the
66          * case where we have a single sensor active per iio device but, this is
67          * not the general case. In general several sensors can be handled
68          * through a single iio device, and the _en, _index and _type syfs
69          * entries all concur to paint a picture of what the structure of the
70          * device report is.
71          */
72
73         int s;
74         int c;
75         int n;
76         int i;
77         int ch_index;
78         char* ch_spec;
79         char spec_buf[MAX_TYPE_SPEC_LEN];
80         struct datum_info_t* ch_info;
81         int size;
82         char sysfs_path[PATH_MAX];
83         int known_channels;
84         int offset;
85         int channel_size_from_index[MAX_SENSORS * MAX_CHANNELS] = { 0 };
86         int sensor_handle_from_index[MAX_SENSORS * MAX_CHANNELS] = { 0 };
87         int channel_number_from_index[MAX_SENSORS * MAX_CHANNELS] = { 0 };
88
89         known_channels = 0;
90
91         /* For each sensor that is linked to this device */
92         for (s=0; s<sensor_count; s++) {
93                 if (sensor_info[s].dev_num != dev_num)
94                         continue;
95
96                 i = sensor_info[s].catalog_index;
97
98                 /* Read channel details through sysfs attributes */
99                 for (c=0; c<sensor_info[s].num_channels; c++) {
100
101                         /* Read _type file */
102                         sprintf(sysfs_path, CHANNEL_PATH "%s",
103                                 sensor_info[s].dev_num,
104                                 sensor_catalog[i].channel[c].type_path);
105
106                         n = sysfs_read_str(sysfs_path, spec_buf, 
107                                                 sizeof(spec_buf));
108
109                         if (n == -1) {
110                                         ALOGW(  "Failed to read type: %s\n",
111                                         sysfs_path);
112                                         continue;
113                                 }
114
115                         ch_spec = sensor_info[s].channel[c].type_spec;
116
117                         memcpy(ch_spec, spec_buf, sizeof(spec_buf));
118
119                         ch_info = &sensor_info[s].channel[c].type_info;
120
121                         size = decode_type_spec(ch_spec, ch_info);
122
123                         /* Read _index file */
124                         sprintf(sysfs_path, CHANNEL_PATH "%s",
125                                 sensor_info[s].dev_num,
126                                 sensor_catalog[i].channel[c].index_path);
127
128                         n = sysfs_read_int(sysfs_path, &ch_index);
129
130                         if (n == -1) {
131                                         ALOGW(  "Failed to read index: %s\n",
132                                                 sysfs_path);
133                                         continue;
134                                 }
135
136                         if (ch_index >= MAX_SENSORS) {
137                                 ALOGE("Index out of bounds!: %s\n", sysfs_path);
138                                 continue;
139                         }
140
141                         /* Record what this index is about */
142
143                         sensor_handle_from_index [ch_index] = s;
144                         channel_number_from_index[ch_index] = c;
145                         channel_size_from_index  [ch_index] = size;
146
147                         known_channels++;
148                 }
149
150                 /* Turn on channels we're aware of */
151                 for (c=0;c<sensor_info[s].num_channels; c++) {
152                         sprintf(sysfs_path, CHANNEL_PATH "%s",
153                                 sensor_info[s].dev_num,
154                                 sensor_catalog[i].channel[c].en_path);
155
156                         sysfs_write_int(sysfs_path, 1);
157                 }
158         }
159
160         ALOGI("Found %d channels on iio device %d\n", known_channels, dev_num);
161
162         /*
163          * Now that we know which channels are defined, their sizes and their
164          * ordering, update channels offsets within device report. Note: there
165          * is a possibility that several sensors share the same index, with
166          * their data fields being isolated by masking and shifting as specified
167          * through the real bits and shift values in type attributes. This case
168          * is not currently supported. Also, the code below assumes no hole in
169          * the sequence of indices, so it is dependent on discovery of all
170          * sensors.
171          */
172          offset = 0;
173          for (i=0; i<MAX_SENSORS * MAX_CHANNELS; i++) {
174                 s =     sensor_handle_from_index[i];
175                 c =     channel_number_from_index[i];
176                 size =  channel_size_from_index[i];
177
178                 if (!size)
179                         continue;
180
181                 ALOGI("S%d C%d : offset %d, size %d, type %s\n",
182                       s, c, offset, size, sensor_info[s].channel[c].type_spec);
183
184                 sensor_info[s].channel[c].offset        = offset;
185                 sensor_info[s].channel[c].size          = size;
186
187                 offset += size;
188          }
189 }
190
191
192 int adjust_counters (int s, int enabled)
193 {
194         /*
195          * Adjust counters based on sensor enable action. Return values are:
196          * -1 if there's an inconsistency: abort action in this case
197          *  0 if the operation was completed and we're all set
198          *  1 if we toggled the state of the sensor and there's work left
199          */
200
201         int dev_num = sensor_info[s].dev_num;
202         int catalog_index = sensor_info[s].catalog_index;
203         int sensor_type = sensor_catalog[catalog_index].type;
204
205         /* Refcount per sensor, in terms of enable count */
206         if (enabled) {
207                 ALOGI("Enabling sensor %d (iio device %d: %s)\n",
208                         s, dev_num, sensor_info[s].friendly_name);
209
210                 sensor_info[s].enable_count++;
211
212                 if (sensor_info[s].enable_count != 1) {
213                         return 0; /* The sensor was, and remains, in use */
214                 } else {
215                         if (sensor_type == SENSOR_TYPE_MAGNETIC_FIELD)
216                                 compass_read_data(COMPASS_CALIBRATION_PATH);
217                 }
218         } else {
219                 if (sensor_info[s].enable_count == 0)
220                         return -1; /* Spurious disable call */
221
222                 ALOGI("Disabling sensor %d (iio device %d: %s)\n", s, dev_num,
223                       sensor_info[s].friendly_name);
224
225                 if (sensor_type == SENSOR_TYPE_MAGNETIC_FIELD)
226                         compass_store_data(COMPASS_CALIBRATION_PATH);
227
228                 sensor_info[s].enable_count--;
229
230                 if (sensor_info[s].enable_count > 0)
231                         return 0; /* The sensor was, and remains, in use */
232
233                 /* Sensor disabled, lower report available flag */
234                 sensor_info[s].report_pending = 0;
235         }
236
237         /* We changed the state of a sensor - adjust per iio device counters */
238
239         /* If this is a regular event-driven sensor */
240         if (sensor_info[s].num_channels) {
241
242                         if (enabled)
243                                 trig_sensors_per_dev[dev_num]++;
244                         else
245                                 trig_sensors_per_dev[dev_num]--;
246
247                         return 1;
248                 }
249
250         if (enabled) {
251                 active_poll_sensors++;
252                 poll_sensors_per_dev[dev_num]++;
253                 return 1;
254         }
255
256         active_poll_sensors--;
257         poll_sensors_per_dev[dev_num]--;
258         return 1;
259 }
260
261
262 int sensor_activate(int s, int enabled)
263 {
264         char device_name[PATH_MAX];
265         char trigger_name[MAX_NAME_SIZE + 16];
266         int c;
267         struct epoll_event ev = {0};
268         int dev_fd;
269         int ret;
270         int dev_num = sensor_info[s].dev_num;
271         int i = sensor_info[s].catalog_index;
272         int is_poll_sensor = !sensor_info[s].num_channels;
273
274         ret = adjust_counters(s, enabled);
275
276         /* If the operation was neutral in terms of state, we're done */
277         if (ret <= 0)
278                 return ret;
279
280         if (!is_poll_sensor) {
281
282                 /* Stop sampling */
283                 enable_buffer(dev_num, 0);
284                 setup_trigger(dev_num, "\n");
285
286                 /* If there's at least one sensor enabled on this iio device */
287                 if (trig_sensors_per_dev[dev_num]) {
288                         sprintf(trigger_name, "%s-dev%d",
289                                         sensor_info[s].internal_name, dev_num);
290
291                         /* Start sampling */
292                         setup_trigger(dev_num, trigger_name);
293                         enable_buffer(dev_num, 1);
294                 }
295         }
296
297         /*
298          * Make sure we have a fd on the character device ; conversely, close
299          * the fd if no one is using associated sensor anymore. The assumption
300          * here is that the underlying driver will power on the relevant
301          * hardware block while someone hold a fd on the device.
302          */
303         dev_fd = device_fd[dev_num];
304
305         if (!enabled) {
306                 if (dev_fd != -1 && !poll_sensors_per_dev[dev_num] &&
307                         !trig_sensors_per_dev[dev_num]) {
308                                 /*
309                                  * Stop watching this fd. This should be a no-op
310                                  * in case this fd was not in the poll set.
311                                  */
312                                 epoll_ctl(poll_fd, EPOLL_CTL_DEL, dev_fd, NULL);
313
314                                 close(dev_fd);
315                                 device_fd[dev_num] = -1;
316                         }
317                 return 0;
318         }
319
320         if (dev_fd == -1) {
321                 /* First enabled sensor on this iio device */
322                 sprintf(device_name, DEV_FILE_PATH, dev_num);
323                 dev_fd = open(device_name, O_RDONLY | O_NONBLOCK);
324
325                 device_fd[dev_num] = dev_fd;
326
327                 if (dev_fd == -1) {
328                         ALOGE("Could not open fd on %s (%s)\n",
329                               device_name, strerror(errno));
330                         adjust_counters(s, 0);
331                         return -1;
332                 }
333
334                 ALOGV("Opened %s: fd=%d\n", device_name, dev_fd);
335
336                 if (!is_poll_sensor) {
337
338                         /* Add this iio device fd to the set of watched fds */
339                         ev.events = EPOLLIN;
340                         ev.data.u32 = dev_num;
341
342                         ret = epoll_ctl(poll_fd, EPOLL_CTL_ADD, dev_fd, &ev);
343
344                         if (ret == -1) {
345                                 ALOGE(  "Failed adding %d to poll set (%s)\n",
346                                         dev_fd, strerror(errno));
347                                 return -1;
348                         }
349
350                         /* Note: poll-mode fds are not readable */
351                 }
352         }
353
354         /* Release the polling loop so an updated timeout gets used */
355         write(poll_socket_pair[1], "", 1);
356
357         return 0;
358 }
359
360
361 static int integrate_device_report(int dev_num)
362 {
363         int len;
364         int s,c;
365         unsigned char buf[MAX_SENSOR_REPORT_SIZE * MAX_SENSORS] = { 0 };
366         int sr_offset;
367         unsigned char *target;
368         unsigned char *source;
369         int size;
370         int expected_size = 0;
371         int ts;
372
373         /* There's an incoming report on the specified fd */
374
375         if (dev_num < 0 || dev_num >= MAX_DEVICES) {
376                 ALOGE("Event reported on unexpected iio device %d\n", dev_num);
377                 return -1;
378         }
379
380         if (device_fd[dev_num] == -1) {
381                 ALOGE("Ignoring stale report on iio device %d\n", dev_num);
382                 return -1;
383         }
384
385         for (s=0; s<MAX_SENSORS; s++)
386                 if (sensor_info[s].dev_num == dev_num)
387                         for (c=0; c<sensor_info[s].num_channels; c++)
388                                 expected_size += sensor_info[s].channel[c].size;
389
390         ts = get_timestamp();
391
392         len = read(device_fd[dev_num], buf, expected_size);
393
394         if (len == -1) {
395                 ALOGE("Could not read report from iio device %d (%s)\n",
396                       dev_num, strerror(errno));
397                 return -1;
398         }
399
400         ALOGV("Read %d bytes from iio device %d\n", len, dev_num);
401
402         for (s=0; s<MAX_SENSORS; s++)
403                 if (sensor_info[s].dev_num == dev_num &&
404                     sensor_info[s].enable_count) {
405
406                         sr_offset = 0;
407
408                         /* Copy data from device to sensor report buffer */
409                         for (c=0; c<sensor_info[s].num_channels; c++) {
410
411                                 target = sensor_info[s].report_buffer +
412                                         sr_offset;
413
414                                 source = buf + sensor_info[s].channel[c].offset;
415
416                                 size = sensor_info[s].channel[c].size;
417
418                                 memcpy(target, source, size);
419
420                                 sr_offset += size;
421                         }
422
423                         ALOGV("Sensor %d report available (%d bytes)\n", s,
424                               sr_offset);
425
426                         sensor_info[s].report_ts = ts;
427                         sensor_info[s].report_pending = 1;
428                 }
429
430         return 0;
431 }
432
433
434 static void propagate_sensor_report(int s, struct sensors_event_t* data)
435 {
436         /* There's a sensor report pending for this sensor ; transmit it */
437
438         int catalog_index = sensor_info[s].catalog_index;
439         int sensor_type = sensor_catalog[catalog_index].type;
440         int num_fields;
441         int c;
442         unsigned char* current_sample;
443         int64_t current_ts = get_timestamp();
444
445         memset(data, 0, sizeof(sensors_event_t));
446
447         data->version = sizeof(sensors_event_t);
448         data->sensor = s;
449         data->type = sensor_type;
450
451         if (sensor_info[s].report_ts)
452                 data->timestamp = sensor_info[s].report_ts;
453         else
454                 data->timestamp = current_ts;
455
456         switch (sensor_type) {
457                 case SENSOR_TYPE_ACCELEROMETER:         /* m/s^2        */
458                 case SENSOR_TYPE_MAGNETIC_FIELD:        /* micro-tesla  */
459                 case SENSOR_TYPE_ORIENTATION:           /* degrees      */
460                 case SENSOR_TYPE_GYROSCOPE:             /* radians/s    */
461                         num_fields = 3;
462                         break;
463
464                 case SENSOR_TYPE_LIGHT:                 /* SI lux units */
465                 case SENSOR_TYPE_AMBIENT_TEMPERATURE:   /* Â°C          */
466                 case SENSOR_TYPE_TEMPERATURE:           /* Â°C          */
467                 case SENSOR_TYPE_PROXIMITY:             /* centimeters  */
468                 case SENSOR_TYPE_PRESSURE:              /* hecto-pascal */
469                 case SENSOR_TYPE_RELATIVE_HUMIDITY:     /* percent */
470                         num_fields = 1;
471                         break;
472
473                 case SENSOR_TYPE_ROTATION_VECTOR:
474                         num_fields = 4;
475                         break;
476
477                 case SENSOR_TYPE_DEVICE_PRIVATE_BASE:   /* hidden for now */
478                         num_fields = 0;
479                         break;
480
481                 default:
482                         ALOGE("Unknown sensor type!\n");
483                         num_fields = 0;
484                         break;
485         }
486
487         ALOGV("Sample on sensor %d (type %d):\n", s, sensor_type);
488
489         /* Take note of current time counter value for rate control purposes */
490         sensor_info[s].last_integration_ts = current_ts;
491
492         /* If we're dealing with a poll-mode sensor */
493         if (!sensor_info[s].num_channels) {
494
495                 /* Read values through sysfs rather than from a report buffer */
496                 for (c=0; c<num_fields; c++) {
497
498                         data->data[c] = acquire_immediate_value(s, c);
499
500                         ALOGV("\tfield %d: %f\n", c, data->data[c]);
501                 }
502
503                 sensor_info[s].ops.finalize(s, data);
504                 return;
505         }
506
507         /* Convert the data into the expected Android-level format */
508
509         current_sample = sensor_info[s].report_buffer;
510
511         for (c=0; c<num_fields; c++) {
512
513                 data->data[c] = sensor_info[s].ops.transform
514                                                         (s, c, current_sample);
515
516                 ALOGV("\tfield %d: %f\n", c, data->data[c]);
517                 current_sample += sensor_info[s].channel[c].size;
518         }
519
520         sensor_info[s].ops.finalize(s, data);
521 }
522
523
524 static int get_poll_time (void)
525 {
526         int64_t target_ts;
527         int64_t lowest_target_ts;
528         int64_t current_ts;
529         int s;
530
531         if (!active_poll_sensors)
532                 return -1;      /* Infinite wait */
533
534         /* Check if we should schedule a poll-mode sensor event delivery */
535
536         lowest_target_ts = INT64_MAX;
537
538         for (s=0; s<sensor_count; s++)
539                 if (sensor_info[s].enable_count &&
540                     sensor_info[s].sampling_rate &&
541                     !sensor_info[s].num_channels) {
542                                 target_ts = sensor_info[s].last_integration_ts +
543                                       1000000000LL/sensor_info[s].sampling_rate;
544
545                                 if (target_ts < lowest_target_ts)
546                                         lowest_target_ts = target_ts;
547                         }
548
549         if (lowest_target_ts == INT64_MAX)
550                 return -1;
551
552         current_ts = get_timestamp();
553
554         if (lowest_target_ts <= current_ts)
555                 return 0;
556
557         return (lowest_target_ts - current_ts)/1000000; /* ms */
558 }
559
560
561 static void acknowledge_release (void)
562 {
563         /* A write to our socket circuit was performed to release epoll */
564         char buf;
565         read(poll_socket_pair[0], &buf, 1);
566 }
567
568
569 int sensor_poll(struct sensors_event_t* data, int count)
570 {
571         int s;
572         int i;
573         int nfds;
574         int delta;
575         struct epoll_event ev[MAX_DEVICES];
576         int64_t target_ts;
577
578         /* Get one or more events from our collection of sensors */
579
580 return_first_available_sensor_report:
581
582         /* If there's at least one available report */
583         for (s=0; s<sensor_count; s++)
584                 if (sensor_info[s].report_pending) {
585
586                         /* Return that up */
587                         propagate_sensor_report(s, data);
588                         sensor_info[s].report_pending = 0;
589                         ALOGV("Report on sensor %d\n", s);
590                         return 1;
591                 }
592 await_event:
593
594         ALOGV("Awaiting sensor data\n");
595
596         nfds = epoll_wait(poll_fd, ev, MAX_DEVICES, get_poll_time());
597
598         last_poll_exit_ts = get_timestamp();
599
600         if (nfds == -1) {
601                 ALOGI("epoll_wait returned -1 (%s)\n", strerror(errno));
602                 goto await_event;
603         }
604
605         ALOGV("%d fds signalled\n", nfds);
606
607         /* For each of the devices for which a report is available */
608         for (i=0; i<nfds; i++)
609                 if (ev[i].events == EPOLLIN) {
610                         if (ev[i].data.u32 == INVALID_DEV_NUM) {
611                                 acknowledge_release();
612                                 goto await_event;
613                         } else
614                                 /* Read report */
615                                 integrate_device_report(ev[i].data.u32);
616                 }
617
618         /* Check poll-mode sensors and fire up an event if it's time to do so */
619         if (active_poll_sensors)
620                 for (s=0; s<sensor_count; s++)
621                         if (sensor_info[s].enable_count &&
622                             !sensor_info[s].num_channels &&
623                             sensor_info[s].sampling_rate) {
624                                 target_ts = sensor_info[s].last_integration_ts +
625                                       1000000000LL/sensor_info[s].sampling_rate;
626
627                                 if (last_poll_exit_ts >= target_ts)
628                                         sensor_info[s].report_pending = 1;
629                         }
630
631         goto return_first_available_sensor_report;
632 }
633
634
635 int sensor_set_delay(int s, int64_t ns)
636 {
637         /* Set the rate at which a specific sensor should report events */
638
639         /* See Android sensors.h for indication on sensor trigger modes */
640
641         char sysfs_path[PATH_MAX];
642         char avail_sysfs_path[PATH_MAX];
643         int dev_num             =       sensor_info[s].dev_num;
644         int i                   =       sensor_info[s].catalog_index;
645         const char *prefix      =       sensor_catalog[i].tag;
646         int new_sampling_rate;  /* Granted sampling rate after arbitration   */
647         int cur_sampling_rate;  /* Currently used sampling rate              */
648         int req_sampling_rate;  /* Requested ; may be different from granted */
649         int per_sensor_sampling_rate;
650         int per_device_sampling_rate;
651         int max_supported_rate = 0;
652         int limit;
653         char freqs_buf[100];
654         char* cursor;
655         int n;
656
657         if (!ns) {
658                 ALOGE("Rejecting zero delay request on sensor %d\n", s);
659                 return -EINVAL;
660         }
661
662         new_sampling_rate = req_sampling_rate = (int) (1000000000L/ns);
663
664         if (!new_sampling_rate) {
665                 ALOGI("Sub-HZ sampling rate requested on on sensor %d\n", s);
666                 new_sampling_rate = 1;
667         }
668
669         sensor_info[s].sampling_rate = new_sampling_rate;
670
671         /* If we're dealing with a poll-mode sensor, release poll and return */
672         if (!sensor_info[s].num_channels)
673                 goto exit;
674
675         sprintf(sysfs_path, SENSOR_SAMPLING_PATH, dev_num, prefix);
676
677         if (sysfs_read_int(sysfs_path, &cur_sampling_rate) != -1) {
678                 per_sensor_sampling_rate = 1;
679                 per_device_sampling_rate = 0;
680         } else {
681                 per_sensor_sampling_rate = 0;
682
683                 sprintf(sysfs_path, DEVICE_SAMPLING_PATH, dev_num);
684
685                 if (sysfs_read_int(sysfs_path, &cur_sampling_rate) != -1)
686                         per_device_sampling_rate = 1;
687                 else
688                         per_device_sampling_rate = 0;
689         }
690
691         if (!per_sensor_sampling_rate && !per_device_sampling_rate) {
692                 ALOGE("No way to adjust sampling rate on sensor %d\n", s);
693                 return -ENOSYS;
694         }
695
696         /* Coordinate with others active sensors on the same device, if any */
697         if (per_device_sampling_rate)
698                 for (n=0; n<sensor_count; n++)
699                         if (n != s && sensor_info[n].dev_num == dev_num &&
700                             sensor_info[n].num_channels &&
701                             sensor_info[n].enable_count &&
702                             sensor_info[n].sampling_rate > new_sampling_rate)
703                                 new_sampling_rate= sensor_info[n].sampling_rate;
704
705         /* Check if we have contraints on allowed sampling rates */
706
707         sprintf(avail_sysfs_path, DEVICE_AVAIL_FREQ_PATH, dev_num);
708
709         if (sysfs_read_str(avail_sysfs_path, freqs_buf, sizeof(freqs_buf)) > 0){
710                 cursor = freqs_buf;
711
712                 /* Decode allowed sampling rates string, ex: "10 20 50 100" */
713
714                 /* While we're not at the end of the string */
715                 while (*cursor && cursor[0]) {
716
717                         /* Decode a single integer value */
718                         n = atoi(cursor);
719
720                         if (n > max_supported_rate)
721                                 max_supported_rate = n;
722
723                         /* If this matches the selected rate, we're happy */
724                         if (new_sampling_rate == n)
725                                 break;
726
727                         /*
728                          * If we reached a higher value than the desired rate,
729                          * adjust selected rate so it matches the first higher
730                          * available one and stop parsing - this makes the
731                          * assumption that rates are sorted by increasing value
732                          * in the allowed frequencies string.
733                          */
734                         if (n > new_sampling_rate) {
735                                 ALOGI(
736                         "Increasing sampling rate on sensor %d from %d to %d\n",
737                                 s, req_sampling_rate, n);
738
739                                 new_sampling_rate = n;
740                                 break;
741                         }
742
743                         /* Skip digits */
744                         while (cursor[0] && !isspace(cursor[0]))
745                                 cursor++;
746
747                         /* Skip spaces */
748                         while (cursor[0] && isspace(cursor[0]))
749                                         cursor++;
750                 }
751         }
752
753         /* Cap sampling rate at 1000 events per second for now*/
754
755         limit = 1000;
756
757         if (max_supported_rate && new_sampling_rate > max_supported_rate)
758                 limit = max_supported_rate;
759
760         if (new_sampling_rate > limit) {
761
762                 new_sampling_rate = limit;
763
764                 ALOGI(  "Can't support %d sampling rate, lowering to %d\n",
765                         req_sampling_rate, new_sampling_rate);
766         }
767
768         /* If the desired rate is already active we're all set */
769         if (new_sampling_rate == cur_sampling_rate)
770                 return 0;
771
772         ALOGI("Sensor %d sampling rate switched to %d\n", s, new_sampling_rate);
773
774         if (trig_sensors_per_dev[dev_num])
775                 enable_buffer(dev_num, 0);
776
777         sysfs_write_int(sysfs_path, new_sampling_rate);
778
779         if (trig_sensors_per_dev[dev_num])
780                 enable_buffer(dev_num, 1);
781
782 exit:
783         /* Release the polling loop so an updated timeout value gets used */
784         write(poll_socket_pair[1], "", 1);
785
786         return 0;
787 }
788
789
790
791 int allocate_control_data (void)
792 {
793         int i;
794         struct epoll_event ev = {0};
795
796         for (i=0; i<MAX_DEVICES; i++)
797                 device_fd[i] = -1;
798
799         poll_fd = epoll_create(MAX_DEVICES);
800
801         if (poll_fd == -1) {
802                 ALOGE("Can't create epoll instance for iio sensors!\n");
803                 return -1;
804         }
805
806         /* Create and add "unblocking" fd to the set of watched fds */
807
808         if (socketpair(AF_UNIX, SOCK_STREAM, 0, poll_socket_pair) == -1) {
809                 ALOGE("Can't create socket pair for iio sensors!\n");
810                 close(poll_fd);
811                 return -1;
812         }
813
814         ev.events = EPOLLIN;
815         ev.data.u32 = INVALID_DEV_NUM;
816
817         epoll_ctl(poll_fd, EPOLL_CTL_ADD, poll_socket_pair[0], &ev);
818
819         return poll_fd;
820 }
821
822
823 void delete_control_data (void)
824 {
825 }