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 = 0; i < elem->num_args; i++)
63 p += sprintf(p, "Name: %s\n%s%s%sDescription: %s\n\n",
65 elem->args[i].def ? "Default: " : "",
66 elem->args[i].def ? elem->args[i].def : "",
67 elem->args[i].def ? "\n" : "",
73 static struct device_attribute element_attributes[] = {
74 __ATTR(args, 0444, attr_show_args, NULL),
78 mISDN_dsp_dev_release(struct device *dev)
80 struct dsp_element_entry *entry =
81 container_of(dev, struct dsp_element_entry, dev);
82 list_del(&entry->list);
86 int mISDN_dsp_element_register(struct mISDN_dsp_element *elem)
88 struct dsp_element_entry *entry;
94 entry = kzalloc(sizeof(struct dsp_element_entry), GFP_ATOMIC);
100 entry->dev.class = elements_class;
101 entry->dev.release = mISDN_dsp_dev_release;
102 dev_set_drvdata(&entry->dev, elem);
103 dev_set_name(&entry->dev, elem->name);
104 ret = device_register(&entry->dev);
106 printk(KERN_ERR "%s: failed to register %s\n",
107 __func__, elem->name);
110 list_add_tail(&entry->list, &dsp_elements);
112 for (i = 0; i < ARRAY_SIZE(element_attributes); ++i) {
113 ret = device_create_file(&entry->dev,
114 &element_attributes[i]);
116 printk(KERN_ERR "%s: failed to create device file\n",
122 #ifdef PIPELINE_DEBUG
123 printk(KERN_DEBUG "%s: %s registered\n", __func__, elem->name);
129 device_unregister(&entry->dev);
135 EXPORT_SYMBOL(mISDN_dsp_element_register);
137 void mISDN_dsp_element_unregister(struct mISDN_dsp_element *elem)
139 struct dsp_element_entry *entry, *n;
144 list_for_each_entry_safe(entry, n, &dsp_elements, list)
145 if (entry->elem == elem) {
146 device_unregister(&entry->dev);
147 #ifdef PIPELINE_DEBUG
148 printk(KERN_DEBUG "%s: %s unregistered\n",
149 __func__, elem->name);
153 printk(KERN_ERR "%s: element %s not in list.\n", __func__, elem->name);
155 EXPORT_SYMBOL(mISDN_dsp_element_unregister);
157 int dsp_pipeline_module_init(void)
159 elements_class = class_create(THIS_MODULE, "dsp_pipeline");
160 if (IS_ERR(elements_class))
161 return PTR_ERR(elements_class);
163 #ifdef PIPELINE_DEBUG
164 printk(KERN_DEBUG "%s: dsp pipeline module initialized\n", __func__);
172 void dsp_pipeline_module_exit(void)
174 struct dsp_element_entry *entry, *n;
178 class_destroy(elements_class);
180 list_for_each_entry_safe(entry, n, &dsp_elements, list) {
181 list_del(&entry->list);
182 printk(KERN_WARNING "%s: element was still registered: %s\n",
183 __func__, entry->elem->name);
187 #ifdef PIPELINE_DEBUG
188 printk(KERN_DEBUG "%s: dsp pipeline module exited\n", __func__);
192 int dsp_pipeline_init(struct dsp_pipeline *pipeline)
197 INIT_LIST_HEAD(&pipeline->list);
199 #ifdef PIPELINE_DEBUG
200 printk(KERN_DEBUG "%s: dsp pipeline ready\n", __func__);
206 static inline void _dsp_pipeline_destroy(struct dsp_pipeline *pipeline)
208 struct dsp_pipeline_entry *entry, *n;
210 list_for_each_entry_safe(entry, n, &pipeline->list, list) {
211 list_del(&entry->list);
212 if (entry->elem == dsp_hwec)
213 dsp_hwec_disable(container_of(pipeline, struct dsp,
216 entry->elem->free(entry->p);
221 void dsp_pipeline_destroy(struct dsp_pipeline *pipeline)
227 _dsp_pipeline_destroy(pipeline);
229 #ifdef PIPELINE_DEBUG
230 printk(KERN_DEBUG "%s: dsp pipeline destroyed\n", __func__);
234 int dsp_pipeline_build(struct dsp_pipeline *pipeline, const char *cfg)
236 int len, incomplete = 0, found = 0;
237 char *dup, *tok, *name, *args;
238 struct dsp_element_entry *entry, *n;
239 struct dsp_pipeline_entry *pipeline_entry;
240 struct mISDN_dsp_element *elem;
245 if (!list_empty(&pipeline->list))
246 _dsp_pipeline_destroy(pipeline);
255 dup = kmalloc(len + 1, GFP_ATOMIC);
259 while ((tok = strsep(&dup, "|"))) {
262 name = strsep(&tok, "(");
263 args = strsep(&tok, ")");
267 list_for_each_entry_safe(entry, n, &dsp_elements, list)
268 if (!strcmp(entry->elem->name, name)) {
271 pipeline_entry = kmalloc(sizeof(struct
272 dsp_pipeline_entry), GFP_ATOMIC);
273 if (!pipeline_entry) {
274 printk(KERN_ERR "%s: failed to add "
275 "entry to pipeline: %s (out of "
276 "memory)\n", __func__, elem->name);
280 pipeline_entry->elem = elem;
282 if (elem == dsp_hwec) {
283 /* This is a hack to make the hwec
284 available as a pipeline module */
285 dsp_hwec_enable(container_of(pipeline,
286 struct dsp, pipeline), args);
287 list_add_tail(&pipeline_entry->list,
290 pipeline_entry->p = elem->new(args);
291 if (pipeline_entry->p) {
292 list_add_tail(&pipeline_entry->
293 list, &pipeline->list);
294 #ifdef PIPELINE_DEBUG
295 printk(KERN_DEBUG "%s: created "
296 "instance of %s%s%s\n",
297 __func__, name, args ?
298 " with args " : "", args ?
302 printk(KERN_ERR "%s: failed "
303 "to add entry to pipeline: "
304 "%s (new() returned NULL)\n",
305 __func__, elem->name);
306 kfree(pipeline_entry);
317 printk(KERN_ERR "%s: element not found, skipping: "
318 "%s\n", __func__, name);
324 if (!list_empty(&pipeline->list))
329 #ifdef PIPELINE_DEBUG
330 printk(KERN_DEBUG "%s: dsp pipeline built%s: %s\n",
331 __func__, incomplete ? " incomplete" : "", cfg);
337 void dsp_pipeline_process_tx(struct dsp_pipeline *pipeline, u8 *data, int len)
339 struct dsp_pipeline_entry *entry;
344 list_for_each_entry(entry, &pipeline->list, list)
345 if (entry->elem->process_tx)
346 entry->elem->process_tx(entry->p, data, len);
349 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, txlen);