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