102ac6454SAndrew Thompson /*- 202ac6454SAndrew Thompson * Copyright (c) 2001-2003, Shunsuke Akiyama <akiyama@FreeBSD.org>. 302ac6454SAndrew Thompson * Copyright (c) 1997, 1998, 1999, 2000 Bill Paul <wpaul@ee.columbia.edu>. 402ac6454SAndrew Thompson * All rights reserved. 502ac6454SAndrew Thompson * 602ac6454SAndrew Thompson * Redistribution and use in source and binary forms, with or without 702ac6454SAndrew Thompson * modification, are permitted provided that the following conditions 802ac6454SAndrew Thompson * are met: 902ac6454SAndrew Thompson * 1. Redistributions of source code must retain the above copyright 1002ac6454SAndrew Thompson * notice, this list of conditions and the following disclaimer. 1102ac6454SAndrew Thompson * 2. Redistributions in binary form must reproduce the above copyright 1202ac6454SAndrew Thompson * notice, this list of conditions and the following disclaimer in the 1302ac6454SAndrew Thompson * documentation and/or other materials provided with the distribution. 1402ac6454SAndrew Thompson * 1502ac6454SAndrew Thompson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1602ac6454SAndrew Thompson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1702ac6454SAndrew Thompson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1802ac6454SAndrew Thompson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1902ac6454SAndrew Thompson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2002ac6454SAndrew Thompson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2102ac6454SAndrew Thompson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2202ac6454SAndrew Thompson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2302ac6454SAndrew Thompson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2402ac6454SAndrew Thompson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2502ac6454SAndrew Thompson * SUCH DAMAGE. 2602ac6454SAndrew Thompson */ 2702ac6454SAndrew Thompson /*- 2802ac6454SAndrew Thompson * Copyright (c) 1997, 1998, 1999, 2000 2902ac6454SAndrew Thompson * Bill Paul <wpaul@ee.columbia.edu>. All rights reserved. 3002ac6454SAndrew Thompson * 3102ac6454SAndrew Thompson * Redistribution and use in source and binary forms, with or without 3202ac6454SAndrew Thompson * modification, are permitted provided that the following conditions 3302ac6454SAndrew Thompson * are met: 3402ac6454SAndrew Thompson * 1. Redistributions of source code must retain the above copyright 3502ac6454SAndrew Thompson * notice, this list of conditions and the following disclaimer. 3602ac6454SAndrew Thompson * 2. Redistributions in binary form must reproduce the above copyright 3702ac6454SAndrew Thompson * notice, this list of conditions and the following disclaimer in the 3802ac6454SAndrew Thompson * documentation and/or other materials provided with the distribution. 3902ac6454SAndrew Thompson * 3. All advertising materials mentioning features or use of this software 4002ac6454SAndrew Thompson * must display the following acknowledgement: 4102ac6454SAndrew Thompson * This product includes software developed by Bill Paul. 4202ac6454SAndrew Thompson * 4. Neither the name of the author nor the names of any co-contributors 4302ac6454SAndrew Thompson * may be used to endorse or promote products derived from this software 4402ac6454SAndrew Thompson * without specific prior written permission. 4502ac6454SAndrew Thompson * 4602ac6454SAndrew Thompson * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 4702ac6454SAndrew Thompson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4802ac6454SAndrew Thompson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4902ac6454SAndrew Thompson * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD 5002ac6454SAndrew Thompson * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 5102ac6454SAndrew Thompson * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 5202ac6454SAndrew Thompson * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 5302ac6454SAndrew Thompson * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 5402ac6454SAndrew Thompson * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 5502ac6454SAndrew Thompson * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 5602ac6454SAndrew Thompson * THE POSSIBILITY OF SUCH DAMAGE. 5702ac6454SAndrew Thompson */ 5802ac6454SAndrew Thompson 5902ac6454SAndrew Thompson #include <sys/cdefs.h> 6002ac6454SAndrew Thompson __FBSDID("$FreeBSD$"); 6102ac6454SAndrew Thompson 6202ac6454SAndrew Thompson /* 6302ac6454SAndrew Thompson * RealTek RTL8150 USB to fast ethernet controller driver. 6402ac6454SAndrew Thompson * Datasheet is available from 6502ac6454SAndrew Thompson * ftp://ftp.realtek.com.tw/lancard/data_sheet/8150/. 6602ac6454SAndrew Thompson */ 6702ac6454SAndrew Thompson 6802ac6454SAndrew Thompson #include "usbdevs.h" 6902ac6454SAndrew Thompson #include <dev/usb/usb.h> 7002ac6454SAndrew Thompson #include <dev/usb/usb_mfunc.h> 7102ac6454SAndrew Thompson #include <dev/usb/usb_error.h> 7202ac6454SAndrew Thompson 7302ac6454SAndrew Thompson #define USB_DEBUG_VAR rue_debug 7402ac6454SAndrew Thompson 7502ac6454SAndrew Thompson #include <dev/usb/usb_core.h> 7602ac6454SAndrew Thompson #include <dev/usb/usb_lookup.h> 7702ac6454SAndrew Thompson #include <dev/usb/usb_process.h> 7802ac6454SAndrew Thompson #include <dev/usb/usb_debug.h> 7902ac6454SAndrew Thompson #include <dev/usb/usb_request.h> 8002ac6454SAndrew Thompson #include <dev/usb/usb_busdma.h> 8102ac6454SAndrew Thompson #include <dev/usb/usb_util.h> 8202ac6454SAndrew Thompson 8302ac6454SAndrew Thompson #include <dev/usb/net/usb_ethernet.h> 8402ac6454SAndrew Thompson #include <dev/usb/net/if_ruereg.h> 8502ac6454SAndrew Thompson 8602ac6454SAndrew Thompson #if USB_DEBUG 8702ac6454SAndrew Thompson static int rue_debug = 0; 8802ac6454SAndrew Thompson 899360ae40SAndrew Thompson SYSCTL_NODE(_hw_usb, OID_AUTO, rue, CTLFLAG_RW, 0, "USB rue"); 909360ae40SAndrew Thompson SYSCTL_INT(_hw_usb_rue, OID_AUTO, debug, CTLFLAG_RW, 9102ac6454SAndrew Thompson &rue_debug, 0, "Debug level"); 9202ac6454SAndrew Thompson #endif 9302ac6454SAndrew Thompson 9402ac6454SAndrew Thompson /* 9502ac6454SAndrew Thompson * Various supported device vendors/products. 9602ac6454SAndrew Thompson */ 9702ac6454SAndrew Thompson 98760bc48eSAndrew Thompson static const struct usb_device_id rue_devs[] = { 9902ac6454SAndrew Thompson {USB_VPI(USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUAKTX, 0)}, 10002ac6454SAndrew Thompson {USB_VPI(USB_VENDOR_REALTEK, USB_PRODUCT_REALTEK_USBKR100, 0)}, 10102ac6454SAndrew Thompson }; 10202ac6454SAndrew Thompson 10302ac6454SAndrew Thompson /* prototypes */ 10402ac6454SAndrew Thompson 10502ac6454SAndrew Thompson static device_probe_t rue_probe; 10602ac6454SAndrew Thompson static device_attach_t rue_attach; 10702ac6454SAndrew Thompson static device_detach_t rue_detach; 10802ac6454SAndrew Thompson 10902ac6454SAndrew Thompson static miibus_readreg_t rue_miibus_readreg; 11002ac6454SAndrew Thompson static miibus_writereg_t rue_miibus_writereg; 11102ac6454SAndrew Thompson static miibus_statchg_t rue_miibus_statchg; 11202ac6454SAndrew Thompson 113e0a69b51SAndrew Thompson static usb_callback_t rue_intr_callback; 114e0a69b51SAndrew Thompson static usb_callback_t rue_bulk_read_callback; 115e0a69b51SAndrew Thompson static usb_callback_t rue_bulk_write_callback; 11602ac6454SAndrew Thompson 117e0a69b51SAndrew Thompson static uether_fn_t rue_attach_post; 118e0a69b51SAndrew Thompson static uether_fn_t rue_init; 119e0a69b51SAndrew Thompson static uether_fn_t rue_stop; 120e0a69b51SAndrew Thompson static uether_fn_t rue_start; 121e0a69b51SAndrew Thompson static uether_fn_t rue_tick; 122e0a69b51SAndrew Thompson static uether_fn_t rue_setmulti; 123e0a69b51SAndrew Thompson static uether_fn_t rue_setpromisc; 12402ac6454SAndrew Thompson 12502ac6454SAndrew Thompson static int rue_read_mem(struct rue_softc *, uint16_t, void *, int); 12602ac6454SAndrew Thompson static int rue_write_mem(struct rue_softc *, uint16_t, void *, int); 12702ac6454SAndrew Thompson static uint8_t rue_csr_read_1(struct rue_softc *, uint16_t); 12802ac6454SAndrew Thompson static uint16_t rue_csr_read_2(struct rue_softc *, uint16_t); 12902ac6454SAndrew Thompson static int rue_csr_write_1(struct rue_softc *, uint16_t, uint8_t); 13002ac6454SAndrew Thompson static int rue_csr_write_2(struct rue_softc *, uint16_t, uint16_t); 13102ac6454SAndrew Thompson static int rue_csr_write_4(struct rue_softc *, int, uint32_t); 13202ac6454SAndrew Thompson 13302ac6454SAndrew Thompson static void rue_reset(struct rue_softc *); 13402ac6454SAndrew Thompson static int rue_ifmedia_upd(struct ifnet *); 13502ac6454SAndrew Thompson static void rue_ifmedia_sts(struct ifnet *, struct ifmediareq *); 13602ac6454SAndrew Thompson 137760bc48eSAndrew Thompson static const struct usb_config rue_config[RUE_N_TRANSFER] = { 13802ac6454SAndrew Thompson 13902ac6454SAndrew Thompson [RUE_BULK_DT_WR] = { 14002ac6454SAndrew Thompson .type = UE_BULK, 14102ac6454SAndrew Thompson .endpoint = UE_ADDR_ANY, 14202ac6454SAndrew Thompson .direction = UE_DIR_OUT, 1434eae601eSAndrew Thompson .bufsize = MCLBYTES, 1444eae601eSAndrew Thompson .flags = {.pipe_bof = 1,.force_short_xfer = 1,}, 1454eae601eSAndrew Thompson .callback = rue_bulk_write_callback, 1464eae601eSAndrew Thompson .timeout = 10000, /* 10 seconds */ 14702ac6454SAndrew Thompson }, 14802ac6454SAndrew Thompson 14902ac6454SAndrew Thompson [RUE_BULK_DT_RD] = { 15002ac6454SAndrew Thompson .type = UE_BULK, 15102ac6454SAndrew Thompson .endpoint = UE_ADDR_ANY, 15202ac6454SAndrew Thompson .direction = UE_DIR_IN, 1534eae601eSAndrew Thompson .bufsize = (MCLBYTES + 4), 1544eae601eSAndrew Thompson .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 1554eae601eSAndrew Thompson .callback = rue_bulk_read_callback, 1564eae601eSAndrew Thompson .timeout = 0, /* no timeout */ 15702ac6454SAndrew Thompson }, 15802ac6454SAndrew Thompson 15902ac6454SAndrew Thompson [RUE_INTR_DT_RD] = { 16002ac6454SAndrew Thompson .type = UE_INTERRUPT, 16102ac6454SAndrew Thompson .endpoint = UE_ADDR_ANY, 16202ac6454SAndrew Thompson .direction = UE_DIR_IN, 1634eae601eSAndrew Thompson .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 1644eae601eSAndrew Thompson .bufsize = 0, /* use wMaxPacketSize */ 1654eae601eSAndrew Thompson .callback = rue_intr_callback, 16602ac6454SAndrew Thompson }, 16702ac6454SAndrew Thompson }; 16802ac6454SAndrew Thompson 16902ac6454SAndrew Thompson static device_method_t rue_methods[] = { 17002ac6454SAndrew Thompson /* Device interface */ 17102ac6454SAndrew Thompson DEVMETHOD(device_probe, rue_probe), 17202ac6454SAndrew Thompson DEVMETHOD(device_attach, rue_attach), 17302ac6454SAndrew Thompson DEVMETHOD(device_detach, rue_detach), 17402ac6454SAndrew Thompson 17502ac6454SAndrew Thompson /* Bus interface */ 17602ac6454SAndrew Thompson DEVMETHOD(bus_print_child, bus_generic_print_child), 17702ac6454SAndrew Thompson DEVMETHOD(bus_driver_added, bus_generic_driver_added), 17802ac6454SAndrew Thompson 17902ac6454SAndrew Thompson /* MII interface */ 18002ac6454SAndrew Thompson DEVMETHOD(miibus_readreg, rue_miibus_readreg), 18102ac6454SAndrew Thompson DEVMETHOD(miibus_writereg, rue_miibus_writereg), 18202ac6454SAndrew Thompson DEVMETHOD(miibus_statchg, rue_miibus_statchg), 18302ac6454SAndrew Thompson 18402ac6454SAndrew Thompson {0, 0} 18502ac6454SAndrew Thompson }; 18602ac6454SAndrew Thompson 18702ac6454SAndrew Thompson static driver_t rue_driver = { 18802ac6454SAndrew Thompson .name = "rue", 18902ac6454SAndrew Thompson .methods = rue_methods, 19002ac6454SAndrew Thompson .size = sizeof(struct rue_softc), 19102ac6454SAndrew Thompson }; 19202ac6454SAndrew Thompson 19302ac6454SAndrew Thompson static devclass_t rue_devclass; 19402ac6454SAndrew Thompson 1959aef556dSAndrew Thompson DRIVER_MODULE(rue, uhub, rue_driver, rue_devclass, NULL, 0); 19602ac6454SAndrew Thompson DRIVER_MODULE(miibus, rue, miibus_driver, miibus_devclass, 0, 0); 19702ac6454SAndrew Thompson MODULE_DEPEND(rue, uether, 1, 1, 1); 19802ac6454SAndrew Thompson MODULE_DEPEND(rue, usb, 1, 1, 1); 19902ac6454SAndrew Thompson MODULE_DEPEND(rue, ether, 1, 1, 1); 20002ac6454SAndrew Thompson MODULE_DEPEND(rue, miibus, 1, 1, 1); 20102ac6454SAndrew Thompson 202760bc48eSAndrew Thompson static const struct usb_ether_methods rue_ue_methods = { 20302ac6454SAndrew Thompson .ue_attach_post = rue_attach_post, 20402ac6454SAndrew Thompson .ue_start = rue_start, 20502ac6454SAndrew Thompson .ue_init = rue_init, 20602ac6454SAndrew Thompson .ue_stop = rue_stop, 20702ac6454SAndrew Thompson .ue_tick = rue_tick, 20802ac6454SAndrew Thompson .ue_setmulti = rue_setmulti, 20902ac6454SAndrew Thompson .ue_setpromisc = rue_setpromisc, 21002ac6454SAndrew Thompson .ue_mii_upd = rue_ifmedia_upd, 21102ac6454SAndrew Thompson .ue_mii_sts = rue_ifmedia_sts, 21202ac6454SAndrew Thompson }; 21302ac6454SAndrew Thompson 21402ac6454SAndrew Thompson #define RUE_SETBIT(sc, reg, x) \ 21502ac6454SAndrew Thompson rue_csr_write_1(sc, reg, rue_csr_read_1(sc, reg) | (x)) 21602ac6454SAndrew Thompson 21702ac6454SAndrew Thompson #define RUE_CLRBIT(sc, reg, x) \ 21802ac6454SAndrew Thompson rue_csr_write_1(sc, reg, rue_csr_read_1(sc, reg) & ~(x)) 21902ac6454SAndrew Thompson 22002ac6454SAndrew Thompson static int 22102ac6454SAndrew Thompson rue_read_mem(struct rue_softc *sc, uint16_t addr, void *buf, int len) 22202ac6454SAndrew Thompson { 223760bc48eSAndrew Thompson struct usb_device_request req; 22402ac6454SAndrew Thompson 22502ac6454SAndrew Thompson req.bmRequestType = UT_READ_VENDOR_DEVICE; 22602ac6454SAndrew Thompson req.bRequest = UR_SET_ADDRESS; 22702ac6454SAndrew Thompson USETW(req.wValue, addr); 22802ac6454SAndrew Thompson USETW(req.wIndex, 0); 22902ac6454SAndrew Thompson USETW(req.wLength, len); 23002ac6454SAndrew Thompson 23102ac6454SAndrew Thompson return (usb2_ether_do_request(&sc->sc_ue, &req, buf, 1000)); 23202ac6454SAndrew Thompson } 23302ac6454SAndrew Thompson 23402ac6454SAndrew Thompson static int 23502ac6454SAndrew Thompson rue_write_mem(struct rue_softc *sc, uint16_t addr, void *buf, int len) 23602ac6454SAndrew Thompson { 237760bc48eSAndrew Thompson struct usb_device_request req; 23802ac6454SAndrew Thompson 23902ac6454SAndrew Thompson req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 24002ac6454SAndrew Thompson req.bRequest = UR_SET_ADDRESS; 24102ac6454SAndrew Thompson USETW(req.wValue, addr); 24202ac6454SAndrew Thompson USETW(req.wIndex, 0); 24302ac6454SAndrew Thompson USETW(req.wLength, len); 24402ac6454SAndrew Thompson 24502ac6454SAndrew Thompson return (usb2_ether_do_request(&sc->sc_ue, &req, buf, 1000)); 24602ac6454SAndrew Thompson } 24702ac6454SAndrew Thompson 24802ac6454SAndrew Thompson static uint8_t 24902ac6454SAndrew Thompson rue_csr_read_1(struct rue_softc *sc, uint16_t reg) 25002ac6454SAndrew Thompson { 25102ac6454SAndrew Thompson uint8_t val; 25202ac6454SAndrew Thompson 25302ac6454SAndrew Thompson rue_read_mem(sc, reg, &val, 1); 25402ac6454SAndrew Thompson return (val); 25502ac6454SAndrew Thompson } 25602ac6454SAndrew Thompson 25702ac6454SAndrew Thompson static uint16_t 25802ac6454SAndrew Thompson rue_csr_read_2(struct rue_softc *sc, uint16_t reg) 25902ac6454SAndrew Thompson { 26002ac6454SAndrew Thompson uint8_t val[2]; 26102ac6454SAndrew Thompson 26202ac6454SAndrew Thompson rue_read_mem(sc, reg, &val, 2); 26302ac6454SAndrew Thompson return (UGETW(val)); 26402ac6454SAndrew Thompson } 26502ac6454SAndrew Thompson 26602ac6454SAndrew Thompson static int 26702ac6454SAndrew Thompson rue_csr_write_1(struct rue_softc *sc, uint16_t reg, uint8_t val) 26802ac6454SAndrew Thompson { 26902ac6454SAndrew Thompson return (rue_write_mem(sc, reg, &val, 1)); 27002ac6454SAndrew Thompson } 27102ac6454SAndrew Thompson 27202ac6454SAndrew Thompson static int 27302ac6454SAndrew Thompson rue_csr_write_2(struct rue_softc *sc, uint16_t reg, uint16_t val) 27402ac6454SAndrew Thompson { 27502ac6454SAndrew Thompson uint8_t temp[2]; 27602ac6454SAndrew Thompson 27702ac6454SAndrew Thompson USETW(temp, val); 27802ac6454SAndrew Thompson return (rue_write_mem(sc, reg, &temp, 2)); 27902ac6454SAndrew Thompson } 28002ac6454SAndrew Thompson 28102ac6454SAndrew Thompson static int 28202ac6454SAndrew Thompson rue_csr_write_4(struct rue_softc *sc, int reg, uint32_t val) 28302ac6454SAndrew Thompson { 28402ac6454SAndrew Thompson uint8_t temp[4]; 28502ac6454SAndrew Thompson 28602ac6454SAndrew Thompson USETDW(temp, val); 28702ac6454SAndrew Thompson return (rue_write_mem(sc, reg, &temp, 4)); 28802ac6454SAndrew Thompson } 28902ac6454SAndrew Thompson 29002ac6454SAndrew Thompson static int 29102ac6454SAndrew Thompson rue_miibus_readreg(device_t dev, int phy, int reg) 29202ac6454SAndrew Thompson { 29302ac6454SAndrew Thompson struct rue_softc *sc = device_get_softc(dev); 29402ac6454SAndrew Thompson uint16_t rval; 29502ac6454SAndrew Thompson uint16_t ruereg; 29602ac6454SAndrew Thompson int locked; 29702ac6454SAndrew Thompson 29802ac6454SAndrew Thompson if (phy != 0) /* RTL8150 supports PHY == 0, only */ 29902ac6454SAndrew Thompson return (0); 30002ac6454SAndrew Thompson 30102ac6454SAndrew Thompson locked = mtx_owned(&sc->sc_mtx); 30202ac6454SAndrew Thompson if (!locked) 30302ac6454SAndrew Thompson RUE_LOCK(sc); 30402ac6454SAndrew Thompson 30502ac6454SAndrew Thompson switch (reg) { 30602ac6454SAndrew Thompson case MII_BMCR: 30702ac6454SAndrew Thompson ruereg = RUE_BMCR; 30802ac6454SAndrew Thompson break; 30902ac6454SAndrew Thompson case MII_BMSR: 31002ac6454SAndrew Thompson ruereg = RUE_BMSR; 31102ac6454SAndrew Thompson break; 31202ac6454SAndrew Thompson case MII_ANAR: 31302ac6454SAndrew Thompson ruereg = RUE_ANAR; 31402ac6454SAndrew Thompson break; 31502ac6454SAndrew Thompson case MII_ANER: 31602ac6454SAndrew Thompson ruereg = RUE_AER; 31702ac6454SAndrew Thompson break; 31802ac6454SAndrew Thompson case MII_ANLPAR: 31902ac6454SAndrew Thompson ruereg = RUE_ANLP; 32002ac6454SAndrew Thompson break; 32102ac6454SAndrew Thompson case MII_PHYIDR1: 32202ac6454SAndrew Thompson case MII_PHYIDR2: 32302ac6454SAndrew Thompson rval = 0; 32402ac6454SAndrew Thompson goto done; 32502ac6454SAndrew Thompson default: 32602ac6454SAndrew Thompson if (RUE_REG_MIN <= reg && reg <= RUE_REG_MAX) { 32702ac6454SAndrew Thompson rval = rue_csr_read_1(sc, reg); 32802ac6454SAndrew Thompson goto done; 32902ac6454SAndrew Thompson } 33002ac6454SAndrew Thompson device_printf(sc->sc_ue.ue_dev, "bad phy register\n"); 33102ac6454SAndrew Thompson rval = 0; 33202ac6454SAndrew Thompson goto done; 33302ac6454SAndrew Thompson } 33402ac6454SAndrew Thompson 33502ac6454SAndrew Thompson rval = rue_csr_read_2(sc, ruereg); 33602ac6454SAndrew Thompson done: 33702ac6454SAndrew Thompson if (!locked) 33802ac6454SAndrew Thompson RUE_UNLOCK(sc); 33902ac6454SAndrew Thompson return (rval); 34002ac6454SAndrew Thompson } 34102ac6454SAndrew Thompson 34202ac6454SAndrew Thompson static int 34302ac6454SAndrew Thompson rue_miibus_writereg(device_t dev, int phy, int reg, int data) 34402ac6454SAndrew Thompson { 34502ac6454SAndrew Thompson struct rue_softc *sc = device_get_softc(dev); 34602ac6454SAndrew Thompson uint16_t ruereg; 34702ac6454SAndrew Thompson int locked; 34802ac6454SAndrew Thompson 34902ac6454SAndrew Thompson if (phy != 0) /* RTL8150 supports PHY == 0, only */ 35002ac6454SAndrew Thompson return (0); 35102ac6454SAndrew Thompson 35202ac6454SAndrew Thompson locked = mtx_owned(&sc->sc_mtx); 35302ac6454SAndrew Thompson if (!locked) 35402ac6454SAndrew Thompson RUE_LOCK(sc); 35502ac6454SAndrew Thompson 35602ac6454SAndrew Thompson switch (reg) { 35702ac6454SAndrew Thompson case MII_BMCR: 35802ac6454SAndrew Thompson ruereg = RUE_BMCR; 35902ac6454SAndrew Thompson break; 36002ac6454SAndrew Thompson case MII_BMSR: 36102ac6454SAndrew Thompson ruereg = RUE_BMSR; 36202ac6454SAndrew Thompson break; 36302ac6454SAndrew Thompson case MII_ANAR: 36402ac6454SAndrew Thompson ruereg = RUE_ANAR; 36502ac6454SAndrew Thompson break; 36602ac6454SAndrew Thompson case MII_ANER: 36702ac6454SAndrew Thompson ruereg = RUE_AER; 36802ac6454SAndrew Thompson break; 36902ac6454SAndrew Thompson case MII_ANLPAR: 37002ac6454SAndrew Thompson ruereg = RUE_ANLP; 37102ac6454SAndrew Thompson break; 37202ac6454SAndrew Thompson case MII_PHYIDR1: 37302ac6454SAndrew Thompson case MII_PHYIDR2: 37402ac6454SAndrew Thompson goto done; 37502ac6454SAndrew Thompson default: 37602ac6454SAndrew Thompson if (RUE_REG_MIN <= reg && reg <= RUE_REG_MAX) { 37702ac6454SAndrew Thompson rue_csr_write_1(sc, reg, data); 37802ac6454SAndrew Thompson goto done; 37902ac6454SAndrew Thompson } 38002ac6454SAndrew Thompson device_printf(sc->sc_ue.ue_dev, " bad phy register\n"); 38102ac6454SAndrew Thompson goto done; 38202ac6454SAndrew Thompson } 38302ac6454SAndrew Thompson rue_csr_write_2(sc, ruereg, data); 38402ac6454SAndrew Thompson done: 38502ac6454SAndrew Thompson if (!locked) 38602ac6454SAndrew Thompson RUE_UNLOCK(sc); 38702ac6454SAndrew Thompson return (0); 38802ac6454SAndrew Thompson } 38902ac6454SAndrew Thompson 39002ac6454SAndrew Thompson static void 39102ac6454SAndrew Thompson rue_miibus_statchg(device_t dev) 39202ac6454SAndrew Thompson { 39302ac6454SAndrew Thompson /* 39402ac6454SAndrew Thompson * When the code below is enabled the card starts doing weird 39502ac6454SAndrew Thompson * things after link going from UP to DOWN and back UP. 39602ac6454SAndrew Thompson * 39702ac6454SAndrew Thompson * Looks like some of register writes below messes up PHY 39802ac6454SAndrew Thompson * interface. 39902ac6454SAndrew Thompson * 40002ac6454SAndrew Thompson * No visible regressions were found after commenting this code 40102ac6454SAndrew Thompson * out, so that disable it for good. 40202ac6454SAndrew Thompson */ 40302ac6454SAndrew Thompson #if 0 40402ac6454SAndrew Thompson struct rue_softc *sc = device_get_softc(dev); 40502ac6454SAndrew Thompson struct mii_data *mii = GET_MII(sc); 40602ac6454SAndrew Thompson uint16_t bmcr; 40702ac6454SAndrew Thompson int locked; 40802ac6454SAndrew Thompson 40902ac6454SAndrew Thompson locked = mtx_owned(&sc->sc_mtx); 41002ac6454SAndrew Thompson if (!locked) 41102ac6454SAndrew Thompson RUE_LOCK(sc); 41202ac6454SAndrew Thompson 41302ac6454SAndrew Thompson RUE_CLRBIT(sc, RUE_CR, (RUE_CR_RE | RUE_CR_TE)); 41402ac6454SAndrew Thompson 41502ac6454SAndrew Thompson bmcr = rue_csr_read_2(sc, RUE_BMCR); 41602ac6454SAndrew Thompson 41702ac6454SAndrew Thompson if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX) 41802ac6454SAndrew Thompson bmcr |= RUE_BMCR_SPD_SET; 41902ac6454SAndrew Thompson else 42002ac6454SAndrew Thompson bmcr &= ~RUE_BMCR_SPD_SET; 42102ac6454SAndrew Thompson 42202ac6454SAndrew Thompson if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) 42302ac6454SAndrew Thompson bmcr |= RUE_BMCR_DUPLEX; 42402ac6454SAndrew Thompson else 42502ac6454SAndrew Thompson bmcr &= ~RUE_BMCR_DUPLEX; 42602ac6454SAndrew Thompson 42702ac6454SAndrew Thompson rue_csr_write_2(sc, RUE_BMCR, bmcr); 42802ac6454SAndrew Thompson 42902ac6454SAndrew Thompson RUE_SETBIT(sc, RUE_CR, (RUE_CR_RE | RUE_CR_TE)); 43002ac6454SAndrew Thompson 43102ac6454SAndrew Thompson if (!locked) 43202ac6454SAndrew Thompson RUE_UNLOCK(sc); 43302ac6454SAndrew Thompson #endif 43402ac6454SAndrew Thompson } 43502ac6454SAndrew Thompson 43602ac6454SAndrew Thompson static void 437760bc48eSAndrew Thompson rue_setpromisc(struct usb_ether *ue) 43802ac6454SAndrew Thompson { 43902ac6454SAndrew Thompson struct rue_softc *sc = usb2_ether_getsc(ue); 44002ac6454SAndrew Thompson struct ifnet *ifp = usb2_ether_getifp(ue); 44102ac6454SAndrew Thompson 44202ac6454SAndrew Thompson RUE_LOCK_ASSERT(sc, MA_OWNED); 44302ac6454SAndrew Thompson 44402ac6454SAndrew Thompson /* If we want promiscuous mode, set the allframes bit. */ 44502ac6454SAndrew Thompson if (ifp->if_flags & IFF_PROMISC) 44602ac6454SAndrew Thompson RUE_SETBIT(sc, RUE_RCR, RUE_RCR_AAP); 44702ac6454SAndrew Thompson else 44802ac6454SAndrew Thompson RUE_CLRBIT(sc, RUE_RCR, RUE_RCR_AAP); 44902ac6454SAndrew Thompson } 45002ac6454SAndrew Thompson 45102ac6454SAndrew Thompson /* 45202ac6454SAndrew Thompson * Program the 64-bit multicast hash filter. 45302ac6454SAndrew Thompson */ 45402ac6454SAndrew Thompson static void 455760bc48eSAndrew Thompson rue_setmulti(struct usb_ether *ue) 45602ac6454SAndrew Thompson { 45702ac6454SAndrew Thompson struct rue_softc *sc = usb2_ether_getsc(ue); 45802ac6454SAndrew Thompson struct ifnet *ifp = usb2_ether_getifp(ue); 45902ac6454SAndrew Thompson uint16_t rxcfg; 46002ac6454SAndrew Thompson int h = 0; 46102ac6454SAndrew Thompson uint32_t hashes[2] = { 0, 0 }; 46202ac6454SAndrew Thompson struct ifmultiaddr *ifma; 46302ac6454SAndrew Thompson int mcnt = 0; 46402ac6454SAndrew Thompson 46502ac6454SAndrew Thompson RUE_LOCK_ASSERT(sc, MA_OWNED); 46602ac6454SAndrew Thompson 46702ac6454SAndrew Thompson rxcfg = rue_csr_read_2(sc, RUE_RCR); 46802ac6454SAndrew Thompson 46902ac6454SAndrew Thompson if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) { 47002ac6454SAndrew Thompson rxcfg |= (RUE_RCR_AAM | RUE_RCR_AAP); 47102ac6454SAndrew Thompson rxcfg &= ~RUE_RCR_AM; 47202ac6454SAndrew Thompson rue_csr_write_2(sc, RUE_RCR, rxcfg); 47302ac6454SAndrew Thompson rue_csr_write_4(sc, RUE_MAR0, 0xFFFFFFFF); 47402ac6454SAndrew Thompson rue_csr_write_4(sc, RUE_MAR4, 0xFFFFFFFF); 47502ac6454SAndrew Thompson return; 47602ac6454SAndrew Thompson } 47702ac6454SAndrew Thompson 47802ac6454SAndrew Thompson /* first, zot all the existing hash bits */ 47902ac6454SAndrew Thompson rue_csr_write_4(sc, RUE_MAR0, 0); 48002ac6454SAndrew Thompson rue_csr_write_4(sc, RUE_MAR4, 0); 48102ac6454SAndrew Thompson 48202ac6454SAndrew Thompson /* now program new ones */ 48302ac6454SAndrew Thompson IF_ADDR_LOCK(ifp); 48402ac6454SAndrew Thompson TAILQ_FOREACH (ifma, &ifp->if_multiaddrs, ifma_link) 48502ac6454SAndrew Thompson { 48602ac6454SAndrew Thompson if (ifma->ifma_addr->sa_family != AF_LINK) 48702ac6454SAndrew Thompson continue; 48802ac6454SAndrew Thompson h = ether_crc32_be(LLADDR((struct sockaddr_dl *) 48902ac6454SAndrew Thompson ifma->ifma_addr), ETHER_ADDR_LEN) >> 26; 49002ac6454SAndrew Thompson if (h < 32) 49102ac6454SAndrew Thompson hashes[0] |= (1 << h); 49202ac6454SAndrew Thompson else 49302ac6454SAndrew Thompson hashes[1] |= (1 << (h - 32)); 49402ac6454SAndrew Thompson mcnt++; 49502ac6454SAndrew Thompson } 49602ac6454SAndrew Thompson IF_ADDR_UNLOCK(ifp); 49702ac6454SAndrew Thompson 49802ac6454SAndrew Thompson if (mcnt) 49902ac6454SAndrew Thompson rxcfg |= RUE_RCR_AM; 50002ac6454SAndrew Thompson else 50102ac6454SAndrew Thompson rxcfg &= ~RUE_RCR_AM; 50202ac6454SAndrew Thompson 50302ac6454SAndrew Thompson rxcfg &= ~(RUE_RCR_AAM | RUE_RCR_AAP); 50402ac6454SAndrew Thompson 50502ac6454SAndrew Thompson rue_csr_write_2(sc, RUE_RCR, rxcfg); 50602ac6454SAndrew Thompson rue_csr_write_4(sc, RUE_MAR0, hashes[0]); 50702ac6454SAndrew Thompson rue_csr_write_4(sc, RUE_MAR4, hashes[1]); 50802ac6454SAndrew Thompson } 50902ac6454SAndrew Thompson 51002ac6454SAndrew Thompson static void 51102ac6454SAndrew Thompson rue_reset(struct rue_softc *sc) 51202ac6454SAndrew Thompson { 51302ac6454SAndrew Thompson int i; 51402ac6454SAndrew Thompson 51502ac6454SAndrew Thompson rue_csr_write_1(sc, RUE_CR, RUE_CR_SOFT_RST); 51602ac6454SAndrew Thompson 51702ac6454SAndrew Thompson for (i = 0; i != RUE_TIMEOUT; i++) { 51802ac6454SAndrew Thompson if (usb2_ether_pause(&sc->sc_ue, hz / 1000)) 51902ac6454SAndrew Thompson break; 52002ac6454SAndrew Thompson if (!(rue_csr_read_1(sc, RUE_CR) & RUE_CR_SOFT_RST)) 52102ac6454SAndrew Thompson break; 52202ac6454SAndrew Thompson } 52302ac6454SAndrew Thompson if (i == RUE_TIMEOUT) 52402ac6454SAndrew Thompson device_printf(sc->sc_ue.ue_dev, "reset never completed!\n"); 52502ac6454SAndrew Thompson 52602ac6454SAndrew Thompson usb2_ether_pause(&sc->sc_ue, hz / 100); 52702ac6454SAndrew Thompson } 52802ac6454SAndrew Thompson 52902ac6454SAndrew Thompson static void 530760bc48eSAndrew Thompson rue_attach_post(struct usb_ether *ue) 53102ac6454SAndrew Thompson { 53202ac6454SAndrew Thompson struct rue_softc *sc = usb2_ether_getsc(ue); 53302ac6454SAndrew Thompson 53402ac6454SAndrew Thompson /* reset the adapter */ 53502ac6454SAndrew Thompson rue_reset(sc); 53602ac6454SAndrew Thompson 53702ac6454SAndrew Thompson /* get station address from the EEPROM */ 53802ac6454SAndrew Thompson rue_read_mem(sc, RUE_EEPROM_IDR0, ue->ue_eaddr, ETHER_ADDR_LEN); 53902ac6454SAndrew Thompson } 54002ac6454SAndrew Thompson 54102ac6454SAndrew Thompson /* 54202ac6454SAndrew Thompson * Probe for a RTL8150 chip. 54302ac6454SAndrew Thompson */ 54402ac6454SAndrew Thompson static int 54502ac6454SAndrew Thompson rue_probe(device_t dev) 54602ac6454SAndrew Thompson { 547760bc48eSAndrew Thompson struct usb_attach_arg *uaa = device_get_ivars(dev); 54802ac6454SAndrew Thompson 549f29a0724SAndrew Thompson if (uaa->usb_mode != USB_MODE_HOST) 55002ac6454SAndrew Thompson return (ENXIO); 55102ac6454SAndrew Thompson if (uaa->info.bConfigIndex != RUE_CONFIG_IDX) 55202ac6454SAndrew Thompson return (ENXIO); 55302ac6454SAndrew Thompson if (uaa->info.bIfaceIndex != RUE_IFACE_IDX) 55402ac6454SAndrew Thompson return (ENXIO); 55502ac6454SAndrew Thompson 55602ac6454SAndrew Thompson return (usb2_lookup_id_by_uaa(rue_devs, sizeof(rue_devs), uaa)); 55702ac6454SAndrew Thompson } 55802ac6454SAndrew Thompson 55902ac6454SAndrew Thompson /* 56002ac6454SAndrew Thompson * Attach the interface. Allocate softc structures, do ifmedia 56102ac6454SAndrew Thompson * setup and ethernet/BPF attach. 56202ac6454SAndrew Thompson */ 56302ac6454SAndrew Thompson static int 56402ac6454SAndrew Thompson rue_attach(device_t dev) 56502ac6454SAndrew Thompson { 566760bc48eSAndrew Thompson struct usb_attach_arg *uaa = device_get_ivars(dev); 56702ac6454SAndrew Thompson struct rue_softc *sc = device_get_softc(dev); 568760bc48eSAndrew Thompson struct usb_ether *ue = &sc->sc_ue; 56902ac6454SAndrew Thompson uint8_t iface_index; 57002ac6454SAndrew Thompson int error; 57102ac6454SAndrew Thompson 57202ac6454SAndrew Thompson device_set_usb2_desc(dev); 57302ac6454SAndrew Thompson mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF); 57402ac6454SAndrew Thompson 57502ac6454SAndrew Thompson iface_index = RUE_IFACE_IDX; 57602ac6454SAndrew Thompson error = usb2_transfer_setup(uaa->device, &iface_index, 57702ac6454SAndrew Thompson sc->sc_xfer, rue_config, RUE_N_TRANSFER, 57802ac6454SAndrew Thompson sc, &sc->sc_mtx); 57902ac6454SAndrew Thompson if (error) { 58002ac6454SAndrew Thompson device_printf(dev, "allocating USB transfers failed!\n"); 58102ac6454SAndrew Thompson goto detach; 58202ac6454SAndrew Thompson } 58302ac6454SAndrew Thompson 58402ac6454SAndrew Thompson ue->ue_sc = sc; 58502ac6454SAndrew Thompson ue->ue_dev = dev; 58602ac6454SAndrew Thompson ue->ue_udev = uaa->device; 58702ac6454SAndrew Thompson ue->ue_mtx = &sc->sc_mtx; 58802ac6454SAndrew Thompson ue->ue_methods = &rue_ue_methods; 58902ac6454SAndrew Thompson 59002ac6454SAndrew Thompson error = usb2_ether_ifattach(ue); 59102ac6454SAndrew Thompson if (error) { 59202ac6454SAndrew Thompson device_printf(dev, "could not attach interface\n"); 59302ac6454SAndrew Thompson goto detach; 59402ac6454SAndrew Thompson } 59502ac6454SAndrew Thompson return (0); /* success */ 59602ac6454SAndrew Thompson 59702ac6454SAndrew Thompson detach: 59802ac6454SAndrew Thompson rue_detach(dev); 59902ac6454SAndrew Thompson return (ENXIO); /* failure */ 60002ac6454SAndrew Thompson } 60102ac6454SAndrew Thompson 60202ac6454SAndrew Thompson static int 60302ac6454SAndrew Thompson rue_detach(device_t dev) 60402ac6454SAndrew Thompson { 60502ac6454SAndrew Thompson struct rue_softc *sc = device_get_softc(dev); 606760bc48eSAndrew Thompson struct usb_ether *ue = &sc->sc_ue; 60702ac6454SAndrew Thompson 60802ac6454SAndrew Thompson usb2_transfer_unsetup(sc->sc_xfer, RUE_N_TRANSFER); 60902ac6454SAndrew Thompson usb2_ether_ifdetach(ue); 61002ac6454SAndrew Thompson mtx_destroy(&sc->sc_mtx); 61102ac6454SAndrew Thompson 61202ac6454SAndrew Thompson return (0); 61302ac6454SAndrew Thompson } 61402ac6454SAndrew Thompson 61502ac6454SAndrew Thompson static void 616760bc48eSAndrew Thompson rue_intr_callback(struct usb_xfer *xfer) 61702ac6454SAndrew Thompson { 61802ac6454SAndrew Thompson struct rue_softc *sc = xfer->priv_sc; 61902ac6454SAndrew Thompson struct ifnet *ifp = usb2_ether_getifp(&sc->sc_ue); 62002ac6454SAndrew Thompson struct rue_intrpkt pkt; 62102ac6454SAndrew Thompson 62202ac6454SAndrew Thompson switch (USB_GET_STATE(xfer)) { 62302ac6454SAndrew Thompson case USB_ST_TRANSFERRED: 62402ac6454SAndrew Thompson 62502ac6454SAndrew Thompson if (ifp && (ifp->if_drv_flags & IFF_DRV_RUNNING) && 62602ac6454SAndrew Thompson (xfer->actlen >= sizeof(pkt))) { 62702ac6454SAndrew Thompson 62802ac6454SAndrew Thompson usb2_copy_out(xfer->frbuffers, 0, &pkt, sizeof(pkt)); 62902ac6454SAndrew Thompson 63002ac6454SAndrew Thompson ifp->if_ierrors += pkt.rue_rxlost_cnt; 63102ac6454SAndrew Thompson ifp->if_ierrors += pkt.rue_crcerr_cnt; 63202ac6454SAndrew Thompson ifp->if_collisions += pkt.rue_col_cnt; 63302ac6454SAndrew Thompson } 63402ac6454SAndrew Thompson /* FALLTHROUGH */ 63502ac6454SAndrew Thompson case USB_ST_SETUP: 63602ac6454SAndrew Thompson tr_setup: 63702ac6454SAndrew Thompson xfer->frlengths[0] = xfer->max_data_length; 63802ac6454SAndrew Thompson usb2_start_hardware(xfer); 63902ac6454SAndrew Thompson return; 64002ac6454SAndrew Thompson 64102ac6454SAndrew Thompson default: /* Error */ 64202ac6454SAndrew Thompson if (xfer->error != USB_ERR_CANCELLED) { 64302ac6454SAndrew Thompson /* try to clear stall first */ 64402ac6454SAndrew Thompson xfer->flags.stall_pipe = 1; 64502ac6454SAndrew Thompson goto tr_setup; 64602ac6454SAndrew Thompson } 64702ac6454SAndrew Thompson return; 64802ac6454SAndrew Thompson } 64902ac6454SAndrew Thompson } 65002ac6454SAndrew Thompson 65102ac6454SAndrew Thompson static void 652760bc48eSAndrew Thompson rue_bulk_read_callback(struct usb_xfer *xfer) 65302ac6454SAndrew Thompson { 65402ac6454SAndrew Thompson struct rue_softc *sc = xfer->priv_sc; 655760bc48eSAndrew Thompson struct usb_ether *ue = &sc->sc_ue; 65602ac6454SAndrew Thompson struct ifnet *ifp = usb2_ether_getifp(ue); 65702ac6454SAndrew Thompson uint16_t status; 65802ac6454SAndrew Thompson 65902ac6454SAndrew Thompson switch (USB_GET_STATE(xfer)) { 66002ac6454SAndrew Thompson case USB_ST_TRANSFERRED: 66102ac6454SAndrew Thompson 66202ac6454SAndrew Thompson if (xfer->actlen < 4) { 66302ac6454SAndrew Thompson ifp->if_ierrors++; 66402ac6454SAndrew Thompson goto tr_setup; 66502ac6454SAndrew Thompson } 66602ac6454SAndrew Thompson usb2_copy_out(xfer->frbuffers, xfer->actlen - 4, 66702ac6454SAndrew Thompson &status, sizeof(status)); 66802ac6454SAndrew Thompson xfer->actlen -= 4; 66902ac6454SAndrew Thompson 67002ac6454SAndrew Thompson /* check recieve packet was valid or not */ 67102ac6454SAndrew Thompson status = le16toh(status); 67202ac6454SAndrew Thompson if ((status & RUE_RXSTAT_VALID) == 0) { 67302ac6454SAndrew Thompson ifp->if_ierrors++; 67402ac6454SAndrew Thompson goto tr_setup; 67502ac6454SAndrew Thompson } 67602ac6454SAndrew Thompson usb2_ether_rxbuf(ue, xfer->frbuffers, 0, xfer->actlen); 67702ac6454SAndrew Thompson /* FALLTHROUGH */ 67802ac6454SAndrew Thompson case USB_ST_SETUP: 67902ac6454SAndrew Thompson tr_setup: 68002ac6454SAndrew Thompson xfer->frlengths[0] = xfer->max_data_length; 68102ac6454SAndrew Thompson usb2_start_hardware(xfer); 68202ac6454SAndrew Thompson usb2_ether_rxflush(ue); 68302ac6454SAndrew Thompson return; 68402ac6454SAndrew Thompson 68502ac6454SAndrew Thompson default: /* Error */ 68602ac6454SAndrew Thompson DPRINTF("bulk read error, %s\n", 68702ac6454SAndrew Thompson usb2_errstr(xfer->error)); 68802ac6454SAndrew Thompson 68902ac6454SAndrew Thompson if (xfer->error != USB_ERR_CANCELLED) { 69002ac6454SAndrew Thompson /* try to clear stall first */ 69102ac6454SAndrew Thompson xfer->flags.stall_pipe = 1; 69202ac6454SAndrew Thompson goto tr_setup; 69302ac6454SAndrew Thompson } 69402ac6454SAndrew Thompson return; 69502ac6454SAndrew Thompson } 69602ac6454SAndrew Thompson } 69702ac6454SAndrew Thompson 69802ac6454SAndrew Thompson static void 699760bc48eSAndrew Thompson rue_bulk_write_callback(struct usb_xfer *xfer) 70002ac6454SAndrew Thompson { 70102ac6454SAndrew Thompson struct rue_softc *sc = xfer->priv_sc; 70202ac6454SAndrew Thompson struct ifnet *ifp = usb2_ether_getifp(&sc->sc_ue); 70302ac6454SAndrew Thompson struct mbuf *m; 70402ac6454SAndrew Thompson int temp_len; 70502ac6454SAndrew Thompson 70602ac6454SAndrew Thompson switch (USB_GET_STATE(xfer)) { 70702ac6454SAndrew Thompson case USB_ST_TRANSFERRED: 70802ac6454SAndrew Thompson DPRINTFN(11, "transfer complete\n"); 70902ac6454SAndrew Thompson ifp->if_opackets++; 71002ac6454SAndrew Thompson 71102ac6454SAndrew Thompson /* FALLTHROUGH */ 71202ac6454SAndrew Thompson case USB_ST_SETUP: 71302ac6454SAndrew Thompson tr_setup: 71402ac6454SAndrew Thompson if ((sc->sc_flags & RUE_FLAG_LINK) == 0) { 71502ac6454SAndrew Thompson /* 71602ac6454SAndrew Thompson * don't send anything if there is no link ! 71702ac6454SAndrew Thompson */ 71802ac6454SAndrew Thompson return; 71902ac6454SAndrew Thompson } 72002ac6454SAndrew Thompson IFQ_DRV_DEQUEUE(&ifp->if_snd, m); 72102ac6454SAndrew Thompson 72202ac6454SAndrew Thompson if (m == NULL) 72302ac6454SAndrew Thompson return; 72402ac6454SAndrew Thompson if (m->m_pkthdr.len > MCLBYTES) 72502ac6454SAndrew Thompson m->m_pkthdr.len = MCLBYTES; 72602ac6454SAndrew Thompson temp_len = m->m_pkthdr.len; 72702ac6454SAndrew Thompson 72802ac6454SAndrew Thompson usb2_m_copy_in(xfer->frbuffers, 0, 72902ac6454SAndrew Thompson m, 0, m->m_pkthdr.len); 73002ac6454SAndrew Thompson 73102ac6454SAndrew Thompson /* 73202ac6454SAndrew Thompson * This is an undocumented behavior. 73302ac6454SAndrew Thompson * RTL8150 chip doesn't send frame length smaller than 73402ac6454SAndrew Thompson * RUE_MIN_FRAMELEN (60) byte packet. 73502ac6454SAndrew Thompson */ 73602ac6454SAndrew Thompson if (temp_len < RUE_MIN_FRAMELEN) { 73702ac6454SAndrew Thompson usb2_bzero(xfer->frbuffers, temp_len, 73802ac6454SAndrew Thompson RUE_MIN_FRAMELEN - temp_len); 73902ac6454SAndrew Thompson temp_len = RUE_MIN_FRAMELEN; 74002ac6454SAndrew Thompson } 74102ac6454SAndrew Thompson xfer->frlengths[0] = temp_len; 74202ac6454SAndrew Thompson 74302ac6454SAndrew Thompson /* 74402ac6454SAndrew Thompson * if there's a BPF listener, bounce a copy 74502ac6454SAndrew Thompson * of this frame to him: 74602ac6454SAndrew Thompson */ 74702ac6454SAndrew Thompson BPF_MTAP(ifp, m); 74802ac6454SAndrew Thompson 74902ac6454SAndrew Thompson m_freem(m); 75002ac6454SAndrew Thompson 75102ac6454SAndrew Thompson usb2_start_hardware(xfer); 75202ac6454SAndrew Thompson 75302ac6454SAndrew Thompson return; 75402ac6454SAndrew Thompson 75502ac6454SAndrew Thompson default: /* Error */ 75602ac6454SAndrew Thompson DPRINTFN(11, "transfer error, %s\n", 75702ac6454SAndrew Thompson usb2_errstr(xfer->error)); 75802ac6454SAndrew Thompson 75902ac6454SAndrew Thompson ifp->if_oerrors++; 76002ac6454SAndrew Thompson 76102ac6454SAndrew Thompson if (xfer->error != USB_ERR_CANCELLED) { 76202ac6454SAndrew Thompson /* try to clear stall first */ 76302ac6454SAndrew Thompson xfer->flags.stall_pipe = 1; 76402ac6454SAndrew Thompson goto tr_setup; 76502ac6454SAndrew Thompson } 76602ac6454SAndrew Thompson return; 76702ac6454SAndrew Thompson } 76802ac6454SAndrew Thompson } 76902ac6454SAndrew Thompson 77002ac6454SAndrew Thompson static void 771760bc48eSAndrew Thompson rue_tick(struct usb_ether *ue) 77202ac6454SAndrew Thompson { 77302ac6454SAndrew Thompson struct rue_softc *sc = usb2_ether_getsc(ue); 77402ac6454SAndrew Thompson struct mii_data *mii = GET_MII(sc); 77502ac6454SAndrew Thompson 77602ac6454SAndrew Thompson RUE_LOCK_ASSERT(sc, MA_OWNED); 77702ac6454SAndrew Thompson 77802ac6454SAndrew Thompson mii_tick(mii); 77902ac6454SAndrew Thompson if ((sc->sc_flags & RUE_FLAG_LINK) == 0 78002ac6454SAndrew Thompson && mii->mii_media_status & IFM_ACTIVE && 78102ac6454SAndrew Thompson IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) { 78202ac6454SAndrew Thompson sc->sc_flags |= RUE_FLAG_LINK; 78302ac6454SAndrew Thompson rue_start(ue); 78402ac6454SAndrew Thompson } 78502ac6454SAndrew Thompson } 78602ac6454SAndrew Thompson 78702ac6454SAndrew Thompson static void 788760bc48eSAndrew Thompson rue_start(struct usb_ether *ue) 78902ac6454SAndrew Thompson { 79002ac6454SAndrew Thompson struct rue_softc *sc = usb2_ether_getsc(ue); 79102ac6454SAndrew Thompson 79202ac6454SAndrew Thompson /* 79302ac6454SAndrew Thompson * start the USB transfers, if not already started: 79402ac6454SAndrew Thompson */ 79502ac6454SAndrew Thompson usb2_transfer_start(sc->sc_xfer[RUE_INTR_DT_RD]); 79602ac6454SAndrew Thompson usb2_transfer_start(sc->sc_xfer[RUE_BULK_DT_RD]); 79702ac6454SAndrew Thompson usb2_transfer_start(sc->sc_xfer[RUE_BULK_DT_WR]); 79802ac6454SAndrew Thompson } 79902ac6454SAndrew Thompson 80002ac6454SAndrew Thompson static void 801760bc48eSAndrew Thompson rue_init(struct usb_ether *ue) 80202ac6454SAndrew Thompson { 80302ac6454SAndrew Thompson struct rue_softc *sc = usb2_ether_getsc(ue); 80402ac6454SAndrew Thompson struct ifnet *ifp = usb2_ether_getifp(ue); 80502ac6454SAndrew Thompson 80602ac6454SAndrew Thompson RUE_LOCK_ASSERT(sc, MA_OWNED); 80702ac6454SAndrew Thompson 80802ac6454SAndrew Thompson /* 80902ac6454SAndrew Thompson * Cancel pending I/O 81002ac6454SAndrew Thompson */ 81102ac6454SAndrew Thompson rue_reset(sc); 81202ac6454SAndrew Thompson 81302ac6454SAndrew Thompson /* Set MAC address */ 81402ac6454SAndrew Thompson rue_write_mem(sc, RUE_IDR0, IF_LLADDR(ifp), ETHER_ADDR_LEN); 81502ac6454SAndrew Thompson 81602ac6454SAndrew Thompson rue_stop(ue); 81702ac6454SAndrew Thompson 81802ac6454SAndrew Thompson /* 81902ac6454SAndrew Thompson * Set the initial TX and RX configuration. 82002ac6454SAndrew Thompson */ 82102ac6454SAndrew Thompson rue_csr_write_1(sc, RUE_TCR, RUE_TCR_CONFIG); 82202ac6454SAndrew Thompson rue_csr_write_2(sc, RUE_RCR, RUE_RCR_CONFIG|RUE_RCR_AB); 82302ac6454SAndrew Thompson 82402ac6454SAndrew Thompson /* Load the multicast filter */ 82502ac6454SAndrew Thompson rue_setpromisc(ue); 82602ac6454SAndrew Thompson /* Load the multicast filter. */ 82702ac6454SAndrew Thompson rue_setmulti(ue); 82802ac6454SAndrew Thompson 82902ac6454SAndrew Thompson /* Enable RX and TX */ 83002ac6454SAndrew Thompson rue_csr_write_1(sc, RUE_CR, (RUE_CR_TE | RUE_CR_RE | RUE_CR_EP3CLREN)); 83102ac6454SAndrew Thompson 83202ac6454SAndrew Thompson usb2_transfer_set_stall(sc->sc_xfer[RUE_BULK_DT_WR]); 83302ac6454SAndrew Thompson 83402ac6454SAndrew Thompson ifp->if_drv_flags |= IFF_DRV_RUNNING; 83502ac6454SAndrew Thompson rue_start(ue); 83602ac6454SAndrew Thompson } 83702ac6454SAndrew Thompson 83802ac6454SAndrew Thompson /* 83902ac6454SAndrew Thompson * Set media options. 84002ac6454SAndrew Thompson */ 84102ac6454SAndrew Thompson static int 84202ac6454SAndrew Thompson rue_ifmedia_upd(struct ifnet *ifp) 84302ac6454SAndrew Thompson { 84402ac6454SAndrew Thompson struct rue_softc *sc = ifp->if_softc; 84502ac6454SAndrew Thompson struct mii_data *mii = GET_MII(sc); 84602ac6454SAndrew Thompson 84702ac6454SAndrew Thompson RUE_LOCK_ASSERT(sc, MA_OWNED); 84802ac6454SAndrew Thompson 84902ac6454SAndrew Thompson sc->sc_flags &= ~RUE_FLAG_LINK; 85002ac6454SAndrew Thompson if (mii->mii_instance) { 85102ac6454SAndrew Thompson struct mii_softc *miisc; 85202ac6454SAndrew Thompson 85302ac6454SAndrew Thompson LIST_FOREACH(miisc, &mii->mii_phys, mii_list) 85402ac6454SAndrew Thompson mii_phy_reset(miisc); 85502ac6454SAndrew Thompson } 85602ac6454SAndrew Thompson mii_mediachg(mii); 85702ac6454SAndrew Thompson return (0); 85802ac6454SAndrew Thompson } 85902ac6454SAndrew Thompson 86002ac6454SAndrew Thompson /* 86102ac6454SAndrew Thompson * Report current media status. 86202ac6454SAndrew Thompson */ 86302ac6454SAndrew Thompson static void 86402ac6454SAndrew Thompson rue_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr) 86502ac6454SAndrew Thompson { 86602ac6454SAndrew Thompson struct rue_softc *sc = ifp->if_softc; 86702ac6454SAndrew Thompson struct mii_data *mii = GET_MII(sc); 86802ac6454SAndrew Thompson 86902ac6454SAndrew Thompson RUE_LOCK(sc); 87002ac6454SAndrew Thompson mii_pollstat(mii); 87102ac6454SAndrew Thompson RUE_UNLOCK(sc); 87202ac6454SAndrew Thompson ifmr->ifm_active = mii->mii_media_active; 87302ac6454SAndrew Thompson ifmr->ifm_status = mii->mii_media_status; 87402ac6454SAndrew Thompson } 87502ac6454SAndrew Thompson 87602ac6454SAndrew Thompson static void 877760bc48eSAndrew Thompson rue_stop(struct usb_ether *ue) 87802ac6454SAndrew Thompson { 87902ac6454SAndrew Thompson struct rue_softc *sc = usb2_ether_getsc(ue); 88002ac6454SAndrew Thompson struct ifnet *ifp = usb2_ether_getifp(ue); 88102ac6454SAndrew Thompson 88202ac6454SAndrew Thompson RUE_LOCK_ASSERT(sc, MA_OWNED); 88302ac6454SAndrew Thompson 88402ac6454SAndrew Thompson ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 88502ac6454SAndrew Thompson sc->sc_flags &= ~RUE_FLAG_LINK; 88602ac6454SAndrew Thompson 88702ac6454SAndrew Thompson /* 88802ac6454SAndrew Thompson * stop all the transfers, if not already stopped: 88902ac6454SAndrew Thompson */ 89002ac6454SAndrew Thompson usb2_transfer_stop(sc->sc_xfer[RUE_BULK_DT_WR]); 89102ac6454SAndrew Thompson usb2_transfer_stop(sc->sc_xfer[RUE_BULK_DT_RD]); 89202ac6454SAndrew Thompson usb2_transfer_stop(sc->sc_xfer[RUE_INTR_DT_RD]); 89302ac6454SAndrew Thompson 89402ac6454SAndrew Thompson rue_csr_write_1(sc, RUE_CR, 0x00); 89502ac6454SAndrew Thompson 89602ac6454SAndrew Thompson rue_reset(sc); 89702ac6454SAndrew Thompson } 898