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
,
352 .llseek
= noop_llseek
,
355 static void iio_event_dev_release(struct device
*dev
)
357 struct iio_event_interface
*ev_int
358 = container_of(dev
, struct iio_event_interface
, dev
);
359 cdev_del(&ev_int
->handler
.chrdev
);
360 iio_device_free_chrdev_minor(MINOR(dev
->devt
));
363 static struct device_type iio_event_type
= {
364 .release
= iio_event_dev_release
,
367 int iio_device_get_chrdev_minor(void)
372 if (unlikely(idr_pre_get(&iio_chrdev_idr
, GFP_KERNEL
) == 0))
374 spin_lock(&iio_idr_lock
);
375 ret
= idr_get_new(&iio_chrdev_idr
, NULL
, &val
);
376 spin_unlock(&iio_idr_lock
);
377 if (unlikely(ret
== -EAGAIN
))
379 else if (unlikely(ret
))
381 if (val
> IIO_DEV_MAX
)
386 void iio_device_free_chrdev_minor(int val
)
388 spin_lock(&iio_idr_lock
);
389 idr_remove(&iio_chrdev_idr
, val
);
390 spin_unlock(&iio_idr_lock
);
393 int iio_setup_ev_int(struct iio_event_interface
*ev_int
,
395 struct module
*owner
,
400 ev_int
->dev
.bus
= &iio_bus_type
;
401 ev_int
->dev
.parent
= dev
;
402 ev_int
->dev
.type
= &iio_event_type
;
403 device_initialize(&ev_int
->dev
);
405 minor
= iio_device_get_chrdev_minor();
408 goto error_device_put
;
410 ev_int
->dev
.devt
= MKDEV(MAJOR(iio_devt
), minor
);
411 dev_set_name(&ev_int
->dev
, "%s", name
);
413 ret
= device_add(&ev_int
->dev
);
415 goto error_free_minor
;
417 cdev_init(&ev_int
->handler
.chrdev
, &iio_event_chrdev_fileops
);
418 ev_int
->handler
.chrdev
.owner
= owner
;
420 mutex_init(&ev_int
->event_list_lock
);
421 /* discussion point - make this variable? */
422 ev_int
->max_events
= 10;
423 ev_int
->current_events
= 0;
424 INIT_LIST_HEAD(&ev_int
->det_events
.list
);
425 init_waitqueue_head(&ev_int
->wait
);
426 ev_int
->handler
.private = ev_int
;
427 ev_int
->handler
.flags
= 0;
429 ret
= cdev_add(&ev_int
->handler
.chrdev
, ev_int
->dev
.devt
, 1);
431 goto error_unreg_device
;
436 device_unregister(&ev_int
->dev
);
438 iio_device_free_chrdev_minor(minor
);
440 put_device(&ev_int
->dev
);
445 void iio_free_ev_int(struct iio_event_interface
*ev_int
)
447 device_unregister(&ev_int
->dev
);
448 put_device(&ev_int
->dev
);
451 static int __init
iio_dev_init(void)
455 err
= alloc_chrdev_region(&iio_devt
, 0, IIO_DEV_MAX
, "iio");
457 printk(KERN_ERR
"%s: failed to allocate char dev region\n",
463 static void __exit
iio_dev_exit(void)
466 unregister_chrdev_region(iio_devt
, IIO_DEV_MAX
);
469 static int __init
iio_init(void)
473 /* Register sysfs bus */
474 ret
= bus_register(&iio_bus_type
);
477 "%s could not register bus type\n",
482 ret
= iio_dev_init();
484 goto error_unregister_bus_type
;
488 error_unregister_bus_type
:
489 bus_unregister(&iio_bus_type
);
494 static void __exit
iio_exit(void)
497 bus_unregister(&iio_bus_type
);
500 static int iio_device_register_sysfs(struct iio_dev
*dev_info
)
504 ret
= sysfs_create_group(&dev_info
->dev
.kobj
, dev_info
->attrs
);
506 dev_err(dev_info
->dev
.parent
,
507 "Failed to register sysfs hooks\n");
511 if (dev_info
->scan_el_attrs
) {
512 ret
= sysfs_create_group(&dev_info
->dev
.kobj
,
513 dev_info
->scan_el_attrs
);
515 dev_err(&dev_info
->dev
,
516 "Failed to add sysfs scan els\n");
523 static void iio_device_unregister_sysfs(struct iio_dev
*dev_info
)
525 if (dev_info
->scan_el_attrs
)
526 sysfs_remove_group(&dev_info
->dev
.kobj
,
527 dev_info
->scan_el_attrs
);
529 sysfs_remove_group(&dev_info
->dev
.kobj
, dev_info
->attrs
);
532 /* Return a negative errno on failure */
533 int iio_get_new_idr_val(struct idr
*this_idr
)
539 if (unlikely(idr_pre_get(this_idr
, GFP_KERNEL
) == 0))
542 spin_lock(&iio_idr_lock
);
543 ret
= idr_get_new(this_idr
, NULL
, &val
);
544 spin_unlock(&iio_idr_lock
);
545 if (unlikely(ret
== -EAGAIN
))
547 else if (unlikely(ret
))
552 EXPORT_SYMBOL(iio_get_new_idr_val
);
554 void iio_free_idr_val(struct idr
*this_idr
, int id
)
556 spin_lock(&iio_idr_lock
);
557 idr_remove(this_idr
, id
);
558 spin_unlock(&iio_idr_lock
);
560 EXPORT_SYMBOL(iio_free_idr_val
);
562 static int iio_device_register_id(struct iio_dev
*dev_info
,
563 struct idr
*this_idr
)
566 dev_info
->id
= iio_get_new_idr_val(&iio_idr
);
567 if (dev_info
->id
< 0)
572 static void iio_device_unregister_id(struct iio_dev
*dev_info
)
574 iio_free_idr_val(&iio_idr
, dev_info
->id
);
577 static inline int __iio_add_event_config_attrs(struct iio_dev
*dev_info
, int i
)
580 /*p for adding, q for removing */
581 struct attribute
**attrp
, **attrq
;
583 if (dev_info
->event_conf_attrs
&& dev_info
->event_conf_attrs
[i
].attrs
) {
584 attrp
= dev_info
->event_conf_attrs
[i
].attrs
;
586 ret
= sysfs_add_file_to_group(&dev_info
->dev
.kobj
,
589 ->event_attrs
[i
].name
);
598 attrq
= dev_info
->event_conf_attrs
[i
].attrs
;
599 while (attrq
!= attrp
) {
600 sysfs_remove_file_from_group(&dev_info
->dev
.kobj
,
602 dev_info
->event_attrs
[i
].name
);
609 static inline int __iio_remove_event_config_attrs(struct iio_dev
*dev_info
,
612 struct attribute
**attrq
;
614 if (dev_info
->event_conf_attrs
615 && dev_info
->event_conf_attrs
[i
].attrs
) {
616 attrq
= dev_info
->event_conf_attrs
[i
].attrs
;
618 sysfs_remove_file_from_group(&dev_info
->dev
.kobj
,
621 ->event_attrs
[i
].name
);
629 static int iio_device_register_eventset(struct iio_dev
*dev_info
)
633 if (dev_info
->num_interrupt_lines
== 0)
636 dev_info
->event_interfaces
=
637 kzalloc(sizeof(struct iio_event_interface
)
638 *dev_info
->num_interrupt_lines
,
640 if (dev_info
->event_interfaces
== NULL
) {
645 dev_info
->interrupts
= kzalloc(sizeof(struct iio_interrupt
*)
646 *dev_info
->num_interrupt_lines
,
648 if (dev_info
->interrupts
== NULL
) {
650 goto error_free_event_interfaces
;
653 for (i
= 0; i
< dev_info
->num_interrupt_lines
; i
++) {
654 dev_info
->event_interfaces
[i
].owner
= dev_info
->driver_module
;
656 snprintf(dev_info
->event_interfaces
[i
]._name
, 20,
658 dev_name(&dev_info
->dev
),
661 ret
= iio_setup_ev_int(&dev_info
->event_interfaces
[i
],
662 (const char *)(dev_info
663 ->event_interfaces
[i
]
665 dev_info
->driver_module
,
668 dev_err(&dev_info
->dev
,
669 "Could not get chrdev interface\n");
670 goto error_free_setup_ev_ints
;
673 dev_set_drvdata(&dev_info
->event_interfaces
[i
].dev
,
675 ret
= sysfs_create_group(&dev_info
676 ->event_interfaces
[i
]
678 &dev_info
->event_attrs
[i
]);
681 dev_err(&dev_info
->dev
,
682 "Failed to register sysfs for event attrs");
683 goto error_remove_sysfs_interfaces
;
687 for (i
= 0; i
< dev_info
->num_interrupt_lines
; i
++) {
688 ret
= __iio_add_event_config_attrs(dev_info
, i
);
690 goto error_unregister_config_attrs
;
695 error_unregister_config_attrs
:
696 for (j
= 0; j
< i
; j
++)
697 __iio_remove_event_config_attrs(dev_info
, i
);
698 i
= dev_info
->num_interrupt_lines
- 1;
699 error_remove_sysfs_interfaces
:
700 for (j
= 0; j
< i
; j
++)
701 sysfs_remove_group(&dev_info
702 ->event_interfaces
[j
].dev
.kobj
,
703 &dev_info
->event_attrs
[j
]);
704 error_free_setup_ev_ints
:
705 for (j
= 0; j
< i
; j
++)
706 iio_free_ev_int(&dev_info
->event_interfaces
[j
]);
707 kfree(dev_info
->interrupts
);
708 error_free_event_interfaces
:
709 kfree(dev_info
->event_interfaces
);
715 static void iio_device_unregister_eventset(struct iio_dev
*dev_info
)
719 if (dev_info
->num_interrupt_lines
== 0)
721 for (i
= 0; i
< dev_info
->num_interrupt_lines
; i
++)
722 sysfs_remove_group(&dev_info
723 ->event_interfaces
[i
].dev
.kobj
,
724 &dev_info
->event_attrs
[i
]);
726 for (i
= 0; i
< dev_info
->num_interrupt_lines
; i
++)
727 iio_free_ev_int(&dev_info
->event_interfaces
[i
]);
728 kfree(dev_info
->interrupts
);
729 kfree(dev_info
->event_interfaces
);
732 static void iio_dev_release(struct device
*device
)
734 struct iio_dev
*dev
= to_iio_dev(device
);
740 static struct device_type iio_dev_type
= {
741 .name
= "iio_device",
742 .release
= iio_dev_release
,
745 struct iio_dev
*iio_allocate_device(void)
747 struct iio_dev
*dev
= kzalloc(sizeof *dev
, GFP_KERNEL
);
750 dev
->dev
.type
= &iio_dev_type
;
751 dev
->dev
.bus
= &iio_bus_type
;
752 device_initialize(&dev
->dev
);
753 dev_set_drvdata(&dev
->dev
, (void *)dev
);
754 mutex_init(&dev
->mlock
);
760 EXPORT_SYMBOL(iio_allocate_device
);
762 void iio_free_device(struct iio_dev
*dev
)
767 EXPORT_SYMBOL(iio_free_device
);
769 int iio_device_register(struct iio_dev
*dev_info
)
773 ret
= iio_device_register_id(dev_info
, &iio_idr
);
775 dev_err(&dev_info
->dev
, "Failed to get id\n");
778 dev_set_name(&dev_info
->dev
, "device%d", dev_info
->id
);
780 ret
= device_add(&dev_info
->dev
);
783 ret
= iio_device_register_sysfs(dev_info
);
785 dev_err(dev_info
->dev
.parent
,
786 "Failed to register sysfs interfaces\n");
787 goto error_del_device
;
789 ret
= iio_device_register_eventset(dev_info
);
791 dev_err(dev_info
->dev
.parent
,
792 "Failed to register event set\n");
793 goto error_free_sysfs
;
795 if (dev_info
->modes
& INDIO_RING_TRIGGERED
)
796 iio_device_register_trigger_consumer(dev_info
);
801 iio_device_unregister_sysfs(dev_info
);
803 device_del(&dev_info
->dev
);
805 iio_device_unregister_id(dev_info
);
809 EXPORT_SYMBOL(iio_device_register
);
811 void iio_device_unregister(struct iio_dev
*dev_info
)
813 if (dev_info
->modes
& INDIO_RING_TRIGGERED
)
814 iio_device_unregister_trigger_consumer(dev_info
);
815 iio_device_unregister_eventset(dev_info
);
816 iio_device_unregister_sysfs(dev_info
);
817 iio_device_unregister_id(dev_info
);
818 device_unregister(&dev_info
->dev
);
820 EXPORT_SYMBOL(iio_device_unregister
);
824 module_put(THIS_MODULE
);
829 __module_get(THIS_MODULE
);
832 subsys_initcall(iio_init
);
833 module_exit(iio_exit
);
835 MODULE_AUTHOR("Jonathan Cameron <jic23@cam.ac.uk>");
836 MODULE_DESCRIPTION("Industrial I/O core");
837 MODULE_LICENSE("GPL");