Merge branch 'master' into remove-dcd-set-config

This commit is contained in:
hathach
2020-04-17 13:38:22 +07:00
118 changed files with 1010 additions and 669 deletions

View File

@@ -89,7 +89,7 @@ typedef struct TU_ATTR_ALIGNED(4)
void dcd_init (uint8_t rhport);
// Interrupt Handler
void dcd_irq_handler(uint8_t rhport) TU_ATTR_USED;
void dcd_int_handler(uint8_t rhport);
// Enable device interrupt
void dcd_int_enable (uint8_t rhport);
@@ -106,10 +106,10 @@ void dcd_set_config (uint8_t rhport, uint8_t config_num);
// Wake up host
void dcd_remote_wakeup(uint8_t rhport);
// Connect or disconnect D+/D- line pull-up resistor.
// Defined in dcd source if MCU has internal pull-up.
// Otherwise, may be defined in BSP.
// Connect by enabling internal pull-up resistor on D+/D-
void dcd_connect(uint8_t rhport) TU_ATTR_WEAK;
// Disconnect by disabling internal pull-up resistor on D+/D-
void dcd_disconnect(uint8_t rhport) TU_ATTR_WEAK;
//--------------------------------------------------------------------+
@@ -123,6 +123,10 @@ void dcd_edpt0_status_complete(uint8_t rhport, tusb_control_request_t const * re
// Configure endpoint's registers according to descriptor
bool dcd_edpt_open (uint8_t rhport, tusb_desc_endpoint_t const * p_endpoint_desc);
// Close an endpoint.
// Since it is weak, caller must TU_ASSERT this function's existence before calling it.
void dcd_edpt_close (uint8_t rhport, uint8_t ep_addr) TU_ATTR_WEAK;
// Submit a transfer, When complete dcd_event_xfer_complete() is invoked to notify the stack
bool dcd_edpt_xfer (uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes);

View File

@@ -73,8 +73,17 @@ enum { DRVID_INVALID = 0xFFu };
//--------------------------------------------------------------------+
// Class Driver
//--------------------------------------------------------------------+
typedef struct {
uint8_t class_code;
#if CFG_TUSB_DEBUG >= 2
#define DRIVER_NAME(_name) .name = _name,
#else
#define DRIVER_NAME(_name)
#endif
typedef struct
{
#if CFG_TUSB_DEBUG >= 2
char const* name;
#endif
void (* init ) (void);
void (* reset ) (uint8_t rhport);
@@ -89,7 +98,7 @@ static usbd_class_driver_t const _usbd_driver[] =
{
#if CFG_TUD_CDC
{
.class_code = TUSB_CLASS_CDC,
DRIVER_NAME("CDC")
.init = cdcd_init,
.reset = cdcd_reset,
.open = cdcd_open,
@@ -102,7 +111,7 @@ static usbd_class_driver_t const _usbd_driver[] =
#if CFG_TUD_MSC
{
.class_code = TUSB_CLASS_MSC,
DRIVER_NAME("MSC")
.init = mscd_init,
.reset = mscd_reset,
.open = mscd_open,
@@ -115,7 +124,7 @@ static usbd_class_driver_t const _usbd_driver[] =
#if CFG_TUD_HID
{
.class_code = TUSB_CLASS_HID,
DRIVER_NAME("HID")
.init = hidd_init,
.reset = hidd_reset,
.open = hidd_open,
@@ -128,7 +137,7 @@ static usbd_class_driver_t const _usbd_driver[] =
#if CFG_TUD_MIDI
{
.class_code = TUSB_CLASS_AUDIO,
DRIVER_NAME("MIDI")
.init = midid_init,
.open = midid_open,
.reset = midid_reset,
@@ -141,7 +150,7 @@ static usbd_class_driver_t const _usbd_driver[] =
#if CFG_TUD_VENDOR
{
.class_code = TUSB_CLASS_VENDOR_SPECIFIC,
DRIVER_NAME("VENDOR")
.init = vendord_init,
.reset = vendord_reset,
.open = vendord_open,
@@ -153,12 +162,8 @@ static usbd_class_driver_t const _usbd_driver[] =
#endif
#if CFG_TUD_USBTMC
// Presently USBTMC is the only defined class with the APP_SPECIFIC class code.
// We maybe need to add subclass codes here, or a callback to ask if a driver can
// handle a particular interface.
{
.class_code = TUD_USBTMC_APP_CLASS,
//.subclass_code = TUD_USBTMC_APP_SUBCLASS
DRIVER_NAME("TMC")
.init = usbtmcd_init_cb,
.reset = usbtmcd_reset_cb,
.open = usbtmcd_open_cb,
@@ -171,8 +176,7 @@ static usbd_class_driver_t const _usbd_driver[] =
#if CFG_TUD_DFU_RT
{
.class_code = TUD_DFU_APP_CLASS,
//.subclass_code = TUD_DFU_APP_SUBCLASS
DRIVER_NAME("DFU-RT")
.init = dfu_rtd_init,
.reset = dfu_rtd_reset,
.open = dfu_rtd_open,
@@ -185,19 +189,14 @@ static usbd_class_driver_t const _usbd_driver[] =
#if CFG_TUD_NET
{
.class_code =
#if CFG_TUD_NET == OPT_NET_RNDIS
TUD_RNDIS_ITF_CLASS,
#else
TUSB_CLASS_CDC,
#endif
DRIVER_NAME("NET")
.init = netd_init,
.reset = netd_reset,
.open = netd_open,
.control_request = netd_control_request,
.control_complete = netd_control_complete,
.xfer_cb = netd_xfer_cb,
.sof = NULL
.sof = NULL,
},
#endif
};
@@ -244,32 +243,6 @@ static char const* const _usbd_event_str[DCD_EVENT_COUNT] =
"FUNC_CALL"
};
// must be same driver order as usbd_class_drivers[]
static char const* const _usbd_driver_str[USBD_CLASS_DRIVER_COUNT] =
{
#if CFG_TUD_CDC
"CDC",
#endif
#if CFG_TUD_MSC
"MSC",
#endif
#if CFG_TUD_HID
"HID",
#endif
#if CFG_TUD_MIDI
"MIDI",
#endif
#if CFG_TUD_VENDOR
"Vendor",
#endif
#if CFG_TUD_USBTMC
"USBTMC"
#endif
#if CFG_TUD_NET
"NET"
#endif
};
static char const* const _tusb_std_request_str[] =
{
"Get Status" ,
@@ -326,7 +299,7 @@ bool tud_init (void)
// Init class drivers
for (uint8_t i = 0; i < USBD_CLASS_DRIVER_COUNT; i++)
{
TU_LOG2("%s init\r\n", _usbd_driver_str[i]);
TU_LOG2("%s init\r\n", _usbd_driver[i].name);
_usbd_driver[i].init();
}
@@ -435,7 +408,7 @@ void tud_task (void)
uint8_t const drv_id = _usbd_dev.ep2drv[epnum][ep_dir];
TU_ASSERT(drv_id < USBD_CLASS_DRIVER_COUNT,);
TU_LOG2(" %s xfer callback\r\n", _usbd_driver_str[drv_id]);
TU_LOG2(" %s xfer callback\r\n", _usbd_driver[drv_id].name);
_usbd_driver[drv_id].xfer_cb(event.rhport, ep_addr, event.xfer_complete.result, event.xfer_complete.len);
}
}
@@ -480,7 +453,7 @@ static bool invoke_class_control(uint8_t rhport, uint8_t drvid, tusb_control_req
TU_ASSERT(_usbd_driver[drvid].control_request);
usbd_control_set_complete_callback(_usbd_driver[drvid].control_complete);
TU_LOG2(" %s control request\r\n", _usbd_driver_str[drvid]);
TU_LOG2(" %s control request\r\n", _usbd_driver[drvid].name);
return _usbd_driver[drvid].control_request(rhport, request);
}
@@ -596,38 +569,17 @@ static bool process_control_request(uint8_t rhport, tusb_control_request_t const
uint8_t const drvid = _usbd_dev.itf2drv[itf];
TU_VERIFY(drvid < USBD_CLASS_DRIVER_COUNT);
if (p_request->bmRequestType_bit.type == TUSB_REQ_TYPE_STANDARD)
// all requests to Interface (STD or Class) is forwarded to class driver.
// notable requests are: GET HID REPORT DESCRIPTOR, SET_INTERFACE, GET_INTERFACE
if ( !invoke_class_control(rhport, drvid, p_request) )
{
switch ( p_request->bRequest )
{
case TUSB_REQ_GET_INTERFACE:
{
// TODO not support alternate interface yet
uint8_t alternate = 0;
tud_control_xfer(rhport, p_request, &alternate, 1);
}
break;
// For GET_INTERFACE, it is mandatory to respond even if the class
// driver doesn't use alternate settings.
TU_VERIFY( TUSB_REQ_TYPE_STANDARD == p_request->bmRequestType_bit.type &&
TUSB_REQ_GET_INTERFACE == p_request->bRequest);
case TUSB_REQ_SET_INTERFACE:
{
uint8_t const alternate = (uint8_t) p_request->wValue;
// TODO not support alternate interface yet
TU_ASSERT(alternate == 0);
tud_control_status(rhport, p_request);
}
break;
default:
// forward to class driver: "STD request to Interface"
// GET HID REPORT DESCRIPTOR falls into this case
TU_VERIFY(invoke_class_control(rhport, drvid, p_request));
break;
}
}else
{
// forward to class driver: "non-STD request to Interface"
TU_VERIFY(invoke_class_control(rhport, drvid, p_request));
uint8_t alternate = 0;
tud_control_xfer(rhport, p_request, &alternate, 1);
}
}
break;
@@ -727,37 +679,59 @@ static bool process_set_config(uint8_t rhport, uint8_t cfg_num)
while( p_desc < desc_end )
{
// Each interface always starts with Interface or Association descriptor
tusb_desc_interface_assoc_t const * desc_itf_assoc = NULL;
// Class will always starts with Interface Association (if any) and then Interface descriptor
if ( TUSB_DESC_INTERFACE_ASSOCIATION == tu_desc_type(p_desc) )
{
p_desc = tu_desc_next(p_desc); // ignore Interface Association
}else
{
TU_ASSERT( TUSB_DESC_INTERFACE == tu_desc_type(p_desc) );
tusb_desc_interface_t* desc_itf = (tusb_desc_interface_t*) p_desc;
// Check if class is supported
uint8_t drv_id;
for (drv_id = 0; drv_id < USBD_CLASS_DRIVER_COUNT; drv_id++)
{
if ( _usbd_driver[drv_id].class_code == desc_itf->bInterfaceClass ) break;
}
TU_ASSERT( drv_id < USBD_CLASS_DRIVER_COUNT );
// Interface number must not be used already TODO alternate interface
TU_ASSERT( DRVID_INVALID == _usbd_dev.itf2drv[desc_itf->bInterfaceNumber] );
_usbd_dev.itf2drv[desc_itf->bInterfaceNumber] = drv_id;
uint16_t itf_len=0;
TU_LOG2(" %s open\r\n", _usbd_driver_str[drv_id]);
TU_ASSERT( _usbd_driver[drv_id].open(rhport, desc_itf, &itf_len) );
TU_ASSERT( itf_len >= sizeof(tusb_desc_interface_t) );
mark_interface_endpoint(_usbd_dev.ep2drv, p_desc, itf_len, drv_id);
p_desc += itf_len; // next interface
desc_itf_assoc = (tusb_desc_interface_assoc_t const *) p_desc;
p_desc = tu_desc_next(p_desc); // next to Interface
}
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;
for (drv_id = 0; drv_id < USBD_CLASS_DRIVER_COUNT; drv_id++)
{
usbd_class_driver_t const *driver = &_usbd_driver[drv_id];
drv_len = 0;
if ( driver->open(rhport, desc_itf, &drv_len) )
{
// Interface number must not be used already
TU_ASSERT( DRVID_INVALID == _usbd_dev.itf2drv[desc_itf->bInterfaceNumber] );
TU_LOG2(" %s open\r\n", _usbd_driver[drv_id].name);
_usbd_dev.itf2drv[desc_itf->bInterfaceNumber] = drv_id;
// 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);
for(uint8_t i=1; i<desc_itf_assoc->bInterfaceCount; i++)
{
_usbd_dev.itf2drv[desc_itf->bInterfaceNumber+i] = drv_id;
}
}
break;
}
}
// Assert if cannot find supported driver
TU_ASSERT( drv_id < USBD_CLASS_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
}
// invoke callback
@@ -1035,4 +1009,20 @@ bool usbd_edpt_stalled(uint8_t rhport, uint8_t ep_addr)
return _usbd_dev.ep_status[epnum][dir].stalled;
}
/**
* usbd_edpt_close will disable an endpoint.
*
* In progress transfers on this EP may be delivered after this call.
*
*/
void usbd_edpt_close(uint8_t rhport, uint8_t ep_addr)
{
TU_ASSERT(dcd_edpt_close, /**/);
TU_LOG2(" CLOSING Endpoint: 0x%02X\r\n", ep_addr);
dcd_edpt_close(rhport, ep_addr);
return;
}
#endif

View File

@@ -48,7 +48,7 @@ bool tud_init (void);
void tud_task (void);
// Interrupt handler, name alias to DCD
#define tud_irq_handler dcd_irq_handler
#define tud_int_handler dcd_int_handler
// Check if device is connected and configured
bool tud_mounted(void);
@@ -183,9 +183,9 @@ TU_ATTR_WEAK bool tud_vendor_control_complete_cb(uint8_t rhport, tusb_control_re
// Interface number, string index, EP notification address and size, EP data address (out, in) and size.
#define TUD_CDC_DESCRIPTOR(_itfnum, _stridx, _ep_notif, _ep_notif_size, _epout, _epin, _epsize) \
/* Interface Associate */\
8, TUSB_DESC_INTERFACE_ASSOCIATION, _itfnum, 2, TUSB_CLASS_CDC, CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL, CDC_COMM_PROTOCOL_ATCOMMAND, 0,\
8, TUSB_DESC_INTERFACE_ASSOCIATION, _itfnum, 2, TUSB_CLASS_CDC, CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL, CDC_COMM_PROTOCOL_NONE, 0,\
/* CDC Control Interface */\
9, TUSB_DESC_INTERFACE, _itfnum, 0, 1, TUSB_CLASS_CDC, CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL, CDC_COMM_PROTOCOL_ATCOMMAND, _stridx,\
9, TUSB_DESC_INTERFACE, _itfnum, 0, 1, TUSB_CLASS_CDC, CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL, CDC_COMM_PROTOCOL_NONE, _stridx,\
/* CDC Header */\
5, TUSB_DESC_CS_INTERFACE, CDC_FUNC_DESC_HEADER, U16_TO_U8S_LE(0x0120),\
/* CDC Call */\
@@ -291,23 +291,23 @@ TU_ATTR_WEAK bool tud_vendor_control_complete_cb(uint8_t rhport, tusb_control_re
// Interface number, number of endpoints, EP string index, USB_TMC_PROTOCOL*, bulk-out endpoint ID,
// bulk-in endpoint ID
#define TUD_USBTMC_IF_DESCRIPTOR(_itfnum, _bNumEndpoints, _stridx, _itfProtocol) \
/* Interface */ \
/* Interface */ \
0x09, TUSB_DESC_INTERFACE, _itfnum, 0x00, _bNumEndpoints, TUD_USBTMC_APP_CLASS, TUD_USBTMC_APP_SUBCLASS, _itfProtocol, _stridx
#define TUD_USBTMC_IF_DESCRIPTOR_LEN 9u
#define TUD_USBTMC_BULK_DESCRIPTORS(_epout, _epin, _bulk_epsize) \
/* Endpoint Out */ \
7, TUSB_DESC_ENDPOINT, _epout, TUSB_XFER_BULK, U16_TO_U8S_LE(_bulk_epsize), 0u, \
/* Endpoint In */ \
7, TUSB_DESC_ENDPOINT, _epin, TUSB_XFER_BULK, U16_TO_U8S_LE(_bulk_epsize), 0u
/* Endpoint Out */ \
7, TUSB_DESC_ENDPOINT, _epout, TUSB_XFER_BULK, U16_TO_U8S_LE(_bulk_epsize), 0u, \
/* Endpoint In */ \
7, TUSB_DESC_ENDPOINT, _epin, TUSB_XFER_BULK, U16_TO_U8S_LE(_bulk_epsize), 0u
#define TUD_USBTMC_BULK_DESCRIPTORS_LEN (7u+7u)
/* optional interrupt endpoint */ \
// _int_pollingInterval : for LS/FS, expressed in frames (1ms each). 16 may be a good number?
#define TUD_USBTMC_INT_DESCRIPTOR(_ep_interrupt, _ep_interrupt_size, _int_pollingInterval ) \
7, TUSB_DESC_ENDPOINT, _ep_interrupt, TUSB_XFER_INTERRUPT, U16_TO_U8S_LE(_ep_interrupt_size), 0x16
7, TUSB_DESC_ENDPOINT, _ep_interrupt, TUSB_XFER_INTERRUPT, U16_TO_U8S_LE(_ep_interrupt_size), 0x16
#define TUD_USBTMC_INT_DESCRIPTOR_LEN (7u)
@@ -342,12 +342,14 @@ TU_ATTR_WEAK bool tud_vendor_control_complete_cb(uint8_t rhport, tusb_control_re
//------------- CDC-ECM -------------//
// Length of template descriptor: 62 bytes
#define TUD_CDC_ECM_DESC_LEN (9+5+5+13+7+9+7+7)
// Length of template descriptor: 71 bytes
#define TUD_CDC_ECM_DESC_LEN (8+9+5+5+13+7+9+9+7+7)
// CDC-ECM Descriptor Template
// Interface number, description string index, MAC address string index, EP notification address and size, EP data address (out, in), and size, max segment size.
#define TUD_CDC_ECM_DESCRIPTOR(_itfnum, _desc_stridx, _mac_stridx, _ep_notif, _ep_notif_size, _epout, _epin, _epsize, _maxsegmentsize) \
/* Interface Association */\
8, TUSB_DESC_INTERFACE_ASSOCIATION, _itfnum, 2, TUSB_CLASS_CDC, CDC_COMM_SUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL, 0, 0,\
/* CDC Control Interface */\
9, TUSB_DESC_INTERFACE, _itfnum, 0, 1, TUSB_CLASS_CDC, CDC_COMM_SUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL, 0, _desc_stridx,\
/* CDC-ECM Header */\
@@ -358,8 +360,10 @@ TU_ATTR_WEAK bool tud_vendor_control_complete_cb(uint8_t rhport, tusb_control_re
13, TUSB_DESC_CS_INTERFACE, CDC_FUNC_DESC_ETHERNET_NETWORKING, _mac_stridx, 0, 0, 0, 0, U16_TO_U8S_LE(_maxsegmentsize), U16_TO_U8S_LE(0), 0,\
/* Endpoint Notification */\
7, TUSB_DESC_ENDPOINT, _ep_notif, TUSB_XFER_INTERRUPT, U16_TO_U8S_LE(_ep_notif_size), 1,\
/* CDC Data Interface */\
9, TUSB_DESC_INTERFACE, (uint8_t)((_itfnum)+1), 0, 2, TUSB_CLASS_CDC_DATA, 0, 0, 0,\
/* CDC Data Interface (default inactive) */\
9, TUSB_DESC_INTERFACE, (uint8_t)((_itfnum)+1), 0, 0, TUSB_CLASS_CDC_DATA, 0, 0, 0,\
/* CDC Data Interface (alternative active) */\
9, TUSB_DESC_INTERFACE, (uint8_t)((_itfnum)+1), 1, 2, TUSB_CLASS_CDC_DATA, 0, 0, 0,\
/* Endpoint In */\
7, TUSB_DESC_ENDPOINT, _epin, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0,\
/* Endpoint Out */\
@@ -372,10 +376,10 @@ TU_ATTR_WEAK bool tud_vendor_control_complete_cb(uint8_t rhport, tusb_control_re
/* Windows XP */
#define TUD_RNDIS_ITF_CLASS TUSB_CLASS_CDC
#define TUD_RNDIS_ITF_SUBCLASS CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL
#define TUD_RNDIS_ITF_PROTOCOL CDC_COMM_PROTOCOL_MICROSOFT_RNDIS
#define TUD_RNDIS_ITF_PROTOCOL 0xFF /* CDC_COMM_PROTOCOL_MICROSOFT_RNDIS */
#else
/* Windows 7+ */
#define TUD_RNDIS_ITF_CLASS 0xE0
#define TUD_RNDIS_ITF_CLASS TUSB_CLASS_WIRELESS_CONTROLLER
#define TUD_RNDIS_ITF_SUBCLASS 0x01
#define TUD_RNDIS_ITF_PROTOCOL 0x03
#endif
@@ -408,22 +412,6 @@ TU_ATTR_WEAK bool tud_vendor_control_complete_cb(uint8_t rhport, tusb_control_re
7, TUSB_DESC_ENDPOINT, _epout, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0
//------------- CDC-EEM -------------//
// Length of template descriptor: 23 bytes
#define TUD_CDC_EEM_DESC_LEN (9+7+7)
// CDC-EEM Descriptor Template
// Interface number, description string index, EP data address (out, in) and size.
#define TUD_CDC_EEM_DESCRIPTOR(_itfnum, _stridx, _epout, _epin, _epsize) \
/* EEM Interface */\
9, TUSB_DESC_INTERFACE, _itfnum, 0, 2, TUSB_CLASS_CDC, CDC_COMM_SUBCLASS_ETHERNET_EMULATION_MODEL, CDC_COMM_PROTOCOL_ETHERNET_EMULATION_MODEL, _stridx,\
/* Endpoint In */\
7, TUSB_DESC_ENDPOINT, _epin, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0,\
/* Endpoint Out */\
7, TUSB_DESC_ENDPOINT, _epout, TUSB_XFER_BULK, U16_TO_U8S_LE(_epsize), 0
#ifdef __cplusplus
}
#endif

View File

@@ -38,6 +38,7 @@
//--------------------------------------------------------------------+
//bool usbd_edpt_open(uint8_t rhport, tusb_desc_endpoint_t const * p_endpoint_desc);
void usbd_edpt_close(uint8_t rhport, uint8_t ep_addr);
// Submit a usb transfer
bool usbd_edpt_xfer(uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes);