aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/net/wireless/wavelan.c
blob: 33ed9fe95f3db61661d535bc0a37f8afe30f142c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
/*
 *	WaveLAN ISA driver
 *
 *		Jean II - HPLB '96
 *
 * Reorganisation and extension of the driver.
 * Original copyright follows (also see the end of this file).
 * See wavelan.p.h for details.
 *
 *
 *
 * AT&T GIS (nee NCR) WaveLAN card:
 *	An Ethernet-like radio transceiver
 *	controlled by an Intel 82586 coprocessor.
 */

#include "wavelan.p.h"		/* Private header */

/************************* MISC SUBROUTINES **************************/
/*
 * Subroutines which won't fit in one of the following category
 * (WaveLAN modem or i82586)
 */

/*------------------------------------------------------------------*/
/*
 * Translate irq number to PSA irq parameter
 */
static u8 wv_irq_to_psa(int irq)
{
	if (irq < 0 || irq >= ARRAY_SIZE(irqvals))
		return 0;

	return irqvals[irq];
}

/*------------------------------------------------------------------*/
/*
 * Translate PSA irq parameter to irq number 
 */
static int __init wv_psa_to_irq(u8 irqval)
{
	int irq;

	for (irq = 0; irq < ARRAY_SIZE(irqvals); irq++)
		if (irqvals[irq] == irqval)
			return irq;

	return -1;
}

#ifdef STRUCT_CHECK
/*------------------------------------------------------------------*/
/*
 * Sanity routine to verify the sizes of the various WaveLAN interface
 * structures.
 */
static char *wv_struct_check(void)
{
#define	SC(t,s,n)	if (sizeof(t) != s) return(n);

	SC(psa_t, PSA_SIZE, "psa_t");
	SC(mmw_t, MMW_SIZE, "mmw_t");
	SC(mmr_t, MMR_SIZE, "mmr_t");
	SC(ha_t, HA_SIZE, "ha_t");

#undef	SC

	return ((char *) NULL);
}				/* wv_struct_check */
#endif				/* STRUCT_CHECK */

/********************* HOST ADAPTER SUBROUTINES *********************/
/*
 * Useful subroutines to manage the WaveLAN ISA interface
 *
 * One major difference with the PCMCIA hardware (except the port mapping)
 * is that we have to keep the state of the Host Control Register
 * because of the interrupt enable & bus size flags.
 */

/*------------------------------------------------------------------*/
/*
 * Read from card's Host Adaptor Status Register.
 */
static inline u16 hasr_read(unsigned long ioaddr)
{
	return (inw(HASR(ioaddr)));
}				/* hasr_read */

/*------------------------------------------------------------------*/
/*
 * Write to card's Host Adapter Command Register.
 */
static inline void hacr_write(unsigned long ioaddr, u16 hacr)
{
	outw(hacr, HACR(ioaddr));
}				/* hacr_write */

/*------------------------------------------------------------------*/
/*
 * Write to card's Host Adapter Command Register. Include a delay for
 * those times when it is needed.
 */
static void hacr_write_slow(unsigned long ioaddr, u16 hacr)
{
	hacr_write(ioaddr, hacr);
	/* delay might only be needed sometimes */
	mdelay(1);
}				/* hacr_write_slow */

/*------------------------------------------------------------------*/
/*
 * Set the channel attention bit.
 */
static inline void set_chan_attn(unsigned long ioaddr, u16 hacr)
{
	hacr_write(ioaddr, hacr | HACR_CA);
}				/* set_chan_attn */

/*------------------------------------------------------------------*/
/*
 * Reset, and then set host adaptor into default mode.
 */
static inline void wv_hacr_reset(unsigned long ioaddr)
{
	hacr_write_slow(ioaddr, HACR_RESET);
	hacr_write(ioaddr, HACR_DEFAULT);
}				/* wv_hacr_reset */

/*------------------------------------------------------------------*/
/*
 * Set the I/O transfer over the ISA bus to 8-bit mode
 */
static inline void wv_16_off(unsigned long ioaddr, u16 hacr)
{
	hacr &= ~HACR_16BITS;
	hacr_write(ioaddr, hacr);
}				/* wv_16_off */

/*------------------------------------------------------------------*/
/*
 * Set the I/O transfer over the ISA bus to 8-bit mode
 */
static inline void wv_16_on(unsigned long ioaddr, u16 hacr)
{
	hacr |= HACR_16BITS;
	hacr_write(ioaddr, hacr);
}				/* wv_16_on */

/*------------------------------------------------------------------*/
/*
 * Disable interrupts on the WaveLAN hardware.
 * (called by wv_82586_stop())
 */
static inline void wv_ints_off(struct net_device * dev)
{
	net_local *lp = (net_local *) dev->priv;
	unsigned long ioaddr = dev->base_addr;
	
	lp->hacr &= ~HACR_INTRON;
	hacr_write(ioaddr, lp->hacr);
}				/* wv_ints_off */

/*------------------------------------------------------------------*/
/*
 * Enable interrupts on the WaveLAN hardware.
 * (called by wv_hw_reset())
 */
static inline void wv_ints_on(struct net_device * dev)
{
	net_local *lp = (net_local *) dev->priv;
	unsigned long ioaddr = dev->base_addr;

	lp->hacr |= HACR_INTRON;
	hacr_write(ioaddr, lp->hacr);
}				/* wv_ints_on */

/******************* MODEM MANAGEMENT SUBROUTINES *******************/
/*
 * Useful subroutines to manage the modem of the WaveLAN
 */

/*------------------------------------------------------------------*/
/*
 * Read the Parameter Storage Area from the WaveLAN card's memory
 */
/*
 * Read bytes from the PSA.
 */
static void psa_read(unsigned long ioaddr, u16 hacr, int o,	/* offset in PSA */
		     u8 * b,	/* buffer to fill */
		     int n)
{				/* size to read */
	wv_16_off(ioaddr, hacr);

	while (n-- > 0) {
		outw(o, PIOR2(ioaddr));
		o++;
		*b++ = inb(PIOP2(ioaddr));
	}

	wv_16_on(ioaddr, hacr);
}				/* psa_read */

/*------------------------------------------------------------------*/
/*
 * Write the Parameter Storage Area to the WaveLAN card's memory.
 */
static void psa_write(unsigned long ioaddr, u16 hacr, int o,	/* Offset in PSA */
		      u8 * b,	/* Buffer in memory */
		      int n)
{				/* Length of buffer */
	int count = 0;

	wv_16_off(ioaddr, hacr);

	while (n-- > 0) {
		outw(o, PIOR2(ioaddr));
		o++;

		outb(*b, PIOP2(ioaddr));
		b++;

		/* Wait for the memory to finish its write cycle */
		count = 0;
		while ((count++ < 100) &&
		       (hasr_read(ioaddr) & HASR_PSA_BUSY)) mdelay(1);
	}

	wv_16_on(ioaddr, hacr);
}				/* psa_write */

#ifdef SET_PSA_CRC
/*------------------------------------------------------------------*/
/*
 * Calculate the PSA CRC
 * Thanks to Valster, Nico <NVALSTER@wcnd.nl.lucent.com> for the code
 * NOTE: By specifying a length including the CRC position the
 * returned value should be zero. (i.e. a correct checksum in the PSA)
 *
 * The Windows drivers don't use the CRC, but the AP and the PtP tool
 * depend on it.
 */
static u16 psa_crc(u8 * psa,	/* The PSA */
			      int size)
{				/* Number of short for CRC */
	int byte_cnt;		/* Loop on the PSA */
	u16 crc_bytes = 0;	/* Data in the PSA */
	int bit_cnt;		/* Loop on the bits of the short */

	for (byte_cnt = 0; byte_cnt < size; byte_cnt++) {
		crc_bytes ^= psa[byte_cnt];	/* Its an xor */

		for (bit_cnt = 1; bit_cnt < 9; bit_cnt++) {
			if (crc_bytes & 0x0001)
				crc_bytes = (crc_bytes >> 1) ^ 0xA001;
			else
				crc_bytes >>= 1;
		}
	}

	return crc_bytes;
}				/* psa_crc */
#endif				/* SET_PSA_CRC */

/*------------------------------------------------------------------*/
/*
 * update the checksum field in the Wavelan's PSA
 */
static void update_psa_checksum(struct net_device * dev, unsigned long ioaddr, u16 hacr)
{
#ifdef SET_PSA_CRC
	psa_t psa;
	u16 crc;

	/* read the parameter storage area */
	psa_read(ioaddr, hacr, 0, (unsigned char *) &psa, sizeof(psa));

	/* update the checksum */
	crc = psa_crc((unsigned char *) &psa,
		      sizeof(psa) - sizeof(psa.psa_crc[0]) -
		      sizeof(psa.psa_crc[1])
		      - sizeof(psa.psa_crc_status));

	psa.psa_crc[0] = crc & 0xFF;
	psa.psa_crc[1] = (crc & 0xFF00) >> 8;

	/* Write it ! */
	psa_write(ioaddr, hacr, (char *) &psa.psa_crc - (char *) &psa,
		  (unsigned char *) &psa.psa_crc, 2);

#ifdef DEBUG_IOCTL_INFO
	printk(KERN_DEBUG "%s: update_psa_checksum(): crc = 0x%02x%02x\n",
	       dev->name, psa.psa_crc[0], psa.psa_crc[1]);

	/* Check again (luxury !) */
	crc = psa_crc((unsigned char *) &psa,
		      sizeof(psa) - sizeof(psa.psa_crc_status));

	if (crc != 0)
		printk(KERN_WARNING
		       "%s: update_psa_checksum(): CRC does not agree with PSA data (even after recalculating)\n",
		       dev->name);
#endif				/* DEBUG_IOCTL_INFO */
#endif				/* SET_PSA_CRC */
}				/* update_psa_checksum */

/*------------------------------------------------------------------*/
/*
 * Write 1 byte to the MMC.
 */
static void mmc_out(unsigned long ioaddr, u16 o, u8 d)
{
	int count = 0;

	/* Wait for MMC to go idle */
	while ((count++ < 100) && (inw(HASR(ioaddr)) & HASR_MMC_BUSY))
		udelay(10);

	outw((u16) (((u16) d << 8) | (o << 1) | 1), MMCR(ioaddr));
}

/*------------------------------------------------------------------*/
/*
 * Routine to write bytes to the Modem Management Controller.
 * We start at the end because it is the way it should be!
 */
static void mmc_write(unsigned long ioaddr, u8 o, u8 * b, int n)
{
	o += n;
	b += n;

	while (n-- > 0)
		mmc_out(ioaddr, --o, *(--b));
}				/* mmc_write */

/*------------------------------------------------------------------*/
/*
 * Read a byte from the MMC.
 * Optimised version for 1 byte, avoid using memory.
 */
static u8 mmc_in(unsigned long ioaddr, u16 o)
{
	int count = 0;

	while ((count++ < 100) && (inw(HASR(ioaddr)) & HASR_MMC_BUSY))
		udelay(10);
	outw(o << 1, MMCR(ioaddr));

	while ((count++ < 100) && (inw(HASR(ioaddr)) & HASR_MMC_BUSY))
		udelay(10);
	return (u8) (inw(MMCR(ioaddr)) >> 8);
}

/*------------------------------------------------------------------*/
/*
 * Routine to read bytes from the Modem Management Controller.
 * The implementation is complicated by a lack of address lines,
 * which prevents decoding of the low-order bit.
 * (code has just been moved in the above function)
 * We start at the end because it is the way it should be!
 */
static inline void mmc_read(unsigned long ioaddr, u8 o, u8 * b, int n)
{
	o += n;
	b += n;

	while (n-- > 0)
		*(--b) = mmc_in(ioaddr, --o);
}				/* mmc_read */

/*------------------------------------------------------------------*/
/*
 * Get the type of encryption available.
 */
static inline int mmc_encr(unsigned long ioaddr)
{				/* I/O port of the card */
	int temp;

	temp = mmc_in(ioaddr, mmroff(0, mmr_des_avail));
	if ((temp != MMR_DES_AVAIL_DES) && (temp != MMR_DES_AVAIL_AES))
		return 0;
	else
		return temp;
}

/*------------------------------------------------------------------*/
/*
 * Wait for the frequency EEPROM to complete a command.
 * I hope this one will be optimally inlined.
 */
static inline void fee_wait(unsigned long ioaddr,	/* I/O port of the card */
			    int delay,	/* Base delay to wait for */
			    int number)
{				/* Number of time to wait */
	int count = 0;		/* Wait only a limited time */

	while ((count++ < number) &&
	       (mmc_in(ioaddr, mmroff(0, mmr_fee_status)) &
		MMR_FEE_STATUS_BUSY)) udelay(delay);
}

/*------------------------------------------------------------------*/
/*
 * Read bytes from the Frequency EEPROM (frequency select cards).
 */
static void fee_read(unsigned long ioaddr,	/* I/O port of the card */
		     u16 o,	/* destination offset */
		     u16 * b,	/* data buffer */
		     int n)
{				/* number of registers */
	b += n;			/* Position at the end of the area */

	/* Write the address */
	mmc_out(ioaddr, mmwoff(0, mmw_fee_addr), o + n - 1);

	/* Loop on all buffer */
	while (n-- > 0) {
		/* Write the read command */
		mmc_out(ioaddr, mmwoff(0, mmw_fee_ctrl),
			MMW_FEE_CTRL_READ);

		/* Wait until EEPROM is ready (should be quick). */
		fee_wait(ioaddr, 10, 100);

		/* Read the value. */
		*--b = ((mmc_in(ioaddr, mmroff(0, mmr_fee_data_h)) << 8) |
			mmc_in(ioaddr, mmroff(0, mmr_fee_data_l)));
	}
}


/*------------------------------------------------------------------*/
/*
 * Write bytes from the Frequency EEPROM (frequency select cards).
 * This is a bit complicated, because the frequency EEPROM has to
 * be unprotected and the write enabled.
 * Jean II
 */
static void fee_write(unsigned long ioaddr,	/* I/O port of the card */
		      u16 o,	/* destination offset */
		      u16 * b,	/* data buffer */
		      int n)
{				/* number of registers */
	b += n;			/* Position at the end of the area. */

#ifdef EEPROM_IS_PROTECTED	/* disabled */
#ifdef DOESNT_SEEM_TO_WORK	/* disabled */
	/* Ask to read the protected register */
	mmc_out(ioaddr, mmwoff(0, mmw_fee_ctrl), MMW_FEE_CTRL_PRREAD);

	fee_wait(ioaddr, 10, 100);

	/* Read the protected register. */
	printk("Protected 2:  %02X-%02X\n",
	       mmc_in(ioaddr, mmroff(0, mmr_fee_data_h)),
	       mmc_in(ioaddr, mmroff(0, mmr_fee_data_l)));
#endif				/* DOESNT_SEEM_TO_WORK */

	/* Enable protected register. */
	mmc_out(ioaddr, mmwoff(0, mmw_fee_addr), MMW_FEE_ADDR_EN);
	mmc_out(ioaddr, mmwoff(0, mmw_fee_ctrl), MMW_FEE_CTRL_PREN);

	fee_wait(ioaddr, 10, 100);

	/* Unprotect area. */
	mmc_out(ioaddr, mmwoff(0, mmw_fee_addr), o + n);
	mmc_out(ioaddr, mmwoff(0, mmw_fee_ctrl), MMW_FEE_CTRL_PRWRITE);
#ifdef DOESNT_SEEM_TO_WORK	/* disabled */
	/* or use: */
	mmc_out(ioaddr, mmwoff(0, mmw_fee_ctrl), MMW_FEE_CTRL_PRCLEAR);
#endif				/* DOESNT_SEEM_TO_WORK */

	fee_wait(ioaddr, 10, 100);
#endif				/* EEPROM_IS_PROTECTED */

	/* Write enable. */
	mmc_out(ioaddr, mmwoff(0, mmw_fee_addr), MMW_FEE_ADDR_EN);
	mmc_out(ioaddr, mmwoff(0, mmw_fee_ctrl), MMW_FEE_CTRL_WREN);

	fee_wait(ioaddr, 10, 100);

	/* Write the EEPROM address. */
	mmc_out(ioaddr, mmwoff(0, mmw_fee_addr), o + n - 1);

	/* Loop on all buffer */
	while (n-- > 0) {
		/* Write the value. */
		mmc_out(ioaddr, mmwoff(0, mmw_fee_data_h), (*--b) >> 8);
		mmc_out(ioaddr, mmwoff(0, mmw_fee_data_l), *b & 0xFF);

		/* Write the write command. */
		mmc_out(ioaddr, mmwoff(0, mmw_fee_ctrl),
			MMW_FEE_CTRL_WRITE);

		/* WaveLAN documentation says to wait at least 10 ms for EEBUSY = 0 */
		mdelay(10);
		fee_wait(ioaddr, 10, 100);
	}

	/* Write disable. */
	mmc_out(ioaddr, mmwoff(0, mmw_fee_addr), MMW_FEE_ADDR_DS);
	mmc_out(ioaddr, mmwoff(0, mmw_fee_ctrl), MMW_FEE_CTRL_WDS);

	fee_wait(ioaddr, 10, 100);

#ifdef EEPROM_IS_PROTECTED	/* disabled */
	/* Reprotect EEPROM. */
	mmc_out(ioaddr, mmwoff(0, mmw_fee_addr), 0x00);
	mmc_out(ioaddr, mmwoff(0, mmw_fee_ctrl), MMW_FEE_CTRL_PRWRITE);

	fee_wait(ioaddr, 10, 100);
#endif				/* EEPROM_IS_PROTECTED */
}

/************************ I82586 SUBROUTINES *************************/
/*
 * Useful subroutines to manage the Ethernet controller
 */

/*------------------------------------------------------------------*/
/*
 * Read bytes from the on-board RAM.
 * Why does inlining this function make it fail?
 */
static /*inline */ void obram_read(unsigned long ioaddr,
				   u16 o, u8 * b, int n)
{
	outw(o, PIOR1(ioaddr));
	insw(PIOP1(ioaddr), (unsigned short *) b, (n + 1) >> 1);
}

/*------------------------------------------------------------------*/
/*
 * Write bytes to the on-board RAM.
 */
static inline void obram_write(unsigned long ioaddr, u16 o, u8 * b, int n)
{
	outw(o, PIOR1(ioaddr));
	outsw(PIOP1(ioaddr), (unsigned short *) b, (n + 1) >> 1);
}

/*------------------------------------------------------------------*/
/*
 * Acknowledge the reading of the status issued by the i82586.
 */
static void wv_ack(struct net_device * dev)
{
	net_local *lp = (net_local *) dev->priv;
	unsigned long ioaddr = dev->base_addr;
	u16 scb_cs;
	int i;

	obram_read(ioaddr, scboff(OFFSET_SCB, scb_status),
		   (unsigned char *) &scb_cs, sizeof(scb_cs));
	scb_cs &= SCB_ST_INT;

	if (scb_cs == 0)
		return;

	obram_write(ioaddr, scboff(OFFSET_SCB, scb_command),
		    (unsigned char *) &scb_cs, sizeof(scb_cs));

	set_chan_attn(ioaddr, lp->hacr);

	for (i = 1000; i > 0; i--) {
		obram_read(ioaddr, scboff(OFFSET_SCB, scb_command),
			   (unsigned char *) &scb_cs, sizeof(scb_cs));
		if (scb_cs == 0)
			break;

		udelay(10);
	}
	udelay(100);

#ifdef DEBUG_CONFIG_ERROR
	if (i <= 0)
		printk(KERN_INFO
		       "%s: wv_ack(): board not accepting command.\n",
		       dev->name);
#endif
}

/*------------------------------------------------------------------*/
/*
 * Set channel attention bit and busy wait until command has
 * completed, then acknowledge completion of the command.
 */
static int wv_synchronous_cmd(struct net_device * dev, const char *str)
{
	net_local *lp = (net_local *) dev->priv;
	unsigned long ioaddr = dev->base_addr;
	u16 scb_cmd;
	ach_t cb;
	int i;

	scb_cmd = SCB_CMD_CUC & SCB_CMD_CUC_GO;
	obram_write(ioaddr, scboff(OFFSET_SCB, scb_command),
		    (unsigned char *) &scb_cmd, sizeof(scb_cmd));

	set_chan_attn(ioaddr, lp->hacr);

	for (i = 1000; i > 0; i--) {
		obram_read(ioaddr, OFFSET_CU, (unsigned char *) &cb,
			   sizeof(cb));
		if (cb.ac_status & AC_SFLD_C)
			break;

		udelay(10);
	}
	udelay(100);

	if (i <= 0 || !(cb.ac_status & AC_SFLD_OK)) {
#ifdef DEBUG_CONFIG_ERROR
		printk(KERN_INFO "%s: %s failed; status = 0x%x\n",
		       dev->name, str, cb.ac_status);
#endif
#ifdef DEBUG_I82586_SHOW
		wv_scb_show(ioaddr);
#endif
		return -1;
	}

	/* Ack the status */
	wv_ack(dev);

	return 0;
}

/*------------------------------------------------------------------*/
/*
 * Configuration commands completion interrupt.
 * Check if done, and if OK.
 */
static int
wv_config_complete(struct net_device * dev, unsigned long ioaddr, net_local * lp)
{
	unsigned short mcs_addr;
	unsigned short status;
	int ret;

#ifdef DEBUG_INTERRUPT_TRACE
	printk(KERN_DEBUG "%s: ->wv_config_complete()\n", dev->name);
#endif

	mcs_addr = lp->tx_first_in_use + sizeof(ac_tx_t) + sizeof(ac_nop_t)
	    + sizeof(tbd_t) + sizeof(ac_cfg_t) + sizeof(ac_ias_t);

	/* Read the status of the last command (set mc list). */
	obram_read(ioaddr, acoff(mcs_addr, ac_status),
		   (unsigned char *) &status, sizeof(status));

	/* If not completed -> exit */
	if ((status & AC_SFLD_C) == 0)
		ret = 0;	/* Not ready to be scrapped */
	else {
#ifdef DEBUG_CONFIG_ERROR
		unsigned short cfg_addr;
		unsigned short ias_addr;

		/* Check mc_config command */
		if ((status & AC_SFLD_OK) != AC_SFLD_OK)
			printk(KERN_INFO
			       "%s: wv_config_complete(): set_multicast_address failed; status = 0x%x\n",
			       dev->name, status);

		/* check ia-config command */
		ias_addr = mcs_addr - sizeof(ac_ias_t);
		obram_read(ioaddr, acoff(ias_addr, ac_status),
			   (unsigned char *) &status, sizeof(status));
		if ((status & AC_SFLD_OK) != AC_SFLD_OK)
			printk(KERN_INFO
			       "%s: wv_config_complete(): set_MAC_address failed; status = 0x%x\n",
			       dev->name, status);

		/* Check config command. */
		cfg_addr = ias_addr - sizeof(ac_cfg_t);
		obram_read(ioaddr, acoff(cfg_addr, ac_status),
			   (unsigned char *) &status, sizeof(status));
		if ((status & AC_SFLD_OK) != AC_SFLD_OK)
			printk(KERN_INFO
			       "%s: wv_config_complete(): configure failed; status = 0x%x\n",
			       dev->name, status);
#endif	/* DEBUG_CONFIG_ERROR */

		ret = 1;	/* Ready to be scrapped */
	}

#ifdef DEBUG_INTERRUPT_TRACE
	printk(KERN_DEBUG "%s: <-wv_config_complete() - %d\n", dev->name,
	       ret);
#endif
	return ret;
}

/*------------------------------------------------------------------*/
/*
 * Command completion interrupt.
 * Reclaim as many freed tx buffers as we can.
 * (called in wavelan_interrupt()).
 * Note : the spinlock is already grabbed for us.
 */
static int wv_complete(struct net_device * dev, unsigned long ioaddr, net_local * lp)
{
	int nreaped = 0;

#ifdef DEBUG_INTERRUPT_TRACE
	printk(KERN_DEBUG "%s: ->wv_complete()\n", dev->name);
#endif

	/* Loop on all the transmit buffers */
	while (lp->tx_first_in_use != I82586NULL) {
		unsigned short tx_status;

		/* Read the first transmit buffer */
		obram_read(ioaddr, acoff(lp->tx_first_in_use, ac_status),
			   (unsigned char *) &tx_status,
			   sizeof(tx_status));

		/* If not completed -> exit */
		if ((tx_status & AC_SFLD_C) == 0)
			break;

		/* Hack for reconfiguration */
		if (tx_status == 0xFFFF)
			if (!wv_config_complete(dev, ioaddr, lp))
				break;	/* Not completed */

		/* We now remove this buffer */
		nreaped++;
		--lp->tx_n_in_use;

/*
if (lp->tx_n_in_use > 0)
	printk("%c", "0123456789abcdefghijk"[lp->tx_n_in_use]);
*/

		/* Was it the last one? */
		if (lp->tx_n_in_use <= 0)
			lp->tx_first_in_use = I82586NULL;
		else {
			/* Next one in the chain */
			lp->tx_first_in_use += TXBLOCKZ;
			if (lp->tx_first_in_use >=
			    OFFSET_CU +
			    NTXBLOCKS * TXBLOCKZ) lp->tx_first_in_use -=
				    NTXBLOCKS * TXBLOCKZ;
		}

		/* Hack for reconfiguration */
		if (tx_status == 0xFFFF)
			continue;

		/* Now, check status of the finished command */
		if (tx_status & AC_SFLD_OK) {
			int ncollisions;

			lp->stats.tx_packets++;
			ncollisions = tx_status & AC_SFLD_MAXCOL;
			lp->stats.collisions += ncollisions;
#ifdef DEBUG_TX_INFO
			if (ncollisions > 0)
				printk(KERN_DEBUG
				       "%s: wv_complete(): tx completed after %d collisions.\n",
				       dev->name, ncollisions);
#endif
		} else {
			lp->stats.tx_errors++;
			if (tx_status & AC_SFLD_S10) {
				lp->stats.tx_carrier_errors++;
#ifdef DEBUG_TX_FAIL
				printk(KERN_DEBUG
				       "%s: wv_complete(): tx error: no CS.\n",
				       dev->name);
#endif
			}
			if (tx_status & AC_SFLD_S9) {
				lp->stats.tx_carrier_errors++;
#ifdef DEBUG_TX_FAIL
				printk(KERN_DEBUG
				       "%s: wv_complete(): tx error: lost CTS.\n",
				       dev->name);
#endif
			}
			if (tx_status & AC_SFLD_S8) {
				lp->stats.tx_fifo_errors++;
#ifdef DEBUG_TX_FAIL
				printk(KERN_DEBUG
				       "%s: wv_complete(): tx error: slow DMA.\n",
				       dev->name);
#endif
			}
			if (tx_status & AC_SFLD_S6) {
				lp->stats.tx_heartbeat_errors++;
#ifdef DEBUG_TX_FAIL
				printk(KERN_DEBUG
				       "%s: wv_complete(): tx error: heart beat.\n",
				       dev->name);
#endif
			}
			if (tx_status & AC_SFLD_S5) {
				lp->stats.tx_aborted_errors++;
#ifdef DEBUG_TX_FAIL
				printk(KERN_DEBUG
				       "%s: wv_complete(): tx error: too many collisions.\n",
				       dev->name);
#endif
			}
		}

#ifdef DEBUG_TX_INFO
		printk(KERN_DEBUG
		       "%s: wv_complete(): tx completed, tx_status 0x%04x\n",
		       dev->name, tx_status);
#endif
	}

#ifdef DEBUG_INTERRUPT_INFO
	if (nreaped > 1)
		printk(KERN_DEBUG "%s: wv_complete(): reaped %d\n",
		       dev->name, nreaped);
#endif

	/*
	 * Inform upper layers.
	 */
	if (lp->tx_n_in_use < NTXBLOCKS - 1) {
		netif_wake_queue(dev);
	}
#ifdef DEBUG_INTERRUPT_TRACE
	printk(KERN_DEBUG "%s: <-wv_complete()\n", dev->name);
#endif
	return nreaped;
}

/*------------------------------------------------------------------*/
/*
 * Reconfigure the i82586, or at least ask for it.
 * Because wv_82586_config uses a transmission buffer, we must do it
 * when we are sure that there is one left, so we do it now
 * or in wavelan_packet_xmit() (I can't find any better place,
 * wavelan_interrupt is not an option), so you may experience
 * delays sometimes.
 */
static void wv_82586_reconfig(struct net_device * dev)
{
	net_local *lp = (net_local *) dev->priv;
	unsigned long flags;

	/* Arm the flag, will be cleard in wv_82586_config() */
	lp->reconfig_82586 = 1;

	/* Check if we can do it now ! */
	if((netif_running(dev)) && !(netif_queue_stopped(dev))) {
		spin_lock_irqsave(&lp->spinlock, flags);
		/* May fail */
		wv_82586_config(dev);
		spin_unlock_irqrestore(&lp->spinlock, flags);
	}
	else {
#ifdef DEBUG_CONFIG_INFO
		printk(KERN_DEBUG
		       "%s: wv_82586_reconfig(): delayed (state = %lX)\n",
			       dev->name, dev->state);
#endif
	}
}

/********************* DEBUG & INFO SUBROUTINES *********************/
/*
 * This routine is used in the code to show information for debugging.
 * Most of the time, it dumps the contents of hardware structures.
 */

#ifdef DEBUG_PSA_SHOW
/*------------------------------------------------------------------*/
/*
 * Print the formatted contents of the Parameter Storage Area.
 */
static void wv_psa_show(psa_t * p)
{
	printk(KERN_DEBUG "##### WaveLAN PSA contents: #####\n");
	printk(KERN_DEBUG "psa_io_base_addr_1: 0x%02X %02X %02X %02X\n",
	       p->psa_io_base_addr_1,
	       p->psa_io_base_addr_2,
	       p->psa_io_base_addr_3, p->psa_io_base_addr_4);
	printk(KERN_DEBUG "psa_rem_boot_addr_1: 0x%02X %02X %02X\n",
	       p->psa_rem_boot_addr_1,
	       p->psa_rem_boot_addr_2, p->psa_rem_boot_addr_3);
	printk(KERN_DEBUG "psa_holi_params: 0x%02x, ", p->psa_holi_params);
	printk("psa_int_req_no: %d\n", p->psa_int_req_no);
#ifdef DEBUG_SHOW_UNUSED
	printk(KERN_DEBUG
	       "psa_unused0[]: %02X:%02X:%02X:%02X:%02X:%02X:%02X\n",
	       p->psa_unused0[0], p->psa_unused0[1], p->psa_unused0[2],
	       p->psa_unused0[3], p->psa_unused0[4], p->psa_unused0[5],
	       p->psa_unused0[6]);
#endif				/* DEBUG_SHOW_UNUSED */
	printk(KERN_DEBUG
	       "psa_univ_mac_addr[]: %02x:%02x:%02x:%02x:%02x:%02x\n",
	       p->psa_univ_mac_addr[0], p->psa_univ_mac_addr[1],
	       p->psa_univ_mac_addr[2], p->psa_univ_mac_addr[3],
	       p->psa_univ_mac_addr[4], p->psa_univ_mac_addr[5]);
	printk(KERN_DEBUG
	       "psa_local_mac_addr[]: %02x:%02x:%02x:%02x:%02x:%02x\n",
	       p->psa_local_mac_addr[0], p->psa_local_mac_addr[1],
	       p->psa_local_mac_addr[2], p->psa_local_mac_addr[3],
	       p->psa_local_mac_addr[4], p->psa_local_mac_addr[5]);
	printk(KERN_DEBUG "psa_univ_local_sel: %d, ",
	       p->psa_univ_local_sel);
	printk("psa_comp_number: %d, ", p->psa_comp_number);
	printk("psa_thr_pre_set: 0x%02x\n", p->psa_thr_pre_set);
	printk(KERN_DEBUG "psa_feature_select/decay_prm: 0x%02x, ",
	       p->psa_feature_select);
	printk("psa_subband/decay_update_prm: %d\n", p->psa_subband);
	printk(KERN_DEBUG "psa_quality_thr: 0x%02x, ", p->psa_quality_thr);
	printk("psa_mod_delay: 0x%02x\n", p->psa_mod_delay);
	printk(KERN_DEBUG "psa_nwid: 0x%02x%02x, ", p->psa_nwid[0],
	       p->psa_nwid[1]);
	printk("psa_nwid_select: %d\n", p->psa_nwid_select);
	printk(KERN_DEBUG "psa_encryption_select: %d, ",
	       p->psa_encryption_select);
	printk
	    ("psa_encryption_key[]: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
	     p->psa_encryption_key[0], p->psa_encryption_key[1],
	     p->psa_encryption_key[2], p->psa_encryption_key[3],
	     p->psa_encryption_key[4], p->psa_encryption_key[5],
	     p->psa_encryption_key[6], p->psa_encryption_key[7]);
	printk(KERN_DEBUG "psa_databus_width: %d\n", p->psa_databus_width);
	printk(KERN_DEBUG "psa_call_code/auto_squelch: 0x%02x, ",
	       p->psa_call_code[0]);
	printk
	    ("psa_call_code[]: %02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X\n",
	     p->psa_call_code[0], p->psa_call_code[1], p->psa_call_code[2],
	     p->psa_call_code[3], p->psa_call_code[4], p->psa_call_code[5],
	     p->psa_call_code[6], p->psa_call_code[7]);
#ifdef DEBUG_SHOW_UNUSED
	printk(KERN_DEBUG "psa_reserved[]: %02X:%02X:%02X:%02X\n",
	       p->psa_reserved[0],
	       p->psa_reserved[1], p->psa_reserved[2], p->psa_reserved[3]);
#endif				/* DEBUG_SHOW_UNUSED */
	printk(KERN_DEBUG "psa_conf_status: %d, ", p->psa_conf_status);
	printk("psa_crc: 0x%02x%02x, ", p->psa_crc[0], p->psa_crc[1]);
	printk("psa_crc_status: 0x%02x\n", p->psa_crc_status);
}				/* wv_psa_show */
#endif				/* DEBUG_PSA_SHOW */

#ifdef DEBUG_MMC_SHOW
/*------------------------------------------------------------------*/
/*
 * Print the formatted status of the Modem Management Controller.
 * This function needs to be completed.
 */
static void wv_mmc_show(struct net_device * dev)
{
	unsigned long ioaddr = dev->base_addr;
	net_local *lp = (net_local *) dev->priv;
	mmr_t m;

	/* Basic check */
	if (hasr_read(ioaddr) & HASR_NO_CLK) {
		printk(KERN_WARNING
		       "%s: wv_mmc_show: modem not connected\n",
		       dev->name);
		return;
	}

	/* Read the mmc */
	mmc_out(ioaddr, mmwoff(0, mmw_freeze), 1);
	mmc_read(ioaddr, 0, (u8 *) & m, sizeof(m));
	mmc_out(ioaddr, mmwoff(0, mmw_freeze), 0);

	/* Don't forget to update statistics */
	lp->wstats.discard.nwid +=
	    (m.mmr_wrong_nwid_h << 8) | m.mmr_wrong_nwid_l;

	printk(KERN_DEBUG "##### WaveLAN modem status registers: #####\n");
#ifdef DEBUG_SHOW_UNUSED
	printk(KERN_DEBUG
	       "mmc_unused0[]: %02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X\n",
	       m.mmr_unused0[0], m.mmr_unused0[1], m.mmr_unused0[2],
	       m.mmr_unused0[3], m.mmr_unused0[4], m.mmr_unused0[5],
	       m.mmr_unused0[6], m.mmr_unused0[7]);
#endif				/* DEBUG_SHOW_UNUSED */
	printk(KERN_DEBUG "Encryption algorithm: %02X - Status: %02X\n",
	       m.mmr_des_avail, m.mmr_des_status);
#ifdef DEBUG_SHOW_UNUSED
	printk(KERN_DEBUG "mmc_unused1[]: %02X:%02X:%02X:%02X:%02X\n",
	       m.mmr_unused1[0],
	       m.mmr_unused1[1],
	       m.mmr_unused1[2], m.mmr_unused1[3], m.mmr_unused1[4]);
#endif				/* DEBUG_SHOW_UNUSED */
	printk(KERN_DEBUG "dce_status: 0x%x [%s%s%s%s]\n",
	       m.mmr_dce_status,
	       (m.
		mmr_dce_status & MMR_DCE_STATUS_RX_BUSY) ?
	       "energy detected," : "",
	       (m.
		mmr_dce_status & MMR_DCE_STATUS_LOOPT_IND) ?
	       "loop test indicated," : "",
	       (m.
		mmr_dce_status & MMR_DCE_STATUS_TX_BUSY) ?
	       "transmitter on," : "",
	       (m.
		mmr_dce_status & MMR_DCE_STATUS_JBR_EXPIRED) ?
	       "jabber timer expired," : "");
	printk(KERN_DEBUG "Dsp ID: %02X\n", m.mmr_dsp_id);
#ifdef DEBUG_SHOW_UNUSED
	printk(KERN_DEBUG "mmc_unused2[]: %02X:%02X\n",
	       m.mmr_unused2[0], m.mmr_unused2[1]);
#endif				/* DEBUG_SHOW_UNUSED */
	printk(KERN_DEBUG "# correct_nwid: %d, # wrong_nwid: %d\n",
	       (m.mmr_correct_nwid_h << 8) | m.mmr_correct_nwid_l,
	       (m.mmr_wrong_nwid_h << 8) | m.mmr_wrong_nwid_l);
	printk(KERN_DEBUG "thr_pre_set: 0x%x [current signal %s]\n",
	       m.mmr_thr_pre_set & MMR_THR_PRE_SET,
	       (m.
		mmr_thr_pre_set & MMR_THR_PRE_SET_CUR) ? "above" :
	       "below");
	printk(KERN_DEBUG "signal_lvl: %d [%s], ",
	       m.mmr_signal_lvl & MMR_SIGNAL_LVL,
	       (m.
		mmr_signal_lvl & MMR_SIGNAL_LVL_VALID) ? "new msg" :
	       "no new msg");
	printk("silence_lvl: %d [%s], ",
	       m.mmr_silence_lvl & MMR_SILENCE_LVL,
	       (m.
		mmr_silence_lvl & MMR_SILENCE_LVL_VALID) ? "update done" :
	       "no new update");
	printk("sgnl_qual: 0x%x [%s]\n", m.mmr_sgnl_qual & MMR_SGNL_QUAL,
	       (m.
		mmr_sgnl_qual & MMR_SGNL_QUAL_ANT) ? "Antenna 1" :
	       "Antenna 0");
#ifdef DEBUG_SHOW_UNUSED
	printk(KERN_DEBUG "netw_id_l: %x\n", m.mmr_netw_id_l);
#endif				/* DEBUG_SHOW_UNUSED */
}				/* wv_mmc_show */
#endif				/* DEBUG_MMC_SHOW */

#ifdef DEBUG_I82586_SHOW
/*------------------------------------------------------------------*/
/*
 * Print the last block of the i82586 memory.
 */
static void wv_scb_show(unsigned long ioaddr)
{
	scb_t scb;

	obram_read(ioaddr, OFFSET_SCB, (unsigned char *) &scb,
		   sizeof(scb));

	printk(KERN_DEBUG "##### WaveLAN system control block: #####\n");

	printk(KERN_DEBUG "status: ");
	printk("stat 0x%x[%s%s%s%s] ",
	       (scb.
		scb_status & (SCB_ST_CX | SCB_ST_FR | SCB_ST_CNA |
			      SCB_ST_RNR)) >> 12,
	       (scb.
		scb_status & SCB_ST_CX) ? "command completion interrupt," :
	       "", (scb.scb_status & SCB_ST_FR) ? "frame received," : "",
	       (scb.
		scb_status & SCB_ST_CNA) ? "command unit not active," : "",
	       (scb.
		scb_status & SCB_ST_RNR) ? "receiving unit not ready," :
	       "");
	printk("cus 0x%x[%s%s%s] ", (scb.scb_status & SCB_ST_CUS) >> 8,
	       ((scb.scb_status & SCB_ST_CUS) ==
		SCB_ST_CUS_IDLE) ? "idle" : "",
	       ((scb.scb_status & SCB_ST_CUS) ==
		SCB_ST_CUS_SUSP) ? "suspended" : "",
	       ((scb.scb_status & SCB_ST_CUS) ==
		SCB_ST_CUS_ACTV) ? "active" : "");
	printk("rus 0x%x[%s%s%s%s]\n", (scb.scb_status & SCB_ST_RUS) >> 4,
	       ((scb.scb_status & SCB_ST_RUS) ==
		SCB_ST_RUS_IDLE) ? "idle" : "",
	       ((scb.scb_status & SCB_ST_RUS) ==
		SCB_ST_RUS_SUSP) ? "suspended" : "",
	       ((scb.scb_status & SCB_ST_RUS) ==
		SCB_ST_RUS_NRES) ? "no resources" : "",
	       ((scb.scb_status & SCB_ST_RUS) ==
		SCB_ST_RUS_RDY) ? "ready" : "");

	printk(KERN_DEBUG "command: ");
	printk("ack 0x%x[%s%s%s%s] ",
	       (scb.
		scb_command & (SCB_CMD_ACK_CX | SCB_CMD_ACK_FR |
			       SCB_CMD_ACK_CNA | SCB_CMD_ACK_RNR)) >> 12,
	       (scb.
		scb_command & SCB_CMD_ACK_CX) ? "ack cmd completion," : "",
	       (scb.
		scb_command & SCB_CMD_ACK_FR) ? "ack frame received," : "",
	       (scb.
		scb_command & SCB_CMD_ACK_CNA) ? "ack CU not active," : "",
	       (scb.
		scb_command & SCB_CMD_ACK_RNR) ? "ack RU not ready," : "");
	printk("cuc 0x%x[%s%s%s%s%s] ",
	       (scb.scb_command & SCB_CMD_CUC) >> 8,
	       ((scb.scb_command & SCB_CMD_CUC) ==
		SCB_CMD_CUC_NOP) ? "nop" : "",
	       ((scb.scb_command & SCB_CMD_CUC) ==
		SCB_CMD_CUC_GO) ? "start cbl_offset" : "",
	       ((scb.scb_command & SCB_CMD_CUC) ==
		SCB_CMD_CUC_RES) ? "resume execution" : "",
	       ((scb.scb_command & SCB_CMD_CUC) ==
		SCB_CMD_CUC_SUS) ? "suspend execution" : "",
	       ((scb.scb_command & SCB_CMD_CUC) ==
		SCB_CMD_CUC_ABT) ? "abort execution" : "");
	printk("ruc 0x%x[%s%s%s%s%s]\n",
	       (scb.scb_command & SCB_CMD_RUC) >> 4,
	       ((scb.scb_command & SCB_CMD_RUC) ==
		SCB_CMD_RUC_NOP) ? "nop" : "",
	       ((scb.scb_command & SCB_CMD_RUC) ==
		SCB_CMD_RUC_GO) ? "start rfa_offset" : "",
	       ((scb.scb_command & SCB_CMD_RUC) ==
		SCB_CMD_RUC_RES) ? "resume reception" : "",
	       ((scb.scb_command & SCB_CMD_RUC) ==
		SCB_CMD_RUC_SUS) ? "suspend reception" : "",
	       ((scb.scb_command & SCB_CMD_RUC) ==
		SCB_CMD_RUC_ABT) ? "abort reception" : "");

	printk(KERN_DEBUG "cbl_offset 0x%x ", scb.scb_cbl_offset);
	printk("rfa_offset 0x%x\n", scb.scb_rfa_offset);

	printk(KERN_DEBUG "crcerrs %d ", scb.scb_crcerrs);
	printk("alnerrs %d ", scb.scb_alnerrs);
	printk("rscerrs %d ", scb.scb_rscerrs);
	printk("ovrnerrs %d\n", scb.scb_ovrnerrs);
}

/*------------------------------------------------------------------*/
/*
 * Print the formatted status of the i82586's receive unit.
 */
static void wv_ru_show(struct net_device * dev)
{
	/* net_local *lp = (net_local *) dev->priv; */

	printk(KERN_DEBUG
	       "##### WaveLAN i82586 receiver unit status: #####\n");
	printk(KERN_DEBUG "ru:");
	/*
	 * Not implemented yet
	 */
	printk("\n");
}				/* wv_ru_show */

/*------------------------------------------------------------------*/
/*
 * Display info about one control block of the i82586 memory.
 */
static void wv_cu_show_one(struct net_device * dev, net_local * lp, int i, u16 p)
{
	unsigned long ioaddr;
	ac_tx_t actx;

	ioaddr = dev->base_addr;

	printk("%d: 0x%x:", i, p);

	obram_read(ioaddr, p, (unsigned char *) &actx, sizeof(actx));
	printk(" status=0x%x,", actx.tx_h.ac_status);
	printk(" command=0x%x,", actx.tx_h.ac_command);

	/*
	   {
	   tbd_t      tbd;

	   obram_read(ioaddr, actx.tx_tbd_offset, (unsigned char *)&tbd, sizeof(tbd));
	   printk(" tbd_status=0x%x,", tbd.tbd_status);
	   }
	 */

	printk("|");
}

/*------------------------------------------------------------------*/
/*
 * Print status of the command unit of the i82586.
 */
static void wv_cu_show(struct net_device * dev)
{
	net_local *lp = (net_local *) dev->priv;
	unsigned int i;
	u16 p;

	printk(KERN_DEBUG
	       "##### WaveLAN i82586 command unit status: #####\n");

	printk(KERN_DEBUG);
	for (i = 0, p = lp->tx_first_in_use; i < NTXBLOCKS; i++) {
		wv_cu_show_one(dev, lp, i, p);

		p += TXBLOCKZ;
		if (p >= OFFSET_CU + NTXBLOCKS * TXBLOCKZ)
			p -= NTXBLOCKS * TXBLOCKZ;
	}
	printk("\n");
}
#endif				/* DEBUG_I82586_SHOW */

#ifdef DEBUG_DEVICE_SHOW
/*------------------------------------------------------------------*/
/*
 * Print the formatted status of the WaveLAN PCMCIA device driver.
 */
static void wv_dev_show(struct net_device * dev)
{
	printk(KERN_DEBUG "dev:");
	printk(" state=%lX,", dev->state);
	printk(" trans_start=%ld,", dev->trans_start);
	printk(" flags=0x%x,", dev->flags);
	printk("\n");
}				/* wv_dev_show */

/*------------------------------------------------------------------*/
/*
 * Print the formatted status of the WaveLAN PCMCIA device driver's
 * private information.
 */
static void wv_local_show(struct net_device * dev)
{
	net_local *lp;

	lp = (net_local *) dev->priv;

	printk(KERN_DEBUG "local:");
	printk(" tx_n_in_use=%d,", lp->tx_n_in_use);
	printk(" hacr=0x%x,", lp->hacr);
	printk(" rx_head=0x%x,", lp->rx_head);
	printk(" rx_last=0x%x,", lp->rx_last);
	printk(" tx_first_free=0x%x,", lp->tx_first_free);
	printk(" tx_first_in_use=0x%x,", lp->tx_first_in_use);
	printk("\n");
}				/* wv_local_show */
#endif				/* DEBUG_DEVICE_SHOW */

#if defined(DEBUG_RX_INFO) || defined(DEBUG_TX_INFO)
/*------------------------------------------------------------------*/
/*
 * Dump packet header (and content if necessary) on the screen
 */
static inline void wv_packet_info(u8 * p,	/* Packet to dump */
				  int length,	/* Length of the packet */
				  char *msg1,	/* Name of the device */
				  char *msg2)
{				/* Name of the function */
	int i;
	int maxi;

	printk(KERN_DEBUG
	       "%s: %s(): dest %02X:%02X:%02X:%02X:%02X:%02X, length %d\n",
	       msg1, msg2, p[0], p[1], p[2], p[3], p[4], p[5], length);
	printk(KERN_DEBUG
	       "%s: %s(): src %02X:%02X:%02X:%02X:%02X:%02X, type 0x%02X%02X\n",
	       msg1, msg2, p[6], p[7], p[8], p[9], p[10], p[11], p[12],
	       p[13]);

#ifdef DEBUG_PACKET_DUMP

	printk(KERN_DEBUG "data=\"");

	if ((maxi = length) > DEBUG_PACKET_DUMP)
		maxi = DEBUG_PACKET_DUMP;
	for (i = 14; i < maxi; i++)
		if (p[i] >= ' ' && p[i] <= '~')
			printk(" %c", p[i]);
		else
			printk("%02X", p[i]);
	if (maxi < length)
		printk("..");
	printk("\"\n");
	printk(KERN_DEBUG "\n");
#endif				/* DEBUG_PACKET_DUMP */
}
#endif				/* defined(DEBUG_RX_INFO) || defined(DEBUG_TX_INFO) */

/*------------------------------------------------------------------*/
/*
 * This is the information which is displayed by the driver at startup.
 * There are lots of flags for configuring it to your liking.
 */
static void wv_init_info(struct net_device * dev)
{
	short ioaddr = dev->base_addr;
	net_local *lp = (net_local *) dev->priv;
	psa_t psa;
	int i;

	/* Read the parameter storage area */
	psa_read(ioaddr, lp->hacr, 0, (unsigned char *) &psa, sizeof(psa));

#ifdef DEBUG_PSA_SHOW
	wv_psa_show(&psa);
#endif
#ifdef DEBUG_MMC_SHOW
	wv_mmc_show(dev);
#endif
#ifdef DEBUG_I82586_SHOW
	wv_cu_show(dev);
#endif

#ifdef DEBUG_BASIC_SHOW
	/* Now, let's go for the basic stuff. */
	printk(KERN_NOTICE "%s: WaveLAN at %#x,", dev->name, ioaddr);
	for (i = 0; i < WAVELAN_ADDR_SIZE; i++)
		printk("%s%02X", (i == 0) ? " " : ":", dev->dev_addr[i]);
	printk(", IRQ %d", dev->irq);

	/* Print current network ID. */
	if (psa.psa_nwid_select)
		printk(", nwid 0x%02X-%02X", psa.psa_nwid[0],
		       psa.psa_nwid[1]);
	else
		printk(", nwid off");

	/* If 2.00 card */
	if (!(mmc_in(ioaddr, mmroff(0, mmr_fee_status)) &
	      (MMR_FEE_STATUS_DWLD | MMR_FEE_STATUS_BUSY))) {
		unsigned short freq;

		/* Ask the EEPROM to read the frequency from the first area. */
		fee_read(ioaddr, 0x00, &freq, 1);

		/* Print frequency */
		printk(", 2.00, %ld", (freq >> 6) + 2400L);

		/* Hack! */
		if (freq & 0x20)
			printk(".5");
	} else {
		printk(", PC");
		switch (psa.psa_comp_number) {
		case PSA_COMP_PC_AT_915:
		case PSA_COMP_PC_AT_2400:
			printk("-AT");
			break;
		case PSA_COMP_PC_MC_915:
		case PSA_COMP_PC_MC_2400:
			printk("-MC");
			break;
		case PSA_COMP_PCMCIA_915:
			printk("MCIA");
			break;
		default:
			printk("?");
		}
		printk(", ");
		switch (psa.psa_subband) {
		case PSA_SUBBAND_915:
			printk("915");
			break;
		case PSA_SUBBAND_2425:
			printk("2425");
			break;
		case PSA_SUBBAND_2460:
			printk("2460");
			break;
		case PSA_SUBBAND_2484:
			printk("2484");
			break;
		case PSA_SUBBAND_2430_5:
			printk("2430.5");
			break;
		default:
			printk("?");
		}
	}

	printk(" MHz\n");
#endif				/* DEBUG_BASIC_SHOW */

#ifdef DEBUG_VERSION_SHOW
	/* Print version information */
	printk(KERN_NOTICE "%s", version);
#endif
}				/* wv_init_info */

/********************* IOCTL, STATS & RECONFIG *********************/
/*
 * We found here routines that are called by Linux on different
 * occasions after the configuration and not for transmitting data
 * These may be called when the user use ifconfig, /proc/net/dev
 * or wireless extensions
 */

/*------------------------------------------------------------------*/
/*
 * Get the current Ethernet statistics. This may be called with the
 * card open or closed.
 * Used when the user read /proc/net/dev
 */
static en_stats *wavelan_get_stats(struct net_device * dev)
{
#ifdef DEBUG_IOCTL_TRACE
	printk(KERN_DEBUG "%s: <>wavelan_get_stats()\n", dev->name);
#endif

	return (&((net_local *) dev->priv)->stats);
}

/*------------------------------------------------------------------*/
/*
 * 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 wavelan_set_multicast_list(struct net_device * dev)
{
	net_local *lp = (net_local *) dev->priv;

#ifdef DEBUG_IOCTL_TRACE
	printk(KERN_DEBUG "%s: ->wavelan_set_multicast_list()\n",
	       dev->name);
#endif

#ifdef DEBUG_IOCTL_INFO
	printk(KERN_DEBUG
	       "%s: wavelan_set_multicast_list(): setting Rx mode %02X to %d addresses.\n",
	       dev->name, dev->flags, dev->mc_count);
#endif

	/* Are we asking for promiscuous mode,
	 * or all multicast addresses (we don't have that!)
	 * or too many multicast addresses for the hardware filter? */
	if ((dev->flags & IFF_PROMISC) ||
	    (dev->flags & IFF_ALLMULTI) ||
	    (dev->mc_count > I82586_MAX_MULTICAST_ADDRESSES)) {
		/*
		 * Enable promiscuous mode: receive all packets.
		 */
		if (!lp->promiscuous) {
			lp->promiscuous = 1;
			lp->mc_count = 0;

			wv_82586_reconfig(dev);

			/* Tell the kernel that we are doing a really bad job. */
			dev->flags |= IFF_PROMISC;
		}
	} else
		/* Are there multicast addresses to send? */
	if (dev->mc_list != (struct dev_mc_list *) NULL) {
		/*
		 * Disable promiscuous mode, but receive all packets
		 * in multicast list
		 */
#ifdef MULTICAST_AVOID
		if (lp->promiscuous || (dev->mc_count != lp->mc_count))
#endif
		{
			lp->promiscuous = 0;
			lp->mc_count = dev->mc_count;

			wv_82586_reconfig(dev);
		}
	} else {
		/*
		 * Switch to normal mode: disable promiscuous mode and 
		 * clear the multicast list.
		 */
		if (lp->promiscuous || lp->mc_count == 0) {
			lp->promiscuous = 0;
			lp->mc_count = 0;

			wv_82586_reconfig(dev);
		}
	}
#ifdef DEBUG_IOCTL_TRACE
	printk(KERN_DEBUG "%s: <-wavelan_set_multicast_list()\n",
	       dev->name);
#endif
}

/*------------------------------------------------------------------*/
/*
 * This function doesn't exist.
 * (Note : it was a nice way to test the reconfigure stuff...)
 */
#ifdef SET_MAC_ADDRESS
static int wavelan_set_mac_address(struct net_device * dev, void *addr)
{
	struct sockaddr *mac = addr;

	/* Copy the address. */
	memcpy(dev->dev_addr, mac->sa_data, WAVELAN_ADDR_SIZE);

	/* Reconfigure the beast. */
	wv_82586_reconfig(dev);

	return 0;
}
#endif				/* SET_MAC_ADDRESS */


/*------------------------------------------------------------------*/
/*
 * Frequency setting (for hardware capable of it)
 * It's a bit complicated and you don't really want to look into it.
 * (called in wavelan_ioctl)
 */
static int wv_set_frequency(unsigned long ioaddr,	/* I/O port of the card */
				   iw_freq * frequency)
{
	const int BAND_NUM = 10;	/* Number of bands */
	long freq = 0L;		/* offset to 2.4 GHz in .5 MHz */
#ifdef DEBUG_IOCTL_INFO
	int i;
#endif

	/* Setting by frequency */
	/* Theoretically, you may set any frequency between
	 * the two limits with a 0.5 MHz precision. In practice,
	 * I don't want you to have trouble with local regulations.
	 */
	if ((frequency->e == 1) &&
	    (frequency->m >= (int) 2.412e8)
	    && (frequency->m <= (int) 2.487e8)) {
		freq = ((frequency->m / 10000) - 24000L) / 5;
	}

	/* Setting by channel (same as wfreqsel) */
	/* Warning: each channel is 22 MHz wide, so some of the channels
	 * will interfere. */
	if ((frequency->e == 0) && (frequency->m < BAND_NUM)) {
		/* Get frequency offset. */
		freq = channel_bands[frequency->m] >> 1;
	}

	/* Verify that the frequency is allowed. */
	if (freq != 0L) {
		u16 table[10];	/* Authorized frequency table */

		/* Read the frequency table. */
		fee_read(ioaddr, 0x71, table, 10);

#ifdef DEBUG_IOCTL_INFO
		printk(KERN_DEBUG "Frequency table: ");
		for (i = 0; i < 10; i++) {
			printk(" %04X", table[i]);
		}
		printk("\n");
#endif

		/* Look in the table to see whether the frequency is allowed. */
		if (!(table[9 - ((freq - 24) / 16)] &
		      (1 << ((freq - 24) % 16)))) return -EINVAL;	/* not allowed */
	} else
		return -EINVAL;

	/* if we get a usable frequency */
	if (freq != 0L) {
		unsigned short area[16];
		unsigned short dac[2];
		unsigned short area_verify[16];
		unsigned short dac_verify[2];
		/* Corresponding gain (in the power adjust value table)
		 * See AT&T WaveLAN Data Manual, REF 407-024689/E, page 3-8
		 * and WCIN062D.DOC, page 6.2.9. */
		unsigned short power_limit[] = { 40, 80, 120, 160, 0 };
		int power_band = 0;	/* Selected band */
		unsigned short power_adjust;	/* Correct value */

		/* Search for the gain. */
		power_band = 0;
		while ((freq > power_limit[power_band]) &&
		       (power_limit[++power_band] != 0));

		/* Read the first area. */
		fee_read(ioaddr, 0x00, area, 16);

		/* Read the DAC. */
		fee_read(ioaddr, 0x60, dac, 2);

		/* Read the new power adjust value. */
		fee_read(ioaddr, 0x6B - (power_band >> 1), &power_adjust,
			 1);
		if (power_band & 0x1)
			power_adjust >>= 8;
		else
			power_adjust &= 0xFF;

#ifdef DEBUG_IOCTL_INFO
		printk(KERN_DEBUG "WaveLAN EEPROM Area 1: ");
		for (i = 0; i < 16; i++) {
			printk(" %04X", area[i]);
		}
		printk("\n");

		printk(KERN_DEBUG "WaveLAN EEPROM DAC: %04X %04X\n",
		       dac[0], dac[1]);
#endif

		/* Frequency offset (for info only) */
		area[0] = ((freq << 5) & 0xFFE0) | (area[0] & 0x1F);

		/* Receiver Principle main divider coefficient */
		area[3] = (freq >> 1) + 2400L - 352L;
		area[2] = ((freq & 0x1) << 4) | (area[2] & 0xFFEF);

		/* Transmitter Main divider coefficient */
		area[13] = (freq >> 1) + 2400L;
		area[12] = ((freq & 0x1) << 4) | (area[2] & 0xFFEF);

		/* Other parts of the area are flags, bit streams or unused. */

		/* Set the value in the DAC. */
		dac[1] = ((power_adjust >> 1) & 0x7F) | (dac[1] & 0xFF80);
		dac[0] = ((power_adjust & 0x1) << 4) | (dac[0] & 0xFFEF);

		/* Write the first area. */
		fee_write(ioaddr, 0x00, area, 16);

		/* Write the DAC. */
		fee_write(ioaddr, 0x60, dac, 2);

		/* We now should verify here that the writing of the EEPROM went OK. */

		/* Reread the first area. */
		fee_read(ioaddr, 0x00, area_verify, 16);

		/* Reread the DAC. */
		fee_read(ioaddr, 0x60, dac_verify, 2);

		/* Compare. */
		if (memcmp(area, area_verify, 16 * 2) ||
		    memcmp(dac, dac_verify, 2 * 2)) {
#ifdef DEBUG_IOCTL_ERROR
			printk(KERN_INFO
			       "WaveLAN: wv_set_frequency: unable to write new frequency to EEPROM(?).\n");
#endif
			return -EOPNOTSUPP;
		}

		/* We must download the frequency parameters to the
		 * synthesizers (from the EEPROM - area 1)
		 * Note: as the EEPROM is automatically decremented, we set the end
		 * if the area... */
		mmc_out(ioaddr, mmwoff(0, mmw_fee_addr), 0x0F);
		mmc_out(ioaddr, mmwoff(0, mmw_fee_ctrl),
			MMW_FEE_CTRL_READ | MMW_FEE_CTRL_DWLD);

		/* Wait until the download is finished. */
		fee_wait(ioaddr, 100, 100);

		/* We must now download the power adjust value (gain) to
		 * the synthesizers (from the EEPROM - area 7 - DAC). */
		mmc_out(ioaddr, mmwoff(0, mmw_fee_addr), 0x61);
		mmc_out(ioaddr, mmwoff(0, mmw_fee_ctrl),
			MMW_FEE_CTRL_READ | MMW_FEE_CTRL_DWLD);

		/* Wait for the download to finish. */
		fee_wait(ioaddr, 100, 100);

#ifdef DEBUG_IOCTL_INFO
		/* Verification of what we have done */

		printk(KERN_DEBUG "WaveLAN EEPROM Area 1: ");
		for (i = 0; i < 16; i++) {
			printk(" %04X", area_verify[i]);
		}
		printk("\n");

		printk(KERN_DEBUG "WaveLAN EEPROM DAC:  %04X %04X\n",
		       dac_verify[0], dac_verify[1]);
#endif

		return 0;
	} else
		return -EINVAL;	/* Bah, never get there... */
}

/*------------------------------------------------------------------*/
/*
 * Give the list of available frequencies.
 */
static int wv_frequency_list(unsigned long ioaddr,	/* I/O port of the card */
				    iw_freq * list,	/* List of frequencies to fill */
				    int max)
{				/* Maximum number of frequencies */
	u16 table[10];	/* Authorized frequency table */
	long freq = 0L;		/* offset to 2.4 GHz in .5 MHz + 12 MHz */
	int i;			/* index in the table */
	int c = 0;		/* Channel number */

	/* Read the frequency table. */
	fee_read(ioaddr, 0x71 /* frequency table */ , table, 10);

	/* Check all frequencies. */
	i = 0;
	for (freq = 0; freq < 150; freq++)
		/* Look in the table if the frequency is allowed */
		if (table[9 - (freq / 16)] & (1 << (freq % 16))) {
			/* Compute approximate channel number */
			while ((c < ARRAY_SIZE(channel_bands)) &&
				(((channel_bands[c] >> 1) - 24) < freq)) 
				c++;
			list[i].i = c;	/* Set the list index */

			/* put in the list */
			list[i].m = (((freq + 24) * 5) + 24000L) * 10000;
			list[i++].e = 1;

			/* Check number. */
			if (i >= max)
				return (i);
		}

	return (i);
}

#ifdef IW_WIRELESS_SPY
/*------------------------------------------------------------------*/
/*
 * Gather wireless spy statistics:  for each packet, compare the source
 * address with our list, and if they match, get the statistics.
 * Sorry, but this function really needs the wireless extensions.
 */
static inline void wl_spy_gather(struct net_device * dev,
				 u8 *	mac,	/* MAC address */
				 u8 *	stats)	/* Statistics to gather */
{
	struct iw_quality wstats;

	wstats.qual = stats[2] & MMR_SGNL_QUAL;
	wstats.level = stats[0] & MMR_SIGNAL_LVL;
	wstats.noise = stats[1] & MMR_SILENCE_LVL;
	wstats.updated = 0x7;

	/* Update spy records */
	wireless_spy_update(dev, mac, &wstats);
}
#endif /* IW_WIRELESS_SPY */

#ifdef HISTOGRAM
/*------------------------------------------------------------------*/
/*
 * This function calculates a histogram of the signal level.
 * As the noise is quite constant, it's like doing it on the SNR.
 * We have defined a set of interval (lp->his_range), and each time
 * the level goes in that interval, we increment the count (lp->his_sum).
 * With this histogram you may detect if one WaveLAN is really weak,
 * or you may also calculate the mean and standard deviation of the level.
 */
static inline void wl_his_gather(struct net_device * dev, u8 * stats)
{				/* Statistics to gather */
	net_local *lp = (net_local *) dev->priv;
	u8 level = stats[0] & MMR_SIGNAL_LVL;
	int i;

	/* Find the correct interval. */
	i = 0;
	while ((i < (lp->his_number - 1))
	       && (level >= lp->his_range[i++]));

	/* Increment interval counter. */
	(lp->his_sum[i])++;
}
#endif /* HISTOGRAM */

/*------------------------------------------------------------------*/
/*
 * Wireless Handler : get protocol name
 */
static int wavelan_get_name(struct net_device *dev,
			    struct iw_request_info *info,
			    union iwreq_data *wrqu,
			    char *extra)
{
	strcpy(wrqu->name, "WaveLAN");
	return 0;
}

/*------------------------------------------------------------------*/
/*
 * Wireless Handler : set NWID
 */
static int wavelan_set_nwid(struct net_device *dev,
			    struct iw_request_info *info,
			    union iwreq_data *wrqu,
			    char *extra)
{
	unsigned long ioaddr = dev->base_addr;
	net_local *lp = (net_local *) dev->priv;	/* lp is not unused */
	psa_t psa;
	mm_t m;
	unsigned long flags;
	int ret = 0;

	/* Disable interrupts and save flags. */
	spin_lock_irqsave(&lp->spinlock, flags);
	
	/* Set NWID in WaveLAN. */
	if (!wrqu->nwid.disabled) {
		/* Set NWID in psa */
		psa.psa_nwid[0] = (wrqu->nwid.value & 0xFF00) >> 8;
		psa.psa_nwid[1] = wrqu->nwid.value & 0xFF;
		psa.psa_nwid_select = 0x01;
		psa_write(ioaddr, lp->hacr,
			  (char *) psa.psa_nwid - (char *) &psa,
			  (unsigned char *) psa.psa_nwid, 3);

		/* Set NWID in mmc. */
		m.w.mmw_netw_id_l = psa.psa_nwid[1];
		m.w.mmw_netw_id_h = psa.psa_nwid[0];
		mmc_write(ioaddr,
			  (char *) &m.w.mmw_netw_id_l -
			  (char *) &m,
			  (unsigned char *) &m.w.mmw_netw_id_l, 2);
		mmc_out(ioaddr, mmwoff(0, mmw_loopt_sel), 0x00);
	} else {
		/* Disable NWID in the psa. */
		psa.psa_nwid_select = 0x00;
		psa_write(ioaddr, lp->hacr,
			  (char *) &psa.psa_nwid_select -
			  (char *) &psa,
			  (unsigned char *) &psa.psa_nwid_select,
			  1);

		/* Disable NWID in the mmc (no filtering). */
		mmc_out(ioaddr, mmwoff(0, mmw_loopt_sel),
			MMW_LOOPT_SEL_DIS_NWID);
	}
	/* update the Wavelan checksum */
	update_psa_checksum(dev, ioaddr, lp->hacr);

	/* Enable interrupts and restore flags. */
	spin_unlock_irqrestore(&lp->spinlock, flags);

	return ret;
}

/*------------------------------------------------------------------*/
/*
 * Wireless Handler : get NWID 
 */
static int wavelan_get_nwid(struct net_device *dev,
			    struct iw_request_info *info,
			    union iwreq_data *wrqu,
			    char *extra)
{
	unsigned long ioaddr = dev->base_addr;
	net_local *lp = (net_local *) dev->priv;	/* lp is not unused */
	psa_t psa;
	unsigned long flags;
	int ret = 0;

	/* Disable interrupts and save flags. */
	spin_lock_irqsave(&lp->spinlock, flags);
	
	/* Read the NWID. */
	psa_read(ioaddr, lp->hacr,
		 (char *) psa.psa_nwid - (char *) &psa,
		 (unsigned char *) psa.psa_nwid, 3);
	wrqu->nwid.value = (psa.psa_nwid[0] << 8) + psa.psa_nwid[1];
	wrqu->nwid.disabled = !(psa.psa_nwid_select);
	wrqu->nwid.fixed = 1;	/* Superfluous */

	/* Enable interrupts and restore flags. */
	spin_unlock_irqrestore(&lp->spinlock, flags);

	return ret;
}

/*------------------------------------------------------------------*/
/*
 * Wireless Handler : set frequency
 */
static int wavelan_set_freq(struct net_device *dev,
			    struct iw_request_info *info,
			    union iwreq_data *wrqu,
			    char *extra)
{
	unsigned long ioaddr = dev->base_addr;
	net_local *lp = (net_local *) dev->priv;	/* lp is not unused */
	unsigned long flags;
	int ret;

	/* Disable interrupts and save flags. */
	spin_lock_irqsave(&lp->spinlock, flags);
	
	/* Attempt to recognise 2.00 cards (2.4 GHz frequency selectable). */
	if (!(mmc_in(ioaddr, mmroff(0, mmr_fee_status)) &
	      (MMR_FEE_STATUS_DWLD | MMR_FEE_STATUS_BUSY)))
		ret = wv_set_frequency(ioaddr, &(wrqu->freq));
	else
		ret = -EOPNOTSUPP;

	/* Enable interrupts and restore flags. */
	spin_unlock_irqrestore(&lp->spinlock, flags);

	return ret;
}

/*------------------------------------------------------------------*/
/*
 * Wireless Handler : get frequency
 */
static int wavelan_get_freq(struct net_device *dev,
			    struct iw_request_info *info,
			    union iwreq_data *wrqu,
			    char *extra)
{
	unsigned long ioaddr = dev->base_addr;
	net_local *lp = (net_local *) dev->priv;	/* lp is not unused */
	psa_t psa;
	unsigned long flags;
	int ret = 0;

	/* Disable interrupts and save flags. */
	spin_lock_irqsave(&lp->spinlock, flags);
	
	/* Attempt to recognise 2.00 cards (2.4 GHz frequency selectable).
	 * Does it work for everybody, especially old cards? */
	if (!(mmc_in(ioaddr, mmroff(0, mmr_fee_status)) &
	      (MMR_FEE_STATUS_DWLD | MMR_FEE_STATUS_BUSY))) {
		unsigned short freq;

		/* Ask the EEPROM to read the frequency from the first area. */
		fee_read(ioaddr, 0x00, &freq, 1);
		wrqu->freq.m = ((freq >> 5) * 5 + 24000L) * 10000;
		wrqu->freq.e = 1;
	} else {
		psa_read(ioaddr, lp->hacr,
			 (char *) &psa.psa_subband - (char *) &psa,
			 (unsigned char *) &psa.psa_subband, 1);

		if (psa.psa_subband <= 4) {
			wrqu->freq.m = fixed_bands[psa.psa_subband];
			wrqu->freq.e = (psa.psa_subband != 0);
		} else
			ret = -EOPNOTSUPP;
	}

	/* Enable interrupts and restore flags. */
	spin_unlock_irqrestore(&lp->spinlock, flags);

	return ret;
}

/*------------------------------------------------------------------*/
/*
 * Wireless Handler : set level threshold
 */
static int wavelan_set_sens(struct net_device *dev,
			    struct iw_request_info *info,
			    union iwreq_data *wrqu,
			    char *extra)
{
	unsigned long ioaddr = dev->base_addr;
	net_local *lp = (net_local *) dev->priv;	/* lp is not unused */
	psa_t psa;
	unsigned long flags;
	int ret = 0;

	/* Disable interrupts and save flags. */
	spin_lock_irqsave(&lp->spinlock, flags);
	
	/* Set the level threshold. */
	/* We should complain loudly if wrqu->sens.fixed = 0, because we
	 * can't set auto mode... */
	psa.psa_thr_pre_set = wrqu->sens.value & 0x3F;
	psa_write(ioaddr, lp->hacr,
		  (char *) &psa.psa_thr_pre_set - (char *) &psa,
		  (unsigned char *) &psa.psa_thr_pre_set, 1);
	/* update the Wavelan checksum */
	update_psa_checksum(dev, ioaddr, lp->hacr);
	mmc_out(ioaddr, mmwoff(0, mmw_thr_pre_set),
		psa.psa_thr_pre_set);

	/* Enable interrupts and restore flags. */
	spin_unlock_irqrestore(&lp->spinlock, flags);

	return ret;
}

/*------------------------------------------------------------------*/
/*
 * Wireless Handler : get level threshold
 */
static int wavelan_get_sens(struct net_device *dev,
			    struct iw_request_info *info,
			    union iwreq_data *wrqu,
			    char *extra)
{
	unsigned long ioaddr = dev->base_addr;
	net_local *lp = (net_local *) dev->priv;	/* lp is not unused */
	psa_t psa;
	unsigned long flags;
	int ret = 0;

	/* Disable interrupts and save flags. */
	spin_lock_irqsave(&lp->spinlock, flags);
	
	/* Read the level threshold. */
	psa_read(ioaddr, lp->hacr,
		 (char *) &psa.psa_thr_pre_set - (char *) &psa,
		 (unsigned char *) &psa.psa_thr_pre_set, 1);
	wrqu->sens.value = psa.psa_thr_pre_set & 0x3F;
	wrqu->sens.fixed = 1;

	/* Enable interrupts and restore flags. */
	spin_unlock_irqrestore(&lp->spinlock, flags);

	return ret;
}

/*------------------------------------------------------------------*/
/*
 * Wireless Handler : set encryption key
 */
static int wavelan_set_encode(struct net_device *dev,
			      struct iw_request_info *info,
			      union iwreq_data *wrqu,
			      char *extra)
{
	unsigned long ioaddr = dev->base_addr;
	net_local *lp = (net_local *) dev->priv;	/* lp is not unused */
	unsigned long flags;
	psa_t psa;
	int ret = 0;

	/* Disable interrupts and save flags. */
	spin_lock_irqsave(&lp->spinlock, flags);

	/* Check if capable of encryption */
	if (!mmc_encr(ioaddr)) {
		ret = -EOPNOTSUPP;
	}

	/* Check the size of the key */
	if((wrqu->encoding.length != 8) && (wrqu->encoding.length != 0)) {
		ret = -EINVAL;
	}

	if(!ret) {
		/* Basic checking... */
		if (wrqu->encoding.length == 8) {
			/* Copy the key in the driver */
			memcpy(psa.psa_encryption_key, extra,
			       wrqu->encoding.length);
			psa.psa_encryption_select = 1;

			psa_write(ioaddr, lp->hacr,
				  (char *) &psa.psa_encryption_select -
				  (char *) &psa,
				  (unsigned char *) &psa.
				  psa_encryption_select, 8 + 1);

			mmc_out(ioaddr, mmwoff(0, mmw_encr_enable),
				MMW_ENCR_ENABLE_EN | MMW_ENCR_ENABLE_MODE);
			mmc_write(ioaddr, mmwoff(0, mmw_encr_key),
				  (unsigned char *) &psa.
				  psa_encryption_key, 8);
		}

		/* disable encryption */
		if (wrqu->encoding.flags & IW_ENCODE_DISABLED) {
			psa.psa_encryption_select = 0;
			psa_write(ioaddr, lp->hacr,
				  (char *) &psa.psa_encryption_select -
				  (char *) &psa,
				  (unsigned char *) &psa.
				  psa_encryption_select, 1);

			mmc_out(ioaddr, mmwoff(0, mmw_encr_enable), 0);
		}
		/* update the Wavelan checksum */
		update_psa_checksum(dev, ioaddr, lp->hacr);
	}

	/* Enable interrupts and restore flags. */
	spin_unlock_irqrestore(&lp->spinlock, flags);

	return ret;
}

/*------------------------------------------------------------------*/
/*
 * Wireless Handler : get encryption key
 */
static int wavelan_get_encode(struct net_device *dev,
			      struct iw_request_info *info,
			      union iwreq_data *wrqu,
			      char *extra)
{
	unsigned long ioaddr = dev->base_addr;
	net_local *lp = (net_local *) dev->priv;	/* lp is not unused */
	psa_t psa;
	unsigned long flags;
	int ret = 0;

	/* Disable interrupts and save flags. */
	spin_lock_irqsave(&lp->spinlock, flags);
	
	/* Check if encryption is available */
	if (!mmc_encr(ioaddr)) {
		ret = -EOPNOTSUPP;
	} else {
		/* Read the encryption key */
		psa_read(ioaddr, lp->hacr,
			 (char *) &psa.psa_encryption_select -
			 (char *) &psa,
			 (unsigned char *) &psa.
			 psa_encryption_select, 1 + 8);

		/* encryption is enabled ? */
		if (psa.psa_encryption_select)
			wrqu->encoding.flags = IW_ENCODE_ENABLED;
		else
			wrqu->encoding.flags = IW_ENCODE_DISABLED;
		wrqu->encoding.flags |= mmc_encr(ioaddr);

		/* Copy the key to the user buffer */
		wrqu->encoding.length = 8;
		memcpy(extra, psa.psa_encryption_key, wrqu->encoding.length);
	}

	/* Enable interrupts and restore flags. */
	spin_unlock_irqrestore(&lp->spinlock, flags);

	return ret;
}

/*------------------------------------------------------------------*/
/*
 * Wireless Handler : get range info
 */
static int wavelan_get_range(struct net_device *dev,
			     struct iw_request_info *info,
			     union iwreq_data *wrqu,
			     char *extra)
{
	unsigned long ioaddr = dev->base_addr;
	net_local *lp = (net_local *) dev->priv;	/* lp is not unused */
	struct iw_range *range = (struct iw_range *) extra;
	unsigned long flags;
	int ret = 0;

	/* Set the length (very important for backward compatibility) */
	wrqu->data.length = sizeof(struct iw_range);

	/* Set all the info we don't care or don't know about to zero */
	memset(range, 0, sizeof(struct iw_range));

	/* Set the Wireless Extension versions */
	range->we_version_compiled = WIRELESS_EXT;
	range->we_version_source = 9;

	/* Set information in the range struct.  */
	range->throughput = 1.6 * 1000 * 1000;	/* don't argue on this ! */
	range->min_nwid = 0x0000;
	range->max_nwid = 0xFFFF;

	range->sensitivity = 0x3F;
	range->max_qual.qual = MMR_SGNL_QUAL;
	range->max_qual.level = MMR_SIGNAL_LVL;
	range->max_qual.noise = MMR_SILENCE_LVL;
	range->avg_qual.qual = MMR_SGNL_QUAL; /* Always max */
	/* Need to get better values for those two */
	range->avg_qual.level = 30;
	range->avg_qual.noise = 8;

	range->num_bitrates = 1;
	range->bitrate[0] = 2000000;	/* 2 Mb/s */

	/* Event capability (kernel + driver) */
	range->event_capa[0] = (IW_EVENT_CAPA_MASK(0x8B02) |
				IW_EVENT_CAPA_MASK(0x8B04));
	range->event_capa[1] = IW_EVENT_CAPA_K_1;

	/* Disable interrupts and save flags. */
	spin_lock_irqsave(&lp->spinlock, flags);
	
	/* Attempt to recognise 2.00 cards (2.4 GHz frequency selectable). */
	if (!(mmc_in(ioaddr, mmroff(0, mmr_fee_status)) &
	      (MMR_FEE_STATUS_DWLD | MMR_FEE_STATUS_BUSY))) {
		range->num_channels = 10;
		range->num_frequency = wv_frequency_list(ioaddr, range->freq,
							IW_MAX_FREQUENCIES);
	} else
		range->num_channels = range->num_frequency = 0;

	/* Encryption supported ? */
	if (mmc_encr(ioaddr)) {
		range->encoding_size[0] = 8;	/* DES = 64 bits key */
		range->num_encoding_sizes = 1;
		range->max_encoding_tokens = 1;	/* Only one key possible */
	} else {
		range->num_encoding_sizes = 0;
		range->max_encoding_tokens = 0;
	}

	/* Enable interrupts and restore flags. */
	spin_unlock_irqrestore(&lp->spinlock, flags);

	return ret;
}

/*------------------------------------------------------------------*/
/*
 * Wireless Private Handler : set quality threshold
 */
static int wavelan_set_qthr(struct net_device *dev,
			    struct iw_request_info *info,
			    union iwreq_data *wrqu,
			    char *extra)
{
	unsigned long ioaddr = dev->base_addr;
	net_local *lp = (net_local *) dev->priv;	/* lp is not unused */
	psa_t psa;
	unsigned long flags;

	/* Disable interrupts and save flags. */
	spin_lock_irqsave(&lp->spinlock, flags);
	
	psa.psa_quality_thr = *(extra) & 0x0F;
	psa_write(ioaddr, lp->hacr,
		  (char *) &psa.psa_quality_thr - (char *) &psa,
		  (unsigned char *) &psa.psa_quality_thr, 1);
	/* update the Wavelan checksum */
	update_psa_checksum(dev, ioaddr, lp->hacr);
	mmc_out(ioaddr, mmwoff(0, mmw_quality_thr),
		psa.psa_quality_thr);

	/* Enable interrupts and restore flags. */
	spin_unlock_irqrestore(&lp->spinlock, flags);

	return 0;
}

/*------------------------------------------------------------------*/
/*
 * Wireless Private Handler : get quality threshold
 */
static int wavelan_get_qthr(struct net_device *dev,
			    struct iw_request_info *info,
			    union iwreq_data *wrqu,
			    char *extra)
{
	unsigned long ioaddr = dev->base_addr;
	net_local *lp = (net_local *) dev->priv;	/* lp is not unused */
	psa_t psa;
	unsigned long flags;

	/* Disable interrupts and save flags. */
	spin_lock_irqsave(&lp->spinlock, flags);
	
	psa_read(ioaddr, lp->hacr,
		 (char *) &psa.psa_quality_thr - (char *) &psa,
		 (unsigned char *) &psa.psa_quality_thr, 1);
	*(extra) = psa.psa_quality_thr & 0x0F;

	/* Enable interrupts and restore flags. */
	spin_unlock_irqrestore(&lp->spinlock, flags);

	return 0;
}

#ifdef HISTOGRAM
/*------------------------------------------------------------------*/
/*
 * Wireless Private Handler : set histogram
 */
static int wavelan_set_histo(struct net_device *dev,
			     struct iw_request_info *info,
			     union iwreq_data *wrqu,
			     char *extra)
{
	net_local *lp = (net_local *) dev->priv;	/* lp is not unused */

	/* Check the number of intervals. */
	if (wrqu->data.length > 16) {
		return(-E2BIG);
	}

	/* Disable histo while we copy the addresses.
	 * As we don't disable interrupts, we need to do this */
	lp->his_number = 0;

	/* Are there ranges to copy? */
	if (wrqu->data.length > 0) {
		/* Copy interval ranges to the driver */
		memcpy(lp->his_range, extra, wrqu->data.length);

		{
		  int i;
		  printk(KERN_DEBUG "Histo :");
		  for(i = 0; i < wrqu->data.length; i++)
		    printk(" %d", lp->his_range[i]);
		  printk("\n");
		}

		/* Reset result structure. */
		memset(lp->his_sum, 0x00, sizeof(long) * 16);
	}

	/* Now we can set the number of ranges */
	lp->his_number = wrqu->data.length;

	return(0);
}

/*------------------------------------------------------------------*/
/*
 * Wireless Private Handler : get histogram
 */
static int wavelan_get_histo(struct net_device *dev,
			     struct iw_request_info *info,
			     union iwreq_data *wrqu,
			     char *extra)
{
	net_local *lp = (net_local *) dev->priv;	/* lp is not unused */

	/* Set the number of intervals. */
	wrqu->data.length = lp->his_number;

	/* Give back the distribution statistics */
	if(lp->his_number > 0)
		memcpy(extra, lp->his_sum, sizeof(long) * lp->his_number);

	return(0);
}
#endif			/* HISTOGRAM */

/*------------------------------------------------------------------*/
/*
 * Structures to export the Wireless Handlers
 */

static const iw_handler		wavelan_handler[] =
{
	NULL,				/* SIOCSIWNAME */
	wavelan_get_name,		/* SIOCGIWNAME */
	wavelan_set_nwid,		/* SIOCSIWNWID */
	wavelan_get_nwid,		/* SIOCGIWNWID */
	wavelan_set_freq,		/* SIOCSIWFREQ */
	wavelan_get_freq,		/* SIOCGIWFREQ */
	NULL,				/* SIOCSIWMODE */
	NULL,				/* SIOCGIWMODE */
	wavelan_set_sens,		/* SIOCSIWSENS */
	wavelan_get_sens,		/* SIOCGIWSENS */
	NULL,				/* SIOCSIWRANGE */
	wavelan_get_range,		/* SIOCGIWRANGE */
	NULL,				/* SIOCSIWPRIV */
	NULL,				/* SIOCGIWPRIV */
	NULL,				/* SIOCSIWSTATS */
	NULL,				/* SIOCGIWSTATS */
	iw_handler_set_spy,		/* SIOCSIWSPY */
	iw_handler_get_spy,		/* SIOCGIWSPY */
	iw_handler_set_thrspy,		/* SIOCSIWTHRSPY */
	iw_handler_get_thrspy,		/* SIOCGIWTHRSPY */
	NULL,				/* SIOCSIWAP */
	NULL,				/* SIOCGIWAP */
	NULL,				/* -- hole -- */
	NULL,				/* SIOCGIWAPLIST */
	NULL,				/* -- hole -- */
	NULL,				/* -- hole -- */
	NULL,				/* SIOCSIWESSID */
	NULL,				/* SIOCGIWESSID */
	NULL,				/* SIOCSIWNICKN */
	NULL,				/* SIOCGIWNICKN */
	NULL,				/* -- hole -- */
	NULL,				/* -- hole -- */
	NULL,				/* SIOCSIWRATE */
	NULL,				/* SIOCGIWRATE */
	NULL,				/* SIOCSIWRTS */
	NULL,				/* SIOCGIWRTS */
	NULL,				/* SIOCSIWFRAG */
	NULL,				/* SIOCGIWFRAG */
	NULL,				/* SIOCSIWTXPOW */
	NULL,				/* SIOCGIWTXPOW */
	NULL,				/* SIOCSIWRETRY */
	NULL,				/* SIOCGIWRETRY */
	/* Bummer ! Why those are only at the end ??? */
	wavelan_set_encode,		/* SIOCSIWENCODE */
	wavelan_get_encode,		/* SIOCGIWENCODE */
};

static const iw_handler		wavelan_private_handler[] =
{
	wavelan_set_qthr,		/* SIOCIWFIRSTPRIV */
	wavelan_get_qthr,		/* SIOCIWFIRSTPRIV + 1 */
#ifdef HISTOGRAM
	wavelan_set_histo,		/* SIOCIWFIRSTPRIV + 2 */
	wavelan_get_histo,		/* SIOCIWFIRSTPRIV + 3 */
#endif	/* HISTOGRAM */
};

static const struct iw_priv_args wavelan_private_args[] = {
/*{ cmd,         set_args,                            get_args, name } */
  { SIOCSIPQTHR, IW_PRIV_TYPE_BYTE | IW_PRIV_SIZE_FIXED | 1, 0, "setqualthr" },
  { SIOCGIPQTHR, 0, IW_PRIV_TYPE_BYTE | IW_PRIV_SIZE_FIXED | 1, "getqualthr" },
  { SIOCSIPHISTO, IW_PRIV_TYPE_BYTE | 16,                    0, "sethisto" },
  { SIOCGIPHISTO, 0,                     IW_PRIV_TYPE_INT | 16, "gethisto" },
};

static const struct iw_handler_def	wavelan_handler_def =
{
	.num_standard	= ARRAY_SIZE(wavelan_handler),
	.num_private	= ARRAY_SIZE(wavelan_private_handler),
	.num_private_args = ARRAY_SIZE(wavelan_private_args),
	.standard	= wavelan_handler,
	.private	= wavelan_private_handler,
	.private_args	= wavelan_private_args,
	.get_wireless_stats = wavelan_get_wireless_stats,
};

/*------------------------------------------------------------------*/
/*
 * Get wireless statistics.
 * Called by /proc/net/wireless
 */
static iw_stats *wavelan_get_wireless_stats(struct net_device * dev)
{
	unsigned long ioaddr = dev->base_addr;
	net_local *lp = (net_local *) dev->priv;
	mmr_t m;
	iw_stats *wstats;
	unsigned long flags;

#ifdef DEBUG_IOCTL_TRACE
	printk(KERN_DEBUG "%s: ->wavelan_get_wireless_stats()\n",
	       dev->name);
#endif

	/* Check */
	if (lp == (net_local *) NULL)
		return (iw_stats *) NULL;
	
	/* Disable interrupts and save flags. */
	spin_lock_irqsave(&lp->spinlock, flags);
	
	wstats = &lp->wstats;

	/* Get data from the mmc. */
	mmc_out(ioaddr, mmwoff(0, mmw_freeze), 1);

	mmc_read(ioaddr, mmroff(0, mmr_dce_status), &m.mmr_dce_status, 1);
	mmc_read(ioaddr, mmroff(0, mmr_wrong_nwid_l), &m.mmr_wrong_nwid_l,
		 2);
	mmc_read(ioaddr, mmroff(0, mmr_thr_pre_set), &m.mmr_thr_pre_set,
		 4);

	mmc_out(ioaddr, mmwoff(0, mmw_freeze), 0);

	/* Copy data to wireless stuff. */
	wstats->status = m.mmr_dce_status & MMR_DCE_STATUS;
	wstats->qual.qual = m.mmr_sgnl_qual & MMR_SGNL_QUAL;
	wstats->qual.level = m.mmr_signal_lvl & MMR_SIGNAL_LVL;
	wstats->qual.noise = m.mmr_silence_lvl & MMR_SILENCE_LVL;
	wstats->qual.updated = (((m. mmr_signal_lvl & MMR_SIGNAL_LVL_VALID) >> 7) 
			| ((m.mmr_signal_lvl & MMR_SIGNAL_LVL_VALID) >> 6) 
			| ((m.mmr_silence_lvl & MMR_SILENCE_LVL_VALID) >> 5));
	wstats->discard.nwid += (m.mmr_wrong_nwid_h << 8) | m.mmr_wrong_nwid_l;
	wstats->discard.code = 0L;
	wstats->discard.misc = 0L;

	/* Enable interrupts and restore flags. */
	spin_unlock_irqrestore(&lp->spinlock, flags);

#ifdef DEBUG_IOCTL_TRACE
	printk(KERN_DEBUG "%s: <-wavelan_get_wireless_stats()\n",
	       dev->name);
#endif
	return &lp->wstats;
}

/************************* PACKET RECEPTION *************************/
/*
 * This part deals with receiving the packets.
 * The interrupt handler gets an interrupt when a packet has been
 * successfully received and calls this part.
 */

/*------------------------------------------------------------------*/
/*
 * This routine does the actual copying of data (including the Ethernet
 * header structure) from the WaveLAN card to an sk_buff chain that
 * will be passed up to the network interface layer. NOTE: we
 * currently don't handle trailer protocols (neither does the rest of
 * the network interface), so if that is needed, it will (at least in
 * part) be added here.  The contents of the receive ring buffer are
 * copied to a message chain that is then passed to the kernel.
 *
 * Note: if any errors occur, the packet is "dropped on the floor".
 * (called by wv_packet_rcv())
 */
static void
wv_packet_read(struct net_device * dev, u16 buf_off, int sksize)
{
	net_local *lp = (net_local *) dev->priv;
	unsigned long ioaddr = dev->base_addr;
	struct sk_buff *skb;

#ifdef DEBUG_RX_TRACE
	printk(KERN_DEBUG "%s: ->wv_packet_read(0x%X, %d)\n",
	       dev->name, buf_off, sksize);
#endif

	/* Allocate buffer for the data */
	if ((skb = dev_alloc_skb(sksize)) == (struct sk_buff *) NULL) {
#ifdef DEBUG_RX_ERROR
		printk(KERN_INFO
		       "%s: wv_packet_read(): could not alloc_skb(%d, GFP_ATOMIC).\n",
		       dev->name, sksize);
#endif
		lp->stats.rx_dropped++;
		return;
	}

	/* Copy the packet to the buffer. */
	obram_read(ioaddr, buf_off, skb_put(skb, sksize), sksize);
	skb->protocol = eth_type_trans(skb, dev);

#ifdef DEBUG_RX_INFO
	wv_packet_info(skb_mac_header(skb), sksize, dev->name,
		       "wv_packet_read");
#endif				/* DEBUG_RX_INFO */

	/* Statistics-gathering and associated stuff.
	 * It seem a bit messy with all the define, but it's really
	 * simple... */
	if (
#ifdef IW_WIRELESS_SPY		/* defined in iw_handler.h */
		   (lp->spy_data.spy_number > 0) ||
#endif /* IW_WIRELESS_SPY */
#ifdef HISTOGRAM
		   (lp->his_number > 0) ||
#endif /* HISTOGRAM */
		   0) {
		u8 stats[3];	/* signal level, noise level, signal quality */

		/* Read signal level, silence level and signal quality bytes */
		/* Note: in the PCMCIA hardware, these are part of the frame.
		 * It seems that for the ISA hardware, it's nowhere to be
		 * found in the frame, so I'm obliged to do this (it has a
		 * side effect on /proc/net/wireless).
		 * Any ideas?
		 */
		mmc_out(ioaddr, mmwoff(0, mmw_freeze), 1);
		mmc_read(ioaddr, mmroff(0, mmr_signal_lvl), stats, 3);
		mmc_out(ioaddr, mmwoff(0, mmw_freeze), 0);

#ifdef DEBUG_RX_INFO
		printk(KERN_DEBUG
		       "%s: wv_packet_read(): Signal level %d/63, Silence level %d/63, signal quality %d/16\n",
		       dev->name, stats[0] & 0x3F, stats[1] & 0x3F,
		       stats[2] & 0x0F);
#endif

		/* Spying stuff */
#ifdef IW_WIRELESS_SPY
		wl_spy_gather(dev, skb_mac_header(skb) + WAVELAN_ADDR_SIZE,
			      stats);
#endif /* IW_WIRELESS_SPY */
#ifdef HISTOGRAM
		wl_his_gather(dev, stats);
#endif /* HISTOGRAM */
	}

	/*
	 * Hand the packet to the network module.
	 */
	netif_rx(skb);

	/* Keep statistics up to date */
	dev->last_rx = jiffies;
	lp->stats.rx_packets++;
	lp->stats.rx_bytes += sksize;

#ifdef DEBUG_RX_TRACE
	printk(KERN_DEBUG "%s: <-wv_packet_read()\n", dev->name);
#endif
}

/*------------------------------------------------------------------*/
/*
 * Transfer as many packets as we can
 * from the device RAM.
 * (called in wavelan_interrupt()).
 * Note : the spinlock is already grabbed for us.
 */
static void wv_receive(struct net_device * dev)
{
	unsigned long ioaddr = dev->base_addr;
	net_local *lp = (net_local *) dev->priv;
	fd_t fd;
	rbd_t rbd;
	int nreaped = 0;

#ifdef DEBUG_RX_TRACE
	printk(KERN_DEBUG "%s: ->wv_receive()\n", dev->name);
#endif

	/* Loop on each received packet. */
	for (;;) {
		obram_read(ioaddr, lp->rx_head, (unsigned char *) &fd,
			   sizeof(fd));

		/* Note about the status :
		 * It start up to be 0 (the value we set). Then, when the RU
		 * grab the buffer to prepare for reception, it sets the
		 * FD_STATUS_B flag. When the RU has finished receiving the
		 * frame, it clears FD_STATUS_B, set FD_STATUS_C to indicate
		 * completion and set the other flags to indicate the eventual
		 * errors. FD_STATUS_OK indicates that the reception was OK.
		 */

		/* If the current frame is not complete, we have reached the end. */
		if ((fd.fd_status & FD_STATUS_C) != FD_STATUS_C)
			break;	/* This is how we exit the loop. */

		nreaped++;

		/* Check whether frame was correctly received. */
		if ((fd.fd_status & FD_STATUS_OK) == FD_STATUS_OK) {
			/* Does the frame contain a pointer to the data?  Let's check. */
			if (fd.fd_rbd_offset != I82586NULL) {
				/* Read the receive buffer descriptor */
				obram_read(ioaddr, fd.fd_rbd_offset,
					   (unsigned char *) &rbd,
					   sizeof(rbd));

#ifdef DEBUG_RX_ERROR
				if ((rbd.rbd_status & RBD_STATUS_EOF) !=
				    RBD_STATUS_EOF) printk(KERN_INFO
							   "%s: wv_receive(): missing EOF flag.\n",
							   dev->name);

				if ((rbd.rbd_status & RBD_STATUS_F) !=
				    RBD_STATUS_F) printk(KERN_INFO
							 "%s: wv_receive(): missing F flag.\n",
							 dev->name);
#endif				/* DEBUG_RX_ERROR */

				/* Read the packet and transmit to Linux */
				wv_packet_read(dev, rbd.rbd_bufl,
					       rbd.
					       rbd_status &
					       RBD_STATUS_ACNT);
			}
#ifdef DEBUG_RX_ERROR
			else	/* if frame has no data */
				printk(KERN_INFO
				       "%s: wv_receive(): frame has no data.\n",
				       dev->name);
#endif
		} else {	/* If reception was no successful */

			lp->stats.rx_errors++;

#ifdef DEBUG_RX_INFO
			printk(KERN_DEBUG
			       "%s: wv_receive(): frame not received successfully (%X).\n",
			       dev->name, fd.fd_status);
#endif

#ifdef DEBUG_RX_ERROR
			if ((fd.fd_status & FD_STATUS_S6) != 0)
				printk(KERN_INFO
				       "%s: wv_receive(): no EOF flag.\n",
				       dev->name);
#endif

			if ((fd.fd_status & FD_STATUS_S7) != 0) {
				lp->stats.rx_length_errors++;
#ifdef DEBUG_RX_FAIL
				printk(KERN_DEBUG
				       "%s: wv_receive(): frame too short.\n",
				       dev->name);
#endif
			}

			if ((fd.fd_status & FD_STATUS_S8) != 0) {
				lp->stats.rx_over_errors++;
#ifdef DEBUG_RX_FAIL
				printk(KERN_DEBUG
				       "%s: wv_receive(): rx DMA overrun.\n",
				       dev->name);
#endif
			}

			if ((fd.fd_status & FD_STATUS_S9) != 0) {
				lp->stats.rx_fifo_errors++;
#ifdef DEBUG_RX_FAIL
				printk(KERN_DEBUG
				       "%s: wv_receive(): ran out of resources.\n",
				       dev->name);
#endif
			}

			if ((fd.fd_status & FD_STATUS_S10) != 0) {
				lp->stats.rx_frame_errors++;
#ifdef DEBUG_RX_FAIL
				printk(KERN_DEBUG
				       "%s: wv_receive(): alignment error.\n",
				       dev->name);
#endif
			}

			if ((fd.fd_status & FD_STATUS_S11) != 0) {
				lp->stats.rx_crc_errors++;
#ifdef DEBUG_RX_FAIL
				printk(KERN_DEBUG
				       "%s: wv_receive(): CRC error.\n",
				       dev->name);
#endif
			}
		}

		fd.fd_status = 0;
		obram_write(ioaddr, fdoff(lp->rx_head, fd_status),
			    (unsigned char *) &fd.fd_status,
			    sizeof(fd.fd_status));

		fd.fd_command = FD_COMMAND_EL;
		obram_write(ioaddr, fdoff(lp->rx_head, fd_command),
			    (unsigned char *) &fd.fd_command,
			    sizeof(fd.fd_command));

		fd.fd_command = 0;
		obram_write(ioaddr, fdoff(lp->rx_last, fd_command),
			    (unsigned char *) &fd.fd_command,
			    sizeof(fd.fd_command));

		lp->rx_last = lp->rx_head;
		lp->rx_head = fd.fd_link_offset;
	}			/* for(;;) -> loop on all frames */

#ifdef DEBUG_RX_INFO
	if (nreaped > 1)
		printk(KERN_DEBUG "%s: wv_receive(): reaped %d\n",
		       dev->name, nreaped);
#endif
#ifdef DEBUG_RX_TRACE
	printk(KERN_DEBUG "%s: <-wv_receive()\n", dev->name);
#endif
}

/*********************** PACKET TRANSMISSION ***********************/
/*
 * This part deals with sending packets through the WaveLAN.
 *
 */

/*------------------------------------------------------------------*/
/*
 * This routine fills in the appropriate registers and memory
 * locations on the WaveLAN card and starts the card off on
 * the transmit.
 *
 * The principle:
 * Each block contains a transmit command, a NOP command,
 * a transmit block descriptor and a buffer.
 * The CU read the transmit block which point to the tbd,
 * read the tbd and the content of the buffer.
 * When it has finish with it, it goes to the next command
 * which in our case is the NOP. The NOP points on itself,
 * so the CU stop here.
 * When we add the next block, we modify the previous nop
 * to make it point on the new tx command.
 * Simple, isn't it ?
 *
 * (called in wavelan_packet_xmit())
 */
static int wv_packet_write(struct net_device * dev, void *buf, short length)
{
	net_local *lp = (net_local *) dev->priv;
	unsigned long ioaddr = dev->base_addr;
	unsigned short txblock;
	unsigned short txpred;
	unsigned short tx_addr;
	unsigned short nop_addr;
	unsigned short tbd_addr;
	unsigned short buf_addr;
	ac_tx_t tx;
	ac_nop_t nop;
	tbd_t tbd;
	int clen = length;
	unsigned long flags;

#ifdef DEBUG_TX_TRACE
	printk(KERN_DEBUG "%s: ->wv_packet_write(%d)\n", dev->name,
	       length);
#endif

	spin_lock_irqsave(&lp->spinlock, flags);

	/* Check nothing bad has happened */
	if (lp->tx_n_in_use == (NTXBLOCKS - 1)) {
#ifdef DEBUG_TX_ERROR
		printk(KERN_INFO "%s: wv_packet_write(): Tx queue full.\n",
		       dev->name);
#endif
		spin_unlock_irqrestore(&lp->spinlock, flags);
		return 1;
	}

	/* Calculate addresses of next block and previous block. */
	txblock = lp->tx_first_free;
	txpred = txblock - TXBLOCKZ;
	if (txpred < OFFSET_CU)
		txpred += NTXBLOCKS * TXBLOCKZ;
	lp->tx_first_free += TXBLOCKZ;
	if (lp->tx_first_free >= OFFSET_CU + NTXBLOCKS * TXBLOCKZ)
		lp->tx_first_free -= NTXBLOCKS * TXBLOCKZ;

	lp->tx_n_in_use++;

	/* Calculate addresses of the different parts of the block. */
	tx_addr = txblock;
	nop_addr = tx_addr + sizeof(tx);
	tbd_addr = nop_addr + sizeof(nop);
	buf_addr = tbd_addr + sizeof(tbd);

	/*
	 * Transmit command
	 */
	tx.tx_h.ac_status = 0;
	obram_write(ioaddr, toff(ac_tx_t, tx_addr, tx_h.ac_status),
		    (unsigned char *) &tx.tx_h.ac_status,
		    sizeof(tx.tx_h.ac_status));

	/*
	 * NOP command
	 */
	nop.nop_h.ac_status = 0;
	obram_write(ioaddr, toff(ac_nop_t, nop_addr, nop_h.ac_status),
		    (unsigned char *) &nop.nop_h.ac_status,
		    sizeof(nop.nop_h.ac_status));
	nop.nop_h.ac_link = nop_addr;
	obram_write(ioaddr, toff(ac_nop_t, nop_addr, nop_h.ac_link),
		    (unsigned char *) &nop.nop_h.ac_link,
		    sizeof(nop.nop_h.ac_link));

	/*
	 * Transmit buffer descriptor
	 */
	tbd.tbd_status = TBD_STATUS_EOF | (TBD_STATUS_ACNT & clen);
	tbd.tbd_next_bd_offset = I82586NULL;
	tbd.tbd_bufl = buf_addr;
	tbd.tbd_bufh = 0;
	obram_write(ioaddr, tbd_addr, (unsigned char *) &tbd, sizeof(tbd));

	/*
	 * Data
	 */
	obram_write(ioaddr, buf_addr, buf, length);

	/*
	 * Overwrite the predecessor NOP link
	 * so that it points to this txblock.
	 */
	nop_addr = txpred + sizeof(tx);
	nop.nop_h.ac_status = 0;
	obram_write(ioaddr, toff(ac_nop_t, nop_addr, nop_h.ac_status),
		    (unsigned char *) &nop.nop_h.ac_status,
		    sizeof(nop.nop_h.ac_status));
	nop.nop_h.ac_link = txblock;
	obram_write(ioaddr, toff(ac_nop_t, nop_addr, nop_h.ac_link),
		    (unsigned char *) &nop.nop_h.ac_link,
		    sizeof(nop.nop_h.ac_link));

	/* Make sure the watchdog will keep quiet for a while */
	dev->trans_start = jiffies;

	/* Keep stats up to date. */
	lp->stats.tx_bytes += length;

	if (lp->tx_first_in_use == I82586NULL)
		lp->tx_first_in_use = txblock;

	if (lp->tx_n_in_use < NTXBLOCKS - 1)
		netif_wake_queue(dev);

	spin_unlock_irqrestore(&lp->spinlock, flags);
	
#ifdef DEBUG_TX_INFO
	wv_packet_info((u8 *) buf, length, dev->name,
		       "wv_packet_write");
#endif				/* DEBUG_TX_INFO */

#ifdef DEBUG_TX_TRACE
	printk(KERN_DEBUG "%s: <-wv_packet_write()\n", dev->name);
#endif

	return 0;
}

/*------------------------------------------------------------------*/
/*
 * This routine is called when we want to send a packet (NET3 callback)
 * In this routine, we check if the harware is ready to accept
 * the packet.  We also prevent reentrance.  Then we call the function
 * to send the packet.
 */
static int wavelan_packet_xmit(struct sk_buff *skb, struct net_device * dev)
{
	net_local *lp = (net_local *) dev->priv;
	unsigned long flags;
	char data[ETH_ZLEN];

#ifdef DEBUG_TX_TRACE
	printk(KERN_DEBUG "%s: ->wavelan_packet_xmit(0x%X)\n", dev->name,
	       (unsigned) skb);
#endif

	/*
	 * Block a timer-based transmit from overlapping.
	 * In other words, prevent reentering this routine.
	 */
	netif_stop_queue(dev);

	/* If somebody has asked to reconfigure the controller, 
	 * we can do it now.
	 */
	if (lp->reconfig_82586) {
		spin_lock_irqsave(&lp->spinlock, flags);
		wv_82586_config(dev);
		spin_unlock_irqrestore(&lp->spinlock, flags);
		/* Check that we can continue */
		if (lp->tx_n_in_use == (NTXBLOCKS - 1))
			return 1;
	}
#ifdef DEBUG_TX_ERROR
	if (skb->next)
		printk(KERN_INFO "skb has next\n");
#endif

	/* Do we need some padding? */
	/* Note : on wireless the propagation time is in the order of 1us,
	 * and we don't have the Ethernet specific requirement of beeing
	 * able to detect collisions, therefore in theory we don't really
	 * need to pad. Jean II */
	if (skb->len < ETH_ZLEN) {
		memset(data, 0, ETH_ZLEN);
		skb_copy_from_linear_data(skb, data, skb->len);
		/* Write packet on the card */
		if(wv_packet_write(dev, data, ETH_ZLEN))
			return 1;	/* We failed */
	}
	else if(wv_packet_write(dev, skb->data, skb->len))
		return 1;	/* We failed */


	dev_kfree_skb(skb);

#ifdef DEBUG_TX_TRACE
	printk(KERN_DEBUG "%s: <-wavelan_packet_xmit()\n", dev->name);
#endif
	return 0;
}

/*********************** HARDWARE CONFIGURATION ***********************/
/*
 * This part does the real job of starting and configuring the hardware.
 */

/*--------------------------------------------------------------------*/
/*
 * Routine to initialize the Modem Management Controller.
 * (called by wv_hw_reset())
 */
static int wv_mmc_init(struct net_device * dev)
{
	unsigned long ioaddr = dev->base_addr;
	net_local *lp = (net_local *) dev->priv;
	psa_t psa;
	mmw_t m;
	int configured;

#ifdef DEBUG_CONFIG_TRACE
	printk(KERN_DEBUG "%s: ->wv_mmc_init()\n", dev->name);
#endif

	/* Read the parameter storage area. */
	psa_read(ioaddr, lp->hacr, 0, (unsigned char *) &psa, sizeof(psa));

#ifdef USE_PSA_CONFIG
	configured = psa.psa_conf_status & 1;
#else
	configured = 0;
#endif

	/* Is the PSA is not configured */
	if (!configured) {
		/* User will be able to configure NWID later (with iwconfig). */
		psa.psa_nwid[0] = 0;
		psa.psa_nwid[1] = 0;

		/* no NWID checking since NWID is not set */
		psa.psa_nwid_select = 0;

		/* Disable encryption */
		psa.psa_encryption_select = 0;

		/* Set to standard values:
		 * 0x04 for AT,
		 * 0x01 for MCA,
		 * 0x04 for PCMCIA and 2.00 card (AT&T 407-024689/E document)
		 */
		if (psa.psa_comp_number & 1)
			psa.psa_thr_pre_set = 0x01;
		else
			psa.psa_thr_pre_set = 0x04;
		psa.psa_quality_thr = 0x03;

		/* It is configured */
		psa.psa_conf_status |= 1;

#ifdef USE_PSA_CONFIG
		/* Write the psa. */
		psa_write(ioaddr, lp->hacr,
			  (char *) psa.psa_nwid - (char *) &psa,
			  (unsigned char *) psa.psa_nwid, 4);
		psa_write(ioaddr, lp->hacr,
			  (char *) &psa.psa_thr_pre_set - (char *) &psa,
			  (unsigned char *) &psa.psa_thr_pre_set, 1);
		psa_write(ioaddr, lp->hacr,
			  (char *) &psa.psa_quality_thr - (char *) &psa,
			  (unsigned char *) &psa.psa_quality_thr, 1);
		psa_write(ioaddr, lp->hacr,
			  (char *) &psa.psa_conf_status - (char *) &psa,
			  (unsigned char *) &psa.psa_conf_status, 1);
		/* update the Wavelan checksum */
		update_psa_checksum(dev, ioaddr, lp->hacr);
#endif
	}

	/* Zero the mmc structure. */
	memset(&m, 0x00, sizeof(m));

	/* Copy PSA info to the mmc. */
	m.mmw_netw_id_l = psa.psa_nwid[1];
	m.mmw_netw_id_h = psa.psa_nwid[0];

	if (psa.psa_nwid_select & 1)
		m.mmw_loopt_sel = 0x00;
	else
		m.mmw_loopt_sel = MMW_LOOPT_SEL_DIS_NWID;

	memcpy(&m.mmw_encr_key, &psa.psa_encryption_key,
	       sizeof(m.mmw_encr_key));

	if (psa.psa_encryption_select)
		m.mmw_encr_enable =
		    MMW_ENCR_ENABLE_EN | MMW_ENCR_ENABLE_MODE;
	else
		m.mmw_encr_enable = 0;

	m.mmw_thr_pre_set = psa.psa_thr_pre_set & 0x3F;
	m.mmw_quality_thr = psa.psa_quality_thr & 0x0F;

	/*
	 * Set default modem control parameters.
	 * See NCR document 407-0024326 Rev. A.
	 */
	m.mmw_jabber_enable = 0x01;
	m.mmw_freeze = 0;
	m.mmw_anten_sel = MMW_ANTEN_SEL_ALG_EN;
	m.mmw_ifs = 0x20;
	m.mmw_mod_delay = 0x04;
	m.mmw_jam_time = 0x38;

	m.mmw_des_io_invert = 0;
	m.mmw_decay_prm = 0;
	m.mmw_decay_updat_prm = 0;

	/* Write all info to MMC. */
	mmc_write(ioaddr, 0, (u8 *) & m, sizeof(m));

	/* The following code starts the modem of the 2.00 frequency
	 * selectable cards at power on.  It's not strictly needed for the
	 * following boots.
	 * The original patch was by Joe Finney for the PCMCIA driver, but
	 * I've cleaned it up a bit and added documentation.
	 * Thanks to Loeke Brederveld from Lucent for the info.
	 */

	/* Attempt to recognise 2.00 cards (2.4 GHz frequency selectable)
	 * Does it work for everybody, especially old cards? */
	/* Note: WFREQSEL verifies that it is able to read a sensible
	 * frequency from EEPROM (address 0x00) and that MMR_FEE_STATUS_ID
	 * is 0xA (Xilinx version) or 0xB (Ariadne version).
	 * My test is more crude but does work. */
	if (!(mmc_in(ioaddr, mmroff(0, mmr_fee_status)) &
	      (MMR_FEE_STATUS_DWLD | MMR_FEE_STATUS_BUSY))) {
		/* We must download the frequency parameters to the
		 * synthesizers (from the EEPROM - area 1)
		 * Note: as the EEPROM is automatically decremented, we set the end
		 * if the area... */
		m.mmw_fee_addr = 0x0F;
		m.mmw_fee_ctrl = MMW_FEE_CTRL_READ | MMW_FEE_CTRL_DWLD;
		mmc_write(ioaddr, (char *) &m.mmw_fee_ctrl - (char *) &m,
			  (unsigned char *) &m.mmw_fee_ctrl, 2);

		/* Wait until the download is finished. */
		fee_wait(ioaddr, 100, 100);

#ifdef DEBUG_CONFIG_INFO
		/* The frequency was in the last word downloaded. */
		mmc_read(ioaddr, (char *) &m.mmw_fee_data_l - (char *) &m,
			 (unsigned char *) &m.mmw_fee_data_l, 2);

		/* Print some info for the user. */
		printk(KERN_DEBUG
		       "%s: WaveLAN 2.00 recognised (frequency select).  Current frequency = %ld\n",
		       dev->name,
		       ((m.
			 mmw_fee_data_h << 4) | (m.mmw_fee_data_l >> 4)) *
		       5 / 2 + 24000L);
#endif

		/* We must now download the power adjust value (gain) to
		 * the synthesizers (from the EEPROM - area 7 - DAC). */
		m.mmw_fee_addr = 0x61;
		m.mmw_fee_ctrl = MMW_FEE_CTRL_READ | MMW_FEE_CTRL_DWLD;
		mmc_write(ioaddr, (char *) &m.mmw_fee_ctrl - (char *) &m,
			  (unsigned char *) &m.mmw_fee_ctrl, 2);

		/* Wait until the download is finished. */
	}
	/* if 2.00 card */
#ifdef DEBUG_CONFIG_TRACE
	printk(KERN_DEBUG "%s: <-wv_mmc_init()\n", dev->name);
#endif
	return 0;
}

/*------------------------------------------------------------------*/
/*
 * Construct the fd and rbd structures.
 * Start the receive unit.
 * (called by wv_hw_reset())
 */
static int wv_ru_start(struct net_device * dev)
{
	net_local *lp = (net_local *) dev->priv;
	unsigned long ioaddr = dev->base_addr;
	u16 scb_cs;
	fd_t fd;
	rbd_t rbd;
	u16 rx;
	u16 rx_next;
	int i;

#ifdef DEBUG_CONFIG_TRACE
	printk(KERN_DEBUG "%s: ->wv_ru_start()\n", dev->name);
#endif

	obram_read(ioaddr, scboff(OFFSET_SCB, scb_status),
		   (unsigned char *) &scb_cs, sizeof(scb_cs));
	if ((scb_cs & SCB_ST_RUS) == SCB_ST_RUS_RDY)
		return 0;

	lp->rx_head = OFFSET_RU;

	for (i = 0, rx = lp->rx_head; i < NRXBLOCKS; i++, rx = rx_next) {
		rx_next =
		    (i == NRXBLOCKS - 1) ? lp->rx_head : rx + RXBLOCKZ;

		fd.fd_status = 0;
		fd.fd_command = (i == NRXBLOCKS - 1) ? FD_COMMAND_EL : 0;
		fd.fd_link_offset = rx_next;
		fd.fd_rbd_offset = rx + sizeof(fd);
		obram_write(ioaddr, rx, (unsigned char *) &fd, sizeof(fd));

		rbd.rbd_status = 0;
		rbd.rbd_next_rbd_offset = I82586NULL;
		rbd.rbd_bufl = rx + sizeof(fd) + sizeof(rbd);
		rbd.rbd_bufh = 0;
		rbd.rbd_el_size = RBD_EL | (RBD_SIZE & MAXDATAZ);
		obram_write(ioaddr, rx + sizeof(fd),
			    (unsigned char *) &rbd, sizeof(rbd));

		lp->rx_last = rx;
	}

	obram_write(ioaddr, scboff(OFFSET_SCB, scb_rfa_offset),
		    (unsigned char *) &lp->rx_head, sizeof(lp->rx_head));

	scb_cs = SCB_CMD_RUC_GO;
	obram_write(ioaddr, scboff(OFFSET_SCB, scb_command),
		    (unsigned char *) &scb_cs, sizeof(scb_cs));

	set_chan_attn(ioaddr, lp->hacr);

	for (i = 1000; i > 0; i--) {
		obram_read(ioaddr, scboff(OFFSET_SCB, scb_command),
			   (unsigned char *) &scb_cs, sizeof(scb_cs));
		if (scb_cs == 0)
			break;

		udelay(10);
	}

	if (i <= 0) {
#ifdef DEBUG_CONFIG_ERROR
		printk(KERN_INFO
		       "%s: wavelan_ru_start(): board not accepting command.\n",
		       dev->name);
#endif
		return -1;
	}
#ifdef DEBUG_CONFIG_TRACE
	printk(KERN_DEBUG "%s: <-wv_ru_start()\n", dev->name);
#endif
	return 0;
}

/*------------------------------------------------------------------*/
/*
 * Initialise the transmit blocks.
 * Start the command unit executing the NOP
 * self-loop of the first transmit block.
 *
 * Here we create the list of send buffers used to transmit packets
 * between the PC and the command unit. For each buffer, we create a
 * buffer descriptor (pointing on the buffer), a transmit command
 * (pointing to the buffer descriptor) and a NOP command.
 * The transmit command is linked to the NOP, and the NOP to itself.
 * When we will have finished executing the transmit command, we will
 * then loop on the NOP. By releasing the NOP link to a new command,
 * we may send another buffer.
 *
 * (called by wv_hw_reset())
 */
static int wv_cu_start(struct net_device * dev)
{
	net_local *lp = (net_local *) dev->priv;
	unsigned long ioaddr = dev->base_addr;
	int i;
	u16 txblock;
	u16 first_nop;
	u16 scb_cs;

#ifdef DEBUG_CONFIG_TRACE
	printk(KERN_DEBUG "%s: ->wv_cu_start()\n", dev->name);
#endif

	lp->tx_first_free = OFFSET_CU;
	lp->tx_first_in_use = I82586NULL;

	for (i = 0, txblock = OFFSET_CU;
	     i < NTXBLOCKS; i++, txblock += TXBLOCKZ) {
		ac_tx_t tx;
		ac_nop_t nop;
		tbd_t tbd;
		unsigned short tx_addr;
		unsigned short nop_addr;
		unsigned short tbd_addr;
		unsigned short buf_addr;

		tx_addr = txblock;
		nop_addr = tx_addr + sizeof(tx);
		tbd_addr = nop_addr + sizeof(nop);
		buf_addr = tbd_addr + sizeof(tbd);

		tx.tx_h.ac_status = 0;
		tx.tx_h.ac_command = acmd_transmit | AC_CFLD_I;
		tx.tx_h.ac_link = nop_addr;
		tx.tx_tbd_offset = tbd_addr;
		obram_write(ioaddr, tx_addr, (unsigned char *) &tx,
			    sizeof(tx));

		nop.nop_h.ac_status = 0;
		nop.nop_h.ac_command = acmd_nop;
		nop.nop_h.ac_link = nop_addr;
		obram_write(ioaddr, nop_addr, (unsigned char *) &nop,
			    sizeof(nop));

		tbd.tbd_status = TBD_STATUS_EOF;
		tbd.tbd_next_bd_offset = I82586NULL;
		tbd.tbd_bufl = buf_addr;
		tbd.tbd_bufh = 0;
		obram_write(ioaddr, tbd_addr, (unsigned char *) &tbd,
			    sizeof(tbd));
	}

	first_nop =
	    OFFSET_CU + (NTXBLOCKS - 1) * TXBLOCKZ + sizeof(ac_tx_t);
	obram_write(ioaddr, scboff(OFFSET_SCB, scb_cbl_offset),
		    (unsigned char *) &first_nop, sizeof(first_nop));

	scb_cs = SCB_CMD_CUC_GO;
	obram_write(ioaddr, scboff(OFFSET_SCB, scb_command),
		    (unsigned char *) &scb_cs, sizeof(scb_cs));

	set_chan_attn(ioaddr, lp->hacr);

	for (i = 1000; i > 0; i--) {
		obram_read(ioaddr, scboff(OFFSET_SCB, scb_command),
			   (unsigned char *) &scb_cs, sizeof(scb_cs));
		if (scb_cs == 0)
			break;

		udelay(10);
	}

	if (i <= 0) {
#ifdef DEBUG_CONFIG_ERROR
		printk(KERN_INFO
		       "%s: wavelan_cu_start(): board not accepting command.\n",
		       dev->name);
#endif
		return -1;
	}

	lp->tx_n_in_use = 0;
	netif_start_queue(dev);
#ifdef DEBUG_CONFIG_TRACE
	printk(KERN_DEBUG "%s: <-wv_cu_start()\n", dev->name);
#endif
	return 0;
}

/*------------------------------------------------------------------*/
/*
 * This routine does a standard configuration of the WaveLAN 
 * controller (i82586).
 *
 * It initialises the scp, iscp and scb structure
 * The first two are just pointers to the next.
 * The last one is used for basic configuration and for basic
 * communication (interrupt status).
 *
 * (called by wv_hw_reset())
 */
static int wv_82586_start(struct net_device * dev)
{
	net_local *lp = (net_local *) dev->priv;
	unsigned long ioaddr = dev->base_addr;
	scp_t scp;		/* system configuration pointer */
	iscp_t iscp;		/* intermediate scp */
	scb_t scb;		/* system control block */
	ach_t cb;		/* Action command header */
	u8 zeroes[512];
	int i;

#ifdef DEBUG_CONFIG_TRACE
	printk(KERN_DEBUG "%s: ->wv_82586_start()\n", dev->name);
#endif

	/*
	 * Clear the onboard RAM.
	 */
	memset(&zeroes[0], 0x00, sizeof(zeroes));
	for (i = 0; i < I82586_MEMZ; i += sizeof(zeroes))
		obram_write(ioaddr, i, &zeroes[0], sizeof(zeroes));

	/*
	 * Construct the command unit structures:
	 * scp, iscp, scb, cb.
	 */
	memset(&scp, 0x00, sizeof(scp));
	scp.scp_sysbus = SCP_SY_16BBUS;
	scp.scp_iscpl = OFFSET_ISCP;
	obram_write(ioaddr, OFFSET_SCP, (unsigned char *) &scp,
		    sizeof(scp));

	memset(&iscp, 0x00, sizeof(iscp));
	iscp.iscp_busy = 1;
	iscp.iscp_offset = OFFSET_SCB;
	obram_write(ioaddr, OFFSET_ISCP, (unsigned char *) &iscp,
		    sizeof(iscp));

	/* Our first command is to reset the i82586. */
	memset(&scb, 0x00, sizeof(scb));
	scb.scb_command = SCB_CMD_RESET;
	scb.scb_cbl_offset = OFFSET_CU;
	scb.scb_rfa_offset = OFFSET_RU;
	obram_write(ioaddr, OFFSET_SCB, (unsigned char *) &scb,
		    sizeof(scb));

	set_chan_attn(ioaddr, lp->hacr);

	/* Wait for command to finish. */
	for (i = 1000; i > 0; i--) {
		obram_read(ioaddr, OFFSET_ISCP, (unsigned char *) &iscp,
			   sizeof(iscp));

		if (iscp.iscp_busy == (unsigned short) 0)
			break;

		udelay(10);
	}

	if (i <= 0) {
#ifdef DEBUG_CONFIG_ERROR
		printk(KERN_INFO
		       "%s: wv_82586_start(): iscp_busy timeout.\n",
		       dev->name);
#endif
		return -1;
	}

	/* Check command completion. */
	for (i = 15; i > 0; i--) {
		obram_read(ioaddr, OFFSET_SCB, (unsigned char *) &scb,
			   sizeof(scb));

		if (scb.scb_status == (SCB_ST_CX | SCB_ST_CNA))
			break;

		udelay(10);
	}

	if (i <= 0) {
#ifdef DEBUG_CONFIG_ERROR
		printk(KERN_INFO
		       "%s: wv_82586_start(): status: expected 0x%02x, got 0x%02x.\n",
		       dev->name, SCB_ST_CX | SCB_ST_CNA, scb.scb_status);
#endif
		return -1;
	}

	wv_ack(dev);

	/* Set the action command header. */
	memset(&cb, 0x00, sizeof(cb));
	cb.ac_command = AC_CFLD_EL | (AC_CFLD_CMD & acmd_diagnose);
	cb.ac_link = OFFSET_CU;
	obram_write(ioaddr, OFFSET_CU, (unsigned char *) &cb, sizeof(cb));

	if (wv_synchronous_cmd(dev, "diag()") == -1)
		return -1;

	obram_read(ioaddr, OFFSET_CU, (unsigned char *) &cb, sizeof(cb));
	if (cb.ac_status & AC_SFLD_FAIL) {
#ifdef DEBUG_CONFIG_ERROR
		printk(KERN_INFO
		       "%s: wv_82586_start(): i82586 Self Test failed.\n",
		       dev->name);
#endif
		return -1;
	}
#ifdef DEBUG_I82586_SHOW
	wv_scb_show(ioaddr);
#endif

#ifdef DEBUG_CONFIG_TRACE
	printk(KERN_DEBUG "%s: <-wv_82586_start()\n", dev->name);
#endif
	return 0;
}

/*------------------------------------------------------------------*/
/*
 * This routine does a standard configuration of the WaveLAN
 * controller (i82586).
 *
 * This routine is a violent hack. We use the first free transmit block
 * to make our configuration. In the buffer area, we create the three
 * configuration commands (linked). We make the previous NOP point to
 * the beginning of the buffer instead of the tx command. After, we go
 * as usual to the NOP command.
 * Note that only the last command (mc_set) will generate an interrupt.
 *
 * (called by wv_hw_reset(), wv_82586_reconfig(), wavelan_packet_xmit())
 */
static void wv_82586_config(struct net_device * dev)
{
	net_local *lp = (net_local *) dev->priv;
	unsigned long ioaddr = dev->base_addr;
	unsigned short txblock;
	unsigned short txpred;
	unsigned short tx_addr;
	unsigned short nop_addr;
	unsigned short tbd_addr;
	unsigned short cfg_addr;
	unsigned short ias_addr;
	unsigned short mcs_addr;
	ac_tx_t tx;
	ac_nop_t nop;
	ac_cfg_t cfg;		/* Configure action */
	ac_ias_t ias;		/* IA-setup action */
	ac_mcs_t mcs;		/* Multicast setup */
	struct dev_mc_list *dmi;

#ifdef DEBUG_CONFIG_TRACE
	printk(KERN_DEBUG "%s: ->wv_82586_config()\n", dev->name);
#endif

	/* Check nothing bad has happened */
	if (lp->tx_n_in_use == (NTXBLOCKS - 1)) {
#ifdef DEBUG_CONFIG_ERROR
		printk(KERN_INFO "%s: wv_82586_config(): Tx queue full.\n",
		       dev->name);
#endif
		return;
	}

	/* Calculate addresses of next block and previous block. */
	txblock = lp->tx_first_free;
	txpred = txblock - TXBLOCKZ;
	if (txpred < OFFSET_CU)
		txpred += NTXBLOCKS * TXBLOCKZ;
	lp->tx_first_free += TXBLOCKZ;
	if (lp->tx_first_free >= OFFSET_CU + NTXBLOCKS * TXBLOCKZ)
		lp->tx_first_free -= NTXBLOCKS * TXBLOCKZ;

	lp->tx_n_in_use++;

	/* Calculate addresses of the different parts of the block. */
	tx_addr = txblock;
	nop_addr = tx_addr + sizeof(tx);
	tbd_addr = nop_addr + sizeof(nop);
	cfg_addr = tbd_addr + sizeof(tbd_t);	/* beginning of the buffer */
	ias_addr = cfg_addr + sizeof(cfg);
	mcs_addr = ias_addr + sizeof(ias);

	/*
	 * Transmit command
	 */
	tx.tx_h.ac_status = 0xFFFF;	/* Fake completion value */
	obram_write(ioaddr, toff(ac_tx_t, tx_addr, tx_h.ac_status),
		    (unsigned char *) &tx.tx_h.ac_status,
		    sizeof(tx.tx_h.ac_status));

	/*
	 * NOP command
	 */
	nop.nop_h.ac_status = 0;
	obram_write(ioaddr, toff(ac_nop_t, nop_addr, nop_h.ac_status),
		    (unsigned char *) &nop.nop_h.ac_status,
		    sizeof(nop.nop_h.ac_status));
	nop.nop_h.ac_link = nop_addr;
	obram_write(ioaddr, toff(ac_nop_t, nop_addr, nop_h.ac_link),
		    (unsigned char *) &nop.nop_h.ac_link,
		    sizeof(nop.nop_h.ac_link));

	/* Create a configure action. */
	memset(&cfg, 0x00, sizeof(cfg));

	/*
	 * For Linux we invert AC_CFG_ALOC() so as to conform
	 * to the way that net packets reach us from above.
	 * (See also ac_tx_t.)
	 *
	 * Updated from Wavelan Manual WCIN085B
	 */
	cfg.cfg_byte_cnt =
	    AC_CFG_BYTE_CNT(sizeof(ac_cfg_t) - sizeof(ach_t));
	cfg.cfg_fifolim = AC_CFG_FIFOLIM(4);
	cfg.cfg_byte8 = AC_CFG_SAV_BF(1) | AC_CFG_SRDY(0);
	cfg.cfg_byte9 = AC_CFG_ELPBCK(0) |
	    AC_CFG_ILPBCK(0) |
	    AC_CFG_PRELEN(AC_CFG_PLEN_2) |
	    AC_CFG_ALOC(1) | AC_CFG_ADDRLEN(WAVELAN_ADDR_SIZE);
	cfg.cfg_byte10 = AC_CFG_BOFMET(1) |
	    AC_CFG_ACR(6) | AC_CFG_LINPRIO(0);
	cfg.cfg_ifs = 0x20;
	cfg.cfg_slotl = 0x0C;
	cfg.cfg_byte13 = AC_CFG_RETRYNUM(15) | AC_CFG_SLTTMHI(0);
	cfg.cfg_byte14 = AC_CFG_FLGPAD(0) |
	    AC_CFG_BTSTF(0) |
	    AC_CFG_CRC16(0) |
	    AC_CFG_NCRC(0) |
	    AC_CFG_TNCRS(1) |
	    AC_CFG_MANCH(0) |
	    AC_CFG_BCDIS(0) | AC_CFG_PRM(lp->promiscuous);
	cfg.cfg_byte15 = AC_CFG_ICDS(0) |
	    AC_CFG_CDTF(0) | AC_CFG_ICSS(0) | AC_CFG_CSTF(0);
/*
  cfg.cfg_min_frm_len = AC_CFG_MNFRM(64);
*/
	cfg.cfg_min_frm_len = AC_CFG_MNFRM(8);

	cfg.cfg_h.ac_command = (AC_CFLD_CMD & acmd_configure);
	cfg.cfg_h.ac_link = ias_addr;
	obram_write(ioaddr, cfg_addr, (unsigned char *) &cfg, sizeof(cfg));

	/* Set up the MAC address */
	memset(&ias, 0x00, sizeof(ias));
	ias.ias_h.ac_command = (AC_CFLD_CMD & acmd_ia_setup);
	ias.ias_h.ac_link = mcs_addr;
	memcpy(&ias.ias_addr[0], (unsigned char *) &dev->dev_addr[0],
	       sizeof(ias.ias_addr));
	obram_write(ioaddr, ias_addr, (unsigned char *) &ias, sizeof(ias));

	/* Initialize adapter's Ethernet multicast addresses */
	memset(&mcs, 0x00, sizeof(mcs));
	mcs.mcs_h.ac_command = AC_CFLD_I | (AC_CFLD_CMD & acmd_mc_setup);
	mcs.mcs_h.ac_link = nop_addr;
	mcs.mcs_cnt = WAVELAN_ADDR_SIZE * lp->mc_count;
	obram_write(ioaddr, mcs_addr, (unsigned char *) &mcs, sizeof(mcs));

	/* Any address to set? */
	if (lp->mc_count) {
		for (dmi = dev->mc_list; dmi; dmi = dmi->next)
			outsw(PIOP1(ioaddr), (u16 *) dmi->dmi_addr,
			      WAVELAN_ADDR_SIZE >> 1);

#ifdef DEBUG_CONFIG_INFO
		printk(KERN_DEBUG
		       "%s: wv_82586_config(): set %d multicast addresses:\n",
		       dev->name, lp->mc_count);
		for (dmi = dev->mc_list; dmi; dmi = dmi->next)
			printk(KERN_DEBUG
			       " %02x:%02x:%02x:%02x:%02x:%02x\n",
			       dmi->dmi_addr[0], dmi->dmi_addr[1],
			       dmi->dmi_addr[2], dmi->dmi_addr[3],
			       dmi->dmi_addr[4], dmi->dmi_addr[5]);
#endif
	}

	/*
	 * Overwrite the predecessor NOP link
	 * so that it points to the configure action.
	 */
	nop_addr = txpred + sizeof(tx);
	nop.nop_h.ac_status = 0;
	obram_write(ioaddr, toff(ac_nop_t, nop_addr, nop_h.ac_status),
		    (unsigned char *) &nop.nop_h.ac_status,
		    sizeof(nop.nop_h.ac_status));
	nop.nop_h.ac_link = cfg_addr;
	obram_write(ioaddr, toff(ac_nop_t, nop_addr, nop_h.ac_link),
		    (unsigned char *) &nop.nop_h.ac_link,
		    sizeof(nop.nop_h.ac_link));

	/* Job done, clear the flag */
	lp->reconfig_82586 = 0;

	if (lp->tx_first_in_use == I82586NULL)
		lp->tx_first_in_use = txblock;

	if (lp->tx_n_in_use == (NTXBLOCKS - 1))
		netif_stop_queue(dev);

#ifdef DEBUG_CONFIG_TRACE
	printk(KERN_DEBUG "%s: <-wv_82586_config()\n", dev->name);
#endif
}

/*------------------------------------------------------------------*/
/*
 * This routine, called by wavelan_close(), gracefully stops the 
 * WaveLAN controller (i82586).
 * (called by wavelan_close())
 */
static void wv_82586_stop(struct net_device * dev)
{
	net_local *lp = (net_local *) dev->priv;
	unsigned long ioaddr = dev->base_addr;
	u16 scb_cmd;

#ifdef DEBUG_CONFIG_TRACE
	printk(KERN_DEBUG "%s: ->wv_82586_stop()\n", dev->name);
#endif

	/* Suspend both command unit and receive unit. */
	scb_cmd =
	    (SCB_CMD_CUC & SCB_CMD_CUC_SUS) | (SCB_CMD_RUC &
					       SCB_CMD_RUC_SUS);
	obram_write(ioaddr, scboff(OFFSET_SCB, scb_command),
		    (unsigned char *) &scb_cmd, sizeof(scb_cmd));
	set_chan_attn(ioaddr, lp->hacr);

	/* No more interrupts */
	wv_ints_off(dev);

#ifdef DEBUG_CONFIG_TRACE
	printk(KERN_DEBUG "%s: <-wv_82586_stop()\n", dev->name);
#endif
}

/*------------------------------------------------------------------*/
/*
 * Totally reset the WaveLAN and restart it.
 * Performs the following actions:
 *	1. A power reset (reset DMA)
 *	2. Initialize the radio modem (using wv_mmc_init)
 *	3. Reset & Configure LAN controller (using wv_82586_start)
 *	4. Start the LAN controller's command unit
 *	5. Start the LAN controller's receive unit
 * (called by wavelan_interrupt(), wavelan_watchdog() & wavelan_open())
 */
static int wv_hw_reset(struct net_device * dev)
{
	net_local *lp = (net_local *) dev->priv;
	unsigned long ioaddr = dev->base_addr;

#ifdef DEBUG_CONFIG_TRACE
	printk(KERN_DEBUG "%s: ->wv_hw_reset(dev=0x%x)\n", dev->name,
	       (unsigned int) dev);
#endif

	/* Increase the number of resets done. */
	lp->nresets++;

	wv_hacr_reset(ioaddr);
	lp->hacr = HACR_DEFAULT;

	if ((wv_mmc_init(dev) < 0) || (wv_82586_start(dev) < 0))
		return -1;

	/* Enable the card to send interrupts. */
	wv_ints_on(dev);

	/* Start card functions */
	if (wv_cu_start(dev) < 0)
		return -1;

	/* Setup the controller and parameters */
	wv_82586_config(dev);

	/* Finish configuration with the receive unit */
	if (wv_ru_start(dev) < 0)
		return -1;

#ifdef DEBUG_CONFIG_TRACE
	printk(KERN_DEBUG "%s: <-wv_hw_reset()\n", dev->name);
#endif
	return 0;
}

/*------------------------------------------------------------------*/
/*
 * Check if there is a WaveLAN at the specific base address.
 * As a side effect, this reads the MAC address.
 * (called in wavelan_probe() and init_module())
 */
static int wv_check_ioaddr(unsigned long ioaddr, u8 * mac)
{
	int i;			/* Loop counter */

	/* Check if the base address if available. */
	if (!request_region(ioaddr, sizeof(ha_t), "wavelan probe"))
		return -EBUSY;		/* ioaddr already used */

	/* Reset host interface */
	wv_hacr_reset(ioaddr);

	/* Read the MAC address from the parameter storage area. */
	psa_read(ioaddr, HACR_DEFAULT, psaoff(0, psa_univ_mac_addr),
		 mac, 6);

	release_region(ioaddr, sizeof(ha_t));

	/*
	 * Check the first three octets of the address for the manufacturer's code.
	 * Note: if this can't find your WaveLAN card, you've got a
	 * non-NCR/AT&T/Lucent ISA card.  See wavelan.p.h for detail on
	 * how to configure your card.
	 */
	for (i = 0; i < (sizeof(MAC_ADDRESSES) / sizeof(char) / 3); i++)
		if ((mac[0] == MAC_ADDRESSES[i][0]) &&
		    (mac[1] == MAC_ADDRESSES[i][1]) &&
		    (mac[2] == MAC_ADDRESSES[i][2]))
			return 0;

#ifdef DEBUG_CONFIG_INFO
	printk(KERN_WARNING
	       "WaveLAN (0x%3X): your MAC address might be %02X:%02X:%02X.\n",
	       ioaddr, mac[0], mac[1], mac[2]);
#endif
	return -ENODEV;
}

/************************ INTERRUPT HANDLING ************************/

/*
 * This function is the interrupt handler for the WaveLAN card. This
 * routine will be called whenever: 
 */
static irqreturn_t wavelan_interrupt(int irq, void *dev_id)
{
	struct net_device *dev;
	unsigned long ioaddr;
	net_local *lp;
	u16 hasr;
	u16 status;
	u16 ack_cmd;

	dev = dev_id;

#ifdef DEBUG_INTERRUPT_TRACE
	printk(KERN_DEBUG "%s: ->wavelan_interrupt()\n", dev->name);
#endif

	lp = (net_local *) dev->priv;
	ioaddr = dev->base_addr;

#ifdef DEBUG_INTERRUPT_INFO
	/* Check state of our spinlock */
	if(spin_is_locked(&lp->spinlock))
		printk(KERN_DEBUG
		       "%s: wavelan_interrupt(): spinlock is already locked !!!\n",
		       dev->name);
#endif

	/* Prevent reentrancy. We need to do that because we may have
	 * multiple interrupt handler running concurrently.
	 * It is safe because interrupts are disabled before acquiring
	 * the spinlock. */
	spin_lock(&lp->spinlock);

	/* We always had spurious interrupts at startup, but lately I
	 * saw them comming *between* the request_irq() and the
	 * spin_lock_irqsave() in wavelan_open(), so the spinlock
	 * protection is no enough.
	 * So, we also check lp->hacr that will tell us is we enabled
	 * irqs or not (see wv_ints_on()).
	 * We can't use netif_running(dev) because we depend on the
	 * proper processing of the irq generated during the config. */

	/* Which interrupt it is ? */
	hasr = hasr_read(ioaddr);

#ifdef DEBUG_INTERRUPT_INFO
	printk(KERN_INFO
	       "%s: wavelan_interrupt(): hasr 0x%04x; hacr 0x%04x.\n",
	       dev->name, hasr, lp->hacr);
#endif

	/* Check modem interrupt */
	if ((hasr & HASR_MMC_INTR) && (lp->hacr & HACR_MMC_INT_ENABLE)) {
		u8 dce_status;

		/*
		 * Interrupt from the modem management controller.
		 * This will clear it -- ignored for now.
		 */
		mmc_read(ioaddr, mmroff(0, mmr_dce_status), &dce_status,
			 sizeof(dce_status));

#ifdef DEBUG_INTERRUPT_ERROR
		printk(KERN_INFO
		       "%s: wavelan_interrupt(): unexpected mmc interrupt: status 0x%04x.\n",
		       dev->name, dce_status);
#endif
	}

	/* Check if not controller interrupt */
	if (((hasr & HASR_82586_INTR) == 0) ||
	    ((lp->hacr & HACR_82586_INT_ENABLE) == 0)) {
#ifdef DEBUG_INTERRUPT_ERROR
		printk(KERN_INFO
		       "%s: wavelan_interrupt(): interrupt not coming from i82586 - hasr 0x%04x.\n",
		       dev->name, hasr);
#endif
		spin_unlock (&lp->spinlock);
		return IRQ_NONE;
	}

	/* Read interrupt data. */
	obram_read(ioaddr, scboff(OFFSET_SCB, scb_status),
		   (unsigned char *) &status, sizeof(status));

	/*
	 * Acknowledge the interrupt(s).
	 */
	ack_cmd = status & SCB_ST_INT;
	obram_write(ioaddr, scboff(OFFSET_SCB, scb_command),
		    (unsigned char *) &ack_cmd, sizeof(ack_cmd));
	set_chan_attn(ioaddr, lp->hacr);

#ifdef DEBUG_INTERRUPT_INFO
	printk(KERN_DEBUG "%s: wavelan_interrupt(): status 0x%04x.\n",
	       dev->name, status);
#endif

	/* Command completed. */
	if ((status & SCB_ST_CX) == SCB_ST_CX) {
#ifdef DEBUG_INTERRUPT_INFO
		printk(KERN_DEBUG
		       "%s: wavelan_interrupt(): command completed.\n",
		       dev->name);
#endif
		wv_complete(dev, ioaddr, lp);
	}

	/* Frame received. */
	if ((status & SCB_ST_FR) == SCB_ST_FR) {
#ifdef DEBUG_INTERRUPT_INFO
		printk(KERN_DEBUG
		       "%s: wavelan_interrupt(): received packet.\n",
		       dev->name);
#endif
		wv_receive(dev);
	}

	/* Check the state of the command unit. */
	if (((status & SCB_ST_CNA) == SCB_ST_CNA) ||
	    (((status & SCB_ST_CUS) != SCB_ST_CUS_ACTV) &&
	     (netif_running(dev)))) {
#ifdef DEBUG_INTERRUPT_ERROR
		printk(KERN_INFO
		       "%s: wavelan_interrupt(): CU inactive -- restarting\n",
		       dev->name);
#endif
		wv_hw_reset(dev);
	}

	/* Check the state of the command unit. */
	if (((status & SCB_ST_RNR) == SCB_ST_RNR) ||
	    (((status & SCB_ST_RUS) != SCB_ST_RUS_RDY) &&
	     (netif_running(dev)))) {
#ifdef DEBUG_INTERRUPT_ERROR
		printk(KERN_INFO
		       "%s: wavelan_interrupt(): RU not ready -- restarting\n",
		       dev->name);
#endif
		wv_hw_reset(dev);
	}

	/* Release spinlock */
	spin_unlock (&lp->spinlock);

#ifdef DEBUG_INTERRUPT_TRACE
	printk(KERN_DEBUG "%s: <-wavelan_interrupt()\n", dev->name);
#endif
	return IRQ_HANDLED;
}

/*------------------------------------------------------------------*/
/*
 * Watchdog: when we start a transmission, a timer is set for us in the
 * kernel.  If the transmission completes, this timer is disabled. If
 * the timer expires, we are called and we try to unlock the hardware.
 */
static void wavelan_watchdog(struct net_device *	dev)
{
	net_local *	lp = (net_local *)dev->priv;
	u_long		ioaddr = dev->base_addr;
	unsigned long	flags;
	unsigned int	nreaped;

#ifdef DEBUG_INTERRUPT_TRACE
	printk(KERN_DEBUG "%s: ->wavelan_watchdog()\n", dev->name);
#endif

#ifdef DEBUG_INTERRUPT_ERROR
	printk(KERN_INFO "%s: wavelan_watchdog: watchdog timer expired\n",
	       dev->name);
#endif

	/* Check that we came here for something */
	if (lp->tx_n_in_use <= 0) {
		return;
	}

	spin_lock_irqsave(&lp->spinlock, flags);

	/* Try to see if some buffers are not free (in case we missed
	 * an interrupt */
	nreaped = wv_complete(dev, ioaddr, lp);

#ifdef DEBUG_INTERRUPT_INFO
	printk(KERN_DEBUG
	       "%s: wavelan_watchdog(): %d reaped, %d remain.\n",
	       dev->name, nreaped, lp->tx_n_in_use);
#endif

#ifdef DEBUG_PSA_SHOW
	{
		psa_t psa;
		psa_read(dev, 0, (unsigned char *) &psa, sizeof(psa));
		wv_psa_show(&psa);
	}
#endif
#ifdef DEBUG_MMC_SHOW
	wv_mmc_show(dev);
#endif
#ifdef DEBUG_I82586_SHOW
	wv_cu_show(dev);
#endif

	/* If no buffer has been freed */
	if (nreaped == 0) {
#ifdef DEBUG_INTERRUPT_ERROR
		printk(KERN_INFO
		       "%s: wavelan_watchdog(): cleanup failed, trying reset\n",
		       dev->name);
#endif
		wv_hw_reset(dev);
	}

	/* At this point, we should have some free Tx buffer ;-) */
	if (lp->tx_n_in_use < NTXBLOCKS - 1)
		netif_wake_queue(dev);

	spin_unlock_irqrestore(&lp->spinlock, flags);
	
#ifdef DEBUG_INTERRUPT_TRACE
	printk(KERN_DEBUG "%s: <-wavelan_watchdog()\n", dev->name);
#endif
}

/********************* CONFIGURATION CALLBACKS *********************/
/*
 * Here are the functions called by the Linux networking code (NET3)
 * for initialization, configuration and deinstallations of the 
 * WaveLAN ISA hardware.
 */

/*------------------------------------------------------------------*/
/*
 * Configure and start up the WaveLAN PCMCIA adaptor.
 * Called by NET3 when it "opens" the device.
 */
static int wavelan_open(struct net_device * dev)
{
	net_local *	lp = (net_local *)dev->priv;
	unsigned long	flags;

#ifdef DEBUG_CALLBACK_TRACE
	printk(KERN_DEBUG "%s: ->wavelan_open(dev=0x%x)\n", dev->name,
	       (unsigned int) dev);
#endif

	/* Check irq */
	if (dev->irq == 0) {
#ifdef DEBUG_CONFIG_ERROR
		printk(KERN_WARNING "%s: wavelan_open(): no IRQ\n",
		       dev->name);
#endif
		return -ENXIO;
	}

	if (request_irq(dev->irq, &wavelan_interrupt, 0, "WaveLAN", dev) != 0) 
	{
#ifdef DEBUG_CONFIG_ERROR
		printk(KERN_WARNING "%s: wavelan_open(): invalid IRQ\n",
		       dev->name);
#endif
		return -EAGAIN;
	}

	spin_lock_irqsave(&lp->spinlock, flags);
	
	if (wv_hw_reset(dev) != -1) {
		netif_start_queue(dev);
	} else {
		free_irq(dev->irq, dev);
#ifdef DEBUG_CONFIG_ERROR
		printk(KERN_INFO
		       "%s: wavelan_open(): impossible to start the card\n",
		       dev->name);
#endif
		spin_unlock_irqrestore(&lp->spinlock, flags);
		return -EAGAIN;
	}
	spin_unlock_irqrestore(&lp->spinlock, flags);
	
#ifdef DEBUG_CALLBACK_TRACE
	printk(KERN_DEBUG "%s: <-wavelan_open()\n", dev->name);
#endif
	return 0;
}

/*------------------------------------------------------------------*/
/*
 * Shut down the WaveLAN ISA card.
 * Called by NET3 when it "closes" the device.
 */
static int wavelan_close(struct net_device * dev)
{
	net_local *lp = (net_local *) dev->priv;
	unsigned long flags;

#ifdef DEBUG_CALLBACK_TRACE
	printk(KERN_DEBUG "%s: ->wavelan_close(dev=0x%x)\n", dev->name,
	       (unsigned int) dev);
#endif

	netif_stop_queue(dev);

	/*
	 * Flush the Tx and disable Rx.
	 */
	spin_lock_irqsave(&lp->spinlock, flags);
	wv_82586_stop(dev);
	spin_unlock_irqrestore(&lp->spinlock, flags);

	free_irq(dev->irq, dev);

#ifdef DEBUG_CALLBACK_TRACE
	printk(KERN_DEBUG "%s: <-wavelan_close()\n", dev->name);
#endif
	return 0;
}

/*------------------------------------------------------------------*/
/*
 * Probe an I/O address, and if the WaveLAN is there configure the
 * device structure
 * (called by wavelan_probe() and via init_module()).
 */
static int __init wavelan_config(struct net_device *dev, unsigned short ioaddr)
{
	u8 irq_mask;
	int irq;
	net_local *lp;
	mac_addr mac;
	int err;

	if (!request_region(ioaddr, sizeof(ha_t), "wavelan"))
		return -EADDRINUSE;

	err = wv_check_ioaddr(ioaddr, mac);
	if (err)
		goto out;

	memcpy(dev->dev_addr, mac, 6);

	dev->base_addr = ioaddr;

#ifdef DEBUG_CALLBACK_TRACE
	printk(KERN_DEBUG "%s: ->wavelan_config(dev=0x%x, ioaddr=0x%lx)\n",
	       dev->name, (unsigned int) dev, ioaddr);
#endif

	/* Check IRQ argument on command line. */
	if (dev->irq != 0) {
		irq_mask = wv_irq_to_psa(dev->irq);

		if (irq_mask == 0) {
#ifdef DEBUG_CONFIG_ERROR
			printk(KERN_WARNING
			       "%s: wavelan_config(): invalid IRQ %d ignored.\n",
			       dev->name, dev->irq);
#endif
			dev->irq = 0;
		} else {
#ifdef DEBUG_CONFIG_INFO
			printk(KERN_DEBUG
			       "%s: wavelan_config(): changing IRQ to %d\n",
			       dev->name, dev->irq);
#endif
			psa_write(ioaddr, HACR_DEFAULT,
				  psaoff(0, psa_int_req_no), &irq_mask, 1);
			/* update the Wavelan checksum */
			update_psa_checksum(dev, ioaddr, HACR_DEFAULT);
			wv_hacr_reset(ioaddr);
		}
	}

	psa_read(ioaddr, HACR_DEFAULT, psaoff(0, psa_int_req_no),
		 &irq_mask, 1);
	if ((irq = wv_psa_to_irq(irq_mask)) == -1) {
#ifdef DEBUG_CONFIG_ERROR
		printk(KERN_INFO
		       "%s: wavelan_config(): could not wavelan_map_irq(%d).\n",
		       dev->name, irq_mask);
#endif
		err = -EAGAIN;
		goto out;
	}

	dev->irq = irq;

	dev->mem_start = 0x0000;
	dev->mem_end = 0x0000;
	dev->if_port = 0;

	/* Initialize device structures */
	memset(dev->priv, 0, sizeof(net_local));
	lp = (net_local *) dev->priv;

	/* Back link to the device structure. */
	lp->dev = dev;
	/* Add the device at the beginning of the linked list. */
	lp->next = wavelan_list;
	wavelan_list = lp;

	lp->hacr = HACR_DEFAULT;

	/* Multicast stuff */
	lp->promiscuous = 0;
	lp->mc_count = 0;

	/* Init spinlock */
	spin_lock_init(&lp->spinlock);

	dev->open = wavelan_open;
	dev->stop = wavelan_close;
	dev->hard_start_xmit = wavelan_packet_xmit;
	dev->get_stats = wavelan_get_stats;
	dev->set_multicast_list = &wavelan_set_multicast_list;
        dev->tx_timeout		= &wavelan_watchdog;
        dev->watchdog_timeo	= WATCHDOG_JIFFIES;
#ifdef SET_MAC_ADDRESS
	dev->set_mac_address = &wavelan_set_mac_address;
#endif				/* SET_MAC_ADDRESS */

	dev->wireless_handlers = &wavelan_handler_def;
	lp->wireless_data.spy_data = &lp->spy_data;
	dev->wireless_data = &lp->wireless_data;

	dev->mtu = WAVELAN_MTU;

	/* Display nice information. */
	wv_init_info(dev);

#ifdef DEBUG_CALLBACK_TRACE
	printk(KERN_DEBUG "%s: <-wavelan_config()\n", dev->name);
#endif
	return 0;
out:
	release_region(ioaddr, sizeof(ha_t));
	return err;
}

/*------------------------------------------------------------------*/
/*
 * Check for a network adaptor of this type.  Return '0' iff one 
 * exists.  There seem to be different interpretations of
 * the initial value of dev->base_addr.
 * We follow the example in drivers/net/ne.c.
 * (called in "Space.c")
 */
struct net_device * __init wavelan_probe(int unit)
{
	struct net_device *dev;
	short base_addr;
	int def_irq;
	int i;
	int r = 0;

#ifdef	STRUCT_CHECK
	if (wv_struct_check() != (char *) NULL) {
		printk(KERN_WARNING
		       "%s: wavelan_probe(): structure/compiler botch: \"%s\"\n",
		       dev->name, wv_struct_check());
		return -ENODEV;
	}
#endif				/* STRUCT_CHECK */

	dev = alloc_etherdev(sizeof(net_local));
	if (!dev)
		return ERR_PTR(-ENOMEM);

	sprintf(dev->name, "eth%d", unit);
	netdev_boot_setup_check(dev);
	base_addr = dev->base_addr;
	def_irq = dev->irq;

#ifdef DEBUG_CALLBACK_TRACE
	printk(KERN_DEBUG
	       "%s: ->wavelan_probe(dev=%p (base_addr=0x%x))\n",
	       dev->name, dev, (unsigned int) dev->base_addr);
#endif

	/* Don't probe at all. */
	if (base_addr < 0) {
#ifdef DEBUG_CONFIG_ERROR
		printk(KERN_WARNING
		       "%s: wavelan_probe(): invalid base address\n",
		       dev->name);
#endif
		r = -ENXIO;
	} else if (base_addr > 0x100) { /* Check a single specified location. */
		r = wavelan_config(dev, base_addr);
#ifdef DEBUG_CONFIG_INFO
		if (r != 0)
			printk(KERN_DEBUG
			       "%s: wavelan_probe(): no device at specified base address (0x%X) or address already in use\n",
			       dev->name, base_addr);
#endif

#ifdef DEBUG_CALLBACK_TRACE
		printk(KERN_DEBUG "%s: <-wavelan_probe()\n", dev->name);
#endif
	} else { /* Scan all possible addresses of the WaveLAN hardware. */
		for (i = 0; i < ARRAY_SIZE(iobase); i++) {
			dev->irq = def_irq;
			if (wavelan_config(dev, iobase[i]) == 0) {
#ifdef DEBUG_CALLBACK_TRACE
				printk(KERN_DEBUG
				       "%s: <-wavelan_probe()\n",
				       dev->name);
#endif
				break;
			}
		}
		if (i == ARRAY_SIZE(iobase))
			r = -ENODEV;
	}
	if (r) 
		goto out;
	r = register_netdev(dev);
	if (r)
		goto out1;
	return dev;
out1:
	release_region(dev->base_addr, sizeof(ha_t));
	wavelan_list = wavelan_list->next;
out:
	free_netdev(dev);
	return ERR_PTR(r);
}

/****************************** MODULE ******************************/
/*
 * Module entry point: insertion and removal
 */

#ifdef	MODULE
/*------------------------------------------------------------------*/
/*
 * Insertion of the module
 * I'm now quite proud of the multi-device support.
 */
int __init init_module(void)
{
	int ret = -EIO;		/* Return error if no cards found */
	int i;

#ifdef DEBUG_MODULE_TRACE
	printk(KERN_DEBUG "-> init_module()\n");
#endif

	/* If probing is asked */
	if (io[0] == 0) {
#ifdef DEBUG_CONFIG_ERROR
		printk(KERN_WARNING
		       "WaveLAN init_module(): doing device probing (bad !)\n");
		printk(KERN_WARNING
		       "Specify base addresses while loading module to correct the problem\n");
#endif

		/* Copy the basic set of address to be probed. */
		for (i = 0; i < ARRAY_SIZE(iobase); i++)
			io[i] = iobase[i];
	}


	/* Loop on all possible base addresses. */
	i = -1;
	while ((io[++i] != 0) && (i < ARRAY_SIZE(io))) {
		struct net_device *dev = alloc_etherdev(sizeof(net_local));
		if (!dev)
			break;
		if (name[i])
			strcpy(dev->name, name[i]);	/* Copy name */
		dev->base_addr = io[i];
		dev->irq = irq[i];

		/* Check if there is something at this base address. */
		if (wavelan_config(dev, io[i]) == 0) {
			if (register_netdev(dev) != 0) {
				release_region(dev->base_addr, sizeof(ha_t));
				wavelan_list = wavelan_list->next;
			} else {
				ret = 0;
				continue;
			}
		}
		free_netdev(dev);
	}

#ifdef DEBUG_CONFIG_ERROR
	if (!wavelan_list)
		printk(KERN_WARNING
		       "WaveLAN init_module(): no device found\n");
#endif

#ifdef DEBUG_MODULE_TRACE
	printk(KERN_DEBUG "<- init_module()\n");
#endif
	return ret;
}

/*------------------------------------------------------------------*/
/*
 * Removal of the module
 */
void cleanup_module(void)
{
#ifdef DEBUG_MODULE_TRACE
	printk(KERN_DEBUG "-> cleanup_module()\n");
#endif

	/* Loop on all devices and release them. */
	while (wavelan_list) {
		struct net_device *dev = wavelan_list->dev;

#ifdef DEBUG_CONFIG_INFO
		printk(KERN_DEBUG
		       "%s: cleanup_module(): removing device at 0x%x\n",
		       dev->name, (unsigned int) dev);
#endif
		unregister_netdev(dev);

		release_region(dev->base_addr, sizeof(ha_t));
		wavelan_list = wavelan_list->next;

		free_netdev(dev);
	}

#ifdef DEBUG_MODULE_TRACE
	printk(KERN_DEBUG "<- cleanup_module()\n");
#endif
}
#endif				/* MODULE */
MODULE_LICENSE("GPL");

/*
 * This software may only be used and distributed
 * according to the terms of the GNU General Public License.
 *
 * This software was developed as a component of the
 * Linux operating system.
 * It is based on other device drivers and information
 * either written or supplied by:
 *	Ajay Bakre (bakre@paul.rutgers.edu),
 *	Donald Becker (becker@scyld.com),
 *	Loeke Brederveld (Loeke.Brederveld@Utrecht.NCR.com),
 *	Anders Klemets (klemets@it.kth.se),
 *	Vladimir V. Kolpakov (w@stier.koenig.ru),
 *	Marc Meertens (Marc.Meertens@Utrecht.NCR.com),
 *	Pauline Middelink (middelin@polyware.iaf.nl),
 *	Robert Morris (rtm@das.harvard.edu),
 *	Jean Tourrilhes (jt@hplb.hpl.hp.com),
 *	Girish Welling (welling@paul.rutgers.edu),
 *
 * Thanks go also to:
 *	James Ashton (jaa101@syseng.anu.edu.au),
 *	Alan Cox (alan@redhat.com),
 *	Allan Creighton (allanc@cs.usyd.edu.au),
 *	Matthew Geier (matthew@cs.usyd.edu.au),
 *	Remo di Giovanni (remo@cs.usyd.edu.au),
 *	Eckhard Grah (grah@wrcs1.urz.uni-wuppertal.de),
 *	Vipul Gupta (vgupta@cs.binghamton.edu),
 *	Mark Hagan (mhagan@wtcpost.daytonoh.NCR.COM),
 *	Tim Nicholson (tim@cs.usyd.edu.au),
 *	Ian Parkin (ian@cs.usyd.edu.au),
 *	John Rosenberg (johnr@cs.usyd.edu.au),
 *	George Rossi (george@phm.gov.au),
 *	Arthur Scott (arthur@cs.usyd.edu.au),
 *	Peter Storey,
 * for their assistance and advice.
 *
 * Please send bug reports, updates, comments to:
 *
 * Bruce Janson                                    Email:  bruce@cs.usyd.edu.au
 * Basser Department of Computer Science           Phone:  +61-2-9351-3423
 * University of Sydney, N.S.W., 2006, AUSTRALIA   Fax:    +61-2-9351-3838
 */