1
2
3
4
5
6
7
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
19#define EMI26_PRODUCT_ID 0x0100
20#define EMI26B_PRODUCT_ID 0x0102
21
22#define ANCHOR_LOAD_INTERNAL 0xA0
23#define ANCHOR_LOAD_EXTERNAL 0xA3
24#define ANCHOR_LOAD_FPGA 0xA5
25#define MAX_INTERNAL_ADDRESS 0x1B3F
26#define CPUCS_REG 0x7F92
27#define INTERNAL_RAM(address) (address <= MAX_INTERNAL_ADDRESS)
28
29static int emi26_writememory( struct usb_device *dev, int address,
30 const unsigned char *data, int length,
31 __u8 bRequest);
32static int emi26_set_reset(struct usb_device *dev, unsigned char reset_bit);
33static int emi26_load_firmware (struct usb_device *dev);
34static int emi26_probe(struct usb_interface *intf, const struct usb_device_id *id);
35static void emi26_disconnect(struct usb_interface *intf);
36
37
38static 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
50
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
57static 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
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
71static 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;
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
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
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
121 err = emi26_set_reset(dev,0);
122 if (err < 0)
123 goto wraperr;
124 msleep(250);
125
126
127
128
129 rec = (const struct ihex_binrec *)bitstream_fw->data;
130 do {
131 i = 0;
132 addr = be32_to_cpu(rec->addr);
133
134
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
146 err = emi26_set_reset(dev,1);
147 if (err < 0)
148 goto wraperr;
149
150
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);
160
161
162 err = emi26_set_reset(dev,0);
163 if (err < 0)
164 goto wraperr;
165
166
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
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
196 err = emi26_set_reset(dev,0);
197 if (err < 0)
198 goto wraperr;
199 msleep(250);
200
201
202
203 err = 1;
204
205wraperr:
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
218static 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 { }
222};
223
224MODULE_DEVICE_TABLE (usb, id_table);
225
226static 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
235 return -EIO;
236}
237
238static void emi26_disconnect(struct usb_interface *intf)
239{
240}
241
242static 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
249module_usb_driver(emi26_driver);
250
251MODULE_AUTHOR("Tapio Laxström");
252MODULE_DESCRIPTION("Emagic EMI 2|6 firmware loader.");
253MODULE_LICENSE("GPL");
254
255MODULE_FIRMWARE("emi26/loader.fw");
256MODULE_FIRMWARE("emi26/bitstream.fw");
257MODULE_FIRMWARE("emi26/firmware.fw");
258
259
260