1 // SPDX-License-Identifier: GPL-2.0-only
3 * The Industrial I/O core, software trigger functions
5 * Copyright (c) 2015 Intel Corporation
8 #include <linux/module.h>
9 #include <linux/init.h>
10 #include <linux/kmod.h>
11 #include <linux/list.h>
12 #include <linux/slab.h>
14 #include <linux/iio/sw_trigger.h>
15 #include <linux/iio/configfs.h>
16 #include <linux/configfs.h>
18 static struct config_group *iio_triggers_group;
19 static const struct config_item_type iio_trigger_type_group_type;
21 static const struct config_item_type iio_triggers_group_type = {
22 .ct_owner = THIS_MODULE,
25 static LIST_HEAD(iio_trigger_types_list);
26 static DEFINE_MUTEX(iio_trigger_types_lock);
29 struct iio_sw_trigger_type *__iio_find_sw_trigger_type(const char *name,
32 struct iio_sw_trigger_type *t = NULL, *iter;
34 list_for_each_entry(iter, &iio_trigger_types_list, list)
35 if (!strcmp(iter->name, name)) {
43 int iio_register_sw_trigger_type(struct iio_sw_trigger_type *t)
45 struct iio_sw_trigger_type *iter;
48 mutex_lock(&iio_trigger_types_lock);
49 iter = __iio_find_sw_trigger_type(t->name, strlen(t->name));
53 list_add_tail(&t->list, &iio_trigger_types_list);
54 mutex_unlock(&iio_trigger_types_lock);
59 t->group = configfs_register_default_group(iio_triggers_group, t->name,
60 &iio_trigger_type_group_type);
61 if (IS_ERR(t->group)) {
62 mutex_lock(&iio_trigger_types_lock);
64 mutex_unlock(&iio_trigger_types_lock);
65 ret = PTR_ERR(t->group);
70 EXPORT_SYMBOL(iio_register_sw_trigger_type);
72 void iio_unregister_sw_trigger_type(struct iio_sw_trigger_type *t)
74 struct iio_sw_trigger_type *iter;
76 mutex_lock(&iio_trigger_types_lock);
77 iter = __iio_find_sw_trigger_type(t->name, strlen(t->name));
80 mutex_unlock(&iio_trigger_types_lock);
82 configfs_unregister_default_group(t->group);
84 EXPORT_SYMBOL(iio_unregister_sw_trigger_type);
87 struct iio_sw_trigger_type *iio_get_sw_trigger_type(const char *name)
89 struct iio_sw_trigger_type *t;
91 mutex_lock(&iio_trigger_types_lock);
92 t = __iio_find_sw_trigger_type(name, strlen(name));
93 if (t && !try_module_get(t->owner))
95 mutex_unlock(&iio_trigger_types_lock);
100 struct iio_sw_trigger *iio_sw_trigger_create(const char *type, const char *name)
102 struct iio_sw_trigger *t;
103 struct iio_sw_trigger_type *tt;
105 tt = iio_get_sw_trigger_type(type);
107 pr_err("Invalid trigger type: %s\n", type);
108 return ERR_PTR(-EINVAL);
110 t = tt->ops->probe(name);
114 t->trigger_type = tt;
118 module_put(tt->owner);
121 EXPORT_SYMBOL(iio_sw_trigger_create);
123 void iio_sw_trigger_destroy(struct iio_sw_trigger *t)
125 struct iio_sw_trigger_type *tt = t->trigger_type;
128 module_put(tt->owner);
130 EXPORT_SYMBOL(iio_sw_trigger_destroy);
132 static struct config_group *trigger_make_group(struct config_group *group,
135 struct iio_sw_trigger *t;
137 t = iio_sw_trigger_create(group->cg_item.ci_name, name);
141 config_item_set_name(&t->group.cg_item, "%s", name);
146 static void trigger_drop_group(struct config_group *group,
147 struct config_item *item)
149 struct iio_sw_trigger *t = to_iio_sw_trigger(item);
151 iio_sw_trigger_destroy(t);
152 config_item_put(item);
155 static struct configfs_group_operations trigger_ops = {
156 .make_group = &trigger_make_group,
157 .drop_item = &trigger_drop_group,
160 static const struct config_item_type iio_trigger_type_group_type = {
161 .ct_group_ops = &trigger_ops,
162 .ct_owner = THIS_MODULE,
165 static int __init iio_sw_trigger_init(void)
168 configfs_register_default_group(&iio_configfs_subsys.su_group,
170 &iio_triggers_group_type);
171 return PTR_ERR_OR_ZERO(iio_triggers_group);
173 module_init(iio_sw_trigger_init);
175 static void __exit iio_sw_trigger_exit(void)
177 configfs_unregister_default_group(iio_triggers_group);
179 module_exit(iio_sw_trigger_exit);
181 MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>");
182 MODULE_DESCRIPTION("Industrial I/O software triggers support");
183 MODULE_LICENSE("GPL v2");