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