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