blob: 32b2c2d8d2376a7e200db2cbd1e25a17b3ebdb60 [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
Sachin Kamata7e57dc2013-10-29 11:39:00 +000045/**
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 **/
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +010051int 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;
Lars-Peter Clausen2c001932012-01-03 14:59:39 +010054 struct iio_event_data ev;
Lars-Peter Clausen1b9dc912013-03-07 19:53:00 +000055 unsigned long flags;
Lars-Peter Clausen2c001932012-01-03 14:59:39 +010056 int copied;
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +010057
58 /* Does anyone care? */
Lars-Peter Clausen1b9dc912013-03-07 19:53:00 +000059 spin_lock_irqsave(&ev_int->wait.lock, flags);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +010060 if (test_bit(IIO_BUSY_BIT_POS, &ev_int->flags)) {
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +010061
Lars-Peter Clausen2c001932012-01-03 14:59:39 +010062 ev.id = ev_code;
63 ev.timestamp = timestamp;
64
65 copied = kfifo_put(&ev_int->det_events, &ev);
Lars-Peter Clausen2c001932012-01-03 14:59:39 +010066 if (copied != 0)
Lars-Peter Clausene18045e2012-01-03 14:59:41 +010067 wake_up_locked_poll(&ev_int->wait, POLLIN);
Lars-Peter Clausen43ba1102012-01-03 14:59:40 +010068 }
Lars-Peter Clausen1b9dc912013-03-07 19:53:00 +000069 spin_unlock_irqrestore(&ev_int->wait.lock, flags);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +010070
Lars-Peter Clausen2c001932012-01-03 14:59:39 +010071 return 0;
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +010072}
73EXPORT_SYMBOL(iio_push_event);
74
Lars-Peter Clausene18045e2012-01-03 14:59:41 +010075/**
76 * iio_event_poll() - poll the event queue to find out if it has data
77 */
78static unsigned int iio_event_poll(struct file *filep,
79 struct poll_table_struct *wait)
80{
Lars-Peter Clausencadc2122013-09-18 21:02:00 +010081 struct iio_dev *indio_dev = filep->private_data;
82 struct iio_event_interface *ev_int = indio_dev->event_interface;
Lars-Peter Clausene18045e2012-01-03 14:59:41 +010083 unsigned int events = 0;
84
Lars-Peter Clausenf18e7a02013-10-04 12:06:00 +010085 if (!indio_dev->info)
86 return -ENODEV;
87
Lars-Peter Clausene18045e2012-01-03 14:59:41 +010088 poll_wait(filep, &ev_int->wait, wait);
89
Lars-Peter Clausen1b9dc912013-03-07 19:53:00 +000090 spin_lock_irq(&ev_int->wait.lock);
Lars-Peter Clausene18045e2012-01-03 14:59:41 +010091 if (!kfifo_is_empty(&ev_int->det_events))
92 events = POLLIN | POLLRDNORM;
Lars-Peter Clausen1b9dc912013-03-07 19:53:00 +000093 spin_unlock_irq(&ev_int->wait.lock);
Lars-Peter Clausene18045e2012-01-03 14:59:41 +010094
95 return events;
96}
97
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +010098static ssize_t iio_event_chrdev_read(struct file *filep,
99 char __user *buf,
100 size_t count,
101 loff_t *f_ps)
102{
Lars-Peter Clausencadc2122013-09-18 21:02:00 +0100103 struct iio_dev *indio_dev = filep->private_data;
104 struct iio_event_interface *ev_int = indio_dev->event_interface;
Lars-Peter Clausen2c001932012-01-03 14:59:39 +0100105 unsigned int copied;
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100106 int ret;
107
Lars-Peter Clausenf18e7a02013-10-04 12:06:00 +0100108 if (!indio_dev->info)
109 return -ENODEV;
110
Lars-Peter Clausen2c001932012-01-03 14:59:39 +0100111 if (count < sizeof(struct iio_event_data))
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100112 return -EINVAL;
113
Lars-Peter Clausen1b9dc912013-03-07 19:53:00 +0000114 spin_lock_irq(&ev_int->wait.lock);
Lars-Peter Clausen2c001932012-01-03 14:59:39 +0100115 if (kfifo_is_empty(&ev_int->det_events)) {
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100116 if (filep->f_flags & O_NONBLOCK) {
117 ret = -EAGAIN;
Lars-Peter Clausen43ba1102012-01-03 14:59:40 +0100118 goto error_unlock;
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100119 }
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100120 /* Blocking on device; waiting for something to be there */
Lars-Peter Clausen1b9dc912013-03-07 19:53:00 +0000121 ret = wait_event_interruptible_locked_irq(ev_int->wait,
Lars-Peter Clausend2f0a482013-10-04 12:07:00 +0100122 !kfifo_is_empty(&ev_int->det_events) ||
123 indio_dev->info == NULL);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100124 if (ret)
Lars-Peter Clausen43ba1102012-01-03 14:59:40 +0100125 goto error_unlock;
Lars-Peter Clausend2f0a482013-10-04 12:07:00 +0100126 if (indio_dev->info == NULL) {
127 ret = -ENODEV;
128 goto error_unlock;
129 }
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100130 /* Single access device so no one else can get the data */
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100131 }
132
Lars-Peter Clausen2c001932012-01-03 14:59:39 +0100133 ret = kfifo_to_user(&ev_int->det_events, buf, count, &copied);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100134
Lars-Peter Clausen43ba1102012-01-03 14:59:40 +0100135error_unlock:
Lars-Peter Clausen1b9dc912013-03-07 19:53:00 +0000136 spin_unlock_irq(&ev_int->wait.lock);
Lars-Peter Clausen43ba1102012-01-03 14:59:40 +0100137
Lars-Peter Clausen2c001932012-01-03 14:59:39 +0100138 return ret ? ret : copied;
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100139}
140
141static int iio_event_chrdev_release(struct inode *inode, struct file *filep)
142{
Lars-Peter Clausencadc2122013-09-18 21:02:00 +0100143 struct iio_dev *indio_dev = filep->private_data;
144 struct iio_event_interface *ev_int = indio_dev->event_interface;
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100145
Lars-Peter Clausen1b9dc912013-03-07 19:53:00 +0000146 spin_lock_irq(&ev_int->wait.lock);
Lars-Peter Clausena046c1e2012-01-03 14:59:42 +0100147 __clear_bit(IIO_BUSY_BIT_POS, &ev_int->flags);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100148 /*
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 */
Lars-Peter Clausen2c001932012-01-03 14:59:39 +0100153 kfifo_reset_out(&ev_int->det_events);
Lars-Peter Clausen1b9dc912013-03-07 19:53:00 +0000154 spin_unlock_irq(&ev_int->wait.lock);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100155
Lars-Peter Clausencadc2122013-09-18 21:02:00 +0100156 iio_device_put(indio_dev);
157
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100158 return 0;
159}
160
161static const struct file_operations iio_event_chrdev_fileops = {
162 .read = iio_event_chrdev_read,
Lars-Peter Clausene18045e2012-01-03 14:59:41 +0100163 .poll = iio_event_poll,
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100164 .release = iio_event_chrdev_release,
165 .owner = THIS_MODULE,
166 .llseek = noop_llseek,
167};
168
169int 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
Lars-Peter Clausen1b9dc912013-03-07 19:53:00 +0000177 spin_lock_irq(&ev_int->wait.lock);
Lars-Peter Clausena046c1e2012-01-03 14:59:42 +0100178 if (__test_and_set_bit(IIO_BUSY_BIT_POS, &ev_int->flags)) {
Lars-Peter Clausen1b9dc912013-03-07 19:53:00 +0000179 spin_unlock_irq(&ev_int->wait.lock);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100180 return -EBUSY;
181 }
Lars-Peter Clausen1b9dc912013-03-07 19:53:00 +0000182 spin_unlock_irq(&ev_int->wait.lock);
Lars-Peter Clausencadc2122013-09-18 21:02:00 +0100183 iio_device_get(indio_dev);
184
185 fd = anon_inode_getfd("iio:event", &iio_event_chrdev_fileops,
Greg Kroah-Hartmane2aad1d2013-09-25 08:59:04 -0700186 indio_dev, O_RDONLY | O_CLOEXEC);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100187 if (fd < 0) {
Lars-Peter Clausen1b9dc912013-03-07 19:53:00 +0000188 spin_lock_irq(&ev_int->wait.lock);
Lars-Peter Clausena046c1e2012-01-03 14:59:42 +0100189 __clear_bit(IIO_BUSY_BIT_POS, &ev_int->flags);
Lars-Peter Clausen1b9dc912013-03-07 19:53:00 +0000190 spin_unlock_irq(&ev_int->wait.lock);
Lars-Peter Clausencadc2122013-09-18 21:02:00 +0100191 iio_device_put(indio_dev);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100192 }
193 return fd;
194}
195
196static 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
204static 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
Lars-Peter Clausenb4e3ac02013-10-07 15:11:00 +0100210static const char * const iio_ev_info_text[] = {
211 [IIO_EV_INFO_ENABLE] = "en",
212 [IIO_EV_INFO_VALUE] = "value",
Lars-Peter Clausenec6670a2013-10-07 15:11:00 +0100213 [IIO_EV_INFO_HYSTERESIS] = "hysteresis",
Lars-Peter Clausenb4e3ac02013-10-07 15:11:00 +0100214};
215
216static 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
221static 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
226static enum iio_event_info iio_ev_attr_info(struct iio_dev_attr *attr)
227{
228 return (attr->address >> 16) & 0xffff;
229}
230
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100231static ssize_t iio_ev_state_store(struct device *dev,
232 struct device_attribute *attr,
233 const char *buf,
234 size_t len)
235{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200236 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100237 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
Lars-Peter Clausenb4e3ac02013-10-07 15:11:00 +0100245 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
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100253 return (ret < 0) ? ret : len;
254}
255
256static ssize_t iio_ev_state_show(struct device *dev,
257 struct device_attribute *attr,
258 char *buf)
259{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200260 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100261 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
Lars-Peter Clausenb4e3ac02013-10-07 15:11:00 +0100262 int val;
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100263
Lars-Peter Clausenb4e3ac02013-10-07 15:11:00 +0100264 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));
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100271 if (val < 0)
272 return val;
273 else
274 return sprintf(buf, "%d\n", val);
275}
276
277static ssize_t iio_ev_value_show(struct device *dev,
278 struct device_attribute *attr,
279 char *buf)
280{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200281 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100282 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
Lars-Peter Clausenb4e3ac02013-10-07 15:11:00 +0100283 int val, val2;
284 int ret;
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100285
Lars-Peter Clausenb4e3ac02013-10-07 15:11:00 +0100286 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 }
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100301}
302
303static ssize_t iio_ev_value_store(struct device *dev,
304 struct device_attribute *attr,
305 const char *buf,
306 size_t len)
307{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200308 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100309 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
Lars-Peter Clausenb4e3ac02013-10-07 15:11:00 +0100310 int val, val2;
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100311 int ret;
312
Lars-Peter Clausenb4e3ac02013-10-07 15:11:00 +0100313 if (!indio_dev->info->write_event_value &&
314 !indio_dev->info->write_event_value_new)
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100315 return -EINVAL;
316
Lars-Peter Clausenb4e3ac02013-10-07 15:11:00 +0100317 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 }
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100332 if (ret < 0)
333 return ret;
334
335 return len;
336}
337
Lars-Peter Clausenb4e3ac02013-10-07 15:11:00 +0100338static 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
380static 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;
419error_ret:
420 return ret;
421}
422
423static int iio_device_add_event_sysfs_old(struct iio_dev *indio_dev,
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100424 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)
Lukasz Czerwinski2b0774d2013-09-18 10:21:00 +0100441 mask = IIO_MOD_EVENT_CODE(chan->type, 0, chan->channel2,
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100442 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;
492error_ret:
493 return ret;
494}
495
Lars-Peter Clausenb4e3ac02013-10-07 15:11:00 +0100496
497static 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
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100506static inline int __iio_add_event_config_attrs(struct iio_dev *indio_dev)
507{
508 int j, ret, attrcount = 0;
509
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100510 /* 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)
Julia Lawalle3db9ef2012-10-21 11:52:00 +0100515 return ret;
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100516 attrcount += ret;
517 }
518 return attrcount;
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100519}
520
521static bool iio_check_for_dynamic_events(struct iio_dev *indio_dev)
522{
523 int j;
524
Lars-Peter Clausenb4e3ac02013-10-07 15:11:00 +0100525 for (j = 0; j < indio_dev->num_channels; j++) {
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100526 if (indio_dev->channels[j].event_mask != 0)
527 return true;
Lars-Peter Clausenb4e3ac02013-10-07 15:11:00 +0100528 if (indio_dev->channels[j].num_event_specs != 0)
529 return true;
530 }
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100531 return false;
532}
533
534static void iio_setup_ev_int(struct iio_event_interface *ev_int)
535{
Lars-Peter Clausen2c001932012-01-03 14:59:39 +0100536 INIT_KFIFO(ev_int->det_events);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100537 init_waitqueue_head(&ev_int->wait);
538}
539
540static const char *iio_event_group_name = "events";
541int 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
Sascha Hauer46b24312012-07-03 10:55:40 +0200558 INIT_LIST_HEAD(&indio_dev->event_interface->dev_attr_list);
559
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100560 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
599error_free_setup_event_lines:
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100600 iio_free_chan_devattr_list(&indio_dev->event_interface->dev_attr_list);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100601 kfree(indio_dev->event_interface);
602error_ret:
603
604 return ret;
605}
606
Lars-Peter Clausend2f0a482013-10-04 12:07:00 +0100607/**
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 */
614void 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
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100621void iio_device_unregister_eventset(struct iio_dev *indio_dev)
622{
623 if (indio_dev->event_interface == NULL)
624 return;
Lars-Peter Clausen84088eb2013-10-07 12:50:00 +0100625 iio_free_chan_devattr_list(&indio_dev->event_interface->dev_attr_list);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100626 kfree(indio_dev->event_interface->group.attrs);
627 kfree(indio_dev->event_interface);
628}