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 | { | |
1637db44 | 137 | mutex_lock(&iio_trigger_list_lock); |
582e5489 | 138 | list_del(&trig_info->list); |
1637db44 JC |
139 | mutex_unlock(&iio_trigger_list_lock); |
140 | ||
141 | iio_trigger_unregister_sysfs(trig_info); | |
142 | iio_trigger_unregister_id(trig_info); | |
143 | /* Possible issue in here */ | |
144 | device_unregister(&trig_info->dev); | |
145 | } | |
146 | EXPORT_SYMBOL(iio_trigger_unregister); | |
147 | ||
f6517f22 JC |
148 | static struct iio_trigger *iio_trigger_find_by_name(const char *name, |
149 | size_t len) | |
1637db44 | 150 | { |
f6517f22 | 151 | struct iio_trigger *trig = NULL, *iter; |
7c327857 | 152 | |
1637db44 | 153 | mutex_lock(&iio_trigger_list_lock); |
f6517f22 JC |
154 | list_for_each_entry(iter, &iio_trigger_list, list) |
155 | if (sysfs_streq(iter->name, name)) { | |
156 | trig = iter; | |
1637db44 JC |
157 | break; |
158 | } | |
1637db44 JC |
159 | mutex_unlock(&iio_trigger_list_lock); |
160 | ||
f6517f22 | 161 | return trig; |
5f87404d | 162 | } |
1637db44 | 163 | |
7b2c33b1 | 164 | void iio_trigger_poll(struct iio_trigger *trig, s64 time) |
1637db44 JC |
165 | { |
166 | struct iio_poll_func *pf_cursor; | |
167 | ||
168 | list_for_each_entry(pf_cursor, &trig->pollfunc_list, list) { | |
169 | if (pf_cursor->poll_func_immediate) { | |
170 | pf_cursor->poll_func_immediate(pf_cursor->private_data); | |
171 | trig->use_count++; | |
172 | } | |
173 | } | |
174 | list_for_each_entry(pf_cursor, &trig->pollfunc_list, list) { | |
175 | if (pf_cursor->poll_func_main) { | |
7b2c33b1 JC |
176 | pf_cursor->poll_func_main(pf_cursor->private_data, |
177 | time); | |
1637db44 JC |
178 | trig->use_count++; |
179 | } | |
180 | } | |
181 | } | |
182 | EXPORT_SYMBOL(iio_trigger_poll); | |
183 | ||
184 | void iio_trigger_notify_done(struct iio_trigger *trig) | |
185 | { | |
186 | trig->use_count--; | |
187 | if (trig->use_count == 0 && trig->try_reenable) | |
188 | if (trig->try_reenable(trig)) { | |
189 | /* Missed and interrupt so launch new poll now */ | |
7b2c33b1 | 190 | iio_trigger_poll(trig, 0); |
1637db44 JC |
191 | } |
192 | } | |
193 | EXPORT_SYMBOL(iio_trigger_notify_done); | |
194 | ||
195 | /** | |
196 | * iio_trigger_read_name() - retrieve useful identifying name | |
197 | **/ | |
198 | ssize_t iio_trigger_read_name(struct device *dev, | |
199 | struct device_attribute *attr, | |
200 | char *buf) | |
201 | { | |
202 | struct iio_trigger *trig = dev_get_drvdata(dev); | |
203 | return sprintf(buf, "%s\n", trig->name); | |
204 | } | |
205 | EXPORT_SYMBOL(iio_trigger_read_name); | |
206 | ||
207 | /* Trigger Consumer related functions */ | |
208 | ||
209 | /* Complexity in here. With certain triggers (datardy) an acknowledgement | |
210 | * may be needed if the pollfuncs do not include the data read for the | |
211 | * triggering device. | |
212 | * This is not currently handled. Alternative of not enabling trigger unless | |
213 | * the relevant function is in there may be the best option. | |
214 | */ | |
215 | /* Worth protecting against double additions?*/ | |
216 | int iio_trigger_attach_poll_func(struct iio_trigger *trig, | |
217 | struct iio_poll_func *pf) | |
218 | { | |
219 | int ret = 0; | |
220 | unsigned long flags; | |
221 | ||
222 | spin_lock_irqsave(&trig->pollfunc_list_lock, flags); | |
223 | list_add_tail(&pf->list, &trig->pollfunc_list); | |
224 | spin_unlock_irqrestore(&trig->pollfunc_list_lock, flags); | |
225 | ||
226 | if (trig->set_trigger_state) | |
227 | ret = trig->set_trigger_state(trig, true); | |
228 | if (ret) { | |
229 | printk(KERN_ERR "set trigger state failed\n"); | |
230 | list_del(&pf->list); | |
231 | } | |
232 | return ret; | |
233 | } | |
234 | EXPORT_SYMBOL(iio_trigger_attach_poll_func); | |
235 | ||
236 | int iio_trigger_dettach_poll_func(struct iio_trigger *trig, | |
237 | struct iio_poll_func *pf) | |
238 | { | |
239 | struct iio_poll_func *pf_cursor; | |
240 | unsigned long flags; | |
241 | int ret = -EINVAL; | |
242 | ||
243 | spin_lock_irqsave(&trig->pollfunc_list_lock, flags); | |
244 | list_for_each_entry(pf_cursor, &trig->pollfunc_list, list) | |
245 | if (pf_cursor == pf) { | |
246 | ret = 0; | |
247 | break; | |
248 | } | |
249 | if (!ret) { | |
250 | if (list_is_singular(&trig->pollfunc_list) | |
251 | && trig->set_trigger_state) { | |
252 | spin_unlock_irqrestore(&trig->pollfunc_list_lock, | |
253 | flags); | |
254 | /* May sleep hence cannot hold the spin lock */ | |
255 | ret = trig->set_trigger_state(trig, false); | |
256 | if (ret) | |
257 | goto error_ret; | |
258 | spin_lock_irqsave(&trig->pollfunc_list_lock, flags); | |
259 | } | |
260 | /* | |
261 | * Now we can delete safe in the knowledge that, if this is | |
262 | * the last pollfunc then we have disabled the trigger anyway | |
263 | * and so nothing should be able to call the pollfunc. | |
264 | */ | |
265 | list_del(&pf_cursor->list); | |
266 | } | |
267 | spin_unlock_irqrestore(&trig->pollfunc_list_lock, flags); | |
268 | ||
269 | error_ret: | |
270 | return ret; | |
271 | } | |
272 | EXPORT_SYMBOL(iio_trigger_dettach_poll_func); | |
273 | ||
274 | /** | |
751a3700 | 275 | * iio_trigger_read_currrent() - trigger consumer sysfs query which trigger |
1637db44 JC |
276 | * |
277 | * For trigger consumers the current_trigger interface allows the trigger | |
278 | * used by the device to be queried. | |
279 | **/ | |
280 | static ssize_t iio_trigger_read_current(struct device *dev, | |
281 | struct device_attribute *attr, | |
282 | char *buf) | |
283 | { | |
284 | struct iio_dev *dev_info = dev_get_drvdata(dev); | |
285 | int len = 0; | |
286 | if (dev_info->trig) | |
3c9bbf58 JC |
287 | len = sprintf(buf, |
288 | "%s\n", | |
289 | dev_info->trig->name); | |
1637db44 JC |
290 | return len; |
291 | } | |
292 | ||
293 | /** | |
294 | * iio_trigger_write_current() trigger consumer sysfs set current trigger | |
295 | * | |
296 | * For trigger consumers the current_trigger interface allows the trigger | |
297 | * used for this device to be specified at run time based on the triggers | |
298 | * name. | |
299 | **/ | |
300 | static ssize_t iio_trigger_write_current(struct device *dev, | |
301 | struct device_attribute *attr, | |
302 | const char *buf, | |
303 | size_t len) | |
304 | { | |
305 | struct iio_dev *dev_info = dev_get_drvdata(dev); | |
306 | struct iio_trigger *oldtrig = dev_info->trig; | |
307 | mutex_lock(&dev_info->mlock); | |
308 | if (dev_info->currentmode == INDIO_RING_TRIGGERED) { | |
309 | mutex_unlock(&dev_info->mlock); | |
310 | return -EBUSY; | |
311 | } | |
312 | mutex_unlock(&dev_info->mlock); | |
313 | ||
1637db44 JC |
314 | dev_info->trig = iio_trigger_find_by_name(buf, len); |
315 | if (oldtrig && dev_info->trig != oldtrig) | |
316 | iio_put_trigger(oldtrig); | |
317 | if (dev_info->trig) | |
318 | iio_get_trigger(dev_info->trig); | |
319 | ||
320 | return len; | |
321 | } | |
322 | ||
74112d3f GKH |
323 | static DEVICE_ATTR(current_trigger, S_IRUGO | S_IWUSR, |
324 | iio_trigger_read_current, | |
325 | iio_trigger_write_current); | |
1637db44 JC |
326 | |
327 | static struct attribute *iio_trigger_consumer_attrs[] = { | |
328 | &dev_attr_current_trigger.attr, | |
329 | NULL, | |
330 | }; | |
331 | ||
332 | static const struct attribute_group iio_trigger_consumer_attr_group = { | |
333 | .name = "trigger", | |
334 | .attrs = iio_trigger_consumer_attrs, | |
335 | }; | |
336 | ||
337 | static void iio_trig_release(struct device *device) | |
338 | { | |
339 | struct iio_trigger *trig = to_iio_trigger(device); | |
340 | kfree(trig); | |
341 | iio_put(); | |
342 | } | |
343 | ||
344 | static struct device_type iio_trig_type = { | |
345 | .release = iio_trig_release, | |
346 | }; | |
347 | ||
348 | struct iio_trigger *iio_allocate_trigger(void) | |
349 | { | |
350 | struct iio_trigger *trig; | |
351 | trig = kzalloc(sizeof *trig, GFP_KERNEL); | |
352 | if (trig) { | |
353 | trig->dev.type = &iio_trig_type; | |
5aaaeba8 | 354 | trig->dev.bus = &iio_bus_type; |
1637db44 JC |
355 | device_initialize(&trig->dev); |
356 | dev_set_drvdata(&trig->dev, (void *)trig); | |
357 | spin_lock_init(&trig->pollfunc_list_lock); | |
358 | INIT_LIST_HEAD(&trig->list); | |
359 | INIT_LIST_HEAD(&trig->pollfunc_list); | |
360 | iio_get(); | |
361 | } | |
362 | return trig; | |
363 | } | |
364 | EXPORT_SYMBOL(iio_allocate_trigger); | |
365 | ||
366 | void iio_free_trigger(struct iio_trigger *trig) | |
367 | { | |
368 | if (trig) | |
369 | put_device(&trig->dev); | |
370 | } | |
371 | EXPORT_SYMBOL(iio_free_trigger); | |
372 | ||
373 | int iio_device_register_trigger_consumer(struct iio_dev *dev_info) | |
374 | { | |
375 | int ret; | |
376 | ret = sysfs_create_group(&dev_info->dev.kobj, | |
377 | &iio_trigger_consumer_attr_group); | |
378 | return ret; | |
379 | } | |
380 | EXPORT_SYMBOL(iio_device_register_trigger_consumer); | |
381 | ||
382 | int iio_device_unregister_trigger_consumer(struct iio_dev *dev_info) | |
383 | { | |
384 | sysfs_remove_group(&dev_info->dev.kobj, | |
385 | &iio_trigger_consumer_attr_group); | |
386 | return 0; | |
387 | } | |
388 | EXPORT_SYMBOL(iio_device_unregister_trigger_consumer); | |
389 | ||
15744090 JC |
390 | int iio_alloc_pollfunc(struct iio_dev *indio_dev, |
391 | void (*immediate)(struct iio_dev *indio_dev), | |
7b2c33b1 | 392 | void (*main)(struct iio_dev *private_data, s64 time)) |
15744090 JC |
393 | { |
394 | indio_dev->pollfunc = kzalloc(sizeof(*indio_dev->pollfunc), GFP_KERNEL); | |
395 | if (indio_dev->pollfunc == NULL) | |
396 | return -ENOMEM; | |
397 | indio_dev->pollfunc->poll_func_immediate = immediate; | |
398 | indio_dev->pollfunc->poll_func_main = main; | |
399 | indio_dev->pollfunc->private_data = indio_dev; | |
400 | return 0; | |
401 | } | |
402 | EXPORT_SYMBOL(iio_alloc_pollfunc); | |
c3db00cc JC |
403 | |
404 | int iio_triggered_ring_postenable(struct iio_dev *indio_dev) | |
405 | { | |
406 | return indio_dev->trig | |
407 | ? iio_trigger_attach_poll_func(indio_dev->trig, | |
408 | indio_dev->pollfunc) | |
409 | : 0; | |
410 | } | |
411 | EXPORT_SYMBOL(iio_triggered_ring_postenable); | |
412 | ||
413 | int iio_triggered_ring_predisable(struct iio_dev *indio_dev) | |
414 | { | |
415 | return indio_dev->trig | |
416 | ? iio_trigger_dettach_poll_func(indio_dev->trig, | |
417 | indio_dev->pollfunc) | |
418 | : 0; | |
419 | } | |
420 | EXPORT_SYMBOL(iio_triggered_ring_predisable); |