1 /* Industrial I/O event handling
3 * Copyright (c) 2008 Jonathan Cameron
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
9 * Based on elements of hwmon and input subsystems.
12 #include <linux/anon_inodes.h>
13 #include <linux/device.h>
15 #include <linux/kernel.h>
16 #include <linux/kfifo.h>
17 #include <linux/module.h>
18 #include <linux/poll.h>
19 #include <linux/sched.h>
20 #include <linux/slab.h>
21 #include <linux/uaccess.h>
22 #include <linux/wait.h>
23 #include <linux/iio/iio.h>
25 #include <linux/iio/sysfs.h>
26 #include <linux/iio/events.h>
29 * struct iio_event_interface - chrdev interface for an event line
30 * @wait: wait queue to allow blocking reads of events
31 * @det_events: list of detected events
32 * @dev_attr_list: list of event interface sysfs attribute
33 * @flags: file operations related flags including busy flag.
34 * @group: event interface sysfs attribute group
36 struct iio_event_interface
{
37 wait_queue_head_t wait
;
38 DECLARE_KFIFO(det_events
, struct iio_event_data
, 16);
40 struct list_head dev_attr_list
;
42 struct attribute_group group
;
45 int iio_push_event(struct iio_dev
*indio_dev
, u64 ev_code
, s64 timestamp
)
47 struct iio_event_interface
*ev_int
= indio_dev
->event_interface
;
48 struct iio_event_data ev
;
52 /* Does anyone care? */
53 spin_lock_irqsave(&ev_int
->wait
.lock
, flags
);
54 if (test_bit(IIO_BUSY_BIT_POS
, &ev_int
->flags
)) {
57 ev
.timestamp
= timestamp
;
59 copied
= kfifo_put(&ev_int
->det_events
, &ev
);
61 wake_up_locked_poll(&ev_int
->wait
, POLLIN
);
63 spin_unlock_irqrestore(&ev_int
->wait
.lock
, flags
);
67 EXPORT_SYMBOL(iio_push_event
);
70 * iio_event_poll() - poll the event queue to find out if it has data
72 static unsigned int iio_event_poll(struct file
*filep
,
73 struct poll_table_struct
*wait
)
75 struct iio_dev
*indio_dev
= filep
->private_data
;
76 struct iio_event_interface
*ev_int
= indio_dev
->event_interface
;
77 unsigned int events
= 0;
79 poll_wait(filep
, &ev_int
->wait
, wait
);
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
);
89 static ssize_t
iio_event_chrdev_read(struct file
*filep
,
94 struct iio_dev
*indio_dev
= filep
->private_data
;
95 struct iio_event_interface
*ev_int
= indio_dev
->event_interface
;
99 if (count
< sizeof(struct iio_event_data
))
102 spin_lock_irq(&ev_int
->wait
.lock
);
103 if (kfifo_is_empty(&ev_int
->det_events
)) {
104 if (filep
->f_flags
& O_NONBLOCK
) {
108 /* Blocking on device; waiting for something to be there */
109 ret
= wait_event_interruptible_locked_irq(ev_int
->wait
,
110 !kfifo_is_empty(&ev_int
->det_events
));
113 /* Single access device so no one else can get the data */
116 ret
= kfifo_to_user(&ev_int
->det_events
, buf
, count
, &copied
);
119 spin_unlock_irq(&ev_int
->wait
.lock
);
121 return ret
? ret
: copied
;
124 static int iio_event_chrdev_release(struct inode
*inode
, struct file
*filep
)
126 struct iio_dev
*indio_dev
= filep
->private_data
;
127 struct iio_event_interface
*ev_int
= indio_dev
->event_interface
;
129 spin_lock_irq(&ev_int
->wait
.lock
);
130 __clear_bit(IIO_BUSY_BIT_POS
, &ev_int
->flags
);
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.
136 kfifo_reset_out(&ev_int
->det_events
);
137 spin_unlock_irq(&ev_int
->wait
.lock
);
139 iio_device_put(indio_dev
);
144 static const struct file_operations iio_event_chrdev_fileops
= {
145 .read
= iio_event_chrdev_read
,
146 .poll
= iio_event_poll
,
147 .release
= iio_event_chrdev_release
,
148 .owner
= THIS_MODULE
,
149 .llseek
= noop_llseek
,
152 int iio_event_getfd(struct iio_dev
*indio_dev
)
154 struct iio_event_interface
*ev_int
= indio_dev
->event_interface
;
160 spin_lock_irq(&ev_int
->wait
.lock
);
161 if (__test_and_set_bit(IIO_BUSY_BIT_POS
, &ev_int
->flags
)) {
162 spin_unlock_irq(&ev_int
->wait
.lock
);
165 spin_unlock_irq(&ev_int
->wait
.lock
);
166 iio_device_get(indio_dev
);
168 fd
= anon_inode_getfd("iio:event", &iio_event_chrdev_fileops
,
169 indio_dev
, O_RDONLY
);
171 spin_lock_irq(&ev_int
->wait
.lock
);
172 __clear_bit(IIO_BUSY_BIT_POS
, &ev_int
->flags
);
173 spin_unlock_irq(&ev_int
->wait
.lock
);
174 iio_device_put(indio_dev
);
179 static const char * const iio_ev_type_text
[] = {
180 [IIO_EV_TYPE_THRESH
] = "thresh",
181 [IIO_EV_TYPE_MAG
] = "mag",
182 [IIO_EV_TYPE_ROC
] = "roc",
183 [IIO_EV_TYPE_THRESH_ADAPTIVE
] = "thresh_adaptive",
184 [IIO_EV_TYPE_MAG_ADAPTIVE
] = "mag_adaptive",
187 static const char * const iio_ev_dir_text
[] = {
188 [IIO_EV_DIR_EITHER
] = "either",
189 [IIO_EV_DIR_RISING
] = "rising",
190 [IIO_EV_DIR_FALLING
] = "falling"
193 static ssize_t
iio_ev_state_store(struct device
*dev
,
194 struct device_attribute
*attr
,
198 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
199 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
203 ret
= strtobool(buf
, &val
);
207 ret
= indio_dev
->info
->write_event_config(indio_dev
,
210 return (ret
< 0) ? ret
: len
;
213 static ssize_t
iio_ev_state_show(struct device
*dev
,
214 struct device_attribute
*attr
,
217 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
218 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
219 int val
= indio_dev
->info
->read_event_config(indio_dev
,
225 return sprintf(buf
, "%d\n", val
);
228 static ssize_t
iio_ev_value_show(struct device
*dev
,
229 struct device_attribute
*attr
,
232 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
233 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
236 ret
= indio_dev
->info
->read_event_value(indio_dev
,
237 this_attr
->address
, &val
);
241 return sprintf(buf
, "%d\n", val
);
244 static ssize_t
iio_ev_value_store(struct device
*dev
,
245 struct device_attribute
*attr
,
249 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
250 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
254 if (!indio_dev
->info
->write_event_value
)
257 ret
= kstrtoint(buf
, 10, &val
);
261 ret
= indio_dev
->info
->write_event_value(indio_dev
, this_attr
->address
,
269 static int iio_device_add_event_sysfs(struct iio_dev
*indio_dev
,
270 struct iio_chan_spec
const *chan
)
272 int ret
= 0, i
, attrcount
= 0;
275 if (!chan
->event_mask
)
278 for_each_set_bit(i
, &chan
->event_mask
, sizeof(chan
->event_mask
)*8) {
279 postfix
= kasprintf(GFP_KERNEL
, "%s_%s_en",
280 iio_ev_type_text
[i
/IIO_EV_DIR_MAX
],
281 iio_ev_dir_text
[i
%IIO_EV_DIR_MAX
]);
282 if (postfix
== NULL
) {
287 mask
= IIO_MOD_EVENT_CODE(chan
->type
, 0, chan
->channel2
,
290 else if (chan
->differential
)
291 mask
= IIO_EVENT_CODE(chan
->type
,
299 mask
= IIO_UNMOD_EVENT_CODE(chan
->type
,
304 ret
= __iio_add_chan_devattr(postfix
,
311 &indio_dev
->event_interface
->
317 postfix
= kasprintf(GFP_KERNEL
, "%s_%s_value",
318 iio_ev_type_text
[i
/IIO_EV_DIR_MAX
],
319 iio_ev_dir_text
[i
%IIO_EV_DIR_MAX
]);
320 if (postfix
== NULL
) {
324 ret
= __iio_add_chan_devattr(postfix
, chan
,
330 &indio_dev
->event_interface
->
342 static inline void __iio_remove_event_config_attrs(struct iio_dev
*indio_dev
)
344 struct iio_dev_attr
*p
, *n
;
345 list_for_each_entry_safe(p
, n
,
346 &indio_dev
->event_interface
->
348 kfree(p
->dev_attr
.attr
.name
);
353 static inline int __iio_add_event_config_attrs(struct iio_dev
*indio_dev
)
355 int j
, ret
, attrcount
= 0;
357 /* Dynically created from the channels array */
358 for (j
= 0; j
< indio_dev
->num_channels
; j
++) {
359 ret
= iio_device_add_event_sysfs(indio_dev
,
360 &indio_dev
->channels
[j
]);
368 static bool iio_check_for_dynamic_events(struct iio_dev
*indio_dev
)
372 for (j
= 0; j
< indio_dev
->num_channels
; j
++)
373 if (indio_dev
->channels
[j
].event_mask
!= 0)
378 static void iio_setup_ev_int(struct iio_event_interface
*ev_int
)
380 INIT_KFIFO(ev_int
->det_events
);
381 init_waitqueue_head(&ev_int
->wait
);
384 static const char *iio_event_group_name
= "events";
385 int iio_device_register_eventset(struct iio_dev
*indio_dev
)
387 struct iio_dev_attr
*p
;
388 int ret
= 0, attrcount_orig
= 0, attrcount
, attrn
;
389 struct attribute
**attr
;
391 if (!(indio_dev
->info
->event_attrs
||
392 iio_check_for_dynamic_events(indio_dev
)))
395 indio_dev
->event_interface
=
396 kzalloc(sizeof(struct iio_event_interface
), GFP_KERNEL
);
397 if (indio_dev
->event_interface
== NULL
) {
402 INIT_LIST_HEAD(&indio_dev
->event_interface
->dev_attr_list
);
404 iio_setup_ev_int(indio_dev
->event_interface
);
405 if (indio_dev
->info
->event_attrs
!= NULL
) {
406 attr
= indio_dev
->info
->event_attrs
->attrs
;
407 while (*attr
++ != NULL
)
410 attrcount
= attrcount_orig
;
411 if (indio_dev
->channels
) {
412 ret
= __iio_add_event_config_attrs(indio_dev
);
414 goto error_free_setup_event_lines
;
418 indio_dev
->event_interface
->group
.name
= iio_event_group_name
;
419 indio_dev
->event_interface
->group
.attrs
= kcalloc(attrcount
+ 1,
420 sizeof(indio_dev
->event_interface
->group
.attrs
[0]),
422 if (indio_dev
->event_interface
->group
.attrs
== NULL
) {
424 goto error_free_setup_event_lines
;
426 if (indio_dev
->info
->event_attrs
)
427 memcpy(indio_dev
->event_interface
->group
.attrs
,
428 indio_dev
->info
->event_attrs
->attrs
,
429 sizeof(indio_dev
->event_interface
->group
.attrs
[0])
431 attrn
= attrcount_orig
;
432 /* Add all elements from the list. */
433 list_for_each_entry(p
,
434 &indio_dev
->event_interface
->dev_attr_list
,
436 indio_dev
->event_interface
->group
.attrs
[attrn
++] =
438 indio_dev
->groups
[indio_dev
->groupcounter
++] =
439 &indio_dev
->event_interface
->group
;
443 error_free_setup_event_lines
:
444 __iio_remove_event_config_attrs(indio_dev
);
445 kfree(indio_dev
->event_interface
);
451 void iio_device_unregister_eventset(struct iio_dev
*indio_dev
)
453 if (indio_dev
->event_interface
== NULL
)
455 __iio_remove_event_config_attrs(indio_dev
);
456 kfree(indio_dev
->event_interface
->group
.attrs
);
457 kfree(indio_dev
->event_interface
);