Merge commit 'v2.6.32-rc5' into for-linus
[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
18 #include "iio.h"
19 #include "trigger.h"
20
21 /* RFC - Question of approach
22  * Make the common case (single sensor single trigger)
23  * simple by starting trigger capture from when first sensors
24  * is added.
25  *
26  * Complex simultaneous start requires use of 'hold' functionality
27  * of the trigger. (not implemented)
28  *
29  * Any other suggestions?
30  */
31
32
33 static DEFINE_IDR(iio_trigger_idr);
34 static DEFINE_SPINLOCK(iio_trigger_idr_lock);
35
36 /* Single list of all available triggers */
37 static LIST_HEAD(iio_trigger_list);
38 static DEFINE_MUTEX(iio_trigger_list_lock);
39
40 /**
41  * iio_trigger_register_sysfs() - create a device for this trigger
42  * @trig_info:  the trigger
43  *
44  * Also adds any control attribute registered by the trigger driver
45  **/
46 static int iio_trigger_register_sysfs(struct iio_trigger *trig_info)
47 {
48         int ret = 0;
49
50         if (trig_info->control_attrs)
51                 ret = sysfs_create_group(&trig_info->dev.kobj,
52                                          trig_info->control_attrs);
53
54         return ret;
55 }
56
57 static void iio_trigger_unregister_sysfs(struct iio_trigger *trig_info)
58 {
59         if (trig_info->control_attrs)
60                 sysfs_remove_group(&trig_info->dev.kobj,
61                                    trig_info->control_attrs);
62 }
63
64
65 /**
66  * iio_trigger_register_id() - get a unique id for this trigger
67  * @trig_info:  the trigger
68  **/
69 static int iio_trigger_register_id(struct iio_trigger *trig_info)
70 {
71         int ret = 0;
72
73 idr_again:
74         if (unlikely(idr_pre_get(&iio_trigger_idr, GFP_KERNEL) == 0))
75                 return -ENOMEM;
76
77         spin_lock(&iio_trigger_idr_lock);
78         ret = idr_get_new(&iio_trigger_idr, NULL, &trig_info->id);
79         spin_unlock(&iio_trigger_idr_lock);
80         if (unlikely(ret == -EAGAIN))
81                 goto idr_again;
82         else if (likely(!ret))
83                 trig_info->id = trig_info->id & MAX_ID_MASK;
84
85         return ret;
86 }
87
88 /**
89  * iio_trigger_unregister_id() - free up unique id for use by another trigger
90  * @trig_info: the trigger
91  **/
92 static void iio_trigger_unregister_id(struct iio_trigger *trig_info)
93 {
94                 spin_lock(&iio_trigger_idr_lock);
95                 idr_remove(&iio_trigger_idr, trig_info->id);
96                 spin_unlock(&iio_trigger_idr_lock);
97 }
98
99 int iio_trigger_register(struct iio_trigger *trig_info)
100 {
101         int ret;
102
103         ret = iio_trigger_register_id(trig_info);
104         if (ret)
105                 goto error_ret;
106         /* Set the name used for the sysfs directory etc */
107         dev_set_name(&trig_info->dev, "trigger%ld",
108                      (unsigned long) trig_info->id);
109
110         ret = device_add(&trig_info->dev);
111         if (ret)
112                 goto error_unregister_id;
113
114         ret = iio_trigger_register_sysfs(trig_info);
115         if (ret)
116                 goto error_device_del;
117
118         /* Add to list of available triggers held by the IIO core */
119         mutex_lock(&iio_trigger_list_lock);
120         list_add_tail(&trig_info->list, &iio_trigger_list);
121         mutex_unlock(&iio_trigger_list_lock);
122
123         return 0;
124
125 error_device_del:
126         device_del(&trig_info->dev);
127 error_unregister_id:
128         iio_trigger_unregister_id(trig_info);
129 error_ret:
130         return ret;
131 }
132 EXPORT_SYMBOL(iio_trigger_register);
133
134 void iio_trigger_unregister(struct iio_trigger *trig_info)
135 {
136         struct iio_trigger *cursor;
137
138         mutex_lock(&iio_trigger_list_lock);
139         list_for_each_entry(cursor, &iio_trigger_list, list)
140                 if (cursor == trig_info) {
141                         list_del(&cursor->list);
142                         break;
143                 }
144         mutex_unlock(&iio_trigger_list_lock);
145
146         iio_trigger_unregister_sysfs(trig_info);
147         iio_trigger_unregister_id(trig_info);
148         /* Possible issue in here */
149         device_unregister(&trig_info->dev);
150 }
151 EXPORT_SYMBOL(iio_trigger_unregister);
152
153 struct iio_trigger *iio_trigger_find_by_name(const char *name, size_t len)
154 {
155         struct iio_trigger *trig;
156         bool found = false;
157
158         mutex_lock(&iio_trigger_list_lock);
159         list_for_each_entry(trig, &iio_trigger_list, list) {
160                 if (strncmp(trig->name, name, len) == 0) {
161                         found = true;
162                         break;
163                 }
164         }
165         mutex_unlock(&iio_trigger_list_lock);
166
167         return found ? trig : NULL;
168 };
169 EXPORT_SYMBOL(iio_trigger_find_by_name);
170
171 void iio_trigger_poll(struct iio_trigger *trig)
172 {
173         struct iio_poll_func *pf_cursor;
174
175         list_for_each_entry(pf_cursor, &trig->pollfunc_list, list) {
176                 if (pf_cursor->poll_func_immediate) {
177                         pf_cursor->poll_func_immediate(pf_cursor->private_data);
178                         trig->use_count++;
179                 }
180         }
181         list_for_each_entry(pf_cursor, &trig->pollfunc_list, list) {
182                 if (pf_cursor->poll_func_main) {
183                         pf_cursor->poll_func_main(pf_cursor->private_data);
184                         trig->use_count++;
185                 }
186         }
187 }
188 EXPORT_SYMBOL(iio_trigger_poll);
189
190 void iio_trigger_notify_done(struct iio_trigger *trig)
191 {
192         trig->use_count--;
193         if (trig->use_count == 0 && trig->try_reenable)
194                 if (trig->try_reenable(trig)) {
195                         /* Missed and interrupt so launch new poll now */
196                         trig->timestamp = 0;
197                         iio_trigger_poll(trig);
198                 }
199 }
200 EXPORT_SYMBOL(iio_trigger_notify_done);
201
202 /**
203  * iio_trigger_read_name() - retrieve useful identifying name
204  **/
205 ssize_t iio_trigger_read_name(struct device *dev,
206                               struct device_attribute *attr,
207                               char *buf)
208 {
209         struct iio_trigger *trig = dev_get_drvdata(dev);
210         return sprintf(buf, "%s\n", trig->name);
211 }
212 EXPORT_SYMBOL(iio_trigger_read_name);
213
214 /* Trigger Consumer related functions */
215
216 /* Complexity in here.  With certain triggers (datardy) an acknowledgement
217  * may be needed if the pollfuncs do not include the data read for the
218  * triggering device.
219  * This is not currently handled.  Alternative of not enabling trigger unless
220  * the relevant function is in there may be the best option.
221  */
222 /* Worth protecting against double additions?*/
223 int iio_trigger_attach_poll_func(struct iio_trigger *trig,
224                                  struct iio_poll_func *pf)
225 {
226         int ret = 0;
227         unsigned long flags;
228
229         spin_lock_irqsave(&trig->pollfunc_list_lock, flags);
230         list_add_tail(&pf->list, &trig->pollfunc_list);
231         spin_unlock_irqrestore(&trig->pollfunc_list_lock, flags);
232
233         if (trig->set_trigger_state)
234                 ret = trig->set_trigger_state(trig, true);
235         if (ret) {
236                 printk(KERN_ERR "set trigger state failed\n");
237                 list_del(&pf->list);
238         }
239         return ret;
240 }
241 EXPORT_SYMBOL(iio_trigger_attach_poll_func);
242
243 int iio_trigger_dettach_poll_func(struct iio_trigger *trig,
244                                   struct iio_poll_func *pf)
245 {
246         struct iio_poll_func *pf_cursor;
247         unsigned long flags;
248         int ret = -EINVAL;
249
250         spin_lock_irqsave(&trig->pollfunc_list_lock, flags);
251         list_for_each_entry(pf_cursor, &trig->pollfunc_list, list)
252                 if (pf_cursor == pf) {
253                         ret = 0;
254                         break;
255                 }
256         if (!ret) {
257                 if (list_is_singular(&trig->pollfunc_list)
258                     && trig->set_trigger_state) {
259                         spin_unlock_irqrestore(&trig->pollfunc_list_lock,
260                                                flags);
261                         /* May sleep hence cannot hold the spin lock */
262                         ret = trig->set_trigger_state(trig, false);
263                         if (ret)
264                                 goto error_ret;
265                         spin_lock_irqsave(&trig->pollfunc_list_lock, flags);
266                 }
267                 /*
268                  * Now we can delete safe in the knowledge that, if this is
269                  * the last pollfunc then we have disabled the trigger anyway
270                  * and so nothing should be able to call the pollfunc.
271                  */
272                 list_del(&pf_cursor->list);
273         }
274         spin_unlock_irqrestore(&trig->pollfunc_list_lock, flags);
275
276 error_ret:
277         return ret;
278 }
279 EXPORT_SYMBOL(iio_trigger_dettach_poll_func);
280
281 /**
282  * iio_trigger_read_currrent() trigger consumer sysfs query which trigger
283  *
284  * For trigger consumers the current_trigger interface allows the trigger
285  * used by the device to be queried.
286  **/
287 static ssize_t iio_trigger_read_current(struct device *dev,
288                                         struct device_attribute *attr,
289                                         char *buf)
290 {
291         struct iio_dev *dev_info = dev_get_drvdata(dev);
292         int len = 0;
293         if (dev_info->trig)
294                 len = snprintf(buf,
295                                IIO_TRIGGER_NAME_LENGTH,
296                                "%s\n",
297                                dev_info->trig->name);
298         return len;
299 }
300
301 /**
302  * iio_trigger_write_current() trigger consumer sysfs set current trigger
303  *
304  * For trigger consumers the current_trigger interface allows the trigger
305  * used for this device to be specified at run time based on the triggers
306  * name.
307  **/
308 static ssize_t iio_trigger_write_current(struct device *dev,
309                                          struct device_attribute *attr,
310                                          const char *buf,
311                                          size_t len)
312 {
313         struct iio_dev *dev_info = dev_get_drvdata(dev);
314         struct iio_trigger *oldtrig = dev_info->trig;
315         mutex_lock(&dev_info->mlock);
316         if (dev_info->currentmode == INDIO_RING_TRIGGERED) {
317                 mutex_unlock(&dev_info->mlock);
318                 return -EBUSY;
319         }
320         mutex_unlock(&dev_info->mlock);
321
322         len = len < IIO_TRIGGER_NAME_LENGTH ? len : IIO_TRIGGER_NAME_LENGTH;
323
324         dev_info->trig = iio_trigger_find_by_name(buf, len);
325         if (oldtrig && dev_info->trig != oldtrig)
326                 iio_put_trigger(oldtrig);
327         if (dev_info->trig)
328                 iio_get_trigger(dev_info->trig);
329
330         return len;
331 }
332
333 DEVICE_ATTR(current_trigger, S_IRUGO | S_IWUSR,
334             iio_trigger_read_current,
335             iio_trigger_write_current);
336
337 static struct attribute *iio_trigger_consumer_attrs[] = {
338         &dev_attr_current_trigger.attr,
339         NULL,
340 };
341
342 static const struct attribute_group iio_trigger_consumer_attr_group = {
343         .name = "trigger",
344         .attrs = iio_trigger_consumer_attrs,
345 };
346
347 static void iio_trig_release(struct device *device)
348 {
349         struct iio_trigger *trig = to_iio_trigger(device);
350         kfree(trig);
351         iio_put();
352 }
353
354 static struct device_type iio_trig_type = {
355         .release = iio_trig_release,
356 };
357
358 struct iio_trigger *iio_allocate_trigger(void)
359 {
360         struct iio_trigger *trig;
361         trig = kzalloc(sizeof *trig, GFP_KERNEL);
362         if (trig) {
363                 trig->dev.type = &iio_trig_type;
364                 trig->dev.class = &iio_class;
365                 device_initialize(&trig->dev);
366                 dev_set_drvdata(&trig->dev, (void *)trig);
367                 spin_lock_init(&trig->pollfunc_list_lock);
368                 INIT_LIST_HEAD(&trig->list);
369                 INIT_LIST_HEAD(&trig->pollfunc_list);
370                 iio_get();
371         }
372         return trig;
373 }
374 EXPORT_SYMBOL(iio_allocate_trigger);
375
376 void iio_free_trigger(struct iio_trigger *trig)
377 {
378         if (trig)
379                 put_device(&trig->dev);
380 }
381 EXPORT_SYMBOL(iio_free_trigger);
382
383 int iio_device_register_trigger_consumer(struct iio_dev *dev_info)
384 {
385         int ret;
386         ret = sysfs_create_group(&dev_info->dev.kobj,
387                                  &iio_trigger_consumer_attr_group);
388         return ret;
389 }
390 EXPORT_SYMBOL(iio_device_register_trigger_consumer);
391
392 int iio_device_unregister_trigger_consumer(struct iio_dev *dev_info)
393 {
394         sysfs_remove_group(&dev_info->dev.kobj,
395                            &iio_trigger_consumer_attr_group);
396         return 0;
397 }
398 EXPORT_SYMBOL(iio_device_unregister_trigger_consumer);
399