xref: /freebsd/sys/dev/usb/net/if_kue.c (revision d6b92ffa)
1 /*-
2  * Copyright (c) 1997, 1998, 1999, 2000
3  *	Bill Paul <wpaul@ee.columbia.edu>.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by Bill Paul.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30  * THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
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 #include <sys/stdint.h>
69 #include <sys/stddef.h>
70 #include <sys/param.h>
71 #include <sys/queue.h>
72 #include <sys/types.h>
73 #include <sys/systm.h>
74 #include <sys/socket.h>
75 #include <sys/kernel.h>
76 #include <sys/bus.h>
77 #include <sys/module.h>
78 #include <sys/lock.h>
79 #include <sys/mutex.h>
80 #include <sys/condvar.h>
81 #include <sys/sysctl.h>
82 #include <sys/sx.h>
83 #include <sys/unistd.h>
84 #include <sys/callout.h>
85 #include <sys/malloc.h>
86 #include <sys/priv.h>
87 
88 #include <net/if.h>
89 #include <net/if_var.h>
90 
91 #include <dev/usb/usb.h>
92 #include <dev/usb/usbdi.h>
93 #include <dev/usb/usbdi_util.h>
94 #include "usbdevs.h"
95 
96 #define	USB_DEBUG_VAR kue_debug
97 #include <dev/usb/usb_debug.h>
98 #include <dev/usb/usb_process.h>
99 
100 #include <dev/usb/net/usb_ethernet.h>
101 #include <dev/usb/net/if_kuereg.h>
102 #include <dev/usb/net/if_kuefw.h>
103 
104 /*
105  * Various supported device vendors/products.
106  */
107 static const STRUCT_USB_HOST_ID kue_devs[] = {
108 #define	KUE_DEV(v,p) { USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) }
109 	KUE_DEV(3COM, 3C19250),
110 	KUE_DEV(3COM, 3C460),
111 	KUE_DEV(ABOCOM, URE450),
112 	KUE_DEV(ADS, UBS10BT),
113 	KUE_DEV(ADS, UBS10BTX),
114 	KUE_DEV(AOX, USB101),
115 	KUE_DEV(ASANTE, EA),
116 	KUE_DEV(ATEN, DSB650C),
117 	KUE_DEV(ATEN, UC10T),
118 	KUE_DEV(COREGA, ETHER_USB_T),
119 	KUE_DEV(DLINK, DSB650C),
120 	KUE_DEV(ENTREGA, E45),
121 	KUE_DEV(ENTREGA, XX1),
122 	KUE_DEV(ENTREGA, XX2),
123 	KUE_DEV(IODATA, USBETT),
124 	KUE_DEV(JATON, EDA),
125 	KUE_DEV(KINGSTON, XX1),
126 	KUE_DEV(KLSI, DUH3E10BT),
127 	KUE_DEV(KLSI, DUH3E10BTN),
128 	KUE_DEV(LINKSYS, USB10T),
129 	KUE_DEV(MOBILITY, EA),
130 	KUE_DEV(NETGEAR, EA101),
131 	KUE_DEV(NETGEAR, EA101X),
132 	KUE_DEV(PERACOM, ENET),
133 	KUE_DEV(PERACOM, ENET2),
134 	KUE_DEV(PERACOM, ENET3),
135 	KUE_DEV(PORTGEAR, EA8),
136 	KUE_DEV(PORTGEAR, EA9),
137 	KUE_DEV(PORTSMITH, EEA),
138 	KUE_DEV(SHARK, PA),
139 	KUE_DEV(SILICOM, GPE),
140 	KUE_DEV(SILICOM, U2E),
141 	KUE_DEV(SMC, 2102USB),
142 #undef KUE_DEV
143 };
144 
145 /* prototypes */
146 
147 static device_probe_t kue_probe;
148 static device_attach_t kue_attach;
149 static device_detach_t kue_detach;
150 
151 static usb_callback_t kue_bulk_read_callback;
152 static usb_callback_t kue_bulk_write_callback;
153 
154 static uether_fn_t kue_attach_post;
155 static uether_fn_t kue_init;
156 static uether_fn_t kue_stop;
157 static uether_fn_t kue_start;
158 static uether_fn_t kue_setmulti;
159 static uether_fn_t kue_setpromisc;
160 
161 static int	kue_do_request(struct kue_softc *,
162 		    struct usb_device_request *, void *);
163 static int	kue_setword(struct kue_softc *, uint8_t, uint16_t);
164 static int	kue_ctl(struct kue_softc *, uint8_t, uint8_t, uint16_t,
165 		    void *, int);
166 static int	kue_load_fw(struct kue_softc *);
167 static void	kue_reset(struct kue_softc *);
168 
169 #ifdef USB_DEBUG
170 static int kue_debug = 0;
171 
172 static SYSCTL_NODE(_hw_usb, OID_AUTO, kue, CTLFLAG_RW, 0, "USB kue");
173 SYSCTL_INT(_hw_usb_kue, OID_AUTO, debug, CTLFLAG_RWTUN, &kue_debug, 0,
174     "Debug level");
175 #endif
176 
177 static const struct usb_config kue_config[KUE_N_TRANSFER] = {
178 
179 	[KUE_BULK_DT_WR] = {
180 		.type = UE_BULK,
181 		.endpoint = UE_ADDR_ANY,
182 		.direction = UE_DIR_OUT,
183 		.bufsize = (MCLBYTES + 2 + 64),
184 		.flags = {.pipe_bof = 1,},
185 		.callback = kue_bulk_write_callback,
186 		.timeout = 10000,	/* 10 seconds */
187 	},
188 
189 	[KUE_BULK_DT_RD] = {
190 		.type = UE_BULK,
191 		.endpoint = UE_ADDR_ANY,
192 		.direction = UE_DIR_IN,
193 		.bufsize = (MCLBYTES + 2),
194 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
195 		.callback = kue_bulk_read_callback,
196 		.timeout = 0,	/* no timeout */
197 	},
198 };
199 
200 static device_method_t kue_methods[] = {
201 	/* Device interface */
202 	DEVMETHOD(device_probe, kue_probe),
203 	DEVMETHOD(device_attach, kue_attach),
204 	DEVMETHOD(device_detach, kue_detach),
205 
206 	DEVMETHOD_END
207 };
208 
209 static driver_t kue_driver = {
210 	.name = "kue",
211 	.methods = kue_methods,
212 	.size = sizeof(struct kue_softc),
213 };
214 
215 static devclass_t kue_devclass;
216 
217 DRIVER_MODULE(kue, uhub, kue_driver, kue_devclass, NULL, 0);
218 MODULE_DEPEND(kue, uether, 1, 1, 1);
219 MODULE_DEPEND(kue, usb, 1, 1, 1);
220 MODULE_DEPEND(kue, ether, 1, 1, 1);
221 MODULE_VERSION(kue, 1);
222 USB_PNP_HOST_INFO(kue_devs);
223 
224 static const struct usb_ether_methods kue_ue_methods = {
225 	.ue_attach_post = kue_attach_post,
226 	.ue_start = kue_start,
227 	.ue_init = kue_init,
228 	.ue_stop = kue_stop,
229 	.ue_setmulti = kue_setmulti,
230 	.ue_setpromisc = kue_setpromisc,
231 };
232 
233 /*
234  * We have a custom do_request function which is almost like the
235  * regular do_request function, except it has a much longer timeout.
236  * Why? Because we need to make requests over the control endpoint
237  * to download the firmware to the device, which can take longer
238  * than the default timeout.
239  */
240 static int
241 kue_do_request(struct kue_softc *sc, struct usb_device_request *req,
242     void *data)
243 {
244 	usb_error_t err;
245 
246 	err = uether_do_request(&sc->sc_ue, req, data, 60000);
247 
248 	return (err);
249 }
250 
251 static int
252 kue_setword(struct kue_softc *sc, uint8_t breq, uint16_t word)
253 {
254 	struct usb_device_request req;
255 
256 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
257 	req.bRequest = breq;
258 	USETW(req.wValue, word);
259 	USETW(req.wIndex, 0);
260 	USETW(req.wLength, 0);
261 
262 	return (kue_do_request(sc, &req, NULL));
263 }
264 
265 static int
266 kue_ctl(struct kue_softc *sc, uint8_t rw, uint8_t breq,
267     uint16_t val, void *data, int len)
268 {
269 	struct usb_device_request req;
270 
271 	if (rw == KUE_CTL_WRITE)
272 		req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
273 	else
274 		req.bmRequestType = UT_READ_VENDOR_DEVICE;
275 
276 
277 	req.bRequest = breq;
278 	USETW(req.wValue, val);
279 	USETW(req.wIndex, 0);
280 	USETW(req.wLength, len);
281 
282 	return (kue_do_request(sc, &req, data));
283 }
284 
285 static int
286 kue_load_fw(struct kue_softc *sc)
287 {
288 	struct usb_device_descriptor *dd;
289 	uint16_t hwrev;
290 	usb_error_t err;
291 
292 	dd = usbd_get_device_descriptor(sc->sc_ue.ue_udev);
293 	hwrev = UGETW(dd->bcdDevice);
294 
295 	/*
296 	 * First, check if we even need to load the firmware.
297 	 * If the device was still attached when the system was
298 	 * rebooted, it may already have firmware loaded in it.
299 	 * If this is the case, we don't need to do it again.
300 	 * And in fact, if we try to load it again, we'll hang,
301 	 * so we have to avoid this condition if we don't want
302 	 * to look stupid.
303 	 *
304 	 * We can test this quickly by checking the bcdRevision
305 	 * code. The NIC will return a different revision code if
306 	 * it's probed while the firmware is still loaded and
307 	 * running.
308 	 */
309 	if (hwrev == 0x0202)
310 		return(0);
311 
312 	/* Load code segment */
313 	err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
314 	    0, kue_code_seg, sizeof(kue_code_seg));
315 	if (err) {
316 		device_printf(sc->sc_ue.ue_dev, "failed to load code segment: %s\n",
317 		    usbd_errstr(err));
318 		return(ENXIO);
319 	}
320 
321 	/* Load fixup segment */
322 	err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
323 	    0, kue_fix_seg, sizeof(kue_fix_seg));
324 	if (err) {
325 		device_printf(sc->sc_ue.ue_dev, "failed to load fixup segment: %s\n",
326 		    usbd_errstr(err));
327 		return(ENXIO);
328 	}
329 
330 	/* Send trigger command. */
331 	err = kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SEND_SCAN,
332 	    0, kue_trig_seg, sizeof(kue_trig_seg));
333 	if (err) {
334 		device_printf(sc->sc_ue.ue_dev, "failed to load trigger segment: %s\n",
335 		    usbd_errstr(err));
336 		return(ENXIO);
337 	}
338 
339 	return (0);
340 }
341 
342 static void
343 kue_setpromisc(struct usb_ether *ue)
344 {
345 	struct kue_softc *sc = uether_getsc(ue);
346 	struct ifnet *ifp = uether_getifp(ue);
347 
348 	KUE_LOCK_ASSERT(sc, MA_OWNED);
349 
350 	if (ifp->if_flags & IFF_PROMISC)
351 		sc->sc_rxfilt |= KUE_RXFILT_PROMISC;
352 	else
353 		sc->sc_rxfilt &= ~KUE_RXFILT_PROMISC;
354 
355 	kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->sc_rxfilt);
356 }
357 
358 static void
359 kue_setmulti(struct usb_ether *ue)
360 {
361 	struct kue_softc *sc = uether_getsc(ue);
362 	struct ifnet *ifp = uether_getifp(ue);
363 	struct ifmultiaddr *ifma;
364 	int i = 0;
365 
366 	KUE_LOCK_ASSERT(sc, MA_OWNED);
367 
368 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
369 		sc->sc_rxfilt |= KUE_RXFILT_ALLMULTI;
370 		sc->sc_rxfilt &= ~KUE_RXFILT_MULTICAST;
371 		kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->sc_rxfilt);
372 		return;
373 	}
374 
375 	sc->sc_rxfilt &= ~KUE_RXFILT_ALLMULTI;
376 
377 	if_maddr_rlock(ifp);
378 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
379 	{
380 		if (ifma->ifma_addr->sa_family != AF_LINK)
381 			continue;
382 		/*
383 		 * If there are too many addresses for the
384 		 * internal filter, switch over to allmulti mode.
385 		 */
386 		if (i == KUE_MCFILTCNT(sc))
387 			break;
388 		memcpy(KUE_MCFILT(sc, i),
389 		    LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
390 		    ETHER_ADDR_LEN);
391 		i++;
392 	}
393 	if_maddr_runlock(ifp);
394 
395 	if (i == KUE_MCFILTCNT(sc))
396 		sc->sc_rxfilt |= KUE_RXFILT_ALLMULTI;
397 	else {
398 		sc->sc_rxfilt |= KUE_RXFILT_MULTICAST;
399 		kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SET_MCAST_FILTERS,
400 		    i, sc->sc_mcfilters, i * ETHER_ADDR_LEN);
401 	}
402 
403 	kue_setword(sc, KUE_CMD_SET_PKT_FILTER, sc->sc_rxfilt);
404 }
405 
406 /*
407  * Issue a SET_CONFIGURATION command to reset the MAC. This should be
408  * done after the firmware is loaded into the adapter in order to
409  * bring it into proper operation.
410  */
411 static void
412 kue_reset(struct kue_softc *sc)
413 {
414 	struct usb_config_descriptor *cd;
415 	usb_error_t err;
416 
417 	cd = usbd_get_config_descriptor(sc->sc_ue.ue_udev);
418 
419 	err = usbd_req_set_config(sc->sc_ue.ue_udev, &sc->sc_mtx,
420 	    cd->bConfigurationValue);
421 	if (err)
422 		DPRINTF("reset failed (ignored)\n");
423 
424 	/* wait a little while for the chip to get its brains in order */
425 	uether_pause(&sc->sc_ue, hz / 100);
426 }
427 
428 static void
429 kue_attach_post(struct usb_ether *ue)
430 {
431 	struct kue_softc *sc = uether_getsc(ue);
432 	int error;
433 
434 	/* load the firmware into the NIC */
435 	error = kue_load_fw(sc);
436 	if (error) {
437 		device_printf(sc->sc_ue.ue_dev, "could not load firmware\n");
438 		/* ignore the error */
439 	}
440 
441 	/* reset the adapter */
442 	kue_reset(sc);
443 
444 	/* read ethernet descriptor */
445 	kue_ctl(sc, KUE_CTL_READ, KUE_CMD_GET_ETHER_DESCRIPTOR,
446 	    0, &sc->sc_desc, sizeof(sc->sc_desc));
447 
448 	/* copy in ethernet address */
449 	memcpy(ue->ue_eaddr, sc->sc_desc.kue_macaddr, sizeof(ue->ue_eaddr));
450 }
451 
452 /*
453  * Probe for a KLSI chip.
454  */
455 static int
456 kue_probe(device_t dev)
457 {
458 	struct usb_attach_arg *uaa = device_get_ivars(dev);
459 
460 	if (uaa->usb_mode != USB_MODE_HOST)
461 		return (ENXIO);
462 	if (uaa->info.bConfigIndex != KUE_CONFIG_IDX)
463 		return (ENXIO);
464 	if (uaa->info.bIfaceIndex != KUE_IFACE_IDX)
465 		return (ENXIO);
466 
467 	return (usbd_lookup_id_by_uaa(kue_devs, sizeof(kue_devs), uaa));
468 }
469 
470 /*
471  * Attach the interface. Allocate softc structures, do
472  * setup and ethernet/BPF attach.
473  */
474 static int
475 kue_attach(device_t dev)
476 {
477 	struct usb_attach_arg *uaa = device_get_ivars(dev);
478 	struct kue_softc *sc = device_get_softc(dev);
479 	struct usb_ether *ue = &sc->sc_ue;
480 	uint8_t iface_index;
481 	int error;
482 
483 	device_set_usb_desc(dev);
484 	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
485 
486 	iface_index = KUE_IFACE_IDX;
487 	error = usbd_transfer_setup(uaa->device, &iface_index,
488 	    sc->sc_xfer, kue_config, KUE_N_TRANSFER, sc, &sc->sc_mtx);
489 	if (error) {
490 		device_printf(dev, "allocating USB transfers failed\n");
491 		goto detach;
492 	}
493 
494 	sc->sc_mcfilters = malloc(KUE_MCFILTCNT(sc) * ETHER_ADDR_LEN,
495 	    M_USBDEV, M_WAITOK);
496 	if (sc->sc_mcfilters == NULL) {
497 		device_printf(dev, "failed allocating USB memory\n");
498 		goto detach;
499 	}
500 
501 	ue->ue_sc = sc;
502 	ue->ue_dev = dev;
503 	ue->ue_udev = uaa->device;
504 	ue->ue_mtx = &sc->sc_mtx;
505 	ue->ue_methods = &kue_ue_methods;
506 
507 	error = uether_ifattach(ue);
508 	if (error) {
509 		device_printf(dev, "could not attach interface\n");
510 		goto detach;
511 	}
512 	return (0);			/* success */
513 
514 detach:
515 	kue_detach(dev);
516 	return (ENXIO);			/* failure */
517 }
518 
519 static int
520 kue_detach(device_t dev)
521 {
522 	struct kue_softc *sc = device_get_softc(dev);
523 	struct usb_ether *ue = &sc->sc_ue;
524 
525 	usbd_transfer_unsetup(sc->sc_xfer, KUE_N_TRANSFER);
526 	uether_ifdetach(ue);
527 	mtx_destroy(&sc->sc_mtx);
528 	free(sc->sc_mcfilters, M_USBDEV);
529 
530 	return (0);
531 }
532 
533 /*
534  * A frame has been uploaded: pass the resulting mbuf chain up to
535  * the higher level protocols.
536  */
537 static void
538 kue_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
539 {
540 	struct kue_softc *sc = usbd_xfer_softc(xfer);
541 	struct usb_ether *ue = &sc->sc_ue;
542 	struct ifnet *ifp = uether_getifp(ue);
543 	struct usb_page_cache *pc;
544 	uint8_t buf[2];
545 	int len;
546 	int actlen;
547 
548 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
549 
550 	switch (USB_GET_STATE(xfer)) {
551 	case USB_ST_TRANSFERRED:
552 
553 		if (actlen <= (int)(2 + sizeof(struct ether_header))) {
554 			if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
555 			goto tr_setup;
556 		}
557 		pc = usbd_xfer_get_frame(xfer, 0);
558 		usbd_copy_out(pc, 0, buf, 2);
559 		actlen -= 2;
560 		len = buf[0] | (buf[1] << 8);
561 		len = min(actlen, len);
562 
563 		uether_rxbuf(ue, pc, 2, len);
564 		/* FALLTHROUGH */
565 	case USB_ST_SETUP:
566 tr_setup:
567 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
568 		usbd_transfer_submit(xfer);
569 		uether_rxflush(ue);
570 		return;
571 
572 	default:			/* Error */
573 		DPRINTF("bulk read error, %s\n",
574 		    usbd_errstr(error));
575 
576 		if (error != USB_ERR_CANCELLED) {
577 			/* try to clear stall first */
578 			usbd_xfer_set_stall(xfer);
579 			goto tr_setup;
580 		}
581 		return;
582 
583 	}
584 }
585 
586 static void
587 kue_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
588 {
589 	struct kue_softc *sc = usbd_xfer_softc(xfer);
590 	struct ifnet *ifp = uether_getifp(&sc->sc_ue);
591 	struct usb_page_cache *pc;
592 	struct mbuf *m;
593 	int total_len;
594 	int temp_len;
595 	uint8_t buf[2];
596 
597 	switch (USB_GET_STATE(xfer)) {
598 	case USB_ST_TRANSFERRED:
599 		DPRINTFN(11, "transfer complete\n");
600 		if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
601 
602 		/* FALLTHROUGH */
603 	case USB_ST_SETUP:
604 tr_setup:
605 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
606 
607 		if (m == NULL)
608 			return;
609 		if (m->m_pkthdr.len > MCLBYTES)
610 			m->m_pkthdr.len = MCLBYTES;
611 		temp_len = (m->m_pkthdr.len + 2);
612 		total_len = (temp_len + (64 - (temp_len % 64)));
613 
614 		/* the first two bytes are the frame length */
615 
616 		buf[0] = (uint8_t)(m->m_pkthdr.len);
617 		buf[1] = (uint8_t)(m->m_pkthdr.len >> 8);
618 
619 		pc = usbd_xfer_get_frame(xfer, 0);
620 		usbd_copy_in(pc, 0, buf, 2);
621 		usbd_m_copy_in(pc, 2, m, 0, m->m_pkthdr.len);
622 
623 		usbd_frame_zero(pc, temp_len, total_len - temp_len);
624 		usbd_xfer_set_frame_len(xfer, 0, total_len);
625 
626 		/*
627 		 * if there's a BPF listener, bounce a copy
628 		 * of this frame to him:
629 		 */
630 		BPF_MTAP(ifp, m);
631 
632 		m_freem(m);
633 
634 		usbd_transfer_submit(xfer);
635 
636 		return;
637 
638 	default:			/* Error */
639 		DPRINTFN(11, "transfer error, %s\n",
640 		    usbd_errstr(error));
641 
642 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
643 
644 		if (error != USB_ERR_CANCELLED) {
645 			/* try to clear stall first */
646 			usbd_xfer_set_stall(xfer);
647 			goto tr_setup;
648 		}
649 		return;
650 
651 	}
652 }
653 
654 static void
655 kue_start(struct usb_ether *ue)
656 {
657 	struct kue_softc *sc = uether_getsc(ue);
658 
659 	/*
660 	 * start the USB transfers, if not already started:
661 	 */
662 	usbd_transfer_start(sc->sc_xfer[KUE_BULK_DT_RD]);
663 	usbd_transfer_start(sc->sc_xfer[KUE_BULK_DT_WR]);
664 }
665 
666 static void
667 kue_init(struct usb_ether *ue)
668 {
669 	struct kue_softc *sc = uether_getsc(ue);
670 	struct ifnet *ifp = uether_getifp(ue);
671 
672 	KUE_LOCK_ASSERT(sc, MA_OWNED);
673 
674 	/* set MAC address */
675 	kue_ctl(sc, KUE_CTL_WRITE, KUE_CMD_SET_MAC,
676 	    0, IF_LLADDR(ifp), ETHER_ADDR_LEN);
677 
678 	/* I'm not sure how to tune these. */
679 #if 0
680 	/*
681 	 * Leave this one alone for now; setting it
682 	 * wrong causes lockups on some machines/controllers.
683 	 */
684 	kue_setword(sc, KUE_CMD_SET_SOFS, 1);
685 #endif
686 	kue_setword(sc, KUE_CMD_SET_URB_SIZE, 64);
687 
688 	/* load the multicast filter */
689 	kue_setpromisc(ue);
690 
691 	usbd_xfer_set_stall(sc->sc_xfer[KUE_BULK_DT_WR]);
692 
693 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
694 	kue_start(ue);
695 }
696 
697 static void
698 kue_stop(struct usb_ether *ue)
699 {
700 	struct kue_softc *sc = uether_getsc(ue);
701 	struct ifnet *ifp = uether_getifp(ue);
702 
703 	KUE_LOCK_ASSERT(sc, MA_OWNED);
704 
705 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
706 
707 	/*
708 	 * stop all the transfers, if not already stopped:
709 	 */
710 	usbd_transfer_stop(sc->sc_xfer[KUE_BULK_DT_WR]);
711 	usbd_transfer_stop(sc->sc_xfer[KUE_BULK_DT_RD]);
712 }
713