staging:iio: spit trigger.h into provider and consumer parts.
[deliverable/linux.git] / drivers / staging / iio / industrialio-trigger.c
CommitLineData
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>
1637db44
JC
11#include <linux/idr.h>
12#include <linux/err.h>
13#include <linux/device.h>
14#include <linux/interrupt.h>
15#include <linux/list.h>
5a0e3ad6 16#include <linux/slab.h>
1637db44
JC
17
18#include "iio.h"
19#include "trigger.h"
df9c1c42 20#include "iio_core.h"
6aea1c36 21#include "iio_core_trigger.h"
3f72395e 22#include "trigger_consumer.h"
1637db44
JC
23
24/* RFC - Question of approach
25 * Make the common case (single sensor single trigger)
26 * simple by starting trigger capture from when first sensors
27 * is added.
28 *
29 * Complex simultaneous start requires use of 'hold' functionality
30 * of the trigger. (not implemented)
31 *
32 * Any other suggestions?
33 */
34
1637db44
JC
35static DEFINE_IDR(iio_trigger_idr);
36static DEFINE_SPINLOCK(iio_trigger_idr_lock);
37
38/* Single list of all available triggers */
39static LIST_HEAD(iio_trigger_list);
40static DEFINE_MUTEX(iio_trigger_list_lock);
41
59c85e82
JC
42/**
43 * iio_trigger_read_name() - retrieve useful identifying name
44 **/
45static ssize_t iio_trigger_read_name(struct device *dev,
46 struct device_attribute *attr,
47 char *buf)
48{
49 struct iio_trigger *trig = dev_get_drvdata(dev);
50 return sprintf(buf, "%s\n", trig->name);
51}
52
53static DEVICE_ATTR(name, S_IRUGO, iio_trigger_read_name, NULL);
54
1637db44
JC
55/**
56 * iio_trigger_register_sysfs() - create a device for this trigger
57 * @trig_info: the trigger
58 *
59 * Also adds any control attribute registered by the trigger driver
60 **/
61static int iio_trigger_register_sysfs(struct iio_trigger *trig_info)
62{
59c85e82
JC
63 return sysfs_add_file_to_group(&trig_info->dev.kobj,
64 &dev_attr_name.attr,
65 NULL);
1637db44
JC
66}
67
68static void iio_trigger_unregister_sysfs(struct iio_trigger *trig_info)
69{
59c85e82
JC
70 sysfs_remove_file_from_group(&trig_info->dev.kobj,
71 &dev_attr_name.attr,
72 NULL);
1637db44
JC
73}
74
75
76/**
77 * iio_trigger_register_id() - get a unique id for this trigger
78 * @trig_info: the trigger
79 **/
80static int iio_trigger_register_id(struct iio_trigger *trig_info)
81{
82 int ret = 0;
83
84idr_again:
85 if (unlikely(idr_pre_get(&iio_trigger_idr, GFP_KERNEL) == 0))
86 return -ENOMEM;
87
88 spin_lock(&iio_trigger_idr_lock);
89 ret = idr_get_new(&iio_trigger_idr, NULL, &trig_info->id);
90 spin_unlock(&iio_trigger_idr_lock);
91 if (unlikely(ret == -EAGAIN))
92 goto idr_again;
93 else if (likely(!ret))
94 trig_info->id = trig_info->id & MAX_ID_MASK;
95
96 return ret;
97}
98
99/**
100 * iio_trigger_unregister_id() - free up unique id for use by another trigger
101 * @trig_info: the trigger
102 **/
103static void iio_trigger_unregister_id(struct iio_trigger *trig_info)
104{
74112d3f
GKH
105 spin_lock(&iio_trigger_idr_lock);
106 idr_remove(&iio_trigger_idr, trig_info->id);
107 spin_unlock(&iio_trigger_idr_lock);
1637db44
JC
108}
109
110int iio_trigger_register(struct iio_trigger *trig_info)
111{
112 int ret;
113
114 ret = iio_trigger_register_id(trig_info);
115 if (ret)
116 goto error_ret;
117 /* Set the name used for the sysfs directory etc */
118 dev_set_name(&trig_info->dev, "trigger%ld",
119 (unsigned long) trig_info->id);
120
121 ret = device_add(&trig_info->dev);
122 if (ret)
123 goto error_unregister_id;
124
125 ret = iio_trigger_register_sysfs(trig_info);
126 if (ret)
127 goto error_device_del;
128
129 /* Add to list of available triggers held by the IIO core */
130 mutex_lock(&iio_trigger_list_lock);
131 list_add_tail(&trig_info->list, &iio_trigger_list);
132 mutex_unlock(&iio_trigger_list_lock);
133
134 return 0;
135
136error_device_del:
137 device_del(&trig_info->dev);
138error_unregister_id:
139 iio_trigger_unregister_id(trig_info);
140error_ret:
141 return ret;
142}
143EXPORT_SYMBOL(iio_trigger_register);
144
145void iio_trigger_unregister(struct iio_trigger *trig_info)
146{
1637db44 147 mutex_lock(&iio_trigger_list_lock);
582e5489 148 list_del(&trig_info->list);
1637db44
JC
149 mutex_unlock(&iio_trigger_list_lock);
150
151 iio_trigger_unregister_sysfs(trig_info);
152 iio_trigger_unregister_id(trig_info);
153 /* Possible issue in here */
154 device_unregister(&trig_info->dev);
155}
156EXPORT_SYMBOL(iio_trigger_unregister);
157
f6517f22
JC
158static struct iio_trigger *iio_trigger_find_by_name(const char *name,
159 size_t len)
1637db44 160{
f6517f22 161 struct iio_trigger *trig = NULL, *iter;
7c327857 162
1637db44 163 mutex_lock(&iio_trigger_list_lock);
f6517f22
JC
164 list_for_each_entry(iter, &iio_trigger_list, list)
165 if (sysfs_streq(iter->name, name)) {
166 trig = iter;
1637db44
JC
167 break;
168 }
1637db44
JC
169 mutex_unlock(&iio_trigger_list_lock);
170
f6517f22 171 return trig;
5f87404d 172}
1637db44 173
7b2c33b1 174void iio_trigger_poll(struct iio_trigger *trig, s64 time)
1637db44 175{
d96d1337 176 int i;
d96d1337
JC
177 if (!trig->use_count) {
178 for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++)
179 if (trig->subirqs[i].enabled) {
180 trig->use_count++;
181 generic_handle_irq(trig->subirq_base + i);
182 }
183 }
1637db44
JC
184}
185EXPORT_SYMBOL(iio_trigger_poll);
8384d957
JC
186
187irqreturn_t iio_trigger_generic_data_rdy_poll(int irq, void *private)
188{
189 iio_trigger_poll(private, iio_get_time_ns());
190 return IRQ_HANDLED;
191}
192EXPORT_SYMBOL(iio_trigger_generic_data_rdy_poll);
1637db44 193
1f785681
JC
194void iio_trigger_poll_chained(struct iio_trigger *trig, s64 time)
195{
196 int i;
197 if (!trig->use_count) {
198 for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++)
199 if (trig->subirqs[i].enabled) {
200 trig->use_count++;
201 handle_nested_irq(trig->subirq_base + i);
202 }
203 }
204}
205EXPORT_SYMBOL(iio_trigger_poll_chained);
206
1637db44
JC
207void iio_trigger_notify_done(struct iio_trigger *trig)
208{
209 trig->use_count--;
d29f73db
JC
210 if (trig->use_count == 0 && trig->ops && trig->ops->try_reenable)
211 if (trig->ops->try_reenable(trig)) {
1637db44 212 /* Missed and interrupt so launch new poll now */
7b2c33b1 213 iio_trigger_poll(trig, 0);
1637db44
JC
214 }
215}
216EXPORT_SYMBOL(iio_trigger_notify_done);
217
1637db44
JC
218/* Trigger Consumer related functions */
219
220/* Complexity in here. With certain triggers (datardy) an acknowledgement
221 * may be needed if the pollfuncs do not include the data read for the
222 * triggering device.
223 * This is not currently handled. Alternative of not enabling trigger unless
224 * the relevant function is in there may be the best option.
225 */
226/* Worth protecting against double additions?*/
227int iio_trigger_attach_poll_func(struct iio_trigger *trig,
228 struct iio_poll_func *pf)
229{
230 int ret = 0;
51c060a0
JC
231 bool notinuse
232 = bitmap_empty(trig->pool, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
233
f60c4a02
JC
234 /* Prevent the module being removed whilst attached to a trigger */
235 __module_get(pf->indio_dev->info->driver_module);
51c060a0
JC
236 pf->irq = iio_trigger_get_irq(trig);
237 ret = request_threaded_irq(pf->irq, pf->h, pf->thread,
238 pf->type, pf->name,
239 pf);
d29f73db
JC
240 if (trig->ops && trig->ops->set_trigger_state && notinuse)
241 ret = trig->ops->set_trigger_state(trig, true);
d96d1337 242
1637db44
JC
243 return ret;
244}
245EXPORT_SYMBOL(iio_trigger_attach_poll_func);
246
247int iio_trigger_dettach_poll_func(struct iio_trigger *trig,
248 struct iio_poll_func *pf)
249{
51c060a0
JC
250 int ret = 0;
251 bool no_other_users
252 = (bitmap_weight(trig->pool,
253 CONFIG_IIO_CONSUMERS_PER_TRIGGER)
254 == 1);
d29f73db
JC
255 if (trig->ops && trig->ops->set_trigger_state && no_other_users) {
256 ret = trig->ops->set_trigger_state(trig, false);
51c060a0
JC
257 if (ret)
258 goto error_ret;
1637db44 259 }
51c060a0
JC
260 iio_trigger_put_irq(trig, pf->irq);
261 free_irq(pf->irq, pf);
f60c4a02 262 module_put(pf->indio_dev->info->driver_module);
1637db44
JC
263
264error_ret:
265 return ret;
266}
267EXPORT_SYMBOL(iio_trigger_dettach_poll_func);
268
d96d1337
JC
269irqreturn_t iio_pollfunc_store_time(int irq, void *p)
270{
271 struct iio_poll_func *pf = p;
272 pf->timestamp = iio_get_time_ns();
273 return IRQ_WAKE_THREAD;
274}
275EXPORT_SYMBOL(iio_pollfunc_store_time);
276
21b185f8
JC
277struct iio_poll_func
278*iio_alloc_pollfunc(irqreturn_t (*h)(int irq, void *p),
279 irqreturn_t (*thread)(int irq, void *p),
280 int type,
e65bc6ac 281 struct iio_dev *indio_dev,
21b185f8
JC
282 const char *fmt,
283 ...)
284{
285 va_list vargs;
286 struct iio_poll_func *pf;
287
288 pf = kmalloc(sizeof *pf, GFP_KERNEL);
289 if (pf == NULL)
290 return NULL;
291 va_start(vargs, fmt);
292 pf->name = kvasprintf(GFP_KERNEL, fmt, vargs);
293 va_end(vargs);
294 if (pf->name == NULL) {
295 kfree(pf);
296 return NULL;
297 }
298 pf->h = h;
299 pf->thread = thread;
300 pf->type = type;
e65bc6ac 301 pf->indio_dev = indio_dev;
21b185f8
JC
302
303 return pf;
304}
305EXPORT_SYMBOL_GPL(iio_alloc_pollfunc);
306
307void iio_dealloc_pollfunc(struct iio_poll_func *pf)
308{
309 kfree(pf->name);
310 kfree(pf);
311}
312EXPORT_SYMBOL_GPL(iio_dealloc_pollfunc);
313
1637db44 314/**
751a3700 315 * iio_trigger_read_currrent() - trigger consumer sysfs query which trigger
1637db44
JC
316 *
317 * For trigger consumers the current_trigger interface allows the trigger
318 * used by the device to be queried.
319 **/
320static ssize_t iio_trigger_read_current(struct device *dev,
321 struct device_attribute *attr,
322 char *buf)
323{
324 struct iio_dev *dev_info = dev_get_drvdata(dev);
325 int len = 0;
326 if (dev_info->trig)
3c9bbf58
JC
327 len = sprintf(buf,
328 "%s\n",
329 dev_info->trig->name);
1637db44
JC
330 return len;
331}
332
333/**
334 * iio_trigger_write_current() trigger consumer sysfs set current trigger
335 *
336 * For trigger consumers the current_trigger interface allows the trigger
337 * used for this device to be specified at run time based on the triggers
338 * name.
339 **/
340static ssize_t iio_trigger_write_current(struct device *dev,
341 struct device_attribute *attr,
342 const char *buf,
343 size_t len)
344{
345 struct iio_dev *dev_info = dev_get_drvdata(dev);
346 struct iio_trigger *oldtrig = dev_info->trig;
43a4360e
MH
347 struct iio_trigger *trig;
348 int ret;
349
1637db44
JC
350 mutex_lock(&dev_info->mlock);
351 if (dev_info->currentmode == INDIO_RING_TRIGGERED) {
352 mutex_unlock(&dev_info->mlock);
353 return -EBUSY;
354 }
355 mutex_unlock(&dev_info->mlock);
356
43a4360e
MH
357 trig = iio_trigger_find_by_name(buf, len);
358
359 if (trig && dev_info->info->validate_trigger) {
360 ret = dev_info->info->validate_trigger(dev_info, trig);
361 if (ret)
362 return ret;
363 }
364
d29f73db
JC
365 if (trig && trig->ops && trig->ops->validate_device) {
366 ret = trig->ops->validate_device(trig, dev_info);
43a4360e
MH
367 if (ret)
368 return ret;
369 }
370
371 dev_info->trig = trig;
372
1637db44
JC
373 if (oldtrig && dev_info->trig != oldtrig)
374 iio_put_trigger(oldtrig);
375 if (dev_info->trig)
376 iio_get_trigger(dev_info->trig);
377
378 return len;
379}
380
74112d3f
GKH
381static DEVICE_ATTR(current_trigger, S_IRUGO | S_IWUSR,
382 iio_trigger_read_current,
383 iio_trigger_write_current);
1637db44
JC
384
385static struct attribute *iio_trigger_consumer_attrs[] = {
386 &dev_attr_current_trigger.attr,
387 NULL,
388};
389
390static const struct attribute_group iio_trigger_consumer_attr_group = {
391 .name = "trigger",
392 .attrs = iio_trigger_consumer_attrs,
393};
394
395static void iio_trig_release(struct device *device)
396{
397 struct iio_trigger *trig = to_iio_trigger(device);
d96d1337
JC
398 int i;
399
400 if (trig->subirq_base) {
401 for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
402 irq_modify_status(trig->subirq_base + i,
403 IRQ_NOAUTOEN,
404 IRQ_NOREQUEST | IRQ_NOPROBE);
405 irq_set_chip(trig->subirq_base + i,
406 NULL);
407 irq_set_handler(trig->subirq_base + i,
408 NULL);
409 }
410
411 irq_free_descs(trig->subirq_base,
412 CONFIG_IIO_CONSUMERS_PER_TRIGGER);
413 }
59c85e82 414 kfree(trig->name);
1637db44
JC
415 kfree(trig);
416 iio_put();
417}
418
419static struct device_type iio_trig_type = {
420 .release = iio_trig_release,
421};
422
d96d1337
JC
423static void iio_trig_subirqmask(struct irq_data *d)
424{
425 struct irq_chip *chip = irq_data_get_irq_chip(d);
426 struct iio_trigger *trig
427 = container_of(chip,
428 struct iio_trigger, subirq_chip);
429 trig->subirqs[d->irq - trig->subirq_base].enabled = false;
430}
431
432static void iio_trig_subirqunmask(struct irq_data *d)
433{
434 struct irq_chip *chip = irq_data_get_irq_chip(d);
435 struct iio_trigger *trig
436 = container_of(chip,
437 struct iio_trigger, subirq_chip);
438 trig->subirqs[d->irq - trig->subirq_base].enabled = true;
439}
440
59c85e82 441struct iio_trigger *iio_allocate_trigger(const char *fmt, ...)
1637db44 442{
59c85e82 443 va_list vargs;
1637db44
JC
444 struct iio_trigger *trig;
445 trig = kzalloc(sizeof *trig, GFP_KERNEL);
446 if (trig) {
d96d1337 447 int i;
1637db44 448 trig->dev.type = &iio_trig_type;
5aaaeba8 449 trig->dev.bus = &iio_bus_type;
1637db44
JC
450 device_initialize(&trig->dev);
451 dev_set_drvdata(&trig->dev, (void *)trig);
d96d1337 452
59c85e82
JC
453 mutex_init(&trig->pool_lock);
454 trig->subirq_base
455 = irq_alloc_descs(-1, 0,
456 CONFIG_IIO_CONSUMERS_PER_TRIGGER,
457 0);
458 if (trig->subirq_base < 0) {
459 kfree(trig);
460 return NULL;
461 }
462 va_start(vargs, fmt);
463 trig->name = kvasprintf(GFP_KERNEL, fmt, vargs);
464 va_end(vargs);
465 if (trig->name == NULL) {
466 irq_free_descs(trig->subirq_base,
467 CONFIG_IIO_CONSUMERS_PER_TRIGGER);
468 kfree(trig);
469 return NULL;
470 }
471 trig->subirq_chip.name = trig->name;
472 trig->subirq_chip.irq_mask = &iio_trig_subirqmask;
473 trig->subirq_chip.irq_unmask = &iio_trig_subirqunmask;
474 for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
475 irq_set_chip(trig->subirq_base + i,
476 &trig->subirq_chip);
477 irq_set_handler(trig->subirq_base + i,
478 &handle_simple_irq);
479 irq_modify_status(trig->subirq_base + i,
480 IRQ_NOREQUEST | IRQ_NOAUTOEN,
481 IRQ_NOPROBE);
d96d1337 482 }
1637db44 483 iio_get();
5f9c035c 484 get_device(&trig->dev);
1637db44
JC
485 }
486 return trig;
487}
488EXPORT_SYMBOL(iio_allocate_trigger);
489
490void iio_free_trigger(struct iio_trigger *trig)
491{
492 if (trig)
493 put_device(&trig->dev);
494}
495EXPORT_SYMBOL(iio_free_trigger);
496
497int iio_device_register_trigger_consumer(struct iio_dev *dev_info)
498{
499 int ret;
500 ret = sysfs_create_group(&dev_info->dev.kobj,
501 &iio_trigger_consumer_attr_group);
502 return ret;
503}
504EXPORT_SYMBOL(iio_device_register_trigger_consumer);
505
506int iio_device_unregister_trigger_consumer(struct iio_dev *dev_info)
507{
5f9c035c
JC
508 /* Clean up and associated but not attached triggers references */
509 if (dev_info->trig)
510 iio_put_trigger(dev_info->trig);
1637db44
JC
511 sysfs_remove_group(&dev_info->dev.kobj,
512 &iio_trigger_consumer_attr_group);
513 return 0;
514}
515EXPORT_SYMBOL(iio_device_unregister_trigger_consumer);
516
c3db00cc
JC
517int iio_triggered_ring_postenable(struct iio_dev *indio_dev)
518{
519 return indio_dev->trig
520 ? iio_trigger_attach_poll_func(indio_dev->trig,
521 indio_dev->pollfunc)
522 : 0;
523}
524EXPORT_SYMBOL(iio_triggered_ring_postenable);
525
526int iio_triggered_ring_predisable(struct iio_dev *indio_dev)
527{
528 return indio_dev->trig
529 ? iio_trigger_dettach_poll_func(indio_dev->trig,
530 indio_dev->pollfunc)
531 : 0;
532}
533EXPORT_SYMBOL(iio_triggered_ring_predisable);
This page took 0.230215 seconds and 5 git commands to generate.