1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Industrial I/O event handling
3 *
4 * Copyright (c) 2008 Jonathan Cameron
5 *
6 * Based on elements of hwmon and input subsystems.
7 */
8
9 #include <linux/anon_inodes.h>
10 #include <linux/device.h>
11 #include <linux/fs.h>
12 #include <linux/kernel.h>
13 #include <linux/kfifo.h>
14 #include <linux/module.h>
15 #include <linux/poll.h>
16 #include <linux/sched.h>
17 #include <linux/slab.h>
18 #include <linux/uaccess.h>
19 #include <linux/wait.h>
20 #include <linux/iio/iio.h>
21 #include <linux/iio/iio-opaque.h>
22 #include "iio_core.h"
23 #include <linux/iio/sysfs.h>
24 #include <linux/iio/events.h>
25
26 /**
27 * struct iio_event_interface - chrdev interface for an event line
28 * @wait: wait queue to allow blocking reads of events
29 * @det_events: list of detected events
30 * @dev_attr_list: list of event interface sysfs attribute
31 * @flags: file operations related flags including busy flag.
32 * @group: event interface sysfs attribute group
33 * @read_lock: lock to protect kfifo read operations
34 */
35 struct iio_event_interface {
36 wait_queue_head_t wait;
37 DECLARE_KFIFO(det_events, struct iio_event_data, 16);
38
39 struct list_head dev_attr_list;
40 unsigned long flags;
41 struct attribute_group group;
42 struct mutex read_lock;
43 };
44
iio_event_enabled(const struct iio_event_interface * ev_int)45 bool iio_event_enabled(const struct iio_event_interface *ev_int)
46 {
47 return !!test_bit(IIO_BUSY_BIT_POS, &ev_int->flags);
48 }
49
50 /**
51 * iio_push_event() - try to add event to the list for userspace reading
52 * @indio_dev: IIO device structure
53 * @ev_code: What event
54 * @timestamp: When the event occurred
55 *
56 * Note: The caller must make sure that this function is not running
57 * concurrently for the same indio_dev more than once.
58 *
59 * This function may be safely used as soon as a valid reference to iio_dev has
60 * been obtained via iio_device_alloc(), but any events that are submitted
61 * before iio_device_register() has successfully completed will be silently
62 * discarded.
63 **/
iio_push_event(struct iio_dev * indio_dev,u64 ev_code,s64 timestamp)64 int iio_push_event(struct iio_dev *indio_dev, u64 ev_code, s64 timestamp)
65 {
66 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
67 struct iio_event_interface *ev_int = iio_dev_opaque->event_interface;
68 struct iio_event_data ev;
69 int copied;
70
71 if (!ev_int)
72 return 0;
73
74 /* Does anyone care? */
75 if (iio_event_enabled(ev_int)) {
76
77 ev.id = ev_code;
78 ev.timestamp = timestamp;
79
80 copied = kfifo_put(&ev_int->det_events, ev);
81 if (copied != 0)
82 wake_up_poll(&ev_int->wait, EPOLLIN);
83 }
84
85 return 0;
86 }
87 EXPORT_SYMBOL(iio_push_event);
88
89 /**
90 * iio_event_poll() - poll the event queue to find out if it has data
91 * @filep: File structure pointer to identify the device
92 * @wait: Poll table pointer to add the wait queue on
93 *
94 * Return: (EPOLLIN | EPOLLRDNORM) if data is available for reading
95 * or a negative error code on failure
96 */
iio_event_poll(struct file * filep,struct poll_table_struct * wait)97 static __poll_t iio_event_poll(struct file *filep,
98 struct poll_table_struct *wait)
99 {
100 struct iio_dev *indio_dev = filep->private_data;
101 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
102 struct iio_event_interface *ev_int = iio_dev_opaque->event_interface;
103 __poll_t events = 0;
104
105 if (!indio_dev->info)
106 return events;
107
108 poll_wait(filep, &ev_int->wait, wait);
109
110 if (!kfifo_is_empty(&ev_int->det_events))
111 events = EPOLLIN | EPOLLRDNORM;
112
113 return events;
114 }
115
iio_event_chrdev_read(struct file * filep,char __user * buf,size_t count,loff_t * f_ps)116 static ssize_t iio_event_chrdev_read(struct file *filep,
117 char __user *buf,
118 size_t count,
119 loff_t *f_ps)
120 {
121 struct iio_dev *indio_dev = filep->private_data;
122 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
123 struct iio_event_interface *ev_int = iio_dev_opaque->event_interface;
124 unsigned int copied;
125 int ret;
126
127 if (!indio_dev->info)
128 return -ENODEV;
129
130 if (count < sizeof(struct iio_event_data))
131 return -EINVAL;
132
133 do {
134 if (kfifo_is_empty(&ev_int->det_events)) {
135 if (filep->f_flags & O_NONBLOCK)
136 return -EAGAIN;
137
138 ret = wait_event_interruptible(ev_int->wait,
139 !kfifo_is_empty(&ev_int->det_events) ||
140 indio_dev->info == NULL);
141 if (ret)
142 return ret;
143 if (indio_dev->info == NULL)
144 return -ENODEV;
145 }
146
147 if (mutex_lock_interruptible(&ev_int->read_lock))
148 return -ERESTARTSYS;
149 ret = kfifo_to_user(&ev_int->det_events, buf, count, &copied);
150 mutex_unlock(&ev_int->read_lock);
151
152 if (ret)
153 return ret;
154
155 /*
156 * If we couldn't read anything from the fifo (a different
157 * thread might have been faster) we either return -EAGAIN if
158 * the file descriptor is non-blocking, otherwise we go back to
159 * sleep and wait for more data to arrive.
160 */
161 if (copied == 0 && (filep->f_flags & O_NONBLOCK))
162 return -EAGAIN;
163
164 } while (copied == 0);
165
166 return copied;
167 }
168
iio_event_chrdev_release(struct inode * inode,struct file * filep)169 static int iio_event_chrdev_release(struct inode *inode, struct file *filep)
170 {
171 struct iio_dev *indio_dev = filep->private_data;
172 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
173 struct iio_event_interface *ev_int = iio_dev_opaque->event_interface;
174
175 clear_bit(IIO_BUSY_BIT_POS, &ev_int->flags);
176
177 iio_device_put(indio_dev);
178
179 return 0;
180 }
181
182 static const struct file_operations iio_event_chrdev_fileops = {
183 .read = iio_event_chrdev_read,
184 .poll = iio_event_poll,
185 .release = iio_event_chrdev_release,
186 .owner = THIS_MODULE,
187 .llseek = noop_llseek,
188 };
189
iio_event_getfd(struct iio_dev * indio_dev)190 int iio_event_getfd(struct iio_dev *indio_dev)
191 {
192 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
193 struct iio_event_interface *ev_int = iio_dev_opaque->event_interface;
194 int fd;
195
196 if (ev_int == NULL)
197 return -ENODEV;
198
199 fd = mutex_lock_interruptible(&indio_dev->mlock);
200 if (fd)
201 return fd;
202
203 if (test_and_set_bit(IIO_BUSY_BIT_POS, &ev_int->flags)) {
204 fd = -EBUSY;
205 goto unlock;
206 }
207
208 iio_device_get(indio_dev);
209
210 fd = anon_inode_getfd("iio:event", &iio_event_chrdev_fileops,
211 indio_dev, O_RDONLY | O_CLOEXEC);
212 if (fd < 0) {
213 clear_bit(IIO_BUSY_BIT_POS, &ev_int->flags);
214 iio_device_put(indio_dev);
215 } else {
216 kfifo_reset_out(&ev_int->det_events);
217 }
218
219 unlock:
220 mutex_unlock(&indio_dev->mlock);
221 return fd;
222 }
223
224 static const char * const iio_ev_type_text[] = {
225 [IIO_EV_TYPE_THRESH] = "thresh",
226 [IIO_EV_TYPE_MAG] = "mag",
227 [IIO_EV_TYPE_ROC] = "roc",
228 [IIO_EV_TYPE_THRESH_ADAPTIVE] = "thresh_adaptive",
229 [IIO_EV_TYPE_MAG_ADAPTIVE] = "mag_adaptive",
230 [IIO_EV_TYPE_CHANGE] = "change",
231 };
232
233 static const char * const iio_ev_dir_text[] = {
234 [IIO_EV_DIR_EITHER] = "either",
235 [IIO_EV_DIR_RISING] = "rising",
236 [IIO_EV_DIR_FALLING] = "falling"
237 };
238
239 static const char * const iio_ev_info_text[] = {
240 [IIO_EV_INFO_ENABLE] = "en",
241 [IIO_EV_INFO_VALUE] = "value",
242 [IIO_EV_INFO_HYSTERESIS] = "hysteresis",
243 [IIO_EV_INFO_PERIOD] = "period",
244 [IIO_EV_INFO_HIGH_PASS_FILTER_3DB] = "high_pass_filter_3db",
245 [IIO_EV_INFO_LOW_PASS_FILTER_3DB] = "low_pass_filter_3db",
246 };
247
iio_ev_attr_dir(struct iio_dev_attr * attr)248 static enum iio_event_direction iio_ev_attr_dir(struct iio_dev_attr *attr)
249 {
250 return attr->c->event_spec[attr->address & 0xffff].dir;
251 }
252
iio_ev_attr_type(struct iio_dev_attr * attr)253 static enum iio_event_type iio_ev_attr_type(struct iio_dev_attr *attr)
254 {
255 return attr->c->event_spec[attr->address & 0xffff].type;
256 }
257
iio_ev_attr_info(struct iio_dev_attr * attr)258 static enum iio_event_info iio_ev_attr_info(struct iio_dev_attr *attr)
259 {
260 return (attr->address >> 16) & 0xffff;
261 }
262
iio_ev_state_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)263 static ssize_t iio_ev_state_store(struct device *dev,
264 struct device_attribute *attr,
265 const char *buf,
266 size_t len)
267 {
268 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
269 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
270 int ret;
271 bool val;
272
273 ret = strtobool(buf, &val);
274 if (ret < 0)
275 return ret;
276
277 if (!indio_dev->info->write_event_config)
278 return -EINVAL;
279
280 ret = indio_dev->info->write_event_config(indio_dev,
281 this_attr->c, iio_ev_attr_type(this_attr),
282 iio_ev_attr_dir(this_attr), val);
283
284 return (ret < 0) ? ret : len;
285 }
286
iio_ev_state_show(struct device * dev,struct device_attribute * attr,char * buf)287 static ssize_t iio_ev_state_show(struct device *dev,
288 struct device_attribute *attr,
289 char *buf)
290 {
291 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
292 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
293 int val;
294
295 if (!indio_dev->info->read_event_config)
296 return -EINVAL;
297
298 val = indio_dev->info->read_event_config(indio_dev,
299 this_attr->c, iio_ev_attr_type(this_attr),
300 iio_ev_attr_dir(this_attr));
301 if (val < 0)
302 return val;
303 else
304 return sprintf(buf, "%d\n", val);
305 }
306
iio_ev_value_show(struct device * dev,struct device_attribute * attr,char * buf)307 static ssize_t iio_ev_value_show(struct device *dev,
308 struct device_attribute *attr,
309 char *buf)
310 {
311 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
312 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
313 int val, val2, val_arr[2];
314 int ret;
315
316 if (!indio_dev->info->read_event_value)
317 return -EINVAL;
318
319 ret = indio_dev->info->read_event_value(indio_dev,
320 this_attr->c, iio_ev_attr_type(this_attr),
321 iio_ev_attr_dir(this_attr), iio_ev_attr_info(this_attr),
322 &val, &val2);
323 if (ret < 0)
324 return ret;
325 val_arr[0] = val;
326 val_arr[1] = val2;
327 return iio_format_value(buf, ret, 2, val_arr);
328 }
329
iio_ev_value_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)330 static ssize_t iio_ev_value_store(struct device *dev,
331 struct device_attribute *attr,
332 const char *buf,
333 size_t len)
334 {
335 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
336 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
337 int val, val2;
338 int ret;
339
340 if (!indio_dev->info->write_event_value)
341 return -EINVAL;
342
343 ret = iio_str_to_fixpoint(buf, 100000, &val, &val2);
344 if (ret)
345 return ret;
346 ret = indio_dev->info->write_event_value(indio_dev,
347 this_attr->c, iio_ev_attr_type(this_attr),
348 iio_ev_attr_dir(this_attr), iio_ev_attr_info(this_attr),
349 val, val2);
350 if (ret < 0)
351 return ret;
352
353 return len;
354 }
355
iio_device_add_event(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,unsigned int spec_index,enum iio_event_type type,enum iio_event_direction dir,enum iio_shared_by shared_by,const unsigned long * mask)356 static int iio_device_add_event(struct iio_dev *indio_dev,
357 const struct iio_chan_spec *chan, unsigned int spec_index,
358 enum iio_event_type type, enum iio_event_direction dir,
359 enum iio_shared_by shared_by, const unsigned long *mask)
360 {
361 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
362 ssize_t (*show)(struct device *, struct device_attribute *, char *);
363 ssize_t (*store)(struct device *, struct device_attribute *,
364 const char *, size_t);
365 unsigned int attrcount = 0;
366 unsigned int i;
367 char *postfix;
368 int ret;
369
370 for_each_set_bit(i, mask, sizeof(*mask)*8) {
371 if (i >= ARRAY_SIZE(iio_ev_info_text))
372 return -EINVAL;
373 if (dir != IIO_EV_DIR_NONE)
374 postfix = kasprintf(GFP_KERNEL, "%s_%s_%s",
375 iio_ev_type_text[type],
376 iio_ev_dir_text[dir],
377 iio_ev_info_text[i]);
378 else
379 postfix = kasprintf(GFP_KERNEL, "%s_%s",
380 iio_ev_type_text[type],
381 iio_ev_info_text[i]);
382 if (postfix == NULL)
383 return -ENOMEM;
384
385 if (i == IIO_EV_INFO_ENABLE) {
386 show = iio_ev_state_show;
387 store = iio_ev_state_store;
388 } else {
389 show = iio_ev_value_show;
390 store = iio_ev_value_store;
391 }
392
393 ret = __iio_add_chan_devattr(postfix, chan, show, store,
394 (i << 16) | spec_index, shared_by, &indio_dev->dev,
395 &iio_dev_opaque->event_interface->dev_attr_list);
396 kfree(postfix);
397
398 if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE))
399 continue;
400
401 if (ret)
402 return ret;
403
404 attrcount++;
405 }
406
407 return attrcount;
408 }
409
iio_device_add_event_sysfs(struct iio_dev * indio_dev,struct iio_chan_spec const * chan)410 static int iio_device_add_event_sysfs(struct iio_dev *indio_dev,
411 struct iio_chan_spec const *chan)
412 {
413 int ret = 0, i, attrcount = 0;
414 enum iio_event_direction dir;
415 enum iio_event_type type;
416
417 for (i = 0; i < chan->num_event_specs; i++) {
418 type = chan->event_spec[i].type;
419 dir = chan->event_spec[i].dir;
420
421 ret = iio_device_add_event(indio_dev, chan, i, type, dir,
422 IIO_SEPARATE, &chan->event_spec[i].mask_separate);
423 if (ret < 0)
424 return ret;
425 attrcount += ret;
426
427 ret = iio_device_add_event(indio_dev, chan, i, type, dir,
428 IIO_SHARED_BY_TYPE,
429 &chan->event_spec[i].mask_shared_by_type);
430 if (ret < 0)
431 return ret;
432 attrcount += ret;
433
434 ret = iio_device_add_event(indio_dev, chan, i, type, dir,
435 IIO_SHARED_BY_DIR,
436 &chan->event_spec[i].mask_shared_by_dir);
437 if (ret < 0)
438 return ret;
439 attrcount += ret;
440
441 ret = iio_device_add_event(indio_dev, chan, i, type, dir,
442 IIO_SHARED_BY_ALL,
443 &chan->event_spec[i].mask_shared_by_all);
444 if (ret < 0)
445 return ret;
446 attrcount += ret;
447 }
448 ret = attrcount;
449 return ret;
450 }
451
__iio_add_event_config_attrs(struct iio_dev * indio_dev)452 static inline int __iio_add_event_config_attrs(struct iio_dev *indio_dev)
453 {
454 int j, ret, attrcount = 0;
455
456 /* Dynamically created from the channels array */
457 for (j = 0; j < indio_dev->num_channels; j++) {
458 ret = iio_device_add_event_sysfs(indio_dev,
459 &indio_dev->channels[j]);
460 if (ret < 0)
461 return ret;
462 attrcount += ret;
463 }
464 return attrcount;
465 }
466
iio_check_for_dynamic_events(struct iio_dev * indio_dev)467 static bool iio_check_for_dynamic_events(struct iio_dev *indio_dev)
468 {
469 int j;
470
471 for (j = 0; j < indio_dev->num_channels; j++) {
472 if (indio_dev->channels[j].num_event_specs != 0)
473 return true;
474 }
475 return false;
476 }
477
iio_setup_ev_int(struct iio_event_interface * ev_int)478 static void iio_setup_ev_int(struct iio_event_interface *ev_int)
479 {
480 INIT_KFIFO(ev_int->det_events);
481 init_waitqueue_head(&ev_int->wait);
482 mutex_init(&ev_int->read_lock);
483 }
484
485 static const char *iio_event_group_name = "events";
iio_device_register_eventset(struct iio_dev * indio_dev)486 int iio_device_register_eventset(struct iio_dev *indio_dev)
487 {
488 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
489 struct iio_event_interface *ev_int;
490 struct iio_dev_attr *p;
491 int ret = 0, attrcount_orig = 0, attrcount, attrn;
492 struct attribute **attr;
493
494 if (!(indio_dev->info->event_attrs ||
495 iio_check_for_dynamic_events(indio_dev)))
496 return 0;
497
498 ev_int = kzalloc(sizeof(struct iio_event_interface), GFP_KERNEL);
499 if (ev_int == NULL)
500 return -ENOMEM;
501
502 iio_dev_opaque->event_interface = ev_int;
503
504 INIT_LIST_HEAD(&ev_int->dev_attr_list);
505
506 iio_setup_ev_int(ev_int);
507 if (indio_dev->info->event_attrs != NULL) {
508 attr = indio_dev->info->event_attrs->attrs;
509 while (*attr++ != NULL)
510 attrcount_orig++;
511 }
512 attrcount = attrcount_orig;
513 if (indio_dev->channels) {
514 ret = __iio_add_event_config_attrs(indio_dev);
515 if (ret < 0)
516 goto error_free_setup_event_lines;
517 attrcount += ret;
518 }
519
520 ev_int->group.name = iio_event_group_name;
521 ev_int->group.attrs = kcalloc(attrcount + 1,
522 sizeof(ev_int->group.attrs[0]),
523 GFP_KERNEL);
524 if (ev_int->group.attrs == NULL) {
525 ret = -ENOMEM;
526 goto error_free_setup_event_lines;
527 }
528 if (indio_dev->info->event_attrs)
529 memcpy(ev_int->group.attrs,
530 indio_dev->info->event_attrs->attrs,
531 sizeof(ev_int->group.attrs[0]) * attrcount_orig);
532 attrn = attrcount_orig;
533 /* Add all elements from the list. */
534 list_for_each_entry(p, &ev_int->dev_attr_list, l)
535 ev_int->group.attrs[attrn++] = &p->dev_attr.attr;
536 indio_dev->groups[indio_dev->groupcounter++] = &ev_int->group;
537
538 return 0;
539
540 error_free_setup_event_lines:
541 iio_free_chan_devattr_list(&ev_int->dev_attr_list);
542 kfree(ev_int);
543 iio_dev_opaque->event_interface = NULL;
544 return ret;
545 }
546
547 /**
548 * iio_device_wakeup_eventset - Wakes up the event waitqueue
549 * @indio_dev: The IIO device
550 *
551 * Wakes up the event waitqueue used for poll() and blocking read().
552 * Should usually be called when the device is unregistered.
553 */
iio_device_wakeup_eventset(struct iio_dev * indio_dev)554 void iio_device_wakeup_eventset(struct iio_dev *indio_dev)
555 {
556 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
557
558 if (iio_dev_opaque->event_interface == NULL)
559 return;
560 wake_up(&iio_dev_opaque->event_interface->wait);
561 }
562
iio_device_unregister_eventset(struct iio_dev * indio_dev)563 void iio_device_unregister_eventset(struct iio_dev *indio_dev)
564 {
565 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
566 struct iio_event_interface *ev_int = iio_dev_opaque->event_interface;
567
568 if (ev_int == NULL)
569 return;
570 iio_free_chan_devattr_list(&ev_int->dev_attr_list);
571 kfree(ev_int->group.attrs);
572 kfree(ev_int);
573 iio_dev_opaque->event_interface = NULL;
574 }
575