Skip to content

Commit 3c7b5dc

Browse files
committed
Hub: Handle hub device status change interrupt
1 parent aafea8e commit 3c7b5dc

File tree

1 file changed

+51
-8
lines changed

1 file changed

+51
-8
lines changed

src/host/hub.c

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ typedef struct
4444
uint8_t status_change; // data from status change interrupt endpoint
4545

4646
hub_port_status_response_t port_status;
47+
hub_status_response_t hub_status;
4748
} hub_interface_t;
4849

4950
CFG_TUSB_MEM_SECTION static hub_interface_t hub_data[CFG_TUH_HUB];
@@ -314,6 +315,7 @@ static void config_port_power_complete (tuh_xfer_t* xfer)
314315
//--------------------------------------------------------------------+
315316

316317
static void hub_port_get_status_complete (tuh_xfer_t* xfer);
318+
static void hub_get_status_complete (tuh_xfer_t* xfer);
317319
static void connection_clear_conn_change_complete (tuh_xfer_t* xfer);
318320
static void connection_port_reset_complete (tuh_xfer_t* xfer);
319321

@@ -326,19 +328,31 @@ bool hub_xfer_cb(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t result, uint32
326328

327329
hub_interface_t* p_hub = get_itf(dev_addr);
328330

329-
TU_LOG2(" Port Status Change = 0x%02X\r\n", p_hub->status_change);
331+
TU_LOG2(" Hub Status Change = 0x%02X\r\n", p_hub->status_change);
330332

331-
// Hub ignore bit0 in status change
332-
for (uint8_t port=1; port <= p_hub->port_count; port++)
333+
// Hub bit 0 is for the hub device events
334+
if (tu_bit_test(p_hub->status_change, 0))
333335
{
334-
if ( tu_bit_test(p_hub->status_change, port) )
336+
if (hub_port_get_status(dev_addr, 0, &p_hub->hub_status, hub_get_status_complete, 0) == false)
335337
{
336-
if (hub_port_get_status(dev_addr, port, &p_hub->port_status, hub_port_get_status_complete, 0) == false)
338+
//Hub status control transfer failed, retry
339+
hub_edpt_status_xfer(dev_addr);
340+
}
341+
}
342+
else
343+
{
344+
// Hub bits 1 to n are hub port events
345+
for (uint8_t port=1; port <= p_hub->port_count; port++)
346+
{
347+
if ( tu_bit_test(p_hub->status_change, port) )
337348
{
338-
//Hub status control transfer failed, retry
339-
hub_edpt_status_xfer(dev_addr);
349+
if (hub_port_get_status(dev_addr, port, &p_hub->port_status, hub_port_get_status_complete, 0) == false)
350+
{
351+
//Hub status control transfer failed, retry
352+
hub_edpt_status_xfer(dev_addr);
353+
}
354+
break;
340355
}
341-
break;
342356
}
343357
}
344358

@@ -347,6 +361,35 @@ bool hub_xfer_cb(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t result, uint32
347361
return true;
348362
}
349363

364+
static void hub_clear_feature_complete_stub(tuh_xfer_t* xfer)
365+
{
366+
TU_ASSERT(xfer->result == XFER_RESULT_SUCCESS, );
367+
hub_edpt_status_xfer(xfer->daddr);
368+
}
369+
370+
static void hub_get_status_complete (tuh_xfer_t* xfer)
371+
{
372+
TU_ASSERT(xfer->result == XFER_RESULT_SUCCESS, );
373+
374+
uint8_t const daddr = xfer->daddr;
375+
hub_interface_t* p_hub = get_itf(daddr);
376+
uint8_t const port_num = (uint8_t) tu_le16toh(xfer->setup->wIndex);
377+
TU_ASSERT(port_num == 0 , );
378+
379+
TU_LOG2("HUB Got hub status, addr = %u, status = %04x\r\n", daddr, p_hub->hub_status.change.value);
380+
381+
if (p_hub->hub_status.change.local_power_source)
382+
{
383+
TU_LOG2("HUB Local Power Change, addr = %u\r\n", daddr);
384+
hub_port_clear_feature(daddr, port_num, HUB_FEATURE_HUB_LOCAL_POWER_CHANGE, hub_clear_feature_complete_stub, 0);
385+
}
386+
else if (p_hub->hub_status.change.over_current)
387+
{
388+
TU_LOG1("HUB Over Current, addr = %u\r\n", daddr);
389+
hub_port_clear_feature(daddr, port_num, HUB_FEATURE_HUB_OVER_CURRENT_CHANGE, hub_clear_feature_complete_stub, 0);
390+
}
391+
}
392+
350393
static void hub_port_get_status_complete (tuh_xfer_t* xfer)
351394
{
352395
TU_ASSERT(xfer->result == XFER_RESULT_SUCCESS, );

0 commit comments

Comments
 (0)