Merge git://git.kernel.org/pub/scm/linux/kernel/git/lwfinger/r8192E into staging...
[pandora-kernel.git] / drivers / staging / 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 "iio.h"
19 #include "trigger.h"
20 #include "iio_core.h"
21 #include "iio_core_trigger.h"
22 #include "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_IDR(iio_trigger_idr);
36 static DEFINE_SPINLOCK(iio_trigger_idr_lock);
37
38 /* Single list of all available triggers */
39 static LIST_HEAD(iio_trigger_list);
40 static DEFINE_MUTEX(iio_trigger_list_lock);
41
42 /**
43  * iio_trigger_read_name() - retrieve useful identifying name
44  **/
45 static 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
53 static DEVICE_ATTR(name, S_IRUGO, iio_trigger_read_name, NULL);
54
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  **/
61 static int iio_trigger_register_sysfs(struct iio_trigger *trig_info)
62 {
63         return sysfs_add_file_to_group(&trig_info->dev.kobj,
64                                        &dev_attr_name.attr,
65                                        NULL);
66 }
67
68 static void iio_trigger_unregister_sysfs(struct iio_trigger *trig_info)
69 {
70         sysfs_remove_file_from_group(&trig_info->dev.kobj,
71                                            &dev_attr_name.attr,
72                                            NULL);
73 }
74
75
76 /**
77  * iio_trigger_register_id() - get a unique id for this trigger
78  * @trig_info:  the trigger
79  **/
80 static int iio_trigger_register_id(struct iio_trigger *trig_info)
81 {
82         int ret = 0;
83
84 idr_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  **/
103 static void iio_trigger_unregister_id(struct iio_trigger *trig_info)
104 {
105         spin_lock(&iio_trigger_idr_lock);
106         idr_remove(&iio_trigger_idr, trig_info->id);
107         spin_unlock(&iio_trigger_idr_lock);
108 }
109
110 int 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
136 error_device_del:
137         device_del(&trig_info->dev);
138 error_unregister_id:
139         iio_trigger_unregister_id(trig_info);
140 error_ret:
141         return ret;
142 }
143 EXPORT_SYMBOL(iio_trigger_register);
144
145 void iio_trigger_unregister(struct iio_trigger *trig_info)
146 {
147         mutex_lock(&iio_trigger_list_lock);
148         list_del(&trig_info->list);
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 }
156 EXPORT_SYMBOL(iio_trigger_unregister);
157
158 static struct iio_trigger *iio_trigger_find_by_name(const char *name,
159                                                     size_t len)
160 {
161         struct iio_trigger *trig = NULL, *iter;
162
163         mutex_lock(&iio_trigger_list_lock);
164         list_for_each_entry(iter, &iio_trigger_list, list)
165                 if (sysfs_streq(iter->name, name)) {
166                         trig = iter;
167                         break;
168                 }
169         mutex_unlock(&iio_trigger_list_lock);
170
171         return trig;
172 }
173
174 void iio_trigger_poll(struct iio_trigger *trig, s64 time)
175 {
176         int i;
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 }
184 EXPORT_SYMBOL(iio_trigger_poll);
185
186 irqreturn_t iio_trigger_generic_data_rdy_poll(int irq, void *private)
187 {
188         iio_trigger_poll(private, iio_get_time_ns());
189         return IRQ_HANDLED;
190 }
191 EXPORT_SYMBOL(iio_trigger_generic_data_rdy_poll);
192
193 void iio_trigger_poll_chained(struct iio_trigger *trig, s64 time)
194 {
195         int i;
196         if (!trig->use_count) {
197                 for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++)
198                         if (trig->subirqs[i].enabled) {
199                                 trig->use_count++;
200                                 handle_nested_irq(trig->subirq_base + i);
201                         }
202         }
203 }
204 EXPORT_SYMBOL(iio_trigger_poll_chained);
205
206 void iio_trigger_notify_done(struct iio_trigger *trig)
207 {
208         trig->use_count--;
209         if (trig->use_count == 0 && trig->ops && trig->ops->try_reenable)
210                 if (trig->ops->try_reenable(trig)) {
211                         /* Missed and interrupt so launch new poll now */
212                         iio_trigger_poll(trig, 0);
213                 }
214 }
215 EXPORT_SYMBOL(iio_trigger_notify_done);
216
217 /* Trigger Consumer related functions */
218 static int iio_trigger_get_irq(struct iio_trigger *trig)
219 {
220         int ret;
221         mutex_lock(&trig->pool_lock);
222         ret = bitmap_find_free_region(trig->pool,
223                                       CONFIG_IIO_CONSUMERS_PER_TRIGGER,
224                                       ilog2(1));
225         mutex_unlock(&trig->pool_lock);
226         if (ret >= 0)
227                 ret += trig->subirq_base;
228
229         return ret;
230 }
231
232 static void iio_trigger_put_irq(struct iio_trigger *trig, int irq)
233 {
234         mutex_lock(&trig->pool_lock);
235         clear_bit(irq - trig->subirq_base, trig->pool);
236         mutex_unlock(&trig->pool_lock);
237 }
238
239 /* Complexity in here.  With certain triggers (datardy) an acknowledgement
240  * may be needed if the pollfuncs do not include the data read for the
241  * triggering device.
242  * This is not currently handled.  Alternative of not enabling trigger unless
243  * the relevant function is in there may be the best option.
244  */
245 /* Worth protecting against double additions?*/
246 static int iio_trigger_attach_poll_func(struct iio_trigger *trig,
247                                         struct iio_poll_func *pf)
248 {
249         int ret = 0;
250         bool notinuse
251                 = bitmap_empty(trig->pool, CONFIG_IIO_CONSUMERS_PER_TRIGGER);
252
253         /* Prevent the module being removed whilst attached to a trigger */
254         __module_get(pf->indio_dev->info->driver_module);
255         pf->irq = iio_trigger_get_irq(trig);
256         ret = request_threaded_irq(pf->irq, pf->h, pf->thread,
257                                    pf->type, pf->name,
258                                    pf);
259         if (trig->ops && trig->ops->set_trigger_state && notinuse)
260                 ret = trig->ops->set_trigger_state(trig, true);
261
262         return ret;
263 }
264
265 static int iio_trigger_dettach_poll_func(struct iio_trigger *trig,
266                                          struct iio_poll_func *pf)
267 {
268         int ret = 0;
269         bool no_other_users
270                 = (bitmap_weight(trig->pool,
271                                  CONFIG_IIO_CONSUMERS_PER_TRIGGER)
272                    == 1);
273         if (trig->ops && trig->ops->set_trigger_state && no_other_users) {
274                 ret = trig->ops->set_trigger_state(trig, false);
275                 if (ret)
276                         goto error_ret;
277         }
278         iio_trigger_put_irq(trig, pf->irq);
279         free_irq(pf->irq, pf);
280         module_put(pf->indio_dev->info->driver_module);
281
282 error_ret:
283         return ret;
284 }
285
286 irqreturn_t iio_pollfunc_store_time(int irq, void *p)
287 {
288         struct iio_poll_func *pf = p;
289         pf->timestamp = iio_get_time_ns();
290         return IRQ_WAKE_THREAD;
291 }
292 EXPORT_SYMBOL(iio_pollfunc_store_time);
293
294 struct iio_poll_func
295 *iio_alloc_pollfunc(irqreturn_t (*h)(int irq, void *p),
296                     irqreturn_t (*thread)(int irq, void *p),
297                     int type,
298                     struct iio_dev *indio_dev,
299                     const char *fmt,
300                     ...)
301 {
302         va_list vargs;
303         struct iio_poll_func *pf;
304
305         pf = kmalloc(sizeof *pf, GFP_KERNEL);
306         if (pf == NULL)
307                 return NULL;
308         va_start(vargs, fmt);
309         pf->name = kvasprintf(GFP_KERNEL, fmt, vargs);
310         va_end(vargs);
311         if (pf->name == NULL) {
312                 kfree(pf);
313                 return NULL;
314         }
315         pf->h = h;
316         pf->thread = thread;
317         pf->type = type;
318         pf->indio_dev = indio_dev;
319
320         return pf;
321 }
322 EXPORT_SYMBOL_GPL(iio_alloc_pollfunc);
323
324 void iio_dealloc_pollfunc(struct iio_poll_func *pf)
325 {
326         kfree(pf->name);
327         kfree(pf);
328 }
329 EXPORT_SYMBOL_GPL(iio_dealloc_pollfunc);
330
331 /**
332  * iio_trigger_read_currrent() - trigger consumer sysfs query which trigger
333  *
334  * For trigger consumers the current_trigger interface allows the trigger
335  * used by the device to be queried.
336  **/
337 static ssize_t iio_trigger_read_current(struct device *dev,
338                                         struct device_attribute *attr,
339                                         char *buf)
340 {
341         struct iio_dev *dev_info = dev_get_drvdata(dev);
342
343         if (dev_info->trig)
344                 return sprintf(buf, "%s\n", dev_info->trig->name);
345         return 0;
346 }
347
348 /**
349  * iio_trigger_write_current() trigger consumer sysfs set current trigger
350  *
351  * For trigger consumers the current_trigger interface allows the trigger
352  * used for this device to be specified at run time based on the triggers
353  * name.
354  **/
355 static ssize_t iio_trigger_write_current(struct device *dev,
356                                          struct device_attribute *attr,
357                                          const char *buf,
358                                          size_t len)
359 {
360         struct iio_dev *dev_info = dev_get_drvdata(dev);
361         struct iio_trigger *oldtrig = dev_info->trig;
362         struct iio_trigger *trig;
363         int ret;
364
365         mutex_lock(&dev_info->mlock);
366         if (dev_info->currentmode == INDIO_RING_TRIGGERED) {
367                 mutex_unlock(&dev_info->mlock);
368                 return -EBUSY;
369         }
370         mutex_unlock(&dev_info->mlock);
371
372         trig = iio_trigger_find_by_name(buf, len);
373
374         if (trig && dev_info->info->validate_trigger) {
375                 ret = dev_info->info->validate_trigger(dev_info, trig);
376                 if (ret)
377                         return ret;
378         }
379
380         if (trig && trig->ops && trig->ops->validate_device) {
381                 ret = trig->ops->validate_device(trig, dev_info);
382                 if (ret)
383                         return ret;
384         }
385
386         dev_info->trig = trig;
387
388         if (oldtrig && dev_info->trig != oldtrig)
389                 iio_put_trigger(oldtrig);
390         if (dev_info->trig)
391                 iio_get_trigger(dev_info->trig);
392
393         return len;
394 }
395
396 static DEVICE_ATTR(current_trigger, S_IRUGO | S_IWUSR,
397                    iio_trigger_read_current,
398                    iio_trigger_write_current);
399
400 static struct attribute *iio_trigger_consumer_attrs[] = {
401         &dev_attr_current_trigger.attr,
402         NULL,
403 };
404
405 static const struct attribute_group iio_trigger_consumer_attr_group = {
406         .name = "trigger",
407         .attrs = iio_trigger_consumer_attrs,
408 };
409
410 static void iio_trig_release(struct device *device)
411 {
412         struct iio_trigger *trig = to_iio_trigger(device);
413         int i;
414
415         if (trig->subirq_base) {
416                 for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
417                         irq_modify_status(trig->subirq_base + i,
418                                           IRQ_NOAUTOEN,
419                                           IRQ_NOREQUEST | IRQ_NOPROBE);
420                         irq_set_chip(trig->subirq_base + i,
421                                      NULL);
422                         irq_set_handler(trig->subirq_base + i,
423                                         NULL);
424                 }
425
426                 irq_free_descs(trig->subirq_base,
427                                CONFIG_IIO_CONSUMERS_PER_TRIGGER);
428         }
429         kfree(trig->name);
430         kfree(trig);
431         iio_put();
432 }
433
434 static struct device_type iio_trig_type = {
435         .release = iio_trig_release,
436 };
437
438 static void iio_trig_subirqmask(struct irq_data *d)
439 {
440         struct irq_chip *chip = irq_data_get_irq_chip(d);
441         struct iio_trigger *trig
442                 = container_of(chip,
443                                struct iio_trigger, subirq_chip);
444         trig->subirqs[d->irq - trig->subirq_base].enabled = false;
445 }
446
447 static void iio_trig_subirqunmask(struct irq_data *d)
448 {
449         struct irq_chip *chip = irq_data_get_irq_chip(d);
450         struct iio_trigger *trig
451                 = container_of(chip,
452                                struct iio_trigger, subirq_chip);
453         trig->subirqs[d->irq - trig->subirq_base].enabled = true;
454 }
455
456 struct iio_trigger *iio_allocate_trigger(const char *fmt, ...)
457 {
458         va_list vargs;
459         struct iio_trigger *trig;
460         trig = kzalloc(sizeof *trig, GFP_KERNEL);
461         if (trig) {
462                 int i;
463                 trig->dev.type = &iio_trig_type;
464                 trig->dev.bus = &iio_bus_type;
465                 device_initialize(&trig->dev);
466                 dev_set_drvdata(&trig->dev, (void *)trig);
467
468                 mutex_init(&trig->pool_lock);
469                 trig->subirq_base
470                         = irq_alloc_descs(-1, 0,
471                                           CONFIG_IIO_CONSUMERS_PER_TRIGGER,
472                                           0);
473                 if (trig->subirq_base < 0) {
474                         kfree(trig);
475                         return NULL;
476                 }
477                 va_start(vargs, fmt);
478                 trig->name = kvasprintf(GFP_KERNEL, fmt, vargs);
479                 va_end(vargs);
480                 if (trig->name == NULL) {
481                         irq_free_descs(trig->subirq_base,
482                                        CONFIG_IIO_CONSUMERS_PER_TRIGGER);
483                         kfree(trig);
484                         return NULL;
485                 }
486                 trig->subirq_chip.name = trig->name;
487                 trig->subirq_chip.irq_mask = &iio_trig_subirqmask;
488                 trig->subirq_chip.irq_unmask = &iio_trig_subirqunmask;
489                 for (i = 0; i < CONFIG_IIO_CONSUMERS_PER_TRIGGER; i++) {
490                         irq_set_chip(trig->subirq_base + i,
491                                      &trig->subirq_chip);
492                         irq_set_handler(trig->subirq_base + i,
493                                         &handle_simple_irq);
494                         irq_modify_status(trig->subirq_base + i,
495                                           IRQ_NOREQUEST | IRQ_NOAUTOEN,
496                                           IRQ_NOPROBE);
497                 }
498                 iio_get();
499                 get_device(&trig->dev);
500         }
501         return trig;
502 }
503 EXPORT_SYMBOL(iio_allocate_trigger);
504
505 void iio_free_trigger(struct iio_trigger *trig)
506 {
507         if (trig)
508                 put_device(&trig->dev);
509 }
510 EXPORT_SYMBOL(iio_free_trigger);
511
512 int iio_device_register_trigger_consumer(struct iio_dev *dev_info)
513 {
514         return sysfs_create_group(&dev_info->dev.kobj,
515                                   &iio_trigger_consumer_attr_group);
516 }
517
518 void iio_device_unregister_trigger_consumer(struct iio_dev *dev_info)
519 {
520         /* Clean up and associated but not attached triggers references */
521         if (dev_info->trig)
522                 iio_put_trigger(dev_info->trig);
523         sysfs_remove_group(&dev_info->dev.kobj,
524                            &iio_trigger_consumer_attr_group);
525 }
526
527 int iio_triggered_ring_postenable(struct iio_dev *indio_dev)
528 {
529         return indio_dev->trig
530                 ? iio_trigger_attach_poll_func(indio_dev->trig,
531                                                indio_dev->pollfunc)
532                 : 0;
533 }
534 EXPORT_SYMBOL(iio_triggered_ring_postenable);
535
536 int iio_triggered_ring_predisable(struct iio_dev *indio_dev)
537 {
538         return indio_dev->trig
539                 ? iio_trigger_dettach_poll_func(indio_dev->trig,
540                                                 indio_dev->pollfunc)
541                 : 0;
542 }
543 EXPORT_SYMBOL(iio_triggered_ring_predisable);