Skip to content

Commit e08a875

Browse files
committed
add tuh_descriptor_get() and tuh_descriptor_device_get()
1 parent 3a7d1cf commit e08a875

File tree

4 files changed

+58
-47
lines changed

4 files changed

+58
-47
lines changed

src/common/tusb_common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#define TU_MIN(_x, _y) ( ( (_x) < (_y) ) ? (_x) : (_y) )
3939
#define TU_MAX(_x, _y) ( ( (_x) > (_y) ) ? (_x) : (_y) )
4040

41+
#define TU_U16(_high, _low) ((uint16_t) (((_high) << 8) | (_low)))
4142
#define TU_U16_HIGH(_u16) ((uint8_t) (((_u16) >> 8) & 0x00ff))
4243
#define TU_U16_LOW(_u16) ((uint8_t) ((_u16) & 0x00ff))
4344
#define U16_TO_U8S_BE(_u16) TU_U16_HIGH(_u16), TU_U16_LOW(_u16)

src/device/usbd.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,7 @@ bool tud_init (uint8_t rhport)
407407
if ( tud_inited() ) return true;
408408

409409
TU_LOG2("USBD init\r\n");
410+
TU_LOG2_INT(sizeof(usbd_device_t));
410411

411412
tu_varclr(&_usbd_dev);
412413

src/host/usbh.c

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ typedef struct {
9696
//------------- device -------------//
9797
volatile uint8_t state; // device state, value from enum tusbh_device_state_t
9898

99-
uint8_t itf2drv[16]; // map interface number to driver (0xff is invalid)
99+
uint8_t itf2drv[8]; // map interface number to driver (0xff is invalid)
100100
uint8_t ep2drv[CFG_TUH_ENDPOINT_MAX][2]; // map endpoint to driver ( 0xff is invalid )
101101

102102
struct TU_ATTR_PACKED
@@ -253,6 +253,34 @@ bool tuh_vid_pid_get(uint8_t dev_addr, uint16_t* vid, uint16_t* pid)
253253
return true;
254254
}
255255

256+
257+
bool tuh_descriptor_get(uint8_t daddr, uint8_t type, uint8_t index,
258+
void* buffer, uint16_t len, tuh_control_complete_cb_t complete_cb)
259+
{
260+
tusb_control_request_t const request =
261+
{
262+
.bmRequestType_bit =
263+
{
264+
.recipient = TUSB_REQ_RCPT_DEVICE,
265+
.type = TUSB_REQ_TYPE_STANDARD,
266+
.direction = TUSB_DIR_IN
267+
},
268+
.bRequest = TUSB_REQ_GET_DESCRIPTOR,
269+
.wValue = tu_htole16( TU_U16(type, index) ),
270+
.wIndex = 0,
271+
.wLength = len
272+
};
273+
274+
TU_ASSERT( tuh_control_xfer(daddr, &request, buffer, complete_cb) );
275+
276+
return true;
277+
}
278+
279+
bool tuh_descriptor_device_get(uint8_t daddr, void* buffer, uint16_t len, tuh_control_complete_cb_t complete_cb)
280+
{
281+
return tuh_descriptor_get(daddr, TUSB_DESC_DEVICE, 0, buffer, len, complete_cb);
282+
}
283+
256284
uint8_t tuh_i_manufacturer_get(uint8_t dev_addr) {
257285
TU_VERIFY(tuh_mounted(dev_addr));
258286
usbh_device_t const* dev = get_device(dev_addr);
@@ -338,7 +366,6 @@ bool tuh_init(uint8_t rhport)
338366
if (_usbh_initialized) return _usbh_initialized;
339367

340368
TU_LOG2("USBH init\r\n");
341-
342369
TU_LOG2_INT(sizeof(usbh_device_t));
343370

344371
tu_memclr(_usbh_devices, sizeof(_usbh_devices));
@@ -785,23 +812,10 @@ static bool enum_request_addr0_device_desc(void)
785812
uint8_t const addr0 = 0;
786813
TU_ASSERT( usbh_edpt_control_open(addr0, 8) );
787814

788-
//------------- Get first 8 bytes of device descriptor to get Control Endpoint Size -------------//
815+
// Get first 8 bytes of device descriptor for Control Endpoint size
789816
TU_LOG2("Get 8 byte of Device Descriptor\r\n");
790-
tusb_control_request_t const request =
791-
{
792-
.bmRequestType_bit =
793-
{
794-
.recipient = TUSB_REQ_RCPT_DEVICE,
795-
.type = TUSB_REQ_TYPE_STANDARD,
796-
.direction = TUSB_DIR_IN
797-
},
798-
.bRequest = TUSB_REQ_GET_DESCRIPTOR,
799-
.wValue = TUSB_DESC_DEVICE << 8,
800-
.wIndex = 0,
801-
.wLength = 8
802-
};
803-
TU_ASSERT( tuh_control_xfer(addr0, &request, _usbh_ctrl_buf, enum_get_addr0_device_desc_complete) );
804817

818+
TU_ASSERT(tuh_descriptor_device_get(addr0, _usbh_ctrl_buf, 8, enum_get_addr0_device_desc_complete));
805819
return true;
806820
}
807821

@@ -909,22 +923,8 @@ static bool enum_set_address_complete(uint8_t dev_addr, tusb_control_request_t c
909923

910924
// Get full device descriptor
911925
TU_LOG2("Get Device Descriptor\r\n");
912-
tusb_control_request_t const new_request =
913-
{
914-
.bmRequestType_bit =
915-
{
916-
.recipient = TUSB_REQ_RCPT_DEVICE,
917-
.type = TUSB_REQ_TYPE_STANDARD,
918-
.direction = TUSB_DIR_IN
919-
},
920-
.bRequest = TUSB_REQ_GET_DESCRIPTOR,
921-
.wValue = TUSB_DESC_DEVICE << 8,
922-
.wIndex = 0,
923-
.wLength = sizeof(tusb_desc_device_t)
924-
};
925-
926-
TU_ASSERT(tuh_control_xfer(new_addr, &new_request, _usbh_ctrl_buf, enum_get_device_desc_complete));
927926

927+
TU_ASSERT(tuh_descriptor_device_get(new_addr, _usbh_ctrl_buf, sizeof(tusb_desc_device_t), enum_get_device_desc_complete));
928928
return true;
929929
}
930930

src/host/usbh.h

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
//--------------------------------------------------------------------+
4040

4141
typedef bool (*tuh_complete_cb_t)(xfer_result_t result);
42-
typedef bool (*tuh_control_complete_cb_t)(uint8_t dev_addr, tusb_control_request_t const * request, xfer_result_t result);
42+
typedef bool (*tuh_control_complete_cb_t)(uint8_t daddr, tusb_control_request_t const * request, xfer_result_t result);
4343

4444
//--------------------------------------------------------------------+
4545
// APPLICATION API
@@ -58,51 +58,60 @@ void tuh_task(void);
5858
extern void hcd_int_handler(uint8_t rhport);
5959
#define tuh_int_handler hcd_int_handler
6060

61-
bool tuh_vid_pid_get(uint8_t dev_addr, uint16_t* vid, uint16_t* pid);
61+
//------------- descriptors -------------//
62+
63+
// Get an descriptor
64+
bool tuh_descriptor_get(uint8_t daddr, uint8_t type, uint8_t index,
65+
void* buffer, uint16_t len, tuh_control_complete_cb_t complete_cb);
66+
67+
// Get device descriptor
68+
bool tuh_descriptor_device_get(uint8_t daddr, void* buffer, uint16_t len, tuh_control_complete_cb_t complete_cb);
69+
70+
bool tuh_vid_pid_get(uint8_t daddr, uint16_t* vid, uint16_t* pid);
6271

6372
// Gets the string indices for common device descriptor data.
64-
uint8_t tuh_i_manufacturer_get(uint8_t dev_addr);
65-
uint8_t tuh_i_serial_get(uint8_t dev_addr);
66-
uint8_t tuh_i_product_get(uint8_t dev_addr);
73+
uint8_t tuh_i_manufacturer_get(uint8_t daddr);
74+
uint8_t tuh_i_serial_get(uint8_t daddr);
75+
uint8_t tuh_i_product_get(uint8_t daddr);
6776

6877
// Reads the string descriptor at the string index into the buffer. This is the
6978
// full response so the first entry is the length and the constant 0x03 for
7079
// string descriptor type.
71-
bool tuh_string_get(uint8_t dev_addr, uint8_t string_index, uint16_t* buf, size_t len, tuh_complete_cb_t complete_cb);
80+
bool tuh_string_get(uint8_t daddr, uint8_t string_index, uint16_t* buf, size_t len, tuh_complete_cb_t complete_cb);
7281

73-
tusb_speed_t tuh_speed_get(uint8_t dev_addr);
82+
tusb_speed_t tuh_speed_get(uint8_t daddr);
7483

7584
// Check if device is connected and configured
76-
bool tuh_mounted(uint8_t dev_addr);
85+
bool tuh_mounted(uint8_t daddr);
7786

7887
// Check if device is suspended
79-
static inline bool tuh_suspended(uint8_t dev_addr)
88+
static inline bool tuh_suspended(uint8_t daddr)
8089
{
8190
// TODO implement suspend & resume on host
82-
(void) dev_addr;
91+
(void) daddr;
8392
return false;
8493
}
8594

8695
// Check if device is ready to communicate with
8796
TU_ATTR_ALWAYS_INLINE
88-
static inline bool tuh_ready(uint8_t dev_addr)
97+
static inline bool tuh_ready(uint8_t daddr)
8998
{
90-
return tuh_mounted(dev_addr) && !tuh_suspended(dev_addr);
99+
return tuh_mounted(daddr) && !tuh_suspended(daddr);
91100
}
92101

93102
// Carry out control transfer
94-
bool tuh_control_xfer (uint8_t dev_addr, tusb_control_request_t const* request, void* buffer, tuh_control_complete_cb_t complete_cb);
103+
bool tuh_control_xfer (uint8_t daddr, tusb_control_request_t const* request, void* buffer, tuh_control_complete_cb_t complete_cb);
95104

96105
//--------------------------------------------------------------------+
97106
// APPLICATION CALLBACK
98107
//--------------------------------------------------------------------+
99108
//TU_ATTR_WEAK uint8_t tuh_attach_cb (tusb_desc_device_t const *desc_device);
100109

101110
// Invoked when device is mounted (configured)
102-
TU_ATTR_WEAK void tuh_mount_cb (uint8_t dev_addr);
111+
TU_ATTR_WEAK void tuh_mount_cb (uint8_t daddr);
103112

104113
/// Invoked when device is unmounted (bus reset/unplugged)
105-
TU_ATTR_WEAK void tuh_umount_cb(uint8_t dev_addr);
114+
TU_ATTR_WEAK void tuh_umount_cb(uint8_t daddr);
106115

107116
#ifdef __cplusplus
108117
}

0 commit comments

Comments
 (0)