Commit | Line | Data |
---|---|---|
f9a7e9b2 MH |
1 | /* |
2 | * Copyright 2011 Analog Devices Inc. | |
3 | * | |
4 | * Licensed under the GPL-2. | |
5 | * | |
6 | */ | |
7 | ||
8 | #include <linux/kernel.h> | |
9 | #include <linux/module.h> | |
10 | #include <linux/platform_device.h> | |
11 | #include <linux/slab.h> | |
1f785681 | 12 | #include <linux/list.h> |
f38bc926 | 13 | #include <linux/irq_work.h> |
f9a7e9b2 | 14 | |
06458e27 JC |
15 | #include <linux/iio/iio.h> |
16 | #include <linux/iio/trigger.h> | |
f9a7e9b2 | 17 | |
1f785681 JC |
18 | struct iio_sysfs_trig { |
19 | struct iio_trigger *trig; | |
f38bc926 | 20 | struct irq_work work; |
1f785681 JC |
21 | int id; |
22 | struct list_head l; | |
23 | }; | |
24 | ||
25 | static LIST_HEAD(iio_sysfs_trig_list); | |
10a485c5 | 26 | static DEFINE_MUTEX(iio_sysfs_trig_list_mut); |
1f785681 JC |
27 | |
28 | static int iio_sysfs_trigger_probe(int id); | |
29 | static ssize_t iio_sysfs_trig_add(struct device *dev, | |
30 | struct device_attribute *attr, | |
31 | const char *buf, | |
32 | size_t len) | |
33 | { | |
34 | int ret; | |
35 | unsigned long input; | |
36 | ||
ddeb64f3 | 37 | ret = kstrtoul(buf, 10, &input); |
1f785681 JC |
38 | if (ret) |
39 | return ret; | |
40 | ret = iio_sysfs_trigger_probe(input); | |
41 | if (ret) | |
42 | return ret; | |
43 | return len; | |
44 | } | |
45 | static DEVICE_ATTR(add_trigger, S_IWUSR, NULL, &iio_sysfs_trig_add); | |
46 | ||
47 | static int iio_sysfs_trigger_remove(int id); | |
48 | static ssize_t iio_sysfs_trig_remove(struct device *dev, | |
49 | struct device_attribute *attr, | |
50 | const char *buf, | |
51 | size_t len) | |
52 | { | |
53 | int ret; | |
54 | unsigned long input; | |
55 | ||
ddeb64f3 | 56 | ret = kstrtoul(buf, 10, &input); |
1f785681 JC |
57 | if (ret) |
58 | return ret; | |
59 | ret = iio_sysfs_trigger_remove(input); | |
60 | if (ret) | |
61 | return ret; | |
62 | return len; | |
63 | } | |
64 | ||
65 | static DEVICE_ATTR(remove_trigger, S_IWUSR, NULL, &iio_sysfs_trig_remove); | |
66 | ||
67 | static struct attribute *iio_sysfs_trig_attrs[] = { | |
68 | &dev_attr_add_trigger.attr, | |
69 | &dev_attr_remove_trigger.attr, | |
70 | NULL, | |
71 | }; | |
72 | ||
73 | static const struct attribute_group iio_sysfs_trig_group = { | |
74 | .attrs = iio_sysfs_trig_attrs, | |
75 | }; | |
76 | ||
77 | static const struct attribute_group *iio_sysfs_trig_groups[] = { | |
78 | &iio_sysfs_trig_group, | |
79 | NULL | |
80 | }; | |
81 | ||
9c0c22bd JC |
82 | |
83 | /* Nothing to actually do upon release */ | |
84 | static void iio_trigger_sysfs_release(struct device *dev) | |
85 | { | |
86 | } | |
87 | ||
1f785681 JC |
88 | static struct device iio_sysfs_trig_dev = { |
89 | .bus = &iio_bus_type, | |
90 | .groups = iio_sysfs_trig_groups, | |
9c0c22bd | 91 | .release = &iio_trigger_sysfs_release, |
1f785681 JC |
92 | }; |
93 | ||
f38bc926 LPC |
94 | static void iio_sysfs_trigger_work(struct irq_work *work) |
95 | { | |
96 | struct iio_sysfs_trig *trig = container_of(work, struct iio_sysfs_trig, | |
97 | work); | |
98 | ||
398fd22b | 99 | iio_trigger_poll(trig->trig); |
f38bc926 LPC |
100 | } |
101 | ||
f9a7e9b2 MH |
102 | static ssize_t iio_sysfs_trigger_poll(struct device *dev, |
103 | struct device_attribute *attr, const char *buf, size_t count) | |
104 | { | |
4bf81727 | 105 | struct iio_trigger *trig = to_iio_trigger(dev); |
1e9663c6 | 106 | struct iio_sysfs_trig *sysfs_trig = iio_trigger_get_drvdata(trig); |
f38bc926 LPC |
107 | |
108 | irq_work_queue(&sysfs_trig->work); | |
f9a7e9b2 MH |
109 | |
110 | return count; | |
111 | } | |
112 | ||
113 | static DEVICE_ATTR(trigger_now, S_IWUSR, NULL, iio_sysfs_trigger_poll); | |
f9a7e9b2 MH |
114 | |
115 | static struct attribute *iio_sysfs_trigger_attrs[] = { | |
116 | &dev_attr_trigger_now.attr, | |
f9a7e9b2 MH |
117 | NULL, |
118 | }; | |
119 | ||
120 | static const struct attribute_group iio_sysfs_trigger_attr_group = { | |
121 | .attrs = iio_sysfs_trigger_attrs, | |
122 | }; | |
123 | ||
59c85e82 JC |
124 | static const struct attribute_group *iio_sysfs_trigger_attr_groups[] = { |
125 | &iio_sysfs_trigger_attr_group, | |
126 | NULL | |
127 | }; | |
128 | ||
d29f73db JC |
129 | static const struct iio_trigger_ops iio_sysfs_trigger_ops = { |
130 | .owner = THIS_MODULE, | |
131 | }; | |
132 | ||
1f785681 | 133 | static int iio_sysfs_trigger_probe(int id) |
f9a7e9b2 | 134 | { |
1f785681 | 135 | struct iio_sysfs_trig *t; |
f9a7e9b2 | 136 | int ret; |
1f785681 | 137 | bool foundit = false; |
450a5ff7 | 138 | |
10a485c5 | 139 | mutex_lock(&iio_sysfs_trig_list_mut); |
1f785681 JC |
140 | list_for_each_entry(t, &iio_sysfs_trig_list, l) |
141 | if (id == t->id) { | |
142 | foundit = true; | |
143 | break; | |
144 | } | |
145 | if (foundit) { | |
146 | ret = -EINVAL; | |
147 | goto out1; | |
148 | } | |
1f785681 JC |
149 | t = kmalloc(sizeof(*t), GFP_KERNEL); |
150 | if (t == NULL) { | |
f9a7e9b2 | 151 | ret = -ENOMEM; |
59c85e82 | 152 | goto out1; |
1f785681 JC |
153 | } |
154 | t->id = id; | |
7cbb7537 | 155 | t->trig = iio_trigger_alloc("sysfstrig%d", id); |
1f785681 JC |
156 | if (!t->trig) { |
157 | ret = -ENOMEM; | |
158 | goto free_t; | |
f9a7e9b2 MH |
159 | } |
160 | ||
59c85e82 | 161 | t->trig->dev.groups = iio_sysfs_trigger_attr_groups; |
d29f73db | 162 | t->trig->ops = &iio_sysfs_trigger_ops; |
1f785681 | 163 | t->trig->dev.parent = &iio_sysfs_trig_dev; |
1e9663c6 | 164 | iio_trigger_set_drvdata(t->trig, t); |
f38bc926 LPC |
165 | |
166 | init_irq_work(&t->work, iio_sysfs_trigger_work); | |
f9a7e9b2 | 167 | |
1f785681 JC |
168 | ret = iio_trigger_register(t->trig); |
169 | if (ret) | |
170 | goto out2; | |
171 | list_add(&t->l, &iio_sysfs_trig_list); | |
172 | __module_get(THIS_MODULE); | |
10a485c5 | 173 | mutex_unlock(&iio_sysfs_trig_list_mut); |
f9a7e9b2 | 174 | return 0; |
1f785681 | 175 | |
f9a7e9b2 | 176 | out2: |
7cbb7537 | 177 | iio_trigger_put(t->trig); |
1f785681 JC |
178 | free_t: |
179 | kfree(t); | |
f9a7e9b2 | 180 | out1: |
10a485c5 | 181 | mutex_unlock(&iio_sysfs_trig_list_mut); |
f9a7e9b2 MH |
182 | return ret; |
183 | } | |
184 | ||
1f785681 | 185 | static int iio_sysfs_trigger_remove(int id) |
f9a7e9b2 | 186 | { |
1f785681 JC |
187 | bool foundit = false; |
188 | struct iio_sysfs_trig *t; | |
450a5ff7 | 189 | |
10a485c5 | 190 | mutex_lock(&iio_sysfs_trig_list_mut); |
1f785681 JC |
191 | list_for_each_entry(t, &iio_sysfs_trig_list, l) |
192 | if (id == t->id) { | |
193 | foundit = true; | |
194 | break; | |
195 | } | |
196 | if (!foundit) { | |
10a485c5 | 197 | mutex_unlock(&iio_sysfs_trig_list_mut); |
1f785681 JC |
198 | return -EINVAL; |
199 | } | |
f9a7e9b2 | 200 | |
1f785681 | 201 | iio_trigger_unregister(t->trig); |
7cbb7537 | 202 | iio_trigger_free(t->trig); |
f9a7e9b2 | 203 | |
1f785681 JC |
204 | list_del(&t->l); |
205 | kfree(t); | |
206 | module_put(THIS_MODULE); | |
10a485c5 | 207 | mutex_unlock(&iio_sysfs_trig_list_mut); |
f9a7e9b2 MH |
208 | return 0; |
209 | } | |
210 | ||
f9a7e9b2 MH |
211 | |
212 | static int __init iio_sysfs_trig_init(void) | |
213 | { | |
1f785681 JC |
214 | device_initialize(&iio_sysfs_trig_dev); |
215 | dev_set_name(&iio_sysfs_trig_dev, "iio_sysfs_trigger"); | |
216 | return device_add(&iio_sysfs_trig_dev); | |
f9a7e9b2 MH |
217 | } |
218 | module_init(iio_sysfs_trig_init); | |
219 | ||
220 | static void __exit iio_sysfs_trig_exit(void) | |
221 | { | |
1f785681 | 222 | device_unregister(&iio_sysfs_trig_dev); |
f9a7e9b2 MH |
223 | } |
224 | module_exit(iio_sysfs_trig_exit); | |
225 | ||
226 | MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>"); | |
227 | MODULE_DESCRIPTION("Sysfs based trigger for the iio subsystem"); | |
228 | MODULE_LICENSE("GPL v2"); | |
229 | MODULE_ALIAS("platform:iio-trig-sysfs"); |