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/iio/configfs.h>
19 #include <linux/configfs.h>
20 
21 static struct config_group *iio_triggers_group;
22 static const struct config_item_type iio_trigger_type_group_type;
23 
24 static const struct config_item_type iio_triggers_group_type = {
25 	.ct_owner = THIS_MODULE,
26 };
27 
28 static LIST_HEAD(iio_trigger_types_list);
29 static DEFINE_MUTEX(iio_trigger_types_lock);
30 
31 static
__iio_find_sw_trigger_type(const char * name,unsigned len)32 struct iio_sw_trigger_type *__iio_find_sw_trigger_type(const char *name,
33 						       unsigned len)
34 {
35 	struct iio_sw_trigger_type *t = NULL, *iter;
36 
37 	list_for_each_entry(iter, &iio_trigger_types_list, list)
38 		if (!strcmp(iter->name, name)) {
39 			t = iter;
40 			break;
41 		}
42 
43 	return t;
44 }
45 
iio_register_sw_trigger_type(struct iio_sw_trigger_type * t)46 int iio_register_sw_trigger_type(struct iio_sw_trigger_type *t)
47 {
48 	struct iio_sw_trigger_type *iter;
49 	int ret = 0;
50 
51 	mutex_lock(&iio_trigger_types_lock);
52 	iter = __iio_find_sw_trigger_type(t->name, strlen(t->name));
53 	if (iter)
54 		ret = -EBUSY;
55 	else
56 		list_add_tail(&t->list, &iio_trigger_types_list);
57 	mutex_unlock(&iio_trigger_types_lock);
58 
59 	if (ret)
60 		return ret;
61 
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);
66 		list_del(&t->list);
67 		mutex_unlock(&iio_trigger_types_lock);
68 		ret = PTR_ERR(t->group);
69 	}
70 
71 	return ret;
72 }
73 EXPORT_SYMBOL(iio_register_sw_trigger_type);
74 
iio_unregister_sw_trigger_type(struct iio_sw_trigger_type * t)75 void iio_unregister_sw_trigger_type(struct iio_sw_trigger_type *t)
76 {
77 	struct iio_sw_trigger_type *iter;
78 
79 	mutex_lock(&iio_trigger_types_lock);
80 	iter = __iio_find_sw_trigger_type(t->name, strlen(t->name));
81 	if (iter)
82 		list_del(&t->list);
83 	mutex_unlock(&iio_trigger_types_lock);
84 
85 	configfs_unregister_default_group(t->group);
86 }
87 EXPORT_SYMBOL(iio_unregister_sw_trigger_type);
88 
89 static
iio_get_sw_trigger_type(const char * name)90 struct iio_sw_trigger_type *iio_get_sw_trigger_type(const char *name)
91 {
92 	struct iio_sw_trigger_type *t;
93 
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))
97 		t = NULL;
98 	mutex_unlock(&iio_trigger_types_lock);
99 
100 	return t;
101 }
102 
iio_sw_trigger_create(const char * type,const char * name)103 struct iio_sw_trigger *iio_sw_trigger_create(const char *type, const char *name)
104 {
105 	struct iio_sw_trigger *t;
106 	struct iio_sw_trigger_type *tt;
107 
108 	tt = iio_get_sw_trigger_type(type);
109 	if (!tt) {
110 		pr_err("Invalid trigger type: %s\n", type);
111 		return ERR_PTR(-EINVAL);
112 	}
113 	t = tt->ops->probe(name);
114 	if (IS_ERR(t))
115 		goto out_module_put;
116 
117 	t->trigger_type = tt;
118 
119 	return t;
120 out_module_put:
121 	module_put(tt->owner);
122 	return t;
123 }
124 EXPORT_SYMBOL(iio_sw_trigger_create);
125 
iio_sw_trigger_destroy(struct iio_sw_trigger * t)126 void iio_sw_trigger_destroy(struct iio_sw_trigger *t)
127 {
128 	struct iio_sw_trigger_type *tt = t->trigger_type;
129 
130 	tt->ops->remove(t);
131 	module_put(tt->owner);
132 }
133 EXPORT_SYMBOL(iio_sw_trigger_destroy);
134 
trigger_make_group(struct config_group * group,const char * name)135 static struct config_group *trigger_make_group(struct config_group *group,
136 					       const char *name)
137 {
138 	struct iio_sw_trigger *t;
139 
140 	t = iio_sw_trigger_create(group->cg_item.ci_name, name);
141 	if (IS_ERR(t))
142 		return ERR_CAST(t);
143 
144 	config_item_set_name(&t->group.cg_item, "%s", name);
145 
146 	return &t->group;
147 }
148 
trigger_drop_group(struct config_group * group,struct config_item * item)149 static void trigger_drop_group(struct config_group *group,
150 			       struct config_item *item)
151 {
152 	struct iio_sw_trigger *t = to_iio_sw_trigger(item);
153 
154 	iio_sw_trigger_destroy(t);
155 	config_item_put(item);
156 }
157 
158 static struct configfs_group_operations trigger_ops = {
159 	.make_group	= &trigger_make_group,
160 	.drop_item	= &trigger_drop_group,
161 };
162 
163 static const struct config_item_type iio_trigger_type_group_type = {
164 	.ct_group_ops = &trigger_ops,
165 	.ct_owner       = THIS_MODULE,
166 };
167 
iio_sw_trigger_init(void)168 static int __init iio_sw_trigger_init(void)
169 {
170 	iio_triggers_group =
171 		configfs_register_default_group(&iio_configfs_subsys.su_group,
172 						"triggers",
173 						&iio_triggers_group_type);
174 	return PTR_ERR_OR_ZERO(iio_triggers_group);
175 }
176 module_init(iio_sw_trigger_init);
177 
iio_sw_trigger_exit(void)178 static void __exit iio_sw_trigger_exit(void)
179 {
180 	configfs_unregister_default_group(iio_triggers_group);
181 }
182 module_exit(iio_sw_trigger_exit);
183 
184 MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>");
185 MODULE_DESCRIPTION("Industrial I/O software triggers support");
186 MODULE_LICENSE("GPL v2");
187