mirror of
https://github.com/hathach/tinyusb.git
synced 2025-01-17 05:32:55 +08:00
support class drivers implemented by application
This commit is contained in:
parent
b52bc894f8
commit
c14c0cfc9f
@ -79,21 +79,7 @@ enum { DRVID_INVALID = 0xFFu };
|
|||||||
#define DRIVER_NAME(_name)
|
#define DRIVER_NAME(_name)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
typedef struct
|
// Built-in class drivers
|
||||||
{
|
|
||||||
#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;
|
|
||||||
|
|
||||||
static usbd_class_driver_t const _usbd_driver[] =
|
static usbd_class_driver_t const _usbd_driver[] =
|
||||||
{
|
{
|
||||||
#if CFG_TUD_CDC
|
#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) };
|
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
|
// 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
|
// 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 * ))
|
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;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -309,11 +319,18 @@ bool tud_init (void)
|
|||||||
_usbd_q = osal_queue_create(&_usbd_qdef);
|
_usbd_q = osal_queue_create(&_usbd_qdef);
|
||||||
TU_ASSERT(_usbd_q != NULL);
|
TU_ASSERT(_usbd_q != NULL);
|
||||||
|
|
||||||
// Init class drivers
|
// Get application driver if available
|
||||||
for (uint8_t i = 0; i < USBD_CLASS_DRIVER_COUNT; i++)
|
if ( usbd_app_driver_get_cb )
|
||||||
{
|
{
|
||||||
TU_LOG2("%s init\r\n", _usbd_driver[i].name);
|
_app_driver = usbd_app_driver_get_cb(&_app_driver_count);
|
||||||
_usbd_driver[i].init();
|
}
|
||||||
|
|
||||||
|
// 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
|
// Init device controller driver
|
||||||
@ -333,9 +350,9 @@ static void usbd_reset(uint8_t rhport)
|
|||||||
|
|
||||||
usbd_control_reset();
|
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
|
else
|
||||||
{
|
{
|
||||||
uint8_t const drv_id = _usbd_dev.ep2drv[epnum][ep_dir];
|
usbd_class_driver_t const * driver = get_driver( _usbd_dev.ep2drv[epnum][ep_dir] );
|
||||||
TU_ASSERT(drv_id < USBD_CLASS_DRIVER_COUNT,);
|
TU_ASSERT(driver, );
|
||||||
|
|
||||||
TU_LOG2(" %s xfer callback\r\n", _usbd_driver[drv_id].name);
|
TU_LOG2(" %s xfer callback\r\n", driver->name);
|
||||||
_usbd_driver[drv_id].xfer_cb(event.rhport, ep_addr, event.xfer_complete.result, event.xfer_complete.len);
|
driver->xfer_cb(event.rhport, ep_addr, event.xfer_complete.result, event.xfer_complete.len);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -438,12 +455,10 @@ void tud_task (void)
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case DCD_EVENT_SOF:
|
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_class_driver_t const * driver = get_driver(i);
|
||||||
{
|
if ( driver->sof ) driver->sof(event.rhport);
|
||||||
_usbd_driver[i].sof(event.rhport);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -463,11 +478,11 @@ void tud_task (void)
|
|||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
|
|
||||||
// Helper to invoke class driver control request handler
|
// 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);
|
usbd_control_set_complete_callback(driver->control_complete);
|
||||||
TU_LOG2(" %s control request\r\n", _usbd_driver[drvid].name);
|
TU_LOG2(" %s control request\r\n", driver->name);
|
||||||
return _usbd_driver[drvid].control_request(rhport, request);
|
return driver->control_request(rhport, request);
|
||||||
}
|
}
|
||||||
|
|
||||||
// This handles the actual request and its response.
|
// 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);
|
uint8_t const itf = tu_u16_low(p_request->wIndex);
|
||||||
TU_VERIFY(itf < TU_ARRAY_SIZE(_usbd_dev.itf2drv));
|
TU_VERIFY(itf < TU_ARRAY_SIZE(_usbd_dev.itf2drv));
|
||||||
|
|
||||||
uint8_t const drvid = _usbd_dev.itf2drv[itf];
|
usbd_class_driver_t const * driver = get_driver(_usbd_dev.itf2drv[itf]);
|
||||||
TU_VERIFY(drvid < USBD_CLASS_DRIVER_COUNT);
|
TU_VERIFY(driver);
|
||||||
|
|
||||||
// all requests to Interface (STD or Class) is forwarded to class driver.
|
// all requests to Interface (STD or Class) is forwarded to class driver.
|
||||||
// notable requests are: GET HID REPORT DESCRIPTOR, SET_INTERFACE, GET_INTERFACE
|
// 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
|
// For GET_INTERFACE, it is mandatory to respond even if the class
|
||||||
// driver doesn't use alternate settings.
|
// 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) );
|
TU_ASSERT(ep_num < TU_ARRAY_SIZE(_usbd_dev.ep2drv) );
|
||||||
|
|
||||||
uint8_t const drvid = _usbd_dev.ep2drv[ep_num][ep_dir];
|
|
||||||
|
|
||||||
bool ret = false;
|
bool ret = false;
|
||||||
|
|
||||||
// Handle STD request to endpoint
|
// 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) {
|
usbd_class_driver_t const * driver = get_driver(_usbd_dev.ep2drv[ep_num][ep_dir]);
|
||||||
TU_ASSERT(drvid < USBD_CLASS_DRIVER_COUNT);
|
|
||||||
|
|
||||||
|
if (driver)
|
||||||
|
{
|
||||||
// Some classes such as USBTMC needs to clear/re-init its buffer when receiving CLEAR_FEATURE request
|
// 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
|
// We will forward all request targeted endpoint to class drivers after
|
||||||
// - For class-type requests: driver is fully responsible to reply to host
|
// - 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
|
// - 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.
|
// must not call tud_control_status(), driver's return value will have no effect.
|
||||||
// EP state has already affected (stalled/cleared)
|
// 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 )
|
if ( TUSB_REQ_TYPE_STANDARD == p_request->bmRequestType_bit.type )
|
||||||
@ -708,9 +722,9 @@ static bool process_set_config(uint8_t rhport, uint8_t cfg_num)
|
|||||||
uint8_t drv_id;
|
uint8_t drv_id;
|
||||||
uint16_t drv_len;
|
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;
|
drv_len = 0;
|
||||||
if ( driver->open(rhport, desc_itf, &drv_len) )
|
if ( driver->open(rhport, desc_itf, &drv_len) )
|
||||||
@ -718,7 +732,7 @@ static bool process_set_config(uint8_t rhport, uint8_t cfg_num)
|
|||||||
// Interface number must not be used already
|
// 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;
|
_usbd_dev.itf2drv[desc_itf->bInterfaceNumber] = drv_id;
|
||||||
|
|
||||||
// If IAD exist, assign all interfaces to the same driver
|
// 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
|
// 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
|
mark_interface_endpoint(_usbd_dev.ep2drv, p_desc, drv_len, drv_id); // TODO refactor
|
||||||
|
|
||||||
|
@ -33,6 +33,29 @@
|
|||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#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
|
// USBD Endpoint API
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
|
Loading…
x
Reference in New Issue
Block a user