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