Commit | Line | Data |
---|---|---|
9e567af4 | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
960366cf KK |
2 | /* |
3 | * dsp_pipeline.c: pipelined audio processing | |
4 | * | |
5 | * Copyright (C) 2007, Nadi Sarrar | |
6 | * | |
7 | * Nadi Sarrar <nadi@beronet.com> | |
960366cf KK |
8 | */ |
9 | ||
10 | #include <linux/kernel.h> | |
5a0e3ad6 | 11 | #include <linux/slab.h> |
960366cf KK |
12 | #include <linux/list.h> |
13 | #include <linux/string.h> | |
14 | #include <linux/mISDNif.h> | |
15 | #include <linux/mISDNdsp.h> | |
5d76fc21 | 16 | #include <linux/export.h> |
960366cf KK |
17 | #include "dsp.h" |
18 | #include "dsp_hwec.h" | |
19 | ||
960366cf KK |
20 | struct dsp_pipeline_entry { |
21 | struct mISDN_dsp_element *elem; | |
22 | void *p; | |
23 | struct list_head list; | |
24 | }; | |
25 | struct dsp_element_entry { | |
26 | struct mISDN_dsp_element *elem; | |
27 | struct device dev; | |
28 | struct list_head list; | |
29 | }; | |
30 | ||
31 | static LIST_HEAD(dsp_elements); | |
32 | ||
33 | /* sysfs */ | |
34 | static struct class *elements_class; | |
35 | ||
36 | static ssize_t | |
37 | attr_show_args(struct device *dev, struct device_attribute *attr, char *buf) | |
38 | { | |
39 | struct mISDN_dsp_element *elem = dev_get_drvdata(dev); | |
1ce1513f KK |
40 | int i; |
41 | char *p = buf; | |
960366cf KK |
42 | |
43 | *buf = 0; | |
1ce1513f KK |
44 | for (i = 0; i < elem->num_args; i++) |
45 | p += sprintf(p, "Name: %s\n%s%s%sDescription: %s\n\n", | |
475be4d8 JP |
46 | elem->args[i].name, |
47 | elem->args[i].def ? "Default: " : "", | |
48 | elem->args[i].def ? elem->args[i].def : "", | |
49 | elem->args[i].def ? "\n" : "", | |
50 | elem->args[i].desc); | |
960366cf | 51 | |
1ce1513f | 52 | return p - buf; |
960366cf KK |
53 | } |
54 | ||
55 | static struct device_attribute element_attributes[] = { | |
56 | __ATTR(args, 0444, attr_show_args, NULL), | |
57 | }; | |
58 | ||
808a14a1 AE |
59 | static void |
60 | mISDN_dsp_dev_release(struct device *dev) | |
61 | { | |
62 | struct dsp_element_entry *entry = | |
63 | container_of(dev, struct dsp_element_entry, dev); | |
64 | list_del(&entry->list); | |
65 | kfree(entry); | |
66 | } | |
67 | ||
960366cf KK |
68 | int mISDN_dsp_element_register(struct mISDN_dsp_element *elem) |
69 | { | |
70 | struct dsp_element_entry *entry; | |
71 | int ret, i; | |
72 | ||
73 | if (!elem) | |
74 | return -EINVAL; | |
75 | ||
400fd978 | 76 | entry = kzalloc(sizeof(struct dsp_element_entry), GFP_ATOMIC); |
960366cf KK |
77 | if (!entry) |
78 | return -ENOMEM; | |
79 | ||
98a2ac1c | 80 | INIT_LIST_HEAD(&entry->list); |
960366cf KK |
81 | entry->elem = elem; |
82 | ||
83 | entry->dev.class = elements_class; | |
808a14a1 | 84 | entry->dev.release = mISDN_dsp_dev_release; |
960366cf | 85 | dev_set_drvdata(&entry->dev, elem); |
02aa2a37 | 86 | dev_set_name(&entry->dev, "%s", elem->name); |
960366cf KK |
87 | ret = device_register(&entry->dev); |
88 | if (ret) { | |
89 | printk(KERN_ERR "%s: failed to register %s\n", | |
475be4d8 | 90 | __func__, elem->name); |
960366cf KK |
91 | goto err1; |
92 | } | |
808a14a1 | 93 | list_add_tail(&entry->list, &dsp_elements); |
960366cf | 94 | |
21c150a6 | 95 | for (i = 0; i < ARRAY_SIZE(element_attributes); ++i) { |
960366cf | 96 | ret = device_create_file(&entry->dev, |
475be4d8 | 97 | &element_attributes[i]); |
960366cf KK |
98 | if (ret) { |
99 | printk(KERN_ERR "%s: failed to create device file\n", | |
475be4d8 | 100 | __func__); |
960366cf KK |
101 | goto err2; |
102 | } | |
21c150a6 | 103 | } |
960366cf | 104 | |
960366cf KK |
105 | return 0; |
106 | ||
107 | err2: | |
108 | device_unregister(&entry->dev); | |
808a14a1 | 109 | return ret; |
960366cf | 110 | err1: |
98a2ac1c | 111 | put_device(&entry->dev); |
960366cf KK |
112 | return ret; |
113 | } | |
114 | EXPORT_SYMBOL(mISDN_dsp_element_register); | |
115 | ||
116 | void mISDN_dsp_element_unregister(struct mISDN_dsp_element *elem) | |
117 | { | |
118 | struct dsp_element_entry *entry, *n; | |
119 | ||
120 | if (!elem) | |
121 | return; | |
122 | ||
123 | list_for_each_entry_safe(entry, n, &dsp_elements, list) | |
124 | if (entry->elem == elem) { | |
960366cf | 125 | device_unregister(&entry->dev); |
960366cf KK |
126 | return; |
127 | } | |
128 | printk(KERN_ERR "%s: element %s not in list.\n", __func__, elem->name); | |
129 | } | |
130 | EXPORT_SYMBOL(mISDN_dsp_element_unregister); | |
131 | ||
132 | int dsp_pipeline_module_init(void) | |
133 | { | |
1aaba11d | 134 | elements_class = class_create("dsp_pipeline"); |
960366cf KK |
135 | if (IS_ERR(elements_class)) |
136 | return PTR_ERR(elements_class); | |
137 | ||
960366cf KK |
138 | dsp_hwec_init(); |
139 | ||
140 | return 0; | |
141 | } | |
142 | ||
143 | void dsp_pipeline_module_exit(void) | |
144 | { | |
145 | struct dsp_element_entry *entry, *n; | |
146 | ||
147 | dsp_hwec_exit(); | |
148 | ||
149 | class_destroy(elements_class); | |
150 | ||
151 | list_for_each_entry_safe(entry, n, &dsp_elements, list) { | |
152 | list_del(&entry->list); | |
153 | printk(KERN_WARNING "%s: element was still registered: %s\n", | |
475be4d8 | 154 | __func__, entry->elem->name); |
960366cf KK |
155 | kfree(entry); |
156 | } | |
960366cf KK |
157 | } |
158 | ||
159 | int dsp_pipeline_init(struct dsp_pipeline *pipeline) | |
160 | { | |
161 | if (!pipeline) | |
162 | return -EINVAL; | |
163 | ||
164 | INIT_LIST_HEAD(&pipeline->list); | |
165 | ||
960366cf KK |
166 | return 0; |
167 | } | |
168 | ||
169 | static inline void _dsp_pipeline_destroy(struct dsp_pipeline *pipeline) | |
170 | { | |
171 | struct dsp_pipeline_entry *entry, *n; | |
172 | ||
173 | list_for_each_entry_safe(entry, n, &pipeline->list, list) { | |
174 | list_del(&entry->list); | |
175 | if (entry->elem == dsp_hwec) | |
176 | dsp_hwec_disable(container_of(pipeline, struct dsp, | |
475be4d8 | 177 | pipeline)); |
960366cf KK |
178 | else |
179 | entry->elem->free(entry->p); | |
180 | kfree(entry); | |
181 | } | |
182 | } | |
183 | ||
184 | void dsp_pipeline_destroy(struct dsp_pipeline *pipeline) | |
185 | { | |
186 | ||
187 | if (!pipeline) | |
188 | return; | |
189 | ||
190 | _dsp_pipeline_destroy(pipeline); | |
960366cf KK |
191 | } |
192 | ||
193 | int dsp_pipeline_build(struct dsp_pipeline *pipeline, const char *cfg) | |
194 | { | |
2682ea32 | 195 | int found = 0; |
c6a502c2 | 196 | char *dup, *next, *tok, *name, *args; |
960366cf KK |
197 | struct dsp_element_entry *entry, *n; |
198 | struct dsp_pipeline_entry *pipeline_entry; | |
199 | struct mISDN_dsp_element *elem; | |
200 | ||
201 | if (!pipeline) | |
202 | return -EINVAL; | |
203 | ||
204 | if (!list_empty(&pipeline->list)) | |
205 | _dsp_pipeline_destroy(pipeline); | |
206 | ||
c6a502c2 | 207 | dup = next = kstrdup(cfg, GFP_ATOMIC); |
960366cf KK |
208 | if (!dup) |
209 | return 0; | |
c6a502c2 | 210 | while ((tok = strsep(&next, "|"))) { |
960366cf KK |
211 | if (!strlen(tok)) |
212 | continue; | |
213 | name = strsep(&tok, "("); | |
214 | args = strsep(&tok, ")"); | |
215 | if (args && !*args) | |
bcf91745 | 216 | args = NULL; |
960366cf KK |
217 | |
218 | list_for_each_entry_safe(entry, n, &dsp_elements, list) | |
219 | if (!strcmp(entry->elem->name, name)) { | |
220 | elem = entry->elem; | |
221 | ||
222 | pipeline_entry = kmalloc(sizeof(struct | |
475be4d8 | 223 | dsp_pipeline_entry), GFP_ATOMIC); |
960366cf | 224 | if (!pipeline_entry) { |
808a14a1 | 225 | printk(KERN_ERR "%s: failed to add " |
475be4d8 JP |
226 | "entry to pipeline: %s (out of " |
227 | "memory)\n", __func__, elem->name); | |
960366cf KK |
228 | goto _out; |
229 | } | |
230 | pipeline_entry->elem = elem; | |
231 | ||
232 | if (elem == dsp_hwec) { | |
233 | /* This is a hack to make the hwec | |
234 | available as a pipeline module */ | |
235 | dsp_hwec_enable(container_of(pipeline, | |
475be4d8 | 236 | struct dsp, pipeline), args); |
960366cf | 237 | list_add_tail(&pipeline_entry->list, |
475be4d8 | 238 | &pipeline->list); |
960366cf KK |
239 | } else { |
240 | pipeline_entry->p = elem->new(args); | |
241 | if (pipeline_entry->p) { | |
242 | list_add_tail(&pipeline_entry-> | |
475be4d8 | 243 | list, &pipeline->list); |
960366cf | 244 | } else { |
808a14a1 | 245 | printk(KERN_ERR "%s: failed " |
475be4d8 JP |
246 | "to add entry to pipeline: " |
247 | "%s (new() returned NULL)\n", | |
248 | __func__, elem->name); | |
960366cf | 249 | kfree(pipeline_entry); |
960366cf KK |
250 | } |
251 | } | |
252 | found = 1; | |
253 | break; | |
254 | } | |
255 | ||
256 | if (found) | |
257 | found = 0; | |
2682ea32 | 258 | else |
808a14a1 | 259 | printk(KERN_ERR "%s: element not found, skipping: " |
475be4d8 | 260 | "%s\n", __func__, name); |
960366cf KK |
261 | } |
262 | ||
263 | _out: | |
264 | if (!list_empty(&pipeline->list)) | |
265 | pipeline->inuse = 1; | |
266 | else | |
267 | pipeline->inuse = 0; | |
268 | ||
960366cf KK |
269 | kfree(dup); |
270 | return 0; | |
271 | } | |
272 | ||
273 | void dsp_pipeline_process_tx(struct dsp_pipeline *pipeline, u8 *data, int len) | |
274 | { | |
275 | struct dsp_pipeline_entry *entry; | |
276 | ||
277 | if (!pipeline) | |
278 | return; | |
279 | ||
280 | list_for_each_entry(entry, &pipeline->list, list) | |
281 | if (entry->elem->process_tx) | |
282 | entry->elem->process_tx(entry->p, data, len); | |
283 | } | |
284 | ||
7cfa153d | 285 | void dsp_pipeline_process_rx(struct dsp_pipeline *pipeline, u8 *data, int len, |
475be4d8 | 286 | unsigned int txlen) |
960366cf KK |
287 | { |
288 | struct dsp_pipeline_entry *entry; | |
289 | ||
290 | if (!pipeline) | |
291 | return; | |
292 | ||
293 | list_for_each_entry_reverse(entry, &pipeline->list, list) | |
294 | if (entry->elem->process_rx) | |
7cfa153d | 295 | entry->elem->process_rx(entry->p, data, len, txlen); |
960366cf | 296 | } |