if_rue.c revision 195049
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: head/sys/dev/usb/net/if_rue.c 195049 2009-06-26 11:45:06Z rwatson $"); 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> 74194677Sthompsa#include <sys/kernel.h> 75194677Sthompsa#include <sys/bus.h> 76194677Sthompsa#include <sys/linker_set.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 88194677Sthompsa#include <dev/usb/usb.h> 89194677Sthompsa#include <dev/usb/usbdi.h> 90194677Sthompsa#include <dev/usb/usbdi_util.h> 91188746Sthompsa#include "usbdevs.h" 92184610Salfred 93184610Salfred#define USB_DEBUG_VAR rue_debug 94194677Sthompsa#include <dev/usb/usb_debug.h> 95188942Sthompsa#include <dev/usb/usb_process.h> 96184610Salfred 97188942Sthompsa#include <dev/usb/net/usb_ethernet.h> 98188942Sthompsa#include <dev/usb/net/if_ruereg.h> 99184610Salfred 100184610Salfred#if USB_DEBUG 101184610Salfredstatic int rue_debug = 0; 102184610Salfred 103192502SthompsaSYSCTL_NODE(_hw_usb, OID_AUTO, rue, CTLFLAG_RW, 0, "USB rue"); 104192502SthompsaSYSCTL_INT(_hw_usb_rue, OID_AUTO, debug, CTLFLAG_RW, 105184610Salfred &rue_debug, 0, "Debug level"); 106184610Salfred#endif 107184610Salfred 108184610Salfred/* 109184610Salfred * Various supported device vendors/products. 110184610Salfred */ 111184610Salfred 112192984Sthompsastatic const struct usb_device_id rue_devs[] = { 113184610Salfred {USB_VPI(USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUAKTX, 0)}, 114184610Salfred {USB_VPI(USB_VENDOR_REALTEK, USB_PRODUCT_REALTEK_USBKR100, 0)}, 115184610Salfred}; 116184610Salfred 117184610Salfred/* prototypes */ 118184610Salfred 119184610Salfredstatic device_probe_t rue_probe; 120184610Salfredstatic device_attach_t rue_attach; 121184610Salfredstatic device_detach_t rue_detach; 122184610Salfred 123188412Sthompsastatic miibus_readreg_t rue_miibus_readreg; 124188412Sthompsastatic miibus_writereg_t rue_miibus_writereg; 125188412Sthompsastatic miibus_statchg_t rue_miibus_statchg; 126188412Sthompsa 127193045Sthompsastatic usb_callback_t rue_intr_callback; 128193045Sthompsastatic usb_callback_t rue_bulk_read_callback; 129193045Sthompsastatic usb_callback_t rue_bulk_write_callback; 130184610Salfred 131193045Sthompsastatic uether_fn_t rue_attach_post; 132193045Sthompsastatic uether_fn_t rue_init; 133193045Sthompsastatic uether_fn_t rue_stop; 134193045Sthompsastatic uether_fn_t rue_start; 135193045Sthompsastatic uether_fn_t rue_tick; 136193045Sthompsastatic uether_fn_t rue_setmulti; 137193045Sthompsastatic uether_fn_t rue_setpromisc; 138184610Salfred 139188412Sthompsastatic int rue_read_mem(struct rue_softc *, uint16_t, void *, int); 140188412Sthompsastatic int rue_write_mem(struct rue_softc *, uint16_t, void *, int); 141188412Sthompsastatic uint8_t rue_csr_read_1(struct rue_softc *, uint16_t); 142188412Sthompsastatic uint16_t rue_csr_read_2(struct rue_softc *, uint16_t); 143188412Sthompsastatic int rue_csr_write_1(struct rue_softc *, uint16_t, uint8_t); 144188412Sthompsastatic int rue_csr_write_2(struct rue_softc *, uint16_t, uint16_t); 145188412Sthompsastatic int rue_csr_write_4(struct rue_softc *, int, uint32_t); 146184610Salfred 147188412Sthompsastatic void rue_reset(struct rue_softc *); 148188412Sthompsastatic int rue_ifmedia_upd(struct ifnet *); 149188412Sthompsastatic void rue_ifmedia_sts(struct ifnet *, struct ifmediareq *); 150184610Salfred 151192984Sthompsastatic const struct usb_config rue_config[RUE_N_TRANSFER] = { 152184610Salfred 153187259Sthompsa [RUE_BULK_DT_WR] = { 154184610Salfred .type = UE_BULK, 155184610Salfred .endpoint = UE_ADDR_ANY, 156184610Salfred .direction = UE_DIR_OUT, 157190734Sthompsa .bufsize = MCLBYTES, 158190734Sthompsa .flags = {.pipe_bof = 1,.force_short_xfer = 1,}, 159190734Sthompsa .callback = rue_bulk_write_callback, 160190734Sthompsa .timeout = 10000, /* 10 seconds */ 161184610Salfred }, 162184610Salfred 163187259Sthompsa [RUE_BULK_DT_RD] = { 164184610Salfred .type = UE_BULK, 165184610Salfred .endpoint = UE_ADDR_ANY, 166184610Salfred .direction = UE_DIR_IN, 167190734Sthompsa .bufsize = (MCLBYTES + 4), 168190734Sthompsa .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 169190734Sthompsa .callback = rue_bulk_read_callback, 170190734Sthompsa .timeout = 0, /* no timeout */ 171184610Salfred }, 172184610Salfred 173187259Sthompsa [RUE_INTR_DT_RD] = { 174184610Salfred .type = UE_INTERRUPT, 175184610Salfred .endpoint = UE_ADDR_ANY, 176184610Salfred .direction = UE_DIR_IN, 177190734Sthompsa .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 178190734Sthompsa .bufsize = 0, /* use wMaxPacketSize */ 179190734Sthompsa .callback = rue_intr_callback, 180184610Salfred }, 181184610Salfred}; 182184610Salfred 183184610Salfredstatic device_method_t rue_methods[] = { 184184610Salfred /* Device interface */ 185184610Salfred DEVMETHOD(device_probe, rue_probe), 186184610Salfred DEVMETHOD(device_attach, rue_attach), 187184610Salfred DEVMETHOD(device_detach, rue_detach), 188184610Salfred 189184610Salfred /* Bus interface */ 190184610Salfred DEVMETHOD(bus_print_child, bus_generic_print_child), 191184610Salfred DEVMETHOD(bus_driver_added, bus_generic_driver_added), 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 198184610Salfred {0, 0} 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 209189275SthompsaDRIVER_MODULE(rue, uhub, rue_driver, rue_devclass, NULL, 0); 210184610SalfredDRIVER_MODULE(miibus, rue, miibus_driver, miibus_devclass, 0, 0); 211188942SthompsaMODULE_DEPEND(rue, uether, 1, 1, 1); 212188942SthompsaMODULE_DEPEND(rue, usb, 1, 1, 1); 213184610SalfredMODULE_DEPEND(rue, ether, 1, 1, 1); 214184610SalfredMODULE_DEPEND(rue, miibus, 1, 1, 1); 215184610Salfred 216192984Sthompsastatic const struct usb_ether_methods rue_ue_methods = { 217188412Sthompsa .ue_attach_post = rue_attach_post, 218188412Sthompsa .ue_start = rue_start, 219188412Sthompsa .ue_init = rue_init, 220188412Sthompsa .ue_stop = rue_stop, 221188412Sthompsa .ue_tick = rue_tick, 222188412Sthompsa .ue_setmulti = rue_setmulti, 223188412Sthompsa .ue_setpromisc = rue_setpromisc, 224188412Sthompsa .ue_mii_upd = rue_ifmedia_upd, 225188412Sthompsa .ue_mii_sts = rue_ifmedia_sts, 226188412Sthompsa}; 227184610Salfred 228188412Sthompsa#define RUE_SETBIT(sc, reg, x) \ 229188412Sthompsa rue_csr_write_1(sc, reg, rue_csr_read_1(sc, reg) | (x)) 230184610Salfred 231188412Sthompsa#define RUE_CLRBIT(sc, reg, x) \ 232188412Sthompsa rue_csr_write_1(sc, reg, rue_csr_read_1(sc, reg) & ~(x)) 233184610Salfred 234188412Sthompsastatic int 235188412Sthompsarue_read_mem(struct rue_softc *sc, uint16_t addr, void *buf, int len) 236184610Salfred{ 237192984Sthompsa struct usb_device_request req; 238184610Salfred 239184610Salfred req.bmRequestType = UT_READ_VENDOR_DEVICE; 240184610Salfred req.bRequest = UR_SET_ADDRESS; 241184610Salfred USETW(req.wValue, addr); 242184610Salfred USETW(req.wIndex, 0); 243184610Salfred USETW(req.wLength, len); 244184610Salfred 245194228Sthompsa return (uether_do_request(&sc->sc_ue, &req, buf, 1000)); 246184610Salfred} 247184610Salfred 248188412Sthompsastatic int 249188412Sthompsarue_write_mem(struct rue_softc *sc, uint16_t addr, void *buf, int len) 250184610Salfred{ 251192984Sthompsa struct usb_device_request req; 252184610Salfred 253184610Salfred req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 254184610Salfred req.bRequest = UR_SET_ADDRESS; 255184610Salfred USETW(req.wValue, addr); 256184610Salfred USETW(req.wIndex, 0); 257184610Salfred USETW(req.wLength, len); 258184610Salfred 259194228Sthompsa return (uether_do_request(&sc->sc_ue, &req, buf, 1000)); 260184610Salfred} 261184610Salfred 262184610Salfredstatic uint8_t 263188412Sthompsarue_csr_read_1(struct rue_softc *sc, uint16_t reg) 264184610Salfred{ 265184610Salfred uint8_t val; 266184610Salfred 267188412Sthompsa rue_read_mem(sc, reg, &val, 1); 268184610Salfred return (val); 269184610Salfred} 270184610Salfred 271184610Salfredstatic uint16_t 272188412Sthompsarue_csr_read_2(struct rue_softc *sc, uint16_t reg) 273184610Salfred{ 274184610Salfred uint8_t val[2]; 275184610Salfred 276188412Sthompsa rue_read_mem(sc, reg, &val, 2); 277184610Salfred return (UGETW(val)); 278184610Salfred} 279184610Salfred 280188412Sthompsastatic int 281188412Sthompsarue_csr_write_1(struct rue_softc *sc, uint16_t reg, uint8_t val) 282184610Salfred{ 283188412Sthompsa return (rue_write_mem(sc, reg, &val, 1)); 284184610Salfred} 285184610Salfred 286188412Sthompsastatic int 287188412Sthompsarue_csr_write_2(struct rue_softc *sc, uint16_t reg, uint16_t val) 288184610Salfred{ 289184610Salfred uint8_t temp[2]; 290184610Salfred 291184610Salfred USETW(temp, val); 292188412Sthompsa return (rue_write_mem(sc, reg, &temp, 2)); 293184610Salfred} 294184610Salfred 295188412Sthompsastatic int 296188412Sthompsarue_csr_write_4(struct rue_softc *sc, int reg, uint32_t val) 297184610Salfred{ 298184610Salfred uint8_t temp[4]; 299184610Salfred 300184610Salfred USETDW(temp, val); 301188412Sthompsa return (rue_write_mem(sc, reg, &temp, 4)); 302184610Salfred} 303184610Salfred 304184610Salfredstatic int 305188412Sthompsarue_miibus_readreg(device_t dev, int phy, int reg) 306184610Salfred{ 307184610Salfred struct rue_softc *sc = device_get_softc(dev); 308184610Salfred uint16_t rval; 309184610Salfred uint16_t ruereg; 310188412Sthompsa int locked; 311184610Salfred 312188412Sthompsa if (phy != 0) /* RTL8150 supports PHY == 0, only */ 313184610Salfred return (0); 314184610Salfred 315188412Sthompsa locked = mtx_owned(&sc->sc_mtx); 316188412Sthompsa if (!locked) 317188412Sthompsa RUE_LOCK(sc); 318188412Sthompsa 319184610Salfred switch (reg) { 320184610Salfred case MII_BMCR: 321184610Salfred ruereg = RUE_BMCR; 322184610Salfred break; 323184610Salfred case MII_BMSR: 324184610Salfred ruereg = RUE_BMSR; 325184610Salfred break; 326184610Salfred case MII_ANAR: 327184610Salfred ruereg = RUE_ANAR; 328184610Salfred break; 329184610Salfred case MII_ANER: 330184610Salfred ruereg = RUE_AER; 331184610Salfred break; 332184610Salfred case MII_ANLPAR: 333184610Salfred ruereg = RUE_ANLP; 334184610Salfred break; 335184610Salfred case MII_PHYIDR1: 336184610Salfred case MII_PHYIDR2: 337184610Salfred rval = 0; 338184610Salfred goto done; 339184610Salfred default: 340188412Sthompsa if (RUE_REG_MIN <= reg && reg <= RUE_REG_MAX) { 341188412Sthompsa rval = rue_csr_read_1(sc, reg); 342184610Salfred goto done; 343184610Salfred } 344188412Sthompsa device_printf(sc->sc_ue.ue_dev, "bad phy register\n"); 345184610Salfred rval = 0; 346184610Salfred goto done; 347184610Salfred } 348184610Salfred 349188412Sthompsa rval = rue_csr_read_2(sc, ruereg); 350184610Salfreddone: 351188412Sthompsa if (!locked) 352188412Sthompsa RUE_UNLOCK(sc); 353184610Salfred return (rval); 354184610Salfred} 355184610Salfred 356184610Salfredstatic int 357188412Sthompsarue_miibus_writereg(device_t dev, int phy, int reg, int data) 358184610Salfred{ 359184610Salfred struct rue_softc *sc = device_get_softc(dev); 360184610Salfred uint16_t ruereg; 361188412Sthompsa int locked; 362184610Salfred 363188412Sthompsa if (phy != 0) /* RTL8150 supports PHY == 0, only */ 364184610Salfred return (0); 365184610Salfred 366188412Sthompsa locked = mtx_owned(&sc->sc_mtx); 367188412Sthompsa if (!locked) 368188412Sthompsa RUE_LOCK(sc); 369188412Sthompsa 370184610Salfred switch (reg) { 371184610Salfred case MII_BMCR: 372184610Salfred ruereg = RUE_BMCR; 373184610Salfred break; 374184610Salfred case MII_BMSR: 375184610Salfred ruereg = RUE_BMSR; 376184610Salfred break; 377184610Salfred case MII_ANAR: 378184610Salfred ruereg = RUE_ANAR; 379184610Salfred break; 380184610Salfred case MII_ANER: 381184610Salfred ruereg = RUE_AER; 382184610Salfred break; 383184610Salfred case MII_ANLPAR: 384184610Salfred ruereg = RUE_ANLP; 385184610Salfred break; 386184610Salfred case MII_PHYIDR1: 387184610Salfred case MII_PHYIDR2: 388184610Salfred goto done; 389184610Salfred default: 390188412Sthompsa if (RUE_REG_MIN <= reg && reg <= RUE_REG_MAX) { 391188412Sthompsa rue_csr_write_1(sc, reg, data); 392184610Salfred goto done; 393184610Salfred } 394188412Sthompsa device_printf(sc->sc_ue.ue_dev, " bad phy register\n"); 395184610Salfred goto done; 396184610Salfred } 397188412Sthompsa rue_csr_write_2(sc, ruereg, data); 398184610Salfreddone: 399188412Sthompsa if (!locked) 400188412Sthompsa RUE_UNLOCK(sc); 401184610Salfred return (0); 402184610Salfred} 403184610Salfred 404184610Salfredstatic void 405188412Sthompsarue_miibus_statchg(device_t dev) 406184610Salfred{ 407184610Salfred /* 408184610Salfred * When the code below is enabled the card starts doing weird 409184610Salfred * things after link going from UP to DOWN and back UP. 410184610Salfred * 411184610Salfred * Looks like some of register writes below messes up PHY 412184610Salfred * interface. 413184610Salfred * 414184610Salfred * No visible regressions were found after commenting this code 415184610Salfred * out, so that disable it for good. 416184610Salfred */ 417184610Salfred#if 0 418184610Salfred struct rue_softc *sc = device_get_softc(dev); 419184610Salfred struct mii_data *mii = GET_MII(sc); 420184610Salfred uint16_t bmcr; 421188412Sthompsa int locked; 422184610Salfred 423188412Sthompsa locked = mtx_owned(&sc->sc_mtx); 424188412Sthompsa if (!locked) 425188412Sthompsa RUE_LOCK(sc); 426184610Salfred 427188412Sthompsa RUE_CLRBIT(sc, RUE_CR, (RUE_CR_RE | RUE_CR_TE)); 428184610Salfred 429188412Sthompsa bmcr = rue_csr_read_2(sc, RUE_BMCR); 430184610Salfred 431184610Salfred if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX) 432184610Salfred bmcr |= RUE_BMCR_SPD_SET; 433184610Salfred else 434184610Salfred bmcr &= ~RUE_BMCR_SPD_SET; 435184610Salfred 436184610Salfred if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) 437184610Salfred bmcr |= RUE_BMCR_DUPLEX; 438184610Salfred else 439184610Salfred bmcr &= ~RUE_BMCR_DUPLEX; 440184610Salfred 441188412Sthompsa rue_csr_write_2(sc, RUE_BMCR, bmcr); 442184610Salfred 443188412Sthompsa RUE_SETBIT(sc, RUE_CR, (RUE_CR_RE | RUE_CR_TE)); 444184610Salfred 445188412Sthompsa if (!locked) 446188412Sthompsa RUE_UNLOCK(sc); 447184610Salfred#endif 448184610Salfred} 449184610Salfred 450184610Salfredstatic void 451192984Sthompsarue_setpromisc(struct usb_ether *ue) 452184610Salfred{ 453194228Sthompsa struct rue_softc *sc = uether_getsc(ue); 454194228Sthompsa struct ifnet *ifp = uether_getifp(ue); 455184610Salfred 456188412Sthompsa RUE_LOCK_ASSERT(sc, MA_OWNED); 457184610Salfred 458188412Sthompsa /* If we want promiscuous mode, set the allframes bit. */ 459188412Sthompsa if (ifp->if_flags & IFF_PROMISC) 460188412Sthompsa RUE_SETBIT(sc, RUE_RCR, RUE_RCR_AAP); 461188412Sthompsa else 462188412Sthompsa RUE_CLRBIT(sc, RUE_RCR, RUE_RCR_AAP); 463184610Salfred} 464184610Salfred 465184610Salfred/* 466184610Salfred * Program the 64-bit multicast hash filter. 467184610Salfred */ 468184610Salfredstatic void 469192984Sthompsarue_setmulti(struct usb_ether *ue) 470184610Salfred{ 471194228Sthompsa struct rue_softc *sc = uether_getsc(ue); 472194228Sthompsa struct ifnet *ifp = uether_getifp(ue); 473184610Salfred uint16_t rxcfg; 474188412Sthompsa int h = 0; 475188412Sthompsa uint32_t hashes[2] = { 0, 0 }; 476188412Sthompsa struct ifmultiaddr *ifma; 477188412Sthompsa int mcnt = 0; 478184610Salfred 479188412Sthompsa RUE_LOCK_ASSERT(sc, MA_OWNED); 480184610Salfred 481188412Sthompsa rxcfg = rue_csr_read_2(sc, RUE_RCR); 482188412Sthompsa 483188412Sthompsa if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) { 484184610Salfred rxcfg |= (RUE_RCR_AAM | RUE_RCR_AAP); 485184610Salfred rxcfg &= ~RUE_RCR_AM; 486188412Sthompsa rue_csr_write_2(sc, RUE_RCR, rxcfg); 487188412Sthompsa rue_csr_write_4(sc, RUE_MAR0, 0xFFFFFFFF); 488188412Sthompsa rue_csr_write_4(sc, RUE_MAR4, 0xFFFFFFFF); 489184610Salfred return; 490184610Salfred } 491184610Salfred 492188412Sthompsa /* first, zot all the existing hash bits */ 493188412Sthompsa rue_csr_write_4(sc, RUE_MAR0, 0); 494188412Sthompsa rue_csr_write_4(sc, RUE_MAR4, 0); 495188412Sthompsa 496188412Sthompsa /* now program new ones */ 497195049Srwatson if_maddr_rlock(ifp); 498188412Sthompsa TAILQ_FOREACH (ifma, &ifp->if_multiaddrs, ifma_link) 499188412Sthompsa { 500188412Sthompsa if (ifma->ifma_addr->sa_family != AF_LINK) 501188412Sthompsa continue; 502188412Sthompsa h = ether_crc32_be(LLADDR((struct sockaddr_dl *) 503188412Sthompsa ifma->ifma_addr), ETHER_ADDR_LEN) >> 26; 504188412Sthompsa if (h < 32) 505188412Sthompsa hashes[0] |= (1 << h); 506188412Sthompsa else 507188412Sthompsa hashes[1] |= (1 << (h - 32)); 508188412Sthompsa mcnt++; 509188412Sthompsa } 510195049Srwatson if_maddr_runlock(ifp); 511188412Sthompsa 512188412Sthompsa if (mcnt) 513184610Salfred rxcfg |= RUE_RCR_AM; 514184610Salfred else 515184610Salfred rxcfg &= ~RUE_RCR_AM; 516184610Salfred 517184610Salfred rxcfg &= ~(RUE_RCR_AAM | RUE_RCR_AAP); 518184610Salfred 519188412Sthompsa rue_csr_write_2(sc, RUE_RCR, rxcfg); 520188412Sthompsa rue_csr_write_4(sc, RUE_MAR0, hashes[0]); 521188412Sthompsa rue_csr_write_4(sc, RUE_MAR4, hashes[1]); 522184610Salfred} 523184610Salfred 524184610Salfredstatic void 525188412Sthompsarue_reset(struct rue_softc *sc) 526184610Salfred{ 527188412Sthompsa int i; 528184610Salfred 529188412Sthompsa rue_csr_write_1(sc, RUE_CR, RUE_CR_SOFT_RST); 530184610Salfred 531188412Sthompsa for (i = 0; i != RUE_TIMEOUT; i++) { 532194228Sthompsa if (uether_pause(&sc->sc_ue, hz / 1000)) 533188412Sthompsa break; 534188412Sthompsa if (!(rue_csr_read_1(sc, RUE_CR) & RUE_CR_SOFT_RST)) 535188412Sthompsa break; 536188412Sthompsa } 537188412Sthompsa if (i == RUE_TIMEOUT) 538188412Sthompsa device_printf(sc->sc_ue.ue_dev, "reset never completed!\n"); 539184610Salfred 540194228Sthompsa uether_pause(&sc->sc_ue, hz / 100); 541188412Sthompsa} 542184610Salfred 543188412Sthompsastatic void 544192984Sthompsarue_attach_post(struct usb_ether *ue) 545188412Sthompsa{ 546194228Sthompsa struct rue_softc *sc = uether_getsc(ue); 547184610Salfred 548188412Sthompsa /* reset the adapter */ 549188412Sthompsa rue_reset(sc); 550184610Salfred 551188412Sthompsa /* get station address from the EEPROM */ 552188412Sthompsa rue_read_mem(sc, RUE_EEPROM_IDR0, ue->ue_eaddr, ETHER_ADDR_LEN); 553184610Salfred} 554184610Salfred 555184610Salfred/* 556184610Salfred * Probe for a RTL8150 chip. 557184610Salfred */ 558184610Salfredstatic int 559184610Salfredrue_probe(device_t dev) 560184610Salfred{ 561192984Sthompsa struct usb_attach_arg *uaa = device_get_ivars(dev); 562184610Salfred 563192499Sthompsa if (uaa->usb_mode != USB_MODE_HOST) 564184610Salfred return (ENXIO); 565188412Sthompsa if (uaa->info.bConfigIndex != RUE_CONFIG_IDX) 566184610Salfred return (ENXIO); 567188412Sthompsa if (uaa->info.bIfaceIndex != RUE_IFACE_IDX) 568184610Salfred return (ENXIO); 569188412Sthompsa 570194228Sthompsa return (usbd_lookup_id_by_uaa(rue_devs, sizeof(rue_devs), uaa)); 571184610Salfred} 572184610Salfred 573184610Salfred/* 574184610Salfred * Attach the interface. Allocate softc structures, do ifmedia 575184610Salfred * setup and ethernet/BPF attach. 576184610Salfred */ 577184610Salfredstatic int 578184610Salfredrue_attach(device_t dev) 579184610Salfred{ 580192984Sthompsa struct usb_attach_arg *uaa = device_get_ivars(dev); 581184610Salfred struct rue_softc *sc = device_get_softc(dev); 582192984Sthompsa struct usb_ether *ue = &sc->sc_ue; 583184610Salfred uint8_t iface_index; 584188412Sthompsa int error; 585184610Salfred 586194228Sthompsa device_set_usb_desc(dev); 587188412Sthompsa mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF); 588184610Salfred 589184610Salfred iface_index = RUE_IFACE_IDX; 590194228Sthompsa error = usbd_transfer_setup(uaa->device, &iface_index, 591187259Sthompsa sc->sc_xfer, rue_config, RUE_N_TRANSFER, 592184610Salfred sc, &sc->sc_mtx); 593184610Salfred if (error) { 594188412Sthompsa device_printf(dev, "allocating USB transfers failed!\n"); 595184610Salfred goto detach; 596184610Salfred } 597188412Sthompsa 598188412Sthompsa ue->ue_sc = sc; 599188412Sthompsa ue->ue_dev = dev; 600188412Sthompsa ue->ue_udev = uaa->device; 601188412Sthompsa ue->ue_mtx = &sc->sc_mtx; 602188412Sthompsa ue->ue_methods = &rue_ue_methods; 603188412Sthompsa 604194228Sthompsa error = uether_ifattach(ue); 605184610Salfred if (error) { 606188412Sthompsa device_printf(dev, "could not attach interface\n"); 607184610Salfred goto detach; 608184610Salfred } 609184610Salfred return (0); /* success */ 610184610Salfred 611184610Salfreddetach: 612184610Salfred rue_detach(dev); 613184610Salfred return (ENXIO); /* failure */ 614184610Salfred} 615184610Salfred 616184610Salfredstatic int 617184610Salfredrue_detach(device_t dev) 618184610Salfred{ 619184610Salfred struct rue_softc *sc = device_get_softc(dev); 620192984Sthompsa struct usb_ether *ue = &sc->sc_ue; 621184610Salfred 622194228Sthompsa usbd_transfer_unsetup(sc->sc_xfer, RUE_N_TRANSFER); 623194228Sthompsa uether_ifdetach(ue); 624184610Salfred mtx_destroy(&sc->sc_mtx); 625184610Salfred 626184610Salfred return (0); 627184610Salfred} 628184610Salfred 629184610Salfredstatic void 630194677Sthompsarue_intr_callback(struct usb_xfer *xfer, usb_error_t error) 631184610Salfred{ 632194677Sthompsa struct rue_softc *sc = usbd_xfer_softc(xfer); 633194228Sthompsa struct ifnet *ifp = uether_getifp(&sc->sc_ue); 634184610Salfred struct rue_intrpkt pkt; 635194677Sthompsa struct usb_page_cache *pc; 636194677Sthompsa int actlen; 637184610Salfred 638194677Sthompsa usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 639194677Sthompsa 640184610Salfred switch (USB_GET_STATE(xfer)) { 641184610Salfred case USB_ST_TRANSFERRED: 642184610Salfred 643184610Salfred if (ifp && (ifp->if_drv_flags & IFF_DRV_RUNNING) && 644194677Sthompsa actlen >= sizeof(pkt)) { 645184610Salfred 646194677Sthompsa pc = usbd_xfer_get_frame(xfer, 0); 647194677Sthompsa usbd_copy_out(pc, 0, &pkt, sizeof(pkt)); 648184610Salfred 649184610Salfred ifp->if_ierrors += pkt.rue_rxlost_cnt; 650184610Salfred ifp->if_ierrors += pkt.rue_crcerr_cnt; 651184610Salfred ifp->if_collisions += pkt.rue_col_cnt; 652184610Salfred } 653188412Sthompsa /* FALLTHROUGH */ 654184610Salfred case USB_ST_SETUP: 655188412Sthompsatr_setup: 656194677Sthompsa usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 657194228Sthompsa usbd_transfer_submit(xfer); 658184610Salfred return; 659184610Salfred 660184610Salfred default: /* Error */ 661194677Sthompsa if (error != USB_ERR_CANCELLED) { 662188412Sthompsa /* try to clear stall first */ 663194677Sthompsa usbd_xfer_set_stall(xfer); 664188412Sthompsa goto tr_setup; 665184610Salfred } 666184610Salfred return; 667184610Salfred } 668184610Salfred} 669184610Salfred 670184610Salfredstatic void 671194677Sthompsarue_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error) 672184610Salfred{ 673194677Sthompsa struct rue_softc *sc = usbd_xfer_softc(xfer); 674192984Sthompsa struct usb_ether *ue = &sc->sc_ue; 675194228Sthompsa struct ifnet *ifp = uether_getifp(ue); 676194677Sthompsa struct usb_page_cache *pc; 677184610Salfred uint16_t status; 678194677Sthompsa int actlen; 679184610Salfred 680194677Sthompsa usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 681194677Sthompsa 682184610Salfred switch (USB_GET_STATE(xfer)) { 683184610Salfred case USB_ST_TRANSFERRED: 684184610Salfred 685194677Sthompsa if (actlen < 4) { 686184610Salfred ifp->if_ierrors++; 687184610Salfred goto tr_setup; 688184610Salfred } 689194677Sthompsa pc = usbd_xfer_get_frame(xfer, 0); 690194677Sthompsa usbd_copy_out(pc, actlen - 4, &status, sizeof(status)); 691194677Sthompsa actlen -= 4; 692184610Salfred 693188412Sthompsa /* check recieve packet was valid or not */ 694184610Salfred status = le16toh(status); 695184610Salfred if ((status & RUE_RXSTAT_VALID) == 0) { 696184610Salfred ifp->if_ierrors++; 697184610Salfred goto tr_setup; 698184610Salfred } 699194677Sthompsa uether_rxbuf(ue, pc, 0, actlen); 700188412Sthompsa /* FALLTHROUGH */ 701184610Salfred case USB_ST_SETUP: 702184610Salfredtr_setup: 703194677Sthompsa usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 704194228Sthompsa usbd_transfer_submit(xfer); 705194228Sthompsa uether_rxflush(ue); 706184610Salfred return; 707184610Salfred 708184610Salfred default: /* Error */ 709188412Sthompsa DPRINTF("bulk read error, %s\n", 710194677Sthompsa usbd_errstr(error)); 711188412Sthompsa 712194677Sthompsa if (error != USB_ERR_CANCELLED) { 713184610Salfred /* try to clear stall first */ 714194677Sthompsa usbd_xfer_set_stall(xfer); 715188412Sthompsa goto tr_setup; 716184610Salfred } 717184610Salfred return; 718184610Salfred } 719184610Salfred} 720184610Salfred 721184610Salfredstatic void 722194677Sthompsarue_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error) 723184610Salfred{ 724194677Sthompsa struct rue_softc *sc = usbd_xfer_softc(xfer); 725194228Sthompsa struct ifnet *ifp = uether_getifp(&sc->sc_ue); 726194677Sthompsa struct usb_page_cache *pc; 727184610Salfred struct mbuf *m; 728188412Sthompsa int temp_len; 729184610Salfred 730184610Salfred switch (USB_GET_STATE(xfer)) { 731184610Salfred case USB_ST_TRANSFERRED: 732184610Salfred DPRINTFN(11, "transfer complete\n"); 733184610Salfred ifp->if_opackets++; 734184610Salfred 735188412Sthompsa /* FALLTHROUGH */ 736184610Salfred case USB_ST_SETUP: 737188412Sthompsatr_setup: 738188412Sthompsa if ((sc->sc_flags & RUE_FLAG_LINK) == 0) { 739184610Salfred /* 740184610Salfred * don't send anything if there is no link ! 741184610Salfred */ 742188412Sthompsa return; 743184610Salfred } 744184610Salfred IFQ_DRV_DEQUEUE(&ifp->if_snd, m); 745184610Salfred 746188412Sthompsa if (m == NULL) 747188412Sthompsa return; 748188412Sthompsa if (m->m_pkthdr.len > MCLBYTES) 749184610Salfred m->m_pkthdr.len = MCLBYTES; 750184610Salfred temp_len = m->m_pkthdr.len; 751184610Salfred 752194677Sthompsa pc = usbd_xfer_get_frame(xfer, 0); 753194677Sthompsa usbd_m_copy_in(pc, 0, m, 0, m->m_pkthdr.len); 754184610Salfred 755184610Salfred /* 756184610Salfred * This is an undocumented behavior. 757184610Salfred * RTL8150 chip doesn't send frame length smaller than 758184610Salfred * RUE_MIN_FRAMELEN (60) byte packet. 759184610Salfred */ 760184610Salfred if (temp_len < RUE_MIN_FRAMELEN) { 761194677Sthompsa usbd_frame_zero(pc, temp_len, 762184610Salfred RUE_MIN_FRAMELEN - temp_len); 763184610Salfred temp_len = RUE_MIN_FRAMELEN; 764184610Salfred } 765194677Sthompsa usbd_xfer_set_frame_len(xfer, 0, temp_len); 766184610Salfred 767184610Salfred /* 768184610Salfred * if there's a BPF listener, bounce a copy 769184610Salfred * of this frame to him: 770184610Salfred */ 771184610Salfred BPF_MTAP(ifp, m); 772184610Salfred 773184610Salfred m_freem(m); 774184610Salfred 775194228Sthompsa usbd_transfer_submit(xfer); 776184610Salfred 777184610Salfred return; 778184610Salfred 779184610Salfred default: /* Error */ 780184610Salfred DPRINTFN(11, "transfer error, %s\n", 781194677Sthompsa usbd_errstr(error)); 782184610Salfred 783188412Sthompsa ifp->if_oerrors++; 784188412Sthompsa 785194677Sthompsa if (error != USB_ERR_CANCELLED) { 786184610Salfred /* try to clear stall first */ 787194677Sthompsa usbd_xfer_set_stall(xfer); 788188412Sthompsa goto tr_setup; 789184610Salfred } 790184610Salfred return; 791184610Salfred } 792184610Salfred} 793184610Salfred 794184610Salfredstatic void 795192984Sthompsarue_tick(struct usb_ether *ue) 796184610Salfred{ 797194228Sthompsa struct rue_softc *sc = uether_getsc(ue); 798184610Salfred struct mii_data *mii = GET_MII(sc); 799184610Salfred 800188412Sthompsa RUE_LOCK_ASSERT(sc, MA_OWNED); 801188412Sthompsa 802184610Salfred mii_tick(mii); 803188412Sthompsa if ((sc->sc_flags & RUE_FLAG_LINK) == 0 804188412Sthompsa && mii->mii_media_status & IFM_ACTIVE && 805188412Sthompsa IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) { 806188412Sthompsa sc->sc_flags |= RUE_FLAG_LINK; 807188412Sthompsa rue_start(ue); 808184610Salfred } 809184610Salfred} 810184610Salfred 811184610Salfredstatic void 812192984Sthompsarue_start(struct usb_ether *ue) 813184610Salfred{ 814194228Sthompsa struct rue_softc *sc = uether_getsc(ue); 815184610Salfred 816188412Sthompsa /* 817188412Sthompsa * start the USB transfers, if not already started: 818188412Sthompsa */ 819194228Sthompsa usbd_transfer_start(sc->sc_xfer[RUE_INTR_DT_RD]); 820194228Sthompsa usbd_transfer_start(sc->sc_xfer[RUE_BULK_DT_RD]); 821194228Sthompsa usbd_transfer_start(sc->sc_xfer[RUE_BULK_DT_WR]); 822184610Salfred} 823184610Salfred 824184610Salfredstatic void 825192984Sthompsarue_init(struct usb_ether *ue) 826184610Salfred{ 827194228Sthompsa struct rue_softc *sc = uether_getsc(ue); 828194228Sthompsa struct ifnet *ifp = uether_getifp(ue); 829184610Salfred 830188412Sthompsa RUE_LOCK_ASSERT(sc, MA_OWNED); 831184610Salfred 832184610Salfred /* 833184610Salfred * Cancel pending I/O 834184610Salfred */ 835188412Sthompsa rue_reset(sc); 836184610Salfred 837188412Sthompsa /* Set MAC address */ 838188412Sthompsa rue_write_mem(sc, RUE_IDR0, IF_LLADDR(ifp), ETHER_ADDR_LEN); 839184610Salfred 840188412Sthompsa rue_stop(ue); 841184610Salfred 842184610Salfred /* 843184610Salfred * Set the initial TX and RX configuration. 844184610Salfred */ 845188412Sthompsa rue_csr_write_1(sc, RUE_TCR, RUE_TCR_CONFIG); 846188412Sthompsa rue_csr_write_2(sc, RUE_RCR, RUE_RCR_CONFIG|RUE_RCR_AB); 847184610Salfred 848184610Salfred /* Load the multicast filter */ 849188412Sthompsa rue_setpromisc(ue); 850188412Sthompsa /* Load the multicast filter. */ 851188412Sthompsa rue_setmulti(ue); 852184610Salfred 853184610Salfred /* Enable RX and TX */ 854188412Sthompsa rue_csr_write_1(sc, RUE_CR, (RUE_CR_TE | RUE_CR_RE | RUE_CR_EP3CLREN)); 855184610Salfred 856194677Sthompsa usbd_xfer_set_stall(sc->sc_xfer[RUE_BULK_DT_WR]); 857184610Salfred 858188412Sthompsa ifp->if_drv_flags |= IFF_DRV_RUNNING; 859188412Sthompsa rue_start(ue); 860184610Salfred} 861184610Salfred 862184610Salfred/* 863184610Salfred * Set media options. 864184610Salfred */ 865184610Salfredstatic int 866188412Sthompsarue_ifmedia_upd(struct ifnet *ifp) 867184610Salfred{ 868184610Salfred struct rue_softc *sc = ifp->if_softc; 869184610Salfred struct mii_data *mii = GET_MII(sc); 870184610Salfred 871188412Sthompsa RUE_LOCK_ASSERT(sc, MA_OWNED); 872184610Salfred 873188412Sthompsa sc->sc_flags &= ~RUE_FLAG_LINK; 874184610Salfred if (mii->mii_instance) { 875184610Salfred struct mii_softc *miisc; 876184610Salfred 877188412Sthompsa LIST_FOREACH(miisc, &mii->mii_phys, mii_list) 878184610Salfred mii_phy_reset(miisc); 879184610Salfred } 880184610Salfred mii_mediachg(mii); 881188412Sthompsa return (0); 882184610Salfred} 883184610Salfred 884184610Salfred/* 885184610Salfred * Report current media status. 886184610Salfred */ 887184610Salfredstatic void 888188412Sthompsarue_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr) 889184610Salfred{ 890184610Salfred struct rue_softc *sc = ifp->if_softc; 891188412Sthompsa struct mii_data *mii = GET_MII(sc); 892184610Salfred 893188412Sthompsa RUE_LOCK(sc); 894188412Sthompsa mii_pollstat(mii); 895188412Sthompsa RUE_UNLOCK(sc); 896188412Sthompsa ifmr->ifm_active = mii->mii_media_active; 897188412Sthompsa ifmr->ifm_status = mii->mii_media_status; 898184610Salfred} 899184610Salfred 900184610Salfredstatic void 901192984Sthompsarue_stop(struct usb_ether *ue) 902184610Salfred{ 903194228Sthompsa struct rue_softc *sc = uether_getsc(ue); 904194228Sthompsa struct ifnet *ifp = uether_getifp(ue); 905184610Salfred 906188412Sthompsa RUE_LOCK_ASSERT(sc, MA_OWNED); 907184610Salfred 908188412Sthompsa ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 909188412Sthompsa sc->sc_flags &= ~RUE_FLAG_LINK; 910184610Salfred 911184610Salfred /* 912184610Salfred * stop all the transfers, if not already stopped: 913184610Salfred */ 914194228Sthompsa usbd_transfer_stop(sc->sc_xfer[RUE_BULK_DT_WR]); 915194228Sthompsa usbd_transfer_stop(sc->sc_xfer[RUE_BULK_DT_RD]); 916194228Sthompsa usbd_transfer_stop(sc->sc_xfer[RUE_INTR_DT_RD]); 917184610Salfred 918188412Sthompsa rue_csr_write_1(sc, RUE_CR, 0x00); 919184610Salfred 920188412Sthompsa rue_reset(sc); 921184610Salfred} 922