1
2
3
4
5
6
7
8
9
10
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>
18#include <linux/delay.h>
19#include <linux/firmware.h>
20#include <linux/ihex.h>
21
22#define EMI26_VENDOR_ID 0x086a
23#define EMI26_PRODUCT_ID 0x0100
24#define EMI26B_PRODUCT_ID 0x0102
25
26#define ANCHOR_LOAD_INTERNAL 0xA0
27#define ANCHOR_LOAD_EXTERNAL 0xA3
28#define ANCHOR_LOAD_FPGA 0xA5
29#define MAX_INTERNAL_ADDRESS 0x1B3F
30#define CPUCS_REG 0x7F92
31#define INTERNAL_RAM(address) (address <= MAX_INTERNAL_ADDRESS)
32
33static int emi26_writememory( struct usb_device *dev, int address,
34 const unsigned char *data, int length,
35 __u8 bRequest);
36static 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);
40
41
42static int emi26_writememory (struct usb_device *dev, int address,
43 const unsigned char *data, int length,
44 __u8 request)
45{
46 int result;
47 unsigned char *buffer = kmemdup(data, length, GFP_KERNEL);
48
49 if (!buffer) {
50 dev_err(&dev->dev, "kmalloc(%d) failed.\n", length);
51 return -ENOMEM;
52 }
53
54
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
61static int emi26_set_reset (struct usb_device *dev, unsigned char reset_bit)
62{
63 int response;
64 dev_info(&dev->dev, "%s - %d\n", __func__, reset_bit);
65
66 response = emi26_writememory (dev, CPUCS_REG, &reset_bit, 1, 0xa0);
67 if (response < 0) {
68 dev_err(&dev->dev, "set_reset (%d) failed\n", reset_bit);
69 }
70 return response;
71}
72
73#define FW_LOAD_SIZE 1023
74
75static int emi26_load_firmware (struct usb_device *dev)
76{
77 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;
81 int err = -ENOMEM;
82 int i;
83 __u32 addr;
84 __u8 *buf;
85
86 buf = kmalloc(FW_LOAD_SIZE, GFP_KERNEL);
87 if (!buf)
88 goto wraperr;
89
90 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:
103 dev_err(&dev->dev, "%s - request_firmware() failed\n",
104 __func__);
105 goto wraperr;
106 }
107
108
109 err = emi26_set_reset(dev,1);
110 if (err < 0)
111 goto wraperr;
112
113 rec = (const struct ihex_binrec *)loader_fw->data;
114
115 while (rec) {
116 err = emi26_writememory(dev, be32_to_cpu(rec->addr),
117 rec->data, be16_to_cpu(rec->len),
118 ANCHOR_LOAD_INTERNAL);
119 if (err < 0)
120 goto wraperr;
121 rec = ihex_next_binrec(rec);
122 }
123
124
125 err = emi26_set_reset(dev,0);
126 if (err < 0)
127 goto wraperr;
128 msleep(250);
129
130
131
132
133 rec = (const struct ihex_binrec *)bitstream_fw->data;
134 do {
135 i = 0;
136 addr = be32_to_cpu(rec->addr);
137
138
139 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);
143 }
144 err = emi26_writememory(dev, addr, buf, i, ANCHOR_LOAD_FPGA);
145 if (err < 0)
146 goto wraperr;
147 } while (rec);
148
149
150 err = emi26_set_reset(dev,1);
151 if (err < 0)
152 goto wraperr;
153
154
155 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);
160 if (err < 0)
161 goto wraperr;
162 }
163 msleep(250);
164
165
166 err = emi26_set_reset(dev,0);
167 if (err < 0)
168 goto wraperr;
169
170
171
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);
178 if (err < 0)
179 goto wraperr;
180 }
181 }
182
183
184 err = emi26_set_reset(dev,1);
185 if (err < 0)
186 goto wraperr;
187
188 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);
194 if (err < 0)
195 goto wraperr;
196 }
197 }
198
199
200 err = emi26_set_reset(dev,0);
201 if (err < 0)
202 goto wraperr;
203 msleep(250);
204
205
206
207 err = 1;
208
209wraperr:
210 if (err < 0)
211 dev_err(&dev->dev,"%s - error loading firmware: error = %d\n",
212 __func__, err);
213
214 release_firmware(loader_fw);
215 release_firmware(bitstream_fw);
216 release_firmware(firmware_fw);
217
218 kfree(buf);
219 return err;
220}
221
222static const struct usb_device_id id_table[] = {
223 { USB_DEVICE(EMI26_VENDOR_ID, EMI26_PRODUCT_ID) },
224 { USB_DEVICE(EMI26_VENDOR_ID, EMI26B_PRODUCT_ID) },
225 { }
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
234 dev_info(&intf->dev, "%s start\n", __func__);
235
236 emi26_load_firmware(dev);
237
238
239 return -EIO;
240}
241
242static void emi26_disconnect(struct usb_interface *intf)
243{
244}
245
246static struct usb_driver emi26_driver = {
247 .name = "emi26 - firmware loader",
248 .probe = emi26_probe,
249 .disconnect = emi26_disconnect,
250 .id_table = id_table,
251};
252
253module_usb_driver(emi26_driver);
254
255MODULE_AUTHOR("Tapio Laxström");
256MODULE_DESCRIPTION("Emagic EMI 2|6 firmware loader.");
257MODULE_LICENSE("GPL");
258
259MODULE_FIRMWARE("emi26/loader.fw");
260MODULE_FIRMWARE("emi26/bitstream.fw");
261MODULE_FIRMWARE("emi26/firmware.fw");
262
263
264