xref: /netbsd-src/sys/dev/usb/if_axe.c (revision 1921cb5602567cfc8401cc953be22fe9d74238f2)
1 /*	$NetBSD: if_axe.c,v 1.14 2006/09/07 02:40:33 dogcow 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.14 2006/09/07 02:40:33 dogcow 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, caddr_t);
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 	if (!uaa->iface) {
431 		return(UMATCH_NONE);
432 	}
433 
434 	return (axe_lookup(uaa->vendor, uaa->product) != NULL ?
435 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
436 }
437 
438 /*
439  * Attach the interface. Allocate softc structures, do ifmedia
440  * setup and ethernet/BPF attach.
441  */
442 USB_ATTACH(axe)
443 {
444 	USB_ATTACH_START(axe, sc, uaa);
445 	usbd_device_handle dev = uaa->device;
446 	usbd_status err;
447 	usb_interface_descriptor_t *id;
448 	usb_endpoint_descriptor_t *ed;
449 	struct mii_data	*mii;
450 	u_char eaddr[ETHER_ADDR_LEN];
451 	char *devinfop;
452 	char *devname = USBDEVNAME(sc->axe_dev);
453 	struct ifnet *ifp;
454 	int i, s;
455 
456 	devinfop = usbd_devinfo_alloc(dev, 0);
457 	USB_ATTACH_SETUP;
458 
459 	err = usbd_set_config_no(dev, AXE_CONFIG_NO, 1);
460 	if (err) {
461 		printf("%s: getting interface handle failed\n",
462 		    USBDEVNAME(sc->axe_dev));
463                 usbd_devinfo_free(devinfop);
464 		USB_ATTACH_ERROR_RETURN;
465 	}
466 
467 	usb_init_task(&sc->axe_tick_task, axe_tick_task, sc);
468 	lockinit(&sc->axe_mii_lock, PZERO, "axemii", 0, LK_CANRECURSE);
469 	usb_init_task(&sc->axe_stop_task, (void (*)(void *))axe_stop, sc);
470 
471 	err = usbd_device2interface_handle(dev, AXE_IFACE_IDX, &sc->axe_iface);
472 	if (err) {
473 		printf("%s: getting interface handle failed\n",
474 		    USBDEVNAME(sc->axe_dev));
475                 usbd_devinfo_free(devinfop);
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), devinfop);
486 	usbd_devinfo_free(devinfop);
487 
488 	/* Find endpoints. */
489 	for (i = 0; i < id->bNumEndpoints; i++) {
490 		ed = usbd_interface2endpoint_descriptor(sc->axe_iface, i);
491 		if (!ed) {
492 			printf("%s: couldn't get ep %d\n",
493 			    USBDEVNAME(sc->axe_dev), i);
494 			USB_ATTACH_ERROR_RETURN;
495 		}
496 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
497 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
498 			sc->axe_ed[AXE_ENDPT_RX] = ed->bEndpointAddress;
499 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
500 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
501 			sc->axe_ed[AXE_ENDPT_TX] = ed->bEndpointAddress;
502 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
503 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
504 			sc->axe_ed[AXE_ENDPT_INTR] = ed->bEndpointAddress;
505 		}
506 	}
507 
508 	s = splnet();
509 
510 	/*
511 	 * Get station address.
512 	 */
513 	axe_cmd(sc, AXE_CMD_READ_NODEID, 0, 0, &eaddr);
514 
515 	/*
516 	 * Load IPG values and PHY indexes.
517 	 */
518 	axe_cmd(sc, AXE_CMD_READ_IPG012, 0, 0, (void *)&sc->axe_ipgs);
519 	axe_cmd(sc, AXE_CMD_READ_PHYID, 0, 0, (void *)&sc->axe_phyaddrs);
520 
521 	/*
522 	 * Work around broken adapters that appear to lie about
523 	 * their PHY addresses.
524 	 */
525 	sc->axe_phyaddrs[0] = sc->axe_phyaddrs[1] = 0xFF;
526 
527 	/*
528 	 * An ASIX chip was detected. Inform the world.
529 	 */
530 	printf("%s: Ethernet address %s\n", USBDEVNAME(sc->axe_dev),
531 	    ether_sprintf(eaddr));
532 
533 	/* Initialize interface info.*/
534 	ifp = GET_IFP(sc);
535 	ifp->if_softc = sc;
536 	strncpy(ifp->if_xname, devname, IFNAMSIZ);
537 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
538 	ifp->if_ioctl = axe_ioctl;
539 	ifp->if_start = axe_start;
540 
541 	ifp->if_watchdog = axe_watchdog;
542 
543 /*	ifp->if_baudrate = 10000000; */
544 /*	ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;*/
545 
546 	IFQ_SET_READY(&ifp->if_snd);
547 
548 	/* Initialize MII/media info. */
549 	mii = &sc->axe_mii;
550 	mii->mii_ifp = ifp;
551 	mii->mii_readreg = axe_miibus_readreg;
552 	mii->mii_writereg = axe_miibus_writereg;
553 	mii->mii_statchg = axe_miibus_statchg;
554 	mii->mii_flags = MIIF_AUTOTSLEEP;
555 
556 	ifmedia_init(&mii->mii_media, 0, axe_ifmedia_upd, axe_ifmedia_sts);
557 	mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0);
558 
559 	if (LIST_FIRST(&mii->mii_phys) == NULL) {
560 		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
561 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
562 	} else
563 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
564 
565 	/* Attach the interface. */
566 	if_attach(ifp);
567 	Ether_ifattach(ifp, eaddr);
568 #if NRND > 0
569 	rnd_attach_source(&sc->rnd_source, USBDEVNAME(sc->axe_dev),
570 	    RND_TYPE_NET, 0);
571 #endif
572 
573 	usb_callout_init(sc->axe_stat_ch);
574 
575 	sc->axe_attached = 1;
576 	splx(s);
577 
578 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->axe_udev,
579 			   USBDEV(sc->axe_dev));
580 
581 	USB_ATTACH_SUCCESS_RETURN;
582 }
583 
584 USB_DETACH(axe)
585 {
586 	USB_DETACH_START(axe, sc);
587 	int			s;
588 	struct ifnet		*ifp = GET_IFP(sc);
589 
590 	DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->axe_dev), __func__));
591 
592 	/* Detached before attached finished, so just bail out. */
593 	if (!sc->axe_attached)
594 		return (0);
595 
596 	usb_uncallout(sc->axe_stat_ch, axe_tick, sc);
597 
598 	sc->axe_dying = 1;
599 
600 	ether_ifdetach(ifp);
601 
602 	if (sc->axe_ep[AXE_ENDPT_TX] != NULL)
603 		usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_TX]);
604 	if (sc->axe_ep[AXE_ENDPT_RX] != NULL)
605 		usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_RX]);
606 	if (sc->axe_ep[AXE_ENDPT_INTR] != NULL)
607 		usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_INTR]);
608 
609 	/*
610 	 * Remove any pending tasks.  They cannot be executing because they run
611 	 * in the same thread as detach.
612 	 */
613 	usb_rem_task(sc->axe_udev, &sc->axe_tick_task);
614 	usb_rem_task(sc->axe_udev, &sc->axe_stop_task);
615 
616 	s = splusb();
617 
618 	if (--sc->axe_refcnt >= 0) {
619 		/* Wait for processes to go away */
620 		usb_detach_wait(USBDEV(sc->axe_dev));
621 	}
622 
623 	if (ifp->if_flags & IFF_RUNNING)
624 		axe_stop(sc);
625 
626 #if defined(__NetBSD__)
627 #if NRND > 0
628 	rnd_detach_source(&sc->rnd_source);
629 #endif
630 #endif /* __NetBSD__ */
631 	mii_detach(&sc->axe_mii, MII_PHY_ANY, MII_OFFSET_ANY);
632 	ifmedia_delete_instance(&sc->axe_mii.mii_media, IFM_INST_ANY);
633 	ether_ifdetach(ifp);
634 	if_detach(ifp);
635 
636 #ifdef DIAGNOSTIC
637 	if (sc->axe_ep[AXE_ENDPT_TX] != NULL ||
638 	    sc->axe_ep[AXE_ENDPT_RX] != NULL ||
639 	    sc->axe_ep[AXE_ENDPT_INTR] != NULL)
640 		printf("%s: detach has active endpoints\n",
641 		       USBDEVNAME(sc->axe_dev));
642 #endif
643 
644 	sc->axe_attached = 0;
645 
646 	if (--sc->axe_refcnt >= 0) {
647 		/* Wait for processes to go away. */
648 		usb_detach_wait(USBDEV(sc->axe_dev));
649 	}
650 	splx(s);
651 
652 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->axe_udev,
653 			   USBDEV(sc->axe_dev));
654 
655 	return (0);
656 }
657 
658 int
659 axe_activate(device_ptr_t self, enum devact act)
660 {
661 	struct axe_softc *sc = (struct axe_softc *)self;
662 
663 	DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->axe_dev), __func__));
664 
665 	switch (act) {
666 	case DVACT_ACTIVATE:
667 		return (EOPNOTSUPP);
668 		break;
669 
670 	case DVACT_DEACTIVATE:
671 		if_deactivate(&sc->axe_ec.ec_if);
672 		sc->axe_dying = 1;
673 		break;
674 	}
675 	return (0);
676 }
677 
678 /*
679  * Initialize an RX descriptor and attach an MBUF cluster.
680  */
681 Static int
682 axe_newbuf(struct axe_softc *sc, struct axe_chain *c, struct mbuf *m)
683 {
684 	struct mbuf		*m_new = NULL;
685 
686 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->axe_dev),__func__));
687 
688 	if (m == NULL) {
689 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
690 		if (m_new == NULL) {
691 			printf("%s: no memory for rx list "
692 			    "-- packet dropped!\n", USBDEVNAME(sc->axe_dev));
693 			return (ENOBUFS);
694 		}
695 
696 		MCLGET(m_new, M_DONTWAIT);
697 		if (!(m_new->m_flags & M_EXT)) {
698 			printf("%s: no memory for rx list "
699 			    "-- packet dropped!\n", USBDEVNAME(sc->axe_dev));
700 			m_freem(m_new);
701 			return (ENOBUFS);
702 		}
703 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
704 	} else {
705 		m_new = m;
706 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
707 		m_new->m_data = m_new->m_ext.ext_buf;
708 	}
709 
710 	m_adj(m_new, ETHER_ALIGN);
711 	c->axe_mbuf = m_new;
712 
713 	return (0);
714 }
715 
716 Static int
717 axe_rx_list_init(struct axe_softc *sc)
718 {
719 	struct axe_cdata *cd;
720 	struct axe_chain *c;
721 	int i;
722 
723 	DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->axe_dev), __func__));
724 
725 	cd = &sc->axe_cdata;
726 	for (i = 0; i < AXE_RX_LIST_CNT; i++) {
727 		c = &cd->axe_rx_chain[i];
728 		c->axe_sc = sc;
729 		c->axe_idx = i;
730 		if (axe_newbuf(sc, c, NULL) == ENOBUFS)
731 			return (ENOBUFS);
732 		if (c->axe_xfer == NULL) {
733 			c->axe_xfer = usbd_alloc_xfer(sc->axe_udev);
734 			if (c->axe_xfer == NULL)
735 				return (ENOBUFS);
736 			c->axe_buf = usbd_alloc_buffer(c->axe_xfer, AXE_BUFSZ);
737 			if (c->axe_buf == NULL) {
738 				usbd_free_xfer(c->axe_xfer);
739 				return (ENOBUFS);
740 			}
741 		}
742 	}
743 
744 	return (0);
745 }
746 
747 Static int
748 axe_tx_list_init(struct axe_softc *sc)
749 {
750 	struct axe_cdata *cd;
751 	struct axe_chain *c;
752 	int i;
753 
754 	DPRINTF(("%s: %s: enter\n", USBDEVNAME(sc->axe_dev), __func__));
755 
756 	cd = &sc->axe_cdata;
757 	for (i = 0; i < AXE_TX_LIST_CNT; i++) {
758 		c = &cd->axe_tx_chain[i];
759 		c->axe_sc = sc;
760 		c->axe_idx = i;
761 		c->axe_mbuf = NULL;
762 		if (c->axe_xfer == NULL) {
763 			c->axe_xfer = usbd_alloc_xfer(sc->axe_udev);
764 			if (c->axe_xfer == NULL)
765 				return (ENOBUFS);
766 			c->axe_buf = usbd_alloc_buffer(c->axe_xfer, AXE_BUFSZ);
767 			if (c->axe_buf == NULL) {
768 				usbd_free_xfer(c->axe_xfer);
769 				return (ENOBUFS);
770 			}
771 		}
772 	}
773 
774 	return (0);
775 }
776 
777 #if 0
778 Static void
779 axe_rxstart(struct ifnet *ifp)
780 {
781 	struct axe_softc	*sc;
782 	struct axe_chain	*c;
783 
784 	sc = ifp->if_softc;
785 	axe_lock_mii(sc);
786 	c = &sc->axe_cdata.axe_rx_chain[sc->axe_cdata.axe_rx_prod];
787 
788 	if (axe_newbuf(sc, c, NULL) == ENOBUFS) {
789 		ifp->if_ierrors++;
790 		axe_unlock_mii(sc);
791 		return;
792 	}
793 
794 	/* Setup new transfer. */
795 	usbd_setup_xfer(c->axe_xfer, sc->axe_ep[AXE_ENDPT_RX],
796 	    c, mtod(c->axe_mbuf, char *), AXE_BUFSZ, USBD_SHORT_XFER_OK,
797 	    USBD_NO_TIMEOUT, axe_rxeof);
798 	usbd_transfer(c->axe_xfer);
799 	axe_unlock_mii(sc);
800 
801 	return;
802 }
803 #endif
804 
805 /*
806  * A frame has been uploaded: pass the resulting mbuf chain up to
807  * the higher level protocols.
808  */
809 Static void
810 axe_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
811 {
812 	struct axe_softc	*sc;
813 	struct axe_chain	*c;
814 	struct ifnet		*ifp;
815 	struct mbuf		*m;
816 	u_int32_t		total_len;
817 	int			s;
818 
819 	c = priv;
820 	sc = c->axe_sc;
821 	ifp = GET_IFP(sc);
822 
823 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->axe_dev),__func__));
824 
825 	if (sc->axe_dying)
826 		return;
827 
828 	if (!(ifp->if_flags & IFF_RUNNING))
829 		return;
830 
831 	if (status != USBD_NORMAL_COMPLETION) {
832 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
833 			return;
834 		if (usbd_ratecheck(&sc->axe_rx_notice)) {
835 			printf("%s: usb errors on rx: %s\n",
836 			    USBDEVNAME(sc->axe_dev), usbd_errstr(status));
837 		}
838 		if (status == USBD_STALLED)
839 			usbd_clear_endpoint_stall_async(sc->axe_ep[AXE_ENDPT_RX]);
840 		goto done;
841 	}
842 
843 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
844 
845 	m = c->axe_mbuf;
846 
847 	if (total_len <= sizeof(struct ether_header)) {
848 		ifp->if_ierrors++;
849 		goto done;
850 	}
851 
852 	ifp->if_ipackets++;
853 	m->m_pkthdr.rcvif = ifp;
854 	m->m_pkthdr.len = m->m_len = total_len;
855 
856 
857 	memcpy(mtod(c->axe_mbuf, char *), c->axe_buf, total_len);
858 
859 	/* No errors; receive the packet. */
860 	total_len -= ETHER_CRC_LEN + 4;
861 
862 	s = splnet();
863 
864 	/* XXX ugly */
865 	if (axe_newbuf(sc, c, NULL) == ENOBUFS) {
866 		ifp->if_ierrors++;
867 		goto done1;
868 	}
869 
870 #if NBPFILTER > 0
871 	if (ifp->if_bpf)
872 		BPF_MTAP(ifp, m);
873 #endif
874 
875 	DPRINTFN(10,("%s: %s: deliver %d\n", USBDEVNAME(sc->axe_dev),
876 		    __func__, m->m_len));
877 	IF_INPUT(ifp, m);
878  done1:
879 	splx(s);
880 
881  done:
882 
883 	/* Setup new transfer. */
884 	usbd_setup_xfer(xfer, sc->axe_ep[AXE_ENDPT_RX],
885 	    c, c->axe_buf, AXE_BUFSZ,
886 	    USBD_SHORT_XFER_OK | USBD_NO_COPY,
887 	    USBD_NO_TIMEOUT, axe_rxeof);
888 	usbd_transfer(xfer);
889 
890 	DPRINTFN(10,("%s: %s: start rx\n", USBDEVNAME(sc->axe_dev),
891 		    __func__));
892 	return;
893 }
894 
895 /*
896  * A frame was downloaded to the chip. It's safe for us to clean up
897  * the list buffers.
898  */
899 
900 Static void
901 axe_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
902 {
903 	struct axe_softc	*sc;
904 	struct axe_chain	*c;
905 	struct ifnet		*ifp;
906 	int			s;
907 
908 	c = priv;
909 	sc = c->axe_sc;
910 	ifp = GET_IFP(sc);
911 
912 	if (sc->axe_dying)
913 		return;
914 
915 	s = splnet();
916 
917 	if (status != USBD_NORMAL_COMPLETION) {
918 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
919 			splx(s);
920 			return;
921 		}
922 		ifp->if_oerrors++;
923 		printf("%s: usb error on tx: %s\n", USBDEVNAME(sc->axe_dev),
924 		    usbd_errstr(status));
925 		if (status == USBD_STALLED)
926 			usbd_clear_endpoint_stall_async(sc->axe_ep[AXE_ENDPT_TX]);
927 		splx(s);
928 		return;
929 	}
930 
931 	ifp->if_timer = 0;
932 	ifp->if_flags &= ~IFF_OACTIVE;
933 
934 	m_freem(c->axe_mbuf);
935 	c->axe_mbuf = NULL;
936 
937 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
938 		axe_start(ifp);
939 
940 	ifp->if_opackets++;
941 	splx(s);
942 	return;
943 }
944 
945 Static void
946 axe_tick(void *xsc)
947 {
948 	struct axe_softc *sc = xsc;
949 
950 	if (sc == NULL)
951 		return;
952 
953 	DPRINTFN(0xff, ("%s: %s: enter\n", USBDEVNAME(sc->axe_dev),
954 			__func__));
955 
956 	if (sc->axe_dying)
957 		return;
958 
959 	/* Perform periodic stuff in process context */
960 	usb_add_task(sc->axe_udev, &sc->axe_tick_task);
961 
962 }
963 
964 Static void
965 axe_tick_task(void *xsc)
966 {
967 	int			s;
968 	struct axe_softc	*sc;
969 	struct ifnet		*ifp;
970 	struct mii_data		*mii;
971 
972 	sc = xsc;
973 
974 	if (sc == NULL)
975 		return;
976 
977 	if (sc->axe_dying)
978 		return;
979 
980 	ifp = GET_IFP(sc);
981 	mii = GET_MII(sc);
982 	if (mii == NULL)
983 		return;
984 
985 	s = splnet();
986 
987 	mii_tick(mii);
988 	if (!sc->axe_link) {
989 		mii_pollstat(mii);
990 		if (mii->mii_media_status & IFM_ACTIVE &&
991 		    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
992 			DPRINTF(("%s: %s: got link\n",
993 				 USBDEVNAME(sc->axe_dev), __func__));
994 			sc->axe_link++;
995 			if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
996 				   axe_start(ifp);
997 		}
998 	}
999 
1000 	usb_callout(sc->axe_stat_ch, hz, axe_tick, sc);
1001 
1002 	splx(s);
1003 }
1004 
1005 Static int
1006 axe_encap(struct axe_softc *sc, struct mbuf *m, int idx)
1007 {
1008 	struct axe_chain	*c;
1009 	usbd_status		err;
1010 
1011 	c = &sc->axe_cdata.axe_tx_chain[idx];
1012 
1013 	/*
1014 	 * Copy the mbuf data into a contiguous buffer, leaving two
1015 	 * bytes at the beginning to hold the frame length.
1016 	 */
1017 	m_copydata(m, 0, m->m_pkthdr.len, c->axe_buf);
1018 	c->axe_mbuf = m;
1019 
1020 	usbd_setup_xfer(c->axe_xfer, sc->axe_ep[AXE_ENDPT_TX],
1021 	    c, c->axe_buf, m->m_pkthdr.len, USBD_FORCE_SHORT_XFER, 10000,
1022 	    axe_txeof);
1023 
1024 	/* Transmit */
1025 	err = usbd_transfer(c->axe_xfer);
1026 	if (err != USBD_IN_PROGRESS) {
1027 		axe_stop(sc);
1028 		return(EIO);
1029 	}
1030 
1031 	sc->axe_cdata.axe_tx_cnt++;
1032 
1033 	return(0);
1034 }
1035 
1036 Static void
1037 axe_start(struct ifnet *ifp)
1038 {
1039 	struct axe_softc	*sc;
1040 	struct mbuf		*m_head = NULL;
1041 
1042 	sc = ifp->if_softc;
1043 
1044 	if (!sc->axe_link) {
1045 		return;
1046 	}
1047 
1048 	if (ifp->if_flags & IFF_OACTIVE) {
1049 		return;
1050 	}
1051 
1052 	IF_DEQUEUE(&ifp->if_snd, m_head);
1053 	if (m_head == NULL) {
1054 		return;
1055 	}
1056 
1057 	if (axe_encap(sc, m_head, 0)) {
1058 		IF_PREPEND(&ifp->if_snd, m_head);
1059 		ifp->if_flags |= IFF_OACTIVE;
1060 		return;
1061 	}
1062 
1063 	/*
1064 	 * If there's a BPF listener, bounce a copy of this frame
1065 	 * to him.
1066 	 */
1067 #if NBPFILTER > 0
1068 	 if (ifp->if_bpf)
1069 	 	BPF_MTAP(ifp, m_head);
1070 #endif
1071 
1072 	ifp->if_flags |= IFF_OACTIVE;
1073 
1074 	/*
1075 	 * Set a timeout in case the chip goes out to lunch.
1076 	 */
1077 	ifp->if_timer = 5;
1078 
1079 	return;
1080 }
1081 
1082 Static void
1083 axe_init(void *xsc)
1084 {
1085 	struct axe_softc	*sc = xsc;
1086 	struct ifnet		*ifp = GET_IFP(sc);
1087 	struct axe_chain	*c;
1088 	usbd_status		err;
1089 	int			rxmode;
1090 	int			i, s;
1091 
1092 	if (ifp->if_flags & IFF_RUNNING)
1093 		return;
1094 
1095 	s = splnet();
1096 
1097 	/*
1098 	 * Cancel pending I/O and free all RX/TX buffers.
1099 	 */
1100 	axe_reset(sc);
1101 
1102 	/* Enable RX logic. */
1103 
1104 	/* Init RX ring. */
1105 	if (axe_rx_list_init(sc) == ENOBUFS) {
1106 		printf("%s: rx list init failed\n", USBDEVNAME(sc->axe_dev));
1107 		splx(s);
1108 		return;
1109 	}
1110 
1111 	/* Init TX ring. */
1112 	if (axe_tx_list_init(sc) == ENOBUFS) {
1113 		printf("%s: tx list init failed\n", USBDEVNAME(sc->axe_dev));
1114 		splx(s);
1115 		return;
1116 	}
1117 
1118 	/* Set transmitter IPG values */
1119 	axe_cmd(sc, AXE_CMD_WRITE_IPG0, 0, sc->axe_ipgs[0], NULL);
1120 	axe_cmd(sc, AXE_CMD_WRITE_IPG1, 0, sc->axe_ipgs[1], NULL);
1121 	axe_cmd(sc, AXE_CMD_WRITE_IPG2, 0, sc->axe_ipgs[2], NULL);
1122 
1123 	/* Enable receiver, set RX mode */
1124 	rxmode = AXE_RXCMD_UNICAST|AXE_RXCMD_MULTICAST|AXE_RXCMD_ENABLE;
1125 
1126 	/* If we want promiscuous mode, set the allframes bit. */
1127 	if (ifp->if_flags & IFF_PROMISC)
1128 		rxmode |= AXE_RXCMD_PROMISC;
1129 
1130 	if (ifp->if_flags & IFF_BROADCAST)
1131 		rxmode |= AXE_RXCMD_BROADCAST;
1132 
1133 	axe_cmd(sc, AXE_CMD_RXCTL_WRITE, 0, rxmode, NULL);
1134 
1135 	/* Load the multicast filter. */
1136 	axe_setmulti(sc);
1137 
1138 	/* Open RX and TX pipes. */
1139 	err = usbd_open_pipe(sc->axe_iface, sc->axe_ed[AXE_ENDPT_RX],
1140 	    USBD_EXCLUSIVE_USE, &sc->axe_ep[AXE_ENDPT_RX]);
1141 	if (err) {
1142 		printf("%s: open rx pipe failed: %s\n",
1143 		    USBDEVNAME(sc->axe_dev), usbd_errstr(err));
1144 		splx(s);
1145 		return;
1146 	}
1147 
1148 	err = usbd_open_pipe(sc->axe_iface, sc->axe_ed[AXE_ENDPT_TX],
1149 	    USBD_EXCLUSIVE_USE, &sc->axe_ep[AXE_ENDPT_TX]);
1150 	if (err) {
1151 		printf("%s: open tx pipe failed: %s\n",
1152 		    USBDEVNAME(sc->axe_dev), usbd_errstr(err));
1153 		splx(s);
1154 		return;
1155 	}
1156 
1157 	/* Start up the receive pipe. */
1158 	for (i = 0; i < AXE_RX_LIST_CNT; i++) {
1159 		c = &sc->axe_cdata.axe_rx_chain[i];
1160 		usbd_setup_xfer(c->axe_xfer, sc->axe_ep[AXE_ENDPT_RX],
1161 		    c, mtod(c->axe_mbuf, char *), AXE_BUFSZ,
1162 		    USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, axe_rxeof);
1163 		usbd_transfer(c->axe_xfer);
1164 	}
1165 
1166 	ifp->if_flags |= IFF_RUNNING;
1167 	ifp->if_flags &= ~IFF_OACTIVE;
1168 
1169 	splx(s);
1170 
1171 	usb_callout_init(sc->axe_stat_ch);
1172 	usb_callout(sc->axe_stat_ch, hz, axe_tick, sc);
1173 	return;
1174 }
1175 
1176 Static int
1177 axe_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1178 {
1179 	struct axe_softc	*sc = ifp->if_softc;
1180 	struct ifreq		*ifr = (struct ifreq *)data;
1181 	struct ifaddr		*ifa = (struct ifaddr *)data;
1182 	struct mii_data		*mii;
1183 	u_int16_t		rxmode;
1184 	int			error = 0;
1185 
1186 	switch(cmd) {
1187 	case SIOCSIFADDR:
1188 		ifp->if_flags |= IFF_UP;
1189 		axe_init(sc);
1190 
1191 		switch (ifa->ifa_addr->sa_family) {
1192 #ifdef INET
1193 		case AF_INET:
1194 #if defined(__NetBSD__)
1195 			arp_ifinit(ifp, ifa);
1196 #else
1197 			arp_ifinit(&sc->arpcom, ifa);
1198 #endif
1199 			break;
1200 #endif /* INET */
1201 		}
1202 		break;
1203 
1204 	case SIOCSIFMTU:
1205 		if (ifr->ifr_mtu > ETHERMTU)
1206 			error = EINVAL;
1207 		else
1208 			ifp->if_mtu = ifr->ifr_mtu;
1209 		break;
1210 
1211 	case SIOCSIFFLAGS:
1212 		if (ifp->if_flags & IFF_UP) {
1213 			if (ifp->if_flags & IFF_RUNNING &&
1214 			    ifp->if_flags & IFF_PROMISC &&
1215 			    !(sc->axe_if_flags & IFF_PROMISC)) {
1216 
1217 				axe_cmd(sc, AXE_CMD_RXCTL_READ,
1218 					0, 0, (void *)&rxmode);
1219 				rxmode = le16toh(rxmode) | AXE_RXCMD_PROMISC;
1220 				axe_cmd(sc, AXE_CMD_RXCTL_WRITE,
1221 					0, rxmode, NULL);
1222 
1223 				axe_setmulti(sc);
1224 			} else if (ifp->if_flags & IFF_RUNNING &&
1225 			    !(ifp->if_flags & IFF_PROMISC) &&
1226 			    sc->axe_if_flags & IFF_PROMISC) {
1227 				axe_cmd(sc, AXE_CMD_RXCTL_READ,
1228 					0, 0, (void *)&rxmode);
1229 				rxmode = le16toh(rxmode) & ~AXE_RXCMD_PROMISC;
1230 				axe_cmd(sc, AXE_CMD_RXCTL_WRITE,
1231 					0, rxmode, NULL);
1232 				axe_setmulti(sc);
1233 			} else if (!(ifp->if_flags & IFF_RUNNING))
1234 				axe_init(sc);
1235 		} else {
1236 			if (ifp->if_flags & IFF_RUNNING)
1237 				axe_stop(sc);
1238 		}
1239 		sc->axe_if_flags = ifp->if_flags;
1240 		error = 0;
1241 		break;
1242 	case SIOCADDMULTI:
1243 	case SIOCDELMULTI:
1244 #ifdef __NetBSD__
1245 		error = (cmd == SIOCADDMULTI) ?
1246 			ether_addmulti(ifr, &sc->axe_ec) :
1247 			ether_delmulti(ifr, &sc->axe_ec);
1248 #else
1249 		error = (cmd == SIOCADDMULTI) ?
1250 		    ether_addmulti(ifr, &sc->arpcom) :
1251 		    ether_delmulti(ifr, &sc->arpcom);
1252 #endif /* __NetBSD__ */
1253 		if (error == ENETRESET) {
1254 			/*
1255 			 * Multicast list has changed; set the hardware
1256 			 * filter accordingly.
1257 			 */
1258 			if (ifp->if_flags & IFF_RUNNING)
1259 				axe_setmulti(sc);
1260 			error = 0;
1261 		}
1262 		break;
1263 	case SIOCGIFMEDIA:
1264 	case SIOCSIFMEDIA:
1265 		mii = GET_MII(sc);
1266 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, cmd);
1267 		break;
1268 
1269 	default:
1270 		error = EINVAL;
1271 		break;
1272 	}
1273 
1274 	return(error);
1275 }
1276 
1277 /*
1278  * XXX
1279  * You can't call axe_txeof since the USB transfer has not
1280  * completed yet.
1281  */
1282 Static void
1283 axe_watchdog(struct ifnet *ifp)
1284 {
1285 	struct axe_softc	*sc;
1286 	struct axe_chain	*c;
1287 	usbd_status		stat;
1288 	int			s;
1289 
1290 	sc = ifp->if_softc;
1291 
1292 	ifp->if_oerrors++;
1293 	printf("%s: watchdog timeout\n", USBDEVNAME(sc->axe_dev));
1294 
1295 	s = splusb();
1296 	c = &sc->axe_cdata.axe_tx_chain[0];
1297 	usbd_get_xfer_status(c->axe_xfer, NULL, NULL, NULL, &stat);
1298 	axe_txeof(c->axe_xfer, c, stat);
1299 
1300 	if (ifp->if_snd.ifq_head != NULL)
1301 		axe_start(ifp);
1302 	splx(s);
1303 }
1304 
1305 /*
1306  * Stop the adapter and free any mbufs allocated to the
1307  * RX and TX lists.
1308  */
1309 Static void
1310 axe_stop(struct axe_softc *sc)
1311 {
1312 	usbd_status		err;
1313 	struct ifnet		*ifp;
1314 	int			i;
1315 
1316 	axe_reset(sc);
1317 
1318 	ifp = GET_IFP(sc);
1319 	ifp->if_timer = 0;
1320 
1321 	usb_uncallout(sc->axe_stat_ch, axe_tick, sc);
1322 
1323 	/* Stop transfers. */
1324 	if (sc->axe_ep[AXE_ENDPT_RX] != NULL) {
1325 		err = usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_RX]);
1326 		if (err) {
1327 			printf("%s: abort rx pipe failed: %s\n",
1328 		    	    USBDEVNAME(sc->axe_dev), usbd_errstr(err));
1329 		}
1330 		err = usbd_close_pipe(sc->axe_ep[AXE_ENDPT_RX]);
1331 		if (err) {
1332 			printf("%s: close rx pipe failed: %s\n",
1333 		    	    USBDEVNAME(sc->axe_dev), usbd_errstr(err));
1334 		}
1335 		sc->axe_ep[AXE_ENDPT_RX] = NULL;
1336 	}
1337 
1338 	if (sc->axe_ep[AXE_ENDPT_TX] != NULL) {
1339 		err = usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_TX]);
1340 		if (err) {
1341 			printf("%s: abort tx pipe failed: %s\n",
1342 		    	    USBDEVNAME(sc->axe_dev), usbd_errstr(err));
1343 		}
1344 		err = usbd_close_pipe(sc->axe_ep[AXE_ENDPT_TX]);
1345 		if (err) {
1346 			printf("%s: close tx pipe failed: %s\n",
1347 			    USBDEVNAME(sc->axe_dev), usbd_errstr(err));
1348 		}
1349 		sc->axe_ep[AXE_ENDPT_TX] = NULL;
1350 	}
1351 
1352 	if (sc->axe_ep[AXE_ENDPT_INTR] != NULL) {
1353 		err = usbd_abort_pipe(sc->axe_ep[AXE_ENDPT_INTR]);
1354 		if (err) {
1355 			printf("%s: abort intr pipe failed: %s\n",
1356 		    	    USBDEVNAME(sc->axe_dev), usbd_errstr(err));
1357 		}
1358 		err = usbd_close_pipe(sc->axe_ep[AXE_ENDPT_INTR]);
1359 		if (err) {
1360 			printf("%s: close intr pipe failed: %s\n",
1361 			    USBDEVNAME(sc->axe_dev), usbd_errstr(err));
1362 		}
1363 		sc->axe_ep[AXE_ENDPT_INTR] = NULL;
1364 	}
1365 
1366 	/* Free RX resources. */
1367 	for (i = 0; i < AXE_RX_LIST_CNT; i++) {
1368 		if (sc->axe_cdata.axe_rx_chain[i].axe_mbuf != NULL) {
1369 			m_freem(sc->axe_cdata.axe_rx_chain[i].axe_mbuf);
1370 			sc->axe_cdata.axe_rx_chain[i].axe_mbuf = NULL;
1371 		}
1372 		if (sc->axe_cdata.axe_rx_chain[i].axe_xfer != NULL) {
1373 			usbd_free_xfer(sc->axe_cdata.axe_rx_chain[i].axe_xfer);
1374 			sc->axe_cdata.axe_rx_chain[i].axe_xfer = NULL;
1375 		}
1376 	}
1377 
1378 	/* Free TX resources. */
1379 	for (i = 0; i < AXE_TX_LIST_CNT; i++) {
1380 		if (sc->axe_cdata.axe_tx_chain[i].axe_mbuf != NULL) {
1381 			m_freem(sc->axe_cdata.axe_tx_chain[i].axe_mbuf);
1382 			sc->axe_cdata.axe_tx_chain[i].axe_mbuf = NULL;
1383 		}
1384 		if (sc->axe_cdata.axe_tx_chain[i].axe_xfer != NULL) {
1385 			usbd_free_xfer(sc->axe_cdata.axe_tx_chain[i].axe_xfer);
1386 			sc->axe_cdata.axe_tx_chain[i].axe_xfer = NULL;
1387 		}
1388 	}
1389 
1390 	sc->axe_link = 0;
1391 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1392 }
1393 
1394