Skip to content

Commit cbf2848

Browse files
zeldinzougloub
authored andcommitted
ezusb: Add support for uploading firmware to FX3
1 parent dbd6b40 commit cbf2848

File tree

9 files changed

+71
-27
lines changed

9 files changed

+71
-27
lines changed

src/ezusb.c

Lines changed: 61 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ SR_PRIV int ezusb_reset(struct libusb_device_handle *hdl, int set_clear)
5454

5555
SR_PRIV int ezusb_install_firmware(struct sr_context *ctx,
5656
libusb_device_handle *hdl,
57-
const char *name)
57+
const char *name, gboolean fx3)
5858
{
5959
unsigned char *firmware;
6060
size_t length, offset, chunksize;
@@ -64,39 +64,82 @@ SR_PRIV int ezusb_install_firmware(struct sr_context *ctx,
6464
* which holds the firmware offset, is only 16 bit wide.
6565
*/
6666
firmware = sr_resource_load(ctx, SR_RESOURCE_FIRMWARE,
67-
name, &length, 1 << 16);
67+
name, &length, (fx3? 536 << 10 : 1 << 16));
6868
if (!firmware)
6969
return SR_ERR;
7070

7171
sr_info("Uploading firmware '%s'.", name);
7272

7373
result = SR_OK;
7474
offset = 0;
75-
while (offset < length) {
76-
chunksize = MIN(length - offset, FW_CHUNKSIZE);
77-
78-
ret = libusb_control_transfer(hdl, LIBUSB_REQUEST_TYPE_VENDOR |
79-
LIBUSB_ENDPOINT_OUT, 0xa0, offset,
80-
0x0000, firmware + offset,
81-
chunksize, 100);
82-
if (ret < 0) {
83-
sr_err("Unable to send firmware to device: %s.",
84-
libusb_error_name(ret));
75+
if (fx3) {
76+
if (length < 4 ||
77+
firmware[0] != 'C' || firmware[1] != 'Y' ||
78+
firmware[3] != 0xb0) {
79+
sr_err("Invalid signature on firmware");
8580
g_free(firmware);
8681
return SR_ERR;
8782
}
88-
sr_info("Uploaded %zu bytes.", chunksize);
89-
offset += chunksize;
83+
offset = 4;
84+
}
85+
while (offset < length) {
86+
size_t addr, sublength, suboffset;
87+
88+
if (fx3) {
89+
if (offset + 4 == length) {
90+
/* Skip checksum */
91+
offset += 4;
92+
break;
93+
}
94+
if (length < offset + 8) {
95+
break;
96+
}
97+
sublength = RL32(firmware + offset) << 2;
98+
offset += 4;
99+
addr = RL32(firmware + offset);
100+
offset += 4;
101+
if (sublength > length - offset) {
102+
break;
103+
}
104+
} else {
105+
sublength = length - offset;
106+
addr = 0;
107+
}
108+
suboffset = 0;
109+
110+
do {
111+
chunksize = MIN(sublength - suboffset, FW_CHUNKSIZE);
112+
113+
ret = libusb_control_transfer(hdl, LIBUSB_REQUEST_TYPE_VENDOR |
114+
LIBUSB_ENDPOINT_OUT, 0xa0, (addr + suboffset) & 0xffff,
115+
(addr + suboffset) >> 16, firmware + offset + suboffset,
116+
chunksize, 100);
117+
if (ret < 0) {
118+
sr_err("Unable to send firmware to device: %s.",
119+
libusb_error_name(ret));
120+
g_free(firmware);
121+
return SR_ERR;
122+
}
123+
sr_info("Uploaded %zu bytes.", chunksize);
124+
suboffset += chunksize;
125+
} while (suboffset < sublength);
126+
127+
offset += sublength;
90128
}
91129
g_free(firmware);
92130

131+
if (offset < length) {
132+
sr_err("Firmware file is truncated.");
133+
return SR_ERR;
134+
}
135+
93136
sr_info("Firmware upload done.");
94137

95138
return result;
96139
}
97140

98141
SR_PRIV int ezusb_upload_firmware(struct sr_context *ctx, libusb_device *dev,
99-
int configuration, const char *name)
142+
int configuration, const char *name, gboolean fx3)
100143
{
101144
struct libusb_device_handle *hdl;
102145
int ret;
@@ -129,13 +172,13 @@ SR_PRIV int ezusb_upload_firmware(struct sr_context *ctx, libusb_device *dev,
129172
return SR_ERR;
130173
}
131174

132-
if ((ezusb_reset(hdl, 1)) < 0)
175+
if (!fx3 && (ezusb_reset(hdl, 1)) < 0)
133176
return SR_ERR;
134177

135-
if (ezusb_install_firmware(ctx, hdl, name) < 0)
178+
if (ezusb_install_firmware(ctx, hdl, name, fx3) < 0)
136179
return SR_ERR;
137180

138-
if ((ezusb_reset(hdl, 0)) < 0)
181+
if (!fx3 && (ezusb_reset(hdl, 0)) < 0)
139182
return SR_ERR;
140183

141184
libusb_close(hdl);

src/hardware/dreamsourcelab-dslogic/api.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
268268
libusb_get_device_address(devlist[i]), NULL);
269269
} else {
270270
if (ezusb_upload_firmware(drvc->sr_ctx, devlist[i],
271-
USB_CONFIGURATION, prof->firmware) == SR_OK) {
271+
USB_CONFIGURATION, prof->firmware, FALSE) == SR_OK) {
272272
/* Store when this device's FW was updated. */
273273
devc->fw_updated = g_get_monotonic_time();
274274
} else {

src/hardware/fx2lafw/api.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,8 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
365365
libusb_get_device_address(devlist[i]), NULL);
366366
} else {
367367
if (ezusb_upload_firmware(drvc->sr_ctx, devlist[i],
368-
USB_CONFIGURATION, prof->firmware) == SR_OK) {
368+
USB_CONFIGURATION, prof->firmware,
369+
FALSE) == SR_OK) {
369370
/* Store when this device's FW was updated. */
370371
devc->fw_updated = g_get_monotonic_time();
371372
} else {

src/hardware/hantek-6xxx/api.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
267267
devices = g_slist_append(devices, sdi);
268268
devc = sdi->priv;
269269
if (ezusb_upload_firmware(drvc->sr_ctx, devlist[i],
270-
USB_CONFIGURATION, prof->firmware) == SR_OK) {
270+
USB_CONFIGURATION, prof->firmware, FALSE) == SR_OK) {
271271
/* Remember when the firmware on this device was updated. */
272272
devc->fw_updated = g_get_monotonic_time();
273273
} else {

src/hardware/hantek-dso/api.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
330330
devices = g_slist_append(devices, sdi);
331331
devc = sdi->priv;
332332
if (ezusb_upload_firmware(drvc->sr_ctx, devlist[i],
333-
USB_CONFIGURATION, prof->firmware) == SR_OK) {
333+
USB_CONFIGURATION, prof->firmware, FALSE) == SR_OK) {
334334
/* Remember when the firmware on this device was updated */
335335
devc->fw_updated = g_get_monotonic_time();
336336
} else {

src/hardware/kingst-la2016/protocol.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ SR_PRIV int la2016_upload_firmware(struct sr_context *sr_ctx, libusb_device *dev
527527
{
528528
char fw_file[1024];
529529
snprintf(fw_file, sizeof(fw_file) - 1, UC_FIRMWARE, product_id);
530-
return ezusb_upload_firmware(sr_ctx, dev, USB_CONFIGURATION, fw_file);
530+
return ezusb_upload_firmware(sr_ctx, dev, USB_CONFIGURATION, fw_file, FALSE);
531531
}
532532

533533
SR_PRIV int la2016_setup_acquisition(const struct sr_dev_inst *sdi)

src/hardware/lecroy-logicstudio/api.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
148148
break;
149149
case LOGICSTUDIO16_PID_LACK_FIRMWARE:
150150
r = ezusb_upload_firmware(drvc->sr_ctx, devlist[i],
151-
USB_CONFIGURATION, FX2_FIRMWARE);
151+
USB_CONFIGURATION, FX2_FIRMWARE, FALSE);
152152
if (r != SR_OK) {
153153
/*
154154
* An error message has already been logged by

src/hardware/saleae-logic16/api.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
219219
libusb_get_device_address(devlist[i]), NULL);
220220
} else {
221221
if (ezusb_upload_firmware(drvc->sr_ctx, devlist[i],
222-
USB_CONFIGURATION, FX2_FIRMWARE) == SR_OK) {
222+
USB_CONFIGURATION, FX2_FIRMWARE, FALSE) == SR_OK) {
223223
/* Store when this device's FW was updated. */
224224
devc->fw_updated = g_get_monotonic_time();
225225
} else {

src/libsigrok-internal.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2040,9 +2040,9 @@ SR_PRIV int sr_bt_check_notify(struct sr_bt_desc *desc);
20402040
#ifdef HAVE_LIBUSB_1_0
20412041
SR_PRIV int ezusb_reset(struct libusb_device_handle *hdl, int set_clear);
20422042
SR_PRIV int ezusb_install_firmware(struct sr_context *ctx, libusb_device_handle *hdl,
2043-
const char *name);
2043+
const char *name, gboolean fx3);
20442044
SR_PRIV int ezusb_upload_firmware(struct sr_context *ctx, libusb_device *dev,
2045-
int configuration, const char *name);
2045+
int configuration, const char *name, gboolean fx3);
20462046
#endif
20472047

20482048
/*--- usb.c -----------------------------------------------------------------*/

0 commit comments

Comments
 (0)