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