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>
24 #include <linux/slab.h>
26 #include "trigger_consumer.h"
28 #define IIO_ID_PREFIX "device"
29 #define IIO_ID_FORMAT IIO_ID_PREFIX "%d"
31 /* IDR to assign each registered device a unique id*/
32 static DEFINE_IDR(iio_idr);
33 /* IDR to allocate character device minor numbers */
34 static DEFINE_IDR(iio_chrdev_idr);
35 /* Lock used to protect both of the above */
36 static DEFINE_SPINLOCK(iio_idr_lock);
39 EXPORT_SYMBOL(iio_devt);
41 #define IIO_DEV_MAX 256
42 struct bus_type iio_bus_type = {
45 EXPORT_SYMBOL(iio_bus_type);
47 void __iio_change_event(struct iio_detected_event_list *ev,
52 ev->ev.timestamp = timestamp;
54 EXPORT_SYMBOL(__iio_change_event);
56 /* Used both in the interrupt line put events and the ring buffer ones */
58 /* Note that in it's current form someone has to be listening before events
59 * are queued. Hence a client MUST open the chrdev before the ring buffer is
62 int __iio_push_event(struct iio_event_interface *ev_int,
65 struct iio_shared_ev_pointer *
68 struct iio_detected_event_list *ev;
71 /* Does anyone care? */
72 mutex_lock(&ev_int->event_list_lock);
73 if (test_bit(IIO_BUSY_BIT_POS, &ev_int->handler.flags)) {
74 if (ev_int->current_events == ev_int->max_events) {
75 mutex_unlock(&ev_int->event_list_lock);
78 ev = kmalloc(sizeof(*ev), GFP_KERNEL);
81 mutex_unlock(&ev_int->event_list_lock);
85 ev->ev.timestamp = timestamp;
86 ev->shared_pointer = shared_pointer_p;
87 if (ev->shared_pointer)
88 shared_pointer_p->ev_p = ev;
90 list_add_tail(&ev->list, &ev_int->det_events.list);
91 ev_int->current_events++;
92 mutex_unlock(&ev_int->event_list_lock);
93 wake_up_interruptible(&ev_int->wait);
95 mutex_unlock(&ev_int->event_list_lock);
100 EXPORT_SYMBOL(__iio_push_event);
102 int iio_push_event(struct iio_dev *dev_info,
107 return __iio_push_event(&dev_info->event_interfaces[ev_line],
108 ev_code, timestamp, NULL);
110 EXPORT_SYMBOL(iio_push_event);
112 /* Generic interrupt line interrupt handler */
113 static irqreturn_t iio_interrupt_handler(int irq, void *_int_info)
115 struct iio_interrupt *int_info = _int_info;
116 struct iio_dev *dev_info = int_info->dev_info;
117 struct iio_event_handler_list *p;
121 spin_lock_irqsave(&int_info->ev_list_lock, flags);
122 if (list_empty(&int_info->ev_list)) {
123 spin_unlock_irqrestore(&int_info->ev_list_lock, flags);
127 time_ns = iio_get_time_ns();
128 /* detect single element list*/
129 if (list_is_singular(&int_info->ev_list)) {
130 disable_irq_nosync(irq);
131 p = list_first_entry(&int_info->ev_list,
132 struct iio_event_handler_list,
134 /* single event handler - maybe shared */
135 p->handler(dev_info, 1, time_ns, !(p->refcount > 1));
137 list_for_each_entry(p, &int_info->ev_list, list) {
138 disable_irq_nosync(irq);
139 p->handler(dev_info, 1, time_ns, 0);
141 spin_unlock_irqrestore(&int_info->ev_list_lock, flags);
146 static struct iio_interrupt *iio_allocate_interrupt(void)
148 struct iio_interrupt *i = kmalloc(sizeof *i, GFP_KERNEL);
150 spin_lock_init(&i->ev_list_lock);
151 INIT_LIST_HEAD(&i->ev_list);
156 /* Confirming the validity of supplied irq is left to drivers.*/
157 int iio_register_interrupt_line(unsigned int irq,
158 struct iio_dev *dev_info,
165 dev_info->interrupts[line_number] = iio_allocate_interrupt();
166 if (dev_info->interrupts[line_number] == NULL) {
170 dev_info->interrupts[line_number]->line_number = line_number;
171 dev_info->interrupts[line_number]->irq = irq;
172 dev_info->interrupts[line_number]->dev_info = dev_info;
174 /* Possibly only request on demand?
175 * Can see this may complicate the handling of interrupts.
176 * However, with this approach we might end up handling lots of
177 * events no-one cares about.*/
178 ret = request_irq(irq,
179 &iio_interrupt_handler,
182 dev_info->interrupts[line_number]);
187 EXPORT_SYMBOL(iio_register_interrupt_line);
189 /* This turns up an awful lot */
190 ssize_t iio_read_const_attr(struct device *dev,
191 struct device_attribute *attr,
194 return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string);
196 EXPORT_SYMBOL(iio_read_const_attr);
198 /* Before this runs the interrupt generator must have been disabled */
199 void iio_unregister_interrupt_line(struct iio_dev *dev_info, int line_number)
201 /* make sure the interrupt handlers are all done */
202 flush_scheduled_work();
203 free_irq(dev_info->interrupts[line_number]->irq,
204 dev_info->interrupts[line_number]);
205 kfree(dev_info->interrupts[line_number]);
207 EXPORT_SYMBOL(iio_unregister_interrupt_line);
209 /* Reference counted add and remove */
210 void iio_add_event_to_list(struct iio_event_handler_list *el,
211 struct list_head *head)
214 struct iio_interrupt *inter = to_iio_interrupt(head);
216 /* take mutex to protect this element */
217 mutex_lock(&el->exist_lock);
218 if (el->refcount == 0) {
219 /* Take the event list spin lock */
220 spin_lock_irqsave(&inter->ev_list_lock, flags);
221 list_add(&el->list, head);
222 spin_unlock_irqrestore(&inter->ev_list_lock, flags);
225 mutex_unlock(&el->exist_lock);
227 EXPORT_SYMBOL(iio_add_event_to_list);
229 void iio_remove_event_from_list(struct iio_event_handler_list *el,
230 struct list_head *head)
233 struct iio_interrupt *inter = to_iio_interrupt(head);
235 mutex_lock(&el->exist_lock);
237 if (el->refcount == 0) {
238 /* Take the event list spin lock */
239 spin_lock_irqsave(&inter->ev_list_lock, flags);
240 list_del_init(&el->list);
241 spin_unlock_irqrestore(&inter->ev_list_lock, flags);
243 mutex_unlock(&el->exist_lock);
245 EXPORT_SYMBOL(iio_remove_event_from_list);
247 static ssize_t iio_event_chrdev_read(struct file *filep,
252 struct iio_event_interface *ev_int = filep->private_data;
253 struct iio_detected_event_list *el;
257 mutex_lock(&ev_int->event_list_lock);
258 if (list_empty(&ev_int->det_events.list)) {
259 if (filep->f_flags & O_NONBLOCK) {
261 goto error_mutex_unlock;
263 mutex_unlock(&ev_int->event_list_lock);
264 /* Blocking on device; waiting for something to be there */
265 ret = wait_event_interruptible(ev_int->wait,
270 /* Single access device so noone else can get the data */
271 mutex_lock(&ev_int->event_list_lock);
274 el = list_first_entry(&ev_int->det_events.list,
275 struct iio_detected_event_list,
278 if (copy_to_user(buf, &(el->ev), len)) {
280 goto error_mutex_unlock;
283 ev_int->current_events--;
284 mutex_unlock(&ev_int->event_list_lock);
286 * Possible concurency issue if an update of this event is on its way
287 * through. May lead to new event being removed whilst the reported
288 * event was the unescalated event. In typical use case this is not a
289 * problem as userspace will say read half the buffer due to a 50%
290 * full event which would make the correct 100% full incorrect anyway.
292 if (el->shared_pointer) {
293 spin_lock(&el->shared_pointer->lock);
294 (el->shared_pointer->ev_p) = NULL;
295 spin_unlock(&el->shared_pointer->lock);
302 mutex_unlock(&ev_int->event_list_lock);
308 static int iio_event_chrdev_release(struct inode *inode, struct file *filep)
310 struct iio_handler *hand = iio_cdev_to_handler(inode->i_cdev);
311 struct iio_event_interface *ev_int = hand->private;
312 struct iio_detected_event_list *el, *t;
314 mutex_lock(&ev_int->event_list_lock);
315 clear_bit(IIO_BUSY_BIT_POS, &ev_int->handler.flags);
317 * In order to maintain a clean state for reopening,
318 * clear out any awaiting events. The mask will prevent
319 * any new __iio_push_event calls running.
321 list_for_each_entry_safe(el, t, &ev_int->det_events.list, list) {
325 mutex_unlock(&ev_int->event_list_lock);
330 static int iio_event_chrdev_open(struct inode *inode, struct file *filep)
332 struct iio_handler *hand = iio_cdev_to_handler(inode->i_cdev);
333 struct iio_event_interface *ev_int = hand->private;
335 mutex_lock(&ev_int->event_list_lock);
336 if (test_and_set_bit(IIO_BUSY_BIT_POS, &hand->flags)) {
337 fops_put(filep->f_op);
338 mutex_unlock(&ev_int->event_list_lock);
341 filep->private_data = hand->private;
342 mutex_unlock(&ev_int->event_list_lock);
347 static const struct file_operations iio_event_chrdev_fileops = {
348 .read = iio_event_chrdev_read,
349 .release = iio_event_chrdev_release,
350 .open = iio_event_chrdev_open,
351 .owner = THIS_MODULE,
354 static void iio_event_dev_release(struct device *dev)
356 struct iio_event_interface *ev_int
357 = container_of(dev, struct iio_event_interface, dev);
358 cdev_del(&ev_int->handler.chrdev);
359 iio_device_free_chrdev_minor(MINOR(dev->devt));
362 static struct device_type iio_event_type = {
363 .release = iio_event_dev_release,
366 int iio_device_get_chrdev_minor(void)
371 if (unlikely(idr_pre_get(&iio_chrdev_idr, GFP_KERNEL) == 0))
373 spin_lock(&iio_idr_lock);
374 ret = idr_get_new(&iio_chrdev_idr, NULL, &val);
375 spin_unlock(&iio_idr_lock);
376 if (unlikely(ret == -EAGAIN))
378 else if (unlikely(ret))
380 if (val > IIO_DEV_MAX)
385 void iio_device_free_chrdev_minor(int val)
387 spin_lock(&iio_idr_lock);
388 idr_remove(&iio_chrdev_idr, val);
389 spin_unlock(&iio_idr_lock);
392 int iio_setup_ev_int(struct iio_event_interface *ev_int,
394 struct module *owner,
399 ev_int->dev.bus = &iio_bus_type;
400 ev_int->dev.parent = dev;
401 ev_int->dev.type = &iio_event_type;
402 device_initialize(&ev_int->dev);
404 minor = iio_device_get_chrdev_minor();
407 goto error_device_put;
409 ev_int->dev.devt = MKDEV(MAJOR(iio_devt), minor);
410 dev_set_name(&ev_int->dev, "%s", name);
412 ret = device_add(&ev_int->dev);
414 goto error_free_minor;
416 cdev_init(&ev_int->handler.chrdev, &iio_event_chrdev_fileops);
417 ev_int->handler.chrdev.owner = owner;
419 mutex_init(&ev_int->event_list_lock);
420 /* discussion point - make this variable? */
421 ev_int->max_events = 10;
422 ev_int->current_events = 0;
423 INIT_LIST_HEAD(&ev_int->det_events.list);
424 init_waitqueue_head(&ev_int->wait);
425 ev_int->handler.private = ev_int;
426 ev_int->handler.flags = 0;
428 ret = cdev_add(&ev_int->handler.chrdev, ev_int->dev.devt, 1);
430 goto error_unreg_device;
435 device_unregister(&ev_int->dev);
437 iio_device_free_chrdev_minor(minor);
439 put_device(&ev_int->dev);
444 void iio_free_ev_int(struct iio_event_interface *ev_int)
446 device_unregister(&ev_int->dev);
447 put_device(&ev_int->dev);
450 static int __init iio_dev_init(void)
454 err = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio");
456 printk(KERN_ERR "%s: failed to allocate char dev region\n",
462 static void __exit iio_dev_exit(void)
465 unregister_chrdev_region(iio_devt, IIO_DEV_MAX);
468 static int __init iio_init(void)
472 /* Register sysfs bus */
473 ret = bus_register(&iio_bus_type);
476 "%s could not register bus type\n",
481 ret = iio_dev_init();
483 goto error_unregister_bus_type;
487 error_unregister_bus_type:
488 bus_unregister(&iio_bus_type);
493 static void __exit iio_exit(void)
496 bus_unregister(&iio_bus_type);
499 static int iio_device_register_sysfs(struct iio_dev *dev_info)
503 ret = sysfs_create_group(&dev_info->dev.kobj, dev_info->attrs);
505 dev_err(dev_info->dev.parent,
506 "Failed to register sysfs hooks\n");
510 if (dev_info->scan_el_attrs) {
511 ret = sysfs_create_group(&dev_info->dev.kobj,
512 dev_info->scan_el_attrs);
514 dev_err(&dev_info->dev,
515 "Failed to add sysfs scan els\n");
522 static void iio_device_unregister_sysfs(struct iio_dev *dev_info)
524 if (dev_info->scan_el_attrs)
525 sysfs_remove_group(&dev_info->dev.kobj,
526 dev_info->scan_el_attrs);
528 sysfs_remove_group(&dev_info->dev.kobj, dev_info->attrs);
531 /* Return a negative errno on failure */
532 int iio_get_new_idr_val(struct idr *this_idr)
538 if (unlikely(idr_pre_get(this_idr, GFP_KERNEL) == 0))
541 spin_lock(&iio_idr_lock);
542 ret = idr_get_new(this_idr, NULL, &val);
543 spin_unlock(&iio_idr_lock);
544 if (unlikely(ret == -EAGAIN))
546 else if (unlikely(ret))
551 EXPORT_SYMBOL(iio_get_new_idr_val);
553 void iio_free_idr_val(struct idr *this_idr, int id)
555 spin_lock(&iio_idr_lock);
556 idr_remove(this_idr, id);
557 spin_unlock(&iio_idr_lock);
559 EXPORT_SYMBOL(iio_free_idr_val);
561 static int iio_device_register_id(struct iio_dev *dev_info,
562 struct idr *this_idr)
565 dev_info->id = iio_get_new_idr_val(&iio_idr);
566 if (dev_info->id < 0)
571 static void iio_device_unregister_id(struct iio_dev *dev_info)
573 iio_free_idr_val(&iio_idr, dev_info->id);
576 static inline int __iio_add_event_config_attrs(struct iio_dev *dev_info, int i)
579 /*p for adding, q for removing */
580 struct attribute **attrp, **attrq;
582 if (dev_info->event_conf_attrs && dev_info->event_conf_attrs[i].attrs) {
583 attrp = dev_info->event_conf_attrs[i].attrs;
585 ret = sysfs_add_file_to_group(&dev_info->dev.kobj,
588 ->event_attrs[i].name);
597 attrq = dev_info->event_conf_attrs[i].attrs;
598 while (attrq != attrp) {
599 sysfs_remove_file_from_group(&dev_info->dev.kobj,
601 dev_info->event_attrs[i].name);
608 static inline int __iio_remove_event_config_attrs(struct iio_dev *dev_info,
611 struct attribute **attrq;
613 if (dev_info->event_conf_attrs
614 && dev_info->event_conf_attrs[i].attrs) {
615 attrq = dev_info->event_conf_attrs[i].attrs;
617 sysfs_remove_file_from_group(&dev_info->dev.kobj,
620 ->event_attrs[i].name);
628 static int iio_device_register_eventset(struct iio_dev *dev_info)
632 if (dev_info->num_interrupt_lines == 0)
635 dev_info->event_interfaces =
636 kzalloc(sizeof(struct iio_event_interface)
637 *dev_info->num_interrupt_lines,
639 if (dev_info->event_interfaces == NULL) {
644 dev_info->interrupts = kzalloc(sizeof(struct iio_interrupt *)
645 *dev_info->num_interrupt_lines,
647 if (dev_info->interrupts == NULL) {
649 goto error_free_event_interfaces;
652 for (i = 0; i < dev_info->num_interrupt_lines; i++) {
653 dev_info->event_interfaces[i].owner = dev_info->driver_module;
655 snprintf(dev_info->event_interfaces[i]._name, 20,
657 dev_name(&dev_info->dev),
660 ret = iio_setup_ev_int(&dev_info->event_interfaces[i],
661 (const char *)(dev_info
662 ->event_interfaces[i]
664 dev_info->driver_module,
667 dev_err(&dev_info->dev,
668 "Could not get chrdev interface\n");
669 goto error_free_setup_ev_ints;
672 dev_set_drvdata(&dev_info->event_interfaces[i].dev,
674 ret = sysfs_create_group(&dev_info
675 ->event_interfaces[i]
677 &dev_info->event_attrs[i]);
680 dev_err(&dev_info->dev,
681 "Failed to register sysfs for event attrs");
682 goto error_remove_sysfs_interfaces;
686 for (i = 0; i < dev_info->num_interrupt_lines; i++) {
687 ret = __iio_add_event_config_attrs(dev_info, i);
689 goto error_unregister_config_attrs;
694 error_unregister_config_attrs:
695 for (j = 0; j < i; j++)
696 __iio_remove_event_config_attrs(dev_info, i);
697 i = dev_info->num_interrupt_lines - 1;
698 error_remove_sysfs_interfaces:
699 for (j = 0; j < i; j++)
700 sysfs_remove_group(&dev_info
701 ->event_interfaces[j].dev.kobj,
702 &dev_info->event_attrs[j]);
703 error_free_setup_ev_ints:
704 for (j = 0; j < i; j++)
705 iio_free_ev_int(&dev_info->event_interfaces[j]);
706 kfree(dev_info->interrupts);
707 error_free_event_interfaces:
708 kfree(dev_info->event_interfaces);
714 static void iio_device_unregister_eventset(struct iio_dev *dev_info)
718 if (dev_info->num_interrupt_lines == 0)
720 for (i = 0; i < dev_info->num_interrupt_lines; i++)
721 sysfs_remove_group(&dev_info
722 ->event_interfaces[i].dev.kobj,
723 &dev_info->event_attrs[i]);
725 for (i = 0; i < dev_info->num_interrupt_lines; i++)
726 iio_free_ev_int(&dev_info->event_interfaces[i]);
727 kfree(dev_info->interrupts);
728 kfree(dev_info->event_interfaces);
731 static void iio_dev_release(struct device *device)
733 struct iio_dev *dev = to_iio_dev(device);
739 static struct device_type iio_dev_type = {
740 .name = "iio_device",
741 .release = iio_dev_release,
744 struct iio_dev *iio_allocate_device(void)
746 struct iio_dev *dev = kzalloc(sizeof *dev, GFP_KERNEL);
749 dev->dev.type = &iio_dev_type;
750 dev->dev.bus = &iio_bus_type;
751 device_initialize(&dev->dev);
752 dev_set_drvdata(&dev->dev, (void *)dev);
753 mutex_init(&dev->mlock);
759 EXPORT_SYMBOL(iio_allocate_device);
761 void iio_free_device(struct iio_dev *dev)
766 EXPORT_SYMBOL(iio_free_device);
768 int iio_device_register(struct iio_dev *dev_info)
772 ret = iio_device_register_id(dev_info, &iio_idr);
774 dev_err(&dev_info->dev, "Failed to get id\n");
777 dev_set_name(&dev_info->dev, "device%d", dev_info->id);
779 ret = device_add(&dev_info->dev);
782 ret = iio_device_register_sysfs(dev_info);
784 dev_err(dev_info->dev.parent,
785 "Failed to register sysfs interfaces\n");
786 goto error_del_device;
788 ret = iio_device_register_eventset(dev_info);
790 dev_err(dev_info->dev.parent,
791 "Failed to register event set\n");
792 goto error_free_sysfs;
794 if (dev_info->modes & INDIO_RING_TRIGGERED)
795 iio_device_register_trigger_consumer(dev_info);
800 iio_device_unregister_sysfs(dev_info);
802 device_del(&dev_info->dev);
804 iio_device_unregister_id(dev_info);
808 EXPORT_SYMBOL(iio_device_register);
810 void iio_device_unregister(struct iio_dev *dev_info)
812 if (dev_info->modes & INDIO_RING_TRIGGERED)
813 iio_device_unregister_trigger_consumer(dev_info);
814 iio_device_unregister_eventset(dev_info);
815 iio_device_unregister_sysfs(dev_info);
816 iio_device_unregister_id(dev_info);
817 device_unregister(&dev_info->dev);
819 EXPORT_SYMBOL(iio_device_unregister);
823 module_put(THIS_MODULE);
828 __module_get(THIS_MODULE);
831 subsys_initcall(iio_init);
832 module_exit(iio_exit);
834 MODULE_AUTHOR("Jonathan Cameron <jic23@cam.ac.uk>");
835 MODULE_DESCRIPTION("Industrial I/O core");
836 MODULE_LICENSE("GPL");