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[] = {
49 [IIO_CURRENT] = "current",
50 [IIO_POWER] = "power",
51 [IIO_ACCEL] = "accel",
52 [IIO_IN_DIFF] = "in-in",
55 [IIO_LIGHT] = "illuminance",
56 [IIO_INTENSITY] = "intensity",
57 [IIO_PROXIMITY] = "proximity",
59 [IIO_INCLI] = "incli",
62 [IIO_TIMESTAMP] = "timestamp",
65 static const char * const iio_chan_type_name_spec_complex[] = {
66 [IIO_IN_DIFF] = "in%d-in%d",
69 static const char * const iio_modifier_names_light[] = {
70 [IIO_MOD_LIGHT_BOTH] = "both",
71 [IIO_MOD_LIGHT_IR] = "ir",
74 static const char * const iio_modifier_names_axial[] = {
80 /* relies on pairs of these shared then separate */
81 static const char * const iio_chan_info_postfix[] = {
82 [IIO_CHAN_INFO_SCALE_SHARED/2] = "scale",
83 [IIO_CHAN_INFO_OFFSET_SHARED/2] = "offset",
84 [IIO_CHAN_INFO_CALIBSCALE_SHARED/2] = "calibscale",
85 [IIO_CHAN_INFO_CALIBBIAS_SHARED/2] = "calibbias",
86 [IIO_CHAN_INFO_PEAK_SHARED/2] = "peak_raw",
87 [IIO_CHAN_INFO_PEAK_SCALE_SHARED/2] = "peak_scale",
90 int iio_push_event(struct iio_dev *dev_info,
95 struct iio_event_interface *ev_int
96 = &dev_info->event_interfaces[ev_line];
97 struct iio_detected_event_list *ev;
100 /* Does anyone care? */
101 mutex_lock(&ev_int->event_list_lock);
102 if (test_bit(IIO_BUSY_BIT_POS, &ev_int->handler.flags)) {
103 if (ev_int->current_events == ev_int->max_events) {
104 mutex_unlock(&ev_int->event_list_lock);
107 ev = kmalloc(sizeof(*ev), GFP_KERNEL);
110 mutex_unlock(&ev_int->event_list_lock);
114 ev->ev.timestamp = timestamp;
116 list_add_tail(&ev->list, &ev_int->det_events);
117 ev_int->current_events++;
118 mutex_unlock(&ev_int->event_list_lock);
119 wake_up_interruptible(&ev_int->wait);
121 mutex_unlock(&ev_int->event_list_lock);
126 EXPORT_SYMBOL(iio_push_event);
129 /* This turns up an awful lot */
130 ssize_t iio_read_const_attr(struct device *dev,
131 struct device_attribute *attr,
134 return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string);
136 EXPORT_SYMBOL(iio_read_const_attr);
139 static ssize_t iio_event_chrdev_read(struct file *filep,
144 struct iio_event_interface *ev_int = filep->private_data;
145 struct iio_detected_event_list *el;
149 mutex_lock(&ev_int->event_list_lock);
150 if (list_empty(&ev_int->det_events)) {
151 if (filep->f_flags & O_NONBLOCK) {
153 goto error_mutex_unlock;
155 mutex_unlock(&ev_int->event_list_lock);
156 /* Blocking on device; waiting for something to be there */
157 ret = wait_event_interruptible(ev_int->wait,
162 /* Single access device so no one else can get the data */
163 mutex_lock(&ev_int->event_list_lock);
166 el = list_first_entry(&ev_int->det_events,
167 struct iio_detected_event_list,
170 if (copy_to_user(buf, &(el->ev), len)) {
172 goto error_mutex_unlock;
175 ev_int->current_events--;
176 mutex_unlock(&ev_int->event_list_lock);
182 mutex_unlock(&ev_int->event_list_lock);
188 static int iio_event_chrdev_release(struct inode *inode, struct file *filep)
190 struct iio_handler *hand = iio_cdev_to_handler(inode->i_cdev);
191 struct iio_event_interface *ev_int = hand->private;
192 struct iio_detected_event_list *el, *t;
194 mutex_lock(&ev_int->event_list_lock);
195 clear_bit(IIO_BUSY_BIT_POS, &ev_int->handler.flags);
197 * In order to maintain a clean state for reopening,
198 * clear out any awaiting events. The mask will prevent
199 * any new __iio_push_event calls running.
201 list_for_each_entry_safe(el, t, &ev_int->det_events, list) {
205 mutex_unlock(&ev_int->event_list_lock);
210 static int iio_event_chrdev_open(struct inode *inode, struct file *filep)
212 struct iio_handler *hand = iio_cdev_to_handler(inode->i_cdev);
213 struct iio_event_interface *ev_int = hand->private;
215 mutex_lock(&ev_int->event_list_lock);
216 if (test_and_set_bit(IIO_BUSY_BIT_POS, &hand->flags)) {
217 fops_put(filep->f_op);
218 mutex_unlock(&ev_int->event_list_lock);
221 filep->private_data = hand->private;
222 mutex_unlock(&ev_int->event_list_lock);
227 static const struct file_operations iio_event_chrdev_fileops = {
228 .read = iio_event_chrdev_read,
229 .release = iio_event_chrdev_release,
230 .open = iio_event_chrdev_open,
231 .owner = THIS_MODULE,
232 .llseek = noop_llseek,
235 static void iio_event_dev_release(struct device *dev)
237 struct iio_event_interface *ev_int
238 = container_of(dev, struct iio_event_interface, dev);
239 cdev_del(&ev_int->handler.chrdev);
240 iio_device_free_chrdev_minor(MINOR(dev->devt));
243 static struct device_type iio_event_type = {
244 .release = iio_event_dev_release,
247 int iio_device_get_chrdev_minor(void)
252 if (unlikely(ida_pre_get(&iio_chrdev_ida, GFP_KERNEL) == 0))
254 spin_lock(&iio_ida_lock);
255 ret = ida_get_new(&iio_chrdev_ida, &val);
256 spin_unlock(&iio_ida_lock);
257 if (unlikely(ret == -EAGAIN))
259 else if (unlikely(ret))
261 if (val > IIO_DEV_MAX)
266 void iio_device_free_chrdev_minor(int val)
268 spin_lock(&iio_ida_lock);
269 ida_remove(&iio_chrdev_ida, val);
270 spin_unlock(&iio_ida_lock);
273 static int iio_setup_ev_int(struct iio_event_interface *ev_int,
274 const char *dev_name,
276 struct module *owner,
281 ev_int->dev.bus = &iio_bus_type;
282 ev_int->dev.parent = dev;
283 ev_int->dev.type = &iio_event_type;
284 device_initialize(&ev_int->dev);
286 minor = iio_device_get_chrdev_minor();
289 goto error_device_put;
291 ev_int->dev.devt = MKDEV(MAJOR(iio_devt), minor);
292 dev_set_name(&ev_int->dev, "%s:event%d", dev_name, index);
294 ret = device_add(&ev_int->dev);
296 goto error_free_minor;
298 cdev_init(&ev_int->handler.chrdev, &iio_event_chrdev_fileops);
299 ev_int->handler.chrdev.owner = owner;
301 mutex_init(&ev_int->event_list_lock);
302 /* discussion point - make this variable? */
303 ev_int->max_events = 10;
304 ev_int->current_events = 0;
305 INIT_LIST_HEAD(&ev_int->det_events);
306 init_waitqueue_head(&ev_int->wait);
307 ev_int->handler.private = ev_int;
308 ev_int->handler.flags = 0;
310 ret = cdev_add(&ev_int->handler.chrdev, ev_int->dev.devt, 1);
312 goto error_unreg_device;
317 device_unregister(&ev_int->dev);
319 iio_device_free_chrdev_minor(minor);
321 put_device(&ev_int->dev);
326 static void iio_free_ev_int(struct iio_event_interface *ev_int)
328 device_unregister(&ev_int->dev);
329 put_device(&ev_int->dev);
332 static int __init iio_dev_init(void)
336 err = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio");
338 printk(KERN_ERR "%s: failed to allocate char dev region\n",
344 static void __exit iio_dev_exit(void)
347 unregister_chrdev_region(iio_devt, IIO_DEV_MAX);
350 static int __init iio_init(void)
354 /* Register sysfs bus */
355 ret = bus_register(&iio_bus_type);
358 "%s could not register bus type\n",
363 ret = iio_dev_init();
365 goto error_unregister_bus_type;
369 error_unregister_bus_type:
370 bus_unregister(&iio_bus_type);
375 static void __exit iio_exit(void)
378 bus_unregister(&iio_bus_type);
381 static ssize_t iio_read_channel_info(struct device *dev,
382 struct device_attribute *attr,
385 struct iio_dev *indio_dev = dev_get_drvdata(dev);
386 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
388 int ret = indio_dev->info->read_raw(indio_dev, this_attr->c,
389 &val, &val2, this_attr->address);
394 if (ret == IIO_VAL_INT)
395 return sprintf(buf, "%d\n", val);
396 else if (ret == IIO_VAL_INT_PLUS_MICRO) {
398 return sprintf(buf, "-%d.%06u\n", val, -val2);
400 return sprintf(buf, "%d.%06u\n", val, val2);
401 } else if (ret == IIO_VAL_INT_PLUS_NANO) {
403 return sprintf(buf, "-%d.%09u\n", val, -val2);
405 return sprintf(buf, "%d.%09u\n", val, val2);
410 static ssize_t iio_write_channel_info(struct device *dev,
411 struct device_attribute *attr,
415 struct iio_dev *indio_dev = dev_get_drvdata(dev);
416 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
417 int ret, integer = 0, fract = 0, fract_mult = 100000;
418 bool integer_part = true, negative = false;
420 /* Assumes decimal - precision based on number of digits */
421 if (!indio_dev->info->write_raw)
424 if (indio_dev->info->write_raw_get_fmt)
425 switch (indio_dev->info->write_raw_get_fmt(indio_dev,
426 this_attr->c, this_attr->address)) {
427 case IIO_VAL_INT_PLUS_MICRO:
430 case IIO_VAL_INT_PLUS_NANO:
431 fract_mult = 100000000;
443 if ('0' <= *buf && *buf <= '9') {
445 integer = integer*10 + *buf - '0';
447 fract += fract_mult*(*buf - '0');
452 } else if (*buf == '\n') {
453 if (*(buf + 1) == '\0')
457 } else if (*buf == '.') {
458 integer_part = false;
471 ret = indio_dev->info->write_raw(indio_dev, this_attr->c,
472 integer, fract, this_attr->address);
479 static int __iio_build_postfix(struct iio_chan_spec const *chan,
485 /* 3 options - generic, extend_name, modified - if generic, extend_name
486 * and modified cannot apply.*/
488 if (generic || (!chan->modified && !chan->extend_name)) {
489 all_post = kasprintf(GFP_KERNEL, "%s", postfix);
490 } else if (chan->modified) {
491 const char *intermediate;
492 switch (chan->type) {
495 = iio_modifier_names_light[chan->channel2];
504 = iio_modifier_names_axial[chan->channel2];
509 if (chan->extend_name)
510 all_post = kasprintf(GFP_KERNEL, "%s_%s_%s",
515 all_post = kasprintf(GFP_KERNEL, "%s_%s",
519 all_post = kasprintf(GFP_KERNEL, "%s_%s", chan->extend_name,
521 if (all_post == NULL)
527 int __iio_device_attr_init(struct device_attribute *dev_attr,
529 struct iio_chan_spec const *chan,
530 ssize_t (*readfunc)(struct device *dev,
531 struct device_attribute *attr,
533 ssize_t (*writefunc)(struct device *dev,
534 struct device_attribute *attr,
540 char *name_format, *full_postfix;
541 sysfs_attr_init(&dev_attr->attr);
542 ret = __iio_build_postfix(chan, generic, postfix, &full_postfix);
546 /* Special case for types that uses both channel numbers in naming */
547 if (chan->type == IIO_IN_DIFF && !generic)
549 = kasprintf(GFP_KERNEL, "%s_%s",
550 iio_chan_type_name_spec_complex[chan->type],
552 else if (generic || !chan->indexed)
554 = kasprintf(GFP_KERNEL, "%s_%s",
555 iio_chan_type_name_spec_shared[chan->type],
559 = kasprintf(GFP_KERNEL, "%s%d_%s",
560 iio_chan_type_name_spec_shared[chan->type],
564 if (name_format == NULL) {
566 goto error_free_full_postfix;
568 dev_attr->attr.name = kasprintf(GFP_KERNEL,
572 if (dev_attr->attr.name == NULL) {
574 goto error_free_name_format;
578 dev_attr->attr.mode |= S_IRUGO;
579 dev_attr->show = readfunc;
583 dev_attr->attr.mode |= S_IWUSR;
584 dev_attr->store = writefunc;
591 error_free_name_format:
593 error_free_full_postfix:
599 void __iio_device_attr_deinit(struct device_attribute *dev_attr)
601 kfree(dev_attr->attr.name);
604 int __iio_add_chan_devattr(const char *postfix,
606 struct iio_chan_spec const *chan,
607 ssize_t (*readfunc)(struct device *dev,
608 struct device_attribute *attr,
610 ssize_t (*writefunc)(struct device *dev,
611 struct device_attribute *attr,
617 struct list_head *attr_list)
620 struct iio_dev_attr *iio_attr, *t;
622 iio_attr = kzalloc(sizeof *iio_attr, GFP_KERNEL);
623 if (iio_attr == NULL) {
627 ret = __iio_device_attr_init(&iio_attr->dev_attr,
629 readfunc, writefunc, generic);
631 goto error_iio_dev_attr_free;
633 iio_attr->address = mask;
634 list_for_each_entry(t, attr_list, l)
635 if (strcmp(t->dev_attr.attr.name,
636 iio_attr->dev_attr.attr.name) == 0) {
638 dev_err(dev, "tried to double register : %s\n",
639 t->dev_attr.attr.name);
641 goto error_device_attr_deinit;
644 ret = sysfs_add_file_to_group(&dev->kobj,
645 &iio_attr->dev_attr.attr, group);
647 goto error_device_attr_deinit;
649 list_add(&iio_attr->l, attr_list);
653 error_device_attr_deinit:
654 __iio_device_attr_deinit(&iio_attr->dev_attr);
655 error_iio_dev_attr_free:
661 static int iio_device_add_channel_sysfs(struct iio_dev *dev_info,
662 struct iio_chan_spec const *chan)
667 if (chan->channel < 0)
669 if (chan->processed_val)
670 ret = __iio_add_chan_devattr("input", NULL, chan,
671 &iio_read_channel_info,
676 &dev_info->channel_attr_list);
678 ret = __iio_add_chan_devattr("raw", NULL, chan,
679 &iio_read_channel_info,
680 (chan->type == IIO_OUT ?
681 &iio_write_channel_info : NULL),
685 &dev_info->channel_attr_list);
689 for_each_set_bit(i, &chan->info_mask, sizeof(long)*8) {
690 ret = __iio_add_chan_devattr(iio_chan_info_postfix[i/2],
692 &iio_read_channel_info,
693 &iio_write_channel_info,
697 &dev_info->channel_attr_list);
698 if (ret == -EBUSY && (i%2 == 0)) {
709 static void iio_device_remove_and_free_read_attr(struct iio_dev *dev_info,
710 struct iio_dev_attr *p)
712 sysfs_remove_file_from_group(&dev_info->dev.kobj,
713 &p->dev_attr.attr, NULL);
714 kfree(p->dev_attr.attr.name);
718 static ssize_t iio_show_dev_name(struct device *dev,
719 struct device_attribute *attr,
722 struct iio_dev *indio_dev = dev_get_drvdata(dev);
723 return sprintf(buf, "%s\n", indio_dev->name);
726 static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL);
728 static int iio_device_register_sysfs(struct iio_dev *dev_info)
731 struct iio_dev_attr *p, *n;
733 if (dev_info->info->attrs) {
734 ret = sysfs_create_group(&dev_info->dev.kobj,
735 dev_info->info->attrs);
737 dev_err(dev_info->dev.parent,
738 "Failed to register sysfs hooks\n");
744 * New channel registration method - relies on the fact a group does
745 * not need to be initialized if it is name is NULL.
747 INIT_LIST_HEAD(&dev_info->channel_attr_list);
748 if (dev_info->channels)
749 for (i = 0; i < dev_info->num_channels; i++) {
750 ret = iio_device_add_channel_sysfs(dev_info,
754 goto error_clear_attrs;
756 if (dev_info->name) {
757 ret = sysfs_add_file_to_group(&dev_info->dev.kobj,
761 goto error_clear_attrs;
766 list_for_each_entry_safe(p, n,
767 &dev_info->channel_attr_list, l) {
769 iio_device_remove_and_free_read_attr(dev_info, p);
771 if (dev_info->info->attrs)
772 sysfs_remove_group(&dev_info->dev.kobj, dev_info->info->attrs);
778 static void iio_device_unregister_sysfs(struct iio_dev *dev_info)
781 struct iio_dev_attr *p, *n;
783 sysfs_remove_file_from_group(&dev_info->dev.kobj,
786 list_for_each_entry_safe(p, n, &dev_info->channel_attr_list, l) {
788 iio_device_remove_and_free_read_attr(dev_info, p);
791 if (dev_info->info->attrs)
792 sysfs_remove_group(&dev_info->dev.kobj, dev_info->info->attrs);
795 /* Return a negative errno on failure */
796 int iio_get_new_ida_val(struct ida *this_ida)
802 if (unlikely(ida_pre_get(this_ida, GFP_KERNEL) == 0))
805 spin_lock(&iio_ida_lock);
806 ret = ida_get_new(this_ida, &val);
807 spin_unlock(&iio_ida_lock);
808 if (unlikely(ret == -EAGAIN))
810 else if (unlikely(ret))
815 EXPORT_SYMBOL(iio_get_new_ida_val);
817 void iio_free_ida_val(struct ida *this_ida, int id)
819 spin_lock(&iio_ida_lock);
820 ida_remove(this_ida, id);
821 spin_unlock(&iio_ida_lock);
823 EXPORT_SYMBOL(iio_free_ida_val);
825 static const char * const iio_ev_type_text[] = {
826 [IIO_EV_TYPE_THRESH] = "thresh",
827 [IIO_EV_TYPE_MAG] = "mag",
828 [IIO_EV_TYPE_ROC] = "roc"
831 static const char * const iio_ev_dir_text[] = {
832 [IIO_EV_DIR_EITHER] = "either",
833 [IIO_EV_DIR_RISING] = "rising",
834 [IIO_EV_DIR_FALLING] = "falling"
837 static ssize_t iio_ev_state_store(struct device *dev,
838 struct device_attribute *attr,
842 struct iio_dev *indio_dev = dev_get_drvdata(dev);
843 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
847 ret = strtobool(buf, &val);
851 ret = indio_dev->info->write_event_config(indio_dev,
854 return (ret < 0) ? ret : len;
857 static ssize_t iio_ev_state_show(struct device *dev,
858 struct device_attribute *attr,
861 struct iio_dev *indio_dev = dev_get_drvdata(dev);
862 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
863 int val = indio_dev->info->read_event_config(indio_dev,
869 return sprintf(buf, "%d\n", val);
872 static ssize_t iio_ev_value_show(struct device *dev,
873 struct device_attribute *attr,
876 struct iio_dev *indio_dev = dev_get_drvdata(dev);
877 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
880 ret = indio_dev->info->read_event_value(indio_dev,
881 this_attr->address, &val);
885 return sprintf(buf, "%d\n", val);
888 static ssize_t iio_ev_value_store(struct device *dev,
889 struct device_attribute *attr,
893 struct iio_dev *indio_dev = dev_get_drvdata(dev);
894 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
898 ret = strict_strtoul(buf, 10, &val);
902 ret = indio_dev->info->write_event_value(indio_dev, this_attr->address,
910 static int iio_device_add_event_sysfs(struct iio_dev *dev_info,
911 struct iio_chan_spec const *chan)
914 int ret = 0, i, mask;
916 if (!chan->event_mask)
919 for_each_set_bit(i, &chan->event_mask, sizeof(chan->event_mask)*8) {
920 postfix = kasprintf(GFP_KERNEL, "%s_%s_en",
921 iio_ev_type_text[i/IIO_EV_TYPE_MAX],
922 iio_ev_dir_text[i%IIO_EV_TYPE_MAX]);
923 if (postfix == NULL) {
927 switch (chan->type) {
928 /* Switch this to a table at some point */
930 mask = IIO_UNMOD_EVENT_CODE(chan->type, chan->channel,
935 mask = IIO_MOD_EVENT_CODE(chan->type, 0, chan->channel,
940 mask = IIO_MOD_EVENT_CODE(chan->type, chan->channel,
946 printk(KERN_INFO "currently unhandled type of event\n");
948 ret = __iio_add_chan_devattr(postfix,
954 /*HACK. - limits us to one
955 event interface - fix by
956 extending the bitmask - but
959 &dev_info->event_interfaces[0].dev,
960 &dev_info->event_interfaces[0].
966 postfix = kasprintf(GFP_KERNEL, "%s_%s_value",
967 iio_ev_type_text[i/IIO_EV_TYPE_MAX],
968 iio_ev_dir_text[i%IIO_EV_TYPE_MAX]);
969 if (postfix == NULL) {
973 ret = __iio_add_chan_devattr(postfix, NULL, chan,
978 &dev_info->event_interfaces[0]
980 &dev_info->event_interfaces[0]
992 static inline void __iio_remove_all_event_sysfs(struct iio_dev *dev_info,
993 const char *groupname,
996 struct iio_dev_attr *p, *n;
997 list_for_each_entry_safe(p, n,
998 &dev_info->event_interfaces[num].
1000 sysfs_remove_file_from_group(&dev_info
1001 ->event_interfaces[num].dev.kobj,
1004 kfree(p->dev_attr.attr.name);
1009 static inline int __iio_add_event_config_attrs(struct iio_dev *dev_info, int i)
1013 INIT_LIST_HEAD(&dev_info->event_interfaces[0].dev_attr_list);
1014 /* Dynically created from the channels array */
1015 if (dev_info->channels) {
1016 for (j = 0; j < dev_info->num_channels; j++) {
1017 ret = iio_device_add_event_sysfs(dev_info,
1021 goto error_clear_attrs;
1027 __iio_remove_all_event_sysfs(dev_info, NULL, i);
1032 static inline int __iio_remove_event_config_attrs(struct iio_dev *dev_info,
1035 __iio_remove_all_event_sysfs(dev_info, NULL, i);
1039 static int iio_device_register_eventset(struct iio_dev *dev_info)
1043 if (dev_info->info->num_interrupt_lines == 0)
1046 dev_info->event_interfaces =
1047 kzalloc(sizeof(struct iio_event_interface)
1048 *dev_info->info->num_interrupt_lines,
1050 if (dev_info->event_interfaces == NULL) {
1055 for (i = 0; i < dev_info->info->num_interrupt_lines; i++) {
1056 ret = iio_setup_ev_int(&dev_info->event_interfaces[i],
1057 dev_name(&dev_info->dev),
1059 dev_info->info->driver_module,
1062 dev_err(&dev_info->dev,
1063 "Could not get chrdev interface\n");
1064 goto error_free_setup_ev_ints;
1067 dev_set_drvdata(&dev_info->event_interfaces[i].dev,
1070 if (dev_info->info->event_attrs != NULL)
1071 ret = sysfs_create_group(&dev_info
1072 ->event_interfaces[i]
1078 dev_err(&dev_info->dev,
1079 "Failed to register sysfs for event attrs");
1080 goto error_remove_sysfs_interfaces;
1084 for (i = 0; i < dev_info->info->num_interrupt_lines; i++) {
1085 ret = __iio_add_event_config_attrs(dev_info, i);
1087 goto error_unregister_config_attrs;
1092 error_unregister_config_attrs:
1093 for (j = 0; j < i; j++)
1094 __iio_remove_event_config_attrs(dev_info, i);
1095 i = dev_info->info->num_interrupt_lines - 1;
1096 error_remove_sysfs_interfaces:
1097 for (j = 0; j < i; j++)
1098 if (dev_info->info->event_attrs != NULL)
1099 sysfs_remove_group(&dev_info
1100 ->event_interfaces[j].dev.kobj,
1101 &dev_info->info->event_attrs[j]);
1102 error_free_setup_ev_ints:
1103 for (j = 0; j < i; j++)
1104 iio_free_ev_int(&dev_info->event_interfaces[j]);
1105 kfree(dev_info->event_interfaces);
1111 static void iio_device_unregister_eventset(struct iio_dev *dev_info)
1115 if (dev_info->info->num_interrupt_lines == 0)
1117 for (i = 0; i < dev_info->info->num_interrupt_lines; i++) {
1118 __iio_remove_event_config_attrs(dev_info, i);
1119 if (dev_info->info->event_attrs != NULL)
1120 sysfs_remove_group(&dev_info
1121 ->event_interfaces[i].dev.kobj,
1122 &dev_info->info->event_attrs[i]);
1125 for (i = 0; i < dev_info->info->num_interrupt_lines; i++)
1126 iio_free_ev_int(&dev_info->event_interfaces[i]);
1127 kfree(dev_info->event_interfaces);
1130 static void iio_dev_release(struct device *device)
1133 kfree(to_iio_dev(device));
1136 static struct device_type iio_dev_type = {
1137 .name = "iio_device",
1138 .release = iio_dev_release,
1141 struct iio_dev *iio_allocate_device(int sizeof_priv)
1143 struct iio_dev *dev;
1146 alloc_size = sizeof(struct iio_dev);
1148 alloc_size = ALIGN(alloc_size, IIO_ALIGN);
1149 alloc_size += sizeof_priv;
1151 /* ensure 32-byte alignment of whole construct ? */
1152 alloc_size += IIO_ALIGN - 1;
1154 dev = kzalloc(alloc_size, GFP_KERNEL);
1157 dev->dev.type = &iio_dev_type;
1158 dev->dev.bus = &iio_bus_type;
1159 device_initialize(&dev->dev);
1160 dev_set_drvdata(&dev->dev, (void *)dev);
1161 mutex_init(&dev->mlock);
1167 EXPORT_SYMBOL(iio_allocate_device);
1169 void iio_free_device(struct iio_dev *dev)
1172 iio_put_device(dev);
1174 EXPORT_SYMBOL(iio_free_device);
1176 int iio_device_register(struct iio_dev *dev_info)
1180 dev_info->id = iio_get_new_ida_val(&iio_ida);
1181 if (dev_info->id < 0) {
1183 dev_err(&dev_info->dev, "Failed to get id\n");
1186 dev_set_name(&dev_info->dev, "device%d", dev_info->id);
1188 ret = device_add(&dev_info->dev);
1190 goto error_free_ida;
1191 ret = iio_device_register_sysfs(dev_info);
1193 dev_err(dev_info->dev.parent,
1194 "Failed to register sysfs interfaces\n");
1195 goto error_del_device;
1197 ret = iio_device_register_eventset(dev_info);
1199 dev_err(dev_info->dev.parent,
1200 "Failed to register event set\n");
1201 goto error_free_sysfs;
1203 if (dev_info->modes & INDIO_RING_TRIGGERED)
1204 iio_device_register_trigger_consumer(dev_info);
1209 iio_device_unregister_sysfs(dev_info);
1211 device_del(&dev_info->dev);
1213 iio_free_ida_val(&iio_ida, dev_info->id);
1217 EXPORT_SYMBOL(iio_device_register);
1219 void iio_device_unregister(struct iio_dev *dev_info)
1221 if (dev_info->modes & INDIO_RING_TRIGGERED)
1222 iio_device_unregister_trigger_consumer(dev_info);
1223 iio_device_unregister_eventset(dev_info);
1224 iio_device_unregister_sysfs(dev_info);
1225 iio_free_ida_val(&iio_ida, dev_info->id);
1226 device_unregister(&dev_info->dev);
1228 EXPORT_SYMBOL(iio_device_unregister);
1232 module_put(THIS_MODULE);
1237 __module_get(THIS_MODULE);
1240 subsys_initcall(iio_init);
1241 module_exit(iio_exit);
1243 MODULE_AUTHOR("Jonathan Cameron <jic23@cam.ac.uk>");
1244 MODULE_DESCRIPTION("Industrial I/O core");
1245 MODULE_LICENSE("GPL");