hpidspcd.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. 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, "Invalid firmware file\n");
  57. goto error2;
  58. }
  59. if ((header.version / 100 & ~1) != (HPI_VER_DECIMAL / 100 & ~1)) {
  60. dev_printk(KERN_ERR, &dev->dev,
  61. "Incompatible firmware version "
  62. "DSP image %d != Driver %d\n", header.version,
  63. HPI_VER_DECIMAL);
  64. goto error2;
  65. }
  66. if (header.version != HPI_VER_DECIMAL) {
  67. dev_printk(KERN_WARNING, &dev->dev,
  68. "Firmware: release version mismatch DSP image %d != Driver %d\n",
  69. header.version, HPI_VER_DECIMAL);
  70. }
  71. HPI_DEBUG_LOG(DEBUG, "dsp code %s opened\n", fw_name);
  72. dsp_code->pvt = kmalloc(sizeof(*dsp_code->pvt), GFP_KERNEL);
  73. if (!dsp_code->pvt)
  74. return HPI_ERROR_MEMORY_ALLOC;
  75. dsp_code->pvt->dev = dev;
  76. dsp_code->pvt->firmware = firmware;
  77. dsp_code->header = header;
  78. dsp_code->block_length = header.size / sizeof(u32);
  79. dsp_code->word_count = sizeof(header) / sizeof(u32);
  80. return 0;
  81. error2:
  82. release_firmware(firmware);
  83. error1:
  84. dsp_code->block_length = 0;
  85. return HPI_ERROR_DSP_FILE_NOT_FOUND;
  86. }
  87. /*-------------------------------------------------------------------*/
  88. void hpi_dsp_code_close(struct dsp_code *dsp_code)
  89. {
  90. if (dsp_code->pvt->firmware) {
  91. HPI_DEBUG_LOG(DEBUG, "dsp code closed\n");
  92. release_firmware(dsp_code->pvt->firmware);
  93. dsp_code->pvt->firmware = NULL;
  94. }
  95. kfree(dsp_code->pvt);
  96. }
  97. /*-------------------------------------------------------------------*/
  98. void hpi_dsp_code_rewind(struct dsp_code *dsp_code)
  99. {
  100. /* Go back to start of data, after header */
  101. dsp_code->word_count = sizeof(struct code_header) / sizeof(u32);
  102. }
  103. /*-------------------------------------------------------------------*/
  104. short hpi_dsp_code_read_word(struct dsp_code *dsp_code, u32 *pword)
  105. {
  106. if (dsp_code->word_count + 1 > dsp_code->block_length)
  107. return HPI_ERROR_DSP_FILE_FORMAT;
  108. *pword = ((u32 *)(dsp_code->pvt->firmware->data))[dsp_code->
  109. word_count];
  110. dsp_code->word_count++;
  111. return 0;
  112. }
  113. /*-------------------------------------------------------------------*/
  114. short hpi_dsp_code_read_block(size_t words_requested,
  115. struct dsp_code *dsp_code, u32 **ppblock)
  116. {
  117. if (dsp_code->word_count + words_requested > dsp_code->block_length)
  118. return HPI_ERROR_DSP_FILE_FORMAT;
  119. *ppblock =
  120. ((u32 *)(dsp_code->pvt->firmware->data)) +
  121. dsp_code->word_count;
  122. dsp_code->word_count += words_requested;
  123. return 0;
  124. }