1 /*********************************************************************
3 * Filename: w83977af_ir.c
5 * Description: FIR driver for the Winbond W83977AF Super I/O chip
6 * Status: Experimental.
7 * Author: Paul VanderSpek
8 * Created at: Wed Nov 4 11:46:16 1998
9 * Modified at: Fri Jan 28 12:10:59 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-1999 Rebel.com
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License as
17 * published by the Free Software Foundation; either version 2 of
18 * the License, or (at your option) any later version.
20 * Neither Paul VanderSpek nor Rebel.com admit liability nor provide
21 * warranty for any of this software. This material is provided "AS-IS"
24 * If you find bugs in this file, its very likely that the same bug
25 * will also be in pc87108.c since the implementations are quite
28 * Notice that all functions that needs to access the chip in _any_
29 * way, must save BSR register on entry, and restore it on exit.
30 * It is _very_ important to follow this policy!
34 * bank = inb( iobase+BSR);
36 * do_your_stuff_here();
38 * outb( bank, iobase+BSR);
40 ********************************************************************/
42 #include <linux/module.h>
43 #include <linux/config.h>
44 #include <linux/kernel.h>
45 #include <linux/types.h>
46 #include <linux/skbuff.h>
47 #include <linux/netdevice.h>
48 #include <linux/ioport.h>
49 #include <linux/delay.h>
50 #include <linux/slab.h>
51 #include <linux/init.h>
52 #include <linux/rtnetlink.h>
56 #include <asm/byteorder.h>
58 #include <net/irda/irda.h>
59 #include <net/irda/wrapper.h>
60 #include <net/irda/irda_device.h>
62 #include "w83977af_ir.h"
64 #ifdef CONFIG_ARCH_NETWINDER /* Adjust to NetWinder differences */
65 #undef CONFIG_NETWINDER_TX_DMA_PROBLEMS /* Not needed */
66 #define CONFIG_NETWINDER_RX_DMA_PROBLEMS /* Must have this one! */
68 #undef CONFIG_USE_INTERNAL_TIMER /* Just cannot make that timer work */
69 #define CONFIG_USE_W977_PNP /* Currently needed */
70 #define PIO_MAX_SPEED 115200
72 static char *driver_name = "w83977af_ir";
73 static int qos_mtt_bits = 0x07; /* 1 ms or more */
75 #define CHIP_IO_EXTENT 8
77 static unsigned int io[] = { 0x180, ~0, ~0, ~0 };
78 #ifdef CONFIG_ARCH_NETWINDER /* Adjust to NetWinder differences */
79 static unsigned int irq[] = { 6, 0, 0, 0 };
81 static unsigned int irq[] = { 11, 0, 0, 0 };
83 static unsigned int dma[] = { 1, 0, 0, 0 };
84 static unsigned int efbase[] = { W977_EFIO_BASE, W977_EFIO2_BASE };
85 static unsigned int efio = W977_EFIO_BASE;
87 static struct w83977af_ir *dev_self[] = { NULL, NULL, NULL, NULL};
90 static int w83977af_open(int i, unsigned int iobase, unsigned int irq,
92 static int w83977af_close(struct w83977af_ir *self);
93 static int w83977af_probe(int iobase, int irq, int dma);
94 static int w83977af_dma_receive(struct w83977af_ir *self);
95 static int w83977af_dma_receive_complete(struct w83977af_ir *self);
96 static int w83977af_hard_xmit(struct sk_buff *skb, struct net_device *dev);
97 static int w83977af_pio_write(int iobase, __u8 *buf, int len, int fifo_size);
98 static void w83977af_dma_write(struct w83977af_ir *self, int iobase);
99 static void w83977af_change_speed(struct w83977af_ir *self, __u32 speed);
100 static int w83977af_is_receiving(struct w83977af_ir *self);
102 static int w83977af_net_open(struct net_device *dev);
103 static int w83977af_net_close(struct net_device *dev);
104 static int w83977af_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
105 static struct net_device_stats *w83977af_net_get_stats(struct net_device *dev);
108 * Function w83977af_init ()
110 * Initialize chip. Just try to find out how many chips we are dealing with
113 static int __init w83977af_init(void)
117 IRDA_DEBUG(0, "%s()\n", __FUNCTION__ );
119 for (i=0; (io[i] < 2000) && (i < 4); i++) {
120 if (w83977af_open(i, io[i], irq[i], dma[i]) == 0)
127 * Function w83977af_cleanup ()
129 * Close all configured chips
132 static void __exit w83977af_cleanup(void)
136 IRDA_DEBUG(4, "%s()\n", __FUNCTION__ );
138 for (i=0; i < 4; i++) {
140 w83977af_close(dev_self[i]);
145 * Function w83977af_open (iobase, irq)
147 * Open driver instance
150 int w83977af_open(int i, unsigned int iobase, unsigned int irq,
153 struct net_device *dev;
154 struct w83977af_ir *self;
157 IRDA_DEBUG(0, "%s()\n", __FUNCTION__ );
159 /* Lock the port that we need */
160 if (!request_region(iobase, CHIP_IO_EXTENT, driver_name)) {
161 IRDA_DEBUG(0, "%s(), can't get iobase of 0x%03x\n",
162 __FUNCTION__ , iobase);
166 if (w83977af_probe(iobase, irq, dma) == -1) {
171 * Allocate new instance of the driver
173 dev = alloc_irdadev(sizeof(struct w83977af_ir));
175 printk( KERN_ERR "IrDA: Can't allocate memory for "
176 "IrDA control block!\n");
182 spin_lock_init(&self->lock);
186 self->io.fir_base = iobase;
188 self->io.fir_ext = CHIP_IO_EXTENT;
190 self->io.fifo_size = 32;
192 /* Initialize QoS for this device */
193 irda_init_max_qos_capabilies(&self->qos);
195 /* The only value we must override it the baudrate */
197 /* FIXME: The HP HDLS-1100 does not support 1152000! */
198 self->qos.baud_rate.bits = IR_9600|IR_19200|IR_38400|IR_57600|
199 IR_115200|IR_576000|IR_1152000|(IR_4000000 << 8);
201 /* The HP HDLS-1100 needs 1 ms according to the specs */
202 self->qos.min_turn_time.bits = qos_mtt_bits;
203 irda_qos_bits_to_value(&self->qos);
205 /* Max DMA buffer size needed = (data_size + 6) * (window_size) + 6; */
206 self->rx_buff.truesize = 14384;
207 self->tx_buff.truesize = 4000;
209 /* Allocate memory if needed */
210 self->rx_buff.head = (__u8 *) kmalloc(self->rx_buff.truesize,
212 if (self->rx_buff.head == NULL) {
217 memset(self->rx_buff.head, 0, self->rx_buff.truesize);
219 self->tx_buff.head = (__u8 *) kmalloc(self->tx_buff.truesize,
221 if (self->tx_buff.head == NULL) {
225 memset(self->tx_buff.head, 0, self->tx_buff.truesize);
227 self->rx_buff.in_frame = FALSE;
228 self->rx_buff.state = OUTSIDE_FRAME;
229 self->tx_buff.data = self->tx_buff.head;
230 self->rx_buff.data = self->rx_buff.head;
233 /* Keep track of module usage */
234 SET_MODULE_OWNER(dev);
236 /* Override the network functions we need to use */
237 dev->hard_start_xmit = w83977af_hard_xmit;
238 dev->open = w83977af_net_open;
239 dev->stop = w83977af_net_close;
240 dev->do_ioctl = w83977af_net_ioctl;
241 dev->get_stats = w83977af_net_get_stats;
243 err = register_netdev(dev);
245 ERROR("%s(), register_netdevice() failed!\n", __FUNCTION__);
248 MESSAGE("IrDA: Registered device %s\n", dev->name);
250 /* Need to store self somewhere */
255 kfree(self->tx_buff.head);
257 kfree(self->rx_buff.head);
261 release_region(iobase, CHIP_IO_EXTENT);
266 * Function w83977af_close (self)
268 * Close driver instance
271 static int w83977af_close(struct w83977af_ir *self)
275 IRDA_DEBUG(0, "%s()\n", __FUNCTION__ );
277 iobase = self->io.fir_base;
279 #ifdef CONFIG_USE_W977_PNP
280 /* enter PnP configuration mode */
281 w977_efm_enter(efio);
283 w977_select_device(W977_DEVICE_IR, efio);
285 /* Deactivate device */
286 w977_write_reg(0x30, 0x00, efio);
289 #endif /* CONFIG_USE_W977_PNP */
291 /* Remove netdevice */
292 unregister_netdev(self->netdev);
294 /* Release the PORT that this driver is using */
295 IRDA_DEBUG(0 , "%s(), Releasing Region %03x\n",
296 __FUNCTION__ , self->io.fir_base);
297 release_region(self->io.fir_base, self->io.fir_ext);
299 if (self->tx_buff.head)
300 kfree(self->tx_buff.head);
302 if (self->rx_buff.head)
303 kfree(self->rx_buff.head);
305 free_netdev(self->netdev);
310 int w83977af_probe( int iobase, int irq, int dma)
315 for (i=0; i < 2; i++) {
316 IRDA_DEBUG( 0, "%s()\n", __FUNCTION__ );
317 #ifdef CONFIG_USE_W977_PNP
318 /* Enter PnP configuration mode */
319 w977_efm_enter(efbase[i]);
321 w977_select_device(W977_DEVICE_IR, efbase[i]);
323 /* Configure PnP port, IRQ, and DMA channel */
324 w977_write_reg(0x60, (iobase >> 8) & 0xff, efbase[i]);
325 w977_write_reg(0x61, (iobase) & 0xff, efbase[i]);
327 w977_write_reg(0x70, irq, efbase[i]);
328 #ifdef CONFIG_ARCH_NETWINDER
329 /* Netwinder uses 1 higher than Linux */
330 w977_write_reg(0x74, dma+1, efbase[i]);
332 w977_write_reg(0x74, dma, efbase[i]);
333 #endif /*CONFIG_ARCH_NETWINDER */
334 w977_write_reg(0x75, 0x04, efbase[i]); /* Disable Tx DMA */
336 /* Set append hardware CRC, enable IR bank selection */
337 w977_write_reg(0xf0, APEDCRC|ENBNKSEL, efbase[i]);
339 /* Activate device */
340 w977_write_reg(0x30, 0x01, efbase[i]);
342 w977_efm_exit(efbase[i]);
343 #endif /* CONFIG_USE_W977_PNP */
344 /* Disable Advanced mode */
345 switch_bank(iobase, SET2);
346 outb(iobase+2, 0x00);
348 /* Turn on UART (global) interrupts */
349 switch_bank(iobase, SET0);
350 outb(HCR_EN_IRQ, iobase+HCR);
352 /* Switch to advanced mode */
353 switch_bank(iobase, SET2);
354 outb(inb(iobase+ADCR1) | ADCR1_ADV_SL, iobase+ADCR1);
356 /* Set default IR-mode */
357 switch_bank(iobase, SET0);
358 outb(HCR_SIR, iobase+HCR);
360 /* Read the Advanced IR ID */
361 switch_bank(iobase, SET3);
362 version = inb(iobase+AUID);
365 if (0x10 == (version & 0xf0)) {
368 /* Set FIFO size to 32 */
369 switch_bank(iobase, SET2);
370 outb(ADCR2_RXFS32|ADCR2_TXFS32, iobase+ADCR2);
372 /* Set FIFO threshold to TX17, RX16 */
373 switch_bank(iobase, SET0);
374 outb(UFR_RXTL|UFR_TXTL|UFR_TXF_RST|UFR_RXF_RST|
375 UFR_EN_FIFO,iobase+UFR);
377 /* Receiver frame length */
378 switch_bank(iobase, SET4);
379 outb(2048 & 0xff, iobase+6);
380 outb((2048 >> 8) & 0x1f, iobase+7);
383 * Init HP HSDL-1100 transceiver.
385 * Set IRX_MSL since we have 2 * receive paths IRRX,
386 * and IRRXH. Clear IRSL0D since we want IRSL0 * to
387 * be a input pin used for IRRXH
389 * IRRX pin 37 connected to receiver
390 * IRTX pin 38 connected to transmitter
391 * FIRRX pin 39 connected to receiver (IRSL0)
392 * CIRRX pin 40 connected to pin 37
394 switch_bank(iobase, SET7);
395 outb(0x40, iobase+7);
397 MESSAGE("W83977AF (IR) driver loaded. "
398 "Version: 0x%02x\n", version);
402 /* Try next extented function register address */
403 IRDA_DEBUG( 0, "%s(), Wrong chip version", __FUNCTION__ );
409 void w83977af_change_speed(struct w83977af_ir *self, __u32 speed)
411 int ir_mode = HCR_SIR;
415 iobase = self->io.fir_base;
417 /* Update accounting for new speed */
418 self->io.speed = speed;
420 /* Save current bank */
421 set = inb(iobase+SSR);
423 /* Disable interrupts */
424 switch_bank(iobase, SET0);
428 switch_bank(iobase, SET2);
429 outb(0x00, iobase+ABHL);
432 case 9600: outb(0x0c, iobase+ABLL); break;
433 case 19200: outb(0x06, iobase+ABLL); break;
434 case 38400: outb(0x03, iobase+ABLL); break;
435 case 57600: outb(0x02, iobase+ABLL); break;
436 case 115200: outb(0x01, iobase+ABLL); break;
438 ir_mode = HCR_MIR_576;
439 IRDA_DEBUG(0, "%s(), handling baud of 576000\n", __FUNCTION__ );
442 ir_mode = HCR_MIR_1152;
443 IRDA_DEBUG(0, "%s(), handling baud of 1152000\n", __FUNCTION__ );
447 IRDA_DEBUG(0, "%s(), handling baud of 4000000\n", __FUNCTION__ );
451 IRDA_DEBUG(0, "%s(), unknown baud rate of %d\n", __FUNCTION__ , speed);
456 switch_bank(iobase, SET0);
457 outb(ir_mode, iobase+HCR);
459 /* set FIFO size to 32 */
460 switch_bank(iobase, SET2);
461 outb(ADCR2_RXFS32|ADCR2_TXFS32, iobase+ADCR2);
463 /* set FIFO threshold to TX17, RX16 */
464 switch_bank(iobase, SET0);
465 outb(0x00, iobase+UFR); /* Reset */
466 outb(UFR_EN_FIFO, iobase+UFR); /* First we must enable FIFO */
467 outb(0xa7, iobase+UFR);
469 netif_wake_queue(self->netdev);
471 /* Enable some interrupts so we can receive frames */
472 switch_bank(iobase, SET0);
473 if (speed > PIO_MAX_SPEED) {
474 outb(ICR_EFSFI, iobase+ICR);
475 w83977af_dma_receive(self);
477 outb(ICR_ERBRI, iobase+ICR);
480 outb(set, iobase+SSR);
484 * Function w83977af_hard_xmit (skb, dev)
486 * Sets up a DMA transfer to send the current frame.
489 int w83977af_hard_xmit(struct sk_buff *skb, struct net_device *dev)
491 struct w83977af_ir *self;
497 self = (struct w83977af_ir *) dev->priv;
499 iobase = self->io.fir_base;
501 IRDA_DEBUG(4, "%s(%ld), skb->len=%d\n", __FUNCTION__ , jiffies,
504 /* Lock transmit buffer */
505 netif_stop_queue(dev);
507 /* Check if we need to change the speed */
508 speed = irda_get_next_speed(skb);
509 if ((speed != self->io.speed) && (speed != -1)) {
510 /* Check for empty frame */
512 w83977af_change_speed(self, speed);
513 dev->trans_start = jiffies;
517 self->new_speed = speed;
520 /* Save current set */
521 set = inb(iobase+SSR);
523 /* Decide if we should use PIO or DMA transfer */
524 if (self->io.speed > PIO_MAX_SPEED) {
525 self->tx_buff.data = self->tx_buff.head;
526 memcpy(self->tx_buff.data, skb->data, skb->len);
527 self->tx_buff.len = skb->len;
529 mtt = irda_get_mtt(skb);
530 #ifdef CONFIG_USE_INTERNAL_TIMER
532 /* Adjust for timer resolution */
536 switch_bank(iobase, SET4);
537 outb(mtt & 0xff, iobase+TMRL);
538 outb((mtt >> 8) & 0x0f, iobase+TMRH);
541 outb(IR_MSL_EN_TMR, iobase+IR_MSL);
542 self->io.direction = IO_XMIT;
544 /* Enable timer interrupt */
545 switch_bank(iobase, SET0);
546 outb(ICR_ETMRI, iobase+ICR);
549 IRDA_DEBUG(4, "%s(%ld), mtt=%d\n", __FUNCTION__ , jiffies, mtt);
553 /* Enable DMA interrupt */
554 switch_bank(iobase, SET0);
555 outb(ICR_EDMAI, iobase+ICR);
556 w83977af_dma_write(self, iobase);
557 #ifdef CONFIG_USE_INTERNAL_TIMER
561 self->tx_buff.data = self->tx_buff.head;
562 self->tx_buff.len = async_wrap_skb(skb, self->tx_buff.data,
563 self->tx_buff.truesize);
565 /* Add interrupt on tx low level (will fire immediately) */
566 switch_bank(iobase, SET0);
567 outb(ICR_ETXTHI, iobase+ICR);
569 dev->trans_start = jiffies;
572 /* Restore set register */
573 outb(set, iobase+SSR);
579 * Function w83977af_dma_write (self, iobase)
581 * Send frame using DMA
584 static void w83977af_dma_write(struct w83977af_ir *self, int iobase)
587 #ifdef CONFIG_NETWINDER_TX_DMA_PROBLEMS
591 IRDA_DEBUG(4, "%s(), len=%d\n", __FUNCTION__ , self->tx_buff.len);
593 /* Save current set */
594 set = inb(iobase+SSR);
597 switch_bank(iobase, SET0);
598 outb(inb(iobase+HCR) & ~HCR_EN_DMA, iobase+HCR);
600 /* Choose transmit DMA channel */
601 switch_bank(iobase, SET2);
602 outb(ADCR1_D_CHSW|/*ADCR1_DMA_F|*/ADCR1_ADV_SL, iobase+ADCR1);
603 #ifdef CONFIG_NETWINDER_TX_DMA_PROBLEMS
604 spin_lock_irqsave(&self->lock, flags);
606 disable_dma(self->io.dma);
607 clear_dma_ff(self->io.dma);
608 set_dma_mode(self->io.dma, DMA_MODE_READ);
609 set_dma_addr(self->io.dma, isa_virt_to_bus(self->tx_buff.data));
610 set_dma_count(self->io.dma, self->tx_buff.len);
612 irda_setup_dma(self->io.dma, self->tx_buff.data, self->tx_buff.len,
615 self->io.direction = IO_XMIT;
618 switch_bank(iobase, SET0);
619 #ifdef CONFIG_NETWINDER_TX_DMA_PROBLEMS
620 hcr = inb(iobase+HCR);
621 outb(hcr | HCR_EN_DMA, iobase+HCR);
622 enable_dma(self->io.dma);
623 spin_unlock_irqrestore(&self->lock, flags);
625 outb(inb(iobase+HCR) | HCR_EN_DMA | HCR_TX_WT, iobase+HCR);
628 /* Restore set register */
629 outb(set, iobase+SSR);
633 * Function w83977af_pio_write (iobase, buf, len, fifo_size)
638 static int w83977af_pio_write(int iobase, __u8 *buf, int len, int fifo_size)
643 IRDA_DEBUG(4, "%s()\n", __FUNCTION__ );
645 /* Save current bank */
646 set = inb(iobase+SSR);
648 switch_bank(iobase, SET0);
649 if (!(inb_p(iobase+USR) & USR_TSRE)) {
651 "%s(), warning, FIFO not empty yet!\n", __FUNCTION__ );
654 IRDA_DEBUG(4, "%s(), %d bytes left in tx fifo\n",
655 __FUNCTION__ , fifo_size);
658 /* Fill FIFO with current frame */
659 while ((fifo_size-- > 0) && (actual < len)) {
660 /* Transmit next byte */
661 outb(buf[actual++], iobase+TBR);
664 IRDA_DEBUG(4, "%s(), fifo_size %d ; %d sent of %d\n",
665 __FUNCTION__ , fifo_size, actual, len);
668 outb(set, iobase+SSR);
674 * Function w83977af_dma_xmit_complete (self)
676 * The transfer of a frame in finished. So do the necessary things
680 static void w83977af_dma_xmit_complete(struct w83977af_ir *self)
685 IRDA_DEBUG(4, "%s(%ld)\n", __FUNCTION__ , jiffies);
687 ASSERT(self != NULL, return;);
689 iobase = self->io.fir_base;
691 /* Save current set */
692 set = inb(iobase+SSR);
695 switch_bank(iobase, SET0);
696 outb(inb(iobase+HCR) & ~HCR_EN_DMA, iobase+HCR);
698 /* Check for underrrun! */
699 if (inb(iobase+AUDR) & AUDR_UNDR) {
700 IRDA_DEBUG(0, "%s(), Transmit underrun!\n", __FUNCTION__ );
702 self->stats.tx_errors++;
703 self->stats.tx_fifo_errors++;
705 /* Clear bit, by writing 1 to it */
706 outb(AUDR_UNDR, iobase+AUDR);
708 self->stats.tx_packets++;
711 if (self->new_speed) {
712 w83977af_change_speed(self, self->new_speed);
716 /* Unlock tx_buff and request another frame */
717 /* Tell the network layer, that we want more frames */
718 netif_wake_queue(self->netdev);
721 outb(set, iobase+SSR);
725 * Function w83977af_dma_receive (self)
727 * Get ready for receiving a frame. The device will initiate a DMA
728 * if it starts to receive a frame.
731 int w83977af_dma_receive(struct w83977af_ir *self)
735 #ifdef CONFIG_NETWINDER_RX_DMA_PROBLEMS
739 ASSERT(self != NULL, return -1;);
741 IRDA_DEBUG(4, "%s\n", __FUNCTION__ );
743 iobase= self->io.fir_base;
745 /* Save current set */
746 set = inb(iobase+SSR);
749 switch_bank(iobase, SET0);
750 outb(inb(iobase+HCR) & ~HCR_EN_DMA, iobase+HCR);
752 /* Choose DMA Rx, DMA Fairness, and Advanced mode */
753 switch_bank(iobase, SET2);
754 outb((inb(iobase+ADCR1) & ~ADCR1_D_CHSW)/*|ADCR1_DMA_F*/|ADCR1_ADV_SL,
757 self->io.direction = IO_RECV;
758 self->rx_buff.data = self->rx_buff.head;
760 #ifdef CONFIG_NETWINDER_RX_DMA_PROBLEMS
761 spin_lock_irqsave(&self->lock, flags);
763 disable_dma(self->io.dma);
764 clear_dma_ff(self->io.dma);
765 set_dma_mode(self->io.dma, DMA_MODE_READ);
766 set_dma_addr(self->io.dma, isa_virt_to_bus(self->rx_buff.data));
767 set_dma_count(self->io.dma, self->rx_buff.truesize);
769 irda_setup_dma(self->io.dma, self->rx_buff.data, self->rx_buff.truesize,
773 * Reset Rx FIFO. This will also flush the ST_FIFO, it's very
774 * important that we don't reset the Tx FIFO since it might not
775 * be finished transmitting yet
777 switch_bank(iobase, SET0);
778 outb(UFR_RXTL|UFR_TXTL|UFR_RXF_RST|UFR_EN_FIFO, iobase+UFR);
779 self->st_fifo.len = self->st_fifo.tail = self->st_fifo.head = 0;
782 switch_bank(iobase, SET0);
783 #ifdef CONFIG_NETWINDER_RX_DMA_PROBLEMS
784 hcr = inb(iobase+HCR);
785 outb(hcr | HCR_EN_DMA, iobase+HCR);
786 enable_dma(self->io.dma);
787 spin_unlock_irqrestore(&self->lock, flags);
789 outb(inb(iobase+HCR) | HCR_EN_DMA, iobase+HCR);
792 outb(set, iobase+SSR);
798 * Function w83977af_receive_complete (self)
800 * Finished with receiving a frame
803 int w83977af_dma_receive_complete(struct w83977af_ir *self)
806 struct st_fifo *st_fifo;
812 IRDA_DEBUG(4, "%s\n", __FUNCTION__ );
814 st_fifo = &self->st_fifo;
816 iobase = self->io.fir_base;
818 /* Save current set */
819 set = inb(iobase+SSR);
821 iobase = self->io.fir_base;
823 /* Read status FIFO */
824 switch_bank(iobase, SET5);
825 while ((status = inb(iobase+FS_FO)) & FS_FO_FSFDR) {
826 st_fifo->entries[st_fifo->tail].status = status;
828 st_fifo->entries[st_fifo->tail].len = inb(iobase+RFLFL);
829 st_fifo->entries[st_fifo->tail].len |= inb(iobase+RFLFH) << 8;
835 while (st_fifo->len) {
836 /* Get first entry */
837 status = st_fifo->entries[st_fifo->head].status;
838 len = st_fifo->entries[st_fifo->head].len;
842 /* Check for errors */
843 if (status & FS_FO_ERR_MSK) {
844 if (status & FS_FO_LST_FR) {
845 /* Add number of lost frames to stats */
846 self->stats.rx_errors += len;
849 self->stats.rx_errors++;
851 self->rx_buff.data += len;
853 if (status & FS_FO_MX_LEX)
854 self->stats.rx_length_errors++;
856 if (status & FS_FO_PHY_ERR)
857 self->stats.rx_frame_errors++;
859 if (status & FS_FO_CRC_ERR)
860 self->stats.rx_crc_errors++;
862 /* The errors below can be reported in both cases */
863 if (status & FS_FO_RX_OV)
864 self->stats.rx_fifo_errors++;
866 if (status & FS_FO_FSF_OV)
867 self->stats.rx_fifo_errors++;
870 /* Check if we have transferred all data to memory */
871 switch_bank(iobase, SET0);
872 if (inb(iobase+USR) & USR_RDR) {
873 #ifdef CONFIG_USE_INTERNAL_TIMER
874 /* Put this entry back in fifo */
877 st_fifo->entries[st_fifo->head].status = status;
878 st_fifo->entries[st_fifo->head].len = len;
880 /* Restore set register */
881 outb(set, iobase+SSR);
883 return FALSE; /* I'll be back! */
885 udelay(80); /* Should be enough!? */
889 skb = dev_alloc_skb(len+1);
892 "%s(), memory squeeze, dropping frame.\n", __FUNCTION__);
893 /* Restore set register */
894 outb(set, iobase+SSR);
899 /* Align to 20 bytes */
902 /* Copy frame without CRC */
903 if (self->io.speed < 4000000) {
905 memcpy(skb->data, self->rx_buff.data, len-2);
908 memcpy(skb->data, self->rx_buff.data, len-4);
911 /* Move to next frame */
912 self->rx_buff.data += len;
913 self->stats.rx_packets++;
915 skb->dev = self->netdev;
916 skb->mac.raw = skb->data;
917 skb->protocol = htons(ETH_P_IRDA);
919 self->netdev->last_rx = jiffies;
922 /* Restore set register */
923 outb(set, iobase+SSR);
929 * Function pc87108_pio_receive (self)
931 * Receive all data in receiver FIFO
934 static void w83977af_pio_receive(struct w83977af_ir *self)
939 IRDA_DEBUG(4, "%s()\n", __FUNCTION__ );
941 ASSERT(self != NULL, return;);
943 iobase = self->io.fir_base;
945 /* Receive all characters in Rx FIFO */
947 byte = inb(iobase+RBR);
948 async_unwrap_char(self->netdev, &self->stats, &self->rx_buff,
950 } while (inb(iobase+USR) & USR_RDR); /* Data available */
954 * Function w83977af_sir_interrupt (self, eir)
956 * Handle SIR interrupt
959 static __u8 w83977af_sir_interrupt(struct w83977af_ir *self, int isr)
966 IRDA_DEBUG(4, "%s(), isr=%#x\n", __FUNCTION__ , isr);
968 iobase = self->io.fir_base;
969 /* Transmit FIFO low on data */
970 if (isr & ISR_TXTH_I) {
971 /* Write data left in transmit buffer */
972 actual = w83977af_pio_write(self->io.fir_base,
977 self->tx_buff.data += actual;
978 self->tx_buff.len -= actual;
980 self->io.direction = IO_XMIT;
982 /* Check if finished */
983 if (self->tx_buff.len > 0) {
984 new_icr |= ICR_ETXTHI;
986 set = inb(iobase+SSR);
987 switch_bank(iobase, SET0);
988 outb(AUDR_SFEND, iobase+AUDR);
989 outb(set, iobase+SSR);
991 self->stats.tx_packets++;
993 /* Feed me more packets */
994 netif_wake_queue(self->netdev);
995 new_icr |= ICR_ETBREI;
998 /* Check if transmission has completed */
999 if (isr & ISR_TXEMP_I) {
1000 /* Check if we need to change the speed? */
1001 if (self->new_speed) {
1003 "%s(), Changing speed!\n", __FUNCTION__ );
1004 w83977af_change_speed(self, self->new_speed);
1005 self->new_speed = 0;
1008 /* Turn around and get ready to receive some data */
1009 self->io.direction = IO_RECV;
1010 new_icr |= ICR_ERBRI;
1013 /* Rx FIFO threshold or timeout */
1014 if (isr & ISR_RXTH_I) {
1015 w83977af_pio_receive(self);
1017 /* Keep receiving */
1018 new_icr |= ICR_ERBRI;
1024 * Function pc87108_fir_interrupt (self, eir)
1026 * Handle MIR/FIR interrupt
1029 static __u8 w83977af_fir_interrupt(struct w83977af_ir *self, int isr)
1035 iobase = self->io.fir_base;
1036 set = inb(iobase+SSR);
1038 /* End of frame detected in FIFO */
1039 if (isr & (ISR_FEND_I|ISR_FSF_I)) {
1040 if (w83977af_dma_receive_complete(self)) {
1042 /* Wait for next status FIFO interrupt */
1043 new_icr |= ICR_EFSFI;
1045 /* DMA not finished yet */
1047 /* Set timer value, resolution 1 ms */
1048 switch_bank(iobase, SET4);
1049 outb(0x01, iobase+TMRL); /* 1 ms */
1050 outb(0x00, iobase+TMRH);
1053 outb(IR_MSL_EN_TMR, iobase+IR_MSL);
1055 new_icr |= ICR_ETMRI;
1058 /* Timer finished */
1059 if (isr & ISR_TMR_I) {
1061 switch_bank(iobase, SET4);
1062 outb(0, iobase+IR_MSL);
1064 /* Clear timer event */
1065 /* switch_bank(iobase, SET0); */
1066 /* outb(ASCR_CTE, iobase+ASCR); */
1068 /* Check if this is a TX timer interrupt */
1069 if (self->io.direction == IO_XMIT) {
1070 w83977af_dma_write(self, iobase);
1072 new_icr |= ICR_EDMAI;
1074 /* Check if DMA has now finished */
1075 w83977af_dma_receive_complete(self);
1077 new_icr |= ICR_EFSFI;
1080 /* Finished with DMA */
1081 if (isr & ISR_DMA_I) {
1082 w83977af_dma_xmit_complete(self);
1084 /* Check if there are more frames to be transmitted */
1085 /* if (irda_device_txqueue_empty(self)) { */
1087 /* Prepare for receive
1089 * ** Netwinder Tx DMA likes that we do this anyway **
1091 w83977af_dma_receive(self);
1092 new_icr = ICR_EFSFI;
1097 outb(set, iobase+SSR);
1103 * Function w83977af_interrupt (irq, dev_id, regs)
1105 * An interrupt from the chip has arrived. Time to do some work
1108 static irqreturn_t w83977af_interrupt(int irq, void *dev_id,
1109 struct pt_regs *regs)
1111 struct net_device *dev = (struct net_device *) dev_id;
1112 struct w83977af_ir *self;
1117 printk(KERN_WARNING "%s: irq %d for unknown device.\n",
1121 self = (struct w83977af_ir *) dev->priv;
1123 iobase = self->io.fir_base;
1125 /* Save current bank */
1126 set = inb(iobase+SSR);
1127 switch_bank(iobase, SET0);
1129 icr = inb(iobase+ICR);
1130 isr = inb(iobase+ISR) & icr; /* Mask out the interesting ones */
1132 outb(0, iobase+ICR); /* Disable interrupts */
1135 /* Dispatch interrupt handler for the current speed */
1136 if (self->io.speed > PIO_MAX_SPEED )
1137 icr = w83977af_fir_interrupt(self, isr);
1139 icr = w83977af_sir_interrupt(self, isr);
1142 outb(icr, iobase+ICR); /* Restore (new) interrupts */
1143 outb(set, iobase+SSR); /* Restore bank register */
1144 return IRQ_RETVAL(isr);
1148 * Function w83977af_is_receiving (self)
1150 * Return TRUE is we are currently receiving a frame
1153 static int w83977af_is_receiving(struct w83977af_ir *self)
1159 ASSERT(self != NULL, return FALSE;);
1161 if (self->io.speed > 115200) {
1162 iobase = self->io.fir_base;
1164 /* Check if rx FIFO is not empty */
1165 set = inb(iobase+SSR);
1166 switch_bank(iobase, SET2);
1167 if ((inb(iobase+RXFDTH) & 0x3f) != 0) {
1168 /* We are receiving something */
1171 outb(set, iobase+SSR);
1173 status = (self->rx_buff.state != OUTSIDE_FRAME);
1179 * Function w83977af_net_open (dev)
1184 static int w83977af_net_open(struct net_device *dev)
1186 struct w83977af_ir *self;
1191 IRDA_DEBUG(0, "%s()\n", __FUNCTION__ );
1193 ASSERT(dev != NULL, return -1;);
1194 self = (struct w83977af_ir *) dev->priv;
1196 ASSERT(self != NULL, return 0;);
1198 iobase = self->io.fir_base;
1200 if (request_irq(self->io.irq, w83977af_interrupt, 0, dev->name,
1205 * Always allocate the DMA channel after the IRQ,
1206 * and clean up on failure.
1208 if (request_dma(self->io.dma, dev->name)) {
1209 free_irq(self->io.irq, self);
1213 /* Save current set */
1214 set = inb(iobase+SSR);
1216 /* Enable some interrupts so we can receive frames again */
1217 switch_bank(iobase, SET0);
1218 if (self->io.speed > 115200) {
1219 outb(ICR_EFSFI, iobase+ICR);
1220 w83977af_dma_receive(self);
1222 outb(ICR_ERBRI, iobase+ICR);
1224 /* Restore bank register */
1225 outb(set, iobase+SSR);
1227 /* Ready to play! */
1228 netif_start_queue(dev);
1230 /* Give self a hardware name */
1231 sprintf(hwname, "w83977af @ 0x%03x", self->io.fir_base);
1234 * Open new IrLAP layer instance, now that everything should be
1235 * initialized properly
1237 self->irlap = irlap_open(dev, &self->qos, hwname);
1243 * Function w83977af_net_close (dev)
1248 static int w83977af_net_close(struct net_device *dev)
1250 struct w83977af_ir *self;
1254 IRDA_DEBUG(0, "%s()\n", __FUNCTION__ );
1256 ASSERT(dev != NULL, return -1;);
1258 self = (struct w83977af_ir *) dev->priv;
1260 ASSERT(self != NULL, return 0;);
1262 iobase = self->io.fir_base;
1265 netif_stop_queue(dev);
1267 /* Stop and remove instance of IrLAP */
1269 irlap_close(self->irlap);
1272 disable_dma(self->io.dma);
1274 /* Save current set */
1275 set = inb(iobase+SSR);
1277 /* Disable interrupts */
1278 switch_bank(iobase, SET0);
1279 outb(0, iobase+ICR);
1281 free_irq(self->io.irq, dev);
1282 free_dma(self->io.dma);
1284 /* Restore bank register */
1285 outb(set, iobase+SSR);
1291 * Function w83977af_net_ioctl (dev, rq, cmd)
1293 * Process IOCTL commands for this device
1296 static int w83977af_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1298 struct if_irda_req *irq = (struct if_irda_req *) rq;
1299 struct w83977af_ir *self;
1300 unsigned long flags;
1303 ASSERT(dev != NULL, return -1;);
1307 ASSERT(self != NULL, return -1;);
1309 IRDA_DEBUG(2, "%s(), %s, (cmd=0x%X)\n", __FUNCTION__ , dev->name, cmd);
1311 spin_lock_irqsave(&self->lock, flags);
1314 case SIOCSBANDWIDTH: /* Set bandwidth */
1315 if (!capable(CAP_NET_ADMIN)) {
1319 w83977af_change_speed(self, irq->ifr_baudrate);
1321 case SIOCSMEDIABUSY: /* Set media busy */
1322 if (!capable(CAP_NET_ADMIN)) {
1326 irda_device_set_media_busy(self->netdev, TRUE);
1328 case SIOCGRECEIVING: /* Check if we are receiving right now */
1329 irq->ifr_receiving = w83977af_is_receiving(self);
1335 spin_unlock_irqrestore(&self->lock, flags);
1339 static struct net_device_stats *w83977af_net_get_stats(struct net_device *dev)
1341 struct w83977af_ir *self = (struct w83977af_ir *) dev->priv;
1343 return &self->stats;
1346 MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
1347 MODULE_DESCRIPTION("Winbond W83977AF IrDA Device Driver");
1348 MODULE_LICENSE("GPL");
1351 MODULE_PARM(qos_mtt_bits, "i");
1352 MODULE_PARM_DESC(qos_mtt_bits, "Mimimum Turn Time");
1353 MODULE_PARM(io, "1-4i");
1354 MODULE_PARM_DESC(io, "Base I/O addresses");
1355 MODULE_PARM(irq, "1-4i");
1356 MODULE_PARM_DESC(irq, "IRQ lines");
1359 * Function init_module (void)
1364 module_init(w83977af_init);
1367 * Function cleanup_module (void)
1372 module_exit(w83977af_cleanup);