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