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 no one 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,
343 .llseek = noop_llseek,
346 static void iio_event_dev_release(struct device *dev)
348 struct iio_event_interface *ev_int
349 = container_of(dev, struct iio_event_interface, dev);
350 cdev_del(&ev_int->handler.chrdev);
351 iio_device_free_chrdev_minor(MINOR(dev->devt));
354 static struct device_type iio_event_type = {
355 .release = iio_event_dev_release,
358 int iio_device_get_chrdev_minor(void)
363 if (unlikely(ida_pre_get(&iio_chrdev_ida, GFP_KERNEL) == 0))
365 spin_lock(&iio_ida_lock);
366 ret = ida_get_new(&iio_chrdev_ida, &val);
367 spin_unlock(&iio_ida_lock);
368 if (unlikely(ret == -EAGAIN))
370 else if (unlikely(ret))
372 if (val > IIO_DEV_MAX)
377 void iio_device_free_chrdev_minor(int val)
379 spin_lock(&iio_ida_lock);
380 ida_remove(&iio_chrdev_ida, val);
381 spin_unlock(&iio_ida_lock);
384 int iio_setup_ev_int(struct iio_event_interface *ev_int,
386 struct module *owner,
391 ev_int->dev.bus = &iio_bus_type;
392 ev_int->dev.parent = dev;
393 ev_int->dev.type = &iio_event_type;
394 device_initialize(&ev_int->dev);
396 minor = iio_device_get_chrdev_minor();
399 goto error_device_put;
401 ev_int->dev.devt = MKDEV(MAJOR(iio_devt), minor);
402 dev_set_name(&ev_int->dev, "%s", name);
404 ret = device_add(&ev_int->dev);
406 goto error_free_minor;
408 cdev_init(&ev_int->handler.chrdev, &iio_event_chrdev_fileops);
409 ev_int->handler.chrdev.owner = owner;
411 mutex_init(&ev_int->event_list_lock);
412 /* discussion point - make this variable? */
413 ev_int->max_events = 10;
414 ev_int->current_events = 0;
415 INIT_LIST_HEAD(&ev_int->det_events.list);
416 init_waitqueue_head(&ev_int->wait);
417 ev_int->handler.private = ev_int;
418 ev_int->handler.flags = 0;
420 ret = cdev_add(&ev_int->handler.chrdev, ev_int->dev.devt, 1);
422 goto error_unreg_device;
427 device_unregister(&ev_int->dev);
429 iio_device_free_chrdev_minor(minor);
431 put_device(&ev_int->dev);
436 void iio_free_ev_int(struct iio_event_interface *ev_int)
438 device_unregister(&ev_int->dev);
439 put_device(&ev_int->dev);
442 static int __init iio_dev_init(void)
446 err = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio");
448 printk(KERN_ERR "%s: failed to allocate char dev region\n",
454 static void __exit iio_dev_exit(void)
457 unregister_chrdev_region(iio_devt, IIO_DEV_MAX);
460 static int __init iio_init(void)
464 /* Register sysfs bus */
465 ret = bus_register(&iio_bus_type);
468 "%s could not register bus type\n",
473 ret = iio_dev_init();
475 goto error_unregister_bus_type;
479 error_unregister_bus_type:
480 bus_unregister(&iio_bus_type);
485 static void __exit iio_exit(void)
488 bus_unregister(&iio_bus_type);
491 static int iio_device_register_sysfs(struct iio_dev *dev_info)
495 ret = sysfs_create_group(&dev_info->dev.kobj, dev_info->attrs);
497 dev_err(dev_info->dev.parent,
498 "Failed to register sysfs hooks\n");
506 static void iio_device_unregister_sysfs(struct iio_dev *dev_info)
508 sysfs_remove_group(&dev_info->dev.kobj, dev_info->attrs);
511 /* Return a negative errno on failure */
512 int iio_get_new_ida_val(struct ida *this_ida)
518 if (unlikely(ida_pre_get(this_ida, GFP_KERNEL) == 0))
521 spin_lock(&iio_ida_lock);
522 ret = ida_get_new(this_ida, &val);
523 spin_unlock(&iio_ida_lock);
524 if (unlikely(ret == -EAGAIN))
526 else if (unlikely(ret))
531 EXPORT_SYMBOL(iio_get_new_ida_val);
533 void iio_free_ida_val(struct ida *this_ida, int id)
535 spin_lock(&iio_ida_lock);
536 ida_remove(this_ida, id);
537 spin_unlock(&iio_ida_lock);
539 EXPORT_SYMBOL(iio_free_ida_val);
541 static int iio_device_register_id(struct iio_dev *dev_info,
542 struct ida *this_ida)
544 dev_info->id = iio_get_new_ida_val(&iio_ida);
545 if (dev_info->id < 0)
550 static void iio_device_unregister_id(struct iio_dev *dev_info)
552 iio_free_ida_val(&iio_ida, dev_info->id);
555 static inline int __iio_add_event_config_attrs(struct iio_dev *dev_info, int i)
558 /*p for adding, q for removing */
559 struct attribute **attrp, **attrq;
561 if (dev_info->event_conf_attrs && dev_info->event_conf_attrs[i].attrs) {
562 attrp = dev_info->event_conf_attrs[i].attrs;
564 ret = sysfs_add_file_to_group(&dev_info->dev.kobj,
567 ->event_attrs[i].name);
576 attrq = dev_info->event_conf_attrs[i].attrs;
577 while (attrq != attrp) {
578 sysfs_remove_file_from_group(&dev_info->dev.kobj,
580 dev_info->event_attrs[i].name);
587 static inline int __iio_remove_event_config_attrs(struct iio_dev *dev_info,
590 struct attribute **attrq;
592 if (dev_info->event_conf_attrs
593 && dev_info->event_conf_attrs[i].attrs) {
594 attrq = dev_info->event_conf_attrs[i].attrs;
596 sysfs_remove_file_from_group(&dev_info->dev.kobj,
599 ->event_attrs[i].name);
607 static int iio_device_register_eventset(struct iio_dev *dev_info)
611 if (dev_info->num_interrupt_lines == 0)
614 dev_info->event_interfaces =
615 kzalloc(sizeof(struct iio_event_interface)
616 *dev_info->num_interrupt_lines,
618 if (dev_info->event_interfaces == NULL) {
623 dev_info->interrupts = kzalloc(sizeof(struct iio_interrupt *)
624 *dev_info->num_interrupt_lines,
626 if (dev_info->interrupts == NULL) {
628 goto error_free_event_interfaces;
631 for (i = 0; i < dev_info->num_interrupt_lines; i++) {
632 dev_info->event_interfaces[i].owner = dev_info->driver_module;
634 snprintf(dev_info->event_interfaces[i]._name, 20,
636 dev_name(&dev_info->dev),
639 ret = iio_setup_ev_int(&dev_info->event_interfaces[i],
640 (const char *)(dev_info
641 ->event_interfaces[i]
643 dev_info->driver_module,
646 dev_err(&dev_info->dev,
647 "Could not get chrdev interface\n");
648 goto error_free_setup_ev_ints;
651 dev_set_drvdata(&dev_info->event_interfaces[i].dev,
653 ret = sysfs_create_group(&dev_info
654 ->event_interfaces[i]
656 &dev_info->event_attrs[i]);
659 dev_err(&dev_info->dev,
660 "Failed to register sysfs for event attrs");
661 goto error_remove_sysfs_interfaces;
665 for (i = 0; i < dev_info->num_interrupt_lines; i++) {
666 ret = __iio_add_event_config_attrs(dev_info, i);
668 goto error_unregister_config_attrs;
673 error_unregister_config_attrs:
674 for (j = 0; j < i; j++)
675 __iio_remove_event_config_attrs(dev_info, i);
676 i = dev_info->num_interrupt_lines - 1;
677 error_remove_sysfs_interfaces:
678 for (j = 0; j < i; j++)
679 sysfs_remove_group(&dev_info
680 ->event_interfaces[j].dev.kobj,
681 &dev_info->event_attrs[j]);
682 error_free_setup_ev_ints:
683 for (j = 0; j < i; j++)
684 iio_free_ev_int(&dev_info->event_interfaces[j]);
685 kfree(dev_info->interrupts);
686 error_free_event_interfaces:
687 kfree(dev_info->event_interfaces);
693 static void iio_device_unregister_eventset(struct iio_dev *dev_info)
697 if (dev_info->num_interrupt_lines == 0)
699 for (i = 0; i < dev_info->num_interrupt_lines; i++)
700 sysfs_remove_group(&dev_info
701 ->event_interfaces[i].dev.kobj,
702 &dev_info->event_attrs[i]);
704 for (i = 0; i < dev_info->num_interrupt_lines; i++)
705 iio_free_ev_int(&dev_info->event_interfaces[i]);
706 kfree(dev_info->interrupts);
707 kfree(dev_info->event_interfaces);
710 static void iio_dev_release(struct device *device)
712 struct iio_dev *dev = to_iio_dev(device);
718 static struct device_type iio_dev_type = {
719 .name = "iio_device",
720 .release = iio_dev_release,
723 struct iio_dev *iio_allocate_device(void)
725 struct iio_dev *dev = kzalloc(sizeof *dev, GFP_KERNEL);
728 dev->dev.type = &iio_dev_type;
729 dev->dev.bus = &iio_bus_type;
730 device_initialize(&dev->dev);
731 dev_set_drvdata(&dev->dev, (void *)dev);
732 mutex_init(&dev->mlock);
738 EXPORT_SYMBOL(iio_allocate_device);
740 void iio_free_device(struct iio_dev *dev)
745 EXPORT_SYMBOL(iio_free_device);
747 int iio_device_register(struct iio_dev *dev_info)
751 ret = iio_device_register_id(dev_info, &iio_ida);
753 dev_err(&dev_info->dev, "Failed to get id\n");
756 dev_set_name(&dev_info->dev, "device%d", dev_info->id);
758 ret = device_add(&dev_info->dev);
761 ret = iio_device_register_sysfs(dev_info);
763 dev_err(dev_info->dev.parent,
764 "Failed to register sysfs interfaces\n");
765 goto error_del_device;
767 ret = iio_device_register_eventset(dev_info);
769 dev_err(dev_info->dev.parent,
770 "Failed to register event set\n");
771 goto error_free_sysfs;
773 if (dev_info->modes & INDIO_RING_TRIGGERED)
774 iio_device_register_trigger_consumer(dev_info);
779 iio_device_unregister_sysfs(dev_info);
781 device_del(&dev_info->dev);
783 iio_device_unregister_id(dev_info);
787 EXPORT_SYMBOL(iio_device_register);
789 void iio_device_unregister(struct iio_dev *dev_info)
791 if (dev_info->modes & INDIO_RING_TRIGGERED)
792 iio_device_unregister_trigger_consumer(dev_info);
793 iio_device_unregister_eventset(dev_info);
794 iio_device_unregister_sysfs(dev_info);
795 iio_device_unregister_id(dev_info);
796 device_unregister(&dev_info->dev);
798 EXPORT_SYMBOL(iio_device_unregister);
802 module_put(THIS_MODULE);
807 __module_get(THIS_MODULE);
810 subsys_initcall(iio_init);
811 module_exit(iio_exit);
813 MODULE_AUTHOR("Jonathan Cameron <jic23@cam.ac.uk>");
814 MODULE_DESCRIPTION("Industrial I/O core");
815 MODULE_LICENSE("GPL");