iio:buffer: Add proper locking for iio_update_buffers()
[deliverable/linux.git] / drivers / iio / industrialio-event.c
CommitLineData
0a769a95
LPC
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>
2c00193f 16#include <linux/kfifo.h>
0a769a95 17#include <linux/module.h>
e18045ed 18#include <linux/poll.h>
0a769a95
LPC
19#include <linux/sched.h>
20#include <linux/slab.h>
21#include <linux/uaccess.h>
22#include <linux/wait.h>
06458e27 23#include <linux/iio/iio.h>
0a769a95 24#include "iio_core.h"
06458e27
JC
25#include <linux/iio/sysfs.h>
26#include <linux/iio/events.h>
0a769a95 27
0a769a95
LPC
28/**
29 * struct iio_event_interface - chrdev interface for an event line
30 * @wait: wait queue to allow blocking reads of events
0a769a95 31 * @det_events: list of detected events
0a769a95
LPC
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 */
36struct iio_event_interface {
37 wait_queue_head_t wait;
2c00193f
LPC
38 DECLARE_KFIFO(det_events, struct iio_event_data, 16);
39
0a769a95
LPC
40 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;
2c00193f 48 struct iio_event_data ev;
1b9dc91e 49 unsigned long flags;
2c00193f 50 int copied;
0a769a95
LPC
51
52 /* Does anyone care? */
1b9dc91e 53 spin_lock_irqsave(&ev_int->wait.lock, flags);
0a769a95 54 if (test_bit(IIO_BUSY_BIT_POS, &ev_int->flags)) {
0a769a95 55
2c00193f
LPC
56 ev.id = ev_code;
57 ev.timestamp = timestamp;
58
59 copied = kfifo_put(&ev_int->det_events, &ev);
2c00193f 60 if (copied != 0)
e18045ed 61 wake_up_locked_poll(&ev_int->wait, POLLIN);
43ba1100 62 }
1b9dc91e 63 spin_unlock_irqrestore(&ev_int->wait.lock, flags);
0a769a95 64
2c00193f 65 return 0;
0a769a95
LPC
66}
67EXPORT_SYMBOL(iio_push_event);
68
e18045ed
LPC
69/**
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{
cadc2125
LPC
75 struct iio_dev *indio_dev = filep->private_data;
76 struct iio_event_interface *ev_int = indio_dev->event_interface;
e18045ed
LPC
77 unsigned int events = 0;
78
f18e7a06
LPC
79 if (!indio_dev->info)
80 return -ENODEV;
81
e18045ed
LPC
82 poll_wait(filep, &ev_int->wait, wait);
83
1b9dc91e 84 spin_lock_irq(&ev_int->wait.lock);
e18045ed
LPC
85 if (!kfifo_is_empty(&ev_int->det_events))
86 events = POLLIN | POLLRDNORM;
1b9dc91e 87 spin_unlock_irq(&ev_int->wait.lock);
e18045ed
LPC
88
89 return events;
90}
91
0a769a95
LPC
92static ssize_t iio_event_chrdev_read(struct file *filep,
93 char __user *buf,
94 size_t count,
95 loff_t *f_ps)
96{
cadc2125
LPC
97 struct iio_dev *indio_dev = filep->private_data;
98 struct iio_event_interface *ev_int = indio_dev->event_interface;
2c00193f 99 unsigned int copied;
0a769a95
LPC
100 int ret;
101
f18e7a06
LPC
102 if (!indio_dev->info)
103 return -ENODEV;
104
2c00193f 105 if (count < sizeof(struct iio_event_data))
0a769a95
LPC
106 return -EINVAL;
107
1b9dc91e 108 spin_lock_irq(&ev_int->wait.lock);
2c00193f 109 if (kfifo_is_empty(&ev_int->det_events)) {
0a769a95
LPC
110 if (filep->f_flags & O_NONBLOCK) {
111 ret = -EAGAIN;
43ba1100 112 goto error_unlock;
0a769a95 113 }
0a769a95 114 /* Blocking on device; waiting for something to be there */
1b9dc91e 115 ret = wait_event_interruptible_locked_irq(ev_int->wait,
d2f0a48f
LPC
116 !kfifo_is_empty(&ev_int->det_events) ||
117 indio_dev->info == NULL);
0a769a95 118 if (ret)
43ba1100 119 goto error_unlock;
d2f0a48f
LPC
120 if (indio_dev->info == NULL) {
121 ret = -ENODEV;
122 goto error_unlock;
123 }
0a769a95 124 /* Single access device so no one else can get the data */
0a769a95
LPC
125 }
126
2c00193f 127 ret = kfifo_to_user(&ev_int->det_events, buf, count, &copied);
0a769a95 128
43ba1100 129error_unlock:
1b9dc91e 130 spin_unlock_irq(&ev_int->wait.lock);
43ba1100 131
2c00193f 132 return ret ? ret : copied;
0a769a95
LPC
133}
134
135static int iio_event_chrdev_release(struct inode *inode, struct file *filep)
136{
cadc2125
LPC
137 struct iio_dev *indio_dev = filep->private_data;
138 struct iio_event_interface *ev_int = indio_dev->event_interface;
0a769a95 139
1b9dc91e 140 spin_lock_irq(&ev_int->wait.lock);
a046c1e8 141 __clear_bit(IIO_BUSY_BIT_POS, &ev_int->flags);
0a769a95
LPC
142 /*
143 * In order to maintain a clean state for reopening,
144 * clear out any awaiting events. The mask will prevent
145 * any new __iio_push_event calls running.
146 */
2c00193f 147 kfifo_reset_out(&ev_int->det_events);
1b9dc91e 148 spin_unlock_irq(&ev_int->wait.lock);
0a769a95 149
cadc2125
LPC
150 iio_device_put(indio_dev);
151
0a769a95
LPC
152 return 0;
153}
154
155static const struct file_operations iio_event_chrdev_fileops = {
156 .read = iio_event_chrdev_read,
e18045ed 157 .poll = iio_event_poll,
0a769a95
LPC
158 .release = iio_event_chrdev_release,
159 .owner = THIS_MODULE,
160 .llseek = noop_llseek,
161};
162
163int iio_event_getfd(struct iio_dev *indio_dev)
164{
165 struct iio_event_interface *ev_int = indio_dev->event_interface;
166 int fd;
167
168 if (ev_int == NULL)
169 return -ENODEV;
170
1b9dc91e 171 spin_lock_irq(&ev_int->wait.lock);
a046c1e8 172 if (__test_and_set_bit(IIO_BUSY_BIT_POS, &ev_int->flags)) {
1b9dc91e 173 spin_unlock_irq(&ev_int->wait.lock);
0a769a95
LPC
174 return -EBUSY;
175 }
1b9dc91e 176 spin_unlock_irq(&ev_int->wait.lock);
cadc2125
LPC
177 iio_device_get(indio_dev);
178
179 fd = anon_inode_getfd("iio:event", &iio_event_chrdev_fileops,
e2aad1d5 180 indio_dev, O_RDONLY | O_CLOEXEC);
0a769a95 181 if (fd < 0) {
1b9dc91e 182 spin_lock_irq(&ev_int->wait.lock);
a046c1e8 183 __clear_bit(IIO_BUSY_BIT_POS, &ev_int->flags);
1b9dc91e 184 spin_unlock_irq(&ev_int->wait.lock);
cadc2125 185 iio_device_put(indio_dev);
0a769a95
LPC
186 }
187 return fd;
188}
189
190static const char * const iio_ev_type_text[] = {
191 [IIO_EV_TYPE_THRESH] = "thresh",
192 [IIO_EV_TYPE_MAG] = "mag",
193 [IIO_EV_TYPE_ROC] = "roc",
194 [IIO_EV_TYPE_THRESH_ADAPTIVE] = "thresh_adaptive",
195 [IIO_EV_TYPE_MAG_ADAPTIVE] = "mag_adaptive",
196};
197
198static const char * const iio_ev_dir_text[] = {
199 [IIO_EV_DIR_EITHER] = "either",
200 [IIO_EV_DIR_RISING] = "rising",
201 [IIO_EV_DIR_FALLING] = "falling"
202};
203
204static ssize_t iio_ev_state_store(struct device *dev,
205 struct device_attribute *attr,
206 const char *buf,
207 size_t len)
208{
e53f5ac5 209 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
0a769a95
LPC
210 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
211 int ret;
212 bool val;
213
214 ret = strtobool(buf, &val);
215 if (ret < 0)
216 return ret;
217
218 ret = indio_dev->info->write_event_config(indio_dev,
219 this_attr->address,
220 val);
221 return (ret < 0) ? ret : len;
222}
223
224static ssize_t iio_ev_state_show(struct device *dev,
225 struct device_attribute *attr,
226 char *buf)
227{
e53f5ac5 228 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
0a769a95
LPC
229 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
230 int val = indio_dev->info->read_event_config(indio_dev,
231 this_attr->address);
232
233 if (val < 0)
234 return val;
235 else
236 return sprintf(buf, "%d\n", val);
237}
238
239static ssize_t iio_ev_value_show(struct device *dev,
240 struct device_attribute *attr,
241 char *buf)
242{
e53f5ac5 243 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
0a769a95
LPC
244 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
245 int val, ret;
246
247 ret = indio_dev->info->read_event_value(indio_dev,
248 this_attr->address, &val);
249 if (ret < 0)
250 return ret;
251
252 return sprintf(buf, "%d\n", val);
253}
254
255static ssize_t iio_ev_value_store(struct device *dev,
256 struct device_attribute *attr,
257 const char *buf,
258 size_t len)
259{
e53f5ac5 260 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
0a769a95 261 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
948ad205 262 int val;
0a769a95
LPC
263 int ret;
264
265 if (!indio_dev->info->write_event_value)
266 return -EINVAL;
267
948ad205 268 ret = kstrtoint(buf, 10, &val);
0a769a95
LPC
269 if (ret)
270 return ret;
271
272 ret = indio_dev->info->write_event_value(indio_dev, this_attr->address,
273 val);
274 if (ret < 0)
275 return ret;
276
277 return len;
278}
279
280static int iio_device_add_event_sysfs(struct iio_dev *indio_dev,
281 struct iio_chan_spec const *chan)
282{
283 int ret = 0, i, attrcount = 0;
284 u64 mask = 0;
285 char *postfix;
286 if (!chan->event_mask)
287 return 0;
288
289 for_each_set_bit(i, &chan->event_mask, sizeof(chan->event_mask)*8) {
290 postfix = kasprintf(GFP_KERNEL, "%s_%s_en",
291 iio_ev_type_text[i/IIO_EV_DIR_MAX],
292 iio_ev_dir_text[i%IIO_EV_DIR_MAX]);
293 if (postfix == NULL) {
294 ret = -ENOMEM;
295 goto error_ret;
296 }
297 if (chan->modified)
2b0774df 298 mask = IIO_MOD_EVENT_CODE(chan->type, 0, chan->channel2,
0a769a95
LPC
299 i/IIO_EV_DIR_MAX,
300 i%IIO_EV_DIR_MAX);
301 else if (chan->differential)
302 mask = IIO_EVENT_CODE(chan->type,
303 0, 0,
304 i%IIO_EV_DIR_MAX,
305 i/IIO_EV_DIR_MAX,
306 0,
307 chan->channel,
308 chan->channel2);
309 else
310 mask = IIO_UNMOD_EVENT_CODE(chan->type,
311 chan->channel,
312 i/IIO_EV_DIR_MAX,
313 i%IIO_EV_DIR_MAX);
314
315 ret = __iio_add_chan_devattr(postfix,
316 chan,
317 &iio_ev_state_show,
318 iio_ev_state_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 postfix = kasprintf(GFP_KERNEL, "%s_%s_value",
329 iio_ev_type_text[i/IIO_EV_DIR_MAX],
330 iio_ev_dir_text[i%IIO_EV_DIR_MAX]);
331 if (postfix == NULL) {
332 ret = -ENOMEM;
333 goto error_ret;
334 }
335 ret = __iio_add_chan_devattr(postfix, chan,
336 iio_ev_value_show,
337 iio_ev_value_store,
338 mask,
339 0,
340 &indio_dev->dev,
341 &indio_dev->event_interface->
342 dev_attr_list);
343 kfree(postfix);
344 if (ret)
345 goto error_ret;
346 attrcount++;
347 }
348 ret = attrcount;
349error_ret:
350 return ret;
351}
352
353static inline void __iio_remove_event_config_attrs(struct iio_dev *indio_dev)
354{
355 struct iio_dev_attr *p, *n;
356 list_for_each_entry_safe(p, n,
357 &indio_dev->event_interface->
358 dev_attr_list, l) {
359 kfree(p->dev_attr.attr.name);
360 kfree(p);
361 }
362}
363
364static inline int __iio_add_event_config_attrs(struct iio_dev *indio_dev)
365{
366 int j, ret, attrcount = 0;
367
0a769a95
LPC
368 /* Dynically created from the channels array */
369 for (j = 0; j < indio_dev->num_channels; j++) {
370 ret = iio_device_add_event_sysfs(indio_dev,
371 &indio_dev->channels[j]);
372 if (ret < 0)
e3db9ef6 373 return ret;
0a769a95
LPC
374 attrcount += ret;
375 }
376 return attrcount;
0a769a95
LPC
377}
378
379static bool iio_check_for_dynamic_events(struct iio_dev *indio_dev)
380{
381 int j;
382
383 for (j = 0; j < indio_dev->num_channels; j++)
384 if (indio_dev->channels[j].event_mask != 0)
385 return true;
386 return false;
387}
388
389static void iio_setup_ev_int(struct iio_event_interface *ev_int)
390{
2c00193f 391 INIT_KFIFO(ev_int->det_events);
0a769a95
LPC
392 init_waitqueue_head(&ev_int->wait);
393}
394
395static const char *iio_event_group_name = "events";
396int iio_device_register_eventset(struct iio_dev *indio_dev)
397{
398 struct iio_dev_attr *p;
399 int ret = 0, attrcount_orig = 0, attrcount, attrn;
400 struct attribute **attr;
401
402 if (!(indio_dev->info->event_attrs ||
403 iio_check_for_dynamic_events(indio_dev)))
404 return 0;
405
406 indio_dev->event_interface =
407 kzalloc(sizeof(struct iio_event_interface), GFP_KERNEL);
408 if (indio_dev->event_interface == NULL) {
409 ret = -ENOMEM;
410 goto error_ret;
411 }
412
46b24311
SH
413 INIT_LIST_HEAD(&indio_dev->event_interface->dev_attr_list);
414
0a769a95
LPC
415 iio_setup_ev_int(indio_dev->event_interface);
416 if (indio_dev->info->event_attrs != NULL) {
417 attr = indio_dev->info->event_attrs->attrs;
418 while (*attr++ != NULL)
419 attrcount_orig++;
420 }
421 attrcount = attrcount_orig;
422 if (indio_dev->channels) {
423 ret = __iio_add_event_config_attrs(indio_dev);
424 if (ret < 0)
425 goto error_free_setup_event_lines;
426 attrcount += ret;
427 }
428
429 indio_dev->event_interface->group.name = iio_event_group_name;
430 indio_dev->event_interface->group.attrs = kcalloc(attrcount + 1,
431 sizeof(indio_dev->event_interface->group.attrs[0]),
432 GFP_KERNEL);
433 if (indio_dev->event_interface->group.attrs == NULL) {
434 ret = -ENOMEM;
435 goto error_free_setup_event_lines;
436 }
437 if (indio_dev->info->event_attrs)
438 memcpy(indio_dev->event_interface->group.attrs,
439 indio_dev->info->event_attrs->attrs,
440 sizeof(indio_dev->event_interface->group.attrs[0])
441 *attrcount_orig);
442 attrn = attrcount_orig;
443 /* Add all elements from the list. */
444 list_for_each_entry(p,
445 &indio_dev->event_interface->dev_attr_list,
446 l)
447 indio_dev->event_interface->group.attrs[attrn++] =
448 &p->dev_attr.attr;
449 indio_dev->groups[indio_dev->groupcounter++] =
450 &indio_dev->event_interface->group;
451
452 return 0;
453
454error_free_setup_event_lines:
455 __iio_remove_event_config_attrs(indio_dev);
456 kfree(indio_dev->event_interface);
457error_ret:
458
459 return ret;
460}
461
d2f0a48f
LPC
462/**
463 * iio_device_wakeup_eventset - Wakes up the event waitqueue
464 * @indio_dev: The IIO device
465 *
466 * Wakes up the event waitqueue used for poll() and blocking read().
467 * Should usually be called when the device is unregistered.
468 */
469void iio_device_wakeup_eventset(struct iio_dev *indio_dev)
470{
471 if (indio_dev->event_interface == NULL)
472 return;
473 wake_up(&indio_dev->event_interface->wait);
474}
475
0a769a95
LPC
476void iio_device_unregister_eventset(struct iio_dev *indio_dev)
477{
478 if (indio_dev->event_interface == NULL)
479 return;
480 __iio_remove_event_config_attrs(indio_dev);
481 kfree(indio_dev->event_interface->group.attrs);
482 kfree(indio_dev->event_interface);
483}
This page took 0.27227 seconds and 5 git commands to generate.