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 | 165 | { |
d96d1337 | 166 | int i; |
1637db44 JC |
167 | struct iio_poll_func *pf_cursor; |
168 | ||
169 | list_for_each_entry(pf_cursor, &trig->pollfunc_list, list) { | |
170 | if (pf_cursor->poll_func_immediate) { | |
171 | pf_cursor->poll_func_immediate(pf_cursor->private_data); | |
172 | trig->use_count++; | |
173 | } | |
174 | } | |
175 | list_for_each_entry(pf_cursor, &trig->pollfunc_list, list) { | |
176 | if (pf_cursor->poll_func_main) { | |
7b2c33b1 JC |
177 | pf_cursor->poll_func_main(pf_cursor->private_data, |
178 | time); | |
1637db44 JC |
179 | trig->use_count++; |
180 | } | |
181 | } | |
d96d1337 JC |
182 | if (!trig->use_count) { |
183 | for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) | |
184 | if (trig->subirqs[i].enabled) { | |
185 | trig->use_count++; | |
186 | generic_handle_irq(trig->subirq_base + i); | |
187 | } | |
188 | } | |
1637db44 JC |
189 | } |
190 | EXPORT_SYMBOL(iio_trigger_poll); | |
191 | ||
192 | void iio_trigger_notify_done(struct iio_trigger *trig) | |
193 | { | |
194 | trig->use_count--; | |
195 | if (trig->use_count == 0 && trig->try_reenable) | |
196 | if (trig->try_reenable(trig)) { | |
197 | /* Missed and interrupt so launch new poll now */ | |
7b2c33b1 | 198 | iio_trigger_poll(trig, 0); |
1637db44 JC |
199 | } |
200 | } | |
201 | EXPORT_SYMBOL(iio_trigger_notify_done); | |
202 | ||
203 | /** | |
204 | * iio_trigger_read_name() - retrieve useful identifying name | |
205 | **/ | |
206 | ssize_t iio_trigger_read_name(struct device *dev, | |
207 | struct device_attribute *attr, | |
208 | char *buf) | |
209 | { | |
210 | struct iio_trigger *trig = dev_get_drvdata(dev); | |
211 | return sprintf(buf, "%s\n", trig->name); | |
212 | } | |
213 | EXPORT_SYMBOL(iio_trigger_read_name); | |
214 | ||
215 | /* Trigger Consumer related functions */ | |
216 | ||
217 | /* Complexity in here. With certain triggers (datardy) an acknowledgement | |
218 | * may be needed if the pollfuncs do not include the data read for the | |
219 | * triggering device. | |
220 | * This is not currently handled. Alternative of not enabling trigger unless | |
221 | * the relevant function is in there may be the best option. | |
222 | */ | |
223 | /* Worth protecting against double additions?*/ | |
224 | int iio_trigger_attach_poll_func(struct iio_trigger *trig, | |
225 | struct iio_poll_func *pf) | |
226 | { | |
227 | int ret = 0; | |
228 | unsigned long flags; | |
229 | ||
d96d1337 JC |
230 | if (pf->thread) { |
231 | bool notinuse | |
232 | = bitmap_empty(trig->pool, | |
233 | CONFIG_IIO_CONSUMERS_PER_TRIGGER); | |
234 | ||
235 | pf->irq = iio_trigger_get_irq(trig); | |
236 | ret = request_threaded_irq(pf->irq, pf->h, pf->thread, | |
237 | pf->type, pf->name, | |
238 | pf); | |
239 | if (trig->set_trigger_state && notinuse) { | |
240 | ret = trig->set_trigger_state(trig, true); | |
241 | } else { | |
242 | spin_lock_irqsave(&trig->pollfunc_list_lock, flags); | |
243 | list_add_tail(&pf->list, &trig->pollfunc_list); | |
244 | spin_unlock_irqrestore(&trig->pollfunc_list_lock, flags); | |
245 | ||
246 | if (trig->set_trigger_state) | |
247 | ret = trig->set_trigger_state(trig, true); | |
248 | } | |
249 | if (ret) { | |
250 | printk(KERN_ERR "set trigger state failed\n"); | |
251 | list_del(&pf->list); | |
252 | } | |
1637db44 | 253 | } |
d96d1337 | 254 | |
1637db44 JC |
255 | return ret; |
256 | } | |
257 | EXPORT_SYMBOL(iio_trigger_attach_poll_func); | |
258 | ||
259 | int iio_trigger_dettach_poll_func(struct iio_trigger *trig, | |
260 | struct iio_poll_func *pf) | |
261 | { | |
262 | struct iio_poll_func *pf_cursor; | |
263 | unsigned long flags; | |
264 | int ret = -EINVAL; | |
265 | ||
d96d1337 JC |
266 | if (pf->thread) { |
267 | bool no_other_users | |
268 | = (bitmap_weight(trig->pool, | |
269 | CONFIG_IIO_CONSUMERS_PER_TRIGGER) | |
270 | == 1); | |
271 | if (trig->set_trigger_state && no_other_users) { | |
1637db44 JC |
272 | ret = trig->set_trigger_state(trig, false); |
273 | if (ret) | |
274 | goto error_ret; | |
d96d1337 JC |
275 | } else |
276 | ret = 0; | |
277 | iio_trigger_put_irq(trig, pf->irq); | |
278 | free_irq(pf->irq, pf); | |
279 | } else { | |
280 | spin_lock_irqsave(&trig->pollfunc_list_lock, flags); | |
281 | list_for_each_entry(pf_cursor, &trig->pollfunc_list, list) | |
282 | if (pf_cursor == pf) { | |
283 | ret = 0; | |
284 | break; | |
285 | } | |
286 | if (!ret) { | |
287 | if (list_is_singular(&trig->pollfunc_list) | |
288 | && trig->set_trigger_state) { | |
289 | spin_unlock_irqrestore(&trig | |
290 | ->pollfunc_list_lock, | |
291 | flags); | |
292 | /* May sleep hence cannot hold the spin lock */ | |
293 | ret = trig->set_trigger_state(trig, false); | |
294 | if (ret) | |
295 | goto error_ret; | |
296 | spin_lock_irqsave(&trig->pollfunc_list_lock, | |
297 | flags); | |
298 | } | |
299 | /* | |
300 | * Now we can delete safe in the knowledge that, if | |
301 | * this is the last pollfunc then we have disabled | |
302 | * the trigger anyway and so nothing should be able | |
303 | * to call the pollfunc. | |
304 | */ | |
305 | list_del(&pf_cursor->list); | |
1637db44 | 306 | } |
d96d1337 | 307 | spin_unlock_irqrestore(&trig->pollfunc_list_lock, flags); |
1637db44 | 308 | } |
1637db44 JC |
309 | |
310 | error_ret: | |
311 | return ret; | |
312 | } | |
313 | EXPORT_SYMBOL(iio_trigger_dettach_poll_func); | |
314 | ||
d96d1337 JC |
315 | irqreturn_t iio_pollfunc_store_time(int irq, void *p) |
316 | { | |
317 | struct iio_poll_func *pf = p; | |
318 | pf->timestamp = iio_get_time_ns(); | |
319 | return IRQ_WAKE_THREAD; | |
320 | } | |
321 | EXPORT_SYMBOL(iio_pollfunc_store_time); | |
322 | ||
1637db44 | 323 | /** |
751a3700 | 324 | * iio_trigger_read_currrent() - trigger consumer sysfs query which trigger |
1637db44 JC |
325 | * |
326 | * For trigger consumers the current_trigger interface allows the trigger | |
327 | * used by the device to be queried. | |
328 | **/ | |
329 | static ssize_t iio_trigger_read_current(struct device *dev, | |
330 | struct device_attribute *attr, | |
331 | char *buf) | |
332 | { | |
333 | struct iio_dev *dev_info = dev_get_drvdata(dev); | |
334 | int len = 0; | |
335 | if (dev_info->trig) | |
3c9bbf58 JC |
336 | len = sprintf(buf, |
337 | "%s\n", | |
338 | dev_info->trig->name); | |
1637db44 JC |
339 | return len; |
340 | } | |
341 | ||
342 | /** | |
343 | * iio_trigger_write_current() trigger consumer sysfs set current trigger | |
344 | * | |
345 | * For trigger consumers the current_trigger interface allows the trigger | |
346 | * used for this device to be specified at run time based on the triggers | |
347 | * name. | |
348 | **/ | |
349 | static ssize_t iio_trigger_write_current(struct device *dev, | |
350 | struct device_attribute *attr, | |
351 | const char *buf, | |
352 | size_t len) | |
353 | { | |
354 | struct iio_dev *dev_info = dev_get_drvdata(dev); | |
355 | struct iio_trigger *oldtrig = dev_info->trig; | |
356 | mutex_lock(&dev_info->mlock); | |
357 | if (dev_info->currentmode == INDIO_RING_TRIGGERED) { | |
358 | mutex_unlock(&dev_info->mlock); | |
359 | return -EBUSY; | |
360 | } | |
361 | mutex_unlock(&dev_info->mlock); | |
362 | ||
1637db44 JC |
363 | dev_info->trig = iio_trigger_find_by_name(buf, len); |
364 | if (oldtrig && dev_info->trig != oldtrig) | |
365 | iio_put_trigger(oldtrig); | |
366 | if (dev_info->trig) | |
367 | iio_get_trigger(dev_info->trig); | |
368 | ||
369 | return len; | |
370 | } | |
371 | ||
74112d3f GKH |
372 | static DEVICE_ATTR(current_trigger, S_IRUGO | S_IWUSR, |
373 | iio_trigger_read_current, | |
374 | iio_trigger_write_current); | |
1637db44 JC |
375 | |
376 | static struct attribute *iio_trigger_consumer_attrs[] = { | |
377 | &dev_attr_current_trigger.attr, | |
378 | NULL, | |
379 | }; | |
380 | ||
381 | static const struct attribute_group iio_trigger_consumer_attr_group = { | |
382 | .name = "trigger", | |
383 | .attrs = iio_trigger_consumer_attrs, | |
384 | }; | |
385 | ||
386 | static void iio_trig_release(struct device *device) | |
387 | { | |
388 | struct iio_trigger *trig = to_iio_trigger(device); | |
d96d1337 JC |
389 | int i; |
390 | ||
391 | if (trig->subirq_base) { | |
392 | for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) { | |
393 | irq_modify_status(trig->subirq_base + i, | |
394 | IRQ_NOAUTOEN, | |
395 | IRQ_NOREQUEST | IRQ_NOPROBE); | |
396 | irq_set_chip(trig->subirq_base + i, | |
397 | NULL); | |
398 | irq_set_handler(trig->subirq_base + i, | |
399 | NULL); | |
400 | } | |
401 | ||
402 | irq_free_descs(trig->subirq_base, | |
403 | CONFIG_IIO_CONSUMERS_PER_TRIGGER); | |
404 | } | |
1637db44 JC |
405 | kfree(trig); |
406 | iio_put(); | |
407 | } | |
408 | ||
409 | static struct device_type iio_trig_type = { | |
410 | .release = iio_trig_release, | |
411 | }; | |
412 | ||
d96d1337 JC |
413 | static void iio_trig_subirqmask(struct irq_data *d) |
414 | { | |
415 | struct irq_chip *chip = irq_data_get_irq_chip(d); | |
416 | struct iio_trigger *trig | |
417 | = container_of(chip, | |
418 | struct iio_trigger, subirq_chip); | |
419 | trig->subirqs[d->irq - trig->subirq_base].enabled = false; | |
420 | } | |
421 | ||
422 | static void iio_trig_subirqunmask(struct irq_data *d) | |
423 | { | |
424 | struct irq_chip *chip = irq_data_get_irq_chip(d); | |
425 | struct iio_trigger *trig | |
426 | = container_of(chip, | |
427 | struct iio_trigger, subirq_chip); | |
428 | trig->subirqs[d->irq - trig->subirq_base].enabled = true; | |
429 | } | |
430 | ||
431 | struct iio_trigger *iio_allocate_trigger_named(const char *name) | |
1637db44 JC |
432 | { |
433 | struct iio_trigger *trig; | |
434 | trig = kzalloc(sizeof *trig, GFP_KERNEL); | |
435 | if (trig) { | |
d96d1337 | 436 | int i; |
1637db44 | 437 | trig->dev.type = &iio_trig_type; |
5aaaeba8 | 438 | trig->dev.bus = &iio_bus_type; |
1637db44 JC |
439 | device_initialize(&trig->dev); |
440 | dev_set_drvdata(&trig->dev, (void *)trig); | |
441 | spin_lock_init(&trig->pollfunc_list_lock); | |
442 | INIT_LIST_HEAD(&trig->list); | |
443 | INIT_LIST_HEAD(&trig->pollfunc_list); | |
d96d1337 JC |
444 | |
445 | if (name) { | |
446 | mutex_init(&trig->pool_lock); | |
447 | trig->subirq_base | |
448 | = irq_alloc_descs(-1, 0, | |
449 | CONFIG_IIO_CONSUMERS_PER_TRIGGER, | |
450 | 0); | |
451 | if (trig->subirq_base < 0) { | |
452 | kfree(trig); | |
453 | return NULL; | |
454 | } | |
455 | trig->name = name; | |
456 | trig->subirq_chip.name = name; | |
457 | trig->subirq_chip.irq_mask = &iio_trig_subirqmask; | |
458 | trig->subirq_chip.irq_unmask = &iio_trig_subirqunmask; | |
459 | for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) { | |
460 | irq_set_chip(trig->subirq_base + i, | |
461 | &trig->subirq_chip); | |
462 | irq_set_handler(trig->subirq_base + i, | |
463 | &handle_simple_irq); | |
464 | irq_modify_status(trig->subirq_base + i, | |
465 | IRQ_NOREQUEST | IRQ_NOAUTOEN, | |
466 | IRQ_NOPROBE); | |
467 | } | |
468 | } | |
1637db44 JC |
469 | iio_get(); |
470 | } | |
471 | return trig; | |
472 | } | |
d96d1337 JC |
473 | EXPORT_SYMBOL(iio_allocate_trigger_named); |
474 | ||
475 | struct iio_trigger *iio_allocate_trigger(void) | |
476 | { | |
477 | return iio_allocate_trigger_named(NULL); | |
478 | } | |
1637db44 JC |
479 | EXPORT_SYMBOL(iio_allocate_trigger); |
480 | ||
481 | void iio_free_trigger(struct iio_trigger *trig) | |
482 | { | |
483 | if (trig) | |
484 | put_device(&trig->dev); | |
485 | } | |
486 | EXPORT_SYMBOL(iio_free_trigger); | |
487 | ||
488 | int iio_device_register_trigger_consumer(struct iio_dev *dev_info) | |
489 | { | |
490 | int ret; | |
491 | ret = sysfs_create_group(&dev_info->dev.kobj, | |
492 | &iio_trigger_consumer_attr_group); | |
493 | return ret; | |
494 | } | |
495 | EXPORT_SYMBOL(iio_device_register_trigger_consumer); | |
496 | ||
497 | int iio_device_unregister_trigger_consumer(struct iio_dev *dev_info) | |
498 | { | |
499 | sysfs_remove_group(&dev_info->dev.kobj, | |
500 | &iio_trigger_consumer_attr_group); | |
501 | return 0; | |
502 | } | |
503 | EXPORT_SYMBOL(iio_device_unregister_trigger_consumer); | |
504 | ||
15744090 JC |
505 | int iio_alloc_pollfunc(struct iio_dev *indio_dev, |
506 | void (*immediate)(struct iio_dev *indio_dev), | |
7b2c33b1 | 507 | void (*main)(struct iio_dev *private_data, s64 time)) |
15744090 JC |
508 | { |
509 | indio_dev->pollfunc = kzalloc(sizeof(*indio_dev->pollfunc), GFP_KERNEL); | |
510 | if (indio_dev->pollfunc == NULL) | |
511 | return -ENOMEM; | |
512 | indio_dev->pollfunc->poll_func_immediate = immediate; | |
513 | indio_dev->pollfunc->poll_func_main = main; | |
514 | indio_dev->pollfunc->private_data = indio_dev; | |
515 | return 0; | |
516 | } | |
517 | EXPORT_SYMBOL(iio_alloc_pollfunc); | |
c3db00cc JC |
518 | |
519 | int iio_triggered_ring_postenable(struct iio_dev *indio_dev) | |
520 | { | |
521 | return indio_dev->trig | |
522 | ? iio_trigger_attach_poll_func(indio_dev->trig, | |
523 | indio_dev->pollfunc) | |
524 | : 0; | |
525 | } | |
526 | EXPORT_SYMBOL(iio_triggered_ring_postenable); | |
527 | ||
528 | int iio_triggered_ring_predisable(struct iio_dev *indio_dev) | |
529 | { | |
530 | return indio_dev->trig | |
531 | ? iio_trigger_dettach_poll_func(indio_dev->trig, | |
532 | indio_dev->pollfunc) | |
533 | : 0; | |
534 | } | |
535 | EXPORT_SYMBOL(iio_triggered_ring_predisable); |