Skip to content

Commit

Permalink
usbus/cdc_ecm: Make use of URBs for inbound frames
Browse files Browse the repository at this point in the history
  • Loading branch information
bergzand committed Oct 31, 2021
1 parent 2303c75 commit 16a98b1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 21 deletions.
8 changes: 7 additions & 1 deletion sys/include/usb/usbus/cdc/ecm.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ extern "C" {
*/
#define USBUS_CDCECM_EP_DATA_SIZE 64

/**
* @brief Full ethernet frame rounded up to a whole number of transfers
*/
#define USBUS_ETHERNET_FRAME_BUF (1536)

/**
* @brief notification state, used to track which information must be send to
* the host
Expand Down Expand Up @@ -115,7 +120,7 @@ typedef struct usbus_cdcecm_device {
/**
* @brief Buffer for received frames
*/
usbdev_ep_buf_t data_out[ETHERNET_FRAME_LEN];
usbdev_ep_buf_t data_out[USBUS_ETHERNET_FRAME_BUF];

/**
* @brief Host in device out data buffer
Expand All @@ -126,6 +131,7 @@ typedef struct usbus_cdcecm_device {
* @brief Host out device in control buffer
*/
usbdev_ep_buf_t control_in[USBUS_CDCECM_EP_CTRL_SIZE];
usbus_urb_t out_urb;
} usbus_cdcecm_device_t;

/**
Expand Down
33 changes: 14 additions & 19 deletions sys/usb/usbus/cdc/ecm/cdc_ecm.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,14 @@ static void _fill_ethernet(usbus_cdcecm_device_t *cdcecm)

}

void _start_urb(usbus_cdcecm_device_t *cdcecm)
{
usbus_urb_init(&cdcecm->out_urb,
cdcecm->data_out,
USBUS_ETHERNET_FRAME_BUF, 0);
usbus_urb_submit(cdcecm->usbus, cdcecm->ep_out, &cdcecm->out_urb);
}

void usbus_cdcecm_init(usbus_t *usbus, usbus_cdcecm_device_t *handler)
{
assert(usbus);
Expand Down Expand Up @@ -251,9 +259,9 @@ static int _control_handler(usbus_t *usbus, usbus_handler_t *handler,
setup->value);
cdcecm->active_iface = (uint8_t)setup->value;
if (cdcecm->active_iface == 1) {
usbdev_ep_xmit(cdcecm->ep_out->ep, cdcecm->data_out,
USBUS_CDCECM_EP_DATA_SIZE);
_notify_link_up(cdcecm);
/* Start URB */
_start_urb(cdcecm);
}
break;

Expand Down Expand Up @@ -298,9 +306,8 @@ static void _handle_rx_flush_ev(event_t *ev)
{
usbus_cdcecm_device_t *cdcecm = container_of(ev, usbus_cdcecm_device_t,
rx_flush);

cdcecm->len = 0; /* Flush packet */
usbdev_ep_xmit(cdcecm->ep_out->ep, cdcecm->data_out, USBUS_CDCECM_EP_DATA_SIZE);
/* Start URB */
_start_urb(cdcecm);
}

static void _transfer_handler(usbus_t *usbus, usbus_handler_t *handler,
Expand All @@ -311,20 +318,8 @@ static void _transfer_handler(usbus_t *usbus, usbus_handler_t *handler,
usbus_cdcecm_device_t *cdcecm = (usbus_cdcecm_device_t *)handler;
if (ep == cdcecm->ep_out->ep) {
/* Retrieve incoming data */
if (cdcecm->notif == USBUS_CDCECM_NOTIF_NONE) {
_notify_link_up(cdcecm);
}
size_t len = 0;
usbdev_ep_get(ep, USBOPT_EP_AVAILABLE, &len, sizeof(size_t));
cdcecm->len += len;
if (len == USBUS_CDCECM_EP_DATA_SIZE) {
/* ready next chunk */
usbdev_ep_xmit(ep, cdcecm->data_out + cdcecm->len,
USBUS_CDCECM_EP_DATA_SIZE);
}
else {
netdev_trigger_event_isr(&cdcecm->netdev);
}
cdcecm->len = cdcecm->out_urb.transferred;
netdev_trigger_event_isr(&cdcecm->netdev);
}
else if (ep == cdcecm->ep_in->ep) {
_handle_in_complete(usbus, handler);
Expand Down
2 changes: 1 addition & 1 deletion sys/usb/usbus/cdc/ecm/cdc_ecm_netdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ static int _recv(netdev_t *netdev, void *buf, size_t max_len, void *info)
(void)info;
usbus_cdcecm_device_t *cdcecm = _netdev_to_cdcecm(netdev);

size_t pktlen = cdcecm->len;
size_t pktlen = cdcecm->out_urb.transferred;

if (max_len == 0 && buf == NULL) {
return pktlen;
Expand Down

0 comments on commit 16a98b1

Please sign in to comment.