]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/staging/iio/industrialio-core.c
b456dfc8fe27b008387bce803db0df9cd9aaa0c6
[mv-sheeva.git] / drivers / staging / iio / industrialio-core.c
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>
21 #include <linux/sched.h>
22 #include <linux/wait.h>
23 #include <linux/cdev.h>
24 #include "iio.h"
25 #include "trigger_consumer.h"
26
27 #define IIO_ID_PREFIX "device"
28 #define IIO_ID_FORMAT IIO_ID_PREFIX "%d"
29
30 /* IDR to assign each registered device a unique id*/
31 static DEFINE_IDR(iio_idr);
32
33 /* IDR for general event identifiers */
34 static DEFINE_IDR(iio_event_idr);
35 /* IDR to allocate character device minor numbers */
36 static DEFINE_IDR(iio_chrdev_idr);
37 /* Lock used to protect both of the above */
38 static DEFINE_SPINLOCK(iio_idr_lock);
39
40 dev_t iio_devt;
41 EXPORT_SYMBOL(iio_devt);
42
43 #define IIO_DEV_MAX 256
44 static char *iio_devnode(struct device *dev, mode_t *mode)
45 {
46         return kasprintf(GFP_KERNEL, "iio/%s", dev_name(dev));
47 }
48
49 struct class iio_class = {
50         .name = "iio",
51         .devnode = iio_devnode,
52 };
53 EXPORT_SYMBOL(iio_class);
54
55 void __iio_change_event(struct iio_detected_event_list *ev,
56                         int ev_code,
57                         s64 timestamp)
58 {
59         ev->ev.id = ev_code;
60         ev->ev.timestamp = timestamp;
61 }
62 EXPORT_SYMBOL(__iio_change_event);
63
64 /* Used both in the interrupt line put events and the ring buffer ones */
65
66 /* Note that in it's current form someone has to be listening before events
67  * are queued. Hence a client MUST open the chrdev before the ring buffer is
68  * switched on.
69  */
70  int __iio_push_event(struct iio_event_interface *ev_int,
71                      int ev_code,
72                      s64 timestamp,
73                      struct iio_shared_ev_pointer *
74                      shared_pointer_p)
75 {
76         struct iio_detected_event_list *ev;
77         int ret = 0;
78
79         /* Does anyone care? */
80         mutex_lock(&ev_int->event_list_lock);
81         if (test_bit(IIO_BUSY_BIT_POS, &ev_int->handler.flags)) {
82                 if (ev_int->current_events == ev_int->max_events) {
83                         mutex_unlock(&ev_int->event_list_lock);
84                         return 0;
85                 }
86                 ev = kmalloc(sizeof(*ev), GFP_KERNEL);
87                 if (ev == NULL) {
88                         ret = -ENOMEM;
89                         mutex_unlock(&ev_int->event_list_lock);
90                         goto error_ret;
91                 }
92                 ev->ev.id = ev_code;
93                 ev->ev.timestamp = timestamp;
94                 ev->shared_pointer = shared_pointer_p;
95                 if (ev->shared_pointer)
96                         shared_pointer_p->ev_p = ev;
97
98                 list_add_tail(&ev->list, &ev_int->det_events.list);
99                 ev_int->current_events++;
100                 mutex_unlock(&ev_int->event_list_lock);
101                 wake_up_interruptible(&ev_int->wait);
102         } else
103                 mutex_unlock(&ev_int->event_list_lock);
104
105 error_ret:
106         return ret;
107 }
108 EXPORT_SYMBOL(__iio_push_event);
109
110 int iio_push_event(struct iio_dev *dev_info,
111                    int ev_line,
112                    int ev_code,
113                    s64 timestamp)
114 {
115         return __iio_push_event(&dev_info->event_interfaces[ev_line],
116                                 ev_code, timestamp, NULL);
117 }
118 EXPORT_SYMBOL(iio_push_event);
119
120 /* Generic interrupt line interrupt handler */
121 static irqreturn_t iio_interrupt_handler(int irq, void *_int_info)
122 {
123         struct iio_interrupt *int_info = _int_info;
124         struct iio_dev *dev_info = int_info->dev_info;
125         struct iio_event_handler_list *p;
126         s64 time_ns;
127         unsigned long flags;
128
129         spin_lock_irqsave(&int_info->ev_list_lock, flags);
130         if (list_empty(&int_info->ev_list)) {
131                 spin_unlock_irqrestore(&int_info->ev_list_lock, flags);
132                 return IRQ_NONE;
133         }
134
135         time_ns = iio_get_time_ns();
136         /* detect single element list*/
137         if (list_is_singular(&int_info->ev_list)) {
138                 disable_irq_nosync(irq);
139                 p = list_first_entry(&int_info->ev_list,
140                                      struct iio_event_handler_list,
141                                      list);
142                 /* single event handler - maybe shared */
143                 p->handler(dev_info, 1, time_ns, !(p->refcount > 1));
144         } else
145                 list_for_each_entry(p, &int_info->ev_list, list) {
146                         disable_irq_nosync(irq);
147                         p->handler(dev_info, 1, time_ns, 0);
148                 }
149         spin_unlock_irqrestore(&int_info->ev_list_lock, flags);
150
151         return IRQ_HANDLED;
152 }
153
154 static struct iio_interrupt *iio_allocate_interrupt(void)
155 {
156         struct iio_interrupt *i = kmalloc(sizeof *i, GFP_KERNEL);
157         if (i) {
158                 spin_lock_init(&i->ev_list_lock);
159                 INIT_LIST_HEAD(&i->ev_list);
160         }
161         return i;
162 }
163
164 /* Confirming the validity of supplied irq is left to drivers.*/
165 int iio_register_interrupt_line(unsigned int irq,
166                                 struct iio_dev *dev_info,
167                                 int line_number,
168                                 unsigned long type,
169                                 const char *name)
170 {
171         int ret;
172
173         dev_info->interrupts[line_number] = iio_allocate_interrupt();
174         if (dev_info->interrupts[line_number] == NULL) {
175                 ret = -ENOMEM;
176                 goto error_ret;
177         }
178         dev_info->interrupts[line_number]->line_number = line_number;
179         dev_info->interrupts[line_number]->irq = irq;
180         dev_info->interrupts[line_number]->dev_info = dev_info;
181
182         /* Possibly only request on demand?
183          * Can see this may complicate the handling of interrupts.
184          * However, with this approach we might end up handling lots of
185          * events no-one cares about.*/
186         ret = request_irq(irq,
187                           &iio_interrupt_handler,
188                           type,
189                           name,
190                           dev_info->interrupts[line_number]);
191
192 error_ret:
193         return ret;
194 }
195 EXPORT_SYMBOL(iio_register_interrupt_line);
196
197 /* This turns up an awful lot */
198 ssize_t iio_read_const_attr(struct device *dev,
199                             struct device_attribute *attr,
200                             char *buf)
201 {
202         return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string);
203 }
204 EXPORT_SYMBOL(iio_read_const_attr);
205
206 /* Before this runs the interrupt generator must have been disabled */
207 void iio_unregister_interrupt_line(struct iio_dev *dev_info, int line_number)
208 {
209         /* make sure the interrupt handlers are all done */
210         flush_scheduled_work();
211         free_irq(dev_info->interrupts[line_number]->irq,
212                  dev_info->interrupts[line_number]);
213         kfree(dev_info->interrupts[line_number]);
214 }
215 EXPORT_SYMBOL(iio_unregister_interrupt_line);
216
217 /* Reference counted add and remove */
218 void iio_add_event_to_list(struct iio_event_handler_list *el,
219                           struct list_head *head)
220 {
221         unsigned long flags;
222         struct iio_interrupt *inter = to_iio_interrupt(head);
223
224         /* take mutex to protect this element */
225         mutex_lock(&el->exist_lock);
226         if (el->refcount == 0) {
227                 /* Take the event list spin lock */
228                 spin_lock_irqsave(&inter->ev_list_lock, flags);
229                 list_add(&el->list, head);
230                 spin_unlock_irqrestore(&inter->ev_list_lock, flags);
231         }
232         el->refcount++;
233         mutex_unlock(&el->exist_lock);
234 }
235 EXPORT_SYMBOL(iio_add_event_to_list);
236
237 void iio_remove_event_from_list(struct iio_event_handler_list *el,
238                                struct list_head *head)
239 {
240         unsigned long flags;
241         struct iio_interrupt *inter = to_iio_interrupt(head);
242
243         mutex_lock(&el->exist_lock);
244         el->refcount--;
245         if (el->refcount == 0) {
246                 /* Take the event list spin lock */
247                 spin_lock_irqsave(&inter->ev_list_lock, flags);
248                 list_del_init(&el->list);
249                 spin_unlock_irqrestore(&inter->ev_list_lock, flags);
250         }
251         mutex_unlock(&el->exist_lock);
252 }
253 EXPORT_SYMBOL(iio_remove_event_from_list);
254
255 static ssize_t iio_event_chrdev_read(struct file *filep,
256                                      char __user *buf,
257                                      size_t count,
258                                      loff_t *f_ps)
259 {
260         struct iio_event_interface *ev_int = filep->private_data;
261         struct iio_detected_event_list *el;
262         int ret;
263         size_t len;
264
265         mutex_lock(&ev_int->event_list_lock);
266         if (list_empty(&ev_int->det_events.list)) {
267                 if (filep->f_flags & O_NONBLOCK) {
268                         ret = -EAGAIN;
269                         goto error_mutex_unlock;
270                 }
271                 mutex_unlock(&ev_int->event_list_lock);
272                 /* Blocking on device; waiting for something to be there */
273                 ret = wait_event_interruptible(ev_int->wait,
274                                                !list_empty(&ev_int
275                                                            ->det_events.list));
276                 if (ret)
277                         goto error_ret;
278                 /* Single access device so noone else can get the data */
279                 mutex_lock(&ev_int->event_list_lock);
280         }
281
282         el = list_first_entry(&ev_int->det_events.list,
283                               struct iio_detected_event_list,
284                               list);
285         len = sizeof el->ev;
286         if (copy_to_user(buf, &(el->ev), len)) {
287                 ret = -EFAULT;
288                 goto error_mutex_unlock;
289         }
290         list_del(&el->list);
291         ev_int->current_events--;
292         mutex_unlock(&ev_int->event_list_lock);
293         /*
294          * Possible concurency issue if an update of this event is on its way
295          * through. May lead to new event being removed whilst the reported
296          * event was the unescalated event. In typical use case this is not a
297          * problem as userspace will say read half the buffer due to a 50%
298          * full event which would make the correct 100% full incorrect anyway.
299          */
300         if (el->shared_pointer) {
301                 spin_lock(&el->shared_pointer->lock);
302                 (el->shared_pointer->ev_p) = NULL;
303                 spin_unlock(&el->shared_pointer->lock);
304         }
305         kfree(el);
306
307         return len;
308
309 error_mutex_unlock:
310         mutex_unlock(&ev_int->event_list_lock);
311 error_ret:
312
313         return ret;
314 }
315
316 static int iio_event_chrdev_release(struct inode *inode, struct file *filep)
317 {
318         struct iio_handler *hand = iio_cdev_to_handler(inode->i_cdev);
319         struct iio_event_interface *ev_int = hand->private;
320         struct iio_detected_event_list *el, *t;
321
322         mutex_lock(&ev_int->event_list_lock);
323         clear_bit(IIO_BUSY_BIT_POS, &ev_int->handler.flags);
324         /*
325          * In order to maintain a clean state for reopening,
326          * clear out any awaiting events. The mask will prevent
327          * any new __iio_push_event calls running.
328          */
329         list_for_each_entry_safe(el, t, &ev_int->det_events.list, list) {
330                 list_del(&el->list);
331                 kfree(el);
332         }
333         mutex_unlock(&ev_int->event_list_lock);
334
335         return 0;
336 }
337
338 static int iio_event_chrdev_open(struct inode *inode, struct file *filep)
339 {
340         struct iio_handler *hand = iio_cdev_to_handler(inode->i_cdev);
341         struct iio_event_interface *ev_int = hand->private;
342
343         mutex_lock(&ev_int->event_list_lock);
344         if (test_and_set_bit(IIO_BUSY_BIT_POS, &hand->flags)) {
345                 fops_put(filep->f_op);
346                 mutex_unlock(&ev_int->event_list_lock);
347                 return -EBUSY;
348         }
349         filep->private_data = hand->private;
350         mutex_unlock(&ev_int->event_list_lock);
351
352         return 0;
353 }
354
355 static const struct file_operations iio_event_chrdev_fileops = {
356         .read =  iio_event_chrdev_read,
357         .release = iio_event_chrdev_release,
358         .open = iio_event_chrdev_open,
359         .owner = THIS_MODULE,
360 };
361
362 static void iio_event_dev_release(struct device *dev)
363 {
364         struct iio_event_interface *ev_int
365                 = container_of(dev, struct iio_event_interface, dev);
366         cdev_del(&ev_int->handler.chrdev);
367         iio_device_free_chrdev_minor(MINOR(dev->devt));
368 };
369
370 static struct device_type iio_event_type = {
371         .release = iio_event_dev_release,
372 };
373
374 int iio_device_get_chrdev_minor(void)
375 {
376         int ret, val;
377
378 idr_again:
379         if (unlikely(idr_pre_get(&iio_chrdev_idr, GFP_KERNEL) == 0))
380                 return -ENOMEM;
381         spin_lock(&iio_idr_lock);
382         ret = idr_get_new(&iio_chrdev_idr, NULL, &val);
383         spin_unlock(&iio_idr_lock);
384         if (unlikely(ret == -EAGAIN))
385                 goto idr_again;
386         else if (unlikely(ret))
387                 return ret;
388         if (val > IIO_DEV_MAX)
389                 return -ENOMEM;
390         return val;
391 }
392
393 void iio_device_free_chrdev_minor(int val)
394 {
395         spin_lock(&iio_idr_lock);
396         idr_remove(&iio_chrdev_idr, val);
397         spin_unlock(&iio_idr_lock);
398 }
399
400 int iio_setup_ev_int(struct iio_event_interface *ev_int,
401                      const char *name,
402                      struct module *owner,
403                      struct device *dev)
404 {
405         int ret, minor;
406
407         ev_int->dev.class = &iio_class;
408         ev_int->dev.parent = dev;
409         ev_int->dev.type = &iio_event_type;
410         device_initialize(&ev_int->dev);
411
412         minor = iio_device_get_chrdev_minor();
413         if (minor < 0) {
414                 ret = minor;
415                 goto error_device_put;
416         }
417         ev_int->dev.devt = MKDEV(MAJOR(iio_devt), minor);
418         dev_set_name(&ev_int->dev, "%s", name);
419
420         ret = device_add(&ev_int->dev);
421         if (ret)
422                 goto error_free_minor;
423
424         cdev_init(&ev_int->handler.chrdev, &iio_event_chrdev_fileops);
425         ev_int->handler.chrdev.owner = owner;
426
427         mutex_init(&ev_int->event_list_lock);
428         /* discussion point - make this variable? */
429         ev_int->max_events = 10;
430         ev_int->current_events = 0;
431         INIT_LIST_HEAD(&ev_int->det_events.list);
432         init_waitqueue_head(&ev_int->wait);
433         ev_int->handler.private = ev_int;
434         ev_int->handler.flags = 0;
435
436         ret = cdev_add(&ev_int->handler.chrdev, ev_int->dev.devt, 1);
437         if (ret)
438                 goto error_unreg_device;
439
440         return 0;
441
442 error_unreg_device:
443         device_unregister(&ev_int->dev);
444 error_free_minor:
445         iio_device_free_chrdev_minor(minor);
446 error_device_put:
447         put_device(&ev_int->dev);
448
449         return ret;
450 }
451
452 void iio_free_ev_int(struct iio_event_interface *ev_int)
453 {
454         device_unregister(&ev_int->dev);
455         put_device(&ev_int->dev);
456 }
457
458 static int __init iio_dev_init(void)
459 {
460         int err;
461
462         err = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio");
463         if (err < 0)
464                 printk(KERN_ERR "%s: failed to allocate char dev region\n",
465                        __FILE__);
466
467         return err;
468 }
469
470 static void __exit iio_dev_exit(void)
471 {
472         if (iio_devt)
473                 unregister_chrdev_region(iio_devt, IIO_DEV_MAX);
474 }
475
476 static int __init iio_init(void)
477 {
478         int ret;
479
480         /* Create sysfs class */
481         ret  = class_register(&iio_class);
482         if (ret < 0) {
483                 printk(KERN_ERR
484                        "%s could not create sysfs class\n",
485                         __FILE__);
486                 goto error_nothing;
487         }
488
489         ret = iio_dev_init();
490         if (ret < 0)
491                 goto error_unregister_class;
492
493         return 0;
494
495 error_unregister_class:
496         class_unregister(&iio_class);
497 error_nothing:
498         return ret;
499 }
500
501 static void __exit iio_exit(void)
502 {
503         iio_dev_exit();
504         class_unregister(&iio_class);
505 }
506
507 static int iio_device_register_sysfs(struct iio_dev *dev_info)
508 {
509         int ret = 0;
510
511         ret = sysfs_create_group(&dev_info->dev.kobj, dev_info->attrs);
512         if (ret) {
513                 dev_err(dev_info->dev.parent,
514                         "Failed to register sysfs hooks\n");
515                 goto error_ret;
516         }
517
518         if (dev_info->scan_el_attrs) {
519                 ret = sysfs_create_group(&dev_info->dev.kobj,
520                                          dev_info->scan_el_attrs);
521                 if (ret)
522                         dev_err(&dev_info->dev,
523                                 "Failed to add sysfs scan els\n");
524         }
525
526 error_ret:
527         return ret;
528 }
529
530 static void iio_device_unregister_sysfs(struct iio_dev *dev_info)
531 {
532         if (dev_info->scan_el_attrs)
533                 sysfs_remove_group(&dev_info->dev.kobj,
534                                    dev_info->scan_el_attrs);
535
536         sysfs_remove_group(&dev_info->dev.kobj, dev_info->attrs);
537 }
538
539 int iio_get_new_idr_val(struct idr *this_idr)
540 {
541         int ret;
542         int val;
543
544 idr_again:
545         if (unlikely(idr_pre_get(this_idr, GFP_KERNEL) == 0))
546                 return -ENOMEM;
547
548         spin_lock(&iio_idr_lock);
549         ret = idr_get_new(this_idr, NULL, &val);
550         spin_unlock(&iio_idr_lock);
551         if (unlikely(ret == -EAGAIN))
552                 goto idr_again;
553         else if (unlikely(ret))
554                 return ret;
555
556         return val;
557 }
558 EXPORT_SYMBOL(iio_get_new_idr_val);
559
560 void iio_free_idr_val(struct idr *this_idr, int id)
561 {
562         spin_lock(&iio_idr_lock);
563         idr_remove(this_idr, id);
564         spin_unlock(&iio_idr_lock);
565 }
566 EXPORT_SYMBOL(iio_free_idr_val);
567
568 static int iio_device_register_id(struct iio_dev *dev_info,
569                                   struct idr *this_idr)
570 {
571
572         dev_info->id = iio_get_new_idr_val(&iio_idr);
573         if (dev_info->id < 0)
574                 return dev_info->id;
575         return 0;
576 }
577
578 static void iio_device_unregister_id(struct iio_dev *dev_info)
579 {
580         iio_free_idr_val(&iio_idr, dev_info->id);
581 }
582
583 static inline int __iio_add_event_config_attrs(struct iio_dev *dev_info, int i)
584 {
585         int ret;
586         /*p for adding, q for removing */
587         struct attribute **attrp, **attrq;
588
589         if (dev_info->event_conf_attrs && dev_info->event_conf_attrs[i].attrs) {
590                 attrp = dev_info->event_conf_attrs[i].attrs;
591                 while (*attrp) {
592                         ret =  sysfs_add_file_to_group(&dev_info->dev.kobj,
593                                                        *attrp,
594                                                        dev_info
595                                                        ->event_attrs[i].name);
596                         if (ret)
597                                 goto error_ret;
598                         attrp++;
599                 }
600         }
601         return 0;
602
603 error_ret:
604         attrq = dev_info->event_conf_attrs[i].attrs;
605         while (attrq != attrp) {
606                         sysfs_remove_file_from_group(&dev_info->dev.kobj,
607                                              *attrq,
608                                              dev_info->event_attrs[i].name);
609                 attrq++;
610         }
611
612         return ret;
613 }
614
615 static inline int __iio_remove_event_config_attrs(struct iio_dev *dev_info,
616                                                   int i)
617 {
618         struct attribute **attrq;
619
620         if (dev_info->event_conf_attrs
621                 && dev_info->event_conf_attrs[i].attrs) {
622                 attrq = dev_info->event_conf_attrs[i].attrs;
623                 while (*attrq) {
624                         sysfs_remove_file_from_group(&dev_info->dev.kobj,
625                                                      *attrq,
626                                                      dev_info
627                                                      ->event_attrs[i].name);
628                         attrq++;
629                 }
630         }
631
632         return 0;
633 }
634
635 static int iio_device_register_eventset(struct iio_dev *dev_info)
636 {
637         int ret = 0, i, j;
638
639         if (dev_info->num_interrupt_lines == 0)
640                 return 0;
641
642         dev_info->event_interfaces =
643                 kzalloc(sizeof(struct iio_event_interface)
644                         *dev_info->num_interrupt_lines,
645                         GFP_KERNEL);
646         if (dev_info->event_interfaces == NULL) {
647                 ret = -ENOMEM;
648                 goto error_ret;
649         }
650
651         dev_info->interrupts = kzalloc(sizeof(struct iio_interrupt *)
652                                        *dev_info->num_interrupt_lines,
653                                        GFP_KERNEL);
654         if (dev_info->interrupts == NULL) {
655                 ret = -ENOMEM;
656                 goto error_free_event_interfaces;
657         }
658
659         for (i = 0; i < dev_info->num_interrupt_lines; i++) {
660                 dev_info->event_interfaces[i].owner = dev_info->driver_module;
661                 ret = iio_get_new_idr_val(&iio_event_idr);
662                 if (ret)
663                         goto error_free_setup_ev_ints;
664                 else
665                         dev_info->event_interfaces[i].id = ret;
666
667                 snprintf(dev_info->event_interfaces[i]._name, 20,
668                          "event_line%d",
669                         dev_info->event_interfaces[i].id);
670
671                 ret = iio_setup_ev_int(&dev_info->event_interfaces[i],
672                                        (const char *)(dev_info
673                                                       ->event_interfaces[i]
674                                                       ._name),
675                                        dev_info->driver_module,
676                                        &dev_info->dev);
677                 if (ret) {
678                         dev_err(&dev_info->dev,
679                                 "Could not get chrdev interface\n");
680                         iio_free_idr_val(&iio_event_idr,
681                                          dev_info->event_interfaces[i].id);
682                         goto error_free_setup_ev_ints;
683                 }
684         }
685
686         for (i = 0; i < dev_info->num_interrupt_lines; i++) {
687                 snprintf(dev_info->event_interfaces[i]._attrname, 20,
688                         "event_line%d_sources", i);
689                 dev_info->event_attrs[i].name
690                         = (const char *)
691                         (dev_info->event_interfaces[i]._attrname);
692                 ret = sysfs_create_group(&dev_info->dev.kobj,
693                                          &dev_info->event_attrs[i]);
694                 if (ret) {
695                         dev_err(&dev_info->dev,
696                                 "Failed to register sysfs for event attrs");
697                         goto error_remove_sysfs_interfaces;
698                 }
699         }
700
701         for (i = 0; i < dev_info->num_interrupt_lines; i++) {
702                 ret = __iio_add_event_config_attrs(dev_info, i);
703                 if (ret)
704                         goto error_unregister_config_attrs;
705         }
706
707         return 0;
708
709 error_unregister_config_attrs:
710         for (j = 0; j < i; j++)
711                 __iio_remove_event_config_attrs(dev_info, i);
712         i = dev_info->num_interrupt_lines - 1;
713 error_remove_sysfs_interfaces:
714         for (j = 0; j < i; j++)
715                 sysfs_remove_group(&dev_info->dev.kobj,
716                                    &dev_info->event_attrs[j]);
717         i = dev_info->num_interrupt_lines - 1;
718 error_free_setup_ev_ints:
719         for (j = 0; j < i; j++) {
720                 iio_free_idr_val(&iio_event_idr,
721                                  dev_info->event_interfaces[i].id);
722                 iio_free_ev_int(&dev_info->event_interfaces[j]);
723         }
724         kfree(dev_info->interrupts);
725 error_free_event_interfaces:
726         kfree(dev_info->event_interfaces);
727 error_ret:
728
729         return ret;
730 }
731
732 static void iio_device_unregister_eventset(struct iio_dev *dev_info)
733 {
734         int i;
735
736         if (dev_info->num_interrupt_lines == 0)
737                 return;
738         for (i = 0; i < dev_info->num_interrupt_lines; i++)
739                 sysfs_remove_group(&dev_info->dev.kobj,
740                                    &dev_info->event_attrs[i]);
741
742         for (i = 0; i < dev_info->num_interrupt_lines; i++) {
743                 iio_free_idr_val(&iio_event_idr,
744                                  dev_info->event_interfaces[i].id);
745                 iio_free_ev_int(&dev_info->event_interfaces[i]);
746         }
747         kfree(dev_info->interrupts);
748         kfree(dev_info->event_interfaces);
749 }
750
751 static void iio_dev_release(struct device *device)
752 {
753         struct iio_dev *dev = to_iio_dev(device);
754
755         iio_put();
756         kfree(dev);
757 }
758
759 static struct device_type iio_dev_type = {
760         .name = "iio_device",
761         .release = iio_dev_release,
762 };
763
764 struct iio_dev *iio_allocate_device(void)
765 {
766         struct iio_dev *dev = kzalloc(sizeof *dev, GFP_KERNEL);
767
768         if (dev) {
769                 dev->dev.type = &iio_dev_type;
770                 dev->dev.class = &iio_class;
771                 device_initialize(&dev->dev);
772                 dev_set_drvdata(&dev->dev, (void *)dev);
773                 mutex_init(&dev->mlock);
774                 iio_get();
775         }
776
777         return dev;
778 }
779 EXPORT_SYMBOL(iio_allocate_device);
780
781 void iio_free_device(struct iio_dev *dev)
782 {
783         if (dev)
784                 iio_put_device(dev);
785 }
786 EXPORT_SYMBOL(iio_free_device);
787
788 int iio_device_register(struct iio_dev *dev_info)
789 {
790         int ret;
791
792         ret = iio_device_register_id(dev_info, &iio_idr);
793         if (ret) {
794                 dev_err(&dev_info->dev, "Failed to get id\n");
795                 goto error_ret;
796         }
797         dev_set_name(&dev_info->dev, "device%d", dev_info->id);
798
799         ret = device_add(&dev_info->dev);
800         if (ret)
801                 goto error_free_idr;
802         ret = iio_device_register_sysfs(dev_info);
803         if (ret) {
804                 dev_err(dev_info->dev.parent,
805                         "Failed to register sysfs interfaces\n");
806                 goto error_del_device;
807         }
808         ret = iio_device_register_eventset(dev_info);
809         if (ret) {
810                 dev_err(dev_info->dev.parent,
811                         "Failed to register event set \n");
812                 goto error_free_sysfs;
813         }
814         if (dev_info->modes & INDIO_RING_TRIGGERED)
815                 iio_device_register_trigger_consumer(dev_info);
816
817         return 0;
818
819 error_free_sysfs:
820         iio_device_unregister_sysfs(dev_info);
821 error_del_device:
822         device_del(&dev_info->dev);
823 error_free_idr:
824         iio_device_unregister_id(dev_info);
825 error_ret:
826         return ret;
827 }
828 EXPORT_SYMBOL(iio_device_register);
829
830 void iio_device_unregister(struct iio_dev *dev_info)
831 {
832         if (dev_info->modes & INDIO_RING_TRIGGERED)
833                 iio_device_unregister_trigger_consumer(dev_info);
834         iio_device_unregister_eventset(dev_info);
835         iio_device_unregister_sysfs(dev_info);
836         iio_device_unregister_id(dev_info);
837         device_unregister(&dev_info->dev);
838 }
839 EXPORT_SYMBOL(iio_device_unregister);
840
841 void iio_put(void)
842 {
843         module_put(THIS_MODULE);
844 }
845
846 void iio_get(void)
847 {
848         __module_get(THIS_MODULE);
849 }
850
851 subsys_initcall(iio_init);
852 module_exit(iio_exit);
853
854 MODULE_AUTHOR("Jonathan Cameron <jic23@cam.ac.uk>");
855 MODULE_DESCRIPTION("Industrial I/O core");
856 MODULE_LICENSE("GPL");