30
30
31
31
#include "host/hcd.h"
32
32
#include "tusb.h"
33
- #include "host/usbh_classdriver .h"
33
+ #include "host/usbh_pvt .h"
34
34
#include "hub.h"
35
35
36
36
//--------------------------------------------------------------------+
@@ -183,6 +183,23 @@ static usbh_class_driver_t const usbh_class_drivers[] =
183
183
enum { USBH_BUILTIN_DRIVER_COUNT = TU_ARRAY_SIZE (usbh_class_drivers ) };
184
184
enum { CONFIG_NUM = 1 }; // default to use configuration 1
185
185
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
+ }
186
203
187
204
//--------------------------------------------------------------------+
188
205
// INTERNAL OBJECT & FUNCTION DECLARATION
@@ -246,21 +263,6 @@ static void process_removing_device(uint8_t rhport, uint8_t hub_addr, uint8_t hu
246
263
static bool usbh_edpt_control_open (uint8_t dev_addr , uint8_t max_packet_size );
247
264
static bool usbh_control_xfer_cb (uint8_t daddr , uint8_t ep_addr , xfer_result_t result , uint32_t xferred_bytes );
248
265
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
-
264
266
#if CFG_TUSB_OS == OPT_OS_NONE
265
267
// TODO rework time-related function later
266
268
// weak and overridable
@@ -354,11 +356,10 @@ bool tuh_init(uint8_t controller_id) {
354
356
_usbh_mutex = osal_mutex_create (& _usbh_mutexdef );
355
357
TU_ASSERT (_usbh_mutex );
356
358
#endif
359
+
357
360
// Get application driver if available
358
- if ( usbh_app_driver_get_cb )
359
- {
361
+ if ( usbh_app_driver_get_cb ) {
360
362
_app_driver = usbh_app_driver_get_cb (& _app_driver_count );
361
- _total_driver_count = USBH_BUILTIN_DRIVER_COUNT + _app_driver_count ;
362
363
}
363
364
364
365
// Device
@@ -372,9 +373,9 @@ bool tuh_init(uint8_t controller_id) {
372
373
}
373
374
374
375
// 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 ++ )
376
377
{
377
- usbh_class_driver_t const * driver = usbh_get_driver (drv_id );
378
+ usbh_class_driver_t const * driver = get_driver (drv_id );
378
379
if ( driver )
379
380
{
380
381
TU_LOG_USBH ("%s init\r\n" , driver -> name );
@@ -508,12 +509,12 @@ void tuh_task_ext(uint32_t timeout_ms, bool in_isr) {
508
509
#endif
509
510
{
510
511
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 );
512
513
if ( driver )
513
514
{
514
515
TU_LOG_USBH ("%s xfer callback\r\n" , driver -> name );
515
516
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 );
517
518
}
518
519
else
519
520
{
@@ -1220,12 +1221,10 @@ static void process_removing_device(uint8_t rhport, uint8_t hub_addr, uint8_t hu
1220
1221
// hub_addr = 0 means roothub, hub_port = 0 means all devices of downstream hub
1221
1222
if (dev -> rhport == rhport && dev -> connected &&
1222
1223
(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 )) {
1225
1225
TU_LOG_USBH ("Device unplugged address = %u\r\n" , daddr );
1226
1226
1227
- if (is_hub_addr (daddr ))
1228
- {
1227
+ if (is_hub_addr (daddr )) {
1229
1228
TU_LOG (CFG_TUH_LOG_LEVEL , " is a HUB device %u\r\n" , daddr );
1230
1229
1231
1230
// 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
1243
1242
}
1244
1243
1245
1244
// 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 );
1253
1248
}
1249
+
1254
1250
hcd_device_close (rhport , daddr );
1255
1251
clear_device (dev );
1256
1252
// abort on-going control xfer if any
@@ -1679,10 +1675,9 @@ static bool _parse_configuration_descriptor(uint8_t dev_addr, tusb_desc_configur
1679
1675
TU_ASSERT (drv_len >= sizeof (tusb_desc_interface_t ));
1680
1676
1681
1677
// 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 ++ )
1684
1679
{
1685
- usbh_class_driver_t const * driver = usbh_get_driver (drv_id );
1680
+ usbh_class_driver_t const * driver = get_driver (drv_id );
1686
1681
1687
1682
if (driver && driver -> open (dev -> rhport , dev_addr , desc_itf , drv_len ) )
1688
1683
{
@@ -1705,12 +1700,11 @@ static bool _parse_configuration_descriptor(uint8_t dev_addr, tusb_desc_configur
1705
1700
break ; // exit driver find loop
1706
1701
}
1707
1702
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
+ }
1714
1708
}
1715
1709
1716
1710
// next Interface or IAD descriptor
@@ -1730,9 +1724,9 @@ void usbh_driver_set_config_complete(uint8_t dev_addr, uint8_t itf_num)
1730
1724
// IAD binding interface such as CDCs should return itf_num + 1 when complete
1731
1725
// with usbh_driver_set_config_complete()
1732
1726
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 )
1734
1729
{
1735
- usbh_class_driver_t const * driver = usbh_get_driver (drv_id );
1736
1730
TU_LOG_USBH ("%s set config: itf = %u\r\n" , driver -> name , itf_num );
1737
1731
driver -> set_config (dev_addr , itf_num );
1738
1732
break ;
0 commit comments