dsp_pipeline.c 8.2 KB

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