Skip to content

Commit 9b88cba

Browse files
committed
usb: add option to initialize USB device support at boot
Possibility to initialize USB device support is useful when only CDC ACM class is enabled and CDC ACM UART is used as backend for console, shell, or logging. Signed-off-by: Johann Fischer <[email protected]>
1 parent 623ace0 commit 9b88cba

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

subsys/usb/Kconfig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,12 @@ config USB_WORKQUEUE_PRIORITY
152152
priority. This means that any work handler, once started, won't
153153
be preempted by any other thread until finished.
154154

155+
config USB_DEVICE_INITIALIZE_AT_BOOT
156+
bool "Initialize USB device support at boot"
157+
depends on USB_CDC_ACM
158+
help
159+
Use CDC ACM UART as backend for console, shell, or logging.
160+
155161
source "subsys/usb/class/Kconfig"
156162

157163
endif # USB_DEVICE_STACK

subsys/usb/usb_device.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1551,7 +1551,8 @@ int usb_enable(usb_dc_status_callback status_cb)
15511551
k_mutex_lock(&usb_enable_lock, K_FOREVER);
15521552

15531553
if (usb_dev.enabled == true) {
1554-
ret = 0;
1554+
LOG_WRN("USB device support already enabled");
1555+
ret = -EALREADY;
15551556
goto out;
15561557
}
15571558

@@ -1636,9 +1637,10 @@ int usb_enable(usb_dc_status_callback status_cb)
16361637
static int usb_device_init(const struct device *dev)
16371638
{
16381639
uint8_t *device_descriptor;
1640+
int ret;
16391641

16401642
if (usb_dev.enabled == true) {
1641-
return 0;
1643+
return -EALREADY;
16421644
}
16431645

16441646
/* register device descriptor */
@@ -1650,7 +1652,14 @@ static int usb_device_init(const struct device *dev)
16501652

16511653
usb_set_config(device_descriptor);
16521654

1655+
if (IS_ENABLED(CONFIG_USB_DEVICE_INITIALIZE_AT_BOOT)) {
1656+
ret = usb_enable(NULL);
1657+
if (ret) {
1658+
return ret;
1659+
}
1660+
}
1661+
16531662
return 0;
16541663
}
16551664

1656-
SYS_INIT(usb_device_init, POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEVICE);
1665+
SYS_INIT(usb_device_init, POST_KERNEL, CONFIG_APPLICATION_INIT_PRIORITY);

0 commit comments

Comments
 (0)