if_rue.c revision 188412
10Sduke/*- 22362Sohair * Copyright (c) 2001-2003, Shunsuke Akiyama <akiyama@FreeBSD.org>. 30Sduke * Copyright (c) 1997, 1998, 1999, 2000 Bill Paul <wpaul@ee.columbia.edu>. 40Sduke * All rights reserved. 50Sduke * 60Sduke * Redistribution and use in source and binary forms, with or without 70Sduke * modification, are permitted provided that the following conditions 80Sduke * are met: 90Sduke * 1. Redistributions of source code must retain the above copyright 100Sduke * notice, this list of conditions and the following disclaimer. 110Sduke * 2. Redistributions in binary form must reproduce the above copyright 120Sduke * notice, this list of conditions and the following disclaimer in the 130Sduke * documentation and/or other materials provided with the distribution. 140Sduke * 150Sduke * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 160Sduke * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 170Sduke * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 180Sduke * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 192362Sohair * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 202362Sohair * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 212362Sohair * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 220Sduke * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 230Sduke * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 240Sduke * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 250Sduke * SUCH DAMAGE. 260Sduke */ 2711822Sdarcy/*- 280Sduke * Copyright (c) 1997, 1998, 1999, 2000 290Sduke * Bill Paul <wpaul@ee.columbia.edu>. All rights reserved. 300Sduke * 310Sduke * Redistribution and use in source and binary forms, with or without 320Sduke * modification, are permitted provided that the following conditions 330Sduke * are met: 340Sduke * 1. Redistributions of source code must retain the above copyright 350Sduke * notice, this list of conditions and the following disclaimer. 360Sduke * 2. Redistributions in binary form must reproduce the above copyright 370Sduke * notice, this list of conditions and the following disclaimer in the 380Sduke * documentation and/or other materials provided with the distribution. 390Sduke * 3. All advertising materials mentioning features or use of this software 400Sduke * must display the following acknowledgement: 410Sduke * This product includes software developed by Bill Paul. 420Sduke * 4. Neither the name of the author nor the names of any co-contributors 430Sduke * may be used to endorse or promote products derived from this software 440Sduke * without specific prior written permission. 450Sduke * 460Sduke * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 470Sduke * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 480Sduke * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 490Sduke * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD 500Sduke * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 510Sduke * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 520Sduke * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 530Sduke * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 540Sduke * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 550Sduke * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 560Sduke * THE POSSIBILITY OF SUCH DAMAGE. 570Sduke */ 580Sduke 590Sduke#include <sys/cdefs.h> 600Sduke__FBSDID("$FreeBSD: head/sys/dev/usb2/ethernet/if_rue2.c 188412 2009-02-09 22:02:38Z thompsa $"); 610Sduke 620Sduke/* 630Sduke * RealTek RTL8150 USB to fast ethernet controller driver. 640Sduke * Datasheet is available from 650Sduke * ftp://ftp.realtek.com.tw/lancard/data_sheet/8150/. 660Sduke */ 670Sduke 680Sduke#include <dev/usb2/include/usb2_devid.h> 690Sduke#include <dev/usb2/include/usb2_standard.h> 700Sduke#include <dev/usb2/include/usb2_mfunc.h> 710Sduke#include <dev/usb2/include/usb2_error.h> 720Sduke 730Sduke#define USB_DEBUG_VAR rue_debug 740Sduke 750Sduke#include <dev/usb2/core/usb2_core.h> 760Sduke#include <dev/usb2/core/usb2_lookup.h> 770Sduke#include <dev/usb2/core/usb2_process.h> 780Sduke#include <dev/usb2/core/usb2_debug.h> 790Sduke#include <dev/usb2/core/usb2_request.h> 800Sduke#include <dev/usb2/core/usb2_busdma.h> 810Sduke#include <dev/usb2/core/usb2_util.h> 820Sduke 830Sduke#include <dev/usb2/ethernet/usb2_ethernet.h> 840Sduke#include <dev/usb2/ethernet/if_ruereg.h> 850Sduke 860Sduke#if USB_DEBUG 870Sdukestatic int rue_debug = 0; 880Sduke 890SdukeSYSCTL_NODE(_hw_usb2, OID_AUTO, rue, CTLFLAG_RW, 0, "USB rue"); 900SdukeSYSCTL_INT(_hw_usb2_rue, OID_AUTO, debug, CTLFLAG_RW, 910Sduke &rue_debug, 0, "Debug level"); 920Sduke#endif 930Sduke 940Sduke/* 950Sduke * Various supported device vendors/products. 960Sduke */ 970Sduke 980Sdukestatic const struct usb2_device_id rue_devs[] = { 990Sduke {USB_VPI(USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUAKTX, 0)}, 1000Sduke {USB_VPI(USB_VENDOR_REALTEK, USB_PRODUCT_REALTEK_USBKR100, 0)}, 1010Sduke}; 1020Sduke 1030Sduke/* prototypes */ 1040Sduke 1050Sdukestatic device_probe_t rue_probe; 1060Sdukestatic device_attach_t rue_attach; 1070Sdukestatic device_detach_t rue_detach; 1080Sdukestatic device_shutdown_t rue_shutdown; 1090Sduke 1100Sdukestatic miibus_readreg_t rue_miibus_readreg; 1110Sdukestatic miibus_writereg_t rue_miibus_writereg; 1120Sdukestatic miibus_statchg_t rue_miibus_statchg; 1130Sduke 1140Sdukestatic usb2_callback_t rue_intr_callback; 1150Sdukestatic usb2_callback_t rue_bulk_read_callback; 1160Sdukestatic usb2_callback_t rue_bulk_write_callback; 1170Sduke 1180Sdukestatic usb2_ether_fn_t rue_attach_post; 1190Sdukestatic usb2_ether_fn_t rue_init; 1200Sdukestatic usb2_ether_fn_t rue_stop; 1210Sdukestatic usb2_ether_fn_t rue_start; 1220Sdukestatic usb2_ether_fn_t rue_tick; 1230Sdukestatic usb2_ether_fn_t rue_setmulti; 1240Sdukestatic usb2_ether_fn_t rue_setpromisc; 1250Sduke 1260Sdukestatic int rue_read_mem(struct rue_softc *, uint16_t, void *, int); 1270Sdukestatic int rue_write_mem(struct rue_softc *, uint16_t, void *, int); 1280Sdukestatic uint8_t rue_csr_read_1(struct rue_softc *, uint16_t); 1290Sdukestatic uint16_t rue_csr_read_2(struct rue_softc *, uint16_t); 1300Sdukestatic int rue_csr_write_1(struct rue_softc *, uint16_t, uint8_t); 1310Sdukestatic int rue_csr_write_2(struct rue_softc *, uint16_t, uint16_t); 1320Sdukestatic int rue_csr_write_4(struct rue_softc *, int, uint32_t); 1330Sduke 1340Sdukestatic void rue_reset(struct rue_softc *); 1350Sdukestatic int rue_ifmedia_upd(struct ifnet *); 1360Sdukestatic void rue_ifmedia_sts(struct ifnet *, struct ifmediareq *); 1370Sduke 1380Sdukestatic const struct usb2_config rue_config[RUE_N_TRANSFER] = { 1390Sduke 1400Sduke [RUE_BULK_DT_WR] = { 1410Sduke .type = UE_BULK, 1420Sduke .endpoint = UE_ADDR_ANY, 1430Sduke .direction = UE_DIR_OUT, 1440Sduke .mh.bufsize = MCLBYTES, 1450Sduke .mh.flags = {.pipe_bof = 1,.force_short_xfer = 1,}, 1460Sduke .mh.callback = rue_bulk_write_callback, 1470Sduke .mh.timeout = 10000, /* 10 seconds */ 1480Sduke }, 1490Sduke 150 [RUE_BULK_DT_RD] = { 151 .type = UE_BULK, 152 .endpoint = UE_ADDR_ANY, 153 .direction = UE_DIR_IN, 154 .mh.bufsize = (MCLBYTES + 4), 155 .mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 156 .mh.callback = rue_bulk_read_callback, 157 .mh.timeout = 0, /* no timeout */ 158 }, 159 160 [RUE_INTR_DT_RD] = { 161 .type = UE_INTERRUPT, 162 .endpoint = UE_ADDR_ANY, 163 .direction = UE_DIR_IN, 164 .mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 165 .mh.bufsize = 0, /* use wMaxPacketSize */ 166 .mh.callback = rue_intr_callback, 167 }, 168}; 169 170static device_method_t rue_methods[] = { 171 /* Device interface */ 172 DEVMETHOD(device_probe, rue_probe), 173 DEVMETHOD(device_attach, rue_attach), 174 DEVMETHOD(device_detach, rue_detach), 175 DEVMETHOD(device_shutdown, rue_shutdown), 176 177 /* Bus interface */ 178 DEVMETHOD(bus_print_child, bus_generic_print_child), 179 DEVMETHOD(bus_driver_added, bus_generic_driver_added), 180 181 /* MII interface */ 182 DEVMETHOD(miibus_readreg, rue_miibus_readreg), 183 DEVMETHOD(miibus_writereg, rue_miibus_writereg), 184 DEVMETHOD(miibus_statchg, rue_miibus_statchg), 185 186 {0, 0} 187}; 188 189static driver_t rue_driver = { 190 .name = "rue", 191 .methods = rue_methods, 192 .size = sizeof(struct rue_softc), 193}; 194 195static devclass_t rue_devclass; 196 197DRIVER_MODULE(rue, ushub, rue_driver, rue_devclass, NULL, 0); 198DRIVER_MODULE(miibus, rue, miibus_driver, miibus_devclass, 0, 0); 199MODULE_DEPEND(rue, usb2_ethernet, 1, 1, 1); 200MODULE_DEPEND(rue, usb2_core, 1, 1, 1); 201MODULE_DEPEND(rue, ether, 1, 1, 1); 202MODULE_DEPEND(rue, miibus, 1, 1, 1); 203 204static const struct usb2_ether_methods rue_ue_methods = { 205 .ue_attach_post = rue_attach_post, 206 .ue_start = rue_start, 207 .ue_init = rue_init, 208 .ue_stop = rue_stop, 209 .ue_tick = rue_tick, 210 .ue_setmulti = rue_setmulti, 211 .ue_setpromisc = rue_setpromisc, 212 .ue_mii_upd = rue_ifmedia_upd, 213 .ue_mii_sts = rue_ifmedia_sts, 214}; 215 216#define RUE_SETBIT(sc, reg, x) \ 217 rue_csr_write_1(sc, reg, rue_csr_read_1(sc, reg) | (x)) 218 219#define RUE_CLRBIT(sc, reg, x) \ 220 rue_csr_write_1(sc, reg, rue_csr_read_1(sc, reg) & ~(x)) 221 222static int 223rue_read_mem(struct rue_softc *sc, uint16_t addr, void *buf, int len) 224{ 225 struct usb2_device_request req; 226 227 req.bmRequestType = UT_READ_VENDOR_DEVICE; 228 req.bRequest = UR_SET_ADDRESS; 229 USETW(req.wValue, addr); 230 USETW(req.wIndex, 0); 231 USETW(req.wLength, len); 232 233 return (usb2_ether_do_request(&sc->sc_ue, &req, buf, 1000)); 234} 235 236static int 237rue_write_mem(struct rue_softc *sc, uint16_t addr, void *buf, int len) 238{ 239 struct usb2_device_request req; 240 241 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 242 req.bRequest = UR_SET_ADDRESS; 243 USETW(req.wValue, addr); 244 USETW(req.wIndex, 0); 245 USETW(req.wLength, len); 246 247 return (usb2_ether_do_request(&sc->sc_ue, &req, buf, 1000)); 248} 249 250static uint8_t 251rue_csr_read_1(struct rue_softc *sc, uint16_t reg) 252{ 253 uint8_t val; 254 255 rue_read_mem(sc, reg, &val, 1); 256 return (val); 257} 258 259static uint16_t 260rue_csr_read_2(struct rue_softc *sc, uint16_t reg) 261{ 262 uint8_t val[2]; 263 264 rue_read_mem(sc, reg, &val, 2); 265 return (UGETW(val)); 266} 267 268static int 269rue_csr_write_1(struct rue_softc *sc, uint16_t reg, uint8_t val) 270{ 271 return (rue_write_mem(sc, reg, &val, 1)); 272} 273 274static int 275rue_csr_write_2(struct rue_softc *sc, uint16_t reg, uint16_t val) 276{ 277 uint8_t temp[2]; 278 279 USETW(temp, val); 280 return (rue_write_mem(sc, reg, &temp, 2)); 281} 282 283static int 284rue_csr_write_4(struct rue_softc *sc, int reg, uint32_t val) 285{ 286 uint8_t temp[4]; 287 288 USETDW(temp, val); 289 return (rue_write_mem(sc, reg, &temp, 4)); 290} 291 292static int 293rue_miibus_readreg(device_t dev, int phy, int reg) 294{ 295 struct rue_softc *sc = device_get_softc(dev); 296 uint16_t rval; 297 uint16_t ruereg; 298 int locked; 299 300 if (phy != 0) /* RTL8150 supports PHY == 0, only */ 301 return (0); 302 303 locked = mtx_owned(&sc->sc_mtx); 304 if (!locked) 305 RUE_LOCK(sc); 306 307 switch (reg) { 308 case MII_BMCR: 309 ruereg = RUE_BMCR; 310 break; 311 case MII_BMSR: 312 ruereg = RUE_BMSR; 313 break; 314 case MII_ANAR: 315 ruereg = RUE_ANAR; 316 break; 317 case MII_ANER: 318 ruereg = RUE_AER; 319 break; 320 case MII_ANLPAR: 321 ruereg = RUE_ANLP; 322 break; 323 case MII_PHYIDR1: 324 case MII_PHYIDR2: 325 rval = 0; 326 goto done; 327 default: 328 if (RUE_REG_MIN <= reg && reg <= RUE_REG_MAX) { 329 rval = rue_csr_read_1(sc, reg); 330 goto done; 331 } 332 device_printf(sc->sc_ue.ue_dev, "bad phy register\n"); 333 rval = 0; 334 goto done; 335 } 336 337 rval = rue_csr_read_2(sc, ruereg); 338done: 339 if (!locked) 340 RUE_UNLOCK(sc); 341 return (rval); 342} 343 344static int 345rue_miibus_writereg(device_t dev, int phy, int reg, int data) 346{ 347 struct rue_softc *sc = device_get_softc(dev); 348 uint16_t ruereg; 349 int locked; 350 351 if (phy != 0) /* RTL8150 supports PHY == 0, only */ 352 return (0); 353 354 locked = mtx_owned(&sc->sc_mtx); 355 if (!locked) 356 RUE_LOCK(sc); 357 358 switch (reg) { 359 case MII_BMCR: 360 ruereg = RUE_BMCR; 361 break; 362 case MII_BMSR: 363 ruereg = RUE_BMSR; 364 break; 365 case MII_ANAR: 366 ruereg = RUE_ANAR; 367 break; 368 case MII_ANER: 369 ruereg = RUE_AER; 370 break; 371 case MII_ANLPAR: 372 ruereg = RUE_ANLP; 373 break; 374 case MII_PHYIDR1: 375 case MII_PHYIDR2: 376 goto done; 377 default: 378 if (RUE_REG_MIN <= reg && reg <= RUE_REG_MAX) { 379 rue_csr_write_1(sc, reg, data); 380 goto done; 381 } 382 device_printf(sc->sc_ue.ue_dev, " bad phy register\n"); 383 goto done; 384 } 385 rue_csr_write_2(sc, ruereg, data); 386done: 387 if (!locked) 388 RUE_UNLOCK(sc); 389 return (0); 390} 391 392static void 393rue_miibus_statchg(device_t dev) 394{ 395 /* 396 * When the code below is enabled the card starts doing weird 397 * things after link going from UP to DOWN and back UP. 398 * 399 * Looks like some of register writes below messes up PHY 400 * interface. 401 * 402 * No visible regressions were found after commenting this code 403 * out, so that disable it for good. 404 */ 405#if 0 406 struct rue_softc *sc = device_get_softc(dev); 407 struct mii_data *mii = GET_MII(sc); 408 uint16_t bmcr; 409 int locked; 410 411 locked = mtx_owned(&sc->sc_mtx); 412 if (!locked) 413 RUE_LOCK(sc); 414 415 RUE_CLRBIT(sc, RUE_CR, (RUE_CR_RE | RUE_CR_TE)); 416 417 bmcr = rue_csr_read_2(sc, RUE_BMCR); 418 419 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX) 420 bmcr |= RUE_BMCR_SPD_SET; 421 else 422 bmcr &= ~RUE_BMCR_SPD_SET; 423 424 if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) 425 bmcr |= RUE_BMCR_DUPLEX; 426 else 427 bmcr &= ~RUE_BMCR_DUPLEX; 428 429 rue_csr_write_2(sc, RUE_BMCR, bmcr); 430 431 RUE_SETBIT(sc, RUE_CR, (RUE_CR_RE | RUE_CR_TE)); 432 433 if (!locked) 434 RUE_UNLOCK(sc); 435#endif 436} 437 438static void 439rue_setpromisc(struct usb2_ether *ue) 440{ 441 struct rue_softc *sc = usb2_ether_getsc(ue); 442 struct ifnet *ifp = usb2_ether_getifp(ue); 443 444 RUE_LOCK_ASSERT(sc, MA_OWNED); 445 446 /* If we want promiscuous mode, set the allframes bit. */ 447 if (ifp->if_flags & IFF_PROMISC) 448 RUE_SETBIT(sc, RUE_RCR, RUE_RCR_AAP); 449 else 450 RUE_CLRBIT(sc, RUE_RCR, RUE_RCR_AAP); 451} 452 453/* 454 * Program the 64-bit multicast hash filter. 455 */ 456static void 457rue_setmulti(struct usb2_ether *ue) 458{ 459 struct rue_softc *sc = usb2_ether_getsc(ue); 460 struct ifnet *ifp = usb2_ether_getifp(ue); 461 uint16_t rxcfg; 462 int h = 0; 463 uint32_t hashes[2] = { 0, 0 }; 464 struct ifmultiaddr *ifma; 465 int mcnt = 0; 466 467 RUE_LOCK_ASSERT(sc, MA_OWNED); 468 469 rxcfg = rue_csr_read_2(sc, RUE_RCR); 470 471 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) { 472 rxcfg |= (RUE_RCR_AAM | RUE_RCR_AAP); 473 rxcfg &= ~RUE_RCR_AM; 474 rue_csr_write_2(sc, RUE_RCR, rxcfg); 475 rue_csr_write_4(sc, RUE_MAR0, 0xFFFFFFFF); 476 rue_csr_write_4(sc, RUE_MAR4, 0xFFFFFFFF); 477 return; 478 } 479 480 /* first, zot all the existing hash bits */ 481 rue_csr_write_4(sc, RUE_MAR0, 0); 482 rue_csr_write_4(sc, RUE_MAR4, 0); 483 484 /* now program new ones */ 485 IF_ADDR_LOCK(ifp); 486 TAILQ_FOREACH (ifma, &ifp->if_multiaddrs, ifma_link) 487 { 488 if (ifma->ifma_addr->sa_family != AF_LINK) 489 continue; 490 h = ether_crc32_be(LLADDR((struct sockaddr_dl *) 491 ifma->ifma_addr), ETHER_ADDR_LEN) >> 26; 492 if (h < 32) 493 hashes[0] |= (1 << h); 494 else 495 hashes[1] |= (1 << (h - 32)); 496 mcnt++; 497 } 498 IF_ADDR_UNLOCK(ifp); 499 500 if (mcnt) 501 rxcfg |= RUE_RCR_AM; 502 else 503 rxcfg &= ~RUE_RCR_AM; 504 505 rxcfg &= ~(RUE_RCR_AAM | RUE_RCR_AAP); 506 507 rue_csr_write_2(sc, RUE_RCR, rxcfg); 508 rue_csr_write_4(sc, RUE_MAR0, hashes[0]); 509 rue_csr_write_4(sc, RUE_MAR4, hashes[1]); 510} 511 512static void 513rue_reset(struct rue_softc *sc) 514{ 515 int i; 516 517 rue_csr_write_1(sc, RUE_CR, RUE_CR_SOFT_RST); 518 519 for (i = 0; i != RUE_TIMEOUT; i++) { 520 if (usb2_ether_pause(&sc->sc_ue, hz / 1000)) 521 break; 522 if (!(rue_csr_read_1(sc, RUE_CR) & RUE_CR_SOFT_RST)) 523 break; 524 } 525 if (i == RUE_TIMEOUT) 526 device_printf(sc->sc_ue.ue_dev, "reset never completed!\n"); 527 528 usb2_ether_pause(&sc->sc_ue, hz / 100); 529} 530 531static void 532rue_attach_post(struct usb2_ether *ue) 533{ 534 struct rue_softc *sc = usb2_ether_getsc(ue); 535 536 /* reset the adapter */ 537 rue_reset(sc); 538 539 /* get station address from the EEPROM */ 540 rue_read_mem(sc, RUE_EEPROM_IDR0, ue->ue_eaddr, ETHER_ADDR_LEN); 541} 542 543/* 544 * Probe for a RTL8150 chip. 545 */ 546static int 547rue_probe(device_t dev) 548{ 549 struct usb2_attach_arg *uaa = device_get_ivars(dev); 550 551 if (uaa->usb2_mode != USB_MODE_HOST) 552 return (ENXIO); 553 if (uaa->info.bConfigIndex != RUE_CONFIG_IDX) 554 return (ENXIO); 555 if (uaa->info.bIfaceIndex != RUE_IFACE_IDX) 556 return (ENXIO); 557 558 return (usb2_lookup_id_by_uaa(rue_devs, sizeof(rue_devs), uaa)); 559} 560 561/* 562 * Attach the interface. Allocate softc structures, do ifmedia 563 * setup and ethernet/BPF attach. 564 */ 565static int 566rue_attach(device_t dev) 567{ 568 struct usb2_attach_arg *uaa = device_get_ivars(dev); 569 struct rue_softc *sc = device_get_softc(dev); 570 struct usb2_ether *ue = &sc->sc_ue; 571 uint8_t iface_index; 572 int error; 573 574 device_set_usb2_desc(dev); 575 mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF); 576 577 iface_index = RUE_IFACE_IDX; 578 error = usb2_transfer_setup(uaa->device, &iface_index, 579 sc->sc_xfer, rue_config, RUE_N_TRANSFER, 580 sc, &sc->sc_mtx); 581 if (error) { 582 device_printf(dev, "allocating USB transfers failed!\n"); 583 goto detach; 584 } 585 586 ue->ue_sc = sc; 587 ue->ue_dev = dev; 588 ue->ue_udev = uaa->device; 589 ue->ue_mtx = &sc->sc_mtx; 590 ue->ue_methods = &rue_ue_methods; 591 592 error = usb2_ether_ifattach(ue); 593 if (error) { 594 device_printf(dev, "could not attach interface\n"); 595 goto detach; 596 } 597 return (0); /* success */ 598 599detach: 600 rue_detach(dev); 601 return (ENXIO); /* failure */ 602} 603 604static int 605rue_detach(device_t dev) 606{ 607 struct rue_softc *sc = device_get_softc(dev); 608 struct usb2_ether *ue = &sc->sc_ue; 609 610 usb2_transfer_unsetup(sc->sc_xfer, RUE_N_TRANSFER); 611 usb2_ether_ifdetach(ue); 612 mtx_destroy(&sc->sc_mtx); 613 614 return (0); 615} 616 617static void 618rue_intr_callback(struct usb2_xfer *xfer) 619{ 620 struct rue_softc *sc = xfer->priv_sc; 621 struct ifnet *ifp = usb2_ether_getifp(&sc->sc_ue); 622 struct rue_intrpkt pkt; 623 624 switch (USB_GET_STATE(xfer)) { 625 case USB_ST_TRANSFERRED: 626 627 if (ifp && (ifp->if_drv_flags & IFF_DRV_RUNNING) && 628 (xfer->actlen >= sizeof(pkt))) { 629 630 usb2_copy_out(xfer->frbuffers, 0, &pkt, sizeof(pkt)); 631 632 ifp->if_ierrors += pkt.rue_rxlost_cnt; 633 ifp->if_ierrors += pkt.rue_crcerr_cnt; 634 ifp->if_collisions += pkt.rue_col_cnt; 635 } 636 /* FALLTHROUGH */ 637 case USB_ST_SETUP: 638tr_setup: 639 xfer->frlengths[0] = xfer->max_data_length; 640 usb2_start_hardware(xfer); 641 return; 642 643 default: /* Error */ 644 if (xfer->error != USB_ERR_CANCELLED) { 645 /* try to clear stall first */ 646 xfer->flags.stall_pipe = 1; 647 goto tr_setup; 648 } 649 return; 650 } 651} 652 653static void 654rue_bulk_read_callback(struct usb2_xfer *xfer) 655{ 656 struct rue_softc *sc = xfer->priv_sc; 657 struct usb2_ether *ue = &sc->sc_ue; 658 struct ifnet *ifp = usb2_ether_getifp(ue); 659 uint16_t status; 660 661 switch (USB_GET_STATE(xfer)) { 662 case USB_ST_TRANSFERRED: 663 664 if (xfer->actlen < 4) { 665 ifp->if_ierrors++; 666 goto tr_setup; 667 } 668 usb2_copy_out(xfer->frbuffers, xfer->actlen - 4, 669 &status, sizeof(status)); 670 xfer->actlen -= 4; 671 672 /* check recieve packet was valid or not */ 673 status = le16toh(status); 674 if ((status & RUE_RXSTAT_VALID) == 0) { 675 ifp->if_ierrors++; 676 goto tr_setup; 677 } 678 usb2_ether_rxbuf(ue, xfer->frbuffers, 0, xfer->actlen); 679 /* FALLTHROUGH */ 680 case USB_ST_SETUP: 681tr_setup: 682 xfer->frlengths[0] = xfer->max_data_length; 683 usb2_start_hardware(xfer); 684 usb2_ether_rxflush(ue); 685 return; 686 687 default: /* Error */ 688 DPRINTF("bulk read error, %s\n", 689 usb2_errstr(xfer->error)); 690 691 if (xfer->error != USB_ERR_CANCELLED) { 692 /* try to clear stall first */ 693 xfer->flags.stall_pipe = 1; 694 goto tr_setup; 695 } 696 return; 697 } 698} 699 700static void 701rue_bulk_write_callback(struct usb2_xfer *xfer) 702{ 703 struct rue_softc *sc = xfer->priv_sc; 704 struct ifnet *ifp = usb2_ether_getifp(&sc->sc_ue); 705 struct mbuf *m; 706 int temp_len; 707 708 switch (USB_GET_STATE(xfer)) { 709 case USB_ST_TRANSFERRED: 710 DPRINTFN(11, "transfer complete\n"); 711 ifp->if_opackets++; 712 713 /* FALLTHROUGH */ 714 case USB_ST_SETUP: 715tr_setup: 716 if ((sc->sc_flags & RUE_FLAG_LINK) == 0) { 717 /* 718 * don't send anything if there is no link ! 719 */ 720 return; 721 } 722 IFQ_DRV_DEQUEUE(&ifp->if_snd, m); 723 724 if (m == NULL) 725 return; 726 if (m->m_pkthdr.len > MCLBYTES) 727 m->m_pkthdr.len = MCLBYTES; 728 temp_len = m->m_pkthdr.len; 729 730 usb2_m_copy_in(xfer->frbuffers, 0, 731 m, 0, m->m_pkthdr.len); 732 733 /* 734 * This is an undocumented behavior. 735 * RTL8150 chip doesn't send frame length smaller than 736 * RUE_MIN_FRAMELEN (60) byte packet. 737 */ 738 if (temp_len < RUE_MIN_FRAMELEN) { 739 usb2_bzero(xfer->frbuffers, temp_len, 740 RUE_MIN_FRAMELEN - temp_len); 741 temp_len = RUE_MIN_FRAMELEN; 742 } 743 xfer->frlengths[0] = temp_len; 744 745 /* 746 * if there's a BPF listener, bounce a copy 747 * of this frame to him: 748 */ 749 BPF_MTAP(ifp, m); 750 751 m_freem(m); 752 753 usb2_start_hardware(xfer); 754 755 return; 756 757 default: /* Error */ 758 DPRINTFN(11, "transfer error, %s\n", 759 usb2_errstr(xfer->error)); 760 761 ifp->if_oerrors++; 762 763 if (xfer->error != USB_ERR_CANCELLED) { 764 /* try to clear stall first */ 765 xfer->flags.stall_pipe = 1; 766 goto tr_setup; 767 } 768 return; 769 } 770} 771 772static void 773rue_tick(struct usb2_ether *ue) 774{ 775 struct rue_softc *sc = usb2_ether_getsc(ue); 776 struct mii_data *mii = GET_MII(sc); 777 778 RUE_LOCK_ASSERT(sc, MA_OWNED); 779 780 mii_tick(mii); 781 if ((sc->sc_flags & RUE_FLAG_LINK) == 0 782 && mii->mii_media_status & IFM_ACTIVE && 783 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) { 784 sc->sc_flags |= RUE_FLAG_LINK; 785 rue_start(ue); 786 } 787} 788 789static void 790rue_start(struct usb2_ether *ue) 791{ 792 struct rue_softc *sc = usb2_ether_getsc(ue); 793 794 /* 795 * start the USB transfers, if not already started: 796 */ 797 usb2_transfer_start(sc->sc_xfer[RUE_INTR_DT_RD]); 798 usb2_transfer_start(sc->sc_xfer[RUE_BULK_DT_RD]); 799 usb2_transfer_start(sc->sc_xfer[RUE_BULK_DT_WR]); 800} 801 802static void 803rue_init(struct usb2_ether *ue) 804{ 805 struct rue_softc *sc = usb2_ether_getsc(ue); 806 struct ifnet *ifp = usb2_ether_getifp(ue); 807 808 RUE_LOCK_ASSERT(sc, MA_OWNED); 809 810 /* 811 * Cancel pending I/O 812 */ 813 rue_reset(sc); 814 815 /* Set MAC address */ 816 rue_write_mem(sc, RUE_IDR0, IF_LLADDR(ifp), ETHER_ADDR_LEN); 817 818 rue_stop(ue); 819 820 /* 821 * Set the initial TX and RX configuration. 822 */ 823 rue_csr_write_1(sc, RUE_TCR, RUE_TCR_CONFIG); 824 rue_csr_write_2(sc, RUE_RCR, RUE_RCR_CONFIG|RUE_RCR_AB); 825 826 /* Load the multicast filter */ 827 rue_setpromisc(ue); 828 /* Load the multicast filter. */ 829 rue_setmulti(ue); 830 831 /* Enable RX and TX */ 832 rue_csr_write_1(sc, RUE_CR, (RUE_CR_TE | RUE_CR_RE | RUE_CR_EP3CLREN)); 833 834 usb2_transfer_set_stall(sc->sc_xfer[RUE_BULK_DT_WR]); 835 836 ifp->if_drv_flags |= IFF_DRV_RUNNING; 837 rue_start(ue); 838} 839 840/* 841 * Set media options. 842 */ 843static int 844rue_ifmedia_upd(struct ifnet *ifp) 845{ 846 struct rue_softc *sc = ifp->if_softc; 847 struct mii_data *mii = GET_MII(sc); 848 849 RUE_LOCK_ASSERT(sc, MA_OWNED); 850 851 sc->sc_flags &= ~RUE_FLAG_LINK; 852 if (mii->mii_instance) { 853 struct mii_softc *miisc; 854 855 LIST_FOREACH(miisc, &mii->mii_phys, mii_list) 856 mii_phy_reset(miisc); 857 } 858 mii_mediachg(mii); 859 return (0); 860} 861 862/* 863 * Report current media status. 864 */ 865static void 866rue_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr) 867{ 868 struct rue_softc *sc = ifp->if_softc; 869 struct mii_data *mii = GET_MII(sc); 870 871 RUE_LOCK(sc); 872 mii_pollstat(mii); 873 RUE_UNLOCK(sc); 874 ifmr->ifm_active = mii->mii_media_active; 875 ifmr->ifm_status = mii->mii_media_status; 876} 877 878static void 879rue_stop(struct usb2_ether *ue) 880{ 881 struct rue_softc *sc = usb2_ether_getsc(ue); 882 struct ifnet *ifp = usb2_ether_getifp(ue); 883 884 RUE_LOCK_ASSERT(sc, MA_OWNED); 885 886 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 887 sc->sc_flags &= ~RUE_FLAG_LINK; 888 889 /* 890 * stop all the transfers, if not already stopped: 891 */ 892 usb2_transfer_stop(sc->sc_xfer[RUE_BULK_DT_WR]); 893 usb2_transfer_stop(sc->sc_xfer[RUE_BULK_DT_RD]); 894 usb2_transfer_stop(sc->sc_xfer[RUE_INTR_DT_RD]); 895 896 rue_csr_write_1(sc, RUE_CR, 0x00); 897 898 rue_reset(sc); 899} 900 901/* 902 * Stop all chip I/O so that the kernel's probe routines don't 903 * get confused by errant DMAs when rebooting. 904 */ 905static int 906rue_shutdown(device_t dev) 907{ 908 struct rue_softc *sc = device_get_softc(dev); 909 910 usb2_ether_ifshutdown(&sc->sc_ue); 911 912 return (0); 913} 914