Merge branch 'linus'
[linux-2.6] / drivers / net / irda / nsc-ircc.c
1 /*********************************************************************
2  *                
3  * Filename:      nsc-ircc.c
4  * Version:       1.0
5  * Description:   Driver for the NSC PC'108 and PC'338 IrDA chipsets
6  * Status:        Stable.
7  * Author:        Dag Brattli <dagb@cs.uit.no>
8  * Created at:    Sat Nov  7 21:43:15 1998
9  * Modified at:   Wed Mar  1 11:29:34 2000
10  * Modified by:   Dag Brattli <dagb@cs.uit.no>
11  * 
12  *     Copyright (c) 1998-2000 Dag Brattli <dagb@cs.uit.no>
13  *     Copyright (c) 1998 Lichen Wang, <lwang@actisys.com>
14  *     Copyright (c) 1998 Actisys Corp., www.actisys.com
15  *     All Rights Reserved
16  *      
17  *     This program is free software; you can redistribute it and/or 
18  *     modify it under the terms of the GNU General Public License as 
19  *     published by the Free Software Foundation; either version 2 of 
20  *     the License, or (at your option) any later version.
21  *  
22  *     Neither Dag Brattli nor University of Tromsø admit liability nor
23  *     provide warranty for any of this software. This material is 
24  *     provided "AS-IS" and at no charge.
25  *
26  *     Notice that all functions that needs to access the chip in _any_
27  *     way, must save BSR register on entry, and restore it on exit. 
28  *     It is _very_ important to follow this policy!
29  *
30  *         __u8 bank;
31  *     
32  *         bank = inb(iobase+BSR);
33  *  
34  *         do_your_stuff_here();
35  *
36  *         outb(bank, iobase+BSR);
37  *
38  *    If you find bugs in this file, its very likely that the same bug
39  *    will also be in w83977af_ir.c since the implementations are quite
40  *    similar.
41  *     
42  ********************************************************************/
43
44 #include <linux/module.h>
45
46 #include <linux/kernel.h>
47 #include <linux/types.h>
48 #include <linux/skbuff.h>
49 #include <linux/netdevice.h>
50 #include <linux/ioport.h>
51 #include <linux/delay.h>
52 #include <linux/slab.h>
53 #include <linux/init.h>
54 #include <linux/rtnetlink.h>
55 #include <linux/dma-mapping.h>
56
57 #include <asm/io.h>
58 #include <asm/dma.h>
59 #include <asm/byteorder.h>
60
61 #include <linux/pm.h>
62 #include <linux/pm_legacy.h>
63
64 #include <net/irda/wrapper.h>
65 #include <net/irda/irda.h>
66 #include <net/irda/irda_device.h>
67
68 #include "nsc-ircc.h"
69
70 #define CHIP_IO_EXTENT 8
71 #define BROKEN_DONGLE_ID
72
73 static char *driver_name = "nsc-ircc";
74
75 /* Module parameters */
76 static int qos_mtt_bits = 0x07;  /* 1 ms or more */
77 static int dongle_id;
78
79 /* Use BIOS settions by default, but user may supply module parameters */
80 static unsigned int io[]  = { ~0, ~0, ~0, ~0 };
81 static unsigned int irq[] = { 0, 0, 0, 0, 0 };
82 static unsigned int dma[] = { 0, 0, 0, 0, 0 };
83
84 static int nsc_ircc_probe_108(nsc_chip_t *chip, chipio_t *info);
85 static int nsc_ircc_probe_338(nsc_chip_t *chip, chipio_t *info);
86 static int nsc_ircc_probe_39x(nsc_chip_t *chip, chipio_t *info);
87 static int nsc_ircc_init_108(nsc_chip_t *chip, chipio_t *info);
88 static int nsc_ircc_init_338(nsc_chip_t *chip, chipio_t *info);
89 static int nsc_ircc_init_39x(nsc_chip_t *chip, chipio_t *info);
90
91 /* These are the known NSC chips */
92 static nsc_chip_t chips[] = {
93 /*  Name, {cfg registers}, chip id index reg, chip id expected value, revision mask */
94         { "PC87108", { 0x150, 0x398, 0xea }, 0x05, 0x10, 0xf0, 
95           nsc_ircc_probe_108, nsc_ircc_init_108 },
96         { "PC87338", { 0x398, 0x15c, 0x2e }, 0x08, 0xb0, 0xf8, 
97           nsc_ircc_probe_338, nsc_ircc_init_338 },
98         /* Contributed by Steffen Pingel - IBM X40 */
99         { "PC8738x", { 0x164e, 0x4e, 0x0 }, 0x20, 0xf4, 0xff,
100           nsc_ircc_probe_39x, nsc_ircc_init_39x },
101         /* Contributed by Jan Frey - IBM A30/A31 */
102         { "PC8739x", { 0x2e, 0x4e, 0x0 }, 0x20, 0xea, 0xff, 
103           nsc_ircc_probe_39x, nsc_ircc_init_39x },
104         { NULL }
105 };
106
107 /* Max 4 instances for now */
108 static struct nsc_ircc_cb *dev_self[] = { NULL, NULL, NULL, NULL };
109
110 static char *dongle_types[] = {
111         "Differential serial interface",
112         "Differential serial interface",
113         "Reserved",
114         "Reserved",
115         "Sharp RY5HD01",
116         "Reserved",
117         "Single-ended serial interface",
118         "Consumer-IR only",
119         "HP HSDL-2300, HP HSDL-3600/HSDL-3610",
120         "IBM31T1100 or Temic TFDS6000/TFDS6500",
121         "Reserved",
122         "Reserved",
123         "HP HSDL-1100/HSDL-2100",
124         "HP HSDL-1100/HSDL-2100",
125         "Supports SIR Mode only",
126         "No dongle connected",
127 };
128
129 /* Some prototypes */
130 static int  nsc_ircc_open(int i, chipio_t *info);
131 static int  nsc_ircc_close(struct nsc_ircc_cb *self);
132 static int  nsc_ircc_setup(chipio_t *info);
133 static void nsc_ircc_pio_receive(struct nsc_ircc_cb *self);
134 static int  nsc_ircc_dma_receive(struct nsc_ircc_cb *self); 
135 static int  nsc_ircc_dma_receive_complete(struct nsc_ircc_cb *self, int iobase);
136 static int  nsc_ircc_hard_xmit_sir(struct sk_buff *skb, struct net_device *dev);
137 static int  nsc_ircc_hard_xmit_fir(struct sk_buff *skb, struct net_device *dev);
138 static int  nsc_ircc_pio_write(int iobase, __u8 *buf, int len, int fifo_size);
139 static void nsc_ircc_dma_xmit(struct nsc_ircc_cb *self, int iobase);
140 static __u8 nsc_ircc_change_speed(struct nsc_ircc_cb *self, __u32 baud);
141 static int  nsc_ircc_is_receiving(struct nsc_ircc_cb *self);
142 static int  nsc_ircc_read_dongle_id (int iobase);
143 static void nsc_ircc_init_dongle_interface (int iobase, int dongle_id);
144
145 static int  nsc_ircc_net_open(struct net_device *dev);
146 static int  nsc_ircc_net_close(struct net_device *dev);
147 static int  nsc_ircc_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
148 static struct net_device_stats *nsc_ircc_net_get_stats(struct net_device *dev);
149 static int nsc_ircc_pmproc(struct pm_dev *dev, pm_request_t rqst, void *data);
150
151 /*
152  * Function nsc_ircc_init ()
153  *
154  *    Initialize chip. Just try to find out how many chips we are dealing with
155  *    and where they are
156  */
157 static int __init nsc_ircc_init(void)
158 {
159         chipio_t info;
160         nsc_chip_t *chip;
161         int ret = -ENODEV;
162         int cfg_base;
163         int cfg, id;
164         int reg;
165         int i = 0;
166
167         /* Probe for all the NSC chipsets we know about */
168         for (chip=chips; chip->name ; chip++) {
169                 IRDA_DEBUG(2, "%s(), Probing for %s ...\n", __FUNCTION__,
170                            chip->name);
171                 
172                 /* Try all config registers for this chip */
173                 for (cfg=0; cfg<3; cfg++) {
174                         cfg_base = chip->cfg[cfg];
175                         if (!cfg_base)
176                                 continue;
177                         
178                         memset(&info, 0, sizeof(chipio_t));
179                         info.cfg_base = cfg_base;
180                         info.fir_base = io[i];
181                         info.dma = dma[i];
182                         info.irq = irq[i];
183
184                         /* Read index register */
185                         reg = inb(cfg_base);
186                         if (reg == 0xff) {
187                                 IRDA_DEBUG(2, "%s() no chip at 0x%03x\n", __FUNCTION__, cfg_base);
188                                 continue;
189                         }
190                         
191                         /* Read chip identification register */
192                         outb(chip->cid_index, cfg_base);
193                         id = inb(cfg_base+1);
194                         if ((id & chip->cid_mask) == chip->cid_value) {
195                                 IRDA_DEBUG(2, "%s() Found %s chip, revision=%d\n",
196                                            __FUNCTION__, chip->name, id & ~chip->cid_mask);
197                                 /* 
198                                  * If the user supplies the base address, then
199                                  * we init the chip, if not we probe the values
200                                  * set by the BIOS
201                                  */                             
202                                 if (io[i] < 0x2000) {
203                                         chip->init(chip, &info);
204                                 } else
205                                         chip->probe(chip, &info);
206
207                                 if (nsc_ircc_open(i, &info) == 0)
208                                         ret = 0;
209                                 i++;
210                         } else {
211                                 IRDA_DEBUG(2, "%s(), Wrong chip id=0x%02x\n", __FUNCTION__, id);
212                         }
213                 } 
214                 
215         }
216
217         return ret;
218 }
219
220 /*
221  * Function nsc_ircc_cleanup ()
222  *
223  *    Close all configured chips
224  *
225  */
226 static void __exit nsc_ircc_cleanup(void)
227 {
228         int i;
229
230         pm_unregister_all(nsc_ircc_pmproc);
231
232         for (i=0; i < 4; i++) {
233                 if (dev_self[i])
234                         nsc_ircc_close(dev_self[i]);
235         }
236 }
237
238 /*
239  * Function nsc_ircc_open (iobase, irq)
240  *
241  *    Open driver instance
242  *
243  */
244 static int __init nsc_ircc_open(int i, chipio_t *info)
245 {
246         struct net_device *dev;
247         struct nsc_ircc_cb *self;
248         struct pm_dev *pmdev;
249         void *ret;
250         int err;
251
252         IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
253
254         IRDA_MESSAGE("%s, Found chip at base=0x%03x\n", driver_name,
255                      info->cfg_base);
256
257         if ((nsc_ircc_setup(info)) == -1)
258                 return -1;
259
260         IRDA_MESSAGE("%s, driver loaded (Dag Brattli)\n", driver_name);
261
262         dev = alloc_irdadev(sizeof(struct nsc_ircc_cb));
263         if (dev == NULL) {
264                 IRDA_ERROR("%s(), can't allocate memory for "
265                            "control block!\n", __FUNCTION__);
266                 return -ENOMEM;
267         }
268
269         self = dev->priv;
270         self->netdev = dev;
271         spin_lock_init(&self->lock);
272    
273         /* Need to store self somewhere */
274         dev_self[i] = self;
275         self->index = i;
276
277         /* Initialize IO */
278         self->io.cfg_base  = info->cfg_base;
279         self->io.fir_base  = info->fir_base;
280         self->io.irq       = info->irq;
281         self->io.fir_ext   = CHIP_IO_EXTENT;
282         self->io.dma       = info->dma;
283         self->io.fifo_size = 32;
284         
285         /* Reserve the ioports that we need */
286         ret = request_region(self->io.fir_base, self->io.fir_ext, driver_name);
287         if (!ret) {
288                 IRDA_WARNING("%s(), can't get iobase of 0x%03x\n",
289                              __FUNCTION__, self->io.fir_base);
290                 err = -ENODEV;
291                 goto out1;
292         }
293
294         /* Initialize QoS for this device */
295         irda_init_max_qos_capabilies(&self->qos);
296         
297         /* The only value we must override it the baudrate */
298         self->qos.baud_rate.bits = IR_9600|IR_19200|IR_38400|IR_57600|
299                 IR_115200|IR_576000|IR_1152000 |(IR_4000000 << 8);
300         
301         self->qos.min_turn_time.bits = qos_mtt_bits;
302         irda_qos_bits_to_value(&self->qos);
303         
304         /* Max DMA buffer size needed = (data_size + 6) * (window_size) + 6; */
305         self->rx_buff.truesize = 14384; 
306         self->tx_buff.truesize = 14384;
307
308         /* Allocate memory if needed */
309         self->rx_buff.head =
310                 dma_alloc_coherent(NULL, self->rx_buff.truesize,
311                                    &self->rx_buff_dma, GFP_KERNEL);
312         if (self->rx_buff.head == NULL) {
313                 err = -ENOMEM;
314                 goto out2;
315
316         }
317         memset(self->rx_buff.head, 0, self->rx_buff.truesize);
318         
319         self->tx_buff.head =
320                 dma_alloc_coherent(NULL, self->tx_buff.truesize,
321                                    &self->tx_buff_dma, GFP_KERNEL);
322         if (self->tx_buff.head == NULL) {
323                 err = -ENOMEM;
324                 goto out3;
325         }
326         memset(self->tx_buff.head, 0, self->tx_buff.truesize);
327
328         self->rx_buff.in_frame = FALSE;
329         self->rx_buff.state = OUTSIDE_FRAME;
330         self->tx_buff.data = self->tx_buff.head;
331         self->rx_buff.data = self->rx_buff.head;
332         
333         /* Reset Tx queue info */
334         self->tx_fifo.len = self->tx_fifo.ptr = self->tx_fifo.free = 0;
335         self->tx_fifo.tail = self->tx_buff.head;
336
337         /* Override the network functions we need to use */
338         SET_MODULE_OWNER(dev);
339         dev->hard_start_xmit = nsc_ircc_hard_xmit_sir;
340         dev->open            = nsc_ircc_net_open;
341         dev->stop            = nsc_ircc_net_close;
342         dev->do_ioctl        = nsc_ircc_net_ioctl;
343         dev->get_stats       = nsc_ircc_net_get_stats;
344
345         err = register_netdev(dev);
346         if (err) {
347                 IRDA_ERROR("%s(), register_netdev() failed!\n", __FUNCTION__);
348                 goto out4;
349         }
350         IRDA_MESSAGE("IrDA: Registered device %s\n", dev->name);
351
352         /* Check if user has supplied a valid dongle id or not */
353         if ((dongle_id <= 0) ||
354             (dongle_id >= (sizeof(dongle_types) / sizeof(dongle_types[0]))) ) {
355                 dongle_id = nsc_ircc_read_dongle_id(self->io.fir_base);
356                 
357                 IRDA_MESSAGE("%s, Found dongle: %s\n", driver_name,
358                              dongle_types[dongle_id]);
359         } else {
360                 IRDA_MESSAGE("%s, Using dongle: %s\n", driver_name,
361                              dongle_types[dongle_id]);
362         }
363         
364         self->io.dongle_id = dongle_id;
365         nsc_ircc_init_dongle_interface(self->io.fir_base, dongle_id);
366
367         pmdev = pm_register(PM_SYS_DEV, PM_SYS_IRDA, nsc_ircc_pmproc);
368         if (pmdev)
369                 pmdev->data = self;
370
371         return 0;
372  out4:
373         dma_free_coherent(NULL, self->tx_buff.truesize,
374                           self->tx_buff.head, self->tx_buff_dma);
375  out3:
376         dma_free_coherent(NULL, self->rx_buff.truesize,
377                           self->rx_buff.head, self->rx_buff_dma);
378  out2:
379         release_region(self->io.fir_base, self->io.fir_ext);
380  out1:
381         free_netdev(dev);
382         dev_self[i] = NULL;
383         return err;
384 }
385
386 /*
387  * Function nsc_ircc_close (self)
388  *
389  *    Close driver instance
390  *
391  */
392 static int __exit nsc_ircc_close(struct nsc_ircc_cb *self)
393 {
394         int iobase;
395
396         IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
397
398         IRDA_ASSERT(self != NULL, return -1;);
399
400         iobase = self->io.fir_base;
401
402         /* Remove netdevice */
403         unregister_netdev(self->netdev);
404
405         /* Release the PORT that this driver is using */
406         IRDA_DEBUG(4, "%s(), Releasing Region %03x\n", 
407                    __FUNCTION__, self->io.fir_base);
408         release_region(self->io.fir_base, self->io.fir_ext);
409
410         if (self->tx_buff.head)
411                 dma_free_coherent(NULL, self->tx_buff.truesize,
412                                   self->tx_buff.head, self->tx_buff_dma);
413         
414         if (self->rx_buff.head)
415                 dma_free_coherent(NULL, self->rx_buff.truesize,
416                                   self->rx_buff.head, self->rx_buff_dma);
417
418         dev_self[self->index] = NULL;
419         free_netdev(self->netdev);
420         
421         return 0;
422 }
423
424 /*
425  * Function nsc_ircc_init_108 (iobase, cfg_base, irq, dma)
426  *
427  *    Initialize the NSC '108 chip
428  *
429  */
430 static int nsc_ircc_init_108(nsc_chip_t *chip, chipio_t *info)
431 {
432         int cfg_base = info->cfg_base;
433         __u8 temp=0;
434
435         outb(2, cfg_base);      /* Mode Control Register (MCTL) */
436         outb(0x00, cfg_base+1); /* Disable device */
437         
438         /* Base Address and Interrupt Control Register (BAIC) */
439         outb(CFG_108_BAIC, cfg_base);
440         switch (info->fir_base) {
441         case 0x3e8: outb(0x14, cfg_base+1); break;
442         case 0x2e8: outb(0x15, cfg_base+1); break;
443         case 0x3f8: outb(0x16, cfg_base+1); break;
444         case 0x2f8: outb(0x17, cfg_base+1); break;
445         default: IRDA_ERROR("%s(), invalid base_address", __FUNCTION__);
446         }
447         
448         /* Control Signal Routing Register (CSRT) */
449         switch (info->irq) {
450         case 3:  temp = 0x01; break;
451         case 4:  temp = 0x02; break;
452         case 5:  temp = 0x03; break;
453         case 7:  temp = 0x04; break;
454         case 9:  temp = 0x05; break;
455         case 11: temp = 0x06; break;
456         case 15: temp = 0x07; break;
457         default: IRDA_ERROR("%s(), invalid irq", __FUNCTION__);
458         }
459         outb(CFG_108_CSRT, cfg_base);
460         
461         switch (info->dma) {    
462         case 0: outb(0x08+temp, cfg_base+1); break;
463         case 1: outb(0x10+temp, cfg_base+1); break;
464         case 3: outb(0x18+temp, cfg_base+1); break;
465         default: IRDA_ERROR("%s(), invalid dma", __FUNCTION__);
466         }
467         
468         outb(CFG_108_MCTL, cfg_base);      /* Mode Control Register (MCTL) */
469         outb(0x03, cfg_base+1); /* Enable device */
470
471         return 0;
472 }
473
474 /*
475  * Function nsc_ircc_probe_108 (chip, info)
476  *
477  *    
478  *
479  */
480 static int nsc_ircc_probe_108(nsc_chip_t *chip, chipio_t *info) 
481 {
482         int cfg_base = info->cfg_base;
483         int reg;
484
485         /* Read address and interrupt control register (BAIC) */
486         outb(CFG_108_BAIC, cfg_base);
487         reg = inb(cfg_base+1);
488         
489         switch (reg & 0x03) {
490         case 0:
491                 info->fir_base = 0x3e8;
492                 break;
493         case 1:
494                 info->fir_base = 0x2e8;
495                 break;
496         case 2:
497                 info->fir_base = 0x3f8;
498                 break;
499         case 3:
500                 info->fir_base = 0x2f8;
501                 break;
502         }
503         info->sir_base = info->fir_base;
504         IRDA_DEBUG(2, "%s(), probing fir_base=0x%03x\n", __FUNCTION__,
505                    info->fir_base);
506
507         /* Read control signals routing register (CSRT) */
508         outb(CFG_108_CSRT, cfg_base);
509         reg = inb(cfg_base+1);
510
511         switch (reg & 0x07) {
512         case 0:
513                 info->irq = -1;
514                 break;
515         case 1:
516                 info->irq = 3;
517                 break;
518         case 2:
519                 info->irq = 4;
520                 break;
521         case 3:
522                 info->irq = 5;
523                 break;
524         case 4:
525                 info->irq = 7;
526                 break;
527         case 5:
528                 info->irq = 9;
529                 break;
530         case 6:
531                 info->irq = 11;
532                 break;
533         case 7:
534                 info->irq = 15;
535                 break;
536         }
537         IRDA_DEBUG(2, "%s(), probing irq=%d\n", __FUNCTION__, info->irq);
538
539         /* Currently we only read Rx DMA but it will also be used for Tx */
540         switch ((reg >> 3) & 0x03) {
541         case 0:
542                 info->dma = -1;
543                 break;
544         case 1:
545                 info->dma = 0;
546                 break;
547         case 2:
548                 info->dma = 1;
549                 break;
550         case 3:
551                 info->dma = 3;
552                 break;
553         }
554         IRDA_DEBUG(2, "%s(), probing dma=%d\n", __FUNCTION__, info->dma);
555
556         /* Read mode control register (MCTL) */
557         outb(CFG_108_MCTL, cfg_base);
558         reg = inb(cfg_base+1);
559
560         info->enabled = reg & 0x01;
561         info->suspended = !((reg >> 1) & 0x01);
562
563         return 0;
564 }
565
566 /*
567  * Function nsc_ircc_init_338 (chip, info)
568  *
569  *    Initialize the NSC '338 chip. Remember that the 87338 needs two 
570  *    consecutive writes to the data registers while CPU interrupts are
571  *    disabled. The 97338 does not require this, but shouldn't be any
572  *    harm if we do it anyway.
573  */
574 static int nsc_ircc_init_338(nsc_chip_t *chip, chipio_t *info) 
575 {
576         /* No init yet */
577         
578         return 0;
579 }
580
581 /*
582  * Function nsc_ircc_probe_338 (chip, info)
583  *
584  *    
585  *
586  */
587 static int nsc_ircc_probe_338(nsc_chip_t *chip, chipio_t *info) 
588 {
589         int cfg_base = info->cfg_base;
590         int reg, com = 0;
591         int pnp;
592
593         /* Read funtion enable register (FER) */
594         outb(CFG_338_FER, cfg_base);
595         reg = inb(cfg_base+1);
596
597         info->enabled = (reg >> 2) & 0x01;
598
599         /* Check if we are in Legacy or PnP mode */
600         outb(CFG_338_PNP0, cfg_base);
601         reg = inb(cfg_base+1);
602         
603         pnp = (reg >> 3) & 0x01;
604         if (pnp) {
605                 IRDA_DEBUG(2, "(), Chip is in PnP mode\n");
606                 outb(0x46, cfg_base);
607                 reg = (inb(cfg_base+1) & 0xfe) << 2;
608
609                 outb(0x47, cfg_base);
610                 reg |= ((inb(cfg_base+1) & 0xfc) << 8);
611
612                 info->fir_base = reg;
613         } else {
614                 /* Read function address register (FAR) */
615                 outb(CFG_338_FAR, cfg_base);
616                 reg = inb(cfg_base+1);
617                 
618                 switch ((reg >> 4) & 0x03) {
619                 case 0:
620                         info->fir_base = 0x3f8;
621                         break;
622                 case 1:
623                         info->fir_base = 0x2f8;
624                         break;
625                 case 2:
626                         com = 3;
627                         break;
628                 case 3:
629                         com = 4;
630                         break;
631                 }
632                 
633                 if (com) {
634                         switch ((reg >> 6) & 0x03) {
635                         case 0:
636                                 if (com == 3)
637                                         info->fir_base = 0x3e8;
638                                 else
639                                         info->fir_base = 0x2e8;
640                                 break;
641                         case 1:
642                                 if (com == 3)
643                                         info->fir_base = 0x338;
644                                 else
645                                         info->fir_base = 0x238;
646                                 break;
647                         case 2:
648                                 if (com == 3)
649                                         info->fir_base = 0x2e8;
650                                 else
651                                         info->fir_base = 0x2e0;
652                                 break;
653                         case 3:
654                                 if (com == 3)
655                                         info->fir_base = 0x220;
656                                 else
657                                         info->fir_base = 0x228;
658                                 break;
659                         }
660                 }
661         }
662         info->sir_base = info->fir_base;
663
664         /* Read PnP register 1 (PNP1) */
665         outb(CFG_338_PNP1, cfg_base);
666         reg = inb(cfg_base+1);
667         
668         info->irq = reg >> 4;
669         
670         /* Read PnP register 3 (PNP3) */
671         outb(CFG_338_PNP3, cfg_base);
672         reg = inb(cfg_base+1);
673
674         info->dma = (reg & 0x07) - 1;
675
676         /* Read power and test register (PTR) */
677         outb(CFG_338_PTR, cfg_base);
678         reg = inb(cfg_base+1);
679
680         info->suspended = reg & 0x01;
681
682         return 0;
683 }
684
685
686 /*
687  * Function nsc_ircc_init_39x (chip, info)
688  *
689  *    Now that we know it's a '39x (see probe below), we need to
690  *    configure it so we can use it.
691  *
692  * The NSC '338 chip is a Super I/O chip with a "bank" architecture,
693  * the configuration of the different functionality (serial, parallel,
694  * floppy...) are each in a different bank (Logical Device Number).
695  * The base address, irq and dma configuration registers are common
696  * to all functionalities (index 0x30 to 0x7F).
697  * There is only one configuration register specific to the
698  * serial port, CFG_39X_SPC.
699  * JeanII
700  *
701  * Note : this code was written by Jan Frey <janfrey@web.de>
702  */
703 static int nsc_ircc_init_39x(nsc_chip_t *chip, chipio_t *info) 
704 {
705         int cfg_base = info->cfg_base;
706         int enabled;
707
708         /* User is shure about his config... accept it. */
709         IRDA_DEBUG(2, "%s(): nsc_ircc_init_39x (user settings): "
710                    "io=0x%04x, irq=%d, dma=%d\n", 
711                    __FUNCTION__, info->fir_base, info->irq, info->dma);
712
713         /* Access bank for SP2 */
714         outb(CFG_39X_LDN, cfg_base);
715         outb(0x02, cfg_base+1);
716
717         /* Configure SP2 */
718
719         /* We want to enable the device if not enabled */
720         outb(CFG_39X_ACT, cfg_base);
721         enabled = inb(cfg_base+1) & 0x01;
722         
723         if (!enabled) {
724                 /* Enable the device */
725                 outb(CFG_39X_SIOCF1, cfg_base);
726                 outb(0x01, cfg_base+1);
727                 /* May want to update info->enabled. Jean II */
728         }
729
730         /* Enable UART bank switching (bit 7) ; Sets the chip to normal
731          * power mode (wake up from sleep mode) (bit 1) */
732         outb(CFG_39X_SPC, cfg_base);
733         outb(0x82, cfg_base+1);
734
735         return 0;
736 }
737
738 /*
739  * Function nsc_ircc_probe_39x (chip, info)
740  *
741  *    Test if we really have a '39x chip at the given address
742  *
743  * Note : this code was written by Jan Frey <janfrey@web.de>
744  */
745 static int nsc_ircc_probe_39x(nsc_chip_t *chip, chipio_t *info) 
746 {
747         int cfg_base = info->cfg_base;
748         int reg1, reg2, irq, irqt, dma1, dma2;
749         int enabled, susp;
750
751         IRDA_DEBUG(2, "%s(), nsc_ircc_probe_39x, base=%d\n",
752                    __FUNCTION__, cfg_base);
753
754         /* This function should be executed with irq off to avoid
755          * another driver messing with the Super I/O bank - Jean II */
756
757         /* Access bank for SP2 */
758         outb(CFG_39X_LDN, cfg_base);
759         outb(0x02, cfg_base+1);
760
761         /* Read infos about SP2 ; store in info struct */
762         outb(CFG_39X_BASEH, cfg_base);
763         reg1 = inb(cfg_base+1);
764         outb(CFG_39X_BASEL, cfg_base);
765         reg2 = inb(cfg_base+1);
766         info->fir_base = (reg1 << 8) | reg2;
767
768         outb(CFG_39X_IRQNUM, cfg_base);
769         irq = inb(cfg_base+1);
770         outb(CFG_39X_IRQSEL, cfg_base);
771         irqt = inb(cfg_base+1);
772         info->irq = irq;
773
774         outb(CFG_39X_DMA0, cfg_base);
775         dma1 = inb(cfg_base+1);
776         outb(CFG_39X_DMA1, cfg_base);
777         dma2 = inb(cfg_base+1);
778         info->dma = dma1 -1;
779
780         outb(CFG_39X_ACT, cfg_base);
781         info->enabled = enabled = inb(cfg_base+1) & 0x01;
782         
783         outb(CFG_39X_SPC, cfg_base);
784         susp = 1 - ((inb(cfg_base+1) & 0x02) >> 1);
785
786         IRDA_DEBUG(2, "%s(): io=0x%02x%02x, irq=%d (type %d), rxdma=%d, txdma=%d, enabled=%d (suspended=%d)\n", __FUNCTION__, reg1,reg2,irq,irqt,dma1,dma2,enabled,susp);
787
788         /* Configure SP2 */
789
790         /* We want to enable the device if not enabled */
791         outb(CFG_39X_ACT, cfg_base);
792         enabled = inb(cfg_base+1) & 0x01;
793         
794         if (!enabled) {
795                 /* Enable the device */
796                 outb(CFG_39X_SIOCF1, cfg_base);
797                 outb(0x01, cfg_base+1);
798                 /* May want to update info->enabled. Jean II */
799         }
800
801         /* Enable UART bank switching (bit 7) ; Sets the chip to normal
802          * power mode (wake up from sleep mode) (bit 1) */
803         outb(CFG_39X_SPC, cfg_base);
804         outb(0x82, cfg_base+1);
805
806         return 0;
807 }
808
809 /*
810  * Function nsc_ircc_setup (info)
811  *
812  *    Returns non-negative on success.
813  *
814  */
815 static int nsc_ircc_setup(chipio_t *info)
816 {
817         int version;
818         int iobase = info->fir_base;
819
820         /* Read the Module ID */
821         switch_bank(iobase, BANK3);
822         version = inb(iobase+MID);
823
824         IRDA_DEBUG(2, "%s() Driver %s Found chip version %02x\n",
825                    __FUNCTION__, driver_name, version);
826
827         /* Should be 0x2? */
828         if (0x20 != (version & 0xf0)) {
829                 IRDA_ERROR("%s, Wrong chip version %02x\n",
830                            driver_name, version);
831                 return -1;
832         }
833
834         /* Switch to advanced mode */
835         switch_bank(iobase, BANK2);
836         outb(ECR1_EXT_SL, iobase+ECR1);
837         switch_bank(iobase, BANK0);
838         
839         /* Set FIFO threshold to TX17, RX16, reset and enable FIFO's */
840         switch_bank(iobase, BANK0);
841         outb(FCR_RXTH|FCR_TXTH|FCR_TXSR|FCR_RXSR|FCR_FIFO_EN, iobase+FCR);
842
843         outb(0x03, iobase+LCR);         /* 8 bit word length */
844         outb(MCR_SIR, iobase+MCR);      /* Start at SIR-mode, also clears LSR*/
845
846         /* Set FIFO size to 32 */
847         switch_bank(iobase, BANK2);
848         outb(EXCR2_RFSIZ|EXCR2_TFSIZ, iobase+EXCR2);
849
850         /* IRCR2: FEND_MD is not set */
851         switch_bank(iobase, BANK5);
852         outb(0x02, iobase+4);
853
854         /* Make sure that some defaults are OK */
855         switch_bank(iobase, BANK6);
856         outb(0x20, iobase+0); /* Set 32 bits FIR CRC */
857         outb(0x0a, iobase+1); /* Set MIR pulse width */
858         outb(0x0d, iobase+2); /* Set SIR pulse width to 1.6us */
859         outb(0x2a, iobase+4); /* Set beginning frag, and preamble length */
860
861         /* Enable receive interrupts */
862         switch_bank(iobase, BANK0);
863         outb(IER_RXHDL_IE, iobase+IER);
864
865         return 0;
866 }
867
868 /*
869  * Function nsc_ircc_read_dongle_id (void)
870  *
871  * Try to read dongle indentification. This procedure needs to be executed
872  * once after power-on/reset. It also needs to be used whenever you suspect
873  * that the user may have plugged/unplugged the IrDA Dongle.
874  */
875 static int nsc_ircc_read_dongle_id (int iobase)
876 {
877         int dongle_id;
878         __u8 bank;
879
880         bank = inb(iobase+BSR);
881
882         /* Select Bank 7 */
883         switch_bank(iobase, BANK7);
884         
885         /* IRCFG4: IRSL0_DS and IRSL21_DS are cleared */
886         outb(0x00, iobase+7);
887         
888         /* ID0, 1, and 2 are pulled up/down very slowly */
889         udelay(50);
890         
891         /* IRCFG1: read the ID bits */
892         dongle_id = inb(iobase+4) & 0x0f;
893
894 #ifdef BROKEN_DONGLE_ID
895         if (dongle_id == 0x0a)
896                 dongle_id = 0x09;
897 #endif  
898         /* Go back to  bank 0 before returning */
899         switch_bank(iobase, BANK0);
900
901         outb(bank, iobase+BSR);
902
903         return dongle_id;
904 }
905
906 /*
907  * Function nsc_ircc_init_dongle_interface (iobase, dongle_id)
908  *
909  *     This function initializes the dongle for the transceiver that is
910  *     used. This procedure needs to be executed once after
911  *     power-on/reset. It also needs to be used whenever you suspect that
912  *     the dongle is changed. 
913  */
914 static void nsc_ircc_init_dongle_interface (int iobase, int dongle_id)
915 {
916         int bank;
917
918         /* Save current bank */
919         bank = inb(iobase+BSR);
920
921         /* Select Bank 7 */
922         switch_bank(iobase, BANK7);
923         
924         /* IRCFG4: set according to dongle_id */
925         switch (dongle_id) {
926         case 0x00: /* same as */
927         case 0x01: /* Differential serial interface */
928                 IRDA_DEBUG(0, "%s(), %s not defined by irda yet\n",
929                            __FUNCTION__, dongle_types[dongle_id]); 
930                 break;
931         case 0x02: /* same as */
932         case 0x03: /* Reserved */
933                 IRDA_DEBUG(0, "%s(), %s not defined by irda yet\n",
934                            __FUNCTION__, dongle_types[dongle_id]); 
935                 break;
936         case 0x04: /* Sharp RY5HD01 */
937                 break;
938         case 0x05: /* Reserved, but this is what the Thinkpad reports */
939                 IRDA_DEBUG(0, "%s(), %s not defined by irda yet\n",
940                            __FUNCTION__, dongle_types[dongle_id]); 
941                 break;
942         case 0x06: /* Single-ended serial interface */
943                 IRDA_DEBUG(0, "%s(), %s not defined by irda yet\n",
944                            __FUNCTION__, dongle_types[dongle_id]); 
945                 break;
946         case 0x07: /* Consumer-IR only */
947                 IRDA_DEBUG(0, "%s(), %s is not for IrDA mode\n",
948                            __FUNCTION__, dongle_types[dongle_id]); 
949                 break;
950         case 0x08: /* HP HSDL-2300, HP HSDL-3600/HSDL-3610 */
951                 IRDA_DEBUG(0, "%s(), %s\n",
952                            __FUNCTION__, dongle_types[dongle_id]);
953                 break;
954         case 0x09: /* IBM31T1100 or Temic TFDS6000/TFDS6500 */
955                 outb(0x28, iobase+7); /* Set irsl[0-2] as output */
956                 break;
957         case 0x0A: /* same as */
958         case 0x0B: /* Reserved */
959                 IRDA_DEBUG(0, "%s(), %s not defined by irda yet\n",
960                            __FUNCTION__, dongle_types[dongle_id]); 
961                 break;
962         case 0x0C: /* same as */
963         case 0x0D: /* HP HSDL-1100/HSDL-2100 */
964                 /* 
965                  * Set irsl0 as input, irsl[1-2] as output, and separate 
966                  * inputs are used for SIR and MIR/FIR 
967                  */
968                 outb(0x48, iobase+7); 
969                 break;
970         case 0x0E: /* Supports SIR Mode only */
971                 outb(0x28, iobase+7); /* Set irsl[0-2] as output */
972                 break;
973         case 0x0F: /* No dongle connected */
974                 IRDA_DEBUG(0, "%s(), %s\n",
975                            __FUNCTION__, dongle_types[dongle_id]); 
976
977                 switch_bank(iobase, BANK0);
978                 outb(0x62, iobase+MCR);
979                 break;
980         default: 
981                 IRDA_DEBUG(0, "%s(), invalid dongle_id %#x", 
982                            __FUNCTION__, dongle_id);
983         }
984         
985         /* IRCFG1: IRSL1 and 2 are set to IrDA mode */
986         outb(0x00, iobase+4);
987
988         /* Restore bank register */
989         outb(bank, iobase+BSR);
990         
991 } /* set_up_dongle_interface */
992
993 /*
994  * Function nsc_ircc_change_dongle_speed (iobase, speed, dongle_id)
995  *
996  *    Change speed of the attach dongle
997  *
998  */
999 static void nsc_ircc_change_dongle_speed(int iobase, int speed, int dongle_id)
1000 {
1001         __u8 bank;
1002
1003         /* Save current bank */
1004         bank = inb(iobase+BSR);
1005
1006         /* Select Bank 7 */
1007         switch_bank(iobase, BANK7);
1008         
1009         /* IRCFG1: set according to dongle_id */
1010         switch (dongle_id) {
1011         case 0x00: /* same as */
1012         case 0x01: /* Differential serial interface */
1013                 IRDA_DEBUG(0, "%s(), %s not defined by irda yet\n",
1014                            __FUNCTION__, dongle_types[dongle_id]); 
1015                 break;
1016         case 0x02: /* same as */
1017         case 0x03: /* Reserved */
1018                 IRDA_DEBUG(0, "%s(), %s not defined by irda yet\n",
1019                            __FUNCTION__, dongle_types[dongle_id]); 
1020                 break;
1021         case 0x04: /* Sharp RY5HD01 */
1022                 break;
1023         case 0x05: /* Reserved */
1024                 IRDA_DEBUG(0, "%s(), %s not defined by irda yet\n",
1025                            __FUNCTION__, dongle_types[dongle_id]); 
1026                 break;
1027         case 0x06: /* Single-ended serial interface */
1028                 IRDA_DEBUG(0, "%s(), %s not defined by irda yet\n",
1029                            __FUNCTION__, dongle_types[dongle_id]); 
1030                 break;
1031         case 0x07: /* Consumer-IR only */
1032                 IRDA_DEBUG(0, "%s(), %s is not for IrDA mode\n",
1033                            __FUNCTION__, dongle_types[dongle_id]); 
1034                 break;
1035         case 0x08: /* HP HSDL-2300, HP HSDL-3600/HSDL-3610 */
1036                 IRDA_DEBUG(0, "%s(), %s\n", 
1037                            __FUNCTION__, dongle_types[dongle_id]); 
1038                 outb(0x00, iobase+4);
1039                 if (speed > 115200)
1040                         outb(0x01, iobase+4);
1041                 break;
1042         case 0x09: /* IBM31T1100 or Temic TFDS6000/TFDS6500 */
1043                 outb(0x01, iobase+4);
1044
1045                 if (speed == 4000000) {
1046                         /* There was a cli() there, but we now are already
1047                          * under spin_lock_irqsave() - JeanII */
1048                         outb(0x81, iobase+4);
1049                         outb(0x80, iobase+4);
1050                 } else
1051                         outb(0x00, iobase+4);
1052                 break;
1053         case 0x0A: /* same as */
1054         case 0x0B: /* Reserved */
1055                 IRDA_DEBUG(0, "%s(), %s not defined by irda yet\n",
1056                            __FUNCTION__, dongle_types[dongle_id]); 
1057                 break;
1058         case 0x0C: /* same as */
1059         case 0x0D: /* HP HSDL-1100/HSDL-2100 */
1060                 break;
1061         case 0x0E: /* Supports SIR Mode only */
1062                 break;
1063         case 0x0F: /* No dongle connected */
1064                 IRDA_DEBUG(0, "%s(), %s is not for IrDA mode\n",
1065                            __FUNCTION__, dongle_types[dongle_id]);
1066
1067                 switch_bank(iobase, BANK0); 
1068                 outb(0x62, iobase+MCR);
1069                 break;
1070         default: 
1071                 IRDA_DEBUG(0, "%s(), invalid data_rate\n", __FUNCTION__);
1072         }
1073         /* Restore bank register */
1074         outb(bank, iobase+BSR);
1075 }
1076
1077 /*
1078  * Function nsc_ircc_change_speed (self, baud)
1079  *
1080  *    Change the speed of the device
1081  *
1082  * This function *must* be called with irq off and spin-lock.
1083  */
1084 static __u8 nsc_ircc_change_speed(struct nsc_ircc_cb *self, __u32 speed)
1085 {
1086         struct net_device *dev = self->netdev;
1087         __u8 mcr = MCR_SIR;
1088         int iobase; 
1089         __u8 bank;
1090         __u8 ier;                  /* Interrupt enable register */
1091
1092         IRDA_DEBUG(2, "%s(), speed=%d\n", __FUNCTION__, speed);
1093
1094         IRDA_ASSERT(self != NULL, return 0;);
1095
1096         iobase = self->io.fir_base;
1097
1098         /* Update accounting for new speed */
1099         self->io.speed = speed;
1100
1101         /* Save current bank */
1102         bank = inb(iobase+BSR);
1103
1104         /* Disable interrupts */
1105         switch_bank(iobase, BANK0);
1106         outb(0, iobase+IER);
1107
1108         /* Select Bank 2 */
1109         switch_bank(iobase, BANK2);
1110
1111         outb(0x00, iobase+BGDH);
1112         switch (speed) {
1113         case 9600:   outb(0x0c, iobase+BGDL); break;
1114         case 19200:  outb(0x06, iobase+BGDL); break;
1115         case 38400:  outb(0x03, iobase+BGDL); break;
1116         case 57600:  outb(0x02, iobase+BGDL); break;
1117         case 115200: outb(0x01, iobase+BGDL); break;
1118         case 576000:
1119                 switch_bank(iobase, BANK5);
1120                 
1121                 /* IRCR2: MDRS is set */
1122                 outb(inb(iobase+4) | 0x04, iobase+4);
1123                
1124                 mcr = MCR_MIR;
1125                 IRDA_DEBUG(0, "%s(), handling baud of 576000\n", __FUNCTION__);
1126                 break;
1127         case 1152000:
1128                 mcr = MCR_MIR;
1129                 IRDA_DEBUG(0, "%s(), handling baud of 1152000\n", __FUNCTION__);
1130                 break;
1131         case 4000000:
1132                 mcr = MCR_FIR;
1133                 IRDA_DEBUG(0, "%s(), handling baud of 4000000\n", __FUNCTION__);
1134                 break;
1135         default:
1136                 mcr = MCR_FIR;
1137                 IRDA_DEBUG(0, "%s(), unknown baud rate of %d\n", 
1138                            __FUNCTION__, speed);
1139                 break;
1140         }
1141
1142         /* Set appropriate speed mode */
1143         switch_bank(iobase, BANK0);
1144         outb(mcr | MCR_TX_DFR, iobase+MCR);
1145
1146         /* Give some hits to the transceiver */
1147         nsc_ircc_change_dongle_speed(iobase, speed, self->io.dongle_id);
1148
1149         /* Set FIFO threshold to TX17, RX16 */
1150         switch_bank(iobase, BANK0);
1151         outb(0x00, iobase+FCR);
1152         outb(FCR_FIFO_EN, iobase+FCR);
1153         outb(FCR_RXTH|     /* Set Rx FIFO threshold */
1154              FCR_TXTH|     /* Set Tx FIFO threshold */
1155              FCR_TXSR|     /* Reset Tx FIFO */
1156              FCR_RXSR|     /* Reset Rx FIFO */
1157              FCR_FIFO_EN,  /* Enable FIFOs */
1158              iobase+FCR);
1159         
1160         /* Set FIFO size to 32 */
1161         switch_bank(iobase, BANK2);
1162         outb(EXCR2_RFSIZ|EXCR2_TFSIZ, iobase+EXCR2);
1163         
1164         /* Enable some interrupts so we can receive frames */
1165         switch_bank(iobase, BANK0); 
1166         if (speed > 115200) {
1167                 /* Install FIR xmit handler */
1168                 dev->hard_start_xmit = nsc_ircc_hard_xmit_fir;
1169                 ier = IER_SFIF_IE;
1170                 nsc_ircc_dma_receive(self);
1171         } else {
1172                 /* Install SIR xmit handler */
1173                 dev->hard_start_xmit = nsc_ircc_hard_xmit_sir;
1174                 ier = IER_RXHDL_IE;
1175         }
1176         /* Set our current interrupt mask */
1177         outb(ier, iobase+IER);
1178         
1179         /* Restore BSR */
1180         outb(bank, iobase+BSR);
1181
1182         /* Make sure interrupt handlers keep the proper interrupt mask */
1183         return(ier);
1184 }
1185
1186 /*
1187  * Function nsc_ircc_hard_xmit (skb, dev)
1188  *
1189  *    Transmit the frame!
1190  *
1191  */
1192 static int nsc_ircc_hard_xmit_sir(struct sk_buff *skb, struct net_device *dev)
1193 {
1194         struct nsc_ircc_cb *self;
1195         unsigned long flags;
1196         int iobase;
1197         __s32 speed;
1198         __u8 bank;
1199         
1200         self = (struct nsc_ircc_cb *) dev->priv;
1201
1202         IRDA_ASSERT(self != NULL, return 0;);
1203
1204         iobase = self->io.fir_base;
1205
1206         netif_stop_queue(dev);
1207                 
1208         /* Make sure tests *& speed change are atomic */
1209         spin_lock_irqsave(&self->lock, flags);
1210         
1211         /* Check if we need to change the speed */
1212         speed = irda_get_next_speed(skb);
1213         if ((speed != self->io.speed) && (speed != -1)) {
1214                 /* Check for empty frame. */
1215                 if (!skb->len) {
1216                         /* If we just sent a frame, we get called before
1217                          * the last bytes get out (because of the SIR FIFO).
1218                          * If this is the case, let interrupt handler change
1219                          * the speed itself... Jean II */
1220                         if (self->io.direction == IO_RECV) {
1221                                 nsc_ircc_change_speed(self, speed); 
1222                                 /* TODO : For SIR->SIR, the next packet
1223                                  * may get corrupted - Jean II */
1224                                 netif_wake_queue(dev);
1225                         } else {
1226                                 self->new_speed = speed;
1227                                 /* Queue will be restarted after speed change
1228                                  * to make sure packets gets through the
1229                                  * proper xmit handler - Jean II */
1230                         }
1231                         dev->trans_start = jiffies;
1232                         spin_unlock_irqrestore(&self->lock, flags);
1233                         dev_kfree_skb(skb);
1234                         return 0;
1235                 } else
1236                         self->new_speed = speed;
1237         }
1238
1239         /* Save current bank */
1240         bank = inb(iobase+BSR);
1241         
1242         self->tx_buff.data = self->tx_buff.head;
1243         
1244         self->tx_buff.len = async_wrap_skb(skb, self->tx_buff.data, 
1245                                            self->tx_buff.truesize);
1246
1247         self->stats.tx_bytes += self->tx_buff.len;
1248         
1249         /* Add interrupt on tx low level (will fire immediately) */
1250         switch_bank(iobase, BANK0);
1251         outb(IER_TXLDL_IE, iobase+IER);
1252         
1253         /* Restore bank register */
1254         outb(bank, iobase+BSR);
1255
1256         dev->trans_start = jiffies;
1257         spin_unlock_irqrestore(&self->lock, flags);
1258
1259         dev_kfree_skb(skb);
1260
1261         return 0;
1262 }
1263
1264 static int nsc_ircc_hard_xmit_fir(struct sk_buff *skb, struct net_device *dev)
1265 {
1266         struct nsc_ircc_cb *self;
1267         unsigned long flags;
1268         int iobase;
1269         __s32 speed;
1270         __u8 bank;
1271         int mtt, diff;
1272         
1273         self = (struct nsc_ircc_cb *) dev->priv;
1274         iobase = self->io.fir_base;
1275
1276         netif_stop_queue(dev);
1277         
1278         /* Make sure tests *& speed change are atomic */
1279         spin_lock_irqsave(&self->lock, flags);
1280
1281         /* Check if we need to change the speed */
1282         speed = irda_get_next_speed(skb);
1283         if ((speed != self->io.speed) && (speed != -1)) {
1284                 /* Check for empty frame. */
1285                 if (!skb->len) {
1286                         /* If we are currently transmitting, defer to
1287                          * interrupt handler. - Jean II */
1288                         if(self->tx_fifo.len == 0) {
1289                                 nsc_ircc_change_speed(self, speed); 
1290                                 netif_wake_queue(dev);
1291                         } else {
1292                                 self->new_speed = speed;
1293                                 /* Keep queue stopped :
1294                                  * the speed change operation may change the
1295                                  * xmit handler, and we want to make sure
1296                                  * the next packet get through the proper
1297                                  * Tx path, so block the Tx queue until
1298                                  * the speed change has been done.
1299                                  * Jean II */
1300                         }
1301                         dev->trans_start = jiffies;
1302                         spin_unlock_irqrestore(&self->lock, flags);
1303                         dev_kfree_skb(skb);
1304                         return 0;
1305                 } else {
1306                         /* Change speed after current frame */
1307                         self->new_speed = speed;
1308                 }
1309         }
1310
1311         /* Save current bank */
1312         bank = inb(iobase+BSR);
1313
1314         /* Register and copy this frame to DMA memory */
1315         self->tx_fifo.queue[self->tx_fifo.free].start = self->tx_fifo.tail;
1316         self->tx_fifo.queue[self->tx_fifo.free].len = skb->len;
1317         self->tx_fifo.tail += skb->len;
1318
1319         self->stats.tx_bytes += skb->len;
1320
1321         memcpy(self->tx_fifo.queue[self->tx_fifo.free].start, skb->data, 
1322                skb->len);
1323         
1324         self->tx_fifo.len++;
1325         self->tx_fifo.free++;
1326
1327         /* Start transmit only if there is currently no transmit going on */
1328         if (self->tx_fifo.len == 1) {
1329                 /* Check if we must wait the min turn time or not */
1330                 mtt = irda_get_mtt(skb);
1331                 if (mtt) {
1332                         /* Check how much time we have used already */
1333                         do_gettimeofday(&self->now);
1334                         diff = self->now.tv_usec - self->stamp.tv_usec;
1335                         if (diff < 0) 
1336                                 diff += 1000000;
1337                         
1338                         /* Check if the mtt is larger than the time we have
1339                          * already used by all the protocol processing
1340                          */
1341                         if (mtt > diff) {
1342                                 mtt -= diff;
1343
1344                                 /* 
1345                                  * Use timer if delay larger than 125 us, and
1346                                  * use udelay for smaller values which should
1347                                  * be acceptable
1348                                  */
1349                                 if (mtt > 125) {
1350                                         /* Adjust for timer resolution */
1351                                         mtt = mtt / 125;
1352                                         
1353                                         /* Setup timer */
1354                                         switch_bank(iobase, BANK4);
1355                                         outb(mtt & 0xff, iobase+TMRL);
1356                                         outb((mtt >> 8) & 0x0f, iobase+TMRH);
1357                                         
1358                                         /* Start timer */
1359                                         outb(IRCR1_TMR_EN, iobase+IRCR1);
1360                                         self->io.direction = IO_XMIT;
1361                                         
1362                                         /* Enable timer interrupt */
1363                                         switch_bank(iobase, BANK0);
1364                                         outb(IER_TMR_IE, iobase+IER);
1365                                         
1366                                         /* Timer will take care of the rest */
1367                                         goto out; 
1368                                 } else
1369                                         udelay(mtt);
1370                         }
1371                 }               
1372                 /* Enable DMA interrupt */
1373                 switch_bank(iobase, BANK0);
1374                 outb(IER_DMA_IE, iobase+IER);
1375
1376                 /* Transmit frame */
1377                 nsc_ircc_dma_xmit(self, iobase);
1378         }
1379  out:
1380         /* Not busy transmitting anymore if window is not full,
1381          * and if we don't need to change speed */
1382         if ((self->tx_fifo.free < MAX_TX_WINDOW) && (self->new_speed == 0))
1383                 netif_wake_queue(self->netdev);
1384
1385         /* Restore bank register */
1386         outb(bank, iobase+BSR);
1387
1388         dev->trans_start = jiffies;
1389         spin_unlock_irqrestore(&self->lock, flags);
1390         dev_kfree_skb(skb);
1391
1392         return 0;
1393 }
1394
1395 /*
1396  * Function nsc_ircc_dma_xmit (self, iobase)
1397  *
1398  *    Transmit data using DMA
1399  *
1400  */
1401 static void nsc_ircc_dma_xmit(struct nsc_ircc_cb *self, int iobase)
1402 {
1403         int bsr;
1404
1405         /* Save current bank */
1406         bsr = inb(iobase+BSR);
1407
1408         /* Disable DMA */
1409         switch_bank(iobase, BANK0);
1410         outb(inb(iobase+MCR) & ~MCR_DMA_EN, iobase+MCR);
1411         
1412         self->io.direction = IO_XMIT;
1413         
1414         /* Choose transmit DMA channel  */ 
1415         switch_bank(iobase, BANK2);
1416         outb(ECR1_DMASWP|ECR1_DMANF|ECR1_EXT_SL, iobase+ECR1);
1417         
1418         irda_setup_dma(self->io.dma, 
1419                        ((u8 *)self->tx_fifo.queue[self->tx_fifo.ptr].start -
1420                         self->tx_buff.head) + self->tx_buff_dma,
1421                        self->tx_fifo.queue[self->tx_fifo.ptr].len, 
1422                        DMA_TX_MODE);
1423
1424         /* Enable DMA and SIR interaction pulse */
1425         switch_bank(iobase, BANK0);     
1426         outb(inb(iobase+MCR)|MCR_TX_DFR|MCR_DMA_EN|MCR_IR_PLS, iobase+MCR);
1427
1428         /* Restore bank register */
1429         outb(bsr, iobase+BSR);
1430 }
1431
1432 /*
1433  * Function nsc_ircc_pio_xmit (self, iobase)
1434  *
1435  *    Transmit data using PIO. Returns the number of bytes that actually
1436  *    got transferred
1437  *
1438  */
1439 static int nsc_ircc_pio_write(int iobase, __u8 *buf, int len, int fifo_size)
1440 {
1441         int actual = 0;
1442         __u8 bank;
1443         
1444         IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
1445
1446         /* Save current bank */
1447         bank = inb(iobase+BSR);
1448
1449         switch_bank(iobase, BANK0);
1450         if (!(inb_p(iobase+LSR) & LSR_TXEMP)) {
1451                 IRDA_DEBUG(4, "%s(), warning, FIFO not empty yet!\n",
1452                            __FUNCTION__);
1453
1454                 /* FIFO may still be filled to the Tx interrupt threshold */
1455                 fifo_size -= 17;
1456         }
1457
1458         /* Fill FIFO with current frame */
1459         while ((fifo_size-- > 0) && (actual < len)) {
1460                 /* Transmit next byte */
1461                 outb(buf[actual++], iobase+TXD);
1462         }
1463         
1464         IRDA_DEBUG(4, "%s(), fifo_size %d ; %d sent of %d\n", 
1465                    __FUNCTION__, fifo_size, actual, len);
1466         
1467         /* Restore bank */
1468         outb(bank, iobase+BSR);
1469
1470         return actual;
1471 }
1472
1473 /*
1474  * Function nsc_ircc_dma_xmit_complete (self)
1475  *
1476  *    The transfer of a frame in finished. This function will only be called 
1477  *    by the interrupt handler
1478  *
1479  */
1480 static int nsc_ircc_dma_xmit_complete(struct nsc_ircc_cb *self)
1481 {
1482         int iobase;
1483         __u8 bank;
1484         int ret = TRUE;
1485
1486         IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
1487
1488         iobase = self->io.fir_base;
1489
1490         /* Save current bank */
1491         bank = inb(iobase+BSR);
1492
1493         /* Disable DMA */
1494         switch_bank(iobase, BANK0);
1495         outb(inb(iobase+MCR) & ~MCR_DMA_EN, iobase+MCR);
1496         
1497         /* Check for underrrun! */
1498         if (inb(iobase+ASCR) & ASCR_TXUR) {
1499                 self->stats.tx_errors++;
1500                 self->stats.tx_fifo_errors++;
1501                 
1502                 /* Clear bit, by writing 1 into it */
1503                 outb(ASCR_TXUR, iobase+ASCR);
1504         } else {
1505                 self->stats.tx_packets++;
1506         }
1507
1508         /* Finished with this frame, so prepare for next */
1509         self->tx_fifo.ptr++;
1510         self->tx_fifo.len--;
1511
1512         /* Any frames to be sent back-to-back? */
1513         if (self->tx_fifo.len) {
1514                 nsc_ircc_dma_xmit(self, iobase);
1515                 
1516                 /* Not finished yet! */
1517                 ret = FALSE;
1518         } else {
1519                 /* Reset Tx FIFO info */
1520                 self->tx_fifo.len = self->tx_fifo.ptr = self->tx_fifo.free = 0;
1521                 self->tx_fifo.tail = self->tx_buff.head;
1522         }
1523
1524         /* Make sure we have room for more frames and
1525          * that we don't need to change speed */
1526         if ((self->tx_fifo.free < MAX_TX_WINDOW) && (self->new_speed == 0)) {
1527                 /* Not busy transmitting anymore */
1528                 /* Tell the network layer, that we can accept more frames */
1529                 netif_wake_queue(self->netdev);
1530         }
1531
1532         /* Restore bank */
1533         outb(bank, iobase+BSR);
1534         
1535         return ret;
1536 }
1537
1538 /*
1539  * Function nsc_ircc_dma_receive (self)
1540  *
1541  *    Get ready for receiving a frame. The device will initiate a DMA
1542  *    if it starts to receive a frame.
1543  *
1544  */
1545 static int nsc_ircc_dma_receive(struct nsc_ircc_cb *self) 
1546 {
1547         int iobase;
1548         __u8 bsr;
1549
1550         iobase = self->io.fir_base;
1551
1552         /* Reset Tx FIFO info */
1553         self->tx_fifo.len = self->tx_fifo.ptr = self->tx_fifo.free = 0;
1554         self->tx_fifo.tail = self->tx_buff.head;
1555
1556         /* Save current bank */
1557         bsr = inb(iobase+BSR);
1558
1559         /* Disable DMA */
1560         switch_bank(iobase, BANK0);
1561         outb(inb(iobase+MCR) & ~MCR_DMA_EN, iobase+MCR);
1562
1563         /* Choose DMA Rx, DMA Fairness, and Advanced mode */
1564         switch_bank(iobase, BANK2);
1565         outb(ECR1_DMANF|ECR1_EXT_SL, iobase+ECR1);
1566
1567         self->io.direction = IO_RECV;
1568         self->rx_buff.data = self->rx_buff.head;
1569         
1570         /* Reset Rx FIFO. This will also flush the ST_FIFO */
1571         switch_bank(iobase, BANK0);
1572         outb(FCR_RXSR|FCR_FIFO_EN, iobase+FCR);
1573
1574         self->st_fifo.len = self->st_fifo.pending_bytes = 0;
1575         self->st_fifo.tail = self->st_fifo.head = 0;
1576         
1577         irda_setup_dma(self->io.dma, self->rx_buff_dma, self->rx_buff.truesize,
1578                        DMA_RX_MODE);
1579
1580         /* Enable DMA */
1581         switch_bank(iobase, BANK0);
1582         outb(inb(iobase+MCR)|MCR_DMA_EN, iobase+MCR);
1583
1584         /* Restore bank register */
1585         outb(bsr, iobase+BSR);
1586         
1587         return 0;
1588 }
1589
1590 /*
1591  * Function nsc_ircc_dma_receive_complete (self)
1592  *
1593  *    Finished with receiving frames
1594  *
1595  *    
1596  */
1597 static int nsc_ircc_dma_receive_complete(struct nsc_ircc_cb *self, int iobase)
1598 {
1599         struct st_fifo *st_fifo;
1600         struct sk_buff *skb;
1601         __u8 status;
1602         __u8 bank;
1603         int len;
1604
1605         st_fifo = &self->st_fifo;
1606
1607         /* Save current bank */
1608         bank = inb(iobase+BSR);
1609         
1610         /* Read all entries in status FIFO */
1611         switch_bank(iobase, BANK5);
1612         while ((status = inb(iobase+FRM_ST)) & FRM_ST_VLD) {
1613                 /* We must empty the status FIFO no matter what */
1614                 len = inb(iobase+RFLFL) | ((inb(iobase+RFLFH) & 0x1f) << 8);
1615
1616                 if (st_fifo->tail >= MAX_RX_WINDOW) {
1617                         IRDA_DEBUG(0, "%s(), window is full!\n", __FUNCTION__);
1618                         continue;
1619                 }
1620                         
1621                 st_fifo->entries[st_fifo->tail].status = status;
1622                 st_fifo->entries[st_fifo->tail].len = len;
1623                 st_fifo->pending_bytes += len;
1624                 st_fifo->tail++;
1625                 st_fifo->len++;
1626         }
1627         /* Try to process all entries in status FIFO */
1628         while (st_fifo->len > 0) {
1629                 /* Get first entry */
1630                 status = st_fifo->entries[st_fifo->head].status;
1631                 len    = st_fifo->entries[st_fifo->head].len;
1632                 st_fifo->pending_bytes -= len;
1633                 st_fifo->head++;
1634                 st_fifo->len--;
1635
1636                 /* Check for errors */
1637                 if (status & FRM_ST_ERR_MSK) {
1638                         if (status & FRM_ST_LOST_FR) {
1639                                 /* Add number of lost frames to stats */
1640                                 self->stats.rx_errors += len;   
1641                         } else {
1642                                 /* Skip frame */
1643                                 self->stats.rx_errors++;
1644                                 
1645                                 self->rx_buff.data += len;
1646                         
1647                                 if (status & FRM_ST_MAX_LEN)
1648                                         self->stats.rx_length_errors++;
1649                                 
1650                                 if (status & FRM_ST_PHY_ERR) 
1651                                         self->stats.rx_frame_errors++;
1652                                 
1653                                 if (status & FRM_ST_BAD_CRC) 
1654                                         self->stats.rx_crc_errors++;
1655                         }
1656                         /* The errors below can be reported in both cases */
1657                         if (status & FRM_ST_OVR1)
1658                                 self->stats.rx_fifo_errors++;                  
1659                         
1660                         if (status & FRM_ST_OVR2)
1661                                 self->stats.rx_fifo_errors++;
1662                 } else {
1663                         /*  
1664                          * First we must make sure that the frame we
1665                          * want to deliver is all in main memory. If we
1666                          * cannot tell, then we check if the Rx FIFO is
1667                          * empty. If not then we will have to take a nap
1668                          * and try again later.  
1669                          */
1670                         if (st_fifo->pending_bytes < self->io.fifo_size) {
1671                                 switch_bank(iobase, BANK0);
1672                                 if (inb(iobase+LSR) & LSR_RXDA) {
1673                                         /* Put this entry back in fifo */
1674                                         st_fifo->head--;
1675                                         st_fifo->len++;
1676                                         st_fifo->pending_bytes += len;
1677                                         st_fifo->entries[st_fifo->head].status = status;
1678                                         st_fifo->entries[st_fifo->head].len = len;
1679                                         /*  
1680                                          * DMA not finished yet, so try again 
1681                                          * later, set timer value, resolution 
1682                                          * 125 us 
1683                                          */
1684                                         switch_bank(iobase, BANK4);
1685                                         outb(0x02, iobase+TMRL); /* x 125 us */
1686                                         outb(0x00, iobase+TMRH);
1687
1688                                         /* Start timer */
1689                                         outb(IRCR1_TMR_EN, iobase+IRCR1);
1690
1691                                         /* Restore bank register */
1692                                         outb(bank, iobase+BSR);
1693                                         
1694                                         return FALSE; /* I'll be back! */
1695                                 }
1696                         }
1697
1698                         /* 
1699                          * Remember the time we received this frame, so we can
1700                          * reduce the min turn time a bit since we will know
1701                          * how much time we have used for protocol processing
1702                          */
1703                         do_gettimeofday(&self->stamp);
1704
1705                         skb = dev_alloc_skb(len+1);
1706                         if (skb == NULL)  {
1707                                 IRDA_WARNING("%s(), memory squeeze, "
1708                                              "dropping frame.\n",
1709                                              __FUNCTION__);
1710                                 self->stats.rx_dropped++;
1711
1712                                 /* Restore bank register */
1713                                 outb(bank, iobase+BSR);
1714
1715                                 return FALSE;
1716                         }
1717                         
1718                         /* Make sure IP header gets aligned */
1719                         skb_reserve(skb, 1); 
1720
1721                         /* Copy frame without CRC */
1722                         if (self->io.speed < 4000000) {
1723                                 skb_put(skb, len-2);
1724                                 memcpy(skb->data, self->rx_buff.data, len-2);
1725                         } else {
1726                                 skb_put(skb, len-4);
1727                                 memcpy(skb->data, self->rx_buff.data, len-4);
1728                         }
1729
1730                         /* Move to next frame */
1731                         self->rx_buff.data += len;
1732                         self->stats.rx_bytes += len;
1733                         self->stats.rx_packets++;
1734
1735                         skb->dev = self->netdev;
1736                         skb->mac.raw  = skb->data;
1737                         skb->protocol = htons(ETH_P_IRDA);
1738                         netif_rx(skb);
1739                         self->netdev->last_rx = jiffies;
1740                 }
1741         }
1742         /* Restore bank register */
1743         outb(bank, iobase+BSR);
1744
1745         return TRUE;
1746 }
1747
1748 /*
1749  * Function nsc_ircc_pio_receive (self)
1750  *
1751  *    Receive all data in receiver FIFO
1752  *
1753  */
1754 static void nsc_ircc_pio_receive(struct nsc_ircc_cb *self) 
1755 {
1756         __u8 byte;
1757         int iobase;
1758
1759         iobase = self->io.fir_base;
1760         
1761         /*  Receive all characters in Rx FIFO */
1762         do {
1763                 byte = inb(iobase+RXD);
1764                 async_unwrap_char(self->netdev, &self->stats, &self->rx_buff, 
1765                                   byte);
1766         } while (inb(iobase+LSR) & LSR_RXDA); /* Data available */      
1767 }
1768
1769 /*
1770  * Function nsc_ircc_sir_interrupt (self, eir)
1771  *
1772  *    Handle SIR interrupt
1773  *
1774  */
1775 static void nsc_ircc_sir_interrupt(struct nsc_ircc_cb *self, int eir)
1776 {
1777         int actual;
1778
1779         /* Check if transmit FIFO is low on data */
1780         if (eir & EIR_TXLDL_EV) {
1781                 /* Write data left in transmit buffer */
1782                 actual = nsc_ircc_pio_write(self->io.fir_base, 
1783                                            self->tx_buff.data, 
1784                                            self->tx_buff.len, 
1785                                            self->io.fifo_size);
1786                 self->tx_buff.data += actual;
1787                 self->tx_buff.len  -= actual;
1788                 
1789                 self->io.direction = IO_XMIT;
1790
1791                 /* Check if finished */
1792                 if (self->tx_buff.len > 0)
1793                         self->ier = IER_TXLDL_IE;
1794                 else { 
1795
1796                         self->stats.tx_packets++;
1797                         netif_wake_queue(self->netdev);
1798                         self->ier = IER_TXEMP_IE;
1799                 }
1800                         
1801         }
1802         /* Check if transmission has completed */
1803         if (eir & EIR_TXEMP_EV) {
1804                 /* Turn around and get ready to receive some data */
1805                 self->io.direction = IO_RECV;
1806                 self->ier = IER_RXHDL_IE;
1807                 /* Check if we need to change the speed?
1808                  * Need to be after self->io.direction to avoid race with
1809                  * nsc_ircc_hard_xmit_sir() - Jean II */
1810                 if (self->new_speed) {
1811                         IRDA_DEBUG(2, "%s(), Changing speed!\n", __FUNCTION__);
1812                         self->ier = nsc_ircc_change_speed(self,
1813                                                           self->new_speed);
1814                         self->new_speed = 0;
1815                         netif_wake_queue(self->netdev);
1816
1817                         /* Check if we are going to FIR */
1818                         if (self->io.speed > 115200) {
1819                                 /* No need to do anymore SIR stuff */
1820                                 return;
1821                         }
1822                 }
1823         }
1824
1825         /* Rx FIFO threshold or timeout */
1826         if (eir & EIR_RXHDL_EV) {
1827                 nsc_ircc_pio_receive(self);
1828
1829                 /* Keep receiving */
1830                 self->ier = IER_RXHDL_IE;
1831         }
1832 }
1833
1834 /*
1835  * Function nsc_ircc_fir_interrupt (self, eir)
1836  *
1837  *    Handle MIR/FIR interrupt
1838  *
1839  */
1840 static void nsc_ircc_fir_interrupt(struct nsc_ircc_cb *self, int iobase, 
1841                                    int eir)
1842 {
1843         __u8 bank;
1844
1845         bank = inb(iobase+BSR);
1846         
1847         /* Status FIFO event*/
1848         if (eir & EIR_SFIF_EV) {
1849                 /* Check if DMA has finished */
1850                 if (nsc_ircc_dma_receive_complete(self, iobase)) {
1851                         /* Wait for next status FIFO interrupt */
1852                         self->ier = IER_SFIF_IE;
1853                 } else {
1854                         self->ier = IER_SFIF_IE | IER_TMR_IE;
1855                 }
1856         } else if (eir & EIR_TMR_EV) { /* Timer finished */
1857                 /* Disable timer */
1858                 switch_bank(iobase, BANK4);
1859                 outb(0, iobase+IRCR1);
1860
1861                 /* Clear timer event */
1862                 switch_bank(iobase, BANK0);
1863                 outb(ASCR_CTE, iobase+ASCR);
1864
1865                 /* Check if this is a Tx timer interrupt */
1866                 if (self->io.direction == IO_XMIT) {
1867                         nsc_ircc_dma_xmit(self, iobase);
1868
1869                         /* Interrupt on DMA */
1870                         self->ier = IER_DMA_IE;
1871                 } else {
1872                         /* Check (again) if DMA has finished */
1873                         if (nsc_ircc_dma_receive_complete(self, iobase)) {
1874                                 self->ier = IER_SFIF_IE;
1875                         } else {
1876                                 self->ier = IER_SFIF_IE | IER_TMR_IE;
1877                         }
1878                 }
1879         } else if (eir & EIR_DMA_EV) {
1880                 /* Finished with all transmissions? */
1881                 if (nsc_ircc_dma_xmit_complete(self)) {
1882                         if(self->new_speed != 0) {
1883                                 /* As we stop the Tx queue, the speed change
1884                                  * need to be done when the Tx fifo is
1885                                  * empty. Ask for a Tx done interrupt */
1886                                 self->ier = IER_TXEMP_IE;
1887                         } else {
1888                                 /* Check if there are more frames to be
1889                                  * transmitted */
1890                                 if (irda_device_txqueue_empty(self->netdev)) {
1891                                         /* Prepare for receive */
1892                                         nsc_ircc_dma_receive(self);
1893                                         self->ier = IER_SFIF_IE;
1894                                 } else
1895                                         IRDA_WARNING("%s(), potential "
1896                                                      "Tx queue lockup !\n",
1897                                                      __FUNCTION__);
1898                         }
1899                 } else {
1900                         /*  Not finished yet, so interrupt on DMA again */
1901                         self->ier = IER_DMA_IE;
1902                 }
1903         } else if (eir & EIR_TXEMP_EV) {
1904                 /* The Tx FIFO has totally drained out, so now we can change
1905                  * the speed... - Jean II */
1906                 self->ier = nsc_ircc_change_speed(self, self->new_speed);
1907                 self->new_speed = 0;
1908                 netif_wake_queue(self->netdev);
1909                 /* Note : nsc_ircc_change_speed() restarted Rx fifo */
1910         }
1911
1912         outb(bank, iobase+BSR);
1913 }
1914
1915 /*
1916  * Function nsc_ircc_interrupt (irq, dev_id, regs)
1917  *
1918  *    An interrupt from the chip has arrived. Time to do some work
1919  *
1920  */
1921 static irqreturn_t nsc_ircc_interrupt(int irq, void *dev_id,
1922                                 struct pt_regs *regs)
1923 {
1924         struct net_device *dev = (struct net_device *) dev_id;
1925         struct nsc_ircc_cb *self;
1926         __u8 bsr, eir;
1927         int iobase;
1928
1929         if (!dev) {
1930                 IRDA_WARNING("%s: irq %d for unknown device.\n",
1931                              driver_name, irq);
1932                 return IRQ_NONE;
1933         }
1934         self = (struct nsc_ircc_cb *) dev->priv;
1935
1936         spin_lock(&self->lock); 
1937
1938         iobase = self->io.fir_base;
1939
1940         bsr = inb(iobase+BSR);  /* Save current bank */
1941
1942         switch_bank(iobase, BANK0);     
1943         self->ier = inb(iobase+IER); 
1944         eir = inb(iobase+EIR) & self->ier; /* Mask out the interesting ones */ 
1945
1946         outb(0, iobase+IER); /* Disable interrupts */
1947         
1948         if (eir) {
1949                 /* Dispatch interrupt handler for the current speed */
1950                 if (self->io.speed > 115200)
1951                         nsc_ircc_fir_interrupt(self, iobase, eir);
1952                 else
1953                         nsc_ircc_sir_interrupt(self, eir);
1954         }
1955         
1956         outb(self->ier, iobase+IER); /* Restore interrupts */
1957         outb(bsr, iobase+BSR);       /* Restore bank register */
1958
1959         spin_unlock(&self->lock);
1960         return IRQ_RETVAL(eir);
1961 }
1962
1963 /*
1964  * Function nsc_ircc_is_receiving (self)
1965  *
1966  *    Return TRUE is we are currently receiving a frame
1967  *
1968  */
1969 static int nsc_ircc_is_receiving(struct nsc_ircc_cb *self)
1970 {
1971         unsigned long flags;
1972         int status = FALSE;
1973         int iobase;
1974         __u8 bank;
1975
1976         IRDA_ASSERT(self != NULL, return FALSE;);
1977
1978         spin_lock_irqsave(&self->lock, flags);
1979
1980         if (self->io.speed > 115200) {
1981                 iobase = self->io.fir_base;
1982
1983                 /* Check if rx FIFO is not empty */
1984                 bank = inb(iobase+BSR);
1985                 switch_bank(iobase, BANK2);
1986                 if ((inb(iobase+RXFLV) & 0x3f) != 0) {
1987                         /* We are receiving something */
1988                         status =  TRUE;
1989                 }
1990                 outb(bank, iobase+BSR);
1991         } else 
1992                 status = (self->rx_buff.state != OUTSIDE_FRAME);
1993         
1994         spin_unlock_irqrestore(&self->lock, flags);
1995
1996         return status;
1997 }
1998
1999 /*
2000  * Function nsc_ircc_net_open (dev)
2001  *
2002  *    Start the device
2003  *
2004  */
2005 static int nsc_ircc_net_open(struct net_device *dev)
2006 {
2007         struct nsc_ircc_cb *self;
2008         int iobase;
2009         char hwname[32];
2010         __u8 bank;
2011         
2012         IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
2013         
2014         IRDA_ASSERT(dev != NULL, return -1;);
2015         self = (struct nsc_ircc_cb *) dev->priv;
2016         
2017         IRDA_ASSERT(self != NULL, return 0;);
2018         
2019         iobase = self->io.fir_base;
2020         
2021         if (request_irq(self->io.irq, nsc_ircc_interrupt, 0, dev->name, dev)) {
2022                 IRDA_WARNING("%s, unable to allocate irq=%d\n",
2023                              driver_name, self->io.irq);
2024                 return -EAGAIN;
2025         }
2026         /*
2027          * Always allocate the DMA channel after the IRQ, and clean up on 
2028          * failure.
2029          */
2030         if (request_dma(self->io.dma, dev->name)) {
2031                 IRDA_WARNING("%s, unable to allocate dma=%d\n",
2032                              driver_name, self->io.dma);
2033                 free_irq(self->io.irq, dev);
2034                 return -EAGAIN;
2035         }
2036         
2037         /* Save current bank */
2038         bank = inb(iobase+BSR);
2039         
2040         /* turn on interrupts */
2041         switch_bank(iobase, BANK0);
2042         outb(IER_LS_IE | IER_RXHDL_IE, iobase+IER);
2043
2044         /* Restore bank register */
2045         outb(bank, iobase+BSR);
2046
2047         /* Ready to play! */
2048         netif_start_queue(dev);
2049         
2050         /* Give self a hardware name */
2051         sprintf(hwname, "NSC-FIR @ 0x%03x", self->io.fir_base);
2052
2053         /* 
2054          * Open new IrLAP layer instance, now that everything should be
2055          * initialized properly 
2056          */
2057         self->irlap = irlap_open(dev, &self->qos, hwname);
2058
2059         return 0;
2060 }
2061
2062 /*
2063  * Function nsc_ircc_net_close (dev)
2064  *
2065  *    Stop the device
2066  *
2067  */
2068 static int nsc_ircc_net_close(struct net_device *dev)
2069 {
2070         struct nsc_ircc_cb *self;
2071         int iobase;
2072         __u8 bank;
2073
2074         IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
2075         
2076         IRDA_ASSERT(dev != NULL, return -1;);
2077
2078         self = (struct nsc_ircc_cb *) dev->priv;
2079         IRDA_ASSERT(self != NULL, return 0;);
2080
2081         /* Stop device */
2082         netif_stop_queue(dev);
2083         
2084         /* Stop and remove instance of IrLAP */
2085         if (self->irlap)
2086                 irlap_close(self->irlap);
2087         self->irlap = NULL;
2088         
2089         iobase = self->io.fir_base;
2090
2091         disable_dma(self->io.dma);
2092
2093         /* Save current bank */
2094         bank = inb(iobase+BSR);
2095
2096         /* Disable interrupts */
2097         switch_bank(iobase, BANK0);
2098         outb(0, iobase+IER); 
2099        
2100         free_irq(self->io.irq, dev);
2101         free_dma(self->io.dma);
2102
2103         /* Restore bank register */
2104         outb(bank, iobase+BSR);
2105
2106         return 0;
2107 }
2108
2109 /*
2110  * Function nsc_ircc_net_ioctl (dev, rq, cmd)
2111  *
2112  *    Process IOCTL commands for this device
2113  *
2114  */
2115 static int nsc_ircc_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2116 {
2117         struct if_irda_req *irq = (struct if_irda_req *) rq;
2118         struct nsc_ircc_cb *self;
2119         unsigned long flags;
2120         int ret = 0;
2121
2122         IRDA_ASSERT(dev != NULL, return -1;);
2123
2124         self = dev->priv;
2125
2126         IRDA_ASSERT(self != NULL, return -1;);
2127
2128         IRDA_DEBUG(2, "%s(), %s, (cmd=0x%X)\n", __FUNCTION__, dev->name, cmd);
2129         
2130         switch (cmd) {
2131         case SIOCSBANDWIDTH: /* Set bandwidth */
2132                 if (!capable(CAP_NET_ADMIN)) {
2133                         ret = -EPERM;
2134                         break;
2135                 }
2136                 spin_lock_irqsave(&self->lock, flags);
2137                 nsc_ircc_change_speed(self, irq->ifr_baudrate);
2138                 spin_unlock_irqrestore(&self->lock, flags);
2139                 break;
2140         case SIOCSMEDIABUSY: /* Set media busy */
2141                 if (!capable(CAP_NET_ADMIN)) {
2142                         ret = -EPERM;
2143                         break;
2144                 }
2145                 irda_device_set_media_busy(self->netdev, TRUE);
2146                 break;
2147         case SIOCGRECEIVING: /* Check if we are receiving right now */
2148                 /* This is already protected */
2149                 irq->ifr_receiving = nsc_ircc_is_receiving(self);
2150                 break;
2151         default:
2152                 ret = -EOPNOTSUPP;
2153         }
2154         return ret;
2155 }
2156
2157 static struct net_device_stats *nsc_ircc_net_get_stats(struct net_device *dev)
2158 {
2159         struct nsc_ircc_cb *self = (struct nsc_ircc_cb *) dev->priv;
2160         
2161         return &self->stats;
2162 }
2163
2164 static void nsc_ircc_suspend(struct nsc_ircc_cb *self)
2165 {
2166         IRDA_MESSAGE("%s, Suspending\n", driver_name);
2167
2168         if (self->io.suspended)
2169                 return;
2170
2171         nsc_ircc_net_close(self->netdev);
2172
2173         self->io.suspended = 1;
2174 }
2175
2176 static void nsc_ircc_wakeup(struct nsc_ircc_cb *self)
2177 {
2178         if (!self->io.suspended)
2179                 return;
2180
2181         nsc_ircc_setup(&self->io);
2182         nsc_ircc_net_open(self->netdev);
2183         
2184         IRDA_MESSAGE("%s, Waking up\n", driver_name);
2185
2186         self->io.suspended = 0;
2187 }
2188
2189 static int nsc_ircc_pmproc(struct pm_dev *dev, pm_request_t rqst, void *data)
2190 {
2191         struct nsc_ircc_cb *self = (struct nsc_ircc_cb*) dev->data;
2192         if (self) {
2193                 switch (rqst) {
2194                 case PM_SUSPEND:
2195                         nsc_ircc_suspend(self);
2196                         break;
2197                 case PM_RESUME:
2198                         nsc_ircc_wakeup(self);
2199                         break;
2200                 }
2201         }
2202         return 0;
2203 }
2204
2205 MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
2206 MODULE_DESCRIPTION("NSC IrDA Device Driver");
2207 MODULE_LICENSE("GPL");
2208
2209
2210 module_param(qos_mtt_bits, int, 0);
2211 MODULE_PARM_DESC(qos_mtt_bits, "Minimum Turn Time");
2212 module_param_array(io, int, NULL, 0);
2213 MODULE_PARM_DESC(io, "Base I/O addresses");
2214 module_param_array(irq, int, NULL, 0);
2215 MODULE_PARM_DESC(irq, "IRQ lines");
2216 module_param_array(dma, int, NULL, 0);
2217 MODULE_PARM_DESC(dma, "DMA channels");
2218 module_param(dongle_id, int, 0);
2219 MODULE_PARM_DESC(dongle_id, "Type-id of used dongle");
2220
2221 module_init(nsc_ircc_init);
2222 module_exit(nsc_ircc_cleanup);
2223