blob: d65984dee751728d7cb96cb0e3aa60109429ca73 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Emagic EMI 2|6 usb audio interface firmware loader.
3 * Copyright (C) 2002
Jan Engelhardt96de0e22007-10-19 23:21:04 +02004 * Tapio Laxström (tapio.laxstrom@iptime.fi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
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 * emi26.c,v 1.13 2002/03/08 13:10:26 tapio Exp
11 */
12#include <linux/kernel.h>
13#include <linux/errno.h>
14#include <linux/slab.h>
15#include <linux/module.h>
16#include <linux/init.h>
17#include <linux/usb.h>
Monty16c23f72006-05-09 12:37:22 -070018#include <linux/delay.h>
David Woodhouseae93a552008-05-30 16:19:39 +030019#include <linux/firmware.h>
20#include <linux/ihex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22#define EMI26_VENDOR_ID 0x086a /* Emagic Soft-und Hardware GmBH */
23#define EMI26_PRODUCT_ID 0x0100 /* EMI 2|6 without firmware */
24#define EMI26B_PRODUCT_ID 0x0102 /* EMI 2|6 without firmware */
25
26#define ANCHOR_LOAD_INTERNAL 0xA0 /* Vendor specific request code for Anchor Upload/Download (This one is implemented in the core) */
27#define ANCHOR_LOAD_EXTERNAL 0xA3 /* This command is not implemented in the core. Requires firmware */
28#define ANCHOR_LOAD_FPGA 0xA5 /* This command is not implemented in the core. Requires firmware. Emagic extension */
29#define MAX_INTERNAL_ADDRESS 0x1B3F /* This is the highest internal RAM address for the AN2131Q */
30#define CPUCS_REG 0x7F92 /* EZ-USB Control and Status Register. Bit 0 controls 8051 reset */
31#define INTERNAL_RAM(address) (address <= MAX_INTERNAL_ADDRESS)
32
David Woodhouseae93a552008-05-30 16:19:39 +030033static int emi26_writememory( struct usb_device *dev, int address,
34 const unsigned char *data, int length,
35 __u8 bRequest);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036static int emi26_set_reset(struct usb_device *dev, unsigned char reset_bit);
37static int emi26_load_firmware (struct usb_device *dev);
38static int emi26_probe(struct usb_interface *intf, const struct usb_device_id *id);
39static void emi26_disconnect(struct usb_interface *intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41/* thanks to drivers/usb/serial/keyspan_pda.c code */
David Woodhouseae93a552008-05-30 16:19:39 +030042static int emi26_writememory (struct usb_device *dev, int address,
43 const unsigned char *data, int length,
44 __u8 request)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
46 int result;
Eric Sesterhenn5d7efe52006-10-26 21:06:24 +020047 unsigned char *buffer = kmemdup(data, length, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49 if (!buffer) {
Greg Kroah-Hartmanfd3f1912008-08-14 09:37:34 -070050 dev_err(&dev->dev, "kmalloc(%d) failed.\n", length);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 return -ENOMEM;
52 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 /* Note: usb_control_msg returns negative value on error or length of the
54 * data that was written! */
55 result = usb_control_msg (dev, usb_sndctrlpipe(dev, 0), request, 0x40, address, 0, buffer, length, 300);
56 kfree (buffer);
57 return result;
58}
59
60/* thanks to drivers/usb/serial/keyspan_pda.c code */
61static int emi26_set_reset (struct usb_device *dev, unsigned char reset_bit)
62{
63 int response;
Greg Kroah-Hartman1b29a372008-08-18 13:21:04 -070064 dev_info(&dev->dev, "%s - %d\n", __func__, reset_bit);
Harvey Harrison441b62c2008-03-03 16:08:34 -080065 /* printk(KERN_DEBUG "%s - %d", __func__, reset_bit); */
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 response = emi26_writememory (dev, CPUCS_REG, &reset_bit, 1, 0xa0);
67 if (response < 0) {
Greg Kroah-Hartmanfd3f1912008-08-14 09:37:34 -070068 dev_err(&dev->dev, "set_reset (%d) failed\n", reset_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 }
70 return response;
71}
72
73#define FW_LOAD_SIZE 1023
74
75static int emi26_load_firmware (struct usb_device *dev)
76{
David Woodhouseae93a552008-05-30 16:19:39 +030077 const struct firmware *loader_fw = NULL;
78 const struct firmware *bitstream_fw = NULL;
79 const struct firmware *firmware_fw = NULL;
80 const struct ihex_binrec *rec;
Greg Kroah-Hartmanb4122842012-04-20 16:53:37 -070081 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 __u32 addr; /* Address to write */
84 __u8 *buf;
85
86 buf = kmalloc(FW_LOAD_SIZE, GFP_KERNEL);
Greg Kroah-Hartmanb4122842012-04-20 16:53:37 -070087 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 goto wraperr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
David Woodhouseae93a552008-05-30 16:19:39 +030090 err = request_ihex_firmware(&loader_fw, "emi26/loader.fw", &dev->dev);
91 if (err)
92 goto nofw;
93
94 err = request_ihex_firmware(&bitstream_fw, "emi26/bitstream.fw",
95 &dev->dev);
96 if (err)
97 goto nofw;
98
99 err = request_ihex_firmware(&firmware_fw, "emi26/firmware.fw",
100 &dev->dev);
101 if (err) {
102 nofw:
Greg Kroah-Hartmanfd3f1912008-08-14 09:37:34 -0700103 dev_err(&dev->dev, "%s - request_firmware() failed\n",
104 __func__);
David Woodhouseae93a552008-05-30 16:19:39 +0300105 goto wraperr;
106 }
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 /* Assert reset (stop the CPU in the EMI) */
109 err = emi26_set_reset(dev,1);
Greg Kroah-Hartmanb4122842012-04-20 16:53:37 -0700110 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 goto wraperr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
David Woodhouseae93a552008-05-30 16:19:39 +0300113 rec = (const struct ihex_binrec *)loader_fw->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 /* 1. We need to put the loader for the FPGA into the EZ-USB */
David Woodhouseae93a552008-05-30 16:19:39 +0300115 while (rec) {
116 err = emi26_writememory(dev, be32_to_cpu(rec->addr),
117 rec->data, be16_to_cpu(rec->len),
118 ANCHOR_LOAD_INTERNAL);
Greg Kroah-Hartmanb4122842012-04-20 16:53:37 -0700119 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 goto wraperr;
David Woodhouseae93a552008-05-30 16:19:39 +0300121 rec = ihex_next_binrec(rec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 }
123
124 /* De-assert reset (let the CPU run) */
125 err = emi26_set_reset(dev,0);
Greg Kroah-Hartmanb4122842012-04-20 16:53:37 -0700126 if (err < 0)
Oliver Neukumcf4cf0b2007-10-25 15:38:44 +0200127 goto wraperr;
Monty16c23f72006-05-09 12:37:22 -0700128 msleep(250); /* let device settle */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130 /* 2. We upload the FPGA firmware into the EMI
131 * Note: collect up to 1023 (yes!) bytes and send them with
132 * a single request. This is _much_ faster! */
David Woodhouseae93a552008-05-30 16:19:39 +0300133 rec = (const struct ihex_binrec *)bitstream_fw->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 do {
135 i = 0;
David Woodhouseae93a552008-05-30 16:19:39 +0300136 addr = be32_to_cpu(rec->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138 /* intel hex records are terminated with type 0 element */
David Woodhouseae93a552008-05-30 16:19:39 +0300139 while (rec && (i + be16_to_cpu(rec->len) < FW_LOAD_SIZE)) {
140 memcpy(buf + i, rec->data, be16_to_cpu(rec->len));
141 i += be16_to_cpu(rec->len);
142 rec = ihex_next_binrec(rec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 }
144 err = emi26_writememory(dev, addr, buf, i, ANCHOR_LOAD_FPGA);
Greg Kroah-Hartmanb4122842012-04-20 16:53:37 -0700145 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 goto wraperr;
Marcin Slusarz327d74f2009-01-04 13:25:13 +0100147 } while (rec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149 /* Assert reset (stop the CPU in the EMI) */
150 err = emi26_set_reset(dev,1);
Greg Kroah-Hartmanb4122842012-04-20 16:53:37 -0700151 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 goto wraperr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154 /* 3. We need to put the loader for the firmware into the EZ-USB (again...) */
David Woodhouseae93a552008-05-30 16:19:39 +0300155 for (rec = (const struct ihex_binrec *)loader_fw->data;
156 rec; rec = ihex_next_binrec(rec)) {
157 err = emi26_writememory(dev, be32_to_cpu(rec->addr),
158 rec->data, be16_to_cpu(rec->len),
159 ANCHOR_LOAD_INTERNAL);
Greg Kroah-Hartmanb4122842012-04-20 16:53:37 -0700160 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 goto wraperr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 }
Monty16c23f72006-05-09 12:37:22 -0700163 msleep(250); /* let device settle */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165 /* De-assert reset (let the CPU run) */
166 err = emi26_set_reset(dev,0);
Greg Kroah-Hartmanb4122842012-04-20 16:53:37 -0700167 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 goto wraperr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170 /* 4. We put the part of the firmware that lies in the external RAM into the EZ-USB */
David Woodhouseae93a552008-05-30 16:19:39 +0300171
172 for (rec = (const struct ihex_binrec *)firmware_fw->data;
173 rec; rec = ihex_next_binrec(rec)) {
174 if (!INTERNAL_RAM(be32_to_cpu(rec->addr))) {
175 err = emi26_writememory(dev, be32_to_cpu(rec->addr),
176 rec->data, be16_to_cpu(rec->len),
177 ANCHOR_LOAD_EXTERNAL);
Greg Kroah-Hartmanb4122842012-04-20 16:53:37 -0700178 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 goto wraperr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 }
181 }
Greg Kroah-Hartmanb4122842012-04-20 16:53:37 -0700182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 /* Assert reset (stop the CPU in the EMI) */
184 err = emi26_set_reset(dev,1);
Greg Kroah-Hartmanb4122842012-04-20 16:53:37 -0700185 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 goto wraperr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
David Woodhouseae93a552008-05-30 16:19:39 +0300188 for (rec = (const struct ihex_binrec *)firmware_fw->data;
189 rec; rec = ihex_next_binrec(rec)) {
190 if (INTERNAL_RAM(be32_to_cpu(rec->addr))) {
191 err = emi26_writememory(dev, be32_to_cpu(rec->addr),
192 rec->data, be16_to_cpu(rec->len),
193 ANCHOR_LOAD_INTERNAL);
Greg Kroah-Hartmanb4122842012-04-20 16:53:37 -0700194 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 goto wraperr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 }
197 }
198
199 /* De-assert reset (let the CPU run) */
200 err = emi26_set_reset(dev,0);
Greg Kroah-Hartmanb4122842012-04-20 16:53:37 -0700201 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 goto wraperr;
Monty16c23f72006-05-09 12:37:22 -0700203 msleep(250); /* let device settle */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 /* return 1 to fail the driver inialization
206 * and give real driver change to load */
207 err = 1;
208
209wraperr:
Greg Kroah-Hartmanb4122842012-04-20 16:53:37 -0700210 if (err < 0)
211 dev_err(&dev->dev,"%s - error loading firmware: error = %d\n",
212 __func__, err);
213
David Woodhouseae93a552008-05-30 16:19:39 +0300214 release_firmware(loader_fw);
215 release_firmware(bitstream_fw);
216 release_firmware(firmware_fw);
217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 kfree(buf);
219 return err;
220}
221
Németh Márton33b9e162010-01-10 15:34:45 +0100222static const struct usb_device_id id_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 { USB_DEVICE(EMI26_VENDOR_ID, EMI26_PRODUCT_ID) },
224 { USB_DEVICE(EMI26_VENDOR_ID, EMI26B_PRODUCT_ID) },
225 { } /* Terminating entry */
226};
227
228MODULE_DEVICE_TABLE (usb, id_table);
229
230static int emi26_probe(struct usb_interface *intf, const struct usb_device_id *id)
231{
232 struct usb_device *dev = interface_to_usbdev(intf);
233
Greg Kroah-Hartman1b29a372008-08-18 13:21:04 -0700234 dev_info(&intf->dev, "%s start\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 emi26_load_firmware(dev);
237
238 /* do not return the driver context, let real audio driver do that */
239 return -EIO;
240}
241
242static void emi26_disconnect(struct usb_interface *intf)
243{
244}
245
246static struct usb_driver emi26_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 .name = "emi26 - firmware loader",
248 .probe = emi26_probe,
249 .disconnect = emi26_disconnect,
250 .id_table = id_table,
251};
252
Greg Kroah-Hartman65db4302011-11-18 09:34:02 -0800253module_usb_driver(emi26_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Jan Engelhardt96de0e22007-10-19 23:21:04 +0200255MODULE_AUTHOR("Tapio Laxström");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256MODULE_DESCRIPTION("Emagic EMI 2|6 firmware loader.");
257MODULE_LICENSE("GPL");
258
David Woodhouseae93a552008-05-30 16:19:39 +0300259MODULE_FIRMWARE("emi26/loader.fw");
260MODULE_FIRMWARE("emi26/bitstream.fw");
261MODULE_FIRMWARE("emi26/firmware.fw");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262/* vi:ai:syntax=c:sw=8:ts=8:tw=80
263 */