aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/net/irda/donauboe.c
blob: 1484c76becfb7addb3ea8bb51d9ab766e88103ca (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
/*****************************************************************
 *
 * Filename:		donauboe.c
 * Version: 		2.17
 * Description:   Driver for the Toshiba OBOE (or type-O or 701)
 *                FIR Chipset, also supports the DONAUOBOE (type-DO
 *                or d01) FIR chipset which as far as I know is
 *                register compatible.
 * Documentation: http://libxg.free.fr/irda/lib-irda.html
 * Status:        Experimental.
 * Author:        James McKenzie <james@fishsoup.dhs.org>
 * Created at:    Sat May 8  12:35:27 1999
 * Modified:      Paul Bristow <paul.bristow@technologist.com>
 * Modified:      Mon Nov 11 19:10:05 1999
 * Modified:      James McKenzie <james@fishsoup.dhs.org>
 * Modified:      Thu Mar 16 12:49:00 2000 (Substantial rewrite)
 * Modified:      Sat Apr 29 00:23:03 2000 (Added DONAUOBOE support)
 * Modified:      Wed May 24 23:45:02 2000 (Fixed chipio_t structure)
 * Modified: 2.13 Christian Gennerat <christian.gennerat@polytechnique.org>
 * Modified: 2.13 dim jan 07 21:57:39 2001 (tested with kernel 2.4 & irnet/ppp)
 * Modified: 2.14 Christian Gennerat <christian.gennerat@polytechnique.org>
 * Modified: 2.14 lun fev 05 17:55:59 2001 (adapted to patch-2.4.1-pre8-irda1)
 * Modified: 2.15 Martin Lucina <mato@kotelna.sk>
 * Modified: 2.15 Fri Jun 21 20:40:59 2002 (sync with 2.4.18, substantial fixes)
 * Modified: 2.16 Martin Lucina <mato@kotelna.sk>
 * Modified: 2.16 Sat Jun 22 18:54:29 2002 (fix freeregion, default to verbose)
 * Modified: 2.17 Christian Gennerat <christian.gennerat@polytechnique.org>
 * Modified: 2.17 jeu sep 12 08:50:20 2002 (save_flags();cli(); replaced by spinlocks)
 * Modified: 2.18 Christian Gennerat <christian.gennerat@polytechnique.org>
 * Modified: 2.18 ven jan 10 03:14:16 2003 Change probe default options
 *
 *     Copyright (c) 1999 James McKenzie, All Rights Reserved.
 *
 *     This program is free software; you can redistribute it and/or
 *     modify it under the terms of the GNU General Public License as
 *     published by the Free Software Foundation; either version 2 of
 *     the License, or (at your option) any later version.
 *
 *     Neither James McKenzie nor Cambridge University admit liability nor
 *     provide warranty for any of this software. This material is
 *     provided "AS-IS" and at no charge.
 *
 *     Applicable Models : Libretto 100/110CT and many more.
 *     Toshiba refers to this chip as the type-O IR port,
 *     or the type-DO IR port.
 *
 ********************************************************************/

/* Look at toshoboe.h (currently in include/net/irda) for details of */
/* Where to get documentation on the chip         */

/* See below for a description of the logic in this driver */

/* User servicable parts */
/* USE_PROBE Create the code which probes the chip and does a few tests */
/* do_probe module parameter Enable this code */
/* Probe code is very useful for understanding how the hardware works */
/* Use it with various combinations of TT_LEN, RX_LEN */
/* Strongly recomended, disable if the probe fails on your machine */
/* and send me <james@fishsoup.dhs.org> the output of dmesg */
#define USE_PROBE 1
#undef  USE_PROBE

/* Trace Transmit ring, interrupts, Receive ring or not ? */
#define PROBE_VERBOSE 1

/* Debug option, examine sent and received raw data */
/* Irdadump is better, but does not see all packets. enable it if you want. */
#undef DUMP_PACKETS

/* MIR mode has not been tested. Some behaviour is different */
/* Seems to work against an Ericsson R520 for me. -Martin */
#define USE_MIR

/* Schedule back to back hardware transmits wherever possible, otherwise */
/* we need an interrupt for every frame, unset if oboe works for a bit and */
/* then hangs */
#define OPTIMIZE_TX

/* Set the number of slots in the rings */
/* If you get rx/tx fifo overflows at high bitrates, you can try increasing */
/* these */

#define RING_SIZE (OBOE_RING_SIZE_RX8 | OBOE_RING_SIZE_TX8)
#define TX_SLOTS    8
#define RX_SLOTS    8


/* Less user servicable parts below here */

/* Test, Transmit and receive buffer sizes, adjust at your peril */
/* remarks: nfs usually needs 1k blocks */
/* remarks: in SIR mode, CRC is received, -> RX_LEN=TX_LEN+2 */
/* remarks: test accepts large blocks. Standard is 0x80 */
/* When TT_LEN > RX_LEN (SIR mode) data is stored in successive slots. */
/* When 3 or more slots are needed for each test packet, */
/* data received in the first slots is overwritten, even */
/* if OBOE_CTL_RX_HW_OWNS is not set, without any error! */
#define TT_LEN      0x80
#define TX_LEN      0xc00
#define RX_LEN      0xc04
/* Real transmitted length (SIR mode) is about 14+(2%*TX_LEN) more */
/* long than user-defined length (see async_wrap_skb) and is less then 4K */
/* Real received length is (max RX_LEN) differs from user-defined */
/* length only b the CRC (2 or 4 bytes) */
#define BUF_SAFETY  0x7a
#define RX_BUF_SZ   (RX_LEN)
#define TX_BUF_SZ   (TX_LEN+BUF_SAFETY)


/* Logic of the netdev part of this driver                             */

/* The RX ring is filled with buffers, when a packet arrives           */
/* it is DMA'd into the buffer which is marked used and RxDone called  */
/* RxDone forms an skb (and checks the CRC if in SIR mode) and ships   */
/* the packet off upstairs */

/* The transmitter on the oboe chip can work in one of two modes       */
/* for each ring->tx[] the transmitter can either                      */
/* a) transmit the packet, leave the trasmitter enabled and proceed to */
/*    the next ring                                                    */
/* OR                                                                  */
/* b) transmit the packet, switch off the transmitter and issue TxDone */

/* All packets are entered into the ring in mode b), if the ring was   */
/* empty the transmitter is started.                                   */

/* If OPTIMIZE_TX is defined then in TxDone if the ring contains       */
/* more than one packet, all but the last are set to mode a) [HOWEVER  */
/* the hardware may not notice this, this is why we start in mode b) ] */
/* then restart the transmitter                                        */

/* If OPTIMIZE_TX is not defined then we just restart the transmitter  */
/* if the ring isn't empty */

/* Speed changes are delayed until the TxRing is empty                 */
/* mtt is handled by generating packets with bad CRCs, before the data */

/* TODO: */
/* check the mtt works ok      */
/* finish the watchdog         */

/* No user servicable parts below here */

#include <linux/module.h>

#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/skbuff.h>
#include <linux/netdevice.h>
#include <linux/ioport.h>
#include <linux/delay.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/pci.h>
#include <linux/rtnetlink.h>

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

#include <net/irda/wrapper.h>
#include <net/irda/irda.h>
//#include <net/irda/irmod.h>
//#include <net/irda/irlap_frame.h>
#include <net/irda/irda_device.h>
#include <net/irda/crc.h>

#include "donauboe.h"

#define INB(port)       inb_p(port)
#define OUTB(val,port)  outb_p(val,port)
#define OUTBP(val,port) outb_p(val,port)

#define PROMPT  OUTB(OBOE_PROMPT_BIT,OBOE_PROMPT);

#if PROBE_VERBOSE
#define PROBE_DEBUG(args...) (printk (args))
#else
#define PROBE_DEBUG(args...) ;
#endif

/* Set the DMA to be byte at a time */
#define CONFIG0H_DMA_OFF OBOE_CONFIG0H_RCVANY
#define CONFIG0H_DMA_ON_NORX CONFIG0H_DMA_OFF| OBOE_CONFIG0H_ENDMAC
#define CONFIG0H_DMA_ON CONFIG0H_DMA_ON_NORX | OBOE_CONFIG0H_ENRX

static struct pci_device_id toshoboe_pci_tbl[] = {
	{ PCI_VENDOR_ID_TOSHIBA, PCI_DEVICE_ID_FIR701, PCI_ANY_ID, PCI_ANY_ID, },
	{ PCI_VENDOR_ID_TOSHIBA, PCI_DEVICE_ID_FIRD01, PCI_ANY_ID, PCI_ANY_ID, },
	{ }			/* Terminating entry */
};
MODULE_DEVICE_TABLE(pci, toshoboe_pci_tbl);

#define DRIVER_NAME "toshoboe"
static char *driver_name = DRIVER_NAME;

static int max_baud = 4000000;
#ifdef USE_PROBE
static int do_probe = 0;
#endif


/**********************************************************************/
static int
toshoboe_checkfcs (unsigned char *buf, int len)
{
  int i;
  union
  {
    __u16 value;
    __u8 bytes[2];
  }
  fcs;

  fcs.value = INIT_FCS;

  for (i = 0; i < len; ++i)
    fcs.value = irda_fcs (fcs.value, *(buf++));

  return (fcs.value == GOOD_FCS);
}

/***********************************************************************/
/* Generic chip handling code */
#ifdef DUMP_PACKETS
static unsigned char dump[50];
static void
_dumpbufs (unsigned char *data, int len, char tete)
{
int i,j;
char head=tete;
for (i=0;i<len;i+=16) {
    for (j=0;j<16 && i+j<len;j++) { sprintf(&dump[3*j],"%02x.",data[i+j]); }
    dump [3*j]=0;
    IRDA_DEBUG (2, "%c%s\n",head , dump);
    head='+';
    }
}
#endif

#ifdef USE_PROBE
/* Dump the registers */
static void
toshoboe_dumpregs (struct toshoboe_cb *self)
{
  __u32 ringbase;

  IRDA_DEBUG (4, "%s()\n", __func__);

  ringbase = INB (OBOE_RING_BASE0) << 10;
  ringbase |= INB (OBOE_RING_BASE1) << 18;
  ringbase |= INB (OBOE_RING_BASE2) << 26;

  printk (KERN_ERR DRIVER_NAME ": Register dump:\n");
  printk (KERN_ERR "Interrupts: Tx:%d Rx:%d TxUnder:%d RxOver:%d Sip:%d\n",
          self->int_tx, self->int_rx, self->int_txunder, self->int_rxover,
          self->int_sip);
  printk (KERN_ERR "RX %02x TX %02x RingBase %08x\n",
          INB (OBOE_RXSLOT), INB (OBOE_TXSLOT), ringbase);
  printk (KERN_ERR "RING_SIZE %02x IER %02x ISR %02x\n",
          INB (OBOE_RING_SIZE), INB (OBOE_IER), INB (OBOE_ISR));
  printk (KERN_ERR "CONFIG1 %02x STATUS %02x\n",
          INB (OBOE_CONFIG1), INB (OBOE_STATUS));
  printk (KERN_ERR "CONFIG0 %02x%02x ENABLE %02x%02x\n",
          INB (OBOE_CONFIG0H), INB (OBOE_CONFIG0L),
          INB (OBOE_ENABLEH), INB (OBOE_ENABLEL));
  printk (KERN_ERR "NEW_PCONFIG %02x%02x CURR_PCONFIG %02x%02x\n",
          INB (OBOE_NEW_PCONFIGH), INB (OBOE_NEW_PCONFIGL),
          INB (OBOE_CURR_PCONFIGH), INB (OBOE_CURR_PCONFIGL));
  printk (KERN_ERR "MAXLEN %02x%02x RXCOUNT %02x%02x\n",
          INB (OBOE_MAXLENH), INB (OBOE_MAXLENL),
          INB (OBOE_RXCOUNTL), INB (OBOE_RXCOUNTH));

  if (self->ring)
    {
      int i;
      ringbase = virt_to_bus (self->ring);
      printk (KERN_ERR "Ring at %08x:\n", ringbase);
      printk (KERN_ERR "RX:");
      for (i = 0; i < RX_SLOTS; ++i)
        printk (" (%d,%02x)",self->ring->rx[i].len,self->ring->rx[i].control);
      printk ("\n");
      printk (KERN_ERR "TX:");
      for (i = 0; i < RX_SLOTS; ++i)
        printk (" (%d,%02x)",self->ring->tx[i].len,self->ring->tx[i].control);
      printk ("\n");
    }
}
#endif

/*Don't let the chip look at memory */
static void
toshoboe_disablebm (struct toshoboe_cb *self)
{
  __u8 command;
  IRDA_DEBUG (4, "%s()\n", __func__);

  pci_read_config_byte (self->pdev, PCI_COMMAND, &command);
  command &= ~PCI_COMMAND_MASTER;
  pci_write_config_byte (self->pdev, PCI_COMMAND, command);

}

/* Shutdown the chip and point the taskfile reg somewhere else */
static void
toshoboe_stopchip (struct toshoboe_cb *self)
{
  IRDA_DEBUG (4, "%s()\n", __func__);

  /*Disable interrupts */
  OUTB (0x0, OBOE_IER);
  /*Disable DMA, Disable Rx, Disable Tx */
  OUTB (CONFIG0H_DMA_OFF, OBOE_CONFIG0H);
  /*Disable SIR MIR FIR, Tx and Rx */
  OUTB (0x00, OBOE_ENABLEH);
  /*Point the ring somewhere safe */
  OUTB (0x3f, OBOE_RING_BASE2);
  OUTB (0xff, OBOE_RING_BASE1);
  OUTB (0xff, OBOE_RING_BASE0);

  OUTB (RX_LEN >> 8, OBOE_MAXLENH);
  OUTB (RX_LEN & 0xff, OBOE_MAXLENL);

  /*Acknoledge any pending interrupts */
  OUTB (0xff, OBOE_ISR);

  /*Why */
  OUTB (OBOE_ENABLEH_PHYANDCLOCK, OBOE_ENABLEH);

  /*switch it off */
  OUTB (OBOE_CONFIG1_OFF, OBOE_CONFIG1);

  toshoboe_disablebm (self);
}

/* Transmitter initialization */
static void
toshoboe_start_DMA (struct toshoboe_cb *self, int opts)
{
  OUTB (0x0, OBOE_ENABLEH);
  OUTB (CONFIG0H_DMA_ON | opts,  OBOE_CONFIG0H);
  OUTB (OBOE_ENABLEH_PHYANDCLOCK, OBOE_ENABLEH);
  PROMPT;
}

/*Set the baud rate */
static void
toshoboe_setbaud (struct toshoboe_cb *self)
{
  __u16 pconfig = 0;
  __u8 config0l = 0;

  IRDA_DEBUG (2, "%s(%d/%d)\n", __func__, self->speed, self->io.speed);

  switch (self->speed)
    {
    case 2400:
    case 4800:
    case 9600:
    case 19200:
    case 38400:
    case 57600:
    case 115200:
#ifdef USE_MIR
    case 1152000:
#endif
    case 4000000:
      break;
    default:

      printk (KERN_ERR DRIVER_NAME ": switch to unsupported baudrate %d\n",
              self->speed);
      return;
    }

  switch (self->speed)
    {
      /* For SIR the preamble is done by adding XBOFs */
      /* to the packet */
      /* set to filtered SIR mode, filter looks for BOF and EOF */
    case 2400:
      pconfig |= 47 << OBOE_PCONFIG_BAUDSHIFT;
      pconfig |= 25 << OBOE_PCONFIG_WIDTHSHIFT;
      break;
    case 4800:
      pconfig |= 23 << OBOE_PCONFIG_BAUDSHIFT;
      pconfig |= 25 << OBOE_PCONFIG_WIDTHSHIFT;
      break;
    case 9600:
      pconfig |= 11 << OBOE_PCONFIG_BAUDSHIFT;
      pconfig |= 25 << OBOE_PCONFIG_WIDTHSHIFT;
      break;
    case 19200:
      pconfig |= 5 << OBOE_PCONFIG_BAUDSHIFT;
      pconfig |= 25 << OBOE_PCONFIG_WIDTHSHIFT;
      break;
    case 38400:
      pconfig |= 2 << OBOE_PCONFIG_BAUDSHIFT;
      pconfig |= 25 << OBOE_PCONFIG_WIDTHSHIFT;
      break;
    case 57600:
      pconfig |= 1 << OBOE_PCONFIG_BAUDSHIFT;
      pconfig |= 25 << OBOE_PCONFIG_WIDTHSHIFT;
      break;
    case 115200:
      pconfig |= 0 << OBOE_PCONFIG_BAUDSHIFT;
      pconfig |= 25 << OBOE_PCONFIG_WIDTHSHIFT;
      break;
    default:
      /*Set to packet based reception */
      OUTB (RX_LEN >> 8, OBOE_MAXLENH);
      OUTB (RX_LEN & 0xff, OBOE_MAXLENL);
      break;
    }

  switch (self->speed)
    {
    case 2400:
    case 4800:
    case 9600:
    case 19200:
    case 38400:
    case 57600:
    case 115200:
      config0l = OBOE_CONFIG0L_ENSIR;
      if (self->async)
        {
          /*Set to character based reception */
          /*System will lock if MAXLEN=0 */
          /*so have to be careful */
          OUTB (0x01, OBOE_MAXLENH);
          OUTB (0x01, OBOE_MAXLENL);
          OUTB (0x00, OBOE_MAXLENH);
        }
      else
        {
          /*Set to packet based reception */
          config0l |= OBOE_CONFIG0L_ENSIRF;
          OUTB (RX_LEN >> 8, OBOE_MAXLENH);
          OUTB (RX_LEN & 0xff, OBOE_MAXLENL);
        }
      break;

#ifdef USE_MIR
      /* MIR mode */
      /* Set for 16 bit CRC and enable MIR */
      /* Preamble now handled by the chip */
    case 1152000:
      pconfig |= 0 << OBOE_PCONFIG_BAUDSHIFT;
      pconfig |= 8 << OBOE_PCONFIG_WIDTHSHIFT;
      pconfig |= 1 << OBOE_PCONFIG_PREAMBLESHIFT;
      config0l = OBOE_CONFIG0L_CRC16 | OBOE_CONFIG0L_ENMIR;
      break;
#endif
      /* FIR mode */
      /* Set for 32 bit CRC and enable FIR */
      /* Preamble handled by the chip */
    case 4000000:
      pconfig |= 0 << OBOE_PCONFIG_BAUDSHIFT;
      /* Documentation says 14, but toshiba use 15 in their drivers */
      pconfig |= 15 << OBOE_PCONFIG_PREAMBLESHIFT;
      config0l = OBOE_CONFIG0L_ENFIR;
      break;
    }

  /* Copy into new PHY config buffer */
  OUTBP (pconfig >> 8, OBOE_NEW_PCONFIGH);
  OUTB (pconfig & 0xff, OBOE_NEW_PCONFIGL);
  OUTB (config0l, OBOE_CONFIG0L);

  /* Now make OBOE copy from new PHY to current PHY */
  OUTB (0x0, OBOE_ENABLEH);
  OUTB (OBOE_ENABLEH_PHYANDCLOCK, OBOE_ENABLEH);
  PROMPT;

  /* speed change executed */
  self->new_speed = 0;
  self->io.speed = self->speed;
}

/*Let the chip look at memory */
static void
toshoboe_enablebm (struct toshoboe_cb *self)
{
  IRDA_DEBUG (4, "%s()\n", __func__);
  pci_set_master (self->pdev);
}

/*setup the ring */
static void
toshoboe_initring (struct toshoboe_cb *self)
{
  int i;

  IRDA_DEBUG (4, "%s()\n", __func__);

  for (i = 0; i < TX_SLOTS; ++i)
    {
      self->ring->tx[i].len = 0;
      self->ring->tx[i].control = 0x00;
      self->ring->tx[i].address = virt_to_bus (self->tx_bufs[i]);
    }

  for (i = 0; i < RX_SLOTS; ++i)
    {
      self->ring->rx[i].len = RX_LEN;
      self->ring->rx[i].len = 0;
      self->ring->rx[i].address = virt_to_bus (self->rx_bufs[i]);
      self->ring->rx[i].control = OBOE_CTL_RX_HW_OWNS;
    }
}

static void
toshoboe_resetptrs (struct toshoboe_cb *self)
{
  /* Can reset pointers by twidling DMA */
  OUTB (0x0, OBOE_ENABLEH);
  OUTBP (CONFIG0H_DMA_OFF, OBOE_CONFIG0H);
  OUTB (OBOE_ENABLEH_PHYANDCLOCK, OBOE_ENABLEH);

  self->rxs = inb_p (OBOE_RXSLOT) & OBOE_SLOT_MASK;
  self->txs = inb_p (OBOE_TXSLOT) & OBOE_SLOT_MASK;
}

/* Called in locked state */
static void
toshoboe_initptrs (struct toshoboe_cb *self)
{

  /* spin_lock_irqsave(self->spinlock, flags); */
  /* save_flags (flags); */

  /* Can reset pointers by twidling DMA */
  toshoboe_resetptrs (self);

  OUTB (0x0, OBOE_ENABLEH);
  OUTB (CONFIG0H_DMA_ON, OBOE_CONFIG0H);
  OUTB (OBOE_ENABLEH_PHYANDCLOCK, OBOE_ENABLEH);

  self->txpending = 0;

  /* spin_unlock_irqrestore(self->spinlock, flags); */
  /* restore_flags (flags); */
}

/* Wake the chip up and get it looking at the rings */
/* Called in locked state */
static void
toshoboe_startchip (struct toshoboe_cb *self)
{
  __u32 physaddr;

  IRDA_DEBUG (4, "%s()\n", __func__);

  toshoboe_initring (self);
  toshoboe_enablebm (self);
  OUTBP (OBOE_CONFIG1_RESET, OBOE_CONFIG1);
  OUTBP (OBOE_CONFIG1_ON, OBOE_CONFIG1);

  /* Stop the clocks */
  OUTB (0, OBOE_ENABLEH);

  /*Set size of rings */
  OUTB (RING_SIZE, OBOE_RING_SIZE);

  /*Acknoledge any pending interrupts */
  OUTB (0xff, OBOE_ISR);

  /*Enable ints */
  OUTB (OBOE_INT_TXDONE  | OBOE_INT_RXDONE |
        OBOE_INT_TXUNDER | OBOE_INT_RXOVER | OBOE_INT_SIP , OBOE_IER);

  /*Acknoledge any pending interrupts */
  OUTB (0xff, OBOE_ISR);

  /*Set the maximum packet length to 0xfff (4095) */
  OUTB (RX_LEN >> 8, OBOE_MAXLENH);
  OUTB (RX_LEN & 0xff, OBOE_MAXLENL);

  /*Shutdown DMA */
  OUTB (CONFIG0H_DMA_OFF, OBOE_CONFIG0H);

  /*Find out where the rings live */
  physaddr = virt_to_bus (self->ring);

  IRDA_ASSERT ((physaddr & 0x3ff) == 0,
	       printk (KERN_ERR DRIVER_NAME "ring not correctly aligned\n");
	       return;);

  OUTB ((physaddr >> 10) & 0xff, OBOE_RING_BASE0);
  OUTB ((physaddr >> 18) & 0xff, OBOE_RING_BASE1);
  OUTB ((physaddr >> 26) & 0x3f, OBOE_RING_BASE2);

  /*Enable DMA controller in byte mode and RX */
  OUTB (CONFIG0H_DMA_ON, OBOE_CONFIG0H);

  /* Start up the clocks */
  OUTB (OBOE_ENABLEH_PHYANDCLOCK, OBOE_ENABLEH);

  /*set to sensible speed */
  self->speed = 9600;
  toshoboe_setbaud (self);
  toshoboe_initptrs (self);
}

static void
toshoboe_isntstuck (struct toshoboe_cb *self)
{
}

static void
toshoboe_checkstuck (struct toshoboe_cb *self)
{
  unsigned long flags;

  if (0)
    {
      spin_lock_irqsave(&self->spinlock, flags);

      /* This will reset the chip completely */
      printk (KERN_ERR DRIVER_NAME ": Resetting chip\n");

      toshoboe_stopchip (self);
      toshoboe_startchip (self);
      spin_unlock_irqrestore(&self->spinlock, flags);
    }
}

/*Generate packet of about mtt us long */
static int
toshoboe_makemttpacket (struct toshoboe_cb *self, void *buf, int mtt)
{
  int xbofs;

  xbofs = ((int) (mtt/100)) * (int) (self->speed);
  xbofs=xbofs/80000; /*Eight bits per byte, and mtt is in us*/
  xbofs++;

  IRDA_DEBUG (2, DRIVER_NAME
      ": generated mtt of %d bytes for %d us at %d baud\n"
	  , xbofs,mtt,self->speed);

  if (xbofs > TX_LEN)
    {
      printk (KERN_ERR DRIVER_NAME ": wanted %d bytes MTT but TX_LEN is %d\n",
              xbofs, TX_LEN);
      xbofs = TX_LEN;
    }

  /*xbofs will do for SIR, MIR and FIR,SIR mode doesn't generate a checksum anyway */
  memset (buf, XBOF, xbofs);

  return xbofs;
}

#ifdef USE_PROBE
/***********************************************************************/
/* Probe code */

static void
toshoboe_dumptx (struct toshoboe_cb *self)
{
  int i;
  PROBE_DEBUG(KERN_WARNING "TX:");
  for (i = 0; i < RX_SLOTS; ++i)
    PROBE_DEBUG(" (%d,%02x)",self->ring->tx[i].len,self->ring->tx[i].control);
  PROBE_DEBUG(" [%d]\n",self->speed);
}

static void
toshoboe_dumprx (struct toshoboe_cb *self, int score)
{
  int i;
  PROBE_DEBUG(" %d\nRX:",score);
  for (i = 0; i < RX_SLOTS; ++i)
    PROBE_DEBUG(" (%d,%02x)",self->ring->rx[i].len,self->ring->rx[i].control);
  PROBE_DEBUG("\n");
}

static inline int
stuff_byte (__u8 byte, __u8 * buf)
{
  switch (byte)
    {
    case BOF:                  /* FALLTHROUGH */
    case EOF:                  /* FALLTHROUGH */
    case CE:
      /* Insert transparently coded */
      buf[0] = CE;              /* Send link escape */
      buf[1] = byte ^ IRDA_TRANS; /* Complement bit 5 */
      return 2;
      /* break; */
    default:
      /* Non-special value, no transparency required */
      buf[0] = byte;
      return 1;
      /* break; */
    }
}

static irqreturn_t
toshoboe_probeinterrupt (int irq, void *dev_id)
{
  struct toshoboe_cb *self = dev_id;
  __u8 irqstat;

  irqstat = INB (OBOE_ISR);

/* was it us */
  if (!(irqstat & OBOE_INT_MASK))
    return IRQ_NONE;

/* Ack all the interrupts */
  OUTB (irqstat, OBOE_ISR);

  if (irqstat & OBOE_INT_TXDONE)
    {
      int txp;

      self->int_tx++;
      PROBE_DEBUG("T");

      txp = INB (OBOE_TXSLOT) & OBOE_SLOT_MASK;
      if (self->ring->tx[txp].control & OBOE_CTL_TX_HW_OWNS)
        {
          self->int_tx+=100;
          PROBE_DEBUG("S");
          toshoboe_start_DMA(self, OBOE_CONFIG0H_ENTX | OBOE_CONFIG0H_LOOP);
        }
    }

  if (irqstat & OBOE_INT_RXDONE) {
    self->int_rx++;
    PROBE_DEBUG("R"); }
  if (irqstat & OBOE_INT_TXUNDER) {
    self->int_txunder++;
    PROBE_DEBUG("U"); }
  if (irqstat & OBOE_INT_RXOVER) {
    self->int_rxover++;
    PROBE_DEBUG("O"); }
  if (irqstat & OBOE_INT_SIP) {
    self->int_sip++;
    PROBE_DEBUG("I"); }
  return IRQ_HANDLED;
}

static int
toshoboe_maketestpacket (unsigned char *buf, int badcrc, int fir)
{
  int i;
  int len = 0;
  union
  {
    __u16 value;
    __u8 bytes[2];
  }
  fcs;

  if (fir)
    {
      memset (buf, 0, TT_LEN);
      return (TT_LEN);
    }

  fcs.value = INIT_FCS;

  memset (buf, XBOF, 10);
  len += 10;
  buf[len++] = BOF;

  for (i = 0; i < TT_LEN; ++i)
    {
      len += stuff_byte (i, buf + len);
      fcs.value = irda_fcs (fcs.value, i);
    }

  len += stuff_byte (fcs.bytes[0] ^ badcrc, buf + len);
  len += stuff_byte (fcs.bytes[1] ^ badcrc, buf + len);
  buf[len++] = EOF;
  len++;
  return len;
}

static int
toshoboe_probefail (struct toshoboe_cb *self, char *msg)
{
  printk (KERN_ERR DRIVER_NAME "probe(%d) failed %s\n",self-> speed, msg);
  toshoboe_dumpregs (self);
  toshoboe_stopchip (self);
  free_irq (self->io.irq, (void *) self);
  return 0;
}

static int
toshoboe_numvalidrcvs (struct toshoboe_cb *self)
{
  int i, ret = 0;
  for (i = 0; i < RX_SLOTS; ++i)
    if ((self->ring->rx[i].control & 0xe0) == 0)
      ret++;

  return ret;
}

static int
toshoboe_numrcvs (struct toshoboe_cb *self)
{
  int i, ret = 0;
  for (i = 0; i < RX_SLOTS; ++i)
    if (!(self->ring->rx[i].control & OBOE_CTL_RX_HW_OWNS))
      ret++;

  return ret;
}

static int
toshoboe_probe (struct toshoboe_cb *self)
{
  int i, j, n;
#ifdef USE_MIR
  int bauds[] = { 9600, 115200, 4000000, 1152000 };
#else
  int bauds[] = { 9600, 115200, 4000000 };
#endif
  unsigned long flags;

  IRDA_DEBUG (4, "%s()\n", __func__);

  if (request_irq (self->io.irq, toshoboe_probeinterrupt,
                   self->io.irqflags, "toshoboe", (void *) self))
    {
      printk (KERN_ERR DRIVER_NAME ": probe failed to allocate irq %d\n",
              self->io.irq);
      return 0;
    }

  /* test 1: SIR filter and back to back */

  for (j = 0; j < ARRAY_SIZE(bauds); ++j)
    {
      int fir = (j > 1);
      toshoboe_stopchip (self);


      spin_lock_irqsave(&self->spinlock, flags);
      /*Address is already setup */
      toshoboe_startchip (self);
      self->int_rx = self->int_tx = 0;
      self->speed = bauds[j];
      toshoboe_setbaud (self);
      toshoboe_initptrs (self);
      spin_unlock_irqrestore(&self->spinlock, flags);

      self->ring->tx[self->txs].control =
/*   (FIR only) OBOE_CTL_TX_SIP needed for switching to next slot */
/*    MIR: all received data is stored in one slot */
        (fir) ? OBOE_CTL_TX_HW_OWNS | OBOE_CTL_TX_RTCENTX
              : OBOE_CTL_TX_HW_OWNS ;
      self->ring->tx[self->txs].len =
        toshoboe_maketestpacket (self->tx_bufs[self->txs], 0, fir);
      self->txs++;
      self->txs %= TX_SLOTS;

      self->ring->tx[self->txs].control =
        (fir) ? OBOE_CTL_TX_HW_OWNS | OBOE_CTL_TX_SIP
              : OBOE_CTL_TX_HW_OWNS | OBOE_CTL_TX_RTCENTX ;
      self->ring->tx[self->txs].len =
        toshoboe_maketestpacket (self->tx_bufs[self->txs], 0, fir);
      self->txs++;
      self->txs %= TX_SLOTS;

      self->ring->tx[self->txs].control =
        (fir) ? OBOE_CTL_TX_HW_OWNS | OBOE_CTL_TX_RTCENTX
              : OBOE_CTL_TX_HW_OWNS ;
      self->ring->tx[self->txs].len =
        toshoboe_maketestpacket (self->tx_bufs[self->txs], 0, fir);
      self->txs++;
      self->txs %= TX_SLOTS;

      self->ring->tx[self->txs].control =
        (fir) ? OBOE_CTL_TX_HW_OWNS | OBOE_CTL_TX_RTCENTX
              | OBOE_CTL_TX_SIP     | OBOE_CTL_TX_BAD_CRC
              : OBOE_CTL_TX_HW_OWNS | OBOE_CTL_TX_RTCENTX ;
      self->ring->tx[self->txs].len =
        toshoboe_maketestpacket (self->tx_bufs[self->txs], 0, fir);
      self->txs++;
      self->txs %= TX_SLOTS;

      toshoboe_dumptx (self);
      /* Turn on TX and RX and loopback */
      toshoboe_start_DMA(self, OBOE_CONFIG0H_ENTX | OBOE_CONFIG0H_LOOP);

      i = 0;
      n = fir ? 1 : 4;
      while (toshoboe_numvalidrcvs (self) != n)
        {
          if (i > 4800)
              return toshoboe_probefail (self, "filter test");
          udelay ((9600*(TT_LEN+16))/self->speed);
          i++;
        }

      n = fir ? 203 : 102;
      while ((toshoboe_numrcvs(self) != self->int_rx) || (self->int_tx != n))
        {
          if (i > 4800)
              return toshoboe_probefail (self, "interrupt test");
          udelay ((9600*(TT_LEN+16))/self->speed);
          i++;
        }
     toshoboe_dumprx (self,i);

     }

  /* test 2: SIR in char at a time */

  toshoboe_stopchip (self);
  self->int_rx = self->int_tx = 0;

  spin_lock_irqsave(&self->spinlock, flags);
  toshoboe_startchip (self);
  spin_unlock_irqrestore(&self->spinlock, flags);

  self->async = 1;
  self->speed = 115200;
  toshoboe_setbaud (self);
  self->ring->tx[self->txs].control =
    OBOE_CTL_TX_RTCENTX | OBOE_CTL_TX_HW_OWNS;
  self->ring->tx[self->txs].len = 4;

  ((unsigned char *) self->tx_bufs[self->txs])[0] = 'f';
  ((unsigned char *) self->tx_bufs[self->txs])[1] = 'i';
  ((unsigned char *) self->tx_bufs[self->txs])[2] = 's';
  ((unsigned char *) self->tx_bufs[self->txs])[3] = 'h';
  toshoboe_dumptx (self);
  toshoboe_start_DMA(self, OBOE_CONFIG0H_ENTX | OBOE_CONFIG0H_LOOP);

  i = 0;
  while (toshoboe_numvalidrcvs (self) != 4)
    {
      if (i > 100)
          return toshoboe_probefail (self, "Async test");
      udelay (100);
      i++;
    }

  while ((toshoboe_numrcvs (self) != self->int_rx) || (self->int_tx != 1))
    {
      if (i > 100)
          return toshoboe_probefail (self, "Async interrupt test");
      udelay (100);
      i++;
    }
  toshoboe_dumprx (self,i);

  self->async = 0;
  self->speed = 9600;
  toshoboe_setbaud (self);
  toshoboe_stopchip (self);

  free_irq (self->io.irq, (void *) self);

  printk (KERN_WARNING DRIVER_NAME ": Self test passed ok\n");

  return 1;
}
#endif

/******************************************************************/
/* Netdev style code */

/* Transmit something */
static int
toshoboe_hard_xmit (struct sk_buff *skb, struct net_device *dev)
{
  struct toshoboe_cb *self;
  __s32 speed;
  int mtt, len, ctl;
  unsigned long flags;
  struct irda_skb_cb *cb = (struct irda_skb_cb *) skb->cb;

  self = netdev_priv(dev);

  IRDA_ASSERT (self != NULL, return 0; );

  IRDA_DEBUG (1, "%s.tx:%x(%x)%x\n", __func__
      ,skb->len,self->txpending,INB (OBOE_ENABLEH));
  if (!cb->magic) {
      IRDA_DEBUG (2, "%s.Not IrLAP:%x\n", __func__, cb->magic);
#ifdef DUMP_PACKETS
      _dumpbufs(skb->data,skb->len,'>');
#endif
    }

  /* change speed pending, wait for its execution */
  if (self->new_speed)
      return NETDEV_TX_BUSY;

  /* device stopped (apm) wait for restart */
  if (self->stopped)
      return NETDEV_TX_BUSY;

  toshoboe_checkstuck (self);

  dev->trans_start = jiffies;

 /* Check if we need to change the speed */
  /* But not now. Wait after transmission if mtt not required */
  speed=irda_get_next_speed(skb);
  if ((speed != self->io.speed) && (speed != -1))
    {
      spin_lock_irqsave(&self->spinlock, flags);

      if (self->txpending || skb->len)
        {
          self->new_speed = speed;
          IRDA_DEBUG (1, "%s: Queued TxDone scheduled speed change %d\n" ,
		      __func__, speed);
          /* if no data, that's all! */
          if (!skb->len)
            {
	      spin_unlock_irqrestore(&self->spinlock, flags);
              dev_kfree_skb (skb);
              return NETDEV_TX_OK;
            }
          /* True packet, go on, but */
          /* do not accept anything before change speed execution */
          netif_stop_queue(dev);
          /* ready to process TxDone interrupt */
	  spin_unlock_irqrestore(&self->spinlock, flags);
        }
      else
        {
          /* idle and no data, change speed now */
          self->speed = speed;
          toshoboe_setbaud (self);
	  spin_unlock_irqrestore(&self->spinlock, flags);
          dev_kfree_skb (skb);
          return NETDEV_TX_OK;
        }

    }

  if ((mtt = irda_get_mtt(skb)))
    {
      /* This is fair since the queue should be empty anyway */
      spin_lock_irqsave(&self->spinlock, flags);

      if (self->txpending)
        {
	  spin_unlock_irqrestore(&self->spinlock, flags);
          return NETDEV_TX_BUSY;
        }

      /* If in SIR mode we need to generate a string of XBOFs */
      /* In MIR and FIR we need to generate a string of data */
      /* which we will add a wrong checksum to */

      mtt = toshoboe_makemttpacket (self, self->tx_bufs[self->txs], mtt);
      IRDA_DEBUG (1, "%s.mtt:%x(%x)%d\n", __func__
          ,skb->len,mtt,self->txpending);
      if (mtt)
        {
          self->ring->tx[self->txs].len = mtt & 0xfff;

          ctl = OBOE_CTL_TX_HW_OWNS | OBOE_CTL_TX_RTCENTX;
          if (INB (OBOE_ENABLEH) & OBOE_ENABLEH_FIRON)
            {
              ctl |= OBOE_CTL_TX_BAD_CRC | OBOE_CTL_TX_SIP ;
            }
#ifdef USE_MIR
          else if (INB (OBOE_ENABLEH) & OBOE_ENABLEH_MIRON)
            {
              ctl |= OBOE_CTL_TX_BAD_CRC;
            }
#endif
          self->ring->tx[self->txs].control = ctl;

          OUTB (0x0, OBOE_ENABLEH);
          /* It is only a timer. Do not send mtt packet outside! */
          toshoboe_start_DMA(self, OBOE_CONFIG0H_ENTX | OBOE_CONFIG0H_LOOP);

          self->txpending++;

          self->txs++;
          self->txs %= TX_SLOTS;

        }
      else
        {
          printk(KERN_ERR DRIVER_NAME ": problem with mtt packet - ignored\n");
        }
      spin_unlock_irqrestore(&self->spinlock, flags);
    }

#ifdef DUMP_PACKETS
dumpbufs(skb->data,skb->len,'>');
#endif

  spin_lock_irqsave(&self->spinlock, flags);

  if (self->ring->tx[self->txs].control & OBOE_CTL_TX_HW_OWNS)
    {
      IRDA_DEBUG (0, "%s.ful:%x(%x)%x\n", __func__
          ,skb->len, self->ring->tx[self->txs].control, self->txpending);
      toshoboe_start_DMA(self, OBOE_CONFIG0H_ENTX);
      spin_unlock_irqrestore(&self->spinlock, flags);
      return NETDEV_TX_BUSY;
    }

  if (INB (OBOE_ENABLEH) & OBOE_ENABLEH_SIRON)
    {
      len = async_wrap_skb (skb, self->tx_bufs[self->txs], TX_BUF_SZ);
    }
  else
    {
      len = skb->len;
      skb_copy_from_linear_data(skb, self->tx_bufs[self->txs], len);
    }
  self->ring->tx[self->txs].len = len & 0x0fff;

  /*Sometimes the HW doesn't see us assert RTCENTX in the interrupt code */
  /*later this plays safe, we garuntee the last packet to be transmitted */
  /*has RTCENTX set */

  ctl = OBOE_CTL_TX_HW_OWNS | OBOE_CTL_TX_RTCENTX;
  if (INB (OBOE_ENABLEH) & OBOE_ENABLEH_FIRON)
    {
      ctl |= OBOE_CTL_TX_SIP ;
    }
  self->ring->tx[self->txs].control = ctl;

  /* If transmitter is idle start in one-shot mode */

  if (!self->txpending)
      toshoboe_start_DMA(self, OBOE_CONFIG0H_ENTX);

  self->txpending++;

  self->txs++;
  self->txs %= TX_SLOTS;

  spin_unlock_irqrestore(&self->spinlock, flags);
  dev_kfree_skb (skb);

  return NETDEV_TX_OK;
}

/*interrupt handler */
static irqreturn_t
toshoboe_interrupt (int irq, void *dev_id)
{
  struct toshoboe_cb *self = dev_id;
  __u8 irqstat;
  struct sk_buff *skb = NULL;

  irqstat = INB (OBOE_ISR);

/* was it us */
  if (!(irqstat & OBOE_INT_MASK))
      return IRQ_NONE;

/* Ack all the interrupts */
  OUTB (irqstat, OBOE_ISR);

  toshoboe_isntstuck (self);

/* Txdone */
  if (irqstat & OBOE_INT_TXDONE)
    {
      int txp, txpc;
      int i;

      txp = self->txpending;
      self->txpending = 0;

      for (i = 0; i < TX_SLOTS; ++i)
        {
          if (self->ring->tx[i].control & OBOE_CTL_TX_HW_OWNS)
              self->txpending++;
        }
      IRDA_DEBUG (1, "%s.txd(%x)%x/%x\n", __func__
          ,irqstat,txp,self->txpending);

      txp = INB (OBOE_TXSLOT) & OBOE_SLOT_MASK;

      /* Got anything queued ? start it together */
      if (self->ring->tx[txp].control & OBOE_CTL_TX_HW_OWNS)
        {
          txpc = txp;
#ifdef OPTIMIZE_TX
          while (self->ring->tx[txpc].control & OBOE_CTL_TX_HW_OWNS)
            {
              txp = txpc;
              txpc++;
              txpc %= TX_SLOTS;
              self->netdev->stats.tx_packets++;
              if (self->ring->tx[txpc].control & OBOE_CTL_TX_HW_OWNS)
                  self->ring->tx[txp].control &= ~OBOE_CTL_TX_RTCENTX;
            }
          self->netdev->stats.tx_packets--;
#else
          self->netdev->stats.tx_packets++;
#endif
          toshoboe_start_DMA(self, OBOE_CONFIG0H_ENTX);
        }

      if ((!self->txpending) && (self->new_speed))
        {
          self->speed = self->new_speed;
          IRDA_DEBUG (1, "%s: Executed TxDone scheduled speed change %d\n",
		      __func__, self->speed);
          toshoboe_setbaud (self);
        }

      /* Tell network layer that we want more frames */
      if (!self->new_speed)
          netif_wake_queue(self->netdev);
    }

  if (irqstat & OBOE_INT_RXDONE)
    {
      while (!(self->ring->rx[self->rxs].control & OBOE_CTL_RX_HW_OWNS))
        {
          int len = self->ring->rx[self->rxs].len;
          skb = NULL;
          IRDA_DEBUG (3, "%s.rcv:%x(%x)\n", __func__
		      ,len,self->ring->rx[self->rxs].control);

#ifdef DUMP_PACKETS
dumpbufs(self->rx_bufs[self->rxs],len,'<');
#endif

          if (self->ring->rx[self->rxs].control == 0)
            {
              __u8 enable = INB (OBOE_ENABLEH);

              /* In SIR mode we need to check the CRC as this */
              /* hasn't been done by the hardware */
              if (enable & OBOE_ENABLEH_SIRON)
                {
                  if (!toshoboe_checkfcs (self->rx_bufs[self->rxs], len))
                      len = 0;
                  /*Trim off the CRC */
                  if (len > 1)
                      len -= 2;
                  else
                      len = 0;
                  IRDA_DEBUG (1, "%s.SIR:%x(%x)\n", __func__, len,enable);
                }

#ifdef USE_MIR
              else if (enable & OBOE_ENABLEH_MIRON)
                {
                  if (len > 1)
                      len -= 2;
                  else
                      len = 0;
                  IRDA_DEBUG (2, "%s.MIR:%x(%x)\n", __func__, len,enable);
                }
#endif
              else if (enable & OBOE_ENABLEH_FIRON)
                {
                  if (len > 3)
                      len -= 4;   /*FIXME: check this */
                  else
                      len = 0;
                  IRDA_DEBUG (1, "%s.FIR:%x(%x)\n", __func__, len,enable);
                }
              else
                  IRDA_DEBUG (0, "%s.?IR:%x(%x)\n", __func__, len,enable);

              if (len)
                {
                  skb = dev_alloc_skb (len + 1);
                  if (skb)
                    {
                      skb_reserve (skb, 1);

                      skb_put (skb, len);
                      skb_copy_to_linear_data(skb, self->rx_bufs[self->rxs],
					      len);
                      self->netdev->stats.rx_packets++;
                      skb->dev = self->netdev;
                      skb_reset_mac_header(skb);
                      skb->protocol = htons (ETH_P_IRDA);
                    }
                  else
                    {
                      printk (KERN_INFO
                              "%s(), memory squeeze, dropping frame.\n",
			      __func__);
                    }
                }
            }
          else
            {
            /* TODO: =========================================== */
            /*  if OBOE_CTL_RX_LENGTH, our buffers are too small */
            /* (MIR or FIR) data is lost. */
            /* (SIR) data is splitted in several slots. */
            /* we have to join all the received buffers received */
            /*in a large buffer before checking CRC. */
            IRDA_DEBUG (0, "%s.err:%x(%x)\n", __func__
                ,len,self->ring->rx[self->rxs].control);
            }

          self->ring->rx[self->rxs].len = 0x0;
          self->ring->rx[self->rxs].control = OBOE_CTL_RX_HW_OWNS;

          self->rxs++;
          self->rxs %= RX_SLOTS;

          if (skb)
              netif_rx (skb);

        }
    }

  if (irqstat & OBOE_INT_TXUNDER)
    {
      printk (KERN_WARNING DRIVER_NAME ": tx fifo underflow\n");
    }
  if (irqstat & OBOE_INT_RXOVER)
    {
      printk (KERN_WARNING DRIVER_NAME ": rx fifo overflow\n");
    }
/* This must be useful for something... */
  if (irqstat & OBOE_INT_SIP)
    {
      self->int_sip++;
      IRDA_DEBUG (1, "%s.sip:%x(%x)%x\n", __func__
	      ,self->int_sip,irqstat,self->txpending);
    }
  return IRQ_HANDLED;
}


static int
toshoboe_net_open (struct net_device *dev)
{
  struct toshoboe_cb *self;
  unsigned long flags;
  int rc;

  IRDA_DEBUG (4, "%s()\n", __func__);

  self = netdev_priv(dev);

  if (self->async)
    return -EBUSY;

  if (self->stopped)
    return 0;

  rc = request_irq (self->io.irq, toshoboe_interrupt,
                    IRQF_SHARED | IRQF_DISABLED, dev->name, self);
  if (rc)
  	return rc;

  spin_lock_irqsave(&self->spinlock, flags);
  toshoboe_startchip (self);
  spin_unlock_irqrestore(&self->spinlock, flags);

  /* Ready to play! */
  netif_start_queue(dev);

  /*
   * Open new IrLAP layer instance, now that everything should be
   * initialized properly
   */
  self->irlap = irlap_open (dev, &self->qos, driver_name);

  self->irdad = 1;

  return 0;
}

static int
toshoboe_net_close (struct net_device *dev)
{
  struct toshoboe_cb *self;

  IRDA_DEBUG (4, "%s()\n", __func__);

  IRDA_ASSERT (dev != NULL, return -1; );
  self = netdev_priv(dev);

  /* Stop device */
  netif_stop_queue(dev);

  /* Stop and remove instance of IrLAP */
  if (self->irlap)
    irlap_close (self->irlap);
  self->irlap = NULL;

  self->irdad = 0;

  free_irq (self->io.irq, (void *) self);

  if (!self->stopped)
    {
      toshoboe_stopchip (self);
    }

  return 0;
}

/*
 * Function toshoboe_net_ioctl (dev, rq, cmd)
 *
 *    Process IOCTL commands for this device
 *
 */
static int
toshoboe_net_ioctl (struct net_device *dev, struct ifreq *rq, int cmd)
{
  struct if_irda_req *irq = (struct if_irda_req *) rq;
  struct toshoboe_cb *self;
  unsigned long flags;
  int ret = 0;

  IRDA_ASSERT (dev != NULL, return -1; );

  self = netdev_priv(dev);

  IRDA_ASSERT (self != NULL, return -1; );

  IRDA_DEBUG (5, "%s(), %s, (cmd=0x%X)\n", __func__, dev->name, cmd);

  /* Disable interrupts & save flags */
  spin_lock_irqsave(&self->spinlock, flags);

  switch (cmd)
    {
    case SIOCSBANDWIDTH:       /* Set bandwidth */
      /* This function will also be used by IrLAP to change the
       * speed, so we still must allow for speed change within
       * interrupt context.
       */
      IRDA_DEBUG (1, "%s(BANDWIDTH), %s, (%X/%ld\n", __func__
          ,dev->name, INB (OBOE_STATUS), irq->ifr_baudrate );
      if (!in_interrupt () && !capable (CAP_NET_ADMIN)) {
	ret = -EPERM;
	goto out;
      }

      /* self->speed=irq->ifr_baudrate; */
      /* toshoboe_setbaud(self); */
      /* Just change speed once - inserted by Paul Bristow */
      self->new_speed = irq->ifr_baudrate;
      break;
    case SIOCSMEDIABUSY:       /* Set media busy */
      IRDA_DEBUG (1, "%s(MEDIABUSY), %s, (%X/%x)\n", __func__
          ,dev->name, INB (OBOE_STATUS), capable (CAP_NET_ADMIN) );
      if (!capable (CAP_NET_ADMIN)) {
	ret = -EPERM;
	goto out;
      }
      irda_device_set_media_busy (self->netdev, TRUE);
      break;
    case SIOCGRECEIVING:       /* Check if we are receiving right now */
      irq->ifr_receiving = (INB (OBOE_STATUS) & OBOE_STATUS_RXBUSY) ? 1 : 0;
      IRDA_DEBUG (3, "%s(RECEIVING), %s, (%X/%x)\n", __func__
          ,dev->name, INB (OBOE_STATUS), irq->ifr_receiving );
      break;
    default:
      IRDA_DEBUG (1, "%s(?), %s, (cmd=0x%X)\n", __func__, dev->name, cmd);
      ret = -EOPNOTSUPP;
    }
out:
  spin_unlock_irqrestore(&self->spinlock, flags);
  return ret;

}

MODULE_DESCRIPTION("Toshiba OBOE IrDA Device Driver");
MODULE_AUTHOR("James McKenzie <james@fishsoup.dhs.org>");
MODULE_LICENSE("GPL");

module_param (max_baud, int, 0);
MODULE_PARM_DESC(max_baud, "Maximum baud rate");

#ifdef USE_PROBE
module_param (do_probe, bool, 0);
MODULE_PARM_DESC(do_probe, "Enable/disable chip probing and self-test");
#endif

static void
toshoboe_close (struct pci_dev *pci_dev)
{
  int i;
  struct toshoboe_cb *self = (struct toshoboe_cb*)pci_get_drvdata(pci_dev);

  IRDA_DEBUG (4, "%s()\n", __func__);

  IRDA_ASSERT (self != NULL, return; );

  if (!self->stopped)
    {
      toshoboe_stopchip (self);
    }

  release_region (self->io.fir_base, self->io.fir_ext);

  for (i = 0; i < TX_SLOTS; ++i)
    {
      kfree (self->tx_bufs[i]);
      self->tx_bufs[i] = NULL;
    }

  for (i = 0; i < RX_SLOTS; ++i)
    {
      kfree (self->rx_bufs[i]);
      self->rx_bufs[i] = NULL;
    }

  unregister_netdev(self->netdev);

  kfree (self->ringbuf);
  self->ringbuf = NULL;
  self->ring = NULL;

  free_netdev(self->netdev);
}

static const struct net_device_ops toshoboe_netdev_ops = {
	.ndo_open	= toshoboe_net_open,
	.ndo_stop	= toshoboe_net_close,
	.ndo_start_xmit	= toshoboe_hard_xmit,
	.ndo_do_ioctl	= toshoboe_net_ioctl,
};

static int
toshoboe_open (struct pci_dev *pci_dev, const struct pci_device_id *pdid)
{
  struct toshoboe_cb *self;
  struct net_device *dev;
  int i = 0;
  int ok = 0;
  int err;

  IRDA_DEBUG (4, "%s()\n", __func__);

  if ((err=pci_enable_device(pci_dev)))
    return err;

  dev = alloc_irdadev(sizeof (struct toshoboe_cb));
  if (dev == NULL)
    {
      printk (KERN_ERR DRIVER_NAME ": can't allocate memory for "
              "IrDA control block\n");
      return -ENOMEM;
    }

  self = netdev_priv(dev);
  self->netdev = dev;
  self->pdev = pci_dev;
  self->base = pci_resource_start(pci_dev,0);

  self->io.fir_base = self->base;
  self->io.fir_ext = OBOE_IO_EXTENT;
  self->io.irq = pci_dev->irq;
  self->io.irqflags = IRQF_SHARED | IRQF_DISABLED;

  self->speed = self->io.speed = 9600;
  self->async = 0;

  /* Lock the port that we need */
  if (NULL==request_region (self->io.fir_base, self->io.fir_ext, driver_name))
    {
      printk (KERN_ERR DRIVER_NAME ": can't get iobase of 0x%03x\n"
	      ,self->io.fir_base);
      err = -EBUSY;
      goto freeself;
    }

  spin_lock_init(&self->spinlock);

  irda_init_max_qos_capabilies (&self->qos);
  self->qos.baud_rate.bits = 0;

  if (max_baud >= 2400)
    self->qos.baud_rate.bits |= IR_2400;
  /*if (max_baud>=4800) idev->qos.baud_rate.bits|=IR_4800; */
  if (max_baud >= 9600)
    self->qos.baud_rate.bits |= IR_9600;
  if (max_baud >= 19200)
    self->qos.baud_rate.bits |= IR_19200;
  if (max_baud >= 115200)
    self->qos.baud_rate.bits |= IR_115200;
#ifdef USE_MIR
  if (max_baud >= 1152000)
    {
      self->qos.baud_rate.bits |= IR_1152000;
    }
#endif
  if (max_baud >= 4000000)
    {
      self->qos.baud_rate.bits |= (IR_4000000 << 8);
    }

  /*FIXME: work this out... */
  self->qos.min_turn_time.bits = 0xff;

  irda_qos_bits_to_value (&self->qos);

  /* Allocate twice the size to guarantee alignment */
  self->ringbuf = kmalloc(OBOE_RING_LEN << 1, GFP_KERNEL);
  if (!self->ringbuf)
    {
      printk (KERN_ERR DRIVER_NAME ": can't allocate DMA buffers\n");
      err = -ENOMEM;
      goto freeregion;
    }

#if (BITS_PER_LONG == 64)
#error broken on 64-bit:  casts pointer to 32-bit, and then back to pointer.
#endif

  /*We need to align the taskfile on a taskfile size boundary */
  {
    unsigned long addr;

    addr = (__u32) self->ringbuf;
    addr &= ~(OBOE_RING_LEN - 1);
    addr += OBOE_RING_LEN;
    self->ring = (struct OboeRing *) addr;
  }

  memset (self->ring, 0, OBOE_RING_LEN);
  self->io.mem_base = (__u32) self->ring;

  ok = 1;
  for (i = 0; i < TX_SLOTS; ++i)
    {
      self->tx_bufs[i] = kmalloc (TX_BUF_SZ, GFP_KERNEL);
      if (!self->tx_bufs[i])
        ok = 0;
    }

  for (i = 0; i < RX_SLOTS; ++i)
    {
      self->rx_bufs[i] = kmalloc (RX_BUF_SZ, GFP_KERNEL);
      if (!self->rx_bufs[i])
        ok = 0;
    }

  if (!ok)
    {
      printk (KERN_ERR DRIVER_NAME ": can't allocate rx/tx buffers\n");
      err = -ENOMEM;
      goto freebufs;
    }


#ifdef USE_PROBE
  if (do_probe)
    if (!toshoboe_probe (self))
      {
        err = -ENODEV;
        goto freebufs;
      }
#endif

  SET_NETDEV_DEV(dev, &pci_dev->dev);
  dev->netdev_ops = &toshoboe_netdev_ops;

  err = register_netdev(dev);
  if (err)
    {
      printk (KERN_ERR DRIVER_NAME ": register_netdev() failed\n");
      err = -ENOMEM;
      goto freebufs;
    }
  printk (KERN_INFO "IrDA: Registered device %s\n", dev->name);

  pci_set_drvdata(pci_dev,self);

  printk (KERN_INFO DRIVER_NAME ": Using multiple tasks\n");

  return 0;

freebufs:
  for (i = 0; i < TX_SLOTS; ++i)
    kfree (self->tx_bufs[i]);
  for (i = 0; i < RX_SLOTS; ++i)
    kfree (self->rx_bufs[i]);
  kfree(self->ringbuf);

freeregion:
  release_region (self->io.fir_base, self->io.fir_ext);

freeself:
  free_netdev(dev);

  return err;
}

static int
toshoboe_gotosleep (struct pci_dev *pci_dev, pm_message_t crap)
{
  struct toshoboe_cb *self = (struct toshoboe_cb*)pci_get_drvdata(pci_dev);
  unsigned long flags;
  int i = 10;

  IRDA_DEBUG (4, "%s()\n", __func__);

  if (!self || self->stopped)
    return 0;

  if ((!self->irdad) && (!self->async))
    return 0;

/* Flush all packets */
  while ((i--) && (self->txpending))
    udelay (10000);

  spin_lock_irqsave(&self->spinlock, flags);

  toshoboe_stopchip (self);
  self->stopped = 1;
  self->txpending = 0;

  spin_unlock_irqrestore(&self->spinlock, flags);
  return 0;
}

static int
toshoboe_wakeup (struct pci_dev *pci_dev)
{
  struct toshoboe_cb *self = (struct toshoboe_cb*)pci_get_drvdata(pci_dev);
  unsigned long flags;

  IRDA_DEBUG (4, "%s()\n", __func__);

  if (!self || !self->stopped)
    return 0;

  if ((!self->irdad) && (!self->async))
    return 0;

  spin_lock_irqsave(&self->spinlock, flags);

  toshoboe_startchip (self);
  self->stopped = 0;

  netif_wake_queue(self->netdev);
  spin_unlock_irqrestore(&self->spinlock, flags);
  return 0;
}

static struct pci_driver donauboe_pci_driver = {
	.name		= "donauboe",
	.id_table	= toshoboe_pci_tbl,
	.probe		= toshoboe_open,
	.remove		= toshoboe_close,
	.suspend	= toshoboe_gotosleep,
	.resume		= toshoboe_wakeup 
};

static int __init
donauboe_init (void)
{
  return pci_register_driver(&donauboe_pci_driver);
}

static void __exit
donauboe_cleanup (void)
{
  pci_unregister_driver(&donauboe_pci_driver);
}

module_init(donauboe_init);
module_exit(donauboe_cleanup);