1 /* The industrial I/O core
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/kernel.h>
13 #include <linux/module.h>
14 #include <linux/idr.h>
15 #include <linux/kdev_t.h>
16 #include <linux/err.h>
17 #include <linux/device.h>
19 #include <linux/interrupt.h>
20 #include <linux/poll.h>
21 #include <linux/sched.h>
22 #include <linux/wait.h>
23 #include <linux/cdev.h>
25 #include "trigger_consumer.h"
27 #define IIO_ID_PREFIX "device"
28 #define IIO_ID_FORMAT IIO_ID_PREFIX "%d"
30 /* IDR to assign each registered device a unique id*/
31 static DEFINE_IDR(iio_idr);
33 /* IDR for general event identifiers */
34 static DEFINE_IDR(iio_event_idr);
35 /* IDR to allocate character device minor numbers */
36 static DEFINE_IDR(iio_chrdev_idr);
37 /* Lock used to protect both of the above */
38 static DEFINE_SPINLOCK(iio_idr_lock);
41 EXPORT_SYMBOL(iio_devt);
43 #define IIO_DEV_MAX 256
44 static char *iio_devnode(struct device *dev, mode_t *mode)
46 return kasprintf(GFP_KERNEL, "iio/%s", dev_name(dev));
49 struct class iio_class = {
51 .devnode = iio_devnode,
53 EXPORT_SYMBOL(iio_class);
55 void __iio_change_event(struct iio_detected_event_list *ev,
60 ev->ev.timestamp = timestamp;
62 EXPORT_SYMBOL(__iio_change_event);
64 /* Used both in the interrupt line put events and the ring buffer ones */
66 /* Note that in it's current form someone has to be listening before events
67 * are queued. Hence a client MUST open the chrdev before the ring buffer is
70 int __iio_push_event(struct iio_event_interface *ev_int,
73 struct iio_shared_ev_pointer *
76 struct iio_detected_event_list *ev;
79 /* Does anyone care? */
80 mutex_lock(&ev_int->event_list_lock);
81 if (test_bit(IIO_BUSY_BIT_POS, &ev_int->handler.flags)) {
82 if (ev_int->current_events == ev_int->max_events) {
83 mutex_unlock(&ev_int->event_list_lock);
86 ev = kmalloc(sizeof(*ev), GFP_KERNEL);
89 mutex_unlock(&ev_int->event_list_lock);
93 ev->ev.timestamp = timestamp;
94 ev->shared_pointer = shared_pointer_p;
95 if (ev->shared_pointer)
96 shared_pointer_p->ev_p = ev;
98 list_add_tail(&ev->list, &ev_int->det_events.list);
99 ev_int->current_events++;
100 mutex_unlock(&ev_int->event_list_lock);
101 wake_up_interruptible(&ev_int->wait);
103 mutex_unlock(&ev_int->event_list_lock);
108 EXPORT_SYMBOL(__iio_push_event);
110 int iio_push_event(struct iio_dev *dev_info,
115 return __iio_push_event(&dev_info->event_interfaces[ev_line],
116 ev_code, timestamp, NULL);
118 EXPORT_SYMBOL(iio_push_event);
120 /* Generic interrupt line interrupt handler */
121 static irqreturn_t iio_interrupt_handler(int irq, void *_int_info)
123 struct iio_interrupt *int_info = _int_info;
124 struct iio_dev *dev_info = int_info->dev_info;
125 struct iio_event_handler_list *p;
129 spin_lock_irqsave(&int_info->ev_list_lock, flags);
130 if (list_empty(&int_info->ev_list)) {
131 spin_unlock_irqrestore(&int_info->ev_list_lock, flags);
135 time_ns = iio_get_time_ns();
136 /* detect single element list*/
137 if (list_is_singular(&int_info->ev_list)) {
138 disable_irq_nosync(irq);
139 p = list_first_entry(&int_info->ev_list,
140 struct iio_event_handler_list,
142 /* single event handler - maybe shared */
143 p->handler(dev_info, 1, time_ns, !(p->refcount > 1));
145 list_for_each_entry(p, &int_info->ev_list, list) {
146 disable_irq_nosync(irq);
147 p->handler(dev_info, 1, time_ns, 0);
149 spin_unlock_irqrestore(&int_info->ev_list_lock, flags);
154 static struct iio_interrupt *iio_allocate_interrupt(void)
156 struct iio_interrupt *i = kmalloc(sizeof *i, GFP_KERNEL);
158 spin_lock_init(&i->ev_list_lock);
159 INIT_LIST_HEAD(&i->ev_list);
164 /* Confirming the validity of supplied irq is left to drivers.*/
165 int iio_register_interrupt_line(unsigned int irq,
166 struct iio_dev *dev_info,
173 dev_info->interrupts[line_number] = iio_allocate_interrupt();
174 if (dev_info->interrupts[line_number] == NULL) {
178 dev_info->interrupts[line_number]->line_number = line_number;
179 dev_info->interrupts[line_number]->irq = irq;
180 dev_info->interrupts[line_number]->dev_info = dev_info;
182 /* Possibly only request on demand?
183 * Can see this may complicate the handling of interrupts.
184 * However, with this approach we might end up handling lots of
185 * events no-one cares about.*/
186 ret = request_irq(irq,
187 &iio_interrupt_handler,
190 dev_info->interrupts[line_number]);
195 EXPORT_SYMBOL(iio_register_interrupt_line);
197 /* This turns up an awful lot */
198 ssize_t iio_read_const_attr(struct device *dev,
199 struct device_attribute *attr,
202 return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string);
204 EXPORT_SYMBOL(iio_read_const_attr);
206 /* Before this runs the interrupt generator must have been disabled */
207 void iio_unregister_interrupt_line(struct iio_dev *dev_info, int line_number)
209 /* make sure the interrupt handlers are all done */
210 flush_scheduled_work();
211 free_irq(dev_info->interrupts[line_number]->irq,
212 dev_info->interrupts[line_number]);
213 kfree(dev_info->interrupts[line_number]);
215 EXPORT_SYMBOL(iio_unregister_interrupt_line);
217 /* Reference counted add and remove */
218 void iio_add_event_to_list(struct iio_event_handler_list *el,
219 struct list_head *head)
222 struct iio_interrupt *inter = to_iio_interrupt(head);
224 /* take mutex to protect this element */
225 mutex_lock(&el->exist_lock);
226 if (el->refcount == 0) {
227 /* Take the event list spin lock */
228 spin_lock_irqsave(&inter->ev_list_lock, flags);
229 list_add(&el->list, head);
230 spin_unlock_irqrestore(&inter->ev_list_lock, flags);
233 mutex_unlock(&el->exist_lock);
235 EXPORT_SYMBOL(iio_add_event_to_list);
237 void iio_remove_event_from_list(struct iio_event_handler_list *el,
238 struct list_head *head)
241 struct iio_interrupt *inter = to_iio_interrupt(head);
243 mutex_lock(&el->exist_lock);
245 if (el->refcount == 0) {
246 /* Take the event list spin lock */
247 spin_lock_irqsave(&inter->ev_list_lock, flags);
248 list_del_init(&el->list);
249 spin_unlock_irqrestore(&inter->ev_list_lock, flags);
251 mutex_unlock(&el->exist_lock);
253 EXPORT_SYMBOL(iio_remove_event_from_list);
255 static ssize_t iio_event_chrdev_read(struct file *filep,
260 struct iio_event_interface *ev_int = filep->private_data;
261 struct iio_detected_event_list *el;
265 mutex_lock(&ev_int->event_list_lock);
266 if (list_empty(&ev_int->det_events.list)) {
267 if (filep->f_flags & O_NONBLOCK) {
269 goto error_mutex_unlock;
271 mutex_unlock(&ev_int->event_list_lock);
272 /* Blocking on device; waiting for something to be there */
273 ret = wait_event_interruptible(ev_int->wait,
278 /* Single access device so noone else can get the data */
279 mutex_lock(&ev_int->event_list_lock);
282 el = list_first_entry(&ev_int->det_events.list,
283 struct iio_detected_event_list,
286 if (copy_to_user(buf, &(el->ev), len)) {
288 goto error_mutex_unlock;
291 ev_int->current_events--;
292 mutex_unlock(&ev_int->event_list_lock);
294 * Possible concurency issue if an update of this event is on its way
295 * through. May lead to new event being removed whilst the reported
296 * event was the unescalated event. In typical use case this is not a
297 * problem as userspace will say read half the buffer due to a 50%
298 * full event which would make the correct 100% full incorrect anyway.
300 if (el->shared_pointer) {
301 spin_lock(&el->shared_pointer->lock);
302 (el->shared_pointer->ev_p) = NULL;
303 spin_unlock(&el->shared_pointer->lock);
310 mutex_unlock(&ev_int->event_list_lock);
316 static int iio_event_chrdev_release(struct inode *inode, struct file *filep)
318 struct iio_handler *hand = iio_cdev_to_handler(inode->i_cdev);
319 struct iio_event_interface *ev_int = hand->private;
320 struct iio_detected_event_list *el, *t;
322 mutex_lock(&ev_int->event_list_lock);
323 clear_bit(IIO_BUSY_BIT_POS, &ev_int->handler.flags);
325 * In order to maintain a clean state for reopening,
326 * clear out any awaiting events. The mask will prevent
327 * any new __iio_push_event calls running.
329 list_for_each_entry_safe(el, t, &ev_int->det_events.list, list) {
333 mutex_unlock(&ev_int->event_list_lock);
338 static int iio_event_chrdev_open(struct inode *inode, struct file *filep)
340 struct iio_handler *hand = iio_cdev_to_handler(inode->i_cdev);
341 struct iio_event_interface *ev_int = hand->private;
343 mutex_lock(&ev_int->event_list_lock);
344 if (test_and_set_bit(IIO_BUSY_BIT_POS, &hand->flags)) {
345 fops_put(filep->f_op);
346 mutex_unlock(&ev_int->event_list_lock);
349 filep->private_data = hand->private;
350 mutex_unlock(&ev_int->event_list_lock);
355 static const struct file_operations iio_event_chrdev_fileops = {
356 .read = iio_event_chrdev_read,
357 .release = iio_event_chrdev_release,
358 .open = iio_event_chrdev_open,
359 .owner = THIS_MODULE,
362 static void iio_event_dev_release(struct device *dev)
364 struct iio_event_interface *ev_int
365 = container_of(dev, struct iio_event_interface, dev);
366 cdev_del(&ev_int->handler.chrdev);
367 iio_device_free_chrdev_minor(MINOR(dev->devt));
370 static struct device_type iio_event_type = {
371 .release = iio_event_dev_release,
374 int iio_device_get_chrdev_minor(void)
379 if (unlikely(idr_pre_get(&iio_chrdev_idr, GFP_KERNEL) == 0))
381 spin_lock(&iio_idr_lock);
382 ret = idr_get_new(&iio_chrdev_idr, NULL, &val);
383 spin_unlock(&iio_idr_lock);
384 if (unlikely(ret == -EAGAIN))
386 else if (unlikely(ret))
388 if (val > IIO_DEV_MAX)
393 void iio_device_free_chrdev_minor(int val)
395 spin_lock(&iio_idr_lock);
396 idr_remove(&iio_chrdev_idr, val);
397 spin_unlock(&iio_idr_lock);
400 int iio_setup_ev_int(struct iio_event_interface *ev_int,
402 struct module *owner,
407 ev_int->dev.class = &iio_class;
408 ev_int->dev.parent = dev;
409 ev_int->dev.type = &iio_event_type;
410 device_initialize(&ev_int->dev);
412 minor = iio_device_get_chrdev_minor();
415 goto error_device_put;
417 ev_int->dev.devt = MKDEV(MAJOR(iio_devt), minor);
418 dev_set_name(&ev_int->dev, "%s", name);
420 ret = device_add(&ev_int->dev);
422 goto error_free_minor;
424 cdev_init(&ev_int->handler.chrdev, &iio_event_chrdev_fileops);
425 ev_int->handler.chrdev.owner = owner;
427 mutex_init(&ev_int->event_list_lock);
428 /* discussion point - make this variable? */
429 ev_int->max_events = 10;
430 ev_int->current_events = 0;
431 INIT_LIST_HEAD(&ev_int->det_events.list);
432 init_waitqueue_head(&ev_int->wait);
433 ev_int->handler.private = ev_int;
434 ev_int->handler.flags = 0;
436 ret = cdev_add(&ev_int->handler.chrdev, ev_int->dev.devt, 1);
438 goto error_unreg_device;
443 device_unregister(&ev_int->dev);
445 iio_device_free_chrdev_minor(minor);
447 put_device(&ev_int->dev);
452 void iio_free_ev_int(struct iio_event_interface *ev_int)
454 device_unregister(&ev_int->dev);
455 put_device(&ev_int->dev);
458 static int __init iio_dev_init(void)
462 err = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio");
464 printk(KERN_ERR "%s: failed to allocate char dev region\n",
470 static void __exit iio_dev_exit(void)
473 unregister_chrdev_region(iio_devt, IIO_DEV_MAX);
476 static int __init iio_init(void)
480 /* Create sysfs class */
481 ret = class_register(&iio_class);
484 "%s could not create sysfs class\n",
489 ret = iio_dev_init();
491 goto error_unregister_class;
495 error_unregister_class:
496 class_unregister(&iio_class);
501 static void __exit iio_exit(void)
504 class_unregister(&iio_class);
507 static int iio_device_register_sysfs(struct iio_dev *dev_info)
511 ret = sysfs_create_group(&dev_info->dev.kobj, dev_info->attrs);
513 dev_err(dev_info->dev.parent,
514 "Failed to register sysfs hooks\n");
518 if (dev_info->scan_el_attrs) {
519 ret = sysfs_create_group(&dev_info->dev.kobj,
520 dev_info->scan_el_attrs);
522 dev_err(&dev_info->dev,
523 "Failed to add sysfs scan els\n");
530 static void iio_device_unregister_sysfs(struct iio_dev *dev_info)
532 if (dev_info->scan_el_attrs)
533 sysfs_remove_group(&dev_info->dev.kobj,
534 dev_info->scan_el_attrs);
536 sysfs_remove_group(&dev_info->dev.kobj, dev_info->attrs);
539 int iio_get_new_idr_val(struct idr *this_idr)
545 if (unlikely(idr_pre_get(this_idr, GFP_KERNEL) == 0))
548 spin_lock(&iio_idr_lock);
549 ret = idr_get_new(this_idr, NULL, &val);
550 spin_unlock(&iio_idr_lock);
551 if (unlikely(ret == -EAGAIN))
553 else if (unlikely(ret))
558 EXPORT_SYMBOL(iio_get_new_idr_val);
560 void iio_free_idr_val(struct idr *this_idr, int id)
562 spin_lock(&iio_idr_lock);
563 idr_remove(this_idr, id);
564 spin_unlock(&iio_idr_lock);
566 EXPORT_SYMBOL(iio_free_idr_val);
568 static int iio_device_register_id(struct iio_dev *dev_info,
569 struct idr *this_idr)
572 dev_info->id = iio_get_new_idr_val(&iio_idr);
573 if (dev_info->id < 0)
578 static void iio_device_unregister_id(struct iio_dev *dev_info)
580 iio_free_idr_val(&iio_idr, dev_info->id);
583 static inline int __iio_add_event_config_attrs(struct iio_dev *dev_info, int i)
586 /*p for adding, q for removing */
587 struct attribute **attrp, **attrq;
589 if (dev_info->event_conf_attrs && dev_info->event_conf_attrs[i].attrs) {
590 attrp = dev_info->event_conf_attrs[i].attrs;
592 ret = sysfs_add_file_to_group(&dev_info->dev.kobj,
595 ->event_attrs[i].name);
604 attrq = dev_info->event_conf_attrs[i].attrs;
605 while (attrq != attrp) {
606 sysfs_remove_file_from_group(&dev_info->dev.kobj,
608 dev_info->event_attrs[i].name);
615 static inline int __iio_remove_event_config_attrs(struct iio_dev *dev_info,
618 struct attribute **attrq;
620 if (dev_info->event_conf_attrs
621 && dev_info->event_conf_attrs[i].attrs) {
622 attrq = dev_info->event_conf_attrs[i].attrs;
624 sysfs_remove_file_from_group(&dev_info->dev.kobj,
627 ->event_attrs[i].name);
635 static int iio_device_register_eventset(struct iio_dev *dev_info)
639 if (dev_info->num_interrupt_lines == 0)
642 dev_info->event_interfaces =
643 kzalloc(sizeof(struct iio_event_interface)
644 *dev_info->num_interrupt_lines,
646 if (dev_info->event_interfaces == NULL) {
651 dev_info->interrupts = kzalloc(sizeof(struct iio_interrupt *)
652 *dev_info->num_interrupt_lines,
654 if (dev_info->interrupts == NULL) {
656 goto error_free_event_interfaces;
659 for (i = 0; i < dev_info->num_interrupt_lines; i++) {
660 dev_info->event_interfaces[i].owner = dev_info->driver_module;
661 ret = iio_get_new_idr_val(&iio_event_idr);
663 goto error_free_setup_ev_ints;
665 dev_info->event_interfaces[i].id = ret;
667 snprintf(dev_info->event_interfaces[i]._name, 20,
669 dev_info->event_interfaces[i].id);
671 ret = iio_setup_ev_int(&dev_info->event_interfaces[i],
672 (const char *)(dev_info
673 ->event_interfaces[i]
675 dev_info->driver_module,
678 dev_err(&dev_info->dev,
679 "Could not get chrdev interface\n");
680 iio_free_idr_val(&iio_event_idr,
681 dev_info->event_interfaces[i].id);
682 goto error_free_setup_ev_ints;
686 for (i = 0; i < dev_info->num_interrupt_lines; i++) {
687 snprintf(dev_info->event_interfaces[i]._attrname, 20,
688 "event_line%d_sources", i);
689 dev_info->event_attrs[i].name
691 (dev_info->event_interfaces[i]._attrname);
692 ret = sysfs_create_group(&dev_info->dev.kobj,
693 &dev_info->event_attrs[i]);
695 dev_err(&dev_info->dev,
696 "Failed to register sysfs for event attrs");
697 goto error_remove_sysfs_interfaces;
701 for (i = 0; i < dev_info->num_interrupt_lines; i++) {
702 ret = __iio_add_event_config_attrs(dev_info, i);
704 goto error_unregister_config_attrs;
709 error_unregister_config_attrs:
710 for (j = 0; j < i; j++)
711 __iio_remove_event_config_attrs(dev_info, i);
712 i = dev_info->num_interrupt_lines - 1;
713 error_remove_sysfs_interfaces:
714 for (j = 0; j < i; j++)
715 sysfs_remove_group(&dev_info->dev.kobj,
716 &dev_info->event_attrs[j]);
717 i = dev_info->num_interrupt_lines - 1;
718 error_free_setup_ev_ints:
719 for (j = 0; j < i; j++) {
720 iio_free_idr_val(&iio_event_idr,
721 dev_info->event_interfaces[i].id);
722 iio_free_ev_int(&dev_info->event_interfaces[j]);
724 kfree(dev_info->interrupts);
725 error_free_event_interfaces:
726 kfree(dev_info->event_interfaces);
732 static void iio_device_unregister_eventset(struct iio_dev *dev_info)
736 if (dev_info->num_interrupt_lines == 0)
738 for (i = 0; i < dev_info->num_interrupt_lines; i++)
739 sysfs_remove_group(&dev_info->dev.kobj,
740 &dev_info->event_attrs[i]);
742 for (i = 0; i < dev_info->num_interrupt_lines; i++) {
743 iio_free_idr_val(&iio_event_idr,
744 dev_info->event_interfaces[i].id);
745 iio_free_ev_int(&dev_info->event_interfaces[i]);
747 kfree(dev_info->interrupts);
748 kfree(dev_info->event_interfaces);
751 static void iio_dev_release(struct device *device)
753 struct iio_dev *dev = to_iio_dev(device);
759 static struct device_type iio_dev_type = {
760 .name = "iio_device",
761 .release = iio_dev_release,
764 struct iio_dev *iio_allocate_device(void)
766 struct iio_dev *dev = kzalloc(sizeof *dev, GFP_KERNEL);
769 dev->dev.type = &iio_dev_type;
770 dev->dev.class = &iio_class;
771 device_initialize(&dev->dev);
772 dev_set_drvdata(&dev->dev, (void *)dev);
773 mutex_init(&dev->mlock);
779 EXPORT_SYMBOL(iio_allocate_device);
781 void iio_free_device(struct iio_dev *dev)
786 EXPORT_SYMBOL(iio_free_device);
788 int iio_device_register(struct iio_dev *dev_info)
792 ret = iio_device_register_id(dev_info, &iio_idr);
794 dev_err(&dev_info->dev, "Failed to get id\n");
797 dev_set_name(&dev_info->dev, "device%d", dev_info->id);
799 ret = device_add(&dev_info->dev);
802 ret = iio_device_register_sysfs(dev_info);
804 dev_err(dev_info->dev.parent,
805 "Failed to register sysfs interfaces\n");
806 goto error_del_device;
808 ret = iio_device_register_eventset(dev_info);
810 dev_err(dev_info->dev.parent,
811 "Failed to register event set \n");
812 goto error_free_sysfs;
814 if (dev_info->modes & INDIO_RING_TRIGGERED)
815 iio_device_register_trigger_consumer(dev_info);
820 iio_device_unregister_sysfs(dev_info);
822 device_del(&dev_info->dev);
824 iio_device_unregister_id(dev_info);
828 EXPORT_SYMBOL(iio_device_register);
830 void iio_device_unregister(struct iio_dev *dev_info)
832 if (dev_info->modes & INDIO_RING_TRIGGERED)
833 iio_device_unregister_trigger_consumer(dev_info);
834 iio_device_unregister_eventset(dev_info);
835 iio_device_unregister_sysfs(dev_info);
836 iio_device_unregister_id(dev_info);
837 device_unregister(&dev_info->dev);
839 EXPORT_SYMBOL(iio_device_unregister);
843 module_put(THIS_MODULE);
848 __module_get(THIS_MODULE);
851 subsys_initcall(iio_init);
852 module_exit(iio_exit);
854 MODULE_AUTHOR("Jonathan Cameron <jic23@cam.ac.uk>");
855 MODULE_DESCRIPTION("Industrial I/O core");
856 MODULE_LICENSE("GPL");