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