xref: /openbsd/sys/dev/usb/if_aue.c (revision d415bd75)
1 /*	$OpenBSD: if_aue.c,v 1.112 2021/08/09 07:21:48 jmatthew 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 write 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 	m_freem(c->aue_mbuf);
1115 	c->aue_mbuf = NULL;
1116 
1117 	if (ifq_empty(&ifp->if_snd) == 0)
1118 		aue_start(ifp);
1119 
1120 	splx(s);
1121 }
1122 
1123 void
1124 aue_tick(void *xsc)
1125 {
1126 	struct aue_softc	*sc = xsc;
1127 
1128 	DPRINTFN(15,("%s: %s: enter\n", sc->aue_dev.dv_xname,__func__));
1129 
1130 	if (sc == NULL)
1131 		return;
1132 
1133 	if (usbd_is_dying(sc->aue_udev))
1134 		return;
1135 
1136 	/* Perform periodic stuff in process context. */
1137 	usb_add_task(sc->aue_udev, &sc->aue_tick_task);
1138 }
1139 
1140 void
1141 aue_tick_task(void *xsc)
1142 {
1143 	struct aue_softc	*sc = xsc;
1144 	struct ifnet		*ifp;
1145 	struct mii_data		*mii;
1146 	int			s;
1147 
1148 	DPRINTFN(15,("%s: %s: enter\n", sc->aue_dev.dv_xname,__func__));
1149 
1150 	if (usbd_is_dying(sc->aue_udev))
1151 		return;
1152 
1153 	ifp = GET_IFP(sc);
1154 	mii = GET_MII(sc);
1155 	if (mii == NULL)
1156 		return;
1157 
1158 	s = splnet();
1159 
1160 	mii_tick(mii);
1161 	if (!sc->aue_link && mii->mii_media_status & IFM_ACTIVE &&
1162 	    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1163 		DPRINTFN(2,("%s: %s: got link\n",
1164 			    sc->aue_dev.dv_xname,__func__));
1165 		sc->aue_link++;
1166 		if (ifq_empty(&ifp->if_snd) == 0)
1167 			aue_start(ifp);
1168 	}
1169 
1170 	timeout_add_sec(&sc->aue_stat_ch, 1);
1171 
1172 	splx(s);
1173 }
1174 
1175 int
1176 aue_send(struct aue_softc *sc, struct mbuf *m, int idx)
1177 {
1178 	int			total_len;
1179 	struct aue_chain	*c;
1180 	usbd_status		err;
1181 
1182 	DPRINTFN(10,("%s: %s: enter\n", sc->aue_dev.dv_xname,__func__));
1183 
1184 	c = &sc->aue_cdata.aue_tx_chain[idx];
1185 
1186 	/*
1187 	 * Copy the mbuf data into a contiguous buffer, leaving two
1188 	 * bytes at the beginning to hold the frame length.
1189 	 */
1190 	m_copydata(m, 0, m->m_pkthdr.len, c->aue_buf + 2);
1191 	c->aue_mbuf = m;
1192 
1193 	/*
1194 	 * The ADMtek documentation says that the packet length is
1195 	 * supposed to be specified in the first two bytes of the
1196 	 * transfer, however it actually seems to ignore this info
1197 	 * and base the frame size on the bulk transfer length.
1198 	 */
1199 	c->aue_buf[0] = (u_int8_t)m->m_pkthdr.len;
1200 	c->aue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8);
1201 	total_len = m->m_pkthdr.len + 2;
1202 
1203 	usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_TX],
1204 	    c, c->aue_buf, total_len, USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
1205 	    AUE_TX_TIMEOUT, aue_txeof);
1206 
1207 	/* Transmit */
1208 	err = usbd_transfer(c->aue_xfer);
1209 	if (err != USBD_IN_PROGRESS) {
1210 		printf("%s: aue_send error=%s\n", sc->aue_dev.dv_xname,
1211 		       usbd_errstr(err));
1212 		/* Stop the interface from process context. */
1213 		usb_add_task(sc->aue_udev, &sc->aue_stop_task);
1214 		return (EIO);
1215 	}
1216 	DPRINTFN(5,("%s: %s: send %d bytes\n", sc->aue_dev.dv_xname,
1217 		    __func__, total_len));
1218 
1219 	sc->aue_cdata.aue_tx_cnt++;
1220 
1221 	return (0);
1222 }
1223 
1224 void
1225 aue_start(struct ifnet *ifp)
1226 {
1227 	struct aue_softc	*sc = ifp->if_softc;
1228 	struct mbuf		*m_head = NULL;
1229 
1230 	DPRINTFN(5,("%s: %s: enter, link=%d\n", sc->aue_dev.dv_xname,
1231 		    __func__, sc->aue_link));
1232 
1233 	if (usbd_is_dying(sc->aue_udev))
1234 		return;
1235 
1236 	if (!sc->aue_link)
1237 		return;
1238 
1239 	if (ifq_is_oactive(&ifp->if_snd))
1240 		return;
1241 
1242 	m_head = ifq_deq_begin(&ifp->if_snd);
1243 	if (m_head == NULL)
1244 		return;
1245 
1246 	if (aue_send(sc, m_head, 0)) {
1247 		ifq_deq_rollback(&ifp->if_snd, m_head);
1248 		ifq_set_oactive(&ifp->if_snd);
1249 		return;
1250 	}
1251 
1252 	ifq_deq_commit(&ifp->if_snd, m_head);
1253 
1254 #if NBPFILTER > 0
1255 	/*
1256 	 * If there's a BPF listener, bounce a copy of this frame
1257 	 * to him.
1258 	 */
1259 	if (ifp->if_bpf)
1260 		bpf_mtap(ifp->if_bpf, m_head, BPF_DIRECTION_OUT);
1261 #endif
1262 
1263 	ifq_set_oactive(&ifp->if_snd);
1264 
1265 	/*
1266 	 * Set a timeout in case the chip goes out to lunch.
1267 	 */
1268 	ifp->if_timer = 5;
1269 }
1270 
1271 void
1272 aue_init(void *xsc)
1273 {
1274 	struct aue_softc	*sc = xsc;
1275 	struct ifnet		*ifp = GET_IFP(sc);
1276 	struct mii_data		*mii = GET_MII(sc);
1277 	int			i, s;
1278 	u_char			*eaddr;
1279 
1280 	DPRINTFN(5,("%s: %s: enter\n", sc->aue_dev.dv_xname, __func__));
1281 
1282 	if (usbd_is_dying(sc->aue_udev))
1283 		return;
1284 
1285 	s = splnet();
1286 
1287 	/*
1288 	 * Cancel pending I/O and free all RX/TX buffers.
1289 	 */
1290 	aue_reset(sc);
1291 
1292 	eaddr = sc->arpcom.ac_enaddr;
1293 	for (i = 0; i < ETHER_ADDR_LEN; i++)
1294 		aue_csr_write_1(sc, AUE_PAR0 + i, eaddr[i]);
1295 
1296 	/* Init TX ring. */
1297 	if (aue_tx_list_init(sc) == ENOBUFS) {
1298 		printf("%s: tx list init failed\n", sc->aue_dev.dv_xname);
1299 		splx(s);
1300 		return;
1301 	}
1302 
1303 	/* Init RX ring. */
1304 	if (aue_rx_list_init(sc) == ENOBUFS) {
1305 		printf("%s: rx list init failed\n", sc->aue_dev.dv_xname);
1306 		splx(s);
1307 		return;
1308 	}
1309 
1310 	/* Program promiscuous mode and multicast filters. */
1311 	aue_iff(sc);
1312 
1313 	/* Enable RX and TX */
1314 	AUE_SETBIT(sc, AUE_CTL0,
1315 	    AUE_CTL0_RXSTAT_APPEND | AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB);
1316 	AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_EP3_CLR);
1317 
1318 	mii_mediachg(mii);
1319 
1320 	if (sc->aue_ep[AUE_ENDPT_RX] == NULL) {
1321 		if (aue_openpipes(sc)) {
1322 			splx(s);
1323 			return;
1324 		}
1325 	}
1326 
1327 	ifp->if_flags |= IFF_RUNNING;
1328 	ifq_clr_oactive(&ifp->if_snd);
1329 
1330 	splx(s);
1331 
1332 	timeout_add_sec(&sc->aue_stat_ch, 1);
1333 }
1334 
1335 int
1336 aue_openpipes(struct aue_softc *sc)
1337 {
1338 	struct aue_chain	*c;
1339 	usbd_status		err;
1340 	int i;
1341 
1342 	/* Open RX and TX pipes. */
1343 	err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_RX],
1344 	    USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_RX]);
1345 	if (err) {
1346 		printf("%s: open rx pipe failed: %s\n",
1347 		    sc->aue_dev.dv_xname, usbd_errstr(err));
1348 		return (EIO);
1349 	}
1350 	err = usbd_open_pipe(sc->aue_iface, sc->aue_ed[AUE_ENDPT_TX],
1351 	    USBD_EXCLUSIVE_USE, &sc->aue_ep[AUE_ENDPT_TX]);
1352 	if (err) {
1353 		printf("%s: open tx pipe failed: %s\n",
1354 		    sc->aue_dev.dv_xname, usbd_errstr(err));
1355 		return (EIO);
1356 	}
1357 	err = usbd_open_pipe_intr(sc->aue_iface, sc->aue_ed[AUE_ENDPT_INTR],
1358 	    0, &sc->aue_ep[AUE_ENDPT_INTR], sc,
1359 	    &sc->aue_cdata.aue_ibuf, AUE_INTR_PKTLEN, aue_intr,
1360 	    AUE_INTR_INTERVAL);
1361 	if (err) {
1362 		printf("%s: open intr pipe failed: %s\n",
1363 		    sc->aue_dev.dv_xname, usbd_errstr(err));
1364 		return (EIO);
1365 	}
1366 
1367 	/* Start up the receive pipe. */
1368 	for (i = 0; i < AUE_RX_LIST_CNT; i++) {
1369 		c = &sc->aue_cdata.aue_rx_chain[i];
1370 		usbd_setup_xfer(c->aue_xfer, sc->aue_ep[AUE_ENDPT_RX],
1371 		    c, c->aue_buf, AUE_BUFSZ,
1372 		    USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT,
1373 		    aue_rxeof);
1374 		(void)usbd_transfer(c->aue_xfer); /* XXX */
1375 		DPRINTFN(5,("%s: %s: start read\n", sc->aue_dev.dv_xname,
1376 			    __func__));
1377 
1378 	}
1379 	return (0);
1380 }
1381 
1382 /*
1383  * Set media options.
1384  */
1385 int
1386 aue_ifmedia_upd(struct ifnet *ifp)
1387 {
1388 	struct aue_softc	*sc = ifp->if_softc;
1389 	struct mii_data		*mii = GET_MII(sc);
1390 
1391 	DPRINTFN(5,("%s: %s: enter\n", sc->aue_dev.dv_xname, __func__));
1392 
1393 	if (usbd_is_dying(sc->aue_udev))
1394 		return (0);
1395 
1396 	sc->aue_link = 0;
1397 	if (mii->mii_instance) {
1398 		struct mii_softc	*miisc;
1399 		LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
1400 			mii_phy_reset(miisc);
1401 	}
1402 	mii_mediachg(mii);
1403 
1404 	return (0);
1405 }
1406 
1407 /*
1408  * Report current media status.
1409  */
1410 void
1411 aue_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1412 {
1413 	struct aue_softc	*sc = ifp->if_softc;
1414 	struct mii_data		*mii = GET_MII(sc);
1415 
1416 	DPRINTFN(5,("%s: %s: enter\n", sc->aue_dev.dv_xname, __func__));
1417 
1418 	mii_pollstat(mii);
1419 	ifmr->ifm_active = mii->mii_media_active;
1420 	ifmr->ifm_status = mii->mii_media_status;
1421 }
1422 
1423 int
1424 aue_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
1425 {
1426 	struct aue_softc	*sc = ifp->if_softc;
1427 	struct ifreq		*ifr = (struct ifreq *)data;
1428 	int			s, error = 0;
1429 
1430 	if (usbd_is_dying(sc->aue_udev))
1431 		return ENXIO;
1432 
1433 	s = splnet();
1434 
1435 	switch(command) {
1436 	case SIOCSIFADDR:
1437 		ifp->if_flags |= IFF_UP;
1438 		if (!(ifp->if_flags & IFF_RUNNING))
1439 			aue_init(sc);
1440 		break;
1441 
1442 	case SIOCSIFFLAGS:
1443 		if (ifp->if_flags & IFF_UP) {
1444 			if (ifp->if_flags & IFF_RUNNING)
1445 				error = ENETRESET;
1446 			else
1447 				aue_init(sc);
1448 		} else {
1449 			if (ifp->if_flags & IFF_RUNNING)
1450 				aue_stop(sc);
1451 		}
1452 		break;
1453 
1454 	case SIOCGIFMEDIA:
1455 	case SIOCSIFMEDIA:
1456 		error = ifmedia_ioctl(ifp, ifr, &sc->aue_mii.mii_media, command);
1457 		break;
1458 
1459 	default:
1460 		error = ether_ioctl(ifp, &sc->arpcom, command, data);
1461 	}
1462 
1463 	if (error == ENETRESET) {
1464 		if (ifp->if_flags & IFF_RUNNING)
1465 			aue_iff(sc);
1466 		error = 0;
1467 	}
1468 
1469 	splx(s);
1470 	return (error);
1471 }
1472 
1473 void
1474 aue_watchdog(struct ifnet *ifp)
1475 {
1476 	struct aue_softc	*sc = ifp->if_softc;
1477 	struct aue_chain	*c;
1478 	usbd_status		stat;
1479 	int			s;
1480 
1481 	DPRINTFN(5,("%s: %s: enter\n", sc->aue_dev.dv_xname, __func__));
1482 
1483 	ifp->if_oerrors++;
1484 	printf("%s: watchdog timeout\n", sc->aue_dev.dv_xname);
1485 
1486 	s = splusb();
1487 	c = &sc->aue_cdata.aue_tx_chain[0];
1488 	usbd_get_xfer_status(c->aue_xfer, NULL, NULL, NULL, &stat);
1489 	aue_txeof(c->aue_xfer, c, stat);
1490 
1491 	if (ifq_empty(&ifp->if_snd) == 0)
1492 		aue_start(ifp);
1493 	splx(s);
1494 }
1495 
1496 /*
1497  * Stop the adapter and free any mbufs allocated to the
1498  * RX and TX lists.
1499  */
1500 void
1501 aue_stop(struct aue_softc *sc)
1502 {
1503 	usbd_status		err;
1504 	struct ifnet		*ifp;
1505 	int			i;
1506 
1507 	DPRINTFN(5,("%s: %s: enter\n", sc->aue_dev.dv_xname, __func__));
1508 
1509 	ifp = GET_IFP(sc);
1510 	ifp->if_timer = 0;
1511 	ifp->if_flags &= ~IFF_RUNNING;
1512 	ifq_clr_oactive(&ifp->if_snd);
1513 
1514 	aue_csr_write_1(sc, AUE_CTL0, 0);
1515 	aue_csr_write_1(sc, AUE_CTL1, 0);
1516 	aue_reset(sc);
1517 	timeout_del(&sc->aue_stat_ch);
1518 
1519 	/* Stop transfers. */
1520 	if (sc->aue_ep[AUE_ENDPT_RX] != NULL) {
1521 		err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_RX]);
1522 		if (err) {
1523 			printf("%s: close rx pipe failed: %s\n",
1524 			    sc->aue_dev.dv_xname, usbd_errstr(err));
1525 		}
1526 		sc->aue_ep[AUE_ENDPT_RX] = NULL;
1527 	}
1528 
1529 	if (sc->aue_ep[AUE_ENDPT_TX] != NULL) {
1530 		err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_TX]);
1531 		if (err) {
1532 			printf("%s: close tx pipe failed: %s\n",
1533 			    sc->aue_dev.dv_xname, usbd_errstr(err));
1534 		}
1535 		sc->aue_ep[AUE_ENDPT_TX] = NULL;
1536 	}
1537 
1538 	if (sc->aue_ep[AUE_ENDPT_INTR] != NULL) {
1539 		err = usbd_close_pipe(sc->aue_ep[AUE_ENDPT_INTR]);
1540 		if (err) {
1541 			printf("%s: close intr pipe failed: %s\n",
1542 			    sc->aue_dev.dv_xname, usbd_errstr(err));
1543 		}
1544 		sc->aue_ep[AUE_ENDPT_INTR] = NULL;
1545 	}
1546 
1547 	/* Free RX resources. */
1548 	for (i = 0; i < AUE_RX_LIST_CNT; i++) {
1549 		if (sc->aue_cdata.aue_rx_chain[i].aue_mbuf != NULL) {
1550 			m_freem(sc->aue_cdata.aue_rx_chain[i].aue_mbuf);
1551 			sc->aue_cdata.aue_rx_chain[i].aue_mbuf = NULL;
1552 		}
1553 		if (sc->aue_cdata.aue_rx_chain[i].aue_xfer != NULL) {
1554 			usbd_free_xfer(sc->aue_cdata.aue_rx_chain[i].aue_xfer);
1555 			sc->aue_cdata.aue_rx_chain[i].aue_xfer = NULL;
1556 		}
1557 	}
1558 
1559 	/* Free TX resources. */
1560 	for (i = 0; i < AUE_TX_LIST_CNT; i++) {
1561 		if (sc->aue_cdata.aue_tx_chain[i].aue_mbuf != NULL) {
1562 			m_freem(sc->aue_cdata.aue_tx_chain[i].aue_mbuf);
1563 			sc->aue_cdata.aue_tx_chain[i].aue_mbuf = NULL;
1564 		}
1565 		if (sc->aue_cdata.aue_tx_chain[i].aue_xfer != NULL) {
1566 			usbd_free_xfer(sc->aue_cdata.aue_tx_chain[i].aue_xfer);
1567 			sc->aue_cdata.aue_tx_chain[i].aue_xfer = NULL;
1568 		}
1569 	}
1570 
1571 	sc->aue_link = 0;
1572 }
1573