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