hpidspcd.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. struct dsp_code_private {
  24. /** Firmware descriptor */
  25. const struct firmware *firmware;
  26. struct pci_dev *dev;
  27. };
  28. #define HPI_VER_DECIMAL ((int)(HPI_VER_MAJOR(HPI_VER) * 10000 + \
  29. HPI_VER_MINOR(HPI_VER) * 100 + HPI_VER_RELEASE(HPI_VER)))
  30. /*-------------------------------------------------------------------*/
  31. short hpi_dsp_code_open(u32 adapter, void *os_data, struct dsp_code *dsp_code,
  32. u32 *os_error_code)
  33. {
  34. const struct firmware *firmware;
  35. struct pci_dev *dev = os_data;
  36. struct code_header header;
  37. char fw_name[20];
  38. short err_ret = HPI_ERROR_DSP_FILE_NOT_FOUND;
  39. int err;
  40. sprintf(fw_name, "asihpi/dsp%04x.bin", adapter);
  41. err = request_firmware(&firmware, fw_name, &dev->dev);
  42. if (err || !firmware) {
  43. dev_printk(KERN_ERR, &dev->dev,
  44. "%d, request_firmware failed for %s\n", err,
  45. fw_name);
  46. goto error1;
  47. }
  48. if (firmware->size < sizeof(header)) {
  49. dev_printk(KERN_ERR, &dev->dev, "Header size too small %s\n",
  50. fw_name);
  51. goto error2;
  52. }
  53. memcpy(&header, firmware->data, sizeof(header));
  54. if ((header.type != 0x45444F43) || /* "CODE" */
  55. (header.adapter != adapter)
  56. || (header.size != firmware->size)) {
  57. dev_printk(KERN_ERR, &dev->dev, "Invalid firmware file\n");
  58. goto error2;
  59. }
  60. if ((header.version / 100 & ~1) != (HPI_VER_DECIMAL / 100 & ~1)) {
  61. dev_printk(KERN_ERR, &dev->dev,
  62. "Incompatible firmware version "
  63. "DSP image %d != Driver %d\n", header.version,
  64. HPI_VER_DECIMAL);
  65. goto error2;
  66. }
  67. if (header.version != HPI_VER_DECIMAL) {
  68. dev_printk(KERN_WARNING, &dev->dev,
  69. "Firmware: release version mismatch DSP image %d != Driver %d\n",
  70. header.version, HPI_VER_DECIMAL);
  71. }
  72. HPI_DEBUG_LOG(DEBUG, "dsp code %s opened\n", fw_name);
  73. dsp_code->pvt = kmalloc(sizeof(*dsp_code->pvt), GFP_KERNEL);
  74. if (!dsp_code->pvt) {
  75. err_ret = HPI_ERROR_MEMORY_ALLOC;
  76. goto error2;
  77. }
  78. dsp_code->pvt->dev = dev;
  79. dsp_code->pvt->firmware = firmware;
  80. dsp_code->header = header;
  81. dsp_code->block_length = header.size / sizeof(u32);
  82. dsp_code->word_count = sizeof(header) / sizeof(u32);
  83. return 0;
  84. error2:
  85. release_firmware(firmware);
  86. error1:
  87. dsp_code->block_length = 0;
  88. return err_ret;
  89. }
  90. /*-------------------------------------------------------------------*/
  91. void hpi_dsp_code_close(struct dsp_code *dsp_code)
  92. {
  93. if (dsp_code->pvt->firmware) {
  94. HPI_DEBUG_LOG(DEBUG, "dsp code closed\n");
  95. release_firmware(dsp_code->pvt->firmware);
  96. dsp_code->pvt->firmware = NULL;
  97. }
  98. kfree(dsp_code->pvt);
  99. }
  100. /*-------------------------------------------------------------------*/
  101. void hpi_dsp_code_rewind(struct dsp_code *dsp_code)
  102. {
  103. /* Go back to start of data, after header */
  104. dsp_code->word_count = sizeof(struct code_header) / sizeof(u32);
  105. }
  106. /*-------------------------------------------------------------------*/
  107. short hpi_dsp_code_read_word(struct dsp_code *dsp_code, u32 *pword)
  108. {
  109. if (dsp_code->word_count + 1 > dsp_code->block_length)
  110. return HPI_ERROR_DSP_FILE_FORMAT;
  111. *pword = ((u32 *)(dsp_code->pvt->firmware->data))[dsp_code->
  112. word_count];
  113. dsp_code->word_count++;
  114. return 0;
  115. }
  116. /*-------------------------------------------------------------------*/
  117. short hpi_dsp_code_read_block(size_t words_requested,
  118. struct dsp_code *dsp_code, u32 **ppblock)
  119. {
  120. if (dsp_code->word_count + words_requested > dsp_code->block_length)
  121. return HPI_ERROR_DSP_FILE_FORMAT;
  122. *ppblock =
  123. ((u32 *)(dsp_code->pvt->firmware->data)) +
  124. dsp_code->word_count;
  125. dsp_code->word_count += words_requested;
  126. return 0;
  127. }