xref: /freebsd/sys/dev/usb/net/if_axe.c (revision 14ae1fa4cf97e9f6599c9265a7b3294809e1b013)
102ac6454SAndrew Thompson /*-
202ac6454SAndrew Thompson  * Copyright (c) 1997, 1998, 1999, 2000-2003
302ac6454SAndrew Thompson  *	Bill Paul <wpaul@windriver.com>.  All rights reserved.
402ac6454SAndrew Thompson  *
502ac6454SAndrew Thompson  * Redistribution and use in source and binary forms, with or without
602ac6454SAndrew Thompson  * modification, are permitted provided that the following conditions
702ac6454SAndrew Thompson  * are met:
802ac6454SAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
902ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer.
1002ac6454SAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
1102ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
1202ac6454SAndrew Thompson  *    documentation and/or other materials provided with the distribution.
1302ac6454SAndrew Thompson  * 3. All advertising materials mentioning features or use of this software
1402ac6454SAndrew Thompson  *    must display the following acknowledgement:
1502ac6454SAndrew Thompson  *	This product includes software developed by Bill Paul.
1602ac6454SAndrew Thompson  * 4. Neither the name of the author nor the names of any co-contributors
1702ac6454SAndrew Thompson  *    may be used to endorse or promote products derived from this software
1802ac6454SAndrew Thompson  *    without specific prior written permission.
1902ac6454SAndrew Thompson  *
2002ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
2102ac6454SAndrew Thompson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2202ac6454SAndrew Thompson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2302ac6454SAndrew Thompson  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
2402ac6454SAndrew Thompson  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2502ac6454SAndrew Thompson  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2602ac6454SAndrew Thompson  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2702ac6454SAndrew Thompson  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2802ac6454SAndrew Thompson  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2902ac6454SAndrew Thompson  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
3002ac6454SAndrew Thompson  * THE POSSIBILITY OF SUCH DAMAGE.
3102ac6454SAndrew Thompson  */
3202ac6454SAndrew Thompson 
3302ac6454SAndrew Thompson #include <sys/cdefs.h>
3402ac6454SAndrew Thompson __FBSDID("$FreeBSD$");
3502ac6454SAndrew Thompson 
3602ac6454SAndrew Thompson /*
3702ac6454SAndrew Thompson  * ASIX Electronics AX88172/AX88178/AX88778 USB 2.0 ethernet driver.
3802ac6454SAndrew Thompson  * Used in the LinkSys USB200M and various other adapters.
3902ac6454SAndrew Thompson  *
4002ac6454SAndrew Thompson  * Manuals available from:
4102ac6454SAndrew Thompson  * http://www.asix.com.tw/datasheet/mac/Ax88172.PDF
4202ac6454SAndrew Thompson  * Note: you need the manual for the AX88170 chip (USB 1.x ethernet
4302ac6454SAndrew Thompson  * controller) to find the definitions for the RX control register.
4402ac6454SAndrew Thompson  * http://www.asix.com.tw/datasheet/mac/Ax88170.PDF
4502ac6454SAndrew Thompson  *
4602ac6454SAndrew Thompson  * Written by Bill Paul <wpaul@windriver.com>
4702ac6454SAndrew Thompson  * Senior Engineer
4802ac6454SAndrew Thompson  * Wind River Systems
4902ac6454SAndrew Thompson  */
5002ac6454SAndrew Thompson 
5102ac6454SAndrew Thompson /*
5202ac6454SAndrew Thompson  * The AX88172 provides USB ethernet supports at 10 and 100Mbps.
5302ac6454SAndrew Thompson  * It uses an external PHY (reference designs use a RealTek chip),
5402ac6454SAndrew Thompson  * and has a 64-bit multicast hash filter. There is some information
5502ac6454SAndrew Thompson  * missing from the manual which one needs to know in order to make
5602ac6454SAndrew Thompson  * the chip function:
5702ac6454SAndrew Thompson  *
5802ac6454SAndrew Thompson  * - You must set bit 7 in the RX control register, otherwise the
5902ac6454SAndrew Thompson  *   chip won't receive any packets.
6002ac6454SAndrew Thompson  * - You must initialize all 3 IPG registers, or you won't be able
6102ac6454SAndrew Thompson  *   to send any packets.
6202ac6454SAndrew Thompson  *
6302ac6454SAndrew Thompson  * Note that this device appears to only support loading the station
6402ac6454SAndrew Thompson  * address via autload from the EEPROM (i.e. there's no way to manaully
6502ac6454SAndrew Thompson  * set it).
6602ac6454SAndrew Thompson  *
6702ac6454SAndrew Thompson  * (Adam Weinberger wanted me to name this driver if_gir.c.)
6802ac6454SAndrew Thompson  */
6902ac6454SAndrew Thompson 
7002ac6454SAndrew Thompson /*
7102ac6454SAndrew Thompson  * Ax88178 and Ax88772 support backported from the OpenBSD driver.
7202ac6454SAndrew Thompson  * 2007/02/12, J.R. Oldroyd, fbsd@opal.com
7302ac6454SAndrew Thompson  *
7402ac6454SAndrew Thompson  * Manual here:
7502ac6454SAndrew Thompson  * http://www.asix.com.tw/FrootAttach/datasheet/AX88178_datasheet_Rev10.pdf
7602ac6454SAndrew Thompson  * http://www.asix.com.tw/FrootAttach/datasheet/AX88772_datasheet_Rev10.pdf
7702ac6454SAndrew Thompson  */
7802ac6454SAndrew Thompson 
79ed6d949aSAndrew Thompson #include <sys/stdint.h>
80ed6d949aSAndrew Thompson #include <sys/stddef.h>
81ed6d949aSAndrew Thompson #include <sys/param.h>
82ed6d949aSAndrew Thompson #include <sys/queue.h>
83ed6d949aSAndrew Thompson #include <sys/types.h>
84ed6d949aSAndrew Thompson #include <sys/systm.h>
85ed6d949aSAndrew Thompson #include <sys/kernel.h>
86ed6d949aSAndrew Thompson #include <sys/bus.h>
87ed6d949aSAndrew Thompson #include <sys/module.h>
88ed6d949aSAndrew Thompson #include <sys/lock.h>
89ed6d949aSAndrew Thompson #include <sys/mutex.h>
90ed6d949aSAndrew Thompson #include <sys/condvar.h>
91ed6d949aSAndrew Thompson #include <sys/sysctl.h>
92ed6d949aSAndrew Thompson #include <sys/sx.h>
93ed6d949aSAndrew Thompson #include <sys/unistd.h>
94ed6d949aSAndrew Thompson #include <sys/callout.h>
95ed6d949aSAndrew Thompson #include <sys/malloc.h>
96ed6d949aSAndrew Thompson #include <sys/priv.h>
97ed6d949aSAndrew Thompson 
9802ac6454SAndrew Thompson #include <dev/usb/usb.h>
99ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h>
100ed6d949aSAndrew Thompson #include <dev/usb/usbdi_util.h>
101ed6d949aSAndrew Thompson #include "usbdevs.h"
10202ac6454SAndrew Thompson 
10302ac6454SAndrew Thompson #define	USB_DEBUG_VAR axe_debug
10402ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
105ed6d949aSAndrew Thompson #include <dev/usb/usb_process.h>
10602ac6454SAndrew Thompson 
10702ac6454SAndrew Thompson #include <dev/usb/net/usb_ethernet.h>
10802ac6454SAndrew Thompson #include <dev/usb/net/if_axereg.h>
10902ac6454SAndrew Thompson 
11002ac6454SAndrew Thompson /*
11102ac6454SAndrew Thompson  * AXE_178_MAX_FRAME_BURST
11202ac6454SAndrew Thompson  * max frame burst size for Ax88178 and Ax88772
11302ac6454SAndrew Thompson  *	0	2048 bytes
11402ac6454SAndrew Thompson  *	1	4096 bytes
11502ac6454SAndrew Thompson  *	2	8192 bytes
11602ac6454SAndrew Thompson  *	3	16384 bytes
11702ac6454SAndrew Thompson  * use the largest your system can handle without USB stalling.
11802ac6454SAndrew Thompson  *
11902ac6454SAndrew Thompson  * NB: 88772 parts appear to generate lots of input errors with
12002ac6454SAndrew Thompson  * a 2K rx buffer and 8K is only slightly faster than 4K on an
12102ac6454SAndrew Thompson  * EHCI port on a T42 so change at your own risk.
12202ac6454SAndrew Thompson  */
12302ac6454SAndrew Thompson #define AXE_178_MAX_FRAME_BURST	1
12402ac6454SAndrew Thompson 
125b850ecc1SAndrew Thompson #ifdef USB_DEBUG
12602ac6454SAndrew Thompson static int axe_debug = 0;
12702ac6454SAndrew Thompson 
1289360ae40SAndrew Thompson SYSCTL_NODE(_hw_usb, OID_AUTO, axe, CTLFLAG_RW, 0, "USB axe");
1299360ae40SAndrew Thompson SYSCTL_INT(_hw_usb_axe, OID_AUTO, debug, CTLFLAG_RW, &axe_debug, 0,
13002ac6454SAndrew Thompson     "Debug level");
13102ac6454SAndrew Thompson #endif
13202ac6454SAndrew Thompson 
13302ac6454SAndrew Thompson /*
13402ac6454SAndrew Thompson  * Various supported device vendors/products.
13502ac6454SAndrew Thompson  */
136f1a16106SHans Petter Selasky static const STRUCT_USB_HOST_ID axe_devs[] = {
1379e6b5313SAndrew Thompson #define	AXE_DEV(v,p,i) { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, i) }
1389e6b5313SAndrew Thompson 	AXE_DEV(ABOCOM, UF200, 0),
1399e6b5313SAndrew Thompson 	AXE_DEV(ACERCM, EP1427X2, 0),
1409e6b5313SAndrew Thompson 	AXE_DEV(APPLE, ETHERNET, AXE_FLAG_772),
1419e6b5313SAndrew Thompson 	AXE_DEV(ASIX, AX88172, 0),
1429e6b5313SAndrew Thompson 	AXE_DEV(ASIX, AX88178, AXE_FLAG_178),
1439e6b5313SAndrew Thompson 	AXE_DEV(ASIX, AX88772, AXE_FLAG_772),
14445b0a349SPyun YongHyeon 	AXE_DEV(ASIX, AX88772A, AXE_FLAG_772A),
145*14ae1fa4SPyun YongHyeon 	AXE_DEV(ASIX, AX88772B, AXE_FLAG_772B),
1469e6b5313SAndrew Thompson 	AXE_DEV(ATEN, UC210T, 0),
1479e6b5313SAndrew Thompson 	AXE_DEV(BELKIN, F5D5055, AXE_FLAG_178),
1489e6b5313SAndrew Thompson 	AXE_DEV(BILLIONTON, USB2AR, 0),
14945b0a349SPyun YongHyeon 	AXE_DEV(CISCOLINKSYS, USB200MV2, AXE_FLAG_772A),
1509e6b5313SAndrew Thompson 	AXE_DEV(COREGA, FETHER_USB2_TX, 0),
1519e6b5313SAndrew Thompson 	AXE_DEV(DLINK, DUBE100, 0),
1529e6b5313SAndrew Thompson 	AXE_DEV(DLINK, DUBE100B1, AXE_FLAG_772),
1539e6b5313SAndrew Thompson 	AXE_DEV(GOODWAY, GWUSB2E, 0),
1549e6b5313SAndrew Thompson 	AXE_DEV(IODATA, ETGUS2, AXE_FLAG_178),
1559e6b5313SAndrew Thompson 	AXE_DEV(JVC, MP_PRX1, 0),
1569e6b5313SAndrew Thompson 	AXE_DEV(LINKSYS2, USB200M, 0),
1579e6b5313SAndrew Thompson 	AXE_DEV(LINKSYS4, USB1000, AXE_FLAG_178),
15887b6f185SMIHIRA Sanpei Yoshiro 	AXE_DEV(LOGITEC, LAN_GTJU2A, AXE_FLAG_178),
1599e6b5313SAndrew Thompson 	AXE_DEV(MELCO, LUAU2KTX, 0),
16087b6f185SMIHIRA Sanpei Yoshiro 	AXE_DEV(MELCO, LUA3U2AGT, AXE_FLAG_178),
1619e6b5313SAndrew Thompson 	AXE_DEV(NETGEAR, FA120, 0),
1629e6b5313SAndrew Thompson 	AXE_DEV(OQO, ETHER01PLUS, AXE_FLAG_772),
1639e6b5313SAndrew Thompson 	AXE_DEV(PLANEX3, GU1000T, AXE_FLAG_178),
1649e6b5313SAndrew Thompson 	AXE_DEV(SITECOM, LN029, 0),
1659e6b5313SAndrew Thompson 	AXE_DEV(SITECOMEU, LN028, AXE_FLAG_178),
1669e6b5313SAndrew Thompson 	AXE_DEV(SYSTEMTALKS, SGCX2UL, 0),
1679e6b5313SAndrew Thompson #undef AXE_DEV
16802ac6454SAndrew Thompson };
16902ac6454SAndrew Thompson 
17002ac6454SAndrew Thompson static device_probe_t axe_probe;
17102ac6454SAndrew Thompson static device_attach_t axe_attach;
17202ac6454SAndrew Thompson static device_detach_t axe_detach;
17302ac6454SAndrew Thompson 
174e0a69b51SAndrew Thompson static usb_callback_t axe_bulk_read_callback;
175e0a69b51SAndrew Thompson static usb_callback_t axe_bulk_write_callback;
17602ac6454SAndrew Thompson 
17702ac6454SAndrew Thompson static miibus_readreg_t axe_miibus_readreg;
17802ac6454SAndrew Thompson static miibus_writereg_t axe_miibus_writereg;
17902ac6454SAndrew Thompson static miibus_statchg_t axe_miibus_statchg;
18002ac6454SAndrew Thompson 
181e0a69b51SAndrew Thompson static uether_fn_t axe_attach_post;
182e0a69b51SAndrew Thompson static uether_fn_t axe_init;
183e0a69b51SAndrew Thompson static uether_fn_t axe_stop;
184e0a69b51SAndrew Thompson static uether_fn_t axe_start;
185e0a69b51SAndrew Thompson static uether_fn_t axe_tick;
186e0a69b51SAndrew Thompson static uether_fn_t axe_setmulti;
187e0a69b51SAndrew Thompson static uether_fn_t axe_setpromisc;
18802ac6454SAndrew Thompson 
18902ac6454SAndrew Thompson static int	axe_ifmedia_upd(struct ifnet *);
19002ac6454SAndrew Thompson static void	axe_ifmedia_sts(struct ifnet *, struct ifmediareq *);
19102ac6454SAndrew Thompson static int	axe_cmd(struct axe_softc *, int, int, int, void *);
19202ac6454SAndrew Thompson static void	axe_ax88178_init(struct axe_softc *);
19302ac6454SAndrew Thompson static void	axe_ax88772_init(struct axe_softc *);
194*14ae1fa4SPyun YongHyeon static void	axe_ax88772_phywake(struct axe_softc *);
19545b0a349SPyun YongHyeon static void	axe_ax88772a_init(struct axe_softc *);
196*14ae1fa4SPyun YongHyeon static void	axe_ax88772b_init(struct axe_softc *);
19702ac6454SAndrew Thompson static int	axe_get_phyno(struct axe_softc *, int);
19802ac6454SAndrew Thompson 
199760bc48eSAndrew Thompson static const struct usb_config axe_config[AXE_N_TRANSFER] = {
20002ac6454SAndrew Thompson 
20102ac6454SAndrew Thompson 	[AXE_BULK_DT_WR] = {
20202ac6454SAndrew Thompson 		.type = UE_BULK,
20302ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
20402ac6454SAndrew Thompson 		.direction = UE_DIR_OUT,
20565c12bf5SPyun YongHyeon 		.frames = 16,
20665c12bf5SPyun YongHyeon 		.bufsize = 16 * MCLBYTES,
2074eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
2084eae601eSAndrew Thompson 		.callback = axe_bulk_write_callback,
2094eae601eSAndrew Thompson 		.timeout = 10000,	/* 10 seconds */
21002ac6454SAndrew Thompson 	},
21102ac6454SAndrew Thompson 
21202ac6454SAndrew Thompson 	[AXE_BULK_DT_RD] = {
21302ac6454SAndrew Thompson 		.type = UE_BULK,
21402ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
21502ac6454SAndrew Thompson 		.direction = UE_DIR_IN,
2163ac526e0SAndrew Thompson 		.bufsize = 16384,	/* bytes */
2174eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
2184eae601eSAndrew Thompson 		.callback = axe_bulk_read_callback,
2194eae601eSAndrew Thompson 		.timeout = 0,	/* no timeout */
22002ac6454SAndrew Thompson 	},
22102ac6454SAndrew Thompson };
22202ac6454SAndrew Thompson 
223*14ae1fa4SPyun YongHyeon static const struct ax88772b_mfb ax88772b_mfb_table[] = {
224*14ae1fa4SPyun YongHyeon 	{ 0x8000, 0x8001, 2048 },
225*14ae1fa4SPyun YongHyeon 	{ 0x8100, 0x8147, 4096},
226*14ae1fa4SPyun YongHyeon 	{ 0x8200, 0x81EB, 6144},
227*14ae1fa4SPyun YongHyeon 	{ 0x8300, 0x83D7, 8192},
228*14ae1fa4SPyun YongHyeon 	{ 0x8400, 0x851E, 16384},
229*14ae1fa4SPyun YongHyeon 	{ 0x8500, 0x8666, 20480},
230*14ae1fa4SPyun YongHyeon 	{ 0x8600, 0x87AE, 24576},
231*14ae1fa4SPyun YongHyeon 	{ 0x8700, 0x8A3D, 32768}
232*14ae1fa4SPyun YongHyeon };
233*14ae1fa4SPyun YongHyeon 
23402ac6454SAndrew Thompson static device_method_t axe_methods[] = {
23502ac6454SAndrew Thompson 	/* Device interface */
23602ac6454SAndrew Thompson 	DEVMETHOD(device_probe, axe_probe),
23702ac6454SAndrew Thompson 	DEVMETHOD(device_attach, axe_attach),
23802ac6454SAndrew Thompson 	DEVMETHOD(device_detach, axe_detach),
23902ac6454SAndrew Thompson 
24002ac6454SAndrew Thompson 	/* bus interface */
24102ac6454SAndrew Thompson 	DEVMETHOD(bus_print_child, bus_generic_print_child),
24202ac6454SAndrew Thompson 	DEVMETHOD(bus_driver_added, bus_generic_driver_added),
24302ac6454SAndrew Thompson 
24402ac6454SAndrew Thompson 	/* MII interface */
24502ac6454SAndrew Thompson 	DEVMETHOD(miibus_readreg, axe_miibus_readreg),
24602ac6454SAndrew Thompson 	DEVMETHOD(miibus_writereg, axe_miibus_writereg),
24702ac6454SAndrew Thompson 	DEVMETHOD(miibus_statchg, axe_miibus_statchg),
24802ac6454SAndrew Thompson 
24902ac6454SAndrew Thompson 	{0, 0}
25002ac6454SAndrew Thompson };
25102ac6454SAndrew Thompson 
25202ac6454SAndrew Thompson static driver_t axe_driver = {
25302ac6454SAndrew Thompson 	.name = "axe",
25402ac6454SAndrew Thompson 	.methods = axe_methods,
25502ac6454SAndrew Thompson 	.size = sizeof(struct axe_softc),
25602ac6454SAndrew Thompson };
25702ac6454SAndrew Thompson 
25802ac6454SAndrew Thompson static devclass_t axe_devclass;
25902ac6454SAndrew Thompson 
2609aef556dSAndrew Thompson DRIVER_MODULE(axe, uhub, axe_driver, axe_devclass, NULL, 0);
26102ac6454SAndrew Thompson DRIVER_MODULE(miibus, axe, miibus_driver, miibus_devclass, 0, 0);
26202ac6454SAndrew Thompson MODULE_DEPEND(axe, uether, 1, 1, 1);
26302ac6454SAndrew Thompson MODULE_DEPEND(axe, usb, 1, 1, 1);
26402ac6454SAndrew Thompson MODULE_DEPEND(axe, ether, 1, 1, 1);
26502ac6454SAndrew Thompson MODULE_DEPEND(axe, miibus, 1, 1, 1);
266910cb8feSAndrew Thompson MODULE_VERSION(axe, 1);
26702ac6454SAndrew Thompson 
268760bc48eSAndrew Thompson static const struct usb_ether_methods axe_ue_methods = {
26902ac6454SAndrew Thompson 	.ue_attach_post = axe_attach_post,
27002ac6454SAndrew Thompson 	.ue_start = axe_start,
27102ac6454SAndrew Thompson 	.ue_init = axe_init,
27202ac6454SAndrew Thompson 	.ue_stop = axe_stop,
27302ac6454SAndrew Thompson 	.ue_tick = axe_tick,
27402ac6454SAndrew Thompson 	.ue_setmulti = axe_setmulti,
27502ac6454SAndrew Thompson 	.ue_setpromisc = axe_setpromisc,
27602ac6454SAndrew Thompson 	.ue_mii_upd = axe_ifmedia_upd,
27702ac6454SAndrew Thompson 	.ue_mii_sts = axe_ifmedia_sts,
27802ac6454SAndrew Thompson };
27902ac6454SAndrew Thompson 
28002ac6454SAndrew Thompson static int
28102ac6454SAndrew Thompson axe_cmd(struct axe_softc *sc, int cmd, int index, int val, void *buf)
28202ac6454SAndrew Thompson {
283760bc48eSAndrew Thompson 	struct usb_device_request req;
284e0a69b51SAndrew Thompson 	usb_error_t err;
28502ac6454SAndrew Thompson 
28602ac6454SAndrew Thompson 	AXE_LOCK_ASSERT(sc, MA_OWNED);
28702ac6454SAndrew Thompson 
28802ac6454SAndrew Thompson 	req.bmRequestType = (AXE_CMD_IS_WRITE(cmd) ?
28902ac6454SAndrew Thompson 	    UT_WRITE_VENDOR_DEVICE :
29002ac6454SAndrew Thompson 	    UT_READ_VENDOR_DEVICE);
29102ac6454SAndrew Thompson 	req.bRequest = AXE_CMD_CMD(cmd);
29202ac6454SAndrew Thompson 	USETW(req.wValue, val);
29302ac6454SAndrew Thompson 	USETW(req.wIndex, index);
29402ac6454SAndrew Thompson 	USETW(req.wLength, AXE_CMD_LEN(cmd));
29502ac6454SAndrew Thompson 
296a593f6b8SAndrew Thompson 	err = uether_do_request(&sc->sc_ue, &req, buf, 1000);
29702ac6454SAndrew Thompson 
29802ac6454SAndrew Thompson 	return (err);
29902ac6454SAndrew Thompson }
30002ac6454SAndrew Thompson 
30102ac6454SAndrew Thompson static int
30202ac6454SAndrew Thompson axe_miibus_readreg(device_t dev, int phy, int reg)
30302ac6454SAndrew Thompson {
30402ac6454SAndrew Thompson 	struct axe_softc *sc = device_get_softc(dev);
30502ac6454SAndrew Thompson 	uint16_t val;
30602ac6454SAndrew Thompson 	int locked;
30702ac6454SAndrew Thompson 
30802ac6454SAndrew Thompson 	if (sc->sc_phyno != phy)
30902ac6454SAndrew Thompson 		return (0);
31002ac6454SAndrew Thompson 
31102ac6454SAndrew Thompson 	locked = mtx_owned(&sc->sc_mtx);
31202ac6454SAndrew Thompson 	if (!locked)
31302ac6454SAndrew Thompson 		AXE_LOCK(sc);
31402ac6454SAndrew Thompson 
31502ac6454SAndrew Thompson 	axe_cmd(sc, AXE_CMD_MII_OPMODE_SW, 0, 0, NULL);
31602ac6454SAndrew Thompson 	axe_cmd(sc, AXE_CMD_MII_READ_REG, reg, phy, &val);
31702ac6454SAndrew Thompson 	axe_cmd(sc, AXE_CMD_MII_OPMODE_HW, 0, 0, NULL);
31802ac6454SAndrew Thompson 
31902ac6454SAndrew Thompson 	val = le16toh(val);
3208c09fbe4SPyun YongHyeon 	if (AXE_IS_772(sc) && reg == MII_BMSR) {
32102ac6454SAndrew Thompson 		/*
32202ac6454SAndrew Thompson 		 * BMSR of AX88772 indicates that it supports extended
32302ac6454SAndrew Thompson 		 * capability but the extended status register is
32402ac6454SAndrew Thompson 		 * revered for embedded ethernet PHY. So clear the
32502ac6454SAndrew Thompson 		 * extended capability bit of BMSR.
32602ac6454SAndrew Thompson 		 */
32702ac6454SAndrew Thompson 		val &= ~BMSR_EXTCAP;
32802ac6454SAndrew Thompson 	}
32902ac6454SAndrew Thompson 
33002ac6454SAndrew Thompson 	if (!locked)
33102ac6454SAndrew Thompson 		AXE_UNLOCK(sc);
33202ac6454SAndrew Thompson 	return (val);
33302ac6454SAndrew Thompson }
33402ac6454SAndrew Thompson 
33502ac6454SAndrew Thompson static int
33602ac6454SAndrew Thompson axe_miibus_writereg(device_t dev, int phy, int reg, int val)
33702ac6454SAndrew Thompson {
33802ac6454SAndrew Thompson 	struct axe_softc *sc = device_get_softc(dev);
33902ac6454SAndrew Thompson 	int locked;
34002ac6454SAndrew Thompson 
34136002e92SAndrew Thompson 	val = htole32(val);
34202ac6454SAndrew Thompson 
34302ac6454SAndrew Thompson 	if (sc->sc_phyno != phy)
34402ac6454SAndrew Thompson 		return (0);
34502ac6454SAndrew Thompson 
34602ac6454SAndrew Thompson 	locked = mtx_owned(&sc->sc_mtx);
34702ac6454SAndrew Thompson 	if (!locked)
34802ac6454SAndrew Thompson 		AXE_LOCK(sc);
34902ac6454SAndrew Thompson 
35002ac6454SAndrew Thompson 	axe_cmd(sc, AXE_CMD_MII_OPMODE_SW, 0, 0, NULL);
35102ac6454SAndrew Thompson 	axe_cmd(sc, AXE_CMD_MII_WRITE_REG, reg, phy, &val);
35202ac6454SAndrew Thompson 	axe_cmd(sc, AXE_CMD_MII_OPMODE_HW, 0, 0, NULL);
35302ac6454SAndrew Thompson 
35402ac6454SAndrew Thompson 	if (!locked)
35502ac6454SAndrew Thompson 		AXE_UNLOCK(sc);
35602ac6454SAndrew Thompson 	return (0);
35702ac6454SAndrew Thompson }
35802ac6454SAndrew Thompson 
35902ac6454SAndrew Thompson static void
36002ac6454SAndrew Thompson axe_miibus_statchg(device_t dev)
36102ac6454SAndrew Thompson {
36202ac6454SAndrew Thompson 	struct axe_softc *sc = device_get_softc(dev);
36302ac6454SAndrew Thompson 	struct mii_data *mii = GET_MII(sc);
36402ac6454SAndrew Thompson 	struct ifnet *ifp;
36502ac6454SAndrew Thompson 	uint16_t val;
36602ac6454SAndrew Thompson 	int err, locked;
36702ac6454SAndrew Thompson 
36802ac6454SAndrew Thompson 	locked = mtx_owned(&sc->sc_mtx);
36902ac6454SAndrew Thompson 	if (!locked)
37002ac6454SAndrew Thompson 		AXE_LOCK(sc);
37102ac6454SAndrew Thompson 
372a593f6b8SAndrew Thompson 	ifp = uether_getifp(&sc->sc_ue);
37302ac6454SAndrew Thompson 	if (mii == NULL || ifp == NULL ||
37402ac6454SAndrew Thompson 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
37502ac6454SAndrew Thompson 		goto done;
37602ac6454SAndrew Thompson 
37702ac6454SAndrew Thompson 	sc->sc_flags &= ~AXE_FLAG_LINK;
37802ac6454SAndrew Thompson 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
37902ac6454SAndrew Thompson 	    (IFM_ACTIVE | IFM_AVALID)) {
38002ac6454SAndrew Thompson 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
38102ac6454SAndrew Thompson 		case IFM_10_T:
38202ac6454SAndrew Thompson 		case IFM_100_TX:
38302ac6454SAndrew Thompson 			sc->sc_flags |= AXE_FLAG_LINK;
38402ac6454SAndrew Thompson 			break;
38502ac6454SAndrew Thompson 		case IFM_1000_T:
38602ac6454SAndrew Thompson 			if ((sc->sc_flags & AXE_FLAG_178) == 0)
38702ac6454SAndrew Thompson 				break;
38802ac6454SAndrew Thompson 			sc->sc_flags |= AXE_FLAG_LINK;
38902ac6454SAndrew Thompson 			break;
39002ac6454SAndrew Thompson 		default:
39102ac6454SAndrew Thompson 			break;
39202ac6454SAndrew Thompson 		}
39302ac6454SAndrew Thompson 	}
39402ac6454SAndrew Thompson 
39502ac6454SAndrew Thompson 	/* Lost link, do nothing. */
39602ac6454SAndrew Thompson 	if ((sc->sc_flags & AXE_FLAG_LINK) == 0)
39702ac6454SAndrew Thompson 		goto done;
39802ac6454SAndrew Thompson 
39902ac6454SAndrew Thompson 	val = 0;
40002ac6454SAndrew Thompson 	if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0)
40102ac6454SAndrew Thompson 		val |= AXE_MEDIA_FULL_DUPLEX;
4028c09fbe4SPyun YongHyeon 	if (AXE_IS_178_FAMILY(sc)) {
40302ac6454SAndrew Thompson 		val |= AXE_178_MEDIA_RX_EN | AXE_178_MEDIA_MAGIC;
40402ac6454SAndrew Thompson 		if ((sc->sc_flags & AXE_FLAG_178) != 0)
40502ac6454SAndrew Thompson 			val |= AXE_178_MEDIA_ENCK;
40602ac6454SAndrew Thompson 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
40702ac6454SAndrew Thompson 		case IFM_1000_T:
40802ac6454SAndrew Thompson 			val |= AXE_178_MEDIA_GMII | AXE_178_MEDIA_ENCK;
40902ac6454SAndrew Thompson 			break;
41002ac6454SAndrew Thompson 		case IFM_100_TX:
41102ac6454SAndrew Thompson 			val |= AXE_178_MEDIA_100TX;
41202ac6454SAndrew Thompson 			break;
41302ac6454SAndrew Thompson 		case IFM_10_T:
41402ac6454SAndrew Thompson 			/* doesn't need to be handled */
41502ac6454SAndrew Thompson 			break;
41602ac6454SAndrew Thompson 		}
41702ac6454SAndrew Thompson 	}
41802ac6454SAndrew Thompson 	err = axe_cmd(sc, AXE_CMD_WRITE_MEDIA, 0, val, NULL);
41902ac6454SAndrew Thompson 	if (err)
42002ac6454SAndrew Thompson 		device_printf(dev, "media change failed, error %d\n", err);
42102ac6454SAndrew Thompson done:
42202ac6454SAndrew Thompson 	if (!locked)
42302ac6454SAndrew Thompson 		AXE_UNLOCK(sc);
42402ac6454SAndrew Thompson }
42502ac6454SAndrew Thompson 
42602ac6454SAndrew Thompson /*
42702ac6454SAndrew Thompson  * Set media options.
42802ac6454SAndrew Thompson  */
42902ac6454SAndrew Thompson static int
43002ac6454SAndrew Thompson axe_ifmedia_upd(struct ifnet *ifp)
43102ac6454SAndrew Thompson {
43202ac6454SAndrew Thompson 	struct axe_softc *sc = ifp->if_softc;
43302ac6454SAndrew Thompson 	struct mii_data *mii = GET_MII(sc);
4343fcb7a53SMarius Strobl 	struct mii_softc *miisc;
43502ac6454SAndrew Thompson 	int error;
43602ac6454SAndrew Thompson 
43702ac6454SAndrew Thompson 	AXE_LOCK_ASSERT(sc, MA_OWNED);
43802ac6454SAndrew Thompson 
43902ac6454SAndrew Thompson 	LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
4403fcb7a53SMarius Strobl 		PHY_RESET(miisc);
44102ac6454SAndrew Thompson 	error = mii_mediachg(mii);
44202ac6454SAndrew Thompson 	return (error);
44302ac6454SAndrew Thompson }
44402ac6454SAndrew Thompson 
44502ac6454SAndrew Thompson /*
44602ac6454SAndrew Thompson  * Report current media status.
44702ac6454SAndrew Thompson  */
44802ac6454SAndrew Thompson static void
44902ac6454SAndrew Thompson axe_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
45002ac6454SAndrew Thompson {
45102ac6454SAndrew Thompson 	struct axe_softc *sc = ifp->if_softc;
45202ac6454SAndrew Thompson 	struct mii_data *mii = GET_MII(sc);
45302ac6454SAndrew Thompson 
45402ac6454SAndrew Thompson 	AXE_LOCK(sc);
45502ac6454SAndrew Thompson 	mii_pollstat(mii);
45602ac6454SAndrew Thompson 	AXE_UNLOCK(sc);
45702ac6454SAndrew Thompson 	ifmr->ifm_active = mii->mii_media_active;
45802ac6454SAndrew Thompson 	ifmr->ifm_status = mii->mii_media_status;
45902ac6454SAndrew Thompson }
46002ac6454SAndrew Thompson 
46102ac6454SAndrew Thompson static void
462760bc48eSAndrew Thompson axe_setmulti(struct usb_ether *ue)
46302ac6454SAndrew Thompson {
464a593f6b8SAndrew Thompson 	struct axe_softc *sc = uether_getsc(ue);
465a593f6b8SAndrew Thompson 	struct ifnet *ifp = uether_getifp(ue);
46602ac6454SAndrew Thompson 	struct ifmultiaddr *ifma;
46702ac6454SAndrew Thompson 	uint32_t h = 0;
46802ac6454SAndrew Thompson 	uint16_t rxmode;
46902ac6454SAndrew Thompson 	uint8_t hashtbl[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
47002ac6454SAndrew Thompson 
47102ac6454SAndrew Thompson 	AXE_LOCK_ASSERT(sc, MA_OWNED);
47202ac6454SAndrew Thompson 
47302ac6454SAndrew Thompson 	axe_cmd(sc, AXE_CMD_RXCTL_READ, 0, 0, &rxmode);
47402ac6454SAndrew Thompson 	rxmode = le16toh(rxmode);
47502ac6454SAndrew Thompson 
47602ac6454SAndrew Thompson 	if (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) {
47702ac6454SAndrew Thompson 		rxmode |= AXE_RXCMD_ALLMULTI;
47802ac6454SAndrew Thompson 		axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
47902ac6454SAndrew Thompson 		return;
48002ac6454SAndrew Thompson 	}
48102ac6454SAndrew Thompson 	rxmode &= ~AXE_RXCMD_ALLMULTI;
48202ac6454SAndrew Thompson 
483eb956cd0SRobert Watson 	if_maddr_rlock(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 		hashtbl[h / 8] |= 1 << (h % 8);
49102ac6454SAndrew Thompson 	}
492eb956cd0SRobert Watson 	if_maddr_runlock(ifp);
49302ac6454SAndrew Thompson 
49402ac6454SAndrew Thompson 	axe_cmd(sc, AXE_CMD_WRITE_MCAST, 0, 0, (void *)&hashtbl);
49502ac6454SAndrew Thompson 	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
49602ac6454SAndrew Thompson }
49702ac6454SAndrew Thompson 
49802ac6454SAndrew Thompson static int
49902ac6454SAndrew Thompson axe_get_phyno(struct axe_softc *sc, int sel)
50002ac6454SAndrew Thompson {
50102ac6454SAndrew Thompson 	int phyno;
50202ac6454SAndrew Thompson 
50302ac6454SAndrew Thompson 	switch (AXE_PHY_TYPE(sc->sc_phyaddrs[sel])) {
50402ac6454SAndrew Thompson 	case PHY_TYPE_100_HOME:
50502ac6454SAndrew Thompson 	case PHY_TYPE_GIG:
50602ac6454SAndrew Thompson 		phyno = AXE_PHY_NO(sc->sc_phyaddrs[sel]);
50702ac6454SAndrew Thompson 		break;
50802ac6454SAndrew Thompson 	case PHY_TYPE_SPECIAL:
50902ac6454SAndrew Thompson 		/* FALLTHROUGH */
51002ac6454SAndrew Thompson 	case PHY_TYPE_RSVD:
51102ac6454SAndrew Thompson 		/* FALLTHROUGH */
51202ac6454SAndrew Thompson 	case PHY_TYPE_NON_SUP:
51302ac6454SAndrew Thompson 		/* FALLTHROUGH */
51402ac6454SAndrew Thompson 	default:
51502ac6454SAndrew Thompson 		phyno = -1;
51602ac6454SAndrew Thompson 		break;
51702ac6454SAndrew Thompson 	}
51802ac6454SAndrew Thompson 
51902ac6454SAndrew Thompson 	return (phyno);
52002ac6454SAndrew Thompson }
52102ac6454SAndrew Thompson 
522edd913ebSAndrew Thompson #define	AXE_GPIO_WRITE(x, y)	do {				\
523edd913ebSAndrew Thompson 	axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, (x), NULL);		\
524edd913ebSAndrew Thompson 	uether_pause(ue, (y));					\
525edd913ebSAndrew Thompson } while (0)
526edd913ebSAndrew Thompson 
52702ac6454SAndrew Thompson static void
52802ac6454SAndrew Thompson axe_ax88178_init(struct axe_softc *sc)
52902ac6454SAndrew Thompson {
530edd913ebSAndrew Thompson 	struct usb_ether *ue;
531082b7543SPyun YongHyeon 	int gpio0, ledmode, phymode;
532edd913ebSAndrew Thompson 	uint16_t eeprom, val;
53302ac6454SAndrew Thompson 
534edd913ebSAndrew Thompson 	ue = &sc->sc_ue;
53502ac6454SAndrew Thompson 	axe_cmd(sc, AXE_CMD_SROM_WR_ENABLE, 0, 0, NULL);
53602ac6454SAndrew Thompson 	/* XXX magic */
53702ac6454SAndrew Thompson 	axe_cmd(sc, AXE_CMD_SROM_READ, 0, 0x0017, &eeprom);
53802ac6454SAndrew Thompson 	eeprom = le16toh(eeprom);
53902ac6454SAndrew Thompson 	axe_cmd(sc, AXE_CMD_SROM_WR_DISABLE, 0, 0, NULL);
54002ac6454SAndrew Thompson 
54102ac6454SAndrew Thompson 	/* if EEPROM is invalid we have to use to GPIO0 */
54202ac6454SAndrew Thompson 	if (eeprom == 0xffff) {
543edd913ebSAndrew Thompson 		phymode = AXE_PHY_MODE_MARVELL;
54402ac6454SAndrew Thompson 		gpio0 = 1;
545082b7543SPyun YongHyeon 		ledmode = 0;
54602ac6454SAndrew Thompson 	} else {
547edd913ebSAndrew Thompson 		phymode = eeprom & 0x7f;
54802ac6454SAndrew Thompson 		gpio0 = (eeprom & 0x80) ? 0 : 1;
549082b7543SPyun YongHyeon 		ledmode = eeprom >> 8;
55002ac6454SAndrew Thompson 	}
55102ac6454SAndrew Thompson 
552edd913ebSAndrew Thompson 	if (bootverbose)
5536dd81ec7SPyun YongHyeon 		device_printf(sc->sc_ue.ue_dev,
5546dd81ec7SPyun YongHyeon 		    "EEPROM data : 0x%04x, phymode : 0x%02x\n", eeprom,
5556dd81ec7SPyun YongHyeon 		    phymode);
556edd913ebSAndrew Thompson 	/* Program GPIOs depending on PHY hardware. */
557edd913ebSAndrew Thompson 	switch (phymode) {
558edd913ebSAndrew Thompson 	case AXE_PHY_MODE_MARVELL:
559edd913ebSAndrew Thompson 		if (gpio0 == 1) {
560edd913ebSAndrew Thompson 			AXE_GPIO_WRITE(AXE_GPIO_RELOAD_EEPROM | AXE_GPIO0_EN,
561edd913ebSAndrew Thompson 			    hz / 32);
562edd913ebSAndrew Thompson 			AXE_GPIO_WRITE(AXE_GPIO0_EN | AXE_GPIO2 | AXE_GPIO2_EN,
563edd913ebSAndrew Thompson 			    hz / 32);
564edd913ebSAndrew Thompson 			AXE_GPIO_WRITE(AXE_GPIO0_EN | AXE_GPIO2_EN, hz / 4);
565edd913ebSAndrew Thompson 			AXE_GPIO_WRITE(AXE_GPIO0_EN | AXE_GPIO2 | AXE_GPIO2_EN,
566edd913ebSAndrew Thompson 			    hz / 32);
567082b7543SPyun YongHyeon 		} else {
568edd913ebSAndrew Thompson 			AXE_GPIO_WRITE(AXE_GPIO_RELOAD_EEPROM | AXE_GPIO1 |
569082b7543SPyun YongHyeon 			    AXE_GPIO1_EN, hz / 3);
570082b7543SPyun YongHyeon 			if (ledmode == 1) {
571082b7543SPyun YongHyeon 				AXE_GPIO_WRITE(AXE_GPIO1_EN, hz / 3);
572082b7543SPyun YongHyeon 				AXE_GPIO_WRITE(AXE_GPIO1 | AXE_GPIO1_EN,
573082b7543SPyun YongHyeon 				    hz / 3);
574082b7543SPyun YongHyeon 			} else {
575082b7543SPyun YongHyeon 				AXE_GPIO_WRITE(AXE_GPIO1 | AXE_GPIO1_EN |
576082b7543SPyun YongHyeon 				    AXE_GPIO2 | AXE_GPIO2_EN, hz / 32);
577082b7543SPyun YongHyeon 				AXE_GPIO_WRITE(AXE_GPIO1 | AXE_GPIO1_EN |
578082b7543SPyun YongHyeon 				    AXE_GPIO2_EN, hz / 4);
579082b7543SPyun YongHyeon 				AXE_GPIO_WRITE(AXE_GPIO1 | AXE_GPIO1_EN |
580082b7543SPyun YongHyeon 				    AXE_GPIO2 | AXE_GPIO2_EN, hz / 32);
581082b7543SPyun YongHyeon 			}
582082b7543SPyun YongHyeon 		}
583edd913ebSAndrew Thompson 		break;
584edd913ebSAndrew Thompson 	case AXE_PHY_MODE_CICADA:
5856dd81ec7SPyun YongHyeon 	case AXE_PHY_MODE_CICADA_V2:
5866dd81ec7SPyun YongHyeon 	case AXE_PHY_MODE_CICADA_V2_ASIX:
587edd913ebSAndrew Thompson 		if (gpio0 == 1)
588edd913ebSAndrew Thompson 			AXE_GPIO_WRITE(AXE_GPIO_RELOAD_EEPROM | AXE_GPIO0 |
589edd913ebSAndrew Thompson 			    AXE_GPIO0_EN, hz / 32);
590edd913ebSAndrew Thompson 		else
591edd913ebSAndrew Thompson 			AXE_GPIO_WRITE(AXE_GPIO_RELOAD_EEPROM | AXE_GPIO1 |
592edd913ebSAndrew Thompson 			    AXE_GPIO1_EN, hz / 32);
593edd913ebSAndrew Thompson 		break;
594edd913ebSAndrew Thompson 	case AXE_PHY_MODE_AGERE:
595edd913ebSAndrew Thompson 		AXE_GPIO_WRITE(AXE_GPIO_RELOAD_EEPROM | AXE_GPIO1 |
596edd913ebSAndrew Thompson 		    AXE_GPIO1_EN, hz / 32);
597edd913ebSAndrew Thompson 		AXE_GPIO_WRITE(AXE_GPIO1 | AXE_GPIO1_EN | AXE_GPIO2 |
598edd913ebSAndrew Thompson 		    AXE_GPIO2_EN, hz / 32);
599edd913ebSAndrew Thompson 		AXE_GPIO_WRITE(AXE_GPIO1 | AXE_GPIO1_EN | AXE_GPIO2_EN, hz / 4);
600edd913ebSAndrew Thompson 		AXE_GPIO_WRITE(AXE_GPIO1 | AXE_GPIO1_EN | AXE_GPIO2 |
601edd913ebSAndrew Thompson 		    AXE_GPIO2_EN, hz / 32);
602edd913ebSAndrew Thompson 		break;
603edd913ebSAndrew Thompson 	case AXE_PHY_MODE_REALTEK_8211CL:
604edd913ebSAndrew Thompson 	case AXE_PHY_MODE_REALTEK_8211BN:
605edd913ebSAndrew Thompson 	case AXE_PHY_MODE_REALTEK_8251CL:
606edd913ebSAndrew Thompson 		val = gpio0 == 1 ? AXE_GPIO0 | AXE_GPIO0_EN :
607edd913ebSAndrew Thompson 		    AXE_GPIO1 | AXE_GPIO1_EN;
608edd913ebSAndrew Thompson 		AXE_GPIO_WRITE(val, hz / 32);
609edd913ebSAndrew Thompson 		AXE_GPIO_WRITE(val | AXE_GPIO2 | AXE_GPIO2_EN, hz / 32);
610edd913ebSAndrew Thompson 		AXE_GPIO_WRITE(val | AXE_GPIO2_EN, hz / 4);
611edd913ebSAndrew Thompson 		AXE_GPIO_WRITE(val | AXE_GPIO2 | AXE_GPIO2_EN, hz / 32);
612edd913ebSAndrew Thompson 		if (phymode == AXE_PHY_MODE_REALTEK_8211CL) {
613edd913ebSAndrew Thompson 			axe_miibus_writereg(ue->ue_dev, sc->sc_phyno,
614edd913ebSAndrew Thompson 			    0x1F, 0x0005);
615edd913ebSAndrew Thompson 			axe_miibus_writereg(ue->ue_dev, sc->sc_phyno,
616edd913ebSAndrew Thompson 			    0x0C, 0x0000);
617edd913ebSAndrew Thompson 			val = axe_miibus_readreg(ue->ue_dev, sc->sc_phyno,
618edd913ebSAndrew Thompson 			    0x0001);
619edd913ebSAndrew Thompson 			axe_miibus_writereg(ue->ue_dev, sc->sc_phyno,
620edd913ebSAndrew Thompson 			    0x01, val | 0x0080);
621edd913ebSAndrew Thompson 			axe_miibus_writereg(ue->ue_dev, sc->sc_phyno,
622edd913ebSAndrew Thompson 			    0x1F, 0x0000);
623edd913ebSAndrew Thompson 		}
624edd913ebSAndrew Thompson 		break;
625edd913ebSAndrew Thompson 	default:
626edd913ebSAndrew Thompson 		/* Unknown PHY model or no need to program GPIOs. */
627edd913ebSAndrew Thompson 		break;
62802ac6454SAndrew Thompson 	}
62902ac6454SAndrew Thompson 
63002ac6454SAndrew Thompson 	/* soft reset */
63102ac6454SAndrew Thompson 	axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_CLEAR, NULL);
632edd913ebSAndrew Thompson 	uether_pause(ue, hz / 4);
63302ac6454SAndrew Thompson 
63402ac6454SAndrew Thompson 	axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
63502ac6454SAndrew Thompson 	    AXE_SW_RESET_PRL | AXE_178_RESET_MAGIC, NULL);
636edd913ebSAndrew Thompson 	uether_pause(ue, hz / 4);
63702ac6454SAndrew Thompson 	/* Enable MII/GMII/RGMII interface to work with external PHY. */
63802ac6454SAndrew Thompson 	axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, 0, NULL);
639edd913ebSAndrew Thompson 	uether_pause(ue, hz / 4);
64002ac6454SAndrew Thompson 
64102ac6454SAndrew Thompson 	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, 0, NULL);
64202ac6454SAndrew Thompson }
64302ac6454SAndrew Thompson 
64402ac6454SAndrew Thompson static void
64502ac6454SAndrew Thompson axe_ax88772_init(struct axe_softc *sc)
64602ac6454SAndrew Thompson {
64702ac6454SAndrew Thompson 	axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, 0x00b0, NULL);
648a593f6b8SAndrew Thompson 	uether_pause(&sc->sc_ue, hz / 16);
64902ac6454SAndrew Thompson 
65002ac6454SAndrew Thompson 	if (sc->sc_phyno == AXE_772_PHY_NO_EPHY) {
65102ac6454SAndrew Thompson 		/* ask for the embedded PHY */
65202ac6454SAndrew Thompson 		axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, 0x01, NULL);
653a593f6b8SAndrew Thompson 		uether_pause(&sc->sc_ue, hz / 64);
65402ac6454SAndrew Thompson 
65502ac6454SAndrew Thompson 		/* power down and reset state, pin reset state */
65602ac6454SAndrew Thompson 		axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
65702ac6454SAndrew Thompson 		    AXE_SW_RESET_CLEAR, NULL);
658a593f6b8SAndrew Thompson 		uether_pause(&sc->sc_ue, hz / 16);
65902ac6454SAndrew Thompson 
66002ac6454SAndrew Thompson 		/* power down/reset state, pin operating state */
66102ac6454SAndrew Thompson 		axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
66202ac6454SAndrew Thompson 		    AXE_SW_RESET_IPPD | AXE_SW_RESET_PRL, NULL);
663a593f6b8SAndrew Thompson 		uether_pause(&sc->sc_ue, hz / 4);
66402ac6454SAndrew Thompson 
66502ac6454SAndrew Thompson 		/* power up, reset */
66602ac6454SAndrew Thompson 		axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_PRL, NULL);
66702ac6454SAndrew Thompson 
66802ac6454SAndrew Thompson 		/* power up, operating */
66902ac6454SAndrew Thompson 		axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
67002ac6454SAndrew Thompson 		    AXE_SW_RESET_IPRL | AXE_SW_RESET_PRL, NULL);
67102ac6454SAndrew Thompson 	} else {
67202ac6454SAndrew Thompson 		/* ask for external PHY */
67302ac6454SAndrew Thompson 		axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, 0x00, NULL);
674a593f6b8SAndrew Thompson 		uether_pause(&sc->sc_ue, hz / 64);
67502ac6454SAndrew Thompson 
67602ac6454SAndrew Thompson 		/* power down internal PHY */
67702ac6454SAndrew Thompson 		axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
67802ac6454SAndrew Thompson 		    AXE_SW_RESET_IPPD | AXE_SW_RESET_PRL, NULL);
67902ac6454SAndrew Thompson 	}
68002ac6454SAndrew Thompson 
681a593f6b8SAndrew Thompson 	uether_pause(&sc->sc_ue, hz / 4);
68202ac6454SAndrew Thompson 	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, 0, NULL);
68302ac6454SAndrew Thompson }
68402ac6454SAndrew Thompson 
68502ac6454SAndrew Thompson static void
686*14ae1fa4SPyun YongHyeon axe_ax88772_phywake(struct axe_softc *sc)
68745b0a349SPyun YongHyeon {
68845b0a349SPyun YongHyeon 	struct usb_ether *ue;
68945b0a349SPyun YongHyeon 
69045b0a349SPyun YongHyeon 	ue = &sc->sc_ue;
69145b0a349SPyun YongHyeon 	if (sc->sc_phyno == AXE_772_PHY_NO_EPHY) {
69245b0a349SPyun YongHyeon 		/* Manually select internal(embedded) PHY - MAC mode. */
69345b0a349SPyun YongHyeon 		axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, AXE_SW_PHY_SELECT_SS_ENB |
69445b0a349SPyun YongHyeon 		    AXE_SW_PHY_SELECT_EMBEDDED | AXE_SW_PHY_SELECT_SS_MII,
69545b0a349SPyun YongHyeon 		    NULL);
69645b0a349SPyun YongHyeon 		uether_pause(&sc->sc_ue, hz / 32);
69745b0a349SPyun YongHyeon 	} else {
69845b0a349SPyun YongHyeon 		/*
69945b0a349SPyun YongHyeon 		 * Manually select external PHY - MAC mode.
70045b0a349SPyun YongHyeon 		 * Reverse MII/RMII is for AX88772A PHY mode.
70145b0a349SPyun YongHyeon 		 */
70245b0a349SPyun YongHyeon 		axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, AXE_SW_PHY_SELECT_SS_ENB |
70345b0a349SPyun YongHyeon 		    AXE_SW_PHY_SELECT_EXT | AXE_SW_PHY_SELECT_SS_MII, NULL);
70445b0a349SPyun YongHyeon 		uether_pause(&sc->sc_ue, hz / 32);
70545b0a349SPyun YongHyeon 	}
70645b0a349SPyun YongHyeon 	/* Take PHY out of power down. */
70745b0a349SPyun YongHyeon 	axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_IPPD |
70845b0a349SPyun YongHyeon 	    AXE_SW_RESET_IPRL, NULL);
70945b0a349SPyun YongHyeon 	uether_pause(&sc->sc_ue, hz / 4);
71045b0a349SPyun YongHyeon 	axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_IPRL, NULL);
71145b0a349SPyun YongHyeon 	uether_pause(&sc->sc_ue, hz);
71245b0a349SPyun YongHyeon 	axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_CLEAR, NULL);
71345b0a349SPyun YongHyeon 	uether_pause(&sc->sc_ue, hz / 32);
71445b0a349SPyun YongHyeon 	axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_IPRL, NULL);
71545b0a349SPyun YongHyeon 	uether_pause(&sc->sc_ue, hz / 32);
716*14ae1fa4SPyun YongHyeon }
717*14ae1fa4SPyun YongHyeon 
718*14ae1fa4SPyun YongHyeon static void
719*14ae1fa4SPyun YongHyeon axe_ax88772a_init(struct axe_softc *sc)
720*14ae1fa4SPyun YongHyeon {
721*14ae1fa4SPyun YongHyeon 	struct usb_ether *ue;
722*14ae1fa4SPyun YongHyeon 
723*14ae1fa4SPyun YongHyeon 	ue = &sc->sc_ue;
724*14ae1fa4SPyun YongHyeon 	/* Reload EEPROM. */
725*14ae1fa4SPyun YongHyeon 	AXE_GPIO_WRITE(AXE_GPIO_RELOAD_EEPROM, hz / 32);
726*14ae1fa4SPyun YongHyeon 	axe_ax88772_phywake(sc);
727*14ae1fa4SPyun YongHyeon 	/* Stop MAC. */
728*14ae1fa4SPyun YongHyeon 	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, 0, NULL);
729*14ae1fa4SPyun YongHyeon }
730*14ae1fa4SPyun YongHyeon 
731*14ae1fa4SPyun YongHyeon static void
732*14ae1fa4SPyun YongHyeon axe_ax88772b_init(struct axe_softc *sc)
733*14ae1fa4SPyun YongHyeon {
734*14ae1fa4SPyun YongHyeon 	struct usb_ether *ue;
735*14ae1fa4SPyun YongHyeon 	uint16_t eeprom;
736*14ae1fa4SPyun YongHyeon 	uint8_t *eaddr;
737*14ae1fa4SPyun YongHyeon 	int i;
738*14ae1fa4SPyun YongHyeon 
739*14ae1fa4SPyun YongHyeon 	ue = &sc->sc_ue;
740*14ae1fa4SPyun YongHyeon 	/* Reload EEPROM. */
741*14ae1fa4SPyun YongHyeon 	AXE_GPIO_WRITE(AXE_GPIO_RELOAD_EEPROM, hz / 32);
742*14ae1fa4SPyun YongHyeon 	/*
743*14ae1fa4SPyun YongHyeon 	 * Save PHY power saving configuration(high byte) and
744*14ae1fa4SPyun YongHyeon 	 * clear EEPROM checksum value(low byte).
745*14ae1fa4SPyun YongHyeon 	 */
746*14ae1fa4SPyun YongHyeon 	axe_cmd(sc, AXE_CMD_SROM_READ, 0, AXE_EEPROM_772B_PHY_PWRCFG, &eeprom);
747*14ae1fa4SPyun YongHyeon 	sc->sc_pwrcfg = le16toh(eeprom) & 0xFF00;
748*14ae1fa4SPyun YongHyeon 
749*14ae1fa4SPyun YongHyeon 	/*
750*14ae1fa4SPyun YongHyeon 	 * Auto-loaded default station address from internal ROM is
751*14ae1fa4SPyun YongHyeon 	 * 00:00:00:00:00:00 such that an explicit access to EEPROM
752*14ae1fa4SPyun YongHyeon 	 * is required to get real station address.
753*14ae1fa4SPyun YongHyeon 	 */
754*14ae1fa4SPyun YongHyeon 	eaddr = ue->ue_eaddr;
755*14ae1fa4SPyun YongHyeon 	for (i = 0; i < ETHER_ADDR_LEN / 2; i++) {
756*14ae1fa4SPyun YongHyeon 		axe_cmd(sc, AXE_CMD_SROM_READ, 0, AXE_EEPROM_772B_NODE_ID + i,
757*14ae1fa4SPyun YongHyeon 		    &eeprom);
758*14ae1fa4SPyun YongHyeon 		eeprom = le16toh(eeprom);
759*14ae1fa4SPyun YongHyeon 		*eaddr++ = (uint8_t)(eeprom & 0xFF);
760*14ae1fa4SPyun YongHyeon 		*eaddr++ = (uint8_t)((eeprom >> 8) & 0xFF);
761*14ae1fa4SPyun YongHyeon 	}
762*14ae1fa4SPyun YongHyeon 	/* Wakeup PHY. */
763*14ae1fa4SPyun YongHyeon 	axe_ax88772_phywake(sc);
764*14ae1fa4SPyun YongHyeon 	/* Stop MAC. */
76545b0a349SPyun YongHyeon 	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, 0, NULL);
76645b0a349SPyun YongHyeon }
76745b0a349SPyun YongHyeon 
76845b0a349SPyun YongHyeon #undef	AXE_GPIO_WRITE
76945b0a349SPyun YongHyeon 
77045b0a349SPyun YongHyeon static void
77102ac6454SAndrew Thompson axe_reset(struct axe_softc *sc)
77202ac6454SAndrew Thompson {
773760bc48eSAndrew Thompson 	struct usb_config_descriptor *cd;
774e0a69b51SAndrew Thompson 	usb_error_t err;
77502ac6454SAndrew Thompson 
776a593f6b8SAndrew Thompson 	cd = usbd_get_config_descriptor(sc->sc_ue.ue_udev);
77702ac6454SAndrew Thompson 
778a593f6b8SAndrew Thompson 	err = usbd_req_set_config(sc->sc_ue.ue_udev, &sc->sc_mtx,
77902ac6454SAndrew Thompson 	    cd->bConfigurationValue);
78002ac6454SAndrew Thompson 	if (err)
78102ac6454SAndrew Thompson 		DPRINTF("reset failed (ignored)\n");
78202ac6454SAndrew Thompson 
78302ac6454SAndrew Thompson 	/* Wait a little while for the chip to get its brains in order. */
784a593f6b8SAndrew Thompson 	uether_pause(&sc->sc_ue, hz / 100);
785ff5fbbc4SPyun YongHyeon 
786ff5fbbc4SPyun YongHyeon 	/* Reinitialize controller to achieve full reset. */
787ff5fbbc4SPyun YongHyeon 	if (sc->sc_flags & AXE_FLAG_178)
788ff5fbbc4SPyun YongHyeon 		axe_ax88178_init(sc);
789ff5fbbc4SPyun YongHyeon 	else if (sc->sc_flags & AXE_FLAG_772)
790ff5fbbc4SPyun YongHyeon 		axe_ax88772_init(sc);
79145b0a349SPyun YongHyeon 	else if (sc->sc_flags & AXE_FLAG_772A)
79245b0a349SPyun YongHyeon 		axe_ax88772a_init(sc);
793*14ae1fa4SPyun YongHyeon 	else if (sc->sc_flags & AXE_FLAG_772B)
794*14ae1fa4SPyun YongHyeon 		axe_ax88772b_init(sc);
79502ac6454SAndrew Thompson }
79602ac6454SAndrew Thompson 
79702ac6454SAndrew Thompson static void
798760bc48eSAndrew Thompson axe_attach_post(struct usb_ether *ue)
79902ac6454SAndrew Thompson {
800a593f6b8SAndrew Thompson 	struct axe_softc *sc = uether_getsc(ue);
80102ac6454SAndrew Thompson 
80202ac6454SAndrew Thompson 	/*
80302ac6454SAndrew Thompson 	 * Load PHY indexes first. Needed by axe_xxx_init().
80402ac6454SAndrew Thompson 	 */
80502ac6454SAndrew Thompson 	axe_cmd(sc, AXE_CMD_READ_PHYID, 0, 0, sc->sc_phyaddrs);
806edd913ebSAndrew Thompson 	if (bootverbose)
80702ac6454SAndrew Thompson 		device_printf(sc->sc_ue.ue_dev, "PHYADDR 0x%02x:0x%02x\n",
80802ac6454SAndrew Thompson 		    sc->sc_phyaddrs[0], sc->sc_phyaddrs[1]);
80902ac6454SAndrew Thompson 	sc->sc_phyno = axe_get_phyno(sc, AXE_PHY_SEL_PRI);
81002ac6454SAndrew Thompson 	if (sc->sc_phyno == -1)
81102ac6454SAndrew Thompson 		sc->sc_phyno = axe_get_phyno(sc, AXE_PHY_SEL_SEC);
81202ac6454SAndrew Thompson 	if (sc->sc_phyno == -1) {
81302ac6454SAndrew Thompson 		device_printf(sc->sc_ue.ue_dev,
81402ac6454SAndrew Thompson 		    "no valid PHY address found, assuming PHY address 0\n");
81502ac6454SAndrew Thompson 		sc->sc_phyno = 0;
81602ac6454SAndrew Thompson 	}
81702ac6454SAndrew Thompson 
818*14ae1fa4SPyun YongHyeon 	/* Initialize controller and get station address. */
8198c09fbe4SPyun YongHyeon 	if (sc->sc_flags & AXE_FLAG_178) {
82002ac6454SAndrew Thompson 		axe_ax88178_init(sc);
8218c09fbe4SPyun YongHyeon 		sc->sc_tx_bufsz = 16 * 1024;
822*14ae1fa4SPyun YongHyeon 		axe_cmd(sc, AXE_178_CMD_READ_NODEID, 0, 0, ue->ue_eaddr);
8238c09fbe4SPyun YongHyeon 	} else if (sc->sc_flags & AXE_FLAG_772) {
82402ac6454SAndrew Thompson 		axe_ax88772_init(sc);
8258c09fbe4SPyun YongHyeon 		sc->sc_tx_bufsz = 8 * 1024;
826*14ae1fa4SPyun YongHyeon 		axe_cmd(sc, AXE_178_CMD_READ_NODEID, 0, 0, ue->ue_eaddr);
82745b0a349SPyun YongHyeon 	} else if (sc->sc_flags & AXE_FLAG_772A) {
82845b0a349SPyun YongHyeon 		axe_ax88772a_init(sc);
82945b0a349SPyun YongHyeon 		sc->sc_tx_bufsz = 8 * 1024;
83002ac6454SAndrew Thompson 		axe_cmd(sc, AXE_178_CMD_READ_NODEID, 0, 0, ue->ue_eaddr);
831*14ae1fa4SPyun YongHyeon 	} else if (sc->sc_flags & AXE_FLAG_772B) {
832*14ae1fa4SPyun YongHyeon 		axe_ax88772b_init(sc);
833*14ae1fa4SPyun YongHyeon 		sc->sc_tx_bufsz = 8 * 1024;
834*14ae1fa4SPyun YongHyeon 	} else
83502ac6454SAndrew Thompson 		axe_cmd(sc, AXE_172_CMD_READ_NODEID, 0, 0, ue->ue_eaddr);
83602ac6454SAndrew Thompson 
83702ac6454SAndrew Thompson 	/*
83802ac6454SAndrew Thompson 	 * Fetch IPG values.
83902ac6454SAndrew Thompson 	 */
840*14ae1fa4SPyun YongHyeon 	if (sc->sc_flags & (AXE_FLAG_772A | AXE_FLAG_772B)) {
84145b0a349SPyun YongHyeon 		/* Set IPG values. */
84245b0a349SPyun YongHyeon 		sc->sc_ipgs[0] = 0x15;
84345b0a349SPyun YongHyeon 		sc->sc_ipgs[1] = 0x16;
84445b0a349SPyun YongHyeon 		sc->sc_ipgs[2] = 0x1A;
84545b0a349SPyun YongHyeon 	} else
84602ac6454SAndrew Thompson 		axe_cmd(sc, AXE_CMD_READ_IPG012, 0, 0, sc->sc_ipgs);
84702ac6454SAndrew Thompson }
84802ac6454SAndrew Thompson 
84902ac6454SAndrew Thompson /*
85002ac6454SAndrew Thompson  * Probe for a AX88172 chip.
85102ac6454SAndrew Thompson  */
85202ac6454SAndrew Thompson static int
85302ac6454SAndrew Thompson axe_probe(device_t dev)
85402ac6454SAndrew Thompson {
855760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
85602ac6454SAndrew Thompson 
857f29a0724SAndrew Thompson 	if (uaa->usb_mode != USB_MODE_HOST)
85802ac6454SAndrew Thompson 		return (ENXIO);
85902ac6454SAndrew Thompson 	if (uaa->info.bConfigIndex != AXE_CONFIG_IDX)
86002ac6454SAndrew Thompson 		return (ENXIO);
86102ac6454SAndrew Thompson 	if (uaa->info.bIfaceIndex != AXE_IFACE_IDX)
86202ac6454SAndrew Thompson 		return (ENXIO);
86302ac6454SAndrew Thompson 
864a593f6b8SAndrew Thompson 	return (usbd_lookup_id_by_uaa(axe_devs, sizeof(axe_devs), uaa));
86502ac6454SAndrew Thompson }
86602ac6454SAndrew Thompson 
86702ac6454SAndrew Thompson /*
86802ac6454SAndrew Thompson  * Attach the interface. Allocate softc structures, do ifmedia
86902ac6454SAndrew Thompson  * setup and ethernet/BPF attach.
87002ac6454SAndrew Thompson  */
87102ac6454SAndrew Thompson static int
87202ac6454SAndrew Thompson axe_attach(device_t dev)
87302ac6454SAndrew Thompson {
874760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
87502ac6454SAndrew Thompson 	struct axe_softc *sc = device_get_softc(dev);
876760bc48eSAndrew Thompson 	struct usb_ether *ue = &sc->sc_ue;
87702ac6454SAndrew Thompson 	uint8_t iface_index;
87802ac6454SAndrew Thompson 	int error;
87902ac6454SAndrew Thompson 
88002ac6454SAndrew Thompson 	sc->sc_flags = USB_GET_DRIVER_INFO(uaa);
88102ac6454SAndrew Thompson 
882a593f6b8SAndrew Thompson 	device_set_usb_desc(dev);
88302ac6454SAndrew Thompson 
88402ac6454SAndrew Thompson 	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
88502ac6454SAndrew Thompson 
88602ac6454SAndrew Thompson 	iface_index = AXE_IFACE_IDX;
887a593f6b8SAndrew Thompson 	error = usbd_transfer_setup(uaa->device, &iface_index, sc->sc_xfer,
88802ac6454SAndrew Thompson 	    axe_config, AXE_N_TRANSFER, sc, &sc->sc_mtx);
88902ac6454SAndrew Thompson 	if (error) {
890767cb2e2SAndrew Thompson 		device_printf(dev, "allocating USB transfers failed\n");
89102ac6454SAndrew Thompson 		goto detach;
89202ac6454SAndrew Thompson 	}
89302ac6454SAndrew Thompson 
89402ac6454SAndrew Thompson 	ue->ue_sc = sc;
89502ac6454SAndrew Thompson 	ue->ue_dev = dev;
89602ac6454SAndrew Thompson 	ue->ue_udev = uaa->device;
89702ac6454SAndrew Thompson 	ue->ue_mtx = &sc->sc_mtx;
89802ac6454SAndrew Thompson 	ue->ue_methods = &axe_ue_methods;
89902ac6454SAndrew Thompson 
900a593f6b8SAndrew Thompson 	error = uether_ifattach(ue);
90102ac6454SAndrew Thompson 	if (error) {
90202ac6454SAndrew Thompson 		device_printf(dev, "could not attach interface\n");
90302ac6454SAndrew Thompson 		goto detach;
90402ac6454SAndrew Thompson 	}
90502ac6454SAndrew Thompson 	return (0);			/* success */
90602ac6454SAndrew Thompson 
90702ac6454SAndrew Thompson detach:
90802ac6454SAndrew Thompson 	axe_detach(dev);
90902ac6454SAndrew Thompson 	return (ENXIO);			/* failure */
91002ac6454SAndrew Thompson }
91102ac6454SAndrew Thompson 
91202ac6454SAndrew Thompson static int
91302ac6454SAndrew Thompson axe_detach(device_t dev)
91402ac6454SAndrew Thompson {
91502ac6454SAndrew Thompson 	struct axe_softc *sc = device_get_softc(dev);
916760bc48eSAndrew Thompson 	struct usb_ether *ue = &sc->sc_ue;
91702ac6454SAndrew Thompson 
918a593f6b8SAndrew Thompson 	usbd_transfer_unsetup(sc->sc_xfer, AXE_N_TRANSFER);
919a593f6b8SAndrew Thompson 	uether_ifdetach(ue);
92002ac6454SAndrew Thompson 	mtx_destroy(&sc->sc_mtx);
92102ac6454SAndrew Thompson 
92202ac6454SAndrew Thompson 	return (0);
92302ac6454SAndrew Thompson }
92402ac6454SAndrew Thompson 
92502ac6454SAndrew Thompson #if (AXE_BULK_BUF_SIZE >= 0x10000)
92602ac6454SAndrew Thompson #error "Please update axe_bulk_read_callback()!"
92702ac6454SAndrew Thompson #endif
92802ac6454SAndrew Thompson 
92902ac6454SAndrew Thompson static void
930ed6d949aSAndrew Thompson axe_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
93102ac6454SAndrew Thompson {
932ed6d949aSAndrew Thompson 	struct axe_softc *sc = usbd_xfer_softc(xfer);
933760bc48eSAndrew Thompson 	struct usb_ether *ue = &sc->sc_ue;
934a593f6b8SAndrew Thompson 	struct ifnet *ifp = uether_getifp(ue);
93502ac6454SAndrew Thompson 	struct axe_sframe_hdr hdr;
936ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
9373ac526e0SAndrew Thompson 	int err, pos, len;
938ed6d949aSAndrew Thompson 	int actlen;
939ed6d949aSAndrew Thompson 
940ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
94102ac6454SAndrew Thompson 
94202ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
94302ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
94402ac6454SAndrew Thompson 		pos = 0;
9453ac526e0SAndrew Thompson 		len = 0;
9463ac526e0SAndrew Thompson 		err = 0;
9473ac526e0SAndrew Thompson 
948ed6d949aSAndrew Thompson 		pc = usbd_xfer_get_frame(xfer, 0);
9498c09fbe4SPyun YongHyeon 		if (AXE_IS_178_FAMILY(sc)) {
9503ac526e0SAndrew Thompson 			while (pos < actlen) {
9513ac526e0SAndrew Thompson 				if ((pos + sizeof(hdr)) > actlen) {
95202ac6454SAndrew Thompson 					/* too little data */
9533ac526e0SAndrew Thompson 					err = EINVAL;
95402ac6454SAndrew Thompson 					break;
95502ac6454SAndrew Thompson 				}
956ed6d949aSAndrew Thompson 				usbd_copy_out(pc, pos, &hdr, sizeof(hdr));
95702ac6454SAndrew Thompson 
95802ac6454SAndrew Thompson 				if ((hdr.len ^ hdr.ilen) != 0xFFFF) {
95902ac6454SAndrew Thompson 					/* we lost sync */
9603ac526e0SAndrew Thompson 					err = EINVAL;
96102ac6454SAndrew Thompson 					break;
96202ac6454SAndrew Thompson 				}
96302ac6454SAndrew Thompson 				pos += sizeof(hdr);
96402ac6454SAndrew Thompson 
96502ac6454SAndrew Thompson 				len = le16toh(hdr.len);
9663ac526e0SAndrew Thompson 				if ((pos + len) > actlen) {
96702ac6454SAndrew Thompson 					/* invalid length */
9683ac526e0SAndrew Thompson 					err = EINVAL;
96902ac6454SAndrew Thompson 					break;
97002ac6454SAndrew Thompson 				}
97140221561SPyun YongHyeon 				uether_rxbuf(ue, pc, pos, len);
97202ac6454SAndrew Thompson 
9733ac526e0SAndrew Thompson 				pos += len + (len % 2);
97402ac6454SAndrew Thompson 			}
97540221561SPyun YongHyeon 		} else
97640221561SPyun YongHyeon 			uether_rxbuf(ue, pc, 0, actlen);
97702ac6454SAndrew Thompson 
9783ac526e0SAndrew Thompson 		if (err != 0)
97902ac6454SAndrew Thompson 			ifp->if_ierrors++;
98002ac6454SAndrew Thompson 
98102ac6454SAndrew Thompson 		/* FALLTHROUGH */
98202ac6454SAndrew Thompson 	case USB_ST_SETUP:
98302ac6454SAndrew Thompson tr_setup:
984ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
985a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
986a593f6b8SAndrew Thompson 		uether_rxflush(ue);
98702ac6454SAndrew Thompson 		return;
98802ac6454SAndrew Thompson 
98902ac6454SAndrew Thompson 	default:			/* Error */
990ed6d949aSAndrew Thompson 		DPRINTF("bulk read error, %s\n", usbd_errstr(error));
99102ac6454SAndrew Thompson 
992ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
99302ac6454SAndrew Thompson 			/* try to clear stall first */
994ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
99502ac6454SAndrew Thompson 			goto tr_setup;
99602ac6454SAndrew Thompson 		}
99702ac6454SAndrew Thompson 		return;
99802ac6454SAndrew Thompson 
99902ac6454SAndrew Thompson 	}
100002ac6454SAndrew Thompson }
100102ac6454SAndrew Thompson 
100202ac6454SAndrew Thompson #if ((AXE_BULK_BUF_SIZE >= 0x10000) || (AXE_BULK_BUF_SIZE < (MCLBYTES+4)))
100302ac6454SAndrew Thompson #error "Please update axe_bulk_write_callback()!"
100402ac6454SAndrew Thompson #endif
100502ac6454SAndrew Thompson 
100602ac6454SAndrew Thompson static void
1007ed6d949aSAndrew Thompson axe_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
100802ac6454SAndrew Thompson {
1009ed6d949aSAndrew Thompson 	struct axe_softc *sc = usbd_xfer_softc(xfer);
101002ac6454SAndrew Thompson 	struct axe_sframe_hdr hdr;
1011a593f6b8SAndrew Thompson 	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
1012ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
101302ac6454SAndrew Thompson 	struct mbuf *m;
101465c12bf5SPyun YongHyeon 	int nframes, pos;
101502ac6454SAndrew Thompson 
101602ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
101702ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
101802ac6454SAndrew Thompson 		DPRINTFN(11, "transfer complete\n");
1019465a52e0SPyun YongHyeon 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
102002ac6454SAndrew Thompson 		/* FALLTHROUGH */
102102ac6454SAndrew Thompson 	case USB_ST_SETUP:
102202ac6454SAndrew Thompson tr_setup:
1023465a52e0SPyun YongHyeon 		if ((sc->sc_flags & AXE_FLAG_LINK) == 0 ||
1024465a52e0SPyun YongHyeon 		    (ifp->if_drv_flags & IFF_DRV_OACTIVE) != 0) {
102502ac6454SAndrew Thompson 			/*
1026465a52e0SPyun YongHyeon 			 * Don't send anything if there is no link or
1027465a52e0SPyun YongHyeon 			 * controller is busy.
102802ac6454SAndrew Thompson 			 */
102902ac6454SAndrew Thompson 			return;
103002ac6454SAndrew Thompson 		}
103102ac6454SAndrew Thompson 
103265c12bf5SPyun YongHyeon 		for (nframes = 0; nframes < 16 &&
103365c12bf5SPyun YongHyeon 		    !IFQ_DRV_IS_EMPTY(&ifp->if_snd); nframes++) {
103402ac6454SAndrew Thompson 			IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
103565c12bf5SPyun YongHyeon 			if (m == NULL)
103665c12bf5SPyun YongHyeon 				break;
103765c12bf5SPyun YongHyeon 			usbd_xfer_set_frame_offset(xfer, nframes * MCLBYTES,
103865c12bf5SPyun YongHyeon 			    nframes);
103965c12bf5SPyun YongHyeon 			pos = 0;
104065c12bf5SPyun YongHyeon 			pc = usbd_xfer_get_frame(xfer, nframes);
10418c09fbe4SPyun YongHyeon 			if (AXE_IS_178_FAMILY(sc)) {
104202ac6454SAndrew Thompson 				hdr.len = htole16(m->m_pkthdr.len);
104302ac6454SAndrew Thompson 				hdr.ilen = ~hdr.len;
1044ed6d949aSAndrew Thompson 				usbd_copy_in(pc, pos, &hdr, sizeof(hdr));
104502ac6454SAndrew Thompson 				pos += sizeof(hdr);
1046ed6d949aSAndrew Thompson 				usbd_m_copy_in(pc, pos, m, 0, m->m_pkthdr.len);
104702ac6454SAndrew Thompson 				pos += m->m_pkthdr.len;
104865c12bf5SPyun YongHyeon 				if ((pos % 512) == 0) {
104965c12bf5SPyun YongHyeon 					hdr.len = 0;
105065c12bf5SPyun YongHyeon 					hdr.ilen = 0xffff;
105165c12bf5SPyun YongHyeon 					usbd_copy_in(pc, pos, &hdr,
105265c12bf5SPyun YongHyeon 					    sizeof(hdr));
105365c12bf5SPyun YongHyeon 					pos += sizeof(hdr);
105465c12bf5SPyun YongHyeon 				}
105565c12bf5SPyun YongHyeon 			} else {
105665c12bf5SPyun YongHyeon 				usbd_m_copy_in(pc, pos, m, 0, m->m_pkthdr.len);
105765c12bf5SPyun YongHyeon 				pos += m->m_pkthdr.len;
105865c12bf5SPyun YongHyeon 			}
105902ac6454SAndrew Thompson 
106002ac6454SAndrew Thompson 			/*
1061b8e98004SPyun YongHyeon 			 * XXX
1062b8e98004SPyun YongHyeon 			 * Update TX packet counter here. This is not
1063b8e98004SPyun YongHyeon 			 * correct way but it seems that there is no way
1064b8e98004SPyun YongHyeon 			 * to know how many packets are sent at the end
1065b8e98004SPyun YongHyeon 			 * of transfer because controller combines
1066b8e98004SPyun YongHyeon 			 * multiple writes into single one if there is
1067b8e98004SPyun YongHyeon 			 * room in TX buffer of controller.
1068b8e98004SPyun YongHyeon 			 */
1069b8e98004SPyun YongHyeon 			ifp->if_opackets++;
1070b8e98004SPyun YongHyeon 
1071b8e98004SPyun YongHyeon 			/*
107202ac6454SAndrew Thompson 			 * if there's a BPF listener, bounce a copy
107302ac6454SAndrew Thompson 			 * of this frame to him:
107402ac6454SAndrew Thompson 			 */
107502ac6454SAndrew Thompson 			BPF_MTAP(ifp, m);
107602ac6454SAndrew Thompson 
107702ac6454SAndrew Thompson 			m_freem(m);
107802ac6454SAndrew Thompson 
107965c12bf5SPyun YongHyeon 			/* Set frame length. */
108065c12bf5SPyun YongHyeon 			usbd_xfer_set_frame_len(xfer, nframes, pos);
108102ac6454SAndrew Thompson 		}
108265c12bf5SPyun YongHyeon 		if (nframes != 0) {
108365c12bf5SPyun YongHyeon 			usbd_xfer_set_frames(xfer, nframes);
1084a593f6b8SAndrew Thompson 			usbd_transfer_submit(xfer);
1085465a52e0SPyun YongHyeon 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
108665c12bf5SPyun YongHyeon 		}
108702ac6454SAndrew Thompson 		return;
108865c12bf5SPyun YongHyeon 		/* NOTREACHED */
108902ac6454SAndrew Thompson 	default:			/* Error */
109002ac6454SAndrew Thompson 		DPRINTFN(11, "transfer error, %s\n",
1091ed6d949aSAndrew Thompson 		    usbd_errstr(error));
109202ac6454SAndrew Thompson 
109302ac6454SAndrew Thompson 		ifp->if_oerrors++;
1094465a52e0SPyun YongHyeon 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
109502ac6454SAndrew Thompson 
1096ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
109702ac6454SAndrew Thompson 			/* try to clear stall first */
1098ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
109902ac6454SAndrew Thompson 			goto tr_setup;
110002ac6454SAndrew Thompson 		}
110102ac6454SAndrew Thompson 		return;
110202ac6454SAndrew Thompson 
110302ac6454SAndrew Thompson 	}
110402ac6454SAndrew Thompson }
110502ac6454SAndrew Thompson 
110602ac6454SAndrew Thompson static void
1107760bc48eSAndrew Thompson axe_tick(struct usb_ether *ue)
110802ac6454SAndrew Thompson {
1109a593f6b8SAndrew Thompson 	struct axe_softc *sc = uether_getsc(ue);
111002ac6454SAndrew Thompson 	struct mii_data *mii = GET_MII(sc);
111102ac6454SAndrew Thompson 
111202ac6454SAndrew Thompson 	AXE_LOCK_ASSERT(sc, MA_OWNED);
111302ac6454SAndrew Thompson 
111402ac6454SAndrew Thompson 	mii_tick(mii);
111502ac6454SAndrew Thompson 	if ((sc->sc_flags & AXE_FLAG_LINK) == 0) {
111602ac6454SAndrew Thompson 		axe_miibus_statchg(ue->ue_dev);
111702ac6454SAndrew Thompson 		if ((sc->sc_flags & AXE_FLAG_LINK) != 0)
111802ac6454SAndrew Thompson 			axe_start(ue);
111902ac6454SAndrew Thompson 	}
112002ac6454SAndrew Thompson }
112102ac6454SAndrew Thompson 
112202ac6454SAndrew Thompson static void
1123760bc48eSAndrew Thompson axe_start(struct usb_ether *ue)
112402ac6454SAndrew Thompson {
1125a593f6b8SAndrew Thompson 	struct axe_softc *sc = uether_getsc(ue);
112602ac6454SAndrew Thompson 
112702ac6454SAndrew Thompson 	/*
112802ac6454SAndrew Thompson 	 * start the USB transfers, if not already started:
112902ac6454SAndrew Thompson 	 */
1130a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[AXE_BULK_DT_RD]);
1131a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[AXE_BULK_DT_WR]);
113202ac6454SAndrew Thompson }
113302ac6454SAndrew Thompson 
113402ac6454SAndrew Thompson static void
1135760bc48eSAndrew Thompson axe_init(struct usb_ether *ue)
113602ac6454SAndrew Thompson {
1137a593f6b8SAndrew Thompson 	struct axe_softc *sc = uether_getsc(ue);
1138a593f6b8SAndrew Thompson 	struct ifnet *ifp = uether_getifp(ue);
113902ac6454SAndrew Thompson 	uint16_t rxmode;
114002ac6454SAndrew Thompson 
114102ac6454SAndrew Thompson 	AXE_LOCK_ASSERT(sc, MA_OWNED);
114202ac6454SAndrew Thompson 
11430369b604SPyun YongHyeon 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
11440369b604SPyun YongHyeon 		return;
11450369b604SPyun YongHyeon 
114602ac6454SAndrew Thompson 	/* Cancel pending I/O */
114702ac6454SAndrew Thompson 	axe_stop(ue);
114802ac6454SAndrew Thompson 
1149675c1ae8SPyun YongHyeon 	axe_reset(sc);
1150675c1ae8SPyun YongHyeon 
115187832c5eSAndrew Thompson 	/* Set MAC address. */
11528c09fbe4SPyun YongHyeon 	if (AXE_IS_178_FAMILY(sc))
115387832c5eSAndrew Thompson 		axe_cmd(sc, AXE_178_CMD_WRITE_NODEID, 0, 0, IF_LLADDR(ifp));
115487832c5eSAndrew Thompson 	else
115587832c5eSAndrew Thompson 		axe_cmd(sc, AXE_172_CMD_WRITE_NODEID, 0, 0, IF_LLADDR(ifp));
115602ac6454SAndrew Thompson 
115702ac6454SAndrew Thompson 	/* Set transmitter IPG values */
11588c09fbe4SPyun YongHyeon 	if (AXE_IS_178_FAMILY(sc))
115902ac6454SAndrew Thompson 		axe_cmd(sc, AXE_178_CMD_WRITE_IPG012, sc->sc_ipgs[2],
116002ac6454SAndrew Thompson 		    (sc->sc_ipgs[1] << 8) | (sc->sc_ipgs[0]), NULL);
11618c09fbe4SPyun YongHyeon 	else {
116202ac6454SAndrew Thompson 		axe_cmd(sc, AXE_172_CMD_WRITE_IPG0, 0, sc->sc_ipgs[0], NULL);
116302ac6454SAndrew Thompson 		axe_cmd(sc, AXE_172_CMD_WRITE_IPG1, 0, sc->sc_ipgs[1], NULL);
116402ac6454SAndrew Thompson 		axe_cmd(sc, AXE_172_CMD_WRITE_IPG2, 0, sc->sc_ipgs[2], NULL);
116502ac6454SAndrew Thompson 	}
116602ac6454SAndrew Thompson 
1167*14ae1fa4SPyun YongHyeon 	/* AX88772B uses different maximum frame burst configuration. */
1168*14ae1fa4SPyun YongHyeon 	if (sc->sc_flags & AXE_FLAG_772B)
1169*14ae1fa4SPyun YongHyeon 		axe_cmd(sc, AXE_772B_CMD_RXCTL_WRITE_CFG,
1170*14ae1fa4SPyun YongHyeon 		    ax88772b_mfb_table[AX88772B_MFB_16K].threshold,
1171*14ae1fa4SPyun YongHyeon 		    ax88772b_mfb_table[AX88772B_MFB_16K].byte_cnt, NULL);
1172*14ae1fa4SPyun YongHyeon 
1173*14ae1fa4SPyun YongHyeon 	/* Enable receiver, set RX mode. */
117402ac6454SAndrew Thompson 	rxmode = (AXE_RXCMD_MULTICAST | AXE_RXCMD_ENABLE);
11758c09fbe4SPyun YongHyeon 	if (AXE_IS_178_FAMILY(sc)) {
1176*14ae1fa4SPyun YongHyeon 		if (sc->sc_flags & AXE_FLAG_772B) {
1177*14ae1fa4SPyun YongHyeon 			/*
1178*14ae1fa4SPyun YongHyeon 			 * Select RX header format type 1.  Aligning IP
1179*14ae1fa4SPyun YongHyeon 			 * header on 4 byte boundary is not needed
1180*14ae1fa4SPyun YongHyeon 			 * because we always copy the received frame in
1181*14ae1fa4SPyun YongHyeon 			 * RX handler.
1182*14ae1fa4SPyun YongHyeon 			 */
1183*14ae1fa4SPyun YongHyeon 			rxmode |= AXE_772B_RXCMD_HDR_TYPE_1;
1184*14ae1fa4SPyun YongHyeon 		} else {
11853ac526e0SAndrew Thompson 			/*
11863ac526e0SAndrew Thompson 			 * Default Rx buffer size is too small to get
11873ac526e0SAndrew Thompson 			 * maximum performance.
11883ac526e0SAndrew Thompson 			 */
11893ac526e0SAndrew Thompson 			rxmode |= AXE_178_RXCMD_MFB_16384;
1190*14ae1fa4SPyun YongHyeon 		}
119102ac6454SAndrew Thompson 	} else {
119202ac6454SAndrew Thompson 		rxmode |= AXE_172_RXCMD_UNICAST;
119302ac6454SAndrew Thompson 	}
119402ac6454SAndrew Thompson 
119502ac6454SAndrew Thompson 	/* If we want promiscuous mode, set the allframes bit. */
119602ac6454SAndrew Thompson 	if (ifp->if_flags & IFF_PROMISC)
119702ac6454SAndrew Thompson 		rxmode |= AXE_RXCMD_PROMISC;
119802ac6454SAndrew Thompson 
119902ac6454SAndrew Thompson 	if (ifp->if_flags & IFF_BROADCAST)
120002ac6454SAndrew Thompson 		rxmode |= AXE_RXCMD_BROADCAST;
120102ac6454SAndrew Thompson 
120202ac6454SAndrew Thompson 	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
120302ac6454SAndrew Thompson 
120402ac6454SAndrew Thompson 	/* Load the multicast filter. */
120502ac6454SAndrew Thompson 	axe_setmulti(ue);
120602ac6454SAndrew Thompson 
1207ed6d949aSAndrew Thompson 	usbd_xfer_set_stall(sc->sc_xfer[AXE_BULK_DT_WR]);
120802ac6454SAndrew Thompson 
120902ac6454SAndrew Thompson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
1210d743bdd2SPyun YongHyeon 	/* Switch to selected media. */
1211d743bdd2SPyun YongHyeon 	axe_ifmedia_upd(ifp);
121202ac6454SAndrew Thompson 	axe_start(ue);
121302ac6454SAndrew Thompson }
121402ac6454SAndrew Thompson 
121502ac6454SAndrew Thompson static void
1216760bc48eSAndrew Thompson axe_setpromisc(struct usb_ether *ue)
121702ac6454SAndrew Thompson {
1218a593f6b8SAndrew Thompson 	struct axe_softc *sc = uether_getsc(ue);
1219a593f6b8SAndrew Thompson 	struct ifnet *ifp = uether_getifp(ue);
122002ac6454SAndrew Thompson 	uint16_t rxmode;
122102ac6454SAndrew Thompson 
122202ac6454SAndrew Thompson 	axe_cmd(sc, AXE_CMD_RXCTL_READ, 0, 0, &rxmode);
122302ac6454SAndrew Thompson 
122402ac6454SAndrew Thompson 	rxmode = le16toh(rxmode);
122502ac6454SAndrew Thompson 
122602ac6454SAndrew Thompson 	if (ifp->if_flags & IFF_PROMISC) {
122702ac6454SAndrew Thompson 		rxmode |= AXE_RXCMD_PROMISC;
122802ac6454SAndrew Thompson 	} else {
122902ac6454SAndrew Thompson 		rxmode &= ~AXE_RXCMD_PROMISC;
123002ac6454SAndrew Thompson 	}
123102ac6454SAndrew Thompson 
123202ac6454SAndrew Thompson 	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
123302ac6454SAndrew Thompson 
123402ac6454SAndrew Thompson 	axe_setmulti(ue);
123502ac6454SAndrew Thompson }
123602ac6454SAndrew Thompson 
123702ac6454SAndrew Thompson static void
1238760bc48eSAndrew Thompson axe_stop(struct usb_ether *ue)
123902ac6454SAndrew Thompson {
1240a593f6b8SAndrew Thompson 	struct axe_softc *sc = uether_getsc(ue);
1241a593f6b8SAndrew Thompson 	struct ifnet *ifp = uether_getifp(ue);
124202ac6454SAndrew Thompson 
124302ac6454SAndrew Thompson 	AXE_LOCK_ASSERT(sc, MA_OWNED);
124402ac6454SAndrew Thompson 
1245465a52e0SPyun YongHyeon 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
124602ac6454SAndrew Thompson 	sc->sc_flags &= ~AXE_FLAG_LINK;
124702ac6454SAndrew Thompson 
124802ac6454SAndrew Thompson 	/*
124902ac6454SAndrew Thompson 	 * stop all the transfers, if not already stopped:
125002ac6454SAndrew Thompson 	 */
1251a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[AXE_BULK_DT_WR]);
1252a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[AXE_BULK_DT_RD]);
125302ac6454SAndrew Thompson }
1254