From 07d749a9fc4aac083742bee7d063865bdbb2098d Mon Sep 17 00:00:00 2001 From: Ren Chen Date: Tue, 18 Jun 2024 13:21:47 +0800 Subject: [PATCH] FROMPULL: usb: device: add USB_CONFIGURATION_STRING_DESC KConfig option This change adds a Kconfig option "USB_CONFIGURATION_STRING_DESC_ENABLE" to enable the USB configuration string descriptor. The default configuration string is specified in "USB_CONFIGURATION_STRING_DESC." Signed-off-by: Ren Chen Manaul cherry-pick of: https://github.com/zephyrproject-rtos/zephyr/pull/74917 BUG=b:327546783 BUG=b:347662534 BUG=b:351756600 TEST=emerge-staryu hammerd and Keyboard automatic upgrade pass Change-Id: If20aa1de4b6b2512525de1a5a34a7d18981e9f3c Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/zephyr/+/5633083 Reviewed-by: Fabio Baltieri Commit-Queue: Fabio Baltieri Tested-by: Fabio Baltieri --- subsys/usb/device/Kconfig | 17 +++++++++++++++++ subsys/usb/device/usb_descriptor.c | 24 ++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/subsys/usb/device/Kconfig b/subsys/usb/device/Kconfig index 1f0a008058e..80c71fb1635 100644 --- a/subsys/usb/device/Kconfig +++ b/subsys/usb/device/Kconfig @@ -48,6 +48,23 @@ config USB_DEVICE_SN Serial Number String will be derived from Hardware Information Driver (HWINFO). +config USB_CONFIGURATION_STRING_DESC_ENABLE + bool "USB configuration string descriptor support" + help + Enable string descriptor that describes the default configuration. + Since there is only one configuration, this string descriptor may not + provide much useful information. + +if USB_CONFIGURATION_STRING_DESC_ENABLE + +config USB_CONFIGURATION_STRING_DESC + string "USB configuration string descriptor" + default "Default Configuration" + help + String descriptor that describes the default configuration. + +endif # USB_CONFIGURATION_STRING_DESC_ENABLE + config USB_COMPOSITE_DEVICE bool "Use Interface Association Descriptor code triple" help diff --git a/subsys/usb/device/usb_descriptor.c b/subsys/usb/device/usb_descriptor.c index 99669d31e80..1fcbefa095a 100644 --- a/subsys/usb/device/usb_descriptor.c +++ b/subsys/usb/device/usb_descriptor.c @@ -46,6 +46,10 @@ struct common_descriptor { #define USB_DESC_PRODUCT_IDX 2 #define USB_DESC_SERIAL_NUMBER_IDX 3 +#ifdef CONFIG_USB_CONFIGURATION_STRING_DESC_ENABLE +#define USB_DESC_CONFIGURATION_IDX 4 +#endif + /* * Device and configuration descriptor placed in the device section, * no additional descriptor may be placed there. @@ -86,7 +90,11 @@ USBD_DEVICE_DESCR_DEFINE(primary) struct common_descriptor common_desc = { .wTotalLength = 0, .bNumInterfaces = 0, .bConfigurationValue = 1, +#ifdef CONFIG_USB_CONFIGURATION_STRING_DESC_ENABLE + .iConfiguration = USB_DESC_CONFIGURATION_IDX, +#else .iConfiguration = 0, +#endif .bmAttributes = USB_SCD_RESERVED | COND_CODE_1(CONFIG_USB_SELF_POWERED, (USB_SCD_SELF_POWERED), (0)) | @@ -116,6 +124,14 @@ struct usb_string_desription { uint8_t bDescriptorType; uint8_t bString[USB_BSTRING_LENGTH(CONFIG_USB_DEVICE_SN)]; } __packed utf16le_sn; + +#ifdef CONFIG_USB_CONFIGURATION_STRING_DESC_ENABLE + struct usb_conf_descriptor { + uint8_t bLength; + uint8_t bDescriptorType; + uint8_t bString[USB_BSTRING_LENGTH(CONFIG_USB_CONFIGURATION_STRING_DESC)]; + } __packed utf16le_conf; +#endif } __packed; /* @@ -149,6 +165,14 @@ USBD_STRING_DESCR_DEFINE(primary) struct usb_string_desription string_descr = { .bDescriptorType = USB_DESC_STRING, .bString = CONFIG_USB_DEVICE_SN, }, +#ifdef CONFIG_USB_CONFIGURATION_STRING_DESC_ENABLE + /* Configuration String Descriptor */ + .utf16le_conf = { + .bLength = USB_STRING_DESCRIPTOR_LENGTH(CONFIG_USB_CONFIGURATION_STRING_DESC), + .bDescriptorType = USB_DESC_STRING, + .bString = CONFIG_USB_CONFIGURATION_STRING_DESC, + }, +#endif }; /* This element marks the end of the entire descriptor. */