patch-2.2.10 linux/drivers/net/irda/w83977af_ir.c

Next file: linux/drivers/net/ni52.c
Previous file: linux/drivers/net/irda/uircc.c
Back to the patch index
Back to the overall index

diff -u --recursive --new-file v2.2.9/linux/drivers/net/irda/w83977af_ir.c linux/drivers/net/irda/w83977af_ir.c
@@ -1,16 +1,16 @@
 /*********************************************************************
  *                
  * Filename:      w83977af_ir.c
- * Version:       0.8
- * Description:   FIR/MIR driver for the Winbond W83977AF Super I/O chip
+ * Version:       1.0
+ * Description:   FIR driver for the Winbond W83977AF Super I/O chip
  * Status:        Experimental.
  * Author:        Paul VanderSpek
  * Created at:    Wed Nov  4 11:46:16 1998
- * Modified at:   Tue Apr 20 11:15:00 1999
+ * Modified at:   Fri May 21 22:18:19 1999
  * Modified by:   Dag Brattli <dagb@cs.uit.no>
  * 
+ *     Copyright (c) 1998-1999 Dag Brattli <dagb@cs.uit.no>
  *     Copyright (c) 1998 Corel Computer Corp.
- *     Copyright (c) 1998 Dag Brattli <dagb@cs.uit.no>
  *      
  *     This program is free software; you can redistribute it and/or 
  *     modify it under the terms of the GNU General Public License as 
@@ -40,7 +40,7 @@
  ********************************************************************/
 
 #include <linux/module.h>
-
+ 
 #include <linux/kernel.h>
 #include <linux/types.h>
 #include <linux/skbuff.h>
@@ -61,44 +61,43 @@
 #include <net/irda/w83977af.h>
 #include <net/irda/w83977af_ir.h>
 
-#define NETWINDER
+#define CONFIG_NETWINDER                 /* Adjust to NetWinder differences */
+#undef  CONFIG_NETWINDER_TX_DMA_PROBLEMS /* Not needed */
+#define CONFIG_NETWINDER_RX_DMA_PROBLEMS /* Must have this one! */
+#undef  CONFIG_USE_INTERNAL_TIMER        /* Just cannot make that timer work */
+#define CONFIG_USE_W977_PNP              /* Currently needed */
+#define PIO_MAX_SPEED       115200 
 
 static char *driver_name = "w83977af_ir";
+static int  qos_mtt_bits = 0x07;           /* 1 ms or more */
 
 #define CHIP_IO_EXTENT 8
 
 static unsigned int io[] = { 0x180, ~0, ~0, ~0 };
 static unsigned int irq[] = { 6, 0, 0, 0 };
-static unsigned int dma[] = { 0, 0, 0, 0 };
-
-static struct irda_device *dev_self[] = { NULL, NULL, NULL, NULL};
+static unsigned int dma[] = 
+{ 1, 0, 0, 0 };
 
-/* For storing entries in the status FIFO */
-struct st_fifo_entry {
-	int status;
-	int len;
-};
-
-static struct st_fifo_entry prev;
+static struct w83977af_ir *dev_self[] = { NULL, NULL, NULL, NULL};
 
 /* Some prototypes */
-static int  w83977af_open( int i, unsigned int iobase, unsigned int irq, 
-			   unsigned int dma);
-static int  w83977af_close( struct irda_device *idev);
-static int  w83977af_probe( int iobase, int irq, int dma);
+static int  w83977af_open(int i, unsigned int iobase, unsigned int irq, 
+                          unsigned int dma);
+static int  w83977af_close(struct irda_device *idev);
+static int  w83977af_probe(int iobase, int irq, int dma);
 static int  w83977af_dma_receive(struct irda_device *idev); 
 static int  w83977af_dma_receive_complete(struct irda_device *idev);
-static int  w83977af_hard_xmit( struct sk_buff *skb, struct device *dev);
-static int  w83977af_pio_write( int iobase, __u8 *buf, int len, int fifo_size);
-static void w83977af_dma_write( struct irda_device *idev, int iobase);
-static void w83977af_change_speed( struct irda_device *idev, int baud);
+static int  w83977af_hard_xmit(struct sk_buff *skb, struct device *dev);
+static int  w83977af_pio_write(int iobase, __u8 *buf, int len, int fifo_size);
+static void w83977af_dma_write(struct irda_device *idev, int iobase);
+static void w83977af_change_speed(struct irda_device *idev, int baud);
 static void w83977af_interrupt(int irq, void *dev_id, struct pt_regs *regs);
-static void w83977af_wait_until_sent( struct irda_device *idev);
-static int w83977af_is_receiving( struct irda_device *idev);
+static void w83977af_wait_until_sent(struct irda_device *idev);
+static int  w83977af_is_receiving(struct irda_device *idev);
 
-static int w83977af_net_init( struct device *dev);
-static int w83977af_net_open( struct device *dev);
-static int w83977af_net_close( struct device *dev);
+static int  w83977af_net_init(struct device *dev);
+static int  w83977af_net_open(struct device *dev);
+static int  w83977af_net_close(struct device *dev);
 
 /*
  * Function w83977af_init ()
@@ -108,13 +107,11 @@
  */
 __initfunc(int w83977af_init(void))
 {
-	int i;
+        int i;
 
-	DEBUG( 0, __FUNCTION__ "()\n");
-
-	prev.status = 0;
+	DEBUG(0, __FUNCTION__ "()\n");
 
-	for ( i=0; (io[i] < 2000) && (i < 4); i++) {
+	for (i=0; (io[i] < 2000) && (i < 4); i++) { 
 		int ioaddr = io[i];
 		if (check_region(ioaddr, CHIP_IO_EXTENT) < 0)
 			continue;
@@ -135,11 +132,11 @@
 {
 	int i;
 
-        DEBUG( 4, __FUNCTION__ "()\n");
+        DEBUG(4, __FUNCTION__ "()\n");
 
-	for ( i=0; i < 4; i++) {
-		if ( dev_self[i])
-			w83977af_close( dev_self[i]);
+	for (i=0; i < 4; i++) {
+		if (dev_self[i])
+			w83977af_close(&(dev_self[i]->idev));
 	}
 }
 #endif /* MODULE */
@@ -154,26 +151,29 @@
 		   unsigned int dma)
 {
 	struct irda_device *idev;
+        struct w83977af_ir *self;
 	int ret;
 
 	DEBUG( 0, __FUNCTION__ "()\n");
 
-	if ( w83977af_probe( iobase, irq, dma) == -1)
+	if (w83977af_probe(iobase, irq, dma) == -1)
 		return -1;
 
 	/*
 	 *  Allocate new instance of the driver
 	 */
-	idev = kmalloc( sizeof(struct irda_device), GFP_KERNEL);
-	if ( idev == NULL) {
+	self = kmalloc(sizeof(struct w83977af_ir), GFP_KERNEL);
+	if (self == NULL) {
 		printk( KERN_ERR "IrDA: Can't allocate memory for "
 			"IrDA control block!\n");
 		return -ENOMEM;
 	}
-	memset( idev, 0, sizeof(struct irda_device));
+	memset(self, 0, sizeof(struct w83977af_ir));
    
 	/* Need to store self somewhere */
-	dev_self[i] = idev;
+	dev_self[i] = self;
+
+	idev = &self->idev;
 
 	/* Initialize IO */
 	idev->io.iobase    = iobase;
@@ -183,17 +183,17 @@
         idev->io.fifo_size = 32;
 
 	/* Lock the port that we need */
-	ret = check_region( idev->io.iobase, idev->io.io_ext);
-	if ( ret < 0) { 
+	ret = check_region(idev->io.iobase, idev->io.io_ext);
+	if (ret < 0) { 
 		DEBUG( 0, __FUNCTION__ "(), can't get iobase of 0x%03x\n",
 		       idev->io.iobase);
 		/* w83977af_cleanup( self->idev);  */
 		return -ENODEV;
 	}
-	request_region( idev->io.iobase, idev->io.io_ext, idev->name);
+	request_region(idev->io.iobase, idev->io.io_ext, idev->name);
 
 	/* Initialize QoS for this device */
-	irda_init_max_qos_capabilies( &idev->qos);
+	irda_init_max_qos_capabilies(&idev->qos);
 	
 	/* The only value we must override it the baudrate */
 
@@ -202,8 +202,8 @@
 		IR_115200|IR_576000|IR_1152000|(IR_4000000 << 8);
 
 	/* The HP HDLS-1100 needs 1 ms according to the specs */
-	idev->qos.min_turn_time.bits = 0x03; /* 1ms and more */
-	irda_qos_bits_to_value( &idev->qos);
+	idev->qos.min_turn_time.bits = qos_mtt_bits;
+	irda_qos_bits_to_value(&idev->qos);
 	
 	idev->flags = IFF_FIR|IFF_MIR|IFF_SIR|IFF_DMA|IFF_PIO;
 
@@ -221,13 +221,13 @@
         idev->is_receiving    = w83977af_is_receiving;
 
 	/* Override the network functions we need to use */
-	idev->netdev.init = w83977af_net_init;
+	idev->netdev.init            = w83977af_net_init;
 	idev->netdev.hard_start_xmit = w83977af_hard_xmit;
-	idev->netdev.open = w83977af_net_open;
-	idev->netdev.stop = w83977af_net_close;
+	idev->netdev.open            = w83977af_net_open;
+	idev->netdev.stop            = w83977af_net_close;
 
 	/* Open the IrDA device */
-	irda_device_open( idev, driver_name, NULL);
+	irda_device_open(idev, driver_name, self);
 	
 	return 0;
 }
@@ -240,15 +240,18 @@
  */
 static int w83977af_close( struct irda_device *idev)
 {
+	struct w83977af_ir *self;
 	int iobase;
 
-	DEBUG( 0, __FUNCTION__ "()\n");
+	DEBUG(0, __FUNCTION__ "()\n");
 
-	ASSERT( idev != NULL, return -1;);
-	ASSERT( idev->magic == IRDA_DEVICE_MAGIC, return -1;);
+	ASSERT(idev != NULL, return -1;);
+	ASSERT(idev->magic == IRDA_DEVICE_MAGIC, return -1;);
 
         iobase = idev->io.iobase;
+	self = (struct w83977af_ir *) idev->priv;
 
+#ifdef CONFIG_USE_W977_PNP
 	/* enter PnP configuration mode */
 	w977_efm_enter();
 
@@ -258,13 +261,15 @@
 	w977_write_reg(0x30, 0x00);
 
 	w977_efm_exit();
-
+#endif /* CONFIG_USE_W977_PNP */
 	/* Release the PORT that this driver is using */
 	DEBUG(0 , __FUNCTION__ "(), Releasing Region %03x\n", 
 	      idev->io.iobase);
-	release_region( idev->io.iobase, idev->io.io_ext);
+	release_region(idev->io.iobase, idev->io.io_ext);
+
+	irda_device_close(idev);
 
-	irda_device_close( idev);
+	kfree(self);
 
 	return 0;
 }
@@ -280,7 +285,7 @@
 	int version;
 	
 	DEBUG( 0, __FUNCTION__ "()\n");
-
+#ifdef CONFIG_USE_W977_PNP
 	/* Enter PnP configuration mode */
 	w977_efm_enter();
 
@@ -289,14 +294,14 @@
 	/* Configure PnP port, IRQ, and DMA channel */
 	w977_write_reg(0x60, (iobase >> 8) & 0xff);
 	w977_write_reg(0x61, (iobase) & 0xff);
-	/* w977_write_reg(0x70, 0x06); */
+
 	w977_write_reg(0x70, irq);
-#ifdef NETWINDER
-	w977_write_reg(0x74, dma+1); /* Netwinder uses one higher than Linux */
+#ifdef CONFIG_NETWINDER
+	w977_write_reg(0x74, dma+1); /* Netwinder uses 1 higher than Linux */
 #else
 	w977_write_reg(0x74, dma);   
 #endif
-	w977_write_reg(0x75, dma);   /* Disable Tx DMA */
+	w977_write_reg(0x75, 0x04);  /* Disable Tx DMA */
 	
 	/* Set append hardware CRC, enable IR bank selection */	
 	w977_write_reg(0xf0, APEDCRC|ENBNKSEL);
@@ -305,26 +310,26 @@
 	w977_write_reg(0x30, 0x01);
 
 	w977_efm_exit();
-
+#endif
  	/* Disable Advanced mode */
- 	switch_bank( iobase, SET2);
+ 	switch_bank(iobase, SET2);
  	outb(iobase+2, 0x00);  
 
 	/* Turn on UART (global) interrupts */
-	switch_bank( iobase, SET0);
-	outb( HCR_EN_IRQ, iobase+HCR);
+	switch_bank(iobase, SET0);
+	outb(HCR_EN_IRQ, iobase+HCR);
 	
 	/* Switch to advanced mode */
-	switch_bank( iobase, SET2);
-	outb( inb( iobase+ADCR1) | ADCR1_ADV_SL, iobase+ADCR1);
+	switch_bank(iobase, SET2);
+	outb(inb(iobase+ADCR1) | ADCR1_ADV_SL, iobase+ADCR1);
 
 	/* Set default IR-mode */
-	switch_bank( iobase, SET0);
-	outb( HCR_SIR, iobase+HCR);
+	switch_bank(iobase, SET0);
+	outb(HCR_SIR, iobase+HCR);
 
 	/* Read the Advanced IR ID */
 	switch_bank(iobase, SET3);
-	version =  inb( iobase+AUID);
+	version = inb(iobase+AUID);
 	
 	/* Should be 0x1? */
 	if (0x10 != (version & 0xf0)) {
@@ -333,18 +338,17 @@
 	}
 	
 	/* Set FIFO size to 32 */
-	switch_bank( iobase, SET2);
-	outb( ADCR2_RXFS32|ADCR2_TXFS32, iobase+ADCR2);	
+	switch_bank(iobase, SET2);
+	outb(ADCR2_RXFS32|ADCR2_TXFS32, iobase+ADCR2);	
 	
 	/* Set FIFO threshold to TX17, RX16 */
 	switch_bank(iobase, SET0);	
 	outb(UFR_RXTL|UFR_TXTL|UFR_TXF_RST|UFR_RXF_RST|UFR_EN_FIFO,iobase+UFR);
-/* 	outb( 0xa7, iobase+UFR); */
 
 	/* Receiver frame length */
-	switch_bank( iobase, SET4);
-	outb( 2048 & 0xff, iobase+6);
-	outb(( 2048 >> 8) & 0x1f, iobase+7);
+	switch_bank(iobase, SET4);
+	outb(2048 & 0xff, iobase+6);
+	outb((2048 >> 8) & 0x1f, iobase+7);
 
 	/* 
 	 * Init HP HSDL-1100 transceiver. 
@@ -358,8 +362,8 @@
 	 *   FIRRX pin 39 connected to receiver      (IRSL0) 
 	 *   CIRRX pin 40 connected to pin 37
 	 */
-	switch_bank( iobase, SET7);
-	outb( 0x40, iobase+7);
+	switch_bank(iobase, SET7);
+	outb(0x40, iobase+7);
 		
 	DEBUG(0, "W83977AF (IR) driver loaded. Version: 0x%02x\n", version);
 	
@@ -372,16 +376,14 @@
  *    Change the speed of the device
  *
  */
-void w83977af_change_speed( struct irda_device *idev, int speed)
+void w83977af_change_speed(struct irda_device *idev, int speed)
 {
 	int ir_mode = HCR_SIR;
 	int iobase; 
 	__u8 set;
 
-	DEBUG( 0, __FUNCTION__ "()\n");
-
-	ASSERT( idev != NULL, return;);
-	ASSERT( idev->magic == IRDA_DEVICE_MAGIC, return;);
+	ASSERT(idev != NULL, return;);
+	ASSERT(idev->magic == IRDA_DEVICE_MAGIC, return;);
 
 	iobase = idev->io.iobase;
 
@@ -389,22 +391,22 @@
 	idev->io.baudrate = speed;
 
 	/* Save current bank */
-	set = inb( iobase+SSR);
+	set = inb(iobase+SSR);
 
 	/* Disable interrupts */
-	switch_bank( iobase, SET0);
-	outb( 0, iobase+ICR);
+	switch_bank(iobase, SET0);
+	outb(0, iobase+ICR);
 
 	/* Select Set 2 */
-	switch_bank( iobase, SET2);
+	switch_bank(iobase, SET2);
+	outb(0x00, iobase+ABHL);
 
-	outb( 0x00, iobase+ABHL);
-	switch ( speed) {
-	case 9600:   outb( 0x0c, iobase+ABLL); break;
-	case 19200:  outb( 0x06, iobase+ABLL); break;
-	case 37600:  outb( 0x03, iobase+ABLL); break;
-	case 57600:  outb( 0x02, iobase+ABLL); break;
-	case 115200: outb( 0x01, iobase+ABLL); break;
+	switch (speed) {
+	case 9600:   outb(0x0c, iobase+ABLL); break;
+	case 19200:  outb(0x06, iobase+ABLL); break;
+	case 37600:  outb(0x03, iobase+ABLL); break;
+	case 57600:  outb(0x02, iobase+ABLL); break;
+	case 115200: outb(0x01, iobase+ABLL); break;
 	case 576000:
 		ir_mode = HCR_MIR_576;
 		DEBUG(0, __FUNCTION__ "(), handling baud of 576000\n");
@@ -419,34 +421,37 @@
 		break;
 	default:
 		ir_mode = HCR_FIR;
-		DEBUG( 0, __FUNCTION__ "(), unknown baud rate of %d\n", speed);
+		DEBUG(0, __FUNCTION__ "(), unknown baud rate of %d\n", speed);
 		break;
 	}
 
 	/* Set speed mode */
 	switch_bank(iobase, SET0);
-	outb( ir_mode, iobase+HCR);
+	outb(ir_mode, iobase+HCR);
 
 	/* set FIFO size to 32 */
-	switch_bank( iobase, SET2);
-	outb( ADCR2_RXFS32|ADCR2_TXFS32, iobase+ADCR2);	
+	switch_bank(iobase, SET2);
+	outb(ADCR2_RXFS32|ADCR2_TXFS32, iobase+ADCR2);	
 	
 	/* set FIFO threshold to TX17, RX16 */
 	switch_bank(iobase, SET0);
-	outb(UFR_RXTL|UFR_TXTL|UFR_TXF_RST|UFR_RXF_RST|UFR_EN_FIFO, iobase+UFR);
-	
+
+	outb(0x00, iobase+UFR);        /* Reset */
+	outb(UFR_EN_FIFO, iobase+UFR); /* First we must enable FIFO */
+	outb(0xa7, iobase+UFR);
+
 	idev->netdev.tbusy = 0;
 	
 	/* Enable some interrupts so we can receive frames */
 	switch_bank(iobase, SET0);
-	if ( speed > 115200) {
-		outb( ICR_EFSFI, iobase+ICR);
-		w83977af_dma_receive( idev);
+	if (speed > PIO_MAX_SPEED) {
+		outb(ICR_EFSFI, iobase+ICR);
+		w83977af_dma_receive(idev);
 	} else
-		outb( ICR_ERBRI, iobase+ICR);
+		outb(ICR_ERBRI, iobase+ICR);
     	
 	/* Restore SSR */
-	outb( set, iobase+SSR);
+	outb(set, iobase+SSR);
 }
 
 /*
@@ -455,7 +460,7 @@
  *    Sets up a DMA transfer to send the current frame.
  *
  */
-int w83977af_hard_xmit( struct sk_buff *skb, struct device *dev)
+int w83977af_hard_xmit(struct sk_buff *skb, struct device *dev)
 {
 	struct irda_device *idev;
 	int iobase;
@@ -474,20 +479,21 @@
 	/* Lock transmit buffer */
 	if (irda_lock((void *) &dev->tbusy) == FALSE)
 		return -EBUSY;
-	
+
 	/* Save current set */
 	set = inb(iobase+SSR);
 	
 	/* Decide if we should use PIO or DMA transfer */
-	if (idev->io.baudrate > 115200) {
+	if (idev->io.baudrate > PIO_MAX_SPEED) {
+		idev->tx_buff.data = idev->tx_buff.head;
 		memcpy(idev->tx_buff.data, skb->data, skb->len);
 		idev->tx_buff.len = skb->len;
-		idev->tx_buff.data = idev->tx_buff.head;
 		
 		mtt = irda_get_mtt(skb);
+#ifdef CONFIG_USE_INTERNAL_TIMER
 	        if (mtt > 50) {
 			/* Adjust for timer resolution */
-			mtt = mtt / 1000 + 1;
+			mtt /= 1000+1;
 
 			/* Setup timer */
 			switch_bank(iobase, SET4);
@@ -502,6 +508,8 @@
 			switch_bank(iobase, SET0);
 			outb(ICR_ETMRI, iobase+ICR);
 		} else {
+#endif
+			DEBUG(4,__FUNCTION__ "(%ld), mtt=%d\n", jiffies, mtt);
 			if (mtt)
 				udelay(mtt);
 
@@ -509,7 +517,9 @@
 			switch_bank(iobase, SET0);
 	 		outb(ICR_EDMAI, iobase+ICR);
 	     		w83977af_dma_write(idev, iobase);
+#ifdef CONFIG_USE_INTERNAL_TIMER
 		}
+#endif
 	} else {
 		idev->tx_buff.data = idev->tx_buff.head;
 		idev->tx_buff.len = async_wrap_skb(skb, idev->tx_buff.data, 
@@ -527,41 +537,57 @@
 	return 0;
 }
 
-
 /*
  * Function w83977af_dma_write (idev, iobase)
  *
- *    
+ *    Send frame using DMA
  *
  */
-static void w83977af_dma_write( struct irda_device *idev, int iobase)
+static void w83977af_dma_write(struct irda_device *idev, int iobase)
 {
 	__u8 set;
-
-        DEBUG( 4, __FUNCTION__ "()\n");
+#ifdef CONFIG_NETWINDER_TX_DMA_PROBLEMS
+	unsigned long flags;
+	__u8 hcr;
+#endif
+        DEBUG(4, __FUNCTION__ "(), len=%d\n", idev->tx_buff.len);
 
 	/* Save current set */
-	set = inb( iobase+SSR);
+	set = inb(iobase+SSR);
 
 	/* Disable DMA */
 	switch_bank(iobase, SET0);
-	outb( inb( iobase+HCR) & ~HCR_EN_DMA, iobase+HCR);
-	
-	setup_dma(idev->io.dma, idev->tx_buff.data, idev->tx_buff.len, 
-		  DMA_MODE_WRITE);
-	
-	/* idev->media_busy = TRUE; */
-	idev->io.direction = IO_XMIT;
-	
+	outb(inb(iobase+HCR) & ~HCR_EN_DMA, iobase+HCR);
+
 	/* Choose transmit DMA channel  */ 
 	switch_bank(iobase, SET2);
-	outb(inb(iobase+ADCR1) | ADCR1_D_CHSW|ADCR1_DMA_F|ADCR1_ADV_SL, 
-	     iobase+ADCR1);
+	outb(ADCR1_D_CHSW|/*ADCR1_DMA_F|*/ADCR1_ADV_SL, iobase+ADCR1);
+#ifdef CONFIG_NETWINDER_TX_DMA_PROBLEMS
+	save_flags(flags);
+	cli();
+
+	disable_dma(idev->io.dma);
+	clear_dma_ff(idev->io.dma);
+	set_dma_mode(idev->io.dma, DMA_MODE_READ);
+	set_dma_addr(idev->io.dma, virt_to_bus(idev->tx_buff.data));
+	set_dma_count(idev->io.dma, idev->tx_buff.len);
+#else
+	setup_dma(idev->io.dma, idev->tx_buff.data, idev->tx_buff.len, 
+		  DMA_MODE_WRITE);	
+#endif
+	idev->io.direction = IO_XMIT;
 	
 	/* Enable DMA */
  	switch_bank(iobase, SET0);
-	outb(inb(iobase+HCR) | HCR_EN_DMA, iobase+HCR);
-	
+#ifdef CONFIG_NETWINDER_TX_DMA_PROBLEMS
+	hcr = inb(iobase+HCR);
+	outb(hcr | HCR_EN_DMA, iobase+HCR);
+	enable_dma(idev->io.dma);
+	restore_flags(flags);
+#else	
+	outb(inb(iobase+HCR) | HCR_EN_DMA | HCR_TX_WT, iobase+HCR);
+#endif
+
 	/* Restore set register */
 	outb(set, iobase+SSR);
 }
@@ -577,17 +603,17 @@
 	int actual = 0;
 	__u8 set;
 	
-	DEBUG( 4, __FUNCTION__ "()\n");
+	DEBUG(4, __FUNCTION__ "()\n");
 
 	/* Save current bank */
-	set = inb( iobase+SSR);
+	set = inb(iobase+SSR);
 
-	switch_bank( iobase, SET0);
+	switch_bank(iobase, SET0);
 	if (!(inb_p(iobase+USR) & USR_TSRE)) {
-		DEBUG( 4, __FUNCTION__ "(), warning, FIFO not empty yet!\n");
+		DEBUG(4, __FUNCTION__ "(), warning, FIFO not empty yet!\n");
 
 		fifo_size -= 17;
-		DEBUG( 4, __FUNCTION__ "%d bytes left in tx fifo\n", fifo_size);
+		DEBUG(4, __FUNCTION__ "%d bytes left in tx fifo\n", fifo_size);
 	}
 
 	/* Fill FIFO with current frame */
@@ -597,7 +623,7 @@
 	}
         
 	DEBUG(4, __FUNCTION__ "(), fifo_size %d ; %d sent of %d\n", 
-	      fifo_size, actual, len);
+	       fifo_size, actual, len);
 
 	/* Restore bank */
 	outb(set, iobase+SSR);
@@ -617,7 +643,7 @@
 	int iobase;
 	__u8 set;
 
-	DEBUG(4, __FUNCTION__ "()\n");
+	DEBUG(4, __FUNCTION__ "(%ld)\n", jiffies);
 
 	ASSERT(idev != NULL, return;);
 	ASSERT(idev->magic == IRDA_DEVICE_MAGIC, return;);
@@ -663,9 +689,10 @@
  */
 int w83977af_dma_receive(struct irda_device *idev) 
 {
+	struct w83977af_ir *self;
 	int iobase;
 	__u8 set;
-#ifdef NETWINDER
+#ifdef CONFIG_NETWINDER_RX_DMA_PROBLEMS
 	unsigned long flags;
 	__u8 hcr;
 #endif
@@ -673,62 +700,60 @@
 	ASSERT(idev != NULL, return -1;);
 	ASSERT(idev->magic == IRDA_DEVICE_MAGIC, return -1;);
 
-	DEBUG(0, __FUNCTION__ "\n");
+	DEBUG(4, __FUNCTION__ "\n");
 
+	self = idev->priv;
 	iobase= idev->io.iobase;
 
 	/* Save current set */
-	set = inb( iobase+SSR);
+	set = inb(iobase+SSR);
 
 	/* Disable DMA */
-	switch_bank( iobase, SET0);
-	outb( inb( iobase+HCR) & ~HCR_EN_DMA, iobase+HCR);
+	switch_bank(iobase, SET0);
+	outb(inb(iobase+HCR) & ~HCR_EN_DMA, iobase+HCR);
+
+	/* Choose DMA Rx, DMA Fairness, and Advanced mode */
+	switch_bank(iobase, SET2);
+	outb((inb(iobase+ADCR1) & ~ADCR1_D_CHSW)/*|ADCR1_DMA_F*/|ADCR1_ADV_SL,
+	     iobase+ADCR1);
+
+	idev->io.direction = IO_RECV;
+	idev->rx_buff.data = idev->rx_buff.head;
 
-#ifdef NETWINDER
+#ifdef CONFIG_NETWINDER_RX_DMA_PROBLEMS
 	save_flags(flags);
 	cli();
 
-	disable_dma( idev->io.dma);
-	clear_dma_ff( idev->io.dma);
-	set_dma_mode( idev->io.dma, DMA_MODE_READ);
-	set_dma_addr( idev->io.dma, virt_to_bus(idev->rx_buff.data));
-	set_dma_count( idev->io.dma, idev->rx_buff.truesize);
+	disable_dma(idev->io.dma);
+	clear_dma_ff(idev->io.dma);
+	set_dma_mode(idev->io.dma, DMA_MODE_READ);
+	set_dma_addr(idev->io.dma, virt_to_bus(idev->rx_buff.data));
+	set_dma_count(idev->io.dma, idev->rx_buff.truesize);
 #else
-	setup_dma(idev->io.dma, idev->rx_buff.data, 
-		  idev->rx_buff.truesize, DMA_MODE_READ);
+	setup_dma(idev->io.dma, idev->rx_buff.data, idev->rx_buff.truesize, 
+		  DMA_MODE_READ);
 #endif
-	/* driver->media_busy = FALSE; */
-	idev->io.direction = IO_RECV;
-	idev->rx_buff.data = idev->rx_buff.head;
-
 	/* 
 	 * Reset Rx FIFO. This will also flush the ST_FIFO, it's very 
 	 * important that we don't reset the Tx FIFO since it might not
 	 * be finished transmitting yet
 	 */
-	outb( UFR_RXTL|UFR_TXTL|UFR_RXF_RST|UFR_EN_FIFO, iobase+UFR);
-	prev.status = 0;
-
-	/* Choose DMA Rx, DMA Fairness, and Advanced mode */
-	switch_bank(iobase, SET2);
-	outb(( inb( iobase+ADCR1) & ~ADCR1_D_CHSW)|ADCR1_DMA_F|ADCR1_ADV_SL,
-	     iobase+ADCR1);
+	switch_bank(iobase, SET0);
+	outb(UFR_RXTL|UFR_TXTL|UFR_RXF_RST|UFR_EN_FIFO, iobase+UFR);
+	self->st_fifo.len = self->st_fifo.tail = self->st_fifo.head = 0;
 	
 	/* Enable DMA */
 	switch_bank(iobase, SET0);
-#ifdef NETWINDER
-	hcr = inb( iobase+HCR);
-	enable_dma( idev->io.dma);
-	outb( hcr | HCR_EN_DMA, iobase+HCR);
+#ifdef CONFIG_NETWINDER_RX_DMA_PROBLEMS
+	hcr = inb(iobase+HCR);
+	outb(hcr | HCR_EN_DMA, iobase+HCR);
+	enable_dma(idev->io.dma);
 	restore_flags(flags);
 #else	
-	outb( inb( iobase+HCR) | HCR_EN_DMA, iobase+HCR);
+	outb(inb(iobase+HCR) | HCR_EN_DMA, iobase+HCR);
 #endif
-	
 	/* Restore set */
-	outb( set, iobase+SSR);
-
-	DEBUG( 4, __FUNCTION__ "(), done!\n");	
+	outb(set, iobase+SSR);
 
 	return 0;
 }
@@ -742,12 +767,17 @@
 int w83977af_dma_receive_complete(struct irda_device *idev)
 {
 	struct sk_buff *skb;
+	struct w83977af_ir *self;
+	struct st_fifo *st_fifo;
 	int len;
 	int iobase;
 	__u8 set;
 	__u8 status;
 
-	DEBUG(0, __FUNCTION__ "\n");
+	DEBUG(4, __FUNCTION__ "\n");
+
+	self = idev->priv;
+	st_fifo = &self->st_fifo;
 
 	iobase = idev->io.iobase;
 
@@ -756,22 +786,28 @@
 	
 	iobase = idev->io.iobase;
 
+	/* Read status FIFO */
 	switch_bank(iobase, SET5);
-	if (prev.status & FS_FO_FSFDR) {
-		status = prev.status;
-		len = prev.len;
+	while ((status = inb(iobase+FS_FO)) & FS_FO_FSFDR) {
+		st_fifo->entries[st_fifo->tail].status = status;
 		
-		prev.status = 0;
-	} else {
-		status = inb(iobase+FS_FO);
-		len = inb(iobase+RFLFL);
-		len |= inb(iobase+RFLFH) << 8;
+		st_fifo->entries[st_fifo->tail].len  = inb(iobase+RFLFL);
+		st_fifo->entries[st_fifo->tail].len |= inb(iobase+RFLFH) << 8;
+		
+		st_fifo->tail++;
+		st_fifo->len++;
 	}
+	
+	while (st_fifo->len) {
+		/* Get first entry */
+		status = st_fifo->entries[st_fifo->head].status;
+		len    = st_fifo->entries[st_fifo->head].len;
+		st_fifo->head++;
+		st_fifo->len--;
 
-	while (status & FS_FO_FSFDR) {
 		/* Check for errors */
 		if (status & FS_FO_ERR_MSK) {
-			if ( status & FS_FO_LST_FR) {
+			if (status & FS_FO_LST_FR) {
 				/* Add number of lost frames to stats */
 				idev->stats.rx_errors += len;	
 			} else {
@@ -800,14 +836,20 @@
 			/* Check if we have transfered all data to memory */
 			switch_bank(iobase, SET0);
 			if (inb(iobase+USR) & USR_RDR) {
+#ifdef CONFIG_USE_INTERNAL_TIMER
 				/* Put this entry back in fifo */
-				prev.status = status;
-				prev.len = len;
-
+				st_fifo->head--;
+				st_fifo->len++;
+				st_fifo->entries[st_fifo->head].status = status;
+				st_fifo->entries[st_fifo->head].len = len;
+				
 				/* Restore set register */
-				outb( set, iobase+SSR);
+				outb(set, iobase+SSR);
 			
 				return FALSE; 	/* I'll be back! */
+#else
+				udelay(80); /* Should be enough!? */
+#endif
 			}
 						
 			skb = dev_alloc_skb(len+1);
@@ -824,28 +866,23 @@
 			skb_reserve(skb, 1); 
 			
 			/* Copy frame without CRC */
-			if ( idev->io.baudrate < 4000000) {
-				skb_put( skb, len-2);
-				memcpy( skb->data, idev->rx_buff.data, len-2);
+			if (idev->io.baudrate < 4000000) {
+				skb_put(skb, len-2);
+				memcpy(skb->data, idev->rx_buff.data, len-2);
 			} else {
-				skb_put( skb, len-4);
-				memcpy( skb->data, idev->rx_buff.data, len-4);
+				skb_put(skb, len-4);
+				memcpy(skb->data, idev->rx_buff.data, len-4);
 			}
 
 			/* Move to next frame */
 			idev->rx_buff.data += len;
+			idev->stats.rx_packets++;
 			
 			skb->dev = &idev->netdev;
 			skb->mac.raw  = skb->data;
 			skb->protocol = htons(ETH_P_IRDA);
-			netif_rx( skb);
-			idev->stats.rx_packets++;
+			netif_rx(skb);
 		}
-		/* Read next entry in ST_FIFO */
-		switch_bank(iobase, SET5);
-		status = inb( iobase+FS_FO);
-		len = inb( iobase+RFLFL);
-		len |= inb( iobase+RFLFH) << 8;
 	}
 	/* Restore set register */
 	outb(set, iobase+SSR);
@@ -875,7 +912,6 @@
 	do {
 		byte = inb(iobase+RBR);
 		async_unwrap_char(idev, byte);
-
 	} while (inb(iobase+USR) & USR_RDR); /* Data available */	
 }
 
@@ -889,9 +925,12 @@
 {
 	int actual;
 	__u8 new_icr = 0;
+	__u8 set;
+	int iobase;
 
 	DEBUG(4, __FUNCTION__ "(), isr=%#x\n", isr);
 	
+	iobase = idev->io.iobase;
 	/* Transmit FIFO low on data */
 	if (isr & ISR_TXTH_I) {
 		/* Write data left in transmit buffer */
@@ -899,16 +938,21 @@
 					    idev->tx_buff.data, 
 					    idev->tx_buff.len, 
 					    idev->io.fifo_size);
+
 		idev->tx_buff.data += actual;
 		idev->tx_buff.len  -= actual;
 		
 		idev->io.direction = IO_XMIT;
 
 		/* Check if finished */
-		if (idev->tx_buff.len > 0)
+		if (idev->tx_buff.len > 0) {
 			new_icr |= ICR_ETXTHI;
-		else { 
-			DEBUG( 4, __FUNCTION__ "(), finished with frame!\n");
+		} else {
+			set = inb(iobase+SSR);
+			switch_bank(iobase, SET0);
+			outb(AUDR_SFEND, iobase+AUDR);
+			outb(set, iobase+SSR); 
+
 			idev->netdev.tbusy = 0; /* Unlock */
 			idev->stats.tx_packets++;
 
@@ -917,7 +961,6 @@
 
 			new_icr |= ICR_ETBREI;
 		}
-		
 	}
 	/* Check if transmission has completed */
 	if (isr & ISR_TXEMP_I) {
@@ -943,22 +986,20 @@
  *    Handle MIR/FIR interrupt
  *
  */
-static __u8 w83977af_fir_interrupt( struct irda_device *idev, int isr)
+static __u8 w83977af_fir_interrupt(struct irda_device *idev, int isr)
 {
 	__u8 new_icr = 0;
 	__u8 set;
 	int iobase;
 
-	DEBUG( 4, __FUNCTION__ "(), isr=%#x\n", isr);
-
 	iobase = idev->io.iobase;
-
 	set = inb(iobase+SSR);
 	
 	/* End of frame detected in FIFO */
 	if (isr & (ISR_FEND_I|ISR_FSF_I)) {
 		if (w83977af_dma_receive_complete(idev)) {
 			
+			/* Wait for next status FIFO interrupt */
 			new_icr |= ICR_EFSFI;
 		} else {
 			/* DMA not finished yet */
@@ -982,7 +1023,7 @@
 
 		/* Clear timer event */
 		/* switch_bank(iobase, SET0); */
-/* 		outb( ASCR_CTE, iobase+ASCR); */
+/* 		outb(ASCR_CTE, iobase+ASCR); */
 
 		/* Check if this is a TX timer interrupt */
 		if (idev->io.direction == IO_XMIT) {
@@ -998,15 +1039,18 @@
 	}	
 	/* Finished with DMA */
 	if (isr & ISR_DMA_I) {
-		w83977af_dma_xmit_complete( idev);
-		
+		w83977af_dma_xmit_complete(idev);
+
 		/* Check if there are more frames to be transmitted */
-		if (irda_device_txqueue_empty( idev)) {
+		/* if (irda_device_txqueue_empty(idev)) { */
 		
-			/* Prepare for receive */
-			w83977af_dma_receive(idev);
-			new_icr = ICR_EFSFI;
-		}
+		/* Prepare for receive 
+		 * 
+		 * ** Netwinder Tx DMA likes that we do this anyway **
+		 */
+		w83977af_dma_receive(idev);
+		new_icr = ICR_EFSFI;
+	       /* } */
 	}
 	
 	/* Restore set */
@@ -1030,7 +1074,7 @@
 
 	if (idev == NULL) {
 		printk(KERN_WARNING "%s: irq %d for unknown device.\n", 
-		       driver_name, irq);
+			driver_name, irq);
 		return;
 	}
 
@@ -1049,7 +1093,7 @@
 	
 	if (isr) {
 		/* Dispatch interrupt handler for the current speed */
-		if ( idev->io.baudrate > 115200)
+		if (idev->io.baudrate > PIO_MAX_SPEED )
 			icr = w83977af_fir_interrupt(idev, isr);
 		else
 			icr = w83977af_sir_interrupt(idev, isr);
@@ -1070,7 +1114,7 @@
 static void w83977af_wait_until_sent(struct irda_device *idev)
 {
 	current->state = TASK_INTERRUPTIBLE;
-	schedule_timeout(6);
+	schedule_timeout(60*HZ/1000);
 }
 
 /*
@@ -1085,16 +1129,16 @@
 	int iobase;
 	__u8 set;
 
-	ASSERT( idev != NULL, return FALSE;);
-	ASSERT( idev->magic == IRDA_DEVICE_MAGIC, return FALSE;);
+	ASSERT(idev != NULL, return FALSE;);
+	ASSERT(idev->magic == IRDA_DEVICE_MAGIC, return FALSE;);
 
-	if ( idev->io.baudrate > 115200) {
+	if (idev->io.baudrate > 115200) {
 		iobase = idev->io.iobase;
 
 		/* Check if rx FIFO is not empty */
 		set = inb(iobase+SSR);
-		switch_bank( iobase, SET2);
-		if (( inb( iobase+RXFDTH) & 0x3f) != 0) {
+		switch_bank(iobase, SET2);
+		if ((inb(iobase+RXFDTH) & 0x3f) != 0) {
 			/* We are receiving something */
 			status =  TRUE;
 		}
@@ -1111,12 +1155,12 @@
  *    
  *
  */
-static int w83977af_net_init( struct device *dev)
+static int w83977af_net_init(struct device *dev)
 {
 	DEBUG(0, __FUNCTION__ "()\n");
 
 	/* Set up to be a normal IrDA network device driver */
-	irda_device_setup( dev);
+	irda_device_setup(dev);
 
 	/* Insert overrides below this line! */
 
@@ -1130,7 +1174,7 @@
  *    Start the device
  *
  */
-static int w83977af_net_open( struct device *dev)
+static int w83977af_net_open(struct device *dev)
 {
 	struct irda_device *idev;
 	int iobase;
@@ -1147,7 +1191,7 @@
 	iobase = idev->io.iobase;
 
 	if (request_irq(idev->io.irq, w83977af_interrupt, 0, idev->name, 
-			(void *) idev)) {
+			 (void *) idev)) {
 		return -EAGAIN;
 	}
 	/*
@@ -1170,13 +1214,13 @@
  	/* Enable some interrupts so we can receive frames again */
  	switch_bank(iobase, SET0);
  	if (idev->io.baudrate > 115200) {
- 		outb( ICR_EFSFI, iobase+ICR);
- 		w83977af_dma_receive( idev);
+ 		outb(ICR_EFSFI, iobase+ICR);
+ 		w83977af_dma_receive(idev);
  	} else
- 		outb( ICR_ERBRI, iobase+ICR);
+ 		outb(ICR_ERBRI, iobase+ICR);
 
 	/* Restore bank register */
-	outb( set, iobase+SSR);
+	outb(set, iobase+SSR);
 
 	MOD_INC_USE_COUNT;
 
@@ -1195,34 +1239,34 @@
 	int iobase;
 	__u8 set;
 
-	DEBUG( 0, __FUNCTION__ "()\n");
+	DEBUG(0, __FUNCTION__ "()\n");
 	
 	/* Stop device */
 	dev->tbusy = 1;
 	dev->start = 0;
 
-	ASSERT( dev != NULL, return -1;);
+	ASSERT(dev != NULL, return -1;);
 	idev = (struct irda_device *) dev->priv;
 	
-	ASSERT( idev != NULL, return 0;);
-	ASSERT( idev->magic == IRDA_DEVICE_MAGIC, return 0;);
+	ASSERT(idev != NULL, return 0;);
+	ASSERT(idev->magic == IRDA_DEVICE_MAGIC, return 0;);
 	
 	iobase = idev->io.iobase;
 
-	disable_dma( idev->io.dma);
+	disable_dma(idev->io.dma);
 
 	/* Save current set */
-	set = inb( iobase+SSR);
+	set = inb(iobase+SSR);
 	
 	/* Disable interrupts */
-	switch_bank( iobase, SET0);
-	outb( 0, iobase+ICR); 
+	switch_bank(iobase, SET0);
+	outb(0, iobase+ICR); 
 
-	free_irq( idev->io.irq, idev);
-	free_dma( idev->io.dma);
+	free_irq(idev->io.irq, idev);
+	free_dma(idev->io.dma);
 
 	/* Restore bank register */
-	outb( set, iobase+SSR);
+	outb(set, iobase+SSR);
 
 	MOD_DEC_USE_COUNT;
 
@@ -1230,6 +1274,11 @@
 }
 
 #ifdef MODULE
+
+MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
+MODULE_DESCRIPTION("Winbond W83977AF IrDA Device Driver");
+
+MODULE_PARM(qos_mtt_bits, "i");
 
 /*
  * Function init_module (void)

FUNET's LINUX-ADM group, linux-adm@nic.funet.fi
TCL-scripts by Sam Shen (who was at: slshen@lbl.gov)