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