w83977af_ir.c 29.6 KB
Newer Older
L
Linus Torvalds 已提交
1
/*********************************************************************
2
 *
L
Linus Torvalds 已提交
3 4 5 6 7 8 9 10
 * Filename:      w83977af_ir.c
 * Version:       1.0
 * Description:   FIR driver for the Winbond W83977AF Super I/O chip
 * Status:        Experimental.
 * Author:        Paul VanderSpek
 * Created at:    Wed Nov  4 11:46:16 1998
 * Modified at:   Fri Jan 28 12:10:59 2000
 * Modified by:   Dag Brattli <dagb@cs.uit.no>
11
 *
L
Linus Torvalds 已提交
12 13
 *     Copyright (c) 1998-2000 Dag Brattli <dagb@cs.uit.no>
 *     Copyright (c) 1998-1999 Rebel.com
14 15 16 17
 *
 *     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
L
Linus Torvalds 已提交
18
 *     the License, or (at your option) any later version.
19
 *
L
Linus Torvalds 已提交
20 21 22
 *     Neither Paul VanderSpek nor Rebel.com admit liability nor provide
 *     warranty for any of this software. This material is provided "AS-IS"
 *     and at no charge.
23
 *
L
Linus Torvalds 已提交
24 25 26 27 28
 *     If you find bugs in this file, its very likely that the same bug
 *     will also be in pc87108.c since the implementations are quite
 *     similar.
 *
 *     Notice that all functions that needs to access the chip in _any_
29
 *     way, must save BSR register on entry, and restore it on exit.
L
Linus Torvalds 已提交
30 31 32
 *     It is _very_ important to follow this policy!
 *
 *         __u8 bank;
33
 *
L
Linus Torvalds 已提交
34
 *         bank = inb( iobase+BSR);
35
 *
L
Linus Torvalds 已提交
36 37 38 39 40 41
 *         do_your_stuff_here();
 *
 *         outb( bank, iobase+BSR);
 *
 ********************************************************************/

J
Joe Perches 已提交
42 43
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

L
Linus Torvalds 已提交
44 45 46 47 48 49 50 51
#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/init.h>
52
#include <linux/interrupt.h>
L
Linus Torvalds 已提交
53 54
#include <linux/rtnetlink.h>
#include <linux/dma-mapping.h>
55
#include <linux/gfp.h>
L
Linus Torvalds 已提交
56 57 58 59 60 61 62 63 64 65 66 67

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

#include <net/irda/irda.h>
#include <net/irda/wrapper.h>
#include <net/irda/irda_device.h>
#include "w83977af.h"
#include "w83977af_ir.h"

#define CONFIG_USE_W977_PNP        /* Currently needed */
68
#define PIO_MAX_SPEED       115200
L
Linus Torvalds 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

static char *driver_name = "w83977af_ir";
static int  qos_mtt_bits = 0x07;   /* 1 ms or more */

#define CHIP_IO_EXTENT 8

static unsigned int io[] = { 0x180, ~0, ~0, ~0 };
#ifdef CONFIG_ARCH_NETWINDER             /* Adjust to NetWinder differences */
static unsigned int irq[] = { 6, 0, 0, 0 };
#else
static unsigned int irq[] = { 11, 0, 0, 0 };
#endif
static unsigned int dma[] = { 1, 0, 0, 0 };
static unsigned int efbase[] = { W977_EFIO_BASE, W977_EFIO2_BASE };
static unsigned int efio = W977_EFIO_BASE;

static struct w83977af_ir *dev_self[] = { NULL, NULL, NULL, NULL};

/* Some prototypes */
88
static int  w83977af_open(int i, unsigned int iobase, unsigned int irq,
89
			  unsigned int dma);
L
Linus Torvalds 已提交
90 91
static int  w83977af_close(struct w83977af_ir *self);
static int  w83977af_probe(int iobase, int irq, int dma);
92
static int  w83977af_dma_receive(struct w83977af_ir *self);
L
Linus Torvalds 已提交
93
static int  w83977af_dma_receive_complete(struct w83977af_ir *self);
S
Stephen Hemminger 已提交
94
static netdev_tx_t  w83977af_hard_xmit(struct sk_buff *skb,
95
				       struct net_device *dev);
L
Linus Torvalds 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
static int  w83977af_pio_write(int iobase, __u8 *buf, int len, int fifo_size);
static void w83977af_dma_write(struct w83977af_ir *self, int iobase);
static void w83977af_change_speed(struct w83977af_ir *self, __u32 speed);
static int  w83977af_is_receiving(struct w83977af_ir *self);

static int  w83977af_net_open(struct net_device *dev);
static int  w83977af_net_close(struct net_device *dev);
static int  w83977af_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);

/*
 * Function w83977af_init ()
 *
 *    Initialize chip. Just try to find out how many chips we are dealing with
 *    and where they are
 */
static int __init w83977af_init(void)
{
113
	int i;
L
Linus Torvalds 已提交
114

115
	for (i = 0; i < ARRAY_SIZE(dev_self) && io[i] < 2000; i++) {
L
Linus Torvalds 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
		if (w83977af_open(i, io[i], irq[i], dma[i]) == 0)
			return 0;
	}
	return -ENODEV;
}

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

132
	for (i = 0; i < ARRAY_SIZE(dev_self); i++) {
L
Linus Torvalds 已提交
133 134 135 136 137
		if (dev_self[i])
			w83977af_close(dev_self[i]);
	}
}

138 139 140 141 142 143 144
static const struct net_device_ops w83977_netdev_ops = {
	.ndo_open       = w83977af_net_open,
	.ndo_stop       = w83977af_net_close,
	.ndo_start_xmit = w83977af_hard_xmit,
	.ndo_do_ioctl   = w83977af_net_ioctl,
};

L
Linus Torvalds 已提交
145 146 147 148 149 150
/*
 * Function w83977af_open (iobase, irq)
 *
 *    Open driver instance
 *
 */
151 152
static int w83977af_open(int i, unsigned int iobase, unsigned int irq,
			 unsigned int dma)
L
Linus Torvalds 已提交
153 154
{
	struct net_device *dev;
155
	struct w83977af_ir *self;
L
Linus Torvalds 已提交
156 157 158 159
	int err;

	/* Lock the port that we need */
	if (!request_region(iobase, CHIP_IO_EXTENT, driver_name)) {
J
Joe Perches 已提交
160
		pr_debug("%s: can't get iobase of 0x%03x\n",
161
			 __func__, iobase);
L
Linus Torvalds 已提交
162 163 164 165 166 167 168 169 170 171 172
		return -ENODEV;
	}

	if (w83977af_probe(iobase, irq, dma) == -1) {
		err = -1;
		goto err_out;
	}
	/*
	 *  Allocate new instance of the driver
	 */
	dev = alloc_irdadev(sizeof(struct w83977af_ir));
173
	if (!dev) {
J
Joe Perches 已提交
174
		pr_err("IrDA: Can't allocate memory for IrDA control block!\n");
L
Linus Torvalds 已提交
175 176 177 178
		err = -ENOMEM;
		goto err_out;
	}

179
	self = netdev_priv(dev);
L
Linus Torvalds 已提交
180
	spin_lock_init(&self->lock);
181

L
Linus Torvalds 已提交
182
	/* Initialize IO */
183 184 185 186 187
	self->io.fir_base = iobase;
	self->io.irq = irq;
	self->io.fir_ext = CHIP_IO_EXTENT;
	self->io.dma = dma;
	self->io.fifo_size = 32;
L
Linus Torvalds 已提交
188 189 190

	/* Initialize QoS for this device */
	irda_init_max_qos_capabilies(&self->qos);
191

L
Linus Torvalds 已提交
192 193 194
	/* The only value we must override it the baudrate */

	/* FIXME: The HP HDLS-1100 does not support 1152000! */
195 196
	self->qos.baud_rate.bits = IR_9600 | IR_19200 | IR_38400 | IR_57600 |
		IR_115200 | IR_576000 | IR_1152000 | (IR_4000000 << 8);
L
Linus Torvalds 已提交
197 198 199 200

	/* The HP HDLS-1100 needs 1 ms according to the specs */
	self->qos.min_turn_time.bits = qos_mtt_bits;
	irda_qos_bits_to_value(&self->qos);
201

L
Linus Torvalds 已提交
202
	/* Max DMA buffer size needed = (data_size + 6) * (window_size) + 6; */
203
	self->rx_buff.truesize = 14384;
L
Linus Torvalds 已提交
204
	self->tx_buff.truesize = 4000;
205

L
Linus Torvalds 已提交
206 207
	/* Allocate memory if needed */
	self->rx_buff.head =
208 209
		dma_zalloc_coherent(NULL, self->rx_buff.truesize,
				    &self->rx_buff_dma, GFP_KERNEL);
210
	if (!self->rx_buff.head) {
L
Linus Torvalds 已提交
211 212 213 214 215
		err = -ENOMEM;
		goto err_out1;
	}

	self->tx_buff.head =
216 217
		dma_zalloc_coherent(NULL, self->tx_buff.truesize,
				    &self->tx_buff_dma, GFP_KERNEL);
218
	if (!self->tx_buff.head) {
L
Linus Torvalds 已提交
219 220 221 222 223 224 225 226 227 228
		err = -ENOMEM;
		goto err_out2;
	}

	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;
	self->netdev = dev;

229
	dev->netdev_ops	= &w83977_netdev_ops;
L
Linus Torvalds 已提交
230 231 232

	err = register_netdev(dev);
	if (err) {
J
Joe Perches 已提交
233
		net_err_ratelimited("%s:, register_netdevice() failed!\n",
234
				    __func__);
L
Linus Torvalds 已提交
235 236
		goto err_out3;
	}
237
	net_info_ratelimited("IrDA: Registered device %s\n", dev->name);
L
Linus Torvalds 已提交
238 239 240

	/* Need to store self somewhere */
	dev_self[i] = self;
241

L
Linus Torvalds 已提交
242 243 244 245
	return 0;
err_out3:
	dma_free_coherent(NULL, self->tx_buff.truesize,
			  self->tx_buff.head, self->tx_buff_dma);
246
err_out2:
L
Linus Torvalds 已提交
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
	dma_free_coherent(NULL, self->rx_buff.truesize,
			  self->rx_buff.head, self->rx_buff_dma);
err_out1:
	free_netdev(dev);
err_out:
	release_region(iobase, CHIP_IO_EXTENT);
	return err;
}

/*
 * Function w83977af_close (self)
 *
 *    Close driver instance
 *
 */
static int w83977af_close(struct w83977af_ir *self)
{
	int iobase;

266
	iobase = self->io.fir_base;
L
Linus Torvalds 已提交
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283

#ifdef CONFIG_USE_W977_PNP
	/* enter PnP configuration mode */
	w977_efm_enter(efio);

	w977_select_device(W977_DEVICE_IR, efio);

	/* Deactivate device */
	w977_write_reg(0x30, 0x00, efio);

	w977_efm_exit(efio);
#endif /* CONFIG_USE_W977_PNP */

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

	/* Release the PORT that this driver is using */
J
Joe Perches 已提交
284
	pr_debug("%s: Releasing Region %03x\n", __func__, self->io.fir_base);
L
Linus Torvalds 已提交
285 286 287 288 289
	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);
290

L
Linus Torvalds 已提交
291 292 293 294 295 296 297 298 299
	if (self->rx_buff.head)
		dma_free_coherent(NULL, self->rx_buff.truesize,
				  self->rx_buff.head, self->rx_buff_dma);

	free_netdev(self->netdev);

	return 0;
}

300
static int w83977af_probe(int iobase, int irq, int dma)
L
Linus Torvalds 已提交
301
{
302
	int version;
L
Linus Torvalds 已提交
303
	int i;
304

305
	for (i = 0; i < 2; i++) {
L
Linus Torvalds 已提交
306
#ifdef CONFIG_USE_W977_PNP
307
		/* Enter PnP configuration mode */
L
Linus Torvalds 已提交
308
		w977_efm_enter(efbase[i]);
309 310 311 312 313 314 315 316

		w977_select_device(W977_DEVICE_IR, efbase[i]);

		/* Configure PnP port, IRQ, and DMA channel */
		w977_write_reg(0x60, (iobase >> 8) & 0xff, efbase[i]);
		w977_write_reg(0x61, (iobase) & 0xff, efbase[i]);

		w977_write_reg(0x70, irq, efbase[i]);
L
Linus Torvalds 已提交
317 318
#ifdef CONFIG_ARCH_NETWINDER
		/* Netwinder uses 1 higher than Linux */
319
		w977_write_reg(0x74, dma + 1, efbase[i]);
L
Linus Torvalds 已提交
320
#else
321
		w977_write_reg(0x74, dma, efbase[i]);
322
#endif /* CONFIG_ARCH_NETWINDER */
323 324 325 326 327 328 329 330 331
		w977_write_reg(0x75, 0x04, efbase[i]);/* Disable Tx DMA */

		/* Set append hardware CRC, enable IR bank selection */
		w977_write_reg(0xf0, APEDCRC | ENBNKSEL, efbase[i]);

		/* Activate device */
		w977_write_reg(0x30, 0x01, efbase[i]);

		w977_efm_exit(efbase[i]);
L
Linus Torvalds 已提交
332
#endif /* CONFIG_USE_W977_PNP */
333 334
		/* Disable Advanced mode */
		switch_bank(iobase, SET2);
335
		outb(iobase + 2, 0x00);
336 337 338

		/* Turn on UART (global) interrupts */
		switch_bank(iobase, SET0);
339
		outb(HCR_EN_IRQ, iobase + HCR);
340 341 342

		/* Switch to advanced mode */
		switch_bank(iobase, SET2);
343
		outb(inb(iobase + ADCR1) | ADCR1_ADV_SL, iobase + ADCR1);
344 345 346

		/* Set default IR-mode */
		switch_bank(iobase, SET0);
347
		outb(HCR_SIR, iobase + HCR);
348 349 350

		/* Read the Advanced IR ID */
		switch_bank(iobase, SET3);
351
		version = inb(iobase + AUID);
352 353 354 355 356 357 358

		/* Should be 0x1? */
		if (0x10 == (version & 0xf0)) {
			efio = efbase[i];

			/* Set FIFO size to 32 */
			switch_bank(iobase, SET2);
359
			outb(ADCR2_RXFS32 | ADCR2_TXFS32, iobase + ADCR2);
360 361 362

			/* Set FIFO threshold to TX17, RX16 */
			switch_bank(iobase, SET0);
363 364
			outb(UFR_RXTL | UFR_TXTL | UFR_TXF_RST | UFR_RXF_RST |
			     UFR_EN_FIFO, iobase + UFR);
365 366 367

			/* Receiver frame length */
			switch_bank(iobase, SET4);
368 369
			outb(2048 & 0xff, iobase + 6);
			outb((2048 >> 8) & 0x1f, iobase + 7);
L
Linus Torvalds 已提交
370

371 372
			/*
			 * Init HP HSDL-1100 transceiver.
L
Linus Torvalds 已提交
373
			 *
374 375 376 377 378
			 * Set IRX_MSL since we have 2 * receive paths IRRX,
			 * and IRRXH. Clear IRSL0D since we want IRSL0 * to
			 * be a input pin used for IRRXH
			 *
			 *   IRRX  pin 37 connected to receiver
L
Linus Torvalds 已提交
379
			 *   IRTX  pin 38 connected to transmitter
380
			 *   FIRRX pin 39 connected to receiver      (IRSL0)
L
Linus Torvalds 已提交
381 382 383
			 *   CIRRX pin 40 connected to pin 37
			 */
			switch_bank(iobase, SET7);
384
			outb(0x40, iobase + 7);
385

386 387
			net_info_ratelimited("W83977AF (IR) driver loaded. Version: 0x%02x\n",
					     version);
388

L
Linus Torvalds 已提交
389 390 391
			return 0;
		} else {
			/* Try next extented function register address */
J
Joe Perches 已提交
392
			pr_debug("%s: Wrong chip version\n", __func__);
L
Linus Torvalds 已提交
393
		}
394
	}
L
Linus Torvalds 已提交
395 396 397
	return -1;
}

398
static void w83977af_change_speed(struct w83977af_ir *self, __u32 speed)
L
Linus Torvalds 已提交
399 400
{
	int ir_mode = HCR_SIR;
401
	int iobase;
L
Linus Torvalds 已提交
402 403 404 405 406 407 408 409
	__u8 set;

	iobase = self->io.fir_base;

	/* Update accounting for new speed */
	self->io.speed = speed;

	/* Save current bank */
410
	set = inb(iobase + SSR);
L
Linus Torvalds 已提交
411 412 413

	/* Disable interrupts */
	switch_bank(iobase, SET0);
414
	outb(0, iobase + ICR);
L
Linus Torvalds 已提交
415 416 417

	/* Select Set 2 */
	switch_bank(iobase, SET2);
418
	outb(0x00, iobase + ABHL);
L
Linus Torvalds 已提交
419 420

	switch (speed) {
421 422 423 424 425
	case 9600:   outb(0x0c, iobase + ABLL); break;
	case 19200:  outb(0x06, iobase + ABLL); break;
	case 38400:  outb(0x03, iobase + ABLL); break;
	case 57600:  outb(0x02, iobase + ABLL); break;
	case 115200: outb(0x01, iobase + ABLL); break;
L
Linus Torvalds 已提交
426 427
	case 576000:
		ir_mode = HCR_MIR_576;
J
Joe Perches 已提交
428
		pr_debug("%s: handling baud of 576000\n", __func__);
L
Linus Torvalds 已提交
429 430 431
		break;
	case 1152000:
		ir_mode = HCR_MIR_1152;
J
Joe Perches 已提交
432
		pr_debug("%s: handling baud of 1152000\n", __func__);
L
Linus Torvalds 已提交
433 434 435
		break;
	case 4000000:
		ir_mode = HCR_FIR;
J
Joe Perches 已提交
436
		pr_debug("%s: handling baud of 4000000\n", __func__);
L
Linus Torvalds 已提交
437 438 439
		break;
	default:
		ir_mode = HCR_FIR;
J
Joe Perches 已提交
440
		pr_debug("%s: unknown baud rate of %d\n", __func__, speed);
L
Linus Torvalds 已提交
441 442 443 444 445
		break;
	}

	/* Set speed mode */
	switch_bank(iobase, SET0);
446
	outb(ir_mode, iobase + HCR);
L
Linus Torvalds 已提交
447 448 449

	/* set FIFO size to 32 */
	switch_bank(iobase, SET2);
450
	outb(ADCR2_RXFS32 | ADCR2_TXFS32, iobase + ADCR2);
451

L
Linus Torvalds 已提交
452 453
	/* set FIFO threshold to TX17, RX16 */
	switch_bank(iobase, SET0);
454 455 456
	outb(0x00, iobase + UFR);        /* Reset */
	outb(UFR_EN_FIFO, iobase + UFR); /* First we must enable FIFO */
	outb(0xa7, iobase + UFR);
L
Linus Torvalds 已提交
457 458

	netif_wake_queue(self->netdev);
459

L
Linus Torvalds 已提交
460 461 462
	/* Enable some interrupts so we can receive frames */
	switch_bank(iobase, SET0);
	if (speed > PIO_MAX_SPEED) {
463
		outb(ICR_EFSFI, iobase + ICR);
L
Linus Torvalds 已提交
464
		w83977af_dma_receive(self);
465
	} else {
466
		outb(ICR_ERBRI, iobase + ICR);
467
	}
468

L
Linus Torvalds 已提交
469
	/* Restore SSR */
470
	outb(set, iobase + SSR);
L
Linus Torvalds 已提交
471 472 473 474 475 476 477 478
}

/*
 * Function w83977af_hard_xmit (skb, dev)
 *
 *    Sets up a DMA transfer to send the current frame.
 *
 */
S
Stephen Hemminger 已提交
479
static netdev_tx_t w83977af_hard_xmit(struct sk_buff *skb,
480
				      struct net_device *dev)
L
Linus Torvalds 已提交
481 482 483 484 485 486
{
	struct w83977af_ir *self;
	__s32 speed;
	int iobase;
	__u8 set;
	int mtt;
487

488
	self = netdev_priv(dev);
L
Linus Torvalds 已提交
489 490 491

	iobase = self->io.fir_base;

J
Joe Perches 已提交
492
	pr_debug("%s: %ld, skb->len=%d\n", __func__, jiffies, (int)skb->len);
493

L
Linus Torvalds 已提交
494 495
	/* Lock transmit buffer */
	netif_stop_queue(dev);
496

L
Linus Torvalds 已提交
497 498 499 500 501
	/* 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) {
502
			w83977af_change_speed(self, speed);
L
Linus Torvalds 已提交
503
			dev_kfree_skb(skb);
504
			return NETDEV_TX_OK;
505 506
		}
		self->new_speed = speed;
L
Linus Torvalds 已提交
507 508 509
	}

	/* Save current set */
510
	set = inb(iobase + SSR);
511

L
Linus Torvalds 已提交
512 513 514
	/* Decide if we should use PIO or DMA transfer */
	if (self->io.speed > PIO_MAX_SPEED) {
		self->tx_buff.data = self->tx_buff.head;
515
		skb_copy_from_linear_data(skb, self->tx_buff.data, skb->len);
L
Linus Torvalds 已提交
516
		self->tx_buff.len = skb->len;
517

L
Linus Torvalds 已提交
518
		mtt = irda_get_mtt(skb);
J
Joe Perches 已提交
519 520 521 522
		pr_debug("%s: %ld, mtt=%d\n", __func__, jiffies, mtt);
			if (mtt > 1000)
				mdelay(mtt / 1000);
			else if (mtt)
523
			udelay(mtt);
L
Linus Torvalds 已提交
524

525 526
		/* Enable DMA interrupt */
		switch_bank(iobase, SET0);
527
		outb(ICR_EDMAI, iobase + ICR);
528
		w83977af_dma_write(self, iobase);
L
Linus Torvalds 已提交
529 530
	} else {
		self->tx_buff.data = self->tx_buff.head;
531
		self->tx_buff.len = async_wrap_skb(skb, self->tx_buff.data,
L
Linus Torvalds 已提交
532
						   self->tx_buff.truesize);
533

L
Linus Torvalds 已提交
534 535
		/* Add interrupt on tx low level (will fire immediately) */
		switch_bank(iobase, SET0);
536
		outb(ICR_ETXTHI, iobase + ICR);
L
Linus Torvalds 已提交
537 538 539 540
	}
	dev_kfree_skb(skb);

	/* Restore set register */
541
	outb(set, iobase + SSR);
L
Linus Torvalds 已提交
542

543
	return NETDEV_TX_OK;
L
Linus Torvalds 已提交
544 545 546 547 548 549 550 551 552 553 554
}

/*
 * Function w83977af_dma_write (self, iobase)
 *
 *    Send frame using DMA
 *
 */
static void w83977af_dma_write(struct w83977af_ir *self, int iobase)
{
	__u8 set;
555

J
Joe Perches 已提交
556
	pr_debug("%s: len=%d\n", __func__, self->tx_buff.len);
L
Linus Torvalds 已提交
557 558

	/* Save current set */
559
	set = inb(iobase + SSR);
L
Linus Torvalds 已提交
560 561 562

	/* Disable DMA */
	switch_bank(iobase, SET0);
563
	outb(inb(iobase + HCR) & ~HCR_EN_DMA, iobase + HCR);
L
Linus Torvalds 已提交
564

565
	/* Choose transmit DMA channel  */
L
Linus Torvalds 已提交
566
	switch_bank(iobase, SET2);
567
	outb(ADCR1_D_CHSW | /*ADCR1_DMA_F|*/ADCR1_ADV_SL, iobase + ADCR1);
L
Linus Torvalds 已提交
568
	irda_setup_dma(self->io.dma, self->tx_buff_dma, self->tx_buff.len,
569
		       DMA_MODE_WRITE);
L
Linus Torvalds 已提交
570
	self->io.direction = IO_XMIT;
571

L
Linus Torvalds 已提交
572
	/* Enable DMA */
573
	switch_bank(iobase, SET0);
574
	outb(inb(iobase + HCR) | HCR_EN_DMA | HCR_TX_WT, iobase + HCR);
L
Linus Torvalds 已提交
575 576

	/* Restore set register */
577
	outb(set, iobase + SSR);
L
Linus Torvalds 已提交
578 579 580 581 582
}

/*
 * Function w83977af_pio_write (iobase, buf, len, fifo_size)
 *
583
 *
L
Linus Torvalds 已提交
584 585 586 587 588 589
 *
 */
static int w83977af_pio_write(int iobase, __u8 *buf, int len, int fifo_size)
{
	int actual = 0;
	__u8 set;
590

L
Linus Torvalds 已提交
591
	/* Save current bank */
592
	set = inb(iobase + SSR);
L
Linus Torvalds 已提交
593 594

	switch_bank(iobase, SET0);
595
	if (!(inb_p(iobase + USR) & USR_TSRE)) {
J
Joe Perches 已提交
596
		pr_debug("%s: warning, FIFO not empty yet!\n", __func__);
L
Linus Torvalds 已提交
597 598

		fifo_size -= 17;
J
Joe Perches 已提交
599
		pr_debug("%s: %d bytes left in tx fifo\n", __func__, fifo_size);
L
Linus Torvalds 已提交
600 601 602 603 604
	}

	/* Fill FIFO with current frame */
	while ((fifo_size-- > 0) && (actual < len)) {
		/* Transmit next byte */
605
		outb(buf[actual++], iobase + TBR);
L
Linus Torvalds 已提交
606
	}
607

J
Joe Perches 已提交
608
	pr_debug("%s: fifo_size %d ; %d sent of %d\n",
609
		 __func__, fifo_size, actual, len);
L
Linus Torvalds 已提交
610 611

	/* Restore bank */
612
	outb(set, iobase + SSR);
L
Linus Torvalds 已提交
613 614 615 616 617 618 619 620 621

	return actual;
}

/*
 * Function w83977af_dma_xmit_complete (self)
 *
 *    The transfer of a frame in finished. So do the necessary things
 *
622
 *
L
Linus Torvalds 已提交
623 624 625 626 627 628
 */
static void w83977af_dma_xmit_complete(struct w83977af_ir *self)
{
	int iobase;
	__u8 set;

J
Joe Perches 已提交
629
	pr_debug("%s: %ld\n", __func__, jiffies);
L
Linus Torvalds 已提交
630

631
	IRDA_ASSERT(self, return;);
L
Linus Torvalds 已提交
632 633 634 635

	iobase = self->io.fir_base;

	/* Save current set */
636
	set = inb(iobase + SSR);
L
Linus Torvalds 已提交
637 638 639

	/* Disable DMA */
	switch_bank(iobase, SET0);
640
	outb(inb(iobase + HCR) & ~HCR_EN_DMA, iobase + HCR);
641

642
	/* Check for underrun! */
643
	if (inb(iobase + AUDR) & AUDR_UNDR) {
J
Joe Perches 已提交
644
		pr_debug("%s: Transmit underrun!\n", __func__);
645

646 647
		self->netdev->stats.tx_errors++;
		self->netdev->stats.tx_fifo_errors++;
L
Linus Torvalds 已提交
648 649

		/* Clear bit, by writing 1 to it */
650
		outb(AUDR_UNDR, iobase + AUDR);
651
	} else {
652
		self->netdev->stats.tx_packets++;
653
	}
L
Linus Torvalds 已提交
654 655 656 657 658 659 660 661 662

	if (self->new_speed) {
		w83977af_change_speed(self, self->new_speed);
		self->new_speed = 0;
	}

	/* Unlock tx_buff and request another frame */
	/* Tell the network layer, that we want more frames */
	netif_wake_queue(self->netdev);
663

L
Linus Torvalds 已提交
664
	/* Restore set */
665
	outb(set, iobase + SSR);
L
Linus Torvalds 已提交
666 667 668 669 670 671 672 673 674
}

/*
 * Function w83977af_dma_receive (self)
 *
 *    Get ready for receiving a frame. The device will initiate a DMA
 *    if it starts to receive a frame.
 *
 */
675
static int w83977af_dma_receive(struct w83977af_ir *self)
L
Linus Torvalds 已提交
676 677 678
{
	int iobase;
	__u8 set;
679
#ifdef CONFIG_ARCH_NETWINDER
L
Linus Torvalds 已提交
680 681 682
	unsigned long flags;
	__u8 hcr;
#endif
683
	IRDA_ASSERT(self, return -1;);
L
Linus Torvalds 已提交
684

J
Joe Perches 已提交
685
	pr_debug("%s\n", __func__);
L
Linus Torvalds 已提交
686

687
	iobase = self->io.fir_base;
L
Linus Torvalds 已提交
688 689

	/* Save current set */
690
	set = inb(iobase + SSR);
L
Linus Torvalds 已提交
691 692 693

	/* Disable DMA */
	switch_bank(iobase, SET0);
694
	outb(inb(iobase + HCR) & ~HCR_EN_DMA, iobase + HCR);
L
Linus Torvalds 已提交
695 696 697

	/* Choose DMA Rx, DMA Fairness, and Advanced mode */
	switch_bank(iobase, SET2);
698 699
	outb((inb(iobase + ADCR1) & ~ADCR1_D_CHSW)/*|ADCR1_DMA_F*/ | ADCR1_ADV_SL,
	     iobase + ADCR1);
L
Linus Torvalds 已提交
700 701 702 703

	self->io.direction = IO_RECV;
	self->rx_buff.data = self->rx_buff.head;

704
#ifdef CONFIG_ARCH_NETWINDER
L
Linus Torvalds 已提交
705 706 707 708 709 710 711 712 713 714 715
	spin_lock_irqsave(&self->lock, flags);

	disable_dma(self->io.dma);
	clear_dma_ff(self->io.dma);
	set_dma_mode(self->io.dma, DMA_MODE_READ);
	set_dma_addr(self->io.dma, self->rx_buff_dma);
	set_dma_count(self->io.dma, self->rx_buff.truesize);
#else
	irda_setup_dma(self->io.dma, self->rx_buff_dma, self->rx_buff.truesize,
		       DMA_MODE_READ);
#endif
716 717
	/*
	 * Reset Rx FIFO. This will also flush the ST_FIFO, it's very
L
Linus Torvalds 已提交
718 719 720 721
	 * important that we don't reset the Tx FIFO since it might not
	 * be finished transmitting yet
	 */
	switch_bank(iobase, SET0);
722
	outb(UFR_RXTL | UFR_TXTL | UFR_RXF_RST | UFR_EN_FIFO, iobase + UFR);
L
Linus Torvalds 已提交
723
	self->st_fifo.len = self->st_fifo.tail = self->st_fifo.head = 0;
724

L
Linus Torvalds 已提交
725 726
	/* Enable DMA */
	switch_bank(iobase, SET0);
727
#ifdef CONFIG_ARCH_NETWINDER
728 729
	hcr = inb(iobase + HCR);
	outb(hcr | HCR_EN_DMA, iobase + HCR);
L
Linus Torvalds 已提交
730 731
	enable_dma(self->io.dma);
	spin_unlock_irqrestore(&self->lock, flags);
732
#else
733
	outb(inb(iobase + HCR) | HCR_EN_DMA, iobase + HCR);
L
Linus Torvalds 已提交
734 735
#endif
	/* Restore set */
736
	outb(set, iobase + SSR);
L
Linus Torvalds 已提交
737 738 739 740 741 742 743 744 745 746

	return 0;
}

/*
 * Function w83977af_receive_complete (self)
 *
 *    Finished with receiving a frame
 *
 */
747
static int w83977af_dma_receive_complete(struct w83977af_ir *self)
L
Linus Torvalds 已提交
748 749 750 751 752 753 754 755
{
	struct sk_buff *skb;
	struct st_fifo *st_fifo;
	int len;
	int iobase;
	__u8 set;
	__u8 status;

J
Joe Perches 已提交
756
	pr_debug("%s\n", __func__);
L
Linus Torvalds 已提交
757 758 759 760 761 762

	st_fifo = &self->st_fifo;

	iobase = self->io.fir_base;

	/* Save current set */
763
	set = inb(iobase + SSR);
764

L
Linus Torvalds 已提交
765 766 767 768
	iobase = self->io.fir_base;

	/* Read status FIFO */
	switch_bank(iobase, SET5);
769
	while ((status = inb(iobase + FS_FO)) & FS_FO_FSFDR) {
L
Linus Torvalds 已提交
770
		st_fifo->entries[st_fifo->tail].status = status;
771

772 773
		st_fifo->entries[st_fifo->tail].len  = inb(iobase + RFLFL);
		st_fifo->entries[st_fifo->tail].len |= inb(iobase + RFLFH) << 8;
774

L
Linus Torvalds 已提交
775 776 777
		st_fifo->tail++;
		st_fifo->len++;
	}
778

L
Linus Torvalds 已提交
779 780 781 782 783 784 785 786 787 788 789
	while (st_fifo->len) {
		/* Get first entry */
		status = st_fifo->entries[st_fifo->head].status;
		len    = st_fifo->entries[st_fifo->head].len;
		st_fifo->head++;
		st_fifo->len--;

		/* Check for errors */
		if (status & FS_FO_ERR_MSK) {
			if (status & FS_FO_LST_FR) {
				/* Add number of lost frames to stats */
790
				self->netdev->stats.rx_errors += len;
L
Linus Torvalds 已提交
791 792
			} else {
				/* Skip frame */
793
				self->netdev->stats.rx_errors++;
794

L
Linus Torvalds 已提交
795
				self->rx_buff.data += len;
796

L
Linus Torvalds 已提交
797
				if (status & FS_FO_MX_LEX)
798
					self->netdev->stats.rx_length_errors++;
799 800

				if (status & FS_FO_PHY_ERR)
801
					self->netdev->stats.rx_frame_errors++;
802 803

				if (status & FS_FO_CRC_ERR)
804
					self->netdev->stats.rx_crc_errors++;
L
Linus Torvalds 已提交
805 806 807
			}
			/* The errors below can be reported in both cases */
			if (status & FS_FO_RX_OV)
808
				self->netdev->stats.rx_fifo_errors++;
809

L
Linus Torvalds 已提交
810
			if (status & FS_FO_FSF_OV)
811
				self->netdev->stats.rx_fifo_errors++;
812

L
Linus Torvalds 已提交
813 814 815
		} else {
			/* Check if we have transferred all data to memory */
			switch_bank(iobase, SET0);
816
			if (inb(iobase + USR) & USR_RDR)
L
Linus Torvalds 已提交
817
				udelay(80); /* Should be enough!? */
818

819
			skb = dev_alloc_skb(len + 1);
820
			if (!skb)  {
J
Joe Perches 已提交
821 822
				pr_info("%s: memory squeeze, dropping frame\n",
					__func__);
L
Linus Torvalds 已提交
823
				/* Restore set register */
824
				outb(set, iobase + SSR);
L
Linus Torvalds 已提交
825 826 827

				return FALSE;
			}
828

L
Linus Torvalds 已提交
829
			/*  Align to 20 bytes */
830 831
			skb_reserve(skb, 1);

L
Linus Torvalds 已提交
832 833
			/* Copy frame without CRC */
			if (self->io.speed < 4000000) {
834
				skb_put(skb, len - 2);
835 836 837
				skb_copy_to_linear_data(skb,
							self->rx_buff.data,
							len - 2);
L
Linus Torvalds 已提交
838
			} else {
839
				skb_put(skb, len - 4);
840 841 842
				skb_copy_to_linear_data(skb,
							self->rx_buff.data,
							len - 4);
L
Linus Torvalds 已提交
843 844 845 846
			}

			/* Move to next frame */
			self->rx_buff.data += len;
847
			self->netdev->stats.rx_packets++;
848

L
Linus Torvalds 已提交
849
			skb->dev = self->netdev;
850
			skb_reset_mac_header(skb);
L
Linus Torvalds 已提交
851 852 853 854 855
			skb->protocol = htons(ETH_P_IRDA);
			netif_rx(skb);
		}
	}
	/* Restore set register */
856
	outb(set, iobase + SSR);
L
Linus Torvalds 已提交
857 858 859 860 861 862 863 864 865 866

	return TRUE;
}

/*
 * Function pc87108_pio_receive (self)
 *
 *    Receive all data in receiver FIFO
 *
 */
867
static void w83977af_pio_receive(struct w83977af_ir *self)
L
Linus Torvalds 已提交
868 869 870 871
{
	__u8 byte = 0x00;
	int iobase;

872
	IRDA_ASSERT(self, return;);
873

L
Linus Torvalds 已提交
874
	iobase = self->io.fir_base;
875

L
Linus Torvalds 已提交
876 877
	/*  Receive all characters in Rx FIFO */
	do {
878
		byte = inb(iobase + RBR);
879
		async_unwrap_char(self->netdev, &self->netdev->stats, &self->rx_buff,
L
Linus Torvalds 已提交
880
				  byte);
881
	} while (inb(iobase + USR) & USR_RDR); /* Data available */
L
Linus Torvalds 已提交
882 883 884 885 886 887 888 889 890 891 892 893 894 895 896
}

/*
 * Function w83977af_sir_interrupt (self, eir)
 *
 *    Handle SIR interrupt
 *
 */
static __u8 w83977af_sir_interrupt(struct w83977af_ir *self, int isr)
{
	int actual;
	__u8 new_icr = 0;
	__u8 set;
	int iobase;

J
Joe Perches 已提交
897
	pr_debug("%s: isr=%#x\n", __func__, isr);
898

L
Linus Torvalds 已提交
899 900 901 902
	iobase = self->io.fir_base;
	/* Transmit FIFO low on data */
	if (isr & ISR_TXTH_I) {
		/* Write data left in transmit buffer */
903 904 905
		actual = w83977af_pio_write(self->io.fir_base,
					    self->tx_buff.data,
					    self->tx_buff.len,
L
Linus Torvalds 已提交
906 907 908 909
					    self->io.fifo_size);

		self->tx_buff.data += actual;
		self->tx_buff.len  -= actual;
910

L
Linus Torvalds 已提交
911 912 913 914 915 916
		self->io.direction = IO_XMIT;

		/* Check if finished */
		if (self->tx_buff.len > 0) {
			new_icr |= ICR_ETXTHI;
		} else {
917
			set = inb(iobase + SSR);
L
Linus Torvalds 已提交
918
			switch_bank(iobase, SET0);
919 920
			outb(AUDR_SFEND, iobase + AUDR);
			outb(set, iobase + SSR);
L
Linus Torvalds 已提交
921

922
			self->netdev->stats.tx_packets++;
L
Linus Torvalds 已提交
923 924 925 926 927 928 929

			/* Feed me more packets */
			netif_wake_queue(self->netdev);
			new_icr |= ICR_ETBREI;
		}
	}
	/* Check if transmission has completed */
930
	if (isr & ISR_TXEMP_I) {
L
Linus Torvalds 已提交
931 932
		/* Check if we need to change the speed? */
		if (self->new_speed) {
J
Joe Perches 已提交
933
			pr_debug("%s: Changing speed!\n", __func__);
L
Linus Torvalds 已提交
934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965
			w83977af_change_speed(self, self->new_speed);
			self->new_speed = 0;
		}

		/* Turn around and get ready to receive some data */
		self->io.direction = IO_RECV;
		new_icr |= ICR_ERBRI;
	}

	/* Rx FIFO threshold or timeout */
	if (isr & ISR_RXTH_I) {
		w83977af_pio_receive(self);

		/* Keep receiving */
		new_icr |= ICR_ERBRI;
	}
	return new_icr;
}

/*
 * Function pc87108_fir_interrupt (self, eir)
 *
 *    Handle MIR/FIR interrupt
 *
 */
static __u8 w83977af_fir_interrupt(struct w83977af_ir *self, int isr)
{
	__u8 new_icr = 0;
	__u8 set;
	int iobase;

	iobase = self->io.fir_base;
966
	set = inb(iobase + SSR);
967

L
Linus Torvalds 已提交
968
	/* End of frame detected in FIFO */
969
	if (isr & (ISR_FEND_I | ISR_FSF_I)) {
L
Linus Torvalds 已提交
970 971 972 973 974 975 976 977
		if (w83977af_dma_receive_complete(self)) {
			/* Wait for next status FIFO interrupt */
			new_icr |= ICR_EFSFI;
		} else {
			/* DMA not finished yet */

			/* Set timer value, resolution 1 ms */
			switch_bank(iobase, SET4);
978 979
			outb(0x01, iobase + TMRL); /* 1 ms */
			outb(0x00, iobase + TMRH);
L
Linus Torvalds 已提交
980 981

			/* Start timer */
982
			outb(IR_MSL_EN_TMR, iobase + IR_MSL);
L
Linus Torvalds 已提交
983 984 985 986 987 988 989 990

			new_icr |= ICR_ETMRI;
		}
	}
	/* Timer finished */
	if (isr & ISR_TMR_I) {
		/* Disable timer */
		switch_bank(iobase, SET4);
991
		outb(0, iobase + IR_MSL);
L
Linus Torvalds 已提交
992 993 994

		/* Clear timer event */
		/* switch_bank(iobase, SET0); */
995
/*		outb(ASCR_CTE, iobase+ASCR); */
L
Linus Torvalds 已提交
996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007

		/* Check if this is a TX timer interrupt */
		if (self->io.direction == IO_XMIT) {
			w83977af_dma_write(self, iobase);

			new_icr |= ICR_EDMAI;
		} else {
			/* Check if DMA has now finished */
			w83977af_dma_receive_complete(self);

			new_icr |= ICR_EFSFI;
		}
1008
	}
L
Linus Torvalds 已提交
1009 1010 1011 1012 1013 1014
	/* Finished with DMA */
	if (isr & ISR_DMA_I) {
		w83977af_dma_xmit_complete(self);

		/* Check if there are more frames to be transmitted */
		/* if (irda_device_txqueue_empty(self)) { */
1015 1016 1017

		/* Prepare for receive
		 *
L
Linus Torvalds 已提交
1018 1019 1020 1021
		 * ** Netwinder Tx DMA likes that we do this anyway **
		 */
		w83977af_dma_receive(self);
		new_icr = ICR_EFSFI;
1022
		/* } */
L
Linus Torvalds 已提交
1023
	}
1024

L
Linus Torvalds 已提交
1025
	/* Restore set */
1026
	outb(set, iobase + SSR);
L
Linus Torvalds 已提交
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036

	return new_icr;
}

/*
 * Function w83977af_interrupt (irq, dev_id, regs)
 *
 *    An interrupt from the chip has arrived. Time to do some work
 *
 */
1037
static irqreturn_t w83977af_interrupt(int irq, void *dev_id)
L
Linus Torvalds 已提交
1038
{
1039
	struct net_device *dev = dev_id;
L
Linus Torvalds 已提交
1040 1041 1042 1043
	struct w83977af_ir *self;
	__u8 set, icr, isr;
	int iobase;

1044
	self = netdev_priv(dev);
L
Linus Torvalds 已提交
1045 1046 1047 1048

	iobase = self->io.fir_base;

	/* Save current bank */
1049
	set = inb(iobase + SSR);
L
Linus Torvalds 已提交
1050
	switch_bank(iobase, SET0);
1051

1052 1053
	icr = inb(iobase + ICR);
	isr = inb(iobase + ISR) & icr; /* Mask out the interesting ones */
L
Linus Torvalds 已提交
1054

1055
	outb(0, iobase + ICR); /* Disable interrupts */
1056

L
Linus Torvalds 已提交
1057 1058
	if (isr) {
		/* Dispatch interrupt handler for the current speed */
1059
		if (self->io.speed > PIO_MAX_SPEED)
L
Linus Torvalds 已提交
1060 1061 1062 1063 1064
			icr = w83977af_fir_interrupt(self, isr);
		else
			icr = w83977af_sir_interrupt(self, isr);
	}

1065 1066
	outb(icr, iobase + ICR);    /* Restore (new) interrupts */
	outb(set, iobase + SSR);    /* Restore bank register */
L
Linus Torvalds 已提交
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
	return IRQ_RETVAL(isr);
}

/*
 * Function w83977af_is_receiving (self)
 *
 *    Return TRUE is we are currently receiving a frame
 *
 */
static int w83977af_is_receiving(struct w83977af_ir *self)
{
	int status = FALSE;
	int iobase;
	__u8 set;

1082
	IRDA_ASSERT(self, return FALSE;);
L
Linus Torvalds 已提交
1083 1084 1085 1086 1087

	if (self->io.speed > 115200) {
		iobase = self->io.fir_base;

		/* Check if rx FIFO is not empty */
1088
		set = inb(iobase + SSR);
L
Linus Torvalds 已提交
1089
		switch_bank(iobase, SET2);
1090
		if ((inb(iobase + RXFDTH) & 0x3f) != 0) {
L
Linus Torvalds 已提交
1091 1092 1093
			/* We are receiving something */
			status =  TRUE;
		}
1094
		outb(set, iobase + SSR);
1095
	} else {
L
Linus Torvalds 已提交
1096
		status = (self->rx_buff.state != OUTSIDE_FRAME);
1097
	}
1098

L
Linus Torvalds 已提交
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
	return status;
}

/*
 * Function w83977af_net_open (dev)
 *
 *    Start the device
 *
 */
static int w83977af_net_open(struct net_device *dev)
{
	struct w83977af_ir *self;
	int iobase;
	char hwname[32];
	__u8 set;
1114

1115
	IRDA_ASSERT(dev, return -1;);
1116
	self = netdev_priv(dev);
1117

1118
	IRDA_ASSERT(self, return 0;);
1119

L
Linus Torvalds 已提交
1120 1121
	iobase = self->io.fir_base;

1122
	if (request_irq(self->io.irq, w83977af_interrupt, 0, dev->name,
1123
			(void *)dev)) {
L
Linus Torvalds 已提交
1124 1125 1126 1127 1128 1129 1130
		return -EAGAIN;
	}
	/*
	 * Always allocate the DMA channel after the IRQ,
	 * and clean up on failure.
	 */
	if (request_dma(self->io.dma, dev->name)) {
1131
		free_irq(self->io.irq, dev);
L
Linus Torvalds 已提交
1132 1133
		return -EAGAIN;
	}
1134

L
Linus Torvalds 已提交
1135
	/* Save current set */
1136
	set = inb(iobase + SSR);
L
Linus Torvalds 已提交
1137

1138 1139 1140
	/* Enable some interrupts so we can receive frames again */
	switch_bank(iobase, SET0);
	if (self->io.speed > 115200) {
1141
		outb(ICR_EFSFI, iobase + ICR);
1142
		w83977af_dma_receive(self);
1143
	} else {
1144
		outb(ICR_ERBRI, iobase + ICR);
1145
	}
L
Linus Torvalds 已提交
1146 1147

	/* Restore bank register */
1148
	outb(set, iobase + SSR);
L
Linus Torvalds 已提交
1149 1150 1151

	/* Ready to play! */
	netif_start_queue(dev);
1152

L
Linus Torvalds 已提交
1153 1154 1155
	/* Give self a hardware name */
	sprintf(hwname, "w83977af @ 0x%03x", self->io.fir_base);

1156
	/*
L
Linus Torvalds 已提交
1157
	 * Open new IrLAP layer instance, now that everything should be
1158
	 * initialized properly
L
Linus Torvalds 已提交
1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176
	 */
	self->irlap = irlap_open(dev, &self->qos, hwname);

	return 0;
}

/*
 * Function w83977af_net_close (dev)
 *
 *    Stop the device
 *
 */
static int w83977af_net_close(struct net_device *dev)
{
	struct w83977af_ir *self;
	int iobase;
	__u8 set;

1177
	IRDA_ASSERT(dev, return -1;);
1178

1179
	self = netdev_priv(dev);
1180

1181
	IRDA_ASSERT(self, return 0;);
1182

L
Linus Torvalds 已提交
1183 1184 1185 1186
	iobase = self->io.fir_base;

	/* Stop device */
	netif_stop_queue(dev);
1187

L
Linus Torvalds 已提交
1188 1189 1190 1191 1192 1193 1194 1195
	/* Stop and remove instance of IrLAP */
	if (self->irlap)
		irlap_close(self->irlap);
	self->irlap = NULL;

	disable_dma(self->io.dma);

	/* Save current set */
1196
	set = inb(iobase + SSR);
1197

L
Linus Torvalds 已提交
1198 1199
	/* Disable interrupts */
	switch_bank(iobase, SET0);
1200
	outb(0, iobase + ICR);
L
Linus Torvalds 已提交
1201 1202 1203 1204 1205

	free_irq(self->io.irq, dev);
	free_dma(self->io.dma);

	/* Restore bank register */
1206
	outb(set, iobase + SSR);
L
Linus Torvalds 已提交
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218

	return 0;
}

/*
 * Function w83977af_net_ioctl (dev, rq, cmd)
 *
 *    Process IOCTL commands for this device
 *
 */
static int w83977af_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
{
1219
	struct if_irda_req *irq = (struct if_irda_req *)rq;
L
Linus Torvalds 已提交
1220 1221 1222 1223
	struct w83977af_ir *self;
	unsigned long flags;
	int ret = 0;

1224
	IRDA_ASSERT(dev, return -1;);
L
Linus Torvalds 已提交
1225

1226
	self = netdev_priv(dev);
L
Linus Torvalds 已提交
1227

1228
	IRDA_ASSERT(self, return -1;);
L
Linus Torvalds 已提交
1229

J
Joe Perches 已提交
1230
	pr_debug("%s: %s, (cmd=0x%X)\n", __func__, dev->name, cmd);
1231

L
Linus Torvalds 已提交
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
	spin_lock_irqsave(&self->lock, flags);

	switch (cmd) {
	case SIOCSBANDWIDTH: /* Set bandwidth */
		if (!capable(CAP_NET_ADMIN)) {
			ret = -EPERM;
			goto out;
		}
		w83977af_change_speed(self, irq->ifr_baudrate);
		break;
	case SIOCSMEDIABUSY: /* Set media busy */
		if (!capable(CAP_NET_ADMIN)) {
			ret = -EPERM;
			goto out;
		}
		irda_device_set_media_busy(self->netdev, TRUE);
		break;
	case SIOCGRECEIVING: /* Check if we are receiving right now */
		irq->ifr_receiving = w83977af_is_receiving(self);
		break;
	default:
		ret = -EOPNOTSUPP;
	}
out:
	spin_unlock_irqrestore(&self->lock, flags);
	return ret;
}

MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
MODULE_DESCRIPTION("Winbond W83977AF IrDA Device Driver");
MODULE_LICENSE("GPL");

module_param(qos_mtt_bits, int, 0);
MODULE_PARM_DESC(qos_mtt_bits, "Mimimum Turn Time");
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");

/*
 * Function init_module (void)
 *
1274
 *
L
Linus Torvalds 已提交
1275 1276 1277 1278 1279 1280 1281
 *
 */
module_init(w83977af_init);

/*
 * Function cleanup_module (void)
 *
1282
 *
L
Linus Torvalds 已提交
1283 1284 1285
 *
 */
module_exit(w83977af_cleanup);