Skip to content

Commit ed4c27b

Browse files
jfischer-nofabiobaltieri
authored andcommitted
usb: device_next: allow BOS support to be disabled
Disabling it if not needed can save about 400 bytes of flash memory. Signed-off-by: Johann Fischer <[email protected]>
1 parent 3400aaf commit ed4c27b

File tree

6 files changed

+33
-10
lines changed

6 files changed

+33
-10
lines changed

include/zephyr/usb/usbd.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,11 +667,15 @@ static inline void *usbd_class_get_private(const struct usbd_class_data *const c
667667
* The application defines a BOS capability descriptor node for descriptors
668668
* such as USB 2.0 Extension Descriptor.
669669
*
670+
* @note It requires Kconfig options USBD_BOS_SUPPORT to be enabled.
671+
*
670672
* @param name Descriptor node identifier
671673
* @param len Device Capability descriptor length
672674
* @param subset Pointer to a Device Capability descriptor
673675
*/
674676
#define USBD_DESC_BOS_DEFINE(name, len, subset) \
677+
BUILD_ASSERT(IS_ENABLED(CONFIG_USBD_BOS_SUPPORT), \
678+
"USB device BOS support is disabled"); \
675679
static struct usbd_desc_node name = { \
676680
.bos = { \
677681
.utype = USBD_DUT_BOS_NONE, \
@@ -713,6 +717,8 @@ static inline void *usbd_class_get_private(const struct usbd_class_data *const c
713717
* @param vto_dev Vendor callback for to-device direction request
714718
*/
715719
#define USBD_DESC_BOS_VREQ_DEFINE(name, len, subset, vcode, vto_host, vto_dev) \
720+
BUILD_ASSERT(IS_ENABLED(CONFIG_USBD_BOS_SUPPORT), \
721+
"USB device BOS support is disabled"); \
716722
USBD_VREQUEST_DEFINE(vreq_nd_##name, vcode, vto_host, vto_dev); \
717723
static struct usbd_desc_node name = { \
718724
.bos = { \

samples/subsys/usb/common/Kconfig.sample_usbd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ config SAMPLE_USBD_MAX_POWER
4747

4848
config SAMPLE_USBD_20_EXTENSION_DESC
4949
bool "Use default USB 2.0 Extension Descriptor"
50+
depends on USBD_BOS_SUPPORT
5051
help
5152
Set bcdUSB value to 0201 and use default USB 2.0 Extension Descriptor.
5253

samples/subsys/usb/common/sample_usbd_init.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ USBD_CONFIGURATION_DEFINE(sample_hs_config,
6060
CONFIG_SAMPLE_USBD_MAX_POWER, &hs_cfg_desc);
6161
/* doc configuration instantiation end */
6262

63+
#if CONFIG_SAMPLE_USBD_20_EXTENSION_DESC
6364
/*
6465
* This does not yet provide valuable information, but rather serves as an
6566
* example, and will be improved in the future.
@@ -72,6 +73,7 @@ static const struct usb_bos_capability_lpm bos_cap_lpm = {
7273
};
7374

7475
USBD_DESC_BOS_DEFINE(sample_usbext, sizeof(bos_cap_lpm), &bos_cap_lpm);
76+
#endif
7577

7678
static void sample_fix_code_triple(struct usbd_context *uds_ctx,
7779
const enum usbd_speed speed)
@@ -175,16 +177,16 @@ struct usbd_context *sample_usbd_setup_device(usbd_msg_cb_t msg_cb)
175177
/* doc device init-and-msg end */
176178
}
177179

178-
if (IS_ENABLED(CONFIG_SAMPLE_USBD_20_EXTENSION_DESC)) {
179-
(void)usbd_device_set_bcd_usb(&sample_usbd, USBD_SPEED_FS, 0x0201);
180-
(void)usbd_device_set_bcd_usb(&sample_usbd, USBD_SPEED_HS, 0x0201);
180+
#if CONFIG_SAMPLE_USBD_20_EXTENSION_DESC
181+
(void)usbd_device_set_bcd_usb(&sample_usbd, USBD_SPEED_FS, 0x0201);
182+
(void)usbd_device_set_bcd_usb(&sample_usbd, USBD_SPEED_HS, 0x0201);
181183

182-
err = usbd_add_descriptor(&sample_usbd, &sample_usbext);
183-
if (err) {
184-
LOG_ERR("Failed to add USB 2.0 Extension Descriptor");
185-
return NULL;
186-
}
184+
err = usbd_add_descriptor(&sample_usbd, &sample_usbext);
185+
if (err) {
186+
LOG_ERR("Failed to add USB 2.0 Extension Descriptor");
187+
return NULL;
187188
}
189+
#endif
188190

189191
return &sample_usbd;
190192
}

subsys/usb/device_next/Kconfig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ config USBD_MAX_SPEED
3636
default 0 if USBD_MAX_SPEED_FULL
3737
default 1 if USBD_MAX_SPEED_HIGH
3838

39+
config USBD_BOS_SUPPORT
40+
bool "USB device BOS support"
41+
default y
42+
help
43+
BOS support can be disabled if the application does not use a BOS
44+
descriptor.
45+
3946
config USBD_SHELL
4047
bool "USB device shell"
4148
depends on SHELL

subsys/usb/device_next/usbd_ch9.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,11 @@ static int sreq_get_desc_bos(struct usbd_context *const uds_ctx,
733733
struct usbd_desc_node *desc_nd;
734734
size_t len;
735735

736+
if (!IS_ENABLED(CONFIG_USBD_BOS_SUPPORT)) {
737+
errno = -ENOTSUP;
738+
return 0;
739+
}
740+
736741
switch (usbd_bus_speed(uds_ctx)) {
737742
case USBD_SPEED_FS:
738743
dev_dsc = uds_ctx->fs_desc;

subsys/usb/device_next/usbd_desc.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ struct usbd_desc_node *usbd_get_descriptor(struct usbd_context *const uds_ctx,
9393
}
9494
}
9595

96-
if (desc_nd->bDescriptorType == USB_DESC_BOS) {
96+
if (IS_ENABLED(CONFIG_USBD_BOS_SUPPORT) &&
97+
desc_nd->bDescriptorType == USB_DESC_BOS) {
9798
return desc_nd;
9899
}
99100
}
@@ -142,7 +143,8 @@ int usbd_add_descriptor(struct usbd_context *const uds_ctx,
142143
goto add_descriptor_error;
143144
}
144145

145-
if (desc_nd->bDescriptorType == USB_DESC_BOS) {
146+
if (IS_ENABLED(CONFIG_USBD_BOS_SUPPORT) &&
147+
desc_nd->bDescriptorType == USB_DESC_BOS) {
146148
if (desc_nd->bos.utype == USBD_DUT_BOS_VREQ) {
147149
ret = usbd_device_register_vreq(uds_ctx, desc_nd->bos.vreq_nd);
148150
if (ret) {

0 commit comments

Comments
 (0)