blob: 987538819b241c072d5455eedbc1a78ce8265361 [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"
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 Blennerhassett719f82d32010-04-21 18:17:39 +020033};
34
Eliot Blennerhassett719f82d32010-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
Joe Perchesd50a2fb2011-06-03 02:28:49 -070038#include <linux/pci.h>
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +020039/*-------------------------------------------------------------------*/
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +120040short hpi_dsp_code_open(u32 adapter, void *os_data, struct dsp_code *dsp_code,
41 u32 *os_error_code)
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +020042{
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +120043 const struct firmware *firmware;
44 struct pci_dev *dev = os_data;
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +020045 struct code_header header;
46 char fw_name[20];
47 int err;
48
49 sprintf(fw_name, "asihpi/dsp%04x.bin", adapter);
Eliot Blennerhassett719f82d32010-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 Blennerhassett719f82d32010-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 Blennerhassett719f82d32010-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 Blennerhassett719f82d32010-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 Blennerhassett719f82d32010-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 Blennerhassett719f82d32010-04-21 18:17:39 +020084 header.version, HPI_VER_DECIMAL);
Eliot Blennerhassett719f82d32010-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);
89 if (!dsp_code->pvt)
90 return HPI_ERROR_MEMORY_ALLOC;
91
92 dsp_code->pvt->dev = dev;
93 dsp_code->pvt->firmware = firmware;
94 dsp_code->header = header;
95 dsp_code->block_length = header.size / sizeof(u32);
96 dsp_code->word_count = sizeof(header) / sizeof(u32);
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +020097 return 0;
98
99error2:
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +1200100 release_firmware(firmware);
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +0200101error1:
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +1200102 dsp_code->block_length = 0;
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +0200103 return HPI_ERROR_DSP_FILE_NOT_FOUND;
104}
105
106/*-------------------------------------------------------------------*/
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +1200107void hpi_dsp_code_close(struct dsp_code *dsp_code)
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +0200108{
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +1200109 if (dsp_code->pvt->firmware) {
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +0200110 HPI_DEBUG_LOG(DEBUG, "dsp code closed\n");
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +1200111 release_firmware(dsp_code->pvt->firmware);
112 dsp_code->pvt->firmware = NULL;
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +0200113 }
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +1200114 kfree(dsp_code->pvt);
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +0200115}
116
117/*-------------------------------------------------------------------*/
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +1200118void hpi_dsp_code_rewind(struct dsp_code *dsp_code)
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +0200119{
120 /* Go back to start of data, after header */
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +1200121 dsp_code->word_count = sizeof(struct code_header) / sizeof(u32);
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +0200122}
123
124/*-------------------------------------------------------------------*/
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +1200125short hpi_dsp_code_read_word(struct dsp_code *dsp_code, u32 *pword)
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +0200126{
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +1200127 if (dsp_code->word_count + 1 > dsp_code->block_length)
Eliot Blennerhassett5ed15da2011-02-10 17:25:54 +1300128 return HPI_ERROR_DSP_FILE_FORMAT;
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +0200129
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +1200130 *pword = ((u32 *)(dsp_code->pvt->firmware->data))[dsp_code->
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +0200131 word_count];
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +1200132 dsp_code->word_count++;
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +0200133 return 0;
134}
135
136/*-------------------------------------------------------------------*/
137short hpi_dsp_code_read_block(size_t words_requested,
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +1200138 struct dsp_code *dsp_code, u32 **ppblock)
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +0200139{
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +1200140 if (dsp_code->word_count + words_requested > dsp_code->block_length)
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +0200141 return HPI_ERROR_DSP_FILE_FORMAT;
142
143 *ppblock =
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +1200144 ((u32 *)(dsp_code->pvt->firmware->data)) +
145 dsp_code->word_count;
146 dsp_code->word_count += words_requested;
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +0200147 return 0;
148}