Skip to content

Commit f71912b

Browse files
gaborcsapoerhankur
authored andcommitted
libusb_helper.h: Increase USB timeout
When we debug a target that works as a USB device, halting the target causes the USB communication with the USB host to become unresponsive. The host will try to reconnect/reset/setup the unresponsive device during which communication with other devices on the same USB bus can get stalled for several seconds. If the JTAG adapter is on the same bus, we need to make sure openOCD will wait for packets at least as long as the host USB stack. Otherwise the USB stack might deliver a valid packet, but openOCD would ignore it due to the timeout. The xHCI spec uses 5 sec timeouts, so let's use that in openOCD with some margin. Use this value in all libusb calls. HID API might have a libusb backend and would probably be victim to the same bug, so it should use this timeout, too. Ticket: https://sourceforge.net/p/openocd/tickets/343/ Signed-off-by: Gabor Csapo <[email protected]> Change-Id: Ia3dc1356e676fe550f57a4c72f7a24ba296b6af2 Reviewed-on: https://review.openocd.org/c/openocd/+/6882 Tested-by: jenkins Reviewed-by: Antonio Borneo <[email protected]>
1 parent 2b11a23 commit f71912b

File tree

7 files changed

+46
-36
lines changed

7 files changed

+46
-36
lines changed

src/jtag/drivers/cmsis_dap.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#include <target/cortex_m.h>
4949

5050
#include "cmsis_dap.h"
51+
#include "libusb_helper.h"
5152

5253
static const struct cmsis_dap_backend *const cmsis_dap_backends[] = {
5354
#if BUILD_CMSIS_DAP_USB == 1
@@ -79,8 +80,6 @@ static uint16_t cmsis_dap_pid[MAX_USB_IDS + 1] = { 0 };
7980
static int cmsis_dap_backend = -1;
8081
static bool swd_mode;
8182

82-
#define USB_TIMEOUT 1000
83-
8483
/* CMSIS-DAP General Commands */
8584
#define CMD_DAP_INFO 0x00
8685
#define CMD_DAP_LED 0x01
@@ -360,12 +359,12 @@ static int cmsis_dap_xfer(struct cmsis_dap *dap, int txlen)
360359
}
361360

362361
uint8_t current_cmd = cmsis_dap_handle->command[0];
363-
int retval = dap->backend->write(dap, txlen, USB_TIMEOUT);
362+
int retval = dap->backend->write(dap, txlen, LIBUSB_TIMEOUT_MS);
364363
if (retval < 0)
365364
return retval;
366365

367366
/* get reply */
368-
retval = dap->backend->read(dap, USB_TIMEOUT);
367+
retval = dap->backend->read(dap, LIBUSB_TIMEOUT_MS);
369368
if (retval < 0)
370369
return retval;
371370

@@ -826,7 +825,7 @@ static void cmsis_dap_swd_write_from_queue(struct cmsis_dap *dap)
826825
}
827826
}
828827

829-
int retval = dap->backend->write(dap, idx, USB_TIMEOUT);
828+
int retval = dap->backend->write(dap, idx, LIBUSB_TIMEOUT_MS);
830829
if (retval < 0) {
831830
queued_retval = retval;
832831
goto skip;
@@ -854,7 +853,7 @@ static void cmsis_dap_swd_read_process(struct cmsis_dap *dap, int timeout_ms)
854853

855854
/* get reply */
856855
int retval = dap->backend->read(dap, timeout_ms);
857-
if (retval == ERROR_TIMEOUT_REACHED && timeout_ms < USB_TIMEOUT)
856+
if (retval == ERROR_TIMEOUT_REACHED && timeout_ms < LIBUSB_TIMEOUT_MS)
858857
return;
859858

860859
if (retval <= 0) {
@@ -929,7 +928,7 @@ static int cmsis_dap_swd_run_queue(void)
929928
cmsis_dap_swd_write_from_queue(cmsis_dap_handle);
930929

931930
while (pending_fifo_block_count)
932-
cmsis_dap_swd_read_process(cmsis_dap_handle, USB_TIMEOUT);
931+
cmsis_dap_swd_read_process(cmsis_dap_handle, LIBUSB_TIMEOUT_MS);
933932

934933
pending_fifo_put_idx = 0;
935934
pending_fifo_get_idx = 0;
@@ -953,7 +952,7 @@ static void cmsis_dap_swd_queue_cmd(uint8_t cmd, uint32_t *dst, uint32_t data)
953952
cmsis_dap_swd_write_from_queue(cmsis_dap_handle);
954953

955954
if (pending_fifo_block_count >= cmsis_dap_handle->packet_count)
956-
cmsis_dap_swd_read_process(cmsis_dap_handle, USB_TIMEOUT);
955+
cmsis_dap_swd_read_process(cmsis_dap_handle, LIBUSB_TIMEOUT_MS);
957956
}
958957

959958
if (queued_retval != ERROR_OK)

src/jtag/drivers/libusb_helper.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,20 @@
2222

2323
#include <libusb.h>
2424

25+
/* When we debug a target that works as a USB device, halting the target causes the
26+
* USB communication with the USB host to become unresponsive. The host will try
27+
* to reconnect/reset/setup the unresponsive device during which communication
28+
* with other devices on the same USB bus can get stalled for several seconds.
29+
* If the JTAG adapter is on the same bus, we need to make sure openOCD will wait
30+
* for packets at least as long as the host USB stack. Otherwise the USB stack
31+
* might deliver a valid packet, but openOCD would ignore it due to the timeout.
32+
* The xHCI spec uses 5 sec timeouts, so let's use that in openOCD with some margin.
33+
*
34+
* Use this value in all libusb calls. HID API might have a libusb backend and
35+
* would probably be victim to the same bug, so it should use this timeout, too.
36+
*/
37+
#define LIBUSB_TIMEOUT_MS (6000)
38+
2539
/* this callback should return a non NULL value only when the serial could not
2640
* be retrieved by the standard 'libusb_get_string_descriptor_ascii' */
2741
typedef char * (*adapter_get_alternate_serial_fn)(struct libusb_device_handle *device,

src/jtag/drivers/nulink_usb.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@
3333

3434
#include <hidapi.h>
3535

36-
#define NULINK_READ_TIMEOUT 1000
36+
#include "libusb_helper.h"
37+
38+
#define NULINK_READ_TIMEOUT LIBUSB_TIMEOUT_MS
3739

3840
#define NULINK_HID_MAX_SIZE (64)
3941
#define NULINK2_HID_MAX_SIZE (1024)

src/jtag/drivers/rlink.c

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@
5959
#define USB_EP2IN_SIZE (USB_EP2OUT_SIZE)
6060
#define USB_EP2BANK_SIZE (512)
6161

62-
#define USB_TIMEOUT_MS (3 * 1000)
63-
6462
#define DTC_STATUS_POLL_BYTE (ST7_USB_BUF_EP0OUT + 0xff)
6563

6664
#define ST7_PD_NBUSY_LED ST7_PD0
@@ -133,7 +131,7 @@ static int ep1_generic_commandl(struct libusb_device_handle *hdev_param, size_t
133131
hdev_param,
134132
USB_EP1OUT_ADDR,
135133
(char *)usb_buffer, sizeof(usb_buffer),
136-
USB_TIMEOUT_MS,
134+
LIBUSB_TIMEOUT_MS,
137135
&transferred
138136
);
139137

@@ -176,7 +174,7 @@ static ssize_t ep1_memory_read(
176174
usb_ret = jtag_libusb_bulk_write(
177175
hdev_param, USB_EP1OUT_ADDR,
178176
(char *)usb_buffer, sizeof(usb_buffer),
179-
USB_TIMEOUT_MS,
177+
LIBUSB_TIMEOUT_MS,
180178
&transferred
181179
);
182180

@@ -186,7 +184,7 @@ static ssize_t ep1_memory_read(
186184
usb_ret = jtag_libusb_bulk_read(
187185
hdev_param, USB_EP1IN_ADDR,
188186
(char *)buffer, length,
189-
USB_TIMEOUT_MS,
187+
LIBUSB_TIMEOUT_MS,
190188
&transferred
191189
);
192190

@@ -241,7 +239,7 @@ static ssize_t ep1_memory_write(struct libusb_device_handle *hdev_param, uint16_
241239
usb_ret = jtag_libusb_bulk_write(
242240
hdev_param, USB_EP1OUT_ADDR,
243241
(char *)usb_buffer, sizeof(usb_buffer),
244-
USB_TIMEOUT_MS,
242+
LIBUSB_TIMEOUT_MS,
245243
&transferred
246244
);
247245

@@ -432,7 +430,7 @@ static int dtc_start_download(void)
432430
usb_err = jtag_libusb_bulk_read(
433431
hdev, USB_EP1IN_ADDR,
434432
(char *)&ep2txr, 1,
435-
USB_TIMEOUT_MS,
433+
LIBUSB_TIMEOUT_MS,
436434
&transferred
437435
);
438436
if (usb_err != ERROR_OK)
@@ -462,7 +460,7 @@ static int dtc_start_download(void)
462460
usb_err = jtag_libusb_bulk_read(
463461
hdev, USB_EP1IN_ADDR,
464462
(char *)&ep2txr, 1,
465-
USB_TIMEOUT_MS,
463+
LIBUSB_TIMEOUT_MS,
466464
&transferred
467465
);
468466

@@ -488,7 +486,7 @@ static int dtc_run_download(
488486
hdev_param,
489487
USB_EP2OUT_ADDR,
490488
(char *)command_buffer, USB_EP2BANK_SIZE,
491-
USB_TIMEOUT_MS,
489+
LIBUSB_TIMEOUT_MS,
492490
&transferred
493491
);
494492
if (usb_err < 0)
@@ -512,7 +510,7 @@ static int dtc_run_download(
512510
hdev_param,
513511
USB_EP1IN_ADDR,
514512
&dtc_status, 1,
515-
USB_TIMEOUT_MS,
513+
LIBUSB_TIMEOUT_MS,
516514
&transferred
517515
);
518516
if (usb_err < 0)
@@ -533,7 +531,7 @@ static int dtc_run_download(
533531
hdev_param,
534532
USB_EP2IN_ADDR,
535533
(char *)reply_buffer, reply_buffer_size,
536-
USB_TIMEOUT_MS,
534+
LIBUSB_TIMEOUT_MS,
537535
&transferred
538536
);
539537

@@ -954,7 +952,7 @@ static void rlink_reset(int trst, int srst)
954952
usb_err = jtag_libusb_bulk_read(
955953
hdev, USB_EP1IN_ADDR,
956954
(char *)&bitmap, 1,
957-
USB_TIMEOUT_MS,
955+
LIBUSB_TIMEOUT_MS,
958956
&transferred
959957
);
960958
if (usb_err != ERROR_OK || transferred < 1) {
@@ -990,7 +988,7 @@ static void rlink_reset(int trst, int srst)
990988
usb_err = jtag_libusb_bulk_read(
991989
hdev, USB_EP1IN_ADDR,
992990
(char *)&bitmap, 1,
993-
USB_TIMEOUT_MS,
991+
LIBUSB_TIMEOUT_MS,
994992
&transferred
995993
);
996994
if (usb_err != ERROR_OK || transferred < 1) {
@@ -1021,7 +1019,7 @@ static void rlink_reset(int trst, int srst)
10211019
usb_err = jtag_libusb_bulk_read(
10221020
hdev, USB_EP1IN_ADDR,
10231021
(char *)&bitmap, 1,
1024-
USB_TIMEOUT_MS,
1022+
LIBUSB_TIMEOUT_MS,
10251023
&transferred
10261024
);
10271025
if (usb_err != ERROR_OK || transferred < 1) {
@@ -1576,7 +1574,7 @@ static int rlink_init(void)
15761574
jtag_libusb_bulk_read(
15771575
hdev, USB_EP1IN_ADDR,
15781576
(char *)reply_buffer, 1,
1579-
USB_TIMEOUT_MS,
1577+
LIBUSB_TIMEOUT_MS,
15801578
&transferred
15811579
);
15821580

@@ -1601,7 +1599,7 @@ static int rlink_init(void)
16011599
jtag_libusb_bulk_read(
16021600
hdev, USB_EP1IN_ADDR,
16031601
(char *)reply_buffer, 1,
1604-
USB_TIMEOUT_MS,
1602+
LIBUSB_TIMEOUT_MS,
16051603
&transferred
16061604
);
16071605

src/jtag/drivers/stlink_usb.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@
7171
#define ENDPOINT_IN 0x80
7272
#define ENDPOINT_OUT 0x00
7373

74-
#define STLINK_WRITE_TIMEOUT 1000
75-
#define STLINK_READ_TIMEOUT 1000
74+
#define STLINK_WRITE_TIMEOUT (LIBUSB_TIMEOUT_MS)
75+
#define STLINK_READ_TIMEOUT (LIBUSB_TIMEOUT_MS)
7676

7777
#define STLINK_RX_EP (1|ENDPOINT_IN)
7878
#define STLINK_TX_EP (2|ENDPOINT_OUT)

src/jtag/drivers/ti_icdi_usb.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@
3737
#define ICDI_WRITE_ENDPOINT 0x02
3838
#define ICDI_READ_ENDPOINT 0x83
3939

40-
#define ICDI_WRITE_TIMEOUT 1000
41-
#define ICDI_READ_TIMEOUT 1000
40+
#define ICDI_WRITE_TIMEOUT (LIBUSB_TIMEOUT_MS)
41+
#define ICDI_READ_TIMEOUT (LIBUSB_TIMEOUT_MS)
4242
#define ICDI_PACKET_SIZE 2048
4343

4444
#define PACKET_START "$"

src/jtag/drivers/ulink.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,6 @@
5656
/** USB interface number */
5757
#define USB_INTERFACE 0
5858

59-
/** libusb timeout in ms */
60-
#define USB_TIMEOUT 5000
61-
6259
/** Delay (in microseconds) to wait while EZ-USB performs ReNumeration. */
6360
#define ULINK_RENUMERATION_DELAY 1500000
6461

@@ -335,7 +332,7 @@ static int ulink_cpu_reset(struct ulink *device, unsigned char reset_bit)
335332

336333
ret = libusb_control_transfer(device->usb_device_handle,
337334
(LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE),
338-
REQUEST_FIRMWARE_LOAD, CPUCS_REG, 0, &reset_bit, 1, USB_TIMEOUT);
335+
REQUEST_FIRMWARE_LOAD, CPUCS_REG, 0, &reset_bit, 1, LIBUSB_TIMEOUT_MS);
339336

340337
/* usb_control_msg() returns the number of bytes transferred during the
341338
* DATA stage of the control transfer - must be exactly 1 in this case! */
@@ -478,7 +475,7 @@ static int ulink_write_firmware_section(struct ulink *device,
478475
ret = libusb_control_transfer(device->usb_device_handle,
479476
(LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE),
480477
REQUEST_FIRMWARE_LOAD, addr, FIRMWARE_ADDR, (unsigned char *)data_ptr,
481-
chunk_size, USB_TIMEOUT);
478+
chunk_size, LIBUSB_TIMEOUT_MS);
482479

483480
if (ret != (int)chunk_size) {
484481
/* Abort if libusb sent less data than requested */
@@ -662,7 +659,7 @@ static int ulink_append_queue(struct ulink *device, struct ulink_cmd *ulink_cmd)
662659
if ((newsize_out > 64) || (newsize_in > 64)) {
663660
/* New command does not fit. Execute all commands in queue before starting
664661
* new queue with the current command as first entry. */
665-
ret = ulink_execute_queued_commands(device, USB_TIMEOUT);
662+
ret = ulink_execute_queued_commands(device, LIBUSB_TIMEOUT_MS);
666663

667664
if (ret == ERROR_OK)
668665
ret = ulink_post_process_queue(device);
@@ -1960,7 +1957,7 @@ static int ulink_execute_queue(void)
19601957
}
19611958

19621959
if (ulink_handle->commands_in_queue > 0) {
1963-
ret = ulink_execute_queued_commands(ulink_handle, USB_TIMEOUT);
1960+
ret = ulink_execute_queued_commands(ulink_handle, LIBUSB_TIMEOUT_MS);
19641961
if (ret != ERROR_OK)
19651962
return ret;
19661963

0 commit comments

Comments
 (0)