1616#include <linux/module.h>
1717#include <linux/usb.h>
1818#include <linux/delay.h>
19-
20- #define MAX_INTEL_HEX_RECORD_LENGTH 16
21- typedef struct _INTEL_HEX_RECORD
22- {
23- __u32 length ;
24- __u32 address ;
25- __u32 type ;
26- __u8 data [MAX_INTEL_HEX_RECORD_LENGTH ];
27- } INTEL_HEX_RECORD , * PINTEL_HEX_RECORD ;
19+ #include <linux/firmware.h>
20+ #include <linux/ihex.h>
2821
2922/* include firmware (variables)*/
3023
@@ -33,9 +26,9 @@ typedef struct _INTEL_HEX_RECORD
3326//#undef SPDIF /* if you want MIDI uncomment this line */
3427
3528#ifdef SPDIF
36- # include "emi62_fw_s.h" /* spdif fw */
29+ #define FIRMWARE_FW "emi62/ spdif.fw"
3730#else
38- # include "emi62_fw_m.h" /* midi fw */
31+ #define FIRMWARE_FW "emi62/ midi.fw"
3932#endif
4033
4134#define EMI62_VENDOR_ID 0x086a /* Emagic Soft-und Hardware GmBH */
@@ -48,7 +41,9 @@ typedef struct _INTEL_HEX_RECORD
4841#define CPUCS_REG 0x7F92 /* EZ-USB Control and Status Register. Bit 0 controls 8051 reset */
4942#define INTERNAL_RAM (address ) (address <= MAX_INTERNAL_ADDRESS)
5043
51- static int emi62_writememory ( struct usb_device * dev , int address , unsigned char * data , int length , __u8 bRequest );
44+ static int emi62_writememory (struct usb_device * dev , int address ,
45+ const unsigned char * data , int length ,
46+ __u8 bRequest );
5247static int emi62_set_reset (struct usb_device * dev , unsigned char reset_bit );
5348static int emi62_load_firmware (struct usb_device * dev );
5449static int emi62_probe (struct usb_interface * intf , const struct usb_device_id * id );
@@ -58,7 +53,9 @@ static void __exit emi62_exit (void);
5853
5954
6055/* thanks to drivers/usb/serial/keyspan_pda.c code */
61- static int emi62_writememory (struct usb_device * dev , int address , unsigned char * data , int length , __u8 request )
56+ static int emi62_writememory (struct usb_device * dev , int address ,
57+ const unsigned char * data , int length ,
58+ __u8 request )
6259{
6360 int result ;
6461 unsigned char * buffer = kmemdup (data , length , GFP_KERNEL );
@@ -91,9 +88,12 @@ static int emi62_set_reset (struct usb_device *dev, unsigned char reset_bit)
9188
9289static int emi62_load_firmware (struct usb_device * dev )
9390{
91+ const struct firmware * loader_fw = NULL ;
92+ const struct firmware * bitstream_fw = NULL ;
93+ const struct firmware * firmware_fw = NULL ;
94+ const struct ihex_binrec * rec ;
9495 int err ;
9596 int i ;
96- int pos = 0 ; /* Position in hex record */
9797 __u32 addr ; /* Address to write */
9898 __u8 * buf ;
9999
@@ -105,20 +105,41 @@ static int emi62_load_firmware (struct usb_device *dev)
105105 goto wraperr ;
106106 }
107107
108+ err = request_ihex_firmware (& loader_fw , "emi62/loader.fw" , & dev -> dev );
109+ if (err )
110+ goto nofw ;
111+
112+ err = request_ihex_firmware (& bitstream_fw , "emi62/bitstream.fw" ,
113+ & dev -> dev );
114+ if (err )
115+ goto nofw ;
116+
117+ err = request_ihex_firmware (& firmware_fw , FIRMWARE_FW , & dev -> dev );
118+ if (err ) {
119+ nofw :
120+ err ( "%s - request_firmware() failed" , __func__ );
121+ goto wraperr ;
122+ }
123+
108124 /* Assert reset (stop the CPU in the EMI) */
109125 err = emi62_set_reset (dev ,1 );
110126 if (err < 0 ) {
111127 err ("%s - error loading firmware: error = %d" , __func__ , err );
112128 goto wraperr ;
113129 }
114130
131+ rec = (const struct ihex_binrec * )loader_fw -> data ;
132+
115133 /* 1. We need to put the loader for the FPGA into the EZ-USB */
116- for (i = 0 ; g_emi62_loader [i ].type == 0 ; i ++ ) {
117- err = emi62_writememory (dev , g_emi62_loader [i ].address , g_emi62_loader [i ].data , g_emi62_loader [i ].length , ANCHOR_LOAD_INTERNAL );
134+ while (rec ) {
135+ err = emi62_writememory (dev , be32_to_cpu (rec -> addr ),
136+ rec -> data , be16_to_cpu (rec -> len ),
137+ ANCHOR_LOAD_INTERNAL );
118138 if (err < 0 ) {
119139 err ("%s - error loading firmware: error = %d" , __func__ , err );
120140 goto wraperr ;
121141 }
142+ rec = ihex_next_binrec (rec );
122143 }
123144
124145 /* De-assert reset (let the CPU run) */
@@ -132,15 +153,16 @@ static int emi62_load_firmware (struct usb_device *dev)
132153 /* 2. We upload the FPGA firmware into the EMI
133154 * Note: collect up to 1023 (yes!) bytes and send them with
134155 * a single request. This is _much_ faster! */
156+ rec = (const struct ihex_binrec * )bitstream_fw -> data ;
135157 do {
136158 i = 0 ;
137- addr = g_emi62bs [ pos ]. address ;
159+ addr = be32_to_cpu ( rec -> addr ) ;
138160
139161 /* intel hex records are terminated with type 0 element */
140- while (( g_emi62bs [ pos ]. type == 0 ) && (i + g_emi62bs [ pos ]. length < FW_LOAD_SIZE )) {
141- memcpy (buf + i , g_emi62bs [ pos ]. data , g_emi62bs [ pos ]. length );
142- i += g_emi62bs [ pos ]. length ;
143- pos ++ ;
162+ while (rec && (i + be16_to_cpu ( rec -> len ) < FW_LOAD_SIZE )) {
163+ memcpy (buf + i , rec -> data , be16_to_cpu ( rec -> len ) );
164+ i += be16_to_cpu ( rec -> len ) ;
165+ rec = ihex_next_binrec ( rec ) ;
144166 }
145167 err = emi62_writememory (dev , addr , buf , i , ANCHOR_LOAD_FPGA );
146168 if (err < 0 ) {
@@ -157,8 +179,11 @@ static int emi62_load_firmware (struct usb_device *dev)
157179 }
158180
159181 /* 3. We need to put the loader for the firmware into the EZ-USB (again...) */
160- for (i = 0 ; g_emi62_loader [i ].type == 0 ; i ++ ) {
161- err = emi62_writememory (dev , g_emi62_loader [i ].address , g_emi62_loader [i ].data , g_emi62_loader [i ].length , ANCHOR_LOAD_INTERNAL );
182+ for (rec = (const struct ihex_binrec * )loader_fw -> data ;
183+ rec ; rec = ihex_next_binrec (rec )) {
184+ err = emi62_writememory (dev , be32_to_cpu (rec -> addr ),
185+ rec -> data , be16_to_cpu (rec -> len ),
186+ ANCHOR_LOAD_INTERNAL );
162187 if (err < 0 ) {
163188 err ("%s - error loading firmware: error = %d" , __func__ , err );
164189 goto wraperr ;
@@ -175,59 +200,39 @@ static int emi62_load_firmware (struct usb_device *dev)
175200
176201 /* 4. We put the part of the firmware that lies in the external RAM into the EZ-USB */
177202
178- /* FIXME: quick and dirty ifdefs */
179- #ifdef SPDIF
180- for (i = 0 ; g_HexSpdifFw62 [i ].type == 0 ; i ++ ) {
181- if (!INTERNAL_RAM (g_HexSpdifFw62 [i ].address )) {
182- err = emi62_writememory (dev , g_HexSpdifFw62 [i ].address , g_HexSpdifFw62 [i ].data , g_HexSpdifFw62 [i ].length , ANCHOR_LOAD_EXTERNAL );
203+ for (rec = (const struct ihex_binrec * )firmware_fw -> data ;
204+ rec ; rec = ihex_next_binrec (rec )) {
205+ if (!INTERNAL_RAM (be32_to_cpu (rec -> addr ))) {
206+ err = emi62_writememory (dev , be32_to_cpu (rec -> addr ),
207+ rec -> data , be16_to_cpu (rec -> len ),
208+ ANCHOR_LOAD_EXTERNAL );
183209 if (err < 0 ) {
184210 err ("%s - error loading firmware: error = %d" , __func__ , err );
185211 goto wraperr ;
186212 }
187213 }
188214 }
189- #else /* MIDI */
190- for (i = 0 ; g_HexMidiFw62 [i ].type == 0 ; i ++ ) {
191- if (!INTERNAL_RAM (g_HexMidiFw62 [i ].address )) {
192- err = emi62_writememory (dev , g_HexMidiFw62 [i ].address , g_HexMidiFw62 [i ].data , g_HexMidiFw62 [i ].length , ANCHOR_LOAD_EXTERNAL );
193- if (err < 0 ) {
194- err ("%s - error loading firmware: error = %d\n" , __func__ , err );
195- goto wraperr ;
196- return err ;
197- }
198- }
199- }
200- #endif
215+
201216 /* Assert reset (stop the CPU in the EMI) */
202217 err = emi62_set_reset (dev ,1 );
203218 if (err < 0 ) {
204219 err ("%s - error loading firmware: error = %d" , __func__ , err );
205220 goto wraperr ;
206221 }
207222
208- /* FIXME: quick and dirty ifdefs */
209- #ifdef SPDIF
210- for (i = 0 ; g_HexSpdifFw62 [i ].type == 0 ; i ++ ) {
211- if (INTERNAL_RAM (g_HexSpdifFw62 [i ].address )) {
212- err = emi62_writememory (dev , g_HexSpdifFw62 [i ].address , g_HexSpdifFw62 [i ].data , g_HexSpdifFw62 [i ].length , ANCHOR_LOAD_INTERNAL );
223+ for (rec = (const struct ihex_binrec * )firmware_fw -> data ;
224+ rec ; rec = ihex_next_binrec (rec )) {
225+ if (INTERNAL_RAM (be32_to_cpu (rec -> addr ))) {
226+ err = emi62_writememory (dev , be32_to_cpu (rec -> addr ),
227+ rec -> data , be16_to_cpu (rec -> len ),
228+ ANCHOR_LOAD_EXTERNAL );
213229 if (err < 0 ) {
214230 err ("%s - error loading firmware: error = %d" , __func__ , err );
215231 goto wraperr ;
216232 }
217233 }
218234 }
219- #else /* MIDI */
220- for (i = 0 ; g_HexMidiFw62 [i ].type == 0 ; i ++ ) {
221- if (INTERNAL_RAM (g_HexMidiFw62 [i ].address )) {
222- err = emi62_writememory (dev , g_HexMidiFw62 [i ].address , g_HexMidiFw62 [i ].data , g_HexMidiFw62 [i ].length , ANCHOR_LOAD_INTERNAL );
223- if (err < 0 ) {
224- err ("%s - error loading firmware: error = %d\n" , __func__ , err );
225- goto wraperr ;
226- }
227- }
228- }
229- #endif
230-
235+
231236 /* De-assert reset (let the CPU run) */
232237 err = emi62_set_reset (dev ,0 );
233238 if (err < 0 ) {
@@ -236,13 +241,21 @@ static int emi62_load_firmware (struct usb_device *dev)
236241 }
237242 msleep (250 ); /* let device settle */
238243
244+ release_firmware (loader_fw );
245+ release_firmware (bitstream_fw );
246+ release_firmware (firmware_fw );
247+
239248 kfree (buf );
240249
241250 /* return 1 to fail the driver inialization
242251 * and give real driver change to load */
243252 return 1 ;
244253
245254wraperr :
255+ release_firmware (loader_fw );
256+ release_firmware (bitstream_fw );
257+ release_firmware (firmware_fw );
258+
246259 kfree (buf );
247260 dev_err (& dev -> dev , "Error\n" );
248261 return err ;
@@ -300,5 +313,8 @@ MODULE_AUTHOR("Tapio Laxström");
300313MODULE_DESCRIPTION ("Emagic EMI 6|2m firmware loader." );
301314MODULE_LICENSE ("GPL" );
302315
316+ MODULE_FIRMWARE ("emi62/loader.fw" );
317+ MODULE_FIRMWARE ("emi62/bitstream.fw" );
318+ MODULE_FIRMWARE (FIRMWARE_FW );
303319/* vi:ai:syntax=c:sw=8:ts=8:tw=80
304320 */
0 commit comments