blob: c10eab64bc0524eca6caa476d224c370fcd49262 [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
Stefani Seibold498d3192013-11-14 14:32:17 -080059 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{
Lars-Peter Clausencadc2122013-09-18 21:02:00 +010075 struct iio_dev *indio_dev = filep->private_data;
76 struct iio_event_interface *ev_int = indio_dev->event_interface;
Lars-Peter Clausene18045e2012-01-03 14:59:41 +010077 unsigned int events = 0;
78
Lars-Peter Clausenf18e7a02013-10-04 12:06:00 +010079 if (!indio_dev->info)
80 return -ENODEV;
81
Lars-Peter Clausene18045e2012-01-03 14:59:41 +010082 poll_wait(filep, &ev_int->wait, wait);
83
Lars-Peter Clausen1b9dc912013-03-07 19:53:00 +000084 spin_lock_irq(&ev_int->wait.lock);
Lars-Peter Clausene18045e2012-01-03 14:59:41 +010085 if (!kfifo_is_empty(&ev_int->det_events))
86 events = POLLIN | POLLRDNORM;
Lars-Peter Clausen1b9dc912013-03-07 19:53:00 +000087 spin_unlock_irq(&ev_int->wait.lock);
Lars-Peter Clausene18045e2012-01-03 14:59:41 +010088
89 return events;
90}
91
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +010092static ssize_t iio_event_chrdev_read(struct file *filep,
93 char __user *buf,
94 size_t count,
95 loff_t *f_ps)
96{
Lars-Peter Clausencadc2122013-09-18 21:02:00 +010097 struct iio_dev *indio_dev = filep->private_data;
98 struct iio_event_interface *ev_int = indio_dev->event_interface;
Lars-Peter Clausen2c001932012-01-03 14:59:39 +010099 unsigned int copied;
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100100 int ret;
101
Lars-Peter Clausenf18e7a02013-10-04 12:06:00 +0100102 if (!indio_dev->info)
103 return -ENODEV;
104
Lars-Peter Clausen2c001932012-01-03 14:59:39 +0100105 if (count < sizeof(struct iio_event_data))
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100106 return -EINVAL;
107
Lars-Peter Clausen1b9dc912013-03-07 19:53:00 +0000108 spin_lock_irq(&ev_int->wait.lock);
Lars-Peter Clausen2c001932012-01-03 14:59:39 +0100109 if (kfifo_is_empty(&ev_int->det_events)) {
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100110 if (filep->f_flags & O_NONBLOCK) {
111 ret = -EAGAIN;
Lars-Peter Clausen43ba1102012-01-03 14:59:40 +0100112 goto error_unlock;
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100113 }
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100114 /* Blocking on device; waiting for something to be there */
Lars-Peter Clausen1b9dc912013-03-07 19:53:00 +0000115 ret = wait_event_interruptible_locked_irq(ev_int->wait,
Lars-Peter Clausend2f0a482013-10-04 12:07:00 +0100116 !kfifo_is_empty(&ev_int->det_events) ||
117 indio_dev->info == NULL);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100118 if (ret)
Lars-Peter Clausen43ba1102012-01-03 14:59:40 +0100119 goto error_unlock;
Lars-Peter Clausend2f0a482013-10-04 12:07:00 +0100120 if (indio_dev->info == NULL) {
121 ret = -ENODEV;
122 goto error_unlock;
123 }
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100124 /* Single access device so no one else can get the data */
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100125 }
126
Lars-Peter Clausen2c001932012-01-03 14:59:39 +0100127 ret = kfifo_to_user(&ev_int->det_events, buf, count, &copied);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100128
Lars-Peter Clausen43ba1102012-01-03 14:59:40 +0100129error_unlock:
Lars-Peter Clausen1b9dc912013-03-07 19:53:00 +0000130 spin_unlock_irq(&ev_int->wait.lock);
Lars-Peter Clausen43ba1102012-01-03 14:59:40 +0100131
Lars-Peter Clausen2c001932012-01-03 14:59:39 +0100132 return ret ? ret : copied;
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100133}
134
135static int iio_event_chrdev_release(struct inode *inode, struct file *filep)
136{
Lars-Peter Clausencadc2122013-09-18 21:02:00 +0100137 struct iio_dev *indio_dev = filep->private_data;
138 struct iio_event_interface *ev_int = indio_dev->event_interface;
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100139
Lars-Peter Clausen1b9dc912013-03-07 19:53:00 +0000140 spin_lock_irq(&ev_int->wait.lock);
Lars-Peter Clausena046c1e2012-01-03 14:59:42 +0100141 __clear_bit(IIO_BUSY_BIT_POS, &ev_int->flags);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100142 /*
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 */
Lars-Peter Clausen2c001932012-01-03 14:59:39 +0100147 kfifo_reset_out(&ev_int->det_events);
Lars-Peter Clausen1b9dc912013-03-07 19:53:00 +0000148 spin_unlock_irq(&ev_int->wait.lock);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100149
Lars-Peter Clausencadc2122013-09-18 21:02:00 +0100150 iio_device_put(indio_dev);
151
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100152 return 0;
153}
154
155static const struct file_operations iio_event_chrdev_fileops = {
156 .read = iio_event_chrdev_read,
Lars-Peter Clausene18045e2012-01-03 14:59:41 +0100157 .poll = iio_event_poll,
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100158 .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
Lars-Peter Clausen1b9dc912013-03-07 19:53:00 +0000171 spin_lock_irq(&ev_int->wait.lock);
Lars-Peter Clausena046c1e2012-01-03 14:59:42 +0100172 if (__test_and_set_bit(IIO_BUSY_BIT_POS, &ev_int->flags)) {
Lars-Peter Clausen1b9dc912013-03-07 19:53:00 +0000173 spin_unlock_irq(&ev_int->wait.lock);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100174 return -EBUSY;
175 }
Lars-Peter Clausen1b9dc912013-03-07 19:53:00 +0000176 spin_unlock_irq(&ev_int->wait.lock);
Lars-Peter Clausencadc2122013-09-18 21:02:00 +0100177 iio_device_get(indio_dev);
178
179 fd = anon_inode_getfd("iio:event", &iio_event_chrdev_fileops,
Greg Kroah-Hartmane2aad1d2013-09-25 08:59:04 -0700180 indio_dev, O_RDONLY | O_CLOEXEC);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100181 if (fd < 0) {
Lars-Peter Clausen1b9dc912013-03-07 19:53:00 +0000182 spin_lock_irq(&ev_int->wait.lock);
Lars-Peter Clausena046c1e2012-01-03 14:59:42 +0100183 __clear_bit(IIO_BUSY_BIT_POS, &ev_int->flags);
Lars-Peter Clausen1b9dc912013-03-07 19:53:00 +0000184 spin_unlock_irq(&ev_int->wait.lock);
Lars-Peter Clausencadc2122013-09-18 21:02:00 +0100185 iio_device_put(indio_dev);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100186 }
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
Lars-Peter Clausenb4e3ac02013-10-07 15:11:00 +0100204static const char * const iio_ev_info_text[] = {
205 [IIO_EV_INFO_ENABLE] = "en",
206 [IIO_EV_INFO_VALUE] = "value",
Lars-Peter Clausenec6670a2013-10-07 15:11:00 +0100207 [IIO_EV_INFO_HYSTERESIS] = "hysteresis",
Lars-Peter Clausenb4e3ac02013-10-07 15:11:00 +0100208};
209
210static enum iio_event_direction iio_ev_attr_dir(struct iio_dev_attr *attr)
211{
212 return attr->c->event_spec[attr->address & 0xffff].dir;
213}
214
215static enum iio_event_type iio_ev_attr_type(struct iio_dev_attr *attr)
216{
217 return attr->c->event_spec[attr->address & 0xffff].type;
218}
219
220static enum iio_event_info iio_ev_attr_info(struct iio_dev_attr *attr)
221{
222 return (attr->address >> 16) & 0xffff;
223}
224
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100225static ssize_t iio_ev_state_store(struct device *dev,
226 struct device_attribute *attr,
227 const char *buf,
228 size_t len)
229{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200230 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100231 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
232 int ret;
233 bool val;
234
235 ret = strtobool(buf, &val);
236 if (ret < 0)
237 return ret;
238
Lars-Peter Clausenb4e3ac02013-10-07 15:11:00 +0100239 if (indio_dev->info->write_event_config)
240 ret = indio_dev->info->write_event_config(indio_dev,
241 this_attr->address, val);
242 else
243 ret = indio_dev->info->write_event_config_new(indio_dev,
244 this_attr->c, iio_ev_attr_type(this_attr),
245 iio_ev_attr_dir(this_attr), val);
246
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100247 return (ret < 0) ? ret : len;
248}
249
250static ssize_t iio_ev_state_show(struct device *dev,
251 struct device_attribute *attr,
252 char *buf)
253{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200254 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100255 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
Lars-Peter Clausenb4e3ac02013-10-07 15:11:00 +0100256 int val;
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100257
Lars-Peter Clausenb4e3ac02013-10-07 15:11:00 +0100258 if (indio_dev->info->read_event_config)
259 val = indio_dev->info->read_event_config(indio_dev,
260 this_attr->address);
261 else
262 val = indio_dev->info->read_event_config_new(indio_dev,
263 this_attr->c, iio_ev_attr_type(this_attr),
264 iio_ev_attr_dir(this_attr));
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100265 if (val < 0)
266 return val;
267 else
268 return sprintf(buf, "%d\n", val);
269}
270
271static ssize_t iio_ev_value_show(struct device *dev,
272 struct device_attribute *attr,
273 char *buf)
274{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200275 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100276 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
Lars-Peter Clausenb4e3ac02013-10-07 15:11:00 +0100277 int val, val2;
278 int ret;
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100279
Lars-Peter Clausenb4e3ac02013-10-07 15:11:00 +0100280 if (indio_dev->info->read_event_value) {
281 ret = indio_dev->info->read_event_value(indio_dev,
282 this_attr->address, &val);
283 if (ret < 0)
284 return ret;
285 return sprintf(buf, "%d\n", val);
286 } else {
287 ret = indio_dev->info->read_event_value_new(indio_dev,
288 this_attr->c, iio_ev_attr_type(this_attr),
289 iio_ev_attr_dir(this_attr), iio_ev_attr_info(this_attr),
290 &val, &val2);
291 if (ret < 0)
292 return ret;
293 return iio_format_value(buf, ret, val, val2);
294 }
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100295}
296
297static ssize_t iio_ev_value_store(struct device *dev,
298 struct device_attribute *attr,
299 const char *buf,
300 size_t len)
301{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200302 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100303 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
Lars-Peter Clausenb4e3ac02013-10-07 15:11:00 +0100304 int val, val2;
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100305 int ret;
306
Lars-Peter Clausenb4e3ac02013-10-07 15:11:00 +0100307 if (!indio_dev->info->write_event_value &&
308 !indio_dev->info->write_event_value_new)
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100309 return -EINVAL;
310
Lars-Peter Clausenb4e3ac02013-10-07 15:11:00 +0100311 if (indio_dev->info->write_event_value) {
312 ret = kstrtoint(buf, 10, &val);
313 if (ret)
314 return ret;
315 ret = indio_dev->info->write_event_value(indio_dev,
316 this_attr->address, val);
317 } else {
318 ret = iio_str_to_fixpoint(buf, 100000, &val, &val2);
319 if (ret)
320 return ret;
321 ret = indio_dev->info->write_event_value_new(indio_dev,
322 this_attr->c, iio_ev_attr_type(this_attr),
323 iio_ev_attr_dir(this_attr), iio_ev_attr_info(this_attr),
324 val, val2);
325 }
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100326 if (ret < 0)
327 return ret;
328
329 return len;
330}
331
Lars-Peter Clausenb4e3ac02013-10-07 15:11:00 +0100332static int iio_device_add_event(struct iio_dev *indio_dev,
333 const struct iio_chan_spec *chan, unsigned int spec_index,
334 enum iio_event_type type, enum iio_event_direction dir,
335 enum iio_shared_by shared_by, const unsigned long *mask)
336{
337 ssize_t (*show)(struct device *, struct device_attribute *, char *);
338 ssize_t (*store)(struct device *, struct device_attribute *,
339 const char *, size_t);
340 unsigned int attrcount = 0;
341 unsigned int i;
342 char *postfix;
343 int ret;
344
345 for_each_set_bit(i, mask, sizeof(*mask)) {
346 postfix = kasprintf(GFP_KERNEL, "%s_%s_%s",
347 iio_ev_type_text[type], iio_ev_dir_text[dir],
348 iio_ev_info_text[i]);
349 if (postfix == NULL)
350 return -ENOMEM;
351
352 if (i == IIO_EV_INFO_ENABLE) {
353 show = iio_ev_state_show;
354 store = iio_ev_state_store;
355 } else {
356 show = iio_ev_value_show;
357 store = iio_ev_value_store;
358 }
359
360 ret = __iio_add_chan_devattr(postfix, chan, show, store,
361 (i << 16) | spec_index, shared_by, &indio_dev->dev,
362 &indio_dev->event_interface->dev_attr_list);
363 kfree(postfix);
364
365 if (ret)
366 return ret;
367
368 attrcount++;
369 }
370
371 return attrcount;
372}
373
374static int iio_device_add_event_sysfs_new(struct iio_dev *indio_dev,
375 struct iio_chan_spec const *chan)
376{
377 int ret = 0, i, attrcount = 0;
378 enum iio_event_direction dir;
379 enum iio_event_type type;
380
381 for (i = 0; i < chan->num_event_specs; i++) {
382 type = chan->event_spec[i].type;
383 dir = chan->event_spec[i].dir;
384
385 ret = iio_device_add_event(indio_dev, chan, i, type, dir,
386 IIO_SEPARATE, &chan->event_spec[i].mask_separate);
387 if (ret < 0)
388 goto error_ret;
389 attrcount += ret;
390
391 ret = iio_device_add_event(indio_dev, chan, i, type, dir,
392 IIO_SHARED_BY_TYPE,
393 &chan->event_spec[i].mask_shared_by_type);
394 if (ret < 0)
395 goto error_ret;
396 attrcount += ret;
397
398 ret = iio_device_add_event(indio_dev, chan, i, type, dir,
399 IIO_SHARED_BY_DIR,
400 &chan->event_spec[i].mask_shared_by_dir);
401 if (ret < 0)
402 goto error_ret;
403 attrcount += ret;
404
405 ret = iio_device_add_event(indio_dev, chan, i, type, dir,
406 IIO_SHARED_BY_ALL,
407 &chan->event_spec[i].mask_shared_by_all);
408 if (ret < 0)
409 goto error_ret;
410 attrcount += ret;
411 }
412 ret = attrcount;
413error_ret:
414 return ret;
415}
416
417static int iio_device_add_event_sysfs_old(struct iio_dev *indio_dev,
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100418 struct iio_chan_spec const *chan)
419{
420 int ret = 0, i, attrcount = 0;
421 u64 mask = 0;
422 char *postfix;
423 if (!chan->event_mask)
424 return 0;
425
426 for_each_set_bit(i, &chan->event_mask, sizeof(chan->event_mask)*8) {
427 postfix = kasprintf(GFP_KERNEL, "%s_%s_en",
428 iio_ev_type_text[i/IIO_EV_DIR_MAX],
429 iio_ev_dir_text[i%IIO_EV_DIR_MAX]);
430 if (postfix == NULL) {
431 ret = -ENOMEM;
432 goto error_ret;
433 }
434 if (chan->modified)
Lukasz Czerwinski2b0774d2013-09-18 10:21:00 +0100435 mask = IIO_MOD_EVENT_CODE(chan->type, 0, chan->channel2,
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100436 i/IIO_EV_DIR_MAX,
437 i%IIO_EV_DIR_MAX);
438 else if (chan->differential)
439 mask = IIO_EVENT_CODE(chan->type,
440 0, 0,
441 i%IIO_EV_DIR_MAX,
442 i/IIO_EV_DIR_MAX,
443 0,
444 chan->channel,
445 chan->channel2);
446 else
447 mask = IIO_UNMOD_EVENT_CODE(chan->type,
448 chan->channel,
449 i/IIO_EV_DIR_MAX,
450 i%IIO_EV_DIR_MAX);
451
452 ret = __iio_add_chan_devattr(postfix,
453 chan,
454 &iio_ev_state_show,
455 iio_ev_state_store,
456 mask,
457 0,
458 &indio_dev->dev,
459 &indio_dev->event_interface->
460 dev_attr_list);
461 kfree(postfix);
462 if (ret)
463 goto error_ret;
464 attrcount++;
465 postfix = kasprintf(GFP_KERNEL, "%s_%s_value",
466 iio_ev_type_text[i/IIO_EV_DIR_MAX],
467 iio_ev_dir_text[i%IIO_EV_DIR_MAX]);
468 if (postfix == NULL) {
469 ret = -ENOMEM;
470 goto error_ret;
471 }
472 ret = __iio_add_chan_devattr(postfix, chan,
473 iio_ev_value_show,
474 iio_ev_value_store,
475 mask,
476 0,
477 &indio_dev->dev,
478 &indio_dev->event_interface->
479 dev_attr_list);
480 kfree(postfix);
481 if (ret)
482 goto error_ret;
483 attrcount++;
484 }
485 ret = attrcount;
486error_ret:
487 return ret;
488}
489
Lars-Peter Clausenb4e3ac02013-10-07 15:11:00 +0100490
491static int iio_device_add_event_sysfs(struct iio_dev *indio_dev,
492 struct iio_chan_spec const *chan)
493{
494 if (chan->event_mask)
495 return iio_device_add_event_sysfs_old(indio_dev, chan);
496 else
497 return iio_device_add_event_sysfs_new(indio_dev, chan);
498}
499
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100500static inline int __iio_add_event_config_attrs(struct iio_dev *indio_dev)
501{
502 int j, ret, attrcount = 0;
503
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100504 /* Dynically created from the channels array */
505 for (j = 0; j < indio_dev->num_channels; j++) {
506 ret = iio_device_add_event_sysfs(indio_dev,
507 &indio_dev->channels[j]);
508 if (ret < 0)
Julia Lawalle3db9ef2012-10-21 11:52:00 +0100509 return ret;
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100510 attrcount += ret;
511 }
512 return attrcount;
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100513}
514
515static bool iio_check_for_dynamic_events(struct iio_dev *indio_dev)
516{
517 int j;
518
Lars-Peter Clausenb4e3ac02013-10-07 15:11:00 +0100519 for (j = 0; j < indio_dev->num_channels; j++) {
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100520 if (indio_dev->channels[j].event_mask != 0)
521 return true;
Lars-Peter Clausenb4e3ac02013-10-07 15:11:00 +0100522 if (indio_dev->channels[j].num_event_specs != 0)
523 return true;
524 }
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100525 return false;
526}
527
528static void iio_setup_ev_int(struct iio_event_interface *ev_int)
529{
Lars-Peter Clausen2c001932012-01-03 14:59:39 +0100530 INIT_KFIFO(ev_int->det_events);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100531 init_waitqueue_head(&ev_int->wait);
532}
533
534static const char *iio_event_group_name = "events";
535int iio_device_register_eventset(struct iio_dev *indio_dev)
536{
537 struct iio_dev_attr *p;
538 int ret = 0, attrcount_orig = 0, attrcount, attrn;
539 struct attribute **attr;
540
541 if (!(indio_dev->info->event_attrs ||
542 iio_check_for_dynamic_events(indio_dev)))
543 return 0;
544
545 indio_dev->event_interface =
546 kzalloc(sizeof(struct iio_event_interface), GFP_KERNEL);
547 if (indio_dev->event_interface == NULL) {
548 ret = -ENOMEM;
549 goto error_ret;
550 }
551
Sascha Hauer46b24312012-07-03 10:55:40 +0200552 INIT_LIST_HEAD(&indio_dev->event_interface->dev_attr_list);
553
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100554 iio_setup_ev_int(indio_dev->event_interface);
555 if (indio_dev->info->event_attrs != NULL) {
556 attr = indio_dev->info->event_attrs->attrs;
557 while (*attr++ != NULL)
558 attrcount_orig++;
559 }
560 attrcount = attrcount_orig;
561 if (indio_dev->channels) {
562 ret = __iio_add_event_config_attrs(indio_dev);
563 if (ret < 0)
564 goto error_free_setup_event_lines;
565 attrcount += ret;
566 }
567
568 indio_dev->event_interface->group.name = iio_event_group_name;
569 indio_dev->event_interface->group.attrs = kcalloc(attrcount + 1,
570 sizeof(indio_dev->event_interface->group.attrs[0]),
571 GFP_KERNEL);
572 if (indio_dev->event_interface->group.attrs == NULL) {
573 ret = -ENOMEM;
574 goto error_free_setup_event_lines;
575 }
576 if (indio_dev->info->event_attrs)
577 memcpy(indio_dev->event_interface->group.attrs,
578 indio_dev->info->event_attrs->attrs,
579 sizeof(indio_dev->event_interface->group.attrs[0])
580 *attrcount_orig);
581 attrn = attrcount_orig;
582 /* Add all elements from the list. */
583 list_for_each_entry(p,
584 &indio_dev->event_interface->dev_attr_list,
585 l)
586 indio_dev->event_interface->group.attrs[attrn++] =
587 &p->dev_attr.attr;
588 indio_dev->groups[indio_dev->groupcounter++] =
589 &indio_dev->event_interface->group;
590
591 return 0;
592
593error_free_setup_event_lines:
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100594 iio_free_chan_devattr_list(&indio_dev->event_interface->dev_attr_list);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100595 kfree(indio_dev->event_interface);
596error_ret:
597
598 return ret;
599}
600
Lars-Peter Clausend2f0a482013-10-04 12:07:00 +0100601/**
602 * iio_device_wakeup_eventset - Wakes up the event waitqueue
603 * @indio_dev: The IIO device
604 *
605 * Wakes up the event waitqueue used for poll() and blocking read().
606 * Should usually be called when the device is unregistered.
607 */
608void iio_device_wakeup_eventset(struct iio_dev *indio_dev)
609{
610 if (indio_dev->event_interface == NULL)
611 return;
612 wake_up(&indio_dev->event_interface->wait);
613}
614
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100615void iio_device_unregister_eventset(struct iio_dev *indio_dev)
616{
617 if (indio_dev->event_interface == NULL)
618 return;
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100619 iio_free_chan_devattr_list(&indio_dev->event_interface->dev_attr_list);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100620 kfree(indio_dev->event_interface->group.attrs);
621 kfree(indio_dev->event_interface);
622}