summaryrefslogtreecommitdiff
path: root/drivers/net/usb/asix.c
blob: 3bbed1d376f9a3ddcc1a0f578d160bcf23d4d481 (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
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
/*
 * ASIX AX8817X based USB 2.0 Ethernet Devices
 * Copyright (C) 2003-2005 David Hollis <dhollis@davehollis.com>
 * Copyright (C) 2005 Phil Chang <pchang23@sbcglobal.net>
 * Copyright (c) 2002-2003 TiVo Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

//#define	DEBUG			// debug messages, extra info

#include <linux/version.h>
//#include <linux/config.h>
#ifdef	CONFIG_USB_DEBUG
#   define DEBUG
#endif
#include <linux/module.h>
#include <linux/kmod.h>
#include <linux/sched.h>
#include <linux/init.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/ethtool.h>
#include <linux/workqueue.h>
#include <linux/mii.h>
#include <linux/usb.h>
#include <linux/crc32.h>

#include "axusbnet.c"
#include "asix.h"

#define DRV_VERSION	"4.2.0"

static char version[] =
KERN_INFO "ASIX USB Ethernet Adapter:v" DRV_VERSION 
	" " __TIME__ " " __DATE__ "\n"
KERN_INFO "    http://www.asix.com.tw\n";

static char g_mac_addr[ETH_ALEN];
static int g_usr_mac = 0;

/* configuration of maximum bulk in size */
static int bsize = AX88772B_MAX_BULKIN_16K;
module_param (bsize, int, 0);
MODULE_PARM_DESC (bsize, "Maximum transfer size per bulk");

#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
static void ax88772b_link_reset (void *data);
static void ax88772a_link_reset (void *data);
static void ax88772_link_reset (void *data);
#else
static void ax88772b_link_reset (struct work_struct *work);
static void ax88772a_link_reset (struct work_struct *work);
static void ax88772_link_reset (struct work_struct *work);
#endif
static int ax88772a_phy_powerup (struct usbnet *dev);

/* Retrieve user set MAC address */
static int __init setup_asix_mac(char *macstr)
{
	int i, j;
	unsigned char result, value;

	for (i = 0; i < ETH_ALEN; i++) {
		result = 0;

		if (i != 5 && *(macstr + 2) != ':')
			return -1;

		for (j = 0; j < 2; j++) {
			if (isxdigit(*macstr)
			    && (value =
				isdigit(*macstr) ? *macstr -
				'0' : toupper(*macstr) - 'A' + 10) < 16) {
				result = result * 16 + value;
				macstr++;
			} else
				return -1;
		}

		macstr++;
		g_mac_addr[i] = result;
	}

	g_usr_mac = 1;

	return 0;
}

__setup("asix_mac=", setup_asix_mac);

/* ASIX AX8817X based USB 2.0 Ethernet Devices */

static int ax8817x_read_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
			    u16 size, void *data)
{
	return usb_control_msg(
		dev->udev,
		usb_rcvctrlpipe(dev->udev, 0),
		cmd,
		USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
		value,
		index,
		data,
		size,
		USB_CTRL_GET_TIMEOUT);
}

static int ax8817x_write_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
			     u16 size, void *data)
{
	return usb_control_msg(
		dev->udev,
		usb_sndctrlpipe(dev->udev, 0),
		cmd,
		USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
		value,
		index,
		data,
		size,
		USB_CTRL_SET_TIMEOUT);
}

#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
static void ax8817x_async_cmd_callback(struct urb *urb, struct pt_regs *regs)
#else
static void ax8817x_async_cmd_callback(struct urb *urb)
#endif
{
	struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context;

	if (urb->status < 0)
		printk(KERN_DEBUG "ax8817x_async_cmd_callback() failed with %d",
			urb->status);

	kfree(req);
	usb_free_urb(urb);
}

static int ax8817x_set_mac_addr (struct net_device *net, void *p)
{
	struct usbnet *dev = netdev_priv(net);
	struct sockaddr *addr = p;

	memcpy (net->dev_addr, addr->sa_data, ETH_ALEN);

	/* Set the MAC address */
	return ax8817x_write_cmd (dev, AX88772_CMD_WRITE_NODE_ID,
			   0, 0, ETH_ALEN, net->dev_addr);

}

static void ax88178_status(struct usbnet *dev, struct urb *urb)
{
	struct ax88172_int_data *event;
	struct ax88178_data *ax178dataptr = (struct ax88178_data *)dev->priv;
	int link;

	if (urb->actual_length < 8)
		return;

	if (ax178dataptr->EepromData == PHY_MODE_MAC_TO_MAC_GMII)
		return;

	event = urb->transfer_buffer;
	link = event->link & 0x01;
	if (netif_carrier_ok(dev->net) != link) {
		if (link) {
			netif_carrier_on(dev->net);
			axusbnet_defer_kevent (dev, EVENT_LINK_RESET);
		} else
			netif_carrier_off(dev->net);
		devwarn(dev, "ax88178 - Link status is: %d", link);
	}
}

static void ax8817x_status(struct usbnet *dev, struct urb *urb)
{
	struct ax88172_int_data *event;
	int link;

	if (urb->actual_length < 8)
		return;

	event = urb->transfer_buffer;
	link = event->link & 0x01;
	if (netif_carrier_ok(dev->net) != link) {
		if (link) {
			netif_carrier_on(dev->net);
			axusbnet_defer_kevent (dev, EVENT_LINK_RESET );
		} else
			netif_carrier_off(dev->net);
		devwarn(dev, "ax8817x - Link status is: %d", link);
	}
}

static void ax88772_status(struct usbnet *dev, struct urb *urb)
{
	struct ax88172_int_data *event;
	struct ax88772_data *ax772_data = (struct ax88772_data *)dev->priv;
	int link;
	
	if (urb->actual_length < 8)
		return;

	event = urb->transfer_buffer;
	link = event->link & 0x01;
	
	if (netif_carrier_ok(dev->net) != link) {
		if (link) {
			netif_carrier_on(dev->net);
			ax772_data->Event = AX_SET_RX_CFG;
		} else {
			netif_carrier_off(dev->net);
			if (ax772_data->Event == AX_NOP) {
				ax772_data->Event = PHY_POWER_DOWN;
				ax772_data->TickToExpire = 25;
			}
		}

		devwarn(dev, "ax88772 - Link status is: %d", link);
	}
	
	if (ax772_data->Event)
		queue_work (ax772_data->ax_work, &ax772_data->check_link);
}

static void ax88772a_status(struct usbnet *dev, struct urb *urb)
{
	struct ax88172_int_data *event;
	struct ax88772a_data *ax772a_data = (struct ax88772a_data *)dev->priv;
	int link;
	int PowSave = (ax772a_data->EepromData >> 14);

	if (urb->actual_length < 8)
		return;

	event = urb->transfer_buffer;
	link = event->link & 0x01;

	if (netif_carrier_ok(dev->net) != link) {

		if (link) {
			netif_carrier_on(dev->net);
			ax772a_data->Event = AX_SET_RX_CFG;
		} else if ((PowSave == 0x3) || (PowSave == 0x1)) {
			netif_carrier_off(dev->net);
			if (ax772a_data->Event == AX_NOP) {
				ax772a_data->Event = CHK_CABLE_EXIST;
				ax772a_data->TickToExpire = 14;
			}
		} else {
			netif_carrier_off(dev->net);
			ax772a_data->Event = AX_NOP;
		}

		devwarn(dev, "ax88772a - Link status is: %d", link);
	}
	
	if (ax772a_data->Event)
		queue_work (ax772a_data->ax_work, &ax772a_data->check_link);
}

static void ax88772b_status(struct usbnet *dev, struct urb *urb)
{
	struct ax88772b_data *ax772b_data = (struct ax88772b_data *)dev->priv;
	struct ax88172_int_data *event;
	int link;

	if (urb->actual_length < 8)
		return;

	if (ax772b_data->OperationMode == OPERATION_PHY_MODE)
		return;

	event = urb->transfer_buffer;
	if (ax772b_data->PhySelect == 0 && ax772b_data->OperationMode 
							== OPERATION_MAC_MODE)
		link = event->link & AX_INT_SPLS_LINK;
	else
		link = event->link & AX_INT_PPLS_LINK;

	if (netif_carrier_ok(dev->net) != link) {
		if (link) {
			netif_carrier_on(dev->net);
			ax772b_data->Event = AX_SET_RX_CFG;
		} else {
			netif_carrier_off(dev->net);
			ax772b_data->time_to_chk = jiffies;
		}
		devwarn(dev, "ax88772b - Link status is: %d", link);
	}

	if (!link) {

		int no_cable = (event->link & AX_INT_CABOFF_UNPLUG) ? 1 : 0;

		if (no_cable) {
			if ((ax772b_data->psc & 
			    (AX_SWRESET_IPPSL_0 | AX_SWRESET_IPPSL_1)) &&
			     !ax772b_data->pw_enabled) {
				/* 
				 * AX88772B already entered power saving state
				 */
				ax772b_data->pw_enabled = 1;
			}

		} else {
			/* AX88772B resumed from power saving state */
			if (ax772b_data->pw_enabled || 
				(jiffies > (ax772b_data->time_to_chk + 
				 AX88772B_WATCHDOG))) {
				if (ax772b_data->pw_enabled)
					ax772b_data->pw_enabled = 0;
				ax772b_data->Event = PHY_POWER_UP;
				ax772b_data->time_to_chk = jiffies;
			}
		}
	}

	if (ax772b_data->Event)
		queue_work (ax772b_data->ax_work, &ax772b_data->check_link);
}

void
ax8817x_write_cmd_async(struct usbnet *dev, u8 cmd, u16 value, u16 index,
				    u16 size, void *data)
{
	struct usb_ctrlrequest *req;
	int status;
	struct urb *urb;

	if ((urb = usb_alloc_urb(0, GFP_ATOMIC)) == NULL) {
		deverr(dev, "Error allocating URB in write_cmd_async!");
		return;
	}

	if ((req = kmalloc (sizeof (struct usb_ctrlrequest),
			    GFP_ATOMIC)) == NULL) {
		deverr(dev, "Failed to allocate memory for control request");
		usb_free_urb(urb);
		return;
	}

	req->bRequestType = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE;
	req->bRequest = cmd;
	req->wValue = cpu_to_le16(value);
	req->wIndex = cpu_to_le16(index);
	req->wLength = cpu_to_le16(size);

	usb_fill_control_urb(urb, dev->udev,
			     usb_sndctrlpipe(dev->udev, 0),
			     (void *)req, data, size,
			     ax8817x_async_cmd_callback, req);

	if((status = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
		deverr(dev, "Error submitting the control message: status=%d",
				status);
		kfree(req);
		usb_free_urb(urb);
	}
}

static void ax8817x_set_multicast(struct net_device *net)
{
	struct usbnet *dev = netdev_priv(net);
	struct ax8817x_data *data = (struct ax8817x_data *)&dev->data;
	u8 rx_ctl = AX_RX_CTL_START | AX_RX_CTL_AB;
	int mc_count;

#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35)
	mc_count = net->mc_count;
#else
	mc_count = netdev_mc_count (net);
#endif

	if (net->flags & IFF_PROMISC) {
		rx_ctl |= AX_RX_CTL_PRO;
	} else if (net->flags & IFF_ALLMULTI
		   || mc_count > AX_MAX_MCAST) {
		rx_ctl |= AX_RX_CTL_AMALL;
	} else if (mc_count == 0) {
		/* just broadcast and directed */
	} else {
		/* We use the 20 byte dev->data
		 * for our 8 byte filter buffer
		 * to avoid allocating memory that
		 * is tricky to free later */
		u32 crc_bits;
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35)
		struct dev_mc_list *mc_list = net->mc_list;
		int i;

		memset(data->multi_filter, 0, AX_MCAST_FILTER_SIZE);

		/* Build the multicast hash filter. */
		for (i = 0; i < net->mc_count; i++) {
			crc_bits =
			    ether_crc(ETH_ALEN,
				      mc_list->dmi_addr) >> 26;
			data->multi_filter[crc_bits >> 3] |=
			    1 << (crc_bits & 7);
			mc_list = mc_list->next;
		}
#else
		struct netdev_hw_addr *ha;
		memset(data->multi_filter, 0, AX_MCAST_FILTER_SIZE);
		netdev_for_each_mc_addr (ha, net) {
			crc_bits = ether_crc(ETH_ALEN, ha->addr) >> 26;
			data->multi_filter[crc_bits >> 3] |=
				1 << (crc_bits & 7);
		}
#endif
		ax8817x_write_cmd_async(dev, AX_CMD_WRITE_MULTI_FILTER, 0, 0,
				   AX_MCAST_FILTER_SIZE, data->multi_filter);

		rx_ctl |= AX_RX_CTL_AM;
	}

	ax8817x_write_cmd_async(dev, AX_CMD_WRITE_RX_CTL, rx_ctl, 0, 0, NULL);
}

static void ax88772b_set_multicast(struct net_device *net)
{
	struct usbnet *dev = netdev_priv(net);
	struct ax8817x_data *data = (struct ax8817x_data *)&dev->data;
	u16 rx_ctl = (AX_RX_CTL_START | AX_RX_CTL_AB | AX_RX_HEADER_DEFAULT);
	int mc_count;

#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35)
	mc_count = net->mc_count;
#else
	mc_count = netdev_mc_count (net);
#endif

	if (net->flags & IFF_PROMISC) {
		rx_ctl |= AX_RX_CTL_PRO;
	} else if (net->flags & IFF_ALLMULTI
		   || mc_count > AX_MAX_MCAST) {
		rx_ctl |= AX_RX_CTL_AMALL;
	} else if (mc_count == 0) {
		/* just broadcast and directed */
	} else {
		/* We use the 20 byte dev->data
		 * for our 8 byte filter buffer
		 * to avoid allocating memory that
		 * is tricky to free later */
		u32 crc_bits;

#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35)
		struct dev_mc_list *mc_list = net->mc_list;
		int i;

		memset(data->multi_filter, 0, AX_MCAST_FILTER_SIZE);

		/* Build the multicast hash filter. */
		for (i = 0; i < net->mc_count; i++) {
			crc_bits =
			    ether_crc(ETH_ALEN,
				      mc_list->dmi_addr) >> 26;
			data->multi_filter[crc_bits >> 3] |=
			    1 << (crc_bits & 7);
			mc_list = mc_list->next;
		}
#else
		struct netdev_hw_addr *ha;
		memset(data->multi_filter, 0, AX_MCAST_FILTER_SIZE);
		netdev_for_each_mc_addr (ha, net) {
			crc_bits = ether_crc(ETH_ALEN, ha->addr) >> 26;
			data->multi_filter[crc_bits >> 3] |=
				1 << (crc_bits & 7);
		}
#endif
		ax8817x_write_cmd_async(dev, AX_CMD_WRITE_MULTI_FILTER, 0, 0,
				   AX_MCAST_FILTER_SIZE, data->multi_filter);

		rx_ctl |= AX_RX_CTL_AM;
	}

	ax8817x_write_cmd_async(dev, AX_CMD_WRITE_RX_CTL, rx_ctl, 0, 0, NULL);
}

static int ax8817x_mdio_read(struct net_device *netdev, int phy_id, int loc)
{
	struct usbnet *dev = netdev_priv(netdev);
	u16 *res;
	u16 ret;

	res = kmalloc (2, GFP_ATOMIC);
	if (!res)
		return 0;

	ax8817x_write_cmd(dev, AX_CMD_SET_SW_MII, 0, 0, 0, NULL);
	ax8817x_read_cmd(dev, AX_CMD_READ_MII_REG, phy_id, (__u16)loc, 2, res);
	ax8817x_write_cmd(dev, AX_CMD_SET_HW_MII, 0, 0, 0, NULL);

	ret = *res & 0xffff;
	kfree (res);

	return ret;
}

static int 
ax8817x_swmii_mdio_read(struct net_device *netdev, int phy_id, int loc)
{
	struct usbnet *dev = netdev_priv(netdev);
	u16 *res;
	u16 ret;

	res = kmalloc (2, GFP_ATOMIC);
	if (!res)
		return 0;

	ax8817x_read_cmd(dev, AX_CMD_READ_MII_REG, phy_id,
				(__u16)loc, 2, res);

	ret = *res & 0xffff;
	kfree (res);

	return ret;
}

/* same as above, but converts resulting value to cpu byte order */
static int ax8817x_mdio_read_le(struct net_device *netdev, int phy_id, int loc)
{
	return le16_to_cpu(ax8817x_mdio_read(netdev,phy_id, loc));
}

static int 
ax8817x_swmii_mdio_read_le(struct net_device *netdev, int phy_id, int loc)
{
	return le16_to_cpu(ax8817x_swmii_mdio_read(netdev,phy_id, loc));
}

static void
ax8817x_mdio_write(struct net_device *netdev, int phy_id, int loc, int val)
{
	struct usbnet *dev = netdev_priv(netdev);
	u16 *res;

	res = kmalloc (2, GFP_ATOMIC);
	if (!res)
		return;
	*res = val;

	ax8817x_write_cmd(dev, AX_CMD_SET_SW_MII, 0, 0, 0, NULL);
	ax8817x_write_cmd(dev, AX_CMD_WRITE_MII_REG, phy_id,
				(__u16)loc, 2, res);
	ax8817x_write_cmd(dev, AX_CMD_SET_HW_MII, 0, 0, 0, NULL);

	kfree (res);
}

static void ax8817x_swmii_mdio_write(struct net_device *netdev, 
			int phy_id, int loc, int val)
{
	struct usbnet *dev = netdev_priv(netdev);
	u16 *res;

	res = kmalloc (2, GFP_ATOMIC);
	if (!res)
		return;
	*res = val;

	ax8817x_write_cmd(dev, AX_CMD_WRITE_MII_REG, phy_id,
				(__u16)loc, 2, res);

	kfree (res);
}

static void
ax88772b_mdio_write(struct net_device *netdev, int phy_id, int loc, int val)
{
	struct usbnet *dev = netdev_priv(netdev);
	u16 *res;

	res = kmalloc (2, GFP_ATOMIC);
	if (!res)
		return;
	*res = val;

	ax8817x_write_cmd(dev, AX_CMD_SET_SW_MII, 0, 0, 0, NULL);
	ax8817x_write_cmd(dev, AX_CMD_WRITE_MII_REG, phy_id,
				(__u16)loc, 2, res);

	if (loc == MII_ADVERTISE) {
		*res = cpu_to_le16(BMCR_ANENABLE | BMCR_ANRESTART);
		ax8817x_write_cmd(dev, AX_CMD_WRITE_MII_REG, phy_id,
				(__u16)MII_BMCR, 2, res);
	}

	ax8817x_write_cmd(dev, AX_CMD_SET_HW_MII, 0, 0, 0, NULL);

	kfree (res);
}

/* same as above, but converts new value to le16 byte order before writing */
static void
ax8817x_mdio_write_le(struct net_device *netdev, int phy_id, int loc, int val)
{
	ax8817x_mdio_write( netdev, phy_id, loc, cpu_to_le16(val) );
}

static void ax8817x_swmii_mdio_write_le(struct net_device *netdev,
			int phy_id, int loc, int val)
{
	ax8817x_swmii_mdio_write( netdev, phy_id, loc, cpu_to_le16(val) );
}

static void
ax88772b_mdio_write_le(struct net_device *netdev, int phy_id, int loc, int val)
{
	ax88772b_mdio_write( netdev, phy_id, loc, cpu_to_le16(val) );
}

static int ax88772_suspend (struct usb_interface *intf,
#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,10)
			pm_message_t message)
#else
			u32 message)
#endif
{
	struct usbnet *dev = usb_get_intfdata(intf);
	u16 *medium;

	medium = kmalloc (2, GFP_ATOMIC);
	if (!medium)
		return axusbnet_suspend (intf, message);

	ax8817x_read_cmd (dev, AX_CMD_READ_MEDIUM_MODE, 0, 0, 2, medium);
	ax8817x_write_cmd (dev, AX_CMD_WRITE_MEDIUM_MODE,
			(*medium & ~AX88772_MEDIUM_RX_ENABLE), 0, 0, NULL);

	kfree (medium);
	return axusbnet_suspend (intf, message);
}

static int ax88772b_suspend (struct usb_interface *intf,
#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,10)
			pm_message_t message)
#else
			u32 message)
#endif
{
	struct usbnet *dev = usb_get_intfdata(intf);
	struct ax88772b_data *ax772b_data = (struct ax88772b_data *)dev->priv;
	u16 *tmp16;
	u8 *opt;

	tmp16 = kmalloc (2, GFP_ATOMIC);
	if (!tmp16)
		return axusbnet_suspend (intf, message);
	opt = (u8 *)tmp16;

	ax8817x_read_cmd (dev, AX_CMD_READ_MEDIUM_MODE, 0, 0, 2, tmp16);
	ax8817x_write_cmd (dev, AX_CMD_WRITE_MEDIUM_MODE,
			(*tmp16 & ~AX88772_MEDIUM_RX_ENABLE), 0, 0, NULL);

	ax8817x_read_cmd(dev, AX_CMD_READ_MONITOR_MODE, 0, 0, 1, opt);
	if (!(*opt & AX_MONITOR_LINK) && !(*opt & AX_MONITOR_MAGIC)) {
		ax8817x_write_cmd (dev, AX_CMD_SW_RESET,
			AX_SWRESET_IPRL | AX_SWRESET_IPPD, 0, 0, NULL);
	} else {

		if (ax772b_data->psc & AX_SWRESET_WOLLP) {
			*tmp16 = ax8817x_mdio_read_le (dev->net,
					dev->mii.phy_id, MII_BMCR);
			ax8817x_mdio_write_le (dev->net, dev->mii.phy_id,
					MII_BMCR, *tmp16 | BMCR_ANENABLE);

			ax8817x_write_cmd (dev, AX_CMD_SW_RESET,
				AX_SWRESET_IPRL | ax772b_data->psc, 0, 0, NULL);
		}

		if (ax772b_data->psc &
		    (AX_SWRESET_IPPSL_0 | AX_SWRESET_IPPSL_1)) {
			*opt |= AX_MONITOR_LINK;
			ax8817x_write_cmd(dev, AX_CMD_WRITE_MONITOR_MODE,
					*opt, 0, 0, NULL);
		}
	}

	kfree (tmp16);
	return axusbnet_suspend (intf, message);
}

static int ax88772_resume (struct usb_interface *intf)
{
	struct usbnet *dev = usb_get_intfdata(intf);

	netif_carrier_off (dev->net);

	return axusbnet_resume (intf);
}

static int ax88772b_resume (struct usb_interface *intf)
{
	struct usbnet *dev = usb_get_intfdata(intf);
	struct ax88772b_data *ax772b_data = (struct ax88772b_data *)dev->priv;

	if (ax772b_data->psc & AX_SWRESET_WOLLP) {
		ax8817x_write_cmd (dev, AX_CMD_SW_RESET,
				AX_SWRESET_IPRL | (ax772b_data->psc & 0x7FFF),
				0, 0, NULL);
	}

	if (ax772b_data->psc & (AX_SWRESET_IPPSL_0 | AX_SWRESET_IPPSL_1)) {
		ax88772a_phy_powerup (dev);
	}

	netif_carrier_off (dev->net);

	return axusbnet_resume (intf);
}

static int ax88172_link_reset(struct usbnet *dev)
{
	u16 lpa;
	u16 adv;
	u16 res;
	u8 mode;

	mode = AX_MEDIUM_TX_ABORT_ALLOW | AX_MEDIUM_FLOW_CONTROL_EN;
	lpa = ax8817x_mdio_read_le(dev->net, dev->mii.phy_id, MII_LPA);
	adv = ax8817x_mdio_read_le(dev->net, dev->mii.phy_id, MII_ADVERTISE);
	res = mii_nway_result(lpa|adv);
	if (res & LPA_DUPLEX)
		mode |= AX_MEDIUM_FULL_DUPLEX;
	ax8817x_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE, mode, 0, 0, NULL);

	return 0;
}

static void
ax8817x_get_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
{
	struct usbnet *dev = netdev_priv(net);
	u8 *opt;

	wolinfo->supported = 0;
	wolinfo->wolopts = 0;

	opt = kmalloc (1, GFP_KERNEL);
	if (!opt)
		return;

	if (ax8817x_read_cmd(dev, AX_CMD_READ_MONITOR_MODE, 0, 0, 1, opt) < 0)
		return;

	wolinfo->supported = WAKE_PHY | WAKE_MAGIC;

	if (*opt & AX_MONITOR_LINK)
		wolinfo->wolopts |= WAKE_PHY;
	if (*opt & AX_MONITOR_MAGIC)
		wolinfo->wolopts |= WAKE_MAGIC;

	kfree (opt);
}

static int
ax8817x_set_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
{
	struct usbnet *dev = netdev_priv(net);
	u8 *opt;

	opt = kmalloc (1, GFP_KERNEL);
	if (!opt)
		return -ENOMEM;

	*opt = 0;
	if (wolinfo->wolopts & WAKE_PHY)
		*opt |= AX_MONITOR_LINK;
	if (wolinfo->wolopts & WAKE_MAGIC)
		*opt |= AX_MONITOR_MAGIC;

	ax8817x_write_cmd(dev, AX_CMD_WRITE_MONITOR_MODE, *opt, 0, 0, NULL);

	kfree (opt);
	return 0;
}

static int ax8817x_get_eeprom_len(struct net_device *net)
{
	return AX_EEPROM_LEN;
}

static int ax8817x_get_eeprom(struct net_device *net,
			      struct ethtool_eeprom *eeprom, u8 *data)
{
	struct usbnet *dev = netdev_priv(net);
	u16 *ebuf = (u16 *)data;
	int i;

	/* Crude hack to ensure that we don't overwrite memory
	 * if an odd length is supplied
	 */
	if (eeprom->len % 2)
		return -EINVAL;

	eeprom->magic = AX_EEPROM_MAGIC;

	/* ax8817x returns 2 bytes from eeprom on read */
	for (i=0; i < eeprom->len / 2; i++) {
		if (ax8817x_read_cmd(dev, AX_CMD_READ_EEPROM,
			eeprom->offset + i, 0, 2, &ebuf[i]) < 0)
			return -EINVAL;
	}
	return 0;
}

static void ax8817x_get_drvinfo (struct net_device *net,
				 struct ethtool_drvinfo *info)
{
	/* Inherit standard device info */
	axusbnet_get_drvinfo(net, info);
	info->eedump_len = 0x3e;
}

static int ax8817x_get_settings(struct net_device *net, struct ethtool_cmd *cmd)
{
	struct usbnet *dev = netdev_priv(net);
	return mii_ethtool_gset(&dev->mii,cmd);
}

static int ax8817x_set_settings(struct net_device *net, struct ethtool_cmd *cmd)
{
	struct usbnet *dev = netdev_priv(net);
	return mii_ethtool_sset(&dev->mii,cmd);
}

/* We need to override some ethtool_ops so we require our
   own structure so we don't interfere with other usbnet
   devices that may be connected at the same time. */
static struct ethtool_ops ax8817x_ethtool_ops = {
	.get_drvinfo		= ax8817x_get_drvinfo,
	.get_link		= ethtool_op_get_link,
	.get_msglevel		= axusbnet_get_msglevel,
	.set_msglevel		= axusbnet_set_msglevel,
	.get_wol		= ax8817x_get_wol,
	.set_wol		= ax8817x_set_wol,
	.get_eeprom_len	= ax8817x_get_eeprom_len,
	.get_eeprom		= ax8817x_get_eeprom,
	.get_settings		= ax8817x_get_settings,
	.set_settings		= ax8817x_set_settings,
};

static int ax8817x_ioctl (struct net_device *net, struct ifreq *rq, int cmd)
{
	struct usbnet *dev = netdev_priv(net);

	return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
}

#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,29)
static const struct net_device_ops ax88x72_netdev_ops = {
	.ndo_open			= axusbnet_open,
	.ndo_stop			= axusbnet_stop,
	.ndo_start_xmit	= axusbnet_start_xmit,
	.ndo_tx_timeout	= axusbnet_tx_timeout,
	.ndo_change_mtu	= axusbnet_change_mtu,
	.ndo_get_stats		= axusbnet_get_stats,
	.ndo_do_ioctl		= ax8817x_ioctl,
	.ndo_set_mac_address		= ax8817x_set_mac_addr,
	.ndo_validate_addr		= eth_validate_addr,
	.ndo_set_multicast_list	= ax8817x_set_multicast,
};
#endif

static int ax8817x_bind(struct usbnet *dev, struct usb_interface *intf)
{
	int ret = 0;
	void *buf;
	int i;
	unsigned long gpio_bits = dev->driver_info->data;
	struct ax8817x_data *data = (struct ax8817x_data *)&dev->data;

	axusbnet_get_endpoints(dev,intf);

	buf = kmalloc(ETH_ALEN, GFP_KERNEL);
	if(!buf) {
		ret = -ENOMEM;
		goto out1;
	}

	/* Toggle the GPIOs in a manufacturer/model specific way */
	for (i = 2; i >= 0; i--) {
		if ((ret = ax8817x_write_cmd(dev, AX_CMD_WRITE_GPIOS,
					(gpio_bits >> (i * 8)) & 0xff, 0, 0,
					NULL)) < 0)
			goto out2;
		msleep(5);
	}

	if ((ret = ax8817x_write_cmd(dev, AX_CMD_WRITE_RX_CTL,
				0x80, 0, 0, NULL)) < 0) {
		deverr(dev, "send AX_CMD_WRITE_RX_CTL failed: %d", ret);
		goto out2;
	}

	/* Get the MAC address */
	memset(buf, 0, ETH_ALEN);
	if ((ret = ax8817x_read_cmd(dev, AX_CMD_READ_NODE_ID,
				0, 0, 6, buf)) < 0) {
		deverr(dev, "read AX_CMD_READ_NODE_ID failed: %d", ret);
		goto out2;
	}
	memcpy(dev->net->dev_addr, buf, ETH_ALEN);

	/* Get the PHY id */
	if ((ret = ax8817x_read_cmd(dev, AX_CMD_READ_PHY_ID,
				0, 0, 2, buf)) < 0) {
		deverr(dev, "error on read AX_CMD_READ_PHY_ID: %02x", ret);
		goto out2;
	} else if (ret < 2) {
		/* this should always return 2 bytes */
		deverr(dev, "Read PHYID returned less than 2 bytes: ret=%02x",
				ret);
		ret = -EIO;
		goto out2;
	}

	/* Initialize MII structure */
	dev->mii.dev = dev->net;
	dev->mii.mdio_read = ax8817x_mdio_read_le;
	dev->mii.mdio_write = ax8817x_mdio_write_le;
	dev->mii.phy_id_mask = 0x3f;
	dev->mii.reg_num_mask = 0x1f;
	dev->mii.phy_id = *((u8 *)buf + 1);

#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
	dev->net->do_ioctl = ax8817x_ioctl;
	dev->net->set_multicast_list = ax8817x_set_multicast;
	dev->net->set_mac_address = ax8817x_set_mac_addr;
#else
	dev->net->netdev_ops = &ax88x72_netdev_ops;
#endif

	dev->net->ethtool_ops = &ax8817x_ethtool_ops;

	/* Register suspend and resume functions */
	data->suspend = axusbnet_suspend;
	data->resume = axusbnet_resume;

	ax8817x_mdio_write_le(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
	ax8817x_mdio_write_le(dev->net, dev->mii.phy_id, MII_ADVERTISE,
		ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
	mii_nway_restart(&dev->mii);

	printk (version);

	return 0;
out2:
	kfree(buf);
out1:
	return ret;
}

static struct ethtool_ops ax88772_ethtool_ops = {
	.get_drvinfo		= ax8817x_get_drvinfo,
	.get_link		= ethtool_op_get_link,
	.get_msglevel		= axusbnet_get_msglevel,
	.set_msglevel		= axusbnet_set_msglevel,
	.get_wol		= ax8817x_get_wol,
	.set_wol		= ax8817x_set_wol,
	.get_eeprom_len		= ax8817x_get_eeprom_len,
	.get_eeprom		= ax8817x_get_eeprom,
	.get_settings		= ax8817x_get_settings,
	.set_settings		= ax8817x_set_settings,
};

static int ax88772_bind(struct usbnet *dev, struct usb_interface *intf)
{
	int ret;
	void *buf;
	struct ax8817x_data *data = (struct ax8817x_data *)&dev->data;
	struct ax88772_data *ax772_data = NULL;

	axusbnet_get_endpoints(dev,intf);

	buf = kmalloc(6, GFP_KERNEL);
	if(!buf) {
		deverr(dev, "Cannot allocate memory for buffer");
		ret = -ENOMEM;
		goto out1;
	}

        ax772_data = kmalloc (sizeof(*ax772_data), GFP_KERNEL);
        if (!ax772_data) {
                deverr(dev, "Cannot allocate memory for AX88772 data");
                kfree (buf);
                return -ENOMEM;
        }
        memset (ax772_data, 0, sizeof(*ax772_data));
        dev->priv = ax772_data;

        ax772_data->ax_work = create_singlethread_workqueue ("ax88772");
        if (!ax772_data->ax_work) {
                kfree (ax772_data);
                kfree (buf);
                return -ENOMEM;
        }

        ax772_data->dev = dev;
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
        INIT_WORK (&ax772_data->check_link, ax88772_link_reset, dev);
#else
        INIT_WORK (&ax772_data->check_link, ax88772_link_reset);
#endif

	/* reload eeprom data */
	if ((ret = ax8817x_write_cmd(dev, AX_CMD_WRITE_GPIOS,
				     0x00B0, 0, 0, NULL)) < 0)
		goto out2;

	msleep(5);

	/* Initialize MII structure */
	dev->mii.dev = dev->net;
	dev->mii.mdio_read = ax8817x_mdio_read_le;
	dev->mii.mdio_write = ax8817x_mdio_write_le;
	dev->mii.phy_id_mask = 0xff;
	dev->mii.reg_num_mask = 0xff;

	/* Get the PHY id */
	if ((ret = ax8817x_read_cmd(dev, AX_CMD_READ_PHY_ID,
			0, 0, 2, buf)) < 0) {
		deverr(dev, "Error reading PHY ID: %02x", ret);
		goto out2;
	} else if (ret < 2) {
		/* this should always return 2 bytes */
		deverr(dev, "Read PHYID returned less than 2 bytes: ret=%02x",
		    ret);
		ret = -EIO;
		goto out2;
	}
	dev->mii.phy_id = *((u8 *)buf + 1);

	if (dev->mii.phy_id == 0x10)
	{
		if ((ret = ax8817x_write_cmd(dev, AX_CMD_SW_PHY_SELECT,
					0x0001, 0, 0, NULL)) < 0) {
			deverr(dev, "Select PHY #1 failed: %d", ret);
			goto out2;
		}

		if ((ret = ax8817x_write_cmd (dev, AX_CMD_SW_RESET,
					AX_SWRESET_IPPD,
					0, 0, NULL)) < 0) {
			deverr(dev, "Failed to power down PHY: %d", ret);
			goto out2;
		}

		msleep(150);
		if ((ret = ax8817x_write_cmd(dev, AX_CMD_SW_RESET,
					AX_SWRESET_CLEAR,
					0, 0, NULL)) < 0) {
			deverr(dev, 
			      "Failed to perform software reset: %d", ret);
			goto out2;
		}

		msleep(150);
		if ((ret = ax8817x_write_cmd(dev, AX_CMD_SW_RESET,
		     			AX_SWRESET_IPRL | AX_SWRESET_PRL,
					0, 0, NULL)) < 0) {
			deverr(dev,
			      "Failed to set PHY reset control: %d", ret);
			goto out2;
		}
	}
	else
	{
		if ((ret = ax8817x_write_cmd(dev, AX_CMD_SW_PHY_SELECT,
					0x0000, 0, 0, NULL)) < 0) {
			deverr(dev, "Select PHY #1 failed: %d", ret);
			goto out2;
		}

		if ((ret = ax8817x_write_cmd(dev, AX_CMD_SW_RESET,
					AX_SWRESET_IPPD | AX_SWRESET_PRL,
					0, 0, NULL)) < 0) {
			deverr(dev,
			      "Failed to power down internal PHY: %d", ret);
			goto out2;
		}
	}

	msleep(150);
	if ((ret = ax8817x_write_cmd(dev, AX_CMD_WRITE_RX_CTL,
				0x0000, 0, 0, NULL)) < 0) {
		deverr(dev, "Failed to reset RX_CTL: %d", ret);
		goto out2;
	}

	/* Get the MAC address */
	memset(buf, 0, ETH_ALEN);
	if ((ret = ax8817x_read_cmd(dev, AX88772_CMD_READ_NODE_ID,
				0, 0, ETH_ALEN, buf)) < 0) {
		deverr(dev, "Failed to read MAC address: %d", ret);
		goto out2;
	}
	memcpy(dev->net->dev_addr, buf, ETH_ALEN);

	if ((ret = ax8817x_write_cmd(dev, AX_CMD_SET_SW_MII,
				0, 0, 0, NULL)) < 0) {
		deverr(dev, "Enabling software MII failed: %d", ret);
		goto out2;
	}

	if (dev->mii.phy_id == 0x10)
	{
		if ((ret = ax8817x_mdio_read_le(dev->net,
				dev->mii.phy_id, 2)) != 0x003b) {
			deverr(dev, "Read PHY register 2 must be 0x3b00: %d",
				ret);
			goto out2;
		}
		
		if ((ret = ax8817x_write_cmd(dev, AX_CMD_SW_RESET,
					AX_SWRESET_PRL,
					0, 0, NULL)) < 0) {
			deverr(dev, "Set external PHY reset pin level: %d", ret);
			goto out2;
		}
		msleep(150);
		if ((ret = ax8817x_write_cmd(dev, AX_CMD_SW_RESET,
			 		AX_SWRESET_IPRL | AX_SWRESET_PRL,
					0, 0, NULL)) < 0) {
			deverr(dev, 
				"Set Internal/External PHY reset control: %d",
				ret);
			goto out2;
		}
		msleep(150);
	}

#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
	dev->net->do_ioctl = ax8817x_ioctl;
	dev->net->set_multicast_list = ax8817x_set_multicast;
	dev->net->set_mac_address = ax8817x_set_mac_addr;
#else
	dev->net->netdev_ops = &ax88x72_netdev_ops;
#endif

	dev->net->ethtool_ops = &ax88772_ethtool_ops;

	/* Register suspend and resume functions */
	data->suspend = ax88772_suspend;
	data->resume = ax88772_resume;

	ax8817x_mdio_write_le(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
	ax8817x_mdio_write_le(dev->net, dev->mii.phy_id, MII_ADVERTISE,
			ADVERTISE_ALL | ADVERTISE_CSMA);

	mii_nway_restart(&dev->mii);
        ax772_data->autoneg_start = jiffies;
        ax772_data->Event = WAIT_AUTONEG_COMPLETE;

	if ((ret = ax8817x_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE,
				AX88772_MEDIUM_DEFAULT, 0, 0, NULL)) < 0) {
		deverr(dev, "Write medium mode register: %d", ret);
		goto out2;
	}

	if ((ret = ax8817x_write_cmd(dev, AX_CMD_WRITE_IPG0,
			AX88772_IPG0_DEFAULT | AX88772_IPG1_DEFAULT << 8,
			AX88772_IPG2_DEFAULT, 0, NULL)) < 0) {
		deverr(dev, "Write IPG,IPG1,IPG2 failed: %d", ret);
		goto out2;
	}
	if ((ret =
	     ax8817x_write_cmd(dev, AX_CMD_SET_HW_MII, 0, 0, 0, NULL)) < 0) {
		deverr(dev, "Failed to set hardware MII: %02x", ret);
		goto out2;
	}

	/* Set RX_CTL to default values with 2k buffer, and enable cactus */
	if ((ret =
	     ax8817x_write_cmd(dev, AX_CMD_WRITE_RX_CTL, 0x0088, 0, 0,
			       NULL)) < 0) {
		deverr(dev, "Reset RX_CTL failed: %d", ret);
		goto out2;
	}

	/* Asix framing packs multiple eth frames into a 2K usb bulk transfer */
	if (dev->driver_info->flags & FLAG_FRAMING_AX) {
		/* hard_mtu  is still the default - the device does not support
		   jumbo eth frames */
		dev->rx_urb_size = 2048;
	}

	kfree (buf);
	printk (version);
	return 0;

out2:
	destroy_workqueue (ax772_data->ax_work);
        kfree (ax772_data);
	kfree(buf);
out1:
	return ret;
}

static void ax88772_unbind(struct usbnet *dev, struct usb_interface *intf)
{
	struct ax88772_data *ax772_data = (struct ax88772_data *)dev->priv;

	if (ax772_data) {

		flush_workqueue (ax772_data->ax_work);
		destroy_workqueue (ax772_data->ax_work);

		/* stop MAC operation */
		ax8817x_write_cmd(dev, AX_CMD_WRITE_RX_CTL,
					AX_RX_CTL_STOP, 0, 0, NULL);

		/* Power down PHY */
		ax8817x_write_cmd(dev, AX_CMD_SW_RESET,
					AX_SWRESET_IPPD, 0, 0, NULL);

		kfree (ax772_data);
	}
}

static int ax88772a_phy_powerup (struct usbnet *dev)
{
	int ret;
	/* set the embedded Ethernet PHY in power-down state */
	if ((ret = ax8817x_write_cmd(dev, AX_CMD_SW_RESET,
			AX_SWRESET_IPPD | AX_SWRESET_IPRL, 0, 0, NULL)) < 0) {
		deverr(dev, "Failed to power down PHY: %d", ret);
		return ret;
	}

	msleep(10);

	
	/* set the embedded Ethernet PHY in power-up state */
	if ((ret = ax8817x_write_cmd(dev, AX_CMD_SW_RESET,
			AX_SWRESET_IPRL, 0, 0, NULL)) < 0) {
		deverr(dev, "Failed to reset PHY: %d", ret);
		return ret;
	}

	msleep(600);

	/* set the embedded Ethernet PHY in reset state */
	if ((ret = ax8817x_write_cmd(dev, AX_CMD_SW_RESET,
			AX_SWRESET_CLEAR, 0, 0, NULL)) < 0) {
		deverr(dev, "Failed to power up PHY: %d", ret);
		return ret;
	}

	/* set the embedded Ethernet PHY in power-up state */
	if ((ret = ax8817x_write_cmd(dev, AX_CMD_SW_RESET,
			AX_SWRESET_IPRL, 0, 0, NULL)) < 0) {
		deverr(dev, "Failed to reset PHY: %d", ret);
		return ret;
	}

	return 0;
}

static int ax88772a_bind(struct usbnet *dev, struct usb_interface *intf)
{
	int ret = -EIO;
	void *buf;
	struct ax8817x_data *data = (struct ax8817x_data *)&dev->data;
	struct ax88772a_data *ax772a_data = NULL;

	axusbnet_get_endpoints(dev,intf);

	buf = kmalloc(6, GFP_KERNEL);
	if(!buf) {
		deverr(dev, "Cannot allocate memory for buffer");
		ret = -ENOMEM;
		goto out1;
	}

	ax772a_data = kmalloc (sizeof(*ax772a_data), GFP_KERNEL);
	if (!ax772a_data) {
		deverr(dev, "Cannot allocate memory for AX88772A data");
		kfree (buf);
		return -ENOMEM;
	}
	memset (ax772a_data, 0, sizeof(*ax772a_data));
	dev->priv = ax772a_data;

	ax772a_data->ax_work = create_singlethread_workqueue ("ax88772a");
	if (!ax772a_data->ax_work) {
		kfree (ax772a_data);
		kfree (buf);
		return -ENOMEM;
	}

	ax772a_data->dev = dev;
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
	INIT_WORK (&ax772a_data->check_link, ax88772a_link_reset, dev);
#else
	INIT_WORK (&ax772a_data->check_link, ax88772a_link_reset);
#endif

	/* Get the EEPROM data*/
	if ((ret = ax8817x_read_cmd(dev, AX_CMD_READ_EEPROM, 
			0x0017, 0, 2, (void *)&ax772a_data->EepromData)) < 0) {
		deverr(dev, "read SROM address 17h failed: %d", ret);
		goto out2;
	}
	le16_to_cpus (&ax772a_data->EepromData);
	/* End of get EEPROM data */

	/* reload eeprom data */
	if ((ret = ax8817x_write_cmd(dev, AX_CMD_WRITE_GPIOS,
			AXGPIOS_RSE, 0, 0, NULL)) < 0)
		goto out2;

	msleep(5);

	/* Initialize MII structure */
	dev->mii.dev = dev->net;
	dev->mii.mdio_read = ax8817x_mdio_read_le;
	dev->mii.mdio_write = ax8817x_mdio_write_le;
	dev->mii.phy_id_mask = 0xff;
	dev->mii.reg_num_mask = 0xff;

	/* Get the PHY id */
	if ((ret = ax8817x_read_cmd(dev, AX_CMD_READ_PHY_ID,
			0, 0, 2, buf)) < 0) {
		deverr(dev, "Error reading PHY ID: %02x", ret);
		goto out2;
	} else if (ret < 2) {
		/* this should always return 2 bytes */
		deverr(dev, "Read PHYID returned less than 2 bytes: ret=%02x",
			ret);
		goto out2;
	}
	dev->mii.phy_id = *((u8 *)buf + 1);

	if(dev->mii.phy_id != 0x10) {
		deverr(dev, "Got wrong PHY ID: %02x", dev->mii.phy_id);
		goto out2;
	}

	/* select the embedded 10/100 Ethernet PHY */
	if ((ret = ax8817x_write_cmd(dev, AX_CMD_SW_PHY_SELECT,
			AX_PHYSEL_SSEN | AX_PHYSEL_PSEL | AX_PHYSEL_SSMII,
			0, 0, NULL)) < 0) {
		deverr(dev, "Select PHY #1 failed: %d", ret);
		goto out2;
	}

	if ((ret = ax88772a_phy_powerup (dev)) < 0)
		goto out2;

	/* stop MAC operation */
	if ((ret = ax8817x_write_cmd(dev, AX_CMD_WRITE_RX_CTL,
			AX_RX_CTL_STOP, 0, 0, NULL)) < 0) {
		deverr(dev, "Reset RX_CTL failed: %d", ret);
		goto out2;
	}

	/* Get the MAC address */
	memset(buf, 0, ETH_ALEN);
	if ((ret = ax8817x_read_cmd(dev, AX88772_CMD_READ_NODE_ID,
				0, 0, ETH_ALEN, buf)) < 0) {
		deverr(dev, "Failed to read MAC address: %d", ret);
		goto out2;
	}
	memcpy(dev->net->dev_addr, buf, ETH_ALEN);

	/* make sure the driver can enable sw mii operation */
	if ((ret = ax8817x_write_cmd(dev, AX_CMD_SET_SW_MII,
			0, 0, 0, NULL)) < 0) {
		deverr(dev, "Enabling software MII failed: %d", ret);
		goto out2;
	}

#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
	dev->net->do_ioctl = ax8817x_ioctl;
	dev->net->set_multicast_list = ax8817x_set_multicast;
	dev->net->set_mac_address = ax8817x_set_mac_addr;
#else
	dev->net->netdev_ops = &ax88x72_netdev_ops;
#endif

	dev->net->ethtool_ops = &ax88772_ethtool_ops;

	/* Register suspend and resume functions */
	data->suspend = ax88772_suspend;
	data->resume = ax88772_resume;

	ax8817x_mdio_write_le(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
	ax8817x_mdio_write_le(dev->net, dev->mii.phy_id, MII_ADVERTISE,
			ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);

	mii_nway_restart(&dev->mii);
	ax772a_data->autoneg_start = jiffies;
	ax772a_data->Event = WAIT_AUTONEG_COMPLETE;

	if ((ret = ax8817x_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE,
				AX88772_MEDIUM_DEFAULT, 0, 0, NULL)) < 0) {
		deverr(dev, "Write medium mode register: %d", ret);
		goto out2;
	}

	if ((ret = ax8817x_write_cmd(dev, AX_CMD_WRITE_IPG0,
			AX88772A_IPG0_DEFAULT | AX88772A_IPG1_DEFAULT << 8,
			AX88772A_IPG2_DEFAULT, 0, NULL)) < 0) {
		deverr(dev, "Write IPG,IPG1,IPG2 failed: %d", ret);
		goto out2;
	}

	/* Set RX_CTL to default values with 2k buffer, and enable cactus */
	if ((ret = ax8817x_write_cmd(dev, AX_CMD_WRITE_RX_CTL,
			(AX_RX_CTL_START | AX_RX_CTL_AB),
			0, 0, NULL)) < 0) {
		deverr(dev, "Reset RX_CTL failed: %d", ret);
		goto out2;
	}

	/* Asix framing packs multiple eth frames into a 2K usb bulk transfer */
	if (dev->driver_info->flags & FLAG_FRAMING_AX) {
		/* hard_mtu  is still the default - the device does not support
		   jumbo eth frames */
		dev->rx_urb_size = 2048;
	}

	kfree (buf);

	printk (version);

	return ret;
out2:
	destroy_workqueue (ax772a_data->ax_work);
	kfree (ax772a_data);
	kfree (buf);
out1:
	return ret;
}

static void ax88772a_unbind(struct usbnet *dev, struct usb_interface *intf)
{
	struct ax88772a_data *ax772a_data = (struct ax88772a_data *)dev->priv;

	if (ax772a_data) {

		flush_workqueue (ax772a_data->ax_work);
		destroy_workqueue (ax772a_data->ax_work);

		/* stop MAC operation */
		ax8817x_write_cmd(dev, AX_CMD_WRITE_RX_CTL,
					AX_RX_CTL_STOP, 0, 0, NULL);

		/* Power down PHY */
		ax8817x_write_cmd(dev, AX_CMD_SW_RESET,
					AX_SWRESET_IPPD, 0, 0, NULL);

		kfree (ax772a_data);
	}
}

static int ax88772b_set_csums(struct usbnet *dev)
{
	struct ax88772b_data *ax772b_data = (struct ax88772b_data *)dev->priv;
	u16 checksum;

	if (ax772b_data->checksum & AX_RX_CHECKSUM)
		checksum = AX_RXCOE_DEF_CSUM;
	else
		checksum = 0;

	ax8817x_write_cmd (dev, AX_CMD_WRITE_RXCOE_CTL,
				 checksum, 0, 0, NULL);

	if (ax772b_data->checksum & AX_TX_CHECKSUM)
		checksum = AX_TXCOE_DEF_CSUM;
	else
		checksum = 0;

	ax8817x_write_cmd (dev, AX_CMD_WRITE_TXCOE_CTL,
				 checksum, 0, 0, NULL);

	return 0;
}

static u32 ax88772b_get_tx_csum(struct net_device *netdev)
{
	struct usbnet *dev = netdev_priv(netdev);
	struct ax88772b_data *ax772b_data = (struct ax88772b_data *)dev->priv;

	return (ax772b_data->checksum & AX_TX_CHECKSUM);
}

static u32 ax88772b_get_rx_csum(struct net_device *netdev)
{
	struct usbnet *dev = netdev_priv(netdev);
	struct ax88772b_data *ax772b_data = (struct ax88772b_data *)dev->priv;

	return (ax772b_data->checksum & AX_RX_CHECKSUM);
}

static int ax88772b_set_rx_csum(struct net_device *netdev, u32 val)
{
	struct usbnet *dev = netdev_priv(netdev);
	struct ax88772b_data *ax772b_data = (struct ax88772b_data *)dev->priv;

	if (val)
		ax772b_data->checksum |= AX_RX_CHECKSUM;
	else
		ax772b_data->checksum &= ~AX_RX_CHECKSUM;

	return ax88772b_set_csums(dev);
}

static int ax88772b_set_tx_csum(struct net_device *netdev, u32 val)
{
	struct usbnet *dev = netdev_priv(netdev);
	struct ax88772b_data *ax772b_data = (struct ax88772b_data *)dev->priv;

	if (val)
		ax772b_data->checksum |= AX_TX_CHECKSUM;
	else
		ax772b_data->checksum &= ~AX_TX_CHECKSUM;

	ethtool_op_set_tx_csum(netdev, val);

	return ax88772b_set_csums(dev);
}

static struct ethtool_ops ax88772b_ethtool_ops = {
	.get_drvinfo		= ax8817x_get_drvinfo,
	.get_link		= ethtool_op_get_link,
	.get_msglevel		= axusbnet_get_msglevel,
	.set_msglevel		= axusbnet_set_msglevel,
	.get_wol		= ax8817x_get_wol,
	.set_wol		= ax8817x_set_wol,
	.get_eeprom_len		= ax8817x_get_eeprom_len,
	.get_eeprom		= ax8817x_get_eeprom,
	.get_settings		= ax8817x_get_settings,
	.set_settings		= ax8817x_set_settings,
	.set_tx_csum		= ax88772b_set_tx_csum,
	.get_tx_csum		= ax88772b_get_tx_csum,
	.get_rx_csum		= ax88772b_get_rx_csum,
	.set_rx_csum		= ax88772b_set_rx_csum,
};

#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,29)
static const struct net_device_ops ax88772b_netdev_ops = {
	.ndo_open			= axusbnet_open,
	.ndo_stop			= axusbnet_stop,
	.ndo_start_xmit	= axusbnet_start_xmit,
	.ndo_tx_timeout	= axusbnet_tx_timeout,
	.ndo_change_mtu	= axusbnet_change_mtu,
	.ndo_do_ioctl		= ax8817x_ioctl,
	.ndo_get_stats		= axusbnet_get_stats,
	.ndo_set_mac_address 	= ax8817x_set_mac_addr,
	.ndo_validate_addr		= eth_validate_addr,
	.ndo_set_multicast_list = ax88772b_set_multicast,
};
#endif

static int ax88772b_bind(struct usbnet *dev, struct usb_interface *intf)
{
	int ret;
	void *buf;
	struct ax8817x_data *data = (struct ax8817x_data *)&dev->data;
	struct ax88772b_data *ax772b_data;
	u16 *tmp16;
	u8 i;
	u8 TempPhySelect;
	bool InternalPhy;

	axusbnet_get_endpoints(dev,intf);

	buf = kmalloc (6, GFP_KERNEL);
	if (!buf) {
		deverr(dev, "Cannot allocate memory for buffer");
		return -ENOMEM;
	}
	tmp16 = (u16 *)buf;

	ax772b_data = kmalloc (sizeof(*ax772b_data), GFP_KERNEL);
	if (!ax772b_data) {
		deverr(dev, "Cannot allocate memory for AX88772B data");
		kfree (buf);
		return -ENOMEM;
	}
	memset (ax772b_data, 0, sizeof(*ax772b_data));
	dev->priv = ax772b_data;

	ax772b_data->ax_work = create_singlethread_workqueue ("ax88772b");
	if (!ax772b_data->ax_work) {
		kfree (buf);
		kfree (ax772b_data);
		return -ENOMEM;
	}

	ax772b_data->dev = dev;
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
	INIT_WORK (&ax772b_data->check_link, ax88772b_link_reset, dev);
#else
	INIT_WORK (&ax772b_data->check_link, ax88772b_link_reset);
#endif

	if ((ret = ax8817x_read_cmd (dev, AX_CMD_SW_PHY_STATUS, 0, 0, 1,
        					&TempPhySelect)) < 0) {
		deverr(dev, "read SW interface selection status register"
							"failed: %d\n", ret);
		goto err_out;
	}

	TempPhySelect &= 0x0C;

	if (TempPhySelect == AX_PHYSEL_SSRMII) {
		InternalPhy = false;
		ax772b_data->OperationMode = OPERATION_MAC_MODE;
		ax772b_data->PhySelect = 0x00;
	}
	else if (TempPhySelect == AX_PHYSEL_SSRRMII) {
		InternalPhy = true;
		ax772b_data->OperationMode = OPERATION_PHY_MODE;
		ax772b_data->PhySelect = 0x00;
	}
	else if (TempPhySelect == AX_PHYSEL_SSMII) {
		InternalPhy = true;
		ax772b_data->OperationMode = OPERATION_MAC_MODE;
		ax772b_data->PhySelect = 0x01;
	}
	else {
		deverr(dev, "Unknown MII type\n");
		goto err_out;
	}

	/* reload eeprom data */
	if ((ret = ax8817x_write_cmd(dev, AX_CMD_WRITE_GPIOS,
			AXGPIOS_RSE, 0, 0, NULL)) < 0) {
		deverr(dev, "Failed to enable GPIO finction: %d", ret);
		goto err_out;
	}
	msleep(5);

	/* Get the EEPROM data*/
	if ((ret = ax8817x_read_cmd (dev, AX_CMD_READ_EEPROM,
				     0x18, 0, 2, (void *)tmp16)) < 0) {
		deverr(dev, "read SROM address 18h failed: %d", ret);
		goto err_out;
	}
	le16_to_cpus(tmp16);
	ax772b_data->psc = *tmp16 & 0xFF00;
	/* End of get EEPROM data */

	if (g_usr_mac) {
		/* Get user set MAC address */
		memcpy(buf, g_mac_addr, ETH_ALEN);
	} else {
		/* Get the MAC address from EEPROM */
		memset(buf, 0, ETH_ALEN);
		for (i = 0; i < (ETH_ALEN >> 1); i++) {
			if ((ret = ax8817x_read_cmd (dev, AX_CMD_READ_EEPROM,
						0x04 + i, 0, 2, (buf + i * 2))) < 0) {
				deverr(dev, "read SROM address 04h failed: %d", ret);
				goto err_out;
			}
		}
	}
	memcpy(dev->net->dev_addr, buf, ETH_ALEN);

	/* Set the MAC address */
	if ((ret = ax8817x_write_cmd (dev, AX88772_CMD_WRITE_NODE_ID,
			0, 0, ETH_ALEN, buf)) < 0) {
		deverr(dev, "set MAC address failed: %d", ret);
		goto err_out;
	}

	/* Initialize MII structure */
	dev->mii.dev = dev->net;
	dev->mii.mdio_read = ax8817x_mdio_read_le;
	dev->mii.mdio_write = ax88772b_mdio_write_le;
	dev->mii.phy_id_mask = 0xff;
	dev->mii.reg_num_mask = 0xff;

	/* Get the PHY id */
	if ((ret = ax8817x_read_cmd(dev, AX_CMD_READ_PHY_ID,
			0, 0, 2, buf)) < 0) {
		deverr(dev, "Error reading PHY ID: %02x", ret);
		goto err_out;
	} else if (ret < 2) {
		/* this should always return 2 bytes */
		deverr(dev, "Read PHYID returned less than 2 bytes: ret=%02x",
		    ret);
		ret = -EIO;
		goto err_out;
	}

	if (InternalPhy)
		dev->mii.phy_id = *((u8 *)buf + 1);
	else
		dev->mii.phy_id = *((u8 *)buf);

	if ((ret = ax8817x_write_cmd(dev, AX_CMD_SW_PHY_SELECT, 
			ax772b_data->PhySelect, 0, 0, NULL)) < 0) {
		deverr(dev, "Select PHY #1 failed: %d", ret);
		goto err_out;
	}

#if 0
	/* select the embedded 10/100 Ethernet PHY */
	if ((ret = ax8817x_write_cmd(dev, AX_CMD_SW_PHY_SELECT,
			AX_PHYSEL_SSEN | AX_PHYSEL_PSEL | AX_PHYSEL_SSMII
						, 0, 0, NULL)) < 0) {
		deverr(dev, "Select PHY #1 failed: %d", ret);
		goto err_out;
	}

	if(dev->mii.phy_id != 0x10) {
		deverr(dev, "Got wrong PHY ID: %02x", dev->mii.phy_id);
		ret = -EIO;
		goto err_out;
	}
#endif
	if ((ret = ax88772a_phy_powerup (dev)) < 0)
		goto err_out;

	/* stop MAC operation */
	if ((ret = ax8817x_write_cmd(dev, AX_CMD_WRITE_RX_CTL,
			AX_RX_CTL_STOP, 0, 0, NULL)) < 0) {
		deverr(dev, "Reset RX_CTL failed: %d", ret);
		goto err_out;
	}

	/* make sure the driver can enable sw mii operation */
	if ((ret = ax8817x_write_cmd(dev, AX_CMD_SET_SW_MII,
			0, 0, 0, NULL)) < 0) {
		deverr(dev, "Enabling software MII failed: %d", ret);
		goto err_out;
	}

#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
	dev->net->do_ioctl = ax8817x_ioctl;
	dev->net->set_multicast_list = ax88772b_set_multicast;
	dev->net->set_mac_address = ax8817x_set_mac_addr;
#else
	dev->net->netdev_ops = &ax88772b_netdev_ops;
#endif

	dev->net->ethtool_ops = &ax88772b_ethtool_ops;

	/* Register suspend and resume functions */
	data->suspend = ax88772b_suspend;
	data->resume = ax88772b_resume;

	if (ax772b_data->OperationMode == OPERATION_PHY_MODE)
		ax8817x_mdio_write_le(dev->net, dev->mii.phy_id
						, MII_BMCR, 0x3900);

	if (dev->mii.phy_id != 0x10)
		ax8817x_mdio_write_le(dev->net, 0x10, MII_BMCR, 0x3900);
		

	if (dev->mii.phy_id == 0x10 && ax772b_data->OperationMode 
						!= OPERATION_PHY_MODE) {

		*tmp16 = ax8817x_mdio_read_le(dev->net, dev->mii.phy_id, 0x12);
		ax8817x_mdio_write_le(dev->net, dev->mii.phy_id, 0x12,
					((*tmp16 & 0xFF9F) | 0x0040));
	}
	
	ax8817x_mdio_write_le(dev->net, dev->mii.phy_id, MII_ADVERTISE,
			ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);

	mii_nway_restart(&dev->mii);

	if ((ret = ax8817x_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE,
				AX88772_MEDIUM_DEFAULT, 0, 0, NULL)) < 0) {
		deverr(dev, "Failed to write medium mode: %d", ret);
		goto err_out;
	}

	if ((ret = ax8817x_write_cmd(dev, AX_CMD_WRITE_IPG0,
			AX88772A_IPG0_DEFAULT | AX88772A_IPG1_DEFAULT << 8,
			AX88772A_IPG2_DEFAULT, 0, NULL)) < 0) {
		deverr(dev, "Failed to write interframe gap: %d", ret);
		goto err_out;
	}

	dev->net->features |= NETIF_F_IP_CSUM;
#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,22)
	dev->net->features |= NETIF_F_IPV6_CSUM;
#endif

	ax772b_data->checksum = AX_RX_CHECKSUM | AX_TX_CHECKSUM;
	if ((ret = ax88772b_set_csums(dev)) < 0) {
		deverr(dev, "Write RX_COE/TX_COE failed: %d", ret);
		goto err_out;
	}

	dev->rx_size = bsize & 0x07;
	if (dev->udev->speed == USB_SPEED_HIGH) {

		if ((ret = ax8817x_write_cmd (dev, 0x2A,
				AX88772B_BULKIN_SIZE[dev->rx_size].byte_cnt,
				AX88772B_BULKIN_SIZE[dev->rx_size].threshold,
				0, NULL)) < 0) {
			deverr(dev, "Reset RX_CTL failed: %d", ret);
			goto err_out;
		}

		dev->rx_urb_size = AX88772B_BULKIN_SIZE[dev->rx_size].size;
	} else {
		if ((ret = ax8817x_write_cmd (dev, 0x2A,
				0x8000, 0x8001, 0, NULL)) < 0) {
			deverr(dev, "Reset RX_CTL failed: %d", ret);
			goto err_out;
		}
		dev->rx_urb_size = 2048;
	}

	/* Configure RX header type */
	if ((ret = ax8817x_write_cmd (dev, AX_CMD_WRITE_RX_CTL,
		      (AX_RX_CTL_START | AX_RX_CTL_AB | AX_RX_HEADER_DEFAULT),
		      0, 0, NULL)) < 0) {
		deverr(dev, "Reset RX_CTL failed: %d", ret);
		goto err_out;
	}

	/* Overwrite power saving configuration from eeprom */
	if ((ret = ax8817x_write_cmd (dev, AX_CMD_SW_RESET,
	    AX_SWRESET_IPRL | (ax772b_data->psc & 0x7FFF), 0, 0, NULL)) < 0) {
		deverr(dev, "Failed to configure PHY power saving: %d", ret);
		goto err_out;
	}

	if (ax772b_data->OperationMode == OPERATION_PHY_MODE)
		netif_carrier_on(dev->net);

	kfree (buf);
	printk (version);

	return ret;
err_out:
	destroy_workqueue (ax772b_data->ax_work);
	kfree (buf);
	kfree (ax772b_data);
	return ret;
}

static void ax88772b_unbind(struct usbnet *dev, struct usb_interface *intf)
{
	struct ax88772b_data *ax772b_data = (struct ax88772b_data *)dev->priv;

	if (ax772b_data) {

		flush_workqueue (ax772b_data->ax_work);
		destroy_workqueue (ax772b_data->ax_work);

		/* stop MAC operation */
		ax8817x_write_cmd(dev, AX_CMD_WRITE_RX_CTL,
					AX_RX_CTL_STOP, 0, 0, NULL);

		/* Power down PHY */
		ax8817x_write_cmd(dev, AX_CMD_SW_RESET,
					AX_SWRESET_IPPD, 0, 0, NULL);

		kfree (ax772b_data);
	}
}

static int 
ax88178_media_check (struct usbnet *dev, struct ax88178_data *ax178dataptr)
{
	int fullduplex;
	u16 tempshort = 0;
	u16 media;
	u16 advertise, lpa, result, stat1000;

	advertise = ax8817x_mdio_read_le (dev->net,
				dev->mii.phy_id, MII_ADVERTISE);
	lpa = ax8817x_mdio_read_le (dev->net, dev->mii.phy_id, MII_LPA);
	result = advertise & lpa;

	stat1000 = ax8817x_mdio_read_le (dev->net,
				dev->mii.phy_id, MII_STAT1000);

	if ((ax178dataptr->PhyMode == PHY_MODE_MARVELL) &&
	    (ax178dataptr->LedMode == 1)) { 
		tempshort = ax8817x_mdio_read_le (dev->net,
				dev->mii.phy_id, MARVELL_MANUAL_LED) & 0xfc0f;
	}

	fullduplex=1;
	if (stat1000 & LPA_1000FULL) {
		media = MEDIUM_GIGA_MODE | MEDIUM_FULL_DUPLEX_MODE |
			MEDIUM_ENABLE_125MHZ | MEDIUM_ENABLE_RECEIVE;
		if ((ax178dataptr->PhyMode == PHY_MODE_MARVELL) &&
		    (ax178dataptr->LedMode == 1))
			tempshort|= 0x3e0;
	} else if (result & LPA_100FULL) {
		media = MEDIUM_FULL_DUPLEX_MODE | MEDIUM_ENABLE_RECEIVE |
			MEDIUM_MII_100M_MODE;
		if ((ax178dataptr->PhyMode == PHY_MODE_MARVELL) &&
		    (ax178dataptr->LedMode == 1))
			tempshort|= 0x3b0;
	} else if (result & LPA_100HALF) {
		fullduplex = 0;
		media = MEDIUM_ENABLE_RECEIVE | MEDIUM_MII_100M_MODE;
		if ((ax178dataptr->PhyMode == PHY_MODE_MARVELL) &&
		    (ax178dataptr->LedMode == 1))
			tempshort |= 0x3b0;
	} else if (result & LPA_10FULL) {
		media = MEDIUM_FULL_DUPLEX_MODE | MEDIUM_ENABLE_RECEIVE;
		if ((ax178dataptr->PhyMode == PHY_MODE_MARVELL) &&
		    (ax178dataptr->LedMode == 1))
			tempshort |= 0x2f0;
	} else {
		media = MEDIUM_ENABLE_RECEIVE;
		fullduplex=0;
		if ((ax178dataptr->PhyMode == PHY_MODE_MARVELL) &&
		    (ax178dataptr->LedMode == 1))
				tempshort |= 0x02f0;
	}

	if ((ax178dataptr->PhyMode == PHY_MODE_MARVELL) &&
	    (ax178dataptr->LedMode == 1)) {
		ax8817x_mdio_write_le (dev->net, 
			dev->mii.phy_id, MARVELL_MANUAL_LED, tempshort);
	}

	media |= 0x0004;
	if(ax178dataptr->UseRgmii)
		media |= 0x0008;
	if(fullduplex) {
		media |= 0x0020;  //ebable tx flow control as default;
		media |= 0x0010;  //ebable rx flow control as default;
	}

	return media;
}

static void Vitess_8601_Init (struct usbnet *dev, int State)
{
	u16 reg;

	switch (State) {
	case 0:	// tx, rx clock skew
		ax8817x_swmii_mdio_write_le (dev->net, dev->mii.phy_id, 31, 1);
		ax8817x_swmii_mdio_write_le (dev->net, dev->mii.phy_id, 28, 0);
		ax8817x_swmii_mdio_write_le (dev->net, dev->mii.phy_id, 31, 0);
		break;

	case 1:
		ax8817x_swmii_mdio_write_le (dev->net,
				dev->mii.phy_id, 31, 0x52B5);
		ax8817x_swmii_mdio_write_le (dev->net,
				dev->mii.phy_id, 18, 0x009E);
		ax8817x_swmii_mdio_write_le (dev->net,
				dev->mii.phy_id, 17, 0xDD39);
		ax8817x_swmii_mdio_write_le (dev->net,
				dev->mii.phy_id, 16, 0x87AA);

		ax8817x_swmii_mdio_write_le (dev->net,
				dev->mii.phy_id, 16, 0xA7B4);

		ax8817x_swmii_mdio_write_le (dev->net,
				dev->mii.phy_id, 18,
				ax8817x_swmii_mdio_read_le (dev->net,
						dev->mii.phy_id, 18));

		reg = (ax8817x_swmii_mdio_read_le (dev->net,
				dev->mii.phy_id, 17) & ~0x003f) | 0x003c;
		ax8817x_swmii_mdio_write_le (dev->net,
				dev->mii.phy_id, 17, reg);
		ax8817x_swmii_mdio_write_le (dev->net,
				dev->mii.phy_id, 16, 0x87B4);

		ax8817x_swmii_mdio_write_le (dev->net,
				dev->mii.phy_id, 16, 0xa794);

		ax8817x_swmii_mdio_write_le (dev->net,
				dev->mii.phy_id, 18,
				ax8817x_swmii_mdio_read_le (dev->net,
					dev->mii.phy_id, 18));

		reg = (ax8817x_swmii_mdio_read_le (dev->net,
				dev->mii.phy_id, 17) & ~0x003f) | 0x003e;
		ax8817x_swmii_mdio_write_le (dev->net,
				dev->mii.phy_id, 17, reg);
		ax8817x_swmii_mdio_write_le (dev->net,
				dev->mii.phy_id, 16, 0x8794);

		ax8817x_swmii_mdio_write_le (dev->net, 
				dev->mii.phy_id, 18, 0x00f7);
		ax8817x_swmii_mdio_write_le (dev->net, 
				dev->mii.phy_id, 17, 0xbe36);
		ax8817x_swmii_mdio_write_le (dev->net, 
				dev->mii.phy_id, 16, 0x879e);

		ax8817x_swmii_mdio_write_le (dev->net, 
				dev->mii.phy_id, 16, 0xa7a0);

		ax8817x_swmii_mdio_write_le (dev->net, 
				dev->mii.phy_id, 18, 
				ax8817x_swmii_mdio_read_le (dev->net, 
						dev->mii.phy_id, 18));

		reg = (ax8817x_swmii_mdio_read_le (dev->net, 
				dev->mii.phy_id, 17) & ~0x003f) | 0x0034;
		ax8817x_swmii_mdio_write_le (dev->net, 
				dev->mii.phy_id, 17, reg);
		ax8817x_swmii_mdio_write_le (dev->net, 
				dev->mii.phy_id, 16, 0x87a0);

		ax8817x_swmii_mdio_write_le (dev->net, 
				dev->mii.phy_id, 18, 0x003c);
		ax8817x_swmii_mdio_write_le (dev->net, 
				dev->mii.phy_id, 17, 0xf3cf);
		ax8817x_swmii_mdio_write_le (dev->net, 
				dev->mii.phy_id, 16, 0x87a2);

		ax8817x_swmii_mdio_write_le (dev->net, 
				dev->mii.phy_id, 18, 0x003c);
		ax8817x_swmii_mdio_write_le (dev->net, 
				dev->mii.phy_id, 17, 0xf3cf);
		ax8817x_swmii_mdio_write_le (dev->net, 
				dev->mii.phy_id, 16, 0x87a4);

		ax8817x_swmii_mdio_write_le (dev->net, 
				dev->mii.phy_id, 18, 0x003c);
		ax8817x_swmii_mdio_write_le (dev->net, 
				dev->mii.phy_id, 17, 0xd287);
		ax8817x_swmii_mdio_write_le (dev->net, 
				dev->mii.phy_id, 16, 0x87a6);

		ax8817x_swmii_mdio_write_le (dev->net, 
				dev->mii.phy_id, 16, 0xa7a8);

		ax8817x_swmii_mdio_write_le (dev->net, 
				dev->mii.phy_id, 18, 
				ax8817x_swmii_mdio_read_le (dev->net, 
						dev->mii.phy_id, 18));

		reg = (ax8817x_swmii_mdio_read_le (dev->net, 
				dev->mii.phy_id, 17) & ~0x0fff) | 0x0125;
		ax8817x_swmii_mdio_write_le (dev->net, 
				dev->mii.phy_id, 17, reg);
		ax8817x_swmii_mdio_write_le (dev->net, 
				dev->mii.phy_id, 16, 0x87a8);

		// Enable Smart Pre-emphasis
		ax8817x_swmii_mdio_write_le (dev->net, 
				dev->mii.phy_id, 16, 0xa7fa);

		ax8817x_swmii_mdio_write_le (dev->net, 
				dev->mii.phy_id, 18, 
				ax8817x_swmii_mdio_read_le (dev->net, 
						dev->mii.phy_id, 18));

		reg = (ax8817x_swmii_mdio_read_le (dev->net, 
				dev->mii.phy_id, 17) & ~0x0008) | 0x0008;

		ax8817x_swmii_mdio_write_le (dev->net, 
				dev->mii.phy_id, 17, reg);
		ax8817x_swmii_mdio_write_le (dev->net, 
				dev->mii.phy_id, 16, 0x87fa);

		ax8817x_swmii_mdio_write_le (dev->net, 
				dev->mii.phy_id, 31, 0);

		break;
	}
}

static int 
ax88178_phy_init (struct usbnet *dev, struct ax88178_data *ax178dataptr)
{
	int i;
	u16 PhyAnar, PhyAuxCtrl, PhyCtrl, TempShort, PhyID1;
	u16 PhyReg = 0;

	//Disable MII operation of AX88178 Hardware
	ax8817x_write_cmd (dev, AX_CMD_SET_SW_MII, 0x0000, 0, 0, NULL);


	//Read SROM - MiiPhy Address (ID)
	ax8817x_read_cmd (dev, AX_CMD_READ_PHY_ID, 0, 0, 2, &dev->mii.phy_id);
	le32_to_cpus (&dev->mii.phy_id);

	/* Initialize MII structure */
	dev->mii.phy_id >>= 8;
	dev->mii.phy_id &= PHY_ID_MASK;
	dev->mii.dev = dev->net;
	dev->mii.mdio_read = ax8817x_mdio_read_le;
	dev->mii.mdio_write = ax8817x_mdio_write_le;
	dev->mii.phy_id_mask = 0x3f;
	dev->mii.reg_num_mask = 0x1f;
#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,11)
	dev->mii.supports_gmii = 1;
#endif

	if (ax178dataptr->PhyMode == PHY_MODE_MAC_TO_MAC_GMII)
	{
		ax178dataptr->UseRgmii = 0;
		ax178dataptr->MediaLink = MEDIUM_GIGA_MODE |
					  MEDIUM_FULL_DUPLEX_MODE |
					  MEDIUM_ENABLE_125MHZ |
					  MEDIUM_ENABLE_RECEIVE |
					  MEDIUM_ENABLE_RX_FLOWCTRL |
					  MEDIUM_ENABLE_TX_FLOWCTRL;

		goto SkipPhySetting;
	}

	// test read phy register 2
	if (!ax178dataptr->UseGpio0) {
		i = 1000;
		while (i--) {
			PhyID1 = ax8817x_swmii_mdio_read_le (dev->net, 
						dev->mii.phy_id, GMII_PHY_OUI);
			if ((PhyID1 == 0x000f) || (PhyID1 == 0x0141) ||
			    (PhyID1 == 0x0282) || (PhyID1 == 0x004d) ||
			    (PhyID1 == 0x0243) || (PhyID1 == 0x001C) ||
			    (PhyID1 == 0x0007))
				break;
			msleep(5);
		}
		if (i < 0)
			return -EIO;
	}

	ax178dataptr->UseRgmii = 0;
	if (ax178dataptr->PhyMode == PHY_MODE_MARVELL) {
		PhyReg = ax8817x_swmii_mdio_read_le(dev->net, 
					dev->mii.phy_id, 27);
		if (!(PhyReg & 4)) {
			ax178dataptr->UseRgmii = 1;
			ax8817x_swmii_mdio_write_le (dev->net, 
					dev->mii.phy_id, 20, 0x82);
			ax178dataptr->MediaLink |= MEDIUM_ENABLE_125MHZ;
		}
	} else if ((ax178dataptr->PhyMode == PHY_MODE_AGERE_V0) ||
		 (ax178dataptr->PhyMode == PHY_MODE_AGERE_V0_GMII)) {
		if (ax178dataptr->PhyMode == PHY_MODE_AGERE_V0) {
			ax178dataptr->UseRgmii = 1;
			ax178dataptr->MediaLink |= MEDIUM_ENABLE_125MHZ;
		}
	} else if (ax178dataptr->PhyMode == PHY_MODE_CICADA_V1) {
		// not Cameo
		if (!ax178dataptr->UseGpio0 || ax178dataptr->LedMode) {
			ax178dataptr->UseRgmii = 1;
			ax178dataptr->MediaLink |= MEDIUM_ENABLE_125MHZ;
		}

		for (i = 0; i < (sizeof(CICADA_FAMILY_HWINIT) / 
			 	 sizeof(CICADA_FAMILY_HWINIT[0])); i++) {
			ax8817x_swmii_mdio_write_le (dev->net, 
					dev->mii.phy_id,
					CICADA_FAMILY_HWINIT[i].offset,
					CICADA_FAMILY_HWINIT[i].value);
		}

	}
	else if (ax178dataptr->PhyMode == PHY_MODE_CICADA_V2)
	{
		// not Cameo
		if (!ax178dataptr->UseGpio0 || ax178dataptr->LedMode)
		{
			ax178dataptr->UseRgmii = 1;
			ax178dataptr->MediaLink |= MEDIUM_ENABLE_125MHZ;
		}

		for (i = 0; i < (sizeof(CICADA_V2_HWINIT) / 
				 sizeof(CICADA_V2_HWINIT[0])); i++) {
			ax8817x_swmii_mdio_write_le (dev->net, 
				dev->mii.phy_id, CICADA_V2_HWINIT[i].offset,
				CICADA_V2_HWINIT[i].value);
		}
	} else if (ax178dataptr->PhyMode == PHY_MODE_CICADA_V2_ASIX) {
		// not Cameo
		if (!ax178dataptr->UseGpio0 || ax178dataptr->LedMode)
		{
			ax178dataptr->UseRgmii = 1;
			ax178dataptr->MediaLink |= MEDIUM_ENABLE_125MHZ;
		}

		for (i = 0; i < (sizeof(CICADA_V2_HWINIT) / 
				 sizeof(CICADA_V2_HWINIT[0])); i++) {
			ax8817x_swmii_mdio_write_le (dev->net, 
				dev->mii.phy_id, CICADA_V2_HWINIT[i].offset, 
				CICADA_V2_HWINIT[i].value);
		}
	} else if (ax178dataptr->PhyMode == PHY_MODE_RTL8211CL) {
		ax178dataptr->UseRgmii = 1;
		ax178dataptr->MediaLink |= MEDIUM_ENABLE_125MHZ;
	} else if (ax178dataptr->PhyMode == PHY_MODE_RTL8211BN) {
		ax178dataptr->UseRgmii = 1;
		ax178dataptr->MediaLink |= MEDIUM_ENABLE_125MHZ;
	} else if (ax178dataptr->PhyMode == PHY_MODE_RTL8251CL) {
		ax178dataptr->UseRgmii = 1;
		ax178dataptr->MediaLink |= MEDIUM_ENABLE_125MHZ;
	} else if (ax178dataptr->PhyMode == PHY_MODE_VSC8601) {
		ax178dataptr->UseRgmii = 1;
		ax178dataptr->MediaLink |= MEDIUM_ENABLE_125MHZ;
//		Vitess_8601_Init (dev, 0);
	}

	if (ax178dataptr->PhyMode != PHY_MODE_ATTANSIC_V0) {
		// software reset
		ax8817x_swmii_mdio_write_le (
			dev->net, dev->mii.phy_id, GMII_PHY_CONTROL,
			ax8817x_swmii_mdio_read_le (
				dev->net, dev->mii.phy_id, GMII_PHY_CONTROL)
				| GMII_CONTROL_RESET);
		msleep (1);
	}

	if ((ax178dataptr->PhyMode == PHY_MODE_AGERE_V0) ||
	    (ax178dataptr->PhyMode == PHY_MODE_AGERE_V0_GMII)) {
		if (ax178dataptr->PhyMode == PHY_MODE_AGERE_V0)
		{
			i = 1000;
			while (i--)
			{
				ax8817x_swmii_mdio_write_le (dev->net, 
						dev->mii.phy_id, 21, 0x1001);

				PhyReg = ax8817x_swmii_mdio_read_le (dev->net, 
						dev->mii.phy_id, 21);
				if ((PhyReg & 0xf00f) == 0x1001)
					break;
			}
			if (i < 0)
				return -EIO;
		}

		if (ax178dataptr->LedMode == 4) {
			ax8817x_swmii_mdio_write_le (dev->net, 
					dev->mii.phy_id, 28, 0x7417);
		} else if (ax178dataptr->LedMode == 9) {
			ax8817x_swmii_mdio_write_le (dev->net, 
					dev->mii.phy_id, 28, 0x7a10);
		} else if (ax178dataptr->LedMode == 10) {
			ax8817x_swmii_mdio_write_le (dev->net, 
					dev->mii.phy_id, 28, 0x7a13);
		}

		for (i = 0; i < (sizeof(AGERE_FAMILY_HWINIT) /
				 sizeof(AGERE_FAMILY_HWINIT[0])); i++) {
			ax8817x_swmii_mdio_write_le (dev->net, 
				dev->mii.phy_id, AGERE_FAMILY_HWINIT[i].offset,
				AGERE_FAMILY_HWINIT[i].value);
		}
	} else if (ax178dataptr->PhyMode == PHY_MODE_RTL8211CL) {

		ax8817x_swmii_mdio_write_le (dev->net, 
				dev->mii.phy_id, 0x1f, 0x0005);
		ax8817x_swmii_mdio_write_le (dev->net, 
				dev->mii.phy_id, 0x0c, 0);

		ax8817x_swmii_mdio_write_le (dev->net, 
				dev->mii.phy_id, 0x01, 
				(ax8817x_swmii_mdio_read_le (dev->net, 
					dev->mii.phy_id, 0x01) | 0x0080));
		ax8817x_swmii_mdio_write_le (dev->net, 
				dev->mii.phy_id, 0x1f, 0);

		if (ax178dataptr->LedMode == 12) {
			ax8817x_swmii_mdio_write_le (dev->net, 
					dev->mii.phy_id, 0x1f, 0x0002);
			ax8817x_swmii_mdio_write_le (dev->net, 
					dev->mii.phy_id, 0x1a, 0x00cb);
			ax8817x_swmii_mdio_write_le (dev->net, 
					dev->mii.phy_id, 0x1f, 0);
		}
	} else if (ax178dataptr->PhyMode == PHY_MODE_VSC8601) {
		Vitess_8601_Init (dev, 1);
	}

	// read phy register 0
	PhyCtrl = ax8817x_swmii_mdio_read_le (dev->net, 
				dev->mii.phy_id, GMII_PHY_CONTROL);
	TempShort = PhyCtrl;
	PhyCtrl &= ~(GMII_CONTROL_POWER_DOWN | GMII_CONTROL_ISOLATE);
	if (PhyCtrl != TempShort) {
		ax8817x_swmii_mdio_write_le (dev->net, 
				dev->mii.phy_id, GMII_PHY_CONTROL, PhyCtrl);
	}

	// led
	if (ax178dataptr->PhyMode == PHY_MODE_MARVELL) {
		if (ax178dataptr->LedMode == 1)	{

			PhyReg = (ax8817x_swmii_mdio_read_le (dev->net, 
				dev->mii.phy_id, 24) & 0xf8ff) | (1 + 0x100);
			ax8817x_swmii_mdio_write_le (dev->net, 
					dev->mii.phy_id, 24, PhyReg);
			PhyReg = ax8817x_swmii_mdio_read_le (dev->net, 
					dev->mii.phy_id, 25) & 0xfc0f;

		} else if (ax178dataptr->LedMode == 2) {

			PhyReg = (ax8817x_swmii_mdio_read_le (dev->net, 
					dev->mii.phy_id, 24) & 0xf886) | 
					(1 + 0x10 + 0x300);
			ax8817x_swmii_mdio_write_le (dev->net, 
					dev->mii.phy_id, 24, PhyReg);

		} else if (ax178dataptr->LedMode == 5) {

			PhyReg = (ax8817x_swmii_mdio_read_le (dev->net, 
					dev->mii.phy_id, 24) & 0xf8be) | 
					(1 + 0x40 + 0x300);
			ax8817x_swmii_mdio_write_le (dev->net, 
					dev->mii.phy_id, 24, PhyReg);

		} else if (ax178dataptr->LedMode == 7) {

			PhyReg = (ax8817x_swmii_mdio_read_le (dev->net, 
						dev->mii.phy_id, 24) & 0xf8ff) |
						(1 + 0x100);
			ax8817x_swmii_mdio_write_le (dev->net, 
					dev->mii.phy_id, 24, PhyReg);

		} else if (ax178dataptr->LedMode == 8) {

			PhyReg = (ax8817x_swmii_mdio_read_le (dev->net, 
					dev->mii.phy_id, 24) & 0xf8be) | 
					(1 + 0x40 + 0x100);
			ax8817x_swmii_mdio_write_le (dev->net, 
					dev->mii.phy_id, 24, PhyReg);

		} else if (ax178dataptr->LedMode == 11) {

			PhyReg = ax8817x_swmii_mdio_read_le (dev->net, 
					dev->mii.phy_id, 24) & 0x4106;
			ax8817x_swmii_mdio_write_le (dev->net, 
					dev->mii.phy_id, 24, PhyReg);

		}
	} else if ((ax178dataptr->PhyMode == PHY_MODE_CICADA_V1) ||
		   (ax178dataptr->PhyMode == PHY_MODE_CICADA_V2) ||
		   (ax178dataptr->PhyMode == PHY_MODE_CICADA_V2_ASIX)) {

		if (ax178dataptr->LedMode == 3) {

			PhyReg = (ax8817x_swmii_mdio_read_le (dev->net, 
					dev->mii.phy_id, 27) & 0xFCFF) | 0x0100;
			ax8817x_swmii_mdio_write_le (dev->net, 
					dev->mii.phy_id, 27, PhyReg);
		}

	}

	if (ax178dataptr->PhyMode == PHY_MODE_MARVELL)
	{
		if (ax178dataptr->LedMode == 1)
			PhyReg |= 0x3f0;
	}

	PhyAnar = 1 | (GMII_ANAR_PAUSE | GMII_ANAR_100TXFD | GMII_ANAR_100TX |
		       GMII_ANAR_10TFD | GMII_ANAR_10T | GMII_ANAR_ASYM_PAUSE);

	PhyAuxCtrl = GMII_1000_AUX_CTRL_FD_CAPABLE;

	ax8817x_swmii_mdio_write_le (dev->net, 
			dev->mii.phy_id, GMII_PHY_ANAR, PhyAnar);
	ax8817x_swmii_mdio_write_le (dev->net, 
			dev->mii.phy_id, GMII_PHY_1000BT_CONTROL, PhyAuxCtrl);

	if (ax178dataptr->PhyMode == PHY_MODE_VSC8601)
	{
		ax8817x_swmii_mdio_write_le (dev->net, 
				dev->mii.phy_id, 31, 0x52B5);
		ax8817x_swmii_mdio_write_le (dev->net, 
				dev->mii.phy_id, 16, 0xA7F8);

		TempShort = ax8817x_swmii_mdio_read_le (dev->net, 
					dev->mii.phy_id, 17) & (~0x0018);

		ax8817x_swmii_mdio_write_le (dev->net, 
					dev->mii.phy_id, 17, TempShort);

		TempShort = ax8817x_swmii_mdio_read_le (dev->net, 
					dev->mii.phy_id, 18);

		ax8817x_swmii_mdio_write_le (dev->net, 
				dev->mii.phy_id, 18, TempShort);

		ax8817x_swmii_mdio_write_le (dev->net, 
				dev->mii.phy_id, 16, 0x87F8);
		ax8817x_swmii_mdio_write_le (dev->net, 
				dev->mii.phy_id, 31, 0);
	}

	if (ax178dataptr->PhyMode == PHY_MODE_ATTANSIC_V0) {

		ax8817x_swmii_mdio_write_le (dev->net, 
				dev->mii.phy_id, GMII_PHY_CONTROL, 0x9000);

	} else {
		PhyCtrl &= ~GMII_CONTROL_LOOPBACK;
		PhyCtrl |= (GMII_CONTROL_ENABLE_AUTO | GMII_CONTROL_START_AUTO);

		ax8817x_swmii_mdio_write_le (dev->net, 
				dev->mii.phy_id, GMII_PHY_CONTROL, PhyCtrl);
	}

	if (ax178dataptr->PhyMode == PHY_MODE_MARVELL) {
		if (ax178dataptr->LedMode == 1)
			ax8817x_swmii_mdio_write_le (dev->net, 
					dev->mii.phy_id, 25, PhyReg);
	}

SkipPhySetting:

	ax8817x_write_cmd (dev, AX_CMD_WRITE_MEDIUM_MODE, 
			ax178dataptr->MediaLink, 0, 0, NULL);

	ax8817x_write_cmd (dev, AX_CMD_WRITE_IPG0, 
			AX88772_IPG0_DEFAULT | (AX88772_IPG1_DEFAULT << 8), 
			AX88772_IPG2_DEFAULT, 0, NULL);

	msleep (1);

	ax8817x_write_cmd (dev, AX_CMD_SET_HW_MII, 0, 0, 0, NULL);

	return 0;
}

static int ax88178_bind(struct usbnet *dev, struct usb_interface *intf)
{
	int ret;
	void *buf;
	struct ax8817x_data *data = (struct ax8817x_data *)&dev->data;
	struct ax88178_data *ax178dataptr = NULL;

	axusbnet_get_endpoints(dev,intf);

	buf = kmalloc(6, GFP_KERNEL);
	if(!buf) {
		deverr(dev, "Cannot allocate memory for buffer");
		return -ENOMEM;
	}

	/* allocate 178 data */
	ax178dataptr = kmalloc (sizeof (*ax178dataptr), GFP_KERNEL);
	if (!ax178dataptr) {
		deverr(dev, "Cannot allocate memory for AX88178 data");
		ret = -ENOMEM;
		goto error_out;
	}
	memset (ax178dataptr, 0, sizeof (struct ax88178_data));
	dev->priv = ax178dataptr;
	/* end of allocate 178 data */

	/* Get the EEPROM data*/
	if ((ret = ax8817x_read_cmd (dev, AX_CMD_READ_EEPROM, 0x0017, 0, 2,
				(void *)(&ax178dataptr->EepromData))) < 0) {
		deverr(dev, "read SROM address 17h failed: %d", ret);
		goto error_out;
	}
	le16_to_cpus (&ax178dataptr->EepromData);
	/* End of get EEPROM data */

	if (ax178dataptr->EepromData == 0xffff) {
		ax178dataptr->PhyMode  = PHY_MODE_MARVELL;
		ax178dataptr->LedMode  = 0;
		ax178dataptr->UseGpio0 = 1; //True
	} else {
		ax178dataptr->PhyMode = (u8)(ax178dataptr->EepromData & 
						EEPROMMASK);
		ax178dataptr->LedMode = (u8)(ax178dataptr->EepromData >> 8);
		if (ax178dataptr->LedMode == 6)	// for buffalo new (use gpio2)
			ax178dataptr->LedMode = 1;
		else if (ax178dataptr->LedMode == 1)
			ax178dataptr->BuffaloOld = 1;


		if(ax178dataptr->EepromData & 0x80) {
			ax178dataptr->UseGpio0=0; //MARVEL se and other
		} else {
			ax178dataptr->UseGpio0=1; //cameo
		}
	}

	if (ax178dataptr->UseGpio0) {

		if (ax178dataptr->PhyMode == PHY_MODE_MARVELL) {

			if ((ret = ax8817x_write_cmd (dev, AX_CMD_WRITE_GPIOS,
					AXGPIOS_GPO0EN | AXGPIOS_RSE,
					0, 0, NULL)) < 0) {
				deverr(dev, "write GPIO failed: %d", ret);
				goto error_out;
			}

			msleep (25);

			if ((ret = ax8817x_write_cmd (dev, AX_CMD_WRITE_GPIOS,
					(AXGPIOS_GPO2 | AXGPIOS_GPO2EN |
					 AXGPIOS_GPO0EN), 0, 0, NULL)) < 0) {
				deverr(dev, "write GPIO failed: %d", ret);
				goto error_out;
			}

			msleep (15);

			if ((ret = ax8817x_write_cmd (dev, AX_CMD_WRITE_GPIOS,
					AXGPIOS_GPO2EN | AXGPIOS_GPO0EN,
					0, 0, NULL)) < 0) {
				deverr(dev, "write GPIO failed: %d", ret);
				goto error_out;
			}

			msleep (245);

			if ((ret = ax8817x_write_cmd (dev, AX_CMD_WRITE_GPIOS,
					(AXGPIOS_GPO2 | AXGPIOS_GPO2EN |
					 AXGPIOS_GPO0EN), 0, 0, NULL)) < 0) {
				deverr(dev, "write GPIO failed: %d", ret);
				goto error_out;
			}

		} else { // vitesse

			if ((ret = ax8817x_write_cmd (dev, AX_CMD_WRITE_GPIOS,
					(AXGPIOS_RSE | AXGPIOS_GPO0EN |
					 AXGPIOS_GPO0), 0, 0, NULL)) < 0) {
				deverr(dev, "write GPIO failed: %d", ret);
				goto error_out;
			}

			msleep (25);

			if ((ret = ax8817x_write_cmd (dev, AX_CMD_WRITE_GPIOS,
					(AXGPIOS_GPO0EN | AXGPIOS_GPO0 |
					 AXGPIOS_GPO2EN | AXGPIOS_GPO2),
					0, 0, NULL)) < 0) {
				deverr(dev, "write GPIO failed: %d", ret);
				goto error_out;
			}

			msleep (25);

			if ((ret = ax8817x_write_cmd (dev, AX_CMD_WRITE_GPIOS,
					(AXGPIOS_GPO0EN | AXGPIOS_GPO0 |
					 AXGPIOS_GPO2EN), 0, 0, NULL)) < 0) {
				deverr(dev, "write GPIO failed: %d", ret);
				goto error_out;
			}

			msleep (245);

			if ((ret = ax8817x_write_cmd (dev, AX_CMD_WRITE_GPIOS,
					(AXGPIOS_GPO0EN | AXGPIOS_GPO0 |
					 AXGPIOS_GPO2EN | AXGPIOS_GPO2),
					0, 0, NULL)) < 0) {
				deverr(dev, "write GPIO failed: %d", ret);
				goto error_out;
			}
		}
	} else {	// use gpio1

		if ((ret = ax8817x_write_cmd (dev, AX_CMD_WRITE_GPIOS,
				(AXGPIOS_GPO1 |AXGPIOS_GPO1EN | AXGPIOS_RSE),
				0, 0, NULL)) < 0) {
			deverr(dev, "write GPIO failed: %d", ret);
			goto error_out;
		}

		if (ax178dataptr->BuffaloOld) {

			msleep (350);

			if ((ret = ax8817x_write_cmd (dev, AX_CMD_WRITE_GPIOS,
					AXGPIOS_GPO1EN, 0, 0, NULL)) < 0) {
				deverr(dev, "write GPIO failed: %d", ret);
				goto error_out;
			}

			msleep (350);

			if ((ret = ax8817x_write_cmd (dev, AX_CMD_WRITE_GPIOS,
					AXGPIOS_GPO1EN | AXGPIOS_GPO1,
					0, 0, NULL)) < 0) {
				deverr(dev, "write GPIO failed: %d", ret);
				goto error_out;
			}
		}
		else
		{
			msleep (25);

			if ((ret = ax8817x_write_cmd (dev, AX_CMD_WRITE_GPIOS,
					(AXGPIOS_GPO1EN | AXGPIOS_GPO1 |
					 AXGPIOS_GPO2EN | AXGPIOS_GPO2),
					0, 0, NULL)) < 0) {
				deverr(dev, "write GPIO failed: %d", ret);
				goto error_out;
			}

			msleep (25);

			if ((ret = ax8817x_write_cmd (dev, AX_CMD_WRITE_GPIOS,
					(AXGPIOS_GPO1EN | AXGPIOS_GPO1 |
					 AXGPIOS_GPO2EN), 0, 0, NULL)) < 0) {
				deverr(dev, "write GPIO failed: %d", ret);
				goto error_out;
			}

			msleep (245);

			if ((ret = ax8817x_write_cmd (dev, AX_CMD_WRITE_GPIOS,
					(AXGPIOS_GPO1EN | AXGPIOS_GPO1 |
					 AXGPIOS_GPO2EN | AXGPIOS_GPO2),
					0, 0, NULL)) < 0) {
				deverr(dev, "write GPIO failed: %d", ret);
				goto error_out;
			}
		}
	}

	if ((ret = ax8817x_write_cmd(dev,
			AX_CMD_SW_PHY_SELECT, 0, 0, 0, NULL)) < 0) {
		deverr(dev, "Select PHY failed: %d", ret);
		goto error_out;
	}

	if ((ret = ax8817x_write_cmd(dev, AX_CMD_SW_RESET,
			AX_SWRESET_IPPD | AX_SWRESET_PRL, 0, 0, NULL)) < 0) {
		deverr(dev, "Issue sw reset failed: %d", ret);
		goto error_out;
	}

	if ((ret = ax8817x_write_cmd(dev, AX_CMD_WRITE_RX_CTL,
			0, 0, 0, NULL)) < 0) {
		deverr(dev, "Issue rx ctrl failed: %d", ret);
		goto error_out;
	}

	/* Get the MAC address */
	memset(buf, 0, ETH_ALEN);
	if ((ret = ax8817x_read_cmd (dev, AX88772_CMD_READ_NODE_ID,
				     0, 0, ETH_ALEN, buf)) < 0) {
		deverr(dev, "read AX_CMD_READ_NODE_ID failed: %d", ret);
		goto error_out;
	}
	memcpy(dev->net->dev_addr, buf, ETH_ALEN);
	/* End of get MAC address */

	if ((ret = ax88178_phy_init (dev, ax178dataptr)) < 0)
		goto error_out;

#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
	dev->net->do_ioctl = ax8817x_ioctl;
	dev->net->set_multicast_list = ax8817x_set_multicast;
	dev->net->set_mac_address = ax8817x_set_mac_addr;
#else
	dev->net->netdev_ops = &ax88x72_netdev_ops;
#endif
	dev->net->ethtool_ops = &ax8817x_ethtool_ops;

	/* Register suspend and resume functions */
	data->suspend = ax88772_suspend;
	data->resume = ax88772_resume;

	if (dev->driver_info->flags & FLAG_FRAMING_AX) {
		dev->rx_urb_size = 16384;
	}

	if ((ret = ax8817x_write_cmd(dev, AX_CMD_WRITE_RX_CTL,
			(AX_RX_CTL_MFB | AX_RX_CTL_START | AX_RX_CTL_AB),
			0, 0, NULL)) < 0) {
		deverr(dev, "write RX ctrl reg failed: %d", ret);
		goto error_out;
	}

	kfree (buf);
	printk (version);
	return ret;

error_out:
	if (ax178dataptr)
		kfree (ax178dataptr);
	kfree (buf);
	return ret;
}

static void ax88178_unbind(struct usbnet *dev, struct usb_interface *intf)
{
	struct ax88178_data *ax178dataptr = (struct ax88178_data *)dev->priv;

	if (ax178dataptr) {

		/* stop MAC operation */
		ax8817x_write_cmd(dev, AX_CMD_WRITE_RX_CTL,
					AX_RX_CTL_STOP, 0, 0, NULL);

		kfree (ax178dataptr);
	}
}

static int ax88772_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
{
	u8  *head;
	u32  header;
	char *packet;
	struct sk_buff *ax_skb;
	u16 size;

	head = (u8 *) skb->data;
	memcpy(&header, head, sizeof(header));
	le32_to_cpus(&header);
	packet = head + sizeof(header);

	skb_pull(skb, 4);

	while (skb->len > 0) {
		if ((short)(header & 0x0000ffff) !=
		    ~((short)((header & 0xffff0000) >> 16))) {
			deverr(dev, "header length data is error 0x%08x, %d\n",
				header, skb->len);
		}
		/* get the packet length */
		size = (u16) (header & 0x0000ffff);

		if ((skb->len) - ((size + 1) & 0xfffe) == 0) {

			/* Make sure ip header is aligned on 32-bit boundary */
			if (!((unsigned long)skb->data & 0x02)) {
				memmove (skb->data - 2, skb->data, size);
				skb->data -= 2;
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22)
				skb->tail = skb->data + size;
#else
				skb_set_tail_pointer (skb, size);
#endif
			}
			skb->truesize = size + sizeof(struct sk_buff);
			return 2;
		}

		if (size > ETH_FRAME_LEN) {
			deverr(dev, "invalid rx length %d", size);
			return 0;
		}
		ax_skb = skb_clone(skb, GFP_ATOMIC);
		if (ax_skb) {

			/* Make sure ip header is aligned on 32-bit boundary */
			if (!((unsigned long)packet & 0x02)) {
				memmove (packet - 2, packet, size);
				packet -= 2;
			}
			ax_skb->data = packet;
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22)
			ax_skb->tail = packet + size;
#else
			skb_set_tail_pointer (ax_skb, size);
#endif
			ax_skb->truesize = size + sizeof(struct sk_buff);
			axusbnet_skb_return(dev, ax_skb);

		} else {
			return 0;
		}

		skb_pull(skb, (size + 1) & 0xfffe);

		if (skb->len == 0)
			break;

		head = (u8 *) skb->data;
		memcpy(&header, head, sizeof(header));
		le32_to_cpus(&header);
		packet = head + sizeof(header);
		skb_pull(skb, 4);
	}

	if (skb->len < 0) {
		deverr(dev, "invalid rx length %d", skb->len);
		return 0;
	}
	return 1;
}

static struct sk_buff *ax88772_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
{
	int padlen = ((skb->len + 4) % 512) ? 0 : 4;
	u32 packet_len;
	u32 padbytes = 0xffff0000;

#if (!AX_FORCE_BUFF_ALIGN)
	int headroom = skb_headroom(skb);
	int tailroom = skb_tailroom(skb);

	if ((!skb_cloned(skb))
	    && ((headroom + tailroom) >= (4 + padlen))) {
		if ((headroom < 4) || (tailroom < padlen)) {
			skb->data = memmove(skb->head + 4, skb->data, skb->len);
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22)
		skb->tail = skb->data + skb->len;
#else
		skb_set_tail_pointer (skb, skb->len);
#endif
		}
	} else
#endif
	{
		struct sk_buff *skb2;
		skb2 = skb_copy_expand(skb, 4, padlen, flags);
		dev_kfree_skb_any(skb);
		skb = skb2;
		if (!skb)
			return NULL;
	}

	skb_push(skb, 4);
	packet_len = (((skb->len - 4) ^ 0x0000ffff) << 16) + (skb->len - 4);
	cpu_to_le32s(&packet_len);
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22)
	memcpy(skb->data, &packet_len, sizeof(packet_len));
#else
	skb_copy_to_linear_data(skb, &packet_len, sizeof(packet_len));
#endif

	if ((skb->len % 512) == 0) {
		cpu_to_le32s(&padbytes);
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22)
		memcpy(skb->tail, &padbytes, sizeof(padbytes));
#else
		memcpy(skb_tail_pointer(skb), &padbytes, sizeof(padbytes));
#endif
		skb_put(skb, sizeof(padbytes));
	}
	return skb;
}

static void
ax88772b_rx_checksum (struct sk_buff *skb, struct ax88772b_rx_header *rx_hdr)
{
	skb->ip_summed = CHECKSUM_NONE;

	/* checksum error bit is set */
	if (rx_hdr->l3_csum_err || rx_hdr->l4_csum_err) {
		return;
	}

	/* It must be a TCP or UDP packet with a valid checksum */
	if ((rx_hdr->l4_type == AX_RXHDR_L4_TYPE_TCP) ||
	    (rx_hdr->l4_type == AX_RXHDR_L4_TYPE_UDP)) {
		skb->ip_summed = CHECKSUM_UNNECESSARY;
	}
}

static int ax88772b_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
{
	struct ax88772b_rx_header rx_hdr;
	struct sk_buff *ax_skb;
	struct ax88772b_data *ax772b_data = (struct ax88772b_data *)dev->priv;

	while (skb->len > 0) {

		le16_to_cpus((u16 *)skb->data);
		le16_to_cpus(((u16 *)skb->data) + 1);

		memcpy (&rx_hdr, skb->data, sizeof (struct ax88772b_rx_header));

		if ((short)rx_hdr.len != (~((short)rx_hdr.len_bar) & 0x7FF)) {
			return 0;
		}

		if (rx_hdr.len > (ETH_FRAME_LEN + 4)) {
			deverr(dev, "invalid rx length %d", rx_hdr.len);
			return 0;
		}

		if (skb->len - ((rx_hdr.len + 
				 sizeof (struct ax88772b_rx_header) + 3) & 
				 0xfffc) == 0) {
			skb_pull(skb, sizeof (struct ax88772b_rx_header));
			skb->len = rx_hdr.len;

#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22)
			skb->tail = skb->data + rx_hdr.len;
#else
			skb_set_tail_pointer(skb, rx_hdr.len);
#endif
			skb->truesize = rx_hdr.len + sizeof(struct sk_buff);

			if (ax772b_data->checksum & AX_RX_CHECKSUM)
				ax88772b_rx_checksum (skb, &rx_hdr);

			return 2;
		}

		ax_skb = skb_clone(skb, GFP_ATOMIC);
		if (ax_skb) {
			ax_skb->len = rx_hdr.len;
			ax_skb->data = skb->data + 
				       sizeof (struct ax88772b_rx_header);

#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22)
			ax_skb->tail = ax_skb->data + rx_hdr.len;
#else
			skb_set_tail_pointer(ax_skb, rx_hdr.len);
#endif
			ax_skb->truesize = rx_hdr.len + sizeof(struct sk_buff);

			if (ax772b_data->checksum & AX_RX_CHECKSUM) {
				ax88772b_rx_checksum (ax_skb, &rx_hdr);
			}

			axusbnet_skb_return(dev, ax_skb);

		} else {
			return 0;
		}

		skb_pull(skb, ((rx_hdr.len + 
				sizeof (struct ax88772b_rx_header) + 3)
				& 0xfffc));
	}

	if (skb->len < 0) {
		deverr(dev, "invalid rx length %d", skb->len);
		return 0;
	}
	return 1;
}

static struct sk_buff *
ax88772b_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
{
	int padlen = ((skb->len + 4) % 512) ? 0 : 4;
	u32 packet_len;
	u32 padbytes = 0xffff0000;

#if (!AX_FORCE_BUFF_ALIGN)
	int headroom = skb_headroom(skb);
	int tailroom = skb_tailroom(skb);

	if ((!skb_cloned(skb))
	    && ((headroom + tailroom) >= (4 + padlen))) {
		if ((headroom < 4) || (tailroom < padlen)) {
			skb->data = memmove(skb->head + 4, skb->data, skb->len);
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22)
			skb->tail = skb->data + skb->len;
#else
			skb_set_tail_pointer(skb, skb->len);
#endif
		}
	} else
#endif
	{
		struct sk_buff *skb2;
		skb2 = skb_copy_expand(skb, 4, padlen, flags);
		dev_kfree_skb_any(skb);
		skb = skb2;
		if (!skb)
			return NULL;
	}

	skb_push(skb, 4);
	packet_len = (((skb->len - 4) ^ 0x0000ffff) << 16) + (skb->len - 4);

	cpu_to_le32s (&packet_len);
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22)
	memcpy(skb->data, &packet_len, sizeof(packet_len));
#else
	skb_copy_to_linear_data(skb, &packet_len, sizeof(packet_len));
#endif

	if ((skb->len % 512) == 0) {
		cpu_to_le32s (&padbytes);
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22)
		memcpy(skb->tail, &padbytes, sizeof(padbytes));
#else
		memcpy(skb_tail_pointer(skb), &padbytes, sizeof(padbytes));
#endif
		skb_put(skb, sizeof(padbytes));
	}

	return skb;
}

static const u8 ChkCntSel [6][3] = 
{
	{12, 23, 31},
	{12, 31, 23},
	{23, 31, 12},
	{23, 12, 31},
	{31, 12, 23},
	{31, 23, 12}
};

#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
static void ax88772_link_reset (void *data)
{
	struct usbnet *dev = (struct usbnet *)data;
	struct ax88772_data *ax772_data = (struct ax88772_data *)dev->priv;
#else
static void ax88772_link_reset (struct work_struct *work)
{
	struct ax88772_data *ax772_data = container_of (work, 
					struct ax88772_data, check_link);
	struct usbnet *dev = ax772_data->dev;
#endif
	
	if (ax772_data->Event == AX_SET_RX_CFG) {
		u16 bmcr;
		u16 mode;
		
		ax772_data->Event = AX_NOP;
	
		mode = AX88772_MEDIUM_DEFAULT;

		bmcr = ax8817x_mdio_read_le(dev->net, 
				dev->mii.phy_id, MII_BMCR);
		if (!(bmcr & BMCR_FULLDPLX))
			mode &= ~AX88772_MEDIUM_FULL_DUPLEX;
		if (!(bmcr & BMCR_SPEED100))
			mode &= ~AX88772_MEDIUM_100MB;

		ax8817x_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE, 
			mode, 0, 0, NULL);
		return;
	}
	
	switch (ax772_data->Event) {
	  case WAIT_AUTONEG_COMPLETE:
		if (jiffies > (ax772_data->autoneg_start + 5 * HZ)) {
			ax772_data->Event = PHY_POWER_DOWN;
			ax772_data->TickToExpire = 23;
		}
		break;
	  case PHY_POWER_DOWN:
		if (ax772_data->TickToExpire == 23) {
			/* Set Phy Power Down */
			ax8817x_write_cmd(dev, AX_CMD_SW_RESET,
					  AX_SWRESET_IPPD,
					  0, 0, NULL);
			--ax772_data->TickToExpire;
		} else if (--ax772_data->TickToExpire == 0) {
			/* Set Phy Power Up */
			ax8817x_write_cmd(dev, AX_CMD_SW_RESET,
				AX_SWRESET_IPRL, 0, 0, NULL);
			ax8817x_write_cmd(dev, AX_CMD_SW_RESET,
				AX_SWRESET_IPPD | AX_SWRESET_IPRL, 0, 0, NULL);
			msleep(10);
			ax8817x_write_cmd(dev, AX_CMD_SW_RESET,
				AX_SWRESET_IPRL, 0, 0, NULL);
			msleep(60);
			ax8817x_write_cmd(dev, AX_CMD_SW_RESET,
				AX_SWRESET_CLEAR, 0, 0, NULL);
			ax8817x_write_cmd(dev, AX_CMD_SW_RESET,
				AX_SWRESET_IPRL, 0, 0, NULL);

			ax8817x_mdio_write_le(dev->net, dev->mii.phy_id, 
				MII_ADVERTISE,
				ADVERTISE_ALL | ADVERTISE_CSMA | 
				ADVERTISE_PAUSE_CAP);
			mii_nway_restart(&dev->mii);
			
			ax772_data->Event = PHY_POWER_UP;
			ax772_data->TickToExpire = 47;
		}
		break;
	  case PHY_POWER_UP:
		if (--ax772_data->TickToExpire == 0) {
			ax772_data->Event = PHY_POWER_DOWN;
			ax772_data->TickToExpire = 23;
		}
		break;
	  default:
		break;
	}
	return;
}

#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
static void ax88772a_link_reset (void *data)
{
	struct usbnet *dev = (struct usbnet *)data;
	struct ax88772a_data *ax772a_data = (struct ax88772a_data *)dev->priv;
#else
static void ax88772a_link_reset (struct work_struct *work)
{
	struct ax88772a_data *ax772a_data = container_of (work, 
					struct ax88772a_data, check_link);
	struct usbnet *dev = ax772a_data->dev;
#endif
	int PowSave = (ax772a_data->EepromData >> 14);
	u16 phy_reg;
	
	if (ax772a_data->Event == AX_SET_RX_CFG) {
		u16 bmcr;
		u16 mode;

		ax772a_data->Event = AX_NOP;
	
		mode = AX88772_MEDIUM_DEFAULT;

		bmcr = ax8817x_mdio_read_le(dev->net, 
				dev->mii.phy_id, MII_BMCR);
		if (!(bmcr & BMCR_FULLDPLX))
			mode &= ~AX88772_MEDIUM_FULL_DUPLEX;
		if (!(bmcr & BMCR_SPEED100))
			mode &= ~AX88772_MEDIUM_100MB;

		ax8817x_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE, 
			mode, 0, 0, NULL);
		return;
	}

	switch (ax772a_data->Event) {
	case WAIT_AUTONEG_COMPLETE:
		if (jiffies > (ax772a_data->autoneg_start + 5 * HZ)) {
			ax772a_data->Event = CHK_CABLE_EXIST;
			ax772a_data->TickToExpire = 14;
		}
		break;
	case CHK_CABLE_EXIST:
		phy_reg = ax8817x_mdio_read_le(dev->net, dev->mii.phy_id, 0x12);
		if ((phy_reg != 0x8012) && (phy_reg != 0x8013)) {
			ax8817x_mdio_write_le(dev->net,
				dev->mii.phy_id, 0x16, 0x4040);
			mii_nway_restart(&dev->mii);
			ax772a_data->Event = CHK_CABLE_STATUS;
			ax772a_data->TickToExpire = 31;
		} else if (--ax772a_data->TickToExpire == 0) {
			mii_nway_restart(&dev->mii);
			ax772a_data->Event = CHK_CABLE_EXIST_AGAIN;
			if (PowSave == 0x03){
			  ax772a_data->TickToExpire = 47;
			} else if (PowSave == 0x01) {
			  ax772a_data->DlyIndex = (u8)(jiffies % 6);
			  ax772a_data->DlySel = 0;
			  ax772a_data->TickToExpire = 
			  ChkCntSel[ax772a_data->DlyIndex][ax772a_data->DlySel];
			}
		}
		break;
	case CHK_CABLE_EXIST_AGAIN:
		/* if cable disconnected */
		phy_reg = ax8817x_mdio_read_le(dev->net, dev->mii.phy_id, 0x12);
		if ((phy_reg != 0x8012) && (phy_reg != 0x8013)) {
			mii_nway_restart(&dev->mii);
			ax772a_data->Event = CHK_CABLE_STATUS;
			ax772a_data->TickToExpire = 31;
		} else if (--ax772a_data->TickToExpire == 0) {
			/* Power down PHY */
			ax8817x_write_cmd(dev, AX_CMD_SW_RESET,
					  AX_SWRESET_IPPD,
					  0, 0, NULL);
			ax772a_data->Event = PHY_POWER_DOWN;
			if (PowSave == 0x03){
			  ax772a_data->TickToExpire = 23;
			} else if (PowSave == 0x01) {
			  ax772a_data->TickToExpire = 31;
			}
		}
		break;
	case PHY_POWER_DOWN:
		if (--ax772a_data->TickToExpire == 0) {
			ax772a_data->Event = PHY_POWER_UP;
		}
		break;
	case CHK_CABLE_STATUS:
		if (--ax772a_data->TickToExpire == 0) {
			ax8817x_mdio_write_le(dev->net,
					dev->mii.phy_id, 0x16, 0x4040);
			mii_nway_restart(&dev->mii);
			ax772a_data->Event = CHK_CABLE_EXIST_AGAIN;
			if (PowSave == 0x03){
			  ax772a_data->TickToExpire = 47;
			} else if (PowSave == 0x01) {
			  ax772a_data->DlyIndex = (u8)(jiffies % 6);
			  ax772a_data->DlySel = 0;
			  ax772a_data->TickToExpire = 
			  ChkCntSel[ax772a_data->DlyIndex][ax772a_data->DlySel];
			}
		}
		break;
	case PHY_POWER_UP:

		ax88772a_phy_powerup (dev);

		ax8817x_mdio_write_le(dev->net, dev->mii.phy_id, MII_ADVERTISE,
			ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);

		mii_nway_restart(&dev->mii);

		ax772a_data->Event = CHK_CABLE_EXIST_AGAIN;

		if (PowSave == 0x03){
			  ax772a_data->TickToExpire = 47;
			  
		} else if (PowSave == 0x01) {
		  
		  if (++ax772a_data->DlySel >= 3) {
		    ax772a_data->DlyIndex = (u8)(jiffies % 6);
		    ax772a_data->DlySel = 0;
		  }  
		  ax772a_data->TickToExpire = 
			ChkCntSel[ax772a_data->DlyIndex][ax772a_data->DlySel];
		}
		break;
	default:
		break;
	}

	return;
}

#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
static void ax88772b_link_reset (void *data)
{
	struct usbnet *dev = (struct usbnet *)data;
	struct ax88772b_data *ax772b_data = (struct ax88772b_data *)dev->priv;
#else
static void ax88772b_link_reset (struct work_struct *work)
{
	struct ax88772b_data *ax772b_data = container_of (work, 
					struct ax88772b_data, check_link);
	struct usbnet *dev = ax772b_data->dev;
#endif

	switch (ax772b_data->Event) {

	case AX_SET_RX_CFG:
	{
		u16 bmcr = ax8817x_mdio_read_le(dev->net,
					dev->mii.phy_id, MII_BMCR);
		u16 mode = AX88772_MEDIUM_DEFAULT;

		if (!(bmcr & BMCR_FULLDPLX))
			mode &= ~AX88772_MEDIUM_FULL_DUPLEX;
		if (!(bmcr & BMCR_SPEED100))
			mode &= ~AX88772_MEDIUM_100MB;

		ax8817x_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE, 
					mode, 0, 0, NULL);
		break;
	}
	case PHY_POWER_UP:
	{
		u16 tmp16;

		ax88772a_phy_powerup (dev);
		tmp16 = ax8817x_mdio_read_le(dev->net, dev->mii.phy_id, 0x12);
		ax8817x_mdio_write_le(dev->net, dev->mii.phy_id, 0x12,
				((tmp16 & 0xFF9F) | 0x0040));

		ax8817x_mdio_write_le(dev->net, dev->mii.phy_id, MII_ADVERTISE,
			ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
		break;
	}
	default:
		break;
	}

	ax772b_data->Event = AX_NOP;

	return;
}

static int ax88178_set_media(struct usbnet *dev)
{
	int	ret;
	struct ax88178_data *ax178dataptr = (struct ax88178_data *)dev->priv;
	int media;

	media = ax88178_media_check (dev, ax178dataptr);
	if (media < 0)
		return media;

	if ((ret = ax8817x_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE, 
			media, 0, 0, NULL)) < 0) {
		deverr(dev, "write mode medium reg failed: %d", ret);
		return ret;
	}

	return 0;
}

static int ax88178_link_reset(struct usbnet *dev)
{
	return ax88178_set_media (dev);
}

static int ax_suspend (struct usb_interface *intf,
#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,10)
			pm_message_t message)
#else
			u32 message)
#endif
{
	struct usbnet *dev = usb_get_intfdata(intf);
	struct ax8817x_data *data = (struct ax8817x_data *)&dev->data;

	return data->suspend (intf, message);
}

static int ax_resume (struct usb_interface *intf)
{
	struct usbnet *dev = usb_get_intfdata(intf);
	struct ax8817x_data *data = (struct ax8817x_data *)&dev->data;

	return data->resume (intf);
}

static const struct driver_info ax88178_info = {
	.description = "ASIX AX88178 USB 2.0 Ethernet",
	.bind = ax88178_bind,
	.unbind = ax88178_unbind,
	.status = ax88178_status,
	.link_reset = ax88178_link_reset,
	.reset = ax88178_link_reset,
	.flags =  FLAG_ETHER | FLAG_FRAMING_AX,
	.rx_fixup = ax88772_rx_fixup,
	.tx_fixup = ax88772_tx_fixup,
};

static const struct driver_info belkin178_info = {
	.description = "Belkin Gigabit USB 2.0 Network Adapter",
	.bind = ax88178_bind,
	.unbind = ax88178_unbind,
	.status = ax8817x_status,
	.link_reset = ax88178_link_reset,
	.reset = ax88178_link_reset,
	.flags =  FLAG_ETHER | FLAG_FRAMING_AX,
	.rx_fixup = ax88772_rx_fixup,
	.tx_fixup = ax88772_tx_fixup,
};

static const struct driver_info ax8817x_info = {
	.description = "ASIX AX8817x USB 2.0 Ethernet",
	.bind = ax8817x_bind,
	.status = ax8817x_status,
	.link_reset = ax88172_link_reset,
	.reset = ax88172_link_reset,
	.flags =  FLAG_ETHER,
};

static const struct driver_info dlink_dub_e100_info = {
	.description = "DLink DUB-E100 USB Ethernet",
	.bind = ax8817x_bind,
	.status = ax8817x_status,
	.link_reset = ax88172_link_reset,
	.reset = ax88172_link_reset,
	.flags =  FLAG_ETHER,
};

static const struct driver_info netgear_fa120_info = {
	.description = "Netgear FA-120 USB Ethernet",
	.bind = ax8817x_bind,
	.status = ax8817x_status,
	.link_reset = ax88172_link_reset,
	.reset = ax88172_link_reset,
	.flags =  FLAG_ETHER,
};

static const struct driver_info hawking_uf200_info = {
	.description = "Hawking UF200 USB Ethernet",
	.bind = ax8817x_bind,
	.status = ax8817x_status,
	.link_reset = ax88172_link_reset,
	.reset = ax88172_link_reset,
	.flags =  FLAG_ETHER,
};

static const struct driver_info ax88772_info = {
	.description = "ASIX AX88772 USB 2.0 Ethernet",
	.bind = ax88772_bind,
	.unbind = ax88772_unbind,
	.status = ax88772_status,
	.flags = FLAG_ETHER | FLAG_FRAMING_AX,
	.rx_fixup = ax88772_rx_fixup,
	.tx_fixup = ax88772_tx_fixup,
};

static const struct driver_info dlink_dub_e100b_info = {
	.description = "D-Link DUB-E100 USB 2.0 Fast Ethernet Adapter",
	.bind = ax88772_bind,
	.unbind = ax88772_unbind,
	.status = ax88772_status,
	.flags = FLAG_ETHER | FLAG_FRAMING_AX,
	.rx_fixup = ax88772_rx_fixup,
	.tx_fixup = ax88772_tx_fixup,
};

static const struct driver_info ax88772a_info = {
	.description = "ASIX AX88772A USB 2.0 Ethernet",
	.bind = ax88772a_bind,
	.unbind = ax88772a_unbind,
	.status = ax88772a_status,
	.flags = FLAG_ETHER | FLAG_FRAMING_AX,
	.rx_fixup = ax88772_rx_fixup,
	.tx_fixup = ax88772_tx_fixup,
};

static const struct driver_info ax88772b_info = {
	.description = "ASIX AX88772B USB 2.0 Ethernet",
	.bind = ax88772b_bind,
	.unbind = ax88772b_unbind,
	.status = ax88772b_status,
	.flags = FLAG_ETHER | FLAG_FRAMING_AX | FLAG_HW_IP_ALIGNMENT,
	.rx_fixup = ax88772b_rx_fixup,
	.tx_fixup = ax88772b_tx_fixup,
};

static const struct usb_device_id	products [] = {
{
	// 88178
	USB_DEVICE (0x0b95, 0x1780),
	.driver_info =	(unsigned long) &ax88178_info,
}, {
	// 88178 for billianton linksys
	USB_DEVICE (0x077b, 0x2226),
	.driver_info =	(unsigned long) &ax88178_info,
}, {
	// ABOCOM for linksys
	USB_DEVICE (0x1737, 0x0039),
	.driver_info =	(unsigned long) &ax88178_info,
}, {
	// ABOCOM  for pci
	USB_DEVICE (0x14ea, 0xab11),
	.driver_info =	(unsigned long) &ax88178_info,
}, {
	// Belkin
	USB_DEVICE (0x050d, 0x5055),
	.driver_info =	(unsigned long) &belkin178_info,
}, {
	// Linksys USB200M
	USB_DEVICE (0x077b, 0x2226),
	.driver_info =	(unsigned long) &ax8817x_info,
}, {
	// Netgear FA120
	USB_DEVICE (0x0846, 0x1040),
	.driver_info =  (unsigned long) &netgear_fa120_info,
}, {
	// DLink DUB-E100
	USB_DEVICE (0x2001, 0x1a00),
	.driver_info =  (unsigned long) &dlink_dub_e100_info,
}, {
	// DLink DUB-E100B
	USB_DEVICE (0x2001, 0x3c05),
	.driver_info =  (unsigned long) &dlink_dub_e100b_info,
}, {
	// DLink DUB-E100B
	USB_DEVICE (0x07d1, 0x3c05),
	.driver_info =  (unsigned long) &dlink_dub_e100b_info,
}, {
	// Intellinet, ST Lab USB Ethernet
	USB_DEVICE (0x0b95, 0x1720),
	.driver_info =  (unsigned long) &ax8817x_info,
}, {
	// Hawking UF200, TrendNet TU2-ET100
	USB_DEVICE (0x07b8, 0x420a),
	.driver_info =  (unsigned long) &hawking_uf200_info,
}, {
        // Billionton Systems, USB2AR
        USB_DEVICE (0x08dd, 0x90ff),
        .driver_info =  (unsigned long) &ax8817x_info,
}, {
	// ATEN UC210T
	USB_DEVICE (0x0557, 0x2009),
	.driver_info =  (unsigned long) &ax8817x_info,
}, {
	// Buffalo LUA-U2-KTX
	USB_DEVICE (0x0411, 0x003d),
	.driver_info =  (unsigned long) &ax8817x_info,
}, {
	// Sitecom LN-029 "USB 2.0 10/100 Ethernet adapter"
	USB_DEVICE (0x6189, 0x182d),
	.driver_info =  (unsigned long) &ax8817x_info,
}, {
	// corega FEther USB2-TX
	USB_DEVICE (0x07aa, 0x0017),
	.driver_info =  (unsigned long) &ax8817x_info,
}, {
	// Surecom EP-1427X-2
	USB_DEVICE (0x1189, 0x0893),
	.driver_info = (unsigned long) &ax8817x_info,
}, {
	// goodway corp usb gwusb2e
	USB_DEVICE (0x1631, 0x6200),
	.driver_info = (unsigned long) &ax8817x_info,
}, {
	// ASIX AX88772 10/100
        USB_DEVICE (0x0b95, 0x7720),
        .driver_info = (unsigned long) &ax88772_info,
}, {
	// ASIX AX88772 10/100
        USB_DEVICE (0x125E, 0x180D),
        .driver_info = (unsigned long) &ax88772_info,
}, {
	// ASIX AX88772A 10/100
        USB_DEVICE (0x0b95, 0x772A),
        .driver_info = (unsigned long) &ax88772a_info,
}, {
	// ASIX AX88772A 10/100
        USB_DEVICE (0x0db0, 0xA877),
        .driver_info = (unsigned long) &ax88772a_info,
}, {
	// ASIX AX88772A 10/100
        USB_DEVICE (0x0421, 0x772A),
        .driver_info = (unsigned long) &ax88772a_info,
}, {
	// Linksys 200M
        USB_DEVICE (0x13B1, 0x0018),
        .driver_info = (unsigned long) &ax88772a_info,
}, {
        USB_DEVICE (0x05ac, 0x1402),
        .driver_info = (unsigned long) &ax88772a_info,
}, {
	// ASIX AX88772B 10/100
        USB_DEVICE (0x0b95, 0x772B),
        .driver_info = (unsigned long) &ax88772b_info,
}, {
	// ASIX AX88772B 10/100
        USB_DEVICE (0x0b95, 0x7E2B),
        .driver_info = (unsigned long) &ax88772b_info,
},
	{ },		// END
};
MODULE_DEVICE_TABLE(usb, products);

static struct usb_driver asix_driver = {
//	.owner =	THIS_MODULE,
	.name =		"asix",
	.id_table =	products,
	.probe =	axusbnet_probe,
	.suspend =	ax_suspend,
	.resume =	ax_resume,
	.disconnect =	axusbnet_disconnect,
};

static int __init asix_init(void)
{
 	return usb_register(&asix_driver);
}
module_init(asix_init);

static void __exit asix_exit(void)
{
 	usb_deregister(&asix_driver);
}
module_exit(asix_exit);

MODULE_AUTHOR("David Hollis");
MODULE_DESCRIPTION("ASIX AX8817X based USB 2.0 Ethernet Devices");
MODULE_LICENSE("GPL");