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),
78 int mISDN_dsp_element_register(struct mISDN_dsp_element *elem)
80 struct dsp_element_entry *entry;
86 entry = kzalloc(sizeof(struct dsp_element_entry), GFP_KERNEL);
92 entry->dev.class = elements_class;
93 dev_set_drvdata(&entry->dev, elem);
94 dev_set_name(&entry->dev, elem->name);
95 ret = device_register(&entry->dev);
97 printk(KERN_ERR "%s: failed to register %s\n",
98 __func__, elem->name);
102 for (i = 0; i < (sizeof(element_attributes)
103 / sizeof(struct device_attribute)); ++i)
104 ret = device_create_file(&entry->dev,
105 &element_attributes[i]);
107 printk(KERN_ERR "%s: failed to create device file\n",
112 list_add_tail(&entry->list, &dsp_elements);
114 printk(KERN_DEBUG "%s: %s registered\n", __func__, elem->name);
119 device_unregister(&entry->dev);
124 EXPORT_SYMBOL(mISDN_dsp_element_register);
126 void mISDN_dsp_element_unregister(struct mISDN_dsp_element *elem)
128 struct dsp_element_entry *entry, *n;
133 list_for_each_entry_safe(entry, n, &dsp_elements, list)
134 if (entry->elem == elem) {
135 list_del(&entry->list);
136 device_unregister(&entry->dev);
138 printk(KERN_DEBUG "%s: %s unregistered\n",
139 __func__, elem->name);
142 printk(KERN_ERR "%s: element %s not in list.\n", __func__, elem->name);
144 EXPORT_SYMBOL(mISDN_dsp_element_unregister);
146 int dsp_pipeline_module_init(void)
148 elements_class = class_create(THIS_MODULE, "dsp_pipeline");
149 if (IS_ERR(elements_class))
150 return PTR_ERR(elements_class);
152 #ifdef PIPELINE_DEBUG
153 printk(KERN_DEBUG "%s: dsp pipeline module initialized\n", __func__);
161 void dsp_pipeline_module_exit(void)
163 struct dsp_element_entry *entry, *n;
167 class_destroy(elements_class);
169 list_for_each_entry_safe(entry, n, &dsp_elements, list) {
170 list_del(&entry->list);
171 printk(KERN_WARNING "%s: element was still registered: %s\n",
172 __func__, entry->elem->name);
176 printk(KERN_DEBUG "%s: dsp pipeline module exited\n", __func__);
179 int dsp_pipeline_init(struct dsp_pipeline *pipeline)
184 INIT_LIST_HEAD(&pipeline->list);
186 #ifdef PIPELINE_DEBUG
187 printk(KERN_DEBUG "%s: dsp pipeline ready\n", __func__);
193 static inline void _dsp_pipeline_destroy(struct dsp_pipeline *pipeline)
195 struct dsp_pipeline_entry *entry, *n;
197 list_for_each_entry_safe(entry, n, &pipeline->list, list) {
198 list_del(&entry->list);
199 if (entry->elem == dsp_hwec)
200 dsp_hwec_disable(container_of(pipeline, struct dsp,
203 entry->elem->free(entry->p);
208 void dsp_pipeline_destroy(struct dsp_pipeline *pipeline)
214 _dsp_pipeline_destroy(pipeline);
216 #ifdef PIPELINE_DEBUG
217 printk(KERN_DEBUG "%s: dsp pipeline destroyed\n", __func__);
221 int dsp_pipeline_build(struct dsp_pipeline *pipeline, const char *cfg)
223 int len, incomplete = 0, found = 0;
224 char *dup, *tok, *name, *args;
225 struct dsp_element_entry *entry, *n;
226 struct dsp_pipeline_entry *pipeline_entry;
227 struct mISDN_dsp_element *elem;
232 if (!list_empty(&pipeline->list))
233 _dsp_pipeline_destroy(pipeline);
242 dup = kmalloc(len + 1, GFP_KERNEL);
246 while ((tok = strsep(&dup, "|"))) {
249 name = strsep(&tok, "(");
250 args = strsep(&tok, ")");
254 list_for_each_entry_safe(entry, n, &dsp_elements, list)
255 if (!strcmp(entry->elem->name, name)) {
258 pipeline_entry = kmalloc(sizeof(struct
259 dsp_pipeline_entry), GFP_KERNEL);
260 if (!pipeline_entry) {
261 printk(KERN_DEBUG "%s: failed to add "
262 "entry to pipeline: %s (out of "
263 "memory)\n", __func__, elem->name);
267 pipeline_entry->elem = elem;
269 if (elem == dsp_hwec) {
270 /* This is a hack to make the hwec
271 available as a pipeline module */
272 dsp_hwec_enable(container_of(pipeline,
273 struct dsp, pipeline), args);
274 list_add_tail(&pipeline_entry->list,
277 pipeline_entry->p = elem->new(args);
278 if (pipeline_entry->p) {
279 list_add_tail(&pipeline_entry->
280 list, &pipeline->list);
281 #ifdef PIPELINE_DEBUG
282 printk(KERN_DEBUG "%s: created "
283 "instance of %s%s%s\n",
284 __func__, name, args ?
285 " with args " : "", args ?
289 printk(KERN_DEBUG "%s: failed "
290 "to add entry to pipeline: "
291 "%s (new() returned NULL)\n",
292 __func__, elem->name);
293 kfree(pipeline_entry);
304 printk(KERN_DEBUG "%s: element not found, skipping: "
305 "%s\n", __func__, name);
311 if (!list_empty(&pipeline->list))
316 #ifdef PIPELINE_DEBUG
317 printk(KERN_DEBUG "%s: dsp pipeline built%s: %s\n",
318 __func__, incomplete ? " incomplete" : "", cfg);
324 void dsp_pipeline_process_tx(struct dsp_pipeline *pipeline, u8 *data, int len)
326 struct dsp_pipeline_entry *entry;
331 list_for_each_entry(entry, &pipeline->list, list)
332 if (entry->elem->process_tx)
333 entry->elem->process_tx(entry->p, data, len);
336 void dsp_pipeline_process_rx(struct dsp_pipeline *pipeline, u8 *data, int len)
338 struct dsp_pipeline_entry *entry;
343 list_for_each_entry_reverse(entry, &pipeline->list, list)
344 if (entry->elem->process_rx)
345 entry->elem->process_rx(entry->p, data, len);