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