xref: /openbsd-src/sys/dev/usb/if_aue.c (revision db4dc9aafbed936e879d4362ca141e998a6e925c)
1 /*	$OpenBSD: if_aue.c,v 1.100 2015/06/24 09:40:54 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_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 || uaa->configno != 1)
688 		return (UMATCH_NONE);
689 
690 	return (aue_lookup(uaa->vendor, uaa->product) != NULL ?
691 		UMATCH_VENDOR_PRODUCT_CONF_IFACE : 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 = uaa->iface;
709 	usb_interface_descriptor_t	*id;
710 	usb_endpoint_descriptor_t	*ed;
711 	int			i;
712 
713 	DPRINTFN(5,(" : aue_attach: sc=%p", sc));
714 
715 	sc->aue_udev = dev;
716 
717 	usb_init_task(&sc->aue_tick_task, aue_tick_task, sc,
718 	    USB_TASK_TYPE_GENERIC);
719 	usb_init_task(&sc->aue_stop_task, (void (*)(void *))aue_stop, sc,
720 	    USB_TASK_TYPE_GENERIC);
721 	rw_init(&sc->aue_mii_lock, "auemii");
722 
723 	sc->aue_flags = aue_lookup(uaa->vendor, uaa->product)->aue_flags;
724 
725 	sc->aue_iface = iface;
726 	sc->aue_product = uaa->product;
727 	sc->aue_vendor = uaa->vendor;
728 
729 	id = usbd_get_interface_descriptor(iface);
730 
731 	/* Find endpoints. */
732 	for (i = 0; i < id->bNumEndpoints; i++) {
733 		ed = usbd_interface2endpoint_descriptor(iface, i);
734 		if (ed == NULL) {
735 			printf("%s: couldn't get endpoint descriptor %d\n",
736 			    sc->aue_dev.dv_xname, i);
737 			return;
738 		}
739 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
740 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
741 			sc->aue_ed[AUE_ENDPT_RX] = ed->bEndpointAddress;
742 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
743 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
744 			sc->aue_ed[AUE_ENDPT_TX] = ed->bEndpointAddress;
745 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
746 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
747 			sc->aue_ed[AUE_ENDPT_INTR] = ed->bEndpointAddress;
748 		}
749 	}
750 
751 	if (sc->aue_ed[AUE_ENDPT_RX] == 0 || sc->aue_ed[AUE_ENDPT_TX] == 0 ||
752 	    sc->aue_ed[AUE_ENDPT_INTR] == 0) {
753 		printf("%s: missing endpoint\n", sc->aue_dev.dv_xname);
754 		return;
755 	}
756 
757 
758 	s = splnet();
759 
760 	/* Reset the adapter. */
761 	aue_reset(sc);
762 
763 	/*
764 	 * Get station address from the EEPROM.
765 	 */
766 	aue_read_mac(sc, eaddr);
767 
768 	/*
769 	 * A Pegasus chip was detected. Inform the world.
770 	 */
771 	ifp = GET_IFP(sc);
772 	printf("%s: address %s\n", sc->aue_dev.dv_xname,
773 	    ether_sprintf(eaddr));
774 
775 	bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
776 
777 	/* Initialize interface info.*/
778 	ifp->if_softc = sc;
779 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
780 	ifp->if_ioctl = aue_ioctl;
781 	ifp->if_start = aue_start;
782 	ifp->if_watchdog = aue_watchdog;
783 	strlcpy(ifp->if_xname, sc->aue_dev.dv_xname, IFNAMSIZ);
784 
785 	IFQ_SET_READY(&ifp->if_snd);
786 
787 	ifp->if_capabilities = IFCAP_VLAN_MTU;
788 
789 	/* Initialize MII/media info. */
790 	mii = &sc->aue_mii;
791 	mii->mii_ifp = ifp;
792 	mii->mii_readreg = aue_miibus_readreg;
793 	mii->mii_writereg = aue_miibus_writereg;
794 	mii->mii_statchg = aue_miibus_statchg;
795 	mii->mii_flags = MIIF_AUTOTSLEEP;
796 	ifmedia_init(&mii->mii_media, 0, aue_ifmedia_upd, aue_ifmedia_sts);
797 	mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0);
798 	if (LIST_FIRST(&mii->mii_phys) == NULL) {
799 		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
800 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
801 	} else
802 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
803 
804 	/* Attach the interface. */
805 	if_attach(ifp);
806 	ether_ifattach(ifp);
807 
808 	timeout_set(&sc->aue_stat_ch, aue_tick, sc);
809 
810 	splx(s);
811 }
812 
813 int
814 aue_detach(struct device *self, int flags)
815 {
816 	struct aue_softc	*sc = (struct aue_softc *)self;
817 	struct ifnet		*ifp = GET_IFP(sc);
818 	int			s;
819 
820 	DPRINTFN(2,("%s: %s: enter\n", sc->aue_dev.dv_xname, __func__));
821 
822 	if (timeout_initialized(&sc->aue_stat_ch))
823 		timeout_del(&sc->aue_stat_ch);
824 
825 	/*
826 	 * Remove any pending tasks.  They cannot be executing because they run
827 	 * in the same thread as detach.
828 	 */
829 	usb_rem_task(sc->aue_udev, &sc->aue_tick_task);
830 	usb_rem_task(sc->aue_udev, &sc->aue_stop_task);
831 
832 	s = splusb();
833 
834 	if (ifp->if_flags & IFF_RUNNING)
835 		aue_stop(sc);
836 
837 	mii_detach(&sc->aue_mii, MII_PHY_ANY, MII_OFFSET_ANY);
838 	ifmedia_delete_instance(&sc->aue_mii.mii_media, IFM_INST_ANY);
839 	if (ifp->if_softc != NULL) {
840 		ether_ifdetach(ifp);
841 		if_detach(ifp);
842 	}
843 
844 #ifdef DIAGNOSTIC
845 	if (sc->aue_ep[AUE_ENDPT_TX] != NULL ||
846 	    sc->aue_ep[AUE_ENDPT_RX] != NULL ||
847 	    sc->aue_ep[AUE_ENDPT_INTR] != NULL)
848 		printf("%s: detach has active endpoints\n",
849 		       sc->aue_dev.dv_xname);
850 #endif
851 
852 	if (--sc->aue_refcnt >= 0) {
853 		/* Wait for processes to go away. */
854 		usb_detach_wait(&sc->aue_dev);
855 	}
856 	splx(s);
857 
858 	return (0);
859 }
860 
861 /*
862  * Initialize an RX descriptor and attach an MBUF cluster.
863  */
864 int
865 aue_newbuf(struct aue_softc *sc, struct aue_chain *c, struct mbuf *m)
866 {
867 	struct mbuf		*m_new = NULL;
868 
869 	DPRINTFN(10,("%s: %s: enter\n", sc->aue_dev.dv_xname,__func__));
870 
871 	if (m == NULL) {
872 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
873 		if (m_new == NULL) {
874 			printf("%s: no memory for rx list "
875 			    "-- packet dropped!\n", sc->aue_dev.dv_xname);
876 			return (ENOBUFS);
877 		}
878 
879 		MCLGET(m_new, M_DONTWAIT);
880 		if (!(m_new->m_flags & M_EXT)) {
881 			printf("%s: no memory for rx list "
882 			    "-- packet dropped!\n", sc->aue_dev.dv_xname);
883 			m_freem(m_new);
884 			return (ENOBUFS);
885 		}
886 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
887 	} else {
888 		m_new = m;
889 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
890 		m_new->m_data = m_new->m_ext.ext_buf;
891 	}
892 
893 	m_adj(m_new, ETHER_ALIGN);
894 	c->aue_mbuf = m_new;
895 
896 	return (0);
897 }
898 
899 int
900 aue_rx_list_init(struct aue_softc *sc)
901 {
902 	struct aue_cdata	*cd;
903 	struct aue_chain	*c;
904 	int			i;
905 
906 	DPRINTFN(5,("%s: %s: enter\n", sc->aue_dev.dv_xname, __func__));
907 
908 	cd = &sc->aue_cdata;
909 	for (i = 0; i < AUE_RX_LIST_CNT; i++) {
910 		c = &cd->aue_rx_chain[i];
911 		c->aue_sc = sc;
912 		c->aue_idx = i;
913 		if (aue_newbuf(sc, c, NULL) == ENOBUFS)
914 			return (ENOBUFS);
915 		if (c->aue_xfer == NULL) {
916 			c->aue_xfer = usbd_alloc_xfer(sc->aue_udev);
917 			if (c->aue_xfer == NULL)
918 				return (ENOBUFS);
919 			c->aue_buf = usbd_alloc_buffer(c->aue_xfer, AUE_BUFSZ);
920 			if (c->aue_buf == NULL)
921 				return (ENOBUFS); /* XXX free xfer */
922 		}
923 	}
924 
925 	return (0);
926 }
927 
928 int
929 aue_tx_list_init(struct aue_softc *sc)
930 {
931 	struct aue_cdata	*cd;
932 	struct aue_chain	*c;
933 	int			i;
934 
935 	DPRINTFN(5,("%s: %s: enter\n", sc->aue_dev.dv_xname, __func__));
936 
937 	cd = &sc->aue_cdata;
938 	for (i = 0; i < AUE_TX_LIST_CNT; i++) {
939 		c = &cd->aue_tx_chain[i];
940 		c->aue_sc = sc;
941 		c->aue_idx = i;
942 		c->aue_mbuf = NULL;
943 		if (c->aue_xfer == NULL) {
944 			c->aue_xfer = usbd_alloc_xfer(sc->aue_udev);
945 			if (c->aue_xfer == NULL)
946 				return (ENOBUFS);
947 			c->aue_buf = usbd_alloc_buffer(c->aue_xfer, AUE_BUFSZ);
948 			if (c->aue_buf == NULL)
949 				return (ENOBUFS);
950 		}
951 	}
952 
953 	return (0);
954 }
955 
956 void
957 aue_intr(struct usbd_xfer *xfer, void *priv, usbd_status status)
958 {
959 	struct aue_softc	*sc = priv;
960 	struct ifnet		*ifp = GET_IFP(sc);
961 	struct aue_intrpkt	*p = &sc->aue_cdata.aue_ibuf;
962 
963 	DPRINTFN(15,("%s: %s: enter\n", sc->aue_dev.dv_xname,__func__));
964 
965 	if (usbd_is_dying(sc->aue_udev))
966 		return;
967 
968 	if (!(ifp->if_flags & IFF_RUNNING))
969 		return;
970 
971 	if (status != USBD_NORMAL_COMPLETION) {
972 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
973 			return;
974 		}
975 		sc->aue_intr_errs++;
976 		if (usbd_ratecheck(&sc->aue_rx_notice)) {
977 			printf("%s: %u usb errors on intr: %s\n",
978 			    sc->aue_dev.dv_xname, sc->aue_intr_errs,
979 			    usbd_errstr(status));
980 			sc->aue_intr_errs = 0;
981 		}
982 		if (status == USBD_STALLED)
983 			usbd_clear_endpoint_stall_async(sc->aue_ep[AUE_ENDPT_RX]);
984 		return;
985 	}
986 
987 	if (p->aue_txstat0)
988 		ifp->if_oerrors++;
989 
990 	if (p->aue_txstat0 & (AUE_TXSTAT0_LATECOLL | AUE_TXSTAT0_EXCESSCOLL))
991 		ifp->if_collisions++;
992 }
993 
994 /*
995  * A frame has been uploaded: pass the resulting mbuf chain up to
996  * the higher level protocols.
997  */
998 void
999 aue_rxeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
1000 {
1001 	struct aue_chain	*c = priv;
1002 	struct aue_softc	*sc = c->aue_sc;
1003 	struct ifnet		*ifp = GET_IFP(sc);
1004 	struct mbuf		*m;
1005 	struct mbuf_list	ml = MBUF_LIST_INITIALIZER();
1006 	u_int32_t		total_len;
1007 	struct aue_rxpkt	r;
1008 	int			s;
1009 
1010 	DPRINTFN(10,("%s: %s: enter\n", sc->aue_dev.dv_xname,__func__));
1011 
1012 	if (usbd_is_dying(sc->aue_udev))
1013 		return;
1014 
1015 	if (!(ifp->if_flags & IFF_RUNNING))
1016 		return;
1017 
1018 	if (status != USBD_NORMAL_COMPLETION) {
1019 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
1020 			return;
1021 		sc->aue_rx_errs++;
1022 		if (usbd_ratecheck(&sc->aue_rx_notice)) {
1023 			printf("%s: %u usb errors on rx: %s\n",
1024 			    sc->aue_dev.dv_xname, sc->aue_rx_errs,
1025 			    usbd_errstr(status));
1026 			sc->aue_rx_errs = 0;
1027 		}
1028 		if (status == USBD_STALLED)
1029 			usbd_clear_endpoint_stall_async(sc->aue_ep[AUE_ENDPT_RX]);
1030 		goto done;
1031 	}
1032 
1033 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
1034 
1035 	memcpy(mtod(c->aue_mbuf, char *), c->aue_buf, total_len);
1036 
1037 	if (total_len <= 4 + ETHER_CRC_LEN) {
1038 		ifp->if_ierrors++;
1039 		goto done;
1040 	}
1041 
1042 	memcpy(&r, c->aue_buf + total_len - 4, sizeof(r));
1043 
1044 	/* Turn off all the non-error bits in the rx status word. */
1045 	r.aue_rxstat &= AUE_RXSTAT_MASK;
1046 	if (r.aue_rxstat) {
1047 		ifp->if_ierrors++;
1048 		goto done;
1049 	}
1050 
1051 	/* No errors; receive the packet. */
1052 	m = c->aue_mbuf;
1053 	total_len -= ETHER_CRC_LEN + 4;
1054 	m->m_pkthdr.len = m->m_len = total_len;
1055 	ml_enqueue(&ml, m);
1056 
1057 	if (aue_newbuf(sc, c, NULL) == ENOBUFS) {
1058 		ifp->if_ierrors++;
1059 		goto done;
1060 	}
1061 
1062 	s = splnet();
1063 	if_input(ifp, &ml);
1064 	splx(s);
1065 
1066  done:
1067 
1068 	/* Setup new transfer. */
1069 	usbd_setup_xfer(xfer, sc->aue_ep[AUE_ENDPT_RX],
1070 	    c, c->aue_buf, AUE_BUFSZ,
1071 	    USBD_SHORT_XFER_OK | USBD_NO_COPY,
1072 	    USBD_NO_TIMEOUT, aue_rxeof);
1073 	usbd_transfer(xfer);
1074 
1075 	DPRINTFN(10,("%s: %s: start rx\n", sc->aue_dev.dv_xname,
1076 		    __func__));
1077 }
1078 
1079 /*
1080  * A frame was downloaded to the chip. It's safe for us to clean up
1081  * the list buffers.
1082  */
1083 
1084 void
1085 aue_txeof(struct usbd_xfer *xfer, void *priv, usbd_status status)
1086 {
1087 	struct aue_chain	*c = priv;
1088 	struct aue_softc	*sc = c->aue_sc;
1089 	struct ifnet		*ifp = GET_IFP(sc);
1090 	int			s;
1091 
1092 	if (usbd_is_dying(sc->aue_udev))
1093 		return;
1094 
1095 	s = splnet();
1096 
1097 	DPRINTFN(10,("%s: %s: enter status=%d\n", sc->aue_dev.dv_xname,
1098 		    __func__, status));
1099 
1100 	ifp->if_timer = 0;
1101 	ifp->if_flags &= ~IFF_OACTIVE;
1102 
1103 	if (status != USBD_NORMAL_COMPLETION) {
1104 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
1105 			splx(s);
1106 			return;
1107 		}
1108 		ifp->if_oerrors++;
1109 		printf("%s: usb error on tx: %s\n", sc->aue_dev.dv_xname,
1110 		    usbd_errstr(status));
1111 		if (status == USBD_STALLED)
1112 			usbd_clear_endpoint_stall_async(sc->aue_ep[AUE_ENDPT_TX]);
1113 		splx(s);
1114 		return;
1115 	}
1116 
1117 	ifp->if_opackets++;
1118 
1119 	m_freem(c->aue_mbuf);
1120 	c->aue_mbuf = NULL;
1121 
1122 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1123 		aue_start(ifp);
1124 
1125 	splx(s);
1126 }
1127 
1128 void
1129 aue_tick(void *xsc)
1130 {
1131 	struct aue_softc	*sc = xsc;
1132 
1133 	DPRINTFN(15,("%s: %s: enter\n", sc->aue_dev.dv_xname,__func__));
1134 
1135 	if (sc == NULL)
1136 		return;
1137 
1138 	if (usbd_is_dying(sc->aue_udev))
1139 		return;
1140 
1141 	/* Perform periodic stuff in process context. */
1142 	usb_add_task(sc->aue_udev, &sc->aue_tick_task);
1143 }
1144 
1145 void
1146 aue_tick_task(void *xsc)
1147 {
1148 	struct aue_softc	*sc = xsc;
1149 	struct ifnet		*ifp;
1150 	struct mii_data		*mii;
1151 	int			s;
1152 
1153 	DPRINTFN(15,("%s: %s: enter\n", sc->aue_dev.dv_xname,__func__));
1154 
1155 	if (usbd_is_dying(sc->aue_udev))
1156 		return;
1157 
1158 	ifp = GET_IFP(sc);
1159 	mii = GET_MII(sc);
1160 	if (mii == NULL)
1161 		return;
1162 
1163 	s = splnet();
1164 
1165 	mii_tick(mii);
1166 	if (!sc->aue_link && mii->mii_media_status & IFM_ACTIVE &&
1167 	    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1168 		DPRINTFN(2,("%s: %s: got link\n",
1169 			    sc->aue_dev.dv_xname,__func__));
1170 		sc->aue_link++;
1171 		if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1172 			aue_start(ifp);
1173 	}
1174 
1175 	timeout_add_sec(&sc->aue_stat_ch, 1);
1176 
1177 	splx(s);
1178 }
1179 
1180 int
1181 aue_send(struct aue_softc *sc, struct mbuf *m, int idx)
1182 {
1183 	int			total_len;
1184 	struct aue_chain	*c;
1185 	usbd_status		err;
1186 
1187 	DPRINTFN(10,("%s: %s: enter\n", sc->aue_dev.dv_xname,__func__));
1188 
1189 	c = &sc->aue_cdata.aue_tx_chain[idx];
1190 
1191 	/*
1192 	 * Copy the mbuf data into a contiguous buffer, leaving two
1193 	 * bytes at the beginning to hold the frame length.
1194 	 */
1195 	m_copydata(m, 0, m->m_pkthdr.len, c->aue_buf + 2);
1196 	c->aue_mbuf = m;
1197 
1198 	/*
1199 	 * The ADMtek documentation says that the packet length is
1200 	 * supposed to be specified in the first two bytes of the
1201 	 * transfer, however it actually seems to ignore this info
1202 	 * and base the frame size on the bulk transfer length.
1203 	 */
1204 	c->aue_buf[0] = (u_int8_t)m->m_pkthdr.len;
1205 	c->aue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8);
1206 	total_len = m->m_pkthdr.len + 2;
1207 
1208 	usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_TX],
1209 	    c, c->aue_buf, total_len, USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
1210 	    AUE_TX_TIMEOUT, aue_txeof);
1211 
1212 	/* Transmit */
1213 	err = usbd_transfer(c->aue_xfer);
1214 	if (err != USBD_IN_PROGRESS) {
1215 		printf("%s: aue_send error=%s\n", sc->aue_dev.dv_xname,
1216 		       usbd_errstr(err));
1217 		/* Stop the interface from process context. */
1218 		usb_add_task(sc->aue_udev, &sc->aue_stop_task);
1219 		return (EIO);
1220 	}
1221 	DPRINTFN(5,("%s: %s: send %d bytes\n", sc->aue_dev.dv_xname,
1222 		    __func__, total_len));
1223 
1224 	sc->aue_cdata.aue_tx_cnt++;
1225 
1226 	return (0);
1227 }
1228 
1229 void
1230 aue_start(struct ifnet *ifp)
1231 {
1232 	struct aue_softc	*sc = ifp->if_softc;
1233 	struct mbuf		*m_head = NULL;
1234 
1235 	DPRINTFN(5,("%s: %s: enter, link=%d\n", sc->aue_dev.dv_xname,
1236 		    __func__, sc->aue_link));
1237 
1238 	if (usbd_is_dying(sc->aue_udev))
1239 		return;
1240 
1241 	if (!sc->aue_link)
1242 		return;
1243 
1244 	if (ifp->if_flags & IFF_OACTIVE)
1245 		return;
1246 
1247 	IFQ_POLL(&ifp->if_snd, m_head);
1248 	if (m_head == NULL)
1249 		return;
1250 
1251 	if (aue_send(sc, m_head, 0)) {
1252 		ifp->if_flags |= IFF_OACTIVE;
1253 		return;
1254 	}
1255 
1256 	IFQ_DEQUEUE(&ifp->if_snd, m_head);
1257 
1258 #if NBPFILTER > 0
1259 	/*
1260 	 * If there's a BPF listener, bounce a copy of this frame
1261 	 * to him.
1262 	 */
1263 	if (ifp->if_bpf)
1264 		bpf_mtap(ifp->if_bpf, m_head, BPF_DIRECTION_OUT);
1265 #endif
1266 
1267 	ifp->if_flags |= IFF_OACTIVE;
1268 
1269 	/*
1270 	 * Set a timeout in case the chip goes out to lunch.
1271 	 */
1272 	ifp->if_timer = 5;
1273 }
1274 
1275 void
1276 aue_init(void *xsc)
1277 {
1278 	struct aue_softc	*sc = xsc;
1279 	struct ifnet		*ifp = GET_IFP(sc);
1280 	struct mii_data		*mii = GET_MII(sc);
1281 	int			i, s;
1282 	u_char			*eaddr;
1283 
1284 	DPRINTFN(5,("%s: %s: enter\n", sc->aue_dev.dv_xname, __func__));
1285 
1286 	if (usbd_is_dying(sc->aue_udev))
1287 		return;
1288 
1289 	s = splnet();
1290 
1291 	/*
1292 	 * Cancel pending I/O and free all RX/TX buffers.
1293 	 */
1294 	aue_reset(sc);
1295 
1296 	eaddr = sc->arpcom.ac_enaddr;
1297 	for (i = 0; i < ETHER_ADDR_LEN; i++)
1298 		aue_csr_write_1(sc, AUE_PAR0 + i, eaddr[i]);
1299 
1300 	/* Init TX ring. */
1301 	if (aue_tx_list_init(sc) == ENOBUFS) {
1302 		printf("%s: tx list init failed\n", sc->aue_dev.dv_xname);
1303 		splx(s);
1304 		return;
1305 	}
1306 
1307 	/* Init RX ring. */
1308 	if (aue_rx_list_init(sc) == ENOBUFS) {
1309 		printf("%s: rx list init failed\n", sc->aue_dev.dv_xname);
1310 		splx(s);
1311 		return;
1312 	}
1313 
1314 	/* Program promiscuous mode and multicast filters. */
1315 	aue_iff(sc);
1316 
1317 	/* Enable RX and TX */
1318 	AUE_SETBIT(sc, AUE_CTL0,
1319 	    AUE_CTL0_RXSTAT_APPEND | AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB);
1320 	AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_EP3_CLR);
1321 
1322 	mii_mediachg(mii);
1323 
1324 	if (sc->aue_ep[AUE_ENDPT_RX] == NULL) {
1325 		if (aue_openpipes(sc)) {
1326 			splx(s);
1327 			return;
1328 		}
1329 	}
1330 
1331 	ifp->if_flags |= IFF_RUNNING;
1332 	ifp->if_flags &= ~IFF_OACTIVE;
1333 
1334 	splx(s);
1335 
1336 	timeout_add_sec(&sc->aue_stat_ch, 1);
1337 }
1338 
1339 int
1340 aue_openpipes(struct aue_softc *sc)
1341 {
1342 	struct aue_chain	*c;
1343 	usbd_status		err;
1344 	int i;
1345 
1346 	/* Open RX and TX pipes. */
1347 	err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_RX],
1348 	    USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_RX]);
1349 	if (err) {
1350 		printf("%s: open rx pipe failed: %s\n",
1351 		    sc->aue_dev.dv_xname, usbd_errstr(err));
1352 		return (EIO);
1353 	}
1354 	err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_TX],
1355 	    USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_TX]);
1356 	if (err) {
1357 		printf("%s: open tx pipe failed: %s\n",
1358 		    sc->aue_dev.dv_xname, usbd_errstr(err));
1359 		return (EIO);
1360 	}
1361 	err = usbd_open_pipe_intr(sc->aue_iface, sc->aue_ed[AUE_ENDPT_INTR],
1362 	    USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_INTR], sc,
1363 	    &sc->aue_cdata.aue_ibuf, AUE_INTR_PKTLEN, aue_intr,
1364 	    AUE_INTR_INTERVAL);
1365 	if (err) {
1366 		printf("%s: open intr pipe failed: %s\n",
1367 		    sc->aue_dev.dv_xname, usbd_errstr(err));
1368 		return (EIO);
1369 	}
1370 
1371 	/* Start up the receive pipe. */
1372 	for (i = 0; i < AUE_RX_LIST_CNT; i++) {
1373 		c = &sc->aue_cdata.aue_rx_chain[i];
1374 		usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_RX],
1375 		    c, c->aue_buf, AUE_BUFSZ,
1376 		    USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT,
1377 		    aue_rxeof);
1378 		(void)usbd_transfer(c->aue_xfer); /* XXX */
1379 		DPRINTFN(5,("%s: %s: start read\n", sc->aue_dev.dv_xname,
1380 			    __func__));
1381 
1382 	}
1383 	return (0);
1384 }
1385 
1386 /*
1387  * Set media options.
1388  */
1389 int
1390 aue_ifmedia_upd(struct ifnet *ifp)
1391 {
1392 	struct aue_softc	*sc = ifp->if_softc;
1393 	struct mii_data		*mii = GET_MII(sc);
1394 
1395 	DPRINTFN(5,("%s: %s: enter\n", sc->aue_dev.dv_xname, __func__));
1396 
1397 	if (usbd_is_dying(sc->aue_udev))
1398 		return (0);
1399 
1400 	sc->aue_link = 0;
1401 	if (mii->mii_instance) {
1402 		struct mii_softc	*miisc;
1403 		for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
1404 		    miisc = LIST_NEXT(miisc, mii_list))
1405 			 mii_phy_reset(miisc);
1406 	}
1407 	mii_mediachg(mii);
1408 
1409 	return (0);
1410 }
1411 
1412 /*
1413  * Report current media status.
1414  */
1415 void
1416 aue_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1417 {
1418 	struct aue_softc	*sc = ifp->if_softc;
1419 	struct mii_data		*mii = GET_MII(sc);
1420 
1421 	DPRINTFN(5,("%s: %s: enter\n", sc->aue_dev.dv_xname, __func__));
1422 
1423 	mii_pollstat(mii);
1424 	ifmr->ifm_active = mii->mii_media_active;
1425 	ifmr->ifm_status = mii->mii_media_status;
1426 }
1427 
1428 int
1429 aue_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
1430 {
1431 	struct aue_softc	*sc = ifp->if_softc;
1432 	struct ifaddr 		*ifa = (struct ifaddr *)data;
1433 	struct ifreq		*ifr = (struct ifreq *)data;
1434 	int			s, error = 0;
1435 
1436 	if (usbd_is_dying(sc->aue_udev))
1437 		return (EIO);
1438 
1439 	s = splnet();
1440 
1441 	switch(command) {
1442 	case SIOCSIFADDR:
1443 		ifp->if_flags |= IFF_UP;
1444 		if (!(ifp->if_flags & IFF_RUNNING))
1445 			aue_init(sc);
1446 		if (ifa->ifa_addr->sa_family == AF_INET)
1447 			arp_ifinit(&sc->arpcom, ifa);
1448 		break;
1449 
1450 	case SIOCSIFFLAGS:
1451 		if (ifp->if_flags & IFF_UP) {
1452 			if (ifp->if_flags & IFF_RUNNING)
1453 				error = ENETRESET;
1454 			else
1455 				aue_init(sc);
1456 		} else {
1457 			if (ifp->if_flags & IFF_RUNNING)
1458 				aue_stop(sc);
1459 		}
1460 		break;
1461 
1462 	case SIOCGIFMEDIA:
1463 	case SIOCSIFMEDIA:
1464 		error = ifmedia_ioctl(ifp, ifr, &sc->aue_mii.mii_media, command);
1465 		break;
1466 
1467 	default:
1468 		error = ether_ioctl(ifp, &sc->arpcom, command, data);
1469 	}
1470 
1471 	if (error == ENETRESET) {
1472 		if (ifp->if_flags & IFF_RUNNING)
1473 			aue_iff(sc);
1474 		error = 0;
1475 	}
1476 
1477 	splx(s);
1478 	return (error);
1479 }
1480 
1481 void
1482 aue_watchdog(struct ifnet *ifp)
1483 {
1484 	struct aue_softc	*sc = ifp->if_softc;
1485 	struct aue_chain	*c;
1486 	usbd_status		stat;
1487 	int			s;
1488 
1489 	DPRINTFN(5,("%s: %s: enter\n", sc->aue_dev.dv_xname, __func__));
1490 
1491 	ifp->if_oerrors++;
1492 	printf("%s: watchdog timeout\n", sc->aue_dev.dv_xname);
1493 
1494 	s = splusb();
1495 	c = &sc->aue_cdata.aue_tx_chain[0];
1496 	usbd_get_xfer_status(c->aue_xfer, NULL, NULL, NULL, &stat);
1497 	aue_txeof(c->aue_xfer, c, stat);
1498 
1499 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1500 		aue_start(ifp);
1501 	splx(s);
1502 }
1503 
1504 /*
1505  * Stop the adapter and free any mbufs allocated to the
1506  * RX and TX lists.
1507  */
1508 void
1509 aue_stop(struct aue_softc *sc)
1510 {
1511 	usbd_status		err;
1512 	struct ifnet		*ifp;
1513 	int			i;
1514 
1515 	DPRINTFN(5,("%s: %s: enter\n", sc->aue_dev.dv_xname, __func__));
1516 
1517 	ifp = GET_IFP(sc);
1518 	ifp->if_timer = 0;
1519 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1520 
1521 	aue_csr_write_1(sc, AUE_CTL0, 0);
1522 	aue_csr_write_1(sc, AUE_CTL1, 0);
1523 	aue_reset(sc);
1524 	timeout_del(&sc->aue_stat_ch);
1525 
1526 	/* Stop transfers. */
1527 	if (sc->aue_ep[AUE_ENDPT_RX] != NULL) {
1528 		usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_RX]);
1529 		err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_RX]);
1530 		if (err) {
1531 			printf("%s: close rx pipe failed: %s\n",
1532 			    sc->aue_dev.dv_xname, usbd_errstr(err));
1533 		}
1534 		sc->aue_ep[AUE_ENDPT_RX] = NULL;
1535 	}
1536 
1537 	if (sc->aue_ep[AUE_ENDPT_TX] != NULL) {
1538 		usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_TX]);
1539 		err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_TX]);
1540 		if (err) {
1541 			printf("%s: close tx pipe failed: %s\n",
1542 			    sc->aue_dev.dv_xname, usbd_errstr(err));
1543 		}
1544 		sc->aue_ep[AUE_ENDPT_TX] = NULL;
1545 	}
1546 
1547 	if (sc->aue_ep[AUE_ENDPT_INTR] != NULL) {
1548 		usbd_abort_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
1549 		err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
1550 		if (err) {
1551 			printf("%s: close intr pipe failed: %s\n",
1552 			    sc->aue_dev.dv_xname, usbd_errstr(err));
1553 		}
1554 		sc->aue_ep[AUE_ENDPT_INTR] = NULL;
1555 	}
1556 
1557 	/* Free RX resources. */
1558 	for (i = 0; i < AUE_RX_LIST_CNT; i++) {
1559 		if (sc->aue_cdata.aue_rx_chain[i].aue_mbuf != NULL) {
1560 			m_freem(sc->aue_cdata.aue_rx_chain[i].aue_mbuf);
1561 			sc->aue_cdata.aue_rx_chain[i].aue_mbuf = NULL;
1562 		}
1563 		if (sc->aue_cdata.aue_rx_chain[i].aue_xfer != NULL) {
1564 			usbd_free_xfer(sc->aue_cdata.aue_rx_chain[i].aue_xfer);
1565 			sc->aue_cdata.aue_rx_chain[i].aue_xfer = NULL;
1566 		}
1567 	}
1568 
1569 	/* Free TX resources. */
1570 	for (i = 0; i < AUE_TX_LIST_CNT; i++) {
1571 		if (sc->aue_cdata.aue_tx_chain[i].aue_mbuf != NULL) {
1572 			m_freem(sc->aue_cdata.aue_tx_chain[i].aue_mbuf);
1573 			sc->aue_cdata.aue_tx_chain[i].aue_mbuf = NULL;
1574 		}
1575 		if (sc->aue_cdata.aue_tx_chain[i].aue_xfer != NULL) {
1576 			usbd_free_xfer(sc->aue_cdata.aue_tx_chain[i].aue_xfer);
1577 			sc->aue_cdata.aue_tx_chain[i].aue_xfer = NULL;
1578 		}
1579 	}
1580 
1581 	sc->aue_link = 0;
1582 }
1583