Eliot Blennerhassett | 4398643 | 2014-12-18 17:57:13 +1300 | [diff] [blame] | 1 | /*********************************************************************** |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 2 | |
| 3 | AudioScience HPI driver |
Eliot Blennerhassett | 4398643 | 2014-12-18 17:57:13 +1300 | [diff] [blame] | 4 | Functions for reading DSP code using hotplug firmware loader |
| 5 | |
| 6 | Copyright (C) 1997-2014 AudioScience Inc. <support@audioscience.com> |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 7 | |
| 8 | This program is free software; you can redistribute it and/or modify |
| 9 | it under the terms of version 2 of the GNU General Public License as |
| 10 | published by the Free Software Foundation; |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with this program; if not, write to the Free Software |
| 19 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | |
Eliot Blennerhassett | 4398643 | 2014-12-18 17:57:13 +1300 | [diff] [blame] | 21 | ***********************************************************************/ |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 22 | #define SOURCEFILE_NAME "hpidspcd.c" |
| 23 | #include "hpidspcd.h" |
| 24 | #include "hpidebug.h" |
Eliot Blennerhassett | f6baaec | 2011-12-22 13:38:31 +1300 | [diff] [blame] | 25 | #include "hpi_version.h" |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 26 | |
Eliot Blennerhassett | 95a4c6e | 2011-07-22 15:52:42 +1200 | [diff] [blame] | 27 | struct dsp_code_private { |
| 28 | /** Firmware descriptor */ |
| 29 | const struct firmware *firmware; |
| 30 | struct pci_dev *dev; |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 31 | }; |
| 32 | |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 33 | /*-------------------------------------------------------------------*/ |
Eliot Blennerhassett | 95a4c6e | 2011-07-22 15:52:42 +1200 | [diff] [blame] | 34 | short hpi_dsp_code_open(u32 adapter, void *os_data, struct dsp_code *dsp_code, |
| 35 | u32 *os_error_code) |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 36 | { |
Eliot Blennerhassett | 95a4c6e | 2011-07-22 15:52:42 +1200 | [diff] [blame] | 37 | const struct firmware *firmware; |
| 38 | struct pci_dev *dev = os_data; |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 39 | struct code_header header; |
| 40 | char fw_name[20]; |
Jesper Juhl | dc889f1 | 2011-07-31 23:16:43 +0200 | [diff] [blame] | 41 | short err_ret = HPI_ERROR_DSP_FILE_NOT_FOUND; |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 42 | int err; |
| 43 | |
| 44 | sprintf(fw_name, "asihpi/dsp%04x.bin", adapter); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 45 | |
Eliot Blennerhassett | 95a4c6e | 2011-07-22 15:52:42 +1200 | [diff] [blame] | 46 | err = request_firmware(&firmware, fw_name, &dev->dev); |
Eliot Blennerhassett | 5ed15da | 2011-02-10 17:25:54 +1300 | [diff] [blame] | 47 | |
Eliot Blennerhassett | 95a4c6e | 2011-07-22 15:52:42 +1200 | [diff] [blame] | 48 | if (err || !firmware) { |
Joe Perches | 4ee3bff | 2012-10-28 01:05:54 -0700 | [diff] [blame] | 49 | dev_err(&dev->dev, "%d, request_firmware failed for %s\n", |
| 50 | err, fw_name); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 51 | goto error1; |
| 52 | } |
Eliot Blennerhassett | 95a4c6e | 2011-07-22 15:52:42 +1200 | [diff] [blame] | 53 | if (firmware->size < sizeof(header)) { |
Joe Perches | 4ee3bff | 2012-10-28 01:05:54 -0700 | [diff] [blame] | 54 | dev_err(&dev->dev, "Header size too small %s\n", fw_name); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 55 | goto error2; |
| 56 | } |
Eliot Blennerhassett | 95a4c6e | 2011-07-22 15:52:42 +1200 | [diff] [blame] | 57 | memcpy(&header, firmware->data, sizeof(header)); |
| 58 | |
| 59 | if ((header.type != 0x45444F43) || /* "CODE" */ |
| 60 | (header.adapter != adapter) |
| 61 | || (header.size != firmware->size)) { |
Joe Perches | 4ee3bff | 2012-10-28 01:05:54 -0700 | [diff] [blame] | 62 | dev_err(&dev->dev, |
Eliot Blennerhassett | f6baaec | 2011-12-22 13:38:31 +1300 | [diff] [blame] | 63 | "Invalid firmware header size %d != file %zd\n", |
| 64 | header.size, firmware->size); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 65 | goto error2; |
| 66 | } |
| 67 | |
Eliot Blennerhassett | 4398643 | 2014-12-18 17:57:13 +1300 | [diff] [blame] | 68 | if (HPI_VER_MAJOR(header.version) != HPI_VER_MAJOR(HPI_VER)) { |
| 69 | /* Major version change probably means Host-DSP protocol change */ |
| 70 | dev_err(&dev->dev, |
| 71 | "Incompatible firmware version DSP image %X != Driver %X\n", |
Joe Perches | 4ee3bff | 2012-10-28 01:05:54 -0700 | [diff] [blame] | 72 | header.version, HPI_VER); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 73 | goto error2; |
| 74 | } |
| 75 | |
Eliot Blennerhassett | f6baaec | 2011-12-22 13:38:31 +1300 | [diff] [blame] | 76 | if (header.version != HPI_VER) { |
Eliot Blennerhassett | 4398643 | 2014-12-18 17:57:13 +1300 | [diff] [blame] | 77 | dev_warn(&dev->dev, |
| 78 | "Firmware version mismatch: DSP image %X != Driver %X\n", |
| 79 | header.version, HPI_VER); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 80 | } |
| 81 | |
Eliot Blennerhassett | 5ed15da | 2011-02-10 17:25:54 +1300 | [diff] [blame] | 82 | HPI_DEBUG_LOG(DEBUG, "dsp code %s opened\n", fw_name); |
Eliot Blennerhassett | 95a4c6e | 2011-07-22 15:52:42 +1200 | [diff] [blame] | 83 | dsp_code->pvt = kmalloc(sizeof(*dsp_code->pvt), GFP_KERNEL); |
Jesper Juhl | dc889f1 | 2011-07-31 23:16:43 +0200 | [diff] [blame] | 84 | if (!dsp_code->pvt) { |
| 85 | err_ret = HPI_ERROR_MEMORY_ALLOC; |
| 86 | goto error2; |
| 87 | } |
Eliot Blennerhassett | 95a4c6e | 2011-07-22 15:52:42 +1200 | [diff] [blame] | 88 | |
| 89 | dsp_code->pvt->dev = dev; |
| 90 | dsp_code->pvt->firmware = firmware; |
| 91 | dsp_code->header = header; |
| 92 | dsp_code->block_length = header.size / sizeof(u32); |
| 93 | dsp_code->word_count = sizeof(header) / sizeof(u32); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 94 | return 0; |
| 95 | |
| 96 | error2: |
Eliot Blennerhassett | 95a4c6e | 2011-07-22 15:52:42 +1200 | [diff] [blame] | 97 | release_firmware(firmware); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 98 | error1: |
Eliot Blennerhassett | 95a4c6e | 2011-07-22 15:52:42 +1200 | [diff] [blame] | 99 | dsp_code->block_length = 0; |
Jesper Juhl | dc889f1 | 2011-07-31 23:16:43 +0200 | [diff] [blame] | 100 | return err_ret; |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /*-------------------------------------------------------------------*/ |
Eliot Blennerhassett | 95a4c6e | 2011-07-22 15:52:42 +1200 | [diff] [blame] | 104 | void hpi_dsp_code_close(struct dsp_code *dsp_code) |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 105 | { |
Eliot Blennerhassett | 50d5f77 | 2011-12-22 13:38:44 +1300 | [diff] [blame] | 106 | HPI_DEBUG_LOG(DEBUG, "dsp code closed\n"); |
| 107 | release_firmware(dsp_code->pvt->firmware); |
Eliot Blennerhassett | 95a4c6e | 2011-07-22 15:52:42 +1200 | [diff] [blame] | 108 | kfree(dsp_code->pvt); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | /*-------------------------------------------------------------------*/ |
Eliot Blennerhassett | 95a4c6e | 2011-07-22 15:52:42 +1200 | [diff] [blame] | 112 | void hpi_dsp_code_rewind(struct dsp_code *dsp_code) |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 113 | { |
| 114 | /* Go back to start of data, after header */ |
Eliot Blennerhassett | 95a4c6e | 2011-07-22 15:52:42 +1200 | [diff] [blame] | 115 | dsp_code->word_count = sizeof(struct code_header) / sizeof(u32); |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | /*-------------------------------------------------------------------*/ |
Eliot Blennerhassett | 95a4c6e | 2011-07-22 15:52:42 +1200 | [diff] [blame] | 119 | short hpi_dsp_code_read_word(struct dsp_code *dsp_code, u32 *pword) |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 120 | { |
Eliot Blennerhassett | 95a4c6e | 2011-07-22 15:52:42 +1200 | [diff] [blame] | 121 | if (dsp_code->word_count + 1 > dsp_code->block_length) |
Eliot Blennerhassett | 5ed15da | 2011-02-10 17:25:54 +1300 | [diff] [blame] | 122 | return HPI_ERROR_DSP_FILE_FORMAT; |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 123 | |
Eliot Blennerhassett | 95a4c6e | 2011-07-22 15:52:42 +1200 | [diff] [blame] | 124 | *pword = ((u32 *)(dsp_code->pvt->firmware->data))[dsp_code-> |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 125 | word_count]; |
Eliot Blennerhassett | 95a4c6e | 2011-07-22 15:52:42 +1200 | [diff] [blame] | 126 | dsp_code->word_count++; |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | /*-------------------------------------------------------------------*/ |
| 131 | short hpi_dsp_code_read_block(size_t words_requested, |
Eliot Blennerhassett | 95a4c6e | 2011-07-22 15:52:42 +1200 | [diff] [blame] | 132 | struct dsp_code *dsp_code, u32 **ppblock) |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 133 | { |
Eliot Blennerhassett | 95a4c6e | 2011-07-22 15:52:42 +1200 | [diff] [blame] | 134 | if (dsp_code->word_count + words_requested > dsp_code->block_length) |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 135 | return HPI_ERROR_DSP_FILE_FORMAT; |
| 136 | |
| 137 | *ppblock = |
Eliot Blennerhassett | 95a4c6e | 2011-07-22 15:52:42 +1200 | [diff] [blame] | 138 | ((u32 *)(dsp_code->pvt->firmware->data)) + |
| 139 | dsp_code->word_count; |
| 140 | dsp_code->word_count += words_requested; |
Eliot Blennerhassett | 719f82d | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 141 | return 0; |
| 142 | } |