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