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