Merge branch 'master' into add-app-driver
This commit is contained in:
@@ -40,7 +40,8 @@
|
||||
//--------------------------------------------------------------------+
|
||||
// Device Data
|
||||
//--------------------------------------------------------------------+
|
||||
typedef struct {
|
||||
typedef struct
|
||||
{
|
||||
struct TU_ATTR_PACKED
|
||||
{
|
||||
volatile uint8_t connected : 1;
|
||||
@@ -53,6 +54,8 @@ typedef struct {
|
||||
uint8_t self_powered : 1; // configuration descriptor's attribute
|
||||
};
|
||||
|
||||
uint8_t speed;
|
||||
|
||||
uint8_t itf2drv[16]; // map interface number to driver (0xff is invalid)
|
||||
uint8_t ep2drv[8][2]; // map endpoint to driver ( 0xff is invalid )
|
||||
|
||||
@@ -185,6 +188,19 @@ static usbd_class_driver_t const _usbd_driver[] =
|
||||
.sof = NULL,
|
||||
},
|
||||
#endif
|
||||
|
||||
#if CFG_TUD_BTH
|
||||
{
|
||||
DRIVER_NAME("BTH")
|
||||
.init = btd_init,
|
||||
.reset = btd_reset,
|
||||
.open = btd_open,
|
||||
.control_request = btd_control_request,
|
||||
.control_complete = btd_control_complete,
|
||||
.xfer_cb = btd_xfer_cb,
|
||||
.sof = NULL
|
||||
},
|
||||
#endif
|
||||
};
|
||||
|
||||
enum { USBD_CLASS_DRIVER_COUNT = TU_ARRAY_SIZE(_usbd_driver) };
|
||||
@@ -288,6 +304,11 @@ void usbd_driver_print_control_complete_name(bool (*control_complete) (uint8_t,
|
||||
//--------------------------------------------------------------------+
|
||||
// Application API
|
||||
//--------------------------------------------------------------------+
|
||||
tusb_speed_t tud_speed_get(void)
|
||||
{
|
||||
return (tusb_speed_t) _usbd_dev.speed;
|
||||
}
|
||||
|
||||
bool tud_mounted(void)
|
||||
{
|
||||
return _usbd_dev.configured;
|
||||
@@ -335,7 +356,6 @@ bool tud_init (void)
|
||||
|
||||
// Init device controller driver
|
||||
dcd_init(TUD_OPT_RHPORT);
|
||||
tud_connect();
|
||||
dcd_int_enable(TUD_OPT_RHPORT);
|
||||
|
||||
return true;
|
||||
@@ -356,6 +376,14 @@ static void usbd_reset(uint8_t rhport)
|
||||
}
|
||||
}
|
||||
|
||||
bool tud_task_event_ready(void)
|
||||
{
|
||||
// Skip if stack is not initialized
|
||||
if ( !tusb_inited() ) return false;
|
||||
|
||||
return !osal_queue_empty(_usbd_q);
|
||||
}
|
||||
|
||||
/* USB Device Driver task
|
||||
* This top level thread manages all device controller event and delegates events to class-specific drivers.
|
||||
* This should be called periodically within the mainloop or rtos thread.
|
||||
@@ -386,16 +414,21 @@ void tud_task (void)
|
||||
|
||||
if ( !osal_queue_receive(_usbd_q, &event) ) return;
|
||||
|
||||
TU_LOG2("USBD %s", event.event_id < DCD_EVENT_COUNT ? _usbd_event_str[event.event_id] : "CORRUPTED");
|
||||
TU_LOG2("%s", (event.event_id != DCD_EVENT_XFER_COMPLETE && event.event_id != DCD_EVENT_SETUP_RECEIVED) ? "\r\n" : " ");
|
||||
#if CFG_TUSB_DEBUG >= 2
|
||||
if (event.event_id == DCD_EVENT_SETUP_RECEIVED) TU_LOG2("\r\n"); // extra line for setup
|
||||
TU_LOG2("USBD %s ", event.event_id < DCD_EVENT_COUNT ? _usbd_event_str[event.event_id] : "CORRUPTED");
|
||||
#endif
|
||||
|
||||
switch ( event.event_id )
|
||||
{
|
||||
case DCD_EVENT_BUS_RESET:
|
||||
TU_LOG2("\r\n");
|
||||
usbd_reset(event.rhport);
|
||||
_usbd_dev.speed = event.bus_reset.speed;
|
||||
break;
|
||||
|
||||
case DCD_EVENT_UNPLUGGED:
|
||||
TU_LOG2("\r\n");
|
||||
usbd_reset(event.rhport);
|
||||
|
||||
// invoke callback
|
||||
@@ -433,7 +466,7 @@ void tud_task (void)
|
||||
|
||||
if ( 0 == epnum )
|
||||
{
|
||||
usbd_control_xfer_cb(event.rhport, ep_addr, event.xfer_complete.result, event.xfer_complete.len);
|
||||
usbd_control_xfer_cb(event.rhport, ep_addr, (xfer_result_t)event.xfer_complete.result, event.xfer_complete.len);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -447,14 +480,17 @@ void tud_task (void)
|
||||
break;
|
||||
|
||||
case DCD_EVENT_SUSPEND:
|
||||
TU_LOG2("\r\n");
|
||||
if (tud_suspend_cb) tud_suspend_cb(_usbd_dev.remote_wakeup_en);
|
||||
break;
|
||||
|
||||
case DCD_EVENT_RESUME:
|
||||
TU_LOG2("\r\n");
|
||||
if (tud_resume_cb) tud_resume_cb();
|
||||
break;
|
||||
|
||||
case DCD_EVENT_SOF:
|
||||
TU_LOG2("\r\n");
|
||||
for ( uint8_t i = 0; i < TOTAL_DRIVER_COUNT; i++ )
|
||||
{
|
||||
usbd_class_driver_t const * driver = get_driver(i);
|
||||
@@ -463,6 +499,7 @@ void tud_task (void)
|
||||
break;
|
||||
|
||||
case USBD_EVENT_FUNC_CALL:
|
||||
TU_LOG2("\r\n");
|
||||
if ( event.func_call.func ) event.func_call.func(event.func_call.param);
|
||||
break;
|
||||
|
||||
@@ -514,6 +551,17 @@ static bool process_control_request(uint8_t rhport, tusb_control_request_t const
|
||||
{
|
||||
//------------- Device Requests e.g in enumeration -------------//
|
||||
case TUSB_REQ_RCPT_DEVICE:
|
||||
if ( TUSB_REQ_TYPE_CLASS == p_request->bmRequestType_bit.type )
|
||||
{
|
||||
uint8_t const itf = tu_u16_low(p_request->wIndex);
|
||||
TU_VERIFY(itf < TU_ARRAY_SIZE(_usbd_dev.itf2drv));
|
||||
|
||||
usbd_class_driver_t const * driver = get_driver(_usbd_dev.itf2drv[itf]);
|
||||
TU_VERIFY(driver);
|
||||
|
||||
// forward to class driver: "non-STD request to Interface"
|
||||
return invoke_class_control(rhport, driver, p_request);
|
||||
}
|
||||
if ( TUSB_REQ_TYPE_STANDARD != p_request->bmRequestType_bit.type )
|
||||
{
|
||||
// Non standard request is not supported
|
||||
@@ -595,7 +643,7 @@ static bool process_control_request(uint8_t rhport, tusb_control_request_t const
|
||||
uint8_t const itf = tu_u16_low(p_request->wIndex);
|
||||
TU_VERIFY(itf < TU_ARRAY_SIZE(_usbd_dev.itf2drv));
|
||||
|
||||
usbd_class_driver_t const * driver = get_driver(_usbd_dev.itf2drv[itf]);
|
||||
usbd_class_driver_t const * driver = get_driver(_usbd_dev.itf2drv[itf]);
|
||||
TU_VERIFY(driver);
|
||||
|
||||
// all requests to Interface (STD or Class) is forwarded to class driver.
|
||||
@@ -719,16 +767,20 @@ static bool process_set_config(uint8_t rhport, uint8_t cfg_num)
|
||||
TU_ASSERT( TUSB_DESC_INTERFACE == tu_desc_type(p_desc) );
|
||||
|
||||
tusb_desc_interface_t const * desc_itf = (tusb_desc_interface_t const*) p_desc;
|
||||
uint8_t drv_id;
|
||||
uint16_t drv_len;
|
||||
uint16_t const remaining_len = desc_end-p_desc;
|
||||
|
||||
uint8_t drv_id;
|
||||
for (drv_id = 0; drv_id < TOTAL_DRIVER_COUNT; drv_id++)
|
||||
{
|
||||
usbd_class_driver_t const *driver = get_driver(drv_id);
|
||||
|
||||
drv_len = 0;
|
||||
if ( driver->open(rhport, desc_itf, &drv_len) )
|
||||
uint16_t const drv_len = driver->open(rhport, desc_itf, remaining_len);
|
||||
|
||||
if ( drv_len > 0 )
|
||||
{
|
||||
// Open successfully, check if length is correct
|
||||
TU_ASSERT( sizeof(tusb_desc_interface_t) <= drv_len && drv_len <= remaining_len);
|
||||
|
||||
// Interface number must not be used already
|
||||
TU_ASSERT(DRVID_INVALID == _usbd_dev.itf2drv[desc_itf->bInterfaceNumber]);
|
||||
|
||||
@@ -738,11 +790,9 @@ static bool process_set_config(uint8_t rhport, uint8_t cfg_num)
|
||||
// If IAD exist, assign all interfaces to the same driver
|
||||
if (desc_itf_assoc)
|
||||
{
|
||||
// IAD's first interface number and class/subclass/protocol should match with opened interface
|
||||
TU_ASSERT(desc_itf_assoc->bFirstInterface == desc_itf->bInterfaceNumber &&
|
||||
desc_itf_assoc->bFunctionClass == desc_itf->bInterfaceClass &&
|
||||
desc_itf_assoc->bFunctionSubClass == desc_itf->bInterfaceSubClass &&
|
||||
desc_itf_assoc->bFunctionProtocol == desc_itf->bInterfaceProtocol);
|
||||
// IAD's first interface number and class should match with opened interface
|
||||
TU_ASSERT(desc_itf_assoc->bFirstInterface == desc_itf->bInterfaceNumber &&
|
||||
desc_itf_assoc->bFunctionClass == desc_itf->bInterfaceClass);
|
||||
|
||||
for(uint8_t i=1; i<desc_itf_assoc->bInterfaceCount; i++)
|
||||
{
|
||||
@@ -750,16 +800,16 @@ static bool process_set_config(uint8_t rhport, uint8_t cfg_num)
|
||||
}
|
||||
}
|
||||
|
||||
mark_interface_endpoint(_usbd_dev.ep2drv, p_desc, drv_len, drv_id); // TODO refactor
|
||||
|
||||
p_desc += drv_len; // next interface
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Assert if cannot find supported driver
|
||||
TU_ASSERT( drv_id < TOTAL_DRIVER_COUNT && drv_len >= sizeof(tusb_desc_interface_t) );
|
||||
|
||||
mark_interface_endpoint(_usbd_dev.ep2drv, p_desc, drv_len, drv_id); // TODO refactor
|
||||
|
||||
p_desc += drv_len; // next interface
|
||||
// Failed if cannot find supported driver
|
||||
TU_ASSERT(drv_id < TOTAL_DRIVER_COUNT);
|
||||
}
|
||||
|
||||
// invoke callback
|
||||
@@ -823,8 +873,10 @@ static bool process_get_descriptor(uint8_t rhport, tusb_control_request_t const
|
||||
if (!tud_descriptor_bos_cb) return false;
|
||||
|
||||
tusb_desc_bos_t const* desc_bos = (tusb_desc_bos_t const*) tud_descriptor_bos_cb();
|
||||
|
||||
uint16_t total_len;
|
||||
memcpy(&total_len, &desc_bos->wTotalLength, 2); // possibly mis-aligned memory
|
||||
// Use offsetof to avoid pointer to the odd/misaligned address
|
||||
memcpy(&total_len, (uint8_t*) desc_bos + offsetof(tusb_desc_bos_t, wTotalLength), 2);
|
||||
|
||||
return tud_control_xfer(rhport, p_request, (void*) desc_bos, total_len);
|
||||
}
|
||||
@@ -838,7 +890,8 @@ static bool process_get_descriptor(uint8_t rhport, tusb_control_request_t const
|
||||
TU_ASSERT(desc_config);
|
||||
|
||||
uint16_t total_len;
|
||||
memcpy(&total_len, &desc_config->wTotalLength, 2); // possibly mis-aligned memory
|
||||
// Use offsetof to avoid pointer to the odd/misaligned address
|
||||
memcpy(&total_len, (uint8_t*) desc_config + offsetof(tusb_desc_configuration_t, wTotalLength), 2);
|
||||
|
||||
return tud_control_xfer(rhport, p_request, (void*) desc_config, total_len);
|
||||
}
|
||||
@@ -848,34 +901,41 @@ static bool process_get_descriptor(uint8_t rhport, tusb_control_request_t const
|
||||
TU_LOG2(" String[%u]\r\n", desc_index);
|
||||
|
||||
// String Descriptor always uses the desc set from user
|
||||
if ( desc_index == 0xEE )
|
||||
{
|
||||
// The 0xEE index string is a Microsoft OS Descriptors.
|
||||
// https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
uint8_t const* desc_str = (uint8_t const*) tud_descriptor_string_cb(desc_index, p_request->wIndex);
|
||||
TU_ASSERT(desc_str);
|
||||
uint8_t const* desc_str = (uint8_t const*) tud_descriptor_string_cb(desc_index, p_request->wIndex);
|
||||
TU_VERIFY(desc_str);
|
||||
|
||||
// first byte of descriptor is its size
|
||||
return tud_control_xfer(rhport, p_request, (void*) desc_str, desc_str[0]);
|
||||
}
|
||||
// first byte of descriptor is its size
|
||||
return tud_control_xfer(rhport, p_request, (void*) desc_str, desc_str[0]);
|
||||
break;
|
||||
|
||||
case TUSB_DESC_DEVICE_QUALIFIER:
|
||||
TU_LOG2(" Device Qualifier\r\n");
|
||||
|
||||
// TODO If not highspeed capable stall this request otherwise
|
||||
// return the descriptor that could work in highspeed
|
||||
return false;
|
||||
// Host sends this request to ask why our device with USB BCD from 2.0
|
||||
// but is running at Full/Low Speed. If not highspeed capable stall this request,
|
||||
// otherwise return the descriptor that could work in highspeed mode
|
||||
if ( tud_descriptor_device_qualifier_cb )
|
||||
{
|
||||
uint8_t const* desc_qualifier = tud_descriptor_device_qualifier_cb();
|
||||
TU_ASSERT(desc_qualifier);
|
||||
|
||||
// first byte of descriptor is its size
|
||||
return tud_control_xfer(rhport, p_request, (void*) desc_qualifier, desc_qualifier[0]);
|
||||
}else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
|
||||
case TUSB_DESC_OTHER_SPEED_CONFIG:
|
||||
TU_LOG2(" Other Speed Configuration\r\n");
|
||||
|
||||
// After Device Qualifier descriptor is received host will ask for this descriptor
|
||||
return false; // not supported
|
||||
break;
|
||||
|
||||
default: return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
@@ -925,7 +985,14 @@ void dcd_event_handler(dcd_event_t const * event, bool in_isr)
|
||||
|
||||
void dcd_event_bus_signal (uint8_t rhport, dcd_eventid_t eid, bool in_isr)
|
||||
{
|
||||
dcd_event_t event = { .rhport = rhport, .event_id = eid, };
|
||||
dcd_event_t event = { .rhport = rhport, .event_id = eid };
|
||||
dcd_event_handler(&event, in_isr);
|
||||
}
|
||||
|
||||
void dcd_event_bus_reset (uint8_t rhport, tusb_speed_t speed, bool in_isr)
|
||||
{
|
||||
dcd_event_t event = { .rhport = rhport, .event_id = DCD_EVENT_BUS_RESET };
|
||||
event.bus_reset.speed = speed;
|
||||
dcd_event_handler(&event, in_isr);
|
||||
}
|
||||
|
||||
@@ -1009,12 +1076,21 @@ bool usbd_edpt_xfer(uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t
|
||||
|
||||
TU_LOG2(" Queue EP %02X with %u bytes ... ", ep_addr, total_bytes);
|
||||
|
||||
TU_VERIFY( dcd_edpt_xfer(rhport, ep_addr, buffer, total_bytes) );
|
||||
// Set busy first since the actual transfer can be complete before dcd_edpt_xfer() could return
|
||||
// and usbd task can preempt and clear the busy
|
||||
_usbd_dev.ep_status[epnum][dir].busy = true;
|
||||
|
||||
TU_LOG2("OK\r\n");
|
||||
|
||||
return true;
|
||||
if ( dcd_edpt_xfer(rhport, ep_addr, buffer, total_bytes) )
|
||||
{
|
||||
TU_LOG2("OK\r\n");
|
||||
return true;
|
||||
}else
|
||||
{
|
||||
_usbd_dev.ep_status[epnum][dir].busy = false;
|
||||
TU_LOG2("failed\r\n");
|
||||
TU_BREAKPOINT();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool usbd_edpt_busy(uint8_t rhport, uint8_t ep_addr)
|
||||
|
||||
Reference in New Issue
Block a user