[ARM] pxa: update defconfig for Verdex Pro
[linux-2.6/verdex.git] / drivers / staging / iio / industrialio-trigger.c
blob693ebc48597cf1d7b4495c837a98e20de620109b
1 /* The industrial I/O core, trigger handling functions
3 * Copyright (c) 2008 Jonathan Cameron
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 */
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>
18 #include "iio.h"
19 #include "trigger.h"
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.
26 * Complex simultaneous start requires use of 'hold' functionality
27 * of the trigger. (not implemented)
29 * Any other suggestions?
33 static DEFINE_IDR(iio_trigger_idr);
34 static DEFINE_SPINLOCK(iio_trigger_idr_lock);
36 /* Single list of all available triggers */
37 static LIST_HEAD(iio_trigger_list);
38 static DEFINE_MUTEX(iio_trigger_list_lock);
40 /**
41 * iio_trigger_register_sysfs() - create a device for this trigger
42 * @trig_info: the trigger
44 * Also adds any control attribute registered by the trigger driver
45 **/
46 static int iio_trigger_register_sysfs(struct iio_trigger *trig_info)
48 int ret = 0;
50 if (trig_info->control_attrs)
51 ret = sysfs_create_group(&trig_info->dev.kobj,
52 trig_info->control_attrs);
54 return ret;
57 static void iio_trigger_unregister_sysfs(struct iio_trigger *trig_info)
59 if (trig_info->control_attrs)
60 sysfs_remove_group(&trig_info->dev.kobj,
61 trig_info->control_attrs);
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)
71 int ret = 0;
73 idr_again:
74 if (unlikely(idr_pre_get(&iio_trigger_idr, GFP_KERNEL) == 0))
75 return -ENOMEM;
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;
85 return ret;
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)
94 spin_lock(&iio_trigger_idr_lock);
95 idr_remove(&iio_trigger_idr, trig_info->id);
96 spin_unlock(&iio_trigger_idr_lock);
99 int iio_trigger_register(struct iio_trigger *trig_info)
101 int ret;
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);
110 ret = device_add(&trig_info->dev);
111 if (ret)
112 goto error_unregister_id;
114 ret = iio_trigger_register_sysfs(trig_info);
115 if (ret)
116 goto error_device_del;
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);
123 return 0;
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;
132 EXPORT_SYMBOL(iio_trigger_register);
134 void iio_trigger_unregister(struct iio_trigger *trig_info)
136 struct iio_trigger *cursor;
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;
144 mutex_unlock(&iio_trigger_list_lock);
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);
151 EXPORT_SYMBOL(iio_trigger_unregister);
153 struct iio_trigger *iio_trigger_find_by_name(const char *name, size_t len)
155 struct iio_trigger *trig;
156 bool found = false;
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;
165 mutex_unlock(&iio_trigger_list_lock);
167 return found ? trig : NULL;
169 EXPORT_SYMBOL(iio_trigger_find_by_name);
171 void iio_trigger_poll(struct iio_trigger *trig)
173 struct iio_poll_func *pf_cursor;
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++;
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++;
188 EXPORT_SYMBOL(iio_trigger_poll);
190 void iio_trigger_notify_done(struct iio_trigger *trig)
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);
200 EXPORT_SYMBOL(iio_trigger_notify_done);
203 * iio_trigger_read_name() - retrieve useful identifying name
205 ssize_t iio_trigger_read_name(struct device *dev,
206 struct device_attribute *attr,
207 char *buf)
209 struct iio_trigger *trig = dev_get_drvdata(dev);
210 return sprintf(buf, "%s\n", trig->name);
212 EXPORT_SYMBOL(iio_trigger_read_name);
214 /* Trigger Consumer related functions */
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.
222 /* Worth protecting against double additions?*/
223 int iio_trigger_attach_poll_func(struct iio_trigger *trig,
224 struct iio_poll_func *pf)
226 int ret = 0;
227 unsigned long flags;
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);
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);
239 return ret;
241 EXPORT_SYMBOL(iio_trigger_attach_poll_func);
243 int iio_trigger_dettach_poll_func(struct iio_trigger *trig,
244 struct iio_poll_func *pf)
246 struct iio_poll_func *pf_cursor;
247 unsigned long flags;
248 int ret = -EINVAL;
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;
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);
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.
272 list_del(&pf_cursor->list);
274 spin_unlock_irqrestore(&trig->pollfunc_list_lock, flags);
276 error_ret:
277 return ret;
279 EXPORT_SYMBOL(iio_trigger_dettach_poll_func);
282 * iio_trigger_read_currrent() trigger consumer sysfs query which trigger
284 * For trigger consumers the current_trigger interface allows the trigger
285 * used by the device to be queried.
287 static ssize_t iio_trigger_read_current(struct device *dev,
288 struct device_attribute *attr,
289 char *buf)
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;
302 * iio_trigger_write_current() trigger consumer sysfs set current trigger
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.
308 static ssize_t iio_trigger_write_current(struct device *dev,
309 struct device_attribute *attr,
310 const char *buf,
311 size_t len)
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;
320 mutex_unlock(&dev_info->mlock);
322 len = len < IIO_TRIGGER_NAME_LENGTH ? len : IIO_TRIGGER_NAME_LENGTH;
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);
330 return len;
333 DEVICE_ATTR(current_trigger, S_IRUGO | S_IWUSR,
334 iio_trigger_read_current,
335 iio_trigger_write_current);
337 static struct attribute *iio_trigger_consumer_attrs[] = {
338 &dev_attr_current_trigger.attr,
339 NULL,
342 static const struct attribute_group iio_trigger_consumer_attr_group = {
343 .name = "trigger",
344 .attrs = iio_trigger_consumer_attrs,
347 static void iio_trig_release(struct device *device)
349 struct iio_trigger *trig = to_iio_trigger(device);
350 kfree(trig);
351 iio_put();
354 static struct device_type iio_trig_type = {
355 .release = iio_trig_release,
358 struct iio_trigger *iio_allocate_trigger(void)
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();
372 return trig;
374 EXPORT_SYMBOL(iio_allocate_trigger);
376 void iio_free_trigger(struct iio_trigger *trig)
378 if (trig)
379 put_device(&trig->dev);
381 EXPORT_SYMBOL(iio_free_trigger);
383 int iio_device_register_trigger_consumer(struct iio_dev *dev_info)
385 int ret;
386 ret = sysfs_create_group(&dev_info->dev.kobj,
387 &iio_trigger_consumer_attr_group);
388 return ret;
390 EXPORT_SYMBOL(iio_device_register_trigger_consumer);
392 int iio_device_unregister_trigger_consumer(struct iio_dev *dev_info)
394 sysfs_remove_group(&dev_info->dev.kobj,
395 &iio_trigger_consumer_attr_group);
396 return 0;
398 EXPORT_SYMBOL(iio_device_unregister_trigger_consumer);