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