Skip to content

Commit 206d63e

Browse files
committed
correct EHCI reporting failed xfer (instead of stalled) when device is unplugged
1 parent e2d3c0b commit 206d63e

File tree

9 files changed

+74
-38
lines changed

9 files changed

+74
-38
lines changed

src/class/cdc/cdc_host.c

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@
3535

3636
// Debug level, TUSB_CFG_DEBUG must be at least this level for debug message
3737
#define CDCH_DEBUG 2
38-
39-
#define TU_LOG_CDCH(...) TU_LOG(CDCH_DEBUG, __VA_ARGS__)
38+
#define TU_LOG_DRV(...) TU_LOG(CDCH_DEBUG, __VA_ARGS__)
4039

4140
//--------------------------------------------------------------------+
4241
// Host CDC Interface
@@ -537,6 +536,8 @@ void cdch_close(uint8_t daddr)
537536
cdch_interface_t* p_cdc = &cdch_data[idx];
538537
if (p_cdc->daddr == daddr)
539538
{
539+
TU_LOG_DRV(" CDCh close addr = %u index = %u\r\n", daddr, idx);
540+
540541
// Invoke application callback
541542
if (tuh_cdc_umount_cb) tuh_cdc_umount_cb(idx);
542543

@@ -804,7 +805,7 @@ static void acm_process_config(tuh_xfer_t* xfer)
804805

805806
static bool acm_set_control_line_state(cdch_interface_t* p_cdc, uint16_t line_state, tuh_xfer_cb_t complete_cb, uintptr_t user_data) {
806807
TU_VERIFY(p_cdc->acm_capability.support_line_request);
807-
TU_LOG_CDCH("CDC ACM Set Control Line State\r\n");
808+
TU_LOG_DRV("CDC ACM Set Control Line State\r\n");
808809

809810
tusb_control_request_t const request = {
810811
.bmRequestType_bit = {
@@ -834,7 +835,7 @@ static bool acm_set_control_line_state(cdch_interface_t* p_cdc, uint16_t line_st
834835
}
835836

836837
static bool acm_set_line_coding(cdch_interface_t* p_cdc, cdc_line_coding_t const* line_coding, tuh_xfer_cb_t complete_cb, uintptr_t user_data) {
837-
TU_LOG_CDCH("CDC ACM Set Line Conding\r\n");
838+
TU_LOG_DRV("CDC ACM Set Line Conding\r\n");
838839

839840
tusb_control_request_t const request = {
840841
.bmRequestType_bit = {
@@ -894,7 +895,7 @@ static bool ftdi_open(uint8_t daddr, const tusb_desc_interface_t *itf_desc, uint
894895
cdch_interface_t * p_cdc = make_new_itf(daddr, itf_desc);
895896
TU_VERIFY(p_cdc);
896897

897-
TU_LOG_CDCH("FTDI opened\r\n");
898+
TU_LOG_DRV("FTDI opened\r\n");
898899

899900
p_cdc->serial_drid = SERIAL_DRIVER_FTDI;
900901

@@ -938,7 +939,7 @@ static bool ftdi_sio_reset(cdch_interface_t* p_cdc, tuh_xfer_cb_t complete_cb, u
938939

939940
static bool ftdi_sio_set_modem_ctrl(cdch_interface_t* p_cdc, uint16_t line_state, tuh_xfer_cb_t complete_cb, uintptr_t user_data)
940941
{
941-
TU_LOG_CDCH("CDC FTDI Set Control Line State\r\n");
942+
TU_LOG_DRV("CDC FTDI Set Control Line State\r\n");
942943
p_cdc->user_control_cb = complete_cb;
943944
TU_ASSERT(ftdi_sio_set_request(p_cdc, FTDI_SIO_MODEM_CTRL, 0x0300 | line_state,
944945
complete_cb ? cdch_internal_control_complete : NULL, user_data));
@@ -974,7 +975,7 @@ static uint32_t ftdi_232bm_baud_to_divisor(uint32_t baud)
974975
static bool ftdi_sio_set_baudrate(cdch_interface_t* p_cdc, uint32_t baudrate, tuh_xfer_cb_t complete_cb, uintptr_t user_data)
975976
{
976977
uint16_t const divisor = (uint16_t) ftdi_232bm_baud_to_divisor(baudrate);
977-
TU_LOG_CDCH("CDC FTDI Set BaudRate = %lu, divisor = 0x%04x\n", baudrate, divisor);
978+
TU_LOG_DRV("CDC FTDI Set BaudRate = %lu, divisor = 0x%04x\n", baudrate, divisor);
978979

979980
p_cdc->user_control_cb = complete_cb;
980981
_ftdi_requested_baud = baudrate;
@@ -1061,7 +1062,7 @@ static bool cp210x_open(uint8_t daddr, tusb_desc_interface_t const *itf_desc, ui
10611062
cdch_interface_t * p_cdc = make_new_itf(daddr, itf_desc);
10621063
TU_VERIFY(p_cdc);
10631064

1064-
TU_LOG_CDCH("CP210x opened\r\n");
1065+
TU_LOG_DRV("CP210x opened\r\n");
10651066
p_cdc->serial_drid = SERIAL_DRIVER_CP210X;
10661067

10671068
// endpoint pair
@@ -1109,7 +1110,7 @@ static bool cp210x_ifc_enable(cdch_interface_t* p_cdc, uint16_t enabled, tuh_xfe
11091110
}
11101111

11111112
static bool cp210x_set_baudrate(cdch_interface_t* p_cdc, uint32_t baudrate, tuh_xfer_cb_t complete_cb, uintptr_t user_data) {
1112-
TU_LOG_CDCH("CDC CP210x Set BaudRate = %lu\n", baudrate);
1113+
TU_LOG_DRV("CDC CP210x Set BaudRate = %lu\n", baudrate);
11131114
uint32_t baud_le = tu_htole32(baudrate);
11141115
p_cdc->user_control_cb = complete_cb;
11151116
return cp210x_set_request(p_cdc, CP210X_SET_BAUDRATE, 0, (uint8_t *) &baud_le, 4,
@@ -1118,7 +1119,7 @@ static bool cp210x_set_baudrate(cdch_interface_t* p_cdc, uint32_t baudrate, tuh_
11181119

11191120
static bool cp210x_set_modem_ctrl(cdch_interface_t* p_cdc, uint16_t line_state, tuh_xfer_cb_t complete_cb, uintptr_t user_data)
11201121
{
1121-
TU_LOG_CDCH("CDC CP210x Set Control Line State\r\n");
1122+
TU_LOG_DRV("CDC CP210x Set Control Line State\r\n");
11221123
p_cdc->user_control_cb = complete_cb;
11231124
return cp210x_set_request(p_cdc, CP210X_SET_MHS, 0x0300 | line_state, NULL, 0,
11241125
complete_cb ? cdch_internal_control_complete : NULL, user_data);

src/class/hid/hid_host.c

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@
3333

3434
#include "hid_host.h"
3535

36+
// Debug level, TUSB_CFG_DEBUG must be at least this level for debug message
37+
#define HIDH_DEBUG 2
38+
#define TU_LOG_DRV(...) TU_LOG(HIDH_DEBUG, __VA_ARGS__)
39+
3640
//--------------------------------------------------------------------+
3741
// MACRO CONSTANT TYPEDEF
3842
//--------------------------------------------------------------------+
@@ -207,7 +211,7 @@ static void set_protocol_complete(tuh_xfer_t* xfer)
207211

208212
static bool _hidh_set_protocol(uint8_t daddr, uint8_t itf_num, uint8_t protocol, tuh_xfer_cb_t complete_cb, uintptr_t user_data)
209213
{
210-
TU_LOG2("HID Set Protocol = %d\r\n", protocol);
214+
TU_LOG_DRV("HID Set Protocol = %d\r\n", protocol);
211215

212216
tusb_control_request_t const request =
213217
{
@@ -246,7 +250,7 @@ bool tuh_hid_set_protocol(uint8_t daddr, uint8_t idx, uint8_t protocol)
246250

247251
static void set_report_complete(tuh_xfer_t* xfer)
248252
{
249-
TU_LOG2("HID Set Report complete\r\n");
253+
TU_LOG_DRV("HID Set Report complete\r\n");
250254

251255
if (tuh_hid_set_report_complete_cb)
252256
{
@@ -266,7 +270,7 @@ bool tuh_hid_set_report(uint8_t daddr, uint8_t idx, uint8_t report_id, uint8_t r
266270
hidh_interface_t* p_hid = get_hid_itf(daddr, idx);
267271
TU_VERIFY(p_hid);
268272

269-
TU_LOG2("HID Set Report: id = %u, type = %u, len = %u\r\n", report_id, report_type, len);
273+
TU_LOG_DRV("HID Set Report: id = %u, type = %u, len = %u\r\n", report_id, report_type, len);
270274

271275
tusb_control_request_t const request =
272276
{
@@ -298,7 +302,7 @@ bool tuh_hid_set_report(uint8_t daddr, uint8_t idx, uint8_t report_id, uint8_t r
298302
static bool _hidh_set_idle(uint8_t daddr, uint8_t itf_num, uint16_t idle_rate, tuh_xfer_cb_t complete_cb, uintptr_t user_data)
299303
{
300304
// SET IDLE request, device can stall if not support this request
301-
TU_LOG2("HID Set Idle \r\n");
305+
TU_LOG_DRV("HID Set Idle \r\n");
302306

303307
tusb_control_request_t const request =
304308
{
@@ -367,7 +371,7 @@ bool tuh_hid_send_ready(uint8_t dev_addr, uint8_t idx)
367371

368372
bool tuh_hid_send_report(uint8_t daddr, uint8_t idx, uint8_t report_id, const void* report, uint16_t len)
369373
{
370-
TU_LOG2("HID Send Report %d\r\n", report_id);
374+
TU_LOG_DRV("HID Send Report %d\r\n", report_id);
371375

372376
hidh_interface_t* p_hid = get_hid_itf(daddr, idx);
373377
TU_VERIFY(p_hid);
@@ -430,7 +434,7 @@ bool hidh_xfer_cb(uint8_t daddr, uint8_t ep_addr, xfer_result_t result, uint32_t
430434

431435
if ( dir == TUSB_DIR_IN )
432436
{
433-
TU_LOG2(" Get Report callback (%u, %u)\r\n", daddr, idx);
437+
TU_LOG_DRV(" Get Report callback (%u, %u)\r\n", daddr, idx);
434438
TU_LOG3_MEM(p_hid->epin_buf, xferred_bytes, 2);
435439
tuh_hid_report_received_cb(daddr, idx, p_hid->epin_buf, (uint16_t) xferred_bytes);
436440
}else
@@ -448,8 +452,9 @@ void hidh_close(uint8_t daddr)
448452
hidh_interface_t* p_hid = &_hidh_itf[i];
449453
if (p_hid->daddr == daddr)
450454
{
451-
if(tuh_hid_umount_cb) tuh_hid_umount_cb(daddr, i);
452-
p_hid->daddr = 0;
455+
TU_LOG_DRV(" HIDh close addr = %u index = %u\r\n", daddr, i);
456+
if(tuh_hid_umount_cb) tuh_hid_umount_cb(daddr, i);
457+
p_hid->daddr = 0;
453458
}
454459
}
455460
}
@@ -465,7 +470,7 @@ bool hidh_open(uint8_t rhport, uint8_t daddr, tusb_desc_interface_t const *desc_
465470

466471
TU_VERIFY(TUSB_CLASS_HID == desc_itf->bInterfaceClass);
467472

468-
TU_LOG2("[%u] HID opening Interface %u\r\n", daddr, desc_itf->bInterfaceNumber);
473+
TU_LOG_DRV("[%u] HID opening Interface %u\r\n", daddr, desc_itf->bInterfaceNumber);
469474

470475
// len = interface + hid + n*endpoints
471476
uint16_t const drv_len = (uint16_t) (sizeof(tusb_desc_interface_t) + sizeof(tusb_hid_descriptor_hid_t) +
@@ -592,7 +597,7 @@ static void process_set_config(tuh_xfer_t* xfer)
592597
// using usbh enumeration buffer since report descriptor can be very long
593598
if( p_hid->report_desc_len > CFG_TUH_ENUMERATION_BUFSIZE )
594599
{
595-
TU_LOG2("HID Skip Report Descriptor since it is too large %u bytes\r\n", p_hid->report_desc_len);
600+
TU_LOG_DRV("HID Skip Report Descriptor since it is too large %u bytes\r\n", p_hid->report_desc_len);
596601

597602
// Driver is mounted without report descriptor
598603
config_driver_mount_complete(daddr, idx, NULL, 0);
@@ -763,7 +768,7 @@ uint8_t tuh_hid_parse_report_descriptor(tuh_hid_report_info_t* report_info_arr,
763768
for ( uint8_t i = 0; i < report_num; i++ )
764769
{
765770
info = report_info_arr+i;
766-
TU_LOG2("%u: id = %u, usage_page = %u, usage = %u\r\n", i, info->report_id, info->usage_page, info->usage);
771+
TU_LOG_DRV("%u: id = %u, usage_page = %u, usage = %u\r\n", i, info->report_id, info->usage_page, info->usage);
767772
}
768773

769774
return report_num;

src/class/msc/msc_host.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535

3636
// Debug level, TUSB_CFG_DEBUG must be at least this level for debug message
3737
#define MSCH_DEBUG 2
38-
3938
#define TU_LOG_MSCH(...) TU_LOG(MSCH_DEBUG, __VA_ARGS__)
4039

4140
//--------------------------------------------------------------------+
@@ -82,6 +81,7 @@ CFG_TUH_MEM_SECTION static msch_interface_t _msch_itf[CFG_TUH_DEVICE_MAX];
8281
CFG_TUH_MEM_SECTION CFG_TUH_MEM_ALIGN
8382
static uint8_t _msch_buffer[sizeof(scsi_inquiry_resp_t)];
8483

84+
// FIXME potential nul reference
8585
TU_ATTR_ALWAYS_INLINE
8686
static inline msch_interface_t* get_itf(uint8_t dev_addr)
8787
{
@@ -305,11 +305,15 @@ void msch_init(void)
305305
void msch_close(uint8_t dev_addr)
306306
{
307307
TU_VERIFY(dev_addr <= CFG_TUH_DEVICE_MAX, );
308-
309308
msch_interface_t* p_msc = get_itf(dev_addr);
309+
TU_VERIFY(p_msc->configured, );
310+
311+
TU_LOG_MSCH(" MSCh close addr = %d\r\n", dev_addr);
310312

311313
// invoke Application Callback
312-
if (p_msc->mounted && tuh_msc_umount_cb) tuh_msc_umount_cb(dev_addr);
314+
if (p_msc->mounted) {
315+
if(tuh_msc_umount_cb) tuh_msc_umount_cb(dev_addr);
316+
}
313317

314318
tu_memclr(p_msc, sizeof(msch_interface_t));
315319
}

src/common/tusb_debug.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#if CFG_TUSB_DEBUG >= 2
4747
extern char const* const tu_str_speed[];
4848
extern char const* const tu_str_std_request[];
49+
extern char const* const tu_str_xfer_result[];
4950
#endif
5051

5152
void tu_print_mem(void const *buf, uint32_t count, uint8_t indent);

src/device/usbd_control.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@
3232
#include "tusb.h"
3333
#include "device/usbd_pvt.h"
3434

35-
#if CFG_TUSB_DEBUG >= 2
35+
// Debug level of USBD Control
36+
#define USBD_CONTROL_DEBUG 2
37+
38+
#if CFG_TUSB_DEBUG >= USBD_CONTROL_DEBUG
3639
extern void usbd_driver_print_control_complete_name(usbd_control_xfer_cb_t callback);
3740
#endif
3841

@@ -188,7 +191,7 @@ bool usbd_control_xfer_cb (uint8_t rhport, uint8_t ep_addr, xfer_result_t result
188191
{
189192
TU_VERIFY(_ctrl_xfer.buffer);
190193
memcpy(_ctrl_xfer.buffer, _usbd_ctrl_buf, xferred_bytes);
191-
TU_LOG_MEM(2, _usbd_ctrl_buf, xferred_bytes, 2);
194+
TU_LOG_MEM(USBD_CONTROL_DEBUG, _usbd_ctrl_buf, xferred_bytes, 2);
192195
}
193196

194197
_ctrl_xfer.total_xferred += (uint16_t) xferred_bytes;
@@ -205,7 +208,7 @@ bool usbd_control_xfer_cb (uint8_t rhport, uint8_t ep_addr, xfer_result_t result
205208
// callback can still stall control in status phase e.g out data does not make sense
206209
if ( _ctrl_xfer.complete_cb )
207210
{
208-
#if CFG_TUSB_DEBUG >= 2
211+
#if CFG_TUSB_DEBUG >= USBD_CONTROL_DEBUG
209212
usbd_driver_print_control_complete_name(_ctrl_xfer.complete_cb);
210213
#endif
211214

src/host/hub.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@
3333
#include "usbh_classdriver.h"
3434
#include "hub.h"
3535

36+
// Debug level, TUSB_CFG_DEBUG must be at least this level for debug message
37+
#define HUB_DEBUG 2
38+
#define TU_LOG_DRV(...) TU_LOG(HUB_DEBUG, __VA_ARGS__)
39+
3640
//--------------------------------------------------------------------+
3741
// MACRO CONSTANT TYPEDEF
3842
//--------------------------------------------------------------------+
@@ -218,7 +222,10 @@ void hub_close(uint8_t dev_addr)
218222
TU_VERIFY(dev_addr > CFG_TUH_DEVICE_MAX, );
219223
hub_interface_t* p_hub = get_itf(dev_addr);
220224

221-
if (p_hub->ep_in) tu_memclr(p_hub, sizeof( hub_interface_t));
225+
if (p_hub->ep_in) {
226+
TU_LOG_DRV(" HUB close addr = %d\r\n", dev_addr);
227+
tu_memclr(p_hub, sizeof( hub_interface_t));
228+
}
222229
}
223230

224231
bool hub_edpt_status_xfer(uint8_t dev_addr)

src/host/usbh.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ typedef struct
6161
uint8_t hub_addr;
6262
uint8_t hub_port;
6363
uint8_t speed;
64+
65+
// enumeration is in progress, done when all interfaces are configured
6466
volatile uint8_t enumerating;
6567

6668
// struct TU_ATTR_PACKED {
@@ -436,7 +438,8 @@ void tuh_task_ext(uint32_t timeout_ms, bool in_isr)
436438
uint8_t const epnum = tu_edpt_number(ep_addr);
437439
uint8_t const ep_dir = tu_edpt_dir(ep_addr);
438440

439-
TU_LOG_USBH("on EP %02X with %u bytes\r\n", ep_addr, (unsigned int) event.xfer_complete.len);
441+
TU_LOG_USBH("on EP %02X with %u bytes %s\r\n", ep_addr, (unsigned int) event.xfer_complete.len,
442+
tu_str_xfer_result[event.xfer_complete.result]);
440443

441444
if (event.dev_addr == 0)
442445
{
@@ -866,6 +869,10 @@ TU_ATTR_FAST_FUNC void hcd_event_handler(hcd_event_t const* event, bool in_isr)
866869
{
867870
switch (event->event_id)
868871
{
872+
// case HCD_EVENT_DEVICE_REMOVE:
873+
//
874+
// break;
875+
869876
default:
870877
osal_queue_send(_usbh_q, event, in_isr);
871878
break;
@@ -1128,30 +1135,28 @@ static void process_device_unplugged(uint8_t rhport, uint8_t hub_addr, uint8_t h
11281135
usbh_device_t* dev = &_usbh_devices[dev_id];
11291136
uint8_t const dev_addr = dev_id+1;
11301137

1131-
// TODO Hub multiple level
11321138
if (dev->rhport == rhport &&
11331139
(hub_addr == 0 || dev->hub_addr == hub_addr) && // hub_addr = 0 means roothub
11341140
(hub_port == 0 || dev->hub_port == hub_port) && // hub_port = 0 means all devices of downstream hub
11351141
dev->connected)
11361142
{
1137-
TU_LOG_USBH(" Address = %u\r\n", dev_addr);
1143+
TU_LOG_USBH("Device unplugged address = %u\r\n", dev_addr);
11381144

11391145
if (is_hub_addr(dev_addr))
11401146
{
1141-
TU_LOG(USBH_DEBUG, "HUB address = %u is unmounted\r\n", dev_addr);
1147+
TU_LOG(USBH_DEBUG, " is a HUB device\r\n", dev_addr);
11421148
// If the device itself is a usb hub, unplug downstream devices.
11431149
// FIXME un-roll recursive calls to prevent potential stack overflow
11441150
process_device_unplugged(rhport, dev_addr, 0);
11451151
}else
11461152
{
1147-
// Invoke callback before closing driver
1153+
// Invoke callback before closing driver (maybe call it later ?)
11481154
if (tuh_umount_cb) tuh_umount_cb(dev_addr);
11491155
}
11501156

11511157
// Close class driver
11521158
for (uint8_t drv_id = 0; drv_id < USBH_CLASS_DRIVER_COUNT; drv_id++)
11531159
{
1154-
TU_LOG_USBH("%s close\r\n", usbh_class_drivers[drv_id].name);
11551160
usbh_class_drivers[drv_id].close(dev_addr);
11561161
}
11571162

@@ -1449,6 +1454,7 @@ static uint8_t get_new_address(bool is_hub)
14491454
{
14501455
uint8_t start;
14511456
uint8_t end;
1457+
14521458
if ( is_hub )
14531459
{
14541460
start = CFG_TUH_DEVICE_MAX;
@@ -1459,7 +1465,7 @@ static uint8_t get_new_address(bool is_hub)
14591465
end = start + CFG_TUH_DEVICE_MAX;
14601466
}
14611467

1462-
for ( uint8_t idx = start; idx < end; idx++)
1468+
for (uint8_t idx = start; idx < end; idx++)
14631469
{
14641470
if (!_usbh_devices[idx].connected) return (idx+1);
14651471
}

src/portable/ehci/ehci.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,11 @@ static inline ehci_qhd_t* qhd_get_from_addr (uint8_t dev_addr, uint8_t ep_addr);
142142
// determine if a queue head has bus-related error
143143
static inline bool qhd_has_xact_error (ehci_qhd_t * p_qhd)
144144
{
145-
return (p_qhd->qtd_overlay.buffer_err || p_qhd->qtd_overlay.babble_err || p_qhd->qtd_overlay.xact_err);
146-
//p_qhd->qtd_overlay.non_hs_period_missed_uframe || p_qhd->qtd_overlay.pingstate_err TODO split transaction error
145+
volatile ehci_qtd_t *qtd_overlay = &p_qhd->qtd_overlay;
146+
147+
// Error count = 0 often occurs when device disconnected
148+
return (qtd_overlay->err_count == 0 || qtd_overlay->buffer_err || qtd_overlay->babble_err || qtd_overlay->xact_err);
149+
//qtd_overlay->non_hs_period_missed_uframe || qtd_overlay->pingstate_err TODO split transaction error
147150
}
148151

149152
static void qhd_init(ehci_qhd_t *p_qhd, uint8_t dev_addr, tusb_desc_endpoint_t const * ep_desc);
@@ -630,7 +633,9 @@ static void qhd_xfer_error_isr(ehci_qhd_t * p_qhd)
630633

631634
p_qhd->total_xferred_bytes += p_qhd->p_qtd_list_head->expected_bytes - p_qhd->p_qtd_list_head->total_bytes;
632635

633-
// if ( XFER_RESULT_FAILED == error_event ) TU_BREAKPOINT(); // TODO skip unplugged device
636+
// if ( XFER_RESULT_FAILED == error_event ) {
637+
// TU_BREAKPOINT(); // TODO skip unplugged device
638+
// }
634639

635640
p_qhd->p_qtd_list_head->used = 0; // free QTD
636641
qtd_remove_1st_from_qhd(p_qhd);

0 commit comments

Comments
 (0)