dsp_pipeline.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*
  2. * dsp_pipeline.c: pipelined audio processing
  3. *
  4. * Copyright (C) 2007, Nadi Sarrar
  5. *
  6. * Nadi Sarrar <nadi@beronet.com>
  7. *
  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)
  11. * any later version.
  12. *
  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
  16. * more details.
  17. *
  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.
  21. *
  22. * The full GNU General Public License is included in this distribution in the
  23. * file called LICENSE.
  24. *
  25. */
  26. #include <linux/kernel.h>
  27. #include <linux/slab.h>
  28. #include <linux/list.h>
  29. #include <linux/string.h>
  30. #include <linux/mISDNif.h>
  31. #include <linux/mISDNdsp.h>
  32. #include "dsp.h"
  33. #include "dsp_hwec.h"
  34. /* uncomment for debugging */
  35. /*#define PIPELINE_DEBUG*/
  36. struct dsp_pipeline_entry {
  37. struct mISDN_dsp_element *elem;
  38. void *p;
  39. struct list_head list;
  40. };
  41. struct dsp_element_entry {
  42. struct mISDN_dsp_element *elem;
  43. struct device dev;
  44. struct list_head list;
  45. };
  46. static LIST_HEAD(dsp_elements);
  47. /* sysfs */
  48. static struct class *elements_class;
  49. static ssize_t
  50. attr_show_args(struct device *dev, struct device_attribute *attr, char *buf)
  51. {
  52. struct mISDN_dsp_element *elem = dev_get_drvdata(dev);
  53. int i;
  54. char *p = buf;
  55. *buf = 0;
  56. for (i = 0; i < elem->num_args; i++)
  57. p += sprintf(p, "Name: %s\n%s%s%sDescription: %s\n\n",
  58. elem->args[i].name,
  59. elem->args[i].def ? "Default: " : "",
  60. elem->args[i].def ? elem->args[i].def : "",
  61. elem->args[i].def ? "\n" : "",
  62. elem->args[i].desc);
  63. return p - buf;
  64. }
  65. static struct device_attribute element_attributes[] = {
  66. __ATTR(args, 0444, attr_show_args, NULL),
  67. };
  68. static void
  69. mISDN_dsp_dev_release(struct device *dev)
  70. {
  71. struct dsp_element_entry *entry =
  72. container_of(dev, struct dsp_element_entry, dev);
  73. list_del(&entry->list);
  74. kfree(entry);
  75. }
  76. int mISDN_dsp_element_register(struct mISDN_dsp_element *elem)
  77. {
  78. struct dsp_element_entry *entry;
  79. int ret, i;
  80. if (!elem)
  81. return -EINVAL;
  82. entry = kzalloc(sizeof(struct dsp_element_entry), GFP_ATOMIC);
  83. if (!entry)
  84. return -ENOMEM;
  85. entry->elem = elem;
  86. entry->dev.class = elements_class;
  87. entry->dev.release = mISDN_dsp_dev_release;
  88. dev_set_drvdata(&entry->dev, elem);
  89. dev_set_name(&entry->dev, elem->name);
  90. ret = device_register(&entry->dev);
  91. if (ret) {
  92. printk(KERN_ERR "%s: failed to register %s\n",
  93. __func__, elem->name);
  94. goto err1;
  95. }
  96. list_add_tail(&entry->list, &dsp_elements);
  97. for (i = 0; i < ARRAY_SIZE(element_attributes); ++i) {
  98. ret = device_create_file(&entry->dev,
  99. &element_attributes[i]);
  100. if (ret) {
  101. printk(KERN_ERR "%s: failed to create device file\n",
  102. __func__);
  103. goto err2;
  104. }
  105. }
  106. #ifdef PIPELINE_DEBUG
  107. printk(KERN_DEBUG "%s: %s registered\n", __func__, elem->name);
  108. #endif
  109. return 0;
  110. err2:
  111. device_unregister(&entry->dev);
  112. return ret;
  113. err1:
  114. kfree(entry);
  115. return ret;
  116. }
  117. EXPORT_SYMBOL(mISDN_dsp_element_register);
  118. void mISDN_dsp_element_unregister(struct mISDN_dsp_element *elem)
  119. {
  120. struct dsp_element_entry *entry, *n;
  121. if (!elem)
  122. return;
  123. list_for_each_entry_safe(entry, n, &dsp_elements, list)
  124. if (entry->elem == elem) {
  125. device_unregister(&entry->dev);
  126. #ifdef PIPELINE_DEBUG
  127. printk(KERN_DEBUG "%s: %s unregistered\n",
  128. __func__, elem->name);
  129. #endif
  130. return;
  131. }
  132. printk(KERN_ERR "%s: element %s not in list.\n", __func__, elem->name);
  133. }
  134. EXPORT_SYMBOL(mISDN_dsp_element_unregister);
  135. int dsp_pipeline_module_init(void)
  136. {
  137. elements_class = class_create(THIS_MODULE, "dsp_pipeline");
  138. if (IS_ERR(elements_class))
  139. return PTR_ERR(elements_class);
  140. #ifdef PIPELINE_DEBUG
  141. printk(KERN_DEBUG "%s: dsp pipeline module initialized\n", __func__);
  142. #endif
  143. dsp_hwec_init();
  144. return 0;
  145. }
  146. void dsp_pipeline_module_exit(void)
  147. {
  148. struct dsp_element_entry *entry, *n;
  149. dsp_hwec_exit();
  150. class_destroy(elements_class);
  151. list_for_each_entry_safe(entry, n, &dsp_elements, list) {
  152. list_del(&entry->list);
  153. printk(KERN_WARNING "%s: element was still registered: %s\n",
  154. __func__, entry->elem->name);
  155. kfree(entry);
  156. }
  157. #ifdef PIPELINE_DEBUG
  158. printk(KERN_DEBUG "%s: dsp pipeline module exited\n", __func__);
  159. #endif
  160. }
  161. int dsp_pipeline_init(struct dsp_pipeline *pipeline)
  162. {
  163. if (!pipeline)
  164. return -EINVAL;
  165. INIT_LIST_HEAD(&pipeline->list);
  166. #ifdef PIPELINE_DEBUG
  167. printk(KERN_DEBUG "%s: dsp pipeline ready\n", __func__);
  168. #endif
  169. return 0;
  170. }
  171. static inline void _dsp_pipeline_destroy(struct dsp_pipeline *pipeline)
  172. {
  173. struct dsp_pipeline_entry *entry, *n;
  174. list_for_each_entry_safe(entry, n, &pipeline->list, list) {
  175. list_del(&entry->list);
  176. if (entry->elem == dsp_hwec)
  177. dsp_hwec_disable(container_of(pipeline, struct dsp,
  178. pipeline));
  179. else
  180. entry->elem->free(entry->p);
  181. kfree(entry);
  182. }
  183. }
  184. void dsp_pipeline_destroy(struct dsp_pipeline *pipeline)
  185. {
  186. if (!pipeline)
  187. return;
  188. _dsp_pipeline_destroy(pipeline);
  189. #ifdef PIPELINE_DEBUG
  190. printk(KERN_DEBUG "%s: dsp pipeline destroyed\n", __func__);
  191. #endif
  192. }
  193. int dsp_pipeline_build(struct dsp_pipeline *pipeline, const char *cfg)
  194. {
  195. int len, incomplete = 0, found = 0;
  196. char *dup, *tok, *name, *args;
  197. struct dsp_element_entry *entry, *n;
  198. struct dsp_pipeline_entry *pipeline_entry;
  199. struct mISDN_dsp_element *elem;
  200. if (!pipeline)
  201. return -EINVAL;
  202. if (!list_empty(&pipeline->list))
  203. _dsp_pipeline_destroy(pipeline);
  204. if (!cfg)
  205. return 0;
  206. len = strlen(cfg);
  207. if (!len)
  208. return 0;
  209. dup = kmalloc(len + 1, GFP_ATOMIC);
  210. if (!dup)
  211. return 0;
  212. strcpy(dup, cfg);
  213. while ((tok = strsep(&dup, "|"))) {
  214. if (!strlen(tok))
  215. continue;
  216. name = strsep(&tok, "(");
  217. args = strsep(&tok, ")");
  218. if (args && !*args)
  219. args = NULL;
  220. list_for_each_entry_safe(entry, n, &dsp_elements, list)
  221. if (!strcmp(entry->elem->name, name)) {
  222. elem = entry->elem;
  223. pipeline_entry = kmalloc(sizeof(struct
  224. dsp_pipeline_entry), GFP_ATOMIC);
  225. if (!pipeline_entry) {
  226. printk(KERN_ERR "%s: failed to add "
  227. "entry to pipeline: %s (out of "
  228. "memory)\n", __func__, elem->name);
  229. incomplete = 1;
  230. goto _out;
  231. }
  232. pipeline_entry->elem = elem;
  233. if (elem == dsp_hwec) {
  234. /* This is a hack to make the hwec
  235. available as a pipeline module */
  236. dsp_hwec_enable(container_of(pipeline,
  237. struct dsp, pipeline), args);
  238. list_add_tail(&pipeline_entry->list,
  239. &pipeline->list);
  240. } else {
  241. pipeline_entry->p = elem->new(args);
  242. if (pipeline_entry->p) {
  243. list_add_tail(&pipeline_entry->
  244. list, &pipeline->list);
  245. #ifdef PIPELINE_DEBUG
  246. printk(KERN_DEBUG "%s: created "
  247. "instance of %s%s%s\n",
  248. __func__, name, args ?
  249. " with args " : "", args ?
  250. args : "");
  251. #endif
  252. } else {
  253. printk(KERN_ERR "%s: failed "
  254. "to add entry to pipeline: "
  255. "%s (new() returned NULL)\n",
  256. __func__, elem->name);
  257. kfree(pipeline_entry);
  258. incomplete = 1;
  259. }
  260. }
  261. found = 1;
  262. break;
  263. }
  264. if (found)
  265. found = 0;
  266. else {
  267. printk(KERN_ERR "%s: element not found, skipping: "
  268. "%s\n", __func__, name);
  269. incomplete = 1;
  270. }
  271. }
  272. _out:
  273. if (!list_empty(&pipeline->list))
  274. pipeline->inuse = 1;
  275. else
  276. pipeline->inuse = 0;
  277. #ifdef PIPELINE_DEBUG
  278. printk(KERN_DEBUG "%s: dsp pipeline built%s: %s\n",
  279. __func__, incomplete ? " incomplete" : "", cfg);
  280. #endif
  281. kfree(dup);
  282. return 0;
  283. }
  284. void dsp_pipeline_process_tx(struct dsp_pipeline *pipeline, u8 *data, int len)
  285. {
  286. struct dsp_pipeline_entry *entry;
  287. if (!pipeline)
  288. return;
  289. list_for_each_entry(entry, &pipeline->list, list)
  290. if (entry->elem->process_tx)
  291. entry->elem->process_tx(entry->p, data, len);
  292. }
  293. void dsp_pipeline_process_rx(struct dsp_pipeline *pipeline, u8 *data, int len,
  294. unsigned int txlen)
  295. {
  296. struct dsp_pipeline_entry *entry;
  297. if (!pipeline)
  298. return;
  299. list_for_each_entry_reverse(entry, &pipeline->list, list)
  300. if (entry->elem->process_rx)
  301. entry->elem->process_rx(entry->p, data, len, txlen);
  302. }