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