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