upload tizen1.0 source
[kernel/linux-2.6.36.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 /* IDR to allocate character device minor numbers */
34 static DEFINE_IDR(iio_chrdev_idr);
35 /* Lock used to protect both of the above */
36 static DEFINE_SPINLOCK(iio_idr_lock);
37
38 dev_t iio_devt;
39 EXPORT_SYMBOL(iio_devt);
40
41 #define IIO_DEV_MAX 256
42 struct bus_type iio_bus_type = {
43         .name = "iio",
44 };
45 EXPORT_SYMBOL(iio_bus_type);
46
47 void __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 }
54 EXPORT_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)) {
74                 if (ev_int->current_events == ev_int->max_events) {
75                         mutex_unlock(&ev_int->event_list_lock);
76                         return 0;
77                 }
78                 ev = kmalloc(sizeof(*ev), GFP_KERNEL);
79                 if (ev == NULL) {
80                         ret = -ENOMEM;
81                         mutex_unlock(&ev_int->event_list_lock);
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
97 error_ret:
98         return ret;
99 }
100 EXPORT_SYMBOL(__iio_push_event);
101
102 int 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 }
110 EXPORT_SYMBOL(iio_push_event);
111
112 /* Generic interrupt line interrupt handler */
113 static irqreturn_t iio_interrupt_handler(int irq, void *_int_info)
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
146 static 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.*/
157 int 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
184 error_ret:
185         return ret;
186 }
187 EXPORT_SYMBOL(iio_register_interrupt_line);
188
189 /* This turns up an awful lot */
190 ssize_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 }
196 EXPORT_SYMBOL(iio_read_const_attr);
197
198 /* Before this runs the interrupt generator must have been disabled */
199 void iio_unregister_interrupt_line(struct iio_dev *dev_info, int line_number)
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 }
207 EXPORT_SYMBOL(iio_unregister_interrupt_line);
208
209 /* Reference counted add and remove */
210 void 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 }
227 EXPORT_SYMBOL(iio_add_event_to_list);
228
229 void 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 }
245 EXPORT_SYMBOL(iio_remove_event_from_list);
246
247 static ssize_t iio_event_chrdev_read(struct file *filep,
248                                      char __user *buf,
249                                      size_t count,
250                                      loff_t *f_ps)
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
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.
291          */
292         if (el->shared_pointer) {
293                 spin_lock(&el->shared_pointer->lock);
294                 (el->shared_pointer->ev_p) = NULL;
295                 spin_unlock(&el->shared_pointer->lock);
296         }
297         kfree(el);
298
299         return len;
300
301 error_mutex_unlock:
302         mutex_unlock(&ev_int->event_list_lock);
303 error_ret:
304
305         return ret;
306 }
307
308 static int iio_event_chrdev_release(struct inode *inode, struct file *filep)
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
330 static int iio_event_chrdev_open(struct inode *inode, struct file *filep)
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
347 static const struct file_operations iio_event_chrdev_fileops = {
348         .read =  iio_event_chrdev_read,
349         .release = iio_event_chrdev_release,
350         .open = iio_event_chrdev_open,
351         .owner = THIS_MODULE,
352 };
353
354 static void iio_event_dev_release(struct device *dev)
355 {
356         struct iio_event_interface *ev_int
357                 = container_of(dev, struct iio_event_interface, dev);
358         cdev_del(&ev_int->handler.chrdev);
359         iio_device_free_chrdev_minor(MINOR(dev->devt));
360 };
361
362 static struct device_type iio_event_type = {
363         .release = iio_event_dev_release,
364 };
365
366 int iio_device_get_chrdev_minor(void)
367 {
368         int ret, val;
369
370 idr_again:
371         if (unlikely(idr_pre_get(&iio_chrdev_idr, GFP_KERNEL) == 0))
372                 return -ENOMEM;
373         spin_lock(&iio_idr_lock);
374         ret = idr_get_new(&iio_chrdev_idr, NULL, &val);
375         spin_unlock(&iio_idr_lock);
376         if (unlikely(ret == -EAGAIN))
377                 goto idr_again;
378         else if (unlikely(ret))
379                 return ret;
380         if (val > IIO_DEV_MAX)
381                 return -ENOMEM;
382         return val;
383 }
384
385 void iio_device_free_chrdev_minor(int val)
386 {
387         spin_lock(&iio_idr_lock);
388         idr_remove(&iio_chrdev_idr, val);
389         spin_unlock(&iio_idr_lock);
390 }
391
392 int iio_setup_ev_int(struct iio_event_interface *ev_int,
393                      const char *name,
394                      struct module *owner,
395                      struct device *dev)
396 {
397         int ret, minor;
398
399         ev_int->dev.bus = &iio_bus_type;
400         ev_int->dev.parent = dev;
401         ev_int->dev.type = &iio_event_type;
402         device_initialize(&ev_int->dev);
403
404         minor = iio_device_get_chrdev_minor();
405         if (minor < 0) {
406                 ret = minor;
407                 goto error_device_put;
408         }
409         ev_int->dev.devt = MKDEV(MAJOR(iio_devt), minor);
410         dev_set_name(&ev_int->dev, "%s", name);
411
412         ret = device_add(&ev_int->dev);
413         if (ret)
414                 goto error_free_minor;
415
416         cdev_init(&ev_int->handler.chrdev, &iio_event_chrdev_fileops);
417         ev_int->handler.chrdev.owner = owner;
418
419         mutex_init(&ev_int->event_list_lock);
420         /* discussion point - make this variable? */
421         ev_int->max_events = 10;
422         ev_int->current_events = 0;
423         INIT_LIST_HEAD(&ev_int->det_events.list);
424         init_waitqueue_head(&ev_int->wait);
425         ev_int->handler.private = ev_int;
426         ev_int->handler.flags = 0;
427
428         ret = cdev_add(&ev_int->handler.chrdev, ev_int->dev.devt, 1);
429         if (ret)
430                 goto error_unreg_device;
431
432         return 0;
433
434 error_unreg_device:
435         device_unregister(&ev_int->dev);
436 error_free_minor:
437         iio_device_free_chrdev_minor(minor);
438 error_device_put:
439         put_device(&ev_int->dev);
440
441         return ret;
442 }
443
444 void iio_free_ev_int(struct iio_event_interface *ev_int)
445 {
446         device_unregister(&ev_int->dev);
447         put_device(&ev_int->dev);
448 }
449
450 static int __init iio_dev_init(void)
451 {
452         int err;
453
454         err = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio");
455         if (err < 0)
456                 printk(KERN_ERR "%s: failed to allocate char dev region\n",
457                        __FILE__);
458
459         return err;
460 }
461
462 static void __exit iio_dev_exit(void)
463 {
464         if (iio_devt)
465                 unregister_chrdev_region(iio_devt, IIO_DEV_MAX);
466 }
467
468 static int __init iio_init(void)
469 {
470         int ret;
471
472         /* Register sysfs bus */
473         ret  = bus_register(&iio_bus_type);
474         if (ret < 0) {
475                 printk(KERN_ERR
476                        "%s could not register bus type\n",
477                         __FILE__);
478                 goto error_nothing;
479         }
480
481         ret = iio_dev_init();
482         if (ret < 0)
483                 goto error_unregister_bus_type;
484
485         return 0;
486
487 error_unregister_bus_type:
488         bus_unregister(&iio_bus_type);
489 error_nothing:
490         return ret;
491 }
492
493 static void __exit iio_exit(void)
494 {
495         iio_dev_exit();
496         bus_unregister(&iio_bus_type);
497 }
498
499 static int iio_device_register_sysfs(struct iio_dev *dev_info)
500 {
501         int ret = 0;
502
503         ret = sysfs_create_group(&dev_info->dev.kobj, dev_info->attrs);
504         if (ret) {
505                 dev_err(dev_info->dev.parent,
506                         "Failed to register sysfs hooks\n");
507                 goto error_ret;
508         }
509
510         if (dev_info->scan_el_attrs) {
511                 ret = sysfs_create_group(&dev_info->dev.kobj,
512                                          dev_info->scan_el_attrs);
513                 if (ret)
514                         dev_err(&dev_info->dev,
515                                 "Failed to add sysfs scan els\n");
516         }
517
518 error_ret:
519         return ret;
520 }
521
522 static void iio_device_unregister_sysfs(struct iio_dev *dev_info)
523 {
524         if (dev_info->scan_el_attrs)
525                 sysfs_remove_group(&dev_info->dev.kobj,
526                                    dev_info->scan_el_attrs);
527
528         sysfs_remove_group(&dev_info->dev.kobj, dev_info->attrs);
529 }
530
531 /* Return a negative errno on failure */
532 int iio_get_new_idr_val(struct idr *this_idr)
533 {
534         int ret;
535         int val;
536
537 idr_again:
538         if (unlikely(idr_pre_get(this_idr, GFP_KERNEL) == 0))
539                 return -ENOMEM;
540
541         spin_lock(&iio_idr_lock);
542         ret = idr_get_new(this_idr, NULL, &val);
543         spin_unlock(&iio_idr_lock);
544         if (unlikely(ret == -EAGAIN))
545                 goto idr_again;
546         else if (unlikely(ret))
547                 return ret;
548
549         return val;
550 }
551 EXPORT_SYMBOL(iio_get_new_idr_val);
552
553 void iio_free_idr_val(struct idr *this_idr, int id)
554 {
555         spin_lock(&iio_idr_lock);
556         idr_remove(this_idr, id);
557         spin_unlock(&iio_idr_lock);
558 }
559 EXPORT_SYMBOL(iio_free_idr_val);
560
561 static int iio_device_register_id(struct iio_dev *dev_info,
562                                   struct idr *this_idr)
563 {
564
565         dev_info->id = iio_get_new_idr_val(&iio_idr);
566         if (dev_info->id < 0)
567                 return dev_info->id;
568         return 0;
569 }
570
571 static void iio_device_unregister_id(struct iio_dev *dev_info)
572 {
573         iio_free_idr_val(&iio_idr, dev_info->id);
574 }
575
576 static inline int __iio_add_event_config_attrs(struct iio_dev *dev_info, int i)
577 {
578         int ret;
579         /*p for adding, q for removing */
580         struct attribute **attrp, **attrq;
581
582         if (dev_info->event_conf_attrs && dev_info->event_conf_attrs[i].attrs) {
583                 attrp = dev_info->event_conf_attrs[i].attrs;
584                 while (*attrp) {
585                         ret =  sysfs_add_file_to_group(&dev_info->dev.kobj,
586                                                        *attrp,
587                                                        dev_info
588                                                        ->event_attrs[i].name);
589                         if (ret)
590                                 goto error_ret;
591                         attrp++;
592                 }
593         }
594         return 0;
595
596 error_ret:
597         attrq = dev_info->event_conf_attrs[i].attrs;
598         while (attrq != attrp) {
599                         sysfs_remove_file_from_group(&dev_info->dev.kobj,
600                                              *attrq,
601                                              dev_info->event_attrs[i].name);
602                 attrq++;
603         }
604
605         return ret;
606 }
607
608 static inline int __iio_remove_event_config_attrs(struct iio_dev *dev_info,
609                                                   int i)
610 {
611         struct attribute **attrq;
612
613         if (dev_info->event_conf_attrs
614                 && dev_info->event_conf_attrs[i].attrs) {
615                 attrq = dev_info->event_conf_attrs[i].attrs;
616                 while (*attrq) {
617                         sysfs_remove_file_from_group(&dev_info->dev.kobj,
618                                                      *attrq,
619                                                      dev_info
620                                                      ->event_attrs[i].name);
621                         attrq++;
622                 }
623         }
624
625         return 0;
626 }
627
628 static int iio_device_register_eventset(struct iio_dev *dev_info)
629 {
630         int ret = 0, i, j;
631
632         if (dev_info->num_interrupt_lines == 0)
633                 return 0;
634
635         dev_info->event_interfaces =
636                 kzalloc(sizeof(struct iio_event_interface)
637                         *dev_info->num_interrupt_lines,
638                         GFP_KERNEL);
639         if (dev_info->event_interfaces == NULL) {
640                 ret = -ENOMEM;
641                 goto error_ret;
642         }
643
644         dev_info->interrupts = kzalloc(sizeof(struct iio_interrupt *)
645                                        *dev_info->num_interrupt_lines,
646                                        GFP_KERNEL);
647         if (dev_info->interrupts == NULL) {
648                 ret = -ENOMEM;
649                 goto error_free_event_interfaces;
650         }
651
652         for (i = 0; i < dev_info->num_interrupt_lines; i++) {
653                 dev_info->event_interfaces[i].owner = dev_info->driver_module;
654
655                 snprintf(dev_info->event_interfaces[i]._name, 20,
656                          "%s:event%d",
657                          dev_name(&dev_info->dev),
658                          i);
659
660                 ret = iio_setup_ev_int(&dev_info->event_interfaces[i],
661                                        (const char *)(dev_info
662                                                       ->event_interfaces[i]
663                                                       ._name),
664                                        dev_info->driver_module,
665                                        &dev_info->dev);
666                 if (ret) {
667                         dev_err(&dev_info->dev,
668                                 "Could not get chrdev interface\n");
669                         goto error_free_setup_ev_ints;
670                 }
671
672                 dev_set_drvdata(&dev_info->event_interfaces[i].dev,
673                                 (void *)dev_info);
674                 ret = sysfs_create_group(&dev_info
675                                         ->event_interfaces[i]
676                                         .dev.kobj,
677                                         &dev_info->event_attrs[i]);
678
679                 if (ret) {
680                         dev_err(&dev_info->dev,
681                                 "Failed to register sysfs for event attrs");
682                         goto error_remove_sysfs_interfaces;
683                 }
684         }
685
686         for (i = 0; i < dev_info->num_interrupt_lines; i++) {
687                 ret = __iio_add_event_config_attrs(dev_info, i);
688                 if (ret)
689                         goto error_unregister_config_attrs;
690         }
691
692         return 0;
693
694 error_unregister_config_attrs:
695         for (j = 0; j < i; j++)
696                 __iio_remove_event_config_attrs(dev_info, i);
697         i = dev_info->num_interrupt_lines - 1;
698 error_remove_sysfs_interfaces:
699         for (j = 0; j < i; j++)
700                 sysfs_remove_group(&dev_info
701                                    ->event_interfaces[j].dev.kobj,
702                                    &dev_info->event_attrs[j]);
703 error_free_setup_ev_ints:
704         for (j = 0; j < i; j++)
705                 iio_free_ev_int(&dev_info->event_interfaces[j]);
706         kfree(dev_info->interrupts);
707 error_free_event_interfaces:
708         kfree(dev_info->event_interfaces);
709 error_ret:
710
711         return ret;
712 }
713
714 static void iio_device_unregister_eventset(struct iio_dev *dev_info)
715 {
716         int i;
717
718         if (dev_info->num_interrupt_lines == 0)
719                 return;
720         for (i = 0; i < dev_info->num_interrupt_lines; i++)
721                 sysfs_remove_group(&dev_info
722                                    ->event_interfaces[i].dev.kobj,
723                                    &dev_info->event_attrs[i]);
724
725         for (i = 0; i < dev_info->num_interrupt_lines; i++)
726                 iio_free_ev_int(&dev_info->event_interfaces[i]);
727         kfree(dev_info->interrupts);
728         kfree(dev_info->event_interfaces);
729 }
730
731 static void iio_dev_release(struct device *device)
732 {
733         struct iio_dev *dev = to_iio_dev(device);
734
735         iio_put();
736         kfree(dev);
737 }
738
739 static struct device_type iio_dev_type = {
740         .name = "iio_device",
741         .release = iio_dev_release,
742 };
743
744 struct iio_dev *iio_allocate_device(void)
745 {
746         struct iio_dev *dev = kzalloc(sizeof *dev, GFP_KERNEL);
747
748         if (dev) {
749                 dev->dev.type = &iio_dev_type;
750                 dev->dev.bus = &iio_bus_type;
751                 device_initialize(&dev->dev);
752                 dev_set_drvdata(&dev->dev, (void *)dev);
753                 mutex_init(&dev->mlock);
754                 iio_get();
755         }
756
757         return dev;
758 }
759 EXPORT_SYMBOL(iio_allocate_device);
760
761 void iio_free_device(struct iio_dev *dev)
762 {
763         if (dev)
764                 iio_put_device(dev);
765 }
766 EXPORT_SYMBOL(iio_free_device);
767
768 int iio_device_register(struct iio_dev *dev_info)
769 {
770         int ret;
771
772         ret = iio_device_register_id(dev_info, &iio_idr);
773         if (ret) {
774                 dev_err(&dev_info->dev, "Failed to get id\n");
775                 goto error_ret;
776         }
777         dev_set_name(&dev_info->dev, "device%d", dev_info->id);
778
779         ret = device_add(&dev_info->dev);
780         if (ret)
781                 goto error_free_idr;
782         ret = iio_device_register_sysfs(dev_info);
783         if (ret) {
784                 dev_err(dev_info->dev.parent,
785                         "Failed to register sysfs interfaces\n");
786                 goto error_del_device;
787         }
788         ret = iio_device_register_eventset(dev_info);
789         if (ret) {
790                 dev_err(dev_info->dev.parent,
791                         "Failed to register event set\n");
792                 goto error_free_sysfs;
793         }
794         if (dev_info->modes & INDIO_RING_TRIGGERED)
795                 iio_device_register_trigger_consumer(dev_info);
796
797         return 0;
798
799 error_free_sysfs:
800         iio_device_unregister_sysfs(dev_info);
801 error_del_device:
802         device_del(&dev_info->dev);
803 error_free_idr:
804         iio_device_unregister_id(dev_info);
805 error_ret:
806         return ret;
807 }
808 EXPORT_SYMBOL(iio_device_register);
809
810 void iio_device_unregister(struct iio_dev *dev_info)
811 {
812         if (dev_info->modes & INDIO_RING_TRIGGERED)
813                 iio_device_unregister_trigger_consumer(dev_info);
814         iio_device_unregister_eventset(dev_info);
815         iio_device_unregister_sysfs(dev_info);
816         iio_device_unregister_id(dev_info);
817         device_unregister(&dev_info->dev);
818 }
819 EXPORT_SYMBOL(iio_device_unregister);
820
821 void iio_put(void)
822 {
823         module_put(THIS_MODULE);
824 }
825
826 void iio_get(void)
827 {
828         __module_get(THIS_MODULE);
829 }
830
831 subsys_initcall(iio_init);
832 module_exit(iio_exit);
833
834 MODULE_AUTHOR("Jonathan Cameron <jic23@cam.ac.uk>");
835 MODULE_DESCRIPTION("Industrial I/O core");
836 MODULE_LICENSE("GPL");