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