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