Thomas Gleixner | 07d7fe7 | 2019-05-29 07:12:47 -0700 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Eliot Blennerhassett | 4398643 | 2014-12-18 17:57:13 +1300 | [diff] [blame] | 2 | /*********************************************************************** |
Eliot Blennerhassett | 719f82d3 | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 3 | |
| 4 | AudioScience HPI driver |
Eliot Blennerhassett | 4398643 | 2014-12-18 17:57:13 +1300 | [diff] [blame] | 5 | Functions for reading DSP code using hotplug firmware loader |
| 6 | |
| 7 | Copyright (C) 1997-2014 AudioScience Inc. <support@audioscience.com> |
Eliot Blennerhassett | 719f82d3 | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 8 | |
Eliot Blennerhassett | 719f82d3 | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 9 | |
Eliot Blennerhassett | 4398643 | 2014-12-18 17:57:13 +1300 | [diff] [blame] | 10 | ***********************************************************************/ |
Eliot Blennerhassett | 719f82d3 | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 11 | #define SOURCEFILE_NAME "hpidspcd.c" |
| 12 | #include "hpidspcd.h" |
| 13 | #include "hpidebug.h" |
Eliot Blennerhassett | f6baaec | 2011-12-22 13:38:31 +1300 | [diff] [blame] | 14 | #include "hpi_version.h" |
Eliot Blennerhassett | 719f82d3 | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 15 | |
Eliot Blennerhassett | 95a4c6e | 2011-07-22 15:52:42 +1200 | [diff] [blame] | 16 | struct dsp_code_private { |
| 17 | /** Firmware descriptor */ |
| 18 | const struct firmware *firmware; |
| 19 | struct pci_dev *dev; |
Eliot Blennerhassett | 719f82d3 | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 20 | }; |
| 21 | |
Eliot Blennerhassett | 719f82d3 | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 22 | /*-------------------------------------------------------------------*/ |
Eliot Blennerhassett | 95a4c6e | 2011-07-22 15:52:42 +1200 | [diff] [blame] | 23 | short hpi_dsp_code_open(u32 adapter, void *os_data, struct dsp_code *dsp_code, |
| 24 | u32 *os_error_code) |
Eliot Blennerhassett | 719f82d3 | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 25 | { |
Eliot Blennerhassett | 95a4c6e | 2011-07-22 15:52:42 +1200 | [diff] [blame] | 26 | const struct firmware *firmware; |
| 27 | struct pci_dev *dev = os_data; |
Eliot Blennerhassett | 719f82d3 | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 28 | struct code_header header; |
| 29 | char fw_name[20]; |
Jesper Juhl | dc889f1 | 2011-07-31 23:16:43 +0200 | [diff] [blame] | 30 | short err_ret = HPI_ERROR_DSP_FILE_NOT_FOUND; |
Eliot Blennerhassett | 719f82d3 | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 31 | int err; |
| 32 | |
| 33 | sprintf(fw_name, "asihpi/dsp%04x.bin", adapter); |
Eliot Blennerhassett | 719f82d3 | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 34 | |
Eliot Blennerhassett | 95a4c6e | 2011-07-22 15:52:42 +1200 | [diff] [blame] | 35 | err = request_firmware(&firmware, fw_name, &dev->dev); |
Eliot Blennerhassett | 5ed15da | 2011-02-10 17:25:54 +1300 | [diff] [blame] | 36 | |
Eliot Blennerhassett | 95a4c6e | 2011-07-22 15:52:42 +1200 | [diff] [blame] | 37 | if (err || !firmware) { |
Joe Perches | 4ee3bff | 2012-10-28 01:05:54 -0700 | [diff] [blame] | 38 | dev_err(&dev->dev, "%d, request_firmware failed for %s\n", |
| 39 | err, fw_name); |
Eliot Blennerhassett | 719f82d3 | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 40 | goto error1; |
| 41 | } |
Eliot Blennerhassett | 95a4c6e | 2011-07-22 15:52:42 +1200 | [diff] [blame] | 42 | if (firmware->size < sizeof(header)) { |
Joe Perches | 4ee3bff | 2012-10-28 01:05:54 -0700 | [diff] [blame] | 43 | dev_err(&dev->dev, "Header size too small %s\n", fw_name); |
Eliot Blennerhassett | 719f82d3 | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 44 | goto error2; |
| 45 | } |
Eliot Blennerhassett | 95a4c6e | 2011-07-22 15:52:42 +1200 | [diff] [blame] | 46 | memcpy(&header, firmware->data, sizeof(header)); |
| 47 | |
| 48 | if ((header.type != 0x45444F43) || /* "CODE" */ |
| 49 | (header.adapter != adapter) |
| 50 | || (header.size != firmware->size)) { |
Joe Perches | 4ee3bff | 2012-10-28 01:05:54 -0700 | [diff] [blame] | 51 | dev_err(&dev->dev, |
Eliot Blennerhassett | f6baaec | 2011-12-22 13:38:31 +1300 | [diff] [blame] | 52 | "Invalid firmware header size %d != file %zd\n", |
| 53 | header.size, firmware->size); |
Eliot Blennerhassett | 719f82d3 | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 54 | goto error2; |
| 55 | } |
| 56 | |
Eliot Blennerhassett | 4398643 | 2014-12-18 17:57:13 +1300 | [diff] [blame] | 57 | if (HPI_VER_MAJOR(header.version) != HPI_VER_MAJOR(HPI_VER)) { |
| 58 | /* Major version change probably means Host-DSP protocol change */ |
| 59 | dev_err(&dev->dev, |
| 60 | "Incompatible firmware version DSP image %X != Driver %X\n", |
Joe Perches | 4ee3bff | 2012-10-28 01:05:54 -0700 | [diff] [blame] | 61 | header.version, HPI_VER); |
Eliot Blennerhassett | 719f82d3 | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 62 | goto error2; |
| 63 | } |
| 64 | |
Eliot Blennerhassett | f6baaec | 2011-12-22 13:38:31 +1300 | [diff] [blame] | 65 | if (header.version != HPI_VER) { |
Eliot Blennerhassett | 4398643 | 2014-12-18 17:57:13 +1300 | [diff] [blame] | 66 | dev_warn(&dev->dev, |
| 67 | "Firmware version mismatch: DSP image %X != Driver %X\n", |
| 68 | header.version, HPI_VER); |
Eliot Blennerhassett | 719f82d3 | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 69 | } |
| 70 | |
Eliot Blennerhassett | 5ed15da | 2011-02-10 17:25:54 +1300 | [diff] [blame] | 71 | HPI_DEBUG_LOG(DEBUG, "dsp code %s opened\n", fw_name); |
Eliot Blennerhassett | 95a4c6e | 2011-07-22 15:52:42 +1200 | [diff] [blame] | 72 | dsp_code->pvt = kmalloc(sizeof(*dsp_code->pvt), GFP_KERNEL); |
Jesper Juhl | dc889f1 | 2011-07-31 23:16:43 +0200 | [diff] [blame] | 73 | if (!dsp_code->pvt) { |
| 74 | err_ret = HPI_ERROR_MEMORY_ALLOC; |
| 75 | goto error2; |
| 76 | } |
Eliot Blennerhassett | 95a4c6e | 2011-07-22 15:52:42 +1200 | [diff] [blame] | 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); |
Eliot Blennerhassett | 719f82d3 | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 83 | return 0; |
| 84 | |
| 85 | error2: |
Eliot Blennerhassett | 95a4c6e | 2011-07-22 15:52:42 +1200 | [diff] [blame] | 86 | release_firmware(firmware); |
Eliot Blennerhassett | 719f82d3 | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 87 | error1: |
Eliot Blennerhassett | 95a4c6e | 2011-07-22 15:52:42 +1200 | [diff] [blame] | 88 | dsp_code->block_length = 0; |
Jesper Juhl | dc889f1 | 2011-07-31 23:16:43 +0200 | [diff] [blame] | 89 | return err_ret; |
Eliot Blennerhassett | 719f82d3 | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | /*-------------------------------------------------------------------*/ |
Eliot Blennerhassett | 95a4c6e | 2011-07-22 15:52:42 +1200 | [diff] [blame] | 93 | void hpi_dsp_code_close(struct dsp_code *dsp_code) |
Eliot Blennerhassett | 719f82d3 | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 94 | { |
Eliot Blennerhassett | 50d5f77 | 2011-12-22 13:38:44 +1300 | [diff] [blame] | 95 | HPI_DEBUG_LOG(DEBUG, "dsp code closed\n"); |
| 96 | release_firmware(dsp_code->pvt->firmware); |
Eliot Blennerhassett | 95a4c6e | 2011-07-22 15:52:42 +1200 | [diff] [blame] | 97 | kfree(dsp_code->pvt); |
Eliot Blennerhassett | 719f82d3 | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /*-------------------------------------------------------------------*/ |
Eliot Blennerhassett | 95a4c6e | 2011-07-22 15:52:42 +1200 | [diff] [blame] | 101 | void hpi_dsp_code_rewind(struct dsp_code *dsp_code) |
Eliot Blennerhassett | 719f82d3 | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 102 | { |
| 103 | /* Go back to start of data, after header */ |
Eliot Blennerhassett | 95a4c6e | 2011-07-22 15:52:42 +1200 | [diff] [blame] | 104 | dsp_code->word_count = sizeof(struct code_header) / sizeof(u32); |
Eliot Blennerhassett | 719f82d3 | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /*-------------------------------------------------------------------*/ |
Eliot Blennerhassett | 95a4c6e | 2011-07-22 15:52:42 +1200 | [diff] [blame] | 108 | short hpi_dsp_code_read_word(struct dsp_code *dsp_code, u32 *pword) |
Eliot Blennerhassett | 719f82d3 | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 109 | { |
Eliot Blennerhassett | 95a4c6e | 2011-07-22 15:52:42 +1200 | [diff] [blame] | 110 | if (dsp_code->word_count + 1 > dsp_code->block_length) |
Eliot Blennerhassett | 5ed15da | 2011-02-10 17:25:54 +1300 | [diff] [blame] | 111 | return HPI_ERROR_DSP_FILE_FORMAT; |
Eliot Blennerhassett | 719f82d3 | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 112 | |
Eliot Blennerhassett | 95a4c6e | 2011-07-22 15:52:42 +1200 | [diff] [blame] | 113 | *pword = ((u32 *)(dsp_code->pvt->firmware->data))[dsp_code-> |
Eliot Blennerhassett | 719f82d3 | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 114 | word_count]; |
Eliot Blennerhassett | 95a4c6e | 2011-07-22 15:52:42 +1200 | [diff] [blame] | 115 | dsp_code->word_count++; |
Eliot Blennerhassett | 719f82d3 | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | /*-------------------------------------------------------------------*/ |
| 120 | short hpi_dsp_code_read_block(size_t words_requested, |
Eliot Blennerhassett | 95a4c6e | 2011-07-22 15:52:42 +1200 | [diff] [blame] | 121 | struct dsp_code *dsp_code, u32 **ppblock) |
Eliot Blennerhassett | 719f82d3 | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 122 | { |
Eliot Blennerhassett | 95a4c6e | 2011-07-22 15:52:42 +1200 | [diff] [blame] | 123 | if (dsp_code->word_count + words_requested > dsp_code->block_length) |
Eliot Blennerhassett | 719f82d3 | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 124 | return HPI_ERROR_DSP_FILE_FORMAT; |
| 125 | |
| 126 | *ppblock = |
Eliot Blennerhassett | 95a4c6e | 2011-07-22 15:52:42 +1200 | [diff] [blame] | 127 | ((u32 *)(dsp_code->pvt->firmware->data)) + |
| 128 | dsp_code->word_count; |
| 129 | dsp_code->word_count += words_requested; |
Eliot Blennerhassett | 719f82d3 | 2010-04-21 18:17:39 +0200 | [diff] [blame] | 130 | return 0; |
| 131 | } |