1 /* The industrial I/O core
3 * Copyright (c) 2008 Jonathan Cameron
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
9 * Based on elements of hwmon and input subsystems.
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/idr.h>
15 #include <linux/kdev_t.h>
16 #include <linux/err.h>
17 #include <linux/device.h>
19 #include <linux/poll.h>
20 #include <linux/sched.h>
21 #include <linux/wait.h>
22 #include <linux/cdev.h>
23 #include <linux/slab.h>
25 #include "trigger_consumer.h"
27 #define IIO_ID_PREFIX "device"
28 #define IIO_ID_FORMAT IIO_ID_PREFIX "%d"
30 /* IDR to assign each registered device a unique id*/
31 static DEFINE_IDA(iio_ida
);
32 /* IDR to allocate character device minor numbers */
33 static DEFINE_IDA(iio_chrdev_ida
);
34 /* Lock used to protect both of the above */
35 static DEFINE_SPINLOCK(iio_ida_lock
);
38 EXPORT_SYMBOL(iio_devt
);
40 #define IIO_DEV_MAX 256
41 struct bus_type iio_bus_type
= {
44 EXPORT_SYMBOL(iio_bus_type
);
46 static const char * const iio_chan_type_name_spec_shared
[] = {
47 [IIO_TIMESTAMP
] = "timestamp",
48 [IIO_ACCEL
] = "accel",
51 [IIO_CURRENT
] = "current",
52 [IIO_POWER
] = "power",
53 [IIO_IN_DIFF
] = "in-in",
57 [IIO_INCLI
] = "incli",
59 [IIO_INTENSITY
] = "intensity",
60 [IIO_LIGHT
] = "illuminance",
64 static const char * const iio_chan_type_name_spec_complex
[] = {
65 [IIO_IN_DIFF
] = "in%d-in%d",
68 static const char * const iio_modifier_names_light
[] = {
69 [IIO_MOD_LIGHT_BOTH
] = "both",
70 [IIO_MOD_LIGHT_IR
] = "ir",
73 static const char * const iio_modifier_names_axial
[] = {
79 /* relies on pairs of these shared then separate */
80 static const char * const iio_chan_info_postfix
[] = {
81 [IIO_CHAN_INFO_SCALE_SHARED
/2] = "scale",
82 [IIO_CHAN_INFO_OFFSET_SHARED
/2] = "offset",
83 [IIO_CHAN_INFO_CALIBSCALE_SHARED
/2] = "calibscale",
84 [IIO_CHAN_INFO_CALIBBIAS_SHARED
/2] = "calibbias",
85 [IIO_CHAN_INFO_PEAK_SHARED
/2] = "peak_raw",
86 [IIO_CHAN_INFO_PEAK_SCALE_SHARED
/2] = "peak_scale",
89 int iio_push_event(struct iio_dev
*dev_info
,
94 struct iio_event_interface
*ev_int
95 = &dev_info
->event_interfaces
[ev_line
];
96 struct iio_detected_event_list
*ev
;
99 /* Does anyone care? */
100 mutex_lock(&ev_int
->event_list_lock
);
101 if (test_bit(IIO_BUSY_BIT_POS
, &ev_int
->handler
.flags
)) {
102 if (ev_int
->current_events
== ev_int
->max_events
) {
103 mutex_unlock(&ev_int
->event_list_lock
);
106 ev
= kmalloc(sizeof(*ev
), GFP_KERNEL
);
109 mutex_unlock(&ev_int
->event_list_lock
);
113 ev
->ev
.timestamp
= timestamp
;
115 list_add_tail(&ev
->list
, &ev_int
->det_events
);
116 ev_int
->current_events
++;
117 mutex_unlock(&ev_int
->event_list_lock
);
118 wake_up_interruptible(&ev_int
->wait
);
120 mutex_unlock(&ev_int
->event_list_lock
);
125 EXPORT_SYMBOL(iio_push_event
);
128 /* This turns up an awful lot */
129 ssize_t
iio_read_const_attr(struct device
*dev
,
130 struct device_attribute
*attr
,
133 return sprintf(buf
, "%s\n", to_iio_const_attr(attr
)->string
);
135 EXPORT_SYMBOL(iio_read_const_attr
);
138 static ssize_t
iio_event_chrdev_read(struct file
*filep
,
143 struct iio_event_interface
*ev_int
= filep
->private_data
;
144 struct iio_detected_event_list
*el
;
148 mutex_lock(&ev_int
->event_list_lock
);
149 if (list_empty(&ev_int
->det_events
)) {
150 if (filep
->f_flags
& O_NONBLOCK
) {
152 goto error_mutex_unlock
;
154 mutex_unlock(&ev_int
->event_list_lock
);
155 /* Blocking on device; waiting for something to be there */
156 ret
= wait_event_interruptible(ev_int
->wait
,
161 /* Single access device so no one else can get the data */
162 mutex_lock(&ev_int
->event_list_lock
);
165 el
= list_first_entry(&ev_int
->det_events
,
166 struct iio_detected_event_list
,
169 if (copy_to_user(buf
, &(el
->ev
), len
)) {
171 goto error_mutex_unlock
;
174 ev_int
->current_events
--;
175 mutex_unlock(&ev_int
->event_list_lock
);
181 mutex_unlock(&ev_int
->event_list_lock
);
187 static int iio_event_chrdev_release(struct inode
*inode
, struct file
*filep
)
189 struct iio_handler
*hand
= iio_cdev_to_handler(inode
->i_cdev
);
190 struct iio_event_interface
*ev_int
= hand
->private;
191 struct iio_detected_event_list
*el
, *t
;
193 mutex_lock(&ev_int
->event_list_lock
);
194 clear_bit(IIO_BUSY_BIT_POS
, &ev_int
->handler
.flags
);
196 * In order to maintain a clean state for reopening,
197 * clear out any awaiting events. The mask will prevent
198 * any new __iio_push_event calls running.
200 list_for_each_entry_safe(el
, t
, &ev_int
->det_events
, list
) {
204 mutex_unlock(&ev_int
->event_list_lock
);
209 static int iio_event_chrdev_open(struct inode
*inode
, struct file
*filep
)
211 struct iio_handler
*hand
= iio_cdev_to_handler(inode
->i_cdev
);
212 struct iio_event_interface
*ev_int
= hand
->private;
214 mutex_lock(&ev_int
->event_list_lock
);
215 if (test_and_set_bit(IIO_BUSY_BIT_POS
, &hand
->flags
)) {
216 fops_put(filep
->f_op
);
217 mutex_unlock(&ev_int
->event_list_lock
);
220 filep
->private_data
= hand
->private;
221 mutex_unlock(&ev_int
->event_list_lock
);
226 static const struct file_operations iio_event_chrdev_fileops
= {
227 .read
= iio_event_chrdev_read
,
228 .release
= iio_event_chrdev_release
,
229 .open
= iio_event_chrdev_open
,
230 .owner
= THIS_MODULE
,
231 .llseek
= noop_llseek
,
234 static void iio_event_dev_release(struct device
*dev
)
236 struct iio_event_interface
*ev_int
237 = container_of(dev
, struct iio_event_interface
, dev
);
238 cdev_del(&ev_int
->handler
.chrdev
);
239 iio_device_free_chrdev_minor(MINOR(dev
->devt
));
242 static struct device_type iio_event_type
= {
243 .release
= iio_event_dev_release
,
246 int iio_device_get_chrdev_minor(void)
251 if (unlikely(ida_pre_get(&iio_chrdev_ida
, GFP_KERNEL
) == 0))
253 spin_lock(&iio_ida_lock
);
254 ret
= ida_get_new(&iio_chrdev_ida
, &val
);
255 spin_unlock(&iio_ida_lock
);
256 if (unlikely(ret
== -EAGAIN
))
258 else if (unlikely(ret
))
260 if (val
> IIO_DEV_MAX
)
265 void iio_device_free_chrdev_minor(int val
)
267 spin_lock(&iio_ida_lock
);
268 ida_remove(&iio_chrdev_ida
, val
);
269 spin_unlock(&iio_ida_lock
);
272 static int iio_setup_ev_int(struct iio_event_interface
*ev_int
,
273 const char *dev_name
,
275 struct module
*owner
,
280 ev_int
->dev
.bus
= &iio_bus_type
;
281 ev_int
->dev
.parent
= dev
;
282 ev_int
->dev
.type
= &iio_event_type
;
283 device_initialize(&ev_int
->dev
);
285 minor
= iio_device_get_chrdev_minor();
288 goto error_device_put
;
290 ev_int
->dev
.devt
= MKDEV(MAJOR(iio_devt
), minor
);
291 dev_set_name(&ev_int
->dev
, "%s:event%d", dev_name
, index
);
293 ret
= device_add(&ev_int
->dev
);
295 goto error_free_minor
;
297 cdev_init(&ev_int
->handler
.chrdev
, &iio_event_chrdev_fileops
);
298 ev_int
->handler
.chrdev
.owner
= owner
;
300 mutex_init(&ev_int
->event_list_lock
);
301 /* discussion point - make this variable? */
302 ev_int
->max_events
= 10;
303 ev_int
->current_events
= 0;
304 INIT_LIST_HEAD(&ev_int
->det_events
);
305 init_waitqueue_head(&ev_int
->wait
);
306 ev_int
->handler
.private = ev_int
;
307 ev_int
->handler
.flags
= 0;
309 ret
= cdev_add(&ev_int
->handler
.chrdev
, ev_int
->dev
.devt
, 1);
311 goto error_unreg_device
;
316 device_unregister(&ev_int
->dev
);
318 iio_device_free_chrdev_minor(minor
);
320 put_device(&ev_int
->dev
);
325 static void iio_free_ev_int(struct iio_event_interface
*ev_int
)
327 device_unregister(&ev_int
->dev
);
328 put_device(&ev_int
->dev
);
331 static int __init
iio_dev_init(void)
335 err
= alloc_chrdev_region(&iio_devt
, 0, IIO_DEV_MAX
, "iio");
337 printk(KERN_ERR
"%s: failed to allocate char dev region\n",
343 static void __exit
iio_dev_exit(void)
346 unregister_chrdev_region(iio_devt
, IIO_DEV_MAX
);
349 static int __init
iio_init(void)
353 /* Register sysfs bus */
354 ret
= bus_register(&iio_bus_type
);
357 "%s could not register bus type\n",
362 ret
= iio_dev_init();
364 goto error_unregister_bus_type
;
368 error_unregister_bus_type
:
369 bus_unregister(&iio_bus_type
);
374 static void __exit
iio_exit(void)
377 bus_unregister(&iio_bus_type
);
380 static ssize_t
iio_read_channel_info(struct device
*dev
,
381 struct device_attribute
*attr
,
384 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
385 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
387 int ret
= indio_dev
->info
->read_raw(indio_dev
, this_attr
->c
,
388 &val
, &val2
, this_attr
->address
);
393 if (ret
== IIO_VAL_INT
)
394 return sprintf(buf
, "%d\n", val
);
395 else if (ret
== IIO_VAL_INT_PLUS_MICRO
) {
397 return sprintf(buf
, "-%d.%06u\n", val
, -val2
);
399 return sprintf(buf
, "%d.%06u\n", val
, val2
);
400 } else if (ret
== IIO_VAL_INT_PLUS_NANO
) {
402 return sprintf(buf
, "-%d.%09u\n", val
, -val2
);
404 return sprintf(buf
, "%d.%09u\n", val
, val2
);
409 static ssize_t
iio_write_channel_info(struct device
*dev
,
410 struct device_attribute
*attr
,
414 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
415 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
416 int ret
, integer
= 0, fract
= 0, fract_mult
= 100000;
417 bool integer_part
= true, negative
= false;
419 /* Assumes decimal - precision based on number of digits */
420 if (!indio_dev
->info
->write_raw
)
423 if (indio_dev
->info
->write_raw_get_fmt
)
424 switch (indio_dev
->info
->write_raw_get_fmt(indio_dev
,
425 this_attr
->c
, this_attr
->address
)) {
426 case IIO_VAL_INT_PLUS_MICRO
:
429 case IIO_VAL_INT_PLUS_NANO
:
430 fract_mult
= 100000000;
442 if ('0' <= *buf
&& *buf
<= '9') {
444 integer
= integer
*10 + *buf
- '0';
446 fract
+= fract_mult
*(*buf
- '0');
451 } else if (*buf
== '\n') {
452 if (*(buf
+ 1) == '\0')
456 } else if (*buf
== '.') {
457 integer_part
= false;
470 ret
= indio_dev
->info
->write_raw(indio_dev
, this_attr
->c
,
471 integer
, fract
, this_attr
->address
);
478 static int __iio_build_postfix(struct iio_chan_spec
const *chan
,
484 /* 3 options - generic, extend_name, modified - if generic, extend_name
485 * and modified cannot apply.*/
487 if (generic
|| (!chan
->modified
&& !chan
->extend_name
)) {
488 all_post
= kasprintf(GFP_KERNEL
, "%s", postfix
);
489 } else if (chan
->modified
) {
490 const char *intermediate
;
491 switch (chan
->type
) {
494 = iio_modifier_names_light
[chan
->channel2
];
503 = iio_modifier_names_axial
[chan
->channel2
];
508 if (chan
->extend_name
)
509 all_post
= kasprintf(GFP_KERNEL
, "%s_%s_%s",
514 all_post
= kasprintf(GFP_KERNEL
, "%s_%s",
518 all_post
= kasprintf(GFP_KERNEL
, "%s_%s", chan
->extend_name
,
520 if (all_post
== NULL
)
526 int __iio_device_attr_init(struct device_attribute
*dev_attr
,
528 struct iio_chan_spec
const *chan
,
529 ssize_t (*readfunc
)(struct device
*dev
,
530 struct device_attribute
*attr
,
532 ssize_t (*writefunc
)(struct device
*dev
,
533 struct device_attribute
*attr
,
539 char *name_format
, *full_postfix
;
540 sysfs_attr_init(&dev_attr
->attr
);
541 ret
= __iio_build_postfix(chan
, generic
, postfix
, &full_postfix
);
545 /* Special case for types that uses both channel numbers in naming */
546 if (chan
->type
== IIO_IN_DIFF
&& !generic
)
548 = kasprintf(GFP_KERNEL
, "%s_%s",
549 iio_chan_type_name_spec_complex
[chan
->type
],
551 else if (generic
|| !chan
->indexed
)
553 = kasprintf(GFP_KERNEL
, "%s_%s",
554 iio_chan_type_name_spec_shared
[chan
->type
],
558 = kasprintf(GFP_KERNEL
, "%s%d_%s",
559 iio_chan_type_name_spec_shared
[chan
->type
],
563 if (name_format
== NULL
) {
565 goto error_free_full_postfix
;
567 dev_attr
->attr
.name
= kasprintf(GFP_KERNEL
,
571 if (dev_attr
->attr
.name
== NULL
) {
573 goto error_free_name_format
;
577 dev_attr
->attr
.mode
|= S_IRUGO
;
578 dev_attr
->show
= readfunc
;
582 dev_attr
->attr
.mode
|= S_IWUSR
;
583 dev_attr
->store
= writefunc
;
590 error_free_name_format
:
592 error_free_full_postfix
:
598 void __iio_device_attr_deinit(struct device_attribute
*dev_attr
)
600 kfree(dev_attr
->attr
.name
);
603 int __iio_add_chan_devattr(const char *postfix
,
605 struct iio_chan_spec
const *chan
,
606 ssize_t (*readfunc
)(struct device
*dev
,
607 struct device_attribute
*attr
,
609 ssize_t (*writefunc
)(struct device
*dev
,
610 struct device_attribute
*attr
,
616 struct list_head
*attr_list
)
619 struct iio_dev_attr
*iio_attr
, *t
;
621 iio_attr
= kzalloc(sizeof *iio_attr
, GFP_KERNEL
);
622 if (iio_attr
== NULL
) {
626 ret
= __iio_device_attr_init(&iio_attr
->dev_attr
,
628 readfunc
, writefunc
, generic
);
630 goto error_iio_dev_attr_free
;
632 iio_attr
->address
= mask
;
633 list_for_each_entry(t
, attr_list
, l
)
634 if (strcmp(t
->dev_attr
.attr
.name
,
635 iio_attr
->dev_attr
.attr
.name
) == 0) {
637 dev_err(dev
, "tried to double register : %s\n",
638 t
->dev_attr
.attr
.name
);
640 goto error_device_attr_deinit
;
643 ret
= sysfs_add_file_to_group(&dev
->kobj
,
644 &iio_attr
->dev_attr
.attr
, group
);
646 goto error_device_attr_deinit
;
648 list_add(&iio_attr
->l
, attr_list
);
652 error_device_attr_deinit
:
653 __iio_device_attr_deinit(&iio_attr
->dev_attr
);
654 error_iio_dev_attr_free
:
660 static int iio_device_add_channel_sysfs(struct iio_dev
*dev_info
,
661 struct iio_chan_spec
const *chan
)
666 if (chan
->channel
< 0)
668 if (chan
->processed_val
)
669 ret
= __iio_add_chan_devattr("input", NULL
, chan
,
670 &iio_read_channel_info
,
675 &dev_info
->channel_attr_list
);
677 ret
= __iio_add_chan_devattr("raw", NULL
, chan
,
678 &iio_read_channel_info
,
679 (chan
->type
== IIO_OUT
?
680 &iio_write_channel_info
: NULL
),
684 &dev_info
->channel_attr_list
);
688 for_each_set_bit(i
, &chan
->info_mask
, sizeof(long)*8) {
689 ret
= __iio_add_chan_devattr(iio_chan_info_postfix
[i
/2],
691 &iio_read_channel_info
,
692 &iio_write_channel_info
,
696 &dev_info
->channel_attr_list
);
697 if (ret
== -EBUSY
&& (i
%2 == 0)) {
708 static void iio_device_remove_and_free_read_attr(struct iio_dev
*dev_info
,
709 struct iio_dev_attr
*p
)
711 sysfs_remove_file_from_group(&dev_info
->dev
.kobj
,
712 &p
->dev_attr
.attr
, NULL
);
713 kfree(p
->dev_attr
.attr
.name
);
717 static ssize_t
iio_show_dev_name(struct device
*dev
,
718 struct device_attribute
*attr
,
721 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
722 return sprintf(buf
, "%s\n", indio_dev
->name
);
725 static DEVICE_ATTR(name
, S_IRUGO
, iio_show_dev_name
, NULL
);
727 static int iio_device_register_sysfs(struct iio_dev
*dev_info
)
730 struct iio_dev_attr
*p
, *n
;
732 if (dev_info
->info
->attrs
) {
733 ret
= sysfs_create_group(&dev_info
->dev
.kobj
,
734 dev_info
->info
->attrs
);
736 dev_err(dev_info
->dev
.parent
,
737 "Failed to register sysfs hooks\n");
743 * New channel registration method - relies on the fact a group does
744 * not need to be initialized if it is name is NULL.
746 INIT_LIST_HEAD(&dev_info
->channel_attr_list
);
747 if (dev_info
->channels
)
748 for (i
= 0; i
< dev_info
->num_channels
; i
++) {
749 ret
= iio_device_add_channel_sysfs(dev_info
,
753 goto error_clear_attrs
;
755 if (dev_info
->name
) {
756 ret
= sysfs_add_file_to_group(&dev_info
->dev
.kobj
,
760 goto error_clear_attrs
;
765 list_for_each_entry_safe(p
, n
,
766 &dev_info
->channel_attr_list
, l
) {
768 iio_device_remove_and_free_read_attr(dev_info
, p
);
770 if (dev_info
->info
->attrs
)
771 sysfs_remove_group(&dev_info
->dev
.kobj
, dev_info
->info
->attrs
);
777 static void iio_device_unregister_sysfs(struct iio_dev
*dev_info
)
780 struct iio_dev_attr
*p
, *n
;
782 sysfs_remove_file_from_group(&dev_info
->dev
.kobj
,
785 list_for_each_entry_safe(p
, n
, &dev_info
->channel_attr_list
, l
) {
787 iio_device_remove_and_free_read_attr(dev_info
, p
);
790 if (dev_info
->info
->attrs
)
791 sysfs_remove_group(&dev_info
->dev
.kobj
, dev_info
->info
->attrs
);
794 /* Return a negative errno on failure */
795 int iio_get_new_ida_val(struct ida
*this_ida
)
801 if (unlikely(ida_pre_get(this_ida
, GFP_KERNEL
) == 0))
804 spin_lock(&iio_ida_lock
);
805 ret
= ida_get_new(this_ida
, &val
);
806 spin_unlock(&iio_ida_lock
);
807 if (unlikely(ret
== -EAGAIN
))
809 else if (unlikely(ret
))
814 EXPORT_SYMBOL(iio_get_new_ida_val
);
816 void iio_free_ida_val(struct ida
*this_ida
, int id
)
818 spin_lock(&iio_ida_lock
);
819 ida_remove(this_ida
, id
);
820 spin_unlock(&iio_ida_lock
);
822 EXPORT_SYMBOL(iio_free_ida_val
);
824 static const char * const iio_ev_type_text
[] = {
825 [IIO_EV_TYPE_THRESH
] = "thresh",
826 [IIO_EV_TYPE_MAG
] = "mag",
827 [IIO_EV_TYPE_ROC
] = "roc"
830 static const char * const iio_ev_dir_text
[] = {
831 [IIO_EV_DIR_EITHER
] = "either",
832 [IIO_EV_DIR_RISING
] = "rising",
833 [IIO_EV_DIR_FALLING
] = "falling"
836 static ssize_t
iio_ev_state_store(struct device
*dev
,
837 struct device_attribute
*attr
,
841 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
842 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
846 ret
= strtobool(buf
, &val
);
850 ret
= indio_dev
->info
->write_event_config(indio_dev
,
853 return (ret
< 0) ? ret
: len
;
856 static ssize_t
iio_ev_state_show(struct device
*dev
,
857 struct device_attribute
*attr
,
860 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
861 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
862 int val
= indio_dev
->info
->read_event_config(indio_dev
,
868 return sprintf(buf
, "%d\n", val
);
871 static ssize_t
iio_ev_value_show(struct device
*dev
,
872 struct device_attribute
*attr
,
875 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
876 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
879 ret
= indio_dev
->info
->read_event_value(indio_dev
,
880 this_attr
->address
, &val
);
884 return sprintf(buf
, "%d\n", val
);
887 static ssize_t
iio_ev_value_store(struct device
*dev
,
888 struct device_attribute
*attr
,
892 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
893 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
897 ret
= strict_strtoul(buf
, 10, &val
);
901 ret
= indio_dev
->info
->write_event_value(indio_dev
, this_attr
->address
,
909 static int iio_device_add_event_sysfs(struct iio_dev
*dev_info
,
910 struct iio_chan_spec
const *chan
)
913 int ret
= 0, i
, mask
;
915 if (!chan
->event_mask
)
918 for_each_set_bit(i
, &chan
->event_mask
, sizeof(chan
->event_mask
)*8) {
919 postfix
= kasprintf(GFP_KERNEL
, "%s_%s_en",
920 iio_ev_type_text
[i
/IIO_EV_TYPE_MAX
],
921 iio_ev_dir_text
[i
%IIO_EV_TYPE_MAX
]);
922 if (postfix
== NULL
) {
926 switch (chan
->type
) {
927 /* Switch this to a table at some point */
929 mask
= IIO_UNMOD_EVENT_CODE(chan
->type
, chan
->channel
,
934 mask
= IIO_MOD_EVENT_CODE(chan
->type
, 0, chan
->channel
,
939 mask
= IIO_MOD_EVENT_CODE(chan
->type
, chan
->channel
,
945 printk(KERN_INFO
"currently unhandled type of event\n");
947 ret
= __iio_add_chan_devattr(postfix
,
953 /*HACK. - limits us to one
954 event interface - fix by
955 extending the bitmask - but
958 &dev_info
->event_interfaces
[0].dev
,
959 &dev_info
->event_interfaces
[0].
965 postfix
= kasprintf(GFP_KERNEL
, "%s_%s_value",
966 iio_ev_type_text
[i
/IIO_EV_TYPE_MAX
],
967 iio_ev_dir_text
[i
%IIO_EV_TYPE_MAX
]);
968 if (postfix
== NULL
) {
972 ret
= __iio_add_chan_devattr(postfix
, NULL
, chan
,
977 &dev_info
->event_interfaces
[0]
979 &dev_info
->event_interfaces
[0]
991 static inline void __iio_remove_all_event_sysfs(struct iio_dev
*dev_info
,
992 const char *groupname
,
995 struct iio_dev_attr
*p
, *n
;
996 list_for_each_entry_safe(p
, n
,
997 &dev_info
->event_interfaces
[num
].
999 sysfs_remove_file_from_group(&dev_info
1000 ->event_interfaces
[num
].dev
.kobj
,
1003 kfree(p
->dev_attr
.attr
.name
);
1008 static inline int __iio_add_event_config_attrs(struct iio_dev
*dev_info
, int i
)
1012 INIT_LIST_HEAD(&dev_info
->event_interfaces
[0].dev_attr_list
);
1013 /* Dynically created from the channels array */
1014 if (dev_info
->channels
) {
1015 for (j
= 0; j
< dev_info
->num_channels
; j
++) {
1016 ret
= iio_device_add_event_sysfs(dev_info
,
1020 goto error_clear_attrs
;
1026 __iio_remove_all_event_sysfs(dev_info
, NULL
, i
);
1031 static inline int __iio_remove_event_config_attrs(struct iio_dev
*dev_info
,
1034 __iio_remove_all_event_sysfs(dev_info
, NULL
, i
);
1038 static int iio_device_register_eventset(struct iio_dev
*dev_info
)
1042 if (dev_info
->info
->num_interrupt_lines
== 0)
1045 dev_info
->event_interfaces
=
1046 kzalloc(sizeof(struct iio_event_interface
)
1047 *dev_info
->info
->num_interrupt_lines
,
1049 if (dev_info
->event_interfaces
== NULL
) {
1054 for (i
= 0; i
< dev_info
->info
->num_interrupt_lines
; i
++) {
1055 ret
= iio_setup_ev_int(&dev_info
->event_interfaces
[i
],
1056 dev_name(&dev_info
->dev
),
1058 dev_info
->info
->driver_module
,
1061 dev_err(&dev_info
->dev
,
1062 "Could not get chrdev interface\n");
1063 goto error_free_setup_ev_ints
;
1066 dev_set_drvdata(&dev_info
->event_interfaces
[i
].dev
,
1069 if (dev_info
->info
->event_attrs
!= NULL
)
1070 ret
= sysfs_create_group(&dev_info
1071 ->event_interfaces
[i
]
1077 dev_err(&dev_info
->dev
,
1078 "Failed to register sysfs for event attrs");
1079 goto error_remove_sysfs_interfaces
;
1083 for (i
= 0; i
< dev_info
->info
->num_interrupt_lines
; i
++) {
1084 ret
= __iio_add_event_config_attrs(dev_info
, i
);
1086 goto error_unregister_config_attrs
;
1091 error_unregister_config_attrs
:
1092 for (j
= 0; j
< i
; j
++)
1093 __iio_remove_event_config_attrs(dev_info
, i
);
1094 i
= dev_info
->info
->num_interrupt_lines
- 1;
1095 error_remove_sysfs_interfaces
:
1096 for (j
= 0; j
< i
; j
++)
1097 if (dev_info
->info
->event_attrs
!= NULL
)
1098 sysfs_remove_group(&dev_info
1099 ->event_interfaces
[j
].dev
.kobj
,
1100 &dev_info
->info
->event_attrs
[j
]);
1101 error_free_setup_ev_ints
:
1102 for (j
= 0; j
< i
; j
++)
1103 iio_free_ev_int(&dev_info
->event_interfaces
[j
]);
1104 kfree(dev_info
->event_interfaces
);
1110 static void iio_device_unregister_eventset(struct iio_dev
*dev_info
)
1114 if (dev_info
->info
->num_interrupt_lines
== 0)
1116 for (i
= 0; i
< dev_info
->info
->num_interrupt_lines
; i
++) {
1117 __iio_remove_event_config_attrs(dev_info
, i
);
1118 if (dev_info
->info
->event_attrs
!= NULL
)
1119 sysfs_remove_group(&dev_info
1120 ->event_interfaces
[i
].dev
.kobj
,
1121 &dev_info
->info
->event_attrs
[i
]);
1124 for (i
= 0; i
< dev_info
->info
->num_interrupt_lines
; i
++)
1125 iio_free_ev_int(&dev_info
->event_interfaces
[i
]);
1126 kfree(dev_info
->event_interfaces
);
1129 static void iio_dev_release(struct device
*device
)
1132 kfree(to_iio_dev(device
));
1135 static struct device_type iio_dev_type
= {
1136 .name
= "iio_device",
1137 .release
= iio_dev_release
,
1140 struct iio_dev
*iio_allocate_device(int sizeof_priv
)
1142 struct iio_dev
*dev
;
1145 alloc_size
= sizeof(struct iio_dev
);
1147 alloc_size
= ALIGN(alloc_size
, IIO_ALIGN
);
1148 alloc_size
+= sizeof_priv
;
1150 /* ensure 32-byte alignment of whole construct ? */
1151 alloc_size
+= IIO_ALIGN
- 1;
1153 dev
= kzalloc(alloc_size
, GFP_KERNEL
);
1156 dev
->dev
.type
= &iio_dev_type
;
1157 dev
->dev
.bus
= &iio_bus_type
;
1158 device_initialize(&dev
->dev
);
1159 dev_set_drvdata(&dev
->dev
, (void *)dev
);
1160 mutex_init(&dev
->mlock
);
1166 EXPORT_SYMBOL(iio_allocate_device
);
1168 void iio_free_device(struct iio_dev
*dev
)
1171 iio_put_device(dev
);
1173 EXPORT_SYMBOL(iio_free_device
);
1175 int iio_device_register(struct iio_dev
*dev_info
)
1179 dev_info
->id
= iio_get_new_ida_val(&iio_ida
);
1180 if (dev_info
->id
< 0) {
1182 dev_err(&dev_info
->dev
, "Failed to get id\n");
1185 dev_set_name(&dev_info
->dev
, "device%d", dev_info
->id
);
1187 ret
= device_add(&dev_info
->dev
);
1189 goto error_free_ida
;
1190 ret
= iio_device_register_sysfs(dev_info
);
1192 dev_err(dev_info
->dev
.parent
,
1193 "Failed to register sysfs interfaces\n");
1194 goto error_del_device
;
1196 ret
= iio_device_register_eventset(dev_info
);
1198 dev_err(dev_info
->dev
.parent
,
1199 "Failed to register event set\n");
1200 goto error_free_sysfs
;
1202 if (dev_info
->modes
& INDIO_RING_TRIGGERED
)
1203 iio_device_register_trigger_consumer(dev_info
);
1208 iio_device_unregister_sysfs(dev_info
);
1210 device_del(&dev_info
->dev
);
1212 iio_free_ida_val(&iio_ida
, dev_info
->id
);
1216 EXPORT_SYMBOL(iio_device_register
);
1218 void iio_device_unregister(struct iio_dev
*dev_info
)
1220 if (dev_info
->modes
& INDIO_RING_TRIGGERED
)
1221 iio_device_unregister_trigger_consumer(dev_info
);
1222 iio_device_unregister_eventset(dev_info
);
1223 iio_device_unregister_sysfs(dev_info
);
1224 iio_free_ida_val(&iio_ida
, dev_info
->id
);
1225 device_unregister(&dev_info
->dev
);
1227 EXPORT_SYMBOL(iio_device_unregister
);
1231 module_put(THIS_MODULE
);
1236 __module_get(THIS_MODULE
);
1239 subsys_initcall(iio_init
);
1240 module_exit(iio_exit
);
1242 MODULE_AUTHOR("Jonathan Cameron <jic23@cam.ac.uk>");
1243 MODULE_DESCRIPTION("Industrial I/O core");
1244 MODULE_LICENSE("GPL");