Merge branch 'omap-boards' into omap-for-linus
[pandora-kernel.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 <linux/slab.h>
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*/
32 static DEFINE_IDR(iio_idr);
33
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);
40
41 dev_t iio_devt;
42 EXPORT_SYMBOL(iio_devt);
43
44 #define IIO_DEV_MAX 256
45 static char *iio_devnode(struct device *dev, mode_t *mode)
46 {
47         return kasprintf(GFP_KERNEL, "iio/%s", dev_name(dev));
48 }
49
50 struct class iio_class = {
51         .name = "iio",
52         .devnode = iio_devnode,
53 };
54 EXPORT_SYMBOL(iio_class);
55
56 void __iio_change_event(struct iio_detected_event_list *ev,
57                         int ev_code,
58                         s64 timestamp)
59 {
60         ev->ev.id = ev_code;
61         ev->ev.timestamp = timestamp;
62 }
63 EXPORT_SYMBOL(__iio_change_event);
64
65 /* Used both in the interrupt line put events and the ring buffer ones */
66
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
69  * switched on.
70  */
71  int __iio_push_event(struct iio_event_interface *ev_int,
72                      int ev_code,
73                      s64 timestamp,
74                      struct iio_shared_ev_pointer *
75                      shared_pointer_p)
76 {
77         struct iio_detected_event_list *ev;
78         int ret = 0;
79
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);
85                         return 0;
86                 }
87                 ev = kmalloc(sizeof(*ev), GFP_KERNEL);
88                 if (ev == NULL) {
89                         ret = -ENOMEM;
90                         mutex_unlock(&ev_int->event_list_lock);
91                         goto error_ret;
92                 }
93                 ev->ev.id = ev_code;
94                 ev->ev.timestamp = timestamp;
95                 ev->shared_pointer = shared_pointer_p;
96                 if (ev->shared_pointer)
97                         shared_pointer_p->ev_p = ev;
98
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);
103         } else
104                 mutex_unlock(&ev_int->event_list_lock);
105
106 error_ret:
107         return ret;
108 }
109 EXPORT_SYMBOL(__iio_push_event);
110
111 int iio_push_event(struct iio_dev *dev_info,
112                    int ev_line,
113                    int ev_code,
114                    s64 timestamp)
115 {
116         return __iio_push_event(&dev_info->event_interfaces[ev_line],
117                                 ev_code, timestamp, NULL);
118 }
119 EXPORT_SYMBOL(iio_push_event);
120
121 /* Generic interrupt line interrupt handler */
122 static irqreturn_t iio_interrupt_handler(int irq, void *_int_info)
123 {
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;
127         s64 time_ns;
128         unsigned long flags;
129
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);
133                 return IRQ_NONE;
134         }
135
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,
142                                      list);
143                 /* single event handler - maybe shared */
144                 p->handler(dev_info, 1, time_ns, !(p->refcount > 1));
145         } else
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);
149                 }
150         spin_unlock_irqrestore(&int_info->ev_list_lock, flags);
151
152         return IRQ_HANDLED;
153 }
154
155 static struct iio_interrupt *iio_allocate_interrupt(void)
156 {
157         struct iio_interrupt *i = kmalloc(sizeof *i, GFP_KERNEL);
158         if (i) {
159                 spin_lock_init(&i->ev_list_lock);
160                 INIT_LIST_HEAD(&i->ev_list);
161         }
162         return i;
163 }
164
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,
168                                 int line_number,
169                                 unsigned long type,
170                                 const char *name)
171 {
172         int ret;
173
174         dev_info->interrupts[line_number] = iio_allocate_interrupt();
175         if (dev_info->interrupts[line_number] == NULL) {
176                 ret = -ENOMEM;
177                 goto error_ret;
178         }
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;
182
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,
189                           type,
190                           name,
191                           dev_info->interrupts[line_number]);
192
193 error_ret:
194         return ret;
195 }
196 EXPORT_SYMBOL(iio_register_interrupt_line);
197
198 /* This turns up an awful lot */
199 ssize_t iio_read_const_attr(struct device *dev,
200                             struct device_attribute *attr,
201                             char *buf)
202 {
203         return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string);
204 }
205 EXPORT_SYMBOL(iio_read_const_attr);
206
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)
209 {
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]);
215 }
216 EXPORT_SYMBOL(iio_unregister_interrupt_line);
217
218 /* Reference counted add and remove */
219 void iio_add_event_to_list(struct iio_event_handler_list *el,
220                           struct list_head *head)
221 {
222         unsigned long flags;
223         struct iio_interrupt *inter = to_iio_interrupt(head);
224
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);
232         }
233         el->refcount++;
234         mutex_unlock(&el->exist_lock);
235 }
236 EXPORT_SYMBOL(iio_add_event_to_list);
237
238 void iio_remove_event_from_list(struct iio_event_handler_list *el,
239                                struct list_head *head)
240 {
241         unsigned long flags;
242         struct iio_interrupt *inter = to_iio_interrupt(head);
243
244         mutex_lock(&el->exist_lock);
245         el->refcount--;
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);
251         }
252         mutex_unlock(&el->exist_lock);
253 }
254 EXPORT_SYMBOL(iio_remove_event_from_list);
255
256 static ssize_t iio_event_chrdev_read(struct file *filep,
257                                      char __user *buf,
258                                      size_t count,
259                                      loff_t *f_ps)
260 {
261         struct iio_event_interface *ev_int = filep->private_data;
262         struct iio_detected_event_list *el;
263         int ret;
264         size_t len;
265
266         mutex_lock(&ev_int->event_list_lock);
267         if (list_empty(&ev_int->det_events.list)) {
268                 if (filep->f_flags & O_NONBLOCK) {
269                         ret = -EAGAIN;
270                         goto error_mutex_unlock;
271                 }
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,
275                                                !list_empty(&ev_int
276                                                            ->det_events.list));
277                 if (ret)
278                         goto error_ret;
279                 /* Single access device so noone else can get the data */
280                 mutex_lock(&ev_int->event_list_lock);
281         }
282
283         el = list_first_entry(&ev_int->det_events.list,
284                               struct iio_detected_event_list,
285                               list);
286         len = sizeof el->ev;
287         if (copy_to_user(buf, &(el->ev), len)) {
288                 ret = -EFAULT;
289                 goto error_mutex_unlock;
290         }
291         list_del(&el->list);
292         ev_int->current_events--;
293         mutex_unlock(&ev_int->event_list_lock);
294         /*
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.
300          */
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);
305         }
306         kfree(el);
307
308         return len;
309
310 error_mutex_unlock:
311         mutex_unlock(&ev_int->event_list_lock);
312 error_ret:
313
314         return ret;
315 }
316
317 static int iio_event_chrdev_release(struct inode *inode, struct file *filep)
318 {
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;
322
323         mutex_lock(&ev_int->event_list_lock);
324         clear_bit(IIO_BUSY_BIT_POS, &ev_int->handler.flags);
325         /*
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.
329          */
330         list_for_each_entry_safe(el, t, &ev_int->det_events.list, list) {
331                 list_del(&el->list);
332                 kfree(el);
333         }
334         mutex_unlock(&ev_int->event_list_lock);
335
336         return 0;
337 }
338
339 static int iio_event_chrdev_open(struct inode *inode, struct file *filep)
340 {
341         struct iio_handler *hand = iio_cdev_to_handler(inode->i_cdev);
342         struct iio_event_interface *ev_int = hand->private;
343
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);
348                 return -EBUSY;
349         }
350         filep->private_data = hand->private;
351         mutex_unlock(&ev_int->event_list_lock);
352
353         return 0;
354 }
355
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,
361 };
362
363 static void iio_event_dev_release(struct device *dev)
364 {
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));
369 };
370
371 static struct device_type iio_event_type = {
372         .release = iio_event_dev_release,
373 };
374
375 int iio_device_get_chrdev_minor(void)
376 {
377         int ret, val;
378
379 idr_again:
380         if (unlikely(idr_pre_get(&iio_chrdev_idr, GFP_KERNEL) == 0))
381                 return -ENOMEM;
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))
386                 goto idr_again;
387         else if (unlikely(ret))
388                 return ret;
389         if (val > IIO_DEV_MAX)
390                 return -ENOMEM;
391         return val;
392 }
393
394 void iio_device_free_chrdev_minor(int val)
395 {
396         spin_lock(&iio_idr_lock);
397         idr_remove(&iio_chrdev_idr, val);
398         spin_unlock(&iio_idr_lock);
399 }
400
401 int iio_setup_ev_int(struct iio_event_interface *ev_int,
402                      const char *name,
403                      struct module *owner,
404                      struct device *dev)
405 {
406         int ret, minor;
407
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);
412
413         minor = iio_device_get_chrdev_minor();
414         if (minor < 0) {
415                 ret = minor;
416                 goto error_device_put;
417         }
418         ev_int->dev.devt = MKDEV(MAJOR(iio_devt), minor);
419         dev_set_name(&ev_int->dev, "%s", name);
420
421         ret = device_add(&ev_int->dev);
422         if (ret)
423                 goto error_free_minor;
424
425         cdev_init(&ev_int->handler.chrdev, &iio_event_chrdev_fileops);
426         ev_int->handler.chrdev.owner = owner;
427
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;
436
437         ret = cdev_add(&ev_int->handler.chrdev, ev_int->dev.devt, 1);
438         if (ret)
439                 goto error_unreg_device;
440
441         return 0;
442
443 error_unreg_device:
444         device_unregister(&ev_int->dev);
445 error_free_minor:
446         iio_device_free_chrdev_minor(minor);
447 error_device_put:
448         put_device(&ev_int->dev);
449
450         return ret;
451 }
452
453 void iio_free_ev_int(struct iio_event_interface *ev_int)
454 {
455         device_unregister(&ev_int->dev);
456         put_device(&ev_int->dev);
457 }
458
459 static int __init iio_dev_init(void)
460 {
461         int err;
462
463         err = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio");
464         if (err < 0)
465                 printk(KERN_ERR "%s: failed to allocate char dev region\n",
466                        __FILE__);
467
468         return err;
469 }
470
471 static void __exit iio_dev_exit(void)
472 {
473         if (iio_devt)
474                 unregister_chrdev_region(iio_devt, IIO_DEV_MAX);
475 }
476
477 static int __init iio_init(void)
478 {
479         int ret;
480
481         /* Create sysfs class */
482         ret  = class_register(&iio_class);
483         if (ret < 0) {
484                 printk(KERN_ERR
485                        "%s could not create sysfs class\n",
486                         __FILE__);
487                 goto error_nothing;
488         }
489
490         ret = iio_dev_init();
491         if (ret < 0)
492                 goto error_unregister_class;
493
494         return 0;
495
496 error_unregister_class:
497         class_unregister(&iio_class);
498 error_nothing:
499         return ret;
500 }
501
502 static void __exit iio_exit(void)
503 {
504         iio_dev_exit();
505         class_unregister(&iio_class);
506 }
507
508 static int iio_device_register_sysfs(struct iio_dev *dev_info)
509 {
510         int ret = 0;
511
512         ret = sysfs_create_group(&dev_info->dev.kobj, dev_info->attrs);
513         if (ret) {
514                 dev_err(dev_info->dev.parent,
515                         "Failed to register sysfs hooks\n");
516                 goto error_ret;
517         }
518
519         if (dev_info->scan_el_attrs) {
520                 ret = sysfs_create_group(&dev_info->dev.kobj,
521                                          dev_info->scan_el_attrs);
522                 if (ret)
523                         dev_err(&dev_info->dev,
524                                 "Failed to add sysfs scan els\n");
525         }
526
527 error_ret:
528         return ret;
529 }
530
531 static void iio_device_unregister_sysfs(struct iio_dev *dev_info)
532 {
533         if (dev_info->scan_el_attrs)
534                 sysfs_remove_group(&dev_info->dev.kobj,
535                                    dev_info->scan_el_attrs);
536
537         sysfs_remove_group(&dev_info->dev.kobj, dev_info->attrs);
538 }
539
540 /* Return a negative errno on failure */
541 int iio_get_new_idr_val(struct idr *this_idr)
542 {
543         int ret;
544         int val;
545
546 idr_again:
547         if (unlikely(idr_pre_get(this_idr, GFP_KERNEL) == 0))
548                 return -ENOMEM;
549
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))
554                 goto idr_again;
555         else if (unlikely(ret))
556                 return ret;
557
558         return val;
559 }
560 EXPORT_SYMBOL(iio_get_new_idr_val);
561
562 void iio_free_idr_val(struct idr *this_idr, int id)
563 {
564         spin_lock(&iio_idr_lock);
565         idr_remove(this_idr, id);
566         spin_unlock(&iio_idr_lock);
567 }
568 EXPORT_SYMBOL(iio_free_idr_val);
569
570 static int iio_device_register_id(struct iio_dev *dev_info,
571                                   struct idr *this_idr)
572 {
573
574         dev_info->id = iio_get_new_idr_val(&iio_idr);
575         if (dev_info->id < 0)
576                 return dev_info->id;
577         return 0;
578 }
579
580 static void iio_device_unregister_id(struct iio_dev *dev_info)
581 {
582         iio_free_idr_val(&iio_idr, dev_info->id);
583 }
584
585 static inline int __iio_add_event_config_attrs(struct iio_dev *dev_info, int i)
586 {
587         int ret;
588         /*p for adding, q for removing */
589         struct attribute **attrp, **attrq;
590
591         if (dev_info->event_conf_attrs && dev_info->event_conf_attrs[i].attrs) {
592                 attrp = dev_info->event_conf_attrs[i].attrs;
593                 while (*attrp) {
594                         ret =  sysfs_add_file_to_group(&dev_info->dev.kobj,
595                                                        *attrp,
596                                                        dev_info
597                                                        ->event_attrs[i].name);
598                         if (ret)
599                                 goto error_ret;
600                         attrp++;
601                 }
602         }
603         return 0;
604
605 error_ret:
606         attrq = dev_info->event_conf_attrs[i].attrs;
607         while (attrq != attrp) {
608                         sysfs_remove_file_from_group(&dev_info->dev.kobj,
609                                              *attrq,
610                                              dev_info->event_attrs[i].name);
611                 attrq++;
612         }
613
614         return ret;
615 }
616
617 static inline int __iio_remove_event_config_attrs(struct iio_dev *dev_info,
618                                                   int i)
619 {
620         struct attribute **attrq;
621
622         if (dev_info->event_conf_attrs
623                 && dev_info->event_conf_attrs[i].attrs) {
624                 attrq = dev_info->event_conf_attrs[i].attrs;
625                 while (*attrq) {
626                         sysfs_remove_file_from_group(&dev_info->dev.kobj,
627                                                      *attrq,
628                                                      dev_info
629                                                      ->event_attrs[i].name);
630                         attrq++;
631                 }
632         }
633
634         return 0;
635 }
636
637 static int iio_device_register_eventset(struct iio_dev *dev_info)
638 {
639         int ret = 0, i, j;
640
641         if (dev_info->num_interrupt_lines == 0)
642                 return 0;
643
644         dev_info->event_interfaces =
645                 kzalloc(sizeof(struct iio_event_interface)
646                         *dev_info->num_interrupt_lines,
647                         GFP_KERNEL);
648         if (dev_info->event_interfaces == NULL) {
649                 ret = -ENOMEM;
650                 goto error_ret;
651         }
652
653         dev_info->interrupts = kzalloc(sizeof(struct iio_interrupt *)
654                                        *dev_info->num_interrupt_lines,
655                                        GFP_KERNEL);
656         if (dev_info->interrupts == NULL) {
657                 ret = -ENOMEM;
658                 goto error_free_event_interfaces;
659         }
660
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);
664                 if (ret < 0)
665                         goto error_free_setup_ev_ints;
666                 else
667                         dev_info->event_interfaces[i].id = ret;
668
669                 snprintf(dev_info->event_interfaces[i]._name, 20,
670                          "event_line%d",
671                         dev_info->event_interfaces[i].id);
672
673                 ret = iio_setup_ev_int(&dev_info->event_interfaces[i],
674                                        (const char *)(dev_info
675                                                       ->event_interfaces[i]
676                                                       ._name),
677                                        dev_info->driver_module,
678                                        &dev_info->dev);
679                 if (ret) {
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;
685                 }
686         }
687
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
692                         = (const char *)
693                         (dev_info->event_interfaces[i]._attrname);
694                 ret = sysfs_create_group(&dev_info->dev.kobj,
695                                          &dev_info->event_attrs[i]);
696                 if (ret) {
697                         dev_err(&dev_info->dev,
698                                 "Failed to register sysfs for event attrs");
699                         goto error_remove_sysfs_interfaces;
700                 }
701         }
702
703         for (i = 0; i < dev_info->num_interrupt_lines; i++) {
704                 ret = __iio_add_event_config_attrs(dev_info, i);
705                 if (ret)
706                         goto error_unregister_config_attrs;
707         }
708
709         return 0;
710
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]);
725         }
726         kfree(dev_info->interrupts);
727 error_free_event_interfaces:
728         kfree(dev_info->event_interfaces);
729 error_ret:
730
731         return ret;
732 }
733
734 static void iio_device_unregister_eventset(struct iio_dev *dev_info)
735 {
736         int i;
737
738         if (dev_info->num_interrupt_lines == 0)
739                 return;
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]);
743
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]);
748         }
749         kfree(dev_info->interrupts);
750         kfree(dev_info->event_interfaces);
751 }
752
753 static void iio_dev_release(struct device *device)
754 {
755         struct iio_dev *dev = to_iio_dev(device);
756
757         iio_put();
758         kfree(dev);
759 }
760
761 static struct device_type iio_dev_type = {
762         .name = "iio_device",
763         .release = iio_dev_release,
764 };
765
766 struct iio_dev *iio_allocate_device(void)
767 {
768         struct iio_dev *dev = kzalloc(sizeof *dev, GFP_KERNEL);
769
770         if (dev) {
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);
776                 iio_get();
777         }
778
779         return dev;
780 }
781 EXPORT_SYMBOL(iio_allocate_device);
782
783 void iio_free_device(struct iio_dev *dev)
784 {
785         if (dev)
786                 iio_put_device(dev);
787 }
788 EXPORT_SYMBOL(iio_free_device);
789
790 int iio_device_register(struct iio_dev *dev_info)
791 {
792         int ret;
793
794         ret = iio_device_register_id(dev_info, &iio_idr);
795         if (ret) {
796                 dev_err(&dev_info->dev, "Failed to get id\n");
797                 goto error_ret;
798         }
799         dev_set_name(&dev_info->dev, "device%d", dev_info->id);
800
801         ret = device_add(&dev_info->dev);
802         if (ret)
803                 goto error_free_idr;
804         ret = iio_device_register_sysfs(dev_info);
805         if (ret) {
806                 dev_err(dev_info->dev.parent,
807                         "Failed to register sysfs interfaces\n");
808                 goto error_del_device;
809         }
810         ret = iio_device_register_eventset(dev_info);
811         if (ret) {
812                 dev_err(dev_info->dev.parent,
813                         "Failed to register event set \n");
814                 goto error_free_sysfs;
815         }
816         if (dev_info->modes & INDIO_RING_TRIGGERED)
817                 iio_device_register_trigger_consumer(dev_info);
818
819         return 0;
820
821 error_free_sysfs:
822         iio_device_unregister_sysfs(dev_info);
823 error_del_device:
824         device_del(&dev_info->dev);
825 error_free_idr:
826         iio_device_unregister_id(dev_info);
827 error_ret:
828         return ret;
829 }
830 EXPORT_SYMBOL(iio_device_register);
831
832 void iio_device_unregister(struct iio_dev *dev_info)
833 {
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);
840 }
841 EXPORT_SYMBOL(iio_device_unregister);
842
843 void iio_put(void)
844 {
845         module_put(THIS_MODULE);
846 }
847
848 void iio_get(void)
849 {
850         __module_get(THIS_MODULE);
851 }
852
853 subsys_initcall(iio_init);
854 module_exit(iio_exit);
855
856 MODULE_AUTHOR("Jonathan Cameron <jic23@cam.ac.uk>");
857 MODULE_DESCRIPTION("Industrial I/O core");
858 MODULE_LICENSE("GPL");