summaryrefslogtreecommitdiffstats
path: root/sys/dev/usb2/ethernet/if_aue2.c
blob: 0717e00239fa16c6b71b4c67f32288068d34e820 (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
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
/*-
 * Copyright (c) 1997, 1998, 1999, 2000
 *	Bill Paul <wpaul@ee.columbia.edu>.  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 OR THE VOICES IN HIS HEAD
 * 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.
 */

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

/*
 * ADMtek AN986 Pegasus and AN8511 Pegasus II USB to ethernet driver.
 * Datasheet is available from http://www.admtek.com.tw.
 *
 * Written by Bill Paul <wpaul@ee.columbia.edu>
 * Electrical Engineering Department
 * Columbia University, New York City
 */

/*
 * The Pegasus chip uses four USB "endpoints" to provide 10/100 ethernet
 * support: the control endpoint for reading/writing registers, burst
 * read endpoint for packet reception, burst write for packet transmission
 * and one for "interrupts." The chip uses the same RX filter scheme
 * as the other ADMtek ethernet parts: one perfect filter entry for the
 * the station address and a 64-bit multicast hash table. The chip supports
 * both MII and HomePNA attachments.
 *
 * Since the maximum data transfer speed of USB is supposed to be 12Mbps,
 * you're never really going to get 100Mbps speeds from this device. I
 * think the idea is to allow the device to connect to 10 or 100Mbps
 * networks, not necessarily to provide 100Mbps performance. Also, since
 * the controller uses an external PHY chip, it's possible that board
 * designers might simply choose a 10Mbps PHY.
 *
 * Registers are accessed using usb2_do_request(). Packet transfers are
 * done using usb2_transfer() and friends.
 */

/*
 * NOTE: all function names beginning like "aue_cfg_" can only
 * be called from within the config thread function !
 */

#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>

#define	usb2_config_td_cc usb2_ether_cc
#define	usb2_config_td_softc aue_softc

#define	USB_DEBUG_VAR aue_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/ethernet/usb2_ethernet.h>
#include <dev/usb2/ethernet/if_auereg.h>

MODULE_DEPEND(aue, usb2_ethernet, 1, 1, 1);
MODULE_DEPEND(aue, usb2_core, 1, 1, 1);
MODULE_DEPEND(aue, ether, 1, 1, 1);
MODULE_DEPEND(aue, miibus, 1, 1, 1);

#if USB_DEBUG
static int aue_debug = 0;

SYSCTL_NODE(_hw_usb2, OID_AUTO, aue, CTLFLAG_RW, 0, "USB aue");
SYSCTL_INT(_hw_usb2_aue, OID_AUTO, debug, CTLFLAG_RW, &aue_debug, 0,
    "Debug level");
#endif

/*
 * Various supported device vendors/products.
 */
static const struct usb2_device_id aue_devs[] = {
	{USB_VPI(USB_VENDOR_3COM, USB_PRODUCT_3COM_3C460B, AUE_FLAG_PII)},
	{USB_VPI(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_DSB650TX_PNA, 0)},
	{USB_VPI(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_UFE1000, AUE_FLAG_LSYS)},
	{USB_VPI(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX10, 0)},
	{USB_VPI(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX1, AUE_FLAG_PNA | AUE_FLAG_PII)},
	{USB_VPI(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX2, AUE_FLAG_PII)},
	{USB_VPI(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX4, AUE_FLAG_PNA)},
	{USB_VPI(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX5, AUE_FLAG_PNA)},
	{USB_VPI(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX6, AUE_FLAG_PII)},
	{USB_VPI(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX7, AUE_FLAG_PII)},
	{USB_VPI(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX8, AUE_FLAG_PII)},
	{USB_VPI(USB_VENDOR_ABOCOM, USB_PRODUCT_ABOCOM_XX9, AUE_FLAG_PNA)},
	{USB_VPI(USB_VENDOR_ACCTON, USB_PRODUCT_ACCTON_SS1001, AUE_FLAG_PII)},
	{USB_VPI(USB_VENDOR_ACCTON, USB_PRODUCT_ACCTON_USB320_EC, 0)},
	{USB_VPI(USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUSII_2, AUE_FLAG_PII)},
	{USB_VPI(USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUSII_3, AUE_FLAG_PII)},
	{USB_VPI(USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUSII_4, AUE_FLAG_PII)},
	{USB_VPI(USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUSII, AUE_FLAG_PII)},
	{USB_VPI(USB_VENDOR_ADMTEK, USB_PRODUCT_ADMTEK_PEGASUS, AUE_FLAG_PNA | AUE_FLAG_DUAL_PHY)},
	{USB_VPI(USB_VENDOR_AEI, USB_PRODUCT_AEI_FASTETHERNET, AUE_FLAG_PII)},
	{USB_VPI(USB_VENDOR_ALLIEDTELESYN, USB_PRODUCT_ALLIEDTELESYN_ATUSB100, AUE_FLAG_PII)},
	{USB_VPI(USB_VENDOR_ATEN, USB_PRODUCT_ATEN_UC110T, AUE_FLAG_PII)},
	{USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_USB2LAN, AUE_FLAG_PII)},
	{USB_VPI(USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USB100, 0)},
	{USB_VPI(USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USBE100, AUE_FLAG_PII)},
	{USB_VPI(USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USBEL100, 0)},
	{USB_VPI(USB_VENDOR_BILLIONTON, USB_PRODUCT_BILLIONTON_USBLP100, AUE_FLAG_PNA)},
	{USB_VPI(USB_VENDOR_COREGA, USB_PRODUCT_COREGA_FETHER_USB_TXS, AUE_FLAG_PII)},
	{USB_VPI(USB_VENDOR_COREGA, USB_PRODUCT_COREGA_FETHER_USB_TX, 0)},
	{USB_VPI(USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX1, AUE_FLAG_LSYS)},
	{USB_VPI(USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX2, AUE_FLAG_LSYS | AUE_FLAG_PII)},
	{USB_VPI(USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX3, AUE_FLAG_LSYS | AUE_FLAG_PII)},
	{USB_VPI(USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX4, AUE_FLAG_LSYS | AUE_FLAG_PII)},
	{USB_VPI(USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX_PNA, AUE_FLAG_PNA)},
	{USB_VPI(USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650TX, AUE_FLAG_LSYS)},
	{USB_VPI(USB_VENDOR_DLINK, USB_PRODUCT_DLINK_DSB650, AUE_FLAG_LSYS)},
	{USB_VPI(USB_VENDOR_ELCON, USB_PRODUCT_ELCON_PLAN, AUE_FLAG_PNA | AUE_FLAG_PII)},
	{USB_VPI(USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSB20, AUE_FLAG_PII)},
	{USB_VPI(USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBLTX, AUE_FLAG_PII)},
	{USB_VPI(USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBTX0, 0)},
	{USB_VPI(USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBTX1, AUE_FLAG_LSYS)},
	{USB_VPI(USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBTX2, 0)},
	{USB_VPI(USB_VENDOR_ELECOM, USB_PRODUCT_ELECOM_LDUSBTX3, AUE_FLAG_LSYS)},
	{USB_VPI(USB_VENDOR_ELSA, USB_PRODUCT_ELSA_USB2ETHERNET, 0)},
	{USB_VPI(USB_VENDOR_GIGABYTE, USB_PRODUCT_GIGABYTE_GNBR402W, 0)},
	{USB_VPI(USB_VENDOR_HAWKING, USB_PRODUCT_HAWKING_UF100, AUE_FLAG_PII)},
	{USB_VPI(USB_VENDOR_HP, USB_PRODUCT_HP_HN210E, AUE_FLAG_PII)},
	{USB_VPI(USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBETTXS, AUE_FLAG_PII)},
	{USB_VPI(USB_VENDOR_IODATA, USB_PRODUCT_IODATA_USBETTX, 0)},
	{USB_VPI(USB_VENDOR_KINGSTON, USB_PRODUCT_KINGSTON_KNU101TX, 0)},
	{USB_VPI(USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB100H1, AUE_FLAG_LSYS | AUE_FLAG_PNA)},
	{USB_VPI(USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB100TX, AUE_FLAG_LSYS)},
	{USB_VPI(USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10TA, AUE_FLAG_LSYS)},
	{USB_VPI(USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10TX1, AUE_FLAG_LSYS | AUE_FLAG_PII)},
	{USB_VPI(USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10TX2, AUE_FLAG_LSYS | AUE_FLAG_PII)},
	{USB_VPI(USB_VENDOR_LINKSYS, USB_PRODUCT_LINKSYS_USB10T, AUE_FLAG_LSYS)},
	{USB_VPI(USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUA2TX5, AUE_FLAG_PII)},
	{USB_VPI(USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUATX1, 0)},
	{USB_VPI(USB_VENDOR_MELCO, USB_PRODUCT_MELCO_LUATX5, 0)},
	{USB_VPI(USB_VENDOR_MICROSOFT, USB_PRODUCT_MICROSOFT_MN110, AUE_FLAG_PII)},
	{USB_VPI(USB_VENDOR_NETGEAR, USB_PRODUCT_NETGEAR_FA101, AUE_FLAG_PII)},
	{USB_VPI(USB_VENDOR_SIEMENS, USB_PRODUCT_SIEMENS_SPEEDSTREAM, AUE_FLAG_PII)},
	{USB_VPI(USB_VENDOR_SIIG2, USB_PRODUCT_SIIG2_USBTOETHER, AUE_FLAG_PII)},
	{USB_VPI(USB_VENDOR_SMARTBRIDGES, USB_PRODUCT_SMARTBRIDGES_SMARTNIC, AUE_FLAG_PII)},
	{USB_VPI(USB_VENDOR_SMC, USB_PRODUCT_SMC_2202USB, 0)},
	{USB_VPI(USB_VENDOR_SMC, USB_PRODUCT_SMC_2206USB, AUE_FLAG_PII)},
	{USB_VPI(USB_VENDOR_SOHOWARE, USB_PRODUCT_SOHOWARE_NUB100, 0)},
	{USB_VPI(USB_VENDOR_SOHOWARE, USB_PRODUCT_SOHOWARE_NUB110, AUE_FLAG_PII)},
};

/* prototypes */

static device_probe_t aue_probe;
static device_attach_t aue_attach;
static device_detach_t aue_detach;
static device_shutdown_t aue_shutdown;

static usb2_callback_t aue_intr_clear_stall_callback;
static usb2_callback_t aue_intr_callback;
static usb2_callback_t aue_bulk_read_clear_stall_callback;
static usb2_callback_t aue_bulk_read_callback;
static usb2_callback_t aue_bulk_write_clear_stall_callback;
static usb2_callback_t aue_bulk_write_callback;

static void	aue_cfg_do_request(struct aue_softc *,
		    struct usb2_device_request *, void *);
static uint8_t	aue_cfg_csr_read_1(struct aue_softc *, uint16_t);
static uint16_t	aue_cfg_csr_read_2(struct aue_softc *, uint16_t);
static void	aue_cfg_csr_write_1(struct aue_softc *, uint16_t, uint8_t);
static void	aue_cfg_csr_write_2(struct aue_softc *, uint16_t, uint16_t);
static void	aue_cfg_eeprom_getword(struct aue_softc *, uint8_t, uint8_t *);
static void	aue_cfg_read_eeprom(struct aue_softc *, uint8_t *, uint16_t,
		    uint16_t);

static miibus_readreg_t aue_cfg_miibus_readreg;
static miibus_writereg_t aue_cfg_miibus_writereg;
static miibus_statchg_t aue_cfg_miibus_statchg;

static usb2_config_td_command_t aue_cfg_setmulti;
static usb2_config_td_command_t aue_cfg_first_time_setup;
static usb2_config_td_command_t aue_config_copy;
static usb2_config_td_command_t aue_cfg_tick;
static usb2_config_td_command_t aue_cfg_pre_init;
static usb2_config_td_command_t aue_cfg_init;
static usb2_config_td_command_t aue_cfg_promisc_upd;
static usb2_config_td_command_t aue_cfg_ifmedia_upd;
static usb2_config_td_command_t aue_cfg_pre_stop;
static usb2_config_td_command_t aue_cfg_stop;

static void	aue_cfg_reset_pegasus_II(struct aue_softc *);
static void	aue_cfg_reset(struct aue_softc *);
static void	aue_start_cb(struct ifnet *);
static void	aue_init_cb(void *);
static void	aue_start_transfers(struct aue_softc *);
static int	aue_ifmedia_upd_cb(struct ifnet *);
static void	aue_ifmedia_sts_cb(struct ifnet *, struct ifmediareq *);
static int	aue_ioctl_cb(struct ifnet *, u_long, caddr_t);
static void	aue_watchdog(void *);

static const struct usb2_config aue_config[AUE_N_TRANSFER] = {

	[AUE_BULK_DT_WR] = {
		.type = UE_BULK,
		.endpoint = UE_ADDR_ANY,
		.direction = UE_DIR_OUT,
		.mh.bufsize = (MCLBYTES + 2),
		.mh.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
		.mh.callback = &aue_bulk_write_callback,
		.mh.timeout = 10000,	/* 10 seconds */
	},

	[AUE_BULK_DT_RD] = {
		.type = UE_BULK,
		.endpoint = UE_ADDR_ANY,
		.direction = UE_DIR_IN,
		.mh.bufsize = (MCLBYTES + 4 + ETHER_CRC_LEN),
		.mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
		.mh.callback = &aue_bulk_read_callback,
	},

	[AUE_BULK_CS_WR] = {
		.type = UE_CONTROL,
		.endpoint = 0x00,	/* Control pipe */
		.direction = UE_DIR_ANY,
		.mh.bufsize = sizeof(struct usb2_device_request),
		.mh.flags = {},
		.mh.callback = &aue_bulk_write_clear_stall_callback,
		.mh.timeout = 1000,	/* 1 second */
		.mh.interval = 50,	/* 50ms */
	},

	[AUE_BULK_CS_RD] = {
		.type = UE_CONTROL,
		.endpoint = 0x00,	/* Control pipe */
		.direction = UE_DIR_ANY,
		.mh.bufsize = sizeof(struct usb2_device_request),
		.mh.flags = {},
		.mh.callback = &aue_bulk_read_clear_stall_callback,
		.mh.timeout = 1000,	/* 1 second */
		.mh.interval = 50,	/* 50ms */
	},

	[AUE_INTR_DT_RD] = {
		.type = UE_INTERRUPT,
		.endpoint = UE_ADDR_ANY,
		.direction = UE_DIR_IN,
		.mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
		.mh.bufsize = 0,	/* use wMaxPacketSize */
		.mh.callback = &aue_intr_callback,
	},

	[AUE_INTR_CS_RD] = {
		.type = UE_CONTROL,
		.endpoint = 0x00,	/* Control pipe */
		.direction = UE_DIR_ANY,
		.mh.bufsize = sizeof(struct usb2_device_request),
		.mh.flags = {},
		.mh.callback = &aue_intr_clear_stall_callback,
		.mh.timeout = 1000,	/* 1 second */
		.mh.interval = 50,	/* 50ms */
	},
};

static device_method_t aue_methods[] = {
	/* Device interface */
	DEVMETHOD(device_probe, aue_probe),
	DEVMETHOD(device_attach, aue_attach),
	DEVMETHOD(device_detach, aue_detach),
	DEVMETHOD(device_shutdown, aue_shutdown),

	/* bus interface */
	DEVMETHOD(bus_print_child, bus_generic_print_child),
	DEVMETHOD(bus_driver_added, bus_generic_driver_added),

	/* MII interface */
	DEVMETHOD(miibus_readreg, aue_cfg_miibus_readreg),
	DEVMETHOD(miibus_writereg, aue_cfg_miibus_writereg),
	DEVMETHOD(miibus_statchg, aue_cfg_miibus_statchg),

	{0, 0}
};

static driver_t aue_driver = {
	.name = "aue",
	.methods = aue_methods,
	.size = sizeof(struct aue_softc)
};

static devclass_t aue_devclass;

DRIVER_MODULE(aue, ushub, aue_driver, aue_devclass, NULL, 0);
DRIVER_MODULE(miibus, aue, miibus_driver, miibus_devclass, 0, 0);

static void
aue_cfg_do_request(struct aue_softc *sc, struct usb2_device_request *req,
    void *data)
{
	uint16_t length;
	usb2_error_t err;

	if (usb2_config_td_is_gone(&sc->sc_config_td)) {
		goto error;
	}
	err = usb2_do_request_flags
	    (sc->sc_udev, &sc->sc_mtx, req, data, 0, NULL, 1000);

	if (err) {

		DPRINTF("device request failed, err=%s "
		    "(ignored)\n", usb2_errstr(err));

error:
		length = UGETW(req->wLength);

		if ((req->bmRequestType & UT_READ) && length) {
			bzero(data, length);
		}
	}
}

#define	AUE_CFG_SETBIT(sc, reg, x) \
	aue_cfg_csr_write_1(sc, reg, aue_cfg_csr_read_1(sc, reg) | (x))

#define	AUE_CFG_CLRBIT(sc, reg, x) \
	aue_cfg_csr_write_1(sc, reg, aue_cfg_csr_read_1(sc, reg) & ~(x))

static uint8_t
aue_cfg_csr_read_1(struct aue_softc *sc, uint16_t reg)
{
	struct usb2_device_request req;
	uint8_t val;

	req.bmRequestType = UT_READ_VENDOR_DEVICE;
	req.bRequest = AUE_UR_READREG;
	USETW(req.wValue, 0);
	USETW(req.wIndex, reg);
	USETW(req.wLength, 1);

	aue_cfg_do_request(sc, &req, &val);
	return (val);
}

static uint16_t
aue_cfg_csr_read_2(struct aue_softc *sc, uint16_t reg)
{
	struct usb2_device_request req;
	uint16_t val;

	req.bmRequestType = UT_READ_VENDOR_DEVICE;
	req.bRequest = AUE_UR_READREG;
	USETW(req.wValue, 0);
	USETW(req.wIndex, reg);
	USETW(req.wLength, 2);

	aue_cfg_do_request(sc, &req, &val);
	return (le16toh(val));
}

static void
aue_cfg_csr_write_1(struct aue_softc *sc, uint16_t reg, uint8_t val)
{
	struct usb2_device_request req;

	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
	req.bRequest = AUE_UR_WRITEREG;
	req.wValue[0] = val;
	req.wValue[1] = 0;
	USETW(req.wIndex, reg);
	USETW(req.wLength, 1);

	aue_cfg_do_request(sc, &req, &val);
}

static void
aue_cfg_csr_write_2(struct aue_softc *sc, uint16_t reg, uint16_t val)
{
	struct usb2_device_request req;

	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
	req.bRequest = AUE_UR_WRITEREG;
	USETW(req.wValue, val);
	USETW(req.wIndex, reg);
	USETW(req.wLength, 2);

	val = htole16(val);

	aue_cfg_do_request(sc, &req, &val);
}

/*
 * Read a word of data stored in the EEPROM at address 'addr.'
 */
static void
aue_cfg_eeprom_getword(struct aue_softc *sc, uint8_t addr,
    uint8_t *dest)
{
	uint16_t i;

	aue_cfg_csr_write_1(sc, AUE_EE_REG, addr);
	aue_cfg_csr_write_1(sc, AUE_EE_CTL, AUE_EECTL_READ);

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

		if (i < AUE_TIMEOUT) {

			if (aue_cfg_csr_read_1(sc, AUE_EE_CTL) & AUE_EECTL_DONE) {
				break;
			}
			if (usb2_config_td_sleep(&sc->sc_config_td, hz / 100)) {
				break;
			}
		} else {
			DPRINTF("EEPROM read timed out!\n");
			break;
		}
	}

	i = aue_cfg_csr_read_2(sc, AUE_EE_DATA);

	dest[0] = (i & 0xFF);
	dest[1] = (i >> 8);
}

/*
 * Read a sequence of words from the EEPROM.
 */
static void
aue_cfg_read_eeprom(struct aue_softc *sc, uint8_t *dest,
    uint16_t off, uint16_t len)
{
	uint16_t i;

	for (i = 0; i < len; i++) {
		aue_cfg_eeprom_getword(sc, off + i, dest + (i * 2));
	}
}

static int
aue_cfg_miibus_readreg(device_t dev, int phy, int reg)
{
	struct aue_softc *sc = device_get_softc(dev);
	uint16_t i;
	uint8_t do_unlock;

	/* avoid recursive locking */
	if (mtx_owned(&sc->sc_mtx)) {
		do_unlock = 0;
	} else {
		mtx_lock(&sc->sc_mtx);
		do_unlock = 1;
	}

	/*
	 * The Am79C901 HomePNA PHY actually contains
	 * two transceivers: a 1Mbps HomePNA PHY and a
	 * 10Mbps full/half duplex ethernet PHY with
	 * NWAY autoneg. However in the ADMtek adapter,
	 * only the 1Mbps PHY is actually connected to
	 * anything, so we ignore the 10Mbps one. It
	 * happens to be configured for MII address 3,
	 * so we filter that out.
	 */
	if (sc->sc_flags & AUE_FLAG_DUAL_PHY) {

		if (phy == 3) {
			i = 0;
			goto done;
		}
#if 0
		if (phy != 1) {
			i = 0;
			goto done;
		}
#endif
	}
	aue_cfg_csr_write_1(sc, AUE_PHY_ADDR, phy);
	aue_cfg_csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_READ);

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

		if (i < AUE_TIMEOUT) {

			if (aue_cfg_csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE) {
				break;
			}
			if (usb2_config_td_sleep(&sc->sc_config_td, hz / 100)) {
				break;
			}
		} else {
			DPRINTF("MII read timed out\n");
			break;
		}
	}

	i = aue_cfg_csr_read_2(sc, AUE_PHY_DATA);

done:
	if (do_unlock) {
		mtx_unlock(&sc->sc_mtx);
	}
	return (i);
}

static int
aue_cfg_miibus_writereg(device_t dev, int phy, int reg, int data)
{
	struct aue_softc *sc = device_get_softc(dev);
	uint16_t i;
	uint8_t do_unlock;

	if (phy == 3) {
		return (0);
	}
	/* avoid recursive locking */
	if (mtx_owned(&sc->sc_mtx)) {
		do_unlock = 0;
	} else {
		mtx_lock(&sc->sc_mtx);
		do_unlock = 1;
	}

	aue_cfg_csr_write_2(sc, AUE_PHY_DATA, data);
	aue_cfg_csr_write_1(sc, AUE_PHY_ADDR, phy);
	aue_cfg_csr_write_1(sc, AUE_PHY_CTL, reg | AUE_PHYCTL_WRITE);

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

		if (i < AUE_TIMEOUT) {
			if (aue_cfg_csr_read_1(sc, AUE_PHY_CTL) & AUE_PHYCTL_DONE) {
				break;
			}
			if (usb2_config_td_sleep(&sc->sc_config_td, hz / 100)) {
				break;
			}
		} else {
			DPRINTF("MII write timed out\n");
			break;
		}
	}

	if (do_unlock) {
		mtx_unlock(&sc->sc_mtx);
	}
	return (0);
}

static void
aue_cfg_miibus_statchg(device_t dev)
{
	struct aue_softc *sc = device_get_softc(dev);
	struct mii_data *mii = GET_MII(sc);
	uint8_t do_unlock;

	/* avoid recursive locking */
	if (mtx_owned(&sc->sc_mtx)) {
		do_unlock = 0;
	} else {
		mtx_lock(&sc->sc_mtx);
		do_unlock = 1;
	}

	AUE_CFG_CLRBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB);

	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX) {
		AUE_CFG_SETBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL);
	} else {
		AUE_CFG_CLRBIT(sc, AUE_CTL1, AUE_CTL1_SPEEDSEL);
	}

	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) {
		AUE_CFG_SETBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX);
	} else {
		AUE_CFG_CLRBIT(sc, AUE_CTL1, AUE_CTL1_DUPLEX);
	}

	AUE_CFG_SETBIT(sc, AUE_CTL0, AUE_CTL0_RX_ENB | AUE_CTL0_TX_ENB);

	/*
	 * Set the LED modes on the LinkSys adapter.
	 * This turns on the 'dual link LED' bin in the auxmode
	 * register of the Broadcom PHY.
	 */
	if (sc->sc_flags & AUE_FLAG_LSYS) {
		uint16_t auxmode;

		auxmode = aue_cfg_miibus_readreg(dev, 0, 0x1b);
		aue_cfg_miibus_writereg(dev, 0, 0x1b, auxmode | 0x04);
	}
	if (do_unlock) {
		mtx_unlock(&sc->sc_mtx);
	}
}

static void
aue_cfg_setmulti(struct aue_softc *sc,
    struct usb2_config_td_cc *cc, uint16_t refcount)
{
	uint16_t i;

	if ((cc->if_flags & IFF_ALLMULTI) ||
	    (cc->if_flags & IFF_PROMISC)) {
		AUE_CFG_SETBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);
		return;
	}
	AUE_CFG_CLRBIT(sc, AUE_CTL0, AUE_CTL0_ALLMULTI);

	/* clear existing ones */
	for (i = 0; i < 8; i++) {
		aue_cfg_csr_write_1(sc, AUE_MAR0 + i, 0);
	}

	/* now program new ones */
	for (i = 0; i < 8; i++) {
		aue_cfg_csr_write_1(sc, AUE_MAR0 + i, cc->if_hash[i]);
	}
}

static void
aue_cfg_reset_pegasus_II(struct aue_softc *sc)
{
	/* Magic constants taken from Linux driver. */
	aue_cfg_csr_write_1(sc, AUE_REG_1D, 0);
	aue_cfg_csr_write_1(sc, AUE_REG_7B, 2);
#if 0
	if ((sc->sc_flags & HAS_HOME_PNA) && mii_mode)
		aue_cfg_csr_write_1(sc, AUE_REG_81, 6);
	else
#endif
		aue_cfg_csr_write_1(sc, AUE_REG_81, 2);
}

static void
aue_cfg_reset(struct aue_softc *sc)
{
	uint16_t i;

	AUE_CFG_SETBIT(sc, AUE_CTL1, AUE_CTL1_RESETMAC);

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

		if (i < AUE_TIMEOUT) {

			if (!(aue_cfg_csr_read_1(sc, AUE_CTL1) & AUE_CTL1_RESETMAC)) {
				break;
			}
			if (usb2_config_td_sleep(&sc->sc_config_td, hz / 100)) {
				break;
			}
		} else {
			DPRINTF("reset timed out\n");
			break;
		}
	}

	/*
	 * The PHY(s) attached to the Pegasus chip may be held
	 * in reset until we flip on the GPIO outputs. Make sure
	 * to set the GPIO pins high so that the PHY(s) will
	 * be enabled.
	 *
	 * Note: We force all of the GPIO pins low first, *then*
	 * enable the ones we want.
	 */
	aue_cfg_csr_write_1(sc, AUE_GPIO0, (AUE_GPIO_OUT0 | AUE_GPIO_SEL0));
	aue_cfg_csr_write_1(sc, AUE_GPIO0, (AUE_GPIO_OUT0 | AUE_GPIO_SEL0 |
	    AUE_GPIO_SEL1));

	if (sc->sc_flags & AUE_FLAG_LSYS) {
		/* Grrr. LinkSys has to be different from everyone else. */
		aue_cfg_csr_write_1(sc, AUE_GPIO0,
		    (AUE_GPIO_SEL0 | AUE_GPIO_SEL1));
		aue_cfg_csr_write_1(sc, AUE_GPIO0,
		    (AUE_GPIO_SEL0 |
		    AUE_GPIO_SEL1 |
		    AUE_GPIO_OUT0));
	}
	if (sc->sc_flags & AUE_FLAG_PII) {
		aue_cfg_reset_pegasus_II(sc);
	}
	/* wait a little while for the chip to get its brains in order: */
	usb2_config_td_sleep(&sc->sc_config_td, hz / 100);
}

/*
 * Probe for a Pegasus chip.
 */
static int
aue_probe(device_t dev)
{
	struct usb2_attach_arg *uaa = device_get_ivars(dev);

	if (uaa->usb2_mode != USB_MODE_HOST) {
		return (ENXIO);
	}
	if (uaa->info.bConfigIndex != AUE_CONFIG_INDEX) {
		return (ENXIO);
	}
	if (uaa->info.bIfaceIndex != AUE_IFACE_IDX) {
		return (ENXIO);
	}
	/*
	 * Belkin USB Bluetooth dongles of the F8T012xx1 model series
	 * conflict with older Belkin USB2LAN adapters. Skip if_aue if
	 * we detect one of the devices that look like Bluetooth
	 * adapters.
	 */
	if ((uaa->info.idVendor == USB_VENDOR_BELKIN) &&
	    (uaa->info.idProduct == USB_PRODUCT_BELKIN_F8T012) &&
	    (uaa->info.bcdDevice == 0x0413)) {
		return (ENXIO);
	}
	return (usb2_lookup_id_by_uaa(aue_devs, sizeof(aue_devs), uaa));
}

/*
 * Attach the interface. Allocate softc structures, do ifmedia
 * setup and ethernet/BPF attach.
 */
static int
aue_attach(device_t dev)
{
	struct usb2_attach_arg *uaa = device_get_ivars(dev);
	struct aue_softc *sc = device_get_softc(dev);
	int32_t error;
	uint8_t iface_index;

	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);

	if (uaa->info.bcdDevice >= 0x0201) {
		sc->sc_flags |= AUE_FLAG_VER_2;	/* XXX currently undocumented */
	}
	device_set_usb2_desc(dev);

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

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

	usb2_callout_init_mtx(&sc->sc_watchdog, &sc->sc_mtx, 0);

	iface_index = AUE_IFACE_IDX;
	error = usb2_transfer_setup(uaa->device, &iface_index,
	    sc->sc_xfer, aue_config, AUE_N_TRANSFER,
	    sc, &sc->sc_mtx);
	if (error) {
		device_printf(dev, "allocating USB "
		    "transfers failed!\n");
		goto detach;
	}
	error = usb2_config_td_setup(&sc->sc_config_td, sc, &sc->sc_mtx,
	    NULL, sizeof(struct usb2_config_td_cc), 16);
	if (error) {
		device_printf(dev, "could not setup config "
		    "thread!\n");
		goto detach;
	}
	mtx_lock(&sc->sc_mtx);

	sc->sc_flags |= AUE_FLAG_WAIT_LINK;

	/* start setup */

	usb2_config_td_queue_command
	    (&sc->sc_config_td, NULL, &aue_cfg_first_time_setup, 0, 0);

	aue_watchdog(sc);
	mtx_unlock(&sc->sc_mtx);
	return (0);			/* success */

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

static void
aue_cfg_first_time_setup(struct aue_softc *sc,
    struct usb2_config_td_cc *cc, uint16_t refcount)
{
	struct ifnet *ifp;
	int error;
	uint8_t eaddr[min(ETHER_ADDR_LEN, 6)];

	/* reset the adapter */
	aue_cfg_reset(sc);

	/* set default value */
	bzero(eaddr, sizeof(eaddr));

	/* get station address from the EEPROM */
	aue_cfg_read_eeprom(sc, eaddr, 0, 3);

	mtx_unlock(&sc->sc_mtx);

	ifp = if_alloc(IFT_ETHER);

	mtx_lock(&sc->sc_mtx);

	if (ifp == NULL) {
		printf("%s: could not if_alloc()\n",
		    sc->sc_name);
		goto done;
	}
	sc->sc_evilhack = ifp;

	ifp->if_softc = sc;
	if_initname(ifp, "aue", sc->sc_unit);
	ifp->if_mtu = ETHERMTU;
	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
	ifp->if_ioctl = aue_ioctl_cb;
	ifp->if_start = aue_start_cb;
	ifp->if_watchdog = NULL;
	ifp->if_init = aue_init_cb;
	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
	ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
	IFQ_SET_READY(&ifp->if_snd);

	/*
	 * XXX need Giant when accessing the device structures !
	 */

	mtx_unlock(&sc->sc_mtx);

	mtx_lock(&Giant);

	error = mii_phy_probe(sc->sc_dev, &sc->sc_miibus,
	    &aue_ifmedia_upd_cb,
	    &aue_ifmedia_sts_cb);

	mtx_unlock(&Giant);

	mtx_lock(&sc->sc_mtx);

	/*
	 * Do MII setup.
	 * NOTE: Doing this causes child devices to be attached to us,
	 * which we would normally disconnect at in the detach routine
	 * using device_delete_child(). However the USB code is set up
	 * such that when this driver is removed, all children devices
	 * are removed as well. In effect, the USB code ends up detaching
	 * all of our children for us, so we don't have to do is ourselves
	 * in aue_detach(). It's important to point this out since if
	 * we *do* try to detach the child devices ourselves, we will
	 * end up getting the children deleted twice, which will crash
	 * the system.
	 */
	if (error) {
		printf("%s: MII without any PHY!\n",
		    sc->sc_name);
		if_free(ifp);
		goto done;
	}
	sc->sc_ifp = ifp;

	mtx_unlock(&sc->sc_mtx);

	/*
	 * Call MI attach routine.
	 */
	ether_ifattach(ifp, eaddr);

	mtx_lock(&sc->sc_mtx);

done:
	return;
}

static int
aue_detach(device_t dev)
{
	struct aue_softc *sc = device_get_softc(dev);
	struct ifnet *ifp;

	usb2_config_td_drain(&sc->sc_config_td);

	mtx_lock(&sc->sc_mtx);

	usb2_callout_stop(&sc->sc_watchdog);

	aue_cfg_pre_stop(sc, NULL, 0);

	ifp = sc->sc_ifp;

	mtx_unlock(&sc->sc_mtx);

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

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

	if (ifp) {
		ether_ifdetach(ifp);
		if_free(ifp);
	}
	usb2_config_td_unsetup(&sc->sc_config_td);

	usb2_callout_drain(&sc->sc_watchdog);

	mtx_destroy(&sc->sc_mtx);

	return (0);
}

static void
aue_intr_clear_stall_callback(struct usb2_xfer *xfer)
{
	struct aue_softc *sc = xfer->priv_sc;
	struct usb2_xfer *xfer_other = sc->sc_xfer[AUE_INTR_DT_RD];

	if (usb2_clear_stall_callback(xfer, xfer_other)) {
		DPRINTF("stall cleared\n");
		sc->sc_flags &= ~AUE_FLAG_INTR_STALL;
		usb2_transfer_start(xfer_other);
	}
}

static void
aue_intr_callback(struct usb2_xfer *xfer)
{
	struct aue_softc *sc = xfer->priv_sc;
	struct ifnet *ifp = sc->sc_ifp;
	struct aue_intrpkt pkt;

	switch (USB_GET_STATE(xfer)) {
	case USB_ST_TRANSFERRED:

		if (ifp && (ifp->if_drv_flags & IFF_DRV_RUNNING) &&
		    (xfer->actlen >= sizeof(pkt))) {

			usb2_copy_out(xfer->frbuffers, 0, &pkt, sizeof(pkt));

			if (pkt.aue_txstat0) {
				ifp->if_oerrors++;
			}
			if (pkt.aue_txstat0 & (AUE_TXSTAT0_LATECOLL &
			    AUE_TXSTAT0_EXCESSCOLL)) {
				ifp->if_collisions++;
			}
		}
	case USB_ST_SETUP:
		if (sc->sc_flags & AUE_FLAG_INTR_STALL) {
			usb2_transfer_start(sc->sc_xfer[AUE_INTR_CS_RD]);
		} else {
			xfer->frlengths[0] = xfer->max_data_length;
			usb2_start_hardware(xfer);
		}
		return;

	default:			/* Error */
		if (xfer->error != USB_ERR_CANCELLED) {
			/* start clear stall */
			sc->sc_flags |= AUE_FLAG_INTR_STALL;
			usb2_transfer_start(sc->sc_xfer[AUE_INTR_CS_RD]);
		}
		return;
	}
}

static void
aue_bulk_read_clear_stall_callback(struct usb2_xfer *xfer)
{
	struct aue_softc *sc = xfer->priv_sc;
	struct usb2_xfer *xfer_other = sc->sc_xfer[AUE_BULK_DT_RD];

	if (usb2_clear_stall_callback(xfer, xfer_other)) {
		DPRINTF("stall cleared\n");
		sc->sc_flags &= ~AUE_FLAG_READ_STALL;
		usb2_transfer_start(xfer_other);
	}
}

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

	switch (USB_GET_STATE(xfer)) {
	case USB_ST_TRANSFERRED:
		DPRINTFN(11, "received %d bytes\n", xfer->actlen);

		if (sc->sc_flags & AUE_FLAG_VER_2) {

			if (xfer->actlen == 0) {
				ifp->if_ierrors++;
				goto tr_setup;
			}
		} else {

			if (xfer->actlen <= (4 + ETHER_CRC_LEN)) {
				ifp->if_ierrors++;
				goto tr_setup;
			}
			usb2_copy_out(xfer->frbuffers, xfer->actlen - 4, &sc->sc_rxpkt,
			    sizeof(sc->sc_rxpkt));

			/*
			 * turn off all the non-error bits in the rx status
			 * word:
			 */
			sc->sc_rxpkt.aue_rxstat &= AUE_RXSTAT_MASK;

			if (sc->sc_rxpkt.aue_rxstat) {
				ifp->if_ierrors++;
				goto tr_setup;
			}
			/* No errors; receive the packet. */
			xfer->actlen -= (4 + ETHER_CRC_LEN);
		}

		m = usb2_ether_get_mbuf();

		if (m == NULL) {
			ifp->if_ierrors++;
			goto tr_setup;
		}
		xfer->actlen = min(xfer->actlen, m->m_len);

		usb2_copy_out(xfer->frbuffers, 0, m->m_data, xfer->actlen);

		ifp->if_ipackets++;
		m->m_pkthdr.rcvif = ifp;
		m->m_pkthdr.len = m->m_len = xfer->actlen;

	case USB_ST_SETUP:
tr_setup:

		if (sc->sc_flags & AUE_FLAG_READ_STALL) {
			usb2_transfer_start(sc->sc_xfer[AUE_BULK_CS_RD]);
		} else {
			xfer->frlengths[0] = xfer->max_data_length;
			usb2_start_hardware(xfer);
		}

		/*
		 * 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!
		 */
		if (m) {
			mtx_unlock(&sc->sc_mtx);
			(ifp->if_input) (ifp, m);
			mtx_lock(&sc->sc_mtx);
		}
		return;

	default:			/* Error */
		if (xfer->error != USB_ERR_CANCELLED) {
			/* try to clear stall first */
			sc->sc_flags |= AUE_FLAG_READ_STALL;
			usb2_transfer_start(sc->sc_xfer[AUE_BULK_CS_RD]);
		}
		DPRINTF("bulk read error, %s\n",
		    usb2_errstr(xfer->error));
		return;

	}
}

static void
aue_bulk_write_clear_stall_callback(struct usb2_xfer *xfer)
{
	struct aue_softc *sc = xfer->priv_sc;
	struct usb2_xfer *xfer_other = sc->sc_xfer[AUE_BULK_DT_WR];

	if (usb2_clear_stall_callback(xfer, xfer_other)) {
		DPRINTF("stall cleared\n");
		sc->sc_flags &= ~AUE_FLAG_WRITE_STALL;
		usb2_transfer_start(xfer_other);
	}
}

static void
aue_bulk_write_callback(struct usb2_xfer *xfer)
{
	struct aue_softc *sc = xfer->priv_sc;
	struct ifnet *ifp = sc->sc_ifp;
	struct mbuf *m;
	uint8_t buf[2];

	switch (USB_GET_STATE(xfer)) {
	case USB_ST_TRANSFERRED:
		DPRINTFN(11, "transfer of %d bytes complete\n", xfer->actlen);

		ifp->if_opackets++;

	case USB_ST_SETUP:

		if (sc->sc_flags & AUE_FLAG_WRITE_STALL) {
			usb2_transfer_start(sc->sc_xfer[AUE_BULK_CS_WR]);
			goto done;
		}
		if (sc->sc_flags & AUE_FLAG_WAIT_LINK) {
			/*
			 * don't send anything if there is no link !
			 */
			goto done;
		}
		IFQ_DRV_DEQUEUE(&ifp->if_snd, m);

		if (m == NULL) {
			goto done;
		}
		if (m->m_pkthdr.len > MCLBYTES) {
			m->m_pkthdr.len = MCLBYTES;
		}
		if (sc->sc_flags & AUE_FLAG_VER_2) {

			xfer->frlengths[0] = m->m_pkthdr.len;

			usb2_m_copy_in(xfer->frbuffers, 0,
			    m, 0, m->m_pkthdr.len);

		} else {

			xfer->frlengths[0] = (m->m_pkthdr.len + 2);

			/*
		         * The ADMtek documentation says that the packet length is
		         * supposed to be specified in the first two bytes of the
		         * transfer, however it actually seems to ignore this info
		         * and base the frame size on the bulk transfer length.
		         */
			buf[0] = (uint8_t)(m->m_pkthdr.len);
			buf[1] = (uint8_t)(m->m_pkthdr.len >> 8);

			usb2_copy_in(xfer->frbuffers, 0, buf, 2);

			usb2_m_copy_in(xfer->frbuffers, 2,
			    m, 0, m->m_pkthdr.len);
		}

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

		m_freem(m);

		usb2_start_hardware(xfer);

done:
		return;

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

		if (xfer->error != USB_ERR_CANCELLED) {
			/* try to clear stall first */
			sc->sc_flags |= AUE_FLAG_WRITE_STALL;
			usb2_transfer_start(sc->sc_xfer[AUE_BULK_CS_WR]);
		}
		ifp->if_oerrors++;
		return;

	}
}

#define	AUE_BITS 6

static void
aue_mchash(struct usb2_config_td_cc *cc, const uint8_t *ptr)
{
	uint8_t h;

	h = ether_crc32_le(ptr, ETHER_ADDR_LEN) &
	    ((1 << AUE_BITS) - 1);
	cc->if_hash[(h >> 3)] |= (1 << (h & 7));
}

static void
aue_config_copy(struct aue_softc *sc,
    struct usb2_config_td_cc *cc, uint16_t refcount)
{
	bzero(cc, sizeof(*cc));
	usb2_ether_cc(sc->sc_ifp, &aue_mchash, cc);
}

static void
aue_cfg_tick(struct aue_softc *sc,
    struct usb2_config_td_cc *cc, uint16_t refcount)
{
	struct ifnet *ifp = sc->sc_ifp;
	struct mii_data *mii = GET_MII(sc);

	if ((ifp == NULL) ||
	    (mii == NULL)) {
		/* not ready */
		return;
	}
	mii_tick(mii);

	mii_pollstat(mii);

	if ((sc->sc_flags & AUE_FLAG_WAIT_LINK) &&
	    (mii->mii_media_status & IFM_ACTIVE) &&
	    (IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE)) {
		sc->sc_flags &= ~AUE_FLAG_WAIT_LINK;
	}
	sc->sc_media_active = mii->mii_media_active;
	sc->sc_media_status = mii->mii_media_status;

	/* start stopped transfers, if any */

	aue_start_transfers(sc);
}

static void
aue_start_cb(struct ifnet *ifp)
{
	struct aue_softc *sc = ifp->if_softc;

	mtx_lock(&sc->sc_mtx);

	aue_start_transfers(sc);

	mtx_unlock(&sc->sc_mtx);
}

static void
aue_init_cb(void *arg)
{
	struct aue_softc *sc = arg;

	mtx_lock(&sc->sc_mtx);
	usb2_config_td_queue_command
	    (&sc->sc_config_td, &aue_cfg_pre_init, &aue_cfg_init, 0, 0);
	mtx_unlock(&sc->sc_mtx);
}

static void
aue_start_transfers(struct aue_softc *sc)
{
	if ((sc->sc_flags & AUE_FLAG_LL_READY) &&
	    (sc->sc_flags & AUE_FLAG_HL_READY)) {

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

static void
aue_cfg_pre_init(struct aue_softc *sc,
    struct usb2_config_td_cc *cc, uint16_t refcount)
{
	struct ifnet *ifp = sc->sc_ifp;

	/* immediate configuration */

	aue_cfg_pre_stop(sc, cc, 0);

	ifp->if_drv_flags |= IFF_DRV_RUNNING;

	sc->sc_flags |= AUE_FLAG_HL_READY;
}

static void
aue_cfg_init(struct aue_softc *sc,
    struct usb2_config_td_cc *cc, uint16_t refcount)
{
	struct mii_data *mii = GET_MII(sc);
	uint8_t i;

	/*
	 * Cancel pending I/O
	 */
	aue_cfg_stop(sc, cc, 0);

	/* Set MAC address */
	for (i = 0; i < ETHER_ADDR_LEN; i++) {
		aue_cfg_csr_write_1(sc, AUE_PAR0 + i, cc->if_lladdr[i]);
	}

	/* update promiscuous setting */
	aue_cfg_promisc_upd(sc, cc, 0);

	/* load the multicast filter */
	aue_cfg_setmulti(sc, cc, 0);

	/* enable RX and TX */
	aue_cfg_csr_write_1(sc, AUE_CTL0,
	    (AUE_CTL0_RXSTAT_APPEND |
	    AUE_CTL0_RX_ENB));

	AUE_CFG_SETBIT(sc, AUE_CTL0, AUE_CTL0_TX_ENB);
	AUE_CFG_SETBIT(sc, AUE_CTL2, AUE_CTL2_EP3_CLR);

	mii_mediachg(mii);

	sc->sc_flags |= (AUE_FLAG_READ_STALL |
	    AUE_FLAG_WRITE_STALL |
	    AUE_FLAG_LL_READY);

	aue_start_transfers(sc);
}

static void
aue_cfg_promisc_upd(struct aue_softc *sc,
    struct usb2_config_td_cc *cc, uint16_t refcount)
{
	/* if we want promiscuous mode, set the allframes bit: */
	if (cc->if_flags & IFF_PROMISC) {
		AUE_CFG_SETBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
	} else {
		AUE_CFG_CLRBIT(sc, AUE_CTL2, AUE_CTL2_RX_PROMISC);
	}
}

/*
 * Set media options.
 */
static int
aue_ifmedia_upd_cb(struct ifnet *ifp)
{
	struct aue_softc *sc = ifp->if_softc;

	mtx_lock(&sc->sc_mtx);
	usb2_config_td_queue_command
	    (&sc->sc_config_td, NULL, &aue_cfg_ifmedia_upd, 0, 0);
	mtx_unlock(&sc->sc_mtx);

	return (0);
}

static void
aue_cfg_ifmedia_upd(struct aue_softc *sc,
    struct usb2_config_td_cc *cc, uint16_t refcount)
{
	struct ifnet *ifp = sc->sc_ifp;
	struct mii_data *mii = GET_MII(sc);

	if ((ifp == NULL) ||
	    (mii == NULL)) {
		/* not ready */
		return;
	}
	sc->sc_flags |= AUE_FLAG_WAIT_LINK;

	if (mii->mii_instance) {
		struct mii_softc *miisc;

		LIST_FOREACH(miisc, &mii->mii_phys, mii_list) {
			mii_phy_reset(miisc);
		}
	}
	mii_mediachg(mii);
}

/*
 * Report current media status.
 */
static void
aue_ifmedia_sts_cb(struct ifnet *ifp, struct ifmediareq *ifmr)
{
	struct aue_softc *sc = ifp->if_softc;

	mtx_lock(&sc->sc_mtx);

	ifmr->ifm_active = sc->sc_media_active;
	ifmr->ifm_status = sc->sc_media_status;

	mtx_unlock(&sc->sc_mtx);
}

static int
aue_ioctl_cb(struct ifnet *ifp, u_long command, caddr_t data)
{
	struct aue_softc *sc = ifp->if_softc;
	struct mii_data *mii;
	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) {
				usb2_config_td_queue_command
				    (&sc->sc_config_td, &aue_config_copy,
				    &aue_cfg_promisc_upd, 0, 0);
			} else {
				usb2_config_td_queue_command
				    (&sc->sc_config_td, &aue_cfg_pre_init,
				    &aue_cfg_init, 0, 0);
			}
		} else {
			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
				usb2_config_td_queue_command
				    (&sc->sc_config_td, &aue_cfg_pre_stop,
				    &aue_cfg_stop, 0, 0);
			}
		}
		mtx_unlock(&sc->sc_mtx);
		break;

	case SIOCADDMULTI:
	case SIOCDELMULTI:
		mtx_lock(&sc->sc_mtx);
		usb2_config_td_queue_command
		    (&sc->sc_config_td, &aue_config_copy,
		    &aue_cfg_setmulti, 0, 0);
		mtx_unlock(&sc->sc_mtx);
		break;

	case SIOCGIFMEDIA:
	case SIOCSIFMEDIA:
		mii = GET_MII(sc);
		if (mii == NULL) {
			error = EINVAL;
		} else {
			error = ifmedia_ioctl
			    (ifp, (void *)data, &mii->mii_media, command);
		}
		break;

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

static void
aue_watchdog(void *arg)
{
	struct aue_softc *sc = arg;

	mtx_assert(&sc->sc_mtx, MA_OWNED);

	usb2_config_td_queue_command
	    (&sc->sc_config_td, NULL, &aue_cfg_tick, 0, 0);

	usb2_callout_reset(&sc->sc_watchdog,
	    hz, &aue_watchdog, sc);
}

/*
 * Stop the adapter and free any mbufs allocated to the
 * RX and TX lists.
 *
 * NOTE: can be called when "ifp" is NULL
 */
static void
aue_cfg_pre_stop(struct aue_softc *sc,
    struct usb2_config_td_cc *cc, uint16_t refcount)
{
	struct ifnet *ifp = sc->sc_ifp;

	if (cc) {
		/* copy the needed configuration */
		aue_config_copy(sc, cc, refcount);
	}
	/* immediate configuration */

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

	sc->sc_flags |= AUE_FLAG_WAIT_LINK;

	/*
	 * stop all the transfers, if not already stopped:
	 */
	usb2_transfer_stop(sc->sc_xfer[AUE_BULK_DT_WR]);
	usb2_transfer_stop(sc->sc_xfer[AUE_BULK_DT_RD]);
	usb2_transfer_stop(sc->sc_xfer[AUE_BULK_CS_WR]);
	usb2_transfer_stop(sc->sc_xfer[AUE_BULK_CS_RD]);
	usb2_transfer_stop(sc->sc_xfer[AUE_INTR_DT_RD]);
	usb2_transfer_stop(sc->sc_xfer[AUE_INTR_CS_RD]);
}

static void
aue_cfg_stop(struct aue_softc *sc,
    struct usb2_config_td_cc *cc, uint16_t refcount)
{
	aue_cfg_csr_write_1(sc, AUE_CTL0, 0);
	aue_cfg_csr_write_1(sc, AUE_CTL1, 0);
	aue_cfg_reset(sc);
}

/*
 * Stop all chip I/O so that the kernel's probe routines don't
 * get confused by errant DMAs when rebooting.
 */
static int
aue_shutdown(device_t dev)
{
	struct aue_softc *sc = device_get_softc(dev);

	mtx_lock(&sc->sc_mtx);

	usb2_config_td_queue_command
	    (&sc->sc_config_td, &aue_cfg_pre_stop,
	    &aue_cfg_stop, 0, 0);

	mtx_unlock(&sc->sc_mtx);

	return (0);
}
OpenPOWER on IntegriCloud