dahdi_vpmadt032_loader.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * DAHDI Telephony Interface to VPMADT032 Firmware Loader
  3. *
  4. * Copyright (C) 2008-2010 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 <dahdi/kernel.h>
  27. static int debug;
  28. #include "voicebus/voicebus.h"
  29. #include "voicebus/vpmadtreg.h"
  30. #include "vpmadt032_loader.h"
  31. vpmlinkage static int __attribute__((format (printf, 1, 2)))
  32. logger(const char *format, ...)
  33. {
  34. int res;
  35. va_list args;
  36. #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 9)
  37. va_start(args, format);
  38. res = vprintk(format, args);
  39. va_end(args);
  40. #else
  41. char buf[256];
  42. va_start(args, format);
  43. res = vsnprintf(buf, sizeof(buf), format, args);
  44. va_end(args);
  45. printk(KERN_INFO "%s" buf);
  46. #endif
  47. return res;
  48. }
  49. vpmlinkage static void *memalloc(size_t len)
  50. {
  51. return kmalloc(len, GFP_KERNEL);
  52. }
  53. vpmlinkage static void memfree(void *ptr)
  54. {
  55. kfree(ptr);
  56. }
  57. struct private_context {
  58. struct voicebus *vb;
  59. void *pvt;
  60. struct completion done;
  61. struct voicebus_operations ops;
  62. };
  63. static void handle_receive(struct voicebus *vb, struct list_head *buffers)
  64. {
  65. struct vbb *vbb;
  66. struct private_context *ctx = container_of(vb->ops,
  67. struct private_context, ops);
  68. list_for_each_entry(vbb, buffers, entry) {
  69. __vpmadt032_receive(ctx->pvt, vbb->data);
  70. if (__vpmadt032_done(ctx->pvt))
  71. complete(&ctx->done);
  72. }
  73. }
  74. static void handle_transmit(struct voicebus *vb, struct list_head *buffers)
  75. {
  76. struct vbb *vbb;
  77. struct private_context *ctx = container_of(vb->ops,
  78. struct private_context, ops);
  79. list_for_each_entry(vbb, buffers, entry)
  80. __vpmadt032_transmit(ctx->pvt, vbb->data);
  81. }
  82. static void init_private_context(struct private_context *ctx)
  83. {
  84. init_completion(&ctx->done);
  85. ctx->ops.handle_receive = handle_receive;
  86. ctx->ops.handle_transmit = handle_transmit;
  87. }
  88. static int vpmadt032_load_firmware(struct voicebus *vb)
  89. {
  90. int ret = 0;
  91. struct private_context *ctx;
  92. const struct voicebus_operations *old;
  93. int id;
  94. might_sleep();
  95. ctx = kzalloc(sizeof(struct private_context), GFP_KERNEL);
  96. if (!ctx)
  97. return -ENOMEM;
  98. init_private_context(ctx);
  99. ctx->vb = vb;
  100. if (0x8007 == vb->pdev->device || 0x8008 == vb->pdev->device)
  101. id = vb->pdev->vendor << 16 | 0x2400;
  102. else
  103. id = vb->pdev->vendor << 16 | vb->pdev->device;
  104. ret = __vpmadt032_start_load(0, id, &ctx->pvt);
  105. if (ret)
  106. goto error_exit;
  107. old = vb->ops;
  108. vb->ops = &ctx->ops;
  109. if (!wait_for_completion_timeout(&ctx->done, HZ*20))
  110. ret = -EIO;
  111. vb->ops = old;
  112. __vpmadt032_cleanup(ctx->pvt);
  113. error_exit:
  114. kfree(ctx);
  115. return ret;
  116. }
  117. static struct vpmadt_loader loader = {
  118. .owner = THIS_MODULE,
  119. .load = vpmadt032_load_firmware,
  120. };
  121. static int __init vpmadt032_loader_init(void)
  122. {
  123. __vpmadt032_init(logger, debug, memalloc, memfree);
  124. vpmadtreg_register(&loader);
  125. return 0;
  126. }
  127. static void __exit vpmadt032_loader_exit(void)
  128. {
  129. vpmadtreg_unregister(&loader);
  130. return;
  131. }
  132. module_param(debug, int, S_IRUGO | S_IWUSR);
  133. MODULE_DESCRIPTION("DAHDI VPMADT032 (Hardware Echo Canceller) Firmware Loader");
  134. MODULE_AUTHOR("Digium Incorporated <support@digium.com>");
  135. MODULE_LICENSE("Digium Commercial");
  136. module_init(vpmadt032_loader_init);
  137. module_exit(vpmadt032_loader_exit);