Merge branch 'devel-stable' into devel
[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/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>
17 #include <linux/slab.h>
18
19 #include "iio.h"
20 #include "trigger.h"
21
22 /* RFC - Question of approach
23  * Make the common case (single sensor single trigger)
24  * simple by starting trigger capture from when first sensors
25  * is added.
26  *
27  * Complex simultaneous start requires use of 'hold' functionality
28  * of the trigger. (not implemented)
29  *
30  * Any other suggestions?
31  */
32
33
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 {
95                 spin_lock(&iio_trigger_idr_lock);
96                 idr_remove(&iio_trigger_idr, trig_info->id);
97                 spin_unlock(&iio_trigger_idr_lock);
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 {
137         struct iio_trigger *cursor;
138
139         mutex_lock(&iio_trigger_list_lock);
140         list_for_each_entry(cursor, &iio_trigger_list, list)
141                 if (cursor == trig_info) {
142                         list_del(&cursor->list);
143                         break;
144                 }
145         mutex_unlock(&iio_trigger_list_lock);
146
147         iio_trigger_unregister_sysfs(trig_info);
148         iio_trigger_unregister_id(trig_info);
149         /* Possible issue in here */
150         device_unregister(&trig_info->dev);
151 }
152 EXPORT_SYMBOL(iio_trigger_unregister);
153
154 struct iio_trigger *iio_trigger_find_by_name(const char *name, size_t len)
155 {
156         struct iio_trigger *trig;
157         bool found = false;
158
159         mutex_lock(&iio_trigger_list_lock);
160         list_for_each_entry(trig, &iio_trigger_list, list) {
161                 if (strncmp(trig->name, name, len) == 0) {
162                         found = true;
163                         break;
164                 }
165         }
166         mutex_unlock(&iio_trigger_list_lock);
167
168         return found ? trig : NULL;
169 };
170 EXPORT_SYMBOL(iio_trigger_find_by_name);
171
172 void iio_trigger_poll(struct iio_trigger *trig)
173 {
174         struct iio_poll_func *pf_cursor;
175
176         list_for_each_entry(pf_cursor, &trig->pollfunc_list, list) {
177                 if (pf_cursor->poll_func_immediate) {
178                         pf_cursor->poll_func_immediate(pf_cursor->private_data);
179                         trig->use_count++;
180                 }
181         }
182         list_for_each_entry(pf_cursor, &trig->pollfunc_list, list) {
183                 if (pf_cursor->poll_func_main) {
184                         pf_cursor->poll_func_main(pf_cursor->private_data);
185                         trig->use_count++;
186                 }
187         }
188 }
189 EXPORT_SYMBOL(iio_trigger_poll);
190
191 void iio_trigger_notify_done(struct iio_trigger *trig)
192 {
193         trig->use_count--;
194         if (trig->use_count == 0 && trig->try_reenable)
195                 if (trig->try_reenable(trig)) {
196                         /* Missed and interrupt so launch new poll now */
197                         trig->timestamp = 0;
198                         iio_trigger_poll(trig);
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
230         spin_lock_irqsave(&trig->pollfunc_list_lock, flags);
231         list_add_tail(&pf->list, &trig->pollfunc_list);
232         spin_unlock_irqrestore(&trig->pollfunc_list_lock, flags);
233
234         if (trig->set_trigger_state)
235                 ret = trig->set_trigger_state(trig, true);
236         if (ret) {
237                 printk(KERN_ERR "set trigger state failed\n");
238                 list_del(&pf->list);
239         }
240         return ret;
241 }
242 EXPORT_SYMBOL(iio_trigger_attach_poll_func);
243
244 int iio_trigger_dettach_poll_func(struct iio_trigger *trig,
245                                   struct iio_poll_func *pf)
246 {
247         struct iio_poll_func *pf_cursor;
248         unsigned long flags;
249         int ret = -EINVAL;
250
251         spin_lock_irqsave(&trig->pollfunc_list_lock, flags);
252         list_for_each_entry(pf_cursor, &trig->pollfunc_list, list)
253                 if (pf_cursor == pf) {
254                         ret = 0;
255                         break;
256                 }
257         if (!ret) {
258                 if (list_is_singular(&trig->pollfunc_list)
259                     && trig->set_trigger_state) {
260                         spin_unlock_irqrestore(&trig->pollfunc_list_lock,
261                                                flags);
262                         /* May sleep hence cannot hold the spin lock */
263                         ret = trig->set_trigger_state(trig, false);
264                         if (ret)
265                                 goto error_ret;
266                         spin_lock_irqsave(&trig->pollfunc_list_lock, flags);
267                 }
268                 /*
269                  * Now we can delete safe in the knowledge that, if this is
270                  * the last pollfunc then we have disabled the trigger anyway
271                  * and so nothing should be able to call the pollfunc.
272                  */
273                 list_del(&pf_cursor->list);
274         }
275         spin_unlock_irqrestore(&trig->pollfunc_list_lock, flags);
276
277 error_ret:
278         return ret;
279 }
280 EXPORT_SYMBOL(iio_trigger_dettach_poll_func);
281
282 /**
283  * iio_trigger_read_currrent() trigger consumer sysfs query which trigger
284  *
285  * For trigger consumers the current_trigger interface allows the trigger
286  * used by the device to be queried.
287  **/
288 static ssize_t iio_trigger_read_current(struct device *dev,
289                                         struct device_attribute *attr,
290                                         char *buf)
291 {
292         struct iio_dev *dev_info = dev_get_drvdata(dev);
293         int len = 0;
294         if (dev_info->trig)
295                 len = snprintf(buf,
296                                IIO_TRIGGER_NAME_LENGTH,
297                                "%s\n",
298                                dev_info->trig->name);
299         return len;
300 }
301
302 /**
303  * iio_trigger_write_current() trigger consumer sysfs set current trigger
304  *
305  * For trigger consumers the current_trigger interface allows the trigger
306  * used for this device to be specified at run time based on the triggers
307  * name.
308  **/
309 static ssize_t iio_trigger_write_current(struct device *dev,
310                                          struct device_attribute *attr,
311                                          const char *buf,
312                                          size_t len)
313 {
314         struct iio_dev *dev_info = dev_get_drvdata(dev);
315         struct iio_trigger *oldtrig = dev_info->trig;
316         mutex_lock(&dev_info->mlock);
317         if (dev_info->currentmode == INDIO_RING_TRIGGERED) {
318                 mutex_unlock(&dev_info->mlock);
319                 return -EBUSY;
320         }
321         mutex_unlock(&dev_info->mlock);
322
323         len = len < IIO_TRIGGER_NAME_LENGTH ? len : IIO_TRIGGER_NAME_LENGTH;
324
325         dev_info->trig = iio_trigger_find_by_name(buf, len);
326         if (oldtrig && dev_info->trig != oldtrig)
327                 iio_put_trigger(oldtrig);
328         if (dev_info->trig)
329                 iio_get_trigger(dev_info->trig);
330
331         return len;
332 }
333
334 DEVICE_ATTR(current_trigger, S_IRUGO | S_IWUSR,
335             iio_trigger_read_current,
336             iio_trigger_write_current);
337
338 static struct attribute *iio_trigger_consumer_attrs[] = {
339         &dev_attr_current_trigger.attr,
340         NULL,
341 };
342
343 static const struct attribute_group iio_trigger_consumer_attr_group = {
344         .name = "trigger",
345         .attrs = iio_trigger_consumer_attrs,
346 };
347
348 static void iio_trig_release(struct device *device)
349 {
350         struct iio_trigger *trig = to_iio_trigger(device);
351         kfree(trig);
352         iio_put();
353 }
354
355 static struct device_type iio_trig_type = {
356         .release = iio_trig_release,
357 };
358
359 struct iio_trigger *iio_allocate_trigger(void)
360 {
361         struct iio_trigger *trig;
362         trig = kzalloc(sizeof *trig, GFP_KERNEL);
363         if (trig) {
364                 trig->dev.type = &iio_trig_type;
365                 trig->dev.class = &iio_class;
366                 device_initialize(&trig->dev);
367                 dev_set_drvdata(&trig->dev, (void *)trig);
368                 spin_lock_init(&trig->pollfunc_list_lock);
369                 INIT_LIST_HEAD(&trig->list);
370                 INIT_LIST_HEAD(&trig->pollfunc_list);
371                 iio_get();
372         }
373         return trig;
374 }
375 EXPORT_SYMBOL(iio_allocate_trigger);
376
377 void iio_free_trigger(struct iio_trigger *trig)
378 {
379         if (trig)
380                 put_device(&trig->dev);
381 }
382 EXPORT_SYMBOL(iio_free_trigger);
383
384 int iio_device_register_trigger_consumer(struct iio_dev *dev_info)
385 {
386         int ret;
387         ret = sysfs_create_group(&dev_info->dev.kobj,
388                                  &iio_trigger_consumer_attr_group);
389         return ret;
390 }
391 EXPORT_SYMBOL(iio_device_register_trigger_consumer);
392
393 int iio_device_unregister_trigger_consumer(struct iio_dev *dev_info)
394 {
395         sysfs_remove_group(&dev_info->dev.kobj,
396                            &iio_trigger_consumer_attr_group);
397         return 0;
398 }
399 EXPORT_SYMBOL(iio_device_unregister_trigger_consumer);
400