Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Host edpt xfer #1403

Merged
merged 22 commits into from
Mar 19, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
move result into transfer struct
  • Loading branch information
hathach committed Mar 17, 2022
commit 6df420f7f31b4fbf9b95987211e9ed9fef58ac00
6 changes: 2 additions & 4 deletions examples/host/bare_api/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,9 @@ static void utf16_to_utf8(uint16_t *temp_buf, size_t buf_len) {
((uint8_t*) temp_buf)[utf8_len] = '\0';
}

bool print_device_descriptor(uint8_t daddr, tuh_control_xfer_t const * xfer, xfer_result_t result)
bool print_device_descriptor(uint8_t daddr, tuh_control_xfer_t const * xfer)
{
(void) xfer;

if ( XFER_RESULT_SUCCESS != result )
if ( XFER_RESULT_SUCCESS != xfer->result )
{
printf("Failed to get device descriptor\r\n");
return false;
Expand Down
20 changes: 11 additions & 9 deletions src/class/hid/hid_host.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,13 @@ uint8_t tuh_hid_get_protocol(uint8_t dev_addr, uint8_t instance)
return hid_itf->protocol_mode;
}

static bool set_protocol_complete(uint8_t dev_addr, tuh_control_xfer_t const * xfer, xfer_result_t result)
static bool set_protocol_complete(uint8_t dev_addr, tuh_control_xfer_t const * xfer)
{
uint8_t const itf_num = (uint8_t) xfer->setup->wIndex;
uint8_t const instance = get_instance_id_by_itfnum(dev_addr, itf_num);
hidh_interface_t* hid_itf = get_instance(dev_addr, instance);

if (XFER_RESULT_SUCCESS == result) hid_itf->protocol_mode = (uint8_t) xfer->setup->wValue;
if (XFER_RESULT_SUCCESS == xfer->result) hid_itf->protocol_mode = (uint8_t) xfer->setup->wValue;

if (tuh_hid_set_protocol_complete_cb)
{
Expand Down Expand Up @@ -159,7 +159,7 @@ bool tuh_hid_set_protocol(uint8_t dev_addr, uint8_t instance, uint8_t protocol)
return _hidh_set_protocol(dev_addr, hid_itf->itf_num, protocol, set_protocol_complete, 0);
}

static bool set_report_complete(uint8_t dev_addr, tuh_control_xfer_t const * xfer, xfer_result_t result)
static bool set_report_complete(uint8_t dev_addr, tuh_control_xfer_t const * xfer)
{
TU_LOG2("HID Set Report complete\r\n");

Expand All @@ -171,7 +171,8 @@ static bool set_report_complete(uint8_t dev_addr, tuh_control_xfer_t const * xfe
uint8_t const report_type = tu_u16_high(xfer->setup->wValue);
uint8_t const report_id = tu_u16_low(xfer->setup->wValue);

tuh_hid_set_report_complete_cb(dev_addr, instance, report_id, report_type, (result == XFER_RESULT_SUCCESS) ? xfer->setup->wLength : 0);
tuh_hid_set_report_complete_cb(dev_addr, instance, report_id, report_type,
(xfer->result == XFER_RESULT_SUCCESS) ? xfer->setup->wLength : 0);
}

return true;
Expand Down Expand Up @@ -389,27 +390,28 @@ enum {
};

static void config_driver_mount_complete(uint8_t dev_addr, uint8_t instance, uint8_t const* desc_report, uint16_t desc_len);
static bool process_set_config(uint8_t dev_addr, tuh_control_xfer_t const * xfer, xfer_result_t result);
static bool process_set_config(uint8_t dev_addr, tuh_control_xfer_t const * xfer);

bool hidh_set_config(uint8_t dev_addr, uint8_t itf_num)
{
tusb_control_request_t request;
request.wIndex = tu_htole16((uint16_t) itf_num);

tuh_control_xfer_t xfer;
xfer.result = XFER_RESULT_SUCCESS;
xfer.setup = &request;
xfer.user_arg = CONFG_SET_IDLE;

// fake request to start the set config process
return process_set_config(dev_addr, &xfer, XFER_RESULT_SUCCESS);
// fake request to kick-off the set config process
return process_set_config(dev_addr, &xfer);
}

static bool process_set_config(uint8_t dev_addr, tuh_control_xfer_t const * xfer, xfer_result_t result)
static bool process_set_config(uint8_t dev_addr, tuh_control_xfer_t const * xfer)
{
// Stall is a valid response for SET_IDLE, therefore we could ignore its result
if ( xfer->setup->bRequest != HID_REQ_CONTROL_SET_IDLE )
{
TU_ASSERT(result == XFER_RESULT_SUCCESS);
TU_ASSERT(xfer->result == XFER_RESULT_SUCCESS);
}

uintptr_t const state = xfer->user_arg;
Expand Down
8 changes: 3 additions & 5 deletions src/class/msc/msc_host.c
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ bool msch_xfer_cb(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t event, uint32
// MSC Enumeration
//--------------------------------------------------------------------+

static bool config_get_maxlun_complete (uint8_t dev_addr, tuh_control_xfer_t const * xfer, xfer_result_t result);
static bool config_get_maxlun_complete (uint8_t dev_addr, tuh_control_xfer_t const * xfer);
static bool config_test_unit_ready_complete(uint8_t dev_addr, msc_cbw_t const* cbw, msc_csw_t const* csw);
static bool config_request_sense_complete(uint8_t dev_addr, msc_cbw_t const* cbw, msc_csw_t const* csw);
static bool config_read_capacity_complete(uint8_t dev_addr, msc_cbw_t const* cbw, msc_csw_t const* csw);
Expand Down Expand Up @@ -432,14 +432,12 @@ bool msch_set_config(uint8_t dev_addr, uint8_t itf_num)
return true;
}

static bool config_get_maxlun_complete (uint8_t dev_addr, tuh_control_xfer_t const * xfer, xfer_result_t result)
static bool config_get_maxlun_complete (uint8_t dev_addr, tuh_control_xfer_t const * xfer)
{
(void) xfer;

msch_interface_t* p_msc = get_itf(dev_addr);

// STALL means zero
p_msc->max_lun = (XFER_RESULT_SUCCESS == result) ? _msch_buffer[0] : 0;
p_msc->max_lun = (XFER_RESULT_SUCCESS == xfer->result) ? _msch_buffer[0] : 0;
p_msc->max_lun++; // MAX LUN is minus 1 by specs

// TODO multiple LUN support
Expand Down
31 changes: 15 additions & 16 deletions src/host/hub.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,8 @@ bool hub_edpt_status_xfer(uint8_t dev_addr)
// Set Configure
//--------------------------------------------------------------------+

static bool config_set_port_power (uint8_t dev_addr, tuh_control_xfer_t const * xfer, xfer_result_t result);
static bool config_port_power_complete (uint8_t dev_addr, tuh_control_xfer_t const * xfer, xfer_result_t result);
static bool config_set_port_power (uint8_t dev_addr, tuh_control_xfer_t const * xfer);
static bool config_port_power_complete (uint8_t dev_addr, tuh_control_xfer_t const * xfer);

bool hub_set_config(uint8_t dev_addr, uint8_t itf_num)
{
Expand Down Expand Up @@ -262,10 +262,9 @@ bool hub_set_config(uint8_t dev_addr, uint8_t itf_num)
return true;
}

static bool config_set_port_power (uint8_t dev_addr, tuh_control_xfer_t const * xfer, xfer_result_t result)
static bool config_set_port_power (uint8_t dev_addr, tuh_control_xfer_t const * xfer)
{
(void) xfer;
TU_ASSERT(XFER_RESULT_SUCCESS == result);
TU_ASSERT(XFER_RESULT_SUCCESS == xfer->result);

hub_interface_t* p_hub = get_itf(dev_addr);

Expand All @@ -280,9 +279,9 @@ static bool config_set_port_power (uint8_t dev_addr, tuh_control_xfer_t const *
return hub_port_set_feature(dev_addr, hub_port, HUB_FEATURE_PORT_POWER, config_port_power_complete, 0);
}

static bool config_port_power_complete (uint8_t dev_addr, tuh_control_xfer_t const * xfer, xfer_result_t result)
static bool config_port_power_complete (uint8_t dev_addr, tuh_control_xfer_t const * xfer)
{
TU_ASSERT(XFER_RESULT_SUCCESS == result);
TU_ASSERT(XFER_RESULT_SUCCESS == xfer->result);
hub_interface_t* p_hub = get_itf(dev_addr);

if (xfer->setup->wIndex == p_hub->port_count)
Expand All @@ -306,9 +305,9 @@ static bool config_port_power_complete (uint8_t dev_addr, tuh_control_xfer_t con
// Connection Changes
//--------------------------------------------------------------------+

static bool connection_get_status_complete (uint8_t dev_addr, tuh_control_xfer_t const * xfer, xfer_result_t result);
static bool connection_clear_conn_change_complete (uint8_t dev_addr, tuh_control_xfer_t const * xfer, xfer_result_t result);
static bool connection_port_reset_complete (uint8_t dev_addr, tuh_control_xfer_t const * xfer, xfer_result_t result);
static bool connection_get_status_complete (uint8_t dev_addr, tuh_control_xfer_t const * xfer);
static bool connection_clear_conn_change_complete (uint8_t dev_addr, tuh_control_xfer_t const * xfer);
static bool connection_port_reset_complete (uint8_t dev_addr, tuh_control_xfer_t const * xfer);

// callback as response of interrupt endpoint polling
bool hub_xfer_cb(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes)
Expand Down Expand Up @@ -336,9 +335,9 @@ bool hub_xfer_cb(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t result, uint32
return true;
}

static bool connection_get_status_complete (uint8_t dev_addr, tuh_control_xfer_t const * xfer, xfer_result_t result)
static bool connection_get_status_complete (uint8_t dev_addr, tuh_control_xfer_t const * xfer)
{
TU_ASSERT(result == XFER_RESULT_SUCCESS);
TU_ASSERT(xfer->result == XFER_RESULT_SUCCESS);

hub_interface_t* p_hub = get_itf(dev_addr);
uint8_t const port_num = (uint8_t) xfer->setup->wIndex;
Expand All @@ -364,9 +363,9 @@ static bool connection_get_status_complete (uint8_t dev_addr, tuh_control_xfer_t
return true;
}

static bool connection_clear_conn_change_complete (uint8_t dev_addr, tuh_control_xfer_t const * xfer, xfer_result_t result)
static bool connection_clear_conn_change_complete (uint8_t dev_addr, tuh_control_xfer_t const * xfer)
{
TU_ASSERT(result == XFER_RESULT_SUCCESS);
TU_ASSERT(xfer->result == XFER_RESULT_SUCCESS);

hub_interface_t* p_hub = get_itf(dev_addr);
uint8_t const port_num = (uint8_t) xfer->setup->wIndex;
Expand Down Expand Up @@ -395,9 +394,9 @@ static bool connection_clear_conn_change_complete (uint8_t dev_addr, tuh_control
return true;
}

static bool connection_port_reset_complete (uint8_t dev_addr, tuh_control_xfer_t const * xfer, xfer_result_t result)
static bool connection_port_reset_complete (uint8_t dev_addr, tuh_control_xfer_t const * xfer)
{
TU_ASSERT(result == XFER_RESULT_SUCCESS);
TU_ASSERT(xfer->result == XFER_RESULT_SUCCESS);

// hub_interface_t* p_hub = get_itf(dev_addr);
uint8_t const port_num = (uint8_t) xfer->setup->wIndex;
Expand Down
26 changes: 13 additions & 13 deletions src/host/usbh.c
Original file line number Diff line number Diff line change
Expand Up @@ -882,12 +882,12 @@ bool usbh_edpt_busy(uint8_t dev_addr, uint8_t ep_addr)
// Control transfer
//--------------------------------------------------------------------+

static bool _control_blocking_complete_cb(uint8_t daddr, tuh_control_xfer_t const * xfer, xfer_result_t result)
static bool _control_blocking_complete_cb(uint8_t daddr, tuh_control_xfer_t const * xfer)
{
(void) daddr;

// update result
*((xfer_result_t*) xfer->user_arg) = result;
*((xfer_result_t*) xfer->user_arg) = xfer->result;

return true;
}
Expand Down Expand Up @@ -924,19 +924,17 @@ bool tuh_control_xfer (uint8_t daddr, tuh_control_xfer_t const* xfer)
TU_ASSERT( hcd_setup_send(rhport, daddr, (uint8_t*) &_ctrl_xfer.request) );
}else
{
// user_arg must point to xfer_result_t to hold result
TU_VERIFY(xfer->user_arg);

// blocking if complete callback is not provided
// change callback to internal blocking, and result as user argument
volatile xfer_result_t* result = (volatile xfer_result_t*) xfer->user_arg;
volatile xfer_result_t result = XFER_RESULT_INVALID;

// use user_arg to point to xfer_result_t
_ctrl_xfer.user_arg = (uintptr_t) &result;
_ctrl_xfer.complete_cb = _control_blocking_complete_cb;
*result = XFER_RESULT_INVALID;

TU_ASSERT( hcd_setup_send(rhport, daddr, (uint8_t*) &_ctrl_xfer.request) );

while ((*result) == XFER_RESULT_INVALID)
while (result == XFER_RESULT_INVALID)
{
// only need to call task if not preempted RTOS
#if CFG_TUSB_OS == OPT_OS_NONE || CFG_TUSB_OS == OPT_OS_PICO
Expand Down Expand Up @@ -982,6 +980,7 @@ static void _xfer_complete(uint8_t dev_addr, xfer_result_t result)
tuh_control_xfer_t const xfer_temp =
{
.ep_addr = 0,
.result = result,
.setup = &request,
.actual_len = 0,
.buffer = _ctrl_xfer.buffer,
Expand All @@ -995,7 +994,7 @@ static void _xfer_complete(uint8_t dev_addr, xfer_result_t result)

if (xfer_temp.complete_cb)
{
xfer_temp.complete_cb(dev_addr, &xfer_temp, result);
xfer_temp.complete_cb(dev_addr, &xfer_temp);
}
}

Expand Down Expand Up @@ -1120,9 +1119,9 @@ static bool parse_configuration_descriptor (uint8_t dev_addr, tusb_desc_configur
static void enum_full_complete(void);

// process device enumeration
static bool process_enumeration(uint8_t dev_addr, tuh_control_xfer_t const * xfer, xfer_result_t result)
static bool process_enumeration(uint8_t dev_addr, tuh_control_xfer_t const * xfer)
{
if (XFER_RESULT_SUCCESS != result)
if (XFER_RESULT_SUCCESS != xfer->result)
{
// stop enumeration, maybe we could retry this
enum_full_complete();
Expand Down Expand Up @@ -1323,11 +1322,12 @@ static bool enum_new_device(hcd_event_t* event)
_dev0.speed = hcd_port_speed_get(_dev0.rhport );
TU_LOG2("%s Speed\r\n", tu_str_speed[_dev0.speed]);

// start the enumeration process
// fake transfer to kick-off the enumeration process
tuh_control_xfer_t xfer;
xfer.result = XFER_RESULT_SUCCESS;
xfer.user_arg = ENUM_ADDR0_DEVICE_DESC;

process_enumeration(0, &xfer, XFER_RESULT_SUCCESS);
process_enumeration(0, &xfer);

}
#if CFG_TUH_HUB
Expand Down
6 changes: 5 additions & 1 deletion src/host/usbh.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,19 @@
struct tuh_control_xfer_s;
typedef struct tuh_control_xfer_s tuh_control_xfer_t;

typedef bool (*tuh_control_xfer_cb_t)(uint8_t daddr, tuh_control_xfer_t const * xfer, xfer_result_t result);
typedef bool (*tuh_control_xfer_cb_t)(uint8_t daddr, tuh_control_xfer_t const * xfer);

struct tuh_control_xfer_s
{
uint8_t ep_addr;
xfer_result_t result;

tusb_control_request_t const* setup;
uint32_t actual_len;

uint8_t* buffer;


tuh_control_xfer_cb_t complete_cb;
uintptr_t user_arg;
};
Expand Down