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