staging:iio: Event handling updates
Subsystems (3)
v2 cover staging:iio: Event handling updates
Hi Greg,
This series is a resend of the same series form early January, with two minor issues in patch 4 and 5 being fixed. Also the order of patch 5 and 6 in the series has been changed.
The series starts with fixing some documentation issues for the iio_event_interface struct. The second patch moves the IIO event handling code from the IIO core file to its own file with the next patches containing some cleanups to the event handling code. The last patch implements poll support for IIO event handling fds.
- Lars
Lars-Peter Clausen (6):
staging:iio: Update iio_event_interface documentation staging:iio: Factor out event handling into its own file staging:iio:events: Use kfifo for event queue staging:iio:events: Use waitqueue lock to protect event queue staging:iio:events: Use non-atomic bitops staging:iio:events: Add poll support
drivers/staging/iio/Makefile | 2 +- drivers/staging/iio/iio_core.h | 4 + drivers/staging/iio/industrialio-core.c | 458 ------------------------------ drivers/staging/iio/industrialio-event.c | 453 +++++++++++++++++++++++++++++ 4 files changed, 458 insertions(+), 459 deletions(-) create mode 100644 drivers/staging/iio/industrialio-event.c
Signature · 1 line
1.7.8.3
PATCH v2 1/6 staging:iio: Update iio_event_interface documentation
The documentation for the iio_event_interface does not match the actual struct anymore. This patch removes the documentation for non-existing fields and adds documentation for missing fields.
--- drivers/staging/iio/industrialio-core.c | 3 ++- 1 files changed, 2 insertions(+), 1 deletions(-)
drivers/staging/iio/industrialio-core.c +2 -1
@@ -112,13 +112,14 @@ struct iio_detected_event_list {/** * struct iio_event_interface - chrdev interface for an event line -* @dev: device assocated with event interface * @wait: wait queue to allow blocking reads of events * @event_list_lock: mutex to protect the list of detected events * @det_events: list of detected events * @max_events: maximum number of events before new ones are dropped * @current_events: number of events in detected list +* @dev_attr_list: list of event interface sysfs attribute * @flags: file operations related flags including busy flag. +* @group: event interface sysfs attribute group */ struct iio_event_interface {wait_queue_head_t wait;
Signature · 1 line
1.7.8.3
Greg Kroah-Hartman
maintainerRe: [PATCH v2 1/6] staging:iio: Update iio_event_interface documentation
Quoted 9 lines · Lars-Peter Clausen
The documentation for the
iio_event_interfacedoes not match the actual struct anymore. This patch removes the documentation for non-existing fields and adds documentation for missing fields.Signed-off-by Lars-Peter Clausen <[email protected]>Acked-by Jonathan Cameron <[email protected]>--- drivers/staging/iio/industrialio-core.c | 3 ++- 1 files changed, 2 insertions(+), 1 deletions(-)
This patch fails to apply to my tree, possibly because of your other patches I accepted.
Care to redo this series against the next linux-next release and resend?
thanks,
greg k-h
Re: [PATCH v2 1/6] staging:iio: Update iio_event_interface documentation
Quoted 11 lines · Greg Kroah-Hartman
Quoted 9 lines · Lars-Peter Clausen
The documentation for the
iio_event_interfacedoes not match the actual struct anymore. This patch removes the documentation for non-existing fields and adds documentation for missing fields.Signed-off-by Lars-Peter Clausen <[email protected]>Acked-by Jonathan Cameron <[email protected]>--- drivers/staging/iio/industrialio-core.c | 3 ++- 1 files changed, 2 insertions(+), 1 deletions(-)
Quoted 5 lines
This patch fails to apply to my tree, possibly because of your other patches I accepted.
Care to redo this series against the next linux-next release and resend?
You've applied v1 of the series, but the diffwas only a minor documentation fix anyway, so I'll sent it as an individual patch.
Thanks,
- Lars
PATCH v2 2/6 staging:iio: Factor out event handling into its own file
In reply to Lars-Peter ClausenThe core iio file has gotten quite cluttered over time. This patch moves the event handling code into its own file. Since the event handling code is largely independent from the core code the only code changes necessary for this are to make the moved iio_device_register_eventset, iio_device_unregister_eventset and iio_event_getfd functions non static.
This has also the advantage that industrialio-core.c is now closer again to its counterpart in the outofstaging branch.
---
Changes since v1:
* Add missing include to sched.h
--- drivers/staging/iio/Makefile | 2 +- drivers/staging/iio/iio_core.h | 4 + drivers/staging/iio/industrialio-core.c | 459 ---------------------------- drivers/staging/iio/industrialio-event.c | 483 ++++++++++++++++++++++++++++++ 4 files changed, 488 insertions(+), 460 deletions(-) create mode 100644 drivers/staging/iio/industrialio-event.c
drivers/staging/iio/Makefile +1 -1
@@ -3,7 +3,7 @@ # obj-$(CONFIG_IIO) += industrialio.o -industrialio-y := industrialio-core.o +industrialio-y := industrialio-core.o industrialio-event.o industrialio-$(CONFIG_IIO_BUFFER) += industrialio-buffer.o industrialio-$(CONFIG_IIO_TRIGGER) += industrialio-trigger.o
drivers/staging/iio/iio_core.h +4 -0
@@ -49,4 +49,8 @@ ssize_t iio_buffer_read_first_n_outer(struct file *filp, char __user *buf,#endif +int iio_device_register_eventset (struct iio_dev *indio_dev); +void iio_device_unregister_eventset (struct iio_dev *indio_dev); +int iio_event_getfd (struct iio_dev *indio_dev); +#endif
drivers/staging/iio/industrialio-core.c +0 -459
477 lines not on this page · raw message
drivers/staging/iio/industrialio-event.c new file +483 -0
483 lines not on this page · raw message
Signature · 1 line
1.7.8.3
PATCH v2 3/6 staging:iio:events: Use kfifo for event queue
In reply to Lars-Peter ClausenThe current IIO event code uses a list to emulate FIFO like behavior. Just use a kfifo directly instead to implement the event queue. As part of this patch the maximum of events in the queue is increased from 10 to 16 since kfifo requires a power of two for the number of FIFO elements.
--- drivers/staging/iio/industrialio-event.c | 85 +++++++---------------------- 1 files changed, 21 insertions(+), 64 deletions(-)
drivers/staging/iio/industrialio-event.c +21 -64
@@ -13,6 +13,7 @@#include <linux/device.h> #include <linux/fs.h> #include <linux/kernel.h> +#include <linux/kfifo.h> #include <linux/module.h> #include <linux/sched.h> #include <linux/slab.h>
@@ -24,22 +25,10 @@#include "events.h" /** -* struct iio_detected_event_list - list element for events that have occurred -* @list: linked list header -* @ev: the event itself -*/ -struct iio_detected_event_list { -struct list_head list; -struct iio_event_data ev; -}; - -/** * struct iio_event_interface - chrdev interface for an event line * @wait: wait queue to allow blocking reads of events * @event_list_lock: mutex to protect the list of detected events * @det_events: list of detected events -* @max_events: maximum number of events before new ones are dropped -* @current_events: number of events in detected list * @dev_attr_list: list of event interface sysfs attribute * @flags: file operations related flags including busy flag. * @group: event interface sysfs attribute group
@@ -47,9 +36,8 @@ struct iio_detected_event_list {struct iio_event_interface {wait_queue_head_t wait;struct mutex event_list_lock; -struct list_head det_events; -int max_events; -int current_events; + DECLARE_KFIFO(det_events, struct iio_event_data, 16); +struct list_head dev_attr_list;unsigned long flags;struct attribute_group group;
@@ -58,34 +46,25 @@ struct iio_event_interface {int iio_push_event (struct iio_dev *indio_dev,u64 ev_code,s64 timestamp) {struct iio_event_interface *ev_int = indio_dev->event_interface; -struct iio_detected_event_list *ev; -int ret =0 ; +struct iio_event_data ev; +int copied;/* Does anyone care? */ mutex_lock (&ev_int->event_list_lock);if (test_bit (IIO_BUSY_BIT_POS , &ev_int->flags)) { -if (ev_int->current_events == ev_int->max_events) { -mutex_unlock (&ev_int->event_list_lock); -return 0 ; - } - ev =kmalloc (sizeof (*ev),GFP_KERNEL ); -if (ev ==NULL ) { - ret = -ENOMEM ; -mutex_unlock (&ev_int->event_list_lock); - goto error_ret; - } - ev->ev.id = ev_code; - ev->ev.timestamp = timestamp; -list_add_tail (&ev->list, &ev_int->det_events); - ev_int->current_events++; + ev.id = ev_code; + ev.timestamp = timestamp; + + copied =kfifo_put (&ev_int->det_events, &ev); +mutex_unlock (&ev_int->event_list_lock); -wake_up_interruptible (&ev_int->wait); +if (copied !=0 ) +wake_up_interruptible (&ev_int->wait); }else mutex_unlock (&ev_int->event_list_lock); -error_ret: -return ret; +return 0 ; }EXPORT_SYMBOL (iio_push_event);
@@ -95,15 +74,14 @@ static ssize_t iio_event_chrdev_read(struct file *filep, loff_t *f_ps) {struct iio_event_interface *ev_int = filep->private_data; -struct iio_detected_event_list *el; -size_t len =sizeof (el->ev); +unsigned int copied;int ret; -if (count < len) +if (count <sizeof (struct iio_event_data ))return -EINVAL ;mutex_lock (&ev_int->event_list_lock); -if (list_empty (&ev_int->det_events)) { +if (kfifo_is_empty (&ev_int->det_events)) {if (filep->f_flags &O_NONBLOCK ) { ret = -EAGAIN ; goto error_mutex_unlock;
@@ -111,39 +89,25 @@ static ssize_t iio_event_chrdev_read(struct file *filep,mutex_unlock (&ev_int->event_list_lock);/* Blocking on device; waiting for something to be there */ ret =wait_event_interruptible (ev_int->wait, - !list_empty (&ev_int - ->det_events)); + !kfifo_is_empty (&ev_int->det_events));if (ret) goto error_ret;/* Single access device so no one else can get the data */ mutex_lock (&ev_int->event_list_lock); } - el =list_first_entry (&ev_int->det_events, - struct iio_detected_event_list, - list); -if (copy_to_user (buf, &(el->ev), len)) { - ret = -EFAULT ; - goto error_mutex_unlock; - } -list_del (&el->list); - ev_int->current_events--;mutex_unlock (&ev_int->event_list_lock); -kfree (el); - -return len; + ret =kfifo_to_user (&ev_int->det_events, buf, count, &copied); error_mutex_unlock:mutex_unlock (&ev_int->event_list_lock); error_ret: - -return ret; +return ret ? ret : copied; }static int iio_event_chrdev_release (struct inode *inode,struct file *filep) {struct iio_event_interface *ev_int = filep->private_data; -struct iio_detected_event_list *el, *t;mutex_lock (&ev_int->event_list_lock);clear_bit (IIO_BUSY_BIT_POS , &ev_int->flags);
@@ -152,11 +116,7 @@ static int iio_event_chrdev_release(struct inode *inode, struct file *filep)* clear out any awaiting events. The mask will prevent * any new __iio_push_event calls running. */ -list_for_each_entry_safe (el, t, &ev_int->det_events, list) { -list_del (&el->list); -kfree (el); - } - ev_int->current_events =0 ; +kfifo_reset_out (&ev_int->det_events);mutex_unlock (&ev_int->event_list_lock);return 0 ;
@@ -401,10 +361,7 @@ static bool iio_check_for_dynamic_events(struct iio_dev *indio_dev)static void iio_setup_ev_int (struct iio_event_interface *ev_int) {mutex_init (&ev_int->event_list_lock); -/* discussion point - make this variable? */ - ev_int->max_events =10 ; - ev_int->current_events =0 ; -INIT_LIST_HEAD (&ev_int->det_events); +INIT_KFIFO (ev_int->det_events);init_waitqueue_head (&ev_int->wait); }
Signature · 1 line
1.7.8.3
PATCH v2 4/6 staging:iio:events: Use waitqueue lock to protect event queue
In reply to Lars-Peter ClausenUse the waitqueue lock to protect the event queue instead of a custom mutex. This has the advantage that we can call the waitqueue operations with the lock held, which simplifies the code flow a bit.
--
Changes since v1:
* Also remove documentation for event_list_lock
--- drivers/staging/iio/industrialio-event.c | 44 ++++++++++++----------------- 1 files changed, 18 insertions(+), 26 deletions(-)
drivers/staging/iio/industrialio-event.c +18 -26
@@ -27,7 +27,6 @@/** * struct iio_event_interface - chrdev interface for an event line * @wait: wait queue to allow blocking reads of events -* @event_list_lock: mutex to protect the list of detected events * @det_events: list of detected events * @dev_attr_list: list of event interface sysfs attribute * @flags: file operations related flags including busy flag.
@@ -35,7 +34,6 @@*/ struct iio_event_interface {wait_queue_head_t wait; -struct mutex event_list_lock; DECLARE_KFIFO(det_events, struct iio_event_data, 16);struct list_head dev_attr_list;
@@ -50,19 +48,17 @@ int iio_push_event(struct iio_dev *indio_dev, u64 ev_code, s64 timestamp)int copied;/* Does anyone care? */ -mutex_lock (&ev_int->event_list_lock); +spin_lock (&ev_int->wait.lock);if (test_bit (IIO_BUSY_BIT_POS , &ev_int->flags)) { ev.id = ev_code; ev.timestamp = timestamp; copied =kfifo_put (&ev_int->det_events, &ev); - -mutex_unlock (&ev_int->event_list_lock);if (copied !=0 ) -wake_up_interruptible (&ev_int->wait); - }else -mutex_unlock (&ev_int->event_list_lock); +wake_up_locked (&ev_int->wait); + } +spin_unlock (&ev_int->wait.lock);return 0 ; }
@@ -80,28 +76,25 @@ static ssize_t iio_event_chrdev_read(struct file *filep,if (count <sizeof (struct iio_event_data ))return -EINVAL ; -mutex_lock (&ev_int->event_list_lock); +spin_lock (&ev_int->wait.lock);if (kfifo_is_empty (&ev_int->det_events)) {if (filep->f_flags &O_NONBLOCK ) { ret = -EAGAIN ; - goto error_mutex_unlock; + goto error_unlock; } -mutex_unlock (&ev_int->event_list_lock);/* Blocking on device; waiting for something to be there */ - ret =wait_event_interruptible (ev_int->wait, + ret =wait_event_interruptible_locked (ev_int->wait, !kfifo_is_empty (&ev_int->det_events));if (ret) - goto error_ret; + goto error_unlock;/* Single access device so no one else can get the data */ -mutex_lock (&ev_int->event_list_lock); } -mutex_unlock (&ev_int->event_list_lock); ret =kfifo_to_user (&ev_int->det_events, buf, count, &copied); -error_mutex_unlock: -mutex_unlock (&ev_int->event_list_lock); -error_ret: +error_unlock: +spin_unlock (&ev_int->wait.lock); +return ret ? ret : copied; }
@@ -109,7 +102,7 @@ static int iio_event_chrdev_release(struct inode *inode, struct file *filep) {struct iio_event_interface *ev_int = filep->private_data; -mutex_lock (&ev_int->event_list_lock); +spin_lock (&ev_int->wait.lock);clear_bit (IIO_BUSY_BIT_POS , &ev_int->flags);/* * In order to maintain a clean state for reopening,
@@ -117,7 +110,7 @@ static int iio_event_chrdev_release(struct inode *inode, struct file *filep)* any new __iio_push_event calls running. */ kfifo_reset_out (&ev_int->det_events); -mutex_unlock (&ev_int->event_list_lock); +spin_unlock (&ev_int->wait.lock);return 0 ; }
@@ -137,18 +130,18 @@ int iio_event_getfd(struct iio_dev *indio_dev)if (ev_int ==NULL )return -ENODEV ; -mutex_lock (&ev_int->event_list_lock); +spin_lock (&ev_int->wait.lock);if (test_and_set_bit (IIO_BUSY_BIT_POS , &ev_int->flags)) { -mutex_unlock (&ev_int->event_list_lock); +spin_unlock (&ev_int->wait.lock);return -EBUSY ; } -mutex_unlock (&ev_int->event_list_lock); +spin_unlock (&ev_int->wait.lock); fd =anon_inode_getfd ("iio:event" , &iio_event_chrdev_fileops, ev_int,O_RDONLY );if (fd <0 ) { -mutex_lock (&ev_int->event_list_lock); +spin_lock (&ev_int->wait.lock);clear_bit (IIO_BUSY_BIT_POS , &ev_int->flags); -mutex_unlock (&ev_int->event_list_lock); +spin_unlock (&ev_int->wait.lock); }return fd; }
@@ -360,7 +353,6 @@ static bool iio_check_for_dynamic_events(struct iio_dev *indio_dev)static void iio_setup_ev_int (struct iio_event_interface *ev_int) { -mutex_init (&ev_int->event_list_lock);INIT_KFIFO (ev_int->det_events);init_waitqueue_head (&ev_int->wait); }
Signature · 1 line
1.7.8.3
PATCH v2 5/6 staging:iio:events: Use non-atomic bitops
In reply to Lars-Peter ClausenWe always hold the waitqueue lock when modifying the flags field. So it is safe to use the non-atomic bitops here instead of the atomic versions.
The lock has to be held, because we need to clear the busy flag and flush the event fifo in one atomic operation when closing the event file descriptor.
---
Changes since v1:
* Fix typo in subject
--- drivers/staging/iio/industrialio-event.c | 6 +++--- 1 files changed, 3 insertions(+), 3 deletions(-)
drivers/staging/iio/industrialio-event.c +3 -3
@@ -103,7 +103,7 @@ static int iio_event_chrdev_release(struct inode *inode, struct file *filep)struct iio_event_interface *ev_int = filep->private_data;spin_lock (&ev_int->wait.lock); -clear_bit (IIO_BUSY_BIT_POS , &ev_int->flags); +__clear_bit (IIO_BUSY_BIT_POS , &ev_int->flags);/* * In order to maintain a clean state for reopening, * clear out any awaiting events. The mask will prevent
@@ -131,7 +131,7 @@ int iio_event_getfd(struct iio_dev *indio_dev)return -ENODEV ;spin_lock (&ev_int->wait.lock); -if (test_and_set_bit (IIO_BUSY_BIT_POS , &ev_int->flags)) { +if (__test_and_set_bit (IIO_BUSY_BIT_POS , &ev_int->flags)) {spin_unlock (&ev_int->wait.lock);return -EBUSY ; }
@@ -140,7 +140,7 @@ int iio_event_getfd(struct iio_dev *indio_dev) &iio_event_chrdev_fileops, ev_int, O_RDONLY);if (fd <0 ) {spin_lock (&ev_int->wait.lock); -clear_bit (IIO_BUSY_BIT_POS , &ev_int->flags); +__clear_bit (IIO_BUSY_BIT_POS , &ev_int->flags);spin_unlock (&ev_int->wait.lock); }return fd;
Signature · 1 line
1.7.8.3
PATCH v2 6/6 staging:iio:events: Add poll support
In reply to Lars-Peter ClausenAdd poll support to the event queue. This will allow us to check for pending events in a application's event loop using poll() or similar. Since we already have support for blocking reads adding poll support as well is trivial.
--- drivers/staging/iio/industrialio-event.c | 23 ++++++++++++++++++++++- 1 files changed, 22 insertions(+), 1 deletions(-)
drivers/staging/iio/industrialio-event.c +22 -1
@@ -15,6 +15,7 @@#include <linux/kernel.h> #include <linux/kfifo.h> #include <linux/module.h> +#include <linux/poll.h> #include <linux/sched.h> #include <linux/slab.h> #include <linux/uaccess.h>
@@ -56,7 +57,7 @@ int iio_push_event(struct iio_dev *indio_dev, u64 ev_code, s64 timestamp) copied =kfifo_put (&ev_int->det_events, &ev);if (copied !=0 ) -wake_up_locked (&ev_int->wait); +wake_up_locked_poll (&ev_int->wait,POLLIN ); }spin_unlock (&ev_int->wait.lock);
@@ -64,6 +65,25 @@ int iio_push_event(struct iio_dev *indio_dev, u64 ev_code, s64 timestamp) }EXPORT_SYMBOL (iio_push_event); +/** +* iio_event_poll() - poll the event queue to find out if it has data +*/ +static unsigned int iio_event_poll (struct file *filep, +struct poll_table_struct *wait) +{ +struct iio_event_interface *ev_int = filep->private_data; +unsigned int events =0 ; + +poll_wait (filep, &ev_int->wait, wait); + +spin_lock (&ev_int->wait.lock); +if (!kfifo_is_empty (&ev_int->det_events)) + events =POLLIN |POLLRDNORM ; +spin_unlock (&ev_int->wait.lock); + +return events; +} + static ssize_t iio_event_chrdev_read(struct file *filep, char __user *buf, size_t count,
@@ -117,6 +137,7 @@ static int iio_event_chrdev_release(struct inode *inode, struct file *filep)static const struct file_operations iio_event_chrdev_fileops = { .read = iio_event_chrdev_read, + .poll = iio_event_poll, .release = iio_event_chrdev_release, .owner =THIS_MODULE , .llseek = noop_llseek,
Signature · 1 line
1.7.8.3