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