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