1 /* $OpenBSD: if_aue.c,v 1.16 2001/07/15 03:03:35 mickey Exp $ */ 2 /* $NetBSD: if_aue.c,v 1.55 2001/03/25 22:59:43 augustss Exp $ */ 3 /* 4 * Copyright (c) 1997, 1998, 1999, 2000 5 * Bill Paul <wpaul@ee.columbia.edu>. 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. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by Bill Paul. 18 * 4. Neither the name of the author nor the names of any co-contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD 26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 32 * THE POSSIBILITY OF SUCH DAMAGE. 33 * 34 * $FreeBSD: src/sys/dev/usb/if_aue.c,v 1.11 2000/01/14 01:36:14 wpaul Exp $ 35 */ 36 37 /* 38 * ADMtek AN986 Pegasus USB to ethernet driver. Datasheet is available 39 * from http://www.admtek.com.tw. 40 * 41 * Written by Bill Paul <wpaul@ee.columbia.edu> 42 * Electrical Engineering Department 43 * Columbia University, New York City 44 */ 45 46 /* 47 * The Pegasus chip uses four USB "endpoints" to provide 10/100 ethernet 48 * support: the control endpoint for reading/writing registers, burst 49 * read endpoint for packet reception, burst write for packet transmission 50 * and one for "interrupts." The chip uses the same RX filter scheme 51 * as the other ADMtek ethernet parts: one perfect filter entry for the 52 * the station address and a 64-bit multicast hash table. The chip supports 53 * both MII and HomePNA attachments. 54 * 55 * Since the maximum data transfer speed of USB is supposed to be 12Mbps, 56 * you're never really going to get 100Mbps speeds from this device. I 57 * think the idea is to allow the device to connect to 10 or 100Mbps 58 * networks, not necessarily to provide 100Mbps performance. Also, since 59 * the controller uses an external PHY chip, it's possible that board 60 * designers might simply choose a 10Mbps PHY. 61 * 62 * Registers are accessed using usbd_do_request(). Packet transfers are 63 * done using usbd_transfer() and friends. 64 */ 65 66 /* 67 * Ported to NetBSD and somewhat rewritten by Lennart Augustsson. 68 */ 69 70 /* 71 * TODO: 72 * better error messages from rxstat 73 * split out if_auevar.h 74 * add thread to avoid register reads from interrupt context 75 * more error checks 76 * investigate short rx problem 77 * proper cleanup on errors 78 */ 79 80 #if defined(__NetBSD__) 81 #include "opt_inet.h" 82 #include "opt_ns.h" 83 #include "bpfilter.h" 84 #include "rnd.h" 85 #elif defined(__OpenBSD__) 86 #include "bpfilter.h" 87 #endif /* defined(__OpenBSD__) */ 88 89 #include <sys/param.h> 90 #include <sys/systm.h> 91 #include <sys/sockio.h> 92 #include <sys/lock.h> 93 #include <sys/mbuf.h> 94 #include <sys/malloc.h> 95 #include <sys/kernel.h> 96 #include <sys/proc.h> 97 #include <sys/socket.h> 98 99 #include <sys/device.h> 100 #if NRND > 0 101 #include <sys/rnd.h> 102 #endif 103 104 #include <net/if.h> 105 #if defined(__NetBSD__) 106 #include <net/if_arp.h> 107 #endif 108 #include <net/if_dl.h> 109 #include <net/if_media.h> 110 111 #define BPF_MTAP(ifp, m) bpf_mtap((ifp)->if_bpf, (m)) 112 113 #if NBPFILTER > 0 114 #include <net/bpf.h> 115 #endif 116 117 #if defined(__NetBSD__) 118 #include <net/if_ether.h> 119 #ifdef INET 120 #include <netinet/in.h> 121 #include <netinet/if_inarp.h> 122 #endif 123 #endif /* defined(__NetBSD__) */ 124 125 #if defined(__OpenBSD__) 126 #ifdef INET 127 #include <netinet/in.h> 128 #include <netinet/in_systm.h> 129 #include <netinet/in_var.h> 130 #include <netinet/ip.h> 131 #include <netinet/if_ether.h> 132 #endif 133 #endif /* defined(__OpenBSD__) */ 134 135 #ifdef NS 136 #include <netns/ns.h> 137 #include <netns/ns_if.h> 138 #endif 139 140 #include <dev/mii/mii.h> 141 #include <dev/mii/miivar.h> 142 143 #include <dev/usb/usb.h> 144 #include <dev/usb/usbdi.h> 145 #include <dev/usb/usbdi_util.h> 146 #include <dev/usb/usbdevs.h> 147 148 #include <dev/usb/if_auereg.h> 149 150 #ifdef AUE_DEBUG 151 #define DPRINTF(x) if (auedebug) logprintf x 152 #define DPRINTFN(n,x) if (auedebug >= (n)) logprintf x 153 int auedebug = 0; 154 #else 155 #define DPRINTF(x) 156 #define DPRINTFN(n,x) 157 #endif 158 159 /* 160 * Various supported device vendors/products. 161 */ 162 struct aue_type { 163 u_int16_t aue_vid; 164 u_int16_t aue_did; 165 char aue_linksys; 166 }; 167 168 Static const struct aue_type aue_devs[] = { 169 { USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USB100, 0 }, 170 { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUATX1, 0 }, 171 { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUATX5, 0 }, 172 { USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB100TX, 1 }, 173 { USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB100H1, 1 }, 174 { USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10TA, 1 }, 175 { USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUS, 0 }, 176 { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650, 1 }, 177 { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX, 1 }, 178 { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX_PNA, 0 }, 179 { USB_VENDOR_SOHOWARE, USB_PRODUCT_SOHOWARE_NUB100, 0 }, 180 { USB_VENDOR_SMC, USB_PRODUCT_SMC_2202USB, 0 }, 181 { USB_VENDOR_COREGA, USB_PRODUCT_COREGA_FETHER_USB_TX, 0 }, 182 { USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBETTX, 0 }, 183 { USB_VENDOR_KINGSTON, USB_PRODUCT_KINGSTON_KNU101TX, 0 }, 184 { 0, 0, 0 } 185 }; 186 187 USB_DECLARE_DRIVER(aue); 188 189 Static const struct aue_type *aue_lookup(u_int16_t vendor, u_int16_t product); 190 Static int aue_tx_list_init(struct aue_softc *); 191 Static int aue_rx_list_init(struct aue_softc *); 192 Static int aue_newbuf(struct aue_softc *, struct aue_chain *, struct mbuf *); 193 Static int aue_send(struct aue_softc *, struct mbuf *, int); 194 Static void aue_intr(usbd_xfer_handle, usbd_private_handle, usbd_status); 195 Static void aue_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status); 196 Static void aue_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status); 197 Static void aue_tick(void *); 198 Static void aue_tick_task(void *); 199 Static void aue_start(struct ifnet *); 200 Static int aue_ioctl(struct ifnet *, u_long, caddr_t); 201 Static void aue_init(void *); 202 Static void aue_stop(struct aue_softc *); 203 Static void aue_watchdog(struct ifnet *); 204 Static int aue_openpipes(struct aue_softc *); 205 Static int aue_ifmedia_upd(struct ifnet *); 206 Static void aue_ifmedia_sts(struct ifnet *, struct ifmediareq *); 207 208 Static int aue_eeprom_getword(struct aue_softc *, int); 209 Static void aue_read_mac(struct aue_softc *, u_char *); 210 Static int aue_miibus_readreg(device_ptr_t, int, int); 211 Static void aue_miibus_writereg(device_ptr_t, int, int, int); 212 Static void aue_miibus_statchg(device_ptr_t); 213 214 Static void aue_lock_mii(struct aue_softc *); 215 Static void aue_unlock_mii(struct aue_softc *); 216 217 Static void aue_setmulti(struct aue_softc *); 218 Static u_int32_t aue_crc(caddr_t); 219 Static void aue_reset(struct aue_softc *); 220 221 Static int aue_csr_read_1(struct aue_softc *, int); 222 Static int aue_csr_write_1(struct aue_softc *, int, int); 223 Static int aue_csr_read_2(struct aue_softc *, int); 224 Static int aue_csr_write_2(struct aue_softc *, int, int); 225 226 #define AUE_SETBIT(sc, reg, x) \ 227 aue_csr_write_1(sc, reg, aue_csr_read_1(sc, reg) | (x)) 228 229 #define AUE_CLRBIT(sc, reg, x) \ 230 aue_csr_write_1(sc, reg, aue_csr_read_1(sc, reg) & ~(x)) 231 232 Static int 233 aue_csr_read_1(struct aue_softc *sc, int reg) 234 { 235 usb_device_request_t req; 236 usbd_status err; 237 uByte val = 0; 238 239 if (sc->aue_dying) 240 return (0); 241 242 req.bmRequestType = UT_READ_VENDOR_DEVICE; 243 req.bRequest = AUE_UR_READREG; 244 USETW(req.wValue, 0); 245 USETW(req.wIndex, reg); 246 USETW(req.wLength, 1); 247 248 err = usbd_do_request(sc->aue_udev, &req, &val); 249 250 if (err) { 251 DPRINTF(("%s: aue_csr_read_1: reg=0x%x err=%s\n", 252 USBDEVNAME(sc->aue_dev), reg, usbd_errstr(err))); 253 return (0); 254 } 255 256 return (val); 257 } 258 259 Static int 260 aue_csr_read_2(struct aue_softc *sc, int reg) 261 { 262 usb_device_request_t req; 263 usbd_status err; 264 uWord val; 265 266 if (sc->aue_dying) 267 return (0); 268 269 req.bmRequestType = UT_READ_VENDOR_DEVICE; 270 req.bRequest = AUE_UR_READREG; 271 USETW(req.wValue, 0); 272 USETW(req.wIndex, reg); 273 USETW(req.wLength, 2); 274 275 err = usbd_do_request(sc->aue_udev, &req, &val); 276 277 if (err) { 278 DPRINTF(("%s: aue_csr_read_2: reg=0x%x err=%s\n", 279 USBDEVNAME(sc->aue_dev), reg, usbd_errstr(err))); 280 return (0); 281 } 282 283 return (UGETW(val)); 284 } 285 286 Static int 287 aue_csr_write_1(struct aue_softc *sc, int reg, int aval) 288 { 289 usb_device_request_t req; 290 usbd_status err; 291 uByte val; 292 293 if (sc->aue_dying) 294 return (0); 295 296 val = aval; 297 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 298 req.bRequest = AUE_UR_WRITEREG; 299 USETW(req.wValue, val); 300 USETW(req.wIndex, reg); 301 USETW(req.wLength, 1); 302 303 err = usbd_do_request(sc->aue_udev, &req, &val); 304 305 if (err) { 306 DPRINTF(("%s: aue_csr_write_1: reg=0x%x err=%s\n", 307 USBDEVNAME(sc->aue_dev), reg, usbd_errstr(err))); 308 return (-1); 309 } 310 311 return (0); 312 } 313 314 Static int 315 aue_csr_write_2(struct aue_softc *sc, int reg, int aval) 316 { 317 usb_device_request_t req; 318 usbd_status err; 319 uWord val; 320 321 if (sc->aue_dying) 322 return (0); 323 324 USETW(val, aval); 325 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 326 req.bRequest = AUE_UR_WRITEREG; 327 USETW(req.wValue, aval); 328 USETW(req.wIndex, reg); 329 USETW(req.wLength, 2); 330 331 err = usbd_do_request(sc->aue_udev, &req, &val); 332 333 if (err) { 334 DPRINTF(("%s: aue_csr_write_2: reg=0x%x err=%s\n", 335 USBDEVNAME(sc->aue_dev), reg, usbd_errstr(err))); 336 return (-1); 337 } 338 339 return (0); 340 } 341 342 /* 343 * Read a word of data stored in the EEPROM at address 'addr.' 344 */ 345 Static int 346 aue_eeprom_getword(struct aue_softc *sc, int addr) 347 { 348 int i; 349 350 aue_csr_write_1(sc, AUE_EE_REG, addr); 351 aue_csr_write_1(sc, AUE_EE_CTL, AUE_EECTL_READ); 352 353 for (i = 0; i < AUE_TIMEOUT; i++) { 354 if (aue_csr_read_1(sc, AUE_EE_CTL) & AUE_EECTL_DONE) 355 break; 356 } 357 358 if (i == AUE_TIMEOUT) { 359 printf("%s: EEPROM read timed out\n", 360 USBDEVNAME(sc->aue_dev)); 361 } 362 363 return (aue_csr_read_2(sc, AUE_EE_DATA)); 364 } 365 366 /* 367 * Read the MAC from the EEPROM. It's at offset 0. 368 */ 369 Static void 370 aue_read_mac(struct aue_softc *sc, u_char *dest) 371 { 372 int i; 373 int off = 0; 374 int word; 375 376 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__)); 377 378 for (i = 0; i < 3; i++) { 379 word = aue_eeprom_getword(sc, off + i); 380 dest[2 * i] = (u_char)word; 381 dest[2 * i + 1] = (u_char)(word >> 8); 382 } 383 } 384 385 /* Get exclusive access to the MII registers */ 386 Static void 387 aue_lock_mii(struct aue_softc *sc) 388 { 389 lockmgr(&sc->aue_mii_lock, LK_EXCLUSIVE, NULL, curproc); 390 } 391 392 Static void 393 aue_unlock_mii(struct aue_softc *sc) 394 { 395 lockmgr(&sc->aue_mii_lock, LK_RELEASE, NULL, curproc); 396 } 397 398 Static int 399 aue_miibus_readreg(device_ptr_t dev, int phy, int reg) 400 { 401 struct aue_softc *sc = USBGETSOFTC(dev); 402 int i; 403 u_int16_t val; 404 405 #if 0 406 /* 407 * The Am79C901 HomePNA PHY actually contains 408 * two transceivers: a 1Mbps HomePNA PHY and a 409 * 10Mbps full/half duplex ethernet PHY with 410 * NWAY autoneg. However in the ADMtek adapter, 411 * only the 1Mbps PHY is actually connected to 412 * anything, so we ignore the 10Mbps one. It 413 * happens to be configured for MII address 3, 414 * so we filter that out. 415 */ 416 if (sc->aue_vendor == USB_VENDOR_ADMTEK && 417 sc->aue_product == USB_PRODUCT_ADMTEK_PEGASUS) { 418 if (phy == 3) 419 return (0); 420 } 421 #endif 422 423 aue_lock_mii(sc); 424 aue_csr_write_1(sc, AUE_PHY_ADDR, phy); 425 aue_csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_READ); 426 427 for (i = 0; i < AUE_TIMEOUT; i++) { 428 if (aue_csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE) 429 break; 430 } 431 432 if (i == AUE_TIMEOUT) { 433 printf("%s: MII read timed out\n", USBDEVNAME(sc->aue_dev)); 434 } 435 436 val = aue_csr_read_2(sc, AUE_PHY_DATA); 437 438 DPRINTFN(11,("%s: %s: phy=%d reg=%d => 0x%04x\n", 439 USBDEVNAME(sc->aue_dev), __FUNCTION__, phy, reg, val)); 440 441 aue_unlock_mii(sc); 442 return (val); 443 } 444 445 Static void 446 aue_miibus_writereg(device_ptr_t dev, int phy, int reg, int data) 447 { 448 struct aue_softc *sc = USBGETSOFTC(dev); 449 int i; 450 451 #if 0 452 if (sc->aue_vendor == USB_VENDOR_ADMTEK && 453 sc->aue_product == USB_PRODUCT_ADMTEK_PEGASUS) { 454 if (phy == 3) 455 return; 456 } 457 #endif 458 459 DPRINTFN(11,("%s: %s: phy=%d reg=%d data=0x%04x\n", 460 USBDEVNAME(sc->aue_dev), __FUNCTION__, phy, reg, data)); 461 462 aue_lock_mii(sc); 463 aue_csr_write_2(sc, AUE_PHY_DATA, data); 464 aue_csr_write_1(sc, AUE_PHY_ADDR, phy); 465 aue_csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_WRITE); 466 467 for (i = 0; i < AUE_TIMEOUT; i++) { 468 if (aue_csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE) 469 break; 470 } 471 472 if (i == AUE_TIMEOUT) { 473 printf("%s: MII read timed out\n", 474 USBDEVNAME(sc->aue_dev)); 475 } 476 aue_unlock_mii(sc); 477 } 478 479 Static void 480 aue_miibus_statchg(device_ptr_t dev) 481 { 482 struct aue_softc *sc = USBGETSOFTC(dev); 483 struct mii_data *mii = GET_MII(sc); 484 485 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__)); 486 487 aue_lock_mii(sc); 488 AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB); 489 490 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX) { 491 AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL); 492 } else { 493 AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL); 494 } 495 496 if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) 497 AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX); 498 else 499 AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX); 500 501 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB); 502 aue_unlock_mii(sc); 503 504 /* 505 * Set the LED modes on the LinkSys adapter. 506 * This turns on the 'dual link LED' bin in the auxmode 507 * register of the Broadcom PHY. 508 */ 509 if (sc->aue_linksys) { 510 u_int16_t auxmode; 511 auxmode = aue_miibus_readreg(dev, 0, 0x1b); 512 aue_miibus_writereg(dev, 0, 0x1b, auxmode | 0x04); 513 } 514 } 515 516 #define AUE_POLY 0xEDB88320 517 #define AUE_BITS 6 518 519 Static u_int32_t 520 aue_crc(caddr_t addr) 521 { 522 u_int32_t idx, bit, data, crc; 523 524 /* Compute CRC for the address value. */ 525 crc = 0xFFFFFFFF; /* initial value */ 526 527 for (idx = 0; idx < 6; idx++) { 528 for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1) 529 crc = (crc >> 1) ^ (((crc ^ data) & 1) ? AUE_POLY : 0); 530 } 531 532 return (crc & ((1 << AUE_BITS) - 1)); 533 } 534 535 Static void 536 aue_setmulti(struct aue_softc *sc) 537 { 538 struct ifnet *ifp; 539 struct ether_multi *enm; 540 struct ether_multistep step; 541 u_int32_t h = 0, i; 542 543 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__)); 544 545 ifp = GET_IFP(sc); 546 547 if (ifp->if_flags & IFF_PROMISC) { 548 allmulti: 549 ifp->if_flags |= IFF_ALLMULTI; 550 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI); 551 return; 552 } 553 554 AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI); 555 556 /* first, zot all the existing hash bits */ 557 for (i = 0; i < 8; i++) 558 aue_csr_write_1(sc, AUE_MAR0 + i, 0); 559 560 /* now program new ones */ 561 #if defined(__NetBSD__) 562 ETHER_FIRST_MULTI(step, &sc->aue_ec, enm); 563 #else 564 ETHER_FIRST_MULTI(step, &sc->arpcom, enm); 565 #endif 566 while (enm != NULL) { 567 if (memcmp(enm->enm_addrlo, 568 enm->enm_addrhi, ETHER_ADDR_LEN) != 0) 569 goto allmulti; 570 571 h = aue_crc(enm->enm_addrlo); 572 AUE_SETBIT(sc, AUE_MAR + (h >> 3), 1 << (h & 0x7)); 573 ETHER_NEXT_MULTI(step, enm); 574 } 575 576 ifp->if_flags &= ~IFF_ALLMULTI; 577 } 578 579 Static void 580 aue_reset(struct aue_softc *sc) 581 { 582 int i; 583 584 DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__)); 585 586 AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_RESETMAC); 587 588 for (i = 0; i < AUE_TIMEOUT; i++) { 589 if (!(aue_csr_read_1(sc, AUE_CTL1) & AUE_CTL1_RESETMAC)) 590 break; 591 } 592 593 if (i == AUE_TIMEOUT) 594 printf("%s: reset failed\n", USBDEVNAME(sc->aue_dev)); 595 596 /* 597 * The PHY(s) attached to the Pegasus chip may be held 598 * in reset until we flip on the GPIO outputs. Make sure 599 * to set the GPIO pins high so that the PHY(s) will 600 * be enabled. 601 * 602 * Note: We force all of the GPIO pins low first, *then* 603 * enable the ones we want. 604 */ 605 aue_csr_write_1(sc, AUE_GPIO0, 606 AUE_GPIO_OUT0 | AUE_GPIO_SEL0); 607 aue_csr_write_1(sc, AUE_GPIO0, 608 AUE_GPIO_OUT0 | AUE_GPIO_SEL0 | AUE_GPIO_SEL1); 609 610 /* Grrr. LinkSys has to be different from everyone else. */ 611 if (sc->aue_linksys) { 612 aue_csr_write_1(sc, AUE_GPIO0, 613 AUE_GPIO_SEL0 | AUE_GPIO_SEL1); 614 aue_csr_write_1(sc, AUE_GPIO0, 615 AUE_GPIO_SEL0 | AUE_GPIO_SEL1 | AUE_GPIO_OUT0); 616 } 617 618 /* Wait a little while for the chip to get its brains in order. */ 619 delay(10000); /* XXX */ 620 } 621 622 Static const struct aue_type * 623 aue_lookup(u_int16_t vendor, u_int16_t product) 624 { 625 const struct aue_type *t; 626 627 for (t = aue_devs; t->aue_vid != 0; t++) 628 if (vendor == t->aue_vid && product == t->aue_did) 629 return (t); 630 return (NULL); 631 } 632 633 /* 634 * Probe for a Pegasus chip. 635 */ 636 USB_MATCH(aue) 637 { 638 USB_MATCH_START(aue, uaa); 639 640 if (uaa->iface != NULL) 641 return (UMATCH_NONE); 642 643 return (aue_lookup(uaa->vendor, uaa->product) != NULL ? 644 UMATCH_VENDOR_PRODUCT : UMATCH_NONE); 645 } 646 647 /* 648 * Attach the interface. Allocate softc structures, do ifmedia 649 * setup and ethernet/BPF attach. 650 */ 651 USB_ATTACH(aue) 652 { 653 USB_ATTACH_START(aue, sc, uaa); 654 char devinfo[1024]; 655 int s; 656 u_char eaddr[ETHER_ADDR_LEN]; 657 struct ifnet *ifp; 658 struct mii_data *mii; 659 usbd_device_handle dev = uaa->device; 660 usbd_interface_handle iface; 661 usbd_status err; 662 usb_interface_descriptor_t *id; 663 usb_endpoint_descriptor_t *ed; 664 int i; 665 666 DPRINTFN(5,(" : aue_attach: sc=%p", sc)); 667 668 usbd_devinfo(dev, 0, devinfo); 669 USB_ATTACH_SETUP; 670 printf("%s: %s\n", USBDEVNAME(sc->aue_dev), devinfo); 671 672 err = usbd_set_config_no(dev, AUE_CONFIG_NO, 1); 673 if (err) { 674 printf("%s: setting config no failed\n", 675 USBDEVNAME(sc->aue_dev)); 676 USB_ATTACH_ERROR_RETURN; 677 } 678 679 usb_init_task(&sc->aue_tick_task, aue_tick_task, sc); 680 usb_init_task(&sc->aue_stop_task, (void (*)(void *))aue_stop, sc); 681 lockinit(&sc->aue_mii_lock, PZERO, "auemii", 0, 0); 682 683 err = usbd_device2interface_handle(dev, AUE_IFACE_IDX, &iface); 684 if (err) { 685 printf("%s: getting interface handle failed\n", 686 USBDEVNAME(sc->aue_dev)); 687 USB_ATTACH_ERROR_RETURN; 688 } 689 690 sc->aue_linksys = aue_lookup(uaa->vendor, uaa->product)->aue_linksys; 691 692 sc->aue_udev = dev; 693 sc->aue_iface = iface; 694 sc->aue_product = uaa->product; 695 sc->aue_vendor = uaa->vendor; 696 697 id = usbd_get_interface_descriptor(iface); 698 699 /* Find endpoints. */ 700 for (i = 0; i < id->bNumEndpoints; i++) { 701 ed = usbd_interface2endpoint_descriptor(iface, i); 702 if (ed == NULL) { 703 printf("%s: couldn't get endpoint descriptor %d\n", 704 USBDEVNAME(sc->aue_dev), i); 705 USB_ATTACH_ERROR_RETURN; 706 } 707 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 708 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 709 sc->aue_ed[AUE_ENDPT_RX] = ed->bEndpointAddress; 710 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && 711 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 712 sc->aue_ed[AUE_ENDPT_TX] = ed->bEndpointAddress; 713 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 714 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) { 715 sc->aue_ed[AUE_ENDPT_INTR] = ed->bEndpointAddress; 716 } 717 } 718 719 if (sc->aue_ed[AUE_ENDPT_RX] == 0 || sc->aue_ed[AUE_ENDPT_TX] == 0 || 720 sc->aue_ed[AUE_ENDPT_INTR] == 0) { 721 printf("%s: missing endpoint\n", USBDEVNAME(sc->aue_dev)); 722 USB_ATTACH_ERROR_RETURN; 723 } 724 725 726 s = splnet(); 727 728 /* Reset the adapter. */ 729 aue_reset(sc); 730 731 /* 732 * Get station address from the EEPROM. 733 */ 734 aue_read_mac(sc, eaddr); 735 736 /* 737 * A Pegasus chip was detected. Inform the world. 738 */ 739 ifp = GET_IFP(sc); 740 printf("%s: Ethernet address %s\n", USBDEVNAME(sc->aue_dev), 741 ether_sprintf(eaddr)); 742 743 #if defined(__OpenBSD__) 744 bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN); 745 #endif 746 747 /* Initialize interface info.*/ 748 ifp->if_softc = sc; 749 ifp->if_mtu = ETHERMTU; 750 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 751 ifp->if_ioctl = aue_ioctl; 752 ifp->if_start = aue_start; 753 ifp->if_watchdog = aue_watchdog; 754 IFQ_SET_READY(&ifp->if_snd); 755 strncpy(ifp->if_xname, USBDEVNAME(sc->aue_dev), IFNAMSIZ); 756 757 /* Initialize MII/media info. */ 758 mii = &sc->aue_mii; 759 mii->mii_ifp = ifp; 760 mii->mii_readreg = aue_miibus_readreg; 761 mii->mii_writereg = aue_miibus_writereg; 762 mii->mii_statchg = aue_miibus_statchg; 763 mii->mii_flags = MIIF_AUTOTSLEEP; 764 ifmedia_init(&mii->mii_media, 0, aue_ifmedia_upd, aue_ifmedia_sts); 765 mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0); 766 if (LIST_FIRST(&mii->mii_phys) == NULL) { 767 ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL); 768 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE); 769 } else 770 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO); 771 772 /* Attach the interface. */ 773 if_attach(ifp); 774 Ether_ifattach(ifp, eaddr); 775 #if NRND > 0 776 rnd_attach_source(&sc->rnd_source, USBDEVNAME(sc->aue_dev), 777 RND_TYPE_NET, 0); 778 #endif 779 780 usb_callout_init(sc->aue_stat_ch); 781 782 sc->aue_attached = 1; 783 splx(s); 784 785 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->aue_udev, 786 USBDEV(sc->aue_dev)); 787 788 USB_ATTACH_SUCCESS_RETURN; 789 } 790 791 USB_DETACH(aue) 792 { 793 USB_DETACH_START(aue, sc); 794 struct ifnet *ifp = GET_IFP(sc); 795 int s; 796 797 DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__)); 798 799 usb_uncallout(sc->aue_stat_ch, aue_tick, sc); 800 /* 801 * Remove any pending tasks. They cannot be executing because they run 802 * in the same thread as detach. 803 */ 804 usb_rem_task(sc->aue_udev, &sc->aue_tick_task); 805 usb_rem_task(sc->aue_udev, &sc->aue_stop_task); 806 807 s = splusb(); 808 809 if (!sc->aue_attached) { 810 /* Detached before attached finished, so just bail out. */ 811 splx(s); 812 return (0); 813 } 814 815 if (ifp->if_flags & IFF_RUNNING) 816 aue_stop(sc); 817 818 #if defined(__NetBSD__) 819 #if NRND > 0 820 rnd_detach_source(&sc->rnd_source); 821 #endif 822 #endif 823 824 mii_detach(&sc->aue_mii, MII_PHY_ANY, MII_OFFSET_ANY); 825 ifmedia_delete_instance(&sc->aue_mii.mii_media, IFM_INST_ANY); 826 827 #if defined(__NetBSD__) 828 #if NBPFILTER > 0 829 bpfdetach(ifp); 830 #endif 831 #endif /* __NetBSD__ */ 832 ether_ifdetach(ifp); 833 834 if_detach(ifp); 835 836 #ifdef DIAGNOSTIC 837 if (sc->aue_ep[AUE_ENDPT_TX] != NULL || 838 sc->aue_ep[AUE_ENDPT_RX] != NULL || 839 sc->aue_ep[AUE_ENDPT_INTR] != NULL) 840 printf("%s: detach has active endpoints\n", 841 USBDEVNAME(sc->aue_dev)); 842 #endif 843 844 sc->aue_attached = 0; 845 splx(s); 846 847 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->aue_udev, 848 USBDEV(sc->aue_dev)); 849 850 return (0); 851 } 852 853 int 854 aue_activate(device_ptr_t self, enum devact act) 855 { 856 struct aue_softc *sc = (struct aue_softc *)self; 857 858 DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__)); 859 860 switch (act) { 861 case DVACT_ACTIVATE: 862 return (EOPNOTSUPP); 863 break; 864 865 case DVACT_DEACTIVATE: 866 if_deactivate(&sc->aue_ec.ec_if); 867 sc->aue_dying = 1; 868 break; 869 } 870 return (0); 871 } 872 873 /* 874 * Initialize an RX descriptor and attach an MBUF cluster. 875 */ 876 Static int 877 aue_newbuf(struct aue_softc *sc, struct aue_chain *c, struct mbuf *m) 878 { 879 struct mbuf *m_new = NULL; 880 881 DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__)); 882 883 if (m == NULL) { 884 MGETHDR(m_new, M_DONTWAIT, MT_DATA); 885 if (m_new == NULL) { 886 printf("%s: no memory for rx list " 887 "-- packet dropped!\n", USBDEVNAME(sc->aue_dev)); 888 return (ENOBUFS); 889 } 890 891 MCLGET(m_new, M_DONTWAIT); 892 if (!(m_new->m_flags & M_EXT)) { 893 printf("%s: no memory for rx list " 894 "-- packet dropped!\n", USBDEVNAME(sc->aue_dev)); 895 m_freem(m_new); 896 return (ENOBUFS); 897 } 898 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; 899 } else { 900 m_new = m; 901 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; 902 m_new->m_data = m_new->m_ext.ext_buf; 903 } 904 905 m_adj(m_new, ETHER_ALIGN); 906 c->aue_mbuf = m_new; 907 908 return (0); 909 } 910 911 Static int 912 aue_rx_list_init(struct aue_softc *sc) 913 { 914 struct aue_cdata *cd; 915 struct aue_chain *c; 916 int i; 917 918 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__)); 919 920 cd = &sc->aue_cdata; 921 for (i = 0; i < AUE_RX_LIST_CNT; i++) { 922 c = &cd->aue_rx_chain[i]; 923 c->aue_sc = sc; 924 c->aue_idx = i; 925 if (aue_newbuf(sc, c, NULL) == ENOBUFS) 926 return (ENOBUFS); 927 if (c->aue_xfer == NULL) { 928 c->aue_xfer = usbd_alloc_xfer(sc->aue_udev); 929 if (c->aue_xfer == NULL) 930 return (ENOBUFS); 931 c->aue_buf = usbd_alloc_buffer(c->aue_xfer, AUE_BUFSZ); 932 if (c->aue_buf == NULL) 933 return (ENOBUFS); /* XXX free xfer */ 934 } 935 } 936 937 return (0); 938 } 939 940 Static int 941 aue_tx_list_init(struct aue_softc *sc) 942 { 943 struct aue_cdata *cd; 944 struct aue_chain *c; 945 int i; 946 947 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__)); 948 949 cd = &sc->aue_cdata; 950 for (i = 0; i < AUE_TX_LIST_CNT; i++) { 951 c = &cd->aue_tx_chain[i]; 952 c->aue_sc = sc; 953 c->aue_idx = i; 954 c->aue_mbuf = NULL; 955 if (c->aue_xfer == NULL) { 956 c->aue_xfer = usbd_alloc_xfer(sc->aue_udev); 957 if (c->aue_xfer == NULL) 958 return (ENOBUFS); 959 c->aue_buf = usbd_alloc_buffer(c->aue_xfer, AUE_BUFSZ); 960 if (c->aue_buf == NULL) 961 return (ENOBUFS); 962 } 963 } 964 965 return (0); 966 } 967 968 Static void 969 aue_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status) 970 { 971 struct aue_softc *sc = priv; 972 struct ifnet *ifp = GET_IFP(sc); 973 struct aue_intrpkt *p = &sc->aue_cdata.aue_ibuf; 974 975 DPRINTFN(15,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__)); 976 977 if (sc->aue_dying) 978 return; 979 980 if (!(ifp->if_flags & IFF_RUNNING)) 981 return; 982 983 if (status != USBD_NORMAL_COMPLETION) { 984 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) { 985 return; 986 } 987 sc->aue_intr_errs++; 988 if (usbd_ratecheck(&sc->aue_rx_notice)) { 989 printf("%s: %u usb errors on intr: %s\n", 990 USBDEVNAME(sc->aue_dev), sc->aue_rx_errs, 991 usbd_errstr(status)); 992 sc->aue_intr_errs = 0; 993 } 994 if (status == USBD_STALLED) 995 usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_RX]); 996 return; 997 } 998 999 if (p->aue_txstat0) 1000 ifp->if_oerrors++; 1001 1002 if (p->aue_txstat0 & (AUE_TXSTAT0_LATECOLL | AUE_TXSTAT0_EXCESSCOLL)) 1003 ifp->if_collisions++; 1004 } 1005 1006 /* 1007 * A frame has been uploaded: pass the resulting mbuf chain up to 1008 * the higher level protocols. 1009 */ 1010 Static void 1011 aue_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status) 1012 { 1013 struct aue_chain *c = priv; 1014 struct aue_softc *sc = c->aue_sc; 1015 struct ifnet *ifp = GET_IFP(sc); 1016 struct mbuf *m; 1017 u_int32_t total_len; 1018 struct aue_rxpkt r; 1019 int s; 1020 1021 DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__)); 1022 1023 if (sc->aue_dying) 1024 return; 1025 1026 if (!(ifp->if_flags & IFF_RUNNING)) 1027 return; 1028 1029 if (status != USBD_NORMAL_COMPLETION) { 1030 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) 1031 return; 1032 sc->aue_rx_errs++; 1033 if (usbd_ratecheck(&sc->aue_rx_notice)) { 1034 printf("%s: %u usb errors on rx: %s\n", 1035 USBDEVNAME(sc->aue_dev), sc->aue_rx_errs, 1036 usbd_errstr(status)); 1037 sc->aue_rx_errs = 0; 1038 } 1039 if (status == USBD_STALLED) 1040 usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_RX]); 1041 goto done; 1042 } 1043 1044 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL); 1045 1046 memcpy(mtod(c->aue_mbuf, char*), c->aue_buf, total_len); 1047 1048 if (total_len <= 4 + ETHER_CRC_LEN) { 1049 ifp->if_ierrors++; 1050 goto done; 1051 } 1052 1053 memcpy(&r, c->aue_buf + total_len - 4, sizeof(r)); 1054 1055 /* Turn off all the non-error bits in the rx status word. */ 1056 r.aue_rxstat &= AUE_RXSTAT_MASK; 1057 if (r.aue_rxstat) { 1058 ifp->if_ierrors++; 1059 goto done; 1060 } 1061 1062 /* No errors; receive the packet. */ 1063 m = c->aue_mbuf; 1064 total_len -= ETHER_CRC_LEN + 4; 1065 m->m_pkthdr.len = m->m_len = total_len; 1066 ifp->if_ipackets++; 1067 1068 m->m_pkthdr.rcvif = ifp; 1069 1070 s = splnet(); 1071 1072 /* XXX ugly */ 1073 if (aue_newbuf(sc, c, NULL) == ENOBUFS) { 1074 ifp->if_ierrors++; 1075 goto done1; 1076 } 1077 1078 #if NBPFILTER > 0 1079 /* 1080 * Handle BPF listeners. Let the BPF user see the packet, but 1081 * don't pass it up to the ether_input() layer unless it's 1082 * a broadcast packet, multicast packet, matches our ethernet 1083 * address or the interface is in promiscuous mode. 1084 */ 1085 if (ifp->if_bpf) 1086 BPF_MTAP(ifp, m); 1087 #endif 1088 1089 DPRINTFN(10,("%s: %s: deliver %d\n", USBDEVNAME(sc->aue_dev), 1090 __FUNCTION__, m->m_len)); 1091 IF_INPUT(ifp, m); 1092 done1: 1093 splx(s); 1094 1095 done: 1096 1097 /* Setup new transfer. */ 1098 usbd_setup_xfer(xfer, sc->aue_ep[AUE_ENDPT_RX], 1099 c, c->aue_buf, AUE_BUFSZ, 1100 USBD_SHORT_XFER_OK | USBD_NO_COPY, 1101 USBD_NO_TIMEOUT, aue_rxeof); 1102 usbd_transfer(xfer); 1103 1104 DPRINTFN(10,("%s: %s: start rx\n", USBDEVNAME(sc->aue_dev), 1105 __FUNCTION__)); 1106 } 1107 1108 /* 1109 * A frame was downloaded to the chip. It's safe for us to clean up 1110 * the list buffers. 1111 */ 1112 1113 Static void 1114 aue_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status) 1115 { 1116 struct aue_chain *c = priv; 1117 struct aue_softc *sc = c->aue_sc; 1118 struct ifnet *ifp = GET_IFP(sc); 1119 int s; 1120 1121 if (sc->aue_dying) 1122 return; 1123 1124 s = splnet(); 1125 1126 DPRINTFN(10,("%s: %s: enter status=%d\n", USBDEVNAME(sc->aue_dev), 1127 __FUNCTION__, status)); 1128 1129 ifp->if_timer = 0; 1130 ifp->if_flags &= ~IFF_OACTIVE; 1131 1132 if (status != USBD_NORMAL_COMPLETION) { 1133 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) { 1134 splx(s); 1135 return; 1136 } 1137 ifp->if_oerrors++; 1138 printf("%s: usb error on tx: %s\n", USBDEVNAME(sc->aue_dev), 1139 usbd_errstr(status)); 1140 if (status == USBD_STALLED) 1141 usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_TX]); 1142 splx(s); 1143 return; 1144 } 1145 1146 ifp->if_opackets++; 1147 1148 m_freem(c->aue_mbuf); 1149 c->aue_mbuf = NULL; 1150 1151 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) 1152 aue_start(ifp); 1153 1154 splx(s); 1155 } 1156 1157 Static void 1158 aue_tick(void *xsc) 1159 { 1160 struct aue_softc *sc = xsc; 1161 1162 DPRINTFN(15,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__)); 1163 1164 if (sc == NULL) 1165 return; 1166 1167 if (sc->aue_dying) 1168 return; 1169 1170 /* Perform periodic stuff in process context. */ 1171 usb_add_task(sc->aue_udev, &sc->aue_tick_task); 1172 } 1173 1174 Static void 1175 aue_tick_task(void *xsc) 1176 { 1177 struct aue_softc *sc = xsc; 1178 struct ifnet *ifp; 1179 struct mii_data *mii; 1180 int s; 1181 1182 DPRINTFN(15,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__)); 1183 1184 if (sc->aue_dying) 1185 return; 1186 1187 ifp = GET_IFP(sc); 1188 mii = GET_MII(sc); 1189 if (mii == NULL) 1190 return; 1191 1192 s = splnet(); 1193 1194 mii_tick(mii); 1195 if (!sc->aue_link) { 1196 mii_pollstat(mii); 1197 if (mii->mii_media_status & IFM_ACTIVE && 1198 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) { 1199 DPRINTFN(2,("%s: %s: got link\n", 1200 USBDEVNAME(sc->aue_dev),__FUNCTION__)); 1201 sc->aue_link++; 1202 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) 1203 aue_start(ifp); 1204 } 1205 } 1206 1207 usb_callout(sc->aue_stat_ch, hz, aue_tick, sc); 1208 1209 splx(s); 1210 } 1211 1212 Static int 1213 aue_send(struct aue_softc *sc, struct mbuf *m, int idx) 1214 { 1215 int total_len; 1216 struct aue_chain *c; 1217 usbd_status err; 1218 1219 DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__)); 1220 1221 c = &sc->aue_cdata.aue_tx_chain[idx]; 1222 1223 /* 1224 * Copy the mbuf data into a contiguous buffer, leaving two 1225 * bytes at the beginning to hold the frame length. 1226 */ 1227 m_copydata(m, 0, m->m_pkthdr.len, c->aue_buf + 2); 1228 c->aue_mbuf = m; 1229 1230 /* 1231 * The ADMtek documentation says that the packet length is 1232 * supposed to be specified in the first two bytes of the 1233 * transfer, however it actually seems to ignore this info 1234 * and base the frame size on the bulk transfer length. 1235 */ 1236 c->aue_buf[0] = (u_int8_t)m->m_pkthdr.len; 1237 c->aue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8); 1238 total_len = m->m_pkthdr.len + 2; 1239 1240 usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_TX], 1241 c, c->aue_buf, total_len, USBD_FORCE_SHORT_XFER | USBD_NO_COPY, 1242 AUE_TX_TIMEOUT, aue_txeof); 1243 1244 /* Transmit */ 1245 err = usbd_transfer(c->aue_xfer); 1246 if (err != USBD_IN_PROGRESS) { 1247 printf("%s: aue_send error=%s\n", USBDEVNAME(sc->aue_dev), 1248 usbd_errstr(err)); 1249 /* Stop the interface from process context. */ 1250 usb_add_task(sc->aue_udev, &sc->aue_stop_task); 1251 return (EIO); 1252 } 1253 DPRINTFN(5,("%s: %s: send %d bytes\n", USBDEVNAME(sc->aue_dev), 1254 __FUNCTION__, total_len)); 1255 1256 sc->aue_cdata.aue_tx_cnt++; 1257 1258 return (0); 1259 } 1260 1261 Static void 1262 aue_start(struct ifnet *ifp) 1263 { 1264 struct aue_softc *sc = ifp->if_softc; 1265 struct mbuf *m_head = NULL; 1266 1267 DPRINTFN(5,("%s: %s: enter, link=%d\n", USBDEVNAME(sc->aue_dev), 1268 __FUNCTION__, sc->aue_link)); 1269 1270 if (sc->aue_dying) 1271 return; 1272 1273 if (!sc->aue_link) 1274 return; 1275 1276 if (ifp->if_flags & IFF_OACTIVE) 1277 return; 1278 1279 IFQ_POLL(&ifp->if_snd, m_head); 1280 if (m_head == NULL) 1281 return; 1282 1283 if (aue_send(sc, m_head, 0)) { 1284 ifp->if_flags |= IFF_OACTIVE; 1285 return; 1286 } 1287 1288 IFQ_DEQUEUE(&ifp->if_snd, m_head); 1289 1290 #if NBPFILTER > 0 1291 /* 1292 * If there's a BPF listener, bounce a copy of this frame 1293 * to him. 1294 */ 1295 if (ifp->if_bpf) 1296 BPF_MTAP(ifp, m_head); 1297 #endif 1298 1299 ifp->if_flags |= IFF_OACTIVE; 1300 1301 /* 1302 * Set a timeout in case the chip goes out to lunch. 1303 */ 1304 ifp->if_timer = 5; 1305 } 1306 1307 Static void 1308 aue_init(void *xsc) 1309 { 1310 struct aue_softc *sc = xsc; 1311 struct ifnet *ifp = GET_IFP(sc); 1312 struct mii_data *mii = GET_MII(sc); 1313 int i, s; 1314 u_char *eaddr; 1315 1316 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__)); 1317 1318 if (sc->aue_dying) 1319 return; 1320 1321 if (ifp->if_flags & IFF_RUNNING) 1322 return; 1323 1324 s = splnet(); 1325 1326 /* 1327 * Cancel pending I/O and free all RX/TX buffers. 1328 */ 1329 aue_reset(sc); 1330 1331 #if defined(__OpenBSD__) 1332 eaddr = sc->arpcom.ac_enaddr; 1333 #elif defined(__NetBSD__) 1334 eaddr = LLADDR(ifp->if_sadl); 1335 #endif /* defined(__NetBSD__) */ 1336 for (i = 0; i < ETHER_ADDR_LEN; i++) 1337 aue_csr_write_1(sc, AUE_PAR0 + i, eaddr[i]); 1338 1339 /* If we want promiscuous mode, set the allframes bit. */ 1340 if (ifp->if_flags & IFF_PROMISC) 1341 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC); 1342 else 1343 AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC); 1344 1345 /* Init TX ring. */ 1346 if (aue_tx_list_init(sc) == ENOBUFS) { 1347 printf("%s: tx list init failed\n", USBDEVNAME(sc->aue_dev)); 1348 splx(s); 1349 return; 1350 } 1351 1352 /* Init RX ring. */ 1353 if (aue_rx_list_init(sc) == ENOBUFS) { 1354 printf("%s: rx list init failed\n", USBDEVNAME(sc->aue_dev)); 1355 splx(s); 1356 return; 1357 } 1358 1359 /* Load the multicast filter. */ 1360 aue_setmulti(sc); 1361 1362 /* Enable RX and TX */ 1363 aue_csr_write_1(sc, AUE_CTL0, AUE_CTL0_RXSTAT_APPEND | AUE_CTL0_RX_ENB); 1364 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_TX_ENB); 1365 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_EP3_CLR); 1366 1367 mii_mediachg(mii); 1368 1369 if (sc->aue_ep[AUE_ENDPT_RX] == NULL) { 1370 if (aue_openpipes(sc)) { 1371 splx(s); 1372 return; 1373 } 1374 } 1375 1376 ifp->if_flags |= IFF_RUNNING; 1377 ifp->if_flags &= ~IFF_OACTIVE; 1378 1379 splx(s); 1380 1381 usb_callout(sc->aue_stat_ch, hz, aue_tick, sc); 1382 } 1383 1384 Static int 1385 aue_openpipes(struct aue_softc *sc) 1386 { 1387 struct aue_chain *c; 1388 usbd_status err; 1389 int i; 1390 1391 /* Open RX and TX pipes. */ 1392 err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_RX], 1393 USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_RX]); 1394 if (err) { 1395 printf("%s: open rx pipe failed: %s\n", 1396 USBDEVNAME(sc->aue_dev), usbd_errstr(err)); 1397 return (EIO); 1398 } 1399 err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_TX], 1400 USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_TX]); 1401 if (err) { 1402 printf("%s: open tx pipe failed: %s\n", 1403 USBDEVNAME(sc->aue_dev), usbd_errstr(err)); 1404 return (EIO); 1405 } 1406 err = usbd_open_pipe_intr(sc->aue_iface, sc->aue_ed[AUE_ENDPT_INTR], 1407 USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_INTR], sc, 1408 &sc->aue_cdata.aue_ibuf, AUE_INTR_PKTLEN, aue_intr, 1409 AUE_INTR_INTERVAL); 1410 if (err) { 1411 printf("%s: open intr pipe failed: %s\n", 1412 USBDEVNAME(sc->aue_dev), usbd_errstr(err)); 1413 return (EIO); 1414 } 1415 1416 /* Start up the receive pipe. */ 1417 for (i = 0; i < AUE_RX_LIST_CNT; i++) { 1418 c = &sc->aue_cdata.aue_rx_chain[i]; 1419 usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_RX], 1420 c, c->aue_buf, AUE_BUFSZ, 1421 USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT, 1422 aue_rxeof); 1423 (void)usbd_transfer(c->aue_xfer); /* XXX */ 1424 DPRINTFN(5,("%s: %s: start read\n", USBDEVNAME(sc->aue_dev), 1425 __FUNCTION__)); 1426 1427 } 1428 return (0); 1429 } 1430 1431 /* 1432 * Set media options. 1433 */ 1434 Static int 1435 aue_ifmedia_upd(struct ifnet *ifp) 1436 { 1437 struct aue_softc *sc = ifp->if_softc; 1438 struct mii_data *mii = GET_MII(sc); 1439 1440 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__)); 1441 1442 if (sc->aue_dying) 1443 return (0); 1444 1445 sc->aue_link = 0; 1446 if (mii->mii_instance) { 1447 struct mii_softc *miisc; 1448 for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL; 1449 miisc = LIST_NEXT(miisc, mii_list)) 1450 mii_phy_reset(miisc); 1451 } 1452 mii_mediachg(mii); 1453 1454 return (0); 1455 } 1456 1457 /* 1458 * Report current media status. 1459 */ 1460 Static void 1461 aue_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr) 1462 { 1463 struct aue_softc *sc = ifp->if_softc; 1464 struct mii_data *mii = GET_MII(sc); 1465 1466 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__)); 1467 1468 mii_pollstat(mii); 1469 ifmr->ifm_active = mii->mii_media_active; 1470 ifmr->ifm_status = mii->mii_media_status; 1471 } 1472 1473 Static int 1474 aue_ioctl(struct ifnet *ifp, u_long command, caddr_t data) 1475 { 1476 struct aue_softc *sc = ifp->if_softc; 1477 struct ifaddr *ifa = (struct ifaddr *)data; 1478 struct ifreq *ifr = (struct ifreq *)data; 1479 struct mii_data *mii; 1480 int s, error = 0; 1481 1482 if (sc->aue_dying) 1483 return (EIO); 1484 1485 s = splnet(); 1486 1487 switch(command) { 1488 case SIOCSIFADDR: 1489 ifp->if_flags |= IFF_UP; 1490 aue_init(sc); 1491 1492 switch (ifa->ifa_addr->sa_family) { 1493 #ifdef INET 1494 case AF_INET: 1495 #if defined(__NetBSD__) 1496 arp_ifinit(ifp, ifa); 1497 #else 1498 arp_ifinit(&sc->arpcom, ifa); 1499 #endif 1500 break; 1501 #endif /* INET */ 1502 #ifdef NS 1503 case AF_NS: 1504 { 1505 struct ns_addr *ina = &IA_SNS(ifa)->sns_addr; 1506 1507 if (ns_nullhost(*ina)) 1508 ina->x_host = *(union ns_host *) 1509 LLADDR(ifp->if_sadl); 1510 else 1511 memcpy(LLADDR(ifp->if_sadl), 1512 ina->x_host.c_host, 1513 ifp->if_addrlen); 1514 break; 1515 } 1516 #endif /* NS */ 1517 } 1518 break; 1519 1520 case SIOCSIFMTU: 1521 if (ifr->ifr_mtu > ETHERMTU) 1522 error = EINVAL; 1523 else 1524 ifp->if_mtu = ifr->ifr_mtu; 1525 break; 1526 1527 case SIOCSIFFLAGS: 1528 if (ifp->if_flags & IFF_UP) { 1529 if (ifp->if_flags & IFF_RUNNING && 1530 ifp->if_flags & IFF_PROMISC && 1531 !(sc->aue_if_flags & IFF_PROMISC)) { 1532 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC); 1533 } else if (ifp->if_flags & IFF_RUNNING && 1534 !(ifp->if_flags & IFF_PROMISC) && 1535 sc->aue_if_flags & IFF_PROMISC) { 1536 AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC); 1537 } else if (!(ifp->if_flags & IFF_RUNNING)) 1538 aue_init(sc); 1539 } else { 1540 if (ifp->if_flags & IFF_RUNNING) 1541 aue_stop(sc); 1542 } 1543 sc->aue_if_flags = ifp->if_flags; 1544 error = 0; 1545 break; 1546 case SIOCADDMULTI: 1547 case SIOCDELMULTI: 1548 error = (command == SIOCADDMULTI) ? 1549 #if defined(__NetBSD__) 1550 ether_addmulti(ifr, &sc->aue_ec) : 1551 ether_delmulti(ifr, &sc->aue_ec); 1552 #else 1553 ether_addmulti(ifr, &sc->arpcom) : 1554 ether_delmulti(ifr, &sc->arpcom); 1555 #endif 1556 if (error == ENETRESET) { 1557 aue_init(sc); 1558 } 1559 aue_setmulti(sc); 1560 error = 0; 1561 break; 1562 case SIOCGIFMEDIA: 1563 case SIOCSIFMEDIA: 1564 mii = GET_MII(sc); 1565 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command); 1566 break; 1567 default: 1568 error = EINVAL; 1569 break; 1570 } 1571 1572 splx(s); 1573 1574 return (error); 1575 } 1576 1577 Static void 1578 aue_watchdog(struct ifnet *ifp) 1579 { 1580 struct aue_softc *sc = ifp->if_softc; 1581 struct aue_chain *c; 1582 usbd_status stat; 1583 int s; 1584 1585 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__)); 1586 1587 ifp->if_oerrors++; 1588 printf("%s: watchdog timeout\n", USBDEVNAME(sc->aue_dev)); 1589 1590 s = splusb(); 1591 c = &sc->aue_cdata.aue_tx_chain[0]; 1592 usbd_get_xfer_status(c->aue_xfer, NULL, NULL, NULL, &stat); 1593 aue_txeof(c->aue_xfer, c, stat); 1594 1595 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) 1596 aue_start(ifp); 1597 splx(s); 1598 } 1599 1600 /* 1601 * Stop the adapter and free any mbufs allocated to the 1602 * RX and TX lists. 1603 */ 1604 Static void 1605 aue_stop(struct aue_softc *sc) 1606 { 1607 usbd_status err; 1608 struct ifnet *ifp; 1609 int i; 1610 1611 DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__)); 1612 1613 ifp = GET_IFP(sc); 1614 ifp->if_timer = 0; 1615 1616 aue_csr_write_1(sc, AUE_CTL0, 0); 1617 aue_csr_write_1(sc, AUE_CTL1, 0); 1618 aue_reset(sc); 1619 usb_uncallout(sc->aue_stat_ch, aue_tick, sc); 1620 1621 /* Stop transfers. */ 1622 if (sc->aue_ep[AUE_ENDPT_RX] != NULL) { 1623 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_RX]); 1624 if (err) { 1625 printf("%s: abort rx pipe failed: %s\n", 1626 USBDEVNAME(sc->aue_dev), usbd_errstr(err)); 1627 } 1628 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_RX]); 1629 if (err) { 1630 printf("%s: close rx pipe failed: %s\n", 1631 USBDEVNAME(sc->aue_dev), usbd_errstr(err)); 1632 } 1633 sc->aue_ep[AUE_ENDPT_RX] = NULL; 1634 } 1635 1636 if (sc->aue_ep[AUE_ENDPT_TX] != NULL) { 1637 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_TX]); 1638 if (err) { 1639 printf("%s: abort tx pipe failed: %s\n", 1640 USBDEVNAME(sc->aue_dev), usbd_errstr(err)); 1641 } 1642 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_TX]); 1643 if (err) { 1644 printf("%s: close tx pipe failed: %s\n", 1645 USBDEVNAME(sc->aue_dev), usbd_errstr(err)); 1646 } 1647 sc->aue_ep[AUE_ENDPT_TX] = NULL; 1648 } 1649 1650 if (sc->aue_ep[AUE_ENDPT_INTR] != NULL) { 1651 err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_INTR]); 1652 if (err) { 1653 printf("%s: abort intr pipe failed: %s\n", 1654 USBDEVNAME(sc->aue_dev), usbd_errstr(err)); 1655 } 1656 err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_INTR]); 1657 if (err) { 1658 printf("%s: close intr pipe failed: %s\n", 1659 USBDEVNAME(sc->aue_dev), usbd_errstr(err)); 1660 } 1661 sc->aue_ep[AUE_ENDPT_INTR] = NULL; 1662 } 1663 1664 /* Free RX resources. */ 1665 for (i = 0; i < AUE_RX_LIST_CNT; i++) { 1666 if (sc->aue_cdata.aue_rx_chain[i].aue_mbuf != NULL) { 1667 m_freem(sc->aue_cdata.aue_rx_chain[i].aue_mbuf); 1668 sc->aue_cdata.aue_rx_chain[i].aue_mbuf = NULL; 1669 } 1670 if (sc->aue_cdata.aue_rx_chain[i].aue_xfer != NULL) { 1671 usbd_free_xfer(sc->aue_cdata.aue_rx_chain[i].aue_xfer); 1672 sc->aue_cdata.aue_rx_chain[i].aue_xfer = NULL; 1673 } 1674 } 1675 1676 /* Free TX resources. */ 1677 for (i = 0; i < AUE_TX_LIST_CNT; i++) { 1678 if (sc->aue_cdata.aue_tx_chain[i].aue_mbuf != NULL) { 1679 m_freem(sc->aue_cdata.aue_tx_chain[i].aue_mbuf); 1680 sc->aue_cdata.aue_tx_chain[i].aue_mbuf = NULL; 1681 } 1682 if (sc->aue_cdata.aue_tx_chain[i].aue_xfer != NULL) { 1683 usbd_free_xfer(sc->aue_cdata.aue_tx_chain[i].aue_xfer); 1684 sc->aue_cdata.aue_tx_chain[i].aue_xfer = NULL; 1685 } 1686 } 1687 1688 sc->aue_link = 0; 1689 1690 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); 1691 } 1692