Skip to content

Commit

Permalink
Merge pull request #1960 from hathach/fix-host-enumerate-mul-device
Browse files Browse the repository at this point in the history
Fix host enumerate multiple devices from multiple host controllers
  • Loading branch information
hathach authored Mar 17, 2023
2 parents 96d064e + 511f5be commit 16daad8
Show file tree
Hide file tree
Showing 14 changed files with 72 additions and 53 deletions.
5 changes: 5 additions & 0 deletions examples/host/bare_api/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,8 @@ target_include_directories(${PROJECT} PUBLIC
# Configure compilation flags and libraries for the example... see the corresponding function
# in hw/bsp/FAMILY/family.cmake for details.
family_configure_host_example(${PROJECT})

# Add pico-pio-usb for rp2040 since user can choose to run on bit-banging host
if(FAMILY STREQUAL "rp2040")
family_add_pico_pio_usb(${PROJECT})
endif()
4 changes: 2 additions & 2 deletions examples/host/bare_api/src/tusb_config.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* The MIT License (MIT)
*
* Copyright (c) 2019 Ha Thach (tinyusb.org)
Expand Down Expand Up @@ -100,7 +100,7 @@

// max device support (excluding hub device)
// 1 hub typically has 4 ports
#define CFG_TUH_DEVICE_MAX (CFG_TUH_HUB ? 4 : 1)
#define CFG_TUH_DEVICE_MAX (3*CFG_TUH_HUB + 1)

// Max endpoint per device
#define CFG_TUH_ENDPOINT_MAX 8
Expand Down
5 changes: 5 additions & 0 deletions examples/host/cdc_msc_hid/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,8 @@ target_include_directories(${PROJECT} PUBLIC
# Configure compilation flags and libraries for the example... see the corresponding function
# in hw/bsp/FAMILY/family.cmake for details.
family_configure_host_example(${PROJECT})

# Add pico-pio-usb for rp2040 since user can choose to run on bit-banging host
if(FAMILY STREQUAL "rp2040")
family_add_pico_pio_usb(${PROJECT})
endif()
6 changes: 3 additions & 3 deletions examples/host/cdc_msc_hid/src/tusb_config.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* The MIT License (MIT)
*
* Copyright (c) 2019 Ha Thach (tinyusb.org)
Expand Down Expand Up @@ -101,8 +101,8 @@
#define CFG_TUH_MSC 1
#define CFG_TUH_VENDOR 0

// max device support (excluding hub device)
#define CFG_TUH_DEVICE_MAX (CFG_TUH_HUB ? 4 : 1) // hub typically has 4 ports
// max device support (excluding hub device): 1 hub typically has 4 ports
#define CFG_TUH_DEVICE_MAX (3*CFG_TUH_HUB + 1)

//------------- HID -------------//
#define CFG_TUH_HID_EPIN_BUFSIZE 64
Expand Down
5 changes: 5 additions & 0 deletions examples/host/hid_controller/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,8 @@ target_include_directories(${PROJECT} PUBLIC
# Configure compilation flags and libraries for the example... see the corresponding function
# in hw/bsp/FAMILY/family.cmake for details.
family_configure_host_example(${PROJECT})

# Add pico-pio-usb for rp2040 since user can choose to run on bit-banging host
if(FAMILY STREQUAL "rp2040")
family_add_pico_pio_usb(${PROJECT})
endif()
7 changes: 3 additions & 4 deletions examples/host/hid_controller/src/tusb_config.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* The MIT License (MIT)
*
* Copyright (c) 2019 Ha Thach (tinyusb.org)
Expand Down Expand Up @@ -101,9 +101,8 @@
#define CFG_TUH_MSC 0
#define CFG_TUH_VENDOR 0

// max device support (excluding hub device)
// 1 hub typically has 4 ports
#define CFG_TUH_DEVICE_MAX (CFG_TUH_HUB ? 4 : 1)
// max device support (excluding hub device): 1 hub typically has 4 ports
#define CFG_TUH_DEVICE_MAX (3*CFG_TUH_HUB + 1)

//------------- HID -------------//

Expand Down
4 changes: 4 additions & 0 deletions examples/host/msc_file_explorer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,7 @@ target_include_directories(${PROJECT} PUBLIC
# in hw/bsp/FAMILY/family.cmake for details.
family_configure_host_example(${PROJECT})

# Add pico-pio-usb for rp2040 since user can choose to run on bit-banging host
if(FAMILY STREQUAL "rp2040")
family_add_pico_pio_usb(${PROJECT})
endif()
6 changes: 3 additions & 3 deletions examples/host/msc_file_explorer/src/tusb_config.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* The MIT License (MIT)
*
* Copyright (c) 2019 Ha Thach (tinyusb.org)
Expand Down Expand Up @@ -101,8 +101,8 @@
#define CFG_TUH_HID 0 // typical keyboard + mouse device can have 3-4 HID interfaces
#define CFG_TUH_VENDOR 0

// max device support (excluding hub device)
#define CFG_TUH_DEVICE_MAX (CFG_TUH_HUB ? 4 : 1) // hub typically has 4 ports
// max device support (excluding hub device): 1 hub typically has 4 ports
#define CFG_TUH_DEVICE_MAX (3*CFG_TUH_HUB + 1)

//------------- MSC -------------//
#define CFG_TUH_MSC_MAXLUN 4 // typical for most card reader
Expand Down
3 changes: 0 additions & 3 deletions hw/bsp/rp2040/family.mk
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ $(BUILD):
all: $(BUILD)
$(MAKE) -C $(BUILD)

clean:
$(RM) -rf $(BUILD)

flash: flash-pyocd
flash-uf2:
@$(CP) $(BUILD)/$(PROJECT).uf2 /media/$(USER)/RPI-RP2
8 changes: 4 additions & 4 deletions src/class/cdc/cdc_host.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* The MIT License (MIT)
*
* Copyright (c) 2019 Ha Thach (tinyusb.org)
Expand Down Expand Up @@ -97,7 +97,7 @@ static inline uint8_t get_idx_by_ep_addr(uint8_t daddr, uint8_t ep_addr)
}
}

return TUSB_INDEX_INVALID;
return TU_INDEX_INVALID_8;
}


Expand All @@ -124,7 +124,7 @@ uint8_t tuh_cdc_itf_get_index(uint8_t daddr, uint8_t itf_num)
if (p_cdc->daddr == daddr && p_cdc->bInterfaceNumber == itf_num) return i;
}

return TUSB_INDEX_INVALID;
return TU_INDEX_INVALID_8;
}

bool tuh_cdc_itf_get_info(uint8_t idx, tuh_cdc_itf_info_t* info)
Expand Down Expand Up @@ -533,7 +533,7 @@ static void process_cdc_config(tuh_xfer_t* xfer)
uintptr_t const state = xfer->user_data;
uint8_t const itf_num = (uint8_t) tu_le16toh(xfer->setup->wIndex);
uint8_t const idx = tuh_cdc_itf_get_index(xfer->daddr, itf_num);
TU_ASSERT(idx != TUSB_INDEX_INVALID, );
TU_ASSERT(idx != TU_INDEX_INVALID_8, );

switch(state)
{
Expand Down
6 changes: 6 additions & 0 deletions src/common/tusb_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@

#include "tusb_timeout.h" // TODO remove

enum
{
TU_INDEX_INVALID_8 = 0xFFu
};


//--------------------------------------------------------------------+
// Optional API implemented by application if needed
// TODO move to a more ovious place/file
Expand Down
5 changes: 0 additions & 5 deletions src/common/tusb_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -273,11 +273,6 @@ enum
CONTROL_STAGE_ACK
};

enum
{
TUSB_INDEX_INVALID = 0xff
};

//--------------------------------------------------------------------+
// USB Descriptors
//--------------------------------------------------------------------+
Expand Down
57 changes: 30 additions & 27 deletions src/host/usbh.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* The MIT License (MIT)
*
* Copyright (c) 2019 Ha Thach (tinyusb.org)
Expand Down Expand Up @@ -54,24 +54,20 @@
// USBH-HCD common data structure
//--------------------------------------------------------------------+

// device0 struct must be strictly a subset of normal device struct
// TODO refactor later
typedef struct
{
// port
uint8_t rhport;
uint8_t hub_addr;
uint8_t hub_port;
uint8_t speed;
volatile uint8_t enumerating;

struct TU_ATTR_PACKED
{
volatile uint8_t connected : 1;
volatile uint8_t addressed : 1;
volatile uint8_t configured : 1;
volatile uint8_t suspended : 1;
};

// struct TU_ATTR_PACKED {
// uint8_t speed : 4; // packed speed to save footprint
// volatile uint8_t enumerating : 1;
// uint8_t TU_RESERVED : 3;
// };
} usbh_dev0_t;

typedef struct {
Expand Down Expand Up @@ -122,10 +118,6 @@ typedef struct {
// MACRO CONSTANT TYPEDEF
//--------------------------------------------------------------------+

// Invalid driver ID in itf2drv[] ep2drv[][] mapping
enum { DRVID_INVALID = 0xFFu };
enum { CONTROLLER_INVALID = 0xFFu };

#if CFG_TUSB_DEBUG >= 2
#define DRIVER_NAME(_name) .name = _name,
#else
Expand Down Expand Up @@ -203,7 +195,7 @@ enum { CONFIG_NUM = 1 }; // default to use configuration 1
// sum of end device + hub
#define TOTAL_DEVICES (CFG_TUH_DEVICE_MAX + CFG_TUH_HUB)

static uint8_t _usbh_controller = CONTROLLER_INVALID;
static uint8_t _usbh_controller = TU_INDEX_INVALID_8;

// Device with address = 0 for enumeration
static usbh_dev0_t _dev0;
Expand Down Expand Up @@ -311,13 +303,13 @@ tusb_speed_t tuh_speed_get (uint8_t dev_addr)
static void clear_device(usbh_device_t* dev)
{
tu_memclr(dev, sizeof(usbh_device_t));
memset(dev->itf2drv, DRVID_INVALID, sizeof(dev->itf2drv)); // invalid mapping
memset(dev->ep2drv , DRVID_INVALID, sizeof(dev->ep2drv )); // invalid mapping
memset(dev->itf2drv, TU_INDEX_INVALID_8, sizeof(dev->itf2drv)); // invalid mapping
memset(dev->ep2drv , TU_INDEX_INVALID_8, sizeof(dev->ep2drv )); // invalid mapping
}

bool tuh_inited(void)
{
return _usbh_controller != CONTROLLER_INVALID;
return _usbh_controller != TU_INDEX_INVALID_8;
}

bool tuh_init(uint8_t controller_id)
Expand Down Expand Up @@ -402,10 +394,18 @@ void tuh_task_ext(uint32_t timeout_ms, bool in_isr)
switch (event.event_id)
{
case HCD_EVENT_DEVICE_ATTACH:
// TODO due to the shared _usbh_ctrl_buf, we must complete enumerating
// due to the shared _usbh_ctrl_buf, we must complete enumerating
// one device before enumerating another one.
TU_LOG_USBH("[%u:] USBH DEVICE ATTACH\r\n", event.rhport);
enum_new_device(&event);
if ( _dev0.enumerating )
{
TU_LOG_USBH("[%u:] USBH Defer Attach until current enumeration complete\r\n", event.rhport);
osal_queue_send(_usbh_q, &event, in_isr);
}else
{
TU_LOG_USBH("[%u:] USBH DEVICE ATTACH\r\n", event.rhport);
_dev0.enumerating = 1;
enum_new_device(&event);
}
break;

case HCD_EVENT_DEVICE_REMOVE:
Expand Down Expand Up @@ -1358,11 +1358,11 @@ static void process_enumeration(tuh_xfer_t* xfer)

dev->configured = 1;

// Start the Set Configuration process for interfaces (itf = DRVID_INVALID)
// Start the Set Configuration process for interfaces (itf = TU_INDEX_INVALID_8)
// Since driver can perform control transfer within its set_config, this is done asynchronously.
// The process continue with next interface when class driver complete its sequence with usbh_driver_set_config_complete()
// TODO use separated API instead of using DRVID_INVALID
usbh_driver_set_config_complete(daddr, DRVID_INVALID);
// TODO use separated API instead of using TU_INDEX_INVALID_8
usbh_driver_set_config_complete(daddr, TU_INDEX_INVALID_8);
}
break;

Expand Down Expand Up @@ -1562,7 +1562,7 @@ static bool _parse_configuration_descriptor(uint8_t dev_addr, tusb_desc_configur
uint8_t const itf_num = desc_itf->bInterfaceNumber+i;

// Interface number must not be used already
TU_ASSERT( DRVID_INVALID == dev->itf2drv[itf_num] );
TU_ASSERT( TU_INDEX_INVALID_8 == dev->itf2drv[itf_num] );
dev->itf2drv[itf_num] = drv_id;
}

Expand Down Expand Up @@ -1596,7 +1596,7 @@ 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
// with usbh_driver_set_config_complete()
uint8_t const drv_id = dev->itf2drv[itf_num];
if (drv_id != DRVID_INVALID)
if (drv_id != TU_INDEX_INVALID_8)
{
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);
Expand All @@ -1623,6 +1623,9 @@ void usbh_driver_set_config_complete(uint8_t dev_addr, uint8_t itf_num)

static void enum_full_complete(void)
{
// mark enumeration as complete
_dev0.enumerating = 0;

#if CFG_TUH_HUB
// get next hub status
if (_dev0.hub_addr) hub_edpt_status_xfer(_dev0.hub_addr);
Expand Down
4 changes: 2 additions & 2 deletions tools/get_deps.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
'hw/mcu/nxp/lpcopen' : ['43c45c85405a5dd114fff0ea95cca62837740c13', 'https://github.com/hathach/nxp_lpcopen.git' ],
'hw/mcu/nxp/mcux-sdk' : ['ae2ab01d9d70ad00cd0e935c2552bd5f0e5c0294', 'https://github.com/NXPmicro/mcux-sdk.git' ],
'hw/mcu/nxp/nxp_sdk' : ['845c8fc49b6fb660f06a5c45225494eacb06f00c', 'https://github.com/hathach/nxp_sdk.git' ],
'hw/mcu/raspberry_pi/Pico-PIO-USB' : ['9ff3f52fd3c1f81532bce8dd311aa8fc8d9b2665', 'https://github.com/sekigon-gonnoc/Pico-PIO-USB.git' ],
'hw/mcu/renesas/fsp' : ['8dc14709f2a6518b43f71efad70d900b7718d9f1', 'https://github.com/renesas/fsp.git' ],
'hw/mcu/raspberry_pi/Pico-PIO-USB' : ['c3715ce94b6f6391856de56081d4d9b3e98fa93d', 'https://github.com/sekigon-gonnoc/Pico-PIO-USB.git' ],
'hw/mcu/renesas/fsp' : ['8dc14709f2a6518b43f71efad70d900b7718d9f1', 'https://github.com/renesas/fsp.git' ],
'hw/mcu/renesas/rx' : ['706b4e0cf485605c32351e2f90f5698267996023', 'https://github.com/kkitayam/rx_device.git' ],
'hw/mcu/silabs/cmsis-dfp-efm32gg12b' : ['f1c31b7887669cb230b3ea63f9b56769078960bc', 'https://github.com/cmsis-packs/cmsis-dfp-efm32gg12b.git' ],
'hw/mcu/sony/cxd56/spresense-exported-sdk' : ['2ec2a1538362696118dc3fdf56f33dacaf8f4067', 'https://github.com/sonydevworld/spresense-exported-sdk.git' ],
Expand Down

0 comments on commit 16daad8

Please sign in to comment.