1 /* $NetBSD: if_ncm.c,v 1.2 2025/08/18 20:59:55 andvar Exp $ */ 2 3 /* 4 * Copyright (c) 1997, 1998, 1999, 2000-2003 Bill Paul <wpaul (at) windriver.com> 5 * Copyright (c) 2003 Craig Boston 6 * Copyright (c) 2004 Daniel Hartmeier 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed by Bill Paul. 20 * 4. Neither the name of the author nor the names of any co-contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul, THE VOICES IN HIS HEAD OR 28 * THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 30 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 31 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 32 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 33 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 34 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 35 */ 36 37 /* 38 * USB Network Control Model (NCM) 39 * https://www.usb.org/sites/default/files/NCM10_012011.zip 40 */ 41 42 #include <sys/cdefs.h> 43 __KERNEL_RCSID(0, "$NetBSD: if_ncm.c,v 1.2 2025/08/18 20:59:55 andvar Exp $"); 44 45 #include <sys/param.h> 46 #include <sys/types.h> 47 48 #include <sys/device.h> 49 50 #include <dev/usb/usbnet.h> 51 #include <dev/usb/usbcdc.h> 52 53 #include <dev/usb/if_ncmreg.h> 54 55 struct ncm_softc { 56 struct usbnet ncm_un; 57 uint32_t sc_tx_seq; 58 }; 59 60 static int ncm_match(device_t, cfdata_t, void *); 61 static void ncm_attach(device_t, device_t, void *); 62 63 CFATTACH_DECL_NEW(ncm, sizeof(struct ncm_softc), ncm_match, ncm_attach, 64 usbnet_detach, usbnet_activate); 65 66 static void ncm_uno_rx_loop(struct usbnet *, struct usbnet_chain *, 67 uint32_t); 68 static unsigned ncm_uno_tx_prepare(struct usbnet *, struct mbuf *, 69 struct usbnet_chain *); 70 71 static const struct usbnet_ops ncm_ops = { 72 .uno_tx_prepare = ncm_uno_tx_prepare, 73 .uno_rx_loop = ncm_uno_rx_loop, 74 }; 75 76 static int 77 ncm_match(device_t parent, cfdata_t match, void *aux) 78 { 79 struct usbif_attach_arg *uiaa = aux; 80 81 if (uiaa->uiaa_class == UICLASS_CDC && uiaa->uiaa_subclass == 82 UISUBCLASS_NETWORK_CONTROL_MODEL) 83 return UMATCH_IFACECLASS_GENERIC; 84 85 return UMATCH_NONE; 86 } 87 88 static void 89 ncm_attach(device_t parent, device_t self, void *aux) 90 { 91 struct ncm_softc *sc = device_private(self); 92 struct usbnet * const un = &sc->ncm_un; 93 struct usbif_attach_arg *uiaa = aux; 94 char *devinfop; 95 struct usbd_device *dev = uiaa->uiaa_device; 96 usb_interface_descriptor_t *id; 97 usb_endpoint_descriptor_t *ed; 98 const usb_cdc_union_descriptor_t *ud; 99 usb_config_descriptor_t *cd; 100 int data_ifcno; 101 int i, j, numalts; 102 const usb_cdc_ethernet_descriptor_t *ue; 103 char eaddr_str[USB_MAX_ENCODED_STRING_LEN]; 104 usb_device_request_t req; 105 struct ncm_ntb_parameters np; 106 107 108 aprint_naive("\n"); 109 aprint_normal("\n"); 110 devinfop = usbd_devinfo_alloc(dev, 0); 111 aprint_normal_dev(self, "%s\n", devinfop); 112 usbd_devinfo_free(devinfop); 113 114 un->un_dev = self; 115 un->un_udev = dev; 116 un->un_sc = sc; 117 un->un_ops = &ncm_ops; 118 un->un_rx_xfer_flags = USBD_SHORT_XFER_OK; 119 un->un_tx_xfer_flags = USBD_FORCE_SHORT_XFER; 120 121 ud = (const usb_cdc_union_descriptor_t *)usb_find_desc_if(un->un_udev, 122 UDESC_CS_INTERFACE, UDESCSUB_CDC_UNION, 123 usbd_get_interface_descriptor(uiaa->uiaa_iface)); 124 if (ud == NULL) { 125 aprint_error_dev(self, "no union descriptor\n"); 126 return; 127 } 128 data_ifcno = ud->bSlaveInterface[0]; 129 130 for (i = 0; i < uiaa->uiaa_nifaces; i++) { 131 if (uiaa->uiaa_ifaces[i] != NULL) { 132 id = usbd_get_interface_descriptor( 133 uiaa->uiaa_ifaces[i]); 134 if (id != NULL && id->bInterfaceNumber == 135 data_ifcno) { 136 un->un_iface = uiaa->uiaa_ifaces[i]; 137 uiaa->uiaa_ifaces[i] = NULL; 138 } 139 } 140 } 141 if (un->un_iface == NULL) { 142 aprint_error_dev(self, "no data interface\n"); 143 return; 144 } 145 146 /* 147 * <quote> 148 * The Data Class interface of a networking device shall have a minimum 149 * of two interface settings. The first setting (the default interface 150 * setting) includes no endpoints and therefore no networking traffic is 151 * exchanged whenever the default interface setting is selected. One or 152 * more additional interface settings are used for normal operation, and 153 * therefore each includes a pair of endpoints (one IN, and one OUT) to 154 * exchange network traffic. Select an alternate interface setting to 155 * initialize the network aspects of the device and to enable the 156 * exchange of network traffic. 157 * </quote> 158 * 159 * Some devices, most notably cable modems, include interface settings 160 * that have no IN or OUT endpoint, therefore loop through the list of all 161 * available interface settings looking for one with both IN and OUT 162 * endpoints. 163 */ 164 id = usbd_get_interface_descriptor(un->un_iface); 165 cd = usbd_get_config_descriptor(un->un_udev); 166 numalts = usbd_get_no_alts(cd, id->bInterfaceNumber); 167 168 for (j = 0; j < numalts; j++) { 169 if (usbd_set_interface(un->un_iface, j)) { 170 aprint_error_dev(un->un_dev, 171 "setting alternate interface failed\n"); 172 return; 173 } 174 /* Find endpoints. */ 175 id = usbd_get_interface_descriptor(un->un_iface); 176 un->un_ed[USBNET_ENDPT_RX] = un->un_ed[USBNET_ENDPT_TX] = 0; 177 for (i = 0; i < id->bNumEndpoints; i++) { 178 ed = usbd_interface2endpoint_descriptor(un->un_iface, i); 179 if (!ed) { 180 aprint_error_dev(self, 181 "could not read endpoint descriptor\n"); 182 return; 183 } 184 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 185 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 186 un->un_ed[USBNET_ENDPT_RX] = ed->bEndpointAddress; 187 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && 188 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 189 un->un_ed[USBNET_ENDPT_TX] = ed->bEndpointAddress; 190 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 191 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) { 192 /* XXX: CDC spec defines an interrupt pipe, but it is not 193 * needed for simple host-to-host applications. */ 194 } else { 195 aprint_error_dev(self, "unexpected endpoint\n"); 196 } 197 } 198 /* If we found something, try and use it... */ 199 if (un->un_ed[USBNET_ENDPT_RX] != 0 && un->un_ed[USBNET_ENDPT_TX] != 0) 200 break; 201 } 202 203 if (un->un_ed[USBNET_ENDPT_RX] == 0) { 204 aprint_error_dev(self, "could not find data bulk in\n"); 205 return; 206 } 207 if (un->un_ed[USBNET_ENDPT_TX] == 0) { 208 aprint_error_dev(self, "could not find data bulk out\n"); 209 return; 210 } 211 212 ue = (const usb_cdc_ethernet_descriptor_t *)usb_find_desc_if(dev, 213 UDESC_CS_INTERFACE, UDESCSUB_CDC_ENF, 214 usbd_get_interface_descriptor(uiaa->uiaa_iface)); 215 if (!ue || usbd_get_string(dev, ue->iMacAddress, eaddr_str) || 216 ether_aton_r(un->un_eaddr, sizeof(un->un_eaddr), eaddr_str)) { 217 aprint_normal_dev(self, "faking address\n"); 218 un->un_eaddr[0] = 0x2a; 219 uint32_t ticks = getticks(); 220 memcpy(&un->un_eaddr[1], &ticks, sizeof(ticks)); 221 un->un_eaddr[5] = (uint8_t)(device_unit(un->un_dev)); 222 } 223 224 /* Query NTB transfers sizes */ 225 req.bmRequestType = UT_READ_CLASS_INTERFACE; 226 req.bRequest = NCM_GET_NTB_PARAMETERS; 227 USETW(req.wValue, 0); 228 USETW(req.wIndex, uiaa->uiaa_ifaceno); 229 USETW(req.wLength, sizeof(np)); 230 if (usbd_do_request(un->un_udev, &req, &np) != USBD_NORMAL_COMPLETION || 231 UGETW(np.wLength) != sizeof(np)) { 232 aprint_error_dev(un->un_dev, 233 "NCM_GET_NTB_PARAMETERS failed\n"); 234 return; 235 } 236 un->un_rx_list_cnt = 1; 237 un->un_tx_list_cnt = 1; 238 un->un_rx_bufsz = UGETDW(np.dwNtbInMaxSize); 239 un->un_tx_bufsz = UGETDW(np.dwNtbOutMaxSize); 240 if (un->un_tx_bufsz < NCM_MIN_TX_BUFSZ) { 241 aprint_error_dev(un->un_dev, "dwNtbOutMaxSize %u too small\n", 242 un->un_tx_bufsz); 243 return; 244 } 245 246 usbnet_attach(un); 247 usbnet_attach_ifp(un, IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST, 248 0, NULL); 249 250 /* XXX There is no link state, pretend we are always on */ 251 if_link_state_change(usbnet_ifp(un), LINK_STATE_UP); 252 } 253 254 static void 255 ncm_uno_rx_loop(struct usbnet *un, struct usbnet_chain *c, uint32_t total_len) 256 { 257 struct ifnet *ifp = usbnet_ifp(un); 258 uint8_t *buf = c->unc_buf; 259 struct ncm_header16 *hdr; 260 struct ncm_dptab16 *ptr; 261 unsigned i; 262 263 if (total_len < sizeof(*hdr) + sizeof(*ptr)) { 264 aprint_error_dev(un->un_dev, "got a too small usb message\n"); 265 if_statinc(ifp, if_ierrors); 266 return; 267 } 268 hdr = (struct ncm_header16 *)buf; 269 if (UGETDW(hdr->dwSignature) != NCM_HDR16_SIG) { 270 aprint_error_dev(un->un_dev, 271 "got a non NCM_HDR16_SIG header %08x\n", 272 UGETDW(hdr->dwSignature)); 273 return; 274 } 275 const unsigned ndp_index = UGETW(hdr->wNdpIndex); 276 277 if (ndp_index < sizeof(*hdr) || 278 ndp_index > total_len - sizeof(*ptr)) { 279 aprint_error_dev(un->un_dev, "ndp start offset %d " 280 "bigger than data sizeof(*hdr) %zu sizeof(*ptr) %zu " 281 "total_len %u\n", ndp_index, 282 sizeof(*hdr), sizeof(*ptr), 283 total_len); 284 if_statinc(ifp, if_ierrors); 285 return; 286 } 287 ptr = (struct ncm_dptab16 *)(buf + ndp_index); 288 if (UGETDW(ptr->dwSignature) != NCM_PTR16_SIG_NO_CRC) { 289 aprint_error_dev(un->un_dev, "ncm dptab16 signature %08x is " 290 "weird\n", UGETDW(ptr->dwSignature)); 291 if_statinc(ifp, if_ierrors); 292 return; 293 } 294 295 if ((unsigned)UGETW(ptr->wLength) > total_len - ndp_index) { 296 aprint_error_dev(un->un_dev, "dptab16 wlength %u goes off end " 297 "ndp_index %u total_len %u\n", UGETW(ptr->wLength), 298 ndp_index, total_len); 299 if_statinc(ifp, if_ierrors); 300 return; 301 } 302 303 const unsigned max_datagrams = (UGETW(ptr->wLength) - 304 offsetof(struct ncm_dptab16, datagram))/sizeof(ptr->datagram[0]); 305 for (i = 0; i < max_datagrams; i++) { 306 uint16_t data_start = UGETW(ptr->datagram[i].wDatagramIndex); 307 uint16_t data_len = UGETW(ptr->datagram[i].wDatagramLength); 308 309 if (data_start == 0 || data_len == 0) 310 break; 311 312 if (data_len > total_len || 313 data_start > total_len - data_len) { 314 aprint_error_dev(un->un_dev, 315 "datagram points out of buffer\n"); 316 if_statinc(ifp, if_ierrors); 317 return; 318 } 319 usbnet_enqueue(un, buf + data_start, data_len, 0, 0, 0); 320 } 321 } 322 323 static unsigned 324 ncm_uno_tx_prepare(struct usbnet *un, struct mbuf *m, struct usbnet_chain *c) 325 { 326 struct ncm_softc *sc = un->un_sc; 327 struct ncm_dptab16 *ptr; 328 struct ncm_header16 *hdr; 329 unsigned hdr_len, len; 330 331 hdr_len = sizeof(*hdr) + sizeof(*ptr); 332 len = m->m_pkthdr.len; 333 KASSERT(hdr_len <= un->un_tx_bufsz); 334 KASSERT(len <= un->un_tx_bufsz - hdr_len); 335 336 hdr = (struct ncm_header16 *)c->unc_buf; 337 ptr = (struct ncm_dptab16 *)(hdr + 1); 338 memset(hdr, 0, sizeof(*hdr)); 339 memset(ptr, 0, sizeof(*ptr)); 340 341 USETDW(hdr->dwSignature, NCM_HDR16_SIG); 342 USETW(hdr->wHeaderLength, sizeof(*hdr)); 343 USETW(hdr->wSequence, sc->sc_tx_seq); 344 sc->sc_tx_seq++; 345 346 USETW(hdr->wBlockLength, hdr_len + len); 347 USETW(hdr->wNdpIndex, sizeof(*hdr)); 348 349 USETDW(ptr->dwSignature, NCM_PTR16_SIG_NO_CRC); 350 USETW(ptr->wLength, sizeof(*ptr)); 351 USETW(ptr->wNextNdpIndex, 0); 352 353 USETW(ptr->datagram[0].wDatagramIndex, hdr_len); 354 USETW(ptr->datagram[0].wDatagramLength, len); 355 356 USETW(ptr->datagram[1].wDatagramIndex, 0); 357 USETW(ptr->datagram[1].wDatagramLength, 0); 358 359 m_copydata(m, 0, len, c->unc_buf + hdr_len); 360 361 return len + hdr_len; 362 } 363 364 #ifdef _MODULE 365 #include "ioconf.c" 366 #endif 367 368 USBNET_MODULE(ncm) 369