Skip to content

Commit 0cf5cc0

Browse files
committed
usb: device_next: Simple NCM driver for usb-next
This is a simple USB-NCM driver. It just has one NTB per direction and within the NTB only one datagram can be received/transmitted. Main goal is existance of an NCM driver, performance will follow in further steps. This PR contains the actual driver. Signed-off-by: Hardy Griech <[email protected]>
1 parent af0cbb1 commit 0cf5cc0

File tree

9 files changed

+1459
-5
lines changed

9 files changed

+1459
-5
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Copyright (c) 2024 Hardy Griech
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
description: USB CDC NCM virtual Ethernet controller
5+
6+
compatible: "zephyr,cdc-ncm-ethernet"
7+
8+
include: ethernet-controller.yaml
9+
10+
properties:
11+
remote-mac-address:
12+
type: string
13+
required: true
14+
description: |
15+
Remote MAC address of the virtual Ethernet connection.
16+
Should not be the same as local-mac-address property.

include/zephyr/usb/class/usb_cdc.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@
2828
#define ACM_SUBCLASS 0x02
2929
#define ECM_SUBCLASS 0x06
3030
#define EEM_SUBCLASS 0x0c
31+
#define NCM_SUBCLASS 0x0d
3132

3233
/** Communications Class Protocol Codes */
3334
#define AT_CMD_V250_PROTOCOL 0x01
3435
#define EEM_PROTOCOL 0x07
3536
#define ACM_VENDOR_PROTOCOL 0xFF
37+
#define NCM_DATA_PROTOCOL 1
3638

3739
/**
3840
* @brief Data Class Interface Codes
@@ -50,6 +52,7 @@
5052
#define ACM_FUNC_DESC 0x02
5153
#define UNION_FUNC_DESC 0x06
5254
#define ETHERNET_FUNC_DESC 0x0F
55+
#define ETHERNET_FUNC_DESC_NCM 0x1a
5356

5457
/**
5558
* @brief PSTN Subclass Specific Requests
@@ -199,7 +202,7 @@ struct cdc_acm_notification {
199202
} __packed;
200203

201204
/** Ethernet Networking Functional Descriptor */
202-
struct cdc_ecm_descriptor {
205+
struct cdc_eth_functional_descriptor {
203206
uint8_t bFunctionLength;
204207
uint8_t bDescriptorType;
205208
uint8_t bDescriptorSubtype;
@@ -210,4 +213,12 @@ struct cdc_ecm_descriptor {
210213
uint8_t bNumberPowerFilters;
211214
} __packed;
212215

216+
struct cdc_ncm_functional_descriptor {
217+
uint8_t bFunctionLength;
218+
uint8_t bDescriptorType;
219+
uint8_t bDescriptorSubtype;
220+
uint16_t bcdNcmVersion;
221+
uint8_t bmNetworkCapabilities;
222+
} __packed;
223+
213224
#endif /* ZEPHYR_INCLUDE_USB_CLASS_USB_CDC_H_ */

subsys/usb/device/class/netusb/function_ecm.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ struct usb_cdc_ecm_config {
3535
struct usb_if_descriptor if0;
3636
struct cdc_header_descriptor if0_header;
3737
struct cdc_union_descriptor if0_union;
38-
struct cdc_ecm_descriptor if0_netfun_ecm;
38+
struct cdc_eth_functional_descriptor if0_netfun_ecm;
3939
struct usb_ep_descriptor if0_int_ep;
4040

4141
struct usb_if_descriptor if1_0;
@@ -86,7 +86,7 @@ USBD_CLASS_DESCR_DEFINE(primary, 0) struct usb_cdc_ecm_config cdc_ecm_cfg = {
8686
},
8787
/* Ethernet Networking Functional descriptor */
8888
.if0_netfun_ecm = {
89-
.bFunctionLength = sizeof(struct cdc_ecm_descriptor),
89+
.bFunctionLength = sizeof(struct cdc_eth_functional_descriptor),
9090
.bDescriptorType = USB_DESC_CS_INTERFACE,
9191
.bDescriptorSubtype = ETHERNET_FUNC_DESC,
9292
.iMACAddress = 4,

subsys/usb/device_next/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ zephyr_library_sources_ifdef(
4141
class/usbd_cdc_ecm.c
4242
)
4343

44+
zephyr_include_directories_ifdef(
45+
CONFIG_USBD_CDC_NCM_CLASS
46+
${ZEPHYR_BASE}/drivers/ethernet
47+
)
48+
zephyr_library_sources_ifdef(
49+
CONFIG_USBD_CDC_NCM_CLASS
50+
class/usbd_cdc_ncm.c
51+
)
52+
4453
zephyr_library_sources_ifdef(
4554
CONFIG_USBD_BT_HCI
4655
class/bt_hci.c

subsys/usb/device_next/class/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
rsource "Kconfig.loopback"
66
rsource "Kconfig.cdc_acm"
77
rsource "Kconfig.cdc_ecm"
8+
rsource "Kconfig.cdc_ncm"
89
rsource "Kconfig.bt"
910
rsource "Kconfig.msc"
1011
rsource "Kconfig.uac2"
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright (c) 2024 Hardy Griech
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
config USBD_CDC_NCM_CLASS
6+
bool "USB CDC NCM implementation [EXPERIMENTAL]"
7+
default y
8+
depends on NET_L2_ETHERNET
9+
depends on DT_HAS_ZEPHYR_CDC_NCM_ETHERNET_ENABLED
10+
help
11+
USB CDC Network Control Model (NCM) implementation"
12+
13+
if USBD_CDC_NCM_CLASS
14+
module = USBD_CDC_NCM
15+
module-str = usbd cdc_ncm
16+
default-count = 1
17+
source "subsys/logging/Kconfig.template.log_config"
18+
rsource "Kconfig.template.instances_count"
19+
endif

subsys/usb/device_next/class/usbd_cdc_ecm.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ struct usbd_cdc_ecm_desc {
6363
struct usb_if_descriptor if0;
6464
struct cdc_header_descriptor if0_header;
6565
struct cdc_union_descriptor if0_union;
66-
struct cdc_ecm_descriptor if0_ecm;
66+
struct cdc_eth_functional_descriptor if0_ecm;
6767
struct usb_ep_descriptor if0_int_ep;
6868
struct usb_ep_descriptor if0_hs_int_ep;
6969

@@ -681,7 +681,7 @@ static struct usbd_cdc_ecm_desc cdc_ecm_desc_##n = { \
681681
}, \
682682
\
683683
.if0_ecm = { \
684-
.bFunctionLength = sizeof(struct cdc_ecm_descriptor), \
684+
.bFunctionLength = sizeof(struct cdc_eth_functional_descriptor), \
685685
.bDescriptorType = USB_DESC_CS_INTERFACE, \
686686
.bDescriptorSubtype = ETHERNET_FUNC_DESC, \
687687
.iMACAddress = 0, \

0 commit comments

Comments
 (0)