xref: /netbsd-src/sys/dev/usb/if_kue.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /*	$NetBSD: if_kue.c,v 1.64 2008/11/07 00: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.64 2008/11/07 00: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 	devinfop = usbd_devinfo_alloc(dev, 0);
433 	USB_ATTACH_SETUP;
434 	aprint_normal_dev(self, "%s\n", devinfop);
435 	usbd_devinfo_free(devinfop);
436 
437 	err = usbd_set_config_no(dev, KUE_CONFIG_NO, 1);
438 	if (err) {
439 		aprint_error_dev(self, " setting config no failed\n");
440 		USB_ATTACH_ERROR_RETURN;
441 	}
442 
443 	sc->kue_udev = dev;
444 	sc->kue_product = uaa->product;
445 	sc->kue_vendor = uaa->vendor;
446 
447 	/* Load the firmware into the NIC. */
448 	if (kue_load_fw(sc)) {
449 		aprint_error_dev(self, "loading firmware failed\n");
450 		USB_ATTACH_ERROR_RETURN;
451 	}
452 
453 	err = usbd_device2interface_handle(dev, KUE_IFACE_IDX, &iface);
454 	if (err) {
455 		aprint_error_dev(self, "getting interface handle failed\n");
456 		USB_ATTACH_ERROR_RETURN;
457 	}
458 
459 	sc->kue_iface = iface;
460 	id = usbd_get_interface_descriptor(iface);
461 
462 	/* Find endpoints. */
463 	for (i = 0; i < id->bNumEndpoints; i++) {
464 		ed = usbd_interface2endpoint_descriptor(iface, i);
465 		if (ed == NULL) {
466 			aprint_error_dev(self, "couldn't get ep %d\n", i);
467 			USB_ATTACH_ERROR_RETURN;
468 		}
469 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
470 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
471 			sc->kue_ed[KUE_ENDPT_RX] = ed->bEndpointAddress;
472 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
473 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
474 			sc->kue_ed[KUE_ENDPT_TX] = ed->bEndpointAddress;
475 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
476 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
477 			sc->kue_ed[KUE_ENDPT_INTR] = ed->bEndpointAddress;
478 		}
479 	}
480 
481 	if (sc->kue_ed[KUE_ENDPT_RX] == 0 || sc->kue_ed[KUE_ENDPT_TX] == 0) {
482 		aprint_error_dev(self, "missing endpoint\n");
483 		USB_ATTACH_ERROR_RETURN;
484 	}
485 
486 	/* Read ethernet descriptor */
487 	err = kue_ctl(sc, KUE_CTL_READ, KUE_CMD_GET_ETHER_DESCRIPTOR,
488 	    0, &sc->kue_desc, sizeof(sc->kue_desc));
489 	if (err) {
490 		aprint_error_dev(self, "could not read Ethernet descriptor\n");
491 		USB_ATTACH_ERROR_RETURN;
492 	}
493 
494 	sc->kue_mcfilters = malloc(KUE_MCFILTCNT(sc) * ETHER_ADDR_LEN,
495 	    M_USBDEV, M_NOWAIT);
496 	if (sc->kue_mcfilters == NULL) {
497 		aprint_error_dev(self,
498 		    "no memory for multicast filter buffer\n");
499 		USB_ATTACH_ERROR_RETURN;
500 	}
501 
502 	s = splnet();
503 
504 	/*
505 	 * A KLSI chip was detected. Inform the world.
506 	 */
507 	aprint_normal_dev(self, "Ethernet address %s\n",
508 	    ether_sprintf(sc->kue_desc.kue_macaddr));
509 
510 	/* Initialize interface info.*/
511 	ifp = GET_IFP(sc);
512 	ifp->if_softc = sc;
513 	ifp->if_mtu = ETHERMTU;
514 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
515 	ifp->if_ioctl = kue_ioctl;
516 	ifp->if_start = kue_start;
517 	ifp->if_watchdog = kue_watchdog;
518 #if defined(__OpenBSD__)
519 	ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
520 #endif
521 	strncpy(ifp->if_xname, USBDEVNAME(sc->kue_dev), IFNAMSIZ);
522 
523 	IFQ_SET_READY(&ifp->if_snd);
524 
525 	/* Attach the interface. */
526 	if_attach(ifp);
527 	Ether_ifattach(ifp, sc->kue_desc.kue_macaddr);
528 #if NRND > 0
529 	rnd_attach_source(&sc->rnd_source, USBDEVNAME(sc->kue_dev),
530 	    RND_TYPE_NET, 0);
531 #endif
532 
533 	sc->kue_attached = 1;
534 	splx(s);
535 
536 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->kue_udev,
537 			   USBDEV(sc->kue_dev));
538 
539 	USB_ATTACH_SUCCESS_RETURN;
540 }
541 
542 USB_DETACH(kue)
543 {
544 	USB_DETACH_START(kue, sc);
545 	struct ifnet		*ifp = GET_IFP(sc);
546 	int			s;
547 
548 	s = splusb();		/* XXX why? */
549 
550 	if (sc->kue_mcfilters != NULL) {
551 		free(sc->kue_mcfilters, M_USBDEV);
552 		sc->kue_mcfilters = NULL;
553 	}
554 
555 	if (!sc->kue_attached) {
556 		/* Detached before attached finished, so just bail out. */
557 		splx(s);
558 		return (0);
559 	}
560 
561 	if (ifp->if_flags & IFF_RUNNING)
562 		kue_stop(sc);
563 
564 #if defined(__NetBSD__)
565 #if NRND > 0
566 	rnd_detach_source(&sc->rnd_source);
567 #endif
568 	ether_ifdetach(ifp);
569 #endif /* __NetBSD__ */
570 
571 	if_detach(ifp);
572 
573 #ifdef DIAGNOSTIC
574 	if (sc->kue_ep[KUE_ENDPT_TX] != NULL ||
575 	    sc->kue_ep[KUE_ENDPT_RX] != NULL ||
576 	    sc->kue_ep[KUE_ENDPT_INTR] != NULL)
577 		aprint_debug_dev(self, "detach has active endpoints\n");
578 #endif
579 
580 	sc->kue_attached = 0;
581 	splx(s);
582 
583 	return (0);
584 }
585 
586 int
587 kue_activate(device_ptr_t self, enum devact act)
588 {
589 	struct kue_softc *sc = device_private(self);
590 
591 	DPRINTFN(2,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev), __func__));
592 
593 	switch (act) {
594 	case DVACT_ACTIVATE:
595 		return (EOPNOTSUPP);
596 		break;
597 
598 	case DVACT_DEACTIVATE:
599 #if defined(__NetBSD__)
600 		/* Deactivate the interface. */
601 		if_deactivate(&sc->kue_ec.ec_if);
602 #endif
603 		sc->kue_dying = 1;
604 		break;
605 	}
606 	return (0);
607 }
608 
609 /*
610  * Initialize an RX descriptor and attach an MBUF cluster.
611  */
612 Static int
613 kue_newbuf(struct kue_softc *sc, struct kue_chain *c, struct mbuf *m)
614 {
615 	struct mbuf		*m_new = NULL;
616 
617 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__func__));
618 
619 	if (m == NULL) {
620 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
621 		if (m_new == NULL) {
622 			printf("%s: no memory for rx list "
623 			    "-- packet dropped!\n", USBDEVNAME(sc->kue_dev));
624 			return (ENOBUFS);
625 		}
626 
627 		MCLGET(m_new, M_DONTWAIT);
628 		if (!(m_new->m_flags & M_EXT)) {
629 			printf("%s: no memory for rx list "
630 			    "-- packet dropped!\n", USBDEVNAME(sc->kue_dev));
631 			m_freem(m_new);
632 			return (ENOBUFS);
633 		}
634 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
635 	} else {
636 		m_new = m;
637 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
638 		m_new->m_data = m_new->m_ext.ext_buf;
639 	}
640 
641 	c->kue_mbuf = m_new;
642 
643 	return (0);
644 }
645 
646 Static int
647 kue_rx_list_init(struct kue_softc *sc)
648 {
649 	struct kue_cdata	*cd;
650 	struct kue_chain	*c;
651 	int			i;
652 
653 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev), __func__));
654 
655 	cd = &sc->kue_cdata;
656 	for (i = 0; i < KUE_RX_LIST_CNT; i++) {
657 		c = &cd->kue_rx_chain[i];
658 		c->kue_sc = sc;
659 		c->kue_idx = i;
660 		if (kue_newbuf(sc, c, NULL) == ENOBUFS)
661 			return (ENOBUFS);
662 		if (c->kue_xfer == NULL) {
663 			c->kue_xfer = usbd_alloc_xfer(sc->kue_udev);
664 			if (c->kue_xfer == NULL)
665 				return (ENOBUFS);
666 			c->kue_buf = usbd_alloc_buffer(c->kue_xfer, KUE_BUFSZ);
667 			if (c->kue_buf == NULL)
668 				return (ENOBUFS); /* XXX free xfer */
669 		}
670 	}
671 
672 	return (0);
673 }
674 
675 Static int
676 kue_tx_list_init(struct kue_softc *sc)
677 {
678 	struct kue_cdata	*cd;
679 	struct kue_chain	*c;
680 	int			i;
681 
682 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev), __func__));
683 
684 	cd = &sc->kue_cdata;
685 	for (i = 0; i < KUE_TX_LIST_CNT; i++) {
686 		c = &cd->kue_tx_chain[i];
687 		c->kue_sc = sc;
688 		c->kue_idx = i;
689 		c->kue_mbuf = NULL;
690 		if (c->kue_xfer == NULL) {
691 			c->kue_xfer = usbd_alloc_xfer(sc->kue_udev);
692 			if (c->kue_xfer == NULL)
693 				return (ENOBUFS);
694 			c->kue_buf = usbd_alloc_buffer(c->kue_xfer, KUE_BUFSZ);
695 			if (c->kue_buf == NULL)
696 				return (ENOBUFS);
697 		}
698 	}
699 
700 	return (0);
701 }
702 
703 /*
704  * A frame has been uploaded: pass the resulting mbuf chain up to
705  * the higher level protocols.
706  */
707 Static void
708 kue_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
709 {
710 	struct kue_chain	*c = priv;
711 	struct kue_softc	*sc = c->kue_sc;
712 	struct ifnet		*ifp = GET_IFP(sc);
713 	struct mbuf		*m;
714 	int			total_len = 0;
715 	int			s;
716 
717 	DPRINTFN(10,("%s: %s: enter status=%d\n", USBDEVNAME(sc->kue_dev),
718 		     __func__, status));
719 
720 	if (sc->kue_dying)
721 		return;
722 
723 	if (!(ifp->if_flags & IFF_RUNNING))
724 		return;
725 
726 	if (status != USBD_NORMAL_COMPLETION) {
727 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
728 			return;
729 		sc->kue_rx_errs++;
730 		if (usbd_ratecheck(&sc->kue_rx_notice)) {
731 			printf("%s: %u usb errors on rx: %s\n",
732 			    USBDEVNAME(sc->kue_dev), sc->kue_rx_errs,
733 			    usbd_errstr(status));
734 			sc->kue_rx_errs = 0;
735 		}
736 		if (status == USBD_STALLED)
737 			usbd_clear_endpoint_stall_async(sc->kue_ep[KUE_ENDPT_RX]);
738 		goto done;
739 	}
740 
741 	usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL);
742 
743 	DPRINTFN(10,("%s: %s: total_len=%d len=%d\n", USBDEVNAME(sc->kue_dev),
744 		     __func__, total_len,
745 		     UGETW(mtod(c->kue_mbuf, u_int8_t *))));
746 
747 	if (total_len <= 1)
748 		goto done;
749 
750 	m = c->kue_mbuf;
751 	/* copy data to mbuf */
752 	memcpy(mtod(m, char *), c->kue_buf, total_len);
753 
754 	/* No errors; receive the packet. */
755 	total_len = UGETW(mtod(m, u_int8_t *));
756 	m_adj(m, sizeof(u_int16_t));
757 
758 	if (total_len < sizeof(struct ether_header)) {
759 		ifp->if_ierrors++;
760 		goto done;
761 	}
762 
763 	ifp->if_ipackets++;
764 	m->m_pkthdr.len = m->m_len = total_len;
765 
766 	m->m_pkthdr.rcvif = ifp;
767 
768 	s = splnet();
769 
770 	/* XXX ugly */
771 	if (kue_newbuf(sc, c, NULL) == ENOBUFS) {
772 		ifp->if_ierrors++;
773 		goto done1;
774 	}
775 
776 #if NBPFILTER > 0
777 	/*
778 	 * Handle BPF listeners. Let the BPF user see the packet, but
779 	 * don't pass it up to the ether_input() layer unless it's
780 	 * a broadcast packet, multicast packet, matches our ethernet
781 	 * address or the interface is in promiscuous mode.
782 	 */
783 	if (ifp->if_bpf)
784 		bpf_mtap(ifp->if_bpf, m);
785 #endif
786 
787 	DPRINTFN(10,("%s: %s: deliver %d\n", USBDEVNAME(sc->kue_dev),
788 		    __func__, m->m_len));
789 	IF_INPUT(ifp, m);
790  done1:
791 	splx(s);
792 
793  done:
794 
795 	/* Setup new transfer. */
796 	usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_RX],
797 	    c, c->kue_buf, KUE_BUFSZ, USBD_SHORT_XFER_OK | USBD_NO_COPY,
798 	    USBD_NO_TIMEOUT, kue_rxeof);
799 	usbd_transfer(c->kue_xfer);
800 
801 	DPRINTFN(10,("%s: %s: start rx\n", USBDEVNAME(sc->kue_dev),
802 		    __func__));
803 }
804 
805 /*
806  * A frame was downloaded to the chip. It's safe for us to clean up
807  * the list buffers.
808  */
809 
810 Static void
811 kue_txeof(usbd_xfer_handle xfer, usbd_private_handle priv,
812     usbd_status status)
813 {
814 	struct kue_chain	*c = priv;
815 	struct kue_softc	*sc = c->kue_sc;
816 	struct ifnet		*ifp = GET_IFP(sc);
817 	int			s;
818 
819 	if (sc->kue_dying)
820 		return;
821 
822 	s = splnet();
823 
824 	DPRINTFN(10,("%s: %s: enter status=%d\n", USBDEVNAME(sc->kue_dev),
825 		    __func__, status));
826 
827 	ifp->if_timer = 0;
828 	ifp->if_flags &= ~IFF_OACTIVE;
829 
830 	if (status != USBD_NORMAL_COMPLETION) {
831 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) {
832 			splx(s);
833 			return;
834 		}
835 		ifp->if_oerrors++;
836 		printf("%s: usb error on tx: %s\n", USBDEVNAME(sc->kue_dev),
837 		    usbd_errstr(status));
838 		if (status == USBD_STALLED)
839 			usbd_clear_endpoint_stall_async(sc->kue_ep[KUE_ENDPT_TX]);
840 		splx(s);
841 		return;
842 	}
843 
844 	ifp->if_opackets++;
845 
846 	m_freem(c->kue_mbuf);
847 	c->kue_mbuf = NULL;
848 
849 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
850 		kue_start(ifp);
851 
852 	splx(s);
853 }
854 
855 Static int
856 kue_send(struct kue_softc *sc, struct mbuf *m, int idx)
857 {
858 	int			total_len;
859 	struct kue_chain	*c;
860 	usbd_status		err;
861 
862 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__func__));
863 
864 	c = &sc->kue_cdata.kue_tx_chain[idx];
865 
866 	/*
867 	 * Copy the mbuf data into a contiguous buffer, leaving two
868 	 * bytes at the beginning to hold the frame length.
869 	 */
870 	m_copydata(m, 0, m->m_pkthdr.len, c->kue_buf + 2);
871 	c->kue_mbuf = m;
872 
873 	total_len = m->m_pkthdr.len + 2;
874 	/* XXX what's this? */
875 	total_len += 64 - (total_len % 64);
876 
877 	/* Frame length is specified in the first 2 bytes of the buffer. */
878 	c->kue_buf[0] = (u_int8_t)m->m_pkthdr.len;
879 	c->kue_buf[1] = (u_int8_t)(m->m_pkthdr.len >> 8);
880 
881 	usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_TX],
882 	    c, c->kue_buf, total_len, USBD_NO_COPY, USBD_DEFAULT_TIMEOUT,
883 	    kue_txeof);
884 
885 	/* Transmit */
886 	err = usbd_transfer(c->kue_xfer);
887 	if (err != USBD_IN_PROGRESS) {
888 		printf("%s: kue_send error=%s\n", USBDEVNAME(sc->kue_dev),
889 		       usbd_errstr(err));
890 		kue_stop(sc);
891 		return (EIO);
892 	}
893 
894 	sc->kue_cdata.kue_tx_cnt++;
895 
896 	return (0);
897 }
898 
899 Static void
900 kue_start(struct ifnet *ifp)
901 {
902 	struct kue_softc	*sc = ifp->if_softc;
903 	struct mbuf		*m_head = NULL;
904 
905 	DPRINTFN(10,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__func__));
906 
907 	if (sc->kue_dying)
908 		return;
909 
910 	if (ifp->if_flags & IFF_OACTIVE)
911 		return;
912 
913 	IFQ_POLL(&ifp->if_snd, m_head);
914 	if (m_head == NULL)
915 		return;
916 
917 	if (kue_send(sc, m_head, 0)) {
918 		ifp->if_flags |= IFF_OACTIVE;
919 		return;
920 	}
921 
922 	IFQ_DEQUEUE(&ifp->if_snd, m_head);
923 
924 #if NBPFILTER > 0
925 	/*
926 	 * If there's a BPF listener, bounce a copy of this frame
927 	 * to him.
928 	 */
929 	if (ifp->if_bpf)
930 		bpf_mtap(ifp->if_bpf, m_head);
931 #endif
932 
933 	ifp->if_flags |= IFF_OACTIVE;
934 
935 	/*
936 	 * Set a timeout in case the chip goes out to lunch.
937 	 */
938 	ifp->if_timer = 6;
939 }
940 
941 Static void
942 kue_init(void *xsc)
943 {
944 	struct kue_softc	*sc = xsc;
945 	struct ifnet		*ifp = GET_IFP(sc);
946 	int			s;
947 	u_char			eaddr[ETHER_ADDR_LEN];
948 
949 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__func__));
950 
951 	if (ifp->if_flags & IFF_RUNNING)
952 		return;
953 
954 	s = splnet();
955 
956 	memcpy(eaddr, CLLADDR(ifp->if_sadl), sizeof(eaddr));
957 	/* Set MAC address */
958 	kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SET_MAC, 0, eaddr, ETHER_ADDR_LEN);
959 
960 	sc->kue_rxfilt = KUE_RXFILT_UNICAST | KUE_RXFILT_BROADCAST;
961 
962 	 /* If we want promiscuous mode, set the allframes bit. */
963 	if (ifp->if_flags & IFF_PROMISC)
964 		sc->kue_rxfilt |= KUE_RXFILT_PROMISC;
965 
966 	kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->kue_rxfilt);
967 
968 	/* I'm not sure how to tune these. */
969 #if 0
970 	/*
971 	 * Leave this one alone for now; setting it
972 	 * wrong causes lockups on some machines/controllers.
973 	 */
974 	kue_setword(sc, KUE_CMD_SET_SOFS, 1);
975 #endif
976 	kue_setword(sc, KUE_CMD_SET_URB_SIZE, 64);
977 
978 	/* Init TX ring. */
979 	if (kue_tx_list_init(sc) == ENOBUFS) {
980 		printf("%s: tx list init failed\n", USBDEVNAME(sc->kue_dev));
981 		splx(s);
982 		return;
983 	}
984 
985 	/* Init RX ring. */
986 	if (kue_rx_list_init(sc) == ENOBUFS) {
987 		printf("%s: rx list init failed\n", USBDEVNAME(sc->kue_dev));
988 		splx(s);
989 		return;
990 	}
991 
992 	/* Load the multicast filter. */
993 	kue_setmulti(sc);
994 
995 	if (sc->kue_ep[KUE_ENDPT_RX] == NULL) {
996 		if (kue_open_pipes(sc)) {
997 			splx(s);
998 			return;
999 		}
1000 	}
1001 
1002 	ifp->if_flags |= IFF_RUNNING;
1003 	ifp->if_flags &= ~IFF_OACTIVE;
1004 
1005 	splx(s);
1006 }
1007 
1008 Static int
1009 kue_open_pipes(struct kue_softc *sc)
1010 {
1011 	usbd_status		err;
1012 	struct kue_chain	*c;
1013 	int			i;
1014 
1015 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__func__));
1016 
1017 	/* Open RX and TX pipes. */
1018 	err = usbd_open_pipe(sc->kue_iface, sc->kue_ed[KUE_ENDPT_RX],
1019 	    USBD_EXCLUSIVE_USE, &sc->kue_ep[KUE_ENDPT_RX]);
1020 	if (err) {
1021 		printf("%s: open rx pipe failed: %s\n",
1022 		    USBDEVNAME(sc->kue_dev), usbd_errstr(err));
1023 		return (EIO);
1024 	}
1025 
1026 	err = usbd_open_pipe(sc->kue_iface, sc->kue_ed[KUE_ENDPT_TX],
1027 	    USBD_EXCLUSIVE_USE, &sc->kue_ep[KUE_ENDPT_TX]);
1028 	if (err) {
1029 		printf("%s: open tx pipe failed: %s\n",
1030 		    USBDEVNAME(sc->kue_dev), usbd_errstr(err));
1031 		return (EIO);
1032 	}
1033 
1034 	/* Start up the receive pipe. */
1035 	for (i = 0; i < KUE_RX_LIST_CNT; i++) {
1036 		c = &sc->kue_cdata.kue_rx_chain[i];
1037 		usbd_setup_xfer(c->kue_xfer, sc->kue_ep[KUE_ENDPT_RX],
1038 		    c, c->kue_buf, KUE_BUFSZ,
1039 		    USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT,
1040 		    kue_rxeof);
1041 		DPRINTFN(5,("%s: %s: start read\n", USBDEVNAME(sc->kue_dev),
1042 			    __func__));
1043 		usbd_transfer(c->kue_xfer);
1044 	}
1045 
1046 	return (0);
1047 }
1048 
1049 Static int
1050 kue_ioctl(struct ifnet *ifp, u_long command, void *data)
1051 {
1052 	struct kue_softc	*sc = ifp->if_softc;
1053 	struct ifaddr 		*ifa = (struct ifaddr *)data;
1054 	struct ifreq		*ifr = (struct ifreq *)data;
1055 	int			s, error = 0;
1056 
1057 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__func__));
1058 
1059 	if (sc->kue_dying)
1060 		return (EIO);
1061 
1062 #ifdef DIAGNOSTIC
1063 	if (!curproc) {
1064 		printf("%s: no proc!!\n", USBDEVNAME(sc->kue_dev));
1065 		return EIO;
1066 	}
1067 #endif
1068 
1069 	s = splnet();
1070 
1071 	switch(command) {
1072 	case SIOCINITIFADDR:
1073 		ifp->if_flags |= IFF_UP;
1074 		kue_init(sc);
1075 
1076 		switch (ifa->ifa_addr->sa_family) {
1077 #ifdef INET
1078 		case AF_INET:
1079 #if defined(__NetBSD__)
1080 			arp_ifinit(ifp, ifa);
1081 #else
1082 			arp_ifinit(&sc->arpcom, ifa);
1083 #endif
1084 			break;
1085 #endif /* INET */
1086 		}
1087 		break;
1088 
1089 	case SIOCSIFMTU:
1090 		if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > ETHERMTU)
1091 			error = EINVAL;
1092 		else if ((error = ifioctl_common(ifp, command, data)) == ENETRESET)
1093 			error = 0;
1094 		break;
1095 
1096 	case SIOCSIFFLAGS:
1097 		if ((error = ifioctl_common(ifp, command, data)) != 0)
1098 			break;
1099 		if (ifp->if_flags & IFF_UP) {
1100 			if (ifp->if_flags & IFF_RUNNING &&
1101 			    ifp->if_flags & IFF_PROMISC &&
1102 			    !(sc->kue_if_flags & IFF_PROMISC)) {
1103 				sc->kue_rxfilt |= KUE_RXFILT_PROMISC;
1104 				kue_setword(sc, KUE_CMD_SET_PKT_FILTER,
1105 				    sc->kue_rxfilt);
1106 			} else if (ifp->if_flags & IFF_RUNNING &&
1107 			    !(ifp->if_flags & IFF_PROMISC) &&
1108 			    sc->kue_if_flags & IFF_PROMISC) {
1109 				sc->kue_rxfilt &= ~KUE_RXFILT_PROMISC;
1110 				kue_setword(sc, KUE_CMD_SET_PKT_FILTER,
1111 				    sc->kue_rxfilt);
1112 			} else if (!(ifp->if_flags & IFF_RUNNING))
1113 				kue_init(sc);
1114 		} else {
1115 			if (ifp->if_flags & IFF_RUNNING)
1116 				kue_stop(sc);
1117 		}
1118 		sc->kue_if_flags = ifp->if_flags;
1119 		error = 0;
1120 		break;
1121 	case SIOCADDMULTI:
1122 	case SIOCDELMULTI:
1123 		kue_setmulti(sc);
1124 		error = 0;
1125 		break;
1126 	default:
1127 		error = ether_ioctl(ifp, command, data);
1128 		break;
1129 	}
1130 
1131 	splx(s);
1132 
1133 	return (error);
1134 }
1135 
1136 Static void
1137 kue_watchdog(struct ifnet *ifp)
1138 {
1139 	struct kue_softc	*sc = ifp->if_softc;
1140 	struct kue_chain	*c;
1141 	usbd_status		stat;
1142 	int			s;
1143 
1144 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__func__));
1145 
1146 	if (sc->kue_dying)
1147 		return;
1148 
1149 	ifp->if_oerrors++;
1150 	printf("%s: watchdog timeout\n", USBDEVNAME(sc->kue_dev));
1151 
1152 	s = splusb();
1153 	c = &sc->kue_cdata.kue_tx_chain[0];
1154 	usbd_get_xfer_status(c->kue_xfer, NULL, NULL, NULL, &stat);
1155 	kue_txeof(c->kue_xfer, c, stat);
1156 
1157 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
1158 		kue_start(ifp);
1159 	splx(s);
1160 }
1161 
1162 /*
1163  * Stop the adapter and free any mbufs allocated to the
1164  * RX and TX lists.
1165  */
1166 Static void
1167 kue_stop(struct kue_softc *sc)
1168 {
1169 	usbd_status		err;
1170 	struct ifnet		*ifp;
1171 	int			i;
1172 
1173 	DPRINTFN(5,("%s: %s: enter\n", USBDEVNAME(sc->kue_dev),__func__));
1174 
1175 	ifp = GET_IFP(sc);
1176 	ifp->if_timer = 0;
1177 
1178 	/* Stop transfers. */
1179 	if (sc->kue_ep[KUE_ENDPT_RX] != NULL) {
1180 		err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_RX]);
1181 		if (err) {
1182 			printf("%s: abort rx pipe failed: %s\n",
1183 			    USBDEVNAME(sc->kue_dev), usbd_errstr(err));
1184 		}
1185 		err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_RX]);
1186 		if (err) {
1187 			printf("%s: close rx pipe failed: %s\n",
1188 			    USBDEVNAME(sc->kue_dev), usbd_errstr(err));
1189 		}
1190 		sc->kue_ep[KUE_ENDPT_RX] = NULL;
1191 	}
1192 
1193 	if (sc->kue_ep[KUE_ENDPT_TX] != NULL) {
1194 		err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_TX]);
1195 		if (err) {
1196 			printf("%s: abort tx pipe failed: %s\n",
1197 			    USBDEVNAME(sc->kue_dev), usbd_errstr(err));
1198 		}
1199 		err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_TX]);
1200 		if (err) {
1201 			printf("%s: close tx pipe failed: %s\n",
1202 			    USBDEVNAME(sc->kue_dev), usbd_errstr(err));
1203 		}
1204 		sc->kue_ep[KUE_ENDPT_TX] = NULL;
1205 	}
1206 
1207 	if (sc->kue_ep[KUE_ENDPT_INTR] != NULL) {
1208 		err = usbd_abort_pipe(sc->kue_ep[KUE_ENDPT_INTR]);
1209 		if (err) {
1210 			printf("%s: abort intr pipe failed: %s\n",
1211 			    USBDEVNAME(sc->kue_dev), usbd_errstr(err));
1212 		}
1213 		err = usbd_close_pipe(sc->kue_ep[KUE_ENDPT_INTR]);
1214 		if (err) {
1215 			printf("%s: close intr pipe failed: %s\n",
1216 			    USBDEVNAME(sc->kue_dev), usbd_errstr(err));
1217 		}
1218 		sc->kue_ep[KUE_ENDPT_INTR] = NULL;
1219 	}
1220 
1221 	/* Free RX resources. */
1222 	for (i = 0; i < KUE_RX_LIST_CNT; i++) {
1223 		if (sc->kue_cdata.kue_rx_chain[i].kue_mbuf != NULL) {
1224 			m_freem(sc->kue_cdata.kue_rx_chain[i].kue_mbuf);
1225 			sc->kue_cdata.kue_rx_chain[i].kue_mbuf = NULL;
1226 		}
1227 		if (sc->kue_cdata.kue_rx_chain[i].kue_xfer != NULL) {
1228 			usbd_free_xfer(sc->kue_cdata.kue_rx_chain[i].kue_xfer);
1229 			sc->kue_cdata.kue_rx_chain[i].kue_xfer = NULL;
1230 		}
1231 	}
1232 
1233 	/* Free TX resources. */
1234 	for (i = 0; i < KUE_TX_LIST_CNT; i++) {
1235 		if (sc->kue_cdata.kue_tx_chain[i].kue_mbuf != NULL) {
1236 			m_freem(sc->kue_cdata.kue_tx_chain[i].kue_mbuf);
1237 			sc->kue_cdata.kue_tx_chain[i].kue_mbuf = NULL;
1238 		}
1239 		if (sc->kue_cdata.kue_tx_chain[i].kue_xfer != NULL) {
1240 			usbd_free_xfer(sc->kue_cdata.kue_tx_chain[i].kue_xfer);
1241 			sc->kue_cdata.kue_tx_chain[i].kue_xfer = NULL;
1242 		}
1243 	}
1244 
1245 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1246 }
1247