xref: /freebsd/sys/dev/usb/net/if_axe.c (revision f29a072444d6e31d57f7edd94f952395ae7bb22e)
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 
7902ac6454SAndrew Thompson #include "usbdevs.h"
8002ac6454SAndrew Thompson #include <dev/usb/usb.h>
8102ac6454SAndrew Thompson #include <dev/usb/usb_mfunc.h>
8202ac6454SAndrew Thompson #include <dev/usb/usb_error.h>
8302ac6454SAndrew Thompson 
8402ac6454SAndrew Thompson #define	USB_DEBUG_VAR axe_debug
8502ac6454SAndrew Thompson 
8602ac6454SAndrew Thompson #include <dev/usb/usb_core.h>
8702ac6454SAndrew Thompson #include <dev/usb/usb_lookup.h>
8802ac6454SAndrew Thompson #include <dev/usb/usb_process.h>
8902ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
9002ac6454SAndrew Thompson #include <dev/usb/usb_request.h>
9102ac6454SAndrew Thompson #include <dev/usb/usb_busdma.h>
9202ac6454SAndrew Thompson #include <dev/usb/usb_util.h>
9302ac6454SAndrew Thompson 
9402ac6454SAndrew Thompson #include <dev/usb/net/usb_ethernet.h>
9502ac6454SAndrew Thompson #include <dev/usb/net/if_axereg.h>
9602ac6454SAndrew Thompson 
9702ac6454SAndrew Thompson /*
9802ac6454SAndrew Thompson  * AXE_178_MAX_FRAME_BURST
9902ac6454SAndrew Thompson  * max frame burst size for Ax88178 and Ax88772
10002ac6454SAndrew Thompson  *	0	2048 bytes
10102ac6454SAndrew Thompson  *	1	4096 bytes
10202ac6454SAndrew Thompson  *	2	8192 bytes
10302ac6454SAndrew Thompson  *	3	16384 bytes
10402ac6454SAndrew Thompson  * use the largest your system can handle without USB stalling.
10502ac6454SAndrew Thompson  *
10602ac6454SAndrew Thompson  * NB: 88772 parts appear to generate lots of input errors with
10702ac6454SAndrew Thompson  * a 2K rx buffer and 8K is only slightly faster than 4K on an
10802ac6454SAndrew Thompson  * EHCI port on a T42 so change at your own risk.
10902ac6454SAndrew Thompson  */
11002ac6454SAndrew Thompson #define AXE_178_MAX_FRAME_BURST	1
11102ac6454SAndrew Thompson 
11202ac6454SAndrew Thompson #if USB_DEBUG
11302ac6454SAndrew Thompson static int axe_debug = 0;
11402ac6454SAndrew Thompson 
11502ac6454SAndrew Thompson SYSCTL_NODE(_hw_usb2, OID_AUTO, axe, CTLFLAG_RW, 0, "USB axe");
11602ac6454SAndrew Thompson SYSCTL_INT(_hw_usb2_axe, OID_AUTO, debug, CTLFLAG_RW, &axe_debug, 0,
11702ac6454SAndrew Thompson     "Debug level");
11802ac6454SAndrew Thompson #endif
11902ac6454SAndrew Thompson 
12002ac6454SAndrew Thompson /*
12102ac6454SAndrew Thompson  * Various supported device vendors/products.
12202ac6454SAndrew Thompson  */
12302ac6454SAndrew Thompson static const struct usb2_device_id axe_devs[] = {
12402ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_UF200, 0)},
12502ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_ACERCM, USB_PRODUCT_ACERCM_EP1427X2, 0)},
12602ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_APPLE, USB_PRODUCT_APPLE_ETHERNET, AXE_FLAG_772)},
12702ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_ASIX, USB_PRODUCT_ASIX_AX88172, 0)},
12802ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_ASIX, USB_PRODUCT_ASIX_AX88178, AXE_FLAG_178)},
12902ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_ASIX, USB_PRODUCT_ASIX_AX88772, AXE_FLAG_772)},
13002ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC210T, 0)},
13102ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5D5055, AXE_FLAG_178)},
13202ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USB2AR, 0)},
13302ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_CISCOLINKSYS, USB_PRODUCT_CISCOLINKSYS_USB200MV2, AXE_FLAG_772)},
13402ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_COREGA, USB_PRODUCT_COREGA_FETHER_USB2_TX, 0)},
13502ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DUBE100, 0)},
13602ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DUBE100B1, AXE_FLAG_772)},
13702ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_GOODWAY, USB_PRODUCT_GOODWAY_GWUSB2E, 0)},
13802ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_IODATA, USB_PRODUCT_IODATA_ETGUS2, AXE_FLAG_178)},
13902ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_JVC, USB_PRODUCT_JVC_MP_PRX1, 0)},
14002ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_LINKSYS2, USB_PRODUCT_LINKSYS2_USB200M, 0)},
14102ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_LINKSYS4, USB_PRODUCT_LINKSYS4_USB1000, AXE_FLAG_178)},
14202ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUAU2KTX, 0)},
14302ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_NETGEAR, USB_PRODUCT_NETGEAR_FA120, 0)},
14402ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_OQO, USB_PRODUCT_OQO_ETHER01PLUS, AXE_FLAG_772)},
14502ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_PLANEX3, USB_PRODUCT_PLANEX3_GU1000T, AXE_FLAG_178)},
14602ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_SITECOM, USB_PRODUCT_SITECOM_LN029, 0)},
14702ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_SITECOMEU, USB_PRODUCT_SITECOMEU_LN028, AXE_FLAG_178)},
14802ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_SYSTEMTALKS, USB_PRODUCT_SYSTEMTALKS_SGCX2UL, 0)},
14902ac6454SAndrew Thompson };
15002ac6454SAndrew Thompson 
15102ac6454SAndrew Thompson static device_probe_t axe_probe;
15202ac6454SAndrew Thompson static device_attach_t axe_attach;
15302ac6454SAndrew Thompson static device_detach_t axe_detach;
15402ac6454SAndrew Thompson 
15502ac6454SAndrew Thompson static usb2_callback_t axe_intr_callback;
15602ac6454SAndrew Thompson static usb2_callback_t axe_bulk_read_callback;
15702ac6454SAndrew Thompson static usb2_callback_t axe_bulk_write_callback;
15802ac6454SAndrew Thompson 
15902ac6454SAndrew Thompson static miibus_readreg_t axe_miibus_readreg;
16002ac6454SAndrew Thompson static miibus_writereg_t axe_miibus_writereg;
16102ac6454SAndrew Thompson static miibus_statchg_t axe_miibus_statchg;
16202ac6454SAndrew Thompson 
16302ac6454SAndrew Thompson static usb2_ether_fn_t axe_attach_post;
16402ac6454SAndrew Thompson static usb2_ether_fn_t axe_init;
16502ac6454SAndrew Thompson static usb2_ether_fn_t axe_stop;
16602ac6454SAndrew Thompson static usb2_ether_fn_t axe_start;
16702ac6454SAndrew Thompson static usb2_ether_fn_t axe_tick;
16802ac6454SAndrew Thompson static usb2_ether_fn_t axe_setmulti;
16902ac6454SAndrew Thompson static usb2_ether_fn_t axe_setpromisc;
17002ac6454SAndrew Thompson 
17102ac6454SAndrew Thompson static int	axe_ifmedia_upd(struct ifnet *);
17202ac6454SAndrew Thompson static void	axe_ifmedia_sts(struct ifnet *, struct ifmediareq *);
17302ac6454SAndrew Thompson static int	axe_cmd(struct axe_softc *, int, int, int, void *);
17402ac6454SAndrew Thompson static void	axe_ax88178_init(struct axe_softc *);
17502ac6454SAndrew Thompson static void	axe_ax88772_init(struct axe_softc *);
17602ac6454SAndrew Thompson static int	axe_get_phyno(struct axe_softc *, int);
17702ac6454SAndrew Thompson 
17802ac6454SAndrew Thompson static const struct usb2_config axe_config[AXE_N_TRANSFER] = {
17902ac6454SAndrew Thompson 
18002ac6454SAndrew Thompson 	[AXE_BULK_DT_WR] = {
18102ac6454SAndrew Thompson 		.type = UE_BULK,
18202ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
18302ac6454SAndrew Thompson 		.direction = UE_DIR_OUT,
1844eae601eSAndrew Thompson 		.bufsize = AXE_BULK_BUF_SIZE,
1854eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
1864eae601eSAndrew Thompson 		.callback = axe_bulk_write_callback,
1874eae601eSAndrew Thompson 		.timeout = 10000,	/* 10 seconds */
18802ac6454SAndrew Thompson 	},
18902ac6454SAndrew Thompson 
19002ac6454SAndrew Thompson 	[AXE_BULK_DT_RD] = {
19102ac6454SAndrew Thompson 		.type = UE_BULK,
19202ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
19302ac6454SAndrew Thompson 		.direction = UE_DIR_IN,
19402ac6454SAndrew Thompson #if (MCLBYTES < 2048)
19502ac6454SAndrew Thompson #error "(MCLBYTES < 2048)"
19602ac6454SAndrew Thompson #endif
1974eae601eSAndrew Thompson 		.bufsize = MCLBYTES,
1984eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
1994eae601eSAndrew Thompson 		.callback = axe_bulk_read_callback,
2004eae601eSAndrew Thompson 		.timeout = 0,	/* no timeout */
20102ac6454SAndrew Thompson 	},
20202ac6454SAndrew Thompson 
20302ac6454SAndrew Thompson 	[AXE_INTR_DT_RD] = {
20402ac6454SAndrew Thompson 		.type = UE_INTERRUPT,
20502ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
20602ac6454SAndrew Thompson 		.direction = UE_DIR_IN,
2074eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
2084eae601eSAndrew Thompson 		.bufsize = 0,	/* use wMaxPacketSize */
2094eae601eSAndrew Thompson 		.callback = axe_intr_callback,
21002ac6454SAndrew Thompson 	},
21102ac6454SAndrew Thompson };
21202ac6454SAndrew Thompson 
21302ac6454SAndrew Thompson static device_method_t axe_methods[] = {
21402ac6454SAndrew Thompson 	/* Device interface */
21502ac6454SAndrew Thompson 	DEVMETHOD(device_probe, axe_probe),
21602ac6454SAndrew Thompson 	DEVMETHOD(device_attach, axe_attach),
21702ac6454SAndrew Thompson 	DEVMETHOD(device_detach, axe_detach),
21802ac6454SAndrew Thompson 
21902ac6454SAndrew Thompson 	/* bus interface */
22002ac6454SAndrew Thompson 	DEVMETHOD(bus_print_child, bus_generic_print_child),
22102ac6454SAndrew Thompson 	DEVMETHOD(bus_driver_added, bus_generic_driver_added),
22202ac6454SAndrew Thompson 
22302ac6454SAndrew Thompson 	/* MII interface */
22402ac6454SAndrew Thompson 	DEVMETHOD(miibus_readreg, axe_miibus_readreg),
22502ac6454SAndrew Thompson 	DEVMETHOD(miibus_writereg, axe_miibus_writereg),
22602ac6454SAndrew Thompson 	DEVMETHOD(miibus_statchg, axe_miibus_statchg),
22702ac6454SAndrew Thompson 
22802ac6454SAndrew Thompson 	{0, 0}
22902ac6454SAndrew Thompson };
23002ac6454SAndrew Thompson 
23102ac6454SAndrew Thompson static driver_t axe_driver = {
23202ac6454SAndrew Thompson 	.name = "axe",
23302ac6454SAndrew Thompson 	.methods = axe_methods,
23402ac6454SAndrew Thompson 	.size = sizeof(struct axe_softc),
23502ac6454SAndrew Thompson };
23602ac6454SAndrew Thompson 
23702ac6454SAndrew Thompson static devclass_t axe_devclass;
23802ac6454SAndrew Thompson 
2399aef556dSAndrew Thompson DRIVER_MODULE(axe, uhub, axe_driver, axe_devclass, NULL, 0);
24002ac6454SAndrew Thompson DRIVER_MODULE(miibus, axe, miibus_driver, miibus_devclass, 0, 0);
24102ac6454SAndrew Thompson MODULE_DEPEND(axe, uether, 1, 1, 1);
24202ac6454SAndrew Thompson MODULE_DEPEND(axe, usb, 1, 1, 1);
24302ac6454SAndrew Thompson MODULE_DEPEND(axe, ether, 1, 1, 1);
24402ac6454SAndrew Thompson MODULE_DEPEND(axe, miibus, 1, 1, 1);
24502ac6454SAndrew Thompson 
24602ac6454SAndrew Thompson static const struct usb2_ether_methods axe_ue_methods = {
24702ac6454SAndrew Thompson 	.ue_attach_post = axe_attach_post,
24802ac6454SAndrew Thompson 	.ue_start = axe_start,
24902ac6454SAndrew Thompson 	.ue_init = axe_init,
25002ac6454SAndrew Thompson 	.ue_stop = axe_stop,
25102ac6454SAndrew Thompson 	.ue_tick = axe_tick,
25202ac6454SAndrew Thompson 	.ue_setmulti = axe_setmulti,
25302ac6454SAndrew Thompson 	.ue_setpromisc = axe_setpromisc,
25402ac6454SAndrew Thompson 	.ue_mii_upd = axe_ifmedia_upd,
25502ac6454SAndrew Thompson 	.ue_mii_sts = axe_ifmedia_sts,
25602ac6454SAndrew Thompson };
25702ac6454SAndrew Thompson 
25802ac6454SAndrew Thompson static int
25902ac6454SAndrew Thompson axe_cmd(struct axe_softc *sc, int cmd, int index, int val, void *buf)
26002ac6454SAndrew Thompson {
26102ac6454SAndrew Thompson 	struct usb2_device_request req;
26202ac6454SAndrew Thompson 	usb2_error_t err;
26302ac6454SAndrew Thompson 
26402ac6454SAndrew Thompson 	AXE_LOCK_ASSERT(sc, MA_OWNED);
26502ac6454SAndrew Thompson 
26602ac6454SAndrew Thompson 	req.bmRequestType = (AXE_CMD_IS_WRITE(cmd) ?
26702ac6454SAndrew Thompson 	    UT_WRITE_VENDOR_DEVICE :
26802ac6454SAndrew Thompson 	    UT_READ_VENDOR_DEVICE);
26902ac6454SAndrew Thompson 	req.bRequest = AXE_CMD_CMD(cmd);
27002ac6454SAndrew Thompson 	USETW(req.wValue, val);
27102ac6454SAndrew Thompson 	USETW(req.wIndex, index);
27202ac6454SAndrew Thompson 	USETW(req.wLength, AXE_CMD_LEN(cmd));
27302ac6454SAndrew Thompson 
27402ac6454SAndrew Thompson 	err = usb2_ether_do_request(&sc->sc_ue, &req, buf, 1000);
27502ac6454SAndrew Thompson 
27602ac6454SAndrew Thompson 	return (err);
27702ac6454SAndrew Thompson }
27802ac6454SAndrew Thompson 
27902ac6454SAndrew Thompson static int
28002ac6454SAndrew Thompson axe_miibus_readreg(device_t dev, int phy, int reg)
28102ac6454SAndrew Thompson {
28202ac6454SAndrew Thompson 	struct axe_softc *sc = device_get_softc(dev);
28302ac6454SAndrew Thompson 	uint16_t val;
28402ac6454SAndrew Thompson 	int locked;
28502ac6454SAndrew Thompson 
28602ac6454SAndrew Thompson 	if (sc->sc_phyno != phy)
28702ac6454SAndrew Thompson 		return (0);
28802ac6454SAndrew Thompson 
28902ac6454SAndrew Thompson 	locked = mtx_owned(&sc->sc_mtx);
29002ac6454SAndrew Thompson 	if (!locked)
29102ac6454SAndrew Thompson 		AXE_LOCK(sc);
29202ac6454SAndrew Thompson 
29302ac6454SAndrew Thompson 	axe_cmd(sc, AXE_CMD_MII_OPMODE_SW, 0, 0, NULL);
29402ac6454SAndrew Thompson 	axe_cmd(sc, AXE_CMD_MII_READ_REG, reg, phy, &val);
29502ac6454SAndrew Thompson 	axe_cmd(sc, AXE_CMD_MII_OPMODE_HW, 0, 0, NULL);
29602ac6454SAndrew Thompson 
29702ac6454SAndrew Thompson 	val = le16toh(val);
29802ac6454SAndrew Thompson 	if ((sc->sc_flags & AXE_FLAG_772) != 0 && reg == MII_BMSR) {
29902ac6454SAndrew Thompson 		/*
30002ac6454SAndrew Thompson 		 * BMSR of AX88772 indicates that it supports extended
30102ac6454SAndrew Thompson 		 * capability but the extended status register is
30202ac6454SAndrew Thompson 		 * revered for embedded ethernet PHY. So clear the
30302ac6454SAndrew Thompson 		 * extended capability bit of BMSR.
30402ac6454SAndrew Thompson 		 */
30502ac6454SAndrew Thompson 		val &= ~BMSR_EXTCAP;
30602ac6454SAndrew Thompson 	}
30702ac6454SAndrew Thompson 
30802ac6454SAndrew Thompson 	if (!locked)
30902ac6454SAndrew Thompson 		AXE_UNLOCK(sc);
31002ac6454SAndrew Thompson 	return (val);
31102ac6454SAndrew Thompson }
31202ac6454SAndrew Thompson 
31302ac6454SAndrew Thompson static int
31402ac6454SAndrew Thompson axe_miibus_writereg(device_t dev, int phy, int reg, int val)
31502ac6454SAndrew Thompson {
31602ac6454SAndrew Thompson 	struct axe_softc *sc = device_get_softc(dev);
31702ac6454SAndrew Thompson 	int locked;
31802ac6454SAndrew Thompson 
31936002e92SAndrew Thompson 	val = htole32(val);
32002ac6454SAndrew Thompson 
32102ac6454SAndrew Thompson 	if (sc->sc_phyno != phy)
32202ac6454SAndrew Thompson 		return (0);
32302ac6454SAndrew Thompson 
32402ac6454SAndrew Thompson 	locked = mtx_owned(&sc->sc_mtx);
32502ac6454SAndrew Thompson 	if (!locked)
32602ac6454SAndrew Thompson 		AXE_LOCK(sc);
32702ac6454SAndrew Thompson 
32802ac6454SAndrew Thompson 	axe_cmd(sc, AXE_CMD_MII_OPMODE_SW, 0, 0, NULL);
32902ac6454SAndrew Thompson 	axe_cmd(sc, AXE_CMD_MII_WRITE_REG, reg, phy, &val);
33002ac6454SAndrew Thompson 	axe_cmd(sc, AXE_CMD_MII_OPMODE_HW, 0, 0, NULL);
33102ac6454SAndrew Thompson 
33202ac6454SAndrew Thompson 	if (!locked)
33302ac6454SAndrew Thompson 		AXE_UNLOCK(sc);
33402ac6454SAndrew Thompson 	return (0);
33502ac6454SAndrew Thompson }
33602ac6454SAndrew Thompson 
33702ac6454SAndrew Thompson static void
33802ac6454SAndrew Thompson axe_miibus_statchg(device_t dev)
33902ac6454SAndrew Thompson {
34002ac6454SAndrew Thompson 	struct axe_softc *sc = device_get_softc(dev);
34102ac6454SAndrew Thompson 	struct mii_data *mii = GET_MII(sc);
34202ac6454SAndrew Thompson 	struct ifnet *ifp;
34302ac6454SAndrew Thompson 	uint16_t val;
34402ac6454SAndrew Thompson 	int err, locked;
34502ac6454SAndrew Thompson 
34602ac6454SAndrew Thompson 	locked = mtx_owned(&sc->sc_mtx);
34702ac6454SAndrew Thompson 	if (!locked)
34802ac6454SAndrew Thompson 		AXE_LOCK(sc);
34902ac6454SAndrew Thompson 
35002ac6454SAndrew Thompson 	ifp = usb2_ether_getifp(&sc->sc_ue);
35102ac6454SAndrew Thompson 	if (mii == NULL || ifp == NULL ||
35202ac6454SAndrew Thompson 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
35302ac6454SAndrew Thompson 		goto done;
35402ac6454SAndrew Thompson 
35502ac6454SAndrew Thompson 	sc->sc_flags &= ~AXE_FLAG_LINK;
35602ac6454SAndrew Thompson 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
35702ac6454SAndrew Thompson 	    (IFM_ACTIVE | IFM_AVALID)) {
35802ac6454SAndrew Thompson 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
35902ac6454SAndrew Thompson 		case IFM_10_T:
36002ac6454SAndrew Thompson 		case IFM_100_TX:
36102ac6454SAndrew Thompson 			sc->sc_flags |= AXE_FLAG_LINK;
36202ac6454SAndrew Thompson 			break;
36302ac6454SAndrew Thompson 		case IFM_1000_T:
36402ac6454SAndrew Thompson 			if ((sc->sc_flags & AXE_FLAG_178) == 0)
36502ac6454SAndrew Thompson 				break;
36602ac6454SAndrew Thompson 			sc->sc_flags |= AXE_FLAG_LINK;
36702ac6454SAndrew Thompson 			break;
36802ac6454SAndrew Thompson 		default:
36902ac6454SAndrew Thompson 			break;
37002ac6454SAndrew Thompson 		}
37102ac6454SAndrew Thompson 	}
37202ac6454SAndrew Thompson 
37302ac6454SAndrew Thompson 	/* Lost link, do nothing. */
37402ac6454SAndrew Thompson 	if ((sc->sc_flags & AXE_FLAG_LINK) == 0)
37502ac6454SAndrew Thompson 		goto done;
37602ac6454SAndrew Thompson 
37702ac6454SAndrew Thompson 	val = 0;
37802ac6454SAndrew Thompson 	if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0)
37902ac6454SAndrew Thompson 		val |= AXE_MEDIA_FULL_DUPLEX;
38002ac6454SAndrew Thompson 	if (sc->sc_flags & (AXE_FLAG_178 | AXE_FLAG_772)) {
38102ac6454SAndrew Thompson 		val |= AXE_178_MEDIA_RX_EN | AXE_178_MEDIA_MAGIC;
38202ac6454SAndrew Thompson 		if ((sc->sc_flags & AXE_FLAG_178) != 0)
38302ac6454SAndrew Thompson 			val |= AXE_178_MEDIA_ENCK;
38402ac6454SAndrew Thompson 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
38502ac6454SAndrew Thompson 		case IFM_1000_T:
38602ac6454SAndrew Thompson 			val |= AXE_178_MEDIA_GMII | AXE_178_MEDIA_ENCK;
38702ac6454SAndrew Thompson 			break;
38802ac6454SAndrew Thompson 		case IFM_100_TX:
38902ac6454SAndrew Thompson 			val |= AXE_178_MEDIA_100TX;
39002ac6454SAndrew Thompson 			break;
39102ac6454SAndrew Thompson 		case IFM_10_T:
39202ac6454SAndrew Thompson 			/* doesn't need to be handled */
39302ac6454SAndrew Thompson 			break;
39402ac6454SAndrew Thompson 		}
39502ac6454SAndrew Thompson 	}
39602ac6454SAndrew Thompson 	err = axe_cmd(sc, AXE_CMD_WRITE_MEDIA, 0, val, NULL);
39702ac6454SAndrew Thompson 	if (err)
39802ac6454SAndrew Thompson 		device_printf(dev, "media change failed, error %d\n", err);
39902ac6454SAndrew Thompson done:
40002ac6454SAndrew Thompson 	if (!locked)
40102ac6454SAndrew Thompson 		AXE_UNLOCK(sc);
40202ac6454SAndrew Thompson }
40302ac6454SAndrew Thompson 
40402ac6454SAndrew Thompson /*
40502ac6454SAndrew Thompson  * Set media options.
40602ac6454SAndrew Thompson  */
40702ac6454SAndrew Thompson static int
40802ac6454SAndrew Thompson axe_ifmedia_upd(struct ifnet *ifp)
40902ac6454SAndrew Thompson {
41002ac6454SAndrew Thompson 	struct axe_softc *sc = ifp->if_softc;
41102ac6454SAndrew Thompson 	struct mii_data *mii = GET_MII(sc);
41202ac6454SAndrew Thompson 	int error;
41302ac6454SAndrew Thompson 
41402ac6454SAndrew Thompson 	AXE_LOCK_ASSERT(sc, MA_OWNED);
41502ac6454SAndrew Thompson 
41602ac6454SAndrew Thompson 	if (mii->mii_instance) {
41702ac6454SAndrew Thompson 		struct mii_softc *miisc;
41802ac6454SAndrew Thompson 
41902ac6454SAndrew Thompson 		LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
42002ac6454SAndrew Thompson 			mii_phy_reset(miisc);
42102ac6454SAndrew Thompson 	}
42202ac6454SAndrew Thompson 	error = mii_mediachg(mii);
42302ac6454SAndrew Thompson 	return (error);
42402ac6454SAndrew Thompson }
42502ac6454SAndrew Thompson 
42602ac6454SAndrew Thompson /*
42702ac6454SAndrew Thompson  * Report current media status.
42802ac6454SAndrew Thompson  */
42902ac6454SAndrew Thompson static void
43002ac6454SAndrew Thompson axe_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
43102ac6454SAndrew Thompson {
43202ac6454SAndrew Thompson 	struct axe_softc *sc = ifp->if_softc;
43302ac6454SAndrew Thompson 	struct mii_data *mii = GET_MII(sc);
43402ac6454SAndrew Thompson 
43502ac6454SAndrew Thompson 	AXE_LOCK(sc);
43602ac6454SAndrew Thompson 	mii_pollstat(mii);
43702ac6454SAndrew Thompson 	AXE_UNLOCK(sc);
43802ac6454SAndrew Thompson 	ifmr->ifm_active = mii->mii_media_active;
43902ac6454SAndrew Thompson 	ifmr->ifm_status = mii->mii_media_status;
44002ac6454SAndrew Thompson }
44102ac6454SAndrew Thompson 
44202ac6454SAndrew Thompson static void
44302ac6454SAndrew Thompson axe_setmulti(struct usb2_ether *ue)
44402ac6454SAndrew Thompson {
44502ac6454SAndrew Thompson 	struct axe_softc *sc = usb2_ether_getsc(ue);
44602ac6454SAndrew Thompson 	struct ifnet *ifp = usb2_ether_getifp(ue);
44702ac6454SAndrew Thompson 	struct ifmultiaddr *ifma;
44802ac6454SAndrew Thompson 	uint32_t h = 0;
44902ac6454SAndrew Thompson 	uint16_t rxmode;
45002ac6454SAndrew Thompson 	uint8_t hashtbl[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
45102ac6454SAndrew Thompson 
45202ac6454SAndrew Thompson 	AXE_LOCK_ASSERT(sc, MA_OWNED);
45302ac6454SAndrew Thompson 
45402ac6454SAndrew Thompson 	axe_cmd(sc, AXE_CMD_RXCTL_READ, 0, 0, &rxmode);
45502ac6454SAndrew Thompson 	rxmode = le16toh(rxmode);
45602ac6454SAndrew Thompson 
45702ac6454SAndrew Thompson 	if (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) {
45802ac6454SAndrew Thompson 		rxmode |= AXE_RXCMD_ALLMULTI;
45902ac6454SAndrew Thompson 		axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
46002ac6454SAndrew Thompson 		return;
46102ac6454SAndrew Thompson 	}
46202ac6454SAndrew Thompson 	rxmode &= ~AXE_RXCMD_ALLMULTI;
46302ac6454SAndrew Thompson 
46402ac6454SAndrew Thompson 	IF_ADDR_LOCK(ifp);
46502ac6454SAndrew Thompson 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
46602ac6454SAndrew Thompson 	{
46702ac6454SAndrew Thompson 		if (ifma->ifma_addr->sa_family != AF_LINK)
46802ac6454SAndrew Thompson 			continue;
46902ac6454SAndrew Thompson 		h = ether_crc32_be(LLADDR((struct sockaddr_dl *)
47002ac6454SAndrew Thompson 		    ifma->ifma_addr), ETHER_ADDR_LEN) >> 26;
47102ac6454SAndrew Thompson 		hashtbl[h / 8] |= 1 << (h % 8);
47202ac6454SAndrew Thompson 	}
47302ac6454SAndrew Thompson 	IF_ADDR_UNLOCK(ifp);
47402ac6454SAndrew Thompson 
47502ac6454SAndrew Thompson 	axe_cmd(sc, AXE_CMD_WRITE_MCAST, 0, 0, (void *)&hashtbl);
47602ac6454SAndrew Thompson 	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
47702ac6454SAndrew Thompson }
47802ac6454SAndrew Thompson 
47902ac6454SAndrew Thompson static int
48002ac6454SAndrew Thompson axe_get_phyno(struct axe_softc *sc, int sel)
48102ac6454SAndrew Thompson {
48202ac6454SAndrew Thompson 	int phyno;
48302ac6454SAndrew Thompson 
48402ac6454SAndrew Thompson 	switch (AXE_PHY_TYPE(sc->sc_phyaddrs[sel])) {
48502ac6454SAndrew Thompson 	case PHY_TYPE_100_HOME:
48602ac6454SAndrew Thompson 	case PHY_TYPE_GIG:
48702ac6454SAndrew Thompson 		phyno = AXE_PHY_NO(sc->sc_phyaddrs[sel]);
48802ac6454SAndrew Thompson 		break;
48902ac6454SAndrew Thompson 	case PHY_TYPE_SPECIAL:
49002ac6454SAndrew Thompson 		/* FALLTHROUGH */
49102ac6454SAndrew Thompson 	case PHY_TYPE_RSVD:
49202ac6454SAndrew Thompson 		/* FALLTHROUGH */
49302ac6454SAndrew Thompson 	case PHY_TYPE_NON_SUP:
49402ac6454SAndrew Thompson 		/* FALLTHROUGH */
49502ac6454SAndrew Thompson 	default:
49602ac6454SAndrew Thompson 		phyno = -1;
49702ac6454SAndrew Thompson 		break;
49802ac6454SAndrew Thompson 	}
49902ac6454SAndrew Thompson 
50002ac6454SAndrew Thompson 	return (phyno);
50102ac6454SAndrew Thompson }
50202ac6454SAndrew Thompson 
50302ac6454SAndrew Thompson static void
50402ac6454SAndrew Thompson axe_ax88178_init(struct axe_softc *sc)
50502ac6454SAndrew Thompson {
50602ac6454SAndrew Thompson 	int gpio0 = 0, phymode = 0;
50702ac6454SAndrew Thompson 	uint16_t eeprom;
50802ac6454SAndrew Thompson 
50902ac6454SAndrew Thompson 	axe_cmd(sc, AXE_CMD_SROM_WR_ENABLE, 0, 0, NULL);
51002ac6454SAndrew Thompson 	/* XXX magic */
51102ac6454SAndrew Thompson 	axe_cmd(sc, AXE_CMD_SROM_READ, 0, 0x0017, &eeprom);
51202ac6454SAndrew Thompson 	eeprom = le16toh(eeprom);
51302ac6454SAndrew Thompson 	axe_cmd(sc, AXE_CMD_SROM_WR_DISABLE, 0, 0, NULL);
51402ac6454SAndrew Thompson 
51502ac6454SAndrew Thompson 	/* if EEPROM is invalid we have to use to GPIO0 */
51602ac6454SAndrew Thompson 	if (eeprom == 0xffff) {
51702ac6454SAndrew Thompson 		phymode = 0;
51802ac6454SAndrew Thompson 		gpio0 = 1;
51902ac6454SAndrew Thompson 	} else {
52002ac6454SAndrew Thompson 		phymode = eeprom & 7;
52102ac6454SAndrew Thompson 		gpio0 = (eeprom & 0x80) ? 0 : 1;
52202ac6454SAndrew Thompson 	}
52302ac6454SAndrew Thompson 
52402ac6454SAndrew Thompson 	axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, 0x008c, NULL);
52502ac6454SAndrew Thompson 	usb2_ether_pause(&sc->sc_ue, hz / 16);
52602ac6454SAndrew Thompson 
52702ac6454SAndrew Thompson 	if ((eeprom >> 8) != 0x01) {
52802ac6454SAndrew Thompson 		axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, 0x003c, NULL);
52902ac6454SAndrew Thompson 		usb2_ether_pause(&sc->sc_ue, hz / 32);
53002ac6454SAndrew Thompson 
53102ac6454SAndrew Thompson 		axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, 0x001c, NULL);
53202ac6454SAndrew Thompson 		usb2_ether_pause(&sc->sc_ue, hz / 3);
53302ac6454SAndrew Thompson 
53402ac6454SAndrew Thompson 		axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, 0x003c, NULL);
53502ac6454SAndrew Thompson 		usb2_ether_pause(&sc->sc_ue, hz / 32);
53602ac6454SAndrew Thompson 	} else {
53702ac6454SAndrew Thompson 		axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, 0x0004, NULL);
53802ac6454SAndrew Thompson 		usb2_ether_pause(&sc->sc_ue, hz / 32);
53902ac6454SAndrew Thompson 
54002ac6454SAndrew Thompson 		axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, 0x000c, NULL);
54102ac6454SAndrew Thompson 		usb2_ether_pause(&sc->sc_ue, hz / 32);
54202ac6454SAndrew Thompson 	}
54302ac6454SAndrew Thompson 
54402ac6454SAndrew Thompson 	/* soft reset */
54502ac6454SAndrew Thompson 	axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_CLEAR, NULL);
54602ac6454SAndrew Thompson 	usb2_ether_pause(&sc->sc_ue, hz / 4);
54702ac6454SAndrew Thompson 
54802ac6454SAndrew Thompson 	axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
54902ac6454SAndrew Thompson 	    AXE_SW_RESET_PRL | AXE_178_RESET_MAGIC, NULL);
55002ac6454SAndrew Thompson 	usb2_ether_pause(&sc->sc_ue, hz / 4);
55102ac6454SAndrew Thompson 	/* Enable MII/GMII/RGMII interface to work with external PHY. */
55202ac6454SAndrew Thompson 	axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, 0, NULL);
55302ac6454SAndrew Thompson 	usb2_ether_pause(&sc->sc_ue, hz / 4);
55402ac6454SAndrew Thompson 
55502ac6454SAndrew Thompson 	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, 0, NULL);
55602ac6454SAndrew Thompson }
55702ac6454SAndrew Thompson 
55802ac6454SAndrew Thompson static void
55902ac6454SAndrew Thompson axe_ax88772_init(struct axe_softc *sc)
56002ac6454SAndrew Thompson {
56102ac6454SAndrew Thompson 	axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, 0x00b0, NULL);
56202ac6454SAndrew Thompson 	usb2_ether_pause(&sc->sc_ue, hz / 16);
56302ac6454SAndrew Thompson 
56402ac6454SAndrew Thompson 	if (sc->sc_phyno == AXE_772_PHY_NO_EPHY) {
56502ac6454SAndrew Thompson 		/* ask for the embedded PHY */
56602ac6454SAndrew Thompson 		axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, 0x01, NULL);
56702ac6454SAndrew Thompson 		usb2_ether_pause(&sc->sc_ue, hz / 64);
56802ac6454SAndrew Thompson 
56902ac6454SAndrew Thompson 		/* power down and reset state, pin reset state */
57002ac6454SAndrew Thompson 		axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
57102ac6454SAndrew Thompson 		    AXE_SW_RESET_CLEAR, NULL);
57202ac6454SAndrew Thompson 		usb2_ether_pause(&sc->sc_ue, hz / 16);
57302ac6454SAndrew Thompson 
57402ac6454SAndrew Thompson 		/* power down/reset state, pin operating state */
57502ac6454SAndrew Thompson 		axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
57602ac6454SAndrew Thompson 		    AXE_SW_RESET_IPPD | AXE_SW_RESET_PRL, NULL);
57702ac6454SAndrew Thompson 		usb2_ether_pause(&sc->sc_ue, hz / 4);
57802ac6454SAndrew Thompson 
57902ac6454SAndrew Thompson 		/* power up, reset */
58002ac6454SAndrew Thompson 		axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_PRL, NULL);
58102ac6454SAndrew Thompson 
58202ac6454SAndrew Thompson 		/* power up, operating */
58302ac6454SAndrew Thompson 		axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
58402ac6454SAndrew Thompson 		    AXE_SW_RESET_IPRL | AXE_SW_RESET_PRL, NULL);
58502ac6454SAndrew Thompson 	} else {
58602ac6454SAndrew Thompson 		/* ask for external PHY */
58702ac6454SAndrew Thompson 		axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, 0x00, NULL);
58802ac6454SAndrew Thompson 		usb2_ether_pause(&sc->sc_ue, hz / 64);
58902ac6454SAndrew Thompson 
59002ac6454SAndrew Thompson 		/* power down internal PHY */
59102ac6454SAndrew Thompson 		axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
59202ac6454SAndrew Thompson 		    AXE_SW_RESET_IPPD | AXE_SW_RESET_PRL, NULL);
59302ac6454SAndrew Thompson 	}
59402ac6454SAndrew Thompson 
59502ac6454SAndrew Thompson 	usb2_ether_pause(&sc->sc_ue, hz / 4);
59602ac6454SAndrew Thompson 	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, 0, NULL);
59702ac6454SAndrew Thompson }
59802ac6454SAndrew Thompson 
59902ac6454SAndrew Thompson static void
60002ac6454SAndrew Thompson axe_reset(struct axe_softc *sc)
60102ac6454SAndrew Thompson {
60202ac6454SAndrew Thompson 	struct usb2_config_descriptor *cd;
60302ac6454SAndrew Thompson 	usb2_error_t err;
60402ac6454SAndrew Thompson 
60502ac6454SAndrew Thompson 	cd = usb2_get_config_descriptor(sc->sc_ue.ue_udev);
60602ac6454SAndrew Thompson 
60702ac6454SAndrew Thompson 	err = usb2_req_set_config(sc->sc_ue.ue_udev, &sc->sc_mtx,
60802ac6454SAndrew Thompson 	    cd->bConfigurationValue);
60902ac6454SAndrew Thompson 	if (err)
61002ac6454SAndrew Thompson 		DPRINTF("reset failed (ignored)\n");
61102ac6454SAndrew Thompson 
61202ac6454SAndrew Thompson 	/* Wait a little while for the chip to get its brains in order. */
61302ac6454SAndrew Thompson 	usb2_ether_pause(&sc->sc_ue, hz / 100);
61402ac6454SAndrew Thompson }
61502ac6454SAndrew Thompson 
61602ac6454SAndrew Thompson static void
61702ac6454SAndrew Thompson axe_attach_post(struct usb2_ether *ue)
61802ac6454SAndrew Thompson {
61902ac6454SAndrew Thompson 	struct axe_softc *sc = usb2_ether_getsc(ue);
62002ac6454SAndrew Thompson 
62102ac6454SAndrew Thompson 	/*
62202ac6454SAndrew Thompson 	 * Load PHY indexes first. Needed by axe_xxx_init().
62302ac6454SAndrew Thompson 	 */
62402ac6454SAndrew Thompson 	axe_cmd(sc, AXE_CMD_READ_PHYID, 0, 0, sc->sc_phyaddrs);
62502ac6454SAndrew Thompson #if 1
62602ac6454SAndrew Thompson 	device_printf(sc->sc_ue.ue_dev, "PHYADDR 0x%02x:0x%02x\n",
62702ac6454SAndrew Thompson 	    sc->sc_phyaddrs[0], sc->sc_phyaddrs[1]);
62802ac6454SAndrew Thompson #endif
62902ac6454SAndrew Thompson 	sc->sc_phyno = axe_get_phyno(sc, AXE_PHY_SEL_PRI);
63002ac6454SAndrew Thompson 	if (sc->sc_phyno == -1)
63102ac6454SAndrew Thompson 		sc->sc_phyno = axe_get_phyno(sc, AXE_PHY_SEL_SEC);
63202ac6454SAndrew Thompson 	if (sc->sc_phyno == -1) {
63302ac6454SAndrew Thompson 		device_printf(sc->sc_ue.ue_dev,
63402ac6454SAndrew Thompson 		    "no valid PHY address found, assuming PHY address 0\n");
63502ac6454SAndrew Thompson 		sc->sc_phyno = 0;
63602ac6454SAndrew Thompson 	}
63702ac6454SAndrew Thompson 
63802ac6454SAndrew Thompson 	if (sc->sc_flags & AXE_FLAG_178)
63902ac6454SAndrew Thompson 		axe_ax88178_init(sc);
64002ac6454SAndrew Thompson 	else if (sc->sc_flags & AXE_FLAG_772)
64102ac6454SAndrew Thompson 		axe_ax88772_init(sc);
64202ac6454SAndrew Thompson 
64302ac6454SAndrew Thompson 	/*
64402ac6454SAndrew Thompson 	 * Get station address.
64502ac6454SAndrew Thompson 	 */
64602ac6454SAndrew Thompson 	if (sc->sc_flags & (AXE_FLAG_178 | AXE_FLAG_772))
64702ac6454SAndrew Thompson 		axe_cmd(sc, AXE_178_CMD_READ_NODEID, 0, 0, ue->ue_eaddr);
64802ac6454SAndrew Thompson 	else
64902ac6454SAndrew Thompson 		axe_cmd(sc, AXE_172_CMD_READ_NODEID, 0, 0, ue->ue_eaddr);
65002ac6454SAndrew Thompson 
65102ac6454SAndrew Thompson 	/*
65202ac6454SAndrew Thompson 	 * Fetch IPG values.
65302ac6454SAndrew Thompson 	 */
65402ac6454SAndrew Thompson 	axe_cmd(sc, AXE_CMD_READ_IPG012, 0, 0, sc->sc_ipgs);
65502ac6454SAndrew Thompson }
65602ac6454SAndrew Thompson 
65702ac6454SAndrew Thompson /*
65802ac6454SAndrew Thompson  * Probe for a AX88172 chip.
65902ac6454SAndrew Thompson  */
66002ac6454SAndrew Thompson static int
66102ac6454SAndrew Thompson axe_probe(device_t dev)
66202ac6454SAndrew Thompson {
66302ac6454SAndrew Thompson 	struct usb2_attach_arg *uaa = device_get_ivars(dev);
66402ac6454SAndrew Thompson 
665f29a0724SAndrew Thompson 	if (uaa->usb_mode != USB_MODE_HOST)
66602ac6454SAndrew Thompson 		return (ENXIO);
66702ac6454SAndrew Thompson 	if (uaa->info.bConfigIndex != AXE_CONFIG_IDX)
66802ac6454SAndrew Thompson 		return (ENXIO);
66902ac6454SAndrew Thompson 	if (uaa->info.bIfaceIndex != AXE_IFACE_IDX)
67002ac6454SAndrew Thompson 		return (ENXIO);
67102ac6454SAndrew Thompson 
67202ac6454SAndrew Thompson 	return (usb2_lookup_id_by_uaa(axe_devs, sizeof(axe_devs), uaa));
67302ac6454SAndrew Thompson }
67402ac6454SAndrew Thompson 
67502ac6454SAndrew Thompson /*
67602ac6454SAndrew Thompson  * Attach the interface. Allocate softc structures, do ifmedia
67702ac6454SAndrew Thompson  * setup and ethernet/BPF attach.
67802ac6454SAndrew Thompson  */
67902ac6454SAndrew Thompson static int
68002ac6454SAndrew Thompson axe_attach(device_t dev)
68102ac6454SAndrew Thompson {
68202ac6454SAndrew Thompson 	struct usb2_attach_arg *uaa = device_get_ivars(dev);
68302ac6454SAndrew Thompson 	struct axe_softc *sc = device_get_softc(dev);
68402ac6454SAndrew Thompson 	struct usb2_ether *ue = &sc->sc_ue;
68502ac6454SAndrew Thompson 	uint8_t iface_index;
68602ac6454SAndrew Thompson 	int error;
68702ac6454SAndrew Thompson 
68802ac6454SAndrew Thompson 	sc->sc_flags = USB_GET_DRIVER_INFO(uaa);
68902ac6454SAndrew Thompson 
69002ac6454SAndrew Thompson 	device_set_usb2_desc(dev);
69102ac6454SAndrew Thompson 
69202ac6454SAndrew Thompson 	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
69302ac6454SAndrew Thompson 
69402ac6454SAndrew Thompson 	iface_index = AXE_IFACE_IDX;
69502ac6454SAndrew Thompson 	error = usb2_transfer_setup(uaa->device, &iface_index, sc->sc_xfer,
69602ac6454SAndrew Thompson 	    axe_config, AXE_N_TRANSFER, sc, &sc->sc_mtx);
69702ac6454SAndrew Thompson 	if (error) {
69802ac6454SAndrew Thompson 		device_printf(dev, "allocating USB transfers failed!\n");
69902ac6454SAndrew Thompson 		goto detach;
70002ac6454SAndrew Thompson 	}
70102ac6454SAndrew Thompson 
70202ac6454SAndrew Thompson 	ue->ue_sc = sc;
70302ac6454SAndrew Thompson 	ue->ue_dev = dev;
70402ac6454SAndrew Thompson 	ue->ue_udev = uaa->device;
70502ac6454SAndrew Thompson 	ue->ue_mtx = &sc->sc_mtx;
70602ac6454SAndrew Thompson 	ue->ue_methods = &axe_ue_methods;
70702ac6454SAndrew Thompson 
70802ac6454SAndrew Thompson 	error = usb2_ether_ifattach(ue);
70902ac6454SAndrew Thompson 	if (error) {
71002ac6454SAndrew Thompson 		device_printf(dev, "could not attach interface\n");
71102ac6454SAndrew Thompson 		goto detach;
71202ac6454SAndrew Thompson 	}
71302ac6454SAndrew Thompson 	return (0);			/* success */
71402ac6454SAndrew Thompson 
71502ac6454SAndrew Thompson detach:
71602ac6454SAndrew Thompson 	axe_detach(dev);
71702ac6454SAndrew Thompson 	return (ENXIO);			/* failure */
71802ac6454SAndrew Thompson }
71902ac6454SAndrew Thompson 
72002ac6454SAndrew Thompson static int
72102ac6454SAndrew Thompson axe_detach(device_t dev)
72202ac6454SAndrew Thompson {
72302ac6454SAndrew Thompson 	struct axe_softc *sc = device_get_softc(dev);
72402ac6454SAndrew Thompson 	struct usb2_ether *ue = &sc->sc_ue;
72502ac6454SAndrew Thompson 
72602ac6454SAndrew Thompson 	usb2_transfer_unsetup(sc->sc_xfer, AXE_N_TRANSFER);
72702ac6454SAndrew Thompson 	usb2_ether_ifdetach(ue);
72802ac6454SAndrew Thompson 	mtx_destroy(&sc->sc_mtx);
72902ac6454SAndrew Thompson 
73002ac6454SAndrew Thompson 	return (0);
73102ac6454SAndrew Thompson }
73202ac6454SAndrew Thompson 
73302ac6454SAndrew Thompson static void
73402ac6454SAndrew Thompson axe_intr_callback(struct usb2_xfer *xfer)
73502ac6454SAndrew Thompson {
73602ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
73702ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
73802ac6454SAndrew Thompson 	case USB_ST_SETUP:
73902ac6454SAndrew Thompson tr_setup:
74002ac6454SAndrew Thompson 		xfer->frlengths[0] = xfer->max_data_length;
74102ac6454SAndrew Thompson 		usb2_start_hardware(xfer);
74202ac6454SAndrew Thompson 		return;
74302ac6454SAndrew Thompson 
74402ac6454SAndrew Thompson 	default:			/* Error */
74502ac6454SAndrew Thompson 		if (xfer->error != USB_ERR_CANCELLED) {
74602ac6454SAndrew Thompson 			/* try to clear stall first */
74702ac6454SAndrew Thompson 			xfer->flags.stall_pipe = 1;
74802ac6454SAndrew Thompson 			goto tr_setup;
74902ac6454SAndrew Thompson 		}
75002ac6454SAndrew Thompson 		return;
75102ac6454SAndrew Thompson 	}
75202ac6454SAndrew Thompson }
75302ac6454SAndrew Thompson 
75402ac6454SAndrew Thompson #if (AXE_BULK_BUF_SIZE >= 0x10000)
75502ac6454SAndrew Thompson #error "Please update axe_bulk_read_callback()!"
75602ac6454SAndrew Thompson #endif
75702ac6454SAndrew Thompson 
75802ac6454SAndrew Thompson static void
75902ac6454SAndrew Thompson axe_bulk_read_callback(struct usb2_xfer *xfer)
76002ac6454SAndrew Thompson {
76102ac6454SAndrew Thompson 	struct axe_softc *sc = xfer->priv_sc;
76202ac6454SAndrew Thompson 	struct usb2_ether *ue = &sc->sc_ue;
76302ac6454SAndrew Thompson 	struct ifnet *ifp = usb2_ether_getifp(ue);
76402ac6454SAndrew Thompson 	struct axe_sframe_hdr hdr;
76502ac6454SAndrew Thompson 	int error, pos, len, adjust;
76602ac6454SAndrew Thompson 
76702ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
76802ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
76902ac6454SAndrew Thompson 		pos = 0;
77002ac6454SAndrew Thompson 		while (1) {
77102ac6454SAndrew Thompson 			if (sc->sc_flags & (AXE_FLAG_772 | AXE_FLAG_178)) {
77202ac6454SAndrew Thompson 				if (xfer->actlen < sizeof(hdr)) {
77302ac6454SAndrew Thompson 					/* too little data */
77402ac6454SAndrew Thompson 					break;
77502ac6454SAndrew Thompson 				}
77602ac6454SAndrew Thompson 				usb2_copy_out(xfer->frbuffers, pos, &hdr, sizeof(hdr));
77702ac6454SAndrew Thompson 
77802ac6454SAndrew Thompson 				if ((hdr.len ^ hdr.ilen) != 0xFFFF) {
77902ac6454SAndrew Thompson 					/* we lost sync */
78002ac6454SAndrew Thompson 					break;
78102ac6454SAndrew Thompson 				}
78202ac6454SAndrew Thompson 				xfer->actlen -= sizeof(hdr);
78302ac6454SAndrew Thompson 				pos += sizeof(hdr);
78402ac6454SAndrew Thompson 
78502ac6454SAndrew Thompson 				len = le16toh(hdr.len);
78602ac6454SAndrew Thompson 				if (len > xfer->actlen) {
78702ac6454SAndrew Thompson 					/* invalid length */
78802ac6454SAndrew Thompson 					break;
78902ac6454SAndrew Thompson 				}
79002ac6454SAndrew Thompson 				adjust = (len & 1);
79102ac6454SAndrew Thompson 
79202ac6454SAndrew Thompson 			} else {
79302ac6454SAndrew Thompson 				len = xfer->actlen;
79402ac6454SAndrew Thompson 				adjust = 0;
79502ac6454SAndrew Thompson 			}
79602ac6454SAndrew Thompson 			error = usb2_ether_rxbuf(ue, xfer->frbuffers, pos, len);
79702ac6454SAndrew Thompson 			if (error)
79802ac6454SAndrew Thompson 				break;
79902ac6454SAndrew Thompson 
80002ac6454SAndrew Thompson 			pos += len;
80102ac6454SAndrew Thompson 			xfer->actlen -= len;
80202ac6454SAndrew Thompson 
80302ac6454SAndrew Thompson 			if (xfer->actlen <= adjust) {
80402ac6454SAndrew Thompson 				/* we are finished */
80502ac6454SAndrew Thompson 				goto tr_setup;
80602ac6454SAndrew Thompson 			}
80702ac6454SAndrew Thompson 			pos += adjust;
80802ac6454SAndrew Thompson 			xfer->actlen -= adjust;
80902ac6454SAndrew Thompson 		}
81002ac6454SAndrew Thompson 
81102ac6454SAndrew Thompson 		/* count an error */
81202ac6454SAndrew Thompson 		ifp->if_ierrors++;
81302ac6454SAndrew Thompson 
81402ac6454SAndrew Thompson 		/* FALLTHROUGH */
81502ac6454SAndrew Thompson 	case USB_ST_SETUP:
81602ac6454SAndrew Thompson tr_setup:
81702ac6454SAndrew Thompson 		xfer->frlengths[0] = xfer->max_data_length;
81802ac6454SAndrew Thompson 		usb2_start_hardware(xfer);
81902ac6454SAndrew Thompson 		usb2_ether_rxflush(ue);
82002ac6454SAndrew Thompson 		return;
82102ac6454SAndrew Thompson 
82202ac6454SAndrew Thompson 	default:			/* Error */
82302ac6454SAndrew Thompson 		DPRINTF("bulk read error, %s\n",
82402ac6454SAndrew Thompson 		    usb2_errstr(xfer->error));
82502ac6454SAndrew Thompson 
82602ac6454SAndrew Thompson 		if (xfer->error != USB_ERR_CANCELLED) {
82702ac6454SAndrew Thompson 			/* try to clear stall first */
82802ac6454SAndrew Thompson 			xfer->flags.stall_pipe = 1;
82902ac6454SAndrew Thompson 			goto tr_setup;
83002ac6454SAndrew Thompson 		}
83102ac6454SAndrew Thompson 		return;
83202ac6454SAndrew Thompson 
83302ac6454SAndrew Thompson 	}
83402ac6454SAndrew Thompson }
83502ac6454SAndrew Thompson 
83602ac6454SAndrew Thompson #if ((AXE_BULK_BUF_SIZE >= 0x10000) || (AXE_BULK_BUF_SIZE < (MCLBYTES+4)))
83702ac6454SAndrew Thompson #error "Please update axe_bulk_write_callback()!"
83802ac6454SAndrew Thompson #endif
83902ac6454SAndrew Thompson 
84002ac6454SAndrew Thompson static void
84102ac6454SAndrew Thompson axe_bulk_write_callback(struct usb2_xfer *xfer)
84202ac6454SAndrew Thompson {
84302ac6454SAndrew Thompson 	struct axe_softc *sc = xfer->priv_sc;
84402ac6454SAndrew Thompson 	struct axe_sframe_hdr hdr;
84502ac6454SAndrew Thompson 	struct ifnet *ifp = usb2_ether_getifp(&sc->sc_ue);
84602ac6454SAndrew Thompson 	struct mbuf *m;
84702ac6454SAndrew Thompson 	int pos;
84802ac6454SAndrew Thompson 
84902ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
85002ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
85102ac6454SAndrew Thompson 		DPRINTFN(11, "transfer complete\n");
85202ac6454SAndrew Thompson 		ifp->if_opackets++;
85302ac6454SAndrew Thompson 		/* FALLTHROUGH */
85402ac6454SAndrew Thompson 	case USB_ST_SETUP:
85502ac6454SAndrew Thompson tr_setup:
85602ac6454SAndrew Thompson 		if ((sc->sc_flags & AXE_FLAG_LINK) == 0) {
85702ac6454SAndrew Thompson 			/*
85802ac6454SAndrew Thompson 			 * don't send anything if there is no link !
85902ac6454SAndrew Thompson 			 */
86002ac6454SAndrew Thompson 			return;
86102ac6454SAndrew Thompson 		}
86202ac6454SAndrew Thompson 		pos = 0;
86302ac6454SAndrew Thompson 
86402ac6454SAndrew Thompson 		while (1) {
86502ac6454SAndrew Thompson 
86602ac6454SAndrew Thompson 			IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
86702ac6454SAndrew Thompson 
86802ac6454SAndrew Thompson 			if (m == NULL) {
86902ac6454SAndrew Thompson 				if (pos > 0)
87002ac6454SAndrew Thompson 					break;	/* send out data */
87102ac6454SAndrew Thompson 				return;
87202ac6454SAndrew Thompson 			}
87302ac6454SAndrew Thompson 			if (m->m_pkthdr.len > MCLBYTES) {
87402ac6454SAndrew Thompson 				m->m_pkthdr.len = MCLBYTES;
87502ac6454SAndrew Thompson 			}
87602ac6454SAndrew Thompson 			if (sc->sc_flags & (AXE_FLAG_772 | AXE_FLAG_178)) {
87702ac6454SAndrew Thompson 
87802ac6454SAndrew Thompson 				hdr.len = htole16(m->m_pkthdr.len);
87902ac6454SAndrew Thompson 				hdr.ilen = ~hdr.len;
88002ac6454SAndrew Thompson 
88102ac6454SAndrew Thompson 				usb2_copy_in(xfer->frbuffers, pos, &hdr, sizeof(hdr));
88202ac6454SAndrew Thompson 
88302ac6454SAndrew Thompson 				pos += sizeof(hdr);
88402ac6454SAndrew Thompson 
88502ac6454SAndrew Thompson 				/*
88602ac6454SAndrew Thompson 				 * NOTE: Some drivers force a short packet
88702ac6454SAndrew Thompson 				 * by appending a dummy header with zero
88802ac6454SAndrew Thompson 				 * length at then end of the USB transfer.
88902ac6454SAndrew Thompson 				 * This driver uses the
89002ac6454SAndrew Thompson 				 * USB_FORCE_SHORT_XFER flag instead.
89102ac6454SAndrew Thompson 				 */
89202ac6454SAndrew Thompson 			}
89302ac6454SAndrew Thompson 			usb2_m_copy_in(xfer->frbuffers, pos,
89402ac6454SAndrew Thompson 			    m, 0, m->m_pkthdr.len);
89502ac6454SAndrew Thompson 
89602ac6454SAndrew Thompson 			pos += m->m_pkthdr.len;
89702ac6454SAndrew Thompson 
89802ac6454SAndrew Thompson 			/*
89902ac6454SAndrew Thompson 			 * if there's a BPF listener, bounce a copy
90002ac6454SAndrew Thompson 			 * of this frame to him:
90102ac6454SAndrew Thompson 			 */
90202ac6454SAndrew Thompson 			BPF_MTAP(ifp, m);
90302ac6454SAndrew Thompson 
90402ac6454SAndrew Thompson 			m_freem(m);
90502ac6454SAndrew Thompson 
90602ac6454SAndrew Thompson 			if (sc->sc_flags & (AXE_FLAG_772 | AXE_FLAG_178)) {
90702ac6454SAndrew Thompson 				if (pos > (AXE_BULK_BUF_SIZE - MCLBYTES - sizeof(hdr))) {
90802ac6454SAndrew Thompson 					/* send out frame(s) */
90902ac6454SAndrew Thompson 					break;
91002ac6454SAndrew Thompson 				}
91102ac6454SAndrew Thompson 			} else {
91202ac6454SAndrew Thompson 				/* send out frame */
91302ac6454SAndrew Thompson 				break;
91402ac6454SAndrew Thompson 			}
91502ac6454SAndrew Thompson 		}
91602ac6454SAndrew Thompson 
91702ac6454SAndrew Thompson 		xfer->frlengths[0] = pos;
91802ac6454SAndrew Thompson 		usb2_start_hardware(xfer);
91902ac6454SAndrew Thompson 		return;
92002ac6454SAndrew Thompson 
92102ac6454SAndrew Thompson 	default:			/* Error */
92202ac6454SAndrew Thompson 		DPRINTFN(11, "transfer error, %s\n",
92302ac6454SAndrew Thompson 		    usb2_errstr(xfer->error));
92402ac6454SAndrew Thompson 
92502ac6454SAndrew Thompson 		ifp->if_oerrors++;
92602ac6454SAndrew Thompson 
92702ac6454SAndrew Thompson 		if (xfer->error != USB_ERR_CANCELLED) {
92802ac6454SAndrew Thompson 			/* try to clear stall first */
92902ac6454SAndrew Thompson 			xfer->flags.stall_pipe = 1;
93002ac6454SAndrew Thompson 			goto tr_setup;
93102ac6454SAndrew Thompson 		}
93202ac6454SAndrew Thompson 		return;
93302ac6454SAndrew Thompson 
93402ac6454SAndrew Thompson 	}
93502ac6454SAndrew Thompson }
93602ac6454SAndrew Thompson 
93702ac6454SAndrew Thompson static void
93802ac6454SAndrew Thompson axe_tick(struct usb2_ether *ue)
93902ac6454SAndrew Thompson {
94002ac6454SAndrew Thompson 	struct axe_softc *sc = usb2_ether_getsc(ue);
94102ac6454SAndrew Thompson 	struct mii_data *mii = GET_MII(sc);
94202ac6454SAndrew Thompson 
94302ac6454SAndrew Thompson 	AXE_LOCK_ASSERT(sc, MA_OWNED);
94402ac6454SAndrew Thompson 
94502ac6454SAndrew Thompson 	mii_tick(mii);
94602ac6454SAndrew Thompson 	if ((sc->sc_flags & AXE_FLAG_LINK) == 0) {
94702ac6454SAndrew Thompson 		axe_miibus_statchg(ue->ue_dev);
94802ac6454SAndrew Thompson 		if ((sc->sc_flags & AXE_FLAG_LINK) != 0)
94902ac6454SAndrew Thompson 			axe_start(ue);
95002ac6454SAndrew Thompson 	}
95102ac6454SAndrew Thompson }
95202ac6454SAndrew Thompson 
95302ac6454SAndrew Thompson static void
95402ac6454SAndrew Thompson axe_start(struct usb2_ether *ue)
95502ac6454SAndrew Thompson {
95602ac6454SAndrew Thompson 	struct axe_softc *sc = usb2_ether_getsc(ue);
95702ac6454SAndrew Thompson 
95802ac6454SAndrew Thompson 	/*
95902ac6454SAndrew Thompson 	 * start the USB transfers, if not already started:
96002ac6454SAndrew Thompson 	 */
96102ac6454SAndrew Thompson 	usb2_transfer_start(sc->sc_xfer[AXE_INTR_DT_RD]);
96202ac6454SAndrew Thompson 	usb2_transfer_start(sc->sc_xfer[AXE_BULK_DT_RD]);
96302ac6454SAndrew Thompson 	usb2_transfer_start(sc->sc_xfer[AXE_BULK_DT_WR]);
96402ac6454SAndrew Thompson }
96502ac6454SAndrew Thompson 
96602ac6454SAndrew Thompson static void
96702ac6454SAndrew Thompson axe_init(struct usb2_ether *ue)
96802ac6454SAndrew Thompson {
96902ac6454SAndrew Thompson 	struct axe_softc *sc = usb2_ether_getsc(ue);
97002ac6454SAndrew Thompson 	struct ifnet *ifp = usb2_ether_getifp(ue);
97102ac6454SAndrew Thompson 	uint16_t rxmode;
97202ac6454SAndrew Thompson 
97302ac6454SAndrew Thompson 	AXE_LOCK_ASSERT(sc, MA_OWNED);
97402ac6454SAndrew Thompson 
97502ac6454SAndrew Thompson 	/* Cancel pending I/O */
97602ac6454SAndrew Thompson 	axe_stop(ue);
97702ac6454SAndrew Thompson 
97802ac6454SAndrew Thompson #ifdef notdef
97902ac6454SAndrew Thompson 	/* Set MAC address */
98002ac6454SAndrew Thompson 	axe_mac(sc, IF_LLADDR(ifp), 1);
98102ac6454SAndrew Thompson #endif
98202ac6454SAndrew Thompson 
98302ac6454SAndrew Thompson 	/* Set transmitter IPG values */
98402ac6454SAndrew Thompson 	if (sc->sc_flags & (AXE_FLAG_178 | AXE_FLAG_772)) {
98502ac6454SAndrew Thompson 		axe_cmd(sc, AXE_178_CMD_WRITE_IPG012, sc->sc_ipgs[2],
98602ac6454SAndrew Thompson 		    (sc->sc_ipgs[1] << 8) | (sc->sc_ipgs[0]), NULL);
98702ac6454SAndrew Thompson 	} else {
98802ac6454SAndrew Thompson 		axe_cmd(sc, AXE_172_CMD_WRITE_IPG0, 0, sc->sc_ipgs[0], NULL);
98902ac6454SAndrew Thompson 		axe_cmd(sc, AXE_172_CMD_WRITE_IPG1, 0, sc->sc_ipgs[1], NULL);
99002ac6454SAndrew Thompson 		axe_cmd(sc, AXE_172_CMD_WRITE_IPG2, 0, sc->sc_ipgs[2], NULL);
99102ac6454SAndrew Thompson 	}
99202ac6454SAndrew Thompson 
99302ac6454SAndrew Thompson 	/* Enable receiver, set RX mode */
99402ac6454SAndrew Thompson 	rxmode = (AXE_RXCMD_MULTICAST | AXE_RXCMD_ENABLE);
99502ac6454SAndrew Thompson 	if (sc->sc_flags & (AXE_FLAG_178 | AXE_FLAG_772)) {
99602ac6454SAndrew Thompson 		rxmode |= AXE_178_RXCMD_MFB_2048;	/* chip default */
99702ac6454SAndrew Thompson 	} else {
99802ac6454SAndrew Thompson 		rxmode |= AXE_172_RXCMD_UNICAST;
99902ac6454SAndrew Thompson 	}
100002ac6454SAndrew Thompson 
100102ac6454SAndrew Thompson 	/* If we want promiscuous mode, set the allframes bit. */
100202ac6454SAndrew Thompson 	if (ifp->if_flags & IFF_PROMISC)
100302ac6454SAndrew Thompson 		rxmode |= AXE_RXCMD_PROMISC;
100402ac6454SAndrew Thompson 
100502ac6454SAndrew Thompson 	if (ifp->if_flags & IFF_BROADCAST)
100602ac6454SAndrew Thompson 		rxmode |= AXE_RXCMD_BROADCAST;
100702ac6454SAndrew Thompson 
100802ac6454SAndrew Thompson 	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
100902ac6454SAndrew Thompson 
101002ac6454SAndrew Thompson 	/* Load the multicast filter. */
101102ac6454SAndrew Thompson 	axe_setmulti(ue);
101202ac6454SAndrew Thompson 
101302ac6454SAndrew Thompson 	usb2_transfer_set_stall(sc->sc_xfer[AXE_BULK_DT_WR]);
101402ac6454SAndrew Thompson 
101502ac6454SAndrew Thompson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
101602ac6454SAndrew Thompson 	axe_start(ue);
101702ac6454SAndrew Thompson }
101802ac6454SAndrew Thompson 
101902ac6454SAndrew Thompson static void
102002ac6454SAndrew Thompson axe_setpromisc(struct usb2_ether *ue)
102102ac6454SAndrew Thompson {
102202ac6454SAndrew Thompson 	struct axe_softc *sc = usb2_ether_getsc(ue);
102302ac6454SAndrew Thompson 	struct ifnet *ifp = usb2_ether_getifp(ue);
102402ac6454SAndrew Thompson 	uint16_t rxmode;
102502ac6454SAndrew Thompson 
102602ac6454SAndrew Thompson 	axe_cmd(sc, AXE_CMD_RXCTL_READ, 0, 0, &rxmode);
102702ac6454SAndrew Thompson 
102802ac6454SAndrew Thompson 	rxmode = le16toh(rxmode);
102902ac6454SAndrew Thompson 
103002ac6454SAndrew Thompson 	if (ifp->if_flags & IFF_PROMISC) {
103102ac6454SAndrew Thompson 		rxmode |= AXE_RXCMD_PROMISC;
103202ac6454SAndrew Thompson 	} else {
103302ac6454SAndrew Thompson 		rxmode &= ~AXE_RXCMD_PROMISC;
103402ac6454SAndrew Thompson 	}
103502ac6454SAndrew Thompson 
103602ac6454SAndrew Thompson 	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
103702ac6454SAndrew Thompson 
103802ac6454SAndrew Thompson 	axe_setmulti(ue);
103902ac6454SAndrew Thompson }
104002ac6454SAndrew Thompson 
104102ac6454SAndrew Thompson static void
104202ac6454SAndrew Thompson axe_stop(struct usb2_ether *ue)
104302ac6454SAndrew Thompson {
104402ac6454SAndrew Thompson 	struct axe_softc *sc = usb2_ether_getsc(ue);
104502ac6454SAndrew Thompson 	struct ifnet *ifp = usb2_ether_getifp(ue);
104602ac6454SAndrew Thompson 
104702ac6454SAndrew Thompson 	AXE_LOCK_ASSERT(sc, MA_OWNED);
104802ac6454SAndrew Thompson 
104902ac6454SAndrew Thompson 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
105002ac6454SAndrew Thompson 	sc->sc_flags &= ~AXE_FLAG_LINK;
105102ac6454SAndrew Thompson 
105202ac6454SAndrew Thompson 	/*
105302ac6454SAndrew Thompson 	 * stop all the transfers, if not already stopped:
105402ac6454SAndrew Thompson 	 */
105502ac6454SAndrew Thompson 	usb2_transfer_stop(sc->sc_xfer[AXE_BULK_DT_WR]);
105602ac6454SAndrew Thompson 	usb2_transfer_stop(sc->sc_xfer[AXE_BULK_DT_RD]);
105702ac6454SAndrew Thompson 	usb2_transfer_stop(sc->sc_xfer[AXE_INTR_DT_RD]);
105802ac6454SAndrew Thompson 
105902ac6454SAndrew Thompson 	axe_reset(sc);
106002ac6454SAndrew Thompson }
1061