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);
34 /* IDR for general event identifiers */
35 static DEFINE_IDR(iio_event_idr);
36 /* IDR to allocate character device minor numbers */
37 static DEFINE_IDR(iio_chrdev_idr);
38 /* Lock used to protect both of the above */
39 static DEFINE_SPINLOCK(iio_idr_lock);
42 EXPORT_SYMBOL(iio_devt);
44 #define IIO_DEV_MAX 256
45 struct bus_type iio_bus_type = {
48 EXPORT_SYMBOL(iio_bus_type);
50 void __iio_change_event(struct iio_detected_event_list *ev,
55 ev->ev.timestamp = timestamp;
57 EXPORT_SYMBOL(__iio_change_event);
59 /* Used both in the interrupt line put events and the ring buffer ones */
61 /* Note that in it's current form someone has to be listening before events
62 * are queued. Hence a client MUST open the chrdev before the ring buffer is
65 int __iio_push_event(struct iio_event_interface *ev_int,
68 struct iio_shared_ev_pointer *
71 struct iio_detected_event_list *ev;
74 /* Does anyone care? */
75 mutex_lock(&ev_int->event_list_lock);
76 if (test_bit(IIO_BUSY_BIT_POS, &ev_int->handler.flags)) {
77 if (ev_int->current_events == ev_int->max_events) {
78 mutex_unlock(&ev_int->event_list_lock);
81 ev = kmalloc(sizeof(*ev), GFP_KERNEL);
84 mutex_unlock(&ev_int->event_list_lock);
88 ev->ev.timestamp = timestamp;
89 ev->shared_pointer = shared_pointer_p;
90 if (ev->shared_pointer)
91 shared_pointer_p->ev_p = ev;
93 list_add_tail(&ev->list, &ev_int->det_events.list);
94 ev_int->current_events++;
95 mutex_unlock(&ev_int->event_list_lock);
96 wake_up_interruptible(&ev_int->wait);
98 mutex_unlock(&ev_int->event_list_lock);
103 EXPORT_SYMBOL(__iio_push_event);
105 int iio_push_event(struct iio_dev *dev_info,
110 return __iio_push_event(&dev_info->event_interfaces[ev_line],
111 ev_code, timestamp, NULL);
113 EXPORT_SYMBOL(iio_push_event);
115 /* Generic interrupt line interrupt handler */
116 static irqreturn_t iio_interrupt_handler(int irq, void *_int_info)
118 struct iio_interrupt *int_info = _int_info;
119 struct iio_dev *dev_info = int_info->dev_info;
120 struct iio_event_handler_list *p;
124 spin_lock_irqsave(&int_info->ev_list_lock, flags);
125 if (list_empty(&int_info->ev_list)) {
126 spin_unlock_irqrestore(&int_info->ev_list_lock, flags);
130 time_ns = iio_get_time_ns();
131 /* detect single element list*/
132 if (list_is_singular(&int_info->ev_list)) {
133 disable_irq_nosync(irq);
134 p = list_first_entry(&int_info->ev_list,
135 struct iio_event_handler_list,
137 /* single event handler - maybe shared */
138 p->handler(dev_info, 1, time_ns, !(p->refcount > 1));
140 list_for_each_entry(p, &int_info->ev_list, list) {
141 disable_irq_nosync(irq);
142 p->handler(dev_info, 1, time_ns, 0);
144 spin_unlock_irqrestore(&int_info->ev_list_lock, flags);
149 static struct iio_interrupt *iio_allocate_interrupt(void)
151 struct iio_interrupt *i = kmalloc(sizeof *i, GFP_KERNEL);
153 spin_lock_init(&i->ev_list_lock);
154 INIT_LIST_HEAD(&i->ev_list);
159 /* Confirming the validity of supplied irq is left to drivers.*/
160 int iio_register_interrupt_line(unsigned int irq,
161 struct iio_dev *dev_info,
168 dev_info->interrupts[line_number] = iio_allocate_interrupt();
169 if (dev_info->interrupts[line_number] == NULL) {
173 dev_info->interrupts[line_number]->line_number = line_number;
174 dev_info->interrupts[line_number]->irq = irq;
175 dev_info->interrupts[line_number]->dev_info = dev_info;
177 /* Possibly only request on demand?
178 * Can see this may complicate the handling of interrupts.
179 * However, with this approach we might end up handling lots of
180 * events no-one cares about.*/
181 ret = request_irq(irq,
182 &iio_interrupt_handler,
185 dev_info->interrupts[line_number]);
190 EXPORT_SYMBOL(iio_register_interrupt_line);
192 /* This turns up an awful lot */
193 ssize_t iio_read_const_attr(struct device *dev,
194 struct device_attribute *attr,
197 return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string);
199 EXPORT_SYMBOL(iio_read_const_attr);
201 /* Before this runs the interrupt generator must have been disabled */
202 void iio_unregister_interrupt_line(struct iio_dev *dev_info, int line_number)
204 /* make sure the interrupt handlers are all done */
205 flush_scheduled_work();
206 free_irq(dev_info->interrupts[line_number]->irq,
207 dev_info->interrupts[line_number]);
208 kfree(dev_info->interrupts[line_number]);
210 EXPORT_SYMBOL(iio_unregister_interrupt_line);
212 /* Reference counted add and remove */
213 void iio_add_event_to_list(struct iio_event_handler_list *el,
214 struct list_head *head)
217 struct iio_interrupt *inter = to_iio_interrupt(head);
219 /* take mutex to protect this element */
220 mutex_lock(&el->exist_lock);
221 if (el->refcount == 0) {
222 /* Take the event list spin lock */
223 spin_lock_irqsave(&inter->ev_list_lock, flags);
224 list_add(&el->list, head);
225 spin_unlock_irqrestore(&inter->ev_list_lock, flags);
228 mutex_unlock(&el->exist_lock);
230 EXPORT_SYMBOL(iio_add_event_to_list);
232 void iio_remove_event_from_list(struct iio_event_handler_list *el,
233 struct list_head *head)
236 struct iio_interrupt *inter = to_iio_interrupt(head);
238 mutex_lock(&el->exist_lock);
240 if (el->refcount == 0) {
241 /* Take the event list spin lock */
242 spin_lock_irqsave(&inter->ev_list_lock, flags);
243 list_del_init(&el->list);
244 spin_unlock_irqrestore(&inter->ev_list_lock, flags);
246 mutex_unlock(&el->exist_lock);
248 EXPORT_SYMBOL(iio_remove_event_from_list);
250 static ssize_t iio_event_chrdev_read(struct file *filep,
255 struct iio_event_interface *ev_int = filep->private_data;
256 struct iio_detected_event_list *el;
260 mutex_lock(&ev_int->event_list_lock);
261 if (list_empty(&ev_int->det_events.list)) {
262 if (filep->f_flags & O_NONBLOCK) {
264 goto error_mutex_unlock;
266 mutex_unlock(&ev_int->event_list_lock);
267 /* Blocking on device; waiting for something to be there */
268 ret = wait_event_interruptible(ev_int->wait,
273 /* Single access device so noone else can get the data */
274 mutex_lock(&ev_int->event_list_lock);
277 el = list_first_entry(&ev_int->det_events.list,
278 struct iio_detected_event_list,
281 if (copy_to_user(buf, &(el->ev), len)) {
283 goto error_mutex_unlock;
286 ev_int->current_events--;
287 mutex_unlock(&ev_int->event_list_lock);
289 * Possible concurency issue if an update of this event is on its way
290 * through. May lead to new event being removed whilst the reported
291 * event was the unescalated event. In typical use case this is not a
292 * problem as userspace will say read half the buffer due to a 50%
293 * full event which would make the correct 100% full incorrect anyway.
295 if (el->shared_pointer) {
296 spin_lock(&el->shared_pointer->lock);
297 (el->shared_pointer->ev_p) = NULL;
298 spin_unlock(&el->shared_pointer->lock);
305 mutex_unlock(&ev_int->event_list_lock);
311 static int iio_event_chrdev_release(struct inode *inode, struct file *filep)
313 struct iio_handler *hand = iio_cdev_to_handler(inode->i_cdev);
314 struct iio_event_interface *ev_int = hand->private;
315 struct iio_detected_event_list *el, *t;
317 mutex_lock(&ev_int->event_list_lock);
318 clear_bit(IIO_BUSY_BIT_POS, &ev_int->handler.flags);
320 * In order to maintain a clean state for reopening,
321 * clear out any awaiting events. The mask will prevent
322 * any new __iio_push_event calls running.
324 list_for_each_entry_safe(el, t, &ev_int->det_events.list, list) {
328 mutex_unlock(&ev_int->event_list_lock);
333 static int iio_event_chrdev_open(struct inode *inode, struct file *filep)
335 struct iio_handler *hand = iio_cdev_to_handler(inode->i_cdev);
336 struct iio_event_interface *ev_int = hand->private;
338 mutex_lock(&ev_int->event_list_lock);
339 if (test_and_set_bit(IIO_BUSY_BIT_POS, &hand->flags)) {
340 fops_put(filep->f_op);
341 mutex_unlock(&ev_int->event_list_lock);
344 filep->private_data = hand->private;
345 mutex_unlock(&ev_int->event_list_lock);
350 static const struct file_operations iio_event_chrdev_fileops = {
351 .read = iio_event_chrdev_read,
352 .release = iio_event_chrdev_release,
353 .open = iio_event_chrdev_open,
354 .owner = THIS_MODULE,
357 static void iio_event_dev_release(struct device *dev)
359 struct iio_event_interface *ev_int
360 = container_of(dev, struct iio_event_interface, dev);
361 cdev_del(&ev_int->handler.chrdev);
362 iio_device_free_chrdev_minor(MINOR(dev->devt));
365 static struct device_type iio_event_type = {
366 .release = iio_event_dev_release,
369 int iio_device_get_chrdev_minor(void)
374 if (unlikely(idr_pre_get(&iio_chrdev_idr, GFP_KERNEL) == 0))
376 spin_lock(&iio_idr_lock);
377 ret = idr_get_new(&iio_chrdev_idr, NULL, &val);
378 spin_unlock(&iio_idr_lock);
379 if (unlikely(ret == -EAGAIN))
381 else if (unlikely(ret))
383 if (val > IIO_DEV_MAX)
388 void iio_device_free_chrdev_minor(int val)
390 spin_lock(&iio_idr_lock);
391 idr_remove(&iio_chrdev_idr, val);
392 spin_unlock(&iio_idr_lock);
395 int iio_setup_ev_int(struct iio_event_interface *ev_int,
397 struct module *owner,
402 ev_int->dev.bus = &iio_bus_type;
403 ev_int->dev.parent = dev;
404 ev_int->dev.type = &iio_event_type;
405 device_initialize(&ev_int->dev);
407 minor = iio_device_get_chrdev_minor();
410 goto error_device_put;
412 ev_int->dev.devt = MKDEV(MAJOR(iio_devt), minor);
413 dev_set_name(&ev_int->dev, "%s", name);
415 ret = device_add(&ev_int->dev);
417 goto error_free_minor;
419 cdev_init(&ev_int->handler.chrdev, &iio_event_chrdev_fileops);
420 ev_int->handler.chrdev.owner = owner;
422 mutex_init(&ev_int->event_list_lock);
423 /* discussion point - make this variable? */
424 ev_int->max_events = 10;
425 ev_int->current_events = 0;
426 INIT_LIST_HEAD(&ev_int->det_events.list);
427 init_waitqueue_head(&ev_int->wait);
428 ev_int->handler.private = ev_int;
429 ev_int->handler.flags = 0;
431 ret = cdev_add(&ev_int->handler.chrdev, ev_int->dev.devt, 1);
433 goto error_unreg_device;
438 device_unregister(&ev_int->dev);
440 iio_device_free_chrdev_minor(minor);
442 put_device(&ev_int->dev);
447 void iio_free_ev_int(struct iio_event_interface *ev_int)
449 device_unregister(&ev_int->dev);
450 put_device(&ev_int->dev);
453 static int __init iio_dev_init(void)
457 err = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio");
459 printk(KERN_ERR "%s: failed to allocate char dev region\n",
465 static void __exit iio_dev_exit(void)
468 unregister_chrdev_region(iio_devt, IIO_DEV_MAX);
471 static int __init iio_init(void)
475 /* Register sysfs bus */
476 ret = bus_register(&iio_bus_type);
479 "%s could not register bus type\n",
484 ret = iio_dev_init();
486 goto error_unregister_bus_type;
490 error_unregister_bus_type:
491 bus_unregister(&iio_bus_type);
496 static void __exit iio_exit(void)
499 bus_unregister(&iio_bus_type);
502 static int iio_device_register_sysfs(struct iio_dev *dev_info)
506 ret = sysfs_create_group(&dev_info->dev.kobj, dev_info->attrs);
508 dev_err(dev_info->dev.parent,
509 "Failed to register sysfs hooks\n");
513 if (dev_info->scan_el_attrs) {
514 ret = sysfs_create_group(&dev_info->dev.kobj,
515 dev_info->scan_el_attrs);
517 dev_err(&dev_info->dev,
518 "Failed to add sysfs scan els\n");
525 static void iio_device_unregister_sysfs(struct iio_dev *dev_info)
527 if (dev_info->scan_el_attrs)
528 sysfs_remove_group(&dev_info->dev.kobj,
529 dev_info->scan_el_attrs);
531 sysfs_remove_group(&dev_info->dev.kobj, dev_info->attrs);
534 /* Return a negative errno on failure */
535 int iio_get_new_idr_val(struct idr *this_idr)
541 if (unlikely(idr_pre_get(this_idr, GFP_KERNEL) == 0))
544 spin_lock(&iio_idr_lock);
545 ret = idr_get_new(this_idr, NULL, &val);
546 spin_unlock(&iio_idr_lock);
547 if (unlikely(ret == -EAGAIN))
549 else if (unlikely(ret))
554 EXPORT_SYMBOL(iio_get_new_idr_val);
556 void iio_free_idr_val(struct idr *this_idr, int id)
558 spin_lock(&iio_idr_lock);
559 idr_remove(this_idr, id);
560 spin_unlock(&iio_idr_lock);
562 EXPORT_SYMBOL(iio_free_idr_val);
564 static int iio_device_register_id(struct iio_dev *dev_info,
565 struct idr *this_idr)
568 dev_info->id = iio_get_new_idr_val(&iio_idr);
569 if (dev_info->id < 0)
574 static void iio_device_unregister_id(struct iio_dev *dev_info)
576 iio_free_idr_val(&iio_idr, dev_info->id);
579 static inline int __iio_add_event_config_attrs(struct iio_dev *dev_info, int i)
582 /*p for adding, q for removing */
583 struct attribute **attrp, **attrq;
585 if (dev_info->event_conf_attrs && dev_info->event_conf_attrs[i].attrs) {
586 attrp = dev_info->event_conf_attrs[i].attrs;
588 ret = sysfs_add_file_to_group(&dev_info->dev.kobj,
591 ->event_attrs[i].name);
600 attrq = dev_info->event_conf_attrs[i].attrs;
601 while (attrq != attrp) {
602 sysfs_remove_file_from_group(&dev_info->dev.kobj,
604 dev_info->event_attrs[i].name);
611 static inline int __iio_remove_event_config_attrs(struct iio_dev *dev_info,
614 struct attribute **attrq;
616 if (dev_info->event_conf_attrs
617 && dev_info->event_conf_attrs[i].attrs) {
618 attrq = dev_info->event_conf_attrs[i].attrs;
620 sysfs_remove_file_from_group(&dev_info->dev.kobj,
623 ->event_attrs[i].name);
631 static int iio_device_register_eventset(struct iio_dev *dev_info)
635 if (dev_info->num_interrupt_lines == 0)
638 dev_info->event_interfaces =
639 kzalloc(sizeof(struct iio_event_interface)
640 *dev_info->num_interrupt_lines,
642 if (dev_info->event_interfaces == NULL) {
647 dev_info->interrupts = kzalloc(sizeof(struct iio_interrupt *)
648 *dev_info->num_interrupt_lines,
650 if (dev_info->interrupts == NULL) {
652 goto error_free_event_interfaces;
655 for (i = 0; i < dev_info->num_interrupt_lines; i++) {
656 dev_info->event_interfaces[i].owner = dev_info->driver_module;
657 ret = iio_get_new_idr_val(&iio_event_idr);
659 goto error_free_setup_ev_ints;
661 dev_info->event_interfaces[i].id = ret;
663 snprintf(dev_info->event_interfaces[i]._name, 20,
665 dev_name(&dev_info->dev),
666 dev_info->event_interfaces[i].id);
668 ret = iio_setup_ev_int(&dev_info->event_interfaces[i],
669 (const char *)(dev_info
670 ->event_interfaces[i]
672 dev_info->driver_module,
675 dev_err(&dev_info->dev,
676 "Could not get chrdev interface\n");
677 iio_free_idr_val(&iio_event_idr,
678 dev_info->event_interfaces[i].id);
679 goto error_free_setup_ev_ints;
682 dev_set_drvdata(&dev_info->event_interfaces[i].dev,
684 ret = sysfs_create_group(&dev_info
685 ->event_interfaces[i]
687 &dev_info->event_attrs[i]);
690 dev_err(&dev_info->dev,
691 "Failed to register sysfs for event attrs");
692 goto error_remove_sysfs_interfaces;
696 for (i = 0; i < dev_info->num_interrupt_lines; i++) {
697 ret = __iio_add_event_config_attrs(dev_info, i);
699 goto error_unregister_config_attrs;
704 error_unregister_config_attrs:
705 for (j = 0; j < i; j++)
706 __iio_remove_event_config_attrs(dev_info, i);
707 i = dev_info->num_interrupt_lines - 1;
708 error_remove_sysfs_interfaces:
709 for (j = 0; j < i; j++)
710 sysfs_remove_group(&dev_info
711 ->event_interfaces[j].dev.kobj,
712 &dev_info->event_attrs[j]);
713 error_free_setup_ev_ints:
714 for (j = 0; j < i; j++) {
715 iio_free_idr_val(&iio_event_idr,
716 dev_info->event_interfaces[j].id);
717 iio_free_ev_int(&dev_info->event_interfaces[j]);
719 kfree(dev_info->interrupts);
720 error_free_event_interfaces:
721 kfree(dev_info->event_interfaces);
727 static void iio_device_unregister_eventset(struct iio_dev *dev_info)
731 if (dev_info->num_interrupt_lines == 0)
733 for (i = 0; i < dev_info->num_interrupt_lines; i++)
734 sysfs_remove_group(&dev_info
735 ->event_interfaces[i].dev.kobj,
736 &dev_info->event_attrs[i]);
738 for (i = 0; i < dev_info->num_interrupt_lines; i++) {
739 iio_free_idr_val(&iio_event_idr,
740 dev_info->event_interfaces[i].id);
741 iio_free_ev_int(&dev_info->event_interfaces[i]);
743 kfree(dev_info->interrupts);
744 kfree(dev_info->event_interfaces);
747 static void iio_dev_release(struct device *device)
749 struct iio_dev *dev = to_iio_dev(device);
755 static struct device_type iio_dev_type = {
756 .name = "iio_device",
757 .release = iio_dev_release,
760 struct iio_dev *iio_allocate_device(void)
762 struct iio_dev *dev = kzalloc(sizeof *dev, GFP_KERNEL);
765 dev->dev.type = &iio_dev_type;
766 dev->dev.bus = &iio_bus_type;
767 device_initialize(&dev->dev);
768 dev_set_drvdata(&dev->dev, (void *)dev);
769 mutex_init(&dev->mlock);
775 EXPORT_SYMBOL(iio_allocate_device);
777 void iio_free_device(struct iio_dev *dev)
782 EXPORT_SYMBOL(iio_free_device);
784 int iio_device_register(struct iio_dev *dev_info)
788 ret = iio_device_register_id(dev_info, &iio_idr);
790 dev_err(&dev_info->dev, "Failed to get id\n");
793 dev_set_name(&dev_info->dev, "device%d", dev_info->id);
795 ret = device_add(&dev_info->dev);
798 ret = iio_device_register_sysfs(dev_info);
800 dev_err(dev_info->dev.parent,
801 "Failed to register sysfs interfaces\n");
802 goto error_del_device;
804 ret = iio_device_register_eventset(dev_info);
806 dev_err(dev_info->dev.parent,
807 "Failed to register event set\n");
808 goto error_free_sysfs;
810 if (dev_info->modes & INDIO_RING_TRIGGERED)
811 iio_device_register_trigger_consumer(dev_info);
816 iio_device_unregister_sysfs(dev_info);
818 device_del(&dev_info->dev);
820 iio_device_unregister_id(dev_info);
824 EXPORT_SYMBOL(iio_device_register);
826 void iio_device_unregister(struct iio_dev *dev_info)
828 if (dev_info->modes & INDIO_RING_TRIGGERED)
829 iio_device_unregister_trigger_consumer(dev_info);
830 iio_device_unregister_eventset(dev_info);
831 iio_device_unregister_sysfs(dev_info);
832 iio_device_unregister_id(dev_info);
833 device_unregister(&dev_info->dev);
835 EXPORT_SYMBOL(iio_device_unregister);
839 module_put(THIS_MODULE);
844 __module_get(THIS_MODULE);
847 subsys_initcall(iio_init);
848 module_exit(iio_exit);
850 MODULE_AUTHOR("Jonathan Cameron <jic23@cam.ac.uk>");
851 MODULE_DESCRIPTION("Industrial I/O core");
852 MODULE_LICENSE("GPL");