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