1 /* Industrial I/O event handling
3 * Copyright (c) 2008 Jonathan Cameron
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.
9 * Based on elements of hwmon and input subsystems.
12 #include <linux/anon_inodes.h>
13 #include <linux/device.h>
15 #include <linux/kernel.h>
16 #include <linux/kfifo.h>
17 #include <linux/module.h>
18 #include <linux/poll.h>
19 #include <linux/sched.h>
20 #include <linux/slab.h>
21 #include <linux/uaccess.h>
22 #include <linux/wait.h>
23 #include <linux/iio/iio.h>
25 #include <linux/iio/sysfs.h>
26 #include <linux/iio/events.h>
29 * struct iio_event_interface - chrdev interface for an event line
30 * @wait: wait queue to allow blocking reads of events
31 * @det_events: list of detected events
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
36 struct iio_event_interface {
37 wait_queue_head_t wait;
38 DECLARE_KFIFO(det_events, struct iio_event_data, 16);
40 struct list_head dev_attr_list;
42 struct attribute_group group;
45 int iio_push_event(struct iio_dev *indio_dev, u64 ev_code, s64 timestamp)
47 struct iio_event_interface *ev_int = indio_dev->event_interface;
48 struct iio_event_data ev;
52 /* Does anyone care? */
53 spin_lock_irqsave(&ev_int->wait.lock, flags);
54 if (test_bit(IIO_BUSY_BIT_POS, &ev_int->flags)) {
57 ev.timestamp = timestamp;
59 copied = kfifo_put(&ev_int->det_events, &ev);
61 wake_up_locked_poll(&ev_int->wait, POLLIN);
63 spin_unlock_irqrestore(&ev_int->wait.lock, flags);
67 EXPORT_SYMBOL(iio_push_event);
70 * iio_event_poll() - poll the event queue to find out if it has data
72 static unsigned int iio_event_poll(struct file *filep,
73 struct poll_table_struct *wait)
75 struct iio_event_interface *ev_int = filep->private_data;
76 unsigned int events = 0;
78 poll_wait(filep, &ev_int->wait, wait);
80 spin_lock_irq(&ev_int->wait.lock);
81 if (!kfifo_is_empty(&ev_int->det_events))
82 events = POLLIN | POLLRDNORM;
83 spin_unlock_irq(&ev_int->wait.lock);
88 static ssize_t iio_event_chrdev_read(struct file *filep,
93 struct iio_event_interface *ev_int = filep->private_data;
97 if (count < sizeof(struct iio_event_data))
100 spin_lock_irq(&ev_int->wait.lock);
101 if (kfifo_is_empty(&ev_int->det_events)) {
102 if (filep->f_flags & O_NONBLOCK) {
106 /* Blocking on device; waiting for something to be there */
107 ret = wait_event_interruptible_locked_irq(ev_int->wait,
108 !kfifo_is_empty(&ev_int->det_events));
111 /* Single access device so no one else can get the data */
114 ret = kfifo_to_user(&ev_int->det_events, buf, count, &copied);
117 spin_unlock_irq(&ev_int->wait.lock);
119 return ret ? ret : copied;
122 static int iio_event_chrdev_release(struct inode *inode, struct file *filep)
124 struct iio_event_interface *ev_int = filep->private_data;
126 spin_lock_irq(&ev_int->wait.lock);
127 __clear_bit(IIO_BUSY_BIT_POS, &ev_int->flags);
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.
133 kfifo_reset_out(&ev_int->det_events);
134 spin_unlock_irq(&ev_int->wait.lock);
139 static const struct file_operations iio_event_chrdev_fileops = {
140 .read = iio_event_chrdev_read,
141 .poll = iio_event_poll,
142 .release = iio_event_chrdev_release,
143 .owner = THIS_MODULE,
144 .llseek = noop_llseek,
147 int iio_event_getfd(struct iio_dev *indio_dev)
149 struct iio_event_interface *ev_int = indio_dev->event_interface;
155 spin_lock_irq(&ev_int->wait.lock);
156 if (__test_and_set_bit(IIO_BUSY_BIT_POS, &ev_int->flags)) {
157 spin_unlock_irq(&ev_int->wait.lock);
160 spin_unlock_irq(&ev_int->wait.lock);
161 fd = anon_inode_getfd("iio:event",
162 &iio_event_chrdev_fileops, ev_int, O_RDONLY);
164 spin_lock_irq(&ev_int->wait.lock);
165 __clear_bit(IIO_BUSY_BIT_POS, &ev_int->flags);
166 spin_unlock_irq(&ev_int->wait.lock);
171 static 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",
179 static 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"
185 static ssize_t iio_ev_state_store(struct device *dev,
186 struct device_attribute *attr,
190 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
191 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
195 ret = strtobool(buf, &val);
199 ret = indio_dev->info->write_event_config(indio_dev,
202 return (ret < 0) ? ret : len;
205 static ssize_t iio_ev_state_show(struct device *dev,
206 struct device_attribute *attr,
209 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
210 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
211 int val = indio_dev->info->read_event_config(indio_dev,
217 return sprintf(buf, "%d\n", val);
220 static ssize_t iio_ev_value_show(struct device *dev,
221 struct device_attribute *attr,
224 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
225 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
228 ret = indio_dev->info->read_event_value(indio_dev,
229 this_attr->address, &val);
233 return sprintf(buf, "%d\n", val);
236 static ssize_t iio_ev_value_store(struct device *dev,
237 struct device_attribute *attr,
241 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
242 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
246 if (!indio_dev->info->write_event_value)
249 ret = kstrtoint(buf, 10, &val);
253 ret = indio_dev->info->write_event_value(indio_dev, this_attr->address,
261 static int iio_device_add_event_sysfs(struct iio_dev *indio_dev,
262 struct iio_chan_spec const *chan)
264 int ret = 0, i, attrcount = 0;
267 if (!chan->event_mask)
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) {
279 mask = IIO_MOD_EVENT_CODE(chan->type, 0, chan->channel,
282 else if (chan->differential)
283 mask = IIO_EVENT_CODE(chan->type,
291 mask = IIO_UNMOD_EVENT_CODE(chan->type,
296 ret = __iio_add_chan_devattr(postfix,
303 &indio_dev->event_interface->
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) {
316 ret = __iio_add_chan_devattr(postfix, chan,
322 &indio_dev->event_interface->
334 static inline void __iio_remove_event_config_attrs(struct iio_dev *indio_dev)
336 struct iio_dev_attr *p, *n;
337 list_for_each_entry_safe(p, n,
338 &indio_dev->event_interface->
340 kfree(p->dev_attr.attr.name);
345 static inline int __iio_add_event_config_attrs(struct iio_dev *indio_dev)
347 int j, ret, attrcount = 0;
349 /* 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]);
360 static bool iio_check_for_dynamic_events(struct iio_dev *indio_dev)
364 for (j = 0; j < indio_dev->num_channels; j++)
365 if (indio_dev->channels[j].event_mask != 0)
370 static void iio_setup_ev_int(struct iio_event_interface *ev_int)
372 INIT_KFIFO(ev_int->det_events);
373 init_waitqueue_head(&ev_int->wait);
376 static const char *iio_event_group_name = "events";
377 int iio_device_register_eventset(struct iio_dev *indio_dev)
379 struct iio_dev_attr *p;
380 int ret = 0, attrcount_orig = 0, attrcount, attrn;
381 struct attribute **attr;
383 if (!(indio_dev->info->event_attrs ||
384 iio_check_for_dynamic_events(indio_dev)))
387 indio_dev->event_interface =
388 kzalloc(sizeof(struct iio_event_interface), GFP_KERNEL);
389 if (indio_dev->event_interface == NULL) {
394 INIT_LIST_HEAD(&indio_dev->event_interface->dev_attr_list);
396 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)
402 attrcount = attrcount_orig;
403 if (indio_dev->channels) {
404 ret = __iio_add_event_config_attrs(indio_dev);
406 goto error_free_setup_event_lines;
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]),
414 if (indio_dev->event_interface->group.attrs == NULL) {
416 goto error_free_setup_event_lines;
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])
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,
428 indio_dev->event_interface->group.attrs[attrn++] =
430 indio_dev->groups[indio_dev->groupcounter++] =
431 &indio_dev->event_interface->group;
435 error_free_setup_event_lines:
436 __iio_remove_event_config_attrs(indio_dev);
437 kfree(indio_dev->event_interface);
443 void iio_device_unregister_eventset(struct iio_dev *indio_dev)
445 if (indio_dev->event_interface == NULL)
447 __iio_remove_event_config_attrs(indio_dev);
448 kfree(indio_dev->event_interface->group.attrs);
449 kfree(indio_dev->event_interface);