if_rue.c revision 233774
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 233774 2012-04-02 10:50:42Z hselasky $"); 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/module.h> 77194677Sthompsa#include <sys/lock.h> 78194677Sthompsa#include <sys/mutex.h> 79194677Sthompsa#include <sys/condvar.h> 80194677Sthompsa#include <sys/sysctl.h> 81194677Sthompsa#include <sys/sx.h> 82194677Sthompsa#include <sys/unistd.h> 83194677Sthompsa#include <sys/callout.h> 84194677Sthompsa#include <sys/malloc.h> 85194677Sthompsa#include <sys/priv.h> 86194677Sthompsa 87194677Sthompsa#include <dev/usb/usb.h> 88194677Sthompsa#include <dev/usb/usbdi.h> 89194677Sthompsa#include <dev/usb/usbdi_util.h> 90188746Sthompsa#include "usbdevs.h" 91184610Salfred 92184610Salfred#define USB_DEBUG_VAR rue_debug 93194677Sthompsa#include <dev/usb/usb_debug.h> 94188942Sthompsa#include <dev/usb/usb_process.h> 95184610Salfred 96188942Sthompsa#include <dev/usb/net/usb_ethernet.h> 97188942Sthompsa#include <dev/usb/net/if_ruereg.h> 98184610Salfred 99207077Sthompsa#ifdef USB_DEBUG 100184610Salfredstatic int rue_debug = 0; 101184610Salfred 102227309Sedstatic SYSCTL_NODE(_hw_usb, OID_AUTO, rue, CTLFLAG_RW, 0, "USB rue"); 103192502SthompsaSYSCTL_INT(_hw_usb_rue, OID_AUTO, debug, CTLFLAG_RW, 104184610Salfred &rue_debug, 0, "Debug level"); 105184610Salfred#endif 106184610Salfred 107184610Salfred/* 108184610Salfred * Various supported device vendors/products. 109184610Salfred */ 110184610Salfred 111223486Shselaskystatic const STRUCT_USB_HOST_ID rue_devs[] = { 112184610Salfred {USB_VPI(USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUAKTX, 0)}, 113184610Salfred {USB_VPI(USB_VENDOR_REALTEK, USB_PRODUCT_REALTEK_USBKR100, 0)}, 114209444Sthompsa {USB_VPI(USB_VENDOR_OQO, USB_PRODUCT_OQO_ETHER01, 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 /* MII interface */ 190188412Sthompsa DEVMETHOD(miibus_readreg, rue_miibus_readreg), 191188412Sthompsa DEVMETHOD(miibus_writereg, rue_miibus_writereg), 192188412Sthompsa DEVMETHOD(miibus_statchg, rue_miibus_statchg), 193184610Salfred 194227843Smarius DEVMETHOD_END 195184610Salfred}; 196184610Salfred 197184610Salfredstatic driver_t rue_driver = { 198184610Salfred .name = "rue", 199184610Salfred .methods = rue_methods, 200184610Salfred .size = sizeof(struct rue_softc), 201184610Salfred}; 202184610Salfred 203184610Salfredstatic devclass_t rue_devclass; 204184610Salfred 205189275SthompsaDRIVER_MODULE(rue, uhub, rue_driver, rue_devclass, NULL, 0); 206184610SalfredDRIVER_MODULE(miibus, rue, miibus_driver, miibus_devclass, 0, 0); 207188942SthompsaMODULE_DEPEND(rue, uether, 1, 1, 1); 208188942SthompsaMODULE_DEPEND(rue, usb, 1, 1, 1); 209184610SalfredMODULE_DEPEND(rue, ether, 1, 1, 1); 210184610SalfredMODULE_DEPEND(rue, miibus, 1, 1, 1); 211212122SthompsaMODULE_VERSION(rue, 1); 212184610Salfred 213192984Sthompsastatic const struct usb_ether_methods rue_ue_methods = { 214188412Sthompsa .ue_attach_post = rue_attach_post, 215188412Sthompsa .ue_start = rue_start, 216188412Sthompsa .ue_init = rue_init, 217188412Sthompsa .ue_stop = rue_stop, 218188412Sthompsa .ue_tick = rue_tick, 219188412Sthompsa .ue_setmulti = rue_setmulti, 220188412Sthompsa .ue_setpromisc = rue_setpromisc, 221188412Sthompsa .ue_mii_upd = rue_ifmedia_upd, 222188412Sthompsa .ue_mii_sts = rue_ifmedia_sts, 223188412Sthompsa}; 224184610Salfred 225188412Sthompsa#define RUE_SETBIT(sc, reg, x) \ 226188412Sthompsa rue_csr_write_1(sc, reg, rue_csr_read_1(sc, reg) | (x)) 227184610Salfred 228188412Sthompsa#define RUE_CLRBIT(sc, reg, x) \ 229188412Sthompsa rue_csr_write_1(sc, reg, rue_csr_read_1(sc, reg) & ~(x)) 230184610Salfred 231188412Sthompsastatic int 232188412Sthompsarue_read_mem(struct rue_softc *sc, uint16_t addr, void *buf, int len) 233184610Salfred{ 234192984Sthompsa struct usb_device_request req; 235184610Salfred 236184610Salfred req.bmRequestType = UT_READ_VENDOR_DEVICE; 237184610Salfred req.bRequest = UR_SET_ADDRESS; 238184610Salfred USETW(req.wValue, addr); 239184610Salfred USETW(req.wIndex, 0); 240184610Salfred USETW(req.wLength, len); 241184610Salfred 242194228Sthompsa return (uether_do_request(&sc->sc_ue, &req, buf, 1000)); 243184610Salfred} 244184610Salfred 245188412Sthompsastatic int 246188412Sthompsarue_write_mem(struct rue_softc *sc, uint16_t addr, void *buf, int len) 247184610Salfred{ 248192984Sthompsa struct usb_device_request req; 249184610Salfred 250184610Salfred req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 251184610Salfred req.bRequest = UR_SET_ADDRESS; 252184610Salfred USETW(req.wValue, addr); 253184610Salfred USETW(req.wIndex, 0); 254184610Salfred USETW(req.wLength, len); 255184610Salfred 256194228Sthompsa return (uether_do_request(&sc->sc_ue, &req, buf, 1000)); 257184610Salfred} 258184610Salfred 259184610Salfredstatic uint8_t 260188412Sthompsarue_csr_read_1(struct rue_softc *sc, uint16_t reg) 261184610Salfred{ 262184610Salfred uint8_t val; 263184610Salfred 264188412Sthompsa rue_read_mem(sc, reg, &val, 1); 265184610Salfred return (val); 266184610Salfred} 267184610Salfred 268184610Salfredstatic uint16_t 269188412Sthompsarue_csr_read_2(struct rue_softc *sc, uint16_t reg) 270184610Salfred{ 271184610Salfred uint8_t val[2]; 272184610Salfred 273188412Sthompsa rue_read_mem(sc, reg, &val, 2); 274184610Salfred return (UGETW(val)); 275184610Salfred} 276184610Salfred 277188412Sthompsastatic int 278188412Sthompsarue_csr_write_1(struct rue_softc *sc, uint16_t reg, uint8_t val) 279184610Salfred{ 280188412Sthompsa return (rue_write_mem(sc, reg, &val, 1)); 281184610Salfred} 282184610Salfred 283188412Sthompsastatic int 284188412Sthompsarue_csr_write_2(struct rue_softc *sc, uint16_t reg, uint16_t val) 285184610Salfred{ 286184610Salfred uint8_t temp[2]; 287184610Salfred 288184610Salfred USETW(temp, val); 289188412Sthompsa return (rue_write_mem(sc, reg, &temp, 2)); 290184610Salfred} 291184610Salfred 292188412Sthompsastatic int 293188412Sthompsarue_csr_write_4(struct rue_softc *sc, int reg, uint32_t val) 294184610Salfred{ 295184610Salfred uint8_t temp[4]; 296184610Salfred 297184610Salfred USETDW(temp, val); 298188412Sthompsa return (rue_write_mem(sc, reg, &temp, 4)); 299184610Salfred} 300184610Salfred 301184610Salfredstatic int 302188412Sthompsarue_miibus_readreg(device_t dev, int phy, int reg) 303184610Salfred{ 304184610Salfred struct rue_softc *sc = device_get_softc(dev); 305184610Salfred uint16_t rval; 306184610Salfred uint16_t ruereg; 307188412Sthompsa int locked; 308184610Salfred 309188412Sthompsa if (phy != 0) /* RTL8150 supports PHY == 0, only */ 310184610Salfred return (0); 311184610Salfred 312188412Sthompsa locked = mtx_owned(&sc->sc_mtx); 313188412Sthompsa if (!locked) 314188412Sthompsa RUE_LOCK(sc); 315188412Sthompsa 316184610Salfred switch (reg) { 317184610Salfred case MII_BMCR: 318184610Salfred ruereg = RUE_BMCR; 319184610Salfred break; 320184610Salfred case MII_BMSR: 321184610Salfred ruereg = RUE_BMSR; 322184610Salfred break; 323184610Salfred case MII_ANAR: 324184610Salfred ruereg = RUE_ANAR; 325184610Salfred break; 326184610Salfred case MII_ANER: 327184610Salfred ruereg = RUE_AER; 328184610Salfred break; 329184610Salfred case MII_ANLPAR: 330184610Salfred ruereg = RUE_ANLP; 331184610Salfred break; 332184610Salfred case MII_PHYIDR1: 333184610Salfred case MII_PHYIDR2: 334184610Salfred rval = 0; 335184610Salfred goto done; 336184610Salfred default: 337188412Sthompsa if (RUE_REG_MIN <= reg && reg <= RUE_REG_MAX) { 338188412Sthompsa rval = rue_csr_read_1(sc, reg); 339184610Salfred goto done; 340184610Salfred } 341188412Sthompsa device_printf(sc->sc_ue.ue_dev, "bad phy register\n"); 342184610Salfred rval = 0; 343184610Salfred goto done; 344184610Salfred } 345184610Salfred 346188412Sthompsa rval = rue_csr_read_2(sc, ruereg); 347184610Salfreddone: 348188412Sthompsa if (!locked) 349188412Sthompsa RUE_UNLOCK(sc); 350184610Salfred return (rval); 351184610Salfred} 352184610Salfred 353184610Salfredstatic int 354188412Sthompsarue_miibus_writereg(device_t dev, int phy, int reg, int data) 355184610Salfred{ 356184610Salfred struct rue_softc *sc = device_get_softc(dev); 357184610Salfred uint16_t ruereg; 358188412Sthompsa int locked; 359184610Salfred 360188412Sthompsa if (phy != 0) /* RTL8150 supports PHY == 0, only */ 361184610Salfred return (0); 362184610Salfred 363188412Sthompsa locked = mtx_owned(&sc->sc_mtx); 364188412Sthompsa if (!locked) 365188412Sthompsa RUE_LOCK(sc); 366188412Sthompsa 367184610Salfred switch (reg) { 368184610Salfred case MII_BMCR: 369184610Salfred ruereg = RUE_BMCR; 370184610Salfred break; 371184610Salfred case MII_BMSR: 372184610Salfred ruereg = RUE_BMSR; 373184610Salfred break; 374184610Salfred case MII_ANAR: 375184610Salfred ruereg = RUE_ANAR; 376184610Salfred break; 377184610Salfred case MII_ANER: 378184610Salfred ruereg = RUE_AER; 379184610Salfred break; 380184610Salfred case MII_ANLPAR: 381184610Salfred ruereg = RUE_ANLP; 382184610Salfred break; 383184610Salfred case MII_PHYIDR1: 384184610Salfred case MII_PHYIDR2: 385184610Salfred goto done; 386184610Salfred default: 387188412Sthompsa if (RUE_REG_MIN <= reg && reg <= RUE_REG_MAX) { 388188412Sthompsa rue_csr_write_1(sc, reg, data); 389184610Salfred goto done; 390184610Salfred } 391188412Sthompsa device_printf(sc->sc_ue.ue_dev, " bad phy register\n"); 392184610Salfred goto done; 393184610Salfred } 394188412Sthompsa rue_csr_write_2(sc, ruereg, data); 395184610Salfreddone: 396188412Sthompsa if (!locked) 397188412Sthompsa RUE_UNLOCK(sc); 398184610Salfred return (0); 399184610Salfred} 400184610Salfred 401184610Salfredstatic void 402188412Sthompsarue_miibus_statchg(device_t dev) 403184610Salfred{ 404184610Salfred /* 405184610Salfred * When the code below is enabled the card starts doing weird 406184610Salfred * things after link going from UP to DOWN and back UP. 407184610Salfred * 408184610Salfred * Looks like some of register writes below messes up PHY 409184610Salfred * interface. 410184610Salfred * 411184610Salfred * No visible regressions were found after commenting this code 412184610Salfred * out, so that disable it for good. 413184610Salfred */ 414184610Salfred#if 0 415184610Salfred struct rue_softc *sc = device_get_softc(dev); 416184610Salfred struct mii_data *mii = GET_MII(sc); 417184610Salfred uint16_t bmcr; 418188412Sthompsa int locked; 419184610Salfred 420188412Sthompsa locked = mtx_owned(&sc->sc_mtx); 421188412Sthompsa if (!locked) 422188412Sthompsa RUE_LOCK(sc); 423184610Salfred 424188412Sthompsa RUE_CLRBIT(sc, RUE_CR, (RUE_CR_RE | RUE_CR_TE)); 425184610Salfred 426188412Sthompsa bmcr = rue_csr_read_2(sc, RUE_BMCR); 427184610Salfred 428184610Salfred if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX) 429184610Salfred bmcr |= RUE_BMCR_SPD_SET; 430184610Salfred else 431184610Salfred bmcr &= ~RUE_BMCR_SPD_SET; 432184610Salfred 433184610Salfred if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) 434184610Salfred bmcr |= RUE_BMCR_DUPLEX; 435184610Salfred else 436184610Salfred bmcr &= ~RUE_BMCR_DUPLEX; 437184610Salfred 438188412Sthompsa rue_csr_write_2(sc, RUE_BMCR, bmcr); 439184610Salfred 440188412Sthompsa RUE_SETBIT(sc, RUE_CR, (RUE_CR_RE | RUE_CR_TE)); 441184610Salfred 442188412Sthompsa if (!locked) 443188412Sthompsa RUE_UNLOCK(sc); 444184610Salfred#endif 445184610Salfred} 446184610Salfred 447184610Salfredstatic void 448192984Sthompsarue_setpromisc(struct usb_ether *ue) 449184610Salfred{ 450194228Sthompsa struct rue_softc *sc = uether_getsc(ue); 451194228Sthompsa struct ifnet *ifp = uether_getifp(ue); 452184610Salfred 453188412Sthompsa RUE_LOCK_ASSERT(sc, MA_OWNED); 454184610Salfred 455188412Sthompsa /* If we want promiscuous mode, set the allframes bit. */ 456188412Sthompsa if (ifp->if_flags & IFF_PROMISC) 457188412Sthompsa RUE_SETBIT(sc, RUE_RCR, RUE_RCR_AAP); 458188412Sthompsa else 459188412Sthompsa RUE_CLRBIT(sc, RUE_RCR, RUE_RCR_AAP); 460184610Salfred} 461184610Salfred 462184610Salfred/* 463184610Salfred * Program the 64-bit multicast hash filter. 464184610Salfred */ 465184610Salfredstatic void 466192984Sthompsarue_setmulti(struct usb_ether *ue) 467184610Salfred{ 468194228Sthompsa struct rue_softc *sc = uether_getsc(ue); 469194228Sthompsa struct ifnet *ifp = uether_getifp(ue); 470184610Salfred uint16_t rxcfg; 471188412Sthompsa int h = 0; 472188412Sthompsa uint32_t hashes[2] = { 0, 0 }; 473188412Sthompsa struct ifmultiaddr *ifma; 474188412Sthompsa int mcnt = 0; 475184610Salfred 476188412Sthompsa RUE_LOCK_ASSERT(sc, MA_OWNED); 477184610Salfred 478188412Sthompsa rxcfg = rue_csr_read_2(sc, RUE_RCR); 479188412Sthompsa 480188412Sthompsa if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) { 481184610Salfred rxcfg |= (RUE_RCR_AAM | RUE_RCR_AAP); 482184610Salfred rxcfg &= ~RUE_RCR_AM; 483188412Sthompsa rue_csr_write_2(sc, RUE_RCR, rxcfg); 484188412Sthompsa rue_csr_write_4(sc, RUE_MAR0, 0xFFFFFFFF); 485188412Sthompsa rue_csr_write_4(sc, RUE_MAR4, 0xFFFFFFFF); 486184610Salfred return; 487184610Salfred } 488184610Salfred 489188412Sthompsa /* first, zot all the existing hash bits */ 490188412Sthompsa rue_csr_write_4(sc, RUE_MAR0, 0); 491188412Sthompsa rue_csr_write_4(sc, RUE_MAR4, 0); 492188412Sthompsa 493188412Sthompsa /* now program new ones */ 494195049Srwatson if_maddr_rlock(ifp); 495188412Sthompsa TAILQ_FOREACH (ifma, &ifp->if_multiaddrs, ifma_link) 496188412Sthompsa { 497188412Sthompsa if (ifma->ifma_addr->sa_family != AF_LINK) 498188412Sthompsa continue; 499188412Sthompsa h = ether_crc32_be(LLADDR((struct sockaddr_dl *) 500188412Sthompsa ifma->ifma_addr), ETHER_ADDR_LEN) >> 26; 501188412Sthompsa if (h < 32) 502188412Sthompsa hashes[0] |= (1 << h); 503188412Sthompsa else 504188412Sthompsa hashes[1] |= (1 << (h - 32)); 505188412Sthompsa mcnt++; 506188412Sthompsa } 507195049Srwatson if_maddr_runlock(ifp); 508188412Sthompsa 509188412Sthompsa if (mcnt) 510184610Salfred rxcfg |= RUE_RCR_AM; 511184610Salfred else 512184610Salfred rxcfg &= ~RUE_RCR_AM; 513184610Salfred 514184610Salfred rxcfg &= ~(RUE_RCR_AAM | RUE_RCR_AAP); 515184610Salfred 516188412Sthompsa rue_csr_write_2(sc, RUE_RCR, rxcfg); 517188412Sthompsa rue_csr_write_4(sc, RUE_MAR0, hashes[0]); 518188412Sthompsa rue_csr_write_4(sc, RUE_MAR4, hashes[1]); 519184610Salfred} 520184610Salfred 521184610Salfredstatic void 522188412Sthompsarue_reset(struct rue_softc *sc) 523184610Salfred{ 524188412Sthompsa int i; 525184610Salfred 526188412Sthompsa rue_csr_write_1(sc, RUE_CR, RUE_CR_SOFT_RST); 527184610Salfred 528188412Sthompsa for (i = 0; i != RUE_TIMEOUT; i++) { 529194228Sthompsa if (uether_pause(&sc->sc_ue, hz / 1000)) 530188412Sthompsa break; 531188412Sthompsa if (!(rue_csr_read_1(sc, RUE_CR) & RUE_CR_SOFT_RST)) 532188412Sthompsa break; 533188412Sthompsa } 534188412Sthompsa if (i == RUE_TIMEOUT) 535199816Sthompsa device_printf(sc->sc_ue.ue_dev, "reset never completed\n"); 536184610Salfred 537194228Sthompsa uether_pause(&sc->sc_ue, hz / 100); 538188412Sthompsa} 539184610Salfred 540188412Sthompsastatic void 541192984Sthompsarue_attach_post(struct usb_ether *ue) 542188412Sthompsa{ 543194228Sthompsa struct rue_softc *sc = uether_getsc(ue); 544184610Salfred 545188412Sthompsa /* reset the adapter */ 546188412Sthompsa rue_reset(sc); 547184610Salfred 548188412Sthompsa /* get station address from the EEPROM */ 549188412Sthompsa rue_read_mem(sc, RUE_EEPROM_IDR0, ue->ue_eaddr, ETHER_ADDR_LEN); 550184610Salfred} 551184610Salfred 552184610Salfred/* 553184610Salfred * Probe for a RTL8150 chip. 554184610Salfred */ 555184610Salfredstatic int 556184610Salfredrue_probe(device_t dev) 557184610Salfred{ 558192984Sthompsa struct usb_attach_arg *uaa = device_get_ivars(dev); 559184610Salfred 560192499Sthompsa if (uaa->usb_mode != USB_MODE_HOST) 561184610Salfred return (ENXIO); 562188412Sthompsa if (uaa->info.bConfigIndex != RUE_CONFIG_IDX) 563184610Salfred return (ENXIO); 564188412Sthompsa if (uaa->info.bIfaceIndex != RUE_IFACE_IDX) 565184610Salfred return (ENXIO); 566188412Sthompsa 567194228Sthompsa return (usbd_lookup_id_by_uaa(rue_devs, sizeof(rue_devs), uaa)); 568184610Salfred} 569184610Salfred 570184610Salfred/* 571184610Salfred * Attach the interface. Allocate softc structures, do ifmedia 572184610Salfred * setup and ethernet/BPF attach. 573184610Salfred */ 574184610Salfredstatic int 575184610Salfredrue_attach(device_t dev) 576184610Salfred{ 577192984Sthompsa struct usb_attach_arg *uaa = device_get_ivars(dev); 578184610Salfred struct rue_softc *sc = device_get_softc(dev); 579192984Sthompsa struct usb_ether *ue = &sc->sc_ue; 580184610Salfred uint8_t iface_index; 581188412Sthompsa int error; 582184610Salfred 583194228Sthompsa device_set_usb_desc(dev); 584188412Sthompsa mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF); 585184610Salfred 586184610Salfred iface_index = RUE_IFACE_IDX; 587194228Sthompsa error = usbd_transfer_setup(uaa->device, &iface_index, 588187259Sthompsa sc->sc_xfer, rue_config, RUE_N_TRANSFER, 589184610Salfred sc, &sc->sc_mtx); 590184610Salfred if (error) { 591199816Sthompsa device_printf(dev, "allocating USB transfers failed\n"); 592184610Salfred goto detach; 593184610Salfred } 594188412Sthompsa 595188412Sthompsa ue->ue_sc = sc; 596188412Sthompsa ue->ue_dev = dev; 597188412Sthompsa ue->ue_udev = uaa->device; 598188412Sthompsa ue->ue_mtx = &sc->sc_mtx; 599188412Sthompsa ue->ue_methods = &rue_ue_methods; 600188412Sthompsa 601194228Sthompsa error = uether_ifattach(ue); 602184610Salfred if (error) { 603188412Sthompsa device_printf(dev, "could not attach interface\n"); 604184610Salfred goto detach; 605184610Salfred } 606184610Salfred return (0); /* success */ 607184610Salfred 608184610Salfreddetach: 609184610Salfred rue_detach(dev); 610184610Salfred return (ENXIO); /* failure */ 611184610Salfred} 612184610Salfred 613184610Salfredstatic int 614184610Salfredrue_detach(device_t dev) 615184610Salfred{ 616184610Salfred struct rue_softc *sc = device_get_softc(dev); 617192984Sthompsa struct usb_ether *ue = &sc->sc_ue; 618184610Salfred 619194228Sthompsa usbd_transfer_unsetup(sc->sc_xfer, RUE_N_TRANSFER); 620194228Sthompsa uether_ifdetach(ue); 621184610Salfred mtx_destroy(&sc->sc_mtx); 622184610Salfred 623184610Salfred return (0); 624184610Salfred} 625184610Salfred 626184610Salfredstatic void 627194677Sthompsarue_intr_callback(struct usb_xfer *xfer, usb_error_t error) 628184610Salfred{ 629194677Sthompsa struct rue_softc *sc = usbd_xfer_softc(xfer); 630194228Sthompsa struct ifnet *ifp = uether_getifp(&sc->sc_ue); 631184610Salfred struct rue_intrpkt pkt; 632194677Sthompsa struct usb_page_cache *pc; 633194677Sthompsa int actlen; 634184610Salfred 635194677Sthompsa usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 636194677Sthompsa 637184610Salfred switch (USB_GET_STATE(xfer)) { 638184610Salfred case USB_ST_TRANSFERRED: 639184610Salfred 640184610Salfred if (ifp && (ifp->if_drv_flags & IFF_DRV_RUNNING) && 641233774Shselasky actlen >= (int)sizeof(pkt)) { 642184610Salfred 643194677Sthompsa pc = usbd_xfer_get_frame(xfer, 0); 644194677Sthompsa usbd_copy_out(pc, 0, &pkt, sizeof(pkt)); 645184610Salfred 646184610Salfred ifp->if_ierrors += pkt.rue_rxlost_cnt; 647184610Salfred ifp->if_ierrors += pkt.rue_crcerr_cnt; 648184610Salfred ifp->if_collisions += pkt.rue_col_cnt; 649184610Salfred } 650188412Sthompsa /* FALLTHROUGH */ 651184610Salfred case USB_ST_SETUP: 652188412Sthompsatr_setup: 653194677Sthompsa usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 654194228Sthompsa usbd_transfer_submit(xfer); 655184610Salfred return; 656184610Salfred 657184610Salfred default: /* Error */ 658194677Sthompsa if (error != USB_ERR_CANCELLED) { 659188412Sthompsa /* try to clear stall first */ 660194677Sthompsa usbd_xfer_set_stall(xfer); 661188412Sthompsa goto tr_setup; 662184610Salfred } 663184610Salfred return; 664184610Salfred } 665184610Salfred} 666184610Salfred 667184610Salfredstatic void 668194677Sthompsarue_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error) 669184610Salfred{ 670194677Sthompsa struct rue_softc *sc = usbd_xfer_softc(xfer); 671192984Sthompsa struct usb_ether *ue = &sc->sc_ue; 672194228Sthompsa struct ifnet *ifp = uether_getifp(ue); 673194677Sthompsa struct usb_page_cache *pc; 674184610Salfred uint16_t status; 675194677Sthompsa int actlen; 676184610Salfred 677194677Sthompsa usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 678194677Sthompsa 679184610Salfred switch (USB_GET_STATE(xfer)) { 680184610Salfred case USB_ST_TRANSFERRED: 681184610Salfred 682194677Sthompsa if (actlen < 4) { 683184610Salfred ifp->if_ierrors++; 684184610Salfred goto tr_setup; 685184610Salfred } 686194677Sthompsa pc = usbd_xfer_get_frame(xfer, 0); 687194677Sthompsa usbd_copy_out(pc, actlen - 4, &status, sizeof(status)); 688194677Sthompsa actlen -= 4; 689184610Salfred 690188412Sthompsa /* check recieve packet was valid or not */ 691184610Salfred status = le16toh(status); 692184610Salfred if ((status & RUE_RXSTAT_VALID) == 0) { 693184610Salfred ifp->if_ierrors++; 694184610Salfred goto tr_setup; 695184610Salfred } 696194677Sthompsa uether_rxbuf(ue, pc, 0, actlen); 697188412Sthompsa /* FALLTHROUGH */ 698184610Salfred case USB_ST_SETUP: 699184610Salfredtr_setup: 700194677Sthompsa usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 701194228Sthompsa usbd_transfer_submit(xfer); 702194228Sthompsa uether_rxflush(ue); 703184610Salfred return; 704184610Salfred 705184610Salfred default: /* Error */ 706188412Sthompsa DPRINTF("bulk read error, %s\n", 707194677Sthompsa usbd_errstr(error)); 708188412Sthompsa 709194677Sthompsa if (error != USB_ERR_CANCELLED) { 710184610Salfred /* try to clear stall first */ 711194677Sthompsa usbd_xfer_set_stall(xfer); 712188412Sthompsa goto tr_setup; 713184610Salfred } 714184610Salfred return; 715184610Salfred } 716184610Salfred} 717184610Salfred 718184610Salfredstatic void 719194677Sthompsarue_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error) 720184610Salfred{ 721194677Sthompsa struct rue_softc *sc = usbd_xfer_softc(xfer); 722194228Sthompsa struct ifnet *ifp = uether_getifp(&sc->sc_ue); 723194677Sthompsa struct usb_page_cache *pc; 724184610Salfred struct mbuf *m; 725188412Sthompsa int temp_len; 726184610Salfred 727184610Salfred switch (USB_GET_STATE(xfer)) { 728184610Salfred case USB_ST_TRANSFERRED: 729184610Salfred DPRINTFN(11, "transfer complete\n"); 730184610Salfred ifp->if_opackets++; 731184610Salfred 732188412Sthompsa /* FALLTHROUGH */ 733184610Salfred case USB_ST_SETUP: 734188412Sthompsatr_setup: 735188412Sthompsa if ((sc->sc_flags & RUE_FLAG_LINK) == 0) { 736184610Salfred /* 737184610Salfred * don't send anything if there is no link ! 738184610Salfred */ 739188412Sthompsa return; 740184610Salfred } 741184610Salfred IFQ_DRV_DEQUEUE(&ifp->if_snd, m); 742184610Salfred 743188412Sthompsa if (m == NULL) 744188412Sthompsa return; 745188412Sthompsa if (m->m_pkthdr.len > MCLBYTES) 746184610Salfred m->m_pkthdr.len = MCLBYTES; 747184610Salfred temp_len = m->m_pkthdr.len; 748184610Salfred 749194677Sthompsa pc = usbd_xfer_get_frame(xfer, 0); 750194677Sthompsa usbd_m_copy_in(pc, 0, m, 0, m->m_pkthdr.len); 751184610Salfred 752184610Salfred /* 753184610Salfred * This is an undocumented behavior. 754184610Salfred * RTL8150 chip doesn't send frame length smaller than 755184610Salfred * RUE_MIN_FRAMELEN (60) byte packet. 756184610Salfred */ 757184610Salfred if (temp_len < RUE_MIN_FRAMELEN) { 758194677Sthompsa usbd_frame_zero(pc, temp_len, 759184610Salfred RUE_MIN_FRAMELEN - temp_len); 760184610Salfred temp_len = RUE_MIN_FRAMELEN; 761184610Salfred } 762194677Sthompsa usbd_xfer_set_frame_len(xfer, 0, temp_len); 763184610Salfred 764184610Salfred /* 765184610Salfred * if there's a BPF listener, bounce a copy 766184610Salfred * of this frame to him: 767184610Salfred */ 768184610Salfred BPF_MTAP(ifp, m); 769184610Salfred 770184610Salfred m_freem(m); 771184610Salfred 772194228Sthompsa usbd_transfer_submit(xfer); 773184610Salfred 774184610Salfred return; 775184610Salfred 776184610Salfred default: /* Error */ 777184610Salfred DPRINTFN(11, "transfer error, %s\n", 778194677Sthompsa usbd_errstr(error)); 779184610Salfred 780188412Sthompsa ifp->if_oerrors++; 781188412Sthompsa 782194677Sthompsa if (error != USB_ERR_CANCELLED) { 783184610Salfred /* try to clear stall first */ 784194677Sthompsa usbd_xfer_set_stall(xfer); 785188412Sthompsa goto tr_setup; 786184610Salfred } 787184610Salfred return; 788184610Salfred } 789184610Salfred} 790184610Salfred 791184610Salfredstatic void 792192984Sthompsarue_tick(struct usb_ether *ue) 793184610Salfred{ 794194228Sthompsa struct rue_softc *sc = uether_getsc(ue); 795184610Salfred struct mii_data *mii = GET_MII(sc); 796184610Salfred 797188412Sthompsa RUE_LOCK_ASSERT(sc, MA_OWNED); 798188412Sthompsa 799184610Salfred mii_tick(mii); 800188412Sthompsa if ((sc->sc_flags & RUE_FLAG_LINK) == 0 801188412Sthompsa && mii->mii_media_status & IFM_ACTIVE && 802188412Sthompsa IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) { 803188412Sthompsa sc->sc_flags |= RUE_FLAG_LINK; 804188412Sthompsa rue_start(ue); 805184610Salfred } 806184610Salfred} 807184610Salfred 808184610Salfredstatic void 809192984Sthompsarue_start(struct usb_ether *ue) 810184610Salfred{ 811194228Sthompsa struct rue_softc *sc = uether_getsc(ue); 812184610Salfred 813188412Sthompsa /* 814188412Sthompsa * start the USB transfers, if not already started: 815188412Sthompsa */ 816194228Sthompsa usbd_transfer_start(sc->sc_xfer[RUE_INTR_DT_RD]); 817194228Sthompsa usbd_transfer_start(sc->sc_xfer[RUE_BULK_DT_RD]); 818194228Sthompsa usbd_transfer_start(sc->sc_xfer[RUE_BULK_DT_WR]); 819184610Salfred} 820184610Salfred 821184610Salfredstatic void 822192984Sthompsarue_init(struct usb_ether *ue) 823184610Salfred{ 824194228Sthompsa struct rue_softc *sc = uether_getsc(ue); 825194228Sthompsa struct ifnet *ifp = uether_getifp(ue); 826184610Salfred 827188412Sthompsa RUE_LOCK_ASSERT(sc, MA_OWNED); 828184610Salfred 829184610Salfred /* 830184610Salfred * Cancel pending I/O 831184610Salfred */ 832188412Sthompsa rue_reset(sc); 833184610Salfred 834188412Sthompsa /* Set MAC address */ 835188412Sthompsa rue_write_mem(sc, RUE_IDR0, IF_LLADDR(ifp), ETHER_ADDR_LEN); 836184610Salfred 837188412Sthompsa rue_stop(ue); 838184610Salfred 839184610Salfred /* 840184610Salfred * Set the initial TX and RX configuration. 841184610Salfred */ 842188412Sthompsa rue_csr_write_1(sc, RUE_TCR, RUE_TCR_CONFIG); 843188412Sthompsa rue_csr_write_2(sc, RUE_RCR, RUE_RCR_CONFIG|RUE_RCR_AB); 844184610Salfred 845184610Salfred /* Load the multicast filter */ 846188412Sthompsa rue_setpromisc(ue); 847188412Sthompsa /* Load the multicast filter. */ 848188412Sthompsa rue_setmulti(ue); 849184610Salfred 850184610Salfred /* Enable RX and TX */ 851188412Sthompsa rue_csr_write_1(sc, RUE_CR, (RUE_CR_TE | RUE_CR_RE | RUE_CR_EP3CLREN)); 852184610Salfred 853194677Sthompsa usbd_xfer_set_stall(sc->sc_xfer[RUE_BULK_DT_WR]); 854184610Salfred 855188412Sthompsa ifp->if_drv_flags |= IFF_DRV_RUNNING; 856188412Sthompsa rue_start(ue); 857184610Salfred} 858184610Salfred 859184610Salfred/* 860184610Salfred * Set media options. 861184610Salfred */ 862184610Salfredstatic int 863188412Sthompsarue_ifmedia_upd(struct ifnet *ifp) 864184610Salfred{ 865184610Salfred struct rue_softc *sc = ifp->if_softc; 866184610Salfred struct mii_data *mii = GET_MII(sc); 867221407Smarius struct mii_softc *miisc; 868184610Salfred 869188412Sthompsa RUE_LOCK_ASSERT(sc, MA_OWNED); 870184610Salfred 871188412Sthompsa sc->sc_flags &= ~RUE_FLAG_LINK; 872221407Smarius LIST_FOREACH(miisc, &mii->mii_phys, mii_list) 873221407Smarius PHY_RESET(miisc); 874184610Salfred mii_mediachg(mii); 875188412Sthompsa return (0); 876184610Salfred} 877184610Salfred 878184610Salfred/* 879184610Salfred * Report current media status. 880184610Salfred */ 881184610Salfredstatic void 882188412Sthompsarue_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr) 883184610Salfred{ 884184610Salfred struct rue_softc *sc = ifp->if_softc; 885188412Sthompsa struct mii_data *mii = GET_MII(sc); 886184610Salfred 887188412Sthompsa RUE_LOCK(sc); 888188412Sthompsa mii_pollstat(mii); 889188412Sthompsa ifmr->ifm_active = mii->mii_media_active; 890188412Sthompsa ifmr->ifm_status = mii->mii_media_status; 891226479Syongari RUE_UNLOCK(sc); 892184610Salfred} 893184610Salfred 894184610Salfredstatic void 895192984Sthompsarue_stop(struct usb_ether *ue) 896184610Salfred{ 897194228Sthompsa struct rue_softc *sc = uether_getsc(ue); 898194228Sthompsa struct ifnet *ifp = uether_getifp(ue); 899184610Salfred 900188412Sthompsa RUE_LOCK_ASSERT(sc, MA_OWNED); 901184610Salfred 902188412Sthompsa ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 903188412Sthompsa sc->sc_flags &= ~RUE_FLAG_LINK; 904184610Salfred 905184610Salfred /* 906184610Salfred * stop all the transfers, if not already stopped: 907184610Salfred */ 908194228Sthompsa usbd_transfer_stop(sc->sc_xfer[RUE_BULK_DT_WR]); 909194228Sthompsa usbd_transfer_stop(sc->sc_xfer[RUE_BULK_DT_RD]); 910194228Sthompsa usbd_transfer_stop(sc->sc_xfer[RUE_INTR_DT_RD]); 911184610Salfred 912188412Sthompsa rue_csr_write_1(sc, RUE_CR, 0x00); 913184610Salfred 914188412Sthompsa rue_reset(sc); 915184610Salfred} 916