1 /* $NetBSD: if_cdce.c,v 1.4 2004/10/24 12:50:54 augustss Exp $ */ 2 3 /*- 4 * Copyright (c) 1997, 1998, 1999, 2000-2003 Bill Paul <wpaul@windriver.com> 5 * Copyright (c) 2003-2005 Craig Boston 6 * Copyright (c) 2004 Daniel Hartmeier 7 * Copyright (c) 2009 Hans Petter Selasky 8 * All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by Bill Paul. 21 * 4. Neither the name of the author nor the names of any co-contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul, THE VOICES IN HIS HEAD OR 29 * THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 30 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 31 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 32 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 33 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 34 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 36 */ 37 38 /* 39 * USB Communication Device Class (Ethernet Networking Control Model) 40 * http://www.usb.org/developers/devclass_docs/usbcdc11.pdf 41 */ 42 43 /* 44 * USB Network Control Model (NCM) 45 * http://www.usb.org/developers/devclass_docs/NCM10.zip 46 */ 47 48 #include <sys/cdefs.h> 49 __FBSDID("$FreeBSD$"); 50 51 #include <sys/stdint.h> 52 #include <sys/stddef.h> 53 #include <sys/param.h> 54 #include <sys/queue.h> 55 #include <sys/types.h> 56 #include <sys/systm.h> 57 #include <sys/kernel.h> 58 #include <sys/bus.h> 59 #include <sys/module.h> 60 #include <sys/lock.h> 61 #include <sys/mutex.h> 62 #include <sys/condvar.h> 63 #include <sys/sysctl.h> 64 #include <sys/sx.h> 65 #include <sys/unistd.h> 66 #include <sys/callout.h> 67 #include <sys/malloc.h> 68 #include <sys/priv.h> 69 70 #include <dev/usb/usb.h> 71 #include <dev/usb/usbdi.h> 72 #include <dev/usb/usbdi_util.h> 73 #include <dev/usb/usb_cdc.h> 74 #include "usbdevs.h" 75 76 #define USB_DEBUG_VAR cdce_debug 77 #include <dev/usb/usb_debug.h> 78 #include <dev/usb/usb_process.h> 79 #include "usb_if.h" 80 81 #include <dev/usb/net/usb_ethernet.h> 82 #include <dev/usb/net/if_cdcereg.h> 83 84 static device_probe_t cdce_probe; 85 static device_attach_t cdce_attach; 86 static device_detach_t cdce_detach; 87 static device_suspend_t cdce_suspend; 88 static device_resume_t cdce_resume; 89 static usb_handle_request_t cdce_handle_request; 90 91 static usb_callback_t cdce_bulk_write_callback; 92 static usb_callback_t cdce_bulk_read_callback; 93 static usb_callback_t cdce_intr_read_callback; 94 static usb_callback_t cdce_intr_write_callback; 95 96 #if CDCE_HAVE_NCM 97 static usb_callback_t cdce_ncm_bulk_write_callback; 98 static usb_callback_t cdce_ncm_bulk_read_callback; 99 #endif 100 101 static uether_fn_t cdce_attach_post; 102 static uether_fn_t cdce_init; 103 static uether_fn_t cdce_stop; 104 static uether_fn_t cdce_start; 105 static uether_fn_t cdce_setmulti; 106 static uether_fn_t cdce_setpromisc; 107 108 static uint32_t cdce_m_crc32(struct mbuf *, uint32_t, uint32_t); 109 110 #ifdef USB_DEBUG 111 static int cdce_debug = 0; 112 static int cdce_tx_interval = 0; 113 114 SYSCTL_NODE(_hw_usb, OID_AUTO, cdce, CTLFLAG_RW, 0, "USB CDC-Ethernet"); 115 SYSCTL_INT(_hw_usb_cdce, OID_AUTO, debug, CTLFLAG_RW, &cdce_debug, 0, 116 "Debug level"); 117 SYSCTL_INT(_hw_usb_cdce, OID_AUTO, interval, CTLFLAG_RW, &cdce_tx_interval, 0, 118 "NCM transmit interval in ms"); 119 #endif 120 121 static const struct usb_config cdce_config[CDCE_N_TRANSFER] = { 122 123 [CDCE_BULK_RX] = { 124 .type = UE_BULK, 125 .endpoint = UE_ADDR_ANY, 126 .direction = UE_DIR_RX, 127 .if_index = 0, 128 .frames = CDCE_FRAMES_MAX, 129 .bufsize = (CDCE_FRAMES_MAX * MCLBYTES), 130 .flags = {.pipe_bof = 1,.short_frames_ok = 1,.short_xfer_ok = 1,.ext_buffer = 1,}, 131 .callback = cdce_bulk_read_callback, 132 .timeout = 0, /* no timeout */ 133 .usb_mode = USB_MODE_DUAL, /* both modes */ 134 }, 135 136 [CDCE_BULK_TX] = { 137 .type = UE_BULK, 138 .endpoint = UE_ADDR_ANY, 139 .direction = UE_DIR_TX, 140 .if_index = 0, 141 .frames = CDCE_FRAMES_MAX, 142 .bufsize = (CDCE_FRAMES_MAX * MCLBYTES), 143 .flags = {.pipe_bof = 1,.force_short_xfer = 1,.ext_buffer = 1,}, 144 .callback = cdce_bulk_write_callback, 145 .timeout = 10000, /* 10 seconds */ 146 .usb_mode = USB_MODE_DUAL, /* both modes */ 147 }, 148 149 [CDCE_INTR_RX] = { 150 .type = UE_INTERRUPT, 151 .endpoint = UE_ADDR_ANY, 152 .direction = UE_DIR_RX, 153 .if_index = 1, 154 .bufsize = CDCE_IND_SIZE_MAX, 155 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,.no_pipe_ok = 1,}, 156 .callback = cdce_intr_read_callback, 157 .timeout = 0, 158 .usb_mode = USB_MODE_HOST, 159 }, 160 161 [CDCE_INTR_TX] = { 162 .type = UE_INTERRUPT, 163 .endpoint = UE_ADDR_ANY, 164 .direction = UE_DIR_TX, 165 .if_index = 1, 166 .bufsize = CDCE_IND_SIZE_MAX, 167 .flags = {.pipe_bof = 1,.force_short_xfer = 1,.no_pipe_ok = 1,}, 168 .callback = cdce_intr_write_callback, 169 .timeout = 10000, /* 10 seconds */ 170 .usb_mode = USB_MODE_DEVICE, 171 }, 172 }; 173 174 #if CDCE_HAVE_NCM 175 static const struct usb_config cdce_ncm_config[CDCE_N_TRANSFER] = { 176 177 [CDCE_BULK_RX] = { 178 .type = UE_BULK, 179 .endpoint = UE_ADDR_ANY, 180 .direction = UE_DIR_RX, 181 .if_index = 0, 182 .frames = CDCE_NCM_RX_FRAMES_MAX, 183 .bufsize = (CDCE_NCM_RX_FRAMES_MAX * CDCE_NCM_RX_MAXLEN), 184 .flags = {.pipe_bof = 1,.short_frames_ok = 1,.short_xfer_ok = 1,}, 185 .callback = cdce_ncm_bulk_read_callback, 186 .timeout = 0, /* no timeout */ 187 .usb_mode = USB_MODE_DUAL, /* both modes */ 188 }, 189 190 [CDCE_BULK_TX] = { 191 .type = UE_BULK, 192 .endpoint = UE_ADDR_ANY, 193 .direction = UE_DIR_TX, 194 .if_index = 0, 195 .frames = CDCE_NCM_TX_FRAMES_MAX, 196 .bufsize = (CDCE_NCM_TX_FRAMES_MAX * CDCE_NCM_TX_MAXLEN), 197 .flags = {.pipe_bof = 1,}, 198 .callback = cdce_ncm_bulk_write_callback, 199 .timeout = 10000, /* 10 seconds */ 200 .usb_mode = USB_MODE_DUAL, /* both modes */ 201 }, 202 203 [CDCE_INTR_RX] = { 204 .type = UE_INTERRUPT, 205 .endpoint = UE_ADDR_ANY, 206 .direction = UE_DIR_RX, 207 .if_index = 1, 208 .bufsize = CDCE_IND_SIZE_MAX, 209 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,.no_pipe_ok = 1,}, 210 .callback = cdce_intr_read_callback, 211 .timeout = 0, 212 .usb_mode = USB_MODE_HOST, 213 }, 214 215 [CDCE_INTR_TX] = { 216 .type = UE_INTERRUPT, 217 .endpoint = UE_ADDR_ANY, 218 .direction = UE_DIR_TX, 219 .if_index = 1, 220 .bufsize = CDCE_IND_SIZE_MAX, 221 .flags = {.pipe_bof = 1,.force_short_xfer = 1,.no_pipe_ok = 1,}, 222 .callback = cdce_intr_write_callback, 223 .timeout = 10000, /* 10 seconds */ 224 .usb_mode = USB_MODE_DEVICE, 225 }, 226 }; 227 #endif 228 229 static device_method_t cdce_methods[] = { 230 /* USB interface */ 231 DEVMETHOD(usb_handle_request, cdce_handle_request), 232 233 /* Device interface */ 234 DEVMETHOD(device_probe, cdce_probe), 235 DEVMETHOD(device_attach, cdce_attach), 236 DEVMETHOD(device_detach, cdce_detach), 237 DEVMETHOD(device_suspend, cdce_suspend), 238 DEVMETHOD(device_resume, cdce_resume), 239 240 {0, 0} 241 }; 242 243 static driver_t cdce_driver = { 244 .name = "cdce", 245 .methods = cdce_methods, 246 .size = sizeof(struct cdce_softc), 247 }; 248 249 static devclass_t cdce_devclass; 250 251 DRIVER_MODULE(cdce, uhub, cdce_driver, cdce_devclass, NULL, 0); 252 MODULE_VERSION(cdce, 1); 253 MODULE_DEPEND(cdce, uether, 1, 1, 1); 254 MODULE_DEPEND(cdce, usb, 1, 1, 1); 255 MODULE_DEPEND(cdce, ether, 1, 1, 1); 256 257 static const struct usb_ether_methods cdce_ue_methods = { 258 .ue_attach_post = cdce_attach_post, 259 .ue_start = cdce_start, 260 .ue_init = cdce_init, 261 .ue_stop = cdce_stop, 262 .ue_setmulti = cdce_setmulti, 263 .ue_setpromisc = cdce_setpromisc, 264 }; 265 266 static const struct usb_device_id cdce_devs[] = { 267 {USB_VPI(USB_VENDOR_ACERLABS, USB_PRODUCT_ACERLABS_M5632, CDCE_FLAG_NO_UNION)}, 268 {USB_VPI(USB_VENDOR_AMBIT, USB_PRODUCT_AMBIT_NTL_250, CDCE_FLAG_NO_UNION)}, 269 {USB_VPI(USB_VENDOR_COMPAQ, USB_PRODUCT_COMPAQ_IPAQLINUX, CDCE_FLAG_NO_UNION)}, 270 {USB_VPI(USB_VENDOR_GMATE, USB_PRODUCT_GMATE_YP3X00, CDCE_FLAG_NO_UNION)}, 271 {USB_VPI(USB_VENDOR_MOTOROLA2, USB_PRODUCT_MOTOROLA2_USBLAN, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)}, 272 {USB_VPI(USB_VENDOR_MOTOROLA2, USB_PRODUCT_MOTOROLA2_USBLAN2, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)}, 273 {USB_VPI(USB_VENDOR_NETCHIP, USB_PRODUCT_NETCHIP_ETHERNETGADGET, CDCE_FLAG_NO_UNION)}, 274 {USB_VPI(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2501, CDCE_FLAG_NO_UNION)}, 275 {USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SL5500, CDCE_FLAG_ZAURUS)}, 276 {USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SL5600, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)}, 277 {USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SLA300, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)}, 278 {USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SLC700, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)}, 279 {USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SLC750, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)}, 280 281 {USB_IF_CSI(UICLASS_CDC, UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL, 0)}, 282 {USB_IF_CSI(UICLASS_CDC, UISUBCLASS_MOBILE_DIRECT_LINE_MODEL, 0)}, 283 {USB_IF_CSI(UICLASS_CDC, UISUBCLASS_NETWORK_CONTROL_MODEL, 0)}, 284 }; 285 286 #if CDCE_HAVE_NCM 287 /*------------------------------------------------------------------------* 288 * cdce_ncm_init 289 * 290 * Return values: 291 * 0: Success 292 * Else: Failure 293 *------------------------------------------------------------------------*/ 294 static uint8_t 295 cdce_ncm_init(struct cdce_softc *sc) 296 { 297 struct usb_ncm_parameters temp; 298 struct usb_device_request req; 299 struct usb_ncm_func_descriptor *ufd; 300 uint8_t value[8]; 301 int err; 302 303 ufd = usbd_find_descriptor(sc->sc_ue.ue_udev, NULL, 304 sc->sc_ifaces_index[1], UDESC_CS_INTERFACE, 0 - 1, 305 UCDC_NCM_FUNC_DESC_SUBTYPE, 0 - 1); 306 307 /* verify length of NCM functional descriptor */ 308 if (ufd != NULL) { 309 if (ufd->bLength < sizeof(*ufd)) 310 ufd = NULL; 311 else 312 DPRINTFN(1, "Found NCM functional descriptor.\n"); 313 } 314 315 req.bmRequestType = UT_READ_CLASS_INTERFACE; 316 req.bRequest = UCDC_NCM_GET_NTB_PARAMETERS; 317 USETW(req.wValue, 0); 318 req.wIndex[0] = sc->sc_ifaces_index[1]; 319 req.wIndex[1] = 0; 320 USETW(req.wLength, sizeof(temp)); 321 322 err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req, 323 &temp, 0, NULL, 1000 /* ms */); 324 if (err) 325 return (1); 326 327 /* Read correct set of parameters according to device mode */ 328 329 if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST) { 330 sc->sc_ncm.rx_max = UGETDW(temp.dwNtbInMaxSize); 331 sc->sc_ncm.tx_max = UGETDW(temp.dwNtbOutMaxSize); 332 sc->sc_ncm.tx_remainder = UGETW(temp.wNdpOutPayloadRemainder); 333 sc->sc_ncm.tx_modulus = UGETW(temp.wNdpOutDivisor); 334 sc->sc_ncm.tx_struct_align = UGETW(temp.wNdpOutAlignment); 335 sc->sc_ncm.tx_nframe = UGETW(temp.wNtbOutMaxDatagrams); 336 } else { 337 sc->sc_ncm.rx_max = UGETDW(temp.dwNtbOutMaxSize); 338 sc->sc_ncm.tx_max = UGETDW(temp.dwNtbInMaxSize); 339 sc->sc_ncm.tx_remainder = UGETW(temp.wNdpInPayloadRemainder); 340 sc->sc_ncm.tx_modulus = UGETW(temp.wNdpInDivisor); 341 sc->sc_ncm.tx_struct_align = UGETW(temp.wNdpInAlignment); 342 sc->sc_ncm.tx_nframe = UGETW(temp.wNtbOutMaxDatagrams); 343 } 344 345 /* Verify maximum receive length */ 346 347 if ((sc->sc_ncm.rx_max < 32) || 348 (sc->sc_ncm.rx_max > CDCE_NCM_RX_MAXLEN)) { 349 DPRINTFN(1, "Using default maximum receive length\n"); 350 sc->sc_ncm.rx_max = CDCE_NCM_RX_MAXLEN; 351 } 352 353 /* Verify maximum transmit length */ 354 355 if ((sc->sc_ncm.tx_max < 32) || 356 (sc->sc_ncm.tx_max > CDCE_NCM_TX_MAXLEN)) { 357 DPRINTFN(1, "Using default maximum transmit length\n"); 358 sc->sc_ncm.tx_max = CDCE_NCM_TX_MAXLEN; 359 } 360 361 /* 362 * Verify that the structure alignment is: 363 * - power of two 364 * - not greater than the maximum transmit length 365 * - not less than four bytes 366 */ 367 if ((sc->sc_ncm.tx_struct_align < 4) || 368 (sc->sc_ncm.tx_struct_align != 369 ((-sc->sc_ncm.tx_struct_align) & sc->sc_ncm.tx_struct_align)) || 370 (sc->sc_ncm.tx_struct_align >= sc->sc_ncm.tx_max)) { 371 DPRINTFN(1, "Using default other alignment: 4 bytes\n"); 372 sc->sc_ncm.tx_struct_align = 4; 373 } 374 375 /* 376 * Verify that the payload alignment is: 377 * - power of two 378 * - not greater than the maximum transmit length 379 * - not less than four bytes 380 */ 381 if ((sc->sc_ncm.tx_modulus < 4) || 382 (sc->sc_ncm.tx_modulus != 383 ((-sc->sc_ncm.tx_modulus) & sc->sc_ncm.tx_modulus)) || 384 (sc->sc_ncm.tx_modulus >= sc->sc_ncm.tx_max)) { 385 DPRINTFN(1, "Using default transmit modulus: 4 bytes\n"); 386 sc->sc_ncm.tx_modulus = 4; 387 } 388 389 /* Verify that the payload remainder */ 390 391 if ((sc->sc_ncm.tx_remainder >= sc->sc_ncm.tx_modulus)) { 392 DPRINTFN(1, "Using default transmit remainder: 0 bytes\n"); 393 sc->sc_ncm.tx_remainder = 0; 394 } 395 396 /* 397 * Offset the TX remainder so that IP packet payload starts at 398 * the tx_modulus. This is not too clear in the specification. 399 */ 400 401 sc->sc_ncm.tx_remainder = 402 (sc->sc_ncm.tx_remainder - ETHER_HDR_LEN) & 403 (sc->sc_ncm.tx_modulus - 1); 404 405 /* Verify max datagrams */ 406 407 if (sc->sc_ncm.tx_nframe == 0 || 408 sc->sc_ncm.tx_nframe > (CDCE_NCM_SUBFRAMES_MAX - 1)) { 409 DPRINTFN(1, "Using default max " 410 "subframes: %u units\n", CDCE_NCM_SUBFRAMES_MAX - 1); 411 /* need to reserve one entry for zero padding */ 412 sc->sc_ncm.tx_nframe = (CDCE_NCM_SUBFRAMES_MAX - 1); 413 } 414 415 /* Additional configuration, will fail in device side mode, which is OK. */ 416 417 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 418 req.bRequest = UCDC_NCM_SET_NTB_INPUT_SIZE; 419 USETW(req.wValue, 0); 420 req.wIndex[0] = sc->sc_ifaces_index[1]; 421 req.wIndex[1] = 0; 422 423 if (ufd != NULL && 424 (ufd->bmNetworkCapabilities & UCDC_NCM_CAP_MAX_DGRAM)) { 425 USETW(req.wLength, 8); 426 USETDW(value, sc->sc_ncm.rx_max); 427 USETW(value + 4, (CDCE_NCM_SUBFRAMES_MAX - 1)); 428 USETW(value + 6, 0); 429 } else { 430 USETW(req.wLength, 4); 431 USETDW(value, sc->sc_ncm.rx_max); 432 } 433 434 err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req, 435 &value, 0, NULL, 1000 /* ms */); 436 if (err) { 437 DPRINTFN(1, "Setting input size " 438 "to %u failed.\n", sc->sc_ncm.rx_max); 439 } 440 441 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 442 req.bRequest = UCDC_NCM_SET_CRC_MODE; 443 USETW(req.wValue, 0); /* no CRC */ 444 req.wIndex[0] = sc->sc_ifaces_index[1]; 445 req.wIndex[1] = 0; 446 USETW(req.wLength, 0); 447 448 err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req, 449 NULL, 0, NULL, 1000 /* ms */); 450 if (err) { 451 DPRINTFN(1, "Setting CRC mode to off failed.\n"); 452 } 453 454 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 455 req.bRequest = UCDC_NCM_SET_NTB_FORMAT; 456 USETW(req.wValue, 0); /* NTB-16 */ 457 req.wIndex[0] = sc->sc_ifaces_index[1]; 458 req.wIndex[1] = 0; 459 USETW(req.wLength, 0); 460 461 err = usbd_do_request_flags(sc->sc_ue.ue_udev, NULL, &req, 462 NULL, 0, NULL, 1000 /* ms */); 463 if (err) { 464 DPRINTFN(1, "Setting NTB format to 16-bit failed.\n"); 465 } 466 467 return (0); /* success */ 468 } 469 #endif 470 471 static int 472 cdce_probe(device_t dev) 473 { 474 struct usb_attach_arg *uaa = device_get_ivars(dev); 475 476 return (usbd_lookup_id_by_uaa(cdce_devs, sizeof(cdce_devs), uaa)); 477 } 478 479 static void 480 cdce_attach_post(struct usb_ether *ue) 481 { 482 /* no-op */ 483 return; 484 } 485 486 static int 487 cdce_attach(device_t dev) 488 { 489 struct cdce_softc *sc = device_get_softc(dev); 490 struct usb_ether *ue = &sc->sc_ue; 491 struct usb_attach_arg *uaa = device_get_ivars(dev); 492 struct usb_interface *iface; 493 const struct usb_cdc_union_descriptor *ud; 494 const struct usb_interface_descriptor *id; 495 const struct usb_cdc_ethernet_descriptor *ued; 496 const struct usb_config *pcfg; 497 int error; 498 uint8_t i; 499 uint8_t data_iface_no; 500 char eaddr_str[5 * ETHER_ADDR_LEN]; /* approx */ 501 502 sc->sc_flags = USB_GET_DRIVER_INFO(uaa); 503 sc->sc_ue.ue_udev = uaa->device; 504 505 device_set_usb_desc(dev); 506 507 mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF); 508 509 ud = usbd_find_descriptor 510 (uaa->device, NULL, uaa->info.bIfaceIndex, 511 UDESC_CS_INTERFACE, 0 - 1, UDESCSUB_CDC_UNION, 0 - 1); 512 513 if ((ud == NULL) || (ud->bLength < sizeof(*ud)) || 514 (sc->sc_flags & CDCE_FLAG_NO_UNION)) { 515 DPRINTFN(1, "No union descriptor!\n"); 516 sc->sc_ifaces_index[0] = uaa->info.bIfaceIndex; 517 sc->sc_ifaces_index[1] = uaa->info.bIfaceIndex; 518 goto alloc_transfers; 519 } 520 data_iface_no = ud->bSlaveInterface[0]; 521 522 for (i = 0;; i++) { 523 524 iface = usbd_get_iface(uaa->device, i); 525 526 if (iface) { 527 528 id = usbd_get_interface_descriptor(iface); 529 530 if (id && (id->bInterfaceNumber == data_iface_no)) { 531 sc->sc_ifaces_index[0] = i; 532 sc->sc_ifaces_index[1] = uaa->info.bIfaceIndex; 533 usbd_set_parent_iface(uaa->device, i, uaa->info.bIfaceIndex); 534 break; 535 } 536 } else { 537 device_printf(dev, "no data interface found\n"); 538 goto detach; 539 } 540 } 541 542 /* 543 * <quote> 544 * 545 * The Data Class interface of a networking device shall have 546 * a minimum of two interface settings. The first setting 547 * (the default interface setting) includes no endpoints and 548 * therefore no networking traffic is exchanged whenever the 549 * default interface setting is selected. One or more 550 * additional interface settings are used for normal 551 * operation, and therefore each includes a pair of endpoints 552 * (one IN, and one OUT) to exchange network traffic. Select 553 * an alternate interface setting to initialize the network 554 * aspects of the device and to enable the exchange of 555 * network traffic. 556 * 557 * </quote> 558 * 559 * Some devices, most notably cable modems, include interface 560 * settings that have no IN or OUT endpoint, therefore loop 561 * through the list of all available interface settings 562 * looking for one with both IN and OUT endpoints. 563 */ 564 565 alloc_transfers: 566 567 pcfg = cdce_config; /* Default Configuration */ 568 569 for (i = 0; i != 32; i++) { 570 571 error = usbd_set_alt_interface_index(uaa->device, 572 sc->sc_ifaces_index[0], i); 573 if (error) 574 break; 575 #if CDCE_HAVE_NCM 576 if ((i == 0) && (cdce_ncm_init(sc) == 0)) 577 pcfg = cdce_ncm_config; 578 #endif 579 error = usbd_transfer_setup(uaa->device, 580 sc->sc_ifaces_index, sc->sc_xfer, 581 pcfg, CDCE_N_TRANSFER, sc, &sc->sc_mtx); 582 583 if (error == 0) 584 break; 585 } 586 587 if (error || (i == 32)) { 588 device_printf(dev, "No valid alternate " 589 "setting found\n"); 590 goto detach; 591 } 592 593 ued = usbd_find_descriptor 594 (uaa->device, NULL, uaa->info.bIfaceIndex, 595 UDESC_CS_INTERFACE, 0 - 1, UDESCSUB_CDC_ENF, 0 - 1); 596 597 if ((ued == NULL) || (ued->bLength < sizeof(*ued))) { 598 error = USB_ERR_INVAL; 599 } else { 600 error = usbd_req_get_string_any(uaa->device, NULL, 601 eaddr_str, sizeof(eaddr_str), ued->iMacAddress); 602 } 603 604 if (error) { 605 606 /* fake MAC address */ 607 608 device_printf(dev, "faking MAC address\n"); 609 sc->sc_ue.ue_eaddr[0] = 0x2a; 610 memcpy(&sc->sc_ue.ue_eaddr[1], &ticks, sizeof(uint32_t)); 611 sc->sc_ue.ue_eaddr[5] = device_get_unit(dev); 612 613 } else { 614 615 memset(sc->sc_ue.ue_eaddr, 0, sizeof(sc->sc_ue.ue_eaddr)); 616 617 for (i = 0; i != (ETHER_ADDR_LEN * 2); i++) { 618 619 char c = eaddr_str[i]; 620 621 if ('0' <= c && c <= '9') 622 c -= '0'; 623 else if (c != 0) 624 c -= 'A' - 10; 625 else 626 break; 627 628 c &= 0xf; 629 630 if ((i & 1) == 0) 631 c <<= 4; 632 sc->sc_ue.ue_eaddr[i / 2] |= c; 633 } 634 635 if (uaa->usb_mode == USB_MODE_DEVICE) { 636 /* 637 * Do not use the same MAC address like the peer ! 638 */ 639 sc->sc_ue.ue_eaddr[5] ^= 0xFF; 640 } 641 } 642 643 ue->ue_sc = sc; 644 ue->ue_dev = dev; 645 ue->ue_udev = uaa->device; 646 ue->ue_mtx = &sc->sc_mtx; 647 ue->ue_methods = &cdce_ue_methods; 648 649 error = uether_ifattach(ue); 650 if (error) { 651 device_printf(dev, "could not attach interface\n"); 652 goto detach; 653 } 654 return (0); /* success */ 655 656 detach: 657 cdce_detach(dev); 658 return (ENXIO); /* failure */ 659 } 660 661 static int 662 cdce_detach(device_t dev) 663 { 664 struct cdce_softc *sc = device_get_softc(dev); 665 struct usb_ether *ue = &sc->sc_ue; 666 667 /* stop all USB transfers first */ 668 usbd_transfer_unsetup(sc->sc_xfer, CDCE_N_TRANSFER); 669 uether_ifdetach(ue); 670 mtx_destroy(&sc->sc_mtx); 671 672 return (0); 673 } 674 675 static void 676 cdce_start(struct usb_ether *ue) 677 { 678 struct cdce_softc *sc = uether_getsc(ue); 679 680 /* 681 * Start the USB transfers, if not already started: 682 */ 683 usbd_transfer_start(sc->sc_xfer[CDCE_BULK_TX]); 684 usbd_transfer_start(sc->sc_xfer[CDCE_BULK_RX]); 685 } 686 687 static void 688 cdce_free_queue(struct mbuf **ppm, uint8_t n) 689 { 690 uint8_t x; 691 for (x = 0; x != n; x++) { 692 if (ppm[x] != NULL) { 693 m_freem(ppm[x]); 694 ppm[x] = NULL; 695 } 696 } 697 } 698 699 static void 700 cdce_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error) 701 { 702 struct cdce_softc *sc = usbd_xfer_softc(xfer); 703 struct ifnet *ifp = uether_getifp(&sc->sc_ue); 704 struct mbuf *m; 705 struct mbuf *mt; 706 uint32_t crc; 707 uint8_t x; 708 int actlen, aframes; 709 710 usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL); 711 712 DPRINTFN(1, "\n"); 713 714 switch (USB_GET_STATE(xfer)) { 715 case USB_ST_TRANSFERRED: 716 DPRINTFN(11, "transfer complete: %u bytes in %u frames\n", 717 actlen, aframes); 718 719 ifp->if_opackets++; 720 721 /* free all previous TX buffers */ 722 cdce_free_queue(sc->sc_tx_buf, CDCE_FRAMES_MAX); 723 724 /* FALLTHROUGH */ 725 case USB_ST_SETUP: 726 tr_setup: 727 for (x = 0; x != CDCE_FRAMES_MAX; x++) { 728 729 IFQ_DRV_DEQUEUE(&ifp->if_snd, m); 730 731 if (m == NULL) 732 break; 733 734 if (sc->sc_flags & CDCE_FLAG_ZAURUS) { 735 /* 736 * Zaurus wants a 32-bit CRC appended 737 * to every frame 738 */ 739 740 crc = cdce_m_crc32(m, 0, m->m_pkthdr.len); 741 crc = htole32(crc); 742 743 if (!m_append(m, 4, (void *)&crc)) { 744 m_freem(m); 745 ifp->if_oerrors++; 746 continue; 747 } 748 } 749 if (m->m_len != m->m_pkthdr.len) { 750 mt = m_defrag(m, M_DONTWAIT); 751 if (mt == NULL) { 752 m_freem(m); 753 ifp->if_oerrors++; 754 continue; 755 } 756 m = mt; 757 } 758 if (m->m_pkthdr.len > MCLBYTES) { 759 m->m_pkthdr.len = MCLBYTES; 760 } 761 sc->sc_tx_buf[x] = m; 762 usbd_xfer_set_frame_data(xfer, x, m->m_data, m->m_len); 763 764 /* 765 * If there's a BPF listener, bounce a copy of 766 * this frame to him: 767 */ 768 BPF_MTAP(ifp, m); 769 } 770 if (x != 0) { 771 usbd_xfer_set_frames(xfer, x); 772 773 usbd_transfer_submit(xfer); 774 } 775 break; 776 777 default: /* Error */ 778 DPRINTFN(11, "transfer error, %s\n", 779 usbd_errstr(error)); 780 781 /* free all previous TX buffers */ 782 cdce_free_queue(sc->sc_tx_buf, CDCE_FRAMES_MAX); 783 784 /* count output errors */ 785 ifp->if_oerrors++; 786 787 if (error != USB_ERR_CANCELLED) { 788 /* try to clear stall first */ 789 usbd_xfer_set_stall(xfer); 790 goto tr_setup; 791 } 792 break; 793 } 794 } 795 796 static int32_t 797 cdce_m_crc32_cb(void *arg, void *src, uint32_t count) 798 { 799 uint32_t *p_crc = arg; 800 801 *p_crc = crc32_raw(src, count, *p_crc); 802 return (0); 803 } 804 805 static uint32_t 806 cdce_m_crc32(struct mbuf *m, uint32_t src_offset, uint32_t src_len) 807 { 808 uint32_t crc = 0xFFFFFFFF; 809 int error; 810 811 error = m_apply(m, src_offset, src_len, cdce_m_crc32_cb, &crc); 812 return (crc ^ 0xFFFFFFFF); 813 } 814 815 static void 816 cdce_init(struct usb_ether *ue) 817 { 818 struct cdce_softc *sc = uether_getsc(ue); 819 struct ifnet *ifp = uether_getifp(ue); 820 821 CDCE_LOCK_ASSERT(sc, MA_OWNED); 822 823 ifp->if_drv_flags |= IFF_DRV_RUNNING; 824 825 /* start interrupt transfer */ 826 usbd_transfer_start(sc->sc_xfer[CDCE_INTR_RX]); 827 usbd_transfer_start(sc->sc_xfer[CDCE_INTR_TX]); 828 829 /* stall data write direction, which depends on USB mode */ 830 usbd_xfer_set_stall(sc->sc_xfer[CDCE_BULK_TX]); 831 832 /* start data transfers */ 833 cdce_start(ue); 834 } 835 836 static void 837 cdce_stop(struct usb_ether *ue) 838 { 839 struct cdce_softc *sc = uether_getsc(ue); 840 struct ifnet *ifp = uether_getifp(ue); 841 842 CDCE_LOCK_ASSERT(sc, MA_OWNED); 843 844 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 845 846 /* 847 * stop all the transfers, if not already stopped: 848 */ 849 usbd_transfer_stop(sc->sc_xfer[CDCE_BULK_RX]); 850 usbd_transfer_stop(sc->sc_xfer[CDCE_BULK_TX]); 851 usbd_transfer_stop(sc->sc_xfer[CDCE_INTR_RX]); 852 usbd_transfer_stop(sc->sc_xfer[CDCE_INTR_TX]); 853 } 854 855 static void 856 cdce_setmulti(struct usb_ether *ue) 857 { 858 /* no-op */ 859 return; 860 } 861 862 static void 863 cdce_setpromisc(struct usb_ether *ue) 864 { 865 /* no-op */ 866 return; 867 } 868 869 static int 870 cdce_suspend(device_t dev) 871 { 872 device_printf(dev, "Suspending\n"); 873 return (0); 874 } 875 876 static int 877 cdce_resume(device_t dev) 878 { 879 device_printf(dev, "Resuming\n"); 880 return (0); 881 } 882 883 static void 884 cdce_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error) 885 { 886 struct cdce_softc *sc = usbd_xfer_softc(xfer); 887 struct mbuf *m; 888 uint8_t x; 889 int actlen, aframes, len; 890 891 usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL); 892 893 switch (USB_GET_STATE(xfer)) { 894 case USB_ST_TRANSFERRED: 895 896 DPRINTF("received %u bytes in %u frames\n", actlen, aframes); 897 898 for (x = 0; x != aframes; x++) { 899 900 m = sc->sc_rx_buf[x]; 901 sc->sc_rx_buf[x] = NULL; 902 len = usbd_xfer_frame_len(xfer, x); 903 904 /* Strip off CRC added by Zaurus, if any */ 905 if ((sc->sc_flags & CDCE_FLAG_ZAURUS) && len >= 14) 906 len -= 4; 907 908 if (len < sizeof(struct ether_header)) { 909 m_freem(m); 910 continue; 911 } 912 /* queue up mbuf */ 913 uether_rxmbuf(&sc->sc_ue, m, len); 914 } 915 916 /* FALLTHROUGH */ 917 case USB_ST_SETUP: 918 /* 919 * TODO: Implement support for multi frame transfers, 920 * when the USB hardware supports it. 921 */ 922 for (x = 0; x != 1; x++) { 923 if (sc->sc_rx_buf[x] == NULL) { 924 m = uether_newbuf(); 925 if (m == NULL) 926 goto tr_stall; 927 sc->sc_rx_buf[x] = m; 928 } else { 929 m = sc->sc_rx_buf[x]; 930 } 931 932 usbd_xfer_set_frame_data(xfer, x, m->m_data, m->m_len); 933 } 934 /* set number of frames and start hardware */ 935 usbd_xfer_set_frames(xfer, x); 936 usbd_transfer_submit(xfer); 937 /* flush any received frames */ 938 uether_rxflush(&sc->sc_ue); 939 break; 940 941 default: /* Error */ 942 DPRINTF("error = %s\n", 943 usbd_errstr(error)); 944 945 if (error != USB_ERR_CANCELLED) { 946 tr_stall: 947 /* try to clear stall first */ 948 usbd_xfer_set_stall(xfer); 949 usbd_xfer_set_frames(xfer, 0); 950 usbd_transfer_submit(xfer); 951 break; 952 } 953 954 /* need to free the RX-mbufs when we are cancelled */ 955 cdce_free_queue(sc->sc_rx_buf, CDCE_FRAMES_MAX); 956 break; 957 } 958 } 959 960 static void 961 cdce_intr_read_callback(struct usb_xfer *xfer, usb_error_t error) 962 { 963 int actlen; 964 965 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 966 967 switch (USB_GET_STATE(xfer)) { 968 case USB_ST_TRANSFERRED: 969 970 DPRINTF("Received %d bytes\n", actlen); 971 972 /* TODO: decode some indications */ 973 974 /* FALLTHROUGH */ 975 case USB_ST_SETUP: 976 tr_setup: 977 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 978 usbd_transfer_submit(xfer); 979 break; 980 981 default: /* Error */ 982 if (error != USB_ERR_CANCELLED) { 983 /* start clear stall */ 984 usbd_xfer_set_stall(xfer); 985 goto tr_setup; 986 } 987 break; 988 } 989 } 990 991 static void 992 cdce_intr_write_callback(struct usb_xfer *xfer, usb_error_t error) 993 { 994 int actlen; 995 996 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 997 998 switch (USB_GET_STATE(xfer)) { 999 case USB_ST_TRANSFERRED: 1000 1001 DPRINTF("Transferred %d bytes\n", actlen); 1002 1003 /* FALLTHROUGH */ 1004 case USB_ST_SETUP: 1005 tr_setup: 1006 #if 0 1007 usbd_xfer_set_frame_len(xfer, 0, XXX); 1008 usbd_transfer_submit(xfer); 1009 #endif 1010 break; 1011 1012 default: /* Error */ 1013 if (error != USB_ERR_CANCELLED) { 1014 /* start clear stall */ 1015 usbd_xfer_set_stall(xfer); 1016 goto tr_setup; 1017 } 1018 break; 1019 } 1020 } 1021 1022 static int 1023 cdce_handle_request(device_t dev, 1024 const void *req, void **pptr, uint16_t *plen, 1025 uint16_t offset, uint8_t *pstate) 1026 { 1027 return (ENXIO); /* use builtin handler */ 1028 } 1029 1030 #if CDCE_HAVE_NCM 1031 static void 1032 cdce_ncm_tx_zero(struct usb_page_cache *pc, 1033 uint32_t start, uint32_t end) 1034 { 1035 if (start >= CDCE_NCM_TX_MAXLEN) 1036 return; 1037 if (end > CDCE_NCM_TX_MAXLEN) 1038 end = CDCE_NCM_TX_MAXLEN; 1039 1040 usbd_frame_zero(pc, start, end - start); 1041 } 1042 1043 static uint8_t 1044 cdce_ncm_fill_tx_frames(struct usb_xfer *xfer, uint8_t index) 1045 { 1046 struct cdce_softc *sc = usbd_xfer_softc(xfer); 1047 struct ifnet *ifp = uether_getifp(&sc->sc_ue); 1048 struct usb_page_cache *pc = usbd_xfer_get_frame(xfer, index); 1049 struct mbuf *m; 1050 uint32_t rem; 1051 uint32_t offset; 1052 uint32_t last_offset; 1053 uint16_t n; 1054 uint8_t retval; 1055 1056 usbd_xfer_set_frame_offset(xfer, index * CDCE_NCM_TX_MAXLEN, index); 1057 1058 offset = sizeof(sc->sc_ncm.hdr) + 1059 sizeof(sc->sc_ncm.dpt) + sizeof(sc->sc_ncm.dp); 1060 1061 /* Store last valid offset before alignment */ 1062 last_offset = offset; 1063 1064 /* Align offset */ 1065 offset = CDCE_NCM_ALIGN(sc->sc_ncm.tx_remainder, 1066 offset, sc->sc_ncm.tx_modulus); 1067 1068 /* Zero pad */ 1069 cdce_ncm_tx_zero(pc, last_offset, offset); 1070 1071 /* buffer full */ 1072 retval = 2; 1073 1074 for (n = 0; n != sc->sc_ncm.tx_nframe; n++) { 1075 1076 /* check if end of transmit buffer is reached */ 1077 1078 if (offset >= sc->sc_ncm.tx_max) 1079 break; 1080 1081 /* compute maximum buffer size */ 1082 1083 rem = sc->sc_ncm.tx_max - offset; 1084 1085 IFQ_DRV_DEQUEUE(&(ifp->if_snd), m); 1086 1087 if (m == NULL) { 1088 /* buffer not full */ 1089 retval = 1; 1090 break; 1091 } 1092 1093 if (m->m_pkthdr.len > rem) { 1094 if (n == 0) { 1095 /* The frame won't fit in our buffer */ 1096 DPRINTFN(1, "Frame too big to be transmitted!\n"); 1097 m_freem(m); 1098 ifp->if_oerrors++; 1099 n--; 1100 continue; 1101 } 1102 /* Wait till next buffer becomes ready */ 1103 IFQ_DRV_PREPEND(&(ifp->if_snd), m); 1104 break; 1105 } 1106 usbd_m_copy_in(pc, offset, m, 0, m->m_pkthdr.len); 1107 1108 USETW(sc->sc_ncm.dp[n].wFrameLength, m->m_pkthdr.len); 1109 USETW(sc->sc_ncm.dp[n].wFrameIndex, offset); 1110 1111 /* Update offset */ 1112 offset += m->m_pkthdr.len; 1113 1114 /* Store last valid offset before alignment */ 1115 last_offset = offset; 1116 1117 /* Align offset */ 1118 offset = CDCE_NCM_ALIGN(sc->sc_ncm.tx_remainder, 1119 offset, sc->sc_ncm.tx_modulus); 1120 1121 /* Zero pad */ 1122 cdce_ncm_tx_zero(pc, last_offset, offset); 1123 1124 /* 1125 * If there's a BPF listener, bounce a copy 1126 * of this frame to him: 1127 */ 1128 BPF_MTAP(ifp, m); 1129 1130 /* Free mbuf */ 1131 1132 m_freem(m); 1133 1134 /* Pre-increment interface counter */ 1135 1136 ifp->if_opackets++; 1137 } 1138 1139 if (n == 0) 1140 return (0); 1141 1142 rem = (sizeof(sc->sc_ncm.dpt) + (4 * n) + 4); 1143 1144 USETW(sc->sc_ncm.dpt.wLength, rem); 1145 1146 /* zero the rest of the data pointer entries */ 1147 for (; n != CDCE_NCM_SUBFRAMES_MAX; n++) { 1148 USETW(sc->sc_ncm.dp[n].wFrameLength, 0); 1149 USETW(sc->sc_ncm.dp[n].wFrameIndex, 0); 1150 } 1151 1152 offset = last_offset; 1153 1154 /* Align offset */ 1155 offset = CDCE_NCM_ALIGN(0, offset, CDCE_NCM_TX_MINLEN); 1156 1157 /* Optimise, save bandwidth and force short termination */ 1158 if (offset >= sc->sc_ncm.tx_max) 1159 offset = sc->sc_ncm.tx_max; 1160 else 1161 offset ++; 1162 1163 /* Zero pad */ 1164 cdce_ncm_tx_zero(pc, last_offset, offset); 1165 1166 /* set frame length */ 1167 usbd_xfer_set_frame_len(xfer, index, offset); 1168 1169 /* Fill out 16-bit header */ 1170 sc->sc_ncm.hdr.dwSignature[0] = 'N'; 1171 sc->sc_ncm.hdr.dwSignature[1] = 'C'; 1172 sc->sc_ncm.hdr.dwSignature[2] = 'M'; 1173 sc->sc_ncm.hdr.dwSignature[3] = 'H'; 1174 USETW(sc->sc_ncm.hdr.wHeaderLength, sizeof(sc->sc_ncm.hdr)); 1175 USETW(sc->sc_ncm.hdr.wBlockLength, offset); 1176 USETW(sc->sc_ncm.hdr.wSequence, sc->sc_ncm.tx_seq); 1177 USETW(sc->sc_ncm.hdr.wDptIndex, sizeof(sc->sc_ncm.hdr)); 1178 1179 sc->sc_ncm.tx_seq++; 1180 1181 /* Fill out 16-bit frame table header */ 1182 sc->sc_ncm.dpt.dwSignature[0] = 'N'; 1183 sc->sc_ncm.dpt.dwSignature[1] = 'C'; 1184 sc->sc_ncm.dpt.dwSignature[2] = 'M'; 1185 sc->sc_ncm.dpt.dwSignature[3] = '0'; 1186 USETW(sc->sc_ncm.dpt.wNextNdpIndex, 0); /* reserved */ 1187 1188 usbd_copy_in(pc, 0, &(sc->sc_ncm.hdr), sizeof(sc->sc_ncm.hdr)); 1189 usbd_copy_in(pc, sizeof(sc->sc_ncm.hdr), &(sc->sc_ncm.dpt), 1190 sizeof(sc->sc_ncm.dpt)); 1191 usbd_copy_in(pc, sizeof(sc->sc_ncm.hdr) + sizeof(sc->sc_ncm.dpt), 1192 &(sc->sc_ncm.dp), sizeof(sc->sc_ncm.dp)); 1193 return (retval); 1194 } 1195 1196 static void 1197 cdce_ncm_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error) 1198 { 1199 struct cdce_softc *sc = usbd_xfer_softc(xfer); 1200 struct ifnet *ifp = uether_getifp(&sc->sc_ue); 1201 uint16_t x; 1202 uint8_t temp; 1203 int actlen; 1204 int aframes; 1205 1206 switch (USB_GET_STATE(xfer)) { 1207 case USB_ST_TRANSFERRED: 1208 1209 usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL); 1210 1211 DPRINTFN(10, "transfer complete: " 1212 "%u bytes in %u frames\n", actlen, aframes); 1213 1214 case USB_ST_SETUP: 1215 for (x = 0; x != CDCE_NCM_TX_FRAMES_MAX; x++) { 1216 temp = cdce_ncm_fill_tx_frames(xfer, x); 1217 if (temp == 0) 1218 break; 1219 if (temp == 1) { 1220 x++; 1221 break; 1222 } 1223 } 1224 1225 if (x != 0) { 1226 #ifdef USB_DEBUG 1227 usbd_xfer_set_interval(xfer, cdce_tx_interval); 1228 #endif 1229 usbd_xfer_set_frames(xfer, x); 1230 usbd_transfer_submit(xfer); 1231 } 1232 break; 1233 1234 default: /* Error */ 1235 DPRINTFN(10, "Transfer error: %s\n", 1236 usbd_errstr(error)); 1237 1238 /* update error counter */ 1239 ifp->if_oerrors += 1; 1240 1241 if (error != USB_ERR_CANCELLED) { 1242 /* try to clear stall first */ 1243 usbd_xfer_set_stall(xfer); 1244 usbd_xfer_set_frames(xfer, 0); 1245 usbd_transfer_submit(xfer); 1246 } 1247 break; 1248 } 1249 } 1250 1251 static void 1252 cdce_ncm_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error) 1253 { 1254 struct cdce_softc *sc = usbd_xfer_softc(xfer); 1255 struct usb_page_cache *pc = usbd_xfer_get_frame(xfer, 0); 1256 struct ifnet *ifp = uether_getifp(&sc->sc_ue); 1257 struct mbuf *m; 1258 int sumdata; 1259 int sumlen; 1260 int actlen; 1261 int aframes; 1262 int temp; 1263 int nframes; 1264 int x; 1265 int offset; 1266 1267 switch (USB_GET_STATE(xfer)) { 1268 case USB_ST_TRANSFERRED: 1269 1270 usbd_xfer_status(xfer, &actlen, &sumlen, &aframes, NULL); 1271 1272 DPRINTFN(1, "received %u bytes in %u frames\n", 1273 actlen, aframes); 1274 1275 if (actlen < (sizeof(sc->sc_ncm.hdr) + 1276 sizeof(sc->sc_ncm.dpt))) { 1277 DPRINTFN(1, "frame too short\n"); 1278 goto tr_setup; 1279 } 1280 usbd_copy_out(pc, 0, &(sc->sc_ncm.hdr), 1281 sizeof(sc->sc_ncm.hdr)); 1282 1283 if ((sc->sc_ncm.hdr.dwSignature[0] != 'N') || 1284 (sc->sc_ncm.hdr.dwSignature[1] != 'C') || 1285 (sc->sc_ncm.hdr.dwSignature[2] != 'M') || 1286 (sc->sc_ncm.hdr.dwSignature[3] != 'H')) { 1287 DPRINTFN(1, "invalid HDR signature: " 1288 "0x%02x:0x%02x:0x%02x:0x%02x\n", 1289 sc->sc_ncm.hdr.dwSignature[0], 1290 sc->sc_ncm.hdr.dwSignature[1], 1291 sc->sc_ncm.hdr.dwSignature[2], 1292 sc->sc_ncm.hdr.dwSignature[3]); 1293 goto tr_stall; 1294 } 1295 temp = UGETW(sc->sc_ncm.hdr.wBlockLength); 1296 if (temp > sumlen) { 1297 DPRINTFN(1, "unsupported block length %u/%u\n", 1298 temp, sumlen); 1299 goto tr_stall; 1300 } 1301 temp = UGETW(sc->sc_ncm.hdr.wDptIndex); 1302 if ((temp + sizeof(sc->sc_ncm.dpt)) > actlen) { 1303 DPRINTFN(1, "invalid DPT index: 0x%04x\n", temp); 1304 goto tr_stall; 1305 } 1306 usbd_copy_out(pc, temp, &(sc->sc_ncm.dpt), 1307 sizeof(sc->sc_ncm.dpt)); 1308 1309 if ((sc->sc_ncm.dpt.dwSignature[0] != 'N') || 1310 (sc->sc_ncm.dpt.dwSignature[1] != 'C') || 1311 (sc->sc_ncm.dpt.dwSignature[2] != 'M') || 1312 (sc->sc_ncm.dpt.dwSignature[3] != '0')) { 1313 DPRINTFN(1, "invalid DPT signature" 1314 "0x%02x:0x%02x:0x%02x:0x%02x\n", 1315 sc->sc_ncm.dpt.dwSignature[0], 1316 sc->sc_ncm.dpt.dwSignature[1], 1317 sc->sc_ncm.dpt.dwSignature[2], 1318 sc->sc_ncm.dpt.dwSignature[3]); 1319 goto tr_stall; 1320 } 1321 nframes = UGETW(sc->sc_ncm.dpt.wLength) / 4; 1322 1323 /* Subtract size of header and last zero padded entry */ 1324 if (nframes >= (2 + 1)) 1325 nframes -= (2 + 1); 1326 else 1327 nframes = 0; 1328 1329 DPRINTFN(1, "nframes = %u\n", nframes); 1330 1331 temp += sizeof(sc->sc_ncm.dpt); 1332 1333 if ((temp + (4 * nframes)) > actlen) 1334 goto tr_stall; 1335 1336 if (nframes > CDCE_NCM_SUBFRAMES_MAX) { 1337 DPRINTFN(1, "Truncating number of frames from %u to %u\n", 1338 nframes, CDCE_NCM_SUBFRAMES_MAX); 1339 nframes = CDCE_NCM_SUBFRAMES_MAX; 1340 } 1341 usbd_copy_out(pc, temp, &(sc->sc_ncm.dp), (4 * nframes)); 1342 1343 sumdata = 0; 1344 1345 for (x = 0; x != nframes; x++) { 1346 1347 offset = UGETW(sc->sc_ncm.dp[x].wFrameIndex); 1348 temp = UGETW(sc->sc_ncm.dp[x].wFrameLength); 1349 1350 if ((offset == 0) || 1351 (temp < sizeof(struct ether_header)) || 1352 (temp > (MCLBYTES - ETHER_ALIGN))) { 1353 DPRINTFN(1, "NULL frame detected at %d\n", x); 1354 m = NULL; 1355 /* silently ignore this frame */ 1356 continue; 1357 } else if ((offset + temp) > actlen) { 1358 DPRINTFN(1, "invalid frame " 1359 "detected at %d\n", x); 1360 m = NULL; 1361 /* silently ignore this frame */ 1362 continue; 1363 } else if (temp > (MHLEN - ETHER_ALIGN)) { 1364 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 1365 } else { 1366 m = m_gethdr(M_DONTWAIT, MT_DATA); 1367 } 1368 1369 DPRINTFN(16, "frame %u, offset = %u, length = %u \n", 1370 x, offset, temp); 1371 1372 /* check if we have a buffer */ 1373 if (m) { 1374 m_adj(m, ETHER_ALIGN); 1375 1376 usbd_copy_out(pc, offset, m->m_data, temp); 1377 1378 /* enqueue */ 1379 uether_rxmbuf(&sc->sc_ue, m, temp); 1380 1381 sumdata += temp; 1382 } else { 1383 ifp->if_ierrors++; 1384 } 1385 } 1386 1387 DPRINTFN(1, "Efficiency: %u/%u bytes\n", sumdata, actlen); 1388 1389 case USB_ST_SETUP: 1390 tr_setup: 1391 usbd_xfer_set_frame_len(xfer, 0, sc->sc_ncm.rx_max); 1392 usbd_xfer_set_frames(xfer, 1); 1393 usbd_transfer_submit(xfer); 1394 uether_rxflush(&sc->sc_ue); /* must be last */ 1395 break; 1396 1397 default: /* Error */ 1398 DPRINTFN(1, "error = %s\n", 1399 usbd_errstr(error)); 1400 1401 if (error != USB_ERR_CANCELLED) { 1402 tr_stall: 1403 /* try to clear stall first */ 1404 usbd_xfer_set_stall(xfer); 1405 usbd_xfer_set_frames(xfer, 0); 1406 usbd_transfer_submit(xfer); 1407 } 1408 break; 1409 } 1410 } 1411 #endif 1412