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_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",
86 [IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW_SHARED
/2]
87 = "quadrature_correction_raw",
88 [IIO_CHAN_INFO_AVERAGE_RAW_SHARED
/2] = "mean_raw",
92 * struct iio_detected_event_list - list element for events that have occurred
93 * @list: linked list header
94 * @ev: the event itself
96 struct iio_detected_event_list
{
97 struct list_head list
;
98 struct iio_event_data ev
;
102 * struct iio_event_interface - chrdev interface for an event line
103 * @dev: device assocated with event interface
104 * @wait: wait queue to allow blocking reads of events
105 * @event_list_lock: mutex to protect the list of detected events
106 * @det_events: list of detected events
107 * @max_events: maximum number of events before new ones are dropped
108 * @current_events: number of events in detected list
109 * @flags: file operations related flags including busy flag.
111 struct iio_event_interface
{
112 wait_queue_head_t wait
;
113 struct mutex event_list_lock
;
114 struct list_head det_events
;
117 struct list_head dev_attr_list
;
119 struct attribute_group group
;
122 int iio_push_event(struct iio_dev
*indio_dev
, u64 ev_code
, s64 timestamp
)
124 struct iio_event_interface
*ev_int
= indio_dev
->event_interface
;
125 struct iio_detected_event_list
*ev
;
128 /* Does anyone care? */
129 mutex_lock(&ev_int
->event_list_lock
);
130 if (test_bit(IIO_BUSY_BIT_POS
, &ev_int
->flags
)) {
131 if (ev_int
->current_events
== ev_int
->max_events
) {
132 mutex_unlock(&ev_int
->event_list_lock
);
135 ev
= kmalloc(sizeof(*ev
), GFP_KERNEL
);
138 mutex_unlock(&ev_int
->event_list_lock
);
142 ev
->ev
.timestamp
= timestamp
;
144 list_add_tail(&ev
->list
, &ev_int
->det_events
);
145 ev_int
->current_events
++;
146 mutex_unlock(&ev_int
->event_list_lock
);
147 wake_up_interruptible(&ev_int
->wait
);
149 mutex_unlock(&ev_int
->event_list_lock
);
154 EXPORT_SYMBOL(iio_push_event
);
156 /* This turns up an awful lot */
157 ssize_t
iio_read_const_attr(struct device
*dev
,
158 struct device_attribute
*attr
,
161 return sprintf(buf
, "%s\n", to_iio_const_attr(attr
)->string
);
163 EXPORT_SYMBOL(iio_read_const_attr
);
165 static ssize_t
iio_event_chrdev_read(struct file
*filep
,
170 struct iio_event_interface
*ev_int
= filep
->private_data
;
171 struct iio_detected_event_list
*el
;
175 mutex_lock(&ev_int
->event_list_lock
);
176 if (list_empty(&ev_int
->det_events
)) {
177 if (filep
->f_flags
& O_NONBLOCK
) {
179 goto error_mutex_unlock
;
181 mutex_unlock(&ev_int
->event_list_lock
);
182 /* Blocking on device; waiting for something to be there */
183 ret
= wait_event_interruptible(ev_int
->wait
,
188 /* Single access device so no one else can get the data */
189 mutex_lock(&ev_int
->event_list_lock
);
192 el
= list_first_entry(&ev_int
->det_events
,
193 struct iio_detected_event_list
,
196 if (copy_to_user(buf
, &(el
->ev
), len
)) {
198 goto error_mutex_unlock
;
201 ev_int
->current_events
--;
202 mutex_unlock(&ev_int
->event_list_lock
);
208 mutex_unlock(&ev_int
->event_list_lock
);
214 static int iio_event_chrdev_release(struct inode
*inode
, struct file
*filep
)
216 struct iio_event_interface
*ev_int
= filep
->private_data
;
217 struct iio_detected_event_list
*el
, *t
;
219 mutex_lock(&ev_int
->event_list_lock
);
220 clear_bit(IIO_BUSY_BIT_POS
, &ev_int
->flags
);
222 * In order to maintain a clean state for reopening,
223 * clear out any awaiting events. The mask will prevent
224 * any new __iio_push_event calls running.
226 list_for_each_entry_safe(el
, t
, &ev_int
->det_events
, list
) {
230 ev_int
->current_events
= 0;
231 mutex_unlock(&ev_int
->event_list_lock
);
236 static const struct file_operations iio_event_chrdev_fileops
= {
237 .read
= iio_event_chrdev_read
,
238 .release
= iio_event_chrdev_release
,
239 .owner
= THIS_MODULE
,
240 .llseek
= noop_llseek
,
243 static int iio_event_getfd(struct iio_dev
*indio_dev
)
245 if (indio_dev
->event_interface
== NULL
)
248 mutex_lock(&indio_dev
->event_interface
->event_list_lock
);
249 if (test_and_set_bit(IIO_BUSY_BIT_POS
,
250 &indio_dev
->event_interface
->flags
)) {
251 mutex_unlock(&indio_dev
->event_interface
->event_list_lock
);
254 mutex_unlock(&indio_dev
->event_interface
->event_list_lock
);
255 return anon_inode_getfd("iio:event",
256 &iio_event_chrdev_fileops
,
257 indio_dev
->event_interface
, O_RDONLY
);
260 static int __init
iio_init(void)
264 /* Register sysfs bus */
265 ret
= bus_register(&iio_bus_type
);
268 "%s could not register bus type\n",
273 ret
= alloc_chrdev_region(&iio_devt
, 0, IIO_DEV_MAX
, "iio");
275 printk(KERN_ERR
"%s: failed to allocate char dev region\n",
277 goto error_unregister_bus_type
;
282 error_unregister_bus_type
:
283 bus_unregister(&iio_bus_type
);
288 static void __exit
iio_exit(void)
291 unregister_chrdev_region(iio_devt
, IIO_DEV_MAX
);
292 bus_unregister(&iio_bus_type
);
295 static ssize_t
iio_read_channel_info(struct device
*dev
,
296 struct device_attribute
*attr
,
299 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
300 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
302 int ret
= indio_dev
->info
->read_raw(indio_dev
, this_attr
->c
,
303 &val
, &val2
, this_attr
->address
);
308 if (ret
== IIO_VAL_INT
)
309 return sprintf(buf
, "%d\n", val
);
310 else if (ret
== IIO_VAL_INT_PLUS_MICRO
) {
312 return sprintf(buf
, "-%d.%06u\n", val
, -val2
);
314 return sprintf(buf
, "%d.%06u\n", val
, val2
);
315 } else if (ret
== IIO_VAL_INT_PLUS_NANO
) {
317 return sprintf(buf
, "-%d.%09u\n", val
, -val2
);
319 return sprintf(buf
, "%d.%09u\n", val
, val2
);
324 static ssize_t
iio_write_channel_info(struct device
*dev
,
325 struct device_attribute
*attr
,
329 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
330 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
331 int ret
, integer
= 0, fract
= 0, fract_mult
= 100000;
332 bool integer_part
= true, negative
= false;
334 /* Assumes decimal - precision based on number of digits */
335 if (!indio_dev
->info
->write_raw
)
338 if (indio_dev
->info
->write_raw_get_fmt
)
339 switch (indio_dev
->info
->write_raw_get_fmt(indio_dev
,
340 this_attr
->c
, this_attr
->address
)) {
341 case IIO_VAL_INT_PLUS_MICRO
:
344 case IIO_VAL_INT_PLUS_NANO
:
345 fract_mult
= 100000000;
357 if ('0' <= *buf
&& *buf
<= '9') {
359 integer
= integer
*10 + *buf
- '0';
361 fract
+= fract_mult
*(*buf
- '0');
366 } else if (*buf
== '\n') {
367 if (*(buf
+ 1) == '\0')
371 } else if (*buf
== '.') {
372 integer_part
= false;
385 ret
= indio_dev
->info
->write_raw(indio_dev
, this_attr
->c
,
386 integer
, fract
, this_attr
->address
);
394 int __iio_device_attr_init(struct device_attribute
*dev_attr
,
396 struct iio_chan_spec
const *chan
,
397 ssize_t (*readfunc
)(struct device
*dev
,
398 struct device_attribute
*attr
,
400 ssize_t (*writefunc
)(struct device
*dev
,
401 struct device_attribute
*attr
,
407 char *name_format
, *full_postfix
;
408 sysfs_attr_init(&dev_attr
->attr
);
410 /* Build up postfix of <extend_name>_<modifier>_postfix */
411 if (chan
->modified
) {
412 if (chan
->extend_name
)
413 full_postfix
= kasprintf(GFP_KERNEL
, "%s_%s_%s",
414 iio_modifier_names
[chan
419 full_postfix
= kasprintf(GFP_KERNEL
, "%s_%s",
420 iio_modifier_names
[chan
424 if (chan
->extend_name
== NULL
)
425 full_postfix
= kstrdup(postfix
, GFP_KERNEL
);
427 full_postfix
= kasprintf(GFP_KERNEL
,
432 if (full_postfix
== NULL
) {
437 if (chan
->differential
) { /* Differential can not have modifier */
440 = kasprintf(GFP_KERNEL
, "%s_%s-%s_%s",
441 iio_direction
[chan
->output
],
442 iio_chan_type_name_spec
[chan
->type
],
443 iio_chan_type_name_spec
[chan
->type
],
445 else if (chan
->indexed
)
447 = kasprintf(GFP_KERNEL
, "%s_%s%d-%s%d_%s",
448 iio_direction
[chan
->output
],
449 iio_chan_type_name_spec
[chan
->type
],
451 iio_chan_type_name_spec
[chan
->type
],
455 WARN_ON("Differential channels must be indexed\n");
457 goto error_free_full_postfix
;
459 } else { /* Single ended */
462 = kasprintf(GFP_KERNEL
, "%s_%s_%s",
463 iio_direction
[chan
->output
],
464 iio_chan_type_name_spec
[chan
->type
],
466 else if (chan
->indexed
)
468 = kasprintf(GFP_KERNEL
, "%s_%s%d_%s",
469 iio_direction
[chan
->output
],
470 iio_chan_type_name_spec
[chan
->type
],
475 = kasprintf(GFP_KERNEL
, "%s_%s_%s",
476 iio_direction
[chan
->output
],
477 iio_chan_type_name_spec
[chan
->type
],
480 if (name_format
== NULL
) {
482 goto error_free_full_postfix
;
484 dev_attr
->attr
.name
= kasprintf(GFP_KERNEL
,
488 if (dev_attr
->attr
.name
== NULL
) {
490 goto error_free_name_format
;
494 dev_attr
->attr
.mode
|= S_IRUGO
;
495 dev_attr
->show
= readfunc
;
499 dev_attr
->attr
.mode
|= S_IWUSR
;
500 dev_attr
->store
= writefunc
;
507 error_free_name_format
:
509 error_free_full_postfix
:
515 static void __iio_device_attr_deinit(struct device_attribute
*dev_attr
)
517 kfree(dev_attr
->attr
.name
);
520 int __iio_add_chan_devattr(const char *postfix
,
521 struct iio_chan_spec
const *chan
,
522 ssize_t (*readfunc
)(struct device
*dev
,
523 struct device_attribute
*attr
,
525 ssize_t (*writefunc
)(struct device
*dev
,
526 struct device_attribute
*attr
,
532 struct list_head
*attr_list
)
535 struct iio_dev_attr
*iio_attr
, *t
;
537 iio_attr
= kzalloc(sizeof *iio_attr
, GFP_KERNEL
);
538 if (iio_attr
== NULL
) {
542 ret
= __iio_device_attr_init(&iio_attr
->dev_attr
,
544 readfunc
, writefunc
, generic
);
546 goto error_iio_dev_attr_free
;
548 iio_attr
->address
= mask
;
549 list_for_each_entry(t
, attr_list
, l
)
550 if (strcmp(t
->dev_attr
.attr
.name
,
551 iio_attr
->dev_attr
.attr
.name
) == 0) {
553 dev_err(dev
, "tried to double register : %s\n",
554 t
->dev_attr
.attr
.name
);
556 goto error_device_attr_deinit
;
558 list_add(&iio_attr
->l
, attr_list
);
562 error_device_attr_deinit
:
563 __iio_device_attr_deinit(&iio_attr
->dev_attr
);
564 error_iio_dev_attr_free
:
570 static int iio_device_add_channel_sysfs(struct iio_dev
*indio_dev
,
571 struct iio_chan_spec
const *chan
)
573 int ret
, i
, attrcount
= 0;
575 if (chan
->channel
< 0)
578 ret
= __iio_add_chan_devattr(iio_data_type_name
[chan
->processed_val
],
580 &iio_read_channel_info
,
582 &iio_write_channel_info
: NULL
),
586 &indio_dev
->channel_attr_list
);
591 for_each_set_bit(i
, &chan
->info_mask
, sizeof(long)*8) {
592 ret
= __iio_add_chan_devattr(iio_chan_info_postfix
[i
/2],
594 &iio_read_channel_info
,
595 &iio_write_channel_info
,
599 &indio_dev
->channel_attr_list
);
600 if (ret
== -EBUSY
&& (i
%2 == 0)) {
613 static void iio_device_remove_and_free_read_attr(struct iio_dev
*indio_dev
,
614 struct iio_dev_attr
*p
)
616 kfree(p
->dev_attr
.attr
.name
);
620 static ssize_t
iio_show_dev_name(struct device
*dev
,
621 struct device_attribute
*attr
,
624 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
625 return sprintf(buf
, "%s\n", indio_dev
->name
);
628 static DEVICE_ATTR(name
, S_IRUGO
, iio_show_dev_name
, NULL
);
630 static int iio_device_register_sysfs(struct iio_dev
*indio_dev
)
632 int i
, ret
= 0, attrcount
, attrn
, attrcount_orig
= 0;
633 struct iio_dev_attr
*p
, *n
;
634 struct attribute
**attr
;
636 /* First count elements in any existing group */
637 if (indio_dev
->info
->attrs
) {
638 attr
= indio_dev
->info
->attrs
->attrs
;
639 while (*attr
++ != NULL
)
642 attrcount
= attrcount_orig
;
644 * New channel registration method - relies on the fact a group does
645 * not need to be initialized if it is name is NULL.
647 INIT_LIST_HEAD(&indio_dev
->channel_attr_list
);
648 if (indio_dev
->channels
)
649 for (i
= 0; i
< indio_dev
->num_channels
; i
++) {
650 ret
= iio_device_add_channel_sysfs(indio_dev
,
654 goto error_clear_attrs
;
661 indio_dev
->chan_attr_group
.attrs
662 = kzalloc(sizeof(indio_dev
->chan_attr_group
.attrs
[0])*
665 if (indio_dev
->chan_attr_group
.attrs
== NULL
) {
667 goto error_clear_attrs
;
669 /* Copy across original attributes */
670 if (indio_dev
->info
->attrs
)
671 memcpy(indio_dev
->chan_attr_group
.attrs
,
672 indio_dev
->info
->attrs
->attrs
,
673 sizeof(indio_dev
->chan_attr_group
.attrs
[0])
675 attrn
= attrcount_orig
;
676 /* Add all elements from the list. */
677 list_for_each_entry(p
, &indio_dev
->channel_attr_list
, l
)
678 indio_dev
->chan_attr_group
.attrs
[attrn
++] = &p
->dev_attr
.attr
;
680 indio_dev
->chan_attr_group
.attrs
[attrn
++] = &dev_attr_name
.attr
;
682 indio_dev
->groups
[indio_dev
->groupcounter
++] =
683 &indio_dev
->chan_attr_group
;
688 list_for_each_entry_safe(p
, n
,
689 &indio_dev
->channel_attr_list
, l
) {
691 iio_device_remove_and_free_read_attr(indio_dev
, p
);
697 static void iio_device_unregister_sysfs(struct iio_dev
*indio_dev
)
700 struct iio_dev_attr
*p
, *n
;
702 list_for_each_entry_safe(p
, n
, &indio_dev
->channel_attr_list
, l
) {
704 iio_device_remove_and_free_read_attr(indio_dev
, p
);
706 kfree(indio_dev
->chan_attr_group
.attrs
);
709 static const char * const iio_ev_type_text
[] = {
710 [IIO_EV_TYPE_THRESH
] = "thresh",
711 [IIO_EV_TYPE_MAG
] = "mag",
712 [IIO_EV_TYPE_ROC
] = "roc",
713 [IIO_EV_TYPE_THRESH_ADAPTIVE
] = "thresh_adaptive",
714 [IIO_EV_TYPE_MAG_ADAPTIVE
] = "mag_adaptive",
717 static const char * const iio_ev_dir_text
[] = {
718 [IIO_EV_DIR_EITHER
] = "either",
719 [IIO_EV_DIR_RISING
] = "rising",
720 [IIO_EV_DIR_FALLING
] = "falling"
723 static ssize_t
iio_ev_state_store(struct device
*dev
,
724 struct device_attribute
*attr
,
728 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
729 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
733 ret
= strtobool(buf
, &val
);
737 ret
= indio_dev
->info
->write_event_config(indio_dev
,
740 return (ret
< 0) ? ret
: len
;
743 static ssize_t
iio_ev_state_show(struct device
*dev
,
744 struct device_attribute
*attr
,
747 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
748 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
749 int val
= indio_dev
->info
->read_event_config(indio_dev
,
755 return sprintf(buf
, "%d\n", val
);
758 static ssize_t
iio_ev_value_show(struct device
*dev
,
759 struct device_attribute
*attr
,
762 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
763 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
766 ret
= indio_dev
->info
->read_event_value(indio_dev
,
767 this_attr
->address
, &val
);
771 return sprintf(buf
, "%d\n", val
);
774 static ssize_t
iio_ev_value_store(struct device
*dev
,
775 struct device_attribute
*attr
,
779 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
780 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
784 ret
= strict_strtoul(buf
, 10, &val
);
788 ret
= indio_dev
->info
->write_event_value(indio_dev
, this_attr
->address
,
796 static int iio_device_add_event_sysfs(struct iio_dev
*indio_dev
,
797 struct iio_chan_spec
const *chan
)
799 int ret
= 0, i
, attrcount
= 0;
802 if (!chan
->event_mask
)
805 for_each_set_bit(i
, &chan
->event_mask
, sizeof(chan
->event_mask
)*8) {
806 postfix
= kasprintf(GFP_KERNEL
, "%s_%s_en",
807 iio_ev_type_text
[i
/IIO_EV_DIR_MAX
],
808 iio_ev_dir_text
[i
%IIO_EV_DIR_MAX
]);
809 if (postfix
== NULL
) {
814 mask
= IIO_MOD_EVENT_CODE(chan
->type
, 0, chan
->channel
,
817 else if (chan
->differential
)
818 mask
= IIO_EVENT_CODE(chan
->type
,
826 mask
= IIO_UNMOD_EVENT_CODE(chan
->type
,
831 ret
= __iio_add_chan_devattr(postfix
,
838 &indio_dev
->event_interface
->
844 postfix
= kasprintf(GFP_KERNEL
, "%s_%s_value",
845 iio_ev_type_text
[i
/IIO_EV_DIR_MAX
],
846 iio_ev_dir_text
[i
%IIO_EV_DIR_MAX
]);
847 if (postfix
== NULL
) {
851 ret
= __iio_add_chan_devattr(postfix
, chan
,
857 &indio_dev
->event_interface
->
869 static inline void __iio_remove_event_config_attrs(struct iio_dev
*indio_dev
)
871 struct iio_dev_attr
*p
, *n
;
872 list_for_each_entry_safe(p
, n
,
873 &indio_dev
->event_interface
->
875 kfree(p
->dev_attr
.attr
.name
);
880 static inline int __iio_add_event_config_attrs(struct iio_dev
*indio_dev
)
882 int j
, ret
, attrcount
= 0;
884 INIT_LIST_HEAD(&indio_dev
->event_interface
->dev_attr_list
);
885 /* Dynically created from the channels array */
886 for (j
= 0; j
< indio_dev
->num_channels
; j
++) {
887 ret
= iio_device_add_event_sysfs(indio_dev
,
888 &indio_dev
->channels
[j
]);
890 goto error_clear_attrs
;
896 __iio_remove_event_config_attrs(indio_dev
);
901 static bool iio_check_for_dynamic_events(struct iio_dev
*indio_dev
)
905 for (j
= 0; j
< indio_dev
->num_channels
; j
++)
906 if (indio_dev
->channels
[j
].event_mask
!= 0)
911 static void iio_setup_ev_int(struct iio_event_interface
*ev_int
)
913 mutex_init(&ev_int
->event_list_lock
);
914 /* discussion point - make this variable? */
915 ev_int
->max_events
= 10;
916 ev_int
->current_events
= 0;
917 INIT_LIST_HEAD(&ev_int
->det_events
);
918 init_waitqueue_head(&ev_int
->wait
);
921 static const char *iio_event_group_name
= "events";
922 static int iio_device_register_eventset(struct iio_dev
*indio_dev
)
924 struct iio_dev_attr
*p
;
925 int ret
= 0, attrcount_orig
= 0, attrcount
, attrn
;
926 struct attribute
**attr
;
928 if (!(indio_dev
->info
->event_attrs
||
929 iio_check_for_dynamic_events(indio_dev
)))
932 indio_dev
->event_interface
=
933 kzalloc(sizeof(struct iio_event_interface
), GFP_KERNEL
);
934 if (indio_dev
->event_interface
== NULL
) {
939 iio_setup_ev_int(indio_dev
->event_interface
);
940 if (indio_dev
->info
->event_attrs
!= NULL
) {
941 attr
= indio_dev
->info
->event_attrs
->attrs
;
942 while (*attr
++ != NULL
)
945 attrcount
= attrcount_orig
;
946 if (indio_dev
->channels
) {
947 ret
= __iio_add_event_config_attrs(indio_dev
);
949 goto error_free_setup_event_lines
;
953 indio_dev
->event_interface
->group
.name
= iio_event_group_name
;
954 indio_dev
->event_interface
->group
.attrs
=
955 kzalloc(sizeof(indio_dev
->event_interface
->group
.attrs
[0])
958 if (indio_dev
->event_interface
->group
.attrs
== NULL
) {
960 goto error_free_setup_event_lines
;
962 if (indio_dev
->info
->event_attrs
)
963 memcpy(indio_dev
->event_interface
->group
.attrs
,
964 indio_dev
->info
->event_attrs
->attrs
,
965 sizeof(indio_dev
->event_interface
->group
.attrs
[0])
967 attrn
= attrcount_orig
;
968 /* Add all elements from the list. */
969 list_for_each_entry(p
,
970 &indio_dev
->event_interface
->dev_attr_list
,
972 indio_dev
->event_interface
->group
.attrs
[attrn
++] =
974 indio_dev
->groups
[indio_dev
->groupcounter
++] =
975 &indio_dev
->event_interface
->group
;
979 error_free_setup_event_lines
:
980 __iio_remove_event_config_attrs(indio_dev
);
981 kfree(indio_dev
->event_interface
);
987 static void iio_device_unregister_eventset(struct iio_dev
*indio_dev
)
989 if (indio_dev
->event_interface
== NULL
)
991 __iio_remove_event_config_attrs(indio_dev
);
992 kfree(indio_dev
->event_interface
->group
.attrs
);
993 kfree(indio_dev
->event_interface
);
996 static void iio_dev_release(struct device
*device
)
998 struct iio_dev
*indio_dev
= container_of(device
, struct iio_dev
, dev
);
999 cdev_del(&indio_dev
->chrdev
);
1000 if (indio_dev
->modes
& INDIO_BUFFER_TRIGGERED
)
1001 iio_device_unregister_trigger_consumer(indio_dev
);
1002 iio_device_unregister_eventset(indio_dev
);
1003 iio_device_unregister_sysfs(indio_dev
);
1006 static struct device_type iio_dev_type
= {
1007 .name
= "iio_device",
1008 .release
= iio_dev_release
,
1011 struct iio_dev
*iio_allocate_device(int sizeof_priv
)
1013 struct iio_dev
*dev
;
1016 alloc_size
= sizeof(struct iio_dev
);
1018 alloc_size
= ALIGN(alloc_size
, IIO_ALIGN
);
1019 alloc_size
+= sizeof_priv
;
1021 /* ensure 32-byte alignment of whole construct ? */
1022 alloc_size
+= IIO_ALIGN
- 1;
1024 dev
= kzalloc(alloc_size
, GFP_KERNEL
);
1027 dev
->dev
.groups
= dev
->groups
;
1028 dev
->dev
.type
= &iio_dev_type
;
1029 dev
->dev
.bus
= &iio_bus_type
;
1030 device_initialize(&dev
->dev
);
1031 dev_set_drvdata(&dev
->dev
, (void *)dev
);
1032 mutex_init(&dev
->mlock
);
1034 dev
->id
= ida_simple_get(&iio_ida
, 0, 0, GFP_KERNEL
);
1036 /* cannot use a dev_err as the name isn't available */
1037 printk(KERN_ERR
"Failed to get id\n");
1041 dev_set_name(&dev
->dev
, "iio:device%d", dev
->id
);
1046 EXPORT_SYMBOL(iio_allocate_device
);
1048 void iio_free_device(struct iio_dev
*dev
)
1051 ida_simple_remove(&iio_ida
, dev
->id
);
1055 EXPORT_SYMBOL(iio_free_device
);
1058 * iio_chrdev_open() - chrdev file open for buffer access and ioctls
1060 static int iio_chrdev_open(struct inode
*inode
, struct file
*filp
)
1062 struct iio_dev
*indio_dev
= container_of(inode
->i_cdev
,
1063 struct iio_dev
, chrdev
);
1064 filp
->private_data
= indio_dev
;
1066 return iio_chrdev_buffer_open(indio_dev
);
1070 * iio_chrdev_release() - chrdev file close buffer access and ioctls
1072 static int iio_chrdev_release(struct inode
*inode
, struct file
*filp
)
1074 iio_chrdev_buffer_release(container_of(inode
->i_cdev
,
1075 struct iio_dev
, chrdev
));
1079 /* Somewhat of a cross file organization violation - ioctls here are actually
1081 static long iio_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
1083 struct iio_dev
*indio_dev
= filp
->private_data
;
1084 int __user
*ip
= (int __user
*)arg
;
1087 if (cmd
== IIO_GET_EVENT_FD_IOCTL
) {
1088 fd
= iio_event_getfd(indio_dev
);
1089 if (copy_to_user(ip
, &fd
, sizeof(fd
)))
1096 static const struct file_operations iio_buffer_fileops
= {
1097 .read
= iio_buffer_read_first_n_outer_addr
,
1098 .release
= iio_chrdev_release
,
1099 .open
= iio_chrdev_open
,
1100 .poll
= iio_buffer_poll_addr
,
1101 .owner
= THIS_MODULE
,
1102 .llseek
= noop_llseek
,
1103 .unlocked_ioctl
= iio_ioctl
,
1104 .compat_ioctl
= iio_ioctl
,
1107 int iio_device_register(struct iio_dev
*indio_dev
)
1111 /* configure elements for the chrdev */
1112 indio_dev
->dev
.devt
= MKDEV(MAJOR(iio_devt
), indio_dev
->id
);
1114 ret
= iio_device_register_sysfs(indio_dev
);
1116 dev_err(indio_dev
->dev
.parent
,
1117 "Failed to register sysfs interfaces\n");
1120 ret
= iio_device_register_eventset(indio_dev
);
1122 dev_err(indio_dev
->dev
.parent
,
1123 "Failed to register event set\n");
1124 goto error_free_sysfs
;
1126 if (indio_dev
->modes
& INDIO_BUFFER_TRIGGERED
)
1127 iio_device_register_trigger_consumer(indio_dev
);
1129 ret
= device_add(&indio_dev
->dev
);
1131 goto error_unreg_eventset
;
1132 cdev_init(&indio_dev
->chrdev
, &iio_buffer_fileops
);
1133 indio_dev
->chrdev
.owner
= indio_dev
->info
->driver_module
;
1134 ret
= cdev_add(&indio_dev
->chrdev
, indio_dev
->dev
.devt
, 1);
1136 goto error_del_device
;
1140 device_del(&indio_dev
->dev
);
1141 error_unreg_eventset
:
1142 iio_device_unregister_eventset(indio_dev
);
1144 iio_device_unregister_sysfs(indio_dev
);
1148 EXPORT_SYMBOL(iio_device_register
);
1150 void iio_device_unregister(struct iio_dev
*indio_dev
)
1152 device_unregister(&indio_dev
->dev
);
1154 EXPORT_SYMBOL(iio_device_unregister
);
1155 subsys_initcall(iio_init
);
1156 module_exit(iio_exit
);
1158 MODULE_AUTHOR("Jonathan Cameron <jic23@cam.ac.uk>");
1159 MODULE_DESCRIPTION("Industrial I/O core");
1160 MODULE_LICENSE("GPL");