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