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