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