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