Skip to content

Commit 9442059

Browse files
committed
[nrf fromtree] usb: device_next: Do not add serial number without HWINFO
For many devices iSerialNumber is not required. The only device class currently supported in Zephyr that requires iSerialNumber is MSC. If the serial number is not available then iSerialNumber must be 0. Failure to read the string descriptor for non-zero iSerialNumber fails USB Command Verifier Chapter 9 tests. When USBD_DESC_SERIAL_NUMBER_DEFINE() was used without CONFIG_HWINFO then it did lead to runtime failure when requesting the string (thus failing certification). Fail USBD_DESC_SERIAL_NUMBER_DEFINE() at build-time if CONFIG_HWINFO is not set to prevent the surprise at runtime. Only define serial number descriptors if CONFIG_HWINFO is enabled. Signed-off-by: Tomasz Moń <[email protected]> (cherry picked from commit 7510970)
1 parent 399363a commit 9442059

File tree

4 files changed

+14
-6
lines changed

4 files changed

+14
-6
lines changed

include/zephyr/usb/usbd.h

+1
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,7 @@ static inline void *usbd_class_get_private(const struct usbd_class_data *const c
631631
* @param d_name String descriptor node identifier.
632632
*/
633633
#define USBD_DESC_SERIAL_NUMBER_DEFINE(d_name) \
634+
BUILD_ASSERT(IS_ENABLED(CONFIG_HWINFO), "HWINFO not enabled"); \
634635
static struct usbd_desc_node d_name = { \
635636
.str = { \
636637
.utype = USBD_DUT_STRING_SERIAL_NUMBER, \

samples/subsys/usb/common/sample_usbd_init.c

+5-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ USBD_DEVICE_DEFINE(sample_usbd,
3030
USBD_DESC_LANG_DEFINE(sample_lang);
3131
USBD_DESC_MANUFACTURER_DEFINE(sample_mfr, CONFIG_SAMPLE_USBD_MANUFACTURER);
3232
USBD_DESC_PRODUCT_DEFINE(sample_product, CONFIG_SAMPLE_USBD_PRODUCT);
33-
USBD_DESC_SERIAL_NUMBER_DEFINE(sample_sn);
33+
IF_ENABLED(CONFIG_HWINFO, (USBD_DESC_SERIAL_NUMBER_DEFINE(sample_sn)));
34+
3435
/* doc string instantiation end */
3536

3637
USBD_DESC_CONFIG_DEFINE(fs_cfg_desc, "FS Configuration");
@@ -109,7 +110,9 @@ struct usbd_context *sample_usbd_setup_device(usbd_msg_cb_t msg_cb)
109110
return NULL;
110111
}
111112

112-
err = usbd_add_descriptor(&sample_usbd, &sample_sn);
113+
IF_ENABLED(CONFIG_HWINFO, (
114+
err = usbd_add_descriptor(&sample_usbd, &sample_sn);
115+
))
113116
if (err) {
114117
LOG_ERR("Failed to initialize SN descriptor (%d)", err);
115118
return NULL;

subsys/usb/device_next/app/cdc_acm_serial.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ USBD_DEVICE_DEFINE(cdc_acm_serial,
2727
USBD_DESC_LANG_DEFINE(cdc_acm_serial_lang);
2828
USBD_DESC_MANUFACTURER_DEFINE(cdc_acm_serial_mfr, CONFIG_CDC_ACM_SERIAL_MANUFACTURER_STRING);
2929
USBD_DESC_PRODUCT_DEFINE(cdc_acm_serial_product, CONFIG_CDC_ACM_SERIAL_PRODUCT_STRING);
30-
USBD_DESC_SERIAL_NUMBER_DEFINE(cdc_acm_serial_sn);
30+
IF_ENABLED(CONFIG_HWINFO, (USBD_DESC_SERIAL_NUMBER_DEFINE(cdc_acm_serial_sn)));
3131

3232
USBD_DESC_CONFIG_DEFINE(fs_cfg_desc, "FS Configuration");
3333
USBD_DESC_CONFIG_DEFINE(hs_cfg_desc, "HS Configuration");
@@ -95,7 +95,9 @@ static int cdc_acm_serial_init_device(void)
9595
return err;
9696
}
9797

98-
err = usbd_add_descriptor(&cdc_acm_serial, &cdc_acm_serial_sn);
98+
IF_ENABLED(CONFIG_HWINFO, (
99+
err = usbd_add_descriptor(&cdc_acm_serial, &cdc_acm_serial_sn);
100+
))
99101
if (err) {
100102
LOG_ERR("Failed to initialize SN descriptor (%d)", err);
101103
return err;

subsys/usb/device_next/usbd_shell.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ static struct usbd_shell_speed {
4343
USBD_DESC_LANG_DEFINE(lang);
4444
USBD_DESC_MANUFACTURER_DEFINE(mfr, "ZEPHYR");
4545
USBD_DESC_PRODUCT_DEFINE(product, "Zephyr USBD foobaz");
46-
USBD_DESC_SERIAL_NUMBER_DEFINE(sn);
46+
IF_ENABLED(CONFIG_HWINFO, (USBD_DESC_SERIAL_NUMBER_DEFINE(sn)));
4747

4848
/* Default device descriptors and context used in the shell. */
4949
USBD_DEVICE_DEFINE(sh_uds_ctx, DEVICE_DT_GET(DT_NODELABEL(zephyr_udc0)),
@@ -117,7 +117,9 @@ static int cmd_usbd_default_strings(const struct shell *sh,
117117
err = usbd_add_descriptor(my_uds_ctx, &lang);
118118
err |= usbd_add_descriptor(my_uds_ctx, &mfr);
119119
err |= usbd_add_descriptor(my_uds_ctx, &product);
120-
err |= usbd_add_descriptor(my_uds_ctx, &sn);
120+
IF_ENABLED(CONFIG_HWINFO, (
121+
err |= usbd_add_descriptor(my_uds_ctx, &sn);
122+
))
121123

122124
if (err) {
123125
shell_error(sh, "dev: Failed to add default string descriptors, %d", err);

0 commit comments

Comments
 (0)