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/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>
37 /* uncomment for debugging */
38 /*#define PIPELINE_DEBUG*/
40 struct dsp_pipeline_entry {
41 struct mISDN_dsp_element *elem;
43 struct list_head list;
45 struct dsp_element_entry {
46 struct mISDN_dsp_element *elem;
48 struct list_head list;
51 static LIST_HEAD(dsp_elements);
54 static struct class *elements_class;
57 attr_show_args(struct device *dev, struct device_attribute *attr, char *buf)
59 struct mISDN_dsp_element *elem = dev_get_drvdata(dev);
64 for (i = 0; i < elem->num_args; i++)
65 p += sprintf(p, "Name: %s\n%s%s%sDescription: %s\n\n",
67 elem->args[i].def ? "Default: " : "",
68 elem->args[i].def ? elem->args[i].def : "",
69 elem->args[i].def ? "\n" : "",
75 static struct device_attribute element_attributes[] = {
76 __ATTR(args, 0444, attr_show_args, NULL),
80 mISDN_dsp_dev_release(struct device *dev)
82 struct dsp_element_entry *entry =
83 container_of(dev, struct dsp_element_entry, dev);
84 list_del(&entry->list);
88 int mISDN_dsp_element_register(struct mISDN_dsp_element *elem)
90 struct dsp_element_entry *entry;
96 entry = kzalloc(sizeof(struct dsp_element_entry), GFP_ATOMIC);
100 INIT_LIST_HEAD(&entry->list);
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);
109 printk(KERN_ERR "%s: failed to register %s\n",
110 __func__, elem->name);
113 list_add_tail(&entry->list, &dsp_elements);
115 for (i = 0; i < ARRAY_SIZE(element_attributes); ++i) {
116 ret = device_create_file(&entry->dev,
117 &element_attributes[i]);
119 printk(KERN_ERR "%s: failed to create device file\n",
125 #ifdef PIPELINE_DEBUG
126 printk(KERN_DEBUG "%s: %s registered\n", __func__, elem->name);
132 device_unregister(&entry->dev);
135 put_device(&entry->dev);
138 EXPORT_SYMBOL(mISDN_dsp_element_register);
140 void mISDN_dsp_element_unregister(struct mISDN_dsp_element *elem)
142 struct dsp_element_entry *entry, *n;
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);
156 printk(KERN_ERR "%s: element %s not in list.\n", __func__, elem->name);
158 EXPORT_SYMBOL(mISDN_dsp_element_unregister);
160 int dsp_pipeline_module_init(void)
162 elements_class = class_create(THIS_MODULE, "dsp_pipeline");
163 if (IS_ERR(elements_class))
164 return PTR_ERR(elements_class);
166 #ifdef PIPELINE_DEBUG
167 printk(KERN_DEBUG "%s: dsp pipeline module initialized\n", __func__);
175 void dsp_pipeline_module_exit(void)
177 struct dsp_element_entry *entry, *n;
181 class_destroy(elements_class);
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);
190 #ifdef PIPELINE_DEBUG
191 printk(KERN_DEBUG "%s: dsp pipeline module exited\n", __func__);
195 int dsp_pipeline_init(struct dsp_pipeline *pipeline)
200 INIT_LIST_HEAD(&pipeline->list);
202 #ifdef PIPELINE_DEBUG
203 printk(KERN_DEBUG "%s: dsp pipeline ready\n", __func__);
209 static inline void _dsp_pipeline_destroy(struct dsp_pipeline *pipeline)
211 struct dsp_pipeline_entry *entry, *n;
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,
219 entry->elem->free(entry->p);
224 void dsp_pipeline_destroy(struct dsp_pipeline *pipeline)
230 _dsp_pipeline_destroy(pipeline);
232 #ifdef PIPELINE_DEBUG
233 printk(KERN_DEBUG "%s: dsp pipeline destroyed\n", __func__);
237 int dsp_pipeline_build(struct dsp_pipeline *pipeline, const char *cfg)
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;
248 if (!list_empty(&pipeline->list))
249 _dsp_pipeline_destroy(pipeline);
251 dup = kstrdup(cfg, GFP_ATOMIC);
254 while ((tok = strsep(&dup, "|"))) {
257 name = strsep(&tok, "(");
258 args = strsep(&tok, ")");
262 list_for_each_entry_safe(entry, n, &dsp_elements, list)
263 if (!strcmp(entry->elem->name, name)) {
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);
275 pipeline_entry->elem = elem;
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,
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 ?
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);
312 printk(KERN_ERR "%s: element not found, skipping: "
313 "%s\n", __func__, name);
319 if (!list_empty(&pipeline->list))
324 #ifdef PIPELINE_DEBUG
325 printk(KERN_DEBUG "%s: dsp pipeline built%s: %s\n",
326 __func__, incomplete ? " incomplete" : "", cfg);
332 void dsp_pipeline_process_tx(struct dsp_pipeline *pipeline, u8 *data, int len)
334 struct dsp_pipeline_entry *entry;
339 list_for_each_entry(entry, &pipeline->list, list)
340 if (entry->elem->process_tx)
341 entry->elem->process_tx(entry->p, data, len);
344 void dsp_pipeline_process_rx(struct dsp_pipeline *pipeline, u8 *data, int len,
347 struct dsp_pipeline_entry *entry;
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);