summaryrefslogtreecommitdiffstats
path: root/drivers/net/arcnet.c
blob: b25dd02c26bdd42a81a7adaaaf911e21a27905a5 (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
/* arcnet.c:
	Written 1994-1996 by Avery Pennarun,
	derived from skeleton.c by Donald Becker.

	Contact Avery at: apenwarr@foxnet.net or
	RR #5 Pole Line Road, Thunder Bay, ON, Canada P7C 5M9

	**********************

	The original copyright was as follows:

	skeleton.c Written 1993 by Donald Becker.
	Copyright 1993 United States Government as represented by the
        Director, National Security Agency.  This software may only be used
        and distributed according to the terms of the GNU Public License as
        modified by SRC, incorporated herein by reference.

	**********************

	v2.60 ALPHA (96/11/23)
	  - Added patch from Vojtech Pavlik <vojtech@atrey.karlin.mff.cuni.cz>
	    and Martin Mares <mj@k332.feld.cvut.cz> to make the driver work
	    with the new Linux 2.1.x memory management.  I modified their
	    patch quite a bit though; bugs are my fault.  More changes should
	    be made to get eliminate any remaining phys_to_virt calls.
	  - Quietly ignore protocol id's 0, 1, 8, and 243.  Thanks to Jake
	    Messinger <jake@ams.com> for reporting these codes and their
	    meanings.
	  - Smarter shmem probe for cards with 4k mirrors. (does it work?)
	  - Initial support for RIM I type cards which use no I/O ports at
	    all.  To use this option, you need to compile with RIM_I_MODE
	    enabled.  Thanks to Kolja Waschk <kawk@yo.com> for explaining
	    RIM I programming to me.  Now, does my RIM I code actually
	    work?

	v2.56 (96/10/18)
	  - Turned arc0e/arc0s startup messages back on by default, as most
	    people will probably not notice the additional devices
	    otherwise.  This causes undue confusion.
	  - Fixed a tiny but noticeable bug in the packet debugging routines
	    (thanks Tomasz)

	The following has been SUMMARIZED.  The complete ChangeLog is
	available in the full Linux-ARCnet package at
		http://www.foxnet.net/~apenwarr/arcnet

	v2.50 (96/02/24)
	  - Massively improved autoprobe routines; they now work even as a
	    module.  Thanks to Vojtech Pavlik <Vojtech.Pavlik@st.mff.cuni.cz>
	    for his ideas and help in this area.
	  - Changed printk's around quite a lot.

	v2.22 (95/12/08)
	  - Major cleanups, speedups, and better code-sharing.
	  - Eliminated/changed many useless/meaningless/scary debug messages
	    (and, in most cases, the bugs that caused them).
	  - Better IPX support.
	  - lp->stats updated properly.
	  - RECON checking now by default only prints a message if there are
	    excessive errors (ie. your cable is probably broken).
	  - New RFC1051-compliant "arc0s" virtual device by Tomasz
	    Motylewski.
	  - Excess debug messages can be compiled out to reduce code size.

	v2.00 (95/09/06)
	  - ARCnet RECON messages are now detected and logged as "carrier"
	    errors.
	  - The TXACK flag is now checked, and errors are logged.
	  - Debug levels are now completely different.  See the README.
	  - Massive code cleanups, with several no-longer-necessary and some
	    completely useless options removed.
	  - Multiprotocol support.  You can now use the "arc0e" device to
	    send "Ethernet-Encapsulation" packets, which are compatible with
	    Windows for Workgroups and LAN Manager, and possibly other
	    software.  See the README for more information.

	v1.02 (95/06/21)
          - A fix to make "exception" packets sent from Linux receivable
	    on other systems.  (The protocol_id byte was sometimes being set
	    incorrectly, and Linux wasn't checking it on receive so it
	    didn't show up)

	v1.01 (95/03/24)
	  - Fixed some IPX-related bugs. (Thanks to Tomasz Motylewski
            <motyl@tichy.ch.uj.edu.pl> for the patches to make arcnet work
            with dosemu!)

	v1.00 (95/02/15)
	  - Initial non-alpha release.


	TO DO: (semi-prioritized)

         - Use cleaner "architecture-independent" shared memory access.
           This is half-done in ARCnet 2.60, but still uses some
           undocumented i386 stuff.  (We shouldn't call phys_to_virt,
           for example.)
         - Support "arpless" mode like NetBSD does, and as recommended
           by the (obsoleted) RFC1051.
         - Some way to make RIM_I_MODE runtime switchable?  Yuck...
         - Smarter recovery from RECON-during-transmit conditions. (ie.
           retransmit immediately)
         - Make arcnetE_send_packet use arcnet_prepare_tx for loading the
           packet into ARCnet memory.
         - Probe for multiple devices in one shot (trying to decide whether
           to do it the "ugly" way or not).
         - Add support for the new 1.3.x IP header cache, and other features.
         - Debug level should be changed with a system call, not a hack to
           the "metric" flag.
         - What about cards with shared memory that can be "turned off?"
           (or that have none at all, like the SMC PC500longboard)
         - Autoconfigure PDI5xxPlus cards. (I now have a PDI508Plus to play
           with temporarily.)  Update: yes, the Pure Data config program
           for DOS works fine, but the PDI508Plus I have doesn't! :)
         - Try to implement promiscuous (receive-all-packets) mode available
           on some newer cards with COM20020 and similar chips.  I don't have
           one, but SMC sent me the specs.
         - ATA protocol support??
         - VINES TCP/IP encapsulation?? (info needed)


	Sources:
	 - Crynwr arcnet.com/arcether.com packet drivers.
	 - arcnet.c v0.00 dated 1/1/94 and apparently by
	 	Donald Becker - it didn't work :)
	 - skeleton.c v0.05 dated 11/16/93 by Donald Becker
	 	(from Linux Kernel 1.1.45)
	 - RFC's 1201 and 1051 - re: TCP/IP over ARCnet
	 - The official ARCnet COM9026 data sheets (!) thanks to Ken
		Cornetet <kcornete@nyx10.cs.du.edu>
	 - Information on some more obscure ARCnet controller chips, thanks
	   to the nice people at SMC.
	 - net/inet/eth.c (from kernel 1.1.50) for header-building info.
	 - Alternate Linux ARCnet source by V.Shergin <vsher@sao.stavropol.su>
	 - Textual information and more alternate source from Joachim Koenig
	 	<jojo@repas.de>
*/

static const char *version =
 "arcnet.c: v2.60 96/11/23 Avery Pennarun <apenwarr@foxnet.net>\n";



#include <linux/module.h>
#include <linux/config.h>
#include <linux/version.h>

#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/types.h>
#include <linux/fcntl.h>
#include <linux/interrupt.h>
#include <linux/ptrace.h>
#include <linux/ioport.h>
#include <linux/in.h>
#include <linux/malloc.h>
#include <linux/string.h>
#include <linux/timer.h>
#include <linux/errno.h>
#include <linux/delay.h>
#include <linux/netdevice.h>
#include <linux/if_arp.h>
#include <linux/etherdevice.h>
#include <linux/skbuff.h>
#include <linux/init.h>

#include <asm/system.h>
#include <asm/bitops.h>
#include <asm/io.h>
#include <asm/dma.h>

#include <net/arp.h>

/**************************************************************************/

/* Define this if you have a really ancient "RIM I" ARCnet card with no I/O
 * port at all and _only_ shared memory; this option MAY work for you.  It's
 * untested, though, so good luck and write to me with any results!
 */
#undef RIM_I_MODE

/* Normally, the ARCnet device needs to be assigned a name (default arc0).
 * Ethernet devices have a function to automatically try eth0, eth1, etc
 * until a free name is found.  To name the ARCnet device using an "eth?"
 * device name, define this option.
 */
#undef CONFIG_ARCNET_ETHNAME

/* On a fast computer, the buffer copy from memory to the ARCnet card during
 * a transmit can hog the bus just a little too long.  SLOW_XMIT_COPY
 * replaces the fast memcpy() with a slower for() loop that seems to solve
 * my problems with ftape.
 *
 * Probably a better solution would be to use memcpy_toio (more portable
 * anyway) and modify that routine to support REALLY_SLOW_IO-style
 * defines; ARCnet probably is not the only driver that can screw up an
 * ftape DMA transfer.
 *
 * Turn this on if you have timing-sensitive DMA (ie. a tape drive) and
 * would like to sacrifice a little bit of network speed to reduce tape
 * write retries or some related problem.
 */
#undef SLOW_XMIT_COPY

/* The card sends the reconfiguration signal when it loses the connection to
 * the rest of its network. It is a 'Hello, is anybody there?' cry.  This
 * usually happens when a new computer on the network is powered on or when
 * the cable is broken.
 *
 * Define DETECT_RECONFIGS if you want to detect network reconfigurations.
 * Recons may be a real nuisance on a larger ARCnet network; if you are a
 * network administrator you probably would like to count them.
 * Reconfigurations will be recorded in stats.tx_carrier_errors (the last
 * field of the /proc/net/dev file).
 *
 * Define SHOW_RECONFIGS if you really want to see a log message whenever
 * a RECON occurs.
 */
#define DETECT_RECONFIGS
#undef SHOW_RECONFIGS

/* RECON_THRESHOLD is the maximum number of RECON messages to receive within
 * one minute before printing a "cabling problem" warning.  You must have
 * DETECT_RECONFIGS enabled if you want to use this.  The default value
 * should be fine.
 *
 * After that, a "cabling restored" message will be printed on the next IRQ
 * if no RECON messages have been received for 10 seconds.
 *
 * Do not define RECON_THRESHOLD at all if you want to disable this feature.
 */
#define RECON_THRESHOLD 30

/* Define this to the minimum "timeout" value.  If a transmit takes longer
 * than TX_TIMEOUT jiffies, Linux will abort the TX and retry.  On a large
 * network, or one with heavy network traffic, this timeout may need to be
 * increased.  The larger it is, though, the longer it will be between
 * necessary transmits - don't set this too large.
 */
#define TX_TIMEOUT 20

/* Define this to speed up the autoprobe by assuming if only one io port and
 * shmem are left in the list at Stage 5, they must correspond to each
 * other.
 *
 * This is undefined by default because it might not always be true, and the
 * extra check makes the autoprobe even more careful.  Speed demons can turn
 * it on - I think it should be fine if you only have one ARCnet card
 * installed.
 *
 * If no ARCnet cards are installed, this delay never happens anyway and thus
 * the option has no effect.
 */
#undef FAST_PROBE

/* Define this to speed up "ifconfig up" by moving the card reset command
 * around.  This is a new option in 2.41 ALPHA.  If it causes problems,
 * undefine this to get the old behaviour; then send me email, because if
 * there are no problems, this option will go away very soon.
 */
#define FAST_IFCONFIG

/* Define this if you want to make it easier to use the "call trace" when
 * a kernel NULL pointer assignment occurs.  Hopefully unnecessary, most of
 * the time.  It will make all the function names (and other things) show
 * up as kernel symbols.  (especially handy when using arcnet as a module)
 */
#undef static

/**************************************************************************/

/* New debugging bitflags: each option can be enabled individually.
 *
 * These can be set while the driver is running by typing:
 *	ifconfig arc0 down metric 1xxx HOSTNAME
 *		where 1xxx is 1000 + the debug level you want
 *		and HOSTNAME is your hostname/ip address
 * and then resetting your routes.
 *
 * An ioctl() should be used for this instead, someday.
 *
 * Note: only debug flags included in the ARCNET_DEBUG_MAX define will
 *   actually be available.  GCC will (at least, GCC 2.7.0 will) notice
 *   lines using a BUGLVL not in ARCNET_DEBUG_MAX and automatically optimize
 *   them out.
 */
#define D_NORMAL	1	/* important operational info		*/
#define D_EXTRA		2	/* useful, but non-vital information	*/
#define	D_INIT		4	/* show init/probe messages		*/
#define D_INIT_REASONS	8	/* show reasons for discarding probes	*/
/* debug levels below give LOTS of output during normal operation! */
#define D_DURING	16	/* trace operations (including irq's)	*/
#define D_TX		32	/* show tx packets			*/
#define D_RX		64	/* show rx packets			*/
#define D_SKB		128	/* show skb's				*/

#ifndef ARCNET_DEBUG_MAX
#define ARCNET_DEBUG_MAX (~0)		/* enable ALL debug messages	 */
/*#define ARCNET_DEBUG_MAX (D_NORMAL|D_EXTRA|D_INIT|D_INIT_REASONS) 	 */
/*#define ARCNET_DEBUG_MAX 0	*/	/* enable NO messages (bad idea) */
#endif

#ifndef ARCNET_DEBUG
#define ARCNET_DEBUG (D_NORMAL|D_EXTRA)
#endif
int arcnet_debug = ARCNET_DEBUG;

/* macros to simplify debug checking */
#define BUGLVL(x) if ((ARCNET_DEBUG_MAX)&arcnet_debug&(x))
#define BUGMSG2(x,msg,args...) BUGLVL(x) printk(msg, ## args)
#define BUGMSG(x,msg,args...) BUGMSG2(x,"%s%6s: " msg, \
            x==D_NORMAL	? KERN_WARNING : \
      x<=D_INIT_REASONS	? KERN_INFO    : KERN_DEBUG , \
	dev->name , ## args)

/* Some useful multiprotocol macros.  The idea here is that GCC will
 * optimize away multiple tests or assignments to lp->adev.  Relying on this
 * results in the cleanest mess possible.
 */
#define ADEV lp->adev

#ifdef CONFIG_ARCNET_ETH
 #define EDEV lp->edev
#else
 #define EDEV lp->adev
#endif

#ifdef CONFIG_ARCNET_1051
 #define SDEV lp->sdev
#else
 #define SDEV lp->adev
#endif

#define TBUSY ADEV->tbusy=EDEV->tbusy=SDEV->tbusy
#define IF_TBUSY (ADEV->tbusy||EDEV->tbusy||SDEV->tbusy)

#define INTERRUPT ADEV->interrupt=EDEV->interrupt=SDEV->interrupt
#define IF_INTERRUPT (ADEV->interrupt||EDEV->interrupt||SDEV->interrupt)

#define START ADEV->start=EDEV->start=SDEV->start


/* The number of low I/O ports used by the ethercard. */
#define ARCNET_TOTAL_SIZE	16

/* Handy defines for ARCnet specific stuff */
	/* COM 9026 controller chip --> ARCnet register addresses */
#define _INTMASK (ioaddr+0)	/* writable */
#define _STATUS  (ioaddr+0)	/* readable */
#define _COMMAND (ioaddr+1)	/* writable, returns random vals on read (?) */
#define _RESET  (ioaddr+8)	/* software reset (on read) */

/* RIM I (command/status is memory mapped) versus RIM III (standard I/O
 * mapped) macros.  These make things a bit cleaner.
 */
#ifdef RIM_I_MODE
  #define IOADDR	(dev->mem_start+0x800)
  #define ARCSTATUS	readb(_STATUS)
  #define ACOMMAND(cmd) writeb((cmd),_COMMAND)
  #define ARCRESET	writeb(TESTvalue,ioaddr-0x800)	/* fake reset */
  #define AINTMASK(msk)	writeb((msk),_INTMASK)
  #define RELEASE_REGION(x,y)	/* nothing */
#else
  #define IOADDR	(dev->base_addr)
  #define ARCSTATUS	inb(_STATUS)
  #define ACOMMAND(cmd) outb((cmd),_COMMAND)
  #define AINTMASK(msk)	outb((msk),_INTMASK)
  #define ARCRESET	inb(_RESET)
  #define RELEASE_REGION(x,y)	release_region((x),(y))
#endif

#define SETMASK AINTMASK(lp->intmask)

	/* Time needed to reset the card - in jiffies.  This works on my SMC
	 * PC100.  I can't find a reference that tells me just how long I
	 * should wait.
	 */
#define RESETtime (HZ * 3 / 10)		/* reset */

	/* these are the max/min lengths of packet data. (including
	 * ClientData header)
	 * note: packet sizes 250, 251, 252 are impossible (God knows why)
	 *  so exception packets become necessary.
	 *
	 * These numbers are compared with the length of the full packet,
	 * including ClientData header.
	 */
#define MTU	253	/* normal packet max size */
#define MinTU	257	/* extended packet min size */
#define XMTU	508	/* extended packet max size */

	/* status/interrupt mask bit fields */
#define TXFREEflag	0x01            /* transmitter available */
#define TXACKflag       0x02            /* transmitted msg. ackd */
#define RECONflag       0x04            /* system reconfigured */
#define TESTflag        0x08            /* test flag */
#define RESETflag       0x10            /* power-on-reset */
#define RES1flag        0x20            /* reserved - usually set by jumper */
#define RES2flag        0x40            /* reserved - usually set by jumper */
#define NORXflag        0x80            /* receiver inhibited */

       /* in the command register, the following bits have these meanings:
        *                0-2     command
        *                3-4     page number (for enable rcv/xmt command)
        *                 7      receive broadcasts
        */
#define NOTXcmd         0x01            /* disable transmitter */
#define NORXcmd         0x02            /* disable receiver */
#define TXcmd           0x03            /* enable transmitter */
#define RXcmd           0x04            /* enable receiver */
#define CONFIGcmd       0x05            /* define configuration */
#define CFLAGScmd       0x06            /* clear flags */
#define TESTcmd         0x07            /* load test flags */

       /* flags for "clear flags" command */
#define RESETclear      0x08            /* power-on-reset */
#define CONFIGclear     0x10            /* system reconfigured */

	/* flags for "load test flags" command */
#define TESTload        0x08            /* test flag (diagnostic) */

	/* byte deposited into first address of buffers on reset */
#define TESTvalue       0321		 /* that's octal for 0xD1 :) */

	/* for "enable receiver" command */
#define RXbcasts        0x80            /* receive broadcasts */

	/* flags for "define configuration" command */
#define NORMALconf      0x00            /* 1-249 byte packets */
#define EXTconf         0x08            /* 250-504 byte packets */

	/* Starts receiving packets into recbuf.
	 */
#define EnableReceiver()	ACOMMAND(RXcmd|(recbuf<<3)|RXbcasts)

	/* RFC1201 Protocol ID's */
#define ARC_P_IP	212		/* 0xD4 */
#define ARC_P_ARP	213		/* 0xD5 */
#define ARC_P_RARP	214		/* 0xD6 */
#define ARC_P_IPX	250		/* 0xFA */
#define ARC_P_NOVELL_EC	236		/* 0xEC */

	/* Old RFC1051 Protocol ID's */
#define ARC_P_IP_RFC1051 240		/* 0xF0 */
#define ARC_P_ARP_RFC1051 241		/* 0xF1 */

	/* MS LanMan/WfWg protocol */
#define ARC_P_ETHER	0xE8

	/* Unsupported/indirectly supported protocols */
#define ARC_P_DATAPOINT_BOOT	0	/* very old Datapoint equipment */
#define ARC_P_DATAPOINT_MOUNT	1
#define ARC_P_POWERLAN_BEACON	8	/* Probably ATA-Netbios related */
#define ARC_P_POWERLAN_BEACON2	243
#define ARC_P_LANSOFT	251		/* 0xFB - what is this? */
#define ARC_P_ATALK	0xDD

	/* the header required by the card itself */
struct HardHeader
{
	u_char	source,		/* source ARCnet - filled in automagically */
		destination,	/* destination ARCnet - 0 for broadcast */
		offset1,	/* offset of ClientData (256-byte packets) */
		offset2;	/* offset of ClientData (512-byte packets) */
};

	/* a complete ARCnet packet */
union ArcPacket
{
	struct HardHeader hardheader;	/* the hardware header */
	u_char raw[512];		/* raw packet info, incl ClientData */
};

	/* the "client data" header - RFC1201 information
	 * notice that this screws up if it's not an even number of bytes
	 * <sigh>
	 */
struct ClientData
{
	/* data that's NOT part of real packet - we MUST get rid of it before
	 * actually sending!!
	 */
	u_char  saddr,		/* Source address - needed for IPX */
		daddr;		/* Destination address */

	/* data that IS part of real packet */
	u_char	protocol_id,	/* ARC_P_IP, ARC_P_ARP, etc */
		split_flag;	/* for use with split packets */
	u_short	sequence;	/* sequence number */
};
#define EXTRA_CLIENTDATA (sizeof(struct ClientData)-4)


	/* the "client data" header - RFC1051 information
	 * this also screws up if it's not an even number of bytes
	 * <sigh again>
	 */
struct S_ClientData
{
	/* data that's NOT part of real packet - we MUST get rid of it before
	 * actually sending!!
	 */
	u_char  saddr,		/* Source address - needed for IPX */
		daddr,		/* Destination address */
		junk;		/* padding to make an even length */

	/* data that IS part of real packet */
	u_char	protocol_id;	/* ARC_P_IP, ARC_P_ARP, etc */
};
#define S_EXTRA_CLIENTDATA (sizeof(struct S_ClientData)-1)


/* "Incoming" is information needed for each address that could be sending
 * to us.  Mostly for partially-received split packets.
 */
struct Incoming
{
	struct sk_buff *skb;		/* packet data buffer             */
	unsigned char lastpacket,	/* number of last packet (from 1) */
		      numpackets;	/* number of packets in split     */
	u_short sequence;		/* sequence number of assembly	  */
};

struct Outgoing
{
	struct sk_buff *skb;		/* buffer from upper levels */
	struct ClientData *hdr;		/* clientdata of last packet */
	u_char *data;			/* pointer to data in packet */
	short length,			/* bytes total */
	      dataleft,			/* bytes left */
	      segnum,			/* segment being sent */
	      numsegs,			/* number of segments */
	      seglen;			/* length of segment */
};


/* Information that needs to be kept for each board. */
struct arcnet_local {
	struct net_device_stats stats;
	u_short sequence;	/* sequence number (incs with each packet) */
	u_char stationid,	/* our 8-bit station address */
		recbuf,		/* receive buffer # (0 or 1) */
		txbuf,		/* transmit buffer # (2 or 3) */
		txready,	/* buffer where a packet is ready to send */
		intmask;	/* current value of INTMASK register */
	short intx,		/* in TX routine? */
	      in_txhandler,	/* in TX_IRQ handler? */
	      sending,		/* transmit in progress? */
	      lastload_dest,	/* can last loaded packet be acked? */
	      lasttrans_dest;	/* can last TX'd packet be acked? */

#if defined(DETECT_RECONFIGS) && defined(RECON_THRESHOLD)
	time_t first_recon,	/* time of "first" RECON message to count */
		last_recon;	/* time of most recent RECON */
	int num_recons,		/* number of RECONs between first and last. */
	    network_down;	/* do we think the network is down? */
#endif

	struct timer_list timer; /* the timer interrupt struct */
	struct Incoming incoming[256];	/* one from each address */
	struct Outgoing outgoing; /* packet currently being sent */

	struct device *adev;	/* RFC1201 protocol device */

#ifdef CONFIG_ARCNET_ETH
	struct device *edev;	/* Ethernet-Encap device */
#endif

#ifdef CONFIG_ARCNET_1051
	struct device *sdev;	/* RFC1051 protocol device */
#endif
};


/* Index to functions, as function prototypes. */

#if ARCNET_DEBUG_MAX & D_SKB
static void arcnet_dump_skb(struct device *dev,struct sk_buff *skb,
	char *desc);
#else
#	define arcnet_dump_skb(dev,skb,desc) ;
#endif

#if (ARCNET_DEBUG_MAX & D_RX) || (ARCNET_DEBUG_MAX & D_TX)
static void arcnet_dump_packet(struct device *dev,u_char *buffer,int ext,
	char *desc);
#else
#	define arcnet_dump_packet(dev,buffer,ext,desc) ;
#endif

extern int arcnet_probe(struct device *dev);
static int arcnet_found(struct device *dev,int port,int airq,u_long shmem);

static void arcnet_setup(struct device *dev);
static int arcnet_open(struct device *dev);
static int arcnet_close(struct device *dev);
static int arcnet_reset(struct device *dev,int reset_delay);

static int arcnet_send_packet_bad(struct sk_buff *skb,struct device *dev);
static int arcnetA_send_packet(struct sk_buff *skb, struct device *dev);
static void arcnetA_continue_tx(struct device *dev);
static void arcnetAS_prepare_tx(struct device *dev,u_char *hdr,int hdrlen,
		char *data,int length,int daddr,int exceptA);
static int arcnet_go_tx(struct device *dev,int enable_irq);

static void arcnet_interrupt(int irq,void *dev_id,struct pt_regs *regs);
static void arcnet_inthandler(struct device *dev);

static void arcnet_rx(struct device *dev,int recbuf);
static void arcnetA_rx(struct device *dev,u_char *buf,
	int length,u_char saddr, u_char daddr);

static struct net_device_stats *arcnet_get_stats(struct device *dev);

int arcnetA_header(struct sk_buff *skb,struct device *dev,
		unsigned short type,void *daddr,void *saddr,unsigned len);
int arcnetA_rebuild_header(struct sk_buff *skb);
unsigned short arcnetA_type_trans(struct sk_buff *skb,struct device *dev);

#ifdef CONFIG_ARCNET_ETH
	/* functions specific to Ethernet-Encap */
static int arcnetE_init(struct device *dev);
static int arcnetE_open_close(struct device *dev);
static int arcnetE_send_packet(struct sk_buff *skb, struct device *dev);
static void arcnetE_rx(struct device *dev,u_char *arcsoft,
	int length,u_char saddr, u_char daddr);
#endif

#ifdef CONFIG_ARCNET_1051
	/* functions specific to RFC1051 */
static int arcnetS_init(struct device *dev);
static int arcnetS_open_close(struct device *dev);
static int arcnetS_send_packet(struct sk_buff *skb, struct device *dev);
static void arcnetS_rx(struct device *dev,u_char *buf,
	int length,u_char saddr, u_char daddr);
int arcnetS_header(struct sk_buff *skb,struct device *dev,
		unsigned short type,void *daddr,void *saddr,unsigned len);
int arcnetS_rebuild_header(struct sk_buff *skb);
unsigned short arcnetS_type_trans(struct sk_buff *skb,struct device *dev);
#endif

#ifdef MODULE
int  init_module(void);
void cleanup_module(void);
#endif

#define tx_done(dev) 1

#define JIFFER(time) for (delayval=jiffies+time; jiffies<delayval;) ;


/****************************************************************************
 *                                                                          *
 * Packet dumps for debugging                                               *
 *                                                                          *
 ****************************************************************************/

/* Dump the contents of an sk_buff
 */
#if ARCNET_DEBUG_MAX & D_SKB
void arcnet_dump_skb(struct device *dev,struct sk_buff *skb,char *desc)
{
	int i;
	long flags;

	save_flags(flags);
	cli();
	printk(KERN_DEBUG "%6s: skb dump (%s) follows:",dev->name,desc);
        for(i=0; i<skb->len; i++)
        {
		if (i%16==0)
			printk("\n" KERN_DEBUG "[%04X] ",i);
               	printk("%02X ",((u_char *)skb->data)[i]);
	}
        printk("\n");
        restore_flags(flags);
}
#endif

/* Dump the contents of an ARCnet buffer
 */
#if (ARCNET_DEBUG_MAX & D_RX) || (ARCNET_DEBUG_MAX & D_TX)
void arcnet_dump_packet(struct device *dev,u_char *buffer,int ext,char *desc)
{
	int i;
	long flags;

	save_flags(flags);
	cli();
	printk(KERN_DEBUG "%6s: packet dump (%s) follows:",dev->name,desc);
	for (i=0; i<256+(ext!=0)*256; i++)
	{
		if (i%16==0)
			printk("\n" KERN_DEBUG "[%04X] ",i);
		printk("%02X ",buffer[i]);
	}
	printk("\n");
	restore_flags(flags);
}
#endif

/****************************************************************************
 *                                                                          *
 * Probe and initialization                                                 *
 *                                                                          *
 ****************************************************************************/

#ifdef RIM_I_MODE

/* We cannot probe for a RIM I card; one reason is I don't know how to reset
 * them.  In fact, we can't even get their node ID automatically.  So, we
 * need to be passed a specific shmem address, IRQ, and node ID (stored in
 * dev->base_addr)
 */
__initfunc(int arcnet_probe(struct device *dev))
{
	BUGLVL(D_NORMAL) printk(version);
	BUGMSG(D_NORMAL,"Compiled for ARCnet RIM I (autoprobe disabled)\n");
	BUGMSG(D_NORMAL,"Given: node %02lXh, shmem %lXh, irq %d\n",
		dev->base_addr,dev->mem_start,dev->irq);

	if (dev->mem_start<=0 || dev->irq<=0)
	{
		BUGMSG(D_NORMAL,"No autoprobe for RIM I; you "
			"must specify the shmem and irq!\n");
		return -ENODEV;
	}

	if (dev->base_addr<=0 || dev->base_addr>255)
	{
		BUGMSG(D_NORMAL,"You need to specify your card's station "
			"ID!\n");
		return -ENODEV;
	}

	return arcnet_found(dev,dev->base_addr,dev->irq,dev->mem_start);
}

#else /* not RIM_I_MODE, so use a real autoprobe */

/* Check for an ARCnet network adaptor, and return '0' if one exists.
 *  If dev->base_addr == 0, probe all likely locations.
 *  If dev->base_addr == 1, always return failure.
 *  If dev->base_addr == 2, allocate space for the device and return success
 *  			    (detachable devices only).
 *
 * NOTE: the list of possible ports/shmems is static, so it is retained
 * across calls to arcnet_probe.  So, if more than one ARCnet probe is made,
 * values that were discarded once will not even be tried again.
 *
 * FIXME: grab all devices in one shot and eliminate the big static array.
 */

static int ports[(0x3f0 - 0x200) / 16 + 1] __initdata;
static u_long shmems[(0xFF800 - 0xA0000) / 2048 + 1] __initdata;

__initfunc(int arcnet_probe(struct device *dev))
{
	static int init_once = 0;
	static int numports=sizeof(ports)/sizeof(ports[0]),
	    	   numshmems=sizeof(shmems)/sizeof(shmems[0]);

	int count,status,delayval,ioaddr,numprint,airq,retval=-ENODEV,
		openparen=0;
	unsigned long airqmask;
	int *port;
	u_long *shmem;

	if (!init_once)
	{
		for (count=0x200; count<=0x3f0; count+=16)
			ports[(count-0x200)/16] = count;
		for (count=0xA0000; count<=0xFF800; count+=2048)
			shmems[(count-0xA0000)/2048] = count;
		init_once=1;
	}

	BUGLVL(D_NORMAL) printk(version);

	BUGMSG(D_DURING,"space used for probe buffers: %d+%d=%d bytes\n",
		sizeof(ports),sizeof(shmems),
		sizeof(ports)+sizeof(shmems));


#if 1
	BUGLVL(D_EXTRA)
	{
		printk("arcnet: ***\n");
		printk("arcnet: * Read arcnet.txt for important release notes!\n");
		printk("arcnet: *\n");
		printk("arcnet: * This is an ALPHA version!  (Last stable release: v2.56)  E-mail me if\n");
		printk("arcnet: * you have any questions, comments, or bug reports.\n");
		printk("arcnet: ***\n");
	}
#endif

	BUGMSG(D_INIT,"given: base %lXh, IRQ %d, shmem %lXh\n",
			dev->base_addr,dev->irq,dev->mem_start);

	if (dev->base_addr > 0x1ff)	/* Check a single specified port */
	{
		ports[0]=dev->base_addr;
		numports=1;
	}
	else if (dev->base_addr > 0)	/* Don't probe at all. */
		return -ENXIO;

	if (dev->mem_start)
	{
		shmems[0]=dev->mem_start;
		numshmems=1;
	}


	/* Stage 1: abandon any reserved ports, or ones with status==0xFF
	 * (empty), and reset any others by reading the reset port.
	 */
	BUGMSG(D_INIT,"Stage 1: ");
	numprint=0;
	for (port = &ports[0]; port-ports<numports; port++)
	{
		numprint++;
		if (numprint>8)
		{
			BUGMSG2(D_INIT,"\n");
			BUGMSG(D_INIT,"Stage 1: ");
			numprint=1;
		}
		BUGMSG2(D_INIT,"%Xh ",*port);

		ioaddr=*port;

		if (check_region(*port, ARCNET_TOTAL_SIZE))
		{
			BUGMSG2(D_INIT_REASONS,"(check_region)\n");
			BUGMSG(D_INIT_REASONS,"Stage 1: ");
			BUGLVL(D_INIT_REASONS) numprint=0;
			*port=ports[numports-1];
			numports--;
			port--;
			continue;
		}

		if (ARCSTATUS == 0xFF)
		{
			BUGMSG2(D_INIT_REASONS,"(empty)\n");
			BUGMSG(D_INIT_REASONS,"Stage 1: ");
			BUGLVL(D_INIT_REASONS) numprint=0;
			*port=ports[numports-1];
			numports--;
			port--;
			continue;
		}

		ARCRESET;	/* begin resetting card */

		BUGMSG2(D_INIT_REASONS,"\n");
		BUGMSG(D_INIT_REASONS,"Stage 1: ");
		BUGLVL(D_INIT_REASONS) numprint=0;
	}
	BUGMSG2(D_INIT,"\n");

	if (!numports)
	{
		BUGMSG(D_NORMAL,"Stage 1: No ARCnet cards found.\n");
		return -ENODEV;
	}


	/* Stage 2: we have now reset any possible ARCnet cards, so we can't
	 * do anything until they finish.  If D_INIT, print the list of
	 * cards that are left.
	 */
	BUGMSG(D_INIT,"Stage 2: ");
	numprint=0;
	for (port = &ports[0]; port-ports<numports; port++)
	{
		numprint++;
		if (numprint>8)
		{
			BUGMSG2(D_INIT,"\n");
			BUGMSG(D_INIT,"Stage 2: ");
			numprint=1;
		}
		BUGMSG2(D_INIT,"%Xh ",*port);
	}
	BUGMSG2(D_INIT,"\n");
	JIFFER(RESETtime);


	/* Stage 3: abandon any shmem addresses that don't have the signature
	 * 0xD1 byte in the right place, or are read-only.
	 */
	BUGMSG(D_INIT,"Stage 3: ");
	numprint=0;
	for (shmem = &shmems[0]; shmem-shmems<numshmems; shmem++)
	{
		u_long ptr;

		numprint++;
		if (numprint>8)
		{
			BUGMSG2(D_INIT,"\n");
			BUGMSG(D_INIT,"Stage 3: ");
			numprint=1;
		}
		BUGMSG2(D_INIT,"%lXh ",*shmem);

		ptr=(u_long)(*shmem);

		if (readb(ptr) != TESTvalue)
		{
			BUGMSG2(D_INIT_REASONS,"(mem=%02Xh, not %02Xh)\n",
				readb(ptr),TESTvalue);
			BUGMSG(D_INIT_REASONS,"Stage 3: ");
			BUGLVL(D_INIT_REASONS) numprint=0;
			*shmem=shmems[numshmems-1];
			numshmems--;
			shmem--;
			continue;
		}

		/* By writing 0x42 to the TESTvalue location, we also make
		 * sure no "mirror" shmem areas show up - if they occur
		 * in another pass through this loop, they will be discarded
		 * because *cptr != TESTvalue.
		 */
		writeb(0x42,ptr);
		if (readb(ptr) != 0x42)
		{
			BUGMSG2(D_INIT_REASONS,"(read only)\n");
			BUGMSG(D_INIT_REASONS,"Stage 3: ");
			*shmem=shmems[numshmems-1];
			numshmems--;
			shmem--;
			continue;
		}

		BUGMSG2(D_INIT_REASONS,"\n");
		BUGMSG(D_INIT_REASONS,"Stage 3: ");
		BUGLVL(D_INIT_REASONS) numprint=0;
	}
	BUGMSG2(D_INIT,"\n");

	if (!numshmems)
	{
		BUGMSG(D_NORMAL,"Stage 3: No ARCnet cards found.\n");
		return -ENODEV;
	}

	/* Stage 4: something of a dummy, to report the shmems that are
	 * still possible after stage 3.
	 */
	BUGMSG(D_INIT,"Stage 4: ");
	numprint=0;
	for (shmem = &shmems[0]; shmem-shmems<numshmems; shmem++)
	{
		numprint++;
		if (numprint>8)
		{
			BUGMSG2(D_INIT,"\n");
			BUGMSG(D_INIT,"Stage 4: ");
			numprint=1;
		}
		BUGMSG2(D_INIT,"%lXh ",*shmem);
	}
	BUGMSG2(D_INIT,"\n");


	/* Stage 5: for any ports that have the correct status, can disable
	 * the RESET flag, and (if no irq is given) generate an autoirq,
	 * register an ARCnet device.
	 *
	 * Currently, we can only register one device per probe, so quit
	 * after the first one is found.
	 */
	BUGMSG(D_INIT,"Stage 5: ");
	numprint=0;
	for (port = &ports[0]; port-ports<numports; port++)
	{
		numprint++;
		if (numprint>8)
		{
			BUGMSG2(D_INIT,"\n");
			BUGMSG(D_INIT,"Stage 5: ");
			numprint=1;
		}
		BUGMSG2(D_INIT,"%Xh ",*port);

		ioaddr=*port;
		status=ARCSTATUS;

		if ((status & 0x9D)
			!= (NORXflag|RECONflag|TXFREEflag|RESETflag))
		{
			BUGMSG2(D_INIT_REASONS,"(status=%Xh)\n",status);
			BUGMSG(D_INIT_REASONS,"Stage 5: ");
			BUGLVL(D_INIT_REASONS) numprint=0;
			*port=ports[numports-1];
			numports--;
			port--;
			continue;
		}

		ACOMMAND(CFLAGScmd|RESETclear|CONFIGclear);
		status=ARCSTATUS;
		if (status & RESETflag)
		{
			BUGMSG2(D_INIT_REASONS," (eternal reset, status=%Xh)\n",
					status);
			BUGMSG(D_INIT_REASONS,"Stage 5: ");
			BUGLVL(D_INIT_REASONS) numprint=0;
			*port=ports[numports-1];
			numports--;
			port--;
			continue;
		}

		/* skip this completely if an IRQ was given, because maybe
		 * we're on a machine that locks during autoirq!
		 */
		if (!dev->irq)
		{
			/* if we do this, we're sure to get an IRQ since the
			 * card has just reset and the NORXflag is on until
			 * we tell it to start receiving.
			 */
			airqmask = probe_irq_on();
			AINTMASK(NORXflag);
			udelay(1);
			AINTMASK(0);
			airq = probe_irq_off(airqmask);

			if (airq<=0)
			{
				BUGMSG2(D_INIT_REASONS,"(airq=%d)\n",airq);
				BUGMSG(D_INIT_REASONS,"Stage 5: ");
				BUGLVL(D_INIT_REASONS) numprint=0;
				*port=ports[numports-1];
				numports--;
				port--;
				continue;
			}
		}
		else
		{
			airq=dev->irq;
		}

		BUGMSG2(D_INIT,"(%d,", airq);
		openparen=1;

		/* Everything seems okay.  But which shmem, if any, puts
		 * back its signature byte when the card is reset?
		 *
		 * If there are multiple cards installed, there might be
		 * multiple shmems still in the list.
		 */
#ifdef FAST_PROBE
		if (numports>1 || numshmems>1)
		{
			ARCRESET;
			JIFFER(RESETtime);
		}
		else
		{
			/* just one shmem and port, assume they match */
			writeb(TESTvalue,shmems[0]);
		}
#else
		ARCRESET;
		JIFFER(RESETtime);
#endif


		for (shmem = &shmems[0]; shmem-shmems<numshmems; shmem++)
		{
			u_long ptr;
			ptr=(u_long)(*shmem);

			if (readb(ptr) == TESTvalue)	/* found one */
			{
				BUGMSG2(D_INIT,"%lXh)\n", *shmem);
				openparen=0;

				/* register the card */
				retval=arcnet_found(dev,*port,airq,*shmem);
				if (retval) openparen=0;

				/* remove shmem from the list */
				*shmem=shmems[numshmems-1];
				numshmems--;

				break;
			}
			else
			{
				BUGMSG2(D_INIT_REASONS,"%Xh-", readb(ptr));
			}
		}

		if (openparen)
		{
			BUGMSG2(D_INIT,"no matching shmem)\n");
			BUGMSG(D_INIT_REASONS,"Stage 5: ");
			BUGLVL(D_INIT_REASONS) numprint=0;
		}

		*port=ports[numports-1];
		numports--;
		port--;

		if (!retval) break;
	}
	BUGMSG(D_INIT_REASONS,"\n");

	/* Now put back TESTvalue on all leftover shmems.
	 */
	for (shmem = &shmems[0]; shmem-shmems<numshmems; shmem++)
		writeb(TESTvalue,*shmem);

	if (retval) BUGMSG(D_NORMAL,"Stage 5: No ARCnet cards found.\n");
	return retval;
}

#endif /* !RIM_I_MODE */


/* Set up the struct device associated with this card.  Called after
 * probing succeeds.
 */
__initfunc(int arcnet_found(struct device *dev,int port,int airq, u_long shmem))
{
	u_long first_mirror,last_mirror;
	struct arcnet_local *lp;
	int mirror_size;

	/* reserve the irq */
	if (request_irq(airq,&arcnet_interrupt,0,"arcnet",NULL))
	{
		BUGMSG(D_NORMAL,"Can't get IRQ %d!\n",airq);
		return -ENODEV;
	}
	irq2dev_map[airq]=dev;
	dev->irq=airq;

#ifdef RIM_I_MODE
	dev->base_addr=0;
	writeb(TESTvalue,shmem);
	writeb(port,shmem+1);	/* actually the node ID */
#else
	/* reserve the I/O region - guaranteed to work by check_region */
	request_region(port,ARCNET_TOTAL_SIZE,"arcnet");
	dev->base_addr=port;
#endif

	/* find the real shared memory start/end points, including mirrors */

	#define BUFFER_SIZE (512)
	#define MIRROR_SIZE (BUFFER_SIZE*4)

	/* guess the actual size of one "memory mirror" - the number of
	 * bytes between copies of the shared memory.  On most cards, it's
	 * 2k (or there are no mirrors at all) but on some, it's 4k.
	 */
	mirror_size=MIRROR_SIZE;
	if (readb(shmem)==TESTvalue
	    && readb(shmem-mirror_size)!=TESTvalue
	    && readb(shmem-2*mirror_size)==TESTvalue)
		mirror_size*=2;

	first_mirror=last_mirror=shmem;
	while (readb(first_mirror)==TESTvalue) first_mirror-=mirror_size;
	first_mirror+=mirror_size;

	while (readb(last_mirror)==TESTvalue) last_mirror+=mirror_size;
	last_mirror-=mirror_size;

	dev->mem_start=first_mirror;
	dev->mem_end=last_mirror+MIRROR_SIZE-1;
	dev->rmem_start=dev->mem_start+BUFFER_SIZE*0;
	dev->rmem_end=dev->mem_start+BUFFER_SIZE*2-1;

	/* Initialize the rest of the device structure. */

	dev->priv = kmalloc(sizeof(struct arcnet_local), GFP_KERNEL);
	if (dev->priv == NULL)
	{
		irq2dev_map[airq] = NULL;
		free_irq(airq,NULL);
		RELEASE_REGION(port,ARCNET_TOTAL_SIZE);
		return -ENOMEM;
	}
	memset(dev->priv,0,sizeof(struct arcnet_local));
	lp=(struct arcnet_local *)(dev->priv);

	dev->open=arcnet_open;
	dev->stop=arcnet_close;
	dev->hard_start_xmit=arcnetA_send_packet;
	dev->get_stats=arcnet_get_stats;
	/*dev->set_multicast_list = &set_multicast_list;*/

	/* Fill in the fields of the device structure with generic
	 * values.
	 */
	arcnet_setup(dev);

	/* And now fill particular fields with arcnet values */
	dev->mtu=1500; /* completely arbitrary - agrees with ether, though */
	dev->hard_header_len=sizeof(struct ClientData);
	dev->hard_header=arcnetA_header;
	dev->rebuild_header=arcnetA_rebuild_header;
	lp->sequence=1;
	lp->recbuf=0;

	BUGMSG(D_DURING,"ClientData header size is %d.\n",
		sizeof(struct ClientData));
	BUGMSG(D_DURING,"HardHeader size is %d.\n",
		sizeof(struct HardHeader));

	/* get and check the station ID from offset 1 in shmem */
	lp->stationid = readb(first_mirror+1);
	if (lp->stationid==0)
		BUGMSG(D_NORMAL,"WARNING!  Station address 00 is reserved "
			"for broadcasts!\n");
	else if (lp->stationid==255)
		BUGMSG(D_NORMAL,"WARNING!  Station address FF may confuse "
			"DOS networking programs!\n");
	dev->dev_addr[0]=lp->stationid;

	BUGMSG(D_NORMAL,"ARCnet station %02Xh found at %03lXh, IRQ %d, "
		"ShMem %lXh (%ld*%d bytes).\n",
		lp->stationid,
		dev->base_addr,dev->irq,dev->mem_start,
		(dev->mem_end-dev->mem_start+1)/mirror_size,mirror_size);

	return 0;
}


/* Do a hardware reset on the card, and set up necessary registers.
 *
 * This should be called as little as possible, because it disrupts the
 * token on the network (causes a RECON) and requires a significant delay.
 *
 * However, it does make sure the card is in a defined state.
 */
int arcnet_reset(struct device *dev,int reset_delay)
{
	struct arcnet_local *lp=(struct arcnet_local *)dev->priv;
	short ioaddr=IOADDR;
	int delayval,recbuf=lp->recbuf;

	/* no IRQ's, please! */
	lp->intmask=0;
	SETMASK;

	BUGMSG(D_INIT,"Resetting %s (status=%Xh)\n",
			dev->name,ARCSTATUS);

	if (reset_delay)
	{
		/* reset the card */
		ARCRESET;
		JIFFER(RESETtime);
	}

	ACOMMAND(CFLAGScmd|RESETclear); /* clear flags & end reset */
	ACOMMAND(CFLAGScmd|CONFIGclear);

	/* verify that the ARCnet signature byte is present */
	if (readb(dev->mem_start) != TESTvalue)
	{
		BUGMSG(D_NORMAL,"reset failed: TESTvalue not present.\n");
		return 1;
	}

	/* clear out status variables */
	recbuf=lp->recbuf=0;
	lp->txbuf=2;

	/* enable extended (512-byte) packets */
	ACOMMAND(CONFIGcmd|EXTconf);

#ifndef SLOW_XMIT_COPY
	/* clean out all the memory to make debugging make more sense :) */
	BUGLVL(D_DURING)
		memset_io(dev->mem_start,0x42,2048);
#endif

	/* and enable receive of our first packet to the first buffer */
	EnableReceiver();

	/* re-enable interrupts */
	lp->intmask|=NORXflag;
#ifdef DETECT_RECONFIGS
	lp->intmask|=RECONflag;
#endif
	SETMASK;

	/* done!  return success. */
	return 0;
}


/* Setup a struct device for ARCnet.  This should really be in net_init.c
 * but since there are three different ARCnet devices ANYWAY... <gargle>
 *
 * Actually, the whole idea of having all this kernel-dependent stuff (ie.
 * "new-style flags") setup per-net-device is kind of weird anyway.
 *
 * Intelligent defaults?!  Nah.
 */

void arcnet_setup(struct device *dev)
{
	dev_init_buffers(dev);

	dev->broadcast[0]	= 0x00;	/* for us, broadcasts are address 0 */
	dev->addr_len		= 1;
	dev->type		= ARPHRD_ARCNET;
	dev->tx_queue_len	= 30;	/* fairly long queue - arcnet is
					 * quite speedy.
					 */

	/* New-style flags. */
	dev->flags		= IFF_BROADCAST;
	dev->family		= AF_INET;
	dev->pa_addr		= 0;
	dev->pa_brdaddr 	= 0;
	dev->pa_mask		= 0;
	dev->pa_alen		= 4;
}


/****************************************************************************
 *                                                                          *
 * Open and close the driver                                                *
 *                                                                          *
 ****************************************************************************/


/* Open/initialize the board.  This is called sometime after booting when
 * the 'ifconfig' program is run.
 *
 * This routine should set everything up anew at each open, even
 * registers that "should" only need to be set once at boot, so that
 * there is non-reboot way to recover if something goes wrong.
 */
static int
arcnet_open(struct device *dev)
{
	struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
	int ioaddr=IOADDR;

	if (dev->metric>=1000)
	{
		arcnet_debug=dev->metric-1000;
		printk(KERN_INFO "%6s: debug level set to %d\n",dev->name,arcnet_debug);
		dev->metric=1;
	}

	BUGMSG(D_INIT,"arcnet_open: resetting card.\n");

#ifdef FAST_IFCONFIG
	/* try to put the card in a defined state - if it fails the first
	 * time, actually reset it.
	 */
	if (arcnet_reset(dev,0) && arcnet_reset(dev,1))
		return -ENODEV;
#else
	/* reset the card twice in case something goes wrong the first time.
	 */
	if (arcnet_reset(dev,1) && arcnet_reset(dev,1))
		return -ENODEV;
#endif

	dev->tbusy=0;
	dev->interrupt=0;
	lp->intx=0;
	lp->in_txhandler=0;

	/* The RFC1201 driver is the default - just store */
	lp->adev=dev;
	BUGMSG(D_NORMAL,"ARCnet RFC1201 protocol initialized.\n");

#ifdef CONFIG_ARCNET_ETH
	/* Initialize the ethernet-encap protocol driver */
	lp->edev=(struct device *)kmalloc(sizeof(struct device),GFP_KERNEL);
	if (lp->edev == NULL)
		return -ENOMEM;
	memcpy(lp->edev,dev,sizeof(struct device));
	lp->edev->name=(char *)kmalloc(10,GFP_KERNEL);
	if (lp->edev->name == NULL) {
		kfree(lp->edev);
		lp->edev = NULL;
		return -ENOMEM;
	}
	sprintf(lp->edev->name,"%se",dev->name);
	lp->edev->init=arcnetE_init;
	register_netdev(lp->edev);
#else
	BUGMSG(D_NORMAL,"Ethernet-Encap protocol not available (disabled).\n");
#endif

#ifdef CONFIG_ARCNET_1051
	/* Initialize the RFC1051-encap protocol driver */
	lp->sdev=(struct device *)kmalloc(sizeof(struct device),GFP_KERNEL);
	memcpy(lp->sdev,dev,sizeof(struct device));
	lp->sdev->name=(char *)kmalloc(10,GFP_KERNEL);
	sprintf(lp->sdev->name,"%ss",dev->name);
	lp->sdev->init=arcnetS_init;
	register_netdev(lp->sdev);
#else
	BUGMSG(D_NORMAL,"RFC1051 protocol not available (disabled).\n");
#endif

	/* we're started */
	START=1;

	/* make sure we're ready to receive IRQ's.
	 * arcnet_reset sets this for us, but if we receive one before
	 * START is set to 1, it could be ignored.  So, we turn IRQ's
	 * off, then on again to clean out the IRQ controller.
	 */
	AINTMASK(0);
	udelay(1);	/* give it time to set the mask before
			 * we reset it again. (may not even be
			 * necessary)
			 */
	SETMASK;

	MOD_INC_USE_COUNT;
	return 0;
}


/* The inverse routine to arcnet_open - shuts down the card.
 */
static int
arcnet_close(struct device *dev)
{
	int ioaddr=IOADDR;
	struct arcnet_local *lp = (struct arcnet_local *)dev->priv;

	TBUSY=1;
	START=0;

	/* Shut down the card */
#ifdef FAST_IFCONFIG
	ARCRESET;	/* reset IRQ won't run if START=0 */
#else
	lp->intmask=0;
	SETMASK;	/* no IRQ's (except RESET, of course) */
	ACOMMAND(NOTXcmd);	/* stop transmit */
	ACOMMAND(NORXcmd);	/* disable receive */
#endif

	/* reset more flags */
	INTERRUPT=0;

	/* do NOT free lp->adev!!  It's static! */
	lp->adev=NULL;

#ifdef CONFIG_ARCNET_ETH
	/* free the ethernet-encap protocol device */
	lp->edev->priv=NULL;
	dev_close(lp->edev);
	unregister_netdev(lp->edev);
	kfree(lp->edev->name);
	kfree(lp->edev);
	lp->edev=NULL;
#endif

#ifdef CONFIG_ARCNET_1051
	/* free the RFC1051-encap protocol device */
	lp->sdev->priv=NULL;
	dev_close(lp->sdev);
	unregister_netdev(lp->sdev);
	kfree(lp->sdev->name);
	kfree(lp->sdev);
	lp->sdev=NULL;
#endif

	/* Update the statistics here. (not necessary in ARCnet) */

	MOD_DEC_USE_COUNT;
	return 0;
}



/****************************************************************************
 *                                                                          *
 * Transmitter routines                                                     *
 *                                                                          *
 ****************************************************************************/

/* Generic error checking routine for arcnet??_send_packet
 */
static int
arcnet_send_packet_bad(struct sk_buff *skb, struct device *dev)
{
	struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
	int ioaddr=IOADDR;

	BUGMSG(D_DURING,"transmit requested (status=%Xh, inTX=%d)\n",
			ARCSTATUS,lp->intx);

	if (lp->in_txhandler)
	{
		BUGMSG(D_NORMAL,"send_packet called while in txhandler!\n");
		lp->stats.tx_dropped++;
		return 1;
	}

	if (lp->intx>1)
	{
		BUGMSG(D_NORMAL,"send_packet called while intx!\n");
		lp->stats.tx_dropped++;
		return 1;
	}

	if (IF_TBUSY)
	{
		/* If we get here, some higher level has decided we are broken.
		   There should really be a "kick me" function call instead. */
		int tickssofar = jiffies - dev->trans_start;
		/*int recbuf=lp->recbuf;*/
		int status=ARCSTATUS;

		if (tickssofar < TX_TIMEOUT)
		{
			BUGMSG(D_DURING,"premature kickme! (status=%Xh ticks=%d o.skb=%ph numsegs=%d segnum=%d\n",
					status,tickssofar,lp->outgoing.skb,
					lp->outgoing.numsegs,
					lp->outgoing.segnum);
			return 1;
		}

		lp->intmask &= ~TXFREEflag;
		SETMASK;

		if (status&TXFREEflag)	/* transmit _DID_ finish */
		{
			BUGMSG(D_NORMAL,"tx timeout - missed IRQ? (status=%Xh, ticks=%d, mask=%Xh, dest=%02Xh)\n",
					status,tickssofar,lp->intmask,lp->lasttrans_dest);
			lp->stats.tx_errors++;
		}
		else
		{
			BUGMSG(D_EXTRA,"tx timed out (status=%Xh, tickssofar=%d, intmask=%Xh, dest=%02Xh)\n",
					status,tickssofar,lp->intmask,lp->lasttrans_dest);
			lp->stats.tx_errors++;
			lp->stats.tx_aborted_errors++;

			ACOMMAND(NOTXcmd);
		}

		if (lp->outgoing.skb)
		{
			dev_kfree_skb(lp->outgoing.skb,FREE_WRITE);
			lp->stats.tx_dropped++;
		}
		lp->outgoing.skb=NULL;

		TBUSY=0;
		lp->txready=0;
		lp->sending=0;

		return 1;
	}

	if (lp->txready)	/* transmit already in progress! */
	{
		BUGMSG(D_NORMAL,"trying to start new packet while busy! (status=%Xh)\n",
			ARCSTATUS);
		lp->intmask &= ~TXFREEflag;
		SETMASK;
		ACOMMAND(NOTXcmd); /* abort current send */
		arcnet_inthandler(dev); /* fake an interrupt */
		lp->stats.tx_errors++;
		lp->stats.tx_fifo_errors++;
		lp->txready=0;	/* we definitely need this line! */

		return 1;
	}

	/* Block a timer-based transmit from overlapping.  This could better be
	   done with atomic_swap(1, dev->tbusy), but set_bit() works as well. */
        if (set_bit(0, (void*)&dev->tbusy) != 0)
        {
            BUGMSG(D_NORMAL,"transmitter called with busy bit set! (status=%Xh, inTX=%d, tickssofar=%ld)\n",
            		ARCSTATUS,lp->intx,jiffies-dev->trans_start);
            lp->stats.tx_errors++;
            lp->stats.tx_fifo_errors++;
            return -EBUSY;
        }

	return 0;
}


/* Called by the kernel in order to transmit a packet.
 */
static int
arcnetA_send_packet(struct sk_buff *skb, struct device *dev)
{
	struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
	int ioaddr=IOADDR,bad;
	struct Outgoing *out=&(lp->outgoing);

	lp->intx++;

	bad=arcnet_send_packet_bad(skb,dev);
	if (bad)
	{
		lp->intx--;
		return bad;
	}

	TBUSY=1;

	out->length = 1 < skb->len ? skb->len : 1;
	out->hdr=(struct ClientData*)skb->data;
	out->skb=skb;

	BUGLVL(D_SKB) arcnet_dump_skb(dev,skb,"tx");

	out->hdr->sequence=(lp->sequence++);

	/* fits in one packet? */
	if (out->length-EXTRA_CLIENTDATA<=XMTU)
	{
		BUGMSG(D_DURING,"not splitting %d-byte packet. (split_flag=%d)\n",
				out->length,out->hdr->split_flag);
		if (out->hdr->split_flag)
			BUGMSG(D_NORMAL,"short packet has split_flag set?! (split_flag=%d)\n",
				out->hdr->split_flag);
		out->numsegs=1;
		out->segnum=1;
		arcnetAS_prepare_tx(dev,
			((char *)out->hdr)+EXTRA_CLIENTDATA,
			sizeof(struct ClientData)-EXTRA_CLIENTDATA,
			((char *)skb->data)+sizeof(struct ClientData),
			out->length-sizeof(struct ClientData),
			out->hdr->daddr,1);

		/* done right away */
		dev_kfree_skb(out->skb,FREE_WRITE);
		out->skb=NULL;

		if (arcnet_go_tx(dev,1))
		{
			/* inform upper layers */
			TBUSY=0;
			mark_bh(NET_BH);
		}
	}
	else			/* too big for one - split it */
	{
		int maxsegsize=XMTU-4;

		out->data=(u_char *)skb->data
				+ sizeof(struct ClientData);
		out->dataleft=out->length-sizeof(struct ClientData);
		out->numsegs=(out->dataleft+maxsegsize-1)/maxsegsize;
		out->segnum=0;

		BUGMSG(D_TX,"packet (%d bytes) split into %d fragments:\n",
			out->length,out->numsegs);

		/* if a packet waiting, launch it */
		arcnet_go_tx(dev,1);

		if (!lp->txready)
		{
			/* prepare a packet, launch it and prepare
			 * another.
			 */
			arcnetA_continue_tx(dev);
			if (arcnet_go_tx(dev,1))
			{
				arcnetA_continue_tx(dev);
				arcnet_go_tx(dev,1);
			}
		}

		/* if segnum==numsegs, the transmission is finished;
		 * free the skb right away.
		 */
		if (out->segnum==out->numsegs)
		{
			/* transmit completed */
			out->segnum++;
			if (out->skb)
				dev_kfree_skb(out->skb,FREE_WRITE);
			out->skb=NULL;
		}
	}

	dev->trans_start=jiffies;
	lp->intx--;

	/* make sure we didn't ignore a TX IRQ while we were in here */
	lp->intmask |= TXFREEflag;
	SETMASK;

	return 0;
}


/* After an RFC1201 split packet has been set up, this function calls
 * arcnetAS_prepare_tx to load the next segment into the card.  This function
 * does NOT automatically call arcnet_go_tx.
 */
static void arcnetA_continue_tx(struct device *dev)
{
	struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
	int ioaddr=IOADDR,maxsegsize=XMTU-4;
	struct Outgoing *out=&(lp->outgoing);

	BUGMSG(D_DURING,"continue_tx called (status=%Xh, intx=%d, intxh=%d, intmask=%Xh\n",
		ARCSTATUS,lp->intx,lp->in_txhandler,lp->intmask);

	if (lp->txready)
	{
		BUGMSG(D_NORMAL,"continue_tx: called with packet in buffer!\n");
		return;
	}

	if (out->segnum>=out->numsegs)
	{
		BUGMSG(D_NORMAL,"continue_tx: building segment %d of %d!\n",
			out->segnum+1,out->numsegs);
	}

	if (!out->segnum)	/* first packet */
		out->hdr->split_flag=((out->numsegs-2)<<1)+1;
	else
		out->hdr->split_flag=out->segnum<<1;

	out->seglen=maxsegsize;
	if (out->seglen>out->dataleft) out->seglen=out->dataleft;

	BUGMSG(D_TX,"building packet #%d (%d bytes) of %d (%d total), splitflag=%d\n",
		out->segnum+1,out->seglen,out->numsegs,
		out->length,out->hdr->split_flag);

	arcnetAS_prepare_tx(dev,((char *)out->hdr)+EXTRA_CLIENTDATA,
		sizeof(struct ClientData)-EXTRA_CLIENTDATA,
		out->data,out->seglen,out->hdr->daddr,1);

	out->dataleft-=out->seglen;
	out->data+=out->seglen;
	out->segnum++;
}


/* Given an skb, copy a packet into the ARCnet buffers for later transmission
 * by arcnet_go_tx.
 */
static void
arcnetAS_prepare_tx(struct device *dev,u_char *hdr,int hdrlen,
		char *data,int length,int daddr,int exceptA)
{
	struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
	struct ClientData *arcsoft;
	union ArcPacket *arcpacket =
		(union ArcPacket *)phys_to_virt(dev->mem_start+512*(lp->txbuf^1));
	int offset;

#ifdef SLOW_XMIT_COPY
	char *iptr,*iend,*optr;
#endif

	lp->txbuf=lp->txbuf^1;	/* XOR with 1 to alternate between 2 and 3 */

	length+=hdrlen;

	BUGMSG(D_TX,"arcnetAS_prep_tx: hdr:%ph, length:%d, data:%ph\n",
			hdr,length,data);

#ifndef SLOW_XMIT_COPY
	/* clean out the page to make debugging make more sense :) */
	BUGLVL(D_DURING)
		memset_io(dev->mem_start+lp->txbuf*512,0x42,512);
#endif

	arcpacket->hardheader.destination=daddr;

	/* load packet into shared memory */
	if (length<=MTU)	/* Normal (256-byte) Packet */
	{
		arcpacket->hardheader.offset1=offset=256-length;
		arcsoft=(struct ClientData *)
			(&arcpacket->raw[offset]);
	}
	else if (length>=MinTU)	/* Extended (512-byte) Packet */
	{
		arcpacket->hardheader.offset1=0;
		arcpacket->hardheader.offset2=offset=512-length;

		arcsoft=(struct ClientData *)
			(&arcpacket->raw[offset]);
	}
	else if (exceptA)		/* RFC1201 Exception Packet */
	{
		arcpacket->hardheader.offset1=0;
		arcpacket->hardheader.offset2=offset=512-length-4;
		arcsoft=(struct ClientData *)
			(&arcpacket->raw[offset+4]);

		/* exception-specific stuff - these four bytes
		 * make the packet long enough to fit in a 512-byte
		 * frame.
		 */
		arcpacket->raw[offset+0]=hdr[0];
		arcpacket->raw[offset+1]=0xFF; /* FF flag */
			arcpacket->raw[offset+2]=0xFF; /* FF padding */
			arcpacket->raw[offset+3]=0xFF; /* FF padding */
	}
	else				/* "other" Exception packet */
	{
		/* RFC1051 - set 4 trailing bytes to 0 */
		memset(&arcpacket->raw[508],0,4);

		/* now round up to MinTU */
		arcpacket->hardheader.offset1=0;
		arcpacket->hardheader.offset2=offset=512-MinTU;
		arcsoft=(struct ClientData *)(&arcpacket->raw[offset]);
	}


	/* copy the packet into ARCnet shmem
	 *  - the first bytes of ClientData header are skipped
	 */
	memcpy((u_char*)arcsoft,
		(u_char*)hdr,hdrlen);
#ifdef SLOW_XMIT_COPY
	for (iptr=data,iend=iptr+length-hdrlen,optr=(char *)arcsoft+hdrlen;
		iptr<iend; iptr++,optr++)
	{
		*optr=*iptr;
		/*udelay(5);*/
	}
#else
	memcpy((u_char*)arcsoft+hdrlen,
		data,length-hdrlen);
#endif

	BUGMSG(D_DURING,"transmitting packet to station %02Xh (%d bytes)\n",
			daddr,length);

	BUGLVL(D_TX) arcnet_dump_packet(dev,arcpacket->raw,length>MTU,"tx");
	lp->lastload_dest=daddr;
	lp->txready=lp->txbuf;	/* packet is ready for sending */
}

/* Actually start transmitting a packet that was placed in the card's
 * buffer by arcnetAS_prepare_tx.  Returns 1 if a Tx is really started.
 *
 * This should probably always be called with the INTMASK register set to 0,
 * so go_tx is not called recursively.
 *
 * The enable_irq flag determines whether to actually write INTMASK value
 * to the card; TXFREEflag is always OR'ed into the memory variable either
 * way.
 */
static int
arcnet_go_tx(struct device *dev,int enable_irq)
{
	struct arcnet_local *lp=(struct arcnet_local *)dev->priv;
	int ioaddr=IOADDR;

	BUGMSG(D_DURING,"go_tx: status=%Xh, intmask=%Xh, txready=%d, sending=%d\n",
		ARCSTATUS,lp->intmask,lp->txready,lp->sending);

	if (lp->sending || !lp->txready)
	{
		if (enable_irq && lp->sending)
		{
			lp->intmask |= TXFREEflag;
			SETMASK;
		}
		return 0;
	}

	/* start sending */
	ACOMMAND(TXcmd|(lp->txready<<3));

	lp->stats.tx_packets++;
	lp->txready=0;
	lp->sending++;

	lp->lasttrans_dest=lp->lastload_dest;
	lp->lastload_dest=0;

	lp->intmask |= TXFREEflag;

	if (enable_irq)	SETMASK;

	return 1;
}


/****************************************************************************
 *                                                                          *
 * Interrupt handler                                                        *
 *                                                                          *
 ****************************************************************************/


/* The typical workload of the driver:  Handle the network interface
 * interrupts.  This doesn't do much right now except call arcnet_inthandler,
 * which takes different parameters but may be called from other places
 * as well.
 */
static void
arcnet_interrupt(int irq,void *dev_id,struct pt_regs *regs)
{
	struct device *dev = (struct device *)(irq2dev_map[irq]);
	int ioaddr;

	if (dev==NULL)
	{
		BUGLVL(D_DURING)
			printk(KERN_DEBUG "arcnet: irq %d for unknown device.\n", irq);
		return;
	}

	BUGMSG(D_DURING,"in arcnet_interrupt\n");

	/* RESET flag was enabled - if !dev->start, we must clear it right
	 * away (but nothing else) since inthandler() is never called.
	 */
	ioaddr=IOADDR;	/* can't do this before checking dev!=NULL */
	if (!dev->start)
	{
		if (ARCSTATUS & RESETflag)
			ACOMMAND(CFLAGScmd|RESETclear);
		return;
	}

	/* Call the "real" interrupt handler. */
	arcnet_inthandler(dev);
}


/* The actual interrupt handler routine - handle various IRQ's generated
 * by the card.
 */
static void
arcnet_inthandler(struct device *dev)
{
	struct arcnet_local *lp=(struct arcnet_local *)dev->priv;
	int ioaddr=IOADDR, status, boguscount = 3, didsomething;

	if (IF_INTERRUPT)
	{
		BUGMSG(D_NORMAL,"DRIVER PROBLEM!  Nested arcnet interrupts!\n");
		return;	/* don't even try. */
	}

	AINTMASK(0);
	INTERRUPT = 1;

	BUGMSG(D_DURING,"in arcnet_inthandler (status=%Xh, intmask=%Xh)\n",
		ARCSTATUS,lp->intmask);

	do
	{
		status = ARCSTATUS;
		didsomething=0;


		/* RESET flag was enabled - card is resetting and if RX
		 * is disabled, it's NOT because we just got a packet.
		 */
		if (status & RESETflag)
		{
			BUGMSG(D_NORMAL,"spurious reset (status=%Xh)\n",
					status);
			arcnet_reset(dev,0);

			/* all other flag values are just garbage */
			break;
		}


		/* RX is inhibited - we must have received something. */
		if (status & lp->intmask & NORXflag)
		{
			int recbuf=lp->recbuf=!lp->recbuf;

			BUGMSG(D_DURING,"receive irq (status=%Xh)\n",
					status);

			/* enable receive of our next packet */
			EnableReceiver();

			/* Got a packet. */
			arcnet_rx(dev,!recbuf);
			didsomething++;
		}

		/* it can only be an xmit-done irq if we're xmitting :) */
		/*if (status&TXFREEflag && !lp->in_txhandler && lp->sending)*/
		if (status & lp->intmask & TXFREEflag)
		{
			struct Outgoing *out=&(lp->outgoing);
			int was_sending=lp->sending;

			lp->intmask &= ~TXFREEflag;

			lp->in_txhandler++;
			if (was_sending) lp->sending--;

			BUGMSG(D_DURING,"TX IRQ (stat=%Xh, numsegs=%d, segnum=%d, skb=%ph)\n",
					status,out->numsegs,out->segnum,out->skb);

			if (was_sending && !(status&TXACKflag))
			{
				if (lp->lasttrans_dest != 0)
				{
					BUGMSG(D_EXTRA,"transmit was not acknowledged! (status=%Xh, dest=%02Xh)\n",
						status,lp->lasttrans_dest);
					lp->stats.tx_errors++;
					lp->stats.tx_carrier_errors++;
				}
				else
				{
					BUGMSG(D_DURING,"broadcast was not acknowledged; that's normal (status=%Xh, dest=%02Xh)\n",
							status,
							lp->lasttrans_dest);
				}
			}

			/* send packet if there is one */
			arcnet_go_tx(dev,0);
			didsomething++;

			if (lp->intx)
			{
				BUGMSG(D_DURING,"TXDONE while intx! (status=%Xh, intx=%d)\n",
					ARCSTATUS,lp->intx);
				lp->in_txhandler--;
				continue;
			}

			if (!lp->outgoing.skb)
			{
				BUGMSG(D_DURING,"TX IRQ done: no split to continue.\n");

				/* inform upper layers */
				if (!lp->txready && IF_TBUSY)
				{
					TBUSY=0;
					mark_bh(NET_BH);
				}
				lp->in_txhandler--;
				continue;
			}

			/* if more than one segment, and not all segments
			 * are done, then continue xmit.
			 */
			if (out->segnum<out->numsegs)
				arcnetA_continue_tx(dev);
			arcnet_go_tx(dev,0);

			/* if segnum==numsegs, the transmission is finished;
			 * free the skb.
			 */
			if (out->segnum>=out->numsegs)
			{
				/* transmit completed */
				out->segnum++;
				if (out->skb)
					dev_kfree_skb(out->skb,FREE_WRITE);
				out->skb=NULL;

				/* inform upper layers */
				if (!lp->txready && IF_TBUSY)
				{
					TBUSY=0;
					mark_bh(NET_BH);
				}
			}
			didsomething++;

			lp->in_txhandler--;
		}
		else if (lp->txready && !lp->sending && !lp->intx)
		{
			BUGMSG(D_NORMAL,"recovery from silent TX (status=%Xh)\n",
						status);
			arcnet_go_tx(dev,0);
			didsomething++;
		}

#ifdef DETECT_RECONFIGS
		if (status & (lp->intmask) & RECONflag)
		{
			ACOMMAND(CFLAGScmd|CONFIGclear);
			lp->stats.tx_carrier_errors++;

			#ifdef SHOW_RECONFIGS
			BUGMSG(D_NORMAL,"Network reconfiguration detected (status=%Xh)\n",
					status);
			#endif /* SHOW_RECONFIGS */

			#ifdef RECON_THRESHOLD
			/* is the RECON info empty or old? */
			if (!lp->first_recon || !lp->last_recon ||
				jiffies-lp->last_recon > HZ*10)
			{
				if (lp->network_down)
					BUGMSG(D_NORMAL,"reconfiguration detected: cabling restored?\n");
				lp->first_recon=lp->last_recon=jiffies;
				lp->num_recons=lp->network_down=0;

				BUGMSG(D_DURING,"recon: clearing counters.\n");
			}
			else /* add to current RECON counter */
			{
				lp->last_recon=jiffies;
				lp->num_recons++;

				BUGMSG(D_DURING,"recon: counter=%d, time=%lds, net=%d\n",
					lp->num_recons,
					(lp->last_recon-lp->first_recon)/HZ,
					lp->network_down);

				/* if network is marked up;
				 * and first_recon and last_recon are 60+ sec
				 *   apart;
				 * and the average no. of recons counted is
				 *   > RECON_THRESHOLD/min;
				 * then print a warning message.
				 */
				if (!lp->network_down
				    && (lp->last_recon-lp->first_recon)<=HZ*60
				    && lp->num_recons >= RECON_THRESHOLD)
				{
					lp->network_down=1;
					BUGMSG(D_NORMAL,"many reconfigurations detected: cabling problem?\n");
				}
				else if (!lp->network_down
				    && lp->last_recon-lp->first_recon > HZ*60)
				{
					/* reset counters if we've gone for
					 * over a minute.
					 */
					lp->first_recon=lp->last_recon;
					lp->num_recons=1;
				}
			}
			#endif
		}
		#ifdef RECON_THRESHOLD
		else if (lp->network_down && jiffies-lp->last_recon > HZ*10)
		{
			if (lp->network_down)
				BUGMSG(D_NORMAL,"cabling restored?\n");
			lp->first_recon=lp->last_recon=0;
			lp->num_recons=lp->network_down=0;

			BUGMSG(D_DURING,"not recon: clearing counters anyway.\n");
		}
		#endif
#endif /* DETECT_RECONFIGS */
	} while (--boguscount && didsomething);

	BUGMSG(D_DURING,"net_interrupt complete (status=%Xh, count=%d)\n",
			ARCSTATUS,boguscount);
	BUGMSG(D_DURING,"\n");

	SETMASK;	/* put back interrupt mask */

	INTERRUPT=0;
}


/****************************************************************************
 *                                                                          *
 * Receiver routines                                                        *
 *                                                                          *
 ****************************************************************************/


/* A packet has arrived; grab it from the buffers and possibly unsplit it.
 * This is a generic packet receiver that calls arcnet??_rx depending on the
 * protocol ID found.
 */
static void
arcnet_rx(struct device *dev,int recbuf)
{
	struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
	int ioaddr=IOADDR;
	union ArcPacket *arcpacket=
		(union ArcPacket *)phys_to_virt(dev->mem_start+recbuf*512);
	u_char *arcsoft;
	short length,offset;
	u_char daddr,saddr;

	lp->stats.rx_packets++;

	saddr=arcpacket->hardheader.source;
	daddr=arcpacket->hardheader.destination;

	/* if source is 0, it's a "used" packet! */
	if (saddr==0)
	{
		BUGMSG(D_NORMAL,"discarding old packet. (status=%Xh)\n",
			ARCSTATUS);
		lp->stats.rx_errors++;
		return;
	}
	arcpacket->hardheader.source=0;

	if (arcpacket->hardheader.offset1) /* Normal Packet */
	{
		offset=arcpacket->hardheader.offset1;
		arcsoft=&arcpacket->raw[offset];
		length=256-offset;
	}
	else		/* ExtendedPacket or ExceptionPacket */
	{
		offset=arcpacket->hardheader.offset2;
		arcsoft=&arcpacket->raw[offset];

		length=512-offset;
	}

       	BUGMSG(D_DURING,"received packet from %02Xh to %02Xh (%d bytes)\n",
       			saddr,daddr,length);

	/* call the right receiver for the protocol */
	switch (arcsoft[0])
	{
	case ARC_P_IP:
	case ARC_P_ARP:
	case ARC_P_RARP:
	case ARC_P_IPX:
	case ARC_P_NOVELL_EC:
		arcnetA_rx(lp->adev,arcsoft,length,saddr,daddr);
		break;
#ifdef CONFIG_ARCNET_ETH
	case ARC_P_ETHER:
		arcnetE_rx(lp->edev,arcsoft,length,saddr,daddr);
		break;
#endif
#ifdef CONFIG_ARCNET_1051
	case ARC_P_IP_RFC1051:
	case ARC_P_ARP_RFC1051:
		arcnetS_rx(lp->sdev,arcsoft,length,saddr,daddr);
		break;
#endif
	case ARC_P_DATAPOINT_BOOT:
	case ARC_P_DATAPOINT_MOUNT:
		break;
	case ARC_P_POWERLAN_BEACON:
	case ARC_P_POWERLAN_BEACON2:
		break;
	case ARC_P_LANSOFT: /* don't understand.  fall through. */
	default:
		BUGMSG(D_EXTRA,"received unknown protocol %d (%Xh) from station %d.\n",
			arcsoft[0],arcsoft[0],saddr);
		lp->stats.rx_errors++;
		lp->stats.rx_crc_errors++;
		break;
	}

        BUGLVL(D_RX) arcnet_dump_packet(dev,arcpacket->raw,length>240,"rx");


#ifndef SLOW_XMIT_COPY
	/* clean out the page to make debugging make more sense :) */
	BUGLVL(D_DURING)
		memset((void *)arcpacket->raw,0x42,512);
#endif


	/* If any worth-while packets have been received, a mark_bh(NET_BH)
	 * has been done by netif_rx and Linux will handle them after we
	 * return.
	 */
}


/* Packet receiver for "standard" RFC1201-style packets
 */
static void
arcnetA_rx(struct device *dev,u_char *buf,
	int length,u_char saddr, u_char daddr)
{
	struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
	struct sk_buff *skb;
	struct ClientData *arcsoft,*soft;

	BUGMSG(D_DURING,"it's an RFC1201 packet (length=%d)\n",
			length);

	/* compensate for EXTRA_CLIENTDATA (which isn't actually in the
	 * packet)
	 */
	arcsoft=(struct ClientData *)(buf-EXTRA_CLIENTDATA);
	length+=EXTRA_CLIENTDATA;

	if (arcsoft->split_flag==0xFF)  /* Exception Packet */
	{
		BUGMSG(D_DURING,"compensating for exception packet\n");

		/* skip over 4-byte junkola */
		arcsoft=(struct ClientData *)
			((u_char *)arcsoft + 4);
		length-=4;
	}

	if (!arcsoft->split_flag)		/* not split */
	{
		struct Incoming *in=&lp->incoming[saddr];

		BUGMSG(D_RX,"incoming is not split (splitflag=%d)\n",
			arcsoft->split_flag);

		if (in->skb)	/* already assembling one! */
        	{
        		BUGMSG(D_EXTRA,"aborting assembly (seq=%d) for unsplit packet (splitflag=%d, seq=%d)\n",
        			in->sequence,arcsoft->split_flag,
        			arcsoft->sequence);
        		kfree_skb(in->skb,FREE_WRITE);
			lp->stats.rx_errors++;
        		lp->stats.rx_missed_errors++;
        		in->skb=NULL;
        	}

        	in->sequence=arcsoft->sequence;

         	skb = alloc_skb(length, GFP_ATOMIC);
         	if (skb == NULL) {
         		BUGMSG(D_NORMAL,"Memory squeeze, dropping packet.\n");
         		lp->stats.rx_dropped++;
         		return;
         	}
         	soft=(struct ClientData *)skb->data;

         	skb->len = length;
         	skb->dev = dev;

         	memcpy((u_char *)soft+EXTRA_CLIENTDATA,
         		(u_char *)arcsoft+EXTRA_CLIENTDATA,
         		length-EXTRA_CLIENTDATA);
         	soft->daddr=daddr;
         	soft->saddr=saddr;

         	/* ARP packets have problems when sent from DOS.
         	 * source address is always 0 on some systems!  So we take
         	 * the hardware source addr (which is impossible to fumble)
         	 * and insert it ourselves.
         	 */
         	if (soft->protocol_id == ARC_P_ARP)
         	{
         		struct arphdr *arp=(struct arphdr *)
         				((char *)soft+sizeof(struct ClientData));

         		/* make sure addresses are the right length */
         		if (arp->ar_hln==1 && arp->ar_pln==4)
         		{
         			char *cptr=(char *)(arp)+sizeof(struct arphdr);

         			if (!*cptr)	/* is saddr = 00? */
         			{
         				BUGMSG(D_EXTRA,"ARP source address was 00h, set to %02Xh.\n",
         						saddr);
         				lp->stats.rx_crc_errors++;
         				*cptr=saddr;
         			}
         			else
         			{
         				BUGMSG(D_DURING,"ARP source address (%Xh) is fine.\n",
         					*cptr);
         			}
         		}
         		else
         		{
         			BUGMSG(D_NORMAL,"funny-shaped ARP packet. (%Xh, %Xh)\n",
         				arp->ar_hln,arp->ar_pln);
         			lp->stats.rx_errors++;
         			lp->stats.rx_crc_errors++;
         		}
         	}

		BUGLVL(D_SKB) arcnet_dump_skb(dev,skb,"rx");

		skb->protocol=arcnetA_type_trans(skb,dev);

         	netif_rx(skb);
         }
         else			/* split packet */
         {
	         /* NOTE:  MSDOS ARP packet correction should only need to
	          * apply to unsplit packets, since ARP packets are so short.
	          *
	          * My interpretation of the RFC1201 (ARCnet) document is that
	          * if a packet is received out of order, the entire assembly
	          * process should be aborted.
	          *
	          * The RFC also mentions "it is possible for successfully
	          * received packets to be retransmitted."  As of 0.40 all
	          * previously received packets are allowed, not just the
	          * most recent one.
	          *
	          * We allow multiple assembly processes, one for each
	          * ARCnet card possible on the network.  Seems rather like
	          * a waste of memory.  Necessary?
	          */

	        struct Incoming *in=&lp->incoming[saddr];

	        BUGMSG(D_RX,"packet is split (splitflag=%d, seq=%d)\n",
	        	arcsoft->split_flag,in->sequence);

		if (in->skb && in->sequence!=arcsoft->sequence)
		{
			BUGMSG(D_EXTRA,"wrong seq number (saddr=%d, expected=%d, seq=%d, splitflag=%d)\n",
				saddr,in->sequence,arcsoft->sequence,
				arcsoft->split_flag);
	        	kfree_skb(in->skb,FREE_WRITE);
	        	in->skb=NULL;
			lp->stats.rx_errors++;
	        	lp->stats.rx_missed_errors++;
	        	in->lastpacket=in->numpackets=0;
	        }

	        if (arcsoft->split_flag & 1)	/* first packet in split */
	        {
	        	BUGMSG(D_RX,"brand new splitpacket (splitflag=%d)\n",
	        		arcsoft->split_flag);
	        	if (in->skb)	/* already assembling one! */
	        	{
	        		BUGMSG(D_EXTRA,"aborting previous (seq=%d) assembly (splitflag=%d, seq=%d)\n",
	        			in->sequence,arcsoft->split_flag,
	        			arcsoft->sequence);
				lp->stats.rx_errors++;
	        		lp->stats.rx_missed_errors++;
	        		kfree_skb(in->skb,FREE_WRITE);
	        	}

			in->sequence=arcsoft->sequence;
	        	in->numpackets=((unsigned)arcsoft->split_flag>>1)+2;
	        	in->lastpacket=1;

	        	if (in->numpackets>16)
	        	{
	        		BUGMSG(D_EXTRA,"incoming packet more than 16 segments; dropping. (splitflag=%d)\n",
	        			arcsoft->split_flag);
	        		lp->stats.rx_errors++;
	        		lp->stats.rx_length_errors++;
	        		return;
	        	}

                	in->skb=skb=alloc_skb(508*in->numpackets
                			+ sizeof(struct ClientData),
	                		GFP_ATOMIC);
                	if (skb == NULL) {
                		BUGMSG(D_NORMAL,"(split) memory squeeze, dropping packet.\n");
                		lp->stats.rx_dropped++;
                		return;
                	}

                	soft=(struct ClientData *)skb->data;

                	skb->len=sizeof(struct ClientData);
                	skb->dev=dev;

	         	memcpy((u_char *)soft+EXTRA_CLIENTDATA,
        	 		(u_char *)arcsoft+EXTRA_CLIENTDATA,
         			sizeof(struct ClientData)-EXTRA_CLIENTDATA);
         		soft->split_flag=0; /* final packet won't be split */
	        }
	        else			/* not first packet */
	        {
			int packetnum=((unsigned)arcsoft->split_flag>>1) + 1;

			/* if we're not assembling, there's no point
			 * trying to continue.
			 */
			if (!in->skb)
			{
				BUGMSG(D_EXTRA,"can't continue split without starting first! (splitflag=%d, seq=%d)\n",
					arcsoft->split_flag,arcsoft->sequence);
				lp->stats.rx_errors++;
				lp->stats.rx_missed_errors++;
				return;
			}

	        	in->lastpacket++;
	        	if (packetnum!=in->lastpacket) /* not the right flag! */
	        	{
				/* harmless duplicate? ignore. */
				if (packetnum<=in->lastpacket-1)
				{
					BUGMSG(D_EXTRA,"duplicate splitpacket ignored! (splitflag=%d)\n",
						arcsoft->split_flag);
					lp->stats.rx_errors++;
					lp->stats.rx_frame_errors++;
					return;
				}

				/* "bad" duplicate, kill reassembly */
				BUGMSG(D_EXTRA,"out-of-order splitpacket, reassembly (seq=%d) aborted (splitflag=%d, seq=%d)\n",
					in->sequence,arcsoft->split_flag,
					arcsoft->sequence);
	        		kfree_skb(in->skb,FREE_WRITE);
	        		in->skb=NULL;
				lp->stats.rx_errors++;
	        		lp->stats.rx_missed_errors++;
	        		in->lastpacket=in->numpackets=0;
	        		return;
	        	}

	               	soft=(struct ClientData *)in->skb->data;
	        }

	        skb=in->skb;

	        memcpy(skb->data+skb->len,
	               (u_char *)arcsoft+sizeof(struct ClientData),
	               length-sizeof(struct ClientData));

         	skb->len+=length-sizeof(struct ClientData);

         	soft->daddr=daddr;
         	soft->saddr=saddr;

         	/* are we done? */
         	if (in->lastpacket == in->numpackets)
         	{
         		if (!skb || !in->skb)
         		{
         			BUGMSG(D_NORMAL,"?!? done reassembling packet, no skb? (skb=%ph, in->skb=%ph)\n",
         				skb,in->skb);
         		}
         		else
         		{
	         		in->skb=NULL;
        	 		in->lastpacket=in->numpackets=0;

				BUGLVL(D_SKB) arcnet_dump_skb(dev,skb,"rx");

				skb->protocol=arcnetA_type_trans(skb,dev);

	         		netif_rx(skb);
	         	}
        	}
         }
}




/****************************************************************************
 *                                                                          *
 * Miscellaneous routines                                                   *
 *                                                                          *
 ****************************************************************************/


/* Get the current statistics.	This may be called with the card open or
 * closed.
 */

static struct net_device_stats *arcnet_get_stats(struct device *dev)
{
	struct arcnet_local *lp = (struct arcnet_local *)dev->priv;

	return &lp->stats;
}

#if 0	/* standard ARCnet cards have no promiscuous mode */
/* Set or clear the multicast filter for this adaptor.
 * num_addrs == -1	Promiscuous mode, receive all packets
 * num_addrs == 0	Normal mode, clear multicast list
 * num_addrs > 0	Multicast mode, receive normal and MC packets, and do
 *			best-effort filtering.
 */
static void
set_multicast_list(struct device *dev)
{
	if (num_addrs) {
		/* Enable promiscuous mode */
	} else
		/* Disable promiscuous mode, use normal mode */
}
#endif

/* Create the ARCnet ClientData header for an arbitrary protocol layer
 *
 * saddr=NULL	means use device source address (always will anyway)
 * daddr=NULL	means leave destination address (eg unresolved arp)
 */
int arcnetA_header(struct sk_buff *skb,struct device *dev,
		unsigned short type,void *daddr,void *saddr,unsigned len)
{
	struct ClientData *head = (struct ClientData *)
		skb_push(skb,dev->hard_header_len);
	struct arcnet_local *lp=(struct arcnet_local *)(dev->priv);

	BUGMSG(D_DURING,"create header from %d to %d; protocol %d (%Xh); size %u.\n",
			saddr ? *(u_char*)saddr : -1,
			daddr ? *(u_char*)daddr : -1,
			type,type,len);

	/* set the protocol ID according to RFC1201 */
	switch(type)
	{
	case ETH_P_IP:
		head->protocol_id=ARC_P_IP;
		break;
	case ETH_P_ARP:
		head->protocol_id=ARC_P_ARP;
		break;
	case ETH_P_RARP:
		head->protocol_id=ARC_P_RARP;
		break;
	case ETH_P_IPX:
	case ETH_P_802_3:
	case ETH_P_802_2:
		head->protocol_id=ARC_P_IPX;
		break;
	case ETH_P_ATALK:
		head->protocol_id=ARC_P_ATALK;
		break;
	default:
		BUGMSG(D_NORMAL,"I don't understand protocol %d (%Xh)\n",
			type,type);
		lp->stats.tx_errors++;
		lp->stats.tx_aborted_errors++;
		return 0;
	}

	/*
	 * Set the source hardware address.
	 *
	 * This is pretty pointless for most purposes, but it can help
	 * in debugging.  saddr is stored in the ClientData header and
	 * removed before sending the packet (since ARCnet does not allow
	 * us to change the source address in the actual packet sent)
	 */
	if(saddr)
		head->saddr=((u_char*)saddr)[0];
	else
		head->saddr=((u_char*)(dev->dev_addr))[0];

	head->split_flag=0;	/* split packets are done elsewhere */
	head->sequence=0;	/* so are sequence numbers */

	/* supposedly if daddr is NULL, we should ignore it... */
	if(daddr)
	{
		head->daddr=((u_char*)daddr)[0];
		return dev->hard_header_len;
	}
	else
		head->daddr=0;	/* better fill one in anyway */

	return -dev->hard_header_len;
}



/* Rebuild the ARCnet ClientData header. This is called after an ARP
 * (or in future other address resolution) has completed on this
 * sk_buff. We now let ARP fill in the other fields.
 */
int arcnetA_rebuild_header(struct sk_buff *skb)
{
	struct ClientData *head = (struct ClientData *)skb->data;
	struct device *dev=skb->dev;
	struct arcnet_local *lp=(struct arcnet_local *)(dev->priv);
	int status;

	/*
	 * Only ARP and IP are currently supported
	 *
	 * FIXME: Anyone want to spec IPv6 over ARCnet ?
	 */

	if(head->protocol_id != ARC_P_IP)
	{
		BUGMSG(D_NORMAL,"I don't understand protocol type %d (%Xh) addresses!\n",
			head->protocol_id,head->protocol_id);
		lp->stats.tx_errors++;
		lp->stats.tx_aborted_errors++;
		head->daddr=0;
		/*memcpy(eth->h_source, dev->dev_addr, dev->addr_len);*/
		return 0;
	}

	/*
	 * Try to get ARP to resolve the header.
	 */
#ifdef CONFIG_INET
	BUGMSG(D_DURING,"rebuild header from %d to %d; protocol %Xh\n",
			head->saddr,head->daddr,head->protocol_id);
	status=arp_find(&(head->daddr),skb)? 1 : 0;
	BUGMSG(D_DURING," rebuilt: from %d to %d; protocol %Xh\n",
			head->saddr,head->daddr,head->protocol_id);
	return status;
#else
	return 0;
#endif
}


/* Determine a packet's protocol ID.
 *
 * With ARCnet we have to convert everything to Ethernet-style stuff.
 */
unsigned short arcnetA_type_trans(struct sk_buff *skb,struct device *dev)
{
	struct ClientData *head;
	struct arcnet_local *lp=(struct arcnet_local *) (dev->priv);

	/* Pull off the arcnet header. */
	skb->mac.raw=skb->data;
	skb_pull(skb,dev->hard_header_len);
	head=(struct ClientData *)skb->mac.raw;

	if (head->daddr==0)
		skb->pkt_type=PACKET_BROADCAST;
	else if (dev->flags&IFF_PROMISC)
	{
		/* if we're not sending to ourselves :) */
		if (head->daddr != dev->dev_addr[0])
			skb->pkt_type=PACKET_OTHERHOST;
	}

	/* now return the protocol number */
	switch (head->protocol_id)
	{
	case ARC_P_IP:		return htons(ETH_P_IP);
	case ARC_P_ARP:		return htons(ETH_P_ARP);
	case ARC_P_RARP:	return htons(ETH_P_RARP);

	case ARC_P_IPX:
	case ARC_P_NOVELL_EC:
		return htons(ETH_P_802_3);
	default:
		lp->stats.rx_errors++;
		lp->stats.rx_crc_errors++;
		return 0;
	}

	return htons(ETH_P_IP);
}


#ifdef CONFIG_ARCNET_ETH
/****************************************************************************
 *                                                                          *
 * Ethernet-Encap Support                                                   *
 *                                                                          *
 ****************************************************************************/

/* Initialize the arc0e device.
 */
static int arcnetE_init(struct device *dev)
{
	struct arcnet_local *lp = (struct arcnet_local *)dev->priv;

	ether_setup(dev); /* we're emulating ether here, not ARCnet */
	dev->dev_addr[0]=0;
	dev->dev_addr[5]=lp->stationid;
	dev->mtu=512-sizeof(struct HardHeader)-dev->hard_header_len-1;
	dev->open=arcnetE_open_close;
	dev->stop=arcnetE_open_close;
	dev->hard_start_xmit=arcnetE_send_packet;

	BUGMSG(D_NORMAL,"ARCnet Ethernet-Encap protocol initialized.\n");

	return 0;
}


/* Bring up/down the arc0e device - we don't actually have to do anything,
 * since our parent arc0 handles the card I/O itself.
 */
static int arcnetE_open_close(struct device *dev)
{
	return 0;
}


/* Called by the kernel in order to transmit an ethernet-type packet.
 */
static int
arcnetE_send_packet(struct sk_buff *skb, struct device *dev)
{
	struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
	int ioaddr=IOADDR,bad;
	union ArcPacket *arcpacket =
		(union ArcPacket *)phys_to_virt(dev->mem_start+512*(lp->txbuf^1));
	u_char *arcsoft,daddr;
	short offset,length=skb->len+1;

	lp->intx++;

	bad=arcnet_send_packet_bad(skb,dev);
	if (bad)
	{
		lp->intx--;
		return bad;
	}

	TBUSY=1;

	if (length>XMTU)
	{
		BUGMSG(D_NORMAL,"MTU must be <= 493 for ethernet encap (length=%d).\n",
			length);
		BUGMSG(D_NORMAL,"transmit aborted.\n");

		dev_kfree_skb(skb,FREE_WRITE);
		lp->intx--;
		return 0;
	}

	BUGMSG(D_DURING,"starting tx sequence...\n");

	lp->txbuf=lp->txbuf^1; /* XOR with 1 to alternate btw 2 & 3 */

#ifndef SLOW_XMIT_COPY
	/* clean out the page to make debugging make more sense :) */
	BUGLVL(D_DURING)
		memset_io(dev->mem_start+lp->txbuf*512,0x42,512);
#endif

	/* broadcasts have address FF:FF:FF:FF:FF:FF in etherspeak */
	if (((struct ethhdr*)(skb->data))->h_dest[0] == 0xFF)
		daddr=arcpacket->hardheader.destination=0;
	else
		daddr=arcpacket->hardheader.destination=
			((struct ethhdr*)(skb->data))->h_dest[5];

	/* load packet into shared memory */
	offset=512-length;
	if (length>MTU)		/* long/exception packet */
	{
		if (length<MinTU) offset-=3;
		arcpacket->hardheader.offset1=0;
		arcpacket->hardheader.offset2=offset;
	}
	else			/* short packet */
	{
		arcpacket->hardheader.offset1=(offset-=256);
	}

	BUGMSG(D_DURING," length=%Xh, offset=%Xh, offset1=%Xh, offset2=%Xh\n",
			length,offset,arcpacket->hardheader.offset1,
			arcpacket->hardheader.offset2);

	arcsoft=&arcpacket->raw[offset];
	arcsoft[0]=ARC_P_ETHER;
	arcsoft++;

	/* copy the packet into ARCnet shmem
	 *  - the first bytes of ClientData header are skipped
	 */
	BUGMSG(D_DURING,"ready to memcpy\n");

	memcpy(arcsoft,skb->data,skb->len);

	BUGMSG(D_DURING,"transmitting packet to station %02Xh (%d bytes)\n",
			daddr,length);

	BUGLVL(D_TX) arcnet_dump_packet(dev,arcpacket->raw,length>=240,"tx");

	lp->lastload_dest=daddr;
	lp->txready=lp->txbuf;	/* packet is ready for sending */

	dev_kfree_skb(skb,FREE_WRITE);

	if (arcnet_go_tx(dev,1))
	{
		/* inform upper layers */
		TBUSY=0;
		mark_bh(NET_BH);
	}

	dev->trans_start=jiffies;
	lp->intx--;

	/* make sure we didn't ignore a TX IRQ while we were in here */
	lp->intmask |= TXFREEflag;
	SETMASK;

	return 0;
}


/* Packet receiver for ethernet-encap packets.
 */
static void
arcnetE_rx(struct device *dev,u_char *arcsoft,
	int length,u_char saddr, u_char daddr)
{
	struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
	struct sk_buff *skb;

	BUGMSG(D_DURING,"it's an ethernet-encap packet (length=%d)\n",
			length);

       	skb = alloc_skb(length, GFP_ATOMIC);
       	if (skb == NULL) {
       		BUGMSG(D_NORMAL,"Memory squeeze, dropping packet.\n");
       		lp->stats.rx_dropped++;
       		return;
       	}

       	skb->len = length;
       	skb->dev = dev;

       	memcpy(skb->data,(u_char *)arcsoft+1,length-1);

        BUGLVL(D_SKB) arcnet_dump_skb(dev,skb,"rx");

	skb->protocol=eth_type_trans(skb,dev);

        netif_rx(skb);
}

#endif /* CONFIG_ARCNET_ETH */

#ifdef CONFIG_ARCNET_1051
/****************************************************************************
 *                                                                          *
 * RFC1051 Support                                                          *
 *                                                                          *
 ****************************************************************************/

/* Initialize the arc0s device.
 */
static int arcnetS_init(struct device *dev)
{
	struct arcnet_local *lp = (struct arcnet_local *)dev->priv;

	arcnet_setup(dev);

	/* And now fill particular fields with arcnet values */
	dev->dev_addr[0]=lp->stationid;
	dev->hard_header_len=sizeof(struct S_ClientData);
	dev->mtu=512-sizeof(struct HardHeader)-dev->hard_header_len
		+ S_EXTRA_CLIENTDATA;
	dev->open=arcnetS_open_close;
	dev->stop=arcnetS_open_close;
	dev->hard_start_xmit=arcnetS_send_packet;
	dev->hard_header=arcnetS_header;
	dev->rebuild_header=arcnetS_rebuild_header;
	BUGMSG(D_NORMAL,"ARCnet RFC1051 (NetBSD, AmiTCP) protocol initialized.\n");

	return 0;
}


/* Bring up/down the arc0s device - we don't actually have to do anything,
 * since our parent arc0 handles the card I/O itself.
 */
static int arcnetS_open_close(struct device *dev)
{
	return 0;
}


/* Called by the kernel in order to transmit an RFC1051-type packet.
 */
static int
arcnetS_send_packet(struct sk_buff *skb, struct device *dev)
{
	struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
	int ioaddr=IOADDR,bad,length;
	struct S_ClientData *hdr=(struct S_ClientData *)skb->data;

	lp->intx++;

	bad=arcnet_send_packet_bad(skb,dev);
	if (bad)
	{
		lp->intx--;
		return bad;
	}

	TBUSY=1;

	length = 1 < skb->len ? skb->len : 1;

	BUGLVL(D_SKB) arcnet_dump_skb(dev,skb,"tx");

	/* fits in one packet? */
	if (length-S_EXTRA_CLIENTDATA<=XMTU)
	{
		arcnetAS_prepare_tx(dev,
			skb->data+S_EXTRA_CLIENTDATA,
			sizeof(struct S_ClientData)-S_EXTRA_CLIENTDATA,
			skb->data+sizeof(struct S_ClientData),
			length-sizeof(struct S_ClientData),
			hdr->daddr,0);

		/* done right away */
		dev_kfree_skb(skb,FREE_WRITE);

		if (arcnet_go_tx(dev,1))
		{
			/* inform upper layers */
			TBUSY=0;
			mark_bh(NET_BH);
		}
	}
	else			/* too big for one - not accepted */
	{
		BUGMSG(D_NORMAL,"packet too long (length=%d)\n",
			length);
		dev_kfree_skb(skb,FREE_WRITE);
		lp->stats.tx_dropped++;
		TBUSY=0;
		mark_bh(NET_BH);
	}

	dev->trans_start=jiffies;
	lp->intx--;

	/* make sure we didn't ignore a TX IRQ while we were in here */
	lp->intmask |= TXFREEflag;
	SETMASK;

	return 0;
}


/* Packet receiver for RFC1051 packets;
 */
static void
arcnetS_rx(struct device *dev,u_char *buf,
	int length,u_char saddr, u_char daddr)
{
	struct arcnet_local *lp = (struct arcnet_local *)dev->priv;
	struct sk_buff *skb;
	struct S_ClientData *arcsoft,*soft;

	arcsoft=(struct S_ClientData *)(buf-S_EXTRA_CLIENTDATA);
	length+=S_EXTRA_CLIENTDATA;

	BUGMSG(D_DURING,"it's an RFC1051 packet (length=%d)\n",
			length);



	{    /* was "if not split" in A protocol, S is never split */

         	skb = alloc_skb(length, GFP_ATOMIC);
         	if (skb == NULL) {
         		BUGMSG(D_NORMAL,"Memory squeeze, dropping packet.\n");
         		lp->stats.rx_dropped++;
         		return;
         	}
         	soft=(struct S_ClientData *)skb->data;
		skb->len = length;
		memcpy((u_char *)soft + sizeof(struct S_ClientData)
				- S_EXTRA_CLIENTDATA,
			(u_char *)arcsoft + sizeof(struct S_ClientData)
				- S_EXTRA_CLIENTDATA,
			length - sizeof(struct S_ClientData)
				+ S_EXTRA_CLIENTDATA);
		soft->protocol_id=arcsoft->protocol_id;
         	soft->daddr=daddr;
         	soft->saddr=saddr;
         	skb->dev = dev;  /* is already lp->sdev */

		BUGLVL(D_SKB) arcnet_dump_skb(dev,skb,"rx");

		skb->protocol=arcnetS_type_trans(skb,dev);

         	netif_rx(skb);
         }
}


/* Create the ARCnet ClientData header for an arbitrary protocol layer
 *
 * saddr=NULL	means use device source address (always will anyway)
 * daddr=NULL	means leave destination address (eg unresolved arp)
 */
int arcnetS_header(struct sk_buff *skb,struct device *dev,
		unsigned short type,void *daddr,void *saddr,unsigned len)
{
	struct S_ClientData *head = (struct S_ClientData *)
		skb_push(skb,dev->hard_header_len);
	struct arcnet_local *lp=(struct arcnet_local *)(dev->priv);

	/* set the protocol ID according to RFC1051 */
	switch(type)
	{
	case ETH_P_IP:
		head->protocol_id=ARC_P_IP_RFC1051;
		BUGMSG(D_DURING,"S_header: IP_RFC1051 packet.\n");
		break;
	case ETH_P_ARP:
		head->protocol_id=ARC_P_ARP_RFC1051;
		BUGMSG(D_DURING,"S_header: ARP_RFC1051 packet.\n");
		break;
	default:
		BUGMSG(D_NORMAL,"I don't understand protocol %d (%Xh)\n",
			type,type);
		lp->stats.tx_errors++;
		lp->stats.tx_aborted_errors++;
		return 0;
	}

	/*
	 * Set the source hardware address.
	 *
	 * This is pretty pointless for most purposes, but it can help
	 * in debugging.  saddr is stored in the ClientData header and
	 * removed before sending the packet (since ARCnet does not allow
	 * us to change the source address in the actual packet sent)
	 */
	if(saddr)
		head->saddr=((u_char*)saddr)[0];
	else
		head->saddr=((u_char*)(dev->dev_addr))[0];

	/* supposedly if daddr is NULL, we should ignore it... */
	if(daddr)
	{
		head->daddr=((u_char*)daddr)[0];
		return dev->hard_header_len;
	}
	else
		head->daddr=0;	/* better fill one in anyway */

	return -dev->hard_header_len;
}


/* Rebuild the ARCnet ClientData header. This is called after an ARP
 * (or in future other address resolution) has completed on this
 * sk_buff. We now let ARP fill in the other fields.
 */
int arcnetS_rebuild_header(struct sk_buff *skb)
{
	struct device *dev=skb->dev;
	struct S_ClientData *head = (struct S_ClientData *)skb->data;
	struct arcnet_local *lp=(struct arcnet_local *)(dev->priv);

	/*
	 * Only ARP and IP are currently supported
	 */

	if(head->protocol_id != ARC_P_IP_RFC1051)
	{
		BUGMSG(D_NORMAL,"I don't understand protocol type %d (%Xh) addresses!\n",
			head->protocol_id,head->protocol_id);
		lp->stats.tx_errors++;
		lp->stats.tx_aborted_errors++;
		head->daddr=0;
		/*memcpy(eth->h_source, dev->dev_addr, dev->addr_len);*/
		return 0;
	}

	/*
	 * Try to get ARP to resolve the header.
	 */
#ifdef CONFIG_INET
	return arp_find(&(head->daddr),skb)? 1 : 0;
#else
	return 0;
#endif
}


/* Determine a packet's protocol ID.
 *
 * With ARCnet we have to convert everything to Ethernet-style stuff.
 */
unsigned short arcnetS_type_trans(struct sk_buff *skb,struct device *dev)
{
	struct S_ClientData *head;
	struct arcnet_local *lp=(struct arcnet_local *) (dev->priv);

	/* Pull off the arcnet header. */
	skb->mac.raw=skb->data;
	skb_pull(skb,dev->hard_header_len);
	head=(struct S_ClientData *)skb->mac.raw;

	if (head->daddr==0)
		skb->pkt_type=PACKET_BROADCAST;
	else if (dev->flags&IFF_PROMISC)
	{
		/* if we're not sending to ourselves :) */
		if (head->daddr != dev->dev_addr[0])
			skb->pkt_type=PACKET_OTHERHOST;
	}

	/* now return the protocol number */
	switch (head->protocol_id)
	{
	case ARC_P_IP_RFC1051:	return htons(ETH_P_IP);
	case ARC_P_ARP_RFC1051:	return htons(ETH_P_ARP);
	case ARC_P_ATALK:   return htons(ETH_P_ATALK); /* untested appletalk */
	default:
		lp->stats.rx_errors++;
		lp->stats.rx_crc_errors++;
		return 0;
	}

	return htons(ETH_P_IP);
}

#endif	/* CONFIG_ARCNET_1051 */

/****************************************************************************
 *                                                                          *
 * Kernel Loadable Module Support                                           *
 *                                                                          *
 ****************************************************************************/


#ifdef MODULE

static char devicename[9] = "";
static struct device thiscard = {
  devicename, /* device name is inserted by linux/drivers/net/net_init.c */
  0, 0, 0, 0,
  0, 0,  /* I/O address, IRQ */
  0, 0, 0, NULL, arcnet_probe
};


static int io=0x0;	/* <--- EDIT THESE LINES FOR YOUR CONFIGURATION */
static int irqnum=0;	/* or use the insmod io= irq= shmem= options */
static int irq=0;
static int shmem=0;
static char *device = NULL;	/* use eg. device="arc1" to change name */

MODULE_PARM(io, "i");
MODULE_PARM(irqnum, "i");
MODULE_PARM(shmem, "i");

#ifdef RIM_I_MODE
  static int node=0;	/* you must specify the node ID for RIM I cards */
#endif

int init_module(void)
{
	struct device *dev=&thiscard;
	if (device)
		strcpy(dev->name,device);
	#ifndef CONFIG_ARCNET_ETHNAME
	  else if (!dev->name[0]) strcpy(dev->name,"arc0");
	#endif

	#ifdef RIM_I_MODE
	if (node) io=node;
	#endif

	dev->base_addr=io;

	if (irq) irqnum=irq;
	dev->irq=irqnum;
	if (dev->irq==2) dev->irq=9;

	if (shmem)
	{
		dev->mem_start=shmem;
		dev->mem_end=thiscard.mem_start+512*4-1;
		dev->rmem_start=thiscard.mem_start+512*0;
		dev->rmem_end=thiscard.mem_start+512*2-1;
	}

	if (register_netdev(dev) != 0)
		return -EIO;
	return 0;
}

void cleanup_module(void)
{
	struct device *dev=&thiscard;
	int ioaddr=IOADDR;

	if (dev->start) arcnet_close(dev);

	/* Flush TX and disable RX */
	if (ioaddr)
	{
		AINTMASK(0);		/* disable IRQ's */
		ACOMMAND(NOTXcmd);	/* stop transmit */
		ACOMMAND(NORXcmd);	/* disable receive */
	}

	if (dev->irq)
	{
		irq2dev_map[dev->irq] = NULL;
		free_irq(dev->irq,NULL);
	}

	if (dev->base_addr) RELEASE_REGION(dev->base_addr,ARCNET_TOTAL_SIZE);
	unregister_netdev(dev);
	kfree(dev->priv);
	dev->priv = NULL;
}

#endif /* MODULE */