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