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