blob: 20886c21e7397b7cc5d2ea121399dd761e2bd773 [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.
9 *
10 * $Id: emi62.c,v 1.15 2002/04/23 06:13:59 tapio Exp $
11 */
12#include <linux/kernel.h>
13#include <linux/errno.h>
14#include <linux/slab.h>
15#include <linux/init.h>
16#include <linux/module.h>
17#include <linux/usb.h>
Monty16c23f72006-05-09 12:37:22 -070018#include <linux/delay.h>
David Woodhouseb8e24bf2008-05-30 17:35:47 +030019#include <linux/firmware.h>
20#include <linux/ihex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22/* include firmware (variables)*/
23
24/* FIXME: This is quick and dirty solution! */
25#define SPDIF /* if you want SPDIF comment next line */
26//#undef SPDIF /* if you want MIDI uncomment this line */
27
28#ifdef SPDIF
David Woodhouseb8e24bf2008-05-30 17:35:47 +030029#define FIRMWARE_FW "emi62/spdif.fw"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#else
David Woodhouseb8e24bf2008-05-30 17:35:47 +030031#define FIRMWARE_FW "emi62/midi.fw"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#endif
33
34#define EMI62_VENDOR_ID 0x086a /* Emagic Soft-und Hardware GmBH */
35#define EMI62_PRODUCT_ID 0x0110 /* EMI 6|2m without firmware */
36
37#define ANCHOR_LOAD_INTERNAL 0xA0 /* Vendor specific request code for Anchor Upload/Download (This one is implemented in the core) */
38#define ANCHOR_LOAD_EXTERNAL 0xA3 /* This command is not implemented in the core. Requires firmware */
39#define ANCHOR_LOAD_FPGA 0xA5 /* This command is not implemented in the core. Requires firmware. Emagic extension */
40#define MAX_INTERNAL_ADDRESS 0x1B3F /* This is the highest internal RAM address for the AN2131Q */
41#define CPUCS_REG 0x7F92 /* EZ-USB Control and Status Register. Bit 0 controls 8051 reset */
42#define INTERNAL_RAM(address) (address <= MAX_INTERNAL_ADDRESS)
43
David Woodhouseb8e24bf2008-05-30 17:35:47 +030044static int emi62_writememory(struct usb_device *dev, int address,
45 const unsigned char *data, int length,
46 __u8 bRequest);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047static int emi62_set_reset(struct usb_device *dev, unsigned char reset_bit);
48static int emi62_load_firmware (struct usb_device *dev);
49static int emi62_probe(struct usb_interface *intf, const struct usb_device_id *id);
50static void emi62_disconnect(struct usb_interface *intf);
51static int __init emi62_init (void);
52static void __exit emi62_exit (void);
53
54
55/* thanks to drivers/usb/serial/keyspan_pda.c code */
David Woodhouseb8e24bf2008-05-30 17:35:47 +030056static int emi62_writememory(struct usb_device *dev, int address,
57 const unsigned char *data, int length,
58 __u8 request)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
60 int result;
Eric Sesterhenn5d7efe52006-10-26 21:06:24 +020061 unsigned char *buffer = kmemdup(data, length, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63 if (!buffer) {
64 err("emi62: kmalloc(%d) failed.", length);
65 return -ENOMEM;
66 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 /* Note: usb_control_msg returns negative value on error or length of the
68 * data that was written! */
69 result = usb_control_msg (dev, usb_sndctrlpipe(dev, 0), request, 0x40, address, 0, buffer, length, 300);
70 kfree (buffer);
71 return result;
72}
73
74/* thanks to drivers/usb/serial/keyspan_pda.c code */
75static int emi62_set_reset (struct usb_device *dev, unsigned char reset_bit)
76{
77 int response;
Harvey Harrison441b62c2008-03-03 16:08:34 -080078 info("%s - %d", __func__, reset_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80 response = emi62_writememory (dev, CPUCS_REG, &reset_bit, 1, 0xa0);
81 if (response < 0) {
82 err("emi62: set_reset (%d) failed", reset_bit);
83 }
84 return response;
85}
86
87#define FW_LOAD_SIZE 1023
88
89static int emi62_load_firmware (struct usb_device *dev)
90{
David Woodhouseb8e24bf2008-05-30 17:35:47 +030091 const struct firmware *loader_fw = NULL;
92 const struct firmware *bitstream_fw = NULL;
93 const struct firmware *firmware_fw = NULL;
94 const struct ihex_binrec *rec;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 int err;
96 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 __u32 addr; /* Address to write */
98 __u8 *buf;
99
100 dev_dbg(&dev->dev, "load_firmware\n");
101 buf = kmalloc(FW_LOAD_SIZE, GFP_KERNEL);
102 if (!buf) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800103 err( "%s - error loading firmware: error = %d", __func__, -ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 err = -ENOMEM;
105 goto wraperr;
106 }
107
David Woodhouseb8e24bf2008-05-30 17:35:47 +0300108 err = request_ihex_firmware(&loader_fw, "emi62/loader.fw", &dev->dev);
109 if (err)
110 goto nofw;
111
112 err = request_ihex_firmware(&bitstream_fw, "emi62/bitstream.fw",
113 &dev->dev);
114 if (err)
115 goto nofw;
116
117 err = request_ihex_firmware(&firmware_fw, FIRMWARE_FW, &dev->dev);
118 if (err) {
119 nofw:
120 err( "%s - request_firmware() failed", __func__);
121 goto wraperr;
122 }
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 /* Assert reset (stop the CPU in the EMI) */
125 err = emi62_set_reset(dev,1);
126 if (err < 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800127 err("%s - error loading firmware: error = %d", __func__, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 goto wraperr;
129 }
130
David Woodhouseb8e24bf2008-05-30 17:35:47 +0300131 rec = (const struct ihex_binrec *)loader_fw->data;
132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 /* 1. We need to put the loader for the FPGA into the EZ-USB */
David Woodhouseb8e24bf2008-05-30 17:35:47 +0300134 while (rec) {
135 err = emi62_writememory(dev, be32_to_cpu(rec->addr),
136 rec->data, be16_to_cpu(rec->len),
137 ANCHOR_LOAD_INTERNAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 if (err < 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800139 err("%s - error loading firmware: error = %d", __func__, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 goto wraperr;
141 }
David Woodhouseb8e24bf2008-05-30 17:35:47 +0300142 rec = ihex_next_binrec(rec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 }
144
145 /* De-assert reset (let the CPU run) */
146 err = emi62_set_reset(dev,0);
Oliver Neukum5919a43b2007-10-25 15:42:38 +0200147 if (err < 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800148 err("%s - error loading firmware: error = %d", __func__, err);
Oliver Neukum5919a43b2007-10-25 15:42:38 +0200149 goto wraperr;
150 }
Monty16c23f72006-05-09 12:37:22 -0700151 msleep(250); /* let device settle */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153 /* 2. We upload the FPGA firmware into the EMI
154 * Note: collect up to 1023 (yes!) bytes and send them with
155 * a single request. This is _much_ faster! */
David Woodhouseb8e24bf2008-05-30 17:35:47 +0300156 rec = (const struct ihex_binrec *)bitstream_fw->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 do {
158 i = 0;
David Woodhouseb8e24bf2008-05-30 17:35:47 +0300159 addr = be32_to_cpu(rec->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161 /* intel hex records are terminated with type 0 element */
David Woodhouseb8e24bf2008-05-30 17:35:47 +0300162 while (rec && (i + be16_to_cpu(rec->len) < FW_LOAD_SIZE)) {
163 memcpy(buf + i, rec->data, be16_to_cpu(rec->len));
164 i += be16_to_cpu(rec->len);
165 rec = ihex_next_binrec(rec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 }
167 err = emi62_writememory(dev, addr, buf, i, ANCHOR_LOAD_FPGA);
168 if (err < 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800169 err("%s - error loading firmware: error = %d", __func__, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 goto wraperr;
171 }
172 } while (i > 0);
173
174 /* Assert reset (stop the CPU in the EMI) */
175 err = emi62_set_reset(dev,1);
176 if (err < 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800177 err("%s - error loading firmware: error = %d", __func__, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 goto wraperr;
179 }
180
181 /* 3. We need to put the loader for the firmware into the EZ-USB (again...) */
David Woodhouseb8e24bf2008-05-30 17:35:47 +0300182 for (rec = (const struct ihex_binrec *)loader_fw->data;
183 rec; rec = ihex_next_binrec(rec)) {
184 err = emi62_writememory(dev, be32_to_cpu(rec->addr),
185 rec->data, be16_to_cpu(rec->len),
186 ANCHOR_LOAD_INTERNAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 if (err < 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800188 err("%s - error loading firmware: error = %d", __func__, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 goto wraperr;
190 }
191 }
192
193 /* De-assert reset (let the CPU run) */
194 err = emi62_set_reset(dev,0);
195 if (err < 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800196 err("%s - error loading firmware: error = %d", __func__, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 goto wraperr;
198 }
Monty16c23f72006-05-09 12:37:22 -0700199 msleep(250); /* let device settle */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 /* 4. We put the part of the firmware that lies in the external RAM into the EZ-USB */
202
David Woodhouseb8e24bf2008-05-30 17:35:47 +0300203 for (rec = (const struct ihex_binrec *)firmware_fw->data;
204 rec; rec = ihex_next_binrec(rec)) {
205 if (!INTERNAL_RAM(be32_to_cpu(rec->addr))) {
206 err = emi62_writememory(dev, be32_to_cpu(rec->addr),
207 rec->data, be16_to_cpu(rec->len),
208 ANCHOR_LOAD_EXTERNAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 if (err < 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800210 err("%s - error loading firmware: error = %d", __func__, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 goto wraperr;
212 }
213 }
214 }
David Woodhouseb8e24bf2008-05-30 17:35:47 +0300215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 /* Assert reset (stop the CPU in the EMI) */
217 err = emi62_set_reset(dev,1);
218 if (err < 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800219 err("%s - error loading firmware: error = %d", __func__, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 goto wraperr;
221 }
222
David Woodhouseb8e24bf2008-05-30 17:35:47 +0300223 for (rec = (const struct ihex_binrec *)firmware_fw->data;
224 rec; rec = ihex_next_binrec(rec)) {
225 if (INTERNAL_RAM(be32_to_cpu(rec->addr))) {
226 err = emi62_writememory(dev, be32_to_cpu(rec->addr),
227 rec->data, be16_to_cpu(rec->len),
228 ANCHOR_LOAD_EXTERNAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 if (err < 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800230 err("%s - error loading firmware: error = %d", __func__, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 goto wraperr;
232 }
233 }
234 }
David Woodhouseb8e24bf2008-05-30 17:35:47 +0300235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 /* De-assert reset (let the CPU run) */
237 err = emi62_set_reset(dev,0);
238 if (err < 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800239 err("%s - error loading firmware: error = %d", __func__, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 goto wraperr;
241 }
Monty16c23f72006-05-09 12:37:22 -0700242 msleep(250); /* let device settle */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
David Woodhouseb8e24bf2008-05-30 17:35:47 +0300244 release_firmware(loader_fw);
245 release_firmware(bitstream_fw);
246 release_firmware(firmware_fw);
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 kfree(buf);
249
250 /* return 1 to fail the driver inialization
251 * and give real driver change to load */
252 return 1;
253
254wraperr:
David Woodhouseb8e24bf2008-05-30 17:35:47 +0300255 release_firmware(loader_fw);
256 release_firmware(bitstream_fw);
257 release_firmware(firmware_fw);
258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 kfree(buf);
260 dev_err(&dev->dev, "Error\n");
261 return err;
262}
263
264static __devinitdata struct usb_device_id id_table [] = {
265 { USB_DEVICE(EMI62_VENDOR_ID, EMI62_PRODUCT_ID) },
266 { } /* Terminating entry */
267};
268
269MODULE_DEVICE_TABLE (usb, id_table);
270
271static int emi62_probe(struct usb_interface *intf, const struct usb_device_id *id)
272{
273 struct usb_device *dev = interface_to_usbdev(intf);
274 dev_dbg(&intf->dev, "emi62_probe\n");
275
Harvey Harrison441b62c2008-03-03 16:08:34 -0800276 info("%s start", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278 emi62_load_firmware(dev);
279
280 /* do not return the driver context, let real audio driver do that */
281 return -EIO;
282}
283
284static void emi62_disconnect(struct usb_interface *intf)
285{
286}
287
288static struct usb_driver emi62_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 .name = "emi62 - firmware loader",
290 .probe = emi62_probe,
291 .disconnect = emi62_disconnect,
292 .id_table = id_table,
293};
294
295static int __init emi62_init (void)
296{
297 int retval;
298 retval = usb_register (&emi62_driver);
299 if (retval)
300 printk(KERN_ERR "adi-emi: registration failed\n");
301 return retval;
302}
303
304static void __exit emi62_exit (void)
305{
306 usb_deregister (&emi62_driver);
307}
308
309module_init(emi62_init);
310module_exit(emi62_exit);
311
Jan Engelhardt96de0e22007-10-19 23:21:04 +0200312MODULE_AUTHOR("Tapio Laxström");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313MODULE_DESCRIPTION("Emagic EMI 6|2m firmware loader.");
314MODULE_LICENSE("GPL");
315
David Woodhouseb8e24bf2008-05-30 17:35:47 +0300316MODULE_FIRMWARE("emi62/loader.fw");
317MODULE_FIRMWARE("emi62/bitstream.fw");
318MODULE_FIRMWARE(FIRMWARE_FW);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319/* vi:ai:syntax=c:sw=8:ts=8:tw=80
320 */