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