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