1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * dsp_pipeline.c: pipelined audio processing
5 * Copyright (C) 2007, Nadi Sarrar
7 * Nadi Sarrar <nadi@beronet.com>
10 #include <linux/kernel.h>
11 #include <linux/slab.h>
12 #include <linux/list.h>
13 #include <linux/string.h>
14 #include <linux/mISDNif.h>
15 #include <linux/mISDNdsp.h>
16 #include <linux/export.h>
20 struct dsp_pipeline_entry {
21 struct mISDN_dsp_element *elem;
23 struct list_head list;
25 struct dsp_element_entry {
26 struct mISDN_dsp_element *elem;
28 struct list_head list;
31 static LIST_HEAD(dsp_elements);
34 static struct class *elements_class;
37 attr_show_args(struct device *dev, struct device_attribute *attr, char *buf)
39 struct mISDN_dsp_element *elem = dev_get_drvdata(dev);
44 for (i = 0; i < elem->num_args; i++)
45 p += sprintf(p, "Name: %s\n%s%s%sDescription: %s\n\n",
47 elem->args[i].def ? "Default: " : "",
48 elem->args[i].def ? elem->args[i].def : "",
49 elem->args[i].def ? "\n" : "",
55 static struct device_attribute element_attributes[] = {
56 __ATTR(args, 0444, attr_show_args, NULL),
60 mISDN_dsp_dev_release(struct device *dev)
62 struct dsp_element_entry *entry =
63 container_of(dev, struct dsp_element_entry, dev);
64 list_del(&entry->list);
68 int mISDN_dsp_element_register(struct mISDN_dsp_element *elem)
70 struct dsp_element_entry *entry;
76 entry = kzalloc(sizeof(struct dsp_element_entry), GFP_ATOMIC);
82 entry->dev.class = elements_class;
83 entry->dev.release = mISDN_dsp_dev_release;
84 dev_set_drvdata(&entry->dev, elem);
85 dev_set_name(&entry->dev, "%s", elem->name);
86 ret = device_register(&entry->dev);
88 printk(KERN_ERR "%s: failed to register %s\n",
89 __func__, elem->name);
92 list_add_tail(&entry->list, &dsp_elements);
94 for (i = 0; i < ARRAY_SIZE(element_attributes); ++i) {
95 ret = device_create_file(&entry->dev,
96 &element_attributes[i]);
98 printk(KERN_ERR "%s: failed to create device file\n",
107 device_unregister(&entry->dev);
113 EXPORT_SYMBOL(mISDN_dsp_element_register);
115 void mISDN_dsp_element_unregister(struct mISDN_dsp_element *elem)
117 struct dsp_element_entry *entry, *n;
122 list_for_each_entry_safe(entry, n, &dsp_elements, list)
123 if (entry->elem == elem) {
124 device_unregister(&entry->dev);
127 printk(KERN_ERR "%s: element %s not in list.\n", __func__, elem->name);
129 EXPORT_SYMBOL(mISDN_dsp_element_unregister);
131 int dsp_pipeline_module_init(void)
133 elements_class = class_create(THIS_MODULE, "dsp_pipeline");
134 if (IS_ERR(elements_class))
135 return PTR_ERR(elements_class);
142 void dsp_pipeline_module_exit(void)
144 struct dsp_element_entry *entry, *n;
148 class_destroy(elements_class);
150 list_for_each_entry_safe(entry, n, &dsp_elements, list) {
151 list_del(&entry->list);
152 printk(KERN_WARNING "%s: element was still registered: %s\n",
153 __func__, entry->elem->name);
158 int dsp_pipeline_init(struct dsp_pipeline *pipeline)
163 INIT_LIST_HEAD(&pipeline->list);
168 static inline void _dsp_pipeline_destroy(struct dsp_pipeline *pipeline)
170 struct dsp_pipeline_entry *entry, *n;
172 list_for_each_entry_safe(entry, n, &pipeline->list, list) {
173 list_del(&entry->list);
174 if (entry->elem == dsp_hwec)
175 dsp_hwec_disable(container_of(pipeline, struct dsp,
178 entry->elem->free(entry->p);
183 void dsp_pipeline_destroy(struct dsp_pipeline *pipeline)
189 _dsp_pipeline_destroy(pipeline);
192 int dsp_pipeline_build(struct dsp_pipeline *pipeline, const char *cfg)
195 char *dup, *next, *tok, *name, *args;
196 struct dsp_element_entry *entry, *n;
197 struct dsp_pipeline_entry *pipeline_entry;
198 struct mISDN_dsp_element *elem;
203 if (!list_empty(&pipeline->list))
204 _dsp_pipeline_destroy(pipeline);
206 dup = next = kstrdup(cfg, GFP_ATOMIC);
209 while ((tok = strsep(&next, "|"))) {
212 name = strsep(&tok, "(");
213 args = strsep(&tok, ")");
217 list_for_each_entry_safe(entry, n, &dsp_elements, list)
218 if (!strcmp(entry->elem->name, name)) {
221 pipeline_entry = kmalloc(sizeof(struct
222 dsp_pipeline_entry), GFP_ATOMIC);
223 if (!pipeline_entry) {
224 printk(KERN_ERR "%s: failed to add "
225 "entry to pipeline: %s (out of "
226 "memory)\n", __func__, elem->name);
229 pipeline_entry->elem = elem;
231 if (elem == dsp_hwec) {
232 /* This is a hack to make the hwec
233 available as a pipeline module */
234 dsp_hwec_enable(container_of(pipeline,
235 struct dsp, pipeline), args);
236 list_add_tail(&pipeline_entry->list,
239 pipeline_entry->p = elem->new(args);
240 if (pipeline_entry->p) {
241 list_add_tail(&pipeline_entry->
242 list, &pipeline->list);
244 printk(KERN_ERR "%s: failed "
245 "to add entry to pipeline: "
246 "%s (new() returned NULL)\n",
247 __func__, elem->name);
248 kfree(pipeline_entry);
258 printk(KERN_ERR "%s: element not found, skipping: "
259 "%s\n", __func__, name);
263 if (!list_empty(&pipeline->list))
272 void dsp_pipeline_process_tx(struct dsp_pipeline *pipeline, u8 *data, int len)
274 struct dsp_pipeline_entry *entry;
279 list_for_each_entry(entry, &pipeline->list, list)
280 if (entry->elem->process_tx)
281 entry->elem->process_tx(entry->p, data, len);
284 void dsp_pipeline_process_rx(struct dsp_pipeline *pipeline, u8 *data, int len,
287 struct dsp_pipeline_entry *entry;
292 list_for_each_entry_reverse(entry, &pipeline->list, list)
293 if (entry->elem->process_rx)
294 entry->elem->process_rx(entry->p, data, len, txlen);