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: releng/10.2/sys/dev/usb/net/if_rue.c 251734 2013-06-14 05:36:47Z kevlo $"); 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 205235255SmariusDRIVER_MODULE_ORDERED(rue, uhub, rue_driver, rue_devclass, NULL, NULL, 206235255Smarius SI_ORDER_ANY); 207235255SmariusDRIVER_MODULE(miibus, rue, miibus_driver, miibus_devclass, NULL, NULL); 208188942SthompsaMODULE_DEPEND(rue, uether, 1, 1, 1); 209188942SthompsaMODULE_DEPEND(rue, usb, 1, 1, 1); 210184610SalfredMODULE_DEPEND(rue, ether, 1, 1, 1); 211184610SalfredMODULE_DEPEND(rue, miibus, 1, 1, 1); 212212122SthompsaMODULE_VERSION(rue, 1); 213184610Salfred 214192984Sthompsastatic const struct usb_ether_methods rue_ue_methods = { 215188412Sthompsa .ue_attach_post = rue_attach_post, 216188412Sthompsa .ue_start = rue_start, 217188412Sthompsa .ue_init = rue_init, 218188412Sthompsa .ue_stop = rue_stop, 219188412Sthompsa .ue_tick = rue_tick, 220188412Sthompsa .ue_setmulti = rue_setmulti, 221188412Sthompsa .ue_setpromisc = rue_setpromisc, 222188412Sthompsa .ue_mii_upd = rue_ifmedia_upd, 223188412Sthompsa .ue_mii_sts = rue_ifmedia_sts, 224188412Sthompsa}; 225184610Salfred 226188412Sthompsa#define RUE_SETBIT(sc, reg, x) \ 227188412Sthompsa rue_csr_write_1(sc, reg, rue_csr_read_1(sc, reg) | (x)) 228184610Salfred 229188412Sthompsa#define RUE_CLRBIT(sc, reg, x) \ 230188412Sthompsa rue_csr_write_1(sc, reg, rue_csr_read_1(sc, reg) & ~(x)) 231184610Salfred 232188412Sthompsastatic int 233188412Sthompsarue_read_mem(struct rue_softc *sc, uint16_t addr, void *buf, int len) 234184610Salfred{ 235192984Sthompsa struct usb_device_request req; 236184610Salfred 237184610Salfred req.bmRequestType = UT_READ_VENDOR_DEVICE; 238184610Salfred req.bRequest = UR_SET_ADDRESS; 239184610Salfred USETW(req.wValue, addr); 240184610Salfred USETW(req.wIndex, 0); 241184610Salfred USETW(req.wLength, len); 242184610Salfred 243194228Sthompsa return (uether_do_request(&sc->sc_ue, &req, buf, 1000)); 244184610Salfred} 245184610Salfred 246188412Sthompsastatic int 247188412Sthompsarue_write_mem(struct rue_softc *sc, uint16_t addr, void *buf, int len) 248184610Salfred{ 249192984Sthompsa struct usb_device_request req; 250184610Salfred 251184610Salfred req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 252184610Salfred req.bRequest = UR_SET_ADDRESS; 253184610Salfred USETW(req.wValue, addr); 254184610Salfred USETW(req.wIndex, 0); 255184610Salfred USETW(req.wLength, len); 256184610Salfred 257194228Sthompsa return (uether_do_request(&sc->sc_ue, &req, buf, 1000)); 258184610Salfred} 259184610Salfred 260184610Salfredstatic uint8_t 261188412Sthompsarue_csr_read_1(struct rue_softc *sc, uint16_t reg) 262184610Salfred{ 263184610Salfred uint8_t val; 264184610Salfred 265188412Sthompsa rue_read_mem(sc, reg, &val, 1); 266184610Salfred return (val); 267184610Salfred} 268184610Salfred 269184610Salfredstatic uint16_t 270188412Sthompsarue_csr_read_2(struct rue_softc *sc, uint16_t reg) 271184610Salfred{ 272184610Salfred uint8_t val[2]; 273184610Salfred 274188412Sthompsa rue_read_mem(sc, reg, &val, 2); 275184610Salfred return (UGETW(val)); 276184610Salfred} 277184610Salfred 278188412Sthompsastatic int 279188412Sthompsarue_csr_write_1(struct rue_softc *sc, uint16_t reg, uint8_t val) 280184610Salfred{ 281188412Sthompsa return (rue_write_mem(sc, reg, &val, 1)); 282184610Salfred} 283184610Salfred 284188412Sthompsastatic int 285188412Sthompsarue_csr_write_2(struct rue_softc *sc, uint16_t reg, uint16_t val) 286184610Salfred{ 287184610Salfred uint8_t temp[2]; 288184610Salfred 289184610Salfred USETW(temp, val); 290188412Sthompsa return (rue_write_mem(sc, reg, &temp, 2)); 291184610Salfred} 292184610Salfred 293188412Sthompsastatic int 294188412Sthompsarue_csr_write_4(struct rue_softc *sc, int reg, uint32_t val) 295184610Salfred{ 296184610Salfred uint8_t temp[4]; 297184610Salfred 298184610Salfred USETDW(temp, val); 299188412Sthompsa return (rue_write_mem(sc, reg, &temp, 4)); 300184610Salfred} 301184610Salfred 302184610Salfredstatic int 303188412Sthompsarue_miibus_readreg(device_t dev, int phy, int reg) 304184610Salfred{ 305184610Salfred struct rue_softc *sc = device_get_softc(dev); 306184610Salfred uint16_t rval; 307184610Salfred uint16_t ruereg; 308188412Sthompsa int locked; 309184610Salfred 310188412Sthompsa if (phy != 0) /* RTL8150 supports PHY == 0, only */ 311184610Salfred return (0); 312184610Salfred 313188412Sthompsa locked = mtx_owned(&sc->sc_mtx); 314188412Sthompsa if (!locked) 315188412Sthompsa RUE_LOCK(sc); 316188412Sthompsa 317184610Salfred switch (reg) { 318184610Salfred case MII_BMCR: 319184610Salfred ruereg = RUE_BMCR; 320184610Salfred break; 321184610Salfred case MII_BMSR: 322184610Salfred ruereg = RUE_BMSR; 323184610Salfred break; 324184610Salfred case MII_ANAR: 325184610Salfred ruereg = RUE_ANAR; 326184610Salfred break; 327184610Salfred case MII_ANER: 328184610Salfred ruereg = RUE_AER; 329184610Salfred break; 330184610Salfred case MII_ANLPAR: 331184610Salfred ruereg = RUE_ANLP; 332184610Salfred break; 333184610Salfred case MII_PHYIDR1: 334184610Salfred case MII_PHYIDR2: 335184610Salfred rval = 0; 336184610Salfred goto done; 337184610Salfred default: 338188412Sthompsa if (RUE_REG_MIN <= reg && reg <= RUE_REG_MAX) { 339188412Sthompsa rval = rue_csr_read_1(sc, reg); 340184610Salfred goto done; 341184610Salfred } 342188412Sthompsa device_printf(sc->sc_ue.ue_dev, "bad phy register\n"); 343184610Salfred rval = 0; 344184610Salfred goto done; 345184610Salfred } 346184610Salfred 347188412Sthompsa rval = rue_csr_read_2(sc, ruereg); 348184610Salfreddone: 349188412Sthompsa if (!locked) 350188412Sthompsa RUE_UNLOCK(sc); 351184610Salfred return (rval); 352184610Salfred} 353184610Salfred 354184610Salfredstatic int 355188412Sthompsarue_miibus_writereg(device_t dev, int phy, int reg, int data) 356184610Salfred{ 357184610Salfred struct rue_softc *sc = device_get_softc(dev); 358184610Salfred uint16_t ruereg; 359188412Sthompsa int locked; 360184610Salfred 361188412Sthompsa if (phy != 0) /* RTL8150 supports PHY == 0, only */ 362184610Salfred return (0); 363184610Salfred 364188412Sthompsa locked = mtx_owned(&sc->sc_mtx); 365188412Sthompsa if (!locked) 366188412Sthompsa RUE_LOCK(sc); 367188412Sthompsa 368184610Salfred switch (reg) { 369184610Salfred case MII_BMCR: 370184610Salfred ruereg = RUE_BMCR; 371184610Salfred break; 372184610Salfred case MII_BMSR: 373184610Salfred ruereg = RUE_BMSR; 374184610Salfred break; 375184610Salfred case MII_ANAR: 376184610Salfred ruereg = RUE_ANAR; 377184610Salfred break; 378184610Salfred case MII_ANER: 379184610Salfred ruereg = RUE_AER; 380184610Salfred break; 381184610Salfred case MII_ANLPAR: 382184610Salfred ruereg = RUE_ANLP; 383184610Salfred break; 384184610Salfred case MII_PHYIDR1: 385184610Salfred case MII_PHYIDR2: 386184610Salfred goto done; 387184610Salfred default: 388188412Sthompsa if (RUE_REG_MIN <= reg && reg <= RUE_REG_MAX) { 389188412Sthompsa rue_csr_write_1(sc, reg, data); 390184610Salfred goto done; 391184610Salfred } 392188412Sthompsa device_printf(sc->sc_ue.ue_dev, " bad phy register\n"); 393184610Salfred goto done; 394184610Salfred } 395188412Sthompsa rue_csr_write_2(sc, ruereg, data); 396184610Salfreddone: 397188412Sthompsa if (!locked) 398188412Sthompsa RUE_UNLOCK(sc); 399184610Salfred return (0); 400184610Salfred} 401184610Salfred 402184610Salfredstatic void 403188412Sthompsarue_miibus_statchg(device_t dev) 404184610Salfred{ 405184610Salfred /* 406184610Salfred * When the code below is enabled the card starts doing weird 407184610Salfred * things after link going from UP to DOWN and back UP. 408184610Salfred * 409184610Salfred * Looks like some of register writes below messes up PHY 410184610Salfred * interface. 411184610Salfred * 412184610Salfred * No visible regressions were found after commenting this code 413184610Salfred * out, so that disable it for good. 414184610Salfred */ 415184610Salfred#if 0 416184610Salfred struct rue_softc *sc = device_get_softc(dev); 417184610Salfred struct mii_data *mii = GET_MII(sc); 418184610Salfred uint16_t bmcr; 419188412Sthompsa int locked; 420184610Salfred 421188412Sthompsa locked = mtx_owned(&sc->sc_mtx); 422188412Sthompsa if (!locked) 423188412Sthompsa RUE_LOCK(sc); 424184610Salfred 425188412Sthompsa RUE_CLRBIT(sc, RUE_CR, (RUE_CR_RE | RUE_CR_TE)); 426184610Salfred 427188412Sthompsa bmcr = rue_csr_read_2(sc, RUE_BMCR); 428184610Salfred 429184610Salfred if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX) 430184610Salfred bmcr |= RUE_BMCR_SPD_SET; 431184610Salfred else 432184610Salfred bmcr &= ~RUE_BMCR_SPD_SET; 433184610Salfred 434184610Salfred if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) 435184610Salfred bmcr |= RUE_BMCR_DUPLEX; 436184610Salfred else 437184610Salfred bmcr &= ~RUE_BMCR_DUPLEX; 438184610Salfred 439188412Sthompsa rue_csr_write_2(sc, RUE_BMCR, bmcr); 440184610Salfred 441188412Sthompsa RUE_SETBIT(sc, RUE_CR, (RUE_CR_RE | RUE_CR_TE)); 442184610Salfred 443188412Sthompsa if (!locked) 444188412Sthompsa RUE_UNLOCK(sc); 445184610Salfred#endif 446184610Salfred} 447184610Salfred 448184610Salfredstatic void 449192984Sthompsarue_setpromisc(struct usb_ether *ue) 450184610Salfred{ 451194228Sthompsa struct rue_softc *sc = uether_getsc(ue); 452194228Sthompsa struct ifnet *ifp = uether_getifp(ue); 453184610Salfred 454188412Sthompsa RUE_LOCK_ASSERT(sc, MA_OWNED); 455184610Salfred 456188412Sthompsa /* If we want promiscuous mode, set the allframes bit. */ 457188412Sthompsa if (ifp->if_flags & IFF_PROMISC) 458188412Sthompsa RUE_SETBIT(sc, RUE_RCR, RUE_RCR_AAP); 459188412Sthompsa else 460188412Sthompsa RUE_CLRBIT(sc, RUE_RCR, RUE_RCR_AAP); 461184610Salfred} 462184610Salfred 463184610Salfred/* 464184610Salfred * Program the 64-bit multicast hash filter. 465184610Salfred */ 466184610Salfredstatic void 467192984Sthompsarue_setmulti(struct usb_ether *ue) 468184610Salfred{ 469194228Sthompsa struct rue_softc *sc = uether_getsc(ue); 470194228Sthompsa struct ifnet *ifp = uether_getifp(ue); 471184610Salfred uint16_t rxcfg; 472188412Sthompsa int h = 0; 473188412Sthompsa uint32_t hashes[2] = { 0, 0 }; 474188412Sthompsa struct ifmultiaddr *ifma; 475188412Sthompsa int mcnt = 0; 476184610Salfred 477188412Sthompsa RUE_LOCK_ASSERT(sc, MA_OWNED); 478184610Salfred 479188412Sthompsa rxcfg = rue_csr_read_2(sc, RUE_RCR); 480188412Sthompsa 481188412Sthompsa if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) { 482184610Salfred rxcfg |= (RUE_RCR_AAM | RUE_RCR_AAP); 483184610Salfred rxcfg &= ~RUE_RCR_AM; 484188412Sthompsa rue_csr_write_2(sc, RUE_RCR, rxcfg); 485188412Sthompsa rue_csr_write_4(sc, RUE_MAR0, 0xFFFFFFFF); 486188412Sthompsa rue_csr_write_4(sc, RUE_MAR4, 0xFFFFFFFF); 487184610Salfred return; 488184610Salfred } 489184610Salfred 490188412Sthompsa /* first, zot all the existing hash bits */ 491188412Sthompsa rue_csr_write_4(sc, RUE_MAR0, 0); 492188412Sthompsa rue_csr_write_4(sc, RUE_MAR4, 0); 493188412Sthompsa 494188412Sthompsa /* now program new ones */ 495195049Srwatson if_maddr_rlock(ifp); 496188412Sthompsa TAILQ_FOREACH (ifma, &ifp->if_multiaddrs, ifma_link) 497188412Sthompsa { 498188412Sthompsa if (ifma->ifma_addr->sa_family != AF_LINK) 499188412Sthompsa continue; 500188412Sthompsa h = ether_crc32_be(LLADDR((struct sockaddr_dl *) 501188412Sthompsa ifma->ifma_addr), ETHER_ADDR_LEN) >> 26; 502188412Sthompsa if (h < 32) 503188412Sthompsa hashes[0] |= (1 << h); 504188412Sthompsa else 505188412Sthompsa hashes[1] |= (1 << (h - 32)); 506188412Sthompsa mcnt++; 507188412Sthompsa } 508195049Srwatson if_maddr_runlock(ifp); 509188412Sthompsa 510188412Sthompsa if (mcnt) 511184610Salfred rxcfg |= RUE_RCR_AM; 512184610Salfred else 513184610Salfred rxcfg &= ~RUE_RCR_AM; 514184610Salfred 515184610Salfred rxcfg &= ~(RUE_RCR_AAM | RUE_RCR_AAP); 516184610Salfred 517188412Sthompsa rue_csr_write_2(sc, RUE_RCR, rxcfg); 518188412Sthompsa rue_csr_write_4(sc, RUE_MAR0, hashes[0]); 519188412Sthompsa rue_csr_write_4(sc, RUE_MAR4, hashes[1]); 520184610Salfred} 521184610Salfred 522184610Salfredstatic void 523188412Sthompsarue_reset(struct rue_softc *sc) 524184610Salfred{ 525188412Sthompsa int i; 526184610Salfred 527188412Sthompsa rue_csr_write_1(sc, RUE_CR, RUE_CR_SOFT_RST); 528184610Salfred 529188412Sthompsa for (i = 0; i != RUE_TIMEOUT; i++) { 530194228Sthompsa if (uether_pause(&sc->sc_ue, hz / 1000)) 531188412Sthompsa break; 532188412Sthompsa if (!(rue_csr_read_1(sc, RUE_CR) & RUE_CR_SOFT_RST)) 533188412Sthompsa break; 534188412Sthompsa } 535188412Sthompsa if (i == RUE_TIMEOUT) 536199816Sthompsa device_printf(sc->sc_ue.ue_dev, "reset never completed\n"); 537184610Salfred 538194228Sthompsa uether_pause(&sc->sc_ue, hz / 100); 539188412Sthompsa} 540184610Salfred 541188412Sthompsastatic void 542192984Sthompsarue_attach_post(struct usb_ether *ue) 543188412Sthompsa{ 544194228Sthompsa struct rue_softc *sc = uether_getsc(ue); 545184610Salfred 546188412Sthompsa /* reset the adapter */ 547188412Sthompsa rue_reset(sc); 548184610Salfred 549188412Sthompsa /* get station address from the EEPROM */ 550188412Sthompsa rue_read_mem(sc, RUE_EEPROM_IDR0, ue->ue_eaddr, ETHER_ADDR_LEN); 551184610Salfred} 552184610Salfred 553184610Salfred/* 554184610Salfred * Probe for a RTL8150 chip. 555184610Salfred */ 556184610Salfredstatic int 557184610Salfredrue_probe(device_t dev) 558184610Salfred{ 559192984Sthompsa struct usb_attach_arg *uaa = device_get_ivars(dev); 560184610Salfred 561192499Sthompsa if (uaa->usb_mode != USB_MODE_HOST) 562184610Salfred return (ENXIO); 563188412Sthompsa if (uaa->info.bConfigIndex != RUE_CONFIG_IDX) 564184610Salfred return (ENXIO); 565188412Sthompsa if (uaa->info.bIfaceIndex != RUE_IFACE_IDX) 566184610Salfred return (ENXIO); 567188412Sthompsa 568194228Sthompsa return (usbd_lookup_id_by_uaa(rue_devs, sizeof(rue_devs), uaa)); 569184610Salfred} 570184610Salfred 571184610Salfred/* 572184610Salfred * Attach the interface. Allocate softc structures, do ifmedia 573184610Salfred * setup and ethernet/BPF attach. 574184610Salfred */ 575184610Salfredstatic int 576184610Salfredrue_attach(device_t dev) 577184610Salfred{ 578192984Sthompsa struct usb_attach_arg *uaa = device_get_ivars(dev); 579184610Salfred struct rue_softc *sc = device_get_softc(dev); 580192984Sthompsa struct usb_ether *ue = &sc->sc_ue; 581184610Salfred uint8_t iface_index; 582188412Sthompsa int error; 583184610Salfred 584194228Sthompsa device_set_usb_desc(dev); 585188412Sthompsa mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF); 586184610Salfred 587184610Salfred iface_index = RUE_IFACE_IDX; 588194228Sthompsa error = usbd_transfer_setup(uaa->device, &iface_index, 589187259Sthompsa sc->sc_xfer, rue_config, RUE_N_TRANSFER, 590184610Salfred sc, &sc->sc_mtx); 591184610Salfred if (error) { 592199816Sthompsa device_printf(dev, "allocating USB transfers failed\n"); 593184610Salfred goto detach; 594184610Salfred } 595188412Sthompsa 596188412Sthompsa ue->ue_sc = sc; 597188412Sthompsa ue->ue_dev = dev; 598188412Sthompsa ue->ue_udev = uaa->device; 599188412Sthompsa ue->ue_mtx = &sc->sc_mtx; 600188412Sthompsa ue->ue_methods = &rue_ue_methods; 601188412Sthompsa 602194228Sthompsa error = uether_ifattach(ue); 603184610Salfred if (error) { 604188412Sthompsa device_printf(dev, "could not attach interface\n"); 605184610Salfred goto detach; 606184610Salfred } 607184610Salfred return (0); /* success */ 608184610Salfred 609184610Salfreddetach: 610184610Salfred rue_detach(dev); 611184610Salfred return (ENXIO); /* failure */ 612184610Salfred} 613184610Salfred 614184610Salfredstatic int 615184610Salfredrue_detach(device_t dev) 616184610Salfred{ 617184610Salfred struct rue_softc *sc = device_get_softc(dev); 618192984Sthompsa struct usb_ether *ue = &sc->sc_ue; 619184610Salfred 620194228Sthompsa usbd_transfer_unsetup(sc->sc_xfer, RUE_N_TRANSFER); 621194228Sthompsa uether_ifdetach(ue); 622184610Salfred mtx_destroy(&sc->sc_mtx); 623184610Salfred 624184610Salfred return (0); 625184610Salfred} 626184610Salfred 627184610Salfredstatic void 628194677Sthompsarue_intr_callback(struct usb_xfer *xfer, usb_error_t error) 629184610Salfred{ 630194677Sthompsa struct rue_softc *sc = usbd_xfer_softc(xfer); 631194228Sthompsa struct ifnet *ifp = uether_getifp(&sc->sc_ue); 632184610Salfred struct rue_intrpkt pkt; 633194677Sthompsa struct usb_page_cache *pc; 634194677Sthompsa int actlen; 635184610Salfred 636194677Sthompsa usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 637194677Sthompsa 638184610Salfred switch (USB_GET_STATE(xfer)) { 639184610Salfred case USB_ST_TRANSFERRED: 640184610Salfred 641184610Salfred if (ifp && (ifp->if_drv_flags & IFF_DRV_RUNNING) && 642233774Shselasky actlen >= (int)sizeof(pkt)) { 643184610Salfred 644194677Sthompsa pc = usbd_xfer_get_frame(xfer, 0); 645194677Sthompsa usbd_copy_out(pc, 0, &pkt, sizeof(pkt)); 646184610Salfred 647184610Salfred ifp->if_ierrors += pkt.rue_rxlost_cnt; 648184610Salfred ifp->if_ierrors += pkt.rue_crcerr_cnt; 649184610Salfred ifp->if_collisions += pkt.rue_col_cnt; 650184610Salfred } 651188412Sthompsa /* FALLTHROUGH */ 652184610Salfred case USB_ST_SETUP: 653188412Sthompsatr_setup: 654194677Sthompsa usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 655194228Sthompsa usbd_transfer_submit(xfer); 656184610Salfred return; 657184610Salfred 658184610Salfred default: /* Error */ 659194677Sthompsa if (error != USB_ERR_CANCELLED) { 660188412Sthompsa /* try to clear stall first */ 661194677Sthompsa usbd_xfer_set_stall(xfer); 662188412Sthompsa goto tr_setup; 663184610Salfred } 664184610Salfred return; 665184610Salfred } 666184610Salfred} 667184610Salfred 668184610Salfredstatic void 669194677Sthompsarue_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error) 670184610Salfred{ 671194677Sthompsa struct rue_softc *sc = usbd_xfer_softc(xfer); 672192984Sthompsa struct usb_ether *ue = &sc->sc_ue; 673194228Sthompsa struct ifnet *ifp = uether_getifp(ue); 674194677Sthompsa struct usb_page_cache *pc; 675184610Salfred uint16_t status; 676194677Sthompsa int actlen; 677184610Salfred 678194677Sthompsa usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 679194677Sthompsa 680184610Salfred switch (USB_GET_STATE(xfer)) { 681184610Salfred case USB_ST_TRANSFERRED: 682184610Salfred 683194677Sthompsa if (actlen < 4) { 684184610Salfred ifp->if_ierrors++; 685184610Salfred goto tr_setup; 686184610Salfred } 687194677Sthompsa pc = usbd_xfer_get_frame(xfer, 0); 688194677Sthompsa usbd_copy_out(pc, actlen - 4, &status, sizeof(status)); 689194677Sthompsa actlen -= 4; 690184610Salfred 691188412Sthompsa /* check recieve packet was valid or not */ 692184610Salfred status = le16toh(status); 693184610Salfred if ((status & RUE_RXSTAT_VALID) == 0) { 694184610Salfred ifp->if_ierrors++; 695184610Salfred goto tr_setup; 696184610Salfred } 697194677Sthompsa uether_rxbuf(ue, pc, 0, actlen); 698188412Sthompsa /* FALLTHROUGH */ 699184610Salfred case USB_ST_SETUP: 700184610Salfredtr_setup: 701194677Sthompsa usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 702194228Sthompsa usbd_transfer_submit(xfer); 703194228Sthompsa uether_rxflush(ue); 704184610Salfred return; 705184610Salfred 706184610Salfred default: /* Error */ 707188412Sthompsa DPRINTF("bulk read error, %s\n", 708194677Sthompsa usbd_errstr(error)); 709188412Sthompsa 710194677Sthompsa if (error != USB_ERR_CANCELLED) { 711184610Salfred /* try to clear stall first */ 712194677Sthompsa usbd_xfer_set_stall(xfer); 713188412Sthompsa goto tr_setup; 714184610Salfred } 715184610Salfred return; 716184610Salfred } 717184610Salfred} 718184610Salfred 719184610Salfredstatic void 720194677Sthompsarue_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error) 721184610Salfred{ 722194677Sthompsa struct rue_softc *sc = usbd_xfer_softc(xfer); 723194228Sthompsa struct ifnet *ifp = uether_getifp(&sc->sc_ue); 724194677Sthompsa struct usb_page_cache *pc; 725184610Salfred struct mbuf *m; 726188412Sthompsa int temp_len; 727184610Salfred 728184610Salfred switch (USB_GET_STATE(xfer)) { 729184610Salfred case USB_ST_TRANSFERRED: 730184610Salfred DPRINTFN(11, "transfer complete\n"); 731184610Salfred ifp->if_opackets++; 732184610Salfred 733188412Sthompsa /* FALLTHROUGH */ 734184610Salfred case USB_ST_SETUP: 735188412Sthompsatr_setup: 736188412Sthompsa if ((sc->sc_flags & RUE_FLAG_LINK) == 0) { 737184610Salfred /* 738184610Salfred * don't send anything if there is no link ! 739184610Salfred */ 740188412Sthompsa return; 741184610Salfred } 742184610Salfred IFQ_DRV_DEQUEUE(&ifp->if_snd, m); 743184610Salfred 744188412Sthompsa if (m == NULL) 745188412Sthompsa return; 746188412Sthompsa if (m->m_pkthdr.len > MCLBYTES) 747184610Salfred m->m_pkthdr.len = MCLBYTES; 748184610Salfred temp_len = m->m_pkthdr.len; 749184610Salfred 750194677Sthompsa pc = usbd_xfer_get_frame(xfer, 0); 751194677Sthompsa usbd_m_copy_in(pc, 0, m, 0, m->m_pkthdr.len); 752184610Salfred 753184610Salfred /* 754184610Salfred * This is an undocumented behavior. 755184610Salfred * RTL8150 chip doesn't send frame length smaller than 756184610Salfred * RUE_MIN_FRAMELEN (60) byte packet. 757184610Salfred */ 758184610Salfred if (temp_len < RUE_MIN_FRAMELEN) { 759194677Sthompsa usbd_frame_zero(pc, temp_len, 760184610Salfred RUE_MIN_FRAMELEN - temp_len); 761184610Salfred temp_len = RUE_MIN_FRAMELEN; 762184610Salfred } 763194677Sthompsa usbd_xfer_set_frame_len(xfer, 0, temp_len); 764184610Salfred 765184610Salfred /* 766184610Salfred * if there's a BPF listener, bounce a copy 767184610Salfred * of this frame to him: 768184610Salfred */ 769184610Salfred BPF_MTAP(ifp, m); 770184610Salfred 771184610Salfred m_freem(m); 772184610Salfred 773194228Sthompsa usbd_transfer_submit(xfer); 774184610Salfred 775184610Salfred return; 776184610Salfred 777184610Salfred default: /* Error */ 778184610Salfred DPRINTFN(11, "transfer error, %s\n", 779194677Sthompsa usbd_errstr(error)); 780184610Salfred 781188412Sthompsa ifp->if_oerrors++; 782188412Sthompsa 783194677Sthompsa if (error != USB_ERR_CANCELLED) { 784184610Salfred /* try to clear stall first */ 785194677Sthompsa usbd_xfer_set_stall(xfer); 786188412Sthompsa goto tr_setup; 787184610Salfred } 788184610Salfred return; 789184610Salfred } 790184610Salfred} 791184610Salfred 792184610Salfredstatic void 793192984Sthompsarue_tick(struct usb_ether *ue) 794184610Salfred{ 795194228Sthompsa struct rue_softc *sc = uether_getsc(ue); 796184610Salfred struct mii_data *mii = GET_MII(sc); 797184610Salfred 798188412Sthompsa RUE_LOCK_ASSERT(sc, MA_OWNED); 799188412Sthompsa 800184610Salfred mii_tick(mii); 801188412Sthompsa if ((sc->sc_flags & RUE_FLAG_LINK) == 0 802188412Sthompsa && mii->mii_media_status & IFM_ACTIVE && 803188412Sthompsa IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) { 804188412Sthompsa sc->sc_flags |= RUE_FLAG_LINK; 805188412Sthompsa rue_start(ue); 806184610Salfred } 807184610Salfred} 808184610Salfred 809184610Salfredstatic void 810192984Sthompsarue_start(struct usb_ether *ue) 811184610Salfred{ 812194228Sthompsa struct rue_softc *sc = uether_getsc(ue); 813184610Salfred 814188412Sthompsa /* 815188412Sthompsa * start the USB transfers, if not already started: 816188412Sthompsa */ 817194228Sthompsa usbd_transfer_start(sc->sc_xfer[RUE_INTR_DT_RD]); 818194228Sthompsa usbd_transfer_start(sc->sc_xfer[RUE_BULK_DT_RD]); 819194228Sthompsa usbd_transfer_start(sc->sc_xfer[RUE_BULK_DT_WR]); 820184610Salfred} 821184610Salfred 822184610Salfredstatic void 823192984Sthompsarue_init(struct usb_ether *ue) 824184610Salfred{ 825194228Sthompsa struct rue_softc *sc = uether_getsc(ue); 826194228Sthompsa struct ifnet *ifp = uether_getifp(ue); 827184610Salfred 828188412Sthompsa RUE_LOCK_ASSERT(sc, MA_OWNED); 829184610Salfred 830184610Salfred /* 831184610Salfred * Cancel pending I/O 832184610Salfred */ 833188412Sthompsa rue_reset(sc); 834184610Salfred 835188412Sthompsa /* Set MAC address */ 836188412Sthompsa rue_write_mem(sc, RUE_IDR0, IF_LLADDR(ifp), ETHER_ADDR_LEN); 837184610Salfred 838188412Sthompsa rue_stop(ue); 839184610Salfred 840184610Salfred /* 841184610Salfred * Set the initial TX and RX configuration. 842184610Salfred */ 843188412Sthompsa rue_csr_write_1(sc, RUE_TCR, RUE_TCR_CONFIG); 844188412Sthompsa rue_csr_write_2(sc, RUE_RCR, RUE_RCR_CONFIG|RUE_RCR_AB); 845184610Salfred 846184610Salfred /* Load the multicast filter */ 847188412Sthompsa rue_setpromisc(ue); 848188412Sthompsa /* Load the multicast filter. */ 849188412Sthompsa rue_setmulti(ue); 850184610Salfred 851184610Salfred /* Enable RX and TX */ 852188412Sthompsa rue_csr_write_1(sc, RUE_CR, (RUE_CR_TE | RUE_CR_RE | RUE_CR_EP3CLREN)); 853184610Salfred 854194677Sthompsa usbd_xfer_set_stall(sc->sc_xfer[RUE_BULK_DT_WR]); 855184610Salfred 856188412Sthompsa ifp->if_drv_flags |= IFF_DRV_RUNNING; 857188412Sthompsa rue_start(ue); 858184610Salfred} 859184610Salfred 860184610Salfred/* 861184610Salfred * Set media options. 862184610Salfred */ 863184610Salfredstatic int 864188412Sthompsarue_ifmedia_upd(struct ifnet *ifp) 865184610Salfred{ 866184610Salfred struct rue_softc *sc = ifp->if_softc; 867184610Salfred struct mii_data *mii = GET_MII(sc); 868221407Smarius struct mii_softc *miisc; 869251734Skevlo int error; 870184610Salfred 871188412Sthompsa RUE_LOCK_ASSERT(sc, MA_OWNED); 872184610Salfred 873188412Sthompsa sc->sc_flags &= ~RUE_FLAG_LINK; 874221407Smarius LIST_FOREACH(miisc, &mii->mii_phys, mii_list) 875221407Smarius PHY_RESET(miisc); 876251734Skevlo error = mii_mediachg(mii); 877251734Skevlo return (error); 878184610Salfred} 879184610Salfred 880184610Salfred/* 881184610Salfred * Report current media status. 882184610Salfred */ 883184610Salfredstatic void 884188412Sthompsarue_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr) 885184610Salfred{ 886184610Salfred struct rue_softc *sc = ifp->if_softc; 887188412Sthompsa struct mii_data *mii = GET_MII(sc); 888184610Salfred 889188412Sthompsa RUE_LOCK(sc); 890188412Sthompsa mii_pollstat(mii); 891188412Sthompsa ifmr->ifm_active = mii->mii_media_active; 892188412Sthompsa ifmr->ifm_status = mii->mii_media_status; 893226479Syongari RUE_UNLOCK(sc); 894184610Salfred} 895184610Salfred 896184610Salfredstatic void 897192984Sthompsarue_stop(struct usb_ether *ue) 898184610Salfred{ 899194228Sthompsa struct rue_softc *sc = uether_getsc(ue); 900194228Sthompsa struct ifnet *ifp = uether_getifp(ue); 901184610Salfred 902188412Sthompsa RUE_LOCK_ASSERT(sc, MA_OWNED); 903184610Salfred 904188412Sthompsa ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 905188412Sthompsa sc->sc_flags &= ~RUE_FLAG_LINK; 906184610Salfred 907184610Salfred /* 908184610Salfred * stop all the transfers, if not already stopped: 909184610Salfred */ 910194228Sthompsa usbd_transfer_stop(sc->sc_xfer[RUE_BULK_DT_WR]); 911194228Sthompsa usbd_transfer_stop(sc->sc_xfer[RUE_BULK_DT_RD]); 912194228Sthompsa usbd_transfer_stop(sc->sc_xfer[RUE_INTR_DT_RD]); 913184610Salfred 914188412Sthompsa rue_csr_write_1(sc, RUE_CR, 0x00); 915184610Salfred 916188412Sthompsa rue_reset(sc); 917184610Salfred} 918