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