hpidspcd.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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_printk(KERN_ERR, &dev->dev,
  43. "%d, request_firmware failed for %s\n", err,
  44. fw_name);
  45. goto error1;
  46. }
  47. if (firmware->size < sizeof(header)) {
  48. dev_printk(KERN_ERR, &dev->dev, "Header size too small %s\n",
  49. fw_name);
  50. goto error2;
  51. }
  52. memcpy(&header, firmware->data, sizeof(header));
  53. if ((header.type != 0x45444F43) || /* "CODE" */
  54. (header.adapter != adapter)
  55. || (header.size != firmware->size)) {
  56. dev_printk(KERN_ERR, &dev->dev,
  57. "Invalid firmware header size %d != file %zd\n",
  58. header.size, firmware->size);
  59. goto error2;
  60. }
  61. if ((header.version >> 9) != (HPI_VER >> 9)) {
  62. /* Consider even and subsequent odd minor versions to be compatible */
  63. dev_printk(KERN_ERR, &dev->dev,
  64. "Incompatible firmware version "
  65. "DSP image %X != Driver %X\n", header.version,
  66. HPI_VER);
  67. goto error2;
  68. }
  69. if (header.version != HPI_VER) {
  70. dev_printk(KERN_INFO, &dev->dev,
  71. "Firmware: release version mismatch DSP image %X != Driver %X\n",
  72. header.version, HPI_VER);
  73. }
  74. HPI_DEBUG_LOG(DEBUG, "dsp code %s opened\n", fw_name);
  75. dsp_code->pvt = kmalloc(sizeof(*dsp_code->pvt), GFP_KERNEL);
  76. if (!dsp_code->pvt) {
  77. err_ret = HPI_ERROR_MEMORY_ALLOC;
  78. goto error2;
  79. }
  80. dsp_code->pvt->dev = dev;
  81. dsp_code->pvt->firmware = firmware;
  82. dsp_code->header = header;
  83. dsp_code->block_length = header.size / sizeof(u32);
  84. dsp_code->word_count = sizeof(header) / sizeof(u32);
  85. return 0;
  86. error2:
  87. release_firmware(firmware);
  88. error1:
  89. dsp_code->block_length = 0;
  90. return err_ret;
  91. }
  92. /*-------------------------------------------------------------------*/
  93. void hpi_dsp_code_close(struct dsp_code *dsp_code)
  94. {
  95. HPI_DEBUG_LOG(DEBUG, "dsp code closed\n");
  96. release_firmware(dsp_code->pvt->firmware);
  97. kfree(dsp_code->pvt);
  98. }
  99. /*-------------------------------------------------------------------*/
  100. void hpi_dsp_code_rewind(struct dsp_code *dsp_code)
  101. {
  102. /* Go back to start of data, after header */
  103. dsp_code->word_count = sizeof(struct code_header) / sizeof(u32);
  104. }
  105. /*-------------------------------------------------------------------*/
  106. short hpi_dsp_code_read_word(struct dsp_code *dsp_code, u32 *pword)
  107. {
  108. if (dsp_code->word_count + 1 > dsp_code->block_length)
  109. return HPI_ERROR_DSP_FILE_FORMAT;
  110. *pword = ((u32 *)(dsp_code->pvt->firmware->data))[dsp_code->
  111. word_count];
  112. dsp_code->word_count++;
  113. return 0;
  114. }
  115. /*-------------------------------------------------------------------*/
  116. short hpi_dsp_code_read_block(size_t words_requested,
  117. struct dsp_code *dsp_code, u32 **ppblock)
  118. {
  119. if (dsp_code->word_count + words_requested > dsp_code->block_length)
  120. return HPI_ERROR_DSP_FILE_FORMAT;
  121. *ppblock =
  122. ((u32 *)(dsp_code->pvt->firmware->data)) +
  123. dsp_code->word_count;
  124. dsp_code->word_count += words_requested;
  125. return 0;
  126. }