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"
29 /* IDA to assign each registered device a unique id*/
30 static DEFINE_IDA(iio_ida
);
32 static dev_t iio_devt
;
34 #define IIO_DEV_MAX 256
35 struct bus_type iio_bus_type
= {
38 EXPORT_SYMBOL(iio_bus_type
);
40 static const char * const iio_chan_type_name_spec_shared
[] = {
43 [IIO_CURRENT
] = "current",
44 [IIO_POWER
] = "power",
45 [IIO_ACCEL
] = "accel",
46 [IIO_IN_DIFF
] = "in-in",
49 [IIO_LIGHT
] = "illuminance",
50 [IIO_INTENSITY
] = "intensity",
51 [IIO_PROXIMITY
] = "proximity",
53 [IIO_INCLI
] = "incli",
56 [IIO_TIMESTAMP
] = "timestamp",
59 static const char * const iio_chan_type_name_spec_complex
[] = {
60 [IIO_IN_DIFF
] = "in%d-in%d",
63 static const char * const iio_modifier_names_light
[] = {
64 [IIO_MOD_LIGHT_BOTH
] = "both",
65 [IIO_MOD_LIGHT_IR
] = "ir",
68 static const char * const iio_modifier_names_axial
[] = {
74 /* relies on pairs of these shared then separate */
75 static const char * const iio_chan_info_postfix
[] = {
76 [IIO_CHAN_INFO_SCALE_SHARED
/2] = "scale",
77 [IIO_CHAN_INFO_OFFSET_SHARED
/2] = "offset",
78 [IIO_CHAN_INFO_CALIBSCALE_SHARED
/2] = "calibscale",
79 [IIO_CHAN_INFO_CALIBBIAS_SHARED
/2] = "calibbias",
80 [IIO_CHAN_INFO_PEAK_SHARED
/2] = "peak_raw",
81 [IIO_CHAN_INFO_PEAK_SCALE_SHARED
/2] = "peak_scale",
82 [IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW_SHARED
/2]
83 = "quadrature_correction_raw",
87 * struct iio_detected_event_list - list element for events that have occurred
88 * @list: linked list header
89 * @ev: the event itself
91 struct iio_detected_event_list
{
92 struct list_head list
;
93 struct iio_event_data ev
;
97 * struct iio_event_interface - chrdev interface for an event line
98 * @dev: device assocated with event interface
99 * @wait: wait queue to allow blocking reads of events
100 * @event_list_lock: mutex to protect the list of detected events
101 * @det_events: list of detected events
102 * @max_events: maximum number of events before new ones are dropped
103 * @current_events: number of events in detected list
104 * @flags: file operations related flags including busy flag.
106 struct iio_event_interface
{
107 wait_queue_head_t wait
;
108 struct mutex event_list_lock
;
109 struct list_head det_events
;
112 struct list_head dev_attr_list
;
116 int iio_push_event(struct iio_dev
*dev_info
, int ev_code
, s64 timestamp
)
118 struct iio_event_interface
*ev_int
= dev_info
->event_interface
;
119 struct iio_detected_event_list
*ev
;
122 /* Does anyone care? */
123 mutex_lock(&ev_int
->event_list_lock
);
124 if (test_bit(IIO_BUSY_BIT_POS
, &ev_int
->flags
)) {
125 if (ev_int
->current_events
== ev_int
->max_events
) {
126 mutex_unlock(&ev_int
->event_list_lock
);
129 ev
= kmalloc(sizeof(*ev
), GFP_KERNEL
);
132 mutex_unlock(&ev_int
->event_list_lock
);
136 ev
->ev
.timestamp
= timestamp
;
138 list_add_tail(&ev
->list
, &ev_int
->det_events
);
139 ev_int
->current_events
++;
140 mutex_unlock(&ev_int
->event_list_lock
);
141 wake_up_interruptible(&ev_int
->wait
);
143 mutex_unlock(&ev_int
->event_list_lock
);
148 EXPORT_SYMBOL(iio_push_event
);
151 /* This turns up an awful lot */
152 ssize_t
iio_read_const_attr(struct device
*dev
,
153 struct device_attribute
*attr
,
156 return sprintf(buf
, "%s\n", to_iio_const_attr(attr
)->string
);
158 EXPORT_SYMBOL(iio_read_const_attr
);
161 static ssize_t
iio_event_chrdev_read(struct file
*filep
,
166 struct iio_event_interface
*ev_int
= filep
->private_data
;
167 struct iio_detected_event_list
*el
;
171 mutex_lock(&ev_int
->event_list_lock
);
172 if (list_empty(&ev_int
->det_events
)) {
173 if (filep
->f_flags
& O_NONBLOCK
) {
175 goto error_mutex_unlock
;
177 mutex_unlock(&ev_int
->event_list_lock
);
178 /* Blocking on device; waiting for something to be there */
179 ret
= wait_event_interruptible(ev_int
->wait
,
184 /* Single access device so no one else can get the data */
185 mutex_lock(&ev_int
->event_list_lock
);
188 el
= list_first_entry(&ev_int
->det_events
,
189 struct iio_detected_event_list
,
192 if (copy_to_user(buf
, &(el
->ev
), len
)) {
194 goto error_mutex_unlock
;
197 ev_int
->current_events
--;
198 mutex_unlock(&ev_int
->event_list_lock
);
204 mutex_unlock(&ev_int
->event_list_lock
);
210 static int iio_event_chrdev_release(struct inode
*inode
, struct file
*filep
)
212 struct iio_event_interface
*ev_int
= filep
->private_data
;
213 struct iio_detected_event_list
*el
, *t
;
215 mutex_lock(&ev_int
->event_list_lock
);
216 clear_bit(IIO_BUSY_BIT_POS
, &ev_int
->flags
);
218 * In order to maintain a clean state for reopening,
219 * clear out any awaiting events. The mask will prevent
220 * any new __iio_push_event calls running.
222 list_for_each_entry_safe(el
, t
, &ev_int
->det_events
, list
) {
226 ev_int
->current_events
= 0;
227 mutex_unlock(&ev_int
->event_list_lock
);
232 static const struct file_operations iio_event_chrdev_fileops
= {
233 .read
= iio_event_chrdev_read
,
234 .release
= iio_event_chrdev_release
,
235 .owner
= THIS_MODULE
,
236 .llseek
= noop_llseek
,
239 static int iio_event_getfd(struct iio_dev
*indio_dev
)
241 if (indio_dev
->event_interface
== NULL
)
244 mutex_lock(&indio_dev
->event_interface
->event_list_lock
);
245 if (test_and_set_bit(IIO_BUSY_BIT_POS
,
246 &indio_dev
->event_interface
->flags
)) {
247 mutex_unlock(&indio_dev
->event_interface
->event_list_lock
);
250 mutex_unlock(&indio_dev
->event_interface
->event_list_lock
);
251 return anon_inode_getfd("iio:event",
252 &iio_event_chrdev_fileops
,
253 indio_dev
->event_interface
, O_RDONLY
);
256 static void iio_setup_ev_int(struct iio_event_interface
*ev_int
)
258 mutex_init(&ev_int
->event_list_lock
);
259 /* discussion point - make this variable? */
260 ev_int
->max_events
= 10;
261 ev_int
->current_events
= 0;
262 INIT_LIST_HEAD(&ev_int
->det_events
);
263 init_waitqueue_head(&ev_int
->wait
);
266 static int __init
iio_init(void)
270 /* Register sysfs bus */
271 ret
= bus_register(&iio_bus_type
);
274 "%s could not register bus type\n",
279 ret
= alloc_chrdev_region(&iio_devt
, 0, IIO_DEV_MAX
, "iio");
281 printk(KERN_ERR
"%s: failed to allocate char dev region\n",
283 goto error_unregister_bus_type
;
288 error_unregister_bus_type
:
289 bus_unregister(&iio_bus_type
);
294 static void __exit
iio_exit(void)
297 unregister_chrdev_region(iio_devt
, IIO_DEV_MAX
);
298 bus_unregister(&iio_bus_type
);
301 static ssize_t
iio_read_channel_info(struct device
*dev
,
302 struct device_attribute
*attr
,
305 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
306 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
308 int ret
= indio_dev
->info
->read_raw(indio_dev
, this_attr
->c
,
309 &val
, &val2
, this_attr
->address
);
314 if (ret
== IIO_VAL_INT
)
315 return sprintf(buf
, "%d\n", val
);
316 else if (ret
== IIO_VAL_INT_PLUS_MICRO
) {
318 return sprintf(buf
, "-%d.%06u\n", val
, -val2
);
320 return sprintf(buf
, "%d.%06u\n", val
, val2
);
321 } else if (ret
== IIO_VAL_INT_PLUS_NANO
) {
323 return sprintf(buf
, "-%d.%09u\n", val
, -val2
);
325 return sprintf(buf
, "%d.%09u\n", val
, val2
);
330 static ssize_t
iio_write_channel_info(struct device
*dev
,
331 struct device_attribute
*attr
,
335 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
336 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
337 int ret
, integer
= 0, fract
= 0, fract_mult
= 100000;
338 bool integer_part
= true, negative
= false;
340 /* Assumes decimal - precision based on number of digits */
341 if (!indio_dev
->info
->write_raw
)
344 if (indio_dev
->info
->write_raw_get_fmt
)
345 switch (indio_dev
->info
->write_raw_get_fmt(indio_dev
,
346 this_attr
->c
, this_attr
->address
)) {
347 case IIO_VAL_INT_PLUS_MICRO
:
350 case IIO_VAL_INT_PLUS_NANO
:
351 fract_mult
= 100000000;
363 if ('0' <= *buf
&& *buf
<= '9') {
365 integer
= integer
*10 + *buf
- '0';
367 fract
+= fract_mult
*(*buf
- '0');
372 } else if (*buf
== '\n') {
373 if (*(buf
+ 1) == '\0')
377 } else if (*buf
== '.') {
378 integer_part
= false;
391 ret
= indio_dev
->info
->write_raw(indio_dev
, this_attr
->c
,
392 integer
, fract
, this_attr
->address
);
399 static int __iio_build_postfix(struct iio_chan_spec
const *chan
,
405 /* 3 options - generic, extend_name, modified - if generic, extend_name
406 * and modified cannot apply.*/
408 if (generic
|| (!chan
->modified
&& !chan
->extend_name
)) {
409 all_post
= kasprintf(GFP_KERNEL
, "%s", postfix
);
410 } else if (chan
->modified
) {
411 const char *intermediate
;
412 switch (chan
->type
) {
415 = iio_modifier_names_light
[chan
->channel2
];
424 = iio_modifier_names_axial
[chan
->channel2
];
429 if (chan
->extend_name
)
430 all_post
= kasprintf(GFP_KERNEL
, "%s_%s_%s",
435 all_post
= kasprintf(GFP_KERNEL
, "%s_%s",
439 all_post
= kasprintf(GFP_KERNEL
, "%s_%s", chan
->extend_name
,
441 if (all_post
== NULL
)
448 int __iio_device_attr_init(struct device_attribute
*dev_attr
,
450 struct iio_chan_spec
const *chan
,
451 ssize_t (*readfunc
)(struct device
*dev
,
452 struct device_attribute
*attr
,
454 ssize_t (*writefunc
)(struct device
*dev
,
455 struct device_attribute
*attr
,
461 char *name_format
, *full_postfix
;
462 sysfs_attr_init(&dev_attr
->attr
);
463 ret
= __iio_build_postfix(chan
, generic
, postfix
, &full_postfix
);
467 /* Special case for types that uses both channel numbers in naming */
468 if (chan
->type
== IIO_IN_DIFF
&& !generic
)
470 = kasprintf(GFP_KERNEL
, "%s_%s",
471 iio_chan_type_name_spec_complex
[chan
->type
],
473 else if (generic
|| !chan
->indexed
)
475 = kasprintf(GFP_KERNEL
, "%s_%s",
476 iio_chan_type_name_spec_shared
[chan
->type
],
480 = kasprintf(GFP_KERNEL
, "%s%d_%s",
481 iio_chan_type_name_spec_shared
[chan
->type
],
485 if (name_format
== NULL
) {
487 goto error_free_full_postfix
;
489 dev_attr
->attr
.name
= kasprintf(GFP_KERNEL
,
493 if (dev_attr
->attr
.name
== NULL
) {
495 goto error_free_name_format
;
499 dev_attr
->attr
.mode
|= S_IRUGO
;
500 dev_attr
->show
= readfunc
;
504 dev_attr
->attr
.mode
|= S_IWUSR
;
505 dev_attr
->store
= writefunc
;
512 error_free_name_format
:
514 error_free_full_postfix
:
520 static void __iio_device_attr_deinit(struct device_attribute
*dev_attr
)
522 kfree(dev_attr
->attr
.name
);
525 int __iio_add_chan_devattr(const char *postfix
,
527 struct iio_chan_spec
const *chan
,
528 ssize_t (*readfunc
)(struct device
*dev
,
529 struct device_attribute
*attr
,
531 ssize_t (*writefunc
)(struct device
*dev
,
532 struct device_attribute
*attr
,
538 struct list_head
*attr_list
)
541 struct iio_dev_attr
*iio_attr
, *t
;
543 iio_attr
= kzalloc(sizeof *iio_attr
, GFP_KERNEL
);
544 if (iio_attr
== NULL
) {
548 ret
= __iio_device_attr_init(&iio_attr
->dev_attr
,
550 readfunc
, writefunc
, generic
);
552 goto error_iio_dev_attr_free
;
554 iio_attr
->address
= mask
;
555 list_for_each_entry(t
, attr_list
, l
)
556 if (strcmp(t
->dev_attr
.attr
.name
,
557 iio_attr
->dev_attr
.attr
.name
) == 0) {
559 dev_err(dev
, "tried to double register : %s\n",
560 t
->dev_attr
.attr
.name
);
562 goto error_device_attr_deinit
;
565 ret
= sysfs_add_file_to_group(&dev
->kobj
,
566 &iio_attr
->dev_attr
.attr
, group
);
568 goto error_device_attr_deinit
;
570 list_add(&iio_attr
->l
, attr_list
);
574 error_device_attr_deinit
:
575 __iio_device_attr_deinit(&iio_attr
->dev_attr
);
576 error_iio_dev_attr_free
:
582 static int iio_device_add_channel_sysfs(struct iio_dev
*dev_info
,
583 struct iio_chan_spec
const *chan
)
588 if (chan
->channel
< 0)
590 if (chan
->processed_val
)
591 ret
= __iio_add_chan_devattr("input", NULL
, chan
,
592 &iio_read_channel_info
,
597 &dev_info
->channel_attr_list
);
599 ret
= __iio_add_chan_devattr("raw", NULL
, chan
,
600 &iio_read_channel_info
,
601 (chan
->type
== IIO_OUT
?
602 &iio_write_channel_info
: NULL
),
606 &dev_info
->channel_attr_list
);
610 for_each_set_bit(i
, &chan
->info_mask
, sizeof(long)*8) {
611 ret
= __iio_add_chan_devattr(iio_chan_info_postfix
[i
/2],
613 &iio_read_channel_info
,
614 &iio_write_channel_info
,
618 &dev_info
->channel_attr_list
);
619 if (ret
== -EBUSY
&& (i
%2 == 0)) {
630 static void iio_device_remove_and_free_read_attr(struct iio_dev
*dev_info
,
631 struct iio_dev_attr
*p
)
633 sysfs_remove_file_from_group(&dev_info
->dev
.kobj
,
634 &p
->dev_attr
.attr
, NULL
);
635 kfree(p
->dev_attr
.attr
.name
);
639 static ssize_t
iio_show_dev_name(struct device
*dev
,
640 struct device_attribute
*attr
,
643 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
644 return sprintf(buf
, "%s\n", indio_dev
->name
);
647 static DEVICE_ATTR(name
, S_IRUGO
, iio_show_dev_name
, NULL
);
649 static struct attribute
*iio_base_dummy_attrs
[] = {
652 static struct attribute_group iio_base_dummy_group
= {
653 .attrs
= iio_base_dummy_attrs
,
656 static int iio_device_register_sysfs(struct iio_dev
*dev_info
)
659 struct iio_dev_attr
*p
, *n
;
661 if (dev_info
->info
->attrs
)
662 ret
= sysfs_create_group(&dev_info
->dev
.kobj
,
663 dev_info
->info
->attrs
);
665 ret
= sysfs_create_group(&dev_info
->dev
.kobj
,
666 &iio_base_dummy_group
);
669 dev_err(dev_info
->dev
.parent
,
670 "Failed to register sysfs hooks\n");
675 * New channel registration method - relies on the fact a group does
676 * not need to be initialized if it is name is NULL.
678 INIT_LIST_HEAD(&dev_info
->channel_attr_list
);
679 if (dev_info
->channels
)
680 for (i
= 0; i
< dev_info
->num_channels
; i
++) {
681 ret
= iio_device_add_channel_sysfs(dev_info
,
685 goto error_clear_attrs
;
687 if (dev_info
->name
) {
688 ret
= sysfs_add_file_to_group(&dev_info
->dev
.kobj
,
692 goto error_clear_attrs
;
697 list_for_each_entry_safe(p
, n
,
698 &dev_info
->channel_attr_list
, l
) {
700 iio_device_remove_and_free_read_attr(dev_info
, p
);
702 if (dev_info
->info
->attrs
)
703 sysfs_remove_group(&dev_info
->dev
.kobj
, dev_info
->info
->attrs
);
705 sysfs_remove_group(&dev_info
->dev
.kobj
, &iio_base_dummy_group
);
711 static void iio_device_unregister_sysfs(struct iio_dev
*dev_info
)
714 struct iio_dev_attr
*p
, *n
;
716 sysfs_remove_file_from_group(&dev_info
->dev
.kobj
,
719 list_for_each_entry_safe(p
, n
, &dev_info
->channel_attr_list
, l
) {
721 iio_device_remove_and_free_read_attr(dev_info
, p
);
724 if (dev_info
->info
->attrs
)
725 sysfs_remove_group(&dev_info
->dev
.kobj
, dev_info
->info
->attrs
);
727 sysfs_remove_group(&dev_info
->dev
.kobj
, &iio_base_dummy_group
);
730 static const char * const iio_ev_type_text
[] = {
731 [IIO_EV_TYPE_THRESH
] = "thresh",
732 [IIO_EV_TYPE_MAG
] = "mag",
733 [IIO_EV_TYPE_ROC
] = "roc"
736 static const char * const iio_ev_dir_text
[] = {
737 [IIO_EV_DIR_EITHER
] = "either",
738 [IIO_EV_DIR_RISING
] = "rising",
739 [IIO_EV_DIR_FALLING
] = "falling"
742 static ssize_t
iio_ev_state_store(struct device
*dev
,
743 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
);
752 ret
= strtobool(buf
, &val
);
756 ret
= indio_dev
->info
->write_event_config(indio_dev
,
759 return (ret
< 0) ? ret
: len
;
762 static ssize_t
iio_ev_state_show(struct device
*dev
,
763 struct device_attribute
*attr
,
766 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
767 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
768 int val
= indio_dev
->info
->read_event_config(indio_dev
,
774 return sprintf(buf
, "%d\n", val
);
777 static ssize_t
iio_ev_value_show(struct device
*dev
,
778 struct device_attribute
*attr
,
781 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
782 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
785 ret
= indio_dev
->info
->read_event_value(indio_dev
,
786 this_attr
->address
, &val
);
790 return sprintf(buf
, "%d\n", val
);
793 static ssize_t
iio_ev_value_store(struct device
*dev
,
794 struct device_attribute
*attr
,
798 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
799 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
803 ret
= strict_strtoul(buf
, 10, &val
);
807 ret
= indio_dev
->info
->write_event_value(indio_dev
, this_attr
->address
,
815 static int iio_device_add_event_sysfs(struct iio_dev
*dev_info
,
816 struct iio_chan_spec
const *chan
)
819 int ret
= 0, i
, mask
= 0;
821 if (!chan
->event_mask
)
824 for_each_set_bit(i
, &chan
->event_mask
, sizeof(chan
->event_mask
)*8) {
825 postfix
= kasprintf(GFP_KERNEL
, "%s_%s_en",
826 iio_ev_type_text
[i
/IIO_EV_TYPE_MAX
],
827 iio_ev_dir_text
[i
%IIO_EV_TYPE_MAX
]);
828 if (postfix
== NULL
) {
832 switch (chan
->type
) {
833 /* Switch this to a table at some point */
835 mask
= IIO_UNMOD_EVENT_CODE(chan
->type
, chan
->channel
,
840 mask
= IIO_MOD_EVENT_CODE(chan
->type
, 0, chan
->channel
,
845 mask
= IIO_MOD_EVENT_CODE(chan
->type
, chan
->channel
,
851 printk(KERN_INFO
"currently unhandled type of event\n");
854 ret
= __iio_add_chan_devattr(postfix
,
862 &dev_info
->event_interface
->
868 postfix
= kasprintf(GFP_KERNEL
, "%s_%s_value",
869 iio_ev_type_text
[i
/IIO_EV_TYPE_MAX
],
870 iio_ev_dir_text
[i
%IIO_EV_TYPE_MAX
]);
871 if (postfix
== NULL
) {
875 ret
= __iio_add_chan_devattr(postfix
, "events", chan
,
881 &dev_info
->event_interface
->
893 static inline void __iio_remove_event_config_attrs(struct iio_dev
*dev_info
)
895 struct iio_dev_attr
*p
, *n
;
896 list_for_each_entry_safe(p
, n
,
897 &dev_info
->event_interface
->
899 sysfs_remove_file_from_group(&dev_info
->dev
.kobj
,
902 kfree(p
->dev_attr
.attr
.name
);
907 static inline int __iio_add_event_config_attrs(struct iio_dev
*dev_info
)
911 INIT_LIST_HEAD(&dev_info
->event_interface
->dev_attr_list
);
912 /* Dynically created from the channels array */
913 for (j
= 0; j
< dev_info
->num_channels
; j
++) {
915 ret
= iio_device_add_event_sysfs(dev_info
,
916 &dev_info
->channels
[j
]);
918 goto error_clear_attrs
;
923 __iio_remove_event_config_attrs(dev_info
);
928 static struct attribute
*iio_events_dummy_attrs
[] = {
932 static struct attribute_group iio_events_dummy_group
= {
934 .attrs
= iio_events_dummy_attrs
937 static bool iio_check_for_dynamic_events(struct iio_dev
*dev_info
)
940 for (j
= 0; j
< dev_info
->num_channels
; j
++)
941 if (dev_info
->channels
[j
].event_mask
!= 0)
946 static int iio_device_register_eventset(struct iio_dev
*dev_info
)
950 if (!(dev_info
->info
->event_attrs
||
951 iio_check_for_dynamic_events(dev_info
)))
954 dev_info
->event_interface
=
955 kzalloc(sizeof(struct iio_event_interface
), GFP_KERNEL
);
956 if (dev_info
->event_interface
== NULL
) {
961 iio_setup_ev_int(dev_info
->event_interface
);
962 if (dev_info
->info
->event_attrs
!= NULL
)
963 ret
= sysfs_create_group(&dev_info
->dev
.kobj
,
964 dev_info
->info
->event_attrs
);
966 ret
= sysfs_create_group(&dev_info
->dev
.kobj
,
967 &iio_events_dummy_group
);
969 dev_err(&dev_info
->dev
,
970 "Failed to register sysfs for event attrs");
971 goto error_free_setup_event_lines
;
973 if (dev_info
->channels
) {
974 ret
= __iio_add_event_config_attrs(dev_info
);
976 if (dev_info
->info
->event_attrs
!= NULL
)
977 sysfs_remove_group(&dev_info
->dev
.kobj
,
981 sysfs_remove_group(&dev_info
->dev
.kobj
,
982 &iio_events_dummy_group
);
983 goto error_free_setup_event_lines
;
989 error_free_setup_event_lines
:
990 __iio_remove_event_config_attrs(dev_info
);
991 if (dev_info
->info
->event_attrs
!= NULL
)
992 sysfs_remove_group(&dev_info
->dev
.kobj
,
993 dev_info
->info
->event_attrs
);
995 sysfs_remove_group(&dev_info
->dev
.kobj
,
996 &iio_events_dummy_group
);
997 kfree(dev_info
->event_interface
);
1003 static void iio_device_unregister_eventset(struct iio_dev
*dev_info
)
1005 if (dev_info
->event_interface
== NULL
)
1007 __iio_remove_event_config_attrs(dev_info
);
1008 if (dev_info
->info
->event_attrs
!= NULL
)
1009 sysfs_remove_group(&dev_info
->dev
.kobj
,
1010 dev_info
->info
->event_attrs
);
1012 sysfs_remove_group(&dev_info
->dev
.kobj
,
1013 &iio_events_dummy_group
);
1014 kfree(dev_info
->event_interface
);
1017 static void iio_dev_release(struct device
*device
)
1019 struct iio_dev
*dev_info
= container_of(device
, struct iio_dev
, dev
);
1020 cdev_del(&dev_info
->chrdev
);
1025 static struct device_type iio_dev_type
= {
1026 .name
= "iio_device",
1027 .release
= iio_dev_release
,
1030 struct iio_dev
*iio_allocate_device(int sizeof_priv
)
1032 struct iio_dev
*dev
;
1035 alloc_size
= sizeof(struct iio_dev
);
1037 alloc_size
= ALIGN(alloc_size
, IIO_ALIGN
);
1038 alloc_size
+= sizeof_priv
;
1040 /* ensure 32-byte alignment of whole construct ? */
1041 alloc_size
+= IIO_ALIGN
- 1;
1043 dev
= kzalloc(alloc_size
, GFP_KERNEL
);
1046 dev
->dev
.type
= &iio_dev_type
;
1047 dev
->dev
.bus
= &iio_bus_type
;
1048 device_initialize(&dev
->dev
);
1049 dev_set_drvdata(&dev
->dev
, (void *)dev
);
1050 mutex_init(&dev
->mlock
);
1056 EXPORT_SYMBOL(iio_allocate_device
);
1058 void iio_free_device(struct iio_dev
*dev
)
1065 EXPORT_SYMBOL(iio_free_device
);
1068 * iio_chrdev_open() - chrdev file open for ring buffer access and ioctls
1070 static int iio_chrdev_open(struct inode
*inode
, struct file
*filp
)
1072 struct iio_dev
*dev_info
= container_of(inode
->i_cdev
,
1073 struct iio_dev
, chrdev
);
1074 filp
->private_data
= dev_info
;
1075 iio_chrdev_ring_open(dev_info
);
1080 * iio_chrdev_release() - chrdev file close ring buffer access and ioctls
1082 static int iio_chrdev_release(struct inode
*inode
, struct file
*filp
)
1084 iio_chrdev_ring_release(container_of(inode
->i_cdev
,
1085 struct iio_dev
, chrdev
));
1089 /* Somewhat of a cross file organization violation - ioctls here are actually
1091 static long iio_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
1093 struct iio_dev
*indio_dev
= filp
->private_data
;
1094 int __user
*ip
= (int __user
*)arg
;
1097 if (cmd
== IIO_GET_EVENT_FD_IOCTL
) {
1098 fd
= iio_event_getfd(indio_dev
);
1099 if (copy_to_user(ip
, &fd
, sizeof(fd
)))
1106 static const struct file_operations iio_ring_fileops
= {
1107 .read
= iio_ring_read_first_n_outer_addr
,
1108 .release
= iio_chrdev_release
,
1109 .open
= iio_chrdev_open
,
1110 .poll
= iio_ring_poll_addr
,
1111 .owner
= THIS_MODULE
,
1112 .llseek
= noop_llseek
,
1113 .unlocked_ioctl
= iio_ioctl
,
1114 .compat_ioctl
= iio_ioctl
,
1117 int iio_device_register(struct iio_dev
*dev_info
)
1121 dev_info
->id
= ida_simple_get(&iio_ida
, 0, 0, GFP_KERNEL
);
1122 if (dev_info
->id
< 0) {
1124 dev_err(&dev_info
->dev
, "Failed to get id\n");
1127 dev_set_name(&dev_info
->dev
, "iio:device%d", dev_info
->id
);
1129 /* configure elements for the chrdev */
1130 dev_info
->dev
.devt
= MKDEV(MAJOR(iio_devt
), dev_info
->id
);
1132 ret
= device_add(&dev_info
->dev
);
1134 goto error_free_ida
;
1135 ret
= iio_device_register_sysfs(dev_info
);
1137 dev_err(dev_info
->dev
.parent
,
1138 "Failed to register sysfs interfaces\n");
1139 goto error_del_device
;
1141 ret
= iio_device_register_eventset(dev_info
);
1143 dev_err(dev_info
->dev
.parent
,
1144 "Failed to register event set\n");
1145 goto error_free_sysfs
;
1147 if (dev_info
->modes
& INDIO_RING_TRIGGERED
)
1148 iio_device_register_trigger_consumer(dev_info
);
1150 cdev_init(&dev_info
->chrdev
, &iio_ring_fileops
);
1151 dev_info
->chrdev
.owner
= dev_info
->info
->driver_module
;
1152 ret
= cdev_add(&dev_info
->chrdev
, dev_info
->dev
.devt
, 1);
1156 iio_device_unregister_sysfs(dev_info
);
1158 device_del(&dev_info
->dev
);
1160 ida_simple_remove(&iio_ida
, dev_info
->id
);
1164 EXPORT_SYMBOL(iio_device_register
);
1166 void iio_device_unregister(struct iio_dev
*dev_info
)
1168 if (dev_info
->modes
& INDIO_RING_TRIGGERED
)
1169 iio_device_unregister_trigger_consumer(dev_info
);
1170 iio_device_unregister_eventset(dev_info
);
1171 iio_device_unregister_sysfs(dev_info
);
1172 ida_simple_remove(&iio_ida
, dev_info
->id
);
1173 device_unregister(&dev_info
->dev
);
1175 EXPORT_SYMBOL(iio_device_unregister
);
1179 module_put(THIS_MODULE
);
1184 __module_get(THIS_MODULE
);
1187 subsys_initcall(iio_init
);
1188 module_exit(iio_exit
);
1190 MODULE_AUTHOR("Jonathan Cameron <jic23@cam.ac.uk>");
1191 MODULE_DESCRIPTION("Industrial I/O core");
1192 MODULE_LICENSE("GPL");