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