hpidspcd.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /***********************************************************************/
  2. /*!
  3. AudioScience HPI driver
  4. Copyright (C) 1997-2010 AudioScience Inc. <support@audioscience.com>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of version 2 of the GNU General Public License as
  7. published by the Free Software Foundation;
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. \file
  16. Functions for reading DSP code to load into DSP
  17. (Linux only:) If DSPCODE_FIRMWARE_LOADER is defined, code is read using
  18. hotplug firmware loader from individual dsp code files
  19. If neither of the above is defined, code is read from linked arrays.
  20. DSPCODE_ARRAY is defined.
  21. HPI_INCLUDE_**** must be defined
  22. and the appropriate hzz?????.c or hex?????.c linked in
  23. */
  24. /***********************************************************************/
  25. #define SOURCEFILE_NAME "hpidspcd.c"
  26. #include "hpidspcd.h"
  27. #include "hpidebug.h"
  28. /**
  29. Header structure for binary dsp code file (see asidsp.doc)
  30. This structure must match that used in s2bin.c for generation of asidsp.bin
  31. */
  32. #ifndef DISABLE_PRAGMA_PACK1
  33. #pragma pack(push, 1)
  34. #endif
  35. struct code_header {
  36. u32 size;
  37. char type[4];
  38. u32 adapter;
  39. u32 version;
  40. u32 crc;
  41. };
  42. #ifndef DISABLE_PRAGMA_PACK1
  43. #pragma pack(pop)
  44. #endif
  45. #define HPI_VER_DECIMAL ((int)(HPI_VER_MAJOR(HPI_VER) * 10000 + \
  46. HPI_VER_MINOR(HPI_VER) * 100 + HPI_VER_RELEASE(HPI_VER)))
  47. /***********************************************************************/
  48. #include <linux/pci.h>
  49. /*-------------------------------------------------------------------*/
  50. short hpi_dsp_code_open(u32 adapter, struct dsp_code *ps_dsp_code,
  51. u32 *pos_error_code)
  52. {
  53. const struct firmware *ps_firmware = ps_dsp_code->ps_firmware;
  54. struct code_header header;
  55. char fw_name[20];
  56. int err;
  57. sprintf(fw_name, "asihpi/dsp%04x.bin", adapter);
  58. err = request_firmware(&ps_firmware, fw_name,
  59. &ps_dsp_code->ps_dev->dev);
  60. if (err != 0) {
  61. dev_printk(KERN_ERR, &ps_dsp_code->ps_dev->dev,
  62. "%d, request_firmware failed for %s\n", err,
  63. fw_name);
  64. goto error1;
  65. }
  66. if (ps_firmware->size < sizeof(header)) {
  67. dev_printk(KERN_ERR, &ps_dsp_code->ps_dev->dev,
  68. "Header size too small %s\n", fw_name);
  69. goto error2;
  70. }
  71. memcpy(&header, ps_firmware->data, sizeof(header));
  72. if (header.adapter != adapter) {
  73. dev_printk(KERN_ERR, &ps_dsp_code->ps_dev->dev,
  74. "Adapter type incorrect %4x != %4x\n", header.adapter,
  75. adapter);
  76. goto error2;
  77. }
  78. if (header.size != ps_firmware->size) {
  79. dev_printk(KERN_ERR, &ps_dsp_code->ps_dev->dev,
  80. "Code size wrong %d != %ld\n", header.size,
  81. (unsigned long)ps_firmware->size);
  82. goto error2;
  83. }
  84. if (header.version / 100 != HPI_VER_DECIMAL / 100) {
  85. dev_printk(KERN_ERR, &ps_dsp_code->ps_dev->dev,
  86. "Incompatible firmware version "
  87. "DSP image %d != Driver %d\n", header.version,
  88. HPI_VER_DECIMAL);
  89. goto error2;
  90. }
  91. if (header.version != HPI_VER_DECIMAL) {
  92. dev_printk(KERN_WARNING, &ps_dsp_code->ps_dev->dev,
  93. "Firmware: release version mismatch DSP image %d != Driver %d\n",
  94. header.version, HPI_VER_DECIMAL);
  95. }
  96. HPI_DEBUG_LOG(DEBUG, "dsp code %s opened\n", fw_name);
  97. ps_dsp_code->ps_firmware = ps_firmware;
  98. ps_dsp_code->block_length = header.size / sizeof(u32);
  99. ps_dsp_code->word_count = sizeof(header) / sizeof(u32);
  100. ps_dsp_code->version = header.version;
  101. ps_dsp_code->crc = header.crc;
  102. return 0;
  103. error2:
  104. release_firmware(ps_firmware);
  105. error1:
  106. ps_dsp_code->ps_firmware = NULL;
  107. ps_dsp_code->block_length = 0;
  108. return HPI_ERROR_DSP_FILE_NOT_FOUND;
  109. }
  110. /*-------------------------------------------------------------------*/
  111. void hpi_dsp_code_close(struct dsp_code *ps_dsp_code)
  112. {
  113. if (ps_dsp_code->ps_firmware != NULL) {
  114. HPI_DEBUG_LOG(DEBUG, "dsp code closed\n");
  115. release_firmware(ps_dsp_code->ps_firmware);
  116. ps_dsp_code->ps_firmware = NULL;
  117. }
  118. }
  119. /*-------------------------------------------------------------------*/
  120. void hpi_dsp_code_rewind(struct dsp_code *ps_dsp_code)
  121. {
  122. /* Go back to start of data, after header */
  123. ps_dsp_code->word_count = sizeof(struct code_header) / sizeof(u32);
  124. }
  125. /*-------------------------------------------------------------------*/
  126. short hpi_dsp_code_read_word(struct dsp_code *ps_dsp_code, u32 *pword)
  127. {
  128. if (ps_dsp_code->word_count + 1 > ps_dsp_code->block_length)
  129. return HPI_ERROR_DSP_FILE_FORMAT;
  130. *pword = ((u32 *)(ps_dsp_code->ps_firmware->data))[ps_dsp_code->
  131. word_count];
  132. ps_dsp_code->word_count++;
  133. return 0;
  134. }
  135. /*-------------------------------------------------------------------*/
  136. short hpi_dsp_code_read_block(size_t words_requested,
  137. struct dsp_code *ps_dsp_code, u32 **ppblock)
  138. {
  139. if (ps_dsp_code->word_count + words_requested >
  140. ps_dsp_code->block_length)
  141. return HPI_ERROR_DSP_FILE_FORMAT;
  142. *ppblock =
  143. ((u32 *)(ps_dsp_code->ps_firmware->data)) +
  144. ps_dsp_code->word_count;
  145. ps_dsp_code->word_count += words_requested;
  146. return 0;
  147. }