xref: /openbsd-src/sys/dev/usb/if_axe.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: if_axe.c,v 1.91 2008/11/28 02:44:18 brad Exp $	*/
2 
3 /*
4  * Copyright (c) 2005, 2006, 2007 Jonathan Gray <jsg@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 /*
20  * Copyright (c) 1997, 1998, 1999, 2000-2003
21  *	Bill Paul <wpaul@windriver.com>.  All rights reserved.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the above copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *	This product includes software developed by Bill Paul.
34  * 4. Neither the name of the author nor the names of any co-contributors
35  *    may be used to endorse or promote products derived from this software
36  *    without specific prior written permission.
37  *
38  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
39  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
41  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
42  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
43  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
44  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
45  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
46  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
47  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
48  * THE POSSIBILITY OF SUCH DAMAGE.
49  */
50 
51 #include <sys/cdefs.h>
52 
53 /*
54  * ASIX Electronics AX88172 USB 2.0 ethernet driver. Used in the
55  * LinkSys USB200M and various other adapters.
56  *
57  * Manuals available from:
58  * http://www.asix.com.tw/datasheet/mac/Ax88172.PDF
59  * Note: you need the manual for the AX88170 chip (USB 1.x ethernet
60  * controller) to find the definitions for the RX control register.
61  * http://www.asix.com.tw/datasheet/mac/Ax88170.PDF
62  *
63  * Written by Bill Paul <wpaul@windriver.com>
64  * Senior Engineer
65  * Wind River Systems
66  */
67 
68 /*
69  * The AX88172 provides USB ethernet supports at 10 and 100Mbps.
70  * It uses an external PHY (reference designs use a RealTek chip),
71  * and has a 64-bit multicast hash filter. There is some information
72  * missing from the manual which one needs to know in order to make
73  * the chip function:
74  *
75  * - You must set bit 7 in the RX control register, otherwise the
76  *   chip won't receive any packets.
77  * - You must initialize all 3 IPG registers, or you won't be able
78  *   to send any packets.
79  *
80  * Note that this device appears to only support loading the station
81  * address via autload from the EEPROM (i.e. there's no way to manaully
82  * set it).
83  *
84  * (Adam Weinberger wanted me to name this driver if_gir.c.)
85  */
86 
87 /*
88  * Ported to OpenBSD 3/28/2004 by Greg Taleck <taleck@oz.net>
89  * with bits and pieces from the aue and url drivers.
90  */
91 
92 #include "bpfilter.h"
93 
94 #include <sys/param.h>
95 #include <sys/systm.h>
96 #include <sys/sockio.h>
97 #include <sys/rwlock.h>
98 #include <sys/mbuf.h>
99 #include <sys/kernel.h>
100 #include <sys/proc.h>
101 #include <sys/socket.h>
102 
103 #include <sys/device.h>
104 
105 #include <machine/bus.h>
106 
107 #include <net/if.h>
108 #include <net/if_dl.h>
109 #include <net/if_media.h>
110 
111 #if NBPFILTER > 0
112 #include <net/bpf.h>
113 #endif
114 
115 #ifdef INET
116 #include <netinet/in.h>
117 #include <netinet/in_systm.h>
118 #include <netinet/in_var.h>
119 #include <netinet/ip.h>
120 #include <netinet/if_ether.h>
121 #endif
122 
123 #include <dev/mii/mii.h>
124 #include <dev/mii/miivar.h>
125 
126 #include <dev/usb/usb.h>
127 #include <dev/usb/usbdi.h>
128 #include <dev/usb/usbdi_util.h>
129 #include <dev/usb/usbdivar.h>
130 #include <dev/usb/usbdevs.h>
131 
132 #include <dev/usb/if_axereg.h>
133 
134 #ifdef AXE_DEBUG
135 #define DPRINTF(x)	do { if (axedebug) printf x; } while (0)
136 #define DPRINTFN(n,x)	do { if (axedebug >= (n)) printf x; } while (0)
137 int	axedebug = 0;
138 #else
139 #define DPRINTF(x)
140 #define DPRINTFN(n,x)
141 #endif
142 
143 /*
144  * Various supported device vendors/products.
145  */
146 const struct axe_type axe_devs[] = {
147 	{ { USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_UF200}, 0 },
148 	{ { USB_VENDOR_ACERCM, USB_PRODUCT_ACERCM_EP1427X2}, 0 },
149 	{ { USB_VENDOR_APPLE, USB_PRODUCT_APPLE_ETHERNET }, AX772 },
150 	{ { USB_VENDOR_ASIX, USB_PRODUCT_ASIX_AX88172}, 0 },
151 	{ { USB_VENDOR_ASIX, USB_PRODUCT_ASIX_AX88772}, AX772 },
152 	{ { USB_VENDOR_ASIX, USB_PRODUCT_ASIX_AX88178}, AX178 },
153 	{ { USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC210T}, 0 },
154 	{ { USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5D5055 }, AX178 },
155 	{ { USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USB2AR}, 0},
156 	{ { USB_VENDOR_CISCOLINKSYS, USB_PRODUCT_CISCOLINKSYS_USB200MV2}, AX772 },
157 	{ { USB_VENDOR_COREGA, USB_PRODUCT_COREGA_FETHER_USB2_TX }, 0},
158 	{ { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DUBE100}, 0 },
159 	{ { USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DUBE100B1 }, AX772 },
160 	{ { USB_VENDOR_GOODWAY, USB_PRODUCT_GOODWAY_GWUSB2E}, 0 },
161 	{ { USB_VENDOR_IODATA, USB_PRODUCT_IODATA_ETGUS2 }, AX178 },
162 	{ { USB_VENDOR_JVC, USB_PRODUCT_JVC_MP_PRX1}, 0 },
163 	{ { USB_VENDOR_LINKSYS2, USB_PRODUCT_LINKSYS2_USB200M}, 0 },
164 	{ { USB_VENDOR_LINKSYS4, USB_PRODUCT_LINKSYS4_USB1000 }, AX178 },
165 	{ { USB_VENDOR_LOGITEC, USB_PRODUCT_LOGITEC_LAN_GTJU2}, AX178 },
166 	{ { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUAU2GT}, AX178 },
167 	{ { USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUAU2KTX}, 0 },
168 	{ { USB_VENDOR_NETGEAR, USB_PRODUCT_NETGEAR_FA120}, 0 },
169 	{ { USB_VENDOR_OQO, USB_PRODUCT_OQO_ETHER01PLUS }, AX772 },
170 	{ { USB_VENDOR_PLANEX3, USB_PRODUCT_PLANEX3_GU1000T }, AX178 },
171 	{ { USB_VENDOR_SYSTEMTALKS, USB_PRODUCT_SYSTEMTALKS_SGCX2UL}, 0 },
172 	{ { USB_VENDOR_SITECOM, USB_PRODUCT_SITECOM_LN029}, 0 },
173 	{ { USB_VENDOR_SITECOMEU, USB_PRODUCT_SITECOMEU_LN028 }, AX178 }
174 };
175 
176 #define axe_lookup(v, p) ((struct axe_type *)usb_lookup(axe_devs, v, p))
177 
178 int axe_match(struct device *, void *, void *);
179 void axe_attach(struct device *, struct device *, void *);
180 int axe_detach(struct device *, int);
181 int axe_activate(struct device *, enum devact);
182 
183 struct cfdriver axe_cd = {
184 	NULL, "axe", DV_IFNET
185 };
186 
187 const struct cfattach axe_ca = {
188 	sizeof(struct axe_softc),
189 	axe_match,
190 	axe_attach,
191 	axe_detach,
192 	axe_activate,
193 };
194 
195 int axe_tx_list_init(struct axe_softc *);
196 int axe_rx_list_init(struct axe_softc *);
197 struct mbuf *axe_newbuf(void);
198 int axe_encap(struct axe_softc *, struct mbuf *, int);
199 void axe_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
200 void axe_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
201 void axe_tick(void *);
202 void axe_tick_task(void *);
203 void axe_start(struct ifnet *);
204 int axe_ioctl(struct ifnet *, u_long, caddr_t);
205 void axe_init(void *);
206 void axe_stop(struct axe_softc *);
207 void axe_watchdog(struct ifnet *);
208 int axe_miibus_readreg(struct device *, int, int);
209 void axe_miibus_writereg(struct device *, int, int, int);
210 void axe_miibus_statchg(struct device *);
211 int axe_cmd(struct axe_softc *, int, int, int, void *);
212 int axe_ifmedia_upd(struct ifnet *);
213 void axe_ifmedia_sts(struct ifnet *, struct ifmediareq *);
214 void axe_reset(struct axe_softc *sc);
215 
216 void axe_setmulti(struct axe_softc *);
217 void axe_lock_mii(struct axe_softc *sc);
218 void axe_unlock_mii(struct axe_softc *sc);
219 
220 void axe_ax88178_init(struct axe_softc *);
221 void axe_ax88772_init(struct axe_softc *);
222 
223 /* Get exclusive access to the MII registers */
224 void
225 axe_lock_mii(struct axe_softc *sc)
226 {
227 	sc->axe_refcnt++;
228 	rw_enter_write(&sc->axe_mii_lock);
229 }
230 
231 void
232 axe_unlock_mii(struct axe_softc *sc)
233 {
234 	rw_exit_write(&sc->axe_mii_lock);
235 	if (--sc->axe_refcnt < 0)
236 		usb_detach_wakeup(&sc->axe_dev);
237 }
238 
239 int
240 axe_cmd(struct axe_softc *sc, int cmd, int index, int val, void *buf)
241 {
242 	usb_device_request_t	req;
243 	usbd_status		err;
244 
245 	if (sc->axe_dying)
246 		return(0);
247 
248 	if (AXE_CMD_DIR(cmd))
249 		req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
250 	else
251 		req.bmRequestType = UT_READ_VENDOR_DEVICE;
252 	req.bRequest = AXE_CMD_CMD(cmd);
253 	USETW(req.wValue, val);
254 	USETW(req.wIndex, index);
255 	USETW(req.wLength, AXE_CMD_LEN(cmd));
256 
257 	err = usbd_do_request(sc->axe_udev, &req, buf);
258 
259 	if (err) {
260 		DPRINTF(("axe_cmd err: cmd: %d\n", cmd));
261 		return(-1);
262 	}
263 
264 	return(0);
265 }
266 
267 int
268 axe_miibus_readreg(struct device *dev, int phy, int reg)
269 {
270 	struct axe_softc	*sc = (void *)dev;
271 	usbd_status		err;
272 	uWord			val;
273 
274 	if (sc->axe_dying) {
275 		DPRINTF(("axe: dying\n"));
276 		return(0);
277 	}
278 
279 #ifdef notdef
280 	/*
281 	 * The chip tells us the MII address of any supported
282 	 * PHYs attached to the chip, so only read from those.
283 	 */
284 
285 	DPRINTF(("axe_miibus_readreg: phy 0x%x reg 0x%x\n", phy, reg));
286 
287 	if (sc->axe_phyaddrs[0] != AXE_NOPHY && phy != sc->axe_phyaddrs[0])
288 		return (0);
289 
290 	if (sc->axe_phyaddrs[1] != AXE_NOPHY && phy != sc->axe_phyaddrs[1])
291 		return (0);
292 #endif
293 	if (sc->axe_phyaddrs[0] != 0xFF && sc->axe_phyaddrs[0] != phy)
294 		return (0);
295 
296 	USETW(val, 0);
297 
298 	axe_lock_mii(sc);
299 	axe_cmd(sc, AXE_CMD_MII_OPMODE_SW, 0, 0, NULL);
300 	err = axe_cmd(sc, AXE_CMD_MII_READ_REG, reg, phy, val);
301 	axe_cmd(sc, AXE_CMD_MII_OPMODE_HW, 0, 0, NULL);
302 	axe_unlock_mii(sc);
303 
304 	if (err) {
305 		printf("axe%d: read PHY failed\n", sc->axe_unit);
306 		return(-1);
307 	}
308 	DPRINTF(("axe_miibus_readreg: phy 0x%x reg 0x%x val 0x%x\n",
309 	    phy, reg, UGETW(val)));
310 
311 	if (UGETW(val) && UGETW(val) != 0xffff)
312 		sc->axe_phyaddrs[0] = phy;
313 
314 	return (UGETW(val));
315 }
316 
317 void
318 axe_miibus_writereg(struct device *dev, int phy, int reg, int val)
319 {
320 	struct axe_softc	*sc = (void *)dev;
321 	usbd_status		err;
322 	uWord			uval;
323 
324 	if (sc->axe_dying)
325 		return;
326 
327 	USETW(uval, val);
328 
329 	axe_lock_mii(sc);
330 	axe_cmd(sc, AXE_CMD_MII_OPMODE_SW, 0, 0, NULL);
331 	err = axe_cmd(sc, AXE_CMD_MII_WRITE_REG, reg, phy, uval);
332 	axe_cmd(sc, AXE_CMD_MII_OPMODE_HW, 0, 0, NULL);
333 	axe_unlock_mii(sc);
334 
335 	if (err) {
336 		printf("axe%d: write PHY failed\n", sc->axe_unit);
337 		return;
338 	}
339 }
340 
341 void
342 axe_miibus_statchg(struct device *dev)
343 {
344 	struct axe_softc	*sc = (void *)dev;
345 	struct mii_data		*mii = GET_MII(sc);
346 	int			val, err;
347 
348 	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
349 		val = AXE_MEDIA_FULL_DUPLEX;
350 	else
351 		val = 0;
352 
353 	if (sc->axe_flags & AX178 || sc->axe_flags & AX772) {
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 
369 	DPRINTF(("axe_miibus_statchg: val=0x%x\n", val));
370 	err = axe_cmd(sc, AXE_CMD_WRITE_MEDIA, 0, val, NULL);
371 	if (err) {
372 		printf("%s: media change failed\n", sc->axe_dev.dv_xname);
373 		return;
374 	}
375 }
376 
377 /*
378  * Set media options.
379  */
380 int
381 axe_ifmedia_upd(struct ifnet *ifp)
382 {
383 	struct axe_softc	*sc = ifp->if_softc;
384 	struct mii_data		*mii = GET_MII(sc);
385 
386 	sc->axe_link = 0;
387 	if (mii->mii_instance) {
388 		struct mii_softc	*miisc;
389 		LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
390 			mii_phy_reset(miisc);
391 	}
392 	mii_mediachg(mii);
393 
394 	return (0);
395 }
396 
397 /*
398  * Report current media status.
399  */
400 void
401 axe_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
402 {
403 	struct axe_softc	*sc = ifp->if_softc;
404 	struct mii_data		*mii = GET_MII(sc);
405 
406 	mii_pollstat(mii);
407 	ifmr->ifm_active = mii->mii_media_active;
408 	ifmr->ifm_status = mii->mii_media_status;
409 }
410 
411 void
412 axe_setmulti(struct axe_softc *sc)
413 {
414 	struct ifnet		*ifp;
415 	struct ether_multi *enm;
416 	struct ether_multistep step;
417 	u_int32_t		h = 0;
418 	uWord			urxmode;
419 	u_int16_t		rxmode;
420 	u_int8_t		hashtbl[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
421 
422 	if (sc->axe_dying)
423 		return;
424 
425 	ifp = GET_IFP(sc);
426 
427 	axe_cmd(sc, AXE_CMD_RXCTL_READ, 0, 0, urxmode);
428 	rxmode = UGETW(urxmode);
429 
430 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
431 allmulti:
432 		rxmode |= AXE_RXCMD_ALLMULTI;
433 		axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
434 		return;
435 	} else
436 		rxmode &= ~AXE_RXCMD_ALLMULTI;
437 
438 	/* now program new ones */
439 	ETHER_FIRST_MULTI(step, &sc->arpcom, enm);
440 	while (enm != NULL) {
441 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
442 			   ETHER_ADDR_LEN) != 0)
443 			goto allmulti;
444 
445 		h = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN) >> 26;
446 		hashtbl[h / 8] |= 1 << (h % 8);
447 		ETHER_NEXT_MULTI(step, enm);
448 	}
449 
450 	ifp->if_flags &= ~IFF_ALLMULTI;
451 	axe_cmd(sc, AXE_CMD_WRITE_MCAST, 0, 0, (void *)&hashtbl);
452 	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
453 	return;
454 }
455 
456 void
457 axe_reset(struct axe_softc *sc)
458 {
459 	if (sc->axe_dying)
460 		return;
461 	/* XXX What to reset? */
462 
463 	/* Wait a little while for the chip to get its brains in order. */
464 	DELAY(1000);
465 	return;
466 }
467 
468 void
469 axe_ax88178_init(struct axe_softc *sc)
470 {
471 	int gpio0 = 0, phymode = 0;
472 	u_int16_t eeprom;
473 
474 	axe_cmd(sc, AXE_CMD_SROM_WR_ENABLE, 0, 0, NULL);
475 	/* XXX magic */
476 	axe_cmd(sc, AXE_CMD_SROM_READ, 0, 0x0017, &eeprom);
477 	axe_cmd(sc, AXE_CMD_SROM_WR_DISABLE, 0, 0, NULL);
478 
479 	eeprom = letoh16(eeprom);
480 
481 	DPRINTF((" EEPROM is 0x%x\n", eeprom));
482 
483 	/* if EEPROM is invalid we have to use to GPIO0 */
484 	if (eeprom == 0xffff) {
485 		phymode = 0;
486 		gpio0 = 1;
487 	} else {
488 		phymode = eeprom & 7;
489 		gpio0 = (eeprom & 0x80) ? 0 : 1;
490 	}
491 
492 	DPRINTF(("use gpio0: %d, phymode %d\n", gpio0, phymode));
493 
494 	axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, 0x008c, NULL);
495 	usbd_delay_ms(sc->axe_udev, 40);
496 	if ((eeprom >> 8) != 1) {
497 		axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, 0x003c, NULL);
498 		usbd_delay_ms(sc->axe_udev, 30);
499 
500 		axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, 0x001c, NULL);
501 		usbd_delay_ms(sc->axe_udev, 300);
502 
503 		axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, 0x003c, NULL);
504 		usbd_delay_ms(sc->axe_udev, 30);
505 	} else {
506 		DPRINTF(("axe gpio phymode == 1 path\n"));
507 		axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, 0x0004, NULL);
508 		usbd_delay_ms(sc->axe_udev, 30);
509 		axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, 0x000c, NULL);
510 		usbd_delay_ms(sc->axe_udev, 30);
511 	}
512 
513 	/* soft reset */
514 	axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_CLEAR, NULL);
515 	usbd_delay_ms(sc->axe_udev, 150);
516 	axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
517 	    AXE_SW_RESET_PRL | AXE_178_RESET_MAGIC, NULL);
518 	usbd_delay_ms(sc->axe_udev, 150);
519 	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, 0, NULL);
520 }
521 
522 void
523 axe_ax88772_init(struct axe_softc *sc)
524 {
525 	axe_cmd(sc, AXE_CMD_WRITE_GPIO, 0, 0x00b0, NULL);
526 	usbd_delay_ms(sc->axe_udev, 40);
527 
528 	if (sc->axe_phyaddrs[1] == AXE_INTPHY) {
529 		/* ask for the embedded PHY */
530 		axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, 0x01, NULL);
531 		usbd_delay_ms(sc->axe_udev, 10);
532 
533 		/* power down and reset state, pin reset state */
534 		axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_CLEAR, NULL);
535 		usbd_delay_ms(sc->axe_udev, 60);
536 
537 		/* power down/reset state, pin operating state */
538 		axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
539 		    AXE_SW_RESET_IPPD | AXE_SW_RESET_PRL, NULL);
540 		usbd_delay_ms(sc->axe_udev, 150);
541 
542 		/* power up, reset */
543 		axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0, AXE_SW_RESET_PRL, NULL);
544 
545 		/* power up, operating */
546 		axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
547 		    AXE_SW_RESET_IPRL | AXE_SW_RESET_PRL, NULL);
548 	} else {
549 		/* ask for external PHY */
550 		axe_cmd(sc, AXE_CMD_SW_PHY_SELECT, 0, 0x00, NULL);
551 		usbd_delay_ms(sc->axe_udev, 10);
552 
553 		/* power down internal PHY */
554 		axe_cmd(sc, AXE_CMD_SW_RESET_REG, 0,
555 		    AXE_SW_RESET_IPPD | AXE_SW_RESET_PRL, NULL);
556 	}
557 
558 	usbd_delay_ms(sc->axe_udev, 150);
559 	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, 0, NULL);
560 }
561 
562 /*
563  * Probe for a AX88172 chip.
564  */
565 int
566 axe_match(struct device *parent, void *match, void *aux)
567 {
568 	struct usb_attach_arg *uaa = aux;
569 
570 	if (!uaa->iface)
571 		return(UMATCH_NONE);
572 
573 	return (axe_lookup(uaa->vendor, uaa->product) != NULL ?
574 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
575 }
576 
577 /*
578  * Attach the interface. Allocate softc structures, do ifmedia
579  * setup and ethernet/BPF attach.
580  */
581 void
582 axe_attach(struct device *parent, struct device *self, void *aux)
583 {
584 	struct axe_softc *sc = (struct axe_softc *)self;
585 	struct usb_attach_arg *uaa = aux;
586 	usbd_device_handle dev = uaa->device;
587 	usbd_status err;
588 	usb_interface_descriptor_t *id;
589 	usb_endpoint_descriptor_t *ed;
590 	struct mii_data	*mii;
591 	u_char eaddr[ETHER_ADDR_LEN];
592 	char *devname = sc->axe_dev.dv_xname;
593 	struct ifnet *ifp;
594 	int i, s;
595 
596 	sc->axe_unit = self->dv_unit; /*device_get_unit(self);*/
597 
598 	err = usbd_set_config_no(dev, AXE_CONFIG_NO, 1);
599 	if (err) {
600 		printf("%s: getting interface handle failed\n",
601 		    sc->axe_dev.dv_xname);
602 		return;
603 	}
604 
605 	sc->axe_flags = axe_lookup(uaa->vendor, uaa->product)->axe_flags;
606 
607 	usb_init_task(&sc->axe_tick_task, axe_tick_task, sc);
608 	rw_init(&sc->axe_mii_lock, "axemii");
609 	usb_init_task(&sc->axe_stop_task, (void (*)(void *))axe_stop, sc);
610 
611 	err = usbd_device2interface_handle(dev, AXE_IFACE_IDX, &sc->axe_iface);
612 	if (err) {
613 		printf("%s: getting interface handle failed\n",
614 		    sc->axe_dev.dv_xname);
615 		return;
616 	}
617 
618 	sc->axe_udev = dev;
619 	sc->axe_product = uaa->product;
620 	sc->axe_vendor = uaa->vendor;
621 
622 	id = usbd_get_interface_descriptor(sc->axe_iface);
623 
624 	/* decide on what our bufsize will be */
625 	if (sc->axe_flags & AX178 || sc->axe_flags & AX772)
626 		sc->axe_bufsz = (sc->axe_udev->speed == USB_SPEED_HIGH) ?
627 		    AXE_178_MAX_BUFSZ : AXE_178_MIN_BUFSZ;
628 	else
629 		sc->axe_bufsz = AXE_172_BUFSZ;
630 
631 	/* Find endpoints. */
632 	for (i = 0; i < id->bNumEndpoints; i++) {
633 		ed = usbd_interface2endpoint_descriptor(sc->axe_iface, i);
634 		if (!ed) {
635 			printf("%s: couldn't get ep %d\n",
636 			    sc->axe_dev.dv_xname, i);
637 			return;
638 		}
639 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
640 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
641 			sc->axe_ed[AXE_ENDPT_RX] = ed->bEndpointAddress;
642 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
643 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
644 			sc->axe_ed[AXE_ENDPT_TX] = ed->bEndpointAddress;
645 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
646 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
647 			sc->axe_ed[AXE_ENDPT_INTR] = ed->bEndpointAddress;
648 		}
649 	}
650 
651 	s = splnet();
652 
653 	printf("%s:", sc->axe_dev.dv_xname);
654 
655 	/* We need the PHYID for init dance in some cases */
656 	axe_cmd(sc, AXE_CMD_READ_PHYID, 0, 0, (void *)&sc->axe_phyaddrs);
657 
658 	DPRINTF((" phyaddrs[0]: %x phyaddrs[1]: %x\n",
659 	    sc->axe_phyaddrs[0], sc->axe_phyaddrs[1]));
660 
661 	if (sc->axe_flags & AX178) {
662 		axe_ax88178_init(sc);
663 		printf(" AX88178");
664 	} else if (sc->axe_flags & AX772) {
665 		axe_ax88772_init(sc);
666 		printf(" AX88772");
667 	} else
668 		printf(" AX88172");
669 
670 	/*
671 	 * Get station address.
672 	 */
673 	if (sc->axe_flags & AX178 || sc->axe_flags & AX772)
674 		axe_cmd(sc, AXE_178_CMD_READ_NODEID, 0, 0, &eaddr);
675 	else
676 		axe_cmd(sc, AXE_172_CMD_READ_NODEID, 0, 0, &eaddr);
677 
678 	/*
679 	 * Load IPG values
680 	 */
681 	axe_cmd(sc, AXE_CMD_READ_IPG012, 0, 0, (void *)&sc->axe_ipgs);
682 
683 	/*
684 	 * Work around broken adapters that appear to lie about
685 	 * their PHY addresses.
686 	 */
687 	sc->axe_phyaddrs[0] = sc->axe_phyaddrs[1] = 0xFF;
688 
689 	/*
690 	 * An ASIX chip was detected. Inform the world.
691 	 */
692 	printf(", address %s\n", ether_sprintf(eaddr));
693 
694 	bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
695 
696 	/* Initialize interface info.*/
697 	ifp = &sc->arpcom.ac_if;
698 	ifp->if_softc = sc;
699 	strlcpy(ifp->if_xname, devname, IFNAMSIZ);
700 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
701 	ifp->if_ioctl = axe_ioctl;
702 	ifp->if_start = axe_start;
703 
704 	ifp->if_watchdog = axe_watchdog;
705 
706 /*	ifp->if_baudrate = 10000000; */
707 /*	ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;*/
708 
709 	IFQ_SET_READY(&ifp->if_snd);
710 
711 	ifp->if_capabilities = IFCAP_VLAN_MTU;
712 
713 	/* Initialize MII/media info. */
714 	mii = &sc->axe_mii;
715 	mii->mii_ifp = ifp;
716 	mii->mii_readreg = axe_miibus_readreg;
717 	mii->mii_writereg = axe_miibus_writereg;
718 	mii->mii_statchg = axe_miibus_statchg;
719 	mii->mii_flags = MIIF_AUTOTSLEEP;
720 
721 	ifmedia_init(&mii->mii_media, 0, axe_ifmedia_upd, axe_ifmedia_sts);
722 	mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0);
723 
724 	if (LIST_FIRST(&mii->mii_phys) == NULL) {
725 		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
726 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
727 	} else
728 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
729 
730 	/* Attach the interface. */
731 	if_attach(ifp);
732 	ether_ifattach(ifp);
733 
734 	timeout_set(&sc->axe_stat_ch, axe_tick, sc);
735 
736 	sc->axe_attached = 1;
737 	splx(s);
738 
739 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->axe_udev,
740 			   &sc->axe_dev);
741 }
742 
743 int
744 axe_detach(struct device *self, int flags)
745 {
746 	struct axe_softc	*sc = (struct axe_softc *)self;
747 	int			s;
748 	struct ifnet		*ifp = GET_IFP(sc);
749 
750 	DPRINTFN(2,("%s: %s: enter\n", sc->axe_dev.dv_xname, __func__));
751 
752 	/* Detached before attached finished, so just bail out. */
753 	if (!sc->axe_attached)
754 		return (0);
755 
756 	timeout_del(&sc->axe_stat_ch);
757 
758 	sc->axe_dying = 1;
759 
760 	if (sc->axe_ep[AXE_ENDPT_TX] != NULL)
761 		usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_TX]);
762 	if (sc->axe_ep[AXE_ENDPT_RX] != NULL)
763 		usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_RX]);
764 	if (sc->axe_ep[AXE_ENDPT_INTR] != NULL)
765 		usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_INTR]);
766 
767 	/*
768 	 * Remove any pending tasks.  They cannot be executing because they run
769 	 * in the same thread as detach.
770 	 */
771 	usb_rem_task(sc->axe_udev, &sc->axe_tick_task);
772 	usb_rem_task(sc->axe_udev, &sc->axe_stop_task);
773 
774 	s = splusb();
775 
776 	if (--sc->axe_refcnt >= 0) {
777 		/* Wait for processes to go away */
778 		usb_detach_wait(&sc->axe_dev);
779 	}
780 
781 	if (ifp->if_flags & IFF_RUNNING)
782 		axe_stop(sc);
783 
784 	mii_detach(&sc->axe_mii, MII_PHY_ANY, MII_OFFSET_ANY);
785 	ifmedia_delete_instance(&sc->axe_mii.mii_media, IFM_INST_ANY);
786 	ether_ifdetach(ifp);
787 	if_detach(ifp);
788 
789 #ifdef DIAGNOSTIC
790 	if (sc->axe_ep[AXE_ENDPT_TX] != NULL ||
791 	    sc->axe_ep[AXE_ENDPT_RX] != NULL ||
792 	    sc->axe_ep[AXE_ENDPT_INTR] != NULL)
793 		printf("%s: detach has active endpoints\n",
794 		    sc->axe_dev.dv_xname);
795 #endif
796 
797 	sc->axe_attached = 0;
798 
799 	if (--sc->axe_refcnt >= 0) {
800 		/* Wait for processes to go away. */
801 		usb_detach_wait(&sc->axe_dev);
802 	}
803 	splx(s);
804 
805 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->axe_udev,
806 			   &sc->axe_dev);
807 
808 	return (0);
809 }
810 
811 int
812 axe_activate(struct device *self, enum devact act)
813 {
814 	struct axe_softc *sc = (struct axe_softc *)self;
815 
816 	DPRINTFN(2,("%s: %s: enter\n", sc->axe_dev.dv_xname, __func__));
817 
818 	switch (act) {
819 	case DVACT_ACTIVATE:
820 		break;
821 
822 	case DVACT_DEACTIVATE:
823 		sc->axe_dying = 1;
824 		break;
825 	}
826 	return (0);
827 }
828 
829 struct mbuf *
830 axe_newbuf(void)
831 {
832 	struct mbuf		*m;
833 
834 	MGETHDR(m, M_DONTWAIT, MT_DATA);
835 	if (m == NULL)
836 		return (NULL);
837 
838 	MCLGET(m, M_DONTWAIT);
839 	if (!(m->m_flags & M_EXT)) {
840 		m_freem(m);
841 		return (NULL);
842 	}
843 
844 	m->m_len = m->m_pkthdr.len = MCLBYTES;
845 	m_adj(m, ETHER_ALIGN);
846 
847 	return (m);
848 }
849 
850 int
851 axe_rx_list_init(struct axe_softc *sc)
852 {
853 	struct axe_cdata *cd;
854 	struct axe_chain *c;
855 	int i;
856 
857 	DPRINTF(("%s: %s: enter\n", sc->axe_dev.dv_xname, __func__));
858 
859 	cd = &sc->axe_cdata;
860 	for (i = 0; i < AXE_RX_LIST_CNT; i++) {
861 		c = &cd->axe_rx_chain[i];
862 		c->axe_sc = sc;
863 		c->axe_idx = i;
864 		c->axe_mbuf = NULL;
865 		if (c->axe_xfer == NULL) {
866 			c->axe_xfer = usbd_alloc_xfer(sc->axe_udev);
867 			if (c->axe_xfer == NULL)
868 				return (ENOBUFS);
869 			c->axe_buf = usbd_alloc_buffer(c->axe_xfer,
870 			    sc->axe_bufsz);
871 			if (c->axe_buf == NULL) {
872 				usbd_free_xfer(c->axe_xfer);
873 				return (ENOBUFS);
874 			}
875 		}
876 	}
877 
878 	return (0);
879 }
880 
881 int
882 axe_tx_list_init(struct axe_softc *sc)
883 {
884 	struct axe_cdata *cd;
885 	struct axe_chain *c;
886 	int i;
887 
888 	DPRINTF(("%s: %s: enter\n", sc->axe_dev.dv_xname, __func__));
889 
890 	cd = &sc->axe_cdata;
891 	for (i = 0; i < AXE_TX_LIST_CNT; i++) {
892 		c = &cd->axe_tx_chain[i];
893 		c->axe_sc = sc;
894 		c->axe_idx = i;
895 		c->axe_mbuf = NULL;
896 		if (c->axe_xfer == NULL) {
897 			c->axe_xfer = usbd_alloc_xfer(sc->axe_udev);
898 			if (c->axe_xfer == NULL)
899 				return (ENOBUFS);
900 			c->axe_buf = usbd_alloc_buffer(c->axe_xfer,
901 			    sc->axe_bufsz);
902 			if (c->axe_buf == NULL) {
903 				usbd_free_xfer(c->axe_xfer);
904 				return (ENOBUFS);
905 			}
906 		}
907 	}
908 
909 	return (0);
910 }
911 
912 /*
913  * A frame has been uploaded: pass the resulting mbuf chain up to
914  * the higher level protocols.
915  */
916 void
917 axe_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
918 {
919 	struct axe_chain	*c = (struct axe_chain *)priv;
920 	struct axe_softc	*sc = c->axe_sc;
921 	struct ifnet		*ifp = GET_IFP(sc);
922 	u_char			*buf = c->axe_buf;
923 	u_int32_t		total_len;
924 	u_int16_t		pktlen = 0;
925 	struct mbuf		*m;
926 	struct axe_sframe_hdr	hdr;
927 	int			s;
928 
929 	DPRINTFN(10,("%s: %s: enter\n", sc->axe_dev.dv_xname,__func__));
930 
931 	if (sc->axe_dying)
932 		return;
933 
934 	if (!(ifp->if_flags & IFF_RUNNING))
935 		return;
936 
937 	if (status != USBD_NORMAL_COMPLETION) {
938 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
939 			return;
940 		if (usbd_ratecheck(&sc->axe_rx_notice)) {
941 			printf("%s: usb errors on rx: %s\n",
942 			    sc->axe_dev.dv_xname, usbd_errstr(status));
943 		}
944 		if (status == USBD_STALLED)
945 			usbd_clear_endpoint_stall_async(sc->axe_ep[AXE_ENDPT_RX]);
946 		goto done;
947 	}
948 
949 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
950 
951 	do {
952 		if (sc->axe_flags & AX178 || sc->axe_flags & AX772) {
953 			if (total_len < sizeof(hdr)) {
954 				ifp->if_ierrors++;
955 				goto done;
956 			}
957 
958 			buf += pktlen;
959 
960 			memcpy(&hdr, buf, sizeof(hdr));
961 			total_len -= sizeof(hdr);
962 
963 			if ((hdr.len ^ hdr.ilen) != 0xffff) {
964 				ifp->if_ierrors++;
965 				goto done;
966 			}
967 			pktlen = letoh16(hdr.len);
968 			if (pktlen > total_len) {
969 				ifp->if_ierrors++;
970 				goto done;
971 			}
972 
973 			buf += sizeof(hdr);
974 
975 			if ((pktlen % 2) != 0)
976 				pktlen++;
977 
978 			if ((total_len - pktlen) < 0)
979 				total_len = 0;
980 			else
981 				total_len -= pktlen;
982 		} else {
983 			pktlen = total_len; /* crc on the end? */
984 			total_len = 0;
985 		}
986 
987 		m = axe_newbuf();
988 		if (m == NULL) {
989 			ifp->if_ierrors++;
990 			goto done;
991 		}
992 
993 		ifp->if_ipackets++;
994 		m->m_pkthdr.rcvif = ifp;
995 		m->m_pkthdr.len = m->m_len = pktlen;
996 
997 		memcpy(mtod(m, char *), buf, pktlen);
998 
999 		/* push the packet up */
1000 		s = splnet();
1001 #if NBPFILTER > 0
1002 		if (ifp->if_bpf)
1003 			bpf_mtap(ifp->if_bpf, m, BPF_DIRECTION_IN);
1004 #endif
1005 
1006 		ether_input_mbuf(ifp, m);
1007 
1008 		splx(s);
1009 
1010 	} while (total_len > 0);
1011 
1012 done:
1013 	memset(c->axe_buf, 0, sc->axe_bufsz);
1014 
1015 	/* Setup new transfer. */
1016 	usbd_setup_xfer(xfer, sc->axe_ep[AXE_ENDPT_RX],
1017 	    c, c->axe_buf, sc->axe_bufsz,
1018 	    USBD_SHORT_XFER_OK | USBD_NO_COPY,
1019 	    USBD_NO_TIMEOUT, axe_rxeof);
1020 	usbd_transfer(xfer);
1021 
1022 	DPRINTFN(10,("%s: %s: start rx\n", sc->axe_dev.dv_xname, __func__));
1023 
1024 	return;
1025 }
1026 
1027 /*
1028  * A frame was downloaded to the chip. It's safe for us to clean up
1029  * the list buffers.
1030  */
1031 
1032 void
1033 axe_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
1034 {
1035 	struct axe_softc	*sc;
1036 	struct axe_chain	*c;
1037 	struct ifnet		*ifp;
1038 	int			s;
1039 
1040 	c = priv;
1041 	sc = c->axe_sc;
1042 	ifp = &sc->arpcom.ac_if;
1043 
1044 	if (sc->axe_dying)
1045 		return;
1046 
1047 	s = splnet();
1048 
1049 	if (status != USBD_NORMAL_COMPLETION) {
1050 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1051 			splx(s);
1052 			return;
1053 		}
1054 		ifp->if_oerrors++;
1055 		printf("axe%d: usb error on tx: %s\n", sc->axe_unit,
1056 		    usbd_errstr(status));
1057 		if (status == USBD_STALLED)
1058 			usbd_clear_endpoint_stall_async(sc->axe_ep[AXE_ENDPT_TX]);
1059 		splx(s);
1060 		return;
1061 	}
1062 
1063 	ifp->if_timer = 0;
1064 	ifp->if_flags &= ~IFF_OACTIVE;
1065 
1066 	m_freem(c->axe_mbuf);
1067 	c->axe_mbuf = NULL;
1068 
1069 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1070 		axe_start(ifp);
1071 
1072 	ifp->if_opackets++;
1073 	splx(s);
1074 	return;
1075 }
1076 
1077 void
1078 axe_tick(void *xsc)
1079 {
1080 	struct axe_softc *sc = xsc;
1081 
1082 	if (sc == NULL)
1083 		return;
1084 
1085 	DPRINTFN(0xff, ("%s: %s: enter\n", sc->axe_dev.dv_xname,
1086 			__func__));
1087 
1088 	if (sc->axe_dying)
1089 		return;
1090 
1091 	/* Perform periodic stuff in process context */
1092 	usb_add_task(sc->axe_udev, &sc->axe_tick_task);
1093 
1094 }
1095 
1096 void
1097 axe_tick_task(void *xsc)
1098 {
1099 	int			s;
1100 	struct axe_softc	*sc;
1101 	struct ifnet		*ifp;
1102 	struct mii_data		*mii;
1103 
1104 	sc = xsc;
1105 
1106 	if (sc == NULL)
1107 		return;
1108 
1109 	if (sc->axe_dying)
1110 		return;
1111 
1112 	ifp = GET_IFP(sc);
1113 	mii = GET_MII(sc);
1114 	if (mii == NULL)
1115 		return;
1116 
1117 	s = splnet();
1118 
1119 	mii_tick(mii);
1120 	if (!sc->axe_link && mii->mii_media_status & IFM_ACTIVE &&
1121 	    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1122 		DPRINTF(("%s: %s: got link\n",
1123 			 sc->axe_dev.dv_xname, __func__));
1124 		sc->axe_link++;
1125 		if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1126 			   axe_start(ifp);
1127 	}
1128 
1129 	timeout_add_sec(&sc->axe_stat_ch, 1);
1130 
1131 	splx(s);
1132 }
1133 
1134 int
1135 axe_encap(struct axe_softc *sc, struct mbuf *m, int idx)
1136 {
1137 	struct axe_chain	*c;
1138 	usbd_status		err;
1139 	struct axe_sframe_hdr	hdr;
1140 	int			length, boundary;
1141 
1142 	c = &sc->axe_cdata.axe_tx_chain[idx];
1143 
1144 	if (sc->axe_flags & AX178 || sc->axe_flags & AX772) {
1145 		boundary = (sc->axe_udev->speed == USB_SPEED_HIGH) ? 512 : 64;
1146 
1147 		hdr.len = htole16(m->m_pkthdr.len);
1148 		hdr.ilen = ~hdr.len;
1149 
1150 		memcpy(c->axe_buf, &hdr, sizeof(hdr));
1151 		length = sizeof(hdr);
1152 
1153 		m_copydata(m, 0, m->m_pkthdr.len, c->axe_buf + length);
1154 		length += m->m_pkthdr.len;
1155 
1156 		if ((length % boundary) == 0) {
1157 			hdr.len = 0x0000;
1158 			hdr.ilen = 0xffff;
1159 			memcpy(c->axe_buf + length, &hdr, sizeof(hdr));
1160 			length += sizeof(hdr);
1161 		}
1162 
1163 	} else {
1164 		m_copydata(m, 0, m->m_pkthdr.len, c->axe_buf);
1165 		length = m->m_pkthdr.len;
1166 	}
1167 
1168 	c->axe_mbuf = m;
1169 
1170 	usbd_setup_xfer(c->axe_xfer, sc->axe_ep[AXE_ENDPT_TX],
1171 	    c, c->axe_buf, length, USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
1172 	    10000, axe_txeof);
1173 
1174 	/* Transmit */
1175 	err = usbd_transfer(c->axe_xfer);
1176 	if (err != USBD_IN_PROGRESS) {
1177 		axe_stop(sc);
1178 		return(EIO);
1179 	}
1180 
1181 	sc->axe_cdata.axe_tx_cnt++;
1182 
1183 	return(0);
1184 }
1185 
1186 void
1187 axe_start(struct ifnet *ifp)
1188 {
1189 	struct axe_softc	*sc;
1190 	struct mbuf		*m_head = NULL;
1191 
1192 	sc = ifp->if_softc;
1193 
1194 	if (!sc->axe_link)
1195 		return;
1196 
1197 	if (ifp->if_flags & IFF_OACTIVE)
1198 		return;
1199 
1200 	IFQ_POLL(&ifp->if_snd, m_head);
1201 	if (m_head == NULL)
1202 		return;
1203 
1204 	if (axe_encap(sc, m_head, 0)) {
1205 		ifp->if_flags |= IFF_OACTIVE;
1206 		return;
1207 	}
1208 	IFQ_DEQUEUE(&ifp->if_snd, m_head);
1209 
1210 	/*
1211 	 * If there's a BPF listener, bounce a copy of this frame
1212 	 * to him.
1213 	 */
1214 #if NBPFILTER > 0
1215 	if (ifp->if_bpf)
1216 		bpf_mtap(ifp->if_bpf, m_head, BPF_DIRECTION_OUT);
1217 #endif
1218 
1219 	ifp->if_flags |= IFF_OACTIVE;
1220 
1221 	/*
1222 	 * Set a timeout in case the chip goes out to lunch.
1223 	 */
1224 	ifp->if_timer = 5;
1225 
1226 	return;
1227 }
1228 
1229 void
1230 axe_init(void *xsc)
1231 {
1232 	struct axe_softc	*sc = xsc;
1233 	struct ifnet		*ifp = &sc->arpcom.ac_if;
1234 	struct axe_chain	*c;
1235 	usbd_status		err;
1236 	int			rxmode;
1237 	int			i, s;
1238 
1239 	s = splnet();
1240 
1241 	/*
1242 	 * Cancel pending I/O and free all RX/TX buffers.
1243 	 */
1244 	axe_reset(sc);
1245 
1246 	/* Enable RX logic. */
1247 
1248 	/* Init RX ring. */
1249 	if (axe_rx_list_init(sc) == ENOBUFS) {
1250 		printf("axe%d: rx list init failed\n", sc->axe_unit);
1251 		splx(s);
1252 		return;
1253 	}
1254 
1255 	/* Init TX ring. */
1256 	if (axe_tx_list_init(sc) == ENOBUFS) {
1257 		printf("axe%d: tx list init failed\n", sc->axe_unit);
1258 		splx(s);
1259 		return;
1260 	}
1261 
1262 	/* Set transmitter IPG values */
1263 	if (sc->axe_flags & AX178 || sc->axe_flags & AX772)
1264 		axe_cmd(sc, AXE_178_CMD_WRITE_IPG012, sc->axe_ipgs[2],
1265 		    (sc->axe_ipgs[1] << 8) | (sc->axe_ipgs[0]), NULL);
1266 	else {
1267 		axe_cmd(sc, AXE_172_CMD_WRITE_IPG0, 0, sc->axe_ipgs[0], NULL);
1268 		axe_cmd(sc, AXE_172_CMD_WRITE_IPG1, 0, sc->axe_ipgs[1], NULL);
1269 		axe_cmd(sc, AXE_172_CMD_WRITE_IPG2, 0, sc->axe_ipgs[2], NULL);
1270 	}
1271 
1272 	/* Enable receiver, set RX mode */
1273 	rxmode = AXE_RXCMD_MULTICAST|AXE_RXCMD_ENABLE;
1274 	if (sc->axe_flags & AX178 || sc->axe_flags & AX772) {
1275 		if (sc->axe_udev->speed == USB_SPEED_HIGH) {
1276 			/* largest possible USB buffer size for AX88178 */
1277 			rxmode |= AXE_178_RXCMD_MFB;
1278 		}
1279 	} else
1280 		rxmode |= AXE_172_RXCMD_UNICAST;
1281 
1282 	/* If we want promiscuous mode, set the allframes bit. */
1283 	if (ifp->if_flags & IFF_PROMISC)
1284 		rxmode |= AXE_RXCMD_PROMISC;
1285 
1286 	if (ifp->if_flags & IFF_BROADCAST)
1287 		rxmode |= AXE_RXCMD_BROADCAST;
1288 
1289 	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
1290 
1291 	/* Load the multicast filter. */
1292 	axe_setmulti(sc);
1293 
1294 	/* Open RX and TX pipes. */
1295 	err = usbd_open_pipe(sc->axe_iface, sc->axe_ed[AXE_ENDPT_RX],
1296 	    USBD_EXCLUSIVE_USE, &sc->axe_ep[AXE_ENDPT_RX]);
1297 	if (err) {
1298 		printf("axe%d: open rx pipe failed: %s\n",
1299 		    sc->axe_unit, usbd_errstr(err));
1300 		splx(s);
1301 		return;
1302 	}
1303 
1304 	err = usbd_open_pipe(sc->axe_iface, sc->axe_ed[AXE_ENDPT_TX],
1305 	    USBD_EXCLUSIVE_USE, &sc->axe_ep[AXE_ENDPT_TX]);
1306 	if (err) {
1307 		printf("axe%d: open tx pipe failed: %s\n",
1308 		    sc->axe_unit, usbd_errstr(err));
1309 		splx(s);
1310 		return;
1311 	}
1312 
1313 	/* Start up the receive pipe. */
1314 	for (i = 0; i < AXE_RX_LIST_CNT; i++) {
1315 		c = &sc->axe_cdata.axe_rx_chain[i];
1316 		usbd_setup_xfer(c->axe_xfer, sc->axe_ep[AXE_ENDPT_RX],
1317 		    c, c->axe_buf, sc->axe_bufsz,
1318 		    USBD_SHORT_XFER_OK | USBD_NO_COPY,
1319 		    USBD_NO_TIMEOUT, axe_rxeof);
1320 		usbd_transfer(c->axe_xfer);
1321 	}
1322 
1323 	ifp->if_flags |= IFF_RUNNING;
1324 	ifp->if_flags &= ~IFF_OACTIVE;
1325 
1326 	splx(s);
1327 
1328 	timeout_add_sec(&sc->axe_stat_ch, 1);
1329 	return;
1330 }
1331 
1332 int
1333 axe_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1334 {
1335 	struct axe_softc	*sc = ifp->if_softc;
1336 	struct ifreq		*ifr = (struct ifreq *)data;
1337 	struct ifaddr		*ifa = (struct ifaddr *)data;
1338 	struct mii_data		*mii;
1339 	uWord			rxmode;
1340 	int			s, error = 0;
1341 
1342 	s = splnet();
1343 
1344 	switch(cmd) {
1345 	case SIOCSIFADDR:
1346 		ifp->if_flags |= IFF_UP;
1347 		if (!(ifp->if_flags & IFF_RUNNING))
1348 			axe_init(sc);
1349 #ifdef INET
1350 		if (ifa->ifa_addr->sa_family == AF_INET)
1351 			arp_ifinit(&sc->arpcom, ifa);
1352 #endif
1353 		break;
1354 
1355 	case SIOCSIFFLAGS:
1356 		if (ifp->if_flags & IFF_UP) {
1357 			if (ifp->if_flags & IFF_RUNNING &&
1358 			    ifp->if_flags & IFF_PROMISC &&
1359 			    !(sc->axe_if_flags & IFF_PROMISC)) {
1360 
1361 				axe_cmd(sc, AXE_CMD_RXCTL_READ, 0, 0, rxmode);
1362 				axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0,
1363 				    UGETW(rxmode) | AXE_RXCMD_PROMISC, NULL);
1364 
1365 				axe_setmulti(sc);
1366 			} else if (ifp->if_flags & IFF_RUNNING &&
1367 			    !(ifp->if_flags & IFF_PROMISC) &&
1368 			    sc->axe_if_flags & IFF_PROMISC) {
1369 				axe_cmd(sc, AXE_CMD_RXCTL_READ, 0, 0, rxmode);
1370 				axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0,
1371 				    UGETW(rxmode) & ~AXE_RXCMD_PROMISC, NULL);
1372 				axe_setmulti(sc);
1373 			} else if (!(ifp->if_flags & IFF_RUNNING))
1374 				axe_init(sc);
1375 		} else {
1376 			if (ifp->if_flags & IFF_RUNNING)
1377 				axe_stop(sc);
1378 		}
1379 		sc->axe_if_flags = ifp->if_flags;
1380 		break;
1381 
1382 	case SIOCGIFMEDIA:
1383 	case SIOCSIFMEDIA:
1384 		mii = GET_MII(sc);
1385 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, cmd);
1386 		break;
1387 
1388 	default:
1389 		error = ether_ioctl(ifp, &sc->arpcom, cmd, data);
1390 	}
1391 
1392 	if (error == ENETRESET) {
1393 		if (ifp->if_flags & IFF_RUNNING)
1394 			axe_setmulti(sc);
1395 		error = 0;
1396 	}
1397 
1398 	splx(s);
1399 	return(error);
1400 }
1401 
1402 void
1403 axe_watchdog(struct ifnet *ifp)
1404 {
1405 	struct axe_softc	*sc;
1406 	struct axe_chain	*c;
1407 	usbd_status		stat;
1408 	int			s;
1409 
1410 	sc = ifp->if_softc;
1411 
1412 	ifp->if_oerrors++;
1413 	printf("axe%d: watchdog timeout\n", sc->axe_unit);
1414 
1415 	s = splusb();
1416 	c = &sc->axe_cdata.axe_tx_chain[0];
1417 	usbd_get_xfer_status(c->axe_xfer, NULL, NULL, NULL, &stat);
1418 	axe_txeof(c->axe_xfer, c, stat);
1419 
1420 	if (!IFQ_IS_EMPTY(&ifp->if_snd))
1421 		axe_start(ifp);
1422 	splx(s);
1423 }
1424 
1425 /*
1426  * Stop the adapter and free any mbufs allocated to the
1427  * RX and TX lists.
1428  */
1429 void
1430 axe_stop(struct axe_softc *sc)
1431 {
1432 	usbd_status		err;
1433 	struct ifnet		*ifp;
1434 	int			i;
1435 
1436 	axe_reset(sc);
1437 
1438 	ifp = &sc->arpcom.ac_if;
1439 	ifp->if_timer = 0;
1440 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1441 
1442 	timeout_del(&sc->axe_stat_ch);
1443 
1444 	/* Stop transfers. */
1445 	if (sc->axe_ep[AXE_ENDPT_RX] != NULL) {
1446 		err = usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_RX]);
1447 		if (err) {
1448 			printf("axe%d: abort rx pipe failed: %s\n",
1449 			    sc->axe_unit, usbd_errstr(err));
1450 		}
1451 		err = usbd_close_pipe(sc->axe_ep[AXE_ENDPT_RX]);
1452 		if (err) {
1453 			printf("axe%d: close rx pipe failed: %s\n",
1454 			    sc->axe_unit, usbd_errstr(err));
1455 		}
1456 		sc->axe_ep[AXE_ENDPT_RX] = NULL;
1457 	}
1458 
1459 	if (sc->axe_ep[AXE_ENDPT_TX] != NULL) {
1460 		err = usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_TX]);
1461 		if (err) {
1462 			printf("axe%d: abort tx pipe failed: %s\n",
1463 			    sc->axe_unit, usbd_errstr(err));
1464 		}
1465 		err = usbd_close_pipe(sc->axe_ep[AXE_ENDPT_TX]);
1466 		if (err) {
1467 			printf("axe%d: close tx pipe failed: %s\n",
1468 			    sc->axe_unit, usbd_errstr(err));
1469 		}
1470 		sc->axe_ep[AXE_ENDPT_TX] = NULL;
1471 	}
1472 
1473 	if (sc->axe_ep[AXE_ENDPT_INTR] != NULL) {
1474 		err = usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_INTR]);
1475 		if (err) {
1476 			printf("axe%d: abort intr pipe failed: %s\n",
1477 			    sc->axe_unit, usbd_errstr(err));
1478 		}
1479 		err = usbd_close_pipe(sc->axe_ep[AXE_ENDPT_INTR]);
1480 		if (err) {
1481 			printf("axe%d: close intr pipe failed: %s\n",
1482 			    sc->axe_unit, usbd_errstr(err));
1483 		}
1484 		sc->axe_ep[AXE_ENDPT_INTR] = NULL;
1485 	}
1486 
1487 	/* Free RX resources. */
1488 	for (i = 0; i < AXE_RX_LIST_CNT; i++) {
1489 		if (sc->axe_cdata.axe_rx_chain[i].axe_mbuf != NULL) {
1490 			m_freem(sc->axe_cdata.axe_rx_chain[i].axe_mbuf);
1491 			sc->axe_cdata.axe_rx_chain[i].axe_mbuf = NULL;
1492 		}
1493 		if (sc->axe_cdata.axe_rx_chain[i].axe_xfer != NULL) {
1494 			usbd_free_xfer(sc->axe_cdata.axe_rx_chain[i].axe_xfer);
1495 			sc->axe_cdata.axe_rx_chain[i].axe_xfer = NULL;
1496 		}
1497 	}
1498 
1499 	/* Free TX resources. */
1500 	for (i = 0; i < AXE_TX_LIST_CNT; i++) {
1501 		if (sc->axe_cdata.axe_tx_chain[i].axe_mbuf != NULL) {
1502 			m_freem(sc->axe_cdata.axe_tx_chain[i].axe_mbuf);
1503 			sc->axe_cdata.axe_tx_chain[i].axe_mbuf = NULL;
1504 		}
1505 		if (sc->axe_cdata.axe_tx_chain[i].axe_xfer != NULL) {
1506 			usbd_free_xfer(sc->axe_cdata.axe_tx_chain[i].axe_xfer);
1507 			sc->axe_cdata.axe_tx_chain[i].axe_xfer = NULL;
1508 		}
1509 	}
1510 
1511 	sc->axe_link = 0;
1512 }
1513 
1514