xref: /openbsd-src/sys/dev/usb/if_aue.c (revision 850e275390052b330d93020bf619a739a3c277ac)
1 /*	$OpenBSD: if_aue.c,v 1.72 2008/09/10 14:01:23 blambert Exp $ */
2 /*	$NetBSD: if_aue.c,v 1.82 2003/03/05 17:37:36 shiba Exp $	*/
3 /*
4  * Copyright (c) 1997, 1998, 1999, 2000
5  *	Bill Paul <wpaul@ee.columbia.edu>.  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  * $FreeBSD: src/sys/dev/usb/if_aue.c,v 1.11 2000/01/14 01:36:14 wpaul Exp $
35  */
36 
37 /*
38  * ADMtek AN986 Pegasus and AN8511 Pegasus II USB to ethernet driver.
39  * Datasheet is available from http://www.admtek.com.tw.
40  *
41  * Written by Bill Paul <wpaul@ee.columbia.edu>
42  * Electrical Engineering Department
43  * Columbia University, New York City
44  */
45 
46 /*
47  * The Pegasus chip uses four USB "endpoints" to provide 10/100 ethernet
48  * support: the control endpoint for reading/writing registers, burst
49  * read endpoint for packet reception, burst write for packet transmission
50  * and one for "interrupts." The chip uses the same RX filter scheme
51  * as the other ADMtek ethernet parts: one perfect filter entry for the
52  * the station address and a 64-bit multicast hash table. The chip supports
53  * both MII and HomePNA attachments.
54  *
55  * Since the maximum data transfer speed of USB is supposed to be 12Mbps,
56  * you're never really going to get 100Mbps speeds from this device. I
57  * think the idea is to allow the device to connect to 10 or 100Mbps
58  * networks, not necessarily to provide 100Mbps performance. Also, since
59  * the controller uses an external PHY chip, it's possible that board
60  * designers might simply choose a 10Mbps PHY.
61  *
62  * Registers are accessed using usbd_do_request(). Packet transfers are
63  * done using usbd_transfer() and friends.
64  */
65 
66 /*
67  * Ported to NetBSD and somewhat rewritten by Lennart Augustsson.
68  */
69 
70 /*
71  * TODO:
72  * better error messages from rxstat
73  * split out if_auevar.h
74  * add thread to avoid register reads from interrupt context
75  * more error checks
76  * investigate short rx problem
77  * proper cleanup on errors
78  */
79 
80 #include "bpfilter.h"
81 
82 #include <sys/param.h>
83 #include <sys/systm.h>
84 #include <sys/sockio.h>
85 #include <sys/rwlock.h>
86 #include <sys/mbuf.h>
87 #include <sys/kernel.h>
88 #include <sys/proc.h>
89 #include <sys/socket.h>
90 
91 #include <sys/device.h>
92 
93 #include <net/if.h>
94 #include <net/if_dl.h>
95 #include <net/if_media.h>
96 
97 #if NBPFILTER > 0
98 #include <net/bpf.h>
99 #endif
100 
101 #ifdef INET
102 #include <netinet/in.h>
103 #include <netinet/in_systm.h>
104 #include <netinet/in_var.h>
105 #include <netinet/ip.h>
106 #include <netinet/if_ether.h>
107 #endif
108 
109 #include <dev/mii/mii.h>
110 #include <dev/mii/miivar.h>
111 
112 #include <dev/usb/usb.h>
113 #include <dev/usb/usbdi.h>
114 #include <dev/usb/usbdi_util.h>
115 #include <dev/usb/usbdevs.h>
116 
117 #include <dev/usb/if_auereg.h>
118 
119 #ifdef AUE_DEBUG
120 #define DPRINTF(x)	do { if (auedebug) printf x; } while (0)
121 #define DPRINTFN(n,x)	do { if (auedebug >= (n)) printf x; } while (0)
122 int	auedebug = 0;
123 #else
124 #define DPRINTF(x)
125 #define DPRINTFN(n,x)
126 #endif
127 
128 /*
129  * Various supported device vendors/products.
130  */
131 struct aue_type {
132 	struct usb_devno	aue_dev;
133 	u_int16_t		aue_flags;
134 #define LSYS	0x0001		/* use Linksys reset */
135 #define PNA	0x0002		/* has Home PNA */
136 #define PII	0x0004		/* Pegasus II chip */
137 };
138 
139 const struct aue_type aue_devs[] = {
140  {{ USB_VENDOR_3COM,		USB_PRODUCT_3COM_3C460B},	  PII },
141  {{ USB_VENDOR_ABOCOM,		USB_PRODUCT_ABOCOM_XX1},	  PNA|PII },
142  {{ USB_VENDOR_ABOCOM,		USB_PRODUCT_ABOCOM_XX2},	  PII },
143  {{ USB_VENDOR_ABOCOM,		USB_PRODUCT_ABOCOM_UFE1000},	  LSYS },
144  {{ USB_VENDOR_ABOCOM,		USB_PRODUCT_ABOCOM_XX4},	  PNA },
145  {{ USB_VENDOR_ABOCOM,		USB_PRODUCT_ABOCOM_XX5},	  PNA },
146  {{ USB_VENDOR_ABOCOM,		USB_PRODUCT_ABOCOM_XX6},	  PII },
147  {{ USB_VENDOR_ABOCOM,		USB_PRODUCT_ABOCOM_XX7},	  PII },
148  {{ USB_VENDOR_ABOCOM,		USB_PRODUCT_ABOCOM_XX8},	  PII },
149  {{ USB_VENDOR_ABOCOM,		USB_PRODUCT_ABOCOM_XX9},	  PNA },
150  {{ USB_VENDOR_ABOCOM,		USB_PRODUCT_ABOCOM_XX10},	  0 },
151  {{ USB_VENDOR_ABOCOM,		USB_PRODUCT_ABOCOM_DSB650TX_PNA}, 0 },
152  {{ USB_VENDOR_ACCTON,		USB_PRODUCT_ACCTON_USB320_EC},	  0 },
153  {{ USB_VENDOR_ACCTON,		USB_PRODUCT_ACCTON_SS1001},	  PII },
154  {{ USB_VENDOR_ADMTEK,		USB_PRODUCT_ADMTEK_PEGASUS},	  PNA },
155  {{ USB_VENDOR_ADMTEK,		USB_PRODUCT_ADMTEK_PEGASUSII},	  PII },
156  {{ USB_VENDOR_ADMTEK,		USB_PRODUCT_ADMTEK_PEGASUSII_2},  PII },
157  {{ USB_VENDOR_ADMTEK,		USB_PRODUCT_ADMTEK_PEGASUSII_3},  PII },
158  {{ USB_VENDOR_ADMTEK,		USB_PRODUCT_ADMTEK_PEGASUSII_4},  PII },
159  {{ USB_VENDOR_AEI,		USB_PRODUCT_AEI_FASTETHERNET},	  PII },
160  {{ USB_VENDOR_ALLIEDTELESYN,   USB_PRODUCT_ALLIEDTELESYN_ATUSB100}, PII },
161  {{ USB_VENDOR_ATEN,		USB_PRODUCT_ATEN_UC110T},	  PII },
162  {{ USB_VENDOR_BELKIN,		USB_PRODUCT_BELKIN_F5D5050},	  PII },
163  {{ USB_VENDOR_BILLIONTON,	USB_PRODUCT_BILLIONTON_USB100},	  0 },
164  {{ USB_VENDOR_BILLIONTON,	USB_PRODUCT_BILLIONTON_USBLP100}, PNA },
165  {{ USB_VENDOR_BILLIONTON,	USB_PRODUCT_BILLIONTON_USBEL100}, 0 },
166  {{ USB_VENDOR_BILLIONTON,	USB_PRODUCT_BILLIONTON_USBE100},  PII },
167  {{ USB_VENDOR_COREGA,		USB_PRODUCT_COREGA_FETHER_USB_TX}, 0 },
168  {{ USB_VENDOR_COREGA,		USB_PRODUCT_COREGA_FETHER_USB_TXS},PII },
169  {{ USB_VENDOR_DLINK,		USB_PRODUCT_DLINK_DSB650TX4},	  LSYS|PII },
170  {{ USB_VENDOR_DLINK,		USB_PRODUCT_DLINK_DSB650TX1},	  LSYS },
171  {{ USB_VENDOR_DLINK,		USB_PRODUCT_DLINK_DSB650TX},	  LSYS },
172  {{ USB_VENDOR_DLINK,		USB_PRODUCT_DLINK_DSB650TX_PNA},  PNA },
173  {{ USB_VENDOR_DLINK,		USB_PRODUCT_DLINK_DSB650TX3},	  LSYS|PII },
174  {{ USB_VENDOR_DLINK,		USB_PRODUCT_DLINK_DSB650TX2},	  LSYS|PII },
175  {{ USB_VENDOR_DLINK,		USB_PRODUCT_DLINK_DSB650},	  0 },
176  {{ USB_VENDOR_ELCON,		USB_PRODUCT_ELCON_PLAN},	  PNA|PII },
177  {{ USB_VENDOR_ELECOM,		USB_PRODUCT_ELECOM_LDUSB20},	  PII },
178  {{ USB_VENDOR_ELECOM,		USB_PRODUCT_ELECOM_LDUSBTX0},	  0 },
179  {{ USB_VENDOR_ELECOM,		USB_PRODUCT_ELECOM_LDUSBTX1},	  LSYS },
180  {{ USB_VENDOR_ELECOM,		USB_PRODUCT_ELECOM_LDUSBTX2},	  0 },
181  {{ USB_VENDOR_ELECOM,		USB_PRODUCT_ELECOM_LDUSBTX3},	  LSYS },
182  {{ USB_VENDOR_ELECOM,		USB_PRODUCT_ELECOM_LDUSBLTX},	  PII },
183  {{ USB_VENDOR_ELSA,		USB_PRODUCT_ELSA_USB2ETHERNET},	  0 },
184  {{ USB_VENDOR_GIGABYTE,	USB_PRODUCT_GIGABYTE_GNBR402W},	  0 },
185  {{ USB_VENDOR_HAWKING,		USB_PRODUCT_HAWKING_UF100},       PII },
186  {{ USB_VENDOR_HP,		USB_PRODUCT_HP_HN210E},           PII },
187  {{ USB_VENDOR_IODATA,		USB_PRODUCT_IODATA_USBETTX},	  0 },
188  {{ USB_VENDOR_IODATA,		USB_PRODUCT_IODATA_USBETTXS},	  PII },
189  {{ USB_VENDOR_KINGSTON,	USB_PRODUCT_KINGSTON_KNU101TX},   0 },
190  {{ USB_VENDOR_LINKSYS,		USB_PRODUCT_LINKSYS_USB10TX1},	  LSYS|PII },
191  {{ USB_VENDOR_LINKSYS,		USB_PRODUCT_LINKSYS_USB10T},	  LSYS },
192  {{ USB_VENDOR_LINKSYS,		USB_PRODUCT_LINKSYS_USB100TX},	  LSYS },
193  {{ USB_VENDOR_LINKSYS,		USB_PRODUCT_LINKSYS_USB100H1},	  LSYS|PNA },
194  {{ USB_VENDOR_LINKSYS,		USB_PRODUCT_LINKSYS_USB10TA},	  LSYS },
195  {{ USB_VENDOR_LINKSYS,		USB_PRODUCT_LINKSYS_USB10TX2},	  LSYS|PII },
196  {{ USB_VENDOR_MICROSOFT,	USB_PRODUCT_MICROSOFT_MN110},     PII },
197  {{ USB_VENDOR_MELCO, 		USB_PRODUCT_MELCO_LUATX1}, 	  0 },
198  {{ USB_VENDOR_MELCO, 		USB_PRODUCT_MELCO_LUATX5}, 	  0 },
199  {{ USB_VENDOR_MELCO, 		USB_PRODUCT_MELCO_LUA2TX5}, 	  PII },
200  {{ USB_VENDOR_MOBILITY,	USB_PRODUCT_MOBILITY_EASIDOCK},	  0 },
201  {{ USB_VENDOR_NETGEAR,		USB_PRODUCT_NETGEAR_FA101},	  PII },
202  {{ USB_VENDOR_OCT,		USB_PRODUCT_OCT_USBTOETHER},	  PII },
203  {{ USB_VENDOR_SIEMENS,		USB_PRODUCT_SIEMENS_SPEEDSTREAM}, PII },
204  {{ USB_VENDOR_SMARTBRIDGES,	USB_PRODUCT_SMARTBRIDGES_SMARTNIC},PII },
205  {{ USB_VENDOR_SMC,		USB_PRODUCT_SMC_2202USB},	  0 },
206  {{ USB_VENDOR_SMC,		USB_PRODUCT_SMC_2206USB},	  PII },
207  {{ USB_VENDOR_SOHOWARE,	USB_PRODUCT_SOHOWARE_NUB100},	  0 },
208  {{ USB_VENDOR_SOHOWARE,	USB_PRODUCT_SOHOWARE_NUB110},	  PII },
209  {{ USB_VENDOR_LOGITEC,		USB_PRODUCT_LOGITEC_LANTX},	  PII },
210 };
211 #define aue_lookup(v, p) ((struct aue_type *)usb_lookup(aue_devs, v, p))
212 
213 int aue_match(struct device *, void *, void *);
214 void aue_attach(struct device *, struct device *, void *);
215 int aue_detach(struct device *, int);
216 int aue_activate(struct device *, enum devact);
217 
218 struct cfdriver aue_cd = {
219 	NULL, "aue", DV_IFNET
220 };
221 
222 const struct cfattach aue_ca = {
223 	sizeof(struct aue_softc),
224 	aue_match,
225 	aue_attach,
226 	aue_detach,
227 	aue_activate,
228 };
229 
230 void aue_reset_pegasus_II(struct aue_softc *sc);
231 int aue_tx_list_init(struct aue_softc *);
232 int aue_rx_list_init(struct aue_softc *);
233 int aue_newbuf(struct aue_softc *, struct aue_chain *, struct mbuf *);
234 int aue_send(struct aue_softc *, struct mbuf *, int);
235 void aue_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
236 void aue_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
237 void aue_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
238 void aue_tick(void *);
239 void aue_tick_task(void *);
240 void aue_start(struct ifnet *);
241 int aue_ioctl(struct ifnet *, u_long, caddr_t);
242 void aue_init(void *);
243 void aue_shutdown(void *);
244 void aue_stop(struct aue_softc *);
245 void aue_watchdog(struct ifnet *);
246 int aue_openpipes(struct aue_softc *);
247 int aue_ifmedia_upd(struct ifnet *);
248 void aue_ifmedia_sts(struct ifnet *, struct ifmediareq *);
249 
250 int aue_eeprom_getword(struct aue_softc *, int);
251 void aue_read_mac(struct aue_softc *, u_char *);
252 int aue_miibus_readreg(struct device *, int, int);
253 void aue_miibus_writereg(struct device *, int, int, int);
254 void aue_miibus_statchg(struct device *);
255 
256 void aue_lock_mii(struct aue_softc *);
257 void aue_unlock_mii(struct aue_softc *);
258 
259 void aue_setmulti(struct aue_softc *);
260 u_int32_t aue_crc(caddr_t);
261 void aue_reset(struct aue_softc *);
262 
263 int aue_csr_read_1(struct aue_softc *, int);
264 int aue_csr_write_1(struct aue_softc *, int, int);
265 int aue_csr_read_2(struct aue_softc *, int);
266 int aue_csr_write_2(struct aue_softc *, int, int);
267 
268 #define AUE_SETBIT(sc, reg, x)				\
269 	aue_csr_write_1(sc, reg, aue_csr_read_1(sc, reg) | (x))
270 
271 #define AUE_CLRBIT(sc, reg, x)				\
272 	aue_csr_write_1(sc, reg, aue_csr_read_1(sc, reg) & ~(x))
273 
274 int
275 aue_csr_read_1(struct aue_softc *sc, int reg)
276 {
277 	usb_device_request_t	req;
278 	usbd_status		err;
279 	uByte			val = 0;
280 
281 	if (sc->aue_dying)
282 		return (0);
283 
284 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
285 	req.bRequest = AUE_UR_READREG;
286 	USETW(req.wValue, 0);
287 	USETW(req.wIndex, reg);
288 	USETW(req.wLength, 1);
289 
290 	err = usbd_do_request(sc->aue_udev, &req, &val);
291 
292 	if (err) {
293 		DPRINTF(("%s: aue_csr_read_1: reg=0x%x err=%s\n",
294 			 sc->aue_dev.dv_xname, reg, usbd_errstr(err)));
295 		return (0);
296 	}
297 
298 	return (val);
299 }
300 
301 int
302 aue_csr_read_2(struct aue_softc *sc, int reg)
303 {
304 	usb_device_request_t	req;
305 	usbd_status		err;
306 	uWord			val;
307 
308 	if (sc->aue_dying)
309 		return (0);
310 
311 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
312 	req.bRequest = AUE_UR_READREG;
313 	USETW(req.wValue, 0);
314 	USETW(req.wIndex, reg);
315 	USETW(req.wLength, 2);
316 
317 	err = usbd_do_request(sc->aue_udev, &req, &val);
318 
319 	if (err) {
320 		DPRINTF(("%s: aue_csr_read_2: reg=0x%x err=%s\n",
321 			 sc->aue_dev.dv_xname, reg, usbd_errstr(err)));
322 		return (0);
323 	}
324 
325 	return (UGETW(val));
326 }
327 
328 int
329 aue_csr_write_1(struct aue_softc *sc, int reg, int aval)
330 {
331 	usb_device_request_t	req;
332 	usbd_status		err;
333 	uByte			val;
334 
335 	if (sc->aue_dying)
336 		return (0);
337 
338 	val = aval;
339 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
340 	req.bRequest = AUE_UR_WRITEREG;
341 	USETW(req.wValue, val);
342 	USETW(req.wIndex, reg);
343 	USETW(req.wLength, 1);
344 
345 	err = usbd_do_request(sc->aue_udev, &req, &val);
346 
347 	if (err) {
348 		DPRINTF(("%s: aue_csr_write_1: reg=0x%x err=%s\n",
349 			 sc->aue_dev.dv_xname, reg, usbd_errstr(err)));
350 		return (-1);
351 	}
352 
353 	return (0);
354 }
355 
356 int
357 aue_csr_write_2(struct aue_softc *sc, int reg, int aval)
358 {
359 	usb_device_request_t	req;
360 	usbd_status		err;
361 	uWord			val;
362 
363 	if (sc->aue_dying)
364 		return (0);
365 
366 	USETW(val, aval);
367 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
368 	req.bRequest = AUE_UR_WRITEREG;
369 	USETW(req.wValue, aval);
370 	USETW(req.wIndex, reg);
371 	USETW(req.wLength, 2);
372 
373 	err = usbd_do_request(sc->aue_udev, &req, &val);
374 
375 	if (err) {
376 		DPRINTF(("%s: aue_csr_write_2: reg=0x%x err=%s\n",
377 			 sc->aue_dev.dv_xname, reg, usbd_errstr(err)));
378 		return (-1);
379 	}
380 
381 	return (0);
382 }
383 
384 /*
385  * Read a word of data stored in the EEPROM at address 'addr.'
386  */
387 int
388 aue_eeprom_getword(struct aue_softc *sc, int addr)
389 {
390 	int		i;
391 
392 	aue_csr_write_1(sc, AUE_EE_REG, addr);
393 	aue_csr_write_1(sc, AUE_EE_CTL, AUE_EECTL_READ);
394 
395 	for (i = 0; i < AUE_TIMEOUT; i++) {
396 		if (aue_csr_read_1(sc, AUE_EE_CTL) & AUE_EECTL_DONE)
397 			break;
398 	}
399 
400 	if (i == AUE_TIMEOUT) {
401 		printf("%s: EEPROM read timed out\n",
402 		    sc->aue_dev.dv_xname);
403 	}
404 
405 	return (aue_csr_read_2(sc, AUE_EE_DATA));
406 }
407 
408 /*
409  * Read the MAC from the EEPROM.  It's at offset 0.
410  */
411 void
412 aue_read_mac(struct aue_softc *sc, u_char *dest)
413 {
414 	int			i;
415 	int			off = 0;
416 	int			word;
417 
418 	DPRINTFN(5,("%s: %s: enter\n", sc->aue_dev.dv_xname, __func__));
419 
420 	for (i = 0; i < 3; i++) {
421 		word = aue_eeprom_getword(sc, off + i);
422 		dest[2 * i] = (u_char)word;
423 		dest[2 * i + 1] = (u_char)(word >> 8);
424 	}
425 }
426 
427 /* Get exclusive access to the MII registers */
428 void
429 aue_lock_mii(struct aue_softc *sc)
430 {
431 	sc->aue_refcnt++;
432 	rw_enter_write(&sc->aue_mii_lock);
433 }
434 
435 void
436 aue_unlock_mii(struct aue_softc *sc)
437 {
438 	rw_exit_write(&sc->aue_mii_lock);
439 	if (--sc->aue_refcnt < 0)
440 		usb_detach_wakeup(&sc->aue_dev);
441 }
442 
443 int
444 aue_miibus_readreg(struct device *dev, int phy, int reg)
445 {
446 	struct aue_softc	*sc = (void *)dev;
447 	int			i;
448 	u_int16_t		val;
449 
450 	if (sc->aue_dying) {
451 #ifdef DIAGNOSTIC
452 		printf("%s: dying\n", sc->aue_dev.dv_xname);
453 #endif
454 		return 0;
455 	}
456 
457 #if 0
458 	/*
459 	 * The Am79C901 HomePNA PHY actually contains
460 	 * two transceivers: a 1Mbps HomePNA PHY and a
461 	 * 10Mbps full/half duplex ethernet PHY with
462 	 * NWAY autoneg. However in the ADMtek adapter,
463 	 * only the 1Mbps PHY is actually connected to
464 	 * anything, so we ignore the 10Mbps one. It
465 	 * happens to be configured for MII address 3,
466 	 * so we filter that out.
467 	 */
468 	if (sc->aue_vendor == USB_VENDOR_ADMTEK &&
469 	    sc->aue_product == USB_PRODUCT_ADMTEK_PEGASUS) {
470 		if (phy == 3)
471 			return (0);
472 	}
473 #endif
474 
475 	aue_lock_mii(sc);
476 	aue_csr_write_1(sc, AUE_PHY_ADDR, phy);
477 	aue_csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_READ);
478 
479 	for (i = 0; i < AUE_TIMEOUT; i++) {
480 		if (aue_csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE)
481 			break;
482 	}
483 
484 	if (i == AUE_TIMEOUT) {
485 		printf("%s: MII read timed out\n", sc->aue_dev.dv_xname);
486 	}
487 
488 	val = aue_csr_read_2(sc, AUE_PHY_DATA);
489 
490 	DPRINTFN(11,("%s: %s: phy=%d reg=%d => 0x%04x\n",
491 		     sc->aue_dev.dv_xname, __func__, phy, reg, val));
492 
493 	aue_unlock_mii(sc);
494 	return (val);
495 }
496 
497 void
498 aue_miibus_writereg(struct device *dev, int phy, int reg, int data)
499 {
500 	struct aue_softc	*sc = (void *)dev;
501 	int			i;
502 
503 #if 0
504 	if (sc->aue_vendor == USB_VENDOR_ADMTEK &&
505 	    sc->aue_product == USB_PRODUCT_ADMTEK_PEGASUS) {
506 		if (phy == 3)
507 			return;
508 	}
509 #endif
510 
511 	DPRINTFN(11,("%s: %s: phy=%d reg=%d data=0x%04x\n",
512 		     sc->aue_dev.dv_xname, __func__, phy, reg, data));
513 
514 	aue_lock_mii(sc);
515 	aue_csr_write_2(sc, AUE_PHY_DATA, data);
516 	aue_csr_write_1(sc, AUE_PHY_ADDR, phy);
517 	aue_csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_WRITE);
518 
519 	for (i = 0; i < AUE_TIMEOUT; i++) {
520 		if (aue_csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE)
521 			break;
522 	}
523 
524 	if (i == AUE_TIMEOUT) {
525 		printf("%s: MII read timed out\n",
526 		    sc->aue_dev.dv_xname);
527 	}
528 	aue_unlock_mii(sc);
529 }
530 
531 void
532 aue_miibus_statchg(struct device *dev)
533 {
534 	struct aue_softc	*sc = (void *)dev;
535 	struct mii_data		*mii = GET_MII(sc);
536 
537 	DPRINTFN(5,("%s: %s: enter\n", sc->aue_dev.dv_xname, __func__));
538 
539 	aue_lock_mii(sc);
540 	AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB);
541 
542 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX) {
543 		AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL);
544 	} else {
545 		AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL);
546 	}
547 
548 	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
549 		AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX);
550 	else
551 		AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX);
552 
553 	AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB);
554 	aue_unlock_mii(sc);
555 
556 	/*
557 	 * Set the LED modes on the LinkSys adapter.
558 	 * This turns on the 'dual link LED' bin in the auxmode
559 	 * register of the Broadcom PHY.
560 	 */
561 	if (!sc->aue_dying && (sc->aue_flags & LSYS)) {
562 		u_int16_t auxmode;
563 		auxmode = aue_miibus_readreg(dev, 0, 0x1b);
564 		aue_miibus_writereg(dev, 0, 0x1b, auxmode | 0x04);
565 	}
566 	DPRINTFN(5,("%s: %s: exit\n", sc->aue_dev.dv_xname, __func__));
567 }
568 
569 #define AUE_POLY	0xEDB88320
570 #define AUE_BITS	6
571 
572 u_int32_t
573 aue_crc(caddr_t addr)
574 {
575 	u_int32_t		idx, bit, data, crc;
576 
577 	/* Compute CRC for the address value. */
578 	crc = 0xFFFFFFFF; /* initial value */
579 
580 	for (idx = 0; idx < 6; idx++) {
581 		for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1)
582 			crc = (crc >> 1) ^ (((crc ^ data) & 1) ? AUE_POLY : 0);
583 	}
584 
585 	return (crc & ((1 << AUE_BITS) - 1));
586 }
587 
588 void
589 aue_setmulti(struct aue_softc *sc)
590 {
591 	struct ifnet		*ifp;
592 	struct ether_multi	*enm;
593 	struct ether_multistep	step;
594 	u_int32_t		h = 0, i;
595 
596 	DPRINTFN(5,("%s: %s: enter\n", sc->aue_dev.dv_xname, __func__));
597 
598 	ifp = GET_IFP(sc);
599 
600 	if (ifp->if_flags & IFF_PROMISC) {
601 allmulti:
602 		ifp->if_flags |= IFF_ALLMULTI;
603 		AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
604 		return;
605 	}
606 
607 	AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
608 
609 	/* first, zot all the existing hash bits */
610 	for (i = 0; i < 8; i++)
611 		aue_csr_write_1(sc, AUE_MAR0 + i, 0);
612 
613 	/* now program new ones */
614 	ETHER_FIRST_MULTI(step, &sc->arpcom, enm);
615 	while (enm != NULL) {
616 		if (memcmp(enm->enm_addrlo,
617 		    enm->enm_addrhi, ETHER_ADDR_LEN) != 0)
618 			goto allmulti;
619 
620 		h = aue_crc(enm->enm_addrlo);
621 		AUE_SETBIT(sc, AUE_MAR + (h >> 3), 1 << (h & 0x7));
622 		ETHER_NEXT_MULTI(step, enm);
623 	}
624 
625 	ifp->if_flags &= ~IFF_ALLMULTI;
626 }
627 
628 void
629 aue_reset_pegasus_II(struct aue_softc *sc)
630 {
631 	/* Magic constants taken from Linux driver. */
632 	aue_csr_write_1(sc, AUE_REG_1D, 0);
633 	aue_csr_write_1(sc, AUE_REG_7B, 2);
634 #if 0
635 	if ((sc->aue_flags & HAS_HOME_PNA) && mii_mode)
636 		aue_csr_write_1(sc, AUE_REG_81, 6);
637 	else
638 #endif
639 		aue_csr_write_1(sc, AUE_REG_81, 2);
640 }
641 
642 void
643 aue_reset(struct aue_softc *sc)
644 {
645 	int		i;
646 
647 	DPRINTFN(2,("%s: %s: enter\n", sc->aue_dev.dv_xname, __func__));
648 
649 	AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_RESETMAC);
650 
651 	for (i = 0; i < AUE_TIMEOUT; i++) {
652 		if (!(aue_csr_read_1(sc, AUE_CTL1) & AUE_CTL1_RESETMAC))
653 			break;
654 	}
655 
656 	if (i == AUE_TIMEOUT)
657 		printf("%s: reset failed\n", sc->aue_dev.dv_xname);
658 
659 #if 0
660 	/* XXX what is mii_mode supposed to be */
661 	if (sc->aue_mii_mode && (sc->aue_flags & PNA))
662 		aue_csr_write_1(sc, AUE_GPIO1, 0x34);
663 	else
664 		aue_csr_write_1(sc, AUE_GPIO1, 0x26);
665 #endif
666 
667 	/*
668 	 * The PHY(s) attached to the Pegasus chip may be held
669 	 * in reset until we flip on the GPIO outputs. Make sure
670 	 * to set the GPIO pins high so that the PHY(s) will
671 	 * be enabled.
672 	 *
673 	 * Note: We force all of the GPIO pins low first, *then*
674 	 * enable the ones we want.
675   	 */
676 	if (sc->aue_flags & LSYS) {
677 		/* Grrr. LinkSys has to be different from everyone else. */
678 		aue_csr_write_1(sc, AUE_GPIO0,
679 		    AUE_GPIO_SEL0 | AUE_GPIO_SEL1);
680 	} else {
681 		aue_csr_write_1(sc, AUE_GPIO0,
682 		    AUE_GPIO_OUT0 | AUE_GPIO_SEL0);
683 	}
684   	aue_csr_write_1(sc, AUE_GPIO0,
685 	    AUE_GPIO_OUT0 | AUE_GPIO_SEL0 | AUE_GPIO_SEL1);
686 
687 	if (sc->aue_flags & PII)
688 		aue_reset_pegasus_II(sc);
689 
690 	/* Wait a little while for the chip to get its brains in order. */
691 	delay(10000);		/* XXX */
692 }
693 
694 /*
695  * Probe for a Pegasus chip.
696  */
697 int
698 aue_match(struct device *parent, void *match, void *aux)
699 {
700 	struct usb_attach_arg	*uaa = aux;
701 
702 	if (uaa->iface != NULL)
703 		return (UMATCH_NONE);
704 
705 	return (aue_lookup(uaa->vendor, uaa->product) != NULL ?
706 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
707 }
708 
709 /*
710  * Attach the interface. Allocate softc structures, do ifmedia
711  * setup and ethernet/BPF attach.
712  */
713 void
714 aue_attach(struct device *parent, struct device *self, void *aux)
715 {
716 	struct aue_softc	*sc = (struct aue_softc *)self;
717 	struct usb_attach_arg	*uaa = aux;
718 	int			s;
719 	u_char			eaddr[ETHER_ADDR_LEN];
720 	struct ifnet		*ifp;
721 	struct mii_data		*mii;
722 	usbd_device_handle	dev = uaa->device;
723 	usbd_interface_handle	iface;
724 	usbd_status		err;
725 	usb_interface_descriptor_t	*id;
726 	usb_endpoint_descriptor_t	*ed;
727 	int			i;
728 
729 	DPRINTFN(5,(" : aue_attach: sc=%p", sc));
730 
731 	err = usbd_set_config_no(dev, AUE_CONFIG_NO, 1);
732 	if (err) {
733 		printf("%s: setting config no failed\n",
734 		    sc->aue_dev.dv_xname);
735 		return;
736 	}
737 
738 	usb_init_task(&sc->aue_tick_task, aue_tick_task, sc);
739 	usb_init_task(&sc->aue_stop_task, (void (*)(void *))aue_stop, sc);
740 	rw_init(&sc->aue_mii_lock, "auemii");
741 
742 	err = usbd_device2interface_handle(dev, AUE_IFACE_IDX, &iface);
743 	if (err) {
744 		printf("%s: getting interface handle failed\n",
745 		    sc->aue_dev.dv_xname);
746 		return;
747 	}
748 
749 	sc->aue_flags = aue_lookup(uaa->vendor, uaa->product)->aue_flags;
750 
751 	sc->aue_udev = dev;
752 	sc->aue_iface = iface;
753 	sc->aue_product = uaa->product;
754 	sc->aue_vendor = uaa->vendor;
755 
756 	id = usbd_get_interface_descriptor(iface);
757 
758 	/* Find endpoints. */
759 	for (i = 0; i < id->bNumEndpoints; i++) {
760 		ed = usbd_interface2endpoint_descriptor(iface, i);
761 		if (ed == NULL) {
762 			printf("%s: couldn't get endpoint descriptor %d\n",
763 			    sc->aue_dev.dv_xname, i);
764 			return;
765 		}
766 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
767 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
768 			sc->aue_ed[AUE_ENDPT_RX] = ed->bEndpointAddress;
769 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
770 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
771 			sc->aue_ed[AUE_ENDPT_TX] = ed->bEndpointAddress;
772 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
773 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
774 			sc->aue_ed[AUE_ENDPT_INTR] = ed->bEndpointAddress;
775 		}
776 	}
777 
778 	if (sc->aue_ed[AUE_ENDPT_RX] == 0 || sc->aue_ed[AUE_ENDPT_TX] == 0 ||
779 	    sc->aue_ed[AUE_ENDPT_INTR] == 0) {
780 		printf("%s: missing endpoint\n", sc->aue_dev.dv_xname);
781 		return;
782 	}
783 
784 
785 	s = splnet();
786 
787 	/* Reset the adapter. */
788 	aue_reset(sc);
789 
790 	/*
791 	 * Get station address from the EEPROM.
792 	 */
793 	aue_read_mac(sc, eaddr);
794 
795 	/*
796 	 * A Pegasus chip was detected. Inform the world.
797 	 */
798 	ifp = GET_IFP(sc);
799 	printf("%s: address %s\n", sc->aue_dev.dv_xname,
800 	    ether_sprintf(eaddr));
801 
802 	bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
803 
804 	/* Initialize interface info.*/
805 	ifp->if_softc = sc;
806 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
807 	ifp->if_ioctl = aue_ioctl;
808 	ifp->if_start = aue_start;
809 	ifp->if_watchdog = aue_watchdog;
810 	strlcpy(ifp->if_xname, sc->aue_dev.dv_xname, IFNAMSIZ);
811 
812 	IFQ_SET_READY(&ifp->if_snd);
813 
814 	/* Initialize MII/media info. */
815 	mii = &sc->aue_mii;
816 	mii->mii_ifp = ifp;
817 	mii->mii_readreg = aue_miibus_readreg;
818 	mii->mii_writereg = aue_miibus_writereg;
819 	mii->mii_statchg = aue_miibus_statchg;
820 	mii->mii_flags = MIIF_AUTOTSLEEP;
821 	ifmedia_init(&mii->mii_media, 0, aue_ifmedia_upd, aue_ifmedia_sts);
822 	mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0);
823 	if (LIST_FIRST(&mii->mii_phys) == NULL) {
824 		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
825 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
826 	} else
827 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
828 
829 	/* Attach the interface. */
830 	if_attach(ifp);
831 	ether_ifattach(ifp);
832 
833 	timeout_set(&sc->aue_stat_ch, aue_tick, sc);
834 
835 	sc->aue_attached = 1;
836 	sc->sc_sdhook = shutdownhook_establish(aue_shutdown, sc);
837 	splx(s);
838 
839 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->aue_udev,
840 			   &sc->aue_dev);
841 }
842 
843 int
844 aue_detach(struct device *self, int flags)
845 {
846 	struct aue_softc	*sc = (struct aue_softc *)self;
847 	struct ifnet		*ifp = GET_IFP(sc);
848 	int			s;
849 
850 	DPRINTFN(2,("%s: %s: enter\n", sc->aue_dev.dv_xname, __func__));
851 
852 	if (!sc->aue_attached) {
853 		/* Detached before attached finished, so just bail out. */
854 		return (0);
855 	}
856 
857 	timeout_del(&sc->aue_stat_ch);
858 	/*
859 	 * Remove any pending tasks.  They cannot be executing because they run
860 	 * in the same thread as detach.
861 	 */
862 	usb_rem_task(sc->aue_udev, &sc->aue_tick_task);
863 	usb_rem_task(sc->aue_udev, &sc->aue_stop_task);
864 
865 	s = splusb();
866 
867 	if (ifp->if_flags & IFF_RUNNING)
868 		aue_stop(sc);
869 
870 	mii_detach(&sc->aue_mii, MII_PHY_ANY, MII_OFFSET_ANY);
871 	ifmedia_delete_instance(&sc->aue_mii.mii_media, IFM_INST_ANY);
872 	ether_ifdetach(ifp);
873 	if_detach(ifp);
874 
875 #ifdef DIAGNOSTIC
876 	if (sc->aue_ep[AUE_ENDPT_TX] != NULL ||
877 	    sc->aue_ep[AUE_ENDPT_RX] != NULL ||
878 	    sc->aue_ep[AUE_ENDPT_INTR] != NULL)
879 		printf("%s: detach has active endpoints\n",
880 		       sc->aue_dev.dv_xname);
881 #endif
882 
883 	sc->aue_attached = 0;
884 	if (sc->sc_sdhook != NULL)
885 		shutdownhook_disestablish(sc->sc_sdhook);
886 
887 	if (--sc->aue_refcnt >= 0) {
888 		/* Wait for processes to go away. */
889 		usb_detach_wait(&sc->aue_dev);
890 	}
891 	splx(s);
892 
893 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->aue_udev,
894 			   &sc->aue_dev);
895 
896 	return (0);
897 }
898 
899 int
900 aue_activate(struct device *self, enum devact act)
901 {
902 	struct aue_softc *sc = (struct aue_softc *)self;
903 
904 	DPRINTFN(2,("%s: %s: enter\n", sc->aue_dev.dv_xname, __func__));
905 
906 	switch (act) {
907 	case DVACT_ACTIVATE:
908 		break;
909 
910 	case DVACT_DEACTIVATE:
911 		sc->aue_dying = 1;
912 		break;
913 	}
914 	return (0);
915 }
916 
917 /*
918  * Initialize an RX descriptor and attach an MBUF cluster.
919  */
920 int
921 aue_newbuf(struct aue_softc *sc, struct aue_chain *c, struct mbuf *m)
922 {
923 	struct mbuf		*m_new = NULL;
924 
925 	DPRINTFN(10,("%s: %s: enter\n", sc->aue_dev.dv_xname,__func__));
926 
927 	if (m == NULL) {
928 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
929 		if (m_new == NULL) {
930 			printf("%s: no memory for rx list "
931 			    "-- packet dropped!\n", sc->aue_dev.dv_xname);
932 			return (ENOBUFS);
933 		}
934 
935 		MCLGET(m_new, M_DONTWAIT);
936 		if (!(m_new->m_flags & M_EXT)) {
937 			printf("%s: no memory for rx list "
938 			    "-- packet dropped!\n", sc->aue_dev.dv_xname);
939 			m_freem(m_new);
940 			return (ENOBUFS);
941 		}
942 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
943 	} else {
944 		m_new = m;
945 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
946 		m_new->m_data = m_new->m_ext.ext_buf;
947 	}
948 
949 	m_adj(m_new, ETHER_ALIGN);
950 	c->aue_mbuf = m_new;
951 
952 	return (0);
953 }
954 
955 int
956 aue_rx_list_init(struct aue_softc *sc)
957 {
958 	struct aue_cdata	*cd;
959 	struct aue_chain	*c;
960 	int			i;
961 
962 	DPRINTFN(5,("%s: %s: enter\n", sc->aue_dev.dv_xname, __func__));
963 
964 	cd = &sc->aue_cdata;
965 	for (i = 0; i < AUE_RX_LIST_CNT; i++) {
966 		c = &cd->aue_rx_chain[i];
967 		c->aue_sc = sc;
968 		c->aue_idx = i;
969 		if (aue_newbuf(sc, c, NULL) == ENOBUFS)
970 			return (ENOBUFS);
971 		if (c->aue_xfer == NULL) {
972 			c->aue_xfer = usbd_alloc_xfer(sc->aue_udev);
973 			if (c->aue_xfer == NULL)
974 				return (ENOBUFS);
975 			c->aue_buf = usbd_alloc_buffer(c->aue_xfer, AUE_BUFSZ);
976 			if (c->aue_buf == NULL)
977 				return (ENOBUFS); /* XXX free xfer */
978 		}
979 	}
980 
981 	return (0);
982 }
983 
984 int
985 aue_tx_list_init(struct aue_softc *sc)
986 {
987 	struct aue_cdata	*cd;
988 	struct aue_chain	*c;
989 	int			i;
990 
991 	DPRINTFN(5,("%s: %s: enter\n", sc->aue_dev.dv_xname, __func__));
992 
993 	cd = &sc->aue_cdata;
994 	for (i = 0; i < AUE_TX_LIST_CNT; i++) {
995 		c = &cd->aue_tx_chain[i];
996 		c->aue_sc = sc;
997 		c->aue_idx = i;
998 		c->aue_mbuf = NULL;
999 		if (c->aue_xfer == NULL) {
1000 			c->aue_xfer = usbd_alloc_xfer(sc->aue_udev);
1001 			if (c->aue_xfer == NULL)
1002 				return (ENOBUFS);
1003 			c->aue_buf = usbd_alloc_buffer(c->aue_xfer, AUE_BUFSZ);
1004 			if (c->aue_buf == NULL)
1005 				return (ENOBUFS);
1006 		}
1007 	}
1008 
1009 	return (0);
1010 }
1011 
1012 void
1013 aue_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
1014 {
1015 	struct aue_softc	*sc = priv;
1016 	struct ifnet		*ifp = GET_IFP(sc);
1017 	struct aue_intrpkt	*p = &sc->aue_cdata.aue_ibuf;
1018 
1019 	DPRINTFN(15,("%s: %s: enter\n", sc->aue_dev.dv_xname,__func__));
1020 
1021 	if (sc->aue_dying)
1022 		return;
1023 
1024 	if (!(ifp->if_flags & IFF_RUNNING))
1025 		return;
1026 
1027 	if (status != USBD_NORMAL_COMPLETION) {
1028 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1029 			return;
1030 		}
1031 		sc->aue_intr_errs++;
1032 		if (usbd_ratecheck(&sc->aue_rx_notice)) {
1033 			printf("%s: %u usb errors on intr: %s\n",
1034 			    sc->aue_dev.dv_xname, sc->aue_intr_errs,
1035 			    usbd_errstr(status));
1036 			sc->aue_intr_errs = 0;
1037 		}
1038 		if (status == USBD_STALLED)
1039 			usbd_clear_endpoint_stall_async(sc->aue_ep[AUE_ENDPT_RX]);
1040 		return;
1041 	}
1042 
1043 	if (p->aue_txstat0)
1044 		ifp->if_oerrors++;
1045 
1046 	if (p->aue_txstat0 & (AUE_TXSTAT0_LATECOLL | AUE_TXSTAT0_EXCESSCOLL))
1047 		ifp->if_collisions++;
1048 }
1049 
1050 /*
1051  * A frame has been uploaded: pass the resulting mbuf chain up to
1052  * the higher level protocols.
1053  */
1054 void
1055 aue_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
1056 {
1057 	struct aue_chain	*c = priv;
1058 	struct aue_softc	*sc = c->aue_sc;
1059 	struct ifnet		*ifp = GET_IFP(sc);
1060 	struct mbuf		*m;
1061 	u_int32_t		total_len;
1062 	struct aue_rxpkt	r;
1063 	int			s;
1064 
1065 	DPRINTFN(10,("%s: %s: enter\n", sc->aue_dev.dv_xname,__func__));
1066 
1067 	if (sc->aue_dying)
1068 		return;
1069 
1070 	if (!(ifp->if_flags & IFF_RUNNING))
1071 		return;
1072 
1073 	if (status != USBD_NORMAL_COMPLETION) {
1074 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
1075 			return;
1076 		sc->aue_rx_errs++;
1077 		if (usbd_ratecheck(&sc->aue_rx_notice)) {
1078 			printf("%s: %u usb errors on rx: %s\n",
1079 			    sc->aue_dev.dv_xname, sc->aue_rx_errs,
1080 			    usbd_errstr(status));
1081 			sc->aue_rx_errs = 0;
1082 		}
1083 		if (status == USBD_STALLED)
1084 			usbd_clear_endpoint_stall_async(sc->aue_ep[AUE_ENDPT_RX]);
1085 		goto done;
1086 	}
1087 
1088 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
1089 
1090 	memcpy(mtod(c->aue_mbuf, char *), c->aue_buf, total_len);
1091 
1092 	if (total_len <= 4 + ETHER_CRC_LEN) {
1093 		ifp->if_ierrors++;
1094 		goto done;
1095 	}
1096 
1097 	memcpy(&r, c->aue_buf + total_len - 4, sizeof(r));
1098 
1099 	/* Turn off all the non-error bits in the rx status word. */
1100 	r.aue_rxstat &= AUE_RXSTAT_MASK;
1101 	if (r.aue_rxstat) {
1102 		ifp->if_ierrors++;
1103 		goto done;
1104 	}
1105 
1106 	/* No errors; receive the packet. */
1107 	m = c->aue_mbuf;
1108 	total_len -= ETHER_CRC_LEN + 4;
1109 	m->m_pkthdr.len = m->m_len = total_len;
1110 	ifp->if_ipackets++;
1111 
1112 	m->m_pkthdr.rcvif = ifp;
1113 
1114 	s = splnet();
1115 
1116 	/* XXX ugly */
1117 	if (aue_newbuf(sc, c, NULL) == ENOBUFS) {
1118 		ifp->if_ierrors++;
1119 		goto done1;
1120 	}
1121 
1122 #if NBPFILTER > 0
1123 	/*
1124 	 * Handle BPF listeners. Let the BPF user see the packet, but
1125 	 * don't pass it up to the ether_input() layer unless it's
1126 	 * a broadcast packet, multicast packet, matches our ethernet
1127 	 * address or the interface is in promiscuous mode.
1128 	 */
1129 	if (ifp->if_bpf)
1130 		bpf_mtap(ifp->if_bpf, m, BPF_DIRECTION_IN);
1131 #endif
1132 
1133 	DPRINTFN(10,("%s: %s: deliver %d\n", sc->aue_dev.dv_xname,
1134 		    __func__, m->m_len));
1135 	ether_input_mbuf(ifp, m);
1136  done1:
1137 	splx(s);
1138 
1139  done:
1140 
1141 	/* Setup new transfer. */
1142 	usbd_setup_xfer(xfer, sc->aue_ep[AUE_ENDPT_RX],
1143 	    c, c->aue_buf, AUE_BUFSZ,
1144 	    USBD_SHORT_XFER_OK | USBD_NO_COPY,
1145 	    USBD_NO_TIMEOUT, aue_rxeof);
1146 	usbd_transfer(xfer);
1147 
1148 	DPRINTFN(10,("%s: %s: start rx\n", sc->aue_dev.dv_xname,
1149 		    __func__));
1150 }
1151 
1152 /*
1153  * A frame was downloaded to the chip. It's safe for us to clean up
1154  * the list buffers.
1155  */
1156 
1157 void
1158 aue_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
1159 {
1160 	struct aue_chain	*c = priv;
1161 	struct aue_softc	*sc = c->aue_sc;
1162 	struct ifnet		*ifp = GET_IFP(sc);
1163 	int			s;
1164 
1165 	if (sc->aue_dying)
1166 		return;
1167 
1168 	s = splnet();
1169 
1170 	DPRINTFN(10,("%s: %s: enter status=%d\n", sc->aue_dev.dv_xname,
1171 		    __func__, status));
1172 
1173 	ifp->if_timer = 0;
1174 	ifp->if_flags &= ~IFF_OACTIVE;
1175 
1176 	if (status != USBD_NORMAL_COMPLETION) {
1177 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1178 			splx(s);
1179 			return;
1180 		}
1181 		ifp->if_oerrors++;
1182 		printf("%s: usb error on tx: %s\n", sc->aue_dev.dv_xname,
1183 		    usbd_errstr(status));
1184 		if (status == USBD_STALLED)
1185 			usbd_clear_endpoint_stall_async(sc->aue_ep[AUE_ENDPT_TX]);
1186 		splx(s);
1187 		return;
1188 	}
1189 
1190 	ifp->if_opackets++;
1191 
1192 	m_freem(c->aue_mbuf);
1193 	c->aue_mbuf = NULL;
1194 
1195 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1196 		aue_start(ifp);
1197 
1198 	splx(s);
1199 }
1200 
1201 void
1202 aue_tick(void *xsc)
1203 {
1204 	struct aue_softc	*sc = xsc;
1205 
1206 	DPRINTFN(15,("%s: %s: enter\n", sc->aue_dev.dv_xname,__func__));
1207 
1208 	if (sc == NULL)
1209 		return;
1210 
1211 	if (sc->aue_dying)
1212 		return;
1213 
1214 	/* Perform periodic stuff in process context. */
1215 	usb_add_task(sc->aue_udev, &sc->aue_tick_task);
1216 }
1217 
1218 void
1219 aue_tick_task(void *xsc)
1220 {
1221 	struct aue_softc	*sc = xsc;
1222 	struct ifnet		*ifp;
1223 	struct mii_data		*mii;
1224 	int			s;
1225 
1226 	DPRINTFN(15,("%s: %s: enter\n", sc->aue_dev.dv_xname,__func__));
1227 
1228 	if (sc->aue_dying)
1229 		return;
1230 
1231 	ifp = GET_IFP(sc);
1232 	mii = GET_MII(sc);
1233 	if (mii == NULL)
1234 		return;
1235 
1236 	s = splnet();
1237 
1238 	mii_tick(mii);
1239 	if (!sc->aue_link && mii->mii_media_status & IFM_ACTIVE &&
1240 	    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1241 		DPRINTFN(2,("%s: %s: got link\n",
1242 			    sc->aue_dev.dv_xname,__func__));
1243 		sc->aue_link++;
1244 		if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1245 			aue_start(ifp);
1246 	}
1247 
1248 	timeout_add_sec(&sc->aue_stat_ch, 1);
1249 
1250 	splx(s);
1251 }
1252 
1253 int
1254 aue_send(struct aue_softc *sc, struct mbuf *m, int idx)
1255 {
1256 	int			total_len;
1257 	struct aue_chain	*c;
1258 	usbd_status		err;
1259 
1260 	DPRINTFN(10,("%s: %s: enter\n", sc->aue_dev.dv_xname,__func__));
1261 
1262 	c = &sc->aue_cdata.aue_tx_chain[idx];
1263 
1264 	/*
1265 	 * Copy the mbuf data into a contiguous buffer, leaving two
1266 	 * bytes at the beginning to hold the frame length.
1267 	 */
1268 	m_copydata(m, 0, m->m_pkthdr.len, c->aue_buf + 2);
1269 	c->aue_mbuf = m;
1270 
1271 	/*
1272 	 * The ADMtek documentation says that the packet length is
1273 	 * supposed to be specified in the first two bytes of the
1274 	 * transfer, however it actually seems to ignore this info
1275 	 * and base the frame size on the bulk transfer length.
1276 	 */
1277 	c->aue_buf[0] = (u_int8_t)m->m_pkthdr.len;
1278 	c->aue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8);
1279 	total_len = m->m_pkthdr.len + 2;
1280 
1281 	usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_TX],
1282 	    c, c->aue_buf, total_len, USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
1283 	    AUE_TX_TIMEOUT, aue_txeof);
1284 
1285 	/* Transmit */
1286 	err = usbd_transfer(c->aue_xfer);
1287 	if (err != USBD_IN_PROGRESS) {
1288 		printf("%s: aue_send error=%s\n", sc->aue_dev.dv_xname,
1289 		       usbd_errstr(err));
1290 		/* Stop the interface from process context. */
1291 		usb_add_task(sc->aue_udev, &sc->aue_stop_task);
1292 		return (EIO);
1293 	}
1294 	DPRINTFN(5,("%s: %s: send %d bytes\n", sc->aue_dev.dv_xname,
1295 		    __func__, total_len));
1296 
1297 	sc->aue_cdata.aue_tx_cnt++;
1298 
1299 	return (0);
1300 }
1301 
1302 void
1303 aue_start(struct ifnet *ifp)
1304 {
1305 	struct aue_softc	*sc = ifp->if_softc;
1306 	struct mbuf		*m_head = NULL;
1307 
1308 	DPRINTFN(5,("%s: %s: enter, link=%d\n", sc->aue_dev.dv_xname,
1309 		    __func__, sc->aue_link));
1310 
1311 	if (sc->aue_dying)
1312 		return;
1313 
1314 	if (!sc->aue_link)
1315 		return;
1316 
1317 	if (ifp->if_flags & IFF_OACTIVE)
1318 		return;
1319 
1320 	IFQ_POLL(&ifp->if_snd, m_head);
1321 	if (m_head == NULL)
1322 		return;
1323 
1324 	if (aue_send(sc, m_head, 0)) {
1325 		ifp->if_flags |= IFF_OACTIVE;
1326 		return;
1327 	}
1328 
1329 	IFQ_DEQUEUE(&ifp->if_snd, m_head);
1330 
1331 #if NBPFILTER > 0
1332 	/*
1333 	 * If there's a BPF listener, bounce a copy of this frame
1334 	 * to him.
1335 	 */
1336 	if (ifp->if_bpf)
1337 		bpf_mtap(ifp->if_bpf, m_head, BPF_DIRECTION_OUT);
1338 #endif
1339 
1340 	ifp->if_flags |= IFF_OACTIVE;
1341 
1342 	/*
1343 	 * Set a timeout in case the chip goes out to lunch.
1344 	 */
1345 	ifp->if_timer = 5;
1346 }
1347 
1348 void
1349 aue_init(void *xsc)
1350 {
1351 	struct aue_softc	*sc = xsc;
1352 	struct ifnet		*ifp = GET_IFP(sc);
1353 	struct mii_data		*mii = GET_MII(sc);
1354 	int			i, s;
1355 	u_char			*eaddr;
1356 
1357 	DPRINTFN(5,("%s: %s: enter\n", sc->aue_dev.dv_xname, __func__));
1358 
1359 	if (sc->aue_dying)
1360 		return;
1361 
1362 	if (ifp->if_flags & IFF_RUNNING)
1363 		return;
1364 
1365 	s = splnet();
1366 
1367 	/*
1368 	 * Cancel pending I/O and free all RX/TX buffers.
1369 	 */
1370 	aue_reset(sc);
1371 
1372 	eaddr = sc->arpcom.ac_enaddr;
1373 	for (i = 0; i < ETHER_ADDR_LEN; i++)
1374 		aue_csr_write_1(sc, AUE_PAR0 + i, eaddr[i]);
1375 
1376 	 /* If we want promiscuous mode, set the allframes bit. */
1377 	if (ifp->if_flags & IFF_PROMISC)
1378 		AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1379 	else
1380 		AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1381 
1382 	/* Init TX ring. */
1383 	if (aue_tx_list_init(sc) == ENOBUFS) {
1384 		printf("%s: tx list init failed\n", sc->aue_dev.dv_xname);
1385 		splx(s);
1386 		return;
1387 	}
1388 
1389 	/* Init RX ring. */
1390 	if (aue_rx_list_init(sc) == ENOBUFS) {
1391 		printf("%s: rx list init failed\n", sc->aue_dev.dv_xname);
1392 		splx(s);
1393 		return;
1394 	}
1395 
1396 	/* Load the multicast filter. */
1397 	aue_setmulti(sc);
1398 
1399 	/* Enable RX and TX */
1400 	aue_csr_write_1(sc, AUE_CTL0, AUE_CTL0_RXSTAT_APPEND | AUE_CTL0_RX_ENB);
1401 	AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_TX_ENB);
1402 	AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_EP3_CLR);
1403 
1404 	mii_mediachg(mii);
1405 
1406 	if (sc->aue_ep[AUE_ENDPT_RX] == NULL) {
1407 		if (aue_openpipes(sc)) {
1408 			splx(s);
1409 			return;
1410 		}
1411 	}
1412 
1413 	ifp->if_flags |= IFF_RUNNING;
1414 	ifp->if_flags &= ~IFF_OACTIVE;
1415 
1416 	splx(s);
1417 
1418 	timeout_add_sec(&sc->aue_stat_ch, 1);
1419 }
1420 
1421 int
1422 aue_openpipes(struct aue_softc *sc)
1423 {
1424 	struct aue_chain	*c;
1425 	usbd_status		err;
1426 	int i;
1427 
1428 	/* Open RX and TX pipes. */
1429 	err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_RX],
1430 	    USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_RX]);
1431 	if (err) {
1432 		printf("%s: open rx pipe failed: %s\n",
1433 		    sc->aue_dev.dv_xname, usbd_errstr(err));
1434 		return (EIO);
1435 	}
1436 	err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_TX],
1437 	    USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_TX]);
1438 	if (err) {
1439 		printf("%s: open tx pipe failed: %s\n",
1440 		    sc->aue_dev.dv_xname, usbd_errstr(err));
1441 		return (EIO);
1442 	}
1443 	err = usbd_open_pipe_intr(sc->aue_iface, sc->aue_ed[AUE_ENDPT_INTR],
1444 	    USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_INTR], sc,
1445 	    &sc->aue_cdata.aue_ibuf, AUE_INTR_PKTLEN, aue_intr,
1446 	    AUE_INTR_INTERVAL);
1447 	if (err) {
1448 		printf("%s: open intr pipe failed: %s\n",
1449 		    sc->aue_dev.dv_xname, usbd_errstr(err));
1450 		return (EIO);
1451 	}
1452 
1453 	/* Start up the receive pipe. */
1454 	for (i = 0; i < AUE_RX_LIST_CNT; i++) {
1455 		c = &sc->aue_cdata.aue_rx_chain[i];
1456 		usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_RX],
1457 		    c, c->aue_buf, AUE_BUFSZ,
1458 		    USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT,
1459 		    aue_rxeof);
1460 		(void)usbd_transfer(c->aue_xfer); /* XXX */
1461 		DPRINTFN(5,("%s: %s: start read\n", sc->aue_dev.dv_xname,
1462 			    __func__));
1463 
1464 	}
1465 	return (0);
1466 }
1467 
1468 /*
1469  * Set media options.
1470  */
1471 int
1472 aue_ifmedia_upd(struct ifnet *ifp)
1473 {
1474 	struct aue_softc	*sc = ifp->if_softc;
1475 	struct mii_data		*mii = GET_MII(sc);
1476 
1477 	DPRINTFN(5,("%s: %s: enter\n", sc->aue_dev.dv_xname, __func__));
1478 
1479 	if (sc->aue_dying)
1480 		return (0);
1481 
1482 	sc->aue_link = 0;
1483 	if (mii->mii_instance) {
1484 		struct mii_softc	*miisc;
1485 		for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
1486 		    miisc = LIST_NEXT(miisc, mii_list))
1487 			 mii_phy_reset(miisc);
1488 	}
1489 	mii_mediachg(mii);
1490 
1491 	return (0);
1492 }
1493 
1494 /*
1495  * Report current media status.
1496  */
1497 void
1498 aue_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1499 {
1500 	struct aue_softc	*sc = ifp->if_softc;
1501 	struct mii_data		*mii = GET_MII(sc);
1502 
1503 	DPRINTFN(5,("%s: %s: enter\n", sc->aue_dev.dv_xname, __func__));
1504 
1505 	mii_pollstat(mii);
1506 	ifmr->ifm_active = mii->mii_media_active;
1507 	ifmr->ifm_status = mii->mii_media_status;
1508 }
1509 
1510 int
1511 aue_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
1512 {
1513 	struct aue_softc	*sc = ifp->if_softc;
1514 	struct ifaddr 		*ifa = (struct ifaddr *)data;
1515 	struct ifreq		*ifr = (struct ifreq *)data;
1516 	struct mii_data		*mii;
1517 	int			s, error = 0;
1518 
1519 	if (sc->aue_dying)
1520 		return (EIO);
1521 
1522 	s = splnet();
1523 
1524 	switch(command) {
1525 	case SIOCSIFADDR:
1526 		ifp->if_flags |= IFF_UP;
1527 		aue_init(sc);
1528 
1529 		switch (ifa->ifa_addr->sa_family) {
1530 #ifdef INET
1531 		case AF_INET:
1532 			arp_ifinit(&sc->arpcom, ifa);
1533 			break;
1534 #endif /* INET */
1535 		}
1536 		break;
1537 
1538 	case SIOCSIFMTU:
1539 		if (ifr->ifr_mtu > ETHERMTU)
1540 			error = EINVAL;
1541 		else
1542 			ifp->if_mtu = ifr->ifr_mtu;
1543 		break;
1544 
1545 	case SIOCSIFFLAGS:
1546 		if (ifp->if_flags & IFF_UP) {
1547 			if (ifp->if_flags & IFF_RUNNING &&
1548 			    ifp->if_flags & IFF_PROMISC &&
1549 			    !(sc->aue_if_flags & IFF_PROMISC)) {
1550 				AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1551 			} else if (ifp->if_flags & IFF_RUNNING &&
1552 			    !(ifp->if_flags & IFF_PROMISC) &&
1553 			    sc->aue_if_flags & IFF_PROMISC) {
1554 				AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1555 			} else if (!(ifp->if_flags & IFF_RUNNING))
1556 				aue_init(sc);
1557 		} else {
1558 			if (ifp->if_flags & IFF_RUNNING)
1559 				aue_stop(sc);
1560 		}
1561 		sc->aue_if_flags = ifp->if_flags;
1562 		error = 0;
1563 		break;
1564 	case SIOCADDMULTI:
1565 	case SIOCDELMULTI:
1566 		error = (command == SIOCADDMULTI) ?
1567 			ether_addmulti(ifr, &sc->arpcom) :
1568 			ether_delmulti(ifr, &sc->arpcom);
1569 
1570 		if (error == ENETRESET) {
1571 			if (ifp->if_flags & IFF_RUNNING)
1572 				aue_setmulti(sc);
1573 			error = 0;
1574 		}
1575 		break;
1576 	case SIOCGIFMEDIA:
1577 	case SIOCSIFMEDIA:
1578 		mii = GET_MII(sc);
1579 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1580 		break;
1581 	default:
1582 		error = EINVAL;
1583 		break;
1584 	}
1585 
1586 	splx(s);
1587 
1588 	return (error);
1589 }
1590 
1591 void
1592 aue_watchdog(struct ifnet *ifp)
1593 {
1594 	struct aue_softc	*sc = ifp->if_softc;
1595 	struct aue_chain	*c;
1596 	usbd_status		stat;
1597 	int			s;
1598 
1599 	DPRINTFN(5,("%s: %s: enter\n", sc->aue_dev.dv_xname, __func__));
1600 
1601 	ifp->if_oerrors++;
1602 	printf("%s: watchdog timeout\n", sc->aue_dev.dv_xname);
1603 
1604 	s = splusb();
1605 	c = &sc->aue_cdata.aue_tx_chain[0];
1606 	usbd_get_xfer_status(c->aue_xfer, NULL, NULL, NULL, &stat);
1607 	aue_txeof(c->aue_xfer, c, stat);
1608 
1609 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1610 		aue_start(ifp);
1611 	splx(s);
1612 }
1613 
1614 /*
1615  * Stop all chip I/O so that the kernel's probe routines don't
1616  * get confused by errant DMAs when rebooting.
1617  */
1618 void
1619 aue_shutdown(void *arg)
1620 {
1621 	struct aue_softc *sc = (struct aue_softc *)arg;
1622 
1623 	aue_reset(sc);
1624 	aue_stop(sc);
1625 }
1626 
1627 /*
1628  * Stop the adapter and free any mbufs allocated to the
1629  * RX and TX lists.
1630  */
1631 void
1632 aue_stop(struct aue_softc *sc)
1633 {
1634 	usbd_status		err;
1635 	struct ifnet		*ifp;
1636 	int			i;
1637 
1638 	DPRINTFN(5,("%s: %s: enter\n", sc->aue_dev.dv_xname, __func__));
1639 
1640 	ifp = GET_IFP(sc);
1641 	ifp->if_timer = 0;
1642 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1643 
1644 	aue_csr_write_1(sc, AUE_CTL0, 0);
1645 	aue_csr_write_1(sc, AUE_CTL1, 0);
1646 	aue_reset(sc);
1647 	timeout_del(&sc->aue_stat_ch);
1648 
1649 	/* Stop transfers. */
1650 	if (sc->aue_ep[AUE_ENDPT_RX] != NULL) {
1651 		err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_RX]);
1652 		if (err) {
1653 			printf("%s: abort rx pipe failed: %s\n",
1654 			    sc->aue_dev.dv_xname, usbd_errstr(err));
1655 		}
1656 		err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_RX]);
1657 		if (err) {
1658 			printf("%s: close rx pipe failed: %s\n",
1659 			    sc->aue_dev.dv_xname, usbd_errstr(err));
1660 		}
1661 		sc->aue_ep[AUE_ENDPT_RX] = NULL;
1662 	}
1663 
1664 	if (sc->aue_ep[AUE_ENDPT_TX] != NULL) {
1665 		err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_TX]);
1666 		if (err) {
1667 			printf("%s: abort tx pipe failed: %s\n",
1668 			    sc->aue_dev.dv_xname, usbd_errstr(err));
1669 		}
1670 		err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_TX]);
1671 		if (err) {
1672 			printf("%s: close tx pipe failed: %s\n",
1673 			    sc->aue_dev.dv_xname, usbd_errstr(err));
1674 		}
1675 		sc->aue_ep[AUE_ENDPT_TX] = NULL;
1676 	}
1677 
1678 	if (sc->aue_ep[AUE_ENDPT_INTR] != NULL) {
1679 		err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
1680 		if (err) {
1681 			printf("%s: abort intr pipe failed: %s\n",
1682 			    sc->aue_dev.dv_xname, usbd_errstr(err));
1683 		}
1684 		err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
1685 		if (err) {
1686 			printf("%s: close intr pipe failed: %s\n",
1687 			    sc->aue_dev.dv_xname, usbd_errstr(err));
1688 		}
1689 		sc->aue_ep[AUE_ENDPT_INTR] = NULL;
1690 	}
1691 
1692 	/* Free RX resources. */
1693 	for (i = 0; i < AUE_RX_LIST_CNT; i++) {
1694 		if (sc->aue_cdata.aue_rx_chain[i].aue_mbuf != NULL) {
1695 			m_freem(sc->aue_cdata.aue_rx_chain[i].aue_mbuf);
1696 			sc->aue_cdata.aue_rx_chain[i].aue_mbuf = NULL;
1697 		}
1698 		if (sc->aue_cdata.aue_rx_chain[i].aue_xfer != NULL) {
1699 			usbd_free_xfer(sc->aue_cdata.aue_rx_chain[i].aue_xfer);
1700 			sc->aue_cdata.aue_rx_chain[i].aue_xfer = NULL;
1701 		}
1702 	}
1703 
1704 	/* Free TX resources. */
1705 	for (i = 0; i < AUE_TX_LIST_CNT; i++) {
1706 		if (sc->aue_cdata.aue_tx_chain[i].aue_mbuf != NULL) {
1707 			m_freem(sc->aue_cdata.aue_tx_chain[i].aue_mbuf);
1708 			sc->aue_cdata.aue_tx_chain[i].aue_mbuf = NULL;
1709 		}
1710 		if (sc->aue_cdata.aue_tx_chain[i].aue_xfer != NULL) {
1711 			usbd_free_xfer(sc->aue_cdata.aue_tx_chain[i].aue_xfer);
1712 			sc->aue_cdata.aue_tx_chain[i].aue_xfer = NULL;
1713 		}
1714 	}
1715 
1716 	sc->aue_link = 0;
1717 }
1718