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_IDA(iio_ida);
33 /* IDR to allocate character device minor numbers */
34 static DEFINE_IDA(iio_chrdev_ida);
35 /* Lock used to protect both of the above */
36 static DEFINE_SPINLOCK(iio_ida_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 list_for_each_entry(p, &int_info->ev_list, list) {
129 disable_irq_nosync(irq);
130 p->handler(dev_info, 1, time_ns, !(p->refcount > 1));
132 spin_unlock_irqrestore(&int_info->ev_list_lock, flags);
137 static struct iio_interrupt *iio_allocate_interrupt(void)
139 struct iio_interrupt *i = kmalloc(sizeof *i, GFP_KERNEL);
141 spin_lock_init(&i->ev_list_lock);
142 INIT_LIST_HEAD(&i->ev_list);
147 /* Confirming the validity of supplied irq is left to drivers.*/
148 int iio_register_interrupt_line(unsigned int irq,
149 struct iio_dev *dev_info,
156 dev_info->interrupts[line_number] = iio_allocate_interrupt();
157 if (dev_info->interrupts[line_number] == NULL) {
161 dev_info->interrupts[line_number]->line_number = line_number;
162 dev_info->interrupts[line_number]->irq = irq;
163 dev_info->interrupts[line_number]->dev_info = dev_info;
165 /* Possibly only request on demand?
166 * Can see this may complicate the handling of interrupts.
167 * However, with this approach we might end up handling lots of
168 * events no-one cares about.*/
169 ret = request_irq(irq,
170 &iio_interrupt_handler,
173 dev_info->interrupts[line_number]);
178 EXPORT_SYMBOL(iio_register_interrupt_line);
180 /* This turns up an awful lot */
181 ssize_t iio_read_const_attr(struct device *dev,
182 struct device_attribute *attr,
185 return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string);
187 EXPORT_SYMBOL(iio_read_const_attr);
189 /* Before this runs the interrupt generator must have been disabled */
190 void iio_unregister_interrupt_line(struct iio_dev *dev_info, int line_number)
192 /* make sure the interrupt handlers are all done */
193 flush_scheduled_work();
194 free_irq(dev_info->interrupts[line_number]->irq,
195 dev_info->interrupts[line_number]);
196 kfree(dev_info->interrupts[line_number]);
198 EXPORT_SYMBOL(iio_unregister_interrupt_line);
200 /* Reference counted add and remove */
201 void iio_add_event_to_list(struct iio_event_handler_list *el,
202 struct list_head *head)
205 struct iio_interrupt *inter = to_iio_interrupt(head);
207 /* take mutex to protect this element */
208 mutex_lock(&el->exist_lock);
209 if (el->refcount == 0) {
210 /* Take the event list spin lock */
211 spin_lock_irqsave(&inter->ev_list_lock, flags);
212 list_add(&el->list, head);
213 spin_unlock_irqrestore(&inter->ev_list_lock, flags);
216 mutex_unlock(&el->exist_lock);
218 EXPORT_SYMBOL(iio_add_event_to_list);
220 void iio_remove_event_from_list(struct iio_event_handler_list *el,
221 struct list_head *head)
224 struct iio_interrupt *inter = to_iio_interrupt(head);
226 mutex_lock(&el->exist_lock);
228 if (el->refcount == 0) {
229 /* Take the event list spin lock */
230 spin_lock_irqsave(&inter->ev_list_lock, flags);
231 list_del_init(&el->list);
232 spin_unlock_irqrestore(&inter->ev_list_lock, flags);
234 mutex_unlock(&el->exist_lock);
236 EXPORT_SYMBOL(iio_remove_event_from_list);
238 static ssize_t iio_event_chrdev_read(struct file *filep,
243 struct iio_event_interface *ev_int = filep->private_data;
244 struct iio_detected_event_list *el;
248 mutex_lock(&ev_int->event_list_lock);
249 if (list_empty(&ev_int->det_events.list)) {
250 if (filep->f_flags & O_NONBLOCK) {
252 goto error_mutex_unlock;
254 mutex_unlock(&ev_int->event_list_lock);
255 /* Blocking on device; waiting for something to be there */
256 ret = wait_event_interruptible(ev_int->wait,
261 /* Single access device so noone else can get the data */
262 mutex_lock(&ev_int->event_list_lock);
265 el = list_first_entry(&ev_int->det_events.list,
266 struct iio_detected_event_list,
269 if (copy_to_user(buf, &(el->ev), len)) {
271 goto error_mutex_unlock;
274 ev_int->current_events--;
275 mutex_unlock(&ev_int->event_list_lock);
277 * Possible concurency issue if an update of this event is on its way
278 * through. May lead to new event being removed whilst the reported
279 * event was the unescalated event. In typical use case this is not a
280 * problem as userspace will say read half the buffer due to a 50%
281 * full event which would make the correct 100% full incorrect anyway.
283 if (el->shared_pointer) {
284 spin_lock(&el->shared_pointer->lock);
285 (el->shared_pointer->ev_p) = NULL;
286 spin_unlock(&el->shared_pointer->lock);
293 mutex_unlock(&ev_int->event_list_lock);
299 static int iio_event_chrdev_release(struct inode *inode, struct file *filep)
301 struct iio_handler *hand = iio_cdev_to_handler(inode->i_cdev);
302 struct iio_event_interface *ev_int = hand->private;
303 struct iio_detected_event_list *el, *t;
305 mutex_lock(&ev_int->event_list_lock);
306 clear_bit(IIO_BUSY_BIT_POS, &ev_int->handler.flags);
308 * In order to maintain a clean state for reopening,
309 * clear out any awaiting events. The mask will prevent
310 * any new __iio_push_event calls running.
312 list_for_each_entry_safe(el, t, &ev_int->det_events.list, list) {
316 mutex_unlock(&ev_int->event_list_lock);
321 static int iio_event_chrdev_open(struct inode *inode, struct file *filep)
323 struct iio_handler *hand = iio_cdev_to_handler(inode->i_cdev);
324 struct iio_event_interface *ev_int = hand->private;
326 mutex_lock(&ev_int->event_list_lock);
327 if (test_and_set_bit(IIO_BUSY_BIT_POS, &hand->flags)) {
328 fops_put(filep->f_op);
329 mutex_unlock(&ev_int->event_list_lock);
332 filep->private_data = hand->private;
333 mutex_unlock(&ev_int->event_list_lock);
338 static const struct file_operations iio_event_chrdev_fileops = {
339 .read = iio_event_chrdev_read,
340 .release = iio_event_chrdev_release,
341 .open = iio_event_chrdev_open,
342 .owner = THIS_MODULE,
345 static void iio_event_dev_release(struct device *dev)
347 struct iio_event_interface *ev_int
348 = container_of(dev, struct iio_event_interface, dev);
349 cdev_del(&ev_int->handler.chrdev);
350 iio_device_free_chrdev_minor(MINOR(dev->devt));
353 static struct device_type iio_event_type = {
354 .release = iio_event_dev_release,
357 int iio_device_get_chrdev_minor(void)
362 if (unlikely(ida_pre_get(&iio_chrdev_ida, GFP_KERNEL) == 0))
364 spin_lock(&iio_ida_lock);
365 ret = ida_get_new(&iio_chrdev_ida, &val);
366 spin_unlock(&iio_ida_lock);
367 if (unlikely(ret == -EAGAIN))
369 else if (unlikely(ret))
371 if (val > IIO_DEV_MAX)
376 void iio_device_free_chrdev_minor(int val)
378 spin_lock(&iio_ida_lock);
379 ida_remove(&iio_chrdev_ida, val);
380 spin_unlock(&iio_ida_lock);
383 int iio_setup_ev_int(struct iio_event_interface *ev_int,
385 struct module *owner,
390 ev_int->dev.bus = &iio_bus_type;
391 ev_int->dev.parent = dev;
392 ev_int->dev.type = &iio_event_type;
393 device_initialize(&ev_int->dev);
395 minor = iio_device_get_chrdev_minor();
398 goto error_device_put;
400 ev_int->dev.devt = MKDEV(MAJOR(iio_devt), minor);
401 dev_set_name(&ev_int->dev, "%s", name);
403 ret = device_add(&ev_int->dev);
405 goto error_free_minor;
407 cdev_init(&ev_int->handler.chrdev, &iio_event_chrdev_fileops);
408 ev_int->handler.chrdev.owner = owner;
410 mutex_init(&ev_int->event_list_lock);
411 /* discussion point - make this variable? */
412 ev_int->max_events = 10;
413 ev_int->current_events = 0;
414 INIT_LIST_HEAD(&ev_int->det_events.list);
415 init_waitqueue_head(&ev_int->wait);
416 ev_int->handler.private = ev_int;
417 ev_int->handler.flags = 0;
419 ret = cdev_add(&ev_int->handler.chrdev, ev_int->dev.devt, 1);
421 goto error_unreg_device;
426 device_unregister(&ev_int->dev);
428 iio_device_free_chrdev_minor(minor);
430 put_device(&ev_int->dev);
435 void iio_free_ev_int(struct iio_event_interface *ev_int)
437 device_unregister(&ev_int->dev);
438 put_device(&ev_int->dev);
441 static int __init iio_dev_init(void)
445 err = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio");
447 printk(KERN_ERR "%s: failed to allocate char dev region\n",
453 static void __exit iio_dev_exit(void)
456 unregister_chrdev_region(iio_devt, IIO_DEV_MAX);
459 static int __init iio_init(void)
463 /* Register sysfs bus */
464 ret = bus_register(&iio_bus_type);
467 "%s could not register bus type\n",
472 ret = iio_dev_init();
474 goto error_unregister_bus_type;
478 error_unregister_bus_type:
479 bus_unregister(&iio_bus_type);
484 static void __exit iio_exit(void)
487 bus_unregister(&iio_bus_type);
490 static int iio_device_register_sysfs(struct iio_dev *dev_info)
494 ret = sysfs_create_group(&dev_info->dev.kobj, dev_info->attrs);
496 dev_err(dev_info->dev.parent,
497 "Failed to register sysfs hooks\n");
505 static void iio_device_unregister_sysfs(struct iio_dev *dev_info)
507 sysfs_remove_group(&dev_info->dev.kobj, dev_info->attrs);
510 /* Return a negative errno on failure */
511 int iio_get_new_ida_val(struct ida *this_ida)
517 if (unlikely(ida_pre_get(this_ida, GFP_KERNEL) == 0))
520 spin_lock(&iio_ida_lock);
521 ret = ida_get_new(this_ida, &val);
522 spin_unlock(&iio_ida_lock);
523 if (unlikely(ret == -EAGAIN))
525 else if (unlikely(ret))
530 EXPORT_SYMBOL(iio_get_new_ida_val);
532 void iio_free_ida_val(struct ida *this_ida, int id)
534 spin_lock(&iio_ida_lock);
535 ida_remove(this_ida, id);
536 spin_unlock(&iio_ida_lock);
538 EXPORT_SYMBOL(iio_free_ida_val);
540 static int iio_device_register_id(struct iio_dev *dev_info,
541 struct ida *this_ida)
543 dev_info->id = iio_get_new_ida_val(&iio_ida);
544 if (dev_info->id < 0)
549 static void iio_device_unregister_id(struct iio_dev *dev_info)
551 iio_free_ida_val(&iio_ida, dev_info->id);
554 static inline int __iio_add_event_config_attrs(struct iio_dev *dev_info, int i)
557 /*p for adding, q for removing */
558 struct attribute **attrp, **attrq;
560 if (dev_info->event_conf_attrs && dev_info->event_conf_attrs[i].attrs) {
561 attrp = dev_info->event_conf_attrs[i].attrs;
563 ret = sysfs_add_file_to_group(&dev_info->dev.kobj,
566 ->event_attrs[i].name);
575 attrq = dev_info->event_conf_attrs[i].attrs;
576 while (attrq != attrp) {
577 sysfs_remove_file_from_group(&dev_info->dev.kobj,
579 dev_info->event_attrs[i].name);
586 static inline int __iio_remove_event_config_attrs(struct iio_dev *dev_info,
589 struct attribute **attrq;
591 if (dev_info->event_conf_attrs
592 && dev_info->event_conf_attrs[i].attrs) {
593 attrq = dev_info->event_conf_attrs[i].attrs;
595 sysfs_remove_file_from_group(&dev_info->dev.kobj,
598 ->event_attrs[i].name);
606 static int iio_device_register_eventset(struct iio_dev *dev_info)
610 if (dev_info->num_interrupt_lines == 0)
613 dev_info->event_interfaces =
614 kzalloc(sizeof(struct iio_event_interface)
615 *dev_info->num_interrupt_lines,
617 if (dev_info->event_interfaces == NULL) {
622 dev_info->interrupts = kzalloc(sizeof(struct iio_interrupt *)
623 *dev_info->num_interrupt_lines,
625 if (dev_info->interrupts == NULL) {
627 goto error_free_event_interfaces;
630 for (i = 0; i < dev_info->num_interrupt_lines; i++) {
631 dev_info->event_interfaces[i].owner = dev_info->driver_module;
633 snprintf(dev_info->event_interfaces[i]._name, 20,
635 dev_name(&dev_info->dev),
638 ret = iio_setup_ev_int(&dev_info->event_interfaces[i],
639 (const char *)(dev_info
640 ->event_interfaces[i]
642 dev_info->driver_module,
645 dev_err(&dev_info->dev,
646 "Could not get chrdev interface\n");
647 goto error_free_setup_ev_ints;
650 dev_set_drvdata(&dev_info->event_interfaces[i].dev,
652 ret = sysfs_create_group(&dev_info
653 ->event_interfaces[i]
655 &dev_info->event_attrs[i]);
658 dev_err(&dev_info->dev,
659 "Failed to register sysfs for event attrs");
660 goto error_remove_sysfs_interfaces;
664 for (i = 0; i < dev_info->num_interrupt_lines; i++) {
665 ret = __iio_add_event_config_attrs(dev_info, i);
667 goto error_unregister_config_attrs;
672 error_unregister_config_attrs:
673 for (j = 0; j < i; j++)
674 __iio_remove_event_config_attrs(dev_info, i);
675 i = dev_info->num_interrupt_lines - 1;
676 error_remove_sysfs_interfaces:
677 for (j = 0; j < i; j++)
678 sysfs_remove_group(&dev_info
679 ->event_interfaces[j].dev.kobj,
680 &dev_info->event_attrs[j]);
681 error_free_setup_ev_ints:
682 for (j = 0; j < i; j++)
683 iio_free_ev_int(&dev_info->event_interfaces[j]);
684 kfree(dev_info->interrupts);
685 error_free_event_interfaces:
686 kfree(dev_info->event_interfaces);
692 static void iio_device_unregister_eventset(struct iio_dev *dev_info)
696 if (dev_info->num_interrupt_lines == 0)
698 for (i = 0; i < dev_info->num_interrupt_lines; i++)
699 sysfs_remove_group(&dev_info
700 ->event_interfaces[i].dev.kobj,
701 &dev_info->event_attrs[i]);
703 for (i = 0; i < dev_info->num_interrupt_lines; i++)
704 iio_free_ev_int(&dev_info->event_interfaces[i]);
705 kfree(dev_info->interrupts);
706 kfree(dev_info->event_interfaces);
709 static void iio_dev_release(struct device *device)
711 struct iio_dev *dev = to_iio_dev(device);
717 static struct device_type iio_dev_type = {
718 .name = "iio_device",
719 .release = iio_dev_release,
722 struct iio_dev *iio_allocate_device(void)
724 struct iio_dev *dev = kzalloc(sizeof *dev, GFP_KERNEL);
727 dev->dev.type = &iio_dev_type;
728 dev->dev.bus = &iio_bus_type;
729 device_initialize(&dev->dev);
730 dev_set_drvdata(&dev->dev, (void *)dev);
731 mutex_init(&dev->mlock);
737 EXPORT_SYMBOL(iio_allocate_device);
739 void iio_free_device(struct iio_dev *dev)
744 EXPORT_SYMBOL(iio_free_device);
746 int iio_device_register(struct iio_dev *dev_info)
750 ret = iio_device_register_id(dev_info, &iio_ida);
752 dev_err(&dev_info->dev, "Failed to get id\n");
755 dev_set_name(&dev_info->dev, "device%d", dev_info->id);
757 ret = device_add(&dev_info->dev);
760 ret = iio_device_register_sysfs(dev_info);
762 dev_err(dev_info->dev.parent,
763 "Failed to register sysfs interfaces\n");
764 goto error_del_device;
766 ret = iio_device_register_eventset(dev_info);
768 dev_err(dev_info->dev.parent,
769 "Failed to register event set\n");
770 goto error_free_sysfs;
772 if (dev_info->modes & INDIO_RING_TRIGGERED)
773 iio_device_register_trigger_consumer(dev_info);
778 iio_device_unregister_sysfs(dev_info);
780 device_del(&dev_info->dev);
782 iio_device_unregister_id(dev_info);
786 EXPORT_SYMBOL(iio_device_register);
788 void iio_device_unregister(struct iio_dev *dev_info)
790 if (dev_info->modes & INDIO_RING_TRIGGERED)
791 iio_device_unregister_trigger_consumer(dev_info);
792 iio_device_unregister_eventset(dev_info);
793 iio_device_unregister_sysfs(dev_info);
794 iio_device_unregister_id(dev_info);
795 device_unregister(&dev_info->dev);
797 EXPORT_SYMBOL(iio_device_unregister);
801 module_put(THIS_MODULE);
806 __module_get(THIS_MODULE);
809 subsys_initcall(iio_init);
810 module_exit(iio_exit);
812 MODULE_AUTHOR("Jonathan Cameron <jic23@cam.ac.uk>");
813 MODULE_DESCRIPTION("Industrial I/O core");
814 MODULE_LICENSE("GPL");