Skip to content

Commit b662f80

Browse files
Daniel Balutajic23
authored andcommitted
iio: core: Introduce IIO software triggers
A software trigger associates an IIO device trigger with a software interrupt source (e.g: timer, sysfs). This patch adds the generic infrastructure for handling software triggers. Software interrupts sources are kept in a iio_trigger_types_list and registered separately when the associated kernel module is loaded. Software triggers can be created directly from drivers or from user space via configfs interface. To sum up, this dynamically creates "triggers" group to be found under /config/iio/triggers and offers the possibility of dynamically creating trigger types groups. The first supported trigger type is "hrtimer" found under /config/iio/triggers/hrtimer. Signed-off-by: Daniel Baluta <daniel.baluta@intel.com> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
1 parent 8261d96 commit b662f80

4 files changed

Lines changed: 263 additions & 0 deletions

File tree

‎drivers/iio/Kconfig‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ config IIO_CONSUMERS_PER_TRIGGER
4646
This value controls the maximum number of consumers that a
4747
given trigger may handle. Default is 2.
4848

49+
config IIO_SW_TRIGGER
50+
tristate "Enable software triggers support"
51+
select IIO_CONFIGFS
52+
help
53+
Provides IIO core support for software triggers. A software
54+
trigger can be created via configfs or directly by a driver
55+
using the API provided.
56+
4957
config IIO_TRIGGERED_EVENT
5058
tristate
5159
select IIO_TRIGGER

‎drivers/iio/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ industrialio-$(CONFIG_IIO_BUFFER) += industrialio-buffer.o
88
industrialio-$(CONFIG_IIO_TRIGGER) += industrialio-trigger.o
99

1010
obj-$(CONFIG_IIO_CONFIGFS) += industrialio-configfs.o
11+
obj-$(CONFIG_IIO_SW_TRIGGER) += industrialio-sw-trigger.o
1112
obj-$(CONFIG_IIO_TRIGGERED_EVENT) += industrialio-triggered-event.o
1213

1314
obj-y += accel/
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
/*
2+
* The Industrial I/O core, software trigger functions
3+
*
4+
* Copyright (c) 2015 Intel Corporation
5+
*
6+
* This program is free software; you can redistribute it and/or modify it
7+
* under the terms of the GNU General Public License version 2 as published by
8+
* the Free Software Foundation.
9+
*/
10+
11+
#include <linux/module.h>
12+
#include <linux/init.h>
13+
#include <linux/kmod.h>
14+
#include <linux/list.h>
15+
#include <linux/slab.h>
16+
17+
#include <linux/iio/sw_trigger.h>
18+
#include <linux/configfs.h>
19+
20+
static struct config_group *iio_triggers_group;
21+
static struct config_item_type iio_trigger_type_group_type;
22+
23+
static struct config_item_type iio_triggers_group_type = {
24+
.ct_owner = THIS_MODULE,
25+
};
26+
27+
static LIST_HEAD(iio_trigger_types_list);
28+
static DEFINE_MUTEX(iio_trigger_types_lock);
29+
30+
static
31+
struct iio_sw_trigger_type *__iio_find_sw_trigger_type(const char *name,
32+
unsigned len)
33+
{
34+
struct iio_sw_trigger_type *t = NULL, *iter;
35+
36+
list_for_each_entry(iter, &iio_trigger_types_list, list)
37+
if (!strcmp(iter->name, name)) {
38+
t = iter;
39+
break;
40+
}
41+
42+
return t;
43+
}
44+
45+
int iio_register_sw_trigger_type(struct iio_sw_trigger_type *t)
46+
{
47+
struct iio_sw_trigger_type *iter;
48+
int ret = 0;
49+
50+
mutex_lock(&iio_trigger_types_lock);
51+
iter = __iio_find_sw_trigger_type(t->name, strlen(t->name));
52+
if (iter)
53+
ret = -EBUSY;
54+
else
55+
list_add_tail(&t->list, &iio_trigger_types_list);
56+
mutex_unlock(&iio_trigger_types_lock);
57+
58+
if (ret)
59+
return ret;
60+
61+
t->group = configfs_register_default_group(iio_triggers_group, t->name,
62+
&iio_trigger_type_group_type);
63+
if (IS_ERR(t->group))
64+
ret = PTR_ERR(t->group);
65+
66+
return ret;
67+
}
68+
EXPORT_SYMBOL(iio_register_sw_trigger_type);
69+
70+
void iio_unregister_sw_trigger_type(struct iio_sw_trigger_type *t)
71+
{
72+
struct iio_sw_trigger_type *iter;
73+
74+
mutex_lock(&iio_trigger_types_lock);
75+
iter = __iio_find_sw_trigger_type(t->name, strlen(t->name));
76+
if (iter)
77+
list_del(&t->list);
78+
mutex_unlock(&iio_trigger_types_lock);
79+
80+
configfs_unregister_default_group(t->group);
81+
}
82+
EXPORT_SYMBOL(iio_unregister_sw_trigger_type);
83+
84+
static
85+
struct iio_sw_trigger_type *iio_get_sw_trigger_type(const char *name)
86+
{
87+
struct iio_sw_trigger_type *t;
88+
89+
mutex_lock(&iio_trigger_types_lock);
90+
t = __iio_find_sw_trigger_type(name, strlen(name));
91+
if (t && !try_module_get(t->owner))
92+
t = NULL;
93+
mutex_unlock(&iio_trigger_types_lock);
94+
95+
return t;
96+
}
97+
98+
struct iio_sw_trigger *iio_sw_trigger_create(const char *type, const char *name)
99+
{
100+
struct iio_sw_trigger *t;
101+
struct iio_sw_trigger_type *tt;
102+
103+
tt = iio_get_sw_trigger_type(type);
104+
if (!tt) {
105+
pr_err("Invalid trigger type: %s\n", type);
106+
return ERR_PTR(-EINVAL);
107+
}
108+
t = tt->ops->probe(name);
109+
if (IS_ERR(t))
110+
goto out_module_put;
111+
112+
t->trigger_type = tt;
113+
114+
return t;
115+
out_module_put:
116+
module_put(tt->owner);
117+
return t;
118+
}
119+
EXPORT_SYMBOL(iio_sw_trigger_create);
120+
121+
void iio_sw_trigger_destroy(struct iio_sw_trigger *t)
122+
{
123+
struct iio_sw_trigger_type *tt = t->trigger_type;
124+
125+
tt->ops->remove(t);
126+
module_put(tt->owner);
127+
}
128+
EXPORT_SYMBOL(iio_sw_trigger_destroy);
129+
130+
static struct config_group *trigger_make_group(struct config_group *group,
131+
const char *name)
132+
{
133+
struct iio_sw_trigger *t;
134+
135+
t = iio_sw_trigger_create(group->cg_item.ci_name, name);
136+
if (IS_ERR(t))
137+
return ERR_CAST(t);
138+
139+
config_item_set_name(&t->group.cg_item, "%s", name);
140+
141+
return &t->group;
142+
}
143+
144+
static void trigger_drop_group(struct config_group *group,
145+
struct config_item *item)
146+
{
147+
struct iio_sw_trigger *t = to_iio_sw_trigger(item);
148+
149+
iio_sw_trigger_destroy(t);
150+
config_item_put(item);
151+
}
152+
153+
static struct configfs_group_operations trigger_ops = {
154+
.make_group = &trigger_make_group,
155+
.drop_item = &trigger_drop_group,
156+
};
157+
158+
static struct config_item_type iio_trigger_type_group_type = {
159+
.ct_group_ops = &trigger_ops,
160+
.ct_owner = THIS_MODULE,
161+
};
162+
163+
static int __init iio_sw_trigger_init(void)
164+
{
165+
iio_triggers_group =
166+
configfs_register_default_group(&iio_configfs_subsys.su_group,
167+
"triggers",
168+
&iio_triggers_group_type);
169+
if (IS_ERR(iio_triggers_group))
170+
return PTR_ERR(iio_triggers_group);
171+
return 0;
172+
}
173+
module_init(iio_sw_trigger_init);
174+
175+
static void __exit iio_sw_trigger_exit(void)
176+
{
177+
configfs_unregister_default_group(iio_triggers_group);
178+
}
179+
module_exit(iio_sw_trigger_exit);
180+
181+
MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>");
182+
MODULE_DESCRIPTION("Industrial I/O software triggers support");
183+
MODULE_LICENSE("GPL v2");

‎include/linux/iio/sw_trigger.h‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Industrial I/O software trigger interface
3+
*
4+
* Copyright (c) 2015 Intel Corporation
5+
*
6+
* This program is free software; you can redistribute it and/or modify it
7+
* under the terms of the GNU General Public License version 2 as published by
8+
* the Free Software Foundation.
9+
*/
10+
11+
#ifndef __IIO_SW_TRIGGER
12+
#define __IIO_SW_TRIGGER
13+
14+
#include <linux/module.h>
15+
#include <linux/device.h>
16+
#include <linux/iio/iio.h>
17+
#include <linux/configfs.h>
18+
19+
#define module_iio_sw_trigger_driver(__iio_sw_trigger_type) \
20+
module_driver(__iio_sw_trigger_type, iio_register_sw_trigger_type, \
21+
iio_unregister_sw_trigger_type)
22+
23+
extern struct configfs_subsystem iio_configfs_subsys;
24+
struct iio_sw_trigger_ops;
25+
26+
struct iio_sw_trigger_type {
27+
const char *name;
28+
struct module *owner;
29+
const struct iio_sw_trigger_ops *ops;
30+
struct list_head list;
31+
struct config_group *group;
32+
};
33+
34+
struct iio_sw_trigger {
35+
struct iio_trigger *trigger;
36+
struct iio_sw_trigger_type *trigger_type;
37+
struct config_group group;
38+
};
39+
40+
struct iio_sw_trigger_ops {
41+
struct iio_sw_trigger* (*probe)(const char *);
42+
int (*remove)(struct iio_sw_trigger *);
43+
};
44+
45+
static inline
46+
struct iio_sw_trigger *to_iio_sw_trigger(struct config_item *item)
47+
{
48+
return container_of(to_config_group(item), struct iio_sw_trigger,
49+
group);
50+
}
51+
52+
int iio_register_sw_trigger_type(struct iio_sw_trigger_type *tt);
53+
void iio_unregister_sw_trigger_type(struct iio_sw_trigger_type *tt);
54+
55+
struct iio_sw_trigger *iio_sw_trigger_create(const char *, const char *);
56+
void iio_sw_trigger_destroy(struct iio_sw_trigger *);
57+
58+
int iio_sw_trigger_type_configfs_register(struct iio_sw_trigger_type *tt);
59+
void iio_sw_trigger_type_configfs_unregister(struct iio_sw_trigger_type *tt);
60+
61+
static inline
62+
void iio_swt_group_init_type_name(struct iio_sw_trigger *t,
63+
const char *name,
64+
struct config_item_type *type)
65+
{
66+
#ifdef CONFIG_CONFIGFS_FS
67+
config_group_init_type_name(&t->group, name, type);
68+
#endif
69+
}
70+
71+
#endif /* __IIO_SW_TRIGGER */

0 commit comments

Comments
 (0)