ecm.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2019 Koen Zandberg
3  * SPDX-License-Identifier: LGPL-2.1-only
4  */
5 
6 #pragma once
7 
21 #include <stdint.h>
22 #include <stdlib.h>
23 #include "net/ethernet.h"
24 #include "net/ethernet/hdr.h"
25 #include "usb/descriptor.h"
26 #include "usb/usbus.h"
27 #include "usb/usbus/control.h"
28 #include "macros/math.h"
29 #include "net/netdev.h"
30 #include "mutex.h"
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
43 #ifndef CONFIG_USBUS_CDC_ECM_CONFIG_SPEED
44 #define CONFIG_USBUS_CDC_ECM_CONFIG_SPEED 1000000
45 #endif
46 
50 #ifndef CONFIG_USBUS_CDC_ECM_CONFIG_SPEED_DOWNSTREAM
51 #define CONFIG_USBUS_CDC_ECM_CONFIG_SPEED_DOWNSTREAM CONFIG_USBUS_CDC_ECM_CONFIG_SPEED
52 #endif
53 
57 #ifndef CONFIG_USBUS_CDC_ECM_CONFIG_SPEED_UPSTREAM
58 #define CONFIG_USBUS_CDC_ECM_CONFIG_SPEED_UPSTREAM CONFIG_USBUS_CDC_ECM_CONFIG_SPEED
59 #endif
60 
68 #define USBUS_CDCECM_EP_CTRL_SIZE 16
69 
75 #ifndef MODULE_PERIPH_USBDEV_HS
76 #define USBUS_CDCECM_EP_DATA_SIZE 64
77 #else
78 #define USBUS_CDCECM_EP_DATA_SIZE 512
79 #endif
80 
84 #define USBUS_ETHERNET_FRAME_BUF MATH_ALIGN(ETHERNET_FRAME_LEN, USBUS_CDCECM_EP_DATA_SIZE)
85 
89 #define USBUS_CDC_ECM_EP_IN_REQUIRED_NUMOF 2
90 
94 #define USBUS_CDC_ECM_EP_OUT_REQUIRED_NUMOF 1
95 
100 typedef enum {
105 
109 typedef struct usbus_cdcecm_device {
122  char mac_host[13];
126  size_t tx_len;
128  unsigned active_iface;
134 
139 
149 
157 
158 #ifdef __cplusplus
159 }
160 #endif
161 
USBUS control endpoint module.
Definitions for USB protocol messages.
Definitions low-level network driver interface.
Definitions for Ethernet.
#define usbdev_ep_buf_t
Instantiation type for usbdev endpoint buffers.
Definition: usbdev.h:126
#define ETHERNET_ADDR_LEN
Length of an Ethernet address.
Definition: hdr.h:28
#define USBUS_CDCECM_EP_CTRL_SIZE
CDC ECM interrupt endpoint size.
Definition: ecm.h:68
#define USBUS_CDCECM_EP_DATA_SIZE
CDC ECM bulk data endpoint size.
Definition: ecm.h:76
usbus_cdcecm_notif_t
notification state, used to track which information must be send to the host
Definition: ecm.h:100
#define USBUS_ETHERNET_FRAME_BUF
Full ethernet frame rounded up to a whole number of transfers.
Definition: ecm.h:84
void usbus_cdcecm_init(usbus_t *usbus, usbus_cdcecm_device_t *handler)
CDC ECM initialization function.
struct usbus_cdcecm_device usbus_cdcecm_device_t
USBUS CDC ECM device interface context.
@ USBUS_CDCECM_NOTIF_NONE
Nothing notified so far.
Definition: ecm.h:101
@ USBUS_CDCECM_NOTIF_LINK_UP
Link status is notified.
Definition: ecm.h:102
@ USBUS_CDCECM_NOTIF_SPEED
Link speed is notified.
Definition: ecm.h:103
Math helper macros.
Mutex for thread synchronization.
Ethernet header definitions.
event structure
Definition: event.h:142
Mutex structure.
Definition: mutex.h:36
Structure to hold driver state.
Definition: netdev.h:365
USBUS CDC ECM device interface context.
Definition: ecm.h:109
usbus_cdcecm_notif_t notif
Startup message notification tracker.
Definition: ecm.h:127
usbdev_ep_buf_t control_in[USBUS_CDCECM_EP_CTRL_SIZE]
Host out device in control buffer.
Definition: ecm.h:143
usbdev_ep_buf_t data_in[USBUS_CDCECM_EP_DATA_SIZE]
Host in device out data buffer.
Definition: ecm.h:138
usbus_interface_t iface_data
Data interface.
Definition: ecm.h:111
usbdev_ep_buf_t data_out[USBUS_ETHERNET_FRAME_BUF]
Buffer for received frames from the host.
Definition: ecm.h:133
usbus_endpoint_t * ep_ctrl
Control endpoint.
Definition: ecm.h:116
netdev_t netdev
Netdev context struct.
Definition: ecm.h:120
usbus_descr_gen_t ecm_descr
ECM descriptor generator.
Definition: ecm.h:117
event_t tx_xmit
Transmit ready event.
Definition: ecm.h:119
usbus_interface_t iface_ctrl
Control interface.
Definition: ecm.h:112
unsigned active_iface
Current active data interface.
Definition: ecm.h:128
size_t tx_len
Length of the current tx frame.
Definition: ecm.h:126
usbus_handler_t handler_ctrl
Control interface handler.
Definition: ecm.h:110
usbus_endpoint_t * ep_out
Data endpoint out.
Definition: ecm.h:115
char mac_host[13]
host side's MAC address as string
Definition: ecm.h:122
mutex_t out_lock
mutex used for locking netif/USBUS send
Definition: ecm.h:125
usbus_endpoint_t * ep_in
Data endpoint in.
Definition: ecm.h:114
usbus_urb_t out_urb
Host out device in reception URB.
Definition: ecm.h:147
event_t rx_flush
Receive flush event.
Definition: ecm.h:118
uint8_t mac_netdev[ETHERNET_ADDR_LEN]
this device's MAC address
Definition: ecm.h:121
usbus_interface_alt_t iface_data_alt
Data alternative (active) interface.
Definition: ecm.h:113
usbus_t * usbus
Ptr to the USBUS context.
Definition: ecm.h:124
usbus_string_t mac_str
String context for the host side mac address.
Definition: ecm.h:123
USBUS descriptor generator.
Definition: usbus.h:294
USBUS endpoint context.
Definition: usbus.h:304
USBUS handler struct.
Definition: usbus.h:432
USBUS interface alternative setting.
Definition: usbus.h:337
USBUS interface.
Definition: usbus.h:349
USBUS string type.
Definition: usbus.h:206
USBUS USB request/response block.
Definition: usbus.h:324
USBUS context struct.
Definition: usbus.h:444
USBUS basic interface.