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