1 /*********************************************************************
5 * Description: Driver for the NSC PC'108 and PC'338 IrDA chipsets
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>
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
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.
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.
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!
32 * bank = inb(iobase+BSR);
34 * do_your_stuff_here();
36 * outb(bank, iobase+BSR);
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
42 ********************************************************************/
44 #include <linux/module.h>
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>
58 #include <asm/byteorder.h>
62 #include <net/irda/wrapper.h>
63 #include <net/irda/irda.h>
64 #include <net/irda/irda_device.h>
68 #define CHIP_IO_EXTENT 8
69 #define BROKEN_DONGLE_ID
71 static char *driver_name = "nsc-ircc";
73 /* Module parameters */
74 static int qos_mtt_bits = 0x07; /* 1 ms or more */
77 /* Use BIOS settions by default, but user may supply module parameters */
78 static unsigned int io[] = { ~0, ~0, ~0, ~0 };
79 static unsigned int irq[] = { 0, 0, 0, 0, 0 };
80 static unsigned int dma[] = { 0, 0, 0, 0, 0 };
82 static int nsc_ircc_probe_108(nsc_chip_t *chip, chipio_t *info);
83 static int nsc_ircc_probe_338(nsc_chip_t *chip, chipio_t *info);
84 static int nsc_ircc_probe_39x(nsc_chip_t *chip, chipio_t *info);
85 static int nsc_ircc_init_108(nsc_chip_t *chip, chipio_t *info);
86 static int nsc_ircc_init_338(nsc_chip_t *chip, chipio_t *info);
87 static int nsc_ircc_init_39x(nsc_chip_t *chip, chipio_t *info);
89 /* These are the known NSC chips */
90 static nsc_chip_t chips[] = {
91 /* Name, {cfg registers}, chip id index reg, chip id expected value, revision mask */
92 { "PC87108", { 0x150, 0x398, 0xea }, 0x05, 0x10, 0xf0,
93 nsc_ircc_probe_108, nsc_ircc_init_108 },
94 { "PC87338", { 0x398, 0x15c, 0x2e }, 0x08, 0xb0, 0xf8,
95 nsc_ircc_probe_338, nsc_ircc_init_338 },
96 /* Contributed by Jan Frey - IBM A30/A31 */
97 { "PC8739x", { 0x2e, 0x4e, 0x0 }, 0x20, 0xea, 0xff,
98 nsc_ircc_probe_39x, nsc_ircc_init_39x },
101 /* Probably bogus, "PC8739x" should be the real thing. Jean II */
102 /* Contributed by Kevin Thayer - OmniBook 6100 */
103 { "PC87338?", { 0x2e, 0x15c, 0x398 }, 0x08, 0x00, 0xf8,
104 nsc_ircc_probe_338, nsc_ircc_init_338 },
108 /* Max 4 instances for now */
109 static struct nsc_ircc_cb *dev_self[] = { NULL, NULL, NULL, NULL };
111 static char *dongle_types[] = {
112 "Differential serial interface",
113 "Differential serial interface",
118 "Single-ended serial interface",
120 "HP HSDL-2300, HP HSDL-3600/HSDL-3610",
121 "IBM31T1100 or Temic TFDS6000/TFDS6500",
124 "HP HSDL-1100/HSDL-2100",
125 "HP HSDL-1100/HSDL-2100",
126 "Supports SIR Mode only",
127 "No dongle connected",
130 /* Some prototypes */
131 static int nsc_ircc_open(int i, chipio_t *info);
132 static int nsc_ircc_close(struct nsc_ircc_cb *self);
133 static int nsc_ircc_setup(chipio_t *info);
134 static void nsc_ircc_pio_receive(struct nsc_ircc_cb *self);
135 static int nsc_ircc_dma_receive(struct nsc_ircc_cb *self);
136 static int nsc_ircc_dma_receive_complete(struct nsc_ircc_cb *self, int iobase);
137 static int nsc_ircc_hard_xmit_sir(struct sk_buff *skb, struct net_device *dev);
138 static int nsc_ircc_hard_xmit_fir(struct sk_buff *skb, struct net_device *dev);
139 static int nsc_ircc_pio_write(int iobase, __u8 *buf, int len, int fifo_size);
140 static void nsc_ircc_dma_xmit(struct nsc_ircc_cb *self, int iobase);
141 static __u8 nsc_ircc_change_speed(struct nsc_ircc_cb *self, __u32 baud);
142 static int nsc_ircc_is_receiving(struct nsc_ircc_cb *self);
143 static int nsc_ircc_read_dongle_id (int iobase);
144 static void nsc_ircc_init_dongle_interface (int iobase, int dongle_id);
146 static int nsc_ircc_net_open(struct net_device *dev);
147 static int nsc_ircc_net_close(struct net_device *dev);
148 static int nsc_ircc_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
149 static struct net_device_stats *nsc_ircc_net_get_stats(struct net_device *dev);
150 static int nsc_ircc_pmproc(struct pm_dev *dev, pm_request_t rqst, void *data);
153 * Function nsc_ircc_init ()
155 * Initialize chip. Just try to find out how many chips we are dealing with
158 static int __init nsc_ircc_init(void)
168 /* Probe for all the NSC chipsets we know about */
169 for (chip=chips; chip->name ; chip++) {
170 IRDA_DEBUG(2, "%s(), Probing for %s ...\n", __FUNCTION__,
173 /* Try all config registers for this chip */
174 for (cfg=0; cfg<3; cfg++) {
175 cfg_base = chip->cfg[cfg];
179 memset(&info, 0, sizeof(chipio_t));
180 info.cfg_base = cfg_base;
181 info.fir_base = io[i];
185 /* Read index register */
188 IRDA_DEBUG(2, "%s() no chip at 0x%03x\n", __FUNCTION__, cfg_base);
192 /* Read chip identification register */
193 outb(chip->cid_index, cfg_base);
194 id = inb(cfg_base+1);
195 if ((id & chip->cid_mask) == chip->cid_value) {
196 IRDA_DEBUG(2, "%s() Found %s chip, revision=%d\n",
197 __FUNCTION__, chip->name, id & ~chip->cid_mask);
199 * If the user supplies the base address, then
200 * we init the chip, if not we probe the values
203 if (io[i] < 0x2000) {
204 chip->init(chip, &info);
206 chip->probe(chip, &info);
208 if (nsc_ircc_open(i, &info) == 0)
212 IRDA_DEBUG(2, "%s(), Wrong chip id=0x%02x\n", __FUNCTION__, id);
222 * Function nsc_ircc_cleanup ()
224 * Close all configured chips
227 static void __exit nsc_ircc_cleanup(void)
231 pm_unregister_all(nsc_ircc_pmproc);
233 for (i=0; i < 4; i++) {
235 nsc_ircc_close(dev_self[i]);
240 * Function nsc_ircc_open (iobase, irq)
242 * Open driver instance
245 static int __init nsc_ircc_open(int i, chipio_t *info)
247 struct net_device *dev;
248 struct nsc_ircc_cb *self;
249 struct pm_dev *pmdev;
253 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
255 MESSAGE("%s, Found chip at base=0x%03x\n", driver_name,
258 if ((nsc_ircc_setup(info)) == -1)
261 MESSAGE("%s, driver loaded (Dag Brattli)\n", driver_name);
263 dev = alloc_irdadev(sizeof(struct nsc_ircc_cb));
265 ERROR("%s(), can't allocate memory for "
266 "control block!\n", __FUNCTION__);
272 spin_lock_init(&self->lock);
274 /* Need to store self somewhere */
279 self->io.cfg_base = info->cfg_base;
280 self->io.fir_base = info->fir_base;
281 self->io.irq = info->irq;
282 self->io.fir_ext = CHIP_IO_EXTENT;
283 self->io.dma = info->dma;
284 self->io.fifo_size = 32;
286 /* Reserve the ioports that we need */
287 ret = request_region(self->io.fir_base, self->io.fir_ext, driver_name);
289 WARNING("%s(), can't get iobase of 0x%03x\n",
290 __FUNCTION__, self->io.fir_base);
295 /* Initialize QoS for this device */
296 irda_init_max_qos_capabilies(&self->qos);
298 /* The only value we must override it the baudrate */
299 self->qos.baud_rate.bits = IR_9600|IR_19200|IR_38400|IR_57600|
300 IR_115200|IR_576000|IR_1152000 |(IR_4000000 << 8);
302 self->qos.min_turn_time.bits = qos_mtt_bits;
303 irda_qos_bits_to_value(&self->qos);
305 /* Max DMA buffer size needed = (data_size + 6) * (window_size) + 6; */
306 self->rx_buff.truesize = 14384;
307 self->tx_buff.truesize = 14384;
309 /* Allocate memory if needed */
310 self->rx_buff.head = (__u8 *) kmalloc(self->rx_buff.truesize,
312 if (self->rx_buff.head == NULL) {
317 memset(self->rx_buff.head, 0, self->rx_buff.truesize);
319 self->tx_buff.head = (__u8 *) kmalloc(self->tx_buff.truesize,
321 if (self->tx_buff.head == NULL) {
325 memset(self->tx_buff.head, 0, self->tx_buff.truesize);
327 self->rx_buff.in_frame = FALSE;
328 self->rx_buff.state = OUTSIDE_FRAME;
329 self->tx_buff.data = self->tx_buff.head;
330 self->rx_buff.data = self->rx_buff.head;
332 /* Reset Tx queue info */
333 self->tx_fifo.len = self->tx_fifo.ptr = self->tx_fifo.free = 0;
334 self->tx_fifo.tail = self->tx_buff.head;
336 /* Override the network functions we need to use */
337 SET_MODULE_OWNER(dev);
338 dev->hard_start_xmit = nsc_ircc_hard_xmit_sir;
339 dev->open = nsc_ircc_net_open;
340 dev->stop = nsc_ircc_net_close;
341 dev->do_ioctl = nsc_ircc_net_ioctl;
342 dev->get_stats = nsc_ircc_net_get_stats;
344 err = register_netdev(dev);
346 ERROR("%s(), register_netdev() failed!\n", __FUNCTION__);
349 MESSAGE("IrDA: Registered device %s\n", dev->name);
351 /* Check if user has supplied the dongle id or not */
353 dongle_id = nsc_ircc_read_dongle_id(self->io.fir_base);
355 MESSAGE("%s, Found dongle: %s\n", driver_name,
356 dongle_types[dongle_id]);
358 MESSAGE("%s, Using dongle: %s\n", driver_name,
359 dongle_types[dongle_id]);
362 self->io.dongle_id = dongle_id;
363 nsc_ircc_init_dongle_interface(self->io.fir_base, dongle_id);
365 pmdev = pm_register(PM_SYS_DEV, PM_SYS_IRDA, nsc_ircc_pmproc);
371 kfree(self->tx_buff.head);
373 kfree(self->rx_buff.head);
375 release_region(self->io.fir_base, self->io.fir_ext);
383 * Function nsc_ircc_close (self)
385 * Close driver instance
388 static int __exit nsc_ircc_close(struct nsc_ircc_cb *self)
392 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
394 ASSERT(self != NULL, return -1;);
396 iobase = self->io.fir_base;
398 /* Remove netdevice */
399 unregister_netdev(self->netdev);
401 /* Release the PORT that this driver is using */
402 IRDA_DEBUG(4, "%s(), Releasing Region %03x\n",
403 __FUNCTION__, self->io.fir_base);
404 release_region(self->io.fir_base, self->io.fir_ext);
406 if (self->tx_buff.head)
407 kfree(self->tx_buff.head);
409 if (self->rx_buff.head)
410 kfree(self->rx_buff.head);
412 dev_self[self->index] = NULL;
413 free_netdev(self->netdev);
419 * Function nsc_ircc_init_108 (iobase, cfg_base, irq, dma)
421 * Initialize the NSC '108 chip
424 static int nsc_ircc_init_108(nsc_chip_t *chip, chipio_t *info)
426 int cfg_base = info->cfg_base;
429 outb(2, cfg_base); /* Mode Control Register (MCTL) */
430 outb(0x00, cfg_base+1); /* Disable device */
432 /* Base Address and Interrupt Control Register (BAIC) */
433 outb(CFG_108_BAIC, cfg_base);
434 switch (info->fir_base) {
435 case 0x3e8: outb(0x14, cfg_base+1); break;
436 case 0x2e8: outb(0x15, cfg_base+1); break;
437 case 0x3f8: outb(0x16, cfg_base+1); break;
438 case 0x2f8: outb(0x17, cfg_base+1); break;
439 default: ERROR("%s(), invalid base_address", __FUNCTION__);
442 /* Control Signal Routing Register (CSRT) */
444 case 3: temp = 0x01; break;
445 case 4: temp = 0x02; break;
446 case 5: temp = 0x03; break;
447 case 7: temp = 0x04; break;
448 case 9: temp = 0x05; break;
449 case 11: temp = 0x06; break;
450 case 15: temp = 0x07; break;
451 default: ERROR("%s(), invalid irq", __FUNCTION__);
453 outb(CFG_108_CSRT, cfg_base);
456 case 0: outb(0x08+temp, cfg_base+1); break;
457 case 1: outb(0x10+temp, cfg_base+1); break;
458 case 3: outb(0x18+temp, cfg_base+1); break;
459 default: ERROR("%s(), invalid dma", __FUNCTION__);
462 outb(CFG_108_MCTL, cfg_base); /* Mode Control Register (MCTL) */
463 outb(0x03, cfg_base+1); /* Enable device */
469 * Function nsc_ircc_probe_108 (chip, info)
474 static int nsc_ircc_probe_108(nsc_chip_t *chip, chipio_t *info)
476 int cfg_base = info->cfg_base;
479 /* Read address and interrupt control register (BAIC) */
480 outb(CFG_108_BAIC, cfg_base);
481 reg = inb(cfg_base+1);
483 switch (reg & 0x03) {
485 info->fir_base = 0x3e8;
488 info->fir_base = 0x2e8;
491 info->fir_base = 0x3f8;
494 info->fir_base = 0x2f8;
497 info->sir_base = info->fir_base;
498 IRDA_DEBUG(2, "%s(), probing fir_base=0x%03x\n", __FUNCTION__,
501 /* Read control signals routing register (CSRT) */
502 outb(CFG_108_CSRT, cfg_base);
503 reg = inb(cfg_base+1);
505 switch (reg & 0x07) {
531 IRDA_DEBUG(2, "%s(), probing irq=%d\n", __FUNCTION__, info->irq);
533 /* Currently we only read Rx DMA but it will also be used for Tx */
534 switch ((reg >> 3) & 0x03) {
548 IRDA_DEBUG(2, "%s(), probing dma=%d\n", __FUNCTION__, info->dma);
550 /* Read mode control register (MCTL) */
551 outb(CFG_108_MCTL, cfg_base);
552 reg = inb(cfg_base+1);
554 info->enabled = reg & 0x01;
555 info->suspended = !((reg >> 1) & 0x01);
561 * Function nsc_ircc_init_338 (chip, info)
563 * Initialize the NSC '338 chip. Remember that the 87338 needs two
564 * consecutive writes to the data registers while CPU interrupts are
565 * disabled. The 97338 does not require this, but shouldn't be any
566 * harm if we do it anyway.
568 static int nsc_ircc_init_338(nsc_chip_t *chip, chipio_t *info)
576 * Function nsc_ircc_probe_338 (chip, info)
581 static int nsc_ircc_probe_338(nsc_chip_t *chip, chipio_t *info)
583 int cfg_base = info->cfg_base;
587 /* Read funtion enable register (FER) */
588 outb(CFG_338_FER, cfg_base);
589 reg = inb(cfg_base+1);
591 info->enabled = (reg >> 2) & 0x01;
593 /* Check if we are in Legacy or PnP mode */
594 outb(CFG_338_PNP0, cfg_base);
595 reg = inb(cfg_base+1);
597 pnp = (reg >> 3) & 0x01;
599 IRDA_DEBUG(2, "(), Chip is in PnP mode\n");
600 outb(0x46, cfg_base);
601 reg = (inb(cfg_base+1) & 0xfe) << 2;
603 outb(0x47, cfg_base);
604 reg |= ((inb(cfg_base+1) & 0xfc) << 8);
606 info->fir_base = reg;
608 /* Read function address register (FAR) */
609 outb(CFG_338_FAR, cfg_base);
610 reg = inb(cfg_base+1);
612 switch ((reg >> 4) & 0x03) {
614 info->fir_base = 0x3f8;
617 info->fir_base = 0x2f8;
628 switch ((reg >> 6) & 0x03) {
631 info->fir_base = 0x3e8;
633 info->fir_base = 0x2e8;
637 info->fir_base = 0x338;
639 info->fir_base = 0x238;
643 info->fir_base = 0x2e8;
645 info->fir_base = 0x2e0;
649 info->fir_base = 0x220;
651 info->fir_base = 0x228;
656 info->sir_base = info->fir_base;
658 /* Read PnP register 1 (PNP1) */
659 outb(CFG_338_PNP1, cfg_base);
660 reg = inb(cfg_base+1);
662 info->irq = reg >> 4;
664 /* Read PnP register 3 (PNP3) */
665 outb(CFG_338_PNP3, cfg_base);
666 reg = inb(cfg_base+1);
668 info->dma = (reg & 0x07) - 1;
670 /* Read power and test register (PTR) */
671 outb(CFG_338_PTR, cfg_base);
672 reg = inb(cfg_base+1);
674 info->suspended = reg & 0x01;
681 * Function nsc_ircc_init_39x (chip, info)
683 * Now that we know it's a '39x (see probe below), we need to
684 * configure it so we can use it.
686 * The NSC '338 chip is a Super I/O chip with a "bank" architecture,
687 * the configuration of the different functionality (serial, parallel,
688 * floppy...) are each in a different bank (Logical Device Number).
689 * The base address, irq and dma configuration registers are common
690 * to all functionalities (index 0x30 to 0x7F).
691 * There is only one configuration register specific to the
692 * serial port, CFG_39X_SPC.
695 * Note : this code was written by Jan Frey <janfrey@web.de>
697 static int nsc_ircc_init_39x(nsc_chip_t *chip, chipio_t *info)
699 int cfg_base = info->cfg_base;
702 /* User is shure about his config... accept it. */
703 IRDA_DEBUG(2, "%s(): nsc_ircc_init_39x (user settings): "
704 "io=0x%04x, irq=%d, dma=%d\n",
705 __FUNCTION__, info->fir_base, info->irq, info->dma);
707 /* Access bank for SP2 */
708 outb(CFG_39X_LDN, cfg_base);
709 outb(0x02, cfg_base+1);
713 /* We want to enable the device if not enabled */
714 outb(CFG_39X_ACT, cfg_base);
715 enabled = inb(cfg_base+1) & 0x01;
718 /* Enable the device */
719 outb(CFG_39X_SIOCF1, cfg_base);
720 outb(0x01, cfg_base+1);
721 /* May want to update info->enabled. Jean II */
724 /* Enable UART bank switching (bit 7) ; Sets the chip to normal
725 * power mode (wake up from sleep mode) (bit 1) */
726 outb(CFG_39X_SPC, cfg_base);
727 outb(0x82, cfg_base+1);
733 * Function nsc_ircc_probe_39x (chip, info)
735 * Test if we really have a '39x chip at the given address
737 * Note : this code was written by Jan Frey <janfrey@web.de>
739 static int nsc_ircc_probe_39x(nsc_chip_t *chip, chipio_t *info)
741 int cfg_base = info->cfg_base;
742 int reg1, reg2, irq, irqt, dma1, dma2;
745 IRDA_DEBUG(2, "%s(), nsc_ircc_probe_39x, base=%d\n",
746 __FUNCTION__, cfg_base);
748 /* This function should be executed with irq off to avoid
749 * another driver messing with the Super I/O bank - Jean II */
751 /* Access bank for SP2 */
752 outb(CFG_39X_LDN, cfg_base);
753 outb(0x02, cfg_base+1);
755 /* Read infos about SP2 ; store in info struct */
756 outb(CFG_39X_BASEH, cfg_base);
757 reg1 = inb(cfg_base+1);
758 outb(CFG_39X_BASEL, cfg_base);
759 reg2 = inb(cfg_base+1);
760 info->fir_base = (reg1 << 8) | reg2;
762 outb(CFG_39X_IRQNUM, cfg_base);
763 irq = inb(cfg_base+1);
764 outb(CFG_39X_IRQSEL, cfg_base);
765 irqt = inb(cfg_base+1);
768 outb(CFG_39X_DMA0, cfg_base);
769 dma1 = inb(cfg_base+1);
770 outb(CFG_39X_DMA1, cfg_base);
771 dma2 = inb(cfg_base+1);
774 outb(CFG_39X_ACT, cfg_base);
775 info->enabled = enabled = inb(cfg_base+1) & 0x01;
777 outb(CFG_39X_SPC, cfg_base);
778 susp = 1 - ((inb(cfg_base+1) & 0x02) >> 1);
780 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);
784 /* We want to enable the device if not enabled */
785 outb(CFG_39X_ACT, cfg_base);
786 enabled = inb(cfg_base+1) & 0x01;
789 /* Enable the device */
790 outb(CFG_39X_SIOCF1, cfg_base);
791 outb(0x01, cfg_base+1);
792 /* May want to update info->enabled. Jean II */
795 /* Enable UART bank switching (bit 7) ; Sets the chip to normal
796 * power mode (wake up from sleep mode) (bit 1) */
797 outb(CFG_39X_SPC, cfg_base);
798 outb(0x82, cfg_base+1);
804 * Function nsc_ircc_setup (info)
806 * Returns non-negative on success.
809 static int nsc_ircc_setup(chipio_t *info)
812 int iobase = info->fir_base;
814 /* Read the Module ID */
815 switch_bank(iobase, BANK3);
816 version = inb(iobase+MID);
818 IRDA_DEBUG(2, "%s() Driver %s Found chip version %02x\n",
819 __FUNCTION__, driver_name, version);
822 if (0x20 != (version & 0xf0)) {
823 ERROR("%s, Wrong chip version %02x\n", driver_name, version);
827 /* Switch to advanced mode */
828 switch_bank(iobase, BANK2);
829 outb(ECR1_EXT_SL, iobase+ECR1);
830 switch_bank(iobase, BANK0);
832 /* Set FIFO threshold to TX17, RX16, reset and enable FIFO's */
833 switch_bank(iobase, BANK0);
834 outb(FCR_RXTH|FCR_TXTH|FCR_TXSR|FCR_RXSR|FCR_FIFO_EN, iobase+FCR);
836 outb(0x03, iobase+LCR); /* 8 bit word length */
837 outb(MCR_SIR, iobase+MCR); /* Start at SIR-mode, also clears LSR*/
839 /* Set FIFO size to 32 */
840 switch_bank(iobase, BANK2);
841 outb(EXCR2_RFSIZ|EXCR2_TFSIZ, iobase+EXCR2);
843 /* IRCR2: FEND_MD is not set */
844 switch_bank(iobase, BANK5);
845 outb(0x02, iobase+4);
847 /* Make sure that some defaults are OK */
848 switch_bank(iobase, BANK6);
849 outb(0x20, iobase+0); /* Set 32 bits FIR CRC */
850 outb(0x0a, iobase+1); /* Set MIR pulse width */
851 outb(0x0d, iobase+2); /* Set SIR pulse width to 1.6us */
852 outb(0x2a, iobase+4); /* Set beginning frag, and preamble length */
854 /* Enable receive interrupts */
855 switch_bank(iobase, BANK0);
856 outb(IER_RXHDL_IE, iobase+IER);
862 * Function nsc_ircc_read_dongle_id (void)
864 * Try to read dongle indentification. This procedure needs to be executed
865 * once after power-on/reset. It also needs to be used whenever you suspect
866 * that the user may have plugged/unplugged the IrDA Dongle.
868 static int nsc_ircc_read_dongle_id (int iobase)
873 bank = inb(iobase+BSR);
876 switch_bank(iobase, BANK7);
878 /* IRCFG4: IRSL0_DS and IRSL21_DS are cleared */
879 outb(0x00, iobase+7);
881 /* ID0, 1, and 2 are pulled up/down very slowly */
884 /* IRCFG1: read the ID bits */
885 dongle_id = inb(iobase+4) & 0x0f;
887 #ifdef BROKEN_DONGLE_ID
888 if (dongle_id == 0x0a)
891 /* Go back to bank 0 before returning */
892 switch_bank(iobase, BANK0);
894 outb(bank, iobase+BSR);
900 * Function nsc_ircc_init_dongle_interface (iobase, dongle_id)
902 * This function initializes the dongle for the transceiver that is
903 * used. This procedure needs to be executed once after
904 * power-on/reset. It also needs to be used whenever you suspect that
905 * the dongle is changed.
907 static void nsc_ircc_init_dongle_interface (int iobase, int dongle_id)
911 /* Save current bank */
912 bank = inb(iobase+BSR);
915 switch_bank(iobase, BANK7);
917 /* IRCFG4: set according to dongle_id */
919 case 0x00: /* same as */
920 case 0x01: /* Differential serial interface */
921 IRDA_DEBUG(0, "%s(), %s not defined by irda yet\n",
922 __FUNCTION__, dongle_types[dongle_id]);
924 case 0x02: /* same as */
925 case 0x03: /* Reserved */
926 IRDA_DEBUG(0, "%s(), %s not defined by irda yet\n",
927 __FUNCTION__, dongle_types[dongle_id]);
929 case 0x04: /* Sharp RY5HD01 */
931 case 0x05: /* Reserved, but this is what the Thinkpad reports */
932 IRDA_DEBUG(0, "%s(), %s not defined by irda yet\n",
933 __FUNCTION__, dongle_types[dongle_id]);
935 case 0x06: /* Single-ended serial interface */
936 IRDA_DEBUG(0, "%s(), %s not defined by irda yet\n",
937 __FUNCTION__, dongle_types[dongle_id]);
939 case 0x07: /* Consumer-IR only */
940 IRDA_DEBUG(0, "%s(), %s is not for IrDA mode\n",
941 __FUNCTION__, dongle_types[dongle_id]);
943 case 0x08: /* HP HSDL-2300, HP HSDL-3600/HSDL-3610 */
944 IRDA_DEBUG(0, "%s(), %s\n",
945 __FUNCTION__, dongle_types[dongle_id]);
947 case 0x09: /* IBM31T1100 or Temic TFDS6000/TFDS6500 */
948 outb(0x28, iobase+7); /* Set irsl[0-2] as output */
950 case 0x0A: /* same as */
951 case 0x0B: /* Reserved */
952 IRDA_DEBUG(0, "%s(), %s not defined by irda yet\n",
953 __FUNCTION__, dongle_types[dongle_id]);
955 case 0x0C: /* same as */
956 case 0x0D: /* HP HSDL-1100/HSDL-2100 */
958 * Set irsl0 as input, irsl[1-2] as output, and separate
959 * inputs are used for SIR and MIR/FIR
961 outb(0x48, iobase+7);
963 case 0x0E: /* Supports SIR Mode only */
964 outb(0x28, iobase+7); /* Set irsl[0-2] as output */
966 case 0x0F: /* No dongle connected */
967 IRDA_DEBUG(0, "%s(), %s\n",
968 __FUNCTION__, dongle_types[dongle_id]);
970 switch_bank(iobase, BANK0);
971 outb(0x62, iobase+MCR);
974 IRDA_DEBUG(0, "%s(), invalid dongle_id %#x",
975 __FUNCTION__, dongle_id);
978 /* IRCFG1: IRSL1 and 2 are set to IrDA mode */
979 outb(0x00, iobase+4);
981 /* Restore bank register */
982 outb(bank, iobase+BSR);
984 } /* set_up_dongle_interface */
987 * Function nsc_ircc_change_dongle_speed (iobase, speed, dongle_id)
989 * Change speed of the attach dongle
992 static void nsc_ircc_change_dongle_speed(int iobase, int speed, int dongle_id)
996 /* Save current bank */
997 bank = inb(iobase+BSR);
1000 switch_bank(iobase, BANK7);
1002 /* IRCFG1: set according to dongle_id */
1003 switch (dongle_id) {
1004 case 0x00: /* same as */
1005 case 0x01: /* Differential serial interface */
1006 IRDA_DEBUG(0, "%s(), %s not defined by irda yet\n",
1007 __FUNCTION__, dongle_types[dongle_id]);
1009 case 0x02: /* same as */
1010 case 0x03: /* Reserved */
1011 IRDA_DEBUG(0, "%s(), %s not defined by irda yet\n",
1012 __FUNCTION__, dongle_types[dongle_id]);
1014 case 0x04: /* Sharp RY5HD01 */
1016 case 0x05: /* Reserved */
1017 IRDA_DEBUG(0, "%s(), %s not defined by irda yet\n",
1018 __FUNCTION__, dongle_types[dongle_id]);
1020 case 0x06: /* Single-ended serial interface */
1021 IRDA_DEBUG(0, "%s(), %s not defined by irda yet\n",
1022 __FUNCTION__, dongle_types[dongle_id]);
1024 case 0x07: /* Consumer-IR only */
1025 IRDA_DEBUG(0, "%s(), %s is not for IrDA mode\n",
1026 __FUNCTION__, dongle_types[dongle_id]);
1028 case 0x08: /* HP HSDL-2300, HP HSDL-3600/HSDL-3610 */
1029 IRDA_DEBUG(0, "%s(), %s\n",
1030 __FUNCTION__, dongle_types[dongle_id]);
1031 outb(0x00, iobase+4);
1033 outb(0x01, iobase+4);
1035 case 0x09: /* IBM31T1100 or Temic TFDS6000/TFDS6500 */
1036 outb(0x01, iobase+4);
1038 if (speed == 4000000) {
1039 /* There was a cli() there, but we now are already
1040 * under spin_lock_irqsave() - JeanII */
1041 outb(0x81, iobase+4);
1042 outb(0x80, iobase+4);
1044 outb(0x00, iobase+4);
1046 case 0x0A: /* same as */
1047 case 0x0B: /* Reserved */
1048 IRDA_DEBUG(0, "%s(), %s not defined by irda yet\n",
1049 __FUNCTION__, dongle_types[dongle_id]);
1051 case 0x0C: /* same as */
1052 case 0x0D: /* HP HSDL-1100/HSDL-2100 */
1054 case 0x0E: /* Supports SIR Mode only */
1056 case 0x0F: /* No dongle connected */
1057 IRDA_DEBUG(0, "%s(), %s is not for IrDA mode\n",
1058 __FUNCTION__, dongle_types[dongle_id]);
1060 switch_bank(iobase, BANK0);
1061 outb(0x62, iobase+MCR);
1064 IRDA_DEBUG(0, "%s(), invalid data_rate\n", __FUNCTION__);
1066 /* Restore bank register */
1067 outb(bank, iobase+BSR);
1071 * Function nsc_ircc_change_speed (self, baud)
1073 * Change the speed of the device
1075 * This function *must* be called with irq off and spin-lock.
1077 static __u8 nsc_ircc_change_speed(struct nsc_ircc_cb *self, __u32 speed)
1079 struct net_device *dev = self->netdev;
1083 __u8 ier; /* Interrupt enable register */
1085 IRDA_DEBUG(2, "%s(), speed=%d\n", __FUNCTION__, speed);
1087 ASSERT(self != NULL, return 0;);
1089 iobase = self->io.fir_base;
1091 /* Update accounting for new speed */
1092 self->io.speed = speed;
1094 /* Save current bank */
1095 bank = inb(iobase+BSR);
1097 /* Disable interrupts */
1098 switch_bank(iobase, BANK0);
1099 outb(0, iobase+IER);
1102 switch_bank(iobase, BANK2);
1104 outb(0x00, iobase+BGDH);
1106 case 9600: outb(0x0c, iobase+BGDL); break;
1107 case 19200: outb(0x06, iobase+BGDL); break;
1108 case 38400: outb(0x03, iobase+BGDL); break;
1109 case 57600: outb(0x02, iobase+BGDL); break;
1110 case 115200: outb(0x01, iobase+BGDL); break;
1112 switch_bank(iobase, BANK5);
1114 /* IRCR2: MDRS is set */
1115 outb(inb(iobase+4) | 0x04, iobase+4);
1118 IRDA_DEBUG(0, "%s(), handling baud of 576000\n", __FUNCTION__);
1122 IRDA_DEBUG(0, "%s(), handling baud of 1152000\n", __FUNCTION__);
1126 IRDA_DEBUG(0, "%s(), handling baud of 4000000\n", __FUNCTION__);
1130 IRDA_DEBUG(0, "%s(), unknown baud rate of %d\n",
1131 __FUNCTION__, speed);
1135 /* Set appropriate speed mode */
1136 switch_bank(iobase, BANK0);
1137 outb(mcr | MCR_TX_DFR, iobase+MCR);
1139 /* Give some hits to the transceiver */
1140 nsc_ircc_change_dongle_speed(iobase, speed, self->io.dongle_id);
1142 /* Set FIFO threshold to TX17, RX16 */
1143 switch_bank(iobase, BANK0);
1144 outb(0x00, iobase+FCR);
1145 outb(FCR_FIFO_EN, iobase+FCR);
1146 outb(FCR_RXTH| /* Set Rx FIFO threshold */
1147 FCR_TXTH| /* Set Tx FIFO threshold */
1148 FCR_TXSR| /* Reset Tx FIFO */
1149 FCR_RXSR| /* Reset Rx FIFO */
1150 FCR_FIFO_EN, /* Enable FIFOs */
1153 /* Set FIFO size to 32 */
1154 switch_bank(iobase, BANK2);
1155 outb(EXCR2_RFSIZ|EXCR2_TFSIZ, iobase+EXCR2);
1157 /* Enable some interrupts so we can receive frames */
1158 switch_bank(iobase, BANK0);
1159 if (speed > 115200) {
1160 /* Install FIR xmit handler */
1161 dev->hard_start_xmit = nsc_ircc_hard_xmit_fir;
1163 nsc_ircc_dma_receive(self);
1165 /* Install SIR xmit handler */
1166 dev->hard_start_xmit = nsc_ircc_hard_xmit_sir;
1169 /* Set our current interrupt mask */
1170 outb(ier, iobase+IER);
1173 outb(bank, iobase+BSR);
1175 /* Make sure interrupt handlers keep the proper interrupt mask */
1180 * Function nsc_ircc_hard_xmit (skb, dev)
1182 * Transmit the frame!
1185 static int nsc_ircc_hard_xmit_sir(struct sk_buff *skb, struct net_device *dev)
1187 struct nsc_ircc_cb *self;
1188 unsigned long flags;
1193 self = (struct nsc_ircc_cb *) dev->priv;
1195 ASSERT(self != NULL, return 0;);
1197 iobase = self->io.fir_base;
1199 netif_stop_queue(dev);
1201 /* Make sure tests *& speed change are atomic */
1202 spin_lock_irqsave(&self->lock, flags);
1204 /* Check if we need to change the speed */
1205 speed = irda_get_next_speed(skb);
1206 if ((speed != self->io.speed) && (speed != -1)) {
1207 /* Check for empty frame. */
1209 /* If we just sent a frame, we get called before
1210 * the last bytes get out (because of the SIR FIFO).
1211 * If this is the case, let interrupt handler change
1212 * the speed itself... Jean II */
1213 if (self->io.direction == IO_RECV) {
1214 nsc_ircc_change_speed(self, speed);
1215 /* TODO : For SIR->SIR, the next packet
1216 * may get corrupted - Jean II */
1217 netif_wake_queue(dev);
1219 self->new_speed = speed;
1220 /* Queue will be restarted after speed change
1221 * to make sure packets gets through the
1222 * proper xmit handler - Jean II */
1224 dev->trans_start = jiffies;
1225 spin_unlock_irqrestore(&self->lock, flags);
1229 self->new_speed = speed;
1232 /* Save current bank */
1233 bank = inb(iobase+BSR);
1235 self->tx_buff.data = self->tx_buff.head;
1237 self->tx_buff.len = async_wrap_skb(skb, self->tx_buff.data,
1238 self->tx_buff.truesize);
1240 self->stats.tx_bytes += self->tx_buff.len;
1242 /* Add interrupt on tx low level (will fire immediately) */
1243 switch_bank(iobase, BANK0);
1244 outb(IER_TXLDL_IE, iobase+IER);
1246 /* Restore bank register */
1247 outb(bank, iobase+BSR);
1249 dev->trans_start = jiffies;
1250 spin_unlock_irqrestore(&self->lock, flags);
1257 static int nsc_ircc_hard_xmit_fir(struct sk_buff *skb, struct net_device *dev)
1259 struct nsc_ircc_cb *self;
1260 unsigned long flags;
1266 self = (struct nsc_ircc_cb *) dev->priv;
1267 iobase = self->io.fir_base;
1269 netif_stop_queue(dev);
1271 /* Make sure tests *& speed change are atomic */
1272 spin_lock_irqsave(&self->lock, flags);
1274 /* Check if we need to change the speed */
1275 speed = irda_get_next_speed(skb);
1276 if ((speed != self->io.speed) && (speed != -1)) {
1277 /* Check for empty frame. */
1279 /* If we are currently transmitting, defer to
1280 * interrupt handler. - Jean II */
1281 if(self->tx_fifo.len == 0) {
1282 nsc_ircc_change_speed(self, speed);
1283 netif_wake_queue(dev);
1285 self->new_speed = speed;
1286 /* Keep queue stopped :
1287 * the speed change operation may change the
1288 * xmit handler, and we want to make sure
1289 * the next packet get through the proper
1290 * Tx path, so block the Tx queue until
1291 * the speed change has been done.
1294 dev->trans_start = jiffies;
1295 spin_unlock_irqrestore(&self->lock, flags);
1299 /* Change speed after current frame */
1300 self->new_speed = speed;
1304 /* Save current bank */
1305 bank = inb(iobase+BSR);
1307 /* Register and copy this frame to DMA memory */
1308 self->tx_fifo.queue[self->tx_fifo.free].start = self->tx_fifo.tail;
1309 self->tx_fifo.queue[self->tx_fifo.free].len = skb->len;
1310 self->tx_fifo.tail += skb->len;
1312 self->stats.tx_bytes += skb->len;
1314 memcpy(self->tx_fifo.queue[self->tx_fifo.free].start, skb->data,
1317 self->tx_fifo.len++;
1318 self->tx_fifo.free++;
1320 /* Start transmit only if there is currently no transmit going on */
1321 if (self->tx_fifo.len == 1) {
1322 /* Check if we must wait the min turn time or not */
1323 mtt = irda_get_mtt(skb);
1325 /* Check how much time we have used already */
1326 do_gettimeofday(&self->now);
1327 diff = self->now.tv_usec - self->stamp.tv_usec;
1331 /* Check if the mtt is larger than the time we have
1332 * already used by all the protocol processing
1338 * Use timer if delay larger than 125 us, and
1339 * use udelay for smaller values which should
1343 /* Adjust for timer resolution */
1347 switch_bank(iobase, BANK4);
1348 outb(mtt & 0xff, iobase+TMRL);
1349 outb((mtt >> 8) & 0x0f, iobase+TMRH);
1352 outb(IRCR1_TMR_EN, iobase+IRCR1);
1353 self->io.direction = IO_XMIT;
1355 /* Enable timer interrupt */
1356 switch_bank(iobase, BANK0);
1357 outb(IER_TMR_IE, iobase+IER);
1359 /* Timer will take care of the rest */
1365 /* Enable DMA interrupt */
1366 switch_bank(iobase, BANK0);
1367 outb(IER_DMA_IE, iobase+IER);
1369 /* Transmit frame */
1370 nsc_ircc_dma_xmit(self, iobase);
1373 /* Not busy transmitting anymore if window is not full,
1374 * and if we don't need to change speed */
1375 if ((self->tx_fifo.free < MAX_TX_WINDOW) && (self->new_speed == 0))
1376 netif_wake_queue(self->netdev);
1378 /* Restore bank register */
1379 outb(bank, iobase+BSR);
1381 dev->trans_start = jiffies;
1382 spin_unlock_irqrestore(&self->lock, flags);
1389 * Function nsc_ircc_dma_xmit (self, iobase)
1391 * Transmit data using DMA
1394 static void nsc_ircc_dma_xmit(struct nsc_ircc_cb *self, int iobase)
1398 /* Save current bank */
1399 bsr = inb(iobase+BSR);
1402 switch_bank(iobase, BANK0);
1403 outb(inb(iobase+MCR) & ~MCR_DMA_EN, iobase+MCR);
1405 self->io.direction = IO_XMIT;
1407 /* Choose transmit DMA channel */
1408 switch_bank(iobase, BANK2);
1409 outb(ECR1_DMASWP|ECR1_DMANF|ECR1_EXT_SL, iobase+ECR1);
1411 irda_setup_dma(self->io.dma,
1412 self->tx_fifo.queue[self->tx_fifo.ptr].start,
1413 self->tx_fifo.queue[self->tx_fifo.ptr].len,
1416 /* Enable DMA and SIR interaction pulse */
1417 switch_bank(iobase, BANK0);
1418 outb(inb(iobase+MCR)|MCR_TX_DFR|MCR_DMA_EN|MCR_IR_PLS, iobase+MCR);
1420 /* Restore bank register */
1421 outb(bsr, iobase+BSR);
1425 * Function nsc_ircc_pio_xmit (self, iobase)
1427 * Transmit data using PIO. Returns the number of bytes that actually
1431 static int nsc_ircc_pio_write(int iobase, __u8 *buf, int len, int fifo_size)
1436 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
1438 /* Save current bank */
1439 bank = inb(iobase+BSR);
1441 switch_bank(iobase, BANK0);
1442 if (!(inb_p(iobase+LSR) & LSR_TXEMP)) {
1443 IRDA_DEBUG(4, "%s(), warning, FIFO not empty yet!\n",
1446 /* FIFO may still be filled to the Tx interrupt threshold */
1450 /* Fill FIFO with current frame */
1451 while ((fifo_size-- > 0) && (actual < len)) {
1452 /* Transmit next byte */
1453 outb(buf[actual++], iobase+TXD);
1456 IRDA_DEBUG(4, "%s(), fifo_size %d ; %d sent of %d\n",
1457 __FUNCTION__, fifo_size, actual, len);
1460 outb(bank, iobase+BSR);
1466 * Function nsc_ircc_dma_xmit_complete (self)
1468 * The transfer of a frame in finished. This function will only be called
1469 * by the interrupt handler
1472 static int nsc_ircc_dma_xmit_complete(struct nsc_ircc_cb *self)
1478 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
1480 iobase = self->io.fir_base;
1482 /* Save current bank */
1483 bank = inb(iobase+BSR);
1486 switch_bank(iobase, BANK0);
1487 outb(inb(iobase+MCR) & ~MCR_DMA_EN, iobase+MCR);
1489 /* Check for underrrun! */
1490 if (inb(iobase+ASCR) & ASCR_TXUR) {
1491 self->stats.tx_errors++;
1492 self->stats.tx_fifo_errors++;
1494 /* Clear bit, by writing 1 into it */
1495 outb(ASCR_TXUR, iobase+ASCR);
1497 self->stats.tx_packets++;
1500 /* Finished with this frame, so prepare for next */
1501 self->tx_fifo.ptr++;
1502 self->tx_fifo.len--;
1504 /* Any frames to be sent back-to-back? */
1505 if (self->tx_fifo.len) {
1506 nsc_ircc_dma_xmit(self, iobase);
1508 /* Not finished yet! */
1511 /* Reset Tx FIFO info */
1512 self->tx_fifo.len = self->tx_fifo.ptr = self->tx_fifo.free = 0;
1513 self->tx_fifo.tail = self->tx_buff.head;
1516 /* Make sure we have room for more frames and
1517 * that we don't need to change speed */
1518 if ((self->tx_fifo.free < MAX_TX_WINDOW) && (self->new_speed == 0)) {
1519 /* Not busy transmitting anymore */
1520 /* Tell the network layer, that we can accept more frames */
1521 netif_wake_queue(self->netdev);
1525 outb(bank, iobase+BSR);
1531 * Function nsc_ircc_dma_receive (self)
1533 * Get ready for receiving a frame. The device will initiate a DMA
1534 * if it starts to receive a frame.
1537 static int nsc_ircc_dma_receive(struct nsc_ircc_cb *self)
1542 iobase = self->io.fir_base;
1544 /* Reset Tx FIFO info */
1545 self->tx_fifo.len = self->tx_fifo.ptr = self->tx_fifo.free = 0;
1546 self->tx_fifo.tail = self->tx_buff.head;
1548 /* Save current bank */
1549 bsr = inb(iobase+BSR);
1552 switch_bank(iobase, BANK0);
1553 outb(inb(iobase+MCR) & ~MCR_DMA_EN, iobase+MCR);
1555 /* Choose DMA Rx, DMA Fairness, and Advanced mode */
1556 switch_bank(iobase, BANK2);
1557 outb(ECR1_DMANF|ECR1_EXT_SL, iobase+ECR1);
1559 self->io.direction = IO_RECV;
1560 self->rx_buff.data = self->rx_buff.head;
1562 /* Reset Rx FIFO. This will also flush the ST_FIFO */
1563 switch_bank(iobase, BANK0);
1564 outb(FCR_RXSR|FCR_FIFO_EN, iobase+FCR);
1566 self->st_fifo.len = self->st_fifo.pending_bytes = 0;
1567 self->st_fifo.tail = self->st_fifo.head = 0;
1569 irda_setup_dma(self->io.dma, self->rx_buff.data,
1570 self->rx_buff.truesize, DMA_RX_MODE);
1573 switch_bank(iobase, BANK0);
1574 outb(inb(iobase+MCR)|MCR_DMA_EN, iobase+MCR);
1576 /* Restore bank register */
1577 outb(bsr, iobase+BSR);
1583 * Function nsc_ircc_dma_receive_complete (self)
1585 * Finished with receiving frames
1589 static int nsc_ircc_dma_receive_complete(struct nsc_ircc_cb *self, int iobase)
1591 struct st_fifo *st_fifo;
1592 struct sk_buff *skb;
1597 st_fifo = &self->st_fifo;
1599 /* Save current bank */
1600 bank = inb(iobase+BSR);
1602 /* Read all entries in status FIFO */
1603 switch_bank(iobase, BANK5);
1604 while ((status = inb(iobase+FRM_ST)) & FRM_ST_VLD) {
1605 /* We must empty the status FIFO no matter what */
1606 len = inb(iobase+RFLFL) | ((inb(iobase+RFLFH) & 0x1f) << 8);
1608 if (st_fifo->tail >= MAX_RX_WINDOW) {
1609 IRDA_DEBUG(0, "%s(), window is full!\n", __FUNCTION__);
1613 st_fifo->entries[st_fifo->tail].status = status;
1614 st_fifo->entries[st_fifo->tail].len = len;
1615 st_fifo->pending_bytes += len;
1619 /* Try to process all entries in status FIFO */
1620 while (st_fifo->len > 0) {
1621 /* Get first entry */
1622 status = st_fifo->entries[st_fifo->head].status;
1623 len = st_fifo->entries[st_fifo->head].len;
1624 st_fifo->pending_bytes -= len;
1628 /* Check for errors */
1629 if (status & FRM_ST_ERR_MSK) {
1630 if (status & FRM_ST_LOST_FR) {
1631 /* Add number of lost frames to stats */
1632 self->stats.rx_errors += len;
1635 self->stats.rx_errors++;
1637 self->rx_buff.data += len;
1639 if (status & FRM_ST_MAX_LEN)
1640 self->stats.rx_length_errors++;
1642 if (status & FRM_ST_PHY_ERR)
1643 self->stats.rx_frame_errors++;
1645 if (status & FRM_ST_BAD_CRC)
1646 self->stats.rx_crc_errors++;
1648 /* The errors below can be reported in both cases */
1649 if (status & FRM_ST_OVR1)
1650 self->stats.rx_fifo_errors++;
1652 if (status & FRM_ST_OVR2)
1653 self->stats.rx_fifo_errors++;
1656 * First we must make sure that the frame we
1657 * want to deliver is all in main memory. If we
1658 * cannot tell, then we check if the Rx FIFO is
1659 * empty. If not then we will have to take a nap
1660 * and try again later.
1662 if (st_fifo->pending_bytes < self->io.fifo_size) {
1663 switch_bank(iobase, BANK0);
1664 if (inb(iobase+LSR) & LSR_RXDA) {
1665 /* Put this entry back in fifo */
1668 st_fifo->pending_bytes += len;
1669 st_fifo->entries[st_fifo->head].status = status;
1670 st_fifo->entries[st_fifo->head].len = len;
1672 * DMA not finished yet, so try again
1673 * later, set timer value, resolution
1676 switch_bank(iobase, BANK4);
1677 outb(0x02, iobase+TMRL); /* x 125 us */
1678 outb(0x00, iobase+TMRH);
1681 outb(IRCR1_TMR_EN, iobase+IRCR1);
1683 /* Restore bank register */
1684 outb(bank, iobase+BSR);
1686 return FALSE; /* I'll be back! */
1691 * Remember the time we received this frame, so we can
1692 * reduce the min turn time a bit since we will know
1693 * how much time we have used for protocol processing
1695 do_gettimeofday(&self->stamp);
1697 skb = dev_alloc_skb(len+1);
1699 WARNING("%s(), memory squeeze, "
1700 "dropping frame.\n", __FUNCTION__);
1701 self->stats.rx_dropped++;
1703 /* Restore bank register */
1704 outb(bank, iobase+BSR);
1709 /* Make sure IP header gets aligned */
1710 skb_reserve(skb, 1);
1712 /* Copy frame without CRC */
1713 if (self->io.speed < 4000000) {
1714 skb_put(skb, len-2);
1715 memcpy(skb->data, self->rx_buff.data, len-2);
1717 skb_put(skb, len-4);
1718 memcpy(skb->data, self->rx_buff.data, len-4);
1721 /* Move to next frame */
1722 self->rx_buff.data += len;
1723 self->stats.rx_bytes += len;
1724 self->stats.rx_packets++;
1726 skb->dev = self->netdev;
1727 skb->mac.raw = skb->data;
1728 skb->protocol = htons(ETH_P_IRDA);
1730 self->netdev->last_rx = jiffies;
1733 /* Restore bank register */
1734 outb(bank, iobase+BSR);
1740 * Function nsc_ircc_pio_receive (self)
1742 * Receive all data in receiver FIFO
1745 static void nsc_ircc_pio_receive(struct nsc_ircc_cb *self)
1750 iobase = self->io.fir_base;
1752 /* Receive all characters in Rx FIFO */
1754 byte = inb(iobase+RXD);
1755 async_unwrap_char(self->netdev, &self->stats, &self->rx_buff,
1757 } while (inb(iobase+LSR) & LSR_RXDA); /* Data available */
1761 * Function nsc_ircc_sir_interrupt (self, eir)
1763 * Handle SIR interrupt
1766 static void nsc_ircc_sir_interrupt(struct nsc_ircc_cb *self, int eir)
1770 /* Check if transmit FIFO is low on data */
1771 if (eir & EIR_TXLDL_EV) {
1772 /* Write data left in transmit buffer */
1773 actual = nsc_ircc_pio_write(self->io.fir_base,
1776 self->io.fifo_size);
1777 self->tx_buff.data += actual;
1778 self->tx_buff.len -= actual;
1780 self->io.direction = IO_XMIT;
1782 /* Check if finished */
1783 if (self->tx_buff.len > 0)
1784 self->ier = IER_TXLDL_IE;
1787 self->stats.tx_packets++;
1788 netif_wake_queue(self->netdev);
1789 self->ier = IER_TXEMP_IE;
1793 /* Check if transmission has completed */
1794 if (eir & EIR_TXEMP_EV) {
1795 /* Turn around and get ready to receive some data */
1796 self->io.direction = IO_RECV;
1797 self->ier = IER_RXHDL_IE;
1798 /* Check if we need to change the speed?
1799 * Need to be after self->io.direction to avoid race with
1800 * nsc_ircc_hard_xmit_sir() - Jean II */
1801 if (self->new_speed) {
1802 IRDA_DEBUG(2, "%s(), Changing speed!\n", __FUNCTION__);
1803 self->ier = nsc_ircc_change_speed(self,
1805 self->new_speed = 0;
1806 netif_wake_queue(self->netdev);
1808 /* Check if we are going to FIR */
1809 if (self->io.speed > 115200) {
1810 /* No need to do anymore SIR stuff */
1816 /* Rx FIFO threshold or timeout */
1817 if (eir & EIR_RXHDL_EV) {
1818 nsc_ircc_pio_receive(self);
1820 /* Keep receiving */
1821 self->ier = IER_RXHDL_IE;
1826 * Function nsc_ircc_fir_interrupt (self, eir)
1828 * Handle MIR/FIR interrupt
1831 static void nsc_ircc_fir_interrupt(struct nsc_ircc_cb *self, int iobase,
1836 bank = inb(iobase+BSR);
1838 /* Status FIFO event*/
1839 if (eir & EIR_SFIF_EV) {
1840 /* Check if DMA has finished */
1841 if (nsc_ircc_dma_receive_complete(self, iobase)) {
1842 /* Wait for next status FIFO interrupt */
1843 self->ier = IER_SFIF_IE;
1845 self->ier = IER_SFIF_IE | IER_TMR_IE;
1847 } else if (eir & EIR_TMR_EV) { /* Timer finished */
1849 switch_bank(iobase, BANK4);
1850 outb(0, iobase+IRCR1);
1852 /* Clear timer event */
1853 switch_bank(iobase, BANK0);
1854 outb(ASCR_CTE, iobase+ASCR);
1856 /* Check if this is a Tx timer interrupt */
1857 if (self->io.direction == IO_XMIT) {
1858 nsc_ircc_dma_xmit(self, iobase);
1860 /* Interrupt on DMA */
1861 self->ier = IER_DMA_IE;
1863 /* Check (again) if DMA has finished */
1864 if (nsc_ircc_dma_receive_complete(self, iobase)) {
1865 self->ier = IER_SFIF_IE;
1867 self->ier = IER_SFIF_IE | IER_TMR_IE;
1870 } else if (eir & EIR_DMA_EV) {
1871 /* Finished with all transmissions? */
1872 if (nsc_ircc_dma_xmit_complete(self)) {
1873 if(self->new_speed != 0) {
1874 /* As we stop the Tx queue, the speed change
1875 * need to be done when the Tx fifo is
1876 * empty. Ask for a Tx done interrupt */
1877 self->ier = IER_TXEMP_IE;
1879 /* Check if there are more frames to be
1881 if (irda_device_txqueue_empty(self->netdev)) {
1882 /* Prepare for receive */
1883 nsc_ircc_dma_receive(self);
1884 self->ier = IER_SFIF_IE;
1886 WARNING("%s(), potential "
1887 "Tx queue lockup !\n",
1891 /* Not finished yet, so interrupt on DMA again */
1892 self->ier = IER_DMA_IE;
1894 } else if (eir & EIR_TXEMP_EV) {
1895 /* The Tx FIFO has totally drained out, so now we can change
1896 * the speed... - Jean II */
1897 self->ier = nsc_ircc_change_speed(self, self->new_speed);
1898 self->new_speed = 0;
1899 netif_wake_queue(self->netdev);
1900 /* Note : nsc_ircc_change_speed() restarted Rx fifo */
1903 outb(bank, iobase+BSR);
1907 * Function nsc_ircc_interrupt (irq, dev_id, regs)
1909 * An interrupt from the chip has arrived. Time to do some work
1912 static irqreturn_t nsc_ircc_interrupt(int irq, void *dev_id,
1913 struct pt_regs *regs)
1915 struct net_device *dev = (struct net_device *) dev_id;
1916 struct nsc_ircc_cb *self;
1921 WARNING("%s: irq %d for unknown device.\n", driver_name, irq);
1924 self = (struct nsc_ircc_cb *) dev->priv;
1926 spin_lock(&self->lock);
1928 iobase = self->io.fir_base;
1930 bsr = inb(iobase+BSR); /* Save current bank */
1932 switch_bank(iobase, BANK0);
1933 self->ier = inb(iobase+IER);
1934 eir = inb(iobase+EIR) & self->ier; /* Mask out the interesting ones */
1936 outb(0, iobase+IER); /* Disable interrupts */
1939 /* Dispatch interrupt handler for the current speed */
1940 if (self->io.speed > 115200)
1941 nsc_ircc_fir_interrupt(self, iobase, eir);
1943 nsc_ircc_sir_interrupt(self, eir);
1946 outb(self->ier, iobase+IER); /* Restore interrupts */
1947 outb(bsr, iobase+BSR); /* Restore bank register */
1949 spin_unlock(&self->lock);
1950 return IRQ_RETVAL(eir);
1954 * Function nsc_ircc_is_receiving (self)
1956 * Return TRUE is we are currently receiving a frame
1959 static int nsc_ircc_is_receiving(struct nsc_ircc_cb *self)
1961 unsigned long flags;
1966 ASSERT(self != NULL, return FALSE;);
1968 spin_lock_irqsave(&self->lock, flags);
1970 if (self->io.speed > 115200) {
1971 iobase = self->io.fir_base;
1973 /* Check if rx FIFO is not empty */
1974 bank = inb(iobase+BSR);
1975 switch_bank(iobase, BANK2);
1976 if ((inb(iobase+RXFLV) & 0x3f) != 0) {
1977 /* We are receiving something */
1980 outb(bank, iobase+BSR);
1982 status = (self->rx_buff.state != OUTSIDE_FRAME);
1984 spin_unlock_irqrestore(&self->lock, flags);
1990 * Function nsc_ircc_net_open (dev)
1995 static int nsc_ircc_net_open(struct net_device *dev)
1997 struct nsc_ircc_cb *self;
2002 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
2004 ASSERT(dev != NULL, return -1;);
2005 self = (struct nsc_ircc_cb *) dev->priv;
2007 ASSERT(self != NULL, return 0;);
2009 iobase = self->io.fir_base;
2011 if (request_irq(self->io.irq, nsc_ircc_interrupt, 0, dev->name, dev)) {
2012 WARNING("%s, unable to allocate irq=%d\n", driver_name,
2017 * Always allocate the DMA channel after the IRQ, and clean up on
2020 if (request_dma(self->io.dma, dev->name)) {
2021 WARNING("%s, unable to allocate dma=%d\n", driver_name,
2023 free_irq(self->io.irq, dev);
2027 /* Save current bank */
2028 bank = inb(iobase+BSR);
2030 /* turn on interrupts */
2031 switch_bank(iobase, BANK0);
2032 outb(IER_LS_IE | IER_RXHDL_IE, iobase+IER);
2034 /* Restore bank register */
2035 outb(bank, iobase+BSR);
2037 /* Ready to play! */
2038 netif_start_queue(dev);
2040 /* Give self a hardware name */
2041 sprintf(hwname, "NSC-FIR @ 0x%03x", self->io.fir_base);
2044 * Open new IrLAP layer instance, now that everything should be
2045 * initialized properly
2047 self->irlap = irlap_open(dev, &self->qos, hwname);
2053 * Function nsc_ircc_net_close (dev)
2058 static int nsc_ircc_net_close(struct net_device *dev)
2060 struct nsc_ircc_cb *self;
2064 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
2066 ASSERT(dev != NULL, return -1;);
2068 self = (struct nsc_ircc_cb *) dev->priv;
2069 ASSERT(self != NULL, return 0;);
2072 netif_stop_queue(dev);
2074 /* Stop and remove instance of IrLAP */
2076 irlap_close(self->irlap);
2079 iobase = self->io.fir_base;
2081 disable_dma(self->io.dma);
2083 /* Save current bank */
2084 bank = inb(iobase+BSR);
2086 /* Disable interrupts */
2087 switch_bank(iobase, BANK0);
2088 outb(0, iobase+IER);
2090 free_irq(self->io.irq, dev);
2091 free_dma(self->io.dma);
2093 /* Restore bank register */
2094 outb(bank, iobase+BSR);
2100 * Function nsc_ircc_net_ioctl (dev, rq, cmd)
2102 * Process IOCTL commands for this device
2105 static int nsc_ircc_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2107 struct if_irda_req *irq = (struct if_irda_req *) rq;
2108 struct nsc_ircc_cb *self;
2109 unsigned long flags;
2112 ASSERT(dev != NULL, return -1;);
2116 ASSERT(self != NULL, return -1;);
2118 IRDA_DEBUG(2, "%s(), %s, (cmd=0x%X)\n", __FUNCTION__, dev->name, cmd);
2121 case SIOCSBANDWIDTH: /* Set bandwidth */
2122 if (!capable(CAP_NET_ADMIN)) {
2126 spin_lock_irqsave(&self->lock, flags);
2127 nsc_ircc_change_speed(self, irq->ifr_baudrate);
2128 spin_unlock_irqrestore(&self->lock, flags);
2130 case SIOCSMEDIABUSY: /* Set media busy */
2131 if (!capable(CAP_NET_ADMIN)) {
2135 irda_device_set_media_busy(self->netdev, TRUE);
2137 case SIOCGRECEIVING: /* Check if we are receiving right now */
2138 /* This is already protected */
2139 irq->ifr_receiving = nsc_ircc_is_receiving(self);
2147 static struct net_device_stats *nsc_ircc_net_get_stats(struct net_device *dev)
2149 struct nsc_ircc_cb *self = (struct nsc_ircc_cb *) dev->priv;
2151 return &self->stats;
2154 static void nsc_ircc_suspend(struct nsc_ircc_cb *self)
2156 MESSAGE("%s, Suspending\n", driver_name);
2158 if (self->io.suspended)
2161 nsc_ircc_net_close(self->netdev);
2163 self->io.suspended = 1;
2166 static void nsc_ircc_wakeup(struct nsc_ircc_cb *self)
2168 if (!self->io.suspended)
2171 nsc_ircc_setup(&self->io);
2172 nsc_ircc_net_open(self->netdev);
2174 MESSAGE("%s, Waking up\n", driver_name);
2176 self->io.suspended = 0;
2179 static int nsc_ircc_pmproc(struct pm_dev *dev, pm_request_t rqst, void *data)
2181 struct nsc_ircc_cb *self = (struct nsc_ircc_cb*) dev->data;
2185 nsc_ircc_suspend(self);
2188 nsc_ircc_wakeup(self);
2195 MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
2196 MODULE_DESCRIPTION("NSC IrDA Device Driver");
2197 MODULE_LICENSE("GPL");
2200 MODULE_PARM(qos_mtt_bits, "i");
2201 MODULE_PARM_DESC(qos_mtt_bits, "Minimum Turn Time");
2202 MODULE_PARM(io, "1-4i");
2203 MODULE_PARM_DESC(io, "Base I/O addresses");
2204 MODULE_PARM(irq, "1-4i");
2205 MODULE_PARM_DESC(irq, "IRQ lines");
2206 MODULE_PARM(dma, "1-4i");
2207 MODULE_PARM_DESC(dma, "DMA channels");
2208 MODULE_PARM(dongle_id, "i");
2209 MODULE_PARM_DESC(dongle_id, "Type-id of used dongle");
2211 module_init(nsc_ircc_init);
2212 module_exit(nsc_ircc_cleanup);