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//--------------------------------------------------------------------+
@@ -180,9 +180,26 @@ static usbh_class_driver_t const usbh_class_drivers[] =
180180 #endif
181181};
182182
183- enum { USBH_CLASS_DRIVER_COUNT = TU_ARRAY_SIZE (usbh_class_drivers ) };
183+ enum { 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 + 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 && 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
@@ -340,6 +357,11 @@ bool tuh_init(uint8_t controller_id) {
340357 TU_ASSERT (_usbh_mutex );
341358#endif
342359
360+ // Get application driver if available
361+ if ( usbh_app_driver_get_cb ) {
362+ _app_driver = usbh_app_driver_get_cb (& _app_driver_count );
363+ }
364+
343365 // Device
344366 tu_memclr (& _dev0 , sizeof (_dev0 ));
345367 tu_memclr (_usbh_devices , sizeof (_usbh_devices ));
@@ -351,10 +373,14 @@ bool tuh_init(uint8_t controller_id) {
351373 }
352374
353375 // Class drivers
354- for (uint8_t drv_id = 0 ; drv_id < USBH_CLASS_DRIVER_COUNT ; drv_id ++ )
376+ for (uint8_t drv_id = 0 ; drv_id < TOTAL_DRIVER_COUNT ; drv_id ++ )
355377 {
356- TU_LOG_USBH ("%s init\r\n" , usbh_class_drivers [drv_id ].name );
357- usbh_class_drivers [drv_id ].init ();
378+ usbh_class_driver_t const * driver = get_driver (drv_id );
379+ if ( driver )
380+ {
381+ TU_LOG_USBH ("%s init\r\n" , driver -> name );
382+ driver -> init ();
383+ }
358384 }
359385
360386 _usbh_controller = controller_id ;;
@@ -482,12 +508,16 @@ void tuh_task_ext(uint32_t timeout_ms, bool in_isr) {
482508 }else
483509 #endif
484510 {
485- uint8_t const drv_id = dev -> ep2drv [epnum ][ep_dir ];
486- if ( drv_id < USBH_CLASS_DRIVER_COUNT ) {
487- TU_LOG_USBH ("%s xfer callback\r\n" , usbh_class_drivers [drv_id ].name );
488- usbh_class_drivers [drv_id ].xfer_cb (event .dev_addr , ep_addr , (xfer_result_t ) event .xfer_complete .result ,
489- event .xfer_complete .len );
490- } else {
511+ uint8_t drv_id = dev -> ep2drv [epnum ][ep_dir ];
512+ usbh_class_driver_t const * driver = get_driver (drv_id );
513+ if ( driver )
514+ {
515+ TU_LOG_USBH ("%s xfer callback\r\n" , driver -> name );
516+ driver -> xfer_cb (event .dev_addr , ep_addr , (xfer_result_t ) event .xfer_complete .result ,
517+ event .xfer_complete .len );
518+ }
519+ else
520+ {
491521 // no driver/callback responsible for this transfer
492522 TU_ASSERT (false,);
493523 }
@@ -1183,7 +1213,8 @@ static void process_removing_device(uint8_t rhport, uint8_t hub_addr, uint8_t hu
11831213 uint8_t nop_count = 0 ;
11841214#endif
11851215
1186- for (uint8_t dev_id = 0 ; dev_id < TOTAL_DEVICES ; dev_id ++ ) {
1216+ for (uint8_t dev_id = 0 ; dev_id < TOTAL_DEVICES ; dev_id ++ )
1217+ {
11871218 usbh_device_t * dev = & _usbh_devices [dev_id ];
11881219 uint8_t const daddr = dev_id + 1 ;
11891220
@@ -1211,8 +1242,9 @@ static void process_removing_device(uint8_t rhport, uint8_t hub_addr, uint8_t hu
12111242 }
12121243
12131244 // Close class driver
1214- for (uint8_t drv_id = 0 ; drv_id < USBH_CLASS_DRIVER_COUNT ; drv_id ++ ) {
1215- usbh_class_drivers [drv_id ].close (daddr );
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 );
12161248 }
12171249
12181250 hcd_device_close (rhport , daddr );
@@ -1643,11 +1675,11 @@ static bool _parse_configuration_descriptor(uint8_t dev_addr, tusb_desc_configur
16431675 TU_ASSERT (drv_len >= sizeof (tusb_desc_interface_t ));
16441676
16451677 // Find driver for this interface
1646- for (uint8_t drv_id = 0 ; drv_id < USBH_CLASS_DRIVER_COUNT ; drv_id ++ )
1678+ for (uint8_t drv_id = 0 ; drv_id < TOTAL_DRIVER_COUNT ; drv_id ++ )
16471679 {
1648- usbh_class_driver_t const * driver = & usbh_class_drivers [ drv_id ] ;
1680+ usbh_class_driver_t const * driver = get_driver ( drv_id ) ;
16491681
1650- if ( driver -> open (dev -> rhport , dev_addr , desc_itf , drv_len ) )
1682+ if (driver && driver -> open (dev -> rhport , dev_addr , desc_itf , drv_len ) )
16511683 {
16521684 // open successfully
16531685 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
16681700 break ; // exit driver find loop
16691701 }
16701702
1671- if ( drv_id >= USBH_CLASS_DRIVER_COUNT )
1703+ if ( drv_id == TOTAL_DRIVER_COUNT - 1 )
16721704 {
1673- TU_LOG (CFG_TUH_LOG_LEVEL , "Interface %u: class = %u subclass = %u protocol = %u is not supported\r\n" ,
1674- desc_itf -> bInterfaceNumber , desc_itf -> bInterfaceClass , desc_itf -> bInterfaceSubClass , desc_itf -> bInterfaceProtocol );
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 );
16751707 }
16761708 }
16771709
@@ -1692,9 +1724,9 @@ void usbh_driver_set_config_complete(uint8_t dev_addr, uint8_t itf_num)
16921724 // IAD binding interface such as CDCs should return itf_num + 1 when complete
16931725 // with usbh_driver_set_config_complete()
16941726 uint8_t const drv_id = dev -> itf2drv [itf_num ];
1695- if (drv_id != TUSB_INDEX_INVALID_8 )
1727+ usbh_class_driver_t const * driver = get_driver (drv_id );
1728+ if (driver )
16961729 {
1697- usbh_class_driver_t const * driver = & usbh_class_drivers [drv_id ];
16981730 TU_LOG_USBH ("%s set config: itf = %u\r\n" , driver -> name , itf_num );
16991731 driver -> set_config (dev_addr , itf_num );
17001732 break ;
0 commit comments