mirror of
https://github.com/hathach/tinyusb.git
synced 2025-04-02 13:20:20 +00:00
Merge pull request #2222 from rppicomidi/fix-2188
fix issue 2188: support usbh_app_driver_get_cb()
This commit is contained in:
commit
7537985c08
@ -89,6 +89,8 @@ Host Stack
|
|||||||
- Mass Storage Class (MSC)
|
- Mass Storage Class (MSC)
|
||||||
- Hub with multiple-level support
|
- Hub with multiple-level support
|
||||||
|
|
||||||
|
Similar to the Device Stack, if you have a special requirement, `usbh_app_driver_get_cb()` can be used to write your own class driver without modifying the stack.
|
||||||
|
|
||||||
TypeC PD Stack
|
TypeC PD Stack
|
||||||
==============
|
==============
|
||||||
|
|
||||||
|
@ -29,7 +29,7 @@
|
|||||||
#if (CFG_TUH_ENABLED && CFG_TUH_CDC)
|
#if (CFG_TUH_ENABLED && CFG_TUH_CDC)
|
||||||
|
|
||||||
#include "host/usbh.h"
|
#include "host/usbh.h"
|
||||||
#include "host/usbh_classdriver.h"
|
#include "host/usbh_pvt.h"
|
||||||
|
|
||||||
#include "cdc_host.h"
|
#include "cdc_host.h"
|
||||||
|
|
||||||
|
@ -29,7 +29,7 @@
|
|||||||
#if (CFG_TUH_ENABLED && CFG_TUH_HID)
|
#if (CFG_TUH_ENABLED && CFG_TUH_HID)
|
||||||
|
|
||||||
#include "host/usbh.h"
|
#include "host/usbh.h"
|
||||||
#include "host/usbh_classdriver.h"
|
#include "host/usbh_pvt.h"
|
||||||
|
|
||||||
#include "hid_host.h"
|
#include "hid_host.h"
|
||||||
|
|
||||||
|
@ -29,7 +29,7 @@
|
|||||||
#if CFG_TUH_ENABLED && CFG_TUH_MSC
|
#if CFG_TUH_ENABLED && CFG_TUH_MSC
|
||||||
|
|
||||||
#include "host/usbh.h"
|
#include "host/usbh.h"
|
||||||
#include "host/usbh_classdriver.h"
|
#include "host/usbh_pvt.h"
|
||||||
|
|
||||||
#include "msc_host.h"
|
#include "msc_host.h"
|
||||||
|
|
||||||
|
@ -238,34 +238,25 @@ enum { BUILTIN_DRIVER_COUNT = TU_ARRAY_SIZE(_usbd_driver) };
|
|||||||
tu_static usbd_class_driver_t const * _app_driver = NULL;
|
tu_static usbd_class_driver_t const * _app_driver = NULL;
|
||||||
tu_static uint8_t _app_driver_count = 0;
|
tu_static uint8_t _app_driver_count = 0;
|
||||||
|
|
||||||
|
#define TOTAL_DRIVER_COUNT (_app_driver_count + BUILTIN_DRIVER_COUNT)
|
||||||
|
|
||||||
// virtually joins built-in and application drivers together.
|
// virtually joins built-in and application drivers together.
|
||||||
// Application is positioned first to allow overwriting built-in ones.
|
// Application is positioned first to allow overwriting built-in ones.
|
||||||
static inline usbd_class_driver_t const * get_driver(uint8_t drvid)
|
static inline usbd_class_driver_t const * get_driver(uint8_t drvid)
|
||||||
{
|
{
|
||||||
// Application drivers
|
usbd_class_driver_t const * driver = NULL;
|
||||||
if ( usbd_app_driver_get_cb )
|
|
||||||
{
|
if ( drvid < _app_driver_count ) {
|
||||||
if ( drvid < _app_driver_count ) return &_app_driver[drvid];
|
// Application drivers
|
||||||
drvid -= _app_driver_count;
|
driver = &_app_driver[drvid];
|
||||||
|
} else if ( drvid < TOTAL_DRIVER_COUNT && BUILTIN_DRIVER_COUNT > 0 ){
|
||||||
|
driver = &_usbd_driver[drvid - _app_driver_count];
|
||||||
}
|
}
|
||||||
|
|
||||||
// when there is no built-in drivers BUILTIN_DRIVER_COUNT = 0 will cause -Wtype-limits warning
|
return driver;
|
||||||
#ifdef __GNUC__
|
|
||||||
#pragma GCC diagnostic push
|
|
||||||
#pragma GCC diagnostic ignored "-Wtype-limits"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// Built-in drivers
|
|
||||||
if (drvid < BUILTIN_DRIVER_COUNT) return &_usbd_driver[drvid];
|
|
||||||
|
|
||||||
#ifdef __GNUC__
|
|
||||||
#pragma GCC diagnostic pop
|
|
||||||
#endif
|
|
||||||
|
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#define TOTAL_DRIVER_COUNT (_app_driver_count + BUILTIN_DRIVER_COUNT)
|
|
||||||
|
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
// DCD Event
|
// DCD Event
|
||||||
|
@ -59,7 +59,7 @@ typedef struct
|
|||||||
} usbd_class_driver_t;
|
} usbd_class_driver_t;
|
||||||
|
|
||||||
// Invoked when initializing device stack to get additional class drivers.
|
// Invoked when initializing device stack to get additional class drivers.
|
||||||
// Can optionally implemented by application to extend/overwrite class driver support.
|
// Can be implemented by application to extend/overwrite class driver support.
|
||||||
// Note: The drivers array must be accessible at all time when stack is active
|
// Note: The drivers array must be accessible at all time when stack is active
|
||||||
usbd_class_driver_t const* usbd_app_driver_get_cb(uint8_t* driver_count) TU_ATTR_WEAK;
|
usbd_class_driver_t const* usbd_app_driver_get_cb(uint8_t* driver_count) TU_ATTR_WEAK;
|
||||||
|
|
||||||
|
@ -30,7 +30,7 @@
|
|||||||
|
|
||||||
#include "hcd.h"
|
#include "hcd.h"
|
||||||
#include "usbh.h"
|
#include "usbh.h"
|
||||||
#include "usbh_classdriver.h"
|
#include "usbh_pvt.h"
|
||||||
#include "hub.h"
|
#include "hub.h"
|
||||||
|
|
||||||
// Debug level, TUSB_CFG_DEBUG must be at least this level for debug message
|
// Debug level, TUSB_CFG_DEBUG must be at least this level for debug message
|
||||||
|
@ -30,7 +30,7 @@
|
|||||||
|
|
||||||
#include "host/hcd.h"
|
#include "host/hcd.h"
|
||||||
#include "tusb.h"
|
#include "tusb.h"
|
||||||
#include "host/usbh_classdriver.h"
|
#include "host/usbh_pvt.h"
|
||||||
#include "hub.h"
|
#include "hub.h"
|
||||||
|
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
@ -180,9 +180,26 @@ static usbh_class_driver_t const usbh_class_drivers[] =
|
|||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
enum { USBH_CLASS_DRIVER_COUNT = TU_ARRAY_SIZE(usbh_class_drivers) };
|
enum { BUILTIN_DRIVER_COUNT = TU_ARRAY_SIZE(usbh_class_drivers) };
|
||||||
enum { CONFIG_NUM = 1 }; // default to use configuration 1
|
enum { CONFIG_NUM = 1 }; // default to use configuration 1
|
||||||
|
|
||||||
|
// Additional class drivers implemented by application
|
||||||
|
tu_static usbh_class_driver_t const * _app_driver = NULL;
|
||||||
|
tu_static uint8_t _app_driver_count = 0;
|
||||||
|
|
||||||
|
#define TOTAL_DRIVER_COUNT (_app_driver_count + BUILTIN_DRIVER_COUNT)
|
||||||
|
|
||||||
|
static inline usbh_class_driver_t const *get_driver(uint8_t drv_id) {
|
||||||
|
usbh_class_driver_t const *driver = NULL;
|
||||||
|
|
||||||
|
if ( drv_id < _app_driver_count ) {
|
||||||
|
driver = &_app_driver[drv_id];
|
||||||
|
} else if ( drv_id < TOTAL_DRIVER_COUNT && BUILTIN_DRIVER_COUNT > 0) {
|
||||||
|
driver = &usbh_class_drivers[drv_id - _app_driver_count];
|
||||||
|
}
|
||||||
|
|
||||||
|
return driver;
|
||||||
|
}
|
||||||
|
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
// INTERNAL OBJECT & FUNCTION DECLARATION
|
// INTERNAL OBJECT & FUNCTION DECLARATION
|
||||||
@ -340,6 +357,11 @@ bool tuh_init(uint8_t controller_id) {
|
|||||||
TU_ASSERT(_usbh_mutex);
|
TU_ASSERT(_usbh_mutex);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// Get application driver if available
|
||||||
|
if ( usbh_app_driver_get_cb ) {
|
||||||
|
_app_driver = usbh_app_driver_get_cb(&_app_driver_count);
|
||||||
|
}
|
||||||
|
|
||||||
// Device
|
// Device
|
||||||
tu_memclr(&_dev0, sizeof(_dev0));
|
tu_memclr(&_dev0, sizeof(_dev0));
|
||||||
tu_memclr(_usbh_devices, sizeof(_usbh_devices));
|
tu_memclr(_usbh_devices, sizeof(_usbh_devices));
|
||||||
@ -351,10 +373,14 @@ bool tuh_init(uint8_t controller_id) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Class drivers
|
// Class drivers
|
||||||
for (uint8_t drv_id = 0; drv_id < USBH_CLASS_DRIVER_COUNT; drv_id++)
|
for (uint8_t drv_id = 0; drv_id < TOTAL_DRIVER_COUNT; drv_id++)
|
||||||
{
|
{
|
||||||
TU_LOG_USBH("%s init\r\n", usbh_class_drivers[drv_id].name);
|
usbh_class_driver_t const * driver = get_driver(drv_id);
|
||||||
usbh_class_drivers[drv_id].init();
|
if ( driver )
|
||||||
|
{
|
||||||
|
TU_LOG_USBH("%s init\r\n", driver->name);
|
||||||
|
driver->init();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_usbh_controller = controller_id;;
|
_usbh_controller = controller_id;;
|
||||||
@ -482,12 +508,16 @@ void tuh_task_ext(uint32_t timeout_ms, bool in_isr) {
|
|||||||
}else
|
}else
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
uint8_t const drv_id = dev->ep2drv[epnum][ep_dir];
|
uint8_t drv_id = dev->ep2drv[epnum][ep_dir];
|
||||||
if ( drv_id < USBH_CLASS_DRIVER_COUNT ) {
|
usbh_class_driver_t const * driver = get_driver(drv_id);
|
||||||
TU_LOG_USBH("%s xfer callback\r\n", usbh_class_drivers[drv_id].name);
|
if ( driver )
|
||||||
usbh_class_drivers[drv_id].xfer_cb(event.dev_addr, ep_addr, (xfer_result_t) event.xfer_complete.result,
|
{
|
||||||
event.xfer_complete.len);
|
TU_LOG_USBH("%s xfer callback\r\n", driver->name);
|
||||||
} else {
|
driver->xfer_cb(event.dev_addr, ep_addr, (xfer_result_t) event.xfer_complete.result,
|
||||||
|
event.xfer_complete.len);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
// no driver/callback responsible for this transfer
|
// no driver/callback responsible for this transfer
|
||||||
TU_ASSERT(false,);
|
TU_ASSERT(false,);
|
||||||
}
|
}
|
||||||
@ -1183,7 +1213,8 @@ static void process_removing_device(uint8_t rhport, uint8_t hub_addr, uint8_t hu
|
|||||||
uint8_t nop_count = 0;
|
uint8_t nop_count = 0;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
for (uint8_t dev_id = 0; dev_id < TOTAL_DEVICES; dev_id++) {
|
for (uint8_t dev_id = 0; dev_id < TOTAL_DEVICES; dev_id++)
|
||||||
|
{
|
||||||
usbh_device_t *dev = &_usbh_devices[dev_id];
|
usbh_device_t *dev = &_usbh_devices[dev_id];
|
||||||
uint8_t const daddr = dev_id + 1;
|
uint8_t const daddr = dev_id + 1;
|
||||||
|
|
||||||
@ -1211,8 +1242,9 @@ static void process_removing_device(uint8_t rhport, uint8_t hub_addr, uint8_t hu
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Close class driver
|
// Close class driver
|
||||||
for (uint8_t drv_id = 0; drv_id < USBH_CLASS_DRIVER_COUNT; drv_id++) {
|
for (uint8_t drv_id = 0; drv_id < TOTAL_DRIVER_COUNT; drv_id++) {
|
||||||
usbh_class_drivers[drv_id].close(daddr);
|
usbh_class_driver_t const * driver = get_driver(drv_id);
|
||||||
|
if ( driver ) driver->close(daddr);
|
||||||
}
|
}
|
||||||
|
|
||||||
hcd_device_close(rhport, daddr);
|
hcd_device_close(rhport, daddr);
|
||||||
@ -1643,11 +1675,11 @@ static bool _parse_configuration_descriptor(uint8_t dev_addr, tusb_desc_configur
|
|||||||
TU_ASSERT(drv_len >= sizeof(tusb_desc_interface_t));
|
TU_ASSERT(drv_len >= sizeof(tusb_desc_interface_t));
|
||||||
|
|
||||||
// Find driver for this interface
|
// Find driver for this interface
|
||||||
for (uint8_t drv_id = 0; drv_id < USBH_CLASS_DRIVER_COUNT; drv_id++)
|
for (uint8_t drv_id = 0; drv_id < TOTAL_DRIVER_COUNT; drv_id++)
|
||||||
{
|
{
|
||||||
usbh_class_driver_t const * driver = &usbh_class_drivers[drv_id];
|
usbh_class_driver_t const * driver = get_driver(drv_id);
|
||||||
|
|
||||||
if ( driver->open(dev->rhport, dev_addr, desc_itf, drv_len) )
|
if (driver && driver->open(dev->rhport, dev_addr, desc_itf, drv_len) )
|
||||||
{
|
{
|
||||||
// open successfully
|
// open successfully
|
||||||
TU_LOG_USBH(" %s opened\r\n", driver->name);
|
TU_LOG_USBH(" %s opened\r\n", driver->name);
|
||||||
@ -1668,10 +1700,10 @@ static bool _parse_configuration_descriptor(uint8_t dev_addr, tusb_desc_configur
|
|||||||
break; // exit driver find loop
|
break; // exit driver find loop
|
||||||
}
|
}
|
||||||
|
|
||||||
if( drv_id >= USBH_CLASS_DRIVER_COUNT )
|
if ( drv_id == TOTAL_DRIVER_COUNT - 1 )
|
||||||
{
|
{
|
||||||
TU_LOG(CFG_TUH_LOG_LEVEL, "Interface %u: class = %u subclass = %u protocol = %u is not supported\r\n",
|
TU_LOG(CFG_TUH_LOG_LEVEL, "[%u:%u] Interface %u: class = %u subclass = %u protocol = %u is not supported\r\n",
|
||||||
desc_itf->bInterfaceNumber, desc_itf->bInterfaceClass, desc_itf->bInterfaceSubClass, desc_itf->bInterfaceProtocol);
|
dev->rhport, dev_addr, desc_itf->bInterfaceNumber, desc_itf->bInterfaceClass, desc_itf->bInterfaceSubClass, desc_itf->bInterfaceProtocol);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1692,9 +1724,9 @@ void usbh_driver_set_config_complete(uint8_t dev_addr, uint8_t itf_num)
|
|||||||
// IAD binding interface such as CDCs should return itf_num + 1 when complete
|
// IAD binding interface such as CDCs should return itf_num + 1 when complete
|
||||||
// with usbh_driver_set_config_complete()
|
// with usbh_driver_set_config_complete()
|
||||||
uint8_t const drv_id = dev->itf2drv[itf_num];
|
uint8_t const drv_id = dev->itf2drv[itf_num];
|
||||||
if (drv_id != TUSB_INDEX_INVALID_8)
|
usbh_class_driver_t const * driver = get_driver(drv_id);
|
||||||
|
if (driver)
|
||||||
{
|
{
|
||||||
usbh_class_driver_t const * driver = &usbh_class_drivers[drv_id];
|
|
||||||
TU_LOG_USBH("%s set config: itf = %u\r\n", driver->name, itf_num);
|
TU_LOG_USBH("%s set config: itf = %u\r\n", driver->name, itf_num);
|
||||||
driver->set_config(dev_addr, itf_num);
|
driver->set_config(dev_addr, itf_num);
|
||||||
break;
|
break;
|
||||||
|
@ -62,6 +62,11 @@ typedef struct {
|
|||||||
void (* const close )(uint8_t dev_addr);
|
void (* const close )(uint8_t dev_addr);
|
||||||
} usbh_class_driver_t;
|
} usbh_class_driver_t;
|
||||||
|
|
||||||
|
// Invoked when initializing host stack to get additional class drivers.
|
||||||
|
// Can be implemented by application to extend/overwrite class driver support.
|
||||||
|
// Note: The drivers array must be accessible at all time when stack is active
|
||||||
|
usbh_class_driver_t const* usbh_app_driver_get_cb(uint8_t* driver_count) TU_ATTR_WEAK;
|
||||||
|
|
||||||
// Call by class driver to tell USBH that it has complete the enumeration
|
// Call by class driver to tell USBH that it has complete the enumeration
|
||||||
void usbh_driver_set_config_complete(uint8_t dev_addr, uint8_t itf_num);
|
void usbh_driver_set_config_complete(uint8_t dev_addr, uint8_t itf_num);
|
||||||
|
|
@ -36,7 +36,7 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if CFG_TUH_ENABLED
|
#if CFG_TUH_ENABLED
|
||||||
#include "host/usbh_classdriver.h"
|
#include "host/usbh_pvt.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
//--------------------------------------------------------------------+
|
//--------------------------------------------------------------------+
|
||||||
|
Loading…
x
Reference in New Issue
Block a user