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