xref: /netbsd-src/sys/dev/usb/if_axe.c (revision 7f21db1c0118155e0dd40b75182e30c589d9f63e)
1 /*	$NetBSD: if_axe.c,v 1.31 2010/01/19 22:07:43 pooka Exp $	*/
2 
3 /*
4  * Copyright (c) 1997, 1998, 1999, 2000-2003
5  *	Bill Paul <wpaul@windriver.com>.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by Bill Paul.
18  * 4. Neither the name of the author nor the names of any co-contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32  * THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 /*
36  * ASIX Electronics AX88172 USB 2.0 ethernet driver. Used in the
37  * LinkSys USB200M and various other adapters.
38  *
39  * Manuals available from:
40  * http://www.asix.com.tw/datasheet/mac/Ax88172.PDF
41  * (also http://people.freebsd.org/~wpaul/ASIX/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  * Ported to OpenBSD 3/28/2004 by Greg Taleck <taleck@oz.net>
72  * with bits and pieces from the aue and url drivers.
73  */
74 
75 #include <sys/cdefs.h>
76 __KERNEL_RCSID(0, "$NetBSD: if_axe.c,v 1.31 2010/01/19 22:07:43 pooka Exp $");
77 
78 #if defined(__NetBSD__)
79 #include "opt_inet.h"
80 #include "rnd.h"
81 #endif
82 
83 
84 #include <sys/param.h>
85 #include <sys/systm.h>
86 #include <sys/sockio.h>
87 #include <sys/mutex.h>
88 #include <sys/mbuf.h>
89 #include <sys/kernel.h>
90 #if defined(__OpenBSD__)
91 #include <sys/proc.h>
92 #endif
93 #include <sys/socket.h>
94 
95 #include <sys/device.h>
96 #if NRND > 0
97 #include <sys/rnd.h>
98 #endif
99 
100 #include <net/if.h>
101 #if defined(__NetBSD__)
102 #include <net/if_arp.h>
103 #endif
104 #include <net/if_dl.h>
105 #include <net/if_media.h>
106 
107 #include <net/bpf.h>
108 
109 #if defined(__NetBSD__)
110 #include <net/if_ether.h>
111 #ifdef INET
112 #include <netinet/in.h>
113 #include <netinet/if_inarp.h>
114 #endif
115 #endif /* defined(__NetBSD__) */
116 
117 #if defined(__OpenBSD__)
118 #ifdef INET
119 #include <netinet/in.h>
120 #include <netinet/in_systm.h>
121 #include <netinet/in_var.h>
122 #include <netinet/ip.h>
123 #include <netinet/if_ether.h>
124 #endif
125 #endif /* defined(__OpenBSD__) */
126 
127 
128 #include <dev/mii/mii.h>
129 #include <dev/mii/miivar.h>
130 
131 #include <dev/usb/usb.h>
132 #include <dev/usb/usbdi.h>
133 #include <dev/usb/usbdi_util.h>
134 #include <dev/usb/usbdevs.h>
135 
136 #include <dev/usb/if_axereg.h>
137 
138 #ifdef AXE_DEBUG
139 #define DPRINTF(x)	do { if (axedebug) logprintf x; } while (0)
140 #define DPRINTFN(n,x)	do { if (axedebug >= (n)) logprintf x; } while (0)
141 int	axedebug = 0;
142 #else
143 #define DPRINTF(x)
144 #define DPRINTFN(n,x)
145 #endif
146 
147 /*
148  * Various supported device vendors/products.
149  */
150 Static const struct axe_type axe_devs[] = {
151 	{ { USB_VENDOR_ASIX,		USB_PRODUCT_ASIX_AX88172}, 0 },
152 	{ { USB_VENDOR_COREGA,		USB_PRODUCT_COREGA_FETHER_USB2_TX }, 0},
153 	{ { USB_VENDOR_DLINK,		USB_PRODUCT_DLINK_DUBE100}, 0 },
154 	{ { USB_VENDOR_LINKSYS2,	USB_PRODUCT_LINKSYS2_USB200M}, 0 },
155 	{ { USB_VENDOR_MELCO,		USB_PRODUCT_MELCO_LUAU2KTX}, 0 },
156 	{ { USB_VENDOR_NETGEAR,		USB_PRODUCT_NETGEAR_FA120}, 0 },
157 	{ { USB_VENDOR_SITECOM,		USB_PRODUCT_SITECOM_LN029}, 0 },
158 	{ { USB_VENDOR_SYSTEMTALKS,	USB_PRODUCT_SYSTEMTALKS_SGCX2UL}, 0 },
159 };
160 #define axe_lookup(v, p) ((const struct axe_type *)usb_lookup(axe_devs, v, p))
161 
162 int axe_match(device_t, cfdata_t, void *);
163 void axe_attach(device_t, device_t, void *);
164 int axe_detach(device_t, int);
165 int axe_activate(device_t, enum devact);
166 extern struct cfdriver axe_cd;
167 CFATTACH_DECL_NEW(axe, sizeof(struct axe_softc), axe_match, axe_attach,
168     axe_detach, axe_activate);
169 
170 Static int axe_tx_list_init(struct axe_softc *);
171 Static int axe_rx_list_init(struct axe_softc *);
172 Static int axe_newbuf(struct axe_softc *, struct axe_chain *, struct mbuf *);
173 Static int axe_encap(struct axe_softc *, struct mbuf *, int);
174 Static void axe_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
175 Static void axe_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
176 Static void axe_tick(void *);
177 Static void axe_tick_task(void *);
178 #if 0
179 Static void axe_rxstart(struct ifnet *);
180 #endif
181 Static void axe_start(struct ifnet *);
182 Static int axe_ioctl(struct ifnet *, u_long, void *);
183 Static void axe_init(void *);
184 Static void axe_stop(struct axe_softc *);
185 Static void axe_watchdog(struct ifnet *);
186 Static int axe_miibus_readreg(device_t, int, int);
187 Static void axe_miibus_writereg(device_t, int, int, int);
188 Static void axe_miibus_statchg(device_t);
189 Static int axe_cmd(struct axe_softc *, int, int, int, void *);
190 Static void axe_reset(struct axe_softc *sc);
191 
192 Static void axe_setmulti(struct axe_softc *);
193 Static void axe_lock_mii(struct axe_softc *sc);
194 Static void axe_unlock_mii(struct axe_softc *sc);
195 
196 /* Get exclusive access to the MII registers */
197 Static void
198 axe_lock_mii(struct axe_softc *sc)
199 {
200 	sc->axe_refcnt++;
201 	mutex_enter(&sc->axe_mii_lock);
202 }
203 
204 Static void
205 axe_unlock_mii(struct axe_softc *sc)
206 {
207 	mutex_exit(&sc->axe_mii_lock);
208 	if (--sc->axe_refcnt < 0)
209 		usb_detach_wakeup((sc->axe_dev));
210 }
211 
212 Static int
213 axe_cmd(struct axe_softc *sc, int cmd, int index, int val, void *buf)
214 {
215 	usb_device_request_t	req;
216 	usbd_status		err;
217 
218 	KASSERT(mutex_owned(&sc->axe_mii_lock));
219 
220 	if (sc->axe_dying)
221 		return(0);
222 
223 	if (AXE_CMD_DIR(cmd))
224 		req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
225 	else
226 		req.bmRequestType = UT_READ_VENDOR_DEVICE;
227 	req.bRequest = AXE_CMD_CMD(cmd);
228 	USETW(req.wValue, val);
229 	USETW(req.wIndex, index);
230 	USETW(req.wLength, AXE_CMD_LEN(cmd));
231 
232 	err = usbd_do_request(sc->axe_udev, &req, buf);
233 
234 	if (err)
235 		return(-1);
236 
237 	return(0);
238 }
239 
240 Static int
241 axe_miibus_readreg(device_t dev, int phy, int reg)
242 {
243 	struct axe_softc *sc = device_private(dev);
244 	usbd_status		err;
245 	u_int16_t		val;
246 
247 	if (sc->axe_dying) {
248 		DPRINTF(("axe: dying\n"));
249 		return(0);
250 	}
251 
252 #ifdef notdef
253 	/*
254 	 * The chip tells us the MII address of any supported
255 	 * PHYs attached to the chip, so only read from those.
256 	 */
257 
258 	if (sc->axe_phyaddrs[0] != AXE_NOPHY && phy != sc->axe_phyaddrs[0])
259 		return (0);
260 
261 	if (sc->axe_phyaddrs[1] != AXE_NOPHY && phy != sc->axe_phyaddrs[1])
262 		return (0);
263 #endif
264 	if (sc->axe_phyaddrs[0] != 0xFF && sc->axe_phyaddrs[0] != phy)
265 		return (0);
266 
267 	val = 0;
268 
269 	axe_lock_mii(sc);
270 	axe_cmd(sc, AXE_CMD_MII_OPMODE_SW, 0, 0, NULL);
271 	err = axe_cmd(sc, AXE_CMD_MII_READ_REG, reg, phy, (void *)&val);
272 	axe_cmd(sc, AXE_CMD_MII_OPMODE_HW, 0, 0, NULL);
273 	axe_unlock_mii(sc);
274 
275 	if (err) {
276 		aprint_error_dev(sc->axe_dev, "read PHY failed\n");
277 		return(-1);
278 	}
279 
280 	if (val)
281 		sc->axe_phyaddrs[0] = phy;
282 
283 	return (le16toh(val));
284 }
285 
286 Static void
287 axe_miibus_writereg(device_t dev, int phy, int reg, int aval)
288 {
289 	struct axe_softc *sc = device_private(dev);
290 	usbd_status		err;
291 	u_int16_t		val;
292 
293 	if (sc->axe_dying)
294 		return;
295 
296 	val = htole16(aval);
297 	axe_lock_mii(sc);
298 	axe_cmd(sc, AXE_CMD_MII_OPMODE_SW, 0, 0, NULL);
299 	err = axe_cmd(sc, AXE_CMD_MII_WRITE_REG, reg, phy, (void *)&val);
300 	axe_cmd(sc, AXE_CMD_MII_OPMODE_HW, 0, 0, NULL);
301 	axe_unlock_mii(sc);
302 
303 	if (err) {
304 		aprint_error_dev(sc->axe_dev, "write PHY failed\n");
305 		return;
306 	}
307 }
308 
309 Static void
310 axe_miibus_statchg(device_t dev)
311 {
312 	struct axe_softc *sc = device_private(dev);
313 	struct mii_data		*mii = GET_MII(sc);
314 	int val, err;
315 
316 	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
317 		val = AXE_MEDIA_FULL_DUPLEX;
318 	else
319 		val = 0;
320 	DPRINTF(("axe_miibus_statchg: val=0x%x\n", val));
321 	axe_lock_mii(sc);
322 	err = axe_cmd(sc, AXE_CMD_WRITE_MEDIA, 0, val, NULL);
323 	axe_unlock_mii(sc);
324 	if (err) {
325 		aprint_error_dev(sc->axe_dev, "media change failed\n");
326 		return;
327 	}
328 }
329 
330 Static void
331 axe_setmulti(struct axe_softc *sc)
332 {
333 	struct ifnet		*ifp;
334 	struct ether_multi *enm;
335 	struct ether_multistep step;
336 	u_int32_t		h = 0;
337 	u_int16_t		rxmode;
338 	u_int8_t		hashtbl[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
339 
340 	if (sc->axe_dying)
341 		return;
342 
343 	ifp = GET_IFP(sc);
344 
345 	axe_lock_mii(sc);
346 	axe_cmd(sc, AXE_CMD_RXCTL_READ, 0, 0, (void *)&rxmode);
347 	rxmode = le16toh(rxmode);
348 
349 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
350 	allmulti:
351 		rxmode |= AXE_RXCMD_ALLMULTI;
352 		axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
353 		return;
354 	} else
355 		rxmode &= ~AXE_RXCMD_ALLMULTI;
356 
357 	/* now program new ones */
358 #if defined(__NetBSD__)
359 	ETHER_FIRST_MULTI(step, &sc->axe_ec, enm);
360 #else
361 	ETHER_FIRST_MULTI(step, &sc->arpcom, enm);
362 #endif
363 	while (enm != NULL) {
364 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
365 			   ETHER_ADDR_LEN) != 0)
366 			goto allmulti;
367 
368 		h = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN) >> 26;
369 		hashtbl[h / 8] |= 1 << (h % 8);
370 		ETHER_NEXT_MULTI(step, enm);
371 	}
372 
373 	ifp->if_flags &= ~IFF_ALLMULTI;
374 	axe_cmd(sc, AXE_CMD_WRITE_MCAST, 0, 0, (void *)&hashtbl);
375 	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
376 	axe_unlock_mii(sc);
377 	return;
378 }
379 
380 Static void
381 axe_reset(struct axe_softc *sc)
382 {
383 	if (sc->axe_dying)
384 		return;
385 	/* XXX What to reset? */
386 
387 	/* Wait a little while for the chip to get its brains in order. */
388 	DELAY(1000);
389 	return;
390 }
391 
392 /*
393  * Probe for a AX88172 chip.
394  */
395 int
396 axe_match(device_t parent, cfdata_t match, void *aux)
397 {
398 	struct usb_attach_arg *uaa = aux;
399 
400 	return (axe_lookup(uaa->vendor, uaa->product) != NULL ?
401 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
402 }
403 
404 /*
405  * Attach the interface. Allocate softc structures, do ifmedia
406  * setup and ethernet/BPF attach.
407  */
408 void
409 axe_attach(device_t parent, device_t self, void *aux)
410 {
411 	struct axe_softc *sc = device_private(self);
412 	struct usb_attach_arg *uaa = aux;
413 	usbd_device_handle dev = uaa->device;
414 	usbd_status err;
415 	usb_interface_descriptor_t *id;
416 	usb_endpoint_descriptor_t *ed;
417 	struct mii_data	*mii;
418 	u_char eaddr[ETHER_ADDR_LEN];
419 	char *devinfop;
420 	const char *devname = device_xname(self);
421 	struct ifnet *ifp;
422 	int i, s;
423 
424 	sc->axe_dev = self;
425 
426 	aprint_naive("\n");
427 	aprint_normal("\n");
428 
429 	devinfop = usbd_devinfo_alloc(dev, 0);
430 	aprint_normal_dev(self, "%s\n", devinfop);
431 	usbd_devinfo_free(devinfop);
432 
433 	err = usbd_set_config_no(dev, AXE_CONFIG_NO, 1);
434 	if (err) {
435 		aprint_error_dev(self, "getting interface handle failed\n");
436 		return;
437 	}
438 
439 	usb_init_task(&sc->axe_tick_task, axe_tick_task, sc);
440 	mutex_init(&sc->axe_mii_lock, MUTEX_DEFAULT, IPL_NONE);
441 	usb_init_task(&sc->axe_stop_task, (void (*)(void *))axe_stop, sc);
442 
443 	err = usbd_device2interface_handle(dev, AXE_IFACE_IDX, &sc->axe_iface);
444 	if (err) {
445 		aprint_error_dev(self, "getting interface handle failed\n");
446 		return;
447 	}
448 
449 	sc->axe_udev = dev;
450 	sc->axe_product = uaa->product;
451 	sc->axe_vendor = uaa->vendor;
452 
453 	id = usbd_get_interface_descriptor(sc->axe_iface);
454 
455 	/* Find endpoints. */
456 	for (i = 0; i < id->bNumEndpoints; i++) {
457 		ed = usbd_interface2endpoint_descriptor(sc->axe_iface, i);
458 		if (!ed) {
459 			aprint_error_dev(self, "couldn't get ep %d\n", i);
460 			return;
461 		}
462 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
463 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
464 			sc->axe_ed[AXE_ENDPT_RX] = ed->bEndpointAddress;
465 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
466 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
467 			sc->axe_ed[AXE_ENDPT_TX] = ed->bEndpointAddress;
468 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
469 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
470 			sc->axe_ed[AXE_ENDPT_INTR] = ed->bEndpointAddress;
471 		}
472 	}
473 
474 	s = splnet();
475 
476 	/*
477 	 * Get station address.
478 	 */
479 	axe_lock_mii(sc);
480 	axe_cmd(sc, AXE_CMD_READ_NODEID, 0, 0, &eaddr);
481 
482 	/*
483 	 * Load IPG values and PHY indexes.
484 	 */
485 	axe_cmd(sc, AXE_CMD_READ_IPG012, 0, 0, (void *)&sc->axe_ipgs);
486 	axe_cmd(sc, AXE_CMD_READ_PHYID, 0, 0, (void *)&sc->axe_phyaddrs);
487 	axe_unlock_mii(sc);
488 
489 	/*
490 	 * Work around broken adapters that appear to lie about
491 	 * their PHY addresses.
492 	 */
493 	sc->axe_phyaddrs[0] = sc->axe_phyaddrs[1] = 0xFF;
494 
495 	/*
496 	 * An ASIX chip was detected. Inform the world.
497 	 */
498 	aprint_normal_dev(self, "Ethernet address %s\n",
499 	    ether_sprintf(eaddr));
500 
501 	/* Initialize interface info.*/
502 	ifp = GET_IFP(sc);
503 	ifp->if_softc = sc;
504 	strncpy(ifp->if_xname, devname, IFNAMSIZ);
505 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
506 	ifp->if_ioctl = axe_ioctl;
507 	ifp->if_start = axe_start;
508 
509 	ifp->if_watchdog = axe_watchdog;
510 
511 /*	ifp->if_baudrate = 10000000; */
512 /*	ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;*/
513 
514 	IFQ_SET_READY(&ifp->if_snd);
515 
516 	/* Initialize MII/media info. */
517 	mii = &sc->axe_mii;
518 	mii->mii_ifp = ifp;
519 	mii->mii_readreg = axe_miibus_readreg;
520 	mii->mii_writereg = axe_miibus_writereg;
521 	mii->mii_statchg = axe_miibus_statchg;
522 	mii->mii_flags = MIIF_AUTOTSLEEP;
523 
524 	sc->axe_ec.ec_mii = mii;
525 	ifmedia_init(&mii->mii_media, 0, ether_mediachange, ether_mediastatus);
526 	mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0);
527 
528 	if (LIST_EMPTY(&mii->mii_phys)) {
529 		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
530 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
531 	} else
532 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
533 
534 	/* Attach the interface. */
535 	if_attach(ifp);
536 	ether_ifattach(ifp, eaddr);
537 #if NRND > 0
538 	rnd_attach_source(&sc->rnd_source, device_xname(sc->axe_dev),
539 	    RND_TYPE_NET, 0);
540 #endif
541 
542 	callout_init(&(sc->axe_stat_ch), 0);
543 
544 	sc->axe_attached = 1;
545 	splx(s);
546 
547 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->axe_udev, sc->axe_dev);
548 
549 	return;
550 }
551 
552 int
553 axe_detach(device_t self, int flags)
554 {
555 	struct axe_softc *sc = device_private(self);
556 	int			s;
557 	struct ifnet		*ifp = GET_IFP(sc);
558 
559 	DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->axe_dev), __func__));
560 
561 	/* Detached before attached finished, so just bail out. */
562 	if (!sc->axe_attached)
563 		return (0);
564 
565 	callout_stop(&(sc->axe_stat_ch));
566 
567 	sc->axe_dying = 1;
568 
569 	ether_ifdetach(ifp);
570 
571 	if (sc->axe_ep[AXE_ENDPT_TX] != NULL)
572 		usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_TX]);
573 	if (sc->axe_ep[AXE_ENDPT_RX] != NULL)
574 		usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_RX]);
575 	if (sc->axe_ep[AXE_ENDPT_INTR] != NULL)
576 		usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_INTR]);
577 
578 	/*
579 	 * Remove any pending tasks.  They cannot be executing because they run
580 	 * in the same thread as detach.
581 	 */
582 	usb_rem_task(sc->axe_udev, &sc->axe_tick_task);
583 	usb_rem_task(sc->axe_udev, &sc->axe_stop_task);
584 
585 	s = splusb();
586 
587 	if (--sc->axe_refcnt >= 0) {
588 		/* Wait for processes to go away */
589 		usb_detach_wait((sc->axe_dev));
590 	}
591 
592 	if (ifp->if_flags & IFF_RUNNING)
593 		axe_stop(sc);
594 
595 #if defined(__NetBSD__)
596 #if NRND > 0
597 	rnd_detach_source(&sc->rnd_source);
598 #endif
599 #endif /* __NetBSD__ */
600 	mii_detach(&sc->axe_mii, MII_PHY_ANY, MII_OFFSET_ANY);
601 	ifmedia_delete_instance(&sc->axe_mii.mii_media, IFM_INST_ANY);
602 	ether_ifdetach(ifp);
603 	if_detach(ifp);
604 
605 #ifdef DIAGNOSTIC
606 	if (sc->axe_ep[AXE_ENDPT_TX] != NULL ||
607 	    sc->axe_ep[AXE_ENDPT_RX] != NULL ||
608 	    sc->axe_ep[AXE_ENDPT_INTR] != NULL)
609 		aprint_debug_dev(self, "detach has active endpoints\n");
610 #endif
611 
612 	sc->axe_attached = 0;
613 
614 	if (--sc->axe_refcnt >= 0) {
615 		/* Wait for processes to go away. */
616 		usb_detach_wait((sc->axe_dev));
617 	}
618 	splx(s);
619 
620 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->axe_udev, sc->axe_dev);
621 
622 	return (0);
623 }
624 
625 int
626 axe_activate(device_t self, enum devact act)
627 {
628 	struct axe_softc *sc = device_private(self);
629 
630 	DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->axe_dev), __func__));
631 
632 	switch (act) {
633 	case DVACT_DEACTIVATE:
634 		if_deactivate(&sc->axe_ec.ec_if);
635 		sc->axe_dying = 1;
636 		return 0;
637 	default:
638 		return EOPNOTSUPP;
639 	}
640 }
641 
642 /*
643  * Initialize an RX descriptor and attach an MBUF cluster.
644  */
645 Static int
646 axe_newbuf(struct axe_softc *sc, struct axe_chain *c, struct mbuf *m)
647 {
648 	struct mbuf		*m_new = NULL;
649 
650 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->axe_dev),__func__));
651 
652 	if (m == NULL) {
653 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
654 		if (m_new == NULL) {
655 			aprint_error_dev(sc->axe_dev, "no memory for rx list "
656 			    "-- packet dropped!\n");
657 			return (ENOBUFS);
658 		}
659 
660 		MCLGET(m_new, M_DONTWAIT);
661 		if (!(m_new->m_flags & M_EXT)) {
662 			aprint_error_dev(sc->axe_dev, "no memory for rx list "
663 			    "-- packet dropped!\n");
664 			m_freem(m_new);
665 			return (ENOBUFS);
666 		}
667 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
668 	} else {
669 		m_new = m;
670 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
671 		m_new->m_data = m_new->m_ext.ext_buf;
672 	}
673 
674 	m_adj(m_new, ETHER_ALIGN);
675 	c->axe_mbuf = m_new;
676 
677 	return (0);
678 }
679 
680 Static int
681 axe_rx_list_init(struct axe_softc *sc)
682 {
683 	struct axe_cdata *cd;
684 	struct axe_chain *c;
685 	int i;
686 
687 	DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->axe_dev), __func__));
688 
689 	cd = &sc->axe_cdata;
690 	for (i = 0; i < AXE_RX_LIST_CNT; i++) {
691 		c = &cd->axe_rx_chain[i];
692 		c->axe_sc = sc;
693 		c->axe_idx = i;
694 		if (axe_newbuf(sc, c, NULL) == ENOBUFS)
695 			return (ENOBUFS);
696 		if (c->axe_xfer == NULL) {
697 			c->axe_xfer = usbd_alloc_xfer(sc->axe_udev);
698 			if (c->axe_xfer == NULL)
699 				return (ENOBUFS);
700 			c->axe_buf = usbd_alloc_buffer(c->axe_xfer, AXE_BUFSZ);
701 			if (c->axe_buf == NULL) {
702 				usbd_free_xfer(c->axe_xfer);
703 				return (ENOBUFS);
704 			}
705 		}
706 	}
707 
708 	return (0);
709 }
710 
711 Static int
712 axe_tx_list_init(struct axe_softc *sc)
713 {
714 	struct axe_cdata *cd;
715 	struct axe_chain *c;
716 	int i;
717 
718 	DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->axe_dev), __func__));
719 
720 	cd = &sc->axe_cdata;
721 	for (i = 0; i < AXE_TX_LIST_CNT; i++) {
722 		c = &cd->axe_tx_chain[i];
723 		c->axe_sc = sc;
724 		c->axe_idx = i;
725 		c->axe_mbuf = NULL;
726 		if (c->axe_xfer == NULL) {
727 			c->axe_xfer = usbd_alloc_xfer(sc->axe_udev);
728 			if (c->axe_xfer == NULL)
729 				return (ENOBUFS);
730 			c->axe_buf = usbd_alloc_buffer(c->axe_xfer, AXE_BUFSZ);
731 			if (c->axe_buf == NULL) {
732 				usbd_free_xfer(c->axe_xfer);
733 				return (ENOBUFS);
734 			}
735 		}
736 	}
737 
738 	return (0);
739 }
740 
741 #if 0
742 Static void
743 axe_rxstart(struct ifnet *ifp)
744 {
745 	struct axe_softc	*sc;
746 	struct axe_chain	*c;
747 
748 	sc = ifp->if_softc;
749 	axe_lock_mii(sc);
750 	c = &sc->axe_cdata.axe_rx_chain[sc->axe_cdata.axe_rx_prod];
751 
752 	if (axe_newbuf(sc, c, NULL) == ENOBUFS) {
753 		ifp->if_ierrors++;
754 		axe_unlock_mii(sc);
755 		return;
756 	}
757 
758 	/* Setup new transfer. */
759 	usbd_setup_xfer(c->axe_xfer, sc->axe_ep[AXE_ENDPT_RX],
760 	    c, mtod(c->axe_mbuf, char *), AXE_BUFSZ, USBD_SHORT_XFER_OK,
761 	    USBD_NO_TIMEOUT, axe_rxeof);
762 	usbd_transfer(c->axe_xfer);
763 	axe_unlock_mii(sc);
764 
765 	return;
766 }
767 #endif
768 
769 /*
770  * A frame has been uploaded: pass the resulting mbuf chain up to
771  * the higher level protocols.
772  */
773 Static void
774 axe_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
775 {
776 	struct axe_softc	*sc;
777 	struct axe_chain	*c;
778 	struct ifnet		*ifp;
779 	struct mbuf		*m;
780 	u_int32_t		total_len;
781 	int			s;
782 
783 	c = priv;
784 	sc = c->axe_sc;
785 	ifp = GET_IFP(sc);
786 
787 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->axe_dev),__func__));
788 
789 	if (sc->axe_dying)
790 		return;
791 
792 	if (!(ifp->if_flags & IFF_RUNNING))
793 		return;
794 
795 	if (status != USBD_NORMAL_COMPLETION) {
796 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
797 			return;
798 		if (usbd_ratecheck(&sc->axe_rx_notice)) {
799 			printf("%s: usb errors on rx: %s\n",
800 			    device_xname(sc->axe_dev), usbd_errstr(status));
801 		}
802 		if (status == USBD_STALLED)
803 			usbd_clear_endpoint_stall_async(sc->axe_ep[AXE_ENDPT_RX]);
804 		goto done;
805 	}
806 
807 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
808 
809 	m = c->axe_mbuf;
810 
811 	if (total_len <= sizeof(struct ether_header)) {
812 		ifp->if_ierrors++;
813 		goto done;
814 	}
815 
816 	ifp->if_ipackets++;
817 	m->m_pkthdr.rcvif = ifp;
818 	m->m_pkthdr.len = m->m_len = total_len;
819 
820 
821 	memcpy(mtod(c->axe_mbuf, char *), c->axe_buf, total_len);
822 
823 	/* No errors; receive the packet. */
824 	total_len -= ETHER_CRC_LEN + 4;
825 
826 	s = splnet();
827 
828 	/* XXX ugly */
829 	if (axe_newbuf(sc, c, NULL) == ENOBUFS) {
830 		ifp->if_ierrors++;
831 		goto done1;
832 	}
833 
834 	if (ifp->if_bpf)
835 		bpf_ops->bpf_mtap(ifp->if_bpf, m);
836 
837 	DPRINTFN(10,("%s: %s: deliver %d\n", USBDEVNAME(sc->axe_dev),
838 		    __func__, m->m_len));
839 	(*(ifp)->if_input)((ifp), (m));
840  done1:
841 	splx(s);
842 
843  done:
844 
845 	/* Setup new transfer. */
846 	usbd_setup_xfer(xfer, sc->axe_ep[AXE_ENDPT_RX],
847 	    c, c->axe_buf, AXE_BUFSZ,
848 	    USBD_SHORT_XFER_OK | USBD_NO_COPY,
849 	    USBD_NO_TIMEOUT, axe_rxeof);
850 	usbd_transfer(xfer);
851 
852 	DPRINTFN(10,("%s: %s: start rx\n", USBDEVNAME(sc->axe_dev),
853 		    __func__));
854 	return;
855 }
856 
857 /*
858  * A frame was downloaded to the chip. It's safe for us to clean up
859  * the list buffers.
860  */
861 
862 Static void
863 axe_txeof(usbd_xfer_handle xfer, usbd_private_handle priv,
864     usbd_status status)
865 {
866 	struct axe_softc	*sc;
867 	struct axe_chain	*c;
868 	struct ifnet		*ifp;
869 	int			s;
870 
871 	c = priv;
872 	sc = c->axe_sc;
873 	ifp = GET_IFP(sc);
874 
875 	if (sc->axe_dying)
876 		return;
877 
878 	s = splnet();
879 
880 	if (status != USBD_NORMAL_COMPLETION) {
881 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
882 			splx(s);
883 			return;
884 		}
885 		ifp->if_oerrors++;
886 		printf("%s: usb error on tx: %s\n", device_xname(sc->axe_dev),
887 		    usbd_errstr(status));
888 		if (status == USBD_STALLED)
889 			usbd_clear_endpoint_stall_async(sc->axe_ep[AXE_ENDPT_TX]);
890 		splx(s);
891 		return;
892 	}
893 
894 	ifp->if_timer = 0;
895 	ifp->if_flags &= ~IFF_OACTIVE;
896 
897 	m_freem(c->axe_mbuf);
898 	c->axe_mbuf = NULL;
899 
900 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
901 		axe_start(ifp);
902 
903 	ifp->if_opackets++;
904 	splx(s);
905 	return;
906 }
907 
908 Static void
909 axe_tick(void *xsc)
910 {
911 	struct axe_softc *sc = xsc;
912 
913 	if (sc == NULL)
914 		return;
915 
916 	DPRINTFN(0xff, ("%s: %s: enter\n", USBDEVNAME(sc->axe_dev),
917 			__func__));
918 
919 	if (sc->axe_dying)
920 		return;
921 
922 	/* Perform periodic stuff in process context */
923 	usb_add_task(sc->axe_udev, &sc->axe_tick_task, USB_TASKQ_DRIVER);
924 
925 }
926 
927 Static void
928 axe_tick_task(void *xsc)
929 {
930 	int			s;
931 	struct axe_softc	*sc;
932 	struct ifnet		*ifp;
933 	struct mii_data		*mii;
934 
935 	sc = xsc;
936 
937 	if (sc == NULL)
938 		return;
939 
940 	if (sc->axe_dying)
941 		return;
942 
943 	ifp = GET_IFP(sc);
944 	mii = GET_MII(sc);
945 	if (mii == NULL)
946 		return;
947 
948 	s = splnet();
949 
950 	mii_tick(mii);
951 
952 	callout_reset(&(sc->axe_stat_ch), (hz), (axe_tick), (sc));
953 
954 	splx(s);
955 }
956 
957 Static int
958 axe_encap(struct axe_softc *sc, struct mbuf *m, int idx)
959 {
960 	struct axe_chain	*c;
961 	usbd_status		err;
962 
963 	c = &sc->axe_cdata.axe_tx_chain[idx];
964 
965 	/*
966 	 * Copy the mbuf data into a contiguous buffer, leaving two
967 	 * bytes at the beginning to hold the frame length.
968 	 */
969 	m_copydata(m, 0, m->m_pkthdr.len, c->axe_buf);
970 	c->axe_mbuf = m;
971 
972 	usbd_setup_xfer(c->axe_xfer, sc->axe_ep[AXE_ENDPT_TX],
973 	    c, c->axe_buf, m->m_pkthdr.len, USBD_FORCE_SHORT_XFER, 10000,
974 	    axe_txeof);
975 
976 	/* Transmit */
977 	err = usbd_transfer(c->axe_xfer);
978 	if (err != USBD_IN_PROGRESS) {
979 		axe_stop(sc);
980 		return(EIO);
981 	}
982 
983 	sc->axe_cdata.axe_tx_cnt++;
984 
985 	return(0);
986 }
987 
988 Static void
989 axe_start(struct ifnet *ifp)
990 {
991 	struct axe_softc	*sc;
992 	struct mbuf		*m_head = NULL;
993 
994 	sc = ifp->if_softc;
995 
996 	if ((ifp->if_flags & (IFF_OACTIVE|IFF_RUNNING)) != IFF_RUNNING)
997 		return;
998 
999 	IF_DEQUEUE(&ifp->if_snd, m_head);
1000 	if (m_head == NULL) {
1001 		return;
1002 	}
1003 
1004 	if (axe_encap(sc, m_head, 0)) {
1005 		IF_PREPEND(&ifp->if_snd, m_head);
1006 		ifp->if_flags |= IFF_OACTIVE;
1007 		return;
1008 	}
1009 
1010 	/*
1011 	 * If there's a BPF listener, bounce a copy of this frame
1012 	 * to him.
1013 	 */
1014 	 if (ifp->if_bpf)
1015 	 	bpf_ops->bpf_mtap(ifp->if_bpf, m_head);
1016 
1017 	ifp->if_flags |= IFF_OACTIVE;
1018 
1019 	/*
1020 	 * Set a timeout in case the chip goes out to lunch.
1021 	 */
1022 	ifp->if_timer = 5;
1023 
1024 	return;
1025 }
1026 
1027 Static void
1028 axe_init(void *xsc)
1029 {
1030 	struct axe_softc	*sc = xsc;
1031 	struct ifnet		*ifp = GET_IFP(sc);
1032 	struct axe_chain	*c;
1033 	usbd_status		err;
1034 	int			rxmode;
1035 	int			i, s;
1036 
1037 	if (ifp->if_flags & IFF_RUNNING)
1038 		return;
1039 
1040 	s = splnet();
1041 
1042 	/*
1043 	 * Cancel pending I/O and free all RX/TX buffers.
1044 	 */
1045 	axe_reset(sc);
1046 
1047 	/* Enable RX logic. */
1048 
1049 	/* Init RX ring. */
1050 	if (axe_rx_list_init(sc) == ENOBUFS) {
1051 		printf("%s: rx list init failed\n", device_xname(sc->axe_dev));
1052 		splx(s);
1053 		return;
1054 	}
1055 
1056 	/* Init TX ring. */
1057 	if (axe_tx_list_init(sc) == ENOBUFS) {
1058 		printf("%s: tx list init failed\n", device_xname(sc->axe_dev));
1059 		splx(s);
1060 		return;
1061 	}
1062 
1063 	/* Set transmitter IPG values */
1064 	axe_lock_mii(sc);
1065 	axe_cmd(sc, AXE_CMD_WRITE_IPG0, 0, sc->axe_ipgs[0], NULL);
1066 	axe_cmd(sc, AXE_CMD_WRITE_IPG1, 0, sc->axe_ipgs[1], NULL);
1067 	axe_cmd(sc, AXE_CMD_WRITE_IPG2, 0, sc->axe_ipgs[2], NULL);
1068 
1069 	/* Enable receiver, set RX mode */
1070 	rxmode = AXE_RXCMD_UNICAST|AXE_RXCMD_MULTICAST|AXE_RXCMD_ENABLE;
1071 
1072 	/* If we want promiscuous mode, set the allframes bit. */
1073 	if (ifp->if_flags & IFF_PROMISC)
1074 		rxmode |= AXE_RXCMD_PROMISC;
1075 
1076 	if (ifp->if_flags & IFF_BROADCAST)
1077 		rxmode |= AXE_RXCMD_BROADCAST;
1078 
1079 	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
1080 	axe_unlock_mii(sc);
1081 
1082 	/* Load the multicast filter. */
1083 	axe_setmulti(sc);
1084 
1085 	/* Open RX and TX pipes. */
1086 	err = usbd_open_pipe(sc->axe_iface, sc->axe_ed[AXE_ENDPT_RX],
1087 	    USBD_EXCLUSIVE_USE, &sc->axe_ep[AXE_ENDPT_RX]);
1088 	if (err) {
1089 		printf("%s: open rx pipe failed: %s\n",
1090 		    device_xname(sc->axe_dev), usbd_errstr(err));
1091 		splx(s);
1092 		return;
1093 	}
1094 
1095 	err = usbd_open_pipe(sc->axe_iface, sc->axe_ed[AXE_ENDPT_TX],
1096 	    USBD_EXCLUSIVE_USE, &sc->axe_ep[AXE_ENDPT_TX]);
1097 	if (err) {
1098 		printf("%s: open tx pipe failed: %s\n",
1099 		    device_xname(sc->axe_dev), usbd_errstr(err));
1100 		splx(s);
1101 		return;
1102 	}
1103 
1104 	/* Start up the receive pipe. */
1105 	for (i = 0; i < AXE_RX_LIST_CNT; i++) {
1106 		c = &sc->axe_cdata.axe_rx_chain[i];
1107 		usbd_setup_xfer(c->axe_xfer, sc->axe_ep[AXE_ENDPT_RX],
1108 		    c, mtod(c->axe_mbuf, char *), AXE_BUFSZ,
1109 		    USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, axe_rxeof);
1110 		usbd_transfer(c->axe_xfer);
1111 	}
1112 
1113 	ifp->if_flags |= IFF_RUNNING;
1114 	ifp->if_flags &= ~IFF_OACTIVE;
1115 
1116 	splx(s);
1117 
1118 	callout_init(&(sc->axe_stat_ch), 0);
1119 	callout_reset(&(sc->axe_stat_ch), (hz), (axe_tick), (sc));
1120 	return;
1121 }
1122 
1123 Static int
1124 axe_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1125 {
1126 	struct axe_softc	*sc = ifp->if_softc;
1127 	struct ifreq		*ifr = (struct ifreq *)data;
1128 	struct ifaddr		*ifa = (struct ifaddr *)data;
1129 	u_int16_t		rxmode;
1130 	int			error = 0;
1131 
1132 	switch(cmd) {
1133 	case SIOCINITIFADDR:
1134 		ifp->if_flags |= IFF_UP;
1135 		axe_init(sc);
1136 
1137 		switch (ifa->ifa_addr->sa_family) {
1138 #ifdef INET
1139 		case AF_INET:
1140 #if defined(__NetBSD__)
1141 			arp_ifinit(ifp, ifa);
1142 #else
1143 			arp_ifinit(&sc->arpcom, ifa);
1144 #endif
1145 			break;
1146 #endif /* INET */
1147 		}
1148 		break;
1149 
1150 	case SIOCSIFMTU:
1151 		if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > ETHERMTU)
1152 			error = EINVAL;
1153 		else if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET)
1154 			error = 0;
1155 		break;
1156 
1157 	case SIOCSIFFLAGS:
1158 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
1159 			break;
1160 		if (ifp->if_flags & IFF_UP) {
1161 			if (ifp->if_flags & IFF_RUNNING &&
1162 			    ifp->if_flags & IFF_PROMISC &&
1163 			    !(sc->axe_if_flags & IFF_PROMISC)) {
1164 
1165 				axe_lock_mii(sc);
1166 				axe_cmd(sc, AXE_CMD_RXCTL_READ,
1167 					0, 0, (void *)&rxmode);
1168 				rxmode = le16toh(rxmode) | AXE_RXCMD_PROMISC;
1169 				axe_cmd(sc, AXE_CMD_RXCTL_WRITE,
1170 					0, rxmode, NULL);
1171 				axe_unlock_mii(sc);
1172 
1173 				axe_setmulti(sc);
1174 			} else if (ifp->if_flags & IFF_RUNNING &&
1175 			    !(ifp->if_flags & IFF_PROMISC) &&
1176 			    sc->axe_if_flags & IFF_PROMISC) {
1177 				axe_lock_mii(sc);
1178 				axe_cmd(sc, AXE_CMD_RXCTL_READ,
1179 					0, 0, (void *)&rxmode);
1180 				rxmode = le16toh(rxmode) & ~AXE_RXCMD_PROMISC;
1181 				axe_cmd(sc, AXE_CMD_RXCTL_WRITE,
1182 					0, rxmode, NULL);
1183 				axe_unlock_mii(sc);
1184 				axe_setmulti(sc);
1185 			} else if (!(ifp->if_flags & IFF_RUNNING))
1186 				axe_init(sc);
1187 		} else {
1188 			if (ifp->if_flags & IFF_RUNNING)
1189 				axe_stop(sc);
1190 		}
1191 		sc->axe_if_flags = ifp->if_flags;
1192 		error = 0;
1193 		break;
1194 	case SIOCADDMULTI:
1195 	case SIOCDELMULTI:
1196 	case SIOCGIFMEDIA:
1197 	case SIOCSIFMEDIA:
1198 		error = ether_ioctl(ifp, cmd, data);
1199 		if (error == ENETRESET) {
1200 			/*
1201 			 * Multicast list has changed; set the hardware
1202 			 * filter accordingly.
1203 			 */
1204 			if (ifp->if_flags & IFF_RUNNING)
1205 				axe_setmulti(sc);
1206 			error = 0;
1207 		}
1208 		break;
1209 	default:
1210 		error = ether_ioctl(ifp, cmd, data);
1211 		break;
1212 	}
1213 
1214 	return(error);
1215 }
1216 
1217 /*
1218  * XXX
1219  * You can't call axe_txeof since the USB transfer has not
1220  * completed yet.
1221  */
1222 Static void
1223 axe_watchdog(struct ifnet *ifp)
1224 {
1225 	struct axe_softc	*sc;
1226 	struct axe_chain	*c;
1227 	usbd_status		stat;
1228 	int			s;
1229 
1230 	sc = ifp->if_softc;
1231 
1232 	ifp->if_oerrors++;
1233 	printf("%s: watchdog timeout\n", device_xname(sc->axe_dev));
1234 
1235 	s = splusb();
1236 	c = &sc->axe_cdata.axe_tx_chain[0];
1237 	usbd_get_xfer_status(c->axe_xfer, NULL, NULL, NULL, &stat);
1238 	axe_txeof(c->axe_xfer, c, stat);
1239 
1240 	if (ifp->if_snd.ifq_head != NULL)
1241 		axe_start(ifp);
1242 	splx(s);
1243 }
1244 
1245 /*
1246  * Stop the adapter and free any mbufs allocated to the
1247  * RX and TX lists.
1248  */
1249 Static void
1250 axe_stop(struct axe_softc *sc)
1251 {
1252 	usbd_status		err;
1253 	struct ifnet		*ifp;
1254 	int			i;
1255 
1256 	axe_reset(sc);
1257 
1258 	ifp = GET_IFP(sc);
1259 	ifp->if_timer = 0;
1260 
1261 	callout_stop(&(sc->axe_stat_ch));
1262 
1263 	/* Stop transfers. */
1264 	if (sc->axe_ep[AXE_ENDPT_RX] != NULL) {
1265 		err = usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_RX]);
1266 		if (err) {
1267 			printf("%s: abort rx pipe failed: %s\n",
1268 			    device_xname(sc->axe_dev), usbd_errstr(err));
1269 		}
1270 		err = usbd_close_pipe(sc->axe_ep[AXE_ENDPT_RX]);
1271 		if (err) {
1272 			printf("%s: close rx pipe failed: %s\n",
1273 			    device_xname(sc->axe_dev), usbd_errstr(err));
1274 		}
1275 		sc->axe_ep[AXE_ENDPT_RX] = NULL;
1276 	}
1277 
1278 	if (sc->axe_ep[AXE_ENDPT_TX] != NULL) {
1279 		err = usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_TX]);
1280 		if (err) {
1281 			printf("%s: abort tx pipe failed: %s\n",
1282 			    device_xname(sc->axe_dev), usbd_errstr(err));
1283 		}
1284 		err = usbd_close_pipe(sc->axe_ep[AXE_ENDPT_TX]);
1285 		if (err) {
1286 			printf("%s: close tx pipe failed: %s\n",
1287 			    device_xname(sc->axe_dev), usbd_errstr(err));
1288 		}
1289 		sc->axe_ep[AXE_ENDPT_TX] = NULL;
1290 	}
1291 
1292 	if (sc->axe_ep[AXE_ENDPT_INTR] != NULL) {
1293 		err = usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_INTR]);
1294 		if (err) {
1295 			printf("%s: abort intr pipe failed: %s\n",
1296 			    device_xname(sc->axe_dev), usbd_errstr(err));
1297 		}
1298 		err = usbd_close_pipe(sc->axe_ep[AXE_ENDPT_INTR]);
1299 		if (err) {
1300 			printf("%s: close intr pipe failed: %s\n",
1301 			    device_xname(sc->axe_dev), usbd_errstr(err));
1302 		}
1303 		sc->axe_ep[AXE_ENDPT_INTR] = NULL;
1304 	}
1305 
1306 	/* Free RX resources. */
1307 	for (i = 0; i < AXE_RX_LIST_CNT; i++) {
1308 		if (sc->axe_cdata.axe_rx_chain[i].axe_mbuf != NULL) {
1309 			m_freem(sc->axe_cdata.axe_rx_chain[i].axe_mbuf);
1310 			sc->axe_cdata.axe_rx_chain[i].axe_mbuf = NULL;
1311 		}
1312 		if (sc->axe_cdata.axe_rx_chain[i].axe_xfer != NULL) {
1313 			usbd_free_xfer(sc->axe_cdata.axe_rx_chain[i].axe_xfer);
1314 			sc->axe_cdata.axe_rx_chain[i].axe_xfer = NULL;
1315 		}
1316 	}
1317 
1318 	/* Free TX resources. */
1319 	for (i = 0; i < AXE_TX_LIST_CNT; i++) {
1320 		if (sc->axe_cdata.axe_tx_chain[i].axe_mbuf != NULL) {
1321 			m_freem(sc->axe_cdata.axe_tx_chain[i].axe_mbuf);
1322 			sc->axe_cdata.axe_tx_chain[i].axe_mbuf = NULL;
1323 		}
1324 		if (sc->axe_cdata.axe_tx_chain[i].axe_xfer != NULL) {
1325 			usbd_free_xfer(sc->axe_cdata.axe_tx_chain[i].axe_xfer);
1326 			sc->axe_cdata.axe_tx_chain[i].axe_xfer = NULL;
1327 		}
1328 	}
1329 
1330 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1331 }
1332