Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions dts/bindings/ethernet/zephyr,cdc-ncm-ethernet.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright (c) 2024 Nordic Semiconductor ASA
# SPDX-License-Identifier: Apache-2.0

description: USB CDC NCM virtual Ethernet controller

compatible: "zephyr,cdc-ncm-ethernet"

include: ethernet-controller.yaml

properties:
remote-mac-address:
type: string
required: true
description: |
Remote MAC address of the virtual Ethernet connection.
Should not be the same as local-mac-address property.
28 changes: 28 additions & 0 deletions include/zephyr/usb/class/usb_cdc.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@
#define ACM_SUBCLASS 0x02
#define ECM_SUBCLASS 0x06
#define EEM_SUBCLASS 0x0c
#define NCM_SUBCLASS 0x0d

/** Communications Class Protocol Codes */
#define AT_CMD_V250_PROTOCOL 0x01
#define EEM_PROTOCOL 0x07
#define ACM_VENDOR_PROTOCOL 0xFF
#define NCM_DATA_PROTOCOL 0x01

/**
* @brief Data Class Interface Codes
Expand All @@ -50,6 +52,7 @@
#define ACM_FUNC_DESC 0x02
#define UNION_FUNC_DESC 0x06
#define ETHERNET_FUNC_DESC 0x0F
#define ETHERNET_FUNC_DESC_NCM 0x1a

/**
* @brief PSTN Subclass Specific Requests
Expand Down Expand Up @@ -139,6 +142,22 @@
#define SET_ETHERNET_PACKET_FILTER 0x43
#define GET_ETHERNET_STATISTIC 0x44

/**
* @brief Class-Specific Request Codes for NCM subclass
* @note NCM100.pdf, 6.2, Table 6-2
*/
#define GET_NTB_PARAMETERS 0x80
#define GET_NET_ADDRESS 0x81
#define SET_NET_ADDRESS 0x82
#define GET_NTB_FORMAT 0x83
#define SET_NTB_FORMAT 0x84
#define GET_NTB_INPUT_SIZE 0x85
#define SET_NTB_INPUT_SIZE 0x86
#define GET_MAX_DATAGRAM_SIZE 0x87
#define SET_MAX_DATAGRAM_SIZE 0x88
#define GET_CRC_MODE 0x89
#define SET_CRC_MODE 0x8A

/** Ethernet Packet Filter Bitmap */
#define PACKET_TYPE_MULTICAST 0x10
#define PACKET_TYPE_BROADCAST 0x08
Expand Down Expand Up @@ -210,4 +229,13 @@ struct cdc_ecm_descriptor {
uint8_t bNumberPowerFilters;
} __packed;

/** Ethernet Network Control Model (NCM) Descriptor */
struct cdc_ncm_descriptor {
uint8_t bFunctionLength;
uint8_t bDescriptorType;
uint8_t bDescriptorSubtype;
uint16_t bcdNcmVersion;
uint8_t bmNetworkCapabilities;
} __packed;

#endif /* ZEPHYR_INCLUDE_USB_CLASS_USB_CDC_H_ */
10 changes: 9 additions & 1 deletion samples/net/sockets/echo_server/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,15 @@ target_sources_ifdef(CONFIG_NET_UDP app PRIVATE src/udp.c)
target_sources_ifdef(CONFIG_NET_TCP app PRIVATE src/tcp.c)
target_sources_ifdef(CONFIG_NET_VLAN app PRIVATE src/vlan.c)
target_sources_ifdef(CONFIG_NET_L2_IPIP app PRIVATE src/tunnel.c)
target_sources_ifdef(CONFIG_USB_DEVICE_STACK app PRIVATE src/usb.c)

if (CONFIG_USB_DEVICE_STACK)
target_sources(app PRIVATE src/usb.c)
endif()

if (CONFIG_USB_DEVICE_STACK_NEXT)
target_sources(app PRIVATE src/usb.c)
include(${ZEPHYR_BASE}/samples/subsys/usb/common/common.cmake)
endif()

include(${ZEPHYR_BASE}/samples/net/common/common.cmake)

Expand Down
7 changes: 7 additions & 0 deletions samples/net/sockets/echo_server/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,11 @@ config NET_SAMPLE_HTTPS_SERVER_SERVICE_PORT
default 443
depends on NET_SAMPLE_HTTPS_SERVICE

if USB_DEVICE_STACK_NEXT
# Source common USB sample options used to initialize new experimental USB
# device stack. The scope of these options is limited to USB samples in project
# tree, you cannot use them in your own application.
source "samples/subsys/usb/common/Kconfig.sample_usbd"
endif

source "Kconfig.zephyr"
7 changes: 7 additions & 0 deletions samples/net/sockets/echo_server/overlay-usbd_next.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CONFIG_USB_DEVICE_STACK_NEXT=y

CONFIG_LOG=y
CONFIG_USBD_LOG_LEVEL_WRN=y
CONFIG_UDC_DRIVER_LOG_LEVEL_WRN=y

CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=2048
2 changes: 1 addition & 1 deletion samples/net/sockets/echo_server/src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ static inline bool is_tunnel(struct net_if *iface)
}
#endif /* CONFIG_NET_L2_IPIP */

#if defined(CONFIG_USB_DEVICE_STACK)
#if defined(CONFIG_USB_DEVICE_STACK) || defined(CONFIG_USB_DEVICE_STACK_NEXT)
int init_usb(void);
#else
static inline int init_usb(void)
Expand Down
31 changes: 31 additions & 0 deletions samples/net/sockets/echo_server/src/usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,46 @@ LOG_MODULE_DECLARE(net_echo_server_sample, LOG_LEVEL_DBG);
#include <zephyr/usb/usb_device.h>
#include <zephyr/net/net_config.h>

#if defined(CONFIG_USB_DEVICE_STACK_NEXT)
#include <sample_usbd.h>

static struct usbd_context *sample_usbd;

static int enable_usb_device_next(void)
{
int err;

sample_usbd = sample_usbd_init_device(NULL);
if (sample_usbd == NULL) {
return -ENODEV;
}

err = usbd_enable(sample_usbd);
if (err) {
return err;
}

return 0;
}
#endif /* CONFIG_USB_DEVICE_STACK_NEXT */

int init_usb(void)
{
#if defined(CONFIG_USB_DEVICE_STACK)
int ret;

ret = usb_enable(NULL);
if (ret != 0) {
LOG_ERR("Cannot enable USB (%d)", ret);
return ret;
}
#endif /* CONFIG_USB_DEVICE_STACK */

#if defined(CONFIG_USB_DEVICE_STACK_NEXT)
if (enable_usb_device_next()) {
return 0;
}
#endif /* CONFIG_USB_DEVICE_STACK_NEXT */

(void)net_config_init_app(NULL, "Initializing network");

Expand Down
12 changes: 12 additions & 0 deletions samples/net/sockets/echo_server/usbd_next_ncm.overlay
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* Copyright (c) 2024 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/

/ {
cdc_ncm_eth0: cdc_ncm_eth0 {
compatible = "zephyr,cdc-ncm-ethernet";
remote-mac-address = "00005E005301";
};
};
4 changes: 4 additions & 0 deletions samples/net/zperf/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ if (CONFIG_NET_L2_ETHERNET)
LOCATION "${CONFIG_NET_SAMPLE_CODE_RAM_NAME}_TEXT" NOKEEP)
endif()
endif()

if (CONFIG_USB_DEVICE_STACK_NEXT)
include(${ZEPHYR_BASE}/samples/subsys/usb/common/common.cmake)
endif()
7 changes: 7 additions & 0 deletions samples/net/zperf/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,10 @@ config NET_SAMPLE_CODE_RAM_NAME
Region to relocate networking code to

endif # NET_SAMPLE_CODE_RELOCATE

if USB_DEVICE_STACK_NEXT
# Source common USB sample options used to initialize new experimental USB
# device stack. The scope of these options is limited to USB samples in project
# tree, you cannot use them in your own application.
source "samples/subsys/usb/common/Kconfig.sample_usbd"
endif
9 changes: 8 additions & 1 deletion samples/net/zperf/sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,18 @@ tests:
- native_sim/native/64
sample.net.zperf.device_next_ecm:
harness: net
extra_args: EXTRA_CONF_FILE="overlay-usbd_next_ecm.conf"
extra_args: EXTRA_CONF_FILE="overlay-usbd_next.conf"
DTC_OVERLAY_FILE="usbd_next_ecm.overlay"
platform_allow: nrf52840dk/nrf52840 frdm_k64f
tags: usb net zperf
depends_on: usb_device
sample.net.zperf.device_next_ncm:
harness: net
extra_args: OVERLAY_CONFIG="overlay-usbd_next.conf"
DTC_OVERLAY_FILE="usbd_next_ncm.overlay"
platform_allow: nrf52840dk/nrf52840 frdm_k64f
tags: usb net zperf
depends_on: usb_device
sample.net.zperf.netusb_eem:
harness: net
extra_args: EXTRA_CONF_FILE="overlay-netusb.conf"
Expand Down
34 changes: 34 additions & 0 deletions samples/net/zperf/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,36 @@
* @brief Zperf sample.
*/
#include <zephyr/usb/usb_device.h>
#include <zephyr/usb/usbd.h>
#include <zephyr/net/net_config.h>

#ifdef CONFIG_NET_LOOPBACK_SIMULATE_PACKET_DROP
#include <zephyr/net/loopback.h>
#endif

#if defined(CONFIG_USB_DEVICE_STACK_NEXT)
#include <sample_usbd.h>

static struct usbd_context *sample_usbd;

static int enable_usb_device_next(void)
{
int err;

sample_usbd = sample_usbd_init_device(NULL);
if (sample_usbd == NULL) {
return -ENODEV;
}

err = usbd_enable(sample_usbd);
if (err) {
return err;
}

return 0;
}
#endif /* CONFIG_USB_DEVICE_STACK_NEXT */

int main(void)
{
#if defined(CONFIG_USB_DEVICE_STACK)
Expand All @@ -26,6 +51,15 @@ int main(void)

(void)net_config_init_app(NULL, "Initializing network");
#endif /* CONFIG_USB_DEVICE_STACK */

#if defined(CONFIG_USB_DEVICE_STACK_NEXT)
if (enable_usb_device_next()) {
return 0;
}

(void)net_config_init_app(NULL, "Initializing network");
#endif /* CONFIG_USB_DEVICE_STACK_NEXT */

#ifdef CONFIG_NET_LOOPBACK_SIMULATE_PACKET_DROP
loopback_set_packet_drop_ratio(1);
#endif
Expand Down
12 changes: 12 additions & 0 deletions samples/net/zperf/usbd_next_ncm.overlay
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* Copyright (c) 2024 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/

/ {
cdc_ncm_eth0: cdc_ncm_eth0 {
compatible = "zephyr,cdc-ncm-ethernet";
remote-mac-address = "00005E005301";
};
};
1 change: 1 addition & 0 deletions samples/subsys/usb/common/sample_usbd_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ static void sample_fix_code_triple(struct usbd_context *uds_ctx,
/* Always use class code information from Interface Descriptors */
if (IS_ENABLED(CONFIG_USBD_CDC_ACM_CLASS) ||
IS_ENABLED(CONFIG_USBD_CDC_ECM_CLASS) ||
IS_ENABLED(CONFIG_USBD_CDC_NCM_CLASS) ||
IS_ENABLED(CONFIG_USBD_AUDIO2_CLASS)) {
/*
* Class with multiple interfaces have an Interface
Expand Down
2 changes: 1 addition & 1 deletion subsys/net/lib/config/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ if NET_CONFIG_SETTINGS

config NET_CONFIG_AUTO_INIT
bool "Init networking support automatically during device startup"
default n if USB_DEVICE_NETWORK
default n if USB_DEVICE_NETWORK || USBD_CDC_ECM_CLASS || USBD_CDC_NCM_CLASS
default y
help
If this option is set, then the networking system is automatically
Expand Down
9 changes: 9 additions & 0 deletions subsys/usb/device_next/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ zephyr_library_sources_ifdef(
class/usbd_cdc_ecm.c
)

zephyr_include_directories_ifdef(
CONFIG_USBD_CDC_NCM_CLASS
${ZEPHYR_BASE}/drivers/ethernet
)
zephyr_library_sources_ifdef(
CONFIG_USBD_CDC_NCM_CLASS
class/usbd_cdc_ncm.c
)

zephyr_library_sources_ifdef(
CONFIG_USBD_BT_HCI
class/bt_hci.c
Expand Down
1 change: 1 addition & 0 deletions subsys/usb/device_next/class/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
rsource "Kconfig.loopback"
rsource "Kconfig.cdc_acm"
rsource "Kconfig.cdc_ecm"
rsource "Kconfig.cdc_ncm"
rsource "Kconfig.bt"
rsource "Kconfig.msc"
rsource "Kconfig.uac2"
Expand Down
33 changes: 33 additions & 0 deletions subsys/usb/device_next/class/Kconfig.cdc_ncm
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright (c) 2024 Nordic Semiconductor ASA
#
# SPDX-License-Identifier: Apache-2.0

config USBD_CDC_NCM_CLASS
bool "USB CDC NCM implementation [EXPERIMENTAL]"
default y
depends on NET_L2_ETHERNET
depends on DT_HAS_ZEPHYR_CDC_NCM_ETHERNET_ENABLED
select EXPERIMENTAL
help
USB CDC Network Control Model (NCM) implementation

if USBD_CDC_NCM_CLASS

config USBD_CDC_NCM_MAX_DGRAM_PER_NTB
int "Max number of received datagrams per NTB"
range 0 $(UINT16_MAX)
default 2
help
How many datagrams we are able to receive per NTB.

config USBD_CDC_NCM_SUPPORT_NTB32
bool "Support NTB32 format"
help
Enable support for NTB32 format which allows larger
packet sizes.

module = USBD_CDC_NCM
module-str = usbd cdc_ncm
source "subsys/logging/Kconfig.template.log_config"

endif
Loading