OSDN Git Service

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