1 /* $NetBSD: if_url.c,v 1.58 2018/06/26 06:48:02 msaitoh Exp $ */ 2 3 /* 4 * Copyright (c) 2001, 2002 5 * Shingo WATANABE <nabe@nabechan.org>. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the author nor the names of any co-contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 */ 32 33 /* 34 * The RTL8150L(Realtek USB to fast ethernet controller) spec can be found at 35 * ftp://ftp.realtek.com.tw/lancard/data_sheet/8150/8150v14.pdf 36 * ftp://152.104.125.40/lancard/data_sheet/8150/8150v14.pdf 37 */ 38 39 /* 40 * TODO: 41 * Interrupt Endpoint support 42 * External PHYs 43 * powerhook() support? 44 */ 45 46 #include <sys/cdefs.h> 47 __KERNEL_RCSID(0, "$NetBSD: if_url.c,v 1.58 2018/06/26 06:48:02 msaitoh Exp $"); 48 49 #ifdef _KERNEL_OPT 50 #include "opt_inet.h" 51 #include "opt_usb.h" 52 #endif 53 54 #include <sys/param.h> 55 #include <sys/systm.h> 56 #include <sys/rwlock.h> 57 #include <sys/mbuf.h> 58 #include <sys/kernel.h> 59 #include <sys/socket.h> 60 61 #include <sys/device.h> 62 #include <sys/rndsource.h> 63 64 #include <net/if.h> 65 #include <net/if_arp.h> 66 #include <net/if_dl.h> 67 #include <net/if_media.h> 68 69 #include <net/bpf.h> 70 71 #include <net/if_ether.h> 72 #ifdef INET 73 #include <netinet/in.h> 74 #include <netinet/if_inarp.h> 75 #endif 76 77 #include <dev/mii/mii.h> 78 #include <dev/mii/miivar.h> 79 #include <dev/mii/urlphyreg.h> 80 81 #include <dev/usb/usb.h> 82 #include <dev/usb/usbdi.h> 83 #include <dev/usb/usbdi_util.h> 84 #include <dev/usb/usbdevs.h> 85 86 #include <dev/usb/if_urlreg.h> 87 88 89 /* Function declarations */ 90 int url_match(device_t, cfdata_t, void *); 91 void url_attach(device_t, device_t, void *); 92 int url_detach(device_t, int); 93 int url_activate(device_t, enum devact); 94 extern struct cfdriver url_cd; 95 CFATTACH_DECL_NEW(url, sizeof(struct url_softc), url_match, url_attach, 96 url_detach, url_activate); 97 98 Static int url_openpipes(struct url_softc *); 99 Static int url_rx_list_init(struct url_softc *); 100 Static int url_tx_list_init(struct url_softc *); 101 Static int url_newbuf(struct url_softc *, struct url_chain *, struct mbuf *); 102 Static void url_start(struct ifnet *); 103 Static int url_send(struct url_softc *, struct mbuf *, int); 104 Static void url_txeof(struct usbd_xfer *, void *, usbd_status); 105 Static void url_rxeof(struct usbd_xfer *, void *, usbd_status); 106 Static void url_tick(void *); 107 Static void url_tick_task(void *); 108 Static int url_ioctl(struct ifnet *, u_long, void *); 109 Static void url_stop_task(struct url_softc *); 110 Static void url_stop(struct ifnet *, int); 111 Static void url_watchdog(struct ifnet *); 112 Static int url_ifmedia_change(struct ifnet *); 113 Static void url_ifmedia_status(struct ifnet *, struct ifmediareq *); 114 Static void url_lock_mii(struct url_softc *); 115 Static void url_unlock_mii(struct url_softc *); 116 Static int url_int_miibus_readreg(device_t, int, int); 117 Static void url_int_miibus_writereg(device_t, int, int, int); 118 Static void url_miibus_statchg(struct ifnet *); 119 Static int url_init(struct ifnet *); 120 Static void url_setmulti(struct url_softc *); 121 Static void url_reset(struct url_softc *); 122 123 Static int url_csr_read_1(struct url_softc *, int); 124 Static int url_csr_read_2(struct url_softc *, int); 125 Static int url_csr_write_1(struct url_softc *, int, int); 126 Static int url_csr_write_2(struct url_softc *, int, int); 127 Static int url_csr_write_4(struct url_softc *, int, int); 128 Static int url_mem(struct url_softc *, int, int, void *, int); 129 130 /* Macros */ 131 #ifdef URL_DEBUG 132 #define DPRINTF(x) if (urldebug) printf x 133 #define DPRINTFN(n,x) if (urldebug >= (n)) printf x 134 int urldebug = 0; 135 #else 136 #define DPRINTF(x) 137 #define DPRINTFN(n,x) 138 #endif 139 140 #define URL_SETBIT(sc, reg, x) \ 141 url_csr_write_1(sc, reg, url_csr_read_1(sc, reg) | (x)) 142 143 #define URL_SETBIT2(sc, reg, x) \ 144 url_csr_write_2(sc, reg, url_csr_read_2(sc, reg) | (x)) 145 146 #define URL_CLRBIT(sc, reg, x) \ 147 url_csr_write_1(sc, reg, url_csr_read_1(sc, reg) & ~(x)) 148 149 #define URL_CLRBIT2(sc, reg, x) \ 150 url_csr_write_2(sc, reg, url_csr_read_2(sc, reg) & ~(x)) 151 152 static const struct url_type { 153 struct usb_devno url_dev; 154 uint16_t url_flags; 155 #define URL_EXT_PHY 0x0001 156 } url_devs [] = { 157 /* MELCO LUA-KTX */ 158 {{ USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUAKTX }, 0}, 159 /* Realtek RTL8150L Generic (GREEN HOUSE USBKR100) */ 160 {{ USB_VENDOR_REALTEK, USB_PRODUCT_REALTEK_RTL8150L}, 0}, 161 /* Longshine LCS-8138TX */ 162 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_LCS8138TX}, 0}, 163 /* Micronet SP128AR */ 164 {{ USB_VENDOR_MICRONET, USB_PRODUCT_MICRONET_SP128AR}, 0}, 165 /* OQO model 01 */ 166 {{ USB_VENDOR_OQO, USB_PRODUCT_OQO_ETHER01}, 0}, 167 }; 168 #define url_lookup(v, p) ((const struct url_type *)usb_lookup(url_devs, v, p)) 169 170 171 /* Probe */ 172 int 173 url_match(device_t parent, cfdata_t match, void *aux) 174 { 175 struct usb_attach_arg *uaa = aux; 176 177 return url_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ? 178 UMATCH_VENDOR_PRODUCT : UMATCH_NONE; 179 } 180 /* Attach */ 181 void 182 url_attach(device_t parent, device_t self, void *aux) 183 { 184 struct url_softc *sc = device_private(self); 185 struct usb_attach_arg *uaa = aux; 186 struct usbd_device *dev = uaa->uaa_device; 187 struct usbd_interface *iface; 188 usbd_status err; 189 usb_interface_descriptor_t *id; 190 usb_endpoint_descriptor_t *ed; 191 char *devinfop; 192 struct ifnet *ifp; 193 struct mii_data *mii; 194 u_char eaddr[ETHER_ADDR_LEN]; 195 int i, s; 196 197 sc->sc_dev = self; 198 199 aprint_naive("\n"); 200 aprint_normal("\n"); 201 202 devinfop = usbd_devinfo_alloc(dev, 0); 203 aprint_normal_dev(self, "%s\n", devinfop); 204 usbd_devinfo_free(devinfop); 205 206 /* Move the device into the configured state. */ 207 err = usbd_set_config_no(dev, URL_CONFIG_NO, 1); 208 if (err) { 209 aprint_error_dev(self, "failed to set configuration" 210 ", err=%s\n", usbd_errstr(err)); 211 goto bad; 212 } 213 214 usb_init_task(&sc->sc_tick_task, url_tick_task, sc, 0); 215 rw_init(&sc->sc_mii_rwlock); 216 usb_init_task(&sc->sc_stop_task, (void (*)(void *))url_stop_task, sc, 0); 217 218 /* get control interface */ 219 err = usbd_device2interface_handle(dev, URL_IFACE_INDEX, &iface); 220 if (err) { 221 aprint_error_dev(self, "failed to get interface, err=%s\n", 222 usbd_errstr(err)); 223 goto bad; 224 } 225 226 sc->sc_udev = dev; 227 sc->sc_ctl_iface = iface; 228 sc->sc_flags = url_lookup(uaa->uaa_vendor, uaa->uaa_product)->url_flags; 229 230 /* get interface descriptor */ 231 id = usbd_get_interface_descriptor(sc->sc_ctl_iface); 232 233 /* find endpoints */ 234 sc->sc_bulkin_no = sc->sc_bulkout_no = sc->sc_intrin_no = -1; 235 for (i = 0; i < id->bNumEndpoints; i++) { 236 ed = usbd_interface2endpoint_descriptor(sc->sc_ctl_iface, i); 237 if (ed == NULL) { 238 aprint_error_dev(self, 239 "couldn't get endpoint %d\n", i); 240 goto bad; 241 } 242 if ((ed->bmAttributes & UE_XFERTYPE) == UE_BULK && 243 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN) 244 sc->sc_bulkin_no = ed->bEndpointAddress; /* RX */ 245 else if ((ed->bmAttributes & UE_XFERTYPE) == UE_BULK && 246 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT) 247 sc->sc_bulkout_no = ed->bEndpointAddress; /* TX */ 248 else if ((ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT && 249 UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN) 250 sc->sc_intrin_no = ed->bEndpointAddress; /* Status */ 251 } 252 253 if (sc->sc_bulkin_no == -1 || sc->sc_bulkout_no == -1 || 254 sc->sc_intrin_no == -1) { 255 aprint_error_dev(self, "missing endpoint\n"); 256 goto bad; 257 } 258 259 s = splnet(); 260 261 /* reset the adapter */ 262 url_reset(sc); 263 264 /* Get Ethernet Address */ 265 err = url_mem(sc, URL_CMD_READMEM, URL_IDR0, (void *)eaddr, 266 ETHER_ADDR_LEN); 267 if (err) { 268 aprint_error_dev(self, "read MAC address failed\n"); 269 splx(s); 270 goto bad; 271 } 272 273 /* Print Ethernet Address */ 274 aprint_normal_dev(self, "Ethernet address %s\n", ether_sprintf(eaddr)); 275 276 /* initialize interface information */ 277 ifp = GET_IFP(sc); 278 ifp->if_softc = sc; 279 ifp->if_mtu = ETHERMTU; 280 strlcpy(ifp->if_xname, device_xname(self), IFNAMSIZ); 281 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 282 ifp->if_start = url_start; 283 ifp->if_ioctl = url_ioctl; 284 ifp->if_watchdog = url_watchdog; 285 ifp->if_init = url_init; 286 ifp->if_stop = url_stop; 287 288 IFQ_SET_READY(&ifp->if_snd); 289 290 /* 291 * Do ifmedia setup. 292 */ 293 mii = &sc->sc_mii; 294 mii->mii_ifp = ifp; 295 mii->mii_readreg = url_int_miibus_readreg; 296 mii->mii_writereg = url_int_miibus_writereg; 297 #if 0 298 if (sc->sc_flags & URL_EXT_PHY) { 299 mii->mii_readreg = url_ext_miibus_readreg; 300 mii->mii_writereg = url_ext_miibus_writereg; 301 } 302 #endif 303 mii->mii_statchg = url_miibus_statchg; 304 mii->mii_flags = MIIF_AUTOTSLEEP; 305 sc->sc_ec.ec_mii = mii; 306 ifmedia_init(&mii->mii_media, 0, 307 url_ifmedia_change, url_ifmedia_status); 308 mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0); 309 if (LIST_FIRST(&mii->mii_phys) == NULL) { 310 ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL); 311 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE); 312 } else 313 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO); 314 315 /* attach the interface */ 316 if_attach(ifp); 317 ether_ifattach(ifp, eaddr); 318 319 rnd_attach_source(&sc->rnd_source, device_xname(self), 320 RND_TYPE_NET, RND_FLAG_DEFAULT); 321 322 callout_init(&sc->sc_stat_ch, 0); 323 sc->sc_attached = 1; 324 splx(s); 325 326 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, dev, sc->sc_dev); 327 328 return; 329 330 bad: 331 sc->sc_dying = 1; 332 return; 333 } 334 335 /* detach */ 336 int 337 url_detach(device_t self, int flags) 338 { 339 struct url_softc *sc = device_private(self); 340 struct ifnet *ifp = GET_IFP(sc); 341 int s; 342 343 DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); 344 345 /* Detached before attached finished */ 346 if (!sc->sc_attached) 347 return 0; 348 349 callout_stop(&sc->sc_stat_ch); 350 351 /* Remove any pending tasks */ 352 usb_rem_task(sc->sc_udev, &sc->sc_tick_task); 353 usb_rem_task(sc->sc_udev, &sc->sc_stop_task); 354 355 s = splusb(); 356 357 if (--sc->sc_refcnt >= 0) { 358 /* Wait for processes to go away */ 359 usb_detach_waitold(sc->sc_dev); 360 } 361 362 if (ifp->if_flags & IFF_RUNNING) 363 url_stop(GET_IFP(sc), 1); 364 365 rnd_detach_source(&sc->rnd_source); 366 mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY); 367 ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY); 368 ether_ifdetach(ifp); 369 if_detach(ifp); 370 371 #ifdef DIAGNOSTIC 372 if (sc->sc_pipe_tx != NULL) 373 aprint_debug_dev(self, "detach has active tx endpoint.\n"); 374 if (sc->sc_pipe_rx != NULL) 375 aprint_debug_dev(self, "detach has active rx endpoint.\n"); 376 if (sc->sc_pipe_intr != NULL) 377 aprint_debug_dev(self, "detach has active intr endpoint.\n"); 378 #endif 379 380 sc->sc_attached = 0; 381 382 splx(s); 383 384 rw_destroy(&sc->sc_mii_rwlock); 385 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev); 386 387 return 0; 388 } 389 390 /* read/write memory */ 391 Static int 392 url_mem(struct url_softc *sc, int cmd, int offset, void *buf, int len) 393 { 394 usb_device_request_t req; 395 usbd_status err; 396 397 if (sc == NULL) 398 return 0; 399 400 DPRINTFN(0x200, 401 ("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); 402 403 if (sc->sc_dying) 404 return 0; 405 406 if (cmd == URL_CMD_READMEM) 407 req.bmRequestType = UT_READ_VENDOR_DEVICE; 408 else 409 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 410 req.bRequest = URL_REQ_MEM; 411 USETW(req.wValue, offset); 412 USETW(req.wIndex, 0x0000); 413 USETW(req.wLength, len); 414 415 sc->sc_refcnt++; 416 err = usbd_do_request(sc->sc_udev, &req, buf); 417 if (--sc->sc_refcnt < 0) 418 usb_detach_wakeupold(sc->sc_dev); 419 if (err) { 420 DPRINTF(("%s: url_mem(): %s failed. off=%04x, err=%d\n", 421 device_xname(sc->sc_dev), 422 cmd == URL_CMD_READMEM ? "read" : "write", 423 offset, err)); 424 } 425 426 return err; 427 } 428 429 /* read 1byte from register */ 430 Static int 431 url_csr_read_1(struct url_softc *sc, int reg) 432 { 433 uint8_t val = 0; 434 435 DPRINTFN(0x100, 436 ("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); 437 438 if (sc->sc_dying) 439 return 0; 440 441 return url_mem(sc, URL_CMD_READMEM, reg, &val, 1) ? 0 : val; 442 } 443 444 /* read 2bytes from register */ 445 Static int 446 url_csr_read_2(struct url_softc *sc, int reg) 447 { 448 uWord val; 449 450 DPRINTFN(0x100, 451 ("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); 452 453 if (sc->sc_dying) 454 return 0; 455 456 USETW(val, 0); 457 return url_mem(sc, URL_CMD_READMEM, reg, &val, 2) ? 0 : UGETW(val); 458 } 459 460 /* write 1byte to register */ 461 Static int 462 url_csr_write_1(struct url_softc *sc, int reg, int aval) 463 { 464 uint8_t val = aval; 465 466 DPRINTFN(0x100, 467 ("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); 468 469 if (sc->sc_dying) 470 return 0; 471 472 return url_mem(sc, URL_CMD_WRITEMEM, reg, &val, 1) ? -1 : 0; 473 } 474 475 /* write 2bytes to register */ 476 Static int 477 url_csr_write_2(struct url_softc *sc, int reg, int aval) 478 { 479 uWord val; 480 481 DPRINTFN(0x100, 482 ("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); 483 484 USETW(val, aval); 485 486 if (sc->sc_dying) 487 return 0; 488 489 return url_mem(sc, URL_CMD_WRITEMEM, reg, &val, 2) ? -1 : 0; 490 } 491 492 /* write 4bytes to register */ 493 Static int 494 url_csr_write_4(struct url_softc *sc, int reg, int aval) 495 { 496 uDWord val; 497 498 DPRINTFN(0x100, 499 ("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); 500 501 USETDW(val, aval); 502 503 if (sc->sc_dying) 504 return 0; 505 506 return url_mem(sc, URL_CMD_WRITEMEM, reg, &val, 4) ? -1 : 0; 507 } 508 509 Static int 510 url_init(struct ifnet *ifp) 511 { 512 struct url_softc *sc = ifp->if_softc; 513 struct mii_data *mii = GET_MII(sc); 514 const u_char *eaddr; 515 int i, rc, s; 516 517 DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); 518 519 if (sc->sc_dying) 520 return EIO; 521 522 s = splnet(); 523 524 /* Cancel pending I/O and free all TX/RX buffers */ 525 url_stop(ifp, 1); 526 527 eaddr = CLLADDR(ifp->if_sadl); 528 for (i = 0; i < ETHER_ADDR_LEN; i++) 529 url_csr_write_1(sc, URL_IDR0 + i, eaddr[i]); 530 531 /* Init transmission control register */ 532 URL_CLRBIT(sc, URL_TCR, 533 URL_TCR_TXRR1 | URL_TCR_TXRR0 | 534 URL_TCR_IFG1 | URL_TCR_IFG0 | 535 URL_TCR_NOCRC); 536 537 /* Init receive control register */ 538 URL_SETBIT2(sc, URL_RCR, URL_RCR_TAIL | URL_RCR_AD); 539 if (ifp->if_flags & IFF_BROADCAST) 540 URL_SETBIT2(sc, URL_RCR, URL_RCR_AB); 541 else 542 URL_CLRBIT2(sc, URL_RCR, URL_RCR_AB); 543 544 /* If we want promiscuous mode, accept all physical frames. */ 545 if (ifp->if_flags & IFF_PROMISC) 546 URL_SETBIT2(sc, URL_RCR, URL_RCR_AAM|URL_RCR_AAP); 547 else 548 URL_CLRBIT2(sc, URL_RCR, URL_RCR_AAM|URL_RCR_AAP); 549 550 551 /* Load the multicast filter */ 552 url_setmulti(sc); 553 554 /* Enable RX and TX */ 555 URL_SETBIT(sc, URL_CR, URL_CR_TE | URL_CR_RE); 556 557 if ((rc = mii_mediachg(mii)) == ENXIO) 558 rc = 0; 559 else if (rc != 0) 560 goto out; 561 562 if (sc->sc_pipe_tx == NULL || sc->sc_pipe_rx == NULL) { 563 if (url_openpipes(sc)) { 564 splx(s); 565 return EIO; 566 } 567 } 568 /* Initialize transmit ring */ 569 if (url_tx_list_init(sc)) { 570 printf("%s: tx list init failed\n", device_xname(sc->sc_dev)); 571 splx(s); 572 return EIO; 573 } 574 575 /* Initialize receive ring */ 576 if (url_rx_list_init(sc)) { 577 printf("%s: rx list init failed\n", device_xname(sc->sc_dev)); 578 splx(s); 579 return EIO; 580 } 581 /* Start up the receive pipe. */ 582 for (i = 0; i < URL_RX_LIST_CNT; i++) { 583 struct url_chain *c = &sc->sc_cdata.url_rx_chain[i]; 584 585 usbd_setup_xfer(c->url_xfer, c, c->url_buf, URL_BUFSZ, 586 USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, url_rxeof); 587 (void)usbd_transfer(c->url_xfer); 588 DPRINTF(("%s: %s: start read\n", device_xname(sc->sc_dev), 589 __func__)); 590 } 591 592 ifp->if_flags |= IFF_RUNNING; 593 ifp->if_flags &= ~IFF_OACTIVE; 594 595 callout_reset(&sc->sc_stat_ch, hz, url_tick, sc); 596 597 out: 598 splx(s); 599 return rc; 600 } 601 602 Static void 603 url_reset(struct url_softc *sc) 604 { 605 int i; 606 607 DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); 608 609 if (sc->sc_dying) 610 return; 611 612 URL_SETBIT(sc, URL_CR, URL_CR_SOFT_RST); 613 614 for (i = 0; i < URL_TX_TIMEOUT; i++) { 615 if (!(url_csr_read_1(sc, URL_CR) & URL_CR_SOFT_RST)) 616 break; 617 delay(10); /* XXX */ 618 } 619 620 delay(10000); /* XXX */ 621 } 622 623 int 624 url_activate(device_t self, enum devact act) 625 { 626 struct url_softc *sc = device_private(self); 627 628 DPRINTF(("%s: %s: enter, act=%d\n", device_xname(sc->sc_dev), 629 __func__, act)); 630 631 switch (act) { 632 case DVACT_DEACTIVATE: 633 if_deactivate(&sc->sc_ec.ec_if); 634 sc->sc_dying = 1; 635 return 0; 636 default: 637 return EOPNOTSUPP; 638 } 639 } 640 641 #define url_calchash(addr) (ether_crc32_be((addr), ETHER_ADDR_LEN) >> 26) 642 643 644 Static void 645 url_setmulti(struct url_softc *sc) 646 { 647 struct ifnet *ifp; 648 struct ether_multi *enm; 649 struct ether_multistep step; 650 uint32_t hashes[2] = { 0, 0 }; 651 int h = 0; 652 int mcnt = 0; 653 654 DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); 655 656 if (sc->sc_dying) 657 return; 658 659 ifp = GET_IFP(sc); 660 661 if (ifp->if_flags & IFF_PROMISC) { 662 URL_SETBIT2(sc, URL_RCR, URL_RCR_AAM|URL_RCR_AAP); 663 return; 664 } else if (ifp->if_flags & IFF_ALLMULTI) { 665 allmulti: 666 ifp->if_flags |= IFF_ALLMULTI; 667 URL_SETBIT2(sc, URL_RCR, URL_RCR_AAM); 668 URL_CLRBIT2(sc, URL_RCR, URL_RCR_AAP); 669 return; 670 } 671 672 /* first, zot all the existing hash bits */ 673 url_csr_write_4(sc, URL_MAR0, 0); 674 url_csr_write_4(sc, URL_MAR4, 0); 675 676 /* now program new ones */ 677 ETHER_FIRST_MULTI(step, &sc->sc_ec, enm); 678 while (enm != NULL) { 679 if (memcmp(enm->enm_addrlo, enm->enm_addrhi, 680 ETHER_ADDR_LEN) != 0) 681 goto allmulti; 682 683 h = url_calchash(enm->enm_addrlo); 684 if (h < 32) 685 hashes[0] |= (1 << h); 686 else 687 hashes[1] |= (1 << (h -32)); 688 mcnt++; 689 ETHER_NEXT_MULTI(step, enm); 690 } 691 692 ifp->if_flags &= ~IFF_ALLMULTI; 693 694 URL_CLRBIT2(sc, URL_RCR, URL_RCR_AAM|URL_RCR_AAP); 695 696 if (mcnt){ 697 URL_SETBIT2(sc, URL_RCR, URL_RCR_AM); 698 } else { 699 URL_CLRBIT2(sc, URL_RCR, URL_RCR_AM); 700 } 701 url_csr_write_4(sc, URL_MAR0, hashes[0]); 702 url_csr_write_4(sc, URL_MAR4, hashes[1]); 703 } 704 705 Static int 706 url_openpipes(struct url_softc *sc) 707 { 708 usbd_status err; 709 int error = 0; 710 711 if (sc->sc_dying) 712 return EIO; 713 714 sc->sc_refcnt++; 715 716 /* Open RX pipe */ 717 err = usbd_open_pipe(sc->sc_ctl_iface, sc->sc_bulkin_no, 718 USBD_EXCLUSIVE_USE, &sc->sc_pipe_rx); 719 if (err) { 720 printf("%s: open rx pipe failed: %s\n", 721 device_xname(sc->sc_dev), usbd_errstr(err)); 722 error = EIO; 723 goto done; 724 } 725 726 /* Open TX pipe */ 727 err = usbd_open_pipe(sc->sc_ctl_iface, sc->sc_bulkout_no, 728 USBD_EXCLUSIVE_USE, &sc->sc_pipe_tx); 729 if (err) { 730 printf("%s: open tx pipe failed: %s\n", 731 device_xname(sc->sc_dev), usbd_errstr(err)); 732 error = EIO; 733 goto done; 734 } 735 736 #if 0 737 /* XXX: interrupt endpoint is not yet supported */ 738 /* Open Interrupt pipe */ 739 err = usbd_open_pipe_intr(sc->sc_ctl_iface, sc->sc_intrin_no, 740 USBD_EXCLUSIVE_USE, &sc->sc_pipe_intr, sc, 741 &sc->sc_cdata.url_ibuf, URL_INTR_PKGLEN, 742 url_intr, USBD_DEFAULT_INTERVAL); 743 if (err) { 744 printf("%s: open intr pipe failed: %s\n", 745 device_xname(sc->sc_dev), usbd_errstr(err)); 746 error = EIO; 747 goto done; 748 } 749 #endif 750 751 done: 752 if (--sc->sc_refcnt < 0) 753 usb_detach_wakeupold(sc->sc_dev); 754 755 return error; 756 } 757 758 Static int 759 url_newbuf(struct url_softc *sc, struct url_chain *c, struct mbuf *m) 760 { 761 struct mbuf *m_new = NULL; 762 763 DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); 764 765 if (m == NULL) { 766 MGETHDR(m_new, M_DONTWAIT, MT_DATA); 767 if (m_new == NULL) { 768 printf("%s: no memory for rx list " 769 "-- packet dropped!\n", device_xname(sc->sc_dev)); 770 return ENOBUFS; 771 } 772 MCLGET(m_new, M_DONTWAIT); 773 if (!(m_new->m_flags & M_EXT)) { 774 printf("%s: no memory for rx list " 775 "-- packet dropped!\n", device_xname(sc->sc_dev)); 776 m_freem(m_new); 777 return ENOBUFS; 778 } 779 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; 780 } else { 781 m_new = m; 782 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; 783 m_new->m_data = m_new->m_ext.ext_buf; 784 } 785 786 m_adj(m_new, ETHER_ALIGN); 787 c->url_mbuf = m_new; 788 789 return 0; 790 } 791 792 793 Static int 794 url_rx_list_init(struct url_softc *sc) 795 { 796 struct url_cdata *cd; 797 struct url_chain *c; 798 int i; 799 800 DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); 801 802 cd = &sc->sc_cdata; 803 for (i = 0; i < URL_RX_LIST_CNT; i++) { 804 c = &cd->url_rx_chain[i]; 805 c->url_sc = sc; 806 c->url_idx = i; 807 if (url_newbuf(sc, c, NULL) == ENOBUFS) 808 return ENOBUFS; 809 if (c->url_xfer == NULL) { 810 int error = usbd_create_xfer(sc->sc_pipe_rx, URL_BUFSZ, 811 0, 0, &c->url_xfer); 812 if (error) 813 return error; 814 c->url_buf = usbd_get_buffer(c->url_xfer); 815 } 816 } 817 818 return 0; 819 } 820 821 Static int 822 url_tx_list_init(struct url_softc *sc) 823 { 824 struct url_cdata *cd; 825 struct url_chain *c; 826 int i; 827 828 DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); 829 830 cd = &sc->sc_cdata; 831 for (i = 0; i < URL_TX_LIST_CNT; i++) { 832 c = &cd->url_tx_chain[i]; 833 c->url_sc = sc; 834 c->url_idx = i; 835 c->url_mbuf = NULL; 836 if (c->url_xfer == NULL) { 837 int error = usbd_create_xfer(sc->sc_pipe_tx, URL_BUFSZ, 838 USBD_FORCE_SHORT_XFER, 0, &c->url_xfer); 839 if (error) 840 return error; 841 c->url_buf = usbd_get_buffer(c->url_xfer); 842 } 843 } 844 845 return 0; 846 } 847 848 Static void 849 url_start(struct ifnet *ifp) 850 { 851 struct url_softc *sc = ifp->if_softc; 852 struct mbuf *m_head = NULL; 853 854 DPRINTF(("%s: %s: enter, link=%d\n", device_xname(sc->sc_dev), 855 __func__, sc->sc_link)); 856 857 if (sc->sc_dying) 858 return; 859 860 if (!sc->sc_link) 861 return; 862 863 if (ifp->if_flags & IFF_OACTIVE) 864 return; 865 866 IFQ_POLL(&ifp->if_snd, m_head); 867 if (m_head == NULL) 868 return; 869 870 if (url_send(sc, m_head, 0)) { 871 ifp->if_flags |= IFF_OACTIVE; 872 return; 873 } 874 875 IFQ_DEQUEUE(&ifp->if_snd, m_head); 876 877 bpf_mtap(ifp, m_head, BPF_D_OUT); 878 879 ifp->if_flags |= IFF_OACTIVE; 880 881 /* Set a timeout in case the chip goes out to lunch. */ 882 ifp->if_timer = 5; 883 } 884 885 Static int 886 url_send(struct url_softc *sc, struct mbuf *m, int idx) 887 { 888 int total_len; 889 struct url_chain *c; 890 usbd_status err; 891 892 DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev),__func__)); 893 894 c = &sc->sc_cdata.url_tx_chain[idx]; 895 896 /* Copy the mbuf data into a contiguous buffer */ 897 m_copydata(m, 0, m->m_pkthdr.len, c->url_buf); 898 c->url_mbuf = m; 899 total_len = m->m_pkthdr.len; 900 901 if (total_len < URL_MIN_FRAME_LEN) { 902 memset(c->url_buf + total_len, 0, 903 URL_MIN_FRAME_LEN - total_len); 904 total_len = URL_MIN_FRAME_LEN; 905 } 906 usbd_setup_xfer(c->url_xfer,c, c->url_buf, total_len, 907 USBD_FORCE_SHORT_XFER, URL_TX_TIMEOUT, url_txeof); 908 909 /* Transmit */ 910 sc->sc_refcnt++; 911 err = usbd_transfer(c->url_xfer); 912 if (--sc->sc_refcnt < 0) 913 usb_detach_wakeupold(sc->sc_dev); 914 if (err != USBD_IN_PROGRESS) { 915 printf("%s: url_send error=%s\n", device_xname(sc->sc_dev), 916 usbd_errstr(err)); 917 /* Stop the interface */ 918 usb_add_task(sc->sc_udev, &sc->sc_stop_task, 919 USB_TASKQ_DRIVER); 920 return EIO; 921 } 922 923 DPRINTF(("%s: %s: send %d bytes\n", device_xname(sc->sc_dev), 924 __func__, total_len)); 925 926 sc->sc_cdata.url_tx_cnt++; 927 928 return 0; 929 } 930 931 Static void 932 url_txeof(struct usbd_xfer *xfer, void *priv, 933 usbd_status status) 934 { 935 struct url_chain *c = priv; 936 struct url_softc *sc = c->url_sc; 937 struct ifnet *ifp = GET_IFP(sc); 938 int s; 939 940 if (sc->sc_dying) 941 return; 942 943 s = splnet(); 944 945 DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); 946 947 ifp->if_timer = 0; 948 ifp->if_flags &= ~IFF_OACTIVE; 949 950 if (status != USBD_NORMAL_COMPLETION) { 951 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) { 952 splx(s); 953 return; 954 } 955 ifp->if_oerrors++; 956 printf("%s: usb error on tx: %s\n", device_xname(sc->sc_dev), 957 usbd_errstr(status)); 958 if (status == USBD_STALLED) { 959 sc->sc_refcnt++; 960 usbd_clear_endpoint_stall_async(sc->sc_pipe_tx); 961 if (--sc->sc_refcnt < 0) 962 usb_detach_wakeupold(sc->sc_dev); 963 } 964 splx(s); 965 return; 966 } 967 968 ifp->if_opackets++; 969 970 m_freem(c->url_mbuf); 971 c->url_mbuf = NULL; 972 973 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) 974 url_start(ifp); 975 976 splx(s); 977 } 978 979 Static void 980 url_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status) 981 { 982 struct url_chain *c = priv; 983 struct url_softc *sc = c->url_sc; 984 struct ifnet *ifp = GET_IFP(sc); 985 struct mbuf *m; 986 uint32_t total_len; 987 url_rxhdr_t rxhdr; 988 int s; 989 990 DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev),__func__)); 991 992 if (sc->sc_dying) 993 return; 994 995 if (status != USBD_NORMAL_COMPLETION) { 996 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) 997 return; 998 sc->sc_rx_errs++; 999 if (usbd_ratecheck(&sc->sc_rx_notice)) { 1000 printf("%s: %u usb errors on rx: %s\n", 1001 device_xname(sc->sc_dev), sc->sc_rx_errs, 1002 usbd_errstr(status)); 1003 sc->sc_rx_errs = 0; 1004 } 1005 if (status == USBD_STALLED) { 1006 sc->sc_refcnt++; 1007 usbd_clear_endpoint_stall_async(sc->sc_pipe_rx); 1008 if (--sc->sc_refcnt < 0) 1009 usb_detach_wakeupold(sc->sc_dev); 1010 } 1011 goto done; 1012 } 1013 1014 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL); 1015 1016 memcpy(mtod(c->url_mbuf, char *), c->url_buf, total_len); 1017 1018 if (total_len <= ETHER_CRC_LEN) { 1019 ifp->if_ierrors++; 1020 goto done; 1021 } 1022 1023 memcpy(&rxhdr, c->url_buf + total_len - ETHER_CRC_LEN, sizeof(rxhdr)); 1024 1025 DPRINTF(("%s: RX Status: %dbytes%s%s%s%s packets\n", 1026 device_xname(sc->sc_dev), 1027 UGETW(rxhdr) & URL_RXHDR_BYTEC_MASK, 1028 UGETW(rxhdr) & URL_RXHDR_VALID_MASK ? ", Valid" : "", 1029 UGETW(rxhdr) & URL_RXHDR_RUNTPKT_MASK ? ", Runt" : "", 1030 UGETW(rxhdr) & URL_RXHDR_PHYPKT_MASK ? ", Physical match" : "", 1031 UGETW(rxhdr) & URL_RXHDR_MCASTPKT_MASK ? ", Multicast" : "")); 1032 1033 if ((UGETW(rxhdr) & URL_RXHDR_VALID_MASK) == 0) { 1034 ifp->if_ierrors++; 1035 goto done; 1036 } 1037 1038 total_len -= ETHER_CRC_LEN; 1039 1040 m = c->url_mbuf; 1041 m->m_pkthdr.len = m->m_len = total_len; 1042 m_set_rcvif(m, ifp); 1043 1044 s = splnet(); 1045 1046 if (url_newbuf(sc, c, NULL) == ENOBUFS) { 1047 ifp->if_ierrors++; 1048 goto done1; 1049 } 1050 1051 DPRINTF(("%s: %s: deliver %d\n", device_xname(sc->sc_dev), 1052 __func__, m->m_len)); 1053 if_percpuq_enqueue((ifp)->if_percpuq, (m)); 1054 1055 done1: 1056 splx(s); 1057 1058 done: 1059 /* Setup new transfer */ 1060 usbd_setup_xfer(xfer, c, c->url_buf, URL_BUFSZ, USBD_SHORT_XFER_OK, 1061 USBD_NO_TIMEOUT, url_rxeof); 1062 sc->sc_refcnt++; 1063 usbd_transfer(xfer); 1064 if (--sc->sc_refcnt < 0) 1065 usb_detach_wakeupold(sc->sc_dev); 1066 1067 DPRINTF(("%s: %s: start rx\n", device_xname(sc->sc_dev), __func__)); 1068 } 1069 1070 #if 0 1071 Static void url_intr(void) 1072 { 1073 } 1074 #endif 1075 1076 Static int 1077 url_ioctl(struct ifnet *ifp, u_long cmd, void *data) 1078 { 1079 struct url_softc *sc = ifp->if_softc; 1080 int s, error = 0; 1081 1082 DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); 1083 1084 if (sc->sc_dying) 1085 return EIO; 1086 1087 s = splnet(); 1088 1089 error = ether_ioctl(ifp, cmd, data); 1090 if (error == ENETRESET) { 1091 if (ifp->if_flags & IFF_RUNNING) 1092 url_setmulti(sc); 1093 error = 0; 1094 } 1095 1096 splx(s); 1097 1098 return error; 1099 } 1100 1101 Static void 1102 url_watchdog(struct ifnet *ifp) 1103 { 1104 struct url_softc *sc = ifp->if_softc; 1105 struct url_chain *c; 1106 usbd_status stat; 1107 int s; 1108 1109 DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); 1110 1111 ifp->if_oerrors++; 1112 printf("%s: watchdog timeout\n", device_xname(sc->sc_dev)); 1113 1114 s = splusb(); 1115 c = &sc->sc_cdata.url_tx_chain[0]; 1116 usbd_get_xfer_status(c->url_xfer, NULL, NULL, NULL, &stat); 1117 url_txeof(c->url_xfer, c, stat); 1118 1119 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) 1120 url_start(ifp); 1121 splx(s); 1122 } 1123 1124 Static void 1125 url_stop_task(struct url_softc *sc) 1126 { 1127 url_stop(GET_IFP(sc), 1); 1128 } 1129 1130 /* Stop the adapter and free any mbufs allocated to the RX and TX lists. */ 1131 Static void 1132 url_stop(struct ifnet *ifp, int disable) 1133 { 1134 struct url_softc *sc = ifp->if_softc; 1135 usbd_status err; 1136 int i; 1137 1138 DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); 1139 1140 ifp->if_timer = 0; 1141 1142 url_reset(sc); 1143 1144 callout_stop(&sc->sc_stat_ch); 1145 1146 /* Stop transfers */ 1147 /* RX endpoint */ 1148 if (sc->sc_pipe_rx != NULL) { 1149 err = usbd_abort_pipe(sc->sc_pipe_rx); 1150 if (err) 1151 printf("%s: abort rx pipe failed: %s\n", 1152 device_xname(sc->sc_dev), usbd_errstr(err)); 1153 } 1154 1155 /* TX endpoint */ 1156 if (sc->sc_pipe_tx != NULL) { 1157 err = usbd_abort_pipe(sc->sc_pipe_tx); 1158 if (err) 1159 printf("%s: abort tx pipe failed: %s\n", 1160 device_xname(sc->sc_dev), usbd_errstr(err)); 1161 } 1162 1163 #if 0 1164 /* XXX: Interrupt endpoint is not yet supported!! */ 1165 /* Interrupt endpoint */ 1166 if (sc->sc_pipe_intr != NULL) { 1167 err = usbd_abort_pipe(sc->sc_pipe_intr); 1168 if (err) 1169 printf("%s: abort intr pipe failed: %s\n", 1170 device_xname(sc->sc_dev), usbd_errstr(err)); 1171 err = usbd_close_pipe(sc->sc_pipe_intr); 1172 if (err) 1173 printf("%s: close intr pipe failed: %s\n", 1174 device_xname(sc->sc_dev), usbd_errstr(err)); 1175 sc->sc_pipe_intr = NULL; 1176 } 1177 #endif 1178 1179 /* Free RX resources. */ 1180 for (i = 0; i < URL_RX_LIST_CNT; i++) { 1181 if (sc->sc_cdata.url_rx_chain[i].url_mbuf != NULL) { 1182 m_freem(sc->sc_cdata.url_rx_chain[i].url_mbuf); 1183 sc->sc_cdata.url_rx_chain[i].url_mbuf = NULL; 1184 } 1185 if (sc->sc_cdata.url_rx_chain[i].url_xfer != NULL) { 1186 usbd_destroy_xfer(sc->sc_cdata.url_rx_chain[i].url_xfer); 1187 sc->sc_cdata.url_rx_chain[i].url_xfer = NULL; 1188 } 1189 } 1190 1191 /* Free TX resources. */ 1192 for (i = 0; i < URL_TX_LIST_CNT; i++) { 1193 if (sc->sc_cdata.url_tx_chain[i].url_mbuf != NULL) { 1194 m_freem(sc->sc_cdata.url_tx_chain[i].url_mbuf); 1195 sc->sc_cdata.url_tx_chain[i].url_mbuf = NULL; 1196 } 1197 if (sc->sc_cdata.url_tx_chain[i].url_xfer != NULL) { 1198 usbd_destroy_xfer(sc->sc_cdata.url_tx_chain[i].url_xfer); 1199 sc->sc_cdata.url_tx_chain[i].url_xfer = NULL; 1200 } 1201 } 1202 1203 /* Close pipes */ 1204 /* RX endpoint */ 1205 if (sc->sc_pipe_rx != NULL) { 1206 err = usbd_close_pipe(sc->sc_pipe_rx); 1207 if (err) 1208 printf("%s: close rx pipe failed: %s\n", 1209 device_xname(sc->sc_dev), usbd_errstr(err)); 1210 sc->sc_pipe_rx = NULL; 1211 } 1212 1213 /* TX endpoint */ 1214 if (sc->sc_pipe_tx != NULL) { 1215 err = usbd_close_pipe(sc->sc_pipe_tx); 1216 if (err) 1217 printf("%s: close tx pipe failed: %s\n", 1218 device_xname(sc->sc_dev), usbd_errstr(err)); 1219 sc->sc_pipe_tx = NULL; 1220 } 1221 1222 sc->sc_link = 0; 1223 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); 1224 } 1225 1226 /* Set media options */ 1227 Static int 1228 url_ifmedia_change(struct ifnet *ifp) 1229 { 1230 struct url_softc *sc = ifp->if_softc; 1231 struct mii_data *mii = GET_MII(sc); 1232 int rc; 1233 1234 DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); 1235 1236 if (sc->sc_dying) 1237 return 0; 1238 1239 sc->sc_link = 0; 1240 if ((rc = mii_mediachg(mii)) == ENXIO) 1241 return 0; 1242 return rc; 1243 } 1244 1245 /* Report current media status. */ 1246 Static void 1247 url_ifmedia_status(struct ifnet *ifp, struct ifmediareq *ifmr) 1248 { 1249 struct url_softc *sc = ifp->if_softc; 1250 1251 DPRINTF(("%s: %s: enter\n", device_xname(sc->sc_dev), __func__)); 1252 1253 if (sc->sc_dying) 1254 return; 1255 1256 ether_mediastatus(ifp, ifmr); 1257 } 1258 1259 Static void 1260 url_tick(void *xsc) 1261 { 1262 struct url_softc *sc = xsc; 1263 1264 if (sc == NULL) 1265 return; 1266 1267 DPRINTFN(0xff, ("%s: %s: enter\n", device_xname(sc->sc_dev), 1268 __func__)); 1269 1270 if (sc->sc_dying) 1271 return; 1272 1273 /* Perform periodic stuff in process context */ 1274 usb_add_task(sc->sc_udev, &sc->sc_tick_task, USB_TASKQ_DRIVER); 1275 } 1276 1277 Static void 1278 url_tick_task(void *xsc) 1279 { 1280 struct url_softc *sc = xsc; 1281 struct ifnet *ifp; 1282 struct mii_data *mii; 1283 int s; 1284 1285 if (sc == NULL) 1286 return; 1287 1288 DPRINTFN(0xff, ("%s: %s: enter\n", device_xname(sc->sc_dev), 1289 __func__)); 1290 1291 if (sc->sc_dying) 1292 return; 1293 1294 ifp = GET_IFP(sc); 1295 mii = GET_MII(sc); 1296 1297 if (mii == NULL) 1298 return; 1299 1300 s = splnet(); 1301 1302 mii_tick(mii); 1303 if (!sc->sc_link) { 1304 mii_pollstat(mii); 1305 if (mii->mii_media_status & IFM_ACTIVE && 1306 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) { 1307 DPRINTF(("%s: %s: got link\n", 1308 device_xname(sc->sc_dev), __func__)); 1309 sc->sc_link++; 1310 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) 1311 url_start(ifp); 1312 } 1313 } 1314 1315 callout_reset(&sc->sc_stat_ch, hz, url_tick, sc); 1316 1317 splx(s); 1318 } 1319 1320 /* Get exclusive access to the MII registers */ 1321 Static void 1322 url_lock_mii(struct url_softc *sc) 1323 { 1324 DPRINTFN(0xff, ("%s: %s: enter\n", device_xname(sc->sc_dev), 1325 __func__)); 1326 1327 sc->sc_refcnt++; 1328 rw_enter(&sc->sc_mii_rwlock, RW_WRITER); 1329 } 1330 1331 Static void 1332 url_unlock_mii(struct url_softc *sc) 1333 { 1334 DPRINTFN(0xff, ("%s: %s: enter\n", device_xname(sc->sc_dev), 1335 __func__)); 1336 1337 rw_exit(&sc->sc_mii_rwlock); 1338 if (--sc->sc_refcnt < 0) 1339 usb_detach_wakeupold(sc->sc_dev); 1340 } 1341 1342 Static int 1343 url_int_miibus_readreg(device_t dev, int phy, int reg) 1344 { 1345 struct url_softc *sc; 1346 uint16_t val; 1347 1348 if (dev == NULL) 1349 return 0; 1350 1351 sc = device_private(dev); 1352 1353 DPRINTFN(0xff, ("%s: %s: enter, phy=%d reg=0x%04x\n", 1354 device_xname(sc->sc_dev), __func__, phy, reg)); 1355 1356 if (sc->sc_dying) { 1357 #ifdef DIAGNOSTIC 1358 printf("%s: %s: dying\n", device_xname(sc->sc_dev), 1359 __func__); 1360 #endif 1361 return 0; 1362 } 1363 1364 /* XXX: one PHY only for the RTL8150 internal PHY */ 1365 if (phy != 0) { 1366 DPRINTFN(0xff, ("%s: %s: phy=%d is not supported\n", 1367 device_xname(sc->sc_dev), __func__, phy)); 1368 return 0; 1369 } 1370 1371 url_lock_mii(sc); 1372 1373 switch (reg) { 1374 case MII_BMCR: /* Control Register */ 1375 reg = URL_BMCR; 1376 break; 1377 case MII_BMSR: /* Status Register */ 1378 reg = URL_BMSR; 1379 break; 1380 case MII_PHYIDR1: 1381 case MII_PHYIDR2: 1382 val = 0; 1383 goto R_DONE; 1384 break; 1385 case MII_ANAR: /* Autonegotiation advertisement */ 1386 reg = URL_ANAR; 1387 break; 1388 case MII_ANLPAR: /* Autonegotiation link partner abilities */ 1389 reg = URL_ANLP; 1390 break; 1391 case URLPHY_MSR: /* Media Status Register */ 1392 reg = URL_MSR; 1393 break; 1394 default: 1395 printf("%s: %s: bad register %04x\n", 1396 device_xname(sc->sc_dev), __func__, reg); 1397 val = 0; 1398 goto R_DONE; 1399 break; 1400 } 1401 1402 if (reg == URL_MSR) 1403 val = url_csr_read_1(sc, reg); 1404 else 1405 val = url_csr_read_2(sc, reg); 1406 1407 R_DONE: 1408 DPRINTFN(0xff, ("%s: %s: phy=%d reg=0x%04x => 0x%04x\n", 1409 device_xname(sc->sc_dev), __func__, phy, reg, val)); 1410 1411 url_unlock_mii(sc); 1412 return val; 1413 } 1414 1415 Static void 1416 url_int_miibus_writereg(device_t dev, int phy, int reg, int data) 1417 { 1418 struct url_softc *sc; 1419 1420 if (dev == NULL) 1421 return; 1422 1423 sc = device_private(dev); 1424 1425 DPRINTFN(0xff, ("%s: %s: enter, phy=%d reg=0x%04x data=0x%04x\n", 1426 device_xname(sc->sc_dev), __func__, phy, reg, data)); 1427 1428 if (sc->sc_dying) { 1429 #ifdef DIAGNOSTIC 1430 printf("%s: %s: dying\n", device_xname(sc->sc_dev), 1431 __func__); 1432 #endif 1433 return; 1434 } 1435 1436 /* XXX: one PHY only for the RTL8150 internal PHY */ 1437 if (phy != 0) { 1438 DPRINTFN(0xff, ("%s: %s: phy=%d is not supported\n", 1439 device_xname(sc->sc_dev), __func__, phy)); 1440 return; 1441 } 1442 1443 url_lock_mii(sc); 1444 1445 switch (reg) { 1446 case MII_BMCR: /* Control Register */ 1447 reg = URL_BMCR; 1448 break; 1449 case MII_BMSR: /* Status Register */ 1450 reg = URL_BMSR; 1451 break; 1452 case MII_PHYIDR1: 1453 case MII_PHYIDR2: 1454 goto W_DONE; 1455 break; 1456 case MII_ANAR: /* Autonegotiation advertisement */ 1457 reg = URL_ANAR; 1458 break; 1459 case MII_ANLPAR: /* Autonegotiation link partner abilities */ 1460 reg = URL_ANLP; 1461 break; 1462 case URLPHY_MSR: /* Media Status Register */ 1463 reg = URL_MSR; 1464 break; 1465 default: 1466 printf("%s: %s: bad register %04x\n", 1467 device_xname(sc->sc_dev), __func__, reg); 1468 goto W_DONE; 1469 break; 1470 } 1471 1472 if (reg == URL_MSR) 1473 url_csr_write_1(sc, reg, data); 1474 else 1475 url_csr_write_2(sc, reg, data); 1476 W_DONE: 1477 1478 url_unlock_mii(sc); 1479 return; 1480 } 1481 1482 Static void 1483 url_miibus_statchg(struct ifnet *ifp) 1484 { 1485 #ifdef URL_DEBUG 1486 if (ifp == NULL) 1487 return; 1488 1489 DPRINTF(("%s: %s: enter\n", ifp->if_xname, __func__)); 1490 #endif 1491 /* Nothing to do */ 1492 } 1493 1494 #if 0 1495 /* 1496 * external PHYs support, but not test. 1497 */ 1498 Static int 1499 url_ext_miibus_redreg(device_t dev, int phy, int reg) 1500 { 1501 struct url_softc *sc = device_private(dev); 1502 uint16_t val; 1503 1504 DPRINTF(("%s: %s: enter, phy=%d reg=0x%04x\n", 1505 device_xname(sc->sc_dev), __func__, phy, reg)); 1506 1507 if (sc->sc_dying) { 1508 #ifdef DIAGNOSTIC 1509 printf("%s: %s: dying\n", device_xname(sc->sc_dev), 1510 __func__); 1511 #endif 1512 return 0; 1513 } 1514 1515 url_lock_mii(sc); 1516 1517 url_csr_write_1(sc, URL_PHYADD, phy & URL_PHYADD_MASK); 1518 /* 1519 * RTL8150L will initiate a MII management data transaction 1520 * if PHYCNT_OWN bit is set 1 by software. After transaction, 1521 * this bit is auto cleared by TRL8150L. 1522 */ 1523 url_csr_write_1(sc, URL_PHYCNT, 1524 (reg | URL_PHYCNT_PHYOWN) & ~URL_PHYCNT_RWCR); 1525 for (i = 0; i < URL_TIMEOUT; i++) { 1526 if ((url_csr_read_1(sc, URL_PHYCNT) & URL_PHYCNT_PHYOWN) == 0) 1527 break; 1528 } 1529 if (i == URL_TIMEOUT) { 1530 printf("%s: MII read timed out\n", device_xname(sc->sc_dev)); 1531 } 1532 1533 val = url_csr_read_2(sc, URL_PHYDAT); 1534 1535 DPRINTF(("%s: %s: phy=%d reg=0x%04x => 0x%04x\n", 1536 device_xname(sc->sc_dev), __func__, phy, reg, val)); 1537 1538 url_unlock_mii(sc); 1539 return val; 1540 } 1541 1542 Static void 1543 url_ext_miibus_writereg(device_t dev, int phy, int reg, int data) 1544 { 1545 struct url_softc *sc = device_private(dev); 1546 1547 DPRINTF(("%s: %s: enter, phy=%d reg=0x%04x data=0x%04x\n", 1548 device_xname(sc->sc_dev), __func__, phy, reg, data)); 1549 1550 if (sc->sc_dying) { 1551 #ifdef DIAGNOSTIC 1552 printf("%s: %s: dying\n", device_xname(sc->sc_dev), 1553 __func__); 1554 #endif 1555 return; 1556 } 1557 1558 url_lock_mii(sc); 1559 1560 url_csr_write_2(sc, URL_PHYDAT, data); 1561 url_csr_write_1(sc, URL_PHYADD, phy); 1562 url_csr_write_1(sc, URL_PHYCNT, reg | URL_PHYCNT_RWCR); /* Write */ 1563 1564 for (i=0; i < URL_TIMEOUT; i++) { 1565 if (url_csr_read_1(sc, URL_PHYCNT) & URL_PHYCNT_PHYOWN) 1566 break; 1567 } 1568 1569 if (i == URL_TIMEOUT) { 1570 printf("%s: MII write timed out\n", 1571 device_xname(sc->sc_dev)); 1572 } 1573 1574 url_unlock_mii(sc); 1575 return; 1576 } 1577 #endif 1578