Skip to content

Commit 2cd73ca

Browse files
committed
Add host string descriptor functions
Plus typo fixes, GCC11 array bounds fix, snprintf for malloc-free debug and pragmas for alignment checks.
1 parent 7c627f5 commit 2cd73ca

File tree

7 files changed

+78
-4
lines changed

7 files changed

+78
-4
lines changed

src/common/tusb_common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ static inline const char* tu_lookup_find(tu_lookup_table_t const* p_table, uint3
349349
}
350350

351351
// not found return the key value in hex
352-
sprintf(not_found, "0x%08lX", (unsigned long) key);
352+
snprintf(not_found, sizeof(not_found), "0x%08lX", (unsigned long) key);
353353

354354
return not_found;
355355
}

src/host/usbh.c

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,60 @@ bool tuh_vid_pid_get(uint8_t dev_addr, uint16_t* vid, uint16_t* pid)
258258
return true;
259259
}
260260

261+
uint8_t tuh_i_manufacturer_get(uint8_t dev_addr) {
262+
TU_VERIFY(tuh_mounted(dev_addr));
263+
usbh_device_t const* dev = get_device(dev_addr);
264+
265+
return dev->i_manufacturer;
266+
}
267+
268+
uint8_t tuh_i_serial_get(uint8_t dev_addr) {
269+
TU_VERIFY(tuh_mounted(dev_addr));
270+
usbh_device_t const* dev = get_device(dev_addr);
271+
272+
return dev->i_serial;
273+
}
274+
275+
uint8_t tuh_i_product_get(uint8_t dev_addr) {
276+
TU_VERIFY(tuh_mounted(dev_addr));
277+
usbh_device_t const* dev = get_device(dev_addr);
278+
279+
return dev->i_product;
280+
}
281+
282+
static tuh_complete_cb_t string_get_cb;
283+
284+
static bool string_get_complete (uint8_t dev_addr, tusb_control_request_t const * request, xfer_result_t result) {
285+
if (string_get_cb != NULL) {
286+
TU_LOG2("string get done %d\r\n", result);
287+
string_get_cb(result);
288+
}
289+
string_get_cb = NULL;
290+
return true;
291+
}
292+
293+
bool tuh_string_get(uint8_t dev_addr, uint8_t string_index, uint16_t* buf, size_t len, tuh_complete_cb_t complete_cb) {
294+
if (string_get_cb != NULL) {
295+
return false;
296+
}
297+
tusb_control_request_t const request =
298+
{
299+
.bmRequestType_bit =
300+
{
301+
.recipient = TUSB_REQ_RCPT_DEVICE,
302+
.type = TUSB_REQ_TYPE_STANDARD,
303+
.direction = TUSB_DIR_IN
304+
},
305+
.bRequest = TUSB_REQ_GET_DESCRIPTOR,
306+
.wValue = TUSB_DESC_STRING << 8 | string_index,
307+
.wIndex = 0,
308+
.wLength = len * sizeof(uint16_t)
309+
};
310+
string_get_cb = complete_cb;
311+
TU_ASSERT( tuh_control_xfer(dev_addr, &request, buf, string_get_complete) );
312+
return true;
313+
}
314+
261315
tusb_speed_t tuh_speed_get (uint8_t dev_addr)
262316
{
263317
return (tusb_speed_t) (dev_addr ? get_device(dev_addr)->speed : _dev0.speed);
@@ -561,6 +615,7 @@ void process_device_unplugged(uint8_t rhport, uint8_t hub_addr, uint8_t hub_port
561615
tu_memclr(dev->ep_status, sizeof(dev->ep_status));
562616

563617
dev->state = TUSB_DEVICE_STATE_UNPLUG;
618+
dev->configured = false;
564619
}
565620
}
566621
}
@@ -609,7 +664,7 @@ void usbh_driver_set_config_complete(uint8_t dev_addr, uint8_t itf_num)
609664

610665
//--------------------------------------------------------------------+
611666
// Enumeration Process
612-
// is a lengthy process with a seires of control transfer to configure
667+
// is a lengthy process with a series of control transfer to configure
613668
// newly attached device. Each step is handled by a function in this
614669
// section
615670
// TODO due to the shared _usbh_ctrl_buf, we must complete enumerating

src/host/usbh.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
// MACRO CONSTANT TYPEDEF
3939
//--------------------------------------------------------------------+
4040

41+
typedef bool (*tuh_complete_cb_t)(xfer_result_t result);
4142
typedef bool (*tuh_control_complete_cb_t)(uint8_t dev_addr, tusb_control_request_t const * request, xfer_result_t result);
4243

4344
//--------------------------------------------------------------------+
@@ -58,6 +59,17 @@ extern void hcd_int_handler(uint8_t rhport);
5859
#define tuh_int_handler hcd_int_handler
5960

6061
bool tuh_vid_pid_get(uint8_t dev_addr, uint16_t* vid, uint16_t* pid);
62+
63+
// 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);
67+
68+
// Reads the string descriptor at the string index into the buffer. This is the
69+
// full response so the first entry is the length and the constant 0x03 for
70+
// 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);
72+
6173
tusb_speed_t tuh_speed_get(uint8_t dev_addr);
6274

6375
// Check if device is connected and configured

src/portable/chipidea/ci_hs/hcd_ci_hs.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
// INCLUDE
3636
//--------------------------------------------------------------------+
3737
#include "common/tusb_common.h"
38+
#include "host/hcd.h"
3839
#include "portable/ehci/ehci_api.h"
3940
#include "ci_hs_type.h"
4041

src/portable/ehci/ehci.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,10 @@ static void list_remove_qhd_by_addr(ehci_link_t* list_head, uint8_t dev_addr)
189189
prev = list_next(prev) )
190190
{
191191
// TODO check type for ISO iTD and siTD
192+
#pragma GCC diagnostic push
193+
#pragma GCC diagnostic ignored "-Wcast-align"
192194
ehci_qhd_t* qhd = (ehci_qhd_t*) list_next(prev);
195+
#pragma GCC diagnostic pop
193196
if ( qhd->dev_addr == dev_addr )
194197
{
195198
// TODO deactive all TD, wait for QHD to inactive before removal

src/portable/ehci/ehci.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ typedef struct
101101

102102
// Word 2: qTQ Token
103103
volatile uint32_t ping_err : 1 ; ///< For Highspeed: 0 Out, 1 Ping. Full/Slow used as error indicator
104-
volatile uint32_t non_hs_split_state : 1 ; ///< Used by HC to track the state of slipt transaction
105-
volatile uint32_t non_hs_missed_uframe : 1 ; ///< HC misses a complete slip transaction
104+
volatile uint32_t non_hs_split_state : 1 ; ///< Used by HC to track the state of split transaction
105+
volatile uint32_t non_hs_missed_uframe : 1 ; ///< HC misses a complete split transaction
106106
volatile uint32_t xact_err : 1 ; ///< Error (Timeout, CRC, Bad PID ... )
107107
volatile uint32_t babble_err : 1 ; ///< Babble detected, also set Halted bit to 1
108108
volatile uint32_t buffer_err : 1 ; ///< Data overrun/underrun error

src/portable/raspberrypi/rp2040/rp2040_usb.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,11 @@ void rp2040_usb_init(void)
5858
unreset_block_wait(RESETS_RESET_USBCTRL_BITS);
5959

6060
// Clear any previous state just in case
61+
#pragma GCC diagnostic push
62+
#pragma GCC diagnostic ignored "-Warray-bounds"
6163
memset(usb_hw, 0, sizeof(*usb_hw));
6264
memset(usb_dpram, 0, sizeof(*usb_dpram));
65+
#pragma GCC diagnostic pop
6366

6467
// Mux the controller to the onboard usb phy
6568
usb_hw->muxing = USB_USB_MUXING_TO_PHY_BITS | USB_USB_MUXING_SOFTCON_BITS;

0 commit comments

Comments
 (0)