xref: /netbsd-src/sys/dev/usb/if_aue.c (revision 53d1339bf7f9c7367b35a9e1ebe693f9b047a47b)
1 /*	$NetBSD: if_aue.c,v 1.171 2020/03/18 11:33:32 kre Exp $	*/
2 
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  * more error checks
74  * investigate short rx problem
75  * proper cleanup on errors
76  */
77 
78 #include <sys/cdefs.h>
79 __KERNEL_RCSID(0, "$NetBSD: if_aue.c,v 1.171 2020/03/18 11:33:32 kre Exp $");
80 
81 #ifdef _KERNEL_OPT
82 #include "opt_usb.h"
83 #include "opt_inet.h"
84 #endif
85 
86 #include <sys/param.h>
87 
88 #include <dev/usb/usbnet.h>
89 #include <dev/usb/usbhist.h>
90 #include <dev/usb/if_auereg.h>
91 
92 #ifdef INET
93 #include <netinet/in.h>
94 #include <netinet/if_inarp.h>
95 #endif
96 
97 #ifdef USB_DEBUG
98 #ifndef AUE_DEBUG
99 #define auedebug 0
100 #else
101 static int auedebug = 10;
102 
103 SYSCTL_SETUP(sysctl_hw_aue_setup, "sysctl hw.aue setup")
104 {
105 	int err;
106 	const struct sysctlnode *rnode;
107 	const struct sysctlnode *cnode;
108 
109 	err = sysctl_createv(clog, 0, NULL, &rnode,
110 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "aue",
111 	    SYSCTL_DESCR("aue global controls"),
112 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
113 
114 	if (err)
115 		goto fail;
116 
117 	/* control debugging printfs */
118 	err = sysctl_createv(clog, 0, &rnode, &cnode,
119 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
120 	    "debug", SYSCTL_DESCR("Enable debugging output"),
121 	    NULL, 0, &auedebug, sizeof(auedebug), CTL_CREATE, CTL_EOL);
122 	if (err)
123 		goto fail;
124 
125 	return;
126 fail:
127 	aprint_error("%s: sysctl_createv failed (err = %d)\n", __func__, err);
128 }
129 
130 #endif /* AUE_DEBUG */
131 #endif /* USB_DEBUG */
132 
133 #define DPRINTF(FMT,A,B,C,D)	USBHIST_LOGN(auedebug,1,FMT,A,B,C,D)
134 #define DPRINTFN(N,FMT,A,B,C,D)	USBHIST_LOGN(auedebug,N,FMT,A,B,C,D)
135 #define AUEHIST_FUNC()		USBHIST_FUNC()
136 #define AUEHIST_CALLED(name)	USBHIST_CALLED(auedebug)
137 #define AUEHIST_CALLARGS(FMT,A,B,C,D) \
138 				USBHIST_CALLARGS(auedebug,FMT,A,B,C,D)
139 #define AUEHIST_CALLARGSN(N,FMT,A,B,C,D) \
140 				USBHIST_CALLARGSN(auedebug,N,FMT,A,B,C,D)
141 
142 #define AUE_TX_LIST_CNT		1
143 #define AUE_RX_LIST_CNT		1
144 
145 struct aue_softc {
146 	struct usbnet		aue_un;
147 	struct usbnet_intr	aue_intr;
148 	struct aue_intrpkt	aue_ibuf;
149 };
150 
151 #define AUE_TIMEOUT		1000
152 #define AUE_BUFSZ		1536
153 #define AUE_MIN_FRAMELEN	60
154 #define AUE_TX_TIMEOUT		10000 /* ms */
155 #define AUE_INTR_INTERVAL	100 /* ms */
156 
157 /*
158  * Various supported device vendors/products.
159  */
160 struct aue_type {
161 	struct usb_devno	aue_dev;
162 	uint16_t		aue_flags;
163 #define LSYS	0x0001		/* use Linksys reset */
164 #define PNA	0x0002		/* has Home PNA */
165 #define PII	0x0004		/* Pegasus II chip */
166 };
167 
168 static const struct aue_type aue_devs[] = {
169  {{ USB_VENDOR_3COM,		USB_PRODUCT_3COM_3C460B},	  PII },
170  {{ USB_VENDOR_ABOCOM,		USB_PRODUCT_ABOCOM_XX1},	  PNA | PII },
171  {{ USB_VENDOR_ABOCOM,		USB_PRODUCT_ABOCOM_XX2},	  PII },
172  {{ USB_VENDOR_ABOCOM,		USB_PRODUCT_ABOCOM_UFE1000},	  LSYS },
173  {{ USB_VENDOR_ABOCOM,		USB_PRODUCT_ABOCOM_XX4},	  PNA },
174  {{ USB_VENDOR_ABOCOM,		USB_PRODUCT_ABOCOM_XX5},	  PNA },
175  {{ USB_VENDOR_ABOCOM,		USB_PRODUCT_ABOCOM_XX6},	  PII },
176  {{ USB_VENDOR_ABOCOM,		USB_PRODUCT_ABOCOM_XX7},	  PII },
177  {{ USB_VENDOR_ABOCOM,		USB_PRODUCT_ABOCOM_XX8},	  PII },
178  {{ USB_VENDOR_ABOCOM,		USB_PRODUCT_ABOCOM_XX9},	  PNA },
179  {{ USB_VENDOR_ABOCOM,		USB_PRODUCT_ABOCOM_XX10},	  0 },
180  {{ USB_VENDOR_ABOCOM,		USB_PRODUCT_ABOCOM_DSB650TX_PNA}, 0 },
181  {{ USB_VENDOR_ACCTON,		USB_PRODUCT_ACCTON_USB320_EC},	  0 },
182  {{ USB_VENDOR_ACCTON,		USB_PRODUCT_ACCTON_SS1001},	  PII },
183  {{ USB_VENDOR_ADMTEK,		USB_PRODUCT_ADMTEK_PEGASUS},	  PNA },
184  {{ USB_VENDOR_ADMTEK,		USB_PRODUCT_ADMTEK_PEGASUSII},	  PII },
185  {{ USB_VENDOR_ADMTEK,		USB_PRODUCT_ADMTEK_PEGASUSII_2},  PII },
186  {{ USB_VENDOR_ADMTEK,		USB_PRODUCT_ADMTEK_PEGASUSII_3},  PII },
187  {{ USB_VENDOR_AEI,		USB_PRODUCT_AEI_USBTOLAN},	  PII },
188  {{ USB_VENDOR_BELKIN,		USB_PRODUCT_BELKIN_USB2LAN},	  PII },
189  {{ USB_VENDOR_BILLIONTON,	USB_PRODUCT_BILLIONTON_USB100},	  0 },
190  {{ USB_VENDOR_BILLIONTON,	USB_PRODUCT_BILLIONTON_USBLP100}, PNA },
191  {{ USB_VENDOR_BILLIONTON,	USB_PRODUCT_BILLIONTON_USBEL100}, 0 },
192  {{ USB_VENDOR_BILLIONTON,	USB_PRODUCT_BILLIONTON_USBE100},  PII },
193  {{ USB_VENDOR_COMPAQ,		USB_PRODUCT_COMPAQ_HNE200},	  PII },
194  {{ USB_VENDOR_COREGA,		USB_PRODUCT_COREGA_FETHER_USB_TX}, 0 },
195  {{ USB_VENDOR_COREGA,		USB_PRODUCT_COREGA_FETHER_USB_TXS},PII },
196  {{ USB_VENDOR_DLINK,		USB_PRODUCT_DLINK_DSB650TX4},	  LSYS | PII },
197  {{ USB_VENDOR_DLINK,		USB_PRODUCT_DLINK_DSB650TX1},	  LSYS },
198  {{ USB_VENDOR_DLINK,		USB_PRODUCT_DLINK_DSB650TX},	  LSYS },
199  {{ USB_VENDOR_DLINK,		USB_PRODUCT_DLINK_DSB650TX_PNA},  PNA },
200  {{ USB_VENDOR_DLINK,		USB_PRODUCT_DLINK_DSB650TX3},	  LSYS | PII },
201  {{ USB_VENDOR_DLINK,		USB_PRODUCT_DLINK_DSB650TX2},	  LSYS | PII },
202  {{ USB_VENDOR_DLINK,		USB_PRODUCT_DLINK_DSB650},	  0 },
203  {{ USB_VENDOR_ELECOM,		USB_PRODUCT_ELECOM_LDUSBTX0},	  0 },
204  {{ USB_VENDOR_ELECOM,		USB_PRODUCT_ELECOM_LDUSBTX1},	  LSYS },
205  {{ USB_VENDOR_ELECOM,		USB_PRODUCT_ELECOM_LDUSBTX2},	  0 },
206  {{ USB_VENDOR_ELECOM,		USB_PRODUCT_ELECOM_LDUSBTX3},	  LSYS },
207  {{ USB_VENDOR_ELECOM,		USB_PRODUCT_ELECOM_LDUSBLTX},	  PII },
208  {{ USB_VENDOR_ELSA,		USB_PRODUCT_ELSA_USB2ETHERNET},	  0 },
209  {{ USB_VENDOR_HAWKING,		USB_PRODUCT_HAWKING_UF100},	  PII },
210  {{ USB_VENDOR_HP,		USB_PRODUCT_HP_HN210E},		  PII },
211  {{ USB_VENDOR_IODATA,		USB_PRODUCT_IODATA_USBETTX},	  0 },
212  {{ USB_VENDOR_IODATA,		USB_PRODUCT_IODATA_USBETTXS},	  PII },
213  {{ USB_VENDOR_IODATA,		USB_PRODUCT_IODATA_ETXUS2},	  PII },
214  {{ USB_VENDOR_KINGSTON,	USB_PRODUCT_KINGSTON_KNU101TX},	  0 },
215  {{ USB_VENDOR_LINKSYS,		USB_PRODUCT_LINKSYS_USB10TX1},	  LSYS | PII },
216  {{ USB_VENDOR_LINKSYS,		USB_PRODUCT_LINKSYS_USB10T},	  LSYS },
217  {{ USB_VENDOR_LINKSYS,		USB_PRODUCT_LINKSYS_USB100TX},	  LSYS },
218  {{ USB_VENDOR_LINKSYS,		USB_PRODUCT_LINKSYS_USB100H1},	  LSYS | PNA },
219  {{ USB_VENDOR_LINKSYS,		USB_PRODUCT_LINKSYS_USB10TA},	  LSYS },
220  {{ USB_VENDOR_LINKSYS,		USB_PRODUCT_LINKSYS_USB10TX2},	  LSYS | PII },
221  {{ USB_VENDOR_MELCO,		USB_PRODUCT_MELCO_LUATX1},	  0 },
222  {{ USB_VENDOR_MELCO,		USB_PRODUCT_MELCO_LUATX5},	  0 },
223  {{ USB_VENDOR_MELCO,		USB_PRODUCT_MELCO_LUA2TX5},	  PII },
224  {{ USB_VENDOR_MICROSOFT,	USB_PRODUCT_MICROSOFT_MN110},	  PII },
225  {{ USB_VENDOR_NETGEAR,		USB_PRODUCT_NETGEAR_FA101},	  PII },
226  {{ USB_VENDOR_SIEMENS,		USB_PRODUCT_SIEMENS_SPEEDSTREAM}, PII },
227  {{ USB_VENDOR_SMARTBRIDGES,	USB_PRODUCT_SMARTBRIDGES_SMARTNIC},PII },
228  {{ USB_VENDOR_SMC,		USB_PRODUCT_SMC_2202USB},	  0 },
229  {{ USB_VENDOR_SMC,		USB_PRODUCT_SMC_2206USB},	  PII },
230  {{ USB_VENDOR_SOHOWARE,	USB_PRODUCT_SOHOWARE_NUB100},	  0 },
231 };
232 #define aue_lookup(v, p) ((const struct aue_type *)usb_lookup(aue_devs, v, p))
233 
234 static int aue_match(device_t, cfdata_t, void *);
235 static void aue_attach(device_t, device_t, void *);
236 
237 CFATTACH_DECL_NEW(aue, sizeof(struct aue_softc), aue_match, aue_attach,
238     usbnet_detach, usbnet_activate);
239 
240 static void aue_reset_pegasus_II(struct aue_softc *);
241 
242 static void aue_uno_stop(struct ifnet *, int);
243 static int aue_uno_ioctl(struct ifnet *, u_long, void *);
244 static int aue_uno_mii_read_reg(struct usbnet *, int, int, uint16_t *);
245 static int aue_uno_mii_write_reg(struct usbnet *, int, int, uint16_t);
246 static void aue_uno_mii_statchg(struct ifnet *);
247 static unsigned aue_uno_tx_prepare(struct usbnet *, struct mbuf *,
248 				   struct usbnet_chain *);
249 static void aue_uno_rx_loop(struct usbnet *, struct usbnet_chain *, uint32_t);
250 static int aue_uno_init(struct ifnet *);
251 static void aue_uno_intr(struct usbnet *, usbd_status);
252 
253 static const struct usbnet_ops aue_ops = {
254 	.uno_stop = aue_uno_stop,
255 	.uno_ioctl = aue_uno_ioctl,
256 	.uno_read_reg = aue_uno_mii_read_reg,
257 	.uno_write_reg = aue_uno_mii_write_reg,
258 	.uno_statchg = aue_uno_mii_statchg,
259 	.uno_tx_prepare = aue_uno_tx_prepare,
260 	.uno_rx_loop = aue_uno_rx_loop,
261 	.uno_init = aue_uno_init,
262 	.uno_intr = aue_uno_intr,
263 };
264 
265 static uint32_t aue_crc(void *);
266 static void aue_reset(struct aue_softc *);
267 
268 static int aue_csr_read_1(struct aue_softc *, int);
269 static int aue_csr_write_1(struct aue_softc *, int, int);
270 static int aue_csr_read_2(struct aue_softc *, int);
271 static int aue_csr_write_2(struct aue_softc *, int, int);
272 
273 #define AUE_SETBIT(sc, reg, x)				\
274 	aue_csr_write_1(sc, reg, aue_csr_read_1(sc, reg) | (x))
275 
276 #define AUE_CLRBIT(sc, reg, x)				\
277 	aue_csr_write_1(sc, reg, aue_csr_read_1(sc, reg) & ~(x))
278 
279 static int
280 aue_csr_read_1(struct aue_softc *sc, int reg)
281 {
282 	struct usbnet * const	un = &sc->aue_un;
283 	usb_device_request_t	req;
284 	usbd_status		err;
285 	uByte			val = 0;
286 
287 	usbnet_isowned_core(un);
288 
289 	if (usbnet_isdying(un))
290 		return 0;
291 
292 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
293 	req.bRequest = AUE_UR_READREG;
294 	USETW(req.wValue, 0);
295 	USETW(req.wIndex, reg);
296 	USETW(req.wLength, 1);
297 
298 	err = usbd_do_request(un->un_udev, &req, &val);
299 
300 	if (err) {
301 		AUEHIST_FUNC();
302 		AUEHIST_CALLARGS("aue%jd: reg=%#jx err=%jd",
303 		    device_unit(un->un_dev), reg, err, 0);
304 		return 0;
305 	}
306 
307 	return val;
308 }
309 
310 static int
311 aue_csr_read_2(struct aue_softc *sc, int reg)
312 {
313 	struct usbnet * const	un = &sc->aue_un;
314 	usb_device_request_t	req;
315 	usbd_status		err;
316 	uWord			val;
317 
318 	usbnet_isowned_core(un);
319 
320 	if (usbnet_isdying(un))
321 		return 0;
322 
323 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
324 	req.bRequest = AUE_UR_READREG;
325 	USETW(req.wValue, 0);
326 	USETW(req.wIndex, reg);
327 	USETW(req.wLength, 2);
328 
329 	err = usbd_do_request(un->un_udev, &req, &val);
330 
331 	if (err) {
332 		AUEHIST_FUNC();
333 		AUEHIST_CALLARGS("aue%jd: reg=%#jx err=%jd",
334 		    device_unit(un->un_dev), reg, err, 0);
335 		return 0;
336 	}
337 
338 	return UGETW(val);
339 }
340 
341 static int
342 aue_csr_write_1(struct aue_softc *sc, int reg, int aval)
343 {
344 	struct usbnet * const	un = &sc->aue_un;
345 	usb_device_request_t	req;
346 	usbd_status		err;
347 	uByte			val;
348 
349 	usbnet_isowned_core(un);
350 
351 	if (usbnet_isdying(un))
352 		return 0;
353 
354 	val = aval;
355 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
356 	req.bRequest = AUE_UR_WRITEREG;
357 	USETW(req.wValue, val);
358 	USETW(req.wIndex, reg);
359 	USETW(req.wLength, 1);
360 
361 	err = usbd_do_request(un->un_udev, &req, &val);
362 
363 	if (err) {
364 		AUEHIST_FUNC();
365 		AUEHIST_CALLARGS("aue%jd: reg=%#jx err=%jd",
366 		    device_unit(un->un_dev), reg, err, 0);
367 		return -1;
368 	}
369 
370 	return 0;
371 }
372 
373 static int
374 aue_csr_write_2(struct aue_softc *sc, int reg, int aval)
375 {
376 	struct usbnet * const	un = &sc->aue_un;
377 	usb_device_request_t	req;
378 	usbd_status		err;
379 	uWord			val;
380 
381 	usbnet_isowned_core(un);
382 
383 	if (usbnet_isdying(un))
384 		return 0;
385 
386 	USETW(val, aval);
387 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
388 	req.bRequest = AUE_UR_WRITEREG;
389 	USETW(req.wValue, aval);
390 	USETW(req.wIndex, reg);
391 	USETW(req.wLength, 2);
392 
393 	err = usbd_do_request(un->un_udev, &req, &val);
394 
395 	if (err) {
396 		AUEHIST_FUNC();
397 		AUEHIST_CALLARGS("aue%jd: reg=%#jx err=%jd",
398 		    device_unit(un->un_dev), reg, err, 0);
399 		return -1;
400 	}
401 
402 	return 0;
403 }
404 
405 /*
406  * Read a word of data stored in the EEPROM at address 'addr.'
407  */
408 static int
409 aue_eeprom_getword(struct aue_softc *sc, int addr)
410 {
411 	struct usbnet * const	un = &sc->aue_un;
412 	int			i;
413 
414 	AUEHIST_FUNC(); AUEHIST_CALLED();
415 
416 	aue_csr_write_1(sc, AUE_EE_REG, addr);
417 	aue_csr_write_1(sc, AUE_EE_CTL, AUE_EECTL_READ);
418 
419 	for (i = 0; i < AUE_TIMEOUT; i++) {
420 		if (aue_csr_read_1(sc, AUE_EE_CTL) & AUE_EECTL_DONE)
421 			break;
422 	}
423 
424 	if (i == AUE_TIMEOUT) {
425 		printf("%s: EEPROM read timed out\n",
426 		    device_xname(un->un_dev));
427 	}
428 
429 	return aue_csr_read_2(sc, AUE_EE_DATA);
430 }
431 
432 /*
433  * Read the MAC from the EEPROM.  It's at offset 0.
434  */
435 static void
436 aue_read_mac(struct usbnet *un)
437 {
438 	struct aue_softc	*sc = usbnet_softc(un);
439 	int			i;
440 	int			off = 0;
441 	int			word;
442 
443 	usbnet_isowned_core(un);
444 
445 	AUEHIST_FUNC();
446 	AUEHIST_CALLARGS("aue%jd: enter",
447 	    device_unit(un->un_dev), 0, 0, 0);
448 
449 	for (i = 0; i < 3; i++) {
450 		word = aue_eeprom_getword(sc, off + i);
451 		un->un_eaddr[2 * i] =     (u_char)word;
452 		un->un_eaddr[2 * i + 1] = (u_char)(word >> 8);
453 	}
454 }
455 
456 static int
457 aue_uno_mii_read_reg(struct usbnet *un, int phy, int reg, uint16_t *val)
458 {
459 	struct aue_softc	*sc = usbnet_softc(un);
460 	int			i;
461 
462 	AUEHIST_FUNC();
463 
464 #if 0
465 	/*
466 	 * The Am79C901 HomePNA PHY actually contains
467 	 * two transceivers: a 1Mbps HomePNA PHY and a
468 	 * 10Mbps full/half duplex ethernet PHY with
469 	 * NWAY autoneg. However in the ADMtek adapter,
470 	 * only the 1Mbps PHY is actually connected to
471 	 * anything, so we ignore the 10Mbps one. It
472 	 * happens to be configured for MII address 3,
473 	 * so we filter that out.
474 	 */
475 	if (sc->aue_vendor == USB_VENDOR_ADMTEK &&
476 	    sc->aue_product == USB_PRODUCT_ADMTEK_PEGASUS) {
477 		if (phy == 3)
478 			return EINVAL;
479 	}
480 #endif
481 
482 	aue_csr_write_1(sc, AUE_PHY_ADDR, phy);
483 	aue_csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_READ);
484 
485 	for (i = 0; i < AUE_TIMEOUT; i++) {
486 		if (aue_csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE)
487 			break;
488 	}
489 
490 	if (i == AUE_TIMEOUT) {
491 		AUEHIST_CALLARGS("aue%jd: phy=%#jx reg=%#jx read timed out",
492 		    device_unit(un->un_dev), phy, reg, 0);
493 		return ETIMEDOUT;
494 	}
495 
496 	*val = aue_csr_read_2(sc, AUE_PHY_DATA);
497 
498 	AUEHIST_CALLARGSN(11, "aue%jd: phy=%#jx reg=%#jx => 0x%04jx",
499 	    device_unit(un->un_dev), phy, reg, *val);
500 
501 	return 0;
502 }
503 
504 static int
505 aue_uno_mii_write_reg(struct usbnet *un, int phy, int reg, uint16_t val)
506 {
507 	struct aue_softc	*sc = usbnet_softc(un);
508 	int			i;
509 
510 	AUEHIST_FUNC();
511 	AUEHIST_CALLARGSN(11, "aue%jd: phy=%jd reg=%jd data=0x%04jx",
512 	    device_unit(un->un_dev), phy, reg, val);
513 
514 #if 0
515 	if (sc->aue_vendor == USB_VENDOR_ADMTEK &&
516 	    sc->aue_product == USB_PRODUCT_ADMTEK_PEGASUS) {
517 		if (phy == 3)
518 			return EINVAL;
519 	}
520 #endif
521 
522 	aue_csr_write_2(sc, AUE_PHY_DATA, val);
523 	aue_csr_write_1(sc, AUE_PHY_ADDR, phy);
524 	aue_csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_WRITE);
525 
526 	for (i = 0; i < AUE_TIMEOUT; i++) {
527 		if (aue_csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE)
528 			break;
529 	}
530 
531 	if (i == AUE_TIMEOUT) {
532 		DPRINTF("aue%jd: phy=%#jx reg=%#jx val=%#jx write timed out",
533 		    device_unit(un->un_dev), phy, reg, val);
534 		return ETIMEDOUT;
535 	}
536 
537 	return 0;
538 }
539 
540 static void
541 aue_uno_mii_statchg(struct ifnet *ifp)
542 {
543 	struct usbnet *un = ifp->if_softc;
544 	struct aue_softc *sc = usbnet_softc(un);
545 	struct mii_data	*mii = usbnet_mii(un);
546 	const bool hadlink __diagused = usbnet_havelink(un);
547 
548 	AUEHIST_FUNC(); AUEHIST_CALLED();
549 	AUEHIST_CALLARGSN(5, "aue%jd: ifp=%#jx link=%jd",
550 	    device_unit(un->un_dev), (uintptr_t)ifp, hadlink, 0);
551 
552 	AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB);
553 
554 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX) {
555 		AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL);
556 	} else {
557 		AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL);
558 	}
559 
560 	if ((mii->mii_media_active & IFM_FDX) != 0)
561 		AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX);
562 	else
563 		AUE_CLRBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX);
564 
565 	AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB);
566 
567 	if (mii->mii_media_status & IFM_ACTIVE &&
568 	    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
569 		usbnet_set_link(un, true);
570 	}
571 
572 	/*
573 	 * Set the LED modes on the LinkSys adapter.
574 	 * This turns on the 'dual link LED' bin in the auxmode
575 	 * register of the Broadcom PHY.
576 	 */
577 	if (!usbnet_isdying(un) && (un->un_flags & LSYS)) {
578 		uint16_t auxmode;
579 		aue_uno_mii_read_reg(un, 0, 0x1b, &auxmode);
580 		aue_uno_mii_write_reg(un, 0, 0x1b, auxmode | 0x04);
581 	}
582 
583 	if (usbnet_havelink(un) != hadlink) {
584 		DPRINTFN(5, "aue%jd: exit link %jd",
585 		    device_unit(un->un_dev), usbnet_havelink(un), 0, 0);
586 	}
587 }
588 
589 #define AUE_POLY	0xEDB88320
590 #define AUE_BITS	6
591 
592 static uint32_t
593 aue_crc(void *addrv)
594 {
595 	uint32_t		idx, bit, data, crc;
596 	char *addr = addrv;
597 
598 	/* Compute CRC for the address value. */
599 	crc = 0xFFFFFFFF; /* initial value */
600 
601 	for (idx = 0; idx < 6; idx++) {
602 		for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1)
603 			crc = (crc >> 1) ^ (((crc ^ data) & 1) ? AUE_POLY : 0);
604 	}
605 
606 	return crc & ((1 << AUE_BITS) - 1);
607 }
608 
609 static void
610 aue_setiff_locked(struct usbnet *un)
611 {
612 	struct aue_softc * const sc = usbnet_softc(un);
613 	struct ifnet * const	ifp = usbnet_ifp(un);
614 	struct ethercom *	ec = usbnet_ec(un);
615 	struct ether_multi	*enm;
616 	struct ether_multistep	step;
617 	uint32_t		h = 0, i;
618 	uint8_t hashtbl[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
619 
620 	AUEHIST_FUNC();
621 	AUEHIST_CALLARGSN(5, "aue%jd: enter", device_unit(un->un_dev), 0, 0, 0);
622 
623 	usbnet_isowned_core(un);
624 
625 	if (ifp->if_flags & IFF_PROMISC) {
626 allmulti:
627 		ifp->if_flags |= IFF_ALLMULTI;
628 		AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
629 		return;
630 	}
631 
632 	AUE_CLRBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
633 
634 	/* now program new ones */
635 	ETHER_LOCK(ec);
636 	ETHER_FIRST_MULTI(step, ec, enm);
637 	while (enm != NULL) {
638 		if (memcmp(enm->enm_addrlo,
639 		    enm->enm_addrhi, ETHER_ADDR_LEN) != 0) {
640 			ETHER_UNLOCK(ec);
641 			goto allmulti;
642 		}
643 
644 		h = aue_crc(enm->enm_addrlo);
645 		hashtbl[h >> 3] |= 1 << (h & 0x7);
646 		ETHER_NEXT_MULTI(step, enm);
647 	}
648 	ETHER_UNLOCK(ec);
649 
650 	/* write the hashtable */
651 	for (i = 0; i < 8; i++)
652 		aue_csr_write_1(sc, AUE_MAR0 + i, hashtbl[i]);
653 
654 	ifp->if_flags &= ~IFF_ALLMULTI;
655 }
656 
657 static void
658 aue_reset_pegasus_II(struct aue_softc *sc)
659 {
660 	/* Magic constants taken from Linux driver. */
661 	aue_csr_write_1(sc, AUE_REG_1D, 0);
662 	aue_csr_write_1(sc, AUE_REG_7B, 2);
663 #if 0
664 	if ((un->un_flags & PNA) && mii_mode)
665 		aue_csr_write_1(sc, AUE_REG_81, 6);
666 	else
667 #endif
668 		aue_csr_write_1(sc, AUE_REG_81, 2);
669 }
670 
671 static void
672 aue_reset(struct aue_softc *sc)
673 {
674 	struct usbnet * const un = &sc->aue_un;
675 	int		i;
676 
677 	AUEHIST_FUNC();
678 	AUEHIST_CALLARGSN(2, "aue%jd: enter", device_unit(un->un_dev), 0, 0, 0);
679 
680 	AUE_SETBIT(sc, AUE_CTL1, AUE_CTL1_RESETMAC);
681 
682 	for (i = 0; i < AUE_TIMEOUT; i++) {
683 		if (!(aue_csr_read_1(sc, AUE_CTL1) & AUE_CTL1_RESETMAC))
684 			break;
685 	}
686 
687 	if (i == AUE_TIMEOUT)
688 		printf("%s: reset failed\n", device_xname(un->un_dev));
689 
690 #if 0
691 	/* XXX what is mii_mode supposed to be */
692 	if (sc->sc_mii_mode && (un->un_flags & PNA))
693 		aue_csr_write_1(sc, AUE_GPIO1, 0x34);
694 	else
695 		aue_csr_write_1(sc, AUE_GPIO1, 0x26);
696 #endif
697 
698 	/*
699 	 * The PHY(s) attached to the Pegasus chip may be held
700 	 * in reset until we flip on the GPIO outputs. Make sure
701 	 * to set the GPIO pins high so that the PHY(s) will
702 	 * be enabled.
703 	 *
704 	 * Note: We force all of the GPIO pins low first, *then*
705 	 * enable the ones we want.
706 	 */
707 	if (un->un_flags & LSYS) {
708 		/* Grrr. LinkSys has to be different from everyone else. */
709 		aue_csr_write_1(sc, AUE_GPIO0,
710 		    AUE_GPIO_SEL0 | AUE_GPIO_SEL1);
711 	} else {
712 		aue_csr_write_1(sc, AUE_GPIO0,
713 		    AUE_GPIO_OUT0 | AUE_GPIO_SEL0);
714 	}
715 	aue_csr_write_1(sc, AUE_GPIO0,
716 	    AUE_GPIO_OUT0 | AUE_GPIO_SEL0 | AUE_GPIO_SEL1);
717 
718 	if (un->un_flags & PII)
719 		aue_reset_pegasus_II(sc);
720 
721 	/* Wait a little while for the chip to get its brains in order. */
722 	delay(10000);	/* XXX */
723 	//usbd_delay_ms(un->un_udev, 10);	/* XXX */
724 
725 	DPRINTFN(2, "aue%jd: exit", device_unit(un->un_dev), 0, 0, 0);
726 }
727 
728 /*
729  * Probe for a Pegasus chip.
730  */
731 static int
732 aue_match(device_t parent, cfdata_t match, void *aux)
733 {
734 	struct usb_attach_arg *uaa = aux;
735 
736 	/*
737 	 * Some manufacturers use the same vendor and product id for
738 	 * different devices. We need to sanity check the DeviceClass
739 	 * in this case
740 	 * Currently known guilty products:
741 	 * 0x050d/0x0121 Belkin Bluetooth and USB2LAN
742 	 *
743 	 * If this turns out to be more common, we could use a quirk
744 	 * table.
745 	 */
746 	if (uaa->uaa_vendor == USB_VENDOR_BELKIN &&
747 		uaa->uaa_product == USB_PRODUCT_BELKIN_USB2LAN) {
748 		usb_device_descriptor_t *dd;
749 
750 		dd = usbd_get_device_descriptor(uaa->uaa_device);
751 		if (dd != NULL &&
752 			dd->bDeviceClass != UDCLASS_IN_INTERFACE)
753 			return UMATCH_NONE;
754 	}
755 
756 	return aue_lookup(uaa->uaa_vendor, uaa->uaa_product) != NULL ?
757 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
758 }
759 
760 /*
761  * Attach the interface. Allocate softc structures, do ifmedia
762  * setup and ethernet/BPF attach.
763  */
764 static void
765 aue_attach(device_t parent, device_t self, void *aux)
766 {
767 	USBNET_MII_DECL_DEFAULT(unm);
768 	struct aue_softc * const sc = device_private(self);
769 	struct usbnet * const un = &sc->aue_un;
770 	struct usb_attach_arg *uaa = aux;
771 	char			*devinfop;
772 	struct usbd_device	*dev = uaa->uaa_device;
773 	usbd_status		err;
774 	usb_interface_descriptor_t	*id;
775 	usb_endpoint_descriptor_t	*ed;
776 	int			i;
777 
778 	AUEHIST_FUNC();
779 	AUEHIST_CALLARGSN(2, "aue%jd: enter sc=%#jx",
780 	    device_unit(self), (uintptr_t)sc, 0, 0);
781 
782 	KASSERT((void *)sc == un);
783 
784 	aprint_naive("\n");
785 	aprint_normal("\n");
786 	devinfop = usbd_devinfo_alloc(uaa->uaa_device, 0);
787 	aprint_normal_dev(self, "%s\n", devinfop);
788 	usbd_devinfo_free(devinfop);
789 
790 	un->un_dev = self;
791 	un->un_udev = dev;
792 	un->un_sc = sc;
793 	un->un_ops = &aue_ops;
794 	un->un_intr = &sc->aue_intr;
795 	un->un_rx_xfer_flags = USBD_SHORT_XFER_OK;
796 	un->un_tx_xfer_flags = USBD_FORCE_SHORT_XFER;
797 	un->un_rx_list_cnt = AUE_RX_LIST_CNT;
798 	un->un_tx_list_cnt = AUE_RX_LIST_CNT;
799 	un->un_rx_bufsz = AUE_BUFSZ;
800 	un->un_tx_bufsz = AUE_BUFSZ;
801 
802 	sc->aue_intr.uni_buf = &sc->aue_ibuf;
803 	sc->aue_intr.uni_bufsz = sizeof(sc->aue_ibuf);
804 	sc->aue_intr.uni_interval = AUE_INTR_INTERVAL;
805 
806 	err = usbd_set_config_no(dev, AUE_CONFIG_NO, 1);
807 	if (err) {
808 		aprint_error_dev(self, "failed to set configuration"
809 		    ", err=%s\n", usbd_errstr(err));
810 		return;
811 	}
812 
813 	err = usbd_device2interface_handle(dev, AUE_IFACE_IDX, &un->un_iface);
814 	if (err) {
815 		aprint_error_dev(self, "getting interface handle failed\n");
816 		return;
817 	}
818 
819 	un->un_flags = aue_lookup(uaa->uaa_vendor, uaa->uaa_product)->aue_flags;
820 
821 	id = usbd_get_interface_descriptor(un->un_iface);
822 
823 	/* Find endpoints. */
824 	for (i = 0; i < id->bNumEndpoints; i++) {
825 		ed = usbd_interface2endpoint_descriptor(un->un_iface, i);
826 		if (ed == NULL) {
827 			aprint_error_dev(self,
828 			    "couldn't get endpoint descriptor %d\n", i);
829 			return;
830 		}
831 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
832 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
833 			un->un_ed[USBNET_ENDPT_RX] = ed->bEndpointAddress;
834 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
835 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
836 			un->un_ed[USBNET_ENDPT_TX] = ed->bEndpointAddress;
837 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
838 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
839 			un->un_ed[USBNET_ENDPT_INTR] = ed->bEndpointAddress;
840 		}
841 	}
842 
843 	if (un->un_ed[USBNET_ENDPT_RX] == 0 ||
844 	    un->un_ed[USBNET_ENDPT_TX] == 0 ||
845 	    un->un_ed[USBNET_ENDPT_INTR] == 0) {
846 		aprint_error_dev(self, "missing endpoint\n");
847 		return;
848 	}
849 
850 	/* First level attach. */
851 	usbnet_attach(un, "auedet");
852 
853 	usbnet_lock_core(un);
854 
855 	/* Reset the adapter and get station address from the EEPROM.  */
856 	aue_reset(sc);
857 	aue_read_mac(un);
858 
859 	usbnet_unlock_core(un);
860 
861 	usbnet_attach_ifp(un, IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST,
862 	    0, &unm);
863 }
864 
865 static void
866 aue_uno_intr(struct usbnet *un, usbd_status status)
867 {
868 	struct ifnet		*ifp = usbnet_ifp(un);
869 	struct aue_softc	*sc = usbnet_softc(un);
870 	struct aue_intrpkt	*p = &sc->aue_ibuf;
871 
872 	AUEHIST_FUNC();
873 	AUEHIST_CALLARGSN(20, "aue%jd: enter txstat0 %#jx\n",
874 	    device_unit(un->un_dev), p->aue_txstat0, 0, 0);
875 
876 	if (p->aue_txstat0)
877 		if_statinc(ifp, if_oerrors);
878 
879 	if (p->aue_txstat0 & (AUE_TXSTAT0_LATECOLL | AUE_TXSTAT0_EXCESSCOLL))
880 		if_statinc(ifp, if_collisions);
881 }
882 
883 static void
884 aue_uno_rx_loop(struct usbnet *un, struct usbnet_chain *c, uint32_t total_len)
885 {
886 	struct ifnet		*ifp = usbnet_ifp(un);
887 	uint8_t			*buf = c->unc_buf;
888 	struct aue_rxpkt	r;
889 	uint32_t		pktlen;
890 
891 	AUEHIST_FUNC();
892 	AUEHIST_CALLARGSN(10, "aue%jd: enter len %ju",
893 	    device_unit(un->un_dev), total_len, 0, 0);
894 
895 	if (total_len <= 4 + ETHER_CRC_LEN) {
896 		if_statinc(ifp, if_ierrors);
897 		return;
898 	}
899 
900 	memcpy(&r, buf + total_len - 4, sizeof(r));
901 
902 	/* Turn off all the non-error bits in the rx status word. */
903 	r.aue_rxstat &= AUE_RXSTAT_MASK;
904 	if (r.aue_rxstat) {
905 		if_statinc(ifp, if_ierrors);
906 		return;
907 	}
908 
909 	/* No errors; receive the packet. */
910 	pktlen = total_len - ETHER_CRC_LEN - 4;
911 
912 	usbnet_enqueue(un, buf, pktlen, 0, 0, 0);
913 }
914 
915 static unsigned
916 aue_uno_tx_prepare(struct usbnet *un, struct mbuf *m, struct usbnet_chain *c)
917 {
918 	uint8_t			*buf = c->unc_buf;
919 	int			total_len;
920 
921 	AUEHIST_FUNC();
922 	AUEHIST_CALLARGSN(10, "aue%jd: enter pktlen=%jd",
923 	    device_unit(un->un_dev), m->m_pkthdr.len, 0, 0);
924 
925 	if ((unsigned)m->m_pkthdr.len > un->un_tx_bufsz - 2)
926 		return 0;
927 
928 	/*
929 	 * Copy the mbuf data into a contiguous buffer, leaving two
930 	 * bytes at the beginning to hold the frame length.
931 	 */
932 	m_copydata(m, 0, m->m_pkthdr.len, buf + 2);
933 
934 	/*
935 	 * The ADMtek documentation says that the packet length is
936 	 * supposed to be specified in the first two bytes of the
937 	 * transfer, however it actually seems to ignore this info
938 	 * and base the frame size on the bulk transfer length.
939 	 */
940 	buf[0] = (uint8_t)m->m_pkthdr.len;
941 	buf[1] = (uint8_t)(m->m_pkthdr.len >> 8);
942 	total_len = m->m_pkthdr.len + 2;
943 
944 	DPRINTFN(5, "aue%jd: send %jd bytes",
945 	    device_unit(un->un_dev), total_len, 0, 0);
946 
947 	return total_len;
948 }
949 
950 static int
951 aue_init_locked(struct ifnet *ifp)
952 {
953 	struct usbnet * const	un = ifp->if_softc;
954 	struct aue_softc	*sc = usbnet_softc(un);
955 	int			i, rv;
956 	const u_char		*eaddr;
957 
958 	AUEHIST_FUNC();
959 	AUEHIST_CALLARGSN(5, "aue%jd: enter link=%jd",
960 	    device_unit(un->un_dev), usbnet_havelink(un), 0, 0);
961 
962 	if (usbnet_isdying(un))
963 		return EIO;
964 
965 	/* Cancel pending I/O */
966 	if (ifp->if_flags & IFF_RUNNING)
967 		return 0;
968 
969 	/* Reset the interface. */
970 	aue_reset(sc);
971 
972 	eaddr = CLLADDR(ifp->if_sadl);
973 	for (i = 0; i < ETHER_ADDR_LEN; i++)
974 		aue_csr_write_1(sc, AUE_PAR0 + i, eaddr[i]);
975 
976 	 /* If we want promiscuous mode, set the allframes bit. */
977 	if (ifp->if_flags & IFF_PROMISC)
978 		AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
979 	else
980 		AUE_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
981 
982 	rv = usbnet_init_rx_tx(un);
983 
984 	/* Load the multicast filter. */
985 	aue_setiff_locked(un);
986 
987 	/* Enable RX and TX */
988 	aue_csr_write_1(sc, AUE_CTL0, AUE_CTL0_RXSTAT_APPEND | AUE_CTL0_RX_ENB);
989 	AUE_SETBIT(sc, AUE_CTL0, AUE_CTL0_TX_ENB);
990 	AUE_SETBIT(sc, AUE_CTL2, AUE_CTL2_EP3_CLR);
991 
992 	//mii_mediachg(mii);
993 
994 	return rv;
995 }
996 
997 static int
998 aue_uno_init(struct ifnet *ifp)
999 {
1000 	struct usbnet * const	un = ifp->if_softc;
1001 	int rv;
1002 
1003 	usbnet_lock_core(un);
1004 	usbnet_busy(un);
1005 	rv = aue_init_locked(ifp);
1006 	usbnet_unbusy(un);
1007 	usbnet_unlock_core(un);
1008 
1009 	return rv;
1010 }
1011 
1012 static int
1013 aue_uno_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1014 {
1015 
1016 	AUEHIST_FUNC();
1017 	AUEHIST_CALLARGSN(5, "aue%jd: enter cmd %#jx data %#jx",
1018 	    device_unit(((struct usbnet *)(ifp->if_softc))->un_dev),
1019 	    cmd, (uintptr_t)data, 0);
1020 
1021 	switch (cmd) {
1022 	case SIOCADDMULTI:
1023 	case SIOCDELMULTI:
1024 		aue_uno_init(ifp);
1025 		break;
1026 	default:
1027 		break;
1028 	}
1029 
1030 	return 0;
1031 }
1032 
1033 static void
1034 aue_uno_stop(struct ifnet *ifp, int disable)
1035 {
1036 	struct usbnet * const	un = ifp->if_softc;
1037 	struct aue_softc * const sc = usbnet_softc(un);
1038 
1039 	AUEHIST_FUNC();
1040 	AUEHIST_CALLARGSN(5, "aue%jd: enter", device_unit(un->un_dev), 0, 0, 0);
1041 
1042 	aue_csr_write_1(sc, AUE_CTL0, 0);
1043 	aue_csr_write_1(sc, AUE_CTL1, 0);
1044 	aue_reset(sc);
1045 }
1046 
1047 #ifdef _MODULE
1048 #include "ioconf.c"
1049 #endif
1050 
1051 USBNET_MODULE(aue)
1052