summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorhathach <[email protected]>2020-09-07 15:19:20 +0700
committerhathach <[email protected]>2020-09-07 15:19:20 +0700
commit66a10ec9c8042a48053b4af67a932f3cfe9470c2 (patch)
treea3f70a3ad963af7cedc9e533180a123265096811 /src
parentb3e81673c0582a07a764981604a560ee3d310ef9 (diff)
rework usbh control transfer
use series of complete callback instead of blocking semaphore, which is more noOS friendly. still working with hid host
Diffstat (limited to 'src')
-rw-r--r--src/class/hid/hid_host.c22
-rw-r--r--src/common/tusb_types.h4
-rw-r--r--src/host/usbh.c299
-rw-r--r--src/host/usbh.h9
-rw-r--r--src/host/usbh_control.c135
-rw-r--r--src/host/usbh_hcd.h11
6 files changed, 432 insertions, 48 deletions
diff --git a/src/class/hid/hid_host.c b/src/class/hid/hid_host.c
index 6002ead3c..5058191f4 100644
--- a/src/class/hid/hid_host.c
+++ b/src/class/hid/hid_host.c
@@ -175,14 +175,22 @@ bool hidh_open_subtask(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t c
// SET IDLE = 0 request
// Device can stall if not support this request
- tusb_control_request_t request = {
- .bmRequestType_bit = { .recipient = TUSB_REQ_RCPT_INTERFACE, .type = TUSB_REQ_TYPE_CLASS, .direction = TUSB_DIR_OUT },
- .bRequest = HID_REQ_CONTROL_SET_IDLE,
- .wValue = 0, // idle_rate = 0
- .wIndex = p_interface_desc->bInterfaceNumber,
- .wLength = 0
+ tusb_control_request_t const request =
+ {
+ .bmRequestType_bit =
+ {
+ .recipient = TUSB_REQ_RCPT_INTERFACE,
+ .type = TUSB_REQ_TYPE_CLASS,
+ .direction = TUSB_DIR_OUT
+ },
+ .bRequest = HID_REQ_CONTROL_SET_IDLE,
+ .wValue = 0, // idle_rate = 0
+ .wIndex = p_interface_desc->bInterfaceNumber,
+ .wLength = 0
};
- usbh_control_xfer( dev_addr, &request, NULL ); // TODO stall is valid
+
+ // stall is a valid response for SET_IDLE, therefore we could ignore result of this request
+ tuh_control_xfer(dev_addr, &request, NULL, NULL);
#if 0
//------------- Get Report Descriptor TODO HID parser -------------//
diff --git a/src/common/tusb_types.h b/src/common/tusb_types.h
index 70d0db942..d6b6ea627 100644
--- a/src/common/tusb_types.h
+++ b/src/common/tusb_types.h
@@ -276,6 +276,8 @@ typedef struct TU_ATTR_PACKED
uint8_t bNumConfigurations ; ///< Number of possible configurations.
} tusb_desc_device_t;
+TU_VERIFY_STATIC( sizeof(tusb_desc_device_t) == 18, "size is not correct");
+
// USB Binary Device Object Store (BOS) Descriptor
typedef struct TU_ATTR_PACKED
{
@@ -431,7 +433,7 @@ typedef struct TU_ATTR_PACKED{
uint16_t wLength;
} tusb_control_request_t;
-TU_VERIFY_STATIC( sizeof(tusb_control_request_t) == 8, "mostly compiler option issue");
+TU_VERIFY_STATIC( sizeof(tusb_control_request_t) == 8, "size is not correct");
// TODO move to somewhere suitable
static inline uint8_t bm_request_type(uint8_t direction, uint8_t type, uint8_t recipient)
diff --git a/src/host/usbh.c b/src/host/usbh.c
index a7f6397cd..592eac267 100644
--- a/src/host/usbh.c
+++ b/src/host/usbh.c
@@ -108,6 +108,11 @@ static usbh_class_driver_t const usbh_class_drivers[] =
enum { USBH_CLASS_DRIVER_COUNT = TU_ARRAY_SIZE(usbh_class_drivers) };
+enum { RESET_DELAY = 500 }; // 200 USB specs say only 50ms but many devices require much longer
+
+enum { CONFIG_NUM = 1 }; // default to use configuration 1
+
+
//--------------------------------------------------------------------+
// INTERNAL OBJECT & FUNCTION DECLARATION
//--------------------------------------------------------------------+
@@ -125,6 +130,9 @@ CFG_TUSB_MEM_SECTION TU_ATTR_ALIGNED(4) static uint8_t _usbh_ctrl_buf[CFG_TUSB_H
//------------- Helper Function Prototypes -------------//
static inline uint8_t get_new_address(void);
+// from usbh_control.c
+extern bool usbh_control_xfer_cb (uint8_t dev_addr, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes);
+
//--------------------------------------------------------------------+
// PUBLIC API (Parameter Verification is required)
//--------------------------------------------------------------------+
@@ -281,23 +289,21 @@ void hcd_event_xfer_complete(uint8_t dev_addr, uint8_t ep_addr, uint32_t xferred
// usbh_devices[ pipe_hdl.dev_addr ].control.xferred_bytes = xferred_bytes; not yet neccessary
osal_semaphore_post( dev->control.sem_hdl, true ); // FIXME post within ISR
}
- else
+
+ hcd_event_t event =
{
- hcd_event_t event =
+ .rhport = 0, // TODO correct rhport
+ .event_id = HCD_EVENT_XFER_COMPLETE,
+ .dev_addr = dev_addr,
+ .xfer_complete =
{
- .rhport = 0,
- .event_id = HCD_EVENT_XFER_COMPLETE,
- .dev_addr = dev_addr,
- .xfer_complete =
- {
- .ep_addr = ep_addr,
- .result = result,
- .len = xferred_bytes
- }
- };
+ .ep_addr = ep_addr,
+ .result = result,
+ .len = xferred_bytes
+ }
+ };
- hcd_event_handler(&event, in_isr);
- }
+ hcd_event_handler(&event, in_isr);
}
void hcd_event_device_attach(uint8_t rhport, bool in_isr)
@@ -373,8 +379,6 @@ static bool parse_configuration_descriptor(uint8_t dev_addr, tusb_desc_configura
uint8_t const* p_desc = (uint8_t const*) desc_cfg;
p_desc = tu_desc_next(p_desc);
- TU_LOG2_MEM(desc_cfg, desc_cfg->wTotalLength, 0);
-
// parse each interfaces
while( p_desc < _usbh_ctrl_buf + desc_cfg->wTotalLength )
{
@@ -426,16 +430,226 @@ static bool parse_configuration_descriptor(uint8_t dev_addr, tusb_desc_configura
return true;
}
-bool enum_task(hcd_event_t* event)
+static bool enum_set_config_complete(uint8_t dev_addr, tusb_control_request_t const * request, xfer_result_t result)
+{
+ TU_LOG2_LOCATION();
+ TU_ASSERT(XFER_RESULT_SUCCESS == result);
+
+ TU_LOG2("Device configured\r\n");
+ usbh_device_t* dev = &_usbh_devices[dev_addr];
+ dev->state = TUSB_DEVICE_STATE_CONFIGURED;
+
+ // Parse configuration & set up drivers
+ parse_configuration_descriptor(dev_addr, (tusb_desc_configuration_t*) _usbh_ctrl_buf);
+
+ return true;
+}
+
+static bool enum_get_config_desc_complete(uint8_t dev_addr, tusb_control_request_t const * request, xfer_result_t result)
{
- enum {
- POWER_STABLE_DELAY = 100,
- RESET_DELAY = 500, // 200 USB specs say only 50ms but many devices require much longer
+ TU_ASSERT(XFER_RESULT_SUCCESS == result);
+
+ TU_LOG2("Set Configuration Descriptor\r\n");
+ tusb_control_request_t const new_request =
+ {
+ .bmRequestType_bit =
+ {
+ .recipient = TUSB_REQ_RCPT_DEVICE,
+ .type = TUSB_REQ_TYPE_STANDARD,
+ .direction = TUSB_DIR_OUT
+ },
+ .bRequest = TUSB_REQ_SET_CONFIGURATION,
+ .wValue = CONFIG_NUM,
+ .wIndex = 0,
+ .wLength = 0
};
+ TU_ASSERT( tuh_control_xfer(dev_addr, &new_request, NULL, enum_set_config_complete) );
+
+ return true;
+}
+
+static bool enum_get_9byte_config_desc_complete(uint8_t dev_addr, tusb_control_request_t const * request, xfer_result_t result)
+{
+ TU_ASSERT(XFER_RESULT_SUCCESS == result);
+
+ // TODO not enough buffer to hold configuration descriptor
+ tusb_desc_configuration_t const * desc_config = (tusb_desc_configuration_t const*) _usbh_ctrl_buf;
+ uint16_t total_len;
+
+ // Use offsetof to avoid pointer to the odd/misaligned address
+ memcpy(&total_len, (uint8_t*) desc_config + offsetof(tusb_desc_configuration_t, wTotalLength), 2);
+
+ TU_ASSERT(total_len <= CFG_TUSB_HOST_ENUM_BUFFER_SIZE);
+
+ //Get full configuration descriptor
+ tusb_control_request_t const new_request =
+ {
+ .bmRequestType_bit =
+ {
+ .recipient = TUSB_REQ_RCPT_DEVICE,
+ .type = TUSB_REQ_TYPE_STANDARD,
+ .direction = TUSB_DIR_IN
+ },
+ .bRequest = TUSB_REQ_GET_DESCRIPTOR,
+ .wValue = (TUSB_DESC_CONFIGURATION << 8) | (CONFIG_NUM - 1),
+ .wIndex = 0,
+ .wLength = total_len
+ };
+
+ TU_ASSERT( tuh_control_xfer(dev_addr, &new_request, _usbh_ctrl_buf, enum_get_config_desc_complete) );
+
+ return true;
+}
+
+static bool enum_get_device_desc_complete(uint8_t dev_addr, tusb_control_request_t const * request, xfer_result_t result)
+{
+ TU_ASSERT(XFER_RESULT_SUCCESS == result);
+
+ tusb_desc_device_t const * desc_device = (tusb_desc_device_t const*) _usbh_ctrl_buf;
+ usbh_device_t* dev = &_usbh_devices[dev_addr];
+
+ dev->vendor_id = desc_device->idVendor;
+ dev->product_id = desc_device->idProduct;
+
+// if (tuh_attach_cb) tuh_attach_cb((tusb_desc_device_t*) _usbh_ctrl_buf);
+
+ TU_LOG2("Get 9 bytes of Configuration Descriptor\r\n");
+ tusb_control_request_t const new_request =
+ {
+ .bmRequestType_bit =
+ {
+ .recipient = TUSB_REQ_RCPT_DEVICE,
+ .type = TUSB_REQ_TYPE_STANDARD,
+ .direction = TUSB_DIR_IN
+ },
+ .bRequest = TUSB_REQ_GET_DESCRIPTOR,
+ .wValue = (TUSB_DESC_CONFIGURATION << 8) | (CONFIG_NUM - 1),
+ .wIndex = 0,
+ .wLength = 9
+ };
+
+ TU_ASSERT( tuh_control_xfer(dev_addr, &new_request, _usbh_ctrl_buf, enum_get_9byte_config_desc_complete) );
+
+ return true;
+}
+
+// After SET_ADDRESS is complete
+static bool enum_set_address_complete(uint8_t dev_addr, tusb_control_request_t const * request, xfer_result_t result)
+{
+ TU_ASSERT(0 == dev_addr);
+ TU_ASSERT(XFER_RESULT_SUCCESS == result);
+
+ uint8_t const new_addr = (uint8_t const) request->wValue;
+
+ usbh_device_t* new_dev = &_usbh_devices[new_addr];
+ new_dev->addressed = 1;
+
+ // TODO close device 0, may not be needed
usbh_device_t* dev0 = &_usbh_devices[0];
- tusb_control_request_t request;
+ hcd_device_close(dev0->rhport, 0);
+ dev0->state = TUSB_DEVICE_STATE_UNPLUG;
+
+ // open control pipe for new address
+ TU_ASSERT ( usbh_pipe_control_open(new_addr, new_dev->ep0_packet_size) );
+
+ // Get full device descriptor
+ tusb_control_request_t const new_request =
+ {
+ .bmRequestType_bit =
+ {
+ .recipient = TUSB_REQ_RCPT_DEVICE,
+ .type = TUSB_REQ_TYPE_STANDARD,
+ .direction = TUSB_DIR_IN
+ },
+ .bRequest = TUSB_REQ_GET_DESCRIPTOR,
+ .wValue = TUSB_DESC_DEVICE << 8,
+ .wIndex = 0,
+ .wLength = sizeof(tusb_desc_device_t)
+ };
+ TU_ASSERT(tuh_control_xfer(new_addr, &new_request, _usbh_ctrl_buf, enum_get_device_desc_complete));
+
+ return true;
+}
+
+// After Get Device Descriptor of Address 0
+static bool enum_get_dev0_devic_desc_complete(uint8_t dev_addr, tusb_control_request_t const * request, xfer_result_t result)
+{
+ TU_ASSERT(0 == dev_addr);
+
+ usbh_device_t* dev0 = &_usbh_devices[0];
+
+ if (XFER_RESULT_SUCCESS != result)
+ {
+#if CFG_TUH_HUB
+ // TODO remove, waiting for next data on status pipe
+ if (dev0->hub_addr != 0) hub_status_pipe_queue( dev0->hub_addr);
+#endif
+
+ return false;
+ }
+
+ // Reset device again before Set Address
+ TU_LOG2("Port reset \r\n");
+
+ if (dev0->hub_addr == 0)
+ {
+ // connected directly to roothub
+ hcd_port_reset( dev0->rhport ); // reset port after 8 byte descriptor
+ osal_task_delay(RESET_DELAY);
+ }
+#if CFG_TUH_HUB
+ else
+ {
+ // FIXME hub_port_reset use usbh_control_xfer
+ if ( hub_port_reset(dev0->hub_addr, dev0->hub_port) )
+ {
+ osal_task_delay(RESET_DELAY);
+
+ // Acknowledge Port Reset Change if Reset Successful
+ hub_port_clear_feature(dev0->hub_addr, dev0->hub_port, HUB_FEATURE_PORT_RESET_CHANGE);
+ }
+
+ (void) hub_status_pipe_queue( dev0->hub_addr ); // done with hub, waiting for next data on status pipe
+ }
+#endif // CFG_TUH_HUB
+
+ // Set Address
+ TU_LOG2("Set Address \r\n");
+ uint8_t const new_addr = get_new_address();
+ TU_ASSERT(new_addr <= CFG_TUSB_HOST_DEVICE_MAX); // TODO notify application we reach max devices
+
+ usbh_device_t* new_dev = &_usbh_devices[new_addr];
+ new_dev->rhport = dev0->rhport;
+ new_dev->hub_addr = dev0->hub_addr;
+ new_dev->hub_port = dev0->hub_port;
+ new_dev->speed = dev0->speed;
+ new_dev->connected = 1;
+ new_dev->ep0_packet_size = ((tusb_desc_device_t*) _usbh_ctrl_buf)->bMaxPacketSize0;
+
+ tusb_control_request_t const new_request =
+ {
+ .bmRequestType_bit =
+ {
+ .recipient = TUSB_REQ_RCPT_DEVICE,
+ .type = TUSB_REQ_TYPE_STANDARD,
+ .direction = TUSB_DIR_OUT
+ },
+ .bRequest = TUSB_REQ_SET_ADDRESS,
+ .wValue = new_addr,
+ .wIndex = 0,
+ .wLength = 0
+ };
+
+ TU_ASSERT(tuh_control_xfer(0, &new_request, NULL, enum_set_address_complete));
+
+ return true;
+}
+
+bool enum_task(hcd_event_t* event)
+{
+ usbh_device_t* dev0 = &_usbh_devices[0];
dev0->rhport = event->rhport; // TODO refractor integrate to device_pool
dev0->hub_addr = event->connection.hub_addr;
dev0->hub_port = event->connection.hub_port;
@@ -445,14 +659,11 @@ bool enum_task(hcd_event_t* event)
if (dev0->hub_addr == 0)
{
// wait until device is stable. Increase this if the first 8 bytes is failed to get
- osal_task_delay(POWER_STABLE_DELAY);
+ osal_task_delay(RESET_DELAY);
// device unplugged while delaying
if ( !hcd_port_connect_status(dev0->rhport) ) return true;
- hcd_port_reset( dev0->rhport ); // port must be reset to have correct speed operation
- osal_task_delay(RESET_DELAY);
-
dev0->speed = hcd_port_speed_get( dev0->rhport );
}
#if CFG_TUH_HUB
@@ -460,8 +671,9 @@ bool enum_task(hcd_event_t* event)
else
{
// TODO wait for PORT reset change instead
- osal_task_delay(POWER_STABLE_DELAY);
+ osal_task_delay(RESET_DELAY);
+ // FIXME hub API use usbh_control_xfer
hub_port_status_response_t port_status;
TU_VERIFY_HDLR( hub_port_get_status(dev0->hub_addr, dev0->hub_port, &port_status), hub_status_pipe_queue( dev0->hub_addr) );
@@ -479,19 +691,27 @@ bool enum_task(hcd_event_t* event)
}
#endif // CFG_TUH_HUB
+ // TODO probably doesn't need to open/close each enumeration
TU_ASSERT( usbh_pipe_control_open(0, 8) );
//------------- Get first 8 bytes of device descriptor to get Control Endpoint Size -------------//
TU_LOG2("Get 8 byte of Device Descriptor\r\n");
- request = (tusb_control_request_t ) {
- .bmRequestType_bit = { .recipient = TUSB_REQ_RCPT_DEVICE, .type = TUSB_REQ_TYPE_STANDARD, .direction = TUSB_DIR_IN },
- .bRequest = TUSB_REQ_GET_DESCRIPTOR,
- .wValue = TUSB_DESC_DEVICE << 8,
- .wIndex = 0,
- .wLength = 8
+ tusb_control_request_t const request =
+ {
+ .bmRequestType_bit =
+ {
+ .recipient = TUSB_REQ_RCPT_DEVICE,
+ .type = TUSB_REQ_TYPE_STANDARD,
+ .direction = TUSB_DIR_IN
+ },
+ .bRequest = TUSB_REQ_GET_DESCRIPTOR,
+ .wValue = TUSB_DESC_DEVICE << 8,
+ .wIndex = 0,
+ .wLength = 8
};
- bool is_ok = usbh_control_xfer(0, &request, _usbh_ctrl_buf);
+ TU_ASSERT(tuh_control_xfer(0, &request, _usbh_ctrl_buf, enum_get_dev0_devic_desc_complete));
+#if 0
//------------- Reset device again before Set Address -------------//
TU_LOG2("Port reset \r\n");
@@ -526,6 +746,14 @@ bool enum_task(hcd_event_t* event)
uint8_t const new_addr = get_new_address();
TU_ASSERT(new_addr <= CFG_TUSB_HOST_DEVICE_MAX); // TODO notify application we reach max devices
+ usbh_device_t* new_dev = &_usbh_devices[new_addr];
+ new_dev->rhport = dev0->rhport;
+ new_dev->hub_addr = dev0->hub_addr;
+ new_dev->hub_port = dev0->hub_port;
+ new_dev->speed = dev0->speed;
+ new_dev->connected = 1;
+ new_dev->ep0_packet_size = ((tusb_desc_device_t*) _usbh_ctrl_buf)->bMaxPacketSize0;
+
request = (tusb_control_request_t ) {
.bmRequestType_bit = { .recipient = TUSB_REQ_RCPT_DEVICE, .type = TUSB_REQ_TYPE_STANDARD, .direction = TUSB_DIR_OUT },
.bRequest = TUSB_REQ_SET_ADDRESS,
@@ -533,7 +761,7 @@ bool enum_task(hcd_event_t* event)
.wIndex = 0,
.wLength = 0
};
- TU_ASSERT(usbh_control_xfer(0, &request, NULL));
+ TU_ASSERT(tuh_control_xfer(0, &request, NULL, enum_set_address_complete));
//------------- update port info & close control pipe of addr0 -------------//
usbh_device_t* new_dev = &_usbh_devices[new_addr];
@@ -613,6 +841,7 @@ bool enum_task(hcd_event_t* event)
// Invoke callback if available
if (tuh_mount_cb) tuh_mount_cb(new_addr);
+#endif
return true;
}
@@ -679,7 +908,7 @@ void tuh_task(void)
if ( 0 == epnum )
{
- // TODO control transfer
+ usbh_control_xfer_cb(event.dev_addr, ep_addr, event.xfer_complete.result, event.xfer_complete.len);
}else
{
uint8_t drv_id = dev->ep2drv[epnum][ep_dir];
diff --git a/src/host/usbh.h b/src/host/usbh.h
index 91afb90a3..814770722 100644
--- a/src/host/usbh.h
+++ b/src/host/usbh.h
@@ -63,6 +63,8 @@ typedef struct {
bool (* const xfer_cb) (uint8_t dev_addr, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes);
void (* const close) (uint8_t);
} usbh_class_driver_t;
+
+typedef bool (*tuh_control_complete_cb_t)(uint8_t dev_addr, tusb_control_request_t const * request, xfer_result_t result);
//--------------------------------------------------------------------+
// INTERNAL OBJECT & FUNCTION DECLARATION
//--------------------------------------------------------------------+
@@ -87,10 +89,12 @@ static inline bool tuh_device_is_configured(uint8_t dev_addr)
return tuh_device_get_state(dev_addr) == TUSB_DEVICE_STATE_CONFIGURED;
}
+bool tuh_control_xfer (uint8_t dev_addr, tusb_control_request_t const* request, void* buffer, tuh_control_complete_cb_t complete_cb);
+
//--------------------------------------------------------------------+
// APPLICATION CALLBACK
//--------------------------------------------------------------------+
-TU_ATTR_WEAK uint8_t tuh_attach_cb (tusb_desc_device_t const *desc_device);
+//TU_ATTR_WEAK uint8_t tuh_attach_cb (tusb_desc_device_t const *desc_device);
/** Callback invoked when device is mounted (configured) */
TU_ATTR_WEAK void tuh_mount_cb (uint8_t dev_addr);
@@ -107,9 +111,6 @@ bool usbh_edpt_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_endpoint_t const
bool usbh_edpt_xfer(uint8_t dev_addr, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes);
-
-bool tuh_control_xfer (uint8_t dev_addr, tusb_control_request_t const* request, uint8_t* buffer, uint16_t buflen);
-
#ifdef __cplusplus
}
#endif
diff --git a/src/host/usbh_control.c b/src/host/usbh_control.c
new file mode 100644
index 000000000..595c011f5
--- /dev/null
+++ b/src/host/usbh_control.c
@@ -0,0 +1,135 @@
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2020, Ha Thach (tinyusb.org)
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ * This file is part of the TinyUSB stack.
+ */
+
+#include "tusb_option.h"
+
+#if TUSB_OPT_HOST_ENABLED
+
+#include "tusb.h"
+#include "usbh_hcd.h"
+
+enum
+{
+ STAGE_SETUP,
+ STAGE_DATA,
+ STAGE_ACK
+};
+
+typedef struct
+{
+ tusb_control_request_t request TU_ATTR_ALIGNED(4);
+
+ uint8_t stage;
+ uint8_t* buffer;
+ tuh_control_complete_cb_t complete_cb;
+} usbh_control_xfer_t;
+
+static usbh_control_xfer_t _ctrl_xfer;
+
+//CFG_TUSB_MEM_SECTION CFG_TUSB_MEM_ALIGN
+//static uint8_t _tuh_ctrl_buf[CFG_TUSB_HOST_ENUM_BUFFER_SIZE];
+
+//--------------------------------------------------------------------+
+// MACRO TYPEDEF CONSTANT ENUM DECLARATION
+//--------------------------------------------------------------------+
+
+bool tuh_control_xfer (uint8_t dev_addr, tusb_control_request_t const* request, void* buffer, tuh_control_complete_cb_t complete_cb)
+{
+ usbh_device_t* dev = &_usbh_devices[dev_addr];
+ const uint8_t rhport = dev->rhport;
+
+ _ctrl_xfer.request = (*request);
+ _ctrl_xfer.buffer = buffer;
+ _ctrl_xfer.stage = STAGE_SETUP;
+ _ctrl_xfer.complete_cb = complete_cb;
+
+ TU_LOG2("Control Setup: ");
+ TU_LOG2_VAR(request);
+ TU_LOG2("\r\n");
+
+ // Send setup packet
+ TU_ASSERT( hcd_setup_send(rhport, dev_addr, (uint8_t const*) &_ctrl_xfer.request) );
+
+ return true;
+}
+
+static void _xfer_complete(uint8_t dev_addr, xfer_result_t result)
+{
+ if (_ctrl_xfer.complete_cb) _ctrl_xfer.complete_cb(dev_addr, &_ctrl_xfer.request, result);
+}
+
+bool usbh_control_xfer_cb (uint8_t dev_addr, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes)
+{
+ usbh_device_t* dev = &_usbh_devices[dev_addr];
+ const uint8_t rhport = dev->rhport;
+
+ tusb_control_request_t const * request = &_ctrl_xfer.request;
+
+ if (XFER_RESULT_SUCCESS != result)
+ {
+ TU_LOG2("Control failed: result = %d\r\n", result);
+
+ // terminate transfer if any stage failed
+ _xfer_complete(dev_addr, result);
+ }else
+ {
+ switch(_ctrl_xfer.stage)
+ {
+ case STAGE_SETUP:
+ _ctrl_xfer.stage = STAGE_DATA;
+ if (request->wLength)
+ {
+ // Note: initial data toggle is always 1
+ hcd_edpt_xfer(rhport, dev_addr, tu_edpt_addr(0, request->bmRequestType_bit.direction), _ctrl_xfer.buffer, request->wLength);
+ return true;
+ }
+ __attribute__((fallthrough));
+
+ case STAGE_DATA:
+ _ctrl_xfer.stage = STAGE_ACK;
+
+ if (request->wLength)
+ {
+ TU_LOG2("Control data:\r\n");
+ TU_LOG2_MEM(_ctrl_xfer.buffer, request->wLength, 2);
+ }
+
+ // data toggle is always 1
+ hcd_edpt_xfer(rhport, dev_addr, tu_edpt_addr(0, 1-request->bmRequestType_bit.direction), NULL, 0);
+ break;
+
+ case STAGE_ACK:
+ _xfer_complete(dev_addr, result);
+ break;
+
+ default: return false;
+ }
+ }
+
+ return true;
+}
+
+#endif
diff --git a/src/host/usbh_hcd.h b/src/host/usbh_hcd.h
index 435af96bb..f203f3baa 100644
--- a/src/host/usbh_hcd.h
+++ b/src/host/usbh_hcd.h
@@ -53,11 +53,20 @@ typedef struct {
//------------- device descriptor -------------//
uint16_t vendor_id;
uint16_t product_id;
+ uint8_t ep0_packet_size;
//------------- configuration descriptor -------------//
- uint8_t interface_count; // bNumInterfaces alias
+ // uint8_t interface_count; // bNumInterfaces alias
//------------- device -------------//
+ struct TU_ATTR_PACKED
+ {
+ uint8_t connected : 1;
+ uint8_t addressed : 1;
+ uint8_t configured : 1;
+ uint8_t suspended : 1;
+ };
+
volatile uint8_t state; // device state, value from enum tusbh_device_state_t
//------------- control pipe -------------//