1 /* The industrial I/O core
3 * Copyright (c) 2008 Jonathan Cameron
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.
9 * Based on elements of hwmon and input subsystems.
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>
19 #include <linux/poll.h>
20 #include <linux/sched.h>
21 #include <linux/wait.h>
22 #include <linux/cdev.h>
23 #include <linux/slab.h>
25 #include "trigger_consumer.h"
27 #define IIO_ID_PREFIX "device"
28 #define IIO_ID_FORMAT IIO_ID_PREFIX "%d"
30 /* IDR to assign each registered device a unique id*/
31 static DEFINE_IDA(iio_ida);
32 /* IDR to allocate character device minor numbers */
33 static DEFINE_IDA(iio_chrdev_ida);
34 /* Lock used to protect both of the above */
35 static DEFINE_SPINLOCK(iio_ida_lock);
38 EXPORT_SYMBOL(iio_devt);
40 #define IIO_DEV_MAX 256
41 struct bus_type iio_bus_type = {
44 EXPORT_SYMBOL(iio_bus_type);
46 static const char * const iio_chan_type_name_spec_shared[] = {
47 [IIO_TIMESTAMP] = "timestamp",
48 [IIO_ACCEL] = "accel",
50 [IIO_CURRENT] = "current",
51 [IIO_POWER] = "power",
52 [IIO_IN_DIFF] = "in-in",
56 [IIO_INCLI] = "incli",
58 [IIO_INTENSITY] = "intensity",
59 [IIO_LIGHT] = "illuminance",
63 static const char * const iio_chan_type_name_spec_complex[] = {
64 [IIO_IN_DIFF] = "in%d-in%d",
67 static const char * const iio_modifier_names_light[] = {
68 [IIO_MOD_LIGHT_BOTH] = "both",
69 [IIO_MOD_LIGHT_IR] = "ir",
72 static const char * const iio_modifier_names_axial[] = {
78 /* relies on pairs of these shared then separate */
79 static const char * const iio_chan_info_postfix[] = {
80 [IIO_CHAN_INFO_SCALE_SHARED/2] = "scale",
81 [IIO_CHAN_INFO_OFFSET_SHARED/2] = "offset",
82 [IIO_CHAN_INFO_CALIBSCALE_SHARED/2] = "calibscale",
83 [IIO_CHAN_INFO_CALIBBIAS_SHARED/2] = "calibbias",
84 [IIO_CHAN_INFO_PEAK_SHARED/2] = "peak_raw",
85 [IIO_CHAN_INFO_PEAK_SCALE_SHARED/2] = "peak_scale",
88 int iio_push_event(struct iio_dev *dev_info,
93 struct iio_event_interface *ev_int
94 = &dev_info->event_interfaces[ev_line];
95 struct iio_detected_event_list *ev;
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);
105 ev = kmalloc(sizeof(*ev), GFP_KERNEL);
108 mutex_unlock(&ev_int->event_list_lock);
112 ev->ev.timestamp = timestamp;
114 list_add_tail(&ev->list, &ev_int->det_events);
115 ev_int->current_events++;
116 mutex_unlock(&ev_int->event_list_lock);
117 wake_up_interruptible(&ev_int->wait);
119 mutex_unlock(&ev_int->event_list_lock);
124 EXPORT_SYMBOL(iio_push_event);
127 /* This turns up an awful lot */
128 ssize_t iio_read_const_attr(struct device *dev,
129 struct device_attribute *attr,
132 return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string);
134 EXPORT_SYMBOL(iio_read_const_attr);
137 static ssize_t iio_event_chrdev_read(struct file *filep,
142 struct iio_event_interface *ev_int = filep->private_data;
143 struct iio_detected_event_list *el;
147 mutex_lock(&ev_int->event_list_lock);
148 if (list_empty(&ev_int->det_events)) {
149 if (filep->f_flags & O_NONBLOCK) {
151 goto error_mutex_unlock;
153 mutex_unlock(&ev_int->event_list_lock);
154 /* Blocking on device; waiting for something to be there */
155 ret = wait_event_interruptible(ev_int->wait,
160 /* Single access device so no one else can get the data */
161 mutex_lock(&ev_int->event_list_lock);
164 el = list_first_entry(&ev_int->det_events,
165 struct iio_detected_event_list,
168 if (copy_to_user(buf, &(el->ev), len)) {
170 goto error_mutex_unlock;
173 ev_int->current_events--;
174 mutex_unlock(&ev_int->event_list_lock);
180 mutex_unlock(&ev_int->event_list_lock);
186 static int iio_event_chrdev_release(struct inode *inode, struct file *filep)
188 struct iio_handler *hand = iio_cdev_to_handler(inode->i_cdev);
189 struct iio_event_interface *ev_int = hand->private;
190 struct iio_detected_event_list *el, *t;
192 mutex_lock(&ev_int->event_list_lock);
193 clear_bit(IIO_BUSY_BIT_POS, &ev_int->handler.flags);
195 * In order to maintain a clean state for reopening,
196 * clear out any awaiting events. The mask will prevent
197 * any new __iio_push_event calls running.
199 list_for_each_entry_safe(el, t, &ev_int->det_events, list) {
203 mutex_unlock(&ev_int->event_list_lock);
208 static int iio_event_chrdev_open(struct inode *inode, struct file *filep)
210 struct iio_handler *hand = iio_cdev_to_handler(inode->i_cdev);
211 struct iio_event_interface *ev_int = hand->private;
213 mutex_lock(&ev_int->event_list_lock);
214 if (test_and_set_bit(IIO_BUSY_BIT_POS, &hand->flags)) {
215 fops_put(filep->f_op);
216 mutex_unlock(&ev_int->event_list_lock);
219 filep->private_data = hand->private;
220 mutex_unlock(&ev_int->event_list_lock);
225 static const struct file_operations iio_event_chrdev_fileops = {
226 .read = iio_event_chrdev_read,
227 .release = iio_event_chrdev_release,
228 .open = iio_event_chrdev_open,
229 .owner = THIS_MODULE,
230 .llseek = noop_llseek,
233 static void iio_event_dev_release(struct device *dev)
235 struct iio_event_interface *ev_int
236 = container_of(dev, struct iio_event_interface, dev);
237 cdev_del(&ev_int->handler.chrdev);
238 iio_device_free_chrdev_minor(MINOR(dev->devt));
241 static struct device_type iio_event_type = {
242 .release = iio_event_dev_release,
245 int iio_device_get_chrdev_minor(void)
250 if (unlikely(ida_pre_get(&iio_chrdev_ida, GFP_KERNEL) == 0))
252 spin_lock(&iio_ida_lock);
253 ret = ida_get_new(&iio_chrdev_ida, &val);
254 spin_unlock(&iio_ida_lock);
255 if (unlikely(ret == -EAGAIN))
257 else if (unlikely(ret))
259 if (val > IIO_DEV_MAX)
264 void iio_device_free_chrdev_minor(int val)
266 spin_lock(&iio_ida_lock);
267 ida_remove(&iio_chrdev_ida, val);
268 spin_unlock(&iio_ida_lock);
271 static int iio_setup_ev_int(struct iio_event_interface *ev_int,
272 const char *dev_name,
274 struct module *owner,
279 ev_int->dev.bus = &iio_bus_type;
280 ev_int->dev.parent = dev;
281 ev_int->dev.type = &iio_event_type;
282 device_initialize(&ev_int->dev);
284 minor = iio_device_get_chrdev_minor();
287 goto error_device_put;
289 ev_int->dev.devt = MKDEV(MAJOR(iio_devt), minor);
290 dev_set_name(&ev_int->dev, "%s:event%d", dev_name, index);
292 ret = device_add(&ev_int->dev);
294 goto error_free_minor;
296 cdev_init(&ev_int->handler.chrdev, &iio_event_chrdev_fileops);
297 ev_int->handler.chrdev.owner = owner;
299 mutex_init(&ev_int->event_list_lock);
300 /* discussion point - make this variable? */
301 ev_int->max_events = 10;
302 ev_int->current_events = 0;
303 INIT_LIST_HEAD(&ev_int->det_events);
304 init_waitqueue_head(&ev_int->wait);
305 ev_int->handler.private = ev_int;
306 ev_int->handler.flags = 0;
308 ret = cdev_add(&ev_int->handler.chrdev, ev_int->dev.devt, 1);
310 goto error_unreg_device;
315 device_unregister(&ev_int->dev);
317 iio_device_free_chrdev_minor(minor);
319 put_device(&ev_int->dev);
324 static void iio_free_ev_int(struct iio_event_interface *ev_int)
326 device_unregister(&ev_int->dev);
327 put_device(&ev_int->dev);
330 static int __init iio_dev_init(void)
334 err = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio");
336 printk(KERN_ERR "%s: failed to allocate char dev region\n",
342 static void __exit iio_dev_exit(void)
345 unregister_chrdev_region(iio_devt, IIO_DEV_MAX);
348 static int __init iio_init(void)
352 /* Register sysfs bus */
353 ret = bus_register(&iio_bus_type);
356 "%s could not register bus type\n",
361 ret = iio_dev_init();
363 goto error_unregister_bus_type;
367 error_unregister_bus_type:
368 bus_unregister(&iio_bus_type);
373 static void __exit iio_exit(void)
376 bus_unregister(&iio_bus_type);
379 static ssize_t iio_read_channel_info(struct device *dev,
380 struct device_attribute *attr,
383 struct iio_dev *indio_dev = dev_get_drvdata(dev);
384 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
386 int ret = indio_dev->info->read_raw(indio_dev, this_attr->c,
387 &val, &val2, this_attr->address);
392 if (ret == IIO_VAL_INT)
393 return sprintf(buf, "%d\n", val);
394 else if (ret == IIO_VAL_INT_PLUS_MICRO) {
396 return sprintf(buf, "-%d.%06u\n", val, -val2);
398 return sprintf(buf, "%d.%06u\n", val, val2);
403 static ssize_t iio_write_channel_info(struct device *dev,
404 struct device_attribute *attr,
408 struct iio_dev *indio_dev = dev_get_drvdata(dev);
409 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
410 int ret, integer = 0, micro = 0, micro_mult = 100000;
411 bool integer_part = true, negative = false;
413 /* Assumes decimal - precision based on number of digits */
414 if (!indio_dev->info->write_raw)
421 if ('0' <= *buf && *buf <= '9') {
423 integer = integer*10 + *buf - '0';
425 micro += micro_mult*(*buf - '0');
430 } else if (*buf == '\n') {
431 if (*(buf + 1) == '\0')
435 } else if (*buf == '.') {
436 integer_part = false;
449 ret = indio_dev->info->write_raw(indio_dev, this_attr->c,
450 integer, micro, this_attr->address);
457 static int __iio_build_postfix(struct iio_chan_spec const *chan,
463 /* 3 options - generic, extend_name, modified - if generic, extend_name
464 * and modified cannot apply.*/
466 if (generic || (!chan->modified && !chan->extend_name)) {
467 all_post = kasprintf(GFP_KERNEL, "%s", postfix);
468 } else if (chan->modified) {
469 const char *intermediate;
470 switch (chan->type) {
473 = iio_modifier_names_light[chan->channel2];
482 = iio_modifier_names_axial[chan->channel2];
487 if (chan->extend_name)
488 all_post = kasprintf(GFP_KERNEL, "%s_%s_%s",
493 all_post = kasprintf(GFP_KERNEL, "%s_%s",
497 all_post = kasprintf(GFP_KERNEL, "%s_%s", chan->extend_name,
499 if (all_post == NULL)
505 int __iio_device_attr_init(struct device_attribute *dev_attr,
507 struct iio_chan_spec const *chan,
508 ssize_t (*readfunc)(struct device *dev,
509 struct device_attribute *attr,
511 ssize_t (*writefunc)(struct device *dev,
512 struct device_attribute *attr,
518 char *name_format, *full_postfix;
519 sysfs_attr_init(&dev_attr->attr);
520 ret = __iio_build_postfix(chan, generic, postfix, &full_postfix);
524 /* Special case for types that uses both channel numbers in naming */
525 if (chan->type == IIO_IN_DIFF && !generic)
527 = kasprintf(GFP_KERNEL, "%s_%s",
528 iio_chan_type_name_spec_complex[chan->type],
530 else if (generic || !chan->indexed)
532 = kasprintf(GFP_KERNEL, "%s_%s",
533 iio_chan_type_name_spec_shared[chan->type],
537 = kasprintf(GFP_KERNEL, "%s%d_%s",
538 iio_chan_type_name_spec_shared[chan->type],
542 if (name_format == NULL) {
544 goto error_free_full_postfix;
546 dev_attr->attr.name = kasprintf(GFP_KERNEL,
550 if (dev_attr->attr.name == NULL) {
552 goto error_free_name_format;
556 dev_attr->attr.mode |= S_IRUGO;
557 dev_attr->show = readfunc;
561 dev_attr->attr.mode |= S_IWUSR;
562 dev_attr->store = writefunc;
569 error_free_name_format:
571 error_free_full_postfix:
577 void __iio_device_attr_deinit(struct device_attribute *dev_attr)
579 kfree(dev_attr->attr.name);
582 int __iio_add_chan_devattr(const char *postfix,
584 struct iio_chan_spec const *chan,
585 ssize_t (*readfunc)(struct device *dev,
586 struct device_attribute *attr,
588 ssize_t (*writefunc)(struct device *dev,
589 struct device_attribute *attr,
595 struct list_head *attr_list)
598 struct iio_dev_attr *iio_attr, *t;
600 iio_attr = kzalloc(sizeof *iio_attr, GFP_KERNEL);
601 if (iio_attr == NULL) {
605 ret = __iio_device_attr_init(&iio_attr->dev_attr,
607 readfunc, writefunc, generic);
609 goto error_iio_dev_attr_free;
611 iio_attr->address = mask;
612 list_for_each_entry(t, attr_list, l)
613 if (strcmp(t->dev_attr.attr.name,
614 iio_attr->dev_attr.attr.name) == 0) {
616 dev_err(dev, "tried to double register : %s\n",
617 t->dev_attr.attr.name);
619 goto error_device_attr_deinit;
622 ret = sysfs_add_file_to_group(&dev->kobj,
623 &iio_attr->dev_attr.attr, group);
625 goto error_device_attr_deinit;
627 list_add(&iio_attr->l, attr_list);
631 error_device_attr_deinit:
632 __iio_device_attr_deinit(&iio_attr->dev_attr);
633 error_iio_dev_attr_free:
639 static int iio_device_add_channel_sysfs(struct iio_dev *dev_info,
640 struct iio_chan_spec const *chan)
645 if (chan->channel < 0)
647 if (chan->processed_val)
648 ret = __iio_add_chan_devattr("input", NULL, chan,
649 &iio_read_channel_info,
654 &dev_info->channel_attr_list);
656 ret = __iio_add_chan_devattr("raw", NULL, chan,
657 &iio_read_channel_info,
662 &dev_info->channel_attr_list);
666 for_each_set_bit(i, &chan->info_mask, sizeof(long)*8) {
667 ret = __iio_add_chan_devattr(iio_chan_info_postfix[i/2],
669 &iio_read_channel_info,
670 &iio_write_channel_info,
674 &dev_info->channel_attr_list);
675 if (ret == -EBUSY && (i%2 == 0)) {
686 static void iio_device_remove_and_free_read_attr(struct iio_dev *dev_info,
687 struct iio_dev_attr *p)
689 sysfs_remove_file_from_group(&dev_info->dev.kobj,
690 &p->dev_attr.attr, NULL);
691 kfree(p->dev_attr.attr.name);
695 static ssize_t iio_show_dev_name(struct device *dev,
696 struct device_attribute *attr,
699 struct iio_dev *indio_dev = dev_get_drvdata(dev);
700 return sprintf(buf, "%s\n", indio_dev->name);
703 static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL);
705 static int iio_device_register_sysfs(struct iio_dev *dev_info)
708 struct iio_dev_attr *p, *n;
710 if (dev_info->info->attrs) {
711 ret = sysfs_create_group(&dev_info->dev.kobj,
712 dev_info->info->attrs);
714 dev_err(dev_info->dev.parent,
715 "Failed to register sysfs hooks\n");
721 * New channel registration method - relies on the fact a group does
722 * not need to be initialized if it is name is NULL.
724 INIT_LIST_HEAD(&dev_info->channel_attr_list);
725 if (dev_info->channels)
726 for (i = 0; i < dev_info->num_channels; i++) {
727 ret = iio_device_add_channel_sysfs(dev_info,
731 goto error_clear_attrs;
733 if (dev_info->name) {
734 ret = sysfs_add_file_to_group(&dev_info->dev.kobj,
738 goto error_clear_attrs;
743 list_for_each_entry_safe(p, n,
744 &dev_info->channel_attr_list, l) {
746 iio_device_remove_and_free_read_attr(dev_info, p);
748 if (dev_info->info->attrs)
749 sysfs_remove_group(&dev_info->dev.kobj, dev_info->info->attrs);
755 static void iio_device_unregister_sysfs(struct iio_dev *dev_info)
758 struct iio_dev_attr *p, *n;
760 sysfs_remove_file_from_group(&dev_info->dev.kobj,
763 list_for_each_entry_safe(p, n, &dev_info->channel_attr_list, l) {
765 iio_device_remove_and_free_read_attr(dev_info, p);
768 if (dev_info->info->attrs)
769 sysfs_remove_group(&dev_info->dev.kobj, dev_info->info->attrs);
772 /* Return a negative errno on failure */
773 int iio_get_new_ida_val(struct ida *this_ida)
779 if (unlikely(ida_pre_get(this_ida, GFP_KERNEL) == 0))
782 spin_lock(&iio_ida_lock);
783 ret = ida_get_new(this_ida, &val);
784 spin_unlock(&iio_ida_lock);
785 if (unlikely(ret == -EAGAIN))
787 else if (unlikely(ret))
792 EXPORT_SYMBOL(iio_get_new_ida_val);
794 void iio_free_ida_val(struct ida *this_ida, int id)
796 spin_lock(&iio_ida_lock);
797 ida_remove(this_ida, id);
798 spin_unlock(&iio_ida_lock);
800 EXPORT_SYMBOL(iio_free_ida_val);
802 static const char * const iio_ev_type_text[] = {
803 [IIO_EV_TYPE_THRESH] = "thresh",
804 [IIO_EV_TYPE_MAG] = "mag",
805 [IIO_EV_TYPE_ROC] = "roc"
808 static const char * const iio_ev_dir_text[] = {
809 [IIO_EV_DIR_EITHER] = "either",
810 [IIO_EV_DIR_RISING] = "rising",
811 [IIO_EV_DIR_FALLING] = "falling"
814 static ssize_t iio_ev_state_store(struct device *dev,
815 struct device_attribute *attr,
819 struct iio_dev *indio_dev = dev_get_drvdata(dev);
820 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
824 ret = strtobool(buf, &val);
828 ret = indio_dev->info->write_event_config(indio_dev,
831 return (ret < 0) ? ret : len;
834 static ssize_t iio_ev_state_show(struct device *dev,
835 struct device_attribute *attr,
838 struct iio_dev *indio_dev = dev_get_drvdata(dev);
839 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
840 int val = indio_dev->info->read_event_config(indio_dev,
846 return sprintf(buf, "%d\n", val);
849 static ssize_t iio_ev_value_show(struct device *dev,
850 struct device_attribute *attr,
853 struct iio_dev *indio_dev = dev_get_drvdata(dev);
854 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
857 ret = indio_dev->info->read_event_value(indio_dev,
858 this_attr->address, &val);
862 return sprintf(buf, "%d\n", val);
865 static ssize_t iio_ev_value_store(struct device *dev,
866 struct device_attribute *attr,
870 struct iio_dev *indio_dev = dev_get_drvdata(dev);
871 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
875 ret = strict_strtoul(buf, 10, &val);
879 ret = indio_dev->info->write_event_value(indio_dev, this_attr->address,
887 static int iio_device_add_event_sysfs(struct iio_dev *dev_info,
888 struct iio_chan_spec const *chan)
891 int ret = 0, i, mask;
893 if (!chan->event_mask)
896 for_each_set_bit(i, &chan->event_mask, sizeof(chan->event_mask)*8) {
897 postfix = kasprintf(GFP_KERNEL, "%s_%s_en",
898 iio_ev_type_text[i/IIO_EV_TYPE_MAX],
899 iio_ev_dir_text[i%IIO_EV_TYPE_MAX]);
900 if (postfix == NULL) {
904 switch (chan->type) {
905 /* Switch this to a table at some point */
907 mask = IIO_UNMOD_EVENT_CODE(chan->type, chan->channel,
912 mask = IIO_MOD_EVENT_CODE(chan->type, 0, chan->channel,
917 mask = IIO_MOD_EVENT_CODE(chan->type, chan->channel,
923 printk(KERN_INFO "currently unhandled type of event\n");
925 ret = __iio_add_chan_devattr(postfix,
931 /*HACK. - limits us to one
932 event interface - fix by
933 extending the bitmask - but
936 &dev_info->event_interfaces[0].dev,
937 &dev_info->event_interfaces[0].
943 postfix = kasprintf(GFP_KERNEL, "%s_%s_value",
944 iio_ev_type_text[i/IIO_EV_TYPE_MAX],
945 iio_ev_dir_text[i%IIO_EV_TYPE_MAX]);
946 if (postfix == NULL) {
950 ret = __iio_add_chan_devattr(postfix, NULL, chan,
955 &dev_info->event_interfaces[0]
957 &dev_info->event_interfaces[0]
969 static inline void __iio_remove_all_event_sysfs(struct iio_dev *dev_info,
970 const char *groupname,
973 struct iio_dev_attr *p, *n;
974 list_for_each_entry_safe(p, n,
975 &dev_info->event_interfaces[num].
977 sysfs_remove_file_from_group(&dev_info
978 ->event_interfaces[num].dev.kobj,
981 kfree(p->dev_attr.attr.name);
986 static inline int __iio_add_event_config_attrs(struct iio_dev *dev_info, int i)
990 INIT_LIST_HEAD(&dev_info->event_interfaces[0].dev_attr_list);
991 /* Dynically created from the channels array */
992 if (dev_info->channels) {
993 for (j = 0; j < dev_info->num_channels; j++) {
994 ret = iio_device_add_event_sysfs(dev_info,
998 goto error_clear_attrs;
1004 __iio_remove_all_event_sysfs(dev_info, NULL, i);
1009 static inline int __iio_remove_event_config_attrs(struct iio_dev *dev_info,
1012 __iio_remove_all_event_sysfs(dev_info, NULL, i);
1016 static int iio_device_register_eventset(struct iio_dev *dev_info)
1020 if (dev_info->info->num_interrupt_lines == 0)
1023 dev_info->event_interfaces =
1024 kzalloc(sizeof(struct iio_event_interface)
1025 *dev_info->info->num_interrupt_lines,
1027 if (dev_info->event_interfaces == NULL) {
1032 for (i = 0; i < dev_info->info->num_interrupt_lines; i++) {
1033 ret = iio_setup_ev_int(&dev_info->event_interfaces[i],
1034 dev_name(&dev_info->dev),
1036 dev_info->info->driver_module,
1039 dev_err(&dev_info->dev,
1040 "Could not get chrdev interface\n");
1041 goto error_free_setup_ev_ints;
1044 dev_set_drvdata(&dev_info->event_interfaces[i].dev,
1047 if (dev_info->info->event_attrs != NULL)
1048 ret = sysfs_create_group(&dev_info
1049 ->event_interfaces[i]
1055 dev_err(&dev_info->dev,
1056 "Failed to register sysfs for event attrs");
1057 goto error_remove_sysfs_interfaces;
1061 for (i = 0; i < dev_info->info->num_interrupt_lines; i++) {
1062 ret = __iio_add_event_config_attrs(dev_info, i);
1064 goto error_unregister_config_attrs;
1069 error_unregister_config_attrs:
1070 for (j = 0; j < i; j++)
1071 __iio_remove_event_config_attrs(dev_info, i);
1072 i = dev_info->info->num_interrupt_lines - 1;
1073 error_remove_sysfs_interfaces:
1074 for (j = 0; j < i; j++)
1075 if (dev_info->info->event_attrs != NULL)
1076 sysfs_remove_group(&dev_info
1077 ->event_interfaces[j].dev.kobj,
1078 &dev_info->info->event_attrs[j]);
1079 error_free_setup_ev_ints:
1080 for (j = 0; j < i; j++)
1081 iio_free_ev_int(&dev_info->event_interfaces[j]);
1082 kfree(dev_info->event_interfaces);
1088 static void iio_device_unregister_eventset(struct iio_dev *dev_info)
1092 if (dev_info->info->num_interrupt_lines == 0)
1094 for (i = 0; i < dev_info->info->num_interrupt_lines; i++) {
1095 __iio_remove_event_config_attrs(dev_info, i);
1096 if (dev_info->info->event_attrs != NULL)
1097 sysfs_remove_group(&dev_info
1098 ->event_interfaces[i].dev.kobj,
1099 &dev_info->info->event_attrs[i]);
1102 for (i = 0; i < dev_info->info->num_interrupt_lines; i++)
1103 iio_free_ev_int(&dev_info->event_interfaces[i]);
1104 kfree(dev_info->event_interfaces);
1107 static void iio_dev_release(struct device *device)
1110 kfree(to_iio_dev(device));
1113 static struct device_type iio_dev_type = {
1114 .name = "iio_device",
1115 .release = iio_dev_release,
1118 struct iio_dev *iio_allocate_device(int sizeof_priv)
1120 struct iio_dev *dev;
1123 alloc_size = sizeof(struct iio_dev);
1125 alloc_size = ALIGN(alloc_size, IIO_ALIGN);
1126 alloc_size += sizeof_priv;
1128 /* ensure 32-byte alignment of whole construct ? */
1129 alloc_size += IIO_ALIGN - 1;
1131 dev = kzalloc(alloc_size, GFP_KERNEL);
1134 dev->dev.type = &iio_dev_type;
1135 dev->dev.bus = &iio_bus_type;
1136 device_initialize(&dev->dev);
1137 dev_set_drvdata(&dev->dev, (void *)dev);
1138 mutex_init(&dev->mlock);
1144 EXPORT_SYMBOL(iio_allocate_device);
1146 void iio_free_device(struct iio_dev *dev)
1149 iio_put_device(dev);
1151 EXPORT_SYMBOL(iio_free_device);
1153 int iio_device_register(struct iio_dev *dev_info)
1157 dev_info->id = iio_get_new_ida_val(&iio_ida);
1158 if (dev_info->id < 0) {
1160 dev_err(&dev_info->dev, "Failed to get id\n");
1163 dev_set_name(&dev_info->dev, "device%d", dev_info->id);
1165 ret = device_add(&dev_info->dev);
1167 goto error_free_ida;
1168 ret = iio_device_register_sysfs(dev_info);
1170 dev_err(dev_info->dev.parent,
1171 "Failed to register sysfs interfaces\n");
1172 goto error_del_device;
1174 ret = iio_device_register_eventset(dev_info);
1176 dev_err(dev_info->dev.parent,
1177 "Failed to register event set\n");
1178 goto error_free_sysfs;
1180 if (dev_info->modes & INDIO_RING_TRIGGERED)
1181 iio_device_register_trigger_consumer(dev_info);
1186 iio_device_unregister_sysfs(dev_info);
1188 device_del(&dev_info->dev);
1190 iio_free_ida_val(&iio_ida, dev_info->id);
1194 EXPORT_SYMBOL(iio_device_register);
1196 void iio_device_unregister(struct iio_dev *dev_info)
1198 if (dev_info->modes & INDIO_RING_TRIGGERED)
1199 iio_device_unregister_trigger_consumer(dev_info);
1200 iio_device_unregister_eventset(dev_info);
1201 iio_device_unregister_sysfs(dev_info);
1202 iio_free_ida_val(&iio_ida, dev_info->id);
1203 device_unregister(&dev_info->dev);
1205 EXPORT_SYMBOL(iio_device_unregister);
1209 module_put(THIS_MODULE);
1214 __module_get(THIS_MODULE);
1217 subsys_initcall(iio_init);
1218 module_exit(iio_exit);
1220 MODULE_AUTHOR("Jonathan Cameron <jic23@cam.ac.uk>");
1221 MODULE_DESCRIPTION("Industrial I/O core");
1222 MODULE_LICENSE("GPL");