OSDN Git Service

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