blob: 3eea60437f5671fa0d772f3fab55ad36058d8af5 [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Emagic EMI 2|6 usb audio interface firmware loader.
4 * Copyright (C) 2002
Jan Engelhardt96de0e22007-10-19 23:21:04 +02005 * Tapio Laxström (tapio.laxstrom@iptime.fi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7#include <linux/kernel.h>
8#include <linux/errno.h>
9#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/module.h>
11#include <linux/usb.h>
Monty16c23f72006-05-09 12:37:22 -070012#include <linux/delay.h>
David Woodhouseb8e24bf2008-05-30 17:35:47 +030013#include <linux/firmware.h>
14#include <linux/ihex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16/* include firmware (variables)*/
17
18/* FIXME: This is quick and dirty solution! */
19#define SPDIF /* if you want SPDIF comment next line */
20//#undef SPDIF /* if you want MIDI uncomment this line */
21
22#ifdef SPDIF
David Woodhouseb8e24bf2008-05-30 17:35:47 +030023#define FIRMWARE_FW "emi62/spdif.fw"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#else
David Woodhouseb8e24bf2008-05-30 17:35:47 +030025#define FIRMWARE_FW "emi62/midi.fw"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#endif
27
28#define EMI62_VENDOR_ID 0x086a /* Emagic Soft-und Hardware GmBH */
29#define EMI62_PRODUCT_ID 0x0110 /* EMI 6|2m without firmware */
30
31#define ANCHOR_LOAD_INTERNAL 0xA0 /* Vendor specific request code for Anchor Upload/Download (This one is implemented in the core) */
32#define ANCHOR_LOAD_EXTERNAL 0xA3 /* This command is not implemented in the core. Requires firmware */
33#define ANCHOR_LOAD_FPGA 0xA5 /* This command is not implemented in the core. Requires firmware. Emagic extension */
34#define MAX_INTERNAL_ADDRESS 0x1B3F /* This is the highest internal RAM address for the AN2131Q */
35#define CPUCS_REG 0x7F92 /* EZ-USB Control and Status Register. Bit 0 controls 8051 reset */
36#define INTERNAL_RAM(address) (address <= MAX_INTERNAL_ADDRESS)
37
David Woodhouseb8e24bf2008-05-30 17:35:47 +030038static int emi62_writememory(struct usb_device *dev, int address,
39 const unsigned char *data, int length,
40 __u8 bRequest);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041static int emi62_set_reset(struct usb_device *dev, unsigned char reset_bit);
42static int emi62_load_firmware (struct usb_device *dev);
43static int emi62_probe(struct usb_interface *intf, const struct usb_device_id *id);
44static void emi62_disconnect(struct usb_interface *intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46/* thanks to drivers/usb/serial/keyspan_pda.c code */
David Woodhouseb8e24bf2008-05-30 17:35:47 +030047static int emi62_writememory(struct usb_device *dev, int address,
48 const unsigned char *data, int length,
49 __u8 request)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
51 int result;
Eric Sesterhenn5d7efe5b2006-10-26 21:06:24 +020052 unsigned char *buffer = kmemdup(data, length, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54 if (!buffer) {
Greg Kroah-Hartmane9a527d2012-04-20 16:53:40 -070055 dev_err(&dev->dev, "kmalloc(%d) failed.\n", length);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 return -ENOMEM;
57 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 /* Note: usb_control_msg returns negative value on error or length of the
59 * data that was written! */
60 result = usb_control_msg (dev, usb_sndctrlpipe(dev, 0), request, 0x40, address, 0, buffer, length, 300);
61 kfree (buffer);
62 return result;
63}
64
65/* thanks to drivers/usb/serial/keyspan_pda.c code */
66static int emi62_set_reset (struct usb_device *dev, unsigned char reset_bit)
67{
68 int response;
Greg Kroah-Hartman1b29a372008-08-18 13:21:04 -070069 dev_info(&dev->dev, "%s - %d\n", __func__, reset_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71 response = emi62_writememory (dev, CPUCS_REG, &reset_bit, 1, 0xa0);
Greg Kroah-Hartmane9a527d2012-04-20 16:53:40 -070072 if (response < 0)
73 dev_err(&dev->dev, "set_reset (%d) failed\n", reset_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 return response;
75}
76
77#define FW_LOAD_SIZE 1023
78
79static int emi62_load_firmware (struct usb_device *dev)
80{
David Woodhouseb8e24bf2008-05-30 17:35:47 +030081 const struct firmware *loader_fw = NULL;
82 const struct firmware *bitstream_fw = NULL;
83 const struct firmware *firmware_fw = NULL;
84 const struct ihex_binrec *rec;
Greg Kroah-Hartmane9a527d2012-04-20 16:53:40 -070085 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 __u32 addr; /* Address to write */
88 __u8 *buf;
89
90 dev_dbg(&dev->dev, "load_firmware\n");
91 buf = kmalloc(FW_LOAD_SIZE, GFP_KERNEL);
Greg Kroah-Hartmane9a527d2012-04-20 16:53:40 -070092 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 goto wraperr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
David Woodhouseb8e24bf2008-05-30 17:35:47 +030095 err = request_ihex_firmware(&loader_fw, "emi62/loader.fw", &dev->dev);
96 if (err)
97 goto nofw;
98
99 err = request_ihex_firmware(&bitstream_fw, "emi62/bitstream.fw",
100 &dev->dev);
101 if (err)
102 goto nofw;
103
104 err = request_ihex_firmware(&firmware_fw, FIRMWARE_FW, &dev->dev);
105 if (err) {
106 nofw:
David Woodhouseb8e24bf2008-05-30 17:35:47 +0300107 goto wraperr;
108 }
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 /* Assert reset (stop the CPU in the EMI) */
111 err = emi62_set_reset(dev,1);
Greg Kroah-Hartmane9a527d2012-04-20 16:53:40 -0700112 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 goto wraperr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
David Woodhouseb8e24bf2008-05-30 17:35:47 +0300115 rec = (const struct ihex_binrec *)loader_fw->data;
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 /* 1. We need to put the loader for the FPGA into the EZ-USB */
David Woodhouseb8e24bf2008-05-30 17:35:47 +0300118 while (rec) {
119 err = emi62_writememory(dev, be32_to_cpu(rec->addr),
120 rec->data, be16_to_cpu(rec->len),
121 ANCHOR_LOAD_INTERNAL);
Greg Kroah-Hartmane9a527d2012-04-20 16:53:40 -0700122 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 goto wraperr;
David Woodhouseb8e24bf2008-05-30 17:35:47 +0300124 rec = ihex_next_binrec(rec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 }
126
127 /* De-assert reset (let the CPU run) */
128 err = emi62_set_reset(dev,0);
Greg Kroah-Hartmane9a527d2012-04-20 16:53:40 -0700129 if (err < 0)
Oliver Neukum5919a43b2007-10-25 15:42:38 +0200130 goto wraperr;
Monty16c23f72006-05-09 12:37:22 -0700131 msleep(250); /* let device settle */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133 /* 2. We upload the FPGA firmware into the EMI
134 * Note: collect up to 1023 (yes!) bytes and send them with
135 * a single request. This is _much_ faster! */
David Woodhouseb8e24bf2008-05-30 17:35:47 +0300136 rec = (const struct ihex_binrec *)bitstream_fw->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 do {
138 i = 0;
David Woodhouseb8e24bf2008-05-30 17:35:47 +0300139 addr = be32_to_cpu(rec->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 /* intel hex records are terminated with type 0 element */
David Woodhouseb8e24bf2008-05-30 17:35:47 +0300142 while (rec && (i + be16_to_cpu(rec->len) < FW_LOAD_SIZE)) {
143 memcpy(buf + i, rec->data, be16_to_cpu(rec->len));
144 i += be16_to_cpu(rec->len);
145 rec = ihex_next_binrec(rec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 }
147 err = emi62_writememory(dev, addr, buf, i, ANCHOR_LOAD_FPGA);
Greg Kroah-Hartmane9a527d2012-04-20 16:53:40 -0700148 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 goto wraperr;
Clemens Ladischac06c062009-12-21 15:36:44 -0800150 } while (rec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152 /* Assert reset (stop the CPU in the EMI) */
153 err = emi62_set_reset(dev,1);
Greg Kroah-Hartmane9a527d2012-04-20 16:53:40 -0700154 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 goto wraperr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 /* 3. We need to put the loader for the firmware into the EZ-USB (again...) */
David Woodhouseb8e24bf2008-05-30 17:35:47 +0300158 for (rec = (const struct ihex_binrec *)loader_fw->data;
159 rec; rec = ihex_next_binrec(rec)) {
160 err = emi62_writememory(dev, be32_to_cpu(rec->addr),
161 rec->data, be16_to_cpu(rec->len),
162 ANCHOR_LOAD_INTERNAL);
Greg Kroah-Hartmane9a527d2012-04-20 16:53:40 -0700163 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 goto wraperr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 }
166
167 /* De-assert reset (let the CPU run) */
168 err = emi62_set_reset(dev,0);
Greg Kroah-Hartmane9a527d2012-04-20 16:53:40 -0700169 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 goto wraperr;
Monty16c23f72006-05-09 12:37:22 -0700171 msleep(250); /* let device settle */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 /* 4. We put the part of the firmware that lies in the external RAM into the EZ-USB */
174
David Woodhouseb8e24bf2008-05-30 17:35:47 +0300175 for (rec = (const struct ihex_binrec *)firmware_fw->data;
176 rec; rec = ihex_next_binrec(rec)) {
177 if (!INTERNAL_RAM(be32_to_cpu(rec->addr))) {
178 err = emi62_writememory(dev, be32_to_cpu(rec->addr),
179 rec->data, be16_to_cpu(rec->len),
180 ANCHOR_LOAD_EXTERNAL);
Greg Kroah-Hartmane9a527d2012-04-20 16:53:40 -0700181 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 goto wraperr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 }
184 }
David Woodhouseb8e24bf2008-05-30 17:35:47 +0300185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 /* Assert reset (stop the CPU in the EMI) */
187 err = emi62_set_reset(dev,1);
Greg Kroah-Hartmane9a527d2012-04-20 16:53:40 -0700188 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 goto wraperr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
David Woodhouseb8e24bf2008-05-30 17:35:47 +0300191 for (rec = (const struct ihex_binrec *)firmware_fw->data;
192 rec; rec = ihex_next_binrec(rec)) {
193 if (INTERNAL_RAM(be32_to_cpu(rec->addr))) {
194 err = emi62_writememory(dev, be32_to_cpu(rec->addr),
195 rec->data, be16_to_cpu(rec->len),
196 ANCHOR_LOAD_EXTERNAL);
Greg Kroah-Hartmane9a527d2012-04-20 16:53:40 -0700197 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 goto wraperr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 }
200 }
David Woodhouseb8e24bf2008-05-30 17:35:47 +0300201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 /* De-assert reset (let the CPU run) */
203 err = emi62_set_reset(dev,0);
Greg Kroah-Hartmane9a527d2012-04-20 16:53:40 -0700204 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 goto wraperr;
Monty16c23f72006-05-09 12:37:22 -0700206 msleep(250); /* let device settle */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
David Woodhouseb8e24bf2008-05-30 17:35:47 +0300208 release_firmware(loader_fw);
209 release_firmware(bitstream_fw);
210 release_firmware(firmware_fw);
211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 kfree(buf);
213
214 /* return 1 to fail the driver inialization
215 * and give real driver change to load */
216 return 1;
217
218wraperr:
Greg Kroah-Hartmane9a527d2012-04-20 16:53:40 -0700219 if (err < 0)
220 dev_err(&dev->dev,"%s - error loading firmware: error = %d\n",
221 __func__, err);
David Woodhouseb8e24bf2008-05-30 17:35:47 +0300222 release_firmware(loader_fw);
223 release_firmware(bitstream_fw);
224 release_firmware(firmware_fw);
225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 kfree(buf);
227 dev_err(&dev->dev, "Error\n");
228 return err;
229}
230
Greg Kroah-Hartman83957df2012-08-17 17:48:41 -0700231static const struct usb_device_id id_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 { USB_DEVICE(EMI62_VENDOR_ID, EMI62_PRODUCT_ID) },
233 { } /* Terminating entry */
234};
235
236MODULE_DEVICE_TABLE (usb, id_table);
237
238static int emi62_probe(struct usb_interface *intf, const struct usb_device_id *id)
239{
240 struct usb_device *dev = interface_to_usbdev(intf);
241 dev_dbg(&intf->dev, "emi62_probe\n");
242
Greg Kroah-Hartman1b29a372008-08-18 13:21:04 -0700243 dev_info(&intf->dev, "%s start\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245 emi62_load_firmware(dev);
246
247 /* do not return the driver context, let real audio driver do that */
248 return -EIO;
249}
250
251static void emi62_disconnect(struct usb_interface *intf)
252{
253}
254
255static struct usb_driver emi62_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 .name = "emi62 - firmware loader",
257 .probe = emi62_probe,
258 .disconnect = emi62_disconnect,
259 .id_table = id_table,
260};
261
Greg Kroah-Hartman65db4302011-11-18 09:34:02 -0800262module_usb_driver(emi62_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Jan Engelhardt96de0e22007-10-19 23:21:04 +0200264MODULE_AUTHOR("Tapio Laxström");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265MODULE_DESCRIPTION("Emagic EMI 6|2m firmware loader.");
266MODULE_LICENSE("GPL");
267
David Woodhouseb8e24bf2008-05-30 17:35:47 +0300268MODULE_FIRMWARE("emi62/loader.fw");
269MODULE_FIRMWARE("emi62/bitstream.fw");
270MODULE_FIRMWARE(FIRMWARE_FW);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271/* vi:ai:syntax=c:sw=8:ts=8:tw=80
272 */