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 <andrei.emeltchenko@intel.com>
This commit is contained in:
Andrei Emeltchenko 2019-05-03 16:55:34 +03:00 committed by Anas Nashif
commit 3880a42375
10 changed files with 61 additions and 338 deletions

View file

@ -201,11 +201,11 @@ struct usb_cfg_data {
* Function to configure USB controller. * Function to configure USB controller.
* Configuration parameters must be valid or an error is returned * 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 * @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 * @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 * it is now capable of transmitting and receiving on the USB bus and
* of generating interrupts. * of generating interrupts.
* *
* @param[in] config Pointer to configuration structure
*
* @return 0 on success, negative errno code on fail. * @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 * @brief Disable the USB device

View file

@ -119,23 +119,8 @@ void main(void)
LOG_DBG("Starting application"); LOG_DBG("Starting application");
k_delayed_work_init(&delayed_report_send, send_report); 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) static int composite_pre_init(struct device *dev)
{ {
hdev = device_get_binding("HID_0"); 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); SYS_INIT(composite_pre_init, APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEVICE);
#endif

View file

@ -313,24 +313,6 @@ static int bluetooth_init(struct device *dev)
return ret; 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_create(&rx_thread_data, rx_thread_stack,
K_THREAD_STACK_SIZEOF(rx_thread_stack), K_THREAD_STACK_SIZEOF(rx_thread_stack),
(k_thread_entry_t)hci_rx_thread, NULL, NULL, NULL, (k_thread_entry_t)hci_rx_thread, NULL, NULL, NULL,

View file

@ -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", 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); 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_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->cb_work, cdc_acm_irq_callback_work_handler);
k_work_init(&dev_data->tx_work, tx_work_handler); k_work_init(&dev_data->tx_work, tx_work_handler);

View file

@ -657,26 +657,6 @@ int usb_hid_init(const struct device *dev)
*/ */
usb_set_hid_report_size(cfg, dev_data->report_size); 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; return 0;
} }

View file

@ -180,33 +180,3 @@ USBD_CFG_DATA_DEFINE(loopback) struct usb_cfg_data loopback_config = {
.endpoint = ep_cfg, .endpoint = ep_cfg,
}; };
/* usb.rst device config data end */ /* 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);

View file

@ -949,25 +949,6 @@ static int mass_storage_init(struct device *dev)
msd_state_machine_reset(); msd_state_machine_reset();
msd_init(); 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); k_sem_init(&disk_wait_sem, 0, 1);
/* Start a thread to offload disk ops */ /* Start a thread to offload disk ops */

View file

@ -137,34 +137,6 @@ static void netusb_init(struct net_if *iface)
net_if_down(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"); LOG_INF("netusb initialized");
} }

View file

@ -146,8 +146,6 @@ static struct usb_dev_priv {
s32_t data_buf_len; s32_t data_buf_len;
/** Installed custom request handler */ /** Installed custom request handler */
usb_request_handler custom_req_handler; usb_request_handler custom_req_handler;
/** Installed vendor request handler */
usb_request_handler vendor_req_handler;
/** USB stack status clalback */ /** USB stack status clalback */
usb_dc_status_callback status_callback; usb_dc_status_callback status_callback;
/** Pointer to registered descriptors */ /** Pointer to registered descriptors */
@ -877,24 +875,6 @@ static int usb_handle_standard_request(struct usb_setup_packet *setup,
return rc; 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 * @brief Registers a callback for custom device requests
* *
@ -1010,45 +990,6 @@ static int usb_vbus_set(bool on)
return 0; 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) int usb_deconfig(void)
{ {
/* unregister descriptors */ /* unregister descriptors */
@ -1072,90 +1013,6 @@ int usb_deconfig(void)
return 0; 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 usb_disable(void)
{ {
int ret; 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() * The functions class_handler(), custom_handler() and vendor_handler()
* go through the interfaces one after the other and compare the * go through the interfaces one after the other and compare the
@ -1608,46 +1463,36 @@ static int composite_setup_ep_cb(void)
return 0; return 0;
} }
/* int usb_set_config(const u8_t *device_descriptor)
* This function configures the USB device stack based on USB descriptor and
* usb_cfg_data.
*/
static int usb_composite_init(struct device *dev)
{ {
int ret; /* register descriptors */
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;
}
usb_register_descriptors(device_descriptor); usb_register_descriptors(device_descriptor);
/* register standard request handler */ /* register standard request handler */
usb_register_request_handler(REQTYPE_TYPE_STANDARD, usb_register_request_handler(REQTYPE_TYPE_STANDARD,
&(usb_handle_standard_request)); usb_handle_standard_request);
/* register class request handlers for each interface*/ /* register class request handlers for each interface*/
usb_register_request_handler(REQTYPE_TYPE_CLASS, usb_register_request_handler(REQTYPE_TYPE_CLASS, class_handler);
class_handler);
/* register vendor request handlers */ /* register vendor request handler */
usb_register_request_handler(REQTYPE_TYPE_VENDOR, usb_register_request_handler(REQTYPE_TYPE_VENDOR, vendor_handler);
vendor_handler);
/* register class request handlers for each interface*/ /* register class request handlers for each interface*/
usb_register_custom_req_handler(custom_handler); usb_register_custom_req_handler(custom_handler);
usb_register_status_callback(forward_status_cb); return 0;
usb_dc_set_status_callback(forward_status_cb); }
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 */ /* Enable VBUS if needed */
ret = usb_vbus_set(true); ret = usb_vbus_set(true);
@ -1655,6 +1500,9 @@ static int usb_composite_init(struct device *dev)
return ret; return ret;
} }
usb_register_status_callback(forward_status_cb);
usb_dc_set_status_callback(forward_status_cb);
ret = usb_dc_attach(); ret = usb_dc_attach();
if (ret < 0) { if (ret < 0) {
return ret; return ret;
@ -1676,7 +1524,7 @@ static int usb_composite_init(struct device *dev)
return ret; return ret;
} }
/*register endpoint 0 handlers*/ /* Register endpoint 0 handlers*/
ret = usb_dc_ep_set_callback(USB_CONTROL_OUT_EP0, ret = usb_dc_ep_set_callback(USB_CONTROL_OUT_EP0,
usb_handle_control_transfer); usb_handle_control_transfer);
if (ret < 0) { if (ret < 0) {
@ -1689,17 +1537,19 @@ static int usb_composite_init(struct device *dev)
return ret; return ret;
} }
if (composite_setup_ep_cb()) { /* Register endpoint handlers*/
return -1; ret = composite_setup_ep_cb();
if (ret < 0) {
return ret;
} }
/* init transfer slots */ /* Init transfer slots */
for (int i = 0; i < MAX_NUM_TRANSFERS; i++) { for (i = 0U; i < MAX_NUM_TRANSFERS; i++) {
k_work_init(&usb_dev.transfer[i].work, usb_transfer_work); k_work_init(&usb_dev.transfer[i].work, usb_transfer_work);
k_sem_init(&usb_dev.transfer[i].sem, 1, 1); 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); ret = usb_dc_ep_enable(USB_CONTROL_OUT_EP0);
if (ret < 0) { if (ret < 0) {
return ret; return ret;
@ -1715,5 +1565,30 @@ static int usb_composite_init(struct device *dev)
return 0; 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);

View file

@ -130,11 +130,11 @@ static int device_init(void)
int ret; int ret;
/* Initialize the USB driver with the right configuration */ /* 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"); zassert_equal(ret, TC_PASS, "usb_set_config() failed");
/* Enable USB driver */ /* Enable USB driver */
ret = usb_enable(&device_config); ret = usb_enable();
zassert_equal(ret, TC_PASS, "usb_enable() failed"); zassert_equal(ret, TC_PASS, "usb_enable() failed");
return ret; return ret;