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[] =
183183enum { USBH_BUILTIN_DRIVER_COUNT = TU_ARRAY_SIZE (usbh_class_drivers ) };
184184enum { 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
246263static bool usbh_edpt_control_open (uint8_t dev_addr , uint8_t max_packet_size );
247264static 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 ;
0 commit comments