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