2 * dsp_pipeline.c: pipelined audio processing
4 * Copyright (C) 2007, Nadi Sarrar
6 * Nadi Sarrar <nadi@beronet.com>
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)
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
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.
22 * The full GNU General Public License is included in this distribution in the
23 * file called LICENSE.
27 #include <linux/kernel.h>
28 #include <linux/list.h>
29 #include <linux/string.h>
30 #include <linux/mISDNif.h>
31 #include <linux/mISDNdsp.h>
35 /* uncomment for debugging */
36 /*#define PIPELINE_DEBUG*/
38 struct dsp_pipeline_entry {
39 struct mISDN_dsp_element *elem;
41 struct list_head list;
43 struct dsp_element_entry {
44 struct mISDN_dsp_element *elem;
46 struct list_head list;
49 static LIST_HEAD(dsp_elements);
52 static struct class *elements_class;
55 attr_show_args(struct device *dev, struct device_attribute *attr, char *buf)
57 struct mISDN_dsp_element *elem = dev_get_drvdata(dev);
62 for (; i < elem->num_args; ++i)
63 len = sprintf(buf, "%sName: %s\n%s%s%sDescription: %s\n"
66 elem->args[i].def ? "Default: " : "",
67 elem->args[i].def ? elem->args[i].def : "",
68 elem->args[i].def ? "\n" : "",
74 static struct device_attribute element_attributes[] = {
75 __ATTR(args, 0444, attr_show_args, NULL),
79 mISDN_dsp_dev_release(struct device *dev)
81 struct dsp_element_entry *entry =
82 container_of(dev, struct dsp_element_entry, dev);
83 list_del(&entry->list);
87 int mISDN_dsp_element_register(struct mISDN_dsp_element *elem)
89 struct dsp_element_entry *entry;
95 entry = kzalloc(sizeof(struct dsp_element_entry), GFP_ATOMIC);
101 entry->dev.class = elements_class;
102 entry->dev.release = mISDN_dsp_dev_release;
103 dev_set_drvdata(&entry->dev, elem);
104 dev_set_name(&entry->dev, elem->name);
105 ret = device_register(&entry->dev);
107 printk(KERN_ERR "%s: failed to register %s\n",
108 __func__, elem->name);
111 list_add_tail(&entry->list, &dsp_elements);
113 for (i = 0; i < (sizeof(element_attributes)
114 / sizeof(struct device_attribute)); ++i)
115 ret = device_create_file(&entry->dev,
116 &element_attributes[i]);
118 printk(KERN_ERR "%s: failed to create device file\n",
123 #ifdef PIPELINE_DEBUG
124 printk(KERN_DEBUG "%s: %s registered\n", __func__, elem->name);
130 device_unregister(&entry->dev);
136 EXPORT_SYMBOL(mISDN_dsp_element_register);
138 void mISDN_dsp_element_unregister(struct mISDN_dsp_element *elem)
140 struct dsp_element_entry *entry, *n;
145 list_for_each_entry_safe(entry, n, &dsp_elements, list)
146 if (entry->elem == elem) {
147 device_unregister(&entry->dev);
148 #ifdef PIPELINE_DEBUG
149 printk(KERN_DEBUG "%s: %s unregistered\n",
150 __func__, elem->name);
154 printk(KERN_ERR "%s: element %s not in list.\n", __func__, elem->name);
156 EXPORT_SYMBOL(mISDN_dsp_element_unregister);
158 int dsp_pipeline_module_init(void)
160 elements_class = class_create(THIS_MODULE, "dsp_pipeline");
161 if (IS_ERR(elements_class))
162 return PTR_ERR(elements_class);
164 #ifdef PIPELINE_DEBUG
165 printk(KERN_DEBUG "%s: dsp pipeline module initialized\n", __func__);
173 void dsp_pipeline_module_exit(void)
175 struct dsp_element_entry *entry, *n;
179 class_destroy(elements_class);
181 list_for_each_entry_safe(entry, n, &dsp_elements, list) {
182 list_del(&entry->list);
183 printk(KERN_WARNING "%s: element was still registered: %s\n",
184 __func__, entry->elem->name);
188 #ifdef PIPELINE_DEBUG
189 printk(KERN_DEBUG "%s: dsp pipeline module exited\n", __func__);
193 int dsp_pipeline_init(struct dsp_pipeline *pipeline)
198 INIT_LIST_HEAD(&pipeline->list);
200 #ifdef PIPELINE_DEBUG
201 printk(KERN_DEBUG "%s: dsp pipeline ready\n", __func__);
207 static inline void _dsp_pipeline_destroy(struct dsp_pipeline *pipeline)
209 struct dsp_pipeline_entry *entry, *n;
211 list_for_each_entry_safe(entry, n, &pipeline->list, list) {
212 list_del(&entry->list);
213 if (entry->elem == dsp_hwec)
214 dsp_hwec_disable(container_of(pipeline, struct dsp,
217 entry->elem->free(entry->p);
222 void dsp_pipeline_destroy(struct dsp_pipeline *pipeline)
228 _dsp_pipeline_destroy(pipeline);
230 #ifdef PIPELINE_DEBUG
231 printk(KERN_DEBUG "%s: dsp pipeline destroyed\n", __func__);
235 int dsp_pipeline_build(struct dsp_pipeline *pipeline, const char *cfg)
237 int len, incomplete = 0, found = 0;
238 char *dup, *tok, *name, *args;
239 struct dsp_element_entry *entry, *n;
240 struct dsp_pipeline_entry *pipeline_entry;
241 struct mISDN_dsp_element *elem;
246 if (!list_empty(&pipeline->list))
247 _dsp_pipeline_destroy(pipeline);
256 dup = kmalloc(len + 1, GFP_ATOMIC);
260 while ((tok = strsep(&dup, "|"))) {
263 name = strsep(&tok, "(");
264 args = strsep(&tok, ")");
268 list_for_each_entry_safe(entry, n, &dsp_elements, list)
269 if (!strcmp(entry->elem->name, name)) {
272 pipeline_entry = kmalloc(sizeof(struct
273 dsp_pipeline_entry), GFP_ATOMIC);
274 if (!pipeline_entry) {
275 printk(KERN_ERR "%s: failed to add "
276 "entry to pipeline: %s (out of "
277 "memory)\n", __func__, elem->name);
281 pipeline_entry->elem = elem;
283 if (elem == dsp_hwec) {
284 /* This is a hack to make the hwec
285 available as a pipeline module */
286 dsp_hwec_enable(container_of(pipeline,
287 struct dsp, pipeline), args);
288 list_add_tail(&pipeline_entry->list,
291 pipeline_entry->p = elem->new(args);
292 if (pipeline_entry->p) {
293 list_add_tail(&pipeline_entry->
294 list, &pipeline->list);
295 #ifdef PIPELINE_DEBUG
296 printk(KERN_DEBUG "%s: created "
297 "instance of %s%s%s\n",
298 __func__, name, args ?
299 " with args " : "", args ?
303 printk(KERN_ERR "%s: failed "
304 "to add entry to pipeline: "
305 "%s (new() returned NULL)\n",
306 __func__, elem->name);
307 kfree(pipeline_entry);
318 printk(KERN_ERR "%s: element not found, skipping: "
319 "%s\n", __func__, name);
325 if (!list_empty(&pipeline->list))
330 #ifdef PIPELINE_DEBUG
331 printk(KERN_DEBUG "%s: dsp pipeline built%s: %s\n",
332 __func__, incomplete ? " incomplete" : "", cfg);
338 void dsp_pipeline_process_tx(struct dsp_pipeline *pipeline, u8 *data, int len)
340 struct dsp_pipeline_entry *entry;
345 list_for_each_entry(entry, &pipeline->list, list)
346 if (entry->elem->process_tx)
347 entry->elem->process_tx(entry->p, data, len);
350 void dsp_pipeline_process_rx(struct dsp_pipeline *pipeline, u8 *data, int len)
352 struct dsp_pipeline_entry *entry;
357 list_for_each_entry_reverse(entry, &pipeline->list, list)
358 if (entry->elem->process_rx)
359 entry->elem->process_rx(entry->p, data, len);