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