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