xref: /netbsd-src/sys/dev/usb/if_aue.c (revision ce2c90c7c172d95d2402a5b3d96d8f8e6d138a21)
1 /*	$NetBSD: if_aue.c,v 1.97 2006/10/12 01:31:59 christos 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.97 2006/10/12 01:31:59 christos 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, caddr_t);
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(caddr_t);
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(caddr_t addr)
584 {
585 	u_int32_t		idx, bit, data, crc;
586 
587 	/* Compute CRC for the address value. */
588 	crc = 0xFFFFFFFF; /* initial value */
589 
590 	for (idx = 0; idx < 6; idx++) {
591 		for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1)
592 			crc = (crc >> 1) ^ (((crc ^ data) & 1) ? AUE_POLY : 0);
593 	}
594 
595 	return (crc & ((1 << AUE_BITS) - 1));
596 }
597 
598 Static void
599 aue_setmulti(struct aue_softc *sc)
600 {
601 	struct ifnet		*ifp;
602 	struct ether_multi	*enm;
603 	struct ether_multistep	step;
604 	u_int32_t		h = 0, i;
605 
606 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __func__));
607 
608 	ifp = GET_IFP(sc);
609 
610 	if (ifp->if_flags & IFF_PROMISC) {
611 allmulti:
612 		ifp->if_flags |= IFF_ALLMULTI;
613 		AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
614 		return;
615 	}
616 
617 	AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
618 
619 	/* first, zot all the existing hash bits */
620 	for (i = 0; i < 8; i++)
621 		aue_csr_write_1(sc, AUE_MAR0 + i, 0);
622 
623 	/* now program new ones */
624 #if defined(__NetBSD__)
625 	ETHER_FIRST_MULTI(step, &sc->aue_ec, enm);
626 #else
627 	ETHER_FIRST_MULTI(step, &sc->arpcom, enm);
628 #endif
629 	while (enm != NULL) {
630 		if (memcmp(enm->enm_addrlo,
631 		    enm->enm_addrhi, ETHER_ADDR_LEN) != 0)
632 			goto allmulti;
633 
634 		h = aue_crc(enm->enm_addrlo);
635 		AUE_SETBIT(sc, AUE_MAR + (h >> 3), 1 << (h & 0x7));
636 		ETHER_NEXT_MULTI(step, enm);
637 	}
638 
639 	ifp->if_flags &= ~IFF_ALLMULTI;
640 }
641 
642 Static void
643 aue_reset_pegasus_II(struct aue_softc *sc)
644 {
645 	/* Magic constants taken from Linux driver. */
646 	aue_csr_write_1(sc, AUE_REG_1D, 0);
647 	aue_csr_write_1(sc, AUE_REG_7B, 2);
648 #if 0
649 	if ((sc->aue_flags & HAS_HOME_PNA) && mii_mode)
650 		aue_csr_write_1(sc, AUE_REG_81, 6);
651 	else
652 #endif
653 		aue_csr_write_1(sc, AUE_REG_81, 2);
654 }
655 
656 Static void
657 aue_reset(struct aue_softc *sc)
658 {
659 	int		i;
660 
661 	DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __func__));
662 
663 	AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_RESETMAC);
664 
665 	for (i = 0; i < AUE_TIMEOUT; i++) {
666 		if (!(aue_csr_read_1(sc, AUE_CTL1) & AUE_CTL1_RESETMAC))
667 			break;
668 	}
669 
670 	if (i == AUE_TIMEOUT)
671 		printf("%s: reset failed\n", USBDEVNAME(sc->aue_dev));
672 
673 #if 0
674 	/* XXX what is mii_mode supposed to be */
675 	if (sc->aue_mii_mode && (sc->aue_flags & PNA))
676 		aue_csr_write_1(sc, AUE_GPIO1, 0x34);
677 	else
678 		aue_csr_write_1(sc, AUE_GPIO1, 0x26);
679 #endif
680 
681 	/*
682 	 * The PHY(s) attached to the Pegasus chip may be held
683 	 * in reset until we flip on the GPIO outputs. Make sure
684 	 * to set the GPIO pins high so that the PHY(s) will
685 	 * be enabled.
686 	 *
687 	 * Note: We force all of the GPIO pins low first, *then*
688 	 * enable the ones we want.
689   	 */
690 	if (sc->aue_flags & LSYS) {
691 		/* Grrr. LinkSys has to be different from everyone else. */
692 		aue_csr_write_1(sc, AUE_GPIO0,
693 		    AUE_GPIO_SEL0 | AUE_GPIO_SEL1);
694 	} else {
695 		aue_csr_write_1(sc, AUE_GPIO0,
696 		    AUE_GPIO_OUT0 | AUE_GPIO_SEL0);
697 	}
698   	aue_csr_write_1(sc, AUE_GPIO0,
699 	    AUE_GPIO_OUT0 | AUE_GPIO_SEL0 | AUE_GPIO_SEL1);
700 
701 	if (sc->aue_flags & PII)
702 		aue_reset_pegasus_II(sc);
703 
704 	/* Wait a little while for the chip to get its brains in order. */
705 	delay(10000);		/* XXX */
706 }
707 
708 /*
709  * Probe for a Pegasus chip.
710  */
711 USB_MATCH(aue)
712 {
713 	USB_MATCH_START(aue, uaa);
714 
715 	if (uaa->iface != NULL)
716 		return (UMATCH_NONE);
717 
718 	return (aue_lookup(uaa->vendor, uaa->product) != NULL ?
719 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
720 }
721 
722 /*
723  * Attach the interface. Allocate softc structures, do ifmedia
724  * setup and ethernet/BPF attach.
725  */
726 USB_ATTACH(aue)
727 {
728 	USB_ATTACH_START(aue, sc, uaa);
729 	char			*devinfop;
730 	int			s;
731 	u_char			eaddr[ETHER_ADDR_LEN];
732 	struct ifnet		*ifp;
733 	struct mii_data		*mii;
734 	usbd_device_handle	dev = uaa->device;
735 	usbd_interface_handle	iface;
736 	usbd_status		err;
737 	usb_interface_descriptor_t	*id;
738 	usb_endpoint_descriptor_t	*ed;
739 	int			i;
740 
741 	DPRINTFN(5,(" : aue_attach: sc=%p", sc));
742 
743 	devinfop = usbd_devinfo_alloc(uaa->device, 0);
744 	USB_ATTACH_SETUP;
745 	printf("%s: %s\n", USBDEVNAME(sc->aue_dev), devinfop);
746 	usbd_devinfo_free(devinfop);
747 
748 	err = usbd_set_config_no(dev, AUE_CONFIG_NO, 1);
749 	if (err) {
750 		printf("%s: setting config no failed\n",
751 		    USBDEVNAME(sc->aue_dev));
752 		USB_ATTACH_ERROR_RETURN;
753 	}
754 
755 	usb_init_task(&sc->aue_tick_task, aue_tick_task, sc);
756 	usb_init_task(&sc->aue_stop_task, (void (*)(void *))aue_stop, sc);
757 	lockinit(&sc->aue_mii_lock, PZERO, "auemii", 0, 0);
758 
759 	err = usbd_device2interface_handle(dev, AUE_IFACE_IDX, &iface);
760 	if (err) {
761 		printf("%s: getting interface handle failed\n",
762 		    USBDEVNAME(sc->aue_dev));
763 		USB_ATTACH_ERROR_RETURN;
764 	}
765 #if defined(__NetBSD__)
766 	err = workqueue_create(&sc->wqp, USBDEVNAME(sc->aue_dev),
767 		aue_multiwork, sc, 0, IPL_NET, 0);
768 
769 	if (err) {
770 		printf("%s: creating multicast configuration work queue\n",
771 		    USBDEVNAME(sc->aue_dev));
772 		USB_ATTACH_ERROR_RETURN;
773 	}
774 #endif
775 	sc->aue_flags = aue_lookup(uaa->vendor, uaa->product)->aue_flags;
776 
777 	sc->aue_udev = dev;
778 	sc->aue_iface = iface;
779 	sc->aue_product = uaa->product;
780 	sc->aue_vendor = uaa->vendor;
781 
782 	id = usbd_get_interface_descriptor(iface);
783 
784 	/* Find endpoints. */
785 	for (i = 0; i < id->bNumEndpoints; i++) {
786 		ed = usbd_interface2endpoint_descriptor(iface, i);
787 		if (ed == NULL) {
788 			printf("%s: couldn't get endpoint descriptor %d\n",
789 			    USBDEVNAME(sc->aue_dev), i);
790 			USB_ATTACH_ERROR_RETURN;
791 		}
792 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
793 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
794 			sc->aue_ed[AUE_ENDPT_RX] = ed->bEndpointAddress;
795 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
796 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
797 			sc->aue_ed[AUE_ENDPT_TX] = ed->bEndpointAddress;
798 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
799 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
800 			sc->aue_ed[AUE_ENDPT_INTR] = ed->bEndpointAddress;
801 		}
802 	}
803 
804 	if (sc->aue_ed[AUE_ENDPT_RX] == 0 || sc->aue_ed[AUE_ENDPT_TX] == 0 ||
805 	    sc->aue_ed[AUE_ENDPT_INTR] == 0) {
806 		printf("%s: missing endpoint\n", USBDEVNAME(sc->aue_dev));
807 		USB_ATTACH_ERROR_RETURN;
808 	}
809 
810 
811 	s = splnet();
812 
813 	/* Reset the adapter. */
814 	aue_reset(sc);
815 
816 	/*
817 	 * Get station address from the EEPROM.
818 	 */
819 	aue_read_mac(sc, eaddr);
820 
821 	/*
822 	 * A Pegasus chip was detected. Inform the world.
823 	 */
824 	ifp = GET_IFP(sc);
825 	printf("%s: Ethernet address %s\n", USBDEVNAME(sc->aue_dev),
826 	    ether_sprintf(eaddr));
827 
828 	/* Initialize interface info.*/
829 	ifp->if_softc = sc;
830 	ifp->if_mtu = ETHERMTU;
831 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
832 	ifp->if_ioctl = aue_ioctl;
833 	ifp->if_start = aue_start;
834 	ifp->if_watchdog = aue_watchdog;
835 #if defined(__OpenBSD__)
836 	ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
837 #endif
838 	strncpy(ifp->if_xname, USBDEVNAME(sc->aue_dev), IFNAMSIZ);
839 
840 	IFQ_SET_READY(&ifp->if_snd);
841 
842 	/* Initialize MII/media info. */
843 	mii = &sc->aue_mii;
844 	mii->mii_ifp = ifp;
845 	mii->mii_readreg = aue_miibus_readreg;
846 	mii->mii_writereg = aue_miibus_writereg;
847 	mii->mii_statchg = aue_miibus_statchg;
848 	mii->mii_flags = MIIF_AUTOTSLEEP;
849 	ifmedia_init(&mii->mii_media, 0, aue_ifmedia_upd, aue_ifmedia_sts);
850 	mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0);
851 	if (LIST_FIRST(&mii->mii_phys) == NULL) {
852 		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
853 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
854 	} else
855 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
856 
857 	/* Attach the interface. */
858 	if_attach(ifp);
859 	Ether_ifattach(ifp, eaddr);
860 #if NRND > 0
861 	rnd_attach_source(&sc->rnd_source, USBDEVNAME(sc->aue_dev),
862 	    RND_TYPE_NET, 0);
863 #endif
864 
865 	usb_callout_init(sc->aue_stat_ch);
866 
867 	sc->aue_attached = 1;
868 	splx(s);
869 
870 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->aue_udev,
871 			   USBDEV(sc->aue_dev));
872 
873 	USB_ATTACH_SUCCESS_RETURN;
874 }
875 
876 USB_DETACH(aue)
877 {
878 	USB_DETACH_START(aue, sc);
879 	struct ifnet		*ifp = GET_IFP(sc);
880 	int			s;
881 
882 	DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __func__));
883 
884 	if (!sc->aue_attached) {
885 		/* Detached before attached finished, so just bail out. */
886 		return (0);
887 	}
888 
889 	usb_uncallout(sc->aue_stat_ch, aue_tick, sc);
890 	/*
891 	 * Remove any pending tasks.  They cannot be executing because they run
892 	 * in the same thread as detach.
893 	 */
894 	usb_rem_task(sc->aue_udev, &sc->aue_tick_task);
895 	usb_rem_task(sc->aue_udev, &sc->aue_stop_task);
896 
897 	s = splusb();
898 
899 	if (ifp->if_flags & IFF_RUNNING)
900 		aue_stop(sc);
901 
902 #if defined(__NetBSD__)
903 #if NRND > 0
904 	rnd_detach_source(&sc->rnd_source);
905 #endif
906 	mii_detach(&sc->aue_mii, MII_PHY_ANY, MII_OFFSET_ANY);
907 	ifmedia_delete_instance(&sc->aue_mii.mii_media, IFM_INST_ANY);
908 	ether_ifdetach(ifp);
909 #endif /* __NetBSD__ */
910 
911 	if_detach(ifp);
912 
913 #ifdef DIAGNOSTIC
914 	if (sc->aue_ep[AUE_ENDPT_TX] != NULL ||
915 	    sc->aue_ep[AUE_ENDPT_RX] != NULL ||
916 	    sc->aue_ep[AUE_ENDPT_INTR] != NULL)
917 		printf("%s: detach has active endpoints\n",
918 		       USBDEVNAME(sc->aue_dev));
919 #endif
920 
921 	sc->aue_attached = 0;
922 
923 	if (--sc->aue_refcnt >= 0) {
924 		/* Wait for processes to go away. */
925 		usb_detach_wait(USBDEV(sc->aue_dev));
926 	}
927 	splx(s);
928 
929 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->aue_udev,
930 			   USBDEV(sc->aue_dev));
931 
932 	return (0);
933 }
934 
935 int
936 aue_activate(device_ptr_t self, enum devact act)
937 {
938 	struct aue_softc *sc = (struct aue_softc *)self;
939 
940 	DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __func__));
941 
942 	switch (act) {
943 	case DVACT_ACTIVATE:
944 		return (EOPNOTSUPP);
945 		break;
946 
947 	case DVACT_DEACTIVATE:
948 		if_deactivate(&sc->aue_ec.ec_if);
949 		sc->aue_dying = 1;
950 		break;
951 	}
952 	return (0);
953 }
954 
955 /*
956  * Initialize an RX descriptor and attach an MBUF cluster.
957  */
958 Static int
959 aue_newbuf(struct aue_softc *sc, struct aue_chain *c, struct mbuf *m)
960 {
961 	struct mbuf		*m_new = NULL;
962 
963 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__func__));
964 
965 	if (m == NULL) {
966 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
967 		if (m_new == NULL) {
968 			printf("%s: no memory for rx list "
969 			    "-- packet dropped!\n", USBDEVNAME(sc->aue_dev));
970 			return (ENOBUFS);
971 		}
972 
973 		MCLGET(m_new, M_DONTWAIT);
974 		if (!(m_new->m_flags & M_EXT)) {
975 			printf("%s: no memory for rx list "
976 			    "-- packet dropped!\n", USBDEVNAME(sc->aue_dev));
977 			m_freem(m_new);
978 			return (ENOBUFS);
979 		}
980 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
981 	} else {
982 		m_new = m;
983 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
984 		m_new->m_data = m_new->m_ext.ext_buf;
985 	}
986 
987 	m_adj(m_new, ETHER_ALIGN);
988 	c->aue_mbuf = m_new;
989 
990 	return (0);
991 }
992 
993 Static int
994 aue_rx_list_init(struct aue_softc *sc)
995 {
996 	struct aue_cdata	*cd;
997 	struct aue_chain	*c;
998 	int			i;
999 
1000 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __func__));
1001 
1002 	cd = &sc->aue_cdata;
1003 	for (i = 0; i < AUE_RX_LIST_CNT; i++) {
1004 		c = &cd->aue_rx_chain[i];
1005 		c->aue_sc = sc;
1006 		c->aue_idx = i;
1007 		if (aue_newbuf(sc, c, NULL) == ENOBUFS)
1008 			return (ENOBUFS);
1009 		if (c->aue_xfer == NULL) {
1010 			c->aue_xfer = usbd_alloc_xfer(sc->aue_udev);
1011 			if (c->aue_xfer == NULL)
1012 				return (ENOBUFS);
1013 			c->aue_buf = usbd_alloc_buffer(c->aue_xfer, AUE_BUFSZ);
1014 			if (c->aue_buf == NULL)
1015 				return (ENOBUFS); /* XXX free xfer */
1016 		}
1017 	}
1018 
1019 	return (0);
1020 }
1021 
1022 Static int
1023 aue_tx_list_init(struct aue_softc *sc)
1024 {
1025 	struct aue_cdata	*cd;
1026 	struct aue_chain	*c;
1027 	int			i;
1028 
1029 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __func__));
1030 
1031 	cd = &sc->aue_cdata;
1032 	for (i = 0; i < AUE_TX_LIST_CNT; i++) {
1033 		c = &cd->aue_tx_chain[i];
1034 		c->aue_sc = sc;
1035 		c->aue_idx = i;
1036 		c->aue_mbuf = NULL;
1037 		if (c->aue_xfer == NULL) {
1038 			c->aue_xfer = usbd_alloc_xfer(sc->aue_udev);
1039 			if (c->aue_xfer == NULL)
1040 				return (ENOBUFS);
1041 			c->aue_buf = usbd_alloc_buffer(c->aue_xfer, AUE_BUFSZ);
1042 			if (c->aue_buf == NULL)
1043 				return (ENOBUFS);
1044 		}
1045 	}
1046 
1047 	return (0);
1048 }
1049 
1050 Static void
1051 aue_intr(usbd_xfer_handle xfer __unused, usbd_private_handle priv,
1052     usbd_status status)
1053 {
1054 	struct aue_softc	*sc = priv;
1055 	struct ifnet		*ifp = GET_IFP(sc);
1056 	struct aue_intrpkt	*p = &sc->aue_cdata.aue_ibuf;
1057 
1058 	DPRINTFN(15,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__func__));
1059 
1060 	if (sc->aue_dying)
1061 		return;
1062 
1063 	if (!(ifp->if_flags & IFF_RUNNING))
1064 		return;
1065 
1066 	if (status != USBD_NORMAL_COMPLETION) {
1067 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1068 			return;
1069 		}
1070 		sc->aue_intr_errs++;
1071 		if (usbd_ratecheck(&sc->aue_rx_notice)) {
1072 			printf("%s: %u usb errors on intr: %s\n",
1073 			    USBDEVNAME(sc->aue_dev), sc->aue_intr_errs,
1074 			    usbd_errstr(status));
1075 			sc->aue_intr_errs = 0;
1076 		}
1077 		if (status == USBD_STALLED)
1078 			usbd_clear_endpoint_stall_async(sc->aue_ep[AUE_ENDPT_RX]);
1079 		return;
1080 	}
1081 
1082 	if (p->aue_txstat0)
1083 		ifp->if_oerrors++;
1084 
1085 	if (p->aue_txstat0 & (AUE_TXSTAT0_LATECOLL | AUE_TXSTAT0_EXCESSCOLL))
1086 		ifp->if_collisions++;
1087 }
1088 
1089 /*
1090  * A frame has been uploaded: pass the resulting mbuf chain up to
1091  * the higher level protocols.
1092  */
1093 Static void
1094 aue_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
1095 {
1096 	struct aue_chain	*c = priv;
1097 	struct aue_softc	*sc = c->aue_sc;
1098 	struct ifnet		*ifp = GET_IFP(sc);
1099 	struct mbuf		*m;
1100 	u_int32_t		total_len;
1101 	struct aue_rxpkt	r;
1102 	int			s;
1103 
1104 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__func__));
1105 
1106 	if (sc->aue_dying)
1107 		return;
1108 
1109 	if (!(ifp->if_flags & IFF_RUNNING))
1110 		return;
1111 
1112 	if (status != USBD_NORMAL_COMPLETION) {
1113 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
1114 			return;
1115 		sc->aue_rx_errs++;
1116 		if (usbd_ratecheck(&sc->aue_rx_notice)) {
1117 			printf("%s: %u usb errors on rx: %s\n",
1118 			    USBDEVNAME(sc->aue_dev), sc->aue_rx_errs,
1119 			    usbd_errstr(status));
1120 			sc->aue_rx_errs = 0;
1121 		}
1122 		if (status == USBD_STALLED)
1123 			usbd_clear_endpoint_stall_async(sc->aue_ep[AUE_ENDPT_RX]);
1124 		goto done;
1125 	}
1126 
1127 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
1128 
1129 	memcpy(mtod(c->aue_mbuf, char *), c->aue_buf, total_len);
1130 
1131 	if (total_len <= 4 + ETHER_CRC_LEN) {
1132 		ifp->if_ierrors++;
1133 		goto done;
1134 	}
1135 
1136 	memcpy(&r, c->aue_buf + total_len - 4, sizeof(r));
1137 
1138 	/* Turn off all the non-error bits in the rx status word. */
1139 	r.aue_rxstat &= AUE_RXSTAT_MASK;
1140 	if (r.aue_rxstat) {
1141 		ifp->if_ierrors++;
1142 		goto done;
1143 	}
1144 
1145 	/* No errors; receive the packet. */
1146 	m = c->aue_mbuf;
1147 	total_len -= ETHER_CRC_LEN + 4;
1148 	m->m_pkthdr.len = m->m_len = total_len;
1149 	ifp->if_ipackets++;
1150 
1151 	m->m_pkthdr.rcvif = ifp;
1152 
1153 	s = splnet();
1154 
1155 	/* XXX ugly */
1156 	if (aue_newbuf(sc, c, NULL) == ENOBUFS) {
1157 		ifp->if_ierrors++;
1158 		goto done1;
1159 	}
1160 
1161 #if NBPFILTER > 0
1162 	/*
1163 	 * Handle BPF listeners. Let the BPF user see the packet, but
1164 	 * don't pass it up to the ether_input() layer unless it's
1165 	 * a broadcast packet, multicast packet, matches our ethernet
1166 	 * address or the interface is in promiscuous mode.
1167 	 */
1168 	if (ifp->if_bpf)
1169 		BPF_MTAP(ifp, m);
1170 #endif
1171 
1172 	DPRINTFN(10,("%s: %s: deliver %d\n", USBDEVNAME(sc->aue_dev),
1173 		    __func__, m->m_len));
1174 	IF_INPUT(ifp, m);
1175  done1:
1176 	splx(s);
1177 
1178  done:
1179 
1180 	/* Setup new transfer. */
1181 	usbd_setup_xfer(xfer, sc->aue_ep[AUE_ENDPT_RX],
1182 	    c, c->aue_buf, AUE_BUFSZ,
1183 	    USBD_SHORT_XFER_OK | USBD_NO_COPY,
1184 	    USBD_NO_TIMEOUT, aue_rxeof);
1185 	usbd_transfer(xfer);
1186 
1187 	DPRINTFN(10,("%s: %s: start rx\n", USBDEVNAME(sc->aue_dev),
1188 		    __func__));
1189 }
1190 
1191 /*
1192  * A frame was downloaded to the chip. It's safe for us to clean up
1193  * the list buffers.
1194  */
1195 
1196 Static void
1197 aue_txeof(usbd_xfer_handle xfer __unused, usbd_private_handle priv,
1198     usbd_status status)
1199 {
1200 	struct aue_chain	*c = priv;
1201 	struct aue_softc	*sc = c->aue_sc;
1202 	struct ifnet		*ifp = GET_IFP(sc);
1203 	int			s;
1204 
1205 	if (sc->aue_dying)
1206 		return;
1207 
1208 	s = splnet();
1209 
1210 	DPRINTFN(10,("%s: %s: enter status=%d\n", USBDEVNAME(sc->aue_dev),
1211 		    __func__, status));
1212 
1213 	ifp->if_timer = 0;
1214 	ifp->if_flags &= ~IFF_OACTIVE;
1215 
1216 	if (status != USBD_NORMAL_COMPLETION) {
1217 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1218 			splx(s);
1219 			return;
1220 		}
1221 		ifp->if_oerrors++;
1222 		printf("%s: usb error on tx: %s\n", USBDEVNAME(sc->aue_dev),
1223 		    usbd_errstr(status));
1224 		if (status == USBD_STALLED)
1225 			usbd_clear_endpoint_stall_async(sc->aue_ep[AUE_ENDPT_TX]);
1226 		splx(s);
1227 		return;
1228 	}
1229 
1230 	ifp->if_opackets++;
1231 
1232 	m_freem(c->aue_mbuf);
1233 	c->aue_mbuf = NULL;
1234 
1235 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1236 		aue_start(ifp);
1237 
1238 	splx(s);
1239 }
1240 
1241 Static void
1242 aue_tick(void *xsc)
1243 {
1244 	struct aue_softc	*sc = xsc;
1245 
1246 	DPRINTFN(15,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__func__));
1247 
1248 	if (sc == NULL)
1249 		return;
1250 
1251 	if (sc->aue_dying)
1252 		return;
1253 
1254 	/* Perform periodic stuff in process context. */
1255 	usb_add_task(sc->aue_udev, &sc->aue_tick_task);
1256 }
1257 
1258 Static void
1259 aue_tick_task(void *xsc)
1260 {
1261 	struct aue_softc	*sc = xsc;
1262 	struct ifnet		*ifp;
1263 	struct mii_data		*mii;
1264 	int			s;
1265 
1266 	DPRINTFN(15,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__func__));
1267 
1268 	if (sc->aue_dying)
1269 		return;
1270 
1271 	ifp = GET_IFP(sc);
1272 	mii = GET_MII(sc);
1273 	if (mii == NULL)
1274 		return;
1275 
1276 	s = splnet();
1277 
1278 	mii_tick(mii);
1279 	if (!sc->aue_link) {
1280 		mii_pollstat(mii); /* XXX FreeBSD has removed this call */
1281 		if (mii->mii_media_status & IFM_ACTIVE &&
1282 		    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1283 			DPRINTFN(2,("%s: %s: got link\n",
1284 				    USBDEVNAME(sc->aue_dev),__func__));
1285 			sc->aue_link++;
1286 			if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1287 				aue_start(ifp);
1288 		}
1289 	}
1290 
1291 	usb_callout(sc->aue_stat_ch, hz, aue_tick, sc);
1292 
1293 	splx(s);
1294 }
1295 
1296 Static int
1297 aue_send(struct aue_softc *sc, struct mbuf *m, int idx)
1298 {
1299 	int			total_len;
1300 	struct aue_chain	*c;
1301 	usbd_status		err;
1302 
1303 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev),__func__));
1304 
1305 	c = &sc->aue_cdata.aue_tx_chain[idx];
1306 
1307 	/*
1308 	 * Copy the mbuf data into a contiguous buffer, leaving two
1309 	 * bytes at the beginning to hold the frame length.
1310 	 */
1311 	m_copydata(m, 0, m->m_pkthdr.len, c->aue_buf + 2);
1312 	c->aue_mbuf = m;
1313 
1314 	/*
1315 	 * The ADMtek documentation says that the packet length is
1316 	 * supposed to be specified in the first two bytes of the
1317 	 * transfer, however it actually seems to ignore this info
1318 	 * and base the frame size on the bulk transfer length.
1319 	 */
1320 	c->aue_buf[0] = (u_int8_t)m->m_pkthdr.len;
1321 	c->aue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8);
1322 	total_len = m->m_pkthdr.len + 2;
1323 
1324 	usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_TX],
1325 	    c, c->aue_buf, total_len, USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
1326 	    AUE_TX_TIMEOUT, aue_txeof);
1327 
1328 	/* Transmit */
1329 	err = usbd_transfer(c->aue_xfer);
1330 	if (err != USBD_IN_PROGRESS) {
1331 		printf("%s: aue_send error=%s\n", USBDEVNAME(sc->aue_dev),
1332 		       usbd_errstr(err));
1333 		/* Stop the interface from process context. */
1334 		usb_add_task(sc->aue_udev, &sc->aue_stop_task);
1335 		return (EIO);
1336 	}
1337 	DPRINTFN(5,("%s: %s: send %d bytes\n", USBDEVNAME(sc->aue_dev),
1338 		    __func__, total_len));
1339 
1340 	sc->aue_cdata.aue_tx_cnt++;
1341 
1342 	return (0);
1343 }
1344 
1345 Static void
1346 aue_start(struct ifnet *ifp)
1347 {
1348 	struct aue_softc	*sc = ifp->if_softc;
1349 	struct mbuf		*m_head = NULL;
1350 
1351 	DPRINTFN(5,("%s: %s: enter, link=%d\n", USBDEVNAME(sc->aue_dev),
1352 		    __func__, sc->aue_link));
1353 
1354 	if (sc->aue_dying)
1355 		return;
1356 
1357 	if (!sc->aue_link)
1358 		return;
1359 
1360 	if (ifp->if_flags & IFF_OACTIVE)
1361 		return;
1362 
1363 	IFQ_POLL(&ifp->if_snd, m_head);
1364 	if (m_head == NULL)
1365 		return;
1366 
1367 	if (aue_send(sc, m_head, 0)) {
1368 		ifp->if_flags |= IFF_OACTIVE;
1369 		return;
1370 	}
1371 
1372 	IFQ_DEQUEUE(&ifp->if_snd, m_head);
1373 
1374 #if NBPFILTER > 0
1375 	/*
1376 	 * If there's a BPF listener, bounce a copy of this frame
1377 	 * to him.
1378 	 */
1379 	if (ifp->if_bpf)
1380 		BPF_MTAP(ifp, m_head);
1381 #endif
1382 
1383 	ifp->if_flags |= IFF_OACTIVE;
1384 
1385 	/*
1386 	 * Set a timeout in case the chip goes out to lunch.
1387 	 */
1388 	ifp->if_timer = 5;
1389 }
1390 
1391 Static void
1392 aue_init(void *xsc)
1393 {
1394 	struct aue_softc	*sc = xsc;
1395 	struct ifnet		*ifp = GET_IFP(sc);
1396 	struct mii_data		*mii = GET_MII(sc);
1397 	int			i, s;
1398 	u_char			*eaddr;
1399 
1400 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __func__));
1401 
1402 	if (sc->aue_dying)
1403 		return;
1404 
1405 	if (ifp->if_flags & IFF_RUNNING)
1406 		return;
1407 
1408 	s = splnet();
1409 
1410 	/*
1411 	 * Cancel pending I/O and free all RX/TX buffers.
1412 	 */
1413 	aue_reset(sc);
1414 
1415 #if defined(__OpenBSD__)
1416 	eaddr = sc->arpcom.ac_enaddr;
1417 #elif defined(__NetBSD__)
1418 	eaddr = LLADDR(ifp->if_sadl);
1419 #endif /* defined(__NetBSD__) */
1420 	for (i = 0; i < ETHER_ADDR_LEN; i++)
1421 		aue_csr_write_1(sc, AUE_PAR0 + i, eaddr[i]);
1422 
1423 	 /* If we want promiscuous mode, set the allframes bit. */
1424 	if (ifp->if_flags & IFF_PROMISC)
1425 		AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1426 	else
1427 		AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1428 
1429 	/* Init TX ring. */
1430 	if (aue_tx_list_init(sc) == ENOBUFS) {
1431 		printf("%s: tx list init failed\n", USBDEVNAME(sc->aue_dev));
1432 		splx(s);
1433 		return;
1434 	}
1435 
1436 	/* Init RX ring. */
1437 	if (aue_rx_list_init(sc) == ENOBUFS) {
1438 		printf("%s: rx list init failed\n", USBDEVNAME(sc->aue_dev));
1439 		splx(s);
1440 		return;
1441 	}
1442 
1443 	/* Load the multicast filter. */
1444 	aue_setmulti(sc);
1445 
1446 	/* Enable RX and TX */
1447 	aue_csr_write_1(sc, AUE_CTL0, AUE_CTL0_RXSTAT_APPEND | AUE_CTL0_RX_ENB);
1448 	AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_TX_ENB);
1449 	AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_EP3_CLR);
1450 
1451 	mii_mediachg(mii);
1452 
1453 	if (sc->aue_ep[AUE_ENDPT_RX] == NULL) {
1454 		if (aue_openpipes(sc)) {
1455 			splx(s);
1456 			return;
1457 		}
1458 	}
1459 
1460 	ifp->if_flags |= IFF_RUNNING;
1461 	ifp->if_flags &= ~IFF_OACTIVE;
1462 
1463 	splx(s);
1464 
1465 	usb_callout(sc->aue_stat_ch, hz, aue_tick, sc);
1466 }
1467 
1468 Static int
1469 aue_openpipes(struct aue_softc *sc)
1470 {
1471 	struct aue_chain	*c;
1472 	usbd_status		err;
1473 	int i;
1474 
1475 	/* Open RX and TX pipes. */
1476 	err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_RX],
1477 	    USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_RX]);
1478 	if (err) {
1479 		printf("%s: open rx pipe failed: %s\n",
1480 		    USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1481 		return (EIO);
1482 	}
1483 	err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_TX],
1484 	    USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_TX]);
1485 	if (err) {
1486 		printf("%s: open tx pipe failed: %s\n",
1487 		    USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1488 		return (EIO);
1489 	}
1490 	err = usbd_open_pipe_intr(sc->aue_iface, sc->aue_ed[AUE_ENDPT_INTR],
1491 	    USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_INTR], sc,
1492 	    &sc->aue_cdata.aue_ibuf, AUE_INTR_PKTLEN, aue_intr,
1493 	    AUE_INTR_INTERVAL);
1494 	if (err) {
1495 		printf("%s: open intr pipe failed: %s\n",
1496 		    USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1497 		return (EIO);
1498 	}
1499 
1500 	/* Start up the receive pipe. */
1501 	for (i = 0; i < AUE_RX_LIST_CNT; i++) {
1502 		c = &sc->aue_cdata.aue_rx_chain[i];
1503 		usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_RX],
1504 		    c, c->aue_buf, AUE_BUFSZ,
1505 		    USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT,
1506 		    aue_rxeof);
1507 		(void)usbd_transfer(c->aue_xfer); /* XXX */
1508 		DPRINTFN(5,("%s: %s: start read\n", USBDEVNAME(sc->aue_dev),
1509 			    __func__));
1510 
1511 	}
1512 	return (0);
1513 }
1514 
1515 /*
1516  * Set media options.
1517  */
1518 Static int
1519 aue_ifmedia_upd(struct ifnet *ifp)
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), __func__));
1525 
1526 	if (sc->aue_dying)
1527 		return (0);
1528 
1529 	sc->aue_link = 0;
1530 	if (mii->mii_instance) {
1531 		struct mii_softc	*miisc;
1532 		for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
1533 		    miisc = LIST_NEXT(miisc, mii_list))
1534 			 mii_phy_reset(miisc);
1535 	}
1536 	mii_mediachg(mii);
1537 
1538 	return (0);
1539 }
1540 
1541 /*
1542  * Report current media status.
1543  */
1544 Static void
1545 aue_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1546 {
1547 	struct aue_softc	*sc = ifp->if_softc;
1548 	struct mii_data		*mii = GET_MII(sc);
1549 
1550 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __func__));
1551 
1552 	mii_pollstat(mii);
1553 	ifmr->ifm_active = mii->mii_media_active;
1554 	ifmr->ifm_status = mii->mii_media_status;
1555 }
1556 
1557 Static int
1558 aue_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
1559 {
1560 	struct aue_softc	*sc = ifp->if_softc;
1561 	struct ifaddr 		*ifa = (struct ifaddr *)data;
1562 	struct ifreq		*ifr = (struct ifreq *)data;
1563 	struct mii_data		*mii;
1564 	int			s, error = 0;
1565 
1566 	if (sc->aue_dying)
1567 		return (EIO);
1568 
1569 	s = splnet();
1570 
1571 	switch(command) {
1572 	case SIOCSIFADDR:
1573 		ifp->if_flags |= IFF_UP;
1574 		aue_init(sc);
1575 
1576 		switch (ifa->ifa_addr->sa_family) {
1577 #ifdef INET
1578 		case AF_INET:
1579 #if defined(__NetBSD__)
1580 			arp_ifinit(ifp, ifa);
1581 #else
1582 			arp_ifinit(&sc->arpcom, ifa);
1583 #endif
1584 			break;
1585 #endif /* INET */
1586 		}
1587 		break;
1588 
1589 	case SIOCSIFMTU:
1590 		if (ifr->ifr_mtu > ETHERMTU)
1591 			error = EINVAL;
1592 		else
1593 			ifp->if_mtu = ifr->ifr_mtu;
1594 		break;
1595 
1596 	case SIOCSIFFLAGS:
1597 		if (ifp->if_flags & IFF_UP) {
1598 			if (ifp->if_flags & IFF_RUNNING &&
1599 			    ifp->if_flags & IFF_PROMISC &&
1600 			    !(sc->aue_if_flags & IFF_PROMISC)) {
1601 				AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1602 			} else if (ifp->if_flags & IFF_RUNNING &&
1603 			    !(ifp->if_flags & IFF_PROMISC) &&
1604 			    sc->aue_if_flags & IFF_PROMISC) {
1605 				AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
1606 			} else if (!(ifp->if_flags & IFF_RUNNING))
1607 				aue_init(sc);
1608 		} else {
1609 			if (ifp->if_flags & IFF_RUNNING)
1610 				aue_stop(sc);
1611 		}
1612 		sc->aue_if_flags = ifp->if_flags;
1613 		error = 0;
1614 		break;
1615 	case SIOCADDMULTI:
1616 	case SIOCDELMULTI:
1617 		error = (command == SIOCADDMULTI) ?
1618 			ether_addmulti(ifr, &sc->aue_ec) :
1619 			ether_delmulti(ifr, &sc->aue_ec);
1620 		if (error == ENETRESET) {
1621 			if (ifp->if_flags & IFF_RUNNING) {
1622 #if defined(__NetBSD__)
1623 				workqueue_enqueue(sc->wqp,&sc->wk);
1624 				/* XXX */
1625 #else
1626 				aue_init(sc);
1627 				aue_setmulti(sc);
1628 #endif
1629 			}
1630 			error = 0;
1631 		}
1632 		break;
1633 	case SIOCGIFMEDIA:
1634 	case SIOCSIFMEDIA:
1635 		mii = GET_MII(sc);
1636 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1637 		break;
1638 	default:
1639 		error = EINVAL;
1640 		break;
1641 	}
1642 
1643 	splx(s);
1644 
1645 	return (error);
1646 }
1647 
1648 Static void
1649 aue_watchdog(struct ifnet *ifp)
1650 {
1651 	struct aue_softc	*sc = ifp->if_softc;
1652 	struct aue_chain	*c;
1653 	usbd_status		stat;
1654 	int			s;
1655 
1656 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __func__));
1657 
1658 	ifp->if_oerrors++;
1659 	printf("%s: watchdog timeout\n", USBDEVNAME(sc->aue_dev));
1660 
1661 	s = splusb();
1662 	c = &sc->aue_cdata.aue_tx_chain[0];
1663 	usbd_get_xfer_status(c->aue_xfer, NULL, NULL, NULL, &stat);
1664 	aue_txeof(c->aue_xfer, c, stat);
1665 
1666 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1667 		aue_start(ifp);
1668 	splx(s);
1669 }
1670 
1671 /*
1672  * Stop the adapter and free any mbufs allocated to the
1673  * RX and TX lists.
1674  */
1675 Static void
1676 aue_stop(struct aue_softc *sc)
1677 {
1678 	usbd_status		err;
1679 	struct ifnet		*ifp;
1680 	int			i;
1681 
1682 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->aue_dev), __func__));
1683 
1684 	ifp = GET_IFP(sc);
1685 	ifp->if_timer = 0;
1686 
1687 	aue_csr_write_1(sc, AUE_CTL0, 0);
1688 	aue_csr_write_1(sc, AUE_CTL1, 0);
1689 	aue_reset(sc);
1690 	usb_uncallout(sc->aue_stat_ch, aue_tick, sc);
1691 
1692 	/* Stop transfers. */
1693 	if (sc->aue_ep[AUE_ENDPT_RX] != NULL) {
1694 		err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_RX]);
1695 		if (err) {
1696 			printf("%s: abort rx pipe failed: %s\n",
1697 			    USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1698 		}
1699 		err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_RX]);
1700 		if (err) {
1701 			printf("%s: close rx pipe failed: %s\n",
1702 			    USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1703 		}
1704 		sc->aue_ep[AUE_ENDPT_RX] = NULL;
1705 	}
1706 
1707 	if (sc->aue_ep[AUE_ENDPT_TX] != NULL) {
1708 		err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_TX]);
1709 		if (err) {
1710 			printf("%s: abort tx pipe failed: %s\n",
1711 			    USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1712 		}
1713 		err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_TX]);
1714 		if (err) {
1715 			printf("%s: close tx pipe failed: %s\n",
1716 			    USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1717 		}
1718 		sc->aue_ep[AUE_ENDPT_TX] = NULL;
1719 	}
1720 
1721 	if (sc->aue_ep[AUE_ENDPT_INTR] != NULL) {
1722 		err = usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
1723 		if (err) {
1724 			printf("%s: abort intr pipe failed: %s\n",
1725 			    USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1726 		}
1727 		err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
1728 		if (err) {
1729 			printf("%s: close intr pipe failed: %s\n",
1730 			    USBDEVNAME(sc->aue_dev), usbd_errstr(err));
1731 		}
1732 		sc->aue_ep[AUE_ENDPT_INTR] = NULL;
1733 	}
1734 
1735 	/* Free RX resources. */
1736 	for (i = 0; i < AUE_RX_LIST_CNT; i++) {
1737 		if (sc->aue_cdata.aue_rx_chain[i].aue_mbuf != NULL) {
1738 			m_freem(sc->aue_cdata.aue_rx_chain[i].aue_mbuf);
1739 			sc->aue_cdata.aue_rx_chain[i].aue_mbuf = NULL;
1740 		}
1741 		if (sc->aue_cdata.aue_rx_chain[i].aue_xfer != NULL) {
1742 			usbd_free_xfer(sc->aue_cdata.aue_rx_chain[i].aue_xfer);
1743 			sc->aue_cdata.aue_rx_chain[i].aue_xfer = NULL;
1744 		}
1745 	}
1746 
1747 	/* Free TX resources. */
1748 	for (i = 0; i < AUE_TX_LIST_CNT; i++) {
1749 		if (sc->aue_cdata.aue_tx_chain[i].aue_mbuf != NULL) {
1750 			m_freem(sc->aue_cdata.aue_tx_chain[i].aue_mbuf);
1751 			sc->aue_cdata.aue_tx_chain[i].aue_mbuf = NULL;
1752 		}
1753 		if (sc->aue_cdata.aue_tx_chain[i].aue_xfer != NULL) {
1754 			usbd_free_xfer(sc->aue_cdata.aue_tx_chain[i].aue_xfer);
1755 			sc->aue_cdata.aue_tx_chain[i].aue_xfer = NULL;
1756 		}
1757 	}
1758 
1759 	sc->aue_link = 0;
1760 
1761 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1762 }
1763 
1764 #if defined(__NetBSD__)
1765 Static void
1766 aue_multiwork(struct work *wkp, void *arg) {
1767 	struct aue_softc *sc;
1768 
1769 	sc = (struct aue_softc *)arg;
1770 	(void)wkp;
1771 
1772 	aue_init(sc);
1773 	/* XXX called by aue_init, but rc ifconfig hangs without it: */
1774 	aue_setmulti(sc);
1775 }
1776 #endif
1777