From d3ac4c14a3204c585bdeba8e8f27c8257d1ac826 Mon Sep 17 00:00:00 2001 From: hathach Date: Sat, 8 Dec 2018 20:51:48 +0700 Subject: [PATCH] refactor hcd api --- src/host/ehci/ehci.c | 4 +-- src/host/hcd.h | 6 ++++ src/host/ohci/ohci.c | 4 +-- src/host/usbh.c | 33 ++++++++++--------- src/host/usbh_hcd.h | 3 +- .../test/host/ehci/test_ehci_isr.c | 8 ++--- tests/lpc18xx_43xx/test/host/usbh/test_usbh.c | 10 +++--- 7 files changed, 37 insertions(+), 31 deletions(-) diff --git a/src/host/ehci/ehci.c b/src/host/ehci/ehci.c index d18f1919..00bb444f 100644 --- a/src/host/ehci/ehci.c +++ b/src/host/ehci/ehci.c @@ -609,10 +609,10 @@ static void port_connect_status_change_isr(uint8_t hostid) if (regs->portsc_bit.current_connect_status) { hcd_port_reset(hostid); - usbh_hcd_rhport_plugged_isr(hostid); + hcd_event_device_attach(hostid); }else // device unplugged { - usbh_hcd_rhport_unplugged_isr(hostid); + hcd_event_device_remove(hostid); } } diff --git a/src/host/hcd.h b/src/host/hcd.h index 387fbcf1..a5ee63c8 100644 --- a/src/host/hcd.h +++ b/src/host/hcd.h @@ -121,6 +121,12 @@ void hcd_int_disable(uint8_t rhport); //--------------------------------------------------------------------+ void hcd_event_handler(hcd_event_t const* event, bool in_isr); +// Helper to send device attach event +void hcd_event_device_attach(uint8_t rhport); + +// Helper to send device removal event +void hcd_event_device_remove(uint8_t hostid); + //--------------------------------------------------------------------+ // Endpoints API //--------------------------------------------------------------------+ diff --git a/src/host/ohci/ohci.c b/src/host/ohci/ohci.c index 2c22a3c9..88296fb8 100644 --- a/src/host/ohci/ohci.c +++ b/src/host/ohci/ohci.c @@ -745,10 +745,10 @@ void hal_hcd_isr(uint8_t hostid) { // TODO reset port immediately, without this controller will got 2-3 (debouncing connection status change) OHCI_REG->rhport_status[0] = OHCI_RHPORT_PORT_RESET_STATUS_MASK; - usbh_hcd_rhport_plugged_isr(0); + hcd_event_device_attach(0); }else { - usbh_hcd_rhport_unplugged_isr(0); + hcd_event_device_remove(0); } } diff --git a/src/host/usbh.c b/src/host/usbh.c index d97b4b0d..c83424c9 100644 --- a/src/host/usbh.c +++ b/src/host/usbh.c @@ -305,11 +305,11 @@ void usbh_hub_port_plugged_isr(uint8_t hub_addr, uint8_t hub_port) hcd_event_handler(&event, true); } -void usbh_hcd_rhport_plugged_isr(uint8_t hostid) +void hcd_event_device_attach(uint8_t rhport) { hcd_event_t event = { - .rhport = hostid, + .rhport = rhport, .event_id = HCD_EVENT_DEVICE_ATTACH }; @@ -329,6 +329,21 @@ void hcd_event_handler(hcd_event_t const* event, bool in_isr) } } +void hcd_event_device_remove(uint8_t hostid) +{ + hcd_event_t event = + { + .rhport = hostid, + .event_id = HCD_EVENT_DEVICE_REMOVE + }; + + event.attach.hub_addr = 0; + event.attach.hub_port = 0; + + hcd_event_handler(&event, true); +} + + // a device unplugged on hostid, hub_addr, hub_port // return true if found and unmounted device, false if cannot find static void usbh_device_unplugged(uint8_t hostid, uint8_t hub_addr, uint8_t hub_port) @@ -369,20 +384,6 @@ static void usbh_device_unplugged(uint8_t hostid, uint8_t hub_addr, uint8_t hub_ } -void usbh_hcd_rhport_unplugged_isr(uint8_t hostid) -{ - hcd_event_t event = - { - .rhport = hostid, - .event_id = HCD_EVENT_DEVICE_REMOVE - }; - - event.attach.hub_addr = 0; - event.attach.hub_port = 0; - - hcd_event_handler(&event, true); -} - //--------------------------------------------------------------------+ // ENUMERATION TASK //--------------------------------------------------------------------+ diff --git a/src/host/usbh_hcd.h b/src/host/usbh_hcd.h index b4d9adb2..66a0e2d9 100644 --- a/src/host/usbh_hcd.h +++ b/src/host/usbh_hcd.h @@ -94,8 +94,7 @@ extern usbh_device_t _usbh_devices[CFG_TUSB_HOST_DEVICE_MAX+1]; // including zer // callback from HCD ISR //--------------------------------------------------------------------+ void usbh_xfer_isr(pipe_handle_t pipe_hdl, uint8_t class_code, xfer_result_t event, uint32_t xferred_bytes); -void usbh_hcd_rhport_plugged_isr(uint8_t hostid); -void usbh_hcd_rhport_unplugged_isr(uint8_t hostid); + #ifdef __cplusplus } diff --git a/tests/lpc18xx_43xx/test/host/ehci/test_ehci_isr.c b/tests/lpc18xx_43xx/test/host/ehci/test_ehci_isr.c index 58b8f900..df830512 100644 --- a/tests/lpc18xx_43xx/test/host/ehci/test_ehci_isr.c +++ b/tests/lpc18xx_43xx/test/host/ehci/test_ehci_isr.c @@ -70,7 +70,7 @@ void tearDown(void) void test_isr_device_connect_highspeed(void) { - usbh_hcd_rhport_plugged_isr_Expect(hostid); + hcd_event_device_attach_Expect(hostid); //------------- Code Under Test -------------// ehci_controller_device_plug(hostid, TUSB_SPEED_HIGH); @@ -78,7 +78,7 @@ void test_isr_device_connect_highspeed(void) void test_isr_device_connect_fullspeed(void) { - usbh_hcd_rhport_plugged_isr_Expect(hostid); + hcd_event_device_attach_Expect(hostid); //------------- Code Under Test -------------// ehci_controller_device_plug(hostid, TUSB_SPEED_FULL); @@ -86,7 +86,7 @@ void test_isr_device_connect_fullspeed(void) void test_isr_device_connect_slowspeed(void) { - usbh_hcd_rhport_plugged_isr_Expect(hostid); + hcd_event_device_attach_Expect(hostid); //------------- Code Under Test -------------// ehci_controller_device_plug(hostid, TUSB_SPEED_LOW); @@ -94,7 +94,7 @@ void test_isr_device_connect_slowspeed(void) void test_isr_device_disconnect(void) { - usbh_hcd_rhport_unplugged_isr_Expect(hostid); + hcd_event_device_remove_Expect(hostid); //------------- Code Under Test -------------// ehci_controller_device_unplug(hostid); diff --git a/tests/lpc18xx_43xx/test/host/usbh/test_usbh.c b/tests/lpc18xx_43xx/test/host/usbh/test_usbh.c index dde629cf..db5382e4 100644 --- a/tests/lpc18xx_43xx/test/host/usbh/test_usbh.c +++ b/tests/lpc18xx_43xx/test/host/usbh/test_usbh.c @@ -142,7 +142,7 @@ void test_usbh_init_ok(void) #if 0 // TODO TEST enable this // device is not mounted before, even the control pipe is not open, do nothing -void test_usbh_hcd_rhport_unplugged_isr_device_not_previously_mounted(void) +void test_hcd_event_device_remove_device_not_previously_mounted(void) { uint8_t dev_addr = 1; @@ -151,10 +151,10 @@ void test_usbh_hcd_rhport_unplugged_isr_device_not_previously_mounted(void) _usbh_devices[dev_addr].hub_addr = 0; _usbh_devices[dev_addr].hub_port = 0; - usbh_hcd_rhport_unplugged_isr(0); + hcd_event_device_remove(0); } -void test_usbh_hcd_rhport_unplugged_isr(void) +void test_hcd_event_device_remove(void) { uint8_t dev_addr = 1; @@ -168,7 +168,7 @@ void test_usbh_hcd_rhport_unplugged_isr(void) hcd_pipe_control_close_ExpectAndReturn(dev_addr, TUSB_ERROR_NONE); //------------- Code Under Test -------------// - usbh_hcd_rhport_unplugged_isr(0); + hcd_event_device_remove(0); TEST_ASSERT_EQUAL(TUSB_DEVICE_STATE_REMOVING, _usbh_devices[dev_addr].state); } @@ -190,7 +190,7 @@ void test_usbh_device_unplugged_multple_class(void) hcd_pipe_control_close_ExpectAndReturn(dev_addr, TUSB_ERROR_NONE); //------------- Code Under Test -------------// - usbh_hcd_rhport_unplugged_isr(0); + hcd_event_device_remove(0); TEST_ASSERT_EQUAL(TUSB_DEVICE_STATE_REMOVING, _usbh_devices[dev_addr].state);