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