Linux Audio

Check our new training course

Embedded Linux Audio

Check our new training course
with Creative Commons CC-BY-SA
lecture materials

Bootlin logo

Elixir Cross Referencer

Loading...
   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
/* ibmtr.c:  A shared-memory IBM Token Ring 16/4 driver for linux
 *
 *	Written 1993 by Mark Swanson and Peter De Schrijver.
 *	This software may be used and distributed according to the terms
 *	of the GNU Public License, incorporated herein by reference.
 *
 *	This device driver should work with Any IBM Token Ring Card that does
 *	not use DMA.
 *
 *	I used Donald Becker's (becker@cesdis.gsfc.nasa.gov) device driver work
 *	as a base for most of my initial work.
 *
 *	Changes by Peter De Schrijver (Peter.Deschrijver@linux.cc.kuleuven.ac.be) :
 *
 *	+ changed name to ibmtr.c in anticipation of other tr boards.
 *	+ changed reset code and adapter open code.
 *	+ added SAP open code.
 *	+ a first attempt to write interrupt, transmit and receive routines.
 *
 *	Changes by David W. Morris (dwm@shell.portal.com) :
 *	941003 dwm: - Restructure tok_probe for multiple adapters, devices.
 *	+ Add comments, misc reorg for clarity.
 *	+ Flatten interrupt handler levels.
 *
 *	Changes by Farzad Farid (farzy@zen.via.ecp.fr)
 *	and Pascal Andre (andre@chimay.via.ecp.fr) (March 9 1995) :
 *	+ multi ring support clean up.
 *	+ RFC1042 compliance enhanced.
 *
 *	Changes by Pascal Andre (andre@chimay.via.ecp.fr) (September 7 1995) :
 *	+ bug correction in tr_tx
 *	+ removed redundant information display
 *	+ some code reworking
 *
 *	Changes by Michel Lespinasse (walken@via.ecp.fr),
 *	Yann Doussot (doussot@via.ecp.fr) and Pascal Andre (andre@via.ecp.fr)
 *	(February 18, 1996) :
 *	+ modified shared memory and mmio access port the driver to
 *	  alpha platform (structure access -> readb/writeb)
 *
 *	Changes by Steve Kipisz (bungy@ibm.net or kipisz@vnet.ibm.com)
 *	(January 18 1996):
 *	+ swapped WWOR and WWCR in ibmtr.h
 *	+ moved some init code from tok_probe into trdev_init.  The
 *	  PCMCIA code can call trdev_init to complete initializing
 *	  the driver.
 *	+ added -DPCMCIA to support PCMCIA
 *	+ detecting PCMCIA Card Removal in interrupt handler.  If
 *	  ISRP is FF, then a PCMCIA card has been removed
 *
 *	Changes by Paul Norton (pnorton@cts.com) :
 *	+ restructured the READ.LOG logic to prevent the transmit SRB
 *	  from being rudely overwritten before the transmit cycle is
 *	  complete. (August 15 1996)
 *	+ completed multiple adapter support. (November 20 1996)
 *	+ implemented csum_partial_copy in tr_rx and increased receive 
 *        buffer size and count. Minor fixes. (March 15, 1997)
 *
 *	Changes by Christopher Turcksin <wabbit@rtfc.demon.co.uk>
 *	+ Now compiles ok as a module again.
 *
 *	Changes by Paul Norton (pnorton@ieee.org) :
 *      + moved the header manipulation code in tr_tx and tr_rx to
 *        net/802/tr.c. (July 12 1997)
 *      + add retry and timeout on open if cable disconnected. (May 5 1998)
 *      + lifted 2000 byte mtu limit. now depends on shared-RAM size.
 *        May 25 1998)
 *      + can't allocate 2k recv buff at 8k shared-RAM. (20 October 1998)
 *
 *      Changes by Joel Sloan (jjs@c-me.com) :
 *      + disable verbose debug messages by default - to enable verbose
 *	  debugging, edit the IBMTR_DEBUG_MESSAGES define below 
 *	
 *	Changes by Mike Phillips <phillim@amtrak.com> :
 *	+ Added extra #ifdef's to work with new PCMCIA Token Ring Code.
 *	  The PCMCIA code now just sets up the card so it can be recognized
 *        by ibmtr_probe. Also checks allocated memory vs. on-board memory
 *	  for correct figure to use.
 *
 *	Changes by Tim Hockin (thockin@isunix.it.ilstu.edu) :
 *	+ added spinlocks for SMP sanity (10 March 1999)
 */

/* change the define of IBMTR_DEBUG_MESSAGES to a nonzero value 
in the event that chatty debug messages are desired - jjs 12/30/98 */

#define IBMTR_DEBUG_MESSAGES 0

#ifdef PCMCIA
#define MODULE
#endif

#include <linux/module.h>

#ifdef PCMCIA
#undef MODULE
#endif

#define NO_AUTODETECT 1
#undef NO_AUTODETECT
#undef ENABLE_PAGING


#define FALSE 0
#define TRUE (!FALSE)

/* changes the output format of driver initialisation */
#define TR_NEWFORMAT	1
#define TR_VERBOSE	0

/* some 95 OS send many non UI frame; this allow removing the warning */
#define TR_FILTERNONUI	1

/* version and credits */
static char *version =
"ibmtr.c: v1.3.57   8/ 7/94 Peter De Schrijver and Mark Swanson\n"
"         v2.1.125 10/20/98 Paul Norton <pnorton@ieee.org>\n"
"         v2.2.0   12/30/98 Joel Sloan <jjs@c-me.com>\n";

static char pcchannelid[] = {
	0x05, 0x00, 0x04, 0x09,
	0x04, 0x03, 0x04, 0x0f,
	0x03, 0x06, 0x03, 0x01,
	0x03, 0x01, 0x03, 0x00,
	0x03, 0x09, 0x03, 0x09,
	0x03, 0x00, 0x02, 0x00
};

static char mcchannelid[] = {
	0x04, 0x0d, 0x04, 0x01,
	0x05, 0x02, 0x05, 0x03,
	0x03, 0x06, 0x03, 0x03,
	0x05, 0x08, 0x03, 0x04,
	0x03, 0x05, 0x03, 0x01,
	0x03, 0x08, 0x02, 0x00
};

#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/errno.h>
#include <linux/timer.h>
#include <linux/in.h>
#include <linux/ioport.h>
#include <linux/string.h>
#include <linux/skbuff.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/netdevice.h>
#include <linux/trdevice.h>
#include <linux/stddef.h>
#include <linux/init.h>
#include <linux/spinlock.h>
#include <net/checksum.h>

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

#include "ibmtr.h"


#define DPRINTK(format, args...) printk("%s: " format, dev->name , ## args)
#define DPRINTD(format, args...) DummyCall("%s: " format, dev->name , ## args)
#define MIN(X, Y) ((X) < (Y) ? (X) : (Y))
#define MAX(X, Y) ((X) > (Y) ? (X) : (Y))

#if TR_NEWFORMAT
/* this allows displaying full adapter information */

const char *channel_def[] __initdata = { 
	"ISA", "MCA", "ISA P&P" 
};

char __init *adapter_def(char type)
{
	switch (type) 
	{
	      case 0xF : return "PC Adapter | PC Adapter II | Adapter/A";
	      case 0xE : return "16/4 Adapter | 16/4 Adapter/A (long)";
	      case 0xD : return "16/4 Adapter/A (short) | 16/4 ISA-16 Adapter";
	      case 0xC : return "Auto 16/4 Adapter";
	      default  : return "adapter (unknown type)";
	};
};
#endif

#if !TR_NEWFORMAT
unsigned char ibmtr_debug_trace=1;  /*  Patch or otherwise alter to
                                         control tokenring tracing.  */
#else
unsigned char ibmtr_debug_trace=0;
#endif
#define TRC_INIT 0x01              /*  Trace initialization & PROBEs */
#define TRC_INITV 0x02             /*  verbose init trace points     */

int		ibmtr_probe(struct net_device *dev);
static int	ibmtr_probe1(struct net_device *dev, int ioaddr);
static unsigned char	get_sram_size(struct tok_info *adapt_info);
#ifdef PCMCIA
extern unsigned char 	pcmcia_reality_check(unsigned char gss);
#endif
static int	tok_init_card(struct net_device *dev);
void		tok_interrupt(int irq, void *dev_id, struct pt_regs *regs);
static int	trdev_init(struct net_device *dev);
static void 	initial_tok_int(struct net_device *dev);
static void 	open_sap(unsigned char type,struct net_device *dev);
void		tok_open_adapter(unsigned long dev_addr);
static		void tr_rx(struct net_device *dev);
static		void tr_tx(struct net_device *dev);
static int	tok_open(struct net_device *dev);
static int	tok_close(struct net_device *dev);
static int	tok_send_packet(struct sk_buff *skb, struct net_device *dev);
static struct net_device_stats * tok_get_stats(struct net_device *dev);
void		ibmtr_readlog(struct net_device *dev);
void		ibmtr_reset_timer(struct timer_list *tmr, struct net_device *dev);
int             ibmtr_change_mtu(struct net_device *dev, int mtu);

static unsigned int ibmtr_portlist[] __initdata = {
	0xa20, 0xa24, 0
};

static __u32 ibmtr_mem_base = 0xd0000;

static void __init PrtChanID(char *pcid, short stride) 
{
	short i, j;
	for (i=0, j=0; i<24; i++, j+=stride)
		printk("%1x", ((int) pcid[j]) & 0x0f);
	printk("\n");
}

static void __init HWPrtChanID (__u32 pcid, short stride)
{
	short i, j;
	for (i=0, j=0; i<24; i++, j+=stride)
		printk("%1x", ((int)readb(pcid + j)) & 0x0f);
	printk("\n");
}

/*
 *	ibmtr_probe():  Routine specified in the network device structure
 *	to probe for an IBM Token Ring Adapter.  Routine outline:
 *	I.    Interrogate hardware to determine if an adapter exists
 *	      and what the speeds and feeds are
 *	II.   Setup data structures to control execution based upon
 *	      adapter characteristics.
 *	III.  Initialize adapter operation
 *
 *	We expect ibmtr_probe to be called once for each device entry
 *	which references it.
 */
 
int __init ibmtr_probe(struct net_device *dev)
{
        int i;
        int base_addr = dev ? dev->base_addr : 0;

        if (base_addr > 0x1ff) 
        { 
        	/*
        	 *	Check a single specified location. 
 		 */
 		 
	        if (ibmtr_probe1(dev, base_addr)) 
	        {
#ifndef MODULE
#ifndef PCMCIA
		       tr_freedev(dev);
#endif
#endif
		       return -ENODEV;
		} else
		       return 0;
	}
        else if (base_addr != 0)   /* Don't probe at all. */
	        return -ENXIO;

	for (i = 0; ibmtr_portlist[i]; i++) 
	{
	        int ioaddr = ibmtr_portlist[i];
		if (check_region(ioaddr, IBMTR_IO_EXTENT))
		        continue;
                if (ibmtr_probe1(dev, ioaddr)) {
#ifndef MODULE
#ifndef PCMCIA
		        tr_freedev(dev);
#endif
#endif
		} else
			return 0;
        }

        return -ENODEV;
}

static int __init ibmtr_probe1(struct net_device *dev, int PIOaddr)
{
	unsigned char segment=0, intr=0, irq=0, i=0, j=0, cardpresent=NOTOK,temp=0;
	__u32 t_mmio=0;
	struct tok_info *ti=0;
	__u32 cd_chanid;
	unsigned char *tchanid, ctemp;
	unsigned long timeout;

#ifndef MODULE
#ifndef PCMCIA
	dev = init_trdev(dev,0);
#endif
#endif

	/*	Query the adapter PIO base port which will return
	 *	indication of where MMIO was placed. We also have a
	 *	coded interrupt number.
	 */

       	segment = inb(PIOaddr);

	/*
	 *	Out of range values so we'll assume non-existent IO device 
	 */
	 
	if (segment < 0x40 || segment > 0xe0)
                return -ENODEV;

	/*
	 *	Compute the linear base address of the MMIO area
	 *	as LINUX doesn't care about segments
	 */

	t_mmio=(((__u32)(segment & 0xfc) << 11) + 0x80000);
	intr = segment & 0x03;   /* low bits is coded interrupt # */
	if (ibmtr_debug_trace & TRC_INIT)
		DPRINTK("PIOaddr: %4hx seg/intr: %2x mmio base: %08X intr: %d\n",
                         PIOaddr, (int)segment, t_mmio, (int)intr);

	/*
	 *	Now we will compare expected 'channelid' strings with
	 *	what we is there to learn of ISA/MCA or not TR card
	 */
	 
	cd_chanid = (CHANNEL_ID + t_mmio);  /* for efficiency */
	tchanid=pcchannelid;
	cardpresent=TR_ISA;  /* try ISA */

	/*
	 *	Suboptimize knowing first byte different
	 */

	ctemp = readb(cd_chanid) & 0x0f;
	if (ctemp != *tchanid) { /* NOT ISA card, try MCA */
		tchanid=mcchannelid;
		cardpresent=TR_MCA;
		if (ctemp != *tchanid)  /* Neither ISA nor MCA */
			cardpresent=NOTOK;
	}

	if (cardpresent != NOTOK) 
	{
		/* 
		 *	Know presumed type, try rest of ID 
		 */
		for (i=2,j=1; i<=46; i=i+2,j++) 
		{
			if ((readb(cd_chanid+i) & 0x0f) != tchanid[j]) {
				cardpresent=NOTOK;   /* match failed, not TR card */
				break;
			}
		}
	}

	/* 
	 *	If we have an ISA board check for the ISA P&P version,
	 *	as it has different IRQ settings 
	 */

	if (cardpresent == TR_ISA && (readb(AIPFID + t_mmio)==0x0e))
	        cardpresent=TR_ISAPNP;

	if (cardpresent == NOTOK) { /* "channel_id" did not match, report */
		if (ibmtr_debug_trace & TRC_INIT) {
			DPRINTK("Channel ID string not found for PIOaddr: %4hx\n", PIOaddr);
			DPRINTK("Expected for ISA: ");  PrtChanID(pcchannelid,1);
			DPRINTK("           found: ");  HWPrtChanID(cd_chanid,2);
			DPRINTK("Expected for MCA: ");  PrtChanID(mcchannelid,1);
		}
                return -ENODEV;
	}

	/* Now, allocate some of the pl0 buffers for this driver.. */

	/* If called from PCMCIA, ti is already set up, so no need to 
	   waste the memory, just use the existing structure */

#ifndef PCMCIA
	ti = (struct tok_info *)kmalloc(sizeof(struct tok_info), GFP_KERNEL);
	if (ti == NULL) 
		return -ENOMEM;

	memset(ti, 0, sizeof(struct tok_info));
#else
	ti = dev->priv ; 
#endif
	ti->mmio= t_mmio;
	ti->readlog_pending = 0;
	init_waitqueue_head(&ti->wait_for_tok_int);
	init_waitqueue_head(&ti->wait_for_reset);

	dev->priv = ti;     /* this seems like the logical use of the
                         field ... let's try some empirical tests
                         using the token-info structure -- that
                         should fit with out future hope of multiple
                         adapter support as well /dwm   */

	/* if PCMCIA, then the card is recognized as TR_ISAPNP 
	 * and there is no need to set up the interrupt, it is already done. */
        
#ifndef PCMCIA
	switch (cardpresent) 
	{
		case TR_ISA:
			if (intr==0)
				irq=9; /* irq2 really is irq9 */
			if (intr==1)
				irq=3;
			if (intr==2)
				irq=6;
			if (intr==3)
				irq=7;
			ti->global_int_enable=GLOBAL_INT_ENABLE+((irq==9) ? 2 : irq);
			ti->adapter_int_enable=PIOaddr+ADAPTINTREL;
			ti->sram=0;
#if !TR_NEWFORMAT
			DPRINTK("ti->global_int_enable: %04X\n",ti->global_int_enable);
#endif
			break;
		case TR_MCA:
			if (intr==0)
				irq=9;
			if (intr==1)
				irq=3;
			if (intr==2)
				irq=10;
			if (intr==3)
				irq=11;
			ti->global_int_enable=0;
			ti->adapter_int_enable=0;
			ti->sram=((__u32)(inb(PIOaddr+ADAPTRESETREL) & 0xfe) << 12);
			break;
		case TR_ISAPNP:
			if (intr==0)
				irq=9;
			if (intr==1)
				irq=3;
			if (intr==2)
				irq=10;
			if (intr==3)
				irq=11;
			timeout = jiffies + TR_SPIN_INTERVAL;
			while(!readb(ti->mmio + ACA_OFFSET + ACA_RW + RRR_EVEN))
				if (time_after(jiffies, timeout)) {
					DPRINTK("Hardware timeout during initialization.\n");
					kfree_s(ti, sizeof(struct tok_info));
					return -ENODEV;
			        }

			ti->sram=((__u32)readb(ti->mmio + ACA_OFFSET + ACA_RW + RRR_EVEN)<<12);
			ti->global_int_enable=PIOaddr+ADAPTINTREL;
			ti->adapter_int_enable=PIOaddr+ADAPTINTREL;
			break;
	}
#endif

	if (ibmtr_debug_trace & TRC_INIT) { /* just report int */
		DPRINTK("irq=%d",irq);
		if (ibmtr_debug_trace & TRC_INITV) { /* full chat in verbose only */
			DPRINTK(", ti->mmio=%08X",ti->mmio);
			printk(", segment=%02X",segment);
		}
		printk(".\n");
	}

	/* Get hw address of token ring card */
#if !TR_NEWFORMAT
	DPRINTK("hw address: ");
#endif
	j=0;
	for (i=0; i<0x18; i=i+2) 
	{
		/* technical reference states to do this */
		temp = readb(ti->mmio + AIP + i) & 0x0f;
#if !TR_NEWFORMAT
		printk("%1X",ti->hw_address[j]=temp);
#else
		ti->hw_address[j]=temp;
#endif
		if(j&1)
			dev->dev_addr[(j/2)]=ti->hw_address[j]+(ti->hw_address[j-1]<<4);
		++j;
	}
#ifndef TR_NEWFORMAT
	printk("\n");
#endif

	/* get Adapter type:  'F' = Adapter/A, 'E' = 16/4 Adapter II,...*/
	ti->adapter_type = readb(ti->mmio + AIPADAPTYPE);

	/* get Data Rate:  F=4Mb, E=16Mb, D=4Mb & 16Mb ?? */
	ti->data_rate = readb(ti->mmio + AIPDATARATE);

	/* Get Early Token Release support?: F=no, E=4Mb, D=16Mb, C=4&16Mb */
	ti->token_release = readb(ti->mmio + AIPEARLYTOKEN);

	/* How much shared RAM is on adapter ? */
#ifdef PCMCIA
	ti->avail_shared_ram = pcmcia_reality_check(get_sram_size(ti));
	ibmtr_mem_base = ti->sram_base << 12 ; 
#else
	ti->avail_shared_ram = get_sram_size(ti);
#endif
	/* We need to set or do a bunch of work here based on previous results.. */
	/* Support paging?  What sizes?:  F=no, E=16k, D=32k, C=16 & 32k */
	ti->shared_ram_paging = readb(ti->mmio + AIPSHRAMPAGE);

        /* Available DHB  4Mb size:   F=2048, E=4096, D=4464 */
	switch (readb(ti->mmio + AIP4MBDHB)) {
	case 0xe : 
		ti->dhb_size4mb = 4096;
		break; 
	case 0xd : 
		ti->dhb_size4mb = 4464;
		break; 
	default  : 
		ti->dhb_size4mb = 2048;
		break; 
	}

	/* Available DHB 16Mb size:  F=2048, E=4096, D=8192, C=16384, B=17960 */
	switch (readb(ti->mmio + AIP16MBDHB)) {
	case 0xe : 
		ti->dhb_size16mb = 4096;
		break; 
	case 0xd : 
		ti->dhb_size16mb = 8192;
		break; 
	case 0xc : 
		ti->dhb_size16mb = 16384;
		break; 
	case 0xb : 
		ti->dhb_size16mb = 17960;
		break; 
	default  : 
		ti->dhb_size16mb = 2048;
		break; 
	}

#if !TR_NEWFORMAT
	DPRINTK("atype=%x, drate=%x, trel=%x, asram=%dK, srp=%x, "
		"dhb(4mb=%x, 16mb=%x)\n",ti->adapter_type,
		ti->data_rate, ti->token_release, ti->avail_shared_ram/2,
		ti->shared_ram_paging, ti->dhb_size4mb, ti->dhb_size16mb);
#endif

	/*	We must figure out how much shared memory space this adapter
	 *	will occupy so that if there are two adapters we can fit both
	 *	in.  Given a choice, we will limit this adapter to 32K.  The
	 *	maximum space will will use for two adapters is 64K so if the
	 *	adapter we are working on demands 64K (it also doesn't support
	 *	paging), then only one adapter can be supported.  
	 */

	/*
	 *	determine how much of total RAM is mapped into PC space 
	 */
	ti->mapped_ram_size=1<<((((readb(ti->mmio+ ACA_OFFSET + ACA_RW + RRR_ODD)) >>2) & 0x03) + 4);
	ti->page_mask=0;
	if (ti->shared_ram_paging == 0xf) { /* No paging in adapter */
		ti->mapped_ram_size = ti->avail_shared_ram;
	} else {
#ifdef ENABLE_PAGING
		unsigned char pg_size;
#endif

#if !TR_NEWFORMAT
		DPRINTK("shared ram page size: %dK\n",ti->mapped_ram_size/2);
#endif
#ifdef ENABLE_PAGING
	switch(ti->shared_ram_paging) 
	{
		case 0xf:
			break;
		case 0xe:
			ti->page_mask=(ti->mapped_ram_size==32) ? 0xc0 : 0;
			pg_size=32;   /* 16KB page size */
			break;
		case 0xd:
			ti->page_mask=(ti->mapped_ram_size==64) ? 0x80 : 0;
			pg_size=64;   /* 32KB page size */
			break;
		case 0xc:
			ti->page_mask=(ti->mapped_ram_size==32) ? 0xc0 : 0;
			ti->page_mask=(ti->mapped_ram_size==64) ? 0x80 : 0;
			DPRINTK("Dual size shared RAM page (code=0xC), don't support it!\n");
			/* nb/dwm: I did this because RRR (3,2) bits are documented as
			   R/O and I can't find how to select which page size
			   Also, the above conditional statement sequence is invalid
			   as page_mask will always be set by the second stmt */
			kfree_s(ti, sizeof(struct tok_info));
			return -ENODEV;
			break;
		default:
			DPRINTK("Unknown shared ram paging info %01X\n",ti->shared_ram_paging);
			kfree_s(ti, sizeof(struct tok_info));
			return -ENODEV;
			break;
	}
	if (ti->page_mask) {
		if (pg_size > ti->mapped_ram_size) {
			DPRINTK("Page size (%d) > mapped ram window (%d), can't page.\n",
				pg_size, ti->mapped_ram_size);
				ti->page_mask = 0;    /* reset paging */
		} else {
			ti->mapped_ram_size=ti->avail_shared_ram;
			DPRINTK("Shared RAM paging enabled. Page size : %uK\n",
				((ti->page_mask^ 0xff)+1)>>2);
		}
#endif
	}
	/* finish figuring the shared RAM address */
	if (cardpresent==TR_ISA) {
		static __u32 ram_bndry_mask[]={0xffffe000, 0xffffc000, 0xffff8000, 0xffff0000};
		__u32 new_base, rrr_32, chk_base, rbm;

		rrr_32 = ((readb(ti->mmio+ ACA_OFFSET + ACA_RW + RRR_ODD))>>2) & 0x00000003;
		rbm = ram_bndry_mask[rrr_32];
		new_base = (ibmtr_mem_base + (~rbm)) & rbm; /* up to boundary */
		chk_base = new_base + (ti->mapped_ram_size<<9);
		if (chk_base > (ibmtr_mem_base + IBMTR_SHARED_RAM_SIZE)) {
			DPRINTK("Shared RAM for this adapter (%05x) exceeds driver"
				" limit (%05x), adapter not started.\n",
				chk_base, ibmtr_mem_base + IBMTR_SHARED_RAM_SIZE);
			kfree_s(ti, sizeof(struct tok_info));
                        return -ENODEV;
		} else {  /* seems cool, record what we have figured out */
			ti->sram_base = new_base >> 12;
			ibmtr_mem_base = chk_base;
		}
	}

#if !TR_NEWFORMAT
	DPRINTK("Using %dK shared RAM\n",ti->mapped_ram_size/2);
#endif

	/* The PCMCIA has already got the interrupt line and the io port, 
	   so no chance of anybody else getting it - MLP */

#ifndef PCMCIA
	if (request_irq (dev->irq = irq, &tok_interrupt,0,"ibmtr", dev) != 0) {
		DPRINTK("Could not grab irq %d.  Halting Token Ring driver.\n",irq);
		kfree_s(ti, sizeof(struct tok_info));
		return -ENODEV;
	}
 
	/*?? Now, allocate some of the PIO PORTs for this driver.. */
	request_region(PIOaddr,IBMTR_IO_EXTENT,"ibmtr");  /* record PIOaddr range as busy */
#endif

#if !TR_NEWFORMAT
	DPRINTK("%s",version); /* As we have passed card identification,
                                  let the world know we're here! */
#else

	if (version) {
	        printk("%s",version);
                version = NULL;
        }
	DPRINTK("%s %s found\n",
		channel_def[cardpresent-1], adapter_def(ti->adapter_type));
	DPRINTK("using irq %d, PIOaddr %hx, %dK shared RAM.\n",
	        irq, PIOaddr, ti->mapped_ram_size/2);
	DPRINTK("Hardware address : %02X:%02X:%02X:%02X:%02X:%02X\n",
		dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
		dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
#endif
	/* Calculate the maximum DHB we can use */
	switch (ti->mapped_ram_size) {
	case  16 : /* 8KB shared RAM */
		ti->dhb_size4mb  = MIN(ti->dhb_size4mb, 2048);
		ti->rbuf_len4 = 1032;
		ti->rbuf_cnt4 = 2;
		ti->dhb_size16mb = MIN(ti->dhb_size16mb, 2048);
		ti->rbuf_len16 = 1032;
		ti->rbuf_cnt16 = 2;
		break;
	case  32 : /* 16KB shared RAM */
		ti->dhb_size4mb  = MIN(ti->dhb_size4mb, 4464);
		ti->rbuf_len4 = 520;
		ti->rbuf_cnt4 = 9;
		ti->dhb_size16mb = MIN(ti->dhb_size16mb, 4096);
		ti->rbuf_len16 = 1032; /* 1024 usable */
		ti->rbuf_cnt16 = 4;
		break;
	case  64 : /* 32KB shared RAM */
		ti->dhb_size4mb  = MIN(ti->dhb_size4mb, 4464);
		ti->rbuf_len4 = 1032;
		ti->rbuf_cnt4 = 6;
		ti->dhb_size16mb = MIN(ti->dhb_size16mb, 10240);
		ti->rbuf_len16 = 1032;
		ti->rbuf_cnt16 = 10;
		break;
	case 127 : /* 63KB shared RAM */
		ti->dhb_size4mb  = MIN(ti->dhb_size4mb, 4464);
		ti->rbuf_len4 = 1032;
		ti->rbuf_cnt4 = 6;
		ti->dhb_size16mb = MIN(ti->dhb_size16mb, 16384);
		ti->rbuf_len16 = 1032;
		ti->rbuf_cnt16 = 16;
		break;
	case 128 : /* 64KB shared RAM */
		ti->dhb_size4mb  = MIN(ti->dhb_size4mb, 4464);
		ti->rbuf_len4 = 1032;
		ti->rbuf_cnt4 = 6;
		ti->dhb_size16mb = MIN(ti->dhb_size16mb, 17960);
		ti->rbuf_len16 = 1032;
		ti->rbuf_cnt16 = 18;
		break;
	default  :
		ti->dhb_size4mb  = 2048;
		ti->rbuf_len4 = 1032;
		ti->rbuf_cnt4 = 2;
		ti->dhb_size16mb = 2048;
		ti->rbuf_len16 = 1032;
		ti->rbuf_cnt16 = 2;
		break;
	}

	ti->maxmtu16 = (ti->rbuf_len16*ti->rbuf_cnt16)-((ti->rbuf_cnt16)<<3)-TR_HLEN;
	ti->maxmtu4  = (ti->rbuf_len4*ti->rbuf_cnt4)-((ti->rbuf_cnt4)<<3)-TR_HLEN;
	DPRINTK("Maximum MTU 16Mbps: %d, 4Mbps: %d\n",
		ti->maxmtu16, ti->maxmtu4);

	dev->base_addr=PIOaddr; /* set the value for device */

	trdev_init(dev);
	tok_init_card(dev);

	return 0;  /* Return 0 to indicate we have found a Token Ring card. */
}

/* query the adapter for the size of shared RAM  */

static unsigned char __init get_sram_size(struct tok_info *adapt_info)
{

	unsigned char avail_sram_code;
	static unsigned char size_code[]={ 0,16,32,64,127,128 };
	/* Adapter gives
	   'F' -- use RRR bits 3,2
	   'E' -- 8kb   'D' -- 16kb
	   'C' -- 32kb  'A' -- 64KB
	   'B' - 64KB less 512 bytes at top
	   (WARNING ... must zero top bytes in INIT */

	avail_sram_code=0xf-readb(adapt_info->mmio + AIPAVAILSHRAM);
	if (avail_sram_code)
		return size_code[avail_sram_code];
	else  /* for code 'F', must compute size from RRR(3,2) bits */
		return 1<<((readb(adapt_info->mmio+ ACA_OFFSET + ACA_RW + RRR_ODD)>>2)+4);
}

static int __init trdev_init(struct net_device *dev)
{
	struct tok_info *ti=(struct tok_info *)dev->priv;

	ti->open_status		= CLOSED;

	dev->init		= tok_init_card;
	dev->open		= tok_open;
	dev->stop		= tok_close;
	dev->hard_start_xmit	= tok_send_packet;
	dev->get_stats 		= tok_get_stats;
	dev->set_multicast_list = NULL;
	dev->change_mtu         = ibmtr_change_mtu;

#ifndef MODULE
#ifndef PCMCIA
	tr_setup(dev);
#endif
#endif
	return 0;
}



static int tok_open(struct net_device *dev)
{
	struct tok_info *ti=(struct tok_info *)dev->priv;

	/* init the spinlock */
	ti->lock = (spinlock_t) SPIN_LOCK_UNLOCKED;

	if (ti->open_status==CLOSED) tok_init_card(dev);

	if (ti->open_status==IN_PROGRESS) sleep_on(&ti->wait_for_reset);

	if (ti->open_status==SUCCESS) {
		dev->tbusy=0;
		dev->interrupt=0;
		dev->start=1;
		/* NEED to see smem size *AND* reset high 512 bytes if needed */

		MOD_INC_USE_COUNT;

		return 0;
	} else return -EAGAIN;

}

static int tok_close(struct net_device *dev)
{

	struct tok_info *ti=(struct tok_info *) dev->priv;

	writeb(DIR_CLOSE_ADAPTER,
	       ti->srb + offsetof(struct srb_close_adapter, command));
	writeb(CMD_IN_SRB, ti->mmio + ACA_OFFSET + ACA_SET + ISRA_ODD);

	ti->open_status=CLOSED;

	sleep_on(&ti->wait_for_tok_int);

	if (readb(ti->srb + offsetof(struct srb_close_adapter, ret_code)))
		DPRINTK("close adapter failed: %02X\n",
			(int)readb(ti->srb + offsetof(struct srb_close_adapter, ret_code)));

        dev->start = 0;
#ifdef PCMCIA
	ti->sram = 0 ;
#endif
	DPRINTK("Adapter closed.\n");
	MOD_DEC_USE_COUNT;

	return 0;
}

void tok_interrupt (int irq, void *dev_id, struct pt_regs *regs)
{
	unsigned char status;
	struct tok_info *ti;
	struct net_device *dev;

	dev = dev_id;
#if TR_VERBOSE
	DPRINTK("Int from tok_driver, dev : %p\n",dev);
#endif
	ti  = (struct tok_info *) dev->priv;
	spin_lock(&(ti->lock));

      	/* Disable interrupts till processing is finished */
	dev->interrupt=1;
	writeb((~INT_ENABLE), ti->mmio + ACA_OFFSET + ACA_RESET + ISRP_EVEN);

	/* Reset interrupt for ISA boards */
        if (ti->adapter_int_enable)
                outb(0,ti->adapter_int_enable);
	else
		outb(0,ti->global_int_enable);


	switch (ti->do_tok_int) {

	      case NOT_FIRST:

		/*  Begin the regular interrupt handler HERE inline to avoid
		    the extra levels of logic and call depth for the
		    original solution.   */

		status=readb(ti->mmio + ACA_OFFSET + ACA_RW + ISRP_ODD);
#ifdef PCMCIA
      		/* Check if the PCMCIA card was pulled. */
    		if (status == 0xFF)
       		{
		          DPRINTK("PCMCIA card removed.\n");
			  spin_unlock(&(ti->lock));
        		  dev->interrupt = 0;
         		  return;
       		}

    	        /* Check ISRP EVEN too. */
      	        if ( readb (ti->mmio + ACA_OFFSET + ACA_RW + ISRP_EVEN) == 0xFF)
    	        {
         		 DPRINTK("PCMCIA card removed.\n");
			 spin_unlock(&(ti->lock));
         		 dev->interrupt = 0;
         		 return;
      		 }
#endif


		if (status & ADAP_CHK_INT) {

			int i;
			__u32 check_reason;

			check_reason=ti->mmio + ntohs(readw(ti->sram + ACA_OFFSET + ACA_RW +WWCR_EVEN));

			DPRINTK("Adapter check interrupt\n");
			DPRINTK("8 reason bytes follow: ");
			for(i=0; i<8; i++, check_reason++)
				printk("%02X ", (int)readb(check_reason));
			printk("\n");

			writeb((~ADAP_CHK_INT), ti->mmio + ACA_OFFSET + ACA_RESET + ISRP_ODD);
			writeb(INT_ENABLE, ti->mmio + ACA_OFFSET + ACA_SET  + ISRP_EVEN);
			dev->interrupt=0;

		} else if (readb(ti->mmio + ACA_OFFSET + ACA_RW + ISRP_EVEN)
				 & (TCR_INT | ERR_INT | ACCESS_INT)) {

			DPRINTK("adapter error: ISRP_EVEN : %02x\n",
				(int)readb(ti->mmio + ACA_OFFSET + ACA_RW + ISRP_EVEN));
			writeb(~(TCR_INT | ERR_INT | ACCESS_INT),
			       ti->mmio + ACA_OFFSET + ACA_RESET + ISRP_EVEN);
			writeb(INT_ENABLE, ti->mmio + ACA_OFFSET + ACA_SET  + ISRP_EVEN);
			dev->interrupt=0;

		} else if (status
			   & (SRB_RESP_INT | ASB_FREE_INT | ARB_CMD_INT | SSB_RESP_INT)) {
			/* SRB, ASB, ARB or SSB response */

			if (status & SRB_RESP_INT) { /* SRB response */

				switch(readb(ti->srb)) { /* SRB command check */

				      case XMIT_DIR_FRAME: {
					      unsigned char xmit_ret_code;

					      xmit_ret_code=readb(ti->srb + offsetof(struct srb_xmit, ret_code));
					      if (xmit_ret_code != 0xff) {
						      DPRINTK("error on xmit_dir_frame request: %02X\n",
							      xmit_ret_code);
						      if (ti->current_skb) {
							      dev_kfree_skb(ti->current_skb);
							      ti->current_skb=NULL;
						      }
						      dev->tbusy=0;
						      if (ti->readlog_pending) ibmtr_readlog(dev);
					      }
				      }
				      break;

				      case XMIT_UI_FRAME: {
					      unsigned char xmit_ret_code;

					      xmit_ret_code=readb(ti->srb + offsetof(struct srb_xmit, ret_code));
					      if (xmit_ret_code != 0xff) {
						      DPRINTK("error on xmit_ui_frame request: %02X\n",
							      xmit_ret_code);
						      if (ti->current_skb) {
							      dev_kfree_skb(ti->current_skb);
							      ti->current_skb=NULL;
						      }
						      dev->tbusy=0;
						      if (ti->readlog_pending) ibmtr_readlog(dev);
					      }
				      }
				      break;

				      case DIR_OPEN_ADAPTER: {
					      unsigned char open_ret_code;
					      __u16 open_error_code;

					      ti->srb=ti->sram+ntohs(readw(ti->init_srb +offsetof(struct srb_open_response, srb_addr)));
					      ti->ssb=ti->sram+ntohs(readw(ti->init_srb +offsetof(struct srb_open_response, ssb_addr)));
					      ti->arb=ti->sram+ntohs(readw(ti->init_srb +offsetof(struct srb_open_response, arb_addr)));
					      ti->asb=ti->sram+ntohs(readw(ti->init_srb +offsetof(struct srb_open_response, asb_addr)));
					      ti->current_skb=NULL;

					      open_ret_code = readb(ti->init_srb +offsetof(struct srb_open_response, ret_code));
					      open_error_code = ntohs(readw(ti->init_srb +offsetof(struct srb_open_response, error_code)));

					      if (open_ret_code==7) {

						      if (!ti->auto_ringspeedsave && (open_error_code==0x24)) {
							      DPRINTK("Open failed: Adapter speed must match ring "
								      "speed if Automatic Ring Speed Save is disabled.\n");
							      ti->open_status=FAILURE;
							      wake_up(&ti->wait_for_reset);
						      } else if (open_error_code==0x24)
							      DPRINTK("Retrying open to adjust to ring speed.\n");
						      else if ((open_error_code==0x2d) && ti->auto_ringspeedsave)
							      DPRINTK("No signal detected for Auto Speed Detection.\n");
						      else if (open_error_code==0x11)
						      {
							      if (ti->retry_count--) 
								      DPRINTK("Ring broken/disconnected, retrying...\n");
							      else {
								      DPRINTK("Ring broken/disconnected, open failed.\n");
								      ti->open_status = FAILURE;
							      }
						      }
						      else DPRINTK("Unrecoverable error: error code = %04x.\n",
								   open_error_code);

					      } else if (!open_ret_code) {
#if !TR_NEWFORMAT
						      DPRINTK("board opened...\n");
#else
						      DPRINTK("Adapter initialized and opened.\n");
#endif
						      writeb(~(SRB_RESP_INT),
							     ti->mmio + ACA_OFFSET + ACA_RESET + ISRP_ODD);
						      writeb(~(CMD_IN_SRB),
							     ti->mmio + ACA_OFFSET + ACA_RESET + ISRA_ODD);
						      open_sap(EXTENDED_SAP,dev);

						      /* YdW probably hates me */
						      goto skip_reset;
					      } else
						      DPRINTK("open failed: ret_code = %02X, retrying\n",
							      open_ret_code);

					      if (ti->open_status != FAILURE) {
						      ibmtr_reset_timer(&(ti->tr_timer), dev);
					      }

				      }
				      break;

				      case DIR_CLOSE_ADAPTER:
					wake_up(&ti->wait_for_tok_int);
					break;

				      case DLC_OPEN_SAP:
					if (readb(ti->srb+offsetof(struct dlc_open_sap, ret_code))) {
						DPRINTK("open_sap failed: ret_code = %02X,retrying\n",
							(int)readb(ti->srb+offsetof(struct dlc_open_sap, ret_code)));
						ibmtr_reset_timer(&(ti->tr_timer), dev);
					} else {
						ti->exsap_station_id=
							readw(ti->srb+offsetof(struct dlc_open_sap, station_id));
						ti->open_status=SUCCESS; /* TR adapter is now available */
						wake_up(&ti->wait_for_reset);
					}
					break;

				      case DIR_INTERRUPT:
				      case DIR_MOD_OPEN_PARAMS:
				      case DIR_SET_GRP_ADDR:
				      case DIR_SET_FUNC_ADDR:
				      case DLC_CLOSE_SAP:
					if (readb(ti->srb+offsetof(struct srb_interrupt, ret_code)))
						DPRINTK("error on %02X: %02X\n",
							(int)readb(ti->srb+offsetof(struct srb_interrupt, command)),
							(int)readb(ti->srb+offsetof(struct srb_interrupt, ret_code)));
					break;

				      case DIR_READ_LOG:
					if (readb(ti->srb+offsetof(struct srb_read_log, ret_code)))
						DPRINTK("error on dir_read_log: %02X\n",
							(int)readb(ti->srb+offsetof(struct srb_read_log, ret_code)));
					else
					    if (IBMTR_DEBUG_MESSAGES) {
						DPRINTK(
							"Line errors %02X, Internal errors %02X, Burst errors %02X\n"
							"A/C errors %02X, Abort delimiters %02X, Lost frames %02X\n"
							"Receive congestion count %02X, Frame copied errors %02X\n"
							"Frequency errors %02X, Token errors %02X\n",
							(int)readb(ti->srb+offsetof(struct srb_read_log,
										    line_errors)),
							(int)readb(ti->srb+offsetof(struct srb_read_log,
										    internal_errors)),
							(int)readb(ti->srb+offsetof(struct srb_read_log,
										    burst_errors)),
							(int)readb(ti->srb+offsetof(struct srb_read_log, A_C_errors)),
							(int)readb(ti->srb+offsetof(struct srb_read_log,
										    abort_delimiters)),
							(int)readb(ti->srb+offsetof(struct srb_read_log,
										    lost_frames)),
							(int)readb(ti->srb+offsetof(struct srb_read_log,
												    recv_congest_count)),
							(int)readb(ti->srb+offsetof(struct srb_read_log,
										    frame_copied_errors)),
							(int)readb(ti->srb+offsetof(struct srb_read_log,
										    frequency_errors)),
							(int)readb(ti->srb+offsetof(struct srb_read_log,
												    token_errors)));
					    }
					dev->tbusy=0;
					break;

				      default:
					DPRINTK("Unknown command %02X encountered\n",
						(int)readb(ti->srb));

				} /* SRB command check */

				writeb(~CMD_IN_SRB, ti->mmio + ACA_OFFSET + ACA_RESET + ISRA_ODD);
				writeb(~SRB_RESP_INT, ti->mmio + ACA_OFFSET + ACA_RESET + ISRP_ODD);

			  skip_reset:
			} /* SRB response */

			if (status & ASB_FREE_INT) { /* ASB response */

				switch(readb(ti->asb)) { /* ASB command check */

				      case REC_DATA:
				      case XMIT_UI_FRAME:
				      case XMIT_DIR_FRAME:
					break;

				      default:
					DPRINTK("unknown command in asb %02X\n",
						(int)readb(ti->asb));

				} /* ASB command check */

				if (readb(ti->asb+2)!=0xff) /* checks ret_code */
				    DPRINTK("ASB error %02X in cmd %02X\n",
					    (int)readb(ti->asb+2),(int)readb(ti->asb));
				writeb(~ASB_FREE_INT, ti->mmio + ACA_OFFSET + ACA_RESET + ISRP_ODD);

			} /* ASB response */

			if (status & ARB_CMD_INT) { /* ARB response */

				switch (readb(ti->arb)) { /* ARB command check */

				      case DLC_STATUS:
					DPRINTK("DLC_STATUS new status: %02X on station %02X\n",
						ntohs(readw(ti->arb + offsetof(struct arb_dlc_status, status))),
						ntohs(readw(ti->arb
									    +offsetof(struct arb_dlc_status, station_id))));
					break;

				      case REC_DATA:
					tr_rx(dev);
					break;

				      case RING_STAT_CHANGE: {
					      unsigned short ring_status;

					      ring_status=ntohs(readw(ti->arb
								      +offsetof(struct arb_ring_stat_change, ring_status)));

					      if (ring_status & (SIGNAL_LOSS | LOBE_FAULT)) {

						      DPRINTK("Signal loss/Lobe fault\n");
						      DPRINTK("We try to reopen the adapter.\n");
						      ibmtr_reset_timer(&(ti->tr_timer), dev);
					      } else if (ring_status & (HARD_ERROR | XMIT_BEACON
											| AUTO_REMOVAL | REMOVE_RECV | RING_RECOVER))
						      DPRINTK("New ring status: %02X\n", ring_status);

					      if (ring_status & LOG_OVERFLOW) {
						      if (dev->tbusy)
                                                              ti->readlog_pending = 1;
						      else
                                                              ibmtr_readlog(dev);
					      }
				      }
				      break;

				      case XMIT_DATA_REQ:
					tr_tx(dev);
					break;

				      default:
					DPRINTK("Unknown command %02X in arb\n",
						(int)readb(ti->arb));
					break;

				} /* ARB command check */

				writeb(~ARB_CMD_INT, ti->mmio + ACA_OFFSET + ACA_RESET + ISRP_ODD);
				writeb(ARB_FREE, ti->mmio + ACA_OFFSET + ACA_SET + ISRA_ODD);

			} /* ARB response */

			if (status & SSB_RESP_INT) { /* SSB response */
				unsigned char retcode;
				switch (readb(ti->ssb)) { /* SSB command check */
				      
				      case XMIT_DIR_FRAME:
				      case XMIT_UI_FRAME:
					retcode = readb(ti->ssb+2);
					if (retcode && (retcode != 0x22)) /* checks ret_code */
						DPRINTK("xmit ret_code: %02X xmit error code: %02X\n",
							(int)retcode, (int)readb(ti->ssb+6));
					else ti->tr_stats.tx_packets++;
					break;

				      case XMIT_XID_CMD:
					DPRINTK("xmit xid ret_code: %02X\n", (int)readb(ti->ssb+2));

				      default:
					DPRINTK("Unknown command %02X in ssb\n", (int)readb(ti->ssb));

				} /* SSB command check */

				writeb(~SSB_RESP_INT, ti->mmio + ACA_OFFSET + ACA_RESET + ISRP_ODD);
				writeb(SSB_FREE, ti->mmio + ACA_OFFSET + ACA_SET + ISRA_ODD);

			} /* SSB response */

		}	 /* SRB, ARB, ASB or SSB response */

		dev->interrupt=0;
		writeb(INT_ENABLE, ti->mmio + ACA_OFFSET + ACA_SET + ISRP_EVEN);
		break;

	      case FIRST_INT:
		initial_tok_int(dev);
		break;

	      default:
		DPRINTK("Unexpected interrupt from tr adapter\n");

	}
	spin_unlock(&(ti->lock));
}

static void initial_tok_int(struct net_device *dev)
{

	__u32 encoded_addr;
	__u32 hw_encoded_addr;
	struct tok_info *ti;
	ti=(struct tok_info *) dev->priv;

	ti->do_tok_int=NOT_FIRST;

#ifndef TR_NEWFORMAT
	DPRINTK("Initial tok int received\n");
#endif

	/* we assign the shared-ram address for ISA devices */
	if(!ti->sram) {
		writeb(ti->sram_base, ti->mmio + ACA_OFFSET + ACA_RW + RRR_EVEN);
		ti->sram=((__u32)ti->sram_base << 12);
	}
	ti->init_srb=ti->sram
		+ntohs((unsigned short)readw(ti->mmio+ ACA_OFFSET + WRBR_EVEN));
	SET_PAGE(ntohs((unsigned short)readw(ti->mmio+ACA_OFFSET + WRBR_EVEN)));

	dev->mem_start = ti->sram;
	dev->mem_end = ti->sram + (ti->mapped_ram_size<<9) - 1;

#if TR_VERBOSE
	{
		int i;
		DPRINTK("init_srb(%p):", ti->init_srb);
		for (i=0;i<17;i++) printk("%02X ", (int)readb(ti->init_srb+i));
		printk("\n");
	}
#endif

	hw_encoded_addr = readw(ti->init_srb
				+ offsetof(struct srb_init_response, encoded_address));

#if !TR_NEWFORMAT
	DPRINTK("srb_init_response->encoded_address: %04X\n", hw_encoded_addr);
	DPRINTK("ntohs(srb_init_response->encoded_address): %04X\n",
		ntohs(hw_encoded_addr));
#endif

	encoded_addr=(ti->sram + ntohs(hw_encoded_addr));
	ti->ring_speed = readb(ti->init_srb+offsetof(struct srb_init_response, init_status)) & 0x01 ? 16 : 4;
#if !TR_NEWFORMAT
	DPRINTK("encoded addr (%04X,%04X,%08X): ", hw_encoded_addr,
		ntohs(hw_encoded_addr), encoded_addr);
#else
	DPRINTK("Initial interrupt : %d Mbps, shared RAM base %08x.\n",
		ti->ring_speed, ti->sram);
#endif

	ti->auto_ringspeedsave=readb(ti->init_srb
				     +offsetof(struct srb_init_response, init_status_2)) & 0x4 ? TRUE : FALSE;

#if !TR_NEWFORMAT
	for(i=0;i<TR_ALEN;i++) {
		dev->dev_addr[i]=readb(encoded_addr + i);
		printk("%02X%s", dev->dev_addr[i], (i==TR_ALEN-1) ? "" : ":" );
	}
	printk("\n");
#endif

	tok_open_adapter((unsigned long)dev);
}

static int tok_init_card(struct net_device *dev)
{
	struct tok_info *ti;
	short PIOaddr;
	unsigned long i;
	PIOaddr = dev->base_addr;
	ti=(struct tok_info *) dev->priv;

	/* Special processing for first interrupt after reset */
	ti->do_tok_int=FIRST_INT;

	/* Reset adapter */
	dev->tbusy=1; /* nothing can be done before reset and open completed */

#ifdef ENABLE_PAGING
	if(ti->page_mask)
		writeb(SRPR_ENABLE_PAGING, ti->mmio + ACA_OFFSET + ACA_RW + SRPR_EVEN);
#endif

	writeb(~INT_ENABLE, ti->mmio + ACA_OFFSET + ACA_RESET + ISRP_EVEN);

#if !TR_NEWFORMAT
	DPRINTK("resetting card\n");
#endif

	outb(0, PIOaddr+ADAPTRESET);
	for (i=jiffies+TR_RESET_INTERVAL; time_before_eq(jiffies, i);); /* wait 50ms */
	outb(0,PIOaddr+ADAPTRESETREL);

#if !TR_NEWFORMAT
	DPRINTK("card reset\n");
#endif

	ti->open_status=IN_PROGRESS;
	writeb(INT_ENABLE, ti->mmio + ACA_OFFSET + ACA_SET + ISRP_EVEN);
	return 0;
}

static void open_sap(unsigned char type,struct net_device *dev)
{
	int i;
	struct tok_info *ti=(struct tok_info *) dev->priv;

	SET_PAGE(ti->srb);
	for (i=0; i<sizeof(struct dlc_open_sap); i++)
		writeb(0, ti->srb+i);

	writeb(DLC_OPEN_SAP, ti->srb + offsetof(struct dlc_open_sap, command));
	writew(htons(MAX_I_FIELD),
	       ti->srb + offsetof(struct dlc_open_sap, max_i_field));
	writeb(SAP_OPEN_IND_SAP | SAP_OPEN_PRIORITY,
	       ti->srb + offsetof(struct dlc_open_sap, sap_options));
	writeb(SAP_OPEN_STATION_CNT,
	       ti->srb + offsetof(struct dlc_open_sap, station_count));
	writeb(type, ti->srb + offsetof(struct dlc_open_sap, sap_value));

	writeb(CMD_IN_SRB, ti->mmio + ACA_OFFSET + ACA_SET + ISRA_ODD);

}

void tok_open_adapter(unsigned long dev_addr)
{

	struct net_device *dev=(struct net_device *)dev_addr;
	struct tok_info *ti;
	int i;

	ti=(struct tok_info *) dev->priv;

#if !TR_NEWFORMAT
	DPRINTK("now opening the board...\n");
#endif

	writeb(~SRB_RESP_INT, ti->mmio + ACA_OFFSET + ACA_RESET + ISRP_ODD);
	writeb(~CMD_IN_SRB, ti->mmio + ACA_OFFSET + ACA_RESET + ISRA_ODD);

	for (i=0; i<sizeof(struct dir_open_adapter); i++)
		writeb(0, ti->init_srb+i);

	writeb(DIR_OPEN_ADAPTER,
	       ti->init_srb + offsetof(struct dir_open_adapter, command));
	writew(htons(OPEN_PASS_BCON_MAC),
	       ti->init_srb + offsetof(struct dir_open_adapter, open_options));
	if (ti->ring_speed == 16) {
		writew(htons(ti->dhb_size16mb),
		       ti->init_srb + offsetof(struct dir_open_adapter, dhb_length));
		writew(htons(ti->rbuf_cnt16),
		       ti->init_srb + offsetof(struct dir_open_adapter, num_rcv_buf));
		writew(htons(ti->rbuf_len16),
		       ti->init_srb + offsetof(struct dir_open_adapter, rcv_buf_len));
	} else {
		writew(htons(ti->dhb_size4mb),
		       ti->init_srb + offsetof(struct dir_open_adapter, dhb_length));
		writew(htons(ti->rbuf_cnt4),
		       ti->init_srb + offsetof(struct dir_open_adapter, num_rcv_buf));
		writew(htons(ti->rbuf_len4),
		       ti->init_srb + offsetof(struct dir_open_adapter, rcv_buf_len));
	}
	writeb(NUM_DHB, /* always 2 */ 
	       ti->init_srb + offsetof(struct dir_open_adapter, num_dhb));
	writeb(DLC_MAX_SAP,
	       ti->init_srb + offsetof(struct dir_open_adapter, dlc_max_sap));
	writeb(DLC_MAX_STA,
	       ti->init_srb + offsetof(struct dir_open_adapter, dlc_max_sta));

	ti->srb=ti->init_srb; /* We use this one in the interrupt handler */

	writeb(INT_ENABLE, ti->mmio + ACA_OFFSET + ACA_SET + ISRP_EVEN);
	writeb(CMD_IN_SRB, ti->mmio + ACA_OFFSET + ACA_SET + ISRA_ODD);

}

static void tr_tx(struct net_device *dev)
{
	struct tok_info *ti=(struct tok_info *) dev->priv;
	struct trh_hdr *trhdr=(struct trh_hdr *)ti->current_skb->data;
	unsigned int hdr_len;
	__u32 dhb;
	unsigned char xmit_command;
	int i;
	struct trllc	*llc;

	if (readb(ti->asb + offsetof(struct asb_xmit_resp, ret_code))!=0xFF)
		DPRINTK("ASB not free !!!\n");

	/* in providing the transmit interrupts,
	   is telling us it is ready for data and
	   providing a shared memory address for us
	   to stuff with data.  Here we compute the
	   effective address where we will place data.*/
	dhb=ti->sram
		+ntohs(readw(ti->arb + offsetof(struct arb_xmit_req, dhb_address)));
	
	/* Figure out the size of the 802.5 header */
	if (!(trhdr->saddr[0] & 0x80)) /* RIF present? */
		hdr_len=sizeof(struct trh_hdr)-TR_MAXRIFLEN;
	else 
		hdr_len=((ntohs(trhdr->rcf) & TR_RCF_LEN_MASK)>>8)
			+sizeof(struct trh_hdr)-TR_MAXRIFLEN;

	llc = (struct trllc *)(ti->current_skb->data + hdr_len);

	xmit_command = readb(ti->srb + offsetof(struct srb_xmit, command));

	writeb(xmit_command, ti->asb + offsetof(struct asb_xmit_resp, command));
	writew(readb(ti->srb + offsetof(struct srb_xmit, station_id)),
	       ti->asb + offsetof(struct asb_xmit_resp, station_id));
	writeb(llc->ssap, ti->asb + offsetof(struct asb_xmit_resp, rsap_value));
	writeb(readb(ti->srb + offsetof(struct srb_xmit, cmd_corr)),
	       ti->asb + offsetof(struct asb_xmit_resp, cmd_corr));
	writeb(0, ti->asb + offsetof(struct asb_xmit_resp, ret_code));

	if ((xmit_command==XMIT_XID_CMD) || (xmit_command==XMIT_TEST_CMD)) {

		writew(htons(0x11),
		       ti->asb + offsetof(struct asb_xmit_resp, frame_length));
		writeb(0x0e, ti->asb + offsetof(struct asb_xmit_resp, hdr_length));
		writeb(AC, dhb);
		writeb(LLC_FRAME, dhb+1);

		for (i=0; i<TR_ALEN; i++) writeb((int)0x0FF, dhb+i+2);
		for (i=0; i<TR_ALEN; i++) writeb(0, dhb+i+TR_ALEN+2);

		writeb(RESP_IN_ASB, ti->mmio + ACA_OFFSET + ACA_SET + ISRA_ODD);
		return;

	}

	/*
	 *      the token ring packet is copied from sk_buff to the adapter
	 *      buffer identified in the command data received with the interrupt.
	 */
	writeb(hdr_len, ti->asb + offsetof(struct asb_xmit_resp, hdr_length));
	writew(htons(ti->current_skb->len),
	       ti->asb + offsetof(struct asb_xmit_resp, frame_length));

	memcpy_toio(dhb, ti->current_skb->data, ti->current_skb->len);

	writeb(RESP_IN_ASB, ti->mmio + ACA_OFFSET + ACA_SET + ISRA_ODD);
	ti->tr_stats.tx_bytes+=ti->current_skb->len;
	dev->tbusy=0;
	dev_kfree_skb(ti->current_skb);
	ti->current_skb=NULL;
	mark_bh(NET_BH);
	if (ti->readlog_pending) ibmtr_readlog(dev);
}

static void tr_rx(struct net_device *dev)
{
	struct tok_info *ti=(struct tok_info *) dev->priv;
	__u32 rbuffer, rbufdata;
	__u32 llc;
	unsigned char *data;
	unsigned int rbuffer_len, lan_hdr_len, hdr_len, ip_len, length;
	struct sk_buff *skb;
	unsigned int skb_size = 0;
	int	IPv4_p = 0;
	unsigned int chksum = 0;
	struct iphdr *iph;

	rbuffer=(ti->sram
		 +ntohs(readw(ti->arb + offsetof(struct arb_rec_req, rec_buf_addr))))+2;
 
	if(readb(ti->asb + offsetof(struct asb_rec, ret_code))!=0xFF)
		DPRINTK("ASB not free !!!\n");

	writeb(REC_DATA,
	       ti->asb + offsetof(struct asb_rec, command));
	writew(readw(ti->arb + offsetof(struct arb_rec_req, station_id)),
	       ti->asb + offsetof(struct asb_rec, station_id));
	writew(readw(ti->arb + offsetof(struct arb_rec_req, rec_buf_addr)),
	       ti->asb + offsetof(struct asb_rec, rec_buf_addr));

	lan_hdr_len=readb(ti->arb + offsetof(struct arb_rec_req, lan_hdr_len));
	hdr_len = lan_hdr_len + sizeof(struct trllc) + sizeof(struct iphdr);
	
	llc=(rbuffer + offsetof(struct rec_buf, data) + lan_hdr_len);

#if TR_VERBOSE
	DPRINTK("offsetof data: %02X lan_hdr_len: %02X\n",
		(unsigned int)offsetof(struct rec_buf,data), (unsigned int)lan_hdr_len);
	DPRINTK("llc: %08X rec_buf_addr: %04X ti->sram: %p\n", llc,
		ntohs(readw(ti->arb + offsetof(struct arb_rec_req, rec_buf_addr))),
		ti->sram);
	DPRINTK("dsap: %02X, ssap: %02X, llc: %02X, protid: %02X%02X%02X, "
		"ethertype: %04X\n",
		(int)readb(llc + offsetof(struct trllc, dsap)),
		(int)readb(llc + offsetof(struct trllc, ssap)),
		(int)readb(llc + offsetof(struct trllc, llc)),
		(int)readb(llc + offsetof(struct trllc, protid)),
		(int)readb(llc + offsetof(struct trllc, protid)+1),
		(int)readb(llc + offsetof(struct trllc, protid)+2),
		(int)readw(llc + offsetof(struct trllc, ethertype)));
#endif
	if (readb(llc + offsetof(struct trllc, llc))!=UI_CMD) {
		writeb(DATA_LOST, ti->asb + offsetof(struct asb_rec, ret_code));
		ti->tr_stats.rx_dropped++;
		writeb(RESP_IN_ASB, ti->mmio + ACA_OFFSET + ACA_SET + ISRA_ODD);
		return;
	}

	length = ntohs(readw(ti->arb+offsetof(struct arb_rec_req, frame_len)));
       	if ((readb(llc + offsetof(struct trllc, dsap))==EXTENDED_SAP) &&
       	    (readb(llc + offsetof(struct trllc, ssap))==EXTENDED_SAP) &&
		(length>=hdr_len)) {
       		IPv4_p = 1;
       	}

#if TR_VERBOSE
       	if (!IPv4_p){

       		__u32 trhhdr;

       		trhhdr=(rbuffer+offsetof(struct rec_buf,data));

       		DPRINTK("Probably non-IP frame received.\n");
       		DPRINTK("ssap: %02X dsap: %02X saddr: %02X:%02X:%02X:%02X:%02X:%02X "
       			"daddr: %02X:%02X:%02X:%02X:%02X:%02X\n",
       			(int)readb(llc + offsetof(struct trllc, ssap)),
       			(int)readb(llc + offsetof(struct trllc, dsap)),
       			(int)readb(trhhdr + offsetof(struct trh_hdr, saddr)),
       			(int)readb(trhhdr + offsetof(struct trh_hdr, saddr)+1),
       			(int)readb(trhhdr + offsetof(struct trh_hdr, saddr)+2),
       			(int)readb(trhhdr + offsetof(struct trh_hdr, saddr)+3),
       			(int)readb(trhhdr + offsetof(struct trh_hdr, saddr)+4),
       			(int)readb(trhhdr + offsetof(struct trh_hdr, saddr)+5),
       			(int)readb(trhhdr + offsetof(struct trh_hdr, daddr)),
       			(int)readb(trhhdr + offsetof(struct trh_hdr, daddr)+1),
       			(int)readb(trhhdr + offsetof(struct trh_hdr, daddr)+2),
       			(int)readb(trhhdr + offsetof(struct trh_hdr, daddr)+3),
       			(int)readb(trhhdr + offsetof(struct trh_hdr, daddr)+4),
       			(int)readb(trhhdr + offsetof(struct trh_hdr, daddr)+5));
       	}
#endif

       	skb_size = length-lan_hdr_len+sizeof(struct trh_hdr)+sizeof(struct trllc);
 
       	if (!(skb=dev_alloc_skb(skb_size))) {
       		DPRINTK("out of memory. frame dropped.\n");
       		ti->tr_stats.rx_dropped++;
       		writeb(DATA_LOST, ti->asb + offsetof(struct asb_rec, ret_code));
       		writeb(RESP_IN_ASB, ti->mmio + ACA_OFFSET + ACA_SET + ISRA_ODD);
       		return;
       	}

      	skb_put(skb, length);
	skb_reserve(skb, sizeof(struct trh_hdr)-lan_hdr_len+sizeof(struct trllc));
       	skb->dev=dev;
       	data=skb->data;
	rbuffer_len=ntohs(readw(rbuffer + offsetof(struct rec_buf, buf_len)));
	rbufdata = rbuffer + offsetof(struct rec_buf,data);

	if (IPv4_p) {
                /* Copy the headers without checksumming */
		memcpy_fromio(data, rbufdata, hdr_len);

		/* Watch for padded packets and bogons */
		iph=(struct iphdr*)(data + lan_hdr_len + sizeof(struct trllc));
		ip_len = ntohs(iph->tot_len) - sizeof(struct iphdr);
		length -= hdr_len;
		if ((ip_len <= length) && (ip_len > 7))
			length = ip_len;
		data += hdr_len;
		rbuffer_len -= hdr_len;
		rbufdata += hdr_len;
        }

	/* Copy the payload... */
	for (;;) {
		if (IPv4_p)
			chksum = csum_partial_copy(bus_to_virt(rbufdata), data,
						   length < rbuffer_len ? length : rbuffer_len,
						   chksum);
		else
			memcpy_fromio(data, rbufdata, rbuffer_len);
		rbuffer = ntohs(readw(rbuffer));
		if (!rbuffer)
			break;
		length -= rbuffer_len;
		data += rbuffer_len;
		rbuffer += ti->sram;
		rbuffer_len = ntohs(readw(rbuffer + offsetof(struct rec_buf, buf_len)));
		rbufdata = rbuffer + offsetof(struct rec_buf, data);
	}

       	writeb(0, ti->asb + offsetof(struct asb_rec, ret_code));

       	writeb(RESP_IN_ASB, ti->mmio + ACA_OFFSET + ACA_SET + ISRA_ODD);

	ti->tr_stats.rx_bytes += skb->len;
       	ti->tr_stats.rx_packets++;

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

 	if (IPv4_p){
		skb->csum      = chksum;
		skb->ip_summed = 1;
	}

	netif_rx(skb);
}

static int tok_send_packet(struct sk_buff *skb, struct net_device *dev)
{
	struct tok_info *ti;
	ti=(struct tok_info *) dev->priv;

	if (dev->tbusy) {
		int ticks_waited;

		ticks_waited=jiffies - dev->trans_start;
		if (ticks_waited<TR_BUSY_INTERVAL) return 1;

		DPRINTK("Arrg. Transmitter busy.\n");
		dev->trans_start+=5; /* we fake the transmission start time... */
		return 1;
	}

	if (test_and_set_bit(0,(void *)&dev->tbusy)!=0)
		DPRINTK("Transmitter access conflict\n");
	else {
		int flags;

		/* lock against other CPUs */
		spin_lock_irqsave(&(ti->lock), flags);

		/* Save skb; we'll need it when the adapter asks for the data */
		ti->current_skb=skb;
		writeb(XMIT_UI_FRAME, ti->srb + offsetof(struct srb_xmit, command));
		writew(ti->exsap_station_id, ti->srb
		       +offsetof(struct srb_xmit, station_id));
		writeb(CMD_IN_SRB, (ti->mmio + ACA_OFFSET + ACA_SET + ISRA_ODD));
		spin_unlock_irqrestore(&(ti->lock), flags);

		dev->trans_start=jiffies;
	}

	return 0;
}

void ibmtr_reset_timer(struct timer_list *tmr, struct net_device *dev) {
	tmr->expires  = jiffies + TR_RETRY_INTERVAL;
	tmr->data     = (unsigned long) dev;
	tmr->function = tok_open_adapter;
	init_timer(tmr);
	add_timer(tmr);
}

void ibmtr_readlog(struct net_device *dev) {
	 struct tok_info *ti;
	 ti=(struct tok_info *) dev->priv;

	 ti->readlog_pending = 0;
	 writeb(DIR_READ_LOG, ti->srb);
	 writeb(INT_ENABLE, ti->mmio + ACA_OFFSET + ACA_SET + ISRP_EVEN);
	 writeb(CMD_IN_SRB, ti->mmio + ACA_OFFSET + ACA_SET + ISRA_ODD);
	 dev->tbusy=1; /* really srb busy... */
}

/* tok_get_stats():  Basically a scaffold routine which will return
   the address of the tr_statistics structure associated with
   this device -- the tr.... structure is an ethnet look-alike
   so at least for this iteration may suffice.   */

static struct net_device_stats * tok_get_stats(struct net_device *dev) {

	struct tok_info *toki;
	toki=(struct tok_info *) dev->priv;
	return (struct net_device_stats *) &toki->tr_stats;
}

int ibmtr_change_mtu(struct net_device *dev, int mtu) {
	struct tok_info *ti = (struct tok_info *) dev->priv;
	
	if (ti->ring_speed == 16 && mtu > ti->maxmtu16)
		return -EINVAL;
	if (ti->ring_speed == 4 && mtu > ti->maxmtu4)
		return -EINVAL;
	dev->mtu = mtu;
	return 0;
}

#ifdef MODULE

/* 3COM 3C619C supports 8 interrupts, 32 I/O ports */
static struct net_device* dev_ibmtr[IBMTR_MAX_ADAPTERS];
static int io[IBMTR_MAX_ADAPTERS] = {0xa20,0xa24};
static int irq[IBMTR_MAX_ADAPTERS] = {0,0};
static int mem[IBMTR_MAX_ADAPTERS] = {0,0};

MODULE_PARM(io, "1-" __MODULE_STRING(IBMTR_MAX_ADAPTERS) "i");
MODULE_PARM(irq, "1-" __MODULE_STRING(IBMTR_MAX_ADAPTERS) "i");
MODULE_PARM(mem, "1-" __MODULE_STRING(IBMTR_MAX_ADAPTERS) "i");

int init_module(void)
{
        int i;
        for (i = 0; io[i] && (i<IBMTR_MAX_ADAPTERS); i++) {
	        irq[i] = 0;
		mem[i] = 0;
		dev_ibmtr[i] = NULL;
                dev_ibmtr[i] = init_trdev(dev_ibmtr[i], 0);
                if (dev_ibmtr[i] == NULL)
                        return -ENOMEM;

	        dev_ibmtr[i]->base_addr = io[i];
	        dev_ibmtr[i]->irq       = irq[i];
		dev_ibmtr[i]->mem_start = mem[i];
		dev_ibmtr[i]->init      = &ibmtr_probe;

	        if (register_trdev(dev_ibmtr[i]) != 0) {
			kfree_s(dev_ibmtr[i], sizeof(struct net_device));
			dev_ibmtr[i] = NULL;
		        if (i == 0) {
			        printk("ibmtr: register_trdev() returned non-zero.\n");
		                return -EIO;
			} else {
			        return 0;
			}
	        }
	}
	return 0;
}

void cleanup_module(void)
{
        int i;

        for (i = 0; i < IBMTR_MAX_ADAPTERS; i++)
	        if (dev_ibmtr[i]) {
			 unregister_trdev(dev_ibmtr[i]);
			 free_irq(dev_ibmtr[i]->irq, dev_ibmtr[i]);
			 release_region(dev_ibmtr[i]->base_addr, IBMTR_IO_EXTENT);
			 kfree_s(dev_ibmtr[i]->priv, sizeof(struct tok_info));
			 kfree_s(dev_ibmtr[i], sizeof(struct net_device));
			 dev_ibmtr[i] = NULL;
                }
}
#endif /* MODULE */