summaryrefslogtreecommitdiffstats
path: root/uClinux-2.4.20-uc1/drivers/net/smc9194.c
blob: 738e49b26ae83eba94c2e6b8b77f61c65b4f4748 (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
/*------------------------------------------------------------------------
 . smc9194.c
 . This is a driver for SMC's 9000 series of Ethernet cards.
 .
 . Copyright (C) 1996 by Erik Stahlman
 . This software may be used and distributed according to the terms
 . of the GNU General Public License, incorporated herein by reference.
 .
 . "Features" of the SMC chip:
 .   4608 byte packet memory. ( for the 91C92.  Others have more )
 .   EEPROM for configuration
 .   AUI/TP selection  ( mine has 10Base2/10BaseT select )
 .
 . Arguments:
 . 	io		 = for the base address
 .	irq	 = for the IRQ
 .	ifport = 0 for autodetect, 1 for TP, 2 for AUI ( or 10base2 )
 .
 . author:
 . 	Erik Stahlman				( erik@vt.edu )
 . contributors:
 .      Arnaldo Carvalho de Melo <acme@conectiva.com.br>
 .
 . Hardware multicast code from Peter Cammaert ( pc@denkart.be )
 .
 . Sources:
 .    o   SMC databook
 .    o   skeleton.c by Donald Becker ( becker@scyld.com )
 .    o   ( a LOT of advice from Becker as well )
 .
 . History:
 .	12/07/95  Erik Stahlman  written, got receive/xmit handled
 . 	01/03/96  Erik Stahlman  worked out some bugs, actually usable!!! :-)
 .	01/06/96  Erik Stahlman	 cleaned up some, better testing, etc
 .	01/29/96  Erik Stahlman	 fixed autoirq, added multicast
 . 	02/01/96  Erik Stahlman	 1. disabled all interrupts in smc_reset
 .		   		 2. got rid of post-decrementing bug -- UGH.
 .	02/13/96  Erik Stahlman  Tried to fix autoirq failure.  Added more
 .				 descriptive error messages.
 .	02/15/96  Erik Stahlman  Fixed typo that caused detection failure
 . 	02/23/96  Erik Stahlman	 Modified it to fit into kernel tree
 .				 Added support to change hardware address
 .				 Cleared stats on opens
 .	02/26/96  Erik Stahlman	 Trial support for Kernel 1.2.13
 .				 Kludge for automatic IRQ detection
 .	03/04/96  Erik Stahlman	 Fixed kernel 1.3.70 +
 .				 Fixed bug reported by Gardner Buchanan in
 .				   smc_enable, with outw instead of outb
 .	03/06/96  Erik Stahlman  Added hardware multicast from Peter Cammaert
 .	04/14/00  Heiko Pruessing (SMA Regelsysteme)  Fixed bug in chip memory
 .				 allocation
 .      08/20/00  Arnaldo Melo   fix kfree(skb) in smc_hardware_send_packet
 .      12/15/00  Christian Jullien fix "Warning: kfree_skb on hard IRQ"
 .      11/08/01 Matt Domsch     Use common crc32 function
 ----------------------------------------------------------------------------*/

static const char version[] =
	"smc9194.c:v0.14 12/15/00 by Erik Stahlman (erik@vt.edu)\n";

#include <linux/module.h>
#include <linux/version.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/types.h>
#include <linux/fcntl.h>
#include <linux/interrupt.h>
#include <linux/ptrace.h>
#include <linux/ioport.h>
#include <linux/in.h>
#include <linux/slab.h>
#include <linux/string.h>
#include <linux/init.h>
#include <linux/crc32.h>
#include <asm/bitops.h>
#include <asm/io.h>
#include <linux/errno.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/skbuff.h>
#include <linux/delay.h>

#include "smc9194.h"

#ifdef CONFIG_M68EZ328
#include <asm/MC68EZ328.h>
#include <asm/irq.h>
#include <asm/mcfsmc.h>
unsigned char	smc_defethaddr[] = { 0x00, 0x10, 0x8b, 0xf1, 0xda, 0x01 };
#define NO_AUTOPROBE
#endif

#ifdef CONFIG_COLDFIRE
#include <asm/coldfire.h>
#include <asm/mcfsim.h>
#include <asm/mcfsmc.h>

unsigned char	smc_defethaddr[] = { 0x00, 0xd0, 0xcf, 0x00, 0x00, 0x01 };

#define NO_AUTOPROBE
#endif

#ifdef CONFIG_SH_KEYWEST
#include <asm/keywest.h>
#define NO_AUTOPROBE
#define PHY_SETUP
#endif

#ifdef CONFIG_LEDMAN
#include <linux/ledman.h>
#endif

#if defined(CONFIG_CPU_H8300H) || defined(CONFIG_CPU_H8S)
#include <asm/h8300_smsc.h>
#define NO_AUTOPROBE
#endif

/*------------------------------------------------------------------------
 .
 . Configuration options, for the experienced user to change.
 .
 -------------------------------------------------------------------------*/

/*
 . Do you want to use 32 bit xfers?  This should work on all chips, as
 . the chipset is designed to accommodate them.
*/
#if (defined(__sh__) && !defined(CONFIG_SH_KEYWEST)) || \
    defined(__H8300H__) || defined(__H8300S__)
#undef USE_32_BIT
#else
#define USE_32_BIT 1
#endif

/*
 .A typedef so we can change what IO looks like easily
*/
typedef unsigned int smcio_t;

/*
 .the SMC9194 can be at any of the following port addresses.  To change,
 .for a slightly different card, you can add it to the array.  Keep in
 .mind that the array must end in zero.
*/

#if defined(CONFIG_COLDFIRE) || defined(CONFIG_M68EZ328) || \
	defined(CONFIG_SH_KEYWEST)

#ifdef CONFIG_NETtel
static smcio_t smc_portlist[]      = { 0x30600300, 0x30600000, 0 };
static unsigned int smc_irqlist[]  = {         29,         27, 0 };
#elif defined(CONFIG_SH_KEYWEST)
static smcio_t smc_portlist[]      = { KEYWEST_ETHR, 0 };
static unsigned int smc_irqlist[]  = { IRQ4_IRQ,     0 };
#elif defined(CONFIG_M68EZ328)
/* make sure that you program Port D selects to allow the interrupts! */
static smcio_t smc_portlist[]      = { 0x2000300,    0x2000320,    0 };
static unsigned int smc_irqlist[]  = { IRQ1_IRQ_NUM, IRQ2_IRQ_NUM, 0 };
#elif defined(CONFIG_CLEOPATRA)
static unsigned int smc_portlist[] = { 0x30600300, 0 };
static unsigned int smc_irqlist[]  = {         29, 0 };
#else
static smcio_t smc_portlist[]      = { 0x30600300, 0 };
static unsigned int smc_irqlist[]  = {         27, 0 };
#endif

#elif defined(CONFIG_BOARD_EDOSK2674)
static smcio_t smc_portlist[]      = { SMSC_BASE, 0 };
static unsigned int smc_irqlist[]  = { SMSC_IRQ, 0 };
#else

static unsigned int smc_portlist[] __initdata = { 
	0x200, 0x220, 0x240, 0x260, 0x280, 0x2A0, 0x2C0, 0x2E0,
	0x300, 0x320, 0x340, 0x360, 0x380, 0x3A0, 0x3C0, 0x3E0, 0
};

#endif

static unsigned int smc_found[]    = {          0,          0, 0 };

/*
 . Wait time for memory to be free.  This probably shouldn't be
 . tuned that much, as waiting for this means nothing else happens
 . in the system
*/
#define MEMORY_WAIT_TIME 16

/*
 . DEBUGGING LEVELS
 .
 . 0 for normal operation
 . 1 for slightly more details
 . >2 for various levels of increasingly useless information
 .    2 for interrupt tracking, status flags
 .    3 for packet dumps, etc.
*/
#define SMC_DEBUG 0

#if (SMC_DEBUG > 2 )
#define PRINTK3(x) printk x
#else
#define PRINTK3(x)
#endif

#if SMC_DEBUG > 1
#define PRINTK2(x) printk x
#else
#define PRINTK2(x)
#endif

#ifdef SMC_DEBUG
#define PRINTK(x) printk x
#else
#define PRINTK(x)
#endif


/*------------------------------------------------------------------------
 .
 . The internal workings of the driver.  If you are changing anything
 . here with the SMC stuff, you should have the datasheet and known
 . what you are doing.
 .
 -------------------------------------------------------------------------*/
#define CARDNAME "SMC9194"


/* store this information for the driver.. */
struct smc_local {
	/*
 	   these are things that the kernel wants me to keep, so users
	   can find out semi-useless statistics of how well the card is
	   performing
 	*/
	struct net_device_stats stats;

	/*
	   If I have to wait until memory is available to send
	   a packet, I will store the skbuff here, until I get the
	   desired memory.  Then, I'll send it out and free it.
	*/
	struct sk_buff * saved_skb;

	/*
 	 . This keeps track of how many packets that I have
 	 . sent out.  When an TX_EMPTY interrupt comes, I know
	 . that all of these have been sent.
	*/
	int	packets_waiting;
};


/*-----------------------------------------------------------------
 .
 .  The driver can be entered at any of the following entry points.
 .
 .------------------------------------------------------------------  */

/*
 . This is called by  register_netdev().  It is responsible for
 . checking the portlist for the SMC9000 series chipset.  If it finds
 . one, then it will initialize the device, find the hardware information,
 . and sets up the appropriate device parameters.
 . NOTE: Interrupts are *OFF* when this procedure is called.
 .
 . NB:This shouldn't be static since it is referred to externally.
*/
int smc_init(struct net_device *dev);

/*
 . The kernel calls this function when someone wants to use the device,
 . typically 'ifconfig ethX up'.
*/
static int smc_open(struct net_device *dev);

/*
 . Our watchdog timed out. Called by the networking layer
*/
static void smc_timeout(struct net_device *dev);

/*
 . This is called by the kernel in response to 'ifconfig ethX down'.  It
 . is responsible for cleaning up everything that the open routine
 . does, and maybe putting the card into a powerdown state.
*/
static int smc_close(struct net_device *dev);

/*
 . This routine allows the proc file system to query the driver's
 . statistics.
*/
static struct net_device_stats * smc_query_statistics( struct net_device *dev);

/*
 . Finally, a call to set promiscuous mode ( for TCPDUMP and related
 . programs ) and multicast modes.
*/
static void smc_set_multicast_list(struct net_device *dev);


/*---------------------------------------------------------------
 .
 . Interrupt level calls..
 .
 ----------------------------------------------------------------*/

/*
 . Handles the actual interrupt
*/
static void smc_interrupt(int irq, void *, struct pt_regs *regs);
/*
 . This is a separate procedure to handle the receipt of a packet, to
 . leave the interrupt code looking slightly cleaner
*/
static inline void smc_rcv( struct net_device *dev );
/*
 . This handles a TX interrupt, which is only called when an error
 . relating to a packet is sent.
*/
static inline void smc_tx( struct net_device * dev );

/*
 ------------------------------------------------------------
 .
 . Internal routines
 .
 ------------------------------------------------------------
*/

/*
 . Test if a given location contains a chip, trying to cause as
 . little damage as possible if it's not a SMC chip.
*/
static int smc_probe(struct net_device *dev, smcio_t ioaddr);

/*
 . A rather simple routine to print out a packet for debugging purposes.
*/
#if SMC_DEBUG > 2
static void print_packet( byte *, int );
#endif

#define tx_done(dev) 1

/* this is called to actually send the packet to the chip */
static void smc_hardware_send_packet( struct net_device * dev );

/* Since I am not sure if I will have enough room in the chip's ram
 . to store the packet, I call this routine, which either sends it
 . now, or generates an interrupt when the card is ready for the
 . packet */
static int  smc_wait_to_send_packet( struct sk_buff * skb, struct net_device *dev );

/* this does a soft reset on the device */
static void smc_reset( smcio_t ioaddr );

/* Enable Interrupts, Receive, and Transmit */
static void smc_enable( smcio_t ioaddr );

/* this puts the device in an inactive state */
static void smc_shutdown( smcio_t ioaddr );

#ifndef NO_AUTOPROBE
/* This routine will find the IRQ of the driver if one is not
 . specified in the input to the device.  */
static int smc_findirq( smcio_t ioaddr );
#endif

#ifdef PHY_SETUP
static void clkmdio(smcio_t ioaddr, unsigned int MGMTData);
static unsigned PHYAccess(smcio_t ioaddr, unsigned char PHYAdd,
				unsigned char RegAdd, unsigned char OPCode, unsigned wData);
static unsigned char DetectPHY(smcio_t ioaddr, unsigned long *OUI,
						unsigned char *Model, unsigned char *Revision);
static int setup_phy(smcio_t ioaddr);
#endif

/*
 . Function: smc_reset( smcio_t ioaddr )
 . Purpose:
 .  	This sets the SMC91xx chip to its normal state, hopefully from whatever
 . 	mess that any other DOS driver has put it in.
 .
 . Maybe I should reset more registers to defaults in here?  SOFTRESET  should
 . do that for me.
 .
 . Method:
 .	1.  send a SOFT RESET
 .	2.  wait for it to finish
 .	3.  enable autorelease mode
 .	4.  reset the memory management unit
 .	5.  clear all interrupts
 .
*/
static void smc_reset( smcio_t ioaddr )
{
	/* This resets the registers mostly to defaults, but doesn't
	   affect EEPROM.  That seems unnecessary */
	SMC_SELECT_BANK( 0 );
	outw( RCR_SOFTRESET, ioaddr + RCR );

	/* this should pause enough for the chip to be happy */
	SMC_DELAY( );

	/* Set the transmit and receive configuration registers to
	   default values */
	outw( RCR_CLEAR, ioaddr + RCR );
	outw( TCR_CLEAR, ioaddr + TCR );

	/* set the control register to automatically
	   release successfully transmitted packets, to make the best
	   use out of our limited memory */
	SMC_SELECT_BANK( 1 );
	outw( inw( ioaddr + CONTROL ) | CTL_AUTO_RELEASE , ioaddr + CONTROL );

#if defined(CONFIG_LEDMAN) && defined(CONFIG_SNAPGEAR)
	outw( inw( ioaddr + CONTROL ) | CTL_LE_ENABLE , ioaddr + CONTROL );	
#endif

	/* Reset the MMU */
	SMC_SELECT_BANK( 2 );
	outw( MC_RESET, ioaddr + MMU_CMD );

	/* Note:  It doesn't seem that waiting for the MMU busy is needed here,
	   but this is a place where future chipsets _COULD_ break.  Be wary
 	   of issuing another MMU command right after this */

	SMC_SET_INT( 0 );
}

/*
 . Function: smc_enable
 . Purpose: let the chip talk to the outside work
 . Method:
 .	1.  Enable the transmitter
 .	2.  Enable the receiver
 .	3.  Enable interrupts
*/
static void smc_enable( smcio_t ioaddr )
{
	SMC_SELECT_BANK( 0 );
	/* see the header file for options in TCR/RCR NORMAL*/
	outw( TCR_NORMAL, ioaddr + TCR );
	outw( RCR_NORMAL, ioaddr + RCR );

	/* now, enable interrupts */
	SMC_SELECT_BANK( 2 );
	SMC_SET_INT( SMC_INTERRUPT_MASK );
}

/*
 . Function: smc_shutdown
 . Purpose:  closes down the SMC91xxx chip.
 . Method:
 .	1. zero the interrupt mask
 .	2. clear the enable receive flag
 .	3. clear the enable xmit flags
 .
 . TODO:
 .   (1) maybe utilize power down mode.
 .	Why not yet?  Because while the chip will go into power down mode,
 .	the manual says that it will wake up in response to any I/O requests
 .	in the register space.   Empirical results do not show this working.
*/
static void smc_shutdown( smcio_t ioaddr )
{
	/* no more interrupts for me */
	SMC_SELECT_BANK( 2 );
	SMC_SET_INT( 0 );

	/* and tell the card to stay away from that nasty outside world */
	SMC_SELECT_BANK( 0 );
#if defined(CONFIG_COLDFIRE) || defined(CONFIG_M68EZ328)
	outw( RCR_CLEAR, ioaddr + RCR );
	outw( TCR_CLEAR, ioaddr + TCR );
#else
	outb( RCR_CLEAR, ioaddr + RCR );
	outb( TCR_CLEAR, ioaddr + TCR );
#endif /* CONFIG_COLDFIRE */
#if 0
	/* finally, shut the chip down */
	SMC_SELECT_BANK( 1 );
	outw( inw( ioaddr + CONTROL ), CTL_POWERDOWN, ioaddr + CONTROL  );
#endif
}


/*
 . Function: smc_setmulticast( smcio_t ioaddr, int count, dev_mc_list * adds )
 . Purpose:
 .    This sets the internal hardware table to filter out unwanted multicast
 .    packets before they take up memory.
 .
 .    The SMC chip uses a hash table where the high 6 bits of the CRC of
 .    address are the offset into the table.  If that bit is 1, then the
 .    multicast packet is accepted.  Otherwise, it's dropped silently.
 .
 .    To use the 6 bits as an offset into the table, the high 3 bits are the
 .    number of the 8 bit register, while the low 3 bits are the bit within
 .    that register.
 .
 . This routine is based very heavily on the one provided by Peter Cammaert.
*/


static void smc_setmulticast( smcio_t ioaddr, int count, struct dev_mc_list * addrs ) {
	int			i;
	unsigned char		multicast_table[ 8 ];
	struct dev_mc_list	* cur_addr;
	/* table for flipping the order of 3 bits */
	unsigned char invert3[] = { 0, 4, 2, 6, 1, 5, 3, 7 };

	/* start with a table of all zeros: reject all */
	memset( multicast_table, 0, sizeof( multicast_table ) );

	cur_addr = addrs;
	for ( i = 0; i < count ; i ++, cur_addr = cur_addr->next  ) {
		int position;

		/* do we have a pointer here? */
		if ( !cur_addr )
			break;
		/* make sure this is a multicast address - shouldn't this
		   be a given if we have it here ? */
		if ( !( *cur_addr->dmi_addr & 1 ) )
			continue;

		/* only use the low order bits */
		position = ether_crc_le(6, cur_addr->dmi_addr) & 0x3f;

		/* do some messy swapping to put the bit in the right spot */
		multicast_table[invert3[position&7]] |=
					(1<<invert3[(position>>3)&7]);

	}
	/* now, the table can be loaded into the chipset */
	SMC_SELECT_BANK( 3 );

#if defined(CONFIG_COLDFIRE) || defined(CONFIG_M68EZ328)
	for ( i = 0; i < 8 ; i += 2 ) {
		outw(((multicast_table[i+1]<<8)+(multicast_table[i])), ioaddr+MULTICAST1+i );
	}
#else
	for ( i = 0; i < 8 ; i++ ) {
		outb( multicast_table[i], ioaddr + MULTICAST1 + i );
	}
#endif
}

/*
  Finds the CRC32 of a set of bytes.
  Again, from Peter Cammaert's code.
*/
static int crc32( char * s, int length ) {
	/* indices */
	int perByte;
	int perBit;
	/* crc polynomial for Ethernet */
	const unsigned long poly = 0xedb88320;
	/* crc value - preinitialized to all 1's */
	unsigned long crc_value = 0xffffffff;

	for ( perByte = 0; perByte < length; perByte ++ ) {
		unsigned char	c;

		c = *(s++);
		for ( perBit = 0; perBit < 8; perBit++ ) {
			crc_value = (crc_value>>1)^
				(((crc_value^c)&0x01)?poly:0);
			c >>= 1;
		}
	}
	return	crc_value;
}


/*
 . Function: smc_wait_to_send_packet( struct sk_buff * skb, struct net_device * )
 . Purpose:
 .    Attempt to allocate memory for a packet, if chip-memory is not
 .    available, then tell the card to generate an interrupt when it
 .    is available.
 .
 . Algorithm:
 .
 . o	if the saved_skb is not currently null, then drop this packet
 .	on the floor.  This should never happen, because of TBUSY.
 . o	if the saved_skb is null, then replace it with the current packet,
 . o	See if I can sending it now.
 . o 	(NO): Enable interrupts and let the interrupt handler deal with it.
 . o	(YES):Send it now.
*/
static int smc_wait_to_send_packet( struct sk_buff * skb, struct net_device * dev )
{
	struct smc_local *lp 	= (struct smc_local *)dev->priv;
	smcio_t ioaddr		= dev->base_addr;
	word 			length;
	unsigned short 		numPages;
	word			time_out;

	netif_stop_queue(dev);
	/* Well, I want to send the packet.. but I don't know
	   if I can send it right now...  */

	if ( lp->saved_skb) {
		/* THIS SHOULD NEVER HAPPEN. */
		lp->stats.tx_aborted_errors++;
		printk(CARDNAME": Bad Craziness - sent packet while busy.\n" );
		return 1;
	}

	length = skb->len;

	if(length < ETH_ZLEN)
	{
		skb = skb_padto(skb, ETH_ZLEN);
		if(skb == NULL)
		{
			netif_wake_queue(dev);
			return 0;
		}
		length = ETH_ZLEN;
	}
	lp->saved_skb = skb;
		
	/*
	** The MMU wants the number of pages to be the number of 256 bytes
	** 'pages', minus 1 ( since a packet can't ever have 0 pages :) )
	**
	** Pkt size for allocating is data length +6 (for additional status words,
	** length and ctl!) If odd size last byte is included in this header.
	*/
	numPages =  ((length & 0xfffe) + 6) / 256;

	if (numPages > 7 ) {
		printk(CARDNAME": Far too big packet error. \n");
		/* freeing the packet is a good thing here... but should
		 . any packets of this size get down here?   */
		dev_kfree_skb (skb);
		lp->saved_skb = NULL;
		/* this IS an error, but, i don't want the skb saved */
		netif_wake_queue(dev);
		return 0;
	}
	/* either way, a packet is waiting now */
	lp->packets_waiting++;

	/* now, try to allocate the memory */
	SMC_SELECT_BANK( 2 );
	outw( MC_ALLOC | numPages, ioaddr + MMU_CMD );
	/*
 	. Performance Hack
	.
 	. wait a short amount of time.. if I can send a packet now, I send
	. it now.  Otherwise, I enable an interrupt and wait for one to be
	. available.
	.
	. I could have handled this a slightly different way, by checking to
	. see if any memory was available in the FREE MEMORY register.  However,
	. either way, I need to generate an allocation, and the allocation works
	. no matter what, so I saw no point in checking free memory.
	*/
	time_out = MEMORY_WAIT_TIME;
	do {
		word	status;

		status = inb( ioaddr + INTERRUPT );
		if ( status & IM_ALLOC_INT ) {
			/* acknowledge the interrupt */
			SMC_ACK_INT( IM_ALLOC_INT );
  			break;
		}
   	} while ( -- time_out );

   	if ( !time_out ) {
		/* oh well, wait until the chip finds memory later */
		SMC_ENABLE_INT( IM_ALLOC_INT );
      		PRINTK2((CARDNAME": memory allocation deferred. \n"));
		/* it's deferred, but I'll handle it later */
      		return 0;
   	}
	/* or YES! I can send the packet now.. */
	smc_hardware_send_packet(dev);
	netif_wake_queue(dev);
	return 0;
}

/*
 . Function:  smc_hardware_send_packet(struct net_device * )
 . Purpose:
 .	This sends the actual packet to the SMC9xxx chip.
 .
 . Algorithm:
 . 	First, see if a saved_skb is available.
 .		( this should NOT be called if there is no 'saved_skb'
 .	Now, find the packet number that the chip allocated
 .	Point the data pointers at it in memory
 .	Set the length word in the chip's memory
 .	Dump the packet to chip memory
 .	Check if a last byte is needed ( odd length packet )
 .		if so, set the control flag right
 . 	Tell the card to send it
 .	Enable the transmit interrupt, so I know if it failed
 . 	Free the kernel data if I actually sent it.
*/
static void smc_hardware_send_packet( struct net_device * dev )
{
	struct smc_local *lp = (struct smc_local *)dev->priv;
	byte	 		packet_no;
	struct sk_buff * 	skb = lp->saved_skb;
	word			length;
	smcio_t			ioaddr;
	byte			* buf;

	ioaddr = dev->base_addr;

	if ( !skb ) {
		PRINTK((CARDNAME": In XMIT with no packet to send \n"));
		return;
	}
	length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
	buf = skb->data;

	/* If I get here, I _know_ there is a packet slot waiting for me */
	packet_no = inb( ioaddr + PNR_ARR + 1 );
	if ( packet_no & 0x80 ) {
		/* or isn't there?  BAD CHIP! */
		printk(KERN_DEBUG CARDNAME": Memory allocation failed. \n");
		dev_kfree_skb_any(skb);
		lp->saved_skb = NULL;
		netif_wake_queue(dev);
		return;
	}

	/* we have a packet address, so tell the card to use it */
#if defined(CONFIG_COLDFIRE) || defined(CONFIG_M68EZ328)
	outw( packet_no, ioaddr + PNR_ARR );
#else
	outb( packet_no, ioaddr + PNR_ARR );
#endif

	/* point to the beginning of the packet */
	outw( PTR_AUTOINC , ioaddr + POINTER );

   	PRINTK3((CARDNAME": Trying to xmit packet of length %x\n", length ));
#if SMC_DEBUG > 2
	print_packet( buf, length );
#endif

	/* send the packet length ( +6 for status, length and ctl byte )
 	   and the status word ( set to zeros ) */

#ifdef USE_32_BIT
#if defined(CONFIG_COLDFIRE) || defined(CONFIG_M68EZ328)
	outl(  (length +6 ) , ioaddr + DATA_1 );
#else
	outl(  (length +6 ) << 16 , ioaddr + DATA_1 );
#endif
#else
	outw( 0, ioaddr + DATA_1 );
	/* send the packet length ( +6 for status words, length, and ctl*/
#if defined(CONFIG_COLDFIRE) || defined(CONFIG_M68EZ328) || defined(CONFIG_CPU_H8S)
	outw( (length+6) & 0xFFFF, ioaddr + DATA_1 );
#else
	outb( (length+6) & 0xFF,ioaddr + DATA_1 );
	outb( (length+6) >> 8 , ioaddr + DATA_1 );
#endif
#endif

	/* send the actual data
	 . I _think_ it's faster to send the longs first, and then
	 . mop up by sending the last word.  It depends heavily
 	 . on alignment, at least on the 486.  Maybe it would be
 	 . a good idea to check which is optimal?  But that could take
	 . almost as much time as is saved?
	*/
#ifdef USE_32_BIT
	if ( length & 0x2  ) {
		outsl(ioaddr + DATA_1, buf,  length >> 2 );
#if defined(CONFIG_COLDFIRE) || defined(CONFIG_M68EZ328)
		outwd( *((word *)(buf + (length & 0xFFFFFFFC))),ioaddr +DATA_1);
#else
		outw( *((word *)(buf + (length & 0xFFFFFFFC))),ioaddr +DATA_1);
#endif
	}
	else
		outsl(ioaddr + DATA_1, buf,  length >> 2 );
#else
	outsw(ioaddr + DATA_1 , buf, (length ) >> 1);
#endif
	/* Send the last byte, if there is one.   */

	if ( (length & 1) == 0 ) {
		outw( 0, ioaddr + DATA_1 );
	} else {
#if defined(CONFIG_COLDFIRE) || defined(CONFIG_M68EZ328)
		outw( buf[length -1 ] | (0x20 << 8), ioaddr + DATA_1);
#else
		outb( buf[length -1 ], ioaddr + DATA_1 );
		outb( 0x20, ioaddr + DATA_1);
#endif
	}

	/* enable the interrupts */
	SMC_ENABLE_INT( (IM_TX_INT | IM_TX_EMPTY_INT) );

	/* and let the chipset deal with it */
	outw( MC_ENQUEUE , ioaddr + MMU_CMD );

	PRINTK2((CARDNAME": Sent packet of length %d \n",length));

	lp->saved_skb = NULL;
	dev_kfree_skb_any (skb);

	dev->trans_start = jiffies;

	/* we can send another packet */
	netif_wake_queue(dev);

	return;
}

/*-------------------------------------------------------------------------
 |
 | smc_init( struct net_device * dev )
 |   Input parameters:
 |	dev->base_addr == 0, try to find all possible locations
 |	dev->base_addr == 1, return failure code
 |	dev->base_addr == 2, always allocate space,  and return success
 |	dev->base_addr == <anything else>   this is the address to check
 |
 |   Output:
 |	0 --> there is a device
 |	anything else, error
 |
 ---------------------------------------------------------------------------
*/
int __init smc_init(struct net_device *dev)
{
	int i;
	/*  check for special auto-probe address */
	int base_addr = (dev && dev->base_addr != 0xffe0) ? dev->base_addr : 0;

	SET_MODULE_OWNER(dev);

	/*  try a specific location */
	if (base_addr > 0x1ff)
		return smc_probe(dev, base_addr);
	else if (base_addr != 0)
		return -ENXIO;

	/* check every ethernet address */
	for (i = 0; smc_portlist[i]; i++) {
#ifdef CONFIG_NETtel
		smc_remap(smc_portlist[i]);
#endif
#if defined(NO_AUTOPROBE)
		dev->irq = smc_irqlist[i];
		if (smc_found[i])
			continue;
#endif
		if (smc_probe(dev, smc_portlist[i]) == 0) {
#if defined(NO_AUTOPROBE)
			smc_found[i] = 1;
#endif
			return 0;
		}
	}

	/* couldn't find anything */
	return -ENODEV;
}

/*----------------------------------------------------------------------
 . smc_findirq
 .
 . This routine has a simple purpose -- make the SMC chip generate an
 . interrupt, so an auto-detect routine can detect it, and find the IRQ,
 ------------------------------------------------------------------------
*/
#ifndef NO_AUTOPROBE
int __init smc_findirq( smcio_t ioaddr )
{
	int	timeout = 20;
	unsigned long cookie;


	/* I have to do a STI() here, because this is called from
	   a routine that does an CLI during this process, making it
	   rather difficult to get interrupts for auto detection */
	sti();

	cookie = probe_irq_on();

	/*
	 * What I try to do here is trigger an ALLOC_INT. This is done
	 * by allocating a small chunk of memory, which will give an interrupt
	 * when done.
	 */


	SMC_SELECT_BANK(2);
	/* enable ALLOCation interrupts ONLY */
	SMC_SET_INT( IM_ALLOC_INT );

	/*
 	 . Allocate 512 bytes of memory.  Note that the chip was just
	 . reset so all the memory is available
	*/
	outw( MC_ALLOC | 1, ioaddr + MMU_CMD );

	/*
	 . Wait until positive that the interrupt has been generated
	*/
	while ( timeout ) {
		byte	int_status;

		int_status = inb( ioaddr + INTERRUPT );

		if ( int_status & IM_ALLOC_INT )
			break;		/* got the interrupt */
		timeout--;
	}
	/* there is really nothing that I can do here if timeout fails,
	   as autoirq_report will return a 0 anyway, which is what I
	   want in this case.   Plus, the clean up is needed in both
	   cases.  */

	/* DELAY HERE!
	   On a fast machine, the status might change before the interrupt
	   is given to the processor.  This means that the interrupt was
	   never detected, and autoirq_report fails to report anything.
	   This should fix autoirq_* problems.
	*/
	SMC_DELAY();
	SMC_DELAY();

	/* and disable all interrupts again */
	SMC_SET_INT( 0 );

	/* clear hardware interrupts again, because that's how it
	   was when I was called... */
	cli();

	/* and return what I found */
	return probe_irq_off(cookie);
}
#endif /* NO_AUTOPROBE */

/*----------------------------------------------------------------------
 . Function: smc_probe( smcio_t ioaddr )
 .
 . Purpose:
 .	Tests to see if a given ioaddr points to an SMC9xxx chip.
 .	Returns a 0 on success
 .
 . Algorithm:
 .	(1) see if the high byte of BANK_SELECT is 0x33
 . 	(2) compare the ioaddr with the base register's address
 .	(3) see if I recognize the chip ID in the appropriate register
 .
 .---------------------------------------------------------------------
 */

/*---------------------------------------------------------------
 . Here I do typical initialization tasks.
 .
 . o  Initialize the structure if needed
 . o  print out my vanity message if not done so already
 . o  print out what type of hardware is detected
 . o  print out the ethernet address
 . o  find the IRQ
 . o  set up my private data
 . o  configure the dev structure with my subroutines
 . o  actually GRAB the irq.
 . o  GRAB the region
 .-----------------------------------------------------------------
*/
static int __init smc_probe(struct net_device *dev, smcio_t ioaddr)
{
	int i, memory, retval;
	static unsigned version_printed;
	unsigned int bank;
#if defined(CONFIG_NETtel) || defined(CONFIG_eLIA) || defined(CONFIG_DISKtel) || defined(CONFIG_CLEOPATRA)
	static int nr = 0;
#endif
#if defined(CONFIG_COLDFIRE) || defined(CONFIG_M68EZ328)
	unsigned char *ep;
#endif

	const char *version_string;
	const char *if_string;

	/* registers */
	word revision_register;
	word base_address_register;
	word configuration_register;
	word memory_info_register;
	word memory_cfg_register;

#if !defined(CONFIG_COLDFIRE) && !defined(CONFIG_M68EZ328) && \
    !defined(CONFIG_CPU_H8300H) && !defined(CONFIG_CPU_H8S)
	/* Grab the region so that no one else tries to probe our ioports. */
	if (!request_region(ioaddr, SMC_IO_EXTENT, dev->name))
		return -EBUSY;
#elif defined(CONFIG_COLDFIRE)
	/*
	 *	We need to put the SMC into 68k mode.
	 *	Do a write before anything else.
	 */
	outw(0, ioaddr + BANK_SELECT);
#endif

	/* First, see if the high byte is 0x33 */
	bank = inw( ioaddr + BANK_SELECT );
	if ( (bank & 0xFF00) != 0x3300 ) {
		retval = -ENODEV;
		goto err_out;
	}
	/* The above MIGHT indicate a device, but I need to write to further
 	 	test this.  */
	outw( 0x0, ioaddr + BANK_SELECT );
	bank = inw( ioaddr + BANK_SELECT );
	if ( (bank & 0xFF00 ) != 0x3300 ) {
		retval = -ENODEV;
		goto err_out;
	}

	/* well, we've already written once, so hopefully another time won't
 	   hurt.  This time, I need to switch the bank register to bank 1,
	   so I can access the base address register */
#if !defined(CONFIG_CPU_H8300H) && !defined(CONFIG_CPU_H8S)
	SMC_SELECT_BANK(1);
	base_address_register = inw( ioaddr + BASE );
	if ( (ioaddr & 0x3E0) != ( base_address_register >> 3 & 0x3E0 ) )  {
		printk(CARDNAME ": IOADDR %x doesn't match configuration (%x)."
			"Probably not a SMC chip\n",
			ioaddr, base_address_register >> 3 & 0x3E0 );
		/* well, the base address register didn't match.  Must not have
		   been a SMC chip after all. */
		retval = -ENODEV;
		goto err_out;
	}
#endif

	/*  check if the revision register is something that I recognize.
	    These might need to be added to later, as future revisions
	    could be added.  */
	SMC_SELECT_BANK(3);
	revision_register  = inw( ioaddr + REVISION );
	if ( !chip_ids[ ( revision_register  >> 4 ) & 0xF  ] ) {
		/* I don't recognize this chip, so... */
		printk(CARDNAME ": IO %x: Unrecognized revision register:"
			" %x, Contact author. \n", ioaddr, revision_register );

		retval = -ENODEV;
		goto err_out;
	}

	/* at this point I'll assume that the chip is an SMC9xxx.
	   It might be prudent to check a listing of MAC addresses
	   against the hardware address, or do some other tests. */
	if (version_printed++ == 0)
		printk("%s", version);

	/* fill in some of the fields */
	dev->base_addr = ioaddr;

#if defined(CONFIG_COLDFIRE) || defined(CONFIG_M68EZ328)
#if defined(CONFIG_NETtel) || defined(CONFIG_eLIA) || defined(CONFIG_DISKtel) || defined(CONFIG_CLEOPATRA)
	/*
	 . MAC address should be in FLASH, check that it is valid.
	 . If good use it, otherwise use the default.
	*/
	ep = (unsigned char *) (0xf0006000 + (nr++ * 6));
	if ((ep[0] == 0xff) && (ep[1] == 0xff) && (ep[2] == 0xff) &&
	    (ep[3] == 0xff) && (ep[4] == 0xff) && (ep[5] == 0xff))
		ep = (unsigned char *) &smc_defethaddr[0];
	else if ((ep[0] == 0) && (ep[1] == 0) && (ep[2] == 0) &&
	    (ep[3] == 0) && (ep[4] == 0) && (ep[5] == 0))
		ep = (unsigned char *) &smc_defethaddr[0];
#else
	ep = (unsigned char *) &smc_defethaddr[0];
#endif
#endif

	/*
 	 . Get the MAC address ( bank 1, regs 4 - 9 )
	*/
	SMC_SELECT_BANK( 1 );
	for ( i = 0; i < 6; i += 2 ) {
		word	address;

#if defined(CONFIG_COLDFIRE) || defined(CONFIG_M68EZ328)
		dev->dev_addr[ i ] = ep[ i ];
		dev->dev_addr[ i + 1 ] = ep[ i + 1 ];
		address = (((word) ep[ i ]) << 8) | ep[ i + 1 ];
		outw( address, ioaddr + ADDR0 + i);
#else
		address = inw( ioaddr + ADDR0 + i  );
		dev->dev_addr[ i + 1] = address >> 8;
		dev->dev_addr[ i ] = address & 0xFF;	
#endif
	}

#if defined(CONFIG_COLDFIRE) || defined(CONFIG_M68EZ328)
	/* HACK: to support 2 ethernets when using default address! */
	smc_defethaddr[5]++;
#endif

	/* get the memory information */

	SMC_SELECT_BANK( 0 );
	memory_info_register = inw( ioaddr + MIR );
	memory_cfg_register  = inw( ioaddr + MCR );
	memory = ( memory_cfg_register >> 9 )  & 0x7;  /* multiplier */
	memory *= 256 * ( memory_info_register & 0xFF );

	/*
	 Now, I want to find out more about the chip.  This is sort of
 	 redundant, but it's cleaner to have it in both, rather than having
 	 one VERY long probe procedure.
	*/
	SMC_SELECT_BANK(3);
	revision_register  = inw( ioaddr + REVISION );
	version_string = chip_ids[ ( revision_register  >> 4 ) & 0xF  ];
	if ( !version_string ) {
		/* I shouldn't get here because this call was done before.... */
		retval = -ENODEV;
		goto err_out;
	}

	/* is it using AUI or 10BaseT ? */
	if ( dev->if_port == 0 ) {
		SMC_SELECT_BANK(1);
		configuration_register = inw( ioaddr + CONFIG );
		if ( configuration_register & CFG_AUI_SELECT )
			dev->if_port = 2;
		else
			dev->if_port = 1;
	}
	if_string = interfaces[ dev->if_port - 1 ];

	/* now, reset the chip, and put it into a known state */
	smc_reset( ioaddr );

	/*
	 . If dev->irq is 0, then the device has to be banged on to see
	 . what the IRQ is.
 	 .
	 . This banging doesn't always detect the IRQ, for unknown reasons.
	 . a workaround is to reset the chip and try again.
	 .
	 . Interestingly, the DOS packet driver *SETS* the IRQ on the card to
	 . be what is requested on the command line.   I don't do that, mostly
	 . because the card that I have uses a non-standard method of accessing
	 . the IRQs, and because this _should_ work in most configurations.
	 .
	 . Specifying an IRQ is done with the assumption that the user knows
	 . what (s)he is doing.  No checking is done!!!!
 	 .
	*/
#ifndef NO_AUTOPROBE
	if ( dev->irq < 2 ) {
		int	trials;

		trials = 3;
		while ( trials-- ) {
			dev->irq = smc_findirq( ioaddr );
			if ( dev->irq )
				break;
			/* kick the card and try again */
			smc_reset( ioaddr );
		}
	}
	if (dev->irq == 0 ) {
		printk(CARDNAME": Couldn't autodetect your IRQ. Use irq=xx.\n");
		retval = -ENODEV;
		goto err_out;
	}
#else
	if (dev->irq == 0 ) {
		printk(CARDNAME
		": Autoprobing IRQs is not supported for this configuration.\n");
		return -ENODEV;
	}
#endif

	/* now, print out the card info, in a short format.. */

	printk("%s: %s(r:%d) at %#3x IRQ:%d INTF:%s MEM:%db ", dev->name,
		version_string, revision_register & 0xF, ioaddr, dev->irq,
		if_string, memory );
	/*
	 . Print the Ethernet address
	*/
	printk("ADDR: ");
	for (i = 0; i < 5; i++)
		printk("%2.2x:", dev->dev_addr[i] );
	printk("%2.2x \n", dev->dev_addr[5] );


	/* Initialize the private structure. */
	if (dev->priv == NULL) {
		dev->priv = kmalloc(sizeof(struct smc_local), GFP_KERNEL);
		if (dev->priv == NULL) {
			retval = -ENOMEM;
			goto err_out;
		}
	}
	/* set the private data to zero by default */
	memset(dev->priv, 0, sizeof(struct smc_local));

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

	/* Grab the IRQ */
#ifdef CONFIG_COLDFIRE
	mcf_autovector(dev->irq);
    retval = request_irq(dev->irq, &smc_interrupt, 0, dev->name, dev);
#elif defined(CONFIG_M68EZ328)
	retval = request_irq(IRQ_MACHSPEC | dev->irq, &smc_interrupt,
			IRQ_FLG_STD, dev->name, dev);
	if (retval) panic("Unable to attach Lan91C96 intr\n");
#else
	retval = request_irq(dev->irq, &smc_interrupt, 0, dev->name, dev);
#endif
	if (retval) {
		printk("%s: unable to get IRQ %d (irqval=%d).\n", dev->name,
			dev->irq, retval);
		kfree(dev->priv);
		dev->priv = NULL;
  	  	goto err_out;
	}

	dev->open		        = smc_open;
	dev->stop		        = smc_close;
	dev->hard_start_xmit    	= smc_wait_to_send_packet;
	dev->tx_timeout		    	= smc_timeout;
	dev->watchdog_timeo		= HZ/20;
	dev->get_stats			= smc_query_statistics;
	dev->set_multicast_list 	= smc_set_multicast_list;

#ifdef PHY_SETUP
	setup_phy( ioaddr );
#endif
	return 0;

err_out:
	release_region(ioaddr, SMC_IO_EXTENT);
	return retval;
}

#if SMC_DEBUG > 2
static void print_packet( byte * buf, int length )
{
#if 0
	int i;
	int remainder;
	int lines;

	printk("Packet of length %d \n", length );
	lines = length / 16;
	remainder = length % 16;

	for ( i = 0; i < lines ; i ++ ) {
		int cur;

		for ( cur = 0; cur < 8; cur ++ ) {
			byte a, b;

			a = *(buf ++ );
			b = *(buf ++ );
			printk("%02x%02x ", a, b );
		}
		printk("\n");
	}
	for ( i = 0; i < remainder/2 ; i++ ) {
		byte a, b;

		a = *(buf ++ );
		b = *(buf ++ );
		printk("%02x%02x ", a, b );
	}
	printk("\n");
#endif
}
#endif


/*
 * Open and Initialize the board
 *
 * Set up everything, reset the card, etc ..
 *
 */
static int smc_open(struct net_device *dev)
{
	smcio_t	ioaddr = dev->base_addr;

	int	i;	/* used to set hw ethernet address */

	/* clear out all the junk that was put here before... */
	memset(dev->priv, 0, sizeof(struct smc_local));

	/* reset the hardware */

	smc_reset( ioaddr );
	smc_enable( ioaddr );

	/* Select which interface to use */

	SMC_SELECT_BANK( 1 );
#if defined(CONFIG_DISKtel) || defined(CONFIG_SH_KEYWEST)
	/* Setup to use external PHY on smc91c110 */
	outw( inw( ioaddr + CONFIG ) | CFG_NO_WAIT | CFG_MII_SELECT,
		(ioaddr + CONFIG ));
#else
	if ( dev->if_port == 1 ) {
		outw( inw( ioaddr + CONFIG ) & ~CFG_AUI_SELECT,
			ioaddr + CONFIG );
	}
	else if ( dev->if_port == 2 ) {
		outw( inw( ioaddr + CONFIG ) | CFG_AUI_SELECT,
			ioaddr + CONFIG );
	}
#endif

	/*
  		According to Becker, I have to set the hardware address
		at this point, because the (l)user can set it with an
		ioctl.  Easily done...
	*/
	SMC_SELECT_BANK( 1 );
	for ( i = 0; i < 6; i += 2 ) {
		word	address;

		address = dev->dev_addr[ i + 1 ] << 8 ;
		address  |= dev->dev_addr[ i ];
		outw( address, ioaddr + ADDR0 + i );
	}
	
	netif_start_queue(dev);

#if defined(CONFIG_LEDMAN) && defined(CONFIG_SNAPGEAR)
	/*
	 *	fix the link status LED's
	 */
	SMC_SELECT_BANK( 0 );
	ledman_cmd((inw(ioaddr + EPH_STATUS) & ES_LINK_OK) == ES_LINK_OK ?
			LEDMAN_CMD_ON : LEDMAN_CMD_OFF,
			strcmp(dev->name, "eth0") ?
					LEDMAN_LAN2_LINK : LEDMAN_LAN1_LINK);
#endif

	return 0;
}

/*--------------------------------------------------------
 . Called by the kernel to send a packet out into the void
 . of the net.  This routine is largely based on
 . skeleton.c, from Becker.
 .--------------------------------------------------------
*/

static void smc_timeout(struct net_device *dev)
{
	/* If we get here, some higher level has decided we are broken.
	   There should really be a "kick me" function call instead. */
	printk(KERN_WARNING CARDNAME": transmit timed out, %s?\n",
		tx_done(dev) ? "IRQ conflict" :
		"network cable problem");
	/* "kick" the adaptor */
	smc_reset( dev->base_addr );
	smc_enable( dev->base_addr );
	dev->trans_start = jiffies;
	/* clear anything saved */
	((struct smc_local *)dev->priv)->saved_skb = NULL;
	netif_wake_queue(dev);
}

/*--------------------------------------------------------------------
 .
 . This is the main routine of the driver, to handle the device when
 . it needs some attention.
 .
 . So:
 .   first, save state of the chipset
 .   branch off into routines to handle each case, and acknowledge
 .	    each to the interrupt register
 .   and finally restore state.
 .
 ---------------------------------------------------------------------*/

static void smc_interrupt(int irq, void * dev_id,  struct pt_regs * regs)
{
	struct net_device *dev 	= dev_id;
	smcio_t ioaddr 		= dev->base_addr;
	struct smc_local *lp 	= (struct smc_local *)dev->priv;

	byte	status;
	word	card_stats;
	byte	mask;
	int	timeout;
#ifdef CONFIG_LEDMAN
	int tx;
#endif
	/* state registers */
	word	saved_bank;
	word	saved_pointer;



	PRINTK3((CARDNAME": SMC interrupt started \n"));

	saved_bank = inw( ioaddr + BANK_SELECT );

	SMC_SELECT_BANK(2);
	saved_pointer = inw( ioaddr + POINTER );

	mask = inb( ioaddr + INT_MASK );
	/* clear all interrupts */
	SMC_SET_INT( 0 );


	/* set a timeout value, so I don't stay here forever */
	timeout = 4;

	PRINTK2((KERN_WARNING CARDNAME ": MASK IS %x \n", mask ));
	do {
		/* read the status flag, and mask it */
		status = inb( ioaddr + INTERRUPT ) & mask;
		if (!status )
			break;

#ifdef CONFIG_LEDMAN
		tx = 0;
#endif

		PRINTK3((KERN_WARNING CARDNAME
			": Handling interrupt status %x \n", status ));

		if (status & IM_RCV_INT) {
			/* Got a packet(s). */
			PRINTK2((KERN_WARNING CARDNAME
				": Receive Interrupt\n"));
			smc_rcv(dev);
		} else if (status & IM_TX_INT ) {
			PRINTK2((KERN_WARNING CARDNAME
				": TX ERROR handled\n"));
			smc_tx(dev);
			SMC_ACK_INT( IM_TX_INT );
#ifdef CONFIG_LEDMAN
			tx = 1;
#endif
		} else if (status & IM_TX_EMPTY_INT ) {
			/* update stats */
			SMC_SELECT_BANK( 0 );
			card_stats = inw( ioaddr + COUNTER );
			/* single collisions */
			lp->stats.collisions += card_stats & 0xF;
			card_stats >>= 4;
			/* multiple collisions */
			lp->stats.collisions += card_stats & 0xF;

			/* these are for when linux supports these statistics */

			SMC_SELECT_BANK( 2 );
			PRINTK2((KERN_WARNING CARDNAME
				": TX_BUFFER_EMPTY handled\n"));
			SMC_ACK_INT( IM_TX_EMPTY_INT );
			mask &= ~IM_TX_EMPTY_INT;
			lp->stats.tx_packets += lp->packets_waiting;
			lp->stats.tx_bytes += lp->saved_skb->len;
			lp->packets_waiting = 0;
#ifdef CONFIG_LEDMAN
			tx = 1;
#endif
		} else if (status & IM_ALLOC_INT ) {
			PRINTK2((KERN_DEBUG CARDNAME
				": Allocation interrupt \n"));
			/* clear this interrupt so it doesn't happen again */
			mask &= ~IM_ALLOC_INT;

			smc_hardware_send_packet( dev );

			/* enable xmit interrupts based on this */
			mask |= ( IM_TX_EMPTY_INT | IM_TX_INT );

			/* and let the card send more packets to me */
			netif_wake_queue(dev);
			
			PRINTK2((CARDNAME": Handoff done successfully.\n"));
#ifdef CONFIG_LEDMAN
			tx = 1;
#endif
		} else if (status & IM_RX_OVRN_INT ) {
			lp->stats.rx_errors++;
			lp->stats.rx_fifo_errors++;
			SMC_ACK_INT( IM_RX_OVRN_INT );
		} else if (status & IM_EPH_INT ) {
#if defined(CONFIG_LEDMAN) && defined(CONFIG_SNAPGEAR)
			SMC_SELECT_BANK( 0 );
			ledman_cmd((inw(ioaddr + EPH_STATUS) & ES_LINK_OK) == ES_LINK_OK ?
					LEDMAN_CMD_ON : LEDMAN_CMD_OFF,
					strcmp(dev->name, "eth0")?LEDMAN_LAN2_LINK:LEDMAN_LAN1_LINK);
			SMC_SELECT_BANK( 1 );
			/* ACK it */
			outw(inw(ioaddr + CONTROL) & ~CTL_LE_ENABLE, ioaddr + CONTROL);	
			outw(inw(ioaddr + CONTROL) | CTL_LE_ENABLE, ioaddr + CONTROL);	
			SMC_SELECT_BANK( 2 );
			SMC_ACK_INT( IM_EPH_INT );
			continue; /* do not turn the TX/RX leds on */
#else
			PRINTK((CARDNAME ": UNSUPPORTED: EPH INTERRUPT \n"));
#endif
		} else if (status & IM_ERCV_INT ) {
			PRINTK((CARDNAME ": UNSUPPORTED: ERCV INTERRUPT \n"));
			SMC_ACK_INT( IM_ERCV_INT );
		}
#ifdef CONFIG_LEDMAN
		if (tx) {
			ledman_cmd(LEDMAN_CMD_SET,
					strcmp(dev->name, "eth0") == 0 ? LEDMAN_LAN1_TX :
						LEDMAN_LAN2_TX);
		} else {
			ledman_cmd(LEDMAN_CMD_SET,
					strcmp(dev->name, "eth0") == 0 ? LEDMAN_LAN1_RX :
						LEDMAN_LAN2_RX);
		}
#endif
	} while ( timeout -- );


	/* restore state register */
	SMC_SELECT_BANK( 2 );
	SMC_SET_INT( mask );

	PRINTK3(( KERN_WARNING CARDNAME ": MASK is now %x \n", mask ));
	outw( saved_pointer, ioaddr + POINTER );

	SMC_SELECT_BANK( saved_bank );

	PRINTK3((CARDNAME ": Interrupt done\n"));
	return;
}

/*-------------------------------------------------------------
 .
 . smc_rcv -  receive a packet from the card
 .
 . There is ( at least ) a packet waiting to be read from
 . chip-memory.
 .
 . o Read the status
 . o If an error, record it
 . o otherwise, read in the packet
 --------------------------------------------------------------
*/
static void smc_rcv(struct net_device *dev)
{
	struct smc_local *lp = (struct smc_local *)dev->priv;
	smcio_t ioaddr = dev->base_addr;
	int 	packet_number;
	word	status;
	word	packet_length;

	/* assume bank 2 */

	packet_number = inw( ioaddr + FIFO_PORTS );

	if ( packet_number & FP_RXEMPTY ) {
		/* we got called , but nothing was on the FIFO */
		PRINTK((CARDNAME ": WARNING: smc_rcv with nothing on FIFO. \n"));
		/* don't need to restore anything */
		return;
	}

	/*  start reading from the start of the packet */
	outw( PTR_READ | PTR_RCV | PTR_AUTOINC, ioaddr + POINTER );

	/* First two words are status and packet_length */
#ifndef CONFIG_SH_KEYWEST
	status 		= inw( ioaddr + DATA_1 );
	packet_length 	= inw( ioaddr + DATA_1 );
#else
	{
		unsigned int l = inl( ioaddr + DATA_1 );
		status         = l & 0xffff;
		packet_length  = l >> 16;
	}
#endif

	packet_length &= 0x07ff;  /* mask off top bits */

	PRINTK2(("RCV: STATUS %4x LENGTH %4x\n", status, packet_length ));
	/*
	 . the packet length contains 3 extra words :
	 . status, length, and an extra word with an odd byte .
	*/
	packet_length -= 6;

	if ( !(status & RS_ERRORS ) ){
		/* do stuff to make a new packet */
		struct sk_buff  * skb;
		byte		* data;

		/* read one extra byte */
		if ( status & RS_ODDFRAME )
			packet_length++;

		/* set multicast stats */
		if ( status & RS_MULTICAST )
			lp->stats.multicast++;

		skb = dev_alloc_skb( packet_length + 5);

		if ( skb == NULL ) {
			printk(KERN_NOTICE CARDNAME ": Low memory, packet dropped.\n");
			lp->stats.rx_dropped++;
			goto done;
		}

		/*
		 ! This should work without alignment, but it could be
		 ! in the worse case
		*/

		skb_reserve( skb, 2 );   /* 16 bit alignment */

		skb->dev = dev;
		data = skb_put( skb, packet_length);

#ifdef USE_32_BIT
		/* QUESTION:  Like in the TX routine, do I want
		   to send the DWORDs or the bytes first, or some
		   mixture.  A mixture might improve already slow PIO
		   performance  */
		PRINTK3((" Reading %d dwords (and %d bytes) \n",
			packet_length >> 2, packet_length & 3 ));
		insl(ioaddr + DATA_1 , data, packet_length >> 2 );
		/* read the left over bytes */
#ifndef CONFIG_SH_KEYWEST
		insb( ioaddr + DATA_1, data + (packet_length & 0xFFFFFC),
			packet_length & 0x3  );
#else
		if (packet_length & 3) {
			union { unsigned int l; char data[4]; } l;
			l.l = inl(ioaddr + DATA_1);
			memcpy(data + (packet_length & ~0x3), l.data, packet_length & 0x3);
		}
#endif
#else
		PRINTK3((" Reading %d words and %d byte(s) \n",
			(packet_length >> 1 ), packet_length & 1 ));
		insw(ioaddr + DATA_1 , data, packet_length >> 1);
		if ( packet_length & 1 ) {
			data += packet_length & ~1;
			*(data++) = inb( ioaddr + DATA_1 );
		}
#endif
#if	SMC_DEBUG > 2
			print_packet( data, packet_length );
#endif

		skb->protocol = eth_type_trans(skb, dev );
		netif_rx(skb);
		dev->last_rx = jiffies;
		lp->stats.rx_packets++;
		lp->stats.rx_bytes += packet_length;
	} else {
		/* error ... */
		lp->stats.rx_errors++;

		if ( status & RS_ALGNERR )  lp->stats.rx_frame_errors++;
		if ( status & (RS_TOOSHORT | RS_TOOLONG ) )
			lp->stats.rx_length_errors++;
		if ( status & RS_BADCRC)	lp->stats.rx_crc_errors++;
	}

done:
	/*  error or good, tell the card to get rid of this packet */
	outw( MC_RELEASE, ioaddr + MMU_CMD );
}


/*************************************************************************
 . smc_tx
 .
 . Purpose:  Handle a transmit error message.   This will only be called
 .   when an error, because of the AUTO_RELEASE mode.
 .
 . Algorithm:
 .	Save pointer and packet no
 .	Get the packet no from the top of the queue
 .	check if it's valid ( if not, is this an error??? )
 .	read the status word
 .	record the error
 .	( resend?  Not really, since we don't want old packets around )
 .	Restore saved values
 ************************************************************************/
static void smc_tx( struct net_device * dev )
{
	smcio_t	ioaddr = dev->base_addr;
	struct smc_local *lp = (struct smc_local *)dev->priv;
	byte saved_packet;
	byte packet_no;
	word tx_status;


	/* assume bank 2  */

	saved_packet = inb( ioaddr + PNR_ARR );
	packet_no = inw( ioaddr + FIFO_PORTS );
	packet_no &= 0x7F;

	/* select this as the packet to read from */
#if defined(CONFIG_COLDFIRE) || defined(CONFIG_M68EZ328)
	outw( packet_no, ioaddr + PNR_ARR ); 
#else
	outb( packet_no, ioaddr + PNR_ARR ); 
#endif
	

	/* read the first word from this packet */
	outw( PTR_AUTOINC | PTR_READ, ioaddr + POINTER );

	tx_status = inw( ioaddr + DATA_1 );
	PRINTK3((CARDNAME": TX DONE STATUS: %4x \n", tx_status ));

	lp->stats.tx_errors++;
	if ( tx_status & TS_LOSTCAR ) lp->stats.tx_carrier_errors++;
	if ( tx_status & TS_LATCOL  ) {
#if 0
		printk(KERN_DEBUG CARDNAME
			": Late collision occurred on last xmit.\n");
#endif
		lp->stats.tx_window_errors++;
	}
#if 0
		if ( tx_status & TS_16COL ) { ... }
#endif

	if ( tx_status & TS_SUCCESS ) {
		printk(CARDNAME": Successful packet caused interrupt \n");
	}
	/* re-enable transmit */
	SMC_SELECT_BANK( 0 );
	outw( inw( ioaddr + TCR ) | TCR_ENABLE, ioaddr + TCR );

	/* kill the packet */
	SMC_SELECT_BANK( 2 );
	outw( MC_FREEPKT, ioaddr + MMU_CMD );

	/* one less packet waiting for me */
	lp->packets_waiting--;

#if defined(CONFIG_COLDFIRE) || defined(CONFIG_M68EZ328)
	outw( saved_packet, ioaddr + PNR_ARR );
#else
	outb( saved_packet, ioaddr + PNR_ARR );
#endif
	return;
}

/*----------------------------------------------------
 . smc_close
 .
 . this makes the board clean up everything that it can
 . and not talk to the outside world.   Caused by
 . an 'ifconfig ethX down'
 .
 -----------------------------------------------------*/
static int smc_close(struct net_device *dev)
{
	netif_stop_queue(dev);
	/* clear everything */
	smc_shutdown( dev->base_addr );

#if defined(CONFIG_LEDMAN) && defined(CONFIG_SNAPGEAR)
	ledman_cmd(LEDMAN_CMD_OFF,
		strcmp(dev->name, "eth0")?LEDMAN_LAN2_LINK : LEDMAN_LAN1_LINK);
#endif

	/* Update the statistics here. */
	return 0;
}

/*------------------------------------------------------------
 . Get the current statistics.
 . This may be called with the card open or closed.
 .-------------------------------------------------------------*/
static struct net_device_stats* smc_query_statistics(struct net_device *dev) {
	struct smc_local *lp = (struct smc_local *)dev->priv;

	return &lp->stats;
}

/*-----------------------------------------------------------
 . smc_set_multicast_list
 .
 . This routine will, depending on the values passed to it,
 . either make it accept multicast packets, go into
 . promiscuous mode ( for TCPDUMP and cousins ) or accept
 . a select set of multicast packets
*/
static void smc_set_multicast_list(struct net_device *dev)
{
	smcio_t ioaddr = dev->base_addr;

	SMC_SELECT_BANK(0);
	if ( dev->flags & IFF_PROMISC )
		outw( inw(ioaddr + RCR ) | RCR_PROMISC, ioaddr + RCR );

/* BUG?  I never disable promiscuous mode if multicasting was turned on.
   Now, I turn off promiscuous mode, but I don't do anything to multicasting
   when promiscuous mode is turned on.
*/

	/* Here, I am setting this to accept all multicast packets.
	   I don't need to zero the multicast table, because the flag is
	   checked before the table is
	*/
	else if (dev->flags & IFF_ALLMULTI)
		outw( inw(ioaddr + RCR ) | RCR_ALMUL, ioaddr + RCR );

	/* We just get all multicast packets even if we only want them
	 . from one source.  This will be changed at some future
	 . point. */
	else if (dev->mc_count )  {
		/* support hardware multicasting */

		/* be sure I get rid of flags I might have set */
		outw( inw( ioaddr + RCR ) & ~(RCR_PROMISC | RCR_ALMUL),
			ioaddr + RCR );
		/* NOTE: this has to set the bank, so make sure it is the
		   last thing called.  The bank is set to zero at the top */
		smc_setmulticast( ioaddr, dev->mc_count, dev->mc_list );
	}
	else  {
		outw( inw( ioaddr + RCR ) & ~(RCR_PROMISC | RCR_ALMUL),
			ioaddr + RCR );

		/*
		  since I'm disabling all multicast entirely, I need to
		  clear the multicast list
		*/
		SMC_SELECT_BANK( 3 );
		outw( 0, ioaddr + MULTICAST1 );
		outw( 0, ioaddr + MULTICAST2 );
		outw( 0, ioaddr + MULTICAST3 );
		outw( 0, ioaddr + MULTICAST4 );
	}
}

#ifdef PHY_SETUP
static int phy_delay1 = 4;
static int phy_delay2 = 1;
static int phy_delay3 = 100;
#endif

#ifdef MODULE

static struct net_device devSMC9194;
static int io;
static int irq;
static int ifport;
MODULE_LICENSE("GPL");

MODULE_PARM(io, "i");
MODULE_PARM(irq, "i");
MODULE_PARM(ifport, "i");
MODULE_PARM_DESC(io, "SMC 99194 I/O base address");
MODULE_PARM_DESC(irq, "SMC 99194 IRQ number");
MODULE_PARM_DESC(ifport, "SMC 99194 interface port (0-default, 1-TP, 2-AUI)");

#ifdef PHY_SETUP
MODULE_PARM(phy_delay1, "i");
MODULE_PARM(phy_delay2, "i");
MODULE_PARM(phy_delay3, "i");
MODULE_PARM_DESC(phy_delay1, "Per MII clock delay [4]");
MODULE_PARM_DESC(phy_delay2, "General delay [1]");
MODULE_PARM_DESC(phy_delay3, "pre probe delay [100]");
#endif

int init_module(void)
{
	int result;

	if (io == 0)
		printk(KERN_WARNING
		CARDNAME": You shouldn't use auto-probing with insmod!\n" );

#ifdef PHY_SETUP
	printk(CARDNAME ": phy_delays %d %d %d\n", phy_delay1, phy_delay2,
			phy_delay3);
#endif
	/* copy the parameters from insmod into the device structure */
	devSMC9194.base_addr = io;
	devSMC9194.irq       = irq;
	devSMC9194.if_port	= ifport;
	devSMC9194.init   	= smc_init;
	if ((result = register_netdev(&devSMC9194)) != 0)
		return result;

	return 0;
}

void cleanup_module(void)
{
	/* No need to check MOD_IN_USE, as sys_delete_module() checks. */
	unregister_netdev(&devSMC9194);

	free_irq(devSMC9194.irq, &devSMC9194);
	release_region(devSMC9194.base_addr, SMC_IO_EXTENT);

	if (devSMC9194.priv)
		kfree(devSMC9194.priv);
}

#endif /* MODULE */


#ifdef PHY_SETUP
/*-----------------------------------------------------------
 . PHY/MII setup routines
 .
*/

#define phy_delay(x) ({ int d; for (d = 0; d < 100; d++) udelay((x) * 10); })

/*
 *	Ports for talking to the PHY/MII
 */

#define NV_CONTROL	0x10
#define MIICTRL		0x30
#define MIIDATA		0x34
#define MIICFG		0x38

#define MIIREAD		0x0001
#define MIIWRITE	0x0002

#define	MDO			0x01		/* MII Register bits */
#define	MDI			0x02
#define	MCLK		0x04
#define	MDOE		0x08
#define	MALL 		0x0F
#define	OPWrite		0x01
#define	OPRead		0x02


#define PHY_CR			0		/* PHY Registers and bits */
#define PHY_CR_Reset	0x8000
#define PHY_CR_Speed	0x2000
#define PHY_CR_Duplex	0x0100

#define PHY_SR	1
#define PHY_ID1	2
#define PHY_ID2	3

/*
 *	PHY propietary registers
 */

#define PHY_NATIONAL_PAR			0x19
#define PHY_NATIONAL_PAR_DUPLEX		0x0080
#define PHY_NATIONAL_PAR_SPEED_10	0x0040

#define PHY_TDK_DIAG				0x12
#define PHY_TDK_DIAG_DUPLEX			0x0800
#define PHY_TDK_DIAG_RATE			0x0400

#define PHY_QSI_BASETX				0x1F
#define PHY_QSI_BASETX_OPMODE_MASK	0x001c
#define PHY_QSI_BASETX_OPMODE_10HD	(2<<0x1)
#define PHY_QSI_BASETX_OPMODE_100HD	(2<<0x2)
#define PHY_QSI_BASETX_OPMODE_10FD	(2<<0x5)
#define PHY_QSI_BASETX_OPMODE_100FD	(2<<0x6)

#define PHY_SEEQ_STATUS_OUTPUT		0x12
#define PHY_SEEQ_SPD_DET			0x80
#define PHY_SEEQ_DPLX_DET			0x40

#define PHY_OUI_QSI			0x006051
#define PHY_OUI_TDK			0x00C039
#define PHY_OUI_MITELSMSC	0x00A087
#define PHY_OUI_NATIONAL	0x080017
#define PHY_OUI_SEEQSMSC	0x0005BE

#define NWAY_TIMEOUT	10

#define MAC_IS_FEAST()	(1)
#define MAC_IS_EPIC()	(0)

static void
clkmdio(smcio_t ioaddr, unsigned int MGMTData)
{
	outw(MGMTData, ioaddr + MGMT);
	udelay(phy_delay1);
	outw(MGMTData | MCLK, ioaddr + MGMT);
	udelay(phy_delay1);
}


static unsigned
PHYAccess(
	smcio_t ioaddr,
	unsigned char PHYAdd,
	unsigned char RegAdd,
	unsigned char OPCode,
	unsigned wData)
{
	int i;
	unsigned MGMTval;

	// Filter unused bits from input variables.

	PHYAdd &= 0x1F;
	RegAdd &= 0x1F;
	OPCode &= 0x03;

	if (MAC_IS_FEAST()) {
		MGMTval = inw(ioaddr + MGMT) & (MALL ^ 0xFFFF);

		// Output Preamble (32 '1's)

		for (i = 0; i < 32; i++)
			clkmdio(ioaddr, MGMTval | MDOE | MDO);

		// Output Start of Frame ('01')

		for (i = 0; i < 2; i++)
			clkmdio(ioaddr, MGMTval | MDOE | i);

		// Output OPCode ('01' for write or '10' for Read)

		for (i = 1; i >= 0; i--)
			clkmdio(ioaddr, MGMTval | MDOE | ((OPCode>>i) & 0x01) );

		// Output PHY Address

		for (i = 4; i >= 0; i--)
			clkmdio(ioaddr, MGMTval | MDOE | ((PHYAdd>>i) & 0x01) );

		// Output Register Address

		for (i = 4; i >= 0; i--)
			clkmdio(ioaddr, MGMTval | MDOE | ((RegAdd>>i) & 0x01) );

		if (OPCode == OPRead) {
			// Read Operation

			// Implement Turnaround ('Z0')

			clkmdio(ioaddr, MGMTval);
			// clkmdio(ioaddr, MGMTval | MDOE);

			// Read Data

			wData = 0;

			for (i = 15; i >= 0; i--) {
				clkmdio(ioaddr, MGMTval);
				wData |= (((inw(ioaddr + MGMT) & MDI) >> 1) << i);
			}

			// Add Idle state

			clkmdio(ioaddr, MGMTval);

			return (wData);
		} else {
			// Write Operation

			// Implement Turnaround ('10')

			for (i = 1; i >= 0; i--)
				clkmdio(ioaddr, MGMTval | MDOE | ((2>>i) & 0x01));

			// Write Data

			for (i = 15; i >= 0; i--)
				clkmdio(ioaddr, MGMTval | MDOE | ((wData>>i) & 0x01));

			// Add Idle state

			clkmdio(ioaddr, MGMTval);

			return (1);
		}
	}

	if (MAC_IS_EPIC()) {
		if (OPCode == OPRead) {
			// Read Operation
			outw((((unsigned)PHYAdd)<<9) | (((unsigned)RegAdd)<<4) | MIIREAD,
					ioaddr + MIICTRL);
			phy_delay(phy_delay2);
			wData = inw(MIIDATA);
			return(wData);
		} else {
			// Write Operation
			outw(wData, ioaddr + MIIDATA);
			outw((((unsigned)PHYAdd)<<9) | (((unsigned)RegAdd)<<4) | MIIWRITE,
					ioaddr + MIICTRL);
			phy_delay(phy_delay2);
			return(1);
		}
	}

	return(1);

}


static unsigned char
DetectPHY(
	smcio_t ioaddr,
	unsigned long *OUI,
	unsigned char *Model,
	unsigned char *Revision)
{
    unsigned int PhyId1, PhyId2;
    unsigned char PhyAdd=0xff;
    int Count;

    for (Count=31; Count >= 0; Count--) {
		PhyId1 = PHYAccess(ioaddr, Count, PHY_ID1, OPRead, 0);
		PhyId1 = PHYAccess(ioaddr, Count, PHY_ID1, OPRead, 0);
		PhyId2 = PHYAccess(ioaddr, Count, PHY_ID2, OPRead, 0);
		PhyId2 = PHYAccess(ioaddr, Count, PHY_ID2, OPRead, 0);

		if (PhyId1 > 0x0000 && PhyId1 < 0xffff && PhyId2 > 0x0000 &&
			PhyId2 < 0xffff && PhyId1 != 0x8000 && PhyId2 != 0x8000) {
			PhyAdd = (unsigned char) Count;
			break;
		}
		phy_delay(phy_delay2);
    }

    *OUI =		(((unsigned long) PhyId1) << 6) | ((PhyId2 & 0xfc00) >> 10);
    *Model =	(unsigned char) ((PhyId2 & 0x03f0) >> 4);
    *Revision =	(unsigned char) (PhyId2 & 0x000f);

    return(PhyAdd);
}


static int
setup_phy(smcio_t ioaddr)
{
    int duplex = 0; /* 0 = Half,   !0 = Full */
    int speed = 0;  /* 0 = 10Mbps, !0 = 100Mbps */
    char *report = "";
	unsigned long OUI;
	unsigned char Model, Revision;

    unsigned int i, PHYConfig, PHYConfig2, data;
    unsigned char PHYAdd, ositech = 0;

	printk("SMCPHY: ");
#if 0
	ositech = 1;
#endif

	//Setting the AUI Select Bit for 91C110 PCMCIA Design. 11/23/99 PG
	if (ositech) {
		SMC_SELECT_BANK( 1 );
		data = inw(ioaddr + BANK_SELECT);
		outw(data | 0x0100, ioaddr);
	}

    if (MAC_IS_FEAST())
		SMC_SELECT_BANK ( 3 );

	PHYAdd = DetectPHY(ioaddr, &OUI, &Model, &Revision);

	if (PHYAdd > 31) {
	    printk("UNRECOVERABLE ERROR: PHY is not present or not supported\n");
	    return(-1);
	}

	//Setup NV_CONTROL for the cardbus card.
	if (OUI == PHY_OUI_TDK)
		outw(0x7c03, ioaddr + NV_CONTROL);

	// Save Register 0.

	if (OUI == PHY_OUI_TDK)
		PHYAccess(ioaddr, PHYAdd, PHY_CR, OPRead, 0);
	PHYConfig = PHYAccess(ioaddr, PHYAdd,PHY_CR,OPRead,0);

	if (OUI == PHY_OUI_TDK) {
		outw(0x0012, ioaddr + MIICFG);	/* Set ENABLE_694 */
		/* if using EPIC, Hardware Reset the PHY from the MAC */
		outw(inw(ioaddr + CONTROL) | 0x4000, ioaddr + CONTROL);
		phy_delay(phy_delay2);
		outw(inw(ioaddr + CONTROL) & (~0x4000), ioaddr + CONTROL);
		phy_delay(phy_delay2);
	}

	/* Reset PHY */
	PHYAccess(ioaddr, PHYAdd, PHY_CR, OPWrite, PHY_CR_Reset);
	if (OUI == PHY_OUI_TDK)
		PHYAccess(ioaddr, PHYAdd, PHY_CR, OPWrite, PHY_CR_Reset);

	for (i = 0; i < 500; i++) {
		if (OUI == PHY_OUI_TDK)
			PHYAccess(ioaddr, PHYAdd, PHY_CR, OPRead, 0);

		if (PHYAccess(ioaddr, PHYAdd, PHY_CR, OPRead, 0) & PHY_CR_Reset)
			phy_delay(phy_delay2);
		else
			break;
	}

	if (i == 500) {
		printk("UNRECOVERABLE ERROR: Could not reset PHY\n");
		return(-1);
	}

	/* Write selected configuration to the PHY and verify it by reading back */
	/* Set Advertising Register for all 10/100 and Half/Full combinations */

	if (OUI == PHY_OUI_TDK)
		PHYConfig = PHYAccess(ioaddr, PHYAdd, 4, OPRead, 0);
	PHYConfig = PHYAccess(ioaddr, PHYAdd, 4, OPRead, 0);
	PHYConfig |= 0x01e0;
	PHYAccess(ioaddr, PHYAdd, 4, OPWrite, PHYConfig);
	if (OUI == PHY_OUI_TDK)
		PHYAccess(ioaddr, PHYAdd, 4, OPWrite, PHYConfig);

	/* Start 1 */

	/* National PHY requires clear before set 1 enable. */
	PHYAccess(ioaddr, PHYAdd, 0, OPWrite, 0x0000);
	PHYAccess(ioaddr, PHYAdd, 0, OPWrite, 0x1200);
	if (OUI == PHY_OUI_TDK)
		PHYAccess(ioaddr, PHYAdd, 0, OPWrite, 0x1200);

	/* Wait for completion */
	for (i = 0; i < NWAY_TIMEOUT * 10; i++) {
		printk("%c\b", "|/-\\"[i&3]);

		phy_delay(phy_delay3);

		PHYConfig = PHYAccess(ioaddr, PHYAdd, 1, OPRead, 0);
		PHYConfig2 = PHYAccess(ioaddr, PHYAdd, 1, OPRead, 0);

		if (PHYConfig != PHYConfig2) /* Value is not stable */
			continue;
		if (PHYConfig & 0x0010) /* Remote Fault */
			continue;
		if ((PHYConfig == 0x0000) || (PHYConfig == 0xffff)) /* invalid value */
			continue;
		if (PHYConfig & 0x0020)
			break;
	}

	/* Now read the results of the NWAY. */

	if (OUI == PHY_OUI_TDK)
		PHYConfig = PHYAccess(ioaddr, PHYAdd, 5, OPRead, 0);
	PHYConfig = PHYAccess(ioaddr, PHYAdd, 5, OPRead, 0);

	if (PHYConfig != 0) {
		/* Got real NWAY information here */
		report = "ANLPA";
		speed = (PHYConfig & 0x0180);
		duplex = (PHYConfig & 0x0140);
	} else {
		/*
		 * ANLPA was 0 so NWAY did not complete or is not reported fine.
		 * Get the info from propietary regs or from the control register.
		 */
		report = "Prop."; /* Proprietary Status */

		switch (OUI) {
		case PHY_OUI_NATIONAL:
			PHYConfig = PHYAccess(ioaddr, PHYAdd, PHY_NATIONAL_PAR, OPRead, 0);
			duplex = (PHYConfig & PHY_NATIONAL_PAR_DUPLEX);
			speed = ! (PHYConfig & PHY_NATIONAL_PAR_SPEED_10);
			break;

		case PHY_OUI_TDK:
			PHYConfig = PHYAccess(ioaddr, PHYAdd, PHY_TDK_DIAG, OPRead, 0);
			PHYConfig = PHYAccess(ioaddr, PHYAdd, PHY_TDK_DIAG, OPRead, 0);
			speed = ((Revision < 7) && ((PHYConfig & 0x300) == 0x300)) ||
					((Revision >= 7) && (PHYConfig & PHY_TDK_DIAG_RATE));
			duplex = ((Revision >= 7) && (PHYConfig & PHY_TDK_DIAG_DUPLEX));
			break;

		case PHY_OUI_QSI:
			PHYConfig = PHYAccess(ioaddr, PHYAdd, PHY_QSI_BASETX, OPRead, 0);
			PHYConfig &= PHY_QSI_BASETX_OPMODE_MASK;
			duplex = (PHYConfig & PHY_QSI_BASETX_OPMODE_10FD) ||
				(PHYConfig & PHY_QSI_BASETX_OPMODE_100FD);
			speed = (PHYConfig & PHY_QSI_BASETX_OPMODE_100HD) ||
				(PHYConfig & PHY_QSI_BASETX_OPMODE_100FD);
			break;

		case PHY_OUI_SEEQSMSC:
			PHYConfig=PHYAccess(ioaddr,PHYAdd,PHY_SEEQ_STATUS_OUTPUT,OPRead,0);
			duplex = (PHYConfig & PHY_SEEQ_DPLX_DET);
			speed = (PHYConfig & PHY_SEEQ_SPD_DET);
			break;

		default:
			report = "Command";
			PHYConfig = PHYAccess(ioaddr, PHYAdd, 0, OPRead, 0);
			speed = (PHYConfig & 0x2000);
			duplex = (PHYConfig & 0x0100);
			break;
		}
	}

	/* Do we need to adjust the Carrier sense on full duplex FEAST issue ?  */

	if (duplex && MAC_IS_FEAST() && (OUI == PHY_OUI_MITELSMSC))
		PHYAccess(ioaddr, PHYAdd, 0x18, OPWrite,
				0x0020 | PHYAccess(ioaddr, PHYAdd, 0x18, OPRead, 0));

	/* Display what we learned */

    printk(" %s-duplex %d Mbps ", duplex ? "Full" : "Half", speed ? 100 : 10);

	if (MAC_IS_FEAST())
		printk("FEAST ");
	if (MAC_IS_EPIC())
		printk("EPIC ");

	switch (OUI) {
	case PHY_OUI_QSI:       printk("QSI");                break;
	case PHY_OUI_TDK:       printk("TDK");                break;
	case PHY_OUI_MITELSMSC: printk("MITEL/SMSC180");      break;
	case PHY_OUI_NATIONAL:  printk("NATIONAL");           break;
	case PHY_OUI_SEEQSMSC:  printk("SEEQ/SMSC183");       break;
	default:                printk("%06lX(UNKNOWN)",OUI); break;
	}

	printk(" Model=%02X Rev=%02X ", Model, Revision);
#if DEBUG
	printk("Addr=%02X ", PHYAdd);
	printk("Conf=%s ", report);
#endif
	if (i == NWAY_TIMEOUT)
		printk("TIMEOUT!\n");
	else
		printk("Done.\n");
	return(0);
}

/*----------------------------------------------------------- */
#endif /* PHY_SETUP */