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