From 3880a42375df7ce69cb0f5f3632e4b593cf38f52 Mon Sep 17 00:00:00 2001 From: Andrei Emeltchenko Date: Fri, 3 May 2019 16:55:34 +0300 Subject: [PATCH] usb: Align legacy and composite interface Remove duplicated execution path for composite configuration, USB device stack initialization is done inside stack for both cases. Signed-off-by: Andrei Emeltchenko --- include/usb/usb_device.h | 8 +- samples/subsys/usb/hid/src/main.c | 16 -- subsys/usb/class/bluetooth.c | 18 --- subsys/usb/class/cdc_acm.c | 19 --- subsys/usb/class/hid/core.c | 20 --- subsys/usb/class/loopback.c | 30 ---- subsys/usb/class/mass_storage.c | 19 --- subsys/usb/class/netusb/netusb.c | 28 ---- subsys/usb/usb_device.c | 237 +++++++---------------------- tests/subsys/usb/device/src/main.c | 4 +- 10 files changed, 61 insertions(+), 338 deletions(-) diff --git a/include/usb/usb_device.h b/include/usb/usb_device.h index 9792e1c1488..c6c4289c282 100644 --- a/include/usb/usb_device.h +++ b/include/usb/usb_device.h @@ -201,11 +201,11 @@ struct usb_cfg_data { * Function to configure USB controller. * Configuration parameters must be valid or an error is returned * - * @param[in] config Pointer to configuration structure + * @param[in] usb_descriptor USB descriptor table * * @return 0 on success, negative errno code on fail */ -int usb_set_config(struct usb_cfg_data *config); +int usb_set_config(const u8_t *usb_descriptor); /** * @brief Deconfigure USB controller @@ -224,11 +224,9 @@ int usb_deconfig(void); * it is now capable of transmitting and receiving on the USB bus and * of generating interrupts. * - * @param[in] config Pointer to configuration structure - * * @return 0 on success, negative errno code on fail. */ -int usb_enable(struct usb_cfg_data *config); +int usb_enable(void); /** * @brief Disable the USB device diff --git a/samples/subsys/usb/hid/src/main.c b/samples/subsys/usb/hid/src/main.c index ba40a303746..da95864246d 100644 --- a/samples/subsys/usb/hid/src/main.c +++ b/samples/subsys/usb/hid/src/main.c @@ -119,23 +119,8 @@ void main(void) LOG_DBG("Starting application"); k_delayed_work_init(&delayed_report_send, send_report); - -#ifndef CONFIG_USB_COMPOSITE_DEVICE - hdev = device_get_binding("HID_0"); - if (hdev == NULL) { - LOG_ERR("Cannot get USB HID Device"); - return; - } - - LOG_DBG("HID Device: dev %p", hdev); - - usb_hid_register_device(hdev, hid_report_desc, sizeof(hid_report_desc), - &ops); - usb_hid_init(hdev); -#endif } -#ifdef CONFIG_USB_COMPOSITE_DEVICE static int composite_pre_init(struct device *dev) { hdev = device_get_binding("HID_0"); @@ -153,4 +138,3 @@ static int composite_pre_init(struct device *dev) } SYS_INIT(composite_pre_init, APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEVICE); -#endif diff --git a/subsys/usb/class/bluetooth.c b/subsys/usb/class/bluetooth.c index dc899f4011a..23fa53404f9 100644 --- a/subsys/usb/class/bluetooth.c +++ b/subsys/usb/class/bluetooth.c @@ -313,24 +313,6 @@ static int bluetooth_init(struct device *dev) return ret; } -#ifndef CONFIG_USB_COMPOSITE_DEVICE - bluetooth_config.usb_device_description = - usb_get_device_descriptor(); - /* Initialize the USB driver with the right configuration */ - ret = usb_set_config(&bluetooth_config); - if (ret < 0) { - LOG_ERR("Failed to config USB"); - return ret; - } - - /* Enable USB driver */ - ret = usb_enable(&bluetooth_config); - if (ret < 0) { - LOG_ERR("Failed to enable USB"); - return ret; - } -#endif - k_thread_create(&rx_thread_data, rx_thread_stack, K_THREAD_STACK_SIZEOF(rx_thread_stack), (k_thread_entry_t)hci_rx_thread, NULL, NULL, NULL, diff --git a/subsys/usb/class/cdc_acm.c b/subsys/usb/class/cdc_acm.c index 2ae0cd6493d..5e7120689f7 100644 --- a/subsys/usb/class/cdc_acm.c +++ b/subsys/usb/class/cdc_acm.c @@ -522,25 +522,6 @@ static int cdc_acm_init(struct device *dev) LOG_DBG("Device dev %p dev_data %p cfg %p added to devlist %p", dev, dev_data, dev->config->config_info, &cdc_acm_data_devlist); -#ifndef CONFIG_USB_COMPOSITE_DEVICE - struct usb_cfg_data *cfg = (void *)dev->config->config_info; - - cfg->usb_device_description = usb_get_device_descriptor(); - - /* Initialize the USB driver with the right configuration */ - ret = usb_set_config(cfg); - if (ret < 0) { - LOG_ERR("Failed to config USB"); - return ret; - } - - /* Enable USB driver */ - ret = usb_enable(cfg); - if (ret < 0) { - LOG_ERR("Failed to enable USB"); - return ret; - } -#endif k_sem_init(&poll_wait_sem, 0, UINT_MAX); k_work_init(&dev_data->cb_work, cdc_acm_irq_callback_work_handler); k_work_init(&dev_data->tx_work, tx_work_handler); diff --git a/subsys/usb/class/hid/core.c b/subsys/usb/class/hid/core.c index 005c9233846..4fbba9b075f 100644 --- a/subsys/usb/class/hid/core.c +++ b/subsys/usb/class/hid/core.c @@ -657,26 +657,6 @@ int usb_hid_init(const struct device *dev) */ usb_set_hid_report_size(cfg, dev_data->report_size); -#ifndef CONFIG_USB_COMPOSITE_DEVICE - int ret; - - cfg->usb_device_description = usb_get_device_descriptor(); - - /* Initialize the USB driver with the right configuration */ - ret = usb_set_config(cfg); - if (ret < 0) { - LOG_ERR("Failed to config USB"); - return ret; - } - - /* Enable USB driver */ - ret = usb_enable(cfg); - if (ret < 0) { - LOG_ERR("Failed to enable USB"); - return ret; - } -#endif - return 0; } diff --git a/subsys/usb/class/loopback.c b/subsys/usb/class/loopback.c index 3c2c91ed88a..5af7ddc4fd9 100644 --- a/subsys/usb/class/loopback.c +++ b/subsys/usb/class/loopback.c @@ -180,33 +180,3 @@ USBD_CFG_DATA_DEFINE(loopback) struct usb_cfg_data loopback_config = { .endpoint = ep_cfg, }; /* usb.rst device config data end */ - -static int loopback_init(struct device *dev) -{ -#ifndef CONFIG_USB_COMPOSITE_DEVICE - int ret; - - loopback_config.usb_device_description = usb_get_device_descriptor(); - - /* usb.rst configure USB controller start */ - ret = usb_set_config(&loopback_config); - if (ret < 0) { - LOG_ERR("Failed to config USB"); - return ret; - } - /* usb.rst configure USB controller end */ - - /* usb.rst enable USB controller start */ - ret = usb_enable(&loopback_config); - if (ret < 0) { - LOG_ERR("Failed to enable USB"); - return ret; - } - /* usb.rst enable USB controller end */ -#endif - LOG_DBG(""); - - return 0; -} - -SYS_INIT(loopback_init, APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEVICE); diff --git a/subsys/usb/class/mass_storage.c b/subsys/usb/class/mass_storage.c index d637309f689..fa20f0fc25a 100644 --- a/subsys/usb/class/mass_storage.c +++ b/subsys/usb/class/mass_storage.c @@ -949,25 +949,6 @@ static int mass_storage_init(struct device *dev) msd_state_machine_reset(); msd_init(); -#ifndef CONFIG_USB_COMPOSITE_DEVICE - int ret; - - mass_storage_config.usb_device_description = - usb_get_device_descriptor(); - /* Initialize the USB driver with the right configuration */ - ret = usb_set_config(&mass_storage_config); - if (ret < 0) { - LOG_ERR("Failed to config USB"); - return ret; - } - - /* Enable USB driver */ - ret = usb_enable(&mass_storage_config); - if (ret < 0) { - LOG_ERR("Failed to enable USB"); - return ret; - } -#endif k_sem_init(&disk_wait_sem, 0, 1); /* Start a thread to offload disk ops */ diff --git a/subsys/usb/class/netusb/netusb.c b/subsys/usb/class/netusb/netusb.c index 9cc9aecf117..1edc2270a90 100644 --- a/subsys/usb/class/netusb/netusb.c +++ b/subsys/usb/class/netusb/netusb.c @@ -137,34 +137,6 @@ static void netusb_init(struct net_if *iface) net_if_down(iface); -#ifndef CONFIG_USB_COMPOSITE_DEVICE - /* Linker-defined symbols bound the USB descriptor structs */ - extern struct usb_cfg_data __usb_data_start[]; - extern struct usb_cfg_data __usb_data_end[]; - size_t size = (__usb_data_end - __usb_data_start); - - for (size_t i = 0; i < size; i++) { - struct usb_cfg_data *cfg = &(__usb_data_start[i]); - int ret; - - LOG_DBG("Registering function %u", i); - - cfg->usb_device_description = usb_get_device_descriptor(); - - ret = usb_set_config(cfg); - if (ret < 0) { - LOG_ERR("Failed to configure USB device"); - return; - } - - ret = usb_enable(cfg); - if (ret < 0) { - LOG_ERR("Failed to enable USB"); - return; - } - } -#endif /* CONFIG_USB_COMPOSITE_DEVICE */ - LOG_INF("netusb initialized"); } diff --git a/subsys/usb/usb_device.c b/subsys/usb/usb_device.c index 98daad18e1f..267eb3a38ad 100644 --- a/subsys/usb/usb_device.c +++ b/subsys/usb/usb_device.c @@ -146,8 +146,6 @@ static struct usb_dev_priv { s32_t data_buf_len; /** Installed custom request handler */ usb_request_handler custom_req_handler; - /** Installed vendor request handler */ - usb_request_handler vendor_req_handler; /** USB stack status clalback */ usb_dc_status_callback status_callback; /** Pointer to registered descriptors */ @@ -877,24 +875,6 @@ static int usb_handle_standard_request(struct usb_setup_packet *setup, return rc; } -static int usb_handle_vendor_request(struct usb_setup_packet *setup, - s32_t *len, u8_t **data_buf) -{ - LOG_DBG(""); - - if (usb_os_desc_enabled()) { - if (!usb_handle_os_desc_feature(setup, len, data_buf)) { - return 0; - } - } - - if (usb_dev.vendor_req_handler) { - return usb_dev.vendor_req_handler(setup, len, data_buf); - } - - return -ENOTSUP; -} - /* * @brief Registers a callback for custom device requests * @@ -1010,45 +990,6 @@ static int usb_vbus_set(bool on) return 0; } -int usb_set_config(struct usb_cfg_data *config) -{ - if (!config) { - return -EINVAL; - } - - /* register descriptors */ - usb_register_descriptors(config->usb_device_description); - - /* register standard request handler */ - usb_register_request_handler(REQTYPE_TYPE_STANDARD, - usb_handle_standard_request); - - /* register class request handlers for each interface*/ - if (config->interface.class_handler != NULL) { - usb_register_request_handler(REQTYPE_TYPE_CLASS, - config->interface.class_handler); - } - - /* register vendor request handler */ - if (config->interface.vendor_handler || usb_os_desc_enabled()) { - usb_register_request_handler(REQTYPE_TYPE_VENDOR, - usb_handle_vendor_request); - - if (config->interface.vendor_handler) { - usb_dev.vendor_req_handler = - config->interface.vendor_handler; - } - } - - /* register class request handlers for each interface*/ - if (config->interface.custom_handler != NULL) { - usb_register_custom_req_handler( - config->interface.custom_handler); - } - - return 0; -} - int usb_deconfig(void) { /* unregister descriptors */ @@ -1072,90 +1013,6 @@ int usb_deconfig(void) return 0; } -int usb_enable(struct usb_cfg_data *config) -{ - int ret; - u32_t i; - struct usb_dc_ep_cfg_data ep0_cfg; - - if (usb_dev.enabled == true) { - return 0; - } - - /* Enable VBUS if needed */ - ret = usb_vbus_set(true); - if (ret < 0) { - return ret; - } - - usb_register_status_callback(forward_status_cb); - usb_dc_set_status_callback(forward_status_cb); - - ret = usb_dc_attach(); - if (ret < 0) { - return ret; - } - - /* Configure control EP */ - ep0_cfg.ep_mps = USB_MAX_CTRL_MPS; - ep0_cfg.ep_type = USB_DC_EP_CONTROL; - - ep0_cfg.ep_addr = USB_CONTROL_OUT_EP0; - ret = usb_dc_ep_configure(&ep0_cfg); - if (ret < 0) { - return ret; - } - - ep0_cfg.ep_addr = USB_CONTROL_IN_EP0; - ret = usb_dc_ep_configure(&ep0_cfg); - if (ret < 0) { - return ret; - } - - /* Register endpoint 0 handlers*/ - ret = usb_dc_ep_set_callback(USB_CONTROL_OUT_EP0, - usb_handle_control_transfer); - if (ret < 0) { - return ret; - } - - ret = usb_dc_ep_set_callback(USB_CONTROL_IN_EP0, - usb_handle_control_transfer); - if (ret < 0) { - return ret; - } - - /* Register endpoint handlers*/ - for (i = 0U; i < config->num_endpoints; i++) { - ret = usb_dc_ep_set_callback(config->endpoint[i].ep_addr, - config->endpoint[i].ep_cb); - if (ret < 0) { - return ret; - } - } - - /* Init transfer slots */ - for (i = 0U; i < MAX_NUM_TRANSFERS; i++) { - k_work_init(&usb_dev.transfer[i].work, usb_transfer_work); - k_sem_init(&usb_dev.transfer[i].sem, 1, 1); - } - - /* Enable control EP */ - ret = usb_dc_ep_enable(USB_CONTROL_OUT_EP0); - if (ret < 0) { - return ret; - } - - ret = usb_dc_ep_enable(USB_CONTROL_IN_EP0); - if (ret < 0) { - return ret; - } - - usb_dev.enabled = true; - - return 0; -} - int usb_disable(void) { int ret; @@ -1502,8 +1359,6 @@ int usb_wakeup_request(void) } } -#ifdef CONFIG_USB_COMPOSITE_DEVICE - /* * The functions class_handler(), custom_handler() and vendor_handler() * go through the interfaces one after the other and compare the @@ -1608,46 +1463,36 @@ static int composite_setup_ep_cb(void) return 0; } -/* - * This function configures the USB device stack based on USB descriptor and - * usb_cfg_data. - */ -static int usb_composite_init(struct device *dev) +int usb_set_config(const u8_t *device_descriptor) { - int ret; - struct usb_dc_ep_cfg_data ep0_cfg; - u8_t *device_descriptor; - - if (usb_dev.enabled == true) { - return 0; - } - - /* register device descriptor */ - device_descriptor = usb_get_device_descriptor(); - if (!device_descriptor) { - LOG_ERR("Failed to configure USB device stack"); - return -1; - } - + /* register descriptors */ usb_register_descriptors(device_descriptor); /* register standard request handler */ usb_register_request_handler(REQTYPE_TYPE_STANDARD, - &(usb_handle_standard_request)); + usb_handle_standard_request); /* register class request handlers for each interface*/ - usb_register_request_handler(REQTYPE_TYPE_CLASS, - class_handler); + usb_register_request_handler(REQTYPE_TYPE_CLASS, class_handler); - /* register vendor request handlers */ - usb_register_request_handler(REQTYPE_TYPE_VENDOR, - vendor_handler); + /* register vendor request handler */ + usb_register_request_handler(REQTYPE_TYPE_VENDOR, vendor_handler); /* register class request handlers for each interface*/ usb_register_custom_req_handler(custom_handler); - usb_register_status_callback(forward_status_cb); - usb_dc_set_status_callback(forward_status_cb); + return 0; +} + +int usb_enable(void) +{ + int ret; + u32_t i; + struct usb_dc_ep_cfg_data ep0_cfg; + + if (usb_dev.enabled == true) { + return 0; + } /* Enable VBUS if needed */ ret = usb_vbus_set(true); @@ -1655,6 +1500,9 @@ static int usb_composite_init(struct device *dev) return ret; } + usb_register_status_callback(forward_status_cb); + usb_dc_set_status_callback(forward_status_cb); + ret = usb_dc_attach(); if (ret < 0) { return ret; @@ -1676,7 +1524,7 @@ static int usb_composite_init(struct device *dev) return ret; } - /*register endpoint 0 handlers*/ + /* Register endpoint 0 handlers*/ ret = usb_dc_ep_set_callback(USB_CONTROL_OUT_EP0, usb_handle_control_transfer); if (ret < 0) { @@ -1689,17 +1537,19 @@ static int usb_composite_init(struct device *dev) return ret; } - if (composite_setup_ep_cb()) { - return -1; + /* Register endpoint handlers*/ + ret = composite_setup_ep_cb(); + if (ret < 0) { + return ret; } - /* init transfer slots */ - for (int i = 0; i < MAX_NUM_TRANSFERS; i++) { + /* Init transfer slots */ + for (i = 0U; i < MAX_NUM_TRANSFERS; i++) { k_work_init(&usb_dev.transfer[i].work, usb_transfer_work); k_sem_init(&usb_dev.transfer[i].sem, 1, 1); } - /* enable control EP */ + /* Enable control EP */ ret = usb_dc_ep_enable(USB_CONTROL_OUT_EP0); if (ret < 0) { return ret; @@ -1715,5 +1565,30 @@ static int usb_composite_init(struct device *dev) return 0; } -SYS_INIT(usb_composite_init, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY); -#endif /* CONFIG_USB_COMPOSITE_DEVICE */ +/* + * This function configures the USB device stack based on USB descriptor and + * usb_cfg_data. + */ +static int usb_device_init(struct device *dev) +{ + u8_t *device_descriptor; + + if (usb_dev.enabled == true) { + return 0; + } + + /* register device descriptor */ + device_descriptor = usb_get_device_descriptor(); + if (!device_descriptor) { + LOG_ERR("Failed to configure USB device stack"); + return -1; + } + + usb_set_config(device_descriptor); + + usb_enable(); + + return 0; +} + +SYS_INIT(usb_device_init, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY); diff --git a/tests/subsys/usb/device/src/main.c b/tests/subsys/usb/device/src/main.c index 8175dcd7a71..3745d565403 100644 --- a/tests/subsys/usb/device/src/main.c +++ b/tests/subsys/usb/device/src/main.c @@ -130,11 +130,11 @@ static int device_init(void) int ret; /* Initialize the USB driver with the right configuration */ - ret = usb_set_config(&device_config); + ret = usb_set_config(device_config.usb_device_description); zassert_equal(ret, TC_PASS, "usb_set_config() failed"); /* Enable USB driver */ - ret = usb_enable(&device_config); + ret = usb_enable(); zassert_equal(ret, TC_PASS, "usb_enable() failed"); return ret;