From b32ec6b864826d0f1bcf9b6e7b7c20f3540d78a4 Mon Sep 17 00:00:00 2001 From: Koen Zandberg Date: Wed, 23 Oct 2019 09:58:08 +0200 Subject: [PATCH 1/2] usbus: add find_endpoint function for interfaces --- sys/include/usb/usbus.h | 18 ++++++++++++++++++ sys/usb/usbus/usbus.c | 12 ++++++++++++ 2 files changed, 30 insertions(+) diff --git a/sys/include/usb/usbus.h b/sys/include/usb/usbus.h index 090f969d9c..9ce0563ccc 100644 --- a/sys/include/usb/usbus.h +++ b/sys/include/usb/usbus.h @@ -427,6 +427,24 @@ uint16_t usbus_add_string_descriptor(usbus_t *usbus, usbus_string_t *desc, */ uint16_t usbus_add_interface(usbus_t *usbus, usbus_interface_t *iface); +/** + * @brief Find an endpoint from an interface based on the endpoint properties + * + * This iterates over the endpoints in an interface and will return the first + * endpoint from the interface matching the @p type and @p dir. It will return + * NULL when no matching endpoint is found. + * + * @param[in] interface interface to look in + * @param[in] type endpoint type to match + * @param[in] dir endpoint direction to match + * + * @return ptr to the first matching endpoint + * @return NULL when no endpoint is found + */ +usbus_endpoint_t *usbus_interface_find_endpoint(usbus_interface_t *interface, + usb_ep_type_t type, + usb_ep_dir_t dir); + /** * @brief Add an endpoint to the specified interface * diff --git a/sys/usb/usbus/usbus.c b/sys/usb/usbus/usbus.c index a39b9d2ca3..8006f81252 100644 --- a/sys/usb/usbus/usbus.c +++ b/sys/usb/usbus/usbus.c @@ -127,6 +127,18 @@ void usbus_register_event_handler(usbus_t *usbus, usbus_handler_t *handler) *last = handler; } +usbus_endpoint_t *usbus_interface_find_endpoint(usbus_interface_t *interface, + usb_ep_type_t type, + usb_ep_dir_t dir) +{ + for (usbus_endpoint_t *uep = interface->ep; uep; uep = uep->next) { + if (uep->ep->type == type && uep->ep->dir == dir) { + return uep; + } + } + return NULL; +} + usbus_endpoint_t *usbus_add_endpoint(usbus_t *usbus, usbus_interface_t *iface, usb_ep_type_t type, usb_ep_dir_t dir, size_t len) From 1d7032a257f5855c52c7b1aa043c3b863717caca Mon Sep 17 00:00:00 2001 From: Koen Zandberg Date: Mon, 21 Oct 2019 20:35:47 +0200 Subject: [PATCH 2/2] cdcacm: Activate data out endpoint on DTE present signal --- sys/usb/usbus/cdc/acm/cdc_acm.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/sys/usb/usbus/cdc/acm/cdc_acm.c b/sys/usb/usbus/cdc/acm/cdc_acm.c index 3f9fb3fa42..73039ea6b3 100644 --- a/sys/usb/usbus/cdc/acm/cdc_acm.c +++ b/sys/usb/usbus/cdc/acm/cdc_acm.c @@ -213,12 +213,13 @@ static void _init(usbus_t *usbus, usbus_handler_t *handler) USBUS_CDC_ACM_BULK_EP_SIZE); ep->interval = 0; /* Interval is not used with bulk endpoints */ usbus_enable_endpoint(ep); + /* Store the endpoint reference to activate it + * when DTE present is signalled by the host */ ep = usbus_add_endpoint(usbus, &cdcacm->iface_data, USB_EP_TYPE_BULK, USB_EP_DIR_OUT, USBUS_CDC_ACM_BULK_EP_SIZE); ep->interval = 0; /* Interval is not used with bulk endpoints */ usbus_enable_endpoint(ep); - usbdev_ep_ready(ep->ep, 0); /* Add interfaces to the stack */ usbus_add_interface(usbus, &cdcacm->iface_ctrl); @@ -271,6 +272,10 @@ static int _control_handler(usbus_t *usbus, usbus_handler_t *handler, if (setup->value & USB_CDC_ACM_CONTROL_LINE_DTE) { DEBUG("CDC ACM: DTE enabled on interface %u\n", setup->index); cdcacm->state = USBUS_CDC_ACM_LINE_STATE_DTE; + usbus_endpoint_t *data_out = usbus_interface_find_endpoint( + &cdcacm->iface_data, USB_EP_TYPE_BULK, USB_EP_DIR_OUT); + assert(data_out); + usbdev_ep_ready(data_out->ep, 0); usbus_cdc_acm_flush(cdcacm); } else {