blob: 71d32c868c9244e19160687922fb7c7fa86ca97c [file] [log] [blame]
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001/***********************************************************************/
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +12002/**
Eliot Blennerhassett719f82d2010-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 Blennerhassett719f82d2010-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 Blennerhassett719f82d2010-04-21 18:17:39 +020022hotplug firmware loader from individual dsp code files
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +120023*/
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020024/***********************************************************************/
25#define SOURCEFILE_NAME "hpidspcd.c"
26#include "hpidspcd.h"
27#include "hpidebug.h"
28
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +120029struct dsp_code_private {
30 /** Firmware descriptor */
31 const struct firmware *firmware;
32 struct pci_dev *dev;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020033};
34
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020035#define HPI_VER_DECIMAL ((int)(HPI_VER_MAJOR(HPI_VER) * 10000 + \
36 HPI_VER_MINOR(HPI_VER) * 100 + HPI_VER_RELEASE(HPI_VER)))
37
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020038/*-------------------------------------------------------------------*/
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +120039short hpi_dsp_code_open(u32 adapter, void *os_data, struct dsp_code *dsp_code,
40 u32 *os_error_code)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020041{
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +120042 const struct firmware *firmware;
43 struct pci_dev *dev = os_data;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020044 struct code_header header;
45 char fw_name[20];
Jesper Juhldc889f12011-07-31 23:16:43 +020046 short err_ret = HPI_ERROR_DSP_FILE_NOT_FOUND;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020047 int err;
48
49 sprintf(fw_name, "asihpi/dsp%04x.bin", adapter);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020050
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +120051 err = request_firmware(&firmware, fw_name, &dev->dev);
Eliot Blennerhassett5ed15da2011-02-10 17:25:54 +130052
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +120053 if (err || !firmware) {
54 dev_printk(KERN_ERR, &dev->dev,
Eliot Blennerhassett5ed15da2011-02-10 17:25:54 +130055 "%d, request_firmware failed for %s\n", err,
56 fw_name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020057 goto error1;
58 }
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +120059 if (firmware->size < sizeof(header)) {
60 dev_printk(KERN_ERR, &dev->dev, "Header size too small %s\n",
61 fw_name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020062 goto error2;
63 }
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +120064 memcpy(&header, firmware->data, sizeof(header));
65
66 if ((header.type != 0x45444F43) || /* "CODE" */
67 (header.adapter != adapter)
68 || (header.size != firmware->size)) {
69 dev_printk(KERN_ERR, &dev->dev, "Invalid firmware file\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020070 goto error2;
71 }
72
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +120073 if ((header.version / 100 & ~1) != (HPI_VER_DECIMAL / 100 & ~1)) {
74 dev_printk(KERN_ERR, &dev->dev,
Eliot Blennerhassett7f41b612011-02-10 17:26:19 +130075 "Incompatible firmware version "
Eliot Blennerhassett5ed15da2011-02-10 17:25:54 +130076 "DSP image %d != Driver %d\n", header.version,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020077 HPI_VER_DECIMAL);
78 goto error2;
79 }
80
81 if (header.version != HPI_VER_DECIMAL) {
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +120082 dev_printk(KERN_WARNING, &dev->dev,
Eliot Blennerhassett7f41b612011-02-10 17:26:19 +130083 "Firmware: release version mismatch DSP image %d != Driver %d\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020084 header.version, HPI_VER_DECIMAL);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020085 }
86
Eliot Blennerhassett5ed15da2011-02-10 17:25:54 +130087 HPI_DEBUG_LOG(DEBUG, "dsp code %s opened\n", fw_name);
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +120088 dsp_code->pvt = kmalloc(sizeof(*dsp_code->pvt), GFP_KERNEL);
Jesper Juhldc889f12011-07-31 23:16:43 +020089 if (!dsp_code->pvt) {
90 err_ret = HPI_ERROR_MEMORY_ALLOC;
91 goto error2;
92 }
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +120093
94 dsp_code->pvt->dev = dev;
95 dsp_code->pvt->firmware = firmware;
96 dsp_code->header = header;
97 dsp_code->block_length = header.size / sizeof(u32);
98 dsp_code->word_count = sizeof(header) / sizeof(u32);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020099 return 0;
100
101error2:
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +1200102 release_firmware(firmware);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200103error1:
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +1200104 dsp_code->block_length = 0;
Jesper Juhldc889f12011-07-31 23:16:43 +0200105 return err_ret;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200106}
107
108/*-------------------------------------------------------------------*/
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +1200109void hpi_dsp_code_close(struct dsp_code *dsp_code)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200110{
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +1200111 if (dsp_code->pvt->firmware) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200112 HPI_DEBUG_LOG(DEBUG, "dsp code closed\n");
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +1200113 release_firmware(dsp_code->pvt->firmware);
114 dsp_code->pvt->firmware = NULL;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200115 }
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +1200116 kfree(dsp_code->pvt);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200117}
118
119/*-------------------------------------------------------------------*/
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +1200120void hpi_dsp_code_rewind(struct dsp_code *dsp_code)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200121{
122 /* Go back to start of data, after header */
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +1200123 dsp_code->word_count = sizeof(struct code_header) / sizeof(u32);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200124}
125
126/*-------------------------------------------------------------------*/
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +1200127short hpi_dsp_code_read_word(struct dsp_code *dsp_code, u32 *pword)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200128{
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +1200129 if (dsp_code->word_count + 1 > dsp_code->block_length)
Eliot Blennerhassett5ed15da2011-02-10 17:25:54 +1300130 return HPI_ERROR_DSP_FILE_FORMAT;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200131
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +1200132 *pword = ((u32 *)(dsp_code->pvt->firmware->data))[dsp_code->
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200133 word_count];
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +1200134 dsp_code->word_count++;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200135 return 0;
136}
137
138/*-------------------------------------------------------------------*/
139short hpi_dsp_code_read_block(size_t words_requested,
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +1200140 struct dsp_code *dsp_code, u32 **ppblock)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200141{
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +1200142 if (dsp_code->word_count + words_requested > dsp_code->block_length)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200143 return HPI_ERROR_DSP_FILE_FORMAT;
144
145 *ppblock =
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +1200146 ((u32 *)(dsp_code->pvt->firmware->data)) +
147 dsp_code->word_count;
148 dsp_code->word_count += words_requested;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200149 return 0;
150}