Module Name: src
Committed By: maya
Date: Mon Jan 20 13:54:55 UTC 2025
Modified Files:
src/distrib/sets/lists/debug: module.mi
src/distrib/sets/lists/man: mi
src/distrib/sets/lists/manhtml: mi
src/distrib/sets/lists/modules: mi
src/share/man/man4: Makefile
src/sys/dev: DEVNAMES
src/sys/dev/usb: files.usb usbdevices.config
src/sys/modules: Makefile
Added Files:
src/share/man/man4: ncm.4
src/sys/dev/usb: if_ncm.c if_ncmreg.h
src/sys/modules/if_ncm: Makefile if_ncm.ioconf
Log Message:
Add ncm(4) a driver for USB Network Control Model
Seen on my Google Pixel 8, which implements ncm instead of urndis.
To generate a diff of this commit:
cvs rdiff -u -r1.29 -r1.30 src/distrib/sets/lists/debug/module.mi
cvs rdiff -u -r1.1793 -r1.1794 src/distrib/sets/lists/man/mi
cvs rdiff -u -r1.15 -r1.16 src/distrib/sets/lists/manhtml/mi
cvs rdiff -u -r1.161 -r1.162 src/distrib/sets/lists/modules/mi
cvs rdiff -u -r1.738 -r1.739 src/share/man/man4/Makefile
cvs rdiff -u -r0 -r1.1 src/share/man/man4/ncm.4
cvs rdiff -u -r1.331 -r1.332 src/sys/dev/DEVNAMES
cvs rdiff -u -r1.180 -r1.181 src/sys/dev/usb/files.usb
cvs rdiff -u -r0 -r1.1 src/sys/dev/usb/if_ncm.c src/sys/dev/usb/if_ncmreg.h
cvs rdiff -u -r1.45 -r1.46 src/sys/dev/usb/usbdevices.config
cvs rdiff -u -r1.293 -r1.294 src/sys/modules/Makefile
cvs rdiff -u -r0 -r1.1 src/sys/modules/if_ncm/Makefile \
src/sys/modules/if_ncm/if_ncm.ioconf
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: src/distrib/sets/lists/debug/module.mi
diff -u src/distrib/sets/lists/debug/module.mi:1.29 src/distrib/sets/lists/debug/module.mi:1.30
--- src/distrib/sets/lists/debug/module.mi:1.29 Tue Nov 5 07:58:44 2024
+++ src/distrib/sets/lists/debug/module.mi Mon Jan 20 13:54:54 2025
@@ -1,4 +1,4 @@
-# $NetBSD: module.mi,v 1.29 2024/11/05 07:58:44 martin Exp $
+# $NetBSD: module.mi,v 1.30 2025/01/20 13:54:54 maya Exp $
#
./usr/libdata/debug/@MODULEDIR@ modules-base-kernel kmod,debug
./usr/libdata/debug/@MODULEDIR@/accf_dataready modules-base-kernel kmod,debug
@@ -211,6 +211,8 @@
./usr/libdata/debug/@MODULEDIR@/if_mpls/if_mpls.kmod.debug modules-base-kernel kmod,debug
./usr/libdata/debug/@MODULEDIR@/if_mue modules-base-kernel kmod,debug
./usr/libdata/debug/@MODULEDIR@/if_mue/if_mue.kmod.debug modules-base-kernel kmod,debug
+./usr/libdata/debug/@MODULEDIR@/if_ncm modules-base-kernel kmod,debug
+./usr/libdata/debug/@MODULEDIR@/if_ncm/if_ncm.kmod.debug modules-base-kernel kmod,debug
./usr/libdata/debug/@MODULEDIR@/if_npflog modules-base-kernel kmod,debug
./usr/libdata/debug/@MODULEDIR@/if_npflog/if_npflog.kmod.debug modules-base-kernel kmod,debug
./usr/libdata/debug/@MODULEDIR@/if_ppp modules-base-kernel kmod,debug
Index: src/distrib/sets/lists/man/mi
diff -u src/distrib/sets/lists/man/mi:1.1793 src/distrib/sets/lists/man/mi:1.1794
--- src/distrib/sets/lists/man/mi:1.1793 Mon Dec 16 16:37:39 2024
+++ src/distrib/sets/lists/man/mi Mon Jan 20 13:54:54 2025
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.1793 2024/12/16 16:37:39 brad Exp $
+# $NetBSD: mi,v 1.1794 2025/01/20 13:54:54 maya Exp $
#
# Note: don't delete entries from here - mark them as "obsolete" instead.
./etc/mtree/set.man man-sys-root
@@ -1631,6 +1631,7 @@
./usr/share/man/cat4/mvsata.0 man-sys-catman .cat
./usr/share/man/cat4/nadb.0 man-sys-catman .cat
./usr/share/man/cat4/nca.0 man-sys-catman .cat
+./usr/share/man/cat4/ncm.0 man-sys-catman .cat
./usr/share/man/cat4/ncr.0 man-obsolete obsolete
./usr/share/man/cat4/nct.0 man-sys-catman .cat
./usr/share/man/cat4/ne.0 man-sys-catman .cat
@@ -5202,6 +5203,7 @@
./usr/share/man/man4/mvsata.4 man-sys-man .man
./usr/share/man/man4/nadb.4 man-sys-man .man
./usr/share/man/man4/nca.4 man-sys-man .man
+./usr/share/man/man4/ncm.4 man-sys-man .man
./usr/share/man/man4/ncr.4 man-obsolete obsolete
./usr/share/man/man4/nct.4 man-sys-man .man
./usr/share/man/man4/ne.4 man-sys-man .man
Index: src/distrib/sets/lists/manhtml/mi
diff -u src/distrib/sets/lists/manhtml/mi:1.15 src/distrib/sets/lists/manhtml/mi:1.16
--- src/distrib/sets/lists/manhtml/mi:1.15 Mon Dec 16 16:37:39 2024
+++ src/distrib/sets/lists/manhtml/mi Mon Jan 20 13:54:54 2025
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.15 2024/12/16 16:37:39 brad Exp $
+# $NetBSD: mi,v 1.16 2025/01/20 13:54:54 maya Exp $
#
# Note: don't delete entries from here - mark them as "obsolete" instead.
#
@@ -1474,6 +1474,7 @@
./usr/share/man/html4/mvsata.html man-sys-htmlman html
./usr/share/man/html4/nadb.html man-sys-htmlman html
./usr/share/man/html4/nca.html man-sys-htmlman html
+./usr/share/man/html4/ncm.html man-sys-htmlman html
./usr/share/man/html4/nct.html man-sys-htmlman html
./usr/share/man/html4/ne.html man-sys-htmlman html
./usr/share/man/html4/nele.html man-sys-htmlman html
Index: src/distrib/sets/lists/modules/mi
diff -u src/distrib/sets/lists/modules/mi:1.161 src/distrib/sets/lists/modules/mi:1.162
--- src/distrib/sets/lists/modules/mi:1.161 Mon Nov 4 20:43:38 2024
+++ src/distrib/sets/lists/modules/mi Mon Jan 20 13:54:54 2025
@@ -1,4 +1,4 @@
-# $NetBSD: mi,v 1.161 2024/11/04 20:43:38 brad Exp $
+# $NetBSD: mi,v 1.162 2025/01/20 13:54:54 maya Exp $
#
# Note: don't delete entries from here - mark them as "obsolete" instead.
#
@@ -233,6 +233,8 @@
./@MODULEDIR@/if_mpls/if_mpls.kmod modules-base-kernel kmod
./@MODULEDIR@/if_mue modules-base-kernel kmod
./@MODULEDIR@/if_mue/if_mue.kmod modules-base-kernel kmod
+./@MODULEDIR@/if_ncm modules-base-kernel kmod
+./@MODULEDIR@/if_ncm/if_ncm.kmod modules-base-kernel kmod
./@MODULEDIR@/if_npflog modules-base-kernel kmod
./@MODULEDIR@/if_npflog/if_npflog.kmod modules-base-kernel kmod
./@MODULEDIR@/if_ppp modules-base-kernel kmod
Index: src/share/man/man4/Makefile
diff -u src/share/man/man4/Makefile:1.738 src/share/man/man4/Makefile:1.739
--- src/share/man/man4/Makefile:1.738 Mon Dec 16 16:37:38 2024
+++ src/share/man/man4/Makefile Mon Jan 20 13:54:54 2025
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.738 2024/12/16 16:37:38 brad Exp $
+# $NetBSD: Makefile,v 1.739 2025/01/20 13:54:54 maya Exp $
# @(#)Makefile 8.1 (Berkeley) 6/18/93
MAN= aac.4 ac97.4 acardide.4 aceride.4 acphy.4 \
@@ -83,6 +83,7 @@ MAN= aac.4 ac97.4 acardide.4 aceride.4 a
# USB devices
MAN+= atu.4 aubtfwl.4 aue.4 axe.4 axen.4 cdce.4 cue.4 ehci.4 kue.4 \
mos.4 mue.4 \
+ ncm.4 \
ohci.4 \
slhci.4 slurm.4 stuirda.4 u3g.4 ualea.4 uatp.4 uaudio.4 uberry.4 \
ubsa.4 ubt.4 uchcom.4 \
Index: src/sys/dev/DEVNAMES
diff -u src/sys/dev/DEVNAMES:1.331 src/sys/dev/DEVNAMES:1.332
--- src/sys/dev/DEVNAMES:1.331 Tue Jun 29 10:22:33 2021
+++ src/sys/dev/DEVNAMES Mon Jan 20 13:54:55 2025
@@ -1,4 +1,4 @@
-# $NetBSD: DEVNAMES,v 1.331 2021/06/29 10:22:33 nia Exp $
+# $NetBSD: DEVNAMES,v 1.332 2025/01/20 13:54:55 maya Exp $
#
# This file contains all used device names and defined attributes in
# alphabetical order. New devices added to the system somewhere should first
@@ -920,6 +920,7 @@ mvgbe MI
mvpex MI
mvsata MI
nca MI
+ncm MI
ncr5380sbc MI Attribute
ncr53c9x MI Attribute
ncrsc mvme68k
Index: src/sys/dev/usb/files.usb
diff -u src/sys/dev/usb/files.usb:1.180 src/sys/dev/usb/files.usb:1.181
--- src/sys/dev/usb/files.usb:1.180 Mon Dec 16 16:37:38 2024
+++ src/sys/dev/usb/files.usb Mon Jan 20 13:54:55 2025
@@ -1,4 +1,4 @@
-# $NetBSD: files.usb,v 1.180 2024/12/16 16:37:38 brad Exp $
+# $NetBSD: files.usb,v 1.181 2025/01/20 13:54:55 maya Exp $
#
# Config file and device description for machine-independent USB code.
# Included by ports that need it. Ports that use it must provide
@@ -376,6 +376,11 @@ device mue: arp, ether, ifnet, mii, mii_
attach mue at usbdevif
file dev/usb/if_mue.c mue
+# Network Control Model
+device ncm: arp, ether, ifnet, usbnet
+attach ncm at usbifif
+file dev/usb/if_ncm.c ncm
+
# Atheros AR9170
device otus: arp, ether, firmload, ifnet, wlan
attach otus at usbdevif
Index: src/sys/dev/usb/usbdevices.config
diff -u src/sys/dev/usb/usbdevices.config:1.45 src/sys/dev/usb/usbdevices.config:1.46
--- src/sys/dev/usb/usbdevices.config:1.45 Mon Dec 16 16:37:38 2024
+++ src/sys/dev/usb/usbdevices.config Mon Jan 20 13:54:55 2025
@@ -1,4 +1,4 @@
-# $NetBSD: usbdevices.config,v 1.45 2024/12/16 16:37:38 brad Exp $
+# $NetBSD: usbdevices.config,v 1.46 2025/01/20 13:54:55 maya Exp $
#
# This file contains all USB related configuration.
# It is suitable for inclusion in a kernel config(5) file.
@@ -132,6 +132,7 @@ cue* at uhub? port ? # CATC USB-EL1201A
kue* at uhub? port ? # Kawasaki LSI KL5KUSB101B based adapters
mos* at uhub? port ? # Moschip MCS7730/MCS7830/MCS7832 based adapters
mue* at uhub? port ? # Microchip LAN75xx/LAN78xx based adapters
+ncm* at uhub? port ? # NCM, Network Control Model
udav* at uhub? port ? # Davicom DM9601 based adapters
#umb* at uhub? port ? # Mobile Broadband Interface Model (EXPERIMENTAL)
ure* at uhub? port ? # Realtek RTL8152/RTL8153 based adapters
Index: src/sys/modules/Makefile
diff -u src/sys/modules/Makefile:1.293 src/sys/modules/Makefile:1.294
--- src/sys/modules/Makefile:1.293 Mon Nov 4 20:43:38 2024
+++ src/sys/modules/Makefile Mon Jan 20 13:54:55 2025
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.293 2024/11/04 20:43:38 brad Exp $
+# $NetBSD: Makefile,v 1.294 2025/01/20 13:54:55 maya Exp $
.include <bsd.own.mk>
@@ -96,6 +96,7 @@ SUBDIR+= if_lagg
SUBDIR+= if_loop
SUBDIR+= if_mpls
SUBDIR+= if_mue
+SUBDIR+= if_ncm
SUBDIR+= if_npflog
SUBDIR+= if_ppp
SUBDIR+= if_pppoe
Added files:
Index: src/share/man/man4/ncm.4
diff -u /dev/null src/share/man/man4/ncm.4:1.1
--- /dev/null Mon Jan 20 13:54:55 2025
+++ src/share/man/man4/ncm.4 Mon Jan 20 13:54:54 2025
@@ -0,0 +1,74 @@
+.\" $NetBSD: ncm.4,v 1.1 2025/01/20 13:54:54 maya Exp $
+.\"
+.\"
+.\" Copyright (c) 2025 The NetBSD Foundation, Inc.
+.\" All rights reserved.
+.\"
+.\" This code is derived from software contributed to The NetBSD Foundation
+.\" by Maya Rashish
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\" notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\" notice, this list of conditions and the following disclaimer in the
+.\" documentation and/or other materials provided with the distribution.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+.\" POSSIBILITY OF SUCH DAMAGE.
+.\"
+.Dd January 20, 2025
+.Dt NCM 4
+.Os
+.Sh NAME
+.Nm ncm
+.Nd USB Network Control Model driver
+.Sh SYNOPSIS
+.Cd "ncm* at uhub? port ?"
+.Sh DESCRIPTION
+The
+.Nm
+driver provides support for USB Network Control Model devices, often appearing
+as hotspot USB tethering in Android devices on devices such as the following:
+.Pp
+.Bl -bullet -compact
+.It
+Google Pixel 8
+.Sh SEE ALSO
+.Xr arp 4 ,
+.Xr intro 4 ,
+.Xr netintro 4 ,
+.Xr usb 4 ,
+.Xr usbnet 4 ,
+.Xr ifconfig.if 5 ,
+.Xr ifconfig 8
+.Rs
+.%T "Universal Serial Bus Class Communications Class Subclass Specification for Network Control Model Devices"
+.%U https://www.usb.org/sites/default/files/NCM10_012011.zip
+.Re
+.Sh HISTORY
+The
+.Nm
+device driver first appeared in
+.Nx 11.0 .
+.Sh AUTHORS
+.An -nosplit
+The
+.Nm
+driver was written by
+.An Maya Rashish Aq Mt [email protected]
+based on the
+.Xr cdce 4
+driver written by
+.An Craig Boston Aq Mt [email protected] .
Index: src/sys/dev/usb/if_ncm.c
diff -u /dev/null src/sys/dev/usb/if_ncm.c:1.1
--- /dev/null Mon Jan 20 13:54:55 2025
+++ src/sys/dev/usb/if_ncm.c Mon Jan 20 13:54:55 2025
@@ -0,0 +1,368 @@
+/* $NetBSD: if_ncm.c,v 1.1 2025/01/20 13:54:55 maya Exp $ */
+
+/*
+ * Copyright (c) 1997, 1998, 1999, 2000-2003 Bill Paul <[email protected]>
+ * Copyright (c) 2003 Craig Boston
+ * Copyright (c) 2004 Daniel Hartmeier
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by Bill Paul.
+ * 4. Neither the name of the author nor the names of any co-contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul, THE VOICES IN HIS HEAD OR
+ * THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/*
+ * USB Network Control Model (NCM)
+ * https://www.usb.org/sites/default/files/NCM10_012011.zip
+ */
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: if_ncm.c,v 1.1 2025/01/20 13:54:55 maya Exp $");
+
+#include <sys/param.h>
+#include <sys/types.h>
+
+#include <sys/device.h>
+
+#include <dev/usb/usbnet.h>
+#include <dev/usb/usbcdc.h>
+
+#include <dev/usb/if_ncmreg.h>
+
+struct ncm_softc {
+ struct usbnet ncm_un;
+ uint32_t sc_tx_seq;
+};
+
+static int ncm_match(device_t, cfdata_t, void *);
+static void ncm_attach(device_t, device_t, void *);
+
+CFATTACH_DECL_NEW(ncm, sizeof(struct ncm_softc), ncm_match, ncm_attach,
+ usbnet_detach, usbnet_activate);
+
+static void ncm_uno_rx_loop(struct usbnet *, struct usbnet_chain *,
+ uint32_t);
+static unsigned ncm_uno_tx_prepare(struct usbnet *, struct mbuf *,
+ struct usbnet_chain *);
+
+static const struct usbnet_ops ncm_ops = {
+ .uno_tx_prepare = ncm_uno_tx_prepare,
+ .uno_rx_loop = ncm_uno_rx_loop,
+};
+
+static int
+ncm_match(device_t parent, cfdata_t match, void *aux)
+{
+ struct usbif_attach_arg *uiaa = aux;
+
+ if (uiaa->uiaa_class == UICLASS_CDC && uiaa->uiaa_subclass ==
+ UISUBCLASS_NETWORK_CONTROL_MODEL)
+ return UMATCH_IFACECLASS_GENERIC;
+
+ return UMATCH_NONE;
+}
+
+static void
+ncm_attach(device_t parent, device_t self, void *aux)
+{
+ struct ncm_softc *sc = device_private(self);
+ struct usbnet * const un = &sc->ncm_un;
+ struct usbif_attach_arg *uiaa = aux;
+ char *devinfop;
+ struct usbd_device *dev = uiaa->uiaa_device;
+ usb_interface_descriptor_t *id;
+ usb_endpoint_descriptor_t *ed;
+ const usb_cdc_union_descriptor_t *ud;
+ usb_config_descriptor_t *cd;
+ int data_ifcno;
+ int i, j, numalts;
+ const usb_cdc_ethernet_descriptor_t *ue;
+ char eaddr_str[USB_MAX_ENCODED_STRING_LEN];
+ usb_device_request_t req;
+ struct ncm_ntb_parameters np;
+
+
+ aprint_naive("\n");
+ aprint_normal("\n");
+ devinfop = usbd_devinfo_alloc(dev, 0);
+ aprint_normal_dev(self, "%s\n", devinfop);
+ usbd_devinfo_free(devinfop);
+
+ un->un_dev = self;
+ un->un_udev = dev;
+ un->un_sc = sc;
+ un->un_ops = &ncm_ops;
+ un->un_rx_xfer_flags = USBD_SHORT_XFER_OK;
+ un->un_tx_xfer_flags = USBD_FORCE_SHORT_XFER;
+
+ ud = (const usb_cdc_union_descriptor_t *)usb_find_desc_if(un->un_udev,
+ UDESC_CS_INTERFACE, UDESCSUB_CDC_UNION,
+ usbd_get_interface_descriptor(uiaa->uiaa_iface));
+ if (ud == NULL) {
+ aprint_error_dev(self, "no union descriptor\n");
+ return;
+ }
+ data_ifcno = ud->bSlaveInterface[0];
+
+ for (i = 0; i < uiaa->uiaa_nifaces; i++) {
+ if (uiaa->uiaa_ifaces[i] != NULL) {
+ id = usbd_get_interface_descriptor(
+ uiaa->uiaa_ifaces[i]);
+ if (id != NULL && id->bInterfaceNumber ==
+ data_ifcno) {
+ un->un_iface = uiaa->uiaa_ifaces[i];
+ uiaa->uiaa_ifaces[i] = NULL;
+ }
+ }
+ }
+ if (un->un_iface == NULL) {
+ aprint_error_dev(self, "no data interface\n");
+ return;
+ }
+
+ /*
+ * <quote>
+ * The Data Class interface of a networking device shall have a minimum
+ * of two interface settings. The first setting (the default interface
+ * setting) includes no endpoints and therefore no networking traffic is
+ * exchanged whenever the default interface setting is selected. One or
+ * more additional interface settings are used for normal operation, and
+ * therefore each includes a pair of endpoints (one IN, and one OUT) to
+ * exchange network traffic. Select an alternate interface setting to
+ * initialize the network aspects of the device and to enable the
+ * exchange of network traffic.
+ * </quote>
+ *
+ * Some devices, most notably cable modems, include interface settings
+ * that have no IN or OUT endpoint, therefore loop through the list of all
+ * available interface settings looking for one with both IN and OUT
+ * endpoints.
+ */
+ id = usbd_get_interface_descriptor(un->un_iface);
+ cd = usbd_get_config_descriptor(un->un_udev);
+ numalts = usbd_get_no_alts(cd, id->bInterfaceNumber);
+
+ for (j = 0; j < numalts; j++) {
+ if (usbd_set_interface(un->un_iface, j)) {
+ aprint_error_dev(un->un_dev,
+ "setting alternate interface failed\n");
+ return;
+ }
+ /* Find endpoints. */
+ id = usbd_get_interface_descriptor(un->un_iface);
+ un->un_ed[USBNET_ENDPT_RX] = un->un_ed[USBNET_ENDPT_TX] = 0;
+ for (i = 0; i < id->bNumEndpoints; i++) {
+ ed = usbd_interface2endpoint_descriptor(un->un_iface, i);
+ if (!ed) {
+ aprint_error_dev(self,
+ "could not read endpoint descriptor\n");
+ return;
+ }
+ if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
+ UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
+ un->un_ed[USBNET_ENDPT_RX] = ed->bEndpointAddress;
+ } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
+ UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
+ un->un_ed[USBNET_ENDPT_TX] = ed->bEndpointAddress;
+ } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
+ UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
+ /* XXX: CDC spec defines an interrupt pipe, but it is not
+ * needed for simple host-to-host applications. */
+ } else {
+ aprint_error_dev(self, "unexpected endpoint\n");
+ }
+ }
+ /* If we found something, try and use it... */
+ if (un->un_ed[USBNET_ENDPT_RX] != 0 && un->un_ed[USBNET_ENDPT_TX] != 0)
+ break;
+ }
+
+ if (un->un_ed[USBNET_ENDPT_RX] == 0) {
+ aprint_error_dev(self, "could not find data bulk in\n");
+ return;
+ }
+ if (un->un_ed[USBNET_ENDPT_TX] == 0) {
+ aprint_error_dev(self, "could not find data bulk out\n");
+ return;
+ }
+
+ ue = (const usb_cdc_ethernet_descriptor_t *)usb_find_desc_if(dev,
+ UDESC_CS_INTERFACE, UDESCSUB_CDC_ENF,
+ usbd_get_interface_descriptor(uiaa->uiaa_iface));
+ if (!ue || usbd_get_string(dev, ue->iMacAddress, eaddr_str) ||
+ ether_aton_r(un->un_eaddr, sizeof(un->un_eaddr), eaddr_str)) {
+ aprint_normal_dev(self, "faking address\n");
+ un->un_eaddr[0] = 0x2a;
+ uint32_t ticks = getticks();
+ memcpy(&un->un_eaddr[1], &ticks, sizeof(ticks));
+ un->un_eaddr[5] = (uint8_t)(device_unit(un->un_dev));
+ }
+
+ /* Query NTB tranfers sizes */
+ req.bmRequestType = UT_READ_CLASS_INTERFACE;
+ req.bRequest = NCM_GET_NTB_PARAMETERS;
+ USETW(req.wValue, 0);
+ USETW(req.wIndex, uiaa->uiaa_ifaceno);
+ USETW(req.wLength, sizeof(np));
+ if (usbd_do_request(un->un_udev, &req, &np) != USBD_NORMAL_COMPLETION ||
+ UGETW(np.wLength) != sizeof(np)) {
+ aprint_error_dev(un->un_dev,
+ "NCM_GET_NTB_PARAMETERS failed\n");
+ return;
+ }
+ un->un_rx_list_cnt = 1;
+ un->un_tx_list_cnt = 1;
+ un->un_rx_bufsz = UGETDW(np.dwNtbInMaxSize);
+ un->un_tx_bufsz = UGETDW(np.dwNtbOutMaxSize);
+ if (un->un_tx_bufsz < NCM_MIN_TX_BUFSZ) {
+ aprint_error_dev(un->un_dev, "dwNtbOutMaxSize %u too small\n",
+ un->un_tx_bufsz);
+ return;
+ }
+
+ usbnet_attach(un);
+ usbnet_attach_ifp(un, IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST,
+ 0, NULL);
+
+ /* XXX There is no link state, pretend we are always on */
+ if_link_state_change(usbnet_ifp(un), LINK_STATE_UP);
+}
+
+static void
+ncm_uno_rx_loop(struct usbnet *un, struct usbnet_chain *c, uint32_t total_len)
+{
+ struct ifnet *ifp = usbnet_ifp(un);
+ uint8_t *buf = c->unc_buf;
+ struct ncm_header16 *hdr;
+ struct ncm_dptab16 *ptr;
+ unsigned i;
+
+ if (total_len < sizeof(*hdr) + sizeof(*ptr)) {
+ aprint_error_dev(un->un_dev, "got a too small usb message\n");
+ if_statinc(ifp, if_ierrors);
+ return;
+ }
+ hdr = (struct ncm_header16 *)buf;
+ if (UGETDW(hdr->dwSignature) != NCM_HDR16_SIG) {
+ aprint_error_dev(un->un_dev,
+ "got a non NCM_HDR16_SIG header %08x\n",
+ UGETDW(hdr->dwSignature));
+ return;
+ }
+ const unsigned ndp_index = UGETW(hdr->wNdpIndex);
+
+ if (ndp_index < sizeof(*hdr) ||
+ ndp_index > total_len - sizeof(*ptr)) {
+ aprint_error_dev(un->un_dev, "ndp start offset %d "
+ "bigger than data sizeof(*hdr) %zu sizeof(*ptr) %zu "
+ "total_len %u\n", ndp_index,
+ sizeof(*hdr), sizeof(*ptr),
+ total_len);
+ if_statinc(ifp, if_ierrors);
+ return;
+ }
+ ptr = (struct ncm_dptab16 *)(buf + ndp_index);
+ if (UGETDW(ptr->dwSignature) != NCM_PTR16_SIG_NO_CRC) {
+ aprint_error_dev(un->un_dev, "ncm dptab16 signature %08x is "
+ "weird\n", UGETDW(ptr->dwSignature));
+ if_statinc(ifp, if_ierrors);
+ return;
+ }
+
+ if ((unsigned)UGETW(ptr->wLength) > total_len - ndp_index) {
+ aprint_error_dev(un->un_dev, "dptab16 wlength %u goes off end "
+ "ndp_index %u total_len %u\n", UGETW(ptr->wLength),
+ ndp_index, total_len);
+ if_statinc(ifp, if_ierrors);
+ return;
+ }
+
+ const unsigned max_datagrams = (UGETW(ptr->wLength) -
+ offsetof(struct ncm_dptab16, datagram))/sizeof(ptr->datagram[0]);
+ for (i = 0; i < max_datagrams; i++) {
+ uint16_t data_start = UGETW(ptr->datagram[i].wDatagramIndex);
+ uint16_t data_len = UGETW(ptr->datagram[i].wDatagramLength);
+
+ if (data_start == 0 || data_len == 0)
+ break;
+
+ if (data_len > total_len ||
+ data_start > total_len - data_len) {
+ aprint_error_dev(un->un_dev,
+ "datagram points out of buffer\n");
+ if_statinc(ifp, if_ierrors);
+ return;
+ }
+ usbnet_enqueue(un, buf + data_start, data_len, 0, 0, 0);
+ }
+}
+
+static unsigned
+ncm_uno_tx_prepare(struct usbnet *un, struct mbuf *m, struct usbnet_chain *c)
+{
+ struct ncm_softc *sc = un->un_sc;
+ struct ncm_dptab16 *ptr;
+ struct ncm_header16 *hdr;
+ unsigned hdr_len, len;
+
+ hdr_len = sizeof(*hdr) + sizeof(*ptr);
+ len = m->m_pkthdr.len;
+ KASSERT(hdr_len <= un->un_tx_bufsz);
+ KASSERT(len <= un->un_tx_bufsz - hdr_len);
+
+ hdr = (struct ncm_header16 *)c->unc_buf;
+ ptr = (struct ncm_dptab16 *)(hdr + 1);
+ memset(hdr, 0, sizeof(*hdr));
+ memset(ptr, 0, sizeof(*ptr));
+
+ USETDW(hdr->dwSignature, NCM_HDR16_SIG);
+ USETW(hdr->wHeaderLength, sizeof(*hdr));
+ USETW(hdr->wSequence, sc->sc_tx_seq);
+ sc->sc_tx_seq++;
+
+ USETW(hdr->wBlockLength, hdr_len + len);
+ USETW(hdr->wNdpIndex, sizeof(*hdr));
+
+ USETDW(ptr->dwSignature, NCM_PTR16_SIG_NO_CRC);
+ USETW(ptr->wLength, sizeof(*ptr));
+ USETW(ptr->wNextNdpIndex, 0);
+
+ USETW(ptr->datagram[0].wDatagramIndex, hdr_len);
+ USETW(ptr->datagram[0].wDatagramLength, len);
+
+ USETW(ptr->datagram[1].wDatagramIndex, 0);
+ USETW(ptr->datagram[1].wDatagramLength, 0);
+
+ m_copydata(m, 0, len, c->unc_buf + hdr_len);
+
+ return len + hdr_len;
+}
+
+#ifdef _MODULE
+#include "ioconf.c"
+#endif
+
+USBNET_MODULE(ncm)
Index: src/sys/dev/usb/if_ncmreg.h
diff -u /dev/null src/sys/dev/usb/if_ncmreg.h:1.1
--- /dev/null Mon Jan 20 13:54:55 2025
+++ src/sys/dev/usb/if_ncmreg.h Mon Jan 20 13:54:55 2025
@@ -0,0 +1,111 @@
+/* $NetBSD: if_ncmreg.h,v 1.1 2025/01/20 13:54:55 maya Exp $ */
+
+/*-
+ * Copyright (c) 2025 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Maya Rashish
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/*
+ * USB Network Control Model (NCM)
+ * https://www.usb.org/sites/default/files/NCM10_012011.zip
+ */
+
+#ifndef _NCM_H_
+#define _NCM_H_
+
+#include <dev/usb/usb.h>
+
+#define NCM_MIN_TX_BUFSZ \
+ (ETHERMTU + ETHER_HDR_LEN \
+ + sizeof(struct ncm_header16) \
+ + sizeof(struct ncm_pointer16))
+
+struct ncm_header16 {
+#define NCM_HDR16_SIG 0x484d434e
+ uDWord dwSignature;
+ uWord wHeaderLength;
+ uWord wSequence;
+ uWord wBlockLength;
+ uWord wNdpIndex;
+} UPACKED;
+
+struct ncm_header32 {
+#define NCM_HDR32_SIG 0x686d636e
+ uDWord dwSignature;
+ uWord wHeaderLength;
+ uWord wSequence;
+ uDWord dwBlockLength;
+ uDWord dwNdpIndex;
+} UPACKED;
+
+struct ncm_pointer16 {
+ uWord wDatagramIndex;
+ uWord wDatagramLength;
+} UPACKED;
+
+struct ncm_dptab16 {
+#define NCM_PTR16_SIG_NO_CRC 0x304d434e
+#define NCM_PTR16_SIG_CRC 0x314d434e
+ uDWord dwSignature;
+ uWord wLength;
+ uWord wNextNdpIndex;
+ struct ncm_pointer16 datagram[2];
+} UPACKED;
+
+struct ncm_pointer32 {
+ uDWord dwDatagramIndex;
+ uDWord dwDatagramLength;
+} UPACKED;
+
+struct ncm_dptab32 {
+#define NCM_PTR32_SIG_NO_CRC 0x306d636e
+#define NCM_PTR32_SIG_CRC 0x316d636e
+ uDWord dwSignature;
+ uWord wLength;
+ uWord wReserved6;
+ uWord wNextNdpIndex;
+ uDWord dwReserved12;
+ struct ncm_pointer32 datagram[2];
+} UPACKED;
+
+#define NCM_GET_NTB_PARAMETERS 0x80
+struct ncm_ntb_parameters {
+ uWord wLength;
+ uByte bmNtbFormatsSupported[2];
+ uDWord dwNtbInMaxSize;
+ uWord wNdpInDivisor;
+ uWord wNdpInPayloadRemainder;
+ uWord wNdpInAlignment;
+ uWord wReserved;
+ uDWord dwNtbOutMaxSize;
+ uWord wNdpOutDivisor;
+ uWord wNdpOutPayloadRemainder;
+ uWord wNdpOutAlignment;
+ uWord wNtbOutMaxDatagrams;
+} UPACKED;
+
+#endif /* _NCM_H_ */
Index: src/sys/modules/if_ncm/Makefile
diff -u /dev/null src/sys/modules/if_ncm/Makefile:1.1
--- /dev/null Mon Jan 20 13:54:55 2025
+++ src/sys/modules/if_ncm/Makefile Mon Jan 20 13:54:55 2025
@@ -0,0 +1,11 @@
+# $NetBSD: Makefile,v 1.1 2025/01/20 13:54:55 maya Exp $
+
+.include "../Makefile.inc"
+
+.PATH: ${S}/dev/usb
+
+KMOD= if_ncm
+IOCONF= if_ncm.ioconf
+SRCS= if_ncm.c
+
+.include <bsd.kmodule.mk>
Index: src/sys/modules/if_ncm/if_ncm.ioconf
diff -u /dev/null src/sys/modules/if_ncm/if_ncm.ioconf:1.1
--- /dev/null Mon Jan 20 13:54:55 2025
+++ src/sys/modules/if_ncm/if_ncm.ioconf Mon Jan 20 13:54:55 2025
@@ -0,0 +1,10 @@
+# $NetBSD: if_ncm.ioconf,v 1.1 2025/01/20 13:54:55 maya Exp $
+
+ioconf ncm
+
+include "conf/files"
+include "dev/usb/files.usb"
+
+pseudo-root usbifif*
+
+ncm* at usbifif?