staging/lustre: Update FID documentation link.
[cascardo/linux.git] / drivers / iio / industrialio-trigger.c
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/idr.h>
12 #include <linux/err.h>
13 #include <linux/device.h>
14 #include <linux/interrupt.h>
15 #include <linux/list.h>
16 #include <linux/slab.h>
17
18 #include <linux/iio/iio.h>
19 #include <linux/iio/trigger.h>
20 #include "iio_core.h"
21 #include "iio_core_trigger.h"
22 #include <linux/iio/trigger_consumer.h>
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
35 static DEFINE_IDA(iio_trigger_ida);
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_read_name() - retrieve useful identifying name
43  * @dev:        device associated with the iio_trigger
44  * @attr:       pointer to the device_attribute structure that is
45  *              being processed
46  * @buf:        buffer to print the name into
47  *
48  * Return: a negative number on failure or the number of written
49  *         characters on success.
50  */
51 static ssize_t iio_trigger_read_name(struct device *dev,
52                                      struct device_attribute *attr,
53                                      char *buf)
54 {
55         struct iio_trigger *trig = to_iio_trigger(dev);
56         return sprintf(buf, "%s\n", trig->name);
57 }
58
59 static DEVICE_ATTR(name, S_IRUGO, iio_trigger_read_name, NULL);
60
61 static struct attribute *iio_trig_dev_attrs[] = {
62         &dev_attr_name.attr,
63         NULL,
64 };
65 ATTRIBUTE_GROUPS(iio_trig_dev);
66
67 int iio_trigger_register(struct iio_trigger *trig_info)
68 {
69         int ret;
70
71         /* trig_info->ops is required for the module member */
72         if (!trig_info->ops)
73                 return -EINVAL;
74
75         trig_info->id = ida_simple_get(&iio_trigger_ida, 0, 0, GFP_KERNEL);
76         if (trig_info->id < 0)
77                 return trig_info->id;
78
79         /* Set the name used for the sysfs directory etc */
80         dev_set_name(&trig_info->dev, "trigger%ld",
81                      (unsigned long) trig_info->id);
82
83         ret = device_add(&trig_info->dev);
84         if (ret)
85                 goto error_unregister_id;
86
87         /* Add to list of available triggers held by the IIO core */
88         mutex_lock(&iio_trigger_list_lock);
89         list_add_tail(&trig_info->list, &iio_trigger_list);
90         mutex_unlock(&iio_trigger_list_lock);
91
92         return 0;
93
94 error_unregister_id:
95         ida_simple_remove(&iio_trigger_ida, trig_info->id);
96         return ret;
97 }
98 EXPORT_SYMBOL(iio_trigger_register);
99
100 void iio_trigger_unregister(struct iio_trigger *trig_info)
101 {
102         mutex_lock(&iio_trigger_list_lock);
103         list_del(&trig_info->list);
104         mutex_unlock(&iio_trigger_list_lock);
105
106         ida_simple_remove(&iio_trigger_ida, trig_info->id);
107         /* Possible issue in here */
108         device_del(&trig_info->dev);
109 }
110 EXPORT_SYMBOL(iio_trigger_unregister);
111
112 static struct iio_trigger *iio_trigger_find_by_name(const char *name,
113                                                     size_t len)
114 {
115         struct iio_trigger *trig = NULL, *iter;
116
117         mutex_lock(&iio_trigger_list_lock);
118         list_for_each_entry(iter, &iio_trigger_list, list)
119                 if (sysfs_streq(iter->name, name)) {
120                         trig = iter;
121                         break;
122                 }
123         mutex_unlock(&iio_trigger_list_lock);
124
125         return trig;
126 }
127
128 void iio_trigger_poll(struct iio_trigger *trig)
129 {
130         int i;
131
132         if (!atomic_read(&trig->use_count)) {
133                 atomic_set(&trig->use_count, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
134
135                 for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
136                         if (trig->subirqs[i].enabled)
137                                 generic_handle_irq(trig->subirq_base + i);
138                         else
139                                 iio_trigger_notify_done(trig);
140                 }
141         }
142 }
143 EXPORT_SYMBOL(iio_trigger_poll);
144
145 irqreturn_t iio_trigger_generic_data_rdy_poll(int irq, void *private)
146 {
147         iio_trigger_poll(private);
148         return IRQ_HANDLED;
149 }
150 EXPORT_SYMBOL(iio_trigger_generic_data_rdy_poll);
151
152 void iio_trigger_poll_chained(struct iio_trigger *trig)
153 {
154         int i;
155
156         if (!atomic_read(&trig->use_count)) {
157                 atomic_set(&trig->use_count, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
158
159                 for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
160                         if (trig->subirqs[i].enabled)
161                                 handle_nested_irq(trig->subirq_base + i);
162                         else
163                                 iio_trigger_notify_done(trig);
164                 }
165         }
166 }
167 EXPORT_SYMBOL(iio_trigger_poll_chained);
168
169 void iio_trigger_notify_done(struct iio_trigger *trig)
170 {
171         if (atomic_dec_and_test(&trig->use_count) && trig->ops->try_reenable)
172                 if (trig->ops->try_reenable(trig))
173                         /* Missed an interrupt so launch new poll now */
174                         iio_trigger_poll(trig);
175 }
176 EXPORT_SYMBOL(iio_trigger_notify_done);
177
178 /* Trigger Consumer related functions */
179 static int iio_trigger_get_irq(struct iio_trigger *trig)
180 {
181         int ret;
182         mutex_lock(&trig->pool_lock);
183         ret = bitmap_find_free_region(trig->pool,
184                                       CONFIG_IIO_CONSUMERS_PER_TRIGGER,
185                                       ilog2(1));
186         mutex_unlock(&trig->pool_lock);
187         if (ret >= 0)
188                 ret += trig->subirq_base;
189
190         return ret;
191 }
192
193 static void iio_trigger_put_irq(struct iio_trigger *trig, int irq)
194 {
195         mutex_lock(&trig->pool_lock);
196         clear_bit(irq - trig->subirq_base, trig->pool);
197         mutex_unlock(&trig->pool_lock);
198 }
199
200 /* Complexity in here.  With certain triggers (datardy) an acknowledgement
201  * may be needed if the pollfuncs do not include the data read for the
202  * triggering device.
203  * This is not currently handled.  Alternative of not enabling trigger unless
204  * the relevant function is in there may be the best option.
205  */
206 /* Worth protecting against double additions? */
207 static int iio_trigger_attach_poll_func(struct iio_trigger *trig,
208                                         struct iio_poll_func *pf)
209 {
210         int ret = 0;
211         bool notinuse
212                 = bitmap_empty(trig->pool, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
213
214         /* Prevent the module from being removed whilst attached to a trigger */
215         __module_get(pf->indio_dev->info->driver_module);
216         pf->irq = iio_trigger_get_irq(trig);
217         ret = request_threaded_irq(pf->irq, pf->h, pf->thread,
218                                    pf->type, pf->name,
219                                    pf);
220         if (ret < 0) {
221                 module_put(pf->indio_dev->info->driver_module);
222                 return ret;
223         }
224
225         if (trig->ops->set_trigger_state && notinuse) {
226                 ret = trig->ops->set_trigger_state(trig, true);
227                 if (ret < 0)
228                         module_put(pf->indio_dev->info->driver_module);
229         }
230
231         return ret;
232 }
233
234 static int iio_trigger_detach_poll_func(struct iio_trigger *trig,
235                                          struct iio_poll_func *pf)
236 {
237         int ret = 0;
238         bool no_other_users
239                 = (bitmap_weight(trig->pool,
240                                  CONFIG_IIO_CONSUMERS_PER_TRIGGER)
241                    == 1);
242         if (trig->ops->set_trigger_state && no_other_users) {
243                 ret = trig->ops->set_trigger_state(trig, false);
244                 if (ret)
245                         return ret;
246         }
247         iio_trigger_put_irq(trig, pf->irq);
248         free_irq(pf->irq, pf);
249         module_put(pf->indio_dev->info->driver_module);
250
251         return ret;
252 }
253
254 irqreturn_t iio_pollfunc_store_time(int irq, void *p)
255 {
256         struct iio_poll_func *pf = p;
257         pf->timestamp = iio_get_time_ns();
258         return IRQ_WAKE_THREAD;
259 }
260 EXPORT_SYMBOL(iio_pollfunc_store_time);
261
262 struct iio_poll_func
263 *iio_alloc_pollfunc(irqreturn_t (*h)(int irq, void *p),
264                     irqreturn_t (*thread)(int irq, void *p),
265                     int type,
266                     struct iio_dev *indio_dev,
267                     const char *fmt,
268                     ...)
269 {
270         va_list vargs;
271         struct iio_poll_func *pf;
272
273         pf = kmalloc(sizeof *pf, GFP_KERNEL);
274         if (pf == NULL)
275                 return NULL;
276         va_start(vargs, fmt);
277         pf->name = kvasprintf(GFP_KERNEL, fmt, vargs);
278         va_end(vargs);
279         if (pf->name == NULL) {
280                 kfree(pf);
281                 return NULL;
282         }
283         pf->h = h;
284         pf->thread = thread;
285         pf->type = type;
286         pf->indio_dev = indio_dev;
287
288         return pf;
289 }
290 EXPORT_SYMBOL_GPL(iio_alloc_pollfunc);
291
292 void iio_dealloc_pollfunc(struct iio_poll_func *pf)
293 {
294         kfree(pf->name);
295         kfree(pf);
296 }
297 EXPORT_SYMBOL_GPL(iio_dealloc_pollfunc);
298
299 /**
300  * iio_trigger_read_current() - trigger consumer sysfs query current trigger
301  * @dev:        device associated with an industrial I/O device
302  * @attr:       pointer to the device_attribute structure that
303  *              is being processed
304  * @buf:        buffer where the current trigger name will be printed into
305  *
306  * For trigger consumers the current_trigger interface allows the trigger
307  * used by the device to be queried.
308  *
309  * Return: a negative number on failure, the number of characters written
310  *         on success or 0 if no trigger is available
311  */
312 static ssize_t iio_trigger_read_current(struct device *dev,
313                                         struct device_attribute *attr,
314                                         char *buf)
315 {
316         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
317
318         if (indio_dev->trig)
319                 return sprintf(buf, "%s\n", indio_dev->trig->name);
320         return 0;
321 }
322
323 /**
324  * iio_trigger_write_current() - trigger consumer sysfs set current trigger
325  * @dev:        device associated with an industrial I/O device
326  * @attr:       device attribute that is being processed
327  * @buf:        string buffer that holds the name of the trigger
328  * @len:        length of the trigger name held by buf
329  *
330  * For trigger consumers the current_trigger interface allows the trigger
331  * used for this device to be specified at run time based on the trigger's
332  * name.
333  *
334  * Return: negative error code on failure or length of the buffer
335  *         on success
336  */
337 static ssize_t iio_trigger_write_current(struct device *dev,
338                                          struct device_attribute *attr,
339                                          const char *buf,
340                                          size_t len)
341 {
342         struct iio_dev *indio_dev = dev_to_iio_dev(dev);
343         struct iio_trigger *oldtrig = indio_dev->trig;
344         struct iio_trigger *trig;
345         int ret;
346
347         mutex_lock(&indio_dev->mlock);
348         if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED) {
349                 mutex_unlock(&indio_dev->mlock);
350                 return -EBUSY;
351         }
352         mutex_unlock(&indio_dev->mlock);
353
354         trig = iio_trigger_find_by_name(buf, len);
355         if (oldtrig == trig)
356                 return len;
357
358         if (trig && indio_dev->info->validate_trigger) {
359                 ret = indio_dev->info->validate_trigger(indio_dev, trig);
360                 if (ret)
361                         return ret;
362         }
363
364         if (trig && trig->ops->validate_device) {
365                 ret = trig->ops->validate_device(trig, indio_dev);
366                 if (ret)
367                         return ret;
368         }
369
370         indio_dev->trig = trig;
371
372         if (oldtrig) {
373                 if (indio_dev->modes & INDIO_EVENT_TRIGGERED)
374                         iio_trigger_detach_poll_func(oldtrig,
375                                                      indio_dev->pollfunc_event);
376                 iio_trigger_put(oldtrig);
377         }
378         if (indio_dev->trig) {
379                 iio_trigger_get(indio_dev->trig);
380                 if (indio_dev->modes & INDIO_EVENT_TRIGGERED)
381                         iio_trigger_attach_poll_func(indio_dev->trig,
382                                                      indio_dev->pollfunc_event);
383         }
384
385         return len;
386 }
387
388 static DEVICE_ATTR(current_trigger, S_IRUGO | S_IWUSR,
389                    iio_trigger_read_current,
390                    iio_trigger_write_current);
391
392 static struct attribute *iio_trigger_consumer_attrs[] = {
393         &dev_attr_current_trigger.attr,
394         NULL,
395 };
396
397 static const struct attribute_group iio_trigger_consumer_attr_group = {
398         .name = "trigger",
399         .attrs = iio_trigger_consumer_attrs,
400 };
401
402 static void iio_trig_release(struct device *device)
403 {
404         struct iio_trigger *trig = to_iio_trigger(device);
405         int i;
406
407         if (trig->subirq_base) {
408                 for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
409                         irq_modify_status(trig->subirq_base + i,
410                                           IRQ_NOAUTOEN,
411                                           IRQ_NOREQUEST | IRQ_NOPROBE);
412                         irq_set_chip(trig->subirq_base + i,
413                                      NULL);
414                         irq_set_handler(trig->subirq_base + i,
415                                         NULL);
416                 }
417
418                 irq_free_descs(trig->subirq_base,
419                                CONFIG_IIO_CONSUMERS_PER_TRIGGER);
420         }
421         kfree(trig->name);
422         kfree(trig);
423 }
424
425 static struct device_type iio_trig_type = {
426         .release = iio_trig_release,
427         .groups = iio_trig_dev_groups,
428 };
429
430 static void iio_trig_subirqmask(struct irq_data *d)
431 {
432         struct irq_chip *chip = irq_data_get_irq_chip(d);
433         struct iio_trigger *trig
434                 = container_of(chip,
435                                struct iio_trigger, subirq_chip);
436         trig->subirqs[d->irq - trig->subirq_base].enabled = false;
437 }
438
439 static void iio_trig_subirqunmask(struct irq_data *d)
440 {
441         struct irq_chip *chip = irq_data_get_irq_chip(d);
442         struct iio_trigger *trig
443                 = container_of(chip,
444                                struct iio_trigger, subirq_chip);
445         trig->subirqs[d->irq - trig->subirq_base].enabled = true;
446 }
447
448 static struct iio_trigger *viio_trigger_alloc(const char *fmt, va_list vargs)
449 {
450         struct iio_trigger *trig;
451         trig = kzalloc(sizeof *trig, GFP_KERNEL);
452         if (trig) {
453                 int i;
454                 trig->dev.type = &iio_trig_type;
455                 trig->dev.bus = &iio_bus_type;
456                 device_initialize(&trig->dev);
457
458                 mutex_init(&trig->pool_lock);
459                 trig->subirq_base
460                         = irq_alloc_descs(-1, 0,
461                                           CONFIG_IIO_CONSUMERS_PER_TRIGGER,
462                                           0);
463                 if (trig->subirq_base < 0) {
464                         kfree(trig);
465                         return NULL;
466                 }
467
468                 trig->name = kvasprintf(GFP_KERNEL, fmt, vargs);
469                 if (trig->name == NULL) {
470                         irq_free_descs(trig->subirq_base,
471                                        CONFIG_IIO_CONSUMERS_PER_TRIGGER);
472                         kfree(trig);
473                         return NULL;
474                 }
475                 trig->subirq_chip.name = trig->name;
476                 trig->subirq_chip.irq_mask = &iio_trig_subirqmask;
477                 trig->subirq_chip.irq_unmask = &iio_trig_subirqunmask;
478                 for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
479                         irq_set_chip(trig->subirq_base + i,
480                                      &trig->subirq_chip);
481                         irq_set_handler(trig->subirq_base + i,
482                                         &handle_simple_irq);
483                         irq_modify_status(trig->subirq_base + i,
484                                           IRQ_NOREQUEST | IRQ_NOAUTOEN,
485                                           IRQ_NOPROBE);
486                 }
487                 get_device(&trig->dev);
488         }
489
490         return trig;
491 }
492
493 struct iio_trigger *iio_trigger_alloc(const char *fmt, ...)
494 {
495         struct iio_trigger *trig;
496         va_list vargs;
497
498         va_start(vargs, fmt);
499         trig = viio_trigger_alloc(fmt, vargs);
500         va_end(vargs);
501
502         return trig;
503 }
504 EXPORT_SYMBOL(iio_trigger_alloc);
505
506 void iio_trigger_free(struct iio_trigger *trig)
507 {
508         if (trig)
509                 put_device(&trig->dev);
510 }
511 EXPORT_SYMBOL(iio_trigger_free);
512
513 static void devm_iio_trigger_release(struct device *dev, void *res)
514 {
515         iio_trigger_free(*(struct iio_trigger **)res);
516 }
517
518 static int devm_iio_trigger_match(struct device *dev, void *res, void *data)
519 {
520         struct iio_trigger **r = res;
521
522         if (!r || !*r) {
523                 WARN_ON(!r || !*r);
524                 return 0;
525         }
526
527         return *r == data;
528 }
529
530 /**
531  * devm_iio_trigger_alloc - Resource-managed iio_trigger_alloc()
532  * @dev:                Device to allocate iio_trigger for
533  * @fmt:                trigger name format. If it includes format
534  *                      specifiers, the additional arguments following
535  *                      format are formatted and inserted in the resulting
536  *                      string replacing their respective specifiers.
537  *
538  * Managed iio_trigger_alloc.  iio_trigger allocated with this function is
539  * automatically freed on driver detach.
540  *
541  * If an iio_trigger allocated with this function needs to be freed separately,
542  * devm_iio_trigger_free() must be used.
543  *
544  * RETURNS:
545  * Pointer to allocated iio_trigger on success, NULL on failure.
546  */
547 struct iio_trigger *devm_iio_trigger_alloc(struct device *dev,
548                                                 const char *fmt, ...)
549 {
550         struct iio_trigger **ptr, *trig;
551         va_list vargs;
552
553         ptr = devres_alloc(devm_iio_trigger_release, sizeof(*ptr),
554                            GFP_KERNEL);
555         if (!ptr)
556                 return NULL;
557
558         /* use raw alloc_dr for kmalloc caller tracing */
559         va_start(vargs, fmt);
560         trig = viio_trigger_alloc(fmt, vargs);
561         va_end(vargs);
562         if (trig) {
563                 *ptr = trig;
564                 devres_add(dev, ptr);
565         } else {
566                 devres_free(ptr);
567         }
568
569         return trig;
570 }
571 EXPORT_SYMBOL_GPL(devm_iio_trigger_alloc);
572
573 /**
574  * devm_iio_trigger_free - Resource-managed iio_trigger_free()
575  * @dev:                Device this iio_dev belongs to
576  * @iio_trig:           the iio_trigger associated with the device
577  *
578  * Free iio_trigger allocated with devm_iio_trigger_alloc().
579  */
580 void devm_iio_trigger_free(struct device *dev, struct iio_trigger *iio_trig)
581 {
582         int rc;
583
584         rc = devres_release(dev, devm_iio_trigger_release,
585                             devm_iio_trigger_match, iio_trig);
586         WARN_ON(rc);
587 }
588 EXPORT_SYMBOL_GPL(devm_iio_trigger_free);
589
590 void iio_device_register_trigger_consumer(struct iio_dev *indio_dev)
591 {
592         indio_dev->groups[indio_dev->groupcounter++] =
593                 &iio_trigger_consumer_attr_group;
594 }
595
596 void iio_device_unregister_trigger_consumer(struct iio_dev *indio_dev)
597 {
598         /* Clean up an associated but not attached trigger reference */
599         if (indio_dev->trig)
600                 iio_trigger_put(indio_dev->trig);
601 }
602
603 int iio_triggered_buffer_postenable(struct iio_dev *indio_dev)
604 {
605         return iio_trigger_attach_poll_func(indio_dev->trig,
606                                             indio_dev->pollfunc);
607 }
608 EXPORT_SYMBOL(iio_triggered_buffer_postenable);
609
610 int iio_triggered_buffer_predisable(struct iio_dev *indio_dev)
611 {
612         return iio_trigger_detach_poll_func(indio_dev->trig,
613                                              indio_dev->pollfunc);
614 }
615 EXPORT_SYMBOL(iio_triggered_buffer_predisable);