blob: 99ba657b856865744b17613230d0617c479e47a6 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +01002/* Industrial I/O event handling
3 *
4 * Copyright (c) 2008 Jonathan Cameron
5 *
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +01006 * 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 Clausen2c001932012-01-03 14:59:39 +010013#include <linux/kfifo.h>
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +010014#include <linux/module.h>
Lars-Peter Clausene18045e2012-01-03 14:59:41 +010015#include <linux/poll.h>
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +010016#include <linux/sched.h>
17#include <linux/slab.h>
18#include <linux/uaccess.h>
19#include <linux/wait.h>
Jonathan Cameron06458e22012-04-25 15:54:58 +010020#include <linux/iio/iio.h>
Alexandru Ardeleanfa83c3b2020-06-30 07:57:08 +030021#include <linux/iio/iio-opaque.h>
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +010022#include "iio_core.h"
Jonathan Cameron06458e22012-04-25 15:54:58 +010023#include <linux/iio/sysfs.h>
24#include <linux/iio/events.h>
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +010025
26/**
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +010027 * struct iio_event_interface - chrdev interface for an event line
28 * @wait: wait queue to allow blocking reads of events
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +010029 * @det_events: list of detected events
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +010030 * @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 Opriceanaa316c012015-07-24 16:21:50 +030033 * @read_lock: lock to protect kfifo read operations
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +010034 */
35struct iio_event_interface {
36 wait_queue_head_t wait;
Lars-Peter Clausen2c001932012-01-03 14:59:39 +010037 DECLARE_KFIFO(det_events, struct iio_event_data, 16);
38
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +010039 struct list_head dev_attr_list;
40 unsigned long flags;
41 struct attribute_group group;
Lars-Peter Clausenb91acca2014-02-14 18:49:00 +000042 struct mutex read_lock;
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +010043};
44
Gregor Boiriebc2b7da2016-03-09 19:05:49 +010045bool 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 Kamata7e57dc2013-10-29 11:39:00 +000050/**
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 Clausenb91acca2014-02-14 18:49:00 +000055 *
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 Clausen4b1a9382016-09-08 18:49:10 +020058 *
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 Kamata7e57dc2013-10-29 11:39:00 +000063 **/
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +010064int iio_push_event(struct iio_dev *indio_dev, u64 ev_code, s64 timestamp)
65{
Alexandru Ardeleanfa83c3b2020-06-30 07:57:08 +030066 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 Clausen2c001932012-01-03 14:59:39 +010068 struct iio_event_data ev;
69 int copied;
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +010070
Lars-Peter Clausen4b1a9382016-09-08 18:49:10 +020071 if (!ev_int)
72 return 0;
73
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +010074 /* Does anyone care? */
Gregor Boiriebc2b7da2016-03-09 19:05:49 +010075 if (iio_event_enabled(ev_int)) {
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +010076
Lars-Peter Clausen2c001932012-01-03 14:59:39 +010077 ev.id = ev_code;
78 ev.timestamp = timestamp;
79
Stefani Seibold498d3192013-11-14 14:32:17 -080080 copied = kfifo_put(&ev_int->det_events, ev);
Lars-Peter Clausen2c001932012-01-03 14:59:39 +010081 if (copied != 0)
Linus Torvaldsa9a08842018-02-11 14:34:03 -080082 wake_up_poll(&ev_int->wait, EPOLLIN);
Lars-Peter Clausen43ba1102012-01-03 14:59:40 +010083 }
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +010084
Lars-Peter Clausen2c001932012-01-03 14:59:39 +010085 return 0;
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +010086}
87EXPORT_SYMBOL(iio_push_event);
88
Lars-Peter Clausene18045e2012-01-03 14:59:41 +010089/**
90 * iio_event_poll() - poll the event queue to find out if it has data
Cristina Opriceanaa316c012015-07-24 16:21:50 +030091 * @filep: File structure pointer to identify the device
92 * @wait: Poll table pointer to add the wait queue on
93 *
Linus Torvaldsa9a08842018-02-11 14:34:03 -080094 * Return: (EPOLLIN | EPOLLRDNORM) if data is available for reading
Cristina Opriceanaa316c012015-07-24 16:21:50 +030095 * or a negative error code on failure
Lars-Peter Clausene18045e2012-01-03 14:59:41 +010096 */
Al Viroafc9a422017-07-03 06:39:46 -040097static __poll_t iio_event_poll(struct file *filep,
Lars-Peter Clausene18045e2012-01-03 14:59:41 +010098 struct poll_table_struct *wait)
99{
Lars-Peter Clausencadc2122013-09-18 21:02:00 +0100100 struct iio_dev *indio_dev = filep->private_data;
Alexandru Ardeleanfa83c3b2020-06-30 07:57:08 +0300101 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 Viroafc9a422017-07-03 06:39:46 -0400103 __poll_t events = 0;
Lars-Peter Clausene18045e2012-01-03 14:59:41 +0100104
Lars-Peter Clausenf18e7a02013-10-04 12:06:00 +0100105 if (!indio_dev->info)
Cristina Opriceana41d903c2015-08-03 13:00:47 +0300106 return events;
Lars-Peter Clausenf18e7a02013-10-04 12:06:00 +0100107
Lars-Peter Clausene18045e2012-01-03 14:59:41 +0100108 poll_wait(filep, &ev_int->wait, wait);
109
Lars-Peter Clausene18045e2012-01-03 14:59:41 +0100110 if (!kfifo_is_empty(&ev_int->det_events))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800111 events = EPOLLIN | EPOLLRDNORM;
Lars-Peter Clausene18045e2012-01-03 14:59:41 +0100112
113 return events;
114}
115
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100116static 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 Clausencadc2122013-09-18 21:02:00 +0100121 struct iio_dev *indio_dev = filep->private_data;
Alexandru Ardeleanfa83c3b2020-06-30 07:57:08 +0300122 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 Clausen2c001932012-01-03 14:59:39 +0100124 unsigned int copied;
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100125 int ret;
126
Lars-Peter Clausenf18e7a02013-10-04 12:06:00 +0100127 if (!indio_dev->info)
128 return -ENODEV;
129
Lars-Peter Clausen2c001932012-01-03 14:59:39 +0100130 if (count < sizeof(struct iio_event_data))
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100131 return -EINVAL;
132
Lars-Peter Clausenb91acca2014-02-14 18:49:00 +0000133 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 Clausend2f0a482013-10-04 12:07:00 +0100139 !kfifo_is_empty(&ev_int->det_events) ||
140 indio_dev->info == NULL);
Lars-Peter Clausenb91acca2014-02-14 18:49:00 +0000141 if (ret)
142 return ret;
143 if (indio_dev->info == NULL)
144 return -ENODEV;
Lars-Peter Clausend2f0a482013-10-04 12:07:00 +0100145 }
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100146
Lars-Peter Clausenb91acca2014-02-14 18:49:00 +0000147 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 Clausen0a769a92012-01-03 14:59:38 +0100151
Lars-Peter Clausenb91acca2014-02-14 18:49:00 +0000152 if (ret)
153 return ret;
Lars-Peter Clausen43ba1102012-01-03 14:59:40 +0100154
Lars-Peter Clausenb91acca2014-02-14 18:49:00 +0000155 /*
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 Clausen0a769a92012-01-03 14:59:38 +0100167}
168
169static int iio_event_chrdev_release(struct inode *inode, struct file *filep)
170{
Lars-Peter Clausencadc2122013-09-18 21:02:00 +0100171 struct iio_dev *indio_dev = filep->private_data;
Alexandru Ardeleanfa83c3b2020-06-30 07:57:08 +0300172 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 Clausen0a769a92012-01-03 14:59:38 +0100174
Lars-Peter Clausenb91acca2014-02-14 18:49:00 +0000175 clear_bit(IIO_BUSY_BIT_POS, &ev_int->flags);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100176
Lars-Peter Clausencadc2122013-09-18 21:02:00 +0100177 iio_device_put(indio_dev);
178
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100179 return 0;
180}
181
182static const struct file_operations iio_event_chrdev_fileops = {
183 .read = iio_event_chrdev_read,
Lars-Peter Clausene18045e2012-01-03 14:59:41 +0100184 .poll = iio_event_poll,
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100185 .release = iio_event_chrdev_release,
186 .owner = THIS_MODULE,
187 .llseek = noop_llseek,
188};
189
190int iio_event_getfd(struct iio_dev *indio_dev)
191{
Alexandru Ardeleanfa83c3b2020-06-30 07:57:08 +0300192 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 Clausen0a769a92012-01-03 14:59:38 +0100194 int fd;
195
196 if (ev_int == NULL)
197 return -ENODEV;
198
Gregor Boiriebc2b7da2016-03-09 19:05:49 +0100199 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 Clausenb91acca2014-02-14 18:49:00 +0000207
Lars-Peter Clausencadc2122013-09-18 21:02:00 +0100208 iio_device_get(indio_dev);
209
210 fd = anon_inode_getfd("iio:event", &iio_event_chrdev_fileops,
Greg Kroah-Hartmane2aad1d2013-09-25 08:59:04 -0700211 indio_dev, O_RDONLY | O_CLOEXEC);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100212 if (fd < 0) {
Lars-Peter Clausenb91acca2014-02-14 18:49:00 +0000213 clear_bit(IIO_BUSY_BIT_POS, &ev_int->flags);
Lars-Peter Clausencadc2122013-09-18 21:02:00 +0100214 iio_device_put(indio_dev);
Lars-Peter Clausenb91acca2014-02-14 18:49:00 +0000215 } else {
216 kfifo_reset_out(&ev_int->det_events);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100217 }
Lars-Peter Clausenb91acca2014-02-14 18:49:00 +0000218
Gregor Boiriebc2b7da2016-03-09 19:05:49 +0100219unlock:
220 mutex_unlock(&indio_dev->mlock);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100221 return fd;
222}
223
224static 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 Tirdea27be8422015-01-11 21:10:11 +0200230 [IIO_EV_TYPE_CHANGE] = "change",
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100231};
232
233static 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 Clausenb4e3ac02013-10-07 15:11:00 +0100239static const char * const iio_ev_info_text[] = {
240 [IIO_EV_INFO_ENABLE] = "en",
241 [IIO_EV_INFO_VALUE] = "value",
Lars-Peter Clausenec6670a2013-10-07 15:11:00 +0100242 [IIO_EV_INFO_HYSTERESIS] = "hysteresis",
Srinivas Pandruvada77a533c2014-08-07 23:29:00 +0100243 [IIO_EV_INFO_PERIOD] = "period",
Martin Fuzzey3f7f6422015-05-13 12:26:42 +0200244 [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 Clausenb4e3ac02013-10-07 15:11:00 +0100246};
247
248static 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
253static 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
258static enum iio_event_info iio_ev_attr_info(struct iio_dev_attr *attr)
259{
260 return (attr->address >> 16) & 0xffff;
261}
262
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100263static 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 Clausene53f5ac2012-05-12 15:39:33 +0200268 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100269 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 Clausencb955852013-12-07 10:45:00 +0000277 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 Clausenb4e3ac02013-10-07 15:11:00 +0100280
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100281 return (ret < 0) ? ret : len;
282}
283
284static ssize_t iio_ev_state_show(struct device *dev,
285 struct device_attribute *attr,
286 char *buf)
287{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200288 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100289 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
Lars-Peter Clausenb4e3ac02013-10-07 15:11:00 +0100290 int val;
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100291
Lars-Peter Clausencb955852013-12-07 10:45:00 +0000292 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 Clausen0a769a92012-01-03 14:59:38 +0100295 if (val < 0)
296 return val;
297 else
298 return sprintf(buf, "%d\n", val);
299}
300
301static ssize_t iio_ev_value_show(struct device *dev,
302 struct device_attribute *attr,
303 char *buf)
304{
Lars-Peter Clausene53f5ac2012-05-12 15:39:33 +0200305 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100306 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
Srinivas Pandruvada9fbfb4b2014-04-29 00:51:00 +0100307 int val, val2, val_arr[2];
Lars-Peter Clausenb4e3ac02013-10-07 15:11:00 +0100308 int ret;
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100309
Lars-Peter Clausencb955852013-12-07 10:45:00 +0000310 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 Pandruvada9fbfb4b2014-04-29 00:51:00 +0100316 val_arr[0] = val;
317 val_arr[1] = val2;
318 return iio_format_value(buf, ret, 2, val_arr);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100319}
320
321static 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 Clausene53f5ac2012-05-12 15:39:33 +0200326 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100327 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
Lars-Peter Clausenb4e3ac02013-10-07 15:11:00 +0100328 int val, val2;
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100329 int ret;
330
Lars-Peter Clausencb955852013-12-07 10:45:00 +0000331 if (!indio_dev->info->write_event_value)
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100332 return -EINVAL;
333
Lars-Peter Clausencb955852013-12-07 10:45:00 +0000334 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 Clausen0a769a92012-01-03 14:59:38 +0100341 if (ret < 0)
342 return ret;
343
344 return len;
345}
346
Lars-Peter Clausenb4e3ac02013-10-07 15:11:00 +0100347static 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 Ardeleanfa83c3b2020-06-30 07:57:08 +0300352 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
Lars-Peter Clausenb4e3ac02013-10-07 15:11:00 +0100353 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 Cameronef4b4852014-01-03 22:24:00 +0000361 for_each_set_bit(i, mask, sizeof(*mask)*8) {
362 if (i >= ARRAY_SIZE(iio_ev_info_text))
363 return -EINVAL;
Irina Tirdea1843c2f2014-11-10 14:45:31 +0200364 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 Clausenb4e3ac02013-10-07 15:11:00 +0100373 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 Ardeleanfa83c3b2020-06-30 07:57:08 +0300386 &iio_dev_opaque->event_interface->dev_attr_list);
Lars-Peter Clausenb4e3ac02013-10-07 15:11:00 +0100387 kfree(postfix);
388
Srinivas Pandruvada78b33212014-08-07 22:03:00 +0100389 if ((ret == -EBUSY) && (shared_by != IIO_SEPARATE))
390 continue;
391
Lars-Peter Clausenb4e3ac02013-10-07 15:11:00 +0100392 if (ret)
393 return ret;
394
395 attrcount++;
396 }
397
398 return attrcount;
399}
400
Lars-Peter Clausencb955852013-12-07 10:45:00 +0000401static int iio_device_add_event_sysfs(struct iio_dev *indio_dev,
Lars-Peter Clausenb4e3ac02013-10-07 15:11:00 +0100402 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 Knaack92825ff2014-02-16 11:53:00 +0000415 return ret;
Lars-Peter Clausenb4e3ac02013-10-07 15:11:00 +0100416 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 Knaack92825ff2014-02-16 11:53:00 +0000422 return ret;
Lars-Peter Clausenb4e3ac02013-10-07 15:11:00 +0100423 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 Knaack92825ff2014-02-16 11:53:00 +0000429 return ret;
Lars-Peter Clausenb4e3ac02013-10-07 15:11:00 +0100430 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 Knaack92825ff2014-02-16 11:53:00 +0000436 return ret;
Lars-Peter Clausenb4e3ac02013-10-07 15:11:00 +0100437 attrcount += ret;
438 }
439 ret = attrcount;
Lars-Peter Clausenb4e3ac02013-10-07 15:11:00 +0100440 return ret;
441}
442
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100443static inline int __iio_add_event_config_attrs(struct iio_dev *indio_dev)
444{
445 int j, ret, attrcount = 0;
446
Roberta Dobrescu2179aab2015-01-16 00:24:14 +0200447 /* Dynamically created from the channels array */
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100448 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 Lawalle3db9ef2012-10-21 11:52:00 +0100452 return ret;
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100453 attrcount += ret;
454 }
455 return attrcount;
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100456}
457
458static bool iio_check_for_dynamic_events(struct iio_dev *indio_dev)
459{
460 int j;
461
Lars-Peter Clausenb4e3ac02013-10-07 15:11:00 +0100462 for (j = 0; j < indio_dev->num_channels; j++) {
Lars-Peter Clausenb4e3ac02013-10-07 15:11:00 +0100463 if (indio_dev->channels[j].num_event_specs != 0)
464 return true;
465 }
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100466 return false;
467}
468
469static void iio_setup_ev_int(struct iio_event_interface *ev_int)
470{
Lars-Peter Clausen2c001932012-01-03 14:59:39 +0100471 INIT_KFIFO(ev_int->det_events);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100472 init_waitqueue_head(&ev_int->wait);
Lars-Peter Clausenb91acca2014-02-14 18:49:00 +0000473 mutex_init(&ev_int->read_lock);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100474}
475
476static const char *iio_event_group_name = "events";
477int iio_device_register_eventset(struct iio_dev *indio_dev)
478{
Alexandru Ardeleanfa83c3b2020-06-30 07:57:08 +0300479 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
Alexandru Ardeleanf2f45a52020-09-21 13:31:55 +0300480 struct iio_event_interface *ev_int;
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100481 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 Ardeleanf2f45a52020-09-21 13:31:55 +0300489 ev_int = kzalloc(sizeof(struct iio_event_interface), GFP_KERNEL);
490 if (ev_int == NULL)
Hartmut Knaack92825ff2014-02-16 11:53:00 +0000491 return -ENOMEM;
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100492
Alexandru Ardeleanf2f45a52020-09-21 13:31:55 +0300493 iio_dev_opaque->event_interface = ev_int;
Sascha Hauer46b24312012-07-03 10:55:40 +0200494
Alexandru Ardeleanf2f45a52020-09-21 13:31:55 +0300495 INIT_LIST_HEAD(&ev_int->dev_attr_list);
496
497 iio_setup_ev_int(ev_int);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100498 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 Ardeleanf2f45a52020-09-21 13:31:55 +0300511 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 Clausen0a769a92012-01-03 14:59:38 +0100516 ret = -ENOMEM;
517 goto error_free_setup_event_lines;
518 }
519 if (indio_dev->info->event_attrs)
Alexandru Ardeleanf2f45a52020-09-21 13:31:55 +0300520 memcpy(ev_int->group.attrs,
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100521 indio_dev->info->event_attrs->attrs,
Alexandru Ardeleanf2f45a52020-09-21 13:31:55 +0300522 sizeof(ev_int->group.attrs[0]) * attrcount_orig);
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100523 attrn = attrcount_orig;
524 /* Add all elements from the list. */
Alexandru Ardeleanf2f45a52020-09-21 13:31:55 +0300525 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 Clausen0a769a92012-01-03 14:59:38 +0100528
529 return 0;
530
531error_free_setup_event_lines:
Alexandru Ardeleanf2f45a52020-09-21 13:31:55 +0300532 iio_free_chan_devattr_list(&ev_int->dev_attr_list);
533 kfree(ev_int);
Alexandru Ardeleanfa83c3b2020-06-30 07:57:08 +0300534 iio_dev_opaque->event_interface = NULL;
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100535 return ret;
536}
537
Lars-Peter Clausend2f0a482013-10-04 12:07:00 +0100538/**
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 */
545void iio_device_wakeup_eventset(struct iio_dev *indio_dev)
546{
Alexandru Ardeleanfa83c3b2020-06-30 07:57:08 +0300547 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
548
549 if (iio_dev_opaque->event_interface == NULL)
Lars-Peter Clausend2f0a482013-10-04 12:07:00 +0100550 return;
Alexandru Ardeleanfa83c3b2020-06-30 07:57:08 +0300551 wake_up(&iio_dev_opaque->event_interface->wait);
Lars-Peter Clausend2f0a482013-10-04 12:07:00 +0100552}
553
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100554void iio_device_unregister_eventset(struct iio_dev *indio_dev)
555{
Alexandru Ardeleanfa83c3b2020-06-30 07:57:08 +0300556 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
Alexandru Ardeleanf2f45a52020-09-21 13:31:55 +0300557 struct iio_event_interface *ev_int = iio_dev_opaque->event_interface;
Alexandru Ardeleanfa83c3b2020-06-30 07:57:08 +0300558
Alexandru Ardeleanf2f45a52020-09-21 13:31:55 +0300559 if (ev_int == NULL)
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100560 return;
Alexandru Ardeleanf2f45a52020-09-21 13:31:55 +0300561 iio_free_chan_devattr_list(&ev_int->dev_attr_list);
562 kfree(ev_int->group.attrs);
563 kfree(ev_int);
Alexandru Ardeleana3598d12020-09-21 13:31:56 +0300564 iio_dev_opaque->event_interface = NULL;
Lars-Peter Clausen0a769a92012-01-03 14:59:38 +0100565}