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