blob: b7a5d7cbed42c4089d70b0eef8d270ec7b956ca5 [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{
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",
207};
208
209static enum iio_event_direction iio_ev_attr_dir(struct iio_dev_attr *attr)
210{
211 return attr->c->event_spec[attr->address & 0xffff].dir;
212}
213
214static enum iio_event_type iio_ev_attr_type(struct iio_dev_attr *attr)
215{
216 return attr->c->event_spec[attr->address & 0xffff].type;
217}
218
219static enum iio_event_info iio_ev_attr_info(struct iio_dev_attr *attr)
220{
221 return (attr->address >> 16) & 0xffff;
222}
223
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100224static ssize_t iio_ev_state_store(struct device *dev,
225 struct device_attribute *attr,
226 const char *buf,
227 size_t len)
228{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200229 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100230 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
231 int ret;
232 bool val;
233
234 ret = strtobool(buf, &val);
235 if (ret < 0)
236 return ret;
237
Lars-Peter Clausenb4e3ac02013-10-07 15:11:00 +0100238 if (indio_dev->info->write_event_config)
239 ret = indio_dev->info->write_event_config(indio_dev,
240 this_attr->address, val);
241 else
242 ret = indio_dev->info->write_event_config_new(indio_dev,
243 this_attr->c, iio_ev_attr_type(this_attr),
244 iio_ev_attr_dir(this_attr), val);
245
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100246 return (ret < 0) ? ret : len;
247}
248
249static ssize_t iio_ev_state_show(struct device *dev,
250 struct device_attribute *attr,
251 char *buf)
252{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200253 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100254 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
Lars-Peter Clausenb4e3ac02013-10-07 15:11:00 +0100255 int val;
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100256
Lars-Peter Clausenb4e3ac02013-10-07 15:11:00 +0100257 if (indio_dev->info->read_event_config)
258 val = indio_dev->info->read_event_config(indio_dev,
259 this_attr->address);
260 else
261 val = indio_dev->info->read_event_config_new(indio_dev,
262 this_attr->c, iio_ev_attr_type(this_attr),
263 iio_ev_attr_dir(this_attr));
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100264 if (val < 0)
265 return val;
266 else
267 return sprintf(buf, "%d\n", val);
268}
269
270static ssize_t iio_ev_value_show(struct device *dev,
271 struct device_attribute *attr,
272 char *buf)
273{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200274 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100275 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
Lars-Peter Clausenb4e3ac02013-10-07 15:11:00 +0100276 int val, val2;
277 int ret;
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100278
Lars-Peter Clausenb4e3ac02013-10-07 15:11:00 +0100279 if (indio_dev->info->read_event_value) {
280 ret = indio_dev->info->read_event_value(indio_dev,
281 this_attr->address, &val);
282 if (ret < 0)
283 return ret;
284 return sprintf(buf, "%d\n", val);
285 } else {
286 ret = indio_dev->info->read_event_value_new(indio_dev,
287 this_attr->c, iio_ev_attr_type(this_attr),
288 iio_ev_attr_dir(this_attr), iio_ev_attr_info(this_attr),
289 &val, &val2);
290 if (ret < 0)
291 return ret;
292 return iio_format_value(buf, ret, val, val2);
293 }
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100294}
295
296static ssize_t iio_ev_value_store(struct device *dev,
297 struct device_attribute *attr,
298 const char *buf,
299 size_t len)
300{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200301 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100302 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
Lars-Peter Clausenb4e3ac02013-10-07 15:11:00 +0100303 int val, val2;
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100304 int ret;
305
Lars-Peter Clausenb4e3ac02013-10-07 15:11:00 +0100306 if (!indio_dev->info->write_event_value &&
307 !indio_dev->info->write_event_value_new)
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100308 return -EINVAL;
309
Lars-Peter Clausenb4e3ac02013-10-07 15:11:00 +0100310 if (indio_dev->info->write_event_value) {
311 ret = kstrtoint(buf, 10, &val);
312 if (ret)
313 return ret;
314 ret = indio_dev->info->write_event_value(indio_dev,
315 this_attr->address, val);
316 } else {
317 ret = iio_str_to_fixpoint(buf, 100000, &val, &val2);
318 if (ret)
319 return ret;
320 ret = indio_dev->info->write_event_value_new(indio_dev,
321 this_attr->c, iio_ev_attr_type(this_attr),
322 iio_ev_attr_dir(this_attr), iio_ev_attr_info(this_attr),
323 val, val2);
324 }
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100325 if (ret < 0)
326 return ret;
327
328 return len;
329}
330
Lars-Peter Clausenb4e3ac02013-10-07 15:11:00 +0100331static int iio_device_add_event(struct iio_dev *indio_dev,
332 const struct iio_chan_spec *chan, unsigned int spec_index,
333 enum iio_event_type type, enum iio_event_direction dir,
334 enum iio_shared_by shared_by, const unsigned long *mask)
335{
336 ssize_t (*show)(struct device *, struct device_attribute *, char *);
337 ssize_t (*store)(struct device *, struct device_attribute *,
338 const char *, size_t);
339 unsigned int attrcount = 0;
340 unsigned int i;
341 char *postfix;
342 int ret;
343
344 for_each_set_bit(i, mask, sizeof(*mask)) {
345 postfix = kasprintf(GFP_KERNEL, "%s_%s_%s",
346 iio_ev_type_text[type], iio_ev_dir_text[dir],
347 iio_ev_info_text[i]);
348 if (postfix == NULL)
349 return -ENOMEM;
350
351 if (i == IIO_EV_INFO_ENABLE) {
352 show = iio_ev_state_show;
353 store = iio_ev_state_store;
354 } else {
355 show = iio_ev_value_show;
356 store = iio_ev_value_store;
357 }
358
359 ret = __iio_add_chan_devattr(postfix, chan, show, store,
360 (i << 16) | spec_index, shared_by, &indio_dev->dev,
361 &indio_dev->event_interface->dev_attr_list);
362 kfree(postfix);
363
364 if (ret)
365 return ret;
366
367 attrcount++;
368 }
369
370 return attrcount;
371}
372
373static int iio_device_add_event_sysfs_new(struct iio_dev *indio_dev,
374 struct iio_chan_spec const *chan)
375{
376 int ret = 0, i, attrcount = 0;
377 enum iio_event_direction dir;
378 enum iio_event_type type;
379
380 for (i = 0; i < chan->num_event_specs; i++) {
381 type = chan->event_spec[i].type;
382 dir = chan->event_spec[i].dir;
383
384 ret = iio_device_add_event(indio_dev, chan, i, type, dir,
385 IIO_SEPARATE, &chan->event_spec[i].mask_separate);
386 if (ret < 0)
387 goto error_ret;
388 attrcount += ret;
389
390 ret = iio_device_add_event(indio_dev, chan, i, type, dir,
391 IIO_SHARED_BY_TYPE,
392 &chan->event_spec[i].mask_shared_by_type);
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_DIR,
399 &chan->event_spec[i].mask_shared_by_dir);
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_ALL,
406 &chan->event_spec[i].mask_shared_by_all);
407 if (ret < 0)
408 goto error_ret;
409 attrcount += ret;
410 }
411 ret = attrcount;
412error_ret:
413 return ret;
414}
415
416static int iio_device_add_event_sysfs_old(struct iio_dev *indio_dev,
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100417 struct iio_chan_spec const *chan)
418{
419 int ret = 0, i, attrcount = 0;
420 u64 mask = 0;
421 char *postfix;
422 if (!chan->event_mask)
423 return 0;
424
425 for_each_set_bit(i, &chan->event_mask, sizeof(chan->event_mask)*8) {
426 postfix = kasprintf(GFP_KERNEL, "%s_%s_en",
427 iio_ev_type_text[i/IIO_EV_DIR_MAX],
428 iio_ev_dir_text[i%IIO_EV_DIR_MAX]);
429 if (postfix == NULL) {
430 ret = -ENOMEM;
431 goto error_ret;
432 }
433 if (chan->modified)
Lukasz Czerwinski2b0774d2013-09-18 10:21:00 +0100434 mask = IIO_MOD_EVENT_CODE(chan->type, 0, chan->channel2,
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100435 i/IIO_EV_DIR_MAX,
436 i%IIO_EV_DIR_MAX);
437 else if (chan->differential)
438 mask = IIO_EVENT_CODE(chan->type,
439 0, 0,
440 i%IIO_EV_DIR_MAX,
441 i/IIO_EV_DIR_MAX,
442 0,
443 chan->channel,
444 chan->channel2);
445 else
446 mask = IIO_UNMOD_EVENT_CODE(chan->type,
447 chan->channel,
448 i/IIO_EV_DIR_MAX,
449 i%IIO_EV_DIR_MAX);
450
451 ret = __iio_add_chan_devattr(postfix,
452 chan,
453 &iio_ev_state_show,
454 iio_ev_state_store,
455 mask,
456 0,
457 &indio_dev->dev,
458 &indio_dev->event_interface->
459 dev_attr_list);
460 kfree(postfix);
461 if (ret)
462 goto error_ret;
463 attrcount++;
464 postfix = kasprintf(GFP_KERNEL, "%s_%s_value",
465 iio_ev_type_text[i/IIO_EV_DIR_MAX],
466 iio_ev_dir_text[i%IIO_EV_DIR_MAX]);
467 if (postfix == NULL) {
468 ret = -ENOMEM;
469 goto error_ret;
470 }
471 ret = __iio_add_chan_devattr(postfix, chan,
472 iio_ev_value_show,
473 iio_ev_value_store,
474 mask,
475 0,
476 &indio_dev->dev,
477 &indio_dev->event_interface->
478 dev_attr_list);
479 kfree(postfix);
480 if (ret)
481 goto error_ret;
482 attrcount++;
483 }
484 ret = attrcount;
485error_ret:
486 return ret;
487}
488
Lars-Peter Clausenb4e3ac02013-10-07 15:11:00 +0100489
490static int iio_device_add_event_sysfs(struct iio_dev *indio_dev,
491 struct iio_chan_spec const *chan)
492{
493 if (chan->event_mask)
494 return iio_device_add_event_sysfs_old(indio_dev, chan);
495 else
496 return iio_device_add_event_sysfs_new(indio_dev, chan);
497}
498
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100499static inline int __iio_add_event_config_attrs(struct iio_dev *indio_dev)
500{
501 int j, ret, attrcount = 0;
502
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100503 /* Dynically created from the channels array */
504 for (j = 0; j < indio_dev->num_channels; j++) {
505 ret = iio_device_add_event_sysfs(indio_dev,
506 &indio_dev->channels[j]);
507 if (ret < 0)
Julia Lawalle3db9ef2012-10-21 11:52:00 +0100508 return ret;
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100509 attrcount += ret;
510 }
511 return attrcount;
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100512}
513
514static bool iio_check_for_dynamic_events(struct iio_dev *indio_dev)
515{
516 int j;
517
Lars-Peter Clausenb4e3ac02013-10-07 15:11:00 +0100518 for (j = 0; j < indio_dev->num_channels; j++) {
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100519 if (indio_dev->channels[j].event_mask != 0)
520 return true;
Lars-Peter Clausenb4e3ac02013-10-07 15:11:00 +0100521 if (indio_dev->channels[j].num_event_specs != 0)
522 return true;
523 }
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100524 return false;
525}
526
527static void iio_setup_ev_int(struct iio_event_interface *ev_int)
528{
Lars-Peter Clausen2c001932012-01-03 14:59:39 +0100529 INIT_KFIFO(ev_int->det_events);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100530 init_waitqueue_head(&ev_int->wait);
531}
532
533static const char *iio_event_group_name = "events";
534int iio_device_register_eventset(struct iio_dev *indio_dev)
535{
536 struct iio_dev_attr *p;
537 int ret = 0, attrcount_orig = 0, attrcount, attrn;
538 struct attribute **attr;
539
540 if (!(indio_dev->info->event_attrs ||
541 iio_check_for_dynamic_events(indio_dev)))
542 return 0;
543
544 indio_dev->event_interface =
545 kzalloc(sizeof(struct iio_event_interface), GFP_KERNEL);
546 if (indio_dev->event_interface == NULL) {
547 ret = -ENOMEM;
548 goto error_ret;
549 }
550
Sascha Hauer46b24312012-07-03 10:55:40 +0200551 INIT_LIST_HEAD(&indio_dev->event_interface->dev_attr_list);
552
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100553 iio_setup_ev_int(indio_dev->event_interface);
554 if (indio_dev->info->event_attrs != NULL) {
555 attr = indio_dev->info->event_attrs->attrs;
556 while (*attr++ != NULL)
557 attrcount_orig++;
558 }
559 attrcount = attrcount_orig;
560 if (indio_dev->channels) {
561 ret = __iio_add_event_config_attrs(indio_dev);
562 if (ret < 0)
563 goto error_free_setup_event_lines;
564 attrcount += ret;
565 }
566
567 indio_dev->event_interface->group.name = iio_event_group_name;
568 indio_dev->event_interface->group.attrs = kcalloc(attrcount + 1,
569 sizeof(indio_dev->event_interface->group.attrs[0]),
570 GFP_KERNEL);
571 if (indio_dev->event_interface->group.attrs == NULL) {
572 ret = -ENOMEM;
573 goto error_free_setup_event_lines;
574 }
575 if (indio_dev->info->event_attrs)
576 memcpy(indio_dev->event_interface->group.attrs,
577 indio_dev->info->event_attrs->attrs,
578 sizeof(indio_dev->event_interface->group.attrs[0])
579 *attrcount_orig);
580 attrn = attrcount_orig;
581 /* Add all elements from the list. */
582 list_for_each_entry(p,
583 &indio_dev->event_interface->dev_attr_list,
584 l)
585 indio_dev->event_interface->group.attrs[attrn++] =
586 &p->dev_attr.attr;
587 indio_dev->groups[indio_dev->groupcounter++] =
588 &indio_dev->event_interface->group;
589
590 return 0;
591
592error_free_setup_event_lines:
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100593 iio_free_chan_devattr_list(&indio_dev->event_interface->dev_attr_list);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100594 kfree(indio_dev->event_interface);
595error_ret:
596
597 return ret;
598}
599
Lars-Peter Clausend2f0a482013-10-04 12:07:00 +0100600/**
601 * iio_device_wakeup_eventset - Wakes up the event waitqueue
602 * @indio_dev: The IIO device
603 *
604 * Wakes up the event waitqueue used for poll() and blocking read().
605 * Should usually be called when the device is unregistered.
606 */
607void iio_device_wakeup_eventset(struct iio_dev *indio_dev)
608{
609 if (indio_dev->event_interface == NULL)
610 return;
611 wake_up(&indio_dev->event_interface->wait);
612}
613
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100614void iio_device_unregister_eventset(struct iio_dev *indio_dev)
615{
616 if (indio_dev->event_interface == NULL)
617 return;
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100618 iio_free_chan_devattr_list(&indio_dev->event_interface->dev_attr_list);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100619 kfree(indio_dev->event_interface->group.attrs);
620 kfree(indio_dev->event_interface);
621}