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