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