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