blob: f146acd1273775b75efb993b77a5e5ed6446b533 [file] [log] [blame]
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +01001/* 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>
Lars-Peter Clausen2c001932012-01-03 14:59:39 +010016#include <linux/kfifo.h>
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +010017#include <linux/module.h>
Lars-Peter Clausene18045e2012-01-03 14:59:41 +010018#include <linux/poll.h>
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +010019#include <linux/sched.h>
20#include <linux/slab.h>
21#include <linux/uaccess.h>
22#include <linux/wait.h>
Jonathan Cameron06458e22012-04-25 15:54:58 +010023#include <linux/iio/iio.h>
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +010024#include "iio_core.h"
Jonathan Cameron06458e22012-04-25 15:54:58 +010025#include <linux/iio/sysfs.h>
26#include <linux/iio/events.h>
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +010027
28/**
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +010029 * struct iio_event_interface - chrdev interface for an event line
30 * @wait: wait queue to allow blocking reads of events
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +010031 * @det_events: list of detected events
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +010032 * @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 */
36struct iio_event_interface {
37 wait_queue_head_t wait;
Lars-Peter Clausen2c001932012-01-03 14:59:39 +010038 DECLARE_KFIFO(det_events, struct iio_event_data, 16);
39
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +010040 struct list_head dev_attr_list;
41 unsigned long flags;
42 struct attribute_group group;
43};
44
45int iio_push_event(struct iio_dev *indio_dev, u64 ev_code, s64 timestamp)
46{
47 struct iio_event_interface *ev_int = indio_dev->event_interface;
Lars-Peter Clausen2c001932012-01-03 14:59:39 +010048 struct iio_event_data ev;
Lars-Peter Clausen1b9dc912013-03-07 19:53:00 +000049 unsigned long flags;
Lars-Peter Clausen2c001932012-01-03 14:59:39 +010050 int copied;
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +010051
52 /* Does anyone care? */
Lars-Peter Clausen1b9dc912013-03-07 19:53:00 +000053 spin_lock_irqsave(&ev_int->wait.lock, flags);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +010054 if (test_bit(IIO_BUSY_BIT_POS, &ev_int->flags)) {
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +010055
Lars-Peter Clausen2c001932012-01-03 14:59:39 +010056 ev.id = ev_code;
57 ev.timestamp = timestamp;
58
59 copied = kfifo_put(&ev_int->det_events, &ev);
Lars-Peter Clausen2c001932012-01-03 14:59:39 +010060 if (copied != 0)
Lars-Peter Clausene18045e2012-01-03 14:59:41 +010061 wake_up_locked_poll(&ev_int->wait, POLLIN);
Lars-Peter Clausen43ba1102012-01-03 14:59:40 +010062 }
Lars-Peter Clausen1b9dc912013-03-07 19:53:00 +000063 spin_unlock_irqrestore(&ev_int->wait.lock, flags);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +010064
Lars-Peter Clausen2c001932012-01-03 14:59:39 +010065 return 0;
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +010066}
67EXPORT_SYMBOL(iio_push_event);
68
Lars-Peter Clausene18045e2012-01-03 14:59:41 +010069/**
70 * iio_event_poll() - poll the event queue to find out if it has data
71 */
72static unsigned int iio_event_poll(struct file *filep,
73 struct poll_table_struct *wait)
74{
75 struct iio_event_interface *ev_int = filep->private_data;
76 unsigned int events = 0;
77
78 poll_wait(filep, &ev_int->wait, wait);
79
Lars-Peter Clausen1b9dc912013-03-07 19:53:00 +000080 spin_lock_irq(&ev_int->wait.lock);
Lars-Peter Clausene18045e2012-01-03 14:59:41 +010081 if (!kfifo_is_empty(&ev_int->det_events))
82 events = POLLIN | POLLRDNORM;
Lars-Peter Clausen1b9dc912013-03-07 19:53:00 +000083 spin_unlock_irq(&ev_int->wait.lock);
Lars-Peter Clausene18045e2012-01-03 14:59:41 +010084
85 return events;
86}
87
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +010088static ssize_t iio_event_chrdev_read(struct file *filep,
89 char __user *buf,
90 size_t count,
91 loff_t *f_ps)
92{
93 struct iio_event_interface *ev_int = filep->private_data;
Lars-Peter Clausen2c001932012-01-03 14:59:39 +010094 unsigned int copied;
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +010095 int ret;
96
Lars-Peter Clausen2c001932012-01-03 14:59:39 +010097 if (count < sizeof(struct iio_event_data))
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +010098 return -EINVAL;
99
Lars-Peter Clausen1b9dc912013-03-07 19:53:00 +0000100 spin_lock_irq(&ev_int->wait.lock);
Lars-Peter Clausen2c001932012-01-03 14:59:39 +0100101 if (kfifo_is_empty(&ev_int->det_events)) {
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100102 if (filep->f_flags & O_NONBLOCK) {
103 ret = -EAGAIN;
Lars-Peter Clausen43ba1102012-01-03 14:59:40 +0100104 goto error_unlock;
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100105 }
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100106 /* Blocking on device; waiting for something to be there */
Lars-Peter Clausen1b9dc912013-03-07 19:53:00 +0000107 ret = wait_event_interruptible_locked_irq(ev_int->wait,
Lars-Peter Clausen2c001932012-01-03 14:59:39 +0100108 !kfifo_is_empty(&ev_int->det_events));
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100109 if (ret)
Lars-Peter Clausen43ba1102012-01-03 14:59:40 +0100110 goto error_unlock;
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100111 /* Single access device so no one else can get the data */
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100112 }
113
Lars-Peter Clausen2c001932012-01-03 14:59:39 +0100114 ret = kfifo_to_user(&ev_int->det_events, buf, count, &copied);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100115
Lars-Peter Clausen43ba1102012-01-03 14:59:40 +0100116error_unlock:
Lars-Peter Clausen1b9dc912013-03-07 19:53:00 +0000117 spin_unlock_irq(&ev_int->wait.lock);
Lars-Peter Clausen43ba1102012-01-03 14:59:40 +0100118
Lars-Peter Clausen2c001932012-01-03 14:59:39 +0100119 return ret ? ret : copied;
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100120}
121
122static int iio_event_chrdev_release(struct inode *inode, struct file *filep)
123{
124 struct iio_event_interface *ev_int = filep->private_data;
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100125
Lars-Peter Clausen1b9dc912013-03-07 19:53:00 +0000126 spin_lock_irq(&ev_int->wait.lock);
Lars-Peter Clausena046c1e2012-01-03 14:59:42 +0100127 __clear_bit(IIO_BUSY_BIT_POS, &ev_int->flags);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100128 /*
129 * In order to maintain a clean state for reopening,
130 * clear out any awaiting events. The mask will prevent
131 * any new __iio_push_event calls running.
132 */
Lars-Peter Clausen2c001932012-01-03 14:59:39 +0100133 kfifo_reset_out(&ev_int->det_events);
Lars-Peter Clausen1b9dc912013-03-07 19:53:00 +0000134 spin_unlock_irq(&ev_int->wait.lock);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100135
136 return 0;
137}
138
139static const struct file_operations iio_event_chrdev_fileops = {
140 .read = iio_event_chrdev_read,
Lars-Peter Clausene18045e2012-01-03 14:59:41 +0100141 .poll = iio_event_poll,
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100142 .release = iio_event_chrdev_release,
143 .owner = THIS_MODULE,
144 .llseek = noop_llseek,
145};
146
147int iio_event_getfd(struct iio_dev *indio_dev)
148{
149 struct iio_event_interface *ev_int = indio_dev->event_interface;
150 int fd;
151
152 if (ev_int == NULL)
153 return -ENODEV;
154
Lars-Peter Clausen1b9dc912013-03-07 19:53:00 +0000155 spin_lock_irq(&ev_int->wait.lock);
Lars-Peter Clausena046c1e2012-01-03 14:59:42 +0100156 if (__test_and_set_bit(IIO_BUSY_BIT_POS, &ev_int->flags)) {
Lars-Peter Clausen1b9dc912013-03-07 19:53:00 +0000157 spin_unlock_irq(&ev_int->wait.lock);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100158 return -EBUSY;
159 }
Lars-Peter Clausen1b9dc912013-03-07 19:53:00 +0000160 spin_unlock_irq(&ev_int->wait.lock);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100161 fd = anon_inode_getfd("iio:event",
162 &iio_event_chrdev_fileops, ev_int, O_RDONLY);
163 if (fd < 0) {
Lars-Peter Clausen1b9dc912013-03-07 19:53:00 +0000164 spin_lock_irq(&ev_int->wait.lock);
Lars-Peter Clausena046c1e2012-01-03 14:59:42 +0100165 __clear_bit(IIO_BUSY_BIT_POS, &ev_int->flags);
Lars-Peter Clausen1b9dc912013-03-07 19:53:00 +0000166 spin_unlock_irq(&ev_int->wait.lock);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100167 }
168 return fd;
169}
170
171static const char * const iio_ev_type_text[] = {
172 [IIO_EV_TYPE_THRESH] = "thresh",
173 [IIO_EV_TYPE_MAG] = "mag",
174 [IIO_EV_TYPE_ROC] = "roc",
175 [IIO_EV_TYPE_THRESH_ADAPTIVE] = "thresh_adaptive",
176 [IIO_EV_TYPE_MAG_ADAPTIVE] = "mag_adaptive",
177};
178
179static const char * const iio_ev_dir_text[] = {
180 [IIO_EV_DIR_EITHER] = "either",
181 [IIO_EV_DIR_RISING] = "rising",
182 [IIO_EV_DIR_FALLING] = "falling"
183};
184
185static ssize_t iio_ev_state_store(struct device *dev,
186 struct device_attribute *attr,
187 const char *buf,
188 size_t len)
189{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200190 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100191 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
192 int ret;
193 bool val;
194
195 ret = strtobool(buf, &val);
196 if (ret < 0)
197 return ret;
198
199 ret = indio_dev->info->write_event_config(indio_dev,
200 this_attr->address,
201 val);
202 return (ret < 0) ? ret : len;
203}
204
205static ssize_t iio_ev_state_show(struct device *dev,
206 struct device_attribute *attr,
207 char *buf)
208{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200209 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100210 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
211 int val = indio_dev->info->read_event_config(indio_dev,
212 this_attr->address);
213
214 if (val < 0)
215 return val;
216 else
217 return sprintf(buf, "%d\n", val);
218}
219
220static ssize_t iio_ev_value_show(struct device *dev,
221 struct device_attribute *attr,
222 char *buf)
223{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200224 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100225 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
226 int val, ret;
227
228 ret = indio_dev->info->read_event_value(indio_dev,
229 this_attr->address, &val);
230 if (ret < 0)
231 return ret;
232
233 return sprintf(buf, "%d\n", val);
234}
235
236static ssize_t iio_ev_value_store(struct device *dev,
237 struct device_attribute *attr,
238 const char *buf,
239 size_t len)
240{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200241 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100242 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
Lars-Peter Clausen948ad202012-10-18 14:47:00 +0100243 int val;
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100244 int ret;
245
246 if (!indio_dev->info->write_event_value)
247 return -EINVAL;
248
Lars-Peter Clausen948ad202012-10-18 14:47:00 +0100249 ret = kstrtoint(buf, 10, &val);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100250 if (ret)
251 return ret;
252
253 ret = indio_dev->info->write_event_value(indio_dev, this_attr->address,
254 val);
255 if (ret < 0)
256 return ret;
257
258 return len;
259}
260
261static int iio_device_add_event_sysfs(struct iio_dev *indio_dev,
262 struct iio_chan_spec const *chan)
263{
264 int ret = 0, i, attrcount = 0;
265 u64 mask = 0;
266 char *postfix;
267 if (!chan->event_mask)
268 return 0;
269
270 for_each_set_bit(i, &chan->event_mask, sizeof(chan->event_mask)*8) {
271 postfix = kasprintf(GFP_KERNEL, "%s_%s_en",
272 iio_ev_type_text[i/IIO_EV_DIR_MAX],
273 iio_ev_dir_text[i%IIO_EV_DIR_MAX]);
274 if (postfix == NULL) {
275 ret = -ENOMEM;
276 goto error_ret;
277 }
278 if (chan->modified)
Lukasz Czerwinski2b0774d2013-09-18 10:21:00 +0100279 mask = IIO_MOD_EVENT_CODE(chan->type, 0, chan->channel2,
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100280 i/IIO_EV_DIR_MAX,
281 i%IIO_EV_DIR_MAX);
282 else if (chan->differential)
283 mask = IIO_EVENT_CODE(chan->type,
284 0, 0,
285 i%IIO_EV_DIR_MAX,
286 i/IIO_EV_DIR_MAX,
287 0,
288 chan->channel,
289 chan->channel2);
290 else
291 mask = IIO_UNMOD_EVENT_CODE(chan->type,
292 chan->channel,
293 i/IIO_EV_DIR_MAX,
294 i%IIO_EV_DIR_MAX);
295
296 ret = __iio_add_chan_devattr(postfix,
297 chan,
298 &iio_ev_state_show,
299 iio_ev_state_store,
300 mask,
301 0,
302 &indio_dev->dev,
303 &indio_dev->event_interface->
304 dev_attr_list);
305 kfree(postfix);
306 if (ret)
307 goto error_ret;
308 attrcount++;
309 postfix = kasprintf(GFP_KERNEL, "%s_%s_value",
310 iio_ev_type_text[i/IIO_EV_DIR_MAX],
311 iio_ev_dir_text[i%IIO_EV_DIR_MAX]);
312 if (postfix == NULL) {
313 ret = -ENOMEM;
314 goto error_ret;
315 }
316 ret = __iio_add_chan_devattr(postfix, chan,
317 iio_ev_value_show,
318 iio_ev_value_store,
319 mask,
320 0,
321 &indio_dev->dev,
322 &indio_dev->event_interface->
323 dev_attr_list);
324 kfree(postfix);
325 if (ret)
326 goto error_ret;
327 attrcount++;
328 }
329 ret = attrcount;
330error_ret:
331 return ret;
332}
333
334static inline void __iio_remove_event_config_attrs(struct iio_dev *indio_dev)
335{
336 struct iio_dev_attr *p, *n;
337 list_for_each_entry_safe(p, n,
338 &indio_dev->event_interface->
339 dev_attr_list, l) {
340 kfree(p->dev_attr.attr.name);
341 kfree(p);
342 }
343}
344
345static inline int __iio_add_event_config_attrs(struct iio_dev *indio_dev)
346{
347 int j, ret, attrcount = 0;
348
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100349 /* Dynically created from the channels array */
350 for (j = 0; j < indio_dev->num_channels; j++) {
351 ret = iio_device_add_event_sysfs(indio_dev,
352 &indio_dev->channels[j]);
353 if (ret < 0)
Julia Lawalle3db9ef2012-10-21 11:52:00 +0100354 return ret;
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100355 attrcount += ret;
356 }
357 return attrcount;
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100358}
359
360static bool iio_check_for_dynamic_events(struct iio_dev *indio_dev)
361{
362 int j;
363
364 for (j = 0; j < indio_dev->num_channels; j++)
365 if (indio_dev->channels[j].event_mask != 0)
366 return true;
367 return false;
368}
369
370static void iio_setup_ev_int(struct iio_event_interface *ev_int)
371{
Lars-Peter Clausen2c001932012-01-03 14:59:39 +0100372 INIT_KFIFO(ev_int->det_events);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100373 init_waitqueue_head(&ev_int->wait);
374}
375
376static const char *iio_event_group_name = "events";
377int iio_device_register_eventset(struct iio_dev *indio_dev)
378{
379 struct iio_dev_attr *p;
380 int ret = 0, attrcount_orig = 0, attrcount, attrn;
381 struct attribute **attr;
382
383 if (!(indio_dev->info->event_attrs ||
384 iio_check_for_dynamic_events(indio_dev)))
385 return 0;
386
387 indio_dev->event_interface =
388 kzalloc(sizeof(struct iio_event_interface), GFP_KERNEL);
389 if (indio_dev->event_interface == NULL) {
390 ret = -ENOMEM;
391 goto error_ret;
392 }
393
Sascha Hauer46b24312012-07-03 10:55:40 +0200394 INIT_LIST_HEAD(&indio_dev->event_interface->dev_attr_list);
395
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100396 iio_setup_ev_int(indio_dev->event_interface);
397 if (indio_dev->info->event_attrs != NULL) {
398 attr = indio_dev->info->event_attrs->attrs;
399 while (*attr++ != NULL)
400 attrcount_orig++;
401 }
402 attrcount = attrcount_orig;
403 if (indio_dev->channels) {
404 ret = __iio_add_event_config_attrs(indio_dev);
405 if (ret < 0)
406 goto error_free_setup_event_lines;
407 attrcount += ret;
408 }
409
410 indio_dev->event_interface->group.name = iio_event_group_name;
411 indio_dev->event_interface->group.attrs = kcalloc(attrcount + 1,
412 sizeof(indio_dev->event_interface->group.attrs[0]),
413 GFP_KERNEL);
414 if (indio_dev->event_interface->group.attrs == NULL) {
415 ret = -ENOMEM;
416 goto error_free_setup_event_lines;
417 }
418 if (indio_dev->info->event_attrs)
419 memcpy(indio_dev->event_interface->group.attrs,
420 indio_dev->info->event_attrs->attrs,
421 sizeof(indio_dev->event_interface->group.attrs[0])
422 *attrcount_orig);
423 attrn = attrcount_orig;
424 /* Add all elements from the list. */
425 list_for_each_entry(p,
426 &indio_dev->event_interface->dev_attr_list,
427 l)
428 indio_dev->event_interface->group.attrs[attrn++] =
429 &p->dev_attr.attr;
430 indio_dev->groups[indio_dev->groupcounter++] =
431 &indio_dev->event_interface->group;
432
433 return 0;
434
435error_free_setup_event_lines:
436 __iio_remove_event_config_attrs(indio_dev);
437 kfree(indio_dev->event_interface);
438error_ret:
439
440 return ret;
441}
442
443void iio_device_unregister_eventset(struct iio_dev *indio_dev)
444{
445 if (indio_dev->event_interface == NULL)
446 return;
447 __iio_remove_event_config_attrs(indio_dev);
448 kfree(indio_dev->event_interface->group.attrs);
449 kfree(indio_dev->event_interface);
450}