if_axe.c revision 188412
1/*-
2 * Copyright (c) 1997, 1998, 1999, 2000-2003
3 *	Bill Paul <wpaul@windriver.com>.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by Bill Paul.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include <sys/cdefs.h>
34__FBSDID("$FreeBSD: head/sys/dev/usb2/ethernet/if_axe2.c 188412 2009-02-09 22:02:38Z thompsa $");
35
36/*
37 * ASIX Electronics AX88172/AX88178/AX88778 USB 2.0 ethernet driver.
38 * Used in the LinkSys USB200M and various other adapters.
39 *
40 * Manuals available from:
41 * http://www.asix.com.tw/datasheet/mac/Ax88172.PDF
42 * Note: you need the manual for the AX88170 chip (USB 1.x ethernet
43 * controller) to find the definitions for the RX control register.
44 * http://www.asix.com.tw/datasheet/mac/Ax88170.PDF
45 *
46 * Written by Bill Paul <wpaul@windriver.com>
47 * Senior Engineer
48 * Wind River Systems
49 */
50
51/*
52 * The AX88172 provides USB ethernet supports at 10 and 100Mbps.
53 * It uses an external PHY (reference designs use a RealTek chip),
54 * and has a 64-bit multicast hash filter. There is some information
55 * missing from the manual which one needs to know in order to make
56 * the chip function:
57 *
58 * - You must set bit 7 in the RX control register, otherwise the
59 *   chip won't receive any packets.
60 * - You must initialize all 3 IPG registers, or you won't be able
61 *   to send any packets.
62 *
63 * Note that this device appears to only support loading the station
64 * address via autload from the EEPROM (i.e. there's no way to manaully
65 * set it).
66 *
67 * (Adam Weinberger wanted me to name this driver if_gir.c.)
68 */
69
70/*
71 * Ax88178 and Ax88772 support backported from the OpenBSD driver.
72 * 2007/02/12, J.R. Oldroyd, fbsd@opal.com
73 *
74 * Manual here:
75 * http://www.asix.com.tw/FrootAttach/datasheet/AX88178_datasheet_Rev10.pdf
76 * http://www.asix.com.tw/FrootAttach/datasheet/AX88772_datasheet_Rev10.pdf
77 */
78
79#include <dev/usb2/include/usb2_devid.h>
80#include <dev/usb2/include/usb2_standard.h>
81#include <dev/usb2/include/usb2_mfunc.h>
82#include <dev/usb2/include/usb2_error.h>
83
84#define	USB_DEBUG_VAR axe_debug
85
86#include <dev/usb2/core/usb2_core.h>
87#include <dev/usb2/core/usb2_lookup.h>
88#include <dev/usb2/core/usb2_process.h>
89#include <dev/usb2/core/usb2_debug.h>
90#include <dev/usb2/core/usb2_request.h>
91#include <dev/usb2/core/usb2_busdma.h>
92#include <dev/usb2/core/usb2_util.h>
93
94#include <dev/usb2/ethernet/usb2_ethernet.h>
95#include <dev/usb2/ethernet/if_axereg.h>
96
97/*
98 * AXE_178_MAX_FRAME_BURST
99 * max frame burst size for Ax88178 and Ax88772
100 *	0	2048 bytes
101 *	1	4096 bytes
102 *	2	8192 bytes
103 *	3	16384 bytes
104 * use the largest your system can handle without USB stalling.
105 *
106 * NB: 88772 parts appear to generate lots of input errors with
107 * a 2K rx buffer and 8K is only slightly faster than 4K on an
108 * EHCI port on a T42 so change at your own risk.
109 */
110#define AXE_178_MAX_FRAME_BURST	1
111
112#if USB_DEBUG
113static int axe_debug = 0;
114
115SYSCTL_NODE(_hw_usb2, OID_AUTO, axe, CTLFLAG_RW, 0, "USB axe");
116SYSCTL_INT(_hw_usb2_axe, OID_AUTO, debug, CTLFLAG_RW, &axe_debug, 0,
117    "Debug level");
118#endif
119
120/*
121 * Various supported device vendors/products.
122 */
123static const struct usb2_device_id axe_devs[] = {
124	{USB_VPI(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_UF200, 0)},
125	{USB_VPI(USB_VENDOR_ACERCM, USB_PRODUCT_ACERCM_EP1427X2, 0)},
126	{USB_VPI(USB_VENDOR_APPLE, USB_PRODUCT_APPLE_ETHERNET, AXE_FLAG_772)},
127	{USB_VPI(USB_VENDOR_ASIX, USB_PRODUCT_ASIX_AX88172, 0)},
128	{USB_VPI(USB_VENDOR_ASIX, USB_PRODUCT_ASIX_AX88178, AXE_FLAG_178)},
129	{USB_VPI(USB_VENDOR_ASIX, USB_PRODUCT_ASIX_AX88772, AXE_FLAG_772)},
130	{USB_VPI(USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC210T, 0)},
131	{USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5D5055, AXE_FLAG_178)},
132	{USB_VPI(USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USB2AR, 0)},
133	{USB_VPI(USB_VENDOR_CISCOLINKSYS, USB_PRODUCT_CISCOLINKSYS_USB200MV2, AXE_FLAG_772)},
134	{USB_VPI(USB_VENDOR_COREGA, USB_PRODUCT_COREGA_FETHER_USB2_TX, 0)},
135	{USB_VPI(USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DUBE100, 0)},
136	{USB_VPI(USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DUBE100B1, AXE_FLAG_772)},
137	{USB_VPI(USB_VENDOR_GOODWAY, USB_PRODUCT_GOODWAY_GWUSB2E, 0)},
138	{USB_VPI(USB_VENDOR_IODATA, USB_PRODUCT_IODATA_ETGUS2, AXE_FLAG_178)},
139	{USB_VPI(USB_VENDOR_JVC, USB_PRODUCT_JVC_MP_PRX1, 0)},
140	{USB_VPI(USB_VENDOR_LINKSYS2, USB_PRODUCT_LINKSYS2_USB200M, 0)},
141	{USB_VPI(USB_VENDOR_LINKSYS4, USB_PRODUCT_LINKSYS4_USB1000, AXE_FLAG_178)},
142	{USB_VPI(USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUAU2KTX, 0)},
143	{USB_VPI(USB_VENDOR_NETGEAR, USB_PRODUCT_NETGEAR_FA120, 0)},
144	{USB_VPI(USB_VENDOR_OQO, USB_PRODUCT_OQO_ETHER01PLUS, AXE_FLAG_772)},
145	{USB_VPI(USB_VENDOR_PLANEX3, USB_PRODUCT_PLANEX3_GU1000T, AXE_FLAG_178)},
146	{USB_VPI(USB_VENDOR_SITECOM, USB_PRODUCT_SITECOM_LN029, 0)},
147	{USB_VPI(USB_VENDOR_SITECOMEU, USB_PRODUCT_SITECOMEU_LN028, AXE_FLAG_178)},
148	{USB_VPI(USB_VENDOR_SYSTEMTALKS, USB_PRODUCT_SYSTEMTALKS_SGCX2UL, 0)},
149};
150
151static device_probe_t axe_probe;
152static device_attach_t axe_attach;
153static device_detach_t axe_detach;
154static device_shutdown_t axe_shutdown;
155
156static usb2_callback_t axe_intr_callback;
157static usb2_callback_t axe_bulk_read_callback;
158static usb2_callback_t axe_bulk_write_callback;
159
160static miibus_readreg_t axe_miibus_readreg;
161static miibus_writereg_t axe_miibus_writereg;
162static miibus_statchg_t axe_miibus_statchg;
163
164static usb2_ether_fn_t axe_attach_post;
165static usb2_ether_fn_t axe_init;
166static usb2_ether_fn_t axe_stop;
167static usb2_ether_fn_t axe_start;
168static usb2_ether_fn_t axe_tick;
169static usb2_ether_fn_t axe_setmulti;
170static usb2_ether_fn_t axe_setpromisc;
171
172static int	axe_ifmedia_upd(struct ifnet *);
173static void	axe_ifmedia_sts(struct ifnet *, struct ifmediareq *);
174static int	axe_cmd(struct axe_softc *, int, int, int, void *);
175static void	axe_ax88178_init(struct axe_softc *);
176static void	axe_ax88772_init(struct axe_softc *);
177static int	axe_get_phyno(struct axe_softc *, int);
178
179static const struct usb2_config axe_config[AXE_N_TRANSFER] = {
180
181	[AXE_BULK_DT_WR] = {
182		.type = UE_BULK,
183		.endpoint = UE_ADDR_ANY,
184		.direction = UE_DIR_OUT,
185		.mh.bufsize = AXE_BULK_BUF_SIZE,
186		.mh.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
187		.mh.callback = axe_bulk_write_callback,
188		.mh.timeout = 10000,	/* 10 seconds */
189	},
190
191	[AXE_BULK_DT_RD] = {
192		.type = UE_BULK,
193		.endpoint = UE_ADDR_ANY,
194		.direction = UE_DIR_IN,
195#if (MCLBYTES < 2048)
196#error "(MCLBYTES < 2048)"
197#endif
198		.mh.bufsize = MCLBYTES,
199		.mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
200		.mh.callback = axe_bulk_read_callback,
201		.mh.timeout = 0,	/* no timeout */
202	},
203
204	[AXE_INTR_DT_RD] = {
205		.type = UE_INTERRUPT,
206		.endpoint = UE_ADDR_ANY,
207		.direction = UE_DIR_IN,
208		.mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
209		.mh.bufsize = 0,	/* use wMaxPacketSize */
210		.mh.callback = axe_intr_callback,
211	},
212};
213
214static device_method_t axe_methods[] = {
215	/* Device interface */
216	DEVMETHOD(device_probe, axe_probe),
217	DEVMETHOD(device_attach, axe_attach),
218	DEVMETHOD(device_detach, axe_detach),
219	DEVMETHOD(device_shutdown, axe_shutdown),
220
221	/* bus interface */
222	DEVMETHOD(bus_print_child, bus_generic_print_child),
223	DEVMETHOD(bus_driver_added, bus_generic_driver_added),
224
225	/* MII interface */
226	DEVMETHOD(miibus_readreg, axe_miibus_readreg),
227	DEVMETHOD(miibus_writereg, axe_miibus_writereg),
228	DEVMETHOD(miibus_statchg, axe_miibus_statchg),
229
230	{0, 0}
231};
232
233static driver_t axe_driver = {
234	.name = "axe",
235	.methods = axe_methods,
236	.size = sizeof(struct axe_softc),
237};
238
239static devclass_t axe_devclass;
240
241DRIVER_MODULE(axe, ushub, axe_driver, axe_devclass, NULL, 0);
242DRIVER_MODULE(miibus, axe, miibus_driver, miibus_devclass, 0, 0);
243MODULE_DEPEND(axe, usb2_ethernet, 1, 1, 1);
244MODULE_DEPEND(axe, usb2_core, 1, 1, 1);
245MODULE_DEPEND(axe, ether, 1, 1, 1);
246MODULE_DEPEND(axe, miibus, 1, 1, 1);
247
248static const struct usb2_ether_methods axe_ue_methods = {
249	.ue_attach_post = axe_attach_post,
250	.ue_start = axe_start,
251	.ue_init = axe_init,
252	.ue_stop = axe_stop,
253	.ue_tick = axe_tick,
254	.ue_setmulti = axe_setmulti,
255	.ue_setpromisc = axe_setpromisc,
256	.ue_mii_upd = axe_ifmedia_upd,
257	.ue_mii_sts = axe_ifmedia_sts,
258};
259
260static int
261axe_cmd(struct axe_softc *sc, int cmd, int index, int val, void *buf)
262{
263	struct usb2_device_request req;
264	usb2_error_t err;
265
266	AXE_LOCK_ASSERT(sc, MA_OWNED);
267
268	req.bmRequestType = (AXE_CMD_IS_WRITE(cmd) ?
269	    UT_WRITE_VENDOR_DEVICE :
270	    UT_READ_VENDOR_DEVICE);
271	req.bRequest = AXE_CMD_CMD(cmd);
272	USETW(req.wValue, val);
273	USETW(req.wIndex, index);
274	USETW(req.wLength, AXE_CMD_LEN(cmd));
275
276	err = usb2_ether_do_request(&sc->sc_ue, &req, buf, 1000);
277
278	return (err);
279}
280
281static int
282axe_miibus_readreg(device_t dev, int phy, int reg)
283{
284	struct axe_softc *sc = device_get_softc(dev);
285	uint16_t val;
286	int locked;
287
288	if (sc->sc_phyno != phy)
289		return (0);
290
291	locked = mtx_owned(&sc->sc_mtx);
292	if (!locked)
293		AXE_LOCK(sc);
294
295	axe_cmd(sc, AXE_CMD_MII_OPMODE_SW, 0, 0, NULL);
296	axe_cmd(sc, AXE_CMD_MII_READ_REG, reg, phy, &val);
297	axe_cmd(sc, AXE_CMD_MII_OPMODE_HW, 0, 0, NULL);
298
299	val = le16toh(val);
300	if ((sc->sc_flags & AXE_FLAG_772) != 0 && reg == MII_BMSR) {
301		/*
302		 * BMSR of AX88772 indicates that it supports extended
303		 * capability but the extended status register is
304		 * revered for embedded ethernet PHY. So clear the
305		 * extended capability bit of BMSR.
306		 */
307		val &= ~BMSR_EXTCAP;
308	}
309
310	if (!locked)
311		AXE_UNLOCK(sc);
312	return (val);
313}
314
315static int
316axe_miibus_writereg(device_t dev, int phy, int reg, int val)
317{
318	struct axe_softc *sc = device_get_softc(dev);
319	int locked;
320
321	val = htole16(val);
322
323	if (sc->sc_phyno != phy)
324		return (0);
325
326	locked = mtx_owned(&sc->sc_mtx);
327	if (!locked)
328		AXE_LOCK(sc);
329
330	axe_cmd(sc, AXE_CMD_MII_OPMODE_SW, 0, 0, NULL);
331	axe_cmd(sc, AXE_CMD_MII_WRITE_REG, reg, phy, &val);
332	axe_cmd(sc, AXE_CMD_MII_OPMODE_HW, 0, 0, NULL);
333
334	if (!locked)
335		AXE_UNLOCK(sc);
336	return (0);
337}
338
339static void
340axe_miibus_statchg(device_t dev)
341{
342	struct axe_softc *sc = device_get_softc(dev);
343	struct mii_data *mii = GET_MII(sc);
344	uint16_t val;
345	int err, locked;
346
347	locked = mtx_owned(&sc->sc_mtx);
348	if (!locked)
349		AXE_LOCK(sc);
350
351	val = (mii->mii_media_active & IFM_GMASK) == IFM_FDX ?
352	    AXE_MEDIA_FULL_DUPLEX : 0;
353	if (sc->sc_flags & (AXE_FLAG_178 | AXE_FLAG_772)) {
354		val |= AXE_178_MEDIA_RX_EN | AXE_178_MEDIA_MAGIC;
355
356		switch (IFM_SUBTYPE(mii->mii_media_active)) {
357		case IFM_1000_T:
358			val |= AXE_178_MEDIA_GMII | AXE_178_MEDIA_ENCK;
359			break;
360		case IFM_100_TX:
361			val |= AXE_178_MEDIA_100TX;
362			break;
363		case IFM_10_T:
364			/* doesn't need to be handled */
365			break;
366		}
367	}
368	err = axe_cmd(sc, AXE_CMD_WRITE_MEDIA, 0, val, NULL);
369	if (err)
370		device_printf(dev, "media change failed, error %d\n", err);
371
372	if (!locked)
373		AXE_UNLOCK(sc);
374}
375
376/*
377 * Set media options.
378 */
379static int
380axe_ifmedia_upd(struct ifnet *ifp)
381{
382	struct axe_softc *sc = ifp->if_softc;
383	struct mii_data *mii = GET_MII(sc);
384
385	AXE_LOCK_ASSERT(sc, MA_OWNED);
386
387        sc->sc_flags &= ~AXE_FLAG_LINK;
388	if (mii->mii_instance) {
389		struct mii_softc *miisc;
390
391		LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
392			mii_phy_reset(miisc);
393	}
394	mii_mediachg(mii);
395	return (0);
396}
397
398/*
399 * Report current media status.
400 */
401static void
402axe_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
403{
404	struct axe_softc *sc = ifp->if_softc;
405	struct mii_data *mii = GET_MII(sc);
406
407	AXE_LOCK(sc);
408	mii_pollstat(mii);
409	AXE_UNLOCK(sc);
410	ifmr->ifm_active = mii->mii_media_active;
411	ifmr->ifm_status = mii->mii_media_status;
412}
413
414static void
415axe_setmulti(struct usb2_ether *ue)
416{
417	struct axe_softc *sc = usb2_ether_getsc(ue);
418	struct ifnet *ifp = usb2_ether_getifp(ue);
419	struct ifmultiaddr *ifma;
420	uint32_t h = 0;
421	uint16_t rxmode;
422	uint8_t hashtbl[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
423
424	AXE_LOCK_ASSERT(sc, MA_OWNED);
425
426	axe_cmd(sc, AXE_CMD_RXCTL_READ, 0, 0, &rxmode);
427	rxmode = le16toh(rxmode);
428
429	if (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) {
430		rxmode |= AXE_RXCMD_ALLMULTI;
431		axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
432		return;
433	}
434	rxmode &= ~AXE_RXCMD_ALLMULTI;
435
436	IF_ADDR_LOCK(ifp);
437	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
438	{
439		if (ifma->ifma_addr->sa_family != AF_LINK)
440			continue;
441		h = ether_crc32_be(LLADDR((struct sockaddr_dl *)
442		    ifma->ifma_addr), ETHER_ADDR_LEN) >> 26;
443		hashtbl[h / 8] |= 1 << (h % 8);
444	}
445	IF_ADDR_UNLOCK(ifp);
446
447	axe_cmd(sc, AXE_CMD_WRITE_MCAST, 0, 0, (void *)&hashtbl);
448	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
449}
450
451static int
452axe_get_phyno(struct axe_softc *sc, int sel)
453{
454	int phyno;
455
456	switch (AXE_PHY_TYPE(sc->sc_phyaddrs[sel])) {
457	case PHY_TYPE_100_HOME:
458	case PHY_TYPE_GIG:
459		phyno = AXE_PHY_NO(sc->sc_phyaddrs[sel]);
460		break;
461	case PHY_TYPE_SPECIAL:
462		/* FALLTHROUGH */
463	case PHY_TYPE_RSVD:
464		/* FALLTHROUGH */
465	case PHY_TYPE_NON_SUP:
466		/* FALLTHROUGH */
467	default:
468		phyno = -1;
469		break;
470	}
471
472	return (phyno);
473}
474
475static void
476axe_ax88178_init(struct axe_softc *sc)
477{
478	int gpio0 = 0, phymode = 0;
479	uint16_t eeprom;
480
481	axe_cmd(sc, AXE_CMD_SROM_WR_ENABLE, 0, 0, NULL);
482	/* XXX magic */
483	axe_cmd(sc, AXE_CMD_SROM_READ, 0, 0x0017, &eeprom);
484	eeprom = le16toh(eeprom);
485	axe_cmd(sc, AXE_CMD_SROM_WR_DISABLE, 0, 0, NULL);
486
487	/* if EEPROM is invalid we have to use to GPIO0 */
488	if (eeprom == 0xffff) {
489		phymode = 0;
490		gpio0 = 1;
491	} else {
492		phymode = eeprom & 7;
493		gpio0 = (eeprom & 0x80) ? 0 : 1;
494	}
495
496	axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, 0x008c, NULL);
497	usb2_ether_pause(&sc->sc_ue, hz / 16);
498
499	if ((eeprom >> 8) != 0x01) {
500		axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, 0x003c, NULL);
501		usb2_ether_pause(&sc->sc_ue, hz / 32);
502
503		axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, 0x001c, NULL);
504		usb2_ether_pause(&sc->sc_ue, hz / 3);
505
506		axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, 0x003c, NULL);
507		usb2_ether_pause(&sc->sc_ue, hz / 32);
508	} else {
509		axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, 0x0004, NULL);
510		usb2_ether_pause(&sc->sc_ue, hz / 32);
511
512		axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, 0x000c, NULL);
513		usb2_ether_pause(&sc->sc_ue, hz / 32);
514	}
515
516	/* soft reset */
517	axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_CLEAR, NULL);
518	usb2_ether_pause(&sc->sc_ue, hz / 4);
519
520	axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
521	    AXE_SW_RESET_PRL | AXE_178_RESET_MAGIC, NULL);
522	usb2_ether_pause(&sc->sc_ue, hz / 4);
523	/* Enable MII/GMII/RGMII interface to work with external PHY. */
524	axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, 0, NULL);
525	usb2_ether_pause(&sc->sc_ue, hz / 4);
526
527	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, 0, NULL);
528}
529
530static void
531axe_ax88772_init(struct axe_softc *sc)
532{
533	axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, 0x00b0, NULL);
534	usb2_ether_pause(&sc->sc_ue, hz / 16);
535
536	if (sc->sc_phyno == AXE_772_PHY_NO_EPHY) {
537		/* ask for the embedded PHY */
538		axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, 0x01, NULL);
539		usb2_ether_pause(&sc->sc_ue, hz / 64);
540
541		/* power down and reset state, pin reset state */
542		axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
543		    AXE_SW_RESET_CLEAR, NULL);
544		usb2_ether_pause(&sc->sc_ue, hz / 16);
545
546		/* power down/reset state, pin operating state */
547		axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
548		    AXE_SW_RESET_IPPD | AXE_SW_RESET_PRL, NULL);
549		usb2_ether_pause(&sc->sc_ue, hz / 4);
550
551		/* power up, reset */
552		axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_PRL, NULL);
553
554		/* power up, operating */
555		axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
556		    AXE_SW_RESET_IPRL | AXE_SW_RESET_PRL, NULL);
557	} else {
558		/* ask for external PHY */
559		axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, 0x00, NULL);
560		usb2_ether_pause(&sc->sc_ue, hz / 64);
561
562		/* power down internal PHY */
563		axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
564		    AXE_SW_RESET_IPPD | AXE_SW_RESET_PRL, NULL);
565	}
566
567	usb2_ether_pause(&sc->sc_ue, hz / 4);
568	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, 0, NULL);
569}
570
571static void
572axe_reset(struct axe_softc *sc)
573{
574	struct usb2_config_descriptor *cd;
575	usb2_error_t err;
576
577	cd = usb2_get_config_descriptor(sc->sc_ue.ue_udev);
578
579	err = usb2_req_set_config(sc->sc_ue.ue_udev, &sc->sc_mtx,
580	    cd->bConfigurationValue);
581	if (err)
582		DPRINTF("reset failed (ignored)\n");
583
584	/* Wait a little while for the chip to get its brains in order. */
585	usb2_ether_pause(&sc->sc_ue, hz / 100);
586}
587
588static void
589axe_attach_post(struct usb2_ether *ue)
590{
591	struct axe_softc *sc = usb2_ether_getsc(ue);
592
593	/*
594	 * Load PHY indexes first. Needed by axe_xxx_init().
595	 */
596	axe_cmd(sc, AXE_CMD_READ_PHYID, 0, 0, sc->sc_phyaddrs);
597#if 1
598	device_printf(sc->sc_ue.ue_dev, "PHYADDR 0x%02x:0x%02x\n",
599	    sc->sc_phyaddrs[0], sc->sc_phyaddrs[1]);
600#endif
601	sc->sc_phyno = axe_get_phyno(sc, AXE_PHY_SEL_PRI);
602	if (sc->sc_phyno == -1)
603		sc->sc_phyno = axe_get_phyno(sc, AXE_PHY_SEL_SEC);
604	if (sc->sc_phyno == -1) {
605		device_printf(sc->sc_ue.ue_dev,
606		    "no valid PHY address found, assuming PHY address 0\n");
607		sc->sc_phyno = 0;
608	}
609
610	if (sc->sc_flags & AXE_FLAG_178)
611		axe_ax88178_init(sc);
612	else if (sc->sc_flags & AXE_FLAG_772)
613		axe_ax88772_init(sc);
614
615	/*
616	 * Get station address.
617	 */
618	if (sc->sc_flags & (AXE_FLAG_178 | AXE_FLAG_772))
619		axe_cmd(sc, AXE_178_CMD_READ_NODEID, 0, 0, ue->ue_eaddr);
620	else
621		axe_cmd(sc, AXE_172_CMD_READ_NODEID, 0, 0, ue->ue_eaddr);
622
623	/*
624	 * Fetch IPG values.
625	 */
626	axe_cmd(sc, AXE_CMD_READ_IPG012, 0, 0, sc->sc_ipgs);
627}
628
629/*
630 * Probe for a AX88172 chip.
631 */
632static int
633axe_probe(device_t dev)
634{
635	struct usb2_attach_arg *uaa = device_get_ivars(dev);
636
637	if (uaa->usb2_mode != USB_MODE_HOST)
638		return (ENXIO);
639	if (uaa->info.bConfigIndex != AXE_CONFIG_IDX)
640		return (ENXIO);
641	if (uaa->info.bIfaceIndex != AXE_IFACE_IDX)
642		return (ENXIO);
643
644	return (usb2_lookup_id_by_uaa(axe_devs, sizeof(axe_devs), uaa));
645}
646
647/*
648 * Attach the interface. Allocate softc structures, do ifmedia
649 * setup and ethernet/BPF attach.
650 */
651static int
652axe_attach(device_t dev)
653{
654	struct usb2_attach_arg *uaa = device_get_ivars(dev);
655	struct axe_softc *sc = device_get_softc(dev);
656	struct usb2_ether *ue = &sc->sc_ue;
657	uint8_t iface_index;
658	int error;
659
660	sc->sc_flags = USB_GET_DRIVER_INFO(uaa);
661
662	device_set_usb2_desc(dev);
663
664	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
665
666	iface_index = AXE_IFACE_IDX;
667	error = usb2_transfer_setup(uaa->device, &iface_index, sc->sc_xfer,
668	    axe_config, AXE_N_TRANSFER, sc, &sc->sc_mtx);
669	if (error) {
670		device_printf(dev, "allocating USB transfers failed!\n");
671		goto detach;
672	}
673
674	ue->ue_sc = sc;
675	ue->ue_dev = dev;
676	ue->ue_udev = uaa->device;
677	ue->ue_mtx = &sc->sc_mtx;
678	ue->ue_methods = &axe_ue_methods;
679
680	error = usb2_ether_ifattach(ue);
681	if (error) {
682		device_printf(dev, "could not attach interface\n");
683		goto detach;
684	}
685	return (0);			/* success */
686
687detach:
688	axe_detach(dev);
689	return (ENXIO);			/* failure */
690}
691
692static int
693axe_detach(device_t dev)
694{
695	struct axe_softc *sc = device_get_softc(dev);
696	struct usb2_ether *ue = &sc->sc_ue;
697
698	usb2_transfer_unsetup(sc->sc_xfer, AXE_N_TRANSFER);
699	usb2_ether_ifdetach(ue);
700	mtx_destroy(&sc->sc_mtx);
701
702	return (0);
703}
704
705static void
706axe_intr_callback(struct usb2_xfer *xfer)
707{
708	switch (USB_GET_STATE(xfer)) {
709	case USB_ST_TRANSFERRED:
710	case USB_ST_SETUP:
711tr_setup:
712		xfer->frlengths[0] = xfer->max_data_length;
713		usb2_start_hardware(xfer);
714		return;
715
716	default:			/* Error */
717		if (xfer->error != USB_ERR_CANCELLED) {
718			/* try to clear stall first */
719			xfer->flags.stall_pipe = 1;
720			goto tr_setup;
721		}
722		return;
723	}
724}
725
726#if (AXE_BULK_BUF_SIZE >= 0x10000)
727#error "Please update axe_bulk_read_callback()!"
728#endif
729
730static void
731axe_bulk_read_callback(struct usb2_xfer *xfer)
732{
733	struct axe_softc *sc = xfer->priv_sc;
734	struct usb2_ether *ue = &sc->sc_ue;
735	struct ifnet *ifp = usb2_ether_getifp(ue);
736	struct axe_sframe_hdr hdr;
737	int error, pos, len, adjust;
738
739	switch (USB_GET_STATE(xfer)) {
740	case USB_ST_TRANSFERRED:
741		pos = 0;
742		while (1) {
743			if (sc->sc_flags & (AXE_FLAG_772 | AXE_FLAG_178)) {
744				if (xfer->actlen < sizeof(hdr)) {
745					/* too little data */
746					break;
747				}
748				usb2_copy_out(xfer->frbuffers, pos, &hdr, sizeof(hdr));
749
750				if ((hdr.len ^ hdr.ilen) != 0xFFFF) {
751					/* we lost sync */
752					break;
753				}
754				xfer->actlen -= sizeof(hdr);
755				pos += sizeof(hdr);
756
757				len = le16toh(hdr.len);
758				if (len > xfer->actlen) {
759					/* invalid length */
760					break;
761				}
762				adjust = (len & 1);
763
764			} else {
765				len = xfer->actlen;
766				adjust = 0;
767			}
768			error = usb2_ether_rxbuf(ue, xfer->frbuffers, pos, len);
769			if (error)
770				break;
771
772			pos += len;
773			xfer->actlen -= len;
774
775			if (xfer->actlen <= adjust) {
776				/* we are finished */
777				goto tr_setup;
778			}
779			pos += adjust;
780			xfer->actlen -= adjust;
781		}
782
783		/* count an error */
784		ifp->if_ierrors++;
785
786		/* FALLTHROUGH */
787	case USB_ST_SETUP:
788tr_setup:
789		xfer->frlengths[0] = xfer->max_data_length;
790		usb2_start_hardware(xfer);
791		usb2_ether_rxflush(ue);
792		return;
793
794	default:			/* Error */
795		DPRINTF("bulk read error, %s\n",
796		    usb2_errstr(xfer->error));
797
798		if (xfer->error != USB_ERR_CANCELLED) {
799			/* try to clear stall first */
800			xfer->flags.stall_pipe = 1;
801			goto tr_setup;
802		}
803		return;
804
805	}
806}
807
808#if ((AXE_BULK_BUF_SIZE >= 0x10000) || (AXE_BULK_BUF_SIZE < (MCLBYTES+4)))
809#error "Please update axe_bulk_write_callback()!"
810#endif
811
812static void
813axe_bulk_write_callback(struct usb2_xfer *xfer)
814{
815	struct axe_softc *sc = xfer->priv_sc;
816	struct axe_sframe_hdr hdr;
817	struct ifnet *ifp = usb2_ether_getifp(&sc->sc_ue);
818	struct mbuf *m;
819	int pos;
820
821	switch (USB_GET_STATE(xfer)) {
822	case USB_ST_TRANSFERRED:
823		DPRINTFN(11, "transfer complete\n");
824		ifp->if_opackets++;
825		/* FALLTHROUGH */
826	case USB_ST_SETUP:
827tr_setup:
828		if ((sc->sc_flags & AXE_FLAG_LINK) == 0) {
829			/*
830			 * don't send anything if there is no link !
831			 */
832			return;
833		}
834		pos = 0;
835
836		while (1) {
837
838			IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
839
840			if (m == NULL) {
841				if (pos > 0)
842					break;	/* send out data */
843				return;
844			}
845			if (m->m_pkthdr.len > MCLBYTES) {
846				m->m_pkthdr.len = MCLBYTES;
847			}
848			if (sc->sc_flags & (AXE_FLAG_772 | AXE_FLAG_178)) {
849
850				hdr.len = htole16(m->m_pkthdr.len);
851				hdr.ilen = ~hdr.len;
852
853				usb2_copy_in(xfer->frbuffers, pos, &hdr, sizeof(hdr));
854
855				pos += sizeof(hdr);
856
857				/*
858				 * NOTE: Some drivers force a short packet
859				 * by appending a dummy header with zero
860				 * length at then end of the USB transfer.
861				 * This driver uses the
862				 * USB_FORCE_SHORT_XFER flag instead.
863				 */
864			}
865			usb2_m_copy_in(xfer->frbuffers, pos,
866			    m, 0, m->m_pkthdr.len);
867
868			pos += m->m_pkthdr.len;
869
870			/*
871			 * if there's a BPF listener, bounce a copy
872			 * of this frame to him:
873			 */
874			BPF_MTAP(ifp, m);
875
876			m_freem(m);
877
878			if (sc->sc_flags & (AXE_FLAG_772 | AXE_FLAG_178)) {
879				if (pos > (AXE_BULK_BUF_SIZE - MCLBYTES - sizeof(hdr))) {
880					/* send out frame(s) */
881					break;
882				}
883			} else {
884				/* send out frame */
885				break;
886			}
887		}
888
889		xfer->frlengths[0] = pos;
890		usb2_start_hardware(xfer);
891		return;
892
893	default:			/* Error */
894		DPRINTFN(11, "transfer error, %s\n",
895		    usb2_errstr(xfer->error));
896
897		ifp->if_oerrors++;
898
899		if (xfer->error != USB_ERR_CANCELLED) {
900			/* try to clear stall first */
901			xfer->flags.stall_pipe = 1;
902			goto tr_setup;
903		}
904		return;
905
906	}
907}
908
909static void
910axe_tick(struct usb2_ether *ue)
911{
912	struct axe_softc *sc = usb2_ether_getsc(ue);
913	struct mii_data *mii = GET_MII(sc);
914
915	AXE_LOCK_ASSERT(sc, MA_OWNED);
916
917	mii_tick(mii);
918	if ((sc->sc_flags & AXE_FLAG_LINK) == 0
919	    && mii->mii_media_status & IFM_ACTIVE &&
920	    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
921		sc->sc_flags |= AXE_FLAG_LINK;
922		axe_start(ue);
923	}
924}
925
926static void
927axe_start(struct usb2_ether *ue)
928{
929	struct axe_softc *sc = usb2_ether_getsc(ue);
930
931	/*
932	 * start the USB transfers, if not already started:
933	 */
934	usb2_transfer_start(sc->sc_xfer[AXE_INTR_DT_RD]);
935	usb2_transfer_start(sc->sc_xfer[AXE_BULK_DT_RD]);
936	usb2_transfer_start(sc->sc_xfer[AXE_BULK_DT_WR]);
937}
938
939static void
940axe_init(struct usb2_ether *ue)
941{
942	struct axe_softc *sc = usb2_ether_getsc(ue);
943	struct ifnet *ifp = usb2_ether_getifp(ue);
944	uint16_t rxmode;
945
946	AXE_LOCK_ASSERT(sc, MA_OWNED);
947
948	/* Cancel pending I/O */
949	axe_stop(ue);
950
951#ifdef notdef
952	/* Set MAC address */
953	axe_mac(sc, IF_LLADDR(ifp), 1);
954#endif
955
956	/* Set transmitter IPG values */
957	if (sc->sc_flags & (AXE_FLAG_178 | AXE_FLAG_772)) {
958		axe_cmd(sc, AXE_178_CMD_WRITE_IPG012, sc->sc_ipgs[2],
959		    (sc->sc_ipgs[1] << 8) | (sc->sc_ipgs[0]), NULL);
960	} else {
961		axe_cmd(sc, AXE_172_CMD_WRITE_IPG0, 0, sc->sc_ipgs[0], NULL);
962		axe_cmd(sc, AXE_172_CMD_WRITE_IPG1, 0, sc->sc_ipgs[1], NULL);
963		axe_cmd(sc, AXE_172_CMD_WRITE_IPG2, 0, sc->sc_ipgs[2], NULL);
964	}
965
966	/* Enable receiver, set RX mode */
967	rxmode = (AXE_RXCMD_MULTICAST | AXE_RXCMD_ENABLE);
968	if (sc->sc_flags & (AXE_FLAG_178 | AXE_FLAG_772)) {
969		rxmode |= AXE_178_RXCMD_MFB_2048;	/* chip default */
970	} else {
971		rxmode |= AXE_172_RXCMD_UNICAST;
972	}
973
974	/* If we want promiscuous mode, set the allframes bit. */
975	if (ifp->if_flags & IFF_PROMISC)
976		rxmode |= AXE_RXCMD_PROMISC;
977
978	if (ifp->if_flags & IFF_BROADCAST)
979		rxmode |= AXE_RXCMD_BROADCAST;
980
981	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
982
983	/* Load the multicast filter. */
984	axe_setmulti(ue);
985
986	usb2_transfer_set_stall(sc->sc_xfer[AXE_BULK_DT_WR]);
987
988	ifp->if_drv_flags |= IFF_DRV_RUNNING;
989	axe_start(ue);
990}
991
992static void
993axe_setpromisc(struct usb2_ether *ue)
994{
995	struct axe_softc *sc = usb2_ether_getsc(ue);
996	struct ifnet *ifp = usb2_ether_getifp(ue);
997	uint16_t rxmode;
998
999	axe_cmd(sc, AXE_CMD_RXCTL_READ, 0, 0, &rxmode);
1000
1001	rxmode = le16toh(rxmode);
1002
1003	if (ifp->if_flags & IFF_PROMISC) {
1004		rxmode |= AXE_RXCMD_PROMISC;
1005	} else {
1006		rxmode &= ~AXE_RXCMD_PROMISC;
1007	}
1008
1009	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
1010
1011	axe_setmulti(ue);
1012}
1013
1014static void
1015axe_stop(struct usb2_ether *ue)
1016{
1017	struct axe_softc *sc = usb2_ether_getsc(ue);
1018	struct ifnet *ifp = usb2_ether_getifp(ue);
1019
1020	AXE_LOCK_ASSERT(sc, MA_OWNED);
1021
1022	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1023	sc->sc_flags &= ~AXE_FLAG_LINK;
1024
1025	/*
1026	 * stop all the transfers, if not already stopped:
1027	 */
1028	usb2_transfer_stop(sc->sc_xfer[AXE_BULK_DT_WR]);
1029	usb2_transfer_stop(sc->sc_xfer[AXE_BULK_DT_RD]);
1030	usb2_transfer_stop(sc->sc_xfer[AXE_INTR_DT_RD]);
1031
1032	axe_reset(sc);
1033}
1034
1035/*
1036 * Stop all chip I/O so that the kernel's probe routines don't
1037 * get confused by errant DMAs when rebooting.
1038 */
1039static int
1040axe_shutdown(device_t dev)
1041{
1042	struct axe_softc *sc = device_get_softc(dev);
1043
1044	usb2_ether_ifshutdown(&sc->sc_ue);
1045
1046	return (0);
1047}
1048