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