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