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