xref: /openbsd-src/sys/dev/usb/if_kue.c (revision 2b0358df1d88d06ef4139321dd05bd5e05d91eaf)
1 /*	$OpenBSD: if_kue.c,v 1.58 2008/11/28 02:44:18 brad Exp $ */
2 /*	$NetBSD: if_kue.c,v 1.50 2002/07/16 22:00:31 augustss Exp $	*/
3 /*
4  * Copyright (c) 1997, 1998, 1999, 2000
5  *	Bill Paul <wpaul@ee.columbia.edu>.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by Bill Paul.
18  * 4. Neither the name of the author nor the names of any co-contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32  * THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  * $FreeBSD: src/sys/dev/usb/if_kue.c,v 1.14 2000/01/14 01:36:15 wpaul Exp $
35  */
36 
37 /*
38  * Kawasaki LSI KL5KUSB101B USB to ethernet adapter driver.
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 KLSI USB to ethernet adapter chip contains an USB serial interface,
47  * ethernet MAC and embedded microcontroller (called the QT Engine).
48  * The chip must have firmware loaded into it before it will operate.
49  * Packets are passed between the chip and host via bulk transfers.
50  * There is an interrupt endpoint mentioned in the software spec, however
51  * it's currently unused. This device is 10Mbps half-duplex only, hence
52  * there is no media selection logic. The MAC supports a 128 entry
53  * multicast filter, though the exact size of the filter can depend
54  * on the firmware. Curiously, while the software spec describes various
55  * ethernet statistics counters, my sample adapter and firmware combination
56  * claims not to support any statistics counters at all.
57  *
58  * Note that once we load the firmware in the device, we have to be
59  * careful not to load it again: if you restart your computer but
60  * leave the adapter attached to the USB controller, it may remain
61  * powered on and retain its firmware. In this case, we don't need
62  * to load the firmware a second time.
63  *
64  * Special thanks to Rob Furr for providing an ADS Technologies
65  * adapter for development and testing. No monkeys were harmed during
66  * the development of this driver.
67  */
68 
69 /*
70  * Ported to NetBSD and somewhat rewritten by Lennart Augustsson.
71  */
72 
73 #include "bpfilter.h"
74 
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/sockio.h>
78 #include <sys/mbuf.h>
79 #include <sys/malloc.h>
80 #include <sys/kernel.h>
81 #include <sys/socket.h>
82 #include <sys/device.h>
83 #include <sys/proc.h>
84 
85 #include <net/if.h>
86 #include <net/if_dl.h>
87 
88 #if NBPFILTER > 0
89 #include <net/bpf.h>
90 #endif
91 
92 #ifdef INET
93 #include <netinet/in.h>
94 #include <netinet/in_systm.h>
95 #include <netinet/in_var.h>
96 #include <netinet/ip.h>
97 #include <netinet/if_ether.h>
98 #endif
99 
100 #include <dev/usb/usb.h>
101 #include <dev/usb/usbdi.h>
102 #include <dev/usb/usbdi_util.h>
103 #include <dev/usb/usbdevs.h>
104 
105 #include <dev/usb/if_kuereg.h>
106 #include <dev/usb/if_kuevar.h>
107 
108 #ifdef KUE_DEBUG
109 #define DPRINTF(x)	do { if (kuedebug) printf x; } while (0)
110 #define DPRINTFN(n,x)	do { if (kuedebug >= (n)) printf x; } while (0)
111 int	kuedebug = 0;
112 #else
113 #define DPRINTF(x)
114 #define DPRINTFN(n,x)
115 #endif
116 
117 /*
118  * Various supported device vendors/products.
119  */
120 const struct usb_devno kue_devs[] = {
121 	{ USB_VENDOR_3COM, USB_PRODUCT_3COM_3C19250 },
122 	{ USB_VENDOR_3COM, USB_PRODUCT_3COM_3C460 },
123 	{ USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_URE450 },
124 	{ USB_VENDOR_ADS, USB_PRODUCT_ADS_UBS10BT },
125 	{ USB_VENDOR_ADS, USB_PRODUCT_ADS_UBS10BTX },
126 	{ USB_VENDOR_AOX, USB_PRODUCT_AOX_USB101 },
127 	{ USB_VENDOR_ASANTE, USB_PRODUCT_ASANTE_EA },
128 	{ USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC10T },
129 	{ USB_VENDOR_ATEN, USB_PRODUCT_ATEN_DSB650C },
130 	{ USB_VENDOR_COREGA, USB_PRODUCT_COREGA_ETHER_USB_T },
131 	{ USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650C },
132 	{ USB_VENDOR_ENTREGA, USB_PRODUCT_ENTREGA_E45 },
133 	{ USB_VENDOR_ENTREGA, USB_PRODUCT_ENTREGA_XX1 },
134 	{ USB_VENDOR_ENTREGA, USB_PRODUCT_ENTREGA_XX2 },
135 	{ USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBETT },
136 	{ USB_VENDOR_JATON, USB_PRODUCT_JATON_EDA },
137 	{ USB_VENDOR_KINGSTON, USB_PRODUCT_KINGSTON_XX1 },
138 	{ USB_VENDOR_KLSI, USB_PRODUCT_KLSI_DUH3E10BT },
139 	{ USB_VENDOR_KLSI, USB_PRODUCT_KLSI_DUH3E10BTN },
140 	{ USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10T },
141 	{ USB_VENDOR_MOBILITY, USB_PRODUCT_MOBILITY_EA },
142 	{ USB_VENDOR_NETGEAR, USB_PRODUCT_NETGEAR_EA101 },
143 	{ USB_VENDOR_NETGEAR, USB_PRODUCT_NETGEAR_EA101X },
144 	{ USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_ENET },
145 	{ USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_ENET2 },
146 	{ USB_VENDOR_PERACOM, USB_PRODUCT_PERACOM_ENET3 },
147 	{ USB_VENDOR_PORTGEAR, USB_PRODUCT_PORTGEAR_EA8 },
148 	{ USB_VENDOR_PORTGEAR, USB_PRODUCT_PORTGEAR_EA9 },
149 	{ USB_VENDOR_PORTSMITH, USB_PRODUCT_PORTSMITH_EEA },
150 	{ USB_VENDOR_SHARK, USB_PRODUCT_SHARK_PA },
151 	{ USB_VENDOR_SILICOM, USB_PRODUCT_SILICOM_U2E },
152 	{ USB_VENDOR_SILICOM, USB_PRODUCT_SILICOM_GPE },
153 	{ USB_VENDOR_SMC, USB_PRODUCT_SMC_2102USB },
154 };
155 #define kue_lookup(v, p) (usb_lookup(kue_devs, v, p))
156 
157 int kue_match(struct device *, void *, void *);
158 void kue_attach(struct device *, struct device *, void *);
159 int kue_detach(struct device *, int);
160 int kue_activate(struct device *, enum devact);
161 
162 struct cfdriver kue_cd = {
163 	NULL, "kue", DV_IFNET
164 };
165 
166 const struct cfattach kue_ca = {
167 	sizeof(struct kue_softc),
168 	kue_match,
169 	kue_attach,
170 	kue_detach,
171 	kue_activate,
172 };
173 
174 int kue_tx_list_init(struct kue_softc *);
175 int kue_rx_list_init(struct kue_softc *);
176 int kue_newbuf(struct kue_softc *, struct kue_chain *,struct mbuf *);
177 int kue_send(struct kue_softc *, struct mbuf *, int);
178 int kue_open_pipes(struct kue_softc *);
179 void kue_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
180 void kue_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
181 void kue_start(struct ifnet *);
182 int kue_ioctl(struct ifnet *, u_long, caddr_t);
183 void kue_init(void *);
184 void kue_stop(struct kue_softc *);
185 void kue_watchdog(struct ifnet *);
186 
187 void kue_setmulti(struct kue_softc *);
188 void kue_reset(struct kue_softc *);
189 
190 usbd_status kue_ctl(struct kue_softc *, int, u_int8_t,
191 			   u_int16_t, void *, u_int32_t);
192 usbd_status kue_setword(struct kue_softc *, u_int8_t, u_int16_t);
193 int kue_load_fw(struct kue_softc *);
194 void kue_attachhook(void *);
195 
196 usbd_status
197 kue_setword(struct kue_softc *sc, u_int8_t breq, u_int16_t word)
198 {
199 	usb_device_request_t	req;
200 
201 	DPRINTFN(10,("%s: %s: enter\n", sc->kue_dev.dv_xname,__func__));
202 
203 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
204 	req.bRequest = breq;
205 	USETW(req.wValue, word);
206 	USETW(req.wIndex, 0);
207 	USETW(req.wLength, 0);
208 
209 	return (usbd_do_request(sc->kue_udev, &req, NULL));
210 }
211 
212 usbd_status
213 kue_ctl(struct kue_softc *sc, int rw, u_int8_t breq, u_int16_t val,
214 	void *data, u_int32_t len)
215 {
216 	usb_device_request_t	req;
217 
218 	DPRINTFN(10,("%s: %s: enter, len=%d\n", sc->kue_dev.dv_xname,
219 		     __func__, len));
220 
221 	if (rw == KUE_CTL_WRITE)
222 		req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
223 	else
224 		req.bmRequestType = UT_READ_VENDOR_DEVICE;
225 
226 	req.bRequest = breq;
227 	USETW(req.wValue, val);
228 	USETW(req.wIndex, 0);
229 	USETW(req.wLength, len);
230 
231 	return (usbd_do_request(sc->kue_udev, &req, data));
232 }
233 
234 int
235 kue_load_fw(struct kue_softc *sc)
236 {
237 	usb_device_descriptor_t dd;
238 	usbd_status		err;
239 	struct kue_firmware	*fw;
240 	u_char			*buf;
241 	size_t			buflen;
242 
243 	DPRINTFN(1,("%s: %s: enter\n", sc->kue_dev.dv_xname, __func__));
244 
245 	/*
246 	 * First, check if we even need to load the firmware.
247 	 * If the device was still attached when the system was
248 	 * rebooted, it may already have firmware loaded in it.
249 	 * If this is the case, we don't need to do it again.
250 	 * And in fact, if we try to load it again, we'll hang,
251 	 * so we have to avoid this condition if we don't want
252 	 * to look stupid.
253 	 *
254 	 * We can test this quickly by checking the bcdRevision
255 	 * code. The NIC will return a different revision code if
256 	 * it's probed while the firmware is still loaded and
257 	 * running.
258 	 */
259 	if (usbd_get_device_desc(sc->kue_udev, &dd))
260 		return (EIO);
261 	if (UGETW(dd.bcdDevice) >= KUE_WARM_REV) {
262 		printf("%s: warm boot, no firmware download\n",
263 		       sc->kue_dev.dv_xname);
264 		return (0);
265 	}
266 
267 	err = loadfirmware("kue", &buf, &buflen);
268 	if (err) {
269 		printf("%s: failed loadfirmware of file %s: errno %d\n",
270 		    sc->kue_dev.dv_xname, "kue", err);
271 		return (err);
272 	}
273 	fw = (struct kue_firmware *)buf;
274 
275 	printf("%s: cold boot, downloading firmware\n",
276 	       sc->kue_dev.dv_xname);
277 
278 	/* Load code segment */
279 	DPRINTFN(1,("%s: kue_load_fw: download code_seg\n",
280 		    sc->kue_dev.dv_xname));
281 	err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
282 	    0, (void *)&fw->data[0], ntohl(fw->codeseglen));
283 	if (err) {
284 		printf("%s: failed to load code segment: %s\n",
285 		    sc->kue_dev.dv_xname, usbd_errstr(err));
286 		free(buf, M_DEVBUF);
287 		return (EIO);
288 	}
289 
290 	/* Load fixup segment */
291 	DPRINTFN(1,("%s: kue_load_fw: download fix_seg\n",
292 		    sc->kue_dev.dv_xname));
293 	err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
294 	    0, (void *)&fw->data[ntohl(fw->codeseglen)], ntohl(fw->fixseglen));
295 	if (err) {
296 		printf("%s: failed to load fixup segment: %s\n",
297 		    sc->kue_dev.dv_xname, usbd_errstr(err));
298 		free(buf, M_DEVBUF);
299 		return (EIO);
300 	}
301 
302 	/* Send trigger command. */
303 	DPRINTFN(1,("%s: kue_load_fw: download trig_seg\n",
304 		    sc->kue_dev.dv_xname));
305 	err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
306 	    0, (void *)&fw->data[ntohl(fw->codeseglen) + ntohl(fw->fixseglen)],
307 	    ntohl(fw->trigseglen));
308 	if (err) {
309 		printf("%s: failed to load trigger segment: %s\n",
310 		    sc->kue_dev.dv_xname, usbd_errstr(err));
311 		free(buf, M_DEVBUF);
312 		return (EIO);
313 	}
314 	free(buf, M_DEVBUF);
315 
316 	usbd_delay_ms(sc->kue_udev, 10);
317 
318 	/*
319 	 * Reload device descriptor.
320 	 * Why? The chip without the firmware loaded returns
321 	 * one revision code. The chip with the firmware
322 	 * loaded and running returns a *different* revision
323 	 * code. This confuses the quirk mechanism, which is
324 	 * dependent on the revision data.
325 	 */
326 	(void)usbd_reload_device_desc(sc->kue_udev);
327 
328 	DPRINTFN(1,("%s: %s: done\n", sc->kue_dev.dv_xname, __func__));
329 
330 	/* Reset the adapter. */
331 	kue_reset(sc);
332 
333 	return (0);
334 }
335 
336 void
337 kue_setmulti(struct kue_softc *sc)
338 {
339 	struct ifnet		*ifp = GET_IFP(sc);
340 	struct ether_multi	*enm;
341 	struct ether_multistep	step;
342 	int			i;
343 
344 	DPRINTFN(5,("%s: %s: enter\n", sc->kue_dev.dv_xname, __func__));
345 
346 	if (ifp->if_flags & IFF_PROMISC) {
347 allmulti:
348 		ifp->if_flags |= IFF_ALLMULTI;
349 		sc->kue_rxfilt |= KUE_RXFILT_ALLMULTI;
350 		sc->kue_rxfilt &= ~KUE_RXFILT_MULTICAST;
351 		kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt);
352 		return;
353 	}
354 
355 	sc->kue_rxfilt &= ~KUE_RXFILT_ALLMULTI;
356 
357 	i = 0;
358 	ETHER_FIRST_MULTI(step, &sc->arpcom, enm);
359 	while (enm != NULL) {
360 		if (i == KUE_MCFILTCNT(sc) ||
361 		    memcmp(enm->enm_addrlo, enm->enm_addrhi,
362 			ETHER_ADDR_LEN) != 0)
363 			goto allmulti;
364 
365 		memcpy(KUE_MCFILT(sc, i), enm->enm_addrlo, ETHER_ADDR_LEN);
366 		ETHER_NEXT_MULTI(step, enm);
367 		i++;
368 	}
369 
370 	ifp->if_flags &= ~IFF_ALLMULTI;
371 
372 	sc->kue_rxfilt |= KUE_RXFILT_MULTICAST;
373 	kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SET_MCAST_FILTERS,
374 	    i, sc->kue_mcfilters, i * ETHER_ADDR_LEN);
375 
376 	kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt);
377 }
378 
379 /*
380  * Issue a SET_CONFIGURATION command to reset the MAC. This should be
381  * done after the firmware is loaded into the adapter in order to
382  * bring it into proper operation.
383  */
384 void
385 kue_reset(struct kue_softc *sc)
386 {
387 	DPRINTFN(5,("%s: %s: enter\n", sc->kue_dev.dv_xname, __func__));
388 
389 	if (usbd_set_config_no(sc->kue_udev, KUE_CONFIG_NO, 1) ||
390 	    usbd_device2interface_handle(sc->kue_udev, KUE_IFACE_IDX,
391 					 &sc->kue_iface))
392 		printf("%s: reset failed\n", sc->kue_dev.dv_xname);
393 
394 	/* Wait a little while for the chip to get its brains in order. */
395 	usbd_delay_ms(sc->kue_udev, 10);
396 }
397 
398 /*
399  * Probe for a KLSI chip.
400  */
401 int
402 kue_match(struct device *parent, void *match, void *aux)
403 {
404 	struct usb_attach_arg	*uaa = aux;
405 
406 	DPRINTFN(25,("kue_match: enter\n"));
407 
408 	if (uaa->iface != NULL)
409 		return (UMATCH_NONE);
410 
411 	return (kue_lookup(uaa->vendor, uaa->product) != NULL ?
412 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
413 }
414 
415 void
416 kue_attachhook(void *xsc)
417 {
418 	struct kue_softc *sc = xsc;
419 	int			s;
420 	struct ifnet		*ifp;
421 	usbd_device_handle	dev = sc->kue_udev;
422 	usbd_interface_handle	iface;
423 	usbd_status		err;
424 	usb_interface_descriptor_t	*id;
425 	usb_endpoint_descriptor_t	*ed;
426 	int			i;
427 
428 	/* Load the firmware into the NIC. */
429 	if (kue_load_fw(sc)) {
430 		printf("%s: loading firmware failed\n",
431 		    sc->kue_dev.dv_xname);
432 		return;
433 	}
434 
435 	err = usbd_device2interface_handle(dev, KUE_IFACE_IDX, &iface);
436 	if (err) {
437 		printf("%s: getting interface handle failed\n",
438 		    sc->kue_dev.dv_xname);
439 		return;
440 	}
441 
442 	sc->kue_iface = iface;
443 	id = usbd_get_interface_descriptor(iface);
444 
445 	/* Find endpoints. */
446 	for (i = 0; i < id->bNumEndpoints; i++) {
447 		ed = usbd_interface2endpoint_descriptor(iface, i);
448 		if (ed == NULL) {
449 			printf("%s: couldn't get ep %d\n",
450 			    sc->kue_dev.dv_xname, i);
451 			return;
452 		}
453 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
454 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
455 			sc->kue_ed[KUE_ENDPT_RX] = ed->bEndpointAddress;
456 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
457 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
458 			sc->kue_ed[KUE_ENDPT_TX] = ed->bEndpointAddress;
459 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
460 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
461 			sc->kue_ed[KUE_ENDPT_INTR] = ed->bEndpointAddress;
462 		}
463 	}
464 
465 	if (sc->kue_ed[KUE_ENDPT_RX] == 0 || sc->kue_ed[KUE_ENDPT_TX] == 0) {
466 		printf("%s: missing endpoint\n", sc->kue_dev.dv_xname);
467 		return;
468 	}
469 
470 	/* Read ethernet descriptor */
471 	err = kue_ctl(sc, KUE_CTL_READ, KUE_CMD_GET_ETHER_DESCRIPTOR,
472 	    0, &sc->kue_desc, sizeof(sc->kue_desc));
473 	if (err) {
474 		printf("%s: could not read Ethernet descriptor\n",
475 		    sc->kue_dev.dv_xname);
476 		return;
477 	}
478 
479 	sc->kue_mcfilters = malloc(KUE_MCFILTCNT(sc) * ETHER_ADDR_LEN,
480 	    M_USBDEV, M_NOWAIT);
481 	if (sc->kue_mcfilters == NULL) {
482 		printf("%s: no memory for multicast filter buffer\n",
483 		    sc->kue_dev.dv_xname);
484 		return;
485 	}
486 
487 	s = splnet();
488 
489 	/*
490 	 * A KLSI chip was detected. Inform the world.
491 	 */
492 	printf("%s: address %s\n", sc->kue_dev.dv_xname,
493 	    ether_sprintf(sc->kue_desc.kue_macaddr));
494 
495 	bcopy(sc->kue_desc.kue_macaddr,
496 	    (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
497 
498 	/* Initialize interface info.*/
499 	ifp = GET_IFP(sc);
500 	ifp->if_softc = sc;
501 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
502 	ifp->if_ioctl = kue_ioctl;
503 	ifp->if_start = kue_start;
504 	ifp->if_watchdog = kue_watchdog;
505 	strlcpy(ifp->if_xname, sc->kue_dev.dv_xname, IFNAMSIZ);
506 
507 	IFQ_SET_READY(&ifp->if_snd);
508 
509 	/* Attach the interface. */
510 	if_attach(ifp);
511 	ether_ifattach(ifp);
512 
513 	sc->kue_attached = 1;
514 	splx(s);
515 
516 }
517 
518 /*
519  * Attach the interface. Allocate softc structures, do
520  * setup and ethernet/BPF attach.
521  */
522 void
523 kue_attach(struct device *parent, struct device *self, void *aux)
524 {
525 	struct kue_softc	*sc = (struct kue_softc *)self;
526 	struct usb_attach_arg	*uaa = aux;
527 	usbd_device_handle	dev = uaa->device;
528 	usbd_status		err;
529 
530 	DPRINTFN(5,(" : kue_attach: sc=%p, dev=%p", sc, dev));
531 
532 	err = usbd_set_config_no(dev, KUE_CONFIG_NO, 1);
533 	if (err) {
534 		printf("%s: setting config no failed\n",
535 		    sc->kue_dev.dv_xname);
536 		return;
537 	}
538 
539 	sc->kue_udev = dev;
540 	sc->kue_product = uaa->product;
541 	sc->kue_vendor = uaa->vendor;
542 
543 	if (rootvp == NULL)
544 		mountroothook_establish(kue_attachhook, sc);
545 	else
546 		kue_attachhook(sc);
547 
548 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->kue_udev,
549 			   &sc->kue_dev);
550 }
551 
552 int
553 kue_detach(struct device *self, int flags)
554 {
555 	struct kue_softc	*sc = (struct kue_softc *)self;
556 	struct ifnet		*ifp = GET_IFP(sc);
557 	int			s;
558 
559 	s = splusb();		/* XXX why? */
560 
561 	if (sc->kue_mcfilters != NULL) {
562 		free(sc->kue_mcfilters, M_USBDEV);
563 		sc->kue_mcfilters = NULL;
564 	}
565 
566 	if (!sc->kue_attached) {
567 		/* Detached before attached finished, so just bail out. */
568 		splx(s);
569 		return (0);
570 	}
571 
572 	if (ifp->if_flags & IFF_RUNNING)
573 		kue_stop(sc);
574 
575 	ether_ifdetach(ifp);
576 
577 	if_detach(ifp);
578 
579 #ifdef DIAGNOSTIC
580 	if (sc->kue_ep[KUE_ENDPT_TX] != NULL ||
581 	    sc->kue_ep[KUE_ENDPT_RX] != NULL ||
582 	    sc->kue_ep[KUE_ENDPT_INTR] != NULL)
583 		printf("%s: detach has active endpoints\n",
584 		       sc->kue_dev.dv_xname);
585 #endif
586 
587 	sc->kue_attached = 0;
588 	splx(s);
589 
590 	return (0);
591 }
592 
593 int
594 kue_activate(struct device *self, enum devact act)
595 {
596 	struct kue_softc *sc = (struct kue_softc *)self;
597 
598 	DPRINTFN(2,("%s: %s: enter\n", sc->kue_dev.dv_xname, __func__));
599 
600 	switch (act) {
601 	case DVACT_ACTIVATE:
602 		break;
603 
604 	case DVACT_DEACTIVATE:
605 		sc->kue_dying = 1;
606 		break;
607 	}
608 	return (0);
609 }
610 
611 /*
612  * Initialize an RX descriptor and attach an MBUF cluster.
613  */
614 int
615 kue_newbuf(struct kue_softc *sc, struct kue_chain *c, struct mbuf *m)
616 {
617 	struct mbuf		*m_new = NULL;
618 
619 	DPRINTFN(10,("%s: %s: enter\n", sc->kue_dev.dv_xname,__func__));
620 
621 	if (m == NULL) {
622 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
623 		if (m_new == NULL) {
624 			printf("%s: no memory for rx list "
625 			    "-- packet dropped!\n", sc->kue_dev.dv_xname);
626 			return (ENOBUFS);
627 		}
628 
629 		MCLGET(m_new, M_DONTWAIT);
630 		if (!(m_new->m_flags & M_EXT)) {
631 			printf("%s: no memory for rx list "
632 			    "-- packet dropped!\n", sc->kue_dev.dv_xname);
633 			m_freem(m_new);
634 			return (ENOBUFS);
635 		}
636 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
637 	} else {
638 		m_new = m;
639 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
640 		m_new->m_data = m_new->m_ext.ext_buf;
641 	}
642 
643 	c->kue_mbuf = m_new;
644 
645 	return (0);
646 }
647 
648 int
649 kue_rx_list_init(struct kue_softc *sc)
650 {
651 	struct kue_cdata	*cd;
652 	struct kue_chain	*c;
653 	int			i;
654 
655 	DPRINTFN(5,("%s: %s: enter\n", sc->kue_dev.dv_xname, __func__));
656 
657 	cd = &sc->kue_cdata;
658 	for (i = 0; i < KUE_RX_LIST_CNT; i++) {
659 		c = &cd->kue_rx_chain[i];
660 		c->kue_sc = sc;
661 		c->kue_idx = i;
662 		if (kue_newbuf(sc, c, NULL) == ENOBUFS)
663 			return (ENOBUFS);
664 		if (c->kue_xfer == NULL) {
665 			c->kue_xfer = usbd_alloc_xfer(sc->kue_udev);
666 			if (c->kue_xfer == NULL)
667 				return (ENOBUFS);
668 			c->kue_buf = usbd_alloc_buffer(c->kue_xfer, KUE_BUFSZ);
669 			if (c->kue_buf == NULL)
670 				return (ENOBUFS); /* XXX free xfer */
671 		}
672 	}
673 
674 	return (0);
675 }
676 
677 int
678 kue_tx_list_init(struct kue_softc *sc)
679 {
680 	struct kue_cdata	*cd;
681 	struct kue_chain	*c;
682 	int			i;
683 
684 	DPRINTFN(5,("%s: %s: enter\n", sc->kue_dev.dv_xname, __func__));
685 
686 	cd = &sc->kue_cdata;
687 	for (i = 0; i < KUE_TX_LIST_CNT; i++) {
688 		c = &cd->kue_tx_chain[i];
689 		c->kue_sc = sc;
690 		c->kue_idx = i;
691 		c->kue_mbuf = NULL;
692 		if (c->kue_xfer == NULL) {
693 			c->kue_xfer = usbd_alloc_xfer(sc->kue_udev);
694 			if (c->kue_xfer == NULL)
695 				return (ENOBUFS);
696 			c->kue_buf = usbd_alloc_buffer(c->kue_xfer, KUE_BUFSZ);
697 			if (c->kue_buf == NULL)
698 				return (ENOBUFS);
699 		}
700 	}
701 
702 	return (0);
703 }
704 
705 /*
706  * A frame has been uploaded: pass the resulting mbuf chain up to
707  * the higher level protocols.
708  */
709 void
710 kue_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
711 {
712 	struct kue_chain	*c = priv;
713 	struct kue_softc	*sc = c->kue_sc;
714 	struct ifnet		*ifp = GET_IFP(sc);
715 	struct mbuf		*m;
716 	int			total_len = 0;
717 	int			s;
718 
719 	DPRINTFN(10,("%s: %s: enter status=%d\n", sc->kue_dev.dv_xname,
720 		     __func__, status));
721 
722 	if (sc->kue_dying)
723 		return;
724 
725 	if (!(ifp->if_flags & IFF_RUNNING))
726 		return;
727 
728 	if (status != USBD_NORMAL_COMPLETION) {
729 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
730 			return;
731 		sc->kue_rx_errs++;
732 		if (usbd_ratecheck(&sc->kue_rx_notice)) {
733 			printf("%s: %u usb errors on rx: %s\n",
734 			    sc->kue_dev.dv_xname, sc->kue_rx_errs,
735 			    usbd_errstr(status));
736 			sc->kue_rx_errs = 0;
737 		}
738 		if (status == USBD_STALLED)
739 			usbd_clear_endpoint_stall_async(sc->kue_ep[KUE_ENDPT_RX]);
740 		goto done;
741 	}
742 
743 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
744 
745 	DPRINTFN(10,("%s: %s: total_len=%d len=%d\n", sc->kue_dev.dv_xname,
746 		     __func__, total_len,
747 		     UGETW(mtod(c->kue_mbuf, u_int8_t *))));
748 
749 	if (total_len <= 1)
750 		goto done;
751 
752 	m = c->kue_mbuf;
753 	/* copy data to mbuf */
754 	memcpy(mtod(m, char *), c->kue_buf, total_len);
755 
756 	/* No errors; receive the packet. */
757 	total_len = UGETW(mtod(m, u_int8_t *));
758 	m_adj(m, sizeof(u_int16_t));
759 
760 	if (total_len < sizeof(struct ether_header)) {
761 		ifp->if_ierrors++;
762 		goto done;
763 	}
764 
765 	ifp->if_ipackets++;
766 	m->m_pkthdr.len = m->m_len = total_len;
767 
768 	m->m_pkthdr.rcvif = ifp;
769 
770 	s = splnet();
771 
772 	/* XXX ugly */
773 	if (kue_newbuf(sc, c, NULL) == ENOBUFS) {
774 		ifp->if_ierrors++;
775 		goto done1;
776 	}
777 
778 #if NBPFILTER > 0
779 	/*
780 	 * Handle BPF listeners. Let the BPF user see the packet, but
781 	 * don't pass it up to the ether_input() layer unless it's
782 	 * a broadcast packet, multicast packet, matches our ethernet
783 	 * address or the interface is in promiscuous mode.
784 	 */
785 	if (ifp->if_bpf)
786 		bpf_mtap(ifp->if_bpf, m, BPF_DIRECTION_IN);
787 #endif
788 
789 	DPRINTFN(10,("%s: %s: deliver %d\n", sc->kue_dev.dv_xname,
790 		    __func__, m->m_len));
791 	ether_input_mbuf(ifp, m);
792  done1:
793 	splx(s);
794 
795  done:
796 
797 	/* Setup new transfer. */
798 	usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_RX],
799 	    c, c->kue_buf, KUE_BUFSZ, USBD_SHORT_XFER_OK | USBD_NO_COPY,
800 	    USBD_NO_TIMEOUT, kue_rxeof);
801 	usbd_transfer(c->kue_xfer);
802 
803 	DPRINTFN(10,("%s: %s: start rx\n", sc->kue_dev.dv_xname,
804 		    __func__));
805 }
806 
807 /*
808  * A frame was downloaded to the chip. It's safe for us to clean up
809  * the list buffers.
810  */
811 
812 void
813 kue_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
814 {
815 	struct kue_chain	*c = priv;
816 	struct kue_softc	*sc = c->kue_sc;
817 	struct ifnet		*ifp = GET_IFP(sc);
818 	int			s;
819 
820 	if (sc->kue_dying)
821 		return;
822 
823 	s = splnet();
824 
825 	DPRINTFN(10,("%s: %s: enter status=%d\n", sc->kue_dev.dv_xname,
826 		    __func__, status));
827 
828 	ifp->if_timer = 0;
829 	ifp->if_flags &= ~IFF_OACTIVE;
830 
831 	if (status != USBD_NORMAL_COMPLETION) {
832 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
833 			splx(s);
834 			return;
835 		}
836 		ifp->if_oerrors++;
837 		printf("%s: usb error on tx: %s\n", sc->kue_dev.dv_xname,
838 		    usbd_errstr(status));
839 		if (status == USBD_STALLED)
840 			usbd_clear_endpoint_stall_async(sc->kue_ep[KUE_ENDPT_TX]);
841 		splx(s);
842 		return;
843 	}
844 
845 	ifp->if_opackets++;
846 
847 	m_freem(c->kue_mbuf);
848 	c->kue_mbuf = NULL;
849 
850 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
851 		kue_start(ifp);
852 
853 	splx(s);
854 }
855 
856 int
857 kue_send(struct kue_softc *sc, struct mbuf *m, int idx)
858 {
859 	int			total_len;
860 	struct kue_chain	*c;
861 	usbd_status		err;
862 
863 	DPRINTFN(10,("%s: %s: enter\n", sc->kue_dev.dv_xname,__func__));
864 
865 	c = &sc->kue_cdata.kue_tx_chain[idx];
866 
867 	/*
868 	 * Copy the mbuf data into a contiguous buffer, leaving two
869 	 * bytes at the beginning to hold the frame length.
870 	 */
871 	m_copydata(m, 0, m->m_pkthdr.len, c->kue_buf + 2);
872 	c->kue_mbuf = m;
873 
874 	total_len = m->m_pkthdr.len + 2;
875 	/* XXX what's this? */
876 	total_len += 64 - (total_len % 64);
877 
878 	/* Frame length is specified in the first 2 bytes of the buffer. */
879 	c->kue_buf[0] = (u_int8_t)m->m_pkthdr.len;
880 	c->kue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8);
881 
882 	usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_TX],
883 	    c, c->kue_buf, total_len, USBD_NO_COPY, USBD_DEFAULT_TIMEOUT,
884 	    kue_txeof);
885 
886 	/* Transmit */
887 	err = usbd_transfer(c->kue_xfer);
888 	if (err != USBD_IN_PROGRESS) {
889 		printf("%s: kue_send error=%s\n", sc->kue_dev.dv_xname,
890 		       usbd_errstr(err));
891 		kue_stop(sc);
892 		return (EIO);
893 	}
894 
895 	sc->kue_cdata.kue_tx_cnt++;
896 
897 	return (0);
898 }
899 
900 void
901 kue_start(struct ifnet *ifp)
902 {
903 	struct kue_softc	*sc = ifp->if_softc;
904 	struct mbuf		*m_head = NULL;
905 
906 	DPRINTFN(10,("%s: %s: enter\n", sc->kue_dev.dv_xname,__func__));
907 
908 	if (sc->kue_dying)
909 		return;
910 
911 	if (ifp->if_flags & IFF_OACTIVE)
912 		return;
913 
914 	IFQ_POLL(&ifp->if_snd, m_head);
915 	if (m_head == NULL)
916 		return;
917 
918 	if (kue_send(sc, m_head, 0)) {
919 		ifp->if_flags |= IFF_OACTIVE;
920 		return;
921 	}
922 
923 	IFQ_DEQUEUE(&ifp->if_snd, m_head);
924 
925 #if NBPFILTER > 0
926 	/*
927 	 * If there's a BPF listener, bounce a copy of this frame
928 	 * to him.
929 	 */
930 	if (ifp->if_bpf)
931 		bpf_mtap(ifp->if_bpf, m_head, BPF_DIRECTION_OUT);
932 #endif
933 
934 	ifp->if_flags |= IFF_OACTIVE;
935 
936 	/*
937 	 * Set a timeout in case the chip goes out to lunch.
938 	 */
939 	ifp->if_timer = 6;
940 }
941 
942 void
943 kue_init(void *xsc)
944 {
945 	struct kue_softc	*sc = xsc;
946 	struct ifnet		*ifp = GET_IFP(sc);
947 	int			s;
948 	u_char			*eaddr;
949 
950 	DPRINTFN(5,("%s: %s: enter\n", sc->kue_dev.dv_xname,__func__));
951 
952 	if (ifp->if_flags & IFF_RUNNING)
953 		return;
954 
955 	s = splnet();
956 
957 	eaddr = sc->arpcom.ac_enaddr;
958 	/* Set MAC address */
959 	kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SET_MAC, 0, eaddr, ETHER_ADDR_LEN);
960 
961 	sc->kue_rxfilt = KUE_RXFILT_UNICAST | KUE_RXFILT_BROADCAST;
962 
963 	 /* If we want promiscuous mode, set the allframes bit. */
964 	if (ifp->if_flags & IFF_PROMISC)
965 		sc->kue_rxfilt |= KUE_RXFILT_PROMISC;
966 
967 	kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt);
968 
969 	/* I'm not sure how to tune these. */
970 #if 0
971 	/*
972 	 * Leave this one alone for now; setting it
973 	 * wrong causes lockups on some machines/controllers.
974 	 */
975 	kue_setword(sc, KUE_CMD_SET_SOFS, 1);
976 #endif
977 	kue_setword(sc, KUE_CMD_SET_URB_SIZE, 64);
978 
979 	/* Init TX ring. */
980 	if (kue_tx_list_init(sc) == ENOBUFS) {
981 		printf("%s: tx list init failed\n", sc->kue_dev.dv_xname);
982 		splx(s);
983 		return;
984 	}
985 
986 	/* Init RX ring. */
987 	if (kue_rx_list_init(sc) == ENOBUFS) {
988 		printf("%s: rx list init failed\n", sc->kue_dev.dv_xname);
989 		splx(s);
990 		return;
991 	}
992 
993 	/* Load the multicast filter. */
994 	kue_setmulti(sc);
995 
996 	if (sc->kue_ep[KUE_ENDPT_RX] == NULL) {
997 		if (kue_open_pipes(sc)) {
998 			splx(s);
999 			return;
1000 		}
1001 	}
1002 
1003 	ifp->if_flags |= IFF_RUNNING;
1004 	ifp->if_flags &= ~IFF_OACTIVE;
1005 
1006 	splx(s);
1007 }
1008 
1009 int
1010 kue_open_pipes(struct kue_softc *sc)
1011 {
1012 	usbd_status		err;
1013 	struct kue_chain	*c;
1014 	int			i;
1015 
1016 	DPRINTFN(5,("%s: %s: enter\n", sc->kue_dev.dv_xname,__func__));
1017 
1018 	/* Open RX and TX pipes. */
1019 	err = usbd_open_pipe(sc->kue_iface, sc->kue_ed[KUE_ENDPT_RX],
1020 	    USBD_EXCLUSIVE_USE, &sc->kue_ep[KUE_ENDPT_RX]);
1021 	if (err) {
1022 		printf("%s: open rx pipe failed: %s\n",
1023 		    sc->kue_dev.dv_xname, usbd_errstr(err));
1024 		return (EIO);
1025 	}
1026 
1027 	err = usbd_open_pipe(sc->kue_iface, sc->kue_ed[KUE_ENDPT_TX],
1028 	    USBD_EXCLUSIVE_USE, &sc->kue_ep[KUE_ENDPT_TX]);
1029 	if (err) {
1030 		printf("%s: open tx pipe failed: %s\n",
1031 		    sc->kue_dev.dv_xname, usbd_errstr(err));
1032 		return (EIO);
1033 	}
1034 
1035 	/* Start up the receive pipe. */
1036 	for (i = 0; i < KUE_RX_LIST_CNT; i++) {
1037 		c = &sc->kue_cdata.kue_rx_chain[i];
1038 		usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_RX],
1039 		    c, c->kue_buf, KUE_BUFSZ,
1040 		    USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT,
1041 		    kue_rxeof);
1042 		DPRINTFN(5,("%s: %s: start read\n", sc->kue_dev.dv_xname,
1043 			    __func__));
1044 		usbd_transfer(c->kue_xfer);
1045 	}
1046 
1047 	return (0);
1048 }
1049 
1050 int
1051 kue_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
1052 {
1053 	struct kue_softc	*sc = ifp->if_softc;
1054 	struct ifaddr 		*ifa = (struct ifaddr *)data;
1055 	int			s, error = 0;
1056 
1057 	DPRINTFN(5,("%s: %s: enter\n", sc->kue_dev.dv_xname,__func__));
1058 
1059 	if (sc->kue_dying)
1060 		return (EIO);
1061 
1062 #ifdef DIAGNOSTIC
1063 	if (!curproc) {
1064 		printf("%s: no proc!!\n", sc->kue_dev.dv_xname);
1065 		return EIO;
1066 	}
1067 #endif
1068 
1069 	s = splnet();
1070 
1071 	switch(command) {
1072 	case SIOCSIFADDR:
1073 		ifp->if_flags |= IFF_UP;
1074 		kue_init(sc);
1075 
1076 		switch (ifa->ifa_addr->sa_family) {
1077 #ifdef INET
1078 		case AF_INET:
1079 			arp_ifinit(&sc->arpcom, ifa);
1080 			break;
1081 #endif /* INET */
1082 		}
1083 		break;
1084 
1085 	case SIOCSIFFLAGS:
1086 		if (ifp->if_flags & IFF_UP) {
1087 			if (ifp->if_flags & IFF_RUNNING &&
1088 			    ifp->if_flags & IFF_PROMISC &&
1089 			    !(sc->kue_if_flags & IFF_PROMISC)) {
1090 				sc->kue_rxfilt |= KUE_RXFILT_PROMISC;
1091 				kue_setword(sc, KUE_CMD_SET_PKT_FILTER,
1092 				    sc->kue_rxfilt);
1093 			} else if (ifp->if_flags & IFF_RUNNING &&
1094 			    !(ifp->if_flags & IFF_PROMISC) &&
1095 			    sc->kue_if_flags & IFF_PROMISC) {
1096 				sc->kue_rxfilt &= ~KUE_RXFILT_PROMISC;
1097 				kue_setword(sc, KUE_CMD_SET_PKT_FILTER,
1098 				    sc->kue_rxfilt);
1099 			} else if (!(ifp->if_flags & IFF_RUNNING))
1100 				kue_init(sc);
1101 		} else {
1102 			if (ifp->if_flags & IFF_RUNNING)
1103 				kue_stop(sc);
1104 		}
1105 		sc->kue_if_flags = ifp->if_flags;
1106 		error = 0;
1107 		break;
1108 
1109 	default:
1110 		error = ether_ioctl(ifp, &sc->arpcom, command, data);
1111 	}
1112 
1113 	if (error == ENETRESET) {
1114 		if (ifp->if_flags & IFF_RUNNING)
1115 			kue_setmulti(sc);
1116 		error = 0;
1117 	}
1118 
1119 	splx(s);
1120 	return (error);
1121 }
1122 
1123 void
1124 kue_watchdog(struct ifnet *ifp)
1125 {
1126 	struct kue_softc	*sc = ifp->if_softc;
1127 	struct kue_chain	*c;
1128 	usbd_status		stat;
1129 	int			s;
1130 
1131 	DPRINTFN(5,("%s: %s: enter\n", sc->kue_dev.dv_xname,__func__));
1132 
1133 	if (sc->kue_dying)
1134 		return;
1135 
1136 	ifp->if_oerrors++;
1137 	printf("%s: watchdog timeout\n", sc->kue_dev.dv_xname);
1138 
1139 	s = splusb();
1140 	c = &sc->kue_cdata.kue_tx_chain[0];
1141 	usbd_get_xfer_status(c->kue_xfer, NULL, NULL, NULL, &stat);
1142 	kue_txeof(c->kue_xfer, c, stat);
1143 
1144 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1145 		kue_start(ifp);
1146 	splx(s);
1147 }
1148 
1149 /*
1150  * Stop the adapter and free any mbufs allocated to the
1151  * RX and TX lists.
1152  */
1153 void
1154 kue_stop(struct kue_softc *sc)
1155 {
1156 	usbd_status		err;
1157 	struct ifnet		*ifp;
1158 	int			i;
1159 
1160 	DPRINTFN(5,("%s: %s: enter\n", sc->kue_dev.dv_xname,__func__));
1161 
1162 	ifp = GET_IFP(sc);
1163 	ifp->if_timer = 0;
1164 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1165 
1166 	/* Stop transfers. */
1167 	if (sc->kue_ep[KUE_ENDPT_RX] != NULL) {
1168 		err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_RX]);
1169 		if (err) {
1170 			printf("%s: abort rx pipe failed: %s\n",
1171 			    sc->kue_dev.dv_xname, usbd_errstr(err));
1172 		}
1173 		err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_RX]);
1174 		if (err) {
1175 			printf("%s: close rx pipe failed: %s\n",
1176 			    sc->kue_dev.dv_xname, usbd_errstr(err));
1177 		}
1178 		sc->kue_ep[KUE_ENDPT_RX] = NULL;
1179 	}
1180 
1181 	if (sc->kue_ep[KUE_ENDPT_TX] != NULL) {
1182 		err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_TX]);
1183 		if (err) {
1184 			printf("%s: abort tx pipe failed: %s\n",
1185 			    sc->kue_dev.dv_xname, usbd_errstr(err));
1186 		}
1187 		err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_TX]);
1188 		if (err) {
1189 			printf("%s: close tx pipe failed: %s\n",
1190 			    sc->kue_dev.dv_xname, usbd_errstr(err));
1191 		}
1192 		sc->kue_ep[KUE_ENDPT_TX] = NULL;
1193 	}
1194 
1195 	if (sc->kue_ep[KUE_ENDPT_INTR] != NULL) {
1196 		err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_INTR]);
1197 		if (err) {
1198 			printf("%s: abort intr pipe failed: %s\n",
1199 			    sc->kue_dev.dv_xname, usbd_errstr(err));
1200 		}
1201 		err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_INTR]);
1202 		if (err) {
1203 			printf("%s: close intr pipe failed: %s\n",
1204 			    sc->kue_dev.dv_xname, usbd_errstr(err));
1205 		}
1206 		sc->kue_ep[KUE_ENDPT_INTR] = NULL;
1207 	}
1208 
1209 	/* Free RX resources. */
1210 	for (i = 0; i < KUE_RX_LIST_CNT; i++) {
1211 		if (sc->kue_cdata.kue_rx_chain[i].kue_mbuf != NULL) {
1212 			m_freem(sc->kue_cdata.kue_rx_chain[i].kue_mbuf);
1213 			sc->kue_cdata.kue_rx_chain[i].kue_mbuf = NULL;
1214 		}
1215 		if (sc->kue_cdata.kue_rx_chain[i].kue_xfer != NULL) {
1216 			usbd_free_xfer(sc->kue_cdata.kue_rx_chain[i].kue_xfer);
1217 			sc->kue_cdata.kue_rx_chain[i].kue_xfer = NULL;
1218 		}
1219 	}
1220 
1221 	/* Free TX resources. */
1222 	for (i = 0; i < KUE_TX_LIST_CNT; i++) {
1223 		if (sc->kue_cdata.kue_tx_chain[i].kue_mbuf != NULL) {
1224 			m_freem(sc->kue_cdata.kue_tx_chain[i].kue_mbuf);
1225 			sc->kue_cdata.kue_tx_chain[i].kue_mbuf = NULL;
1226 		}
1227 		if (sc->kue_cdata.kue_tx_chain[i].kue_xfer != NULL) {
1228 			usbd_free_xfer(sc->kue_cdata.kue_tx_chain[i].kue_xfer);
1229 			sc->kue_cdata.kue_tx_chain[i].kue_xfer = NULL;
1230 		}
1231 	}
1232 }
1233