blob: ae794b90766ba627c94712f52a899ba244f95479 [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>
13#include <linux/init.h>
14#include <linux/module.h>
15#include <linux/usb.h>
Monty16c23f72006-05-09 12:37:22 -070016#include <linux/delay.h>
David Woodhouseb8e24bf2008-05-30 17:35:47 +030017#include <linux/firmware.h>
18#include <linux/ihex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20/* include firmware (variables)*/
21
22/* FIXME: This is quick and dirty solution! */
23#define SPDIF /* if you want SPDIF comment next line */
24//#undef SPDIF /* if you want MIDI uncomment this line */
25
26#ifdef SPDIF
David Woodhouseb8e24bf2008-05-30 17:35:47 +030027#define FIRMWARE_FW "emi62/spdif.fw"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#else
David Woodhouseb8e24bf2008-05-30 17:35:47 +030029#define FIRMWARE_FW "emi62/midi.fw"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#endif
31
32#define EMI62_VENDOR_ID 0x086a /* Emagic Soft-und Hardware GmBH */
33#define EMI62_PRODUCT_ID 0x0110 /* EMI 6|2m without firmware */
34
35#define ANCHOR_LOAD_INTERNAL 0xA0 /* Vendor specific request code for Anchor Upload/Download (This one is implemented in the core) */
36#define ANCHOR_LOAD_EXTERNAL 0xA3 /* This command is not implemented in the core. Requires firmware */
37#define ANCHOR_LOAD_FPGA 0xA5 /* This command is not implemented in the core. Requires firmware. Emagic extension */
38#define MAX_INTERNAL_ADDRESS 0x1B3F /* This is the highest internal RAM address for the AN2131Q */
39#define CPUCS_REG 0x7F92 /* EZ-USB Control and Status Register. Bit 0 controls 8051 reset */
40#define INTERNAL_RAM(address) (address <= MAX_INTERNAL_ADDRESS)
41
David Woodhouseb8e24bf2008-05-30 17:35:47 +030042static int emi62_writememory(struct usb_device *dev, int address,
43 const unsigned char *data, int length,
44 __u8 bRequest);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045static int emi62_set_reset(struct usb_device *dev, unsigned char reset_bit);
46static int emi62_load_firmware (struct usb_device *dev);
47static int emi62_probe(struct usb_interface *intf, const struct usb_device_id *id);
48static void emi62_disconnect(struct usb_interface *intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50/* thanks to drivers/usb/serial/keyspan_pda.c code */
David Woodhouseb8e24bf2008-05-30 17:35:47 +030051static int emi62_writememory(struct usb_device *dev, int address,
52 const unsigned char *data, int length,
53 __u8 request)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
55 int result;
Eric Sesterhenn5d7efe52006-10-26 21:06:24 +020056 unsigned char *buffer = kmemdup(data, length, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58 if (!buffer) {
Greg Kroah-Hartmane9a527d2012-04-20 16:53:40 -070059 dev_err(&dev->dev, "kmalloc(%d) failed.\n", length);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 return -ENOMEM;
61 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 /* Note: usb_control_msg returns negative value on error or length of the
63 * data that was written! */
64 result = usb_control_msg (dev, usb_sndctrlpipe(dev, 0), request, 0x40, address, 0, buffer, length, 300);
65 kfree (buffer);
66 return result;
67}
68
69/* thanks to drivers/usb/serial/keyspan_pda.c code */
70static int emi62_set_reset (struct usb_device *dev, unsigned char reset_bit)
71{
72 int response;
Greg Kroah-Hartman1b29a372008-08-18 13:21:04 -070073 dev_info(&dev->dev, "%s - %d\n", __func__, reset_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75 response = emi62_writememory (dev, CPUCS_REG, &reset_bit, 1, 0xa0);
Greg Kroah-Hartmane9a527d2012-04-20 16:53:40 -070076 if (response < 0)
77 dev_err(&dev->dev, "set_reset (%d) failed\n", reset_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 return response;
79}
80
81#define FW_LOAD_SIZE 1023
82
83static int emi62_load_firmware (struct usb_device *dev)
84{
David Woodhouseb8e24bf2008-05-30 17:35:47 +030085 const struct firmware *loader_fw = NULL;
86 const struct firmware *bitstream_fw = NULL;
87 const struct firmware *firmware_fw = NULL;
88 const struct ihex_binrec *rec;
Greg Kroah-Hartmane9a527d2012-04-20 16:53:40 -070089 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 __u32 addr; /* Address to write */
92 __u8 *buf;
93
94 dev_dbg(&dev->dev, "load_firmware\n");
95 buf = kmalloc(FW_LOAD_SIZE, GFP_KERNEL);
Greg Kroah-Hartmane9a527d2012-04-20 16:53:40 -070096 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 goto wraperr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
David Woodhouseb8e24bf2008-05-30 17:35:47 +030099 err = request_ihex_firmware(&loader_fw, "emi62/loader.fw", &dev->dev);
100 if (err)
101 goto nofw;
102
103 err = request_ihex_firmware(&bitstream_fw, "emi62/bitstream.fw",
104 &dev->dev);
105 if (err)
106 goto nofw;
107
108 err = request_ihex_firmware(&firmware_fw, FIRMWARE_FW, &dev->dev);
109 if (err) {
110 nofw:
David Woodhouseb8e24bf2008-05-30 17:35:47 +0300111 goto wraperr;
112 }
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 /* Assert reset (stop the CPU in the EMI) */
115 err = emi62_set_reset(dev,1);
Greg Kroah-Hartmane9a527d2012-04-20 16:53:40 -0700116 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 goto wraperr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
David Woodhouseb8e24bf2008-05-30 17:35:47 +0300119 rec = (const struct ihex_binrec *)loader_fw->data;
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 /* 1. We need to put the loader for the FPGA into the EZ-USB */
David Woodhouseb8e24bf2008-05-30 17:35:47 +0300122 while (rec) {
123 err = emi62_writememory(dev, be32_to_cpu(rec->addr),
124 rec->data, be16_to_cpu(rec->len),
125 ANCHOR_LOAD_INTERNAL);
Greg Kroah-Hartmane9a527d2012-04-20 16:53:40 -0700126 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 goto wraperr;
David Woodhouseb8e24bf2008-05-30 17:35:47 +0300128 rec = ihex_next_binrec(rec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 }
130
131 /* De-assert reset (let the CPU run) */
132 err = emi62_set_reset(dev,0);
Greg Kroah-Hartmane9a527d2012-04-20 16:53:40 -0700133 if (err < 0)
Oliver Neukum5919a43b2007-10-25 15:42:38 +0200134 goto wraperr;
Monty16c23f72006-05-09 12:37:22 -0700135 msleep(250); /* let device settle */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137 /* 2. We upload the FPGA firmware into the EMI
138 * Note: collect up to 1023 (yes!) bytes and send them with
139 * a single request. This is _much_ faster! */
David Woodhouseb8e24bf2008-05-30 17:35:47 +0300140 rec = (const struct ihex_binrec *)bitstream_fw->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 do {
142 i = 0;
David Woodhouseb8e24bf2008-05-30 17:35:47 +0300143 addr = be32_to_cpu(rec->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145 /* intel hex records are terminated with type 0 element */
David Woodhouseb8e24bf2008-05-30 17:35:47 +0300146 while (rec && (i + be16_to_cpu(rec->len) < FW_LOAD_SIZE)) {
147 memcpy(buf + i, rec->data, be16_to_cpu(rec->len));
148 i += be16_to_cpu(rec->len);
149 rec = ihex_next_binrec(rec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 }
151 err = emi62_writememory(dev, addr, buf, i, ANCHOR_LOAD_FPGA);
Greg Kroah-Hartmane9a527d2012-04-20 16:53:40 -0700152 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 goto wraperr;
Clemens Ladischac06c062009-12-21 15:36:44 -0800154 } while (rec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 /* Assert reset (stop the CPU in the EMI) */
157 err = emi62_set_reset(dev,1);
Greg Kroah-Hartmane9a527d2012-04-20 16:53:40 -0700158 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 goto wraperr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161 /* 3. We need to put the loader for the firmware into the EZ-USB (again...) */
David Woodhouseb8e24bf2008-05-30 17:35:47 +0300162 for (rec = (const struct ihex_binrec *)loader_fw->data;
163 rec; rec = ihex_next_binrec(rec)) {
164 err = emi62_writememory(dev, be32_to_cpu(rec->addr),
165 rec->data, be16_to_cpu(rec->len),
166 ANCHOR_LOAD_INTERNAL);
Greg Kroah-Hartmane9a527d2012-04-20 16:53:40 -0700167 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 goto wraperr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 }
170
171 /* De-assert reset (let the CPU run) */
172 err = emi62_set_reset(dev,0);
Greg Kroah-Hartmane9a527d2012-04-20 16:53:40 -0700173 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 goto wraperr;
Monty16c23f72006-05-09 12:37:22 -0700175 msleep(250); /* let device settle */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177 /* 4. We put the part of the firmware that lies in the external RAM into the EZ-USB */
178
David Woodhouseb8e24bf2008-05-30 17:35:47 +0300179 for (rec = (const struct ihex_binrec *)firmware_fw->data;
180 rec; rec = ihex_next_binrec(rec)) {
181 if (!INTERNAL_RAM(be32_to_cpu(rec->addr))) {
182 err = emi62_writememory(dev, be32_to_cpu(rec->addr),
183 rec->data, be16_to_cpu(rec->len),
184 ANCHOR_LOAD_EXTERNAL);
Greg Kroah-Hartmane9a527d2012-04-20 16:53:40 -0700185 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 goto wraperr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 }
188 }
David Woodhouseb8e24bf2008-05-30 17:35:47 +0300189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 /* Assert reset (stop the CPU in the EMI) */
191 err = emi62_set_reset(dev,1);
Greg Kroah-Hartmane9a527d2012-04-20 16:53:40 -0700192 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 goto wraperr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
David Woodhouseb8e24bf2008-05-30 17:35:47 +0300195 for (rec = (const struct ihex_binrec *)firmware_fw->data;
196 rec; rec = ihex_next_binrec(rec)) {
197 if (INTERNAL_RAM(be32_to_cpu(rec->addr))) {
198 err = emi62_writememory(dev, be32_to_cpu(rec->addr),
199 rec->data, be16_to_cpu(rec->len),
200 ANCHOR_LOAD_EXTERNAL);
Greg Kroah-Hartmane9a527d2012-04-20 16:53:40 -0700201 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 goto wraperr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 }
204 }
David Woodhouseb8e24bf2008-05-30 17:35:47 +0300205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 /* De-assert reset (let the CPU run) */
207 err = emi62_set_reset(dev,0);
Greg Kroah-Hartmane9a527d2012-04-20 16:53:40 -0700208 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 goto wraperr;
Monty16c23f72006-05-09 12:37:22 -0700210 msleep(250); /* let device settle */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
David Woodhouseb8e24bf2008-05-30 17:35:47 +0300212 release_firmware(loader_fw);
213 release_firmware(bitstream_fw);
214 release_firmware(firmware_fw);
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 kfree(buf);
217
218 /* return 1 to fail the driver inialization
219 * and give real driver change to load */
220 return 1;
221
222wraperr:
Greg Kroah-Hartmane9a527d2012-04-20 16:53:40 -0700223 if (err < 0)
224 dev_err(&dev->dev,"%s - error loading firmware: error = %d\n",
225 __func__, err);
David Woodhouseb8e24bf2008-05-30 17:35:47 +0300226 release_firmware(loader_fw);
227 release_firmware(bitstream_fw);
228 release_firmware(firmware_fw);
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 kfree(buf);
231 dev_err(&dev->dev, "Error\n");
232 return err;
233}
234
Greg Kroah-Hartman83957df2012-08-17 17:48:41 -0700235static const struct usb_device_id id_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 { USB_DEVICE(EMI62_VENDOR_ID, EMI62_PRODUCT_ID) },
237 { } /* Terminating entry */
238};
239
240MODULE_DEVICE_TABLE (usb, id_table);
241
242static int emi62_probe(struct usb_interface *intf, const struct usb_device_id *id)
243{
244 struct usb_device *dev = interface_to_usbdev(intf);
245 dev_dbg(&intf->dev, "emi62_probe\n");
246
Greg Kroah-Hartman1b29a372008-08-18 13:21:04 -0700247 dev_info(&intf->dev, "%s start\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 emi62_load_firmware(dev);
250
251 /* do not return the driver context, let real audio driver do that */
252 return -EIO;
253}
254
255static void emi62_disconnect(struct usb_interface *intf)
256{
257}
258
259static struct usb_driver emi62_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 .name = "emi62 - firmware loader",
261 .probe = emi62_probe,
262 .disconnect = emi62_disconnect,
263 .id_table = id_table,
264};
265
Greg Kroah-Hartman65db4302011-11-18 09:34:02 -0800266module_usb_driver(emi62_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Jan Engelhardt96de0e22007-10-19 23:21:04 +0200268MODULE_AUTHOR("Tapio Laxström");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269MODULE_DESCRIPTION("Emagic EMI 6|2m firmware loader.");
270MODULE_LICENSE("GPL");
271
David Woodhouseb8e24bf2008-05-30 17:35:47 +0300272MODULE_FIRMWARE("emi62/loader.fw");
273MODULE_FIRMWARE("emi62/bitstream.fw");
274MODULE_FIRMWARE(FIRMWARE_FW);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275/* vi:ai:syntax=c:sw=8:ts=8:tw=80
276 */