2 * The Industrial I/O core, software trigger functions
4 * Copyright (c) 2015 Intel Corporation
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.
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>
17 #include <linux/iio/sw_trigger.h>
18 #include <linux/iio/configfs.h>
19 #include <linux/configfs.h>
21 static struct config_group *iio_triggers_group;
22 static const struct config_item_type iio_trigger_type_group_type;
24 static const struct config_item_type iio_triggers_group_type = {
25 .ct_owner = THIS_MODULE,
28 static LIST_HEAD(iio_trigger_types_list);
29 static DEFINE_MUTEX(iio_trigger_types_lock);
32 struct iio_sw_trigger_type *__iio_find_sw_trigger_type(const char *name,
35 struct iio_sw_trigger_type *t = NULL, *iter;
37 list_for_each_entry(iter, &iio_trigger_types_list, list)
38 if (!strcmp(iter->name, name)) {
46 int iio_register_sw_trigger_type(struct iio_sw_trigger_type *t)
48 struct iio_sw_trigger_type *iter;
51 mutex_lock(&iio_trigger_types_lock);
52 iter = __iio_find_sw_trigger_type(t->name, strlen(t->name));
56 list_add_tail(&t->list, &iio_trigger_types_list);
57 mutex_unlock(&iio_trigger_types_lock);
62 t->group = configfs_register_default_group(iio_triggers_group, t->name,
63 &iio_trigger_type_group_type);
64 if (IS_ERR(t->group)) {
65 mutex_lock(&iio_trigger_types_lock);
67 mutex_unlock(&iio_trigger_types_lock);
68 ret = PTR_ERR(t->group);
73 EXPORT_SYMBOL(iio_register_sw_trigger_type);
75 void iio_unregister_sw_trigger_type(struct iio_sw_trigger_type *t)
77 struct iio_sw_trigger_type *iter;
79 mutex_lock(&iio_trigger_types_lock);
80 iter = __iio_find_sw_trigger_type(t->name, strlen(t->name));
83 mutex_unlock(&iio_trigger_types_lock);
85 configfs_unregister_default_group(t->group);
87 EXPORT_SYMBOL(iio_unregister_sw_trigger_type);
90 struct iio_sw_trigger_type *iio_get_sw_trigger_type(const char *name)
92 struct iio_sw_trigger_type *t;
94 mutex_lock(&iio_trigger_types_lock);
95 t = __iio_find_sw_trigger_type(name, strlen(name));
96 if (t && !try_module_get(t->owner))
98 mutex_unlock(&iio_trigger_types_lock);
103 struct iio_sw_trigger *iio_sw_trigger_create(const char *type, const char *name)
105 struct iio_sw_trigger *t;
106 struct iio_sw_trigger_type *tt;
108 tt = iio_get_sw_trigger_type(type);
110 pr_err("Invalid trigger type: %s\n", type);
111 return ERR_PTR(-EINVAL);
113 t = tt->ops->probe(name);
117 t->trigger_type = tt;
121 module_put(tt->owner);
124 EXPORT_SYMBOL(iio_sw_trigger_create);
126 void iio_sw_trigger_destroy(struct iio_sw_trigger *t)
128 struct iio_sw_trigger_type *tt = t->trigger_type;
131 module_put(tt->owner);
133 EXPORT_SYMBOL(iio_sw_trigger_destroy);
135 static struct config_group *trigger_make_group(struct config_group *group,
138 struct iio_sw_trigger *t;
140 t = iio_sw_trigger_create(group->cg_item.ci_name, name);
144 config_item_set_name(&t->group.cg_item, "%s", name);
149 static void trigger_drop_group(struct config_group *group,
150 struct config_item *item)
152 struct iio_sw_trigger *t = to_iio_sw_trigger(item);
154 iio_sw_trigger_destroy(t);
155 config_item_put(item);
158 static struct configfs_group_operations trigger_ops = {
159 .make_group = &trigger_make_group,
160 .drop_item = &trigger_drop_group,
163 static const struct config_item_type iio_trigger_type_group_type = {
164 .ct_group_ops = &trigger_ops,
165 .ct_owner = THIS_MODULE,
168 static int __init iio_sw_trigger_init(void)
171 configfs_register_default_group(&iio_configfs_subsys.su_group,
173 &iio_triggers_group_type);
174 return PTR_ERR_OR_ZERO(iio_triggers_group);
176 module_init(iio_sw_trigger_init);
178 static void __exit iio_sw_trigger_exit(void)
180 configfs_unregister_default_group(iio_triggers_group);
182 module_exit(iio_sw_trigger_exit);
184 MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>");
185 MODULE_DESCRIPTION("Industrial I/O software triggers support");
186 MODULE_LICENSE("GPL v2");