Skip to content

Commit 1b33a31

Browse files
committed
more minor clean up
- also rename usbh_classdriver.h to usbh_pvt.h to consitent with usbd
1 parent 9d94296 commit 1b33a31

File tree

9 files changed

+61
-79
lines changed

9 files changed

+61
-79
lines changed

src/class/cdc/cdc_host.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#if (CFG_TUH_ENABLED && CFG_TUH_CDC)
3030

3131
#include "host/usbh.h"
32-
#include "host/usbh_classdriver.h"
32+
#include "host/usbh_pvt.h"
3333

3434
#include "cdc_host.h"
3535

src/class/hid/hid_host.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#if (CFG_TUH_ENABLED && CFG_TUH_HID)
3030

3131
#include "host/usbh.h"
32-
#include "host/usbh_classdriver.h"
32+
#include "host/usbh_pvt.h"
3333

3434
#include "hid_host.h"
3535

src/class/msc/msc_host.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#if CFG_TUH_ENABLED && CFG_TUH_MSC
3030

3131
#include "host/usbh.h"
32-
#include "host/usbh_classdriver.h"
32+
#include "host/usbh_pvt.h"
3333

3434
#include "msc_host.h"
3535

src/device/usbd.c

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -238,34 +238,25 @@ enum { BUILTIN_DRIVER_COUNT = TU_ARRAY_SIZE(_usbd_driver) };
238238
tu_static usbd_class_driver_t const * _app_driver = NULL;
239239
tu_static uint8_t _app_driver_count = 0;
240240

241+
#define TOTAL_DRIVER_COUNT (_app_driver_count + BUILTIN_DRIVER_COUNT)
242+
241243
// virtually joins built-in and application drivers together.
242244
// Application is positioned first to allow overwriting built-in ones.
243245
static inline usbd_class_driver_t const * get_driver(uint8_t drvid)
244246
{
245-
// Application drivers
246-
if ( usbd_app_driver_get_cb )
247-
{
248-
if ( drvid < _app_driver_count ) return &_app_driver[drvid];
249-
drvid -= _app_driver_count;
250-
}
251-
252-
// when there is no built-in drivers BUILTIN_DRIVER_COUNT = 0 will cause -Wtype-limits warning
253-
#ifdef __GNUC__
254-
#pragma GCC diagnostic push
255-
#pragma GCC diagnostic ignored "-Wtype-limits"
256-
#endif
257-
258-
// Built-in drivers
259-
if (drvid < BUILTIN_DRIVER_COUNT) return &_usbd_driver[drvid];
247+
usbd_class_driver_t const * driver = NULL;
260248

261-
#ifdef __GNUC__
262-
#pragma GCC diagnostic pop
263-
#endif
249+
if ( drvid < _app_driver_count ) {
250+
// Application drivers
251+
driver = &_app_driver[drvid];
252+
} else if ( drvid < TOTAL_DRIVER_COUNT && BUILTIN_DRIVER_COUNT > 0 ){
253+
driver = &_usbd_driver[drvid - _app_driver_count];
254+
}
264255

265-
return NULL;
256+
return driver;
266257
}
267258

268-
#define TOTAL_DRIVER_COUNT (_app_driver_count + BUILTIN_DRIVER_COUNT)
259+
269260

270261
//--------------------------------------------------------------------+
271262
// DCD Event

src/device/usbd_pvt.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ typedef struct
5959
} usbd_class_driver_t;
6060

6161
// Invoked when initializing device stack to get additional class drivers.
62-
// Can optionally implemented by application to extend/overwrite class driver support.
62+
// Can be implemented by application to extend/overwrite class driver support.
6363
// Note: The drivers array must be accessible at all time when stack is active
6464
usbd_class_driver_t const* usbd_app_driver_get_cb(uint8_t* driver_count) TU_ATTR_WEAK;
6565

src/host/hub.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
#include "hcd.h"
3232
#include "usbh.h"
33-
#include "usbh_classdriver.h"
33+
#include "usbh_pvt.h"
3434
#include "hub.h"
3535

3636
// Debug level, TUSB_CFG_DEBUG must be at least this level for debug message

src/host/usbh.c

Lines changed: 39 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
#include "host/hcd.h"
3232
#include "tusb.h"
33-
#include "host/usbh_classdriver.h"
33+
#include "host/usbh_pvt.h"
3434
#include "hub.h"
3535

3636
//--------------------------------------------------------------------+
@@ -183,6 +183,23 @@ static usbh_class_driver_t const usbh_class_drivers[] =
183183
enum { USBH_BUILTIN_DRIVER_COUNT = TU_ARRAY_SIZE(usbh_class_drivers) };
184184
enum { CONFIG_NUM = 1 }; // default to use configuration 1
185185

186+
// Additional class drivers implemented by application
187+
tu_static usbh_class_driver_t const * _app_driver = NULL;
188+
tu_static uint8_t _app_driver_count = 0;
189+
190+
#define TOTAL_DRIVER_COUNT (_app_driver_count + USBH_BUILTIN_DRIVER_COUNT)
191+
192+
static inline usbh_class_driver_t const *get_driver(uint8_t drv_id) {
193+
usbh_class_driver_t const *driver = NULL;
194+
195+
if ( drv_id < _app_driver_count ) {
196+
driver = &_app_driver[drv_id];
197+
} else if ( drv_id < TOTAL_DRIVER_COUNT && USBH_BUILTIN_DRIVER_COUNT > 0) {
198+
driver = &usbh_class_drivers[drv_id - _app_driver_count];
199+
}
200+
201+
return driver;
202+
}
186203

187204
//--------------------------------------------------------------------+
188205
// INTERNAL OBJECT & FUNCTION DECLARATION
@@ -246,21 +263,6 @@ static void process_removing_device(uint8_t rhport, uint8_t hub_addr, uint8_t hu
246263
static bool usbh_edpt_control_open(uint8_t dev_addr, uint8_t max_packet_size);
247264
static bool usbh_control_xfer_cb (uint8_t daddr, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes);
248265

249-
// Additional class drivers implemented by application
250-
tu_static usbh_class_driver_t const * _app_driver = NULL;
251-
tu_static uint8_t _app_driver_count = 0;
252-
tu_static uint8_t _total_driver_count = USBH_BUILTIN_DRIVER_COUNT;
253-
254-
static usbh_class_driver_t const * usbh_get_driver(uint8_t drv_id)
255-
{
256-
usbh_class_driver_t const * driver = NULL;
257-
if ( drv_id < _app_driver_count )
258-
driver = &_app_driver[drv_id];
259-
else if ( drv_id < _total_driver_count )
260-
driver = &usbh_class_drivers[drv_id - _app_driver_count];
261-
return driver;
262-
}
263-
264266
#if CFG_TUSB_OS == OPT_OS_NONE
265267
// TODO rework time-related function later
266268
// weak and overridable
@@ -354,11 +356,10 @@ bool tuh_init(uint8_t controller_id) {
354356
_usbh_mutex = osal_mutex_create(&_usbh_mutexdef);
355357
TU_ASSERT(_usbh_mutex);
356358
#endif
359+
357360
// Get application driver if available
358-
if ( usbh_app_driver_get_cb )
359-
{
361+
if ( usbh_app_driver_get_cb ) {
360362
_app_driver = usbh_app_driver_get_cb(&_app_driver_count);
361-
_total_driver_count = USBH_BUILTIN_DRIVER_COUNT + _app_driver_count;
362363
}
363364

364365
// Device
@@ -372,9 +373,9 @@ bool tuh_init(uint8_t controller_id) {
372373
}
373374

374375
// Class drivers
375-
for (uint8_t drv_id = 0; drv_id < _total_driver_count; drv_id++)
376+
for (uint8_t drv_id = 0; drv_id < TOTAL_DRIVER_COUNT; drv_id++)
376377
{
377-
usbh_class_driver_t const * driver = usbh_get_driver(drv_id);
378+
usbh_class_driver_t const * driver = get_driver(drv_id);
378379
if ( driver )
379380
{
380381
TU_LOG_USBH("%s init\r\n", driver->name);
@@ -508,12 +509,12 @@ void tuh_task_ext(uint32_t timeout_ms, bool in_isr) {
508509
#endif
509510
{
510511
uint8_t drv_id = dev->ep2drv[epnum][ep_dir];
511-
usbh_class_driver_t const * driver = usbh_get_driver(drv_id);
512+
usbh_class_driver_t const * driver = get_driver(drv_id);
512513
if ( driver )
513514
{
514515
TU_LOG_USBH("%s xfer callback\r\n", driver->name);
515516
driver->xfer_cb(event.dev_addr, ep_addr, (xfer_result_t) event.xfer_complete.result,
516-
event.xfer_complete.len);
517+
event.xfer_complete.len);
517518
}
518519
else
519520
{
@@ -1220,12 +1221,10 @@ static void process_removing_device(uint8_t rhport, uint8_t hub_addr, uint8_t hu
12201221
// hub_addr = 0 means roothub, hub_port = 0 means all devices of downstream hub
12211222
if (dev->rhport == rhport && dev->connected &&
12221223
(hub_addr == 0 || dev->hub_addr == hub_addr) &&
1223-
(hub_port == 0 || dev->hub_port == hub_port))
1224-
{
1224+
(hub_port == 0 || dev->hub_port == hub_port)) {
12251225
TU_LOG_USBH("Device unplugged address = %u\r\n", daddr);
12261226

1227-
if (is_hub_addr(daddr))
1228-
{
1227+
if (is_hub_addr(daddr)) {
12291228
TU_LOG(CFG_TUH_LOG_LEVEL, " is a HUB device %u\r\n", daddr);
12301229

12311230
// Submit removed event If the device itself is a hub (un-rolled recursive)
@@ -1243,14 +1242,11 @@ static void process_removing_device(uint8_t rhport, uint8_t hub_addr, uint8_t hu
12431242
}
12441243

12451244
// Close class driver
1246-
for (uint8_t drv_id = 0; drv_id < _total_driver_count; drv_id++)
1247-
{
1248-
usbh_class_driver_t const * driver = usbh_get_driver(drv_id);
1249-
if ( driver )
1250-
{
1251-
driver->close(daddr);
1252-
}
1245+
for (uint8_t drv_id = 0; drv_id < TOTAL_DRIVER_COUNT; drv_id++) {
1246+
usbh_class_driver_t const * driver = get_driver(drv_id);
1247+
if ( driver ) driver->close(daddr);
12531248
}
1249+
12541250
hcd_device_close(rhport, daddr);
12551251
clear_device(dev);
12561252
// abort on-going control xfer if any
@@ -1679,10 +1675,9 @@ static bool _parse_configuration_descriptor(uint8_t dev_addr, tusb_desc_configur
16791675
TU_ASSERT(drv_len >= sizeof(tusb_desc_interface_t));
16801676

16811677
// Find driver for this interface
1682-
uint8_t drv_id = 0;
1683-
for (; drv_id < _total_driver_count; drv_id++)
1678+
for (uint8_t drv_id = 0; drv_id < TOTAL_DRIVER_COUNT; drv_id++)
16841679
{
1685-
usbh_class_driver_t const * driver = usbh_get_driver(drv_id);
1680+
usbh_class_driver_t const * driver = get_driver(drv_id);
16861681

16871682
if (driver && driver->open(dev->rhport, dev_addr, desc_itf, drv_len) )
16881683
{
@@ -1705,12 +1700,11 @@ static bool _parse_configuration_descriptor(uint8_t dev_addr, tusb_desc_configur
17051700
break; // exit driver find loop
17061701
}
17071702

1708-
}
1709-
1710-
if( drv_id >= _total_driver_count )
1711-
{
1712-
TU_LOG(CFG_TUH_LOG_LEVEL, "Interface %u: class = %u subclass = %u protocol = %u is not supported\r\n",
1713-
desc_itf->bInterfaceNumber, desc_itf->bInterfaceClass, desc_itf->bInterfaceSubClass, desc_itf->bInterfaceProtocol);
1703+
if ( drv_id == TOTAL_DRIVER_COUNT - 1 )
1704+
{
1705+
TU_LOG(CFG_TUH_LOG_LEVEL, "[%u:%u] Interface %u: class = %u subclass = %u protocol = %u is not supported\r\n",
1706+
dev->rhport, dev_addr, desc_itf->bInterfaceNumber, desc_itf->bInterfaceClass, desc_itf->bInterfaceSubClass, desc_itf->bInterfaceProtocol);
1707+
}
17141708
}
17151709

17161710
// next Interface or IAD descriptor
@@ -1730,9 +1724,9 @@ void usbh_driver_set_config_complete(uint8_t dev_addr, uint8_t itf_num)
17301724
// IAD binding interface such as CDCs should return itf_num + 1 when complete
17311725
// with usbh_driver_set_config_complete()
17321726
uint8_t const drv_id = dev->itf2drv[itf_num];
1733-
if (drv_id != TUSB_INDEX_INVALID_8)
1727+
usbh_class_driver_t const * driver = get_driver(drv_id);
1728+
if (driver)
17341729
{
1735-
usbh_class_driver_t const * driver = usbh_get_driver(drv_id);
17361730
TU_LOG_USBH("%s set config: itf = %u\r\n", driver->name, itf_num);
17371731
driver->set_config(dev_addr, itf_num);
17381732
break;

src/host/usbh_classdriver.h renamed to src/host/usbh_pvt.h

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ typedef struct {
6262
void (* const close )(uint8_t dev_addr);
6363
} usbh_class_driver_t;
6464

65+
// Invoked when initializing host stack to get additional class drivers.
66+
// Can be implemented by application to extend/overwrite class driver support.
67+
// Note: The drivers array must be accessible at all time when stack is active
68+
usbh_class_driver_t const* usbh_app_driver_get_cb(uint8_t* driver_count) TU_ATTR_WEAK;
69+
6570
// Call by class driver to tell USBH that it has complete the enumeration
6671
void usbh_driver_set_config_complete(uint8_t dev_addr, uint8_t itf_num);
6772

@@ -96,14 +101,6 @@ bool usbh_edpt_release(uint8_t dev_addr, uint8_t ep_addr);
96101
// Check if endpoint transferring is complete
97102
bool usbh_edpt_busy(uint8_t dev_addr, uint8_t ep_addr);
98103

99-
//--------------------------------------------------------------------+
100-
// USBH application additional driver API
101-
//--------------------------------------------------------------------+
102-
// Invoked when initializing host stack to get additional class drivers.
103-
// Can optionally implemented by application to extend/overwrite class driver support.
104-
// Note: The drivers array must be accessible at all time when stack is active
105-
usbh_class_driver_t const* usbh_app_driver_get_cb(uint8_t* driver_count) TU_ATTR_WEAK;
106-
107104
#ifdef __cplusplus
108105
}
109106
#endif

src/tusb.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
#endif
3737

3838
#if CFG_TUH_ENABLED
39-
#include "host/usbh_classdriver.h"
39+
#include "host/usbh_pvt.h"
4040
#endif
4141

4242
//--------------------------------------------------------------------+

0 commit comments

Comments
 (0)