ali-ircc.c 56.3 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
/*********************************************************************
 *                
 * Filename:      ali-ircc.h
 * Version:       0.5
 * Description:   Driver for the ALI M1535D and M1543C FIR Controller
 * Status:        Experimental.
 * Author:        Benjamin Kong <benjamin_kong@ali.com.tw>
 * Created at:    2000/10/16 03:46PM
 * Modified at:   2001/1/3 02:55PM
 * Modified by:   Benjamin Kong <benjamin_kong@ali.com.tw>
 * Modified at:   2003/11/6 and support for ALi south-bridge chipsets M1563
 * Modified by:   Clear Zhang <clear_zhang@ali.com.tw>
 * 
 *     Copyright (c) 2000 Benjamin Kong <benjamin_kong@ali.com.tw>
 *     All Rights Reserved
 *      
 *     This program is free software; you can redistribute it and/or 
 *     modify it under the terms of the GNU General Public License as 
 *     published by the Free Software Foundation; either version 2 of 
 *     the License, or (at your option) any later version.
 *  
 ********************************************************************/

#include <linux/module.h>

#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/skbuff.h>
#include <linux/netdevice.h>
#include <linux/ioport.h>
#include <linux/delay.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/rtnetlink.h>
#include <linux/serial_reg.h>
#include <linux/dma-mapping.h>
37
#include <linux/platform_device.h>
L
Linus Torvalds 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51

#include <asm/io.h>
#include <asm/dma.h>
#include <asm/byteorder.h>

#include <net/irda/wrapper.h>
#include <net/irda/irda.h>
#include <net/irda/irda_device.h>

#include "ali-ircc.h"

#define CHIP_IO_EXTENT 8
#define BROKEN_DONGLE_ID

52 53 54 55 56 57 58 59 60 61 62
#define ALI_IRCC_DRIVER_NAME "ali-ircc"

/* Power Management */
static int ali_ircc_suspend(struct platform_device *dev, pm_message_t state);
static int ali_ircc_resume(struct platform_device *dev);

static struct platform_driver ali_ircc_driver = {
	.suspend	= ali_ircc_suspend,
	.resume		= ali_ircc_resume,
	.driver		= {
		.name	= ALI_IRCC_DRIVER_NAME,
63
		.owner	= THIS_MODULE,
64 65
	},
};
L
Linus Torvalds 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113

/* Module parameters */
static int qos_mtt_bits = 0x07;  /* 1 ms or more */

/* Use BIOS settions by default, but user may supply module parameters */
static unsigned int io[]  = { ~0, ~0, ~0, ~0 };
static unsigned int irq[] = { 0, 0, 0, 0 };
static unsigned int dma[] = { 0, 0, 0, 0 };

static int  ali_ircc_probe_53(ali_chip_t *chip, chipio_t *info);
static int  ali_ircc_init_43(ali_chip_t *chip, chipio_t *info);
static int  ali_ircc_init_53(ali_chip_t *chip, chipio_t *info);

/* These are the currently known ALi sourth-bridge chipsets, the only one difference
 * is that M1543C doesn't support HP HDSL-3600
 */
static ali_chip_t chips[] =
{
	{ "M1543", { 0x3f0, 0x370 }, 0x51, 0x23, 0x20, 0x43, ali_ircc_probe_53, ali_ircc_init_43 },
	{ "M1535", { 0x3f0, 0x370 }, 0x51, 0x23, 0x20, 0x53, ali_ircc_probe_53, ali_ircc_init_53 },
	{ "M1563", { 0x3f0, 0x370 }, 0x51, 0x23, 0x20, 0x63, ali_ircc_probe_53, ali_ircc_init_53 },
	{ NULL }
};

/* Max 4 instances for now */
static struct ali_ircc_cb *dev_self[] = { NULL, NULL, NULL, NULL };

/* Dongle Types */
static char *dongle_types[] = {
	"TFDS6000",
	"HP HSDL-3600",
	"HP HSDL-1100",	
	"No dongle connected",
};

/* Some prototypes */
static int  ali_ircc_open(int i, chipio_t *info);

static int  ali_ircc_close(struct ali_ircc_cb *self);

static int  ali_ircc_setup(chipio_t *info);
static int  ali_ircc_is_receiving(struct ali_ircc_cb *self);
static int  ali_ircc_net_open(struct net_device *dev);
static int  ali_ircc_net_close(struct net_device *dev);
static int  ali_ircc_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
static void ali_ircc_change_speed(struct ali_ircc_cb *self, __u32 baud);

/* SIR function */
S
Stephen Hemminger 已提交
114 115
static netdev_tx_t ali_ircc_sir_hard_xmit(struct sk_buff *skb,
						struct net_device *dev);
L
Linus Torvalds 已提交
116 117 118 119 120 121 122
static irqreturn_t ali_ircc_sir_interrupt(struct ali_ircc_cb *self);
static void ali_ircc_sir_receive(struct ali_ircc_cb *self);
static void ali_ircc_sir_write_wakeup(struct ali_ircc_cb *self);
static int  ali_ircc_sir_write(int iobase, int fifo_size, __u8 *buf, int len);
static void ali_ircc_sir_change_speed(struct ali_ircc_cb *priv, __u32 speed);

/* FIR function */
S
Stephen Hemminger 已提交
123 124
static netdev_tx_t  ali_ircc_fir_hard_xmit(struct sk_buff *skb,
						 struct net_device *dev);
L
Linus Torvalds 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
static void ali_ircc_fir_change_speed(struct ali_ircc_cb *priv, __u32 speed);
static irqreturn_t ali_ircc_fir_interrupt(struct ali_ircc_cb *self);
static int  ali_ircc_dma_receive(struct ali_ircc_cb *self); 
static int  ali_ircc_dma_receive_complete(struct ali_ircc_cb *self);
static int  ali_ircc_dma_xmit_complete(struct ali_ircc_cb *self);
static void ali_ircc_dma_xmit(struct ali_ircc_cb *self);

/* My Function */
static int  ali_ircc_read_dongle_id (int i, chipio_t *info);
static void ali_ircc_change_dongle_speed(struct ali_ircc_cb *priv, int speed);

/* ALi chip function */
static void SIR2FIR(int iobase);
static void FIR2SIR(int iobase);
static void SetCOMInterrupts(struct ali_ircc_cb *self , unsigned char enable);

/*
 * Function ali_ircc_init ()
 *
 *    Initialize chip. Find out whay kinds of chips we are dealing with
 *    and their configuation registers address
 */
static int __init ali_ircc_init(void)
{
	ali_chip_t *chip;
	chipio_t info;
151
	int ret;
L
Linus Torvalds 已提交
152 153 154 155
	int cfg, cfg_base;
	int reg, revision;
	int i = 0;
	
156
	IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __func__);
157 158 159 160 161 162 163 164

	ret = platform_driver_register(&ali_ircc_driver);
        if (ret) {
                IRDA_ERROR("%s, Can't register driver!\n",
			   ALI_IRCC_DRIVER_NAME);
                return ret;
        }

165
	ret = -ENODEV;
L
Linus Torvalds 已提交
166 167 168 169
	
	/* Probe for all the ALi chipsets we know about */
	for (chip= chips; chip->name; chip++, i++) 
	{
170
		IRDA_DEBUG(2, "%s(), Probing for %s ...\n", __func__, chip->name);
L
Linus Torvalds 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
				
		/* Try all config registers for this chip */
		for (cfg=0; cfg<2; cfg++)
		{
			cfg_base = chip->cfg[cfg];
			if (!cfg_base)
				continue;
				
			memset(&info, 0, sizeof(chipio_t));
			info.cfg_base = cfg_base;
			info.fir_base = io[i];
			info.dma = dma[i];
			info.irq = irq[i];
			
			
			/* Enter Configuration */
			outb(chip->entr1, cfg_base);
			outb(chip->entr2, cfg_base);
			
			/* Select Logical Device 5 Registers (UART2) */
			outb(0x07, cfg_base);
			outb(0x05, cfg_base+1);
			
			/* Read Chip Identification Register */
			outb(chip->cid_index, cfg_base);	
			reg = inb(cfg_base+1);	
				
			if (reg == chip->cid_value)
			{
200
				IRDA_DEBUG(2, "%s(), Chip found at 0x%03x\n", __func__, cfg_base);
L
Linus Torvalds 已提交
201 202 203
					   
				outb(0x1F, cfg_base);
				revision = inb(cfg_base+1);
204
				IRDA_DEBUG(2, "%s(), Found %s chip, revision=%d\n", __func__,
L
Linus Torvalds 已提交
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
					   chip->name, revision);					
				
				/* 
				 * If the user supplies the base address, then
				 * we init the chip, if not we probe the values
				 * set by the BIOS
				 */				
				if (io[i] < 2000)
				{
					chip->init(chip, &info);
				}
				else
				{
					chip->probe(chip, &info);	
				}
				
				if (ali_ircc_open(i, &info) == 0)
					ret = 0;
				i++;				
			}
			else
			{
227
				IRDA_DEBUG(2, "%s(), No %s chip at 0x%03x\n", __func__, chip->name, cfg_base);
L
Linus Torvalds 已提交
228 229 230 231 232 233
			}
			/* Exit configuration */
			outb(0xbb, cfg_base);
		}
	}		
		
234
	IRDA_DEBUG(2, "%s(), ----------------- End -----------------\n", __func__);
235 236 237 238

	if (ret)
		platform_driver_unregister(&ali_ircc_driver);

L
Linus Torvalds 已提交
239 240 241 242 243 244 245 246 247 248 249 250 251
	return ret;
}

/*
 * Function ali_ircc_cleanup ()
 *
 *    Close all configured chips
 *
 */
static void __exit ali_ircc_cleanup(void)
{
	int i;

252
	IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __func__);
L
Linus Torvalds 已提交
253

254
	for (i=0; i < ARRAY_SIZE(dev_self); i++) {
L
Linus Torvalds 已提交
255 256 257 258
		if (dev_self[i])
			ali_ircc_close(dev_self[i]);
	}
	
259 260
	platform_driver_unregister(&ali_ircc_driver);

261
	IRDA_DEBUG(2, "%s(), ----------------- End -----------------\n", __func__);
L
Linus Torvalds 已提交
262 263
}

264 265 266 267 268 269 270 271 272 273 274 275 276 277
static const struct net_device_ops ali_ircc_sir_ops = {
	.ndo_open       = ali_ircc_net_open,
	.ndo_stop       = ali_ircc_net_close,
	.ndo_start_xmit = ali_ircc_sir_hard_xmit,
	.ndo_do_ioctl   = ali_ircc_net_ioctl,
};

static const struct net_device_ops ali_ircc_fir_ops = {
	.ndo_open       = ali_ircc_net_open,
	.ndo_stop       = ali_ircc_net_close,
	.ndo_start_xmit = ali_ircc_fir_hard_xmit,
	.ndo_do_ioctl   = ali_ircc_net_ioctl,
};

L
Linus Torvalds 已提交
278 279 280 281 282 283 284 285 286 287 288 289 290
/*
 * Function ali_ircc_open (int i, chipio_t *inf)
 *
 *    Open driver instance
 *
 */
static int ali_ircc_open(int i, chipio_t *info)
{
	struct net_device *dev;
	struct ali_ircc_cb *self;
	int dongle_id;
	int err;
			
291
	IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __func__);
292 293 294

	if (i >= ARRAY_SIZE(dev_self)) {
		IRDA_ERROR("%s(), maximum number of supported chips reached!\n",
295
			   __func__);
296 297
		return -ENOMEM;
	}
L
Linus Torvalds 已提交
298 299 300 301 302 303 304 305
	
	/* Set FIR FIFO and DMA Threshold */
	if ((ali_ircc_setup(info)) == -1)
		return -1;
		
	dev = alloc_irdadev(sizeof(*self));
	if (dev == NULL) {
		IRDA_ERROR("%s(), can't allocate memory for control block!\n",
306
			   __func__);
L
Linus Torvalds 已提交
307 308 309
		return -ENOMEM;
	}

310
	self = netdev_priv(dev);
L
Linus Torvalds 已提交
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
	self->netdev = dev;
	spin_lock_init(&self->lock);
   
	/* Need to store self somewhere */
	dev_self[i] = self;
	self->index = i;

	/* Initialize IO */
	self->io.cfg_base  = info->cfg_base;	/* In ali_ircc_probe_53 assign 		*/
	self->io.fir_base  = info->fir_base;	/* info->sir_base = info->fir_base 	*/
	self->io.sir_base  = info->sir_base; 	/* ALi SIR and FIR use the same address */
        self->io.irq       = info->irq;
        self->io.fir_ext   = CHIP_IO_EXTENT;
        self->io.dma       = info->dma;
        self->io.fifo_size = 16;		/* SIR: 16, FIR: 32 Benjamin 2000/11/1 */
	
	/* Reserve the ioports that we need */
328 329
	if (!request_region(self->io.fir_base, self->io.fir_ext,
			    ALI_IRCC_DRIVER_NAME)) {
330
		IRDA_WARNING("%s(), can't get iobase of 0x%03x\n", __func__,
L
Linus Torvalds 已提交
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
			self->io.fir_base);
		err = -ENODEV;
		goto err_out1;
	}

	/* Initialize QoS for this device */
	irda_init_max_qos_capabilies(&self->qos);
	
	/* The only value we must override it the baudrate */
	self->qos.baud_rate.bits = IR_9600|IR_19200|IR_38400|IR_57600|
		IR_115200|IR_576000|IR_1152000|(IR_4000000 << 8); // benjamin 2000/11/8 05:27PM
			
	self->qos.min_turn_time.bits = qos_mtt_bits;
			
	irda_qos_bits_to_value(&self->qos);
	
	/* Max DMA buffer size needed = (data_size + 6) * (window_size) + 6; */
	self->rx_buff.truesize = 14384; 
	self->tx_buff.truesize = 14384;

	/* Allocate memory if needed */
	self->rx_buff.head =
		dma_alloc_coherent(NULL, self->rx_buff.truesize,
				   &self->rx_buff_dma, GFP_KERNEL);
	if (self->rx_buff.head == NULL) {
		err = -ENOMEM;
		goto err_out2;
	}
	memset(self->rx_buff.head, 0, self->rx_buff.truesize);
	
	self->tx_buff.head =
		dma_alloc_coherent(NULL, self->tx_buff.truesize,
				   &self->tx_buff_dma, GFP_KERNEL);
	if (self->tx_buff.head == NULL) {
		err = -ENOMEM;
		goto err_out3;
	}
	memset(self->tx_buff.head, 0, self->tx_buff.truesize);

	self->rx_buff.in_frame = FALSE;
	self->rx_buff.state = OUTSIDE_FRAME;
	self->tx_buff.data = self->tx_buff.head;
	self->rx_buff.data = self->rx_buff.head;
	
	/* Reset Tx queue info */
	self->tx_fifo.len = self->tx_fifo.ptr = self->tx_fifo.free = 0;
	self->tx_fifo.tail = self->tx_buff.head;

	/* Override the network functions we need to use */
380
	dev->netdev_ops = &ali_ircc_sir_ops;
L
Linus Torvalds 已提交
381 382 383

	err = register_netdev(dev);
	if (err) {
384
		IRDA_ERROR("%s(), register_netdev() failed!\n", __func__);
L
Linus Torvalds 已提交
385 386 387 388 389 390
		goto err_out4;
	}
	IRDA_MESSAGE("IrDA: Registered device %s\n", dev->name);

	/* Check dongle id */
	dongle_id = ali_ircc_read_dongle_id(i, info);
391
	IRDA_MESSAGE("%s(), %s, Found dongle: %s\n", __func__,
392
		     ALI_IRCC_DRIVER_NAME, dongle_types[dongle_id]);
L
Linus Torvalds 已提交
393 394 395
		
	self->io.dongle_id = dongle_id;

396
	IRDA_DEBUG(2, "%s(), ----------------- End -----------------\n", __func__);
L
Linus Torvalds 已提交
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
	
	return 0;

 err_out4:
	dma_free_coherent(NULL, self->tx_buff.truesize,
			  self->tx_buff.head, self->tx_buff_dma);
 err_out3:
	dma_free_coherent(NULL, self->rx_buff.truesize,
			  self->rx_buff.head, self->rx_buff_dma);
 err_out2:
	release_region(self->io.fir_base, self->io.fir_ext);
 err_out1:
	dev_self[i] = NULL;
	free_netdev(dev);
	return err;
}


/*
 * Function ali_ircc_close (self)
 *
 *    Close driver instance
 *
 */
static int __exit ali_ircc_close(struct ali_ircc_cb *self)
{
	int iobase;

425
	IRDA_DEBUG(4, "%s(), ---------------- Start ----------------\n", __func__);
L
Linus Torvalds 已提交
426 427 428 429 430 431 432 433 434

	IRDA_ASSERT(self != NULL, return -1;);

        iobase = self->io.fir_base;

	/* Remove netdevice */
	unregister_netdev(self->netdev);

	/* Release the PORT that this driver is using */
435
	IRDA_DEBUG(4, "%s(), Releasing Region %03x\n", __func__, self->io.fir_base);
L
Linus Torvalds 已提交
436 437 438 439 440 441 442 443 444 445 446 447 448
	release_region(self->io.fir_base, self->io.fir_ext);

	if (self->tx_buff.head)
		dma_free_coherent(NULL, self->tx_buff.truesize,
				  self->tx_buff.head, self->tx_buff_dma);
	
	if (self->rx_buff.head)
		dma_free_coherent(NULL, self->rx_buff.truesize,
				  self->rx_buff.head, self->rx_buff_dma);

	dev_self[self->index] = NULL;
	free_netdev(self->netdev);
	
449
	IRDA_DEBUG(2, "%s(), ----------------- End -----------------\n", __func__);
L
Linus Torvalds 已提交
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
	
	return 0;
}

/*
 * Function ali_ircc_init_43 (chip, info)
 *
 *    Initialize the ALi M1543 chip. 
 */
static int ali_ircc_init_43(ali_chip_t *chip, chipio_t *info) 
{
	/* All controller information like I/O address, DMA channel, IRQ
	 * are set by BIOS
	 */
	
	return 0;
}

/*
 * Function ali_ircc_init_53 (chip, info)
 *
 *    Initialize the ALi M1535 chip. 
 */
static int ali_ircc_init_53(ali_chip_t *chip, chipio_t *info) 
{
	/* All controller information like I/O address, DMA channel, IRQ
	 * are set by BIOS
	 */
	
	return 0;
}

/*
 * Function ali_ircc_probe_53 (chip, info)
 *    	
 *	Probes for the ALi M1535D or M1535
 */
static int ali_ircc_probe_53(ali_chip_t *chip, chipio_t *info)
{
	int cfg_base = info->cfg_base;
	int hi, low, reg;
	
492
	IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __func__);
L
Linus Torvalds 已提交
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
	
	/* Enter Configuration */
	outb(chip->entr1, cfg_base);
	outb(chip->entr2, cfg_base);
	
	/* Select Logical Device 5 Registers (UART2) */
	outb(0x07, cfg_base);
	outb(0x05, cfg_base+1);
	
	/* Read address control register */
	outb(0x60, cfg_base);
	hi = inb(cfg_base+1);	
	outb(0x61, cfg_base);
	low = inb(cfg_base+1);
	info->fir_base = (hi<<8) + low;
	
	info->sir_base = info->fir_base;
	
511
	IRDA_DEBUG(2, "%s(), probing fir_base=0x%03x\n", __func__, info->fir_base);
L
Linus Torvalds 已提交
512 513 514 515 516
		
	/* Read IRQ control register */
	outb(0x70, cfg_base);
	reg = inb(cfg_base+1);
	info->irq = reg & 0x0f;
517
	IRDA_DEBUG(2, "%s(), probing irq=%d\n", __func__, info->irq);
L
Linus Torvalds 已提交
518 519 520 521 522 523 524
	
	/* Read DMA channel */
	outb(0x74, cfg_base);
	reg = inb(cfg_base+1);
	info->dma = reg & 0x07;
	
	if(info->dma == 0x04)
525
		IRDA_WARNING("%s(), No DMA channel assigned !\n", __func__);
L
Linus Torvalds 已提交
526
	else
527
		IRDA_DEBUG(2, "%s(), probing dma=%d\n", __func__, info->dma);
L
Linus Torvalds 已提交
528 529 530 531 532
	
	/* Read Enabled Status */
	outb(0x30, cfg_base);
	reg = inb(cfg_base+1);
	info->enabled = (reg & 0x80) && (reg & 0x01);
533
	IRDA_DEBUG(2, "%s(), probing enabled=%d\n", __func__, info->enabled);
L
Linus Torvalds 已提交
534 535 536 537 538
	
	/* Read Power Status */
	outb(0x22, cfg_base);
	reg = inb(cfg_base+1);
	info->suspended = (reg & 0x20);
539
	IRDA_DEBUG(2, "%s(), probing suspended=%d\n", __func__, info->suspended);
L
Linus Torvalds 已提交
540 541 542 543
	
	/* Exit configuration */
	outb(0xbb, cfg_base);
		
544
	IRDA_DEBUG(2, "%s(), ----------------- End -----------------\n", __func__);
L
Linus Torvalds 已提交
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
	
	return 0;	
}

/*
 * Function ali_ircc_setup (info)
 *
 *    	Set FIR FIFO and DMA Threshold
 *	Returns non-negative on success.
 *
 */
static int ali_ircc_setup(chipio_t *info)
{
	unsigned char tmp;
	int version;
	int iobase = info->fir_base;
	
562
	IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __func__);
L
Linus Torvalds 已提交
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
	
	/* Locking comments :
	 * Most operations here need to be protected. We are called before
	 * the device instance is created in ali_ircc_open(), therefore 
	 * nobody can bother us - Jean II */

	/* Switch to FIR space */
	SIR2FIR(iobase);
	
	/* Master Reset */
	outb(0x40, iobase+FIR_MCR); // benjamin 2000/11/30 11:45AM
	
	/* Read FIR ID Version Register */
	switch_bank(iobase, BANK3);
	version = inb(iobase+FIR_ID_VR);
	
	/* Should be 0x00 in the M1535/M1535D */
	if(version != 0x00)
	{
582 583
		IRDA_ERROR("%s, Wrong chip version %02x\n",
			   ALI_IRCC_DRIVER_NAME, version);
L
Linus Torvalds 已提交
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
		return -1;
	}
	
	/* Set FIR FIFO Threshold Register */
	switch_bank(iobase, BANK1);
	outb(RX_FIFO_Threshold, iobase+FIR_FIFO_TR);
	
	/* Set FIR DMA Threshold Register */
	outb(RX_DMA_Threshold, iobase+FIR_DMA_TR);
	
	/* CRC enable */
	switch_bank(iobase, BANK2);
	outb(inb(iobase+FIR_IRDA_CR) | IRDA_CR_CRC, iobase+FIR_IRDA_CR);
	
	/* NDIS driver set TX Length here BANK2 Alias 3, Alias4*/
	
	/* Switch to Bank 0 */
	switch_bank(iobase, BANK0);
	
	tmp = inb(iobase+FIR_LCR_B);
	tmp &=~0x20; // disable SIP
	tmp |= 0x80; // these two steps make RX mode
	tmp &= 0xbf;	
	outb(tmp, iobase+FIR_LCR_B);
		
	/* Disable Interrupt */
	outb(0x00, iobase+FIR_IER);
	
	
	/* Switch to SIR space */
	FIR2SIR(iobase);
	
616 617
	IRDA_MESSAGE("%s, driver loaded (Benjamin Kong)\n",
		     ALI_IRCC_DRIVER_NAME);
L
Linus Torvalds 已提交
618 619 620 621 622
	
	/* Enable receive interrupts */ 
	// outb(UART_IER_RDI, iobase+UART_IER); //benjamin 2000/11/23 01:25PM
	// Turn on the interrupts in ali_ircc_net_open
	
623
	IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __func__);
L
Linus Torvalds 已提交
624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
	
	return 0;
}

/*
 * Function ali_ircc_read_dongle_id (int index, info)
 *
 * Try to read dongle indentification. This procedure needs to be executed
 * once after power-on/reset. It also needs to be used whenever you suspect
 * that the user may have plugged/unplugged the IrDA Dongle.
 */
static int ali_ircc_read_dongle_id (int i, chipio_t *info)
{
	int dongle_id, reg;
	int cfg_base = info->cfg_base;
	
640
	IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __func__);
L
Linus Torvalds 已提交
641 642 643 644 645 646 647 648 649 650 651 652 653
		
	/* Enter Configuration */
	outb(chips[i].entr1, cfg_base);
	outb(chips[i].entr2, cfg_base);
	
	/* Select Logical Device 5 Registers (UART2) */
	outb(0x07, cfg_base);
	outb(0x05, cfg_base+1);
	
	/* Read Dongle ID */
	outb(0xf0, cfg_base);
	reg = inb(cfg_base+1);	
	dongle_id = ((reg>>6)&0x02) | ((reg>>5)&0x01);
654
	IRDA_DEBUG(2, "%s(), probing dongle_id=%d, dongle_types=%s\n", __func__,
L
Linus Torvalds 已提交
655 656 657 658 659
		dongle_id, dongle_types[dongle_id]);
	
	/* Exit configuration */
	outb(0xbb, cfg_base);
			
660
	IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __func__);
L
Linus Torvalds 已提交
661 662 663 664 665 666 667 668 669 670
	
	return dongle_id;
}

/*
 * Function ali_ircc_interrupt (irq, dev_id, regs)
 *
 *    An interrupt from the chip has arrived. Time to do some work
 *
 */
671
static irqreturn_t ali_ircc_interrupt(int irq, void *dev_id)
L
Linus Torvalds 已提交
672
{
673
	struct net_device *dev = dev_id;
L
Linus Torvalds 已提交
674 675 676
	struct ali_ircc_cb *self;
	int ret;
		
677
	IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __func__);
L
Linus Torvalds 已提交
678
		
679
	self = netdev_priv(dev);
L
Linus Torvalds 已提交
680 681 682 683 684 685 686 687 688 689 690
	
	spin_lock(&self->lock);
	
	/* Dispatch interrupt handler for the current speed */
	if (self->io.speed > 115200)
		ret = ali_ircc_fir_interrupt(self);
	else
		ret = ali_ircc_sir_interrupt(self);
		
	spin_unlock(&self->lock);
	
691
	IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __func__);
L
Linus Torvalds 已提交
692 693 694 695 696 697 698 699 700 701 702 703 704
	return ret;
}
/*
 * Function ali_ircc_fir_interrupt(irq, struct ali_ircc_cb *self)
 *
 *    Handle MIR/FIR interrupt
 *
 */
static irqreturn_t ali_ircc_fir_interrupt(struct ali_ircc_cb *self)
{
	__u8 eir, OldMessageCount;
	int iobase, tmp;
	
705
	IRDA_DEBUG(1, "%s(), ---------------- Start ----------------\n", __func__);
L
Linus Torvalds 已提交
706 707 708 709 710 711 712 713 714 715 716 717
	
	iobase = self->io.fir_base;
	
	switch_bank(iobase, BANK0);	
	self->InterruptID = inb(iobase+FIR_IIR);		
	self->BusStatus = inb(iobase+FIR_BSR);	
	
	OldMessageCount = (self->LineStatus + 1) & 0x07;
	self->LineStatus = inb(iobase+FIR_LSR);	
	//self->ier = inb(iobase+FIR_IER); 		2000/12/1 04:32PM
	eir = self->InterruptID & self->ier; /* Mask out the interesting ones */ 
	
718 719 720 721
	IRDA_DEBUG(1, "%s(), self->InterruptID = %x\n", __func__,self->InterruptID);
	IRDA_DEBUG(1, "%s(), self->LineStatus = %x\n", __func__,self->LineStatus);
	IRDA_DEBUG(1, "%s(), self->ier = %x\n", __func__,self->ier);
	IRDA_DEBUG(1, "%s(), eir = %x\n", __func__,eir);
L
Linus Torvalds 已提交
722 723 724 725 726 727 728 729 730 731
	
	/* Disable interrupts */
	 SetCOMInterrupts(self, FALSE);
	
	/* Tx or Rx Interrupt */
	
	if (eir & IIR_EOM) 
	{		
		if (self->io.direction == IO_XMIT) /* TX */
		{
732
			IRDA_DEBUG(1, "%s(), ******* IIR_EOM (Tx) *******\n", __func__);
L
Linus Torvalds 已提交
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
			
			if(ali_ircc_dma_xmit_complete(self))
			{
				if (irda_device_txqueue_empty(self->netdev)) 
				{
					/* Prepare for receive */
					ali_ircc_dma_receive(self);					
					self->ier = IER_EOM;									
				}
			}
			else
			{
				self->ier = IER_EOM; 					
			}
									
		}	
		else /* RX */
		{
751
			IRDA_DEBUG(1, "%s(), ******* IIR_EOM (Rx) *******\n", __func__);
L
Linus Torvalds 已提交
752 753 754 755
			
			if(OldMessageCount > ((self->LineStatus+1) & 0x07))
			{
				self->rcvFramesOverflow = TRUE;	
756
				IRDA_DEBUG(1, "%s(), ******* self->rcvFramesOverflow = TRUE ********\n", __func__);
L
Linus Torvalds 已提交
757 758 759 760
			}
						
			if (ali_ircc_dma_receive_complete(self))
			{
761
				IRDA_DEBUG(1, "%s(), ******* receive complete ********\n", __func__);
L
Linus Torvalds 已提交
762 763 764 765 766
				
				self->ier = IER_EOM;				
			}
			else
			{
767
				IRDA_DEBUG(1, "%s(), ******* Not receive complete ********\n", __func__);
L
Linus Torvalds 已提交
768 769 770 771 772 773 774 775 776 777 778 779
				
				self->ier = IER_EOM | IER_TIMER;								
			}	
		
		}		
	}
	/* Timer Interrupt */
	else if (eir & IIR_TIMER)
	{	
		if(OldMessageCount > ((self->LineStatus+1) & 0x07))
		{
			self->rcvFramesOverflow = TRUE;	
780
			IRDA_DEBUG(1, "%s(), ******* self->rcvFramesOverflow = TRUE *******\n", __func__);
L
Linus Torvalds 已提交
781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811
		}
		/* Disable Timer */
		switch_bank(iobase, BANK1);
		tmp = inb(iobase+FIR_CR);
		outb( tmp& ~CR_TIMER_EN, iobase+FIR_CR);
		
		/* Check if this is a Tx timer interrupt */
		if (self->io.direction == IO_XMIT)
		{
			ali_ircc_dma_xmit(self);
			
			/* Interrupt on EOM */
			self->ier = IER_EOM;
									
		}
		else /* Rx */
		{
			if(ali_ircc_dma_receive_complete(self)) 
			{
				self->ier = IER_EOM;
			}
			else
			{
				self->ier = IER_EOM | IER_TIMER;
			}	
		}		
	}
	
	/* Restore Interrupt */	
	SetCOMInterrupts(self, TRUE);	
		
812
	IRDA_DEBUG(1, "%s(), ----------------- End ---------------\n", __func__);
L
Linus Torvalds 已提交
813 814 815 816 817 818 819 820 821 822 823 824 825 826
	return IRQ_RETVAL(eir);
}

/*
 * Function ali_ircc_sir_interrupt (irq, self, eir)
 *
 *    Handle SIR interrupt
 *
 */
static irqreturn_t ali_ircc_sir_interrupt(struct ali_ircc_cb *self)
{
	int iobase;
	int iir, lsr;
	
827
	IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __func__);
L
Linus Torvalds 已提交
828 829 830 831 832 833 834 835
	
	iobase = self->io.sir_base;

	iir = inb(iobase+UART_IIR) & UART_IIR_ID;
	if (iir) {	
		/* Clear interrupt */
		lsr = inb(iobase+UART_LSR);

836
		IRDA_DEBUG(4, "%s(), iir=%02x, lsr=%02x, iobase=%#x\n", __func__,
L
Linus Torvalds 已提交
837 838 839 840 841
			   iir, lsr, iobase);

		switch (iir) 
		{
			case UART_IIR_RLSI:
842
				IRDA_DEBUG(2, "%s(), RLSI\n", __func__);
L
Linus Torvalds 已提交
843 844 845 846 847 848 849 850 851 852 853 854 855
				break;
			case UART_IIR_RDI:
				/* Receive interrupt */
				ali_ircc_sir_receive(self);
				break;
			case UART_IIR_THRI:
				if (lsr & UART_LSR_THRE)
				{
					/* Transmitter ready for data */
					ali_ircc_sir_write_wakeup(self);				
				}				
				break;
			default:
856
				IRDA_DEBUG(0, "%s(), unhandled IIR=%#x\n", __func__, iir);
L
Linus Torvalds 已提交
857 858 859 860 861 862
				break;
		} 
		
	}
	
	
863
	IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __func__);
L
Linus Torvalds 已提交
864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879

	return IRQ_RETVAL(iir);
}


/*
 * Function ali_ircc_sir_receive (self)
 *
 *    Receive one frame from the infrared port
 *
 */
static void ali_ircc_sir_receive(struct ali_ircc_cb *self) 
{
	int boguscount = 0;
	int iobase;
	
880
	IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __func__);
L
Linus Torvalds 已提交
881 882 883 884 885 886 887 888 889
	IRDA_ASSERT(self != NULL, return;);

	iobase = self->io.sir_base;

	/*  
	 * Receive all characters in Rx FIFO, unwrap and unstuff them. 
         * async_unwrap_char will deliver all found frames  
	 */
	do {
890
		async_unwrap_char(self->netdev, &self->netdev->stats, &self->rx_buff,
L
Linus Torvalds 已提交
891 892 893 894
				  inb(iobase+UART_RX));

		/* Make sure we don't stay here too long */
		if (boguscount++ > 32) {
895
			IRDA_DEBUG(2,"%s(), breaking!\n", __func__);
L
Linus Torvalds 已提交
896 897 898 899
			break;
		}
	} while (inb(iobase+UART_LSR) & UART_LSR_DR);	
	
900
	IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __func__ );
L
Linus Torvalds 已提交
901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916
}

/*
 * Function ali_ircc_sir_write_wakeup (tty)
 *
 *    Called by the driver when there's room for more data.  If we have
 *    more packets to send, we send them here.
 *
 */
static void ali_ircc_sir_write_wakeup(struct ali_ircc_cb *self)
{
	int actual = 0;
	int iobase;	

	IRDA_ASSERT(self != NULL, return;);

917
	IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __func__ );
L
Linus Torvalds 已提交
918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935
	
	iobase = self->io.sir_base;

	/* Finished with frame?  */
	if (self->tx_buff.len > 0)  
	{
		/* Write data left in transmit buffer */
		actual = ali_ircc_sir_write(iobase, self->io.fifo_size, 
				      self->tx_buff.data, self->tx_buff.len);
		self->tx_buff.data += actual;
		self->tx_buff.len  -= actual;
	} 
	else 
	{
		if (self->new_speed) 
		{
			/* We must wait until all data are gone */
			while(!(inb(iobase+UART_LSR) & UART_LSR_TEMT))
936
				IRDA_DEBUG(1, "%s(), UART_LSR_THRE\n", __func__ );
L
Linus Torvalds 已提交
937
			
938
			IRDA_DEBUG(1, "%s(), Changing speed! self->new_speed = %d\n", __func__ , self->new_speed);
L
Linus Torvalds 已提交
939 940 941 942 943 944
			ali_ircc_change_speed(self, self->new_speed);
			self->new_speed = 0;			
			
			// benjamin 2000/11/10 06:32PM
			if (self->io.speed > 115200)
			{
945
				IRDA_DEBUG(2, "%s(), ali_ircc_change_speed from UART_LSR_TEMT\n", __func__ );
L
Linus Torvalds 已提交
946 947 948 949 950 951 952 953 954 955 956
					
				self->ier = IER_EOM;
				// SetCOMInterrupts(self, TRUE);							
				return;							
			}
		}
		else
		{
			netif_wake_queue(self->netdev);	
		}
			
957
		self->netdev->stats.tx_packets++;
L
Linus Torvalds 已提交
958 959 960 961 962
		
		/* Turn on receive interrupts */
		outb(UART_IER_RDI, iobase+UART_IER);
	}
		
963
	IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __func__ );
L
Linus Torvalds 已提交
964 965 966 967 968 969 970
}

static void ali_ircc_change_speed(struct ali_ircc_cb *self, __u32 baud)
{
	struct net_device *dev = self->netdev;
	int iobase;
	
971
	IRDA_DEBUG(1, "%s(), ---------------- Start ----------------\n", __func__ );
L
Linus Torvalds 已提交
972
	
973
	IRDA_DEBUG(2, "%s(), setting speed = %d\n", __func__ , baud);
L
Linus Torvalds 已提交
974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989
	
	/* This function *must* be called with irq off and spin-lock.
	 * - Jean II */

	iobase = self->io.fir_base;
	
	SetCOMInterrupts(self, FALSE); // 2000/11/24 11:43AM
	
	/* Go to MIR, FIR Speed */
	if (baud > 115200)
	{
		
					
		ali_ircc_fir_change_speed(self, baud);			
		
		/* Install FIR xmit handler*/
990
		dev->netdev_ops = &ali_ircc_fir_ops;
L
Linus Torvalds 已提交
991 992 993 994 995 996 997 998 999 1000 1001 1002 1003
				
		/* Enable Interuupt */
		self->ier = IER_EOM; // benjamin 2000/11/20 07:24PM					
				
		/* Be ready for incomming frames */
		ali_ircc_dma_receive(self);	// benajmin 2000/11/8 07:46PM not complete
	}	
	/* Go to SIR Speed */
	else
	{
		ali_ircc_sir_change_speed(self, baud);
				
		/* Install SIR xmit handler*/
1004
		dev->netdev_ops = &ali_ircc_sir_ops;
L
Linus Torvalds 已提交
1005 1006 1007 1008 1009 1010 1011
	}
	
		
	SetCOMInterrupts(self, TRUE);	// 2000/11/24 11:43AM
		
	netif_wake_queue(self->netdev);	
	
1012
	IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __func__ );
L
Linus Torvalds 已提交
1013 1014 1015 1016 1017 1018 1019 1020 1021
}

static void ali_ircc_fir_change_speed(struct ali_ircc_cb *priv, __u32 baud)
{
		
	int iobase; 
	struct ali_ircc_cb *self = (struct ali_ircc_cb *) priv;
	struct net_device *dev;

1022
	IRDA_DEBUG(1, "%s(), ---------------- Start ----------------\n", __func__ );
L
Linus Torvalds 已提交
1023 1024 1025 1026 1027 1028
		
	IRDA_ASSERT(self != NULL, return;);

	dev = self->netdev;
	iobase = self->io.fir_base;
	
1029
	IRDA_DEBUG(1, "%s(), self->io.speed = %d, change to speed = %d\n", __func__ ,self->io.speed,baud);
L
Linus Torvalds 已提交
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
	
	/* Come from SIR speed */
	if(self->io.speed <=115200)
	{
		SIR2FIR(iobase);
	}
		
	/* Update accounting for new speed */
	self->io.speed = baud;
		
	// Set Dongle Speed mode
	ali_ircc_change_dongle_speed(self, baud);
		
1043
	IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __func__ );
L
Linus Torvalds 已提交
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
}

/*
 * Function ali_sir_change_speed (self, speed)
 *
 *    Set speed of IrDA port to specified baudrate
 *
 */
static void ali_ircc_sir_change_speed(struct ali_ircc_cb *priv, __u32 speed)
{
	struct ali_ircc_cb *self = (struct ali_ircc_cb *) priv;
	unsigned long flags;
	int iobase; 
	int fcr;    /* FIFO control reg */
	int lcr;    /* Line control reg */
	int divisor;

1061
	IRDA_DEBUG(1, "%s(), ---------------- Start ----------------\n", __func__ );
L
Linus Torvalds 已提交
1062
	
1063
	IRDA_DEBUG(1, "%s(), Setting speed to: %d\n", __func__ , speed);
L
Linus Torvalds 已提交
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116

	IRDA_ASSERT(self != NULL, return;);

	iobase = self->io.sir_base;
	
	/* Come from MIR or FIR speed */
	if(self->io.speed >115200)
	{	
		// Set Dongle Speed mode first
		ali_ircc_change_dongle_speed(self, speed);
			
		FIR2SIR(iobase);
	}
		
	// Clear Line and Auxiluary status registers 2000/11/24 11:47AM
		
	inb(iobase+UART_LSR);
	inb(iobase+UART_SCR);
		
	/* Update accounting for new speed */
	self->io.speed = speed;

	spin_lock_irqsave(&self->lock, flags);

	divisor = 115200/speed;
	
	fcr = UART_FCR_ENABLE_FIFO;

	/* 
	 * Use trigger level 1 to avoid 3 ms. timeout delay at 9600 bps, and
	 * almost 1,7 ms at 19200 bps. At speeds above that we can just forget
	 * about this timeout since it will always be fast enough. 
	 */
	if (self->io.speed < 38400)
		fcr |= UART_FCR_TRIGGER_1;
	else 
		fcr |= UART_FCR_TRIGGER_14;
        
	/* IrDA ports use 8N1 */
	lcr = UART_LCR_WLEN8;
	
	outb(UART_LCR_DLAB | lcr, iobase+UART_LCR); /* Set DLAB */
	outb(divisor & 0xff,      iobase+UART_DLL); /* Set speed */
	outb(divisor >> 8,	  iobase+UART_DLM);
	outb(lcr,		  iobase+UART_LCR); /* Set 8N1	*/
	outb(fcr,		  iobase+UART_FCR); /* Enable FIFO's */

	/* without this, the conection will be broken after come back from FIR speed,
	   but with this, the SIR connection is harder to established */
	outb((UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2), iobase+UART_MCR);
	
	spin_unlock_irqrestore(&self->lock, flags);
	
1117
	IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __func__ );
L
Linus Torvalds 已提交
1118 1119 1120 1121 1122 1123 1124 1125 1126
}

static void ali_ircc_change_dongle_speed(struct ali_ircc_cb *priv, int speed)
{
	
	struct ali_ircc_cb *self = (struct ali_ircc_cb *) priv;
	int iobase,dongle_id;
	int tmp = 0;
			
1127
	IRDA_DEBUG(1, "%s(), ---------------- Start ----------------\n", __func__ );
L
Linus Torvalds 已提交
1128 1129 1130 1131 1132 1133
	
	iobase = self->io.fir_base; 	/* or iobase = self->io.sir_base; */
	dongle_id = self->io.dongle_id;
	
	/* We are already locked, no need to do it again */
		
1134
	IRDA_DEBUG(1, "%s(), Set Speed for %s , Speed = %d\n", __func__ , dongle_types[dongle_id], speed);
L
Linus Torvalds 已提交
1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297
	
	switch_bank(iobase, BANK2);
	tmp = inb(iobase+FIR_IRDA_CR);
		
	/* IBM type dongle */
	if(dongle_id == 0)
	{				
		if(speed == 4000000)
		{
			//	      __ __	
			// SD/MODE __|     |__ __
			//               __ __ 
			// IRTX    __ __|     |__
			//         T1 T2 T3 T4 T5
			
			tmp &=  ~IRDA_CR_HDLC;		// HDLC=0
			tmp |= IRDA_CR_CRC;	   	// CRC=1
			
			switch_bank(iobase, BANK2);
			outb(tmp, iobase+FIR_IRDA_CR);
			
      			// T1 -> SD/MODE:0 IRTX:0
      			tmp &= ~0x09;
      			tmp |= 0x02;
      			outb(tmp, iobase+FIR_IRDA_CR);
      			udelay(2);
      			
      			// T2 -> SD/MODE:1 IRTX:0
      			tmp &= ~0x01;
      			tmp |= 0x0a;
      			outb(tmp, iobase+FIR_IRDA_CR);
      			udelay(2);
      			
      			// T3 -> SD/MODE:1 IRTX:1
      			tmp |= 0x0b;
      			outb(tmp, iobase+FIR_IRDA_CR);
      			udelay(2);
      			
      			// T4 -> SD/MODE:0 IRTX:1
      			tmp &= ~0x08;
      			tmp |= 0x03;
      			outb(tmp, iobase+FIR_IRDA_CR);
      			udelay(2);
      			
      			// T5 -> SD/MODE:0 IRTX:0
      			tmp &= ~0x09;
      			tmp |= 0x02;
      			outb(tmp, iobase+FIR_IRDA_CR);
      			udelay(2);
      			
      			// reset -> Normal TX output Signal
      			outb(tmp & ~0x02, iobase+FIR_IRDA_CR);      			
		}
		else /* speed <=1152000 */
		{	
			//	      __	
			// SD/MODE __|  |__
			//
			// IRTX    ________
			//         T1 T2 T3  
			
			/* MIR 115200, 57600 */
			if (speed==1152000)
			{
				tmp |= 0xA0;	   //HDLC=1, 1.152Mbps=1
      			}
      			else
      			{
				tmp &=~0x80;	   //HDLC 0.576Mbps
				tmp |= 0x20;	   //HDLC=1,
      			}			
      			
      			tmp |= IRDA_CR_CRC;	   	// CRC=1
      			
      			switch_bank(iobase, BANK2);
      			outb(tmp, iobase+FIR_IRDA_CR);
						
			/* MIR 115200, 57600 */	
						
			//switch_bank(iobase, BANK2);			
			// T1 -> SD/MODE:0 IRTX:0
      			tmp &= ~0x09;
      			tmp |= 0x02;
      			outb(tmp, iobase+FIR_IRDA_CR);
      			udelay(2);
      			
      			// T2 -> SD/MODE:1 IRTX:0
      			tmp &= ~0x01;     
      			tmp |= 0x0a;      
      			outb(tmp, iobase+FIR_IRDA_CR);
      			
      			// T3 -> SD/MODE:0 IRTX:0
      			tmp &= ~0x09;
      			tmp |= 0x02;
      			outb(tmp, iobase+FIR_IRDA_CR);
      			udelay(2);
      			
      			// reset -> Normal TX output Signal
      			outb(tmp & ~0x02, iobase+FIR_IRDA_CR);      						
		}		
	}
	else if (dongle_id == 1) /* HP HDSL-3600 */
	{
		switch(speed)
		{
		case 4000000:
			tmp &=  ~IRDA_CR_HDLC;	// HDLC=0
			break;	
			
		case 1152000:
			tmp |= 0xA0;	   	// HDLC=1, 1.152Mbps=1
      			break;
      			
      		case 576000:
      			tmp &=~0x80;	   	// HDLC 0.576Mbps
			tmp |= 0x20;	   	// HDLC=1,
			break;
      		}			
			
		tmp |= IRDA_CR_CRC;	   	// CRC=1
			
		switch_bank(iobase, BANK2);
      		outb(tmp, iobase+FIR_IRDA_CR);		
	}
	else /* HP HDSL-1100 */
	{
		if(speed <= 115200) /* SIR */
		{
			
			tmp &= ~IRDA_CR_FIR_SIN;	// HP sin select = 0
			
			switch_bank(iobase, BANK2);
      			outb(tmp, iobase+FIR_IRDA_CR);			
		}
		else /* MIR FIR */
		{	
			
			switch(speed)
			{
			case 4000000:
				tmp &=  ~IRDA_CR_HDLC;	// HDLC=0
				break;	
			
			case 1152000:
				tmp |= 0xA0;	   	// HDLC=1, 1.152Mbps=1
      				break;
      			
      			case 576000:
      				tmp &=~0x80;	   	// HDLC 0.576Mbps
				tmp |= 0x20;	   	// HDLC=1,
				break;
      			}			
			
			tmp |= IRDA_CR_CRC;	   	// CRC=1
			tmp |= IRDA_CR_FIR_SIN;		// HP sin select = 1
			
			switch_bank(iobase, BANK2);
      			outb(tmp, iobase+FIR_IRDA_CR);			
		}
	}
			
	switch_bank(iobase, BANK0);
	
1298
	IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __func__ );
L
Linus Torvalds 已提交
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310
}

/*
 * Function ali_ircc_sir_write (driver)
 *
 *    Fill Tx FIFO with transmit data
 *
 */
static int ali_ircc_sir_write(int iobase, int fifo_size, __u8 *buf, int len)
{
	int actual = 0;
	
1311
	IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __func__ );
L
Linus Torvalds 已提交
1312 1313 1314
		
	/* Tx FIFO should be empty! */
	if (!(inb(iobase+UART_LSR) & UART_LSR_THRE)) {
1315
		IRDA_DEBUG(0, "%s(), failed, fifo not empty!\n", __func__ );
L
Linus Torvalds 已提交
1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326
		return 0;
	}
        
	/* Fill FIFO with current frame */
	while ((fifo_size-- > 0) && (actual < len)) {
		/* Transmit next byte */
		outb(buf[actual], iobase+UART_TX);

		actual++;
	}
	
1327
        IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __func__ );
L
Linus Torvalds 已提交
1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342
	return actual;
}

/*
 * Function ali_ircc_net_open (dev)
 *
 *    Start the device
 *
 */
static int ali_ircc_net_open(struct net_device *dev)
{
	struct ali_ircc_cb *self;
	int iobase;
	char hwname[32];
		
1343
	IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __func__ );
L
Linus Torvalds 已提交
1344 1345 1346
	
	IRDA_ASSERT(dev != NULL, return -1;);
	
1347
	self = netdev_priv(dev);
L
Linus Torvalds 已提交
1348 1349 1350 1351 1352 1353 1354 1355
	
	IRDA_ASSERT(self != NULL, return 0;);
	
	iobase = self->io.fir_base;
	
	/* Request IRQ and install Interrupt Handler */
	if (request_irq(self->io.irq, ali_ircc_interrupt, 0, dev->name, dev)) 
	{
1356 1357
		IRDA_WARNING("%s, unable to allocate irq=%d\n",
			     ALI_IRCC_DRIVER_NAME,
L
Linus Torvalds 已提交
1358 1359 1360 1361 1362 1363 1364 1365 1366
			     self->io.irq);
		return -EAGAIN;
	}
	
	/*
	 * Always allocate the DMA channel after the IRQ, and clean up on 
	 * failure.
	 */
	if (request_dma(self->io.dma, dev->name)) {
1367 1368
		IRDA_WARNING("%s, unable to allocate dma=%d\n",
			     ALI_IRCC_DRIVER_NAME,
L
Linus Torvalds 已提交
1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388
			     self->io.dma);
		free_irq(self->io.irq, self);
		return -EAGAIN;
	}
	
	/* Turn on interrups */
	outb(UART_IER_RDI , iobase+UART_IER);

	/* Ready to play! */
	netif_start_queue(dev); //benjamin by irport
	
	/* Give self a hardware name */
	sprintf(hwname, "ALI-FIR @ 0x%03x", self->io.fir_base);

	/* 
	 * Open new IrLAP layer instance, now that everything should be
	 * initialized properly 
	 */
	self->irlap = irlap_open(dev, &self->qos, hwname);
		
1389
	IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __func__ );
L
Linus Torvalds 已提交
1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405
	
	return 0;
}

/*
 * Function ali_ircc_net_close (dev)
 *
 *    Stop the device
 *
 */
static int ali_ircc_net_close(struct net_device *dev)
{	

	struct ali_ircc_cb *self;
	//int iobase;
			
1406
	IRDA_DEBUG(4, "%s(), ---------------- Start ----------------\n", __func__ );
L
Linus Torvalds 已提交
1407 1408 1409
		
	IRDA_ASSERT(dev != NULL, return -1;);

1410
	self = netdev_priv(dev);
L
Linus Torvalds 已提交
1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428
	IRDA_ASSERT(self != NULL, return 0;);

	/* Stop device */
	netif_stop_queue(dev);
	
	/* Stop and remove instance of IrLAP */
	if (self->irlap)
		irlap_close(self->irlap);
	self->irlap = NULL;
		
	disable_dma(self->io.dma);

	/* Disable interrupts */
	SetCOMInterrupts(self, FALSE);
	       
	free_irq(self->io.irq, dev);
	free_dma(self->io.dma);

1429
	IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __func__ );
L
Linus Torvalds 已提交
1430 1431 1432 1433 1434 1435 1436 1437 1438 1439
	
	return 0;
}

/*
 * Function ali_ircc_fir_hard_xmit (skb, dev)
 *
 *    Transmit the frame
 *
 */
S
Stephen Hemminger 已提交
1440 1441
static netdev_tx_t ali_ircc_fir_hard_xmit(struct sk_buff *skb,
						struct net_device *dev)
L
Linus Torvalds 已提交
1442 1443 1444 1445 1446 1447 1448
{
	struct ali_ircc_cb *self;
	unsigned long flags;
	int iobase;
	__u32 speed;
	int mtt, diff;
	
1449
	IRDA_DEBUG(1, "%s(), ---------------- Start -----------------\n", __func__ );
L
Linus Torvalds 已提交
1450
	
1451
	self = netdev_priv(dev);
L
Linus Torvalds 已提交
1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471
	iobase = self->io.fir_base;

	netif_stop_queue(dev);
	
	/* Make sure tests *& speed change are atomic */
	spin_lock_irqsave(&self->lock, flags);
	
	/* Note : you should make sure that speed changes are not going
	 * to corrupt any outgoing frame. Look at nsc-ircc for the gory
	 * details - Jean II */

	/* Check if we need to change the speed */
	speed = irda_get_next_speed(skb);
	if ((speed != self->io.speed) && (speed != -1)) {
		/* Check for empty frame */
		if (!skb->len) {
			ali_ircc_change_speed(self, speed); 
			dev->trans_start = jiffies;
			spin_unlock_irqrestore(&self->lock, flags);
			dev_kfree_skb(skb);
1472
			return NETDEV_TX_OK;
L
Linus Torvalds 已提交
1473 1474 1475 1476 1477 1478 1479 1480 1481
		} else
			self->new_speed = speed;
	}

	/* Register and copy this frame to DMA memory */
	self->tx_fifo.queue[self->tx_fifo.free].start = self->tx_fifo.tail;
	self->tx_fifo.queue[self->tx_fifo.free].len = skb->len;
	self->tx_fifo.tail += skb->len;

1482
	dev->stats.tx_bytes += skb->len;
L
Linus Torvalds 已提交
1483

1484 1485
	skb_copy_from_linear_data(skb, self->tx_fifo.queue[self->tx_fifo.free].start,
		      skb->len);
L
Linus Torvalds 已提交
1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502
	self->tx_fifo.len++;
	self->tx_fifo.free++;

	/* Start transmit only if there is currently no transmit going on */
	if (self->tx_fifo.len == 1) 
	{
		/* Check if we must wait the min turn time or not */
		mtt = irda_get_mtt(skb);
				
		if (mtt) 
		{
			/* Check how much time we have used already */
			do_gettimeofday(&self->now);
			
			diff = self->now.tv_usec - self->stamp.tv_usec;
			/* self->stamp is set from ali_ircc_dma_receive_complete() */
							
1503
			IRDA_DEBUG(1, "%s(), ******* diff = %d *******\n", __func__ , diff);
L
Linus Torvalds 已提交
1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524
			
			if (diff < 0) 
				diff += 1000000;
			
			/* Check if the mtt is larger than the time we have
			 * already used by all the protocol processing
			 */
			if (mtt > diff)
			{				
				mtt -= diff;
								
				/* 
				 * Use timer if delay larger than 1000 us, and
				 * use udelay for smaller values which should
				 * be acceptable
				 */
				if (mtt > 500) 
				{
					/* Adjust for timer resolution */
					mtt = (mtt+250) / 500; 	/* 4 discard, 5 get advanced, Let's round off */
					
1525
					IRDA_DEBUG(1, "%s(), ************** mtt = %d ***********\n", __func__ , mtt);
L
Linus Torvalds 已提交
1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581
					
					/* Setup timer */
					if (mtt == 1) /* 500 us */
					{
						switch_bank(iobase, BANK1);
						outb(TIMER_IIR_500, iobase+FIR_TIMER_IIR);
					}	
					else if (mtt == 2) /* 1 ms */
					{
						switch_bank(iobase, BANK1);
						outb(TIMER_IIR_1ms, iobase+FIR_TIMER_IIR);
					}					
					else /* > 2ms -> 4ms */
					{
						switch_bank(iobase, BANK1);
						outb(TIMER_IIR_2ms, iobase+FIR_TIMER_IIR);
					}
					
					
					/* Start timer */
					outb(inb(iobase+FIR_CR) | CR_TIMER_EN, iobase+FIR_CR);
					self->io.direction = IO_XMIT;
					
					/* Enable timer interrupt */
					self->ier = IER_TIMER;
					SetCOMInterrupts(self, TRUE);					
					
					/* Timer will take care of the rest */
					goto out; 
				} 
				else
					udelay(mtt);
			} // if (if (mtt > diff)
		}// if (mtt) 
				
		/* Enable EOM interrupt */
		self->ier = IER_EOM;
		SetCOMInterrupts(self, TRUE);
		
		/* Transmit frame */
		ali_ircc_dma_xmit(self);
	} // if (self->tx_fifo.len == 1) 
	
 out:
 	
	/* Not busy transmitting anymore if window is not full */
	if (self->tx_fifo.free < MAX_TX_WINDOW)
		netif_wake_queue(self->netdev);
	
	/* Restore bank register */
	switch_bank(iobase, BANK0);

	dev->trans_start = jiffies;
	spin_unlock_irqrestore(&self->lock, flags);
	dev_kfree_skb(skb);

1582
	IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __func__ );
1583
	return NETDEV_TX_OK;	
L
Linus Torvalds 已提交
1584 1585 1586 1587 1588 1589 1590 1591 1592
}


static void ali_ircc_dma_xmit(struct ali_ircc_cb *self)
{
	int iobase, tmp;
	unsigned char FIFO_OPTI, Hi, Lo;
	
	
1593
	IRDA_DEBUG(1, "%s(), ---------------- Start -----------------\n", __func__ );
L
Linus Torvalds 已提交
1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643
	
	iobase = self->io.fir_base;
	
	/* FIFO threshold , this method comes from NDIS5 code */
	
	if(self->tx_fifo.queue[self->tx_fifo.ptr].len < TX_FIFO_Threshold)
		FIFO_OPTI = self->tx_fifo.queue[self->tx_fifo.ptr].len-1;
	else
		FIFO_OPTI = TX_FIFO_Threshold;
	
	/* Disable DMA */
	switch_bank(iobase, BANK1);
	outb(inb(iobase+FIR_CR) & ~CR_DMA_EN, iobase+FIR_CR);
	
	self->io.direction = IO_XMIT;
	
	irda_setup_dma(self->io.dma, 
		       ((u8 *)self->tx_fifo.queue[self->tx_fifo.ptr].start -
			self->tx_buff.head) + self->tx_buff_dma,
		       self->tx_fifo.queue[self->tx_fifo.ptr].len, 
		       DMA_TX_MODE);
		
	/* Reset Tx FIFO */
	switch_bank(iobase, BANK0);
	outb(LCR_A_FIFO_RESET, iobase+FIR_LCR_A);
	
	/* Set Tx FIFO threshold */
	if (self->fifo_opti_buf!=FIFO_OPTI) 
	{
		switch_bank(iobase, BANK1);
	    	outb(FIFO_OPTI, iobase+FIR_FIFO_TR) ;
	    	self->fifo_opti_buf=FIFO_OPTI;
	}
	
	/* Set Tx DMA threshold */
	switch_bank(iobase, BANK1);
	outb(TX_DMA_Threshold, iobase+FIR_DMA_TR);
	
	/* Set max Tx frame size */
	Hi = (self->tx_fifo.queue[self->tx_fifo.ptr].len >> 8) & 0x0f;
	Lo = self->tx_fifo.queue[self->tx_fifo.ptr].len & 0xff;
	switch_bank(iobase, BANK2);
	outb(Hi, iobase+FIR_TX_DSR_HI);
	outb(Lo, iobase+FIR_TX_DSR_LO);
	
	/* Disable SIP , Disable Brick Wall (we don't support in TX mode), Change to TX mode */
	switch_bank(iobase, BANK0);	
	tmp = inb(iobase+FIR_LCR_B);
	tmp &= ~0x20; // Disable SIP
	outb(((unsigned char)(tmp & 0x3f) | LCR_B_TX_MODE) & ~LCR_B_BW, iobase+FIR_LCR_B);
1644
	IRDA_DEBUG(1, "%s(), *** Change to TX mode: FIR_LCR_B = 0x%x ***\n", __func__ , inb(iobase+FIR_LCR_B));
L
Linus Torvalds 已提交
1645 1646 1647 1648 1649 1650 1651 1652 1653
	
	outb(0, iobase+FIR_LSR);
			
	/* Enable DMA and Burst Mode */
	switch_bank(iobase, BANK1);
	outb(inb(iobase+FIR_CR) | CR_DMA_EN | CR_DMA_BURST, iobase+FIR_CR);
	
	switch_bank(iobase, BANK0); 
	
1654
	IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __func__ );
L
Linus Torvalds 已提交
1655 1656 1657 1658 1659 1660 1661
}

static int  ali_ircc_dma_xmit_complete(struct ali_ircc_cb *self)
{
	int iobase;
	int ret = TRUE;
	
1662
	IRDA_DEBUG(1, "%s(), ---------------- Start -----------------\n", __func__ );
L
Linus Torvalds 已提交
1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674
	
	iobase = self->io.fir_base;
	
	/* Disable DMA */
	switch_bank(iobase, BANK1);
	outb(inb(iobase+FIR_CR) & ~CR_DMA_EN, iobase+FIR_CR);
	
	/* Check for underrun! */
	switch_bank(iobase, BANK0);
	if((inb(iobase+FIR_LSR) & LSR_FRAME_ABORT) == LSR_FRAME_ABORT)
	
	{
1675
		IRDA_ERROR("%s(), ********* LSR_FRAME_ABORT *********\n", __func__);
1676 1677
		self->netdev->stats.tx_errors++;
		self->netdev->stats.tx_fifo_errors++;
L
Linus Torvalds 已提交
1678 1679 1680
	}
	else 
	{
1681
		self->netdev->stats.tx_packets++;
L
Linus Torvalds 已提交
1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717
	}

	/* Check if we need to change the speed */
	if (self->new_speed) 
	{
		ali_ircc_change_speed(self, self->new_speed);
		self->new_speed = 0;
	}

	/* Finished with this frame, so prepare for next */
	self->tx_fifo.ptr++;
	self->tx_fifo.len--;

	/* Any frames to be sent back-to-back? */
	if (self->tx_fifo.len) 
	{
		ali_ircc_dma_xmit(self);
		
		/* Not finished yet! */
		ret = FALSE;
	} 
	else 
	{	/* Reset Tx FIFO info */
		self->tx_fifo.len = self->tx_fifo.ptr = self->tx_fifo.free = 0;
		self->tx_fifo.tail = self->tx_buff.head;
	}

	/* Make sure we have room for more frames */
	if (self->tx_fifo.free < MAX_TX_WINDOW) {
		/* Not busy transmitting anymore */
		/* Tell the network layer, that we can accept more frames */
		netif_wake_queue(self->netdev);
	}
		
	switch_bank(iobase, BANK0); 
	
1718
	IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __func__ );
L
Linus Torvalds 已提交
1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732
	return ret;
}

/*
 * Function ali_ircc_dma_receive (self)
 *
 *    Get ready for receiving a frame. The device will initiate a DMA
 *    if it starts to receive a frame.
 *
 */
static int ali_ircc_dma_receive(struct ali_ircc_cb *self) 
{
	int iobase, tmp;
	
1733
	IRDA_DEBUG(1, "%s(), ---------------- Start -----------------\n", __func__ );
L
Linus Torvalds 已提交
1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770
	
	iobase = self->io.fir_base;
	
	/* Reset Tx FIFO info */
	self->tx_fifo.len = self->tx_fifo.ptr = self->tx_fifo.free = 0;
	self->tx_fifo.tail = self->tx_buff.head;
		
	/* Disable DMA */
	switch_bank(iobase, BANK1);
	outb(inb(iobase+FIR_CR) & ~CR_DMA_EN, iobase+FIR_CR);
	
	/* Reset Message Count */
	switch_bank(iobase, BANK0);
	outb(0x07, iobase+FIR_LSR);
		
	self->rcvFramesOverflow = FALSE;	
	
	self->LineStatus = inb(iobase+FIR_LSR) ;
	
	/* Reset Rx FIFO info */
	self->io.direction = IO_RECV;
	self->rx_buff.data = self->rx_buff.head;
		
	/* Reset Rx FIFO */
	// switch_bank(iobase, BANK0);
	outb(LCR_A_FIFO_RESET, iobase+FIR_LCR_A); 
	
	self->st_fifo.len = self->st_fifo.pending_bytes = 0;
	self->st_fifo.tail = self->st_fifo.head = 0;
		
	irda_setup_dma(self->io.dma, self->rx_buff_dma, self->rx_buff.truesize,
		       DMA_RX_MODE);
	 
	/* Set Receive Mode,Brick Wall */
	//switch_bank(iobase, BANK0);
	tmp = inb(iobase+FIR_LCR_B);
	outb((unsigned char)(tmp &0x3f) | LCR_B_RX_MODE | LCR_B_BW , iobase + FIR_LCR_B); // 2000/12/1 05:16PM
1771
	IRDA_DEBUG(1, "%s(), *** Change To RX mode: FIR_LCR_B = 0x%x ***\n", __func__ , inb(iobase+FIR_LCR_B));
L
Linus Torvalds 已提交
1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782
			
	/* Set Rx Threshold */
	switch_bank(iobase, BANK1);
	outb(RX_FIFO_Threshold, iobase+FIR_FIFO_TR);
	outb(RX_DMA_Threshold, iobase+FIR_DMA_TR);
		
	/* Enable DMA and Burst Mode */
	// switch_bank(iobase, BANK1);
	outb(CR_DMA_EN | CR_DMA_BURST, iobase+FIR_CR);
				
	switch_bank(iobase, BANK0); 
1783
	IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __func__ );
L
Linus Torvalds 已提交
1784 1785 1786 1787 1788 1789 1790 1791 1792 1793
	return 0;
}

static int  ali_ircc_dma_receive_complete(struct ali_ircc_cb *self)
{
	struct st_fifo *st_fifo;
	struct sk_buff *skb;
	__u8 status, MessageCount;
	int len, i, iobase, val;	

1794
	IRDA_DEBUG(1, "%s(), ---------------- Start -----------------\n", __func__ );
L
Linus Torvalds 已提交
1795 1796 1797 1798 1799 1800 1801 1802

	st_fifo = &self->st_fifo;		
	iobase = self->io.fir_base;	
		
	switch_bank(iobase, BANK0);
	MessageCount = inb(iobase+ FIR_LSR)&0x07;
	
	if (MessageCount > 0)	
1803
		IRDA_DEBUG(0, "%s(), Messsage count = %d,\n", __func__ , MessageCount);
L
Linus Torvalds 已提交
1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815
		
	for (i=0; i<=MessageCount; i++)
	{
		/* Bank 0 */
		switch_bank(iobase, BANK0);
		status = inb(iobase+FIR_LSR);
		
		switch_bank(iobase, BANK2);
		len = inb(iobase+FIR_RX_DSR_HI) & 0x0f;
		len = len << 8; 
		len |= inb(iobase+FIR_RX_DSR_LO);
		
1816 1817
		IRDA_DEBUG(1, "%s(), RX Length = 0x%.2x,\n", __func__ , len);
		IRDA_DEBUG(1, "%s(), RX Status = 0x%.2x,\n", __func__ , status);
L
Linus Torvalds 已提交
1818 1819
		
		if (st_fifo->tail >= MAX_RX_WINDOW) {
1820
			IRDA_DEBUG(0, "%s(), window is full!\n", __func__ );
L
Linus Torvalds 已提交
1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842
			continue;
		}
			
		st_fifo->entries[st_fifo->tail].status = status;
		st_fifo->entries[st_fifo->tail].len = len;
		st_fifo->pending_bytes += len;
		st_fifo->tail++;
		st_fifo->len++;
	}
			
	for (i=0; i<=MessageCount; i++)
	{	
		/* Get first entry */
		status = st_fifo->entries[st_fifo->head].status;
		len    = st_fifo->entries[st_fifo->head].len;
		st_fifo->pending_bytes -= len;
		st_fifo->head++;
		st_fifo->len--;			
		
		/* Check for errors */
		if ((status & 0xd8) || self->rcvFramesOverflow || (len==0)) 		
		{
1843
			IRDA_DEBUG(0,"%s(), ************* RX Errors ************\n", __func__ );
L
Linus Torvalds 已提交
1844 1845
			
			/* Skip frame */
1846
			self->netdev->stats.rx_errors++;
L
Linus Torvalds 已提交
1847 1848 1849 1850 1851
			
			self->rx_buff.data += len;
			
			if (status & LSR_FIFO_UR) 
			{
1852
				self->netdev->stats.rx_frame_errors++;
1853
				IRDA_DEBUG(0,"%s(), ************* FIFO Errors ************\n", __func__ );
L
Linus Torvalds 已提交
1854 1855 1856
			}	
			if (status & LSR_FRAME_ERROR)
			{
1857
				self->netdev->stats.rx_frame_errors++;
1858
				IRDA_DEBUG(0,"%s(), ************* FRAME Errors ************\n", __func__ );
L
Linus Torvalds 已提交
1859 1860 1861 1862
			}
							
			if (status & LSR_CRC_ERROR) 
			{
1863
				self->netdev->stats.rx_crc_errors++;
1864
				IRDA_DEBUG(0,"%s(), ************* CRC Errors ************\n", __func__ );
L
Linus Torvalds 已提交
1865 1866 1867 1868
			}
			
			if(self->rcvFramesOverflow)
			{
1869
				self->netdev->stats.rx_frame_errors++;
1870
				IRDA_DEBUG(0,"%s(), ************* Overran DMA buffer ************\n", __func__ );
L
Linus Torvalds 已提交
1871 1872 1873
			}
			if(len == 0)
			{
1874
				self->netdev->stats.rx_frame_errors++;
1875
				IRDA_DEBUG(0,"%s(), ********** Receive Frame Size = 0 *********\n", __func__ );
L
Linus Torvalds 已提交
1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886
			}
		}	 
		else 
		{
			
			if (st_fifo->pending_bytes < 32) 
			{
				switch_bank(iobase, BANK0);
				val = inb(iobase+FIR_BSR);	
				if ((val& BSR_FIFO_NOT_EMPTY)== 0x80) 
				{
1887
					IRDA_DEBUG(0, "%s(), ************* BSR_FIFO_NOT_EMPTY ************\n", __func__ );
L
Linus Torvalds 已提交
1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923
					
					/* Put this entry back in fifo */
					st_fifo->head--;
					st_fifo->len++;
					st_fifo->pending_bytes += len;
					st_fifo->entries[st_fifo->head].status = status;
					st_fifo->entries[st_fifo->head].len = len;
						
					/*  
		 			* DMA not finished yet, so try again 
		 			* later, set timer value, resolution 
		 			* 500 us 
		 			*/
					 
					switch_bank(iobase, BANK1);
					outb(TIMER_IIR_500, iobase+FIR_TIMER_IIR); // 2001/1/2 05:07PM
					
					/* Enable Timer */
					outb(inb(iobase+FIR_CR) | CR_TIMER_EN, iobase+FIR_CR);
						
					return FALSE; /* I'll be back! */
				}
			}		
			
			/* 
			 * Remember the time we received this frame, so we can
			 * reduce the min turn time a bit since we will know
			 * how much time we have used for protocol processing
			 */
			do_gettimeofday(&self->stamp);

			skb = dev_alloc_skb(len+1);
			if (skb == NULL)  
			{
				IRDA_WARNING("%s(), memory squeeze, "
					     "dropping frame.\n",
1924
					     __func__);
1925
				self->netdev->stats.rx_dropped++;
L
Linus Torvalds 已提交
1926 1927 1928 1929 1930 1931 1932 1933 1934

				return FALSE;
			}
			
			/* Make sure IP header gets aligned */
			skb_reserve(skb, 1); 
			
			/* Copy frame without CRC, CRC is removed by hardware*/
			skb_put(skb, len);
1935
			skb_copy_to_linear_data(skb, self->rx_buff.data, len);
L
Linus Torvalds 已提交
1936 1937 1938

			/* Move to next frame */
			self->rx_buff.data += len;
1939 1940
			self->netdev->stats.rx_bytes += len;
			self->netdev->stats.rx_packets++;
L
Linus Torvalds 已提交
1941 1942

			skb->dev = self->netdev;
1943
			skb_reset_mac_header(skb);
L
Linus Torvalds 已提交
1944 1945 1946 1947 1948 1949 1950
			skb->protocol = htons(ETH_P_IRDA);
			netif_rx(skb);
		}
	}
	
	switch_bank(iobase, BANK0);	
		
1951
	IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __func__ );
L
Linus Torvalds 已提交
1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962
	return TRUE;
}



/*
 * Function ali_ircc_sir_hard_xmit (skb, dev)
 *
 *    Transmit the frame!
 *
 */
S
Stephen Hemminger 已提交
1963 1964
static netdev_tx_t ali_ircc_sir_hard_xmit(struct sk_buff *skb,
						struct net_device *dev)
L
Linus Torvalds 已提交
1965 1966 1967 1968 1969 1970
{
	struct ali_ircc_cb *self;
	unsigned long flags;
	int iobase;
	__u32 speed;
	
1971
	IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __func__ );
L
Linus Torvalds 已提交
1972
	
1973
	IRDA_ASSERT(dev != NULL, return NETDEV_TX_OK;);
L
Linus Torvalds 已提交
1974
	
1975
	self = netdev_priv(dev);
1976
	IRDA_ASSERT(self != NULL, return NETDEV_TX_OK;);
L
Linus Torvalds 已提交
1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997

	iobase = self->io.sir_base;

	netif_stop_queue(dev);
	
	/* Make sure tests *& speed change are atomic */
	spin_lock_irqsave(&self->lock, flags);

	/* Note : you should make sure that speed changes are not going
	 * to corrupt any outgoing frame. Look at nsc-ircc for the gory
	 * details - Jean II */

	/* Check if we need to change the speed */
	speed = irda_get_next_speed(skb);
	if ((speed != self->io.speed) && (speed != -1)) {
		/* Check for empty frame */
		if (!skb->len) {
			ali_ircc_change_speed(self, speed); 
			dev->trans_start = jiffies;
			spin_unlock_irqrestore(&self->lock, flags);
			dev_kfree_skb(skb);
1998
			return NETDEV_TX_OK;
L
Linus Torvalds 已提交
1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009
		} else
			self->new_speed = speed;
	}

	/* Init tx buffer */
	self->tx_buff.data = self->tx_buff.head;

        /* Copy skb to tx_buff while wrapping, stuffing and making CRC */
	self->tx_buff.len = async_wrap_skb(skb, self->tx_buff.data, 
					   self->tx_buff.truesize);
	
2010
	self->netdev->stats.tx_bytes += self->tx_buff.len;
L
Linus Torvalds 已提交
2011 2012 2013 2014 2015 2016 2017 2018 2019

	/* Turn on transmit finished interrupt. Will fire immediately!  */
	outb(UART_IER_THRI, iobase+UART_IER); 

	dev->trans_start = jiffies;
	spin_unlock_irqrestore(&self->lock, flags);

	dev_kfree_skb(skb);
	
2020
	IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __func__ );
L
Linus Torvalds 已提交
2021
	
2022
	return NETDEV_TX_OK;	
L
Linus Torvalds 已提交
2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038
}


/*
 * Function ali_ircc_net_ioctl (dev, rq, cmd)
 *
 *    Process IOCTL commands for this device
 *
 */
static int ali_ircc_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
{
	struct if_irda_req *irq = (struct if_irda_req *) rq;
	struct ali_ircc_cb *self;
	unsigned long flags;
	int ret = 0;
	
2039
	IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __func__ );
L
Linus Torvalds 已提交
2040 2041 2042
	
	IRDA_ASSERT(dev != NULL, return -1;);

2043
	self = netdev_priv(dev);
L
Linus Torvalds 已提交
2044 2045 2046

	IRDA_ASSERT(self != NULL, return -1;);

2047
	IRDA_DEBUG(2, "%s(), %s, (cmd=0x%X)\n", __func__ , dev->name, cmd);
L
Linus Torvalds 已提交
2048 2049 2050
	
	switch (cmd) {
	case SIOCSBANDWIDTH: /* Set bandwidth */
2051
		IRDA_DEBUG(1, "%s(), SIOCSBANDWIDTH\n", __func__ );
L
Linus Torvalds 已提交
2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064
		/*
		 * This function will also be used by IrLAP to change the
		 * speed, so we still must allow for speed change within
		 * interrupt context.
		 */
		if (!in_interrupt() && !capable(CAP_NET_ADMIN))
			return -EPERM;
		
		spin_lock_irqsave(&self->lock, flags);
		ali_ircc_change_speed(self, irq->ifr_baudrate);		
		spin_unlock_irqrestore(&self->lock, flags);
		break;
	case SIOCSMEDIABUSY: /* Set media busy */
2065
		IRDA_DEBUG(1, "%s(), SIOCSMEDIABUSY\n", __func__ );
L
Linus Torvalds 已提交
2066 2067 2068 2069 2070
		if (!capable(CAP_NET_ADMIN))
			return -EPERM;
		irda_device_set_media_busy(self->netdev, TRUE);
		break;
	case SIOCGRECEIVING: /* Check if we are receiving right now */
2071
		IRDA_DEBUG(2, "%s(), SIOCGRECEIVING\n", __func__ );
L
Linus Torvalds 已提交
2072 2073 2074 2075 2076 2077 2078
		/* This is protected */
		irq->ifr_receiving = ali_ircc_is_receiving(self);
		break;
	default:
		ret = -EOPNOTSUPP;
	}
	
2079
	IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __func__ );
L
Linus Torvalds 已提交
2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095
	
	return ret;
}

/*
 * Function ali_ircc_is_receiving (self)
 *
 *    Return TRUE is we are currently receiving a frame
 *
 */
static int ali_ircc_is_receiving(struct ali_ircc_cb *self)
{
	unsigned long flags;
	int status = FALSE;
	int iobase;		
	
2096
	IRDA_DEBUG(2, "%s(), ---------------- Start -----------------\n", __func__ );
L
Linus Torvalds 已提交
2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109
	
	IRDA_ASSERT(self != NULL, return FALSE;);

	spin_lock_irqsave(&self->lock, flags);

	if (self->io.speed > 115200) 
	{
		iobase = self->io.fir_base;
		
		switch_bank(iobase, BANK1);
		if((inb(iobase+FIR_FIFO_FR) & 0x3f) != 0) 		
		{
			/* We are receiving something */
2110
			IRDA_DEBUG(1, "%s(), We are receiving something\n", __func__ );
L
Linus Torvalds 已提交
2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121
			status = TRUE;
		}
		switch_bank(iobase, BANK0);		
	} 
	else
	{ 
		status = (self->rx_buff.state != OUTSIDE_FRAME);
	}
	
	spin_unlock_irqrestore(&self->lock, flags);
	
2122
	IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __func__ );
L
Linus Torvalds 已提交
2123 2124 2125 2126
	
	return status;
}

2127
static int ali_ircc_suspend(struct platform_device *dev, pm_message_t state)
L
Linus Torvalds 已提交
2128
{
2129
	struct ali_ircc_cb *self = platform_get_drvdata(dev);
L
Linus Torvalds 已提交
2130
	
2131
	IRDA_MESSAGE("%s, Suspending\n", ALI_IRCC_DRIVER_NAME);
L
Linus Torvalds 已提交
2132 2133

	if (self->io.suspended)
2134
		return 0;
L
Linus Torvalds 已提交
2135 2136 2137 2138 2139

	ali_ircc_net_close(self->netdev);

	self->io.suspended = 1;
	
2140
	return 0;
L
Linus Torvalds 已提交
2141 2142
}

2143
static int ali_ircc_resume(struct platform_device *dev)
L
Linus Torvalds 已提交
2144
{
2145
	struct ali_ircc_cb *self = platform_get_drvdata(dev);
L
Linus Torvalds 已提交
2146 2147
	
	if (!self->io.suspended)
2148
		return 0;
L
Linus Torvalds 已提交
2149 2150 2151
	
	ali_ircc_net_open(self->netdev);
	
2152
	IRDA_MESSAGE("%s, Waking up\n", ALI_IRCC_DRIVER_NAME);
L
Linus Torvalds 已提交
2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167

	self->io.suspended = 0;

	return 0;
}

/* ALi Chip Function */

static void SetCOMInterrupts(struct ali_ircc_cb *self , unsigned char enable)
{
	
	unsigned char newMask;
	
	int iobase = self->io.fir_base; /* or sir_base */

2168
	IRDA_DEBUG(2, "%s(), -------- Start -------- ( Enable = %d )\n", __func__ , enable);
L
Linus Torvalds 已提交
2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208
	
	/* Enable the interrupt which we wish to */
	if (enable){
		if (self->io.direction == IO_XMIT)
		{
			if (self->io.speed > 115200) /* FIR, MIR */
			{
				newMask = self->ier;
			}
			else /* SIR */
			{
				newMask = UART_IER_THRI | UART_IER_RDI;
			}
		}
		else {
			if (self->io.speed > 115200) /* FIR, MIR */
			{
				newMask = self->ier;
			}
			else /* SIR */
			{
				newMask = UART_IER_RDI;
			}
		}
	}
	else /* Disable all the interrupts */
	{
		newMask = 0x00;

	}

	//SIR and FIR has different registers
	if (self->io.speed > 115200)
	{	
		switch_bank(iobase, BANK0);
		outb(newMask, iobase+FIR_IER);
	}
	else
		outb(newMask, iobase+UART_IER);
		
2209
	IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __func__ );
L
Linus Torvalds 已提交
2210 2211 2212 2213 2214 2215
}

static void SIR2FIR(int iobase)
{
	//unsigned char tmp;
		
2216
	IRDA_DEBUG(1, "%s(), ---------------- Start ----------------\n", __func__ );
L
Linus Torvalds 已提交
2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231
	
	/* Already protected (change_speed() or setup()), no need to lock.
	 * Jean II */
	
	outb(0x28, iobase+UART_MCR);
	outb(0x68, iobase+UART_MCR);
	outb(0x88, iobase+UART_MCR);		
	
	outb(0x60, iobase+FIR_MCR); 	/*  Master Reset */
	outb(0x20, iobase+FIR_MCR); 	/*  Master Interrupt Enable */
	
	//tmp = inb(iobase+FIR_LCR_B);	/* SIP enable */
	//tmp |= 0x20;
	//outb(tmp, iobase+FIR_LCR_B);	
	
2232
	IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __func__ );
L
Linus Torvalds 已提交
2233 2234 2235 2236 2237 2238
}

static void FIR2SIR(int iobase)
{
	unsigned char val;
	
2239
	IRDA_DEBUG(1, "%s(), ---------------- Start ----------------\n", __func__ );
L
Linus Torvalds 已提交
2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254
	
	/* Already protected (change_speed() or setup()), no need to lock.
	 * Jean II */
	
	outb(0x20, iobase+FIR_MCR); 	/* IRQ to low */
	outb(0x00, iobase+UART_IER); 	
		
	outb(0xA0, iobase+FIR_MCR); 	/* Don't set master reset */
	outb(0x00, iobase+UART_FCR);
	outb(0x07, iobase+UART_FCR);		
	
	val = inb(iobase+UART_RX);
	val = inb(iobase+UART_LSR);
	val = inb(iobase+UART_MSR);
	
2255
	IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __func__ );
L
Linus Torvalds 已提交
2256 2257 2258 2259 2260
}

MODULE_AUTHOR("Benjamin Kong <benjamin_kong@ali.com.tw>");
MODULE_DESCRIPTION("ALi FIR Controller Driver");
MODULE_LICENSE("GPL");
2261
MODULE_ALIAS("platform:" ALI_IRCC_DRIVER_NAME);
L
Linus Torvalds 已提交
2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272


module_param_array(io, int, NULL, 0);
MODULE_PARM_DESC(io, "Base I/O addresses");
module_param_array(irq, int, NULL, 0);
MODULE_PARM_DESC(irq, "IRQ lines");
module_param_array(dma, int, NULL, 0);
MODULE_PARM_DESC(dma, "DMA channels");

module_init(ali_ircc_init);
module_exit(ali_ircc_cleanup);