hpidspcd.c 4.3 KB

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