Skip to content

Commit

Permalink
add parse config descriptor to example
Browse files Browse the repository at this point in the history
move usbh_edpt_open() to public API, remove rhport from its signature
  • Loading branch information
hathach committed Mar 18, 2022
1 parent ba1185b commit 4795cca
Show file tree
Hide file tree
Showing 11 changed files with 179 additions and 45 deletions.
152 changes: 128 additions & 24 deletions examples/host/bare_api/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,19 @@
#include "bsp/board.h"
#include "tusb.h"

// English
#define LANGUAGE_ID 0x0409

//--------------------------------------------------------------------+
// MACRO CONSTANT TYPEDEF PROTYPES
//--------------------------------------------------------------------+
void led_blinking_task(void);

static void print_utf16(uint16_t *temp_buf, size_t buf_len);
void print_device_descriptor(tuh_xfer_t* xfer);
void parse_config_descriptor(uint8_t dev_addr, tusb_desc_configuration_t const* desc_cfg);

tusb_desc_device_t desc_device;

/*------------- MAIN -------------*/
int main(void)
Expand All @@ -60,20 +67,27 @@ int main(void)
return 0;
}

//--------------------------------------------------------------------+
// TinyUSB Callbacks
//--------------------------------------------------------------------+
/*------------- TinyUSB Callbacks -------------*/

// English
#define LANGUAGE_ID 0x0409
// Invoked when device is mounted (configured)
void tuh_mount_cb (uint8_t daddr)
{
printf("Device attached, address = %d\r\n", daddr);

tusb_desc_device_t desc_device;
// Get Device Descriptor sync API
// TODO: invoking control trannsfer now has issue with mounting hub with multiple devices attached, fix later
tuh_descriptor_get_device(daddr, &desc_device, 18, print_device_descriptor, 0);
}

//void parse_config_descriptor(uint8_t dev_addr, tusb_desc_configuration_t const* desc_cfg)
//{
// uint8_t const* desc_end = ((uint8_t const*) desc_cfg) + tu_le16toh(desc_cfg->wTotalLength);
// uint8_t const* p_desc = tu_desc_next(desc_cfg);
//}
/// Invoked when device is unmounted (bus reset/unplugged)
void tuh_umount_cb(uint8_t daddr)
{
printf("Device removed, address = %d\r\n", daddr);
}

//--------------------------------------------------------------------+
// Device Descriptor
//--------------------------------------------------------------------+

void print_device_descriptor(tuh_xfer_t* xfer)
{
Expand Down Expand Up @@ -125,28 +139,118 @@ void print_device_descriptor(tuh_xfer_t* xfer)
printf(" bNumConfigurations %u\r\n" , desc_device.bNumConfigurations);

// Get configuration descriptor with sync API
// if (XFER_RESULT_SUCCESS == tuh_descriptor_get_configuration_sync(daddr, 0, temp_buf, sizeof(temp_buf)) )
// {
// parse_config_descriptor(daddr, (tusb_desc_configuration_t*) temp_buf);
// }
if (XFER_RESULT_SUCCESS == tuh_descriptor_get_configuration_sync(daddr, 0, temp_buf, sizeof(temp_buf)) )
{
parse_config_descriptor(daddr, (tusb_desc_configuration_t*) temp_buf);
}
}

// Invoked when device is mounted (configured)
void tuh_mount_cb (uint8_t daddr)

//--------------------------------------------------------------------+
// Configuration Descriptor
//--------------------------------------------------------------------+

// count total length of an interface
uint16_t count_interface_total_len(tusb_desc_interface_t const* desc_itf, uint8_t itf_count, uint16_t max_len);

void open_hid_interface(uint8_t dev_addr, tusb_desc_interface_t const *desc_itf, uint16_t max_len)
{
printf("Device attached, address = %d\r\n", daddr);
// len = interface + hid + n*endpoints
uint16_t const drv_len = sizeof(tusb_desc_interface_t) + sizeof(tusb_hid_descriptor_hid_t) + desc_itf->bNumEndpoints*sizeof(tusb_desc_endpoint_t);

// Get Device Descriptor sync API
// TODO: invoking control trannsfer now has issue with mounting hub with multiple devices attached, fix later
tuh_descriptor_get_device(daddr, &desc_device, 18, print_device_descriptor, 0);
// corrupted descriptor
if (max_len < drv_len) return;

uint8_t const *p_desc = (uint8_t const *) desc_itf;

// HID descriptor
p_desc = tu_desc_next(p_desc);

// Endpoint descriptor
tusb_desc_endpoint_t const * desc_ep = (tusb_desc_endpoint_t const *) p_desc;

for(int i = 0; i < desc_itf->bNumEndpoints; i++)
{
if (TUSB_DESC_ENDPOINT != desc_ep->bDescriptorType) return;

if(tu_edpt_dir(desc_ep->bEndpointAddress) == TUSB_DIR_IN)
{
//usbh_edpt_open(rhport, dev_addr, desc_ep);
}
}
}

/// Invoked when device is unmounted (bus reset/unplugged)
void tuh_umount_cb(uint8_t daddr)
// simple configuration parser to open and listen to HID Endpoint IN
void parse_config_descriptor(uint8_t dev_addr, tusb_desc_configuration_t const* desc_cfg)
{
printf("Device removed, address = %d\r\n", daddr);
uint8_t const* desc_end = ((uint8_t const*) desc_cfg) + tu_le16toh(desc_cfg->wTotalLength);
uint8_t const* p_desc = tu_desc_next(desc_cfg);

// parse each interfaces
while( p_desc < desc_end )
{
uint8_t assoc_itf_count = 1;

// Class will always starts with Interface Association (if any) and then Interface descriptor
if ( TUSB_DESC_INTERFACE_ASSOCIATION == tu_desc_type(p_desc) )
{
tusb_desc_interface_assoc_t const * desc_iad = (tusb_desc_interface_assoc_t const *) p_desc;
assoc_itf_count = desc_iad->bInterfaceCount;

p_desc = tu_desc_next(p_desc); // next to Interface
}

// must be interface from now
if( TUSB_DESC_INTERFACE != tu_desc_type(p_desc) ) return;
tusb_desc_interface_t const* desc_itf = (tusb_desc_interface_t const*) p_desc;

uint16_t const drv_len = count_interface_total_len(desc_itf, assoc_itf_count, desc_end-p_desc);

// probably corrupted descriptor
if(drv_len < sizeof(tusb_desc_interface_t)) return;

// only open and listen to HID endpoint IN
if (desc_itf->bInterfaceClass == TUSB_CLASS_HID)
{
open_hid_interface(dev_addr, desc_itf, drv_len);
}

// next Interface or IAD descriptor
p_desc += drv_len;
}
}

uint16_t count_interface_total_len(tusb_desc_interface_t const* desc_itf, uint8_t itf_count, uint16_t max_len)
{
uint8_t const* p_desc = (uint8_t const*) desc_itf;
uint16_t len = 0;

while (itf_count--)
{
// Next on interface desc
len += tu_desc_len(desc_itf);
p_desc = tu_desc_next(p_desc);

while (len < max_len)
{
// return on IAD regardless of itf count
if ( tu_desc_type(p_desc) == TUSB_DESC_INTERFACE_ASSOCIATION ) return len;

if ( (tu_desc_type(p_desc) == TUSB_DESC_INTERFACE) &&
((tusb_desc_interface_t const*) p_desc)->bAlternateSetting == 0 )
{
break;
}

len += tu_desc_len(p_desc);
p_desc = tu_desc_next(p_desc);
}
}

return len;
}


//--------------------------------------------------------------------+
// Blinking Task
//--------------------------------------------------------------------+
Expand Down
5 changes: 3 additions & 2 deletions src/class/cdc/cdc_host.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ void cdch_init(void)

bool cdch_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *itf_desc, uint16_t max_len)
{
(void) rhport;
(void) max_len;

// Only support ACM subclass
Expand Down Expand Up @@ -196,7 +197,7 @@ bool cdch_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *it
// notification endpoint
tusb_desc_endpoint_t const * desc_ep = (tusb_desc_endpoint_t const *) p_desc;

TU_ASSERT( usbh_edpt_open(rhport, dev_addr, desc_ep) );
TU_ASSERT( usbh_edpt_open(dev_addr, desc_ep) );
p_cdc->ep_notif = desc_ep->bEndpointAddress;

drv_len += tu_desc_len(p_desc);
Expand All @@ -217,7 +218,7 @@ bool cdch_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *it
tusb_desc_endpoint_t const *desc_ep = (tusb_desc_endpoint_t const *) p_desc;
TU_ASSERT(TUSB_DESC_ENDPOINT == desc_ep->bDescriptorType && TUSB_XFER_BULK == desc_ep->bmAttributes.xfer);

TU_ASSERT(usbh_edpt_open(rhport, dev_addr, desc_ep));
TU_ASSERT(usbh_edpt_open(dev_addr, desc_ep));

if ( tu_edpt_dir(desc_ep->bEndpointAddress) == TUSB_DIR_IN )
{
Expand Down
2 changes: 1 addition & 1 deletion src/class/hid/hid.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
/** \defgroup ClassDriver_HID_Common Common Definitions
* @{ */

/// USB HID Descriptor
/// USB HID Descriptor
typedef struct TU_ATTR_PACKED
{
uint8_t bLength; /**< Numeric expression that is the total size of the HID descriptor */
Expand Down
3 changes: 2 additions & 1 deletion src/class/hid/hid_host.c
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ void hidh_close(uint8_t dev_addr)

bool hidh_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *desc_itf, uint16_t max_len)
{
(void) rhport;
(void) max_len;

TU_VERIFY(TUSB_CLASS_HID == desc_itf->bInterfaceClass);
Expand Down Expand Up @@ -355,7 +356,7 @@ bool hidh_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *de
for(int i = 0; i < desc_itf->bNumEndpoints; i++)
{
TU_ASSERT(TUSB_DESC_ENDPOINT == desc_ep->bDescriptorType);
TU_ASSERT( usbh_edpt_open(rhport, dev_addr, desc_ep) );
TU_ASSERT( usbh_edpt_open(dev_addr, desc_ep) );

if(tu_edpt_dir(desc_ep->bEndpointAddress) == TUSB_DIR_IN)
{
Expand Down
3 changes: 2 additions & 1 deletion src/class/msc/msc_host.c
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ static bool config_read_capacity_complete(uint8_t dev_addr, msc_cbw_t const* cbw

bool msch_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *desc_itf, uint16_t max_len)
{
(void) rhport;
TU_VERIFY (MSC_SUBCLASS_SCSI == desc_itf->bInterfaceSubClass &&
MSC_PROTOCOL_BOT == desc_itf->bInterfaceProtocol);

Expand All @@ -378,7 +379,7 @@ bool msch_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *de
for(uint32_t i=0; i<2; i++)
{
TU_ASSERT(TUSB_DESC_ENDPOINT == ep_desc->bDescriptorType && TUSB_XFER_BULK == ep_desc->bmAttributes.xfer);
TU_ASSERT(usbh_edpt_open(rhport, dev_addr, ep_desc));
TU_ASSERT(usbh_edpt_open(dev_addr, ep_desc));

if ( tu_edpt_dir(ep_desc->bEndpointAddress) == TUSB_DIR_IN )
{
Expand Down
4 changes: 3 additions & 1 deletion src/host/hub.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ void hub_init(void)

bool hub_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *itf_desc, uint16_t max_len)
{
(void) rhport;

TU_VERIFY(TUSB_CLASS_HUB == itf_desc->bInterfaceClass &&
0 == itf_desc->bInterfaceSubClass);

Expand All @@ -199,7 +201,7 @@ bool hub_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *itf
TU_ASSERT(TUSB_DESC_ENDPOINT == desc_ep->bDescriptorType &&
TUSB_XFER_INTERRUPT == desc_ep->bmAttributes.xfer, 0);

TU_ASSERT(usbh_edpt_open(rhport, dev_addr, desc_ep));
TU_ASSERT(usbh_edpt_open(dev_addr, desc_ep));

hub_interface_t* p_hub = get_itf(dev_addr);

Expand Down
11 changes: 6 additions & 5 deletions src/host/usbh.c
Original file line number Diff line number Diff line change
Expand Up @@ -806,10 +806,11 @@ static bool usbh_edpt_control_open(uint8_t dev_addr, uint8_t max_packet_size)
return hcd_edpt_open(usbh_get_rhport(dev_addr), dev_addr, &ep0_desc);
}

bool usbh_edpt_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_endpoint_t const * desc_ep)
bool usbh_edpt_open(uint8_t dev_addr, tusb_desc_endpoint_t const * desc_ep)
{
TU_ASSERT( tu_edpt_validate(desc_ep, tuh_speed_get(dev_addr)) );
return hcd_edpt_open(rhport, dev_addr, desc_ep);

return hcd_edpt_open(usbh_get_rhport(dev_addr), dev_addr, desc_ep);
}

bool usbh_edpt_busy(uint8_t dev_addr, uint8_t ep_addr)
Expand Down Expand Up @@ -1189,7 +1190,7 @@ enum {
};

static bool enum_request_set_addr(void);
static bool parse_configuration_descriptor (uint8_t dev_addr, tusb_desc_configuration_t const* desc_cfg);
static bool _parse_configuration_descriptor (uint8_t dev_addr, tusb_desc_configuration_t const* desc_cfg);
static void enum_full_complete(void);

// process device enumeration
Expand Down Expand Up @@ -1350,7 +1351,7 @@ static void process_enumeration(tuh_xfer_t* xfer)
case ENUM_SET_CONFIG:
// Parse configuration & set up drivers
// Driver open aren't allowed to make any usb transfer yet
TU_ASSERT( parse_configuration_descriptor(daddr, (tusb_desc_configuration_t*) _usbh_ctrl_buf), );
TU_ASSERT( _parse_configuration_descriptor(daddr, (tusb_desc_configuration_t*) _usbh_ctrl_buf), );

TU_ASSERT( tuh_configuration_set(daddr, CONFIG_NUM, process_enumeration, ENUM_CONFIG_DRIVER), );
break;
Expand Down Expand Up @@ -1496,7 +1497,7 @@ static bool enum_request_set_addr(void)
return true;
}

static bool parse_configuration_descriptor(uint8_t dev_addr, tusb_desc_configuration_t const* desc_cfg)
static bool _parse_configuration_descriptor(uint8_t dev_addr, tusb_desc_configuration_t const* desc_cfg)
{
usbh_device_t* dev = get_device(dev_addr);

Expand Down
14 changes: 10 additions & 4 deletions src/host/usbh.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ typedef struct tuh_xfer_s tuh_xfer_t;

typedef void (*tuh_xfer_cb_t)(tuh_xfer_t* xfer);

// Note: layout and order of this will be changed in near future
// Note1: layout and order of this will be changed in near future
// it is advised to initialize it using member name
// Note2: not all field is available/meaningful in callback, some info is not saved by
// usbh to save SRAM
struct tuh_xfer_s
{
uint8_t daddr;
Expand Down Expand Up @@ -124,14 +126,18 @@ static inline bool tuh_ready(uint8_t daddr)
//--------------------------------------------------------------------+

// Submit a control transfer
// Note: blocking if complete callback is NULL, in this case xfer contents will be updated to reflect the result
// - async: complete callback invoked when finished.
// - sync : blocking if complete callback is NULL.
bool tuh_control_xfer(tuh_xfer_t* xfer);

// Submit a bulk/interrupt transfer
// xfer memory must exist until transfer is complete.
// Note: blocking if complete callback is NULL.
// - async: complete callback invoked when finished.
// - sync : blocking if complete callback is NULL.
bool tuh_edpt_xfer(tuh_xfer_t* xfer);

// Open an endpoint
bool usbh_edpt_open(uint8_t dev_addr, tusb_desc_endpoint_t const * desc_ep);

// Set Configuration (control transfer)
// config_num = 0 will un-configure device. Note: config_num = config_descriptor_index + 1
// true on success, false if there is on-going control transfer or incorrect parameters
Expand Down
3 changes: 0 additions & 3 deletions src/host/usbh_classdriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,6 @@ void usbh_int_set(bool enabled);
// USBH Endpoint API
//--------------------------------------------------------------------+

// Open an endpoint
bool usbh_edpt_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_endpoint_t const * desc_ep);

// Submit a usb transfer with callback support, require CFG_TUH_API_EDPT_XFER
bool usbh_edpt_xfer_with_callback(uint8_t dev_addr, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes,
tuh_xfer_cb_t complete_cb, uintptr_t user_data);
Expand Down
Loading

1 comment on commit 4795cca

@MatiMcFly
Copy link

@MatiMcFly MatiMcFly commented on 4795cca Sep 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest moving line 41 in tusb.h to

  #if CFG_TUH_HID
    #include "class/hid/hid_host.h"
  #endif

and/or

  #if CFG_TUD_HID
    #include "class/hid/hid_device.h"
  #endif

Not doing so leads to an unnecessary include if the HID class is not used.

Please sign in to comment.