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