]> Pileus Git - ~andy/linux/blob - drivers/iio/industrialio-event.c
Merge tag 'iio-for-3.14a' of git://git.kernel.org/pub/scm/linux/kernel/git/jic23...
[~andy/linux] / drivers / iio / industrialio-event.c
1 /* Industrial I/O event handling
2  *
3  * Copyright (c) 2008 Jonathan Cameron
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 as published by
7  * the Free Software Foundation.
8  *
9  * Based on elements of hwmon and input subsystems.
10  */
11
12 #include <linux/anon_inodes.h>
13 #include <linux/device.h>
14 #include <linux/fs.h>
15 #include <linux/kernel.h>
16 #include <linux/kfifo.h>
17 #include <linux/module.h>
18 #include <linux/poll.h>
19 #include <linux/sched.h>
20 #include <linux/slab.h>
21 #include <linux/uaccess.h>
22 #include <linux/wait.h>
23 #include <linux/iio/iio.h>
24 #include "iio_core.h"
25 #include <linux/iio/sysfs.h>
26 #include <linux/iio/events.h>
27
28 /**
29  * struct iio_event_interface - chrdev interface for an event line
30  * @wait:               wait queue to allow blocking reads of events
31  * @det_events:         list of detected events
32  * @dev_attr_list:      list of event interface sysfs attribute
33  * @flags:              file operations related flags including busy flag.
34  * @group:              event interface sysfs attribute group
35  */
36 struct iio_event_interface {
37         wait_queue_head_t       wait;
38         DECLARE_KFIFO(det_events, struct iio_event_data, 16);
39
40         struct list_head        dev_attr_list;
41         unsigned long           flags;
42         struct attribute_group  group;
43 };
44
45 /**
46  * iio_push_event() - try to add event to the list for userspace reading
47  * @indio_dev:          IIO device structure
48  * @ev_code:            What event
49  * @timestamp:          When the event occurred
50  **/
51 int iio_push_event(struct iio_dev *indio_dev, u64 ev_code, s64 timestamp)
52 {
53         struct iio_event_interface *ev_int = indio_dev->event_interface;
54         struct iio_event_data ev;
55         unsigned long flags;
56         int copied;
57
58         /* Does anyone care? */
59         spin_lock_irqsave(&ev_int->wait.lock, flags);
60         if (test_bit(IIO_BUSY_BIT_POS, &ev_int->flags)) {
61
62                 ev.id = ev_code;
63                 ev.timestamp = timestamp;
64
65                 copied = kfifo_put(&ev_int->det_events, ev);
66                 if (copied != 0)
67                         wake_up_locked_poll(&ev_int->wait, POLLIN);
68         }
69         spin_unlock_irqrestore(&ev_int->wait.lock, flags);
70
71         return 0;
72 }
73 EXPORT_SYMBOL(iio_push_event);
74
75 /**
76  * iio_event_poll() - poll the event queue to find out if it has data
77  */
78 static unsigned int iio_event_poll(struct file *filep,
79                              struct poll_table_struct *wait)
80 {
81         struct iio_dev *indio_dev = filep->private_data;
82         struct iio_event_interface *ev_int = indio_dev->event_interface;
83         unsigned int events = 0;
84
85         if (!indio_dev->info)
86                 return -ENODEV;
87
88         poll_wait(filep, &ev_int->wait, wait);
89
90         spin_lock_irq(&ev_int->wait.lock);
91         if (!kfifo_is_empty(&ev_int->det_events))
92                 events = POLLIN | POLLRDNORM;
93         spin_unlock_irq(&ev_int->wait.lock);
94
95         return events;
96 }
97
98 static ssize_t iio_event_chrdev_read(struct file *filep,
99                                      char __user *buf,
100                                      size_t count,
101                                      loff_t *f_ps)
102 {
103         struct iio_dev *indio_dev = filep->private_data;
104         struct iio_event_interface *ev_int = indio_dev->event_interface;
105         unsigned int copied;
106         int ret;
107
108         if (!indio_dev->info)
109                 return -ENODEV;
110
111         if (count < sizeof(struct iio_event_data))
112                 return -EINVAL;
113
114         spin_lock_irq(&ev_int->wait.lock);
115         if (kfifo_is_empty(&ev_int->det_events)) {
116                 if (filep->f_flags & O_NONBLOCK) {
117                         ret = -EAGAIN;
118                         goto error_unlock;
119                 }
120                 /* Blocking on device; waiting for something to be there */
121                 ret = wait_event_interruptible_locked_irq(ev_int->wait,
122                                         !kfifo_is_empty(&ev_int->det_events) ||
123                                         indio_dev->info == NULL);
124                 if (ret)
125                         goto error_unlock;
126                 if (indio_dev->info == NULL) {
127                         ret = -ENODEV;
128                         goto error_unlock;
129                 }
130                 /* Single access device so no one else can get the data */
131         }
132
133         ret = kfifo_to_user(&ev_int->det_events, buf, count, &copied);
134
135 error_unlock:
136         spin_unlock_irq(&ev_int->wait.lock);
137
138         return ret ? ret : copied;
139 }
140
141 static int iio_event_chrdev_release(struct inode *inode, struct file *filep)
142 {
143         struct iio_dev *indio_dev = filep->private_data;
144         struct iio_event_interface *ev_int = indio_dev->event_interface;
145
146         spin_lock_irq(&ev_int->wait.lock);
147         __clear_bit(IIO_BUSY_BIT_POS, &ev_int->flags);
148         /*
149          * In order to maintain a clean state for reopening,
150          * clear out any awaiting events. The mask will prevent
151          * any new __iio_push_event calls running.
152          */
153         kfifo_reset_out(&ev_int->det_events);
154         spin_unlock_irq(&ev_int->wait.lock);
155
156         iio_device_put(indio_dev);
157
158         return 0;
159 }
160
161 static const struct file_operations iio_event_chrdev_fileops = {
162         .read =  iio_event_chrdev_read,
163         .poll =  iio_event_poll,
164         .release = iio_event_chrdev_release,
165         .owner = THIS_MODULE,
166         .llseek = noop_llseek,
167 };
168
169 int iio_event_getfd(struct iio_dev *indio_dev)
170 {
171         struct iio_event_interface *ev_int = indio_dev->event_interface;
172         int fd;
173
174         if (ev_int == NULL)
175                 return -ENODEV;
176
177         spin_lock_irq(&ev_int->wait.lock);
178         if (__test_and_set_bit(IIO_BUSY_BIT_POS, &ev_int->flags)) {
179                 spin_unlock_irq(&ev_int->wait.lock);
180                 return -EBUSY;
181         }
182         spin_unlock_irq(&ev_int->wait.lock);
183         iio_device_get(indio_dev);
184
185         fd = anon_inode_getfd("iio:event", &iio_event_chrdev_fileops,
186                                 indio_dev, O_RDONLY | O_CLOEXEC);
187         if (fd < 0) {
188                 spin_lock_irq(&ev_int->wait.lock);
189                 __clear_bit(IIO_BUSY_BIT_POS, &ev_int->flags);
190                 spin_unlock_irq(&ev_int->wait.lock);
191                 iio_device_put(indio_dev);
192         }
193         return fd;
194 }
195
196 static const char * const iio_ev_type_text[] = {
197         [IIO_EV_TYPE_THRESH] = "thresh",
198         [IIO_EV_TYPE_MAG] = "mag",
199         [IIO_EV_TYPE_ROC] = "roc",
200         [IIO_EV_TYPE_THRESH_ADAPTIVE] = "thresh_adaptive",
201         [IIO_EV_TYPE_MAG_ADAPTIVE] = "mag_adaptive",
202 };
203
204 static const char * const iio_ev_dir_text[] = {
205         [IIO_EV_DIR_EITHER] = "either",
206         [IIO_EV_DIR_RISING] = "rising",
207         [IIO_EV_DIR_FALLING] = "falling"
208 };
209
210 static const char * const iio_ev_info_text[] = {
211         [IIO_EV_INFO_ENABLE] = "en",
212         [IIO_EV_INFO_VALUE] = "value",
213         [IIO_EV_INFO_HYSTERESIS] = "hysteresis",
214 };
215
216 static enum iio_event_direction iio_ev_attr_dir(struct iio_dev_attr *attr)
217 {
218         return attr->c->event_spec[attr->address & 0xffff].dir;
219 }
220
221 static enum iio_event_type iio_ev_attr_type(struct iio_dev_attr *attr)
222 {
223         return attr->c->event_spec[attr->address & 0xffff].type;
224 }
225
226 static enum iio_event_info iio_ev_attr_info(struct iio_dev_attr *attr)
227 {
228         return (attr->address >> 16) & 0xffff;
229 }
230
231 static ssize_t iio_ev_state_store(struct device *dev,
232                                   struct device_attribute *attr,
233                                   const char *buf,
234                                   size_t len)
235 {
236         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
237         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
238         int ret;
239         bool val;
240
241         ret = strtobool(buf, &val);
242         if (ret < 0)
243                 return ret;
244
245         if (indio_dev->info->write_event_config)
246                 ret = indio_dev->info->write_event_config(indio_dev,
247                         this_attr->address, val);
248         else
249                 ret = indio_dev->info->write_event_config_new(indio_dev,
250                         this_attr->c, iio_ev_attr_type(this_attr),
251                         iio_ev_attr_dir(this_attr), val);
252
253         return (ret < 0) ? ret : len;
254 }
255
256 static ssize_t iio_ev_state_show(struct device *dev,
257                                  struct device_attribute *attr,
258                                  char *buf)
259 {
260         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
261         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
262         int val;
263
264         if (indio_dev->info->read_event_config)
265                 val = indio_dev->info->read_event_config(indio_dev,
266                         this_attr->address);
267         else
268                 val = indio_dev->info->read_event_config_new(indio_dev,
269                         this_attr->c, iio_ev_attr_type(this_attr),
270                         iio_ev_attr_dir(this_attr));
271         if (val < 0)
272                 return val;
273         else
274                 return sprintf(buf, "%d\n", val);
275 }
276
277 static ssize_t iio_ev_value_show(struct device *dev,
278                                  struct device_attribute *attr,
279                                  char *buf)
280 {
281         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
282         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
283         int val, val2;
284         int ret;
285
286         if (indio_dev->info->read_event_value) {
287                 ret = indio_dev->info->read_event_value(indio_dev,
288                         this_attr->address, &val);
289                 if (ret < 0)
290                         return ret;
291                 return sprintf(buf, "%d\n", val);
292         } else {
293                 ret = indio_dev->info->read_event_value_new(indio_dev,
294                         this_attr->c, iio_ev_attr_type(this_attr),
295                         iio_ev_attr_dir(this_attr), iio_ev_attr_info(this_attr),
296                         &val, &val2);
297                 if (ret < 0)
298                         return ret;
299                 return iio_format_value(buf, ret, val, val2);
300         }
301 }
302
303 static ssize_t iio_ev_value_store(struct device *dev,
304                                   struct device_attribute *attr,
305                                   const char *buf,
306                                   size_t len)
307 {
308         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
309         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
310         int val, val2;
311         int ret;
312
313         if (!indio_dev->info->write_event_value &&
314                 !indio_dev->info->write_event_value_new)
315                 return -EINVAL;
316
317         if (indio_dev->info->write_event_value) {
318                 ret = kstrtoint(buf, 10, &val);
319                 if (ret)
320                         return ret;
321                 ret = indio_dev->info->write_event_value(indio_dev,
322                         this_attr->address, val);
323         } else {
324                 ret = iio_str_to_fixpoint(buf, 100000, &val, &val2);
325                 if (ret)
326                         return ret;
327                 ret = indio_dev->info->write_event_value_new(indio_dev,
328                         this_attr->c, iio_ev_attr_type(this_attr),
329                         iio_ev_attr_dir(this_attr), iio_ev_attr_info(this_attr),
330                         val, val2);
331         }
332         if (ret < 0)
333                 return ret;
334
335         return len;
336 }
337
338 static int iio_device_add_event(struct iio_dev *indio_dev,
339         const struct iio_chan_spec *chan, unsigned int spec_index,
340         enum iio_event_type type, enum iio_event_direction dir,
341         enum iio_shared_by shared_by, const unsigned long *mask)
342 {
343         ssize_t (*show)(struct device *, struct device_attribute *, char *);
344         ssize_t (*store)(struct device *, struct device_attribute *,
345                 const char *, size_t);
346         unsigned int attrcount = 0;
347         unsigned int i;
348         char *postfix;
349         int ret;
350
351         for_each_set_bit(i, mask, sizeof(*mask)) {
352                 postfix = kasprintf(GFP_KERNEL, "%s_%s_%s",
353                                 iio_ev_type_text[type], iio_ev_dir_text[dir],
354                                 iio_ev_info_text[i]);
355                 if (postfix == NULL)
356                         return -ENOMEM;
357
358                 if (i == IIO_EV_INFO_ENABLE) {
359                         show = iio_ev_state_show;
360                         store = iio_ev_state_store;
361                 } else {
362                         show = iio_ev_value_show;
363                         store = iio_ev_value_store;
364                 }
365
366                 ret = __iio_add_chan_devattr(postfix, chan, show, store,
367                          (i << 16) | spec_index, shared_by, &indio_dev->dev,
368                         &indio_dev->event_interface->dev_attr_list);
369                 kfree(postfix);
370
371                 if (ret)
372                         return ret;
373
374                 attrcount++;
375         }
376
377         return attrcount;
378 }
379
380 static int iio_device_add_event_sysfs_new(struct iio_dev *indio_dev,
381         struct iio_chan_spec const *chan)
382 {
383         int ret = 0, i, attrcount = 0;
384         enum iio_event_direction dir;
385         enum iio_event_type type;
386
387         for (i = 0; i < chan->num_event_specs; i++) {
388                 type = chan->event_spec[i].type;
389                 dir = chan->event_spec[i].dir;
390
391                 ret = iio_device_add_event(indio_dev, chan, i, type, dir,
392                         IIO_SEPARATE, &chan->event_spec[i].mask_separate);
393                 if (ret < 0)
394                         goto error_ret;
395                 attrcount += ret;
396
397                 ret = iio_device_add_event(indio_dev, chan, i, type, dir,
398                         IIO_SHARED_BY_TYPE,
399                         &chan->event_spec[i].mask_shared_by_type);
400                 if (ret < 0)
401                         goto error_ret;
402                 attrcount += ret;
403
404                 ret = iio_device_add_event(indio_dev, chan, i, type, dir,
405                         IIO_SHARED_BY_DIR,
406                         &chan->event_spec[i].mask_shared_by_dir);
407                 if (ret < 0)
408                         goto error_ret;
409                 attrcount += ret;
410
411                 ret = iio_device_add_event(indio_dev, chan, i, type, dir,
412                         IIO_SHARED_BY_ALL,
413                         &chan->event_spec[i].mask_shared_by_all);
414                 if (ret < 0)
415                         goto error_ret;
416                 attrcount += ret;
417         }
418         ret = attrcount;
419 error_ret:
420         return ret;
421 }
422
423 static int iio_device_add_event_sysfs_old(struct iio_dev *indio_dev,
424                                       struct iio_chan_spec const *chan)
425 {
426         int ret = 0, i, attrcount = 0;
427         u64 mask = 0;
428         char *postfix;
429         if (!chan->event_mask)
430                 return 0;
431
432         for_each_set_bit(i, &chan->event_mask, sizeof(chan->event_mask)*8) {
433                 postfix = kasprintf(GFP_KERNEL, "%s_%s_en",
434                                     iio_ev_type_text[i/IIO_EV_DIR_MAX],
435                                     iio_ev_dir_text[i%IIO_EV_DIR_MAX]);
436                 if (postfix == NULL) {
437                         ret = -ENOMEM;
438                         goto error_ret;
439                 }
440                 if (chan->modified)
441                         mask = IIO_MOD_EVENT_CODE(chan->type, 0, chan->channel2,
442                                                   i/IIO_EV_DIR_MAX,
443                                                   i%IIO_EV_DIR_MAX);
444                 else if (chan->differential)
445                         mask = IIO_EVENT_CODE(chan->type,
446                                               0, 0,
447                                               i%IIO_EV_DIR_MAX,
448                                               i/IIO_EV_DIR_MAX,
449                                               0,
450                                               chan->channel,
451                                               chan->channel2);
452                 else
453                         mask = IIO_UNMOD_EVENT_CODE(chan->type,
454                                                     chan->channel,
455                                                     i/IIO_EV_DIR_MAX,
456                                                     i%IIO_EV_DIR_MAX);
457
458                 ret = __iio_add_chan_devattr(postfix,
459                                              chan,
460                                              &iio_ev_state_show,
461                                              iio_ev_state_store,
462                                              mask,
463                                              0,
464                                              &indio_dev->dev,
465                                              &indio_dev->event_interface->
466                                              dev_attr_list);
467                 kfree(postfix);
468                 if (ret)
469                         goto error_ret;
470                 attrcount++;
471                 postfix = kasprintf(GFP_KERNEL, "%s_%s_value",
472                                     iio_ev_type_text[i/IIO_EV_DIR_MAX],
473                                     iio_ev_dir_text[i%IIO_EV_DIR_MAX]);
474                 if (postfix == NULL) {
475                         ret = -ENOMEM;
476                         goto error_ret;
477                 }
478                 ret = __iio_add_chan_devattr(postfix, chan,
479                                              iio_ev_value_show,
480                                              iio_ev_value_store,
481                                              mask,
482                                              0,
483                                              &indio_dev->dev,
484                                              &indio_dev->event_interface->
485                                              dev_attr_list);
486                 kfree(postfix);
487                 if (ret)
488                         goto error_ret;
489                 attrcount++;
490         }
491         ret = attrcount;
492 error_ret:
493         return ret;
494 }
495
496
497 static int iio_device_add_event_sysfs(struct iio_dev *indio_dev,
498                                       struct iio_chan_spec const *chan)
499 {
500         if (chan->event_mask)
501                 return iio_device_add_event_sysfs_old(indio_dev, chan);
502         else
503                 return iio_device_add_event_sysfs_new(indio_dev, chan);
504 }
505
506 static inline int __iio_add_event_config_attrs(struct iio_dev *indio_dev)
507 {
508         int j, ret, attrcount = 0;
509
510         /* Dynically created from the channels array */
511         for (j = 0; j < indio_dev->num_channels; j++) {
512                 ret = iio_device_add_event_sysfs(indio_dev,
513                                                  &indio_dev->channels[j]);
514                 if (ret < 0)
515                         return ret;
516                 attrcount += ret;
517         }
518         return attrcount;
519 }
520
521 static bool iio_check_for_dynamic_events(struct iio_dev *indio_dev)
522 {
523         int j;
524
525         for (j = 0; j < indio_dev->num_channels; j++) {
526                 if (indio_dev->channels[j].event_mask != 0)
527                         return true;
528                 if (indio_dev->channels[j].num_event_specs != 0)
529                         return true;
530         }
531         return false;
532 }
533
534 static void iio_setup_ev_int(struct iio_event_interface *ev_int)
535 {
536         INIT_KFIFO(ev_int->det_events);
537         init_waitqueue_head(&ev_int->wait);
538 }
539
540 static const char *iio_event_group_name = "events";
541 int iio_device_register_eventset(struct iio_dev *indio_dev)
542 {
543         struct iio_dev_attr *p;
544         int ret = 0, attrcount_orig = 0, attrcount, attrn;
545         struct attribute **attr;
546
547         if (!(indio_dev->info->event_attrs ||
548               iio_check_for_dynamic_events(indio_dev)))
549                 return 0;
550
551         indio_dev->event_interface =
552                 kzalloc(sizeof(struct iio_event_interface), GFP_KERNEL);
553         if (indio_dev->event_interface == NULL) {
554                 ret = -ENOMEM;
555                 goto error_ret;
556         }
557
558         INIT_LIST_HEAD(&indio_dev->event_interface->dev_attr_list);
559
560         iio_setup_ev_int(indio_dev->event_interface);
561         if (indio_dev->info->event_attrs != NULL) {
562                 attr = indio_dev->info->event_attrs->attrs;
563                 while (*attr++ != NULL)
564                         attrcount_orig++;
565         }
566         attrcount = attrcount_orig;
567         if (indio_dev->channels) {
568                 ret = __iio_add_event_config_attrs(indio_dev);
569                 if (ret < 0)
570                         goto error_free_setup_event_lines;
571                 attrcount += ret;
572         }
573
574         indio_dev->event_interface->group.name = iio_event_group_name;
575         indio_dev->event_interface->group.attrs = kcalloc(attrcount + 1,
576                                                           sizeof(indio_dev->event_interface->group.attrs[0]),
577                                                           GFP_KERNEL);
578         if (indio_dev->event_interface->group.attrs == NULL) {
579                 ret = -ENOMEM;
580                 goto error_free_setup_event_lines;
581         }
582         if (indio_dev->info->event_attrs)
583                 memcpy(indio_dev->event_interface->group.attrs,
584                        indio_dev->info->event_attrs->attrs,
585                        sizeof(indio_dev->event_interface->group.attrs[0])
586                        *attrcount_orig);
587         attrn = attrcount_orig;
588         /* Add all elements from the list. */
589         list_for_each_entry(p,
590                             &indio_dev->event_interface->dev_attr_list,
591                             l)
592                 indio_dev->event_interface->group.attrs[attrn++] =
593                         &p->dev_attr.attr;
594         indio_dev->groups[indio_dev->groupcounter++] =
595                 &indio_dev->event_interface->group;
596
597         return 0;
598
599 error_free_setup_event_lines:
600         iio_free_chan_devattr_list(&indio_dev->event_interface->dev_attr_list);
601         kfree(indio_dev->event_interface);
602 error_ret:
603
604         return ret;
605 }
606
607 /**
608  * iio_device_wakeup_eventset - Wakes up the event waitqueue
609  * @indio_dev: The IIO device
610  *
611  * Wakes up the event waitqueue used for poll() and blocking read().
612  * Should usually be called when the device is unregistered.
613  */
614 void iio_device_wakeup_eventset(struct iio_dev *indio_dev)
615 {
616         if (indio_dev->event_interface == NULL)
617                 return;
618         wake_up(&indio_dev->event_interface->wait);
619 }
620
621 void iio_device_unregister_eventset(struct iio_dev *indio_dev)
622 {
623         if (indio_dev->event_interface == NULL)
624                 return;
625         iio_free_chan_devattr_list(&indio_dev->event_interface->dev_attr_list);
626         kfree(indio_dev->event_interface->group.attrs);
627         kfree(indio_dev->event_interface);
628 }