dsp_pipeline.c 8.2 KB

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