mlx4_en: Move to SW counters for total bytes and packets
[linux-2.6] / drivers / net / irda / ali-ircc.c
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>
13  * 
14  *     Copyright (c) 2000 Benjamin Kong <benjamin_kong@ali.com.tw>
15  *     All Rights Reserved
16  *      
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.
21  *  
22  ********************************************************************/
23
24 #include <linux/module.h>
25
26 #include <linux/kernel.h>
27 #include <linux/types.h>
28 #include <linux/skbuff.h>
29 #include <linux/netdevice.h>
30 #include <linux/ioport.h>
31 #include <linux/delay.h>
32 #include <linux/slab.h>
33 #include <linux/init.h>
34 #include <linux/rtnetlink.h>
35 #include <linux/serial_reg.h>
36 #include <linux/dma-mapping.h>
37 #include <linux/platform_device.h>
38
39 #include <asm/io.h>
40 #include <asm/dma.h>
41 #include <asm/byteorder.h>
42
43 #include <net/irda/wrapper.h>
44 #include <net/irda/irda.h>
45 #include <net/irda/irda_device.h>
46
47 #include "ali-ircc.h"
48
49 #define CHIP_IO_EXTENT 8
50 #define BROKEN_DONGLE_ID
51
52 #define ALI_IRCC_DRIVER_NAME "ali-ircc"
53
54 /* Power Management */
55 static int ali_ircc_suspend(struct platform_device *dev, pm_message_t state);
56 static int ali_ircc_resume(struct platform_device *dev);
57
58 static struct platform_driver ali_ircc_driver = {
59         .suspend        = ali_ircc_suspend,
60         .resume         = ali_ircc_resume,
61         .driver         = {
62                 .name   = ALI_IRCC_DRIVER_NAME,
63                 .owner  = THIS_MODULE,
64         },
65 };
66
67 /* Module parameters */
68 static int qos_mtt_bits = 0x07;  /* 1 ms or more */
69
70 /* Use BIOS settions by default, but user may supply module parameters */
71 static unsigned int io[]  = { ~0, ~0, ~0, ~0 };
72 static unsigned int irq[] = { 0, 0, 0, 0 };
73 static unsigned int dma[] = { 0, 0, 0, 0 };
74
75 static int  ali_ircc_probe_53(ali_chip_t *chip, chipio_t *info);
76 static int  ali_ircc_init_43(ali_chip_t *chip, chipio_t *info);
77 static int  ali_ircc_init_53(ali_chip_t *chip, chipio_t *info);
78
79 /* These are the currently known ALi sourth-bridge chipsets, the only one difference
80  * is that M1543C doesn't support HP HDSL-3600
81  */
82 static ali_chip_t chips[] =
83 {
84         { "M1543", { 0x3f0, 0x370 }, 0x51, 0x23, 0x20, 0x43, ali_ircc_probe_53, ali_ircc_init_43 },
85         { "M1535", { 0x3f0, 0x370 }, 0x51, 0x23, 0x20, 0x53, ali_ircc_probe_53, ali_ircc_init_53 },
86         { "M1563", { 0x3f0, 0x370 }, 0x51, 0x23, 0x20, 0x63, ali_ircc_probe_53, ali_ircc_init_53 },
87         { NULL }
88 };
89
90 /* Max 4 instances for now */
91 static struct ali_ircc_cb *dev_self[] = { NULL, NULL, NULL, NULL };
92
93 /* Dongle Types */
94 static char *dongle_types[] = {
95         "TFDS6000",
96         "HP HSDL-3600",
97         "HP HSDL-1100", 
98         "No dongle connected",
99 };
100
101 /* Some prototypes */
102 static int  ali_ircc_open(int i, chipio_t *info);
103
104 static int  ali_ircc_close(struct ali_ircc_cb *self);
105
106 static int  ali_ircc_setup(chipio_t *info);
107 static int  ali_ircc_is_receiving(struct ali_ircc_cb *self);
108 static int  ali_ircc_net_open(struct net_device *dev);
109 static int  ali_ircc_net_close(struct net_device *dev);
110 static int  ali_ircc_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
111 static void ali_ircc_change_speed(struct ali_ircc_cb *self, __u32 baud);
112
113 /* SIR function */
114 static int  ali_ircc_sir_hard_xmit(struct sk_buff *skb, struct net_device *dev);
115 static irqreturn_t ali_ircc_sir_interrupt(struct ali_ircc_cb *self);
116 static void ali_ircc_sir_receive(struct ali_ircc_cb *self);
117 static void ali_ircc_sir_write_wakeup(struct ali_ircc_cb *self);
118 static int  ali_ircc_sir_write(int iobase, int fifo_size, __u8 *buf, int len);
119 static void ali_ircc_sir_change_speed(struct ali_ircc_cb *priv, __u32 speed);
120
121 /* FIR function */
122 static int  ali_ircc_fir_hard_xmit(struct sk_buff *skb, struct net_device *dev);
123 static void ali_ircc_fir_change_speed(struct ali_ircc_cb *priv, __u32 speed);
124 static irqreturn_t ali_ircc_fir_interrupt(struct ali_ircc_cb *self);
125 static int  ali_ircc_dma_receive(struct ali_ircc_cb *self); 
126 static int  ali_ircc_dma_receive_complete(struct ali_ircc_cb *self);
127 static int  ali_ircc_dma_xmit_complete(struct ali_ircc_cb *self);
128 static void ali_ircc_dma_xmit(struct ali_ircc_cb *self);
129
130 /* My Function */
131 static int  ali_ircc_read_dongle_id (int i, chipio_t *info);
132 static void ali_ircc_change_dongle_speed(struct ali_ircc_cb *priv, int speed);
133
134 /* ALi chip function */
135 static void SIR2FIR(int iobase);
136 static void FIR2SIR(int iobase);
137 static void SetCOMInterrupts(struct ali_ircc_cb *self , unsigned char enable);
138
139 /*
140  * Function ali_ircc_init ()
141  *
142  *    Initialize chip. Find out whay kinds of chips we are dealing with
143  *    and their configuation registers address
144  */
145 static int __init ali_ircc_init(void)
146 {
147         ali_chip_t *chip;
148         chipio_t info;
149         int ret;
150         int cfg, cfg_base;
151         int reg, revision;
152         int i = 0;
153         
154         IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __func__);
155
156         ret = platform_driver_register(&ali_ircc_driver);
157         if (ret) {
158                 IRDA_ERROR("%s, Can't register driver!\n",
159                            ALI_IRCC_DRIVER_NAME);
160                 return ret;
161         }
162
163         ret = -ENODEV;
164         
165         /* Probe for all the ALi chipsets we know about */
166         for (chip= chips; chip->name; chip++, i++) 
167         {
168                 IRDA_DEBUG(2, "%s(), Probing for %s ...\n", __func__, chip->name);
169                                 
170                 /* Try all config registers for this chip */
171                 for (cfg=0; cfg<2; cfg++)
172                 {
173                         cfg_base = chip->cfg[cfg];
174                         if (!cfg_base)
175                                 continue;
176                                 
177                         memset(&info, 0, sizeof(chipio_t));
178                         info.cfg_base = cfg_base;
179                         info.fir_base = io[i];
180                         info.dma = dma[i];
181                         info.irq = irq[i];
182                         
183                         
184                         /* Enter Configuration */
185                         outb(chip->entr1, cfg_base);
186                         outb(chip->entr2, cfg_base);
187                         
188                         /* Select Logical Device 5 Registers (UART2) */
189                         outb(0x07, cfg_base);
190                         outb(0x05, cfg_base+1);
191                         
192                         /* Read Chip Identification Register */
193                         outb(chip->cid_index, cfg_base);        
194                         reg = inb(cfg_base+1);  
195                                 
196                         if (reg == chip->cid_value)
197                         {
198                                 IRDA_DEBUG(2, "%s(), Chip found at 0x%03x\n", __func__, cfg_base);
199                                            
200                                 outb(0x1F, cfg_base);
201                                 revision = inb(cfg_base+1);
202                                 IRDA_DEBUG(2, "%s(), Found %s chip, revision=%d\n", __func__,
203                                            chip->name, revision);                                       
204                                 
205                                 /* 
206                                  * If the user supplies the base address, then
207                                  * we init the chip, if not we probe the values
208                                  * set by the BIOS
209                                  */                             
210                                 if (io[i] < 2000)
211                                 {
212                                         chip->init(chip, &info);
213                                 }
214                                 else
215                                 {
216                                         chip->probe(chip, &info);       
217                                 }
218                                 
219                                 if (ali_ircc_open(i, &info) == 0)
220                                         ret = 0;
221                                 i++;                            
222                         }
223                         else
224                         {
225                                 IRDA_DEBUG(2, "%s(), No %s chip at 0x%03x\n", __func__, chip->name, cfg_base);
226                         }
227                         /* Exit configuration */
228                         outb(0xbb, cfg_base);
229                 }
230         }               
231                 
232         IRDA_DEBUG(2, "%s(), ----------------- End -----------------\n", __func__);
233
234         if (ret)
235                 platform_driver_unregister(&ali_ircc_driver);
236
237         return ret;
238 }
239
240 /*
241  * Function ali_ircc_cleanup ()
242  *
243  *    Close all configured chips
244  *
245  */
246 static void __exit ali_ircc_cleanup(void)
247 {
248         int i;
249
250         IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __func__);
251
252         for (i=0; i < ARRAY_SIZE(dev_self); i++) {
253                 if (dev_self[i])
254                         ali_ircc_close(dev_self[i]);
255         }
256         
257         platform_driver_unregister(&ali_ircc_driver);
258
259         IRDA_DEBUG(2, "%s(), ----------------- End -----------------\n", __func__);
260 }
261
262 static const struct net_device_ops ali_ircc_sir_ops = {
263         .ndo_open       = ali_ircc_net_open,
264         .ndo_stop       = ali_ircc_net_close,
265         .ndo_start_xmit = ali_ircc_sir_hard_xmit,
266         .ndo_do_ioctl   = ali_ircc_net_ioctl,
267 };
268
269 static const struct net_device_ops ali_ircc_fir_ops = {
270         .ndo_open       = ali_ircc_net_open,
271         .ndo_stop       = ali_ircc_net_close,
272         .ndo_start_xmit = ali_ircc_fir_hard_xmit,
273         .ndo_do_ioctl   = ali_ircc_net_ioctl,
274 };
275
276 /*
277  * Function ali_ircc_open (int i, chipio_t *inf)
278  *
279  *    Open driver instance
280  *
281  */
282 static int ali_ircc_open(int i, chipio_t *info)
283 {
284         struct net_device *dev;
285         struct ali_ircc_cb *self;
286         int dongle_id;
287         int err;
288                         
289         IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __func__);
290
291         if (i >= ARRAY_SIZE(dev_self)) {
292                 IRDA_ERROR("%s(), maximum number of supported chips reached!\n",
293                            __func__);
294                 return -ENOMEM;
295         }
296         
297         /* Set FIR FIFO and DMA Threshold */
298         if ((ali_ircc_setup(info)) == -1)
299                 return -1;
300                 
301         dev = alloc_irdadev(sizeof(*self));
302         if (dev == NULL) {
303                 IRDA_ERROR("%s(), can't allocate memory for control block!\n",
304                            __func__);
305                 return -ENOMEM;
306         }
307
308         self = netdev_priv(dev);
309         self->netdev = dev;
310         spin_lock_init(&self->lock);
311    
312         /* Need to store self somewhere */
313         dev_self[i] = self;
314         self->index = i;
315
316         /* Initialize IO */
317         self->io.cfg_base  = info->cfg_base;    /* In ali_ircc_probe_53 assign          */
318         self->io.fir_base  = info->fir_base;    /* info->sir_base = info->fir_base      */
319         self->io.sir_base  = info->sir_base;    /* ALi SIR and FIR use the same address */
320         self->io.irq       = info->irq;
321         self->io.fir_ext   = CHIP_IO_EXTENT;
322         self->io.dma       = info->dma;
323         self->io.fifo_size = 16;                /* SIR: 16, FIR: 32 Benjamin 2000/11/1 */
324         
325         /* Reserve the ioports that we need */
326         if (!request_region(self->io.fir_base, self->io.fir_ext,
327                             ALI_IRCC_DRIVER_NAME)) {
328                 IRDA_WARNING("%s(), can't get iobase of 0x%03x\n", __func__,
329                         self->io.fir_base);
330                 err = -ENODEV;
331                 goto err_out1;
332         }
333
334         /* Initialize QoS for this device */
335         irda_init_max_qos_capabilies(&self->qos);
336         
337         /* The only value we must override it the baudrate */
338         self->qos.baud_rate.bits = IR_9600|IR_19200|IR_38400|IR_57600|
339                 IR_115200|IR_576000|IR_1152000|(IR_4000000 << 8); // benjamin 2000/11/8 05:27PM
340                         
341         self->qos.min_turn_time.bits = qos_mtt_bits;
342                         
343         irda_qos_bits_to_value(&self->qos);
344         
345         /* Max DMA buffer size needed = (data_size + 6) * (window_size) + 6; */
346         self->rx_buff.truesize = 14384; 
347         self->tx_buff.truesize = 14384;
348
349         /* Allocate memory if needed */
350         self->rx_buff.head =
351                 dma_alloc_coherent(NULL, self->rx_buff.truesize,
352                                    &self->rx_buff_dma, GFP_KERNEL);
353         if (self->rx_buff.head == NULL) {
354                 err = -ENOMEM;
355                 goto err_out2;
356         }
357         memset(self->rx_buff.head, 0, self->rx_buff.truesize);
358         
359         self->tx_buff.head =
360                 dma_alloc_coherent(NULL, self->tx_buff.truesize,
361                                    &self->tx_buff_dma, GFP_KERNEL);
362         if (self->tx_buff.head == NULL) {
363                 err = -ENOMEM;
364                 goto err_out3;
365         }
366         memset(self->tx_buff.head, 0, self->tx_buff.truesize);
367
368         self->rx_buff.in_frame = FALSE;
369         self->rx_buff.state = OUTSIDE_FRAME;
370         self->tx_buff.data = self->tx_buff.head;
371         self->rx_buff.data = self->rx_buff.head;
372         
373         /* Reset Tx queue info */
374         self->tx_fifo.len = self->tx_fifo.ptr = self->tx_fifo.free = 0;
375         self->tx_fifo.tail = self->tx_buff.head;
376
377         /* Override the network functions we need to use */
378         dev->netdev_ops = &ali_ircc_sir_ops;
379
380         err = register_netdev(dev);
381         if (err) {
382                 IRDA_ERROR("%s(), register_netdev() failed!\n", __func__);
383                 goto err_out4;
384         }
385         IRDA_MESSAGE("IrDA: Registered device %s\n", dev->name);
386
387         /* Check dongle id */
388         dongle_id = ali_ircc_read_dongle_id(i, info);
389         IRDA_MESSAGE("%s(), %s, Found dongle: %s\n", __func__,
390                      ALI_IRCC_DRIVER_NAME, dongle_types[dongle_id]);
391                 
392         self->io.dongle_id = dongle_id;
393
394         IRDA_DEBUG(2, "%s(), ----------------- End -----------------\n", __func__);
395         
396         return 0;
397
398  err_out4:
399         dma_free_coherent(NULL, self->tx_buff.truesize,
400                           self->tx_buff.head, self->tx_buff_dma);
401  err_out3:
402         dma_free_coherent(NULL, self->rx_buff.truesize,
403                           self->rx_buff.head, self->rx_buff_dma);
404  err_out2:
405         release_region(self->io.fir_base, self->io.fir_ext);
406  err_out1:
407         dev_self[i] = NULL;
408         free_netdev(dev);
409         return err;
410 }
411
412
413 /*
414  * Function ali_ircc_close (self)
415  *
416  *    Close driver instance
417  *
418  */
419 static int __exit ali_ircc_close(struct ali_ircc_cb *self)
420 {
421         int iobase;
422
423         IRDA_DEBUG(4, "%s(), ---------------- Start ----------------\n", __func__);
424
425         IRDA_ASSERT(self != NULL, return -1;);
426
427         iobase = self->io.fir_base;
428
429         /* Remove netdevice */
430         unregister_netdev(self->netdev);
431
432         /* Release the PORT that this driver is using */
433         IRDA_DEBUG(4, "%s(), Releasing Region %03x\n", __func__, self->io.fir_base);
434         release_region(self->io.fir_base, self->io.fir_ext);
435
436         if (self->tx_buff.head)
437                 dma_free_coherent(NULL, self->tx_buff.truesize,
438                                   self->tx_buff.head, self->tx_buff_dma);
439         
440         if (self->rx_buff.head)
441                 dma_free_coherent(NULL, self->rx_buff.truesize,
442                                   self->rx_buff.head, self->rx_buff_dma);
443
444         dev_self[self->index] = NULL;
445         free_netdev(self->netdev);
446         
447         IRDA_DEBUG(2, "%s(), ----------------- End -----------------\n", __func__);
448         
449         return 0;
450 }
451
452 /*
453  * Function ali_ircc_init_43 (chip, info)
454  *
455  *    Initialize the ALi M1543 chip. 
456  */
457 static int ali_ircc_init_43(ali_chip_t *chip, chipio_t *info) 
458 {
459         /* All controller information like I/O address, DMA channel, IRQ
460          * are set by BIOS
461          */
462         
463         return 0;
464 }
465
466 /*
467  * Function ali_ircc_init_53 (chip, info)
468  *
469  *    Initialize the ALi M1535 chip. 
470  */
471 static int ali_ircc_init_53(ali_chip_t *chip, chipio_t *info) 
472 {
473         /* All controller information like I/O address, DMA channel, IRQ
474          * are set by BIOS
475          */
476         
477         return 0;
478 }
479
480 /*
481  * Function ali_ircc_probe_53 (chip, info)
482  *      
483  *      Probes for the ALi M1535D or M1535
484  */
485 static int ali_ircc_probe_53(ali_chip_t *chip, chipio_t *info)
486 {
487         int cfg_base = info->cfg_base;
488         int hi, low, reg;
489         
490         IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __func__);
491         
492         /* Enter Configuration */
493         outb(chip->entr1, cfg_base);
494         outb(chip->entr2, cfg_base);
495         
496         /* Select Logical Device 5 Registers (UART2) */
497         outb(0x07, cfg_base);
498         outb(0x05, cfg_base+1);
499         
500         /* Read address control register */
501         outb(0x60, cfg_base);
502         hi = inb(cfg_base+1);   
503         outb(0x61, cfg_base);
504         low = inb(cfg_base+1);
505         info->fir_base = (hi<<8) + low;
506         
507         info->sir_base = info->fir_base;
508         
509         IRDA_DEBUG(2, "%s(), probing fir_base=0x%03x\n", __func__, info->fir_base);
510                 
511         /* Read IRQ control register */
512         outb(0x70, cfg_base);
513         reg = inb(cfg_base+1);
514         info->irq = reg & 0x0f;
515         IRDA_DEBUG(2, "%s(), probing irq=%d\n", __func__, info->irq);
516         
517         /* Read DMA channel */
518         outb(0x74, cfg_base);
519         reg = inb(cfg_base+1);
520         info->dma = reg & 0x07;
521         
522         if(info->dma == 0x04)
523                 IRDA_WARNING("%s(), No DMA channel assigned !\n", __func__);
524         else
525                 IRDA_DEBUG(2, "%s(), probing dma=%d\n", __func__, info->dma);
526         
527         /* Read Enabled Status */
528         outb(0x30, cfg_base);
529         reg = inb(cfg_base+1);
530         info->enabled = (reg & 0x80) && (reg & 0x01);
531         IRDA_DEBUG(2, "%s(), probing enabled=%d\n", __func__, info->enabled);
532         
533         /* Read Power Status */
534         outb(0x22, cfg_base);
535         reg = inb(cfg_base+1);
536         info->suspended = (reg & 0x20);
537         IRDA_DEBUG(2, "%s(), probing suspended=%d\n", __func__, info->suspended);
538         
539         /* Exit configuration */
540         outb(0xbb, cfg_base);
541                 
542         IRDA_DEBUG(2, "%s(), ----------------- End -----------------\n", __func__);
543         
544         return 0;       
545 }
546
547 /*
548  * Function ali_ircc_setup (info)
549  *
550  *      Set FIR FIFO and DMA Threshold
551  *      Returns non-negative on success.
552  *
553  */
554 static int ali_ircc_setup(chipio_t *info)
555 {
556         unsigned char tmp;
557         int version;
558         int iobase = info->fir_base;
559         
560         IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __func__);
561         
562         /* Locking comments :
563          * Most operations here need to be protected. We are called before
564          * the device instance is created in ali_ircc_open(), therefore 
565          * nobody can bother us - Jean II */
566
567         /* Switch to FIR space */
568         SIR2FIR(iobase);
569         
570         /* Master Reset */
571         outb(0x40, iobase+FIR_MCR); // benjamin 2000/11/30 11:45AM
572         
573         /* Read FIR ID Version Register */
574         switch_bank(iobase, BANK3);
575         version = inb(iobase+FIR_ID_VR);
576         
577         /* Should be 0x00 in the M1535/M1535D */
578         if(version != 0x00)
579         {
580                 IRDA_ERROR("%s, Wrong chip version %02x\n",
581                            ALI_IRCC_DRIVER_NAME, version);
582                 return -1;
583         }
584         
585         /* Set FIR FIFO Threshold Register */
586         switch_bank(iobase, BANK1);
587         outb(RX_FIFO_Threshold, iobase+FIR_FIFO_TR);
588         
589         /* Set FIR DMA Threshold Register */
590         outb(RX_DMA_Threshold, iobase+FIR_DMA_TR);
591         
592         /* CRC enable */
593         switch_bank(iobase, BANK2);
594         outb(inb(iobase+FIR_IRDA_CR) | IRDA_CR_CRC, iobase+FIR_IRDA_CR);
595         
596         /* NDIS driver set TX Length here BANK2 Alias 3, Alias4*/
597         
598         /* Switch to Bank 0 */
599         switch_bank(iobase, BANK0);
600         
601         tmp = inb(iobase+FIR_LCR_B);
602         tmp &=~0x20; // disable SIP
603         tmp |= 0x80; // these two steps make RX mode
604         tmp &= 0xbf;    
605         outb(tmp, iobase+FIR_LCR_B);
606                 
607         /* Disable Interrupt */
608         outb(0x00, iobase+FIR_IER);
609         
610         
611         /* Switch to SIR space */
612         FIR2SIR(iobase);
613         
614         IRDA_MESSAGE("%s, driver loaded (Benjamin Kong)\n",
615                      ALI_IRCC_DRIVER_NAME);
616         
617         /* Enable receive interrupts */ 
618         // outb(UART_IER_RDI, iobase+UART_IER); //benjamin 2000/11/23 01:25PM
619         // Turn on the interrupts in ali_ircc_net_open
620         
621         IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __func__);
622         
623         return 0;
624 }
625
626 /*
627  * Function ali_ircc_read_dongle_id (int index, info)
628  *
629  * Try to read dongle indentification. This procedure needs to be executed
630  * once after power-on/reset. It also needs to be used whenever you suspect
631  * that the user may have plugged/unplugged the IrDA Dongle.
632  */
633 static int ali_ircc_read_dongle_id (int i, chipio_t *info)
634 {
635         int dongle_id, reg;
636         int cfg_base = info->cfg_base;
637         
638         IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __func__);
639                 
640         /* Enter Configuration */
641         outb(chips[i].entr1, cfg_base);
642         outb(chips[i].entr2, cfg_base);
643         
644         /* Select Logical Device 5 Registers (UART2) */
645         outb(0x07, cfg_base);
646         outb(0x05, cfg_base+1);
647         
648         /* Read Dongle ID */
649         outb(0xf0, cfg_base);
650         reg = inb(cfg_base+1);  
651         dongle_id = ((reg>>6)&0x02) | ((reg>>5)&0x01);
652         IRDA_DEBUG(2, "%s(), probing dongle_id=%d, dongle_types=%s\n", __func__,
653                 dongle_id, dongle_types[dongle_id]);
654         
655         /* Exit configuration */
656         outb(0xbb, cfg_base);
657                         
658         IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __func__);
659         
660         return dongle_id;
661 }
662
663 /*
664  * Function ali_ircc_interrupt (irq, dev_id, regs)
665  *
666  *    An interrupt from the chip has arrived. Time to do some work
667  *
668  */
669 static irqreturn_t ali_ircc_interrupt(int irq, void *dev_id)
670 {
671         struct net_device *dev = dev_id;
672         struct ali_ircc_cb *self;
673         int ret;
674                 
675         IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __func__);
676                 
677         self = netdev_priv(dev);
678         
679         spin_lock(&self->lock);
680         
681         /* Dispatch interrupt handler for the current speed */
682         if (self->io.speed > 115200)
683                 ret = ali_ircc_fir_interrupt(self);
684         else
685                 ret = ali_ircc_sir_interrupt(self);
686                 
687         spin_unlock(&self->lock);
688         
689         IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __func__);
690         return ret;
691 }
692 /*
693  * Function ali_ircc_fir_interrupt(irq, struct ali_ircc_cb *self)
694  *
695  *    Handle MIR/FIR interrupt
696  *
697  */
698 static irqreturn_t ali_ircc_fir_interrupt(struct ali_ircc_cb *self)
699 {
700         __u8 eir, OldMessageCount;
701         int iobase, tmp;
702         
703         IRDA_DEBUG(1, "%s(), ---------------- Start ----------------\n", __func__);
704         
705         iobase = self->io.fir_base;
706         
707         switch_bank(iobase, BANK0);     
708         self->InterruptID = inb(iobase+FIR_IIR);                
709         self->BusStatus = inb(iobase+FIR_BSR);  
710         
711         OldMessageCount = (self->LineStatus + 1) & 0x07;
712         self->LineStatus = inb(iobase+FIR_LSR); 
713         //self->ier = inb(iobase+FIR_IER);              2000/12/1 04:32PM
714         eir = self->InterruptID & self->ier; /* Mask out the interesting ones */ 
715         
716         IRDA_DEBUG(1, "%s(), self->InterruptID = %x\n", __func__,self->InterruptID);
717         IRDA_DEBUG(1, "%s(), self->LineStatus = %x\n", __func__,self->LineStatus);
718         IRDA_DEBUG(1, "%s(), self->ier = %x\n", __func__,self->ier);
719         IRDA_DEBUG(1, "%s(), eir = %x\n", __func__,eir);
720         
721         /* Disable interrupts */
722          SetCOMInterrupts(self, FALSE);
723         
724         /* Tx or Rx Interrupt */
725         
726         if (eir & IIR_EOM) 
727         {               
728                 if (self->io.direction == IO_XMIT) /* TX */
729                 {
730                         IRDA_DEBUG(1, "%s(), ******* IIR_EOM (Tx) *******\n", __func__);
731                         
732                         if(ali_ircc_dma_xmit_complete(self))
733                         {
734                                 if (irda_device_txqueue_empty(self->netdev)) 
735                                 {
736                                         /* Prepare for receive */
737                                         ali_ircc_dma_receive(self);                                     
738                                         self->ier = IER_EOM;                                                                    
739                                 }
740                         }
741                         else
742                         {
743                                 self->ier = IER_EOM;                                    
744                         }
745                                                                         
746                 }       
747                 else /* RX */
748                 {
749                         IRDA_DEBUG(1, "%s(), ******* IIR_EOM (Rx) *******\n", __func__);
750                         
751                         if(OldMessageCount > ((self->LineStatus+1) & 0x07))
752                         {
753                                 self->rcvFramesOverflow = TRUE; 
754                                 IRDA_DEBUG(1, "%s(), ******* self->rcvFramesOverflow = TRUE ******** \n", __func__);
755                         }
756                                                 
757                         if (ali_ircc_dma_receive_complete(self))
758                         {
759                                 IRDA_DEBUG(1, "%s(), ******* receive complete ******** \n", __func__);
760                                 
761                                 self->ier = IER_EOM;                            
762                         }
763                         else
764                         {
765                                 IRDA_DEBUG(1, "%s(), ******* Not receive complete ******** \n", __func__);
766                                 
767                                 self->ier = IER_EOM | IER_TIMER;                                                                
768                         }       
769                 
770                 }               
771         }
772         /* Timer Interrupt */
773         else if (eir & IIR_TIMER)
774         {       
775                 if(OldMessageCount > ((self->LineStatus+1) & 0x07))
776                 {
777                         self->rcvFramesOverflow = TRUE; 
778                         IRDA_DEBUG(1, "%s(), ******* self->rcvFramesOverflow = TRUE ******* \n", __func__);
779                 }
780                 /* Disable Timer */
781                 switch_bank(iobase, BANK1);
782                 tmp = inb(iobase+FIR_CR);
783                 outb( tmp& ~CR_TIMER_EN, iobase+FIR_CR);
784                 
785                 /* Check if this is a Tx timer interrupt */
786                 if (self->io.direction == IO_XMIT)
787                 {
788                         ali_ircc_dma_xmit(self);
789                         
790                         /* Interrupt on EOM */
791                         self->ier = IER_EOM;
792                                                                         
793                 }
794                 else /* Rx */
795                 {
796                         if(ali_ircc_dma_receive_complete(self)) 
797                         {
798                                 self->ier = IER_EOM;
799                         }
800                         else
801                         {
802                                 self->ier = IER_EOM | IER_TIMER;
803                         }       
804                 }               
805         }
806         
807         /* Restore Interrupt */ 
808         SetCOMInterrupts(self, TRUE);   
809                 
810         IRDA_DEBUG(1, "%s(), ----------------- End ---------------\n", __func__);
811         return IRQ_RETVAL(eir);
812 }
813
814 /*
815  * Function ali_ircc_sir_interrupt (irq, self, eir)
816  *
817  *    Handle SIR interrupt
818  *
819  */
820 static irqreturn_t ali_ircc_sir_interrupt(struct ali_ircc_cb *self)
821 {
822         int iobase;
823         int iir, lsr;
824         
825         IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __func__);
826         
827         iobase = self->io.sir_base;
828
829         iir = inb(iobase+UART_IIR) & UART_IIR_ID;
830         if (iir) {      
831                 /* Clear interrupt */
832                 lsr = inb(iobase+UART_LSR);
833
834                 IRDA_DEBUG(4, "%s(), iir=%02x, lsr=%02x, iobase=%#x\n", __func__,
835                            iir, lsr, iobase);
836
837                 switch (iir) 
838                 {
839                         case UART_IIR_RLSI:
840                                 IRDA_DEBUG(2, "%s(), RLSI\n", __func__);
841                                 break;
842                         case UART_IIR_RDI:
843                                 /* Receive interrupt */
844                                 ali_ircc_sir_receive(self);
845                                 break;
846                         case UART_IIR_THRI:
847                                 if (lsr & UART_LSR_THRE)
848                                 {
849                                         /* Transmitter ready for data */
850                                         ali_ircc_sir_write_wakeup(self);                                
851                                 }                               
852                                 break;
853                         default:
854                                 IRDA_DEBUG(0, "%s(), unhandled IIR=%#x\n", __func__, iir);
855                                 break;
856                 } 
857                 
858         }
859         
860         
861         IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __func__);
862
863         return IRQ_RETVAL(iir);
864 }
865
866
867 /*
868  * Function ali_ircc_sir_receive (self)
869  *
870  *    Receive one frame from the infrared port
871  *
872  */
873 static void ali_ircc_sir_receive(struct ali_ircc_cb *self) 
874 {
875         int boguscount = 0;
876         int iobase;
877         
878         IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __func__);
879         IRDA_ASSERT(self != NULL, return;);
880
881         iobase = self->io.sir_base;
882
883         /*  
884          * Receive all characters in Rx FIFO, unwrap and unstuff them. 
885          * async_unwrap_char will deliver all found frames  
886          */
887         do {
888                 async_unwrap_char(self->netdev, &self->netdev->stats, &self->rx_buff,
889                                   inb(iobase+UART_RX));
890
891                 /* Make sure we don't stay here too long */
892                 if (boguscount++ > 32) {
893                         IRDA_DEBUG(2,"%s(), breaking!\n", __func__);
894                         break;
895                 }
896         } while (inb(iobase+UART_LSR) & UART_LSR_DR);   
897         
898         IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __func__ );
899 }
900
901 /*
902  * Function ali_ircc_sir_write_wakeup (tty)
903  *
904  *    Called by the driver when there's room for more data.  If we have
905  *    more packets to send, we send them here.
906  *
907  */
908 static void ali_ircc_sir_write_wakeup(struct ali_ircc_cb *self)
909 {
910         int actual = 0;
911         int iobase;     
912
913         IRDA_ASSERT(self != NULL, return;);
914
915         IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __func__ );
916         
917         iobase = self->io.sir_base;
918
919         /* Finished with frame?  */
920         if (self->tx_buff.len > 0)  
921         {
922                 /* Write data left in transmit buffer */
923                 actual = ali_ircc_sir_write(iobase, self->io.fifo_size, 
924                                       self->tx_buff.data, self->tx_buff.len);
925                 self->tx_buff.data += actual;
926                 self->tx_buff.len  -= actual;
927         } 
928         else 
929         {
930                 if (self->new_speed) 
931                 {
932                         /* We must wait until all data are gone */
933                         while(!(inb(iobase+UART_LSR) & UART_LSR_TEMT))
934                                 IRDA_DEBUG(1, "%s(), UART_LSR_THRE\n", __func__ );
935                         
936                         IRDA_DEBUG(1, "%s(), Changing speed! self->new_speed = %d\n", __func__ , self->new_speed);
937                         ali_ircc_change_speed(self, self->new_speed);
938                         self->new_speed = 0;                    
939                         
940                         // benjamin 2000/11/10 06:32PM
941                         if (self->io.speed > 115200)
942                         {
943                                 IRDA_DEBUG(2, "%s(), ali_ircc_change_speed from UART_LSR_TEMT \n", __func__ );
944                                         
945                                 self->ier = IER_EOM;
946                                 // SetCOMInterrupts(self, TRUE);                                                        
947                                 return;                                                 
948                         }
949                 }
950                 else
951                 {
952                         netif_wake_queue(self->netdev); 
953                 }
954                         
955                 self->netdev->stats.tx_packets++;
956                 
957                 /* Turn on receive interrupts */
958                 outb(UART_IER_RDI, iobase+UART_IER);
959         }
960                 
961         IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __func__ );
962 }
963
964 static void ali_ircc_change_speed(struct ali_ircc_cb *self, __u32 baud)
965 {
966         struct net_device *dev = self->netdev;
967         int iobase;
968         
969         IRDA_DEBUG(1, "%s(), ---------------- Start ----------------\n", __func__ );
970         
971         IRDA_DEBUG(2, "%s(), setting speed = %d \n", __func__ , baud);
972         
973         /* This function *must* be called with irq off and spin-lock.
974          * - Jean II */
975
976         iobase = self->io.fir_base;
977         
978         SetCOMInterrupts(self, FALSE); // 2000/11/24 11:43AM
979         
980         /* Go to MIR, FIR Speed */
981         if (baud > 115200)
982         {
983                 
984                                         
985                 ali_ircc_fir_change_speed(self, baud);                  
986                 
987                 /* Install FIR xmit handler*/
988                 dev->netdev_ops = &ali_ircc_fir_ops;
989                                 
990                 /* Enable Interuupt */
991                 self->ier = IER_EOM; // benjamin 2000/11/20 07:24PM                                     
992                                 
993                 /* Be ready for incomming frames */
994                 ali_ircc_dma_receive(self);     // benajmin 2000/11/8 07:46PM not complete
995         }       
996         /* Go to SIR Speed */
997         else
998         {
999                 ali_ircc_sir_change_speed(self, baud);
1000                                 
1001                 /* Install SIR xmit handler*/
1002                 dev->netdev_ops = &ali_ircc_sir_ops;
1003         }
1004         
1005                 
1006         SetCOMInterrupts(self, TRUE);   // 2000/11/24 11:43AM
1007                 
1008         netif_wake_queue(self->netdev); 
1009         
1010         IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __func__ );
1011 }
1012
1013 static void ali_ircc_fir_change_speed(struct ali_ircc_cb *priv, __u32 baud)
1014 {
1015                 
1016         int iobase; 
1017         struct ali_ircc_cb *self = (struct ali_ircc_cb *) priv;
1018         struct net_device *dev;
1019
1020         IRDA_DEBUG(1, "%s(), ---------------- Start ----------------\n", __func__ );
1021                 
1022         IRDA_ASSERT(self != NULL, return;);
1023
1024         dev = self->netdev;
1025         iobase = self->io.fir_base;
1026         
1027         IRDA_DEBUG(1, "%s(), self->io.speed = %d, change to speed = %d\n", __func__ ,self->io.speed,baud);
1028         
1029         /* Come from SIR speed */
1030         if(self->io.speed <=115200)
1031         {
1032                 SIR2FIR(iobase);
1033         }
1034                 
1035         /* Update accounting for new speed */
1036         self->io.speed = baud;
1037                 
1038         // Set Dongle Speed mode
1039         ali_ircc_change_dongle_speed(self, baud);
1040                 
1041         IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __func__ );
1042 }
1043
1044 /*
1045  * Function ali_sir_change_speed (self, speed)
1046  *
1047  *    Set speed of IrDA port to specified baudrate
1048  *
1049  */
1050 static void ali_ircc_sir_change_speed(struct ali_ircc_cb *priv, __u32 speed)
1051 {
1052         struct ali_ircc_cb *self = (struct ali_ircc_cb *) priv;
1053         unsigned long flags;
1054         int iobase; 
1055         int fcr;    /* FIFO control reg */
1056         int lcr;    /* Line control reg */
1057         int divisor;
1058
1059         IRDA_DEBUG(1, "%s(), ---------------- Start ----------------\n", __func__ );
1060         
1061         IRDA_DEBUG(1, "%s(), Setting speed to: %d\n", __func__ , speed);
1062
1063         IRDA_ASSERT(self != NULL, return;);
1064
1065         iobase = self->io.sir_base;
1066         
1067         /* Come from MIR or FIR speed */
1068         if(self->io.speed >115200)
1069         {       
1070                 // Set Dongle Speed mode first
1071                 ali_ircc_change_dongle_speed(self, speed);
1072                         
1073                 FIR2SIR(iobase);
1074         }
1075                 
1076         // Clear Line and Auxiluary status registers 2000/11/24 11:47AM
1077                 
1078         inb(iobase+UART_LSR);
1079         inb(iobase+UART_SCR);
1080                 
1081         /* Update accounting for new speed */
1082         self->io.speed = speed;
1083
1084         spin_lock_irqsave(&self->lock, flags);
1085
1086         divisor = 115200/speed;
1087         
1088         fcr = UART_FCR_ENABLE_FIFO;
1089
1090         /* 
1091          * Use trigger level 1 to avoid 3 ms. timeout delay at 9600 bps, and
1092          * almost 1,7 ms at 19200 bps. At speeds above that we can just forget
1093          * about this timeout since it will always be fast enough. 
1094          */
1095         if (self->io.speed < 38400)
1096                 fcr |= UART_FCR_TRIGGER_1;
1097         else 
1098                 fcr |= UART_FCR_TRIGGER_14;
1099         
1100         /* IrDA ports use 8N1 */
1101         lcr = UART_LCR_WLEN8;
1102         
1103         outb(UART_LCR_DLAB | lcr, iobase+UART_LCR); /* Set DLAB */
1104         outb(divisor & 0xff,      iobase+UART_DLL); /* Set speed */
1105         outb(divisor >> 8,        iobase+UART_DLM);
1106         outb(lcr,                 iobase+UART_LCR); /* Set 8N1  */
1107         outb(fcr,                 iobase+UART_FCR); /* Enable FIFO's */
1108
1109         /* without this, the conection will be broken after come back from FIR speed,
1110            but with this, the SIR connection is harder to established */
1111         outb((UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2), iobase+UART_MCR);
1112         
1113         spin_unlock_irqrestore(&self->lock, flags);
1114         
1115         IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __func__ );
1116 }
1117
1118 static void ali_ircc_change_dongle_speed(struct ali_ircc_cb *priv, int speed)
1119 {
1120         
1121         struct ali_ircc_cb *self = (struct ali_ircc_cb *) priv;
1122         int iobase,dongle_id;
1123         int tmp = 0;
1124                         
1125         IRDA_DEBUG(1, "%s(), ---------------- Start ----------------\n", __func__ );
1126         
1127         iobase = self->io.fir_base;     /* or iobase = self->io.sir_base; */
1128         dongle_id = self->io.dongle_id;
1129         
1130         /* We are already locked, no need to do it again */
1131                 
1132         IRDA_DEBUG(1, "%s(), Set Speed for %s , Speed = %d\n", __func__ , dongle_types[dongle_id], speed);
1133         
1134         switch_bank(iobase, BANK2);
1135         tmp = inb(iobase+FIR_IRDA_CR);
1136                 
1137         /* IBM type dongle */
1138         if(dongle_id == 0)
1139         {                               
1140                 if(speed == 4000000)
1141                 {
1142                         //            __ __     
1143                         // SD/MODE __|     |__ __
1144                         //               __ __ 
1145                         // IRTX    __ __|     |__
1146                         //         T1 T2 T3 T4 T5
1147                         
1148                         tmp &=  ~IRDA_CR_HDLC;          // HDLC=0
1149                         tmp |= IRDA_CR_CRC;             // CRC=1
1150                         
1151                         switch_bank(iobase, BANK2);
1152                         outb(tmp, iobase+FIR_IRDA_CR);
1153                         
1154                         // T1 -> SD/MODE:0 IRTX:0
1155                         tmp &= ~0x09;
1156                         tmp |= 0x02;
1157                         outb(tmp, iobase+FIR_IRDA_CR);
1158                         udelay(2);
1159                         
1160                         // T2 -> SD/MODE:1 IRTX:0
1161                         tmp &= ~0x01;
1162                         tmp |= 0x0a;
1163                         outb(tmp, iobase+FIR_IRDA_CR);
1164                         udelay(2);
1165                         
1166                         // T3 -> SD/MODE:1 IRTX:1
1167                         tmp |= 0x0b;
1168                         outb(tmp, iobase+FIR_IRDA_CR);
1169                         udelay(2);
1170                         
1171                         // T4 -> SD/MODE:0 IRTX:1
1172                         tmp &= ~0x08;
1173                         tmp |= 0x03;
1174                         outb(tmp, iobase+FIR_IRDA_CR);
1175                         udelay(2);
1176                         
1177                         // T5 -> SD/MODE:0 IRTX:0
1178                         tmp &= ~0x09;
1179                         tmp |= 0x02;
1180                         outb(tmp, iobase+FIR_IRDA_CR);
1181                         udelay(2);
1182                         
1183                         // reset -> Normal TX output Signal
1184                         outb(tmp & ~0x02, iobase+FIR_IRDA_CR);                          
1185                 }
1186                 else /* speed <=1152000 */
1187                 {       
1188                         //            __        
1189                         // SD/MODE __|  |__
1190                         //
1191                         // IRTX    ________
1192                         //         T1 T2 T3  
1193                         
1194                         /* MIR 115200, 57600 */
1195                         if (speed==1152000)
1196                         {
1197                                 tmp |= 0xA0;       //HDLC=1, 1.152Mbps=1
1198                         }
1199                         else
1200                         {
1201                                 tmp &=~0x80;       //HDLC 0.576Mbps
1202                                 tmp |= 0x20;       //HDLC=1,
1203                         }                       
1204                         
1205                         tmp |= IRDA_CR_CRC;             // CRC=1
1206                         
1207                         switch_bank(iobase, BANK2);
1208                         outb(tmp, iobase+FIR_IRDA_CR);
1209                                                 
1210                         /* MIR 115200, 57600 */ 
1211                                                 
1212                         //switch_bank(iobase, BANK2);                   
1213                         // T1 -> SD/MODE:0 IRTX:0
1214                         tmp &= ~0x09;
1215                         tmp |= 0x02;
1216                         outb(tmp, iobase+FIR_IRDA_CR);
1217                         udelay(2);
1218                         
1219                         // T2 -> SD/MODE:1 IRTX:0
1220                         tmp &= ~0x01;     
1221                         tmp |= 0x0a;      
1222                         outb(tmp, iobase+FIR_IRDA_CR);
1223                         
1224                         // T3 -> SD/MODE:0 IRTX:0
1225                         tmp &= ~0x09;
1226                         tmp |= 0x02;
1227                         outb(tmp, iobase+FIR_IRDA_CR);
1228                         udelay(2);
1229                         
1230                         // reset -> Normal TX output Signal
1231                         outb(tmp & ~0x02, iobase+FIR_IRDA_CR);                                                  
1232                 }               
1233         }
1234         else if (dongle_id == 1) /* HP HDSL-3600 */
1235         {
1236                 switch(speed)
1237                 {
1238                 case 4000000:
1239                         tmp &=  ~IRDA_CR_HDLC;  // HDLC=0
1240                         break;  
1241                         
1242                 case 1152000:
1243                         tmp |= 0xA0;            // HDLC=1, 1.152Mbps=1
1244                         break;
1245                         
1246                 case 576000:
1247                         tmp &=~0x80;            // HDLC 0.576Mbps
1248                         tmp |= 0x20;            // HDLC=1,
1249                         break;
1250                 }                       
1251                         
1252                 tmp |= IRDA_CR_CRC;             // CRC=1
1253                         
1254                 switch_bank(iobase, BANK2);
1255                 outb(tmp, iobase+FIR_IRDA_CR);          
1256         }
1257         else /* HP HDSL-1100 */
1258         {
1259                 if(speed <= 115200) /* SIR */
1260                 {
1261                         
1262                         tmp &= ~IRDA_CR_FIR_SIN;        // HP sin select = 0
1263                         
1264                         switch_bank(iobase, BANK2);
1265                         outb(tmp, iobase+FIR_IRDA_CR);                  
1266                 }
1267                 else /* MIR FIR */
1268                 {       
1269                         
1270                         switch(speed)
1271                         {
1272                         case 4000000:
1273                                 tmp &=  ~IRDA_CR_HDLC;  // HDLC=0
1274                                 break;  
1275                         
1276                         case 1152000:
1277                                 tmp |= 0xA0;            // HDLC=1, 1.152Mbps=1
1278                                 break;
1279                         
1280                         case 576000:
1281                                 tmp &=~0x80;            // HDLC 0.576Mbps
1282                                 tmp |= 0x20;            // HDLC=1,
1283                                 break;
1284                         }                       
1285                         
1286                         tmp |= IRDA_CR_CRC;             // CRC=1
1287                         tmp |= IRDA_CR_FIR_SIN;         // HP sin select = 1
1288                         
1289                         switch_bank(iobase, BANK2);
1290                         outb(tmp, iobase+FIR_IRDA_CR);                  
1291                 }
1292         }
1293                         
1294         switch_bank(iobase, BANK0);
1295         
1296         IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __func__ );
1297 }
1298
1299 /*
1300  * Function ali_ircc_sir_write (driver)
1301  *
1302  *    Fill Tx FIFO with transmit data
1303  *
1304  */
1305 static int ali_ircc_sir_write(int iobase, int fifo_size, __u8 *buf, int len)
1306 {
1307         int actual = 0;
1308         
1309         IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __func__ );
1310                 
1311         /* Tx FIFO should be empty! */
1312         if (!(inb(iobase+UART_LSR) & UART_LSR_THRE)) {
1313                 IRDA_DEBUG(0, "%s(), failed, fifo not empty!\n", __func__ );
1314                 return 0;
1315         }
1316         
1317         /* Fill FIFO with current frame */
1318         while ((fifo_size-- > 0) && (actual < len)) {
1319                 /* Transmit next byte */
1320                 outb(buf[actual], iobase+UART_TX);
1321
1322                 actual++;
1323         }
1324         
1325         IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __func__ );
1326         return actual;
1327 }
1328
1329 /*
1330  * Function ali_ircc_net_open (dev)
1331  *
1332  *    Start the device
1333  *
1334  */
1335 static int ali_ircc_net_open(struct net_device *dev)
1336 {
1337         struct ali_ircc_cb *self;
1338         int iobase;
1339         char hwname[32];
1340                 
1341         IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __func__ );
1342         
1343         IRDA_ASSERT(dev != NULL, return -1;);
1344         
1345         self = netdev_priv(dev);
1346         
1347         IRDA_ASSERT(self != NULL, return 0;);
1348         
1349         iobase = self->io.fir_base;
1350         
1351         /* Request IRQ and install Interrupt Handler */
1352         if (request_irq(self->io.irq, ali_ircc_interrupt, 0, dev->name, dev)) 
1353         {
1354                 IRDA_WARNING("%s, unable to allocate irq=%d\n",
1355                              ALI_IRCC_DRIVER_NAME,
1356                              self->io.irq);
1357                 return -EAGAIN;
1358         }
1359         
1360         /*
1361          * Always allocate the DMA channel after the IRQ, and clean up on 
1362          * failure.
1363          */
1364         if (request_dma(self->io.dma, dev->name)) {
1365                 IRDA_WARNING("%s, unable to allocate dma=%d\n",
1366                              ALI_IRCC_DRIVER_NAME,
1367                              self->io.dma);
1368                 free_irq(self->io.irq, self);
1369                 return -EAGAIN;
1370         }
1371         
1372         /* Turn on interrups */
1373         outb(UART_IER_RDI , iobase+UART_IER);
1374
1375         /* Ready to play! */
1376         netif_start_queue(dev); //benjamin by irport
1377         
1378         /* Give self a hardware name */
1379         sprintf(hwname, "ALI-FIR @ 0x%03x", self->io.fir_base);
1380
1381         /* 
1382          * Open new IrLAP layer instance, now that everything should be
1383          * initialized properly 
1384          */
1385         self->irlap = irlap_open(dev, &self->qos, hwname);
1386                 
1387         IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __func__ );
1388         
1389         return 0;
1390 }
1391
1392 /*
1393  * Function ali_ircc_net_close (dev)
1394  *
1395  *    Stop the device
1396  *
1397  */
1398 static int ali_ircc_net_close(struct net_device *dev)
1399 {       
1400
1401         struct ali_ircc_cb *self;
1402         //int iobase;
1403                         
1404         IRDA_DEBUG(4, "%s(), ---------------- Start ----------------\n", __func__ );
1405                 
1406         IRDA_ASSERT(dev != NULL, return -1;);
1407
1408         self = netdev_priv(dev);
1409         IRDA_ASSERT(self != NULL, return 0;);
1410
1411         /* Stop device */
1412         netif_stop_queue(dev);
1413         
1414         /* Stop and remove instance of IrLAP */
1415         if (self->irlap)
1416                 irlap_close(self->irlap);
1417         self->irlap = NULL;
1418                 
1419         disable_dma(self->io.dma);
1420
1421         /* Disable interrupts */
1422         SetCOMInterrupts(self, FALSE);
1423                
1424         free_irq(self->io.irq, dev);
1425         free_dma(self->io.dma);
1426
1427         IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __func__ );
1428         
1429         return 0;
1430 }
1431
1432 /*
1433  * Function ali_ircc_fir_hard_xmit (skb, dev)
1434  *
1435  *    Transmit the frame
1436  *
1437  */
1438 static int ali_ircc_fir_hard_xmit(struct sk_buff *skb, struct net_device *dev)
1439 {
1440         struct ali_ircc_cb *self;
1441         unsigned long flags;
1442         int iobase;
1443         __u32 speed;
1444         int mtt, diff;
1445         
1446         IRDA_DEBUG(1, "%s(), ---------------- Start -----------------\n", __func__ );
1447         
1448         self = netdev_priv(dev);
1449         iobase = self->io.fir_base;
1450
1451         netif_stop_queue(dev);
1452         
1453         /* Make sure tests *& speed change are atomic */
1454         spin_lock_irqsave(&self->lock, flags);
1455         
1456         /* Note : you should make sure that speed changes are not going
1457          * to corrupt any outgoing frame. Look at nsc-ircc for the gory
1458          * details - Jean II */
1459
1460         /* Check if we need to change the speed */
1461         speed = irda_get_next_speed(skb);
1462         if ((speed != self->io.speed) && (speed != -1)) {
1463                 /* Check for empty frame */
1464                 if (!skb->len) {
1465                         ali_ircc_change_speed(self, speed); 
1466                         dev->trans_start = jiffies;
1467                         spin_unlock_irqrestore(&self->lock, flags);
1468                         dev_kfree_skb(skb);
1469                         return 0;
1470                 } else
1471                         self->new_speed = speed;
1472         }
1473
1474         /* Register and copy this frame to DMA memory */
1475         self->tx_fifo.queue[self->tx_fifo.free].start = self->tx_fifo.tail;
1476         self->tx_fifo.queue[self->tx_fifo.free].len = skb->len;
1477         self->tx_fifo.tail += skb->len;
1478
1479         dev->stats.tx_bytes += skb->len;
1480
1481         skb_copy_from_linear_data(skb, self->tx_fifo.queue[self->tx_fifo.free].start,
1482                       skb->len);
1483         self->tx_fifo.len++;
1484         self->tx_fifo.free++;
1485
1486         /* Start transmit only if there is currently no transmit going on */
1487         if (self->tx_fifo.len == 1) 
1488         {
1489                 /* Check if we must wait the min turn time or not */
1490                 mtt = irda_get_mtt(skb);
1491                                 
1492                 if (mtt) 
1493                 {
1494                         /* Check how much time we have used already */
1495                         do_gettimeofday(&self->now);
1496                         
1497                         diff = self->now.tv_usec - self->stamp.tv_usec;
1498                         /* self->stamp is set from ali_ircc_dma_receive_complete() */
1499                                                         
1500                         IRDA_DEBUG(1, "%s(), ******* diff = %d ******* \n", __func__ , diff);
1501                         
1502                         if (diff < 0) 
1503                                 diff += 1000000;
1504                         
1505                         /* Check if the mtt is larger than the time we have
1506                          * already used by all the protocol processing
1507                          */
1508                         if (mtt > diff)
1509                         {                               
1510                                 mtt -= diff;
1511                                                                 
1512                                 /* 
1513                                  * Use timer if delay larger than 1000 us, and
1514                                  * use udelay for smaller values which should
1515                                  * be acceptable
1516                                  */
1517                                 if (mtt > 500) 
1518                                 {
1519                                         /* Adjust for timer resolution */
1520                                         mtt = (mtt+250) / 500;  /* 4 discard, 5 get advanced, Let's round off */
1521                                         
1522                                         IRDA_DEBUG(1, "%s(), ************** mtt = %d ***********\n", __func__ , mtt);
1523                                         
1524                                         /* Setup timer */
1525                                         if (mtt == 1) /* 500 us */
1526                                         {
1527                                                 switch_bank(iobase, BANK1);
1528                                                 outb(TIMER_IIR_500, iobase+FIR_TIMER_IIR);
1529                                         }       
1530                                         else if (mtt == 2) /* 1 ms */
1531                                         {
1532                                                 switch_bank(iobase, BANK1);
1533                                                 outb(TIMER_IIR_1ms, iobase+FIR_TIMER_IIR);
1534                                         }                                       
1535                                         else /* > 2ms -> 4ms */
1536                                         {
1537                                                 switch_bank(iobase, BANK1);
1538                                                 outb(TIMER_IIR_2ms, iobase+FIR_TIMER_IIR);
1539                                         }
1540                                         
1541                                         
1542                                         /* Start timer */
1543                                         outb(inb(iobase+FIR_CR) | CR_TIMER_EN, iobase+FIR_CR);
1544                                         self->io.direction = IO_XMIT;
1545                                         
1546                                         /* Enable timer interrupt */
1547                                         self->ier = IER_TIMER;
1548                                         SetCOMInterrupts(self, TRUE);                                   
1549                                         
1550                                         /* Timer will take care of the rest */
1551                                         goto out; 
1552                                 } 
1553                                 else
1554                                         udelay(mtt);
1555                         } // if (if (mtt > diff)
1556                 }// if (mtt) 
1557                                 
1558                 /* Enable EOM interrupt */
1559                 self->ier = IER_EOM;
1560                 SetCOMInterrupts(self, TRUE);
1561                 
1562                 /* Transmit frame */
1563                 ali_ircc_dma_xmit(self);
1564         } // if (self->tx_fifo.len == 1) 
1565         
1566  out:
1567         
1568         /* Not busy transmitting anymore if window is not full */
1569         if (self->tx_fifo.free < MAX_TX_WINDOW)
1570                 netif_wake_queue(self->netdev);
1571         
1572         /* Restore bank register */
1573         switch_bank(iobase, BANK0);
1574
1575         dev->trans_start = jiffies;
1576         spin_unlock_irqrestore(&self->lock, flags);
1577         dev_kfree_skb(skb);
1578
1579         IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __func__ );
1580         return 0;       
1581 }
1582
1583
1584 static void ali_ircc_dma_xmit(struct ali_ircc_cb *self)
1585 {
1586         int iobase, tmp;
1587         unsigned char FIFO_OPTI, Hi, Lo;
1588         
1589         
1590         IRDA_DEBUG(1, "%s(), ---------------- Start -----------------\n", __func__ );
1591         
1592         iobase = self->io.fir_base;
1593         
1594         /* FIFO threshold , this method comes from NDIS5 code */
1595         
1596         if(self->tx_fifo.queue[self->tx_fifo.ptr].len < TX_FIFO_Threshold)
1597                 FIFO_OPTI = self->tx_fifo.queue[self->tx_fifo.ptr].len-1;
1598         else
1599                 FIFO_OPTI = TX_FIFO_Threshold;
1600         
1601         /* Disable DMA */
1602         switch_bank(iobase, BANK1);
1603         outb(inb(iobase+FIR_CR) & ~CR_DMA_EN, iobase+FIR_CR);
1604         
1605         self->io.direction = IO_XMIT;
1606         
1607         irda_setup_dma(self->io.dma, 
1608                        ((u8 *)self->tx_fifo.queue[self->tx_fifo.ptr].start -
1609                         self->tx_buff.head) + self->tx_buff_dma,
1610                        self->tx_fifo.queue[self->tx_fifo.ptr].len, 
1611                        DMA_TX_MODE);
1612                 
1613         /* Reset Tx FIFO */
1614         switch_bank(iobase, BANK0);
1615         outb(LCR_A_FIFO_RESET, iobase+FIR_LCR_A);
1616         
1617         /* Set Tx FIFO threshold */
1618         if (self->fifo_opti_buf!=FIFO_OPTI) 
1619         {
1620                 switch_bank(iobase, BANK1);
1621                 outb(FIFO_OPTI, iobase+FIR_FIFO_TR) ;
1622                 self->fifo_opti_buf=FIFO_OPTI;
1623         }
1624         
1625         /* Set Tx DMA threshold */
1626         switch_bank(iobase, BANK1);
1627         outb(TX_DMA_Threshold, iobase+FIR_DMA_TR);
1628         
1629         /* Set max Tx frame size */
1630         Hi = (self->tx_fifo.queue[self->tx_fifo.ptr].len >> 8) & 0x0f;
1631         Lo = self->tx_fifo.queue[self->tx_fifo.ptr].len & 0xff;
1632         switch_bank(iobase, BANK2);
1633         outb(Hi, iobase+FIR_TX_DSR_HI);
1634         outb(Lo, iobase+FIR_TX_DSR_LO);
1635         
1636         /* Disable SIP , Disable Brick Wall (we don't support in TX mode), Change to TX mode */
1637         switch_bank(iobase, BANK0);     
1638         tmp = inb(iobase+FIR_LCR_B);
1639         tmp &= ~0x20; // Disable SIP
1640         outb(((unsigned char)(tmp & 0x3f) | LCR_B_TX_MODE) & ~LCR_B_BW, iobase+FIR_LCR_B);
1641         IRDA_DEBUG(1, "%s(), ******* Change to TX mode: FIR_LCR_B = 0x%x ******* \n", __func__ , inb(iobase+FIR_LCR_B));
1642         
1643         outb(0, iobase+FIR_LSR);
1644                         
1645         /* Enable DMA and Burst Mode */
1646         switch_bank(iobase, BANK1);
1647         outb(inb(iobase+FIR_CR) | CR_DMA_EN | CR_DMA_BURST, iobase+FIR_CR);
1648         
1649         switch_bank(iobase, BANK0); 
1650         
1651         IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __func__ );
1652 }
1653
1654 static int  ali_ircc_dma_xmit_complete(struct ali_ircc_cb *self)
1655 {
1656         int iobase;
1657         int ret = TRUE;
1658         
1659         IRDA_DEBUG(1, "%s(), ---------------- Start -----------------\n", __func__ );
1660         
1661         iobase = self->io.fir_base;
1662         
1663         /* Disable DMA */
1664         switch_bank(iobase, BANK1);
1665         outb(inb(iobase+FIR_CR) & ~CR_DMA_EN, iobase+FIR_CR);
1666         
1667         /* Check for underrun! */
1668         switch_bank(iobase, BANK0);
1669         if((inb(iobase+FIR_LSR) & LSR_FRAME_ABORT) == LSR_FRAME_ABORT)
1670         
1671         {
1672                 IRDA_ERROR("%s(), ********* LSR_FRAME_ABORT *********\n", __func__);
1673                 self->netdev->stats.tx_errors++;
1674                 self->netdev->stats.tx_fifo_errors++;
1675         }
1676         else 
1677         {
1678                 self->netdev->stats.tx_packets++;
1679         }
1680
1681         /* Check if we need to change the speed */
1682         if (self->new_speed) 
1683         {
1684                 ali_ircc_change_speed(self, self->new_speed);
1685                 self->new_speed = 0;
1686         }
1687
1688         /* Finished with this frame, so prepare for next */
1689         self->tx_fifo.ptr++;
1690         self->tx_fifo.len--;
1691
1692         /* Any frames to be sent back-to-back? */
1693         if (self->tx_fifo.len) 
1694         {
1695                 ali_ircc_dma_xmit(self);
1696                 
1697                 /* Not finished yet! */
1698                 ret = FALSE;
1699         } 
1700         else 
1701         {       /* Reset Tx FIFO info */
1702                 self->tx_fifo.len = self->tx_fifo.ptr = self->tx_fifo.free = 0;
1703                 self->tx_fifo.tail = self->tx_buff.head;
1704         }
1705
1706         /* Make sure we have room for more frames */
1707         if (self->tx_fifo.free < MAX_TX_WINDOW) {
1708                 /* Not busy transmitting anymore */
1709                 /* Tell the network layer, that we can accept more frames */
1710                 netif_wake_queue(self->netdev);
1711         }
1712                 
1713         switch_bank(iobase, BANK0); 
1714         
1715         IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __func__ );
1716         return ret;
1717 }
1718
1719 /*
1720  * Function ali_ircc_dma_receive (self)
1721  *
1722  *    Get ready for receiving a frame. The device will initiate a DMA
1723  *    if it starts to receive a frame.
1724  *
1725  */
1726 static int ali_ircc_dma_receive(struct ali_ircc_cb *self) 
1727 {
1728         int iobase, tmp;
1729         
1730         IRDA_DEBUG(1, "%s(), ---------------- Start -----------------\n", __func__ );
1731         
1732         iobase = self->io.fir_base;
1733         
1734         /* Reset Tx FIFO info */
1735         self->tx_fifo.len = self->tx_fifo.ptr = self->tx_fifo.free = 0;
1736         self->tx_fifo.tail = self->tx_buff.head;
1737                 
1738         /* Disable DMA */
1739         switch_bank(iobase, BANK1);
1740         outb(inb(iobase+FIR_CR) & ~CR_DMA_EN, iobase+FIR_CR);
1741         
1742         /* Reset Message Count */
1743         switch_bank(iobase, BANK0);
1744         outb(0x07, iobase+FIR_LSR);
1745                 
1746         self->rcvFramesOverflow = FALSE;        
1747         
1748         self->LineStatus = inb(iobase+FIR_LSR) ;
1749         
1750         /* Reset Rx FIFO info */
1751         self->io.direction = IO_RECV;
1752         self->rx_buff.data = self->rx_buff.head;
1753                 
1754         /* Reset Rx FIFO */
1755         // switch_bank(iobase, BANK0);
1756         outb(LCR_A_FIFO_RESET, iobase+FIR_LCR_A); 
1757         
1758         self->st_fifo.len = self->st_fifo.pending_bytes = 0;
1759         self->st_fifo.tail = self->st_fifo.head = 0;
1760                 
1761         irda_setup_dma(self->io.dma, self->rx_buff_dma, self->rx_buff.truesize,
1762                        DMA_RX_MODE);
1763          
1764         /* Set Receive Mode,Brick Wall */
1765         //switch_bank(iobase, BANK0);
1766         tmp = inb(iobase+FIR_LCR_B);
1767         outb((unsigned char)(tmp &0x3f) | LCR_B_RX_MODE | LCR_B_BW , iobase + FIR_LCR_B); // 2000/12/1 05:16PM
1768         IRDA_DEBUG(1, "%s(), *** Change To RX mode: FIR_LCR_B = 0x%x *** \n", __func__ , inb(iobase+FIR_LCR_B));
1769                         
1770         /* Set Rx Threshold */
1771         switch_bank(iobase, BANK1);
1772         outb(RX_FIFO_Threshold, iobase+FIR_FIFO_TR);
1773         outb(RX_DMA_Threshold, iobase+FIR_DMA_TR);
1774                 
1775         /* Enable DMA and Burst Mode */
1776         // switch_bank(iobase, BANK1);
1777         outb(CR_DMA_EN | CR_DMA_BURST, iobase+FIR_CR);
1778                                 
1779         switch_bank(iobase, BANK0); 
1780         IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __func__ );
1781         return 0;
1782 }
1783
1784 static int  ali_ircc_dma_receive_complete(struct ali_ircc_cb *self)
1785 {
1786         struct st_fifo *st_fifo;
1787         struct sk_buff *skb;
1788         __u8 status, MessageCount;
1789         int len, i, iobase, val;        
1790
1791         IRDA_DEBUG(1, "%s(), ---------------- Start -----------------\n", __func__ );
1792
1793         st_fifo = &self->st_fifo;               
1794         iobase = self->io.fir_base;     
1795                 
1796         switch_bank(iobase, BANK0);
1797         MessageCount = inb(iobase+ FIR_LSR)&0x07;
1798         
1799         if (MessageCount > 0)   
1800                 IRDA_DEBUG(0, "%s(), Messsage count = %d,\n", __func__ , MessageCount);
1801                 
1802         for (i=0; i<=MessageCount; i++)
1803         {
1804                 /* Bank 0 */
1805                 switch_bank(iobase, BANK0);
1806                 status = inb(iobase+FIR_LSR);
1807                 
1808                 switch_bank(iobase, BANK2);
1809                 len = inb(iobase+FIR_RX_DSR_HI) & 0x0f;
1810                 len = len << 8; 
1811                 len |= inb(iobase+FIR_RX_DSR_LO);
1812                 
1813                 IRDA_DEBUG(1, "%s(), RX Length = 0x%.2x,\n", __func__ , len);
1814                 IRDA_DEBUG(1, "%s(), RX Status = 0x%.2x,\n", __func__ , status);
1815                 
1816                 if (st_fifo->tail >= MAX_RX_WINDOW) {
1817                         IRDA_DEBUG(0, "%s(), window is full!\n", __func__ );
1818                         continue;
1819                 }
1820                         
1821                 st_fifo->entries[st_fifo->tail].status = status;
1822                 st_fifo->entries[st_fifo->tail].len = len;
1823                 st_fifo->pending_bytes += len;
1824                 st_fifo->tail++;
1825                 st_fifo->len++;
1826         }
1827                         
1828         for (i=0; i<=MessageCount; i++)
1829         {       
1830                 /* Get first entry */
1831                 status = st_fifo->entries[st_fifo->head].status;
1832                 len    = st_fifo->entries[st_fifo->head].len;
1833                 st_fifo->pending_bytes -= len;
1834                 st_fifo->head++;
1835                 st_fifo->len--;                 
1836                 
1837                 /* Check for errors */
1838                 if ((status & 0xd8) || self->rcvFramesOverflow || (len==0))             
1839                 {
1840                         IRDA_DEBUG(0,"%s(), ************* RX Errors ************ \n", __func__ );
1841                         
1842                         /* Skip frame */
1843                         self->netdev->stats.rx_errors++;
1844                         
1845                         self->rx_buff.data += len;
1846                         
1847                         if (status & LSR_FIFO_UR) 
1848                         {
1849                                 self->netdev->stats.rx_frame_errors++;
1850                                 IRDA_DEBUG(0,"%s(), ************* FIFO Errors ************ \n", __func__ );
1851                         }       
1852                         if (status & LSR_FRAME_ERROR)
1853                         {
1854                                 self->netdev->stats.rx_frame_errors++;
1855                                 IRDA_DEBUG(0,"%s(), ************* FRAME Errors ************ \n", __func__ );
1856                         }
1857                                                         
1858                         if (status & LSR_CRC_ERROR) 
1859                         {
1860                                 self->netdev->stats.rx_crc_errors++;
1861                                 IRDA_DEBUG(0,"%s(), ************* CRC Errors ************ \n", __func__ );
1862                         }
1863                         
1864                         if(self->rcvFramesOverflow)
1865                         {
1866                                 self->netdev->stats.rx_frame_errors++;
1867                                 IRDA_DEBUG(0,"%s(), ************* Overran DMA buffer ************ \n", __func__ );
1868                         }
1869                         if(len == 0)
1870                         {
1871                                 self->netdev->stats.rx_frame_errors++;
1872                                 IRDA_DEBUG(0,"%s(), ********** Receive Frame Size = 0 ********* \n", __func__ );
1873                         }
1874                 }        
1875                 else 
1876                 {
1877                         
1878                         if (st_fifo->pending_bytes < 32) 
1879                         {
1880                                 switch_bank(iobase, BANK0);
1881                                 val = inb(iobase+FIR_BSR);      
1882                                 if ((val& BSR_FIFO_NOT_EMPTY)== 0x80) 
1883                                 {
1884                                         IRDA_DEBUG(0, "%s(), ************* BSR_FIFO_NOT_EMPTY ************ \n", __func__ );
1885                                         
1886                                         /* Put this entry back in fifo */
1887                                         st_fifo->head--;
1888                                         st_fifo->len++;
1889                                         st_fifo->pending_bytes += len;
1890                                         st_fifo->entries[st_fifo->head].status = status;
1891                                         st_fifo->entries[st_fifo->head].len = len;
1892                                                 
1893                                         /*  
1894                                         * DMA not finished yet, so try again 
1895                                         * later, set timer value, resolution 
1896                                         * 500 us 
1897                                         */
1898                                          
1899                                         switch_bank(iobase, BANK1);
1900                                         outb(TIMER_IIR_500, iobase+FIR_TIMER_IIR); // 2001/1/2 05:07PM
1901                                         
1902                                         /* Enable Timer */
1903                                         outb(inb(iobase+FIR_CR) | CR_TIMER_EN, iobase+FIR_CR);
1904                                                 
1905                                         return FALSE; /* I'll be back! */
1906                                 }
1907                         }               
1908                         
1909                         /* 
1910                          * Remember the time we received this frame, so we can
1911                          * reduce the min turn time a bit since we will know
1912                          * how much time we have used for protocol processing
1913                          */
1914                         do_gettimeofday(&self->stamp);
1915
1916                         skb = dev_alloc_skb(len+1);
1917                         if (skb == NULL)  
1918                         {
1919                                 IRDA_WARNING("%s(), memory squeeze, "
1920                                              "dropping frame.\n",
1921                                              __func__);
1922                                 self->netdev->stats.rx_dropped++;
1923
1924                                 return FALSE;
1925                         }
1926                         
1927                         /* Make sure IP header gets aligned */
1928                         skb_reserve(skb, 1); 
1929                         
1930                         /* Copy frame without CRC, CRC is removed by hardware*/
1931                         skb_put(skb, len);
1932                         skb_copy_to_linear_data(skb, self->rx_buff.data, len);
1933
1934                         /* Move to next frame */
1935                         self->rx_buff.data += len;
1936                         self->netdev->stats.rx_bytes += len;
1937                         self->netdev->stats.rx_packets++;
1938
1939                         skb->dev = self->netdev;
1940                         skb_reset_mac_header(skb);
1941                         skb->protocol = htons(ETH_P_IRDA);
1942                         netif_rx(skb);
1943                 }
1944         }
1945         
1946         switch_bank(iobase, BANK0);     
1947                 
1948         IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __func__ );
1949         return TRUE;
1950 }
1951
1952
1953
1954 /*
1955  * Function ali_ircc_sir_hard_xmit (skb, dev)
1956  *
1957  *    Transmit the frame!
1958  *
1959  */
1960 static int ali_ircc_sir_hard_xmit(struct sk_buff *skb, struct net_device *dev)
1961 {
1962         struct ali_ircc_cb *self;
1963         unsigned long flags;
1964         int iobase;
1965         __u32 speed;
1966         
1967         IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __func__ );
1968         
1969         IRDA_ASSERT(dev != NULL, return 0;);
1970         
1971         self = netdev_priv(dev);
1972         IRDA_ASSERT(self != NULL, return 0;);
1973
1974         iobase = self->io.sir_base;
1975
1976         netif_stop_queue(dev);
1977         
1978         /* Make sure tests *& speed change are atomic */
1979         spin_lock_irqsave(&self->lock, flags);
1980
1981         /* Note : you should make sure that speed changes are not going
1982          * to corrupt any outgoing frame. Look at nsc-ircc for the gory
1983          * details - Jean II */
1984
1985         /* Check if we need to change the speed */
1986         speed = irda_get_next_speed(skb);
1987         if ((speed != self->io.speed) && (speed != -1)) {
1988                 /* Check for empty frame */
1989                 if (!skb->len) {
1990                         ali_ircc_change_speed(self, speed); 
1991                         dev->trans_start = jiffies;
1992                         spin_unlock_irqrestore(&self->lock, flags);
1993                         dev_kfree_skb(skb);
1994                         return 0;
1995                 } else
1996                         self->new_speed = speed;
1997         }
1998
1999         /* Init tx buffer */
2000         self->tx_buff.data = self->tx_buff.head;
2001
2002         /* Copy skb to tx_buff while wrapping, stuffing and making CRC */
2003         self->tx_buff.len = async_wrap_skb(skb, self->tx_buff.data, 
2004                                            self->tx_buff.truesize);
2005         
2006         self->netdev->stats.tx_bytes += self->tx_buff.len;
2007
2008         /* Turn on transmit finished interrupt. Will fire immediately!  */
2009         outb(UART_IER_THRI, iobase+UART_IER); 
2010
2011         dev->trans_start = jiffies;
2012         spin_unlock_irqrestore(&self->lock, flags);
2013
2014         dev_kfree_skb(skb);
2015         
2016         IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __func__ );
2017         
2018         return 0;       
2019 }
2020
2021
2022 /*
2023  * Function ali_ircc_net_ioctl (dev, rq, cmd)
2024  *
2025  *    Process IOCTL commands for this device
2026  *
2027  */
2028 static int ali_ircc_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2029 {
2030         struct if_irda_req *irq = (struct if_irda_req *) rq;
2031         struct ali_ircc_cb *self;
2032         unsigned long flags;
2033         int ret = 0;
2034         
2035         IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __func__ );
2036         
2037         IRDA_ASSERT(dev != NULL, return -1;);
2038
2039         self = netdev_priv(dev);
2040
2041         IRDA_ASSERT(self != NULL, return -1;);
2042
2043         IRDA_DEBUG(2, "%s(), %s, (cmd=0x%X)\n", __func__ , dev->name, cmd);
2044         
2045         switch (cmd) {
2046         case SIOCSBANDWIDTH: /* Set bandwidth */
2047                 IRDA_DEBUG(1, "%s(), SIOCSBANDWIDTH\n", __func__ );
2048                 /*
2049                  * This function will also be used by IrLAP to change the
2050                  * speed, so we still must allow for speed change within
2051                  * interrupt context.
2052                  */
2053                 if (!in_interrupt() && !capable(CAP_NET_ADMIN))
2054                         return -EPERM;
2055                 
2056                 spin_lock_irqsave(&self->lock, flags);
2057                 ali_ircc_change_speed(self, irq->ifr_baudrate);         
2058                 spin_unlock_irqrestore(&self->lock, flags);
2059                 break;
2060         case SIOCSMEDIABUSY: /* Set media busy */
2061                 IRDA_DEBUG(1, "%s(), SIOCSMEDIABUSY\n", __func__ );
2062                 if (!capable(CAP_NET_ADMIN))
2063                         return -EPERM;
2064                 irda_device_set_media_busy(self->netdev, TRUE);
2065                 break;
2066         case SIOCGRECEIVING: /* Check if we are receiving right now */
2067                 IRDA_DEBUG(2, "%s(), SIOCGRECEIVING\n", __func__ );
2068                 /* This is protected */
2069                 irq->ifr_receiving = ali_ircc_is_receiving(self);
2070                 break;
2071         default:
2072                 ret = -EOPNOTSUPP;
2073         }
2074         
2075         IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __func__ );
2076         
2077         return ret;
2078 }
2079
2080 /*
2081  * Function ali_ircc_is_receiving (self)
2082  *
2083  *    Return TRUE is we are currently receiving a frame
2084  *
2085  */
2086 static int ali_ircc_is_receiving(struct ali_ircc_cb *self)
2087 {
2088         unsigned long flags;
2089         int status = FALSE;
2090         int iobase;             
2091         
2092         IRDA_DEBUG(2, "%s(), ---------------- Start -----------------\n", __func__ );
2093         
2094         IRDA_ASSERT(self != NULL, return FALSE;);
2095
2096         spin_lock_irqsave(&self->lock, flags);
2097
2098         if (self->io.speed > 115200) 
2099         {
2100                 iobase = self->io.fir_base;
2101                 
2102                 switch_bank(iobase, BANK1);
2103                 if((inb(iobase+FIR_FIFO_FR) & 0x3f) != 0)               
2104                 {
2105                         /* We are receiving something */
2106                         IRDA_DEBUG(1, "%s(), We are receiving something\n", __func__ );
2107                         status = TRUE;
2108                 }
2109                 switch_bank(iobase, BANK0);             
2110         } 
2111         else
2112         { 
2113                 status = (self->rx_buff.state != OUTSIDE_FRAME);
2114         }
2115         
2116         spin_unlock_irqrestore(&self->lock, flags);
2117         
2118         IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __func__ );
2119         
2120         return status;
2121 }
2122
2123 static int ali_ircc_suspend(struct platform_device *dev, pm_message_t state)
2124 {
2125         struct ali_ircc_cb *self = platform_get_drvdata(dev);
2126         
2127         IRDA_MESSAGE("%s, Suspending\n", ALI_IRCC_DRIVER_NAME);
2128
2129         if (self->io.suspended)
2130                 return 0;
2131
2132         ali_ircc_net_close(self->netdev);
2133
2134         self->io.suspended = 1;
2135         
2136         return 0;
2137 }
2138
2139 static int ali_ircc_resume(struct platform_device *dev)
2140 {
2141         struct ali_ircc_cb *self = platform_get_drvdata(dev);
2142         
2143         if (!self->io.suspended)
2144                 return 0;
2145         
2146         ali_ircc_net_open(self->netdev);
2147         
2148         IRDA_MESSAGE("%s, Waking up\n", ALI_IRCC_DRIVER_NAME);
2149
2150         self->io.suspended = 0;
2151
2152         return 0;
2153 }
2154
2155 /* ALi Chip Function */
2156
2157 static void SetCOMInterrupts(struct ali_ircc_cb *self , unsigned char enable)
2158 {
2159         
2160         unsigned char newMask;
2161         
2162         int iobase = self->io.fir_base; /* or sir_base */
2163
2164         IRDA_DEBUG(2, "%s(), -------- Start -------- ( Enable = %d )\n", __func__ , enable);
2165         
2166         /* Enable the interrupt which we wish to */
2167         if (enable){
2168                 if (self->io.direction == IO_XMIT)
2169                 {
2170                         if (self->io.speed > 115200) /* FIR, MIR */
2171                         {
2172                                 newMask = self->ier;
2173                         }
2174                         else /* SIR */
2175                         {
2176                                 newMask = UART_IER_THRI | UART_IER_RDI;
2177                         }
2178                 }
2179                 else {
2180                         if (self->io.speed > 115200) /* FIR, MIR */
2181                         {
2182                                 newMask = self->ier;
2183                         }
2184                         else /* SIR */
2185                         {
2186                                 newMask = UART_IER_RDI;
2187                         }
2188                 }
2189         }
2190         else /* Disable all the interrupts */
2191         {
2192                 newMask = 0x00;
2193
2194         }
2195
2196         //SIR and FIR has different registers
2197         if (self->io.speed > 115200)
2198         {       
2199                 switch_bank(iobase, BANK0);
2200                 outb(newMask, iobase+FIR_IER);
2201         }
2202         else
2203                 outb(newMask, iobase+UART_IER);
2204                 
2205         IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __func__ );
2206 }
2207
2208 static void SIR2FIR(int iobase)
2209 {
2210         //unsigned char tmp;
2211                 
2212         IRDA_DEBUG(1, "%s(), ---------------- Start ----------------\n", __func__ );
2213         
2214         /* Already protected (change_speed() or setup()), no need to lock.
2215          * Jean II */
2216         
2217         outb(0x28, iobase+UART_MCR);
2218         outb(0x68, iobase+UART_MCR);
2219         outb(0x88, iobase+UART_MCR);            
2220         
2221         outb(0x60, iobase+FIR_MCR);     /*  Master Reset */
2222         outb(0x20, iobase+FIR_MCR);     /*  Master Interrupt Enable */
2223         
2224         //tmp = inb(iobase+FIR_LCR_B);  /* SIP enable */
2225         //tmp |= 0x20;
2226         //outb(tmp, iobase+FIR_LCR_B);  
2227         
2228         IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __func__ );
2229 }
2230
2231 static void FIR2SIR(int iobase)
2232 {
2233         unsigned char val;
2234         
2235         IRDA_DEBUG(1, "%s(), ---------------- Start ----------------\n", __func__ );
2236         
2237         /* Already protected (change_speed() or setup()), no need to lock.
2238          * Jean II */
2239         
2240         outb(0x20, iobase+FIR_MCR);     /* IRQ to low */
2241         outb(0x00, iobase+UART_IER);    
2242                 
2243         outb(0xA0, iobase+FIR_MCR);     /* Don't set master reset */
2244         outb(0x00, iobase+UART_FCR);
2245         outb(0x07, iobase+UART_FCR);            
2246         
2247         val = inb(iobase+UART_RX);
2248         val = inb(iobase+UART_LSR);
2249         val = inb(iobase+UART_MSR);
2250         
2251         IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __func__ );
2252 }
2253
2254 MODULE_AUTHOR("Benjamin Kong <benjamin_kong@ali.com.tw>");
2255 MODULE_DESCRIPTION("ALi FIR Controller Driver");
2256 MODULE_LICENSE("GPL");
2257 MODULE_ALIAS("platform:" ALI_IRCC_DRIVER_NAME);
2258
2259
2260 module_param_array(io, int, NULL, 0);
2261 MODULE_PARM_DESC(io, "Base I/O addresses");
2262 module_param_array(irq, int, NULL, 0);
2263 MODULE_PARM_DESC(irq, "IRQ lines");
2264 module_param_array(dma, int, NULL, 0);
2265 MODULE_PARM_DESC(dma, "DMA channels");
2266
2267 module_init(ali_ircc_init);
2268 module_exit(ali_ircc_cleanup);