1 /* $NetBSD: if_url.c,v 1.20 2006/09/07 02:40:33 dogcow 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.20 2006/09/07 02:40:33 dogcow 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, usbd_private_handle priv, usbd_status status) 945 { 946 struct url_chain *c = priv; 947 struct url_softc *sc = c->url_sc; 948 struct ifnet *ifp = GET_IFP(sc); 949 int s; 950 951 if (sc->sc_dying) 952 return; 953 954 s = splnet(); 955 956 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__)); 957 958 ifp->if_timer = 0; 959 ifp->if_flags &= ~IFF_OACTIVE; 960 961 if (status != USBD_NORMAL_COMPLETION) { 962 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) { 963 splx(s); 964 return; 965 } 966 ifp->if_oerrors++; 967 printf("%s: usb error on tx: %s\n", USBDEVNAME(sc->sc_dev), 968 usbd_errstr(status)); 969 if (status == USBD_STALLED) { 970 sc->sc_refcnt++; 971 usbd_clear_endpoint_stall_async(sc->sc_pipe_tx); 972 if (--sc->sc_refcnt < 0) 973 usb_detach_wakeup(USBDEV(sc->sc_dev)); 974 } 975 splx(s); 976 return; 977 } 978 979 ifp->if_opackets++; 980 981 m_freem(c->url_mbuf); 982 c->url_mbuf = NULL; 983 984 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) 985 url_start(ifp); 986 987 splx(s); 988 } 989 990 Static void 991 url_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status) 992 { 993 struct url_chain *c = priv; 994 struct url_softc *sc = c->url_sc; 995 struct ifnet *ifp = GET_IFP(sc); 996 struct mbuf *m; 997 u_int32_t total_len; 998 url_rxhdr_t rxhdr; 999 int s; 1000 1001 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev),__func__)); 1002 1003 if (sc->sc_dying) 1004 return; 1005 1006 if (status != USBD_NORMAL_COMPLETION) { 1007 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) 1008 return; 1009 sc->sc_rx_errs++; 1010 if (usbd_ratecheck(&sc->sc_rx_notice)) { 1011 printf("%s: %u usb errors on rx: %s\n", 1012 USBDEVNAME(sc->sc_dev), sc->sc_rx_errs, 1013 usbd_errstr(status)); 1014 sc->sc_rx_errs = 0; 1015 } 1016 if (status == USBD_STALLED) { 1017 sc->sc_refcnt++; 1018 usbd_clear_endpoint_stall_async(sc->sc_pipe_rx); 1019 if (--sc->sc_refcnt < 0) 1020 usb_detach_wakeup(USBDEV(sc->sc_dev)); 1021 } 1022 goto done; 1023 } 1024 1025 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL); 1026 1027 memcpy(mtod(c->url_mbuf, char *), c->url_buf, total_len); 1028 1029 if (total_len <= ETHER_CRC_LEN) { 1030 ifp->if_ierrors++; 1031 goto done; 1032 } 1033 1034 memcpy(&rxhdr, c->url_buf + total_len - ETHER_CRC_LEN, sizeof(rxhdr)); 1035 1036 DPRINTF(("%s: RX Status: %dbytes%s%s%s%s packets\n", 1037 USBDEVNAME(sc->sc_dev), 1038 UGETW(rxhdr) & URL_RXHDR_BYTEC_MASK, 1039 UGETW(rxhdr) & URL_RXHDR_VALID_MASK ? ", Valid" : "", 1040 UGETW(rxhdr) & URL_RXHDR_RUNTPKT_MASK ? ", Runt" : "", 1041 UGETW(rxhdr) & URL_RXHDR_PHYPKT_MASK ? ", Physical match" : "", 1042 UGETW(rxhdr) & URL_RXHDR_MCASTPKT_MASK ? ", Multicast" : "")); 1043 1044 if ((UGETW(rxhdr) & URL_RXHDR_VALID_MASK) == 0) { 1045 ifp->if_ierrors++; 1046 goto done; 1047 } 1048 1049 ifp->if_ipackets++; 1050 total_len -= ETHER_CRC_LEN; 1051 1052 m = c->url_mbuf; 1053 m->m_pkthdr.len = m->m_len = total_len; 1054 m->m_pkthdr.rcvif = ifp; 1055 1056 s = splnet(); 1057 1058 if (url_newbuf(sc, c, NULL) == ENOBUFS) { 1059 ifp->if_ierrors++; 1060 goto done1; 1061 } 1062 1063 #if NBPFILTER > 0 1064 if (ifp->if_bpf) 1065 BPF_MTAP(ifp, m); 1066 #endif 1067 1068 DPRINTF(("%s: %s: deliver %d\n", USBDEVNAME(sc->sc_dev), 1069 __func__, m->m_len)); 1070 IF_INPUT(ifp, m); 1071 1072 done1: 1073 splx(s); 1074 1075 done: 1076 /* Setup new transfer */ 1077 usbd_setup_xfer(xfer, sc->sc_pipe_rx, c, c->url_buf, URL_BUFSZ, 1078 USBD_SHORT_XFER_OK | USBD_NO_COPY, 1079 USBD_NO_TIMEOUT, url_rxeof); 1080 sc->sc_refcnt++; 1081 usbd_transfer(xfer); 1082 if (--sc->sc_refcnt < 0) 1083 usb_detach_wakeup(USBDEV(sc->sc_dev)); 1084 1085 DPRINTF(("%s: %s: start rx\n", USBDEVNAME(sc->sc_dev), __func__)); 1086 } 1087 1088 #if 0 1089 Static void url_intr() 1090 { 1091 } 1092 #endif 1093 1094 Static int 1095 url_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 1096 { 1097 struct url_softc *sc = ifp->if_softc; 1098 struct ifreq *ifr = (struct ifreq *)data; 1099 struct mii_data *mii; 1100 int s, error = 0; 1101 1102 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__)); 1103 1104 if (sc->sc_dying) 1105 return (EIO); 1106 1107 s = splnet(); 1108 1109 switch (cmd) { 1110 case SIOCGIFMEDIA: 1111 case SIOCSIFMEDIA: 1112 mii = GET_MII(sc); 1113 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, cmd); 1114 break; 1115 1116 default: 1117 error = ether_ioctl(ifp, cmd, data); 1118 if (error == ENETRESET) { 1119 if (ifp->if_flags & IFF_RUNNING) 1120 url_setmulti(sc); 1121 error = 0; 1122 } 1123 break; 1124 } 1125 1126 splx(s); 1127 1128 return (error); 1129 } 1130 1131 Static void 1132 url_watchdog(struct ifnet *ifp) 1133 { 1134 struct url_softc *sc = ifp->if_softc; 1135 struct url_chain *c; 1136 usbd_status stat; 1137 int s; 1138 1139 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__)); 1140 1141 ifp->if_oerrors++; 1142 printf("%s: watchdog timeout\n", USBDEVNAME(sc->sc_dev)); 1143 1144 s = splusb(); 1145 c = &sc->sc_cdata.url_tx_chain[0]; 1146 usbd_get_xfer_status(c->url_xfer, NULL, NULL, NULL, &stat); 1147 url_txeof(c->url_xfer, c, stat); 1148 1149 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) 1150 url_start(ifp); 1151 splx(s); 1152 } 1153 1154 Static void 1155 url_stop_task(struct url_softc *sc) 1156 { 1157 url_stop(GET_IFP(sc), 1); 1158 } 1159 1160 /* Stop the adapter and free any mbufs allocated to the RX and TX lists. */ 1161 Static void 1162 url_stop(struct ifnet *ifp, int disable) 1163 { 1164 struct url_softc *sc = ifp->if_softc; 1165 usbd_status err; 1166 int i; 1167 1168 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__)); 1169 1170 ifp->if_timer = 0; 1171 1172 url_reset(sc); 1173 1174 usb_uncallout(sc->sc_stat_ch, url_tick, sc); 1175 1176 /* Stop transfers */ 1177 /* RX endpoint */ 1178 if (sc->sc_pipe_rx != NULL) { 1179 err = usbd_abort_pipe(sc->sc_pipe_rx); 1180 if (err) 1181 printf("%s: abort rx pipe failed: %s\n", 1182 USBDEVNAME(sc->sc_dev), usbd_errstr(err)); 1183 err = usbd_close_pipe(sc->sc_pipe_rx); 1184 if (err) 1185 printf("%s: close rx pipe failed: %s\n", 1186 USBDEVNAME(sc->sc_dev), usbd_errstr(err)); 1187 sc->sc_pipe_rx = NULL; 1188 } 1189 1190 /* TX endpoint */ 1191 if (sc->sc_pipe_tx != NULL) { 1192 err = usbd_abort_pipe(sc->sc_pipe_tx); 1193 if (err) 1194 printf("%s: abort tx pipe failed: %s\n", 1195 USBDEVNAME(sc->sc_dev), usbd_errstr(err)); 1196 err = usbd_close_pipe(sc->sc_pipe_tx); 1197 if (err) 1198 printf("%s: close tx pipe failed: %s\n", 1199 USBDEVNAME(sc->sc_dev), usbd_errstr(err)); 1200 sc->sc_pipe_tx = NULL; 1201 } 1202 1203 #if 0 1204 /* XXX: Interrupt endpoint is not yet supported!! */ 1205 /* Interrupt endpoint */ 1206 if (sc->sc_pipe_intr != NULL) { 1207 err = usbd_abort_pipe(sc->sc_pipe_intr); 1208 if (err) 1209 printf("%s: abort intr pipe failed: %s\n", 1210 USBDEVNAME(sc->sc_dev), usbd_errstr(err)); 1211 err = usbd_close_pipe(sc->sc_pipe_intr); 1212 if (err) 1213 printf("%s: close intr pipe failed: %s\n", 1214 USBDEVNAME(sc->sc_dev), usbd_errstr(err)); 1215 sc->sc_pipe_intr = NULL; 1216 } 1217 #endif 1218 1219 /* Free RX resources. */ 1220 for (i = 0; i < URL_RX_LIST_CNT; i++) { 1221 if (sc->sc_cdata.url_rx_chain[i].url_mbuf != NULL) { 1222 m_freem(sc->sc_cdata.url_rx_chain[i].url_mbuf); 1223 sc->sc_cdata.url_rx_chain[i].url_mbuf = NULL; 1224 } 1225 if (sc->sc_cdata.url_rx_chain[i].url_xfer != NULL) { 1226 usbd_free_xfer(sc->sc_cdata.url_rx_chain[i].url_xfer); 1227 sc->sc_cdata.url_rx_chain[i].url_xfer = NULL; 1228 } 1229 } 1230 1231 /* Free TX resources. */ 1232 for (i = 0; i < URL_TX_LIST_CNT; i++) { 1233 if (sc->sc_cdata.url_tx_chain[i].url_mbuf != NULL) { 1234 m_freem(sc->sc_cdata.url_tx_chain[i].url_mbuf); 1235 sc->sc_cdata.url_tx_chain[i].url_mbuf = NULL; 1236 } 1237 if (sc->sc_cdata.url_tx_chain[i].url_xfer != NULL) { 1238 usbd_free_xfer(sc->sc_cdata.url_tx_chain[i].url_xfer); 1239 sc->sc_cdata.url_tx_chain[i].url_xfer = NULL; 1240 } 1241 } 1242 1243 sc->sc_link = 0; 1244 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); 1245 } 1246 1247 /* Set media options */ 1248 Static int 1249 url_ifmedia_change(struct ifnet *ifp) 1250 { 1251 struct url_softc *sc = ifp->if_softc; 1252 struct mii_data *mii = GET_MII(sc); 1253 1254 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__)); 1255 1256 if (sc->sc_dying) 1257 return (0); 1258 1259 sc->sc_link = 0; 1260 if (mii->mii_instance) { 1261 struct mii_softc *miisc; 1262 for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL; 1263 miisc = LIST_NEXT(miisc, mii_list)) 1264 mii_phy_reset(miisc); 1265 } 1266 1267 return (mii_mediachg(mii)); 1268 } 1269 1270 /* Report current media status. */ 1271 Static void 1272 url_ifmedia_status(struct ifnet *ifp, struct ifmediareq *ifmr) 1273 { 1274 struct url_softc *sc = ifp->if_softc; 1275 struct mii_data *mii = GET_MII(sc); 1276 1277 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__)); 1278 1279 if (sc->sc_dying) 1280 return; 1281 1282 if ((ifp->if_flags & IFF_RUNNING) == 0) { 1283 ifmr->ifm_active = IFM_ETHER | IFM_NONE; 1284 ifmr->ifm_status = 0; 1285 return; 1286 } 1287 1288 mii_pollstat(mii); 1289 ifmr->ifm_active = mii->mii_media_active; 1290 ifmr->ifm_status = mii->mii_media_status; 1291 } 1292 1293 Static void 1294 url_tick(void *xsc) 1295 { 1296 struct url_softc *sc = xsc; 1297 1298 if (sc == NULL) 1299 return; 1300 1301 DPRINTFN(0xff, ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), 1302 __func__)); 1303 1304 if (sc->sc_dying) 1305 return; 1306 1307 /* Perform periodic stuff in process context */ 1308 usb_add_task(sc->sc_udev, &sc->sc_tick_task); 1309 } 1310 1311 Static void 1312 url_tick_task(void *xsc) 1313 { 1314 struct url_softc *sc = xsc; 1315 struct ifnet *ifp; 1316 struct mii_data *mii; 1317 int s; 1318 1319 if (sc == NULL) 1320 return; 1321 1322 DPRINTFN(0xff, ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), 1323 __func__)); 1324 1325 if (sc->sc_dying) 1326 return; 1327 1328 ifp = GET_IFP(sc); 1329 mii = GET_MII(sc); 1330 1331 if (mii == NULL) 1332 return; 1333 1334 s = splnet(); 1335 1336 mii_tick(mii); 1337 if (!sc->sc_link) { 1338 mii_pollstat(mii); 1339 if (mii->mii_media_status & IFM_ACTIVE && 1340 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) { 1341 DPRINTF(("%s: %s: got link\n", 1342 USBDEVNAME(sc->sc_dev), __func__)); 1343 sc->sc_link++; 1344 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) 1345 url_start(ifp); 1346 } 1347 } 1348 1349 usb_callout(sc->sc_stat_ch, hz, url_tick, sc); 1350 1351 splx(s); 1352 } 1353 1354 /* Get exclusive access to the MII registers */ 1355 Static void 1356 url_lock_mii(struct url_softc *sc) 1357 { 1358 DPRINTFN(0xff, ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), 1359 __func__)); 1360 1361 sc->sc_refcnt++; 1362 lockmgr(&sc->sc_mii_lock, LK_EXCLUSIVE, NULL); 1363 } 1364 1365 Static void 1366 url_unlock_mii(struct url_softc *sc) 1367 { 1368 DPRINTFN(0xff, ("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), 1369 __func__)); 1370 1371 lockmgr(&sc->sc_mii_lock, LK_RELEASE, NULL); 1372 if (--sc->sc_refcnt < 0) 1373 usb_detach_wakeup(USBDEV(sc->sc_dev)); 1374 } 1375 1376 Static int 1377 url_int_miibus_readreg(device_ptr_t dev, int phy, int reg) 1378 { 1379 struct url_softc *sc; 1380 u_int16_t val; 1381 1382 if (dev == NULL) 1383 return (0); 1384 1385 sc = USBGETSOFTC(dev); 1386 1387 DPRINTFN(0xff, ("%s: %s: enter, phy=%d reg=0x%04x\n", 1388 USBDEVNAME(sc->sc_dev), __func__, phy, reg)); 1389 1390 if (sc->sc_dying) { 1391 #ifdef DIAGNOSTIC 1392 printf("%s: %s: dying\n", USBDEVNAME(sc->sc_dev), 1393 __func__); 1394 #endif 1395 return (0); 1396 } 1397 1398 /* XXX: one PHY only for the RTL8150 internal PHY */ 1399 if (phy != 0) { 1400 DPRINTFN(0xff, ("%s: %s: phy=%d is not supported\n", 1401 USBDEVNAME(sc->sc_dev), __func__, phy)); 1402 return (0); 1403 } 1404 1405 url_lock_mii(sc); 1406 1407 switch (reg) { 1408 case MII_BMCR: /* Control Register */ 1409 reg = URL_BMCR; 1410 break; 1411 case MII_BMSR: /* Status Register */ 1412 reg = URL_BMSR; 1413 break; 1414 case MII_PHYIDR1: 1415 case MII_PHYIDR2: 1416 val = 0; 1417 goto R_DONE; 1418 break; 1419 case MII_ANAR: /* Autonegotiation advertisement */ 1420 reg = URL_ANAR; 1421 break; 1422 case MII_ANLPAR: /* Autonegotiation link partner abilities */ 1423 reg = URL_ANLP; 1424 break; 1425 case URLPHY_MSR: /* Media Status Register */ 1426 reg = URL_MSR; 1427 break; 1428 default: 1429 printf("%s: %s: bad register %04x\n", 1430 USBDEVNAME(sc->sc_dev), __func__, reg); 1431 val = 0; 1432 goto R_DONE; 1433 break; 1434 } 1435 1436 if (reg == URL_MSR) 1437 val = url_csr_read_1(sc, reg); 1438 else 1439 val = url_csr_read_2(sc, reg); 1440 1441 R_DONE: 1442 DPRINTFN(0xff, ("%s: %s: phy=%d reg=0x%04x => 0x%04x\n", 1443 USBDEVNAME(sc->sc_dev), __func__, phy, reg, val)); 1444 1445 url_unlock_mii(sc); 1446 return (val); 1447 } 1448 1449 Static void 1450 url_int_miibus_writereg(device_ptr_t dev, int phy, int reg, int data) 1451 { 1452 struct url_softc *sc; 1453 1454 if (dev == NULL) 1455 return; 1456 1457 sc = USBGETSOFTC(dev); 1458 1459 DPRINTFN(0xff, ("%s: %s: enter, phy=%d reg=0x%04x data=0x%04x\n", 1460 USBDEVNAME(sc->sc_dev), __func__, phy, reg, data)); 1461 1462 if (sc->sc_dying) { 1463 #ifdef DIAGNOSTIC 1464 printf("%s: %s: dying\n", USBDEVNAME(sc->sc_dev), 1465 __func__); 1466 #endif 1467 return; 1468 } 1469 1470 /* XXX: one PHY only for the RTL8150 internal PHY */ 1471 if (phy != 0) { 1472 DPRINTFN(0xff, ("%s: %s: phy=%d is not supported\n", 1473 USBDEVNAME(sc->sc_dev), __func__, phy)); 1474 return; 1475 } 1476 1477 url_lock_mii(sc); 1478 1479 switch (reg) { 1480 case MII_BMCR: /* Control Register */ 1481 reg = URL_BMCR; 1482 break; 1483 case MII_BMSR: /* Status Register */ 1484 reg = URL_BMSR; 1485 break; 1486 case MII_PHYIDR1: 1487 case MII_PHYIDR2: 1488 goto W_DONE; 1489 break; 1490 case MII_ANAR: /* Autonegotiation advertisement */ 1491 reg = URL_ANAR; 1492 break; 1493 case MII_ANLPAR: /* Autonegotiation link partner abilities */ 1494 reg = URL_ANLP; 1495 break; 1496 case URLPHY_MSR: /* Media Status Register */ 1497 reg = URL_MSR; 1498 break; 1499 default: 1500 printf("%s: %s: bad register %04x\n", 1501 USBDEVNAME(sc->sc_dev), __func__, reg); 1502 goto W_DONE; 1503 break; 1504 } 1505 1506 if (reg == URL_MSR) 1507 url_csr_write_1(sc, reg, data); 1508 else 1509 url_csr_write_2(sc, reg, data); 1510 W_DONE: 1511 1512 url_unlock_mii(sc); 1513 return; 1514 } 1515 1516 Static void 1517 url_miibus_statchg(device_ptr_t dev) 1518 { 1519 #ifdef URL_DEBUG 1520 struct url_softc *sc; 1521 1522 if (dev == NULL) 1523 return; 1524 1525 sc = USBGETSOFTC(dev); 1526 DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->sc_dev), __func__)); 1527 #endif 1528 /* Nothing to do */ 1529 } 1530 1531 #if 0 1532 /* 1533 * external PHYs support, but not test. 1534 */ 1535 Static int 1536 url_ext_miibus_redreg(device_ptr_t dev, int phy, int reg) 1537 { 1538 struct url_softc *sc = USBGETSOFTC(dev); 1539 u_int16_t val; 1540 1541 DPRINTF(("%s: %s: enter, phy=%d reg=0x%04x\n", 1542 USBDEVNAME(sc->sc_dev), __func__, phy, reg)); 1543 1544 if (sc->sc_dying) { 1545 #ifdef DIAGNOSTIC 1546 printf("%s: %s: dying\n", USBDEVNAME(sc->sc_dev), 1547 __func__); 1548 #endif 1549 return (0); 1550 } 1551 1552 url_lock_mii(sc); 1553 1554 url_csr_write_1(sc, URL_PHYADD, phy & URL_PHYADD_MASK); 1555 /* 1556 * RTL8150L will initiate a MII management data transaction 1557 * if PHYCNT_OWN bit is set 1 by software. After transaction, 1558 * this bit is auto cleared by TRL8150L. 1559 */ 1560 url_csr_write_1(sc, URL_PHYCNT, 1561 (reg | URL_PHYCNT_PHYOWN) & ~URL_PHYCNT_RWCR); 1562 for (i = 0; i < URL_TIMEOUT; i++) { 1563 if ((url_csr_read_1(sc, URL_PHYCNT) & URL_PHYCNT_PHYOWN) == 0) 1564 break; 1565 } 1566 if (i == URL_TIMEOUT) { 1567 printf("%s: MII read timed out\n", USBDEVNAME(sc->sc_dev)); 1568 } 1569 1570 val = url_csr_read_2(sc, URL_PHYDAT); 1571 1572 DPRINTF(("%s: %s: phy=%d reg=0x%04x => 0x%04x\n", 1573 USBDEVNAME(sc->sc_dev), __func__, phy, reg, val)); 1574 1575 url_unlock_mii(sc); 1576 return (val); 1577 } 1578 1579 Static void 1580 url_ext_miibus_writereg(device_ptr_t dev, int phy, int reg, int data) 1581 { 1582 struct url_softc *sc = USBGETSOFTC(dev); 1583 1584 DPRINTF(("%s: %s: enter, phy=%d reg=0x%04x data=0x%04x\n", 1585 USBDEVNAME(sc->sc_dev), __func__, phy, reg, data)); 1586 1587 if (sc->sc_dying) { 1588 #ifdef DIAGNOSTIC 1589 printf("%s: %s: dying\n", USBDEVNAME(sc->sc_dev), 1590 __func__); 1591 #endif 1592 return; 1593 } 1594 1595 url_lock_mii(sc); 1596 1597 url_csr_write_2(sc, URL_PHYDAT, data); 1598 url_csr_write_1(sc, URL_PHYADD, phy); 1599 url_csr_write_1(sc, URL_PHYCNT, reg | URL_PHYCNT_RWCR); /* Write */ 1600 1601 for (i=0; i < URL_TIMEOUT; i++) { 1602 if (url_csr_read_1(sc, URL_PHYCNT) & URL_PHYCNT_PHYOWN) 1603 break; 1604 } 1605 1606 if (i == URL_TIMEOUT) { 1607 printf("%s: MII write timed out\n", 1608 USBDEVNAME(sc->sc_dev)); 1609 } 1610 1611 url_unlock_mii(sc); 1612 return; 1613 } 1614 #endif 1615 1616