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>
39 #include <asm/byteorder.h>
43 #include <net/irda/wrapper.h>
44 #include <net/irda/irda.h>
45 #include <net/irda/irda_device.h>
49 #define CHIP_IO_EXTENT 8
50 #define BROKEN_DONGLE_ID
52 static char *driver_name = "ali-ircc";
54 /* Module parameters */
55 static int qos_mtt_bits = 0x07; /* 1 ms or more */
57 /* Use BIOS settions by default, but user may supply module parameters */
58 static unsigned int io[] = { ~0, ~0, ~0, ~0 };
59 static unsigned int irq[] = { 0, 0, 0, 0 };
60 static unsigned int dma[] = { 0, 0, 0, 0 };
62 static int ali_ircc_probe_53(ali_chip_t *chip, chipio_t *info);
63 static int ali_ircc_init_43(ali_chip_t *chip, chipio_t *info);
64 static int ali_ircc_init_53(ali_chip_t *chip, chipio_t *info);
66 /* These are the currently known ALi sourth-bridge chipsets, the only one difference
67 * is that M1543C doesn't support HP HDSL-3600
69 static ali_chip_t chips[] =
71 { "M1543", { 0x3f0, 0x370 }, 0x51, 0x23, 0x20, 0x43, ali_ircc_probe_53, ali_ircc_init_43 },
72 { "M1535", { 0x3f0, 0x370 }, 0x51, 0x23, 0x20, 0x53, ali_ircc_probe_53, ali_ircc_init_53 },
73 { "M1563", { 0x3f0, 0x370 }, 0x51, 0x23, 0x20, 0x63, ali_ircc_probe_53, ali_ircc_init_53 },
77 /* Max 4 instances for now */
78 static struct ali_ircc_cb *dev_self[] = { NULL, NULL, NULL, NULL };
81 static char *dongle_types[] = {
85 "No dongle connected",
89 static int ali_ircc_open(int i, chipio_t *info);
91 static int ali_ircc_close(struct ali_ircc_cb *self);
93 static int ali_ircc_setup(chipio_t *info);
94 static int ali_ircc_is_receiving(struct ali_ircc_cb *self);
95 static int ali_ircc_net_open(struct net_device *dev);
96 static int ali_ircc_net_close(struct net_device *dev);
97 static int ali_ircc_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
98 static int ali_ircc_pmproc(struct pm_dev *dev, pm_request_t rqst, void *data);
99 static void ali_ircc_change_speed(struct ali_ircc_cb *self, __u32 baud);
100 static void ali_ircc_suspend(struct ali_ircc_cb *self);
101 static void ali_ircc_wakeup(struct ali_ircc_cb *self);
102 static struct net_device_stats *ali_ircc_net_get_stats(struct net_device *dev);
105 static int ali_ircc_sir_hard_xmit(struct sk_buff *skb, struct net_device *dev);
106 static irqreturn_t ali_ircc_sir_interrupt(struct ali_ircc_cb *self);
107 static void ali_ircc_sir_receive(struct ali_ircc_cb *self);
108 static void ali_ircc_sir_write_wakeup(struct ali_ircc_cb *self);
109 static int ali_ircc_sir_write(int iobase, int fifo_size, __u8 *buf, int len);
110 static void ali_ircc_sir_change_speed(struct ali_ircc_cb *priv, __u32 speed);
113 static int ali_ircc_fir_hard_xmit(struct sk_buff *skb, struct net_device *dev);
114 static void ali_ircc_fir_change_speed(struct ali_ircc_cb *priv, __u32 speed);
115 static irqreturn_t ali_ircc_fir_interrupt(struct ali_ircc_cb *self);
116 static int ali_ircc_dma_receive(struct ali_ircc_cb *self);
117 static int ali_ircc_dma_receive_complete(struct ali_ircc_cb *self);
118 static int ali_ircc_dma_xmit_complete(struct ali_ircc_cb *self);
119 static void ali_ircc_dma_xmit(struct ali_ircc_cb *self);
122 static int ali_ircc_read_dongle_id (int i, chipio_t *info);
123 static void ali_ircc_change_dongle_speed(struct ali_ircc_cb *priv, int speed);
125 /* ALi chip function */
126 static void SIR2FIR(int iobase);
127 static void FIR2SIR(int iobase);
128 static void SetCOMInterrupts(struct ali_ircc_cb *self , unsigned char enable);
131 * Function ali_ircc_init ()
133 * Initialize chip. Find out whay kinds of chips we are dealing with
134 * and their configuation registers address
136 static int __init ali_ircc_init(void)
145 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__);
147 /* Probe for all the ALi chipsets we know about */
148 for (chip= chips; chip->name; chip++, i++)
150 IRDA_DEBUG(2, "%s(), Probing for %s ...\n", __FUNCTION__, chip->name);
152 /* Try all config registers for this chip */
153 for (cfg=0; cfg<2; cfg++)
155 cfg_base = chip->cfg[cfg];
159 memset(&info, 0, sizeof(chipio_t));
160 info.cfg_base = cfg_base;
161 info.fir_base = io[i];
166 /* Enter Configuration */
167 outb(chip->entr1, cfg_base);
168 outb(chip->entr2, cfg_base);
170 /* Select Logical Device 5 Registers (UART2) */
171 outb(0x07, cfg_base);
172 outb(0x05, cfg_base+1);
174 /* Read Chip Identification Register */
175 outb(chip->cid_index, cfg_base);
176 reg = inb(cfg_base+1);
178 if (reg == chip->cid_value)
180 IRDA_DEBUG(2, "%s(), Chip found at 0x%03x\n", __FUNCTION__, cfg_base);
182 outb(0x1F, cfg_base);
183 revision = inb(cfg_base+1);
184 IRDA_DEBUG(2, "%s(), Found %s chip, revision=%d\n", __FUNCTION__,
185 chip->name, revision);
188 * If the user supplies the base address, then
189 * we init the chip, if not we probe the values
194 chip->init(chip, &info);
198 chip->probe(chip, &info);
201 if (ali_ircc_open(i, &info) == 0)
207 IRDA_DEBUG(2, "%s(), No %s chip at 0x%03x\n", __FUNCTION__, chip->name, cfg_base);
209 /* Exit configuration */
210 outb(0xbb, cfg_base);
214 IRDA_DEBUG(2, "%s(), ----------------- End -----------------\n", __FUNCTION__);
219 * Function ali_ircc_cleanup ()
221 * Close all configured chips
224 static void __exit ali_ircc_cleanup(void)
228 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__);
230 pm_unregister_all(ali_ircc_pmproc);
232 for (i=0; i < 4; i++) {
234 ali_ircc_close(dev_self[i]);
237 IRDA_DEBUG(2, "%s(), ----------------- End -----------------\n", __FUNCTION__);
241 * Function ali_ircc_open (int i, chipio_t *inf)
243 * Open driver instance
246 static int ali_ircc_open(int i, chipio_t *info)
248 struct net_device *dev;
249 struct ali_ircc_cb *self;
250 struct pm_dev *pmdev;
254 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__);
256 /* Set FIR FIFO and DMA Threshold */
257 if ((ali_ircc_setup(info)) == -1)
260 dev = alloc_irdadev(sizeof(*self));
262 ERROR("%s(), can't allocate memory for control block!\n", __FUNCTION__);
268 spin_lock_init(&self->lock);
270 /* Need to store self somewhere */
275 self->io.cfg_base = info->cfg_base; /* In ali_ircc_probe_53 assign */
276 self->io.fir_base = info->fir_base; /* info->sir_base = info->fir_base */
277 self->io.sir_base = info->sir_base; /* ALi SIR and FIR use the same address */
278 self->io.irq = info->irq;
279 self->io.fir_ext = CHIP_IO_EXTENT;
280 self->io.dma = info->dma;
281 self->io.fifo_size = 16; /* SIR: 16, FIR: 32 Benjamin 2000/11/1 */
283 /* Reserve the ioports that we need */
284 if (!request_region(self->io.fir_base, self->io.fir_ext, driver_name)) {
285 WARNING("%s(), can't get iobase of 0x%03x\n", __FUNCTION__,
291 /* Initialize QoS for this device */
292 irda_init_max_qos_capabilies(&self->qos);
294 /* The only value we must override it the baudrate */
295 self->qos.baud_rate.bits = IR_9600|IR_19200|IR_38400|IR_57600|
296 IR_115200|IR_576000|IR_1152000|(IR_4000000 << 8); // benjamin 2000/11/8 05:27PM
298 self->qos.min_turn_time.bits = qos_mtt_bits;
300 irda_qos_bits_to_value(&self->qos);
302 /* Max DMA buffer size needed = (data_size + 6) * (window_size) + 6; */
303 self->rx_buff.truesize = 14384;
304 self->tx_buff.truesize = 14384;
306 /* Allocate memory if needed */
307 self->rx_buff.head = (__u8 *) kmalloc(self->rx_buff.truesize,
308 GFP_KERNEL |GFP_DMA);
309 if (self->rx_buff.head == NULL) {
313 memset(self->rx_buff.head, 0, self->rx_buff.truesize);
315 self->tx_buff.head = (__u8 *) kmalloc(self->tx_buff.truesize,
317 if (self->tx_buff.head == NULL) {
321 memset(self->tx_buff.head, 0, self->tx_buff.truesize);
323 self->rx_buff.in_frame = FALSE;
324 self->rx_buff.state = OUTSIDE_FRAME;
325 self->tx_buff.data = self->tx_buff.head;
326 self->rx_buff.data = self->rx_buff.head;
328 /* Reset Tx queue info */
329 self->tx_fifo.len = self->tx_fifo.ptr = self->tx_fifo.free = 0;
330 self->tx_fifo.tail = self->tx_buff.head;
333 /* Keep track of module usage */
334 SET_MODULE_OWNER(dev);
336 /* Override the network functions we need to use */
337 dev->hard_start_xmit = ali_ircc_sir_hard_xmit;
338 dev->open = ali_ircc_net_open;
339 dev->stop = ali_ircc_net_close;
340 dev->do_ioctl = ali_ircc_net_ioctl;
341 dev->get_stats = ali_ircc_net_get_stats;
343 err = register_netdev(dev);
345 ERROR("%s(), register_netdev() failed!\n", __FUNCTION__);
348 MESSAGE("IrDA: Registered device %s\n", dev->name);
350 /* Check dongle id */
351 dongle_id = ali_ircc_read_dongle_id(i, info);
352 MESSAGE("%s(), %s, Found dongle: %s\n", __FUNCTION__, driver_name, dongle_types[dongle_id]);
354 self->io.dongle_id = dongle_id;
356 pmdev = pm_register(PM_SYS_DEV, PM_SYS_IRDA, ali_ircc_pmproc);
360 IRDA_DEBUG(2, "%s(), ----------------- End -----------------\n", __FUNCTION__);
365 kfree(self->tx_buff.head);
367 kfree(self->rx_buff.head);
369 release_region(self->io.fir_base, self->io.fir_ext);
378 * Function ali_ircc_close (self)
380 * Close driver instance
383 static int __exit ali_ircc_close(struct ali_ircc_cb *self)
387 IRDA_DEBUG(4, "%s(), ---------------- Start ----------------\n", __FUNCTION__);
389 ASSERT(self != NULL, return -1;);
391 iobase = self->io.fir_base;
393 /* Remove netdevice */
394 unregister_netdev(self->netdev);
396 /* Release the PORT that this driver is using */
397 IRDA_DEBUG(4, "%s(), Releasing Region %03x\n", __FUNCTION__, self->io.fir_base);
398 release_region(self->io.fir_base, self->io.fir_ext);
400 if (self->tx_buff.head)
401 kfree(self->tx_buff.head);
403 if (self->rx_buff.head)
404 kfree(self->rx_buff.head);
406 dev_self[self->index] = NULL;
407 free_netdev(self->netdev);
409 IRDA_DEBUG(2, "%s(), ----------------- End -----------------\n", __FUNCTION__);
415 * Function ali_ircc_init_43 (chip, info)
417 * Initialize the ALi M1543 chip.
419 static int ali_ircc_init_43(ali_chip_t *chip, chipio_t *info)
421 /* All controller information like I/O address, DMA channel, IRQ
429 * Function ali_ircc_init_53 (chip, info)
431 * Initialize the ALi M1535 chip.
433 static int ali_ircc_init_53(ali_chip_t *chip, chipio_t *info)
435 /* All controller information like I/O address, DMA channel, IRQ
443 * Function ali_ircc_probe_53 (chip, info)
445 * Probes for the ALi M1535D or M1535
447 static int ali_ircc_probe_53(ali_chip_t *chip, chipio_t *info)
449 int cfg_base = info->cfg_base;
452 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__);
454 /* Enter Configuration */
455 outb(chip->entr1, cfg_base);
456 outb(chip->entr2, cfg_base);
458 /* Select Logical Device 5 Registers (UART2) */
459 outb(0x07, cfg_base);
460 outb(0x05, cfg_base+1);
462 /* Read address control register */
463 outb(0x60, cfg_base);
464 hi = inb(cfg_base+1);
465 outb(0x61, cfg_base);
466 low = inb(cfg_base+1);
467 info->fir_base = (hi<<8) + low;
469 info->sir_base = info->fir_base;
471 IRDA_DEBUG(2, "%s(), probing fir_base=0x%03x\n", __FUNCTION__, info->fir_base);
473 /* Read IRQ control register */
474 outb(0x70, cfg_base);
475 reg = inb(cfg_base+1);
476 info->irq = reg & 0x0f;
477 IRDA_DEBUG(2, "%s(), probing irq=%d\n", __FUNCTION__, info->irq);
479 /* Read DMA channel */
480 outb(0x74, cfg_base);
481 reg = inb(cfg_base+1);
482 info->dma = reg & 0x07;
484 if(info->dma == 0x04)
485 WARNING("%s(), No DMA channel assigned !\n", __FUNCTION__);
487 IRDA_DEBUG(2, "%s(), probing dma=%d\n", __FUNCTION__, info->dma);
489 /* Read Enabled Status */
490 outb(0x30, cfg_base);
491 reg = inb(cfg_base+1);
492 info->enabled = (reg & 0x80) && (reg & 0x01);
493 IRDA_DEBUG(2, "%s(), probing enabled=%d\n", __FUNCTION__, info->enabled);
495 /* Read Power Status */
496 outb(0x22, cfg_base);
497 reg = inb(cfg_base+1);
498 info->suspended = (reg & 0x20);
499 IRDA_DEBUG(2, "%s(), probing suspended=%d\n", __FUNCTION__, info->suspended);
501 /* Exit configuration */
502 outb(0xbb, cfg_base);
504 IRDA_DEBUG(2, "%s(), ----------------- End -----------------\n", __FUNCTION__);
510 * Function ali_ircc_setup (info)
512 * Set FIR FIFO and DMA Threshold
513 * Returns non-negative on success.
516 static int ali_ircc_setup(chipio_t *info)
520 int iobase = info->fir_base;
522 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__);
524 /* Locking comments :
525 * Most operations here need to be protected. We are called before
526 * the device instance is created in ali_ircc_open(), therefore
527 * nobody can bother us - Jean II */
529 /* Switch to FIR space */
533 outb(0x40, iobase+FIR_MCR); // benjamin 2000/11/30 11:45AM
535 /* Read FIR ID Version Register */
536 switch_bank(iobase, BANK3);
537 version = inb(iobase+FIR_ID_VR);
539 /* Should be 0x00 in the M1535/M1535D */
542 ERROR("%s, Wrong chip version %02x\n", driver_name, version);
546 // MESSAGE("%s, Found chip at base=0x%03x\n", driver_name, info->cfg_base);
548 /* Set FIR FIFO Threshold Register */
549 switch_bank(iobase, BANK1);
550 outb(RX_FIFO_Threshold, iobase+FIR_FIFO_TR);
552 /* Set FIR DMA Threshold Register */
553 outb(RX_DMA_Threshold, iobase+FIR_DMA_TR);
556 switch_bank(iobase, BANK2);
557 outb(inb(iobase+FIR_IRDA_CR) | IRDA_CR_CRC, iobase+FIR_IRDA_CR);
559 /* NDIS driver set TX Length here BANK2 Alias 3, Alias4*/
561 /* Switch to Bank 0 */
562 switch_bank(iobase, BANK0);
564 tmp = inb(iobase+FIR_LCR_B);
565 tmp &=~0x20; // disable SIP
566 tmp |= 0x80; // these two steps make RX mode
568 outb(tmp, iobase+FIR_LCR_B);
570 /* Disable Interrupt */
571 outb(0x00, iobase+FIR_IER);
574 /* Switch to SIR space */
577 MESSAGE("%s, driver loaded (Benjamin Kong)\n", driver_name);
579 /* Enable receive interrupts */
580 // outb(UART_IER_RDI, iobase+UART_IER); //benjamin 2000/11/23 01:25PM
581 // Turn on the interrupts in ali_ircc_net_open
583 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__);
589 * Function ali_ircc_read_dongle_id (int index, info)
591 * Try to read dongle indentification. This procedure needs to be executed
592 * once after power-on/reset. It also needs to be used whenever you suspect
593 * that the user may have plugged/unplugged the IrDA Dongle.
595 static int ali_ircc_read_dongle_id (int i, chipio_t *info)
598 int cfg_base = info->cfg_base;
600 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__);
602 /* Enter Configuration */
603 outb(chips[i].entr1, cfg_base);
604 outb(chips[i].entr2, cfg_base);
606 /* Select Logical Device 5 Registers (UART2) */
607 outb(0x07, cfg_base);
608 outb(0x05, cfg_base+1);
611 outb(0xf0, cfg_base);
612 reg = inb(cfg_base+1);
613 dongle_id = ((reg>>6)&0x02) | ((reg>>5)&0x01);
614 IRDA_DEBUG(2, "%s(), probing dongle_id=%d, dongle_types=%s\n", __FUNCTION__,
615 dongle_id, dongle_types[dongle_id]);
617 /* Exit configuration */
618 outb(0xbb, cfg_base);
620 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__);
626 * Function ali_ircc_interrupt (irq, dev_id, regs)
628 * An interrupt from the chip has arrived. Time to do some work
631 static irqreturn_t ali_ircc_interrupt(int irq, void *dev_id,
632 struct pt_regs *regs)
634 struct net_device *dev = (struct net_device *) dev_id;
635 struct ali_ircc_cb *self;
638 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__);
641 WARNING("%s: irq %d for unknown device.\n", driver_name, irq);
645 self = (struct ali_ircc_cb *) dev->priv;
647 spin_lock(&self->lock);
649 /* Dispatch interrupt handler for the current speed */
650 if (self->io.speed > 115200)
651 ret = ali_ircc_fir_interrupt(self);
653 ret = ali_ircc_sir_interrupt(self);
655 spin_unlock(&self->lock);
657 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__);
661 * Function ali_ircc_fir_interrupt(irq, struct ali_ircc_cb *self)
663 * Handle MIR/FIR interrupt
666 static irqreturn_t ali_ircc_fir_interrupt(struct ali_ircc_cb *self)
668 __u8 eir, OldMessageCount;
671 IRDA_DEBUG(1, "%s(), ---------------- Start ----------------\n", __FUNCTION__);
673 iobase = self->io.fir_base;
675 switch_bank(iobase, BANK0);
676 self->InterruptID = inb(iobase+FIR_IIR);
677 self->BusStatus = inb(iobase+FIR_BSR);
679 OldMessageCount = (self->LineStatus + 1) & 0x07;
680 self->LineStatus = inb(iobase+FIR_LSR);
681 //self->ier = inb(iobase+FIR_IER); 2000/12/1 04:32PM
682 eir = self->InterruptID & self->ier; /* Mask out the interesting ones */
684 IRDA_DEBUG(1, "%s(), self->InterruptID = %x\n", __FUNCTION__,self->InterruptID);
685 IRDA_DEBUG(1, "%s(), self->LineStatus = %x\n", __FUNCTION__,self->LineStatus);
686 IRDA_DEBUG(1, "%s(), self->ier = %x\n", __FUNCTION__,self->ier);
687 IRDA_DEBUG(1, "%s(), eir = %x\n", __FUNCTION__,eir);
689 /* Disable interrupts */
690 SetCOMInterrupts(self, FALSE);
692 /* Tx or Rx Interrupt */
696 if (self->io.direction == IO_XMIT) /* TX */
698 IRDA_DEBUG(1, "%s(), ******* IIR_EOM (Tx) *******\n", __FUNCTION__);
700 if(ali_ircc_dma_xmit_complete(self))
702 if (irda_device_txqueue_empty(self->netdev))
704 /* Prepare for receive */
705 ali_ircc_dma_receive(self);
717 IRDA_DEBUG(1, "%s(), ******* IIR_EOM (Rx) *******\n", __FUNCTION__);
719 if(OldMessageCount > ((self->LineStatus+1) & 0x07))
721 self->rcvFramesOverflow = TRUE;
722 IRDA_DEBUG(1, "%s(), ******* self->rcvFramesOverflow = TRUE ******** \n", __FUNCTION__);
725 if (ali_ircc_dma_receive_complete(self))
727 IRDA_DEBUG(1, "%s(), ******* receive complete ******** \n", __FUNCTION__);
733 IRDA_DEBUG(1, "%s(), ******* Not receive complete ******** \n", __FUNCTION__);
735 self->ier = IER_EOM | IER_TIMER;
740 /* Timer Interrupt */
741 else if (eir & IIR_TIMER)
743 if(OldMessageCount > ((self->LineStatus+1) & 0x07))
745 self->rcvFramesOverflow = TRUE;
746 IRDA_DEBUG(1, "%s(), ******* self->rcvFramesOverflow = TRUE ******* \n", __FUNCTION__);
749 switch_bank(iobase, BANK1);
750 tmp = inb(iobase+FIR_CR);
751 outb( tmp& ~CR_TIMER_EN, iobase+FIR_CR);
753 /* Check if this is a Tx timer interrupt */
754 if (self->io.direction == IO_XMIT)
756 ali_ircc_dma_xmit(self);
758 /* Interrupt on EOM */
764 if(ali_ircc_dma_receive_complete(self))
770 self->ier = IER_EOM | IER_TIMER;
775 /* Restore Interrupt */
776 SetCOMInterrupts(self, TRUE);
778 IRDA_DEBUG(1, "%s(), ----------------- End ---------------\n", __FUNCTION__);
779 return IRQ_RETVAL(eir);
783 * Function ali_ircc_sir_interrupt (irq, self, eir)
785 * Handle SIR interrupt
788 static irqreturn_t ali_ircc_sir_interrupt(struct ali_ircc_cb *self)
793 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__);
795 iobase = self->io.sir_base;
797 iir = inb(iobase+UART_IIR) & UART_IIR_ID;
799 /* Clear interrupt */
800 lsr = inb(iobase+UART_LSR);
802 IRDA_DEBUG(4, "%s(), iir=%02x, lsr=%02x, iobase=%#x\n", __FUNCTION__,
808 IRDA_DEBUG(2, "%s(), RLSI\n", __FUNCTION__);
811 /* Receive interrupt */
812 ali_ircc_sir_receive(self);
815 if (lsr & UART_LSR_THRE)
817 /* Transmitter ready for data */
818 ali_ircc_sir_write_wakeup(self);
822 IRDA_DEBUG(0, "%s(), unhandled IIR=%#x\n", __FUNCTION__, iir);
829 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__);
831 return IRQ_RETVAL(iir);
836 * Function ali_ircc_sir_receive (self)
838 * Receive one frame from the infrared port
841 static void ali_ircc_sir_receive(struct ali_ircc_cb *self)
846 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__);
847 ASSERT(self != NULL, return;);
849 iobase = self->io.sir_base;
852 * Receive all characters in Rx FIFO, unwrap and unstuff them.
853 * async_unwrap_char will deliver all found frames
856 async_unwrap_char(self->netdev, &self->stats, &self->rx_buff,
857 inb(iobase+UART_RX));
859 /* Make sure we don't stay here too long */
860 if (boguscount++ > 32) {
861 IRDA_DEBUG(2,"%s(), breaking!\n", __FUNCTION__);
864 } while (inb(iobase+UART_LSR) & UART_LSR_DR);
866 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
870 * Function ali_ircc_sir_write_wakeup (tty)
872 * Called by the driver when there's room for more data. If we have
873 * more packets to send, we send them here.
876 static void ali_ircc_sir_write_wakeup(struct ali_ircc_cb *self)
881 ASSERT(self != NULL, return;);
883 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
885 iobase = self->io.sir_base;
887 /* Finished with frame? */
888 if (self->tx_buff.len > 0)
890 /* Write data left in transmit buffer */
891 actual = ali_ircc_sir_write(iobase, self->io.fifo_size,
892 self->tx_buff.data, self->tx_buff.len);
893 self->tx_buff.data += actual;
894 self->tx_buff.len -= actual;
900 /* We must wait until all data are gone */
901 while(!(inb(iobase+UART_LSR) & UART_LSR_TEMT))
902 IRDA_DEBUG(1, "%s(), UART_LSR_THRE\n", __FUNCTION__ );
904 IRDA_DEBUG(1, "%s(), Changing speed! self->new_speed = %d\n", __FUNCTION__ , self->new_speed);
905 ali_ircc_change_speed(self, self->new_speed);
908 // benjamin 2000/11/10 06:32PM
909 if (self->io.speed > 115200)
911 IRDA_DEBUG(2, "%s(), ali_ircc_change_speed from UART_LSR_TEMT \n", __FUNCTION__ );
914 // SetCOMInterrupts(self, TRUE);
920 netif_wake_queue(self->netdev);
923 self->stats.tx_packets++;
925 /* Turn on receive interrupts */
926 outb(UART_IER_RDI, iobase+UART_IER);
929 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
932 static void ali_ircc_change_speed(struct ali_ircc_cb *self, __u32 baud)
934 struct net_device *dev = self->netdev;
937 IRDA_DEBUG(1, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
939 IRDA_DEBUG(2, "%s(), setting speed = %d \n", __FUNCTION__ , baud);
941 /* This function *must* be called with irq off and spin-lock.
944 iobase = self->io.fir_base;
946 SetCOMInterrupts(self, FALSE); // 2000/11/24 11:43AM
948 /* Go to MIR, FIR Speed */
953 ali_ircc_fir_change_speed(self, baud);
955 /* Install FIR xmit handler*/
956 dev->hard_start_xmit = ali_ircc_fir_hard_xmit;
958 /* Enable Interuupt */
959 self->ier = IER_EOM; // benjamin 2000/11/20 07:24PM
961 /* Be ready for incomming frames */
962 ali_ircc_dma_receive(self); // benajmin 2000/11/8 07:46PM not complete
964 /* Go to SIR Speed */
967 ali_ircc_sir_change_speed(self, baud);
969 /* Install SIR xmit handler*/
970 dev->hard_start_xmit = ali_ircc_sir_hard_xmit;
974 SetCOMInterrupts(self, TRUE); // 2000/11/24 11:43AM
976 netif_wake_queue(self->netdev);
978 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
981 static void ali_ircc_fir_change_speed(struct ali_ircc_cb *priv, __u32 baud)
985 struct ali_ircc_cb *self = (struct ali_ircc_cb *) priv;
986 struct net_device *dev;
988 IRDA_DEBUG(1, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
990 ASSERT(self != NULL, return;);
993 iobase = self->io.fir_base;
995 IRDA_DEBUG(1, "%s(), self->io.speed = %d, change to speed = %d\n", __FUNCTION__ ,self->io.speed,baud);
997 /* Come from SIR speed */
998 if(self->io.speed <=115200)
1003 /* Update accounting for new speed */
1004 self->io.speed = baud;
1006 // Set Dongle Speed mode
1007 ali_ircc_change_dongle_speed(self, baud);
1009 IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
1013 * Function ali_sir_change_speed (self, speed)
1015 * Set speed of IrDA port to specified baudrate
1018 static void ali_ircc_sir_change_speed(struct ali_ircc_cb *priv, __u32 speed)
1020 struct ali_ircc_cb *self = (struct ali_ircc_cb *) priv;
1021 unsigned long flags;
1023 int fcr; /* FIFO control reg */
1024 int lcr; /* Line control reg */
1027 IRDA_DEBUG(1, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
1029 IRDA_DEBUG(1, "%s(), Setting speed to: %d\n", __FUNCTION__ , speed);
1031 ASSERT(self != NULL, return;);
1033 iobase = self->io.sir_base;
1035 /* Come from MIR or FIR speed */
1036 if(self->io.speed >115200)
1038 // Set Dongle Speed mode first
1039 ali_ircc_change_dongle_speed(self, speed);
1044 // Clear Line and Auxiluary status registers 2000/11/24 11:47AM
1046 inb(iobase+UART_LSR);
1047 inb(iobase+UART_SCR);
1049 /* Update accounting for new speed */
1050 self->io.speed = speed;
1052 spin_lock_irqsave(&self->lock, flags);
1054 divisor = 115200/speed;
1056 fcr = UART_FCR_ENABLE_FIFO;
1059 * Use trigger level 1 to avoid 3 ms. timeout delay at 9600 bps, and
1060 * almost 1,7 ms at 19200 bps. At speeds above that we can just forget
1061 * about this timeout since it will always be fast enough.
1063 if (self->io.speed < 38400)
1064 fcr |= UART_FCR_TRIGGER_1;
1066 fcr |= UART_FCR_TRIGGER_14;
1068 /* IrDA ports use 8N1 */
1069 lcr = UART_LCR_WLEN8;
1071 outb(UART_LCR_DLAB | lcr, iobase+UART_LCR); /* Set DLAB */
1072 outb(divisor & 0xff, iobase+UART_DLL); /* Set speed */
1073 outb(divisor >> 8, iobase+UART_DLM);
1074 outb(lcr, iobase+UART_LCR); /* Set 8N1 */
1075 outb(fcr, iobase+UART_FCR); /* Enable FIFO's */
1077 /* without this, the conection will be broken after come back from FIR speed,
1078 but with this, the SIR connection is harder to established */
1079 outb((UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2), iobase+UART_MCR);
1081 spin_unlock_irqrestore(&self->lock, flags);
1083 IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
1086 static void ali_ircc_change_dongle_speed(struct ali_ircc_cb *priv, int speed)
1089 struct ali_ircc_cb *self = (struct ali_ircc_cb *) priv;
1090 int iobase,dongle_id;
1093 IRDA_DEBUG(1, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
1095 iobase = self->io.fir_base; /* or iobase = self->io.sir_base; */
1096 dongle_id = self->io.dongle_id;
1098 /* We are already locked, no need to do it again */
1100 IRDA_DEBUG(1, "%s(), Set Speed for %s , Speed = %d\n", __FUNCTION__ , dongle_types[dongle_id], speed);
1102 switch_bank(iobase, BANK2);
1103 tmp = inb(iobase+FIR_IRDA_CR);
1105 /* IBM type dongle */
1108 if(speed == 4000000)
1111 // SD/MODE __| |__ __
1116 tmp &= ~IRDA_CR_HDLC; // HDLC=0
1117 tmp |= IRDA_CR_CRC; // CRC=1
1119 switch_bank(iobase, BANK2);
1120 outb(tmp, iobase+FIR_IRDA_CR);
1122 // T1 -> SD/MODE:0 IRTX:0
1125 outb(tmp, iobase+FIR_IRDA_CR);
1128 // T2 -> SD/MODE:1 IRTX:0
1131 outb(tmp, iobase+FIR_IRDA_CR);
1134 // T3 -> SD/MODE:1 IRTX:1
1136 outb(tmp, iobase+FIR_IRDA_CR);
1139 // T4 -> SD/MODE:0 IRTX:1
1142 outb(tmp, iobase+FIR_IRDA_CR);
1145 // T5 -> SD/MODE:0 IRTX:0
1148 outb(tmp, iobase+FIR_IRDA_CR);
1151 // reset -> Normal TX output Signal
1152 outb(tmp & ~0x02, iobase+FIR_IRDA_CR);
1154 else /* speed <=1152000 */
1162 /* MIR 115200, 57600 */
1165 tmp |= 0xA0; //HDLC=1, 1.152Mbps=1
1169 tmp &=~0x80; //HDLC 0.576Mbps
1170 tmp |= 0x20; //HDLC=1,
1173 tmp |= IRDA_CR_CRC; // CRC=1
1175 switch_bank(iobase, BANK2);
1176 outb(tmp, iobase+FIR_IRDA_CR);
1178 /* MIR 115200, 57600 */
1180 //switch_bank(iobase, BANK2);
1181 // T1 -> SD/MODE:0 IRTX:0
1184 outb(tmp, iobase+FIR_IRDA_CR);
1187 // T2 -> SD/MODE:1 IRTX:0
1190 outb(tmp, iobase+FIR_IRDA_CR);
1192 // T3 -> SD/MODE:0 IRTX:0
1195 outb(tmp, iobase+FIR_IRDA_CR);
1198 // reset -> Normal TX output Signal
1199 outb(tmp & ~0x02, iobase+FIR_IRDA_CR);
1202 else if (dongle_id == 1) /* HP HDSL-3600 */
1207 tmp &= ~IRDA_CR_HDLC; // HDLC=0
1211 tmp |= 0xA0; // HDLC=1, 1.152Mbps=1
1215 tmp &=~0x80; // HDLC 0.576Mbps
1216 tmp |= 0x20; // HDLC=1,
1220 tmp |= IRDA_CR_CRC; // CRC=1
1222 switch_bank(iobase, BANK2);
1223 outb(tmp, iobase+FIR_IRDA_CR);
1225 else /* HP HDSL-1100 */
1227 if(speed <= 115200) /* SIR */
1230 tmp &= ~IRDA_CR_FIR_SIN; // HP sin select = 0
1232 switch_bank(iobase, BANK2);
1233 outb(tmp, iobase+FIR_IRDA_CR);
1241 tmp &= ~IRDA_CR_HDLC; // HDLC=0
1245 tmp |= 0xA0; // HDLC=1, 1.152Mbps=1
1249 tmp &=~0x80; // HDLC 0.576Mbps
1250 tmp |= 0x20; // HDLC=1,
1254 tmp |= IRDA_CR_CRC; // CRC=1
1255 tmp |= IRDA_CR_FIR_SIN; // HP sin select = 1
1257 switch_bank(iobase, BANK2);
1258 outb(tmp, iobase+FIR_IRDA_CR);
1262 switch_bank(iobase, BANK0);
1264 IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
1268 * Function ali_ircc_sir_write (driver)
1270 * Fill Tx FIFO with transmit data
1273 static int ali_ircc_sir_write(int iobase, int fifo_size, __u8 *buf, int len)
1277 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
1279 /* Tx FIFO should be empty! */
1280 if (!(inb(iobase+UART_LSR) & UART_LSR_THRE)) {
1281 IRDA_DEBUG(0, "%s(), failed, fifo not empty!\n", __FUNCTION__ );
1285 /* Fill FIFO with current frame */
1286 while ((fifo_size-- > 0) && (actual < len)) {
1287 /* Transmit next byte */
1288 outb(buf[actual], iobase+UART_TX);
1293 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
1298 * Function ali_ircc_net_open (dev)
1303 static int ali_ircc_net_open(struct net_device *dev)
1305 struct ali_ircc_cb *self;
1309 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
1311 ASSERT(dev != NULL, return -1;);
1313 self = (struct ali_ircc_cb *) dev->priv;
1315 ASSERT(self != NULL, return 0;);
1317 iobase = self->io.fir_base;
1319 /* Request IRQ and install Interrupt Handler */
1320 if (request_irq(self->io.irq, ali_ircc_interrupt, 0, dev->name, dev))
1322 WARNING("%s, unable to allocate irq=%d\n", driver_name,
1328 * Always allocate the DMA channel after the IRQ, and clean up on
1331 if (request_dma(self->io.dma, dev->name)) {
1332 WARNING("%s, unable to allocate dma=%d\n", driver_name,
1334 free_irq(self->io.irq, self);
1338 /* Turn on interrups */
1339 outb(UART_IER_RDI , iobase+UART_IER);
1341 /* Ready to play! */
1342 netif_start_queue(dev); //benjamin by irport
1344 /* Give self a hardware name */
1345 sprintf(hwname, "ALI-FIR @ 0x%03x", self->io.fir_base);
1348 * Open new IrLAP layer instance, now that everything should be
1349 * initialized properly
1351 self->irlap = irlap_open(dev, &self->qos, hwname);
1353 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
1359 * Function ali_ircc_net_close (dev)
1364 static int ali_ircc_net_close(struct net_device *dev)
1367 struct ali_ircc_cb *self;
1370 IRDA_DEBUG(4, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
1372 ASSERT(dev != NULL, return -1;);
1374 self = (struct ali_ircc_cb *) dev->priv;
1375 ASSERT(self != NULL, return 0;);
1378 netif_stop_queue(dev);
1380 /* Stop and remove instance of IrLAP */
1382 irlap_close(self->irlap);
1385 disable_dma(self->io.dma);
1387 /* Disable interrupts */
1388 SetCOMInterrupts(self, FALSE);
1390 free_irq(self->io.irq, dev);
1391 free_dma(self->io.dma);
1393 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
1399 * Function ali_ircc_fir_hard_xmit (skb, dev)
1401 * Transmit the frame
1404 static int ali_ircc_fir_hard_xmit(struct sk_buff *skb, struct net_device *dev)
1406 struct ali_ircc_cb *self;
1407 unsigned long flags;
1412 IRDA_DEBUG(1, "%s(), ---------------- Start -----------------\n", __FUNCTION__ );
1414 self = (struct ali_ircc_cb *) dev->priv;
1415 iobase = self->io.fir_base;
1417 netif_stop_queue(dev);
1419 /* Make sure tests *& speed change are atomic */
1420 spin_lock_irqsave(&self->lock, flags);
1422 /* Note : you should make sure that speed changes are not going
1423 * to corrupt any outgoing frame. Look at nsc-ircc for the gory
1424 * details - Jean II */
1426 /* Check if we need to change the speed */
1427 speed = irda_get_next_speed(skb);
1428 if ((speed != self->io.speed) && (speed != -1)) {
1429 /* Check for empty frame */
1431 ali_ircc_change_speed(self, speed);
1432 dev->trans_start = jiffies;
1433 spin_unlock_irqrestore(&self->lock, flags);
1437 self->new_speed = speed;
1440 /* Register and copy this frame to DMA memory */
1441 self->tx_fifo.queue[self->tx_fifo.free].start = self->tx_fifo.tail;
1442 self->tx_fifo.queue[self->tx_fifo.free].len = skb->len;
1443 self->tx_fifo.tail += skb->len;
1445 self->stats.tx_bytes += skb->len;
1447 memcpy(self->tx_fifo.queue[self->tx_fifo.free].start, skb->data,
1450 self->tx_fifo.len++;
1451 self->tx_fifo.free++;
1453 /* Start transmit only if there is currently no transmit going on */
1454 if (self->tx_fifo.len == 1)
1456 /* Check if we must wait the min turn time or not */
1457 mtt = irda_get_mtt(skb);
1461 /* Check how much time we have used already */
1462 do_gettimeofday(&self->now);
1464 diff = self->now.tv_usec - self->stamp.tv_usec;
1465 /* self->stamp is set from ali_ircc_dma_receive_complete() */
1467 IRDA_DEBUG(1, "%s(), ******* diff = %d ******* \n", __FUNCTION__ , diff);
1472 /* Check if the mtt is larger than the time we have
1473 * already used by all the protocol processing
1480 * Use timer if delay larger than 1000 us, and
1481 * use udelay for smaller values which should
1486 /* Adjust for timer resolution */
1487 mtt = (mtt+250) / 500; /* 4 discard, 5 get advanced, Let's round off */
1489 IRDA_DEBUG(1, "%s(), ************** mtt = %d ***********\n", __FUNCTION__ , mtt);
1492 if (mtt == 1) /* 500 us */
1494 switch_bank(iobase, BANK1);
1495 outb(TIMER_IIR_500, iobase+FIR_TIMER_IIR);
1497 else if (mtt == 2) /* 1 ms */
1499 switch_bank(iobase, BANK1);
1500 outb(TIMER_IIR_1ms, iobase+FIR_TIMER_IIR);
1502 else /* > 2ms -> 4ms */
1504 switch_bank(iobase, BANK1);
1505 outb(TIMER_IIR_2ms, iobase+FIR_TIMER_IIR);
1510 outb(inb(iobase+FIR_CR) | CR_TIMER_EN, iobase+FIR_CR);
1511 self->io.direction = IO_XMIT;
1513 /* Enable timer interrupt */
1514 self->ier = IER_TIMER;
1515 SetCOMInterrupts(self, TRUE);
1517 /* Timer will take care of the rest */
1522 } // if (if (mtt > diff)
1525 /* Enable EOM interrupt */
1526 self->ier = IER_EOM;
1527 SetCOMInterrupts(self, TRUE);
1529 /* Transmit frame */
1530 ali_ircc_dma_xmit(self);
1531 } // if (self->tx_fifo.len == 1)
1535 /* Not busy transmitting anymore if window is not full */
1536 if (self->tx_fifo.free < MAX_TX_WINDOW)
1537 netif_wake_queue(self->netdev);
1539 /* Restore bank register */
1540 switch_bank(iobase, BANK0);
1542 dev->trans_start = jiffies;
1543 spin_unlock_irqrestore(&self->lock, flags);
1546 IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
1551 static void ali_ircc_dma_xmit(struct ali_ircc_cb *self)
1554 unsigned char FIFO_OPTI, Hi, Lo;
1557 IRDA_DEBUG(1, "%s(), ---------------- Start -----------------\n", __FUNCTION__ );
1559 iobase = self->io.fir_base;
1561 /* FIFO threshold , this method comes from NDIS5 code */
1563 if(self->tx_fifo.queue[self->tx_fifo.ptr].len < TX_FIFO_Threshold)
1564 FIFO_OPTI = self->tx_fifo.queue[self->tx_fifo.ptr].len-1;
1566 FIFO_OPTI = TX_FIFO_Threshold;
1569 switch_bank(iobase, BANK1);
1570 outb(inb(iobase+FIR_CR) & ~CR_DMA_EN, iobase+FIR_CR);
1572 self->io.direction = IO_XMIT;
1574 irda_setup_dma(self->io.dma,
1575 self->tx_fifo.queue[self->tx_fifo.ptr].start,
1576 self->tx_fifo.queue[self->tx_fifo.ptr].len,
1580 switch_bank(iobase, BANK0);
1581 outb(LCR_A_FIFO_RESET, iobase+FIR_LCR_A);
1583 /* Set Tx FIFO threshold */
1584 if (self->fifo_opti_buf!=FIFO_OPTI)
1586 switch_bank(iobase, BANK1);
1587 outb(FIFO_OPTI, iobase+FIR_FIFO_TR) ;
1588 self->fifo_opti_buf=FIFO_OPTI;
1591 /* Set Tx DMA threshold */
1592 switch_bank(iobase, BANK1);
1593 outb(TX_DMA_Threshold, iobase+FIR_DMA_TR);
1595 /* Set max Tx frame size */
1596 Hi = (self->tx_fifo.queue[self->tx_fifo.ptr].len >> 8) & 0x0f;
1597 Lo = self->tx_fifo.queue[self->tx_fifo.ptr].len & 0xff;
1598 switch_bank(iobase, BANK2);
1599 outb(Hi, iobase+FIR_TX_DSR_HI);
1600 outb(Lo, iobase+FIR_TX_DSR_LO);
1602 /* Disable SIP , Disable Brick Wall (we don't support in TX mode), Change to TX mode */
1603 switch_bank(iobase, BANK0);
1604 tmp = inb(iobase+FIR_LCR_B);
1605 tmp &= ~0x20; // Disable SIP
1606 outb(((unsigned char)(tmp & 0x3f) | LCR_B_TX_MODE) & ~LCR_B_BW, iobase+FIR_LCR_B);
1607 IRDA_DEBUG(1, "%s(), ******* Change to TX mode: FIR_LCR_B = 0x%x ******* \n", __FUNCTION__ , inb(iobase+FIR_LCR_B));
1609 outb(0, iobase+FIR_LSR);
1611 /* Enable DMA and Burst Mode */
1612 switch_bank(iobase, BANK1);
1613 outb(inb(iobase+FIR_CR) | CR_DMA_EN | CR_DMA_BURST, iobase+FIR_CR);
1615 switch_bank(iobase, BANK0);
1617 IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
1620 static int ali_ircc_dma_xmit_complete(struct ali_ircc_cb *self)
1625 IRDA_DEBUG(1, "%s(), ---------------- Start -----------------\n", __FUNCTION__ );
1627 iobase = self->io.fir_base;
1630 switch_bank(iobase, BANK1);
1631 outb(inb(iobase+FIR_CR) & ~CR_DMA_EN, iobase+FIR_CR);
1633 /* Check for underrun! */
1634 switch_bank(iobase, BANK0);
1635 if((inb(iobase+FIR_LSR) & LSR_FRAME_ABORT) == LSR_FRAME_ABORT)
1638 ERROR("%s(), ********* LSR_FRAME_ABORT *********\n", __FUNCTION__);
1639 self->stats.tx_errors++;
1640 self->stats.tx_fifo_errors++;
1644 self->stats.tx_packets++;
1647 /* Check if we need to change the speed */
1648 if (self->new_speed)
1650 ali_ircc_change_speed(self, self->new_speed);
1651 self->new_speed = 0;
1654 /* Finished with this frame, so prepare for next */
1655 self->tx_fifo.ptr++;
1656 self->tx_fifo.len--;
1658 /* Any frames to be sent back-to-back? */
1659 if (self->tx_fifo.len)
1661 ali_ircc_dma_xmit(self);
1663 /* Not finished yet! */
1667 { /* Reset Tx FIFO info */
1668 self->tx_fifo.len = self->tx_fifo.ptr = self->tx_fifo.free = 0;
1669 self->tx_fifo.tail = self->tx_buff.head;
1672 /* Make sure we have room for more frames */
1673 if (self->tx_fifo.free < MAX_TX_WINDOW) {
1674 /* Not busy transmitting anymore */
1675 /* Tell the network layer, that we can accept more frames */
1676 netif_wake_queue(self->netdev);
1679 switch_bank(iobase, BANK0);
1681 IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
1686 * Function ali_ircc_dma_receive (self)
1688 * Get ready for receiving a frame. The device will initiate a DMA
1689 * if it starts to receive a frame.
1692 static int ali_ircc_dma_receive(struct ali_ircc_cb *self)
1696 IRDA_DEBUG(1, "%s(), ---------------- Start -----------------\n", __FUNCTION__ );
1698 iobase = self->io.fir_base;
1700 /* Reset Tx FIFO info */
1701 self->tx_fifo.len = self->tx_fifo.ptr = self->tx_fifo.free = 0;
1702 self->tx_fifo.tail = self->tx_buff.head;
1705 switch_bank(iobase, BANK1);
1706 outb(inb(iobase+FIR_CR) & ~CR_DMA_EN, iobase+FIR_CR);
1708 /* Reset Message Count */
1709 switch_bank(iobase, BANK0);
1710 outb(0x07, iobase+FIR_LSR);
1712 self->rcvFramesOverflow = FALSE;
1714 self->LineStatus = inb(iobase+FIR_LSR) ;
1716 /* Reset Rx FIFO info */
1717 self->io.direction = IO_RECV;
1718 self->rx_buff.data = self->rx_buff.head;
1721 // switch_bank(iobase, BANK0);
1722 outb(LCR_A_FIFO_RESET, iobase+FIR_LCR_A);
1724 self->st_fifo.len = self->st_fifo.pending_bytes = 0;
1725 self->st_fifo.tail = self->st_fifo.head = 0;
1727 irda_setup_dma(self->io.dma, self->rx_buff.data,
1728 self->rx_buff.truesize, DMA_RX_MODE);
1730 /* Set Receive Mode,Brick Wall */
1731 //switch_bank(iobase, BANK0);
1732 tmp = inb(iobase+FIR_LCR_B);
1733 outb((unsigned char)(tmp &0x3f) | LCR_B_RX_MODE | LCR_B_BW , iobase + FIR_LCR_B); // 2000/12/1 05:16PM
1734 IRDA_DEBUG(1, "%s(), *** Change To RX mode: FIR_LCR_B = 0x%x *** \n", __FUNCTION__ , inb(iobase+FIR_LCR_B));
1736 /* Set Rx Threshold */
1737 switch_bank(iobase, BANK1);
1738 outb(RX_FIFO_Threshold, iobase+FIR_FIFO_TR);
1739 outb(RX_DMA_Threshold, iobase+FIR_DMA_TR);
1741 /* Enable DMA and Burst Mode */
1742 // switch_bank(iobase, BANK1);
1743 outb(CR_DMA_EN | CR_DMA_BURST, iobase+FIR_CR);
1745 switch_bank(iobase, BANK0);
1746 IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
1750 static int ali_ircc_dma_receive_complete(struct ali_ircc_cb *self)
1752 struct st_fifo *st_fifo;
1753 struct sk_buff *skb;
1754 __u8 status, MessageCount;
1755 int len, i, iobase, val;
1757 IRDA_DEBUG(1, "%s(), ---------------- Start -----------------\n", __FUNCTION__ );
1759 st_fifo = &self->st_fifo;
1760 iobase = self->io.fir_base;
1762 switch_bank(iobase, BANK0);
1763 MessageCount = inb(iobase+ FIR_LSR)&0x07;
1765 if (MessageCount > 0)
1766 IRDA_DEBUG(0, "%s(), Messsage count = %d,\n", __FUNCTION__ , MessageCount);
1768 for (i=0; i<=MessageCount; i++)
1771 switch_bank(iobase, BANK0);
1772 status = inb(iobase+FIR_LSR);
1774 switch_bank(iobase, BANK2);
1775 len = inb(iobase+FIR_RX_DSR_HI) & 0x0f;
1777 len |= inb(iobase+FIR_RX_DSR_LO);
1779 IRDA_DEBUG(1, "%s(), RX Length = 0x%.2x,\n", __FUNCTION__ , len);
1780 IRDA_DEBUG(1, "%s(), RX Status = 0x%.2x,\n", __FUNCTION__ , status);
1782 if (st_fifo->tail >= MAX_RX_WINDOW) {
1783 IRDA_DEBUG(0, "%s(), window is full!\n", __FUNCTION__ );
1787 st_fifo->entries[st_fifo->tail].status = status;
1788 st_fifo->entries[st_fifo->tail].len = len;
1789 st_fifo->pending_bytes += len;
1794 for (i=0; i<=MessageCount; i++)
1796 /* Get first entry */
1797 status = st_fifo->entries[st_fifo->head].status;
1798 len = st_fifo->entries[st_fifo->head].len;
1799 st_fifo->pending_bytes -= len;
1803 /* Check for errors */
1804 if ((status & 0xd8) || self->rcvFramesOverflow || (len==0))
1806 IRDA_DEBUG(0,"%s(), ************* RX Errors ************ \n", __FUNCTION__ );
1809 self->stats.rx_errors++;
1811 self->rx_buff.data += len;
1813 if (status & LSR_FIFO_UR)
1815 self->stats.rx_frame_errors++;
1816 IRDA_DEBUG(0,"%s(), ************* FIFO Errors ************ \n", __FUNCTION__ );
1818 if (status & LSR_FRAME_ERROR)
1820 self->stats.rx_frame_errors++;
1821 IRDA_DEBUG(0,"%s(), ************* FRAME Errors ************ \n", __FUNCTION__ );
1824 if (status & LSR_CRC_ERROR)
1826 self->stats.rx_crc_errors++;
1827 IRDA_DEBUG(0,"%s(), ************* CRC Errors ************ \n", __FUNCTION__ );
1830 if(self->rcvFramesOverflow)
1832 self->stats.rx_frame_errors++;
1833 IRDA_DEBUG(0,"%s(), ************* Overran DMA buffer ************ \n", __FUNCTION__ );
1837 self->stats.rx_frame_errors++;
1838 IRDA_DEBUG(0,"%s(), ********** Receive Frame Size = 0 ********* \n", __FUNCTION__ );
1844 if (st_fifo->pending_bytes < 32)
1846 switch_bank(iobase, BANK0);
1847 val = inb(iobase+FIR_BSR);
1848 if ((val& BSR_FIFO_NOT_EMPTY)== 0x80)
1850 IRDA_DEBUG(0, "%s(), ************* BSR_FIFO_NOT_EMPTY ************ \n", __FUNCTION__ );
1852 /* Put this entry back in fifo */
1855 st_fifo->pending_bytes += len;
1856 st_fifo->entries[st_fifo->head].status = status;
1857 st_fifo->entries[st_fifo->head].len = len;
1860 * DMA not finished yet, so try again
1861 * later, set timer value, resolution
1865 switch_bank(iobase, BANK1);
1866 outb(TIMER_IIR_500, iobase+FIR_TIMER_IIR); // 2001/1/2 05:07PM
1869 outb(inb(iobase+FIR_CR) | CR_TIMER_EN, iobase+FIR_CR);
1871 return FALSE; /* I'll be back! */
1876 * Remember the time we received this frame, so we can
1877 * reduce the min turn time a bit since we will know
1878 * how much time we have used for protocol processing
1880 do_gettimeofday(&self->stamp);
1882 skb = dev_alloc_skb(len+1);
1885 WARNING("%s(), memory squeeze, "
1886 "dropping frame.\n", __FUNCTION__);
1887 self->stats.rx_dropped++;
1892 /* Make sure IP header gets aligned */
1893 skb_reserve(skb, 1);
1895 /* Copy frame without CRC, CRC is removed by hardware*/
1897 memcpy(skb->data, self->rx_buff.data, len);
1899 /* Move to next frame */
1900 self->rx_buff.data += len;
1901 self->stats.rx_bytes += len;
1902 self->stats.rx_packets++;
1904 skb->dev = self->netdev;
1905 skb->mac.raw = skb->data;
1906 skb->protocol = htons(ETH_P_IRDA);
1908 self->netdev->last_rx = jiffies;
1912 switch_bank(iobase, BANK0);
1914 IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
1921 * Function ali_ircc_sir_hard_xmit (skb, dev)
1923 * Transmit the frame!
1926 static int ali_ircc_sir_hard_xmit(struct sk_buff *skb, struct net_device *dev)
1928 struct ali_ircc_cb *self;
1929 unsigned long flags;
1933 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
1935 ASSERT(dev != NULL, return 0;);
1937 self = (struct ali_ircc_cb *) dev->priv;
1938 ASSERT(self != NULL, return 0;);
1940 iobase = self->io.sir_base;
1942 netif_stop_queue(dev);
1944 /* Make sure tests *& speed change are atomic */
1945 spin_lock_irqsave(&self->lock, flags);
1947 /* Note : you should make sure that speed changes are not going
1948 * to corrupt any outgoing frame. Look at nsc-ircc for the gory
1949 * details - Jean II */
1951 /* Check if we need to change the speed */
1952 speed = irda_get_next_speed(skb);
1953 if ((speed != self->io.speed) && (speed != -1)) {
1954 /* Check for empty frame */
1956 ali_ircc_change_speed(self, speed);
1957 dev->trans_start = jiffies;
1958 spin_unlock_irqrestore(&self->lock, flags);
1962 self->new_speed = speed;
1965 /* Init tx buffer */
1966 self->tx_buff.data = self->tx_buff.head;
1968 /* Copy skb to tx_buff while wrapping, stuffing and making CRC */
1969 self->tx_buff.len = async_wrap_skb(skb, self->tx_buff.data,
1970 self->tx_buff.truesize);
1972 self->stats.tx_bytes += self->tx_buff.len;
1974 /* Turn on transmit finished interrupt. Will fire immediately! */
1975 outb(UART_IER_THRI, iobase+UART_IER);
1977 dev->trans_start = jiffies;
1978 spin_unlock_irqrestore(&self->lock, flags);
1982 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
1989 * Function ali_ircc_net_ioctl (dev, rq, cmd)
1991 * Process IOCTL commands for this device
1994 static int ali_ircc_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1996 struct if_irda_req *irq = (struct if_irda_req *) rq;
1997 struct ali_ircc_cb *self;
1998 unsigned long flags;
2001 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
2003 ASSERT(dev != NULL, return -1;);
2007 ASSERT(self != NULL, return -1;);
2009 IRDA_DEBUG(2, "%s(), %s, (cmd=0x%X)\n", __FUNCTION__ , dev->name, cmd);
2012 case SIOCSBANDWIDTH: /* Set bandwidth */
2013 IRDA_DEBUG(1, "%s(), SIOCSBANDWIDTH\n", __FUNCTION__ );
2015 * This function will also be used by IrLAP to change the
2016 * speed, so we still must allow for speed change within
2017 * interrupt context.
2019 if (!in_interrupt() && !capable(CAP_NET_ADMIN))
2022 spin_lock_irqsave(&self->lock, flags);
2023 ali_ircc_change_speed(self, irq->ifr_baudrate);
2024 spin_unlock_irqrestore(&self->lock, flags);
2026 case SIOCSMEDIABUSY: /* Set media busy */
2027 IRDA_DEBUG(1, "%s(), SIOCSMEDIABUSY\n", __FUNCTION__ );
2028 if (!capable(CAP_NET_ADMIN))
2030 irda_device_set_media_busy(self->netdev, TRUE);
2032 case SIOCGRECEIVING: /* Check if we are receiving right now */
2033 IRDA_DEBUG(2, "%s(), SIOCGRECEIVING\n", __FUNCTION__ );
2034 /* This is protected */
2035 irq->ifr_receiving = ali_ircc_is_receiving(self);
2041 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
2047 * Function ali_ircc_is_receiving (self)
2049 * Return TRUE is we are currently receiving a frame
2052 static int ali_ircc_is_receiving(struct ali_ircc_cb *self)
2054 unsigned long flags;
2058 IRDA_DEBUG(2, "%s(), ---------------- Start -----------------\n", __FUNCTION__ );
2060 ASSERT(self != NULL, return FALSE;);
2062 spin_lock_irqsave(&self->lock, flags);
2064 if (self->io.speed > 115200)
2066 iobase = self->io.fir_base;
2068 switch_bank(iobase, BANK1);
2069 if((inb(iobase+FIR_FIFO_FR) & 0x3f) != 0)
2071 /* We are receiving something */
2072 IRDA_DEBUG(1, "%s(), We are receiving something\n", __FUNCTION__ );
2075 switch_bank(iobase, BANK0);
2079 status = (self->rx_buff.state != OUTSIDE_FRAME);
2082 spin_unlock_irqrestore(&self->lock, flags);
2084 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
2089 static struct net_device_stats *ali_ircc_net_get_stats(struct net_device *dev)
2091 struct ali_ircc_cb *self = (struct ali_ircc_cb *) dev->priv;
2093 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
2095 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
2097 return &self->stats;
2100 static void ali_ircc_suspend(struct ali_ircc_cb *self)
2102 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
2104 MESSAGE("%s, Suspending\n", driver_name);
2106 if (self->io.suspended)
2109 ali_ircc_net_close(self->netdev);
2111 self->io.suspended = 1;
2113 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
2116 static void ali_ircc_wakeup(struct ali_ircc_cb *self)
2118 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
2120 if (!self->io.suspended)
2123 ali_ircc_net_open(self->netdev);
2125 MESSAGE("%s, Waking up\n", driver_name);
2127 self->io.suspended = 0;
2129 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
2132 static int ali_ircc_pmproc(struct pm_dev *dev, pm_request_t rqst, void *data)
2134 struct ali_ircc_cb *self = (struct ali_ircc_cb*) dev->data;
2136 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
2141 ali_ircc_suspend(self);
2144 ali_ircc_wakeup(self);
2149 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
2155 /* ALi Chip Function */
2157 static void SetCOMInterrupts(struct ali_ircc_cb *self , unsigned char enable)
2160 unsigned char newMask;
2162 int iobase = self->io.fir_base; /* or sir_base */
2164 IRDA_DEBUG(2, "%s(), -------- Start -------- ( Enable = %d )\n", __FUNCTION__ , enable);
2166 /* Enable the interrupt which we wish to */
2168 if (self->io.direction == IO_XMIT)
2170 if (self->io.speed > 115200) /* FIR, MIR */
2172 newMask = self->ier;
2176 newMask = UART_IER_THRI | UART_IER_RDI;
2180 if (self->io.speed > 115200) /* FIR, MIR */
2182 newMask = self->ier;
2186 newMask = UART_IER_RDI;
2190 else /* Disable all the interrupts */
2196 //SIR and FIR has different registers
2197 if (self->io.speed > 115200)
2199 switch_bank(iobase, BANK0);
2200 outb(newMask, iobase+FIR_IER);
2203 outb(newMask, iobase+UART_IER);
2205 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
2208 static void SIR2FIR(int iobase)
2210 //unsigned char tmp;
2212 IRDA_DEBUG(1, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
2214 /* Already protected (change_speed() or setup()), no need to lock.
2217 outb(0x28, iobase+UART_MCR);
2218 outb(0x68, iobase+UART_MCR);
2219 outb(0x88, iobase+UART_MCR);
2221 outb(0x60, iobase+FIR_MCR); /* Master Reset */
2222 outb(0x20, iobase+FIR_MCR); /* Master Interrupt Enable */
2224 //tmp = inb(iobase+FIR_LCR_B); /* SIP enable */
2226 //outb(tmp, iobase+FIR_LCR_B);
2228 IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
2231 static void FIR2SIR(int iobase)
2235 IRDA_DEBUG(1, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
2237 /* Already protected (change_speed() or setup()), no need to lock.
2240 outb(0x20, iobase+FIR_MCR); /* IRQ to low */
2241 outb(0x00, iobase+UART_IER);
2243 outb(0xA0, iobase+FIR_MCR); /* Don't set master reset */
2244 outb(0x00, iobase+UART_FCR);
2245 outb(0x07, iobase+UART_FCR);
2247 val = inb(iobase+UART_RX);
2248 val = inb(iobase+UART_LSR);
2249 val = inb(iobase+UART_MSR);
2251 IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
2254 MODULE_AUTHOR("Benjamin Kong <benjamin_kong@ali.com.tw>");
2255 MODULE_DESCRIPTION("ALi FIR Controller Driver");
2256 MODULE_LICENSE("GPL");
2259 MODULE_PARM(io, "1-4i");
2260 MODULE_PARM_DESC(io, "Base I/O addresses");
2261 MODULE_PARM(irq, "1-4i");
2262 MODULE_PARM_DESC(irq, "IRQ lines");
2263 MODULE_PARM(dma, "1-4i");
2264 MODULE_PARM_DESC(dma, "DMA channels");
2266 module_init(ali_ircc_init);
2267 module_exit(ali_ircc_cleanup);