xref: /freebsd/sys/dev/usb/net/if_rue.c (revision 685dc743)
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 /*-
284d846d26SWarner Losh  * SPDX-License-Identifier: BSD-4-Clause AND BSD-2-Clause
29df57947fSPedro F. Giffuni  *
3002ac6454SAndrew Thompson  * Copyright (c) 1997, 1998, 1999, 2000
3102ac6454SAndrew Thompson  *	Bill Paul <wpaul@ee.columbia.edu>.  All rights reserved.
3202ac6454SAndrew Thompson  *
3302ac6454SAndrew Thompson  * Redistribution and use in source and binary forms, with or without
3402ac6454SAndrew Thompson  * modification, are permitted provided that the following conditions
3502ac6454SAndrew Thompson  * are met:
3602ac6454SAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
3702ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer.
3802ac6454SAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
3902ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
4002ac6454SAndrew Thompson  *    documentation and/or other materials provided with the distribution.
4102ac6454SAndrew Thompson  * 3. All advertising materials mentioning features or use of this software
4202ac6454SAndrew Thompson  *    must display the following acknowledgement:
4302ac6454SAndrew Thompson  *	This product includes software developed by Bill Paul.
4402ac6454SAndrew Thompson  * 4. Neither the name of the author nor the names of any co-contributors
4502ac6454SAndrew Thompson  *    may be used to endorse or promote products derived from this software
4602ac6454SAndrew Thompson  *    without specific prior written permission.
4702ac6454SAndrew Thompson  *
4802ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
4902ac6454SAndrew Thompson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
5002ac6454SAndrew Thompson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
5102ac6454SAndrew Thompson  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
5202ac6454SAndrew Thompson  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
5302ac6454SAndrew Thompson  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
5402ac6454SAndrew Thompson  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
5502ac6454SAndrew Thompson  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
5602ac6454SAndrew Thompson  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
5702ac6454SAndrew Thompson  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
5802ac6454SAndrew Thompson  * THE POSSIBILITY OF SUCH DAMAGE.
5902ac6454SAndrew Thompson  */
6002ac6454SAndrew Thompson 
6102ac6454SAndrew Thompson #include <sys/cdefs.h>
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 
68ed6d949aSAndrew Thompson #include <sys/stdint.h>
69ed6d949aSAndrew Thompson #include <sys/stddef.h>
70ed6d949aSAndrew Thompson #include <sys/param.h>
71ed6d949aSAndrew Thompson #include <sys/queue.h>
72ed6d949aSAndrew Thompson #include <sys/types.h>
73ed6d949aSAndrew Thompson #include <sys/systm.h>
7476039bc8SGleb Smirnoff #include <sys/socket.h>
75ed6d949aSAndrew Thompson #include <sys/kernel.h>
76ed6d949aSAndrew Thompson #include <sys/bus.h>
77ed6d949aSAndrew Thompson #include <sys/module.h>
78ed6d949aSAndrew Thompson #include <sys/lock.h>
79ed6d949aSAndrew Thompson #include <sys/mutex.h>
80ed6d949aSAndrew Thompson #include <sys/condvar.h>
81ed6d949aSAndrew Thompson #include <sys/sysctl.h>
82ed6d949aSAndrew Thompson #include <sys/sx.h>
83ed6d949aSAndrew Thompson #include <sys/unistd.h>
84ed6d949aSAndrew Thompson #include <sys/callout.h>
85ed6d949aSAndrew Thompson #include <sys/malloc.h>
86ed6d949aSAndrew Thompson #include <sys/priv.h>
87ed6d949aSAndrew Thompson 
8876039bc8SGleb Smirnoff #include <net/if.h>
8976039bc8SGleb Smirnoff #include <net/if_var.h>
9031c484adSJustin Hibbits #include <net/if_media.h>
9131c484adSJustin Hibbits 
9231c484adSJustin Hibbits #include <dev/mii/mii.h>
9331c484adSJustin Hibbits #include <dev/mii/miivar.h>
9476039bc8SGleb Smirnoff 
9502ac6454SAndrew Thompson #include <dev/usb/usb.h>
96ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h>
97ed6d949aSAndrew Thompson #include <dev/usb/usbdi_util.h>
98ed6d949aSAndrew Thompson #include "usbdevs.h"
9902ac6454SAndrew Thompson 
10002ac6454SAndrew Thompson #define	USB_DEBUG_VAR rue_debug
10102ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
102ed6d949aSAndrew Thompson #include <dev/usb/usb_process.h>
10302ac6454SAndrew Thompson 
10402ac6454SAndrew Thompson #include <dev/usb/net/usb_ethernet.h>
10502ac6454SAndrew Thompson #include <dev/usb/net/if_ruereg.h>
10602ac6454SAndrew Thompson 
10731c484adSJustin Hibbits #include "miibus_if.h"
10831c484adSJustin Hibbits 
109b850ecc1SAndrew Thompson #ifdef USB_DEBUG
11002ac6454SAndrew Thompson static int rue_debug = 0;
11102ac6454SAndrew Thompson 
112f8d2b1f3SPawel Biernacki static SYSCTL_NODE(_hw_usb, OID_AUTO, rue, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
113f8d2b1f3SPawel Biernacki     "USB rue");
114ece4b0bdSHans Petter Selasky SYSCTL_INT(_hw_usb_rue, OID_AUTO, debug, CTLFLAG_RWTUN,
11502ac6454SAndrew Thompson     &rue_debug, 0, "Debug level");
11602ac6454SAndrew Thompson #endif
11702ac6454SAndrew Thompson 
11802ac6454SAndrew Thompson /*
11902ac6454SAndrew Thompson  * Various supported device vendors/products.
12002ac6454SAndrew Thompson  */
12102ac6454SAndrew Thompson 
122f1a16106SHans Petter Selasky static const STRUCT_USB_HOST_ID rue_devs[] = {
12302ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUAKTX, 0)},
12402ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_REALTEK, USB_PRODUCT_REALTEK_USBKR100, 0)},
1258f55d259SAndrew Thompson 	{USB_VPI(USB_VENDOR_OQO, USB_PRODUCT_OQO_ETHER01, 0)},
12602ac6454SAndrew Thompson };
12702ac6454SAndrew Thompson 
12802ac6454SAndrew Thompson /* prototypes */
12902ac6454SAndrew Thompson 
13002ac6454SAndrew Thompson static device_probe_t rue_probe;
13102ac6454SAndrew Thompson static device_attach_t rue_attach;
13202ac6454SAndrew Thompson static device_detach_t rue_detach;
13302ac6454SAndrew Thompson 
13402ac6454SAndrew Thompson static miibus_readreg_t rue_miibus_readreg;
13502ac6454SAndrew Thompson static miibus_writereg_t rue_miibus_writereg;
13602ac6454SAndrew Thompson static miibus_statchg_t rue_miibus_statchg;
13702ac6454SAndrew Thompson 
138e0a69b51SAndrew Thompson static usb_callback_t rue_intr_callback;
139e0a69b51SAndrew Thompson static usb_callback_t rue_bulk_read_callback;
140e0a69b51SAndrew Thompson static usb_callback_t rue_bulk_write_callback;
14102ac6454SAndrew Thompson 
142e0a69b51SAndrew Thompson static uether_fn_t rue_attach_post;
143e0a69b51SAndrew Thompson static uether_fn_t rue_init;
144e0a69b51SAndrew Thompson static uether_fn_t rue_stop;
145e0a69b51SAndrew Thompson static uether_fn_t rue_start;
146e0a69b51SAndrew Thompson static uether_fn_t rue_tick;
147e0a69b51SAndrew Thompson static uether_fn_t rue_setmulti;
148e0a69b51SAndrew Thompson static uether_fn_t rue_setpromisc;
14902ac6454SAndrew Thompson 
15002ac6454SAndrew Thompson static int	rue_read_mem(struct rue_softc *, uint16_t, void *, int);
15102ac6454SAndrew Thompson static int	rue_write_mem(struct rue_softc *, uint16_t, void *, int);
15202ac6454SAndrew Thompson static uint8_t	rue_csr_read_1(struct rue_softc *, uint16_t);
15302ac6454SAndrew Thompson static uint16_t	rue_csr_read_2(struct rue_softc *, uint16_t);
15402ac6454SAndrew Thompson static int	rue_csr_write_1(struct rue_softc *, uint16_t, uint8_t);
15502ac6454SAndrew Thompson static int	rue_csr_write_2(struct rue_softc *, uint16_t, uint16_t);
15602ac6454SAndrew Thompson static int	rue_csr_write_4(struct rue_softc *, int, uint32_t);
15702ac6454SAndrew Thompson 
15802ac6454SAndrew Thompson static void	rue_reset(struct rue_softc *);
159935b194dSJustin Hibbits static int	rue_ifmedia_upd(if_t);
160935b194dSJustin Hibbits static void	rue_ifmedia_sts(if_t, struct ifmediareq *);
16102ac6454SAndrew Thompson 
162760bc48eSAndrew Thompson static const struct usb_config rue_config[RUE_N_TRANSFER] = {
16302ac6454SAndrew Thompson 	[RUE_BULK_DT_WR] = {
16402ac6454SAndrew Thompson 		.type = UE_BULK,
16502ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
16602ac6454SAndrew Thompson 		.direction = UE_DIR_OUT,
1674eae601eSAndrew Thompson 		.bufsize = MCLBYTES,
1684eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
1694eae601eSAndrew Thompson 		.callback = rue_bulk_write_callback,
1704eae601eSAndrew Thompson 		.timeout = 10000,	/* 10 seconds */
17102ac6454SAndrew Thompson 	},
17202ac6454SAndrew Thompson 
17302ac6454SAndrew Thompson 	[RUE_BULK_DT_RD] = {
17402ac6454SAndrew Thompson 		.type = UE_BULK,
17502ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
17602ac6454SAndrew Thompson 		.direction = UE_DIR_IN,
1774eae601eSAndrew Thompson 		.bufsize = (MCLBYTES + 4),
1784eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
1794eae601eSAndrew Thompson 		.callback = rue_bulk_read_callback,
1804eae601eSAndrew Thompson 		.timeout = 0,	/* no timeout */
18102ac6454SAndrew Thompson 	},
18202ac6454SAndrew Thompson 
18302ac6454SAndrew Thompson 	[RUE_INTR_DT_RD] = {
18402ac6454SAndrew Thompson 		.type = UE_INTERRUPT,
18502ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
18602ac6454SAndrew Thompson 		.direction = UE_DIR_IN,
1874eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
1884eae601eSAndrew Thompson 		.bufsize = 0,	/* use wMaxPacketSize */
1894eae601eSAndrew Thompson 		.callback = rue_intr_callback,
19002ac6454SAndrew Thompson 	},
19102ac6454SAndrew Thompson };
19202ac6454SAndrew Thompson 
19302ac6454SAndrew Thompson static device_method_t rue_methods[] = {
19402ac6454SAndrew Thompson 	/* Device interface */
19502ac6454SAndrew Thompson 	DEVMETHOD(device_probe, rue_probe),
19602ac6454SAndrew Thompson 	DEVMETHOD(device_attach, rue_attach),
19702ac6454SAndrew Thompson 	DEVMETHOD(device_detach, rue_detach),
19802ac6454SAndrew Thompson 
19902ac6454SAndrew Thompson 	/* MII interface */
20002ac6454SAndrew Thompson 	DEVMETHOD(miibus_readreg, rue_miibus_readreg),
20102ac6454SAndrew Thompson 	DEVMETHOD(miibus_writereg, rue_miibus_writereg),
20202ac6454SAndrew Thompson 	DEVMETHOD(miibus_statchg, rue_miibus_statchg),
20302ac6454SAndrew Thompson 
2044b7ec270SMarius Strobl 	DEVMETHOD_END
20502ac6454SAndrew Thompson };
20602ac6454SAndrew Thompson 
20702ac6454SAndrew Thompson static driver_t rue_driver = {
20802ac6454SAndrew Thompson 	.name = "rue",
20902ac6454SAndrew Thompson 	.methods = rue_methods,
21002ac6454SAndrew Thompson 	.size = sizeof(struct rue_softc),
21102ac6454SAndrew Thompson };
21202ac6454SAndrew Thompson 
213bc9372d7SJohn Baldwin DRIVER_MODULE_ORDERED(rue, uhub, rue_driver, NULL, NULL, SI_ORDER_ANY);
2143e38757dSJohn Baldwin DRIVER_MODULE(miibus, rue, miibus_driver, NULL, NULL);
21502ac6454SAndrew Thompson MODULE_DEPEND(rue, uether, 1, 1, 1);
21602ac6454SAndrew Thompson MODULE_DEPEND(rue, usb, 1, 1, 1);
21702ac6454SAndrew Thompson MODULE_DEPEND(rue, ether, 1, 1, 1);
21802ac6454SAndrew Thompson MODULE_DEPEND(rue, miibus, 1, 1, 1);
219910cb8feSAndrew Thompson MODULE_VERSION(rue, 1);
220f809f280SWarner Losh USB_PNP_HOST_INFO(rue_devs);
22102ac6454SAndrew Thompson 
222760bc48eSAndrew Thompson static const struct usb_ether_methods rue_ue_methods = {
22302ac6454SAndrew Thompson 	.ue_attach_post = rue_attach_post,
22402ac6454SAndrew Thompson 	.ue_start = rue_start,
22502ac6454SAndrew Thompson 	.ue_init = rue_init,
22602ac6454SAndrew Thompson 	.ue_stop = rue_stop,
22702ac6454SAndrew Thompson 	.ue_tick = rue_tick,
22802ac6454SAndrew Thompson 	.ue_setmulti = rue_setmulti,
22902ac6454SAndrew Thompson 	.ue_setpromisc = rue_setpromisc,
23002ac6454SAndrew Thompson 	.ue_mii_upd = rue_ifmedia_upd,
23102ac6454SAndrew Thompson 	.ue_mii_sts = rue_ifmedia_sts,
23202ac6454SAndrew Thompson };
23302ac6454SAndrew Thompson 
23402ac6454SAndrew Thompson #define	RUE_SETBIT(sc, reg, x) \
23502ac6454SAndrew Thompson 	rue_csr_write_1(sc, reg, rue_csr_read_1(sc, reg) | (x))
23602ac6454SAndrew Thompson 
23702ac6454SAndrew Thompson #define	RUE_CLRBIT(sc, reg, x) \
23802ac6454SAndrew Thompson 	rue_csr_write_1(sc, reg, rue_csr_read_1(sc, reg) & ~(x))
23902ac6454SAndrew Thompson 
24002ac6454SAndrew Thompson static int
rue_read_mem(struct rue_softc * sc,uint16_t addr,void * buf,int len)24102ac6454SAndrew Thompson rue_read_mem(struct rue_softc *sc, uint16_t addr, void *buf, int len)
24202ac6454SAndrew Thompson {
243760bc48eSAndrew Thompson 	struct usb_device_request req;
24402ac6454SAndrew Thompson 
24502ac6454SAndrew Thompson 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
24602ac6454SAndrew Thompson 	req.bRequest = UR_SET_ADDRESS;
24702ac6454SAndrew Thompson 	USETW(req.wValue, addr);
24802ac6454SAndrew Thompson 	USETW(req.wIndex, 0);
24902ac6454SAndrew Thompson 	USETW(req.wLength, len);
25002ac6454SAndrew Thompson 
251a593f6b8SAndrew Thompson 	return (uether_do_request(&sc->sc_ue, &req, buf, 1000));
25202ac6454SAndrew Thompson }
25302ac6454SAndrew Thompson 
25402ac6454SAndrew Thompson static int
rue_write_mem(struct rue_softc * sc,uint16_t addr,void * buf,int len)25502ac6454SAndrew Thompson rue_write_mem(struct rue_softc *sc, uint16_t addr, void *buf, int len)
25602ac6454SAndrew Thompson {
257760bc48eSAndrew Thompson 	struct usb_device_request req;
25802ac6454SAndrew Thompson 
25902ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
26002ac6454SAndrew Thompson 	req.bRequest = UR_SET_ADDRESS;
26102ac6454SAndrew Thompson 	USETW(req.wValue, addr);
26202ac6454SAndrew Thompson 	USETW(req.wIndex, 0);
26302ac6454SAndrew Thompson 	USETW(req.wLength, len);
26402ac6454SAndrew Thompson 
265a593f6b8SAndrew Thompson 	return (uether_do_request(&sc->sc_ue, &req, buf, 1000));
26602ac6454SAndrew Thompson }
26702ac6454SAndrew Thompson 
26802ac6454SAndrew Thompson static uint8_t
rue_csr_read_1(struct rue_softc * sc,uint16_t reg)26902ac6454SAndrew Thompson rue_csr_read_1(struct rue_softc *sc, uint16_t reg)
27002ac6454SAndrew Thompson {
27102ac6454SAndrew Thompson 	uint8_t val;
27202ac6454SAndrew Thompson 
27302ac6454SAndrew Thompson 	rue_read_mem(sc, reg, &val, 1);
27402ac6454SAndrew Thompson 	return (val);
27502ac6454SAndrew Thompson }
27602ac6454SAndrew Thompson 
27702ac6454SAndrew Thompson static uint16_t
rue_csr_read_2(struct rue_softc * sc,uint16_t reg)27802ac6454SAndrew Thompson rue_csr_read_2(struct rue_softc *sc, uint16_t reg)
27902ac6454SAndrew Thompson {
28002ac6454SAndrew Thompson 	uint8_t val[2];
28102ac6454SAndrew Thompson 
28202ac6454SAndrew Thompson 	rue_read_mem(sc, reg, &val, 2);
28302ac6454SAndrew Thompson 	return (UGETW(val));
28402ac6454SAndrew Thompson }
28502ac6454SAndrew Thompson 
28602ac6454SAndrew Thompson static int
rue_csr_write_1(struct rue_softc * sc,uint16_t reg,uint8_t val)28702ac6454SAndrew Thompson rue_csr_write_1(struct rue_softc *sc, uint16_t reg, uint8_t val)
28802ac6454SAndrew Thompson {
28902ac6454SAndrew Thompson 	return (rue_write_mem(sc, reg, &val, 1));
29002ac6454SAndrew Thompson }
29102ac6454SAndrew Thompson 
29202ac6454SAndrew Thompson static int
rue_csr_write_2(struct rue_softc * sc,uint16_t reg,uint16_t val)29302ac6454SAndrew Thompson rue_csr_write_2(struct rue_softc *sc, uint16_t reg, uint16_t val)
29402ac6454SAndrew Thompson {
29502ac6454SAndrew Thompson 	uint8_t temp[2];
29602ac6454SAndrew Thompson 
29702ac6454SAndrew Thompson 	USETW(temp, val);
29802ac6454SAndrew Thompson 	return (rue_write_mem(sc, reg, &temp, 2));
29902ac6454SAndrew Thompson }
30002ac6454SAndrew Thompson 
30102ac6454SAndrew Thompson static int
rue_csr_write_4(struct rue_softc * sc,int reg,uint32_t val)30202ac6454SAndrew Thompson rue_csr_write_4(struct rue_softc *sc, int reg, uint32_t val)
30302ac6454SAndrew Thompson {
30402ac6454SAndrew Thompson 	uint8_t temp[4];
30502ac6454SAndrew Thompson 
30602ac6454SAndrew Thompson 	USETDW(temp, val);
30702ac6454SAndrew Thompson 	return (rue_write_mem(sc, reg, &temp, 4));
30802ac6454SAndrew Thompson }
30902ac6454SAndrew Thompson 
31002ac6454SAndrew Thompson static int
rue_miibus_readreg(device_t dev,int phy,int reg)31102ac6454SAndrew Thompson rue_miibus_readreg(device_t dev, int phy, int reg)
31202ac6454SAndrew Thompson {
31302ac6454SAndrew Thompson 	struct rue_softc *sc = device_get_softc(dev);
31402ac6454SAndrew Thompson 	uint16_t rval;
31502ac6454SAndrew Thompson 	uint16_t ruereg;
31602ac6454SAndrew Thompson 	int locked;
31702ac6454SAndrew Thompson 
31802ac6454SAndrew Thompson 	if (phy != 0)		/* RTL8150 supports PHY == 0, only */
31902ac6454SAndrew Thompson 		return (0);
32002ac6454SAndrew Thompson 
32102ac6454SAndrew Thompson 	locked = mtx_owned(&sc->sc_mtx);
32202ac6454SAndrew Thompson 	if (!locked)
32302ac6454SAndrew Thompson 		RUE_LOCK(sc);
32402ac6454SAndrew Thompson 
32502ac6454SAndrew Thompson 	switch (reg) {
32602ac6454SAndrew Thompson 	case MII_BMCR:
32702ac6454SAndrew Thompson 		ruereg = RUE_BMCR;
32802ac6454SAndrew Thompson 		break;
32902ac6454SAndrew Thompson 	case MII_BMSR:
33002ac6454SAndrew Thompson 		ruereg = RUE_BMSR;
33102ac6454SAndrew Thompson 		break;
33202ac6454SAndrew Thompson 	case MII_ANAR:
33302ac6454SAndrew Thompson 		ruereg = RUE_ANAR;
33402ac6454SAndrew Thompson 		break;
33502ac6454SAndrew Thompson 	case MII_ANER:
33602ac6454SAndrew Thompson 		ruereg = RUE_AER;
33702ac6454SAndrew Thompson 		break;
33802ac6454SAndrew Thompson 	case MII_ANLPAR:
33902ac6454SAndrew Thompson 		ruereg = RUE_ANLP;
34002ac6454SAndrew Thompson 		break;
34102ac6454SAndrew Thompson 	case MII_PHYIDR1:
34202ac6454SAndrew Thompson 	case MII_PHYIDR2:
34302ac6454SAndrew Thompson 		rval = 0;
34402ac6454SAndrew Thompson 		goto done;
34502ac6454SAndrew Thompson 	default:
34602ac6454SAndrew Thompson 		if (RUE_REG_MIN <= reg && reg <= RUE_REG_MAX) {
34702ac6454SAndrew Thompson 			rval = rue_csr_read_1(sc, reg);
34802ac6454SAndrew Thompson 			goto done;
34902ac6454SAndrew Thompson 		}
35002ac6454SAndrew Thompson 		device_printf(sc->sc_ue.ue_dev, "bad phy register\n");
35102ac6454SAndrew Thompson 		rval = 0;
35202ac6454SAndrew Thompson 		goto done;
35302ac6454SAndrew Thompson 	}
35402ac6454SAndrew Thompson 
35502ac6454SAndrew Thompson 	rval = rue_csr_read_2(sc, ruereg);
35602ac6454SAndrew Thompson done:
35702ac6454SAndrew Thompson 	if (!locked)
35802ac6454SAndrew Thompson 		RUE_UNLOCK(sc);
35902ac6454SAndrew Thompson 	return (rval);
36002ac6454SAndrew Thompson }
36102ac6454SAndrew Thompson 
36202ac6454SAndrew Thompson static int
rue_miibus_writereg(device_t dev,int phy,int reg,int data)36302ac6454SAndrew Thompson rue_miibus_writereg(device_t dev, int phy, int reg, int data)
36402ac6454SAndrew Thompson {
36502ac6454SAndrew Thompson 	struct rue_softc *sc = device_get_softc(dev);
36602ac6454SAndrew Thompson 	uint16_t ruereg;
36702ac6454SAndrew Thompson 	int locked;
36802ac6454SAndrew Thompson 
36902ac6454SAndrew Thompson 	if (phy != 0)		/* RTL8150 supports PHY == 0, only */
37002ac6454SAndrew Thompson 		return (0);
37102ac6454SAndrew Thompson 
37202ac6454SAndrew Thompson 	locked = mtx_owned(&sc->sc_mtx);
37302ac6454SAndrew Thompson 	if (!locked)
37402ac6454SAndrew Thompson 		RUE_LOCK(sc);
37502ac6454SAndrew Thompson 
37602ac6454SAndrew Thompson 	switch (reg) {
37702ac6454SAndrew Thompson 	case MII_BMCR:
37802ac6454SAndrew Thompson 		ruereg = RUE_BMCR;
37902ac6454SAndrew Thompson 		break;
38002ac6454SAndrew Thompson 	case MII_BMSR:
38102ac6454SAndrew Thompson 		ruereg = RUE_BMSR;
38202ac6454SAndrew Thompson 		break;
38302ac6454SAndrew Thompson 	case MII_ANAR:
38402ac6454SAndrew Thompson 		ruereg = RUE_ANAR;
38502ac6454SAndrew Thompson 		break;
38602ac6454SAndrew Thompson 	case MII_ANER:
38702ac6454SAndrew Thompson 		ruereg = RUE_AER;
38802ac6454SAndrew Thompson 		break;
38902ac6454SAndrew Thompson 	case MII_ANLPAR:
39002ac6454SAndrew Thompson 		ruereg = RUE_ANLP;
39102ac6454SAndrew Thompson 		break;
39202ac6454SAndrew Thompson 	case MII_PHYIDR1:
39302ac6454SAndrew Thompson 	case MII_PHYIDR2:
39402ac6454SAndrew Thompson 		goto done;
39502ac6454SAndrew Thompson 	default:
39602ac6454SAndrew Thompson 		if (RUE_REG_MIN <= reg && reg <= RUE_REG_MAX) {
39702ac6454SAndrew Thompson 			rue_csr_write_1(sc, reg, data);
39802ac6454SAndrew Thompson 			goto done;
39902ac6454SAndrew Thompson 		}
40002ac6454SAndrew Thompson 		device_printf(sc->sc_ue.ue_dev, " bad phy register\n");
40102ac6454SAndrew Thompson 		goto done;
40202ac6454SAndrew Thompson 	}
40302ac6454SAndrew Thompson 	rue_csr_write_2(sc, ruereg, data);
40402ac6454SAndrew Thompson done:
40502ac6454SAndrew Thompson 	if (!locked)
40602ac6454SAndrew Thompson 		RUE_UNLOCK(sc);
40702ac6454SAndrew Thompson 	return (0);
40802ac6454SAndrew Thompson }
40902ac6454SAndrew Thompson 
41002ac6454SAndrew Thompson static void
rue_miibus_statchg(device_t dev)41102ac6454SAndrew Thompson rue_miibus_statchg(device_t dev)
41202ac6454SAndrew Thompson {
41302ac6454SAndrew Thompson 	/*
41402ac6454SAndrew Thompson 	 * When the code below is enabled the card starts doing weird
41502ac6454SAndrew Thompson 	 * things after link going from UP to DOWN and back UP.
41602ac6454SAndrew Thompson 	 *
41702ac6454SAndrew Thompson 	 * Looks like some of register writes below messes up PHY
41802ac6454SAndrew Thompson 	 * interface.
41902ac6454SAndrew Thompson 	 *
42002ac6454SAndrew Thompson 	 * No visible regressions were found after commenting this code
42102ac6454SAndrew Thompson 	 * out, so that disable it for good.
42202ac6454SAndrew Thompson 	 */
42302ac6454SAndrew Thompson #if 0
42402ac6454SAndrew Thompson 	struct rue_softc *sc = device_get_softc(dev);
42502ac6454SAndrew Thompson 	struct mii_data *mii = GET_MII(sc);
42602ac6454SAndrew Thompson 	uint16_t bmcr;
42702ac6454SAndrew Thompson 	int locked;
42802ac6454SAndrew Thompson 
42902ac6454SAndrew Thompson 	locked = mtx_owned(&sc->sc_mtx);
43002ac6454SAndrew Thompson 	if (!locked)
43102ac6454SAndrew Thompson 		RUE_LOCK(sc);
43202ac6454SAndrew Thompson 
43302ac6454SAndrew Thompson 	RUE_CLRBIT(sc, RUE_CR, (RUE_CR_RE | RUE_CR_TE));
43402ac6454SAndrew Thompson 
43502ac6454SAndrew Thompson 	bmcr = rue_csr_read_2(sc, RUE_BMCR);
43602ac6454SAndrew Thompson 
43702ac6454SAndrew Thompson 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX)
43802ac6454SAndrew Thompson 		bmcr |= RUE_BMCR_SPD_SET;
43902ac6454SAndrew Thompson 	else
44002ac6454SAndrew Thompson 		bmcr &= ~RUE_BMCR_SPD_SET;
44102ac6454SAndrew Thompson 
44202ac6454SAndrew Thompson 	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
44302ac6454SAndrew Thompson 		bmcr |= RUE_BMCR_DUPLEX;
44402ac6454SAndrew Thompson 	else
44502ac6454SAndrew Thompson 		bmcr &= ~RUE_BMCR_DUPLEX;
44602ac6454SAndrew Thompson 
44702ac6454SAndrew Thompson 	rue_csr_write_2(sc, RUE_BMCR, bmcr);
44802ac6454SAndrew Thompson 
44902ac6454SAndrew Thompson 	RUE_SETBIT(sc, RUE_CR, (RUE_CR_RE | RUE_CR_TE));
45002ac6454SAndrew Thompson 
45102ac6454SAndrew Thompson 	if (!locked)
45202ac6454SAndrew Thompson 		RUE_UNLOCK(sc);
45302ac6454SAndrew Thompson #endif
45402ac6454SAndrew Thompson }
45502ac6454SAndrew Thompson 
45602ac6454SAndrew Thompson static void
rue_setpromisc(struct usb_ether * ue)457760bc48eSAndrew Thompson rue_setpromisc(struct usb_ether *ue)
45802ac6454SAndrew Thompson {
459a593f6b8SAndrew Thompson 	struct rue_softc *sc = uether_getsc(ue);
460935b194dSJustin Hibbits 	if_t ifp = uether_getifp(ue);
46102ac6454SAndrew Thompson 
46202ac6454SAndrew Thompson 	RUE_LOCK_ASSERT(sc, MA_OWNED);
46302ac6454SAndrew Thompson 
46402ac6454SAndrew Thompson 	/* If we want promiscuous mode, set the allframes bit. */
465935b194dSJustin Hibbits 	if (if_getflags(ifp) & IFF_PROMISC)
46602ac6454SAndrew Thompson 		RUE_SETBIT(sc, RUE_RCR, RUE_RCR_AAP);
46702ac6454SAndrew Thompson 	else
46802ac6454SAndrew Thompson 		RUE_CLRBIT(sc, RUE_RCR, RUE_RCR_AAP);
46902ac6454SAndrew Thompson }
47002ac6454SAndrew Thompson 
471100ad729SGleb Smirnoff static u_int
rue_hash_maddr(void * arg,struct sockaddr_dl * sdl,u_int cnt)472100ad729SGleb Smirnoff rue_hash_maddr(void *arg, struct sockaddr_dl *sdl, u_int cnt)
473100ad729SGleb Smirnoff {
474100ad729SGleb Smirnoff 	uint32_t *hashes = arg;
475100ad729SGleb Smirnoff 	int h;
476100ad729SGleb Smirnoff 
477100ad729SGleb Smirnoff 	h = ether_crc32_be(LLADDR(sdl), ETHER_ADDR_LEN) >> 26;
478100ad729SGleb Smirnoff 	if (h < 32)
479100ad729SGleb Smirnoff 		hashes[0] |= (1 << h);
480100ad729SGleb Smirnoff 	else
481100ad729SGleb Smirnoff 		hashes[1] |= (1 << (h - 32));
482100ad729SGleb Smirnoff 
483100ad729SGleb Smirnoff 	return (1);
484100ad729SGleb Smirnoff }
485100ad729SGleb Smirnoff 
48602ac6454SAndrew Thompson /*
48702ac6454SAndrew Thompson  * Program the 64-bit multicast hash filter.
48802ac6454SAndrew Thompson  */
48902ac6454SAndrew Thompson static void
rue_setmulti(struct usb_ether * ue)490760bc48eSAndrew Thompson rue_setmulti(struct usb_ether *ue)
49102ac6454SAndrew Thompson {
492a593f6b8SAndrew Thompson 	struct rue_softc *sc = uether_getsc(ue);
493935b194dSJustin Hibbits 	if_t ifp = uether_getifp(ue);
49402ac6454SAndrew Thompson 	uint16_t rxcfg;
49502ac6454SAndrew Thompson 	uint32_t hashes[2] = { 0, 0 };
496100ad729SGleb Smirnoff 	int mcnt;
49702ac6454SAndrew Thompson 
49802ac6454SAndrew Thompson 	RUE_LOCK_ASSERT(sc, MA_OWNED);
49902ac6454SAndrew Thompson 
50002ac6454SAndrew Thompson 	rxcfg = rue_csr_read_2(sc, RUE_RCR);
50102ac6454SAndrew Thompson 
502935b194dSJustin Hibbits 	if (if_getflags(ifp) & IFF_ALLMULTI || if_getflags(ifp) & IFF_PROMISC) {
50302ac6454SAndrew Thompson 		rxcfg |= (RUE_RCR_AAM | RUE_RCR_AAP);
50402ac6454SAndrew Thompson 		rxcfg &= ~RUE_RCR_AM;
50502ac6454SAndrew Thompson 		rue_csr_write_2(sc, RUE_RCR, rxcfg);
50602ac6454SAndrew Thompson 		rue_csr_write_4(sc, RUE_MAR0, 0xFFFFFFFF);
50702ac6454SAndrew Thompson 		rue_csr_write_4(sc, RUE_MAR4, 0xFFFFFFFF);
50802ac6454SAndrew Thompson 		return;
50902ac6454SAndrew Thompson 	}
51002ac6454SAndrew Thompson 
51102ac6454SAndrew Thompson 	/* first, zot all the existing hash bits */
51202ac6454SAndrew Thompson 	rue_csr_write_4(sc, RUE_MAR0, 0);
51302ac6454SAndrew Thompson 	rue_csr_write_4(sc, RUE_MAR4, 0);
51402ac6454SAndrew Thompson 
51502ac6454SAndrew Thompson 	/* now program new ones */
516100ad729SGleb Smirnoff 	mcnt = if_foreach_llmaddr(ifp, rue_hash_maddr, &hashes);
51702ac6454SAndrew Thompson 
51802ac6454SAndrew Thompson 	if (mcnt)
51902ac6454SAndrew Thompson 		rxcfg |= RUE_RCR_AM;
52002ac6454SAndrew Thompson 	else
52102ac6454SAndrew Thompson 		rxcfg &= ~RUE_RCR_AM;
52202ac6454SAndrew Thompson 
52302ac6454SAndrew Thompson 	rxcfg &= ~(RUE_RCR_AAM | RUE_RCR_AAP);
52402ac6454SAndrew Thompson 
52502ac6454SAndrew Thompson 	rue_csr_write_2(sc, RUE_RCR, rxcfg);
52602ac6454SAndrew Thompson 	rue_csr_write_4(sc, RUE_MAR0, hashes[0]);
52702ac6454SAndrew Thompson 	rue_csr_write_4(sc, RUE_MAR4, hashes[1]);
52802ac6454SAndrew Thompson }
52902ac6454SAndrew Thompson 
53002ac6454SAndrew Thompson static void
rue_reset(struct rue_softc * sc)53102ac6454SAndrew Thompson rue_reset(struct rue_softc *sc)
53202ac6454SAndrew Thompson {
53302ac6454SAndrew Thompson 	int i;
53402ac6454SAndrew Thompson 
53502ac6454SAndrew Thompson 	rue_csr_write_1(sc, RUE_CR, RUE_CR_SOFT_RST);
53602ac6454SAndrew Thompson 
53702ac6454SAndrew Thompson 	for (i = 0; i != RUE_TIMEOUT; i++) {
538a593f6b8SAndrew Thompson 		if (uether_pause(&sc->sc_ue, hz / 1000))
53902ac6454SAndrew Thompson 			break;
54002ac6454SAndrew Thompson 		if (!(rue_csr_read_1(sc, RUE_CR) & RUE_CR_SOFT_RST))
54102ac6454SAndrew Thompson 			break;
54202ac6454SAndrew Thompson 	}
54302ac6454SAndrew Thompson 	if (i == RUE_TIMEOUT)
544767cb2e2SAndrew Thompson 		device_printf(sc->sc_ue.ue_dev, "reset never completed\n");
54502ac6454SAndrew Thompson 
546a593f6b8SAndrew Thompson 	uether_pause(&sc->sc_ue, hz / 100);
54702ac6454SAndrew Thompson }
54802ac6454SAndrew Thompson 
54902ac6454SAndrew Thompson static void
rue_attach_post(struct usb_ether * ue)550760bc48eSAndrew Thompson rue_attach_post(struct usb_ether *ue)
55102ac6454SAndrew Thompson {
552a593f6b8SAndrew Thompson 	struct rue_softc *sc = uether_getsc(ue);
55302ac6454SAndrew Thompson 
55402ac6454SAndrew Thompson 	/* reset the adapter */
55502ac6454SAndrew Thompson 	rue_reset(sc);
55602ac6454SAndrew Thompson 
55702ac6454SAndrew Thompson 	/* get station address from the EEPROM */
55802ac6454SAndrew Thompson 	rue_read_mem(sc, RUE_EEPROM_IDR0, ue->ue_eaddr, ETHER_ADDR_LEN);
55902ac6454SAndrew Thompson }
56002ac6454SAndrew Thompson 
56102ac6454SAndrew Thompson /*
56202ac6454SAndrew Thompson  * Probe for a RTL8150 chip.
56302ac6454SAndrew Thompson  */
56402ac6454SAndrew Thompson static int
rue_probe(device_t dev)56502ac6454SAndrew Thompson rue_probe(device_t dev)
56602ac6454SAndrew Thompson {
567760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
56802ac6454SAndrew Thompson 
569f29a0724SAndrew Thompson 	if (uaa->usb_mode != USB_MODE_HOST)
57002ac6454SAndrew Thompson 		return (ENXIO);
57102ac6454SAndrew Thompson 	if (uaa->info.bConfigIndex != RUE_CONFIG_IDX)
57202ac6454SAndrew Thompson 		return (ENXIO);
57302ac6454SAndrew Thompson 	if (uaa->info.bIfaceIndex != RUE_IFACE_IDX)
57402ac6454SAndrew Thompson 		return (ENXIO);
57502ac6454SAndrew Thompson 
576a593f6b8SAndrew Thompson 	return (usbd_lookup_id_by_uaa(rue_devs, sizeof(rue_devs), uaa));
57702ac6454SAndrew Thompson }
57802ac6454SAndrew Thompson 
57902ac6454SAndrew Thompson /*
58002ac6454SAndrew Thompson  * Attach the interface. Allocate softc structures, do ifmedia
58102ac6454SAndrew Thompson  * setup and ethernet/BPF attach.
58202ac6454SAndrew Thompson  */
58302ac6454SAndrew Thompson static int
rue_attach(device_t dev)58402ac6454SAndrew Thompson rue_attach(device_t dev)
58502ac6454SAndrew Thompson {
586760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
58702ac6454SAndrew Thompson 	struct rue_softc *sc = device_get_softc(dev);
588760bc48eSAndrew Thompson 	struct usb_ether *ue = &sc->sc_ue;
58902ac6454SAndrew Thompson 	uint8_t iface_index;
59002ac6454SAndrew Thompson 	int error;
59102ac6454SAndrew Thompson 
592a593f6b8SAndrew Thompson 	device_set_usb_desc(dev);
59302ac6454SAndrew Thompson 	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
59402ac6454SAndrew Thompson 
59502ac6454SAndrew Thompson 	iface_index = RUE_IFACE_IDX;
596a593f6b8SAndrew Thompson 	error = usbd_transfer_setup(uaa->device, &iface_index,
59702ac6454SAndrew Thompson 	    sc->sc_xfer, rue_config, RUE_N_TRANSFER,
59802ac6454SAndrew Thompson 	    sc, &sc->sc_mtx);
59902ac6454SAndrew Thompson 	if (error) {
600767cb2e2SAndrew Thompson 		device_printf(dev, "allocating USB transfers failed\n");
60102ac6454SAndrew Thompson 		goto detach;
60202ac6454SAndrew Thompson 	}
60302ac6454SAndrew Thompson 
60402ac6454SAndrew Thompson 	ue->ue_sc = sc;
60502ac6454SAndrew Thompson 	ue->ue_dev = dev;
60602ac6454SAndrew Thompson 	ue->ue_udev = uaa->device;
60702ac6454SAndrew Thompson 	ue->ue_mtx = &sc->sc_mtx;
60802ac6454SAndrew Thompson 	ue->ue_methods = &rue_ue_methods;
60902ac6454SAndrew Thompson 
610a593f6b8SAndrew Thompson 	error = uether_ifattach(ue);
61102ac6454SAndrew Thompson 	if (error) {
61202ac6454SAndrew Thompson 		device_printf(dev, "could not attach interface\n");
61302ac6454SAndrew Thompson 		goto detach;
61402ac6454SAndrew Thompson 	}
61502ac6454SAndrew Thompson 	return (0);			/* success */
61602ac6454SAndrew Thompson 
61702ac6454SAndrew Thompson detach:
61802ac6454SAndrew Thompson 	rue_detach(dev);
61902ac6454SAndrew Thompson 	return (ENXIO);			/* failure */
62002ac6454SAndrew Thompson }
62102ac6454SAndrew Thompson 
62202ac6454SAndrew Thompson static int
rue_detach(device_t dev)62302ac6454SAndrew Thompson rue_detach(device_t dev)
62402ac6454SAndrew Thompson {
62502ac6454SAndrew Thompson 	struct rue_softc *sc = device_get_softc(dev);
626760bc48eSAndrew Thompson 	struct usb_ether *ue = &sc->sc_ue;
62702ac6454SAndrew Thompson 
628a593f6b8SAndrew Thompson 	usbd_transfer_unsetup(sc->sc_xfer, RUE_N_TRANSFER);
629a593f6b8SAndrew Thompson 	uether_ifdetach(ue);
63002ac6454SAndrew Thompson 	mtx_destroy(&sc->sc_mtx);
63102ac6454SAndrew Thompson 
63202ac6454SAndrew Thompson 	return (0);
63302ac6454SAndrew Thompson }
63402ac6454SAndrew Thompson 
63502ac6454SAndrew Thompson static void
rue_intr_callback(struct usb_xfer * xfer,usb_error_t error)636ed6d949aSAndrew Thompson rue_intr_callback(struct usb_xfer *xfer, usb_error_t error)
63702ac6454SAndrew Thompson {
638ed6d949aSAndrew Thompson 	struct rue_softc *sc = usbd_xfer_softc(xfer);
639935b194dSJustin Hibbits 	if_t ifp = uether_getifp(&sc->sc_ue);
64002ac6454SAndrew Thompson 	struct rue_intrpkt pkt;
641ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
642ed6d949aSAndrew Thompson 	int actlen;
643ed6d949aSAndrew Thompson 
644ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
64502ac6454SAndrew Thompson 
64602ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
64702ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
64802ac6454SAndrew Thompson 
649935b194dSJustin Hibbits 		if (ifp && (if_getdrvflags(ifp) & IFF_DRV_RUNNING) &&
6506d917491SHans Petter Selasky 		    actlen >= (int)sizeof(pkt)) {
651ed6d949aSAndrew Thompson 			pc = usbd_xfer_get_frame(xfer, 0);
652ed6d949aSAndrew Thompson 			usbd_copy_out(pc, 0, &pkt, sizeof(pkt));
65302ac6454SAndrew Thompson 
654ecc70d3fSGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_IERRORS, pkt.rue_rxlost_cnt);
655ecc70d3fSGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_IERRORS, pkt.rue_crcerr_cnt);
656ecc70d3fSGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_COLLISIONS, pkt.rue_col_cnt);
65702ac6454SAndrew Thompson 		}
65802ac6454SAndrew Thompson 		/* FALLTHROUGH */
65902ac6454SAndrew Thompson 	case USB_ST_SETUP:
66002ac6454SAndrew Thompson tr_setup:
661ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
662a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
66302ac6454SAndrew Thompson 		return;
66402ac6454SAndrew Thompson 
66502ac6454SAndrew Thompson 	default:			/* Error */
666ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
66702ac6454SAndrew Thompson 			/* try to clear stall first */
668ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
66902ac6454SAndrew Thompson 			goto tr_setup;
67002ac6454SAndrew Thompson 		}
67102ac6454SAndrew Thompson 		return;
67202ac6454SAndrew Thompson 	}
67302ac6454SAndrew Thompson }
67402ac6454SAndrew Thompson 
67502ac6454SAndrew Thompson static void
rue_bulk_read_callback(struct usb_xfer * xfer,usb_error_t error)676ed6d949aSAndrew Thompson rue_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
67702ac6454SAndrew Thompson {
678ed6d949aSAndrew Thompson 	struct rue_softc *sc = usbd_xfer_softc(xfer);
679760bc48eSAndrew Thompson 	struct usb_ether *ue = &sc->sc_ue;
680935b194dSJustin Hibbits 	if_t ifp = uether_getifp(ue);
681ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
68202ac6454SAndrew Thompson 	uint16_t status;
683ed6d949aSAndrew Thompson 	int actlen;
684ed6d949aSAndrew Thompson 
685ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
68602ac6454SAndrew Thompson 
68702ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
68802ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
68902ac6454SAndrew Thompson 
690ed6d949aSAndrew Thompson 		if (actlen < 4) {
691ecc70d3fSGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
69202ac6454SAndrew Thompson 			goto tr_setup;
69302ac6454SAndrew Thompson 		}
694ed6d949aSAndrew Thompson 		pc = usbd_xfer_get_frame(xfer, 0);
695ed6d949aSAndrew Thompson 		usbd_copy_out(pc, actlen - 4, &status, sizeof(status));
696ed6d949aSAndrew Thompson 		actlen -= 4;
69702ac6454SAndrew Thompson 
69820733245SPedro F. Giffuni 		/* check receive packet was valid or not */
69902ac6454SAndrew Thompson 		status = le16toh(status);
70002ac6454SAndrew Thompson 		if ((status & RUE_RXSTAT_VALID) == 0) {
701ecc70d3fSGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
70202ac6454SAndrew Thompson 			goto tr_setup;
70302ac6454SAndrew Thompson 		}
704ed6d949aSAndrew Thompson 		uether_rxbuf(ue, pc, 0, actlen);
70502ac6454SAndrew Thompson 		/* FALLTHROUGH */
70602ac6454SAndrew Thompson 	case USB_ST_SETUP:
70702ac6454SAndrew Thompson tr_setup:
708ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
709a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
710a593f6b8SAndrew Thompson 		uether_rxflush(ue);
71102ac6454SAndrew Thompson 		return;
71202ac6454SAndrew Thompson 
71302ac6454SAndrew Thompson 	default:			/* Error */
71402ac6454SAndrew Thompson 		DPRINTF("bulk read error, %s\n",
715ed6d949aSAndrew Thompson 		    usbd_errstr(error));
71602ac6454SAndrew Thompson 
717ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
71802ac6454SAndrew Thompson 			/* try to clear stall first */
719ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
72002ac6454SAndrew Thompson 			goto tr_setup;
72102ac6454SAndrew Thompson 		}
72202ac6454SAndrew Thompson 		return;
72302ac6454SAndrew Thompson 	}
72402ac6454SAndrew Thompson }
72502ac6454SAndrew Thompson 
72602ac6454SAndrew Thompson static void
rue_bulk_write_callback(struct usb_xfer * xfer,usb_error_t error)727ed6d949aSAndrew Thompson rue_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
72802ac6454SAndrew Thompson {
729ed6d949aSAndrew Thompson 	struct rue_softc *sc = usbd_xfer_softc(xfer);
730935b194dSJustin Hibbits 	if_t ifp = uether_getifp(&sc->sc_ue);
731ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
73202ac6454SAndrew Thompson 	struct mbuf *m;
73302ac6454SAndrew Thompson 	int temp_len;
73402ac6454SAndrew Thompson 
73502ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
73602ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
73702ac6454SAndrew Thompson 		DPRINTFN(11, "transfer complete\n");
738ecc70d3fSGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
73902ac6454SAndrew Thompson 
74002ac6454SAndrew Thompson 		/* FALLTHROUGH */
74102ac6454SAndrew Thompson 	case USB_ST_SETUP:
74202ac6454SAndrew Thompson tr_setup:
74302ac6454SAndrew Thompson 		if ((sc->sc_flags & RUE_FLAG_LINK) == 0) {
74402ac6454SAndrew Thompson 			/*
74502ac6454SAndrew Thompson 			 * don't send anything if there is no link !
74602ac6454SAndrew Thompson 			 */
74702ac6454SAndrew Thompson 			return;
74802ac6454SAndrew Thompson 		}
749935b194dSJustin Hibbits 		m = if_dequeue(ifp);
75002ac6454SAndrew Thompson 
75102ac6454SAndrew Thompson 		if (m == NULL)
75202ac6454SAndrew Thompson 			return;
75302ac6454SAndrew Thompson 		if (m->m_pkthdr.len > MCLBYTES)
75402ac6454SAndrew Thompson 			m->m_pkthdr.len = MCLBYTES;
75502ac6454SAndrew Thompson 		temp_len = m->m_pkthdr.len;
75602ac6454SAndrew Thompson 
757ed6d949aSAndrew Thompson 		pc = usbd_xfer_get_frame(xfer, 0);
758ed6d949aSAndrew Thompson 		usbd_m_copy_in(pc, 0, m, 0, m->m_pkthdr.len);
75902ac6454SAndrew Thompson 
76002ac6454SAndrew Thompson 		/*
76102ac6454SAndrew Thompson 		 * This is an undocumented behavior.
76202ac6454SAndrew Thompson 		 * RTL8150 chip doesn't send frame length smaller than
76302ac6454SAndrew Thompson 		 * RUE_MIN_FRAMELEN (60) byte packet.
76402ac6454SAndrew Thompson 		 */
76502ac6454SAndrew Thompson 		if (temp_len < RUE_MIN_FRAMELEN) {
766ed6d949aSAndrew Thompson 			usbd_frame_zero(pc, temp_len,
76702ac6454SAndrew Thompson 			    RUE_MIN_FRAMELEN - temp_len);
76802ac6454SAndrew Thompson 			temp_len = RUE_MIN_FRAMELEN;
76902ac6454SAndrew Thompson 		}
770ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, temp_len);
77102ac6454SAndrew Thompson 
77202ac6454SAndrew Thompson 		/*
77302ac6454SAndrew Thompson 		 * if there's a BPF listener, bounce a copy
77402ac6454SAndrew Thompson 		 * of this frame to him:
77502ac6454SAndrew Thompson 		 */
77602ac6454SAndrew Thompson 		BPF_MTAP(ifp, m);
77702ac6454SAndrew Thompson 
77802ac6454SAndrew Thompson 		m_freem(m);
77902ac6454SAndrew Thompson 
780a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
78102ac6454SAndrew Thompson 
78202ac6454SAndrew Thompson 		return;
78302ac6454SAndrew Thompson 
78402ac6454SAndrew Thompson 	default:			/* Error */
78502ac6454SAndrew Thompson 		DPRINTFN(11, "transfer error, %s\n",
786ed6d949aSAndrew Thompson 		    usbd_errstr(error));
78702ac6454SAndrew Thompson 
788ecc70d3fSGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
78902ac6454SAndrew Thompson 
790ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
79102ac6454SAndrew Thompson 			/* try to clear stall first */
792ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
79302ac6454SAndrew Thompson 			goto tr_setup;
79402ac6454SAndrew Thompson 		}
79502ac6454SAndrew Thompson 		return;
79602ac6454SAndrew Thompson 	}
79702ac6454SAndrew Thompson }
79802ac6454SAndrew Thompson 
79902ac6454SAndrew Thompson static void
rue_tick(struct usb_ether * ue)800760bc48eSAndrew Thompson rue_tick(struct usb_ether *ue)
80102ac6454SAndrew Thompson {
802a593f6b8SAndrew Thompson 	struct rue_softc *sc = uether_getsc(ue);
80302ac6454SAndrew Thompson 	struct mii_data *mii = GET_MII(sc);
80402ac6454SAndrew Thompson 
80502ac6454SAndrew Thompson 	RUE_LOCK_ASSERT(sc, MA_OWNED);
80602ac6454SAndrew Thompson 
80702ac6454SAndrew Thompson 	mii_tick(mii);
80802ac6454SAndrew Thompson 	if ((sc->sc_flags & RUE_FLAG_LINK) == 0
80902ac6454SAndrew Thompson 	    && mii->mii_media_status & IFM_ACTIVE &&
81002ac6454SAndrew Thompson 	    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
81102ac6454SAndrew Thompson 		sc->sc_flags |= RUE_FLAG_LINK;
81202ac6454SAndrew Thompson 		rue_start(ue);
81302ac6454SAndrew Thompson 	}
81402ac6454SAndrew Thompson }
81502ac6454SAndrew Thompson 
81602ac6454SAndrew Thompson static void
rue_start(struct usb_ether * ue)817760bc48eSAndrew Thompson rue_start(struct usb_ether *ue)
81802ac6454SAndrew Thompson {
819a593f6b8SAndrew Thompson 	struct rue_softc *sc = uether_getsc(ue);
82002ac6454SAndrew Thompson 
82102ac6454SAndrew Thompson 	/*
82202ac6454SAndrew Thompson 	 * start the USB transfers, if not already started:
82302ac6454SAndrew Thompson 	 */
824a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[RUE_INTR_DT_RD]);
825a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[RUE_BULK_DT_RD]);
826a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[RUE_BULK_DT_WR]);
82702ac6454SAndrew Thompson }
82802ac6454SAndrew Thompson 
82902ac6454SAndrew Thompson static void
rue_init(struct usb_ether * ue)830760bc48eSAndrew Thompson rue_init(struct usb_ether *ue)
83102ac6454SAndrew Thompson {
832a593f6b8SAndrew Thompson 	struct rue_softc *sc = uether_getsc(ue);
833935b194dSJustin Hibbits 	if_t ifp = uether_getifp(ue);
83402ac6454SAndrew Thompson 
83502ac6454SAndrew Thompson 	RUE_LOCK_ASSERT(sc, MA_OWNED);
83602ac6454SAndrew Thompson 
83702ac6454SAndrew Thompson 	/*
83802ac6454SAndrew Thompson 	 * Cancel pending I/O
83902ac6454SAndrew Thompson 	 */
84002ac6454SAndrew Thompson 	rue_reset(sc);
84102ac6454SAndrew Thompson 
84202ac6454SAndrew Thompson 	/* Set MAC address */
843935b194dSJustin Hibbits 	rue_write_mem(sc, RUE_IDR0, if_getlladdr(ifp), ETHER_ADDR_LEN);
84402ac6454SAndrew Thompson 
84502ac6454SAndrew Thompson 	rue_stop(ue);
84602ac6454SAndrew Thompson 
84702ac6454SAndrew Thompson 	/*
84802ac6454SAndrew Thompson 	 * Set the initial TX and RX configuration.
84902ac6454SAndrew Thompson 	 */
85002ac6454SAndrew Thompson 	rue_csr_write_1(sc, RUE_TCR, RUE_TCR_CONFIG);
85102ac6454SAndrew Thompson 	rue_csr_write_2(sc, RUE_RCR, RUE_RCR_CONFIG|RUE_RCR_AB);
85202ac6454SAndrew Thompson 
85302ac6454SAndrew Thompson 	/* Load the multicast filter */
85402ac6454SAndrew Thompson 	rue_setpromisc(ue);
85502ac6454SAndrew Thompson 	/* Load the multicast filter. */
85602ac6454SAndrew Thompson 	rue_setmulti(ue);
85702ac6454SAndrew Thompson 
85802ac6454SAndrew Thompson 	/* Enable RX and TX */
85902ac6454SAndrew Thompson 	rue_csr_write_1(sc, RUE_CR, (RUE_CR_TE | RUE_CR_RE | RUE_CR_EP3CLREN));
86002ac6454SAndrew Thompson 
861ed6d949aSAndrew Thompson 	usbd_xfer_set_stall(sc->sc_xfer[RUE_BULK_DT_WR]);
86202ac6454SAndrew Thompson 
863935b194dSJustin Hibbits 	if_setdrvflagbits(ifp, IFF_DRV_RUNNING, 0);
86402ac6454SAndrew Thompson 	rue_start(ue);
86502ac6454SAndrew Thompson }
86602ac6454SAndrew Thompson 
86702ac6454SAndrew Thompson /*
86802ac6454SAndrew Thompson  * Set media options.
86902ac6454SAndrew Thompson  */
87002ac6454SAndrew Thompson static int
rue_ifmedia_upd(if_t ifp)871935b194dSJustin Hibbits rue_ifmedia_upd(if_t ifp)
87202ac6454SAndrew Thompson {
873935b194dSJustin Hibbits 	struct rue_softc *sc = if_getsoftc(ifp);
87402ac6454SAndrew Thompson 	struct mii_data *mii = GET_MII(sc);
8753fcb7a53SMarius Strobl 	struct mii_softc *miisc;
876ebe01023SKevin Lo 	int error;
87702ac6454SAndrew Thompson 
87802ac6454SAndrew Thompson 	RUE_LOCK_ASSERT(sc, MA_OWNED);
87902ac6454SAndrew Thompson 
88002ac6454SAndrew Thompson         sc->sc_flags &= ~RUE_FLAG_LINK;
88102ac6454SAndrew Thompson 	LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
8823fcb7a53SMarius Strobl 		PHY_RESET(miisc);
883ebe01023SKevin Lo 	error = mii_mediachg(mii);
884ebe01023SKevin Lo 	return (error);
88502ac6454SAndrew Thompson }
88602ac6454SAndrew Thompson 
88702ac6454SAndrew Thompson /*
88802ac6454SAndrew Thompson  * Report current media status.
88902ac6454SAndrew Thompson  */
89002ac6454SAndrew Thompson static void
rue_ifmedia_sts(if_t ifp,struct ifmediareq * ifmr)891935b194dSJustin Hibbits rue_ifmedia_sts(if_t ifp, struct ifmediareq *ifmr)
89202ac6454SAndrew Thompson {
893935b194dSJustin Hibbits 	struct rue_softc *sc = if_getsoftc(ifp);
89402ac6454SAndrew Thompson 	struct mii_data *mii = GET_MII(sc);
89502ac6454SAndrew Thompson 
89602ac6454SAndrew Thompson 	RUE_LOCK(sc);
89702ac6454SAndrew Thompson 	mii_pollstat(mii);
89802ac6454SAndrew Thompson 	ifmr->ifm_active = mii->mii_media_active;
89902ac6454SAndrew Thompson 	ifmr->ifm_status = mii->mii_media_status;
900e9e549efSPyun YongHyeon 	RUE_UNLOCK(sc);
90102ac6454SAndrew Thompson }
90202ac6454SAndrew Thompson 
90302ac6454SAndrew Thompson static void
rue_stop(struct usb_ether * ue)904760bc48eSAndrew Thompson rue_stop(struct usb_ether *ue)
90502ac6454SAndrew Thompson {
906a593f6b8SAndrew Thompson 	struct rue_softc *sc = uether_getsc(ue);
907935b194dSJustin Hibbits 	if_t ifp = uether_getifp(ue);
90802ac6454SAndrew Thompson 
90902ac6454SAndrew Thompson 	RUE_LOCK_ASSERT(sc, MA_OWNED);
91002ac6454SAndrew Thompson 
911935b194dSJustin Hibbits 	if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
91202ac6454SAndrew Thompson 	sc->sc_flags &= ~RUE_FLAG_LINK;
91302ac6454SAndrew Thompson 
91402ac6454SAndrew Thompson 	/*
91502ac6454SAndrew Thompson 	 * stop all the transfers, if not already stopped:
91602ac6454SAndrew Thompson 	 */
917a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[RUE_BULK_DT_WR]);
918a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[RUE_BULK_DT_RD]);
919a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[RUE_INTR_DT_RD]);
92002ac6454SAndrew Thompson 
92102ac6454SAndrew Thompson 	rue_csr_write_1(sc, RUE_CR, 0x00);
92202ac6454SAndrew Thompson 
92302ac6454SAndrew Thompson 	rue_reset(sc);
92402ac6454SAndrew Thompson }
925