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