1616#include <linux/init.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 ;
28-
29- /* include firmware (variables) */
30- #include "emi26_fw.h"
19+ #include <linux/firmware.h>
20+ #include <linux/ihex.h>
3121
3222#define EMI26_VENDOR_ID 0x086a /* Emagic Soft-und Hardware GmBH */
3323#define EMI26_PRODUCT_ID 0x0100 /* EMI 2|6 without firmware */
@@ -40,7 +30,9 @@ typedef struct _INTEL_HEX_RECORD
4030#define CPUCS_REG 0x7F92 /* EZ-USB Control and Status Register. Bit 0 controls 8051 reset */
4131#define INTERNAL_RAM (address ) (address <= MAX_INTERNAL_ADDRESS)
4232
43- static int emi26_writememory ( struct usb_device * dev , int address , unsigned char * data , int length , __u8 bRequest );
33+ static int emi26_writememory ( struct usb_device * dev , int address ,
34+ const unsigned char * data , int length ,
35+ __u8 bRequest );
4436static int emi26_set_reset (struct usb_device * dev , unsigned char reset_bit );
4537static int emi26_load_firmware (struct usb_device * dev );
4638static int emi26_probe (struct usb_interface * intf , const struct usb_device_id * id );
@@ -50,7 +42,9 @@ static void __exit emi26_exit (void);
5042
5143
5244/* thanks to drivers/usb/serial/keyspan_pda.c code */
53- static int emi26_writememory (struct usb_device * dev , int address , unsigned char * data , int length , __u8 request )
45+ static int emi26_writememory (struct usb_device * dev , int address ,
46+ const unsigned char * data , int length ,
47+ __u8 request )
5448{
5549 int result ;
5650 unsigned char * buffer = kmemdup (data , length , GFP_KERNEL );
@@ -83,9 +77,12 @@ static int emi26_set_reset (struct usb_device *dev, unsigned char reset_bit)
8377
8478static int emi26_load_firmware (struct usb_device * dev )
8579{
80+ const struct firmware * loader_fw = NULL ;
81+ const struct firmware * bitstream_fw = NULL ;
82+ const struct firmware * firmware_fw = NULL ;
83+ const struct ihex_binrec * rec ;
8684 int err ;
8785 int i ;
88- int pos = 0 ; /* Position in hex record */
8986 __u32 addr ; /* Address to write */
9087 __u8 * buf ;
9188
@@ -96,20 +93,41 @@ static int emi26_load_firmware (struct usb_device *dev)
9693 goto wraperr ;
9794 }
9895
96+ err = request_ihex_firmware (& loader_fw , "emi26/loader.fw" , & dev -> dev );
97+ if (err )
98+ goto nofw ;
99+
100+ err = request_ihex_firmware (& bitstream_fw , "emi26/bitstream.fw" ,
101+ & dev -> dev );
102+ if (err )
103+ goto nofw ;
104+
105+ err = request_ihex_firmware (& firmware_fw , "emi26/firmware.fw" ,
106+ & dev -> dev );
107+ if (err ) {
108+ nofw :
109+ err ( "%s - request_firmware() failed" , __func__ );
110+ goto wraperr ;
111+ }
112+
99113 /* Assert reset (stop the CPU in the EMI) */
100114 err = emi26_set_reset (dev ,1 );
101115 if (err < 0 ) {
102116 err ( "%s - error loading firmware: error = %d" , __func__ , err );
103117 goto wraperr ;
104118 }
105119
120+ rec = (const struct ihex_binrec * )loader_fw -> data ;
106121 /* 1. We need to put the loader for the FPGA into the EZ-USB */
107- for (i = 0 ; g_Loader [i ].type == 0 ; i ++ ) {
108- err = emi26_writememory (dev , g_Loader [i ].address , g_Loader [i ].data , g_Loader [i ].length , ANCHOR_LOAD_INTERNAL );
122+ while (rec ) {
123+ err = emi26_writememory (dev , be32_to_cpu (rec -> addr ),
124+ rec -> data , be16_to_cpu (rec -> len ),
125+ ANCHOR_LOAD_INTERNAL );
109126 if (err < 0 ) {
110127 err ("%s - error loading firmware: error = %d" , __func__ , err );
111128 goto wraperr ;
112129 }
130+ rec = ihex_next_binrec (rec );
113131 }
114132
115133 /* De-assert reset (let the CPU run) */
@@ -123,15 +141,16 @@ static int emi26_load_firmware (struct usb_device *dev)
123141 /* 2. We upload the FPGA firmware into the EMI
124142 * Note: collect up to 1023 (yes!) bytes and send them with
125143 * a single request. This is _much_ faster! */
144+ rec = (const struct ihex_binrec * )bitstream_fw -> data ;
126145 do {
127146 i = 0 ;
128- addr = g_bitstream [ pos ]. address ;
147+ addr = be32_to_cpu ( rec -> addr ) ;
129148
130149 /* intel hex records are terminated with type 0 element */
131- while (( g_bitstream [ pos ]. type == 0 ) && (i + g_bitstream [ pos ]. length < FW_LOAD_SIZE )) {
132- memcpy (buf + i , g_bitstream [ pos ]. data , g_bitstream [ pos ]. length );
133- i += g_bitstream [ pos ]. length ;
134- pos ++ ;
150+ while (rec && (i + be16_to_cpu ( rec -> len ) < FW_LOAD_SIZE )) {
151+ memcpy (buf + i , rec -> data , be16_to_cpu ( rec -> len ) );
152+ i += be16_to_cpu ( rec -> len ) ;
153+ rec = ihex_next_binrec ( rec ) ;
135154 }
136155 err = emi26_writememory (dev , addr , buf , i , ANCHOR_LOAD_FPGA );
137156 if (err < 0 ) {
@@ -148,8 +167,11 @@ static int emi26_load_firmware (struct usb_device *dev)
148167 }
149168
150169 /* 3. We need to put the loader for the firmware into the EZ-USB (again...) */
151- for (i = 0 ; g_Loader [i ].type == 0 ; i ++ ) {
152- err = emi26_writememory (dev , g_Loader [i ].address , g_Loader [i ].data , g_Loader [i ].length , ANCHOR_LOAD_INTERNAL );
170+ for (rec = (const struct ihex_binrec * )loader_fw -> data ;
171+ rec ; rec = ihex_next_binrec (rec )) {
172+ err = emi26_writememory (dev , be32_to_cpu (rec -> addr ),
173+ rec -> data , be16_to_cpu (rec -> len ),
174+ ANCHOR_LOAD_INTERNAL );
153175 if (err < 0 ) {
154176 err ("%s - error loading firmware: error = %d" , __func__ , err );
155177 goto wraperr ;
@@ -165,9 +187,13 @@ static int emi26_load_firmware (struct usb_device *dev)
165187 }
166188
167189 /* 4. We put the part of the firmware that lies in the external RAM into the EZ-USB */
168- for (i = 0 ; g_Firmware [i ].type == 0 ; i ++ ) {
169- if (!INTERNAL_RAM (g_Firmware [i ].address )) {
170- err = emi26_writememory (dev , g_Firmware [i ].address , g_Firmware [i ].data , g_Firmware [i ].length , ANCHOR_LOAD_EXTERNAL );
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 = emi26_writememory (dev , be32_to_cpu (rec -> addr ),
195+ rec -> data , be16_to_cpu (rec -> len ),
196+ ANCHOR_LOAD_EXTERNAL );
171197 if (err < 0 ) {
172198 err ("%s - error loading firmware: error = %d" , __func__ , err );
173199 goto wraperr ;
@@ -182,9 +208,12 @@ static int emi26_load_firmware (struct usb_device *dev)
182208 goto wraperr ;
183209 }
184210
185- for (i = 0 ; g_Firmware [i ].type == 0 ; i ++ ) {
186- if (INTERNAL_RAM (g_Firmware [i ].address )) {
187- err = emi26_writememory (dev , g_Firmware [i ].address , g_Firmware [i ].data , g_Firmware [i ].length , ANCHOR_LOAD_INTERNAL );
211+ for (rec = (const struct ihex_binrec * )firmware_fw -> data ;
212+ rec ; rec = ihex_next_binrec (rec )) {
213+ if (INTERNAL_RAM (be32_to_cpu (rec -> addr ))) {
214+ err = emi26_writememory (dev , be32_to_cpu (rec -> addr ),
215+ rec -> data , be16_to_cpu (rec -> len ),
216+ ANCHOR_LOAD_INTERNAL );
188217 if (err < 0 ) {
189218 err ("%s - error loading firmware: error = %d" , __func__ , err );
190219 goto wraperr ;
@@ -205,6 +234,10 @@ static int emi26_load_firmware (struct usb_device *dev)
205234 err = 1 ;
206235
207236wraperr :
237+ release_firmware (loader_fw );
238+ release_firmware (bitstream_fw );
239+ release_firmware (firmware_fw );
240+
208241 kfree (buf );
209242 return err ;
210243}
@@ -257,5 +290,8 @@ MODULE_AUTHOR("Tapio Laxström");
257290MODULE_DESCRIPTION ("Emagic EMI 2|6 firmware loader." );
258291MODULE_LICENSE ("GPL" );
259292
293+ MODULE_FIRMWARE ("emi26/loader.fw" );
294+ MODULE_FIRMWARE ("emi26/bitstream.fw" );
295+ MODULE_FIRMWARE ("emi26/firmware.fw" );
260296/* vi:ai:syntax=c:sw=8:ts=8:tw=80
261297 */
0 commit comments