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>
24 #include <linux/anon_inodes.h>
27 #include "iio_core_trigger.h"
31 /* IDA to assign each registered device a unique id*/
32 static DEFINE_IDA(iio_ida);
34 static dev_t iio_devt;
36 #define IIO_DEV_MAX 256
37 struct bus_type iio_bus_type = {
40 EXPORT_SYMBOL(iio_bus_type);
42 static const char * const iio_data_type_name[] = {
44 [IIO_PROCESSED] = "input",
47 static const char * const iio_direction[] = {
52 static const char * const iio_chan_type_name_spec_shared[] = {
53 [IIO_VOLTAGE] = "voltage",
54 [IIO_CURRENT] = "current",
55 [IIO_POWER] = "power",
56 [IIO_ACCEL] = "accel",
57 [IIO_VOLTAGE_DIFF] = "voltage-voltage",
60 [IIO_LIGHT] = "illuminance",
61 [IIO_INTENSITY] = "intensity",
62 [IIO_PROXIMITY] = "proximity",
64 [IIO_INCLI] = "incli",
67 [IIO_TIMESTAMP] = "timestamp",
68 [IIO_CAPACITANCE] = "capacitance",
71 static const char * const iio_chan_type_name_spec_complex[] = {
72 [IIO_VOLTAGE_DIFF] = "voltage%d-voltage%d",
75 static const char * const iio_modifier_names[] = {
79 [IIO_MOD_LIGHT_BOTH] = "both",
80 [IIO_MOD_LIGHT_IR] = "ir",
83 /* relies on pairs of these shared then separate */
84 static const char * const iio_chan_info_postfix[] = {
85 [IIO_CHAN_INFO_SCALE_SHARED/2] = "scale",
86 [IIO_CHAN_INFO_OFFSET_SHARED/2] = "offset",
87 [IIO_CHAN_INFO_CALIBSCALE_SHARED/2] = "calibscale",
88 [IIO_CHAN_INFO_CALIBBIAS_SHARED/2] = "calibbias",
89 [IIO_CHAN_INFO_PEAK_SHARED/2] = "peak_raw",
90 [IIO_CHAN_INFO_PEAK_SCALE_SHARED/2] = "peak_scale",
91 [IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW_SHARED/2]
92 = "quadrature_correction_raw",
93 [IIO_CHAN_INFO_AVERAGE_RAW_SHARED/2] = "mean_raw",
97 * struct iio_detected_event_list - list element for events that have occurred
98 * @list: linked list header
99 * @ev: the event itself
101 struct iio_detected_event_list {
102 struct list_head list;
103 struct iio_event_data ev;
107 * struct iio_event_interface - chrdev interface for an event line
108 * @dev: device assocated with event interface
109 * @wait: wait queue to allow blocking reads of events
110 * @event_list_lock: mutex to protect the list of detected events
111 * @det_events: list of detected events
112 * @max_events: maximum number of events before new ones are dropped
113 * @current_events: number of events in detected list
114 * @flags: file operations related flags including busy flag.
116 struct iio_event_interface {
117 wait_queue_head_t wait;
118 struct mutex event_list_lock;
119 struct list_head det_events;
122 struct list_head dev_attr_list;
124 struct attribute_group group;
127 int iio_push_event(struct iio_dev *dev_info, u64 ev_code, s64 timestamp)
129 struct iio_event_interface *ev_int = dev_info->event_interface;
130 struct iio_detected_event_list *ev;
133 /* Does anyone care? */
134 mutex_lock(&ev_int->event_list_lock);
135 if (test_bit(IIO_BUSY_BIT_POS, &ev_int->flags)) {
136 if (ev_int->current_events == ev_int->max_events) {
137 mutex_unlock(&ev_int->event_list_lock);
140 ev = kmalloc(sizeof(*ev), GFP_KERNEL);
143 mutex_unlock(&ev_int->event_list_lock);
147 ev->ev.timestamp = timestamp;
149 list_add_tail(&ev->list, &ev_int->det_events);
150 ev_int->current_events++;
151 mutex_unlock(&ev_int->event_list_lock);
152 wake_up_interruptible(&ev_int->wait);
154 mutex_unlock(&ev_int->event_list_lock);
159 EXPORT_SYMBOL(iio_push_event);
162 /* This turns up an awful lot */
163 ssize_t iio_read_const_attr(struct device *dev,
164 struct device_attribute *attr,
167 return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string);
169 EXPORT_SYMBOL(iio_read_const_attr);
172 static ssize_t iio_event_chrdev_read(struct file *filep,
177 struct iio_event_interface *ev_int = filep->private_data;
178 struct iio_detected_event_list *el;
182 mutex_lock(&ev_int->event_list_lock);
183 if (list_empty(&ev_int->det_events)) {
184 if (filep->f_flags & O_NONBLOCK) {
186 goto error_mutex_unlock;
188 mutex_unlock(&ev_int->event_list_lock);
189 /* Blocking on device; waiting for something to be there */
190 ret = wait_event_interruptible(ev_int->wait,
195 /* Single access device so no one else can get the data */
196 mutex_lock(&ev_int->event_list_lock);
199 el = list_first_entry(&ev_int->det_events,
200 struct iio_detected_event_list,
203 if (copy_to_user(buf, &(el->ev), len)) {
205 goto error_mutex_unlock;
208 ev_int->current_events--;
209 mutex_unlock(&ev_int->event_list_lock);
215 mutex_unlock(&ev_int->event_list_lock);
221 static int iio_event_chrdev_release(struct inode *inode, struct file *filep)
223 struct iio_event_interface *ev_int = filep->private_data;
224 struct iio_detected_event_list *el, *t;
226 mutex_lock(&ev_int->event_list_lock);
227 clear_bit(IIO_BUSY_BIT_POS, &ev_int->flags);
229 * In order to maintain a clean state for reopening,
230 * clear out any awaiting events. The mask will prevent
231 * any new __iio_push_event calls running.
233 list_for_each_entry_safe(el, t, &ev_int->det_events, list) {
237 ev_int->current_events = 0;
238 mutex_unlock(&ev_int->event_list_lock);
243 static const struct file_operations iio_event_chrdev_fileops = {
244 .read = iio_event_chrdev_read,
245 .release = iio_event_chrdev_release,
246 .owner = THIS_MODULE,
247 .llseek = noop_llseek,
250 static int iio_event_getfd(struct iio_dev *indio_dev)
252 if (indio_dev->event_interface == NULL)
255 mutex_lock(&indio_dev->event_interface->event_list_lock);
256 if (test_and_set_bit(IIO_BUSY_BIT_POS,
257 &indio_dev->event_interface->flags)) {
258 mutex_unlock(&indio_dev->event_interface->event_list_lock);
261 mutex_unlock(&indio_dev->event_interface->event_list_lock);
262 return anon_inode_getfd("iio:event",
263 &iio_event_chrdev_fileops,
264 indio_dev->event_interface, O_RDONLY);
267 static int __init iio_init(void)
271 /* Register sysfs bus */
272 ret = bus_register(&iio_bus_type);
275 "%s could not register bus type\n",
280 ret = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio");
282 printk(KERN_ERR "%s: failed to allocate char dev region\n",
284 goto error_unregister_bus_type;
289 error_unregister_bus_type:
290 bus_unregister(&iio_bus_type);
295 static void __exit iio_exit(void)
298 unregister_chrdev_region(iio_devt, IIO_DEV_MAX);
299 bus_unregister(&iio_bus_type);
302 static ssize_t iio_read_channel_info(struct device *dev,
303 struct device_attribute *attr,
306 struct iio_dev *indio_dev = dev_get_drvdata(dev);
307 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
309 int ret = indio_dev->info->read_raw(indio_dev, this_attr->c,
310 &val, &val2, this_attr->address);
315 if (ret == IIO_VAL_INT)
316 return sprintf(buf, "%d\n", val);
317 else if (ret == IIO_VAL_INT_PLUS_MICRO) {
319 return sprintf(buf, "-%d.%06u\n", val, -val2);
321 return sprintf(buf, "%d.%06u\n", val, val2);
322 } else if (ret == IIO_VAL_INT_PLUS_NANO) {
324 return sprintf(buf, "-%d.%09u\n", val, -val2);
326 return sprintf(buf, "%d.%09u\n", val, val2);
331 static ssize_t iio_write_channel_info(struct device *dev,
332 struct device_attribute *attr,
336 struct iio_dev *indio_dev = dev_get_drvdata(dev);
337 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
338 int ret, integer = 0, fract = 0, fract_mult = 100000;
339 bool integer_part = true, negative = false;
341 /* Assumes decimal - precision based on number of digits */
342 if (!indio_dev->info->write_raw)
345 if (indio_dev->info->write_raw_get_fmt)
346 switch (indio_dev->info->write_raw_get_fmt(indio_dev,
347 this_attr->c, this_attr->address)) {
348 case IIO_VAL_INT_PLUS_MICRO:
351 case IIO_VAL_INT_PLUS_NANO:
352 fract_mult = 100000000;
364 if ('0' <= *buf && *buf <= '9') {
366 integer = integer*10 + *buf - '0';
368 fract += fract_mult*(*buf - '0');
373 } else if (*buf == '\n') {
374 if (*(buf + 1) == '\0')
378 } else if (*buf == '.') {
379 integer_part = false;
392 ret = indio_dev->info->write_raw(indio_dev, this_attr->c,
393 integer, fract, this_attr->address);
400 static int __iio_build_postfix(struct iio_chan_spec const *chan,
406 /* 3 options - generic, extend_name, modified - if generic, extend_name
407 * and modified cannot apply.*/
409 if (generic || (!chan->modified && !chan->extend_name)) {
410 all_post = kasprintf(GFP_KERNEL, "%s", postfix);
411 } else if (chan->modified) {
412 if (chan->extend_name)
413 all_post = kasprintf(GFP_KERNEL, "%s_%s_%s",
414 iio_modifier_names[chan->channel2],
418 all_post = kasprintf(GFP_KERNEL, "%s_%s",
419 iio_modifier_names[chan->channel2],
422 all_post = kasprintf(GFP_KERNEL, "%s_%s", chan->extend_name,
424 if (all_post == NULL)
431 int __iio_device_attr_init(struct device_attribute *dev_attr,
433 struct iio_chan_spec const *chan,
434 ssize_t (*readfunc)(struct device *dev,
435 struct device_attribute *attr,
437 ssize_t (*writefunc)(struct device *dev,
438 struct device_attribute *attr,
444 char *name_format, *full_postfix;
445 sysfs_attr_init(&dev_attr->attr);
446 ret = __iio_build_postfix(chan, generic, postfix, &full_postfix);
450 /* Special case for types that uses both channel numbers in naming */
451 if (chan->type == IIO_VOLTAGE_DIFF && !generic)
453 = kasprintf(GFP_KERNEL, "%s_%s_%s",
454 iio_direction[chan->output],
455 iio_chan_type_name_spec_complex[chan->type],
457 else if (generic || !chan->indexed)
459 = kasprintf(GFP_KERNEL, "%s_%s_%s",
460 iio_direction[chan->output],
461 iio_chan_type_name_spec_shared[chan->type],
465 = kasprintf(GFP_KERNEL, "%s_%s%d_%s",
466 iio_direction[chan->output],
467 iio_chan_type_name_spec_shared[chan->type],
471 if (name_format == NULL) {
473 goto error_free_full_postfix;
475 dev_attr->attr.name = kasprintf(GFP_KERNEL,
479 if (dev_attr->attr.name == NULL) {
481 goto error_free_name_format;
485 dev_attr->attr.mode |= S_IRUGO;
486 dev_attr->show = readfunc;
490 dev_attr->attr.mode |= S_IWUSR;
491 dev_attr->store = writefunc;
498 error_free_name_format:
500 error_free_full_postfix:
506 static void __iio_device_attr_deinit(struct device_attribute *dev_attr)
508 kfree(dev_attr->attr.name);
511 int __iio_add_chan_devattr(const char *postfix,
512 struct iio_chan_spec const *chan,
513 ssize_t (*readfunc)(struct device *dev,
514 struct device_attribute *attr,
516 ssize_t (*writefunc)(struct device *dev,
517 struct device_attribute *attr,
523 struct list_head *attr_list)
526 struct iio_dev_attr *iio_attr, *t;
528 iio_attr = kzalloc(sizeof *iio_attr, GFP_KERNEL);
529 if (iio_attr == NULL) {
533 ret = __iio_device_attr_init(&iio_attr->dev_attr,
535 readfunc, writefunc, generic);
537 goto error_iio_dev_attr_free;
539 iio_attr->address = mask;
540 list_for_each_entry(t, attr_list, l)
541 if (strcmp(t->dev_attr.attr.name,
542 iio_attr->dev_attr.attr.name) == 0) {
544 dev_err(dev, "tried to double register : %s\n",
545 t->dev_attr.attr.name);
547 goto error_device_attr_deinit;
549 list_add(&iio_attr->l, attr_list);
553 error_device_attr_deinit:
554 __iio_device_attr_deinit(&iio_attr->dev_attr);
555 error_iio_dev_attr_free:
561 static int iio_device_add_channel_sysfs(struct iio_dev *dev_info,
562 struct iio_chan_spec const *chan)
564 int ret, i, attrcount = 0;
566 if (chan->channel < 0)
569 ret = __iio_add_chan_devattr(iio_data_type_name[chan->processed_val],
571 &iio_read_channel_info,
573 &iio_write_channel_info : NULL),
577 &dev_info->channel_attr_list);
582 for_each_set_bit(i, &chan->info_mask, sizeof(long)*8) {
583 ret = __iio_add_chan_devattr(iio_chan_info_postfix[i/2],
585 &iio_read_channel_info,
586 &iio_write_channel_info,
590 &dev_info->channel_attr_list);
591 if (ret == -EBUSY && (i%2 == 0)) {
604 static void iio_device_remove_and_free_read_attr(struct iio_dev *dev_info,
605 struct iio_dev_attr *p)
607 kfree(p->dev_attr.attr.name);
611 static ssize_t iio_show_dev_name(struct device *dev,
612 struct device_attribute *attr,
615 struct iio_dev *indio_dev = dev_get_drvdata(dev);
616 return sprintf(buf, "%s\n", indio_dev->name);
619 static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL);
621 static int iio_device_register_sysfs(struct iio_dev *dev_info)
623 int i, ret = 0, attrcount, attrn, attrcount_orig = 0;
624 struct iio_dev_attr *p, *n;
625 struct attribute **attr;
627 /* First count elements in any existing group */
628 if (dev_info->info->attrs) {
629 attr = dev_info->info->attrs->attrs;
630 while (*attr++ != NULL)
633 attrcount = attrcount_orig;
635 * New channel registration method - relies on the fact a group does
636 * not need to be initialized if it is name is NULL.
638 INIT_LIST_HEAD(&dev_info->channel_attr_list);
639 if (dev_info->channels)
640 for (i = 0; i < dev_info->num_channels; i++) {
641 ret = iio_device_add_channel_sysfs(dev_info,
645 goto error_clear_attrs;
652 dev_info->chan_attr_group.attrs
653 = kzalloc(sizeof(dev_info->chan_attr_group.attrs[0])*
656 if (dev_info->chan_attr_group.attrs == NULL) {
658 goto error_clear_attrs;
660 /* Copy across original attributes */
661 if (dev_info->info->attrs)
662 memcpy(dev_info->chan_attr_group.attrs,
663 dev_info->info->attrs->attrs,
664 sizeof(dev_info->chan_attr_group.attrs[0])
666 attrn = attrcount_orig;
667 /* Add all elements from the list. */
668 list_for_each_entry(p, &dev_info->channel_attr_list, l)
669 dev_info->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr;
671 dev_info->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr;
673 dev_info->groups[dev_info->groupcounter++] =
674 &dev_info->chan_attr_group;
679 list_for_each_entry_safe(p, n,
680 &dev_info->channel_attr_list, l) {
682 iio_device_remove_and_free_read_attr(dev_info, p);
688 static void iio_device_unregister_sysfs(struct iio_dev *dev_info)
691 struct iio_dev_attr *p, *n;
693 list_for_each_entry_safe(p, n, &dev_info->channel_attr_list, l) {
695 iio_device_remove_and_free_read_attr(dev_info, p);
697 kfree(dev_info->chan_attr_group.attrs);
700 static const char * const iio_ev_type_text[] = {
701 [IIO_EV_TYPE_THRESH] = "thresh",
702 [IIO_EV_TYPE_MAG] = "mag",
703 [IIO_EV_TYPE_ROC] = "roc",
704 [IIO_EV_TYPE_THRESH_ADAPTIVE] = "thresh_adaptive",
705 [IIO_EV_TYPE_MAG_ADAPTIVE] = "mag_adaptive",
708 static const char * const iio_ev_dir_text[] = {
709 [IIO_EV_DIR_EITHER] = "either",
710 [IIO_EV_DIR_RISING] = "rising",
711 [IIO_EV_DIR_FALLING] = "falling"
714 static ssize_t iio_ev_state_store(struct device *dev,
715 struct device_attribute *attr,
719 struct iio_dev *indio_dev = dev_get_drvdata(dev);
720 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
724 ret = strtobool(buf, &val);
728 ret = indio_dev->info->write_event_config(indio_dev,
731 return (ret < 0) ? ret : len;
734 static ssize_t iio_ev_state_show(struct device *dev,
735 struct device_attribute *attr,
738 struct iio_dev *indio_dev = dev_get_drvdata(dev);
739 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
740 int val = indio_dev->info->read_event_config(indio_dev,
746 return sprintf(buf, "%d\n", val);
749 static ssize_t iio_ev_value_show(struct device *dev,
750 struct device_attribute *attr,
753 struct iio_dev *indio_dev = dev_get_drvdata(dev);
754 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
757 ret = indio_dev->info->read_event_value(indio_dev,
758 this_attr->address, &val);
762 return sprintf(buf, "%d\n", val);
765 static ssize_t iio_ev_value_store(struct device *dev,
766 struct device_attribute *attr,
770 struct iio_dev *indio_dev = dev_get_drvdata(dev);
771 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
775 ret = strict_strtoul(buf, 10, &val);
779 ret = indio_dev->info->write_event_value(indio_dev, this_attr->address,
787 static int iio_device_add_event_sysfs(struct iio_dev *dev_info,
788 struct iio_chan_spec const *chan)
790 int ret = 0, i, attrcount = 0;
793 if (!chan->event_mask)
796 for_each_set_bit(i, &chan->event_mask, sizeof(chan->event_mask)*8) {
797 postfix = kasprintf(GFP_KERNEL, "%s_%s_en",
798 iio_ev_type_text[i/IIO_EV_DIR_MAX],
799 iio_ev_dir_text[i%IIO_EV_DIR_MAX]);
800 if (postfix == NULL) {
805 mask = IIO_MOD_EVENT_CODE(chan->type, 0, chan->channel,
808 else if (chan->type == IIO_VOLTAGE_DIFF)
809 mask = IIO_MOD_EVENT_CODE(chan->type,
815 mask = IIO_UNMOD_EVENT_CODE(chan->type,
820 ret = __iio_add_chan_devattr(postfix,
827 &dev_info->event_interface->
833 postfix = kasprintf(GFP_KERNEL, "%s_%s_value",
834 iio_ev_type_text[i/IIO_EV_DIR_MAX],
835 iio_ev_dir_text[i%IIO_EV_DIR_MAX]);
836 if (postfix == NULL) {
840 ret = __iio_add_chan_devattr(postfix, chan,
846 &dev_info->event_interface->
858 static inline void __iio_remove_event_config_attrs(struct iio_dev *dev_info)
860 struct iio_dev_attr *p, *n;
861 list_for_each_entry_safe(p, n,
862 &dev_info->event_interface->
864 kfree(p->dev_attr.attr.name);
869 static inline int __iio_add_event_config_attrs(struct iio_dev *dev_info)
871 int j, ret, attrcount = 0;
873 INIT_LIST_HEAD(&dev_info->event_interface->dev_attr_list);
874 /* Dynically created from the channels array */
875 for (j = 0; j < dev_info->num_channels; j++) {
876 ret = iio_device_add_event_sysfs(dev_info,
877 &dev_info->channels[j]);
879 goto error_clear_attrs;
885 __iio_remove_event_config_attrs(dev_info);
890 static bool iio_check_for_dynamic_events(struct iio_dev *dev_info)
894 for (j = 0; j < dev_info->num_channels; j++)
895 if (dev_info->channels[j].event_mask != 0)
900 static void iio_setup_ev_int(struct iio_event_interface *ev_int)
902 mutex_init(&ev_int->event_list_lock);
903 /* discussion point - make this variable? */
904 ev_int->max_events = 10;
905 ev_int->current_events = 0;
906 INIT_LIST_HEAD(&ev_int->det_events);
907 init_waitqueue_head(&ev_int->wait);
910 static const char *iio_event_group_name = "events";
911 static int iio_device_register_eventset(struct iio_dev *dev_info)
913 struct iio_dev_attr *p;
914 int ret = 0, attrcount_orig = 0, attrcount, attrn;
915 struct attribute **attr;
917 if (!(dev_info->info->event_attrs ||
918 iio_check_for_dynamic_events(dev_info)))
921 dev_info->event_interface =
922 kzalloc(sizeof(struct iio_event_interface), GFP_KERNEL);
923 if (dev_info->event_interface == NULL) {
928 iio_setup_ev_int(dev_info->event_interface);
929 if (dev_info->info->event_attrs != NULL) {
930 attr = dev_info->info->event_attrs->attrs;
931 while (*attr++ != NULL)
934 attrcount = attrcount_orig;
935 if (dev_info->channels) {
936 ret = __iio_add_event_config_attrs(dev_info);
938 goto error_free_setup_event_lines;
942 dev_info->event_interface->group.name = iio_event_group_name;
943 dev_info->event_interface->group.attrs =
944 kzalloc(sizeof(dev_info->event_interface->group.attrs[0])
947 if (dev_info->event_interface->group.attrs == NULL) {
949 goto error_free_setup_event_lines;
951 if (dev_info->info->event_attrs)
952 memcpy(dev_info->event_interface->group.attrs,
953 dev_info->info->event_attrs->attrs,
954 sizeof(dev_info->event_interface->group.attrs[0])
956 attrn = attrcount_orig;
957 /* Add all elements from the list. */
958 list_for_each_entry(p,
959 &dev_info->event_interface->dev_attr_list,
961 dev_info->event_interface->group.attrs[attrn++] =
964 dev_info->groups[dev_info->groupcounter++] =
965 &dev_info->event_interface->group;
969 error_free_setup_event_lines:
970 __iio_remove_event_config_attrs(dev_info);
971 kfree(dev_info->event_interface);
977 static void iio_device_unregister_eventset(struct iio_dev *dev_info)
979 if (dev_info->event_interface == NULL)
981 __iio_remove_event_config_attrs(dev_info);
982 kfree(dev_info->event_interface->group.attrs);
983 kfree(dev_info->event_interface);
986 static void iio_dev_release(struct device *device)
988 struct iio_dev *dev_info = container_of(device, struct iio_dev, dev);
989 cdev_del(&dev_info->chrdev);
990 if (dev_info->modes & INDIO_RING_TRIGGERED)
991 iio_device_unregister_trigger_consumer(dev_info);
992 iio_device_unregister_eventset(dev_info);
993 iio_device_unregister_sysfs(dev_info);
994 ida_simple_remove(&iio_ida, dev_info->id);
998 static struct device_type iio_dev_type = {
999 .name = "iio_device",
1000 .release = iio_dev_release,
1003 struct iio_dev *iio_allocate_device(int sizeof_priv)
1005 struct iio_dev *dev;
1008 alloc_size = sizeof(struct iio_dev);
1010 alloc_size = ALIGN(alloc_size, IIO_ALIGN);
1011 alloc_size += sizeof_priv;
1013 /* ensure 32-byte alignment of whole construct ? */
1014 alloc_size += IIO_ALIGN - 1;
1016 dev = kzalloc(alloc_size, GFP_KERNEL);
1019 dev->dev.groups = dev->groups;
1020 dev->dev.type = &iio_dev_type;
1021 dev->dev.bus = &iio_bus_type;
1022 device_initialize(&dev->dev);
1023 dev_set_drvdata(&dev->dev, (void *)dev);
1024 mutex_init(&dev->mlock);
1029 EXPORT_SYMBOL(iio_allocate_device);
1031 void iio_free_device(struct iio_dev *dev)
1036 EXPORT_SYMBOL(iio_free_device);
1039 * iio_chrdev_open() - chrdev file open for ring buffer access and ioctls
1041 static int iio_chrdev_open(struct inode *inode, struct file *filp)
1043 struct iio_dev *dev_info = container_of(inode->i_cdev,
1044 struct iio_dev, chrdev);
1045 filp->private_data = dev_info;
1046 iio_chrdev_ring_open(dev_info);
1051 * iio_chrdev_release() - chrdev file close ring buffer access and ioctls
1053 static int iio_chrdev_release(struct inode *inode, struct file *filp)
1055 iio_chrdev_ring_release(container_of(inode->i_cdev,
1056 struct iio_dev, chrdev));
1060 /* Somewhat of a cross file organization violation - ioctls here are actually
1062 static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1064 struct iio_dev *indio_dev = filp->private_data;
1065 int __user *ip = (int __user *)arg;
1068 if (cmd == IIO_GET_EVENT_FD_IOCTL) {
1069 fd = iio_event_getfd(indio_dev);
1070 if (copy_to_user(ip, &fd, sizeof(fd)))
1077 static const struct file_operations iio_ring_fileops = {
1078 .read = iio_ring_read_first_n_outer_addr,
1079 .release = iio_chrdev_release,
1080 .open = iio_chrdev_open,
1081 .poll = iio_ring_poll_addr,
1082 .owner = THIS_MODULE,
1083 .llseek = noop_llseek,
1084 .unlocked_ioctl = iio_ioctl,
1085 .compat_ioctl = iio_ioctl,
1088 int iio_device_register(struct iio_dev *dev_info)
1092 dev_info->id = ida_simple_get(&iio_ida, 0, 0, GFP_KERNEL);
1093 if (dev_info->id < 0) {
1095 dev_err(&dev_info->dev, "Failed to get id\n");
1098 dev_set_name(&dev_info->dev, "iio:device%d", dev_info->id);
1100 /* configure elements for the chrdev */
1101 dev_info->dev.devt = MKDEV(MAJOR(iio_devt), dev_info->id);
1103 ret = iio_device_register_sysfs(dev_info);
1105 dev_err(dev_info->dev.parent,
1106 "Failed to register sysfs interfaces\n");
1107 goto error_free_ida;
1109 ret = iio_device_register_eventset(dev_info);
1111 dev_err(dev_info->dev.parent,
1112 "Failed to register event set\n");
1113 goto error_free_sysfs;
1115 if (dev_info->modes & INDIO_RING_TRIGGERED)
1116 iio_device_register_trigger_consumer(dev_info);
1118 ret = device_add(&dev_info->dev);
1120 goto error_unreg_eventset;
1121 cdev_init(&dev_info->chrdev, &iio_ring_fileops);
1122 dev_info->chrdev.owner = dev_info->info->driver_module;
1123 ret = cdev_add(&dev_info->chrdev, dev_info->dev.devt, 1);
1125 goto error_del_device;
1129 device_del(&dev_info->dev);
1130 error_unreg_eventset:
1131 iio_device_unregister_eventset(dev_info);
1133 iio_device_unregister_sysfs(dev_info);
1135 ida_simple_remove(&iio_ida, dev_info->id);
1139 EXPORT_SYMBOL(iio_device_register);
1141 void iio_device_unregister(struct iio_dev *dev_info)
1143 device_unregister(&dev_info->dev);
1145 EXPORT_SYMBOL(iio_device_unregister);
1146 subsys_initcall(iio_init);
1147 module_exit(iio_exit);
1149 MODULE_AUTHOR("Jonathan Cameron <jic23@cam.ac.uk>");
1150 MODULE_DESCRIPTION("Industrial I/O core");
1151 MODULE_LICENSE("GPL");