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