blob: 9acc0ac75ecae21f49bf4c1dc1ed8e9837ab7888 [file] [log] [blame]
Thomas Gleixner07d7fe72019-05-29 07:12:47 -07001// SPDX-License-Identifier: GPL-2.0-only
Eliot Blennerhassett43986432014-12-18 17:57:13 +13002/***********************************************************************
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +02003
4 AudioScience HPI driver
Eliot Blennerhassett43986432014-12-18 17:57:13 +13005 Functions for reading DSP code using hotplug firmware loader
6
7 Copyright (C) 1997-2014 AudioScience Inc. <support@audioscience.com>
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +02008
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +02009
Eliot Blennerhassett43986432014-12-18 17:57:13 +130010***********************************************************************/
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +020011#define SOURCEFILE_NAME "hpidspcd.c"
12#include "hpidspcd.h"
13#include "hpidebug.h"
Eliot Blennerhassettf6baaec2011-12-22 13:38:31 +130014#include "hpi_version.h"
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +020015
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +120016struct dsp_code_private {
17 /** Firmware descriptor */
18 const struct firmware *firmware;
19 struct pci_dev *dev;
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +020020};
21
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +020022/*-------------------------------------------------------------------*/
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +120023short hpi_dsp_code_open(u32 adapter, void *os_data, struct dsp_code *dsp_code,
24 u32 *os_error_code)
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +020025{
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +120026 const struct firmware *firmware;
27 struct pci_dev *dev = os_data;
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +020028 struct code_header header;
29 char fw_name[20];
Jesper Juhldc889f12011-07-31 23:16:43 +020030 short err_ret = HPI_ERROR_DSP_FILE_NOT_FOUND;
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +020031 int err;
32
33 sprintf(fw_name, "asihpi/dsp%04x.bin", adapter);
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +020034
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +120035 err = request_firmware(&firmware, fw_name, &dev->dev);
Eliot Blennerhassett5ed15da2011-02-10 17:25:54 +130036
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +120037 if (err || !firmware) {
Joe Perches4ee3bff2012-10-28 01:05:54 -070038 dev_err(&dev->dev, "%d, request_firmware failed for %s\n",
39 err, fw_name);
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +020040 goto error1;
41 }
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +120042 if (firmware->size < sizeof(header)) {
Joe Perches4ee3bff2012-10-28 01:05:54 -070043 dev_err(&dev->dev, "Header size too small %s\n", fw_name);
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +020044 goto error2;
45 }
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +120046 memcpy(&header, firmware->data, sizeof(header));
47
48 if ((header.type != 0x45444F43) || /* "CODE" */
49 (header.adapter != adapter)
50 || (header.size != firmware->size)) {
Joe Perches4ee3bff2012-10-28 01:05:54 -070051 dev_err(&dev->dev,
Eliot Blennerhassettf6baaec2011-12-22 13:38:31 +130052 "Invalid firmware header size %d != file %zd\n",
53 header.size, firmware->size);
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +020054 goto error2;
55 }
56
Eliot Blennerhassett43986432014-12-18 17:57:13 +130057 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 Perches4ee3bff2012-10-28 01:05:54 -070061 header.version, HPI_VER);
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +020062 goto error2;
63 }
64
Eliot Blennerhassettf6baaec2011-12-22 13:38:31 +130065 if (header.version != HPI_VER) {
Eliot Blennerhassett43986432014-12-18 17:57:13 +130066 dev_warn(&dev->dev,
67 "Firmware version mismatch: DSP image %X != Driver %X\n",
68 header.version, HPI_VER);
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +020069 }
70
Eliot Blennerhassett5ed15da2011-02-10 17:25:54 +130071 HPI_DEBUG_LOG(DEBUG, "dsp code %s opened\n", fw_name);
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +120072 dsp_code->pvt = kmalloc(sizeof(*dsp_code->pvt), GFP_KERNEL);
Jesper Juhldc889f12011-07-31 23:16:43 +020073 if (!dsp_code->pvt) {
74 err_ret = HPI_ERROR_MEMORY_ALLOC;
75 goto error2;
76 }
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +120077
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 Blennerhassett719f82d32010-04-21 18:17:39 +020083 return 0;
84
85error2:
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +120086 release_firmware(firmware);
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +020087error1:
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +120088 dsp_code->block_length = 0;
Jesper Juhldc889f12011-07-31 23:16:43 +020089 return err_ret;
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +020090}
91
92/*-------------------------------------------------------------------*/
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +120093void hpi_dsp_code_close(struct dsp_code *dsp_code)
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +020094{
Eliot Blennerhassett50d5f772011-12-22 13:38:44 +130095 HPI_DEBUG_LOG(DEBUG, "dsp code closed\n");
96 release_firmware(dsp_code->pvt->firmware);
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +120097 kfree(dsp_code->pvt);
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +020098}
99
100/*-------------------------------------------------------------------*/
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +1200101void hpi_dsp_code_rewind(struct dsp_code *dsp_code)
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +0200102{
103 /* Go back to start of data, after header */
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +1200104 dsp_code->word_count = sizeof(struct code_header) / sizeof(u32);
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +0200105}
106
107/*-------------------------------------------------------------------*/
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +1200108short hpi_dsp_code_read_word(struct dsp_code *dsp_code, u32 *pword)
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +0200109{
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +1200110 if (dsp_code->word_count + 1 > dsp_code->block_length)
Eliot Blennerhassett5ed15da2011-02-10 17:25:54 +1300111 return HPI_ERROR_DSP_FILE_FORMAT;
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +0200112
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +1200113 *pword = ((u32 *)(dsp_code->pvt->firmware->data))[dsp_code->
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +0200114 word_count];
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +1200115 dsp_code->word_count++;
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +0200116 return 0;
117}
118
119/*-------------------------------------------------------------------*/
120short hpi_dsp_code_read_block(size_t words_requested,
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +1200121 struct dsp_code *dsp_code, u32 **ppblock)
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +0200122{
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +1200123 if (dsp_code->word_count + words_requested > dsp_code->block_length)
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +0200124 return HPI_ERROR_DSP_FILE_FORMAT;
125
126 *ppblock =
Eliot Blennerhassett95a4c6e2011-07-22 15:52:42 +1200127 ((u32 *)(dsp_code->pvt->firmware->data)) +
128 dsp_code->word_count;
129 dsp_code->word_count += words_requested;
Eliot Blennerhassett719f82d32010-04-21 18:17:39 +0200130 return 0;
131}