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