Commit | Line | Data |
---|---|---|
1637db44 JC |
1 | /* The industrial I/O core, trigger handling functions |
2 | * | |
3 | * Copyright (c) 2008 Jonathan Cameron | |
4 | * | |
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. | |
8 | */ | |
9 | ||
10 | #include <linux/kernel.h> | |
11 | #include <linux/module.h> | |
12 | #include <linux/idr.h> | |
13 | #include <linux/err.h> | |
14 | #include <linux/device.h> | |
15 | #include <linux/interrupt.h> | |
16 | #include <linux/list.h> | |
5a0e3ad6 | 17 | #include <linux/slab.h> |
1637db44 JC |
18 | |
19 | #include "iio.h" | |
20 | #include "trigger.h" | |
74112d3f | 21 | #include "trigger_consumer.h" |
1637db44 JC |
22 | |
23 | /* RFC - Question of approach | |
24 | * Make the common case (single sensor single trigger) | |
25 | * simple by starting trigger capture from when first sensors | |
26 | * is added. | |
27 | * | |
28 | * Complex simultaneous start requires use of 'hold' functionality | |
29 | * of the trigger. (not implemented) | |
30 | * | |
31 | * Any other suggestions? | |
32 | */ | |
33 | ||
34 | ||
35 | static DEFINE_IDR(iio_trigger_idr); | |
36 | static DEFINE_SPINLOCK(iio_trigger_idr_lock); | |
37 | ||
38 | /* Single list of all available triggers */ | |
39 | static LIST_HEAD(iio_trigger_list); | |
40 | static DEFINE_MUTEX(iio_trigger_list_lock); | |
41 | ||
42 | /** | |
43 | * iio_trigger_register_sysfs() - create a device for this trigger | |
44 | * @trig_info: the trigger | |
45 | * | |
46 | * Also adds any control attribute registered by the trigger driver | |
47 | **/ | |
48 | static int iio_trigger_register_sysfs(struct iio_trigger *trig_info) | |
49 | { | |
50 | int ret = 0; | |
51 | ||
52 | if (trig_info->control_attrs) | |
53 | ret = sysfs_create_group(&trig_info->dev.kobj, | |
54 | trig_info->control_attrs); | |
55 | ||
56 | return ret; | |
57 | } | |
58 | ||
59 | static void iio_trigger_unregister_sysfs(struct iio_trigger *trig_info) | |
60 | { | |
61 | if (trig_info->control_attrs) | |
62 | sysfs_remove_group(&trig_info->dev.kobj, | |
63 | trig_info->control_attrs); | |
64 | } | |
65 | ||
66 | ||
67 | /** | |
68 | * iio_trigger_register_id() - get a unique id for this trigger | |
69 | * @trig_info: the trigger | |
70 | **/ | |
71 | static int iio_trigger_register_id(struct iio_trigger *trig_info) | |
72 | { | |
73 | int ret = 0; | |
74 | ||
75 | idr_again: | |
76 | if (unlikely(idr_pre_get(&iio_trigger_idr, GFP_KERNEL) == 0)) | |
77 | return -ENOMEM; | |
78 | ||
79 | spin_lock(&iio_trigger_idr_lock); | |
80 | ret = idr_get_new(&iio_trigger_idr, NULL, &trig_info->id); | |
81 | spin_unlock(&iio_trigger_idr_lock); | |
82 | if (unlikely(ret == -EAGAIN)) | |
83 | goto idr_again; | |
84 | else if (likely(!ret)) | |
85 | trig_info->id = trig_info->id & MAX_ID_MASK; | |
86 | ||
87 | return ret; | |
88 | } | |
89 | ||
90 | /** | |
91 | * iio_trigger_unregister_id() - free up unique id for use by another trigger | |
92 | * @trig_info: the trigger | |
93 | **/ | |
94 | static void iio_trigger_unregister_id(struct iio_trigger *trig_info) | |
95 | { | |
74112d3f GKH |
96 | spin_lock(&iio_trigger_idr_lock); |
97 | idr_remove(&iio_trigger_idr, trig_info->id); | |
98 | spin_unlock(&iio_trigger_idr_lock); | |
1637db44 JC |
99 | } |
100 | ||
101 | int iio_trigger_register(struct iio_trigger *trig_info) | |
102 | { | |
103 | int ret; | |
104 | ||
105 | ret = iio_trigger_register_id(trig_info); | |
106 | if (ret) | |
107 | goto error_ret; | |
108 | /* Set the name used for the sysfs directory etc */ | |
109 | dev_set_name(&trig_info->dev, "trigger%ld", | |
110 | (unsigned long) trig_info->id); | |
111 | ||
112 | ret = device_add(&trig_info->dev); | |
113 | if (ret) | |
114 | goto error_unregister_id; | |
115 | ||
116 | ret = iio_trigger_register_sysfs(trig_info); | |
117 | if (ret) | |
118 | goto error_device_del; | |
119 | ||
120 | /* Add to list of available triggers held by the IIO core */ | |
121 | mutex_lock(&iio_trigger_list_lock); | |
122 | list_add_tail(&trig_info->list, &iio_trigger_list); | |
123 | mutex_unlock(&iio_trigger_list_lock); | |
124 | ||
125 | return 0; | |
126 | ||
127 | error_device_del: | |
128 | device_del(&trig_info->dev); | |
129 | error_unregister_id: | |
130 | iio_trigger_unregister_id(trig_info); | |
131 | error_ret: | |
132 | return ret; | |
133 | } | |
134 | EXPORT_SYMBOL(iio_trigger_register); | |
135 | ||
136 | void iio_trigger_unregister(struct iio_trigger *trig_info) | |
137 | { | |
138 | struct iio_trigger *cursor; | |
139 | ||
140 | mutex_lock(&iio_trigger_list_lock); | |
141 | list_for_each_entry(cursor, &iio_trigger_list, list) | |
142 | if (cursor == trig_info) { | |
143 | list_del(&cursor->list); | |
144 | break; | |
145 | } | |
146 | mutex_unlock(&iio_trigger_list_lock); | |
147 | ||
148 | iio_trigger_unregister_sysfs(trig_info); | |
149 | iio_trigger_unregister_id(trig_info); | |
150 | /* Possible issue in here */ | |
151 | device_unregister(&trig_info->dev); | |
152 | } | |
153 | EXPORT_SYMBOL(iio_trigger_unregister); | |
154 | ||
155 | struct iio_trigger *iio_trigger_find_by_name(const char *name, size_t len) | |
156 | { | |
157 | struct iio_trigger *trig; | |
158 | bool found = false; | |
159 | ||
7c327857 MH |
160 | if (len && name[len - 1] == '\n') |
161 | len--; | |
162 | ||
1637db44 JC |
163 | mutex_lock(&iio_trigger_list_lock); |
164 | list_for_each_entry(trig, &iio_trigger_list, list) { | |
165 | if (strncmp(trig->name, name, len) == 0) { | |
166 | found = true; | |
167 | break; | |
168 | } | |
169 | } | |
170 | mutex_unlock(&iio_trigger_list_lock); | |
171 | ||
172 | return found ? trig : NULL; | |
5f87404d | 173 | } |
1637db44 JC |
174 | EXPORT_SYMBOL(iio_trigger_find_by_name); |
175 | ||
176 | void iio_trigger_poll(struct iio_trigger *trig) | |
177 | { | |
178 | struct iio_poll_func *pf_cursor; | |
179 | ||
180 | list_for_each_entry(pf_cursor, &trig->pollfunc_list, list) { | |
181 | if (pf_cursor->poll_func_immediate) { | |
182 | pf_cursor->poll_func_immediate(pf_cursor->private_data); | |
183 | trig->use_count++; | |
184 | } | |
185 | } | |
186 | list_for_each_entry(pf_cursor, &trig->pollfunc_list, list) { | |
187 | if (pf_cursor->poll_func_main) { | |
188 | pf_cursor->poll_func_main(pf_cursor->private_data); | |
189 | trig->use_count++; | |
190 | } | |
191 | } | |
192 | } | |
193 | EXPORT_SYMBOL(iio_trigger_poll); | |
194 | ||
195 | void iio_trigger_notify_done(struct iio_trigger *trig) | |
196 | { | |
197 | trig->use_count--; | |
198 | if (trig->use_count == 0 && trig->try_reenable) | |
199 | if (trig->try_reenable(trig)) { | |
200 | /* Missed and interrupt so launch new poll now */ | |
201 | trig->timestamp = 0; | |
202 | iio_trigger_poll(trig); | |
203 | } | |
204 | } | |
205 | EXPORT_SYMBOL(iio_trigger_notify_done); | |
206 | ||
207 | /** | |
208 | * iio_trigger_read_name() - retrieve useful identifying name | |
209 | **/ | |
210 | ssize_t iio_trigger_read_name(struct device *dev, | |
211 | struct device_attribute *attr, | |
212 | char *buf) | |
213 | { | |
214 | struct iio_trigger *trig = dev_get_drvdata(dev); | |
215 | return sprintf(buf, "%s\n", trig->name); | |
216 | } | |
217 | EXPORT_SYMBOL(iio_trigger_read_name); | |
218 | ||
219 | /* Trigger Consumer related functions */ | |
220 | ||
221 | /* Complexity in here. With certain triggers (datardy) an acknowledgement | |
222 | * may be needed if the pollfuncs do not include the data read for the | |
223 | * triggering device. | |
224 | * This is not currently handled. Alternative of not enabling trigger unless | |
225 | * the relevant function is in there may be the best option. | |
226 | */ | |
227 | /* Worth protecting against double additions?*/ | |
228 | int iio_trigger_attach_poll_func(struct iio_trigger *trig, | |
229 | struct iio_poll_func *pf) | |
230 | { | |
231 | int ret = 0; | |
232 | unsigned long flags; | |
233 | ||
234 | spin_lock_irqsave(&trig->pollfunc_list_lock, flags); | |
235 | list_add_tail(&pf->list, &trig->pollfunc_list); | |
236 | spin_unlock_irqrestore(&trig->pollfunc_list_lock, flags); | |
237 | ||
238 | if (trig->set_trigger_state) | |
239 | ret = trig->set_trigger_state(trig, true); | |
240 | if (ret) { | |
241 | printk(KERN_ERR "set trigger state failed\n"); | |
242 | list_del(&pf->list); | |
243 | } | |
244 | return ret; | |
245 | } | |
246 | EXPORT_SYMBOL(iio_trigger_attach_poll_func); | |
247 | ||
248 | int iio_trigger_dettach_poll_func(struct iio_trigger *trig, | |
249 | struct iio_poll_func *pf) | |
250 | { | |
251 | struct iio_poll_func *pf_cursor; | |
252 | unsigned long flags; | |
253 | int ret = -EINVAL; | |
254 | ||
255 | spin_lock_irqsave(&trig->pollfunc_list_lock, flags); | |
256 | list_for_each_entry(pf_cursor, &trig->pollfunc_list, list) | |
257 | if (pf_cursor == pf) { | |
258 | ret = 0; | |
259 | break; | |
260 | } | |
261 | if (!ret) { | |
262 | if (list_is_singular(&trig->pollfunc_list) | |
263 | && trig->set_trigger_state) { | |
264 | spin_unlock_irqrestore(&trig->pollfunc_list_lock, | |
265 | flags); | |
266 | /* May sleep hence cannot hold the spin lock */ | |
267 | ret = trig->set_trigger_state(trig, false); | |
268 | if (ret) | |
269 | goto error_ret; | |
270 | spin_lock_irqsave(&trig->pollfunc_list_lock, flags); | |
271 | } | |
272 | /* | |
273 | * Now we can delete safe in the knowledge that, if this is | |
274 | * the last pollfunc then we have disabled the trigger anyway | |
275 | * and so nothing should be able to call the pollfunc. | |
276 | */ | |
277 | list_del(&pf_cursor->list); | |
278 | } | |
279 | spin_unlock_irqrestore(&trig->pollfunc_list_lock, flags); | |
280 | ||
281 | error_ret: | |
282 | return ret; | |
283 | } | |
284 | EXPORT_SYMBOL(iio_trigger_dettach_poll_func); | |
285 | ||
286 | /** | |
287 | * iio_trigger_read_currrent() trigger consumer sysfs query which trigger | |
288 | * | |
289 | * For trigger consumers the current_trigger interface allows the trigger | |
290 | * used by the device to be queried. | |
291 | **/ | |
292 | static ssize_t iio_trigger_read_current(struct device *dev, | |
293 | struct device_attribute *attr, | |
294 | char *buf) | |
295 | { | |
296 | struct iio_dev *dev_info = dev_get_drvdata(dev); | |
297 | int len = 0; | |
298 | if (dev_info->trig) | |
299 | len = snprintf(buf, | |
300 | IIO_TRIGGER_NAME_LENGTH, | |
301 | "%s\n", | |
302 | dev_info->trig->name); | |
303 | return len; | |
304 | } | |
305 | ||
306 | /** | |
307 | * iio_trigger_write_current() trigger consumer sysfs set current trigger | |
308 | * | |
309 | * For trigger consumers the current_trigger interface allows the trigger | |
310 | * used for this device to be specified at run time based on the triggers | |
311 | * name. | |
312 | **/ | |
313 | static ssize_t iio_trigger_write_current(struct device *dev, | |
314 | struct device_attribute *attr, | |
315 | const char *buf, | |
316 | size_t len) | |
317 | { | |
318 | struct iio_dev *dev_info = dev_get_drvdata(dev); | |
319 | struct iio_trigger *oldtrig = dev_info->trig; | |
320 | mutex_lock(&dev_info->mlock); | |
321 | if (dev_info->currentmode == INDIO_RING_TRIGGERED) { | |
322 | mutex_unlock(&dev_info->mlock); | |
323 | return -EBUSY; | |
324 | } | |
325 | mutex_unlock(&dev_info->mlock); | |
326 | ||
327 | len = len < IIO_TRIGGER_NAME_LENGTH ? len : IIO_TRIGGER_NAME_LENGTH; | |
328 | ||
329 | dev_info->trig = iio_trigger_find_by_name(buf, len); | |
330 | if (oldtrig && dev_info->trig != oldtrig) | |
331 | iio_put_trigger(oldtrig); | |
332 | if (dev_info->trig) | |
333 | iio_get_trigger(dev_info->trig); | |
334 | ||
335 | return len; | |
336 | } | |
337 | ||
74112d3f GKH |
338 | static DEVICE_ATTR(current_trigger, S_IRUGO | S_IWUSR, |
339 | iio_trigger_read_current, | |
340 | iio_trigger_write_current); | |
1637db44 JC |
341 | |
342 | static struct attribute *iio_trigger_consumer_attrs[] = { | |
343 | &dev_attr_current_trigger.attr, | |
344 | NULL, | |
345 | }; | |
346 | ||
347 | static const struct attribute_group iio_trigger_consumer_attr_group = { | |
348 | .name = "trigger", | |
349 | .attrs = iio_trigger_consumer_attrs, | |
350 | }; | |
351 | ||
352 | static void iio_trig_release(struct device *device) | |
353 | { | |
354 | struct iio_trigger *trig = to_iio_trigger(device); | |
355 | kfree(trig); | |
356 | iio_put(); | |
357 | } | |
358 | ||
359 | static struct device_type iio_trig_type = { | |
360 | .release = iio_trig_release, | |
361 | }; | |
362 | ||
363 | struct iio_trigger *iio_allocate_trigger(void) | |
364 | { | |
365 | struct iio_trigger *trig; | |
366 | trig = kzalloc(sizeof *trig, GFP_KERNEL); | |
367 | if (trig) { | |
368 | trig->dev.type = &iio_trig_type; | |
5aaaeba8 | 369 | trig->dev.bus = &iio_bus_type; |
1637db44 JC |
370 | device_initialize(&trig->dev); |
371 | dev_set_drvdata(&trig->dev, (void *)trig); | |
372 | spin_lock_init(&trig->pollfunc_list_lock); | |
373 | INIT_LIST_HEAD(&trig->list); | |
374 | INIT_LIST_HEAD(&trig->pollfunc_list); | |
375 | iio_get(); | |
376 | } | |
377 | return trig; | |
378 | } | |
379 | EXPORT_SYMBOL(iio_allocate_trigger); | |
380 | ||
381 | void iio_free_trigger(struct iio_trigger *trig) | |
382 | { | |
383 | if (trig) | |
384 | put_device(&trig->dev); | |
385 | } | |
386 | EXPORT_SYMBOL(iio_free_trigger); | |
387 | ||
388 | int iio_device_register_trigger_consumer(struct iio_dev *dev_info) | |
389 | { | |
390 | int ret; | |
391 | ret = sysfs_create_group(&dev_info->dev.kobj, | |
392 | &iio_trigger_consumer_attr_group); | |
393 | return ret; | |
394 | } | |
395 | EXPORT_SYMBOL(iio_device_register_trigger_consumer); | |
396 | ||
397 | int iio_device_unregister_trigger_consumer(struct iio_dev *dev_info) | |
398 | { | |
399 | sysfs_remove_group(&dev_info->dev.kobj, | |
400 | &iio_trigger_consumer_attr_group); | |
401 | return 0; | |
402 | } | |
403 | EXPORT_SYMBOL(iio_device_unregister_trigger_consumer); | |
404 |