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 static char *iio_devnode(struct device
*dev
, mode_t
*mode
)
47 return kasprintf(GFP_KERNEL
, "iio/%s", dev_name(dev
));
50 struct class iio_class
= {
52 .devnode
= iio_devnode
,
54 EXPORT_SYMBOL(iio_class
);
56 void __iio_change_event(struct iio_detected_event_list
*ev
,
61 ev
->ev
.timestamp
= timestamp
;
63 EXPORT_SYMBOL(__iio_change_event
);
65 /* Used both in the interrupt line put events and the ring buffer ones */
67 /* Note that in it's current form someone has to be listening before events
68 * are queued. Hence a client MUST open the chrdev before the ring buffer is
71 int __iio_push_event(struct iio_event_interface
*ev_int
,
74 struct iio_shared_ev_pointer
*
77 struct iio_detected_event_list
*ev
;
80 /* Does anyone care? */
81 mutex_lock(&ev_int
->event_list_lock
);
82 if (test_bit(IIO_BUSY_BIT_POS
, &ev_int
->handler
.flags
)) {
83 if (ev_int
->current_events
== ev_int
->max_events
) {
84 mutex_unlock(&ev_int
->event_list_lock
);
87 ev
= kmalloc(sizeof(*ev
), GFP_KERNEL
);
90 mutex_unlock(&ev_int
->event_list_lock
);
94 ev
->ev
.timestamp
= timestamp
;
95 ev
->shared_pointer
= shared_pointer_p
;
96 if (ev
->shared_pointer
)
97 shared_pointer_p
->ev_p
= ev
;
99 list_add_tail(&ev
->list
, &ev_int
->det_events
.list
);
100 ev_int
->current_events
++;
101 mutex_unlock(&ev_int
->event_list_lock
);
102 wake_up_interruptible(&ev_int
->wait
);
104 mutex_unlock(&ev_int
->event_list_lock
);
109 EXPORT_SYMBOL(__iio_push_event
);
111 int iio_push_event(struct iio_dev
*dev_info
,
116 return __iio_push_event(&dev_info
->event_interfaces
[ev_line
],
117 ev_code
, timestamp
, NULL
);
119 EXPORT_SYMBOL(iio_push_event
);
121 /* Generic interrupt line interrupt handler */
122 static irqreturn_t
iio_interrupt_handler(int irq
, void *_int_info
)
124 struct iio_interrupt
*int_info
= _int_info
;
125 struct iio_dev
*dev_info
= int_info
->dev_info
;
126 struct iio_event_handler_list
*p
;
130 spin_lock_irqsave(&int_info
->ev_list_lock
, flags
);
131 if (list_empty(&int_info
->ev_list
)) {
132 spin_unlock_irqrestore(&int_info
->ev_list_lock
, flags
);
136 time_ns
= iio_get_time_ns();
137 /* detect single element list*/
138 if (list_is_singular(&int_info
->ev_list
)) {
139 disable_irq_nosync(irq
);
140 p
= list_first_entry(&int_info
->ev_list
,
141 struct iio_event_handler_list
,
143 /* single event handler - maybe shared */
144 p
->handler(dev_info
, 1, time_ns
, !(p
->refcount
> 1));
146 list_for_each_entry(p
, &int_info
->ev_list
, list
) {
147 disable_irq_nosync(irq
);
148 p
->handler(dev_info
, 1, time_ns
, 0);
150 spin_unlock_irqrestore(&int_info
->ev_list_lock
, flags
);
155 static struct iio_interrupt
*iio_allocate_interrupt(void)
157 struct iio_interrupt
*i
= kmalloc(sizeof *i
, GFP_KERNEL
);
159 spin_lock_init(&i
->ev_list_lock
);
160 INIT_LIST_HEAD(&i
->ev_list
);
165 /* Confirming the validity of supplied irq is left to drivers.*/
166 int iio_register_interrupt_line(unsigned int irq
,
167 struct iio_dev
*dev_info
,
174 dev_info
->interrupts
[line_number
] = iio_allocate_interrupt();
175 if (dev_info
->interrupts
[line_number
] == NULL
) {
179 dev_info
->interrupts
[line_number
]->line_number
= line_number
;
180 dev_info
->interrupts
[line_number
]->irq
= irq
;
181 dev_info
->interrupts
[line_number
]->dev_info
= dev_info
;
183 /* Possibly only request on demand?
184 * Can see this may complicate the handling of interrupts.
185 * However, with this approach we might end up handling lots of
186 * events no-one cares about.*/
187 ret
= request_irq(irq
,
188 &iio_interrupt_handler
,
191 dev_info
->interrupts
[line_number
]);
196 EXPORT_SYMBOL(iio_register_interrupt_line
);
198 /* This turns up an awful lot */
199 ssize_t
iio_read_const_attr(struct device
*dev
,
200 struct device_attribute
*attr
,
203 return sprintf(buf
, "%s\n", to_iio_const_attr(attr
)->string
);
205 EXPORT_SYMBOL(iio_read_const_attr
);
207 /* Before this runs the interrupt generator must have been disabled */
208 void iio_unregister_interrupt_line(struct iio_dev
*dev_info
, int line_number
)
210 /* make sure the interrupt handlers are all done */
211 flush_scheduled_work();
212 free_irq(dev_info
->interrupts
[line_number
]->irq
,
213 dev_info
->interrupts
[line_number
]);
214 kfree(dev_info
->interrupts
[line_number
]);
216 EXPORT_SYMBOL(iio_unregister_interrupt_line
);
218 /* Reference counted add and remove */
219 void iio_add_event_to_list(struct iio_event_handler_list
*el
,
220 struct list_head
*head
)
223 struct iio_interrupt
*inter
= to_iio_interrupt(head
);
225 /* take mutex to protect this element */
226 mutex_lock(&el
->exist_lock
);
227 if (el
->refcount
== 0) {
228 /* Take the event list spin lock */
229 spin_lock_irqsave(&inter
->ev_list_lock
, flags
);
230 list_add(&el
->list
, head
);
231 spin_unlock_irqrestore(&inter
->ev_list_lock
, flags
);
234 mutex_unlock(&el
->exist_lock
);
236 EXPORT_SYMBOL(iio_add_event_to_list
);
238 void iio_remove_event_from_list(struct iio_event_handler_list
*el
,
239 struct list_head
*head
)
242 struct iio_interrupt
*inter
= to_iio_interrupt(head
);
244 mutex_lock(&el
->exist_lock
);
246 if (el
->refcount
== 0) {
247 /* Take the event list spin lock */
248 spin_lock_irqsave(&inter
->ev_list_lock
, flags
);
249 list_del_init(&el
->list
);
250 spin_unlock_irqrestore(&inter
->ev_list_lock
, flags
);
252 mutex_unlock(&el
->exist_lock
);
254 EXPORT_SYMBOL(iio_remove_event_from_list
);
256 static ssize_t
iio_event_chrdev_read(struct file
*filep
,
261 struct iio_event_interface
*ev_int
= filep
->private_data
;
262 struct iio_detected_event_list
*el
;
266 mutex_lock(&ev_int
->event_list_lock
);
267 if (list_empty(&ev_int
->det_events
.list
)) {
268 if (filep
->f_flags
& O_NONBLOCK
) {
270 goto error_mutex_unlock
;
272 mutex_unlock(&ev_int
->event_list_lock
);
273 /* Blocking on device; waiting for something to be there */
274 ret
= wait_event_interruptible(ev_int
->wait
,
279 /* Single access device so noone else can get the data */
280 mutex_lock(&ev_int
->event_list_lock
);
283 el
= list_first_entry(&ev_int
->det_events
.list
,
284 struct iio_detected_event_list
,
287 if (copy_to_user(buf
, &(el
->ev
), len
)) {
289 goto error_mutex_unlock
;
292 ev_int
->current_events
--;
293 mutex_unlock(&ev_int
->event_list_lock
);
295 * Possible concurency issue if an update of this event is on its way
296 * through. May lead to new event being removed whilst the reported
297 * event was the unescalated event. In typical use case this is not a
298 * problem as userspace will say read half the buffer due to a 50%
299 * full event which would make the correct 100% full incorrect anyway.
301 if (el
->shared_pointer
) {
302 spin_lock(&el
->shared_pointer
->lock
);
303 (el
->shared_pointer
->ev_p
) = NULL
;
304 spin_unlock(&el
->shared_pointer
->lock
);
311 mutex_unlock(&ev_int
->event_list_lock
);
317 static int iio_event_chrdev_release(struct inode
*inode
, struct file
*filep
)
319 struct iio_handler
*hand
= iio_cdev_to_handler(inode
->i_cdev
);
320 struct iio_event_interface
*ev_int
= hand
->private;
321 struct iio_detected_event_list
*el
, *t
;
323 mutex_lock(&ev_int
->event_list_lock
);
324 clear_bit(IIO_BUSY_BIT_POS
, &ev_int
->handler
.flags
);
326 * In order to maintain a clean state for reopening,
327 * clear out any awaiting events. The mask will prevent
328 * any new __iio_push_event calls running.
330 list_for_each_entry_safe(el
, t
, &ev_int
->det_events
.list
, list
) {
334 mutex_unlock(&ev_int
->event_list_lock
);
339 static int iio_event_chrdev_open(struct inode
*inode
, struct file
*filep
)
341 struct iio_handler
*hand
= iio_cdev_to_handler(inode
->i_cdev
);
342 struct iio_event_interface
*ev_int
= hand
->private;
344 mutex_lock(&ev_int
->event_list_lock
);
345 if (test_and_set_bit(IIO_BUSY_BIT_POS
, &hand
->flags
)) {
346 fops_put(filep
->f_op
);
347 mutex_unlock(&ev_int
->event_list_lock
);
350 filep
->private_data
= hand
->private;
351 mutex_unlock(&ev_int
->event_list_lock
);
356 static const struct file_operations iio_event_chrdev_fileops
= {
357 .read
= iio_event_chrdev_read
,
358 .release
= iio_event_chrdev_release
,
359 .open
= iio_event_chrdev_open
,
360 .owner
= THIS_MODULE
,
363 static void iio_event_dev_release(struct device
*dev
)
365 struct iio_event_interface
*ev_int
366 = container_of(dev
, struct iio_event_interface
, dev
);
367 cdev_del(&ev_int
->handler
.chrdev
);
368 iio_device_free_chrdev_minor(MINOR(dev
->devt
));
371 static struct device_type iio_event_type
= {
372 .release
= iio_event_dev_release
,
375 int iio_device_get_chrdev_minor(void)
380 if (unlikely(idr_pre_get(&iio_chrdev_idr
, GFP_KERNEL
) == 0))
382 spin_lock(&iio_idr_lock
);
383 ret
= idr_get_new(&iio_chrdev_idr
, NULL
, &val
);
384 spin_unlock(&iio_idr_lock
);
385 if (unlikely(ret
== -EAGAIN
))
387 else if (unlikely(ret
))
389 if (val
> IIO_DEV_MAX
)
394 void iio_device_free_chrdev_minor(int val
)
396 spin_lock(&iio_idr_lock
);
397 idr_remove(&iio_chrdev_idr
, val
);
398 spin_unlock(&iio_idr_lock
);
401 int iio_setup_ev_int(struct iio_event_interface
*ev_int
,
403 struct module
*owner
,
408 ev_int
->dev
.class = &iio_class
;
409 ev_int
->dev
.parent
= dev
;
410 ev_int
->dev
.type
= &iio_event_type
;
411 device_initialize(&ev_int
->dev
);
413 minor
= iio_device_get_chrdev_minor();
416 goto error_device_put
;
418 ev_int
->dev
.devt
= MKDEV(MAJOR(iio_devt
), minor
);
419 dev_set_name(&ev_int
->dev
, "%s", name
);
421 ret
= device_add(&ev_int
->dev
);
423 goto error_free_minor
;
425 cdev_init(&ev_int
->handler
.chrdev
, &iio_event_chrdev_fileops
);
426 ev_int
->handler
.chrdev
.owner
= owner
;
428 mutex_init(&ev_int
->event_list_lock
);
429 /* discussion point - make this variable? */
430 ev_int
->max_events
= 10;
431 ev_int
->current_events
= 0;
432 INIT_LIST_HEAD(&ev_int
->det_events
.list
);
433 init_waitqueue_head(&ev_int
->wait
);
434 ev_int
->handler
.private = ev_int
;
435 ev_int
->handler
.flags
= 0;
437 ret
= cdev_add(&ev_int
->handler
.chrdev
, ev_int
->dev
.devt
, 1);
439 goto error_unreg_device
;
444 device_unregister(&ev_int
->dev
);
446 iio_device_free_chrdev_minor(minor
);
448 put_device(&ev_int
->dev
);
453 void iio_free_ev_int(struct iio_event_interface
*ev_int
)
455 device_unregister(&ev_int
->dev
);
456 put_device(&ev_int
->dev
);
459 static int __init
iio_dev_init(void)
463 err
= alloc_chrdev_region(&iio_devt
, 0, IIO_DEV_MAX
, "iio");
465 printk(KERN_ERR
"%s: failed to allocate char dev region\n",
471 static void __exit
iio_dev_exit(void)
474 unregister_chrdev_region(iio_devt
, IIO_DEV_MAX
);
477 static int __init
iio_init(void)
481 /* Create sysfs class */
482 ret
= class_register(&iio_class
);
485 "%s could not create sysfs class\n",
490 ret
= iio_dev_init();
492 goto error_unregister_class
;
496 error_unregister_class
:
497 class_unregister(&iio_class
);
502 static void __exit
iio_exit(void)
505 class_unregister(&iio_class
);
508 static int iio_device_register_sysfs(struct iio_dev
*dev_info
)
512 ret
= sysfs_create_group(&dev_info
->dev
.kobj
, dev_info
->attrs
);
514 dev_err(dev_info
->dev
.parent
,
515 "Failed to register sysfs hooks\n");
519 if (dev_info
->scan_el_attrs
) {
520 ret
= sysfs_create_group(&dev_info
->dev
.kobj
,
521 dev_info
->scan_el_attrs
);
523 dev_err(&dev_info
->dev
,
524 "Failed to add sysfs scan els\n");
531 static void iio_device_unregister_sysfs(struct iio_dev
*dev_info
)
533 if (dev_info
->scan_el_attrs
)
534 sysfs_remove_group(&dev_info
->dev
.kobj
,
535 dev_info
->scan_el_attrs
);
537 sysfs_remove_group(&dev_info
->dev
.kobj
, dev_info
->attrs
);
540 /* Return a negative errno on failure */
541 int iio_get_new_idr_val(struct idr
*this_idr
)
547 if (unlikely(idr_pre_get(this_idr
, GFP_KERNEL
) == 0))
550 spin_lock(&iio_idr_lock
);
551 ret
= idr_get_new(this_idr
, NULL
, &val
);
552 spin_unlock(&iio_idr_lock
);
553 if (unlikely(ret
== -EAGAIN
))
555 else if (unlikely(ret
))
560 EXPORT_SYMBOL(iio_get_new_idr_val
);
562 void iio_free_idr_val(struct idr
*this_idr
, int id
)
564 spin_lock(&iio_idr_lock
);
565 idr_remove(this_idr
, id
);
566 spin_unlock(&iio_idr_lock
);
568 EXPORT_SYMBOL(iio_free_idr_val
);
570 static int iio_device_register_id(struct iio_dev
*dev_info
,
571 struct idr
*this_idr
)
574 dev_info
->id
= iio_get_new_idr_val(&iio_idr
);
575 if (dev_info
->id
< 0)
580 static void iio_device_unregister_id(struct iio_dev
*dev_info
)
582 iio_free_idr_val(&iio_idr
, dev_info
->id
);
585 static inline int __iio_add_event_config_attrs(struct iio_dev
*dev_info
, int i
)
588 /*p for adding, q for removing */
589 struct attribute
**attrp
, **attrq
;
591 if (dev_info
->event_conf_attrs
&& dev_info
->event_conf_attrs
[i
].attrs
) {
592 attrp
= dev_info
->event_conf_attrs
[i
].attrs
;
594 ret
= sysfs_add_file_to_group(&dev_info
->dev
.kobj
,
597 ->event_attrs
[i
].name
);
606 attrq
= dev_info
->event_conf_attrs
[i
].attrs
;
607 while (attrq
!= attrp
) {
608 sysfs_remove_file_from_group(&dev_info
->dev
.kobj
,
610 dev_info
->event_attrs
[i
].name
);
617 static inline int __iio_remove_event_config_attrs(struct iio_dev
*dev_info
,
620 struct attribute
**attrq
;
622 if (dev_info
->event_conf_attrs
623 && dev_info
->event_conf_attrs
[i
].attrs
) {
624 attrq
= dev_info
->event_conf_attrs
[i
].attrs
;
626 sysfs_remove_file_from_group(&dev_info
->dev
.kobj
,
629 ->event_attrs
[i
].name
);
637 static int iio_device_register_eventset(struct iio_dev
*dev_info
)
641 if (dev_info
->num_interrupt_lines
== 0)
644 dev_info
->event_interfaces
=
645 kzalloc(sizeof(struct iio_event_interface
)
646 *dev_info
->num_interrupt_lines
,
648 if (dev_info
->event_interfaces
== NULL
) {
653 dev_info
->interrupts
= kzalloc(sizeof(struct iio_interrupt
*)
654 *dev_info
->num_interrupt_lines
,
656 if (dev_info
->interrupts
== NULL
) {
658 goto error_free_event_interfaces
;
661 for (i
= 0; i
< dev_info
->num_interrupt_lines
; i
++) {
662 dev_info
->event_interfaces
[i
].owner
= dev_info
->driver_module
;
663 ret
= iio_get_new_idr_val(&iio_event_idr
);
665 goto error_free_setup_ev_ints
;
667 dev_info
->event_interfaces
[i
].id
= ret
;
669 snprintf(dev_info
->event_interfaces
[i
]._name
, 20,
671 dev_info
->event_interfaces
[i
].id
);
673 ret
= iio_setup_ev_int(&dev_info
->event_interfaces
[i
],
674 (const char *)(dev_info
675 ->event_interfaces
[i
]
677 dev_info
->driver_module
,
680 dev_err(&dev_info
->dev
,
681 "Could not get chrdev interface\n");
682 iio_free_idr_val(&iio_event_idr
,
683 dev_info
->event_interfaces
[i
].id
);
684 goto error_free_setup_ev_ints
;
688 for (i
= 0; i
< dev_info
->num_interrupt_lines
; i
++) {
689 snprintf(dev_info
->event_interfaces
[i
]._attrname
, 20,
690 "event_line%d_sources", i
);
691 dev_info
->event_attrs
[i
].name
693 (dev_info
->event_interfaces
[i
]._attrname
);
694 ret
= sysfs_create_group(&dev_info
->dev
.kobj
,
695 &dev_info
->event_attrs
[i
]);
697 dev_err(&dev_info
->dev
,
698 "Failed to register sysfs for event attrs");
699 goto error_remove_sysfs_interfaces
;
703 for (i
= 0; i
< dev_info
->num_interrupt_lines
; i
++) {
704 ret
= __iio_add_event_config_attrs(dev_info
, i
);
706 goto error_unregister_config_attrs
;
711 error_unregister_config_attrs
:
712 for (j
= 0; j
< i
; j
++)
713 __iio_remove_event_config_attrs(dev_info
, i
);
714 i
= dev_info
->num_interrupt_lines
- 1;
715 error_remove_sysfs_interfaces
:
716 for (j
= 0; j
< i
; j
++)
717 sysfs_remove_group(&dev_info
->dev
.kobj
,
718 &dev_info
->event_attrs
[j
]);
719 i
= dev_info
->num_interrupt_lines
- 1;
720 error_free_setup_ev_ints
:
721 for (j
= 0; j
< i
; j
++) {
722 iio_free_idr_val(&iio_event_idr
,
723 dev_info
->event_interfaces
[i
].id
);
724 iio_free_ev_int(&dev_info
->event_interfaces
[j
]);
726 kfree(dev_info
->interrupts
);
727 error_free_event_interfaces
:
728 kfree(dev_info
->event_interfaces
);
734 static void iio_device_unregister_eventset(struct iio_dev
*dev_info
)
738 if (dev_info
->num_interrupt_lines
== 0)
740 for (i
= 0; i
< dev_info
->num_interrupt_lines
; i
++)
741 sysfs_remove_group(&dev_info
->dev
.kobj
,
742 &dev_info
->event_attrs
[i
]);
744 for (i
= 0; i
< dev_info
->num_interrupt_lines
; i
++) {
745 iio_free_idr_val(&iio_event_idr
,
746 dev_info
->event_interfaces
[i
].id
);
747 iio_free_ev_int(&dev_info
->event_interfaces
[i
]);
749 kfree(dev_info
->interrupts
);
750 kfree(dev_info
->event_interfaces
);
753 static void iio_dev_release(struct device
*device
)
755 struct iio_dev
*dev
= to_iio_dev(device
);
761 static struct device_type iio_dev_type
= {
762 .name
= "iio_device",
763 .release
= iio_dev_release
,
766 struct iio_dev
*iio_allocate_device(void)
768 struct iio_dev
*dev
= kzalloc(sizeof *dev
, GFP_KERNEL
);
771 dev
->dev
.type
= &iio_dev_type
;
772 dev
->dev
.class = &iio_class
;
773 device_initialize(&dev
->dev
);
774 dev_set_drvdata(&dev
->dev
, (void *)dev
);
775 mutex_init(&dev
->mlock
);
781 EXPORT_SYMBOL(iio_allocate_device
);
783 void iio_free_device(struct iio_dev
*dev
)
788 EXPORT_SYMBOL(iio_free_device
);
790 int iio_device_register(struct iio_dev
*dev_info
)
794 ret
= iio_device_register_id(dev_info
, &iio_idr
);
796 dev_err(&dev_info
->dev
, "Failed to get id\n");
799 dev_set_name(&dev_info
->dev
, "device%d", dev_info
->id
);
801 ret
= device_add(&dev_info
->dev
);
804 ret
= iio_device_register_sysfs(dev_info
);
806 dev_err(dev_info
->dev
.parent
,
807 "Failed to register sysfs interfaces\n");
808 goto error_del_device
;
810 ret
= iio_device_register_eventset(dev_info
);
812 dev_err(dev_info
->dev
.parent
,
813 "Failed to register event set \n");
814 goto error_free_sysfs
;
816 if (dev_info
->modes
& INDIO_RING_TRIGGERED
)
817 iio_device_register_trigger_consumer(dev_info
);
822 iio_device_unregister_sysfs(dev_info
);
824 device_del(&dev_info
->dev
);
826 iio_device_unregister_id(dev_info
);
830 EXPORT_SYMBOL(iio_device_register
);
832 void iio_device_unregister(struct iio_dev
*dev_info
)
834 if (dev_info
->modes
& INDIO_RING_TRIGGERED
)
835 iio_device_unregister_trigger_consumer(dev_info
);
836 iio_device_unregister_eventset(dev_info
);
837 iio_device_unregister_sysfs(dev_info
);
838 iio_device_unregister_id(dev_info
);
839 device_unregister(&dev_info
->dev
);
841 EXPORT_SYMBOL(iio_device_unregister
);
845 module_put(THIS_MODULE
);
850 __module_get(THIS_MODULE
);
853 subsys_initcall(iio_init
);
854 module_exit(iio_exit
);
856 MODULE_AUTHOR("Jonathan Cameron <jic23@cam.ac.uk>");
857 MODULE_DESCRIPTION("Industrial I/O core");
858 MODULE_LICENSE("GPL");