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