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.
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>
21 /* RFC - Question of approach
22 * Make the common case (single sensor single trigger)
23 * simple by starting trigger capture from when first sensors
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
);
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
46 static int iio_trigger_register_sysfs(struct iio_trigger
*trig_info
)
50 if (trig_info
->control_attrs
)
51 ret
= sysfs_create_group(&trig_info
->dev
.kobj
,
52 trig_info
->control_attrs
);
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
);
66 * iio_trigger_register_id() - get a unique id for this trigger
67 * @trig_info: the trigger
69 static int iio_trigger_register_id(struct iio_trigger
*trig_info
)
74 if (unlikely(idr_pre_get(&iio_trigger_idr
, GFP_KERNEL
) == 0))
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
))
82 else if (likely(!ret
))
83 trig_info
->id
= trig_info
->id
& MAX_ID_MASK
;
89 * iio_trigger_unregister_id() - free up unique id for use by another trigger
90 * @trig_info: the trigger
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
)
103 ret
= iio_trigger_register_id(trig_info
);
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
);
112 goto error_unregister_id
;
114 ret
= iio_trigger_register_sysfs(trig_info
);
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
);
126 device_del(&trig_info
->dev
);
128 iio_trigger_unregister_id(trig_info
);
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
);
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
;
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) {
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
);
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
);
188 EXPORT_SYMBOL(iio_trigger_poll
);
190 void iio_trigger_notify_done(struct iio_trigger
*trig
)
193 if (trig
->use_count
== 0 && trig
->try_reenable
)
194 if (trig
->try_reenable(trig
)) {
195 /* Missed and interrupt so launch new poll now */
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
,
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
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
)
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);
236 printk(KERN_ERR
"set trigger state failed\n");
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
;
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
) {
257 if (list_is_singular(&trig
->pollfunc_list
)
258 && trig
->set_trigger_state
) {
259 spin_unlock_irqrestore(&trig
->pollfunc_list_lock
,
261 /* May sleep hence cannot hold the spin lock */
262 ret
= trig
->set_trigger_state(trig
, false);
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
);
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
,
291 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
295 IIO_TRIGGER_NAME_LENGTH
,
297 dev_info
->trig
->name
);
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
308 static ssize_t
iio_trigger_write_current(struct device
*dev
,
309 struct device_attribute
*attr
,
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
);
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
);
328 iio_get_trigger(dev_info
->trig
);
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
,
342 static const struct attribute_group iio_trigger_consumer_attr_group
= {
344 .attrs
= iio_trigger_consumer_attrs
,
347 static void iio_trig_release(struct device
*device
)
349 struct iio_trigger
*trig
= to_iio_trigger(device
);
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
);
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
);
374 EXPORT_SYMBOL(iio_allocate_trigger
);
376 void iio_free_trigger(struct iio_trigger
*trig
)
379 put_device(&trig
->dev
);
381 EXPORT_SYMBOL(iio_free_trigger
);
383 int iio_device_register_trigger_consumer(struct iio_dev
*dev_info
)
386 ret
= sysfs_create_group(&dev_info
->dev
.kobj
,
387 &iio_trigger_consumer_attr_group
);
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
);
398 EXPORT_SYMBOL(iio_device_unregister_trigger_consumer
);