staging:iio: Code cleanups
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / iio / industrialio-trigger.c
blobef088a4ce5b7c76ab271fea60f5a1dfdb9aa0c9a
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>
17 #include <linux/slab.h>
19 #include "iio.h"
20 #include "trigger.h"
21 #include "trigger_consumer.h"
23 /* RFC - Question of approach
24 * Make the common case (single sensor single trigger)
25 * simple by starting trigger capture from when first sensors
26 * is added.
28 * Complex simultaneous start requires use of 'hold' functionality
29 * of the trigger. (not implemented)
31 * Any other suggestions?
34 static DEFINE_IDR(iio_trigger_idr);
35 static DEFINE_SPINLOCK(iio_trigger_idr_lock);
37 /* Single list of all available triggers */
38 static LIST_HEAD(iio_trigger_list);
39 static DEFINE_MUTEX(iio_trigger_list_lock);
41 /**
42 * iio_trigger_register_sysfs() - create a device for this trigger
43 * @trig_info: the trigger
45 * Also adds any control attribute registered by the trigger driver
46 **/
47 static int iio_trigger_register_sysfs(struct iio_trigger *trig_info)
49 int ret = 0;
51 if (trig_info->control_attrs)
52 ret = sysfs_create_group(&trig_info->dev.kobj,
53 trig_info->control_attrs);
55 return ret;
58 static void iio_trigger_unregister_sysfs(struct iio_trigger *trig_info)
60 if (trig_info->control_attrs)
61 sysfs_remove_group(&trig_info->dev.kobj,
62 trig_info->control_attrs);
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)
72 int ret = 0;
74 idr_again:
75 if (unlikely(idr_pre_get(&iio_trigger_idr, GFP_KERNEL) == 0))
76 return -ENOMEM;
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;
86 return ret;
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)
95 spin_lock(&iio_trigger_idr_lock);
96 idr_remove(&iio_trigger_idr, trig_info->id);
97 spin_unlock(&iio_trigger_idr_lock);
100 int iio_trigger_register(struct iio_trigger *trig_info)
102 int ret;
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);
111 ret = device_add(&trig_info->dev);
112 if (ret)
113 goto error_unregister_id;
115 ret = iio_trigger_register_sysfs(trig_info);
116 if (ret)
117 goto error_device_del;
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);
124 return 0;
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;
133 EXPORT_SYMBOL(iio_trigger_register);
135 void iio_trigger_unregister(struct iio_trigger *trig_info)
137 struct iio_trigger *cursor;
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;
145 mutex_unlock(&iio_trigger_list_lock);
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);
152 EXPORT_SYMBOL(iio_trigger_unregister);
154 struct iio_trigger *iio_trigger_find_by_name(const char *name, size_t len)
156 struct iio_trigger *trig;
157 bool found = false;
159 if (len && name[len - 1] == '\n')
160 len--;
162 mutex_lock(&iio_trigger_list_lock);
163 list_for_each_entry(trig, &iio_trigger_list, list) {
164 if (strncmp(trig->name, name, len) == 0) {
165 found = true;
166 break;
169 mutex_unlock(&iio_trigger_list_lock);
171 return found ? trig : NULL;
173 EXPORT_SYMBOL(iio_trigger_find_by_name);
175 void iio_trigger_poll(struct iio_trigger *trig)
177 struct iio_poll_func *pf_cursor;
179 list_for_each_entry(pf_cursor, &trig->pollfunc_list, list) {
180 if (pf_cursor->poll_func_immediate) {
181 pf_cursor->poll_func_immediate(pf_cursor->private_data);
182 trig->use_count++;
185 list_for_each_entry(pf_cursor, &trig->pollfunc_list, list) {
186 if (pf_cursor->poll_func_main) {
187 pf_cursor->poll_func_main(pf_cursor->private_data);
188 trig->use_count++;
192 EXPORT_SYMBOL(iio_trigger_poll);
194 void iio_trigger_notify_done(struct iio_trigger *trig)
196 trig->use_count--;
197 if (trig->use_count == 0 && trig->try_reenable)
198 if (trig->try_reenable(trig)) {
199 /* Missed and interrupt so launch new poll now */
200 trig->timestamp = 0;
201 iio_trigger_poll(trig);
204 EXPORT_SYMBOL(iio_trigger_notify_done);
207 * iio_trigger_read_name() - retrieve useful identifying name
209 ssize_t iio_trigger_read_name(struct device *dev,
210 struct device_attribute *attr,
211 char *buf)
213 struct iio_trigger *trig = dev_get_drvdata(dev);
214 return sprintf(buf, "%s\n", trig->name);
216 EXPORT_SYMBOL(iio_trigger_read_name);
218 /* Trigger Consumer related functions */
220 /* Complexity in here. With certain triggers (datardy) an acknowledgement
221 * may be needed if the pollfuncs do not include the data read for the
222 * triggering device.
223 * This is not currently handled. Alternative of not enabling trigger unless
224 * the relevant function is in there may be the best option.
226 /* Worth protecting against double additions?*/
227 int iio_trigger_attach_poll_func(struct iio_trigger *trig,
228 struct iio_poll_func *pf)
230 int ret = 0;
231 unsigned long flags;
233 spin_lock_irqsave(&trig->pollfunc_list_lock, flags);
234 list_add_tail(&pf->list, &trig->pollfunc_list);
235 spin_unlock_irqrestore(&trig->pollfunc_list_lock, flags);
237 if (trig->set_trigger_state)
238 ret = trig->set_trigger_state(trig, true);
239 if (ret) {
240 printk(KERN_ERR "set trigger state failed\n");
241 list_del(&pf->list);
243 return ret;
245 EXPORT_SYMBOL(iio_trigger_attach_poll_func);
247 int iio_trigger_dettach_poll_func(struct iio_trigger *trig,
248 struct iio_poll_func *pf)
250 struct iio_poll_func *pf_cursor;
251 unsigned long flags;
252 int ret = -EINVAL;
254 spin_lock_irqsave(&trig->pollfunc_list_lock, flags);
255 list_for_each_entry(pf_cursor, &trig->pollfunc_list, list)
256 if (pf_cursor == pf) {
257 ret = 0;
258 break;
260 if (!ret) {
261 if (list_is_singular(&trig->pollfunc_list)
262 && trig->set_trigger_state) {
263 spin_unlock_irqrestore(&trig->pollfunc_list_lock,
264 flags);
265 /* May sleep hence cannot hold the spin lock */
266 ret = trig->set_trigger_state(trig, false);
267 if (ret)
268 goto error_ret;
269 spin_lock_irqsave(&trig->pollfunc_list_lock, flags);
272 * Now we can delete safe in the knowledge that, if this is
273 * the last pollfunc then we have disabled the trigger anyway
274 * and so nothing should be able to call the pollfunc.
276 list_del(&pf_cursor->list);
278 spin_unlock_irqrestore(&trig->pollfunc_list_lock, flags);
280 error_ret:
281 return ret;
283 EXPORT_SYMBOL(iio_trigger_dettach_poll_func);
286 * iio_trigger_read_currrent() - trigger consumer sysfs query which trigger
288 * For trigger consumers the current_trigger interface allows the trigger
289 * used by the device to be queried.
291 static ssize_t iio_trigger_read_current(struct device *dev,
292 struct device_attribute *attr,
293 char *buf)
295 struct iio_dev *dev_info = dev_get_drvdata(dev);
296 int len = 0;
297 if (dev_info->trig)
298 len = snprintf(buf,
299 IIO_TRIGGER_NAME_LENGTH,
300 "%s\n",
301 dev_info->trig->name);
302 return len;
306 * iio_trigger_write_current() trigger consumer sysfs set current trigger
308 * For trigger consumers the current_trigger interface allows the trigger
309 * used for this device to be specified at run time based on the triggers
310 * name.
312 static ssize_t iio_trigger_write_current(struct device *dev,
313 struct device_attribute *attr,
314 const char *buf,
315 size_t len)
317 struct iio_dev *dev_info = dev_get_drvdata(dev);
318 struct iio_trigger *oldtrig = dev_info->trig;
319 mutex_lock(&dev_info->mlock);
320 if (dev_info->currentmode == INDIO_RING_TRIGGERED) {
321 mutex_unlock(&dev_info->mlock);
322 return -EBUSY;
324 mutex_unlock(&dev_info->mlock);
326 len = len < IIO_TRIGGER_NAME_LENGTH ? len : IIO_TRIGGER_NAME_LENGTH;
328 dev_info->trig = iio_trigger_find_by_name(buf, len);
329 if (oldtrig && dev_info->trig != oldtrig)
330 iio_put_trigger(oldtrig);
331 if (dev_info->trig)
332 iio_get_trigger(dev_info->trig);
334 return len;
337 static DEVICE_ATTR(current_trigger, S_IRUGO | S_IWUSR,
338 iio_trigger_read_current,
339 iio_trigger_write_current);
341 static struct attribute *iio_trigger_consumer_attrs[] = {
342 &dev_attr_current_trigger.attr,
343 NULL,
346 static const struct attribute_group iio_trigger_consumer_attr_group = {
347 .name = "trigger",
348 .attrs = iio_trigger_consumer_attrs,
351 static void iio_trig_release(struct device *device)
353 struct iio_trigger *trig = to_iio_trigger(device);
354 kfree(trig);
355 iio_put();
358 static struct device_type iio_trig_type = {
359 .release = iio_trig_release,
362 struct iio_trigger *iio_allocate_trigger(void)
364 struct iio_trigger *trig;
365 trig = kzalloc(sizeof *trig, GFP_KERNEL);
366 if (trig) {
367 trig->dev.type = &iio_trig_type;
368 trig->dev.bus = &iio_bus_type;
369 device_initialize(&trig->dev);
370 dev_set_drvdata(&trig->dev, (void *)trig);
371 spin_lock_init(&trig->pollfunc_list_lock);
372 INIT_LIST_HEAD(&trig->list);
373 INIT_LIST_HEAD(&trig->pollfunc_list);
374 iio_get();
376 return trig;
378 EXPORT_SYMBOL(iio_allocate_trigger);
380 void iio_free_trigger(struct iio_trigger *trig)
382 if (trig)
383 put_device(&trig->dev);
385 EXPORT_SYMBOL(iio_free_trigger);
387 int iio_device_register_trigger_consumer(struct iio_dev *dev_info)
389 int ret;
390 ret = sysfs_create_group(&dev_info->dev.kobj,
391 &iio_trigger_consumer_attr_group);
392 return ret;
394 EXPORT_SYMBOL(iio_device_register_trigger_consumer);
396 int iio_device_unregister_trigger_consumer(struct iio_dev *dev_info)
398 sysfs_remove_group(&dev_info->dev.kobj,
399 &iio_trigger_consumer_attr_group);
400 return 0;
402 EXPORT_SYMBOL(iio_device_unregister_trigger_consumer);