dahdi_vpmadt032_loader.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * DAHDI Telephony Interface to VPMADT032 Firmware Loader
  3. *
  4. * Copyright (C) 2008-2012 Digium, Inc. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/errno.h>
  21. #include <linux/module.h>
  22. #include <linux/init.h>
  23. #include <linux/ctype.h>
  24. #include <linux/moduleparam.h>
  25. #include <linux/pci.h>
  26. #include <linux/slab.h>
  27. #include <dahdi/kernel.h>
  28. static int debug;
  29. #include "voicebus/voicebus.h"
  30. #include "voicebus/vpmadtreg.h"
  31. #include "vpmadt032_loader.h"
  32. vpmlinkage static int __attribute__((format (printf, 1, 2)))
  33. logger(const char *format, ...)
  34. {
  35. int res;
  36. va_list args;
  37. va_start(args, format);
  38. res = vprintk(format, args);
  39. va_end(args);
  40. return res;
  41. }
  42. vpmlinkage static void *memalloc(size_t len)
  43. {
  44. return kmalloc(len, GFP_KERNEL);
  45. }
  46. vpmlinkage static void memfree(void *ptr)
  47. {
  48. kfree(ptr);
  49. }
  50. struct private_context {
  51. struct voicebus *vb;
  52. void *pvt;
  53. struct completion done;
  54. struct voicebus_operations ops;
  55. };
  56. static void handle_receive(struct voicebus *vb, struct list_head *buffers)
  57. {
  58. struct vbb *vbb;
  59. struct private_context *ctx = container_of(vb->ops,
  60. struct private_context, ops);
  61. list_for_each_entry(vbb, buffers, entry) {
  62. __vpmadt032_receive(ctx->pvt, vbb->data);
  63. if (__vpmadt032_done(ctx->pvt))
  64. complete(&ctx->done);
  65. }
  66. }
  67. static void handle_transmit(struct voicebus *vb, struct list_head *buffers)
  68. {
  69. struct vbb *vbb;
  70. struct private_context *ctx = container_of(vb->ops,
  71. struct private_context, ops);
  72. list_for_each_entry(vbb, buffers, entry)
  73. __vpmadt032_transmit(ctx->pvt, vbb->data);
  74. }
  75. static void init_private_context(struct private_context *ctx)
  76. {
  77. init_completion(&ctx->done);
  78. ctx->ops.handle_receive = handle_receive;
  79. ctx->ops.handle_transmit = handle_transmit;
  80. }
  81. static int vpmadt032_load_firmware(struct voicebus *vb)
  82. {
  83. int ret = 0;
  84. struct private_context *ctx;
  85. const struct voicebus_operations *old;
  86. int id;
  87. might_sleep();
  88. ctx = kzalloc(sizeof(struct private_context), GFP_KERNEL);
  89. if (!ctx)
  90. return -ENOMEM;
  91. init_private_context(ctx);
  92. ctx->vb = vb;
  93. if (0x8007 == vb->pdev->device || 0x8008 == vb->pdev->device)
  94. id = vb->pdev->vendor << 16 | 0x2400;
  95. else
  96. id = vb->pdev->vendor << 16 | vb->pdev->device;
  97. ret = __vpmadt032_start_load(0, id, &ctx->pvt);
  98. if (ret)
  99. goto error_exit;
  100. old = vb->ops;
  101. vb->ops = &ctx->ops;
  102. if (!wait_for_completion_timeout(&ctx->done, HZ*20))
  103. ret = -EIO;
  104. vb->ops = old;
  105. __vpmadt032_cleanup(ctx->pvt);
  106. error_exit:
  107. kfree(ctx);
  108. return ret;
  109. }
  110. static struct vpmadt_loader loader = {
  111. .owner = THIS_MODULE,
  112. .load = vpmadt032_load_firmware,
  113. };
  114. static int __init vpmadt032_loader_init(void)
  115. {
  116. __vpmadt032_init(logger, debug, memalloc, memfree);
  117. vpmadtreg_register(&loader);
  118. return 0;
  119. }
  120. static void __exit vpmadt032_loader_exit(void)
  121. {
  122. vpmadtreg_unregister(&loader);
  123. return;
  124. }
  125. module_param(debug, int, S_IRUGO | S_IWUSR);
  126. MODULE_DESCRIPTION("DAHDI VPMADT032 (Hardware Echo Canceller) Firmware Loader");
  127. MODULE_AUTHOR("Digium Incorporated <support@digium.com>");
  128. MODULE_LICENSE("Digium Commercial");
  129. module_init(vpmadt032_loader_init);
  130. module_exit(vpmadt032_loader_exit);