1184610Salfred/*- 2184610Salfred * Copyright (c) 2001-2003, Shunsuke Akiyama <akiyama@FreeBSD.org>. 3184610Salfred * Copyright (c) 1997, 1998, 1999, 2000 Bill Paul <wpaul@ee.columbia.edu>. 4184610Salfred * All rights reserved. 5184610Salfred * 6184610Salfred * Redistribution and use in source and binary forms, with or without 7184610Salfred * modification, are permitted provided that the following conditions 8184610Salfred * are met: 9184610Salfred * 1. Redistributions of source code must retain the above copyright 10184610Salfred * notice, this list of conditions and the following disclaimer. 11184610Salfred * 2. Redistributions in binary form must reproduce the above copyright 12184610Salfred * notice, this list of conditions and the following disclaimer in the 13184610Salfred * documentation and/or other materials provided with the distribution. 14184610Salfred * 15184610Salfred * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16184610Salfred * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17184610Salfred * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18184610Salfred * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19184610Salfred * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20184610Salfred * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21184610Salfred * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22184610Salfred * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23184610Salfred * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24184610Salfred * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25184610Salfred * SUCH DAMAGE. 26184610Salfred */ 27184610Salfred/*- 28184610Salfred * Copyright (c) 1997, 1998, 1999, 2000 29184610Salfred * Bill Paul <wpaul@ee.columbia.edu>. All rights reserved. 30184610Salfred * 31184610Salfred * Redistribution and use in source and binary forms, with or without 32184610Salfred * modification, are permitted provided that the following conditions 33184610Salfred * are met: 34184610Salfred * 1. Redistributions of source code must retain the above copyright 35184610Salfred * notice, this list of conditions and the following disclaimer. 36184610Salfred * 2. Redistributions in binary form must reproduce the above copyright 37184610Salfred * notice, this list of conditions and the following disclaimer in the 38184610Salfred * documentation and/or other materials provided with the distribution. 39184610Salfred * 3. All advertising materials mentioning features or use of this software 40184610Salfred * must display the following acknowledgement: 41184610Salfred * This product includes software developed by Bill Paul. 42184610Salfred * 4. Neither the name of the author nor the names of any co-contributors 43184610Salfred * may be used to endorse or promote products derived from this software 44184610Salfred * without specific prior written permission. 45184610Salfred * 46184610Salfred * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 47184610Salfred * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 48184610Salfred * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 49184610Salfred * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD 50184610Salfred * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 51184610Salfred * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 52184610Salfred * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 53184610Salfred * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 54184610Salfred * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 55184610Salfred * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 56184610Salfred * THE POSSIBILITY OF SUCH DAMAGE. 57184610Salfred */ 58184610Salfred 59184610Salfred#include <sys/cdefs.h> 60184610Salfred__FBSDID("$FreeBSD$"); 61184610Salfred 62184610Salfred/* 63184610Salfred * RealTek RTL8150 USB to fast ethernet controller driver. 64184610Salfred * Datasheet is available from 65184610Salfred * ftp://ftp.realtek.com.tw/lancard/data_sheet/8150/. 66184610Salfred */ 67184610Salfred 68194677Sthompsa#include <sys/stdint.h> 69194677Sthompsa#include <sys/stddef.h> 70194677Sthompsa#include <sys/param.h> 71194677Sthompsa#include <sys/queue.h> 72194677Sthompsa#include <sys/types.h> 73194677Sthompsa#include <sys/systm.h> 74257176Sglebius#include <sys/socket.h> 75194677Sthompsa#include <sys/kernel.h> 76194677Sthompsa#include <sys/bus.h> 77194677Sthompsa#include <sys/module.h> 78194677Sthompsa#include <sys/lock.h> 79194677Sthompsa#include <sys/mutex.h> 80194677Sthompsa#include <sys/condvar.h> 81194677Sthompsa#include <sys/sysctl.h> 82194677Sthompsa#include <sys/sx.h> 83194677Sthompsa#include <sys/unistd.h> 84194677Sthompsa#include <sys/callout.h> 85194677Sthompsa#include <sys/malloc.h> 86194677Sthompsa#include <sys/priv.h> 87194677Sthompsa 88257176Sglebius#include <net/if.h> 89257176Sglebius#include <net/if_var.h> 90257176Sglebius 91194677Sthompsa#include <dev/usb/usb.h> 92194677Sthompsa#include <dev/usb/usbdi.h> 93194677Sthompsa#include <dev/usb/usbdi_util.h> 94188746Sthompsa#include "usbdevs.h" 95184610Salfred 96184610Salfred#define USB_DEBUG_VAR rue_debug 97194677Sthompsa#include <dev/usb/usb_debug.h> 98188942Sthompsa#include <dev/usb/usb_process.h> 99184610Salfred 100188942Sthompsa#include <dev/usb/net/usb_ethernet.h> 101188942Sthompsa#include <dev/usb/net/if_ruereg.h> 102184610Salfred 103207077Sthompsa#ifdef USB_DEBUG 104184610Salfredstatic int rue_debug = 0; 105184610Salfred 106227309Sedstatic SYSCTL_NODE(_hw_usb, OID_AUTO, rue, CTLFLAG_RW, 0, "USB rue"); 107276701ShselaskySYSCTL_INT(_hw_usb_rue, OID_AUTO, debug, CTLFLAG_RWTUN, 108184610Salfred &rue_debug, 0, "Debug level"); 109184610Salfred#endif 110184610Salfred 111184610Salfred/* 112184610Salfred * Various supported device vendors/products. 113184610Salfred */ 114184610Salfred 115223486Shselaskystatic const STRUCT_USB_HOST_ID rue_devs[] = { 116184610Salfred {USB_VPI(USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUAKTX, 0)}, 117184610Salfred {USB_VPI(USB_VENDOR_REALTEK, USB_PRODUCT_REALTEK_USBKR100, 0)}, 118209444Sthompsa {USB_VPI(USB_VENDOR_OQO, USB_PRODUCT_OQO_ETHER01, 0)}, 119184610Salfred}; 120184610Salfred 121184610Salfred/* prototypes */ 122184610Salfred 123184610Salfredstatic device_probe_t rue_probe; 124184610Salfredstatic device_attach_t rue_attach; 125184610Salfredstatic device_detach_t rue_detach; 126184610Salfred 127188412Sthompsastatic miibus_readreg_t rue_miibus_readreg; 128188412Sthompsastatic miibus_writereg_t rue_miibus_writereg; 129188412Sthompsastatic miibus_statchg_t rue_miibus_statchg; 130188412Sthompsa 131193045Sthompsastatic usb_callback_t rue_intr_callback; 132193045Sthompsastatic usb_callback_t rue_bulk_read_callback; 133193045Sthompsastatic usb_callback_t rue_bulk_write_callback; 134184610Salfred 135193045Sthompsastatic uether_fn_t rue_attach_post; 136193045Sthompsastatic uether_fn_t rue_init; 137193045Sthompsastatic uether_fn_t rue_stop; 138193045Sthompsastatic uether_fn_t rue_start; 139193045Sthompsastatic uether_fn_t rue_tick; 140193045Sthompsastatic uether_fn_t rue_setmulti; 141193045Sthompsastatic uether_fn_t rue_setpromisc; 142184610Salfred 143188412Sthompsastatic int rue_read_mem(struct rue_softc *, uint16_t, void *, int); 144188412Sthompsastatic int rue_write_mem(struct rue_softc *, uint16_t, void *, int); 145188412Sthompsastatic uint8_t rue_csr_read_1(struct rue_softc *, uint16_t); 146188412Sthompsastatic uint16_t rue_csr_read_2(struct rue_softc *, uint16_t); 147188412Sthompsastatic int rue_csr_write_1(struct rue_softc *, uint16_t, uint8_t); 148188412Sthompsastatic int rue_csr_write_2(struct rue_softc *, uint16_t, uint16_t); 149188412Sthompsastatic int rue_csr_write_4(struct rue_softc *, int, uint32_t); 150184610Salfred 151188412Sthompsastatic void rue_reset(struct rue_softc *); 152188412Sthompsastatic int rue_ifmedia_upd(struct ifnet *); 153188412Sthompsastatic void rue_ifmedia_sts(struct ifnet *, struct ifmediareq *); 154184610Salfred 155192984Sthompsastatic const struct usb_config rue_config[RUE_N_TRANSFER] = { 156184610Salfred 157187259Sthompsa [RUE_BULK_DT_WR] = { 158184610Salfred .type = UE_BULK, 159184610Salfred .endpoint = UE_ADDR_ANY, 160184610Salfred .direction = UE_DIR_OUT, 161190734Sthompsa .bufsize = MCLBYTES, 162190734Sthompsa .flags = {.pipe_bof = 1,.force_short_xfer = 1,}, 163190734Sthompsa .callback = rue_bulk_write_callback, 164190734Sthompsa .timeout = 10000, /* 10 seconds */ 165184610Salfred }, 166184610Salfred 167187259Sthompsa [RUE_BULK_DT_RD] = { 168184610Salfred .type = UE_BULK, 169184610Salfred .endpoint = UE_ADDR_ANY, 170184610Salfred .direction = UE_DIR_IN, 171190734Sthompsa .bufsize = (MCLBYTES + 4), 172190734Sthompsa .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 173190734Sthompsa .callback = rue_bulk_read_callback, 174190734Sthompsa .timeout = 0, /* no timeout */ 175184610Salfred }, 176184610Salfred 177187259Sthompsa [RUE_INTR_DT_RD] = { 178184610Salfred .type = UE_INTERRUPT, 179184610Salfred .endpoint = UE_ADDR_ANY, 180184610Salfred .direction = UE_DIR_IN, 181190734Sthompsa .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 182190734Sthompsa .bufsize = 0, /* use wMaxPacketSize */ 183190734Sthompsa .callback = rue_intr_callback, 184184610Salfred }, 185184610Salfred}; 186184610Salfred 187184610Salfredstatic device_method_t rue_methods[] = { 188184610Salfred /* Device interface */ 189184610Salfred DEVMETHOD(device_probe, rue_probe), 190184610Salfred DEVMETHOD(device_attach, rue_attach), 191184610Salfred DEVMETHOD(device_detach, rue_detach), 192184610Salfred 193184610Salfred /* MII interface */ 194188412Sthompsa DEVMETHOD(miibus_readreg, rue_miibus_readreg), 195188412Sthompsa DEVMETHOD(miibus_writereg, rue_miibus_writereg), 196188412Sthompsa DEVMETHOD(miibus_statchg, rue_miibus_statchg), 197184610Salfred 198227843Smarius DEVMETHOD_END 199184610Salfred}; 200184610Salfred 201184610Salfredstatic driver_t rue_driver = { 202184610Salfred .name = "rue", 203184610Salfred .methods = rue_methods, 204184610Salfred .size = sizeof(struct rue_softc), 205184610Salfred}; 206184610Salfred 207184610Salfredstatic devclass_t rue_devclass; 208184610Salfred 209235255SmariusDRIVER_MODULE_ORDERED(rue, uhub, rue_driver, rue_devclass, NULL, NULL, 210235255Smarius SI_ORDER_ANY); 211235255SmariusDRIVER_MODULE(miibus, rue, miibus_driver, miibus_devclass, NULL, NULL); 212188942SthompsaMODULE_DEPEND(rue, uether, 1, 1, 1); 213188942SthompsaMODULE_DEPEND(rue, usb, 1, 1, 1); 214184610SalfredMODULE_DEPEND(rue, ether, 1, 1, 1); 215184610SalfredMODULE_DEPEND(rue, miibus, 1, 1, 1); 216212122SthompsaMODULE_VERSION(rue, 1); 217292080SimpUSB_PNP_HOST_INFO(rue_devs); 218184610Salfred 219192984Sthompsastatic const struct usb_ether_methods rue_ue_methods = { 220188412Sthompsa .ue_attach_post = rue_attach_post, 221188412Sthompsa .ue_start = rue_start, 222188412Sthompsa .ue_init = rue_init, 223188412Sthompsa .ue_stop = rue_stop, 224188412Sthompsa .ue_tick = rue_tick, 225188412Sthompsa .ue_setmulti = rue_setmulti, 226188412Sthompsa .ue_setpromisc = rue_setpromisc, 227188412Sthompsa .ue_mii_upd = rue_ifmedia_upd, 228188412Sthompsa .ue_mii_sts = rue_ifmedia_sts, 229188412Sthompsa}; 230184610Salfred 231188412Sthompsa#define RUE_SETBIT(sc, reg, x) \ 232188412Sthompsa rue_csr_write_1(sc, reg, rue_csr_read_1(sc, reg) | (x)) 233184610Salfred 234188412Sthompsa#define RUE_CLRBIT(sc, reg, x) \ 235188412Sthompsa rue_csr_write_1(sc, reg, rue_csr_read_1(sc, reg) & ~(x)) 236184610Salfred 237188412Sthompsastatic int 238188412Sthompsarue_read_mem(struct rue_softc *sc, uint16_t addr, void *buf, int len) 239184610Salfred{ 240192984Sthompsa struct usb_device_request req; 241184610Salfred 242184610Salfred req.bmRequestType = UT_READ_VENDOR_DEVICE; 243184610Salfred req.bRequest = UR_SET_ADDRESS; 244184610Salfred USETW(req.wValue, addr); 245184610Salfred USETW(req.wIndex, 0); 246184610Salfred USETW(req.wLength, len); 247184610Salfred 248194228Sthompsa return (uether_do_request(&sc->sc_ue, &req, buf, 1000)); 249184610Salfred} 250184610Salfred 251188412Sthompsastatic int 252188412Sthompsarue_write_mem(struct rue_softc *sc, uint16_t addr, void *buf, int len) 253184610Salfred{ 254192984Sthompsa struct usb_device_request req; 255184610Salfred 256184610Salfred req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 257184610Salfred req.bRequest = UR_SET_ADDRESS; 258184610Salfred USETW(req.wValue, addr); 259184610Salfred USETW(req.wIndex, 0); 260184610Salfred USETW(req.wLength, len); 261184610Salfred 262194228Sthompsa return (uether_do_request(&sc->sc_ue, &req, buf, 1000)); 263184610Salfred} 264184610Salfred 265184610Salfredstatic uint8_t 266188412Sthompsarue_csr_read_1(struct rue_softc *sc, uint16_t reg) 267184610Salfred{ 268184610Salfred uint8_t val; 269184610Salfred 270188412Sthompsa rue_read_mem(sc, reg, &val, 1); 271184610Salfred return (val); 272184610Salfred} 273184610Salfred 274184610Salfredstatic uint16_t 275188412Sthompsarue_csr_read_2(struct rue_softc *sc, uint16_t reg) 276184610Salfred{ 277184610Salfred uint8_t val[2]; 278184610Salfred 279188412Sthompsa rue_read_mem(sc, reg, &val, 2); 280184610Salfred return (UGETW(val)); 281184610Salfred} 282184610Salfred 283188412Sthompsastatic int 284188412Sthompsarue_csr_write_1(struct rue_softc *sc, uint16_t reg, uint8_t val) 285184610Salfred{ 286188412Sthompsa return (rue_write_mem(sc, reg, &val, 1)); 287184610Salfred} 288184610Salfred 289188412Sthompsastatic int 290188412Sthompsarue_csr_write_2(struct rue_softc *sc, uint16_t reg, uint16_t val) 291184610Salfred{ 292184610Salfred uint8_t temp[2]; 293184610Salfred 294184610Salfred USETW(temp, val); 295188412Sthompsa return (rue_write_mem(sc, reg, &temp, 2)); 296184610Salfred} 297184610Salfred 298188412Sthompsastatic int 299188412Sthompsarue_csr_write_4(struct rue_softc *sc, int reg, uint32_t val) 300184610Salfred{ 301184610Salfred uint8_t temp[4]; 302184610Salfred 303184610Salfred USETDW(temp, val); 304188412Sthompsa return (rue_write_mem(sc, reg, &temp, 4)); 305184610Salfred} 306184610Salfred 307184610Salfredstatic int 308188412Sthompsarue_miibus_readreg(device_t dev, int phy, int reg) 309184610Salfred{ 310184610Salfred struct rue_softc *sc = device_get_softc(dev); 311184610Salfred uint16_t rval; 312184610Salfred uint16_t ruereg; 313188412Sthompsa int locked; 314184610Salfred 315188412Sthompsa if (phy != 0) /* RTL8150 supports PHY == 0, only */ 316184610Salfred return (0); 317184610Salfred 318188412Sthompsa locked = mtx_owned(&sc->sc_mtx); 319188412Sthompsa if (!locked) 320188412Sthompsa RUE_LOCK(sc); 321188412Sthompsa 322184610Salfred switch (reg) { 323184610Salfred case MII_BMCR: 324184610Salfred ruereg = RUE_BMCR; 325184610Salfred break; 326184610Salfred case MII_BMSR: 327184610Salfred ruereg = RUE_BMSR; 328184610Salfred break; 329184610Salfred case MII_ANAR: 330184610Salfred ruereg = RUE_ANAR; 331184610Salfred break; 332184610Salfred case MII_ANER: 333184610Salfred ruereg = RUE_AER; 334184610Salfred break; 335184610Salfred case MII_ANLPAR: 336184610Salfred ruereg = RUE_ANLP; 337184610Salfred break; 338184610Salfred case MII_PHYIDR1: 339184610Salfred case MII_PHYIDR2: 340184610Salfred rval = 0; 341184610Salfred goto done; 342184610Salfred default: 343188412Sthompsa if (RUE_REG_MIN <= reg && reg <= RUE_REG_MAX) { 344188412Sthompsa rval = rue_csr_read_1(sc, reg); 345184610Salfred goto done; 346184610Salfred } 347188412Sthompsa device_printf(sc->sc_ue.ue_dev, "bad phy register\n"); 348184610Salfred rval = 0; 349184610Salfred goto done; 350184610Salfred } 351184610Salfred 352188412Sthompsa rval = rue_csr_read_2(sc, ruereg); 353184610Salfreddone: 354188412Sthompsa if (!locked) 355188412Sthompsa RUE_UNLOCK(sc); 356184610Salfred return (rval); 357184610Salfred} 358184610Salfred 359184610Salfredstatic int 360188412Sthompsarue_miibus_writereg(device_t dev, int phy, int reg, int data) 361184610Salfred{ 362184610Salfred struct rue_softc *sc = device_get_softc(dev); 363184610Salfred uint16_t ruereg; 364188412Sthompsa int locked; 365184610Salfred 366188412Sthompsa if (phy != 0) /* RTL8150 supports PHY == 0, only */ 367184610Salfred return (0); 368184610Salfred 369188412Sthompsa locked = mtx_owned(&sc->sc_mtx); 370188412Sthompsa if (!locked) 371188412Sthompsa RUE_LOCK(sc); 372188412Sthompsa 373184610Salfred switch (reg) { 374184610Salfred case MII_BMCR: 375184610Salfred ruereg = RUE_BMCR; 376184610Salfred break; 377184610Salfred case MII_BMSR: 378184610Salfred ruereg = RUE_BMSR; 379184610Salfred break; 380184610Salfred case MII_ANAR: 381184610Salfred ruereg = RUE_ANAR; 382184610Salfred break; 383184610Salfred case MII_ANER: 384184610Salfred ruereg = RUE_AER; 385184610Salfred break; 386184610Salfred case MII_ANLPAR: 387184610Salfred ruereg = RUE_ANLP; 388184610Salfred break; 389184610Salfred case MII_PHYIDR1: 390184610Salfred case MII_PHYIDR2: 391184610Salfred goto done; 392184610Salfred default: 393188412Sthompsa if (RUE_REG_MIN <= reg && reg <= RUE_REG_MAX) { 394188412Sthompsa rue_csr_write_1(sc, reg, data); 395184610Salfred goto done; 396184610Salfred } 397188412Sthompsa device_printf(sc->sc_ue.ue_dev, " bad phy register\n"); 398184610Salfred goto done; 399184610Salfred } 400188412Sthompsa rue_csr_write_2(sc, ruereg, data); 401184610Salfreddone: 402188412Sthompsa if (!locked) 403188412Sthompsa RUE_UNLOCK(sc); 404184610Salfred return (0); 405184610Salfred} 406184610Salfred 407184610Salfredstatic void 408188412Sthompsarue_miibus_statchg(device_t dev) 409184610Salfred{ 410184610Salfred /* 411184610Salfred * When the code below is enabled the card starts doing weird 412184610Salfred * things after link going from UP to DOWN and back UP. 413184610Salfred * 414184610Salfred * Looks like some of register writes below messes up PHY 415184610Salfred * interface. 416184610Salfred * 417184610Salfred * No visible regressions were found after commenting this code 418184610Salfred * out, so that disable it for good. 419184610Salfred */ 420184610Salfred#if 0 421184610Salfred struct rue_softc *sc = device_get_softc(dev); 422184610Salfred struct mii_data *mii = GET_MII(sc); 423184610Salfred uint16_t bmcr; 424188412Sthompsa int locked; 425184610Salfred 426188412Sthompsa locked = mtx_owned(&sc->sc_mtx); 427188412Sthompsa if (!locked) 428188412Sthompsa RUE_LOCK(sc); 429184610Salfred 430188412Sthompsa RUE_CLRBIT(sc, RUE_CR, (RUE_CR_RE | RUE_CR_TE)); 431184610Salfred 432188412Sthompsa bmcr = rue_csr_read_2(sc, RUE_BMCR); 433184610Salfred 434184610Salfred if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX) 435184610Salfred bmcr |= RUE_BMCR_SPD_SET; 436184610Salfred else 437184610Salfred bmcr &= ~RUE_BMCR_SPD_SET; 438184610Salfred 439184610Salfred if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) 440184610Salfred bmcr |= RUE_BMCR_DUPLEX; 441184610Salfred else 442184610Salfred bmcr &= ~RUE_BMCR_DUPLEX; 443184610Salfred 444188412Sthompsa rue_csr_write_2(sc, RUE_BMCR, bmcr); 445184610Salfred 446188412Sthompsa RUE_SETBIT(sc, RUE_CR, (RUE_CR_RE | RUE_CR_TE)); 447184610Salfred 448188412Sthompsa if (!locked) 449188412Sthompsa RUE_UNLOCK(sc); 450184610Salfred#endif 451184610Salfred} 452184610Salfred 453184610Salfredstatic void 454192984Sthompsarue_setpromisc(struct usb_ether *ue) 455184610Salfred{ 456194228Sthompsa struct rue_softc *sc = uether_getsc(ue); 457194228Sthompsa struct ifnet *ifp = uether_getifp(ue); 458184610Salfred 459188412Sthompsa RUE_LOCK_ASSERT(sc, MA_OWNED); 460184610Salfred 461188412Sthompsa /* If we want promiscuous mode, set the allframes bit. */ 462188412Sthompsa if (ifp->if_flags & IFF_PROMISC) 463188412Sthompsa RUE_SETBIT(sc, RUE_RCR, RUE_RCR_AAP); 464188412Sthompsa else 465188412Sthompsa RUE_CLRBIT(sc, RUE_RCR, RUE_RCR_AAP); 466184610Salfred} 467184610Salfred 468184610Salfred/* 469184610Salfred * Program the 64-bit multicast hash filter. 470184610Salfred */ 471184610Salfredstatic void 472192984Sthompsarue_setmulti(struct usb_ether *ue) 473184610Salfred{ 474194228Sthompsa struct rue_softc *sc = uether_getsc(ue); 475194228Sthompsa struct ifnet *ifp = uether_getifp(ue); 476184610Salfred uint16_t rxcfg; 477188412Sthompsa int h = 0; 478188412Sthompsa uint32_t hashes[2] = { 0, 0 }; 479188412Sthompsa struct ifmultiaddr *ifma; 480188412Sthompsa int mcnt = 0; 481184610Salfred 482188412Sthompsa RUE_LOCK_ASSERT(sc, MA_OWNED); 483184610Salfred 484188412Sthompsa rxcfg = rue_csr_read_2(sc, RUE_RCR); 485188412Sthompsa 486188412Sthompsa if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) { 487184610Salfred rxcfg |= (RUE_RCR_AAM | RUE_RCR_AAP); 488184610Salfred rxcfg &= ~RUE_RCR_AM; 489188412Sthompsa rue_csr_write_2(sc, RUE_RCR, rxcfg); 490188412Sthompsa rue_csr_write_4(sc, RUE_MAR0, 0xFFFFFFFF); 491188412Sthompsa rue_csr_write_4(sc, RUE_MAR4, 0xFFFFFFFF); 492184610Salfred return; 493184610Salfred } 494184610Salfred 495188412Sthompsa /* first, zot all the existing hash bits */ 496188412Sthompsa rue_csr_write_4(sc, RUE_MAR0, 0); 497188412Sthompsa rue_csr_write_4(sc, RUE_MAR4, 0); 498188412Sthompsa 499188412Sthompsa /* now program new ones */ 500195049Srwatson if_maddr_rlock(ifp); 501188412Sthompsa TAILQ_FOREACH (ifma, &ifp->if_multiaddrs, ifma_link) 502188412Sthompsa { 503188412Sthompsa if (ifma->ifma_addr->sa_family != AF_LINK) 504188412Sthompsa continue; 505188412Sthompsa h = ether_crc32_be(LLADDR((struct sockaddr_dl *) 506188412Sthompsa ifma->ifma_addr), ETHER_ADDR_LEN) >> 26; 507188412Sthompsa if (h < 32) 508188412Sthompsa hashes[0] |= (1 << h); 509188412Sthompsa else 510188412Sthompsa hashes[1] |= (1 << (h - 32)); 511188412Sthompsa mcnt++; 512188412Sthompsa } 513195049Srwatson if_maddr_runlock(ifp); 514188412Sthompsa 515188412Sthompsa if (mcnt) 516184610Salfred rxcfg |= RUE_RCR_AM; 517184610Salfred else 518184610Salfred rxcfg &= ~RUE_RCR_AM; 519184610Salfred 520184610Salfred rxcfg &= ~(RUE_RCR_AAM | RUE_RCR_AAP); 521184610Salfred 522188412Sthompsa rue_csr_write_2(sc, RUE_RCR, rxcfg); 523188412Sthompsa rue_csr_write_4(sc, RUE_MAR0, hashes[0]); 524188412Sthompsa rue_csr_write_4(sc, RUE_MAR4, hashes[1]); 525184610Salfred} 526184610Salfred 527184610Salfredstatic void 528188412Sthompsarue_reset(struct rue_softc *sc) 529184610Salfred{ 530188412Sthompsa int i; 531184610Salfred 532188412Sthompsa rue_csr_write_1(sc, RUE_CR, RUE_CR_SOFT_RST); 533184610Salfred 534188412Sthompsa for (i = 0; i != RUE_TIMEOUT; i++) { 535194228Sthompsa if (uether_pause(&sc->sc_ue, hz / 1000)) 536188412Sthompsa break; 537188412Sthompsa if (!(rue_csr_read_1(sc, RUE_CR) & RUE_CR_SOFT_RST)) 538188412Sthompsa break; 539188412Sthompsa } 540188412Sthompsa if (i == RUE_TIMEOUT) 541199816Sthompsa device_printf(sc->sc_ue.ue_dev, "reset never completed\n"); 542184610Salfred 543194228Sthompsa uether_pause(&sc->sc_ue, hz / 100); 544188412Sthompsa} 545184610Salfred 546188412Sthompsastatic void 547192984Sthompsarue_attach_post(struct usb_ether *ue) 548188412Sthompsa{ 549194228Sthompsa struct rue_softc *sc = uether_getsc(ue); 550184610Salfred 551188412Sthompsa /* reset the adapter */ 552188412Sthompsa rue_reset(sc); 553184610Salfred 554188412Sthompsa /* get station address from the EEPROM */ 555188412Sthompsa rue_read_mem(sc, RUE_EEPROM_IDR0, ue->ue_eaddr, ETHER_ADDR_LEN); 556184610Salfred} 557184610Salfred 558184610Salfred/* 559184610Salfred * Probe for a RTL8150 chip. 560184610Salfred */ 561184610Salfredstatic int 562184610Salfredrue_probe(device_t dev) 563184610Salfred{ 564192984Sthompsa struct usb_attach_arg *uaa = device_get_ivars(dev); 565184610Salfred 566192499Sthompsa if (uaa->usb_mode != USB_MODE_HOST) 567184610Salfred return (ENXIO); 568188412Sthompsa if (uaa->info.bConfigIndex != RUE_CONFIG_IDX) 569184610Salfred return (ENXIO); 570188412Sthompsa if (uaa->info.bIfaceIndex != RUE_IFACE_IDX) 571184610Salfred return (ENXIO); 572188412Sthompsa 573194228Sthompsa return (usbd_lookup_id_by_uaa(rue_devs, sizeof(rue_devs), uaa)); 574184610Salfred} 575184610Salfred 576184610Salfred/* 577184610Salfred * Attach the interface. Allocate softc structures, do ifmedia 578184610Salfred * setup and ethernet/BPF attach. 579184610Salfred */ 580184610Salfredstatic int 581184610Salfredrue_attach(device_t dev) 582184610Salfred{ 583192984Sthompsa struct usb_attach_arg *uaa = device_get_ivars(dev); 584184610Salfred struct rue_softc *sc = device_get_softc(dev); 585192984Sthompsa struct usb_ether *ue = &sc->sc_ue; 586184610Salfred uint8_t iface_index; 587188412Sthompsa int error; 588184610Salfred 589194228Sthompsa device_set_usb_desc(dev); 590188412Sthompsa mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF); 591184610Salfred 592184610Salfred iface_index = RUE_IFACE_IDX; 593194228Sthompsa error = usbd_transfer_setup(uaa->device, &iface_index, 594187259Sthompsa sc->sc_xfer, rue_config, RUE_N_TRANSFER, 595184610Salfred sc, &sc->sc_mtx); 596184610Salfred if (error) { 597199816Sthompsa device_printf(dev, "allocating USB transfers failed\n"); 598184610Salfred goto detach; 599184610Salfred } 600188412Sthompsa 601188412Sthompsa ue->ue_sc = sc; 602188412Sthompsa ue->ue_dev = dev; 603188412Sthompsa ue->ue_udev = uaa->device; 604188412Sthompsa ue->ue_mtx = &sc->sc_mtx; 605188412Sthompsa ue->ue_methods = &rue_ue_methods; 606188412Sthompsa 607194228Sthompsa error = uether_ifattach(ue); 608184610Salfred if (error) { 609188412Sthompsa device_printf(dev, "could not attach interface\n"); 610184610Salfred goto detach; 611184610Salfred } 612184610Salfred return (0); /* success */ 613184610Salfred 614184610Salfreddetach: 615184610Salfred rue_detach(dev); 616184610Salfred return (ENXIO); /* failure */ 617184610Salfred} 618184610Salfred 619184610Salfredstatic int 620184610Salfredrue_detach(device_t dev) 621184610Salfred{ 622184610Salfred struct rue_softc *sc = device_get_softc(dev); 623192984Sthompsa struct usb_ether *ue = &sc->sc_ue; 624184610Salfred 625194228Sthompsa usbd_transfer_unsetup(sc->sc_xfer, RUE_N_TRANSFER); 626194228Sthompsa uether_ifdetach(ue); 627184610Salfred mtx_destroy(&sc->sc_mtx); 628184610Salfred 629184610Salfred return (0); 630184610Salfred} 631184610Salfred 632184610Salfredstatic void 633194677Sthompsarue_intr_callback(struct usb_xfer *xfer, usb_error_t error) 634184610Salfred{ 635194677Sthompsa struct rue_softc *sc = usbd_xfer_softc(xfer); 636194228Sthompsa struct ifnet *ifp = uether_getifp(&sc->sc_ue); 637184610Salfred struct rue_intrpkt pkt; 638194677Sthompsa struct usb_page_cache *pc; 639194677Sthompsa int actlen; 640184610Salfred 641194677Sthompsa usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 642194677Sthompsa 643184610Salfred switch (USB_GET_STATE(xfer)) { 644184610Salfred case USB_ST_TRANSFERRED: 645184610Salfred 646184610Salfred if (ifp && (ifp->if_drv_flags & IFF_DRV_RUNNING) && 647233774Shselasky actlen >= (int)sizeof(pkt)) { 648184610Salfred 649194677Sthompsa pc = usbd_xfer_get_frame(xfer, 0); 650194677Sthompsa usbd_copy_out(pc, 0, &pkt, sizeof(pkt)); 651184610Salfred 652271832Sglebius if_inc_counter(ifp, IFCOUNTER_IERRORS, pkt.rue_rxlost_cnt); 653271832Sglebius if_inc_counter(ifp, IFCOUNTER_IERRORS, pkt.rue_crcerr_cnt); 654271832Sglebius if_inc_counter(ifp, IFCOUNTER_COLLISIONS, pkt.rue_col_cnt); 655184610Salfred } 656188412Sthompsa /* FALLTHROUGH */ 657184610Salfred case USB_ST_SETUP: 658188412Sthompsatr_setup: 659194677Sthompsa usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 660194228Sthompsa usbd_transfer_submit(xfer); 661184610Salfred return; 662184610Salfred 663184610Salfred default: /* Error */ 664194677Sthompsa if (error != USB_ERR_CANCELLED) { 665188412Sthompsa /* try to clear stall first */ 666194677Sthompsa usbd_xfer_set_stall(xfer); 667188412Sthompsa goto tr_setup; 668184610Salfred } 669184610Salfred return; 670184610Salfred } 671184610Salfred} 672184610Salfred 673184610Salfredstatic void 674194677Sthompsarue_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error) 675184610Salfred{ 676194677Sthompsa struct rue_softc *sc = usbd_xfer_softc(xfer); 677192984Sthompsa struct usb_ether *ue = &sc->sc_ue; 678194228Sthompsa struct ifnet *ifp = uether_getifp(ue); 679194677Sthompsa struct usb_page_cache *pc; 680184610Salfred uint16_t status; 681194677Sthompsa int actlen; 682184610Salfred 683194677Sthompsa usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 684194677Sthompsa 685184610Salfred switch (USB_GET_STATE(xfer)) { 686184610Salfred case USB_ST_TRANSFERRED: 687184610Salfred 688194677Sthompsa if (actlen < 4) { 689271832Sglebius if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 690184610Salfred goto tr_setup; 691184610Salfred } 692194677Sthompsa pc = usbd_xfer_get_frame(xfer, 0); 693194677Sthompsa usbd_copy_out(pc, actlen - 4, &status, sizeof(status)); 694194677Sthompsa actlen -= 4; 695184610Salfred 696298932Spfg /* check receive packet was valid or not */ 697184610Salfred status = le16toh(status); 698184610Salfred if ((status & RUE_RXSTAT_VALID) == 0) { 699271832Sglebius if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 700184610Salfred goto tr_setup; 701184610Salfred } 702194677Sthompsa uether_rxbuf(ue, pc, 0, actlen); 703188412Sthompsa /* FALLTHROUGH */ 704184610Salfred case USB_ST_SETUP: 705184610Salfredtr_setup: 706194677Sthompsa usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 707194228Sthompsa usbd_transfer_submit(xfer); 708194228Sthompsa uether_rxflush(ue); 709184610Salfred return; 710184610Salfred 711184610Salfred default: /* Error */ 712188412Sthompsa DPRINTF("bulk read error, %s\n", 713194677Sthompsa usbd_errstr(error)); 714188412Sthompsa 715194677Sthompsa if (error != USB_ERR_CANCELLED) { 716184610Salfred /* try to clear stall first */ 717194677Sthompsa usbd_xfer_set_stall(xfer); 718188412Sthompsa goto tr_setup; 719184610Salfred } 720184610Salfred return; 721184610Salfred } 722184610Salfred} 723184610Salfred 724184610Salfredstatic void 725194677Sthompsarue_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error) 726184610Salfred{ 727194677Sthompsa struct rue_softc *sc = usbd_xfer_softc(xfer); 728194228Sthompsa struct ifnet *ifp = uether_getifp(&sc->sc_ue); 729194677Sthompsa struct usb_page_cache *pc; 730184610Salfred struct mbuf *m; 731188412Sthompsa int temp_len; 732184610Salfred 733184610Salfred switch (USB_GET_STATE(xfer)) { 734184610Salfred case USB_ST_TRANSFERRED: 735184610Salfred DPRINTFN(11, "transfer complete\n"); 736271832Sglebius if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); 737184610Salfred 738188412Sthompsa /* FALLTHROUGH */ 739184610Salfred case USB_ST_SETUP: 740188412Sthompsatr_setup: 741188412Sthompsa if ((sc->sc_flags & RUE_FLAG_LINK) == 0) { 742184610Salfred /* 743184610Salfred * don't send anything if there is no link ! 744184610Salfred */ 745188412Sthompsa return; 746184610Salfred } 747184610Salfred IFQ_DRV_DEQUEUE(&ifp->if_snd, m); 748184610Salfred 749188412Sthompsa if (m == NULL) 750188412Sthompsa return; 751188412Sthompsa if (m->m_pkthdr.len > MCLBYTES) 752184610Salfred m->m_pkthdr.len = MCLBYTES; 753184610Salfred temp_len = m->m_pkthdr.len; 754184610Salfred 755194677Sthompsa pc = usbd_xfer_get_frame(xfer, 0); 756194677Sthompsa usbd_m_copy_in(pc, 0, m, 0, m->m_pkthdr.len); 757184610Salfred 758184610Salfred /* 759184610Salfred * This is an undocumented behavior. 760184610Salfred * RTL8150 chip doesn't send frame length smaller than 761184610Salfred * RUE_MIN_FRAMELEN (60) byte packet. 762184610Salfred */ 763184610Salfred if (temp_len < RUE_MIN_FRAMELEN) { 764194677Sthompsa usbd_frame_zero(pc, temp_len, 765184610Salfred RUE_MIN_FRAMELEN - temp_len); 766184610Salfred temp_len = RUE_MIN_FRAMELEN; 767184610Salfred } 768194677Sthompsa usbd_xfer_set_frame_len(xfer, 0, temp_len); 769184610Salfred 770184610Salfred /* 771184610Salfred * if there's a BPF listener, bounce a copy 772184610Salfred * of this frame to him: 773184610Salfred */ 774184610Salfred BPF_MTAP(ifp, m); 775184610Salfred 776184610Salfred m_freem(m); 777184610Salfred 778194228Sthompsa usbd_transfer_submit(xfer); 779184610Salfred 780184610Salfred return; 781184610Salfred 782184610Salfred default: /* Error */ 783184610Salfred DPRINTFN(11, "transfer error, %s\n", 784194677Sthompsa usbd_errstr(error)); 785184610Salfred 786271832Sglebius if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 787188412Sthompsa 788194677Sthompsa if (error != USB_ERR_CANCELLED) { 789184610Salfred /* try to clear stall first */ 790194677Sthompsa usbd_xfer_set_stall(xfer); 791188412Sthompsa goto tr_setup; 792184610Salfred } 793184610Salfred return; 794184610Salfred } 795184610Salfred} 796184610Salfred 797184610Salfredstatic void 798192984Sthompsarue_tick(struct usb_ether *ue) 799184610Salfred{ 800194228Sthompsa struct rue_softc *sc = uether_getsc(ue); 801184610Salfred struct mii_data *mii = GET_MII(sc); 802184610Salfred 803188412Sthompsa RUE_LOCK_ASSERT(sc, MA_OWNED); 804188412Sthompsa 805184610Salfred mii_tick(mii); 806188412Sthompsa if ((sc->sc_flags & RUE_FLAG_LINK) == 0 807188412Sthompsa && mii->mii_media_status & IFM_ACTIVE && 808188412Sthompsa IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) { 809188412Sthompsa sc->sc_flags |= RUE_FLAG_LINK; 810188412Sthompsa rue_start(ue); 811184610Salfred } 812184610Salfred} 813184610Salfred 814184610Salfredstatic void 815192984Sthompsarue_start(struct usb_ether *ue) 816184610Salfred{ 817194228Sthompsa struct rue_softc *sc = uether_getsc(ue); 818184610Salfred 819188412Sthompsa /* 820188412Sthompsa * start the USB transfers, if not already started: 821188412Sthompsa */ 822194228Sthompsa usbd_transfer_start(sc->sc_xfer[RUE_INTR_DT_RD]); 823194228Sthompsa usbd_transfer_start(sc->sc_xfer[RUE_BULK_DT_RD]); 824194228Sthompsa usbd_transfer_start(sc->sc_xfer[RUE_BULK_DT_WR]); 825184610Salfred} 826184610Salfred 827184610Salfredstatic void 828192984Sthompsarue_init(struct usb_ether *ue) 829184610Salfred{ 830194228Sthompsa struct rue_softc *sc = uether_getsc(ue); 831194228Sthompsa struct ifnet *ifp = uether_getifp(ue); 832184610Salfred 833188412Sthompsa RUE_LOCK_ASSERT(sc, MA_OWNED); 834184610Salfred 835184610Salfred /* 836184610Salfred * Cancel pending I/O 837184610Salfred */ 838188412Sthompsa rue_reset(sc); 839184610Salfred 840188412Sthompsa /* Set MAC address */ 841188412Sthompsa rue_write_mem(sc, RUE_IDR0, IF_LLADDR(ifp), ETHER_ADDR_LEN); 842184610Salfred 843188412Sthompsa rue_stop(ue); 844184610Salfred 845184610Salfred /* 846184610Salfred * Set the initial TX and RX configuration. 847184610Salfred */ 848188412Sthompsa rue_csr_write_1(sc, RUE_TCR, RUE_TCR_CONFIG); 849188412Sthompsa rue_csr_write_2(sc, RUE_RCR, RUE_RCR_CONFIG|RUE_RCR_AB); 850184610Salfred 851184610Salfred /* Load the multicast filter */ 852188412Sthompsa rue_setpromisc(ue); 853188412Sthompsa /* Load the multicast filter. */ 854188412Sthompsa rue_setmulti(ue); 855184610Salfred 856184610Salfred /* Enable RX and TX */ 857188412Sthompsa rue_csr_write_1(sc, RUE_CR, (RUE_CR_TE | RUE_CR_RE | RUE_CR_EP3CLREN)); 858184610Salfred 859194677Sthompsa usbd_xfer_set_stall(sc->sc_xfer[RUE_BULK_DT_WR]); 860184610Salfred 861188412Sthompsa ifp->if_drv_flags |= IFF_DRV_RUNNING; 862188412Sthompsa rue_start(ue); 863184610Salfred} 864184610Salfred 865184610Salfred/* 866184610Salfred * Set media options. 867184610Salfred */ 868184610Salfredstatic int 869188412Sthompsarue_ifmedia_upd(struct ifnet *ifp) 870184610Salfred{ 871184610Salfred struct rue_softc *sc = ifp->if_softc; 872184610Salfred struct mii_data *mii = GET_MII(sc); 873221407Smarius struct mii_softc *miisc; 874251734Skevlo int error; 875184610Salfred 876188412Sthompsa RUE_LOCK_ASSERT(sc, MA_OWNED); 877184610Salfred 878188412Sthompsa sc->sc_flags &= ~RUE_FLAG_LINK; 879221407Smarius LIST_FOREACH(miisc, &mii->mii_phys, mii_list) 880221407Smarius PHY_RESET(miisc); 881251734Skevlo error = mii_mediachg(mii); 882251734Skevlo return (error); 883184610Salfred} 884184610Salfred 885184610Salfred/* 886184610Salfred * Report current media status. 887184610Salfred */ 888184610Salfredstatic void 889188412Sthompsarue_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr) 890184610Salfred{ 891184610Salfred struct rue_softc *sc = ifp->if_softc; 892188412Sthompsa struct mii_data *mii = GET_MII(sc); 893184610Salfred 894188412Sthompsa RUE_LOCK(sc); 895188412Sthompsa mii_pollstat(mii); 896188412Sthompsa ifmr->ifm_active = mii->mii_media_active; 897188412Sthompsa ifmr->ifm_status = mii->mii_media_status; 898226479Syongari RUE_UNLOCK(sc); 899184610Salfred} 900184610Salfred 901184610Salfredstatic void 902192984Sthompsarue_stop(struct usb_ether *ue) 903184610Salfred{ 904194228Sthompsa struct rue_softc *sc = uether_getsc(ue); 905194228Sthompsa struct ifnet *ifp = uether_getifp(ue); 906184610Salfred 907188412Sthompsa RUE_LOCK_ASSERT(sc, MA_OWNED); 908184610Salfred 909188412Sthompsa ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 910188412Sthompsa sc->sc_flags &= ~RUE_FLAG_LINK; 911184610Salfred 912184610Salfred /* 913184610Salfred * stop all the transfers, if not already stopped: 914184610Salfred */ 915194228Sthompsa usbd_transfer_stop(sc->sc_xfer[RUE_BULK_DT_WR]); 916194228Sthompsa usbd_transfer_stop(sc->sc_xfer[RUE_BULK_DT_RD]); 917194228Sthompsa usbd_transfer_stop(sc->sc_xfer[RUE_INTR_DT_RD]); 918184610Salfred 919188412Sthompsa rue_csr_write_1(sc, RUE_CR, 0x00); 920184610Salfred 921188412Sthompsa rue_reset(sc); 922184610Salfred} 923