1 /* $NetBSD: if_udav.c,v 1.2 2003/09/04 15:17:38 tsutsui Exp $ */ 2 /* $nabe: if_udav.c,v 1.3 2003/08/21 16:57:19 nabe Exp $ */ 3 /* $FreeBSD$ */ 4 /*- 5 * Copyright (c) 2003 6 * Shingo WATANABE <nabe@nabechan.org>. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. 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 THE AUTHOR 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 THE AUTHOR OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 */ 33 34 /* 35 * DM9601(DAVICOM USB to Ethernet MAC Controller with Integrated 10/100 PHY) 36 * The spec can be found at the following url. 37 * http://www.davicom.com.tw/big5/download/Data%20Sheet/DM9601-DS-P01-930914.pdf 38 */ 39 40 /* 41 * TODO: 42 * Interrupt Endpoint support 43 * External PHYs 44 */ 45 46 #include <sys/cdefs.h> 47 __FBSDID("$FreeBSD$"); 48 49 #include "usbdevs.h" 50 #include <dev/usb/usb.h> 51 #include <dev/usb/usb_mfunc.h> 52 #include <dev/usb/usb_error.h> 53 54 #define USB_DEBUG_VAR udav_debug 55 56 #include <dev/usb/usb_core.h> 57 #include <dev/usb/usb_lookup.h> 58 #include <dev/usb/usb_process.h> 59 #include <dev/usb/usb_debug.h> 60 #include <dev/usb/usb_request.h> 61 #include <dev/usb/usb_busdma.h> 62 #include <dev/usb/usb_util.h> 63 64 #include <dev/usb/net/usb_ethernet.h> 65 #include <dev/usb/net/if_udavreg.h> 66 67 /* prototypes */ 68 69 static device_probe_t udav_probe; 70 static device_attach_t udav_attach; 71 static device_detach_t udav_detach; 72 73 static usb_callback_t udav_bulk_write_callback; 74 static usb_callback_t udav_bulk_read_callback; 75 static usb_callback_t udav_intr_callback; 76 77 static uether_fn_t udav_attach_post; 78 static uether_fn_t udav_init; 79 static uether_fn_t udav_stop; 80 static uether_fn_t udav_start; 81 static uether_fn_t udav_tick; 82 static uether_fn_t udav_setmulti; 83 static uether_fn_t udav_setpromisc; 84 85 static int udav_csr_read(struct udav_softc *, uint16_t, void *, int); 86 static int udav_csr_write(struct udav_softc *, uint16_t, void *, int); 87 static uint8_t udav_csr_read1(struct udav_softc *, uint16_t); 88 static int udav_csr_write1(struct udav_softc *, uint16_t, uint8_t); 89 static void udav_reset(struct udav_softc *); 90 static int udav_ifmedia_upd(struct ifnet *); 91 static void udav_ifmedia_status(struct ifnet *, struct ifmediareq *); 92 93 static miibus_readreg_t udav_miibus_readreg; 94 static miibus_writereg_t udav_miibus_writereg; 95 static miibus_statchg_t udav_miibus_statchg; 96 97 static const struct usb_config udav_config[UDAV_N_TRANSFER] = { 98 99 [UDAV_BULK_DT_WR] = { 100 .type = UE_BULK, 101 .endpoint = UE_ADDR_ANY, 102 .direction = UE_DIR_OUT, 103 .bufsize = (MCLBYTES + 2), 104 .flags = {.pipe_bof = 1,.force_short_xfer = 1,}, 105 .callback = udav_bulk_write_callback, 106 .timeout = 10000, /* 10 seconds */ 107 }, 108 109 [UDAV_BULK_DT_RD] = { 110 .type = UE_BULK, 111 .endpoint = UE_ADDR_ANY, 112 .direction = UE_DIR_IN, 113 .bufsize = (MCLBYTES + 3), 114 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 115 .callback = udav_bulk_read_callback, 116 .timeout = 0, /* no timeout */ 117 }, 118 119 [UDAV_INTR_DT_RD] = { 120 .type = UE_INTERRUPT, 121 .endpoint = UE_ADDR_ANY, 122 .direction = UE_DIR_IN, 123 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 124 .bufsize = 0, /* use wMaxPacketSize */ 125 .callback = udav_intr_callback, 126 }, 127 }; 128 129 static device_method_t udav_methods[] = { 130 /* Device interface */ 131 DEVMETHOD(device_probe, udav_probe), 132 DEVMETHOD(device_attach, udav_attach), 133 DEVMETHOD(device_detach, udav_detach), 134 135 /* bus interface */ 136 DEVMETHOD(bus_print_child, bus_generic_print_child), 137 DEVMETHOD(bus_driver_added, bus_generic_driver_added), 138 139 /* MII interface */ 140 DEVMETHOD(miibus_readreg, udav_miibus_readreg), 141 DEVMETHOD(miibus_writereg, udav_miibus_writereg), 142 DEVMETHOD(miibus_statchg, udav_miibus_statchg), 143 144 {0, 0} 145 }; 146 147 static driver_t udav_driver = { 148 .name = "udav", 149 .methods = udav_methods, 150 .size = sizeof(struct udav_softc), 151 }; 152 153 static devclass_t udav_devclass; 154 155 DRIVER_MODULE(udav, uhub, udav_driver, udav_devclass, NULL, 0); 156 DRIVER_MODULE(miibus, udav, miibus_driver, miibus_devclass, 0, 0); 157 MODULE_DEPEND(udav, uether, 1, 1, 1); 158 MODULE_DEPEND(udav, usb, 1, 1, 1); 159 MODULE_DEPEND(udav, ether, 1, 1, 1); 160 MODULE_DEPEND(udav, miibus, 1, 1, 1); 161 162 static const struct usb_ether_methods udav_ue_methods = { 163 .ue_attach_post = udav_attach_post, 164 .ue_start = udav_start, 165 .ue_init = udav_init, 166 .ue_stop = udav_stop, 167 .ue_tick = udav_tick, 168 .ue_setmulti = udav_setmulti, 169 .ue_setpromisc = udav_setpromisc, 170 .ue_mii_upd = udav_ifmedia_upd, 171 .ue_mii_sts = udav_ifmedia_status, 172 }; 173 174 #if USB_DEBUG 175 static int udav_debug = 0; 176 177 SYSCTL_NODE(_hw_usb, OID_AUTO, udav, CTLFLAG_RW, 0, "USB udav"); 178 SYSCTL_INT(_hw_usb_udav, OID_AUTO, debug, CTLFLAG_RW, &udav_debug, 0, 179 "Debug level"); 180 #endif 181 182 #define UDAV_SETBIT(sc, reg, x) \ 183 udav_csr_write1(sc, reg, udav_csr_read1(sc, reg) | (x)) 184 185 #define UDAV_CLRBIT(sc, reg, x) \ 186 udav_csr_write1(sc, reg, udav_csr_read1(sc, reg) & ~(x)) 187 188 static const struct usb_device_id udav_devs[] = { 189 /* ShanTou DM9601 USB NIC */ 190 {USB_VPI(USB_VENDOR_SHANTOU, USB_PRODUCT_SHANTOU_DM9601, 0)}, 191 /* ShanTou ST268 USB NIC */ 192 {USB_VPI(USB_VENDOR_SHANTOU, USB_PRODUCT_SHANTOU_ST268, 0)}, 193 /* Corega USB-TXC */ 194 {USB_VPI(USB_VENDOR_COREGA, USB_PRODUCT_COREGA_FETHER_USB_TXC, 0)}, 195 }; 196 197 static void 198 udav_attach_post(struct usb_ether *ue) 199 { 200 struct udav_softc *sc = usb2_ether_getsc(ue); 201 202 /* reset the adapter */ 203 udav_reset(sc); 204 205 /* Get Ethernet Address */ 206 udav_csr_read(sc, UDAV_PAR, ue->ue_eaddr, ETHER_ADDR_LEN); 207 } 208 209 static int 210 udav_probe(device_t dev) 211 { 212 struct usb_attach_arg *uaa = device_get_ivars(dev); 213 214 if (uaa->usb_mode != USB_MODE_HOST) 215 return (ENXIO); 216 if (uaa->info.bConfigIndex != UDAV_CONFIG_INDEX) 217 return (ENXIO); 218 if (uaa->info.bIfaceIndex != UDAV_IFACE_INDEX) 219 return (ENXIO); 220 221 return (usb2_lookup_id_by_uaa(udav_devs, sizeof(udav_devs), uaa)); 222 } 223 224 static int 225 udav_attach(device_t dev) 226 { 227 struct usb_attach_arg *uaa = device_get_ivars(dev); 228 struct udav_softc *sc = device_get_softc(dev); 229 struct usb_ether *ue = &sc->sc_ue; 230 uint8_t iface_index; 231 int error; 232 233 sc->sc_flags = USB_GET_DRIVER_INFO(uaa); 234 235 device_set_usb2_desc(dev); 236 237 mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF); 238 239 iface_index = UDAV_IFACE_INDEX; 240 error = usb2_transfer_setup(uaa->device, &iface_index, 241 sc->sc_xfer, udav_config, UDAV_N_TRANSFER, sc, &sc->sc_mtx); 242 if (error) { 243 device_printf(dev, "allocating USB transfers failed!\n"); 244 goto detach; 245 } 246 247 ue->ue_sc = sc; 248 ue->ue_dev = dev; 249 ue->ue_udev = uaa->device; 250 ue->ue_mtx = &sc->sc_mtx; 251 ue->ue_methods = &udav_ue_methods; 252 253 error = usb2_ether_ifattach(ue); 254 if (error) { 255 device_printf(dev, "could not attach interface\n"); 256 goto detach; 257 } 258 259 return (0); /* success */ 260 261 detach: 262 udav_detach(dev); 263 return (ENXIO); /* failure */ 264 } 265 266 static int 267 udav_detach(device_t dev) 268 { 269 struct udav_softc *sc = device_get_softc(dev); 270 struct usb_ether *ue = &sc->sc_ue; 271 272 usb2_transfer_unsetup(sc->sc_xfer, UDAV_N_TRANSFER); 273 usb2_ether_ifdetach(ue); 274 mtx_destroy(&sc->sc_mtx); 275 276 return (0); 277 } 278 279 #if 0 280 static int 281 udav_mem_read(struct udav_softc *sc, uint16_t offset, void *buf, 282 int len) 283 { 284 struct usb_device_request req; 285 286 len &= 0xff; 287 288 req.bmRequestType = UT_READ_VENDOR_DEVICE; 289 req.bRequest = UDAV_REQ_MEM_READ; 290 USETW(req.wValue, 0x0000); 291 USETW(req.wIndex, offset); 292 USETW(req.wLength, len); 293 294 return (usb2_ether_do_request(&sc->sc_ue, &req, buf, 1000)); 295 } 296 297 static int 298 udav_mem_write(struct udav_softc *sc, uint16_t offset, void *buf, 299 int len) 300 { 301 struct usb_device_request req; 302 303 len &= 0xff; 304 305 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 306 req.bRequest = UDAV_REQ_MEM_WRITE; 307 USETW(req.wValue, 0x0000); 308 USETW(req.wIndex, offset); 309 USETW(req.wLength, len); 310 311 return (usb2_ether_do_request(&sc->sc_ue, &req, buf, 1000)); 312 } 313 314 static int 315 udav_mem_write1(struct udav_softc *sc, uint16_t offset, 316 uint8_t ch) 317 { 318 struct usb_device_request req; 319 320 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 321 req.bRequest = UDAV_REQ_MEM_WRITE1; 322 USETW(req.wValue, ch); 323 USETW(req.wIndex, offset); 324 USETW(req.wLength, 0x0000); 325 326 return (usb2_ether_do_request(&sc->sc_ue, &req, NULL, 1000)); 327 } 328 #endif 329 330 static int 331 udav_csr_read(struct udav_softc *sc, uint16_t offset, void *buf, int len) 332 { 333 struct usb_device_request req; 334 335 len &= 0xff; 336 337 req.bmRequestType = UT_READ_VENDOR_DEVICE; 338 req.bRequest = UDAV_REQ_REG_READ; 339 USETW(req.wValue, 0x0000); 340 USETW(req.wIndex, offset); 341 USETW(req.wLength, len); 342 343 return (usb2_ether_do_request(&sc->sc_ue, &req, buf, 1000)); 344 } 345 346 static int 347 udav_csr_write(struct udav_softc *sc, uint16_t offset, void *buf, int len) 348 { 349 struct usb_device_request req; 350 351 offset &= 0xff; 352 len &= 0xff; 353 354 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 355 req.bRequest = UDAV_REQ_REG_WRITE; 356 USETW(req.wValue, 0x0000); 357 USETW(req.wIndex, offset); 358 USETW(req.wLength, len); 359 360 return (usb2_ether_do_request(&sc->sc_ue, &req, buf, 1000)); 361 } 362 363 static uint8_t 364 udav_csr_read1(struct udav_softc *sc, uint16_t offset) 365 { 366 uint8_t val; 367 368 udav_csr_read(sc, offset, &val, 1); 369 return (val); 370 } 371 372 static int 373 udav_csr_write1(struct udav_softc *sc, uint16_t offset, 374 uint8_t ch) 375 { 376 struct usb_device_request req; 377 378 offset &= 0xff; 379 380 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 381 req.bRequest = UDAV_REQ_REG_WRITE1; 382 USETW(req.wValue, ch); 383 USETW(req.wIndex, offset); 384 USETW(req.wLength, 0x0000); 385 386 return (usb2_ether_do_request(&sc->sc_ue, &req, NULL, 1000)); 387 } 388 389 static void 390 udav_init(struct usb_ether *ue) 391 { 392 struct udav_softc *sc = ue->ue_sc; 393 struct ifnet *ifp = usb2_ether_getifp(&sc->sc_ue); 394 395 UDAV_LOCK_ASSERT(sc, MA_OWNED); 396 397 /* 398 * Cancel pending I/O 399 */ 400 udav_stop(ue); 401 402 /* set MAC address */ 403 udav_csr_write(sc, UDAV_PAR, IF_LLADDR(ifp), ETHER_ADDR_LEN); 404 405 /* initialize network control register */ 406 407 /* disable loopback */ 408 UDAV_CLRBIT(sc, UDAV_NCR, UDAV_NCR_LBK0 | UDAV_NCR_LBK1); 409 410 /* Initialize RX control register */ 411 UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_DIS_LONG | UDAV_RCR_DIS_CRC); 412 413 /* load multicast filter and update promiscious mode bit */ 414 udav_setpromisc(ue); 415 416 /* enable RX */ 417 UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_RXEN); 418 419 /* clear POWER_DOWN state of internal PHY */ 420 UDAV_SETBIT(sc, UDAV_GPCR, UDAV_GPCR_GEP_CNTL0); 421 UDAV_CLRBIT(sc, UDAV_GPR, UDAV_GPR_GEPIO0); 422 423 usb2_transfer_set_stall(sc->sc_xfer[UDAV_BULK_DT_WR]); 424 425 ifp->if_drv_flags |= IFF_DRV_RUNNING; 426 udav_start(ue); 427 } 428 429 static void 430 udav_reset(struct udav_softc *sc) 431 { 432 int i; 433 434 /* Select PHY */ 435 #if 1 436 /* 437 * XXX: force select internal phy. 438 * external phy routines are not tested. 439 */ 440 UDAV_CLRBIT(sc, UDAV_NCR, UDAV_NCR_EXT_PHY); 441 #else 442 if (sc->sc_flags & UDAV_EXT_PHY) 443 UDAV_SETBIT(sc, UDAV_NCR, UDAV_NCR_EXT_PHY); 444 else 445 UDAV_CLRBIT(sc, UDAV_NCR, UDAV_NCR_EXT_PHY); 446 #endif 447 448 UDAV_SETBIT(sc, UDAV_NCR, UDAV_NCR_RST); 449 450 for (i = 0; i < UDAV_TX_TIMEOUT; i++) { 451 if (!(udav_csr_read1(sc, UDAV_NCR) & UDAV_NCR_RST)) 452 break; 453 if (usb2_ether_pause(&sc->sc_ue, hz / 100)) 454 break; 455 } 456 457 usb2_ether_pause(&sc->sc_ue, hz / 100); 458 } 459 460 #define UDAV_BITS 6 461 static void 462 udav_setmulti(struct usb_ether *ue) 463 { 464 struct udav_softc *sc = ue->ue_sc; 465 struct ifnet *ifp = usb2_ether_getifp(&sc->sc_ue); 466 struct ifmultiaddr *ifma; 467 uint8_t hashtbl[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; 468 int h = 0; 469 470 UDAV_LOCK_ASSERT(sc, MA_OWNED); 471 472 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) { 473 UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_ALL|UDAV_RCR_PRMSC); 474 return; 475 } 476 477 /* first, zot all the existing hash bits */ 478 memset(hashtbl, 0x00, sizeof(hashtbl)); 479 hashtbl[7] |= 0x80; /* broadcast address */ 480 udav_csr_write(sc, UDAV_MAR, hashtbl, sizeof(hashtbl)); 481 482 /* now program new ones */ 483 IF_ADDR_LOCK(ifp); 484 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) 485 { 486 if (ifma->ifma_addr->sa_family != AF_LINK) 487 continue; 488 h = ether_crc32_be(LLADDR((struct sockaddr_dl *) 489 ifma->ifma_addr), ETHER_ADDR_LEN) >> 26; 490 hashtbl[h / 8] |= 1 << (h % 8); 491 } 492 IF_ADDR_UNLOCK(ifp); 493 494 /* disable all multicast */ 495 UDAV_CLRBIT(sc, UDAV_RCR, UDAV_RCR_ALL); 496 497 /* write hash value to the register */ 498 udav_csr_write(sc, UDAV_MAR, hashtbl, sizeof(hashtbl)); 499 } 500 501 static void 502 udav_setpromisc(struct usb_ether *ue) 503 { 504 struct udav_softc *sc = ue->ue_sc; 505 struct ifnet *ifp = usb2_ether_getifp(&sc->sc_ue); 506 uint8_t rxmode; 507 508 rxmode = udav_csr_read1(sc, UDAV_RCR); 509 rxmode &= ~(UDAV_RCR_ALL | UDAV_RCR_PRMSC); 510 511 if (ifp->if_flags & IFF_PROMISC) 512 rxmode |= UDAV_RCR_ALL | UDAV_RCR_PRMSC; 513 else if (ifp->if_flags & IFF_ALLMULTI) 514 rxmode |= UDAV_RCR_ALL; 515 516 /* write new mode bits */ 517 udav_csr_write1(sc, UDAV_RCR, rxmode); 518 } 519 520 static void 521 udav_start(struct usb_ether *ue) 522 { 523 struct udav_softc *sc = ue->ue_sc; 524 525 /* 526 * start the USB transfers, if not already started: 527 */ 528 usb2_transfer_start(sc->sc_xfer[UDAV_INTR_DT_RD]); 529 usb2_transfer_start(sc->sc_xfer[UDAV_BULK_DT_RD]); 530 usb2_transfer_start(sc->sc_xfer[UDAV_BULK_DT_WR]); 531 } 532 533 static void 534 udav_bulk_write_callback(struct usb_xfer *xfer) 535 { 536 struct udav_softc *sc = xfer->priv_sc; 537 struct ifnet *ifp = usb2_ether_getifp(&sc->sc_ue); 538 struct mbuf *m; 539 int extra_len; 540 int temp_len; 541 uint8_t buf[2]; 542 543 switch (USB_GET_STATE(xfer)) { 544 case USB_ST_TRANSFERRED: 545 DPRINTFN(11, "transfer complete\n"); 546 ifp->if_opackets++; 547 548 /* FALLTHROUGH */ 549 case USB_ST_SETUP: 550 tr_setup: 551 if ((sc->sc_flags & UDAV_FLAG_LINK) == 0) { 552 /* 553 * don't send anything if there is no link ! 554 */ 555 return; 556 } 557 IFQ_DRV_DEQUEUE(&ifp->if_snd, m); 558 559 if (m == NULL) 560 return; 561 if (m->m_pkthdr.len > MCLBYTES) 562 m->m_pkthdr.len = MCLBYTES; 563 if (m->m_pkthdr.len < UDAV_MIN_FRAME_LEN) { 564 extra_len = UDAV_MIN_FRAME_LEN - m->m_pkthdr.len; 565 } else { 566 extra_len = 0; 567 } 568 569 temp_len = (m->m_pkthdr.len + extra_len); 570 571 /* 572 * the frame length is specified in the first 2 bytes of the 573 * buffer 574 */ 575 buf[0] = (uint8_t)(temp_len); 576 buf[1] = (uint8_t)(temp_len >> 8); 577 578 temp_len += 2; 579 580 usb2_copy_in(xfer->frbuffers, 0, buf, 2); 581 582 usb2_m_copy_in(xfer->frbuffers, 2, 583 m, 0, m->m_pkthdr.len); 584 585 if (extra_len) { 586 usb2_bzero(xfer->frbuffers, temp_len - extra_len, 587 extra_len); 588 } 589 /* 590 * if there's a BPF listener, bounce a copy 591 * of this frame to him: 592 */ 593 BPF_MTAP(ifp, m); 594 595 m_freem(m); 596 597 xfer->frlengths[0] = temp_len; 598 usb2_start_hardware(xfer); 599 return; 600 601 default: /* Error */ 602 DPRINTFN(11, "transfer error, %s\n", 603 usb2_errstr(xfer->error)); 604 605 ifp->if_oerrors++; 606 607 if (xfer->error != USB_ERR_CANCELLED) { 608 /* try to clear stall first */ 609 xfer->flags.stall_pipe = 1; 610 goto tr_setup; 611 } 612 return; 613 } 614 } 615 616 static void 617 udav_bulk_read_callback(struct usb_xfer *xfer) 618 { 619 struct udav_softc *sc = xfer->priv_sc; 620 struct usb_ether *ue = &sc->sc_ue; 621 struct ifnet *ifp = usb2_ether_getifp(ue); 622 struct udav_rxpkt stat; 623 int len; 624 625 switch (USB_GET_STATE(xfer)) { 626 case USB_ST_TRANSFERRED: 627 628 if (xfer->actlen < sizeof(stat) + ETHER_CRC_LEN) { 629 ifp->if_ierrors++; 630 goto tr_setup; 631 } 632 usb2_copy_out(xfer->frbuffers, 0, &stat, sizeof(stat)); 633 xfer->actlen -= sizeof(stat); 634 len = min(xfer->actlen, le16toh(stat.pktlen)); 635 len -= ETHER_CRC_LEN; 636 637 if (stat.rxstat & UDAV_RSR_LCS) { 638 ifp->if_collisions++; 639 goto tr_setup; 640 } 641 if (stat.rxstat & UDAV_RSR_ERR) { 642 ifp->if_ierrors++; 643 goto tr_setup; 644 } 645 usb2_ether_rxbuf(ue, xfer->frbuffers, sizeof(stat), len); 646 /* FALLTHROUGH */ 647 case USB_ST_SETUP: 648 tr_setup: 649 xfer->frlengths[0] = xfer->max_data_length; 650 usb2_start_hardware(xfer); 651 usb2_ether_rxflush(ue); 652 return; 653 654 default: /* Error */ 655 DPRINTF("bulk read error, %s\n", 656 usb2_errstr(xfer->error)); 657 658 if (xfer->error != USB_ERR_CANCELLED) { 659 /* try to clear stall first */ 660 xfer->flags.stall_pipe = 1; 661 goto tr_setup; 662 } 663 return; 664 } 665 } 666 667 static void 668 udav_intr_callback(struct usb_xfer *xfer) 669 { 670 switch (USB_GET_STATE(xfer)) { 671 case USB_ST_TRANSFERRED: 672 case USB_ST_SETUP: 673 tr_setup: 674 xfer->frlengths[0] = xfer->max_data_length; 675 usb2_start_hardware(xfer); 676 return; 677 678 default: /* Error */ 679 if (xfer->error != USB_ERR_CANCELLED) { 680 /* try to clear stall first */ 681 xfer->flags.stall_pipe = 1; 682 goto tr_setup; 683 } 684 return; 685 } 686 } 687 688 static void 689 udav_stop(struct usb_ether *ue) 690 { 691 struct udav_softc *sc = ue->ue_sc; 692 struct ifnet *ifp = usb2_ether_getifp(&sc->sc_ue); 693 694 UDAV_LOCK_ASSERT(sc, MA_OWNED); 695 696 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 697 sc->sc_flags &= ~UDAV_FLAG_LINK; 698 699 /* 700 * stop all the transfers, if not already stopped: 701 */ 702 usb2_transfer_stop(sc->sc_xfer[UDAV_BULK_DT_WR]); 703 usb2_transfer_stop(sc->sc_xfer[UDAV_BULK_DT_RD]); 704 usb2_transfer_stop(sc->sc_xfer[UDAV_INTR_DT_RD]); 705 706 udav_reset(sc); 707 } 708 709 static int 710 udav_ifmedia_upd(struct ifnet *ifp) 711 { 712 struct udav_softc *sc = ifp->if_softc; 713 struct mii_data *mii = GET_MII(sc); 714 715 UDAV_LOCK_ASSERT(sc, MA_OWNED); 716 717 sc->sc_flags &= ~UDAV_FLAG_LINK; 718 if (mii->mii_instance) { 719 struct mii_softc *miisc; 720 721 LIST_FOREACH(miisc, &mii->mii_phys, mii_list) 722 mii_phy_reset(miisc); 723 } 724 mii_mediachg(mii); 725 return (0); 726 } 727 728 static void 729 udav_ifmedia_status(struct ifnet *ifp, struct ifmediareq *ifmr) 730 { 731 struct udav_softc *sc = ifp->if_softc; 732 struct mii_data *mii = GET_MII(sc); 733 734 UDAV_LOCK(sc); 735 mii_pollstat(mii); 736 UDAV_UNLOCK(sc); 737 ifmr->ifm_active = mii->mii_media_active; 738 ifmr->ifm_status = mii->mii_media_status; 739 } 740 741 static void 742 udav_tick(struct usb_ether *ue) 743 { 744 struct udav_softc *sc = ue->ue_sc; 745 struct mii_data *mii = GET_MII(sc); 746 747 UDAV_LOCK_ASSERT(sc, MA_OWNED); 748 749 mii_tick(mii); 750 if ((sc->sc_flags & UDAV_FLAG_LINK) == 0 751 && mii->mii_media_status & IFM_ACTIVE && 752 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) { 753 sc->sc_flags |= UDAV_FLAG_LINK; 754 udav_start(ue); 755 } 756 } 757 758 static int 759 udav_miibus_readreg(device_t dev, int phy, int reg) 760 { 761 struct udav_softc *sc = device_get_softc(dev); 762 uint16_t data16; 763 uint8_t val[2]; 764 int locked; 765 766 /* XXX: one PHY only for the internal PHY */ 767 if (phy != 0) 768 return (0); 769 770 locked = mtx_owned(&sc->sc_mtx); 771 if (!locked) 772 UDAV_LOCK(sc); 773 774 /* select internal PHY and set PHY register address */ 775 udav_csr_write1(sc, UDAV_EPAR, 776 UDAV_EPAR_PHY_ADR0 | (reg & UDAV_EPAR_EROA_MASK)); 777 778 /* select PHY operation and start read command */ 779 udav_csr_write1(sc, UDAV_EPCR, UDAV_EPCR_EPOS | UDAV_EPCR_ERPRR); 780 781 /* XXX: should we wait? */ 782 783 /* end read command */ 784 UDAV_CLRBIT(sc, UDAV_EPCR, UDAV_EPCR_ERPRR); 785 786 /* retrieve the result from data registers */ 787 udav_csr_read(sc, UDAV_EPDRL, val, 2); 788 789 data16 = (val[0] | (val[1] << 8)); 790 791 DPRINTFN(11, "phy=%d reg=0x%04x => 0x%04x\n", 792 phy, reg, data16); 793 794 if (!locked) 795 UDAV_UNLOCK(sc); 796 return (data16); 797 } 798 799 static int 800 udav_miibus_writereg(device_t dev, int phy, int reg, int data) 801 { 802 struct udav_softc *sc = device_get_softc(dev); 803 uint8_t val[2]; 804 int locked; 805 806 /* XXX: one PHY only for the internal PHY */ 807 if (phy != 0) 808 return (0); 809 810 locked = mtx_owned(&sc->sc_mtx); 811 if (!locked) 812 UDAV_LOCK(sc); 813 814 /* select internal PHY and set PHY register address */ 815 udav_csr_write1(sc, UDAV_EPAR, 816 UDAV_EPAR_PHY_ADR0 | (reg & UDAV_EPAR_EROA_MASK)); 817 818 /* put the value to the data registers */ 819 val[0] = (data & 0xff); 820 val[1] = (data >> 8) & 0xff; 821 udav_csr_write(sc, UDAV_EPDRL, val, 2); 822 823 /* select PHY operation and start write command */ 824 udav_csr_write1(sc, UDAV_EPCR, UDAV_EPCR_EPOS | UDAV_EPCR_ERPRW); 825 826 /* XXX: should we wait? */ 827 828 /* end write command */ 829 UDAV_CLRBIT(sc, UDAV_EPCR, UDAV_EPCR_ERPRW); 830 831 if (!locked) 832 UDAV_UNLOCK(sc); 833 return (0); 834 } 835 836 static void 837 udav_miibus_statchg(device_t dev) 838 { 839 /* nothing to do */ 840 } 841