]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/staging/iio/industrialio-core.c
llseek: automatically add .llseek fop
[mirror_ubuntu-artful-kernel.git] / drivers / staging / iio / industrialio-core.c
CommitLineData
847ec80b
JC
1/* The industrial I/O core
2 *
3 * Copyright (c) 2008 Jonathan Cameron
4 *
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.
8 *
9 * Based on elements of hwmon and input subsystems.
10 */
11
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>
18#include <linux/fs.h>
19#include <linux/interrupt.h>
20#include <linux/poll.h>
ffc18afa 21#include <linux/sched.h>
4439c935 22#include <linux/wait.h>
847ec80b 23#include <linux/cdev.h>
5a0e3ad6 24#include <linux/slab.h>
847ec80b
JC
25#include "iio.h"
26#include "trigger_consumer.h"
27
28#define IIO_ID_PREFIX "device"
29#define IIO_ID_FORMAT IIO_ID_PREFIX "%d"
30
31/* IDR to assign each registered device a unique id*/
32static DEFINE_IDR(iio_idr);
847ec80b
JC
33/* IDR to allocate character device minor numbers */
34static DEFINE_IDR(iio_chrdev_idr);
35/* Lock used to protect both of the above */
36static DEFINE_SPINLOCK(iio_idr_lock);
37
38dev_t iio_devt;
39EXPORT_SYMBOL(iio_devt);
40
41#define IIO_DEV_MAX 256
5aaaeba8 42struct bus_type iio_bus_type = {
847ec80b 43 .name = "iio",
847ec80b 44};
5aaaeba8 45EXPORT_SYMBOL(iio_bus_type);
847ec80b
JC
46
47void __iio_change_event(struct iio_detected_event_list *ev,
48 int ev_code,
49 s64 timestamp)
50{
51 ev->ev.id = ev_code;
52 ev->ev.timestamp = timestamp;
53}
54EXPORT_SYMBOL(__iio_change_event);
55
56/* Used both in the interrupt line put events and the ring buffer ones */
57
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
60 * switched on.
61 */
62 int __iio_push_event(struct iio_event_interface *ev_int,
63 int ev_code,
64 s64 timestamp,
65 struct iio_shared_ev_pointer *
66 shared_pointer_p)
67{
68 struct iio_detected_event_list *ev;
69 int ret = 0;
70
71 /* Does anyone care? */
72 mutex_lock(&ev_int->event_list_lock);
73 if (test_bit(IIO_BUSY_BIT_POS, &ev_int->handler.flags)) {
75c80753
JC
74 if (ev_int->current_events == ev_int->max_events) {
75 mutex_unlock(&ev_int->event_list_lock);
847ec80b 76 return 0;
75c80753 77 }
847ec80b
JC
78 ev = kmalloc(sizeof(*ev), GFP_KERNEL);
79 if (ev == NULL) {
80 ret = -ENOMEM;
75c80753 81 mutex_unlock(&ev_int->event_list_lock);
847ec80b
JC
82 goto error_ret;
83 }
84 ev->ev.id = ev_code;
85 ev->ev.timestamp = timestamp;
86 ev->shared_pointer = shared_pointer_p;
87 if (ev->shared_pointer)
88 shared_pointer_p->ev_p = ev;
89
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);
94 } else
95 mutex_unlock(&ev_int->event_list_lock);
96
97error_ret:
98 return ret;
99}
100EXPORT_SYMBOL(__iio_push_event);
101
102int iio_push_event(struct iio_dev *dev_info,
103 int ev_line,
104 int ev_code,
105 s64 timestamp)
106{
107 return __iio_push_event(&dev_info->event_interfaces[ev_line],
108 ev_code, timestamp, NULL);
109}
110EXPORT_SYMBOL(iio_push_event);
111
112/* Generic interrupt line interrupt handler */
77712e5f 113static irqreturn_t iio_interrupt_handler(int irq, void *_int_info)
847ec80b
JC
114{
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;
118 s64 time_ns;
119 unsigned long flags;
120
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);
124 return IRQ_NONE;
125 }
126
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,
133 list);
134 /* single event handler - maybe shared */
135 p->handler(dev_info, 1, time_ns, !(p->refcount > 1));
136 } else
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);
140 }
141 spin_unlock_irqrestore(&int_info->ev_list_lock, flags);
142
143 return IRQ_HANDLED;
144}
145
146static struct iio_interrupt *iio_allocate_interrupt(void)
147{
148 struct iio_interrupt *i = kmalloc(sizeof *i, GFP_KERNEL);
149 if (i) {
150 spin_lock_init(&i->ev_list_lock);
151 INIT_LIST_HEAD(&i->ev_list);
152 }
153 return i;
154}
155
156/* Confirming the validity of supplied irq is left to drivers.*/
157int iio_register_interrupt_line(unsigned int irq,
158 struct iio_dev *dev_info,
159 int line_number,
160 unsigned long type,
161 const char *name)
162{
163 int ret;
164
165 dev_info->interrupts[line_number] = iio_allocate_interrupt();
166 if (dev_info->interrupts[line_number] == NULL) {
167 ret = -ENOMEM;
168 goto error_ret;
169 }
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;
173
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,
180 type,
181 name,
182 dev_info->interrupts[line_number]);
183
184error_ret:
185 return ret;
186}
187EXPORT_SYMBOL(iio_register_interrupt_line);
188
189/* This turns up an awful lot */
190ssize_t iio_read_const_attr(struct device *dev,
191 struct device_attribute *attr,
192 char *buf)
193{
194 return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string);
195}
196EXPORT_SYMBOL(iio_read_const_attr);
197
198/* Before this runs the interrupt generator must have been disabled */
199void iio_unregister_interrupt_line(struct iio_dev *dev_info, int line_number)
200{
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]);
206}
207EXPORT_SYMBOL(iio_unregister_interrupt_line);
208
209/* Reference counted add and remove */
210void iio_add_event_to_list(struct iio_event_handler_list *el,
211 struct list_head *head)
212{
213 unsigned long flags;
214 struct iio_interrupt *inter = to_iio_interrupt(head);
215
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);
223 }
224 el->refcount++;
225 mutex_unlock(&el->exist_lock);
226}
227EXPORT_SYMBOL(iio_add_event_to_list);
228
229void iio_remove_event_from_list(struct iio_event_handler_list *el,
230 struct list_head *head)
231{
232 unsigned long flags;
233 struct iio_interrupt *inter = to_iio_interrupt(head);
234
235 mutex_lock(&el->exist_lock);
236 el->refcount--;
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);
242 }
243 mutex_unlock(&el->exist_lock);
244}
245EXPORT_SYMBOL(iio_remove_event_from_list);
246
77712e5f
MB
247static ssize_t iio_event_chrdev_read(struct file *filep,
248 char __user *buf,
249 size_t count,
250 loff_t *f_ps)
847ec80b
JC
251{
252 struct iio_event_interface *ev_int = filep->private_data;
253 struct iio_detected_event_list *el;
254 int ret;
255 size_t len;
256
257 mutex_lock(&ev_int->event_list_lock);
258 if (list_empty(&ev_int->det_events.list)) {
259 if (filep->f_flags & O_NONBLOCK) {
260 ret = -EAGAIN;
261 goto error_mutex_unlock;
262 }
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,
266 !list_empty(&ev_int
267 ->det_events.list));
268 if (ret)
269 goto error_ret;
270 /* Single access device so noone else can get the data */
271 mutex_lock(&ev_int->event_list_lock);
272 }
273
274 el = list_first_entry(&ev_int->det_events.list,
275 struct iio_detected_event_list,
276 list);
277 len = sizeof el->ev;
278 if (copy_to_user(buf, &(el->ev), len)) {
279 ret = -EFAULT;
280 goto error_mutex_unlock;
281 }
282 list_del(&el->list);
283 ev_int->current_events--;
284 mutex_unlock(&ev_int->event_list_lock);
285 /*
286 * Possible concurency issue if an update of this event is on its way
75b16013
JC
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.
847ec80b 291 */
75b16013
JC
292 if (el->shared_pointer) {
293 spin_lock(&el->shared_pointer->lock);
847ec80b 294 (el->shared_pointer->ev_p) = NULL;
75b16013
JC
295 spin_unlock(&el->shared_pointer->lock);
296 }
847ec80b
JC
297 kfree(el);
298
299 return len;
300
301error_mutex_unlock:
302 mutex_unlock(&ev_int->event_list_lock);
303error_ret:
304
305 return ret;
306}
307
77712e5f 308static int iio_event_chrdev_release(struct inode *inode, struct file *filep)
847ec80b
JC
309{
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;
313
314 mutex_lock(&ev_int->event_list_lock);
315 clear_bit(IIO_BUSY_BIT_POS, &ev_int->handler.flags);
316 /*
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.
320 */
321 list_for_each_entry_safe(el, t, &ev_int->det_events.list, list) {
322 list_del(&el->list);
323 kfree(el);
324 }
325 mutex_unlock(&ev_int->event_list_lock);
326
327 return 0;
328}
329
77712e5f 330static int iio_event_chrdev_open(struct inode *inode, struct file *filep)
847ec80b
JC
331{
332 struct iio_handler *hand = iio_cdev_to_handler(inode->i_cdev);
333 struct iio_event_interface *ev_int = hand->private;
334
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);
339 return -EBUSY;
340 }
341 filep->private_data = hand->private;
342 mutex_unlock(&ev_int->event_list_lock);
343
344 return 0;
345}
346
347static 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,
6038f373 352 .llseek = noop_llseek,
847ec80b
JC
353};
354
355static void iio_event_dev_release(struct device *dev)
356{
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));
361};
362
363static struct device_type iio_event_type = {
364 .release = iio_event_dev_release,
365};
366
367int iio_device_get_chrdev_minor(void)
368{
369 int ret, val;
370
371idr_again:
372 if (unlikely(idr_pre_get(&iio_chrdev_idr, GFP_KERNEL) == 0))
373 return -ENOMEM;
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))
378 goto idr_again;
379 else if (unlikely(ret))
380 return ret;
381 if (val > IIO_DEV_MAX)
382 return -ENOMEM;
383 return val;
384}
385
386void iio_device_free_chrdev_minor(int val)
387{
388 spin_lock(&iio_idr_lock);
389 idr_remove(&iio_chrdev_idr, val);
390 spin_unlock(&iio_idr_lock);
391}
392
393int iio_setup_ev_int(struct iio_event_interface *ev_int,
394 const char *name,
395 struct module *owner,
396 struct device *dev)
397{
398 int ret, minor;
399
5aaaeba8 400 ev_int->dev.bus = &iio_bus_type;
847ec80b
JC
401 ev_int->dev.parent = dev;
402 ev_int->dev.type = &iio_event_type;
403 device_initialize(&ev_int->dev);
404
405 minor = iio_device_get_chrdev_minor();
406 if (minor < 0) {
407 ret = minor;
408 goto error_device_put;
409 }
410 ev_int->dev.devt = MKDEV(MAJOR(iio_devt), minor);
411 dev_set_name(&ev_int->dev, "%s", name);
412
413 ret = device_add(&ev_int->dev);
414 if (ret)
415 goto error_free_minor;
416
417 cdev_init(&ev_int->handler.chrdev, &iio_event_chrdev_fileops);
418 ev_int->handler.chrdev.owner = owner;
419
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;
428
429 ret = cdev_add(&ev_int->handler.chrdev, ev_int->dev.devt, 1);
430 if (ret)
431 goto error_unreg_device;
432
433 return 0;
434
435error_unreg_device:
436 device_unregister(&ev_int->dev);
437error_free_minor:
438 iio_device_free_chrdev_minor(minor);
439error_device_put:
440 put_device(&ev_int->dev);
441
442 return ret;
443}
444
445void iio_free_ev_int(struct iio_event_interface *ev_int)
446{
447 device_unregister(&ev_int->dev);
448 put_device(&ev_int->dev);
449}
450
451static int __init iio_dev_init(void)
452{
453 int err;
454
455 err = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio");
456 if (err < 0)
457 printk(KERN_ERR "%s: failed to allocate char dev region\n",
458 __FILE__);
459
460 return err;
461}
462
463static void __exit iio_dev_exit(void)
464{
465 if (iio_devt)
466 unregister_chrdev_region(iio_devt, IIO_DEV_MAX);
467}
468
469static int __init iio_init(void)
470{
471 int ret;
472
5aaaeba8
JC
473 /* Register sysfs bus */
474 ret = bus_register(&iio_bus_type);
847ec80b
JC
475 if (ret < 0) {
476 printk(KERN_ERR
5aaaeba8 477 "%s could not register bus type\n",
847ec80b
JC
478 __FILE__);
479 goto error_nothing;
480 }
481
482 ret = iio_dev_init();
483 if (ret < 0)
5aaaeba8 484 goto error_unregister_bus_type;
847ec80b
JC
485
486 return 0;
487
5aaaeba8
JC
488error_unregister_bus_type:
489 bus_unregister(&iio_bus_type);
847ec80b
JC
490error_nothing:
491 return ret;
492}
493
494static void __exit iio_exit(void)
495{
496 iio_dev_exit();
5aaaeba8 497 bus_unregister(&iio_bus_type);
847ec80b
JC
498}
499
500static int iio_device_register_sysfs(struct iio_dev *dev_info)
501{
502 int ret = 0;
503
504 ret = sysfs_create_group(&dev_info->dev.kobj, dev_info->attrs);
505 if (ret) {
506 dev_err(dev_info->dev.parent,
507 "Failed to register sysfs hooks\n");
508 goto error_ret;
509 }
510
511 if (dev_info->scan_el_attrs) {
512 ret = sysfs_create_group(&dev_info->dev.kobj,
513 dev_info->scan_el_attrs);
514 if (ret)
515 dev_err(&dev_info->dev,
516 "Failed to add sysfs scan els\n");
517 }
518
519error_ret:
520 return ret;
521}
522
523static void iio_device_unregister_sysfs(struct iio_dev *dev_info)
524{
525 if (dev_info->scan_el_attrs)
526 sysfs_remove_group(&dev_info->dev.kobj,
527 dev_info->scan_el_attrs);
528
529 sysfs_remove_group(&dev_info->dev.kobj, dev_info->attrs);
530}
531
f3cdc285 532/* Return a negative errno on failure */
847ec80b
JC
533int iio_get_new_idr_val(struct idr *this_idr)
534{
535 int ret;
536 int val;
537
538idr_again:
539 if (unlikely(idr_pre_get(this_idr, GFP_KERNEL) == 0))
540 return -ENOMEM;
541
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))
546 goto idr_again;
547 else if (unlikely(ret))
548 return ret;
549
550 return val;
551}
552EXPORT_SYMBOL(iio_get_new_idr_val);
553
554void iio_free_idr_val(struct idr *this_idr, int id)
555{
556 spin_lock(&iio_idr_lock);
557 idr_remove(this_idr, id);
558 spin_unlock(&iio_idr_lock);
559}
560EXPORT_SYMBOL(iio_free_idr_val);
561
562static int iio_device_register_id(struct iio_dev *dev_info,
563 struct idr *this_idr)
564{
565
566 dev_info->id = iio_get_new_idr_val(&iio_idr);
567 if (dev_info->id < 0)
568 return dev_info->id;
569 return 0;
570}
571
572static void iio_device_unregister_id(struct iio_dev *dev_info)
573{
574 iio_free_idr_val(&iio_idr, dev_info->id);
575}
576
577static inline int __iio_add_event_config_attrs(struct iio_dev *dev_info, int i)
578{
579 int ret;
580 /*p for adding, q for removing */
581 struct attribute **attrp, **attrq;
582
583 if (dev_info->event_conf_attrs && dev_info->event_conf_attrs[i].attrs) {
584 attrp = dev_info->event_conf_attrs[i].attrs;
585 while (*attrp) {
586 ret = sysfs_add_file_to_group(&dev_info->dev.kobj,
587 *attrp,
588 dev_info
589 ->event_attrs[i].name);
590 if (ret)
591 goto error_ret;
592 attrp++;
593 }
594 }
595 return 0;
596
597error_ret:
598 attrq = dev_info->event_conf_attrs[i].attrs;
599 while (attrq != attrp) {
600 sysfs_remove_file_from_group(&dev_info->dev.kobj,
601 *attrq,
602 dev_info->event_attrs[i].name);
603 attrq++;
604 }
605
606 return ret;
607}
608
609static inline int __iio_remove_event_config_attrs(struct iio_dev *dev_info,
610 int i)
611{
612 struct attribute **attrq;
613
614 if (dev_info->event_conf_attrs
615 && dev_info->event_conf_attrs[i].attrs) {
616 attrq = dev_info->event_conf_attrs[i].attrs;
617 while (*attrq) {
618 sysfs_remove_file_from_group(&dev_info->dev.kobj,
619 *attrq,
620 dev_info
621 ->event_attrs[i].name);
622 attrq++;
623 }
624 }
625
626 return 0;
627}
628
629static int iio_device_register_eventset(struct iio_dev *dev_info)
630{
631 int ret = 0, i, j;
632
633 if (dev_info->num_interrupt_lines == 0)
634 return 0;
635
636 dev_info->event_interfaces =
637 kzalloc(sizeof(struct iio_event_interface)
638 *dev_info->num_interrupt_lines,
639 GFP_KERNEL);
640 if (dev_info->event_interfaces == NULL) {
641 ret = -ENOMEM;
642 goto error_ret;
643 }
644
645 dev_info->interrupts = kzalloc(sizeof(struct iio_interrupt *)
646 *dev_info->num_interrupt_lines,
647 GFP_KERNEL);
648 if (dev_info->interrupts == NULL) {
649 ret = -ENOMEM;
650 goto error_free_event_interfaces;
651 }
652
653 for (i = 0; i < dev_info->num_interrupt_lines; i++) {
654 dev_info->event_interfaces[i].owner = dev_info->driver_module;
847ec80b
JC
655
656 snprintf(dev_info->event_interfaces[i]._name, 20,
ba5c6fba
JC
657 "%s:event%d",
658 dev_name(&dev_info->dev),
3d550fba 659 i);
847ec80b
JC
660
661 ret = iio_setup_ev_int(&dev_info->event_interfaces[i],
662 (const char *)(dev_info
663 ->event_interfaces[i]
664 ._name),
665 dev_info->driver_module,
666 &dev_info->dev);
667 if (ret) {
668 dev_err(&dev_info->dev,
669 "Could not get chrdev interface\n");
847ec80b
JC
670 goto error_free_setup_ev_ints;
671 }
847ec80b 672
5cba220b
JC
673 dev_set_drvdata(&dev_info->event_interfaces[i].dev,
674 (void *)dev_info);
675 ret = sysfs_create_group(&dev_info
676 ->event_interfaces[i]
677 .dev.kobj,
678 &dev_info->event_attrs[i]);
679
847ec80b
JC
680 if (ret) {
681 dev_err(&dev_info->dev,
682 "Failed to register sysfs for event attrs");
683 goto error_remove_sysfs_interfaces;
684 }
685 }
686
687 for (i = 0; i < dev_info->num_interrupt_lines; i++) {
688 ret = __iio_add_event_config_attrs(dev_info, i);
689 if (ret)
690 goto error_unregister_config_attrs;
691 }
692
693 return 0;
694
695error_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;
699error_remove_sysfs_interfaces:
700 for (j = 0; j < i; j++)
5cba220b
JC
701 sysfs_remove_group(&dev_info
702 ->event_interfaces[j].dev.kobj,
847ec80b 703 &dev_info->event_attrs[j]);
847ec80b 704error_free_setup_ev_ints:
3d550fba 705 for (j = 0; j < i; j++)
847ec80b 706 iio_free_ev_int(&dev_info->event_interfaces[j]);
847ec80b
JC
707 kfree(dev_info->interrupts);
708error_free_event_interfaces:
709 kfree(dev_info->event_interfaces);
710error_ret:
711
712 return ret;
713}
714
715static void iio_device_unregister_eventset(struct iio_dev *dev_info)
716{
717 int i;
718
719 if (dev_info->num_interrupt_lines == 0)
720 return;
721 for (i = 0; i < dev_info->num_interrupt_lines; i++)
5cba220b
JC
722 sysfs_remove_group(&dev_info
723 ->event_interfaces[i].dev.kobj,
847ec80b
JC
724 &dev_info->event_attrs[i]);
725
3d550fba 726 for (i = 0; i < dev_info->num_interrupt_lines; i++)
847ec80b 727 iio_free_ev_int(&dev_info->event_interfaces[i]);
847ec80b
JC
728 kfree(dev_info->interrupts);
729 kfree(dev_info->event_interfaces);
730}
731
732static void iio_dev_release(struct device *device)
733{
734 struct iio_dev *dev = to_iio_dev(device);
735
736 iio_put();
737 kfree(dev);
738}
739
740static struct device_type iio_dev_type = {
741 .name = "iio_device",
742 .release = iio_dev_release,
743};
744
745struct iio_dev *iio_allocate_device(void)
746{
747 struct iio_dev *dev = kzalloc(sizeof *dev, GFP_KERNEL);
748
749 if (dev) {
750 dev->dev.type = &iio_dev_type;
5aaaeba8 751 dev->dev.bus = &iio_bus_type;
847ec80b
JC
752 device_initialize(&dev->dev);
753 dev_set_drvdata(&dev->dev, (void *)dev);
754 mutex_init(&dev->mlock);
755 iio_get();
756 }
757
758 return dev;
759}
760EXPORT_SYMBOL(iio_allocate_device);
761
762void iio_free_device(struct iio_dev *dev)
763{
764 if (dev)
765 iio_put_device(dev);
766}
767EXPORT_SYMBOL(iio_free_device);
768
769int iio_device_register(struct iio_dev *dev_info)
770{
771 int ret;
772
773 ret = iio_device_register_id(dev_info, &iio_idr);
774 if (ret) {
775 dev_err(&dev_info->dev, "Failed to get id\n");
776 goto error_ret;
777 }
778 dev_set_name(&dev_info->dev, "device%d", dev_info->id);
779
780 ret = device_add(&dev_info->dev);
781 if (ret)
782 goto error_free_idr;
783 ret = iio_device_register_sysfs(dev_info);
784 if (ret) {
785 dev_err(dev_info->dev.parent,
786 "Failed to register sysfs interfaces\n");
787 goto error_del_device;
788 }
789 ret = iio_device_register_eventset(dev_info);
790 if (ret) {
791 dev_err(dev_info->dev.parent,
c849d253 792 "Failed to register event set\n");
847ec80b
JC
793 goto error_free_sysfs;
794 }
795 if (dev_info->modes & INDIO_RING_TRIGGERED)
796 iio_device_register_trigger_consumer(dev_info);
797
798 return 0;
799
800error_free_sysfs:
801 iio_device_unregister_sysfs(dev_info);
802error_del_device:
803 device_del(&dev_info->dev);
804error_free_idr:
805 iio_device_unregister_id(dev_info);
806error_ret:
807 return ret;
808}
809EXPORT_SYMBOL(iio_device_register);
810
811void iio_device_unregister(struct iio_dev *dev_info)
812{
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);
819}
820EXPORT_SYMBOL(iio_device_unregister);
821
822void iio_put(void)
823{
824 module_put(THIS_MODULE);
825}
826
827void iio_get(void)
828{
829 __module_get(THIS_MODULE);
830}
831
832subsys_initcall(iio_init);
833module_exit(iio_exit);
834
835MODULE_AUTHOR("Jonathan Cameron <jic23@cam.ac.uk>");
836MODULE_DESCRIPTION("Industrial I/O core");
837MODULE_LICENSE("GPL");