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