Skip to content

Commit ecfec34

Browse files
committed
NCM works with CONFIG_NET_BUF_VARIABLE_DATA_SIZE=y
1 parent 0e940ba commit ecfec34

File tree

2 files changed

+29
-27
lines changed

2 files changed

+29
-27
lines changed

subsys/usb/device_next/class/usbd_cdc_ncm.c

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@
2626
* - itf_data_alt if != 0 -> data xmit/recv are allowed (see spec)
2727
* - ep_in IN endpoints take data from the device intended to go in to the host (the device transmits)
2828
* - ep_out OUT endpoints send data out of the host to the device (the device receives)
29+
*
30+
* Linux host NCM driver
31+
* ---------------------
32+
* - https://github.com/torvalds/linux/blob/master/drivers/net/usb/cdc_ncm.c
33+
* - https://github.com/torvalds/linux/blob/master/include/linux/usb/cdc_ncm.h
34+
* - https://github.com/torvalds/linux/blob/master/include/uapi/linux/usb/cdc.h
2935
*/
3036

3137
#define DT_DRV_COMPAT zephyr_cdc_ncm_ethernet
@@ -43,7 +49,7 @@
4349
#include <zephyr/logging/log.h>
4450
LOG_MODULE_REGISTER(cdc_ncm, CONFIG_USBD_CDC_NCM_LOG_LEVEL);
4551

46-
#include "usbd_cdc_ncm_local.h"
52+
#include "usbd_cdc_ncm_private.h"
4753

4854
#define CDC_NCM_EP_MPS_INT 64
4955
#define CDC_NCM_INTERVAL_DEFAULT 50000UL
@@ -63,11 +69,11 @@ enum {
6369

6470
/*
6571
* Transfers through two endpoints proceed in a synchronous manner,
66-
* with maximum block of CONFIG_CDC_NCM_XMT_NTB_MAX_SIZE.
72+
* with maximum block of CFG_CDC_NCM_XMT_NTB_MAX_SIZE.
6773
*/
6874
NET_BUF_POOL_FIXED_DEFINE(cdc_ncm_ep_pool,
6975
DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT) * 2,
70-
CONFIG_CDC_NCM_XMT_NTB_MAX_SIZE,
76+
MAX(CFG_CDC_NCM_XMT_NTB_MAX_SIZE, CFG_CDC_NCM_RCV_NTB_MAX_SIZE),
7177
sizeof(struct udc_buf_info), NULL);
7278

7379

@@ -77,16 +83,16 @@ NET_BUF_POOL_FIXED_DEFINE(cdc_ncm_ep_pool,
7783
static struct ntb_parameters_t ntb_parameters = {
7884
.wLength = sys_cpu_to_le16(sizeof(struct ntb_parameters_t)),
7985
.bmNtbFormatsSupported = sys_cpu_to_le16(0x01), // 16-bit NTB supported
80-
.dwNtbInMaxSize = sys_cpu_to_le32(CONFIG_CDC_NCM_XMT_NTB_MAX_SIZE),
86+
.dwNtbInMaxSize = sys_cpu_to_le32(CFG_CDC_NCM_XMT_NTB_MAX_SIZE),
8187
.wNdbInDivisor = sys_cpu_to_le16(4),
8288
.wNdbInPayloadRemainder = sys_cpu_to_le16(0),
83-
.wNdbInAlignment = sys_cpu_to_le16(CONFIG_CDC_NCM_ALIGNMENT),
89+
.wNdbInAlignment = sys_cpu_to_le16(CFG_CDC_NCM_ALIGNMENT),
8490
.wReserved = sys_cpu_to_le16(0),
85-
.dwNtbOutMaxSize = sys_cpu_to_le32(CONFIG_CDC_NCM_RCV_NTB_MAX_SIZE),
91+
.dwNtbOutMaxSize = sys_cpu_to_le32(CFG_CDC_NCM_RCV_NTB_MAX_SIZE),
8692
.wNdbOutDivisor = sys_cpu_to_le16(4),
8793
.wNdbOutPayloadRemainder = sys_cpu_to_le16(0),
88-
.wNdbOutAlignment = sys_cpu_to_le16(CONFIG_CDC_NCM_ALIGNMENT),
89-
.wNtbOutMaxDatagrams = sys_cpu_to_le16(CONFIG_CDC_NCM_RCV_MAX_DATAGRAMS_PER_NTB)
94+
.wNdbOutAlignment = sys_cpu_to_le16(CFG_CDC_NCM_ALIGNMENT),
95+
.wNtbOutMaxDatagrams = sys_cpu_to_le16(CFG_CDC_NCM_RCV_MAX_DATAGRAMS_PER_NTB)
9096
};
9197

9298
static struct ncm_notify_network_connection_t ncm_notify_connected = {
@@ -337,9 +343,9 @@ static bool _cdc_ncm_frame_ok(struct cdc_ncm_eth_data *data, struct net_buf *con
337343
LOG_ERR(" ill block length: %d > %d", sys_le16_to_cpu(nth16->wBlockLength), len);
338344
return false;
339345
}
340-
if (sys_le16_to_cpu(nth16->wBlockLength) > CONFIG_CDC_NCM_RCV_NTB_MAX_SIZE)
346+
if (sys_le16_to_cpu(nth16->wBlockLength) > CFG_CDC_NCM_RCV_NTB_MAX_SIZE)
341347
{
342-
LOG_ERR(" ill block length2: %d > %d", sys_le16_to_cpu(nth16->wBlockLength), CONFIG_CDC_NCM_RCV_NTB_MAX_SIZE);
348+
LOG_ERR(" ill block length2: %d > %d", sys_le16_to_cpu(nth16->wBlockLength), CFG_CDC_NCM_RCV_NTB_MAX_SIZE);
343349
return false;
344350
}
345351
if (sys_le16_to_cpu(nth16->wNdpIndex) < sizeof(nth16) || sys_le16_to_cpu(nth16->wNdpIndex) > len - (sizeof(struct ndp16_t) + 2*sizeof(struct ndp16_datagram_t)))
@@ -379,7 +385,7 @@ static bool _cdc_ncm_frame_ok(struct cdc_ncm_eth_data *data, struct net_buf *con
379385
int ndx = 0;
380386
uint16_t max_ndx = (uint16_t)((sys_le16_to_cpu(ndp16->wLength) - sizeof(struct ndp16_t)) / sizeof(struct ndp16_datagram_t));
381387

382-
if (max_ndx > CONFIG_CDC_NCM_RCV_MAX_DATAGRAMS_PER_NTB + 1)
388+
if (max_ndx > CFG_CDC_NCM_RCV_MAX_DATAGRAMS_PER_NTB + 1)
383389
{
384390
// number of datagrams in NTB > 1
385391
LOG_ERR("<<xyx %d (%d)", max_ndx - 1, sys_le16_to_cpu(ntb->nth.wBlockLength));
@@ -901,7 +907,7 @@ static int cdc_ncm_send(const struct device *dev, struct net_pkt *const pkt)
901907
ntb->nth.wNdpIndex = sys_cpu_to_le16(sizeof(struct nth16_t));
902908

903909
ntb->ndp.dwSignature = sys_cpu_to_le32(NDP16_SIGNATURE_NCM0);
904-
ntb->ndp.wLength = sys_cpu_to_le16(sizeof(struct ndp16_t) + (CONFIG_CDC_NCM_XMT_MAX_DATAGRAMS_PER_NTB + 1)*sizeof(struct ndp16_datagram_t));
910+
ntb->ndp.wLength = sys_cpu_to_le16(sizeof(struct ndp16_t) + (CFG_CDC_NCM_XMT_MAX_DATAGRAMS_PER_NTB + 1)*sizeof(struct ndp16_datagram_t));
905911
ntb->ndp.wNextNdpIndex = 0;
906912

907913
ntb->ndp_datagram[0].wDatagramIndex = sys_cpu_to_le16(sys_le16_to_cpu(ntb->nth.wHeaderLength) + sys_le16_to_cpu(ntb->ndp.wLength));

subsys/usb/device_next/class/usbd_cdc_ncm_local.h renamed to subsys/usb/device_next/class/usbd_cdc_ncm_private.h

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,17 @@
2929
#define __USBD_CDC_NCM_LOCAL_H_
3030

3131

32-
#ifndef CONFIG_CDC_NCM_ALIGNMENT
33-
#define CONFIG_CDC_NCM_ALIGNMENT 4
32+
#ifndef CFG_CDC_NCM_ALIGNMENT
33+
#define CFG_CDC_NCM_ALIGNMENT 4
3434
#endif
35-
#if (CONFIG_CDC_NCM_ALIGNMENT != 4)
36-
#error "CONFIG_CDC_NCM_ALIGNMENT must be 4, otherwise the headers and start of datagrams have to be aligned (which they are currently not)"
35+
#if (CFG_CDC_NCM_ALIGNMENT != 4)
36+
#error "CFG_CDC_NCM_ALIGNMENT must be 4, otherwise the headers and start of datagrams have to be aligned (which they are currently not)"
3737
#endif
3838

39-
#define CONFIG_CDC_NCM_XMT_MAX_DATAGRAMS_PER_NTB 1
40-
#define CONFIG_CDC_NCM_RCV_MAX_DATAGRAMS_PER_NTB 1
41-
#define CONFIG_CDC_NCM_XMT_NTB_MAX_SIZE 3200 // see discussion in https://github.com/hathach/tinyusb/pull/2227
42-
#define CONFIG_CDC_NCM_RCV_NTB_MAX_SIZE 3200
43-
44-
#if (CONFIG_CDC_NCM_XMT_NTB_MAX_SIZE != CONFIG_CDC_NCM_RCV_NTB_MAX_SIZE)
45-
#error "CONFIG_CDC_NCM_XMT_NTB_MAX_SIZE != CONFIG_CDC_NCM_RCV_NTB_MAX_SIZE"
46-
#endif
39+
#define CFG_CDC_NCM_XMT_MAX_DATAGRAMS_PER_NTB 1
40+
#define CFG_CDC_NCM_RCV_MAX_DATAGRAMS_PER_NTB 1
41+
#define CFG_CDC_NCM_XMT_NTB_MAX_SIZE 2048 // min 2048 according to spec 6.2.7
42+
#define CFG_CDC_NCM_RCV_NTB_MAX_SIZE 3200 // see discussion in https://github.com/hathach/tinyusb/pull/2227
4743

4844
// Table 6.2 Class-Specific Request Codes for Network Control Model subclass
4945
typedef enum
@@ -123,17 +119,17 @@ union xmit_ntb_t {
123119
struct {
124120
struct nth16_t nth;
125121
struct ndp16_t ndp;
126-
struct ndp16_datagram_t ndp_datagram[CONFIG_CDC_NCM_XMT_MAX_DATAGRAMS_PER_NTB + 1];
122+
struct ndp16_datagram_t ndp_datagram[CFG_CDC_NCM_XMT_MAX_DATAGRAMS_PER_NTB + 1];
127123
};
128-
uint8_t data[CONFIG_CDC_NCM_XMT_NTB_MAX_SIZE];
124+
uint8_t data[CFG_CDC_NCM_XMT_NTB_MAX_SIZE];
129125
} __packed;
130126

131127
union recv_ntb_t {
132128
struct {
133129
struct nth16_t nth;
134130
// only the header is at a guaranteed position
135131
};
136-
uint8_t data[CONFIG_CDC_NCM_RCV_NTB_MAX_SIZE];
132+
uint8_t data[CFG_CDC_NCM_RCV_NTB_MAX_SIZE];
137133
} __packed;
138134

139135
// network endianess = LE!

0 commit comments

Comments
 (0)