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