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