xref: /netbsd-src/sys/dev/usb/if_aue.c (revision 481fca6e59249d8ffcf24fef7cfbe7b131bfb080)
1 /*	$NetBSD: if_aue.c,v 1.42 2000/06/01 14:28:57 augustss Exp $	*/
2 /*
3  * Copyright (c) 1997, 1998, 1999, 2000
4  *	Bill Paul <wpaul@ee.columbia.edu>.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by Bill Paul.
17  * 4. Neither the name of the author nor the names of any co-contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31  * THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  * $FreeBSD: src/sys/dev/usb/if_aue.c,v 1.11 2000/01/14 01:36:14 wpaul Exp $
34  */
35 
36 /*
37  * ADMtek AN986 Pegasus USB to ethernet driver. Datasheet is available
38  * from http://www.admtek.com.tw.
39  *
40  * Written by Bill Paul <wpaul@ee.columbia.edu>
41  * Electrical Engineering Department
42  * Columbia University, New York City
43  */
44 
45 /*
46  * The Pegasus chip uses four USB "endpoints" to provide 10/100 ethernet
47  * support: the control endpoint for reading/writing registers, burst
48  * read endpoint for packet reception, burst write for packet transmission
49  * and one for "interrupts." The chip uses the same RX filter scheme
50  * as the other ADMtek ethernet parts: one perfect filter entry for the
51  * the station address and a 64-bit multicast hash table. The chip supports
52  * both MII and HomePNA attachments.
53  *
54  * Since the maximum data transfer speed of USB is supposed to be 12Mbps,
55  * you're never really going to get 100Mbps speeds from this device. I
56  * think the idea is to allow the device to connect to 10 or 100Mbps
57  * networks, not necessarily to provide 100Mbps performance. Also, since
58  * the controller uses an external PHY chip, it's possible that board
59  * designers might simply choose a 10Mbps PHY.
60  *
61  * Registers are accessed using usbd_do_request(). Packet transfers are
62  * done using usbd_transfer() and friends.
63  */
64 
65 /*
66  * Ported to NetBSD and somewhat rewritten by Lennart Augustsson.
67  */
68 
69 /*
70  * TODO:
71  * better error messages from rxstat
72  * split out if_auevar.h
73  * add thread to avoid register reads from interrupt context
74  * more error checks
75  * investigate short rx problem
76  * proper cleanup on errors
77  */
78 
79 #if defined(__NetBSD__)
80 #include "opt_inet.h"
81 #include "opt_ns.h"
82 #include "bpfilter.h"
83 #include "rnd.h"
84 #elif defined(__OpenBSD__)
85 #include "bpfilter.h"
86 #endif /* defined(__OpenBSD__) */
87 
88 #include <sys/param.h>
89 #include <sys/systm.h>
90 #include <sys/sockio.h>
91 #include <sys/mbuf.h>
92 #include <sys/malloc.h>
93 #include <sys/kernel.h>
94 #include <sys/socket.h>
95 
96 #if defined(__FreeBSD__)
97 
98 #include <net/ethernet.h>
99 #include <machine/clock.h>	/* for DELAY */
100 #include <sys/bus.h>
101 /* "controller miibus0" required.  See GENERIC if you get errors here. */
102 #include "miibus_if.h"
103 
104 #elif defined(__NetBSD__) || defined(__OpenBSD__)
105 
106 #include <sys/device.h>
107 #if NRND > 0
108 #include <sys/rnd.h>
109 #endif
110 
111 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
112 
113 #include <net/if.h>
114 #if defined(__NetBSD__) || defined(__FreeBSD__)
115 #include <net/if_arp.h>
116 #endif
117 #include <net/if_dl.h>
118 #include <net/if_media.h>
119 
120 #if defined(__NetBSD__) || defined(__OpenBSD__)
121 #define BPF_MTAP(ifp, m) bpf_mtap((ifp)->if_bpf, (m))
122 #else
123 #define BPF_MTAP(ifp, m) bpf_mtap((ifp), (m))
124 #endif
125 
126 #if defined(__FreeBSD__) || NBPFILTER > 0
127 #include <net/bpf.h>
128 #endif
129 
130 #if defined(__NetBSD__)
131 #include <net/if_ether.h>
132 #ifdef INET
133 #include <netinet/in.h>
134 #include <netinet/if_inarp.h>
135 #endif
136 #endif /* defined(__NetBSD__) */
137 
138 #if defined(__OpenBSD__)
139 #ifdef INET
140 #include <netinet/in.h>
141 #include <netinet/in_systm.h>
142 #include <netinet/in_var.h>
143 #include <netinet/ip.h>
144 #include <netinet/if_ether.h>
145 #endif
146 #endif /* defined(__OpenBSD__) */
147 
148 #if defined(__NetBSD__) || defined(__OpenBSD__)
149 #ifdef NS
150 #include <netns/ns.h>
151 #include <netns/ns_if.h>
152 #endif
153 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
154 
155 #include <dev/mii/mii.h>
156 #include <dev/mii/miivar.h>
157 
158 #include <dev/usb/usb.h>
159 #include <dev/usb/usbdi.h>
160 #include <dev/usb/usbdi_util.h>
161 #include <dev/usb/usbdevs.h>
162 
163 #ifdef __FreeBSD__
164 #include <dev/usb/usb_ethersubr.h>
165 #endif
166 
167 #include <dev/usb/if_auereg.h>
168 
169 #ifdef AUE_DEBUG
170 #define DPRINTF(x)	if (auedebug) logprintf x
171 #define DPRINTFN(n,x)	if (auedebug >= (n)) logprintf x
172 int	auedebug = 0;
173 #else
174 #define DPRINTF(x)
175 #define DPRINTFN(n,x)
176 #endif
177 
178 /*
179  * Various supported device vendors/products.
180  */
181 struct aue_type {
182 	u_int16_t		aue_vid;
183 	u_int16_t		aue_did;
184 	char			aue_linksys;
185 };
186 
187 Static struct aue_type aue_devs[] = {
188   { USB_VENDOR_BILLIONTON,	USB_PRODUCT_BILLIONTON_USB100,	0 },
189   { USB_VENDOR_MELCO, 		USB_PRODUCT_MELCO_LUATX, 	0 },
190   { USB_VENDOR_LINKSYS,		USB_PRODUCT_LINKSYS_USB100TX,	1 },
191   { USB_VENDOR_LINKSYS,		USB_PRODUCT_LINKSYS_USB100H1,	1 },
192   { USB_VENDOR_LINKSYS,		USB_PRODUCT_LINKSYS_USB10TA,	1 },
193   { USB_VENDOR_ADMTEK,		USB_PRODUCT_ADMTEK_PEGASUS,	0 },
194   { USB_VENDOR_DLINK,		USB_PRODUCT_DLINK_DSB650TX,	1 },
195   { USB_VENDOR_DLINK,		USB_PRODUCT_DLINK_DSB650TX_PNA,	0 },
196   { USB_VENDOR_SMC,		USB_PRODUCT_SMC_2202USB,	0 },
197   { USB_VENDOR_COREGA,		USB_PRODUCT_COREGA_FETHER_USB_TX, 0 },
198   { USB_VENDOR_IODATA,		USB_PRODUCT_IODATA_USBETTX,	0 },
199   { 0, 0, 0 }
200 };
201 
202 USB_DECLARE_DRIVER(aue);
203 
204 Static struct aue_type *aue_lookup(u_int16_t vendor, u_int16_t product);
205 Static int aue_tx_list_init(struct aue_softc *);
206 Static int aue_rx_list_init(struct aue_softc *);
207 Static int aue_newbuf(struct aue_softc *, struct aue_chain *, struct mbuf *);
208 Static int aue_send(struct aue_softc *, struct mbuf *, int);
209 Static void aue_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
210 Static void aue_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
211 Static void aue_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
212 Static void aue_tick(void *);
213 Static void aue_start(struct ifnet *);
214 Static int aue_ioctl(struct ifnet *, u_long, caddr_t);
215 Static void aue_init(void *);
216 Static void aue_stop(struct aue_softc *);
217 Static void aue_watchdog(struct ifnet *);
218 #ifdef __FreeBSD__
219 Static void aue_shutdown(device_ptr_t);
220 #endif
221 Static int aue_openpipes(struct aue_softc *);
222 Static int aue_ifmedia_upd(struct ifnet *);
223 Static void aue_ifmedia_sts(struct ifnet *, struct ifmediareq *);
224 
225 Static int aue_eeprom_getword(struct aue_softc *, int);
226 Static void aue_read_mac(struct aue_softc *, u_char *);
227 Static int aue_miibus_readreg(device_ptr_t, int, int);
228 #if defined(__FreeBSD__)
229 Static int aue_miibus_writereg(device_ptr_t, int, int, int);
230 #elif defined(__NetBSD__) || defined(__OpenBSD__)
231 Static void aue_miibus_writereg(device_ptr_t, int, int, int);
232 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
233 Static void aue_miibus_statchg(device_ptr_t);
234 
235 Static void aue_setmulti(struct aue_softc *);
236 Static u_int32_t aue_crc(caddr_t);
237 Static void aue_reset(struct aue_softc *);
238 
239 Static int aue_csr_read_1(struct aue_softc *, int);
240 Static int aue_csr_write_1(struct aue_softc *, int, int);
241 Static int aue_csr_read_2(struct aue_softc *, int);
242 Static int aue_csr_write_2(struct aue_softc *, int, int);
243 
244 #if defined(__FreeBSD__)
245 #if !defined(lint)
246 static const char rcsid[] =
247   "$FreeBSD: src/sys/dev/usb/if_aue.c,v 1.11 2000/01/14 01:36:14 wpaul Exp $";
248 #endif
249 
250 Static void aue_rxstart(struct ifnet *);
251 
252 Static struct usb_qdat aue_qdat;
253 
254 Static device_method_t aue_methods[] = {
255 	/* Device interface */
256 	DEVMETHOD(device_probe,		aue_match),
257 	DEVMETHOD(device_attach,	aue_attach),
258 	DEVMETHOD(device_detach,	aue_detach),
259 	DEVMETHOD(device_shutdown,	aue_shutdown),
260 
261 	/* bus interface */
262 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
263 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
264 
265 	/* MII interface */
266 	DEVMETHOD(miibus_readreg,	aue_miibus_readreg),
267 	DEVMETHOD(miibus_writereg,	aue_miibus_writereg),
268 	DEVMETHOD(miibus_statchg,	aue_miibus_statchg),
269 
270 	{ 0, 0 }
271 };
272 
273 Static driver_t aue_driver = {
274 	"aue",
275 	aue_methods,
276 	sizeof(struct aue_softc)
277 };
278 
279 Static devclass_t aue_devclass;
280 
281 DRIVER_MODULE(if_aue, uhub, aue_driver, aue_devclass, usbd_driver_load, 0);
282 DRIVER_MODULE(miibus, aue, miibus_driver, miibus_devclass, 0, 0);
283 
284 #endif /* __FreeBSD__ */
285 
286 #define AUE_DO_REQUEST(dev, req, data)			\
287 	usbd_do_request_flags(dev, req, data, USBD_NO_TSLEEP, NULL)
288 
289 #define AUE_SETBIT(sc, reg, x)				\
290 	aue_csr_write_1(sc, reg, aue_csr_read_1(sc, reg) | (x))
291 
292 #define AUE_CLRBIT(sc, reg, x)				\
293 	aue_csr_write_1(sc, reg, aue_csr_read_1(sc, reg) & ~(x))
294 
295 Static int
296 aue_csr_read_1(struct aue_softc *sc, int reg)
297 {
298 	usb_device_request_t	req;
299 	usbd_status		err;
300 	uByte			val = 0;
301 	int			s;
302 
303 	if (sc->aue_dying)
304 		return (0);
305 
306 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
307 	req.bRequest = AUE_UR_READREG;
308 	USETW(req.wValue, 0);
309 	USETW(req.wIndex, reg);
310 	USETW(req.wLength, 1);
311 
312 	s = splusb();
313 	err = AUE_DO_REQUEST(sc->aue_udev, &req, &val);
314 	splx(s);
315 
316 	if (err) {
317 		DPRINTF(("%s: aue_csr_read_1: reg=0x%x err=%s\n",
318 			 USBDEVNAME(sc->aue_dev), reg, usbd_errstr(err)));
319 		return (0);
320 	}
321 
322 	return (val);
323 }
324 
325 Static int
326 aue_csr_read_2(struct aue_softc *sc, int reg)
327 {
328 	usb_device_request_t	req;
329 	usbd_status		err;
330 	uWord			val;
331 	int			s;
332 
333 	if (sc->aue_dying)
334 		return (0);
335 
336 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
337 	req.bRequest = AUE_UR_READREG;
338 	USETW(req.wValue, 0);
339 	USETW(req.wIndex, reg);
340 	USETW(req.wLength, 2);
341 
342 	s = splusb();
343 	err = AUE_DO_REQUEST(sc->aue_udev, &req, &val);
344 	splx(s);
345 
346 	if (err) {
347 		DPRINTF(("%s: aue_csr_read_2: reg=0x%x err=%s\n",
348 			 USBDEVNAME(sc->aue_dev), reg, usbd_errstr(err)));
349 		return (0);
350 	}
351 
352 	return (UGETW(val));
353 }
354 
355 Static int
356 aue_csr_write_1(struct aue_softc *sc, int reg, int aval)
357 {
358 	usb_device_request_t	req;
359 	usbd_status		err;
360 	int			s;
361 	uByte			val;
362 
363 	if (sc->aue_dying)
364 		return (0);
365 
366 	val = aval;
367 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
368 	req.bRequest = AUE_UR_WRITEREG;
369 	USETW(req.wValue, val);
370 	USETW(req.wIndex, reg);
371 	USETW(req.wLength, 1);
372 
373 	s = splusb();
374 	err = AUE_DO_REQUEST(sc->aue_udev, &req, &val);
375 	splx(s);
376 
377 	if (err) {
378 		DPRINTF(("%s: aue_csr_write_1: reg=0x%x err=%s\n",
379 			 USBDEVNAME(sc->aue_dev), reg, usbd_errstr(err)));
380 		return (-1);
381 	}
382 
383 	return (0);
384 }
385 
386 Static int
387 aue_csr_write_2(struct aue_softc *sc, int reg, int aval)
388 {
389 	usb_device_request_t	req;
390 	usbd_status		err;
391 	int			s;
392 	uWord			val;
393 
394 	if (sc->aue_dying)
395 		return (0);
396 
397 	USETW(val, aval);
398 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
399 	req.bRequest = AUE_UR_WRITEREG;
400 	USETW(req.wValue, aval);
401 	USETW(req.wIndex, reg);
402 	USETW(req.wLength, 2);
403 
404 	s = splusb();
405 	err = AUE_DO_REQUEST(sc->aue_udev, &req, &val);
406 	splx(s);
407 
408 	if (err) {
409 		DPRINTF(("%s: aue_csr_write_2: reg=0x%x err=%s\n",
410 			 USBDEVNAME(sc->aue_dev), reg, usbd_errstr(err)));
411 		return (-1);
412 	}
413 
414 	return (0);
415 }
416 
417 /*
418  * Read a word of data stored in the EEPROM at address 'addr.'
419  */
420 Static int
421 aue_eeprom_getword(struct aue_softc *sc, int addr)
422 {
423 	int		i;
424 
425 	aue_csr_write_1(sc, AUE_EE_REG, addr);
426 	aue_csr_write_1(sc, AUE_EE_CTL, AUE_EECTL_READ);
427 
428 	for (i = 0; i < AUE_TIMEOUT; i++) {
429 		if (aue_csr_read_1(sc, AUE_EE_CTL) & AUE_EECTL_DONE)
430 			break;
431 	}
432 
433 	if (i == AUE_TIMEOUT) {
434 		printf("%s: EEPROM read timed out\n",
435 		    USBDEVNAME(sc->aue_dev));
436 	}
437 
438 	return (aue_csr_read_2(sc, AUE_EE_DATA));
439 }
440 
441 /*
442  * Read the MAC from the EEPROM.  It's at offset 0.
443  */
444 Static void
445 aue_read_mac(struct aue_softc *sc, u_char *dest)
446 {
447 	int			i;
448 	int			off = 0;
449 	int			word;
450 
451 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
452 
453 	for (i = 0; i < 3; i++) {
454 		word = aue_eeprom_getword(sc, off + i);
455 		dest[2 * i] = (u_char)word;
456 		dest[2 * i + 1] = (u_char)(word >> 8);
457 	}
458 }
459 
460 Static int
461 aue_miibus_readreg(device_ptr_t dev, int phy, int reg)
462 {
463 	struct aue_softc	*sc = USBGETSOFTC(dev);
464 	int			i;
465 	u_int16_t		val;
466 
467 	/*
468 	 * The Am79C901 HomePNA PHY actually contains
469 	 * two transceivers: a 1Mbps HomePNA PHY and a
470 	 * 10Mbps full/half duplex ethernet PHY with
471 	 * NWAY autoneg. However in the ADMtek adapter,
472 	 * only the 1Mbps PHY is actually connected to
473 	 * anything, so we ignore the 10Mbps one. It
474 	 * happens to be configured for MII address 3,
475 	 * so we filter that out.
476 	 */
477 	if (sc->aue_vendor == USB_VENDOR_ADMTEK &&
478 	    sc->aue_product == USB_PRODUCT_ADMTEK_PEGASUS) {
479 		if (phy != 1)
480 			return (0);
481 	}
482 
483 	aue_csr_write_1(sc, AUE_PHY_ADDR, phy);
484 	aue_csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_READ);
485 
486 	for (i = 0; i < AUE_TIMEOUT; i++) {
487 		if (aue_csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE)
488 			break;
489 	}
490 
491 	if (i == AUE_TIMEOUT) {
492 		printf("%s: MII read timed out\n",
493 		    USBDEVNAME(sc->aue_dev));
494 	}
495 
496 	val = aue_csr_read_2(sc, AUE_PHY_DATA);
497 
498 	DPRINTFN(11,("%s: %s: phy=%d reg=%d => 0x%04x\n",
499 		     USBDEVNAME(sc->aue_dev), __FUNCTION__, phy, reg, val));
500 
501 	return (val);
502 }
503 
504 #if defined(__FreeBSD__)
505 Static int
506 #elif defined(__NetBSD__) || defined(__OpenBSD__)
507 Static void
508 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
509 aue_miibus_writereg(device_ptr_t dev, int phy, int reg, int data)
510 {
511 	struct aue_softc	*sc = USBGETSOFTC(dev);
512 	int			i;
513 
514 	if (sc->aue_vendor == USB_VENDOR_ADMTEK &&
515 	    sc->aue_product == USB_PRODUCT_ADMTEK_PEGASUS) {
516 		if (phy == 3)
517 #if defined(__FreeBSD__)
518 			return (0);
519 #elif defined(__NetBSD__) || defined(__OpenBSD__)
520 			return;
521 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
522 	}
523 
524 	DPRINTFN(11,("%s: %s: phy=%d reg=%d data=0x%04x\n",
525 		     USBDEVNAME(sc->aue_dev), __FUNCTION__, phy, reg, data));
526 
527 	aue_csr_write_2(sc, AUE_PHY_DATA, data);
528 	aue_csr_write_1(sc, AUE_PHY_ADDR, phy);
529 	aue_csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_WRITE);
530 
531 	for (i = 0; i < AUE_TIMEOUT; i++) {
532 		if (aue_csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE)
533 			break;
534 	}
535 
536 	if (i == AUE_TIMEOUT) {
537 		printf("%s: MII read timed out\n",
538 		    USBDEVNAME(sc->aue_dev));
539 	}
540 
541 #if defined(__FreeBSD__)
542 	return (0);
543 #endif
544 }
545 
546 Static void
547 aue_miibus_statchg(device_ptr_t dev)
548 {
549 	struct aue_softc	*sc = USBGETSOFTC(dev);
550 	struct mii_data		*mii = GET_MII(sc);
551 
552 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
553 
554 	AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB);
555 
556 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX) {
557 		AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL);
558 	} else {
559 		AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL);
560 	}
561 
562 	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
563 		AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX);
564 	else
565 		AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX);
566 
567 	AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB);
568 
569 	/*
570 	 * Set the LED modes on the LinkSys adapter.
571 	 * This turns on the 'dual link LED' bin in the auxmode
572 	 * register of the Broadcom PHY.
573 	 */
574 	if (sc->aue_linksys) {
575 		u_int16_t auxmode;
576 		auxmode = aue_miibus_readreg(dev, 0, 0x1b);
577 		aue_miibus_writereg(dev, 0, 0x1b, auxmode | 0x04);
578 	}
579 }
580 
581 #define AUE_POLY	0xEDB88320
582 #define AUE_BITS	6
583 
584 Static u_int32_t
585 aue_crc(caddr_t addr)
586 {
587 	u_int32_t		idx, bit, data, crc;
588 
589 	/* Compute CRC for the address value. */
590 	crc = 0xFFFFFFFF; /* initial value */
591 
592 	for (idx = 0; idx < 6; idx++) {
593 		for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1)
594 			crc = (crc >> 1) ^ (((crc ^ data) & 1) ? AUE_POLY : 0);
595 	}
596 
597 	return (crc & ((1 << AUE_BITS) - 1));
598 }
599 
600 Static void
601 aue_setmulti(struct aue_softc *sc)
602 {
603 	struct ifnet		*ifp;
604 #if defined(__FreeBSD__)
605 	struct ifmultiaddr	*ifma;
606 #elif defined(__NetBSD__) || defined(__OpenBSD__)
607 	struct ether_multi	*enm;
608 	struct ether_multistep	step;
609 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
610 	u_int32_t		h = 0, i;
611 
612 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
613 
614 	ifp = GET_IFP(sc);
615 
616 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
617 		AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
618 		return;
619 	}
620 
621 	AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
622 
623 	/* first, zot all the existing hash bits */
624 	for (i = 0; i < 8; i++)
625 		aue_csr_write_1(sc, AUE_MAR0 + i, 0);
626 
627 	/* now program new ones */
628 #if defined(__FreeBSD__)
629 	for (ifma = ifp->if_multiaddrs.lh_first; ifma != NULL;
630 	    ifma = ifma->ifma_link.le_next) {
631 		if (ifma->ifma_addr->sa_family != AF_LINK)
632 			continue;
633 		h = aue_crc(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
634 		AUE_SETBIT(sc, AUE_MAR + (h >> 3), 1 << (h & 0xF));
635 	}
636 #elif defined(__NetBSD__) || defined(__OpenBSD__)
637 #if defined(__NetBSD__)
638 	ETHER_FIRST_MULTI(step, &sc->aue_ec, enm);
639 #else
640 	ETHER_FIRST_MULTI(step, &sc->arpcom, enm);
641 #endif
642 	while (enm != NULL) {
643 #if 1
644 		if (memcmp(enm->enm_addrlo,
645 			   enm->enm_addrhi, ETHER_ADDR_LEN) != 0) {
646 			ifp->if_flags |= IFF_ALLMULTI;
647 			AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
648 			return;
649 		}
650 #endif
651 		h = aue_crc(enm->enm_addrlo);
652 		AUE_SETBIT(sc, AUE_MAR + (h >> 3), 1 << (h & 0xF));
653 		ETHER_NEXT_MULTI(step, enm);
654 	}
655 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
656 }
657 
658 Static void
659 aue_reset(struct aue_softc *sc)
660 {
661 	int		i;
662 
663 	DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
664 
665 	AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_RESETMAC);
666 
667 	for (i = 0; i < AUE_TIMEOUT; i++) {
668 		if (!(aue_csr_read_1(sc, AUE_CTL1) & AUE_CTL1_RESETMAC))
669 			break;
670 	}
671 
672 	if (i == AUE_TIMEOUT)
673 		printf("%s: reset failed\n", USBDEVNAME(sc->aue_dev));
674 
675 	/*
676 	 * The PHY(s) attached to the Pegasus chip may be held
677 	 * in reset until we flip on the GPIO outputs. Make sure
678 	 * to set the GPIO pins high so that the PHY(s) will
679 	 * be enabled.
680 	 *
681 	 * Note: We force all of the GPIO pins low first, *then*
682 	 * enable the ones we want.
683   	 */
684 	aue_csr_write_1(sc, AUE_GPIO0,
685 	    AUE_GPIO_OUT0 | AUE_GPIO_SEL0);
686   	aue_csr_write_1(sc, AUE_GPIO0,
687 	    AUE_GPIO_OUT0 | AUE_GPIO_SEL0 | AUE_GPIO_SEL1);
688 
689 	/* Grrr. LinkSys has to be different from everyone else. */
690 	if (sc->aue_linksys) {
691 		aue_csr_write_1(sc, AUE_GPIO0,
692 		    AUE_GPIO_SEL0 | AUE_GPIO_SEL1);
693 		aue_csr_write_1(sc, AUE_GPIO0,
694 		    AUE_GPIO_SEL0 | AUE_GPIO_SEL1 | AUE_GPIO_OUT0);
695 	}
696 
697 	/* Wait a little while for the chip to get its brains in order. */
698 	delay(10000);		/* XXX */
699 }
700 
701 Static struct aue_type *
702 aue_lookup(u_int16_t vendor, u_int16_t product)
703 {
704 	struct aue_type	*t;
705 
706 	for (t = aue_devs; t->aue_vid != 0; t++)
707 		if (vendor == t->aue_vid && product == t->aue_did)
708 			return (t);
709 	return (NULL);
710 }
711 
712 /*
713  * Probe for a Pegasus chip.
714  */
715 USB_MATCH(aue)
716 {
717 	USB_MATCH_START(aue, uaa);
718 
719 	if (uaa->iface != NULL)
720 		return (UMATCH_NONE);
721 
722 	return (aue_lookup(uaa->vendor, uaa->product) != NULL ?
723 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
724 }
725 
726 /*
727  * Attach the interface. Allocate softc structures, do ifmedia
728  * setup and ethernet/BPF attach.
729  */
730 USB_ATTACH(aue)
731 {
732 	USB_ATTACH_START(aue, sc, uaa);
733 	char			devinfo[1024];
734 	int			s;
735 	u_char			eaddr[ETHER_ADDR_LEN];
736 	struct ifnet		*ifp;
737 	struct mii_data		*mii;
738 	usbd_device_handle	dev = uaa->device;
739 	usbd_interface_handle	iface;
740 	usbd_status		err;
741 	usb_interface_descriptor_t	*id;
742 	usb_endpoint_descriptor_t	*ed;
743 	int			i;
744 
745 #ifdef __FreeBSD__
746 	bzero(sc, sizeof(struct aue_softc));
747 #endif
748 
749 	DPRINTFN(5,(" : aue_attach: sc=%p", sc));
750 
751 	usbd_devinfo(dev, 0, devinfo);
752 	USB_ATTACH_SETUP;
753 	printf("%s: %s\n", USBDEVNAME(sc->aue_dev), devinfo);
754 
755 	err = usbd_set_config_no(dev, AUE_CONFIG_NO, 0);
756 	if (err) {
757 		printf("%s: setting config no failed\n",
758 		    USBDEVNAME(sc->aue_dev));
759 		USB_ATTACH_ERROR_RETURN;
760 	}
761 
762 	err = usbd_device2interface_handle(dev, AUE_IFACE_IDX, &iface);
763 	if (err) {
764 		printf("%s: getting interface handle failed\n",
765 		    USBDEVNAME(sc->aue_dev));
766 		USB_ATTACH_ERROR_RETURN;
767 	}
768 
769 	sc->aue_linksys = aue_lookup(uaa->vendor, uaa->product)->aue_linksys;
770 
771 	sc->aue_udev = dev;
772 	sc->aue_iface = iface;
773 	sc->aue_product = uaa->product;
774 	sc->aue_vendor = uaa->vendor;
775 
776 	id = usbd_get_interface_descriptor(iface);
777 
778 	/* Find endpoints. */
779 	for (i = 0; i < id->bNumEndpoints; i++) {
780 		ed = usbd_interface2endpoint_descriptor(iface, i);
781 		if (ed == NULL) {
782 			printf("%s: couldn't get endpoint descriptor %d\n",
783 			    USBDEVNAME(sc->aue_dev), i);
784 			USB_ATTACH_ERROR_RETURN;
785 		}
786 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
787 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
788 			sc->aue_ed[AUE_ENDPT_RX] = ed->bEndpointAddress;
789 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
790 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
791 			sc->aue_ed[AUE_ENDPT_TX] = ed->bEndpointAddress;
792 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
793 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
794 			sc->aue_ed[AUE_ENDPT_INTR] = ed->bEndpointAddress;
795 		}
796 	}
797 
798 	if (sc->aue_ed[AUE_ENDPT_RX] == 0 || sc->aue_ed[AUE_ENDPT_TX] == 0 ||
799 	    sc->aue_ed[AUE_ENDPT_INTR] == 0) {
800 		printf("%s: missing endpoint\n", USBDEVNAME(sc->aue_dev));
801 		USB_ATTACH_ERROR_RETURN;
802 	}
803 
804 
805 	s = splimp();
806 
807 	/* Reset the adapter. */
808 	aue_reset(sc);
809 
810 	/*
811 	 * Get station address from the EEPROM.
812 	 */
813 	aue_read_mac(sc, eaddr);
814 
815 	/*
816 	 * A Pegasus chip was detected. Inform the world.
817 	 */
818 	ifp = GET_IFP(sc);
819 #if defined(__FreeBSD__)
820 	printf("%s: Ethernet address: %6D\n", USBDEVNAME(sc->aue_dev),
821 	    eaddr, ":");
822 
823 	bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
824 
825 	ifp->if_softc = sc;
826 	ifp->if_unit = sc->aue_unit;
827 	ifp->if_name = "aue";
828 	ifp->if_mtu = ETHERMTU;
829 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
830 	ifp->if_ioctl = aue_ioctl;
831 	ifp->if_output = ether_output;
832 	ifp->if_start = aue_start;
833 	ifp->if_watchdog = aue_watchdog;
834 	ifp->if_init = aue_init;
835 	ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
836 
837 	/*
838 	 * Do MII setup.
839 	 * NOTE: Doing this causes child devices to be attached to us,
840 	 * which we would normally disconnect at in the detach routine
841 	 * using device_delete_child(). However the USB code is set up
842 	 * such that when this driver is removed, all childred devices
843 	 * are removed as well. In effect, the USB code ends up detaching
844 	 * all of our children for us, so we don't have to do is ourselves
845 	 * in aue_detach(). It's important to point this out since if
846 	 * we *do* try to detach the child devices ourselves, we will
847 	 * end up getting the children deleted twice, which will crash
848 	 * the system.
849 	 */
850 	if (mii_phy_probe(self, &sc->aue_miibus,
851 	    aue_ifmedia_upd, aue_ifmedia_sts)) {
852 		printf("%s: MII without any PHY!\n", USBDEVNAME(sc->aue_dev));
853 		splx(s);
854 		USB_ATTACH_ERROR_RETURN;
855 	}
856 
857 	aue_qdat.ifp = ifp;
858 	aue_qdat.if_rxstart = aue_rxstart;
859 
860 	/*
861 	 * Call MI attach routines.
862 	 */
863 	if_attach(ifp);
864 	ether_ifattach(ifp);
865 	bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header));
866 
867 	usb_register_netisr();
868 
869 #elif defined(__NetBSD__) || defined(__OpenBSD__)
870 
871 	printf("%s: Ethernet address %s\n", USBDEVNAME(sc->aue_dev),
872 	    ether_sprintf(eaddr));
873 
874 	/* Initialize interface info.*/
875 	ifp->if_softc = sc;
876 	ifp->if_mtu = ETHERMTU;
877 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
878 	ifp->if_ioctl = aue_ioctl;
879 	ifp->if_start = aue_start;
880 	ifp->if_watchdog = aue_watchdog;
881 #if defined(__OpenBSD__)
882 	ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
883 #endif
884 	strncpy(ifp->if_xname, USBDEVNAME(sc->aue_dev), IFNAMSIZ);
885 
886 	/* Initialize MII/media info. */
887 	mii = &sc->aue_mii;
888 	mii->mii_ifp = ifp;
889 	mii->mii_readreg = aue_miibus_readreg;
890 	mii->mii_writereg = aue_miibus_writereg;
891 	mii->mii_statchg = aue_miibus_statchg;
892 	ifmedia_init(&mii->mii_media, 0, aue_ifmedia_upd, aue_ifmedia_sts);
893 	mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0);
894 	if (LIST_FIRST(&mii->mii_phys) == NULL) {
895 		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
896 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
897 	} else
898 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
899 
900 	/* Attach the interface. */
901 	if_attach(ifp);
902 	Ether_ifattach(ifp, eaddr);
903 
904 #if NBPFILTER > 0
905 	bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB,
906 		  sizeof(struct ether_header));
907 #endif
908 #if NRND > 0
909 	rnd_attach_source(&sc->rnd_source, USBDEVNAME(sc->aue_dev),
910 	    RND_TYPE_NET, 0);
911 #endif
912 
913 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
914 
915 	usb_callout_init(sc->aue_stat_ch);
916 
917 	sc->aue_attached = 1;
918 	splx(s);
919 
920 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->aue_udev,
921 			   USBDEV(sc->aue_dev));
922 
923 	USB_ATTACH_SUCCESS_RETURN;
924 }
925 
926 USB_DETACH(aue)
927 {
928 	USB_DETACH_START(aue, sc);
929 	struct ifnet		*ifp = GET_IFP(sc);
930 	int			s;
931 
932 	DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
933 
934 	s = splusb();
935 
936 	usb_uncallout(sc->aue_stat_ch, aue_tick, sc);
937 
938 	if (!sc->aue_attached) {
939 		/* Detached before attached finished, so just bail out. */
940 		splx(s);
941 		return (0);
942 	}
943 
944 	if (ifp->if_flags & IFF_RUNNING)
945 		aue_stop(sc);
946 
947 #if defined(__NetBSD__)
948 #if NRND > 0
949 	rnd_detach_source(&sc->rnd_source);
950 #endif
951 	mii_detach(&sc->aue_mii, MII_PHY_ANY, MII_OFFSET_ANY);
952 	ifmedia_delete_instance(&sc->aue_mii.mii_media, IFM_INST_ANY);
953 #if NBPFILTER > 0
954 	bpfdetach(ifp);
955 #endif
956 	ether_ifdetach(ifp);
957 #endif /* __NetBSD__ */
958 
959 	if_detach(ifp);
960 
961 #ifdef DIAGNOSTIC
962 	if (sc->aue_ep[AUE_ENDPT_TX] != NULL ||
963 	    sc->aue_ep[AUE_ENDPT_RX] != NULL ||
964 	    sc->aue_ep[AUE_ENDPT_INTR] != NULL)
965 		printf("%s: detach has active endpoints\n",
966 		       USBDEVNAME(sc->aue_dev));
967 #endif
968 
969 	sc->aue_attached = 0;
970 	splx(s);
971 
972 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->aue_udev,
973 			   USBDEV(sc->aue_dev));
974 
975 	return (0);
976 }
977 
978 #if defined(__NetBSD__) || defined(__OpenBSD__)
979 int
980 aue_activate(device_ptr_t self, enum devact act)
981 {
982 	struct aue_softc *sc = (struct aue_softc *)self;
983 
984 	DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
985 
986 	switch (act) {
987 	case DVACT_ACTIVATE:
988 		return (EOPNOTSUPP);
989 		break;
990 
991 	case DVACT_DEACTIVATE:
992 		if_deactivate(&sc->aue_ec.ec_if);
993 		sc->aue_dying = 1;
994 		break;
995 	}
996 	return (0);
997 }
998 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
999 
1000 /*
1001  * Initialize an RX descriptor and attach an MBUF cluster.
1002  */
1003 Static int
1004 aue_newbuf(struct aue_softc *sc, struct aue_chain *c, struct mbuf *m)
1005 {
1006 	struct mbuf		*m_new = NULL;
1007 
1008 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
1009 
1010 	if (m == NULL) {
1011 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
1012 		if (m_new == NULL) {
1013 			printf("%s: no memory for rx list "
1014 			    "-- packet dropped!\n", USBDEVNAME(sc->aue_dev));
1015 			return (ENOBUFS);
1016 		}
1017 
1018 		MCLGET(m_new, M_DONTWAIT);
1019 		if (!(m_new->m_flags & M_EXT)) {
1020 			printf("%s: no memory for rx list "
1021 			    "-- packet dropped!\n", USBDEVNAME(sc->aue_dev));
1022 			m_freem(m_new);
1023 			return (ENOBUFS);
1024 		}
1025 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
1026 	} else {
1027 		m_new = m;
1028 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
1029 		m_new->m_data = m_new->m_ext.ext_buf;
1030 	}
1031 
1032 	m_adj(m_new, ETHER_ALIGN);
1033 	c->aue_mbuf = m_new;
1034 
1035 	return (0);
1036 }
1037 
1038 Static int
1039 aue_rx_list_init(struct aue_softc *sc)
1040 {
1041 	struct aue_cdata	*cd;
1042 	struct aue_chain	*c;
1043 	int			i;
1044 
1045 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1046 
1047 	cd = &sc->aue_cdata;
1048 	for (i = 0; i < AUE_RX_LIST_CNT; i++) {
1049 		c = &cd->aue_rx_chain[i];
1050 		c->aue_sc = sc;
1051 		c->aue_idx = i;
1052 		if (aue_newbuf(sc, c, NULL) == ENOBUFS)
1053 			return (ENOBUFS);
1054 		if (c->aue_xfer == NULL) {
1055 			c->aue_xfer = usbd_alloc_xfer(sc->aue_udev);
1056 			if (c->aue_xfer == NULL)
1057 				return (ENOBUFS);
1058 			c->aue_buf = usbd_alloc_buffer(c->aue_xfer, AUE_BUFSZ);
1059 			if (c->aue_buf == NULL)
1060 				return (ENOBUFS); /* XXX free xfer */
1061 		}
1062 	}
1063 
1064 	return (0);
1065 }
1066 
1067 Static int
1068 aue_tx_list_init(struct aue_softc *sc)
1069 {
1070 	struct aue_cdata	*cd;
1071 	struct aue_chain	*c;
1072 	int			i;
1073 
1074 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1075 
1076 	cd = &sc->aue_cdata;
1077 	for (i = 0; i < AUE_TX_LIST_CNT; i++) {
1078 		c = &cd->aue_tx_chain[i];
1079 		c->aue_sc = sc;
1080 		c->aue_idx = i;
1081 		c->aue_mbuf = NULL;
1082 		if (c->aue_xfer == NULL) {
1083 			c->aue_xfer = usbd_alloc_xfer(sc->aue_udev);
1084 			if (c->aue_xfer == NULL)
1085 				return (ENOBUFS);
1086 			c->aue_buf = usbd_alloc_buffer(c->aue_xfer, AUE_BUFSZ);
1087 			if (c->aue_buf == NULL)
1088 				return (ENOBUFS);
1089 		}
1090 	}
1091 
1092 	return (0);
1093 }
1094 
1095 Static void
1096 aue_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
1097 {
1098 	struct aue_softc	*sc = priv;
1099 	struct ifnet		*ifp = GET_IFP(sc);
1100 	struct aue_intrpkt	*p = &sc->aue_cdata.aue_ibuf;
1101 
1102 	DPRINTFN(15,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
1103 
1104 	if (sc->aue_dying)
1105 		return;
1106 
1107 	if (!(ifp->if_flags & IFF_RUNNING))
1108 		return;
1109 
1110 	if (status != USBD_NORMAL_COMPLETION) {
1111 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1112 			return;
1113 		}
1114 		sc->aue_intr_errs++;
1115 		if (usbd_ratecheck(&sc->aue_rx_notice)) {
1116 			printf("%s: %u usb errors on intr: %s\n",
1117 			    USBDEVNAME(sc->aue_dev), sc->aue_rx_errs,
1118 			    usbd_errstr(status));
1119 			sc->aue_intr_errs = 0;
1120 		}
1121 		if (status == USBD_STALLED)
1122 			usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_RX]);
1123 		return;
1124 	}
1125 
1126 	if (p->aue_txstat0)
1127 		ifp->if_oerrors++;
1128 
1129 	if (p->aue_txstat0 & (AUE_TXSTAT0_LATECOLL | AUE_TXSTAT0_EXCESSCOLL))
1130 		ifp->if_collisions++;
1131 }
1132 
1133 #if defined(__FreeBSD__)
1134 Static void
1135 aue_rxstart(struct ifnet *ifp)
1136 {
1137 	struct aue_softc	*sc;
1138 	struct aue_chain	*c;
1139 
1140 	sc = ifp->if_softc;
1141 	c = &sc->aue_cdata.aue_rx_chain[sc->aue_cdata.aue_rx_prod];
1142 
1143 	if (aue_newbuf(sc, c, NULL) == ENOBUFS) {
1144 		ifp->if_ierrors++;
1145 		return;
1146 	}
1147 
1148 	/* Setup new transfer. */
1149 	usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_RX],
1150 	    c, mtod(c->aue_mbuf, char *), AUE_BUFSZ, USBD_SHORT_XFER_OK,
1151 	    USBD_NO_TIMEOUT, aue_rxeof);
1152 	usbd_transfer(c->aue_xfer);
1153 }
1154 #endif
1155 
1156 /*
1157  * A frame has been uploaded: pass the resulting mbuf chain up to
1158  * the higher level protocols.
1159  */
1160 Static void
1161 aue_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
1162 {
1163 	struct aue_chain	*c = priv;
1164 	struct aue_softc	*sc = c->aue_sc;
1165 	struct ifnet		*ifp = GET_IFP(sc);
1166 	struct mbuf		*m;
1167 	u_int32_t		total_len;
1168 	struct aue_rxpkt	r;
1169 #if defined(__NetBSD__) || defined(__OpenBSD__)
1170 	int			s;
1171 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
1172 
1173 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
1174 
1175 	if (sc->aue_dying)
1176 		return;
1177 
1178 	if (!(ifp->if_flags & IFF_RUNNING))
1179 		return;
1180 
1181 	if (status != USBD_NORMAL_COMPLETION) {
1182 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
1183 			return;
1184 		sc->aue_rx_errs++;
1185 		if (usbd_ratecheck(&sc->aue_rx_notice)) {
1186 			printf("%s: %u usb errors on rx: %s\n",
1187 			    USBDEVNAME(sc->aue_dev), sc->aue_rx_errs,
1188 			    usbd_errstr(status));
1189 			sc->aue_rx_errs = 0;
1190 		}
1191 		if (status == USBD_STALLED)
1192 			usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_RX]);
1193 		goto done;
1194 	}
1195 
1196 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
1197 
1198 	memcpy(mtod(c->aue_mbuf, char*), c->aue_buf, total_len);
1199 
1200 	if (total_len <= 4 + ETHER_CRC_LEN) {
1201 		ifp->if_ierrors++;
1202 		goto done;
1203 	}
1204 
1205 	memcpy(&r, c->aue_buf + total_len - 4, sizeof(r));
1206 
1207 	/* Turn off all the non-error bits in the rx status word. */
1208 	r.aue_rxstat &= AUE_RXSTAT_MASK;
1209 	if (r.aue_rxstat) {
1210 		ifp->if_ierrors++;
1211 		goto done;
1212 	}
1213 
1214 	/* No errors; receive the packet. */
1215 	m = c->aue_mbuf;
1216 	total_len -= ETHER_CRC_LEN + 4;
1217 	m->m_pkthdr.len = m->m_len = total_len;
1218 	ifp->if_ipackets++;
1219 
1220 #if defined(__FreeBSD__)
1221 	m->m_pkthdr.rcvif = (struct ifnet *)&kue_qdat;
1222 	/* Put the packet on the special USB input queue. */
1223 	usb_ether_input(m);
1224 
1225 	return;
1226 
1227 #elif defined(__NetBSD__) || defined(__OpenBSD__)
1228 	m->m_pkthdr.rcvif = ifp;
1229 
1230 	s = splimp();
1231 
1232 	/* XXX ugly */
1233 	if (aue_newbuf(sc, c, NULL) == ENOBUFS) {
1234 		ifp->if_ierrors++;
1235 		goto done1;
1236 	}
1237 
1238 #if NBPFILTER > 0
1239 	/*
1240 	 * Handle BPF listeners. Let the BPF user see the packet, but
1241 	 * don't pass it up to the ether_input() layer unless it's
1242 	 * a broadcast packet, multicast packet, matches our ethernet
1243 	 * address or the interface is in promiscuous mode.
1244 	 */
1245 	if (ifp->if_bpf) {
1246 #if defined(__NetBSD__)
1247 		struct ether_header *eh = mtod(m, struct ether_header *);
1248 		BPF_MTAP(ifp, m);
1249 		if ((ifp->if_flags & IFF_PROMISC) &&
1250 		    memcmp(eh->ether_dhost, LLADDR(ifp->if_sadl),
1251 			   ETHER_ADDR_LEN) &&
1252 		    !(eh->ether_dhost[0] & 1)) {
1253 			m_freem(m);
1254 			goto done1;
1255 		}
1256 #else
1257 		BPF_MTAP(ifp, m);
1258 #endif
1259 	}
1260 #endif
1261 
1262 	DPRINTFN(10,("%s: %s: deliver %d\n", USBDEVNAME(sc->aue_dev),
1263 		    __FUNCTION__, m->m_len));
1264 	IF_INPUT(ifp, m);
1265  done1:
1266 	splx(s);
1267 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
1268 
1269  done:
1270 
1271 	/* Setup new transfer. */
1272 	usbd_setup_xfer(xfer, sc->aue_ep[AUE_ENDPT_RX],
1273 	    c, c->aue_buf, AUE_BUFSZ,
1274 	    USBD_SHORT_XFER_OK | USBD_NO_COPY,
1275 	    USBD_NO_TIMEOUT, aue_rxeof);
1276 	usbd_transfer(xfer);
1277 
1278 	DPRINTFN(10,("%s: %s: start rx\n", USBDEVNAME(sc->aue_dev),
1279 		    __FUNCTION__));
1280 }
1281 
1282 /*
1283  * A frame was downloaded to the chip. It's safe for us to clean up
1284  * the list buffers.
1285  */
1286 
1287 Static void
1288 aue_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
1289 {
1290 	struct aue_chain	*c = priv;
1291 	struct aue_softc	*sc = c->aue_sc;
1292 	struct ifnet		*ifp = GET_IFP(sc);
1293 	int			s;
1294 
1295 	if (sc->aue_dying)
1296 		return;
1297 
1298 	s = splimp();
1299 
1300 	DPRINTFN(10,("%s: %s: enter status=%d\n", USBDEVNAME(sc->aue_dev),
1301 		    __FUNCTION__, status));
1302 
1303 	ifp->if_timer = 0;
1304 	ifp->if_flags &= ~IFF_OACTIVE;
1305 
1306 	if (status != USBD_NORMAL_COMPLETION) {
1307 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1308 			splx(s);
1309 			return;
1310 		}
1311 		ifp->if_oerrors++;
1312 		printf("%s: usb error on tx: %s\n", USBDEVNAME(sc->aue_dev),
1313 		    usbd_errstr(status));
1314 		if (status == USBD_STALLED)
1315 			usbd_clear_endpoint_stall(sc->aue_ep[AUE_ENDPT_TX]);
1316 		splx(s);
1317 		return;
1318 	}
1319 
1320 	ifp->if_opackets++;
1321 
1322 #if defined(__FreeBSD__)
1323 	c->aue_mbuf->m_pkthdr.rcvif = ifp;
1324 	usb_tx_done(c->aue_mbuf);
1325   	c->aue_mbuf = NULL;
1326 #elif defined(__NetBSD__) || defined(__OpenBSD__)
1327 	m_freem(c->aue_mbuf);
1328 	c->aue_mbuf = NULL;
1329 
1330 	if (ifp->if_snd.ifq_head != NULL)
1331 		aue_start(ifp);
1332 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
1333 
1334 	splx(s);
1335 }
1336 
1337 Static void
1338 aue_tick(void *xsc)
1339 {
1340 	struct aue_softc	*sc = xsc;
1341 	struct ifnet		*ifp;
1342 	struct mii_data		*mii;
1343 	int			s;
1344 
1345 	DPRINTFN(15,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
1346 
1347 	if (sc == NULL)
1348 		return;
1349 
1350 	if (sc->aue_dying)
1351 		return;
1352 
1353 	ifp = GET_IFP(sc);
1354 	mii = GET_MII(sc);
1355 	if (mii == NULL)
1356 		return;
1357 
1358 	s = splimp();
1359 
1360 	mii_tick(mii);
1361 	if (!sc->aue_link) {
1362 		mii_pollstat(mii);
1363 		if (mii->mii_media_status & IFM_ACTIVE &&
1364 		    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1365 			DPRINTFN(2,("%s: %s: got link\n",
1366 				    USBDEVNAME(sc->aue_dev),__FUNCTION__));
1367 			sc->aue_link++;
1368 			if (ifp->if_snd.ifq_head != NULL)
1369 				aue_start(ifp);
1370 		}
1371 	}
1372 
1373 	usb_callout(sc->aue_stat_ch, hz, aue_tick, sc);
1374 
1375 	splx(s);
1376 }
1377 
1378 Static int
1379 aue_send(struct aue_softc *sc, struct mbuf *m, int idx)
1380 {
1381 	int			total_len;
1382 	struct aue_chain	*c;
1383 	usbd_status		err;
1384 
1385 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__FUNCTION__));
1386 
1387 	c = &sc->aue_cdata.aue_tx_chain[idx];
1388 
1389 	/*
1390 	 * Copy the mbuf data into a contiguous buffer, leaving two
1391 	 * bytes at the beginning to hold the frame length.
1392 	 */
1393 	m_copydata(m, 0, m->m_pkthdr.len, c->aue_buf + 2);
1394 	c->aue_mbuf = m;
1395 
1396 	/*
1397 	 * The ADMtek documentation says that the packet length is
1398 	 * supposed to be specified in the first two bytes of the
1399 	 * transfer, however it actually seems to ignore this info
1400 	 * and base the frame size on the bulk transfer length.
1401 	 */
1402 	c->aue_buf[0] = (u_int8_t)m->m_pkthdr.len;
1403 	c->aue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8);
1404 	total_len = m->m_pkthdr.len + 2;
1405 
1406 	usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_TX],
1407 	    c, c->aue_buf, total_len, USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
1408 	    AUE_TX_TIMEOUT, aue_txeof);
1409 
1410 	/* Transmit */
1411 	err = usbd_transfer(c->aue_xfer);
1412 	if (err != USBD_IN_PROGRESS) {
1413 		printf("%s: aue_send error=%s\n", USBDEVNAME(sc->aue_dev),
1414 		       usbd_errstr(err));
1415 		aue_stop(sc);
1416 		return (EIO);
1417 	}
1418 	DPRINTFN(5,("%s: %s: send %d bytes\n", USBDEVNAME(sc->aue_dev),
1419 		    __FUNCTION__, total_len));
1420 
1421 	sc->aue_cdata.aue_tx_cnt++;
1422 
1423 	return (0);
1424 }
1425 
1426 Static void
1427 aue_start(struct ifnet *ifp)
1428 {
1429 	struct aue_softc	*sc = ifp->if_softc;
1430 	struct mbuf		*m_head = NULL;
1431 
1432 	DPRINTFN(5,("%s: %s: enter, link=%d\n", USBDEVNAME(sc->aue_dev),
1433 		    __FUNCTION__, sc->aue_link));
1434 
1435 	if (sc->aue_dying)
1436 		return;
1437 
1438 	if (!sc->aue_link)
1439 		return;
1440 
1441 	if (ifp->if_flags & IFF_OACTIVE)
1442 		return;
1443 
1444 	IF_DEQUEUE(&ifp->if_snd, m_head);
1445 	if (m_head == NULL)
1446 		return;
1447 
1448 	if (aue_send(sc, m_head, 0)) {
1449 		IF_PREPEND(&ifp->if_snd, m_head);
1450 		ifp->if_flags |= IFF_OACTIVE;
1451 		return;
1452 	}
1453 
1454 #if NBPFILTER > 0
1455 	/*
1456 	 * If there's a BPF listener, bounce a copy of this frame
1457 	 * to him.
1458 	 */
1459 	if (ifp->if_bpf)
1460 		BPF_MTAP(ifp, m_head);
1461 #endif
1462 
1463 	ifp->if_flags |= IFF_OACTIVE;
1464 
1465 	/*
1466 	 * Set a timeout in case the chip goes out to lunch.
1467 	 */
1468 	ifp->if_timer = 5;
1469 }
1470 
1471 Static void
1472 aue_init(void *xsc)
1473 {
1474 	struct aue_softc	*sc = xsc;
1475 	struct ifnet		*ifp = GET_IFP(sc);
1476 	struct mii_data		*mii = GET_MII(sc);
1477 	int			i, s;
1478 	u_char			*eaddr;
1479 
1480 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1481 
1482 	if (sc->aue_dying)
1483 		return;
1484 
1485 	if (ifp->if_flags & IFF_RUNNING)
1486 		return;
1487 
1488 	s = splimp();
1489 
1490 	/*
1491 	 * Cancel pending I/O and free all RX/TX buffers.
1492 	 */
1493 	aue_reset(sc);
1494 
1495 #if defined(__FreeBSD__) || defined(__OpenBSD__)
1496 	eaddr = sc->arpcom.ac_enaddr;
1497 #elif defined(__NetBSD__)
1498 	eaddr = LLADDR(ifp->if_sadl);
1499 #endif /* defined(__NetBSD__) */
1500 	for (i = 0; i < ETHER_ADDR_LEN; i++)
1501 		aue_csr_write_1(sc, AUE_PAR0 + i, eaddr[i]);
1502 
1503 	 /* If we want promiscuous mode, set the allframes bit. */
1504 	if (ifp->if_flags & IFF_PROMISC)
1505 		AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1506 	else
1507 		AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1508 
1509 	/* Init TX ring. */
1510 	if (aue_tx_list_init(sc) == ENOBUFS) {
1511 		printf("%s: tx list init failed\n", USBDEVNAME(sc->aue_dev));
1512 		splx(s);
1513 		return;
1514 	}
1515 
1516 	/* Init RX ring. */
1517 	if (aue_rx_list_init(sc) == ENOBUFS) {
1518 		printf("%s: rx list init failed\n", USBDEVNAME(sc->aue_dev));
1519 		splx(s);
1520 		return;
1521 	}
1522 
1523 	/* Load the multicast filter. */
1524 	aue_setmulti(sc);
1525 
1526 	/* Enable RX and TX */
1527 	aue_csr_write_1(sc, AUE_CTL0, AUE_CTL0_RXSTAT_APPEND | AUE_CTL0_RX_ENB);
1528 	AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_TX_ENB);
1529 	AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_EP3_CLR);
1530 
1531 	mii_mediachg(mii);
1532 
1533 	if (sc->aue_ep[AUE_ENDPT_RX] == NULL) {
1534 		if (aue_openpipes(sc)) {
1535 			splx(s);
1536 			return;
1537 		}
1538 	}
1539 
1540 	ifp->if_flags |= IFF_RUNNING;
1541 	ifp->if_flags &= ~IFF_OACTIVE;
1542 
1543 	splx(s);
1544 
1545 	usb_callout(sc->aue_stat_ch, hz, aue_tick, sc);
1546 }
1547 
1548 Static int
1549 aue_openpipes(struct aue_softc *sc)
1550 {
1551 	struct aue_chain	*c;
1552 	usbd_status		err;
1553 	int i;
1554 
1555 	/* Open RX and TX pipes. */
1556 	err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_RX],
1557 	    USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_RX]);
1558 	if (err) {
1559 		printf("%s: open rx pipe failed: %s\n",
1560 		    USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1561 		return (EIO);
1562 	}
1563 	err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_TX],
1564 	    USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_TX]);
1565 	if (err) {
1566 		printf("%s: open tx pipe failed: %s\n",
1567 		    USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1568 		return (EIO);
1569 	}
1570 	err = usbd_open_pipe_intr(sc->aue_iface, sc->aue_ed[AUE_ENDPT_INTR],
1571 	    USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_INTR], sc,
1572 	    &sc->aue_cdata.aue_ibuf, AUE_INTR_PKTLEN, aue_intr,
1573 	    AUE_INTR_INTERVAL);
1574 	if (err) {
1575 		printf("%s: open intr pipe failed: %s\n",
1576 		    USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1577 		return (EIO);
1578 	}
1579 
1580 	/* Start up the receive pipe. */
1581 	for (i = 0; i < AUE_RX_LIST_CNT; i++) {
1582 		c = &sc->aue_cdata.aue_rx_chain[i];
1583 		usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_RX],
1584 		    c, c->aue_buf, AUE_BUFSZ,
1585 		    USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT,
1586 		    aue_rxeof);
1587 		(void)usbd_transfer(c->aue_xfer); /* XXX */
1588 		DPRINTFN(5,("%s: %s: start read\n", USBDEVNAME(sc->aue_dev),
1589 			    __FUNCTION__));
1590 
1591 	}
1592 	return (0);
1593 }
1594 
1595 /*
1596  * Set media options.
1597  */
1598 Static int
1599 aue_ifmedia_upd(struct ifnet *ifp)
1600 {
1601 	struct aue_softc	*sc = ifp->if_softc;
1602 	struct mii_data		*mii = GET_MII(sc);
1603 
1604 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1605 
1606 	if (sc->aue_dying)
1607 		return (0);
1608 
1609 	sc->aue_link = 0;
1610 	if (mii->mii_instance) {
1611 		struct mii_softc	*miisc;
1612 		for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
1613 		    miisc = LIST_NEXT(miisc, mii_list))
1614 			 mii_phy_reset(miisc);
1615 	}
1616 	mii_mediachg(mii);
1617 
1618 	return (0);
1619 }
1620 
1621 /*
1622  * Report current media status.
1623  */
1624 Static void
1625 aue_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1626 {
1627 	struct aue_softc	*sc = ifp->if_softc;
1628 	struct mii_data		*mii = GET_MII(sc);
1629 
1630 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1631 
1632 	mii_pollstat(mii);
1633 	ifmr->ifm_active = mii->mii_media_active;
1634 	ifmr->ifm_status = mii->mii_media_status;
1635 }
1636 
1637 Static int
1638 aue_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
1639 {
1640 	struct aue_softc	*sc = ifp->if_softc;
1641 #if defined(__NetBSD__) || defined(__OpenBSD__)
1642 	struct ifaddr 		*ifa = (struct ifaddr *)data;
1643 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
1644 	struct ifreq		*ifr = (struct ifreq *)data;
1645 	struct mii_data		*mii;
1646 	int			s, error = 0;
1647 
1648 	if (sc->aue_dying)
1649 		return (EIO);
1650 
1651 	s = splimp();
1652 
1653 	switch(command) {
1654 #if defined(__FreeBSD__)
1655 	case SIOCSIFADDR:
1656 	case SIOCGIFADDR:
1657 	case SIOCSIFMTU:
1658 		error = ether_ioctl(ifp, command, data);
1659 		break;
1660 #elif defined(__NetBSD__) || defined(__OpenBSD__)
1661 	case SIOCSIFADDR:
1662 		ifp->if_flags |= IFF_UP;
1663 		aue_init(sc);
1664 
1665 		switch (ifa->ifa_addr->sa_family) {
1666 #ifdef INET
1667 		case AF_INET:
1668 #if defined(__NetBSD__)
1669 			arp_ifinit(ifp, ifa);
1670 #else
1671 			arp_ifinit(&sc->arpcom, ifa);
1672 #endif
1673 			break;
1674 #endif /* INET */
1675 #ifdef NS
1676 		case AF_NS:
1677 		    {
1678 			struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
1679 
1680 			if (ns_nullhost(*ina))
1681 				ina->x_host = *(union ns_host *)
1682 					LLADDR(ifp->if_sadl);
1683 			else
1684 				memcpy(LLADDR(ifp->if_sadl),
1685 				       ina->x_host.c_host,
1686 				       ifp->if_addrlen);
1687 			break;
1688 		    }
1689 #endif /* NS */
1690 		}
1691 		break;
1692 
1693 	case SIOCSIFMTU:
1694 		if (ifr->ifr_mtu > ETHERMTU)
1695 			error = EINVAL;
1696 		else
1697 			ifp->if_mtu = ifr->ifr_mtu;
1698 		break;
1699 
1700 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
1701 	case SIOCSIFFLAGS:
1702 		if (ifp->if_flags & IFF_UP) {
1703 			if (ifp->if_flags & IFF_RUNNING &&
1704 			    ifp->if_flags & IFF_PROMISC &&
1705 			    !(sc->aue_if_flags & IFF_PROMISC)) {
1706 				AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1707 			} else if (ifp->if_flags & IFF_RUNNING &&
1708 			    !(ifp->if_flags & IFF_PROMISC) &&
1709 			    sc->aue_if_flags & IFF_PROMISC) {
1710 				AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1711 			} else if (!(ifp->if_flags & IFF_RUNNING))
1712 				aue_init(sc);
1713 		} else {
1714 			if (ifp->if_flags & IFF_RUNNING)
1715 				aue_stop(sc);
1716 		}
1717 		sc->aue_if_flags = ifp->if_flags;
1718 		error = 0;
1719 		break;
1720 	case SIOCADDMULTI:
1721 	case SIOCDELMULTI:
1722 		aue_setmulti(sc);
1723 		error = 0;
1724 		break;
1725 	case SIOCGIFMEDIA:
1726 	case SIOCSIFMEDIA:
1727 		mii = GET_MII(sc);
1728 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1729 		break;
1730 	default:
1731 		error = EINVAL;
1732 		break;
1733 	}
1734 
1735 	splx(s);
1736 
1737 	return (error);
1738 }
1739 
1740 Static void
1741 aue_watchdog(struct ifnet *ifp)
1742 {
1743 	struct aue_softc	*sc = ifp->if_softc;
1744 
1745 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1746 
1747 	ifp->if_oerrors++;
1748 	printf("%s: watchdog timeout\n", USBDEVNAME(sc->aue_dev));
1749 
1750 	/*
1751 	 * The polling business is a kludge to avoid allowing the
1752 	 * USB code to call tsleep() in usbd_delay_ms(), which will
1753 	 * kill us since the watchdog routine is invoked from
1754 	 * interrupt context.
1755 	 */
1756 	usbd_set_polling(sc->aue_udev, 1);
1757 	aue_stop(sc);
1758 	aue_init(sc);
1759 	usbd_set_polling(sc->aue_udev, 0);
1760 
1761 	if (ifp->if_snd.ifq_head != NULL)
1762 		aue_start(ifp);
1763 }
1764 
1765 /*
1766  * Stop the adapter and free any mbufs allocated to the
1767  * RX and TX lists.
1768  */
1769 Static void
1770 aue_stop(struct aue_softc *sc)
1771 {
1772 	usbd_status		err;
1773 	struct ifnet		*ifp;
1774 	int			i;
1775 
1776 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1777 
1778 	ifp = GET_IFP(sc);
1779 	ifp->if_timer = 0;
1780 
1781 	aue_csr_write_1(sc, AUE_CTL0, 0);
1782 	aue_csr_write_1(sc, AUE_CTL1, 0);
1783 	aue_reset(sc);
1784 	usb_uncallout(sc->aue_stat_ch, aue_tick, sc);
1785 
1786 	/* Stop transfers. */
1787 	if (sc->aue_ep[AUE_ENDPT_RX] != NULL) {
1788 		err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_RX]);
1789 		if (err) {
1790 			printf("%s: abort rx pipe failed: %s\n",
1791 			    USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1792 		}
1793 		err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_RX]);
1794 		if (err) {
1795 			printf("%s: close rx pipe failed: %s\n",
1796 			    USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1797 		}
1798 		sc->aue_ep[AUE_ENDPT_RX] = NULL;
1799 	}
1800 
1801 	if (sc->aue_ep[AUE_ENDPT_TX] != NULL) {
1802 		err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_TX]);
1803 		if (err) {
1804 			printf("%s: abort tx pipe failed: %s\n",
1805 			    USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1806 		}
1807 		err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_TX]);
1808 		if (err) {
1809 			printf("%s: close tx pipe failed: %s\n",
1810 			    USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1811 		}
1812 		sc->aue_ep[AUE_ENDPT_TX] = NULL;
1813 	}
1814 
1815 	if (sc->aue_ep[AUE_ENDPT_INTR] != NULL) {
1816 		err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
1817 		if (err) {
1818 			printf("%s: abort intr pipe failed: %s\n",
1819 			    USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1820 		}
1821 		err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
1822 		if (err) {
1823 			printf("%s: close intr pipe failed: %s\n",
1824 			    USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1825 		}
1826 		sc->aue_ep[AUE_ENDPT_INTR] = NULL;
1827 	}
1828 
1829 	/* Free RX resources. */
1830 	for (i = 0; i < AUE_RX_LIST_CNT; i++) {
1831 		if (sc->aue_cdata.aue_rx_chain[i].aue_mbuf != NULL) {
1832 			m_freem(sc->aue_cdata.aue_rx_chain[i].aue_mbuf);
1833 			sc->aue_cdata.aue_rx_chain[i].aue_mbuf = NULL;
1834 		}
1835 		if (sc->aue_cdata.aue_rx_chain[i].aue_xfer != NULL) {
1836 			usbd_free_xfer(sc->aue_cdata.aue_rx_chain[i].aue_xfer);
1837 			sc->aue_cdata.aue_rx_chain[i].aue_xfer = NULL;
1838 		}
1839 	}
1840 
1841 	/* Free TX resources. */
1842 	for (i = 0; i < AUE_TX_LIST_CNT; i++) {
1843 		if (sc->aue_cdata.aue_tx_chain[i].aue_mbuf != NULL) {
1844 			m_freem(sc->aue_cdata.aue_tx_chain[i].aue_mbuf);
1845 			sc->aue_cdata.aue_tx_chain[i].aue_mbuf = NULL;
1846 		}
1847 		if (sc->aue_cdata.aue_tx_chain[i].aue_xfer != NULL) {
1848 			usbd_free_xfer(sc->aue_cdata.aue_tx_chain[i].aue_xfer);
1849 			sc->aue_cdata.aue_tx_chain[i].aue_xfer = NULL;
1850 		}
1851 	}
1852 
1853 	sc->aue_link = 0;
1854 
1855 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1856 }
1857 
1858 #ifdef __FreeBSD__
1859 /*
1860  * Stop all chip I/O so that the kernel's probe routines don't
1861  * get confused by errant DMAs when rebooting.
1862  */
1863 Static void
1864 aue_shutdown(device_ptr_t dev)
1865 {
1866 	struct aue_softc	*sc = USBGETSOFTC(dev);
1867 
1868 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __FUNCTION__));
1869 
1870 	aue_reset(sc);
1871 	aue_stop(sc);
1872 }
1873 #endif
1874