1 /********************************************************************
4 Description: Driver for the VIA VT8231/VT8233 IrDA chipsets
5 Author: VIA Technologies,inc
8 Copyright (c) 1998-2003 VIA Technologies, Inc.
10 This program is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free Software
12 Foundation; either version 2, or (at your option) any later version.
14 This program is distributed in the hope that it will be useful, but WITHOUT
15 ANY WARRANTIES OR REPRESENTATIONS; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17 See the GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License along with
20 this program; if not, see <http://www.gnu.org/licenses/>.
22 F01 Oct/02/02: Modify code for V0.11(move out back to back transfer)
23 F02 Oct/28/02: Add SB device ID for 3147 and 3177.
25 jul/09/2002 : only implement two kind of dongle currently.
26 Oct/02/2002 : work on VT8231 and VT8233 .
27 Aug/06/2003 : change driver format to pci driver .
30 - Removed unneeded 'legacy' pci stuff.
31 - Make sure SIR mode is set (hw_init()) before calling mode-dependent stuff.
32 - On speed change from core, don't send SIR frame with new speed.
33 Use current speed and change speeds later.
34 - Make module-param dongle_id actually work.
35 - New dongle_id 17 (0x11): TDFS4500. Single-ended SIR only.
36 Tested with home-grown PCB on EPIA boards.
39 ********************************************************************/
40 #include <linux/module.h>
41 #include <linux/kernel.h>
42 #include <linux/types.h>
43 #include <linux/skbuff.h>
44 #include <linux/netdevice.h>
45 #include <linux/ioport.h>
46 #include <linux/delay.h>
47 #include <linux/init.h>
48 #include <linux/interrupt.h>
49 #include <linux/rtnetlink.h>
50 #include <linux/pci.h>
51 #include <linux/dma-mapping.h>
52 #include <linux/gfp.h>
56 #include <asm/byteorder.h>
60 #include <net/irda/wrapper.h>
61 #include <net/irda/irda.h>
62 #include <net/irda/irda_device.h>
66 #define VIA_MODULE_NAME "via-ircc"
67 #define CHIP_IO_EXTENT 0x40
69 static char *driver_name = VIA_MODULE_NAME;
71 /* Module parameters */
72 static int qos_mtt_bits = 0x07; /* 1 ms or more */
73 static int dongle_id = 0; /* default: probe */
75 /* We can't guess the type of connected dongle, user *must* supply it. */
76 module_param(dongle_id, int, 0);
79 static int via_ircc_open(struct pci_dev *pdev, chipio_t *info,
81 static int via_ircc_dma_receive(struct via_ircc_cb *self);
82 static int via_ircc_dma_receive_complete(struct via_ircc_cb *self,
84 static netdev_tx_t via_ircc_hard_xmit_sir(struct sk_buff *skb,
85 struct net_device *dev);
86 static netdev_tx_t via_ircc_hard_xmit_fir(struct sk_buff *skb,
87 struct net_device *dev);
88 static void via_hw_init(struct via_ircc_cb *self);
89 static void via_ircc_change_speed(struct via_ircc_cb *self, __u32 baud);
90 static irqreturn_t via_ircc_interrupt(int irq, void *dev_id);
91 static int via_ircc_is_receiving(struct via_ircc_cb *self);
92 static int via_ircc_read_dongle_id(int iobase);
94 static int via_ircc_net_open(struct net_device *dev);
95 static int via_ircc_net_close(struct net_device *dev);
96 static int via_ircc_net_ioctl(struct net_device *dev, struct ifreq *rq,
98 static void via_ircc_change_dongle_speed(int iobase, int speed,
100 static int RxTimerHandler(struct via_ircc_cb *self, int iobase);
101 static void hwreset(struct via_ircc_cb *self);
102 static int via_ircc_dma_xmit(struct via_ircc_cb *self, u16 iobase);
103 static int upload_rxdata(struct via_ircc_cb *self, int iobase);
104 static int via_init_one(struct pci_dev *pcidev, const struct pci_device_id *id);
105 static void via_remove_one(struct pci_dev *pdev);
107 /* FIXME : Should use udelay() instead, even if we are x86 only - Jean II */
108 static void iodelay(int udelay)
113 for (i = 0; i < udelay; i++) {
118 static const struct pci_device_id via_pci_tbl[] = {
119 { PCI_VENDOR_ID_VIA, 0x8231, PCI_ANY_ID, PCI_ANY_ID,0,0,0 },
120 { PCI_VENDOR_ID_VIA, 0x3109, PCI_ANY_ID, PCI_ANY_ID,0,0,1 },
121 { PCI_VENDOR_ID_VIA, 0x3074, PCI_ANY_ID, PCI_ANY_ID,0,0,2 },
122 { PCI_VENDOR_ID_VIA, 0x3147, PCI_ANY_ID, PCI_ANY_ID,0,0,3 },
123 { PCI_VENDOR_ID_VIA, 0x3177, PCI_ANY_ID, PCI_ANY_ID,0,0,4 },
127 MODULE_DEVICE_TABLE(pci,via_pci_tbl);
130 static struct pci_driver via_driver = {
131 .name = VIA_MODULE_NAME,
132 .id_table = via_pci_tbl,
133 .probe = via_init_one,
134 .remove = via_remove_one,
139 * Function via_ircc_init ()
141 * Initialize chip. Just find out chip type and resource.
143 static int __init via_ircc_init(void)
147 rc = pci_register_driver(&via_driver);
149 pr_debug("%s(): error rc = %d, returning -ENODEV...\n",
156 static int via_init_one(struct pci_dev *pcidev, const struct pci_device_id *id)
159 u8 temp,oldPCI_40,oldPCI_44,bTmp,bTmp1;
160 u16 Chipset,FirDRQ1,FirDRQ0,FirIRQ,FirIOBase;
163 pr_debug("%s(): Device ID=(0X%X)\n", __func__, id->device);
165 rc = pci_enable_device (pcidev);
167 pr_debug("%s(): error rc = %d\n", __func__, rc);
171 // South Bridge exist
172 if ( ReadLPCReg(0x20) != 0x3C )
177 if (Chipset==0x3076) {
178 pr_debug("%s(): Chipset = 3076\n", __func__);
180 WriteLPCReg(7,0x0c );
181 temp=ReadLPCReg(0x30);//check if BIOS Enable Fir
182 if((temp&0x01)==1) { // BIOS close or no FIR
183 WriteLPCReg(0x1d, 0x82 );
184 WriteLPCReg(0x23,0x18);
185 temp=ReadLPCReg(0xF0);
187 temp=(ReadLPCReg(0x74)&0x03); //DMA
189 temp=(ReadLPCReg(0x74)&0x0C) >> 2;
192 temp=(ReadLPCReg(0x74)&0x0C) >> 2; //DMA
196 FirIRQ=(ReadLPCReg(0x70)&0x0f); //IRQ
197 FirIOBase=ReadLPCReg(0x60 ) << 8; //IO Space :high byte
198 FirIOBase=FirIOBase| ReadLPCReg(0x61) ; //low byte
199 FirIOBase=FirIOBase ;
200 info.fir_base=FirIOBase;
204 pci_read_config_byte(pcidev,0x40,&bTmp);
205 pci_write_config_byte(pcidev,0x40,((bTmp | 0x08) & 0xfe));
206 pci_read_config_byte(pcidev,0x42,&bTmp);
207 pci_write_config_byte(pcidev,0x42,(bTmp | 0xf0));
208 pci_write_config_byte(pcidev,0x5a,0xc0);
209 WriteLPCReg(0x28, 0x70 );
210 rc = via_ircc_open(pcidev, &info, 0x3076);
212 rc = -ENODEV; //IR not turn on
213 } else { //Not VT1211
214 pr_debug("%s(): Chipset = 3096\n", __func__);
216 pci_read_config_byte(pcidev,0x67,&bTmp);//check if BIOS Enable Fir
217 if((bTmp&0x01)==1) { // BIOS enable FIR
218 //Enable Double DMA clock
219 pci_read_config_byte(pcidev,0x42,&oldPCI_40);
220 pci_write_config_byte(pcidev,0x42,oldPCI_40 | 0x80);
221 pci_read_config_byte(pcidev,0x40,&oldPCI_40);
222 pci_write_config_byte(pcidev,0x40,oldPCI_40 & 0xf7);
223 pci_read_config_byte(pcidev,0x44,&oldPCI_44);
224 pci_write_config_byte(pcidev,0x44,0x4e);
225 //---------- read configuration from Function0 of south bridge
227 pci_read_config_byte(pcidev,0x44,&bTmp1); //DMA
228 FirDRQ0 = (bTmp1 & 0x30) >> 4;
229 pci_read_config_byte(pcidev,0x44,&bTmp1);
230 FirDRQ1 = (bTmp1 & 0xc0) >> 6;
232 pci_read_config_byte(pcidev,0x44,&bTmp1); //DMA
233 FirDRQ0 = (bTmp1 & 0x30) >> 4 ;
236 pci_read_config_byte(pcidev,0x47,&bTmp1); //IRQ
237 FirIRQ = bTmp1 & 0x0f;
239 pci_read_config_byte(pcidev,0x69,&bTmp);
240 FirIOBase = bTmp << 8;//hight byte
241 pci_read_config_byte(pcidev,0x68,&bTmp);
242 FirIOBase = (FirIOBase | bTmp ) & 0xfff0;
243 //-------------------------
244 info.fir_base=FirIOBase;
248 rc = via_ircc_open(pcidev, &info, 0x3096);
250 rc = -ENODEV; //IR not turn on !!!!!
253 pr_debug("%s(): End - rc = %d\n", __func__, rc);
257 static void __exit via_ircc_cleanup(void)
259 /* Cleanup all instances of the driver */
260 pci_unregister_driver (&via_driver);
263 static const struct net_device_ops via_ircc_sir_ops = {
264 .ndo_start_xmit = via_ircc_hard_xmit_sir,
265 .ndo_open = via_ircc_net_open,
266 .ndo_stop = via_ircc_net_close,
267 .ndo_do_ioctl = via_ircc_net_ioctl,
269 static const struct net_device_ops via_ircc_fir_ops = {
270 .ndo_start_xmit = via_ircc_hard_xmit_fir,
271 .ndo_open = via_ircc_net_open,
272 .ndo_stop = via_ircc_net_close,
273 .ndo_do_ioctl = via_ircc_net_ioctl,
277 * Function via_ircc_open(pdev, iobase, irq)
279 * Open driver instance
282 static int via_ircc_open(struct pci_dev *pdev, chipio_t *info, unsigned int id)
284 struct net_device *dev;
285 struct via_ircc_cb *self;
288 /* Allocate new instance of the driver */
289 dev = alloc_irdadev(sizeof(struct via_ircc_cb));
293 self = netdev_priv(dev);
295 spin_lock_init(&self->lock);
297 pci_set_drvdata(pdev, self);
299 /* Initialize Resource */
300 self->io.cfg_base = info->cfg_base;
301 self->io.fir_base = info->fir_base;
302 self->io.irq = info->irq;
303 self->io.fir_ext = CHIP_IO_EXTENT;
304 self->io.dma = info->dma;
305 self->io.dma2 = info->dma2;
306 self->io.fifo_size = 32;
308 self->st_fifo.len = 0;
309 self->RxDataReady = 0;
311 /* Reserve the ioports that we need */
312 if (!request_region(self->io.fir_base, self->io.fir_ext, driver_name)) {
313 pr_debug("%s(), can't get iobase of 0x%03x\n",
314 __func__, self->io.fir_base);
319 /* Initialize QoS for this device */
320 irda_init_max_qos_capabilies(&self->qos);
322 /* Check if user has supplied the dongle id or not */
324 dongle_id = via_ircc_read_dongle_id(self->io.fir_base);
325 self->io.dongle_id = dongle_id;
327 /* The only value we must override it the baudrate */
328 /* Maximum speeds and capabilities are dongle-dependent. */
329 switch( self->io.dongle_id ){
331 self->qos.baud_rate.bits =
332 IR_9600 | IR_19200 | IR_38400 | IR_57600 | IR_115200 |
333 IR_576000 | IR_1152000 | (IR_4000000 << 8);
336 self->qos.baud_rate.bits =
337 IR_9600 | IR_19200 | IR_38400 | IR_57600 | IR_115200;
341 /* Following was used for testing:
343 * self->qos.baud_rate.bits = IR_9600;
345 * Is is no good, as it prohibits (error-prone) speed-changes.
348 self->qos.min_turn_time.bits = qos_mtt_bits;
349 irda_qos_bits_to_value(&self->qos);
351 /* Max DMA buffer size needed = (data_size + 6) * (window_size) + 6; */
352 self->rx_buff.truesize = 14384 + 2048;
353 self->tx_buff.truesize = 14384 + 2048;
355 /* Allocate memory if needed */
357 dma_zalloc_coherent(&pdev->dev, self->rx_buff.truesize,
358 &self->rx_buff_dma, GFP_KERNEL);
359 if (self->rx_buff.head == NULL) {
365 dma_zalloc_coherent(&pdev->dev, self->tx_buff.truesize,
366 &self->tx_buff_dma, GFP_KERNEL);
367 if (self->tx_buff.head == NULL) {
372 self->rx_buff.in_frame = FALSE;
373 self->rx_buff.state = OUTSIDE_FRAME;
374 self->tx_buff.data = self->tx_buff.head;
375 self->rx_buff.data = self->rx_buff.head;
377 /* Reset Tx queue info */
378 self->tx_fifo.len = self->tx_fifo.ptr = self->tx_fifo.free = 0;
379 self->tx_fifo.tail = self->tx_buff.head;
381 /* Override the network functions we need to use */
382 dev->netdev_ops = &via_ircc_sir_ops;
384 err = register_netdev(dev);
388 net_info_ratelimited("IrDA: Registered device %s (via-ircc)\n",
391 /* Initialise the hardware..
393 self->io.speed = 9600;
397 dma_free_coherent(&pdev->dev, self->tx_buff.truesize,
398 self->tx_buff.head, self->tx_buff_dma);
400 dma_free_coherent(&pdev->dev, self->rx_buff.truesize,
401 self->rx_buff.head, self->rx_buff_dma);
403 release_region(self->io.fir_base, self->io.fir_ext);
410 * Function via_remove_one(pdev)
412 * Close driver instance
415 static void via_remove_one(struct pci_dev *pdev)
417 struct via_ircc_cb *self = pci_get_drvdata(pdev);
420 iobase = self->io.fir_base;
422 ResetChip(iobase, 5); //hardware reset.
423 /* Remove netdevice */
424 unregister_netdev(self->netdev);
426 /* Release the PORT that this driver is using */
427 pr_debug("%s(), Releasing Region %03x\n",
428 __func__, self->io.fir_base);
429 release_region(self->io.fir_base, self->io.fir_ext);
430 if (self->tx_buff.head)
431 dma_free_coherent(&pdev->dev, self->tx_buff.truesize,
432 self->tx_buff.head, self->tx_buff_dma);
433 if (self->rx_buff.head)
434 dma_free_coherent(&pdev->dev, self->rx_buff.truesize,
435 self->rx_buff.head, self->rx_buff_dma);
437 free_netdev(self->netdev);
439 pci_disable_device(pdev);
443 * Function via_hw_init(self)
445 * Returns non-negative on success.
447 * Formerly via_ircc_setup
449 static void via_hw_init(struct via_ircc_cb *self)
451 int iobase = self->io.fir_base;
453 SetMaxRxPacketSize(iobase, 0x0fff); //set to max:4095
455 EnRXFIFOReadyInt(iobase, OFF);
456 EnRXFIFOHalfLevelInt(iobase, OFF);
457 EnTXFIFOHalfLevelInt(iobase, OFF);
458 EnTXFIFOUnderrunEOMInt(iobase, ON);
459 EnTXFIFOReadyInt(iobase, OFF);
460 InvertTX(iobase, OFF);
461 InvertRX(iobase, OFF);
463 if (ReadLPCReg(0x20) == 0x3c)
464 WriteLPCReg(0xF0, 0); // for VT1211
466 EnRXSpecInt(iobase, ON);
468 /* The following is basically hwreset */
469 /* If this is the case, why not just call hwreset() ? Jean II */
470 ResetChip(iobase, 5);
471 EnableDMA(iobase, OFF);
472 EnableTX(iobase, OFF);
473 EnableRX(iobase, OFF);
474 EnRXDMA(iobase, OFF);
475 EnTXDMA(iobase, OFF);
476 RXStart(iobase, OFF);
477 TXStart(iobase, OFF);
480 SIRFilter(iobase, ON);
484 WriteReg(iobase, I_ST_CT_0, 0x00);
485 SetBaudRate(iobase, 9600);
486 SetPulseWidth(iobase, 12);
487 SetSendPreambleCount(iobase, 0);
489 self->io.speed = 9600;
490 self->st_fifo.len = 0;
492 via_ircc_change_dongle_speed(iobase, self->io.speed,
495 WriteReg(iobase, I_ST_CT_0, 0x80);
499 * Function via_ircc_read_dongle_id (void)
502 static int via_ircc_read_dongle_id(int iobase)
504 net_err_ratelimited("via-ircc: dongle probing not supported, please specify dongle_id module parameter\n");
505 return 9; /* Default to IBM */
509 * Function via_ircc_change_dongle_speed (iobase, speed, dongle_id)
510 * Change speed of the attach dongle
511 * only implement two type of dongle currently.
513 static void via_ircc_change_dongle_speed(int iobase, int speed,
518 /* speed is unused, as we use IsSIROn()/IsMIROn() */
521 pr_debug("%s(): change_dongle_speed to %d for 0x%x, %d\n",
522 __func__, speed, iobase, dongle_id);
526 /* Note: The dongle_id's listed here are derived from
529 case 0x08: /* HP HSDL-2300, HP HSDL-3600/HSDL-3610 */
530 UseOneRX(iobase, ON); // use one RX pin RX1,RX2
531 InvertTX(iobase, OFF);
532 InvertRX(iobase, OFF);
534 EnRX2(iobase, ON); //sir to rx2
535 EnGPIOtoRX2(iobase, OFF);
537 if (IsSIROn(iobase)) { //sir
539 SlowIRRXLowActive(iobase, ON);
541 SlowIRRXLowActive(iobase, OFF);
543 if (IsMIROn(iobase)) { //mir
545 SlowIRRXLowActive(iobase, OFF);
548 if (IsFIROn(iobase)) { //fir
550 SlowIRRXLowActive(iobase, OFF);
557 case 0x09: /* IBM31T1100 or Temic TFDS6000/TFDS6500 */
558 UseOneRX(iobase, ON); //use ONE RX....RX1
559 InvertTX(iobase, OFF);
560 InvertRX(iobase, OFF); // invert RX pin
563 EnGPIOtoRX2(iobase, OFF);
564 if (IsSIROn(iobase)) { //sir
566 SlowIRRXLowActive(iobase, ON);
569 SlowIRRXLowActive(iobase, OFF);
571 if (IsMIROn(iobase)) { //mir
573 SlowIRRXLowActive(iobase, OFF);
576 SlowIRRXLowActive(iobase, ON);
578 if (IsFIROn(iobase)) { //fir
580 SlowIRRXLowActive(iobase, OFF);
585 SlowIRRXLowActive(iobase, ON);
588 WriteTX(iobase, OFF);
594 UseOneRX(iobase, OFF); // use two RX pin RX1,RX2
595 InvertTX(iobase, OFF);
596 InvertRX(iobase, OFF);
597 SlowIRRXLowActive(iobase, OFF);
598 if (IsSIROn(iobase)) { //sir
599 EnGPIOtoRX2(iobase, OFF);
600 WriteGIO(iobase, OFF);
601 EnRX2(iobase, OFF); //sir to rx2
603 EnGPIOtoRX2(iobase, OFF);
604 WriteGIO(iobase, OFF);
605 EnRX2(iobase, OFF); //fir to rx
609 case 0x11: /* Temic TFDS4500 */
611 pr_debug("%s: Temic TFDS4500: One RX pin, TX normal, RX inverted\n",
614 UseOneRX(iobase, ON); //use ONE RX....RX1
615 InvertTX(iobase, OFF);
616 InvertRX(iobase, ON); // invert RX pin
618 EnRX2(iobase, ON); //sir to rx2
619 EnGPIOtoRX2(iobase, OFF);
621 if( IsSIROn(iobase) ){ //sir
624 SlowIRRXLowActive(iobase, ON);
627 SlowIRRXLowActive(iobase, OFF);
630 pr_debug("%s: Warning: TFDS4500 not running in SIR mode !\n",
635 case 0x0ff: /* Vishay */
638 else if (IsMIROn(iobase))
640 else if (IsFIROn(iobase))
642 else if (IsVFIROn(iobase))
644 SI_SetMode(iobase, mode);
648 net_err_ratelimited("%s: Error: dongle_id %d unsupported !\n",
649 __func__, dongle_id);
654 * Function via_ircc_change_speed (self, baud)
656 * Change the speed of the device
659 static void via_ircc_change_speed(struct via_ircc_cb *self, __u32 speed)
661 struct net_device *dev = self->netdev;
665 iobase = self->io.fir_base;
666 /* Update accounting for new speed */
667 self->io.speed = speed;
668 pr_debug("%s: change_speed to %d bps.\n", __func__, speed);
670 WriteReg(iobase, I_ST_CT_0, 0x0);
672 /* Controller mode sellection */
680 value = (115200/speed)-1;
685 /* FIXME: this can't be right, as it's the same as 115200,
686 * and 576000 is MIR, not SIR. */
699 SetPulseWidth(iobase, 0);
700 SetSendPreambleCount(iobase, 14);
714 /* Set baudrate to 0x19[2..7] */
715 bTmp = (ReadReg(iobase, I_CF_H_1) & 0x03);
717 WriteReg(iobase, I_CF_H_1, bTmp);
719 /* Some dongles may need to be informed about speed changes. */
720 via_ircc_change_dongle_speed(iobase, speed, self->io.dongle_id);
722 /* Set FIFO size to 64 */
726 WriteReg(iobase, I_ST_CT_0, 0x80);
728 // EnTXFIFOHalfLevelInt(iobase,ON);
730 /* Enable some interrupts so we can receive frames */
731 //EnAllInt(iobase,ON);
733 if (IsSIROn(iobase)) {
734 SIRFilter(iobase, ON);
735 SIRRecvAny(iobase, ON);
737 SIRFilter(iobase, OFF);
738 SIRRecvAny(iobase, OFF);
741 if (speed > 115200) {
742 /* Install FIR xmit handler */
743 dev->netdev_ops = &via_ircc_fir_ops;
744 via_ircc_dma_receive(self);
746 /* Install SIR xmit handler */
747 dev->netdev_ops = &via_ircc_sir_ops;
749 netif_wake_queue(dev);
753 * Function via_ircc_hard_xmit (skb, dev)
755 * Transmit the frame!
758 static netdev_tx_t via_ircc_hard_xmit_sir(struct sk_buff *skb,
759 struct net_device *dev)
761 struct via_ircc_cb *self;
766 self = netdev_priv(dev);
767 IRDA_ASSERT(self != NULL, return NETDEV_TX_OK;);
768 iobase = self->io.fir_base;
770 netif_stop_queue(dev);
771 /* Check if we need to change the speed */
772 speed = irda_get_next_speed(skb);
773 if ((speed != self->io.speed) && (speed != -1)) {
774 /* Check for empty frame */
776 via_ircc_change_speed(self, speed);
777 dev->trans_start = jiffies;
781 self->new_speed = speed;
785 SIRFilter(iobase, ON);
789 WriteReg(iobase, I_ST_CT_0, 0x00);
791 spin_lock_irqsave(&self->lock, flags);
792 self->tx_buff.data = self->tx_buff.head;
794 async_wrap_skb(skb, self->tx_buff.data,
795 self->tx_buff.truesize);
797 dev->stats.tx_bytes += self->tx_buff.len;
798 /* Send this frame with old speed */
799 SetBaudRate(iobase, self->io.speed);
800 SetPulseWidth(iobase, 12);
801 SetSendPreambleCount(iobase, 0);
802 WriteReg(iobase, I_ST_CT_0, 0x80);
804 EnableTX(iobase, ON);
805 EnableRX(iobase, OFF);
807 ResetChip(iobase, 0);
808 ResetChip(iobase, 1);
809 ResetChip(iobase, 2);
810 ResetChip(iobase, 3);
811 ResetChip(iobase, 4);
813 EnAllInt(iobase, ON);
815 EnRXDMA(iobase, OFF);
817 irda_setup_dma(self->io.dma, self->tx_buff_dma, self->tx_buff.len,
820 SetSendByte(iobase, self->tx_buff.len);
821 RXStart(iobase, OFF);
824 dev->trans_start = jiffies;
825 spin_unlock_irqrestore(&self->lock, flags);
830 static netdev_tx_t via_ircc_hard_xmit_fir(struct sk_buff *skb,
831 struct net_device *dev)
833 struct via_ircc_cb *self;
838 self = netdev_priv(dev);
839 iobase = self->io.fir_base;
841 if (self->st_fifo.len)
843 if (self->chip_id == 0x3076)
847 netif_stop_queue(dev);
848 speed = irda_get_next_speed(skb);
849 if ((speed != self->io.speed) && (speed != -1)) {
851 via_ircc_change_speed(self, speed);
852 dev->trans_start = jiffies;
856 self->new_speed = speed;
858 spin_lock_irqsave(&self->lock, flags);
859 self->tx_fifo.queue[self->tx_fifo.free].start = self->tx_fifo.tail;
860 self->tx_fifo.queue[self->tx_fifo.free].len = skb->len;
862 self->tx_fifo.tail += skb->len;
863 dev->stats.tx_bytes += skb->len;
864 skb_copy_from_linear_data(skb,
865 self->tx_fifo.queue[self->tx_fifo.free].start, skb->len);
867 self->tx_fifo.free++;
868 //F01 if (self->tx_fifo.len == 1) {
869 via_ircc_dma_xmit(self, iobase);
871 //F01 if (self->tx_fifo.free < (MAX_TX_WINDOW -1 )) netif_wake_queue(self->netdev);
872 dev->trans_start = jiffies;
874 spin_unlock_irqrestore(&self->lock, flags);
879 static int via_ircc_dma_xmit(struct via_ircc_cb *self, u16 iobase)
881 EnTXDMA(iobase, OFF);
882 self->io.direction = IO_XMIT;
884 EnableTX(iobase, ON);
885 EnableRX(iobase, OFF);
886 ResetChip(iobase, 0);
887 ResetChip(iobase, 1);
888 ResetChip(iobase, 2);
889 ResetChip(iobase, 3);
890 ResetChip(iobase, 4);
891 EnAllInt(iobase, ON);
893 EnRXDMA(iobase, OFF);
894 irda_setup_dma(self->io.dma,
895 ((u8 *)self->tx_fifo.queue[self->tx_fifo.ptr].start -
896 self->tx_buff.head) + self->tx_buff_dma,
897 self->tx_fifo.queue[self->tx_fifo.ptr].len, DMA_TX_MODE);
898 pr_debug("%s: tx_fifo.ptr=%x,len=%x,tx_fifo.len=%x..\n",
899 __func__, self->tx_fifo.ptr,
900 self->tx_fifo.queue[self->tx_fifo.ptr].len,
903 SetSendByte(iobase, self->tx_fifo.queue[self->tx_fifo.ptr].len);
904 RXStart(iobase, OFF);
911 * Function via_ircc_dma_xmit_complete (self)
913 * The transfer of a frame in finished. This function will only be called
914 * by the interrupt handler
917 static int via_ircc_dma_xmit_complete(struct via_ircc_cb *self)
922 iobase = self->io.fir_base;
924 // DisableDmaChannel(self->io.dma);
925 /* Check for underrun! */
926 /* Clear bit, by writing 1 into it */
927 Tx_status = GetTXStatus(iobase);
928 if (Tx_status & 0x08) {
929 self->netdev->stats.tx_errors++;
930 self->netdev->stats.tx_fifo_errors++;
932 /* how to clear underrun? */
934 self->netdev->stats.tx_packets++;
935 ResetChip(iobase, 3);
936 ResetChip(iobase, 4);
938 /* Check if we need to change the speed */
939 if (self->new_speed) {
940 via_ircc_change_speed(self, self->new_speed);
944 /* Finished with this frame, so prepare for next */
945 if (IsFIROn(iobase)) {
946 if (self->tx_fifo.len) {
951 pr_debug("%s: tx_fifo.len=%x ,tx_fifo.ptr=%x,tx_fifo.free=%x...\n",
953 self->tx_fifo.len, self->tx_fifo.ptr, self->tx_fifo.free);
955 // Any frames to be sent back-to-back?
956 if (self->tx_fifo.len) {
958 via_ircc_dma_xmit(self, iobase);
962 // Reset Tx FIFO info
963 self->tx_fifo.len = self->tx_fifo.ptr = self->tx_fifo.free = 0;
964 self->tx_fifo.tail = self->tx_buff.head;
967 // Make sure we have room for more frames
968 //F01 if (self->tx_fifo.free < (MAX_TX_WINDOW -1 )) {
969 // Not busy transmitting anymore
970 // Tell the network layer, that we can accept more frames
971 netif_wake_queue(self->netdev);
977 * Function via_ircc_dma_receive (self)
979 * Set configuration for receive a frame.
982 static int via_ircc_dma_receive(struct via_ircc_cb *self)
986 iobase = self->io.fir_base;
988 self->tx_fifo.len = self->tx_fifo.ptr = self->tx_fifo.free = 0;
989 self->tx_fifo.tail = self->tx_buff.head;
990 self->RxDataReady = 0;
991 self->io.direction = IO_RECV;
992 self->rx_buff.data = self->rx_buff.head;
993 self->st_fifo.len = self->st_fifo.pending_bytes = 0;
994 self->st_fifo.tail = self->st_fifo.head = 0;
997 EnableTX(iobase, OFF);
998 EnableRX(iobase, ON);
1000 ResetChip(iobase, 0);
1001 ResetChip(iobase, 1);
1002 ResetChip(iobase, 2);
1003 ResetChip(iobase, 3);
1004 ResetChip(iobase, 4);
1006 EnAllInt(iobase, ON);
1007 EnTXDMA(iobase, OFF);
1008 EnRXDMA(iobase, ON);
1009 irda_setup_dma(self->io.dma2, self->rx_buff_dma,
1010 self->rx_buff.truesize, DMA_RX_MODE);
1011 TXStart(iobase, OFF);
1012 RXStart(iobase, ON);
1018 * Function via_ircc_dma_receive_complete (self)
1020 * Controller Finished with receiving frames,
1021 * and this routine is call by ISR
1024 static int via_ircc_dma_receive_complete(struct via_ircc_cb *self,
1027 struct st_fifo *st_fifo;
1028 struct sk_buff *skb;
1032 iobase = self->io.fir_base;
1033 st_fifo = &self->st_fifo;
1035 if (self->io.speed < 4000000) { //Speed below FIR
1036 len = GetRecvByte(iobase, self);
1037 skb = dev_alloc_skb(len + 1);
1040 // Make sure IP header gets aligned
1041 skb_reserve(skb, 1);
1042 skb_put(skb, len - 2);
1043 if (self->chip_id == 0x3076) {
1044 for (i = 0; i < len - 2; i++)
1045 skb->data[i] = self->rx_buff.data[i * 2];
1047 if (self->chip_id == 0x3096) {
1048 for (i = 0; i < len - 2; i++)
1050 self->rx_buff.data[i];
1053 // Move to next frame
1054 self->rx_buff.data += len;
1055 self->netdev->stats.rx_bytes += len;
1056 self->netdev->stats.rx_packets++;
1057 skb->dev = self->netdev;
1058 skb_reset_mac_header(skb);
1059 skb->protocol = htons(ETH_P_IRDA);
1065 len = GetRecvByte(iobase, self);
1067 return TRUE; //interrupt only, data maybe move by RxT
1068 if (((len - 4) < 2) || ((len - 4) > 2048)) {
1069 pr_debug("%s(): Trouble:len=%x,CurCount=%x,LastCount=%x\n",
1070 __func__, len, RxCurCount(iobase, self),
1075 pr_debug("%s(): fifo.len=%x,len=%x,CurCount=%x..\n",
1077 st_fifo->len, len - 4, RxCurCount(iobase, self));
1079 st_fifo->entries[st_fifo->tail].status = status;
1080 st_fifo->entries[st_fifo->tail].len = len;
1081 st_fifo->pending_bytes += len;
1084 if (st_fifo->tail > MAX_RX_WINDOW)
1086 self->RxDataReady = 0;
1088 // It maybe have MAX_RX_WINDOW package receive by
1089 // receive_complete before Timer IRQ
1091 if (st_fifo->len < (MAX_RX_WINDOW+2 )) {
1097 EnableRX(iobase, OFF);
1098 EnRXDMA(iobase, OFF);
1099 RXStart(iobase, OFF);
1101 // Put this entry back in fifo
1102 if (st_fifo->head > MAX_RX_WINDOW)
1104 status = st_fifo->entries[st_fifo->head].status;
1105 len = st_fifo->entries[st_fifo->head].len;
1109 skb = dev_alloc_skb(len + 1 - 4);
1111 * if frame size, data ptr, or skb ptr are wrong, then get next
1114 if ((skb == NULL) || (skb->data == NULL) ||
1115 (self->rx_buff.data == NULL) || (len < 6)) {
1116 self->netdev->stats.rx_dropped++;
1120 skb_reserve(skb, 1);
1121 skb_put(skb, len - 4);
1123 skb_copy_to_linear_data(skb, self->rx_buff.data, len - 4);
1124 pr_debug("%s(): len=%x.rx_buff=%p\n", __func__,
1125 len - 4, self->rx_buff.data);
1127 // Move to next frame
1128 self->rx_buff.data += len;
1129 self->netdev->stats.rx_bytes += len;
1130 self->netdev->stats.rx_packets++;
1131 skb->dev = self->netdev;
1132 skb_reset_mac_header(skb);
1133 skb->protocol = htons(ETH_P_IRDA);
1143 * if frame is received , but no INT ,then use this routine to upload frame.
1145 static int upload_rxdata(struct via_ircc_cb *self, int iobase)
1147 struct sk_buff *skb;
1149 struct st_fifo *st_fifo;
1150 st_fifo = &self->st_fifo;
1152 len = GetRecvByte(iobase, self);
1154 pr_debug("%s(): len=%x\n", __func__, len);
1156 if ((len - 4) < 2) {
1157 self->netdev->stats.rx_dropped++;
1161 skb = dev_alloc_skb(len + 1);
1163 self->netdev->stats.rx_dropped++;
1166 skb_reserve(skb, 1);
1167 skb_put(skb, len - 4 + 1);
1168 skb_copy_to_linear_data(skb, self->rx_buff.data, len - 4 + 1);
1171 if (st_fifo->tail > MAX_RX_WINDOW)
1173 // Move to next frame
1174 self->rx_buff.data += len;
1175 self->netdev->stats.rx_bytes += len;
1176 self->netdev->stats.rx_packets++;
1177 skb->dev = self->netdev;
1178 skb_reset_mac_header(skb);
1179 skb->protocol = htons(ETH_P_IRDA);
1181 if (st_fifo->len < (MAX_RX_WINDOW + 2)) {
1182 RXStart(iobase, ON);
1184 EnableRX(iobase, OFF);
1185 EnRXDMA(iobase, OFF);
1186 RXStart(iobase, OFF);
1192 * Implement back to back receive , use this routine to upload data.
1195 static int RxTimerHandler(struct via_ircc_cb *self, int iobase)
1197 struct st_fifo *st_fifo;
1198 struct sk_buff *skb;
1202 st_fifo = &self->st_fifo;
1204 if (CkRxRecv(iobase, self)) {
1205 // if still receiving ,then return ,don't upload frame
1206 self->RetryCount = 0;
1207 SetTimer(iobase, 20);
1208 self->RxDataReady++;
1213 if ((self->RetryCount >= 1) ||
1214 ((st_fifo->pending_bytes + 2048) > self->rx_buff.truesize) ||
1215 (st_fifo->len >= (MAX_RX_WINDOW))) {
1216 while (st_fifo->len > 0) { //upload frame
1217 // Put this entry back in fifo
1218 if (st_fifo->head > MAX_RX_WINDOW)
1220 status = st_fifo->entries[st_fifo->head].status;
1221 len = st_fifo->entries[st_fifo->head].len;
1225 skb = dev_alloc_skb(len + 1 - 4);
1227 * if frame size, data ptr, or skb ptr are wrong,
1228 * then get next entry.
1230 if ((skb == NULL) || (skb->data == NULL) ||
1231 (self->rx_buff.data == NULL) || (len < 6)) {
1232 self->netdev->stats.rx_dropped++;
1235 skb_reserve(skb, 1);
1236 skb_put(skb, len - 4);
1237 skb_copy_to_linear_data(skb, self->rx_buff.data, len - 4);
1239 pr_debug("%s(): len=%x.head=%x\n", __func__,
1240 len - 4, st_fifo->head);
1242 // Move to next frame
1243 self->rx_buff.data += len;
1244 self->netdev->stats.rx_bytes += len;
1245 self->netdev->stats.rx_packets++;
1246 skb->dev = self->netdev;
1247 skb_reset_mac_header(skb);
1248 skb->protocol = htons(ETH_P_IRDA);
1251 self->RetryCount = 0;
1253 pr_debug("%s(): End of upload HostStatus=%x,RxStatus=%x\n",
1254 __func__, GetHostStatus(iobase), GetRXStatus(iobase));
1257 * if frame is receive complete at this routine ,then upload
1260 if ((GetRXStatus(iobase) & 0x10) &&
1261 (RxCurCount(iobase, self) != self->RxLastCount)) {
1262 upload_rxdata(self, iobase);
1263 if (irda_device_txqueue_empty(self->netdev))
1264 via_ircc_dma_receive(self);
1266 } // timer detect complete
1268 SetTimer(iobase, 4);
1276 * Function via_ircc_interrupt (irq, dev_id)
1278 * An interrupt from the chip has arrived. Time to do some work
1281 static irqreturn_t via_ircc_interrupt(int dummy, void *dev_id)
1283 struct net_device *dev = dev_id;
1284 struct via_ircc_cb *self = netdev_priv(dev);
1286 u8 iHostIntType, iRxIntType, iTxIntType;
1288 iobase = self->io.fir_base;
1289 spin_lock(&self->lock);
1290 iHostIntType = GetHostStatus(iobase);
1292 pr_debug("%s(): iHostIntType %02x: %s %s %s %02x\n",
1293 __func__, iHostIntType,
1294 (iHostIntType & 0x40) ? "Timer" : "",
1295 (iHostIntType & 0x20) ? "Tx" : "",
1296 (iHostIntType & 0x10) ? "Rx" : "",
1297 (iHostIntType & 0x0e) >> 1);
1299 if ((iHostIntType & 0x40) != 0) { //Timer Event
1300 self->EventFlag.TimeOut++;
1301 ClearTimerInt(iobase, 1);
1302 if (self->io.direction == IO_XMIT) {
1303 via_ircc_dma_xmit(self, iobase);
1305 if (self->io.direction == IO_RECV) {
1307 * frame ready hold too long, must reset.
1309 if (self->RxDataReady > 30) {
1311 if (irda_device_txqueue_empty(self->netdev)) {
1312 via_ircc_dma_receive(self);
1314 } else { // call this to upload frame.
1315 RxTimerHandler(self, iobase);
1319 if ((iHostIntType & 0x20) != 0) { //Tx Event
1320 iTxIntType = GetTXStatus(iobase);
1322 pr_debug("%s(): iTxIntType %02x: %s %s %s %s\n",
1323 __func__, iTxIntType,
1324 (iTxIntType & 0x08) ? "FIFO underr." : "",
1325 (iTxIntType & 0x04) ? "EOM" : "",
1326 (iTxIntType & 0x02) ? "FIFO ready" : "",
1327 (iTxIntType & 0x01) ? "Early EOM" : "");
1329 if (iTxIntType & 0x4) {
1330 self->EventFlag.EOMessage++; // read and will auto clean
1331 if (via_ircc_dma_xmit_complete(self)) {
1332 if (irda_device_txqueue_empty
1334 via_ircc_dma_receive(self);
1337 self->EventFlag.Unknown++;
1341 //----------------------------------------
1342 if ((iHostIntType & 0x10) != 0) { //Rx Event
1343 /* Check if DMA has finished */
1344 iRxIntType = GetRXStatus(iobase);
1346 pr_debug("%s(): iRxIntType %02x: %s %s %s %s %s %s %s\n",
1347 __func__, iRxIntType,
1348 (iRxIntType & 0x80) ? "PHY err." : "",
1349 (iRxIntType & 0x40) ? "CRC err" : "",
1350 (iRxIntType & 0x20) ? "FIFO overr." : "",
1351 (iRxIntType & 0x10) ? "EOF" : "",
1352 (iRxIntType & 0x08) ? "RxData" : "",
1353 (iRxIntType & 0x02) ? "RxMaxLen" : "",
1354 (iRxIntType & 0x01) ? "SIR bad" : "");
1356 pr_debug("%s(): RxIRQ =0\n", __func__);
1358 if (iRxIntType & 0x10) {
1359 if (via_ircc_dma_receive_complete(self, iobase)) {
1360 //F01 if(!(IsFIROn(iobase))) via_ircc_dma_receive(self);
1361 via_ircc_dma_receive(self);
1365 pr_debug("%s(): RxIRQ ERR:iRxIntType=%x,HostIntType=%x,CurCount=%x,RxLastCount=%x_____\n",
1366 __func__, iRxIntType, iHostIntType,
1367 RxCurCount(iobase, self), self->RxLastCount);
1369 if (iRxIntType & 0x20) { //FIFO OverRun ERR
1370 ResetChip(iobase, 0);
1371 ResetChip(iobase, 1);
1372 } else { //PHY,CRC ERR
1374 if (iRxIntType != 0x08)
1375 hwreset(self); //F01
1377 via_ircc_dma_receive(self);
1381 spin_unlock(&self->lock);
1382 return IRQ_RETVAL(iHostIntType);
1385 static void hwreset(struct via_ircc_cb *self)
1388 iobase = self->io.fir_base;
1390 ResetChip(iobase, 5);
1391 EnableDMA(iobase, OFF);
1392 EnableTX(iobase, OFF);
1393 EnableRX(iobase, OFF);
1394 EnRXDMA(iobase, OFF);
1395 EnTXDMA(iobase, OFF);
1396 RXStart(iobase, OFF);
1397 TXStart(iobase, OFF);
1400 SIRFilter(iobase, ON);
1404 WriteReg(iobase, I_ST_CT_0, 0x00);
1405 SetBaudRate(iobase, 9600);
1406 SetPulseWidth(iobase, 12);
1407 SetSendPreambleCount(iobase, 0);
1408 WriteReg(iobase, I_ST_CT_0, 0x80);
1410 /* Restore speed. */
1411 via_ircc_change_speed(self, self->io.speed);
1413 self->st_fifo.len = 0;
1417 * Function via_ircc_is_receiving (self)
1419 * Return TRUE is we are currently receiving a frame
1422 static int via_ircc_is_receiving(struct via_ircc_cb *self)
1427 IRDA_ASSERT(self != NULL, return FALSE;);
1429 iobase = self->io.fir_base;
1430 if (CkRxRecv(iobase, self))
1433 pr_debug("%s(): status=%x....\n", __func__, status);
1440 * Function via_ircc_net_open (dev)
1445 static int via_ircc_net_open(struct net_device *dev)
1447 struct via_ircc_cb *self;
1451 IRDA_ASSERT(dev != NULL, return -1;);
1452 self = netdev_priv(dev);
1453 dev->stats.rx_packets = 0;
1454 IRDA_ASSERT(self != NULL, return 0;);
1455 iobase = self->io.fir_base;
1456 if (request_irq(self->io.irq, via_ircc_interrupt, 0, dev->name, dev)) {
1457 net_warn_ratelimited("%s, unable to allocate irq=%d\n",
1458 driver_name, self->io.irq);
1462 * Always allocate the DMA channel after the IRQ, and clean up on
1465 if (request_dma(self->io.dma, dev->name)) {
1466 net_warn_ratelimited("%s, unable to allocate dma=%d\n",
1467 driver_name, self->io.dma);
1468 free_irq(self->io.irq, dev);
1471 if (self->io.dma2 != self->io.dma) {
1472 if (request_dma(self->io.dma2, dev->name)) {
1473 net_warn_ratelimited("%s, unable to allocate dma2=%d\n",
1474 driver_name, self->io.dma2);
1475 free_irq(self->io.irq, dev);
1476 free_dma(self->io.dma);
1482 /* turn on interrupts */
1483 EnAllInt(iobase, ON);
1484 EnInternalLoop(iobase, OFF);
1485 EnExternalLoop(iobase, OFF);
1488 via_ircc_dma_receive(self);
1490 /* Ready to play! */
1491 netif_start_queue(dev);
1494 * Open new IrLAP layer instance, now that everything should be
1495 * initialized properly
1497 sprintf(hwname, "VIA @ 0x%x", iobase);
1498 self->irlap = irlap_open(dev, &self->qos, hwname);
1500 self->RxLastCount = 0;
1506 * Function via_ircc_net_close (dev)
1511 static int via_ircc_net_close(struct net_device *dev)
1513 struct via_ircc_cb *self;
1516 IRDA_ASSERT(dev != NULL, return -1;);
1517 self = netdev_priv(dev);
1518 IRDA_ASSERT(self != NULL, return 0;);
1521 netif_stop_queue(dev);
1522 /* Stop and remove instance of IrLAP */
1524 irlap_close(self->irlap);
1526 iobase = self->io.fir_base;
1527 EnTXDMA(iobase, OFF);
1528 EnRXDMA(iobase, OFF);
1529 DisableDmaChannel(self->io.dma);
1531 /* Disable interrupts */
1532 EnAllInt(iobase, OFF);
1533 free_irq(self->io.irq, dev);
1534 free_dma(self->io.dma);
1535 if (self->io.dma2 != self->io.dma)
1536 free_dma(self->io.dma2);
1542 * Function via_ircc_net_ioctl (dev, rq, cmd)
1544 * Process IOCTL commands for this device
1547 static int via_ircc_net_ioctl(struct net_device *dev, struct ifreq *rq,
1550 struct if_irda_req *irq = (struct if_irda_req *) rq;
1551 struct via_ircc_cb *self;
1552 unsigned long flags;
1555 IRDA_ASSERT(dev != NULL, return -1;);
1556 self = netdev_priv(dev);
1557 IRDA_ASSERT(self != NULL, return -1;);
1558 pr_debug("%s(), %s, (cmd=0x%X)\n", __func__, dev->name,
1560 /* Disable interrupts & save flags */
1561 spin_lock_irqsave(&self->lock, flags);
1563 case SIOCSBANDWIDTH: /* Set bandwidth */
1564 if (!capable(CAP_NET_ADMIN)) {
1568 via_ircc_change_speed(self, irq->ifr_baudrate);
1570 case SIOCSMEDIABUSY: /* Set media busy */
1571 if (!capable(CAP_NET_ADMIN)) {
1575 irda_device_set_media_busy(self->netdev, TRUE);
1577 case SIOCGRECEIVING: /* Check if we are receiving right now */
1578 irq->ifr_receiving = via_ircc_is_receiving(self);
1584 spin_unlock_irqrestore(&self->lock, flags);
1588 MODULE_AUTHOR("VIA Technologies,inc");
1589 MODULE_DESCRIPTION("VIA IrDA Device Driver");
1590 MODULE_LICENSE("GPL");
1592 module_init(via_ircc_init);
1593 module_exit(via_ircc_cleanup);