1 /*********************************************************************
5 * Description: Driver for the ALI M1535D and M1543C FIR Controller
6 * Status: Experimental.
7 * Author: Benjamin Kong <benjamin_kong@ali.com.tw>
8 * Created at: 2000/10/16 03:46PM
9 * Modified at: 2001/1/3 02:55PM
10 * Modified by: Benjamin Kong <benjamin_kong@ali.com.tw>
11 * Modified at: 2003/11/6 and support for ALi south-bridge chipsets M1563
12 * Modified by: Clear Zhang <clear_zhang@ali.com.tw>
14 * Copyright (c) 2000 Benjamin Kong <benjamin_kong@ali.com.tw>
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 ********************************************************************/
24 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/types.h>
28 #include <linux/skbuff.h>
29 #include <linux/netdevice.h>
30 #include <linux/ioport.h>
31 #include <linux/delay.h>
32 #include <linux/slab.h>
33 #include <linux/init.h>
34 #include <linux/rtnetlink.h>
35 #include <linux/serial_reg.h>
36 #include <linux/dma-mapping.h>
40 #include <asm/byteorder.h>
44 #include <net/irda/wrapper.h>
45 #include <net/irda/irda.h>
46 #include <net/irda/irda_device.h>
50 #define CHIP_IO_EXTENT 8
51 #define BROKEN_DONGLE_ID
53 static char *driver_name = "ali-ircc";
55 /* Module parameters */
56 static int qos_mtt_bits = 0x07; /* 1 ms or more */
58 /* Use BIOS settions by default, but user may supply module parameters */
59 static unsigned int io[] = { ~0, ~0, ~0, ~0 };
60 static unsigned int irq[] = { 0, 0, 0, 0 };
61 static unsigned int dma[] = { 0, 0, 0, 0 };
63 static int ali_ircc_probe_53(ali_chip_t *chip, chipio_t *info);
64 static int ali_ircc_init_43(ali_chip_t *chip, chipio_t *info);
65 static int ali_ircc_init_53(ali_chip_t *chip, chipio_t *info);
67 /* These are the currently known ALi sourth-bridge chipsets, the only one difference
68 * is that M1543C doesn't support HP HDSL-3600
70 static ali_chip_t chips[] =
72 { "M1543", { 0x3f0, 0x370 }, 0x51, 0x23, 0x20, 0x43, ali_ircc_probe_53, ali_ircc_init_43 },
73 { "M1535", { 0x3f0, 0x370 }, 0x51, 0x23, 0x20, 0x53, ali_ircc_probe_53, ali_ircc_init_53 },
74 { "M1563", { 0x3f0, 0x370 }, 0x51, 0x23, 0x20, 0x63, ali_ircc_probe_53, ali_ircc_init_53 },
78 /* Max 4 instances for now */
79 static struct ali_ircc_cb *dev_self[] = { NULL, NULL, NULL, NULL };
82 static char *dongle_types[] = {
86 "No dongle connected",
90 static int ali_ircc_open(int i, chipio_t *info);
92 static int ali_ircc_close(struct ali_ircc_cb *self);
94 static int ali_ircc_setup(chipio_t *info);
95 static int ali_ircc_is_receiving(struct ali_ircc_cb *self);
96 static int ali_ircc_net_open(struct net_device *dev);
97 static int ali_ircc_net_close(struct net_device *dev);
98 static int ali_ircc_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
99 static int ali_ircc_pmproc(struct pm_dev *dev, pm_request_t rqst, void *data);
100 static void ali_ircc_change_speed(struct ali_ircc_cb *self, __u32 baud);
101 static void ali_ircc_suspend(struct ali_ircc_cb *self);
102 static void ali_ircc_wakeup(struct ali_ircc_cb *self);
103 static struct net_device_stats *ali_ircc_net_get_stats(struct net_device *dev);
106 static int ali_ircc_sir_hard_xmit(struct sk_buff *skb, struct net_device *dev);
107 static irqreturn_t ali_ircc_sir_interrupt(struct ali_ircc_cb *self);
108 static void ali_ircc_sir_receive(struct ali_ircc_cb *self);
109 static void ali_ircc_sir_write_wakeup(struct ali_ircc_cb *self);
110 static int ali_ircc_sir_write(int iobase, int fifo_size, __u8 *buf, int len);
111 static void ali_ircc_sir_change_speed(struct ali_ircc_cb *priv, __u32 speed);
114 static int ali_ircc_fir_hard_xmit(struct sk_buff *skb, struct net_device *dev);
115 static void ali_ircc_fir_change_speed(struct ali_ircc_cb *priv, __u32 speed);
116 static irqreturn_t ali_ircc_fir_interrupt(struct ali_ircc_cb *self);
117 static int ali_ircc_dma_receive(struct ali_ircc_cb *self);
118 static int ali_ircc_dma_receive_complete(struct ali_ircc_cb *self);
119 static int ali_ircc_dma_xmit_complete(struct ali_ircc_cb *self);
120 static void ali_ircc_dma_xmit(struct ali_ircc_cb *self);
123 static int ali_ircc_read_dongle_id (int i, chipio_t *info);
124 static void ali_ircc_change_dongle_speed(struct ali_ircc_cb *priv, int speed);
126 /* ALi chip function */
127 static void SIR2FIR(int iobase);
128 static void FIR2SIR(int iobase);
129 static void SetCOMInterrupts(struct ali_ircc_cb *self , unsigned char enable);
132 * Function ali_ircc_init ()
134 * Initialize chip. Find out whay kinds of chips we are dealing with
135 * and their configuation registers address
137 static int __init ali_ircc_init(void)
146 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__);
148 /* Probe for all the ALi chipsets we know about */
149 for (chip= chips; chip->name; chip++, i++)
151 IRDA_DEBUG(2, "%s(), Probing for %s ...\n", __FUNCTION__, chip->name);
153 /* Try all config registers for this chip */
154 for (cfg=0; cfg<2; cfg++)
156 cfg_base = chip->cfg[cfg];
160 memset(&info, 0, sizeof(chipio_t));
161 info.cfg_base = cfg_base;
162 info.fir_base = io[i];
167 /* Enter Configuration */
168 outb(chip->entr1, cfg_base);
169 outb(chip->entr2, cfg_base);
171 /* Select Logical Device 5 Registers (UART2) */
172 outb(0x07, cfg_base);
173 outb(0x05, cfg_base+1);
175 /* Read Chip Identification Register */
176 outb(chip->cid_index, cfg_base);
177 reg = inb(cfg_base+1);
179 if (reg == chip->cid_value)
181 IRDA_DEBUG(2, "%s(), Chip found at 0x%03x\n", __FUNCTION__, cfg_base);
183 outb(0x1F, cfg_base);
184 revision = inb(cfg_base+1);
185 IRDA_DEBUG(2, "%s(), Found %s chip, revision=%d\n", __FUNCTION__,
186 chip->name, revision);
189 * If the user supplies the base address, then
190 * we init the chip, if not we probe the values
195 chip->init(chip, &info);
199 chip->probe(chip, &info);
202 if (ali_ircc_open(i, &info) == 0)
208 IRDA_DEBUG(2, "%s(), No %s chip at 0x%03x\n", __FUNCTION__, chip->name, cfg_base);
210 /* Exit configuration */
211 outb(0xbb, cfg_base);
215 IRDA_DEBUG(2, "%s(), ----------------- End -----------------\n", __FUNCTION__);
220 * Function ali_ircc_cleanup ()
222 * Close all configured chips
225 static void __exit ali_ircc_cleanup(void)
229 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__);
231 pm_unregister_all(ali_ircc_pmproc);
233 for (i=0; i < 4; i++) {
235 ali_ircc_close(dev_self[i]);
238 IRDA_DEBUG(2, "%s(), ----------------- End -----------------\n", __FUNCTION__);
242 * Function ali_ircc_open (int i, chipio_t *inf)
244 * Open driver instance
247 static int ali_ircc_open(int i, chipio_t *info)
249 struct net_device *dev;
250 struct ali_ircc_cb *self;
251 struct pm_dev *pmdev;
255 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__);
257 /* Set FIR FIFO and DMA Threshold */
258 if ((ali_ircc_setup(info)) == -1)
261 dev = alloc_irdadev(sizeof(*self));
263 IRDA_ERROR("%s(), can't allocate memory for control block!\n",
270 spin_lock_init(&self->lock);
272 /* Need to store self somewhere */
277 self->io.cfg_base = info->cfg_base; /* In ali_ircc_probe_53 assign */
278 self->io.fir_base = info->fir_base; /* info->sir_base = info->fir_base */
279 self->io.sir_base = info->sir_base; /* ALi SIR and FIR use the same address */
280 self->io.irq = info->irq;
281 self->io.fir_ext = CHIP_IO_EXTENT;
282 self->io.dma = info->dma;
283 self->io.fifo_size = 16; /* SIR: 16, FIR: 32 Benjamin 2000/11/1 */
285 /* Reserve the ioports that we need */
286 if (!request_region(self->io.fir_base, self->io.fir_ext, driver_name)) {
287 IRDA_WARNING("%s(), can't get iobase of 0x%03x\n", __FUNCTION__,
293 /* Initialize QoS for this device */
294 irda_init_max_qos_capabilies(&self->qos);
296 /* The only value we must override it the baudrate */
297 self->qos.baud_rate.bits = IR_9600|IR_19200|IR_38400|IR_57600|
298 IR_115200|IR_576000|IR_1152000|(IR_4000000 << 8); // benjamin 2000/11/8 05:27PM
300 self->qos.min_turn_time.bits = qos_mtt_bits;
302 irda_qos_bits_to_value(&self->qos);
304 /* Max DMA buffer size needed = (data_size + 6) * (window_size) + 6; */
305 self->rx_buff.truesize = 14384;
306 self->tx_buff.truesize = 14384;
308 /* Allocate memory if needed */
310 dma_alloc_coherent(NULL, self->rx_buff.truesize,
311 &self->rx_buff_dma, GFP_KERNEL);
312 if (self->rx_buff.head == NULL) {
316 memset(self->rx_buff.head, 0, self->rx_buff.truesize);
319 dma_alloc_coherent(NULL, self->tx_buff.truesize,
320 &self->tx_buff_dma, GFP_KERNEL);
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;
337 /* Keep track of module usage */
338 SET_MODULE_OWNER(dev);
340 /* Override the network functions we need to use */
341 dev->hard_start_xmit = ali_ircc_sir_hard_xmit;
342 dev->open = ali_ircc_net_open;
343 dev->stop = ali_ircc_net_close;
344 dev->do_ioctl = ali_ircc_net_ioctl;
345 dev->get_stats = ali_ircc_net_get_stats;
347 err = register_netdev(dev);
349 IRDA_ERROR("%s(), register_netdev() failed!\n", __FUNCTION__);
352 IRDA_MESSAGE("IrDA: Registered device %s\n", dev->name);
354 /* Check dongle id */
355 dongle_id = ali_ircc_read_dongle_id(i, info);
356 IRDA_MESSAGE("%s(), %s, Found dongle: %s\n", __FUNCTION__, driver_name, dongle_types[dongle_id]);
358 self->io.dongle_id = dongle_id;
360 pmdev = pm_register(PM_SYS_DEV, PM_SYS_IRDA, ali_ircc_pmproc);
364 IRDA_DEBUG(2, "%s(), ----------------- End -----------------\n", __FUNCTION__);
369 dma_free_coherent(NULL, self->tx_buff.truesize,
370 self->tx_buff.head, self->tx_buff_dma);
372 dma_free_coherent(NULL, self->rx_buff.truesize,
373 self->rx_buff.head, self->rx_buff_dma);
375 release_region(self->io.fir_base, self->io.fir_ext);
384 * Function ali_ircc_close (self)
386 * Close driver instance
389 static int __exit ali_ircc_close(struct ali_ircc_cb *self)
393 IRDA_DEBUG(4, "%s(), ---------------- Start ----------------\n", __FUNCTION__);
395 IRDA_ASSERT(self != NULL, return -1;);
397 iobase = self->io.fir_base;
399 /* Remove netdevice */
400 unregister_netdev(self->netdev);
402 /* Release the PORT that this driver is using */
403 IRDA_DEBUG(4, "%s(), Releasing Region %03x\n", __FUNCTION__, self->io.fir_base);
404 release_region(self->io.fir_base, self->io.fir_ext);
406 if (self->tx_buff.head)
407 dma_free_coherent(NULL, self->tx_buff.truesize,
408 self->tx_buff.head, self->tx_buff_dma);
410 if (self->rx_buff.head)
411 dma_free_coherent(NULL, self->rx_buff.truesize,
412 self->rx_buff.head, self->rx_buff_dma);
414 dev_self[self->index] = NULL;
415 free_netdev(self->netdev);
417 IRDA_DEBUG(2, "%s(), ----------------- End -----------------\n", __FUNCTION__);
423 * Function ali_ircc_init_43 (chip, info)
425 * Initialize the ALi M1543 chip.
427 static int ali_ircc_init_43(ali_chip_t *chip, chipio_t *info)
429 /* All controller information like I/O address, DMA channel, IRQ
437 * Function ali_ircc_init_53 (chip, info)
439 * Initialize the ALi M1535 chip.
441 static int ali_ircc_init_53(ali_chip_t *chip, chipio_t *info)
443 /* All controller information like I/O address, DMA channel, IRQ
451 * Function ali_ircc_probe_53 (chip, info)
453 * Probes for the ALi M1535D or M1535
455 static int ali_ircc_probe_53(ali_chip_t *chip, chipio_t *info)
457 int cfg_base = info->cfg_base;
460 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__);
462 /* Enter Configuration */
463 outb(chip->entr1, cfg_base);
464 outb(chip->entr2, cfg_base);
466 /* Select Logical Device 5 Registers (UART2) */
467 outb(0x07, cfg_base);
468 outb(0x05, cfg_base+1);
470 /* Read address control register */
471 outb(0x60, cfg_base);
472 hi = inb(cfg_base+1);
473 outb(0x61, cfg_base);
474 low = inb(cfg_base+1);
475 info->fir_base = (hi<<8) + low;
477 info->sir_base = info->fir_base;
479 IRDA_DEBUG(2, "%s(), probing fir_base=0x%03x\n", __FUNCTION__, info->fir_base);
481 /* Read IRQ control register */
482 outb(0x70, cfg_base);
483 reg = inb(cfg_base+1);
484 info->irq = reg & 0x0f;
485 IRDA_DEBUG(2, "%s(), probing irq=%d\n", __FUNCTION__, info->irq);
487 /* Read DMA channel */
488 outb(0x74, cfg_base);
489 reg = inb(cfg_base+1);
490 info->dma = reg & 0x07;
492 if(info->dma == 0x04)
493 IRDA_WARNING("%s(), No DMA channel assigned !\n", __FUNCTION__);
495 IRDA_DEBUG(2, "%s(), probing dma=%d\n", __FUNCTION__, info->dma);
497 /* Read Enabled Status */
498 outb(0x30, cfg_base);
499 reg = inb(cfg_base+1);
500 info->enabled = (reg & 0x80) && (reg & 0x01);
501 IRDA_DEBUG(2, "%s(), probing enabled=%d\n", __FUNCTION__, info->enabled);
503 /* Read Power Status */
504 outb(0x22, cfg_base);
505 reg = inb(cfg_base+1);
506 info->suspended = (reg & 0x20);
507 IRDA_DEBUG(2, "%s(), probing suspended=%d\n", __FUNCTION__, info->suspended);
509 /* Exit configuration */
510 outb(0xbb, cfg_base);
512 IRDA_DEBUG(2, "%s(), ----------------- End -----------------\n", __FUNCTION__);
518 * Function ali_ircc_setup (info)
520 * Set FIR FIFO and DMA Threshold
521 * Returns non-negative on success.
524 static int ali_ircc_setup(chipio_t *info)
528 int iobase = info->fir_base;
530 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__);
532 /* Locking comments :
533 * Most operations here need to be protected. We are called before
534 * the device instance is created in ali_ircc_open(), therefore
535 * nobody can bother us - Jean II */
537 /* Switch to FIR space */
541 outb(0x40, iobase+FIR_MCR); // benjamin 2000/11/30 11:45AM
543 /* Read FIR ID Version Register */
544 switch_bank(iobase, BANK3);
545 version = inb(iobase+FIR_ID_VR);
547 /* Should be 0x00 in the M1535/M1535D */
550 IRDA_ERROR("%s, Wrong chip version %02x\n", driver_name, version);
554 // IRDA_MESSAGE("%s, Found chip at base=0x%03x\n", driver_name, info->cfg_base);
556 /* Set FIR FIFO Threshold Register */
557 switch_bank(iobase, BANK1);
558 outb(RX_FIFO_Threshold, iobase+FIR_FIFO_TR);
560 /* Set FIR DMA Threshold Register */
561 outb(RX_DMA_Threshold, iobase+FIR_DMA_TR);
564 switch_bank(iobase, BANK2);
565 outb(inb(iobase+FIR_IRDA_CR) | IRDA_CR_CRC, iobase+FIR_IRDA_CR);
567 /* NDIS driver set TX Length here BANK2 Alias 3, Alias4*/
569 /* Switch to Bank 0 */
570 switch_bank(iobase, BANK0);
572 tmp = inb(iobase+FIR_LCR_B);
573 tmp &=~0x20; // disable SIP
574 tmp |= 0x80; // these two steps make RX mode
576 outb(tmp, iobase+FIR_LCR_B);
578 /* Disable Interrupt */
579 outb(0x00, iobase+FIR_IER);
582 /* Switch to SIR space */
585 IRDA_MESSAGE("%s, driver loaded (Benjamin Kong)\n", driver_name);
587 /* Enable receive interrupts */
588 // outb(UART_IER_RDI, iobase+UART_IER); //benjamin 2000/11/23 01:25PM
589 // Turn on the interrupts in ali_ircc_net_open
591 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__);
597 * Function ali_ircc_read_dongle_id (int index, info)
599 * Try to read dongle indentification. This procedure needs to be executed
600 * once after power-on/reset. It also needs to be used whenever you suspect
601 * that the user may have plugged/unplugged the IrDA Dongle.
603 static int ali_ircc_read_dongle_id (int i, chipio_t *info)
606 int cfg_base = info->cfg_base;
608 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__);
610 /* Enter Configuration */
611 outb(chips[i].entr1, cfg_base);
612 outb(chips[i].entr2, cfg_base);
614 /* Select Logical Device 5 Registers (UART2) */
615 outb(0x07, cfg_base);
616 outb(0x05, cfg_base+1);
619 outb(0xf0, cfg_base);
620 reg = inb(cfg_base+1);
621 dongle_id = ((reg>>6)&0x02) | ((reg>>5)&0x01);
622 IRDA_DEBUG(2, "%s(), probing dongle_id=%d, dongle_types=%s\n", __FUNCTION__,
623 dongle_id, dongle_types[dongle_id]);
625 /* Exit configuration */
626 outb(0xbb, cfg_base);
628 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__);
634 * Function ali_ircc_interrupt (irq, dev_id, regs)
636 * An interrupt from the chip has arrived. Time to do some work
639 static irqreturn_t ali_ircc_interrupt(int irq, void *dev_id,
640 struct pt_regs *regs)
642 struct net_device *dev = (struct net_device *) dev_id;
643 struct ali_ircc_cb *self;
646 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__);
649 IRDA_WARNING("%s: irq %d for unknown device.\n", driver_name, irq);
653 self = (struct ali_ircc_cb *) dev->priv;
655 spin_lock(&self->lock);
657 /* Dispatch interrupt handler for the current speed */
658 if (self->io.speed > 115200)
659 ret = ali_ircc_fir_interrupt(self);
661 ret = ali_ircc_sir_interrupt(self);
663 spin_unlock(&self->lock);
665 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__);
669 * Function ali_ircc_fir_interrupt(irq, struct ali_ircc_cb *self)
671 * Handle MIR/FIR interrupt
674 static irqreturn_t ali_ircc_fir_interrupt(struct ali_ircc_cb *self)
676 __u8 eir, OldMessageCount;
679 IRDA_DEBUG(1, "%s(), ---------------- Start ----------------\n", __FUNCTION__);
681 iobase = self->io.fir_base;
683 switch_bank(iobase, BANK0);
684 self->InterruptID = inb(iobase+FIR_IIR);
685 self->BusStatus = inb(iobase+FIR_BSR);
687 OldMessageCount = (self->LineStatus + 1) & 0x07;
688 self->LineStatus = inb(iobase+FIR_LSR);
689 //self->ier = inb(iobase+FIR_IER); 2000/12/1 04:32PM
690 eir = self->InterruptID & self->ier; /* Mask out the interesting ones */
692 IRDA_DEBUG(1, "%s(), self->InterruptID = %x\n", __FUNCTION__,self->InterruptID);
693 IRDA_DEBUG(1, "%s(), self->LineStatus = %x\n", __FUNCTION__,self->LineStatus);
694 IRDA_DEBUG(1, "%s(), self->ier = %x\n", __FUNCTION__,self->ier);
695 IRDA_DEBUG(1, "%s(), eir = %x\n", __FUNCTION__,eir);
697 /* Disable interrupts */
698 SetCOMInterrupts(self, FALSE);
700 /* Tx or Rx Interrupt */
704 if (self->io.direction == IO_XMIT) /* TX */
706 IRDA_DEBUG(1, "%s(), ******* IIR_EOM (Tx) *******\n", __FUNCTION__);
708 if(ali_ircc_dma_xmit_complete(self))
710 if (irda_device_txqueue_empty(self->netdev))
712 /* Prepare for receive */
713 ali_ircc_dma_receive(self);
725 IRDA_DEBUG(1, "%s(), ******* IIR_EOM (Rx) *******\n", __FUNCTION__);
727 if(OldMessageCount > ((self->LineStatus+1) & 0x07))
729 self->rcvFramesOverflow = TRUE;
730 IRDA_DEBUG(1, "%s(), ******* self->rcvFramesOverflow = TRUE ******** \n", __FUNCTION__);
733 if (ali_ircc_dma_receive_complete(self))
735 IRDA_DEBUG(1, "%s(), ******* receive complete ******** \n", __FUNCTION__);
741 IRDA_DEBUG(1, "%s(), ******* Not receive complete ******** \n", __FUNCTION__);
743 self->ier = IER_EOM | IER_TIMER;
748 /* Timer Interrupt */
749 else if (eir & IIR_TIMER)
751 if(OldMessageCount > ((self->LineStatus+1) & 0x07))
753 self->rcvFramesOverflow = TRUE;
754 IRDA_DEBUG(1, "%s(), ******* self->rcvFramesOverflow = TRUE ******* \n", __FUNCTION__);
757 switch_bank(iobase, BANK1);
758 tmp = inb(iobase+FIR_CR);
759 outb( tmp& ~CR_TIMER_EN, iobase+FIR_CR);
761 /* Check if this is a Tx timer interrupt */
762 if (self->io.direction == IO_XMIT)
764 ali_ircc_dma_xmit(self);
766 /* Interrupt on EOM */
772 if(ali_ircc_dma_receive_complete(self))
778 self->ier = IER_EOM | IER_TIMER;
783 /* Restore Interrupt */
784 SetCOMInterrupts(self, TRUE);
786 IRDA_DEBUG(1, "%s(), ----------------- End ---------------\n", __FUNCTION__);
787 return IRQ_RETVAL(eir);
791 * Function ali_ircc_sir_interrupt (irq, self, eir)
793 * Handle SIR interrupt
796 static irqreturn_t ali_ircc_sir_interrupt(struct ali_ircc_cb *self)
801 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__);
803 iobase = self->io.sir_base;
805 iir = inb(iobase+UART_IIR) & UART_IIR_ID;
807 /* Clear interrupt */
808 lsr = inb(iobase+UART_LSR);
810 IRDA_DEBUG(4, "%s(), iir=%02x, lsr=%02x, iobase=%#x\n", __FUNCTION__,
816 IRDA_DEBUG(2, "%s(), RLSI\n", __FUNCTION__);
819 /* Receive interrupt */
820 ali_ircc_sir_receive(self);
823 if (lsr & UART_LSR_THRE)
825 /* Transmitter ready for data */
826 ali_ircc_sir_write_wakeup(self);
830 IRDA_DEBUG(0, "%s(), unhandled IIR=%#x\n", __FUNCTION__, iir);
837 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__);
839 return IRQ_RETVAL(iir);
844 * Function ali_ircc_sir_receive (self)
846 * Receive one frame from the infrared port
849 static void ali_ircc_sir_receive(struct ali_ircc_cb *self)
854 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__);
855 IRDA_ASSERT(self != NULL, return;);
857 iobase = self->io.sir_base;
860 * Receive all characters in Rx FIFO, unwrap and unstuff them.
861 * async_unwrap_char will deliver all found frames
864 async_unwrap_char(self->netdev, &self->stats, &self->rx_buff,
865 inb(iobase+UART_RX));
867 /* Make sure we don't stay here too long */
868 if (boguscount++ > 32) {
869 IRDA_DEBUG(2,"%s(), breaking!\n", __FUNCTION__);
872 } while (inb(iobase+UART_LSR) & UART_LSR_DR);
874 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
878 * Function ali_ircc_sir_write_wakeup (tty)
880 * Called by the driver when there's room for more data. If we have
881 * more packets to send, we send them here.
884 static void ali_ircc_sir_write_wakeup(struct ali_ircc_cb *self)
889 IRDA_ASSERT(self != NULL, return;);
891 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
893 iobase = self->io.sir_base;
895 /* Finished with frame? */
896 if (self->tx_buff.len > 0)
898 /* Write data left in transmit buffer */
899 actual = ali_ircc_sir_write(iobase, self->io.fifo_size,
900 self->tx_buff.data, self->tx_buff.len);
901 self->tx_buff.data += actual;
902 self->tx_buff.len -= actual;
908 /* We must wait until all data are gone */
909 while(!(inb(iobase+UART_LSR) & UART_LSR_TEMT))
910 IRDA_DEBUG(1, "%s(), UART_LSR_THRE\n", __FUNCTION__ );
912 IRDA_DEBUG(1, "%s(), Changing speed! self->new_speed = %d\n", __FUNCTION__ , self->new_speed);
913 ali_ircc_change_speed(self, self->new_speed);
916 // benjamin 2000/11/10 06:32PM
917 if (self->io.speed > 115200)
919 IRDA_DEBUG(2, "%s(), ali_ircc_change_speed from UART_LSR_TEMT \n", __FUNCTION__ );
922 // SetCOMInterrupts(self, TRUE);
928 netif_wake_queue(self->netdev);
931 self->stats.tx_packets++;
933 /* Turn on receive interrupts */
934 outb(UART_IER_RDI, iobase+UART_IER);
937 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
940 static void ali_ircc_change_speed(struct ali_ircc_cb *self, __u32 baud)
942 struct net_device *dev = self->netdev;
945 IRDA_DEBUG(1, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
947 IRDA_DEBUG(2, "%s(), setting speed = %d \n", __FUNCTION__ , baud);
949 /* This function *must* be called with irq off and spin-lock.
952 iobase = self->io.fir_base;
954 SetCOMInterrupts(self, FALSE); // 2000/11/24 11:43AM
956 /* Go to MIR, FIR Speed */
961 ali_ircc_fir_change_speed(self, baud);
963 /* Install FIR xmit handler*/
964 dev->hard_start_xmit = ali_ircc_fir_hard_xmit;
966 /* Enable Interuupt */
967 self->ier = IER_EOM; // benjamin 2000/11/20 07:24PM
969 /* Be ready for incomming frames */
970 ali_ircc_dma_receive(self); // benajmin 2000/11/8 07:46PM not complete
972 /* Go to SIR Speed */
975 ali_ircc_sir_change_speed(self, baud);
977 /* Install SIR xmit handler*/
978 dev->hard_start_xmit = ali_ircc_sir_hard_xmit;
982 SetCOMInterrupts(self, TRUE); // 2000/11/24 11:43AM
984 netif_wake_queue(self->netdev);
986 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
989 static void ali_ircc_fir_change_speed(struct ali_ircc_cb *priv, __u32 baud)
993 struct ali_ircc_cb *self = (struct ali_ircc_cb *) priv;
994 struct net_device *dev;
996 IRDA_DEBUG(1, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
998 IRDA_ASSERT(self != NULL, return;);
1001 iobase = self->io.fir_base;
1003 IRDA_DEBUG(1, "%s(), self->io.speed = %d, change to speed = %d\n", __FUNCTION__ ,self->io.speed,baud);
1005 /* Come from SIR speed */
1006 if(self->io.speed <=115200)
1011 /* Update accounting for new speed */
1012 self->io.speed = baud;
1014 // Set Dongle Speed mode
1015 ali_ircc_change_dongle_speed(self, baud);
1017 IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
1021 * Function ali_sir_change_speed (self, speed)
1023 * Set speed of IrDA port to specified baudrate
1026 static void ali_ircc_sir_change_speed(struct ali_ircc_cb *priv, __u32 speed)
1028 struct ali_ircc_cb *self = (struct ali_ircc_cb *) priv;
1029 unsigned long flags;
1031 int fcr; /* FIFO control reg */
1032 int lcr; /* Line control reg */
1035 IRDA_DEBUG(1, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
1037 IRDA_DEBUG(1, "%s(), Setting speed to: %d\n", __FUNCTION__ , speed);
1039 IRDA_ASSERT(self != NULL, return;);
1041 iobase = self->io.sir_base;
1043 /* Come from MIR or FIR speed */
1044 if(self->io.speed >115200)
1046 // Set Dongle Speed mode first
1047 ali_ircc_change_dongle_speed(self, speed);
1052 // Clear Line and Auxiluary status registers 2000/11/24 11:47AM
1054 inb(iobase+UART_LSR);
1055 inb(iobase+UART_SCR);
1057 /* Update accounting for new speed */
1058 self->io.speed = speed;
1060 spin_lock_irqsave(&self->lock, flags);
1062 divisor = 115200/speed;
1064 fcr = UART_FCR_ENABLE_FIFO;
1067 * Use trigger level 1 to avoid 3 ms. timeout delay at 9600 bps, and
1068 * almost 1,7 ms at 19200 bps. At speeds above that we can just forget
1069 * about this timeout since it will always be fast enough.
1071 if (self->io.speed < 38400)
1072 fcr |= UART_FCR_TRIGGER_1;
1074 fcr |= UART_FCR_TRIGGER_14;
1076 /* IrDA ports use 8N1 */
1077 lcr = UART_LCR_WLEN8;
1079 outb(UART_LCR_DLAB | lcr, iobase+UART_LCR); /* Set DLAB */
1080 outb(divisor & 0xff, iobase+UART_DLL); /* Set speed */
1081 outb(divisor >> 8, iobase+UART_DLM);
1082 outb(lcr, iobase+UART_LCR); /* Set 8N1 */
1083 outb(fcr, iobase+UART_FCR); /* Enable FIFO's */
1085 /* without this, the conection will be broken after come back from FIR speed,
1086 but with this, the SIR connection is harder to established */
1087 outb((UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2), iobase+UART_MCR);
1089 spin_unlock_irqrestore(&self->lock, flags);
1091 IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
1094 static void ali_ircc_change_dongle_speed(struct ali_ircc_cb *priv, int speed)
1097 struct ali_ircc_cb *self = (struct ali_ircc_cb *) priv;
1098 int iobase,dongle_id;
1101 IRDA_DEBUG(1, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
1103 iobase = self->io.fir_base; /* or iobase = self->io.sir_base; */
1104 dongle_id = self->io.dongle_id;
1106 /* We are already locked, no need to do it again */
1108 IRDA_DEBUG(1, "%s(), Set Speed for %s , Speed = %d\n", __FUNCTION__ , dongle_types[dongle_id], speed);
1110 switch_bank(iobase, BANK2);
1111 tmp = inb(iobase+FIR_IRDA_CR);
1113 /* IBM type dongle */
1116 if(speed == 4000000)
1119 // SD/MODE __| |__ __
1124 tmp &= ~IRDA_CR_HDLC; // HDLC=0
1125 tmp |= IRDA_CR_CRC; // CRC=1
1127 switch_bank(iobase, BANK2);
1128 outb(tmp, iobase+FIR_IRDA_CR);
1130 // T1 -> SD/MODE:0 IRTX:0
1133 outb(tmp, iobase+FIR_IRDA_CR);
1136 // T2 -> SD/MODE:1 IRTX:0
1139 outb(tmp, iobase+FIR_IRDA_CR);
1142 // T3 -> SD/MODE:1 IRTX:1
1144 outb(tmp, iobase+FIR_IRDA_CR);
1147 // T4 -> SD/MODE:0 IRTX:1
1150 outb(tmp, iobase+FIR_IRDA_CR);
1153 // T5 -> SD/MODE:0 IRTX:0
1156 outb(tmp, iobase+FIR_IRDA_CR);
1159 // reset -> Normal TX output Signal
1160 outb(tmp & ~0x02, iobase+FIR_IRDA_CR);
1162 else /* speed <=1152000 */
1170 /* MIR 115200, 57600 */
1173 tmp |= 0xA0; //HDLC=1, 1.152Mbps=1
1177 tmp &=~0x80; //HDLC 0.576Mbps
1178 tmp |= 0x20; //HDLC=1,
1181 tmp |= IRDA_CR_CRC; // CRC=1
1183 switch_bank(iobase, BANK2);
1184 outb(tmp, iobase+FIR_IRDA_CR);
1186 /* MIR 115200, 57600 */
1188 //switch_bank(iobase, BANK2);
1189 // T1 -> SD/MODE:0 IRTX:0
1192 outb(tmp, iobase+FIR_IRDA_CR);
1195 // T2 -> SD/MODE:1 IRTX:0
1198 outb(tmp, iobase+FIR_IRDA_CR);
1200 // T3 -> SD/MODE:0 IRTX:0
1203 outb(tmp, iobase+FIR_IRDA_CR);
1206 // reset -> Normal TX output Signal
1207 outb(tmp & ~0x02, iobase+FIR_IRDA_CR);
1210 else if (dongle_id == 1) /* HP HDSL-3600 */
1215 tmp &= ~IRDA_CR_HDLC; // HDLC=0
1219 tmp |= 0xA0; // HDLC=1, 1.152Mbps=1
1223 tmp &=~0x80; // HDLC 0.576Mbps
1224 tmp |= 0x20; // HDLC=1,
1228 tmp |= IRDA_CR_CRC; // CRC=1
1230 switch_bank(iobase, BANK2);
1231 outb(tmp, iobase+FIR_IRDA_CR);
1233 else /* HP HDSL-1100 */
1235 if(speed <= 115200) /* SIR */
1238 tmp &= ~IRDA_CR_FIR_SIN; // HP sin select = 0
1240 switch_bank(iobase, BANK2);
1241 outb(tmp, iobase+FIR_IRDA_CR);
1249 tmp &= ~IRDA_CR_HDLC; // HDLC=0
1253 tmp |= 0xA0; // HDLC=1, 1.152Mbps=1
1257 tmp &=~0x80; // HDLC 0.576Mbps
1258 tmp |= 0x20; // HDLC=1,
1262 tmp |= IRDA_CR_CRC; // CRC=1
1263 tmp |= IRDA_CR_FIR_SIN; // HP sin select = 1
1265 switch_bank(iobase, BANK2);
1266 outb(tmp, iobase+FIR_IRDA_CR);
1270 switch_bank(iobase, BANK0);
1272 IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
1276 * Function ali_ircc_sir_write (driver)
1278 * Fill Tx FIFO with transmit data
1281 static int ali_ircc_sir_write(int iobase, int fifo_size, __u8 *buf, int len)
1285 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
1287 /* Tx FIFO should be empty! */
1288 if (!(inb(iobase+UART_LSR) & UART_LSR_THRE)) {
1289 IRDA_DEBUG(0, "%s(), failed, fifo not empty!\n", __FUNCTION__ );
1293 /* Fill FIFO with current frame */
1294 while ((fifo_size-- > 0) && (actual < len)) {
1295 /* Transmit next byte */
1296 outb(buf[actual], iobase+UART_TX);
1301 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
1306 * Function ali_ircc_net_open (dev)
1311 static int ali_ircc_net_open(struct net_device *dev)
1313 struct ali_ircc_cb *self;
1317 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
1319 IRDA_ASSERT(dev != NULL, return -1;);
1321 self = (struct ali_ircc_cb *) dev->priv;
1323 IRDA_ASSERT(self != NULL, return 0;);
1325 iobase = self->io.fir_base;
1327 /* Request IRQ and install Interrupt Handler */
1328 if (request_irq(self->io.irq, ali_ircc_interrupt, 0, dev->name, dev))
1330 IRDA_WARNING("%s, unable to allocate irq=%d\n", driver_name,
1336 * Always allocate the DMA channel after the IRQ, and clean up on
1339 if (request_dma(self->io.dma, dev->name)) {
1340 IRDA_WARNING("%s, unable to allocate dma=%d\n", driver_name,
1342 free_irq(self->io.irq, self);
1346 /* Turn on interrups */
1347 outb(UART_IER_RDI , iobase+UART_IER);
1349 /* Ready to play! */
1350 netif_start_queue(dev); //benjamin by irport
1352 /* Give self a hardware name */
1353 sprintf(hwname, "ALI-FIR @ 0x%03x", self->io.fir_base);
1356 * Open new IrLAP layer instance, now that everything should be
1357 * initialized properly
1359 self->irlap = irlap_open(dev, &self->qos, hwname);
1361 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
1367 * Function ali_ircc_net_close (dev)
1372 static int ali_ircc_net_close(struct net_device *dev)
1375 struct ali_ircc_cb *self;
1378 IRDA_DEBUG(4, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
1380 IRDA_ASSERT(dev != NULL, return -1;);
1382 self = (struct ali_ircc_cb *) dev->priv;
1383 IRDA_ASSERT(self != NULL, return 0;);
1386 netif_stop_queue(dev);
1388 /* Stop and remove instance of IrLAP */
1390 irlap_close(self->irlap);
1393 disable_dma(self->io.dma);
1395 /* Disable interrupts */
1396 SetCOMInterrupts(self, FALSE);
1398 free_irq(self->io.irq, dev);
1399 free_dma(self->io.dma);
1401 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
1407 * Function ali_ircc_fir_hard_xmit (skb, dev)
1409 * Transmit the frame
1412 static int ali_ircc_fir_hard_xmit(struct sk_buff *skb, struct net_device *dev)
1414 struct ali_ircc_cb *self;
1415 unsigned long flags;
1420 IRDA_DEBUG(1, "%s(), ---------------- Start -----------------\n", __FUNCTION__ );
1422 self = (struct ali_ircc_cb *) dev->priv;
1423 iobase = self->io.fir_base;
1425 netif_stop_queue(dev);
1427 /* Make sure tests *& speed change are atomic */
1428 spin_lock_irqsave(&self->lock, flags);
1430 /* Note : you should make sure that speed changes are not going
1431 * to corrupt any outgoing frame. Look at nsc-ircc for the gory
1432 * details - Jean II */
1434 /* Check if we need to change the speed */
1435 speed = irda_get_next_speed(skb);
1436 if ((speed != self->io.speed) && (speed != -1)) {
1437 /* Check for empty frame */
1439 ali_ircc_change_speed(self, speed);
1440 dev->trans_start = jiffies;
1441 spin_unlock_irqrestore(&self->lock, flags);
1445 self->new_speed = speed;
1448 /* Register and copy this frame to DMA memory */
1449 self->tx_fifo.queue[self->tx_fifo.free].start = self->tx_fifo.tail;
1450 self->tx_fifo.queue[self->tx_fifo.free].len = skb->len;
1451 self->tx_fifo.tail += skb->len;
1453 self->stats.tx_bytes += skb->len;
1455 memcpy(self->tx_fifo.queue[self->tx_fifo.free].start, skb->data,
1458 self->tx_fifo.len++;
1459 self->tx_fifo.free++;
1461 /* Start transmit only if there is currently no transmit going on */
1462 if (self->tx_fifo.len == 1)
1464 /* Check if we must wait the min turn time or not */
1465 mtt = irda_get_mtt(skb);
1469 /* Check how much time we have used already */
1470 do_gettimeofday(&self->now);
1472 diff = self->now.tv_usec - self->stamp.tv_usec;
1473 /* self->stamp is set from ali_ircc_dma_receive_complete() */
1475 IRDA_DEBUG(1, "%s(), ******* diff = %d ******* \n", __FUNCTION__ , diff);
1480 /* Check if the mtt is larger than the time we have
1481 * already used by all the protocol processing
1488 * Use timer if delay larger than 1000 us, and
1489 * use udelay for smaller values which should
1494 /* Adjust for timer resolution */
1495 mtt = (mtt+250) / 500; /* 4 discard, 5 get advanced, Let's round off */
1497 IRDA_DEBUG(1, "%s(), ************** mtt = %d ***********\n", __FUNCTION__ , mtt);
1500 if (mtt == 1) /* 500 us */
1502 switch_bank(iobase, BANK1);
1503 outb(TIMER_IIR_500, iobase+FIR_TIMER_IIR);
1505 else if (mtt == 2) /* 1 ms */
1507 switch_bank(iobase, BANK1);
1508 outb(TIMER_IIR_1ms, iobase+FIR_TIMER_IIR);
1510 else /* > 2ms -> 4ms */
1512 switch_bank(iobase, BANK1);
1513 outb(TIMER_IIR_2ms, iobase+FIR_TIMER_IIR);
1518 outb(inb(iobase+FIR_CR) | CR_TIMER_EN, iobase+FIR_CR);
1519 self->io.direction = IO_XMIT;
1521 /* Enable timer interrupt */
1522 self->ier = IER_TIMER;
1523 SetCOMInterrupts(self, TRUE);
1525 /* Timer will take care of the rest */
1530 } // if (if (mtt > diff)
1533 /* Enable EOM interrupt */
1534 self->ier = IER_EOM;
1535 SetCOMInterrupts(self, TRUE);
1537 /* Transmit frame */
1538 ali_ircc_dma_xmit(self);
1539 } // if (self->tx_fifo.len == 1)
1543 /* Not busy transmitting anymore if window is not full */
1544 if (self->tx_fifo.free < MAX_TX_WINDOW)
1545 netif_wake_queue(self->netdev);
1547 /* Restore bank register */
1548 switch_bank(iobase, BANK0);
1550 dev->trans_start = jiffies;
1551 spin_unlock_irqrestore(&self->lock, flags);
1554 IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
1559 static void ali_ircc_dma_xmit(struct ali_ircc_cb *self)
1562 unsigned char FIFO_OPTI, Hi, Lo;
1565 IRDA_DEBUG(1, "%s(), ---------------- Start -----------------\n", __FUNCTION__ );
1567 iobase = self->io.fir_base;
1569 /* FIFO threshold , this method comes from NDIS5 code */
1571 if(self->tx_fifo.queue[self->tx_fifo.ptr].len < TX_FIFO_Threshold)
1572 FIFO_OPTI = self->tx_fifo.queue[self->tx_fifo.ptr].len-1;
1574 FIFO_OPTI = TX_FIFO_Threshold;
1577 switch_bank(iobase, BANK1);
1578 outb(inb(iobase+FIR_CR) & ~CR_DMA_EN, iobase+FIR_CR);
1580 self->io.direction = IO_XMIT;
1582 irda_setup_dma(self->io.dma,
1583 ((u8 *)self->tx_fifo.queue[self->tx_fifo.ptr].start -
1584 self->tx_buff.head) + self->tx_buff_dma,
1585 self->tx_fifo.queue[self->tx_fifo.ptr].len,
1589 switch_bank(iobase, BANK0);
1590 outb(LCR_A_FIFO_RESET, iobase+FIR_LCR_A);
1592 /* Set Tx FIFO threshold */
1593 if (self->fifo_opti_buf!=FIFO_OPTI)
1595 switch_bank(iobase, BANK1);
1596 outb(FIFO_OPTI, iobase+FIR_FIFO_TR) ;
1597 self->fifo_opti_buf=FIFO_OPTI;
1600 /* Set Tx DMA threshold */
1601 switch_bank(iobase, BANK1);
1602 outb(TX_DMA_Threshold, iobase+FIR_DMA_TR);
1604 /* Set max Tx frame size */
1605 Hi = (self->tx_fifo.queue[self->tx_fifo.ptr].len >> 8) & 0x0f;
1606 Lo = self->tx_fifo.queue[self->tx_fifo.ptr].len & 0xff;
1607 switch_bank(iobase, BANK2);
1608 outb(Hi, iobase+FIR_TX_DSR_HI);
1609 outb(Lo, iobase+FIR_TX_DSR_LO);
1611 /* Disable SIP , Disable Brick Wall (we don't support in TX mode), Change to TX mode */
1612 switch_bank(iobase, BANK0);
1613 tmp = inb(iobase+FIR_LCR_B);
1614 tmp &= ~0x20; // Disable SIP
1615 outb(((unsigned char)(tmp & 0x3f) | LCR_B_TX_MODE) & ~LCR_B_BW, iobase+FIR_LCR_B);
1616 IRDA_DEBUG(1, "%s(), ******* Change to TX mode: FIR_LCR_B = 0x%x ******* \n", __FUNCTION__ , inb(iobase+FIR_LCR_B));
1618 outb(0, iobase+FIR_LSR);
1620 /* Enable DMA and Burst Mode */
1621 switch_bank(iobase, BANK1);
1622 outb(inb(iobase+FIR_CR) | CR_DMA_EN | CR_DMA_BURST, iobase+FIR_CR);
1624 switch_bank(iobase, BANK0);
1626 IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
1629 static int ali_ircc_dma_xmit_complete(struct ali_ircc_cb *self)
1634 IRDA_DEBUG(1, "%s(), ---------------- Start -----------------\n", __FUNCTION__ );
1636 iobase = self->io.fir_base;
1639 switch_bank(iobase, BANK1);
1640 outb(inb(iobase+FIR_CR) & ~CR_DMA_EN, iobase+FIR_CR);
1642 /* Check for underrun! */
1643 switch_bank(iobase, BANK0);
1644 if((inb(iobase+FIR_LSR) & LSR_FRAME_ABORT) == LSR_FRAME_ABORT)
1647 IRDA_ERROR("%s(), ********* LSR_FRAME_ABORT *********\n", __FUNCTION__);
1648 self->stats.tx_errors++;
1649 self->stats.tx_fifo_errors++;
1653 self->stats.tx_packets++;
1656 /* Check if we need to change the speed */
1657 if (self->new_speed)
1659 ali_ircc_change_speed(self, self->new_speed);
1660 self->new_speed = 0;
1663 /* Finished with this frame, so prepare for next */
1664 self->tx_fifo.ptr++;
1665 self->tx_fifo.len--;
1667 /* Any frames to be sent back-to-back? */
1668 if (self->tx_fifo.len)
1670 ali_ircc_dma_xmit(self);
1672 /* Not finished yet! */
1676 { /* Reset Tx FIFO info */
1677 self->tx_fifo.len = self->tx_fifo.ptr = self->tx_fifo.free = 0;
1678 self->tx_fifo.tail = self->tx_buff.head;
1681 /* Make sure we have room for more frames */
1682 if (self->tx_fifo.free < MAX_TX_WINDOW) {
1683 /* Not busy transmitting anymore */
1684 /* Tell the network layer, that we can accept more frames */
1685 netif_wake_queue(self->netdev);
1688 switch_bank(iobase, BANK0);
1690 IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
1695 * Function ali_ircc_dma_receive (self)
1697 * Get ready for receiving a frame. The device will initiate a DMA
1698 * if it starts to receive a frame.
1701 static int ali_ircc_dma_receive(struct ali_ircc_cb *self)
1705 IRDA_DEBUG(1, "%s(), ---------------- Start -----------------\n", __FUNCTION__ );
1707 iobase = self->io.fir_base;
1709 /* Reset Tx FIFO info */
1710 self->tx_fifo.len = self->tx_fifo.ptr = self->tx_fifo.free = 0;
1711 self->tx_fifo.tail = self->tx_buff.head;
1714 switch_bank(iobase, BANK1);
1715 outb(inb(iobase+FIR_CR) & ~CR_DMA_EN, iobase+FIR_CR);
1717 /* Reset Message Count */
1718 switch_bank(iobase, BANK0);
1719 outb(0x07, iobase+FIR_LSR);
1721 self->rcvFramesOverflow = FALSE;
1723 self->LineStatus = inb(iobase+FIR_LSR) ;
1725 /* Reset Rx FIFO info */
1726 self->io.direction = IO_RECV;
1727 self->rx_buff.data = self->rx_buff.head;
1730 // switch_bank(iobase, BANK0);
1731 outb(LCR_A_FIFO_RESET, iobase+FIR_LCR_A);
1733 self->st_fifo.len = self->st_fifo.pending_bytes = 0;
1734 self->st_fifo.tail = self->st_fifo.head = 0;
1736 irda_setup_dma(self->io.dma, self->rx_buff_dma, self->rx_buff.truesize,
1739 /* Set Receive Mode,Brick Wall */
1740 //switch_bank(iobase, BANK0);
1741 tmp = inb(iobase+FIR_LCR_B);
1742 outb((unsigned char)(tmp &0x3f) | LCR_B_RX_MODE | LCR_B_BW , iobase + FIR_LCR_B); // 2000/12/1 05:16PM
1743 IRDA_DEBUG(1, "%s(), *** Change To RX mode: FIR_LCR_B = 0x%x *** \n", __FUNCTION__ , inb(iobase+FIR_LCR_B));
1745 /* Set Rx Threshold */
1746 switch_bank(iobase, BANK1);
1747 outb(RX_FIFO_Threshold, iobase+FIR_FIFO_TR);
1748 outb(RX_DMA_Threshold, iobase+FIR_DMA_TR);
1750 /* Enable DMA and Burst Mode */
1751 // switch_bank(iobase, BANK1);
1752 outb(CR_DMA_EN | CR_DMA_BURST, iobase+FIR_CR);
1754 switch_bank(iobase, BANK0);
1755 IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
1759 static int ali_ircc_dma_receive_complete(struct ali_ircc_cb *self)
1761 struct st_fifo *st_fifo;
1762 struct sk_buff *skb;
1763 __u8 status, MessageCount;
1764 int len, i, iobase, val;
1766 IRDA_DEBUG(1, "%s(), ---------------- Start -----------------\n", __FUNCTION__ );
1768 st_fifo = &self->st_fifo;
1769 iobase = self->io.fir_base;
1771 switch_bank(iobase, BANK0);
1772 MessageCount = inb(iobase+ FIR_LSR)&0x07;
1774 if (MessageCount > 0)
1775 IRDA_DEBUG(0, "%s(), Messsage count = %d,\n", __FUNCTION__ , MessageCount);
1777 for (i=0; i<=MessageCount; i++)
1780 switch_bank(iobase, BANK0);
1781 status = inb(iobase+FIR_LSR);
1783 switch_bank(iobase, BANK2);
1784 len = inb(iobase+FIR_RX_DSR_HI) & 0x0f;
1786 len |= inb(iobase+FIR_RX_DSR_LO);
1788 IRDA_DEBUG(1, "%s(), RX Length = 0x%.2x,\n", __FUNCTION__ , len);
1789 IRDA_DEBUG(1, "%s(), RX Status = 0x%.2x,\n", __FUNCTION__ , status);
1791 if (st_fifo->tail >= MAX_RX_WINDOW) {
1792 IRDA_DEBUG(0, "%s(), window is full!\n", __FUNCTION__ );
1796 st_fifo->entries[st_fifo->tail].status = status;
1797 st_fifo->entries[st_fifo->tail].len = len;
1798 st_fifo->pending_bytes += len;
1803 for (i=0; i<=MessageCount; i++)
1805 /* Get first entry */
1806 status = st_fifo->entries[st_fifo->head].status;
1807 len = st_fifo->entries[st_fifo->head].len;
1808 st_fifo->pending_bytes -= len;
1812 /* Check for errors */
1813 if ((status & 0xd8) || self->rcvFramesOverflow || (len==0))
1815 IRDA_DEBUG(0,"%s(), ************* RX Errors ************ \n", __FUNCTION__ );
1818 self->stats.rx_errors++;
1820 self->rx_buff.data += len;
1822 if (status & LSR_FIFO_UR)
1824 self->stats.rx_frame_errors++;
1825 IRDA_DEBUG(0,"%s(), ************* FIFO Errors ************ \n", __FUNCTION__ );
1827 if (status & LSR_FRAME_ERROR)
1829 self->stats.rx_frame_errors++;
1830 IRDA_DEBUG(0,"%s(), ************* FRAME Errors ************ \n", __FUNCTION__ );
1833 if (status & LSR_CRC_ERROR)
1835 self->stats.rx_crc_errors++;
1836 IRDA_DEBUG(0,"%s(), ************* CRC Errors ************ \n", __FUNCTION__ );
1839 if(self->rcvFramesOverflow)
1841 self->stats.rx_frame_errors++;
1842 IRDA_DEBUG(0,"%s(), ************* Overran DMA buffer ************ \n", __FUNCTION__ );
1846 self->stats.rx_frame_errors++;
1847 IRDA_DEBUG(0,"%s(), ********** Receive Frame Size = 0 ********* \n", __FUNCTION__ );
1853 if (st_fifo->pending_bytes < 32)
1855 switch_bank(iobase, BANK0);
1856 val = inb(iobase+FIR_BSR);
1857 if ((val& BSR_FIFO_NOT_EMPTY)== 0x80)
1859 IRDA_DEBUG(0, "%s(), ************* BSR_FIFO_NOT_EMPTY ************ \n", __FUNCTION__ );
1861 /* Put this entry back in fifo */
1864 st_fifo->pending_bytes += len;
1865 st_fifo->entries[st_fifo->head].status = status;
1866 st_fifo->entries[st_fifo->head].len = len;
1869 * DMA not finished yet, so try again
1870 * later, set timer value, resolution
1874 switch_bank(iobase, BANK1);
1875 outb(TIMER_IIR_500, iobase+FIR_TIMER_IIR); // 2001/1/2 05:07PM
1878 outb(inb(iobase+FIR_CR) | CR_TIMER_EN, iobase+FIR_CR);
1880 return FALSE; /* I'll be back! */
1885 * Remember the time we received this frame, so we can
1886 * reduce the min turn time a bit since we will know
1887 * how much time we have used for protocol processing
1889 do_gettimeofday(&self->stamp);
1891 skb = dev_alloc_skb(len+1);
1894 IRDA_WARNING("%s(), memory squeeze, "
1895 "dropping frame.\n",
1897 self->stats.rx_dropped++;
1902 /* Make sure IP header gets aligned */
1903 skb_reserve(skb, 1);
1905 /* Copy frame without CRC, CRC is removed by hardware*/
1907 memcpy(skb->data, self->rx_buff.data, len);
1909 /* Move to next frame */
1910 self->rx_buff.data += len;
1911 self->stats.rx_bytes += len;
1912 self->stats.rx_packets++;
1914 skb->dev = self->netdev;
1915 skb->mac.raw = skb->data;
1916 skb->protocol = htons(ETH_P_IRDA);
1918 self->netdev->last_rx = jiffies;
1922 switch_bank(iobase, BANK0);
1924 IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
1931 * Function ali_ircc_sir_hard_xmit (skb, dev)
1933 * Transmit the frame!
1936 static int ali_ircc_sir_hard_xmit(struct sk_buff *skb, struct net_device *dev)
1938 struct ali_ircc_cb *self;
1939 unsigned long flags;
1943 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
1945 IRDA_ASSERT(dev != NULL, return 0;);
1947 self = (struct ali_ircc_cb *) dev->priv;
1948 IRDA_ASSERT(self != NULL, return 0;);
1950 iobase = self->io.sir_base;
1952 netif_stop_queue(dev);
1954 /* Make sure tests *& speed change are atomic */
1955 spin_lock_irqsave(&self->lock, flags);
1957 /* Note : you should make sure that speed changes are not going
1958 * to corrupt any outgoing frame. Look at nsc-ircc for the gory
1959 * details - Jean II */
1961 /* Check if we need to change the speed */
1962 speed = irda_get_next_speed(skb);
1963 if ((speed != self->io.speed) && (speed != -1)) {
1964 /* Check for empty frame */
1966 ali_ircc_change_speed(self, speed);
1967 dev->trans_start = jiffies;
1968 spin_unlock_irqrestore(&self->lock, flags);
1972 self->new_speed = speed;
1975 /* Init tx buffer */
1976 self->tx_buff.data = self->tx_buff.head;
1978 /* Copy skb to tx_buff while wrapping, stuffing and making CRC */
1979 self->tx_buff.len = async_wrap_skb(skb, self->tx_buff.data,
1980 self->tx_buff.truesize);
1982 self->stats.tx_bytes += self->tx_buff.len;
1984 /* Turn on transmit finished interrupt. Will fire immediately! */
1985 outb(UART_IER_THRI, iobase+UART_IER);
1987 dev->trans_start = jiffies;
1988 spin_unlock_irqrestore(&self->lock, flags);
1992 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
1999 * Function ali_ircc_net_ioctl (dev, rq, cmd)
2001 * Process IOCTL commands for this device
2004 static int ali_ircc_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2006 struct if_irda_req *irq = (struct if_irda_req *) rq;
2007 struct ali_ircc_cb *self;
2008 unsigned long flags;
2011 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
2013 IRDA_ASSERT(dev != NULL, return -1;);
2017 IRDA_ASSERT(self != NULL, return -1;);
2019 IRDA_DEBUG(2, "%s(), %s, (cmd=0x%X)\n", __FUNCTION__ , dev->name, cmd);
2022 case SIOCSBANDWIDTH: /* Set bandwidth */
2023 IRDA_DEBUG(1, "%s(), SIOCSBANDWIDTH\n", __FUNCTION__ );
2025 * This function will also be used by IrLAP to change the
2026 * speed, so we still must allow for speed change within
2027 * interrupt context.
2029 if (!in_interrupt() && !capable(CAP_NET_ADMIN))
2032 spin_lock_irqsave(&self->lock, flags);
2033 ali_ircc_change_speed(self, irq->ifr_baudrate);
2034 spin_unlock_irqrestore(&self->lock, flags);
2036 case SIOCSMEDIABUSY: /* Set media busy */
2037 IRDA_DEBUG(1, "%s(), SIOCSMEDIABUSY\n", __FUNCTION__ );
2038 if (!capable(CAP_NET_ADMIN))
2040 irda_device_set_media_busy(self->netdev, TRUE);
2042 case SIOCGRECEIVING: /* Check if we are receiving right now */
2043 IRDA_DEBUG(2, "%s(), SIOCGRECEIVING\n", __FUNCTION__ );
2044 /* This is protected */
2045 irq->ifr_receiving = ali_ircc_is_receiving(self);
2051 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
2057 * Function ali_ircc_is_receiving (self)
2059 * Return TRUE is we are currently receiving a frame
2062 static int ali_ircc_is_receiving(struct ali_ircc_cb *self)
2064 unsigned long flags;
2068 IRDA_DEBUG(2, "%s(), ---------------- Start -----------------\n", __FUNCTION__ );
2070 IRDA_ASSERT(self != NULL, return FALSE;);
2072 spin_lock_irqsave(&self->lock, flags);
2074 if (self->io.speed > 115200)
2076 iobase = self->io.fir_base;
2078 switch_bank(iobase, BANK1);
2079 if((inb(iobase+FIR_FIFO_FR) & 0x3f) != 0)
2081 /* We are receiving something */
2082 IRDA_DEBUG(1, "%s(), We are receiving something\n", __FUNCTION__ );
2085 switch_bank(iobase, BANK0);
2089 status = (self->rx_buff.state != OUTSIDE_FRAME);
2092 spin_unlock_irqrestore(&self->lock, flags);
2094 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
2099 static struct net_device_stats *ali_ircc_net_get_stats(struct net_device *dev)
2101 struct ali_ircc_cb *self = (struct ali_ircc_cb *) dev->priv;
2103 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
2105 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
2107 return &self->stats;
2110 static void ali_ircc_suspend(struct ali_ircc_cb *self)
2112 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
2114 IRDA_MESSAGE("%s, Suspending\n", driver_name);
2116 if (self->io.suspended)
2119 ali_ircc_net_close(self->netdev);
2121 self->io.suspended = 1;
2123 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
2126 static void ali_ircc_wakeup(struct ali_ircc_cb *self)
2128 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
2130 if (!self->io.suspended)
2133 ali_ircc_net_open(self->netdev);
2135 IRDA_MESSAGE("%s, Waking up\n", driver_name);
2137 self->io.suspended = 0;
2139 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
2142 static int ali_ircc_pmproc(struct pm_dev *dev, pm_request_t rqst, void *data)
2144 struct ali_ircc_cb *self = (struct ali_ircc_cb*) dev->data;
2146 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
2151 ali_ircc_suspend(self);
2154 ali_ircc_wakeup(self);
2159 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
2165 /* ALi Chip Function */
2167 static void SetCOMInterrupts(struct ali_ircc_cb *self , unsigned char enable)
2170 unsigned char newMask;
2172 int iobase = self->io.fir_base; /* or sir_base */
2174 IRDA_DEBUG(2, "%s(), -------- Start -------- ( Enable = %d )\n", __FUNCTION__ , enable);
2176 /* Enable the interrupt which we wish to */
2178 if (self->io.direction == IO_XMIT)
2180 if (self->io.speed > 115200) /* FIR, MIR */
2182 newMask = self->ier;
2186 newMask = UART_IER_THRI | UART_IER_RDI;
2190 if (self->io.speed > 115200) /* FIR, MIR */
2192 newMask = self->ier;
2196 newMask = UART_IER_RDI;
2200 else /* Disable all the interrupts */
2206 //SIR and FIR has different registers
2207 if (self->io.speed > 115200)
2209 switch_bank(iobase, BANK0);
2210 outb(newMask, iobase+FIR_IER);
2213 outb(newMask, iobase+UART_IER);
2215 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
2218 static void SIR2FIR(int iobase)
2220 //unsigned char tmp;
2222 IRDA_DEBUG(1, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
2224 /* Already protected (change_speed() or setup()), no need to lock.
2227 outb(0x28, iobase+UART_MCR);
2228 outb(0x68, iobase+UART_MCR);
2229 outb(0x88, iobase+UART_MCR);
2231 outb(0x60, iobase+FIR_MCR); /* Master Reset */
2232 outb(0x20, iobase+FIR_MCR); /* Master Interrupt Enable */
2234 //tmp = inb(iobase+FIR_LCR_B); /* SIP enable */
2236 //outb(tmp, iobase+FIR_LCR_B);
2238 IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
2241 static void FIR2SIR(int iobase)
2245 IRDA_DEBUG(1, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
2247 /* Already protected (change_speed() or setup()), no need to lock.
2250 outb(0x20, iobase+FIR_MCR); /* IRQ to low */
2251 outb(0x00, iobase+UART_IER);
2253 outb(0xA0, iobase+FIR_MCR); /* Don't set master reset */
2254 outb(0x00, iobase+UART_FCR);
2255 outb(0x07, iobase+UART_FCR);
2257 val = inb(iobase+UART_RX);
2258 val = inb(iobase+UART_LSR);
2259 val = inb(iobase+UART_MSR);
2261 IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
2264 MODULE_AUTHOR("Benjamin Kong <benjamin_kong@ali.com.tw>");
2265 MODULE_DESCRIPTION("ALi FIR Controller Driver");
2266 MODULE_LICENSE("GPL");
2269 module_param_array(io, int, NULL, 0);
2270 MODULE_PARM_DESC(io, "Base I/O addresses");
2271 module_param_array(irq, int, NULL, 0);
2272 MODULE_PARM_DESC(irq, "IRQ lines");
2273 module_param_array(dma, int, NULL, 0);
2274 MODULE_PARM_DESC(dma, "DMA channels");
2276 module_init(ali_ircc_init);
2277 module_exit(ali_ircc_cleanup);