aboutsummaryrefslogtreecommitdiff
path: root/sys/dev/usb/net/if_cdceem.c
blob: 92441d8173eec7585d5908b075a277de294ba4a5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
/*-
 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
 *
 * Copyright (C) 2012 Ben Gray <bgray@freebsd.org>.
 * Copyright (C) 2018 The FreeBSD Foundation.
 * Copyright (c) 2019 Edward Tomasz Napierala <trasz@FreeBSD.org>
 *
 * This software was developed by Arshan Khanifar <arshankhanifar@gmail.com>
 * under sponsorship from the FreeBSD Foundation.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/*
 * Universal Serial Bus Communications Class Subclass Specification
 * for Ethernet Emulation Model Devices:
 *
 * https://usb.org/sites/default/files/CDC_EEM10.pdf
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/gsb_crc32.h>
#include <sys/eventhandler.h>
#include <sys/stdint.h>
#include <sys/stddef.h>
#include <sys/queue.h>
#include <sys/systm.h>
#include <sys/socket.h>
#include <sys/kernel.h>
#include <sys/bus.h>
#include <sys/module.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/condvar.h>
#include <sys/sysctl.h>
#include <sys/sx.h>
#include <sys/unistd.h>
#include <sys/callout.h>
#include <sys/malloc.h>
#include <sys/priv.h>

#include <net/if.h>
#include <net/if_var.h>

#include <dev/usb/usb.h>
#include <dev/usb/usbdi.h>
#include <dev/usb/usbdi_util.h>
#include <dev/usb/usb_cdc.h>
#include "usbdevs.h"

#define	USB_DEBUG_VAR cdceem_debug
#include <dev/usb/usb_debug.h>
#include <dev/usb/usb_process.h>
#include <dev/usb/usb_msctest.h>
#include "usb_if.h"

#include <dev/usb/net/usb_ethernet.h>

#define	CDCEEM_FRAMES_MAX	1
#define	CDCEEM_ECHO_MAX		1024

#define	CDCEEM_ECHO_PAYLOAD	\
    "ICH DALEKOPIS FALSZUJE GDY PROBY XQV NIE WYTRZYMUJE 1234567890"

enum {
	CDCEEM_BULK_RX,
	CDCEEM_BULK_TX,
	CDCEEM_N_TRANSFER,
};

struct cdceem_softc {
	struct usb_ether	sc_ue;
	struct mtx		sc_mtx;
	int			sc_flags;
	struct usb_xfer		*sc_xfer[CDCEEM_N_TRANSFER];
	size_t			sc_echo_len;
	char			sc_echo_buffer[CDCEEM_ECHO_MAX];
};

#define	CDCEEM_SC_FLAGS_ECHO_RESPONSE_PENDING	0x1
#define	CDCEEM_SC_FLAGS_ECHO_PENDING		0x2

static SYSCTL_NODE(_hw_usb, OID_AUTO, cdceem, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
    "USB CDC EEM");
static int cdceem_debug = 1;
SYSCTL_INT(_hw_usb_cdceem, OID_AUTO, debug, CTLFLAG_RWTUN,
    &cdceem_debug, 0, "Debug level");
static int cdceem_send_echoes = 0;
SYSCTL_INT(_hw_usb_cdceem, OID_AUTO, send_echoes, CTLFLAG_RWTUN,
    &cdceem_send_echoes, 0, "Send an Echo command");
static int cdceem_send_fake_crc = 0;
SYSCTL_INT(_hw_usb_cdceem, OID_AUTO, send_fake_crc, CTLFLAG_RWTUN,
    &cdceem_send_fake_crc, 0, "Use 0xdeadbeef instead of CRC");

#define	CDCEEM_DEBUG(S, X, ...)						\
	do {								\
		if (cdceem_debug > 1) {					\
			device_printf(S->sc_ue.ue_dev, "%s: " X "\n",	\
			    __func__, ## __VA_ARGS__);			\
		}							\
	} while (0)

#define	CDCEEM_WARN(S, X, ...)						\
	do {								\
		if (cdceem_debug > 0) {					\
			device_printf(S->sc_ue.ue_dev,			\
			    "WARNING: %s: " X "\n",			\
			    __func__, ## __VA_ARGS__);			\
		}							\
	} while (0)

#define	CDCEEM_LOCK(X)				mtx_lock(&(X)->sc_mtx)
#define	CDCEEM_UNLOCK(X)			mtx_unlock(&(X)->sc_mtx)

#define	CDCEEM_TYPE_CMD				(0x1 << 15)

#define	CDCEEM_CMD_MASK				(0x7 << 11)

#define	CDCEEM_CMD_ECHO				(0x0 << 11)
#define	CDCEEM_CMD_ECHO_RESPONSE		(0x1 << 11)
#define	CDCEEM_CMD_SUSPEND_HINT			(0x2 << 11)
#define	CDCEEM_CMD_RESPONSE_HINT		(0x3 << 11)
#define	CDCEEM_CMD_RESPONSE_COMPLETE_HINT	(0x4 << 11)
#define	CDCEEM_CMD_TICKLE			(0x5 << 11)

#define	CDCEEM_CMD_RESERVED			(0x1 << 14)

#define	CDCEEM_ECHO_LEN_MASK			0x3ff

#define	CDCEEM_DATA_CRC				(0x1 << 14)
#define	CDCEEM_DATA_LEN_MASK			0x3fff

static device_probe_t cdceem_probe;
static device_attach_t cdceem_attach;
static device_detach_t cdceem_detach;
static device_suspend_t cdceem_suspend;
static device_resume_t cdceem_resume;

static usb_callback_t cdceem_bulk_write_callback;
static usb_callback_t cdceem_bulk_read_callback;

static uether_fn_t cdceem_attach_post;
static uether_fn_t cdceem_init;
static uether_fn_t cdceem_stop;
static uether_fn_t cdceem_start;
static uether_fn_t cdceem_setmulti;
static uether_fn_t cdceem_setpromisc;

static uint32_t	cdceem_m_crc32(struct mbuf *, uint32_t, uint32_t);

static const struct usb_config cdceem_config[CDCEEM_N_TRANSFER] = {
	[CDCEEM_BULK_TX] = {
		.type = UE_BULK,
		.endpoint = UE_ADDR_ANY,
		.direction = UE_DIR_TX,
		.bufsize = 16 * (MCLBYTES + 16),
		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
		.callback = cdceem_bulk_write_callback,
		.timeout = 10000,	/* 10 seconds */
		.usb_mode = USB_MODE_DUAL,
	},

	[CDCEEM_BULK_RX] = {
		.type = UE_BULK,
		.endpoint = UE_ADDR_ANY,
		.direction = UE_DIR_RX,
		.bufsize = 20480,	/* bytes */
		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
		.callback = cdceem_bulk_read_callback,
		.timeout = 0,	/* no timeout */
		.usb_mode = USB_MODE_DUAL,
	},
};

static device_method_t cdceem_methods[] = {
	/* Device interface */
	DEVMETHOD(device_probe, cdceem_probe),
	DEVMETHOD(device_attach, cdceem_attach),
	DEVMETHOD(device_detach, cdceem_detach),
	DEVMETHOD(device_suspend, cdceem_suspend),
	DEVMETHOD(device_resume, cdceem_resume),

	DEVMETHOD_END
};

static driver_t cdceem_driver = {
	.name = "cdceem",
	.methods = cdceem_methods,
	.size = sizeof(struct cdceem_softc),
};

static devclass_t cdceem_devclass;

static const STRUCT_USB_DUAL_ID cdceem_dual_devs[] = {
	{USB_IFACE_CLASS(UICLASS_CDC),
		USB_IFACE_SUBCLASS(UISUBCLASS_ETHERNET_EMULATION_MODEL),
		0},
};

DRIVER_MODULE(cdceem, uhub, cdceem_driver, cdceem_devclass, NULL, NULL);
MODULE_VERSION(cdceem, 1);
MODULE_DEPEND(cdceem, uether, 1, 1, 1);
MODULE_DEPEND(cdceem, usb, 1, 1, 1);
MODULE_DEPEND(cdceem, ether, 1, 1, 1);
USB_PNP_DUAL_INFO(cdceem_dual_devs);

static const struct usb_ether_methods cdceem_ue_methods = {
	.ue_attach_post = cdceem_attach_post,
	.ue_start = cdceem_start,
	.ue_init = cdceem_init,
	.ue_stop = cdceem_stop,
	.ue_setmulti = cdceem_setmulti,
	.ue_setpromisc = cdceem_setpromisc,
};

static int
cdceem_probe(device_t dev)
{
	struct usb_attach_arg *uaa;
	int error;

	uaa = device_get_ivars(dev);
	error = usbd_lookup_id_by_uaa(cdceem_dual_devs,
	    sizeof(cdceem_dual_devs), uaa);

	return (error);
}

static void
cdceem_attach_post(struct usb_ether *ue)
{

	return;
}

static int
cdceem_attach(device_t dev)
{
	struct cdceem_softc *sc;
	struct usb_ether *ue;
	struct usb_attach_arg *uaa;
	int error;
	uint8_t iface_index;

	sc = device_get_softc(dev);
	ue = &sc->sc_ue;
	uaa = device_get_ivars(dev);

	device_set_usb_desc(dev);

	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);

	/* Setup the endpoints. */
	iface_index = 0;
	error = usbd_transfer_setup(uaa->device, &iface_index, sc->sc_xfer,
	    cdceem_config, CDCEEM_N_TRANSFER, sc, &sc->sc_mtx);
	if (error != 0) {
		CDCEEM_WARN(sc,
		    "allocating USB transfers failed, error %d", error);
		mtx_destroy(&sc->sc_mtx);
		return (error);
	}

	/* Random MAC address. */
	arc4rand(ue->ue_eaddr, ETHER_ADDR_LEN, 0);
	ue->ue_eaddr[0] &= ~0x01;	/* unicast */
	ue->ue_eaddr[0] |= 0x02;	/* locally administered */

	ue->ue_sc = sc;
	ue->ue_dev = dev;
	ue->ue_udev = uaa->device;
	ue->ue_mtx = &sc->sc_mtx;
	ue->ue_methods = &cdceem_ue_methods;

	error = uether_ifattach(ue);
	if (error != 0) {
		CDCEEM_WARN(sc, "could not attach interface, error %d", error);
		usbd_transfer_unsetup(sc->sc_xfer, CDCEEM_N_TRANSFER);
		mtx_destroy(&sc->sc_mtx);
		return (error);
	}

	return (0);
}

static int
cdceem_detach(device_t dev)
{
	struct cdceem_softc *sc = device_get_softc(dev);
	struct usb_ether *ue = &sc->sc_ue;

	/* Stop all USB transfers first. */
	usbd_transfer_unsetup(sc->sc_xfer, CDCEEM_N_TRANSFER);
	uether_ifdetach(ue);
	mtx_destroy(&sc->sc_mtx);

	return (0);
}

static void
cdceem_handle_cmd(struct usb_xfer *xfer, uint16_t hdr, int *offp)
{
	struct cdceem_softc *sc;
	struct usb_page_cache *pc;
	int actlen, off;
	uint16_t pktlen;

	off = *offp;
	sc = usbd_xfer_softc(xfer);
	pc = usbd_xfer_get_frame(xfer, 0);
	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);

	if (hdr & CDCEEM_CMD_RESERVED) {
		CDCEEM_WARN(sc, "received command header %#x "
		    "with Reserved bit set; ignoring", hdr);
		return;
	}

	switch (hdr & CDCEEM_CMD_MASK) {
	case CDCEEM_CMD_ECHO:
		pktlen = hdr & CDCEEM_ECHO_LEN_MASK;
		CDCEEM_DEBUG(sc, "received Echo, length %d", pktlen);

		if (pktlen > (actlen - off)) {
			CDCEEM_WARN(sc,
			    "bad Echo length %d, should be at most %d",
			    pktlen, actlen - off);
			break;
		}

		if (pktlen > sizeof(sc->sc_echo_buffer)) {
			CDCEEM_WARN(sc,
			    "Echo length %u too big, must be less than %zd",
			    pktlen, sizeof(sc->sc_echo_buffer));
			break;
		}

		sc->sc_flags |= CDCEEM_SC_FLAGS_ECHO_RESPONSE_PENDING;
		sc->sc_echo_len = pktlen;
		usbd_copy_out(pc, off, sc->sc_echo_buffer, pktlen);
		off += pktlen;
		break;

	case CDCEEM_CMD_ECHO_RESPONSE:
		pktlen = hdr & CDCEEM_ECHO_LEN_MASK;
		CDCEEM_DEBUG(sc, "received Echo Response, length %d", pktlen);

		if (pktlen > (actlen - off)) {
			CDCEEM_WARN(sc,
			    "bad Echo Response length %d, "
			    "should be at most %d",
			    pktlen, actlen - off);
			break;
		}

		if (pktlen != sizeof(CDCEEM_ECHO_PAYLOAD)) {
			CDCEEM_WARN(sc, "received Echo Response with bad "
			    "length %hu, should be %zd",
			    pktlen, sizeof(CDCEEM_ECHO_PAYLOAD));
			break;
		}

		usbd_copy_out(pc, off, sc->sc_echo_buffer, pktlen);
		off += pktlen;

		if (memcmp(sc->sc_echo_buffer, CDCEEM_ECHO_PAYLOAD,
		    sizeof(CDCEEM_ECHO_PAYLOAD)) != 0) {
			CDCEEM_WARN(sc,
			    "received Echo Response payload does not match");
		} else {
			CDCEEM_DEBUG(sc, "received Echo Response is valid");
		}
		break;

	case CDCEEM_CMD_SUSPEND_HINT:
		CDCEEM_DEBUG(sc, "received SuspendHint; ignoring");
		break;

	case CDCEEM_CMD_RESPONSE_HINT:
		CDCEEM_DEBUG(sc, "received ResponseHint; ignoring");
		break;

	case CDCEEM_CMD_RESPONSE_COMPLETE_HINT:
		CDCEEM_DEBUG(sc, "received ResponseCompleteHint; ignoring");
		break;

	case CDCEEM_CMD_TICKLE:
		CDCEEM_DEBUG(sc, "received Tickle; ignoring");
		break;

	default:
		CDCEEM_WARN(sc,
		    "received unknown command %u, header %#x; ignoring",
		    (hdr & CDCEEM_CMD_MASK >> 11), hdr);
		break;
	}

	*offp = off;
}

static void
cdceem_handle_data(struct usb_xfer *xfer, uint16_t hdr, int *offp)
{
	struct cdceem_softc *sc;
	struct usb_page_cache *pc;
	struct usb_ether *ue;
	struct ifnet *ifp;
	struct mbuf *m;
	uint32_t computed_crc, received_crc;
	int pktlen;
	int actlen;
	int off;

	off = *offp;
	sc = usbd_xfer_softc(xfer);
	pc = usbd_xfer_get_frame(xfer, 0);
	ue = &sc->sc_ue;
	ifp = uether_getifp(ue);
	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);

	pktlen = hdr & CDCEEM_DATA_LEN_MASK;
	CDCEEM_DEBUG(sc, "received Data, CRC %s, length %d",
	    (hdr & CDCEEM_DATA_CRC) ? "valid" : "absent",
	    pktlen);

	if (pktlen < (ETHER_HDR_LEN + 4)) {
		CDCEEM_WARN(sc,
		    "bad ethernet frame length %d, should be at least %d",
		    pktlen, ETHER_HDR_LEN);
		if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
		return;
	}

	if (pktlen > (actlen - off)) {
		CDCEEM_WARN(sc,
		    "bad ethernet frame length %d, should be at most %d",
		    pktlen, actlen - off);
		if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
		return;
	}

	m = uether_newbuf();
	if (m == NULL) {
		CDCEEM_WARN(sc, "uether_newbuf() failed");
		if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
		return;
	}

	pktlen -= 4; /* Subtract the CRC. */

	if (pktlen > m->m_len) {
		CDCEEM_WARN(sc, "buffer too small %d vs %d bytes",
		    pktlen, m->m_len);
		if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
		m_freem(m);
		return;
	}
	usbd_copy_out(pc, off, mtod(m, uint8_t *), pktlen);
	off += pktlen;

	usbd_copy_out(pc, off, &received_crc, sizeof(received_crc));
	off += sizeof(received_crc);

	if (hdr & CDCEEM_DATA_CRC) {
		computed_crc = cdceem_m_crc32(m, 0, pktlen);
	} else {
		computed_crc = be32toh(0xdeadbeef);
	}

	if (received_crc != computed_crc) {
		CDCEEM_WARN(sc,
		    "received Data packet with wrong CRC %#x, expected %#x",
		    received_crc, computed_crc);
		if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
		m_freem(m);
		return;
	} else {
		CDCEEM_DEBUG(sc, "received correct CRC %#x", received_crc);
	}

	uether_rxmbuf(ue, m, pktlen);
	*offp = off;
}

static void
cdceem_bulk_read_callback(struct usb_xfer *xfer, usb_error_t usb_error)
{
	struct cdceem_softc *sc;
	struct usb_page_cache *pc;
	int actlen, aframes, off;
	uint16_t hdr;

	sc = usbd_xfer_softc(xfer);
	usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL);

	switch (USB_GET_STATE(xfer)) {
	case USB_ST_TRANSFERRED:
		CDCEEM_DEBUG(sc,
		    "received %u bytes in %u frames", actlen, aframes);

		pc = usbd_xfer_get_frame(xfer, 0);
		off = 0;

		while ((off + sizeof(hdr)) <= actlen) {
			usbd_copy_out(pc, off, &hdr, sizeof(hdr));
			CDCEEM_DEBUG(sc, "hdr = %#x", hdr);
			off += sizeof(hdr);

			if (hdr == 0) {
				CDCEEM_DEBUG(sc, "received Zero Length EEM");
				continue;
			}

			hdr = le16toh(hdr);

			if ((hdr & CDCEEM_TYPE_CMD) != 0) {
				cdceem_handle_cmd(xfer, hdr, &off);
			} else {
				cdceem_handle_data(xfer, hdr, &off);
			}

			KASSERT(off <= actlen,
			    ("%s: went past the buffer, off %d, actlen %d",
			     __func__, off, actlen));
		}

		/* FALLTHROUGH */
	case USB_ST_SETUP:
		CDCEEM_DEBUG(sc, "setup");
tr_setup:
		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
		usbd_transfer_submit(xfer);
		uether_rxflush(&sc->sc_ue);
		break;

	default:
		CDCEEM_WARN(sc, "USB_ST_ERROR: %s", usbd_errstr(usb_error));

		if (usb_error != USB_ERR_CANCELLED) {
			/* try to clear stall first */
			usbd_xfer_set_stall(xfer);
			goto tr_setup;
		}
		break;
	}
}

static void
cdceem_send_echo(struct usb_xfer *xfer, int *offp)
{
	struct cdceem_softc *sc;
	struct usb_page_cache *pc;
	int maxlen, off;
	uint16_t hdr;

	off = *offp;
	sc = usbd_xfer_softc(xfer);
	pc = usbd_xfer_get_frame(xfer, 0);
	maxlen = usbd_xfer_max_len(xfer);

	CDCEEM_DEBUG(sc, "sending Echo, length %zd",
	    sizeof(CDCEEM_ECHO_PAYLOAD));

	KASSERT(off + sizeof(hdr) + sizeof(CDCEEM_ECHO_PAYLOAD) < maxlen,
	    ("%s: out of space; have %d, need %zd", __func__, maxlen,
	    off + sizeof(hdr) + sizeof(CDCEEM_ECHO_PAYLOAD)));

	hdr = 0;
	hdr |= CDCEEM_TYPE_CMD;
	hdr |= CDCEEM_CMD_ECHO;
	hdr |= sizeof(CDCEEM_ECHO_PAYLOAD);
	CDCEEM_DEBUG(sc, "hdr = %#x", hdr);
	hdr = htole16(hdr);

	usbd_copy_in(pc, off, &hdr, sizeof(hdr));
	off += sizeof(hdr);

	usbd_copy_in(pc, off, CDCEEM_ECHO_PAYLOAD,
	    sizeof(CDCEEM_ECHO_PAYLOAD));
	off += sizeof(CDCEEM_ECHO_PAYLOAD);

	sc->sc_flags &= ~CDCEEM_SC_FLAGS_ECHO_PENDING;

	*offp = off;
}

static void
cdceem_send_echo_response(struct usb_xfer *xfer, int *offp)
{
	struct cdceem_softc *sc;
	struct usb_page_cache *pc;
	int maxlen, off;
	uint16_t hdr;

	off = *offp;
	sc = usbd_xfer_softc(xfer);
	pc = usbd_xfer_get_frame(xfer, 0);
	maxlen = usbd_xfer_max_len(xfer);

	KASSERT(off + sizeof(hdr) + sc->sc_echo_len < maxlen,
	    ("%s: out of space; have %d, need %zd", __func__, maxlen,
	    off + sizeof(hdr) + sc->sc_echo_len));

	CDCEEM_DEBUG(sc, "sending Echo Response, length %zd", sc->sc_echo_len);

	hdr = 0;
	hdr |= CDCEEM_TYPE_CMD;
	hdr |= CDCEEM_CMD_ECHO_RESPONSE;
	hdr |= sc->sc_echo_len;
	CDCEEM_DEBUG(sc, "hdr = %#x", hdr);
	hdr = htole16(hdr);

	usbd_copy_in(pc, off, &hdr, sizeof(hdr));
	off += sizeof(hdr);

	usbd_copy_in(pc, off, sc->sc_echo_buffer, sc->sc_echo_len);
	off += sc->sc_echo_len;

	sc->sc_flags &= ~CDCEEM_SC_FLAGS_ECHO_RESPONSE_PENDING;
	sc->sc_echo_len = 0;

	*offp = off;
}

static void
cdceem_send_data(struct usb_xfer *xfer, int *offp)
{
	struct cdceem_softc *sc;
	struct usb_page_cache *pc;
	struct ifnet *ifp;
	struct mbuf *m;
	int maxlen, off;
	uint32_t crc;
	uint16_t hdr;

	off = *offp;
	sc = usbd_xfer_softc(xfer);
	pc = usbd_xfer_get_frame(xfer, 0);
	ifp = uether_getifp(&sc->sc_ue);
	maxlen = usbd_xfer_max_len(xfer);

	IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
	if (m == NULL) {
		CDCEEM_DEBUG(sc, "no Data packets to send");
		return;
	}

	KASSERT((m->m_pkthdr.len & CDCEEM_DATA_LEN_MASK) == m->m_pkthdr.len,
	    ("%s: packet too long: %d, should be %d\n", __func__,
	     m->m_pkthdr.len, m->m_pkthdr.len & CDCEEM_DATA_LEN_MASK));
	KASSERT(off + sizeof(hdr) + m->m_pkthdr.len + 4 < maxlen,
	    ("%s: out of space; have %d, need %zd", __func__, maxlen,
	    off + sizeof(hdr) + m->m_pkthdr.len + 4));

	CDCEEM_DEBUG(sc, "sending Data, length %d + 4", m->m_pkthdr.len);

	hdr = 0;
	if (!cdceem_send_fake_crc)
		hdr |= CDCEEM_DATA_CRC;
	hdr |= (m->m_pkthdr.len + 4); /* +4 for CRC */
	CDCEEM_DEBUG(sc, "hdr = %#x", hdr);
	hdr = htole16(hdr);

	usbd_copy_in(pc, off, &hdr, sizeof(hdr));
	off += sizeof(hdr);

	usbd_m_copy_in(pc, off, m, 0, m->m_pkthdr.len);
	off += m->m_pkthdr.len;

	if (cdceem_send_fake_crc) {
		crc = htobe32(0xdeadbeef);
	} else {
		crc = cdceem_m_crc32(m, 0, m->m_pkthdr.len);
	}
	CDCEEM_DEBUG(sc, "CRC = %#x", crc);

	usbd_copy_in(pc, off, &crc, sizeof(crc));
	off += sizeof(crc);

	if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);

	/*
	 * If there's a BPF listener, bounce a copy of this frame to it.
	 */
	BPF_MTAP(ifp, m);
	m_freem(m);

	*offp = off;
}

static void
cdceem_bulk_write_callback(struct usb_xfer *xfer, usb_error_t usb_error)
{
	struct cdceem_softc *sc;
	struct ifnet *ifp;
	int actlen, aframes, maxlen, off;

	sc = usbd_xfer_softc(xfer);
	maxlen = usbd_xfer_max_len(xfer);

	switch (USB_GET_STATE(xfer)) {
	case USB_ST_TRANSFERRED:
		usbd_xfer_status(xfer, &actlen, NULL, &aframes, NULL);
		CDCEEM_DEBUG(sc, "transferred %u bytes in %u frames",
		    actlen, aframes);

		/* FALLTHROUGH */
	case USB_ST_SETUP:
		CDCEEM_DEBUG(sc, "setup");
tr_setup:

		off = 0;
		usbd_xfer_set_frame_offset(xfer, 0, 0);

		if (sc->sc_flags & CDCEEM_SC_FLAGS_ECHO_PENDING) {
			cdceem_send_echo(xfer, &off);
		} else if (sc->sc_flags & CDCEEM_SC_FLAGS_ECHO_RESPONSE_PENDING) {
			cdceem_send_echo_response(xfer, &off);
		} else {
			cdceem_send_data(xfer, &off);
		}

		KASSERT(off <= maxlen,
		    ("%s: went past the buffer, off %d, maxlen %d",
		     __func__, off, maxlen));

		if (off > 0) {
			CDCEEM_DEBUG(sc, "starting transfer, length %d", off);
			usbd_xfer_set_frame_len(xfer, 0, off);
			usbd_transfer_submit(xfer);
		} else {
			CDCEEM_DEBUG(sc, "nothing to transfer");
		}

		break;

	default:
		CDCEEM_WARN(sc, "USB_ST_ERROR: %s", usbd_errstr(usb_error));

		ifp = uether_getifp(&sc->sc_ue);
		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);

		if (usb_error != USB_ERR_CANCELLED) {
			/* try to clear stall first */
			usbd_xfer_set_stall(xfer);
			goto tr_setup;
		}
		break;
	}
}

static int32_t
cdceem_m_crc32_cb(void *arg, void *src, uint32_t count)
{
	uint32_t *p_crc = arg;

	*p_crc = crc32_raw(src, count, *p_crc);
	return (0);
}

static uint32_t
cdceem_m_crc32(struct mbuf *m, uint32_t src_offset, uint32_t src_len)
{
	uint32_t crc = 0xFFFFFFFF;
	int error;

	error = m_apply(m, src_offset, src_len, cdceem_m_crc32_cb, &crc);
	return (crc ^ 0xFFFFFFFF);
}

static void
cdceem_start(struct usb_ether *ue)
{
	struct cdceem_softc *sc;

	sc = uether_getsc(ue);

	/*
	 * Start the USB transfers, if not already started.
	 */
	usbd_transfer_start(sc->sc_xfer[CDCEEM_BULK_RX]);
	usbd_transfer_start(sc->sc_xfer[CDCEEM_BULK_TX]);
}

static void
cdceem_init(struct usb_ether *ue)
{
	struct cdceem_softc *sc;
	struct ifnet *ifp;

	sc = uether_getsc(ue);
	ifp = uether_getifp(ue);

	ifp->if_drv_flags |= IFF_DRV_RUNNING;

	if (cdceem_send_echoes)
		sc->sc_flags = CDCEEM_SC_FLAGS_ECHO_PENDING;
	else
		sc->sc_flags = 0;

	/*
	 * Stall data write direction, which depends on USB mode.
	 *
	 * Some USB host stacks (e.g. Mac OS X) don't clears stall
	 * bit as it should, so set it in our host mode only.
	 */
	if (usbd_get_mode(sc->sc_ue.ue_udev) == USB_MODE_HOST)
		usbd_xfer_set_stall(sc->sc_xfer[CDCEEM_BULK_TX]);

	cdceem_start(ue);
}

static void
cdceem_stop(struct usb_ether *ue)
{
	struct cdceem_softc *sc;
	struct ifnet *ifp;

	sc = uether_getsc(ue);
	ifp = uether_getifp(ue);

	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;

	usbd_transfer_stop(sc->sc_xfer[CDCEEM_BULK_RX]);
	usbd_transfer_stop(sc->sc_xfer[CDCEEM_BULK_TX]);
}

static void
cdceem_setmulti(struct usb_ether *ue)
{
	/* no-op */
	return;
}

static void
cdceem_setpromisc(struct usb_ether *ue)
{
	/* no-op */
	return;
}

static int
cdceem_suspend(device_t dev)
{
	struct cdceem_softc *sc = device_get_softc(dev);

	CDCEEM_DEBUG(sc, "go");
	return (0);
}

static int
cdceem_resume(device_t dev)
{
	struct cdceem_softc *sc = device_get_softc(dev);

	CDCEEM_DEBUG(sc, "go");
	return (0);
}