staging:iio: remove ability to escalate events.
[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_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 static const char * const iio_chan_type_name_spec_shared[] = {
48         [IIO_TIMESTAMP] = "timestamp",
49         [IIO_ACCEL] = "accel",
50         [IIO_IN] = "in",
51         [IIO_IN_DIFF] = "in-in",
52         [IIO_GYRO] = "gyro",
53         [IIO_TEMP] = "temp",
54         [IIO_MAGN] = "magn",
55         [IIO_INCLI] = "incli",
56         [IIO_ROT] = "rot",
57         [IIO_INTENSITY] = "intensity",
58         [IIO_LIGHT] = "illuminance",
59         [IIO_ANGL] = "angl",
60 };
61
62 static const char * const iio_chan_type_name_spec_complex[] = {
63         [IIO_IN_DIFF] = "in%d-in%d",
64 };
65
66 static const char * const iio_modifier_names_light[] = {
67         [IIO_MOD_LIGHT_BOTH] = "both",
68         [IIO_MOD_LIGHT_IR] = "ir",
69 };
70
71 static const char * const iio_modifier_names_axial[] = {
72         [IIO_MOD_X] = "x",
73         [IIO_MOD_Y] = "y",
74         [IIO_MOD_Z] = "z",
75 };
76
77 /* relies on pairs of these shared then separate */
78 static const char * const iio_chan_info_postfix[] = {
79         [IIO_CHAN_INFO_SCALE_SHARED/2] = "scale",
80         [IIO_CHAN_INFO_OFFSET_SHARED/2] = "offset",
81         [IIO_CHAN_INFO_CALIBSCALE_SHARED/2] = "calibscale",
82         [IIO_CHAN_INFO_CALIBBIAS_SHARED/2] = "calibbias",
83 };
84
85 /* Used both in the interrupt line put events and the ring buffer ones */
86
87 /* Note that in it's current form someone has to be listening before events
88  * are queued. Hence a client MUST open the chrdev before the ring buffer is
89  * switched on.
90  */
91 int __iio_push_event(struct iio_event_interface *ev_int,
92                      int ev_code,
93                      s64 timestamp)
94 {
95         struct iio_detected_event_list *ev;
96         int ret = 0;
97
98         /* Does anyone care? */
99         mutex_lock(&ev_int->event_list_lock);
100         if (test_bit(IIO_BUSY_BIT_POS, &ev_int->handler.flags)) {
101                 if (ev_int->current_events == ev_int->max_events) {
102                         mutex_unlock(&ev_int->event_list_lock);
103                         return 0;
104                 }
105                 ev = kmalloc(sizeof(*ev), GFP_KERNEL);
106                 if (ev == NULL) {
107                         ret = -ENOMEM;
108                         mutex_unlock(&ev_int->event_list_lock);
109                         goto error_ret;
110                 }
111                 ev->ev.id = ev_code;
112                 ev->ev.timestamp = timestamp;
113
114                 list_add_tail(&ev->list, &ev_int->det_events.list);
115                 ev_int->current_events++;
116                 mutex_unlock(&ev_int->event_list_lock);
117                 wake_up_interruptible(&ev_int->wait);
118         } else
119                 mutex_unlock(&ev_int->event_list_lock);
120
121 error_ret:
122         return ret;
123 }
124 EXPORT_SYMBOL(__iio_push_event);
125
126 int iio_push_event(struct iio_dev *dev_info,
127                    int ev_line,
128                    int ev_code,
129                    s64 timestamp)
130 {
131         return __iio_push_event(&dev_info->event_interfaces[ev_line],
132                                 ev_code, timestamp);
133 }
134 EXPORT_SYMBOL(iio_push_event);
135
136 /* Generic interrupt line interrupt handler */
137 static irqreturn_t iio_interrupt_handler(int irq, void *_int_info)
138 {
139         struct iio_interrupt *int_info = _int_info;
140         struct iio_dev *dev_info = int_info->dev_info;
141         struct iio_event_handler_list *p;
142         s64 time_ns;
143         unsigned long flags;
144
145         spin_lock_irqsave(&int_info->ev_list_lock, flags);
146         if (list_empty(&int_info->ev_list)) {
147                 spin_unlock_irqrestore(&int_info->ev_list_lock, flags);
148                 return IRQ_NONE;
149         }
150
151         time_ns = iio_get_time_ns();
152         list_for_each_entry(p, &int_info->ev_list, list) {
153                 disable_irq_nosync(irq);
154                 p->handler(dev_info, 1, time_ns, !(p->refcount > 1));
155         }
156         spin_unlock_irqrestore(&int_info->ev_list_lock, flags);
157
158         return IRQ_HANDLED;
159 }
160
161 static struct iio_interrupt *iio_allocate_interrupt(void)
162 {
163         struct iio_interrupt *i = kmalloc(sizeof *i, GFP_KERNEL);
164         if (i) {
165                 spin_lock_init(&i->ev_list_lock);
166                 INIT_LIST_HEAD(&i->ev_list);
167         }
168         return i;
169 }
170
171 /* Confirming the validity of supplied irq is left to drivers.*/
172 int iio_register_interrupt_line(unsigned int irq,
173                                 struct iio_dev *dev_info,
174                                 int line_number,
175                                 unsigned long type,
176                                 const char *name)
177 {
178         int ret;
179
180         dev_info->interrupts[line_number] = iio_allocate_interrupt();
181         if (dev_info->interrupts[line_number] == NULL) {
182                 ret = -ENOMEM;
183                 goto error_ret;
184         }
185         dev_info->interrupts[line_number]->line_number = line_number;
186         dev_info->interrupts[line_number]->irq = irq;
187         dev_info->interrupts[line_number]->dev_info = dev_info;
188
189         /* Possibly only request on demand?
190          * Can see this may complicate the handling of interrupts.
191          * However, with this approach we might end up handling lots of
192          * events no-one cares about.*/
193         ret = request_irq(irq,
194                           &iio_interrupt_handler,
195                           type,
196                           name,
197                           dev_info->interrupts[line_number]);
198
199 error_ret:
200         return ret;
201 }
202 EXPORT_SYMBOL(iio_register_interrupt_line);
203
204 /* This turns up an awful lot */
205 ssize_t iio_read_const_attr(struct device *dev,
206                             struct device_attribute *attr,
207                             char *buf)
208 {
209         return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string);
210 }
211 EXPORT_SYMBOL(iio_read_const_attr);
212
213 /* Before this runs the interrupt generator must have been disabled */
214 void iio_unregister_interrupt_line(struct iio_dev *dev_info, int line_number)
215 {
216         /* make sure the interrupt handlers are all done */
217         flush_scheduled_work();
218         free_irq(dev_info->interrupts[line_number]->irq,
219                  dev_info->interrupts[line_number]);
220         kfree(dev_info->interrupts[line_number]);
221 }
222 EXPORT_SYMBOL(iio_unregister_interrupt_line);
223
224 /* Reference counted add and remove */
225 void iio_add_event_to_list(struct iio_event_handler_list *el,
226                           struct list_head *head)
227 {
228         unsigned long flags;
229         struct iio_interrupt *inter = to_iio_interrupt(head);
230
231         /* take mutex to protect this element */
232         mutex_lock(&el->exist_lock);
233         if (el->refcount == 0) {
234                 /* Take the event list spin lock */
235                 spin_lock_irqsave(&inter->ev_list_lock, flags);
236                 list_add(&el->list, head);
237                 spin_unlock_irqrestore(&inter->ev_list_lock, flags);
238         }
239         el->refcount++;
240         mutex_unlock(&el->exist_lock);
241 }
242 EXPORT_SYMBOL(iio_add_event_to_list);
243
244 void iio_remove_event_from_list(struct iio_event_handler_list *el,
245                                struct list_head *head)
246 {
247         unsigned long flags;
248         struct iio_interrupt *inter = to_iio_interrupt(head);
249
250         mutex_lock(&el->exist_lock);
251         el->refcount--;
252         if (el->refcount == 0) {
253                 /* Take the event list spin lock */
254                 spin_lock_irqsave(&inter->ev_list_lock, flags);
255                 list_del_init(&el->list);
256                 spin_unlock_irqrestore(&inter->ev_list_lock, flags);
257         }
258         mutex_unlock(&el->exist_lock);
259 }
260 EXPORT_SYMBOL(iio_remove_event_from_list);
261
262 static ssize_t iio_event_chrdev_read(struct file *filep,
263                                      char __user *buf,
264                                      size_t count,
265                                      loff_t *f_ps)
266 {
267         struct iio_event_interface *ev_int = filep->private_data;
268         struct iio_detected_event_list *el;
269         int ret;
270         size_t len;
271
272         mutex_lock(&ev_int->event_list_lock);
273         if (list_empty(&ev_int->det_events.list)) {
274                 if (filep->f_flags & O_NONBLOCK) {
275                         ret = -EAGAIN;
276                         goto error_mutex_unlock;
277                 }
278                 mutex_unlock(&ev_int->event_list_lock);
279                 /* Blocking on device; waiting for something to be there */
280                 ret = wait_event_interruptible(ev_int->wait,
281                                                !list_empty(&ev_int
282                                                            ->det_events.list));
283                 if (ret)
284                         goto error_ret;
285                 /* Single access device so no one else can get the data */
286                 mutex_lock(&ev_int->event_list_lock);
287         }
288
289         el = list_first_entry(&ev_int->det_events.list,
290                               struct iio_detected_event_list,
291                               list);
292         len = sizeof el->ev;
293         if (copy_to_user(buf, &(el->ev), len)) {
294                 ret = -EFAULT;
295                 goto error_mutex_unlock;
296         }
297         list_del(&el->list);
298         ev_int->current_events--;
299         mutex_unlock(&ev_int->event_list_lock);
300         kfree(el);
301
302         return len;
303
304 error_mutex_unlock:
305         mutex_unlock(&ev_int->event_list_lock);
306 error_ret:
307
308         return ret;
309 }
310
311 static int iio_event_chrdev_release(struct inode *inode, struct file *filep)
312 {
313         struct iio_handler *hand = iio_cdev_to_handler(inode->i_cdev);
314         struct iio_event_interface *ev_int = hand->private;
315         struct iio_detected_event_list *el, *t;
316
317         mutex_lock(&ev_int->event_list_lock);
318         clear_bit(IIO_BUSY_BIT_POS, &ev_int->handler.flags);
319         /*
320          * In order to maintain a clean state for reopening,
321          * clear out any awaiting events. The mask will prevent
322          * any new __iio_push_event calls running.
323          */
324         list_for_each_entry_safe(el, t, &ev_int->det_events.list, list) {
325                 list_del(&el->list);
326                 kfree(el);
327         }
328         mutex_unlock(&ev_int->event_list_lock);
329
330         return 0;
331 }
332
333 static int iio_event_chrdev_open(struct inode *inode, struct file *filep)
334 {
335         struct iio_handler *hand = iio_cdev_to_handler(inode->i_cdev);
336         struct iio_event_interface *ev_int = hand->private;
337
338         mutex_lock(&ev_int->event_list_lock);
339         if (test_and_set_bit(IIO_BUSY_BIT_POS, &hand->flags)) {
340                 fops_put(filep->f_op);
341                 mutex_unlock(&ev_int->event_list_lock);
342                 return -EBUSY;
343         }
344         filep->private_data = hand->private;
345         mutex_unlock(&ev_int->event_list_lock);
346
347         return 0;
348 }
349
350 static const struct file_operations iio_event_chrdev_fileops = {
351         .read =  iio_event_chrdev_read,
352         .release = iio_event_chrdev_release,
353         .open = iio_event_chrdev_open,
354         .owner = THIS_MODULE,
355         .llseek = noop_llseek,
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 ida_again:
375         if (unlikely(ida_pre_get(&iio_chrdev_ida, GFP_KERNEL) == 0))
376                 return -ENOMEM;
377         spin_lock(&iio_ida_lock);
378         ret = ida_get_new(&iio_chrdev_ida, &val);
379         spin_unlock(&iio_ida_lock);
380         if (unlikely(ret == -EAGAIN))
381                 goto ida_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_ida_lock);
392         ida_remove(&iio_chrdev_ida, val);
393         spin_unlock(&iio_ida_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.bus = &iio_bus_type;
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         /* Register sysfs bus */
477         ret  = bus_register(&iio_bus_type);
478         if (ret < 0) {
479                 printk(KERN_ERR
480                        "%s could not register bus type\n",
481                         __FILE__);
482                 goto error_nothing;
483         }
484
485         ret = iio_dev_init();
486         if (ret < 0)
487                 goto error_unregister_bus_type;
488
489         return 0;
490
491 error_unregister_bus_type:
492         bus_unregister(&iio_bus_type);
493 error_nothing:
494         return ret;
495 }
496
497 static void __exit iio_exit(void)
498 {
499         iio_dev_exit();
500         bus_unregister(&iio_bus_type);
501 }
502
503 static ssize_t iio_read_channel_info(struct device *dev,
504                                      struct device_attribute *attr,
505                                      char *buf)
506 {
507         struct iio_dev *indio_dev = dev_get_drvdata(dev);
508         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
509         int val, val2;
510         int ret = indio_dev->read_raw(indio_dev, this_attr->c,
511                                       &val, &val2, this_attr->address);
512
513         if (ret < 0)
514                 return ret;
515
516         if (ret == IIO_VAL_INT)
517                 return sprintf(buf, "%d\n", val);
518         else if (ret == IIO_VAL_INT_PLUS_MICRO) {
519                 if (val2 < 0)
520                         return sprintf(buf, "-%d.%06u\n", val, -val2);
521                 else
522                         return sprintf(buf, "%d.%06u\n", val, val2);
523         } else
524                 return 0;
525 }
526
527 static ssize_t iio_write_channel_info(struct device *dev,
528                                       struct device_attribute *attr,
529                                       const char *buf,
530                                       size_t len)
531 {
532         struct iio_dev *indio_dev = dev_get_drvdata(dev);
533         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
534         int ret, integer = 0, micro = 0, micro_mult = 100000;
535         bool integer_part = true, negative = false;
536
537         /* Assumes decimal - precision based on number of digits */
538         if (!indio_dev->write_raw)
539                 return -EINVAL;
540         if (buf[0] == '-') {
541                 negative = true;
542                 buf++;
543         }
544         while (*buf) {
545                 if ('0' <= *buf && *buf <= '9') {
546                         if (integer_part)
547                                 integer = integer*10 + *buf - '0';
548                         else {
549                                 micro += micro_mult*(*buf - '0');
550                                 if (micro_mult == 1)
551                                         break;
552                                 micro_mult /= 10;
553                         }
554                 } else if (*buf == '\n') {
555                         if (*(buf + 1) == '\0')
556                                 break;
557                         else
558                                 return -EINVAL;
559                 } else if (*buf == '.') {
560                         integer_part = false;
561                 } else {
562                         return -EINVAL;
563                 }
564                 buf++;
565         }
566         if (negative) {
567                 if (integer)
568                         integer = -integer;
569                 else
570                         micro = -micro;
571         }
572
573         ret = indio_dev->write_raw(indio_dev, this_attr->c,
574                                        integer, micro, this_attr->address);
575         if (ret)
576                 return ret;
577
578         return len;
579 }
580
581 static int __iio_build_postfix(struct iio_chan_spec const *chan,
582                                bool generic,
583                                const char *postfix,
584                                char **result)
585 {
586         char *all_post;
587         /* 3 options - generic, extend_name, modified - if generic, extend_name
588         * and modified cannot apply.*/
589
590         if (generic || (!chan->modified && !chan->extend_name)) {
591                 all_post = kasprintf(GFP_KERNEL, "%s", postfix);
592         } else if (chan->modified) {
593                 const char *intermediate;
594                 switch (chan->type) {
595                 case IIO_INTENSITY:
596                         intermediate
597                                 = iio_modifier_names_light[chan->channel2];
598                         break;
599                 case IIO_ACCEL:
600                 case IIO_GYRO:
601                 case IIO_MAGN:
602                 case IIO_INCLI:
603                 case IIO_ROT:
604                 case IIO_ANGL:
605                         intermediate
606                                 = iio_modifier_names_axial[chan->channel2];
607                         break;
608                 default:
609                         return -EINVAL;
610                 }
611                 if (chan->extend_name)
612                         all_post = kasprintf(GFP_KERNEL, "%s_%s_%s",
613                                              intermediate,
614                                              chan->extend_name,
615                                              postfix);
616                 else
617                         all_post = kasprintf(GFP_KERNEL, "%s_%s",
618                                              intermediate,
619                                              postfix);
620         } else
621                 all_post = kasprintf(GFP_KERNEL, "%s_%s", chan->extend_name,
622                                      postfix);
623         if (all_post == NULL)
624                 return -ENOMEM;
625         *result = all_post;
626         return 0;
627 }
628
629 int __iio_device_attr_init(struct device_attribute *dev_attr,
630                            const char *postfix,
631                            struct iio_chan_spec const *chan,
632                            ssize_t (*readfunc)(struct device *dev,
633                                                struct device_attribute *attr,
634                                                char *buf),
635                            ssize_t (*writefunc)(struct device *dev,
636                                                 struct device_attribute *attr,
637                                                 const char *buf,
638                                                 size_t len),
639                            bool generic)
640 {
641         int ret;
642         char *name_format, *full_postfix;
643         sysfs_attr_init(&dev_attr->attr);
644         ret = __iio_build_postfix(chan, generic, postfix, &full_postfix);
645         if (ret)
646                 goto error_ret;
647
648         /* Special case for types that uses both channel numbers in naming */
649         if (chan->type == IIO_IN_DIFF && !generic)
650                 name_format
651                         = kasprintf(GFP_KERNEL, "%s_%s",
652                                     iio_chan_type_name_spec_complex[chan->type],
653                                     full_postfix);
654         else if (generic || !chan->indexed)
655                 name_format
656                         = kasprintf(GFP_KERNEL, "%s_%s",
657                                     iio_chan_type_name_spec_shared[chan->type],
658                                     full_postfix);
659         else
660                 name_format
661                         = kasprintf(GFP_KERNEL, "%s%d_%s",
662                                     iio_chan_type_name_spec_shared[chan->type],
663                                     chan->channel,
664                                     full_postfix);
665
666         if (name_format == NULL) {
667                 ret = -ENOMEM;
668                 goto error_free_full_postfix;
669         }
670         dev_attr->attr.name = kasprintf(GFP_KERNEL,
671                                         name_format,
672                                         chan->channel,
673                                         chan->channel2);
674         if (dev_attr->attr.name == NULL) {
675                 ret = -ENOMEM;
676                 goto error_free_name_format;
677         }
678
679         if (readfunc) {
680                 dev_attr->attr.mode |= S_IRUGO;
681                 dev_attr->show = readfunc;
682         }
683
684         if (writefunc) {
685                 dev_attr->attr.mode |= S_IWUSR;
686                 dev_attr->store = writefunc;
687         }
688         kfree(name_format);
689         kfree(full_postfix);
690
691         return 0;
692
693 error_free_name_format:
694         kfree(name_format);
695 error_free_full_postfix:
696         kfree(full_postfix);
697 error_ret:
698         return ret;
699 }
700
701 void __iio_device_attr_deinit(struct device_attribute *dev_attr)
702 {
703         kfree(dev_attr->attr.name);
704 }
705
706 int __iio_add_chan_devattr(const char *postfix,
707                            const char *group,
708                            struct iio_chan_spec const *chan,
709                            ssize_t (*readfunc)(struct device *dev,
710                                                struct device_attribute *attr,
711                                                char *buf),
712                            ssize_t (*writefunc)(struct device *dev,
713                                                 struct device_attribute *attr,
714                                                 const char *buf,
715                                                 size_t len),
716                            int mask,
717                            bool generic,
718                            struct device *dev,
719                            struct list_head *attr_list)
720 {
721         int ret;
722         struct iio_dev_attr *iio_attr, *t;
723
724         iio_attr = kzalloc(sizeof *iio_attr, GFP_KERNEL);
725         if (iio_attr == NULL) {
726                 ret = -ENOMEM;
727                 goto error_ret;
728         }
729         ret = __iio_device_attr_init(&iio_attr->dev_attr,
730                                      postfix, chan,
731                                      readfunc, writefunc, generic);
732         if (ret)
733                 goto error_iio_dev_attr_free;
734         iio_attr->c = chan;
735         iio_attr->address = mask;
736         list_for_each_entry(t, attr_list, l)
737                 if (strcmp(t->dev_attr.attr.name,
738                            iio_attr->dev_attr.attr.name) == 0) {
739                         if (!generic)
740                                 dev_err(dev, "tried to double register : %s\n",
741                                         t->dev_attr.attr.name);
742                         ret = -EBUSY;
743                         goto error_device_attr_deinit;
744                 }
745
746         ret = sysfs_add_file_to_group(&dev->kobj,
747                                       &iio_attr->dev_attr.attr, group);
748         if (ret < 0)
749                 goto error_device_attr_deinit;
750
751         list_add(&iio_attr->l, attr_list);
752
753         return 0;
754
755 error_device_attr_deinit:
756         __iio_device_attr_deinit(&iio_attr->dev_attr);
757 error_iio_dev_attr_free:
758         kfree(iio_attr);
759 error_ret:
760         return ret;
761 }
762
763 static int iio_device_add_channel_sysfs(struct iio_dev *dev_info,
764                                         struct iio_chan_spec const *chan)
765 {
766         int ret, i;
767
768
769         if (chan->channel < 0)
770                 return 0;
771         if (chan->processed_val)
772                 ret = __iio_add_chan_devattr("input", NULL, chan,
773                                              &iio_read_channel_info,
774                                              NULL,
775                                              0,
776                                              0,
777                                              &dev_info->dev,
778                                              &dev_info->channel_attr_list);
779         else
780                 ret = __iio_add_chan_devattr("raw", NULL, chan,
781                                              &iio_read_channel_info,
782                                              NULL,
783                                              0,
784                                              0,
785                                              &dev_info->dev,
786                                              &dev_info->channel_attr_list);
787         if (ret)
788                 goto error_ret;
789
790         for_each_set_bit(i, &chan->info_mask, sizeof(long)*8) {
791                 ret = __iio_add_chan_devattr(iio_chan_info_postfix[i/2],
792                                              NULL, chan,
793                                              &iio_read_channel_info,
794                                              &iio_write_channel_info,
795                                              (1 << i),
796                                              !(i%2),
797                                              &dev_info->dev,
798                                              &dev_info->channel_attr_list);
799                 if (ret == -EBUSY && (i%2 == 0)) {
800                         ret = 0;
801                         continue;
802                 }
803                 if (ret < 0)
804                         goto error_ret;
805         }
806 error_ret:
807         return ret;
808 }
809
810 static void iio_device_remove_and_free_read_attr(struct iio_dev *dev_info,
811                                                  struct iio_dev_attr *p)
812 {
813         sysfs_remove_file_from_group(&dev_info->dev.kobj,
814                                      &p->dev_attr.attr, NULL);
815         kfree(p->dev_attr.attr.name);
816         kfree(p);
817 }
818
819 static int iio_device_register_sysfs(struct iio_dev *dev_info)
820 {
821         int i, ret = 0;
822         struct iio_dev_attr *p, *n;
823
824         if (dev_info->attrs) {
825                 ret = sysfs_create_group(&dev_info->dev.kobj, dev_info->attrs);
826                 if (ret) {
827                         dev_err(dev_info->dev.parent,
828                                 "Failed to register sysfs hooks\n");
829                         goto error_ret;
830                 }
831         }
832
833         /*
834          * New channel registration method - relies on the fact a group does
835          *  not need to be initialized if it is name is NULL.
836          */
837         INIT_LIST_HEAD(&dev_info->channel_attr_list);
838         if (dev_info->channels)
839                 for (i = 0; i < dev_info->num_channels; i++) {
840                         ret = iio_device_add_channel_sysfs(dev_info,
841                                                            &dev_info
842                                                            ->channels[i]);
843                         if (ret < 0)
844                                 goto error_clear_attrs;
845                 }
846
847         return 0;
848 error_clear_attrs:
849         list_for_each_entry_safe(p, n,
850                                  &dev_info->channel_attr_list, l) {
851                 list_del(&p->l);
852                 iio_device_remove_and_free_read_attr(dev_info, p);
853         }
854         if (dev_info->attrs)
855                 sysfs_remove_group(&dev_info->dev.kobj, dev_info->attrs);
856 error_ret:
857         return ret;
858
859 }
860
861 static void iio_device_unregister_sysfs(struct iio_dev *dev_info)
862 {
863
864         struct iio_dev_attr *p, *n;
865         list_for_each_entry_safe(p, n, &dev_info->channel_attr_list, l) {
866                 list_del(&p->l);
867                 iio_device_remove_and_free_read_attr(dev_info, p);
868         }
869
870         if (dev_info->attrs)
871                 sysfs_remove_group(&dev_info->dev.kobj, dev_info->attrs);
872 }
873
874 /* Return a negative errno on failure */
875 int iio_get_new_ida_val(struct ida *this_ida)
876 {
877         int ret;
878         int val;
879
880 ida_again:
881         if (unlikely(ida_pre_get(this_ida, GFP_KERNEL) == 0))
882                 return -ENOMEM;
883
884         spin_lock(&iio_ida_lock);
885         ret = ida_get_new(this_ida, &val);
886         spin_unlock(&iio_ida_lock);
887         if (unlikely(ret == -EAGAIN))
888                 goto ida_again;
889         else if (unlikely(ret))
890                 return ret;
891
892         return val;
893 }
894 EXPORT_SYMBOL(iio_get_new_ida_val);
895
896 void iio_free_ida_val(struct ida *this_ida, int id)
897 {
898         spin_lock(&iio_ida_lock);
899         ida_remove(this_ida, id);
900         spin_unlock(&iio_ida_lock);
901 }
902 EXPORT_SYMBOL(iio_free_ida_val);
903
904 static int iio_device_register_id(struct iio_dev *dev_info,
905                                   struct ida *this_ida)
906 {
907         dev_info->id = iio_get_new_ida_val(&iio_ida);
908         if (dev_info->id < 0)
909                 return dev_info->id;
910         return 0;
911 }
912
913 static void iio_device_unregister_id(struct iio_dev *dev_info)
914 {
915         iio_free_ida_val(&iio_ida, dev_info->id);
916 }
917
918 static const char * const iio_ev_type_text[] = {
919         [IIO_EV_TYPE_THRESH] = "thresh",
920         [IIO_EV_TYPE_MAG] = "mag",
921         [IIO_EV_TYPE_ROC] = "roc"
922 };
923
924 static const char * const iio_ev_dir_text[] = {
925         [IIO_EV_DIR_EITHER] = "either",
926         [IIO_EV_DIR_RISING] = "rising",
927         [IIO_EV_DIR_FALLING] = "falling"
928 };
929
930 static ssize_t iio_ev_state_store(struct device *dev,
931                                   struct device_attribute *attr,
932                                   const char *buf,
933                                   size_t len)
934 {
935         struct iio_dev *indio_dev = dev_get_drvdata(dev);
936         struct iio_event_attr *this_attr = to_iio_event_attr(attr);
937         int ret;
938         unsigned long val;
939         ret = strict_strtoul(buf, 10, &val);
940         if (ret || val < 0 || val > 1)
941                 return -EINVAL;
942
943         ret = indio_dev->write_event_config(indio_dev, this_attr->mask,
944                                             this_attr->listel,
945                                             val);
946         return (ret < 0) ? ret : len;
947 }
948
949 static ssize_t iio_ev_state_show(struct device *dev,
950                                  struct device_attribute *attr,
951                                  char *buf)
952 {
953         struct iio_dev *indio_dev = dev_get_drvdata(dev);
954         struct iio_event_attr *this_attr = to_iio_event_attr(attr);
955         int val = indio_dev->read_event_config(indio_dev, this_attr->mask);
956
957         if (val < 0)
958                 return val;
959         else
960                 return sprintf(buf, "%d\n", val);
961 }
962
963 static ssize_t iio_ev_value_show(struct device *dev,
964                                  struct device_attribute *attr,
965                                  char *buf)
966 {
967         struct iio_dev *indio_dev = dev_get_drvdata(dev);
968         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
969         int val, ret;
970
971         ret = indio_dev->read_event_value(indio_dev,
972                                           this_attr->address, &val);
973         if (ret < 0)
974                 return ret;
975
976         return sprintf(buf, "%d\n", val);
977 }
978
979 static ssize_t iio_ev_value_store(struct device *dev,
980                                   struct device_attribute *attr,
981                                   const char *buf,
982                                   size_t len)
983 {
984         struct iio_dev *indio_dev = dev_get_drvdata(dev);
985         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
986         unsigned long val;
987         int ret;
988
989         ret = strict_strtoul(buf, 10, &val);
990         if (ret)
991                 return ret;
992
993         ret = indio_dev->write_event_value(indio_dev, this_attr->address,
994                                            val);
995         if (ret < 0)
996                 return ret;
997
998         return len;
999 }
1000
1001 static int __iio_add_chan_event_attr(const char *postfix,
1002                                      const char *group,
1003                                      struct iio_chan_spec const *chan,
1004                                      unsigned int mask,
1005                                      struct device *dev,
1006                                      struct list_head *attr_list)
1007 {
1008         char *name_format, *full_postfix;
1009         int ret;
1010         struct iio_event_attr *iio_ev_attr;
1011
1012         iio_ev_attr = kzalloc(sizeof *iio_ev_attr, GFP_KERNEL);
1013         if (iio_ev_attr == NULL) {
1014                 ret = -ENOMEM;
1015                 goto error_ret;
1016         }
1017
1018         sysfs_attr_init(&iio_ev_attr->dev_attr.attr);
1019         ret = __iio_build_postfix(chan, 0, postfix, &full_postfix);
1020         if (ret)
1021                 goto error_ret;
1022         /* Special case for types that uses both channel numbers in naming */
1023         if (chan->type == IIO_IN_DIFF)
1024                 name_format
1025                         = kasprintf(GFP_KERNEL, "%s_%s",
1026                                     iio_chan_type_name_spec_complex[chan->type],
1027                                     full_postfix);
1028         else if (!chan->indexed)
1029                 name_format
1030                         = kasprintf(GFP_KERNEL, "%s_%s",
1031                                     iio_chan_type_name_spec_shared[chan->type],
1032                                     full_postfix);
1033         else
1034                 name_format
1035                         = kasprintf(GFP_KERNEL, "%s%d_%s",
1036                                     iio_chan_type_name_spec_shared[chan->type],
1037                                     chan->channel,
1038                                     full_postfix);
1039         if (name_format == NULL) {
1040                 ret = -ENOMEM;
1041                 goto error_free_attr;
1042         }
1043
1044         iio_ev_attr->dev_attr.attr.name = kasprintf(GFP_KERNEL,
1045                                                     name_format,
1046                                                     chan->channel,
1047                                                     chan->channel2);
1048         if (iio_ev_attr->dev_attr.attr.name == NULL) {
1049                 ret = -ENOMEM;
1050                 goto error_free_name_format;
1051         }
1052
1053         iio_ev_attr->dev_attr.attr.mode = S_IRUGO | S_IWUSR;
1054         iio_ev_attr->dev_attr.show = &iio_ev_state_show;
1055         iio_ev_attr->dev_attr.store = &iio_ev_state_store;
1056         iio_ev_attr->mask = mask;
1057         iio_ev_attr->listel = chan->shared_handler;
1058         ret = sysfs_add_file_to_group(&dev->kobj,
1059                                       &iio_ev_attr->dev_attr.attr,
1060                                       group);
1061         if (ret < 0)
1062                 goto error_free_name;
1063         list_add(&iio_ev_attr->l, attr_list);
1064         kfree(name_format);
1065         return 0;
1066
1067 error_free_name:
1068         kfree(iio_ev_attr->dev_attr.attr.name);
1069 error_free_name_format:
1070         kfree(name_format);
1071 error_free_attr:
1072         kfree(iio_ev_attr);
1073 error_ret:
1074         return ret;
1075 }
1076
1077
1078 static int iio_device_add_event_sysfs(struct iio_dev *dev_info,
1079                                       struct iio_chan_spec const *chan)
1080 {
1081
1082         int ret = 0, i, mask;
1083         char *postfix;
1084         if (!chan->event_mask)
1085                 return 0;
1086
1087         for_each_set_bit(i, &chan->event_mask, sizeof(chan->event_mask)*8) {
1088                 postfix = kasprintf(GFP_KERNEL, "%s_%s_en",
1089                                     iio_ev_type_text[i/IIO_EV_TYPE_MAX],
1090                                     iio_ev_dir_text[i%IIO_EV_TYPE_MAX]);
1091                 if (postfix == NULL) {
1092                         ret = -ENOMEM;
1093                         goto error_ret;
1094                 }
1095                 switch (chan->type) {
1096                         /* Switch this to a table at some point */
1097                 case IIO_IN:
1098                         mask = IIO_UNMOD_EVENT_CODE(chan->type, chan->channel,
1099                                                     i/IIO_EV_TYPE_MAX,
1100                                                     i%IIO_EV_TYPE_MAX);
1101                         break;
1102                 case IIO_ACCEL:
1103                         mask = IIO_MOD_EVENT_CODE(chan->type, 0, chan->channel,
1104                                                   i/IIO_EV_TYPE_MAX,
1105                                                   i%IIO_EV_TYPE_MAX);
1106                         break;
1107                 case IIO_IN_DIFF:
1108                         mask = IIO_MOD_EVENT_CODE(chan->type, chan->channel,
1109                                                   chan->channel2,
1110                                                   i/IIO_EV_TYPE_MAX,
1111                                                   i%IIO_EV_TYPE_MAX);
1112                         break;
1113                 default:
1114                         printk(KERN_INFO "currently unhandled type of event\n");
1115                 }
1116                 ret = __iio_add_chan_event_attr(postfix,
1117                                                 NULL,
1118                                                 chan,
1119                                                 mask,
1120                                                 /*HACK. - limits us to one
1121                                                   event interface - fix by
1122                                                   extending the bitmask - but
1123                                                   how far*/
1124                                                 &dev_info->event_interfaces[0]
1125                                                 .dev,
1126                                                 &dev_info->event_interfaces[0].
1127                                                 event_attr_list);
1128                 kfree(postfix);
1129                 if (ret)
1130                         goto error_ret;
1131
1132                 postfix = kasprintf(GFP_KERNEL, "%s_%s_value",
1133                                     iio_ev_type_text[i/IIO_EV_TYPE_MAX],
1134                                     iio_ev_dir_text[i%IIO_EV_TYPE_MAX]);
1135                 if (postfix == NULL) {
1136                         ret = -ENOMEM;
1137                         goto error_ret;
1138                 }
1139                 ret = __iio_add_chan_devattr(postfix, NULL, chan,
1140                                              iio_ev_value_show,
1141                                              iio_ev_value_store,
1142                                              mask,
1143                                              0,
1144                                              &dev_info->event_interfaces[0]
1145                                              .dev,
1146                                              &dev_info->event_interfaces[0]
1147                                              .dev_attr_list);
1148                 kfree(postfix);
1149                 if (ret)
1150                         goto error_ret;
1151
1152         }
1153
1154 error_ret:
1155         return ret;
1156 }
1157
1158 static inline void __iio_remove_all_event_sysfs(struct iio_dev *dev_info,
1159                                                 const char *groupname,
1160                                                 int num)
1161 {
1162         struct iio_dev_attr *p, *n;
1163         struct iio_event_attr *q, *m;
1164         list_for_each_entry_safe(p, n,
1165                                  &dev_info->event_interfaces[num].
1166                                  dev_attr_list, l) {
1167                 sysfs_remove_file_from_group(&dev_info
1168                                              ->event_interfaces[num].dev.kobj,
1169                                              &p->dev_attr.attr,
1170                                              groupname);
1171                 kfree(p->dev_attr.attr.name);
1172                 kfree(p);
1173         }
1174         list_for_each_entry_safe(q, m,
1175                                  &dev_info->event_interfaces[num].
1176                                  event_attr_list, l) {
1177                 sysfs_remove_file_from_group(&dev_info
1178                                              ->event_interfaces[num].dev.kobj,
1179                                              &q->dev_attr.attr,
1180                                              groupname);
1181                 kfree(q->dev_attr.attr.name);
1182                 kfree(q);
1183         }
1184 }
1185
1186 static inline int __iio_add_event_config_attrs(struct iio_dev *dev_info, int i)
1187 {
1188         int j;
1189         int ret;
1190         /*p for adding, q for removing */
1191         struct attribute **attrp, **attrq;
1192
1193         if (dev_info->event_conf_attrs && dev_info->event_conf_attrs[i].attrs) {
1194                 attrp = dev_info->event_conf_attrs[i].attrs;
1195                 while (*attrp) {
1196                         ret =  sysfs_add_file_to_group(&dev_info
1197                                                        ->event_interfaces[0]
1198                                                        .dev.kobj,
1199                                                        *attrp,
1200                                                        NULL);
1201                         if (ret)
1202                                 goto error_ret;
1203                         attrp++;
1204                 }
1205         }
1206         INIT_LIST_HEAD(&dev_info->event_interfaces[0].event_attr_list);
1207         INIT_LIST_HEAD(&dev_info->event_interfaces[0].dev_attr_list);
1208         /* Dynically created from the channels array */
1209         if (dev_info->channels) {
1210                 for (j = 0; j < dev_info->num_channels; j++) {
1211                         ret = iio_device_add_event_sysfs(dev_info,
1212                                                          &dev_info
1213                                                          ->channels[j]);
1214                         if (ret)
1215                                 goto error_clear_attrs;
1216                 }
1217         }
1218         return 0;
1219
1220 error_clear_attrs:
1221         __iio_remove_all_event_sysfs(dev_info,
1222                                      NULL,
1223                                      i);
1224 error_ret:
1225         attrq = dev_info->event_conf_attrs[i].attrs;
1226         while (attrq != attrp) {
1227                         sysfs_remove_file_from_group(&dev_info
1228                                                      ->event_interfaces[0]
1229                                                      .dev.kobj,
1230                                                      *attrq,
1231                                                      NULL);
1232                 attrq++;
1233         }
1234
1235         return ret;
1236 }
1237
1238 static inline int __iio_remove_event_config_attrs(struct iio_dev *dev_info,
1239                                                   int i)
1240 {
1241         struct attribute **attrq;
1242         __iio_remove_all_event_sysfs(dev_info,
1243                                      NULL,
1244                                      i);
1245         if (dev_info->event_conf_attrs
1246                 && dev_info->event_conf_attrs[i].attrs) {
1247                 attrq = dev_info->event_conf_attrs[i].attrs;
1248                 while (*attrq) {
1249                         sysfs_remove_file_from_group(&dev_info
1250                                                      ->event_interfaces[0]
1251                                                      .dev.kobj,
1252                                                      *attrq,
1253                                                      NULL);
1254                         attrq++;
1255                 }
1256         }
1257
1258         return 0;
1259 }
1260
1261 static int iio_device_register_eventset(struct iio_dev *dev_info)
1262 {
1263         int ret = 0, i, j;
1264
1265         if (dev_info->num_interrupt_lines == 0)
1266                 return 0;
1267
1268         dev_info->event_interfaces =
1269                 kzalloc(sizeof(struct iio_event_interface)
1270                         *dev_info->num_interrupt_lines,
1271                         GFP_KERNEL);
1272         if (dev_info->event_interfaces == NULL) {
1273                 ret = -ENOMEM;
1274                 goto error_ret;
1275         }
1276
1277         dev_info->interrupts = kzalloc(sizeof(struct iio_interrupt *)
1278                                        *dev_info->num_interrupt_lines,
1279                                        GFP_KERNEL);
1280         if (dev_info->interrupts == NULL) {
1281                 ret = -ENOMEM;
1282                 goto error_free_event_interfaces;
1283         }
1284
1285         for (i = 0; i < dev_info->num_interrupt_lines; i++) {
1286                 dev_info->event_interfaces[i].owner = dev_info->driver_module;
1287
1288                 snprintf(dev_info->event_interfaces[i]._name, 20,
1289                          "%s:event%d",
1290                          dev_name(&dev_info->dev),
1291                          i);
1292
1293                 ret = iio_setup_ev_int(&dev_info->event_interfaces[i],
1294                                        (const char *)(dev_info
1295                                                       ->event_interfaces[i]
1296                                                       ._name),
1297                                        dev_info->driver_module,
1298                                        &dev_info->dev);
1299                 if (ret) {
1300                         dev_err(&dev_info->dev,
1301                                 "Could not get chrdev interface\n");
1302                         goto error_free_setup_ev_ints;
1303                 }
1304
1305                 dev_set_drvdata(&dev_info->event_interfaces[i].dev,
1306                                 (void *)dev_info);
1307
1308                 if (dev_info->event_attrs != NULL)
1309                         ret = sysfs_create_group(&dev_info
1310                                                  ->event_interfaces[i]
1311                                                  .dev.kobj,
1312                                                  &dev_info->event_attrs[i]);
1313
1314                 if (ret) {
1315                         dev_err(&dev_info->dev,
1316                                 "Failed to register sysfs for event attrs");
1317                         goto error_remove_sysfs_interfaces;
1318                 }
1319         }
1320
1321         for (i = 0; i < dev_info->num_interrupt_lines; i++) {
1322                 ret = __iio_add_event_config_attrs(dev_info, i);
1323                 if (ret)
1324                         goto error_unregister_config_attrs;
1325         }
1326
1327         return 0;
1328
1329 error_unregister_config_attrs:
1330         for (j = 0; j < i; j++)
1331                 __iio_remove_event_config_attrs(dev_info, i);
1332         i = dev_info->num_interrupt_lines - 1;
1333 error_remove_sysfs_interfaces:
1334         for (j = 0; j < i; j++)
1335                 if (dev_info->event_attrs != NULL)
1336                         sysfs_remove_group(&dev_info
1337                                    ->event_interfaces[j].dev.kobj,
1338                                    &dev_info->event_attrs[j]);
1339 error_free_setup_ev_ints:
1340         for (j = 0; j < i; j++)
1341                 iio_free_ev_int(&dev_info->event_interfaces[j]);
1342         kfree(dev_info->interrupts);
1343 error_free_event_interfaces:
1344         kfree(dev_info->event_interfaces);
1345 error_ret:
1346
1347         return ret;
1348 }
1349
1350 static void iio_device_unregister_eventset(struct iio_dev *dev_info)
1351 {
1352         int i;
1353
1354         if (dev_info->num_interrupt_lines == 0)
1355                 return;
1356         for (i = 0; i < dev_info->num_interrupt_lines; i++) {
1357                 __iio_remove_event_config_attrs(dev_info, i);
1358                 if (dev_info->event_attrs != NULL)
1359                         sysfs_remove_group(&dev_info
1360                                            ->event_interfaces[i].dev.kobj,
1361                                            &dev_info->event_attrs[i]);
1362         }
1363
1364         for (i = 0; i < dev_info->num_interrupt_lines; i++)
1365                 iio_free_ev_int(&dev_info->event_interfaces[i]);
1366         kfree(dev_info->interrupts);
1367         kfree(dev_info->event_interfaces);
1368 }
1369
1370 static void iio_dev_release(struct device *device)
1371 {
1372         struct iio_dev *dev = to_iio_dev(device);
1373
1374         iio_put();
1375         kfree(dev);
1376 }
1377
1378 static struct device_type iio_dev_type = {
1379         .name = "iio_device",
1380         .release = iio_dev_release,
1381 };
1382
1383 struct iio_dev *iio_allocate_device(int sizeof_priv)
1384 {
1385         struct iio_dev *dev;
1386         size_t alloc_size;
1387
1388         alloc_size = sizeof(struct iio_dev);
1389         if (sizeof_priv) {
1390                 alloc_size = ALIGN(alloc_size, IIO_ALIGN);
1391                 alloc_size += sizeof_priv;
1392         }
1393         /* ensure 32-byte alignment of whole construct ? */
1394         alloc_size += IIO_ALIGN - 1;
1395
1396         dev = kzalloc(alloc_size, GFP_KERNEL);
1397
1398         if (dev) {
1399                 dev->dev.type = &iio_dev_type;
1400                 dev->dev.bus = &iio_bus_type;
1401                 device_initialize(&dev->dev);
1402                 dev_set_drvdata(&dev->dev, (void *)dev);
1403                 mutex_init(&dev->mlock);
1404                 iio_get();
1405         }
1406
1407         return dev;
1408 }
1409 EXPORT_SYMBOL(iio_allocate_device);
1410
1411 void iio_free_device(struct iio_dev *dev)
1412 {
1413         if (dev)
1414                 iio_put_device(dev);
1415 }
1416 EXPORT_SYMBOL(iio_free_device);
1417
1418 int iio_device_register(struct iio_dev *dev_info)
1419 {
1420         int ret;
1421
1422         ret = iio_device_register_id(dev_info, &iio_ida);
1423         if (ret) {
1424                 dev_err(&dev_info->dev, "Failed to get id\n");
1425                 goto error_ret;
1426         }
1427         dev_set_name(&dev_info->dev, "device%d", dev_info->id);
1428
1429         ret = device_add(&dev_info->dev);
1430         if (ret)
1431                 goto error_free_ida;
1432         ret = iio_device_register_sysfs(dev_info);
1433         if (ret) {
1434                 dev_err(dev_info->dev.parent,
1435                         "Failed to register sysfs interfaces\n");
1436                 goto error_del_device;
1437         }
1438         ret = iio_device_register_eventset(dev_info);
1439         if (ret) {
1440                 dev_err(dev_info->dev.parent,
1441                         "Failed to register event set\n");
1442                 goto error_free_sysfs;
1443         }
1444         if (dev_info->modes & INDIO_RING_TRIGGERED)
1445                 iio_device_register_trigger_consumer(dev_info);
1446
1447         return 0;
1448
1449 error_free_sysfs:
1450         iio_device_unregister_sysfs(dev_info);
1451 error_del_device:
1452         device_del(&dev_info->dev);
1453 error_free_ida:
1454         iio_device_unregister_id(dev_info);
1455 error_ret:
1456         return ret;
1457 }
1458 EXPORT_SYMBOL(iio_device_register);
1459
1460 void iio_device_unregister(struct iio_dev *dev_info)
1461 {
1462         if (dev_info->modes & INDIO_RING_TRIGGERED)
1463                 iio_device_unregister_trigger_consumer(dev_info);
1464         iio_device_unregister_eventset(dev_info);
1465         iio_device_unregister_sysfs(dev_info);
1466         iio_device_unregister_id(dev_info);
1467         device_unregister(&dev_info->dev);
1468 }
1469 EXPORT_SYMBOL(iio_device_unregister);
1470
1471 void iio_put(void)
1472 {
1473         module_put(THIS_MODULE);
1474 }
1475
1476 void iio_get(void)
1477 {
1478         __module_get(THIS_MODULE);
1479 }
1480
1481 subsys_initcall(iio_init);
1482 module_exit(iio_exit);
1483
1484 MODULE_AUTHOR("Jonathan Cameron <jic23@cam.ac.uk>");
1485 MODULE_DESCRIPTION("Industrial I/O core");
1486 MODULE_LICENSE("GPL");