support class drivers implemented by application

This commit is contained in:
hathach 2020-04-29 10:49:58 +07:00
parent b52bc894f8
commit c14c0cfc9f
2 changed files with 88 additions and 51 deletions

View File

@ -79,21 +79,7 @@ enum { DRVID_INVALID = 0xFFu };
#define DRIVER_NAME(_name)
#endif
typedef struct
{
#if CFG_TUSB_DEBUG >= 2
char const* name;
#endif
void (* init ) (void);
void (* reset ) (uint8_t rhport);
bool (* open ) (uint8_t rhport, tusb_desc_interface_t const * desc_intf, uint16_t* p_length);
bool (* control_request ) (uint8_t rhport, tusb_control_request_t const * request);
bool (* control_complete ) (uint8_t rhport, tusb_control_request_t const * request);
bool (* xfer_cb ) (uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes);
void (* sof ) (uint8_t rhport); /* optional */
} usbd_class_driver_t;
// Built-in class drivers
static usbd_class_driver_t const _usbd_driver[] =
{
#if CFG_TUD_CDC
@ -203,6 +189,29 @@ static usbd_class_driver_t const _usbd_driver[] =
enum { USBD_CLASS_DRIVER_COUNT = TU_ARRAY_SIZE(_usbd_driver) };
// Additional class drivers implemented by application
static usbd_class_driver_t const * _app_driver = NULL;
static uint8_t _app_driver_count = 0;
// virtually joins built-in and application drivers together
// All drivers = built-in + application
static inline usbd_class_driver_t const * get_driver(uint8_t drvid)
{
// Built-in drivers
if (drvid < USBD_CLASS_DRIVER_COUNT) return &_usbd_driver[drvid];
// App drivers
if ( usbd_app_driver_get_cb )
{
drvid -= USBD_CLASS_DRIVER_COUNT;
if ( drvid < _app_driver_count ) return &_app_driver[drvid];
}
return NULL;
}
#define TOTAL_DRIVER_COUNT (USBD_CLASS_DRIVER_COUNT+_app_driver_count)
//--------------------------------------------------------------------+
// DCD Event
//--------------------------------------------------------------------+
@ -263,11 +272,12 @@ static char const* const _tusb_std_request_str[] =
// for usbd_control to print the name of control complete driver
void usbd_driver_print_control_complete_name(bool (*control_complete) (uint8_t, tusb_control_request_t const * ))
{
for (uint8_t i = 0; i < USBD_CLASS_DRIVER_COUNT; i++)
for (uint8_t i = 0; i < TOTAL_DRIVER_COUNT; i++)
{
if (_usbd_driver[i].control_complete == control_complete )
usbd_class_driver_t const * driver = get_driver(i);
if ( driver->control_complete == control_complete )
{
TU_LOG2(" %s control complete\r\n", _usbd_driver[i].name);
TU_LOG2(" %s control complete\r\n", driver->name);
return;
}
}
@ -309,11 +319,18 @@ bool tud_init (void)
_usbd_q = osal_queue_create(&_usbd_qdef);
TU_ASSERT(_usbd_q != NULL);
// Init class drivers
for (uint8_t i = 0; i < USBD_CLASS_DRIVER_COUNT; i++)
// Get application driver if available
if ( usbd_app_driver_get_cb )
{
TU_LOG2("%s init\r\n", _usbd_driver[i].name);
_usbd_driver[i].init();
_app_driver = usbd_app_driver_get_cb(&_app_driver_count);
}
// Init class drivers
for (uint8_t i = 0; i < TOTAL_DRIVER_COUNT; i++)
{
usbd_class_driver_t const * driver = get_driver(i);
TU_LOG2("%s init\r\n", driver->name);
driver->init();
}
// Init device controller driver
@ -333,9 +350,9 @@ static void usbd_reset(uint8_t rhport)
usbd_control_reset();
for (uint8_t i = 0; i < USBD_CLASS_DRIVER_COUNT; i++)
for ( uint8_t i = 0; i < TOTAL_DRIVER_COUNT; i++ )
{
if ( _usbd_driver[i].reset ) _usbd_driver[i].reset( rhport );
get_driver(i)->reset(rhport);
}
}
@ -420,11 +437,11 @@ void tud_task (void)
}
else
{
uint8_t const drv_id = _usbd_dev.ep2drv[epnum][ep_dir];
TU_ASSERT(drv_id < USBD_CLASS_DRIVER_COUNT,);
usbd_class_driver_t const * driver = get_driver( _usbd_dev.ep2drv[epnum][ep_dir] );
TU_ASSERT(driver, );
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);
TU_LOG2(" %s xfer callback\r\n", driver->name);
driver->xfer_cb(event.rhport, ep_addr, event.xfer_complete.result, event.xfer_complete.len);
}
}
break;
@ -438,12 +455,10 @@ void tud_task (void)
break;
case DCD_EVENT_SOF:
for ( uint8_t i = 0; i < USBD_CLASS_DRIVER_COUNT; i++ )
for ( uint8_t i = 0; i < TOTAL_DRIVER_COUNT; i++ )
{
if ( _usbd_driver[i].sof )
{
_usbd_driver[i].sof(event.rhport);
}
usbd_class_driver_t const * driver = get_driver(i);
if ( driver->sof ) driver->sof(event.rhport);
}
break;
@ -463,11 +478,11 @@ void tud_task (void)
//--------------------------------------------------------------------+
// Helper to invoke class driver control request handler
static bool invoke_class_control(uint8_t rhport, uint8_t drvid, tusb_control_request_t const * request)
static bool invoke_class_control(uint8_t rhport, usbd_class_driver_t const * driver, tusb_control_request_t const * request)
{
usbd_control_set_complete_callback(_usbd_driver[drvid].control_complete);
TU_LOG2(" %s control request\r\n", _usbd_driver[drvid].name);
return _usbd_driver[drvid].control_request(rhport, request);
usbd_control_set_complete_callback(driver->control_complete);
TU_LOG2(" %s control request\r\n", driver->name);
return driver->control_request(rhport, request);
}
// This handles the actual request and its response.
@ -580,12 +595,12 @@ 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));
uint8_t const drvid = _usbd_dev.itf2drv[itf];
TU_VERIFY(drvid < USBD_CLASS_DRIVER_COUNT);
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.
// notable requests are: GET HID REPORT DESCRIPTOR, SET_INTERFACE, GET_INTERFACE
if ( !invoke_class_control(rhport, drvid, p_request) )
if ( !invoke_class_control(rhport, driver, p_request) )
{
// For GET_INTERFACE, it is mandatory to respond even if the class
// driver doesn't use alternate settings.
@ -607,8 +622,6 @@ static bool process_control_request(uint8_t rhport, tusb_control_request_t const
TU_ASSERT(ep_num < TU_ARRAY_SIZE(_usbd_dev.ep2drv) );
uint8_t const drvid = _usbd_dev.ep2drv[ep_num][ep_dir];
bool ret = false;
// Handle STD request to endpoint
@ -647,16 +660,17 @@ static bool process_control_request(uint8_t rhport, tusb_control_request_t const
}
}
if (drvid < 0xFF) {
TU_ASSERT(drvid < USBD_CLASS_DRIVER_COUNT);
usbd_class_driver_t const * driver = get_driver(_usbd_dev.ep2drv[ep_num][ep_dir]);
if (driver)
{
// Some classes such as USBTMC needs to clear/re-init its buffer when receiving CLEAR_FEATURE request
// We will forward all request targeted endpoint to class drivers after
// - For class-type requests: driver is fully responsible to reply to host
// - For std-type requests : driver init/re-init internal variable/buffer only, and
// must not call tud_control_status(), driver's return value will have no effect.
// EP state has already affected (stalled/cleared)
if ( invoke_class_control(rhport, drvid, p_request) ) ret = true;
if ( invoke_class_control(rhport, driver, p_request) ) ret = true;
}
if ( TUSB_REQ_TYPE_STANDARD == p_request->bmRequestType_bit.type )
@ -708,17 +722,17 @@ static bool process_set_config(uint8_t rhport, uint8_t cfg_num)
uint8_t drv_id;
uint16_t drv_len;
for (drv_id = 0; drv_id < USBD_CLASS_DRIVER_COUNT; drv_id++)
for (drv_id = 0; drv_id < TOTAL_DRIVER_COUNT; drv_id++)
{
usbd_class_driver_t const *driver = &_usbd_driver[drv_id];
usbd_class_driver_t const *driver = get_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_ASSERT(DRVID_INVALID == _usbd_dev.itf2drv[desc_itf->bInterfaceNumber]);
TU_LOG2(" %s opened\r\n", _usbd_driver[drv_id].name);
TU_LOG2(" %s opened\r\n", driver->name);
_usbd_dev.itf2drv[desc_itf->bInterfaceNumber] = drv_id;
// If IAD exist, assign all interfaces to the same driver
@ -741,7 +755,7 @@ static bool process_set_config(uint8_t rhport, uint8_t cfg_num)
}
// Assert if cannot find supported driver
TU_ASSERT( drv_id < USBD_CLASS_DRIVER_COUNT && drv_len >= sizeof(tusb_desc_interface_t) );
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

View File

@ -33,6 +33,29 @@
extern "C" {
#endif
//--------------------------------------------------------------------+
// Class Drivers
//--------------------------------------------------------------------+
typedef struct
{
#if CFG_TUSB_DEBUG >= 2
char const* name;
#endif
void (* init ) (void);
void (* reset ) (uint8_t rhport);
bool (* open ) (uint8_t rhport, tusb_desc_interface_t const * desc_intf, uint16_t* p_length);
bool (* control_request ) (uint8_t rhport, tusb_control_request_t const * request);
bool (* control_complete ) (uint8_t rhport, tusb_control_request_t const * request);
bool (* xfer_cb ) (uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes);
void (* sof ) (uint8_t rhport); /* optional */
} usbd_class_driver_t;
// Invoked when initializing device stack to get additional class drivers.
// Can optionally implemented by application to extend/overwrite class driver support.
usbd_class_driver_t const* usbd_app_driver_get_cb(uint8_t* driver_count) TU_ATTR_WEAK;
//--------------------------------------------------------------------+
// USBD Endpoint API
//--------------------------------------------------------------------+