xref: /openbsd-src/sys/dev/usb/if_aue.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: if_aue.c,v 1.75 2008/11/28 02:44:18 brad 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 	ifp->if_capabilities = IFCAP_VLAN_MTU;
815 
816 	/* Initialize MII/media info. */
817 	mii = &sc->aue_mii;
818 	mii->mii_ifp = ifp;
819 	mii->mii_readreg = aue_miibus_readreg;
820 	mii->mii_writereg = aue_miibus_writereg;
821 	mii->mii_statchg = aue_miibus_statchg;
822 	mii->mii_flags = MIIF_AUTOTSLEEP;
823 	ifmedia_init(&mii->mii_media, 0, aue_ifmedia_upd, aue_ifmedia_sts);
824 	mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0);
825 	if (LIST_FIRST(&mii->mii_phys) == NULL) {
826 		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
827 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
828 	} else
829 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
830 
831 	/* Attach the interface. */
832 	if_attach(ifp);
833 	ether_ifattach(ifp);
834 
835 	timeout_set(&sc->aue_stat_ch, aue_tick, sc);
836 
837 	sc->aue_attached = 1;
838 	sc->sc_sdhook = shutdownhook_establish(aue_shutdown, sc);
839 	splx(s);
840 
841 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->aue_udev,
842 			   &sc->aue_dev);
843 }
844 
845 int
846 aue_detach(struct device *self, int flags)
847 {
848 	struct aue_softc	*sc = (struct aue_softc *)self;
849 	struct ifnet		*ifp = GET_IFP(sc);
850 	int			s;
851 
852 	DPRINTFN(2,("%s: %s: enter\n", sc->aue_dev.dv_xname, __func__));
853 
854 	if (!sc->aue_attached) {
855 		/* Detached before attached finished, so just bail out. */
856 		return (0);
857 	}
858 
859 	timeout_del(&sc->aue_stat_ch);
860 	/*
861 	 * Remove any pending tasks.  They cannot be executing because they run
862 	 * in the same thread as detach.
863 	 */
864 	usb_rem_task(sc->aue_udev, &sc->aue_tick_task);
865 	usb_rem_task(sc->aue_udev, &sc->aue_stop_task);
866 
867 	s = splusb();
868 
869 	if (ifp->if_flags & IFF_RUNNING)
870 		aue_stop(sc);
871 
872 	mii_detach(&sc->aue_mii, MII_PHY_ANY, MII_OFFSET_ANY);
873 	ifmedia_delete_instance(&sc->aue_mii.mii_media, IFM_INST_ANY);
874 	ether_ifdetach(ifp);
875 	if_detach(ifp);
876 
877 #ifdef DIAGNOSTIC
878 	if (sc->aue_ep[AUE_ENDPT_TX] != NULL ||
879 	    sc->aue_ep[AUE_ENDPT_RX] != NULL ||
880 	    sc->aue_ep[AUE_ENDPT_INTR] != NULL)
881 		printf("%s: detach has active endpoints\n",
882 		       sc->aue_dev.dv_xname);
883 #endif
884 
885 	sc->aue_attached = 0;
886 	if (sc->sc_sdhook != NULL)
887 		shutdownhook_disestablish(sc->sc_sdhook);
888 
889 	if (--sc->aue_refcnt >= 0) {
890 		/* Wait for processes to go away. */
891 		usb_detach_wait(&sc->aue_dev);
892 	}
893 	splx(s);
894 
895 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->aue_udev,
896 			   &sc->aue_dev);
897 
898 	return (0);
899 }
900 
901 int
902 aue_activate(struct device *self, enum devact act)
903 {
904 	struct aue_softc *sc = (struct aue_softc *)self;
905 
906 	DPRINTFN(2,("%s: %s: enter\n", sc->aue_dev.dv_xname, __func__));
907 
908 	switch (act) {
909 	case DVACT_ACTIVATE:
910 		break;
911 
912 	case DVACT_DEACTIVATE:
913 		sc->aue_dying = 1;
914 		break;
915 	}
916 	return (0);
917 }
918 
919 /*
920  * Initialize an RX descriptor and attach an MBUF cluster.
921  */
922 int
923 aue_newbuf(struct aue_softc *sc, struct aue_chain *c, struct mbuf *m)
924 {
925 	struct mbuf		*m_new = NULL;
926 
927 	DPRINTFN(10,("%s: %s: enter\n", sc->aue_dev.dv_xname,__func__));
928 
929 	if (m == NULL) {
930 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
931 		if (m_new == NULL) {
932 			printf("%s: no memory for rx list "
933 			    "-- packet dropped!\n", sc->aue_dev.dv_xname);
934 			return (ENOBUFS);
935 		}
936 
937 		MCLGET(m_new, M_DONTWAIT);
938 		if (!(m_new->m_flags & M_EXT)) {
939 			printf("%s: no memory for rx list "
940 			    "-- packet dropped!\n", sc->aue_dev.dv_xname);
941 			m_freem(m_new);
942 			return (ENOBUFS);
943 		}
944 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
945 	} else {
946 		m_new = m;
947 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
948 		m_new->m_data = m_new->m_ext.ext_buf;
949 	}
950 
951 	m_adj(m_new, ETHER_ALIGN);
952 	c->aue_mbuf = m_new;
953 
954 	return (0);
955 }
956 
957 int
958 aue_rx_list_init(struct aue_softc *sc)
959 {
960 	struct aue_cdata	*cd;
961 	struct aue_chain	*c;
962 	int			i;
963 
964 	DPRINTFN(5,("%s: %s: enter\n", sc->aue_dev.dv_xname, __func__));
965 
966 	cd = &sc->aue_cdata;
967 	for (i = 0; i < AUE_RX_LIST_CNT; i++) {
968 		c = &cd->aue_rx_chain[i];
969 		c->aue_sc = sc;
970 		c->aue_idx = i;
971 		if (aue_newbuf(sc, c, NULL) == ENOBUFS)
972 			return (ENOBUFS);
973 		if (c->aue_xfer == NULL) {
974 			c->aue_xfer = usbd_alloc_xfer(sc->aue_udev);
975 			if (c->aue_xfer == NULL)
976 				return (ENOBUFS);
977 			c->aue_buf = usbd_alloc_buffer(c->aue_xfer, AUE_BUFSZ);
978 			if (c->aue_buf == NULL)
979 				return (ENOBUFS); /* XXX free xfer */
980 		}
981 	}
982 
983 	return (0);
984 }
985 
986 int
987 aue_tx_list_init(struct aue_softc *sc)
988 {
989 	struct aue_cdata	*cd;
990 	struct aue_chain	*c;
991 	int			i;
992 
993 	DPRINTFN(5,("%s: %s: enter\n", sc->aue_dev.dv_xname, __func__));
994 
995 	cd = &sc->aue_cdata;
996 	for (i = 0; i < AUE_TX_LIST_CNT; i++) {
997 		c = &cd->aue_tx_chain[i];
998 		c->aue_sc = sc;
999 		c->aue_idx = i;
1000 		c->aue_mbuf = NULL;
1001 		if (c->aue_xfer == NULL) {
1002 			c->aue_xfer = usbd_alloc_xfer(sc->aue_udev);
1003 			if (c->aue_xfer == NULL)
1004 				return (ENOBUFS);
1005 			c->aue_buf = usbd_alloc_buffer(c->aue_xfer, AUE_BUFSZ);
1006 			if (c->aue_buf == NULL)
1007 				return (ENOBUFS);
1008 		}
1009 	}
1010 
1011 	return (0);
1012 }
1013 
1014 void
1015 aue_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
1016 {
1017 	struct aue_softc	*sc = priv;
1018 	struct ifnet		*ifp = GET_IFP(sc);
1019 	struct aue_intrpkt	*p = &sc->aue_cdata.aue_ibuf;
1020 
1021 	DPRINTFN(15,("%s: %s: enter\n", sc->aue_dev.dv_xname,__func__));
1022 
1023 	if (sc->aue_dying)
1024 		return;
1025 
1026 	if (!(ifp->if_flags & IFF_RUNNING))
1027 		return;
1028 
1029 	if (status != USBD_NORMAL_COMPLETION) {
1030 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1031 			return;
1032 		}
1033 		sc->aue_intr_errs++;
1034 		if (usbd_ratecheck(&sc->aue_rx_notice)) {
1035 			printf("%s: %u usb errors on intr: %s\n",
1036 			    sc->aue_dev.dv_xname, sc->aue_intr_errs,
1037 			    usbd_errstr(status));
1038 			sc->aue_intr_errs = 0;
1039 		}
1040 		if (status == USBD_STALLED)
1041 			usbd_clear_endpoint_stall_async(sc->aue_ep[AUE_ENDPT_RX]);
1042 		return;
1043 	}
1044 
1045 	if (p->aue_txstat0)
1046 		ifp->if_oerrors++;
1047 
1048 	if (p->aue_txstat0 & (AUE_TXSTAT0_LATECOLL | AUE_TXSTAT0_EXCESSCOLL))
1049 		ifp->if_collisions++;
1050 }
1051 
1052 /*
1053  * A frame has been uploaded: pass the resulting mbuf chain up to
1054  * the higher level protocols.
1055  */
1056 void
1057 aue_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
1058 {
1059 	struct aue_chain	*c = priv;
1060 	struct aue_softc	*sc = c->aue_sc;
1061 	struct ifnet		*ifp = GET_IFP(sc);
1062 	struct mbuf		*m;
1063 	u_int32_t		total_len;
1064 	struct aue_rxpkt	r;
1065 	int			s;
1066 
1067 	DPRINTFN(10,("%s: %s: enter\n", sc->aue_dev.dv_xname,__func__));
1068 
1069 	if (sc->aue_dying)
1070 		return;
1071 
1072 	if (!(ifp->if_flags & IFF_RUNNING))
1073 		return;
1074 
1075 	if (status != USBD_NORMAL_COMPLETION) {
1076 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
1077 			return;
1078 		sc->aue_rx_errs++;
1079 		if (usbd_ratecheck(&sc->aue_rx_notice)) {
1080 			printf("%s: %u usb errors on rx: %s\n",
1081 			    sc->aue_dev.dv_xname, sc->aue_rx_errs,
1082 			    usbd_errstr(status));
1083 			sc->aue_rx_errs = 0;
1084 		}
1085 		if (status == USBD_STALLED)
1086 			usbd_clear_endpoint_stall_async(sc->aue_ep[AUE_ENDPT_RX]);
1087 		goto done;
1088 	}
1089 
1090 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
1091 
1092 	memcpy(mtod(c->aue_mbuf, char *), c->aue_buf, total_len);
1093 
1094 	if (total_len <= 4 + ETHER_CRC_LEN) {
1095 		ifp->if_ierrors++;
1096 		goto done;
1097 	}
1098 
1099 	memcpy(&r, c->aue_buf + total_len - 4, sizeof(r));
1100 
1101 	/* Turn off all the non-error bits in the rx status word. */
1102 	r.aue_rxstat &= AUE_RXSTAT_MASK;
1103 	if (r.aue_rxstat) {
1104 		ifp->if_ierrors++;
1105 		goto done;
1106 	}
1107 
1108 	/* No errors; receive the packet. */
1109 	m = c->aue_mbuf;
1110 	total_len -= ETHER_CRC_LEN + 4;
1111 	m->m_pkthdr.len = m->m_len = total_len;
1112 	ifp->if_ipackets++;
1113 
1114 	m->m_pkthdr.rcvif = ifp;
1115 
1116 	s = splnet();
1117 
1118 	/* XXX ugly */
1119 	if (aue_newbuf(sc, c, NULL) == ENOBUFS) {
1120 		ifp->if_ierrors++;
1121 		goto done1;
1122 	}
1123 
1124 #if NBPFILTER > 0
1125 	/*
1126 	 * Handle BPF listeners. Let the BPF user see the packet, but
1127 	 * don't pass it up to the ether_input() layer unless it's
1128 	 * a broadcast packet, multicast packet, matches our ethernet
1129 	 * address or the interface is in promiscuous mode.
1130 	 */
1131 	if (ifp->if_bpf)
1132 		bpf_mtap(ifp->if_bpf, m, BPF_DIRECTION_IN);
1133 #endif
1134 
1135 	DPRINTFN(10,("%s: %s: deliver %d\n", sc->aue_dev.dv_xname,
1136 		    __func__, m->m_len));
1137 	ether_input_mbuf(ifp, m);
1138  done1:
1139 	splx(s);
1140 
1141  done:
1142 
1143 	/* Setup new transfer. */
1144 	usbd_setup_xfer(xfer, sc->aue_ep[AUE_ENDPT_RX],
1145 	    c, c->aue_buf, AUE_BUFSZ,
1146 	    USBD_SHORT_XFER_OK | USBD_NO_COPY,
1147 	    USBD_NO_TIMEOUT, aue_rxeof);
1148 	usbd_transfer(xfer);
1149 
1150 	DPRINTFN(10,("%s: %s: start rx\n", sc->aue_dev.dv_xname,
1151 		    __func__));
1152 }
1153 
1154 /*
1155  * A frame was downloaded to the chip. It's safe for us to clean up
1156  * the list buffers.
1157  */
1158 
1159 void
1160 aue_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
1161 {
1162 	struct aue_chain	*c = priv;
1163 	struct aue_softc	*sc = c->aue_sc;
1164 	struct ifnet		*ifp = GET_IFP(sc);
1165 	int			s;
1166 
1167 	if (sc->aue_dying)
1168 		return;
1169 
1170 	s = splnet();
1171 
1172 	DPRINTFN(10,("%s: %s: enter status=%d\n", sc->aue_dev.dv_xname,
1173 		    __func__, status));
1174 
1175 	ifp->if_timer = 0;
1176 	ifp->if_flags &= ~IFF_OACTIVE;
1177 
1178 	if (status != USBD_NORMAL_COMPLETION) {
1179 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1180 			splx(s);
1181 			return;
1182 		}
1183 		ifp->if_oerrors++;
1184 		printf("%s: usb error on tx: %s\n", sc->aue_dev.dv_xname,
1185 		    usbd_errstr(status));
1186 		if (status == USBD_STALLED)
1187 			usbd_clear_endpoint_stall_async(sc->aue_ep[AUE_ENDPT_TX]);
1188 		splx(s);
1189 		return;
1190 	}
1191 
1192 	ifp->if_opackets++;
1193 
1194 	m_freem(c->aue_mbuf);
1195 	c->aue_mbuf = NULL;
1196 
1197 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1198 		aue_start(ifp);
1199 
1200 	splx(s);
1201 }
1202 
1203 void
1204 aue_tick(void *xsc)
1205 {
1206 	struct aue_softc	*sc = xsc;
1207 
1208 	DPRINTFN(15,("%s: %s: enter\n", sc->aue_dev.dv_xname,__func__));
1209 
1210 	if (sc == NULL)
1211 		return;
1212 
1213 	if (sc->aue_dying)
1214 		return;
1215 
1216 	/* Perform periodic stuff in process context. */
1217 	usb_add_task(sc->aue_udev, &sc->aue_tick_task);
1218 }
1219 
1220 void
1221 aue_tick_task(void *xsc)
1222 {
1223 	struct aue_softc	*sc = xsc;
1224 	struct ifnet		*ifp;
1225 	struct mii_data		*mii;
1226 	int			s;
1227 
1228 	DPRINTFN(15,("%s: %s: enter\n", sc->aue_dev.dv_xname,__func__));
1229 
1230 	if (sc->aue_dying)
1231 		return;
1232 
1233 	ifp = GET_IFP(sc);
1234 	mii = GET_MII(sc);
1235 	if (mii == NULL)
1236 		return;
1237 
1238 	s = splnet();
1239 
1240 	mii_tick(mii);
1241 	if (!sc->aue_link && mii->mii_media_status & IFM_ACTIVE &&
1242 	    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1243 		DPRINTFN(2,("%s: %s: got link\n",
1244 			    sc->aue_dev.dv_xname,__func__));
1245 		sc->aue_link++;
1246 		if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1247 			aue_start(ifp);
1248 	}
1249 
1250 	timeout_add_sec(&sc->aue_stat_ch, 1);
1251 
1252 	splx(s);
1253 }
1254 
1255 int
1256 aue_send(struct aue_softc *sc, struct mbuf *m, int idx)
1257 {
1258 	int			total_len;
1259 	struct aue_chain	*c;
1260 	usbd_status		err;
1261 
1262 	DPRINTFN(10,("%s: %s: enter\n", sc->aue_dev.dv_xname,__func__));
1263 
1264 	c = &sc->aue_cdata.aue_tx_chain[idx];
1265 
1266 	/*
1267 	 * Copy the mbuf data into a contiguous buffer, leaving two
1268 	 * bytes at the beginning to hold the frame length.
1269 	 */
1270 	m_copydata(m, 0, m->m_pkthdr.len, c->aue_buf + 2);
1271 	c->aue_mbuf = m;
1272 
1273 	/*
1274 	 * The ADMtek documentation says that the packet length is
1275 	 * supposed to be specified in the first two bytes of the
1276 	 * transfer, however it actually seems to ignore this info
1277 	 * and base the frame size on the bulk transfer length.
1278 	 */
1279 	c->aue_buf[0] = (u_int8_t)m->m_pkthdr.len;
1280 	c->aue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8);
1281 	total_len = m->m_pkthdr.len + 2;
1282 
1283 	usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_TX],
1284 	    c, c->aue_buf, total_len, USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
1285 	    AUE_TX_TIMEOUT, aue_txeof);
1286 
1287 	/* Transmit */
1288 	err = usbd_transfer(c->aue_xfer);
1289 	if (err != USBD_IN_PROGRESS) {
1290 		printf("%s: aue_send error=%s\n", sc->aue_dev.dv_xname,
1291 		       usbd_errstr(err));
1292 		/* Stop the interface from process context. */
1293 		usb_add_task(sc->aue_udev, &sc->aue_stop_task);
1294 		return (EIO);
1295 	}
1296 	DPRINTFN(5,("%s: %s: send %d bytes\n", sc->aue_dev.dv_xname,
1297 		    __func__, total_len));
1298 
1299 	sc->aue_cdata.aue_tx_cnt++;
1300 
1301 	return (0);
1302 }
1303 
1304 void
1305 aue_start(struct ifnet *ifp)
1306 {
1307 	struct aue_softc	*sc = ifp->if_softc;
1308 	struct mbuf		*m_head = NULL;
1309 
1310 	DPRINTFN(5,("%s: %s: enter, link=%d\n", sc->aue_dev.dv_xname,
1311 		    __func__, sc->aue_link));
1312 
1313 	if (sc->aue_dying)
1314 		return;
1315 
1316 	if (!sc->aue_link)
1317 		return;
1318 
1319 	if (ifp->if_flags & IFF_OACTIVE)
1320 		return;
1321 
1322 	IFQ_POLL(&ifp->if_snd, m_head);
1323 	if (m_head == NULL)
1324 		return;
1325 
1326 	if (aue_send(sc, m_head, 0)) {
1327 		ifp->if_flags |= IFF_OACTIVE;
1328 		return;
1329 	}
1330 
1331 	IFQ_DEQUEUE(&ifp->if_snd, m_head);
1332 
1333 #if NBPFILTER > 0
1334 	/*
1335 	 * If there's a BPF listener, bounce a copy of this frame
1336 	 * to him.
1337 	 */
1338 	if (ifp->if_bpf)
1339 		bpf_mtap(ifp->if_bpf, m_head, BPF_DIRECTION_OUT);
1340 #endif
1341 
1342 	ifp->if_flags |= IFF_OACTIVE;
1343 
1344 	/*
1345 	 * Set a timeout in case the chip goes out to lunch.
1346 	 */
1347 	ifp->if_timer = 5;
1348 }
1349 
1350 void
1351 aue_init(void *xsc)
1352 {
1353 	struct aue_softc	*sc = xsc;
1354 	struct ifnet		*ifp = GET_IFP(sc);
1355 	struct mii_data		*mii = GET_MII(sc);
1356 	int			i, s;
1357 	u_char			*eaddr;
1358 
1359 	DPRINTFN(5,("%s: %s: enter\n", sc->aue_dev.dv_xname, __func__));
1360 
1361 	if (sc->aue_dying)
1362 		return;
1363 
1364 	if (ifp->if_flags & IFF_RUNNING)
1365 		return;
1366 
1367 	s = splnet();
1368 
1369 	/*
1370 	 * Cancel pending I/O and free all RX/TX buffers.
1371 	 */
1372 	aue_reset(sc);
1373 
1374 	eaddr = sc->arpcom.ac_enaddr;
1375 	for (i = 0; i < ETHER_ADDR_LEN; i++)
1376 		aue_csr_write_1(sc, AUE_PAR0 + i, eaddr[i]);
1377 
1378 	 /* If we want promiscuous mode, set the allframes bit. */
1379 	if (ifp->if_flags & IFF_PROMISC)
1380 		AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1381 	else
1382 		AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1383 
1384 	/* Init TX ring. */
1385 	if (aue_tx_list_init(sc) == ENOBUFS) {
1386 		printf("%s: tx list init failed\n", sc->aue_dev.dv_xname);
1387 		splx(s);
1388 		return;
1389 	}
1390 
1391 	/* Init RX ring. */
1392 	if (aue_rx_list_init(sc) == ENOBUFS) {
1393 		printf("%s: rx list init failed\n", sc->aue_dev.dv_xname);
1394 		splx(s);
1395 		return;
1396 	}
1397 
1398 	/* Load the multicast filter. */
1399 	aue_setmulti(sc);
1400 
1401 	/* Enable RX and TX */
1402 	aue_csr_write_1(sc, AUE_CTL0, AUE_CTL0_RXSTAT_APPEND | AUE_CTL0_RX_ENB);
1403 	AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_TX_ENB);
1404 	AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_EP3_CLR);
1405 
1406 	mii_mediachg(mii);
1407 
1408 	if (sc->aue_ep[AUE_ENDPT_RX] == NULL) {
1409 		if (aue_openpipes(sc)) {
1410 			splx(s);
1411 			return;
1412 		}
1413 	}
1414 
1415 	ifp->if_flags |= IFF_RUNNING;
1416 	ifp->if_flags &= ~IFF_OACTIVE;
1417 
1418 	splx(s);
1419 
1420 	timeout_add_sec(&sc->aue_stat_ch, 1);
1421 }
1422 
1423 int
1424 aue_openpipes(struct aue_softc *sc)
1425 {
1426 	struct aue_chain	*c;
1427 	usbd_status		err;
1428 	int i;
1429 
1430 	/* Open RX and TX pipes. */
1431 	err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_RX],
1432 	    USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_RX]);
1433 	if (err) {
1434 		printf("%s: open rx pipe failed: %s\n",
1435 		    sc->aue_dev.dv_xname, usbd_errstr(err));
1436 		return (EIO);
1437 	}
1438 	err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_TX],
1439 	    USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_TX]);
1440 	if (err) {
1441 		printf("%s: open tx pipe failed: %s\n",
1442 		    sc->aue_dev.dv_xname, usbd_errstr(err));
1443 		return (EIO);
1444 	}
1445 	err = usbd_open_pipe_intr(sc->aue_iface, sc->aue_ed[AUE_ENDPT_INTR],
1446 	    USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_INTR], sc,
1447 	    &sc->aue_cdata.aue_ibuf, AUE_INTR_PKTLEN, aue_intr,
1448 	    AUE_INTR_INTERVAL);
1449 	if (err) {
1450 		printf("%s: open intr pipe failed: %s\n",
1451 		    sc->aue_dev.dv_xname, usbd_errstr(err));
1452 		return (EIO);
1453 	}
1454 
1455 	/* Start up the receive pipe. */
1456 	for (i = 0; i < AUE_RX_LIST_CNT; i++) {
1457 		c = &sc->aue_cdata.aue_rx_chain[i];
1458 		usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_RX],
1459 		    c, c->aue_buf, AUE_BUFSZ,
1460 		    USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT,
1461 		    aue_rxeof);
1462 		(void)usbd_transfer(c->aue_xfer); /* XXX */
1463 		DPRINTFN(5,("%s: %s: start read\n", sc->aue_dev.dv_xname,
1464 			    __func__));
1465 
1466 	}
1467 	return (0);
1468 }
1469 
1470 /*
1471  * Set media options.
1472  */
1473 int
1474 aue_ifmedia_upd(struct ifnet *ifp)
1475 {
1476 	struct aue_softc	*sc = ifp->if_softc;
1477 	struct mii_data		*mii = GET_MII(sc);
1478 
1479 	DPRINTFN(5,("%s: %s: enter\n", sc->aue_dev.dv_xname, __func__));
1480 
1481 	if (sc->aue_dying)
1482 		return (0);
1483 
1484 	sc->aue_link = 0;
1485 	if (mii->mii_instance) {
1486 		struct mii_softc	*miisc;
1487 		for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
1488 		    miisc = LIST_NEXT(miisc, mii_list))
1489 			 mii_phy_reset(miisc);
1490 	}
1491 	mii_mediachg(mii);
1492 
1493 	return (0);
1494 }
1495 
1496 /*
1497  * Report current media status.
1498  */
1499 void
1500 aue_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1501 {
1502 	struct aue_softc	*sc = ifp->if_softc;
1503 	struct mii_data		*mii = GET_MII(sc);
1504 
1505 	DPRINTFN(5,("%s: %s: enter\n", sc->aue_dev.dv_xname, __func__));
1506 
1507 	mii_pollstat(mii);
1508 	ifmr->ifm_active = mii->mii_media_active;
1509 	ifmr->ifm_status = mii->mii_media_status;
1510 }
1511 
1512 int
1513 aue_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
1514 {
1515 	struct aue_softc	*sc = ifp->if_softc;
1516 	struct ifaddr 		*ifa = (struct ifaddr *)data;
1517 	struct ifreq		*ifr = (struct ifreq *)data;
1518 	struct mii_data		*mii;
1519 	int			s, error = 0;
1520 
1521 	if (sc->aue_dying)
1522 		return (EIO);
1523 
1524 	s = splnet();
1525 
1526 	switch(command) {
1527 	case SIOCSIFADDR:
1528 		ifp->if_flags |= IFF_UP;
1529 		aue_init(sc);
1530 
1531 		switch (ifa->ifa_addr->sa_family) {
1532 #ifdef INET
1533 		case AF_INET:
1534 			arp_ifinit(&sc->arpcom, ifa);
1535 			break;
1536 #endif /* INET */
1537 		}
1538 		break;
1539 
1540 	case SIOCSIFFLAGS:
1541 		if (ifp->if_flags & IFF_UP) {
1542 			if (ifp->if_flags & IFF_RUNNING &&
1543 			    ifp->if_flags & IFF_PROMISC &&
1544 			    !(sc->aue_if_flags & IFF_PROMISC)) {
1545 				AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1546 			} else if (ifp->if_flags & IFF_RUNNING &&
1547 			    !(ifp->if_flags & IFF_PROMISC) &&
1548 			    sc->aue_if_flags & IFF_PROMISC) {
1549 				AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1550 			} else if (!(ifp->if_flags & IFF_RUNNING))
1551 				aue_init(sc);
1552 		} else {
1553 			if (ifp->if_flags & IFF_RUNNING)
1554 				aue_stop(sc);
1555 		}
1556 		sc->aue_if_flags = ifp->if_flags;
1557 		error = 0;
1558 		break;
1559 
1560 	case SIOCGIFMEDIA:
1561 	case SIOCSIFMEDIA:
1562 		mii = GET_MII(sc);
1563 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1564 		break;
1565 
1566 	default:
1567 		error = ether_ioctl(ifp, &sc->arpcom, command, data);
1568 	}
1569 
1570 	if (error == ENETRESET) {
1571 		if (ifp->if_flags & IFF_RUNNING)
1572 			aue_setmulti(sc);
1573 		error = 0;
1574 	}
1575 
1576 	splx(s);
1577 	return (error);
1578 }
1579 
1580 void
1581 aue_watchdog(struct ifnet *ifp)
1582 {
1583 	struct aue_softc	*sc = ifp->if_softc;
1584 	struct aue_chain	*c;
1585 	usbd_status		stat;
1586 	int			s;
1587 
1588 	DPRINTFN(5,("%s: %s: enter\n", sc->aue_dev.dv_xname, __func__));
1589 
1590 	ifp->if_oerrors++;
1591 	printf("%s: watchdog timeout\n", sc->aue_dev.dv_xname);
1592 
1593 	s = splusb();
1594 	c = &sc->aue_cdata.aue_tx_chain[0];
1595 	usbd_get_xfer_status(c->aue_xfer, NULL, NULL, NULL, &stat);
1596 	aue_txeof(c->aue_xfer, c, stat);
1597 
1598 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1599 		aue_start(ifp);
1600 	splx(s);
1601 }
1602 
1603 /*
1604  * Stop all chip I/O so that the kernel's probe routines don't
1605  * get confused by errant DMAs when rebooting.
1606  */
1607 void
1608 aue_shutdown(void *arg)
1609 {
1610 	struct aue_softc *sc = (struct aue_softc *)arg;
1611 
1612 	aue_reset(sc);
1613 	aue_stop(sc);
1614 }
1615 
1616 /*
1617  * Stop the adapter and free any mbufs allocated to the
1618  * RX and TX lists.
1619  */
1620 void
1621 aue_stop(struct aue_softc *sc)
1622 {
1623 	usbd_status		err;
1624 	struct ifnet		*ifp;
1625 	int			i;
1626 
1627 	DPRINTFN(5,("%s: %s: enter\n", sc->aue_dev.dv_xname, __func__));
1628 
1629 	ifp = GET_IFP(sc);
1630 	ifp->if_timer = 0;
1631 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1632 
1633 	aue_csr_write_1(sc, AUE_CTL0, 0);
1634 	aue_csr_write_1(sc, AUE_CTL1, 0);
1635 	aue_reset(sc);
1636 	timeout_del(&sc->aue_stat_ch);
1637 
1638 	/* Stop transfers. */
1639 	if (sc->aue_ep[AUE_ENDPT_RX] != NULL) {
1640 		err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_RX]);
1641 		if (err) {
1642 			printf("%s: abort rx pipe failed: %s\n",
1643 			    sc->aue_dev.dv_xname, usbd_errstr(err));
1644 		}
1645 		err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_RX]);
1646 		if (err) {
1647 			printf("%s: close rx pipe failed: %s\n",
1648 			    sc->aue_dev.dv_xname, usbd_errstr(err));
1649 		}
1650 		sc->aue_ep[AUE_ENDPT_RX] = NULL;
1651 	}
1652 
1653 	if (sc->aue_ep[AUE_ENDPT_TX] != NULL) {
1654 		err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_TX]);
1655 		if (err) {
1656 			printf("%s: abort tx pipe failed: %s\n",
1657 			    sc->aue_dev.dv_xname, usbd_errstr(err));
1658 		}
1659 		err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_TX]);
1660 		if (err) {
1661 			printf("%s: close tx pipe failed: %s\n",
1662 			    sc->aue_dev.dv_xname, usbd_errstr(err));
1663 		}
1664 		sc->aue_ep[AUE_ENDPT_TX] = NULL;
1665 	}
1666 
1667 	if (sc->aue_ep[AUE_ENDPT_INTR] != NULL) {
1668 		err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
1669 		if (err) {
1670 			printf("%s: abort intr pipe failed: %s\n",
1671 			    sc->aue_dev.dv_xname, usbd_errstr(err));
1672 		}
1673 		err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
1674 		if (err) {
1675 			printf("%s: close intr pipe failed: %s\n",
1676 			    sc->aue_dev.dv_xname, usbd_errstr(err));
1677 		}
1678 		sc->aue_ep[AUE_ENDPT_INTR] = NULL;
1679 	}
1680 
1681 	/* Free RX resources. */
1682 	for (i = 0; i < AUE_RX_LIST_CNT; i++) {
1683 		if (sc->aue_cdata.aue_rx_chain[i].aue_mbuf != NULL) {
1684 			m_freem(sc->aue_cdata.aue_rx_chain[i].aue_mbuf);
1685 			sc->aue_cdata.aue_rx_chain[i].aue_mbuf = NULL;
1686 		}
1687 		if (sc->aue_cdata.aue_rx_chain[i].aue_xfer != NULL) {
1688 			usbd_free_xfer(sc->aue_cdata.aue_rx_chain[i].aue_xfer);
1689 			sc->aue_cdata.aue_rx_chain[i].aue_xfer = NULL;
1690 		}
1691 	}
1692 
1693 	/* Free TX resources. */
1694 	for (i = 0; i < AUE_TX_LIST_CNT; i++) {
1695 		if (sc->aue_cdata.aue_tx_chain[i].aue_mbuf != NULL) {
1696 			m_freem(sc->aue_cdata.aue_tx_chain[i].aue_mbuf);
1697 			sc->aue_cdata.aue_tx_chain[i].aue_mbuf = NULL;
1698 		}
1699 		if (sc->aue_cdata.aue_tx_chain[i].aue_xfer != NULL) {
1700 			usbd_free_xfer(sc->aue_cdata.aue_tx_chain[i].aue_xfer);
1701 			sc->aue_cdata.aue_tx_chain[i].aue_xfer = NULL;
1702 		}
1703 	}
1704 
1705 	sc->aue_link = 0;
1706 }
1707