From a07bbdb721fa0399a5b821f286f0b705713c3422 Mon Sep 17 00:00:00 2001 From: Josuah Demangeon Date: Fri, 7 Nov 2025 16:26:05 +0000 Subject: [PATCH 1/2] usb: host: add a usbh_device_set_address() function To complement usbh_req_set_address(), add a matching function usbh_device_set_address() that also takes care of updating the data structures. Signed-off-by: Josuah Demangeon --- subsys/usb/host/usbh_device.c | 29 ++++++++++++++++++++++------- subsys/usb/host/usbh_device.h | 3 +++ 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/subsys/usb/host/usbh_device.c b/subsys/usb/host/usbh_device.c index 92ecf6ae7aee1..3a7252eff6880 100644 --- a/subsys/usb/host/usbh_device.c +++ b/subsys/usb/host/usbh_device.c @@ -447,6 +447,27 @@ int usbh_device_set_configuration(struct usb_device *const udev, const uint8_t n return err; } +int usbh_device_set_address(struct usb_device *const udev, const uint8_t new_addr) +{ + int err; + + err = usbh_req_set_address(udev, new_addr); + if (err) { + LOG_ERR("Failed to set device address to 0x%02x", new_addr); + return err; + } + + udev->addr = new_addr; + + if (new_addr == 0) { + udev->state = USB_STATE_DEFAULT; + } else { + udev->state = USB_STATE_ADDRESSED; + } + + return 0; +} + int usbh_device_init(struct usb_device *const udev) { struct usbh_context *const uhs_ctx = udev->ctx; @@ -505,17 +526,11 @@ int usbh_device_init(struct usb_device *const udev) goto error; } - err = usbh_req_set_address(udev, new_addr); + err = usbh_device_set_address(udev, new_addr); if (err) { - LOG_ERR("Failed to set device address"); - udev->addr = 0; - goto error; } - udev->addr = new_addr; - udev->state = USB_STATE_ADDRESSED; - LOG_INF("New device with address %u state %u", udev->addr, udev->state); err = usbh_device_set_configuration(udev, 1); diff --git a/subsys/usb/host/usbh_device.h b/subsys/usb/host/usbh_device.h index 5c5637fb0bee8..560c7908afe87 100644 --- a/subsys/usb/host/usbh_device.h +++ b/subsys/usb/host/usbh_device.h @@ -36,6 +36,9 @@ int usbh_device_interface_set(struct usb_device *const udev, const uint8_t iface, const uint8_t alt, const bool dry); +/* Set USB device address */ +int usbh_device_set_address(struct usb_device *const udev, const uint8_t num); + /* Wrappers around to avoid glue UHC calls. */ static inline struct uhc_transfer *usbh_xfer_alloc(struct usb_device *udev, const uint8_t ep, From d2f31435a01756a888dac888d09fe26b73e91c2c Mon Sep 17 00:00:00 2001 From: Josuah Demangeon Date: Sun, 16 Nov 2025 02:28:02 +0000 Subject: [PATCH 2/2] usb: host: shell: call usbh_device_set_address() Instead of calling the low-level function from usbh_ch9.c, call the higher level function from usbh_device.c to update the address, making sure that the udev->address and udev->state are updated appropriately. Signed-off-by: Josuah Demangeon --- subsys/usb/host/usbh_shell.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/subsys/usb/host/usbh_shell.c b/subsys/usb/host/usbh_shell.c index 2832e644d081c..3e0bfd79daca6 100644 --- a/subsys/usb/host/usbh_shell.c +++ b/subsys/usb/host/usbh_shell.c @@ -561,7 +561,7 @@ static int cmd_device_address(const struct shell *sh, new_addr = strtol(argv[2], NULL, 10); - err = usbh_req_set_address(udev, new_addr); + err = usbh_device_set_address(udev, new_addr); if (err) { shell_error(sh, "host: Failed to set address"); } else {