2 * Copyright 2011 Analog Devices Inc.
4 * Licensed under the GPL-2.
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/platform_device.h>
11 #include <linux/slab.h>
12 #include <linux/list.h>
15 #include "../trigger.h"
17 struct iio_sysfs_trig
{
18 struct iio_trigger
*trig
;
23 static LIST_HEAD(iio_sysfs_trig_list
);
24 static DEFINE_MUTEX(iio_syfs_trig_list_mut
);
26 static int iio_sysfs_trigger_probe(int id
);
27 static ssize_t
iio_sysfs_trig_add(struct device
*dev
,
28 struct device_attribute
*attr
,
35 ret
= strict_strtoul(buf
, 10, &input
);
38 ret
= iio_sysfs_trigger_probe(input
);
43 static DEVICE_ATTR(add_trigger
, S_IWUSR
, NULL
, &iio_sysfs_trig_add
);
45 static int iio_sysfs_trigger_remove(int id
);
46 static ssize_t
iio_sysfs_trig_remove(struct device
*dev
,
47 struct device_attribute
*attr
,
54 ret
= strict_strtoul(buf
, 10, &input
);
57 ret
= iio_sysfs_trigger_remove(input
);
63 static DEVICE_ATTR(remove_trigger
, S_IWUSR
, NULL
, &iio_sysfs_trig_remove
);
65 static struct attribute
*iio_sysfs_trig_attrs
[] = {
66 &dev_attr_add_trigger
.attr
,
67 &dev_attr_remove_trigger
.attr
,
71 static const struct attribute_group iio_sysfs_trig_group
= {
72 .attrs
= iio_sysfs_trig_attrs
,
75 static const struct attribute_group
*iio_sysfs_trig_groups
[] = {
76 &iio_sysfs_trig_group
,
81 /* Nothing to actually do upon release */
82 static void iio_trigger_sysfs_release(struct device
*dev
)
86 static struct device iio_sysfs_trig_dev
= {
88 .groups
= iio_sysfs_trig_groups
,
89 .release
= &iio_trigger_sysfs_release
,
92 static ssize_t
iio_sysfs_trigger_poll(struct device
*dev
,
93 struct device_attribute
*attr
, const char *buf
, size_t count
)
95 struct iio_trigger
*trig
= dev_get_drvdata(dev
);
96 iio_trigger_poll_chained(trig
, 0);
101 static DEVICE_ATTR(trigger_now
, S_IWUSR
, NULL
, iio_sysfs_trigger_poll
);
103 static struct attribute
*iio_sysfs_trigger_attrs
[] = {
104 &dev_attr_trigger_now
.attr
,
108 static const struct attribute_group iio_sysfs_trigger_attr_group
= {
109 .attrs
= iio_sysfs_trigger_attrs
,
112 static const struct attribute_group
*iio_sysfs_trigger_attr_groups
[] = {
113 &iio_sysfs_trigger_attr_group
,
117 static const struct iio_trigger_ops iio_sysfs_trigger_ops
= {
118 .owner
= THIS_MODULE
,
121 static int iio_sysfs_trigger_probe(int id
)
123 struct iio_sysfs_trig
*t
;
125 bool foundit
= false;
126 mutex_lock(&iio_syfs_trig_list_mut
);
127 list_for_each_entry(t
, &iio_sysfs_trig_list
, l
)
136 t
= kmalloc(sizeof(*t
), GFP_KERNEL
);
142 t
->trig
= iio_allocate_trigger("sysfstrig%d", id
);
148 t
->trig
->dev
.groups
= iio_sysfs_trigger_attr_groups
;
149 t
->trig
->ops
= &iio_sysfs_trigger_ops
;
150 t
->trig
->dev
.parent
= &iio_sysfs_trig_dev
;
152 ret
= iio_trigger_register(t
->trig
);
155 list_add(&t
->l
, &iio_sysfs_trig_list
);
156 __module_get(THIS_MODULE
);
157 mutex_unlock(&iio_syfs_trig_list_mut
);
161 iio_put_trigger(t
->trig
);
165 mutex_unlock(&iio_syfs_trig_list_mut
);
169 static int iio_sysfs_trigger_remove(int id
)
171 bool foundit
= false;
172 struct iio_sysfs_trig
*t
;
173 mutex_lock(&iio_syfs_trig_list_mut
);
174 list_for_each_entry(t
, &iio_sysfs_trig_list
, l
)
180 mutex_unlock(&iio_syfs_trig_list_mut
);
184 iio_trigger_unregister(t
->trig
);
185 iio_free_trigger(t
->trig
);
189 module_put(THIS_MODULE
);
190 mutex_unlock(&iio_syfs_trig_list_mut
);
195 static int __init
iio_sysfs_trig_init(void)
197 device_initialize(&iio_sysfs_trig_dev
);
198 dev_set_name(&iio_sysfs_trig_dev
, "iio_sysfs_trigger");
199 return device_add(&iio_sysfs_trig_dev
);
201 module_init(iio_sysfs_trig_init
);
203 static void __exit
iio_sysfs_trig_exit(void)
205 device_unregister(&iio_sysfs_trig_dev
);
207 module_exit(iio_sysfs_trig_exit
);
209 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
210 MODULE_DESCRIPTION("Sysfs based trigger for the iio subsystem");
211 MODULE_LICENSE("GPL v2");
212 MODULE_ALIAS("platform:iio-trig-sysfs");