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