xref: /linux/drivers/usb/misc/emi26.c (revision 664b0bae0b87f69bc9deb098f5e0158b9cf18e04)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Emagic EMI 2|6 usb audio interface firmware loader.
4  * Copyright (C) 2002
5  * 	Tapio Laxström (tapio.laxstrom@iptime.fi)
6  *
7  * emi26.c,v 1.13 2002/03/08 13:10:26 tapio Exp
8  */
9 #include <linux/kernel.h>
10 #include <linux/errno.h>
11 #include <linux/slab.h>
12 #include <linux/module.h>
13 #include <linux/usb.h>
14 #include <linux/delay.h>
15 #include <linux/firmware.h>
16 #include <linux/ihex.h>
17 
18 #define EMI26_VENDOR_ID 		0x086a  /* Emagic Soft-und Hardware GmBH */
19 #define EMI26_PRODUCT_ID		0x0100	/* EMI 2|6 without firmware */
20 #define EMI26B_PRODUCT_ID		0x0102	/* EMI 2|6 without firmware */
21 
22 #define ANCHOR_LOAD_INTERNAL	0xA0	/* Vendor specific request code for Anchor Upload/Download (This one is implemented in the core) */
23 #define ANCHOR_LOAD_EXTERNAL	0xA3	/* This command is not implemented in the core. Requires firmware */
24 #define ANCHOR_LOAD_FPGA	0xA5	/* This command is not implemented in the core. Requires firmware. Emagic extension */
25 #define MAX_INTERNAL_ADDRESS	0x1B3F	/* This is the highest internal RAM address for the AN2131Q */
26 #define CPUCS_REG		0x7F92  /* EZ-USB Control and Status Register.  Bit 0 controls 8051 reset */
27 #define INTERNAL_RAM(address)   (address <= MAX_INTERNAL_ADDRESS)
28 
29 static int emi26_writememory( struct usb_device *dev, int address,
30 			      const unsigned char *data, int length,
31 			      __u8 bRequest);
32 static int emi26_set_reset(struct usb_device *dev, unsigned char reset_bit);
33 static int emi26_load_firmware (struct usb_device *dev);
34 static int emi26_probe(struct usb_interface *intf, const struct usb_device_id *id);
35 static void emi26_disconnect(struct usb_interface *intf);
36 
37 /* thanks to drivers/usb/serial/keyspan_pda.c code */
emi26_writememory(struct usb_device * dev,int address,const unsigned char * data,int length,__u8 request)38 static int emi26_writememory (struct usb_device *dev, int address,
39 			      const unsigned char *data, int length,
40 			      __u8 request)
41 {
42 	int result;
43 	unsigned char *buffer =  kmemdup(data, length, GFP_KERNEL);
44 
45 	if (!buffer) {
46 		dev_err(&dev->dev, "kmalloc(%d) failed.\n", length);
47 		return -ENOMEM;
48 	}
49 	/* Note: usb_control_msg returns negative value on error or length of the
50 	 * 		 data that was written! */
51 	result = usb_control_msg (dev, usb_sndctrlpipe(dev, 0), request, 0x40, address, 0, buffer, length, 300);
52 	kfree (buffer);
53 	return result;
54 }
55 
56 /* thanks to drivers/usb/serial/keyspan_pda.c code */
emi26_set_reset(struct usb_device * dev,unsigned char reset_bit)57 static int emi26_set_reset (struct usb_device *dev, unsigned char reset_bit)
58 {
59 	int response;
60 	dev_info(&dev->dev, "%s - %d\n", __func__, reset_bit);
61 	/* printk(KERN_DEBUG "%s - %d", __func__, reset_bit); */
62 	response = emi26_writememory (dev, CPUCS_REG, &reset_bit, 1, 0xa0);
63 	if (response < 0) {
64 		dev_err(&dev->dev, "set_reset (%d) failed\n", reset_bit);
65 	}
66 	return response;
67 }
68 
69 #define FW_LOAD_SIZE		1023
70 
emi26_load_firmware(struct usb_device * dev)71 static int emi26_load_firmware (struct usb_device *dev)
72 {
73 	const struct firmware *loader_fw = NULL;
74 	const struct firmware *bitstream_fw = NULL;
75 	const struct firmware *firmware_fw = NULL;
76 	const struct ihex_binrec *rec;
77 	int err = -ENOMEM;
78 	int i;
79 	__u32 addr;	/* Address to write */
80 	__u8 *buf;
81 
82 	buf = kmalloc(FW_LOAD_SIZE, GFP_KERNEL);
83 	if (!buf)
84 		goto wraperr;
85 
86 	err = request_ihex_firmware(&loader_fw, "emi26/loader.fw", &dev->dev);
87 	if (err)
88 		goto nofw;
89 
90 	err = request_ihex_firmware(&bitstream_fw, "emi26/bitstream.fw",
91 				    &dev->dev);
92 	if (err)
93 		goto nofw;
94 
95 	err = request_ihex_firmware(&firmware_fw, "emi26/firmware.fw",
96 				    &dev->dev);
97 	if (err) {
98 	nofw:
99 		dev_err(&dev->dev, "%s - request_firmware() failed\n",
100 			__func__);
101 		goto wraperr;
102 	}
103 
104 	/* Assert reset (stop the CPU in the EMI) */
105 	err = emi26_set_reset(dev,1);
106 	if (err < 0)
107 		goto wraperr;
108 
109 	rec = (const struct ihex_binrec *)loader_fw->data;
110 	/* 1. We need to put the loader for the FPGA into the EZ-USB */
111 	while (rec) {
112 		err = emi26_writememory(dev, be32_to_cpu(rec->addr),
113 					rec->data, be16_to_cpu(rec->len),
114 					ANCHOR_LOAD_INTERNAL);
115 		if (err < 0)
116 			goto wraperr;
117 		rec = ihex_next_binrec(rec);
118 	}
119 
120 	/* De-assert reset (let the CPU run) */
121 	err = emi26_set_reset(dev,0);
122 	if (err < 0)
123 		goto wraperr;
124 	msleep(250);	/* let device settle */
125 
126 	/* 2. We upload the FPGA firmware into the EMI
127 	 * Note: collect up to 1023 (yes!) bytes and send them with
128 	 * a single request. This is _much_ faster! */
129 	rec = (const struct ihex_binrec *)bitstream_fw->data;
130 	do {
131 		i = 0;
132 		addr = be32_to_cpu(rec->addr);
133 
134 		/* intel hex records are terminated with type 0 element */
135 		while (rec && (i + be16_to_cpu(rec->len) < FW_LOAD_SIZE)) {
136 			memcpy(buf + i, rec->data, be16_to_cpu(rec->len));
137 			i += be16_to_cpu(rec->len);
138 			rec = ihex_next_binrec(rec);
139 		}
140 		err = emi26_writememory(dev, addr, buf, i, ANCHOR_LOAD_FPGA);
141 		if (err < 0)
142 			goto wraperr;
143 	} while (rec);
144 
145 	/* Assert reset (stop the CPU in the EMI) */
146 	err = emi26_set_reset(dev,1);
147 	if (err < 0)
148 		goto wraperr;
149 
150 	/* 3. We need to put the loader for the firmware into the EZ-USB (again...) */
151 	for (rec = (const struct ihex_binrec *)loader_fw->data;
152 	     rec; rec = ihex_next_binrec(rec)) {
153 		err = emi26_writememory(dev, be32_to_cpu(rec->addr),
154 					rec->data, be16_to_cpu(rec->len),
155 					ANCHOR_LOAD_INTERNAL);
156 		if (err < 0)
157 			goto wraperr;
158 	}
159 	msleep(250);	/* let device settle */
160 
161 	/* De-assert reset (let the CPU run) */
162 	err = emi26_set_reset(dev,0);
163 	if (err < 0)
164 		goto wraperr;
165 
166 	/* 4. We put the part of the firmware that lies in the external RAM into the EZ-USB */
167 
168 	for (rec = (const struct ihex_binrec *)firmware_fw->data;
169 	     rec; rec = ihex_next_binrec(rec)) {
170 		if (!INTERNAL_RAM(be32_to_cpu(rec->addr))) {
171 			err = emi26_writememory(dev, be32_to_cpu(rec->addr),
172 						rec->data, be16_to_cpu(rec->len),
173 						ANCHOR_LOAD_EXTERNAL);
174 			if (err < 0)
175 				goto wraperr;
176 		}
177 	}
178 
179 	/* Assert reset (stop the CPU in the EMI) */
180 	err = emi26_set_reset(dev,1);
181 	if (err < 0)
182 		goto wraperr;
183 
184 	for (rec = (const struct ihex_binrec *)firmware_fw->data;
185 	     rec; rec = ihex_next_binrec(rec)) {
186 		if (INTERNAL_RAM(be32_to_cpu(rec->addr))) {
187 			err = emi26_writememory(dev, be32_to_cpu(rec->addr),
188 						rec->data, be16_to_cpu(rec->len),
189 						ANCHOR_LOAD_INTERNAL);
190 			if (err < 0)
191 				goto wraperr;
192 		}
193 	}
194 
195 	/* De-assert reset (let the CPU run) */
196 	err = emi26_set_reset(dev,0);
197 	if (err < 0)
198 		goto wraperr;
199 	msleep(250);	/* let device settle */
200 
201 	/* return 1 to fail the driver inialization
202 	 * and give real driver change to load */
203 	err = 1;
204 
205 wraperr:
206 	if (err < 0)
207 		dev_err(&dev->dev,"%s - error loading firmware: error = %d\n",
208 			__func__, err);
209 
210 	release_firmware(loader_fw);
211 	release_firmware(bitstream_fw);
212 	release_firmware(firmware_fw);
213 
214 	kfree(buf);
215 	return err;
216 }
217 
218 static const struct usb_device_id id_table[] = {
219 	{ USB_DEVICE(EMI26_VENDOR_ID, EMI26_PRODUCT_ID) },
220 	{ USB_DEVICE(EMI26_VENDOR_ID, EMI26B_PRODUCT_ID) },
221 	{ }                                             /* Terminating entry */
222 };
223 
224 MODULE_DEVICE_TABLE (usb, id_table);
225 
emi26_probe(struct usb_interface * intf,const struct usb_device_id * id)226 static int emi26_probe(struct usb_interface *intf, const struct usb_device_id *id)
227 {
228 	struct usb_device *dev = interface_to_usbdev(intf);
229 
230 	dev_info(&intf->dev, "%s start\n", __func__);
231 
232 	emi26_load_firmware(dev);
233 
234 	/* do not return the driver context, let real audio driver do that */
235 	return -EIO;
236 }
237 
emi26_disconnect(struct usb_interface * intf)238 static void emi26_disconnect(struct usb_interface *intf)
239 {
240 }
241 
242 static struct usb_driver emi26_driver = {
243 	.name		= "emi26 - firmware loader",
244 	.probe		= emi26_probe,
245 	.disconnect	= emi26_disconnect,
246 	.id_table	= id_table,
247 };
248 
249 module_usb_driver(emi26_driver);
250 
251 MODULE_AUTHOR("Tapio Laxström");
252 MODULE_DESCRIPTION("Emagic EMI 2|6 firmware loader.");
253 MODULE_LICENSE("GPL");
254 
255 MODULE_FIRMWARE("emi26/loader.fw");
256 MODULE_FIRMWARE("emi26/bitstream.fw");
257 MODULE_FIRMWARE("emi26/firmware.fw");
258 /* vi:ai:syntax=c:sw=8:ts=8:tw=80
259  */
260