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