summaryrefslogtreecommitdiff
path: root/src/host
diff options
context:
space:
mode:
authorhathach <[email protected]>2025-11-18 09:36:16 +0700
committerhathach <[email protected]>2025-11-18 09:36:16 +0700
commit98f7b0c7ba03fd770616195a6312e1bb65e5155f (patch)
tree6dce3fafc3794fd3cd1f13bd0cc04bfb84989e24 /src/host
parentbeea882c6bcee352b5ff69446aef9fc43237bab0 (diff)
parent66c84528f67c0eedc23d25221500d820e702d93f (diff)
Merge branch 'master' into fork/HiFiPhile/dwc2_iso_incomp
Diffstat (limited to 'src/host')
-rw-r--r--src/host/hcd.h6
-rw-r--r--src/host/usbh.c130
-rw-r--r--src/host/usbh.h6
-rw-r--r--src/host/usbh_pvt.h4
4 files changed, 75 insertions, 71 deletions
diff --git a/src/host/hcd.h b/src/host/hcd.h
index d3551bf5b..36a7f5da5 100644
--- a/src/host/hcd.h
+++ b/src/host/hcd.h
@@ -24,8 +24,8 @@
* This file is part of the TinyUSB stack.
*/
-#ifndef _TUSB_HCD_H_
-#define _TUSB_HCD_H_
+#ifndef TUSB_HCD_H_
+#define TUSB_HCD_H_
#include "common/tusb_common.h"
#include "osal/osal.h"
@@ -84,7 +84,7 @@ typedef struct {
// FUNC_CALL
struct {
- void (*func) (void*);
+ void (*func) (void* param);
void* param;
}func_call;
};
diff --git a/src/host/usbh.c b/src/host/usbh.c
index 6bafde368..734024771 100644
--- a/src/host/usbh.c
+++ b/src/host/usbh.c
@@ -292,13 +292,17 @@ static uint8_t _app_driver_count = 0;
#define TOTAL_DRIVER_COUNT (_app_driver_count + BUILTIN_DRIVER_COUNT)
-static inline usbh_class_driver_t const *get_driver(uint8_t drv_id) {
+// virtually joins built-in and application drivers together.
+// Application is positioned first to allow overwriting built-in ones.
+TU_ATTR_ALWAYS_INLINE static inline usbh_class_driver_t const *get_driver(uint8_t drv_id) {
usbh_class_driver_t const *driver = NULL;
-
- if ( drv_id < _app_driver_count ) {
+ if (drv_id < _app_driver_count) {
driver = &_app_driver[drv_id];
- } else if ( drv_id < TOTAL_DRIVER_COUNT && BUILTIN_DRIVER_COUNT > 0) {
- driver = &usbh_class_drivers[drv_id - _app_driver_count];
+ } else {
+ drv_id -= _app_driver_count;
+ if (drv_id < BUILTIN_DRIVER_COUNT) {
+ driver = &usbh_class_drivers[drv_id];
+ }
}
return driver;
@@ -318,7 +322,7 @@ TU_ATTR_ALWAYS_INLINE static inline usbh_device_t* get_device(uint8_t dev_addr)
}
TU_ATTR_ALWAYS_INLINE static inline bool is_hub_addr(uint8_t daddr) {
- return (CFG_TUH_HUB > 0) && (daddr > CFG_TUH_DEVICE_MAX);
+ return (CFG_TUH_HUB > 0) && (daddr > CFG_TUH_DEVICE_MAX); //-V560
}
TU_ATTR_ALWAYS_INLINE static inline bool queue_event(hcd_event_t const * event, bool in_isr) {
@@ -372,7 +376,8 @@ bool tuh_connected(uint8_t daddr) {
return _usbh_data.enumerating_daddr == 0;
} else {
const usbh_device_t* dev = get_device(daddr);
- return dev && dev->connected;
+ TU_VERIFY(dev != NULL);
+ return dev->connected;
}
}
@@ -439,8 +444,8 @@ bool tuh_configure(uint8_t rhport, uint32_t cfg_id, const void *cfg_param) {
static void clear_device(usbh_device_t* dev) {
tu_memclr(dev, sizeof(usbh_device_t));
- memset(dev->itf2drv, TUSB_INDEX_INVALID_8, sizeof(dev->itf2drv)); // invalid mapping
- memset(dev->ep2drv , TUSB_INDEX_INVALID_8, sizeof(dev->ep2drv )); // invalid mapping
+ (void) memset(dev->itf2drv, TUSB_INDEX_INVALID_8, sizeof(dev->itf2drv)); // invalid mapping
+ (void) memset(dev->ep2drv , TUSB_INDEX_INVALID_8, sizeof(dev->ep2drv )); // invalid mapping
}
bool tuh_inited(void) {
@@ -510,7 +515,7 @@ bool tuh_rhport_init(uint8_t rhport, const tusb_rhport_init_t* rh_init) {
// Class drivers
for (uint8_t drv_id = 0; drv_id < TOTAL_DRIVER_COUNT; drv_id++) {
usbh_class_driver_t const* driver = get_driver(drv_id);
- if (driver) {
+ if (driver != NULL) {
TU_LOG_USBH("%s init\r\n", driver->name);
driver->init();
}
@@ -657,7 +662,7 @@ void tuh_task_ext(uint32_t timeout_ms, bool in_isr) {
// with enabled driver e.g HID endpoint
#if CFG_TUH_API_EDPT_XFER
tuh_xfer_cb_t const complete_cb = dev->ep_callback[epnum][ep_dir].complete_cb;
- if ( complete_cb ) {
+ if (complete_cb != NULL) {
// re-construct xfer info
tuh_xfer_t xfer = {
.daddr = event.dev_addr,
@@ -675,7 +680,7 @@ void tuh_task_ext(uint32_t timeout_ms, bool in_isr) {
{
uint8_t drv_id = dev->ep2drv[epnum][ep_dir];
usbh_class_driver_t const* driver = get_driver(drv_id);
- if (driver) {
+ if (driver != NULL) {
TU_LOG_USBH(" %s xfer callback\r\n", driver->name);
driver->xfer_cb(event.dev_addr, ep_addr, (xfer_result_t) event.xfer_complete.result,
event.xfer_complete.len);
@@ -690,10 +695,13 @@ void tuh_task_ext(uint32_t timeout_ms, bool in_isr) {
}
case USBH_EVENT_FUNC_CALL:
- if (event.func_call.func) event.func_call.func(event.func_call.param);
+ if (event.func_call.func != NULL) {
+ event.func_call.func(event.func_call.param);
+ }
break;
default:
+ // unknown event
break;
}
@@ -743,7 +751,7 @@ bool tuh_control_xfer (tuh_xfer_t* xfer) {
tu_str_std_request[xfer->setup->bRequest] : "Class Request");
TU_LOG_BUF_USBH(xfer->setup, 8);
- if (xfer->complete_cb) {
+ if (xfer->complete_cb != NULL) {
TU_ASSERT(usbh_setup_send(daddr, (uint8_t const *) &_usbh_epbuf.request));
}else {
// blocking if complete callback is not provided
@@ -795,7 +803,7 @@ static void _control_xfer_complete(uint8_t daddr, xfer_result_t result) {
_control_set_xfer_stage(CONTROL_STAGE_IDLE);
- if (xfer_temp.complete_cb) {
+ if (xfer_temp.complete_cb != NULL) {
xfer_temp.complete_cb(&xfer_temp);
}
}
@@ -834,7 +842,7 @@ static bool usbh_control_xfer_cb (uint8_t daddr, uint8_t ep_addr, xfer_result_t
case XFER_RESULT_SUCCESS:
switch(ctrl_info->stage) {
case CONTROL_STAGE_SETUP:
- if (request->wLength) {
+ if (request->wLength > 0) {
// DATA stage: initial data toggle is always 1
_control_set_xfer_stage(CONTROL_STAGE_DATA);
const uint8_t ep_data = tu_edpt_addr(0, request->bmRequestType_bit.direction);
@@ -844,7 +852,7 @@ static bool usbh_control_xfer_cb (uint8_t daddr, uint8_t ep_addr, xfer_result_t
TU_ATTR_FALLTHROUGH;
case CONTROL_STAGE_DATA: {
- if (request->wLength) {
+ if (request->wLength > 0) {
TU_LOG_USBH("[%u:%u] Control data:\r\n", rhport, daddr);
TU_LOG_MEM_USBH(ctrl_info->buffer, xferred_bytes, 2);
}
@@ -1084,7 +1092,7 @@ bool usbh_edpt_busy(uint8_t dev_addr, uint8_t ep_addr) {
bool tuh_bus_info_get(uint8_t daddr, tuh_bus_info_t* bus_info) {
usbh_device_t const* dev = get_device(daddr);
- if (dev) {
+ if (dev != NULL) {
*bus_info = dev->bus_info;
} else {
*bus_info = _usbh_data.dev0_bus;
@@ -1109,7 +1117,9 @@ TU_ATTR_FAST_FUNC void hcd_event_handler(hcd_event_t const* event, bool in_isr)
}
break;
- default: break;
+ default:
+ // nothing to do
+ break;
}
queue_event(event, in_isr);
@@ -1314,7 +1324,7 @@ static void process_removed_device(uint8_t rhport, uint8_t hub_addr, uint8_t hub
do {
for (uint8_t dev_id = 0; dev_id < TOTAL_DEVICES; dev_id++) {
usbh_device_t* dev = &_usbh_devices[dev_id];
- uint8_t const daddr = dev_id + 1;
+ uint8_t const daddr = dev_id + 1u;
// hub_addr = 0 means roothub, hub_port = 0 means all devices of downstream hub
if (dev->bus_info.rhport == rhport && dev->connected &&
@@ -1336,7 +1346,7 @@ static void process_removed_device(uint8_t rhport, uint8_t hub_addr, uint8_t hub
// Close class driver
for (uint8_t drv_id = 0; drv_id < TOTAL_DRIVER_COUNT; drv_id++) {
usbh_class_driver_t const* driver = get_driver(drv_id);
- if (driver) {
+ if (driver != NULL) {
driver->close(daddr);
}
}
@@ -1354,7 +1364,7 @@ static void process_removed_device(uint8_t rhport, uint8_t hub_addr, uint8_t hub
// find a marked hub to process
for (uint8_t h_id = 0; h_id < CFG_TUH_HUB; h_id++) {
- if (removing_hubs[h_id]) {
+ if (0 != removing_hubs[h_id]) {
removing_hubs[h_id] = 0;
// update hub_addr and hub_port for next loop
@@ -1423,16 +1433,13 @@ static bool enum_new_device(hcd_event_t* event) {
// wait until device connection is stable TODO non blocking
tusb_time_delay_ms_api(ENUM_DEBOUNCING_DELAY_MS);
- // clear roothub debouncing delay
- if (dev0_bus->hub_addr == 0) {
- _usbh_data.attach_debouncing_bm &= (uint8_t) ~TU_BIT(dev0_bus->rhport);
- }
-
if (dev0_bus->hub_addr == 0) {
// connected directly to roothub
// USB bus not active and frame number is not available yet.
// need to depend on tusb_time_millis_api() TODO non blocking
+ _usbh_data.attach_debouncing_bm &= (uint8_t) ~TU_BIT(dev0_bus->rhport); // clear roothub debouncing delay
+
if (!hcd_port_connect_status(dev0_bus->rhport)) {
TU_LOG_USBH("Device unplugged while debouncing\r\n");
enum_full_complete();
@@ -1503,7 +1510,7 @@ static void process_enumeration(tuh_xfer_t* xfer) {
usbh_device_t* dev = get_device(daddr);
tuh_bus_info_t* dev0_bus = &_usbh_data.dev0_bus;
if (daddr > 0) {
- TU_ASSERT(dev,);
+ TU_ASSERT(dev != NULL,);
}
uint16_t langid = 0x0409; // default is English
@@ -1513,7 +1520,7 @@ static void process_enumeration(tuh_xfer_t* xfer) {
hub_port_status_response_t port_status;
hub_port_get_status_local(dev0_bus->hub_addr, dev0_bus->hub_port, &port_status);
- if (!port_status.status.connection) {
+ if (0 == port_status.status.connection) {
TU_LOG_USBH("Device unplugged from hub while debouncing\r\n");
enum_full_complete();
return;
@@ -1535,7 +1542,7 @@ static void process_enumeration(tuh_xfer_t* xfer) {
hub_port_status_response_t port_status;
hub_port_get_status_local(dev0_bus->hub_addr, dev0_bus->hub_port, &port_status);
- if (port_status.change.reset) {
+ if (1 == port_status.change.reset) {
// Acknowledge Port Reset Change
TU_ASSERT(hub_port_clear_reset_change(dev0_bus->hub_addr, dev0_bus->hub_port, process_enumeration, ENUM_HUB_CLEAR_RESET_COMPLETE),);
} else {
@@ -1550,7 +1557,7 @@ static void process_enumeration(tuh_xfer_t* xfer) {
hub_port_status_response_t port_status;
hub_port_get_status_local(dev0_bus->hub_addr, dev0_bus->hub_port, &port_status);
- if (!port_status.status.connection) {
+ if (0 == port_status.status.connection) {
TU_LOG_USBH("Device unplugged from hub (not addressed yet)\r\n");
enum_full_complete();
return;
@@ -1619,17 +1626,18 @@ static void process_enumeration(tuh_xfer_t* xfer) {
case ENUM_GET_STRING_LANGUAGE_ID_LEN: {
// save the received device descriptor
tusb_desc_device_t const *desc_device = (tusb_desc_device_t const *) _usbh_epbuf.ctrl;
- dev->bcdUSB = desc_device->bcdUSB;
- dev->bDeviceClass = desc_device->bDeviceClass;
- dev->bDeviceSubClass = desc_device->bDeviceSubClass;
- dev->bDeviceProtocol = desc_device->bDeviceProtocol;
- dev->bMaxPacketSize0 = desc_device->bMaxPacketSize0;
- dev->idVendor = desc_device->idVendor;
- dev->idProduct = desc_device->idProduct;
- dev->bcdDevice = desc_device->bcdDevice;
- dev->iManufacturer = desc_device->iManufacturer;
- dev->iProduct = desc_device->iProduct;
- dev->iSerialNumber = desc_device->iSerialNumber;
+
+ dev->bcdUSB = desc_device->bcdUSB;
+ dev->bDeviceClass = desc_device->bDeviceClass;
+ dev->bDeviceSubClass = desc_device->bDeviceSubClass;
+ dev->bDeviceProtocol = desc_device->bDeviceProtocol;
+ dev->bMaxPacketSize0 = desc_device->bMaxPacketSize0;
+ dev->idVendor = desc_device->idVendor;
+ dev->idProduct = desc_device->idProduct;
+ dev->bcdDevice = desc_device->bcdDevice;
+ dev->iManufacturer = desc_device->iManufacturer;
+ dev->iProduct = desc_device->iProduct;
+ dev->iSerialNumber = desc_device->iSerialNumber;
dev->bNumConfigurations = desc_device->bNumConfigurations;
tuh_enum_descriptor_device_cb(daddr, desc_device); // callback
@@ -1654,9 +1662,8 @@ static void process_enumeration(tuh_xfer_t* xfer) {
tuh_descriptor_get_string(daddr, dev->iManufacturer, langid, _usbh_epbuf.ctrl, 2,
process_enumeration, ENUM_GET_STRING_MANUFACTURER);
break;
- }else {
- TU_ATTR_FALLTHROUGH;
}
+ TU_ATTR_FALLTHROUGH;
}
case ENUM_GET_STRING_MANUFACTURER: {
@@ -1666,22 +1673,21 @@ static void process_enumeration(tuh_xfer_t* xfer) {
tuh_descriptor_get_string(daddr, dev->iManufacturer, langid, _usbh_epbuf.ctrl, str_len,
process_enumeration, ENUM_GET_STRING_PRODUCT_LEN);
break;
- } else {
- TU_ATTR_FALLTHROUGH;
}
+ TU_ATTR_FALLTHROUGH;
}
- case ENUM_GET_STRING_PRODUCT_LEN:
+ case ENUM_GET_STRING_PRODUCT_LEN: {
if (dev->iProduct != 0) {
if (state == ENUM_GET_STRING_PRODUCT_LEN) {
langid = tu_le16toh(xfer->setup->wIndex); // get langid from previous setup packet if not fall through
}
- tuh_descriptor_get_string(daddr, dev->iProduct, langid, _usbh_epbuf.ctrl, 2,
- process_enumeration, ENUM_GET_STRING_PRODUCT);
+ tuh_descriptor_get_string(
+ daddr, dev->iProduct, langid, _usbh_epbuf.ctrl, 2, process_enumeration, ENUM_GET_STRING_PRODUCT);
break;
- } else {
- TU_ATTR_FALLTHROUGH;
}
+ TU_ATTR_FALLTHROUGH;
+ }
case ENUM_GET_STRING_PRODUCT: {
if (dev->iProduct != 0) {
@@ -1690,22 +1696,21 @@ static void process_enumeration(tuh_xfer_t* xfer) {
tuh_descriptor_get_string(daddr, dev->iProduct, langid, _usbh_epbuf.ctrl, str_len,
process_enumeration, ENUM_GET_STRING_SERIAL_LEN);
break;
- } else {
- TU_ATTR_FALLTHROUGH;
}
+ TU_ATTR_FALLTHROUGH;
}
- case ENUM_GET_STRING_SERIAL_LEN:
+ case ENUM_GET_STRING_SERIAL_LEN: {
if (dev->iSerialNumber != 0) {
if (state == ENUM_GET_STRING_SERIAL_LEN) {
langid = tu_le16toh(xfer->setup->wIndex); // get langid from previous setup packet if not fall through
}
- tuh_descriptor_get_string(daddr, dev->iSerialNumber, langid, _usbh_epbuf.ctrl, 2,
- process_enumeration, ENUM_GET_STRING_SERIAL);
+ tuh_descriptor_get_string(
+ daddr, dev->iSerialNumber, langid, _usbh_epbuf.ctrl, 2, process_enumeration, ENUM_GET_STRING_SERIAL);
break;
- } else {
- TU_ATTR_FALLTHROUGH;
}
+ TU_ATTR_FALLTHROUGH;
+ }
case ENUM_GET_STRING_SERIAL: {
if (dev->iSerialNumber != 0) {
@@ -1714,9 +1719,8 @@ static void process_enumeration(tuh_xfer_t* xfer) {
tuh_descriptor_get_string(daddr, dev->iSerialNumber, langid, _usbh_epbuf.ctrl, str_len,
process_enumeration, ENUM_GET_9BYTE_CONFIG_DESC);
break;
- } else {
- TU_ATTR_FALLTHROUGH;
}
+ TU_ATTR_FALLTHROUGH;
}
case ENUM_GET_9BYTE_CONFIG_DESC: {
@@ -1748,7 +1752,7 @@ static void process_enumeration(tuh_xfer_t* xfer) {
case ENUM_SET_CONFIG: {
uint8_t config_idx = (uint8_t) tu_le16toh(xfer->setup->wIndex);
if (tuh_enum_descriptor_configuration_cb(daddr, config_idx, (const tusb_desc_configuration_t*) _usbh_epbuf.ctrl)) {
- TU_ASSERT(tuh_configuration_set(daddr, config_idx+1, process_enumeration, ENUM_CONFIG_DRIVER),);
+ TU_ASSERT(tuh_configuration_set(daddr, config_idx+1u, process_enumeration, ENUM_CONFIG_DRIVER),);
} else {
config_idx++;
TU_ASSERT(config_idx < dev->bNumConfigurations,);
@@ -1794,7 +1798,7 @@ static uint8_t enum_get_new_address(bool is_hub) {
}
for (uint8_t idx = start; idx < end; idx++) {
- if (!_usbh_devices[idx].connected) {
+ if (0 == _usbh_devices[idx].connected) {
return (idx + 1);
}
}
@@ -1904,7 +1908,7 @@ void usbh_driver_set_config_complete(uint8_t dev_addr, uint8_t itf_num) {
// with usbh_driver_set_config_complete()
uint8_t const drv_id = dev->itf2drv[itf_num];
usbh_class_driver_t const * driver = get_driver(drv_id);
- if (driver) {
+ if (driver != NULL) {
TU_LOG_USBH("%s set config: itf = %u\r\n", driver->name, itf_num);
driver->set_config(dev_addr, itf_num);
break;
diff --git a/src/host/usbh.h b/src/host/usbh.h
index 8d48bf90d..4b6747848 100644
--- a/src/host/usbh.h
+++ b/src/host/usbh.h
@@ -24,8 +24,8 @@
* This file is part of the TinyUSB stack.
*/
-#ifndef _TUSB_USBH_H_
-#define _TUSB_USBH_H_
+#ifndef TUSB_USBH_H_
+#define TUSB_USBH_H_
#ifdef __cplusplus
extern "C" {
@@ -179,7 +179,7 @@ TU_ATTR_ALWAYS_INLINE static inline void tuh_task(void) {
// Check if there is pending events need processing by tuh_task()
bool tuh_task_event_ready(void);
-#ifndef _TUSB_HCD_H_
+#ifndef TUSB_HCD_H_
extern void hcd_int_handler(uint8_t rhport, bool in_isr);
#endif
diff --git a/src/host/usbh_pvt.h b/src/host/usbh_pvt.h
index 9d91e52e8..d722bb7e8 100644
--- a/src/host/usbh_pvt.h
+++ b/src/host/usbh_pvt.h
@@ -24,8 +24,8 @@
* This file is part of the TinyUSB stack.
*/
-#ifndef _TUSB_USBH_PVT_H_
-#define _TUSB_USBH_PVT_H_
+#ifndef TUSB_USBH_PVT_H_
+#define TUSB_USBH_PVT_H_
#include "osal/osal.h"
#include "common/tusb_fifo.h"