hpidspcd.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /***********************************************************************
  2. AudioScience HPI driver
  3. Functions for reading DSP code using hotplug firmware loader
  4. Copyright (C) 1997-2014 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. ***********************************************************************/
  16. #define SOURCEFILE_NAME "hpidspcd.c"
  17. #include "hpidspcd.h"
  18. #include "hpidebug.h"
  19. #include "hpi_version.h"
  20. struct dsp_code_private {
  21. /** Firmware descriptor */
  22. const struct firmware *firmware;
  23. struct pci_dev *dev;
  24. };
  25. /*-------------------------------------------------------------------*/
  26. short hpi_dsp_code_open(u32 adapter, void *os_data, struct dsp_code *dsp_code,
  27. u32 *os_error_code)
  28. {
  29. const struct firmware *firmware;
  30. struct pci_dev *dev = os_data;
  31. struct code_header header;
  32. char fw_name[20];
  33. short err_ret = HPI_ERROR_DSP_FILE_NOT_FOUND;
  34. int err;
  35. sprintf(fw_name, "asihpi/dsp%04x.bin", adapter);
  36. err = request_firmware(&firmware, fw_name, &dev->dev);
  37. if (err || !firmware) {
  38. dev_err(&dev->dev, "%d, request_firmware failed for %s\n",
  39. err, fw_name);
  40. goto error1;
  41. }
  42. if (firmware->size < sizeof(header)) {
  43. dev_err(&dev->dev, "Header size too small %s\n", fw_name);
  44. goto error2;
  45. }
  46. memcpy(&header, firmware->data, sizeof(header));
  47. if ((header.type != 0x45444F43) || /* "CODE" */
  48. (header.adapter != adapter)
  49. || (header.size != firmware->size)) {
  50. dev_err(&dev->dev,
  51. "Invalid firmware header size %d != file %zd\n",
  52. header.size, firmware->size);
  53. goto error2;
  54. }
  55. if (HPI_VER_MAJOR(header.version) != HPI_VER_MAJOR(HPI_VER)) {
  56. /* Major version change probably means Host-DSP protocol change */
  57. dev_err(&dev->dev,
  58. "Incompatible firmware version DSP image %X != Driver %X\n",
  59. header.version, HPI_VER);
  60. goto error2;
  61. }
  62. if (header.version != HPI_VER) {
  63. dev_warn(&dev->dev,
  64. "Firmware version mismatch: DSP image %X != Driver %X\n",
  65. header.version, HPI_VER);
  66. }
  67. HPI_DEBUG_LOG(DEBUG, "dsp code %s opened\n", fw_name);
  68. dsp_code->pvt = kmalloc(sizeof(*dsp_code->pvt), GFP_KERNEL);
  69. if (!dsp_code->pvt) {
  70. err_ret = HPI_ERROR_MEMORY_ALLOC;
  71. goto error2;
  72. }
  73. dsp_code->pvt->dev = dev;
  74. dsp_code->pvt->firmware = firmware;
  75. dsp_code->header = header;
  76. dsp_code->block_length = header.size / sizeof(u32);
  77. dsp_code->word_count = sizeof(header) / sizeof(u32);
  78. return 0;
  79. error2:
  80. release_firmware(firmware);
  81. error1:
  82. dsp_code->block_length = 0;
  83. return err_ret;
  84. }
  85. /*-------------------------------------------------------------------*/
  86. void hpi_dsp_code_close(struct dsp_code *dsp_code)
  87. {
  88. HPI_DEBUG_LOG(DEBUG, "dsp code closed\n");
  89. release_firmware(dsp_code->pvt->firmware);
  90. kfree(dsp_code->pvt);
  91. }
  92. /*-------------------------------------------------------------------*/
  93. void hpi_dsp_code_rewind(struct dsp_code *dsp_code)
  94. {
  95. /* Go back to start of data, after header */
  96. dsp_code->word_count = sizeof(struct code_header) / sizeof(u32);
  97. }
  98. /*-------------------------------------------------------------------*/
  99. short hpi_dsp_code_read_word(struct dsp_code *dsp_code, u32 *pword)
  100. {
  101. if (dsp_code->word_count + 1 > dsp_code->block_length)
  102. return HPI_ERROR_DSP_FILE_FORMAT;
  103. *pword = ((u32 *)(dsp_code->pvt->firmware->data))[dsp_code->
  104. word_count];
  105. dsp_code->word_count++;
  106. return 0;
  107. }
  108. /*-------------------------------------------------------------------*/
  109. short hpi_dsp_code_read_block(size_t words_requested,
  110. struct dsp_code *dsp_code, u32 **ppblock)
  111. {
  112. if (dsp_code->word_count + words_requested > dsp_code->block_length)
  113. return HPI_ERROR_DSP_FILE_FORMAT;
  114. *ppblock =
  115. ((u32 *)(dsp_code->pvt->firmware->data)) +
  116. dsp_code->word_count;
  117. dsp_code->word_count += words_requested;
  118. return 0;
  119. }