blob: 1d9be4431b7283ad8b06226f1e994da80ea04a48 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Emagic EMI 2|6 usb audio interface firmware loader.
3 * Copyright (C) 2002
Jan Engelhardt96de0e22007-10-19 23:21:04 +02004 * Tapio Laxström (tapio.laxstrom@iptime.fi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, as published by
8 * the Free Software Foundation, version 2.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10#include <linux/kernel.h>
11#include <linux/errno.h>
12#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/module.h>
14#include <linux/usb.h>
Monty16c23f72006-05-09 12:37:22 -070015#include <linux/delay.h>
David Woodhouseb8e24bf2008-05-30 17:35:47 +030016#include <linux/firmware.h>
17#include <linux/ihex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19/* include firmware (variables)*/
20
21/* FIXME: This is quick and dirty solution! */
22#define SPDIF /* if you want SPDIF comment next line */
23//#undef SPDIF /* if you want MIDI uncomment this line */
24
25#ifdef SPDIF
David Woodhouseb8e24bf2008-05-30 17:35:47 +030026#define FIRMWARE_FW "emi62/spdif.fw"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#else
David Woodhouseb8e24bf2008-05-30 17:35:47 +030028#define FIRMWARE_FW "emi62/midi.fw"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#endif
30
31#define EMI62_VENDOR_ID 0x086a /* Emagic Soft-und Hardware GmBH */
32#define EMI62_PRODUCT_ID 0x0110 /* EMI 6|2m without firmware */
33
34#define ANCHOR_LOAD_INTERNAL 0xA0 /* Vendor specific request code for Anchor Upload/Download (This one is implemented in the core) */
35#define ANCHOR_LOAD_EXTERNAL 0xA3 /* This command is not implemented in the core. Requires firmware */
36#define ANCHOR_LOAD_FPGA 0xA5 /* This command is not implemented in the core. Requires firmware. Emagic extension */
37#define MAX_INTERNAL_ADDRESS 0x1B3F /* This is the highest internal RAM address for the AN2131Q */
38#define CPUCS_REG 0x7F92 /* EZ-USB Control and Status Register. Bit 0 controls 8051 reset */
39#define INTERNAL_RAM(address) (address <= MAX_INTERNAL_ADDRESS)
40
David Woodhouseb8e24bf2008-05-30 17:35:47 +030041static int emi62_writememory(struct usb_device *dev, int address,
42 const unsigned char *data, int length,
43 __u8 bRequest);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044static int emi62_set_reset(struct usb_device *dev, unsigned char reset_bit);
45static int emi62_load_firmware (struct usb_device *dev);
46static int emi62_probe(struct usb_interface *intf, const struct usb_device_id *id);
47static void emi62_disconnect(struct usb_interface *intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49/* thanks to drivers/usb/serial/keyspan_pda.c code */
David Woodhouseb8e24bf2008-05-30 17:35:47 +030050static int emi62_writememory(struct usb_device *dev, int address,
51 const unsigned char *data, int length,
52 __u8 request)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
54 int result;
Eric Sesterhenn5d7efe52006-10-26 21:06:24 +020055 unsigned char *buffer = kmemdup(data, length, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57 if (!buffer) {
Greg Kroah-Hartmane9a527d2012-04-20 16:53:40 -070058 dev_err(&dev->dev, "kmalloc(%d) failed.\n", length);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 return -ENOMEM;
60 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 /* Note: usb_control_msg returns negative value on error or length of the
62 * data that was written! */
63 result = usb_control_msg (dev, usb_sndctrlpipe(dev, 0), request, 0x40, address, 0, buffer, length, 300);
64 kfree (buffer);
65 return result;
66}
67
68/* thanks to drivers/usb/serial/keyspan_pda.c code */
69static int emi62_set_reset (struct usb_device *dev, unsigned char reset_bit)
70{
71 int response;
Greg Kroah-Hartman1b29a372008-08-18 13:21:04 -070072 dev_info(&dev->dev, "%s - %d\n", __func__, reset_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74 response = emi62_writememory (dev, CPUCS_REG, &reset_bit, 1, 0xa0);
Greg Kroah-Hartmane9a527d2012-04-20 16:53:40 -070075 if (response < 0)
76 dev_err(&dev->dev, "set_reset (%d) failed\n", reset_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 return response;
78}
79
80#define FW_LOAD_SIZE 1023
81
82static int emi62_load_firmware (struct usb_device *dev)
83{
David Woodhouseb8e24bf2008-05-30 17:35:47 +030084 const struct firmware *loader_fw = NULL;
85 const struct firmware *bitstream_fw = NULL;
86 const struct firmware *firmware_fw = NULL;
87 const struct ihex_binrec *rec;
Greg Kroah-Hartmane9a527d2012-04-20 16:53:40 -070088 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 __u32 addr; /* Address to write */
91 __u8 *buf;
92
93 dev_dbg(&dev->dev, "load_firmware\n");
94 buf = kmalloc(FW_LOAD_SIZE, GFP_KERNEL);
Greg Kroah-Hartmane9a527d2012-04-20 16:53:40 -070095 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 goto wraperr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
David Woodhouseb8e24bf2008-05-30 17:35:47 +030098 err = request_ihex_firmware(&loader_fw, "emi62/loader.fw", &dev->dev);
99 if (err)
100 goto nofw;
101
102 err = request_ihex_firmware(&bitstream_fw, "emi62/bitstream.fw",
103 &dev->dev);
104 if (err)
105 goto nofw;
106
107 err = request_ihex_firmware(&firmware_fw, FIRMWARE_FW, &dev->dev);
108 if (err) {
109 nofw:
David Woodhouseb8e24bf2008-05-30 17:35:47 +0300110 goto wraperr;
111 }
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 /* Assert reset (stop the CPU in the EMI) */
114 err = emi62_set_reset(dev,1);
Greg Kroah-Hartmane9a527d2012-04-20 16:53:40 -0700115 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 goto wraperr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
David Woodhouseb8e24bf2008-05-30 17:35:47 +0300118 rec = (const struct ihex_binrec *)loader_fw->data;
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 /* 1. We need to put the loader for the FPGA into the EZ-USB */
David Woodhouseb8e24bf2008-05-30 17:35:47 +0300121 while (rec) {
122 err = emi62_writememory(dev, be32_to_cpu(rec->addr),
123 rec->data, be16_to_cpu(rec->len),
124 ANCHOR_LOAD_INTERNAL);
Greg Kroah-Hartmane9a527d2012-04-20 16:53:40 -0700125 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 goto wraperr;
David Woodhouseb8e24bf2008-05-30 17:35:47 +0300127 rec = ihex_next_binrec(rec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 }
129
130 /* De-assert reset (let the CPU run) */
131 err = emi62_set_reset(dev,0);
Greg Kroah-Hartmane9a527d2012-04-20 16:53:40 -0700132 if (err < 0)
Oliver Neukum5919a43b2007-10-25 15:42:38 +0200133 goto wraperr;
Monty16c23f72006-05-09 12:37:22 -0700134 msleep(250); /* let device settle */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136 /* 2. We upload the FPGA firmware into the EMI
137 * Note: collect up to 1023 (yes!) bytes and send them with
138 * a single request. This is _much_ faster! */
David Woodhouseb8e24bf2008-05-30 17:35:47 +0300139 rec = (const struct ihex_binrec *)bitstream_fw->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 do {
141 i = 0;
David Woodhouseb8e24bf2008-05-30 17:35:47 +0300142 addr = be32_to_cpu(rec->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 /* intel hex records are terminated with type 0 element */
David Woodhouseb8e24bf2008-05-30 17:35:47 +0300145 while (rec && (i + be16_to_cpu(rec->len) < FW_LOAD_SIZE)) {
146 memcpy(buf + i, rec->data, be16_to_cpu(rec->len));
147 i += be16_to_cpu(rec->len);
148 rec = ihex_next_binrec(rec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 }
150 err = emi62_writememory(dev, addr, buf, i, ANCHOR_LOAD_FPGA);
Greg Kroah-Hartmane9a527d2012-04-20 16:53:40 -0700151 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 goto wraperr;
Clemens Ladischac06c062009-12-21 15:36:44 -0800153 } while (rec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 /* Assert reset (stop the CPU in the EMI) */
156 err = emi62_set_reset(dev,1);
Greg Kroah-Hartmane9a527d2012-04-20 16:53:40 -0700157 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 goto wraperr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160 /* 3. We need to put the loader for the firmware into the EZ-USB (again...) */
David Woodhouseb8e24bf2008-05-30 17:35:47 +0300161 for (rec = (const struct ihex_binrec *)loader_fw->data;
162 rec; rec = ihex_next_binrec(rec)) {
163 err = emi62_writememory(dev, be32_to_cpu(rec->addr),
164 rec->data, be16_to_cpu(rec->len),
165 ANCHOR_LOAD_INTERNAL);
Greg Kroah-Hartmane9a527d2012-04-20 16:53:40 -0700166 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 goto wraperr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 }
169
170 /* De-assert reset (let the CPU run) */
171 err = emi62_set_reset(dev,0);
Greg Kroah-Hartmane9a527d2012-04-20 16:53:40 -0700172 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 goto wraperr;
Monty16c23f72006-05-09 12:37:22 -0700174 msleep(250); /* let device settle */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 /* 4. We put the part of the firmware that lies in the external RAM into the EZ-USB */
177
David Woodhouseb8e24bf2008-05-30 17:35:47 +0300178 for (rec = (const struct ihex_binrec *)firmware_fw->data;
179 rec; rec = ihex_next_binrec(rec)) {
180 if (!INTERNAL_RAM(be32_to_cpu(rec->addr))) {
181 err = emi62_writememory(dev, be32_to_cpu(rec->addr),
182 rec->data, be16_to_cpu(rec->len),
183 ANCHOR_LOAD_EXTERNAL);
Greg Kroah-Hartmane9a527d2012-04-20 16:53:40 -0700184 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 goto wraperr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 }
187 }
David Woodhouseb8e24bf2008-05-30 17:35:47 +0300188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 /* Assert reset (stop the CPU in the EMI) */
190 err = emi62_set_reset(dev,1);
Greg Kroah-Hartmane9a527d2012-04-20 16:53:40 -0700191 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 goto wraperr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
David Woodhouseb8e24bf2008-05-30 17:35:47 +0300194 for (rec = (const struct ihex_binrec *)firmware_fw->data;
195 rec; rec = ihex_next_binrec(rec)) {
196 if (INTERNAL_RAM(be32_to_cpu(rec->addr))) {
197 err = emi62_writememory(dev, be32_to_cpu(rec->addr),
198 rec->data, be16_to_cpu(rec->len),
199 ANCHOR_LOAD_EXTERNAL);
Greg Kroah-Hartmane9a527d2012-04-20 16:53:40 -0700200 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 goto wraperr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 }
203 }
David Woodhouseb8e24bf2008-05-30 17:35:47 +0300204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 /* De-assert reset (let the CPU run) */
206 err = emi62_set_reset(dev,0);
Greg Kroah-Hartmane9a527d2012-04-20 16:53:40 -0700207 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 goto wraperr;
Monty16c23f72006-05-09 12:37:22 -0700209 msleep(250); /* let device settle */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
David Woodhouseb8e24bf2008-05-30 17:35:47 +0300211 release_firmware(loader_fw);
212 release_firmware(bitstream_fw);
213 release_firmware(firmware_fw);
214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 kfree(buf);
216
217 /* return 1 to fail the driver inialization
218 * and give real driver change to load */
219 return 1;
220
221wraperr:
Greg Kroah-Hartmane9a527d2012-04-20 16:53:40 -0700222 if (err < 0)
223 dev_err(&dev->dev,"%s - error loading firmware: error = %d\n",
224 __func__, err);
David Woodhouseb8e24bf2008-05-30 17:35:47 +0300225 release_firmware(loader_fw);
226 release_firmware(bitstream_fw);
227 release_firmware(firmware_fw);
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 kfree(buf);
230 dev_err(&dev->dev, "Error\n");
231 return err;
232}
233
Greg Kroah-Hartman83957df2012-08-17 17:48:41 -0700234static const struct usb_device_id id_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 { USB_DEVICE(EMI62_VENDOR_ID, EMI62_PRODUCT_ID) },
236 { } /* Terminating entry */
237};
238
239MODULE_DEVICE_TABLE (usb, id_table);
240
241static int emi62_probe(struct usb_interface *intf, const struct usb_device_id *id)
242{
243 struct usb_device *dev = interface_to_usbdev(intf);
244 dev_dbg(&intf->dev, "emi62_probe\n");
245
Greg Kroah-Hartman1b29a372008-08-18 13:21:04 -0700246 dev_info(&intf->dev, "%s start\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 emi62_load_firmware(dev);
249
250 /* do not return the driver context, let real audio driver do that */
251 return -EIO;
252}
253
254static void emi62_disconnect(struct usb_interface *intf)
255{
256}
257
258static struct usb_driver emi62_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 .name = "emi62 - firmware loader",
260 .probe = emi62_probe,
261 .disconnect = emi62_disconnect,
262 .id_table = id_table,
263};
264
Greg Kroah-Hartman65db4302011-11-18 09:34:02 -0800265module_usb_driver(emi62_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Jan Engelhardt96de0e22007-10-19 23:21:04 +0200267MODULE_AUTHOR("Tapio Laxström");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268MODULE_DESCRIPTION("Emagic EMI 6|2m firmware loader.");
269MODULE_LICENSE("GPL");
270
David Woodhouseb8e24bf2008-05-30 17:35:47 +0300271MODULE_FIRMWARE("emi62/loader.fw");
272MODULE_FIRMWARE("emi62/bitstream.fw");
273MODULE_FIRMWARE(FIRMWARE_FW);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274/* vi:ai:syntax=c:sw=8:ts=8:tw=80
275 */