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