mm/zsmalloc: allocate exactly size of struct zs_pool
[linux/fpc-iii.git] / drivers / net / irda / ali-ircc.c
blob588680a72fa13319f4fa5d45e92ee3b2eacb1617
1 /*********************************************************************
2 *
3 * Filename: ali-ircc.h
4 * Version: 0.5
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>
15 * All Rights Reserved
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>
25 #include <linux/gfp.h>
27 #include <linux/kernel.h>
28 #include <linux/types.h>
29 #include <linux/skbuff.h>
30 #include <linux/netdevice.h>
31 #include <linux/ioport.h>
32 #include <linux/delay.h>
33 #include <linux/init.h>
34 #include <linux/interrupt.h>
35 #include <linux/rtnetlink.h>
36 #include <linux/serial_reg.h>
37 #include <linux/dma-mapping.h>
38 #include <linux/platform_device.h>
40 #include <asm/io.h>
41 #include <asm/dma.h>
42 #include <asm/byteorder.h>
44 #include <net/irda/wrapper.h>
45 #include <net/irda/irda.h>
46 #include <net/irda/irda_device.h>
48 #include "ali-ircc.h"
50 #define CHIP_IO_EXTENT 8
51 #define BROKEN_DONGLE_ID
53 #define ALI_IRCC_DRIVER_NAME "ali-ircc"
55 /* Power Management */
56 static int ali_ircc_suspend(struct platform_device *dev, pm_message_t state);
57 static int ali_ircc_resume(struct platform_device *dev);
59 static struct platform_driver ali_ircc_driver = {
60 .suspend = ali_ircc_suspend,
61 .resume = ali_ircc_resume,
62 .driver = {
63 .name = ALI_IRCC_DRIVER_NAME,
64 .owner = THIS_MODULE,
68 /* Module parameters */
69 static int qos_mtt_bits = 0x07; /* 1 ms or more */
71 /* Use BIOS settions by default, but user may supply module parameters */
72 static unsigned int io[] = { ~0, ~0, ~0, ~0 };
73 static unsigned int irq[] = { 0, 0, 0, 0 };
74 static unsigned int dma[] = { 0, 0, 0, 0 };
76 static int ali_ircc_probe_53(ali_chip_t *chip, chipio_t *info);
77 static int ali_ircc_init_43(ali_chip_t *chip, chipio_t *info);
78 static int ali_ircc_init_53(ali_chip_t *chip, chipio_t *info);
80 /* These are the currently known ALi south-bridge chipsets, the only one difference
81 * is that M1543C doesn't support HP HDSL-3600
83 static ali_chip_t chips[] =
85 { "M1543", { 0x3f0, 0x370 }, 0x51, 0x23, 0x20, 0x43, ali_ircc_probe_53, ali_ircc_init_43 },
86 { "M1535", { 0x3f0, 0x370 }, 0x51, 0x23, 0x20, 0x53, ali_ircc_probe_53, ali_ircc_init_53 },
87 { "M1563", { 0x3f0, 0x370 }, 0x51, 0x23, 0x20, 0x63, ali_ircc_probe_53, ali_ircc_init_53 },
88 { NULL }
91 /* Max 4 instances for now */
92 static struct ali_ircc_cb *dev_self[] = { NULL, NULL, NULL, NULL };
94 /* Dongle Types */
95 static char *dongle_types[] = {
96 "TFDS6000",
97 "HP HSDL-3600",
98 "HP HSDL-1100",
99 "No dongle connected",
102 /* Some prototypes */
103 static int ali_ircc_open(int i, chipio_t *info);
105 static int ali_ircc_close(struct ali_ircc_cb *self);
107 static int ali_ircc_setup(chipio_t *info);
108 static int ali_ircc_is_receiving(struct ali_ircc_cb *self);
109 static int ali_ircc_net_open(struct net_device *dev);
110 static int ali_ircc_net_close(struct net_device *dev);
111 static int ali_ircc_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
112 static void ali_ircc_change_speed(struct ali_ircc_cb *self, __u32 baud);
114 /* SIR function */
115 static netdev_tx_t ali_ircc_sir_hard_xmit(struct sk_buff *skb,
116 struct net_device *dev);
117 static irqreturn_t ali_ircc_sir_interrupt(struct ali_ircc_cb *self);
118 static void ali_ircc_sir_receive(struct ali_ircc_cb *self);
119 static void ali_ircc_sir_write_wakeup(struct ali_ircc_cb *self);
120 static int ali_ircc_sir_write(int iobase, int fifo_size, __u8 *buf, int len);
121 static void ali_ircc_sir_change_speed(struct ali_ircc_cb *priv, __u32 speed);
123 /* FIR function */
124 static netdev_tx_t ali_ircc_fir_hard_xmit(struct sk_buff *skb,
125 struct net_device *dev);
126 static void ali_ircc_fir_change_speed(struct ali_ircc_cb *priv, __u32 speed);
127 static irqreturn_t ali_ircc_fir_interrupt(struct ali_ircc_cb *self);
128 static int ali_ircc_dma_receive(struct ali_ircc_cb *self);
129 static int ali_ircc_dma_receive_complete(struct ali_ircc_cb *self);
130 static int ali_ircc_dma_xmit_complete(struct ali_ircc_cb *self);
131 static void ali_ircc_dma_xmit(struct ali_ircc_cb *self);
133 /* My Function */
134 static int ali_ircc_read_dongle_id (int i, chipio_t *info);
135 static void ali_ircc_change_dongle_speed(struct ali_ircc_cb *priv, int speed);
137 /* ALi chip function */
138 static void SIR2FIR(int iobase);
139 static void FIR2SIR(int iobase);
140 static void SetCOMInterrupts(struct ali_ircc_cb *self , unsigned char enable);
143 * Function ali_ircc_init ()
145 * Initialize chip. Find out whay kinds of chips we are dealing with
146 * and their configuration registers address
148 static int __init ali_ircc_init(void)
150 ali_chip_t *chip;
151 chipio_t info;
152 int ret;
153 int cfg, cfg_base;
154 int reg, revision;
155 int i = 0;
157 ret = platform_driver_register(&ali_ircc_driver);
158 if (ret) {
159 net_err_ratelimited("%s, Can't register driver!\n",
160 ALI_IRCC_DRIVER_NAME);
161 return ret;
164 ret = -ENODEV;
166 /* Probe for all the ALi chipsets we know about */
167 for (chip= chips; chip->name; chip++, i++)
169 pr_debug("%s(), Probing for %s ...\n", __func__, chip->name);
171 /* Try all config registers for this chip */
172 for (cfg=0; cfg<2; cfg++)
174 cfg_base = chip->cfg[cfg];
175 if (!cfg_base)
176 continue;
178 memset(&info, 0, sizeof(chipio_t));
179 info.cfg_base = cfg_base;
180 info.fir_base = io[i];
181 info.dma = dma[i];
182 info.irq = irq[i];
185 /* Enter Configuration */
186 outb(chip->entr1, cfg_base);
187 outb(chip->entr2, cfg_base);
189 /* Select Logical Device 5 Registers (UART2) */
190 outb(0x07, cfg_base);
191 outb(0x05, cfg_base+1);
193 /* Read Chip Identification Register */
194 outb(chip->cid_index, cfg_base);
195 reg = inb(cfg_base+1);
197 if (reg == chip->cid_value)
199 pr_debug("%s(), Chip found at 0x%03x\n",
200 __func__, cfg_base);
202 outb(0x1F, cfg_base);
203 revision = inb(cfg_base+1);
204 pr_debug("%s(), Found %s chip, revision=%d\n",
205 __func__, chip->name, revision);
208 * If the user supplies the base address, then
209 * we init the chip, if not we probe the values
210 * set by the BIOS
212 if (io[i] < 2000)
214 chip->init(chip, &info);
216 else
218 chip->probe(chip, &info);
221 if (ali_ircc_open(i, &info) == 0)
222 ret = 0;
223 i++;
225 else
227 pr_debug("%s(), No %s chip at 0x%03x\n",
228 __func__, chip->name, cfg_base);
230 /* Exit configuration */
231 outb(0xbb, cfg_base);
235 if (ret)
236 platform_driver_unregister(&ali_ircc_driver);
238 return ret;
242 * Function ali_ircc_cleanup ()
244 * Close all configured chips
247 static void __exit ali_ircc_cleanup(void)
249 int i;
251 for (i=0; i < ARRAY_SIZE(dev_self); i++) {
252 if (dev_self[i])
253 ali_ircc_close(dev_self[i]);
256 platform_driver_unregister(&ali_ircc_driver);
260 static const struct net_device_ops ali_ircc_sir_ops = {
261 .ndo_open = ali_ircc_net_open,
262 .ndo_stop = ali_ircc_net_close,
263 .ndo_start_xmit = ali_ircc_sir_hard_xmit,
264 .ndo_do_ioctl = ali_ircc_net_ioctl,
267 static const struct net_device_ops ali_ircc_fir_ops = {
268 .ndo_open = ali_ircc_net_open,
269 .ndo_stop = ali_ircc_net_close,
270 .ndo_start_xmit = ali_ircc_fir_hard_xmit,
271 .ndo_do_ioctl = ali_ircc_net_ioctl,
275 * Function ali_ircc_open (int i, chipio_t *inf)
277 * Open driver instance
280 static int ali_ircc_open(int i, chipio_t *info)
282 struct net_device *dev;
283 struct ali_ircc_cb *self;
284 int dongle_id;
285 int err;
287 if (i >= ARRAY_SIZE(dev_self)) {
288 net_err_ratelimited("%s(), maximum number of supported chips reached!\n",
289 __func__);
290 return -ENOMEM;
293 /* Set FIR FIFO and DMA Threshold */
294 if ((ali_ircc_setup(info)) == -1)
295 return -1;
297 dev = alloc_irdadev(sizeof(*self));
298 if (dev == NULL) {
299 net_err_ratelimited("%s(), can't allocate memory for control block!\n",
300 __func__);
301 return -ENOMEM;
304 self = netdev_priv(dev);
305 self->netdev = dev;
306 spin_lock_init(&self->lock);
308 /* Need to store self somewhere */
309 dev_self[i] = self;
310 self->index = i;
312 /* Initialize IO */
313 self->io.cfg_base = info->cfg_base; /* In ali_ircc_probe_53 assign */
314 self->io.fir_base = info->fir_base; /* info->sir_base = info->fir_base */
315 self->io.sir_base = info->sir_base; /* ALi SIR and FIR use the same address */
316 self->io.irq = info->irq;
317 self->io.fir_ext = CHIP_IO_EXTENT;
318 self->io.dma = info->dma;
319 self->io.fifo_size = 16; /* SIR: 16, FIR: 32 Benjamin 2000/11/1 */
321 /* Reserve the ioports that we need */
322 if (!request_region(self->io.fir_base, self->io.fir_ext,
323 ALI_IRCC_DRIVER_NAME)) {
324 net_warn_ratelimited("%s(), can't get iobase of 0x%03x\n",
325 __func__, self->io.fir_base);
326 err = -ENODEV;
327 goto err_out1;
330 /* Initialize QoS for this device */
331 irda_init_max_qos_capabilies(&self->qos);
333 /* The only value we must override it the baudrate */
334 self->qos.baud_rate.bits = IR_9600|IR_19200|IR_38400|IR_57600|
335 IR_115200|IR_576000|IR_1152000|(IR_4000000 << 8); // benjamin 2000/11/8 05:27PM
337 self->qos.min_turn_time.bits = qos_mtt_bits;
339 irda_qos_bits_to_value(&self->qos);
341 /* Max DMA buffer size needed = (data_size + 6) * (window_size) + 6; */
342 self->rx_buff.truesize = 14384;
343 self->tx_buff.truesize = 14384;
345 /* Allocate memory if needed */
346 self->rx_buff.head =
347 dma_zalloc_coherent(NULL, self->rx_buff.truesize,
348 &self->rx_buff_dma, GFP_KERNEL);
349 if (self->rx_buff.head == NULL) {
350 err = -ENOMEM;
351 goto err_out2;
354 self->tx_buff.head =
355 dma_zalloc_coherent(NULL, self->tx_buff.truesize,
356 &self->tx_buff_dma, GFP_KERNEL);
357 if (self->tx_buff.head == NULL) {
358 err = -ENOMEM;
359 goto err_out3;
362 self->rx_buff.in_frame = FALSE;
363 self->rx_buff.state = OUTSIDE_FRAME;
364 self->tx_buff.data = self->tx_buff.head;
365 self->rx_buff.data = self->rx_buff.head;
367 /* Reset Tx queue info */
368 self->tx_fifo.len = self->tx_fifo.ptr = self->tx_fifo.free = 0;
369 self->tx_fifo.tail = self->tx_buff.head;
371 /* Override the network functions we need to use */
372 dev->netdev_ops = &ali_ircc_sir_ops;
374 err = register_netdev(dev);
375 if (err) {
376 net_err_ratelimited("%s(), register_netdev() failed!\n",
377 __func__);
378 goto err_out4;
380 net_info_ratelimited("IrDA: Registered device %s\n", dev->name);
382 /* Check dongle id */
383 dongle_id = ali_ircc_read_dongle_id(i, info);
384 net_info_ratelimited("%s(), %s, Found dongle: %s\n",
385 __func__, ALI_IRCC_DRIVER_NAME,
386 dongle_types[dongle_id]);
388 self->io.dongle_id = dongle_id;
391 return 0;
393 err_out4:
394 dma_free_coherent(NULL, self->tx_buff.truesize,
395 self->tx_buff.head, self->tx_buff_dma);
396 err_out3:
397 dma_free_coherent(NULL, self->rx_buff.truesize,
398 self->rx_buff.head, self->rx_buff_dma);
399 err_out2:
400 release_region(self->io.fir_base, self->io.fir_ext);
401 err_out1:
402 dev_self[i] = NULL;
403 free_netdev(dev);
404 return err;
409 * Function ali_ircc_close (self)
411 * Close driver instance
414 static int __exit ali_ircc_close(struct ali_ircc_cb *self)
416 int iobase;
418 IRDA_ASSERT(self != NULL, return -1;);
420 iobase = self->io.fir_base;
422 /* Remove netdevice */
423 unregister_netdev(self->netdev);
425 /* Release the PORT that this driver is using */
426 pr_debug("%s(), Releasing Region %03x\n", __func__, self->io.fir_base);
427 release_region(self->io.fir_base, self->io.fir_ext);
429 if (self->tx_buff.head)
430 dma_free_coherent(NULL, self->tx_buff.truesize,
431 self->tx_buff.head, self->tx_buff_dma);
433 if (self->rx_buff.head)
434 dma_free_coherent(NULL, self->rx_buff.truesize,
435 self->rx_buff.head, self->rx_buff_dma);
437 dev_self[self->index] = NULL;
438 free_netdev(self->netdev);
441 return 0;
445 * Function ali_ircc_init_43 (chip, info)
447 * Initialize the ALi M1543 chip.
449 static int ali_ircc_init_43(ali_chip_t *chip, chipio_t *info)
451 /* All controller information like I/O address, DMA channel, IRQ
452 * are set by BIOS
455 return 0;
459 * Function ali_ircc_init_53 (chip, info)
461 * Initialize the ALi M1535 chip.
463 static int ali_ircc_init_53(ali_chip_t *chip, chipio_t *info)
465 /* All controller information like I/O address, DMA channel, IRQ
466 * are set by BIOS
469 return 0;
473 * Function ali_ircc_probe_53 (chip, info)
475 * Probes for the ALi M1535D or M1535
477 static int ali_ircc_probe_53(ali_chip_t *chip, chipio_t *info)
479 int cfg_base = info->cfg_base;
480 int hi, low, reg;
483 /* Enter Configuration */
484 outb(chip->entr1, cfg_base);
485 outb(chip->entr2, cfg_base);
487 /* Select Logical Device 5 Registers (UART2) */
488 outb(0x07, cfg_base);
489 outb(0x05, cfg_base+1);
491 /* Read address control register */
492 outb(0x60, cfg_base);
493 hi = inb(cfg_base+1);
494 outb(0x61, cfg_base);
495 low = inb(cfg_base+1);
496 info->fir_base = (hi<<8) + low;
498 info->sir_base = info->fir_base;
500 pr_debug("%s(), probing fir_base=0x%03x\n", __func__, info->fir_base);
502 /* Read IRQ control register */
503 outb(0x70, cfg_base);
504 reg = inb(cfg_base+1);
505 info->irq = reg & 0x0f;
506 pr_debug("%s(), probing irq=%d\n", __func__, info->irq);
508 /* Read DMA channel */
509 outb(0x74, cfg_base);
510 reg = inb(cfg_base+1);
511 info->dma = reg & 0x07;
513 if(info->dma == 0x04)
514 net_warn_ratelimited("%s(), No DMA channel assigned !\n",
515 __func__);
516 else
517 pr_debug("%s(), probing dma=%d\n", __func__, info->dma);
519 /* Read Enabled Status */
520 outb(0x30, cfg_base);
521 reg = inb(cfg_base+1);
522 info->enabled = (reg & 0x80) && (reg & 0x01);
523 pr_debug("%s(), probing enabled=%d\n", __func__, info->enabled);
525 /* Read Power Status */
526 outb(0x22, cfg_base);
527 reg = inb(cfg_base+1);
528 info->suspended = (reg & 0x20);
529 pr_debug("%s(), probing suspended=%d\n", __func__, info->suspended);
531 /* Exit configuration */
532 outb(0xbb, cfg_base);
535 return 0;
539 * Function ali_ircc_setup (info)
541 * Set FIR FIFO and DMA Threshold
542 * Returns non-negative on success.
545 static int ali_ircc_setup(chipio_t *info)
547 unsigned char tmp;
548 int version;
549 int iobase = info->fir_base;
552 /* Locking comments :
553 * Most operations here need to be protected. We are called before
554 * the device instance is created in ali_ircc_open(), therefore
555 * nobody can bother us - Jean II */
557 /* Switch to FIR space */
558 SIR2FIR(iobase);
560 /* Master Reset */
561 outb(0x40, iobase+FIR_MCR); // benjamin 2000/11/30 11:45AM
563 /* Read FIR ID Version Register */
564 switch_bank(iobase, BANK3);
565 version = inb(iobase+FIR_ID_VR);
567 /* Should be 0x00 in the M1535/M1535D */
568 if(version != 0x00)
570 net_err_ratelimited("%s, Wrong chip version %02x\n",
571 ALI_IRCC_DRIVER_NAME, version);
572 return -1;
575 /* Set FIR FIFO Threshold Register */
576 switch_bank(iobase, BANK1);
577 outb(RX_FIFO_Threshold, iobase+FIR_FIFO_TR);
579 /* Set FIR DMA Threshold Register */
580 outb(RX_DMA_Threshold, iobase+FIR_DMA_TR);
582 /* CRC enable */
583 switch_bank(iobase, BANK2);
584 outb(inb(iobase+FIR_IRDA_CR) | IRDA_CR_CRC, iobase+FIR_IRDA_CR);
586 /* NDIS driver set TX Length here BANK2 Alias 3, Alias4*/
588 /* Switch to Bank 0 */
589 switch_bank(iobase, BANK0);
591 tmp = inb(iobase+FIR_LCR_B);
592 tmp &=~0x20; // disable SIP
593 tmp |= 0x80; // these two steps make RX mode
594 tmp &= 0xbf;
595 outb(tmp, iobase+FIR_LCR_B);
597 /* Disable Interrupt */
598 outb(0x00, iobase+FIR_IER);
601 /* Switch to SIR space */
602 FIR2SIR(iobase);
604 net_info_ratelimited("%s, driver loaded (Benjamin Kong)\n",
605 ALI_IRCC_DRIVER_NAME);
607 /* Enable receive interrupts */
608 // outb(UART_IER_RDI, iobase+UART_IER); //benjamin 2000/11/23 01:25PM
609 // Turn on the interrupts in ali_ircc_net_open
612 return 0;
616 * Function ali_ircc_read_dongle_id (int index, info)
618 * Try to read dongle identification. This procedure needs to be executed
619 * once after power-on/reset. It also needs to be used whenever you suspect
620 * that the user may have plugged/unplugged the IrDA Dongle.
622 static int ali_ircc_read_dongle_id (int i, chipio_t *info)
624 int dongle_id, reg;
625 int cfg_base = info->cfg_base;
628 /* Enter Configuration */
629 outb(chips[i].entr1, cfg_base);
630 outb(chips[i].entr2, cfg_base);
632 /* Select Logical Device 5 Registers (UART2) */
633 outb(0x07, cfg_base);
634 outb(0x05, cfg_base+1);
636 /* Read Dongle ID */
637 outb(0xf0, cfg_base);
638 reg = inb(cfg_base+1);
639 dongle_id = ((reg>>6)&0x02) | ((reg>>5)&0x01);
640 pr_debug("%s(), probing dongle_id=%d, dongle_types=%s\n",
641 __func__, dongle_id, dongle_types[dongle_id]);
643 /* Exit configuration */
644 outb(0xbb, cfg_base);
647 return dongle_id;
651 * Function ali_ircc_interrupt (irq, dev_id, regs)
653 * An interrupt from the chip has arrived. Time to do some work
656 static irqreturn_t ali_ircc_interrupt(int irq, void *dev_id)
658 struct net_device *dev = dev_id;
659 struct ali_ircc_cb *self;
660 int ret;
663 self = netdev_priv(dev);
665 spin_lock(&self->lock);
667 /* Dispatch interrupt handler for the current speed */
668 if (self->io.speed > 115200)
669 ret = ali_ircc_fir_interrupt(self);
670 else
671 ret = ali_ircc_sir_interrupt(self);
673 spin_unlock(&self->lock);
675 return ret;
678 * Function ali_ircc_fir_interrupt(irq, struct ali_ircc_cb *self)
680 * Handle MIR/FIR interrupt
683 static irqreturn_t ali_ircc_fir_interrupt(struct ali_ircc_cb *self)
685 __u8 eir, OldMessageCount;
686 int iobase, tmp;
689 iobase = self->io.fir_base;
691 switch_bank(iobase, BANK0);
692 self->InterruptID = inb(iobase+FIR_IIR);
693 self->BusStatus = inb(iobase+FIR_BSR);
695 OldMessageCount = (self->LineStatus + 1) & 0x07;
696 self->LineStatus = inb(iobase+FIR_LSR);
697 //self->ier = inb(iobase+FIR_IER); 2000/12/1 04:32PM
698 eir = self->InterruptID & self->ier; /* Mask out the interesting ones */
700 pr_debug("%s(), self->InterruptID = %x\n", __func__, self->InterruptID);
701 pr_debug("%s(), self->LineStatus = %x\n", __func__, self->LineStatus);
702 pr_debug("%s(), self->ier = %x\n", __func__, self->ier);
703 pr_debug("%s(), eir = %x\n", __func__, eir);
705 /* Disable interrupts */
706 SetCOMInterrupts(self, FALSE);
708 /* Tx or Rx Interrupt */
710 if (eir & IIR_EOM)
712 if (self->io.direction == IO_XMIT) /* TX */
714 pr_debug("%s(), ******* IIR_EOM (Tx) *******\n",
715 __func__);
717 if(ali_ircc_dma_xmit_complete(self))
719 if (irda_device_txqueue_empty(self->netdev))
721 /* Prepare for receive */
722 ali_ircc_dma_receive(self);
723 self->ier = IER_EOM;
726 else
728 self->ier = IER_EOM;
732 else /* RX */
734 pr_debug("%s(), ******* IIR_EOM (Rx) *******\n",
735 __func__);
737 if(OldMessageCount > ((self->LineStatus+1) & 0x07))
739 self->rcvFramesOverflow = TRUE;
740 pr_debug("%s(), ******* self->rcvFramesOverflow = TRUE ********\n",
741 __func__);
744 if (ali_ircc_dma_receive_complete(self))
746 pr_debug("%s(), ******* receive complete ********\n",
747 __func__);
749 self->ier = IER_EOM;
751 else
753 pr_debug("%s(), ******* Not receive complete ********\n",
754 __func__);
756 self->ier = IER_EOM | IER_TIMER;
761 /* Timer Interrupt */
762 else if (eir & IIR_TIMER)
764 if(OldMessageCount > ((self->LineStatus+1) & 0x07))
766 self->rcvFramesOverflow = TRUE;
767 pr_debug("%s(), ******* self->rcvFramesOverflow = TRUE *******\n",
768 __func__);
770 /* Disable Timer */
771 switch_bank(iobase, BANK1);
772 tmp = inb(iobase+FIR_CR);
773 outb( tmp& ~CR_TIMER_EN, iobase+FIR_CR);
775 /* Check if this is a Tx timer interrupt */
776 if (self->io.direction == IO_XMIT)
778 ali_ircc_dma_xmit(self);
780 /* Interrupt on EOM */
781 self->ier = IER_EOM;
784 else /* Rx */
786 if(ali_ircc_dma_receive_complete(self))
788 self->ier = IER_EOM;
790 else
792 self->ier = IER_EOM | IER_TIMER;
797 /* Restore Interrupt */
798 SetCOMInterrupts(self, TRUE);
800 return IRQ_RETVAL(eir);
804 * Function ali_ircc_sir_interrupt (irq, self, eir)
806 * Handle SIR interrupt
809 static irqreturn_t ali_ircc_sir_interrupt(struct ali_ircc_cb *self)
811 int iobase;
812 int iir, lsr;
815 iobase = self->io.sir_base;
817 iir = inb(iobase+UART_IIR) & UART_IIR_ID;
818 if (iir) {
819 /* Clear interrupt */
820 lsr = inb(iobase+UART_LSR);
822 pr_debug("%s(), iir=%02x, lsr=%02x, iobase=%#x\n",
823 __func__, iir, lsr, iobase);
825 switch (iir)
827 case UART_IIR_RLSI:
828 pr_debug("%s(), RLSI\n", __func__);
829 break;
830 case UART_IIR_RDI:
831 /* Receive interrupt */
832 ali_ircc_sir_receive(self);
833 break;
834 case UART_IIR_THRI:
835 if (lsr & UART_LSR_THRE)
837 /* Transmitter ready for data */
838 ali_ircc_sir_write_wakeup(self);
840 break;
841 default:
842 pr_debug("%s(), unhandled IIR=%#x\n",
843 __func__, iir);
844 break;
850 return IRQ_RETVAL(iir);
855 * Function ali_ircc_sir_receive (self)
857 * Receive one frame from the infrared port
860 static void ali_ircc_sir_receive(struct ali_ircc_cb *self)
862 int boguscount = 0;
863 int iobase;
865 IRDA_ASSERT(self != NULL, return;);
867 iobase = self->io.sir_base;
870 * Receive all characters in Rx FIFO, unwrap and unstuff them.
871 * async_unwrap_char will deliver all found frames
873 do {
874 async_unwrap_char(self->netdev, &self->netdev->stats, &self->rx_buff,
875 inb(iobase+UART_RX));
877 /* Make sure we don't stay here too long */
878 if (boguscount++ > 32) {
879 pr_debug("%s(), breaking!\n", __func__);
880 break;
882 } while (inb(iobase+UART_LSR) & UART_LSR_DR);
887 * Function ali_ircc_sir_write_wakeup (tty)
889 * Called by the driver when there's room for more data. If we have
890 * more packets to send, we send them here.
893 static void ali_ircc_sir_write_wakeup(struct ali_ircc_cb *self)
895 int actual = 0;
896 int iobase;
898 IRDA_ASSERT(self != NULL, return;);
901 iobase = self->io.sir_base;
903 /* Finished with frame? */
904 if (self->tx_buff.len > 0)
906 /* Write data left in transmit buffer */
907 actual = ali_ircc_sir_write(iobase, self->io.fifo_size,
908 self->tx_buff.data, self->tx_buff.len);
909 self->tx_buff.data += actual;
910 self->tx_buff.len -= actual;
912 else
914 if (self->new_speed)
916 /* We must wait until all data are gone */
917 while(!(inb(iobase+UART_LSR) & UART_LSR_TEMT))
918 pr_debug("%s(), UART_LSR_THRE\n", __func__);
920 pr_debug("%s(), Changing speed! self->new_speed = %d\n",
921 __func__, self->new_speed);
922 ali_ircc_change_speed(self, self->new_speed);
923 self->new_speed = 0;
925 // benjamin 2000/11/10 06:32PM
926 if (self->io.speed > 115200)
928 pr_debug("%s(), ali_ircc_change_speed from UART_LSR_TEMT\n",
929 __func__);
931 self->ier = IER_EOM;
932 // SetCOMInterrupts(self, TRUE);
933 return;
936 else
938 netif_wake_queue(self->netdev);
941 self->netdev->stats.tx_packets++;
943 /* Turn on receive interrupts */
944 outb(UART_IER_RDI, iobase+UART_IER);
949 static void ali_ircc_change_speed(struct ali_ircc_cb *self, __u32 baud)
951 struct net_device *dev = self->netdev;
952 int iobase;
955 pr_debug("%s(), setting speed = %d\n", __func__, baud);
957 /* This function *must* be called with irq off and spin-lock.
958 * - Jean II */
960 iobase = self->io.fir_base;
962 SetCOMInterrupts(self, FALSE); // 2000/11/24 11:43AM
964 /* Go to MIR, FIR Speed */
965 if (baud > 115200)
969 ali_ircc_fir_change_speed(self, baud);
971 /* Install FIR xmit handler*/
972 dev->netdev_ops = &ali_ircc_fir_ops;
974 /* Enable Interuupt */
975 self->ier = IER_EOM; // benjamin 2000/11/20 07:24PM
977 /* Be ready for incoming frames */
978 ali_ircc_dma_receive(self); // benajmin 2000/11/8 07:46PM not complete
980 /* Go to SIR Speed */
981 else
983 ali_ircc_sir_change_speed(self, baud);
985 /* Install SIR xmit handler*/
986 dev->netdev_ops = &ali_ircc_sir_ops;
990 SetCOMInterrupts(self, TRUE); // 2000/11/24 11:43AM
992 netif_wake_queue(self->netdev);
996 static void ali_ircc_fir_change_speed(struct ali_ircc_cb *priv, __u32 baud)
999 int iobase;
1000 struct ali_ircc_cb *self = priv;
1001 struct net_device *dev;
1004 IRDA_ASSERT(self != NULL, return;);
1006 dev = self->netdev;
1007 iobase = self->io.fir_base;
1009 pr_debug("%s(), self->io.speed = %d, change to speed = %d\n",
1010 __func__, self->io.speed, baud);
1012 /* Come from SIR speed */
1013 if(self->io.speed <=115200)
1015 SIR2FIR(iobase);
1018 /* Update accounting for new speed */
1019 self->io.speed = baud;
1021 // Set Dongle Speed mode
1022 ali_ircc_change_dongle_speed(self, baud);
1027 * Function ali_sir_change_speed (self, speed)
1029 * Set speed of IrDA port to specified baudrate
1032 static void ali_ircc_sir_change_speed(struct ali_ircc_cb *priv, __u32 speed)
1034 struct ali_ircc_cb *self = priv;
1035 unsigned long flags;
1036 int iobase;
1037 int fcr; /* FIFO control reg */
1038 int lcr; /* Line control reg */
1039 int divisor;
1042 pr_debug("%s(), Setting speed to: %d\n", __func__, speed);
1044 IRDA_ASSERT(self != NULL, return;);
1046 iobase = self->io.sir_base;
1048 /* Come from MIR or FIR speed */
1049 if(self->io.speed >115200)
1051 // Set Dongle Speed mode first
1052 ali_ircc_change_dongle_speed(self, speed);
1054 FIR2SIR(iobase);
1057 // Clear Line and Auxiluary status registers 2000/11/24 11:47AM
1059 inb(iobase+UART_LSR);
1060 inb(iobase+UART_SCR);
1062 /* Update accounting for new speed */
1063 self->io.speed = speed;
1065 spin_lock_irqsave(&self->lock, flags);
1067 divisor = 115200/speed;
1069 fcr = UART_FCR_ENABLE_FIFO;
1072 * Use trigger level 1 to avoid 3 ms. timeout delay at 9600 bps, and
1073 * almost 1,7 ms at 19200 bps. At speeds above that we can just forget
1074 * about this timeout since it will always be fast enough.
1076 if (self->io.speed < 38400)
1077 fcr |= UART_FCR_TRIGGER_1;
1078 else
1079 fcr |= UART_FCR_TRIGGER_14;
1081 /* IrDA ports use 8N1 */
1082 lcr = UART_LCR_WLEN8;
1084 outb(UART_LCR_DLAB | lcr, iobase+UART_LCR); /* Set DLAB */
1085 outb(divisor & 0xff, iobase+UART_DLL); /* Set speed */
1086 outb(divisor >> 8, iobase+UART_DLM);
1087 outb(lcr, iobase+UART_LCR); /* Set 8N1 */
1088 outb(fcr, iobase+UART_FCR); /* Enable FIFO's */
1090 /* without this, the connection will be broken after come back from FIR speed,
1091 but with this, the SIR connection is harder to established */
1092 outb((UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2), iobase+UART_MCR);
1094 spin_unlock_irqrestore(&self->lock, flags);
1098 static void ali_ircc_change_dongle_speed(struct ali_ircc_cb *priv, int speed)
1101 struct ali_ircc_cb *self = priv;
1102 int iobase,dongle_id;
1103 int tmp = 0;
1106 iobase = self->io.fir_base; /* or iobase = self->io.sir_base; */
1107 dongle_id = self->io.dongle_id;
1109 /* We are already locked, no need to do it again */
1111 pr_debug("%s(), Set Speed for %s , Speed = %d\n",
1112 __func__, dongle_types[dongle_id], speed);
1114 switch_bank(iobase, BANK2);
1115 tmp = inb(iobase+FIR_IRDA_CR);
1117 /* IBM type dongle */
1118 if(dongle_id == 0)
1120 if(speed == 4000000)
1122 // __ __
1123 // SD/MODE __| |__ __
1124 // __ __
1125 // IRTX __ __| |__
1126 // T1 T2 T3 T4 T5
1128 tmp &= ~IRDA_CR_HDLC; // HDLC=0
1129 tmp |= IRDA_CR_CRC; // CRC=1
1131 switch_bank(iobase, BANK2);
1132 outb(tmp, iobase+FIR_IRDA_CR);
1134 // T1 -> SD/MODE:0 IRTX:0
1135 tmp &= ~0x09;
1136 tmp |= 0x02;
1137 outb(tmp, iobase+FIR_IRDA_CR);
1138 udelay(2);
1140 // T2 -> SD/MODE:1 IRTX:0
1141 tmp &= ~0x01;
1142 tmp |= 0x0a;
1143 outb(tmp, iobase+FIR_IRDA_CR);
1144 udelay(2);
1146 // T3 -> SD/MODE:1 IRTX:1
1147 tmp |= 0x0b;
1148 outb(tmp, iobase+FIR_IRDA_CR);
1149 udelay(2);
1151 // T4 -> SD/MODE:0 IRTX:1
1152 tmp &= ~0x08;
1153 tmp |= 0x03;
1154 outb(tmp, iobase+FIR_IRDA_CR);
1155 udelay(2);
1157 // T5 -> SD/MODE:0 IRTX:0
1158 tmp &= ~0x09;
1159 tmp |= 0x02;
1160 outb(tmp, iobase+FIR_IRDA_CR);
1161 udelay(2);
1163 // reset -> Normal TX output Signal
1164 outb(tmp & ~0x02, iobase+FIR_IRDA_CR);
1166 else /* speed <=1152000 */
1168 // __
1169 // SD/MODE __| |__
1171 // IRTX ________
1172 // T1 T2 T3
1174 /* MIR 115200, 57600 */
1175 if (speed==1152000)
1177 tmp |= 0xA0; //HDLC=1, 1.152Mbps=1
1179 else
1181 tmp &=~0x80; //HDLC 0.576Mbps
1182 tmp |= 0x20; //HDLC=1,
1185 tmp |= IRDA_CR_CRC; // CRC=1
1187 switch_bank(iobase, BANK2);
1188 outb(tmp, iobase+FIR_IRDA_CR);
1190 /* MIR 115200, 57600 */
1192 //switch_bank(iobase, BANK2);
1193 // T1 -> SD/MODE:0 IRTX:0
1194 tmp &= ~0x09;
1195 tmp |= 0x02;
1196 outb(tmp, iobase+FIR_IRDA_CR);
1197 udelay(2);
1199 // T2 -> SD/MODE:1 IRTX:0
1200 tmp &= ~0x01;
1201 tmp |= 0x0a;
1202 outb(tmp, iobase+FIR_IRDA_CR);
1204 // T3 -> SD/MODE:0 IRTX:0
1205 tmp &= ~0x09;
1206 tmp |= 0x02;
1207 outb(tmp, iobase+FIR_IRDA_CR);
1208 udelay(2);
1210 // reset -> Normal TX output Signal
1211 outb(tmp & ~0x02, iobase+FIR_IRDA_CR);
1214 else if (dongle_id == 1) /* HP HDSL-3600 */
1216 switch(speed)
1218 case 4000000:
1219 tmp &= ~IRDA_CR_HDLC; // HDLC=0
1220 break;
1222 case 1152000:
1223 tmp |= 0xA0; // HDLC=1, 1.152Mbps=1
1224 break;
1226 case 576000:
1227 tmp &=~0x80; // HDLC 0.576Mbps
1228 tmp |= 0x20; // HDLC=1,
1229 break;
1232 tmp |= IRDA_CR_CRC; // CRC=1
1234 switch_bank(iobase, BANK2);
1235 outb(tmp, iobase+FIR_IRDA_CR);
1237 else /* HP HDSL-1100 */
1239 if(speed <= 115200) /* SIR */
1242 tmp &= ~IRDA_CR_FIR_SIN; // HP sin select = 0
1244 switch_bank(iobase, BANK2);
1245 outb(tmp, iobase+FIR_IRDA_CR);
1247 else /* MIR FIR */
1250 switch(speed)
1252 case 4000000:
1253 tmp &= ~IRDA_CR_HDLC; // HDLC=0
1254 break;
1256 case 1152000:
1257 tmp |= 0xA0; // HDLC=1, 1.152Mbps=1
1258 break;
1260 case 576000:
1261 tmp &=~0x80; // HDLC 0.576Mbps
1262 tmp |= 0x20; // HDLC=1,
1263 break;
1266 tmp |= IRDA_CR_CRC; // CRC=1
1267 tmp |= IRDA_CR_FIR_SIN; // HP sin select = 1
1269 switch_bank(iobase, BANK2);
1270 outb(tmp, iobase+FIR_IRDA_CR);
1274 switch_bank(iobase, BANK0);
1279 * Function ali_ircc_sir_write (driver)
1281 * Fill Tx FIFO with transmit data
1284 static int ali_ircc_sir_write(int iobase, int fifo_size, __u8 *buf, int len)
1286 int actual = 0;
1289 /* Tx FIFO should be empty! */
1290 if (!(inb(iobase+UART_LSR) & UART_LSR_THRE)) {
1291 pr_debug("%s(), failed, fifo not empty!\n", __func__);
1292 return 0;
1295 /* Fill FIFO with current frame */
1296 while ((fifo_size-- > 0) && (actual < len)) {
1297 /* Transmit next byte */
1298 outb(buf[actual], iobase+UART_TX);
1300 actual++;
1303 return actual;
1307 * Function ali_ircc_net_open (dev)
1309 * Start the device
1312 static int ali_ircc_net_open(struct net_device *dev)
1314 struct ali_ircc_cb *self;
1315 int iobase;
1316 char hwname[32];
1319 IRDA_ASSERT(dev != NULL, return -1;);
1321 self = netdev_priv(dev);
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 net_warn_ratelimited("%s, unable to allocate irq=%d\n",
1331 ALI_IRCC_DRIVER_NAME, self->io.irq);
1332 return -EAGAIN;
1336 * Always allocate the DMA channel after the IRQ, and clean up on
1337 * failure.
1339 if (request_dma(self->io.dma, dev->name)) {
1340 net_warn_ratelimited("%s, unable to allocate dma=%d\n",
1341 ALI_IRCC_DRIVER_NAME, self->io.dma);
1342 free_irq(self->io.irq, dev);
1343 return -EAGAIN;
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);
1362 return 0;
1366 * Function ali_ircc_net_close (dev)
1368 * Stop the device
1371 static int ali_ircc_net_close(struct net_device *dev)
1374 struct ali_ircc_cb *self;
1375 //int iobase;
1378 IRDA_ASSERT(dev != NULL, return -1;);
1380 self = netdev_priv(dev);
1381 IRDA_ASSERT(self != NULL, return 0;);
1383 /* Stop device */
1384 netif_stop_queue(dev);
1386 /* Stop and remove instance of IrLAP */
1387 if (self->irlap)
1388 irlap_close(self->irlap);
1389 self->irlap = NULL;
1391 disable_dma(self->io.dma);
1393 /* Disable interrupts */
1394 SetCOMInterrupts(self, FALSE);
1396 free_irq(self->io.irq, dev);
1397 free_dma(self->io.dma);
1400 return 0;
1404 * Function ali_ircc_fir_hard_xmit (skb, dev)
1406 * Transmit the frame
1409 static netdev_tx_t ali_ircc_fir_hard_xmit(struct sk_buff *skb,
1410 struct net_device *dev)
1412 struct ali_ircc_cb *self;
1413 unsigned long flags;
1414 int iobase;
1415 __u32 speed;
1416 int mtt, diff;
1419 self = netdev_priv(dev);
1420 iobase = self->io.fir_base;
1422 netif_stop_queue(dev);
1424 /* Make sure tests *& speed change are atomic */
1425 spin_lock_irqsave(&self->lock, flags);
1427 /* Note : you should make sure that speed changes are not going
1428 * to corrupt any outgoing frame. Look at nsc-ircc for the gory
1429 * details - Jean II */
1431 /* Check if we need to change the speed */
1432 speed = irda_get_next_speed(skb);
1433 if ((speed != self->io.speed) && (speed != -1)) {
1434 /* Check for empty frame */
1435 if (!skb->len) {
1436 ali_ircc_change_speed(self, speed);
1437 dev->trans_start = jiffies;
1438 spin_unlock_irqrestore(&self->lock, flags);
1439 dev_kfree_skb(skb);
1440 return NETDEV_TX_OK;
1441 } else
1442 self->new_speed = speed;
1445 /* Register and copy this frame to DMA memory */
1446 self->tx_fifo.queue[self->tx_fifo.free].start = self->tx_fifo.tail;
1447 self->tx_fifo.queue[self->tx_fifo.free].len = skb->len;
1448 self->tx_fifo.tail += skb->len;
1450 dev->stats.tx_bytes += skb->len;
1452 skb_copy_from_linear_data(skb, self->tx_fifo.queue[self->tx_fifo.free].start,
1453 skb->len);
1454 self->tx_fifo.len++;
1455 self->tx_fifo.free++;
1457 /* Start transmit only if there is currently no transmit going on */
1458 if (self->tx_fifo.len == 1)
1460 /* Check if we must wait the min turn time or not */
1461 mtt = irda_get_mtt(skb);
1463 if (mtt)
1465 /* Check how much time we have used already */
1466 do_gettimeofday(&self->now);
1468 diff = self->now.tv_usec - self->stamp.tv_usec;
1469 /* self->stamp is set from ali_ircc_dma_receive_complete() */
1471 pr_debug("%s(), ******* diff = %d *******\n",
1472 __func__, diff);
1474 if (diff < 0)
1475 diff += 1000000;
1477 /* Check if the mtt is larger than the time we have
1478 * already used by all the protocol processing
1480 if (mtt > diff)
1482 mtt -= diff;
1485 * Use timer if delay larger than 1000 us, and
1486 * use udelay for smaller values which should
1487 * be acceptable
1489 if (mtt > 500)
1491 /* Adjust for timer resolution */
1492 mtt = (mtt+250) / 500; /* 4 discard, 5 get advanced, Let's round off */
1494 pr_debug("%s(), ************** mtt = %d ***********\n",
1495 __func__, mtt);
1497 /* Setup timer */
1498 if (mtt == 1) /* 500 us */
1500 switch_bank(iobase, BANK1);
1501 outb(TIMER_IIR_500, iobase+FIR_TIMER_IIR);
1503 else if (mtt == 2) /* 1 ms */
1505 switch_bank(iobase, BANK1);
1506 outb(TIMER_IIR_1ms, iobase+FIR_TIMER_IIR);
1508 else /* > 2ms -> 4ms */
1510 switch_bank(iobase, BANK1);
1511 outb(TIMER_IIR_2ms, iobase+FIR_TIMER_IIR);
1515 /* Start timer */
1516 outb(inb(iobase+FIR_CR) | CR_TIMER_EN, iobase+FIR_CR);
1517 self->io.direction = IO_XMIT;
1519 /* Enable timer interrupt */
1520 self->ier = IER_TIMER;
1521 SetCOMInterrupts(self, TRUE);
1523 /* Timer will take care of the rest */
1524 goto out;
1526 else
1527 udelay(mtt);
1528 } // if (if (mtt > diff)
1529 }// if (mtt)
1531 /* Enable EOM interrupt */
1532 self->ier = IER_EOM;
1533 SetCOMInterrupts(self, TRUE);
1535 /* Transmit frame */
1536 ali_ircc_dma_xmit(self);
1537 } // if (self->tx_fifo.len == 1)
1539 out:
1541 /* Not busy transmitting anymore if window is not full */
1542 if (self->tx_fifo.free < MAX_TX_WINDOW)
1543 netif_wake_queue(self->netdev);
1545 /* Restore bank register */
1546 switch_bank(iobase, BANK0);
1548 dev->trans_start = jiffies;
1549 spin_unlock_irqrestore(&self->lock, flags);
1550 dev_kfree_skb(skb);
1552 return NETDEV_TX_OK;
1556 static void ali_ircc_dma_xmit(struct ali_ircc_cb *self)
1558 int iobase, tmp;
1559 unsigned char FIFO_OPTI, Hi, Lo;
1563 iobase = self->io.fir_base;
1565 /* FIFO threshold , this method comes from NDIS5 code */
1567 if(self->tx_fifo.queue[self->tx_fifo.ptr].len < TX_FIFO_Threshold)
1568 FIFO_OPTI = self->tx_fifo.queue[self->tx_fifo.ptr].len-1;
1569 else
1570 FIFO_OPTI = TX_FIFO_Threshold;
1572 /* Disable DMA */
1573 switch_bank(iobase, BANK1);
1574 outb(inb(iobase+FIR_CR) & ~CR_DMA_EN, iobase+FIR_CR);
1576 self->io.direction = IO_XMIT;
1578 irda_setup_dma(self->io.dma,
1579 ((u8 *)self->tx_fifo.queue[self->tx_fifo.ptr].start -
1580 self->tx_buff.head) + self->tx_buff_dma,
1581 self->tx_fifo.queue[self->tx_fifo.ptr].len,
1582 DMA_TX_MODE);
1584 /* Reset Tx FIFO */
1585 switch_bank(iobase, BANK0);
1586 outb(LCR_A_FIFO_RESET, iobase+FIR_LCR_A);
1588 /* Set Tx FIFO threshold */
1589 if (self->fifo_opti_buf!=FIFO_OPTI)
1591 switch_bank(iobase, BANK1);
1592 outb(FIFO_OPTI, iobase+FIR_FIFO_TR) ;
1593 self->fifo_opti_buf=FIFO_OPTI;
1596 /* Set Tx DMA threshold */
1597 switch_bank(iobase, BANK1);
1598 outb(TX_DMA_Threshold, iobase+FIR_DMA_TR);
1600 /* Set max Tx frame size */
1601 Hi = (self->tx_fifo.queue[self->tx_fifo.ptr].len >> 8) & 0x0f;
1602 Lo = self->tx_fifo.queue[self->tx_fifo.ptr].len & 0xff;
1603 switch_bank(iobase, BANK2);
1604 outb(Hi, iobase+FIR_TX_DSR_HI);
1605 outb(Lo, iobase+FIR_TX_DSR_LO);
1607 /* Disable SIP , Disable Brick Wall (we don't support in TX mode), Change to TX mode */
1608 switch_bank(iobase, BANK0);
1609 tmp = inb(iobase+FIR_LCR_B);
1610 tmp &= ~0x20; // Disable SIP
1611 outb(((unsigned char)(tmp & 0x3f) | LCR_B_TX_MODE) & ~LCR_B_BW, iobase+FIR_LCR_B);
1612 pr_debug("%s(), *** Change to TX mode: FIR_LCR_B = 0x%x ***\n",
1613 __func__, inb(iobase + FIR_LCR_B));
1615 outb(0, iobase+FIR_LSR);
1617 /* Enable DMA and Burst Mode */
1618 switch_bank(iobase, BANK1);
1619 outb(inb(iobase+FIR_CR) | CR_DMA_EN | CR_DMA_BURST, iobase+FIR_CR);
1621 switch_bank(iobase, BANK0);
1625 static int ali_ircc_dma_xmit_complete(struct ali_ircc_cb *self)
1627 int iobase;
1628 int ret = TRUE;
1631 iobase = self->io.fir_base;
1633 /* Disable DMA */
1634 switch_bank(iobase, BANK1);
1635 outb(inb(iobase+FIR_CR) & ~CR_DMA_EN, iobase+FIR_CR);
1637 /* Check for underrun! */
1638 switch_bank(iobase, BANK0);
1639 if((inb(iobase+FIR_LSR) & LSR_FRAME_ABORT) == LSR_FRAME_ABORT)
1642 net_err_ratelimited("%s(), ********* LSR_FRAME_ABORT *********\n",
1643 __func__);
1644 self->netdev->stats.tx_errors++;
1645 self->netdev->stats.tx_fifo_errors++;
1647 else
1649 self->netdev->stats.tx_packets++;
1652 /* Check if we need to change the speed */
1653 if (self->new_speed)
1655 ali_ircc_change_speed(self, self->new_speed);
1656 self->new_speed = 0;
1659 /* Finished with this frame, so prepare for next */
1660 self->tx_fifo.ptr++;
1661 self->tx_fifo.len--;
1663 /* Any frames to be sent back-to-back? */
1664 if (self->tx_fifo.len)
1666 ali_ircc_dma_xmit(self);
1668 /* Not finished yet! */
1669 ret = FALSE;
1671 else
1672 { /* Reset Tx FIFO info */
1673 self->tx_fifo.len = self->tx_fifo.ptr = self->tx_fifo.free = 0;
1674 self->tx_fifo.tail = self->tx_buff.head;
1677 /* Make sure we have room for more frames */
1678 if (self->tx_fifo.free < MAX_TX_WINDOW) {
1679 /* Not busy transmitting anymore */
1680 /* Tell the network layer, that we can accept more frames */
1681 netif_wake_queue(self->netdev);
1684 switch_bank(iobase, BANK0);
1686 return ret;
1690 * Function ali_ircc_dma_receive (self)
1692 * Get ready for receiving a frame. The device will initiate a DMA
1693 * if it starts to receive a frame.
1696 static int ali_ircc_dma_receive(struct ali_ircc_cb *self)
1698 int iobase, tmp;
1701 iobase = self->io.fir_base;
1703 /* Reset Tx FIFO info */
1704 self->tx_fifo.len = self->tx_fifo.ptr = self->tx_fifo.free = 0;
1705 self->tx_fifo.tail = self->tx_buff.head;
1707 /* Disable DMA */
1708 switch_bank(iobase, BANK1);
1709 outb(inb(iobase+FIR_CR) & ~CR_DMA_EN, iobase+FIR_CR);
1711 /* Reset Message Count */
1712 switch_bank(iobase, BANK0);
1713 outb(0x07, iobase+FIR_LSR);
1715 self->rcvFramesOverflow = FALSE;
1717 self->LineStatus = inb(iobase+FIR_LSR) ;
1719 /* Reset Rx FIFO info */
1720 self->io.direction = IO_RECV;
1721 self->rx_buff.data = self->rx_buff.head;
1723 /* Reset Rx FIFO */
1724 // switch_bank(iobase, BANK0);
1725 outb(LCR_A_FIFO_RESET, iobase+FIR_LCR_A);
1727 self->st_fifo.len = self->st_fifo.pending_bytes = 0;
1728 self->st_fifo.tail = self->st_fifo.head = 0;
1730 irda_setup_dma(self->io.dma, self->rx_buff_dma, self->rx_buff.truesize,
1731 DMA_RX_MODE);
1733 /* Set Receive Mode,Brick Wall */
1734 //switch_bank(iobase, BANK0);
1735 tmp = inb(iobase+FIR_LCR_B);
1736 outb((unsigned char)(tmp &0x3f) | LCR_B_RX_MODE | LCR_B_BW , iobase + FIR_LCR_B); // 2000/12/1 05:16PM
1737 pr_debug("%s(), *** Change To RX mode: FIR_LCR_B = 0x%x ***\n",
1738 __func__, inb(iobase + FIR_LCR_B));
1740 /* Set Rx Threshold */
1741 switch_bank(iobase, BANK1);
1742 outb(RX_FIFO_Threshold, iobase+FIR_FIFO_TR);
1743 outb(RX_DMA_Threshold, iobase+FIR_DMA_TR);
1745 /* Enable DMA and Burst Mode */
1746 // switch_bank(iobase, BANK1);
1747 outb(CR_DMA_EN | CR_DMA_BURST, iobase+FIR_CR);
1749 switch_bank(iobase, BANK0);
1750 return 0;
1753 static int ali_ircc_dma_receive_complete(struct ali_ircc_cb *self)
1755 struct st_fifo *st_fifo;
1756 struct sk_buff *skb;
1757 __u8 status, MessageCount;
1758 int len, i, iobase, val;
1760 st_fifo = &self->st_fifo;
1761 iobase = self->io.fir_base;
1763 switch_bank(iobase, BANK0);
1764 MessageCount = inb(iobase+ FIR_LSR)&0x07;
1766 if (MessageCount > 0)
1767 pr_debug("%s(), Message count = %d\n", __func__, MessageCount);
1769 for (i=0; i<=MessageCount; i++)
1771 /* Bank 0 */
1772 switch_bank(iobase, BANK0);
1773 status = inb(iobase+FIR_LSR);
1775 switch_bank(iobase, BANK2);
1776 len = inb(iobase+FIR_RX_DSR_HI) & 0x0f;
1777 len = len << 8;
1778 len |= inb(iobase+FIR_RX_DSR_LO);
1780 pr_debug("%s(), RX Length = 0x%.2x,\n", __func__ , len);
1781 pr_debug("%s(), RX Status = 0x%.2x,\n", __func__ , status);
1783 if (st_fifo->tail >= MAX_RX_WINDOW) {
1784 pr_debug("%s(), window is full!\n", __func__);
1785 continue;
1788 st_fifo->entries[st_fifo->tail].status = status;
1789 st_fifo->entries[st_fifo->tail].len = len;
1790 st_fifo->pending_bytes += len;
1791 st_fifo->tail++;
1792 st_fifo->len++;
1795 for (i=0; i<=MessageCount; i++)
1797 /* Get first entry */
1798 status = st_fifo->entries[st_fifo->head].status;
1799 len = st_fifo->entries[st_fifo->head].len;
1800 st_fifo->pending_bytes -= len;
1801 st_fifo->head++;
1802 st_fifo->len--;
1804 /* Check for errors */
1805 if ((status & 0xd8) || self->rcvFramesOverflow || (len==0))
1807 pr_debug("%s(), ************* RX Errors ************\n",
1808 __func__);
1810 /* Skip frame */
1811 self->netdev->stats.rx_errors++;
1813 self->rx_buff.data += len;
1815 if (status & LSR_FIFO_UR)
1817 self->netdev->stats.rx_frame_errors++;
1818 pr_debug("%s(), ************* FIFO Errors ************\n",
1819 __func__);
1821 if (status & LSR_FRAME_ERROR)
1823 self->netdev->stats.rx_frame_errors++;
1824 pr_debug("%s(), ************* FRAME Errors ************\n",
1825 __func__);
1828 if (status & LSR_CRC_ERROR)
1830 self->netdev->stats.rx_crc_errors++;
1831 pr_debug("%s(), ************* CRC Errors ************\n",
1832 __func__);
1835 if(self->rcvFramesOverflow)
1837 self->netdev->stats.rx_frame_errors++;
1838 pr_debug("%s(), ************* Overran DMA buffer ************\n",
1839 __func__);
1841 if(len == 0)
1843 self->netdev->stats.rx_frame_errors++;
1844 pr_debug("%s(), ********** Receive Frame Size = 0 *********\n",
1845 __func__);
1848 else
1851 if (st_fifo->pending_bytes < 32)
1853 switch_bank(iobase, BANK0);
1854 val = inb(iobase+FIR_BSR);
1855 if ((val& BSR_FIFO_NOT_EMPTY)== 0x80)
1857 pr_debug("%s(), ************* BSR_FIFO_NOT_EMPTY ************\n",
1858 __func__);
1860 /* Put this entry back in fifo */
1861 st_fifo->head--;
1862 st_fifo->len++;
1863 st_fifo->pending_bytes += len;
1864 st_fifo->entries[st_fifo->head].status = status;
1865 st_fifo->entries[st_fifo->head].len = len;
1868 * DMA not finished yet, so try again
1869 * later, set timer value, resolution
1870 * 500 us
1873 switch_bank(iobase, BANK1);
1874 outb(TIMER_IIR_500, iobase+FIR_TIMER_IIR); // 2001/1/2 05:07PM
1876 /* Enable Timer */
1877 outb(inb(iobase+FIR_CR) | CR_TIMER_EN, iobase+FIR_CR);
1879 return FALSE; /* I'll be back! */
1884 * Remember the time we received this frame, so we can
1885 * reduce the min turn time a bit since we will know
1886 * how much time we have used for protocol processing
1888 do_gettimeofday(&self->stamp);
1890 skb = dev_alloc_skb(len+1);
1891 if (skb == NULL)
1893 self->netdev->stats.rx_dropped++;
1895 return FALSE;
1898 /* Make sure IP header gets aligned */
1899 skb_reserve(skb, 1);
1901 /* Copy frame without CRC, CRC is removed by hardware*/
1902 skb_put(skb, len);
1903 skb_copy_to_linear_data(skb, self->rx_buff.data, len);
1905 /* Move to next frame */
1906 self->rx_buff.data += len;
1907 self->netdev->stats.rx_bytes += len;
1908 self->netdev->stats.rx_packets++;
1910 skb->dev = self->netdev;
1911 skb_reset_mac_header(skb);
1912 skb->protocol = htons(ETH_P_IRDA);
1913 netif_rx(skb);
1917 switch_bank(iobase, BANK0);
1919 return TRUE;
1925 * Function ali_ircc_sir_hard_xmit (skb, dev)
1927 * Transmit the frame!
1930 static netdev_tx_t ali_ircc_sir_hard_xmit(struct sk_buff *skb,
1931 struct net_device *dev)
1933 struct ali_ircc_cb *self;
1934 unsigned long flags;
1935 int iobase;
1936 __u32 speed;
1939 IRDA_ASSERT(dev != NULL, return NETDEV_TX_OK;);
1941 self = netdev_priv(dev);
1942 IRDA_ASSERT(self != NULL, return NETDEV_TX_OK;);
1944 iobase = self->io.sir_base;
1946 netif_stop_queue(dev);
1948 /* Make sure tests *& speed change are atomic */
1949 spin_lock_irqsave(&self->lock, flags);
1951 /* Note : you should make sure that speed changes are not going
1952 * to corrupt any outgoing frame. Look at nsc-ircc for the gory
1953 * details - Jean II */
1955 /* Check if we need to change the speed */
1956 speed = irda_get_next_speed(skb);
1957 if ((speed != self->io.speed) && (speed != -1)) {
1958 /* Check for empty frame */
1959 if (!skb->len) {
1960 ali_ircc_change_speed(self, speed);
1961 dev->trans_start = jiffies;
1962 spin_unlock_irqrestore(&self->lock, flags);
1963 dev_kfree_skb(skb);
1964 return NETDEV_TX_OK;
1965 } else
1966 self->new_speed = speed;
1969 /* Init tx buffer */
1970 self->tx_buff.data = self->tx_buff.head;
1972 /* Copy skb to tx_buff while wrapping, stuffing and making CRC */
1973 self->tx_buff.len = async_wrap_skb(skb, self->tx_buff.data,
1974 self->tx_buff.truesize);
1976 self->netdev->stats.tx_bytes += self->tx_buff.len;
1978 /* Turn on transmit finished interrupt. Will fire immediately! */
1979 outb(UART_IER_THRI, iobase+UART_IER);
1981 dev->trans_start = jiffies;
1982 spin_unlock_irqrestore(&self->lock, flags);
1984 dev_kfree_skb(skb);
1987 return NETDEV_TX_OK;
1992 * Function ali_ircc_net_ioctl (dev, rq, cmd)
1994 * Process IOCTL commands for this device
1997 static int ali_ircc_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1999 struct if_irda_req *irq = (struct if_irda_req *) rq;
2000 struct ali_ircc_cb *self;
2001 unsigned long flags;
2002 int ret = 0;
2005 IRDA_ASSERT(dev != NULL, return -1;);
2007 self = netdev_priv(dev);
2009 IRDA_ASSERT(self != NULL, return -1;);
2011 pr_debug("%s(), %s, (cmd=0x%X)\n", __func__ , dev->name, cmd);
2013 switch (cmd) {
2014 case SIOCSBANDWIDTH: /* Set bandwidth */
2015 pr_debug("%s(), SIOCSBANDWIDTH\n", __func__);
2017 * This function will also be used by IrLAP to change the
2018 * speed, so we still must allow for speed change within
2019 * interrupt context.
2021 if (!in_interrupt() && !capable(CAP_NET_ADMIN))
2022 return -EPERM;
2024 spin_lock_irqsave(&self->lock, flags);
2025 ali_ircc_change_speed(self, irq->ifr_baudrate);
2026 spin_unlock_irqrestore(&self->lock, flags);
2027 break;
2028 case SIOCSMEDIABUSY: /* Set media busy */
2029 pr_debug("%s(), SIOCSMEDIABUSY\n", __func__);
2030 if (!capable(CAP_NET_ADMIN))
2031 return -EPERM;
2032 irda_device_set_media_busy(self->netdev, TRUE);
2033 break;
2034 case SIOCGRECEIVING: /* Check if we are receiving right now */
2035 pr_debug("%s(), SIOCGRECEIVING\n", __func__);
2036 /* This is protected */
2037 irq->ifr_receiving = ali_ircc_is_receiving(self);
2038 break;
2039 default:
2040 ret = -EOPNOTSUPP;
2044 return ret;
2048 * Function ali_ircc_is_receiving (self)
2050 * Return TRUE is we are currently receiving a frame
2053 static int ali_ircc_is_receiving(struct ali_ircc_cb *self)
2055 unsigned long flags;
2056 int status = FALSE;
2057 int iobase;
2060 IRDA_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 pr_debug("%s(), We are receiving something\n",
2073 __func__);
2074 status = TRUE;
2076 switch_bank(iobase, BANK0);
2078 else
2080 status = (self->rx_buff.state != OUTSIDE_FRAME);
2083 spin_unlock_irqrestore(&self->lock, flags);
2086 return status;
2089 static int ali_ircc_suspend(struct platform_device *dev, pm_message_t state)
2091 struct ali_ircc_cb *self = platform_get_drvdata(dev);
2093 net_info_ratelimited("%s, Suspending\n", ALI_IRCC_DRIVER_NAME);
2095 if (self->io.suspended)
2096 return 0;
2098 ali_ircc_net_close(self->netdev);
2100 self->io.suspended = 1;
2102 return 0;
2105 static int ali_ircc_resume(struct platform_device *dev)
2107 struct ali_ircc_cb *self = platform_get_drvdata(dev);
2109 if (!self->io.suspended)
2110 return 0;
2112 ali_ircc_net_open(self->netdev);
2114 net_info_ratelimited("%s, Waking up\n", ALI_IRCC_DRIVER_NAME);
2116 self->io.suspended = 0;
2118 return 0;
2121 /* ALi Chip Function */
2123 static void SetCOMInterrupts(struct ali_ircc_cb *self , unsigned char enable)
2126 unsigned char newMask;
2128 int iobase = self->io.fir_base; /* or sir_base */
2130 pr_debug("%s(), -------- Start -------- ( Enable = %d )\n",
2131 __func__, enable);
2133 /* Enable the interrupt which we wish to */
2134 if (enable){
2135 if (self->io.direction == IO_XMIT)
2137 if (self->io.speed > 115200) /* FIR, MIR */
2139 newMask = self->ier;
2141 else /* SIR */
2143 newMask = UART_IER_THRI | UART_IER_RDI;
2146 else {
2147 if (self->io.speed > 115200) /* FIR, MIR */
2149 newMask = self->ier;
2151 else /* SIR */
2153 newMask = UART_IER_RDI;
2157 else /* Disable all the interrupts */
2159 newMask = 0x00;
2163 //SIR and FIR has different registers
2164 if (self->io.speed > 115200)
2166 switch_bank(iobase, BANK0);
2167 outb(newMask, iobase+FIR_IER);
2169 else
2170 outb(newMask, iobase+UART_IER);
2174 static void SIR2FIR(int iobase)
2176 //unsigned char tmp;
2179 /* Already protected (change_speed() or setup()), no need to lock.
2180 * Jean II */
2182 outb(0x28, iobase+UART_MCR);
2183 outb(0x68, iobase+UART_MCR);
2184 outb(0x88, iobase+UART_MCR);
2186 outb(0x60, iobase+FIR_MCR); /* Master Reset */
2187 outb(0x20, iobase+FIR_MCR); /* Master Interrupt Enable */
2189 //tmp = inb(iobase+FIR_LCR_B); /* SIP enable */
2190 //tmp |= 0x20;
2191 //outb(tmp, iobase+FIR_LCR_B);
2195 static void FIR2SIR(int iobase)
2197 unsigned char val;
2200 /* Already protected (change_speed() or setup()), no need to lock.
2201 * Jean II */
2203 outb(0x20, iobase+FIR_MCR); /* IRQ to low */
2204 outb(0x00, iobase+UART_IER);
2206 outb(0xA0, iobase+FIR_MCR); /* Don't set master reset */
2207 outb(0x00, iobase+UART_FCR);
2208 outb(0x07, iobase+UART_FCR);
2210 val = inb(iobase+UART_RX);
2211 val = inb(iobase+UART_LSR);
2212 val = inb(iobase+UART_MSR);
2216 MODULE_AUTHOR("Benjamin Kong <benjamin_kong@ali.com.tw>");
2217 MODULE_DESCRIPTION("ALi FIR Controller Driver");
2218 MODULE_LICENSE("GPL");
2219 MODULE_ALIAS("platform:" ALI_IRCC_DRIVER_NAME);
2222 module_param_array(io, int, NULL, 0);
2223 MODULE_PARM_DESC(io, "Base I/O addresses");
2224 module_param_array(irq, int, NULL, 0);
2225 MODULE_PARM_DESC(irq, "IRQ lines");
2226 module_param_array(dma, int, NULL, 0);
2227 MODULE_PARM_DESC(dma, "DMA channels");
2229 module_init(ali_ircc_init);
2230 module_exit(ali_ircc_cleanup);