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