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[] = {
53 [IIO_VOLTAGE] = "voltage",
54 [IIO_CURRENT] = "current",
55 [IIO_POWER] = "power",
56 [IIO_ACCEL] = "accel",
57 [IIO_ANGL_VEL] = "anglvel",
59 [IIO_LIGHT] = "illuminance",
60 [IIO_INTENSITY] = "intensity",
61 [IIO_PROXIMITY] = "proximity",
63 [IIO_INCLI] = "incli",
66 [IIO_TIMESTAMP] = "timestamp",
67 [IIO_CAPACITANCE] = "capacitance",
70 static const char * const iio_modifier_names[] = {
74 [IIO_MOD_LIGHT_BOTH] = "both",
75 [IIO_MOD_LIGHT_IR] = "ir",
78 /* relies on pairs of these shared then separate */
79 static const char * const iio_chan_info_postfix[] = {
80 [IIO_CHAN_INFO_SCALE] = "scale",
81 [IIO_CHAN_INFO_OFFSET] = "offset",
82 [IIO_CHAN_INFO_CALIBSCALE] = "calibscale",
83 [IIO_CHAN_INFO_CALIBBIAS] = "calibbias",
84 [IIO_CHAN_INFO_PEAK] = "peak_raw",
85 [IIO_CHAN_INFO_PEAK_SCALE] = "peak_scale",
86 [IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW] = "quadrature_correction_raw",
87 [IIO_CHAN_INFO_AVERAGE_RAW] = "mean_raw",
88 [IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY]
89 = "filter_low_pass_3db_frequency",
93 * struct iio_detected_event_list - list element for events that have occurred
94 * @list: linked list header
95 * @ev: the event itself
97 struct iio_detected_event_list {
98 struct list_head list;
99 struct iio_event_data ev;
103 * struct iio_event_interface - chrdev interface for an event line
104 * @dev: device assocated with event interface
105 * @wait: wait queue to allow blocking reads of events
106 * @event_list_lock: mutex to protect the list of detected events
107 * @det_events: list of detected events
108 * @max_events: maximum number of events before new ones are dropped
109 * @current_events: number of events in detected list
110 * @flags: file operations related flags including busy flag.
112 struct iio_event_interface {
113 wait_queue_head_t wait;
114 struct mutex event_list_lock;
115 struct list_head det_events;
118 struct list_head dev_attr_list;
120 struct attribute_group group;
123 int iio_push_event(struct iio_dev *indio_dev, u64 ev_code, s64 timestamp)
125 struct iio_event_interface *ev_int = indio_dev->event_interface;
126 struct iio_detected_event_list *ev;
129 /* Does anyone care? */
130 mutex_lock(&ev_int->event_list_lock);
131 if (test_bit(IIO_BUSY_BIT_POS, &ev_int->flags)) {
132 if (ev_int->current_events == ev_int->max_events) {
133 mutex_unlock(&ev_int->event_list_lock);
136 ev = kmalloc(sizeof(*ev), GFP_KERNEL);
139 mutex_unlock(&ev_int->event_list_lock);
143 ev->ev.timestamp = timestamp;
145 list_add_tail(&ev->list, &ev_int->det_events);
146 ev_int->current_events++;
147 mutex_unlock(&ev_int->event_list_lock);
148 wake_up_interruptible(&ev_int->wait);
150 mutex_unlock(&ev_int->event_list_lock);
155 EXPORT_SYMBOL(iio_push_event);
157 /* This turns up an awful lot */
158 ssize_t iio_read_const_attr(struct device *dev,
159 struct device_attribute *attr,
162 return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string);
164 EXPORT_SYMBOL(iio_read_const_attr);
166 static ssize_t iio_event_chrdev_read(struct file *filep,
171 struct iio_event_interface *ev_int = filep->private_data;
172 struct iio_detected_event_list *el;
173 size_t len = sizeof(el->ev);
179 mutex_lock(&ev_int->event_list_lock);
180 if (list_empty(&ev_int->det_events)) {
181 if (filep->f_flags & O_NONBLOCK) {
183 goto error_mutex_unlock;
185 mutex_unlock(&ev_int->event_list_lock);
186 /* Blocking on device; waiting for something to be there */
187 ret = wait_event_interruptible(ev_int->wait,
192 /* Single access device so no one else can get the data */
193 mutex_lock(&ev_int->event_list_lock);
196 el = list_first_entry(&ev_int->det_events,
197 struct iio_detected_event_list,
199 if (copy_to_user(buf, &(el->ev), len)) {
201 goto error_mutex_unlock;
204 ev_int->current_events--;
205 mutex_unlock(&ev_int->event_list_lock);
211 mutex_unlock(&ev_int->event_list_lock);
217 static int iio_event_chrdev_release(struct inode *inode, struct file *filep)
219 struct iio_event_interface *ev_int = filep->private_data;
220 struct iio_detected_event_list *el, *t;
222 mutex_lock(&ev_int->event_list_lock);
223 clear_bit(IIO_BUSY_BIT_POS, &ev_int->flags);
225 * In order to maintain a clean state for reopening,
226 * clear out any awaiting events. The mask will prevent
227 * any new __iio_push_event calls running.
229 list_for_each_entry_safe(el, t, &ev_int->det_events, list) {
233 ev_int->current_events = 0;
234 mutex_unlock(&ev_int->event_list_lock);
239 static const struct file_operations iio_event_chrdev_fileops = {
240 .read = iio_event_chrdev_read,
241 .release = iio_event_chrdev_release,
242 .owner = THIS_MODULE,
243 .llseek = noop_llseek,
246 static int iio_event_getfd(struct iio_dev *indio_dev)
250 if (indio_dev->event_interface == NULL)
253 mutex_lock(&indio_dev->event_interface->event_list_lock);
254 if (test_and_set_bit(IIO_BUSY_BIT_POS,
255 &indio_dev->event_interface->flags)) {
256 mutex_unlock(&indio_dev->event_interface->event_list_lock);
259 mutex_unlock(&indio_dev->event_interface->event_list_lock);
260 fd = anon_inode_getfd("iio:event",
261 &iio_event_chrdev_fileops,
262 indio_dev->event_interface, O_RDONLY);
264 mutex_lock(&indio_dev->event_interface->event_list_lock);
265 clear_bit(IIO_BUSY_BIT_POS, &indio_dev->event_interface->flags);
266 mutex_unlock(&indio_dev->event_interface->event_list_lock);
271 static int __init iio_init(void)
275 /* Register sysfs bus */
276 ret = bus_register(&iio_bus_type);
279 "%s could not register bus type\n",
284 ret = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio");
286 printk(KERN_ERR "%s: failed to allocate char dev region\n",
288 goto error_unregister_bus_type;
293 error_unregister_bus_type:
294 bus_unregister(&iio_bus_type);
299 static void __exit iio_exit(void)
302 unregister_chrdev_region(iio_devt, IIO_DEV_MAX);
303 bus_unregister(&iio_bus_type);
306 static ssize_t iio_read_channel_info(struct device *dev,
307 struct device_attribute *attr,
310 struct iio_dev *indio_dev = dev_get_drvdata(dev);
311 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
313 int ret = indio_dev->info->read_raw(indio_dev, this_attr->c,
314 &val, &val2, this_attr->address);
319 if (ret == IIO_VAL_INT)
320 return sprintf(buf, "%d\n", val);
321 else if (ret == IIO_VAL_INT_PLUS_MICRO) {
323 return sprintf(buf, "-%d.%06u\n", val, -val2);
325 return sprintf(buf, "%d.%06u\n", val, val2);
326 } else if (ret == IIO_VAL_INT_PLUS_NANO) {
328 return sprintf(buf, "-%d.%09u\n", val, -val2);
330 return sprintf(buf, "%d.%09u\n", val, val2);
335 static ssize_t iio_write_channel_info(struct device *dev,
336 struct device_attribute *attr,
340 struct iio_dev *indio_dev = dev_get_drvdata(dev);
341 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
342 int ret, integer = 0, fract = 0, fract_mult = 100000;
343 bool integer_part = true, negative = false;
345 /* Assumes decimal - precision based on number of digits */
346 if (!indio_dev->info->write_raw)
349 if (indio_dev->info->write_raw_get_fmt)
350 switch (indio_dev->info->write_raw_get_fmt(indio_dev,
351 this_attr->c, this_attr->address)) {
352 case IIO_VAL_INT_PLUS_MICRO:
355 case IIO_VAL_INT_PLUS_NANO:
356 fract_mult = 100000000;
368 if ('0' <= *buf && *buf <= '9') {
370 integer = integer*10 + *buf - '0';
372 fract += fract_mult*(*buf - '0');
377 } else if (*buf == '\n') {
378 if (*(buf + 1) == '\0')
382 } else if (*buf == '.') {
383 integer_part = false;
396 ret = indio_dev->info->write_raw(indio_dev, this_attr->c,
397 integer, fract, this_attr->address);
405 int __iio_device_attr_init(struct device_attribute *dev_attr,
407 struct iio_chan_spec const *chan,
408 ssize_t (*readfunc)(struct device *dev,
409 struct device_attribute *attr,
411 ssize_t (*writefunc)(struct device *dev,
412 struct device_attribute *attr,
418 char *name_format, *full_postfix;
419 sysfs_attr_init(&dev_attr->attr);
421 /* Build up postfix of <extend_name>_<modifier>_postfix */
422 if (chan->modified && !generic) {
423 if (chan->extend_name)
424 full_postfix = kasprintf(GFP_KERNEL, "%s_%s_%s",
425 iio_modifier_names[chan
430 full_postfix = kasprintf(GFP_KERNEL, "%s_%s",
431 iio_modifier_names[chan
435 if (chan->extend_name == NULL)
436 full_postfix = kstrdup(postfix, GFP_KERNEL);
438 full_postfix = kasprintf(GFP_KERNEL,
443 if (full_postfix == NULL) {
448 if (chan->differential) { /* Differential can not have modifier */
451 = kasprintf(GFP_KERNEL, "%s_%s-%s_%s",
452 iio_direction[chan->output],
453 iio_chan_type_name_spec[chan->type],
454 iio_chan_type_name_spec[chan->type],
456 else if (chan->indexed)
458 = kasprintf(GFP_KERNEL, "%s_%s%d-%s%d_%s",
459 iio_direction[chan->output],
460 iio_chan_type_name_spec[chan->type],
462 iio_chan_type_name_spec[chan->type],
466 WARN_ON("Differential channels must be indexed\n");
468 goto error_free_full_postfix;
470 } else { /* Single ended */
473 = kasprintf(GFP_KERNEL, "%s_%s_%s",
474 iio_direction[chan->output],
475 iio_chan_type_name_spec[chan->type],
477 else if (chan->indexed)
479 = kasprintf(GFP_KERNEL, "%s_%s%d_%s",
480 iio_direction[chan->output],
481 iio_chan_type_name_spec[chan->type],
486 = kasprintf(GFP_KERNEL, "%s_%s_%s",
487 iio_direction[chan->output],
488 iio_chan_type_name_spec[chan->type],
491 if (name_format == NULL) {
493 goto error_free_full_postfix;
495 dev_attr->attr.name = kasprintf(GFP_KERNEL,
499 if (dev_attr->attr.name == NULL) {
501 goto error_free_name_format;
505 dev_attr->attr.mode |= S_IRUGO;
506 dev_attr->show = readfunc;
510 dev_attr->attr.mode |= S_IWUSR;
511 dev_attr->store = writefunc;
518 error_free_name_format:
520 error_free_full_postfix:
526 static void __iio_device_attr_deinit(struct device_attribute *dev_attr)
528 kfree(dev_attr->attr.name);
531 int __iio_add_chan_devattr(const char *postfix,
532 struct iio_chan_spec const *chan,
533 ssize_t (*readfunc)(struct device *dev,
534 struct device_attribute *attr,
536 ssize_t (*writefunc)(struct device *dev,
537 struct device_attribute *attr,
543 struct list_head *attr_list)
546 struct iio_dev_attr *iio_attr, *t;
548 iio_attr = kzalloc(sizeof *iio_attr, GFP_KERNEL);
549 if (iio_attr == NULL) {
553 ret = __iio_device_attr_init(&iio_attr->dev_attr,
555 readfunc, writefunc, generic);
557 goto error_iio_dev_attr_free;
559 iio_attr->address = mask;
560 list_for_each_entry(t, attr_list, l)
561 if (strcmp(t->dev_attr.attr.name,
562 iio_attr->dev_attr.attr.name) == 0) {
564 dev_err(dev, "tried to double register : %s\n",
565 t->dev_attr.attr.name);
567 goto error_device_attr_deinit;
569 list_add(&iio_attr->l, attr_list);
573 error_device_attr_deinit:
574 __iio_device_attr_deinit(&iio_attr->dev_attr);
575 error_iio_dev_attr_free:
581 static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev,
582 struct iio_chan_spec const *chan)
584 int ret, i, attrcount = 0;
586 if (chan->channel < 0)
589 ret = __iio_add_chan_devattr(iio_data_type_name[chan->processed_val],
591 &iio_read_channel_info,
593 &iio_write_channel_info : NULL),
597 &indio_dev->channel_attr_list);
602 for_each_set_bit(i, &chan->info_mask, sizeof(long)*8) {
603 ret = __iio_add_chan_devattr(iio_chan_info_postfix[i/2],
605 &iio_read_channel_info,
606 &iio_write_channel_info,
610 &indio_dev->channel_attr_list);
611 if (ret == -EBUSY && (i%2 == 0)) {
624 static void iio_device_remove_and_free_read_attr(struct iio_dev *indio_dev,
625 struct iio_dev_attr *p)
627 kfree(p->dev_attr.attr.name);
631 static ssize_t iio_show_dev_name(struct device *dev,
632 struct device_attribute *attr,
635 struct iio_dev *indio_dev = dev_get_drvdata(dev);
636 return sprintf(buf, "%s\n", indio_dev->name);
639 static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL);
641 static int iio_device_register_sysfs(struct iio_dev *indio_dev)
643 int i, ret = 0, attrcount, attrn, attrcount_orig = 0;
644 struct iio_dev_attr *p, *n;
645 struct attribute **attr;
647 /* First count elements in any existing group */
648 if (indio_dev->info->attrs) {
649 attr = indio_dev->info->attrs->attrs;
650 while (*attr++ != NULL)
653 attrcount = attrcount_orig;
655 * New channel registration method - relies on the fact a group does
656 * not need to be initialized if it is name is NULL.
658 INIT_LIST_HEAD(&indio_dev->channel_attr_list);
659 if (indio_dev->channels)
660 for (i = 0; i < indio_dev->num_channels; i++) {
661 ret = iio_device_add_channel_sysfs(indio_dev,
665 goto error_clear_attrs;
672 indio_dev->chan_attr_group.attrs
673 = kzalloc(sizeof(indio_dev->chan_attr_group.attrs[0])*
676 if (indio_dev->chan_attr_group.attrs == NULL) {
678 goto error_clear_attrs;
680 /* Copy across original attributes */
681 if (indio_dev->info->attrs)
682 memcpy(indio_dev->chan_attr_group.attrs,
683 indio_dev->info->attrs->attrs,
684 sizeof(indio_dev->chan_attr_group.attrs[0])
686 attrn = attrcount_orig;
687 /* Add all elements from the list. */
688 list_for_each_entry(p, &indio_dev->channel_attr_list, l)
689 indio_dev->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr;
691 indio_dev->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr;
693 indio_dev->groups[indio_dev->groupcounter++] =
694 &indio_dev->chan_attr_group;
699 list_for_each_entry_safe(p, n,
700 &indio_dev->channel_attr_list, l) {
702 iio_device_remove_and_free_read_attr(indio_dev, p);
708 static void iio_device_unregister_sysfs(struct iio_dev *indio_dev)
711 struct iio_dev_attr *p, *n;
713 list_for_each_entry_safe(p, n, &indio_dev->channel_attr_list, l) {
715 iio_device_remove_and_free_read_attr(indio_dev, p);
717 kfree(indio_dev->chan_attr_group.attrs);
720 static const char * const iio_ev_type_text[] = {
721 [IIO_EV_TYPE_THRESH] = "thresh",
722 [IIO_EV_TYPE_MAG] = "mag",
723 [IIO_EV_TYPE_ROC] = "roc",
724 [IIO_EV_TYPE_THRESH_ADAPTIVE] = "thresh_adaptive",
725 [IIO_EV_TYPE_MAG_ADAPTIVE] = "mag_adaptive",
728 static const char * const iio_ev_dir_text[] = {
729 [IIO_EV_DIR_EITHER] = "either",
730 [IIO_EV_DIR_RISING] = "rising",
731 [IIO_EV_DIR_FALLING] = "falling"
734 static ssize_t iio_ev_state_store(struct device *dev,
735 struct device_attribute *attr,
739 struct iio_dev *indio_dev = dev_get_drvdata(dev);
740 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
744 ret = strtobool(buf, &val);
748 ret = indio_dev->info->write_event_config(indio_dev,
751 return (ret < 0) ? ret : len;
754 static ssize_t iio_ev_state_show(struct device *dev,
755 struct device_attribute *attr,
758 struct iio_dev *indio_dev = dev_get_drvdata(dev);
759 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
760 int val = indio_dev->info->read_event_config(indio_dev,
766 return sprintf(buf, "%d\n", val);
769 static ssize_t iio_ev_value_show(struct device *dev,
770 struct device_attribute *attr,
773 struct iio_dev *indio_dev = dev_get_drvdata(dev);
774 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
777 ret = indio_dev->info->read_event_value(indio_dev,
778 this_attr->address, &val);
782 return sprintf(buf, "%d\n", val);
785 static ssize_t iio_ev_value_store(struct device *dev,
786 struct device_attribute *attr,
790 struct iio_dev *indio_dev = dev_get_drvdata(dev);
791 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
795 if (!indio_dev->info->write_event_value)
798 ret = strict_strtoul(buf, 10, &val);
802 ret = indio_dev->info->write_event_value(indio_dev, this_attr->address,
810 static int iio_device_add_event_sysfs(struct iio_dev *indio_dev,
811 struct iio_chan_spec const *chan)
813 int ret = 0, i, attrcount = 0;
816 if (!chan->event_mask)
819 for_each_set_bit(i, &chan->event_mask, sizeof(chan->event_mask)*8) {
820 postfix = kasprintf(GFP_KERNEL, "%s_%s_en",
821 iio_ev_type_text[i/IIO_EV_DIR_MAX],
822 iio_ev_dir_text[i%IIO_EV_DIR_MAX]);
823 if (postfix == NULL) {
828 mask = IIO_MOD_EVENT_CODE(chan->type, 0, chan->channel,
831 else if (chan->differential)
832 mask = IIO_EVENT_CODE(chan->type,
840 mask = IIO_UNMOD_EVENT_CODE(chan->type,
845 ret = __iio_add_chan_devattr(postfix,
852 &indio_dev->event_interface->
858 postfix = kasprintf(GFP_KERNEL, "%s_%s_value",
859 iio_ev_type_text[i/IIO_EV_DIR_MAX],
860 iio_ev_dir_text[i%IIO_EV_DIR_MAX]);
861 if (postfix == NULL) {
865 ret = __iio_add_chan_devattr(postfix, chan,
871 &indio_dev->event_interface->
883 static inline void __iio_remove_event_config_attrs(struct iio_dev *indio_dev)
885 struct iio_dev_attr *p, *n;
886 list_for_each_entry_safe(p, n,
887 &indio_dev->event_interface->
889 kfree(p->dev_attr.attr.name);
894 static inline int __iio_add_event_config_attrs(struct iio_dev *indio_dev)
896 int j, ret, attrcount = 0;
898 INIT_LIST_HEAD(&indio_dev->event_interface->dev_attr_list);
899 /* Dynically created from the channels array */
900 for (j = 0; j < indio_dev->num_channels; j++) {
901 ret = iio_device_add_event_sysfs(indio_dev,
902 &indio_dev->channels[j]);
904 goto error_clear_attrs;
910 __iio_remove_event_config_attrs(indio_dev);
915 static bool iio_check_for_dynamic_events(struct iio_dev *indio_dev)
919 for (j = 0; j < indio_dev->num_channels; j++)
920 if (indio_dev->channels[j].event_mask != 0)
925 static void iio_setup_ev_int(struct iio_event_interface *ev_int)
927 mutex_init(&ev_int->event_list_lock);
928 /* discussion point - make this variable? */
929 ev_int->max_events = 10;
930 ev_int->current_events = 0;
931 INIT_LIST_HEAD(&ev_int->det_events);
932 init_waitqueue_head(&ev_int->wait);
935 static const char *iio_event_group_name = "events";
936 static int iio_device_register_eventset(struct iio_dev *indio_dev)
938 struct iio_dev_attr *p;
939 int ret = 0, attrcount_orig = 0, attrcount, attrn;
940 struct attribute **attr;
942 if (!(indio_dev->info->event_attrs ||
943 iio_check_for_dynamic_events(indio_dev)))
946 indio_dev->event_interface =
947 kzalloc(sizeof(struct iio_event_interface), GFP_KERNEL);
948 if (indio_dev->event_interface == NULL) {
953 iio_setup_ev_int(indio_dev->event_interface);
954 if (indio_dev->info->event_attrs != NULL) {
955 attr = indio_dev->info->event_attrs->attrs;
956 while (*attr++ != NULL)
959 attrcount = attrcount_orig;
960 if (indio_dev->channels) {
961 ret = __iio_add_event_config_attrs(indio_dev);
963 goto error_free_setup_event_lines;
967 indio_dev->event_interface->group.name = iio_event_group_name;
968 indio_dev->event_interface->group.attrs =
969 kzalloc(sizeof(indio_dev->event_interface->group.attrs[0])
972 if (indio_dev->event_interface->group.attrs == NULL) {
974 goto error_free_setup_event_lines;
976 if (indio_dev->info->event_attrs)
977 memcpy(indio_dev->event_interface->group.attrs,
978 indio_dev->info->event_attrs->attrs,
979 sizeof(indio_dev->event_interface->group.attrs[0])
981 attrn = attrcount_orig;
982 /* Add all elements from the list. */
983 list_for_each_entry(p,
984 &indio_dev->event_interface->dev_attr_list,
986 indio_dev->event_interface->group.attrs[attrn++] =
988 indio_dev->groups[indio_dev->groupcounter++] =
989 &indio_dev->event_interface->group;
993 error_free_setup_event_lines:
994 __iio_remove_event_config_attrs(indio_dev);
995 kfree(indio_dev->event_interface);
1001 static void iio_device_unregister_eventset(struct iio_dev *indio_dev)
1003 if (indio_dev->event_interface == NULL)
1005 __iio_remove_event_config_attrs(indio_dev);
1006 kfree(indio_dev->event_interface->group.attrs);
1007 kfree(indio_dev->event_interface);
1010 static void iio_dev_release(struct device *device)
1012 struct iio_dev *indio_dev = container_of(device, struct iio_dev, dev);
1013 cdev_del(&indio_dev->chrdev);
1014 if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
1015 iio_device_unregister_trigger_consumer(indio_dev);
1016 iio_device_unregister_eventset(indio_dev);
1017 iio_device_unregister_sysfs(indio_dev);
1020 static struct device_type iio_dev_type = {
1021 .name = "iio_device",
1022 .release = iio_dev_release,
1025 struct iio_dev *iio_allocate_device(int sizeof_priv)
1027 struct iio_dev *dev;
1030 alloc_size = sizeof(struct iio_dev);
1032 alloc_size = ALIGN(alloc_size, IIO_ALIGN);
1033 alloc_size += sizeof_priv;
1035 /* ensure 32-byte alignment of whole construct ? */
1036 alloc_size += IIO_ALIGN - 1;
1038 dev = kzalloc(alloc_size, GFP_KERNEL);
1041 dev->dev.groups = dev->groups;
1042 dev->dev.type = &iio_dev_type;
1043 dev->dev.bus = &iio_bus_type;
1044 device_initialize(&dev->dev);
1045 dev_set_drvdata(&dev->dev, (void *)dev);
1046 mutex_init(&dev->mlock);
1048 dev->id = ida_simple_get(&iio_ida, 0, 0, GFP_KERNEL);
1050 /* cannot use a dev_err as the name isn't available */
1051 printk(KERN_ERR "Failed to get id\n");
1055 dev_set_name(&dev->dev, "iio:device%d", dev->id);
1060 EXPORT_SYMBOL(iio_allocate_device);
1062 void iio_free_device(struct iio_dev *dev)
1065 ida_simple_remove(&iio_ida, dev->id);
1069 EXPORT_SYMBOL(iio_free_device);
1072 * iio_chrdev_open() - chrdev file open for buffer access and ioctls
1074 static int iio_chrdev_open(struct inode *inode, struct file *filp)
1076 struct iio_dev *indio_dev = container_of(inode->i_cdev,
1077 struct iio_dev, chrdev);
1078 filp->private_data = indio_dev;
1080 return iio_chrdev_buffer_open(indio_dev);
1084 * iio_chrdev_release() - chrdev file close buffer access and ioctls
1086 static int iio_chrdev_release(struct inode *inode, struct file *filp)
1088 iio_chrdev_buffer_release(container_of(inode->i_cdev,
1089 struct iio_dev, chrdev));
1093 /* Somewhat of a cross file organization violation - ioctls here are actually
1095 static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1097 struct iio_dev *indio_dev = filp->private_data;
1098 int __user *ip = (int __user *)arg;
1101 if (cmd == IIO_GET_EVENT_FD_IOCTL) {
1102 fd = iio_event_getfd(indio_dev);
1103 if (copy_to_user(ip, &fd, sizeof(fd)))
1110 static const struct file_operations iio_buffer_fileops = {
1111 .read = iio_buffer_read_first_n_outer_addr,
1112 .release = iio_chrdev_release,
1113 .open = iio_chrdev_open,
1114 .poll = iio_buffer_poll_addr,
1115 .owner = THIS_MODULE,
1116 .llseek = noop_llseek,
1117 .unlocked_ioctl = iio_ioctl,
1118 .compat_ioctl = iio_ioctl,
1121 int iio_device_register(struct iio_dev *indio_dev)
1125 /* configure elements for the chrdev */
1126 indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), indio_dev->id);
1128 ret = iio_device_register_sysfs(indio_dev);
1130 dev_err(indio_dev->dev.parent,
1131 "Failed to register sysfs interfaces\n");
1134 ret = iio_device_register_eventset(indio_dev);
1136 dev_err(indio_dev->dev.parent,
1137 "Failed to register event set\n");
1138 goto error_free_sysfs;
1140 if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
1141 iio_device_register_trigger_consumer(indio_dev);
1143 ret = device_add(&indio_dev->dev);
1145 goto error_unreg_eventset;
1146 cdev_init(&indio_dev->chrdev, &iio_buffer_fileops);
1147 indio_dev->chrdev.owner = indio_dev->info->driver_module;
1148 ret = cdev_add(&indio_dev->chrdev, indio_dev->dev.devt, 1);
1150 goto error_del_device;
1154 device_del(&indio_dev->dev);
1155 error_unreg_eventset:
1156 iio_device_unregister_eventset(indio_dev);
1158 iio_device_unregister_sysfs(indio_dev);
1162 EXPORT_SYMBOL(iio_device_register);
1164 void iio_device_unregister(struct iio_dev *indio_dev)
1166 device_unregister(&indio_dev->dev);
1168 EXPORT_SYMBOL(iio_device_unregister);
1169 subsys_initcall(iio_init);
1170 module_exit(iio_exit);
1172 MODULE_AUTHOR("Jonathan Cameron <jic23@cam.ac.uk>");
1173 MODULE_DESCRIPTION("Industrial I/O core");
1174 MODULE_LICENSE("GPL");