summaryrefslogtreecommitdiffstats
path: root/drivers/net/irda/toshoboe.c
blob: d0dbaa4f726b6665ef2ffeaa2db5f52e795c72d9 (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
/*********************************************************************
 *                
 * Filename:      toshoboe.c
 * Version:       0.1
 * Description:   Driver for the Toshiba OBOE (or type-O or 700 or 701)
 *                FIR Chipset. 
 * Status:        Experimental.
 * Author:        James McKenzie <james@fishsoup.dhs.org>
 * Created at:    Sat May 8  12:35:27 1999
 * 
 *     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 100CT. and many more
 *     Toshiba refers to this chip as the type-O IR port.
 *
 ********************************************************************/

/* This driver is experimental, I have only three ir devices */
/* an olivetti notebook which doesn't have FIR, a toshiba libretto, and */
/* an hp printer, this works fine at 4MBPS with my HP printer */

static char *rcsid = "$Id: toshoboe.c,v 1.9 1999/06/29 14:21:06 root Exp $";

/* 
 * $Log: toshoboe.c,v $
 * Revision 1.9  1999/06/29 14:21:06  root
 * *** empty log message ***
 *
 * Revision 1.8  1999/06/29 14:15:08  root
 * *** empty log message ***
 *
 * Revision 1.7  1999/06/29 13:46:42  root
 * *** empty log message ***
 *
 * Revision 1.6  1999/06/29 12:31:03  root
 * *** empty log message ***
 *
 * Revision 1.5  1999/05/12 12:24:39  root
 * *** empty log message ***
 *
 * Revision 1.4  1999/05/12 11:55:08  root
 * *** empty log message ***
 *
 * Revision 1.3  1999/05/09 01:33:12  root
 * *** empty log message ***
 *
 * Revision 1.2  1999/05/09 01:30:38  root
 * *** empty log message ***
 *
 * Revision 1.1  1999/05/09 01:25:04  root
 * Initial revision
 * 
 */

/* Define this to have only one frame in the XMIT or RECV queue */
/* Toshiba's drivers do this, but it disables back to back tansfers */
/* I think that the chip may have some problems certainly, I have */
/* seen it jump over tasks in the taskfile->xmit with this turned on */
#define ONETASK

/* To adjust the number of tasks in use edit toshoboe.h */

/* Define this to enable FIR and MIR support */
#define ENABLE_FAST

/* Number of ports this driver can support, you also need to edit dev_self below */
#define NSELFS 4

/* Size of IO window */
#define CHIP_IO_EXTENT	0x1f

/* Transmit and receive buffer sizes, adjust at your peril */
#define RX_BUF_SZ 	4196
#define TX_BUF_SZ	4196

/* 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/malloc.h>
#include <linux/init.h>
#include <linux/pci.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>

#ifdef CONFIG_APM
#include <linux/apm_bios.h>
#endif

#include <net/irda/toshoboe.h>

static char *driver_name = "toshoboe";

static struct toshoboe_cb *dev_self[NSELFS + 1] =
{NULL, NULL, NULL, NULL, NULL};

static int max_baud = 4000000;

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

  outb_p (0x0e, OBOE_REG_11);

  outb_p (0x00, OBOE_RST);
  outb_p (0x3f, OBOE_TFP2);     /*Write the taskfile address */
  outb_p (0xff, OBOE_TFP1);
  outb_p (0xff, OBOE_TFP0);
  outb_p (0x0f, OBOE_REG_1B);
  outb_p (0xff, OBOE_REG_1A);
  outb_p (0x00, OBOE_ISR);      /*FIXME: should i do this to disbale ints */
  outb_p (0x80, OBOE_RST);
  outb_p (0xe, OBOE_LOCK);

}

/*Set the baud rate */
static void
toshoboe_setbaud (struct toshoboe_cb *self, int baud)
{
  unsigned long flags;
  DEBUG (4, __FUNCTION__ "()\n");

  printk (KERN_WARNING "ToshOboe: seting baud to %d\n", baud);

  save_flags (flags);
  cli ();
  switch (baud)
    {
    case 2400:
      outb_p (OBOE_PMDL_SIR, OBOE_PMDL);
      outb_p (OBOE_SMDL_SIR, OBOE_SMDL);
      outb_p (0xbf, OBOE_UDIV);
      break;
    case 4800:
      outb_p (OBOE_PMDL_SIR, OBOE_PMDL);
      outb_p (OBOE_SMDL_SIR, OBOE_SMDL);
      outb_p (0x5f, OBOE_UDIV);
      break;
    case 9600:
      outb_p (OBOE_PMDL_SIR, OBOE_PMDL);
      outb_p (OBOE_SMDL_SIR, OBOE_SMDL);
      outb_p (0x2f, OBOE_UDIV);
      break;
    case 19200:
      outb_p (OBOE_PMDL_SIR, OBOE_PMDL);
      outb_p (OBOE_SMDL_SIR, OBOE_SMDL);
      outb_p (0x17, OBOE_UDIV);
      break;
    case 38400:
      outb_p (OBOE_PMDL_SIR, OBOE_PMDL);
      outb_p (OBOE_SMDL_SIR, OBOE_SMDL);
      outb_p (0xb, OBOE_UDIV);
      break;
    case 57600:
      outb_p (OBOE_PMDL_SIR, OBOE_PMDL);
      outb_p (OBOE_SMDL_SIR, OBOE_SMDL);
      outb_p (0x7, OBOE_UDIV);
      break;
    case 115200:
      outb_p (OBOE_PMDL_SIR, OBOE_PMDL);
      outb_p (OBOE_SMDL_SIR, OBOE_SMDL);
      outb_p (0x3, OBOE_UDIV);
      break;
    case 1152000:
      outb_p (OBOE_PMDL_MIR, OBOE_PMDL);
      outb_p (OBOE_SMDL_MIR, OBOE_SMDL);
      outb_p (0x1, OBOE_UDIV);
      break;
    case 4000000:
      outb_p (OBOE_PMDL_FIR, OBOE_PMDL);
      outb_p (OBOE_SMDL_FIR, OBOE_SMDL);
      outb_p (0x0, OBOE_UDIV);
      break;
    }

  restore_flags (flags);

  outb_p (0x00, OBOE_RST);
  outb_p (0x80, OBOE_RST);
  outb_p (0x01, OBOE_REG_9);

}

/* Wake the chip up and get it looking at the taskfile */
static void
toshoboe_startchip (struct toshoboe_cb *self)
{
  __u32 physaddr;

  DEBUG (4, __FUNCTION__ "()\n");


  outb_p (0, OBOE_LOCK);
  outb_p (0, OBOE_RST);
  outb_p (OBOE_NTR_VAL, OBOE_NTR);
  outb_p (0xf0, OBOE_REG_D);
  outb_p (0xff, OBOE_ISR);
  outb_p (0x0f, OBOE_REG_1A);
  outb_p (0xff, OBOE_REG_1B);


  physaddr = virt_to_bus (self->taskfile);

  outb_p ((physaddr >> 0x0a) & 0xff, OBOE_TFP0);
  outb_p ((physaddr >> 0x12) & 0xff, OBOE_TFP1);
  outb_p ((physaddr >> 0x1a) & 0x3f, OBOE_TFP2);

  outb_p (0x0e, OBOE_REG_11);
  outb_p (0x80, OBOE_RST);

  toshoboe_setbaud (self, 9600);

}

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

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

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

}

/*setup the taskfile */
static void
toshoboe_initbuffs (struct toshoboe_cb *self)
{
  int i;
  unsigned long flags;

  DEBUG (4, __FUNCTION__ "()\n");

  save_flags (flags);
  cli ();

  for (i = 0; i < TX_SLOTS; ++i)
    {
      self->taskfile->xmit[i].len = 0;
      self->taskfile->xmit[i].control = 0x00;
      self->taskfile->xmit[i].buffer = virt_to_bus (self->xmit_bufs[i]);
    }

  for (i = 0; i < RX_SLOTS; ++i)
    {
      self->taskfile->recv[i].len = 0;
      self->taskfile->recv[i].control = 0x83;
      self->taskfile->recv[i].buffer = virt_to_bus (self->recv_bufs[i]);
    }

  restore_flags (flags);
}




/*Transmit something */
static int
toshoboe_hard_xmit (struct sk_buff *skb, struct net_device *dev)
{
  struct irda_device *idev;
  struct toshoboe_cb *self;
  int mtt, len;

  idev = (struct irda_device *) dev->priv;
  ASSERT (idev != NULL, return 0;
    );
  ASSERT (idev->magic == IRDA_DEVICE_MAGIC, return 0;
    );

  self = idev->priv;
  ASSERT (self != NULL, return 0;
    );

  if (self->stopped)
    return 0;

#ifdef ONETASK
  if (self->txpending)
    return -EBUSY;

  self->txs = inb_p (OBOE_XMTT) - OBOE_XMTT_OFFSET;

  self->txs &= 0x3f;

#endif

  if (self->taskfile->xmit[self->txs].control)
    return -EBUSY;


  if (inb_p (OBOE_RST) & OBOE_RST_WRAP)
    {
      len = async_wrap_skb (skb, self->xmit_bufs[self->txs], TX_BUF_SZ);
    }
  else
    {
      len = skb->len;
      memcpy (self->xmit_bufs[self->txs], skb->data, len);
    }
  self->taskfile->xmit[self->txs].len = len & 0x0fff;



  outb_p (0, OBOE_RST);
  outb_p (0x1e, OBOE_REG_11);

  self->taskfile->xmit[self->txs].control = 0x84;

  mtt = irda_get_mtt (skb);
  if (mtt)
    udelay (mtt);

  self->txpending++;

  /*FIXME: ask about tbusy,media_busy stuff, for the moment */
  /*tbusy means can't queue any more */
#ifndef ONETASK
  if (self->txpending == TX_SLOTS)
    {
#else
  {
#endif
    if (irda_lock ((void *) &dev->tbusy) == FALSE)
      return -EBUSY;
  }

  outb_p (0x80, OBOE_RST);
  outb_p (1, OBOE_REG_9);

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

  dev_kfree_skb (skb);

  return 0;
}

/*interrupt handler */
static void
toshoboe_interrupt (int irq, void *dev_id, struct pt_regs *regs)
{
  struct irda_device *idev = (struct irda_device *) dev_id;
  struct toshoboe_cb *self;
  __u8 irqstat;
  struct sk_buff *skb;

  if (idev == NULL)
    {
      printk (KERN_WARNING "%s: irq %d for unknown device.\n",
              driver_name, irq);
      return;
    }

  self = idev->priv;

  if (!self)
    return;

  DEBUG (4, __FUNCTION__ "()\n");

  irqstat = inb_p (OBOE_ISR);

/* woz it us */
  if (!(irqstat & 0xf8))
    return;

  outb_p (irqstat, OBOE_ISR);   /*Acknologede it */


/* Txdone */
  if (irqstat & OBOE_ISR_TXDONE)
    {
      self->txpending--;

      idev->stats.tx_packets++;

      idev->media_busy = FALSE;
      idev->netdev.tbusy = 0;

      mark_bh (NET_BH);
    }

  if (irqstat & OBOE_ISR_RXDONE)
    {

#ifdef ONETASK
      self->rxs = inb_p (OBOE_RCVT);
      self->rxs += (RX_SLOTS - 1);
      self->rxs %= RX_SLOTS;
#else
      while (self->taskfile->recv[self->rxs].control == 0)
#endif
        {
          int len = self->taskfile->recv[self->rxs].len;

          if (len > 2)
            len -= 2;

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

              skb_put (skb, len);
              memcpy (skb->data, self->recv_bufs[self->rxs], len);

              idev->stats.rx_packets++;
              skb->dev = &idev->netdev;
              skb->mac.raw = skb->data;
              skb->protocol = htons (ETH_P_IRDA);
            }
          else
            {
              printk (KERN_INFO __FUNCTION__
                      "(), memory squeeze, dropping frame.\n");
            }



          self->taskfile->recv[self->rxs].control = 0x83;
          self->taskfile->recv[self->rxs].len = 0x0;

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

          if (skb)
            netif_rx (skb);

        }

    }

  if (irqstat & OBOE_ISR_20)
    {
      printk (KERN_WARNING "Oboe_irq: 20\n");
    }
  if (irqstat & OBOE_ISR_10)
    {
      printk (KERN_WARNING "Oboe_irq: 10\n");
    }
  if (irqstat & 0x8)
    {
      /*FIXME: I think this is a TX or RX error of some sort */

      idev->stats.tx_errors++;
      idev->stats.rx_errors++;

    }


}



/* Change the baud rate */
static void
toshoboe_change_speed (struct irda_device *idev, __u32 speed)
{
  struct toshoboe_cb *self;
  DEBUG (4, __FUNCTION__ "()\n");

  ASSERT (idev != NULL, return;
    );
  ASSERT (idev->magic == IRDA_DEVICE_MAGIC, return;
    );

  self = idev->priv;
  ASSERT (self != NULL, return;
    );


  idev->io.baudrate = speed;

  if (self->stopped)
    return;

  toshoboe_setbaud (self, speed);

}


/* Check all xmit_tasks finished */
static void
toshoboe_wait_until_sent (struct irda_device *idev)
{
  struct toshoboe_cb *self;
  int i;

  DEBUG (4, __FUNCTION__ "()\n");

  ASSERT (idev != NULL, return;
    );
  ASSERT (idev->magic == IRDA_DEVICE_MAGIC, return;
    );

  self = idev->priv;
  ASSERT (self != NULL, return;
    );

  if (self->stopped)
    return;

  for (i = 0; i < TX_SLOTS; ++i)
    {
      while (self->taskfile->xmit[i].control)
        {
          current->state = TASK_INTERRUPTIBLE;
          schedule_timeout (MSECS_TO_JIFFIES(60));
        }
    }

}

static int
toshoboe_is_receiving (struct irda_device *idev)
{
  DEBUG (4, __FUNCTION__ "()\n");

/*FIXME Can't tell! */
  return (FALSE);
}


static int
toshoboe_net_init (struct net_device *dev)
{
  DEBUG (4, __FUNCTION__ "()\n");

  /* Setup to be a normal IrDA network device driver */
  irda_device_setup (dev);

  /* Insert overrides below this line! */
  return 0;
}


static void 
toshoboe_initptrs (struct toshoboe_cb *self)
{

  unsigned long flags;
  save_flags (flags);
  cli ();

  /*FIXME: need to test this carefully to check which one */
  /*of the two possible startup logics the chip uses */
  /*although it won't make any difference if no-one xmits durining init */
  /*and none what soever if using ONETASK */

  self->rxs = inb_p (OBOE_RCVT);
  self->txs = inb_p (OBOE_XMTT) - OBOE_XMTT_OFFSET;

#if 0
  self->rxs = 0;
  self->txs = 0;
#endif
#if 0
  self->rxs = RX_SLOTS - 1;
  self->txs = 0;
#endif


  self->txpending = 0;

  restore_flags (flags);

}


static int
toshoboe_net_open (struct net_device *dev)
{
  struct irda_device *idev;
  struct toshoboe_cb *self;

  DEBUG (4, __FUNCTION__ "()\n");

  ASSERT (dev != NULL, return -1;
    );
  idev = (struct irda_device *) dev->priv;

  ASSERT (idev != NULL, return 0;
    );
  ASSERT (idev->magic == IRDA_DEVICE_MAGIC, return 0;
    );

  self = idev->priv;
  ASSERT (self != NULL, return 0;
    );

  if (self->stopped)
    return 0;


  if (request_irq (idev->io.irq, toshoboe_interrupt,
                   SA_SHIRQ | SA_INTERRUPT, idev->name, (void *) idev))
    {

      return -EAGAIN;
    }

  toshoboe_initbuffs (self);
  toshoboe_enablebm (self);
  toshoboe_startchip (self);
  toshoboe_initptrs (self);

  irda_device_net_open(dev);
	
  self->open = 1;
	
  MOD_INC_USE_COUNT;

  return 0;

}

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

  DEBUG (4, __FUNCTION__ "()\n");

  ASSERT (dev != NULL, return -1;
    );
  idev = (struct irda_device *) dev->priv;

  ASSERT (idev != NULL, return 0;
    );
  ASSERT (idev->magic == IRDA_DEVICE_MAGIC, return 0;
    );

  irda_device_net_close(dev);

  self = idev->priv;

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

  self->open = 0;

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

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

  MOD_DEC_USE_COUNT;

  return 0;

}



#ifdef MODULE

MODULE_PARM (max_baud, "i");

static int
toshoboe_close (struct irda_device *idev)
{
  struct toshoboe_cb *self;
  int i;

  DEBUG (4, __FUNCTION__ "()\n");

  ASSERT (idev != NULL, return -1;
    );
  ASSERT (idev->magic == IRDA_DEVICE_MAGIC, return -1;
    );

  self = idev->priv;

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

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

  release_region (idev->io.iobase, idev->io.io_ext);


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

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


  kfree (self->taskfilebuf);
  self->taskfilebuf = NULL;
  self->taskfile = NULL;


  irda_device_close (idev);

  return (0);

}

#endif



static int
toshoboe_open (struct pci_dev *pci_dev)
{
  struct toshoboe_cb *self;
  struct irda_device *idev;
  int i = 0;
  int ok = 0;


  DEBUG (4, __FUNCTION__ "()\n");

  while (dev_self[i])
    i++;

  if (i == NSELFS)
    {
      printk (KERN_ERR "Oboe: No more instances available");
      return -ENOMEM;
    }

  self = kmalloc (sizeof (struct toshoboe_cb), GFP_KERNEL);

  if (self == NULL)
    {
      printk (KERN_ERR "IrDA: Can't allocate memory for "
              "IrDA control block!\n");
      return -ENOMEM;
    }

  memset (self, 0, sizeof (struct toshoboe_cb));

  dev_self[i] = self;           /*This needs moving if we ever get more than one chip */

  self->open = 0;
  self->stopped = 0;
  self->pdev = pci_dev;
  self->base = pci_dev->resource[0].start;

  idev = &self->idev;

  /*Setup idev */

  idev->io.iobase = self->base;
  idev->io.irq = pci_dev->irq;
  idev->io.io_ext = CHIP_IO_EXTENT;
  idev->io.baudrate = 9600;

  /* Lock the port that we need */
  i = check_region (idev->io.iobase, idev->io.io_ext);
  if (i < 0)
    {
      DEBUG (0, __FUNCTION__ "(), can't get iobase of 0x%03x\n",
             idev->io.iobase);

      dev_self[i] = NULL;
      kfree (self);

      return -ENODEV;
    }


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

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


  idev->qos.min_turn_time.bits = 0xff;  /*FIXME: what does this do? */

  irda_qos_bits_to_value (&idev->qos);

  idev->flags = IFF_SIR | IFF_DMA | IFF_PIO;

#ifdef ENABLE_FAST
  if (max_baud >= 576000)
    idev->flags |= IFF_FIR;
#endif

  /* These aren't much use as we need to have a whole panoply of
   * buffers running */

  idev->rx_buff.flags = 0;
  idev->tx_buff.flags = 0;
  idev->rx_buff.truesize = 0;
  idev->rx_buff.truesize = 0;

  idev->change_speed = toshoboe_change_speed;
  idev->wait_until_sent = toshoboe_wait_until_sent;
  idev->is_receiving = toshoboe_is_receiving;

  idev->netdev.init = toshoboe_net_init;
  idev->netdev.hard_start_xmit = toshoboe_hard_xmit;
  idev->netdev.open = toshoboe_net_open;
  idev->netdev.stop = toshoboe_net_close;


  /* Now setup the endless buffers we need */

  self->txs = 0;
  self->rxs = 0;

  self->taskfilebuf = kmalloc (OBOE_TASK_BUF_LEN, GFP_KERNEL);
  if (!self->taskfilebuf)
    {
      printk (KERN_ERR "toshoboe: kmalloc for DMA failed()\n");
      kfree (self);
      return -ENOMEM;
    }


  memset (self->taskfilebuf, 0, OBOE_TASK_BUF_LEN);

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

    addr = (__u32) self->taskfilebuf;
    addr &= ~(sizeof (struct OboeTaskFile) - 1);
    addr += sizeof (struct OboeTaskFile);

    self->taskfile = (struct OboeTaskFile *) addr;
  }

  for (i = 0; i < TX_SLOTS; ++i)
    {
      self->xmit_bufs[i] = kmalloc (TX_BUF_SZ, GFP_KERNEL | GFP_DMA);
      if (self->xmit_bufs[i])
        ok++;
    }

  for (i = 0; i < RX_SLOTS; ++i)
    {
      self->recv_bufs[i] = kmalloc (TX_BUF_SZ, GFP_KERNEL | GFP_DMA);
      if (self->recv_bufs[i])
        ok++;
    }

  if (ok != RX_SLOTS + TX_SLOTS)
    {
      printk (KERN_ERR "toshoboe: kmalloc for buffers failed()\n");


      for (i = 0; i < TX_SLOTS; ++i)
        if (self->xmit_bufs[i])
          kfree (self->xmit_bufs[i]);
      for (i = 0; i < RX_SLOTS; ++i)
        if (self->recv_bufs[i])
          kfree (self->recv_bufs[i]);

      kfree (self);
      return -ENOMEM;

    }

  request_region (idev->io.iobase, idev->io.io_ext, driver_name);

  irda_device_open (idev, driver_name, self);

  printk (KERN_WARNING "ToshOboe: Using ");
#ifdef ONETASK
  printk ("single");
#else
  printk ("multiple");
#endif
  printk (" tasks, version %s\n", rcsid);

  return (0);
}

#ifdef CONFIG_APM
static void 
toshoboe_gotosleep (struct toshoboe_cb *self)
{
  int i = 10;

  printk (KERN_WARNING "ToshOboe: suspending\n");

  if (self->stopped)
    return;

  self->stopped = 1;

  if (!self->open)
    return;

/*FIXME: can't sleep here wait one second */

  while ((i--) && (self->txpending))
    udelay (100000);

  toshoboe_stopchip (self);
  toshoboe_disablebm (self);

  self->txpending = 0;

}


static void 
toshoboe_wakeup (struct toshoboe_cb *self)
{
  struct irda_device *idev = &self->idev;
  struct net_device *dev = &idev->netdev;
  unsigned long flags;

  if (!self->stopped)
    return;

  if (!self->open)
    {
      self->stopped = 0;
      return;
    }

  save_flags (flags);
  cli ();

  toshoboe_initbuffs (self);
  toshoboe_enablebm (self);
  toshoboe_startchip (self);

  toshoboe_setbaud (self, idev->io.baudrate);

  toshoboe_initptrs (self);



  dev->tbusy = 0;
  dev->interrupt = 0;
  dev->start = 1;
  self->stopped = 0;

  restore_flags (flags);
  printk (KERN_WARNING "ToshOboe: waking up\n");

}

static int 
toshoboe_apmproc (apm_event_t event)
{
  static int down = 0;          /*Filter out double events */
  int i;

  switch (event)
    {
    case APM_SYS_SUSPEND:
    case APM_USER_SUSPEND:
      if (!down)
        {

          for (i = 0; i < 4; i++)
            {
              if (dev_self[i])
                toshoboe_gotosleep (dev_self[i]);
            }

        }
      down = 1;
      break;
    case APM_NORMAL_RESUME:
    case APM_CRITICAL_RESUME:
      if (down)
        {



          for (i = 0; i < 4; i++)
            {
              if (dev_self[i])
                toshoboe_wakeup (dev_self[i]);
            }



        }
      down = 0;
      break;
    }
  return 0;
}


#endif

int __init toshoboe_init (void)
{
  struct pci_dev *pci_dev = NULL;
  int found = 0;

  do
    {
      pci_dev = pci_find_device (PCI_VENDOR_ID_TOSHIBA,
                                 PCI_DEVICE_ID_FIR701, pci_dev);
      if (pci_dev)
        {
          printk (KERN_WARNING "ToshOboe: Found 701 chip at 0x%0lx irq %d\n",
                  pci_dev->resource[0].start,
                  pci_dev->irq);

          if (!toshoboe_open (pci_dev))
            found++;
        }

    }
  while (pci_dev);


  if (found)
    {
#ifdef CONFIG_APM
      apm_register_callback (toshoboe_apmproc);
#endif
      return 0;
    }

  return -ENODEV;
}

#ifdef MODULE

static void
toshoboe_cleanup (void)
{
  int i;

  DEBUG (4, __FUNCTION__ "()\n");

  for (i = 0; i < 4; i++)
    {
      if (dev_self[i])
        toshoboe_close (&(dev_self[i]->idev));
    }

#ifdef CONFIG_APM
  apm_unregister_callback (toshoboe_apmproc);
#endif

}



int
init_module (void)
{
  return toshoboe_init ();
}


void
cleanup_module (void)
{
  toshoboe_cleanup ();
}


#endif