Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 2 | /* Industrial I/O event handling |
| 3 | * |
| 4 | * Copyright (c) 2008 Jonathan Cameron |
| 5 | * |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 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> |
Lars-Peter Clausen | 2c00193 | 2012-01-03 14:59:39 +0100 | [diff] [blame] | 13 | #include <linux/kfifo.h> |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 14 | #include <linux/module.h> |
Lars-Peter Clausen | e18045e | 2012-01-03 14:59:41 +0100 | [diff] [blame] | 15 | #include <linux/poll.h> |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 16 | #include <linux/sched.h> |
| 17 | #include <linux/slab.h> |
| 18 | #include <linux/uaccess.h> |
| 19 | #include <linux/wait.h> |
Jonathan Cameron | 06458e2 | 2012-04-25 15:54:58 +0100 | [diff] [blame] | 20 | #include <linux/iio/iio.h> |
Alexandru Ardelean | fa83c3b | 2020-06-30 07:57:08 +0300 | [diff] [blame] | 21 | #include <linux/iio/iio-opaque.h> |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 22 | #include "iio_core.h" |
Jonathan Cameron | 06458e2 | 2012-04-25 15:54:58 +0100 | [diff] [blame] | 23 | #include <linux/iio/sysfs.h> |
| 24 | #include <linux/iio/events.h> |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 25 | |
| 26 | /** |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 27 | * struct iio_event_interface - chrdev interface for an event line |
| 28 | * @wait: wait queue to allow blocking reads of events |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 29 | * @det_events: list of detected events |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 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 |
Cristina Opriceana | a316c01 | 2015-07-24 16:21:50 +0300 | [diff] [blame] | 33 | * @read_lock: lock to protect kfifo read operations |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 34 | */ |
| 35 | struct iio_event_interface { |
| 36 | wait_queue_head_t wait; |
Lars-Peter Clausen | 2c00193 | 2012-01-03 14:59:39 +0100 | [diff] [blame] | 37 | DECLARE_KFIFO(det_events, struct iio_event_data, 16); |
| 38 | |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 39 | struct list_head dev_attr_list; |
| 40 | unsigned long flags; |
| 41 | struct attribute_group group; |
Lars-Peter Clausen | b91acca | 2014-02-14 18:49:00 +0000 | [diff] [blame] | 42 | struct mutex read_lock; |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 43 | }; |
| 44 | |
Gregor Boirie | bc2b7da | 2016-03-09 19:05:49 +0100 | [diff] [blame] | 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 | |
Sachin Kamat | a7e57dc | 2013-10-29 11:39:00 +0000 | [diff] [blame] | 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 |
Lars-Peter Clausen | b91acca | 2014-02-14 18:49:00 +0000 | [diff] [blame] | 55 | * |
| 56 | * Note: The caller must make sure that this function is not running |
| 57 | * concurrently for the same indio_dev more than once. |
Lars-Peter Clausen | 4b1a938 | 2016-09-08 18:49:10 +0200 | [diff] [blame] | 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. |
Sachin Kamat | a7e57dc | 2013-10-29 11:39:00 +0000 | [diff] [blame] | 63 | **/ |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 64 | int iio_push_event(struct iio_dev *indio_dev, u64 ev_code, s64 timestamp) |
| 65 | { |
Alexandru Ardelean | fa83c3b | 2020-06-30 07:57:08 +0300 | [diff] [blame] | 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; |
Lars-Peter Clausen | 2c00193 | 2012-01-03 14:59:39 +0100 | [diff] [blame] | 68 | struct iio_event_data ev; |
| 69 | int copied; |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 70 | |
Lars-Peter Clausen | 4b1a938 | 2016-09-08 18:49:10 +0200 | [diff] [blame] | 71 | if (!ev_int) |
| 72 | return 0; |
| 73 | |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 74 | /* Does anyone care? */ |
Gregor Boirie | bc2b7da | 2016-03-09 19:05:49 +0100 | [diff] [blame] | 75 | if (iio_event_enabled(ev_int)) { |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 76 | |
Lars-Peter Clausen | 2c00193 | 2012-01-03 14:59:39 +0100 | [diff] [blame] | 77 | ev.id = ev_code; |
| 78 | ev.timestamp = timestamp; |
| 79 | |
Stefani Seibold | 498d319 | 2013-11-14 14:32:17 -0800 | [diff] [blame] | 80 | copied = kfifo_put(&ev_int->det_events, ev); |
Lars-Peter Clausen | 2c00193 | 2012-01-03 14:59:39 +0100 | [diff] [blame] | 81 | if (copied != 0) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 82 | wake_up_poll(&ev_int->wait, EPOLLIN); |
Lars-Peter Clausen | 43ba110 | 2012-01-03 14:59:40 +0100 | [diff] [blame] | 83 | } |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 84 | |
Lars-Peter Clausen | 2c00193 | 2012-01-03 14:59:39 +0100 | [diff] [blame] | 85 | return 0; |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 86 | } |
| 87 | EXPORT_SYMBOL(iio_push_event); |
| 88 | |
Lars-Peter Clausen | e18045e | 2012-01-03 14:59:41 +0100 | [diff] [blame] | 89 | /** |
| 90 | * iio_event_poll() - poll the event queue to find out if it has data |
Cristina Opriceana | a316c01 | 2015-07-24 16:21:50 +0300 | [diff] [blame] | 91 | * @filep: File structure pointer to identify the device |
| 92 | * @wait: Poll table pointer to add the wait queue on |
| 93 | * |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 94 | * Return: (EPOLLIN | EPOLLRDNORM) if data is available for reading |
Cristina Opriceana | a316c01 | 2015-07-24 16:21:50 +0300 | [diff] [blame] | 95 | * or a negative error code on failure |
Lars-Peter Clausen | e18045e | 2012-01-03 14:59:41 +0100 | [diff] [blame] | 96 | */ |
Al Viro | afc9a42 | 2017-07-03 06:39:46 -0400 | [diff] [blame] | 97 | static __poll_t iio_event_poll(struct file *filep, |
Lars-Peter Clausen | e18045e | 2012-01-03 14:59:41 +0100 | [diff] [blame] | 98 | struct poll_table_struct *wait) |
| 99 | { |
Lars-Peter Clausen | cadc212 | 2013-09-18 21:02:00 +0100 | [diff] [blame] | 100 | struct iio_dev *indio_dev = filep->private_data; |
Alexandru Ardelean | fa83c3b | 2020-06-30 07:57:08 +0300 | [diff] [blame] | 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; |
Al Viro | afc9a42 | 2017-07-03 06:39:46 -0400 | [diff] [blame] | 103 | __poll_t events = 0; |
Lars-Peter Clausen | e18045e | 2012-01-03 14:59:41 +0100 | [diff] [blame] | 104 | |
Lars-Peter Clausen | f18e7a0 | 2013-10-04 12:06:00 +0100 | [diff] [blame] | 105 | if (!indio_dev->info) |
Cristina Opriceana | 41d903c | 2015-08-03 13:00:47 +0300 | [diff] [blame] | 106 | return events; |
Lars-Peter Clausen | f18e7a0 | 2013-10-04 12:06:00 +0100 | [diff] [blame] | 107 | |
Lars-Peter Clausen | e18045e | 2012-01-03 14:59:41 +0100 | [diff] [blame] | 108 | poll_wait(filep, &ev_int->wait, wait); |
| 109 | |
Lars-Peter Clausen | e18045e | 2012-01-03 14:59:41 +0100 | [diff] [blame] | 110 | if (!kfifo_is_empty(&ev_int->det_events)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 111 | events = EPOLLIN | EPOLLRDNORM; |
Lars-Peter Clausen | e18045e | 2012-01-03 14:59:41 +0100 | [diff] [blame] | 112 | |
| 113 | return events; |
| 114 | } |
| 115 | |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 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 | { |
Lars-Peter Clausen | cadc212 | 2013-09-18 21:02:00 +0100 | [diff] [blame] | 121 | struct iio_dev *indio_dev = filep->private_data; |
Alexandru Ardelean | fa83c3b | 2020-06-30 07:57:08 +0300 | [diff] [blame] | 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; |
Lars-Peter Clausen | 2c00193 | 2012-01-03 14:59:39 +0100 | [diff] [blame] | 124 | unsigned int copied; |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 125 | int ret; |
| 126 | |
Lars-Peter Clausen | f18e7a0 | 2013-10-04 12:06:00 +0100 | [diff] [blame] | 127 | if (!indio_dev->info) |
| 128 | return -ENODEV; |
| 129 | |
Lars-Peter Clausen | 2c00193 | 2012-01-03 14:59:39 +0100 | [diff] [blame] | 130 | if (count < sizeof(struct iio_event_data)) |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 131 | return -EINVAL; |
| 132 | |
Lars-Peter Clausen | b91acca | 2014-02-14 18:49:00 +0000 | [diff] [blame] | 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, |
Lars-Peter Clausen | d2f0a48 | 2013-10-04 12:07:00 +0100 | [diff] [blame] | 139 | !kfifo_is_empty(&ev_int->det_events) || |
| 140 | indio_dev->info == NULL); |
Lars-Peter Clausen | b91acca | 2014-02-14 18:49:00 +0000 | [diff] [blame] | 141 | if (ret) |
| 142 | return ret; |
| 143 | if (indio_dev->info == NULL) |
| 144 | return -ENODEV; |
Lars-Peter Clausen | d2f0a48 | 2013-10-04 12:07:00 +0100 | [diff] [blame] | 145 | } |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 146 | |
Lars-Peter Clausen | b91acca | 2014-02-14 18:49:00 +0000 | [diff] [blame] | 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); |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 151 | |
Lars-Peter Clausen | b91acca | 2014-02-14 18:49:00 +0000 | [diff] [blame] | 152 | if (ret) |
| 153 | return ret; |
Lars-Peter Clausen | 43ba110 | 2012-01-03 14:59:40 +0100 | [diff] [blame] | 154 | |
Lars-Peter Clausen | b91acca | 2014-02-14 18:49:00 +0000 | [diff] [blame] | 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; |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | static int iio_event_chrdev_release(struct inode *inode, struct file *filep) |
| 170 | { |
Lars-Peter Clausen | cadc212 | 2013-09-18 21:02:00 +0100 | [diff] [blame] | 171 | struct iio_dev *indio_dev = filep->private_data; |
Alexandru Ardelean | fa83c3b | 2020-06-30 07:57:08 +0300 | [diff] [blame] | 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; |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 174 | |
Lars-Peter Clausen | b91acca | 2014-02-14 18:49:00 +0000 | [diff] [blame] | 175 | clear_bit(IIO_BUSY_BIT_POS, &ev_int->flags); |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 176 | |
Lars-Peter Clausen | cadc212 | 2013-09-18 21:02:00 +0100 | [diff] [blame] | 177 | iio_device_put(indio_dev); |
| 178 | |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | static const struct file_operations iio_event_chrdev_fileops = { |
| 183 | .read = iio_event_chrdev_read, |
Lars-Peter Clausen | e18045e | 2012-01-03 14:59:41 +0100 | [diff] [blame] | 184 | .poll = iio_event_poll, |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 185 | .release = iio_event_chrdev_release, |
| 186 | .owner = THIS_MODULE, |
| 187 | .llseek = noop_llseek, |
| 188 | }; |
| 189 | |
| 190 | int iio_event_getfd(struct iio_dev *indio_dev) |
| 191 | { |
Alexandru Ardelean | fa83c3b | 2020-06-30 07:57:08 +0300 | [diff] [blame] | 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; |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 194 | int fd; |
| 195 | |
| 196 | if (ev_int == NULL) |
| 197 | return -ENODEV; |
| 198 | |
Gregor Boirie | bc2b7da | 2016-03-09 19:05:49 +0100 | [diff] [blame] | 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 | } |
Lars-Peter Clausen | b91acca | 2014-02-14 18:49:00 +0000 | [diff] [blame] | 207 | |
Lars-Peter Clausen | cadc212 | 2013-09-18 21:02:00 +0100 | [diff] [blame] | 208 | iio_device_get(indio_dev); |
| 209 | |
| 210 | fd = anon_inode_getfd("iio:event", &iio_event_chrdev_fileops, |
Greg Kroah-Hartman | e2aad1d | 2013-09-25 08:59:04 -0700 | [diff] [blame] | 211 | indio_dev, O_RDONLY | O_CLOEXEC); |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 212 | if (fd < 0) { |
Lars-Peter Clausen | b91acca | 2014-02-14 18:49:00 +0000 | [diff] [blame] | 213 | clear_bit(IIO_BUSY_BIT_POS, &ev_int->flags); |
Lars-Peter Clausen | cadc212 | 2013-09-18 21:02:00 +0100 | [diff] [blame] | 214 | iio_device_put(indio_dev); |
Lars-Peter Clausen | b91acca | 2014-02-14 18:49:00 +0000 | [diff] [blame] | 215 | } else { |
| 216 | kfifo_reset_out(&ev_int->det_events); |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 217 | } |
Lars-Peter Clausen | b91acca | 2014-02-14 18:49:00 +0000 | [diff] [blame] | 218 | |
Gregor Boirie | bc2b7da | 2016-03-09 19:05:49 +0100 | [diff] [blame] | 219 | unlock: |
| 220 | mutex_unlock(&indio_dev->mlock); |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 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", |
Irina Tirdea | 27be842 | 2015-01-11 21:10:11 +0200 | [diff] [blame] | 230 | [IIO_EV_TYPE_CHANGE] = "change", |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 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 | |
Lars-Peter Clausen | b4e3ac0 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 239 | static const char * const iio_ev_info_text[] = { |
| 240 | [IIO_EV_INFO_ENABLE] = "en", |
| 241 | [IIO_EV_INFO_VALUE] = "value", |
Lars-Peter Clausen | ec6670a | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 242 | [IIO_EV_INFO_HYSTERESIS] = "hysteresis", |
Srinivas Pandruvada | 77a533c | 2014-08-07 23:29:00 +0100 | [diff] [blame] | 243 | [IIO_EV_INFO_PERIOD] = "period", |
Martin Fuzzey | 3f7f642 | 2015-05-13 12:26:42 +0200 | [diff] [blame] | 244 | [IIO_EV_INFO_HIGH_PASS_FILTER_3DB] = "high_pass_filter_3db", |
| 245 | [IIO_EV_INFO_LOW_PASS_FILTER_3DB] = "low_pass_filter_3db", |
Lars-Peter Clausen | b4e3ac0 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 246 | }; |
| 247 | |
| 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 | |
| 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 | |
| 258 | static enum iio_event_info iio_ev_attr_info(struct iio_dev_attr *attr) |
| 259 | { |
| 260 | return (attr->address >> 16) & 0xffff; |
| 261 | } |
| 262 | |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 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 | { |
Lars-Peter Clausen | e53f5ac | 2012-05-12 15:39:33 +0200 | [diff] [blame] | 268 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 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 | |
Lars-Peter Clausen | cb95585 | 2013-12-07 10:45:00 +0000 | [diff] [blame] | 277 | ret = indio_dev->info->write_event_config(indio_dev, |
| 278 | this_attr->c, iio_ev_attr_type(this_attr), |
| 279 | iio_ev_attr_dir(this_attr), val); |
Lars-Peter Clausen | b4e3ac0 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 280 | |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 281 | return (ret < 0) ? ret : len; |
| 282 | } |
| 283 | |
| 284 | static ssize_t iio_ev_state_show(struct device *dev, |
| 285 | struct device_attribute *attr, |
| 286 | char *buf) |
| 287 | { |
Lars-Peter Clausen | e53f5ac | 2012-05-12 15:39:33 +0200 | [diff] [blame] | 288 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 289 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
Lars-Peter Clausen | b4e3ac0 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 290 | int val; |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 291 | |
Lars-Peter Clausen | cb95585 | 2013-12-07 10:45:00 +0000 | [diff] [blame] | 292 | val = indio_dev->info->read_event_config(indio_dev, |
| 293 | this_attr->c, iio_ev_attr_type(this_attr), |
| 294 | iio_ev_attr_dir(this_attr)); |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 295 | if (val < 0) |
| 296 | return val; |
| 297 | else |
| 298 | return sprintf(buf, "%d\n", val); |
| 299 | } |
| 300 | |
| 301 | static ssize_t iio_ev_value_show(struct device *dev, |
| 302 | struct device_attribute *attr, |
| 303 | char *buf) |
| 304 | { |
Lars-Peter Clausen | e53f5ac | 2012-05-12 15:39:33 +0200 | [diff] [blame] | 305 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 306 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
Srinivas Pandruvada | 9fbfb4b | 2014-04-29 00:51:00 +0100 | [diff] [blame] | 307 | int val, val2, val_arr[2]; |
Lars-Peter Clausen | b4e3ac0 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 308 | int ret; |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 309 | |
Lars-Peter Clausen | cb95585 | 2013-12-07 10:45:00 +0000 | [diff] [blame] | 310 | ret = indio_dev->info->read_event_value(indio_dev, |
| 311 | this_attr->c, iio_ev_attr_type(this_attr), |
| 312 | iio_ev_attr_dir(this_attr), iio_ev_attr_info(this_attr), |
| 313 | &val, &val2); |
| 314 | if (ret < 0) |
| 315 | return ret; |
Srinivas Pandruvada | 9fbfb4b | 2014-04-29 00:51:00 +0100 | [diff] [blame] | 316 | val_arr[0] = val; |
| 317 | val_arr[1] = val2; |
| 318 | return iio_format_value(buf, ret, 2, val_arr); |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | static ssize_t iio_ev_value_store(struct device *dev, |
| 322 | struct device_attribute *attr, |
| 323 | const char *buf, |
| 324 | size_t len) |
| 325 | { |
Lars-Peter Clausen | e53f5ac | 2012-05-12 15:39:33 +0200 | [diff] [blame] | 326 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 327 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
Lars-Peter Clausen | b4e3ac0 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 328 | int val, val2; |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 329 | int ret; |
| 330 | |
Lars-Peter Clausen | cb95585 | 2013-12-07 10:45:00 +0000 | [diff] [blame] | 331 | if (!indio_dev->info->write_event_value) |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 332 | return -EINVAL; |
| 333 | |
Lars-Peter Clausen | cb95585 | 2013-12-07 10:45:00 +0000 | [diff] [blame] | 334 | ret = iio_str_to_fixpoint(buf, 100000, &val, &val2); |
| 335 | if (ret) |
| 336 | return ret; |
| 337 | ret = indio_dev->info->write_event_value(indio_dev, |
| 338 | this_attr->c, iio_ev_attr_type(this_attr), |
| 339 | iio_ev_attr_dir(this_attr), iio_ev_attr_info(this_attr), |
| 340 | val, val2); |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 341 | if (ret < 0) |
| 342 | return ret; |
| 343 | |
| 344 | return len; |
| 345 | } |
| 346 | |
Lars-Peter Clausen | b4e3ac0 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 347 | static int iio_device_add_event(struct iio_dev *indio_dev, |
| 348 | const struct iio_chan_spec *chan, unsigned int spec_index, |
| 349 | enum iio_event_type type, enum iio_event_direction dir, |
| 350 | enum iio_shared_by shared_by, const unsigned long *mask) |
| 351 | { |
Alexandru Ardelean | fa83c3b | 2020-06-30 07:57:08 +0300 | [diff] [blame] | 352 | struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); |
Lars-Peter Clausen | b4e3ac0 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 353 | ssize_t (*show)(struct device *, struct device_attribute *, char *); |
| 354 | ssize_t (*store)(struct device *, struct device_attribute *, |
| 355 | const char *, size_t); |
| 356 | unsigned int attrcount = 0; |
| 357 | unsigned int i; |
| 358 | char *postfix; |
| 359 | int ret; |
| 360 | |
Jonathan Cameron | ef4b485 | 2014-01-03 22:24:00 +0000 | [diff] [blame] | 361 | for_each_set_bit(i, mask, sizeof(*mask)*8) { |
| 362 | if (i >= ARRAY_SIZE(iio_ev_info_text)) |
| 363 | return -EINVAL; |
Irina Tirdea | 1843c2f | 2014-11-10 14:45:31 +0200 | [diff] [blame] | 364 | if (dir != IIO_EV_DIR_NONE) |
| 365 | postfix = kasprintf(GFP_KERNEL, "%s_%s_%s", |
| 366 | iio_ev_type_text[type], |
| 367 | iio_ev_dir_text[dir], |
| 368 | iio_ev_info_text[i]); |
| 369 | else |
| 370 | postfix = kasprintf(GFP_KERNEL, "%s_%s", |
| 371 | iio_ev_type_text[type], |
| 372 | iio_ev_info_text[i]); |
Lars-Peter Clausen | b4e3ac0 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 373 | if (postfix == NULL) |
| 374 | return -ENOMEM; |
| 375 | |
| 376 | if (i == IIO_EV_INFO_ENABLE) { |
| 377 | show = iio_ev_state_show; |
| 378 | store = iio_ev_state_store; |
| 379 | } else { |
| 380 | show = iio_ev_value_show; |
| 381 | store = iio_ev_value_store; |
| 382 | } |
| 383 | |
| 384 | ret = __iio_add_chan_devattr(postfix, chan, show, store, |
| 385 | (i << 16) | spec_index, shared_by, &indio_dev->dev, |
Alexandru Ardelean | fa83c3b | 2020-06-30 07:57:08 +0300 | [diff] [blame] | 386 | &iio_dev_opaque->event_interface->dev_attr_list); |
Lars-Peter Clausen | b4e3ac0 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 387 | kfree(postfix); |
| 388 | |
Srinivas Pandruvada | 78b3321 | 2014-08-07 22:03:00 +0100 | [diff] [blame] | 389 | if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE)) |
| 390 | continue; |
| 391 | |
Lars-Peter Clausen | b4e3ac0 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 392 | if (ret) |
| 393 | return ret; |
| 394 | |
| 395 | attrcount++; |
| 396 | } |
| 397 | |
| 398 | return attrcount; |
| 399 | } |
| 400 | |
Lars-Peter Clausen | cb95585 | 2013-12-07 10:45:00 +0000 | [diff] [blame] | 401 | static int iio_device_add_event_sysfs(struct iio_dev *indio_dev, |
Lars-Peter Clausen | b4e3ac0 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 402 | struct iio_chan_spec const *chan) |
| 403 | { |
| 404 | int ret = 0, i, attrcount = 0; |
| 405 | enum iio_event_direction dir; |
| 406 | enum iio_event_type type; |
| 407 | |
| 408 | for (i = 0; i < chan->num_event_specs; i++) { |
| 409 | type = chan->event_spec[i].type; |
| 410 | dir = chan->event_spec[i].dir; |
| 411 | |
| 412 | ret = iio_device_add_event(indio_dev, chan, i, type, dir, |
| 413 | IIO_SEPARATE, &chan->event_spec[i].mask_separate); |
| 414 | if (ret < 0) |
Hartmut Knaack | 92825ff | 2014-02-16 11:53:00 +0000 | [diff] [blame] | 415 | return ret; |
Lars-Peter Clausen | b4e3ac0 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 416 | attrcount += ret; |
| 417 | |
| 418 | ret = iio_device_add_event(indio_dev, chan, i, type, dir, |
| 419 | IIO_SHARED_BY_TYPE, |
| 420 | &chan->event_spec[i].mask_shared_by_type); |
| 421 | if (ret < 0) |
Hartmut Knaack | 92825ff | 2014-02-16 11:53:00 +0000 | [diff] [blame] | 422 | return ret; |
Lars-Peter Clausen | b4e3ac0 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 423 | attrcount += ret; |
| 424 | |
| 425 | ret = iio_device_add_event(indio_dev, chan, i, type, dir, |
| 426 | IIO_SHARED_BY_DIR, |
| 427 | &chan->event_spec[i].mask_shared_by_dir); |
| 428 | if (ret < 0) |
Hartmut Knaack | 92825ff | 2014-02-16 11:53:00 +0000 | [diff] [blame] | 429 | return ret; |
Lars-Peter Clausen | b4e3ac0 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 430 | attrcount += ret; |
| 431 | |
| 432 | ret = iio_device_add_event(indio_dev, chan, i, type, dir, |
| 433 | IIO_SHARED_BY_ALL, |
| 434 | &chan->event_spec[i].mask_shared_by_all); |
| 435 | if (ret < 0) |
Hartmut Knaack | 92825ff | 2014-02-16 11:53:00 +0000 | [diff] [blame] | 436 | return ret; |
Lars-Peter Clausen | b4e3ac0 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 437 | attrcount += ret; |
| 438 | } |
| 439 | ret = attrcount; |
Lars-Peter Clausen | b4e3ac0 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 440 | return ret; |
| 441 | } |
| 442 | |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 443 | static inline int __iio_add_event_config_attrs(struct iio_dev *indio_dev) |
| 444 | { |
| 445 | int j, ret, attrcount = 0; |
| 446 | |
Roberta Dobrescu | 2179aab | 2015-01-16 00:24:14 +0200 | [diff] [blame] | 447 | /* Dynamically created from the channels array */ |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 448 | for (j = 0; j < indio_dev->num_channels; j++) { |
| 449 | ret = iio_device_add_event_sysfs(indio_dev, |
| 450 | &indio_dev->channels[j]); |
| 451 | if (ret < 0) |
Julia Lawall | e3db9ef | 2012-10-21 11:52:00 +0100 | [diff] [blame] | 452 | return ret; |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 453 | attrcount += ret; |
| 454 | } |
| 455 | return attrcount; |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | static bool iio_check_for_dynamic_events(struct iio_dev *indio_dev) |
| 459 | { |
| 460 | int j; |
| 461 | |
Lars-Peter Clausen | b4e3ac0 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 462 | for (j = 0; j < indio_dev->num_channels; j++) { |
Lars-Peter Clausen | b4e3ac0 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 463 | if (indio_dev->channels[j].num_event_specs != 0) |
| 464 | return true; |
| 465 | } |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 466 | return false; |
| 467 | } |
| 468 | |
| 469 | static void iio_setup_ev_int(struct iio_event_interface *ev_int) |
| 470 | { |
Lars-Peter Clausen | 2c00193 | 2012-01-03 14:59:39 +0100 | [diff] [blame] | 471 | INIT_KFIFO(ev_int->det_events); |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 472 | init_waitqueue_head(&ev_int->wait); |
Lars-Peter Clausen | b91acca | 2014-02-14 18:49:00 +0000 | [diff] [blame] | 473 | mutex_init(&ev_int->read_lock); |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | static const char *iio_event_group_name = "events"; |
| 477 | int iio_device_register_eventset(struct iio_dev *indio_dev) |
| 478 | { |
Alexandru Ardelean | fa83c3b | 2020-06-30 07:57:08 +0300 | [diff] [blame] | 479 | struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); |
Alexandru Ardelean | f2f45a5 | 2020-09-21 13:31:55 +0300 | [diff] [blame] | 480 | struct iio_event_interface *ev_int; |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 481 | struct iio_dev_attr *p; |
| 482 | int ret = 0, attrcount_orig = 0, attrcount, attrn; |
| 483 | struct attribute **attr; |
| 484 | |
| 485 | if (!(indio_dev->info->event_attrs || |
| 486 | iio_check_for_dynamic_events(indio_dev))) |
| 487 | return 0; |
| 488 | |
Alexandru Ardelean | f2f45a5 | 2020-09-21 13:31:55 +0300 | [diff] [blame] | 489 | ev_int = kzalloc(sizeof(struct iio_event_interface), GFP_KERNEL); |
| 490 | if (ev_int == NULL) |
Hartmut Knaack | 92825ff | 2014-02-16 11:53:00 +0000 | [diff] [blame] | 491 | return -ENOMEM; |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 492 | |
Alexandru Ardelean | f2f45a5 | 2020-09-21 13:31:55 +0300 | [diff] [blame] | 493 | iio_dev_opaque->event_interface = ev_int; |
Sascha Hauer | 46b2431 | 2012-07-03 10:55:40 +0200 | [diff] [blame] | 494 | |
Alexandru Ardelean | f2f45a5 | 2020-09-21 13:31:55 +0300 | [diff] [blame] | 495 | INIT_LIST_HEAD(&ev_int->dev_attr_list); |
| 496 | |
| 497 | iio_setup_ev_int(ev_int); |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 498 | if (indio_dev->info->event_attrs != NULL) { |
| 499 | attr = indio_dev->info->event_attrs->attrs; |
| 500 | while (*attr++ != NULL) |
| 501 | attrcount_orig++; |
| 502 | } |
| 503 | attrcount = attrcount_orig; |
| 504 | if (indio_dev->channels) { |
| 505 | ret = __iio_add_event_config_attrs(indio_dev); |
| 506 | if (ret < 0) |
| 507 | goto error_free_setup_event_lines; |
| 508 | attrcount += ret; |
| 509 | } |
| 510 | |
Alexandru Ardelean | f2f45a5 | 2020-09-21 13:31:55 +0300 | [diff] [blame] | 511 | ev_int->group.name = iio_event_group_name; |
| 512 | ev_int->group.attrs = kcalloc(attrcount + 1, |
| 513 | sizeof(ev_int->group.attrs[0]), |
| 514 | GFP_KERNEL); |
| 515 | if (ev_int->group.attrs == NULL) { |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 516 | ret = -ENOMEM; |
| 517 | goto error_free_setup_event_lines; |
| 518 | } |
| 519 | if (indio_dev->info->event_attrs) |
Alexandru Ardelean | f2f45a5 | 2020-09-21 13:31:55 +0300 | [diff] [blame] | 520 | memcpy(ev_int->group.attrs, |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 521 | indio_dev->info->event_attrs->attrs, |
Alexandru Ardelean | f2f45a5 | 2020-09-21 13:31:55 +0300 | [diff] [blame] | 522 | sizeof(ev_int->group.attrs[0]) * attrcount_orig); |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 523 | attrn = attrcount_orig; |
| 524 | /* Add all elements from the list. */ |
Alexandru Ardelean | f2f45a5 | 2020-09-21 13:31:55 +0300 | [diff] [blame] | 525 | list_for_each_entry(p, &ev_int->dev_attr_list, l) |
| 526 | ev_int->group.attrs[attrn++] = &p->dev_attr.attr; |
| 527 | indio_dev->groups[indio_dev->groupcounter++] = &ev_int->group; |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 528 | |
| 529 | return 0; |
| 530 | |
| 531 | error_free_setup_event_lines: |
Alexandru Ardelean | f2f45a5 | 2020-09-21 13:31:55 +0300 | [diff] [blame] | 532 | iio_free_chan_devattr_list(&ev_int->dev_attr_list); |
| 533 | kfree(ev_int); |
Alexandru Ardelean | fa83c3b | 2020-06-30 07:57:08 +0300 | [diff] [blame] | 534 | iio_dev_opaque->event_interface = NULL; |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 535 | return ret; |
| 536 | } |
| 537 | |
Lars-Peter Clausen | d2f0a48 | 2013-10-04 12:07:00 +0100 | [diff] [blame] | 538 | /** |
| 539 | * iio_device_wakeup_eventset - Wakes up the event waitqueue |
| 540 | * @indio_dev: The IIO device |
| 541 | * |
| 542 | * Wakes up the event waitqueue used for poll() and blocking read(). |
| 543 | * Should usually be called when the device is unregistered. |
| 544 | */ |
| 545 | void iio_device_wakeup_eventset(struct iio_dev *indio_dev) |
| 546 | { |
Alexandru Ardelean | fa83c3b | 2020-06-30 07:57:08 +0300 | [diff] [blame] | 547 | struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); |
| 548 | |
| 549 | if (iio_dev_opaque->event_interface == NULL) |
Lars-Peter Clausen | d2f0a48 | 2013-10-04 12:07:00 +0100 | [diff] [blame] | 550 | return; |
Alexandru Ardelean | fa83c3b | 2020-06-30 07:57:08 +0300 | [diff] [blame] | 551 | wake_up(&iio_dev_opaque->event_interface->wait); |
Lars-Peter Clausen | d2f0a48 | 2013-10-04 12:07:00 +0100 | [diff] [blame] | 552 | } |
| 553 | |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 554 | void iio_device_unregister_eventset(struct iio_dev *indio_dev) |
| 555 | { |
Alexandru Ardelean | fa83c3b | 2020-06-30 07:57:08 +0300 | [diff] [blame] | 556 | struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); |
Alexandru Ardelean | f2f45a5 | 2020-09-21 13:31:55 +0300 | [diff] [blame] | 557 | struct iio_event_interface *ev_int = iio_dev_opaque->event_interface; |
Alexandru Ardelean | fa83c3b | 2020-06-30 07:57:08 +0300 | [diff] [blame] | 558 | |
Alexandru Ardelean | f2f45a5 | 2020-09-21 13:31:55 +0300 | [diff] [blame] | 559 | if (ev_int == NULL) |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 560 | return; |
Alexandru Ardelean | f2f45a5 | 2020-09-21 13:31:55 +0300 | [diff] [blame] | 561 | iio_free_chan_devattr_list(&ev_int->dev_attr_list); |
| 562 | kfree(ev_int->group.attrs); |
| 563 | kfree(ev_int); |
Alexandru Ardelean | a3598d1 | 2020-09-21 13:31:56 +0300 | [diff] [blame] | 564 | iio_dev_opaque->event_interface = NULL; |
Lars-Peter Clausen | 0a769a9 | 2012-01-03 14:59:38 +0100 | [diff] [blame] | 565 | } |