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://ptm2.cc.utu.fi/ftp/network/cards/DM9601/From_NET/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 <sys/stdint.h> 50 #include <sys/stddef.h> 51 #include <sys/param.h> 52 #include <sys/queue.h> 53 #include <sys/types.h> 54 #include <sys/systm.h> 55 #include <sys/socket.h> 56 #include <sys/kernel.h> 57 #include <sys/bus.h> 58 #include <sys/module.h> 59 #include <sys/lock.h> 60 #include <sys/mutex.h> 61 #include <sys/condvar.h> 62 #include <sys/sysctl.h> 63 #include <sys/sx.h> 64 #include <sys/unistd.h> 65 #include <sys/callout.h> 66 #include <sys/malloc.h> 67 #include <sys/priv.h> 68 69 #include <net/if.h> 70 #include <net/if_var.h> 71 72 #include <dev/usb/usb.h> 73 #include <dev/usb/usbdi.h> 74 #include <dev/usb/usbdi_util.h> 75 #include "usbdevs.h" 76 77 #define USB_DEBUG_VAR udav_debug 78 #include <dev/usb/usb_debug.h> 79 #include <dev/usb/usb_process.h> 80 81 #include <dev/usb/net/usb_ethernet.h> 82 #include <dev/usb/net/if_udavreg.h> 83 84 /* prototypes */ 85 86 static device_probe_t udav_probe; 87 static device_attach_t udav_attach; 88 static device_detach_t udav_detach; 89 90 static usb_callback_t udav_bulk_write_callback; 91 static usb_callback_t udav_bulk_read_callback; 92 static usb_callback_t udav_intr_callback; 93 94 static uether_fn_t udav_attach_post; 95 static uether_fn_t udav_init; 96 static uether_fn_t udav_stop; 97 static uether_fn_t udav_start; 98 static uether_fn_t udav_tick; 99 static uether_fn_t udav_setmulti; 100 static uether_fn_t udav_setpromisc; 101 102 static int udav_csr_read(struct udav_softc *, uint16_t, void *, int); 103 static int udav_csr_write(struct udav_softc *, uint16_t, void *, int); 104 static uint8_t udav_csr_read1(struct udav_softc *, uint16_t); 105 static int udav_csr_write1(struct udav_softc *, uint16_t, uint8_t); 106 static void udav_reset(struct udav_softc *); 107 static int udav_ifmedia_upd(struct ifnet *); 108 static void udav_ifmedia_status(struct ifnet *, struct ifmediareq *); 109 110 static miibus_readreg_t udav_miibus_readreg; 111 static miibus_writereg_t udav_miibus_writereg; 112 static miibus_statchg_t udav_miibus_statchg; 113 114 static const struct usb_config udav_config[UDAV_N_TRANSFER] = { 115 116 [UDAV_BULK_DT_WR] = { 117 .type = UE_BULK, 118 .endpoint = UE_ADDR_ANY, 119 .direction = UE_DIR_OUT, 120 .bufsize = (MCLBYTES + 2), 121 .flags = {.pipe_bof = 1,.force_short_xfer = 1,}, 122 .callback = udav_bulk_write_callback, 123 .timeout = 10000, /* 10 seconds */ 124 }, 125 126 [UDAV_BULK_DT_RD] = { 127 .type = UE_BULK, 128 .endpoint = UE_ADDR_ANY, 129 .direction = UE_DIR_IN, 130 .bufsize = (MCLBYTES + 3), 131 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 132 .callback = udav_bulk_read_callback, 133 .timeout = 0, /* no timeout */ 134 }, 135 136 [UDAV_INTR_DT_RD] = { 137 .type = UE_INTERRUPT, 138 .endpoint = UE_ADDR_ANY, 139 .direction = UE_DIR_IN, 140 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 141 .bufsize = 0, /* use wMaxPacketSize */ 142 .callback = udav_intr_callback, 143 }, 144 }; 145 146 static device_method_t udav_methods[] = { 147 /* Device interface */ 148 DEVMETHOD(device_probe, udav_probe), 149 DEVMETHOD(device_attach, udav_attach), 150 DEVMETHOD(device_detach, udav_detach), 151 152 /* MII interface */ 153 DEVMETHOD(miibus_readreg, udav_miibus_readreg), 154 DEVMETHOD(miibus_writereg, udav_miibus_writereg), 155 DEVMETHOD(miibus_statchg, udav_miibus_statchg), 156 157 DEVMETHOD_END 158 }; 159 160 static driver_t udav_driver = { 161 .name = "udav", 162 .methods = udav_methods, 163 .size = sizeof(struct udav_softc), 164 }; 165 166 static devclass_t udav_devclass; 167 168 DRIVER_MODULE(udav, uhub, udav_driver, udav_devclass, NULL, 0); 169 DRIVER_MODULE(miibus, udav, miibus_driver, miibus_devclass, 0, 0); 170 MODULE_DEPEND(udav, uether, 1, 1, 1); 171 MODULE_DEPEND(udav, usb, 1, 1, 1); 172 MODULE_DEPEND(udav, ether, 1, 1, 1); 173 MODULE_DEPEND(udav, miibus, 1, 1, 1); 174 MODULE_VERSION(udav, 1); 175 176 static const struct usb_ether_methods udav_ue_methods = { 177 .ue_attach_post = udav_attach_post, 178 .ue_start = udav_start, 179 .ue_init = udav_init, 180 .ue_stop = udav_stop, 181 .ue_tick = udav_tick, 182 .ue_setmulti = udav_setmulti, 183 .ue_setpromisc = udav_setpromisc, 184 .ue_mii_upd = udav_ifmedia_upd, 185 .ue_mii_sts = udav_ifmedia_status, 186 }; 187 188 static const struct usb_ether_methods udav_ue_methods_nophy = { 189 .ue_attach_post = udav_attach_post, 190 .ue_start = udav_start, 191 .ue_init = udav_init, 192 .ue_stop = udav_stop, 193 .ue_setmulti = udav_setmulti, 194 .ue_setpromisc = udav_setpromisc, 195 }; 196 197 #ifdef USB_DEBUG 198 static int udav_debug = 0; 199 200 static SYSCTL_NODE(_hw_usb, OID_AUTO, udav, CTLFLAG_RW, 0, "USB udav"); 201 SYSCTL_INT(_hw_usb_udav, OID_AUTO, debug, CTLFLAG_RW, &udav_debug, 0, 202 "Debug level"); 203 #endif 204 205 #define UDAV_SETBIT(sc, reg, x) \ 206 udav_csr_write1(sc, reg, udav_csr_read1(sc, reg) | (x)) 207 208 #define UDAV_CLRBIT(sc, reg, x) \ 209 udav_csr_write1(sc, reg, udav_csr_read1(sc, reg) & ~(x)) 210 211 static const STRUCT_USB_HOST_ID udav_devs[] = { 212 /* ShanTou DM9601 USB NIC */ 213 {USB_VPI(USB_VENDOR_SHANTOU, USB_PRODUCT_SHANTOU_DM9601, 0)}, 214 /* ShanTou ST268 USB NIC */ 215 {USB_VPI(USB_VENDOR_SHANTOU, USB_PRODUCT_SHANTOU_ST268, 0)}, 216 /* Corega USB-TXC */ 217 {USB_VPI(USB_VENDOR_COREGA, USB_PRODUCT_COREGA_FETHER_USB_TXC, 0)}, 218 /* ShanTou AMD8515 USB NIC */ 219 {USB_VPI(USB_VENDOR_SHANTOU, USB_PRODUCT_SHANTOU_ADM8515, 0)}, 220 /* Kontron AG USB Ethernet */ 221 {USB_VPI(USB_VENDOR_KONTRON, USB_PRODUCT_KONTRON_DM9601, 0)}, 222 {USB_VPI(USB_VENDOR_KONTRON, USB_PRODUCT_KONTRON_JP1082, 223 UDAV_FLAG_NO_PHY)}, 224 }; 225 226 static void 227 udav_attach_post(struct usb_ether *ue) 228 { 229 struct udav_softc *sc = uether_getsc(ue); 230 231 /* reset the adapter */ 232 udav_reset(sc); 233 234 /* Get Ethernet Address */ 235 udav_csr_read(sc, UDAV_PAR, ue->ue_eaddr, ETHER_ADDR_LEN); 236 } 237 238 static int 239 udav_probe(device_t dev) 240 { 241 struct usb_attach_arg *uaa = device_get_ivars(dev); 242 243 if (uaa->usb_mode != USB_MODE_HOST) 244 return (ENXIO); 245 if (uaa->info.bConfigIndex != UDAV_CONFIG_INDEX) 246 return (ENXIO); 247 if (uaa->info.bIfaceIndex != UDAV_IFACE_INDEX) 248 return (ENXIO); 249 250 return (usbd_lookup_id_by_uaa(udav_devs, sizeof(udav_devs), uaa)); 251 } 252 253 static int 254 udav_attach(device_t dev) 255 { 256 struct usb_attach_arg *uaa = device_get_ivars(dev); 257 struct udav_softc *sc = device_get_softc(dev); 258 struct usb_ether *ue = &sc->sc_ue; 259 uint8_t iface_index; 260 int error; 261 262 sc->sc_flags = USB_GET_DRIVER_INFO(uaa); 263 264 device_set_usb_desc(dev); 265 266 mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF); 267 268 iface_index = UDAV_IFACE_INDEX; 269 error = usbd_transfer_setup(uaa->device, &iface_index, 270 sc->sc_xfer, udav_config, UDAV_N_TRANSFER, sc, &sc->sc_mtx); 271 if (error) { 272 device_printf(dev, "allocating USB transfers failed\n"); 273 goto detach; 274 } 275 276 /* 277 * The JP1082 has an unusable PHY and provides no link information. 278 */ 279 if (sc->sc_flags & UDAV_FLAG_NO_PHY) { 280 ue->ue_methods = &udav_ue_methods_nophy; 281 sc->sc_flags |= UDAV_FLAG_LINK; 282 } else { 283 ue->ue_methods = &udav_ue_methods; 284 } 285 286 ue->ue_sc = sc; 287 ue->ue_dev = dev; 288 ue->ue_udev = uaa->device; 289 ue->ue_mtx = &sc->sc_mtx; 290 291 error = uether_ifattach(ue); 292 if (error) { 293 device_printf(dev, "could not attach interface\n"); 294 goto detach; 295 } 296 297 return (0); /* success */ 298 299 detach: 300 udav_detach(dev); 301 return (ENXIO); /* failure */ 302 } 303 304 static int 305 udav_detach(device_t dev) 306 { 307 struct udav_softc *sc = device_get_softc(dev); 308 struct usb_ether *ue = &sc->sc_ue; 309 310 usbd_transfer_unsetup(sc->sc_xfer, UDAV_N_TRANSFER); 311 uether_ifdetach(ue); 312 mtx_destroy(&sc->sc_mtx); 313 314 return (0); 315 } 316 317 #if 0 318 static int 319 udav_mem_read(struct udav_softc *sc, uint16_t offset, void *buf, 320 int len) 321 { 322 struct usb_device_request req; 323 324 len &= 0xff; 325 326 req.bmRequestType = UT_READ_VENDOR_DEVICE; 327 req.bRequest = UDAV_REQ_MEM_READ; 328 USETW(req.wValue, 0x0000); 329 USETW(req.wIndex, offset); 330 USETW(req.wLength, len); 331 332 return (uether_do_request(&sc->sc_ue, &req, buf, 1000)); 333 } 334 335 static int 336 udav_mem_write(struct udav_softc *sc, uint16_t offset, void *buf, 337 int len) 338 { 339 struct usb_device_request req; 340 341 len &= 0xff; 342 343 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 344 req.bRequest = UDAV_REQ_MEM_WRITE; 345 USETW(req.wValue, 0x0000); 346 USETW(req.wIndex, offset); 347 USETW(req.wLength, len); 348 349 return (uether_do_request(&sc->sc_ue, &req, buf, 1000)); 350 } 351 352 static int 353 udav_mem_write1(struct udav_softc *sc, uint16_t offset, 354 uint8_t ch) 355 { 356 struct usb_device_request req; 357 358 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 359 req.bRequest = UDAV_REQ_MEM_WRITE1; 360 USETW(req.wValue, ch); 361 USETW(req.wIndex, offset); 362 USETW(req.wLength, 0x0000); 363 364 return (uether_do_request(&sc->sc_ue, &req, NULL, 1000)); 365 } 366 #endif 367 368 static int 369 udav_csr_read(struct udav_softc *sc, uint16_t offset, void *buf, int len) 370 { 371 struct usb_device_request req; 372 373 len &= 0xff; 374 375 req.bmRequestType = UT_READ_VENDOR_DEVICE; 376 req.bRequest = UDAV_REQ_REG_READ; 377 USETW(req.wValue, 0x0000); 378 USETW(req.wIndex, offset); 379 USETW(req.wLength, len); 380 381 return (uether_do_request(&sc->sc_ue, &req, buf, 1000)); 382 } 383 384 static int 385 udav_csr_write(struct udav_softc *sc, uint16_t offset, void *buf, int len) 386 { 387 struct usb_device_request req; 388 389 offset &= 0xff; 390 len &= 0xff; 391 392 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 393 req.bRequest = UDAV_REQ_REG_WRITE; 394 USETW(req.wValue, 0x0000); 395 USETW(req.wIndex, offset); 396 USETW(req.wLength, len); 397 398 return (uether_do_request(&sc->sc_ue, &req, buf, 1000)); 399 } 400 401 static uint8_t 402 udav_csr_read1(struct udav_softc *sc, uint16_t offset) 403 { 404 uint8_t val; 405 406 udav_csr_read(sc, offset, &val, 1); 407 return (val); 408 } 409 410 static int 411 udav_csr_write1(struct udav_softc *sc, uint16_t offset, 412 uint8_t ch) 413 { 414 struct usb_device_request req; 415 416 offset &= 0xff; 417 418 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 419 req.bRequest = UDAV_REQ_REG_WRITE1; 420 USETW(req.wValue, ch); 421 USETW(req.wIndex, offset); 422 USETW(req.wLength, 0x0000); 423 424 return (uether_do_request(&sc->sc_ue, &req, NULL, 1000)); 425 } 426 427 static void 428 udav_init(struct usb_ether *ue) 429 { 430 struct udav_softc *sc = ue->ue_sc; 431 struct ifnet *ifp = uether_getifp(&sc->sc_ue); 432 433 UDAV_LOCK_ASSERT(sc, MA_OWNED); 434 435 /* 436 * Cancel pending I/O 437 */ 438 udav_stop(ue); 439 440 /* set MAC address */ 441 udav_csr_write(sc, UDAV_PAR, IF_LLADDR(ifp), ETHER_ADDR_LEN); 442 443 /* initialize network control register */ 444 445 /* disable loopback */ 446 UDAV_CLRBIT(sc, UDAV_NCR, UDAV_NCR_LBK0 | UDAV_NCR_LBK1); 447 448 /* Initialize RX control register */ 449 UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_DIS_LONG | UDAV_RCR_DIS_CRC); 450 451 /* load multicast filter and update promiscious mode bit */ 452 udav_setpromisc(ue); 453 454 /* enable RX */ 455 UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_RXEN); 456 457 /* clear POWER_DOWN state of internal PHY */ 458 UDAV_SETBIT(sc, UDAV_GPCR, UDAV_GPCR_GEP_CNTL0); 459 UDAV_CLRBIT(sc, UDAV_GPR, UDAV_GPR_GEPIO0); 460 461 usbd_xfer_set_stall(sc->sc_xfer[UDAV_BULK_DT_WR]); 462 463 ifp->if_drv_flags |= IFF_DRV_RUNNING; 464 udav_start(ue); 465 } 466 467 static void 468 udav_reset(struct udav_softc *sc) 469 { 470 int i; 471 472 /* Select PHY */ 473 #if 1 474 /* 475 * XXX: force select internal phy. 476 * external phy routines are not tested. 477 */ 478 UDAV_CLRBIT(sc, UDAV_NCR, UDAV_NCR_EXT_PHY); 479 #else 480 if (sc->sc_flags & UDAV_EXT_PHY) 481 UDAV_SETBIT(sc, UDAV_NCR, UDAV_NCR_EXT_PHY); 482 else 483 UDAV_CLRBIT(sc, UDAV_NCR, UDAV_NCR_EXT_PHY); 484 #endif 485 486 UDAV_SETBIT(sc, UDAV_NCR, UDAV_NCR_RST); 487 488 for (i = 0; i < UDAV_TX_TIMEOUT; i++) { 489 if (!(udav_csr_read1(sc, UDAV_NCR) & UDAV_NCR_RST)) 490 break; 491 if (uether_pause(&sc->sc_ue, hz / 100)) 492 break; 493 } 494 495 uether_pause(&sc->sc_ue, hz / 100); 496 } 497 498 #define UDAV_BITS 6 499 static void 500 udav_setmulti(struct usb_ether *ue) 501 { 502 struct udav_softc *sc = ue->ue_sc; 503 struct ifnet *ifp = uether_getifp(&sc->sc_ue); 504 struct ifmultiaddr *ifma; 505 uint8_t hashtbl[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; 506 int h = 0; 507 508 UDAV_LOCK_ASSERT(sc, MA_OWNED); 509 510 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) { 511 UDAV_SETBIT(sc, UDAV_RCR, UDAV_RCR_ALL|UDAV_RCR_PRMSC); 512 return; 513 } 514 515 /* first, zot all the existing hash bits */ 516 memset(hashtbl, 0x00, sizeof(hashtbl)); 517 hashtbl[7] |= 0x80; /* broadcast address */ 518 udav_csr_write(sc, UDAV_MAR, hashtbl, sizeof(hashtbl)); 519 520 /* now program new ones */ 521 if_maddr_rlock(ifp); 522 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) 523 { 524 if (ifma->ifma_addr->sa_family != AF_LINK) 525 continue; 526 h = ether_crc32_be(LLADDR((struct sockaddr_dl *) 527 ifma->ifma_addr), ETHER_ADDR_LEN) >> 26; 528 hashtbl[h / 8] |= 1 << (h % 8); 529 } 530 if_maddr_runlock(ifp); 531 532 /* disable all multicast */ 533 UDAV_CLRBIT(sc, UDAV_RCR, UDAV_RCR_ALL); 534 535 /* write hash value to the register */ 536 udav_csr_write(sc, UDAV_MAR, hashtbl, sizeof(hashtbl)); 537 } 538 539 static void 540 udav_setpromisc(struct usb_ether *ue) 541 { 542 struct udav_softc *sc = ue->ue_sc; 543 struct ifnet *ifp = uether_getifp(&sc->sc_ue); 544 uint8_t rxmode; 545 546 rxmode = udav_csr_read1(sc, UDAV_RCR); 547 rxmode &= ~(UDAV_RCR_ALL | UDAV_RCR_PRMSC); 548 549 if (ifp->if_flags & IFF_PROMISC) 550 rxmode |= UDAV_RCR_ALL | UDAV_RCR_PRMSC; 551 else if (ifp->if_flags & IFF_ALLMULTI) 552 rxmode |= UDAV_RCR_ALL; 553 554 /* write new mode bits */ 555 udav_csr_write1(sc, UDAV_RCR, rxmode); 556 } 557 558 static void 559 udav_start(struct usb_ether *ue) 560 { 561 struct udav_softc *sc = ue->ue_sc; 562 563 /* 564 * start the USB transfers, if not already started: 565 */ 566 usbd_transfer_start(sc->sc_xfer[UDAV_INTR_DT_RD]); 567 usbd_transfer_start(sc->sc_xfer[UDAV_BULK_DT_RD]); 568 usbd_transfer_start(sc->sc_xfer[UDAV_BULK_DT_WR]); 569 } 570 571 static void 572 udav_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error) 573 { 574 struct udav_softc *sc = usbd_xfer_softc(xfer); 575 struct ifnet *ifp = uether_getifp(&sc->sc_ue); 576 struct usb_page_cache *pc; 577 struct mbuf *m; 578 int extra_len; 579 int temp_len; 580 uint8_t buf[2]; 581 582 switch (USB_GET_STATE(xfer)) { 583 case USB_ST_TRANSFERRED: 584 DPRINTFN(11, "transfer complete\n"); 585 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); 586 587 /* FALLTHROUGH */ 588 case USB_ST_SETUP: 589 tr_setup: 590 if ((sc->sc_flags & UDAV_FLAG_LINK) == 0) { 591 /* 592 * don't send anything if there is no link ! 593 */ 594 return; 595 } 596 IFQ_DRV_DEQUEUE(&ifp->if_snd, m); 597 598 if (m == NULL) 599 return; 600 if (m->m_pkthdr.len > MCLBYTES) 601 m->m_pkthdr.len = MCLBYTES; 602 if (m->m_pkthdr.len < UDAV_MIN_FRAME_LEN) { 603 extra_len = UDAV_MIN_FRAME_LEN - m->m_pkthdr.len; 604 } else { 605 extra_len = 0; 606 } 607 608 temp_len = (m->m_pkthdr.len + extra_len); 609 610 /* 611 * the frame length is specified in the first 2 bytes of the 612 * buffer 613 */ 614 buf[0] = (uint8_t)(temp_len); 615 buf[1] = (uint8_t)(temp_len >> 8); 616 617 temp_len += 2; 618 619 pc = usbd_xfer_get_frame(xfer, 0); 620 usbd_copy_in(pc, 0, buf, 2); 621 usbd_m_copy_in(pc, 2, m, 0, m->m_pkthdr.len); 622 623 if (extra_len) 624 usbd_frame_zero(pc, temp_len - extra_len, extra_len); 625 /* 626 * if there's a BPF listener, bounce a copy 627 * of this frame to him: 628 */ 629 BPF_MTAP(ifp, m); 630 631 m_freem(m); 632 633 usbd_xfer_set_frame_len(xfer, 0, temp_len); 634 usbd_transfer_submit(xfer); 635 return; 636 637 default: /* Error */ 638 DPRINTFN(11, "transfer error, %s\n", 639 usbd_errstr(error)); 640 641 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 642 643 if (error != USB_ERR_CANCELLED) { 644 /* try to clear stall first */ 645 usbd_xfer_set_stall(xfer); 646 goto tr_setup; 647 } 648 return; 649 } 650 } 651 652 static void 653 udav_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error) 654 { 655 struct udav_softc *sc = usbd_xfer_softc(xfer); 656 struct usb_ether *ue = &sc->sc_ue; 657 struct ifnet *ifp = uether_getifp(ue); 658 struct usb_page_cache *pc; 659 struct udav_rxpkt stat; 660 int len; 661 int actlen; 662 663 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 664 665 switch (USB_GET_STATE(xfer)) { 666 case USB_ST_TRANSFERRED: 667 668 if (actlen < (int)(sizeof(stat) + ETHER_CRC_LEN)) { 669 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 670 goto tr_setup; 671 } 672 pc = usbd_xfer_get_frame(xfer, 0); 673 usbd_copy_out(pc, 0, &stat, sizeof(stat)); 674 actlen -= sizeof(stat); 675 len = min(actlen, le16toh(stat.pktlen)); 676 len -= ETHER_CRC_LEN; 677 678 if (stat.rxstat & UDAV_RSR_LCS) { 679 if_inc_counter(ifp, IFCOUNTER_COLLISIONS, 1); 680 goto tr_setup; 681 } 682 if (stat.rxstat & UDAV_RSR_ERR) { 683 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 684 goto tr_setup; 685 } 686 uether_rxbuf(ue, pc, sizeof(stat), len); 687 /* FALLTHROUGH */ 688 case USB_ST_SETUP: 689 tr_setup: 690 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 691 usbd_transfer_submit(xfer); 692 uether_rxflush(ue); 693 return; 694 695 default: /* Error */ 696 DPRINTF("bulk read error, %s\n", 697 usbd_errstr(error)); 698 699 if (error != USB_ERR_CANCELLED) { 700 /* try to clear stall first */ 701 usbd_xfer_set_stall(xfer); 702 goto tr_setup; 703 } 704 return; 705 } 706 } 707 708 static void 709 udav_intr_callback(struct usb_xfer *xfer, usb_error_t error) 710 { 711 switch (USB_GET_STATE(xfer)) { 712 case USB_ST_TRANSFERRED: 713 case USB_ST_SETUP: 714 tr_setup: 715 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 716 usbd_transfer_submit(xfer); 717 return; 718 719 default: /* Error */ 720 if (error != USB_ERR_CANCELLED) { 721 /* try to clear stall first */ 722 usbd_xfer_set_stall(xfer); 723 goto tr_setup; 724 } 725 return; 726 } 727 } 728 729 static void 730 udav_stop(struct usb_ether *ue) 731 { 732 struct udav_softc *sc = ue->ue_sc; 733 struct ifnet *ifp = uether_getifp(&sc->sc_ue); 734 735 UDAV_LOCK_ASSERT(sc, MA_OWNED); 736 737 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 738 if (!(sc->sc_flags & UDAV_FLAG_NO_PHY)) 739 sc->sc_flags &= ~UDAV_FLAG_LINK; 740 741 /* 742 * stop all the transfers, if not already stopped: 743 */ 744 usbd_transfer_stop(sc->sc_xfer[UDAV_BULK_DT_WR]); 745 usbd_transfer_stop(sc->sc_xfer[UDAV_BULK_DT_RD]); 746 usbd_transfer_stop(sc->sc_xfer[UDAV_INTR_DT_RD]); 747 748 udav_reset(sc); 749 } 750 751 static int 752 udav_ifmedia_upd(struct ifnet *ifp) 753 { 754 struct udav_softc *sc = ifp->if_softc; 755 struct mii_data *mii = GET_MII(sc); 756 struct mii_softc *miisc; 757 int error; 758 759 UDAV_LOCK_ASSERT(sc, MA_OWNED); 760 761 sc->sc_flags &= ~UDAV_FLAG_LINK; 762 LIST_FOREACH(miisc, &mii->mii_phys, mii_list) 763 PHY_RESET(miisc); 764 error = mii_mediachg(mii); 765 return (error); 766 } 767 768 static void 769 udav_ifmedia_status(struct ifnet *ifp, struct ifmediareq *ifmr) 770 { 771 struct udav_softc *sc = ifp->if_softc; 772 struct mii_data *mii = GET_MII(sc); 773 774 UDAV_LOCK(sc); 775 mii_pollstat(mii); 776 ifmr->ifm_active = mii->mii_media_active; 777 ifmr->ifm_status = mii->mii_media_status; 778 UDAV_UNLOCK(sc); 779 } 780 781 static void 782 udav_tick(struct usb_ether *ue) 783 { 784 struct udav_softc *sc = ue->ue_sc; 785 struct mii_data *mii = GET_MII(sc); 786 787 UDAV_LOCK_ASSERT(sc, MA_OWNED); 788 789 mii_tick(mii); 790 if ((sc->sc_flags & UDAV_FLAG_LINK) == 0 791 && mii->mii_media_status & IFM_ACTIVE && 792 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) { 793 sc->sc_flags |= UDAV_FLAG_LINK; 794 udav_start(ue); 795 } 796 } 797 798 static int 799 udav_miibus_readreg(device_t dev, int phy, int reg) 800 { 801 struct udav_softc *sc = device_get_softc(dev); 802 uint16_t data16; 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 /* select PHY operation and start read command */ 819 udav_csr_write1(sc, UDAV_EPCR, UDAV_EPCR_EPOS | UDAV_EPCR_ERPRR); 820 821 /* XXX: should we wait? */ 822 823 /* end read command */ 824 UDAV_CLRBIT(sc, UDAV_EPCR, UDAV_EPCR_ERPRR); 825 826 /* retrieve the result from data registers */ 827 udav_csr_read(sc, UDAV_EPDRL, val, 2); 828 829 data16 = (val[0] | (val[1] << 8)); 830 831 DPRINTFN(11, "phy=%d reg=0x%04x => 0x%04x\n", 832 phy, reg, data16); 833 834 if (!locked) 835 UDAV_UNLOCK(sc); 836 return (data16); 837 } 838 839 static int 840 udav_miibus_writereg(device_t dev, int phy, int reg, int data) 841 { 842 struct udav_softc *sc = device_get_softc(dev); 843 uint8_t val[2]; 844 int locked; 845 846 /* XXX: one PHY only for the internal PHY */ 847 if (phy != 0) 848 return (0); 849 850 locked = mtx_owned(&sc->sc_mtx); 851 if (!locked) 852 UDAV_LOCK(sc); 853 854 /* select internal PHY and set PHY register address */ 855 udav_csr_write1(sc, UDAV_EPAR, 856 UDAV_EPAR_PHY_ADR0 | (reg & UDAV_EPAR_EROA_MASK)); 857 858 /* put the value to the data registers */ 859 val[0] = (data & 0xff); 860 val[1] = (data >> 8) & 0xff; 861 udav_csr_write(sc, UDAV_EPDRL, val, 2); 862 863 /* select PHY operation and start write command */ 864 udav_csr_write1(sc, UDAV_EPCR, UDAV_EPCR_EPOS | UDAV_EPCR_ERPRW); 865 866 /* XXX: should we wait? */ 867 868 /* end write command */ 869 UDAV_CLRBIT(sc, UDAV_EPCR, UDAV_EPCR_ERPRW); 870 871 if (!locked) 872 UDAV_UNLOCK(sc); 873 return (0); 874 } 875 876 static void 877 udav_miibus_statchg(device_t dev) 878 { 879 /* nothing to do */ 880 } 881