Linux Audio

Check our new training course

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