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