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