1 /* $OpenBSD: if_aue.c,v 1.76 2009/08/10 20:02:19 deraadt Exp $ */ 2 /* $NetBSD: if_aue.c,v 1.82 2003/03/05 17:37:36 shiba 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 and AN8511 Pegasus II USB to ethernet driver. 39 * Datasheet is available 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 #include "bpfilter.h" 81 82 #include <sys/param.h> 83 #include <sys/systm.h> 84 #include <sys/sockio.h> 85 #include <sys/rwlock.h> 86 #include <sys/mbuf.h> 87 #include <sys/kernel.h> 88 #include <sys/proc.h> 89 #include <sys/socket.h> 90 91 #include <sys/device.h> 92 93 #include <net/if.h> 94 #include <net/if_dl.h> 95 #include <net/if_media.h> 96 97 #if NBPFILTER > 0 98 #include <net/bpf.h> 99 #endif 100 101 #ifdef INET 102 #include <netinet/in.h> 103 #include <netinet/in_systm.h> 104 #include <netinet/in_var.h> 105 #include <netinet/ip.h> 106 #include <netinet/if_ether.h> 107 #endif 108 109 #include <dev/mii/mii.h> 110 #include <dev/mii/miivar.h> 111 112 #include <dev/usb/usb.h> 113 #include <dev/usb/usbdi.h> 114 #include <dev/usb/usbdi_util.h> 115 #include <dev/usb/usbdevs.h> 116 117 #include <dev/usb/if_auereg.h> 118 119 #ifdef AUE_DEBUG 120 #define DPRINTF(x) do { if (auedebug) printf x; } while (0) 121 #define DPRINTFN(n,x) do { if (auedebug >= (n)) printf x; } while (0) 122 int auedebug = 0; 123 #else 124 #define DPRINTF(x) 125 #define DPRINTFN(n,x) 126 #endif 127 128 /* 129 * Various supported device vendors/products. 130 */ 131 struct aue_type { 132 struct usb_devno aue_dev; 133 u_int16_t aue_flags; 134 #define LSYS 0x0001 /* use Linksys reset */ 135 #define PNA 0x0002 /* has Home PNA */ 136 #define PII 0x0004 /* Pegasus II chip */ 137 }; 138 139 const struct aue_type aue_devs[] = { 140 {{ USB_VENDOR_3COM, USB_PRODUCT_3COM_3C460B}, PII }, 141 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX1}, PNA|PII }, 142 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX2}, PII }, 143 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_UFE1000}, LSYS }, 144 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX4}, PNA }, 145 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX5}, PNA }, 146 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX6}, PII }, 147 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX7}, PII }, 148 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX8}, PII }, 149 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX9}, PNA }, 150 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX10}, 0 }, 151 {{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_DSB650TX_PNA}, 0 }, 152 {{ USB_VENDOR_ACCTON, USB_PRODUCT_ACCTON_USB320_EC}, 0 }, 153 {{ USB_VENDOR_ACCTON, USB_PRODUCT_ACCTON_SS1001}, PII }, 154 {{ USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUS}, PNA }, 155 {{ USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUSII}, PII }, 156 {{ USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUSII_2}, PII }, 157 {{ USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUSII_3}, PII }, 158 {{ USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUSII_4}, PII }, 159 {{ USB_VENDOR_AEI, USB_PRODUCT_AEI_FASTETHERNET}, PII }, 160 {{ USB_VENDOR_ALLIEDTELESYN, USB_PRODUCT_ALLIEDTELESYN_ATUSB100}, PII }, 161 {{ USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC110T}, PII }, 162 {{ USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5D5050}, PII }, 163 {{ USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USB100}, 0 }, 164 {{ USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USBLP100}, PNA }, 165 {{ USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USBEL100}, 0 }, 166 {{ USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USBE100}, PII }, 167 {{ USB_VENDOR_COREGA, USB_PRODUCT_COREGA_FETHER_USB_TX}, 0 }, 168 {{ USB_VENDOR_COREGA, USB_PRODUCT_COREGA_FETHER_USB_TXS},PII }, 169 {{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX4}, LSYS|PII }, 170 {{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX1}, LSYS }, 171 {{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX}, LSYS }, 172 {{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX_PNA}, PNA }, 173 {{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX3}, LSYS|PII }, 174 {{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX2}, LSYS|PII }, 175 {{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650}, 0 }, 176 {{ USB_VENDOR_ELCON, USB_PRODUCT_ELCON_PLAN}, PNA|PII }, 177 {{ USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSB20}, PII }, 178 {{ USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBTX0}, 0 }, 179 {{ USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBTX1}, LSYS }, 180 {{ USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBTX2}, 0 }, 181 {{ USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBTX3}, LSYS }, 182 {{ USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBLTX}, PII }, 183 {{ USB_VENDOR_ELSA, USB_PRODUCT_ELSA_USB2ETHERNET}, 0 }, 184 {{ USB_VENDOR_GIGABYTE, USB_PRODUCT_GIGABYTE_GNBR402W}, 0 }, 185 {{ USB_VENDOR_HAWKING, USB_PRODUCT_HAWKING_UF100}, PII }, 186 {{ USB_VENDOR_HP, USB_PRODUCT_HP_HN210E}, PII }, 187 {{ USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBETTX}, 0 }, 188 {{ USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBETTXS}, PII }, 189 {{ USB_VENDOR_KINGSTON, USB_PRODUCT_KINGSTON_KNU101TX}, 0 }, 190 {{ USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10TX1}, LSYS|PII }, 191 {{ USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10T}, LSYS }, 192 {{ USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB100TX}, LSYS }, 193 {{ USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB100H1}, LSYS|PNA }, 194 {{ USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10TA}, LSYS }, 195 {{ USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10TX2}, LSYS|PII }, 196 {{ USB_VENDOR_MICROSOFT, USB_PRODUCT_MICROSOFT_MN110}, PII }, 197 {{ USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUATX1}, 0 }, 198 {{ USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUATX5}, 0 }, 199 {{ USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUA2TX5}, PII }, 200 {{ USB_VENDOR_MOBILITY, USB_PRODUCT_MOBILITY_EASIDOCK}, 0 }, 201 {{ USB_VENDOR_NETGEAR, USB_PRODUCT_NETGEAR_FA101}, PII }, 202 {{ USB_VENDOR_OCT, USB_PRODUCT_OCT_USBTOETHER}, PII }, 203 {{ USB_VENDOR_SIEMENS, USB_PRODUCT_SIEMENS_SPEEDSTREAM}, PII }, 204 {{ USB_VENDOR_SMARTBRIDGES, USB_PRODUCT_SMARTBRIDGES_SMARTNIC},PII }, 205 {{ USB_VENDOR_SMC, USB_PRODUCT_SMC_2202USB}, 0 }, 206 {{ USB_VENDOR_SMC, USB_PRODUCT_SMC_2206USB}, PII }, 207 {{ USB_VENDOR_SOHOWARE, USB_PRODUCT_SOHOWARE_NUB100}, 0 }, 208 {{ USB_VENDOR_SOHOWARE, USB_PRODUCT_SOHOWARE_NUB110}, PII }, 209 {{ USB_VENDOR_LOGITEC, USB_PRODUCT_LOGITEC_LANTX}, PII }, 210 }; 211 #define aue_lookup(v, p) ((struct aue_type *)usb_lookup(aue_devs, v, p)) 212 213 int aue_match(struct device *, void *, void *); 214 void aue_attach(struct device *, struct device *, void *); 215 int aue_detach(struct device *, int); 216 int aue_activate(struct device *, enum devact); 217 218 struct cfdriver aue_cd = { 219 NULL, "aue", DV_IFNET 220 }; 221 222 const struct cfattach aue_ca = { 223 sizeof(struct aue_softc), 224 aue_match, 225 aue_attach, 226 aue_detach, 227 aue_activate, 228 }; 229 230 void aue_reset_pegasus_II(struct aue_softc *sc); 231 int aue_tx_list_init(struct aue_softc *); 232 int aue_rx_list_init(struct aue_softc *); 233 int aue_newbuf(struct aue_softc *, struct aue_chain *, struct mbuf *); 234 int aue_send(struct aue_softc *, struct mbuf *, int); 235 void aue_intr(usbd_xfer_handle, usbd_private_handle, usbd_status); 236 void aue_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status); 237 void aue_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status); 238 void aue_tick(void *); 239 void aue_tick_task(void *); 240 void aue_start(struct ifnet *); 241 int aue_ioctl(struct ifnet *, u_long, caddr_t); 242 void aue_init(void *); 243 void aue_stop(struct aue_softc *); 244 void aue_watchdog(struct ifnet *); 245 int aue_openpipes(struct aue_softc *); 246 int aue_ifmedia_upd(struct ifnet *); 247 void aue_ifmedia_sts(struct ifnet *, struct ifmediareq *); 248 249 int aue_eeprom_getword(struct aue_softc *, int); 250 void aue_read_mac(struct aue_softc *, u_char *); 251 int aue_miibus_readreg(struct device *, int, int); 252 void aue_miibus_writereg(struct device *, int, int, int); 253 void aue_miibus_statchg(struct device *); 254 255 void aue_lock_mii(struct aue_softc *); 256 void aue_unlock_mii(struct aue_softc *); 257 258 void aue_setmulti(struct aue_softc *); 259 u_int32_t aue_crc(caddr_t); 260 void aue_reset(struct aue_softc *); 261 262 int aue_csr_read_1(struct aue_softc *, int); 263 int aue_csr_write_1(struct aue_softc *, int, int); 264 int aue_csr_read_2(struct aue_softc *, int); 265 int aue_csr_write_2(struct aue_softc *, int, int); 266 267 #define AUE_SETBIT(sc, reg, x) \ 268 aue_csr_write_1(sc, reg, aue_csr_read_1(sc, reg) | (x)) 269 270 #define AUE_CLRBIT(sc, reg, x) \ 271 aue_csr_write_1(sc, reg, aue_csr_read_1(sc, reg) & ~(x)) 272 273 int 274 aue_csr_read_1(struct aue_softc *sc, int reg) 275 { 276 usb_device_request_t req; 277 usbd_status err; 278 uByte val = 0; 279 280 if (sc->aue_dying) 281 return (0); 282 283 req.bmRequestType = UT_READ_VENDOR_DEVICE; 284 req.bRequest = AUE_UR_READREG; 285 USETW(req.wValue, 0); 286 USETW(req.wIndex, reg); 287 USETW(req.wLength, 1); 288 289 err = usbd_do_request(sc->aue_udev, &req, &val); 290 291 if (err) { 292 DPRINTF(("%s: aue_csr_read_1: reg=0x%x err=%s\n", 293 sc->aue_dev.dv_xname, reg, usbd_errstr(err))); 294 return (0); 295 } 296 297 return (val); 298 } 299 300 int 301 aue_csr_read_2(struct aue_softc *sc, int reg) 302 { 303 usb_device_request_t req; 304 usbd_status err; 305 uWord val; 306 307 if (sc->aue_dying) 308 return (0); 309 310 req.bmRequestType = UT_READ_VENDOR_DEVICE; 311 req.bRequest = AUE_UR_READREG; 312 USETW(req.wValue, 0); 313 USETW(req.wIndex, reg); 314 USETW(req.wLength, 2); 315 316 err = usbd_do_request(sc->aue_udev, &req, &val); 317 318 if (err) { 319 DPRINTF(("%s: aue_csr_read_2: reg=0x%x err=%s\n", 320 sc->aue_dev.dv_xname, reg, usbd_errstr(err))); 321 return (0); 322 } 323 324 return (UGETW(val)); 325 } 326 327 int 328 aue_csr_write_1(struct aue_softc *sc, int reg, int aval) 329 { 330 usb_device_request_t req; 331 usbd_status err; 332 uByte val; 333 334 if (sc->aue_dying) 335 return (0); 336 337 val = aval; 338 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 339 req.bRequest = AUE_UR_WRITEREG; 340 USETW(req.wValue, val); 341 USETW(req.wIndex, reg); 342 USETW(req.wLength, 1); 343 344 err = usbd_do_request(sc->aue_udev, &req, &val); 345 346 if (err) { 347 DPRINTF(("%s: aue_csr_write_1: reg=0x%x err=%s\n", 348 sc->aue_dev.dv_xname, reg, usbd_errstr(err))); 349 return (-1); 350 } 351 352 return (0); 353 } 354 355 int 356 aue_csr_write_2(struct aue_softc *sc, int reg, int aval) 357 { 358 usb_device_request_t req; 359 usbd_status err; 360 uWord val; 361 362 if (sc->aue_dying) 363 return (0); 364 365 USETW(val, aval); 366 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 367 req.bRequest = AUE_UR_WRITEREG; 368 USETW(req.wValue, aval); 369 USETW(req.wIndex, reg); 370 USETW(req.wLength, 2); 371 372 err = usbd_do_request(sc->aue_udev, &req, &val); 373 374 if (err) { 375 DPRINTF(("%s: aue_csr_write_2: reg=0x%x err=%s\n", 376 sc->aue_dev.dv_xname, reg, usbd_errstr(err))); 377 return (-1); 378 } 379 380 return (0); 381 } 382 383 /* 384 * Read a word of data stored in the EEPROM at address 'addr.' 385 */ 386 int 387 aue_eeprom_getword(struct aue_softc *sc, int addr) 388 { 389 int i; 390 391 aue_csr_write_1(sc, AUE_EE_REG, addr); 392 aue_csr_write_1(sc, AUE_EE_CTL, AUE_EECTL_READ); 393 394 for (i = 0; i < AUE_TIMEOUT; i++) { 395 if (aue_csr_read_1(sc, AUE_EE_CTL) & AUE_EECTL_DONE) 396 break; 397 } 398 399 if (i == AUE_TIMEOUT) { 400 printf("%s: EEPROM read timed out\n", 401 sc->aue_dev.dv_xname); 402 } 403 404 return (aue_csr_read_2(sc, AUE_EE_DATA)); 405 } 406 407 /* 408 * Read the MAC from the EEPROM. It's at offset 0. 409 */ 410 void 411 aue_read_mac(struct aue_softc *sc, u_char *dest) 412 { 413 int i; 414 int off = 0; 415 int word; 416 417 DPRINTFN(5,("%s: %s: enter\n", sc->aue_dev.dv_xname, __func__)); 418 419 for (i = 0; i < 3; i++) { 420 word = aue_eeprom_getword(sc, off + i); 421 dest[2 * i] = (u_char)word; 422 dest[2 * i + 1] = (u_char)(word >> 8); 423 } 424 } 425 426 /* Get exclusive access to the MII registers */ 427 void 428 aue_lock_mii(struct aue_softc *sc) 429 { 430 sc->aue_refcnt++; 431 rw_enter_write(&sc->aue_mii_lock); 432 } 433 434 void 435 aue_unlock_mii(struct aue_softc *sc) 436 { 437 rw_exit_write(&sc->aue_mii_lock); 438 if (--sc->aue_refcnt < 0) 439 usb_detach_wakeup(&sc->aue_dev); 440 } 441 442 int 443 aue_miibus_readreg(struct device *dev, int phy, int reg) 444 { 445 struct aue_softc *sc = (void *)dev; 446 int i; 447 u_int16_t val; 448 449 if (sc->aue_dying) { 450 #ifdef DIAGNOSTIC 451 printf("%s: dying\n", sc->aue_dev.dv_xname); 452 #endif 453 return 0; 454 } 455 456 #if 0 457 /* 458 * The Am79C901 HomePNA PHY actually contains 459 * two transceivers: a 1Mbps HomePNA PHY and a 460 * 10Mbps full/half duplex ethernet PHY with 461 * NWAY autoneg. However in the ADMtek adapter, 462 * only the 1Mbps PHY is actually connected to 463 * anything, so we ignore the 10Mbps one. It 464 * happens to be configured for MII address 3, 465 * so we filter that out. 466 */ 467 if (sc->aue_vendor == USB_VENDOR_ADMTEK && 468 sc->aue_product == USB_PRODUCT_ADMTEK_PEGASUS) { 469 if (phy == 3) 470 return (0); 471 } 472 #endif 473 474 aue_lock_mii(sc); 475 aue_csr_write_1(sc, AUE_PHY_ADDR, phy); 476 aue_csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_READ); 477 478 for (i = 0; i < AUE_TIMEOUT; i++) { 479 if (aue_csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE) 480 break; 481 } 482 483 if (i == AUE_TIMEOUT) { 484 printf("%s: MII read timed out\n", sc->aue_dev.dv_xname); 485 } 486 487 val = aue_csr_read_2(sc, AUE_PHY_DATA); 488 489 DPRINTFN(11,("%s: %s: phy=%d reg=%d => 0x%04x\n", 490 sc->aue_dev.dv_xname, __func__, phy, reg, val)); 491 492 aue_unlock_mii(sc); 493 return (val); 494 } 495 496 void 497 aue_miibus_writereg(struct device *dev, int phy, int reg, int data) 498 { 499 struct aue_softc *sc = (void *)dev; 500 int i; 501 502 #if 0 503 if (sc->aue_vendor == USB_VENDOR_ADMTEK && 504 sc->aue_product == USB_PRODUCT_ADMTEK_PEGASUS) { 505 if (phy == 3) 506 return; 507 } 508 #endif 509 510 DPRINTFN(11,("%s: %s: phy=%d reg=%d data=0x%04x\n", 511 sc->aue_dev.dv_xname, __func__, phy, reg, data)); 512 513 aue_lock_mii(sc); 514 aue_csr_write_2(sc, AUE_PHY_DATA, data); 515 aue_csr_write_1(sc, AUE_PHY_ADDR, phy); 516 aue_csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_WRITE); 517 518 for (i = 0; i < AUE_TIMEOUT; i++) { 519 if (aue_csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE) 520 break; 521 } 522 523 if (i == AUE_TIMEOUT) { 524 printf("%s: MII read timed out\n", 525 sc->aue_dev.dv_xname); 526 } 527 aue_unlock_mii(sc); 528 } 529 530 void 531 aue_miibus_statchg(struct device *dev) 532 { 533 struct aue_softc *sc = (void *)dev; 534 struct mii_data *mii = GET_MII(sc); 535 536 DPRINTFN(5,("%s: %s: enter\n", sc->aue_dev.dv_xname, __func__)); 537 538 aue_lock_mii(sc); 539 AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB); 540 541 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX) { 542 AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL); 543 } else { 544 AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL); 545 } 546 547 if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) 548 AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX); 549 else 550 AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX); 551 552 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB); 553 aue_unlock_mii(sc); 554 555 /* 556 * Set the LED modes on the LinkSys adapter. 557 * This turns on the 'dual link LED' bin in the auxmode 558 * register of the Broadcom PHY. 559 */ 560 if (!sc->aue_dying && (sc->aue_flags & LSYS)) { 561 u_int16_t auxmode; 562 auxmode = aue_miibus_readreg(dev, 0, 0x1b); 563 aue_miibus_writereg(dev, 0, 0x1b, auxmode | 0x04); 564 } 565 DPRINTFN(5,("%s: %s: exit\n", sc->aue_dev.dv_xname, __func__)); 566 } 567 568 #define AUE_POLY 0xEDB88320 569 #define AUE_BITS 6 570 571 u_int32_t 572 aue_crc(caddr_t addr) 573 { 574 u_int32_t idx, bit, data, crc; 575 576 /* Compute CRC for the address value. */ 577 crc = 0xFFFFFFFF; /* initial value */ 578 579 for (idx = 0; idx < 6; idx++) { 580 for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1) 581 crc = (crc >> 1) ^ (((crc ^ data) & 1) ? AUE_POLY : 0); 582 } 583 584 return (crc & ((1 << AUE_BITS) - 1)); 585 } 586 587 void 588 aue_setmulti(struct aue_softc *sc) 589 { 590 struct ifnet *ifp; 591 struct ether_multi *enm; 592 struct ether_multistep step; 593 u_int32_t h = 0, i; 594 595 DPRINTFN(5,("%s: %s: enter\n", sc->aue_dev.dv_xname, __func__)); 596 597 ifp = GET_IFP(sc); 598 599 if (ifp->if_flags & IFF_PROMISC) { 600 allmulti: 601 ifp->if_flags |= IFF_ALLMULTI; 602 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI); 603 return; 604 } 605 606 AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI); 607 608 /* first, zot all the existing hash bits */ 609 for (i = 0; i < 8; i++) 610 aue_csr_write_1(sc, AUE_MAR0 + i, 0); 611 612 /* now program new ones */ 613 ETHER_FIRST_MULTI(step, &sc->arpcom, enm); 614 while (enm != NULL) { 615 if (memcmp(enm->enm_addrlo, 616 enm->enm_addrhi, ETHER_ADDR_LEN) != 0) 617 goto allmulti; 618 619 h = aue_crc(enm->enm_addrlo); 620 AUE_SETBIT(sc, AUE_MAR + (h >> 3), 1 << (h & 0x7)); 621 ETHER_NEXT_MULTI(step, enm); 622 } 623 624 ifp->if_flags &= ~IFF_ALLMULTI; 625 } 626 627 void 628 aue_reset_pegasus_II(struct aue_softc *sc) 629 { 630 /* Magic constants taken from Linux driver. */ 631 aue_csr_write_1(sc, AUE_REG_1D, 0); 632 aue_csr_write_1(sc, AUE_REG_7B, 2); 633 #if 0 634 if ((sc->aue_flags & HAS_HOME_PNA) && mii_mode) 635 aue_csr_write_1(sc, AUE_REG_81, 6); 636 else 637 #endif 638 aue_csr_write_1(sc, AUE_REG_81, 2); 639 } 640 641 void 642 aue_reset(struct aue_softc *sc) 643 { 644 int i; 645 646 DPRINTFN(2,("%s: %s: enter\n", sc->aue_dev.dv_xname, __func__)); 647 648 AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_RESETMAC); 649 650 for (i = 0; i < AUE_TIMEOUT; i++) { 651 if (!(aue_csr_read_1(sc, AUE_CTL1) & AUE_CTL1_RESETMAC)) 652 break; 653 } 654 655 if (i == AUE_TIMEOUT) 656 printf("%s: reset failed\n", sc->aue_dev.dv_xname); 657 658 #if 0 659 /* XXX what is mii_mode supposed to be */ 660 if (sc->aue_mii_mode && (sc->aue_flags & PNA)) 661 aue_csr_write_1(sc, AUE_GPIO1, 0x34); 662 else 663 aue_csr_write_1(sc, AUE_GPIO1, 0x26); 664 #endif 665 666 /* 667 * The PHY(s) attached to the Pegasus chip may be held 668 * in reset until we flip on the GPIO outputs. Make sure 669 * to set the GPIO pins high so that the PHY(s) will 670 * be enabled. 671 * 672 * Note: We force all of the GPIO pins low first, *then* 673 * enable the ones we want. 674 */ 675 if (sc->aue_flags & LSYS) { 676 /* Grrr. LinkSys has to be different from everyone else. */ 677 aue_csr_write_1(sc, AUE_GPIO0, 678 AUE_GPIO_SEL0 | AUE_GPIO_SEL1); 679 } else { 680 aue_csr_write_1(sc, AUE_GPIO0, 681 AUE_GPIO_OUT0 | AUE_GPIO_SEL0); 682 } 683 aue_csr_write_1(sc, AUE_GPIO0, 684 AUE_GPIO_OUT0 | AUE_GPIO_SEL0 | AUE_GPIO_SEL1); 685 686 if (sc->aue_flags & PII) 687 aue_reset_pegasus_II(sc); 688 689 /* Wait a little while for the chip to get its brains in order. */ 690 delay(10000); /* XXX */ 691 } 692 693 /* 694 * Probe for a Pegasus chip. 695 */ 696 int 697 aue_match(struct device *parent, void *match, void *aux) 698 { 699 struct usb_attach_arg *uaa = aux; 700 701 if (uaa->iface != NULL) 702 return (UMATCH_NONE); 703 704 return (aue_lookup(uaa->vendor, uaa->product) != NULL ? 705 UMATCH_VENDOR_PRODUCT : UMATCH_NONE); 706 } 707 708 /* 709 * Attach the interface. Allocate softc structures, do ifmedia 710 * setup and ethernet/BPF attach. 711 */ 712 void 713 aue_attach(struct device *parent, struct device *self, void *aux) 714 { 715 struct aue_softc *sc = (struct aue_softc *)self; 716 struct usb_attach_arg *uaa = aux; 717 int s; 718 u_char eaddr[ETHER_ADDR_LEN]; 719 struct ifnet *ifp; 720 struct mii_data *mii; 721 usbd_device_handle dev = uaa->device; 722 usbd_interface_handle iface; 723 usbd_status err; 724 usb_interface_descriptor_t *id; 725 usb_endpoint_descriptor_t *ed; 726 int i; 727 728 DPRINTFN(5,(" : aue_attach: sc=%p", sc)); 729 730 err = usbd_set_config_no(dev, AUE_CONFIG_NO, 1); 731 if (err) { 732 printf("%s: setting config no failed\n", 733 sc->aue_dev.dv_xname); 734 return; 735 } 736 737 usb_init_task(&sc->aue_tick_task, aue_tick_task, sc); 738 usb_init_task(&sc->aue_stop_task, (void (*)(void *))aue_stop, sc); 739 rw_init(&sc->aue_mii_lock, "auemii"); 740 741 err = usbd_device2interface_handle(dev, AUE_IFACE_IDX, &iface); 742 if (err) { 743 printf("%s: getting interface handle failed\n", 744 sc->aue_dev.dv_xname); 745 return; 746 } 747 748 sc->aue_flags = aue_lookup(uaa->vendor, uaa->product)->aue_flags; 749 750 sc->aue_udev = dev; 751 sc->aue_iface = iface; 752 sc->aue_product = uaa->product; 753 sc->aue_vendor = uaa->vendor; 754 755 id = usbd_get_interface_descriptor(iface); 756 757 /* Find endpoints. */ 758 for (i = 0; i < id->bNumEndpoints; i++) { 759 ed = usbd_interface2endpoint_descriptor(iface, i); 760 if (ed == NULL) { 761 printf("%s: couldn't get endpoint descriptor %d\n", 762 sc->aue_dev.dv_xname, i); 763 return; 764 } 765 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 766 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 767 sc->aue_ed[AUE_ENDPT_RX] = ed->bEndpointAddress; 768 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && 769 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 770 sc->aue_ed[AUE_ENDPT_TX] = ed->bEndpointAddress; 771 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 772 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) { 773 sc->aue_ed[AUE_ENDPT_INTR] = ed->bEndpointAddress; 774 } 775 } 776 777 if (sc->aue_ed[AUE_ENDPT_RX] == 0 || sc->aue_ed[AUE_ENDPT_TX] == 0 || 778 sc->aue_ed[AUE_ENDPT_INTR] == 0) { 779 printf("%s: missing endpoint\n", sc->aue_dev.dv_xname); 780 return; 781 } 782 783 784 s = splnet(); 785 786 /* Reset the adapter. */ 787 aue_reset(sc); 788 789 /* 790 * Get station address from the EEPROM. 791 */ 792 aue_read_mac(sc, eaddr); 793 794 /* 795 * A Pegasus chip was detected. Inform the world. 796 */ 797 ifp = GET_IFP(sc); 798 printf("%s: address %s\n", sc->aue_dev.dv_xname, 799 ether_sprintf(eaddr)); 800 801 bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN); 802 803 /* Initialize interface info.*/ 804 ifp->if_softc = sc; 805 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 806 ifp->if_ioctl = aue_ioctl; 807 ifp->if_start = aue_start; 808 ifp->if_watchdog = aue_watchdog; 809 strlcpy(ifp->if_xname, sc->aue_dev.dv_xname, IFNAMSIZ); 810 811 IFQ_SET_READY(&ifp->if_snd); 812 813 ifp->if_capabilities = IFCAP_VLAN_MTU; 814 815 /* Initialize MII/media info. */ 816 mii = &sc->aue_mii; 817 mii->mii_ifp = ifp; 818 mii->mii_readreg = aue_miibus_readreg; 819 mii->mii_writereg = aue_miibus_writereg; 820 mii->mii_statchg = aue_miibus_statchg; 821 mii->mii_flags = MIIF_AUTOTSLEEP; 822 ifmedia_init(&mii->mii_media, 0, aue_ifmedia_upd, aue_ifmedia_sts); 823 mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0); 824 if (LIST_FIRST(&mii->mii_phys) == NULL) { 825 ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL); 826 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE); 827 } else 828 ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO); 829 830 /* Attach the interface. */ 831 if_attach(ifp); 832 ether_ifattach(ifp); 833 834 timeout_set(&sc->aue_stat_ch, aue_tick, sc); 835 836 sc->aue_attached = 1; 837 splx(s); 838 839 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->aue_udev, 840 &sc->aue_dev); 841 } 842 843 int 844 aue_detach(struct device *self, int flags) 845 { 846 struct aue_softc *sc = (struct aue_softc *)self; 847 struct ifnet *ifp = GET_IFP(sc); 848 int s; 849 850 DPRINTFN(2,("%s: %s: enter\n", sc->aue_dev.dv_xname, __func__)); 851 852 if (!sc->aue_attached) { 853 /* Detached before attached finished, so just bail out. */ 854 return (0); 855 } 856 857 timeout_del(&sc->aue_stat_ch); 858 /* 859 * Remove any pending tasks. They cannot be executing because they run 860 * in the same thread as detach. 861 */ 862 usb_rem_task(sc->aue_udev, &sc->aue_tick_task); 863 usb_rem_task(sc->aue_udev, &sc->aue_stop_task); 864 865 s = splusb(); 866 867 if (ifp->if_flags & IFF_RUNNING) 868 aue_stop(sc); 869 870 mii_detach(&sc->aue_mii, MII_PHY_ANY, MII_OFFSET_ANY); 871 ifmedia_delete_instance(&sc->aue_mii.mii_media, IFM_INST_ANY); 872 ether_ifdetach(ifp); 873 if_detach(ifp); 874 875 #ifdef DIAGNOSTIC 876 if (sc->aue_ep[AUE_ENDPT_TX] != NULL || 877 sc->aue_ep[AUE_ENDPT_RX] != NULL || 878 sc->aue_ep[AUE_ENDPT_INTR] != NULL) 879 printf("%s: detach has active endpoints\n", 880 sc->aue_dev.dv_xname); 881 #endif 882 883 sc->aue_attached = 0; 884 885 if (--sc->aue_refcnt >= 0) { 886 /* Wait for processes to go away. */ 887 usb_detach_wait(&sc->aue_dev); 888 } 889 splx(s); 890 891 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->aue_udev, 892 &sc->aue_dev); 893 894 return (0); 895 } 896 897 int 898 aue_activate(struct device *self, enum devact act) 899 { 900 struct aue_softc *sc = (struct aue_softc *)self; 901 902 DPRINTFN(2,("%s: %s: enter\n", sc->aue_dev.dv_xname, __func__)); 903 904 switch (act) { 905 case DVACT_ACTIVATE: 906 break; 907 908 case DVACT_DEACTIVATE: 909 sc->aue_dying = 1; 910 break; 911 } 912 return (0); 913 } 914 915 /* 916 * Initialize an RX descriptor and attach an MBUF cluster. 917 */ 918 int 919 aue_newbuf(struct aue_softc *sc, struct aue_chain *c, struct mbuf *m) 920 { 921 struct mbuf *m_new = NULL; 922 923 DPRINTFN(10,("%s: %s: enter\n", sc->aue_dev.dv_xname,__func__)); 924 925 if (m == NULL) { 926 MGETHDR(m_new, M_DONTWAIT, MT_DATA); 927 if (m_new == NULL) { 928 printf("%s: no memory for rx list " 929 "-- packet dropped!\n", sc->aue_dev.dv_xname); 930 return (ENOBUFS); 931 } 932 933 MCLGET(m_new, M_DONTWAIT); 934 if (!(m_new->m_flags & M_EXT)) { 935 printf("%s: no memory for rx list " 936 "-- packet dropped!\n", sc->aue_dev.dv_xname); 937 m_freem(m_new); 938 return (ENOBUFS); 939 } 940 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; 941 } else { 942 m_new = m; 943 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; 944 m_new->m_data = m_new->m_ext.ext_buf; 945 } 946 947 m_adj(m_new, ETHER_ALIGN); 948 c->aue_mbuf = m_new; 949 950 return (0); 951 } 952 953 int 954 aue_rx_list_init(struct aue_softc *sc) 955 { 956 struct aue_cdata *cd; 957 struct aue_chain *c; 958 int i; 959 960 DPRINTFN(5,("%s: %s: enter\n", sc->aue_dev.dv_xname, __func__)); 961 962 cd = &sc->aue_cdata; 963 for (i = 0; i < AUE_RX_LIST_CNT; i++) { 964 c = &cd->aue_rx_chain[i]; 965 c->aue_sc = sc; 966 c->aue_idx = i; 967 if (aue_newbuf(sc, c, NULL) == ENOBUFS) 968 return (ENOBUFS); 969 if (c->aue_xfer == NULL) { 970 c->aue_xfer = usbd_alloc_xfer(sc->aue_udev); 971 if (c->aue_xfer == NULL) 972 return (ENOBUFS); 973 c->aue_buf = usbd_alloc_buffer(c->aue_xfer, AUE_BUFSZ); 974 if (c->aue_buf == NULL) 975 return (ENOBUFS); /* XXX free xfer */ 976 } 977 } 978 979 return (0); 980 } 981 982 int 983 aue_tx_list_init(struct aue_softc *sc) 984 { 985 struct aue_cdata *cd; 986 struct aue_chain *c; 987 int i; 988 989 DPRINTFN(5,("%s: %s: enter\n", sc->aue_dev.dv_xname, __func__)); 990 991 cd = &sc->aue_cdata; 992 for (i = 0; i < AUE_TX_LIST_CNT; i++) { 993 c = &cd->aue_tx_chain[i]; 994 c->aue_sc = sc; 995 c->aue_idx = i; 996 c->aue_mbuf = NULL; 997 if (c->aue_xfer == NULL) { 998 c->aue_xfer = usbd_alloc_xfer(sc->aue_udev); 999 if (c->aue_xfer == NULL) 1000 return (ENOBUFS); 1001 c->aue_buf = usbd_alloc_buffer(c->aue_xfer, AUE_BUFSZ); 1002 if (c->aue_buf == NULL) 1003 return (ENOBUFS); 1004 } 1005 } 1006 1007 return (0); 1008 } 1009 1010 void 1011 aue_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status) 1012 { 1013 struct aue_softc *sc = priv; 1014 struct ifnet *ifp = GET_IFP(sc); 1015 struct aue_intrpkt *p = &sc->aue_cdata.aue_ibuf; 1016 1017 DPRINTFN(15,("%s: %s: enter\n", sc->aue_dev.dv_xname,__func__)); 1018 1019 if (sc->aue_dying) 1020 return; 1021 1022 if (!(ifp->if_flags & IFF_RUNNING)) 1023 return; 1024 1025 if (status != USBD_NORMAL_COMPLETION) { 1026 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) { 1027 return; 1028 } 1029 sc->aue_intr_errs++; 1030 if (usbd_ratecheck(&sc->aue_rx_notice)) { 1031 printf("%s: %u usb errors on intr: %s\n", 1032 sc->aue_dev.dv_xname, sc->aue_intr_errs, 1033 usbd_errstr(status)); 1034 sc->aue_intr_errs = 0; 1035 } 1036 if (status == USBD_STALLED) 1037 usbd_clear_endpoint_stall_async(sc->aue_ep[AUE_ENDPT_RX]); 1038 return; 1039 } 1040 1041 if (p->aue_txstat0) 1042 ifp->if_oerrors++; 1043 1044 if (p->aue_txstat0 & (AUE_TXSTAT0_LATECOLL | AUE_TXSTAT0_EXCESSCOLL)) 1045 ifp->if_collisions++; 1046 } 1047 1048 /* 1049 * A frame has been uploaded: pass the resulting mbuf chain up to 1050 * the higher level protocols. 1051 */ 1052 void 1053 aue_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status) 1054 { 1055 struct aue_chain *c = priv; 1056 struct aue_softc *sc = c->aue_sc; 1057 struct ifnet *ifp = GET_IFP(sc); 1058 struct mbuf *m; 1059 u_int32_t total_len; 1060 struct aue_rxpkt r; 1061 int s; 1062 1063 DPRINTFN(10,("%s: %s: enter\n", sc->aue_dev.dv_xname,__func__)); 1064 1065 if (sc->aue_dying) 1066 return; 1067 1068 if (!(ifp->if_flags & IFF_RUNNING)) 1069 return; 1070 1071 if (status != USBD_NORMAL_COMPLETION) { 1072 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) 1073 return; 1074 sc->aue_rx_errs++; 1075 if (usbd_ratecheck(&sc->aue_rx_notice)) { 1076 printf("%s: %u usb errors on rx: %s\n", 1077 sc->aue_dev.dv_xname, sc->aue_rx_errs, 1078 usbd_errstr(status)); 1079 sc->aue_rx_errs = 0; 1080 } 1081 if (status == USBD_STALLED) 1082 usbd_clear_endpoint_stall_async(sc->aue_ep[AUE_ENDPT_RX]); 1083 goto done; 1084 } 1085 1086 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL); 1087 1088 memcpy(mtod(c->aue_mbuf, char *), c->aue_buf, total_len); 1089 1090 if (total_len <= 4 + ETHER_CRC_LEN) { 1091 ifp->if_ierrors++; 1092 goto done; 1093 } 1094 1095 memcpy(&r, c->aue_buf + total_len - 4, sizeof(r)); 1096 1097 /* Turn off all the non-error bits in the rx status word. */ 1098 r.aue_rxstat &= AUE_RXSTAT_MASK; 1099 if (r.aue_rxstat) { 1100 ifp->if_ierrors++; 1101 goto done; 1102 } 1103 1104 /* No errors; receive the packet. */ 1105 m = c->aue_mbuf; 1106 total_len -= ETHER_CRC_LEN + 4; 1107 m->m_pkthdr.len = m->m_len = total_len; 1108 ifp->if_ipackets++; 1109 1110 m->m_pkthdr.rcvif = ifp; 1111 1112 s = splnet(); 1113 1114 /* XXX ugly */ 1115 if (aue_newbuf(sc, c, NULL) == ENOBUFS) { 1116 ifp->if_ierrors++; 1117 goto done1; 1118 } 1119 1120 #if NBPFILTER > 0 1121 /* 1122 * Handle BPF listeners. Let the BPF user see the packet, but 1123 * don't pass it up to the ether_input() layer unless it's 1124 * a broadcast packet, multicast packet, matches our ethernet 1125 * address or the interface is in promiscuous mode. 1126 */ 1127 if (ifp->if_bpf) 1128 bpf_mtap(ifp->if_bpf, m, BPF_DIRECTION_IN); 1129 #endif 1130 1131 DPRINTFN(10,("%s: %s: deliver %d\n", sc->aue_dev.dv_xname, 1132 __func__, m->m_len)); 1133 ether_input_mbuf(ifp, m); 1134 done1: 1135 splx(s); 1136 1137 done: 1138 1139 /* Setup new transfer. */ 1140 usbd_setup_xfer(xfer, sc->aue_ep[AUE_ENDPT_RX], 1141 c, c->aue_buf, AUE_BUFSZ, 1142 USBD_SHORT_XFER_OK | USBD_NO_COPY, 1143 USBD_NO_TIMEOUT, aue_rxeof); 1144 usbd_transfer(xfer); 1145 1146 DPRINTFN(10,("%s: %s: start rx\n", sc->aue_dev.dv_xname, 1147 __func__)); 1148 } 1149 1150 /* 1151 * A frame was downloaded to the chip. It's safe for us to clean up 1152 * the list buffers. 1153 */ 1154 1155 void 1156 aue_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status) 1157 { 1158 struct aue_chain *c = priv; 1159 struct aue_softc *sc = c->aue_sc; 1160 struct ifnet *ifp = GET_IFP(sc); 1161 int s; 1162 1163 if (sc->aue_dying) 1164 return; 1165 1166 s = splnet(); 1167 1168 DPRINTFN(10,("%s: %s: enter status=%d\n", sc->aue_dev.dv_xname, 1169 __func__, status)); 1170 1171 ifp->if_timer = 0; 1172 ifp->if_flags &= ~IFF_OACTIVE; 1173 1174 if (status != USBD_NORMAL_COMPLETION) { 1175 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) { 1176 splx(s); 1177 return; 1178 } 1179 ifp->if_oerrors++; 1180 printf("%s: usb error on tx: %s\n", sc->aue_dev.dv_xname, 1181 usbd_errstr(status)); 1182 if (status == USBD_STALLED) 1183 usbd_clear_endpoint_stall_async(sc->aue_ep[AUE_ENDPT_TX]); 1184 splx(s); 1185 return; 1186 } 1187 1188 ifp->if_opackets++; 1189 1190 m_freem(c->aue_mbuf); 1191 c->aue_mbuf = NULL; 1192 1193 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) 1194 aue_start(ifp); 1195 1196 splx(s); 1197 } 1198 1199 void 1200 aue_tick(void *xsc) 1201 { 1202 struct aue_softc *sc = xsc; 1203 1204 DPRINTFN(15,("%s: %s: enter\n", sc->aue_dev.dv_xname,__func__)); 1205 1206 if (sc == NULL) 1207 return; 1208 1209 if (sc->aue_dying) 1210 return; 1211 1212 /* Perform periodic stuff in process context. */ 1213 usb_add_task(sc->aue_udev, &sc->aue_tick_task); 1214 } 1215 1216 void 1217 aue_tick_task(void *xsc) 1218 { 1219 struct aue_softc *sc = xsc; 1220 struct ifnet *ifp; 1221 struct mii_data *mii; 1222 int s; 1223 1224 DPRINTFN(15,("%s: %s: enter\n", sc->aue_dev.dv_xname,__func__)); 1225 1226 if (sc->aue_dying) 1227 return; 1228 1229 ifp = GET_IFP(sc); 1230 mii = GET_MII(sc); 1231 if (mii == NULL) 1232 return; 1233 1234 s = splnet(); 1235 1236 mii_tick(mii); 1237 if (!sc->aue_link && mii->mii_media_status & IFM_ACTIVE && 1238 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) { 1239 DPRINTFN(2,("%s: %s: got link\n", 1240 sc->aue_dev.dv_xname,__func__)); 1241 sc->aue_link++; 1242 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) 1243 aue_start(ifp); 1244 } 1245 1246 timeout_add_sec(&sc->aue_stat_ch, 1); 1247 1248 splx(s); 1249 } 1250 1251 int 1252 aue_send(struct aue_softc *sc, struct mbuf *m, int idx) 1253 { 1254 int total_len; 1255 struct aue_chain *c; 1256 usbd_status err; 1257 1258 DPRINTFN(10,("%s: %s: enter\n", sc->aue_dev.dv_xname,__func__)); 1259 1260 c = &sc->aue_cdata.aue_tx_chain[idx]; 1261 1262 /* 1263 * Copy the mbuf data into a contiguous buffer, leaving two 1264 * bytes at the beginning to hold the frame length. 1265 */ 1266 m_copydata(m, 0, m->m_pkthdr.len, c->aue_buf + 2); 1267 c->aue_mbuf = m; 1268 1269 /* 1270 * The ADMtek documentation says that the packet length is 1271 * supposed to be specified in the first two bytes of the 1272 * transfer, however it actually seems to ignore this info 1273 * and base the frame size on the bulk transfer length. 1274 */ 1275 c->aue_buf[0] = (u_int8_t)m->m_pkthdr.len; 1276 c->aue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8); 1277 total_len = m->m_pkthdr.len + 2; 1278 1279 usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_TX], 1280 c, c->aue_buf, total_len, USBD_FORCE_SHORT_XFER | USBD_NO_COPY, 1281 AUE_TX_TIMEOUT, aue_txeof); 1282 1283 /* Transmit */ 1284 err = usbd_transfer(c->aue_xfer); 1285 if (err != USBD_IN_PROGRESS) { 1286 printf("%s: aue_send error=%s\n", sc->aue_dev.dv_xname, 1287 usbd_errstr(err)); 1288 /* Stop the interface from process context. */ 1289 usb_add_task(sc->aue_udev, &sc->aue_stop_task); 1290 return (EIO); 1291 } 1292 DPRINTFN(5,("%s: %s: send %d bytes\n", sc->aue_dev.dv_xname, 1293 __func__, total_len)); 1294 1295 sc->aue_cdata.aue_tx_cnt++; 1296 1297 return (0); 1298 } 1299 1300 void 1301 aue_start(struct ifnet *ifp) 1302 { 1303 struct aue_softc *sc = ifp->if_softc; 1304 struct mbuf *m_head = NULL; 1305 1306 DPRINTFN(5,("%s: %s: enter, link=%d\n", sc->aue_dev.dv_xname, 1307 __func__, sc->aue_link)); 1308 1309 if (sc->aue_dying) 1310 return; 1311 1312 if (!sc->aue_link) 1313 return; 1314 1315 if (ifp->if_flags & IFF_OACTIVE) 1316 return; 1317 1318 IFQ_POLL(&ifp->if_snd, m_head); 1319 if (m_head == NULL) 1320 return; 1321 1322 if (aue_send(sc, m_head, 0)) { 1323 ifp->if_flags |= IFF_OACTIVE; 1324 return; 1325 } 1326 1327 IFQ_DEQUEUE(&ifp->if_snd, m_head); 1328 1329 #if NBPFILTER > 0 1330 /* 1331 * If there's a BPF listener, bounce a copy of this frame 1332 * to him. 1333 */ 1334 if (ifp->if_bpf) 1335 bpf_mtap(ifp->if_bpf, m_head, BPF_DIRECTION_OUT); 1336 #endif 1337 1338 ifp->if_flags |= IFF_OACTIVE; 1339 1340 /* 1341 * Set a timeout in case the chip goes out to lunch. 1342 */ 1343 ifp->if_timer = 5; 1344 } 1345 1346 void 1347 aue_init(void *xsc) 1348 { 1349 struct aue_softc *sc = xsc; 1350 struct ifnet *ifp = GET_IFP(sc); 1351 struct mii_data *mii = GET_MII(sc); 1352 int i, s; 1353 u_char *eaddr; 1354 1355 DPRINTFN(5,("%s: %s: enter\n", sc->aue_dev.dv_xname, __func__)); 1356 1357 if (sc->aue_dying) 1358 return; 1359 1360 if (ifp->if_flags & IFF_RUNNING) 1361 return; 1362 1363 s = splnet(); 1364 1365 /* 1366 * Cancel pending I/O and free all RX/TX buffers. 1367 */ 1368 aue_reset(sc); 1369 1370 eaddr = sc->arpcom.ac_enaddr; 1371 for (i = 0; i < ETHER_ADDR_LEN; i++) 1372 aue_csr_write_1(sc, AUE_PAR0 + i, eaddr[i]); 1373 1374 /* If we want promiscuous mode, set the allframes bit. */ 1375 if (ifp->if_flags & IFF_PROMISC) 1376 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC); 1377 else 1378 AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC); 1379 1380 /* Init TX ring. */ 1381 if (aue_tx_list_init(sc) == ENOBUFS) { 1382 printf("%s: tx list init failed\n", sc->aue_dev.dv_xname); 1383 splx(s); 1384 return; 1385 } 1386 1387 /* Init RX ring. */ 1388 if (aue_rx_list_init(sc) == ENOBUFS) { 1389 printf("%s: rx list init failed\n", sc->aue_dev.dv_xname); 1390 splx(s); 1391 return; 1392 } 1393 1394 /* Load the multicast filter. */ 1395 aue_setmulti(sc); 1396 1397 /* Enable RX and TX */ 1398 aue_csr_write_1(sc, AUE_CTL0, AUE_CTL0_RXSTAT_APPEND | AUE_CTL0_RX_ENB); 1399 AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_TX_ENB); 1400 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_EP3_CLR); 1401 1402 mii_mediachg(mii); 1403 1404 if (sc->aue_ep[AUE_ENDPT_RX] == NULL) { 1405 if (aue_openpipes(sc)) { 1406 splx(s); 1407 return; 1408 } 1409 } 1410 1411 ifp->if_flags |= IFF_RUNNING; 1412 ifp->if_flags &= ~IFF_OACTIVE; 1413 1414 splx(s); 1415 1416 timeout_add_sec(&sc->aue_stat_ch, 1); 1417 } 1418 1419 int 1420 aue_openpipes(struct aue_softc *sc) 1421 { 1422 struct aue_chain *c; 1423 usbd_status err; 1424 int i; 1425 1426 /* Open RX and TX pipes. */ 1427 err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_RX], 1428 USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_RX]); 1429 if (err) { 1430 printf("%s: open rx pipe failed: %s\n", 1431 sc->aue_dev.dv_xname, usbd_errstr(err)); 1432 return (EIO); 1433 } 1434 err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_TX], 1435 USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_TX]); 1436 if (err) { 1437 printf("%s: open tx pipe failed: %s\n", 1438 sc->aue_dev.dv_xname, usbd_errstr(err)); 1439 return (EIO); 1440 } 1441 err = usbd_open_pipe_intr(sc->aue_iface, sc->aue_ed[AUE_ENDPT_INTR], 1442 USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_INTR], sc, 1443 &sc->aue_cdata.aue_ibuf, AUE_INTR_PKTLEN, aue_intr, 1444 AUE_INTR_INTERVAL); 1445 if (err) { 1446 printf("%s: open intr pipe failed: %s\n", 1447 sc->aue_dev.dv_xname, usbd_errstr(err)); 1448 return (EIO); 1449 } 1450 1451 /* Start up the receive pipe. */ 1452 for (i = 0; i < AUE_RX_LIST_CNT; i++) { 1453 c = &sc->aue_cdata.aue_rx_chain[i]; 1454 usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_RX], 1455 c, c->aue_buf, AUE_BUFSZ, 1456 USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT, 1457 aue_rxeof); 1458 (void)usbd_transfer(c->aue_xfer); /* XXX */ 1459 DPRINTFN(5,("%s: %s: start read\n", sc->aue_dev.dv_xname, 1460 __func__)); 1461 1462 } 1463 return (0); 1464 } 1465 1466 /* 1467 * Set media options. 1468 */ 1469 int 1470 aue_ifmedia_upd(struct ifnet *ifp) 1471 { 1472 struct aue_softc *sc = ifp->if_softc; 1473 struct mii_data *mii = GET_MII(sc); 1474 1475 DPRINTFN(5,("%s: %s: enter\n", sc->aue_dev.dv_xname, __func__)); 1476 1477 if (sc->aue_dying) 1478 return (0); 1479 1480 sc->aue_link = 0; 1481 if (mii->mii_instance) { 1482 struct mii_softc *miisc; 1483 for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL; 1484 miisc = LIST_NEXT(miisc, mii_list)) 1485 mii_phy_reset(miisc); 1486 } 1487 mii_mediachg(mii); 1488 1489 return (0); 1490 } 1491 1492 /* 1493 * Report current media status. 1494 */ 1495 void 1496 aue_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr) 1497 { 1498 struct aue_softc *sc = ifp->if_softc; 1499 struct mii_data *mii = GET_MII(sc); 1500 1501 DPRINTFN(5,("%s: %s: enter\n", sc->aue_dev.dv_xname, __func__)); 1502 1503 mii_pollstat(mii); 1504 ifmr->ifm_active = mii->mii_media_active; 1505 ifmr->ifm_status = mii->mii_media_status; 1506 } 1507 1508 int 1509 aue_ioctl(struct ifnet *ifp, u_long command, caddr_t data) 1510 { 1511 struct aue_softc *sc = ifp->if_softc; 1512 struct ifaddr *ifa = (struct ifaddr *)data; 1513 struct ifreq *ifr = (struct ifreq *)data; 1514 struct mii_data *mii; 1515 int s, error = 0; 1516 1517 if (sc->aue_dying) 1518 return (EIO); 1519 1520 s = splnet(); 1521 1522 switch(command) { 1523 case SIOCSIFADDR: 1524 ifp->if_flags |= IFF_UP; 1525 aue_init(sc); 1526 1527 switch (ifa->ifa_addr->sa_family) { 1528 #ifdef INET 1529 case AF_INET: 1530 arp_ifinit(&sc->arpcom, ifa); 1531 break; 1532 #endif /* INET */ 1533 } 1534 break; 1535 1536 case SIOCSIFFLAGS: 1537 if (ifp->if_flags & IFF_UP) { 1538 if (ifp->if_flags & IFF_RUNNING && 1539 ifp->if_flags & IFF_PROMISC && 1540 !(sc->aue_if_flags & IFF_PROMISC)) { 1541 AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC); 1542 } else if (ifp->if_flags & IFF_RUNNING && 1543 !(ifp->if_flags & IFF_PROMISC) && 1544 sc->aue_if_flags & IFF_PROMISC) { 1545 AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC); 1546 } else if (!(ifp->if_flags & IFF_RUNNING)) 1547 aue_init(sc); 1548 } else { 1549 if (ifp->if_flags & IFF_RUNNING) 1550 aue_stop(sc); 1551 } 1552 sc->aue_if_flags = ifp->if_flags; 1553 error = 0; 1554 break; 1555 1556 case SIOCGIFMEDIA: 1557 case SIOCSIFMEDIA: 1558 mii = GET_MII(sc); 1559 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command); 1560 break; 1561 1562 default: 1563 error = ether_ioctl(ifp, &sc->arpcom, command, data); 1564 } 1565 1566 if (error == ENETRESET) { 1567 if (ifp->if_flags & IFF_RUNNING) 1568 aue_setmulti(sc); 1569 error = 0; 1570 } 1571 1572 splx(s); 1573 return (error); 1574 } 1575 1576 void 1577 aue_watchdog(struct ifnet *ifp) 1578 { 1579 struct aue_softc *sc = ifp->if_softc; 1580 struct aue_chain *c; 1581 usbd_status stat; 1582 int s; 1583 1584 DPRINTFN(5,("%s: %s: enter\n", sc->aue_dev.dv_xname, __func__)); 1585 1586 ifp->if_oerrors++; 1587 printf("%s: watchdog timeout\n", sc->aue_dev.dv_xname); 1588 1589 s = splusb(); 1590 c = &sc->aue_cdata.aue_tx_chain[0]; 1591 usbd_get_xfer_status(c->aue_xfer, NULL, NULL, NULL, &stat); 1592 aue_txeof(c->aue_xfer, c, stat); 1593 1594 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) 1595 aue_start(ifp); 1596 splx(s); 1597 } 1598 1599 /* 1600 * Stop the adapter and free any mbufs allocated to the 1601 * RX and TX lists. 1602 */ 1603 void 1604 aue_stop(struct aue_softc *sc) 1605 { 1606 usbd_status err; 1607 struct ifnet *ifp; 1608 int i; 1609 1610 DPRINTFN(5,("%s: %s: enter\n", sc->aue_dev.dv_xname, __func__)); 1611 1612 ifp = GET_IFP(sc); 1613 ifp->if_timer = 0; 1614 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); 1615 1616 aue_csr_write_1(sc, AUE_CTL0, 0); 1617 aue_csr_write_1(sc, AUE_CTL1, 0); 1618 aue_reset(sc); 1619 timeout_del(&sc->aue_stat_ch); 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 sc->aue_dev.dv_xname, 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 sc->aue_dev.dv_xname, 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 sc->aue_dev.dv_xname, 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 sc->aue_dev.dv_xname, 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 sc->aue_dev.dv_xname, 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 sc->aue_dev.dv_xname, 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