Blame view

drivers/isdn/mISDN/dsp_pipeline.c 8.15 KB
960366cf8   Karsten Keil   Add mISDN DSP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
  /*
   * dsp_pipeline.c: pipelined audio processing
   *
   * Copyright (C) 2007, Nadi Sarrar
   *
   * Nadi Sarrar <nadi@beronet.com>
   *
   * This program is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License as published by the Free
   * Software Foundation; either version 2 of the License, or (at your option)
   * any later version.
   *
   * This program is distributed in the hope that it will be useful, but WITHOUT
   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
   * more details.
   *
   * You should have received a copy of the GNU General Public License along with
   * this program; if not, write to the Free Software Foundation, Inc., 59
   * Temple Place - Suite 330, Boston, MA  02111-1307, USA.
   *
   * The full GNU General Public License is included in this distribution in the
   * file called LICENSE.
   *
   */
  
  #include <linux/kernel.h>
  #include <linux/list.h>
  #include <linux/string.h>
  #include <linux/mISDNif.h>
  #include <linux/mISDNdsp.h>
  #include "dsp.h"
  #include "dsp_hwec.h"
  
  /* uncomment for debugging */
  /*#define PIPELINE_DEBUG*/
  
  struct dsp_pipeline_entry {
  	struct mISDN_dsp_element *elem;
  	void                *p;
  	struct list_head     list;
  };
  struct dsp_element_entry {
  	struct mISDN_dsp_element *elem;
  	struct device	     dev;
  	struct list_head     list;
  };
  
  static LIST_HEAD(dsp_elements);
  
  /* sysfs */
  static struct class *elements_class;
  
  static ssize_t
  attr_show_args(struct device *dev, struct device_attribute *attr, char *buf)
  {
  	struct mISDN_dsp_element *elem = dev_get_drvdata(dev);
1ce1513f4   Karsten Keil   mISDN: Fix overla...
58
59
  	int i;
  	char *p = buf;
960366cf8   Karsten Keil   Add mISDN DSP
60
61
  
  	*buf = 0;
1ce1513f4   Karsten Keil   mISDN: Fix overla...
62
63
64
65
66
  	for (i = 0; i < elem->num_args; i++)
  		p += sprintf(p, "Name:        %s
  %s%s%sDescription: %s
  
  ",
960366cf8   Karsten Keil   Add mISDN DSP
67
68
69
70
71
72
  			  elem->args[i].name,
  			  elem->args[i].def ? "Default:     " : "",
  			  elem->args[i].def ? elem->args[i].def : "",
  			  elem->args[i].def ? "
  " : "",
  			  elem->args[i].desc);
1ce1513f4   Karsten Keil   mISDN: Fix overla...
73
  	return p - buf;
960366cf8   Karsten Keil   Add mISDN DSP
74
75
76
77
78
  }
  
  static struct device_attribute element_attributes[] = {
  	__ATTR(args, 0444, attr_show_args, NULL),
  };
808a14a15   Andreas Eversberg   mISDN: Add missin...
79
80
81
82
83
84
85
86
  static void
  mISDN_dsp_dev_release(struct device *dev)
  {
  	struct dsp_element_entry *entry =
  		container_of(dev, struct dsp_element_entry, dev);
  	list_del(&entry->list);
  	kfree(entry);
  }
960366cf8   Karsten Keil   Add mISDN DSP
87
88
89
90
91
92
93
  int mISDN_dsp_element_register(struct mISDN_dsp_element *elem)
  {
  	struct dsp_element_entry *entry;
  	int ret, i;
  
  	if (!elem)
  		return -EINVAL;
400fd9783   Andreas Eversberg   mISDN: Minor clea...
94
  	entry = kzalloc(sizeof(struct dsp_element_entry), GFP_ATOMIC);
960366cf8   Karsten Keil   Add mISDN DSP
95
96
97
98
99
100
  	if (!entry)
  		return -ENOMEM;
  
  	entry->elem = elem;
  
  	entry->dev.class = elements_class;
808a14a15   Andreas Eversberg   mISDN: Add missin...
101
  	entry->dev.release = mISDN_dsp_dev_release;
960366cf8   Karsten Keil   Add mISDN DSP
102
  	dev_set_drvdata(&entry->dev, elem);
0162f3822   Kay Sievers   ISDN: struct devi...
103
  	dev_set_name(&entry->dev, elem->name);
960366cf8   Karsten Keil   Add mISDN DSP
104
105
106
107
108
109
110
  	ret = device_register(&entry->dev);
  	if (ret) {
  		printk(KERN_ERR "%s: failed to register %s
  ",
  			__func__, elem->name);
  		goto err1;
  	}
808a14a15   Andreas Eversberg   mISDN: Add missin...
111
  	list_add_tail(&entry->list, &dsp_elements);
960366cf8   Karsten Keil   Add mISDN DSP
112

21c150a6d   Ilpo Järvinen   misdn: indentatio...
113
  	for (i = 0; i < ARRAY_SIZE(element_attributes); ++i) {
960366cf8   Karsten Keil   Add mISDN DSP
114
115
116
117
118
119
120
121
  		ret = device_create_file(&entry->dev,
  				&element_attributes[i]);
  		if (ret) {
  			printk(KERN_ERR "%s: failed to create device file
  ",
  				__func__);
  			goto err2;
  		}
21c150a6d   Ilpo Järvinen   misdn: indentatio...
122
  	}
960366cf8   Karsten Keil   Add mISDN DSP
123

808a14a15   Andreas Eversberg   mISDN: Add missin...
124
  #ifdef PIPELINE_DEBUG
960366cf8   Karsten Keil   Add mISDN DSP
125
126
  	printk(KERN_DEBUG "%s: %s registered
  ", __func__, elem->name);
808a14a15   Andreas Eversberg   mISDN: Add missin...
127
  #endif
960366cf8   Karsten Keil   Add mISDN DSP
128
129
130
131
132
  
  	return 0;
  
  err2:
  	device_unregister(&entry->dev);
808a14a15   Andreas Eversberg   mISDN: Add missin...
133
  	return ret;
960366cf8   Karsten Keil   Add mISDN DSP
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
  err1:
  	kfree(entry);
  	return ret;
  }
  EXPORT_SYMBOL(mISDN_dsp_element_register);
  
  void mISDN_dsp_element_unregister(struct mISDN_dsp_element *elem)
  {
  	struct dsp_element_entry *entry, *n;
  
  	if (!elem)
  		return;
  
  	list_for_each_entry_safe(entry, n, &dsp_elements, list)
  		if (entry->elem == elem) {
960366cf8   Karsten Keil   Add mISDN DSP
149
  			device_unregister(&entry->dev);
808a14a15   Andreas Eversberg   mISDN: Add missin...
150
  #ifdef PIPELINE_DEBUG
960366cf8   Karsten Keil   Add mISDN DSP
151
152
153
  			printk(KERN_DEBUG "%s: %s unregistered
  ",
  				__func__, elem->name);
808a14a15   Andreas Eversberg   mISDN: Add missin...
154
  #endif
960366cf8   Karsten Keil   Add mISDN DSP
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
  			return;
  		}
  	printk(KERN_ERR "%s: element %s not in list.
  ", __func__, elem->name);
  }
  EXPORT_SYMBOL(mISDN_dsp_element_unregister);
  
  int dsp_pipeline_module_init(void)
  {
  	elements_class = class_create(THIS_MODULE, "dsp_pipeline");
  	if (IS_ERR(elements_class))
  		return PTR_ERR(elements_class);
  
  #ifdef PIPELINE_DEBUG
  	printk(KERN_DEBUG "%s: dsp pipeline module initialized
  ", __func__);
  #endif
  
  	dsp_hwec_init();
  
  	return 0;
  }
  
  void dsp_pipeline_module_exit(void)
  {
  	struct dsp_element_entry *entry, *n;
  
  	dsp_hwec_exit();
  
  	class_destroy(elements_class);
  
  	list_for_each_entry_safe(entry, n, &dsp_elements, list) {
  		list_del(&entry->list);
  		printk(KERN_WARNING "%s: element was still registered: %s
  ",
  			__func__, entry->elem->name);
  		kfree(entry);
  	}
808a14a15   Andreas Eversberg   mISDN: Add missin...
193
  #ifdef PIPELINE_DEBUG
960366cf8   Karsten Keil   Add mISDN DSP
194
195
  	printk(KERN_DEBUG "%s: dsp pipeline module exited
  ", __func__);
808a14a15   Andreas Eversberg   mISDN: Add missin...
196
  #endif
960366cf8   Karsten Keil   Add mISDN DSP
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
  }
  
  int dsp_pipeline_init(struct dsp_pipeline *pipeline)
  {
  	if (!pipeline)
  		return -EINVAL;
  
  	INIT_LIST_HEAD(&pipeline->list);
  
  #ifdef PIPELINE_DEBUG
  	printk(KERN_DEBUG "%s: dsp pipeline ready
  ", __func__);
  #endif
  
  	return 0;
  }
  
  static inline void _dsp_pipeline_destroy(struct dsp_pipeline *pipeline)
  {
  	struct dsp_pipeline_entry *entry, *n;
  
  	list_for_each_entry_safe(entry, n, &pipeline->list, list) {
  		list_del(&entry->list);
  		if (entry->elem == dsp_hwec)
  			dsp_hwec_disable(container_of(pipeline, struct dsp,
  				pipeline));
  		else
  			entry->elem->free(entry->p);
  		kfree(entry);
  	}
  }
  
  void dsp_pipeline_destroy(struct dsp_pipeline *pipeline)
  {
  
  	if (!pipeline)
  		return;
  
  	_dsp_pipeline_destroy(pipeline);
  
  #ifdef PIPELINE_DEBUG
  	printk(KERN_DEBUG "%s: dsp pipeline destroyed
  ", __func__);
  #endif
  }
  
  int dsp_pipeline_build(struct dsp_pipeline *pipeline, const char *cfg)
  {
  	int len, incomplete = 0, found = 0;
  	char *dup, *tok, *name, *args;
  	struct dsp_element_entry *entry, *n;
  	struct dsp_pipeline_entry *pipeline_entry;
  	struct mISDN_dsp_element *elem;
  
  	if (!pipeline)
  		return -EINVAL;
  
  	if (!list_empty(&pipeline->list))
  		_dsp_pipeline_destroy(pipeline);
  
  	if (!cfg)
  		return 0;
  
  	len = strlen(cfg);
  	if (!len)
  		return 0;
400fd9783   Andreas Eversberg   mISDN: Minor clea...
263
  	dup = kmalloc(len + 1, GFP_ATOMIC);
960366cf8   Karsten Keil   Add mISDN DSP
264
265
266
267
268
269
270
271
272
  	if (!dup)
  		return 0;
  	strcpy(dup, cfg);
  	while ((tok = strsep(&dup, "|"))) {
  		if (!strlen(tok))
  			continue;
  		name = strsep(&tok, "(");
  		args = strsep(&tok, ")");
  		if (args && !*args)
bcf917450   Hannes Eder   mISDN: use NULL p...
273
  			args = NULL;
960366cf8   Karsten Keil   Add mISDN DSP
274
275
276
277
278
279
  
  		list_for_each_entry_safe(entry, n, &dsp_elements, list)
  			if (!strcmp(entry->elem->name, name)) {
  				elem = entry->elem;
  
  				pipeline_entry = kmalloc(sizeof(struct
400fd9783   Andreas Eversberg   mISDN: Minor clea...
280
  					dsp_pipeline_entry), GFP_ATOMIC);
960366cf8   Karsten Keil   Add mISDN DSP
281
  				if (!pipeline_entry) {
808a14a15   Andreas Eversberg   mISDN: Add missin...
282
  					printk(KERN_ERR "%s: failed to add "
960366cf8   Karsten Keil   Add mISDN DSP
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
  					    "entry to pipeline: %s (out of "
  					    "memory)
  ", __func__, elem->name);
  					incomplete = 1;
  					goto _out;
  				}
  				pipeline_entry->elem = elem;
  
  				if (elem == dsp_hwec) {
  					/* This is a hack to make the hwec
  					   available as a pipeline module */
  					dsp_hwec_enable(container_of(pipeline,
  						struct dsp, pipeline), args);
  					list_add_tail(&pipeline_entry->list,
  						&pipeline->list);
  				} else {
  					pipeline_entry->p = elem->new(args);
  					if (pipeline_entry->p) {
  						list_add_tail(&pipeline_entry->
  							list, &pipeline->list);
  #ifdef PIPELINE_DEBUG
  						printk(KERN_DEBUG "%s: created "
  						    "instance of %s%s%s
  ",
  						    __func__, name, args ?
  						    " with args " : "", args ?
  						    args : "");
  #endif
  					} else {
808a14a15   Andreas Eversberg   mISDN: Add missin...
312
  						printk(KERN_ERR "%s: failed "
960366cf8   Karsten Keil   Add mISDN DSP
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
  						  "to add entry to pipeline: "
  						  "%s (new() returned NULL)
  ",
  						  __func__, elem->name);
  						kfree(pipeline_entry);
  						incomplete = 1;
  					}
  				}
  				found = 1;
  				break;
  			}
  
  		if (found)
  			found = 0;
  		else {
808a14a15   Andreas Eversberg   mISDN: Add missin...
328
  			printk(KERN_ERR "%s: element not found, skipping: "
960366cf8   Karsten Keil   Add mISDN DSP
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
  				"%s
  ", __func__, name);
  			incomplete = 1;
  		}
  	}
  
  _out:
  	if (!list_empty(&pipeline->list))
  		pipeline->inuse = 1;
  	else
  		pipeline->inuse = 0;
  
  #ifdef PIPELINE_DEBUG
  	printk(KERN_DEBUG "%s: dsp pipeline built%s: %s
  ",
  		__func__, incomplete ? " incomplete" : "", cfg);
  #endif
  	kfree(dup);
  	return 0;
  }
  
  void dsp_pipeline_process_tx(struct dsp_pipeline *pipeline, u8 *data, int len)
  {
  	struct dsp_pipeline_entry *entry;
  
  	if (!pipeline)
  		return;
  
  	list_for_each_entry(entry, &pipeline->list, list)
  		if (entry->elem->process_tx)
  			entry->elem->process_tx(entry->p, data, len);
  }
7cfa153dd   Andreas Eversberg   mISDN: Echo cance...
361
362
  void dsp_pipeline_process_rx(struct dsp_pipeline *pipeline, u8 *data, int len,
  	unsigned int txlen)
960366cf8   Karsten Keil   Add mISDN DSP
363
364
365
366
367
368
369
370
  {
  	struct dsp_pipeline_entry *entry;
  
  	if (!pipeline)
  		return;
  
  	list_for_each_entry_reverse(entry, &pipeline->list, list)
  		if (entry->elem->process_rx)
7cfa153dd   Andreas Eversberg   mISDN: Echo cance...
371
  			entry->elem->process_rx(entry->p, data, len, txlen);
960366cf8   Karsten Keil   Add mISDN DSP
372
  }