xref: /openbsd-src/sys/dev/usb/if_kue.c (revision d13be5d47e4149db2549a9828e244d59dbc43f15)
1 /*	$OpenBSD: if_kue.c,v 1.64 2011/07/03 15:47:17 matthew 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 
156 int kue_match(struct device *, void *, void *);
157 void kue_attach(struct device *, struct device *, void *);
158 int kue_detach(struct device *, int);
159 int kue_activate(struct device *, int);
160 
161 struct cfdriver kue_cd = {
162 	NULL, "kue", DV_IFNET
163 };
164 
165 const struct cfattach kue_ca = {
166 	sizeof(struct kue_softc),
167 	kue_match,
168 	kue_attach,
169 	kue_detach,
170 	kue_activate,
171 };
172 
173 int kue_tx_list_init(struct kue_softc *);
174 int kue_rx_list_init(struct kue_softc *);
175 int kue_newbuf(struct kue_softc *, struct kue_chain *,struct mbuf *);
176 int kue_send(struct kue_softc *, struct mbuf *, int);
177 int kue_open_pipes(struct kue_softc *);
178 void kue_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
179 void kue_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status);
180 void kue_start(struct ifnet *);
181 int kue_ioctl(struct ifnet *, u_long, caddr_t);
182 void kue_init(void *);
183 void kue_stop(struct kue_softc *);
184 void kue_watchdog(struct ifnet *);
185 
186 void kue_setmulti(struct kue_softc *);
187 void kue_reset(struct kue_softc *);
188 
189 usbd_status kue_ctl(struct kue_softc *, int, u_int8_t,
190 			   u_int16_t, void *, u_int32_t);
191 usbd_status kue_setword(struct kue_softc *, u_int8_t, u_int16_t);
192 int kue_load_fw(struct kue_softc *);
193 void kue_attachhook(void *);
194 
195 usbd_status
196 kue_setword(struct kue_softc *sc, u_int8_t breq, u_int16_t word)
197 {
198 	usb_device_request_t	req;
199 
200 	DPRINTFN(10,("%s: %s: enter\n", sc->kue_dev.dv_xname,__func__));
201 
202 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
203 	req.bRequest = breq;
204 	USETW(req.wValue, word);
205 	USETW(req.wIndex, 0);
206 	USETW(req.wLength, 0);
207 
208 	return (usbd_do_request(sc->kue_udev, &req, NULL));
209 }
210 
211 usbd_status
212 kue_ctl(struct kue_softc *sc, int rw, u_int8_t breq, u_int16_t val,
213 	void *data, u_int32_t len)
214 {
215 	usb_device_request_t	req;
216 
217 	DPRINTFN(10,("%s: %s: enter, len=%d\n", sc->kue_dev.dv_xname,
218 		     __func__, len));
219 
220 	if (rw == KUE_CTL_WRITE)
221 		req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
222 	else
223 		req.bmRequestType = UT_READ_VENDOR_DEVICE;
224 
225 	req.bRequest = breq;
226 	USETW(req.wValue, val);
227 	USETW(req.wIndex, 0);
228 	USETW(req.wLength, len);
229 
230 	return (usbd_do_request(sc->kue_udev, &req, data));
231 }
232 
233 int
234 kue_load_fw(struct kue_softc *sc)
235 {
236 	usb_device_descriptor_t dd;
237 	usbd_status		err;
238 	struct kue_firmware	*fw;
239 	u_char			*buf;
240 	size_t			buflen;
241 
242 	DPRINTFN(1,("%s: %s: enter\n", sc->kue_dev.dv_xname, __func__));
243 
244 	/*
245 	 * First, check if we even need to load the firmware.
246 	 * If the device was still attached when the system was
247 	 * rebooted, it may already have firmware loaded in it.
248 	 * If this is the case, we don't need to do it again.
249 	 * And in fact, if we try to load it again, we'll hang,
250 	 * so we have to avoid this condition if we don't want
251 	 * to look stupid.
252 	 *
253 	 * We can test this quickly by checking the bcdRevision
254 	 * code. The NIC will return a different revision code if
255 	 * it's probed while the firmware is still loaded and
256 	 * running.
257 	 */
258 	if (usbd_get_device_desc(sc->kue_udev, &dd))
259 		return (EIO);
260 	if (UGETW(dd.bcdDevice) >= KUE_WARM_REV) {
261 		printf("%s: warm boot, no firmware download\n",
262 		       sc->kue_dev.dv_xname);
263 		return (0);
264 	}
265 
266 	err = loadfirmware("kue", &buf, &buflen);
267 	if (err) {
268 		printf("%s: failed loadfirmware of file %s: errno %d\n",
269 		    sc->kue_dev.dv_xname, "kue", err);
270 		return (err);
271 	}
272 	fw = (struct kue_firmware *)buf;
273 
274 	printf("%s: cold boot, downloading firmware\n",
275 	       sc->kue_dev.dv_xname);
276 
277 	/* Load code segment */
278 	DPRINTFN(1,("%s: kue_load_fw: download code_seg\n",
279 		    sc->kue_dev.dv_xname));
280 	err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
281 	    0, (void *)&fw->data[0], ntohl(fw->codeseglen));
282 	if (err) {
283 		printf("%s: failed to load code segment: %s\n",
284 		    sc->kue_dev.dv_xname, usbd_errstr(err));
285 		free(buf, M_DEVBUF);
286 		return (EIO);
287 	}
288 
289 	/* Load fixup segment */
290 	DPRINTFN(1,("%s: kue_load_fw: download fix_seg\n",
291 		    sc->kue_dev.dv_xname));
292 	err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
293 	    0, (void *)&fw->data[ntohl(fw->codeseglen)], ntohl(fw->fixseglen));
294 	if (err) {
295 		printf("%s: failed to load fixup segment: %s\n",
296 		    sc->kue_dev.dv_xname, usbd_errstr(err));
297 		free(buf, M_DEVBUF);
298 		return (EIO);
299 	}
300 
301 	/* Send trigger command. */
302 	DPRINTFN(1,("%s: kue_load_fw: download trig_seg\n",
303 		    sc->kue_dev.dv_xname));
304 	err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
305 	    0, (void *)&fw->data[ntohl(fw->codeseglen) + ntohl(fw->fixseglen)],
306 	    ntohl(fw->trigseglen));
307 	if (err) {
308 		printf("%s: failed to load trigger segment: %s\n",
309 		    sc->kue_dev.dv_xname, usbd_errstr(err));
310 		free(buf, M_DEVBUF);
311 		return (EIO);
312 	}
313 	free(buf, M_DEVBUF);
314 
315 	usbd_delay_ms(sc->kue_udev, 10);
316 
317 	/*
318 	 * Reload device descriptor.
319 	 * Why? The chip without the firmware loaded returns
320 	 * one revision code. The chip with the firmware
321 	 * loaded and running returns a *different* revision
322 	 * code. This confuses the quirk mechanism, which is
323 	 * dependent on the revision data.
324 	 */
325 	(void)usbd_reload_device_desc(sc->kue_udev);
326 
327 	DPRINTFN(1,("%s: %s: done\n", sc->kue_dev.dv_xname, __func__));
328 
329 	/* Reset the adapter. */
330 	kue_reset(sc);
331 
332 	return (0);
333 }
334 
335 void
336 kue_setmulti(struct kue_softc *sc)
337 {
338 	struct ifnet		*ifp = GET_IFP(sc);
339 	struct ether_multi	*enm;
340 	struct ether_multistep	step;
341 	int			i;
342 
343 	DPRINTFN(5,("%s: %s: enter\n", sc->kue_dev.dv_xname, __func__));
344 
345 	if (ifp->if_flags & IFF_PROMISC) {
346 allmulti:
347 		ifp->if_flags |= IFF_ALLMULTI;
348 		sc->kue_rxfilt |= KUE_RXFILT_ALLMULTI;
349 		sc->kue_rxfilt &= ~KUE_RXFILT_MULTICAST;
350 		kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt);
351 		return;
352 	}
353 
354 	sc->kue_rxfilt &= ~KUE_RXFILT_ALLMULTI;
355 
356 	i = 0;
357 	ETHER_FIRST_MULTI(step, &sc->arpcom, enm);
358 	while (enm != NULL) {
359 		if (i == KUE_MCFILTCNT(sc) ||
360 		    memcmp(enm->enm_addrlo, enm->enm_addrhi,
361 			ETHER_ADDR_LEN) != 0)
362 			goto allmulti;
363 
364 		memcpy(KUE_MCFILT(sc, i), enm->enm_addrlo, ETHER_ADDR_LEN);
365 		ETHER_NEXT_MULTI(step, enm);
366 		i++;
367 	}
368 
369 	ifp->if_flags &= ~IFF_ALLMULTI;
370 
371 	sc->kue_rxfilt |= KUE_RXFILT_MULTICAST;
372 	kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SET_MCAST_FILTERS,
373 	    i, sc->kue_mcfilters, i * ETHER_ADDR_LEN);
374 
375 	kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt);
376 }
377 
378 /*
379  * Issue a SET_CONFIGURATION command to reset the MAC. This should be
380  * done after the firmware is loaded into the adapter in order to
381  * bring it into proper operation.
382  */
383 void
384 kue_reset(struct kue_softc *sc)
385 {
386 	DPRINTFN(5,("%s: %s: enter\n", sc->kue_dev.dv_xname, __func__));
387 
388 	if (usbd_set_config_no(sc->kue_udev, KUE_CONFIG_NO, 1) ||
389 	    usbd_device2interface_handle(sc->kue_udev, KUE_IFACE_IDX,
390 					 &sc->kue_iface))
391 		printf("%s: reset failed\n", sc->kue_dev.dv_xname);
392 
393 	/* Wait a little while for the chip to get its brains in order. */
394 	usbd_delay_ms(sc->kue_udev, 10);
395 }
396 
397 /*
398  * Probe for a KLSI chip.
399  */
400 int
401 kue_match(struct device *parent, void *match, void *aux)
402 {
403 	struct usb_attach_arg	*uaa = aux;
404 
405 	DPRINTFN(25,("kue_match: enter\n"));
406 
407 	if (uaa->iface != NULL)
408 		return (UMATCH_NONE);
409 
410 	return (usb_lookup(kue_devs, uaa->vendor, uaa->product) != NULL ?
411 	    UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
412 }
413 
414 void
415 kue_attachhook(void *xsc)
416 {
417 	struct kue_softc *sc = xsc;
418 	int			s;
419 	struct ifnet		*ifp;
420 	usbd_device_handle	dev = sc->kue_udev;
421 	usbd_interface_handle	iface;
422 	usbd_status		err;
423 	usb_interface_descriptor_t	*id;
424 	usb_endpoint_descriptor_t	*ed;
425 	int			i;
426 
427 	/* Load the firmware into the NIC. */
428 	if (kue_load_fw(sc)) {
429 		printf("%s: loading firmware failed\n",
430 		    sc->kue_dev.dv_xname);
431 		return;
432 	}
433 
434 	err = usbd_device2interface_handle(dev, KUE_IFACE_IDX, &iface);
435 	if (err) {
436 		printf("%s: getting interface handle failed\n",
437 		    sc->kue_dev.dv_xname);
438 		return;
439 	}
440 
441 	sc->kue_iface = iface;
442 	id = usbd_get_interface_descriptor(iface);
443 
444 	/* Find endpoints. */
445 	for (i = 0; i < id->bNumEndpoints; i++) {
446 		ed = usbd_interface2endpoint_descriptor(iface, i);
447 		if (ed == NULL) {
448 			printf("%s: couldn't get ep %d\n",
449 			    sc->kue_dev.dv_xname, i);
450 			return;
451 		}
452 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
453 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
454 			sc->kue_ed[KUE_ENDPT_RX] = ed->bEndpointAddress;
455 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
456 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
457 			sc->kue_ed[KUE_ENDPT_TX] = ed->bEndpointAddress;
458 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
459 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
460 			sc->kue_ed[KUE_ENDPT_INTR] = ed->bEndpointAddress;
461 		}
462 	}
463 
464 	if (sc->kue_ed[KUE_ENDPT_RX] == 0 || sc->kue_ed[KUE_ENDPT_TX] == 0) {
465 		printf("%s: missing endpoint\n", sc->kue_dev.dv_xname);
466 		return;
467 	}
468 
469 	/* Read ethernet descriptor */
470 	err = kue_ctl(sc, KUE_CTL_READ, KUE_CMD_GET_ETHER_DESCRIPTOR,
471 	    0, &sc->kue_desc, sizeof(sc->kue_desc));
472 	if (err) {
473 		printf("%s: could not read Ethernet descriptor\n",
474 		    sc->kue_dev.dv_xname);
475 		return;
476 	}
477 
478 	sc->kue_mcfilters = malloc(KUE_MCFILTCNT(sc) * ETHER_ADDR_LEN,
479 	    M_USBDEV, M_NOWAIT);
480 	if (sc->kue_mcfilters == NULL) {
481 		printf("%s: no memory for multicast filter buffer\n",
482 		    sc->kue_dev.dv_xname);
483 		return;
484 	}
485 
486 	s = splnet();
487 
488 	/*
489 	 * A KLSI chip was detected. Inform the world.
490 	 */
491 	printf("%s: address %s\n", sc->kue_dev.dv_xname,
492 	    ether_sprintf(sc->kue_desc.kue_macaddr));
493 
494 	bcopy(sc->kue_desc.kue_macaddr,
495 	    (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
496 
497 	/* Initialize interface info.*/
498 	ifp = GET_IFP(sc);
499 	ifp->if_softc = sc;
500 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
501 	ifp->if_ioctl = kue_ioctl;
502 	ifp->if_start = kue_start;
503 	ifp->if_watchdog = kue_watchdog;
504 	strlcpy(ifp->if_xname, sc->kue_dev.dv_xname, IFNAMSIZ);
505 
506 	IFQ_SET_READY(&ifp->if_snd);
507 
508 	/* Attach the interface. */
509 	if_attach(ifp);
510 	ether_ifattach(ifp);
511 
512 	sc->kue_attached = 1;
513 	splx(s);
514 
515 }
516 
517 /*
518  * Attach the interface. Allocate softc structures, do
519  * setup and ethernet/BPF attach.
520  */
521 void
522 kue_attach(struct device *parent, struct device *self, void *aux)
523 {
524 	struct kue_softc	*sc = (struct kue_softc *)self;
525 	struct usb_attach_arg	*uaa = aux;
526 	usbd_device_handle	dev = uaa->device;
527 	usbd_status		err;
528 
529 	DPRINTFN(5,(" : kue_attach: sc=%p, dev=%p", sc, dev));
530 
531 	err = usbd_set_config_no(dev, KUE_CONFIG_NO, 1);
532 	if (err) {
533 		printf("%s: setting config no failed\n",
534 		    sc->kue_dev.dv_xname);
535 		return;
536 	}
537 
538 	sc->kue_udev = dev;
539 	sc->kue_product = uaa->product;
540 	sc->kue_vendor = uaa->vendor;
541 
542 	if (rootvp == NULL)
543 		mountroothook_establish(kue_attachhook, sc);
544 	else
545 		kue_attachhook(sc);
546 }
547 
548 int
549 kue_detach(struct device *self, int flags)
550 {
551 	struct kue_softc	*sc = (struct kue_softc *)self;
552 	struct ifnet		*ifp = GET_IFP(sc);
553 	int			s;
554 
555 	/* Detached before attached finished, so just bail out. */
556 	if (!sc->kue_attached)
557 		return (0);
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 (ifp->if_flags & IFF_RUNNING)
567 		kue_stop(sc);
568 
569 	if (ifp->if_softc != NULL) {
570 		ether_ifdetach(ifp);
571 		if_detach(ifp);
572 	}
573 
574 #ifdef DIAGNOSTIC
575 	if (sc->kue_ep[KUE_ENDPT_TX] != NULL ||
576 	    sc->kue_ep[KUE_ENDPT_RX] != NULL ||
577 	    sc->kue_ep[KUE_ENDPT_INTR] != NULL)
578 		printf("%s: detach has active endpoints\n",
579 		       sc->kue_dev.dv_xname);
580 #endif
581 
582 	sc->kue_attached = 0;
583 	splx(s);
584 
585 	return (0);
586 }
587 
588 int
589 kue_activate(struct device *self, int act)
590 {
591 	struct kue_softc *sc = (struct kue_softc *)self;
592 
593 	DPRINTFN(2,("%s: %s: enter\n", sc->kue_dev.dv_xname, __func__));
594 
595 	switch (act) {
596 	case DVACT_DEACTIVATE:
597 		sc->kue_dying = 1;
598 		break;
599 	}
600 	return (0);
601 }
602 
603 /*
604  * Initialize an RX descriptor and attach an MBUF cluster.
605  */
606 int
607 kue_newbuf(struct kue_softc *sc, struct kue_chain *c, struct mbuf *m)
608 {
609 	struct mbuf		*m_new = NULL;
610 
611 	DPRINTFN(10,("%s: %s: enter\n", sc->kue_dev.dv_xname,__func__));
612 
613 	if (m == NULL) {
614 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
615 		if (m_new == NULL) {
616 			printf("%s: no memory for rx list "
617 			    "-- packet dropped!\n", sc->kue_dev.dv_xname);
618 			return (ENOBUFS);
619 		}
620 
621 		MCLGET(m_new, M_DONTWAIT);
622 		if (!(m_new->m_flags & M_EXT)) {
623 			printf("%s: no memory for rx list "
624 			    "-- packet dropped!\n", sc->kue_dev.dv_xname);
625 			m_freem(m_new);
626 			return (ENOBUFS);
627 		}
628 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
629 	} else {
630 		m_new = m;
631 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
632 		m_new->m_data = m_new->m_ext.ext_buf;
633 	}
634 
635 	c->kue_mbuf = m_new;
636 
637 	return (0);
638 }
639 
640 int
641 kue_rx_list_init(struct kue_softc *sc)
642 {
643 	struct kue_cdata	*cd;
644 	struct kue_chain	*c;
645 	int			i;
646 
647 	DPRINTFN(5,("%s: %s: enter\n", sc->kue_dev.dv_xname, __func__));
648 
649 	cd = &sc->kue_cdata;
650 	for (i = 0; i < KUE_RX_LIST_CNT; i++) {
651 		c = &cd->kue_rx_chain[i];
652 		c->kue_sc = sc;
653 		c->kue_idx = i;
654 		if (kue_newbuf(sc, c, NULL) == ENOBUFS)
655 			return (ENOBUFS);
656 		if (c->kue_xfer == NULL) {
657 			c->kue_xfer = usbd_alloc_xfer(sc->kue_udev);
658 			if (c->kue_xfer == NULL)
659 				return (ENOBUFS);
660 			c->kue_buf = usbd_alloc_buffer(c->kue_xfer, KUE_BUFSZ);
661 			if (c->kue_buf == NULL)
662 				return (ENOBUFS); /* XXX free xfer */
663 		}
664 	}
665 
666 	return (0);
667 }
668 
669 int
670 kue_tx_list_init(struct kue_softc *sc)
671 {
672 	struct kue_cdata	*cd;
673 	struct kue_chain	*c;
674 	int			i;
675 
676 	DPRINTFN(5,("%s: %s: enter\n", sc->kue_dev.dv_xname, __func__));
677 
678 	cd = &sc->kue_cdata;
679 	for (i = 0; i < KUE_TX_LIST_CNT; i++) {
680 		c = &cd->kue_tx_chain[i];
681 		c->kue_sc = sc;
682 		c->kue_idx = i;
683 		c->kue_mbuf = NULL;
684 		if (c->kue_xfer == NULL) {
685 			c->kue_xfer = usbd_alloc_xfer(sc->kue_udev);
686 			if (c->kue_xfer == NULL)
687 				return (ENOBUFS);
688 			c->kue_buf = usbd_alloc_buffer(c->kue_xfer, KUE_BUFSZ);
689 			if (c->kue_buf == NULL)
690 				return (ENOBUFS);
691 		}
692 	}
693 
694 	return (0);
695 }
696 
697 /*
698  * A frame has been uploaded: pass the resulting mbuf chain up to
699  * the higher level protocols.
700  */
701 void
702 kue_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
703 {
704 	struct kue_chain	*c = priv;
705 	struct kue_softc	*sc = c->kue_sc;
706 	struct ifnet		*ifp = GET_IFP(sc);
707 	struct mbuf		*m;
708 	int			total_len = 0;
709 	int			s;
710 
711 	DPRINTFN(10,("%s: %s: enter status=%d\n", sc->kue_dev.dv_xname,
712 		     __func__, status));
713 
714 	if (sc->kue_dying)
715 		return;
716 
717 	if (!(ifp->if_flags & IFF_RUNNING))
718 		return;
719 
720 	if (status != USBD_NORMAL_COMPLETION) {
721 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
722 			return;
723 		sc->kue_rx_errs++;
724 		if (usbd_ratecheck(&sc->kue_rx_notice)) {
725 			printf("%s: %u usb errors on rx: %s\n",
726 			    sc->kue_dev.dv_xname, sc->kue_rx_errs,
727 			    usbd_errstr(status));
728 			sc->kue_rx_errs = 0;
729 		}
730 		if (status == USBD_STALLED)
731 			usbd_clear_endpoint_stall_async(sc->kue_ep[KUE_ENDPT_RX]);
732 		goto done;
733 	}
734 
735 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
736 
737 	DPRINTFN(10,("%s: %s: total_len=%d len=%d\n", sc->kue_dev.dv_xname,
738 		     __func__, total_len,
739 		     UGETW(mtod(c->kue_mbuf, u_int8_t *))));
740 
741 	if (total_len <= 1)
742 		goto done;
743 
744 	m = c->kue_mbuf;
745 	/* copy data to mbuf */
746 	memcpy(mtod(m, char *), c->kue_buf, total_len);
747 
748 	/* No errors; receive the packet. */
749 	total_len = UGETW(mtod(m, u_int8_t *));
750 	m_adj(m, sizeof(u_int16_t));
751 
752 	if (total_len < sizeof(struct ether_header)) {
753 		ifp->if_ierrors++;
754 		goto done;
755 	}
756 
757 	ifp->if_ipackets++;
758 	m->m_pkthdr.len = m->m_len = total_len;
759 
760 	m->m_pkthdr.rcvif = ifp;
761 
762 	s = splnet();
763 
764 	/* XXX ugly */
765 	if (kue_newbuf(sc, c, NULL) == ENOBUFS) {
766 		ifp->if_ierrors++;
767 		goto done1;
768 	}
769 
770 #if NBPFILTER > 0
771 	/*
772 	 * Handle BPF listeners. Let the BPF user see the packet, but
773 	 * don't pass it up to the ether_input() layer unless it's
774 	 * a broadcast packet, multicast packet, matches our ethernet
775 	 * address or the interface is in promiscuous mode.
776 	 */
777 	if (ifp->if_bpf)
778 		bpf_mtap(ifp->if_bpf, m, BPF_DIRECTION_IN);
779 #endif
780 
781 	DPRINTFN(10,("%s: %s: deliver %d\n", sc->kue_dev.dv_xname,
782 		    __func__, m->m_len));
783 	ether_input_mbuf(ifp, m);
784  done1:
785 	splx(s);
786 
787  done:
788 
789 	/* Setup new transfer. */
790 	usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_RX],
791 	    c, c->kue_buf, KUE_BUFSZ, USBD_SHORT_XFER_OK | USBD_NO_COPY,
792 	    USBD_NO_TIMEOUT, kue_rxeof);
793 	usbd_transfer(c->kue_xfer);
794 
795 	DPRINTFN(10,("%s: %s: start rx\n", sc->kue_dev.dv_xname,
796 		    __func__));
797 }
798 
799 /*
800  * A frame was downloaded to the chip. It's safe for us to clean up
801  * the list buffers.
802  */
803 
804 void
805 kue_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
806 {
807 	struct kue_chain	*c = priv;
808 	struct kue_softc	*sc = c->kue_sc;
809 	struct ifnet		*ifp = GET_IFP(sc);
810 	int			s;
811 
812 	if (sc->kue_dying)
813 		return;
814 
815 	s = splnet();
816 
817 	DPRINTFN(10,("%s: %s: enter status=%d\n", sc->kue_dev.dv_xname,
818 		    __func__, status));
819 
820 	ifp->if_timer = 0;
821 	ifp->if_flags &= ~IFF_OACTIVE;
822 
823 	if (status != USBD_NORMAL_COMPLETION) {
824 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
825 			splx(s);
826 			return;
827 		}
828 		ifp->if_oerrors++;
829 		printf("%s: usb error on tx: %s\n", sc->kue_dev.dv_xname,
830 		    usbd_errstr(status));
831 		if (status == USBD_STALLED)
832 			usbd_clear_endpoint_stall_async(sc->kue_ep[KUE_ENDPT_TX]);
833 		splx(s);
834 		return;
835 	}
836 
837 	ifp->if_opackets++;
838 
839 	m_freem(c->kue_mbuf);
840 	c->kue_mbuf = NULL;
841 
842 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
843 		kue_start(ifp);
844 
845 	splx(s);
846 }
847 
848 int
849 kue_send(struct kue_softc *sc, struct mbuf *m, int idx)
850 {
851 	int			total_len;
852 	struct kue_chain	*c;
853 	usbd_status		err;
854 
855 	DPRINTFN(10,("%s: %s: enter\n", sc->kue_dev.dv_xname,__func__));
856 
857 	c = &sc->kue_cdata.kue_tx_chain[idx];
858 
859 	/*
860 	 * Copy the mbuf data into a contiguous buffer, leaving two
861 	 * bytes at the beginning to hold the frame length.
862 	 */
863 	m_copydata(m, 0, m->m_pkthdr.len, c->kue_buf + 2);
864 	c->kue_mbuf = m;
865 
866 	total_len = m->m_pkthdr.len + 2;
867 	/* XXX what's this? */
868 	total_len += 64 - (total_len % 64);
869 
870 	/* Frame length is specified in the first 2 bytes of the buffer. */
871 	c->kue_buf[0] = (u_int8_t)m->m_pkthdr.len;
872 	c->kue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8);
873 
874 	usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_TX],
875 	    c, c->kue_buf, total_len, USBD_NO_COPY, USBD_DEFAULT_TIMEOUT,
876 	    kue_txeof);
877 
878 	/* Transmit */
879 	err = usbd_transfer(c->kue_xfer);
880 	if (err != USBD_IN_PROGRESS) {
881 		printf("%s: kue_send error=%s\n", sc->kue_dev.dv_xname,
882 		       usbd_errstr(err));
883 		kue_stop(sc);
884 		return (EIO);
885 	}
886 
887 	sc->kue_cdata.kue_tx_cnt++;
888 
889 	return (0);
890 }
891 
892 void
893 kue_start(struct ifnet *ifp)
894 {
895 	struct kue_softc	*sc = ifp->if_softc;
896 	struct mbuf		*m_head = NULL;
897 
898 	DPRINTFN(10,("%s: %s: enter\n", sc->kue_dev.dv_xname,__func__));
899 
900 	if (sc->kue_dying)
901 		return;
902 
903 	if (ifp->if_flags & IFF_OACTIVE)
904 		return;
905 
906 	IFQ_POLL(&ifp->if_snd, m_head);
907 	if (m_head == NULL)
908 		return;
909 
910 	if (kue_send(sc, m_head, 0)) {
911 		ifp->if_flags |= IFF_OACTIVE;
912 		return;
913 	}
914 
915 	IFQ_DEQUEUE(&ifp->if_snd, m_head);
916 
917 #if NBPFILTER > 0
918 	/*
919 	 * If there's a BPF listener, bounce a copy of this frame
920 	 * to him.
921 	 */
922 	if (ifp->if_bpf)
923 		bpf_mtap(ifp->if_bpf, m_head, BPF_DIRECTION_OUT);
924 #endif
925 
926 	ifp->if_flags |= IFF_OACTIVE;
927 
928 	/*
929 	 * Set a timeout in case the chip goes out to lunch.
930 	 */
931 	ifp->if_timer = 6;
932 }
933 
934 void
935 kue_init(void *xsc)
936 {
937 	struct kue_softc	*sc = xsc;
938 	struct ifnet		*ifp = GET_IFP(sc);
939 	int			s;
940 	u_char			*eaddr;
941 
942 	DPRINTFN(5,("%s: %s: enter\n", sc->kue_dev.dv_xname,__func__));
943 
944 	if (ifp->if_flags & IFF_RUNNING)
945 		return;
946 
947 	s = splnet();
948 
949 	eaddr = sc->arpcom.ac_enaddr;
950 	/* Set MAC address */
951 	kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SET_MAC, 0, eaddr, ETHER_ADDR_LEN);
952 
953 	sc->kue_rxfilt = KUE_RXFILT_UNICAST | KUE_RXFILT_BROADCAST;
954 
955 	 /* If we want promiscuous mode, set the allframes bit. */
956 	if (ifp->if_flags & IFF_PROMISC)
957 		sc->kue_rxfilt |= KUE_RXFILT_PROMISC;
958 
959 	kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt);
960 
961 	/* I'm not sure how to tune these. */
962 #if 0
963 	/*
964 	 * Leave this one alone for now; setting it
965 	 * wrong causes lockups on some machines/controllers.
966 	 */
967 	kue_setword(sc, KUE_CMD_SET_SOFS, 1);
968 #endif
969 	kue_setword(sc, KUE_CMD_SET_URB_SIZE, 64);
970 
971 	/* Init TX ring. */
972 	if (kue_tx_list_init(sc) == ENOBUFS) {
973 		printf("%s: tx list init failed\n", sc->kue_dev.dv_xname);
974 		splx(s);
975 		return;
976 	}
977 
978 	/* Init RX ring. */
979 	if (kue_rx_list_init(sc) == ENOBUFS) {
980 		printf("%s: rx list init failed\n", sc->kue_dev.dv_xname);
981 		splx(s);
982 		return;
983 	}
984 
985 	/* Load the multicast filter. */
986 	kue_setmulti(sc);
987 
988 	if (sc->kue_ep[KUE_ENDPT_RX] == NULL) {
989 		if (kue_open_pipes(sc)) {
990 			splx(s);
991 			return;
992 		}
993 	}
994 
995 	ifp->if_flags |= IFF_RUNNING;
996 	ifp->if_flags &= ~IFF_OACTIVE;
997 
998 	splx(s);
999 }
1000 
1001 int
1002 kue_open_pipes(struct kue_softc *sc)
1003 {
1004 	usbd_status		err;
1005 	struct kue_chain	*c;
1006 	int			i;
1007 
1008 	DPRINTFN(5,("%s: %s: enter\n", sc->kue_dev.dv_xname,__func__));
1009 
1010 	/* Open RX and TX pipes. */
1011 	err = usbd_open_pipe(sc->kue_iface, sc->kue_ed[KUE_ENDPT_RX],
1012 	    USBD_EXCLUSIVE_USE, &sc->kue_ep[KUE_ENDPT_RX]);
1013 	if (err) {
1014 		printf("%s: open rx pipe failed: %s\n",
1015 		    sc->kue_dev.dv_xname, usbd_errstr(err));
1016 		return (EIO);
1017 	}
1018 
1019 	err = usbd_open_pipe(sc->kue_iface, sc->kue_ed[KUE_ENDPT_TX],
1020 	    USBD_EXCLUSIVE_USE, &sc->kue_ep[KUE_ENDPT_TX]);
1021 	if (err) {
1022 		printf("%s: open tx pipe failed: %s\n",
1023 		    sc->kue_dev.dv_xname, usbd_errstr(err));
1024 		return (EIO);
1025 	}
1026 
1027 	/* Start up the receive pipe. */
1028 	for (i = 0; i < KUE_RX_LIST_CNT; i++) {
1029 		c = &sc->kue_cdata.kue_rx_chain[i];
1030 		usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_RX],
1031 		    c, c->kue_buf, KUE_BUFSZ,
1032 		    USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT,
1033 		    kue_rxeof);
1034 		DPRINTFN(5,("%s: %s: start read\n", sc->kue_dev.dv_xname,
1035 			    __func__));
1036 		usbd_transfer(c->kue_xfer);
1037 	}
1038 
1039 	return (0);
1040 }
1041 
1042 int
1043 kue_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
1044 {
1045 	struct kue_softc	*sc = ifp->if_softc;
1046 	struct ifaddr 		*ifa = (struct ifaddr *)data;
1047 	int			s, error = 0;
1048 
1049 	DPRINTFN(5,("%s: %s: enter\n", sc->kue_dev.dv_xname,__func__));
1050 
1051 	if (sc->kue_dying)
1052 		return (EIO);
1053 
1054 #ifdef DIAGNOSTIC
1055 	if (!curproc) {
1056 		printf("%s: no proc!!\n", sc->kue_dev.dv_xname);
1057 		return EIO;
1058 	}
1059 #endif
1060 
1061 	s = splnet();
1062 
1063 	switch(command) {
1064 	case SIOCSIFADDR:
1065 		ifp->if_flags |= IFF_UP;
1066 		kue_init(sc);
1067 
1068 		switch (ifa->ifa_addr->sa_family) {
1069 #ifdef INET
1070 		case AF_INET:
1071 			arp_ifinit(&sc->arpcom, ifa);
1072 			break;
1073 #endif /* INET */
1074 		}
1075 		break;
1076 
1077 	case SIOCSIFFLAGS:
1078 		if (ifp->if_flags & IFF_UP) {
1079 			if (ifp->if_flags & IFF_RUNNING &&
1080 			    ifp->if_flags & IFF_PROMISC &&
1081 			    !(sc->kue_if_flags & IFF_PROMISC)) {
1082 				sc->kue_rxfilt |= KUE_RXFILT_PROMISC;
1083 				kue_setword(sc, KUE_CMD_SET_PKT_FILTER,
1084 				    sc->kue_rxfilt);
1085 			} else if (ifp->if_flags & IFF_RUNNING &&
1086 			    !(ifp->if_flags & IFF_PROMISC) &&
1087 			    sc->kue_if_flags & IFF_PROMISC) {
1088 				sc->kue_rxfilt &= ~KUE_RXFILT_PROMISC;
1089 				kue_setword(sc, KUE_CMD_SET_PKT_FILTER,
1090 				    sc->kue_rxfilt);
1091 			} else if (!(ifp->if_flags & IFF_RUNNING))
1092 				kue_init(sc);
1093 		} else {
1094 			if (ifp->if_flags & IFF_RUNNING)
1095 				kue_stop(sc);
1096 		}
1097 		sc->kue_if_flags = ifp->if_flags;
1098 		error = 0;
1099 		break;
1100 
1101 	default:
1102 		error = ether_ioctl(ifp, &sc->arpcom, command, data);
1103 	}
1104 
1105 	if (error == ENETRESET) {
1106 		if (ifp->if_flags & IFF_RUNNING)
1107 			kue_setmulti(sc);
1108 		error = 0;
1109 	}
1110 
1111 	splx(s);
1112 	return (error);
1113 }
1114 
1115 void
1116 kue_watchdog(struct ifnet *ifp)
1117 {
1118 	struct kue_softc	*sc = ifp->if_softc;
1119 	struct kue_chain	*c;
1120 	usbd_status		stat;
1121 	int			s;
1122 
1123 	DPRINTFN(5,("%s: %s: enter\n", sc->kue_dev.dv_xname,__func__));
1124 
1125 	if (sc->kue_dying)
1126 		return;
1127 
1128 	ifp->if_oerrors++;
1129 	printf("%s: watchdog timeout\n", sc->kue_dev.dv_xname);
1130 
1131 	s = splusb();
1132 	c = &sc->kue_cdata.kue_tx_chain[0];
1133 	usbd_get_xfer_status(c->kue_xfer, NULL, NULL, NULL, &stat);
1134 	kue_txeof(c->kue_xfer, c, stat);
1135 
1136 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1137 		kue_start(ifp);
1138 	splx(s);
1139 }
1140 
1141 /*
1142  * Stop the adapter and free any mbufs allocated to the
1143  * RX and TX lists.
1144  */
1145 void
1146 kue_stop(struct kue_softc *sc)
1147 {
1148 	usbd_status		err;
1149 	struct ifnet		*ifp;
1150 	int			i;
1151 
1152 	DPRINTFN(5,("%s: %s: enter\n", sc->kue_dev.dv_xname,__func__));
1153 
1154 	ifp = GET_IFP(sc);
1155 	ifp->if_timer = 0;
1156 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1157 
1158 	/* Stop transfers. */
1159 	if (sc->kue_ep[KUE_ENDPT_RX] != NULL) {
1160 		err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_RX]);
1161 		if (err) {
1162 			printf("%s: abort rx pipe failed: %s\n",
1163 			    sc->kue_dev.dv_xname, usbd_errstr(err));
1164 		}
1165 		err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_RX]);
1166 		if (err) {
1167 			printf("%s: close rx pipe failed: %s\n",
1168 			    sc->kue_dev.dv_xname, usbd_errstr(err));
1169 		}
1170 		sc->kue_ep[KUE_ENDPT_RX] = NULL;
1171 	}
1172 
1173 	if (sc->kue_ep[KUE_ENDPT_TX] != NULL) {
1174 		err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_TX]);
1175 		if (err) {
1176 			printf("%s: abort tx pipe failed: %s\n",
1177 			    sc->kue_dev.dv_xname, usbd_errstr(err));
1178 		}
1179 		err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_TX]);
1180 		if (err) {
1181 			printf("%s: close tx pipe failed: %s\n",
1182 			    sc->kue_dev.dv_xname, usbd_errstr(err));
1183 		}
1184 		sc->kue_ep[KUE_ENDPT_TX] = NULL;
1185 	}
1186 
1187 	if (sc->kue_ep[KUE_ENDPT_INTR] != NULL) {
1188 		err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_INTR]);
1189 		if (err) {
1190 			printf("%s: abort intr pipe failed: %s\n",
1191 			    sc->kue_dev.dv_xname, usbd_errstr(err));
1192 		}
1193 		err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_INTR]);
1194 		if (err) {
1195 			printf("%s: close intr pipe failed: %s\n",
1196 			    sc->kue_dev.dv_xname, usbd_errstr(err));
1197 		}
1198 		sc->kue_ep[KUE_ENDPT_INTR] = NULL;
1199 	}
1200 
1201 	/* Free RX resources. */
1202 	for (i = 0; i < KUE_RX_LIST_CNT; i++) {
1203 		if (sc->kue_cdata.kue_rx_chain[i].kue_mbuf != NULL) {
1204 			m_freem(sc->kue_cdata.kue_rx_chain[i].kue_mbuf);
1205 			sc->kue_cdata.kue_rx_chain[i].kue_mbuf = NULL;
1206 		}
1207 		if (sc->kue_cdata.kue_rx_chain[i].kue_xfer != NULL) {
1208 			usbd_free_xfer(sc->kue_cdata.kue_rx_chain[i].kue_xfer);
1209 			sc->kue_cdata.kue_rx_chain[i].kue_xfer = NULL;
1210 		}
1211 	}
1212 
1213 	/* Free TX resources. */
1214 	for (i = 0; i < KUE_TX_LIST_CNT; i++) {
1215 		if (sc->kue_cdata.kue_tx_chain[i].kue_mbuf != NULL) {
1216 			m_freem(sc->kue_cdata.kue_tx_chain[i].kue_mbuf);
1217 			sc->kue_cdata.kue_tx_chain[i].kue_mbuf = NULL;
1218 		}
1219 		if (sc->kue_cdata.kue_tx_chain[i].kue_xfer != NULL) {
1220 			usbd_free_xfer(sc->kue_cdata.kue_tx_chain[i].kue_xfer);
1221 			sc->kue_cdata.kue_tx_chain[i].kue_xfer = NULL;
1222 		}
1223 	}
1224 }
1225