xref: /openbmc/linux/drivers/usb/misc/emi26.c (revision 5d62afbe)
15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * Emagic EMI 2|6 usb audio interface firmware loader.
41da177e4SLinus Torvalds  * Copyright (C) 2002
596de0e25SJan Engelhardt  * 	Tapio Laxström (tapio.laxstrom@iptime.fi)
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * emi26.c,v 1.13 2002/03/08 13:10:26 tapio Exp
81da177e4SLinus Torvalds  */
91da177e4SLinus Torvalds #include <linux/kernel.h>
101da177e4SLinus Torvalds #include <linux/errno.h>
111da177e4SLinus Torvalds #include <linux/slab.h>
121da177e4SLinus Torvalds #include <linux/module.h>
131da177e4SLinus Torvalds #include <linux/usb.h>
1416c23f7dSMonty #include <linux/delay.h>
15ae93a55bSDavid Woodhouse #include <linux/firmware.h>
16ae93a55bSDavid Woodhouse #include <linux/ihex.h>
171da177e4SLinus Torvalds 
181da177e4SLinus Torvalds #define EMI26_VENDOR_ID 		0x086a  /* Emagic Soft-und Hardware GmBH */
191da177e4SLinus Torvalds #define EMI26_PRODUCT_ID		0x0100	/* EMI 2|6 without firmware */
201da177e4SLinus Torvalds #define EMI26B_PRODUCT_ID		0x0102	/* EMI 2|6 without firmware */
211da177e4SLinus Torvalds 
221da177e4SLinus Torvalds #define ANCHOR_LOAD_INTERNAL	0xA0	/* Vendor specific request code for Anchor Upload/Download (This one is implemented in the core) */
231da177e4SLinus Torvalds #define ANCHOR_LOAD_EXTERNAL	0xA3	/* This command is not implemented in the core. Requires firmware */
241da177e4SLinus Torvalds #define ANCHOR_LOAD_FPGA	0xA5	/* This command is not implemented in the core. Requires firmware. Emagic extension */
251da177e4SLinus Torvalds #define MAX_INTERNAL_ADDRESS	0x1B3F	/* This is the highest internal RAM address for the AN2131Q */
261da177e4SLinus Torvalds #define CPUCS_REG		0x7F92  /* EZ-USB Control and Status Register.  Bit 0 controls 8051 reset */
271da177e4SLinus Torvalds #define INTERNAL_RAM(address)   (address <= MAX_INTERNAL_ADDRESS)
281da177e4SLinus Torvalds 
29ae93a55bSDavid Woodhouse static int emi26_writememory( struct usb_device *dev, int address,
30ae93a55bSDavid Woodhouse 			      const unsigned char *data, int length,
31ae93a55bSDavid Woodhouse 			      __u8 bRequest);
321da177e4SLinus Torvalds static int emi26_set_reset(struct usb_device *dev, unsigned char reset_bit);
331da177e4SLinus Torvalds static int emi26_load_firmware (struct usb_device *dev);
341da177e4SLinus Torvalds static int emi26_probe(struct usb_interface *intf, const struct usb_device_id *id);
351da177e4SLinus Torvalds static void emi26_disconnect(struct usb_interface *intf);
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds /* 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)38ae93a55bSDavid Woodhouse static int emi26_writememory (struct usb_device *dev, int address,
39ae93a55bSDavid Woodhouse 			      const unsigned char *data, int length,
40ae93a55bSDavid Woodhouse 			      __u8 request)
411da177e4SLinus Torvalds {
421da177e4SLinus Torvalds 	int result;
435d7efe5bSEric Sesterhenn 	unsigned char *buffer =  kmemdup(data, length, GFP_KERNEL);
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds 	if (!buffer) {
46fd3f1917SGreg Kroah-Hartman 		dev_err(&dev->dev, "kmalloc(%d) failed.\n", length);
471da177e4SLinus Torvalds 		return -ENOMEM;
481da177e4SLinus Torvalds 	}
491da177e4SLinus Torvalds 	/* Note: usb_control_msg returns negative value on error or length of the
501da177e4SLinus Torvalds 	 * 		 data that was written! */
511da177e4SLinus Torvalds 	result = usb_control_msg (dev, usb_sndctrlpipe(dev, 0), request, 0x40, address, 0, buffer, length, 300);
521da177e4SLinus Torvalds 	kfree (buffer);
531da177e4SLinus Torvalds 	return result;
541da177e4SLinus Torvalds }
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds /* thanks to drivers/usb/serial/keyspan_pda.c code */
emi26_set_reset(struct usb_device * dev,unsigned char reset_bit)571da177e4SLinus Torvalds static int emi26_set_reset (struct usb_device *dev, unsigned char reset_bit)
581da177e4SLinus Torvalds {
591da177e4SLinus Torvalds 	int response;
601b29a375SGreg Kroah-Hartman 	dev_info(&dev->dev, "%s - %d\n", __func__, reset_bit);
61441b62c1SHarvey Harrison 	/* printk(KERN_DEBUG "%s - %d", __func__, reset_bit); */
621da177e4SLinus Torvalds 	response = emi26_writememory (dev, CPUCS_REG, &reset_bit, 1, 0xa0);
631da177e4SLinus Torvalds 	if (response < 0) {
64fd3f1917SGreg Kroah-Hartman 		dev_err(&dev->dev, "set_reset (%d) failed\n", reset_bit);
651da177e4SLinus Torvalds 	}
661da177e4SLinus Torvalds 	return response;
671da177e4SLinus Torvalds }
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds #define FW_LOAD_SIZE		1023
701da177e4SLinus Torvalds 
emi26_load_firmware(struct usb_device * dev)711da177e4SLinus Torvalds static int emi26_load_firmware (struct usb_device *dev)
721da177e4SLinus Torvalds {
73ae93a55bSDavid Woodhouse 	const struct firmware *loader_fw = NULL;
74ae93a55bSDavid Woodhouse 	const struct firmware *bitstream_fw = NULL;
75ae93a55bSDavid Woodhouse 	const struct firmware *firmware_fw = NULL;
76ae93a55bSDavid Woodhouse 	const struct ihex_binrec *rec;
77b412284bSGreg Kroah-Hartman 	int err = -ENOMEM;
781da177e4SLinus Torvalds 	int i;
791da177e4SLinus Torvalds 	__u32 addr;	/* Address to write */
801da177e4SLinus Torvalds 	__u8 *buf;
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds 	buf = kmalloc(FW_LOAD_SIZE, GFP_KERNEL);
83b412284bSGreg Kroah-Hartman 	if (!buf)
841da177e4SLinus Torvalds 		goto wraperr;
851da177e4SLinus Torvalds 
86ae93a55bSDavid Woodhouse 	err = request_ihex_firmware(&loader_fw, "emi26/loader.fw", &dev->dev);
87ae93a55bSDavid Woodhouse 	if (err)
88ae93a55bSDavid Woodhouse 		goto nofw;
89ae93a55bSDavid Woodhouse 
90ae93a55bSDavid Woodhouse 	err = request_ihex_firmware(&bitstream_fw, "emi26/bitstream.fw",
91ae93a55bSDavid Woodhouse 				    &dev->dev);
92ae93a55bSDavid Woodhouse 	if (err)
93ae93a55bSDavid Woodhouse 		goto nofw;
94ae93a55bSDavid Woodhouse 
95ae93a55bSDavid Woodhouse 	err = request_ihex_firmware(&firmware_fw, "emi26/firmware.fw",
96ae93a55bSDavid Woodhouse 				    &dev->dev);
97ae93a55bSDavid Woodhouse 	if (err) {
98ae93a55bSDavid Woodhouse 	nofw:
99fd3f1917SGreg Kroah-Hartman 		dev_err(&dev->dev, "%s - request_firmware() failed\n",
100fd3f1917SGreg Kroah-Hartman 			__func__);
101ae93a55bSDavid Woodhouse 		goto wraperr;
102ae93a55bSDavid Woodhouse 	}
103ae93a55bSDavid Woodhouse 
1041da177e4SLinus Torvalds 	/* Assert reset (stop the CPU in the EMI) */
1051da177e4SLinus Torvalds 	err = emi26_set_reset(dev,1);
106b412284bSGreg Kroah-Hartman 	if (err < 0)
1071da177e4SLinus Torvalds 		goto wraperr;
1081da177e4SLinus Torvalds 
109ae93a55bSDavid Woodhouse 	rec = (const struct ihex_binrec *)loader_fw->data;
1101da177e4SLinus Torvalds 	/* 1. We need to put the loader for the FPGA into the EZ-USB */
111ae93a55bSDavid Woodhouse 	while (rec) {
112ae93a55bSDavid Woodhouse 		err = emi26_writememory(dev, be32_to_cpu(rec->addr),
113ae93a55bSDavid Woodhouse 					rec->data, be16_to_cpu(rec->len),
114ae93a55bSDavid Woodhouse 					ANCHOR_LOAD_INTERNAL);
115b412284bSGreg Kroah-Hartman 		if (err < 0)
1161da177e4SLinus Torvalds 			goto wraperr;
117ae93a55bSDavid Woodhouse 		rec = ihex_next_binrec(rec);
1181da177e4SLinus Torvalds 	}
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds 	/* De-assert reset (let the CPU run) */
1211da177e4SLinus Torvalds 	err = emi26_set_reset(dev,0);
122b412284bSGreg Kroah-Hartman 	if (err < 0)
123cf4cf0bbSOliver Neukum 		goto wraperr;
12416c23f7dSMonty 	msleep(250);	/* let device settle */
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds 	/* 2. We upload the FPGA firmware into the EMI
1271da177e4SLinus Torvalds 	 * Note: collect up to 1023 (yes!) bytes and send them with
1281da177e4SLinus Torvalds 	 * a single request. This is _much_ faster! */
129ae93a55bSDavid Woodhouse 	rec = (const struct ihex_binrec *)bitstream_fw->data;
1301da177e4SLinus Torvalds 	do {
1311da177e4SLinus Torvalds 		i = 0;
132ae93a55bSDavid Woodhouse 		addr = be32_to_cpu(rec->addr);
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds 		/* intel hex records are terminated with type 0 element */
135ae93a55bSDavid Woodhouse 		while (rec && (i + be16_to_cpu(rec->len) < FW_LOAD_SIZE)) {
136ae93a55bSDavid Woodhouse 			memcpy(buf + i, rec->data, be16_to_cpu(rec->len));
137ae93a55bSDavid Woodhouse 			i += be16_to_cpu(rec->len);
138ae93a55bSDavid Woodhouse 			rec = ihex_next_binrec(rec);
1391da177e4SLinus Torvalds 		}
1401da177e4SLinus Torvalds 		err = emi26_writememory(dev, addr, buf, i, ANCHOR_LOAD_FPGA);
141b412284bSGreg Kroah-Hartman 		if (err < 0)
1421da177e4SLinus Torvalds 			goto wraperr;
143327d74f6SMarcin Slusarz 	} while (rec);
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds 	/* Assert reset (stop the CPU in the EMI) */
1461da177e4SLinus Torvalds 	err = emi26_set_reset(dev,1);
147b412284bSGreg Kroah-Hartman 	if (err < 0)
1481da177e4SLinus Torvalds 		goto wraperr;
1491da177e4SLinus Torvalds 
1501da177e4SLinus Torvalds 	/* 3. We need to put the loader for the firmware into the EZ-USB (again...) */
151ae93a55bSDavid Woodhouse 	for (rec = (const struct ihex_binrec *)loader_fw->data;
152ae93a55bSDavid Woodhouse 	     rec; rec = ihex_next_binrec(rec)) {
153ae93a55bSDavid Woodhouse 		err = emi26_writememory(dev, be32_to_cpu(rec->addr),
154ae93a55bSDavid Woodhouse 					rec->data, be16_to_cpu(rec->len),
155ae93a55bSDavid Woodhouse 					ANCHOR_LOAD_INTERNAL);
156b412284bSGreg Kroah-Hartman 		if (err < 0)
1571da177e4SLinus Torvalds 			goto wraperr;
1581da177e4SLinus Torvalds 	}
15916c23f7dSMonty 	msleep(250);	/* let device settle */
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds 	/* De-assert reset (let the CPU run) */
1621da177e4SLinus Torvalds 	err = emi26_set_reset(dev,0);
163b412284bSGreg Kroah-Hartman 	if (err < 0)
1641da177e4SLinus Torvalds 		goto wraperr;
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds 	/* 4. We put the part of the firmware that lies in the external RAM into the EZ-USB */
167ae93a55bSDavid Woodhouse 
168ae93a55bSDavid Woodhouse 	for (rec = (const struct ihex_binrec *)firmware_fw->data;
169ae93a55bSDavid Woodhouse 	     rec; rec = ihex_next_binrec(rec)) {
170ae93a55bSDavid Woodhouse 		if (!INTERNAL_RAM(be32_to_cpu(rec->addr))) {
171ae93a55bSDavid Woodhouse 			err = emi26_writememory(dev, be32_to_cpu(rec->addr),
172ae93a55bSDavid Woodhouse 						rec->data, be16_to_cpu(rec->len),
173ae93a55bSDavid Woodhouse 						ANCHOR_LOAD_EXTERNAL);
174b412284bSGreg Kroah-Hartman 			if (err < 0)
1751da177e4SLinus Torvalds 				goto wraperr;
1761da177e4SLinus Torvalds 		}
1771da177e4SLinus Torvalds 	}
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds 	/* Assert reset (stop the CPU in the EMI) */
1801da177e4SLinus Torvalds 	err = emi26_set_reset(dev,1);
181b412284bSGreg Kroah-Hartman 	if (err < 0)
1821da177e4SLinus Torvalds 		goto wraperr;
1831da177e4SLinus Torvalds 
184ae93a55bSDavid Woodhouse 	for (rec = (const struct ihex_binrec *)firmware_fw->data;
185ae93a55bSDavid Woodhouse 	     rec; rec = ihex_next_binrec(rec)) {
186ae93a55bSDavid Woodhouse 		if (INTERNAL_RAM(be32_to_cpu(rec->addr))) {
187ae93a55bSDavid Woodhouse 			err = emi26_writememory(dev, be32_to_cpu(rec->addr),
188ae93a55bSDavid Woodhouse 						rec->data, be16_to_cpu(rec->len),
189ae93a55bSDavid Woodhouse 						ANCHOR_LOAD_INTERNAL);
190b412284bSGreg Kroah-Hartman 			if (err < 0)
1911da177e4SLinus Torvalds 				goto wraperr;
1921da177e4SLinus Torvalds 		}
1931da177e4SLinus Torvalds 	}
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds 	/* De-assert reset (let the CPU run) */
1961da177e4SLinus Torvalds 	err = emi26_set_reset(dev,0);
197b412284bSGreg Kroah-Hartman 	if (err < 0)
1981da177e4SLinus Torvalds 		goto wraperr;
19916c23f7dSMonty 	msleep(250);	/* let device settle */
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds 	/* return 1 to fail the driver inialization
2021da177e4SLinus Torvalds 	 * and give real driver change to load */
2031da177e4SLinus Torvalds 	err = 1;
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds wraperr:
206b412284bSGreg Kroah-Hartman 	if (err < 0)
207b412284bSGreg Kroah-Hartman 		dev_err(&dev->dev,"%s - error loading firmware: error = %d\n",
208b412284bSGreg Kroah-Hartman 			__func__, err);
209b412284bSGreg Kroah-Hartman 
210ae93a55bSDavid Woodhouse 	release_firmware(loader_fw);
211ae93a55bSDavid Woodhouse 	release_firmware(bitstream_fw);
212ae93a55bSDavid Woodhouse 	release_firmware(firmware_fw);
213ae93a55bSDavid Woodhouse 
2141da177e4SLinus Torvalds 	kfree(buf);
2151da177e4SLinus Torvalds 	return err;
2161da177e4SLinus Torvalds }
2171da177e4SLinus Torvalds 
21833b9e162SNémeth Márton static const struct usb_device_id id_table[] = {
2191da177e4SLinus Torvalds 	{ USB_DEVICE(EMI26_VENDOR_ID, EMI26_PRODUCT_ID) },
2201da177e4SLinus Torvalds 	{ USB_DEVICE(EMI26_VENDOR_ID, EMI26B_PRODUCT_ID) },
2211da177e4SLinus Torvalds 	{ }                                             /* Terminating entry */
2221da177e4SLinus Torvalds };
2231da177e4SLinus Torvalds 
2241da177e4SLinus Torvalds MODULE_DEVICE_TABLE (usb, id_table);
2251da177e4SLinus Torvalds 
emi26_probe(struct usb_interface * intf,const struct usb_device_id * id)2261da177e4SLinus Torvalds static int emi26_probe(struct usb_interface *intf, const struct usb_device_id *id)
2271da177e4SLinus Torvalds {
2281da177e4SLinus Torvalds 	struct usb_device *dev = interface_to_usbdev(intf);
2291da177e4SLinus Torvalds 
2301b29a375SGreg Kroah-Hartman 	dev_info(&intf->dev, "%s start\n", __func__);
2311da177e4SLinus Torvalds 
2321da177e4SLinus Torvalds 	emi26_load_firmware(dev);
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds 	/* do not return the driver context, let real audio driver do that */
2351da177e4SLinus Torvalds 	return -EIO;
2361da177e4SLinus Torvalds }
2371da177e4SLinus Torvalds 
emi26_disconnect(struct usb_interface * intf)2381da177e4SLinus Torvalds static void emi26_disconnect(struct usb_interface *intf)
2391da177e4SLinus Torvalds {
2401da177e4SLinus Torvalds }
2411da177e4SLinus Torvalds 
2421da177e4SLinus Torvalds static struct usb_driver emi26_driver = {
2431da177e4SLinus Torvalds 	.name		= "emi26 - firmware loader",
2441da177e4SLinus Torvalds 	.probe		= emi26_probe,
2451da177e4SLinus Torvalds 	.disconnect	= emi26_disconnect,
2461da177e4SLinus Torvalds 	.id_table	= id_table,
2471da177e4SLinus Torvalds };
2481da177e4SLinus Torvalds 
24965db4305SGreg Kroah-Hartman module_usb_driver(emi26_driver);
2501da177e4SLinus Torvalds 
25196de0e25SJan Engelhardt MODULE_AUTHOR("Tapio Laxström");
2521da177e4SLinus Torvalds MODULE_DESCRIPTION("Emagic EMI 2|6 firmware loader.");
2531da177e4SLinus Torvalds MODULE_LICENSE("GPL");
2541da177e4SLinus Torvalds 
255ae93a55bSDavid Woodhouse MODULE_FIRMWARE("emi26/loader.fw");
256ae93a55bSDavid Woodhouse MODULE_FIRMWARE("emi26/bitstream.fw");
257ae93a55bSDavid Woodhouse MODULE_FIRMWARE("emi26/firmware.fw");
2581da177e4SLinus Torvalds /* vi:ai:syntax=c:sw=8:ts=8:tw=80
2591da177e4SLinus Torvalds  */
260