52 53#define USB_DEBUG_VAR cdce_debug 54 55#include <dev/usb/usb_core.h> 56#include <dev/usb/usb_lookup.h> 57#include <dev/usb/usb_process.h> 58#include <dev/usb/usb_debug.h> 59#include <dev/usb/usb_request.h> 60#include <dev/usb/usb_busdma.h> 61#include <dev/usb/usb_util.h> 62#include <dev/usb/usb_parse.h> 63#include <dev/usb/usb_device.h> 64 65#include <dev/usb/net/usb_ethernet.h> 66#include <dev/usb/net/if_cdcereg.h> 67 68static device_probe_t cdce_probe; 69static device_attach_t cdce_attach; 70static device_detach_t cdce_detach; 71static device_shutdown_t cdce_shutdown; 72static device_suspend_t cdce_suspend; 73static device_resume_t cdce_resume; 74static usb_handle_request_t cdce_handle_request; 75 76static usb2_callback_t cdce_bulk_write_callback; 77static usb2_callback_t cdce_bulk_read_callback; 78static usb2_callback_t cdce_intr_read_callback; 79static usb2_callback_t cdce_intr_write_callback; 80 81static usb2_ether_fn_t cdce_attach_post; 82static usb2_ether_fn_t cdce_init; 83static usb2_ether_fn_t cdce_stop; 84static usb2_ether_fn_t cdce_start; 85static usb2_ether_fn_t cdce_setmulti; 86static usb2_ether_fn_t cdce_setpromisc; 87 88static uint32_t cdce_m_crc32(struct mbuf *, uint32_t, uint32_t); 89 90#if USB_DEBUG 91static int cdce_debug = 0; 92 93SYSCTL_NODE(_hw_usb2, OID_AUTO, cdce, CTLFLAG_RW, 0, "USB CDC-Ethernet"); 94SYSCTL_INT(_hw_usb2_cdce, OID_AUTO, debug, CTLFLAG_RW, &cdce_debug, 0, 95 "Debug level"); 96#endif 97 98static const struct usb2_config cdce_config[CDCE_N_TRANSFER] = { 99 100 [CDCE_BULK_A] = { 101 .type = UE_BULK, 102 .endpoint = UE_ADDR_ANY, 103 .direction = UE_DIR_OUT, 104 .if_index = 0, 105 /* Host Mode */ 106 .mh.frames = CDCE_FRAMES_MAX, 107 .mh.bufsize = (CDCE_FRAMES_MAX * MCLBYTES), 108 .mh.flags = {.pipe_bof = 1,.force_short_xfer = 1,.ext_buffer = 1,}, 109 .mh.callback = cdce_bulk_write_callback, 110 .mh.timeout = 10000, /* 10 seconds */ 111 /* Device Mode */ 112 .md.frames = CDCE_FRAMES_MAX, 113 .md.bufsize = (CDCE_FRAMES_MAX * MCLBYTES), 114 .md.flags = {.pipe_bof = 1,.short_frames_ok = 1,.short_xfer_ok = 1,.ext_buffer = 1,}, 115 .md.callback = cdce_bulk_read_callback, 116 .md.timeout = 0, /* no timeout */ 117 }, 118 119 [CDCE_BULK_B] = { 120 .type = UE_BULK, 121 .endpoint = UE_ADDR_ANY, 122 .direction = UE_DIR_IN, 123 .if_index = 0, 124 /* Host Mode */ 125 .mh.frames = CDCE_FRAMES_MAX, 126 .mh.bufsize = (CDCE_FRAMES_MAX * MCLBYTES), 127 .mh.flags = {.pipe_bof = 1,.short_frames_ok = 1,.short_xfer_ok = 1,.ext_buffer = 1,}, 128 .mh.callback = cdce_bulk_read_callback, 129 .mh.timeout = 0, /* no timeout */ 130 /* Device Mode */ 131 .md.frames = CDCE_FRAMES_MAX, 132 .md.bufsize = (CDCE_FRAMES_MAX * MCLBYTES), 133 .md.flags = {.pipe_bof = 1,.force_short_xfer = 1,.ext_buffer = 1,}, 134 .md.callback = cdce_bulk_write_callback, 135 .md.timeout = 10000, /* 10 seconds */ 136 }, 137 138 [CDCE_INTR] = { 139 .type = UE_INTERRUPT, 140 .endpoint = UE_ADDR_ANY, 141 .direction = UE_DIR_IN, 142 .if_index = 1, 143 /* Host Mode */ 144 .mh.bufsize = CDCE_IND_SIZE_MAX, 145 .mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,.no_pipe_ok = 1,}, 146 .mh.callback = cdce_intr_read_callback, 147 .mh.timeout = 0, 148 /* Device Mode */ 149 .md.bufsize = CDCE_IND_SIZE_MAX, 150 .md.flags = {.pipe_bof = 1,.force_short_xfer = 1,.no_pipe_ok = 1,}, 151 .md.callback = cdce_intr_write_callback, 152 .md.timeout = 10000, /* 10 seconds */ 153 }, 154}; 155 156static device_method_t cdce_methods[] = { 157 /* USB interface */ 158 DEVMETHOD(usb_handle_request, cdce_handle_request), 159 160 /* Device interface */ 161 DEVMETHOD(device_probe, cdce_probe), 162 DEVMETHOD(device_attach, cdce_attach), 163 DEVMETHOD(device_detach, cdce_detach), 164 DEVMETHOD(device_suspend, cdce_suspend), 165 DEVMETHOD(device_resume, cdce_resume), 166 DEVMETHOD(device_shutdown, cdce_shutdown), 167 168 {0, 0} 169}; 170 171static driver_t cdce_driver = { 172 .name = "cdce", 173 .methods = cdce_methods, 174 .size = sizeof(struct cdce_softc), 175}; 176 177static devclass_t cdce_devclass; 178 179DRIVER_MODULE(cdce, uhub, cdce_driver, cdce_devclass, NULL, 0); 180MODULE_VERSION(cdce, 1); 181MODULE_DEPEND(cdce, uether, 1, 1, 1); 182MODULE_DEPEND(cdce, usb, 1, 1, 1); 183MODULE_DEPEND(cdce, ether, 1, 1, 1); 184 185static const struct usb2_ether_methods cdce_ue_methods = { 186 .ue_attach_post = cdce_attach_post, 187 .ue_start = cdce_start, 188 .ue_init = cdce_init, 189 .ue_stop = cdce_stop, 190 .ue_setmulti = cdce_setmulti, 191 .ue_setpromisc = cdce_setpromisc, 192}; 193 194static const struct usb2_device_id cdce_devs[] = { 195 {USB_IF_CSI(UICLASS_CDC, UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL, 0)}, 196 {USB_IF_CSI(UICLASS_CDC, UISUBCLASS_MOBILE_DIRECT_LINE_MODEL, 0)}, 197 198 {USB_VPI(USB_VENDOR_ACERLABS, USB_PRODUCT_ACERLABS_M5632, CDCE_FLAG_NO_UNION)}, 199 {USB_VPI(USB_VENDOR_AMBIT, USB_PRODUCT_AMBIT_NTL_250, CDCE_FLAG_NO_UNION)}, 200 {USB_VPI(USB_VENDOR_COMPAQ, USB_PRODUCT_COMPAQ_IPAQLINUX, CDCE_FLAG_NO_UNION)}, 201 {USB_VPI(USB_VENDOR_GMATE, USB_PRODUCT_GMATE_YP3X00, CDCE_FLAG_NO_UNION)}, 202 {USB_VPI(USB_VENDOR_MOTOROLA2, USB_PRODUCT_MOTOROLA2_USBLAN, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)}, 203 {USB_VPI(USB_VENDOR_MOTOROLA2, USB_PRODUCT_MOTOROLA2_USBLAN2, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)}, 204 {USB_VPI(USB_VENDOR_NETCHIP, USB_PRODUCT_NETCHIP_ETHERNETGADGET, CDCE_FLAG_NO_UNION)}, 205 {USB_VPI(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2501, CDCE_FLAG_NO_UNION)}, 206 {USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SL5500, CDCE_FLAG_ZAURUS)}, 207 {USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SL5600, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)}, 208 {USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SLA300, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)}, 209 {USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SLC700, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)}, 210 {USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SLC750, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)}, 211}; 212 213static int 214cdce_probe(device_t dev) 215{ 216 struct usb2_attach_arg *uaa = device_get_ivars(dev); 217 218 return (usb2_lookup_id_by_uaa(cdce_devs, sizeof(cdce_devs), uaa)); 219} 220 221static void 222cdce_attach_post(struct usb2_ether *ue) 223{ 224 /* no-op */ 225 return; 226} 227 228static int 229cdce_attach(device_t dev) 230{ 231 struct cdce_softc *sc = device_get_softc(dev); 232 struct usb2_ether *ue = &sc->sc_ue; 233 struct usb2_attach_arg *uaa = device_get_ivars(dev); 234 struct usb2_interface *iface; 235 const struct usb2_cdc_union_descriptor *ud; 236 const struct usb2_interface_descriptor *id; 237 const struct usb2_cdc_ethernet_descriptor *ued; 238 int error; 239 uint8_t i; 240 char eaddr_str[5 * ETHER_ADDR_LEN]; /* approx */ 241 242 sc->sc_flags = USB_GET_DRIVER_INFO(uaa); 243 244 device_set_usb2_desc(dev); 245 246 mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF); 247 248 if (sc->sc_flags & CDCE_FLAG_NO_UNION) { 249 sc->sc_ifaces_index[0] = uaa->info.bIfaceIndex; 250 sc->sc_ifaces_index[1] = uaa->info.bIfaceIndex; 251 sc->sc_data_iface_no = 0; /* not used */ 252 goto alloc_transfers; 253 } 254 ud = usb2_find_descriptor 255 (uaa->device, NULL, uaa->info.bIfaceIndex, 256 UDESC_CS_INTERFACE, 0 - 1, UDESCSUB_CDC_UNION, 0 - 1); 257 258 if ((ud == NULL) || (ud->bLength < sizeof(*ud))) { 259 device_printf(dev, "no union descriptor!\n"); 260 goto detach; 261 } 262 sc->sc_data_iface_no = ud->bSlaveInterface[0]; 263 264 for (i = 0;; i++) { 265 266 iface = usb2_get_iface(uaa->device, i); 267 268 if (iface) { 269 270 id = usb2_get_interface_descriptor(iface); 271 272 if (id && (id->bInterfaceNumber == 273 sc->sc_data_iface_no)) { 274 sc->sc_ifaces_index[0] = i; 275 sc->sc_ifaces_index[1] = uaa->info.bIfaceIndex; 276 usb2_set_parent_iface(uaa->device, i, uaa->info.bIfaceIndex); 277 break; 278 } 279 } else { 280 device_printf(dev, "no data interface found!\n"); 281 goto detach; 282 } 283 } 284 285 /* 286 * <quote> 287 * 288 * The Data Class interface of a networking device shall have 289 * a minimum of two interface settings. The first setting 290 * (the default interface setting) includes no endpoints and 291 * therefore no networking traffic is exchanged whenever the 292 * default interface setting is selected. One or more 293 * additional interface settings are used for normal 294 * operation, and therefore each includes a pair of endpoints 295 * (one IN, and one OUT) to exchange network traffic. Select 296 * an alternate interface setting to initialize the network 297 * aspects of the device and to enable the exchange of 298 * network traffic. 299 * 300 * </quote> 301 * 302 * Some devices, most notably cable modems, include interface 303 * settings that have no IN or OUT endpoint, therefore loop 304 * through the list of all available interface settings 305 * looking for one with both IN and OUT endpoints. 306 */ 307 308alloc_transfers: 309 310 for (i = 0; i != 32; i++) { 311 312 error = usb2_set_alt_interface_index 313 (uaa->device, sc->sc_ifaces_index[0], i); 314 315 if (error) { 316 device_printf(dev, "no valid alternate " 317 "setting found!\n"); 318 goto detach; 319 } 320 error = usb2_transfer_setup 321 (uaa->device, sc->sc_ifaces_index, 322 sc->sc_xfer, cdce_config, CDCE_N_TRANSFER, 323 sc, &sc->sc_mtx); 324 325 if (error == 0) { 326 break; 327 } 328 } 329 330 ued = usb2_find_descriptor 331 (uaa->device, NULL, uaa->info.bIfaceIndex, 332 UDESC_CS_INTERFACE, 0 - 1, UDESCSUB_CDC_ENF, 0 - 1); 333 334 if ((ued == NULL) || (ued->bLength < sizeof(*ued))) { 335 error = USB_ERR_INVAL; 336 } else { 337 error = usb2_req_get_string_any(uaa->device, NULL, 338 eaddr_str, sizeof(eaddr_str), ued->iMacAddress); 339 } 340 341 if (error) { 342 343 /* fake MAC address */ 344 345 device_printf(dev, "faking MAC address\n"); 346 sc->sc_ue.ue_eaddr[0] = 0x2a; 347 memcpy(&sc->sc_ue.ue_eaddr[1], &ticks, sizeof(uint32_t)); 348 sc->sc_ue.ue_eaddr[5] = device_get_unit(dev); 349 350 } else { 351 352 bzero(sc->sc_ue.ue_eaddr, sizeof(sc->sc_ue.ue_eaddr)); 353 354 for (i = 0; i != (ETHER_ADDR_LEN * 2); i++) { 355 356 char c = eaddr_str[i]; 357 358 if ('0' <= c && c <= '9') 359 c -= '0'; 360 else if (c != 0) 361 c -= 'A' - 10; 362 else 363 break; 364 365 c &= 0xf; 366 367 if ((i & 1) == 0) 368 c <<= 4; 369 sc->sc_ue.ue_eaddr[i / 2] |= c; 370 } 371 372 if (uaa->usb2_mode == USB_MODE_DEVICE) { 373 /* 374 * Do not use the same MAC address like the peer ! 375 */ 376 sc->sc_ue.ue_eaddr[5] ^= 0xFF; 377 } 378 } 379 380 ue->ue_sc = sc; 381 ue->ue_dev = dev; 382 ue->ue_udev = uaa->device; 383 ue->ue_mtx = &sc->sc_mtx; 384 ue->ue_methods = &cdce_ue_methods; 385 386 error = usb2_ether_ifattach(ue); 387 if (error) { 388 device_printf(dev, "could not attach interface\n"); 389 goto detach; 390 } 391 return (0); /* success */ 392 393detach: 394 cdce_detach(dev); 395 return (ENXIO); /* failure */ 396} 397 398static int 399cdce_detach(device_t dev) 400{ 401 struct cdce_softc *sc = device_get_softc(dev); 402 struct usb2_ether *ue = &sc->sc_ue; 403 404 /* stop all USB transfers first */ 405 usb2_transfer_unsetup(sc->sc_xfer, CDCE_N_TRANSFER); 406 usb2_ether_ifdetach(ue); 407 mtx_destroy(&sc->sc_mtx); 408 409 return (0); 410} 411 412static void 413cdce_start(struct usb2_ether *ue) 414{ 415 struct cdce_softc *sc = usb2_ether_getsc(ue); 416 417 /* 418 * Start the USB transfers, if not already started: 419 */ 420 usb2_transfer_start(sc->sc_xfer[CDCE_BULK_B]); 421 usb2_transfer_start(sc->sc_xfer[CDCE_BULK_A]); 422} 423 424static void 425cdce_free_queue(struct mbuf **ppm, uint8_t n) 426{ 427 uint8_t x; 428 for (x = 0; x != n; x++) { 429 if (ppm[x] != NULL) { 430 m_freem(ppm[x]); 431 ppm[x] = NULL; 432 } 433 } 434} 435 436static void 437cdce_bulk_write_callback(struct usb2_xfer *xfer) 438{ 439 struct cdce_softc *sc = xfer->priv_sc; 440 struct ifnet *ifp = usb2_ether_getifp(&sc->sc_ue); 441 struct mbuf *m; 442 struct mbuf *mt; 443 uint32_t crc; 444 uint8_t x; 445 446 DPRINTFN(1, "\n"); 447 448 switch (USB_GET_STATE(xfer)) { 449 case USB_ST_TRANSFERRED: 450 DPRINTFN(11, "transfer complete: " 451 "%u bytes in %u frames\n", xfer->actlen, 452 xfer->aframes); 453 454 ifp->if_opackets++; 455 456 /* free all previous TX buffers */ 457 cdce_free_queue(sc->sc_tx_buf, CDCE_FRAMES_MAX); 458 459 /* FALLTHROUGH */ 460 case USB_ST_SETUP: 461tr_setup: 462 for (x = 0; x != CDCE_FRAMES_MAX; x++) { 463 464 IFQ_DRV_DEQUEUE(&ifp->if_snd, m); 465 466 if (m == NULL) 467 break; 468 469 if (sc->sc_flags & CDCE_FLAG_ZAURUS) { 470 /* 471 * Zaurus wants a 32-bit CRC appended 472 * to every frame 473 */ 474 475 crc = cdce_m_crc32(m, 0, m->m_pkthdr.len); 476 crc = htole32(crc); 477 478 if (!m_append(m, 4, (void *)&crc)) { 479 m_freem(m); 480 ifp->if_oerrors++; 481 continue; 482 } 483 } 484 if (m->m_len != m->m_pkthdr.len) { 485 mt = m_defrag(m, M_DONTWAIT); 486 if (mt == NULL) { 487 m_freem(m); 488 ifp->if_oerrors++; 489 continue; 490 } 491 m = mt; 492 } 493 if (m->m_pkthdr.len > MCLBYTES) { 494 m->m_pkthdr.len = MCLBYTES; 495 } 496 sc->sc_tx_buf[x] = m; 497 xfer->frlengths[x] = m->m_len; 498 usb2_set_frame_data(xfer, m->m_data, x); 499 500 /* 501 * If there's a BPF listener, bounce a copy of 502 * this frame to him: 503 */ 504 BPF_MTAP(ifp, m); 505 } 506 if (x != 0) { 507 xfer->nframes = x; 508 usb2_start_hardware(xfer); 509 } 510 break; 511 512 default: /* Error */ 513 DPRINTFN(11, "transfer error, %s\n", 514 usb2_errstr(xfer->error)); 515 516 /* free all previous TX buffers */ 517 cdce_free_queue(sc->sc_tx_buf, CDCE_FRAMES_MAX); 518 519 /* count output errors */ 520 ifp->if_oerrors++; 521 522 if (xfer->error != USB_ERR_CANCELLED) { 523 /* try to clear stall first */ 524 xfer->flags.stall_pipe = 1; 525 goto tr_setup; 526 } 527 break; 528 } 529} 530 531static int32_t 532cdce_m_crc32_cb(void *arg, void *src, uint32_t count) 533{ 534 uint32_t *p_crc = arg; 535 536 *p_crc = crc32_raw(src, count, *p_crc); 537 return (0); 538} 539 540static uint32_t 541cdce_m_crc32(struct mbuf *m, uint32_t src_offset, uint32_t src_len) 542{ 543 uint32_t crc = 0xFFFFFFFF; 544 int error; 545 546 error = m_apply(m, src_offset, src_len, cdce_m_crc32_cb, &crc); 547 return (crc ^ 0xFFFFFFFF); 548} 549 550static void 551cdce_init(struct usb2_ether *ue) 552{ 553 struct cdce_softc *sc = usb2_ether_getsc(ue); 554 struct ifnet *ifp = usb2_ether_getifp(ue); 555 556 CDCE_LOCK_ASSERT(sc, MA_OWNED); 557 558 ifp->if_drv_flags |= IFF_DRV_RUNNING; 559 560 /* start interrupt transfer */ 561 usb2_transfer_start(sc->sc_xfer[CDCE_INTR]); 562 563 /* stall data write direction, which depends on USB mode */ 564 if (usb2_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST) 565 usb2_transfer_set_stall(sc->sc_xfer[CDCE_BULK_A]); 566 else 567 usb2_transfer_set_stall(sc->sc_xfer[CDCE_BULK_B]); 568 569 /* start data transfers */ 570 cdce_start(ue); 571} 572 573static void 574cdce_stop(struct usb2_ether *ue) 575{ 576 struct cdce_softc *sc = usb2_ether_getsc(ue); 577 struct ifnet *ifp = usb2_ether_getifp(ue); 578 579 CDCE_LOCK_ASSERT(sc, MA_OWNED); 580 581 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 582 583 /* 584 * stop all the transfers, if not already stopped: 585 */ 586 usb2_transfer_stop(sc->sc_xfer[CDCE_BULK_A]); 587 usb2_transfer_stop(sc->sc_xfer[CDCE_BULK_B]); 588 usb2_transfer_stop(sc->sc_xfer[CDCE_INTR]); 589} 590 591static void 592cdce_setmulti(struct usb2_ether *ue) 593{ 594 /* no-op */ 595 return; 596} 597 598static void 599cdce_setpromisc(struct usb2_ether *ue) 600{ 601 /* no-op */ 602 return; 603} 604 605static int 606cdce_shutdown(device_t dev) 607{ 608 struct cdce_softc *sc = device_get_softc(dev); 609 610 usb2_ether_ifshutdown(&sc->sc_ue); 611 612 return (0); 613} 614 615static int 616cdce_suspend(device_t dev) 617{ 618 device_printf(dev, "Suspending\n"); 619 return (0); 620} 621 622static int 623cdce_resume(device_t dev) 624{ 625 device_printf(dev, "Resuming\n"); 626 return (0); 627} 628 629static void 630cdce_bulk_read_callback(struct usb2_xfer *xfer) 631{ 632 struct cdce_softc *sc = xfer->priv_sc; 633 struct mbuf *m; 634 uint8_t x; 635 636 switch (USB_GET_STATE(xfer)) { 637 case USB_ST_TRANSFERRED: 638 639 DPRINTF("received %u bytes in %u frames\n", 640 xfer->actlen, xfer->aframes); 641 642 for (x = 0; x != xfer->aframes; x++) { 643 644 m = sc->sc_rx_buf[x]; 645 sc->sc_rx_buf[x] = NULL; 646 647 /* Strip off CRC added by Zaurus, if any */ 648 if ((sc->sc_flags & CDCE_FLAG_ZAURUS) && 649 (xfer->frlengths[x] >= 14)) 650 xfer->frlengths[x] -= 4; 651 652 if (xfer->frlengths[x] < sizeof(struct ether_header)) { 653 m_freem(m); 654 continue; 655 } 656 /* queue up mbuf */ 657 usb2_ether_rxmbuf(&sc->sc_ue, m, xfer->frlengths[x]); 658 } 659 660 /* FALLTHROUGH */ 661 case USB_ST_SETUP: 662 /* 663 * TODO: Implement support for multi frame transfers, 664 * when the USB hardware supports it. 665 */ 666 for (x = 0; x != 1; x++) { 667 if (sc->sc_rx_buf[x] == NULL) { 668 m = usb2_ether_newbuf(); 669 if (m == NULL) 670 goto tr_stall; 671 sc->sc_rx_buf[x] = m; 672 } else { 673 m = sc->sc_rx_buf[x]; 674 } 675 676 usb2_set_frame_data(xfer, m->m_data, x); 677 xfer->frlengths[x] = m->m_len; 678 } 679 /* set number of frames and start hardware */ 680 xfer->nframes = x; 681 usb2_start_hardware(xfer); 682 /* flush any received frames */ 683 usb2_ether_rxflush(&sc->sc_ue); 684 break; 685 686 default: /* Error */ 687 DPRINTF("error = %s\n", 688 usb2_errstr(xfer->error)); 689 690 if (xfer->error != USB_ERR_CANCELLED) { 691tr_stall: 692 /* try to clear stall first */ 693 xfer->flags.stall_pipe = 1; 694 xfer->nframes = 0; 695 usb2_start_hardware(xfer); 696 break; 697 } 698 699 /* need to free the RX-mbufs when we are cancelled */ 700 cdce_free_queue(sc->sc_rx_buf, CDCE_FRAMES_MAX); 701 break; 702 } 703} 704 705static void 706cdce_intr_read_callback(struct usb2_xfer *xfer) 707{ 708 ; /* style fix */ 709 switch (USB_GET_STATE(xfer)) { 710 case USB_ST_TRANSFERRED: 711 712 DPRINTF("Received %d bytes\n", 713 xfer->actlen); 714 715 /* TODO: decode some indications */ 716 717 /* FALLTHROUGH */ 718 case USB_ST_SETUP: 719tr_setup: 720 xfer->frlengths[0] = xfer->max_data_length; 721 usb2_start_hardware(xfer); 722 break; 723 724 default: /* Error */ 725 if (xfer->error != USB_ERR_CANCELLED) { 726 /* start clear stall */ 727 xfer->flags.stall_pipe = 1; 728 goto tr_setup; 729 } 730 break; 731 } 732} 733 734static void 735cdce_intr_write_callback(struct usb2_xfer *xfer) 736{ 737 ; /* style fix */ 738 switch (USB_GET_STATE(xfer)) { 739 case USB_ST_TRANSFERRED: 740 741 DPRINTF("Transferred %d bytes\n", xfer->actlen); 742 743 /* FALLTHROUGH */ 744 case USB_ST_SETUP: 745tr_setup: 746#if 0 747 xfer->frlengths[0] = XXX; 748 usb2_start_hardware(xfer); 749#endif 750 break; 751 752 default: /* Error */ 753 if (xfer->error != USB_ERR_CANCELLED) { 754 /* start clear stall */ 755 xfer->flags.stall_pipe = 1; 756 goto tr_setup; 757 } 758 break; 759 } 760} 761 762static int 763cdce_handle_request(device_t dev, 764 const void *req, void **pptr, uint16_t *plen, 765 uint16_t offset, uint8_t is_complete) 766{ 767 return (ENXIO); /* use builtin handler */ 768}
| 51 52#define USB_DEBUG_VAR cdce_debug 53 54#include <dev/usb/usb_core.h> 55#include <dev/usb/usb_lookup.h> 56#include <dev/usb/usb_process.h> 57#include <dev/usb/usb_debug.h> 58#include <dev/usb/usb_request.h> 59#include <dev/usb/usb_busdma.h> 60#include <dev/usb/usb_util.h> 61#include <dev/usb/usb_parse.h> 62#include <dev/usb/usb_device.h> 63 64#include <dev/usb/net/usb_ethernet.h> 65#include <dev/usb/net/if_cdcereg.h> 66 67static device_probe_t cdce_probe; 68static device_attach_t cdce_attach; 69static device_detach_t cdce_detach; 70static device_shutdown_t cdce_shutdown; 71static device_suspend_t cdce_suspend; 72static device_resume_t cdce_resume; 73static usb_handle_request_t cdce_handle_request; 74 75static usb2_callback_t cdce_bulk_write_callback; 76static usb2_callback_t cdce_bulk_read_callback; 77static usb2_callback_t cdce_intr_read_callback; 78static usb2_callback_t cdce_intr_write_callback; 79 80static usb2_ether_fn_t cdce_attach_post; 81static usb2_ether_fn_t cdce_init; 82static usb2_ether_fn_t cdce_stop; 83static usb2_ether_fn_t cdce_start; 84static usb2_ether_fn_t cdce_setmulti; 85static usb2_ether_fn_t cdce_setpromisc; 86 87static uint32_t cdce_m_crc32(struct mbuf *, uint32_t, uint32_t); 88 89#if USB_DEBUG 90static int cdce_debug = 0; 91 92SYSCTL_NODE(_hw_usb2, OID_AUTO, cdce, CTLFLAG_RW, 0, "USB CDC-Ethernet"); 93SYSCTL_INT(_hw_usb2_cdce, OID_AUTO, debug, CTLFLAG_RW, &cdce_debug, 0, 94 "Debug level"); 95#endif 96 97static const struct usb2_config cdce_config[CDCE_N_TRANSFER] = { 98 99 [CDCE_BULK_A] = { 100 .type = UE_BULK, 101 .endpoint = UE_ADDR_ANY, 102 .direction = UE_DIR_OUT, 103 .if_index = 0, 104 /* Host Mode */ 105 .mh.frames = CDCE_FRAMES_MAX, 106 .mh.bufsize = (CDCE_FRAMES_MAX * MCLBYTES), 107 .mh.flags = {.pipe_bof = 1,.force_short_xfer = 1,.ext_buffer = 1,}, 108 .mh.callback = cdce_bulk_write_callback, 109 .mh.timeout = 10000, /* 10 seconds */ 110 /* Device Mode */ 111 .md.frames = CDCE_FRAMES_MAX, 112 .md.bufsize = (CDCE_FRAMES_MAX * MCLBYTES), 113 .md.flags = {.pipe_bof = 1,.short_frames_ok = 1,.short_xfer_ok = 1,.ext_buffer = 1,}, 114 .md.callback = cdce_bulk_read_callback, 115 .md.timeout = 0, /* no timeout */ 116 }, 117 118 [CDCE_BULK_B] = { 119 .type = UE_BULK, 120 .endpoint = UE_ADDR_ANY, 121 .direction = UE_DIR_IN, 122 .if_index = 0, 123 /* Host Mode */ 124 .mh.frames = CDCE_FRAMES_MAX, 125 .mh.bufsize = (CDCE_FRAMES_MAX * MCLBYTES), 126 .mh.flags = {.pipe_bof = 1,.short_frames_ok = 1,.short_xfer_ok = 1,.ext_buffer = 1,}, 127 .mh.callback = cdce_bulk_read_callback, 128 .mh.timeout = 0, /* no timeout */ 129 /* Device Mode */ 130 .md.frames = CDCE_FRAMES_MAX, 131 .md.bufsize = (CDCE_FRAMES_MAX * MCLBYTES), 132 .md.flags = {.pipe_bof = 1,.force_short_xfer = 1,.ext_buffer = 1,}, 133 .md.callback = cdce_bulk_write_callback, 134 .md.timeout = 10000, /* 10 seconds */ 135 }, 136 137 [CDCE_INTR] = { 138 .type = UE_INTERRUPT, 139 .endpoint = UE_ADDR_ANY, 140 .direction = UE_DIR_IN, 141 .if_index = 1, 142 /* Host Mode */ 143 .mh.bufsize = CDCE_IND_SIZE_MAX, 144 .mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,.no_pipe_ok = 1,}, 145 .mh.callback = cdce_intr_read_callback, 146 .mh.timeout = 0, 147 /* Device Mode */ 148 .md.bufsize = CDCE_IND_SIZE_MAX, 149 .md.flags = {.pipe_bof = 1,.force_short_xfer = 1,.no_pipe_ok = 1,}, 150 .md.callback = cdce_intr_write_callback, 151 .md.timeout = 10000, /* 10 seconds */ 152 }, 153}; 154 155static device_method_t cdce_methods[] = { 156 /* USB interface */ 157 DEVMETHOD(usb_handle_request, cdce_handle_request), 158 159 /* Device interface */ 160 DEVMETHOD(device_probe, cdce_probe), 161 DEVMETHOD(device_attach, cdce_attach), 162 DEVMETHOD(device_detach, cdce_detach), 163 DEVMETHOD(device_suspend, cdce_suspend), 164 DEVMETHOD(device_resume, cdce_resume), 165 DEVMETHOD(device_shutdown, cdce_shutdown), 166 167 {0, 0} 168}; 169 170static driver_t cdce_driver = { 171 .name = "cdce", 172 .methods = cdce_methods, 173 .size = sizeof(struct cdce_softc), 174}; 175 176static devclass_t cdce_devclass; 177 178DRIVER_MODULE(cdce, uhub, cdce_driver, cdce_devclass, NULL, 0); 179MODULE_VERSION(cdce, 1); 180MODULE_DEPEND(cdce, uether, 1, 1, 1); 181MODULE_DEPEND(cdce, usb, 1, 1, 1); 182MODULE_DEPEND(cdce, ether, 1, 1, 1); 183 184static const struct usb2_ether_methods cdce_ue_methods = { 185 .ue_attach_post = cdce_attach_post, 186 .ue_start = cdce_start, 187 .ue_init = cdce_init, 188 .ue_stop = cdce_stop, 189 .ue_setmulti = cdce_setmulti, 190 .ue_setpromisc = cdce_setpromisc, 191}; 192 193static const struct usb2_device_id cdce_devs[] = { 194 {USB_IF_CSI(UICLASS_CDC, UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL, 0)}, 195 {USB_IF_CSI(UICLASS_CDC, UISUBCLASS_MOBILE_DIRECT_LINE_MODEL, 0)}, 196 197 {USB_VPI(USB_VENDOR_ACERLABS, USB_PRODUCT_ACERLABS_M5632, CDCE_FLAG_NO_UNION)}, 198 {USB_VPI(USB_VENDOR_AMBIT, USB_PRODUCT_AMBIT_NTL_250, CDCE_FLAG_NO_UNION)}, 199 {USB_VPI(USB_VENDOR_COMPAQ, USB_PRODUCT_COMPAQ_IPAQLINUX, CDCE_FLAG_NO_UNION)}, 200 {USB_VPI(USB_VENDOR_GMATE, USB_PRODUCT_GMATE_YP3X00, CDCE_FLAG_NO_UNION)}, 201 {USB_VPI(USB_VENDOR_MOTOROLA2, USB_PRODUCT_MOTOROLA2_USBLAN, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)}, 202 {USB_VPI(USB_VENDOR_MOTOROLA2, USB_PRODUCT_MOTOROLA2_USBLAN2, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)}, 203 {USB_VPI(USB_VENDOR_NETCHIP, USB_PRODUCT_NETCHIP_ETHERNETGADGET, CDCE_FLAG_NO_UNION)}, 204 {USB_VPI(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2501, CDCE_FLAG_NO_UNION)}, 205 {USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SL5500, CDCE_FLAG_ZAURUS)}, 206 {USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SL5600, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)}, 207 {USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SLA300, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)}, 208 {USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SLC700, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)}, 209 {USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SLC750, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)}, 210}; 211 212static int 213cdce_probe(device_t dev) 214{ 215 struct usb2_attach_arg *uaa = device_get_ivars(dev); 216 217 return (usb2_lookup_id_by_uaa(cdce_devs, sizeof(cdce_devs), uaa)); 218} 219 220static void 221cdce_attach_post(struct usb2_ether *ue) 222{ 223 /* no-op */ 224 return; 225} 226 227static int 228cdce_attach(device_t dev) 229{ 230 struct cdce_softc *sc = device_get_softc(dev); 231 struct usb2_ether *ue = &sc->sc_ue; 232 struct usb2_attach_arg *uaa = device_get_ivars(dev); 233 struct usb2_interface *iface; 234 const struct usb2_cdc_union_descriptor *ud; 235 const struct usb2_interface_descriptor *id; 236 const struct usb2_cdc_ethernet_descriptor *ued; 237 int error; 238 uint8_t i; 239 char eaddr_str[5 * ETHER_ADDR_LEN]; /* approx */ 240 241 sc->sc_flags = USB_GET_DRIVER_INFO(uaa); 242 243 device_set_usb2_desc(dev); 244 245 mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF); 246 247 if (sc->sc_flags & CDCE_FLAG_NO_UNION) { 248 sc->sc_ifaces_index[0] = uaa->info.bIfaceIndex; 249 sc->sc_ifaces_index[1] = uaa->info.bIfaceIndex; 250 sc->sc_data_iface_no = 0; /* not used */ 251 goto alloc_transfers; 252 } 253 ud = usb2_find_descriptor 254 (uaa->device, NULL, uaa->info.bIfaceIndex, 255 UDESC_CS_INTERFACE, 0 - 1, UDESCSUB_CDC_UNION, 0 - 1); 256 257 if ((ud == NULL) || (ud->bLength < sizeof(*ud))) { 258 device_printf(dev, "no union descriptor!\n"); 259 goto detach; 260 } 261 sc->sc_data_iface_no = ud->bSlaveInterface[0]; 262 263 for (i = 0;; i++) { 264 265 iface = usb2_get_iface(uaa->device, i); 266 267 if (iface) { 268 269 id = usb2_get_interface_descriptor(iface); 270 271 if (id && (id->bInterfaceNumber == 272 sc->sc_data_iface_no)) { 273 sc->sc_ifaces_index[0] = i; 274 sc->sc_ifaces_index[1] = uaa->info.bIfaceIndex; 275 usb2_set_parent_iface(uaa->device, i, uaa->info.bIfaceIndex); 276 break; 277 } 278 } else { 279 device_printf(dev, "no data interface found!\n"); 280 goto detach; 281 } 282 } 283 284 /* 285 * <quote> 286 * 287 * The Data Class interface of a networking device shall have 288 * a minimum of two interface settings. The first setting 289 * (the default interface setting) includes no endpoints and 290 * therefore no networking traffic is exchanged whenever the 291 * default interface setting is selected. One or more 292 * additional interface settings are used for normal 293 * operation, and therefore each includes a pair of endpoints 294 * (one IN, and one OUT) to exchange network traffic. Select 295 * an alternate interface setting to initialize the network 296 * aspects of the device and to enable the exchange of 297 * network traffic. 298 * 299 * </quote> 300 * 301 * Some devices, most notably cable modems, include interface 302 * settings that have no IN or OUT endpoint, therefore loop 303 * through the list of all available interface settings 304 * looking for one with both IN and OUT endpoints. 305 */ 306 307alloc_transfers: 308 309 for (i = 0; i != 32; i++) { 310 311 error = usb2_set_alt_interface_index 312 (uaa->device, sc->sc_ifaces_index[0], i); 313 314 if (error) { 315 device_printf(dev, "no valid alternate " 316 "setting found!\n"); 317 goto detach; 318 } 319 error = usb2_transfer_setup 320 (uaa->device, sc->sc_ifaces_index, 321 sc->sc_xfer, cdce_config, CDCE_N_TRANSFER, 322 sc, &sc->sc_mtx); 323 324 if (error == 0) { 325 break; 326 } 327 } 328 329 ued = usb2_find_descriptor 330 (uaa->device, NULL, uaa->info.bIfaceIndex, 331 UDESC_CS_INTERFACE, 0 - 1, UDESCSUB_CDC_ENF, 0 - 1); 332 333 if ((ued == NULL) || (ued->bLength < sizeof(*ued))) { 334 error = USB_ERR_INVAL; 335 } else { 336 error = usb2_req_get_string_any(uaa->device, NULL, 337 eaddr_str, sizeof(eaddr_str), ued->iMacAddress); 338 } 339 340 if (error) { 341 342 /* fake MAC address */ 343 344 device_printf(dev, "faking MAC address\n"); 345 sc->sc_ue.ue_eaddr[0] = 0x2a; 346 memcpy(&sc->sc_ue.ue_eaddr[1], &ticks, sizeof(uint32_t)); 347 sc->sc_ue.ue_eaddr[5] = device_get_unit(dev); 348 349 } else { 350 351 bzero(sc->sc_ue.ue_eaddr, sizeof(sc->sc_ue.ue_eaddr)); 352 353 for (i = 0; i != (ETHER_ADDR_LEN * 2); i++) { 354 355 char c = eaddr_str[i]; 356 357 if ('0' <= c && c <= '9') 358 c -= '0'; 359 else if (c != 0) 360 c -= 'A' - 10; 361 else 362 break; 363 364 c &= 0xf; 365 366 if ((i & 1) == 0) 367 c <<= 4; 368 sc->sc_ue.ue_eaddr[i / 2] |= c; 369 } 370 371 if (uaa->usb2_mode == USB_MODE_DEVICE) { 372 /* 373 * Do not use the same MAC address like the peer ! 374 */ 375 sc->sc_ue.ue_eaddr[5] ^= 0xFF; 376 } 377 } 378 379 ue->ue_sc = sc; 380 ue->ue_dev = dev; 381 ue->ue_udev = uaa->device; 382 ue->ue_mtx = &sc->sc_mtx; 383 ue->ue_methods = &cdce_ue_methods; 384 385 error = usb2_ether_ifattach(ue); 386 if (error) { 387 device_printf(dev, "could not attach interface\n"); 388 goto detach; 389 } 390 return (0); /* success */ 391 392detach: 393 cdce_detach(dev); 394 return (ENXIO); /* failure */ 395} 396 397static int 398cdce_detach(device_t dev) 399{ 400 struct cdce_softc *sc = device_get_softc(dev); 401 struct usb2_ether *ue = &sc->sc_ue; 402 403 /* stop all USB transfers first */ 404 usb2_transfer_unsetup(sc->sc_xfer, CDCE_N_TRANSFER); 405 usb2_ether_ifdetach(ue); 406 mtx_destroy(&sc->sc_mtx); 407 408 return (0); 409} 410 411static void 412cdce_start(struct usb2_ether *ue) 413{ 414 struct cdce_softc *sc = usb2_ether_getsc(ue); 415 416 /* 417 * Start the USB transfers, if not already started: 418 */ 419 usb2_transfer_start(sc->sc_xfer[CDCE_BULK_B]); 420 usb2_transfer_start(sc->sc_xfer[CDCE_BULK_A]); 421} 422 423static void 424cdce_free_queue(struct mbuf **ppm, uint8_t n) 425{ 426 uint8_t x; 427 for (x = 0; x != n; x++) { 428 if (ppm[x] != NULL) { 429 m_freem(ppm[x]); 430 ppm[x] = NULL; 431 } 432 } 433} 434 435static void 436cdce_bulk_write_callback(struct usb2_xfer *xfer) 437{ 438 struct cdce_softc *sc = xfer->priv_sc; 439 struct ifnet *ifp = usb2_ether_getifp(&sc->sc_ue); 440 struct mbuf *m; 441 struct mbuf *mt; 442 uint32_t crc; 443 uint8_t x; 444 445 DPRINTFN(1, "\n"); 446 447 switch (USB_GET_STATE(xfer)) { 448 case USB_ST_TRANSFERRED: 449 DPRINTFN(11, "transfer complete: " 450 "%u bytes in %u frames\n", xfer->actlen, 451 xfer->aframes); 452 453 ifp->if_opackets++; 454 455 /* free all previous TX buffers */ 456 cdce_free_queue(sc->sc_tx_buf, CDCE_FRAMES_MAX); 457 458 /* FALLTHROUGH */ 459 case USB_ST_SETUP: 460tr_setup: 461 for (x = 0; x != CDCE_FRAMES_MAX; x++) { 462 463 IFQ_DRV_DEQUEUE(&ifp->if_snd, m); 464 465 if (m == NULL) 466 break; 467 468 if (sc->sc_flags & CDCE_FLAG_ZAURUS) { 469 /* 470 * Zaurus wants a 32-bit CRC appended 471 * to every frame 472 */ 473 474 crc = cdce_m_crc32(m, 0, m->m_pkthdr.len); 475 crc = htole32(crc); 476 477 if (!m_append(m, 4, (void *)&crc)) { 478 m_freem(m); 479 ifp->if_oerrors++; 480 continue; 481 } 482 } 483 if (m->m_len != m->m_pkthdr.len) { 484 mt = m_defrag(m, M_DONTWAIT); 485 if (mt == NULL) { 486 m_freem(m); 487 ifp->if_oerrors++; 488 continue; 489 } 490 m = mt; 491 } 492 if (m->m_pkthdr.len > MCLBYTES) { 493 m->m_pkthdr.len = MCLBYTES; 494 } 495 sc->sc_tx_buf[x] = m; 496 xfer->frlengths[x] = m->m_len; 497 usb2_set_frame_data(xfer, m->m_data, x); 498 499 /* 500 * If there's a BPF listener, bounce a copy of 501 * this frame to him: 502 */ 503 BPF_MTAP(ifp, m); 504 } 505 if (x != 0) { 506 xfer->nframes = x; 507 usb2_start_hardware(xfer); 508 } 509 break; 510 511 default: /* Error */ 512 DPRINTFN(11, "transfer error, %s\n", 513 usb2_errstr(xfer->error)); 514 515 /* free all previous TX buffers */ 516 cdce_free_queue(sc->sc_tx_buf, CDCE_FRAMES_MAX); 517 518 /* count output errors */ 519 ifp->if_oerrors++; 520 521 if (xfer->error != USB_ERR_CANCELLED) { 522 /* try to clear stall first */ 523 xfer->flags.stall_pipe = 1; 524 goto tr_setup; 525 } 526 break; 527 } 528} 529 530static int32_t 531cdce_m_crc32_cb(void *arg, void *src, uint32_t count) 532{ 533 uint32_t *p_crc = arg; 534 535 *p_crc = crc32_raw(src, count, *p_crc); 536 return (0); 537} 538 539static uint32_t 540cdce_m_crc32(struct mbuf *m, uint32_t src_offset, uint32_t src_len) 541{ 542 uint32_t crc = 0xFFFFFFFF; 543 int error; 544 545 error = m_apply(m, src_offset, src_len, cdce_m_crc32_cb, &crc); 546 return (crc ^ 0xFFFFFFFF); 547} 548 549static void 550cdce_init(struct usb2_ether *ue) 551{ 552 struct cdce_softc *sc = usb2_ether_getsc(ue); 553 struct ifnet *ifp = usb2_ether_getifp(ue); 554 555 CDCE_LOCK_ASSERT(sc, MA_OWNED); 556 557 ifp->if_drv_flags |= IFF_DRV_RUNNING; 558 559 /* start interrupt transfer */ 560 usb2_transfer_start(sc->sc_xfer[CDCE_INTR]); 561 562 /* stall data write direction, which depends on USB mode */ 563 if (usb2_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST) 564 usb2_transfer_set_stall(sc->sc_xfer[CDCE_BULK_A]); 565 else 566 usb2_transfer_set_stall(sc->sc_xfer[CDCE_BULK_B]); 567 568 /* start data transfers */ 569 cdce_start(ue); 570} 571 572static void 573cdce_stop(struct usb2_ether *ue) 574{ 575 struct cdce_softc *sc = usb2_ether_getsc(ue); 576 struct ifnet *ifp = usb2_ether_getifp(ue); 577 578 CDCE_LOCK_ASSERT(sc, MA_OWNED); 579 580 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 581 582 /* 583 * stop all the transfers, if not already stopped: 584 */ 585 usb2_transfer_stop(sc->sc_xfer[CDCE_BULK_A]); 586 usb2_transfer_stop(sc->sc_xfer[CDCE_BULK_B]); 587 usb2_transfer_stop(sc->sc_xfer[CDCE_INTR]); 588} 589 590static void 591cdce_setmulti(struct usb2_ether *ue) 592{ 593 /* no-op */ 594 return; 595} 596 597static void 598cdce_setpromisc(struct usb2_ether *ue) 599{ 600 /* no-op */ 601 return; 602} 603 604static int 605cdce_shutdown(device_t dev) 606{ 607 struct cdce_softc *sc = device_get_softc(dev); 608 609 usb2_ether_ifshutdown(&sc->sc_ue); 610 611 return (0); 612} 613 614static int 615cdce_suspend(device_t dev) 616{ 617 device_printf(dev, "Suspending\n"); 618 return (0); 619} 620 621static int 622cdce_resume(device_t dev) 623{ 624 device_printf(dev, "Resuming\n"); 625 return (0); 626} 627 628static void 629cdce_bulk_read_callback(struct usb2_xfer *xfer) 630{ 631 struct cdce_softc *sc = xfer->priv_sc; 632 struct mbuf *m; 633 uint8_t x; 634 635 switch (USB_GET_STATE(xfer)) { 636 case USB_ST_TRANSFERRED: 637 638 DPRINTF("received %u bytes in %u frames\n", 639 xfer->actlen, xfer->aframes); 640 641 for (x = 0; x != xfer->aframes; x++) { 642 643 m = sc->sc_rx_buf[x]; 644 sc->sc_rx_buf[x] = NULL; 645 646 /* Strip off CRC added by Zaurus, if any */ 647 if ((sc->sc_flags & CDCE_FLAG_ZAURUS) && 648 (xfer->frlengths[x] >= 14)) 649 xfer->frlengths[x] -= 4; 650 651 if (xfer->frlengths[x] < sizeof(struct ether_header)) { 652 m_freem(m); 653 continue; 654 } 655 /* queue up mbuf */ 656 usb2_ether_rxmbuf(&sc->sc_ue, m, xfer->frlengths[x]); 657 } 658 659 /* FALLTHROUGH */ 660 case USB_ST_SETUP: 661 /* 662 * TODO: Implement support for multi frame transfers, 663 * when the USB hardware supports it. 664 */ 665 for (x = 0; x != 1; x++) { 666 if (sc->sc_rx_buf[x] == NULL) { 667 m = usb2_ether_newbuf(); 668 if (m == NULL) 669 goto tr_stall; 670 sc->sc_rx_buf[x] = m; 671 } else { 672 m = sc->sc_rx_buf[x]; 673 } 674 675 usb2_set_frame_data(xfer, m->m_data, x); 676 xfer->frlengths[x] = m->m_len; 677 } 678 /* set number of frames and start hardware */ 679 xfer->nframes = x; 680 usb2_start_hardware(xfer); 681 /* flush any received frames */ 682 usb2_ether_rxflush(&sc->sc_ue); 683 break; 684 685 default: /* Error */ 686 DPRINTF("error = %s\n", 687 usb2_errstr(xfer->error)); 688 689 if (xfer->error != USB_ERR_CANCELLED) { 690tr_stall: 691 /* try to clear stall first */ 692 xfer->flags.stall_pipe = 1; 693 xfer->nframes = 0; 694 usb2_start_hardware(xfer); 695 break; 696 } 697 698 /* need to free the RX-mbufs when we are cancelled */ 699 cdce_free_queue(sc->sc_rx_buf, CDCE_FRAMES_MAX); 700 break; 701 } 702} 703 704static void 705cdce_intr_read_callback(struct usb2_xfer *xfer) 706{ 707 ; /* style fix */ 708 switch (USB_GET_STATE(xfer)) { 709 case USB_ST_TRANSFERRED: 710 711 DPRINTF("Received %d bytes\n", 712 xfer->actlen); 713 714 /* TODO: decode some indications */ 715 716 /* FALLTHROUGH */ 717 case USB_ST_SETUP: 718tr_setup: 719 xfer->frlengths[0] = xfer->max_data_length; 720 usb2_start_hardware(xfer); 721 break; 722 723 default: /* Error */ 724 if (xfer->error != USB_ERR_CANCELLED) { 725 /* start clear stall */ 726 xfer->flags.stall_pipe = 1; 727 goto tr_setup; 728 } 729 break; 730 } 731} 732 733static void 734cdce_intr_write_callback(struct usb2_xfer *xfer) 735{ 736 ; /* style fix */ 737 switch (USB_GET_STATE(xfer)) { 738 case USB_ST_TRANSFERRED: 739 740 DPRINTF("Transferred %d bytes\n", xfer->actlen); 741 742 /* FALLTHROUGH */ 743 case USB_ST_SETUP: 744tr_setup: 745#if 0 746 xfer->frlengths[0] = XXX; 747 usb2_start_hardware(xfer); 748#endif 749 break; 750 751 default: /* Error */ 752 if (xfer->error != USB_ERR_CANCELLED) { 753 /* start clear stall */ 754 xfer->flags.stall_pipe = 1; 755 goto tr_setup; 756 } 757 break; 758 } 759} 760 761static int 762cdce_handle_request(device_t dev, 763 const void *req, void **pptr, uint16_t *plen, 764 uint16_t offset, uint8_t is_complete) 765{ 766 return (ENXIO); /* use builtin handler */ 767}
|