1/*
2 * Emagic EMI 2|6 usb audio interface firmware loader.
3 * Copyright (C) 2002
4 * 	Tapio Laxstr�m (tapio.laxstrom@iptime.fi)
5 *
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.1.1.1 2007/08/03 18:53:01 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>
18#include <linux/delay.h>
19
20#define MAX_INTEL_HEX_RECORD_LENGTH 16
21typedef struct _INTEL_HEX_RECORD
22{
23	__u32	length;
24	__u32	address;
25	__u32	type;
26	__u8	data[MAX_INTEL_HEX_RECORD_LENGTH];
27} INTEL_HEX_RECORD, *PINTEL_HEX_RECORD;
28
29/* include firmware (variables)*/
30
31#define SPDIF	/* if you want SPDIF comment next line */
32//#undef SPDIF	/* if you want MIDI uncomment this line */
33
34#ifdef SPDIF
35#  include "emi62_fw_s.h" /* spdif fw */
36#else
37#  include "emi62_fw_m.h" /* midi fw */
38#endif
39
40#define EMI62_VENDOR_ID 		0x086a  /* Emagic Soft-und Hardware GmBH */
41#define EMI62_PRODUCT_ID		0x0110	/* EMI 6|2m without firmware */
42
43#define ANCHOR_LOAD_INTERNAL	0xA0	/* Vendor specific request code for Anchor Upload/Download (This one is implemented in the core) */
44#define ANCHOR_LOAD_EXTERNAL	0xA3	/* This command is not implemented in the core. Requires firmware */
45#define ANCHOR_LOAD_FPGA	0xA5	/* This command is not implemented in the core. Requires firmware. Emagic extension */
46#define MAX_INTERNAL_ADDRESS	0x1B3F	/* This is the highest internal RAM address for the AN2131Q */
47#define CPUCS_REG		0x7F92  /* EZ-USB Control and Status Register.  Bit 0 controls 8051 reset */
48#define INTERNAL_RAM(address)   (address <= MAX_INTERNAL_ADDRESS)
49
50static int emi62_writememory( struct usb_device *dev, int address, unsigned char *data, int length, __u8 bRequest);
51static int emi62_set_reset(struct usb_device *dev, unsigned char reset_bit);
52static int emi62_load_firmware (struct usb_device *dev);
53static int emi62_probe(struct usb_interface *intf, const struct usb_device_id *id);
54static void emi62_disconnect(struct usb_interface *intf);
55static int __init emi62_init (void);
56static void __exit emi62_exit (void);
57
58
59/* thanks to drivers/usb/serial/keyspan_pda.c code */
60static int emi62_writememory (struct usb_device *dev, int address, unsigned char *data, int length, __u8 request)
61{
62	int result;
63	unsigned char *buffer =  kmemdup(data, length, GFP_KERNEL);
64
65	if (!buffer) {
66		err("emi62: kmalloc(%d) failed.", length);
67		return -ENOMEM;
68	}
69	/* Note: usb_control_msg returns negative value on error or length of the
70	 * 		 data that was written! */
71	result = usb_control_msg (dev, usb_sndctrlpipe(dev, 0), request, 0x40, address, 0, buffer, length, 300);
72	kfree (buffer);
73	return result;
74}
75
76/* thanks to drivers/usb/serial/keyspan_pda.c code */
77static int emi62_set_reset (struct usb_device *dev, unsigned char reset_bit)
78{
79	int response;
80	info("%s - %d", __FUNCTION__, reset_bit);
81
82	response = emi62_writememory (dev, CPUCS_REG, &reset_bit, 1, 0xa0);
83	if (response < 0) {
84		err("emi62: set_reset (%d) failed", reset_bit);
85	}
86	return response;
87}
88
89#define FW_LOAD_SIZE		1023
90
91static int emi62_load_firmware (struct usb_device *dev)
92{
93	int err;
94	int i;
95	int pos = 0;	/* Position in hex record */
96	__u32 addr;	/* Address to write */
97	__u8 *buf;
98
99	dev_dbg(&dev->dev, "load_firmware\n");
100	buf = kmalloc(FW_LOAD_SIZE, GFP_KERNEL);
101	if (!buf) {
102		err( "%s - error loading firmware: error = %d", __FUNCTION__, -ENOMEM);
103		err = -ENOMEM;
104		goto wraperr;
105	}
106
107	/* Assert reset (stop the CPU in the EMI) */
108	err = emi62_set_reset(dev,1);
109	if (err < 0) {
110		err("%s - error loading firmware: error = %d", __FUNCTION__, err);
111		goto wraperr;
112	}
113
114	/* 1. We need to put the loader for the FPGA into the EZ-USB */
115	for (i=0; g_emi62_loader[i].type == 0; i++) {
116		err = emi62_writememory(dev, g_emi62_loader[i].address, g_emi62_loader[i].data, g_emi62_loader[i].length, ANCHOR_LOAD_INTERNAL);
117		if (err < 0) {
118			err("%s - error loading firmware: error = %d", __FUNCTION__, err);
119			goto wraperr;
120		}
121	}
122
123	/* De-assert reset (let the CPU run) */
124	err = emi62_set_reset(dev,0);
125	msleep(250);	/* let device settle */
126
127	/* 2. We upload the FPGA firmware into the EMI
128	 * Note: collect up to 1023 (yes!) bytes and send them with
129	 * a single request. This is _much_ faster! */
130	do {
131		i = 0;
132		addr = g_emi62bs[pos].address;
133
134		/* intel hex records are terminated with type 0 element */
135		while ((g_emi62bs[pos].type == 0) && (i + g_emi62bs[pos].length < FW_LOAD_SIZE)) {
136			memcpy(buf + i, g_emi62bs[pos].data, g_emi62bs[pos].length);
137			i += g_emi62bs[pos].length;
138			pos++;
139		}
140		err = emi62_writememory(dev, addr, buf, i, ANCHOR_LOAD_FPGA);
141		if (err < 0) {
142			err("%s - error loading firmware: error = %d", __FUNCTION__, err);
143			goto wraperr;
144		}
145	} while (i > 0);
146
147	/* Assert reset (stop the CPU in the EMI) */
148	err = emi62_set_reset(dev,1);
149	if (err < 0) {
150		err("%s - error loading firmware: error = %d", __FUNCTION__, err);
151		goto wraperr;
152	}
153
154	/* 3. We need to put the loader for the firmware into the EZ-USB (again...) */
155	for (i=0; g_emi62_loader[i].type == 0; i++) {
156		err = emi62_writememory(dev, g_emi62_loader[i].address, g_emi62_loader[i].data, g_emi62_loader[i].length, ANCHOR_LOAD_INTERNAL);
157		if (err < 0) {
158			err("%s - error loading firmware: error = %d", __FUNCTION__, err);
159			goto wraperr;
160		}
161	}
162
163	/* De-assert reset (let the CPU run) */
164	err = emi62_set_reset(dev,0);
165	if (err < 0) {
166		err("%s - error loading firmware: error = %d", __FUNCTION__, err);
167		goto wraperr;
168	}
169	msleep(250);	/* let device settle */
170
171	/* 4. We put the part of the firmware that lies in the external RAM into the EZ-USB */
172
173#ifdef SPDIF
174	for (i=0; g_HexSpdifFw62[i].type == 0; i++) {
175		if (!INTERNAL_RAM(g_HexSpdifFw62[i].address)) {
176			err = emi62_writememory(dev, g_HexSpdifFw62[i].address, g_HexSpdifFw62[i].data, g_HexSpdifFw62[i].length, ANCHOR_LOAD_EXTERNAL);
177			if (err < 0) {
178				err("%s - error loading firmware: error = %d", __FUNCTION__, err);
179				goto wraperr;
180			}
181		}
182	}
183#else /* MIDI */
184	for (i=0; g_HexMidiFw62[i].type == 0; i++) {
185		if (!INTERNAL_RAM(g_HexMidiFw62[i].address)) {
186			err = emi62_writememory(dev, g_HexMidiFw62[i].address, g_HexMidiFw62[i].data, g_HexMidiFw62[i].length, ANCHOR_LOAD_EXTERNAL);
187			if (err < 0) {
188				err("%s - error loading firmware: error = %d\n", __FUNCTION__, err);
189				goto wraperr;
190				return err;
191			}
192		}
193	}
194#endif
195	/* Assert reset (stop the CPU in the EMI) */
196	err = emi62_set_reset(dev,1);
197	if (err < 0) {
198		err("%s - error loading firmware: error = %d", __FUNCTION__, err);
199		goto wraperr;
200	}
201
202#ifdef SPDIF
203	for (i=0; g_HexSpdifFw62[i].type == 0; i++) {
204		if (INTERNAL_RAM(g_HexSpdifFw62[i].address)) {
205			err = emi62_writememory(dev, g_HexSpdifFw62[i].address, g_HexSpdifFw62[i].data, g_HexSpdifFw62[i].length, ANCHOR_LOAD_INTERNAL);
206			if (err < 0) {
207				err("%s - error loading firmware: error = %d", __FUNCTION__, err);
208				goto wraperr;
209			}
210		}
211	}
212#else /* MIDI */
213	for (i=0; g_HexMidiFw62[i].type == 0; i++) {
214		if (INTERNAL_RAM(g_HexMidiFw62[i].address)) {
215			err = emi62_writememory(dev, g_HexMidiFw62[i].address, g_HexMidiFw62[i].data, g_HexMidiFw62[i].length, ANCHOR_LOAD_INTERNAL);
216			if (err < 0) {
217				err("%s - error loading firmware: error = %d\n", __FUNCTION__, err);
218				goto wraperr;
219			}
220		}
221	}
222#endif
223
224	/* De-assert reset (let the CPU run) */
225	err = emi62_set_reset(dev,0);
226	if (err < 0) {
227		err("%s - error loading firmware: error = %d", __FUNCTION__, err);
228		goto wraperr;
229	}
230	msleep(250);	/* let device settle */
231
232	kfree(buf);
233
234	/* return 1 to fail the driver inialization
235	 * and give real driver change to load */
236	return 1;
237
238wraperr:
239	kfree(buf);
240	dev_err(&dev->dev, "Error\n");
241	return err;
242}
243
244static __devinitdata struct usb_device_id id_table [] = {
245	{ USB_DEVICE(EMI62_VENDOR_ID, EMI62_PRODUCT_ID) },
246	{ }                                             /* Terminating entry */
247};
248
249MODULE_DEVICE_TABLE (usb, id_table);
250
251static int emi62_probe(struct usb_interface *intf, const struct usb_device_id *id)
252{
253	struct usb_device *dev = interface_to_usbdev(intf);
254	dev_dbg(&intf->dev, "emi62_probe\n");
255
256	info("%s start", __FUNCTION__);
257
258	emi62_load_firmware(dev);
259
260	/* do not return the driver context, let real audio driver do that */
261	return -EIO;
262}
263
264static void emi62_disconnect(struct usb_interface *intf)
265{
266}
267
268static struct usb_driver emi62_driver = {
269	.name		= "emi62 - firmware loader",
270	.probe		= emi62_probe,
271	.disconnect	= emi62_disconnect,
272	.id_table	= id_table,
273};
274
275static int __init emi62_init (void)
276{
277	int retval;
278	retval = usb_register (&emi62_driver);
279	if (retval)
280		printk(KERN_ERR "adi-emi: registration failed\n");
281	return retval;
282}
283
284static void __exit emi62_exit (void)
285{
286	usb_deregister (&emi62_driver);
287}
288
289module_init(emi62_init);
290module_exit(emi62_exit);
291
292MODULE_AUTHOR("tapio laxstr�m");
293MODULE_DESCRIPTION("Emagic EMI 6|2m firmware loader.");
294MODULE_LICENSE("GPL");
295
296/* vi:ai:syntax=c:sw=8:ts=8:tw=80
297 */
298