xref: /netbsd-src/sys/dev/usb/if_cue.c (revision aaf4ece63a859a04e37cf3a7229b5fab0157cc06)
1 /*	$NetBSD: if_cue.c,v 1.44 2005/11/28 13:31:09 augustss Exp $	*/
2 /*
3  * Copyright (c) 1997, 1998, 1999, 2000
4  *	Bill Paul <wpaul@ee.columbia.edu>.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by Bill Paul.
17  * 4. Neither the name of the author nor the names of any co-contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31  * THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  * $FreeBSD: src/sys/dev/usb/if_cue.c,v 1.4 2000/01/16 22:45:06 wpaul Exp $
34  */
35 
36 /*
37  * CATC USB-EL1210A USB to ethernet driver. Used in the CATC Netmate
38  * adapters and others.
39  *
40  * Written by Bill Paul <wpaul@ee.columbia.edu>
41  * Electrical Engineering Department
42  * Columbia University, New York City
43  */
44 
45 /*
46  * The CATC USB-EL1210A provides USB ethernet support at 10Mbps. The
47  * RX filter uses a 512-bit multicast hash table, single perfect entry
48  * for the station address, and promiscuous mode. Unlike the ADMtek
49  * and KLSI chips, the CATC ASIC supports read and write combining
50  * mode where multiple packets can be transfered using a single bulk
51  * transaction, which helps performance a great deal.
52  */
53 
54 /*
55  * Ported to NetBSD and somewhat rewritten by Lennart Augustsson.
56  */
57 
58 #include <sys/cdefs.h>
59 __KERNEL_RCSID(0, "$NetBSD: if_cue.c,v 1.44 2005/11/28 13:31:09 augustss Exp $");
60 
61 #if defined(__NetBSD__)
62 #include "opt_inet.h"
63 #include "opt_ns.h"
64 #include "bpfilter.h"
65 #include "rnd.h"
66 #elif defined(__OpenBSD__)
67 #include "bpfilter.h"
68 #endif /* defined(__OpenBSD__) */
69 
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #if !defined(__OpenBSD__)
73 #include <sys/callout.h>
74 #endif
75 #include <sys/sockio.h>
76 #include <sys/mbuf.h>
77 #include <sys/malloc.h>
78 #include <sys/kernel.h>
79 #include <sys/socket.h>
80 
81 #include <sys/device.h>
82 #if NRND > 0
83 #include <sys/rnd.h>
84 #endif
85 
86 #include <net/if.h>
87 #if defined(__NetBSD__)
88 #include <net/if_arp.h>
89 #endif
90 #include <net/if_dl.h>
91 
92 #define BPF_MTAP(ifp, m) bpf_mtap((ifp)->if_bpf, (m))
93 
94 #if NBPFILTER > 0
95 #include <net/bpf.h>
96 #endif
97 
98 #if defined(__NetBSD__)
99 #include <net/if_ether.h>
100 #ifdef INET
101 #include <netinet/in.h>
102 #include <netinet/if_inarp.h>
103 #endif
104 #endif /* defined(__NetBSD__) */
105 
106 #if defined(__OpenBSD__)
107 #ifdef INET
108 #include <netinet/in.h>
109 #include <netinet/in_systm.h>
110 #include <netinet/in_var.h>
111 #include <netinet/ip.h>
112 #include <netinet/if_ether.h>
113 #endif
114 #endif /* defined(__OpenBSD__) */
115 
116 #ifdef NS
117 #include <netns/ns.h>
118 #include <netns/ns_if.h>
119 #endif
120 
121 #include <dev/usb/usb.h>
122 #include <dev/usb/usbdi.h>
123 #include <dev/usb/usbdi_util.h>
124 #include <dev/usb/usbdevs.h>
125 
126 #include <dev/usb/if_cuereg.h>
127 
128 #ifdef CUE_DEBUG
129 #define DPRINTF(x)	if (cuedebug) logprintf x
130 #define DPRINTFN(n,x)	if (cuedebug >= (n)) logprintf x
131 int	cuedebug = 0;
132 #else
133 #define DPRINTF(x)
134 #define DPRINTFN(n,x)
135 #endif
136 
137 /*
138  * Various supported device vendors/products.
139  */
140 Static struct usb_devno cue_devs[] = {
141 	{ USB_VENDOR_CATC, USB_PRODUCT_CATC_NETMATE },
142 	{ USB_VENDOR_CATC, USB_PRODUCT_CATC_NETMATE2 },
143 	{ USB_VENDOR_SMARTBRIDGES, USB_PRODUCT_SMARTBRIDGES_SMARTLINK },
144 	/* Belkin F5U111 adapter covered by NETMATE entry */
145 };
146 #define cue_lookup(v, p) (usb_lookup(cue_devs, v, p))
147 
148 USB_DECLARE_DRIVER(cue);
149 
150 Static int cue_open_pipes(struct cue_softc *);
151 Static int cue_tx_list_init(struct cue_softc *);
152 Static int cue_rx_list_init(struct cue_softc *);
153 Static int cue_newbuf(struct cue_softc *, struct cue_chain *, struct mbuf *);
154 Static int cue_send(struct cue_softc *, struct mbuf *, int);
155 Static void cue_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
156 Static void cue_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
157 Static void cue_tick(void *);
158 Static void cue_tick_task(void *);
159 Static void cue_start(struct ifnet *);
160 Static int cue_ioctl(struct ifnet *, u_long, caddr_t);
161 Static void cue_init(void *);
162 Static void cue_stop(struct cue_softc *);
163 Static void cue_watchdog(struct ifnet *);
164 
165 Static void cue_setmulti(struct cue_softc *);
166 Static u_int32_t cue_crc(const char *);
167 Static void cue_reset(struct cue_softc *);
168 
169 Static int cue_csr_read_1(struct cue_softc *, int);
170 Static int cue_csr_write_1(struct cue_softc *, int, int);
171 Static int cue_csr_read_2(struct cue_softc *, int);
172 #if 0
173 Static int cue_csr_write_2(struct cue_softc *, int, int);
174 #endif
175 Static int cue_mem(struct cue_softc *, int, int, void *, int);
176 Static int cue_getmac(struct cue_softc *, void *);
177 
178 #define CUE_SETBIT(sc, reg, x)				\
179 	cue_csr_write_1(sc, reg, cue_csr_read_1(sc, reg) | (x))
180 
181 #define CUE_CLRBIT(sc, reg, x)				\
182 	cue_csr_write_1(sc, reg, cue_csr_read_1(sc, reg) & ~(x))
183 
184 Static int
185 cue_csr_read_1(struct cue_softc	*sc, int reg)
186 {
187 	usb_device_request_t	req;
188 	usbd_status		err;
189 	u_int8_t		val = 0;
190 
191 	if (sc->cue_dying)
192 		return (0);
193 
194 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
195 	req.bRequest = CUE_CMD_READREG;
196 	USETW(req.wValue, 0);
197 	USETW(req.wIndex, reg);
198 	USETW(req.wLength, 1);
199 
200 	err = usbd_do_request(sc->cue_udev, &req, &val);
201 
202 	if (err) {
203 		DPRINTF(("%s: cue_csr_read_1: reg=0x%x err=%s\n",
204 			 USBDEVNAME(sc->cue_dev), reg, usbd_errstr(err)));
205 		return (0);
206 	}
207 
208 	DPRINTFN(10,("%s: cue_csr_read_1 reg=0x%x val=0x%x\n",
209 		     USBDEVNAME(sc->cue_dev), reg, val));
210 
211 	return (val);
212 }
213 
214 Static int
215 cue_csr_read_2(struct cue_softc	*sc, int reg)
216 {
217 	usb_device_request_t	req;
218 	usbd_status		err;
219 	uWord			val;
220 
221 	if (sc->cue_dying)
222 		return (0);
223 
224 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
225 	req.bRequest = CUE_CMD_READREG;
226 	USETW(req.wValue, 0);
227 	USETW(req.wIndex, reg);
228 	USETW(req.wLength, 2);
229 
230 	err = usbd_do_request(sc->cue_udev, &req, &val);
231 
232 	DPRINTFN(10,("%s: cue_csr_read_2 reg=0x%x val=0x%x\n",
233 		     USBDEVNAME(sc->cue_dev), reg, UGETW(val)));
234 
235 	if (err) {
236 		DPRINTF(("%s: cue_csr_read_2: reg=0x%x err=%s\n",
237 			 USBDEVNAME(sc->cue_dev), reg, usbd_errstr(err)));
238 		return (0);
239 	}
240 
241 	return (UGETW(val));
242 }
243 
244 Static int
245 cue_csr_write_1(struct cue_softc *sc, int reg, int val)
246 {
247 	usb_device_request_t	req;
248 	usbd_status		err;
249 
250 	if (sc->cue_dying)
251 		return (0);
252 
253 	DPRINTFN(10,("%s: cue_csr_write_1 reg=0x%x val=0x%x\n",
254 		     USBDEVNAME(sc->cue_dev), reg, val));
255 
256 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
257 	req.bRequest = CUE_CMD_WRITEREG;
258 	USETW(req.wValue, val);
259 	USETW(req.wIndex, reg);
260 	USETW(req.wLength, 0);
261 
262 	err = usbd_do_request(sc->cue_udev, &req, NULL);
263 
264 	if (err) {
265 		DPRINTF(("%s: cue_csr_write_1: reg=0x%x err=%s\n",
266 			 USBDEVNAME(sc->cue_dev), reg, usbd_errstr(err)));
267 		return (-1);
268 	}
269 
270 	DPRINTFN(20,("%s: cue_csr_write_1, after reg=0x%x val=0x%x\n",
271 		     USBDEVNAME(sc->cue_dev), reg, cue_csr_read_1(sc, reg)));
272 
273 	return (0);
274 }
275 
276 #if 0
277 Static int
278 cue_csr_write_2(struct cue_softc *sc, int reg, int aval)
279 {
280 	usb_device_request_t	req;
281 	usbd_status		err;
282 	uWord			val;
283 	int			s;
284 
285 	if (sc->cue_dying)
286 		return (0);
287 
288 	DPRINTFN(10,("%s: cue_csr_write_2 reg=0x%x val=0x%x\n",
289 		     USBDEVNAME(sc->cue_dev), reg, aval));
290 
291 	USETW(val, aval);
292 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
293 	req.bRequest = CUE_CMD_WRITEREG;
294 	USETW(req.wValue, val);
295 	USETW(req.wIndex, reg);
296 	USETW(req.wLength, 0);
297 
298 	err = usbd_do_request(sc->cue_udev, &req, NULL);
299 
300 	if (err) {
301 		DPRINTF(("%s: cue_csr_write_2: reg=0x%x err=%s\n",
302 			 USBDEVNAME(sc->cue_dev), reg, usbd_errstr(err)));
303 		return (-1);
304 	}
305 
306 	return (0);
307 }
308 #endif
309 
310 Static int
311 cue_mem(struct cue_softc *sc, int cmd, int addr, void *buf, int len)
312 {
313 	usb_device_request_t	req;
314 	usbd_status		err;
315 
316 	DPRINTFN(10,("%s: cue_mem cmd=0x%x addr=0x%x len=%d\n",
317 		     USBDEVNAME(sc->cue_dev), cmd, addr, len));
318 
319 	if (cmd == CUE_CMD_READSRAM)
320 		req.bmRequestType = UT_READ_VENDOR_DEVICE;
321 	else
322 		req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
323 	req.bRequest = cmd;
324 	USETW(req.wValue, 0);
325 	USETW(req.wIndex, addr);
326 	USETW(req.wLength, len);
327 
328 	err = usbd_do_request(sc->cue_udev, &req, buf);
329 
330 	if (err) {
331 		DPRINTF(("%s: cue_csr_mem: addr=0x%x err=%s\n",
332 			 USBDEVNAME(sc->cue_dev), addr, usbd_errstr(err)));
333 		return (-1);
334 	}
335 
336 	return (0);
337 }
338 
339 Static int
340 cue_getmac(struct cue_softc *sc, void *buf)
341 {
342 	usb_device_request_t	req;
343 	usbd_status		err;
344 
345 	DPRINTFN(10,("%s: cue_getmac\n", USBDEVNAME(sc->cue_dev)));
346 
347 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
348 	req.bRequest = CUE_CMD_GET_MACADDR;
349 	USETW(req.wValue, 0);
350 	USETW(req.wIndex, 0);
351 	USETW(req.wLength, ETHER_ADDR_LEN);
352 
353 	err = usbd_do_request(sc->cue_udev, &req, buf);
354 
355 	if (err) {
356 		printf("%s: read MAC address failed\n",USBDEVNAME(sc->cue_dev));
357 		return (-1);
358 	}
359 
360 	return (0);
361 }
362 
363 #define CUE_POLY	0xEDB88320
364 #define CUE_BITS	9
365 
366 Static u_int32_t
367 cue_crc(const char *addr)
368 {
369 	u_int32_t		idx, bit, data, crc;
370 
371 	/* Compute CRC for the address value. */
372 	crc = 0xFFFFFFFF; /* initial value */
373 
374 	for (idx = 0; idx < 6; idx++) {
375 		for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1)
376 			crc = (crc >> 1) ^ (((crc ^ data) & 1) ? CUE_POLY : 0);
377 	}
378 
379 	return (crc & ((1 << CUE_BITS) - 1));
380 }
381 
382 Static void
383 cue_setmulti(struct cue_softc *sc)
384 {
385 	struct ifnet		*ifp;
386 	struct ether_multi	*enm;
387 	struct ether_multistep	step;
388 	u_int32_t		h, i;
389 
390 	ifp = GET_IFP(sc);
391 
392 	DPRINTFN(2,("%s: cue_setmulti if_flags=0x%x\n",
393 		    USBDEVNAME(sc->cue_dev), ifp->if_flags));
394 
395 	if (ifp->if_flags & IFF_PROMISC) {
396 allmulti:
397 		ifp->if_flags |= IFF_ALLMULTI;
398 		for (i = 0; i < CUE_MCAST_TABLE_LEN; i++)
399 			sc->cue_mctab[i] = 0xFF;
400 		cue_mem(sc, CUE_CMD_WRITESRAM, CUE_MCAST_TABLE_ADDR,
401 		    &sc->cue_mctab, CUE_MCAST_TABLE_LEN);
402 		return;
403 	}
404 
405 	/* first, zot all the existing hash bits */
406 	for (i = 0; i < CUE_MCAST_TABLE_LEN; i++)
407 		sc->cue_mctab[i] = 0;
408 
409 	/* now program new ones */
410 #if defined(__NetBSD__)
411 	ETHER_FIRST_MULTI(step, &sc->cue_ec, enm);
412 #else
413 	ETHER_FIRST_MULTI(step, &sc->arpcom, enm);
414 #endif
415 	while (enm != NULL) {
416 		if (memcmp(enm->enm_addrlo,
417 		    enm->enm_addrhi, ETHER_ADDR_LEN) != 0)
418 			goto allmulti;
419 
420 		h = cue_crc(enm->enm_addrlo);
421 		sc->cue_mctab[h >> 3] |= 1 << (h & 0x7);
422 		ETHER_NEXT_MULTI(step, enm);
423 	}
424 
425 	ifp->if_flags &= ~IFF_ALLMULTI;
426 
427 	/*
428 	 * Also include the broadcast address in the filter
429 	 * so we can receive broadcast frames.
430 	 */
431 	if (ifp->if_flags & IFF_BROADCAST) {
432 		h = cue_crc(etherbroadcastaddr);
433 		sc->cue_mctab[h >> 3] |= 1 << (h & 0x7);
434 	}
435 
436 	cue_mem(sc, CUE_CMD_WRITESRAM, CUE_MCAST_TABLE_ADDR,
437 	    &sc->cue_mctab, CUE_MCAST_TABLE_LEN);
438 }
439 
440 Static void
441 cue_reset(struct cue_softc *sc)
442 {
443 	usb_device_request_t	req;
444 	usbd_status		err;
445 
446 	DPRINTFN(2,("%s: cue_reset\n", USBDEVNAME(sc->cue_dev)));
447 
448 	if (sc->cue_dying)
449 		return;
450 
451 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
452 	req.bRequest = CUE_CMD_RESET;
453 	USETW(req.wValue, 0);
454 	USETW(req.wIndex, 0);
455 	USETW(req.wLength, 0);
456 
457 	err = usbd_do_request(sc->cue_udev, &req, NULL);
458 
459 	if (err)
460 		printf("%s: reset failed\n", USBDEVNAME(sc->cue_dev));
461 
462 	/* Wait a little while for the chip to get its brains in order. */
463 	usbd_delay_ms(sc->cue_udev, 1);
464 }
465 
466 /*
467  * Probe for a CATC chip.
468  */
469 USB_MATCH(cue)
470 {
471 	USB_MATCH_START(cue, uaa);
472 
473 	if (uaa->iface != NULL)
474 		return (UMATCH_NONE);
475 
476 	return (cue_lookup(uaa->vendor, uaa->product) != NULL ?
477 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
478 }
479 
480 /*
481  * Attach the interface. Allocate softc structures, do ifmedia
482  * setup and ethernet/BPF attach.
483  */
484 USB_ATTACH(cue)
485 {
486 	USB_ATTACH_START(cue, sc, uaa);
487 	char			*devinfop;
488 	int			s;
489 	u_char			eaddr[ETHER_ADDR_LEN];
490 	usbd_device_handle	dev = uaa->device;
491 	usbd_interface_handle	iface;
492 	usbd_status		err;
493 	struct ifnet		*ifp;
494 	usb_interface_descriptor_t	*id;
495 	usb_endpoint_descriptor_t	*ed;
496 	int			i;
497 
498 	DPRINTFN(5,(" : cue_attach: sc=%p, dev=%p", sc, dev));
499 
500 	devinfop = usbd_devinfo_alloc(dev, 0);
501 	USB_ATTACH_SETUP;
502 	printf("%s: %s\n", USBDEVNAME(sc->cue_dev), devinfop);
503 	usbd_devinfo_free(devinfop);
504 
505 	err = usbd_set_config_no(dev, CUE_CONFIG_NO, 1);
506 	if (err) {
507 		printf("%s: setting config no failed\n",
508 		    USBDEVNAME(sc->cue_dev));
509 		USB_ATTACH_ERROR_RETURN;
510 	}
511 
512 	sc->cue_udev = dev;
513 	sc->cue_product = uaa->product;
514 	sc->cue_vendor = uaa->vendor;
515 
516 	usb_init_task(&sc->cue_tick_task, cue_tick_task, sc);
517 	usb_init_task(&sc->cue_stop_task, (void (*)(void *))cue_stop, sc);
518 
519 	err = usbd_device2interface_handle(dev, CUE_IFACE_IDX, &iface);
520 	if (err) {
521 		printf("%s: getting interface handle failed\n",
522 		    USBDEVNAME(sc->cue_dev));
523 		USB_ATTACH_ERROR_RETURN;
524 	}
525 
526 	sc->cue_iface = iface;
527 	id = usbd_get_interface_descriptor(iface);
528 
529 	/* Find endpoints. */
530 	for (i = 0; i < id->bNumEndpoints; i++) {
531 		ed = usbd_interface2endpoint_descriptor(iface, i);
532 		if (ed == NULL) {
533 			printf("%s: couldn't get ep %d\n",
534 			    USBDEVNAME(sc->cue_dev), i);
535 			USB_ATTACH_ERROR_RETURN;
536 		}
537 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
538 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
539 			sc->cue_ed[CUE_ENDPT_RX] = ed->bEndpointAddress;
540 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
541 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
542 			sc->cue_ed[CUE_ENDPT_TX] = ed->bEndpointAddress;
543 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
544 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
545 			sc->cue_ed[CUE_ENDPT_INTR] = ed->bEndpointAddress;
546 		}
547 	}
548 
549 #if 0
550 	/* Reset the adapter. */
551 	cue_reset(sc);
552 #endif
553 	/*
554 	 * Get station address.
555 	 */
556 	cue_getmac(sc, &eaddr);
557 
558 	s = splnet();
559 
560 	/*
561 	 * A CATC chip was detected. Inform the world.
562 	 */
563 	printf("%s: Ethernet address %s\n", USBDEVNAME(sc->cue_dev),
564 	    ether_sprintf(eaddr));
565 
566 	/* Initialize interface info.*/
567 	ifp = GET_IFP(sc);
568 	ifp->if_softc = sc;
569 	ifp->if_mtu = ETHERMTU;
570 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
571 	ifp->if_ioctl = cue_ioctl;
572 	ifp->if_start = cue_start;
573 	ifp->if_watchdog = cue_watchdog;
574 #if defined(__OpenBSD__)
575 	ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
576 #endif
577 	strncpy(ifp->if_xname, USBDEVNAME(sc->cue_dev), IFNAMSIZ);
578 
579 	IFQ_SET_READY(&ifp->if_snd);
580 
581 	/* Attach the interface. */
582 	if_attach(ifp);
583 	Ether_ifattach(ifp, eaddr);
584 #if NRND > 0
585 	rnd_attach_source(&sc->rnd_source, USBDEVNAME(sc->cue_dev),
586 	    RND_TYPE_NET, 0);
587 #endif
588 
589 	usb_callout_init(sc->cue_stat_ch);
590 
591 	sc->cue_attached = 1;
592 	splx(s);
593 
594 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->cue_udev,
595 	    USBDEV(sc->cue_dev));
596 
597 	USB_ATTACH_SUCCESS_RETURN;
598 }
599 
600 USB_DETACH(cue)
601 {
602 	USB_DETACH_START(cue, sc);
603 	struct ifnet		*ifp = GET_IFP(sc);
604 	int			s;
605 
606 	DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->cue_dev), __func__));
607 
608 	usb_uncallout(sc->cue_stat_ch, cue_tick, sc);
609 	/*
610 	 * Remove any pending task.  It cannot be executing because it run
611 	 * in the same thread as detach.
612 	 */
613 	usb_rem_task(sc->cue_udev, &sc->cue_tick_task);
614 	usb_rem_task(sc->cue_udev, &sc->cue_stop_task);
615 
616 	if (!sc->cue_attached) {
617 		/* Detached before attached finished, so just bail out. */
618 		return (0);
619 	}
620 
621 	s = splusb();
622 
623 	if (ifp->if_flags & IFF_RUNNING)
624 		cue_stop(sc);
625 
626 #if defined(__NetBSD__)
627 #if NRND > 0
628 	rnd_detach_source(&sc->rnd_source);
629 #endif
630 	ether_ifdetach(ifp);
631 #endif /* __NetBSD__ */
632 
633 	if_detach(ifp);
634 
635 #ifdef DIAGNOSTIC
636 	if (sc->cue_ep[CUE_ENDPT_TX] != NULL ||
637 	    sc->cue_ep[CUE_ENDPT_RX] != NULL ||
638 	    sc->cue_ep[CUE_ENDPT_INTR] != NULL)
639 		printf("%s: detach has active endpoints\n",
640 		       USBDEVNAME(sc->cue_dev));
641 #endif
642 
643 	sc->cue_attached = 0;
644 	splx(s);
645 
646 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->cue_udev,
647 	    USBDEV(sc->cue_dev));
648 
649 	return (0);
650 }
651 
652 int
653 cue_activate(device_ptr_t self, enum devact act)
654 {
655 	struct cue_softc *sc = (struct cue_softc *)self;
656 
657 	DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->cue_dev), __func__));
658 
659 	switch (act) {
660 	case DVACT_ACTIVATE:
661 		return (EOPNOTSUPP);
662 		break;
663 
664 	case DVACT_DEACTIVATE:
665 		/* Deactivate the interface. */
666 		if_deactivate(&sc->cue_ec.ec_if);
667 		sc->cue_dying = 1;
668 		break;
669 	}
670 	return (0);
671 }
672 
673 /*
674  * Initialize an RX descriptor and attach an MBUF cluster.
675  */
676 Static int
677 cue_newbuf(struct cue_softc *sc, struct cue_chain *c, struct mbuf *m)
678 {
679 	struct mbuf		*m_new = NULL;
680 
681 	if (m == NULL) {
682 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
683 		if (m_new == NULL) {
684 			printf("%s: no memory for rx list "
685 			    "-- packet dropped!\n", USBDEVNAME(sc->cue_dev));
686 			return (ENOBUFS);
687 		}
688 
689 		MCLGET(m_new, M_DONTWAIT);
690 		if (!(m_new->m_flags & M_EXT)) {
691 			printf("%s: no memory for rx list "
692 			    "-- packet dropped!\n", USBDEVNAME(sc->cue_dev));
693 			m_freem(m_new);
694 			return (ENOBUFS);
695 		}
696 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
697 	} else {
698 		m_new = m;
699 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
700 		m_new->m_data = m_new->m_ext.ext_buf;
701 	}
702 
703 	m_adj(m_new, ETHER_ALIGN);
704 	c->cue_mbuf = m_new;
705 
706 	return (0);
707 }
708 
709 Static int
710 cue_rx_list_init(struct cue_softc *sc)
711 {
712 	struct cue_cdata	*cd;
713 	struct cue_chain	*c;
714 	int			i;
715 
716 	cd = &sc->cue_cdata;
717 	for (i = 0; i < CUE_RX_LIST_CNT; i++) {
718 		c = &cd->cue_rx_chain[i];
719 		c->cue_sc = sc;
720 		c->cue_idx = i;
721 		if (cue_newbuf(sc, c, NULL) == ENOBUFS)
722 			return (ENOBUFS);
723 		if (c->cue_xfer == NULL) {
724 			c->cue_xfer = usbd_alloc_xfer(sc->cue_udev);
725 			if (c->cue_xfer == NULL)
726 				return (ENOBUFS);
727 			c->cue_buf = usbd_alloc_buffer(c->cue_xfer, CUE_BUFSZ);
728 			if (c->cue_buf == NULL) {
729 				usbd_free_xfer(c->cue_xfer);
730 				return (ENOBUFS);
731 			}
732 		}
733 	}
734 
735 	return (0);
736 }
737 
738 Static int
739 cue_tx_list_init(struct cue_softc *sc)
740 {
741 	struct cue_cdata	*cd;
742 	struct cue_chain	*c;
743 	int			i;
744 
745 	cd = &sc->cue_cdata;
746 	for (i = 0; i < CUE_TX_LIST_CNT; i++) {
747 		c = &cd->cue_tx_chain[i];
748 		c->cue_sc = sc;
749 		c->cue_idx = i;
750 		c->cue_mbuf = NULL;
751 		if (c->cue_xfer == NULL) {
752 			c->cue_xfer = usbd_alloc_xfer(sc->cue_udev);
753 			if (c->cue_xfer == NULL)
754 				return (ENOBUFS);
755 			c->cue_buf = usbd_alloc_buffer(c->cue_xfer, CUE_BUFSZ);
756 			if (c->cue_buf == NULL) {
757 				usbd_free_xfer(c->cue_xfer);
758 				return (ENOBUFS);
759 			}
760 		}
761 	}
762 
763 	return (0);
764 }
765 
766 /*
767  * A frame has been uploaded: pass the resulting mbuf chain up to
768  * the higher level protocols.
769  */
770 Static void
771 cue_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
772 {
773 	struct cue_chain	*c = priv;
774 	struct cue_softc	*sc = c->cue_sc;
775 	struct ifnet		*ifp = GET_IFP(sc);
776 	struct mbuf		*m;
777 	int			total_len = 0;
778 	u_int16_t		len;
779 	int			s;
780 
781 	DPRINTFN(10,("%s: %s: enter status=%d\n", USBDEVNAME(sc->cue_dev),
782 		     __func__, status));
783 
784 	if (sc->cue_dying)
785 		return;
786 
787 	if (!(ifp->if_flags & IFF_RUNNING))
788 		return;
789 
790 	if (status != USBD_NORMAL_COMPLETION) {
791 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
792 			return;
793 		sc->cue_rx_errs++;
794 		if (usbd_ratecheck(&sc->cue_rx_notice)) {
795 			printf("%s: %u usb errors on rx: %s\n",
796 			    USBDEVNAME(sc->cue_dev), sc->cue_rx_errs,
797 			    usbd_errstr(status));
798 			sc->cue_rx_errs = 0;
799 		}
800 		if (status == USBD_STALLED)
801 			usbd_clear_endpoint_stall_async(sc->cue_ep[CUE_ENDPT_RX]);
802 		goto done;
803 	}
804 
805 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
806 
807 	memcpy(mtod(c->cue_mbuf, char *), c->cue_buf, total_len);
808 
809 	m = c->cue_mbuf;
810 	len = UGETW(mtod(m, u_int8_t *));
811 
812 	/* No errors; receive the packet. */
813 	total_len = len;
814 
815 	if (len < sizeof(struct ether_header)) {
816 		ifp->if_ierrors++;
817 		goto done;
818 	}
819 
820 	ifp->if_ipackets++;
821 	m_adj(m, sizeof(u_int16_t));
822 	m->m_pkthdr.len = m->m_len = total_len;
823 
824 	m->m_pkthdr.rcvif = ifp;
825 
826 	s = splnet();
827 
828 	/* XXX ugly */
829 	if (cue_newbuf(sc, c, NULL) == ENOBUFS) {
830 		ifp->if_ierrors++;
831 		goto done1;
832 	}
833 
834 #if NBPFILTER > 0
835 	/*
836 	 * Handle BPF listeners. Let the BPF user see the packet, but
837 	 * don't pass it up to the ether_input() layer unless it's
838 	 * a broadcast packet, multicast packet, matches our ethernet
839 	 * address or the interface is in promiscuous mode.
840 	 */
841 	if (ifp->if_bpf)
842 		BPF_MTAP(ifp, m);
843 #endif
844 
845 	DPRINTFN(10,("%s: %s: deliver %d\n", USBDEVNAME(sc->cue_dev),
846 		    __func__, m->m_len));
847 	IF_INPUT(ifp, m);
848  done1:
849 	splx(s);
850 
851 done:
852 	/* Setup new transfer. */
853 	usbd_setup_xfer(c->cue_xfer, sc->cue_ep[CUE_ENDPT_RX],
854 	    c, c->cue_buf, CUE_BUFSZ, USBD_SHORT_XFER_OK | USBD_NO_COPY,
855 	    USBD_NO_TIMEOUT, cue_rxeof);
856 	usbd_transfer(c->cue_xfer);
857 
858 	DPRINTFN(10,("%s: %s: start rx\n", USBDEVNAME(sc->cue_dev),
859 		    __func__));
860 }
861 
862 /*
863  * A frame was downloaded to the chip. It's safe for us to clean up
864  * the list buffers.
865  */
866 Static void
867 cue_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
868 {
869 	struct cue_chain	*c = priv;
870 	struct cue_softc	*sc = c->cue_sc;
871 	struct ifnet		*ifp = GET_IFP(sc);
872 	int			s;
873 
874 	if (sc->cue_dying)
875 		return;
876 
877 	s = splnet();
878 
879 	DPRINTFN(10,("%s: %s: enter status=%d\n", USBDEVNAME(sc->cue_dev),
880 		    __func__, status));
881 
882 	ifp->if_timer = 0;
883 	ifp->if_flags &= ~IFF_OACTIVE;
884 
885 	if (status != USBD_NORMAL_COMPLETION) {
886 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
887 			splx(s);
888 			return;
889 		}
890 		ifp->if_oerrors++;
891 		printf("%s: usb error on tx: %s\n", USBDEVNAME(sc->cue_dev),
892 		    usbd_errstr(status));
893 		if (status == USBD_STALLED)
894 			usbd_clear_endpoint_stall_async(sc->cue_ep[CUE_ENDPT_TX]);
895 		splx(s);
896 		return;
897 	}
898 
899 	ifp->if_opackets++;
900 
901 	m_freem(c->cue_mbuf);
902 	c->cue_mbuf = NULL;
903 
904 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
905 		cue_start(ifp);
906 
907 	splx(s);
908 }
909 
910 Static void
911 cue_tick(void *xsc)
912 {
913 	struct cue_softc	*sc = xsc;
914 
915 	if (sc == NULL)
916 		return;
917 
918 	if (sc->cue_dying)
919 		return;
920 
921 	DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->cue_dev), __func__));
922 
923 	/* Perform statistics update in process context. */
924 	usb_add_task(sc->cue_udev, &sc->cue_tick_task);
925 }
926 
927 Static void
928 cue_tick_task(void *xsc)
929 {
930 	struct cue_softc	*sc = xsc;
931 	struct ifnet		*ifp;
932 
933 	if (sc->cue_dying)
934 		return;
935 
936 	DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->cue_dev), __func__));
937 
938 	ifp = GET_IFP(sc);
939 
940 	ifp->if_collisions += cue_csr_read_2(sc, CUE_TX_SINGLECOLL);
941 	ifp->if_collisions += cue_csr_read_2(sc, CUE_TX_MULTICOLL);
942 	ifp->if_collisions += cue_csr_read_2(sc, CUE_TX_EXCESSCOLL);
943 
944 	if (cue_csr_read_2(sc, CUE_RX_FRAMEERR))
945 		ifp->if_ierrors++;
946 }
947 
948 Static int
949 cue_send(struct cue_softc *sc, struct mbuf *m, int idx)
950 {
951 	int			total_len;
952 	struct cue_chain	*c;
953 	usbd_status		err;
954 
955 	c = &sc->cue_cdata.cue_tx_chain[idx];
956 
957 	/*
958 	 * Copy the mbuf data into a contiguous buffer, leaving two
959 	 * bytes at the beginning to hold the frame length.
960 	 */
961 	m_copydata(m, 0, m->m_pkthdr.len, c->cue_buf + 2);
962 	c->cue_mbuf = m;
963 
964 	total_len = m->m_pkthdr.len + 2;
965 
966 	DPRINTFN(10,("%s: %s: total_len=%d\n",
967 		     USBDEVNAME(sc->cue_dev), __func__, total_len));
968 
969 	/* The first two bytes are the frame length */
970 	c->cue_buf[0] = (u_int8_t)m->m_pkthdr.len;
971 	c->cue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8);
972 
973 	/* XXX 10000 */
974 	usbd_setup_xfer(c->cue_xfer, sc->cue_ep[CUE_ENDPT_TX],
975 	    c, c->cue_buf, total_len, USBD_NO_COPY, 10000, cue_txeof);
976 
977 	/* Transmit */
978 	err = usbd_transfer(c->cue_xfer);
979 	if (err != USBD_IN_PROGRESS) {
980 		printf("%s: cue_send error=%s\n", USBDEVNAME(sc->cue_dev),
981 		       usbd_errstr(err));
982 		/* Stop the interface from process context. */
983 		usb_add_task(sc->cue_udev, &sc->cue_stop_task);
984 		return (EIO);
985 	}
986 
987 	sc->cue_cdata.cue_tx_cnt++;
988 
989 	return (0);
990 }
991 
992 Static void
993 cue_start(struct ifnet *ifp)
994 {
995 	struct cue_softc	*sc = ifp->if_softc;
996 	struct mbuf		*m_head = NULL;
997 
998 	if (sc->cue_dying)
999 		return;
1000 
1001 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->cue_dev),__func__));
1002 
1003 	if (ifp->if_flags & IFF_OACTIVE)
1004 		return;
1005 
1006 	IFQ_POLL(&ifp->if_snd, m_head);
1007 	if (m_head == NULL)
1008 		return;
1009 
1010 	if (cue_send(sc, m_head, 0)) {
1011 		ifp->if_flags |= IFF_OACTIVE;
1012 		return;
1013 	}
1014 
1015 	IFQ_DEQUEUE(&ifp->if_snd, m_head);
1016 
1017 #if NBPFILTER > 0
1018 	/*
1019 	 * If there's a BPF listener, bounce a copy of this frame
1020 	 * to him.
1021 	 */
1022 	if (ifp->if_bpf)
1023 		BPF_MTAP(ifp, m_head);
1024 #endif
1025 
1026 	ifp->if_flags |= IFF_OACTIVE;
1027 
1028 	/*
1029 	 * Set a timeout in case the chip goes out to lunch.
1030 	 */
1031 	ifp->if_timer = 5;
1032 }
1033 
1034 Static void
1035 cue_init(void *xsc)
1036 {
1037 	struct cue_softc	*sc = xsc;
1038 	struct ifnet		*ifp = GET_IFP(sc);
1039 	int			i, s, ctl;
1040 	u_char			*eaddr;
1041 
1042 	if (sc->cue_dying)
1043 		return;
1044 
1045 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->cue_dev),__func__));
1046 
1047 	if (ifp->if_flags & IFF_RUNNING)
1048 		return;
1049 
1050 	s = splnet();
1051 
1052 	/*
1053 	 * Cancel pending I/O and free all RX/TX buffers.
1054 	 */
1055 #if 1
1056 	cue_reset(sc);
1057 #endif
1058 
1059 	/* Set advanced operation modes. */
1060 	cue_csr_write_1(sc, CUE_ADVANCED_OPMODES,
1061 	    CUE_AOP_EMBED_RXLEN | 0x03); /* 1 wait state */
1062 
1063 #if defined(__OpenBSD__)
1064 	eaddr = sc->arpcom.ac_enaddr;
1065 #elif defined(__NetBSD__)
1066 	eaddr = LLADDR(ifp->if_sadl);
1067 #endif
1068 	/* Set MAC address */
1069 	for (i = 0; i < ETHER_ADDR_LEN; i++)
1070 		cue_csr_write_1(sc, CUE_PAR0 - i, eaddr[i]);
1071 
1072 	/* Enable RX logic. */
1073 	ctl = CUE_ETHCTL_RX_ON | CUE_ETHCTL_MCAST_ON;
1074 	if (ifp->if_flags & IFF_PROMISC)
1075 		ctl |= CUE_ETHCTL_PROMISC;
1076 	cue_csr_write_1(sc, CUE_ETHCTL, ctl);
1077 
1078 	/* Init TX ring. */
1079 	if (cue_tx_list_init(sc) == ENOBUFS) {
1080 		printf("%s: tx list init failed\n", USBDEVNAME(sc->cue_dev));
1081 		splx(s);
1082 		return;
1083 	}
1084 
1085 	/* Init RX ring. */
1086 	if (cue_rx_list_init(sc) == ENOBUFS) {
1087 		printf("%s: rx list init failed\n", USBDEVNAME(sc->cue_dev));
1088 		splx(s);
1089 		return;
1090 	}
1091 
1092 	/* Load the multicast filter. */
1093 	cue_setmulti(sc);
1094 
1095 	/*
1096 	 * Set the number of RX and TX buffers that we want
1097 	 * to reserve inside the ASIC.
1098 	 */
1099 	cue_csr_write_1(sc, CUE_RX_BUFPKTS, CUE_RX_FRAMES);
1100 	cue_csr_write_1(sc, CUE_TX_BUFPKTS, CUE_TX_FRAMES);
1101 
1102 	/* Set advanced operation modes. */
1103 	cue_csr_write_1(sc, CUE_ADVANCED_OPMODES,
1104 	    CUE_AOP_EMBED_RXLEN | 0x01); /* 1 wait state */
1105 
1106 	/* Program the LED operation. */
1107 	cue_csr_write_1(sc, CUE_LEDCTL, CUE_LEDCTL_FOLLOW_LINK);
1108 
1109 	if (sc->cue_ep[CUE_ENDPT_RX] == NULL) {
1110 		if (cue_open_pipes(sc)) {
1111 			splx(s);
1112 			return;
1113 		}
1114 	}
1115 
1116 	ifp->if_flags |= IFF_RUNNING;
1117 	ifp->if_flags &= ~IFF_OACTIVE;
1118 
1119 	splx(s);
1120 
1121 	usb_callout(sc->cue_stat_ch, hz, cue_tick, sc);
1122 }
1123 
1124 Static int
1125 cue_open_pipes(struct cue_softc	*sc)
1126 {
1127 	struct cue_chain	*c;
1128 	usbd_status		err;
1129 	int			i;
1130 
1131 	/* Open RX and TX pipes. */
1132 	err = usbd_open_pipe(sc->cue_iface, sc->cue_ed[CUE_ENDPT_RX],
1133 	    USBD_EXCLUSIVE_USE, &sc->cue_ep[CUE_ENDPT_RX]);
1134 	if (err) {
1135 		printf("%s: open rx pipe failed: %s\n",
1136 		    USBDEVNAME(sc->cue_dev), usbd_errstr(err));
1137 		return (EIO);
1138 	}
1139 	err = usbd_open_pipe(sc->cue_iface, sc->cue_ed[CUE_ENDPT_TX],
1140 	    USBD_EXCLUSIVE_USE, &sc->cue_ep[CUE_ENDPT_TX]);
1141 	if (err) {
1142 		printf("%s: open tx pipe failed: %s\n",
1143 		    USBDEVNAME(sc->cue_dev), usbd_errstr(err));
1144 		return (EIO);
1145 	}
1146 
1147 	/* Start up the receive pipe. */
1148 	for (i = 0; i < CUE_RX_LIST_CNT; i++) {
1149 		c = &sc->cue_cdata.cue_rx_chain[i];
1150 		usbd_setup_xfer(c->cue_xfer, sc->cue_ep[CUE_ENDPT_RX],
1151 		    c, c->cue_buf, CUE_BUFSZ,
1152 		    USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT,
1153 		    cue_rxeof);
1154 		usbd_transfer(c->cue_xfer);
1155 	}
1156 
1157 	return (0);
1158 }
1159 
1160 Static int
1161 cue_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
1162 {
1163 	struct cue_softc	*sc = ifp->if_softc;
1164 	struct ifaddr 		*ifa = (struct ifaddr *)data;
1165 	struct ifreq		*ifr = (struct ifreq *)data;
1166 	int			s, error = 0;
1167 
1168 	if (sc->cue_dying)
1169 		return (EIO);
1170 
1171 	s = splnet();
1172 
1173 	switch(command) {
1174 	case SIOCSIFADDR:
1175 		ifp->if_flags |= IFF_UP;
1176 		cue_init(sc);
1177 
1178 		switch (ifa->ifa_addr->sa_family) {
1179 #ifdef INET
1180 		case AF_INET:
1181 #if defined(__NetBSD__)
1182 			arp_ifinit(ifp, ifa);
1183 #else
1184 			arp_ifinit(&sc->arpcom, ifa);
1185 #endif
1186 			break;
1187 #endif /* INET */
1188 #ifdef NS
1189 		case AF_NS:
1190 		    {
1191 			struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
1192 
1193 			if (ns_nullhost(*ina))
1194 				ina->x_host = *(union ns_host *)
1195 					LLADDR(ifp->if_sadl);
1196 			else
1197 				memcpy(LLADDR(ifp->if_sadl),
1198 				       ina->x_host.c_host,
1199 				       ifp->if_addrlen);
1200 			break;
1201 		    }
1202 #endif /* NS */
1203 		}
1204 		break;
1205 
1206 	case SIOCSIFMTU:
1207 		if (ifr->ifr_mtu > ETHERMTU)
1208 			error = EINVAL;
1209 		else
1210 			ifp->if_mtu = ifr->ifr_mtu;
1211 		break;
1212 
1213 	case SIOCSIFFLAGS:
1214 		if (ifp->if_flags & IFF_UP) {
1215 			if (ifp->if_flags & IFF_RUNNING &&
1216 			    ifp->if_flags & IFF_PROMISC &&
1217 			    !(sc->cue_if_flags & IFF_PROMISC)) {
1218 				CUE_SETBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
1219 				cue_setmulti(sc);
1220 			} else if (ifp->if_flags & IFF_RUNNING &&
1221 			    !(ifp->if_flags & IFF_PROMISC) &&
1222 			    sc->cue_if_flags & IFF_PROMISC) {
1223 				CUE_CLRBIT(sc, CUE_ETHCTL, CUE_ETHCTL_PROMISC);
1224 				cue_setmulti(sc);
1225 			} else if (!(ifp->if_flags & IFF_RUNNING))
1226 				cue_init(sc);
1227 		} else {
1228 			if (ifp->if_flags & IFF_RUNNING)
1229 				cue_stop(sc);
1230 		}
1231 		sc->cue_if_flags = ifp->if_flags;
1232 		error = 0;
1233 		break;
1234 	case SIOCADDMULTI:
1235 	case SIOCDELMULTI:
1236 		cue_setmulti(sc);
1237 		error = 0;
1238 		break;
1239 	default:
1240 		error = EINVAL;
1241 		break;
1242 	}
1243 
1244 	splx(s);
1245 
1246 	return (error);
1247 }
1248 
1249 Static void
1250 cue_watchdog(struct ifnet *ifp)
1251 {
1252 	struct cue_softc	*sc = ifp->if_softc;
1253 	struct cue_chain	*c;
1254 	usbd_status		stat;
1255 	int			s;
1256 
1257 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->cue_dev),__func__));
1258 
1259 	if (sc->cue_dying)
1260 		return;
1261 
1262 	ifp->if_oerrors++;
1263 	printf("%s: watchdog timeout\n", USBDEVNAME(sc->cue_dev));
1264 
1265 	s = splusb();
1266 	c = &sc->cue_cdata.cue_tx_chain[0];
1267 	usbd_get_xfer_status(c->cue_xfer, NULL, NULL, NULL, &stat);
1268 	cue_txeof(c->cue_xfer, c, stat);
1269 
1270 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1271 		cue_start(ifp);
1272 	splx(s);
1273 }
1274 
1275 /*
1276  * Stop the adapter and free any mbufs allocated to the
1277  * RX and TX lists.
1278  */
1279 Static void
1280 cue_stop(struct cue_softc *sc)
1281 {
1282 	usbd_status		err;
1283 	struct ifnet		*ifp;
1284 	int			i;
1285 
1286 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->cue_dev),__func__));
1287 
1288 	ifp = GET_IFP(sc);
1289 	ifp->if_timer = 0;
1290 
1291 	cue_csr_write_1(sc, CUE_ETHCTL, 0);
1292 	cue_reset(sc);
1293 	usb_uncallout(sc->cue_stat_ch, cue_tick, sc);
1294 
1295 	/* Stop transfers. */
1296 	if (sc->cue_ep[CUE_ENDPT_RX] != NULL) {
1297 		err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_RX]);
1298 		if (err) {
1299 			printf("%s: abort rx pipe failed: %s\n",
1300 			USBDEVNAME(sc->cue_dev), usbd_errstr(err));
1301 		}
1302 		err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_RX]);
1303 		if (err) {
1304 			printf("%s: close rx pipe failed: %s\n",
1305 			USBDEVNAME(sc->cue_dev), usbd_errstr(err));
1306 		}
1307 		sc->cue_ep[CUE_ENDPT_RX] = NULL;
1308 	}
1309 
1310 	if (sc->cue_ep[CUE_ENDPT_TX] != NULL) {
1311 		err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_TX]);
1312 		if (err) {
1313 			printf("%s: abort tx pipe failed: %s\n",
1314 			USBDEVNAME(sc->cue_dev), usbd_errstr(err));
1315 		}
1316 		err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_TX]);
1317 		if (err) {
1318 			printf("%s: close tx pipe failed: %s\n",
1319 			    USBDEVNAME(sc->cue_dev), usbd_errstr(err));
1320 		}
1321 		sc->cue_ep[CUE_ENDPT_TX] = NULL;
1322 	}
1323 
1324 	if (sc->cue_ep[CUE_ENDPT_INTR] != NULL) {
1325 		err = usbd_abort_pipe(sc->cue_ep[CUE_ENDPT_INTR]);
1326 		if (err) {
1327 			printf("%s: abort intr pipe failed: %s\n",
1328 			USBDEVNAME(sc->cue_dev), usbd_errstr(err));
1329 		}
1330 		err = usbd_close_pipe(sc->cue_ep[CUE_ENDPT_INTR]);
1331 		if (err) {
1332 			printf("%s: close intr pipe failed: %s\n",
1333 			    USBDEVNAME(sc->cue_dev), usbd_errstr(err));
1334 		}
1335 		sc->cue_ep[CUE_ENDPT_INTR] = NULL;
1336 	}
1337 
1338 	/* Free RX resources. */
1339 	for (i = 0; i < CUE_RX_LIST_CNT; i++) {
1340 		if (sc->cue_cdata.cue_rx_chain[i].cue_mbuf != NULL) {
1341 			m_freem(sc->cue_cdata.cue_rx_chain[i].cue_mbuf);
1342 			sc->cue_cdata.cue_rx_chain[i].cue_mbuf = NULL;
1343 		}
1344 		if (sc->cue_cdata.cue_rx_chain[i].cue_xfer != NULL) {
1345 			usbd_free_xfer(sc->cue_cdata.cue_rx_chain[i].cue_xfer);
1346 			sc->cue_cdata.cue_rx_chain[i].cue_xfer = NULL;
1347 		}
1348 	}
1349 
1350 	/* Free TX resources. */
1351 	for (i = 0; i < CUE_TX_LIST_CNT; i++) {
1352 		if (sc->cue_cdata.cue_tx_chain[i].cue_mbuf != NULL) {
1353 			m_freem(sc->cue_cdata.cue_tx_chain[i].cue_mbuf);
1354 			sc->cue_cdata.cue_tx_chain[i].cue_mbuf = NULL;
1355 		}
1356 		if (sc->cue_cdata.cue_tx_chain[i].cue_xfer != NULL) {
1357 			usbd_free_xfer(sc->cue_cdata.cue_tx_chain[i].cue_xfer);
1358 			sc->cue_cdata.cue_tx_chain[i].cue_xfer = NULL;
1359 		}
1360 	}
1361 
1362 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1363 }
1364