if_cue.c revision 186454
1/*- 2 * Copyright (c) 1997, 1998, 1999, 2000 3 * Bill Paul <wpaul@ee.columbia.edu>. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by Bill Paul. 16 * 4. Neither the name of the author nor the names of any co-contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 30 * THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33#include <sys/cdefs.h> 34__FBSDID("$FreeBSD: head/sys/dev/usb2/ethernet/if_cue2.c 186454 2008-12-23 19:59:21Z thompsa $"); 35 36/* 37 * CATC USB-EL1210A USB to ethernet driver. Used in the CATC Netmate 38 * adapters and others. 39 * 40 * Written by Bill Paul <wpaul@ee.columbia.edu> 41 * Electrical Engineering Department 42 * Columbia University, New York City 43 */ 44 45/* 46 * The CATC USB-EL1210A provides USB ethernet support at 10Mbps. The 47 * RX filter uses a 512-bit multicast hash table, single perfect entry 48 * for the station address, and promiscuous mode. Unlike the ADMtek 49 * and KLSI chips, the CATC ASIC supports read and write combining 50 * mode where multiple packets can be transfered using a single bulk 51 * transaction, which helps performance a great deal. 52 */ 53 54/* 55 * NOTE: all function names beginning like "cue_cfg_" can only 56 * be called from within the config thread function ! 57 */ 58 59#include <dev/usb2/include/usb2_devid.h> 60#include <dev/usb2/include/usb2_standard.h> 61#include <dev/usb2/include/usb2_mfunc.h> 62#include <dev/usb2/include/usb2_error.h> 63 64#define usb2_config_td_cc usb2_ether_cc 65#define usb2_config_td_softc cue_softc 66 67#define USB_DEBUG_VAR cue_debug 68 69#include <dev/usb2/core/usb2_core.h> 70#include <dev/usb2/core/usb2_lookup.h> 71#include <dev/usb2/core/usb2_process.h> 72#include <dev/usb2/core/usb2_config_td.h> 73#include <dev/usb2/core/usb2_debug.h> 74#include <dev/usb2/core/usb2_request.h> 75#include <dev/usb2/core/usb2_busdma.h> 76#include <dev/usb2/core/usb2_util.h> 77 78#include <dev/usb2/ethernet/usb2_ethernet.h> 79#include <dev/usb2/ethernet/if_cue2_reg.h> 80 81/* 82 * Various supported device vendors/products. 83 */ 84 85/* Belkin F5U111 adapter covered by NETMATE entry */ 86 87static const struct usb2_device_id cue_devs[] = { 88 {USB_VPI(USB_VENDOR_CATC, USB_PRODUCT_CATC_NETMATE, 0)}, 89 {USB_VPI(USB_VENDOR_CATC, USB_PRODUCT_CATC_NETMATE2, 0)}, 90 {USB_VPI(USB_VENDOR_SMARTBRIDGES, USB_PRODUCT_SMARTBRIDGES_SMARTLINK, 0)}, 91}; 92 93/* prototypes */ 94 95static device_probe_t cue_probe; 96static device_attach_t cue_attach; 97static device_detach_t cue_detach; 98static device_shutdown_t cue_shutdown; 99 100static usb2_callback_t cue_bulk_read_clear_stall_callback; 101static usb2_callback_t cue_bulk_read_callback; 102static usb2_callback_t cue_bulk_write_clear_stall_callback; 103static usb2_callback_t cue_bulk_write_callback; 104 105static usb2_config_td_command_t cue_cfg_promisc_upd; 106static usb2_config_td_command_t cue_config_copy; 107static usb2_config_td_command_t cue_cfg_first_time_setup; 108static usb2_config_td_command_t cue_cfg_tick; 109static usb2_config_td_command_t cue_cfg_pre_init; 110static usb2_config_td_command_t cue_cfg_init; 111static usb2_config_td_command_t cue_cfg_pre_stop; 112static usb2_config_td_command_t cue_cfg_stop; 113 114static void cue_cfg_do_request(struct cue_softc *, 115 struct usb2_device_request *, void *); 116static uint8_t cue_cfg_csr_read_1(struct cue_softc *, uint16_t); 117static uint16_t cue_cfg_csr_read_2(struct cue_softc *, uint8_t); 118static void cue_cfg_csr_write_1(struct cue_softc *, uint16_t, uint16_t); 119static void cue_cfg_mem(struct cue_softc *, uint8_t, uint16_t, void *, 120 uint16_t); 121static void cue_cfg_getmac(struct cue_softc *, void *); 122static void cue_mchash(struct usb2_config_td_cc *, const uint8_t *); 123static void cue_cfg_reset(struct cue_softc *); 124static void cue_start_cb(struct ifnet *); 125static void cue_start_transfers(struct cue_softc *); 126static void cue_init_cb(void *); 127static int cue_ioctl_cb(struct ifnet *, u_long, caddr_t); 128static void cue_watchdog(void *); 129 130#if USB_DEBUG 131static int cue_debug = 0; 132 133SYSCTL_NODE(_hw_usb2, OID_AUTO, cue, CTLFLAG_RW, 0, "USB cue"); 134SYSCTL_INT(_hw_usb2_cue, OID_AUTO, debug, CTLFLAG_RW, &cue_debug, 0, 135 "Debug level"); 136#endif 137 138static const struct usb2_config cue_config[CUE_ENDPT_MAX] = { 139 140 [0] = { 141 .type = UE_BULK, 142 .endpoint = UE_ADDR_ANY, 143 .direction = UE_DIR_OUT, 144 .mh.bufsize = (MCLBYTES + 2), 145 .mh.flags = {.pipe_bof = 1,}, 146 .mh.callback = &cue_bulk_write_callback, 147 .mh.timeout = 10000, /* 10 seconds */ 148 }, 149 150 [1] = { 151 .type = UE_BULK, 152 .endpoint = UE_ADDR_ANY, 153 .direction = UE_DIR_IN, 154 .mh.bufsize = (MCLBYTES + 2), 155 .mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 156 .mh.callback = &cue_bulk_read_callback, 157 }, 158 159 [2] = { 160 .type = UE_CONTROL, 161 .endpoint = 0x00, /* Control pipe */ 162 .direction = UE_DIR_ANY, 163 .mh.bufsize = sizeof(struct usb2_device_request), 164 .mh.flags = {}, 165 .mh.callback = &cue_bulk_write_clear_stall_callback, 166 .mh.timeout = 1000, /* 1 second */ 167 .mh.interval = 50, /* 50ms */ 168 }, 169 170 [3] = { 171 .type = UE_CONTROL, 172 .endpoint = 0x00, /* Control pipe */ 173 .direction = UE_DIR_ANY, 174 .mh.bufsize = sizeof(struct usb2_device_request), 175 .mh.flags = {}, 176 .mh.callback = &cue_bulk_read_clear_stall_callback, 177 .mh.timeout = 1000, /* 1 second */ 178 .mh.interval = 50, /* 50ms */ 179 }, 180}; 181 182static device_method_t cue_methods[] = { 183 /* Device interface */ 184 DEVMETHOD(device_probe, cue_probe), 185 DEVMETHOD(device_attach, cue_attach), 186 DEVMETHOD(device_detach, cue_detach), 187 DEVMETHOD(device_shutdown, cue_shutdown), 188 189 {0, 0} 190}; 191 192static driver_t cue_driver = { 193 .name = "cue", 194 .methods = cue_methods, 195 .size = sizeof(struct cue_softc), 196}; 197 198static devclass_t cue_devclass; 199 200DRIVER_MODULE(cue, ushub, cue_driver, cue_devclass, NULL, 0); 201MODULE_DEPEND(cue, usb2_ethernet, 1, 1, 1); 202MODULE_DEPEND(cue, usb2_core, 1, 1, 1); 203MODULE_DEPEND(cue, ether, 1, 1, 1); 204 205static void 206cue_cfg_do_request(struct cue_softc *sc, struct usb2_device_request *req, 207 void *data) 208{ 209 uint16_t length; 210 usb2_error_t err; 211 212 if (usb2_config_td_is_gone(&sc->sc_config_td)) { 213 goto error; 214 } 215 err = usb2_do_request_flags 216 (sc->sc_udev, &sc->sc_mtx, req, data, 0, NULL, 1000); 217 218 if (err) { 219 220 DPRINTF("device request failed, err=%s " 221 "(ignored)\n", usb2_errstr(err)); 222 223error: 224 length = UGETW(req->wLength); 225 226 if ((req->bmRequestType & UT_READ) && length) { 227 bzero(data, length); 228 } 229 } 230} 231 232#define CUE_CFG_SETBIT(sc, reg, x) \ 233 cue_cfg_csr_write_1(sc, reg, cue_cfg_csr_read_1(sc, reg) | (x)) 234 235#define CUE_CFG_CLRBIT(sc, reg, x) \ 236 cue_cfg_csr_write_1(sc, reg, cue_cfg_csr_read_1(sc, reg) & ~(x)) 237 238static uint8_t 239cue_cfg_csr_read_1(struct cue_softc *sc, uint16_t reg) 240{ 241 struct usb2_device_request req; 242 uint8_t val; 243 244 req.bmRequestType = UT_READ_VENDOR_DEVICE; 245 req.bRequest = CUE_CMD_READREG; 246 USETW(req.wValue, 0); 247 USETW(req.wIndex, reg); 248 USETW(req.wLength, 1); 249 250 cue_cfg_do_request(sc, &req, &val); 251 return (val); 252} 253 254static uint16_t 255cue_cfg_csr_read_2(struct cue_softc *sc, uint8_t reg) 256{ 257 struct usb2_device_request req; 258 uint16_t val; 259 260 req.bmRequestType = UT_READ_VENDOR_DEVICE; 261 req.bRequest = CUE_CMD_READREG; 262 USETW(req.wValue, 0); 263 USETW(req.wIndex, reg); 264 USETW(req.wLength, 2); 265 266 cue_cfg_do_request(sc, &req, &val); 267 return (le16toh(val)); 268} 269 270static void 271cue_cfg_csr_write_1(struct cue_softc *sc, uint16_t reg, uint16_t val) 272{ 273 struct usb2_device_request req; 274 275 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 276 req.bRequest = CUE_CMD_WRITEREG; 277 USETW(req.wValue, val); 278 USETW(req.wIndex, reg); 279 USETW(req.wLength, 0); 280 281 cue_cfg_do_request(sc, &req, NULL); 282} 283 284static void 285cue_cfg_mem(struct cue_softc *sc, uint8_t cmd, uint16_t addr, 286 void *buf, uint16_t len) 287{ 288 struct usb2_device_request req; 289 290 if (cmd == CUE_CMD_READSRAM) { 291 req.bmRequestType = UT_READ_VENDOR_DEVICE; 292 } else { 293 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 294 } 295 req.bRequest = cmd; 296 USETW(req.wValue, 0); 297 USETW(req.wIndex, addr); 298 USETW(req.wLength, len); 299 300 cue_cfg_do_request(sc, &req, buf); 301} 302 303static void 304cue_cfg_getmac(struct cue_softc *sc, void *buf) 305{ 306 struct usb2_device_request req; 307 308 req.bmRequestType = UT_READ_VENDOR_DEVICE; 309 req.bRequest = CUE_CMD_GET_MACADDR; 310 USETW(req.wValue, 0); 311 USETW(req.wIndex, 0); 312 USETW(req.wLength, ETHER_ADDR_LEN); 313 314 cue_cfg_do_request(sc, &req, buf); 315} 316 317#define CUE_BITS 9 318 319static void 320cue_mchash(struct usb2_config_td_cc *cc, const uint8_t *addr) 321{ 322 uint16_t h; 323 324 h = ether_crc32_le(addr, ETHER_ADDR_LEN) & 325 ((1 << CUE_BITS) - 1); 326 cc->if_hash[h >> 3] |= 1 << (h & 0x7); 327} 328 329static void 330cue_cfg_promisc_upd(struct cue_softc *sc, 331 struct usb2_config_td_cc *cc, uint16_t refcount) 332{ 333 /* if we want promiscuous mode, set the allframes bit */ 334 335 if (cc->if_flags & IFF_PROMISC) { 336 CUE_CFG_SETBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC); 337 } else { 338 CUE_CFG_CLRBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC); 339 } 340 341 /* write multicast hash-bits */ 342 343 cue_cfg_mem(sc, CUE_CMD_WRITESRAM, CUE_MCAST_TABLE_ADDR, 344 cc->if_hash, CUE_MCAST_TABLE_LEN); 345} 346 347static void 348cue_config_copy(struct cue_softc *sc, 349 struct usb2_config_td_cc *cc, uint16_t refcount) 350{ 351 bzero(cc, sizeof(*cc)); 352 usb2_ether_cc(sc->sc_ifp, &cue_mchash, cc); 353} 354 355static void 356cue_cfg_reset(struct cue_softc *sc) 357{ 358 struct usb2_device_request req; 359 360 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 361 req.bRequest = CUE_CMD_RESET; 362 USETW(req.wValue, 0); 363 USETW(req.wIndex, 0); 364 USETW(req.wLength, 0); 365 366 cue_cfg_do_request(sc, &req, NULL); 367 368 /* 369 * wait a little while for the chip to get its brains in order: 370 */ 371 372 (void)usb2_config_td_sleep(&sc->sc_config_td, hz / 100); 373} 374 375static int 376cue_probe(device_t dev) 377{ 378 struct usb2_attach_arg *uaa = device_get_ivars(dev); 379 380 if (uaa->usb2_mode != USB_MODE_HOST) { 381 return (ENXIO); 382 } 383 if (uaa->info.bConfigIndex != CUE_CONFIG_IDX) { 384 return (ENXIO); 385 } 386 if (uaa->info.bIfaceIndex != CUE_IFACE_IDX) { 387 return (ENXIO); 388 } 389 return (usb2_lookup_id_by_uaa(cue_devs, sizeof(cue_devs), uaa)); 390} 391 392static int 393cue_attach(device_t dev) 394{ 395 struct usb2_attach_arg *uaa = device_get_ivars(dev); 396 struct cue_softc *sc = device_get_softc(dev); 397 uint8_t iface_index; 398 int32_t error; 399 400 if (sc == NULL) { 401 return (ENOMEM); 402 } 403 sc->sc_udev = uaa->device; 404 sc->sc_dev = dev; 405 sc->sc_unit = device_get_unit(dev); 406 407 device_set_usb2_desc(dev); 408 409 mtx_init(&sc->sc_mtx, "cue lock", NULL, MTX_DEF | MTX_RECURSE); 410 411 usb2_callout_init_mtx(&sc->sc_watchdog, &sc->sc_mtx, 0); 412 413 iface_index = CUE_IFACE_IDX; 414 error = usb2_transfer_setup(uaa->device, &iface_index, 415 sc->sc_xfer, cue_config, CUE_ENDPT_MAX, sc, &sc->sc_mtx); 416 if (error) { 417 device_printf(dev, "allocating USB " 418 "transfers failed!\n"); 419 goto detach; 420 } 421 error = usb2_config_td_setup(&sc->sc_config_td, sc, &sc->sc_mtx, 422 NULL, sizeof(struct usb2_config_td_cc), 16); 423 if (error) { 424 device_printf(dev, "could not setup config " 425 "thread!\n"); 426 goto detach; 427 } 428 mtx_lock(&sc->sc_mtx); 429 430 /* start setup */ 431 432 usb2_config_td_queue_command 433 (&sc->sc_config_td, NULL, &cue_cfg_first_time_setup, 0, 0); 434 435 cue_watchdog(sc); 436 mtx_unlock(&sc->sc_mtx); 437 return (0); /* success */ 438 439detach: 440 cue_detach(dev); 441 return (ENXIO); /* failure */ 442} 443 444static void 445cue_cfg_first_time_setup(struct cue_softc *sc, 446 struct usb2_config_td_cc *cc, uint16_t refcount) 447{ 448 uint8_t eaddr[ETHER_ADDR_LEN]; 449 struct ifnet *ifp; 450 451#if 0 452 /* Reset the adapter. */ 453 cue_cfg_reset(sc); 454#endif 455 /* 456 * Get station address. 457 */ 458 cue_cfg_getmac(sc, eaddr); 459 460 mtx_unlock(&sc->sc_mtx); 461 462 ifp = if_alloc(IFT_ETHER); 463 464 mtx_lock(&sc->sc_mtx); 465 466 if (ifp == NULL) { 467 printf("cue%d: could not if_alloc()\n", 468 sc->sc_unit); 469 goto done; 470 } 471 sc->sc_evilhack = ifp; 472 473 ifp->if_softc = sc; 474 if_initname(ifp, "cue", sc->sc_unit); 475 ifp->if_mtu = ETHERMTU; 476 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 477 ifp->if_ioctl = cue_ioctl_cb; 478 ifp->if_start = cue_start_cb; 479 ifp->if_watchdog = NULL; 480 ifp->if_init = cue_init_cb; 481 ifp->if_baudrate = 10000000; 482 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN); 483 ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN; 484 IFQ_SET_READY(&ifp->if_snd); 485 486 sc->sc_ifp = ifp; 487 488 mtx_unlock(&sc->sc_mtx); 489 490 ether_ifattach(ifp, eaddr); 491 492 mtx_lock(&sc->sc_mtx); 493 494done: 495 return; 496} 497 498static int 499cue_detach(device_t dev) 500{ 501 struct cue_softc *sc = device_get_softc(dev); 502 struct ifnet *ifp; 503 504 usb2_config_td_drain(&sc->sc_config_td); 505 506 mtx_lock(&sc->sc_mtx); 507 508 usb2_callout_stop(&sc->sc_watchdog); 509 510 cue_cfg_pre_stop(sc, NULL, 0); 511 512 ifp = sc->sc_ifp; 513 514 mtx_unlock(&sc->sc_mtx); 515 516 /* stop all USB transfers first */ 517 usb2_transfer_unsetup(sc->sc_xfer, CUE_ENDPT_MAX); 518 519 /* get rid of any late children */ 520 bus_generic_detach(dev); 521 522 if (ifp) { 523 ether_ifdetach(ifp); 524 if_free(ifp); 525 } 526 usb2_config_td_unsetup(&sc->sc_config_td); 527 528 usb2_callout_drain(&sc->sc_watchdog); 529 530 mtx_destroy(&sc->sc_mtx); 531 532 return (0); 533} 534 535static void 536cue_bulk_read_clear_stall_callback(struct usb2_xfer *xfer) 537{ 538 struct cue_softc *sc = xfer->priv_sc; 539 struct usb2_xfer *xfer_other = sc->sc_xfer[1]; 540 541 if (usb2_clear_stall_callback(xfer, xfer_other)) { 542 DPRINTF("stall cleared\n"); 543 sc->sc_flags &= ~CUE_FLAG_READ_STALL; 544 usb2_transfer_start(xfer_other); 545 } 546} 547 548static void 549cue_bulk_read_callback(struct usb2_xfer *xfer) 550{ 551 struct cue_softc *sc = xfer->priv_sc; 552 struct ifnet *ifp = sc->sc_ifp; 553 struct mbuf *m = NULL; 554 uint8_t buf[2]; 555 uint16_t len; 556 557 switch (USB_GET_STATE(xfer)) { 558 case USB_ST_TRANSFERRED: 559 560 if (xfer->actlen <= (2 + sizeof(struct ether_header))) { 561 ifp->if_ierrors++; 562 goto tr_setup; 563 } 564 usb2_copy_out(xfer->frbuffers, 0, buf, 2); 565 566 len = buf[0] | (buf[1] << 8); 567 568 xfer->actlen -= 2; 569 570 m = usb2_ether_get_mbuf(); 571 572 if (m == NULL) { 573 ifp->if_ierrors++; 574 goto tr_setup; 575 } 576 xfer->actlen = min(xfer->actlen, m->m_len); 577 xfer->actlen = min(xfer->actlen, len); 578 579 usb2_copy_out(xfer->frbuffers, 2, m->m_data, xfer->actlen); 580 581 ifp->if_ipackets++; 582 m->m_pkthdr.rcvif = ifp; 583 m->m_pkthdr.len = m->m_len = xfer->actlen; 584 585 case USB_ST_SETUP: 586tr_setup: 587 588 if (sc->sc_flags & CUE_FLAG_READ_STALL) { 589 usb2_transfer_start(sc->sc_xfer[3]); 590 } else { 591 xfer->frlengths[0] = xfer->max_data_length; 592 usb2_start_hardware(xfer); 593 } 594 595 /* 596 * At the end of a USB callback it is always safe to unlock 597 * the private mutex of a device! That is why we do the 598 * "if_input" here, and not some lines up! 599 */ 600 if (m) { 601 mtx_unlock(&sc->sc_mtx); 602 (ifp->if_input) (ifp, m); 603 mtx_lock(&sc->sc_mtx); 604 } 605 return; 606 607 default: /* Error */ 608 if (xfer->error != USB_ERR_CANCELLED) { 609 /* try to clear stall first */ 610 sc->sc_flags |= CUE_FLAG_READ_STALL; 611 usb2_transfer_start(sc->sc_xfer[3]); 612 } 613 DPRINTF("bulk read error, %s\n", 614 usb2_errstr(xfer->error)); 615 return; 616 617 } 618} 619 620static void 621cue_cfg_tick(struct cue_softc *sc, 622 struct usb2_config_td_cc *cc, uint16_t refcount) 623{ 624 struct ifnet *ifp = sc->sc_ifp; 625 626 if ((ifp == NULL)) { 627 /* not ready */ 628 return; 629 } 630 ifp->if_collisions += cue_cfg_csr_read_2(sc, CUE_TX_SINGLECOLL); 631 ifp->if_collisions += cue_cfg_csr_read_2(sc, CUE_TX_MULTICOLL); 632 ifp->if_collisions += cue_cfg_csr_read_2(sc, CUE_TX_EXCESSCOLL); 633 634 if (cue_cfg_csr_read_2(sc, CUE_RX_FRAMEERR)) { 635 ifp->if_ierrors++; 636 } 637 /* start stopped transfers, if any */ 638 639 cue_start_transfers(sc); 640} 641 642static void 643cue_start_cb(struct ifnet *ifp) 644{ 645 struct cue_softc *sc = ifp->if_softc; 646 647 mtx_lock(&sc->sc_mtx); 648 649 cue_start_transfers(sc); 650 651 mtx_unlock(&sc->sc_mtx); 652} 653 654static void 655cue_start_transfers(struct cue_softc *sc) 656{ 657 if ((sc->sc_flags & CUE_FLAG_LL_READY) && 658 (sc->sc_flags & CUE_FLAG_HL_READY)) { 659 660 /* 661 * start the USB transfers, if not already started: 662 */ 663 usb2_transfer_start(sc->sc_xfer[1]); 664 usb2_transfer_start(sc->sc_xfer[0]); 665 } 666} 667 668static void 669cue_bulk_write_clear_stall_callback(struct usb2_xfer *xfer) 670{ 671 struct cue_softc *sc = xfer->priv_sc; 672 struct usb2_xfer *xfer_other = sc->sc_xfer[0]; 673 674 if (usb2_clear_stall_callback(xfer, xfer_other)) { 675 DPRINTF("stall cleared\n"); 676 sc->sc_flags &= ~CUE_FLAG_WRITE_STALL; 677 usb2_transfer_start(xfer_other); 678 } 679} 680 681static void 682cue_bulk_write_callback(struct usb2_xfer *xfer) 683{ 684 struct cue_softc *sc = xfer->priv_sc; 685 struct ifnet *ifp = sc->sc_ifp; 686 struct mbuf *m; 687 uint8_t buf[2]; 688 689 switch (USB_GET_STATE(xfer)) { 690 case USB_ST_TRANSFERRED: 691 DPRINTFN(11, "transfer complete\n"); 692 693 ifp->if_opackets++; 694 695 case USB_ST_SETUP: 696 697 if (sc->sc_flags & CUE_FLAG_WRITE_STALL) { 698 usb2_transfer_start(sc->sc_xfer[2]); 699 goto done; 700 } 701 IFQ_DRV_DEQUEUE(&ifp->if_snd, m); 702 703 if (m == NULL) { 704 goto done; 705 } 706 if (m->m_pkthdr.len > MCLBYTES) { 707 m->m_pkthdr.len = MCLBYTES; 708 } 709 xfer->frlengths[0] = (m->m_pkthdr.len + 2); 710 711 /* the first two bytes are the frame length */ 712 713 buf[0] = (uint8_t)(m->m_pkthdr.len); 714 buf[1] = (uint8_t)(m->m_pkthdr.len >> 8); 715 716 usb2_copy_in(xfer->frbuffers, 0, buf, 2); 717 718 usb2_m_copy_in(xfer->frbuffers, 2, 719 m, 0, m->m_pkthdr.len); 720 721 /* 722 * If there's a BPF listener, bounce a copy of this frame 723 * to him. 724 */ 725 BPF_MTAP(ifp, m); 726 727 m_freem(m); 728 729 usb2_start_hardware(xfer); 730 731done: 732 return; 733 734 default: /* Error */ 735 DPRINTFN(11, "transfer error, %s\n", 736 usb2_errstr(xfer->error)); 737 738 if (xfer->error != USB_ERR_CANCELLED) { 739 /* try to clear stall first */ 740 sc->sc_flags |= CUE_FLAG_WRITE_STALL; 741 usb2_transfer_start(sc->sc_xfer[2]); 742 } 743 ifp->if_oerrors++; 744 return; 745 746 } 747} 748 749static void 750cue_init_cb(void *arg) 751{ 752 struct cue_softc *sc = arg; 753 754 mtx_lock(&sc->sc_mtx); 755 usb2_config_td_queue_command 756 (&sc->sc_config_td, &cue_cfg_pre_init, 757 &cue_cfg_init, 0, 0); 758 mtx_unlock(&sc->sc_mtx); 759} 760 761static void 762cue_cfg_pre_init(struct cue_softc *sc, 763 struct usb2_config_td_cc *cc, uint16_t refcount) 764{ 765 struct ifnet *ifp = sc->sc_ifp; 766 767 /* immediate configuration */ 768 769 cue_cfg_pre_stop(sc, cc, 0); 770 771 ifp->if_drv_flags |= IFF_DRV_RUNNING; 772 773 sc->sc_flags |= CUE_FLAG_HL_READY; 774} 775 776static void 777cue_cfg_init(struct cue_softc *sc, 778 struct usb2_config_td_cc *cc, uint16_t refcount) 779{ 780 uint8_t i; 781 782 /* 783 * Cancel pending I/O and free all RX/TX buffers. 784 */ 785 cue_cfg_stop(sc, cc, 0); 786#if 0 787 cue_cfg_reset(sc); 788#endif 789 /* Set MAC address */ 790 791 for (i = 0; i < ETHER_ADDR_LEN; i++) { 792 cue_cfg_csr_write_1(sc, CUE_PAR0 - i, cc->if_lladdr[i]); 793 } 794 795 /* Enable RX logic. */ 796 cue_cfg_csr_write_1(sc, CUE_ETHCTL, CUE_ETHCTL_RX_ON | CUE_ETHCTL_MCAST_ON); 797 798 /* Load the multicast filter */ 799 cue_cfg_promisc_upd(sc, cc, 0); 800 801 /* 802 * Set the number of RX and TX buffers that we want 803 * to reserve inside the ASIC. 804 */ 805 cue_cfg_csr_write_1(sc, CUE_RX_BUFPKTS, CUE_RX_FRAMES); 806 cue_cfg_csr_write_1(sc, CUE_TX_BUFPKTS, CUE_TX_FRAMES); 807 808 /* Set advanced operation modes. */ 809 cue_cfg_csr_write_1(sc, CUE_ADVANCED_OPMODES, 810 CUE_AOP_EMBED_RXLEN | 0x01);/* 1 wait state */ 811 812 /* Program the LED operation. */ 813 cue_cfg_csr_write_1(sc, CUE_LEDCTL, CUE_LEDCTL_FOLLOW_LINK); 814 815 sc->sc_flags |= (CUE_FLAG_READ_STALL | 816 CUE_FLAG_WRITE_STALL | 817 CUE_FLAG_LL_READY); 818 819 cue_start_transfers(sc); 820} 821 822static int 823cue_ioctl_cb(struct ifnet *ifp, u_long command, caddr_t data) 824{ 825 struct cue_softc *sc = ifp->if_softc; 826 int error = 0; 827 828 switch (command) { 829 case SIOCSIFFLAGS: 830 mtx_lock(&sc->sc_mtx); 831 if (ifp->if_flags & IFF_UP) { 832 if (ifp->if_drv_flags & IFF_DRV_RUNNING) { 833 usb2_config_td_queue_command 834 (&sc->sc_config_td, &cue_config_copy, 835 &cue_cfg_promisc_upd, 0, 0); 836 } else { 837 usb2_config_td_queue_command 838 (&sc->sc_config_td, &cue_cfg_pre_init, 839 &cue_cfg_init, 0, 0); 840 } 841 } else { 842 if (ifp->if_drv_flags & IFF_DRV_RUNNING) { 843 usb2_config_td_queue_command 844 (&sc->sc_config_td, &cue_cfg_pre_stop, 845 &cue_cfg_stop, 0, 0); 846 } 847 } 848 mtx_unlock(&sc->sc_mtx); 849 break; 850 851 case SIOCADDMULTI: 852 case SIOCDELMULTI: 853 mtx_lock(&sc->sc_mtx); 854 usb2_config_td_queue_command 855 (&sc->sc_config_td, &cue_config_copy, 856 &cue_cfg_promisc_upd, 0, 0); 857 mtx_unlock(&sc->sc_mtx); 858 break; 859 860 default: 861 error = ether_ioctl(ifp, command, data); 862 break; 863 } 864 return (error); 865} 866 867static void 868cue_watchdog(void *arg) 869{ 870 struct cue_softc *sc = arg; 871 872 mtx_assert(&sc->sc_mtx, MA_OWNED); 873 874 usb2_config_td_queue_command 875 (&sc->sc_config_td, NULL, &cue_cfg_tick, 0, 0); 876 877 usb2_callout_reset(&sc->sc_watchdog, 878 hz, &cue_watchdog, sc); 879} 880 881/* 882 * Stop the adapter and free any mbufs allocated to the 883 * RX and TX lists. 884 */ 885static void 886cue_cfg_pre_stop(struct cue_softc *sc, 887 struct usb2_config_td_cc *cc, uint16_t refcount) 888{ 889 struct ifnet *ifp = sc->sc_ifp; 890 891 if (cc) { 892 /* copy the needed configuration */ 893 cue_config_copy(sc, cc, refcount); 894 } 895 /* immediate configuration */ 896 897 if (ifp) { 898 /* clear flags */ 899 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 900 } 901 sc->sc_flags &= ~(CUE_FLAG_HL_READY | 902 CUE_FLAG_LL_READY); 903 904 /* 905 * stop all the transfers, if not already stopped: 906 */ 907 usb2_transfer_stop(sc->sc_xfer[0]); 908 usb2_transfer_stop(sc->sc_xfer[1]); 909 usb2_transfer_stop(sc->sc_xfer[2]); 910 usb2_transfer_stop(sc->sc_xfer[3]); 911} 912 913static void 914cue_cfg_stop(struct cue_softc *sc, 915 struct usb2_config_td_cc *cc, uint16_t refcount) 916{ 917 cue_cfg_csr_write_1(sc, CUE_ETHCTL, 0); 918 cue_cfg_reset(sc); 919} 920 921/* 922 * Stop all chip I/O so that the kernel's probe routines don't 923 * get confused by errant DMAs when rebooting. 924 */ 925static int 926cue_shutdown(device_t dev) 927{ 928 struct cue_softc *sc = device_get_softc(dev); 929 930 mtx_lock(&sc->sc_mtx); 931 932 usb2_config_td_queue_command 933 (&sc->sc_config_td, &cue_cfg_pre_stop, 934 &cue_cfg_stop, 0, 0); 935 936 mtx_unlock(&sc->sc_mtx); 937 938 return (0); 939} 940