summaryrefslogtreecommitdiffstats
path: root/sys/dev/usb2/ethernet/if_cdce2.c
blob: 6b56c7ea82f1d664157752841df541cd83746f6b (plain)
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
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
/*	$NetBSD: if_cdce.c,v 1.4 2004/10/24 12:50:54 augustss Exp $ */

/*-
 * Copyright (c) 1997, 1998, 1999, 2000-2003 Bill Paul <wpaul@windriver.com>
 * Copyright (c) 2003-2005 Craig Boston
 * Copyright (c) 2004 Daniel Hartmeier
 * All rights reserved.
 *
 * 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.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by Bill Paul.
 * 4. Neither the name of the author nor the names of any co-contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Bill Paul 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 Bill Paul, THE VOICES IN HIS HEAD OR
 * THE 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.
 */

/*
 * USB Communication Device Class (Ethernet Networking Control Model)
 * http://www.usb.org/developers/devclass_docs/usbcdc11.pdf
 */

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

#include <dev/usb2/include/usb2_devid.h>
#include <dev/usb2/include/usb2_standard.h>
#include <dev/usb2/include/usb2_mfunc.h>
#include <dev/usb2/include/usb2_error.h>
#include <dev/usb2/include/usb2_cdc.h>
#include <dev/usb2/include/usb2_defs.h>

#define	usb2_config_td_cc usb2_ether_cc
#define	usb2_config_td_softc cdce_softc

#define	USB_DEBUG_VAR cdce_debug

#include <dev/usb2/core/usb2_core.h>
#include <dev/usb2/core/usb2_lookup.h>
#include <dev/usb2/core/usb2_process.h>
#include <dev/usb2/core/usb2_config_td.h>
#include <dev/usb2/core/usb2_debug.h>
#include <dev/usb2/core/usb2_request.h>
#include <dev/usb2/core/usb2_busdma.h>
#include <dev/usb2/core/usb2_util.h>
#include <dev/usb2/core/usb2_parse.h>
#include <dev/usb2/core/usb2_device.h>

#include <dev/usb2/ethernet/usb2_ethernet.h>
#include <dev/usb2/ethernet/if_cdcereg.h>

static device_probe_t cdce_probe;
static device_attach_t cdce_attach;
static device_detach_t cdce_detach;
static device_shutdown_t cdce_shutdown;
static device_suspend_t cdce_suspend;
static device_resume_t cdce_resume;
static usb2_handle_request_t cdce_handle_request;

static usb2_callback_t cdce_bulk_write_callback;
static usb2_callback_t cdce_bulk_read_callback;
static usb2_callback_t cdce_intr_read_callback;
static usb2_callback_t cdce_intr_write_callback;

static void	cdce_start_cb(struct ifnet *);
static void	cdce_start_transfers(struct cdce_softc *);
static uint32_t	cdce_m_crc32(struct mbuf *, uint32_t, uint32_t);
static void	cdce_stop(struct cdce_softc *);
static int	cdce_ioctl_cb(struct ifnet *, u_long, caddr_t);
static void	cdce_init_cb(void *);
static int	cdce_ifmedia_upd_cb(struct ifnet *);
static void	cdce_ifmedia_sts_cb(struct ifnet *, struct ifmediareq *);

#if USB_DEBUG
static int cdce_debug = 0;
static int cdce_force_512x4 = 0;

SYSCTL_NODE(_hw_usb2, OID_AUTO, cdce, CTLFLAG_RW, 0, "USB cdce");
SYSCTL_INT(_hw_usb2_cdce, OID_AUTO, debug, CTLFLAG_RW, &cdce_debug, 0,
    "cdce debug level");
SYSCTL_INT(_hw_usb2_cdce, OID_AUTO, force_512x4, CTLFLAG_RW,
    &cdce_force_512x4, 0, "cdce force 512x4 protocol");
#endif

static const struct usb2_config cdce_config[CDCE_N_TRANSFER] = {

	[CDCE_BULK_A] = {
		.type = UE_BULK,
		.endpoint = UE_ADDR_ANY,
		.direction = UE_DIR_OUT,
		.if_index = 0,
		/* Host Mode */
		.mh.frames = CDCE_512X4_FRAGS_MAX + 1,
		.mh.bufsize = (CDCE_512X4_FRAMES_MAX * MCLBYTES) + sizeof(struct usb2_cdc_mf_eth_512x4_header),
		.mh.flags = {.pipe_bof = 1,.force_short_xfer = 1,.ext_buffer = 1,},
		.mh.callback = &cdce_bulk_write_callback,
		.mh.timeout = 10000,	/* 10 seconds */
		/* Device Mode */
		.md.frames = CDCE_512X4_FRAGS_MAX + 1,
		.md.bufsize = (CDCE_512X4_FRAMES_MAX * MCLBYTES) + sizeof(struct usb2_cdc_mf_eth_512x4_header),
		.md.flags = {.pipe_bof = 1,.short_xfer_ok = 1,.ext_buffer = 1,},
		.md.callback = &cdce_bulk_read_callback,
		.md.timeout = 0,	/* no timeout */
	},

	[CDCE_BULK_B] = {
		.type = UE_BULK,
		.endpoint = UE_ADDR_ANY,
		.direction = UE_DIR_IN,
		.if_index = 0,
		/* Host Mode */
		.mh.frames = CDCE_512X4_FRAGS_MAX + 1,
		.mh.bufsize = (CDCE_512X4_FRAMES_MAX * MCLBYTES) + sizeof(struct usb2_cdc_mf_eth_512x4_header),
		.mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,.ext_buffer = 1,},
		.mh.callback = &cdce_bulk_read_callback,
		.mh.timeout = 0,	/* no timeout */
		/* Device Mode */
		.md.frames = CDCE_512X4_FRAGS_MAX + 1,
		.md.bufsize = (CDCE_512X4_FRAMES_MAX * MCLBYTES) + sizeof(struct usb2_cdc_mf_eth_512x4_header),
		.md.flags = {.pipe_bof = 1,.force_short_xfer = 1,.ext_buffer = 1,},
		.md.callback = &cdce_bulk_write_callback,
		.md.timeout = 10000,	/* 10 seconds */
	},

	[CDCE_INTR] = {
		.type = UE_INTERRUPT,
		.endpoint = UE_ADDR_ANY,
		.direction = UE_DIR_IN,
		.if_index = 1,
		/* Host Mode */
		.mh.bufsize = CDCE_IND_SIZE_MAX,
		.mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,.no_pipe_ok = 1,},
		.mh.callback = &cdce_intr_read_callback,
		.mh.timeout = 0,
		/* Device Mode */
		.md.bufsize = CDCE_IND_SIZE_MAX,
		.md.flags = {.pipe_bof = 1,.force_short_xfer = 1,.no_pipe_ok = 1,},
		.md.callback = &cdce_intr_write_callback,
		.md.timeout = 10000,	/* 10 seconds */
	},
};

static device_method_t cdce_methods[] = {
	/* USB interface */
	DEVMETHOD(usb2_handle_request, cdce_handle_request),

	/* Device interface */
	DEVMETHOD(device_probe, cdce_probe),
	DEVMETHOD(device_attach, cdce_attach),
	DEVMETHOD(device_detach, cdce_detach),
	DEVMETHOD(device_suspend, cdce_suspend),
	DEVMETHOD(device_resume, cdce_resume),
	DEVMETHOD(device_shutdown, cdce_shutdown),

	{0, 0}
};

static driver_t cdce_driver = {
	.name = "cdce",
	.methods = cdce_methods,
	.size = sizeof(struct cdce_softc),
};

static devclass_t cdce_devclass;

DRIVER_MODULE(cdce, ushub, cdce_driver, cdce_devclass, NULL, 0);
MODULE_VERSION(cdce, 1);
MODULE_DEPEND(cdce, usb2_ethernet, 1, 1, 1);
MODULE_DEPEND(cdce, usb2_core, 1, 1, 1);
MODULE_DEPEND(cdce, ether, 1, 1, 1);

static const struct usb2_device_id cdce_devs[] = {
	{USB_IF_CSI(UICLASS_CDC, UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL, 0)},
	{USB_IF_CSI(UICLASS_CDC, UISUBCLASS_MOBILE_DIRECT_LINE_MODEL, 0)},

	{USB_VPI(USB_VENDOR_ACERLABS, USB_PRODUCT_ACERLABS_M5632, CDCE_FLAG_NO_UNION)},
	{USB_VPI(USB_VENDOR_AMBIT, USB_PRODUCT_AMBIT_NTL_250, CDCE_FLAG_NO_UNION)},
	{USB_VPI(USB_VENDOR_COMPAQ, USB_PRODUCT_COMPAQ_IPAQLINUX, CDCE_FLAG_NO_UNION)},
	{USB_VPI(USB_VENDOR_GMATE, USB_PRODUCT_GMATE_YP3X00, CDCE_FLAG_NO_UNION)},
	{USB_VPI(USB_VENDOR_MOTOROLA2, USB_PRODUCT_MOTOROLA2_USBLAN, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
	{USB_VPI(USB_VENDOR_MOTOROLA2, USB_PRODUCT_MOTOROLA2_USBLAN2, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
	{USB_VPI(USB_VENDOR_NETCHIP, USB_PRODUCT_NETCHIP_ETHERNETGADGET, CDCE_FLAG_NO_UNION)},
	{USB_VPI(USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2501, CDCE_FLAG_NO_UNION)},
	{USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SL5500, CDCE_FLAG_ZAURUS)},
	{USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SL5600, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
	{USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SLA300, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
	{USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SLC700, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
	{USB_VPI(USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SLC750, CDCE_FLAG_ZAURUS | CDCE_FLAG_NO_UNION)},
};

static int
cdce_probe(device_t dev)
{
	struct usb2_attach_arg *uaa = device_get_ivars(dev);

	return (usb2_lookup_id_by_uaa(cdce_devs, sizeof(cdce_devs), uaa));
}

static int
cdce_attach(device_t dev)
{
	struct cdce_softc *sc = device_get_softc(dev);
	struct usb2_attach_arg *uaa = device_get_ivars(dev);
	struct usb2_interface *iface;
	const struct usb2_cdc_union_descriptor *ud;
	const struct usb2_cdc_ethernet_descriptor *ue;
	const struct usb2_interface_descriptor *id;
	struct ifnet *ifp;
	int error;
	uint8_t alt_index;
	uint8_t i;
	uint8_t eaddr[ETHER_ADDR_LEN];
	char eaddr_str[5 * ETHER_ADDR_LEN];	/* approx */

	if (sc == NULL) {
		return (ENOMEM);
	}
	sc->sc_udev = uaa->device;
	sc->sc_dev = dev;
	sc->sc_unit = device_get_unit(dev);
	sc->sc_flags = USB_GET_DRIVER_INFO(uaa);

	/* search for alternate settings */
	if (uaa->usb2_mode == USB_MODE_HOST) {

		struct usb2_descriptor *desc;
		struct usb2_config_descriptor *cd;

		cd = usb2_get_config_descriptor(uaa->device);
		desc = (void *)(uaa->iface->idesc);
		id = (void *)desc;
		i = id->bInterfaceNumber;
		alt_index = 0;
		while ((desc = usb2_desc_foreach(cd, desc))) {
			id = (void *)desc;
			if ((id->bDescriptorType == UDESC_INTERFACE) &&
			    (id->bLength >= sizeof(*id))) {
				if (id->bInterfaceNumber != i) {
					alt_index = 0;
					break;
				}
				if ((id->bInterfaceClass == UICLASS_CDC) &&
				    (id->bInterfaceSubClass ==
				    UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL) &&
				    (id->bInterfaceProtocol == UIPROTO_CDC_ETH_512X4)) {

					alt_index = id->bAlternateSetting;
					/*
					 * We want this alt setting hence
					 * the protocol supports multi
					 * sub-framing !
					 */
					break;
				}
			}
		}

		if (alt_index > 0) {

			error = usb2_set_alt_interface_index(uaa->device,
			    uaa->info.bIfaceIndex, alt_index);
			if (error) {
				device_printf(dev, "Could not set alternate "
				    "setting, error = %s\n", usb2_errstr(error));
				return (EINVAL);
			}
		}
	}
	/* get the interface subclass we are using */
	sc->sc_iface_protocol = uaa->iface->idesc->bInterfaceProtocol;
#if USB_DEBUG
	if (cdce_force_512x4) {
		sc->sc_iface_protocol = UIPROTO_CDC_ETH_512X4;
	}
#endif
	device_set_usb2_desc(dev);

	snprintf(sc->sc_name, sizeof(sc->sc_name), "%s",
	    device_get_nameunit(dev));

	mtx_init(&sc->sc_mtx, "cdce lock", NULL, MTX_DEF | MTX_RECURSE);

	if (sc->sc_flags & CDCE_FLAG_NO_UNION) {
		sc->sc_ifaces_index[0] = uaa->info.bIfaceIndex;
		sc->sc_ifaces_index[1] = uaa->info.bIfaceIndex;
		sc->sc_data_iface_no = 0;	/* not used */
		goto alloc_transfers;
	}
	ud = usb2_find_descriptor
	    (uaa->device, NULL, uaa->info.bIfaceIndex,
	    UDESC_CS_INTERFACE, 0 - 1, UDESCSUB_CDC_UNION, 0 - 1);

	if ((ud == NULL) || (ud->bLength < sizeof(*ud))) {
		device_printf(dev, "no union descriptor!\n");
		goto detach;
	}
	sc->sc_data_iface_no = ud->bSlaveInterface[0];

	for (i = 0;; i++) {

		iface = usb2_get_iface(uaa->device, i);

		if (iface) {

			id = usb2_get_interface_descriptor(iface);

			if (id && (id->bInterfaceNumber ==
			    sc->sc_data_iface_no)) {
				sc->sc_ifaces_index[0] = i;
				sc->sc_ifaces_index[1] = uaa->info.bIfaceIndex;
				usb2_set_parent_iface(uaa->device, i, uaa->info.bIfaceIndex);
				break;
			}
		} else {
			device_printf(dev, "no data interface found!\n");
			goto detach;
		}
	}

	/*
	 * <quote>
	 *
	 *  The Data Class interface of a networking device shall have
	 *  a minimum of two interface settings. The first setting
	 *  (the default interface setting) includes no endpoints and
	 *  therefore no networking traffic is exchanged whenever the
	 *  default interface setting is selected. One or more
	 *  additional interface settings are used for normal
	 *  operation, and therefore each includes a pair of endpoints
	 *  (one IN, and one OUT) to exchange network traffic. Select
	 *  an alternate interface setting to initialize the network
	 *  aspects of the device and to enable the exchange of
	 *  network traffic.
	 *
	 * </quote>
	 *
	 * Some devices, most notably cable modems, include interface
	 * settings that have no IN or OUT endpoint, therefore loop
	 * through the list of all available interface settings
	 * looking for one with both IN and OUT endpoints.
	 */

alloc_transfers:

	for (i = 0; i < 32; i++) {

		error = usb2_set_alt_interface_index
		    (uaa->device, sc->sc_ifaces_index[0], i);

		if (error) {
			device_printf(dev, "no valid alternate "
			    "setting found!\n");
			goto detach;
		}
		error = usb2_transfer_setup
		    (uaa->device, sc->sc_ifaces_index,
		    sc->sc_xfer, cdce_config, CDCE_N_TRANSFER,
		    sc, &sc->sc_mtx);

		if (error == 0) {
			break;
		}
	}

	ifmedia_init(&sc->sc_ifmedia, 0,
	    &cdce_ifmedia_upd_cb,
	    &cdce_ifmedia_sts_cb);

	ue = usb2_find_descriptor
	    (uaa->device, NULL, uaa->info.bIfaceIndex,
	    UDESC_CS_INTERFACE, 0 - 1, UDESCSUB_CDC_ENF, 0 - 1);

	if ((ue == NULL) || (ue->bLength < sizeof(*ue))) {
		error = USB_ERR_INVAL;
	} else {
		error = usb2_req_get_string_any
		    (uaa->device, &Giant, eaddr_str,
		    sizeof(eaddr_str), ue->iMacAddress);
	}

	if (error) {

		/* fake MAC address */

		device_printf(dev, "faking MAC address\n");
		eaddr[0] = 0x2a;
		memcpy(&eaddr[1], &ticks, sizeof(uint32_t));
		eaddr[5] = sc->sc_unit;

	} else {

		bzero(eaddr, sizeof(eaddr));

		for (i = 0; i < (ETHER_ADDR_LEN * 2); i++) {

			char c = eaddr_str[i];

			if ((c >= '0') && (c <= '9')) {
				c -= '0';
			} else if (c != 0) {
				c -= 'A' - 10;
			} else {
				break;
			}

			c &= 0xf;

			if ((i & 1) == 0) {
				c <<= 4;
			}
			eaddr[i / 2] |= c;
		}

		if (uaa->usb2_mode == USB_MODE_DEVICE) {
			/*
			 * Do not use the same MAC address like the peer !
			 */
			eaddr[5] ^= 0xFF;
		}
	}

	ifp = if_alloc(IFT_ETHER);

	if (ifp == NULL) {
		device_printf(dev, "cannot if_alloc()\n");
		goto detach;
	}

	ifp->if_softc = sc;
	if_initname(ifp, "cdce", sc->sc_unit);
	ifp->if_mtu = ETHERMTU;
	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
	ifp->if_ioctl = cdce_ioctl_cb;
	ifp->if_output = ether_output;
	ifp->if_start = cdce_start_cb;
	ifp->if_init = cdce_init_cb;
	ifp->if_baudrate = 11000000;
	if (sc->sc_iface_protocol == UIPROTO_CDC_ETH_512X4) {
		IFQ_SET_MAXLEN(&ifp->if_snd, CDCE_512X4_IFQ_MAXLEN);
		ifp->if_snd.ifq_drv_maxlen = CDCE_512X4_IFQ_MAXLEN;
	} else {
		IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
		ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
	}
	IFQ_SET_READY(&ifp->if_snd);

	/* no IFM type for 11Mbps USB, so go with 10baseT */
	ifmedia_add(&sc->sc_ifmedia, IFM_ETHER | IFM_10_T, 0, 0);
	ifmedia_set(&sc->sc_ifmedia, IFM_ETHER | IFM_10_T);

	sc->sc_ifp = ifp;

	ether_ifattach(ifp, eaddr);

	/* start the interrupt transfer, if any */
	mtx_lock(&sc->sc_mtx);
	usb2_transfer_start(sc->sc_xfer[CDCE_INTR]);
	mtx_unlock(&sc->sc_mtx);

	return (0);			/* success */

detach:
	cdce_detach(dev);
	return (ENXIO);			/* failure */
}

static int
cdce_detach(device_t dev)
{
	struct cdce_softc *sc = device_get_softc(dev);
	struct ifnet *ifp;

	mtx_lock(&sc->sc_mtx);

	cdce_stop(sc);

	ifp = sc->sc_ifp;

	mtx_unlock(&sc->sc_mtx);

	/* stop all USB transfers first */
	usb2_transfer_unsetup(sc->sc_xfer, CDCE_N_TRANSFER);

	/* get rid of any late children */
	bus_generic_detach(dev);

	if (ifp) {
		ether_ifdetach(ifp);
		if_free(ifp);
		ifmedia_removeall(&sc->sc_ifmedia);
	}
	mtx_destroy(&sc->sc_mtx);

	return (0);
}

static void
cdce_start_cb(struct ifnet *ifp)
{
	struct cdce_softc *sc = ifp->if_softc;

	mtx_lock(&sc->sc_mtx);

	cdce_start_transfers(sc);

	mtx_unlock(&sc->sc_mtx);
}

static void
cdce_start_transfers(struct cdce_softc *sc)
{
	if ((sc->sc_flags & CDCE_FLAG_LL_READY) &&
	    (sc->sc_flags & CDCE_FLAG_HL_READY)) {

		/*
		 * start the USB transfers, if not already started:
		 */
		usb2_transfer_start(sc->sc_xfer[CDCE_BULK_B]);
		usb2_transfer_start(sc->sc_xfer[CDCE_BULK_A]);
	}
}

static uint32_t
cdce_m_frags(struct mbuf *m)
{
	uint32_t temp = 1;

	while ((m = m->m_next)) {
		temp++;
	}
	return (temp);
}

static void
cdce_fwd_mq(struct cdce_softc *sc, struct cdce_mq *mq)
{
	struct mbuf *m;
	struct ifnet *ifp = sc->sc_ifp;

	if (mq->ifq_head) {

		mtx_unlock(&sc->sc_mtx);

		while (1) {

			_IF_DEQUEUE(mq, m);

			if (m == NULL)
				break;

			(ifp->if_input) (ifp, m);
		}

		mtx_lock(&sc->sc_mtx);
	}
}

static void
cdce_free_mq(struct cdce_mq *mq)
{
	struct mbuf *m;

	if (mq->ifq_head) {

		while (1) {

			_IF_DEQUEUE(mq, m);

			if (m == NULL)
				break;

			m_freem(m);
		}
	}
}

static void
cdce_bulk_write_512x4_callback(struct usb2_xfer *xfer)
{
	struct cdce_softc *sc = xfer->priv_sc;
	struct ifnet *ifp = sc->sc_ifp;
	struct mbuf *m;
	struct mbuf *mt;
	uint16_t x;
	uint16_t y;
	uint16_t flen;

	switch (USB_GET_STATE(xfer)) {
	case USB_ST_TRANSFERRED:
		DPRINTFN(11, "transfer complete: "
		    "%u bytes in %u fragments and %u frames\n",
		    xfer->actlen, xfer->nframes, sc->sc_tx_mq.ifq_len);

		/* update packet counter */
		ifp->if_opackets += sc->sc_tx_mq.ifq_len;

		/* free all previous mbufs */
		cdce_free_mq(&sc->sc_tx_mq);

	case USB_ST_SETUP:
tr_setup:
		x = 0;			/* number of frames */
		y = 1;			/* number of fragments */

		while (x != CDCE_512X4_FRAMES_MAX) {

			IFQ_DRV_DEQUEUE(&ifp->if_snd, m);

			if (m == NULL) {
				break;
			}
			if (m->m_pkthdr.len > MCLBYTES) {
				m_freem(m);
				ifp->if_oerrors++;
				continue;
			}
			if (cdce_m_frags(m) > CDCE_512X4_FRAME_FRAG_MAX) {
				mt = m_defrag(m, M_DONTWAIT);
				if (mt == NULL) {
					m_freem(m);
					ifp->if_oerrors++;
					continue;
				}
				m = mt;
			}
			_IF_ENQUEUE(&sc->sc_tx_mq, m);

			/*
			 * if there's a BPF listener, bounce a copy
			 * of this frame to him:
			 */
			BPF_MTAP(ifp, m);

#if (CDCE_512X4_FRAG_LENGTH_MASK < MCLBYTES)
#error "(CDCE_512X4_FRAG_LENGTH_MASK < MCLBYTES)"
#endif
			do {

				flen = m->m_len & CDCE_512X4_FRAG_LENGTH_MASK;
				xfer->frlengths[y] = m->m_len;
				usb2_set_frame_data(xfer, m->m_data, y);

				if (m->m_next == NULL) {
					flen |= CDCE_512X4_FRAG_LAST_MASK;
				}
				USETW(sc->sc_tx.hdr.wFragLength[y - 1], flen);

				y++;

			} while ((m = m->m_next));

			x++;
		}

		if (y == 1) {
			/* no data to transmit */
			break;
		}
		/* fill in Signature */
		sc->sc_tx.hdr.bSig[0] = 'F';
		sc->sc_tx.hdr.bSig[1] = 'L';

		/*
		 * We ensure that the header results in a short packet by
		 * making the length odd !
		 */
		USETW(sc->sc_tx.hdr.wFragLength[y - 1], 0);
		xfer->frlengths[0] = CDCE_512X4_FRAG_LENGTH_OFFSET + ((y - 1) * 2) + 1;
		usb2_set_frame_data(xfer, &sc->sc_tx.hdr, 0);
		xfer->nframes = y;
		usb2_start_hardware(xfer);
		break;

	default:			/* Error */
		DPRINTFN(11, "transfer error, %s\n",
		    usb2_errstr(xfer->error));

		/* update error counter */
		ifp->if_oerrors += sc->sc_tx_mq.ifq_len;

		/* free all previous mbufs */
		cdce_free_mq(&sc->sc_tx_mq);

		if (xfer->error != USB_ERR_CANCELLED) {
			/* try to clear stall first */
			xfer->flags.stall_pipe = 1;
			goto tr_setup;
		}
		break;
	}
}

static void
cdce_bulk_write_std_callback(struct usb2_xfer *xfer)
{
	struct cdce_softc *sc = xfer->priv_sc;
	struct ifnet *ifp = sc->sc_ifp;
	struct mbuf *m;
	struct mbuf *mt;
	uint32_t crc;

	DPRINTFN(1, "\n");

	switch (USB_GET_STATE(xfer)) {
	case USB_ST_TRANSFERRED:
		DPRINTFN(11, "transfer complete: "
		    "%u bytes in %u frames\n", xfer->actlen,
		    xfer->aframes);

		ifp->if_opackets++;

		/* free all previous mbufs */
		cdce_free_mq(&sc->sc_tx_mq);

	case USB_ST_SETUP:
tr_setup:
		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);

		if (m == NULL) {
			break;
		}
		if (sc->sc_flags & CDCE_FLAG_ZAURUS) {
			/*
			 * Zaurus wants a 32-bit CRC appended to
			 * every frame
			 */

			crc = cdce_m_crc32(m, 0, m->m_pkthdr.len);
			crc = htole32(crc);

			if (!m_append(m, 4, (void *)&crc)) {
				m_freem(m);
				ifp->if_oerrors++;
				goto tr_setup;
			}
		}
		if (m->m_len != m->m_pkthdr.len) {
			mt = m_defrag(m, M_DONTWAIT);
			if (mt == NULL) {
				m_freem(m);
				ifp->if_oerrors++;
				goto tr_setup;
			}
			m = mt;
		}
		if (m->m_pkthdr.len > MCLBYTES) {
			m->m_pkthdr.len = MCLBYTES;
		}
		_IF_ENQUEUE(&sc->sc_tx_mq, m);

		xfer->frlengths[0] = m->m_len;
		usb2_set_frame_data(xfer, m->m_data, 0);
		xfer->nframes = 1;

		/*
		 * if there's a BPF listener, bounce a copy
		 * of this frame to him:
		 */
		BPF_MTAP(ifp, m);

		usb2_start_hardware(xfer);
		break;

	default:			/* Error */
		DPRINTFN(11, "transfer error, %s\n",
		    usb2_errstr(xfer->error));

		/* free all previous mbufs */
		cdce_free_mq(&sc->sc_tx_mq);
		ifp->if_oerrors++;

		if (xfer->error != USB_ERR_CANCELLED) {
			/* try to clear stall first */
			xfer->flags.stall_pipe = 1;
			goto tr_setup;
		}
		break;
	}
}

static void
cdce_bulk_write_callback(struct usb2_xfer *xfer)
{
	struct cdce_softc *sc = xfer->priv_sc;

	/* first call - set the correct callback */
	if (sc->sc_iface_protocol == UIPROTO_CDC_ETH_512X4) {
		xfer->flags.force_short_xfer = 0;
		xfer->callback = &cdce_bulk_write_512x4_callback;
	} else {
		xfer->callback = &cdce_bulk_write_std_callback;
	}
	(xfer->callback) (xfer);
}

static int32_t
#ifdef __FreeBSD__
cdce_m_crc32_cb(void *arg, void *src, uint32_t count)
#else
cdce_m_crc32_cb(void *arg, caddr_t src, uint32_t count)
#endif
{
	register uint32_t *p_crc = arg;

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

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

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

static void
cdce_stop(struct cdce_softc *sc)
{
	struct ifnet *ifp = sc->sc_ifp;

	/* immediate configuration */

	if (ifp) {
		/* clear flags */
		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
	}
	sc->sc_flags &= ~(CDCE_FLAG_HL_READY |
	    CDCE_FLAG_LL_READY);

	/*
	 * stop all the transfers, if not already stopped:
	 */
	usb2_transfer_stop(sc->sc_xfer[CDCE_BULK_A]);
	usb2_transfer_stop(sc->sc_xfer[CDCE_BULK_B]);
}

static int
cdce_shutdown(device_t dev)
{
	struct cdce_softc *sc = device_get_softc(dev);

	mtx_lock(&sc->sc_mtx);

	cdce_stop(sc);

	mtx_unlock(&sc->sc_mtx);

	return (0);
}

static int
cdce_suspend(device_t dev)
{
	device_printf(dev, "Suspending\n");
	return (0);
}

static int
cdce_resume(device_t dev)
{
	device_printf(dev, "Resuming\n");
	return (0);
}

static int
cdce_ioctl_cb(struct ifnet *ifp, u_long command, caddr_t data)
{
	struct cdce_softc *sc = ifp->if_softc;
	int error = 0;

	switch (command) {
	case SIOCSIFFLAGS:
		mtx_lock(&sc->sc_mtx);
		if (ifp->if_flags & IFF_UP) {
			if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
				cdce_init_cb(sc);
			}
		} else {
			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
				cdce_stop(sc);
			}
		}
		mtx_unlock(&sc->sc_mtx);
		break;

	case SIOCSIFMEDIA:
	case SIOCGIFMEDIA:
		error = ifmedia_ioctl(ifp, (void *)data,
		    &sc->sc_ifmedia, command);
		break;

	default:
		error = ether_ioctl(ifp, command, data);
		break;
	}
	return (error);
}

static void
cdce_init_cb(void *arg)
{
	struct cdce_softc *sc = arg;
	struct ifnet *ifp;

	mtx_lock(&sc->sc_mtx);

	ifp = sc->sc_ifp;

	/* immediate configuration */

	cdce_stop(sc);

	ifp->if_drv_flags |= IFF_DRV_RUNNING;

	sc->sc_flags |= (
	    CDCE_FLAG_LL_READY |
	    CDCE_FLAG_HL_READY);

	usb2_transfer_set_stall(sc->sc_xfer[CDCE_BULK_A]);
	usb2_transfer_set_stall(sc->sc_xfer[CDCE_BULK_B]);

	cdce_start_transfers(sc);

	mtx_unlock(&sc->sc_mtx);
}

static void
cdce_bulk_read_512x4_callback(struct usb2_xfer *xfer)
{
	struct cdce_softc *sc = xfer->priv_sc;
	struct ifnet *ifp = sc->sc_ifp;
	struct mbuf *m;
	void *data_ptr;
	uint32_t offset;
	uint16_t x;
	uint16_t y;
	uint16_t z;
	uint16_t rx_frags;
	uint16_t flen;
	uint8_t fwd_mq;
	uint8_t free_mq;

	fwd_mq = 0;
	free_mq = 0;
	rx_frags = 0;

	switch (USB_GET_STATE(xfer)) {
	case USB_ST_TRANSFERRED:

		DPRINTF("received %u bytes in %u frames\n",
		    xfer->actlen, xfer->aframes);

		/* check state */
		if (!(sc->sc_flags & CDCE_FLAG_RX_DATA)) {

			/* verify the header */
			if ((xfer->actlen < CDCE_512X4_FRAG_LENGTH_OFFSET) ||
			    (sc->sc_rx.hdr.bSig[0] != 'F') ||
			    (sc->sc_rx.hdr.bSig[1] != 'L')) {
				/* try to clear stall first */
				xfer->flags.stall_pipe = 1;
				goto tr_setup;
			}
			rx_frags = (xfer->actlen -
			    CDCE_512X4_FRAG_LENGTH_OFFSET) / 2;
			if (rx_frags != 0) {
				/* start receiving data */
				sc->sc_flags |= CDCE_FLAG_RX_DATA;
			}
			DPRINTF("doing %u fragments\n", rx_frags);

		} else {
			/* we are done receiving data */
			sc->sc_flags &= ~CDCE_FLAG_RX_DATA;
			fwd_mq = 1;
		}

	case USB_ST_SETUP:
tr_setup:
		if (xfer->flags.stall_pipe) {
			/* we are done */
			sc->sc_flags &= ~CDCE_FLAG_RX_DATA;
		}
		/* we expect a Multi Frame Ethernet Header */
		if (!(sc->sc_flags & CDCE_FLAG_RX_DATA)) {
			DPRINTF("expecting length header\n");
			usb2_set_frame_data(xfer, &sc->sc_rx.hdr, 0);
			xfer->frlengths[0] = sizeof(sc->sc_rx.hdr);
			xfer->nframes = 1;
			xfer->flags.short_xfer_ok = 1;
			usb2_start_hardware(xfer);
			free_mq = 1;
			break;
		}
		/* verify number of fragments */
		if (rx_frags > CDCE_512X4_FRAGS_MAX) {
			/* try to clear stall first */
			xfer->flags.stall_pipe = 1;
			goto tr_setup;
		}
		/* check if the last fragment does not complete a frame */
		x = rx_frags - 1;
		flen = UGETW(sc->sc_rx.hdr.wFragLength[x]);
		if (!(flen & CDCE_512X4_FRAG_LAST_MASK)) {
			DPRINTF("no last frag mask\n");
			/* try to clear stall first */
			xfer->flags.stall_pipe = 1;
			goto tr_setup;
		}
		/*
		 * Setup a new USB transfer chain to receive all the
		 * IP-frame fragments, automagically defragged :
		 */
		x = 0;
		y = 0;
		while (1) {

			z = x;
			offset = 0;

			/* precompute the frame length */
			while (1) {
				flen = UGETW(sc->sc_rx.hdr.wFragLength[z]);
				offset += (flen & CDCE_512X4_FRAG_LENGTH_MASK);
				if (flen & CDCE_512X4_FRAG_LAST_MASK) {
					break;
				}
				z++;
			}

			if (offset >= sizeof(struct ether_header)) {
				/*
				 * allocate a suitable memory buffer, if
				 * possible
				 */
				if (offset > (MCLBYTES - ETHER_ALIGN)) {
					/* try to clear stall first */
					xfer->flags.stall_pipe = 1;
					goto tr_setup;
				} if (offset > (MHLEN - ETHER_ALIGN)) {
					m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
				} else {
					m = m_gethdr(M_DONTWAIT, MT_DATA);
				}
			} else {
				m = NULL;	/* dump it */
			}

			DPRINTFN(17, "frame %u, length = %u \n", y, offset);

			/* check if we have a buffer */
			if (m) {
				m->m_data = USB_ADD_BYTES(m->m_data, ETHER_ALIGN);
				m->m_pkthdr.rcvif = ifp;
				m->m_pkthdr.len = m->m_len = offset;

				/* enqueue */
				_IF_ENQUEUE(&sc->sc_rx_mq, m);

				data_ptr = m->m_data;
				ifp->if_ipackets++;
			} else {
				data_ptr = sc->sc_rx.data;
				ifp->if_ierrors++;
			}

			/* setup the RX chain */
			offset = 0;
			while (1) {

				flen = UGETW(sc->sc_rx.hdr.wFragLength[x]);

				usb2_set_frame_data(xfer,
				    USB_ADD_BYTES(data_ptr, offset), x);

				xfer->frlengths[x] =
				    (flen & CDCE_512X4_FRAG_LENGTH_MASK);

				DPRINTFN(17, "length[%u] = %u\n",
				    x, xfer->frlengths[x]);

				offset += xfer->frlengths[x];

				x++;

				if (flen & CDCE_512X4_FRAG_LAST_MASK) {
					break;
				}
			}

			y++;

			if (x == rx_frags) {
				break;
			}
			if (y == CDCE_512X4_FRAMES_MAX) {
				/* try to clear stall first */
				xfer->flags.stall_pipe = 1;
				goto tr_setup;
			}
		}

		DPRINTF("nframes = %u\n", x);

		xfer->nframes = x;
		xfer->flags.short_xfer_ok = 0;
		usb2_start_hardware(xfer);
		break;

	default:			/* Error */
		DPRINTF("error = %s\n",
		    usb2_errstr(xfer->error));

		if (xfer->error != USB_ERR_CANCELLED) {
			/* try to clear stall first */
			xfer->flags.stall_pipe = 1;
			goto tr_setup;
		}
		free_mq = 1;
		break;
	}

	/*
	 * At the end of a USB callback it is always safe to unlock
	 * the private mutex of a device!
	 *
	 *
	 * By safe we mean that if "usb2_transfer_stop()" is called,
	 * we will get a callback having the error code
	 * USB_ERR_CANCELLED.
	 */
	if (fwd_mq) {
		cdce_fwd_mq(sc, &sc->sc_rx_mq);
	}
	if (free_mq) {
		cdce_free_mq(&sc->sc_rx_mq);
	}
}

static void
cdce_bulk_read_std_callback(struct usb2_xfer *xfer)
{
	struct cdce_softc *sc = xfer->priv_sc;
	struct ifnet *ifp = sc->sc_ifp;
	struct mbuf *m;
	struct mbuf *m_rx = NULL;

	switch (USB_GET_STATE(xfer)) {
	case USB_ST_TRANSFERRED:

		DPRINTF("received %u bytes in %u frames\n",
		    xfer->actlen, xfer->aframes);

		if (sc->sc_flags & CDCE_FLAG_ZAURUS) {

			/* Strip off CRC added by Zaurus */
			if (xfer->frlengths[0] >= MAX(4, 14)) {
				xfer->frlengths[0] -= 4;
			}
		}
		_IF_DEQUEUE(&sc->sc_rx_mq, m);

		if (m) {

			if (xfer->frlengths[0] < sizeof(struct ether_header)) {
				m_freem(m);
				goto tr_setup;
			}
			ifp->if_ipackets++;
			m->m_pkthdr.rcvif = ifp;
			m->m_pkthdr.len = m->m_len = xfer->frlengths[0];
			m_rx = m;
		}
	case USB_ST_SETUP:
tr_setup:
		m = usb2_ether_get_mbuf();
		if (m == NULL) {

			/*
			 * We are out of mbufs and need to dump all the
			 * received data !
			 */
			usb2_set_frame_data(xfer, &sc->sc_rx.data, 0);
			xfer->frlengths[0] = sizeof(sc->sc_rx.data);

		} else {
			usb2_set_frame_data(xfer, m->m_data, 0);
			xfer->frlengths[0] = m->m_len;
			_IF_ENQUEUE(&sc->sc_rx_mq, m);
		}
		xfer->nframes = 1;
		usb2_start_hardware(xfer);
		break;

	default:			/* Error */
		DPRINTF("error = %s\n",
		    usb2_errstr(xfer->error));

		/* free all mbufs */
		cdce_free_mq(&sc->sc_rx_mq);

		if (xfer->error != USB_ERR_CANCELLED) {
			/* try to clear stall first */
			xfer->flags.stall_pipe = 1;
			goto tr_setup;
		}
		return;
	}

	/*
	 * At the end of a USB callback it is always safe to unlock
	 * the private mutex of a device! That is why we do the
	 * "if_input" here, and not some lines up!
	 *
	 * By safe we mean that if "usb2_transfer_stop()" is called,
	 * we will get a callback having the error code
	 * USB_ERR_CANCELLED.
	 */
	if (m_rx) {
		mtx_unlock(&sc->sc_mtx);
		(ifp->if_input) (ifp, m_rx);
		mtx_lock(&sc->sc_mtx);
	}
}

static void
cdce_bulk_read_callback(struct usb2_xfer *xfer)
{
	struct cdce_softc *sc = xfer->priv_sc;

	/* first call - set the correct callback */
	if (sc->sc_iface_protocol == UIPROTO_CDC_ETH_512X4) {
		xfer->callback = &cdce_bulk_read_512x4_callback;
	} else {
		xfer->callback = &cdce_bulk_read_std_callback;
	}
	(xfer->callback) (xfer);
}

static int
cdce_ifmedia_upd_cb(struct ifnet *ifp)
{
	/* no-op, cdce has only 1 possible media type */
	return (0);
}

static void
cdce_ifmedia_sts_cb(struct ifnet *const ifp, struct ifmediareq *req)
{

	req->ifm_status = IFM_AVALID | IFM_ACTIVE;
	req->ifm_active = IFM_ETHER | IFM_10_T;
}

static void
cdce_intr_read_callback(struct usb2_xfer *xfer)
{
	;				/* style fix */
	switch (USB_GET_STATE(xfer)) {
	case USB_ST_TRANSFERRED:

		DPRINTF("Received %d bytes\n",
		    xfer->actlen);

		/* TODO: decode some indications */

	case USB_ST_SETUP:
tr_setup:
		xfer->frlengths[0] = xfer->max_data_length;
		usb2_start_hardware(xfer);
		break;

	default:			/* Error */
		if (xfer->error != USB_ERR_CANCELLED) {
			/* start clear stall */
			xfer->flags.stall_pipe = 1;
			goto tr_setup;
		}
		break;
	}
}

static void
cdce_intr_write_callback(struct usb2_xfer *xfer)
{
	;				/* style fix */
	switch (USB_GET_STATE(xfer)) {
	case USB_ST_TRANSFERRED:

		DPRINTF("Transferred %d bytes\n", xfer->actlen);

	case USB_ST_SETUP:
tr_setup:
#if 0
		xfer->frlengths[0] = XXX;
		usb2_start_hardware(xfer);
#endif
		break;

	default:			/* Error */
		if (xfer->error != USB_ERR_CANCELLED) {
			/* start clear stall */
			xfer->flags.stall_pipe = 1;
			goto tr_setup;
		}
		break;
	}
}

static int
cdce_handle_request(device_t dev,
    const void *req, void **pptr, uint16_t *plen,
    uint16_t offset, uint8_t is_complete)
{
	return (ENXIO);			/* use builtin handler */
}
OpenPOWER on IntegriCloud