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