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