blob: ac9163770013a8adeab493ff4585f70899afd6c0 [file] [log] [blame]
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +02001/***********************************************************************/
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +12002/**
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +02003
4 AudioScience HPI driver
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +12005 Copyright (C) 1997-2011 AudioScience Inc. <support@audioscience.com>
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +02006
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of version 2 of the GNU General Public License as
9 published by the Free Software Foundation;
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
20\file
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +120021Functions for reading DSP code using
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +020022hotplug firmware loader from individual dsp code files
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +120023*/
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +020024/***********************************************************************/
25#define SOURCEFILE_NAME "hpidspcd.c"
26#include "hpidspcd.h"
27#include "hpidebug.h"
Eliot Blennerhassettf6baaec2011-12-22 13:38:31 +130028#include "hpi_version.h"
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +020029
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +120030struct dsp_code_private {
31 /** Firmware descriptor */
32 const struct firmware *firmware;
33 struct pci_dev *dev;
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +020034};
35
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +020036/*-------------------------------------------------------------------*/
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +120037short hpi_dsp_code_open(u32 adapter, void *os_data, struct dsp_code *dsp_code,
38 u32 *os_error_code)
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +020039{
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +120040 const struct firmware *firmware;
41 struct pci_dev *dev = os_data;
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +020042 struct code_header header;
43 char fw_name[20];
Jesper Juhldc889f12011-07-31 23:16:43 +020044 short err_ret = HPI_ERROR_DSP_FILE_NOT_FOUND;
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +020045 int err;
46
47 sprintf(fw_name, "asihpi/dsp%04x.bin", adapter);
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +020048
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +120049 err = request_firmware(&firmware, fw_name, &dev->dev);
Eliot Blennerhassett5ed15da2011-02-10 17:25:54 +130050
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +120051 if (err || !firmware) {
Joe Perches4ee3bff2012-10-28 01:05:54 -070052 dev_err(&dev->dev, "%d, request_firmware failed for %s\n",
53 err, fw_name);
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +020054 goto error1;
55 }
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +120056 if (firmware->size < sizeof(header)) {
Joe Perches4ee3bff2012-10-28 01:05:54 -070057 dev_err(&dev->dev, "Header size too small %s\n", fw_name);
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +020058 goto error2;
59 }
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +120060 memcpy(&header, firmware->data, sizeof(header));
61
62 if ((header.type != 0x45444F43) || /* "CODE" */
63 (header.adapter != adapter)
64 || (header.size != firmware->size)) {
Joe Perches4ee3bff2012-10-28 01:05:54 -070065 dev_err(&dev->dev,
Eliot Blennerhassettf6baaec2011-12-22 13:38:31 +130066 "Invalid firmware header size %d != file %zd\n",
67 header.size, firmware->size);
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +020068 goto error2;
69 }
70
Eliot Blennerhassettf6baaec2011-12-22 13:38:31 +130071 if ((header.version >> 9) != (HPI_VER >> 9)) {
72 /* Consider even and subsequent odd minor versions to be compatible */
Joe Perches4ee3bff2012-10-28 01:05:54 -070073 dev_err(&dev->dev, "Incompatible firmware version DSP image %X != Driver %X\n",
74 header.version, HPI_VER);
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +020075 goto error2;
76 }
77
Eliot Blennerhassettf6baaec2011-12-22 13:38:31 +130078 if (header.version != HPI_VER) {
Joe Perches4ee3bff2012-10-28 01:05:54 -070079 dev_info(&dev->dev,
80 "Firmware: release version mismatch DSP image %X != Driver %X\n",
81 header.version, HPI_VER);
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +020082 }
83
Eliot Blennerhassett5ed15da2011-02-10 17:25:54 +130084 HPI_DEBUG_LOG(DEBUG, "dsp code %s opened\n", fw_name);
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +120085 dsp_code->pvt = kmalloc(sizeof(*dsp_code->pvt), GFP_KERNEL);
Jesper Juhldc889f12011-07-31 23:16:43 +020086 if (!dsp_code->pvt) {
87 err_ret = HPI_ERROR_MEMORY_ALLOC;
88 goto error2;
89 }
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +120090
91 dsp_code->pvt->dev = dev;
92 dsp_code->pvt->firmware = firmware;
93 dsp_code->header = header;
94 dsp_code->block_length = header.size / sizeof(u32);
95 dsp_code->word_count = sizeof(header) / sizeof(u32);
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +020096 return 0;
97
98error2:
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +120099 release_firmware(firmware);
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +0200100error1:
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +1200101 dsp_code->block_length = 0;
Jesper Juhldc889f12011-07-31 23:16:43 +0200102 return err_ret;
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +0200103}
104
105/*-------------------------------------------------------------------*/
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +1200106void hpi_dsp_code_close(struct dsp_code *dsp_code)
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +0200107{
Eliot Blennerhassett50d5f772011-12-22 13:38:44 +1300108 HPI_DEBUG_LOG(DEBUG, "dsp code closed\n");
109 release_firmware(dsp_code->pvt->firmware);
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +1200110 kfree(dsp_code->pvt);
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +0200111}
112
113/*-------------------------------------------------------------------*/
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +1200114void hpi_dsp_code_rewind(struct dsp_code *dsp_code)
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +0200115{
116 /* Go back to start of data, after header */
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +1200117 dsp_code->word_count = sizeof(struct code_header) / sizeof(u32);
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +0200118}
119
120/*-------------------------------------------------------------------*/
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +1200121short hpi_dsp_code_read_word(struct dsp_code *dsp_code, u32 *pword)
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +0200122{
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +1200123 if (dsp_code->word_count + 1 > dsp_code->block_length)
Eliot Blennerhassett5ed15da2011-02-10 17:25:54 +1300124 return HPI_ERROR_DSP_FILE_FORMAT;
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +0200125
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +1200126 *pword = ((u32 *)(dsp_code->pvt->firmware->data))[dsp_code->
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +0200127 word_count];
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +1200128 dsp_code->word_count++;
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +0200129 return 0;
130}
131
132/*-------------------------------------------------------------------*/
133short hpi_dsp_code_read_block(size_t words_requested,
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +1200134 struct dsp_code *dsp_code, u32 **ppblock)
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +0200135{
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +1200136 if (dsp_code->word_count + words_requested > dsp_code->block_length)
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +0200137 return HPI_ERROR_DSP_FILE_FORMAT;
138
139 *ppblock =
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +1200140 ((u32 *)(dsp_code->pvt->firmware->data)) +
141 dsp_code->word_count;
142 dsp_code->word_count += words_requested;
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +0200143 return 0;
144}