blob: 86ca123887d6ac6aec8de5befb50f570dc2d7e22 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*********************************************************************
2 *
3 * Filename: w83977af_ir.c
4 * Version: 1.0
5 * Description: FIR driver for the Winbond W83977AF Super I/O chip
6 * Status: Experimental.
7 * Author: Paul VanderSpek
8 * Created at: Wed Nov 4 11:46:16 1998
9 * Modified at: Fri Jan 28 12:10:59 2000
10 * Modified by: Dag Brattli <dagb@cs.uit.no>
11 *
12 * Copyright (c) 1998-2000 Dag Brattli <dagb@cs.uit.no>
13 * Copyright (c) 1998-1999 Rebel.com
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License as
17 * published by the Free Software Foundation; either version 2 of
18 * the License, or (at your option) any later version.
19 *
20 * Neither Paul VanderSpek nor Rebel.com admit liability nor provide
21 * warranty for any of this software. This material is provided "AS-IS"
22 * and at no charge.
23 *
24 * If you find bugs in this file, its very likely that the same bug
25 * will also be in pc87108.c since the implementations are quite
26 * similar.
27 *
28 * 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.
30 * It is _very_ important to follow this policy!
31 *
32 * __u8 bank;
33 *
34 * bank = inb( iobase+BSR);
35 *
36 * do_your_stuff_here();
37 *
38 * outb( bank, iobase+BSR);
39 *
40 ********************************************************************/
41
42#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/kernel.h>
44#include <linux/types.h>
45#include <linux/skbuff.h>
46#include <linux/netdevice.h>
47#include <linux/ioport.h>
48#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/init.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000050#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/rtnetlink.h>
52#include <linux/dma-mapping.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090053#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55#include <asm/io.h>
56#include <asm/dma.h>
57#include <asm/byteorder.h>
58
59#include <net/irda/irda.h>
60#include <net/irda/wrapper.h>
61#include <net/irda/irda_device.h>
62#include "w83977af.h"
63#include "w83977af_ir.h"
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#define CONFIG_USE_W977_PNP /* Currently needed */
66#define PIO_MAX_SPEED 115200
67
68static char *driver_name = "w83977af_ir";
69static int qos_mtt_bits = 0x07; /* 1 ms or more */
70
71#define CHIP_IO_EXTENT 8
72
73static unsigned int io[] = { 0x180, ~0, ~0, ~0 };
74#ifdef CONFIG_ARCH_NETWINDER /* Adjust to NetWinder differences */
75static unsigned int irq[] = { 6, 0, 0, 0 };
76#else
77static unsigned int irq[] = { 11, 0, 0, 0 };
78#endif
79static unsigned int dma[] = { 1, 0, 0, 0 };
80static unsigned int efbase[] = { W977_EFIO_BASE, W977_EFIO2_BASE };
81static unsigned int efio = W977_EFIO_BASE;
82
83static struct w83977af_ir *dev_self[] = { NULL, NULL, NULL, NULL};
84
85/* Some prototypes */
86static int w83977af_open(int i, unsigned int iobase, unsigned int irq,
87 unsigned int dma);
88static int w83977af_close(struct w83977af_ir *self);
89static int w83977af_probe(int iobase, int irq, int dma);
90static int w83977af_dma_receive(struct w83977af_ir *self);
91static int w83977af_dma_receive_complete(struct w83977af_ir *self);
Stephen Hemminger6518bbb2009-08-31 19:50:50 +000092static netdev_tx_t w83977af_hard_xmit(struct sk_buff *skb,
93 struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094static int w83977af_pio_write(int iobase, __u8 *buf, int len, int fifo_size);
95static void w83977af_dma_write(struct w83977af_ir *self, int iobase);
96static void w83977af_change_speed(struct w83977af_ir *self, __u32 speed);
97static int w83977af_is_receiving(struct w83977af_ir *self);
98
99static int w83977af_net_open(struct net_device *dev);
100static int w83977af_net_close(struct net_device *dev);
101static int w83977af_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103/*
104 * Function w83977af_init ()
105 *
106 * Initialize chip. Just try to find out how many chips we are dealing with
107 * and where they are
108 */
109static int __init w83977af_init(void)
110{
111 int i;
112
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700113 IRDA_DEBUG(0, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Roel Kluin5d5ceb82009-08-06 13:06:03 +0000115 for (i=0; i < ARRAY_SIZE(dev_self) && io[i] < 2000; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 if (w83977af_open(i, io[i], irq[i], dma[i]) == 0)
117 return 0;
118 }
119 return -ENODEV;
120}
121
122/*
123 * Function w83977af_cleanup ()
124 *
125 * Close all configured chips
126 *
127 */
128static void __exit w83977af_cleanup(void)
129{
130 int i;
131
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700132 IRDA_DEBUG(4, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Bjorn Helgaas9c3bd682006-08-15 00:05:38 -0700134 for (i=0; i < ARRAY_SIZE(dev_self); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 if (dev_self[i])
136 w83977af_close(dev_self[i]);
137 }
138}
139
Stephen Hemminger4113a1a2009-03-20 19:35:38 +0000140static const struct net_device_ops w83977_netdev_ops = {
141 .ndo_open = w83977af_net_open,
142 .ndo_stop = w83977af_net_close,
143 .ndo_start_xmit = w83977af_hard_xmit,
144 .ndo_do_ioctl = w83977af_net_ioctl,
145};
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147/*
148 * Function w83977af_open (iobase, irq)
149 *
150 * Open driver instance
151 *
152 */
Hannes Eder0e49e642008-12-26 00:03:19 -0800153static int w83977af_open(int i, unsigned int iobase, unsigned int irq,
154 unsigned int dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
156 struct net_device *dev;
157 struct w83977af_ir *self;
158 int err;
159
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700160 IRDA_DEBUG(0, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 /* Lock the port that we need */
163 if (!request_region(iobase, CHIP_IO_EXTENT, driver_name)) {
164 IRDA_DEBUG(0, "%s(), can't get iobase of 0x%03x\n",
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700165 __func__ , iobase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 return -ENODEV;
167 }
168
169 if (w83977af_probe(iobase, irq, dma) == -1) {
170 err = -1;
171 goto err_out;
172 }
173 /*
174 * Allocate new instance of the driver
175 */
176 dev = alloc_irdadev(sizeof(struct w83977af_ir));
177 if (dev == NULL) {
178 printk( KERN_ERR "IrDA: Can't allocate memory for "
179 "IrDA control block!\n");
180 err = -ENOMEM;
181 goto err_out;
182 }
183
Wang Chen4cf16532008-11-12 23:38:14 -0800184 self = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 spin_lock_init(&self->lock);
186
187
188 /* Initialize IO */
189 self->io.fir_base = iobase;
190 self->io.irq = irq;
191 self->io.fir_ext = CHIP_IO_EXTENT;
192 self->io.dma = dma;
193 self->io.fifo_size = 32;
194
195 /* Initialize QoS for this device */
196 irda_init_max_qos_capabilies(&self->qos);
197
198 /* The only value we must override it the baudrate */
199
200 /* FIXME: The HP HDLS-1100 does not support 1152000! */
201 self->qos.baud_rate.bits = IR_9600|IR_19200|IR_38400|IR_57600|
202 IR_115200|IR_576000|IR_1152000|(IR_4000000 << 8);
203
204 /* The HP HDLS-1100 needs 1 ms according to the specs */
205 self->qos.min_turn_time.bits = qos_mtt_bits;
206 irda_qos_bits_to_value(&self->qos);
207
208 /* Max DMA buffer size needed = (data_size + 6) * (window_size) + 6; */
209 self->rx_buff.truesize = 14384;
210 self->tx_buff.truesize = 4000;
211
212 /* Allocate memory if needed */
213 self->rx_buff.head =
Joe Perchesede23fa2013-08-26 22:45:23 -0700214 dma_zalloc_coherent(NULL, self->rx_buff.truesize,
215 &self->rx_buff_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 if (self->rx_buff.head == NULL) {
217 err = -ENOMEM;
218 goto err_out1;
219 }
220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 self->tx_buff.head =
Joe Perchesede23fa2013-08-26 22:45:23 -0700222 dma_zalloc_coherent(NULL, self->tx_buff.truesize,
223 &self->tx_buff_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 if (self->tx_buff.head == NULL) {
225 err = -ENOMEM;
226 goto err_out2;
227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 self->rx_buff.in_frame = FALSE;
230 self->rx_buff.state = OUTSIDE_FRAME;
231 self->tx_buff.data = self->tx_buff.head;
232 self->rx_buff.data = self->rx_buff.head;
233 self->netdev = dev;
234
Stephen Hemminger4113a1a2009-03-20 19:35:38 +0000235 dev->netdev_ops = &w83977_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 err = register_netdev(dev);
238 if (err) {
Joe Perches6c910232014-11-11 13:37:30 -0800239 net_err_ratelimited("%s(), register_netdevice() failed!\n",
240 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 goto err_out3;
242 }
Joe Perches6c910232014-11-11 13:37:30 -0800243 net_info_ratelimited("IrDA: Registered device %s\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245 /* Need to store self somewhere */
246 dev_self[i] = self;
247
248 return 0;
249err_out3:
250 dma_free_coherent(NULL, self->tx_buff.truesize,
251 self->tx_buff.head, self->tx_buff_dma);
252err_out2:
253 dma_free_coherent(NULL, self->rx_buff.truesize,
254 self->rx_buff.head, self->rx_buff_dma);
255err_out1:
256 free_netdev(dev);
257err_out:
258 release_region(iobase, CHIP_IO_EXTENT);
259 return err;
260}
261
262/*
263 * Function w83977af_close (self)
264 *
265 * Close driver instance
266 *
267 */
268static int w83977af_close(struct w83977af_ir *self)
269{
270 int iobase;
271
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700272 IRDA_DEBUG(0, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274 iobase = self->io.fir_base;
275
276#ifdef CONFIG_USE_W977_PNP
277 /* enter PnP configuration mode */
278 w977_efm_enter(efio);
279
280 w977_select_device(W977_DEVICE_IR, efio);
281
282 /* Deactivate device */
283 w977_write_reg(0x30, 0x00, efio);
284
285 w977_efm_exit(efio);
286#endif /* CONFIG_USE_W977_PNP */
287
288 /* Remove netdevice */
289 unregister_netdev(self->netdev);
290
291 /* Release the PORT that this driver is using */
292 IRDA_DEBUG(0 , "%s(), Releasing Region %03x\n",
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700293 __func__ , self->io.fir_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 release_region(self->io.fir_base, self->io.fir_ext);
295
296 if (self->tx_buff.head)
297 dma_free_coherent(NULL, self->tx_buff.truesize,
298 self->tx_buff.head, self->tx_buff_dma);
299
300 if (self->rx_buff.head)
301 dma_free_coherent(NULL, self->rx_buff.truesize,
302 self->rx_buff.head, self->rx_buff_dma);
303
304 free_netdev(self->netdev);
305
306 return 0;
307}
308
Hannes Eder0e49e642008-12-26 00:03:19 -0800309static int w83977af_probe(int iobase, int irq, int dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
311 int version;
312 int i;
313
314 for (i=0; i < 2; i++) {
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700315 IRDA_DEBUG( 0, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316#ifdef CONFIG_USE_W977_PNP
317 /* Enter PnP configuration mode */
318 w977_efm_enter(efbase[i]);
319
320 w977_select_device(W977_DEVICE_IR, efbase[i]);
321
322 /* Configure PnP port, IRQ, and DMA channel */
323 w977_write_reg(0x60, (iobase >> 8) & 0xff, efbase[i]);
324 w977_write_reg(0x61, (iobase) & 0xff, efbase[i]);
325
326 w977_write_reg(0x70, irq, efbase[i]);
327#ifdef CONFIG_ARCH_NETWINDER
328 /* Netwinder uses 1 higher than Linux */
329 w977_write_reg(0x74, dma+1, efbase[i]);
330#else
331 w977_write_reg(0x74, dma, efbase[i]);
Paul Bolle528b9712014-05-04 13:03:12 +0200332#endif /* CONFIG_ARCH_NETWINDER */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 w977_write_reg(0x75, 0x04, efbase[i]); /* Disable Tx DMA */
334
335 /* Set append hardware CRC, enable IR bank selection */
336 w977_write_reg(0xf0, APEDCRC|ENBNKSEL, efbase[i]);
337
338 /* Activate device */
339 w977_write_reg(0x30, 0x01, efbase[i]);
340
341 w977_efm_exit(efbase[i]);
342#endif /* CONFIG_USE_W977_PNP */
343 /* Disable Advanced mode */
344 switch_bank(iobase, SET2);
345 outb(iobase+2, 0x00);
346
347 /* Turn on UART (global) interrupts */
348 switch_bank(iobase, SET0);
349 outb(HCR_EN_IRQ, iobase+HCR);
350
351 /* Switch to advanced mode */
352 switch_bank(iobase, SET2);
353 outb(inb(iobase+ADCR1) | ADCR1_ADV_SL, iobase+ADCR1);
354
355 /* Set default IR-mode */
356 switch_bank(iobase, SET0);
357 outb(HCR_SIR, iobase+HCR);
358
359 /* Read the Advanced IR ID */
360 switch_bank(iobase, SET3);
361 version = inb(iobase+AUID);
362
363 /* Should be 0x1? */
364 if (0x10 == (version & 0xf0)) {
365 efio = efbase[i];
366
367 /* Set FIFO size to 32 */
368 switch_bank(iobase, SET2);
369 outb(ADCR2_RXFS32|ADCR2_TXFS32, iobase+ADCR2);
370
371 /* Set FIFO threshold to TX17, RX16 */
372 switch_bank(iobase, SET0);
373 outb(UFR_RXTL|UFR_TXTL|UFR_TXF_RST|UFR_RXF_RST|
374 UFR_EN_FIFO,iobase+UFR);
375
376 /* Receiver frame length */
377 switch_bank(iobase, SET4);
378 outb(2048 & 0xff, iobase+6);
379 outb((2048 >> 8) & 0x1f, iobase+7);
380
381 /*
382 * Init HP HSDL-1100 transceiver.
383 *
384 * Set IRX_MSL since we have 2 * receive paths IRRX,
385 * and IRRXH. Clear IRSL0D since we want IRSL0 * to
386 * be a input pin used for IRRXH
387 *
388 * IRRX pin 37 connected to receiver
389 * IRTX pin 38 connected to transmitter
390 * FIRRX pin 39 connected to receiver (IRSL0)
391 * CIRRX pin 40 connected to pin 37
392 */
393 switch_bank(iobase, SET7);
394 outb(0x40, iobase+7);
395
Joe Perches6c910232014-11-11 13:37:30 -0800396 net_info_ratelimited("W83977AF (IR) driver loaded. Version: 0x%02x\n",
397 version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 return 0;
400 } else {
401 /* Try next extented function register address */
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700402 IRDA_DEBUG( 0, "%s(), Wrong chip version", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 }
404 }
405 return -1;
406}
407
Hannes Eder0e49e642008-12-26 00:03:19 -0800408static void w83977af_change_speed(struct w83977af_ir *self, __u32 speed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
410 int ir_mode = HCR_SIR;
411 int iobase;
412 __u8 set;
413
414 iobase = self->io.fir_base;
415
416 /* Update accounting for new speed */
417 self->io.speed = speed;
418
419 /* Save current bank */
420 set = inb(iobase+SSR);
421
422 /* Disable interrupts */
423 switch_bank(iobase, SET0);
424 outb(0, iobase+ICR);
425
426 /* Select Set 2 */
427 switch_bank(iobase, SET2);
428 outb(0x00, iobase+ABHL);
429
430 switch (speed) {
431 case 9600: outb(0x0c, iobase+ABLL); break;
432 case 19200: outb(0x06, iobase+ABLL); break;
433 case 38400: outb(0x03, iobase+ABLL); break;
434 case 57600: outb(0x02, iobase+ABLL); break;
435 case 115200: outb(0x01, iobase+ABLL); break;
436 case 576000:
437 ir_mode = HCR_MIR_576;
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700438 IRDA_DEBUG(0, "%s(), handling baud of 576000\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 break;
440 case 1152000:
441 ir_mode = HCR_MIR_1152;
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700442 IRDA_DEBUG(0, "%s(), handling baud of 1152000\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 break;
444 case 4000000:
445 ir_mode = HCR_FIR;
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700446 IRDA_DEBUG(0, "%s(), handling baud of 4000000\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 break;
448 default:
449 ir_mode = HCR_FIR;
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700450 IRDA_DEBUG(0, "%s(), unknown baud rate of %d\n", __func__ , speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 break;
452 }
453
454 /* Set speed mode */
455 switch_bank(iobase, SET0);
456 outb(ir_mode, iobase+HCR);
457
458 /* set FIFO size to 32 */
459 switch_bank(iobase, SET2);
460 outb(ADCR2_RXFS32|ADCR2_TXFS32, iobase+ADCR2);
461
462 /* set FIFO threshold to TX17, RX16 */
463 switch_bank(iobase, SET0);
464 outb(0x00, iobase+UFR); /* Reset */
465 outb(UFR_EN_FIFO, iobase+UFR); /* First we must enable FIFO */
466 outb(0xa7, iobase+UFR);
467
468 netif_wake_queue(self->netdev);
469
470 /* Enable some interrupts so we can receive frames */
471 switch_bank(iobase, SET0);
472 if (speed > PIO_MAX_SPEED) {
473 outb(ICR_EFSFI, iobase+ICR);
474 w83977af_dma_receive(self);
475 } else
476 outb(ICR_ERBRI, iobase+ICR);
477
478 /* Restore SSR */
479 outb(set, iobase+SSR);
480}
481
482/*
483 * Function w83977af_hard_xmit (skb, dev)
484 *
485 * Sets up a DMA transfer to send the current frame.
486 *
487 */
Stephen Hemminger6518bbb2009-08-31 19:50:50 +0000488static netdev_tx_t w83977af_hard_xmit(struct sk_buff *skb,
489 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
491 struct w83977af_ir *self;
492 __s32 speed;
493 int iobase;
494 __u8 set;
495 int mtt;
496
Wang Chen4cf16532008-11-12 23:38:14 -0800497 self = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 iobase = self->io.fir_base;
500
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700501 IRDA_DEBUG(4, "%s(%ld), skb->len=%d\n", __func__ , jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 (int) skb->len);
503
504 /* Lock transmit buffer */
505 netif_stop_queue(dev);
506
507 /* Check if we need to change the speed */
508 speed = irda_get_next_speed(skb);
509 if ((speed != self->io.speed) && (speed != -1)) {
510 /* Check for empty frame */
511 if (!skb->len) {
512 w83977af_change_speed(self, speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 dev_kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000514 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 } else
516 self->new_speed = speed;
517 }
518
519 /* Save current set */
520 set = inb(iobase+SSR);
521
522 /* Decide if we should use PIO or DMA transfer */
523 if (self->io.speed > PIO_MAX_SPEED) {
524 self->tx_buff.data = self->tx_buff.head;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300525 skb_copy_from_linear_data(skb, self->tx_buff.data, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 self->tx_buff.len = skb->len;
527
528 mtt = irda_get_mtt(skb);
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700529 IRDA_DEBUG(4, "%s(%ld), mtt=%d\n", __func__ , jiffies, mtt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 if (mtt)
531 udelay(mtt);
532
533 /* Enable DMA interrupt */
534 switch_bank(iobase, SET0);
535 outb(ICR_EDMAI, iobase+ICR);
536 w83977af_dma_write(self, iobase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 } else {
538 self->tx_buff.data = self->tx_buff.head;
539 self->tx_buff.len = async_wrap_skb(skb, self->tx_buff.data,
540 self->tx_buff.truesize);
541
542 /* Add interrupt on tx low level (will fire immediately) */
543 switch_bank(iobase, SET0);
544 outb(ICR_ETXTHI, iobase+ICR);
545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 dev_kfree_skb(skb);
547
548 /* Restore set register */
549 outb(set, iobase+SSR);
550
Patrick McHardy6ed10652009-06-23 06:03:08 +0000551 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552}
553
554/*
555 * Function w83977af_dma_write (self, iobase)
556 *
557 * Send frame using DMA
558 *
559 */
560static void w83977af_dma_write(struct w83977af_ir *self, int iobase)
561{
562 __u8 set;
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700563 IRDA_DEBUG(4, "%s(), len=%d\n", __func__ , self->tx_buff.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 /* Save current set */
566 set = inb(iobase+SSR);
567
568 /* Disable DMA */
569 switch_bank(iobase, SET0);
570 outb(inb(iobase+HCR) & ~HCR_EN_DMA, iobase+HCR);
571
572 /* Choose transmit DMA channel */
573 switch_bank(iobase, SET2);
574 outb(ADCR1_D_CHSW|/*ADCR1_DMA_F|*/ADCR1_ADV_SL, iobase+ADCR1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 irda_setup_dma(self->io.dma, self->tx_buff_dma, self->tx_buff.len,
576 DMA_MODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 self->io.direction = IO_XMIT;
578
579 /* Enable DMA */
580 switch_bank(iobase, SET0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 outb(inb(iobase+HCR) | HCR_EN_DMA | HCR_TX_WT, iobase+HCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 /* Restore set register */
584 outb(set, iobase+SSR);
585}
586
587/*
588 * Function w83977af_pio_write (iobase, buf, len, fifo_size)
589 *
590 *
591 *
592 */
593static int w83977af_pio_write(int iobase, __u8 *buf, int len, int fifo_size)
594{
595 int actual = 0;
596 __u8 set;
597
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700598 IRDA_DEBUG(4, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 /* Save current bank */
601 set = inb(iobase+SSR);
602
603 switch_bank(iobase, SET0);
604 if (!(inb_p(iobase+USR) & USR_TSRE)) {
605 IRDA_DEBUG(4,
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700606 "%s(), warning, FIFO not empty yet!\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608 fifo_size -= 17;
609 IRDA_DEBUG(4, "%s(), %d bytes left in tx fifo\n",
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700610 __func__ , fifo_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 }
612
613 /* Fill FIFO with current frame */
614 while ((fifo_size-- > 0) && (actual < len)) {
615 /* Transmit next byte */
616 outb(buf[actual++], iobase+TBR);
617 }
618
619 IRDA_DEBUG(4, "%s(), fifo_size %d ; %d sent of %d\n",
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700620 __func__ , fifo_size, actual, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 /* Restore bank */
623 outb(set, iobase+SSR);
624
625 return actual;
626}
627
628/*
629 * Function w83977af_dma_xmit_complete (self)
630 *
631 * The transfer of a frame in finished. So do the necessary things
632 *
633 *
634 */
635static void w83977af_dma_xmit_complete(struct w83977af_ir *self)
636{
637 int iobase;
638 __u8 set;
639
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700640 IRDA_DEBUG(4, "%s(%ld)\n", __func__ , jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642 IRDA_ASSERT(self != NULL, return;);
643
644 iobase = self->io.fir_base;
645
646 /* Save current set */
647 set = inb(iobase+SSR);
648
649 /* Disable DMA */
650 switch_bank(iobase, SET0);
651 outb(inb(iobase+HCR) & ~HCR_EN_DMA, iobase+HCR);
652
Justin P. Mattock42b2aa82011-11-28 20:31:00 -0800653 /* Check for underrun! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 if (inb(iobase+AUDR) & AUDR_UNDR) {
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700655 IRDA_DEBUG(0, "%s(), Transmit underrun!\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800657 self->netdev->stats.tx_errors++;
658 self->netdev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 /* Clear bit, by writing 1 to it */
661 outb(AUDR_UNDR, iobase+AUDR);
662 } else
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800663 self->netdev->stats.tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665
666 if (self->new_speed) {
667 w83977af_change_speed(self, self->new_speed);
668 self->new_speed = 0;
669 }
670
671 /* Unlock tx_buff and request another frame */
672 /* Tell the network layer, that we want more frames */
673 netif_wake_queue(self->netdev);
674
675 /* Restore set */
676 outb(set, iobase+SSR);
677}
678
679/*
680 * Function w83977af_dma_receive (self)
681 *
682 * Get ready for receiving a frame. The device will initiate a DMA
683 * if it starts to receive a frame.
684 *
685 */
Hannes Eder0e49e642008-12-26 00:03:19 -0800686static int w83977af_dma_receive(struct w83977af_ir *self)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
688 int iobase;
689 __u8 set;
Paul Bolle528b9712014-05-04 13:03:12 +0200690#ifdef CONFIG_ARCH_NETWINDER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 unsigned long flags;
692 __u8 hcr;
693#endif
694 IRDA_ASSERT(self != NULL, return -1;);
695
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700696 IRDA_DEBUG(4, "%s\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698 iobase= self->io.fir_base;
699
700 /* Save current set */
701 set = inb(iobase+SSR);
702
703 /* Disable DMA */
704 switch_bank(iobase, SET0);
705 outb(inb(iobase+HCR) & ~HCR_EN_DMA, iobase+HCR);
706
707 /* Choose DMA Rx, DMA Fairness, and Advanced mode */
708 switch_bank(iobase, SET2);
709 outb((inb(iobase+ADCR1) & ~ADCR1_D_CHSW)/*|ADCR1_DMA_F*/|ADCR1_ADV_SL,
710 iobase+ADCR1);
711
712 self->io.direction = IO_RECV;
713 self->rx_buff.data = self->rx_buff.head;
714
Paul Bolle528b9712014-05-04 13:03:12 +0200715#ifdef CONFIG_ARCH_NETWINDER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 spin_lock_irqsave(&self->lock, flags);
717
718 disable_dma(self->io.dma);
719 clear_dma_ff(self->io.dma);
720 set_dma_mode(self->io.dma, DMA_MODE_READ);
721 set_dma_addr(self->io.dma, self->rx_buff_dma);
722 set_dma_count(self->io.dma, self->rx_buff.truesize);
723#else
724 irda_setup_dma(self->io.dma, self->rx_buff_dma, self->rx_buff.truesize,
725 DMA_MODE_READ);
726#endif
727 /*
728 * Reset Rx FIFO. This will also flush the ST_FIFO, it's very
729 * important that we don't reset the Tx FIFO since it might not
730 * be finished transmitting yet
731 */
732 switch_bank(iobase, SET0);
733 outb(UFR_RXTL|UFR_TXTL|UFR_RXF_RST|UFR_EN_FIFO, iobase+UFR);
734 self->st_fifo.len = self->st_fifo.tail = self->st_fifo.head = 0;
735
736 /* Enable DMA */
737 switch_bank(iobase, SET0);
Paul Bolle528b9712014-05-04 13:03:12 +0200738#ifdef CONFIG_ARCH_NETWINDER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 hcr = inb(iobase+HCR);
740 outb(hcr | HCR_EN_DMA, iobase+HCR);
741 enable_dma(self->io.dma);
742 spin_unlock_irqrestore(&self->lock, flags);
743#else
744 outb(inb(iobase+HCR) | HCR_EN_DMA, iobase+HCR);
745#endif
746 /* Restore set */
747 outb(set, iobase+SSR);
748
749 return 0;
750}
751
752/*
753 * Function w83977af_receive_complete (self)
754 *
755 * Finished with receiving a frame
756 *
757 */
Hannes Eder0e49e642008-12-26 00:03:19 -0800758static int w83977af_dma_receive_complete(struct w83977af_ir *self)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
760 struct sk_buff *skb;
761 struct st_fifo *st_fifo;
762 int len;
763 int iobase;
764 __u8 set;
765 __u8 status;
766
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700767 IRDA_DEBUG(4, "%s\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
769 st_fifo = &self->st_fifo;
770
771 iobase = self->io.fir_base;
772
773 /* Save current set */
774 set = inb(iobase+SSR);
775
776 iobase = self->io.fir_base;
777
778 /* Read status FIFO */
779 switch_bank(iobase, SET5);
780 while ((status = inb(iobase+FS_FO)) & FS_FO_FSFDR) {
781 st_fifo->entries[st_fifo->tail].status = status;
782
783 st_fifo->entries[st_fifo->tail].len = inb(iobase+RFLFL);
784 st_fifo->entries[st_fifo->tail].len |= inb(iobase+RFLFH) << 8;
785
786 st_fifo->tail++;
787 st_fifo->len++;
788 }
789
790 while (st_fifo->len) {
791 /* Get first entry */
792 status = st_fifo->entries[st_fifo->head].status;
793 len = st_fifo->entries[st_fifo->head].len;
794 st_fifo->head++;
795 st_fifo->len--;
796
797 /* Check for errors */
798 if (status & FS_FO_ERR_MSK) {
799 if (status & FS_FO_LST_FR) {
800 /* Add number of lost frames to stats */
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800801 self->netdev->stats.rx_errors += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 } else {
803 /* Skip frame */
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800804 self->netdev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
806 self->rx_buff.data += len;
807
808 if (status & FS_FO_MX_LEX)
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800809 self->netdev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
811 if (status & FS_FO_PHY_ERR)
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800812 self->netdev->stats.rx_frame_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
814 if (status & FS_FO_CRC_ERR)
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800815 self->netdev->stats.rx_crc_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 }
817 /* The errors below can be reported in both cases */
818 if (status & FS_FO_RX_OV)
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800819 self->netdev->stats.rx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 if (status & FS_FO_FSF_OV)
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800822 self->netdev->stats.rx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
824 } else {
825 /* Check if we have transferred all data to memory */
826 switch_bank(iobase, SET0);
827 if (inb(iobase+USR) & USR_RDR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 udelay(80); /* Should be enough!? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 }
830
831 skb = dev_alloc_skb(len+1);
832 if (skb == NULL) {
833 printk(KERN_INFO
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700834 "%s(), memory squeeze, dropping frame.\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 /* Restore set register */
836 outb(set, iobase+SSR);
837
838 return FALSE;
839 }
840
841 /* Align to 20 bytes */
842 skb_reserve(skb, 1);
843
844 /* Copy frame without CRC */
845 if (self->io.speed < 4000000) {
846 skb_put(skb, len-2);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300847 skb_copy_to_linear_data(skb,
848 self->rx_buff.data,
849 len - 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 } else {
851 skb_put(skb, len-4);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300852 skb_copy_to_linear_data(skb,
853 self->rx_buff.data,
854 len - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 }
856
857 /* Move to next frame */
858 self->rx_buff.data += len;
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800859 self->netdev->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
861 skb->dev = self->netdev;
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700862 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 skb->protocol = htons(ETH_P_IRDA);
864 netif_rx(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 }
866 }
867 /* Restore set register */
868 outb(set, iobase+SSR);
869
870 return TRUE;
871}
872
873/*
874 * Function pc87108_pio_receive (self)
875 *
876 * Receive all data in receiver FIFO
877 *
878 */
879static void w83977af_pio_receive(struct w83977af_ir *self)
880{
881 __u8 byte = 0x00;
882 int iobase;
883
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700884 IRDA_DEBUG(4, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
886 IRDA_ASSERT(self != NULL, return;);
887
888 iobase = self->io.fir_base;
889
890 /* Receive all characters in Rx FIFO */
891 do {
892 byte = inb(iobase+RBR);
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800893 async_unwrap_char(self->netdev, &self->netdev->stats, &self->rx_buff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 byte);
895 } while (inb(iobase+USR) & USR_RDR); /* Data available */
896}
897
898/*
899 * Function w83977af_sir_interrupt (self, eir)
900 *
901 * Handle SIR interrupt
902 *
903 */
904static __u8 w83977af_sir_interrupt(struct w83977af_ir *self, int isr)
905{
906 int actual;
907 __u8 new_icr = 0;
908 __u8 set;
909 int iobase;
910
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700911 IRDA_DEBUG(4, "%s(), isr=%#x\n", __func__ , isr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
913 iobase = self->io.fir_base;
914 /* Transmit FIFO low on data */
915 if (isr & ISR_TXTH_I) {
916 /* Write data left in transmit buffer */
917 actual = w83977af_pio_write(self->io.fir_base,
918 self->tx_buff.data,
919 self->tx_buff.len,
920 self->io.fifo_size);
921
922 self->tx_buff.data += actual;
923 self->tx_buff.len -= actual;
924
925 self->io.direction = IO_XMIT;
926
927 /* Check if finished */
928 if (self->tx_buff.len > 0) {
929 new_icr |= ICR_ETXTHI;
930 } else {
931 set = inb(iobase+SSR);
932 switch_bank(iobase, SET0);
933 outb(AUDR_SFEND, iobase+AUDR);
934 outb(set, iobase+SSR);
935
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800936 self->netdev->stats.tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
938 /* Feed me more packets */
939 netif_wake_queue(self->netdev);
940 new_icr |= ICR_ETBREI;
941 }
942 }
943 /* Check if transmission has completed */
944 if (isr & ISR_TXEMP_I) {
945 /* Check if we need to change the speed? */
946 if (self->new_speed) {
947 IRDA_DEBUG(2,
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700948 "%s(), Changing speed!\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 w83977af_change_speed(self, self->new_speed);
950 self->new_speed = 0;
951 }
952
953 /* Turn around and get ready to receive some data */
954 self->io.direction = IO_RECV;
955 new_icr |= ICR_ERBRI;
956 }
957
958 /* Rx FIFO threshold or timeout */
959 if (isr & ISR_RXTH_I) {
960 w83977af_pio_receive(self);
961
962 /* Keep receiving */
963 new_icr |= ICR_ERBRI;
964 }
965 return new_icr;
966}
967
968/*
969 * Function pc87108_fir_interrupt (self, eir)
970 *
971 * Handle MIR/FIR interrupt
972 *
973 */
974static __u8 w83977af_fir_interrupt(struct w83977af_ir *self, int isr)
975{
976 __u8 new_icr = 0;
977 __u8 set;
978 int iobase;
979
980 iobase = self->io.fir_base;
981 set = inb(iobase+SSR);
982
983 /* End of frame detected in FIFO */
984 if (isr & (ISR_FEND_I|ISR_FSF_I)) {
985 if (w83977af_dma_receive_complete(self)) {
986
987 /* Wait for next status FIFO interrupt */
988 new_icr |= ICR_EFSFI;
989 } else {
990 /* DMA not finished yet */
991
992 /* Set timer value, resolution 1 ms */
993 switch_bank(iobase, SET4);
994 outb(0x01, iobase+TMRL); /* 1 ms */
995 outb(0x00, iobase+TMRH);
996
997 /* Start timer */
998 outb(IR_MSL_EN_TMR, iobase+IR_MSL);
999
1000 new_icr |= ICR_ETMRI;
1001 }
1002 }
1003 /* Timer finished */
1004 if (isr & ISR_TMR_I) {
1005 /* Disable timer */
1006 switch_bank(iobase, SET4);
1007 outb(0, iobase+IR_MSL);
1008
1009 /* Clear timer event */
1010 /* switch_bank(iobase, SET0); */
1011/* outb(ASCR_CTE, iobase+ASCR); */
1012
1013 /* Check if this is a TX timer interrupt */
1014 if (self->io.direction == IO_XMIT) {
1015 w83977af_dma_write(self, iobase);
1016
1017 new_icr |= ICR_EDMAI;
1018 } else {
1019 /* Check if DMA has now finished */
1020 w83977af_dma_receive_complete(self);
1021
1022 new_icr |= ICR_EFSFI;
1023 }
1024 }
1025 /* Finished with DMA */
1026 if (isr & ISR_DMA_I) {
1027 w83977af_dma_xmit_complete(self);
1028
1029 /* Check if there are more frames to be transmitted */
1030 /* if (irda_device_txqueue_empty(self)) { */
1031
1032 /* Prepare for receive
1033 *
1034 * ** Netwinder Tx DMA likes that we do this anyway **
1035 */
1036 w83977af_dma_receive(self);
1037 new_icr = ICR_EFSFI;
1038 /* } */
1039 }
1040
1041 /* Restore set */
1042 outb(set, iobase+SSR);
1043
1044 return new_icr;
1045}
1046
1047/*
1048 * Function w83977af_interrupt (irq, dev_id, regs)
1049 *
1050 * An interrupt from the chip has arrived. Time to do some work
1051 *
1052 */
David Howells7d12e782006-10-05 14:55:46 +01001053static irqreturn_t w83977af_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054{
Jeff Garzikc31f28e2006-10-06 14:56:04 -04001055 struct net_device *dev = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 struct w83977af_ir *self;
1057 __u8 set, icr, isr;
1058 int iobase;
1059
Wang Chen4cf16532008-11-12 23:38:14 -08001060 self = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
1062 iobase = self->io.fir_base;
1063
1064 /* Save current bank */
1065 set = inb(iobase+SSR);
1066 switch_bank(iobase, SET0);
1067
1068 icr = inb(iobase+ICR);
1069 isr = inb(iobase+ISR) & icr; /* Mask out the interesting ones */
1070
1071 outb(0, iobase+ICR); /* Disable interrupts */
1072
1073 if (isr) {
1074 /* Dispatch interrupt handler for the current speed */
1075 if (self->io.speed > PIO_MAX_SPEED )
1076 icr = w83977af_fir_interrupt(self, isr);
1077 else
1078 icr = w83977af_sir_interrupt(self, isr);
1079 }
1080
1081 outb(icr, iobase+ICR); /* Restore (new) interrupts */
1082 outb(set, iobase+SSR); /* Restore bank register */
1083 return IRQ_RETVAL(isr);
1084}
1085
1086/*
1087 * Function w83977af_is_receiving (self)
1088 *
1089 * Return TRUE is we are currently receiving a frame
1090 *
1091 */
1092static int w83977af_is_receiving(struct w83977af_ir *self)
1093{
1094 int status = FALSE;
1095 int iobase;
1096 __u8 set;
1097
1098 IRDA_ASSERT(self != NULL, return FALSE;);
1099
1100 if (self->io.speed > 115200) {
1101 iobase = self->io.fir_base;
1102
1103 /* Check if rx FIFO is not empty */
1104 set = inb(iobase+SSR);
1105 switch_bank(iobase, SET2);
1106 if ((inb(iobase+RXFDTH) & 0x3f) != 0) {
1107 /* We are receiving something */
1108 status = TRUE;
1109 }
1110 outb(set, iobase+SSR);
1111 } else
1112 status = (self->rx_buff.state != OUTSIDE_FRAME);
1113
1114 return status;
1115}
1116
1117/*
1118 * Function w83977af_net_open (dev)
1119 *
1120 * Start the device
1121 *
1122 */
1123static int w83977af_net_open(struct net_device *dev)
1124{
1125 struct w83977af_ir *self;
1126 int iobase;
1127 char hwname[32];
1128 __u8 set;
1129
Harvey Harrisona97a6f12008-07-30 17:20:18 -07001130 IRDA_DEBUG(0, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
1132 IRDA_ASSERT(dev != NULL, return -1;);
Wang Chen4cf16532008-11-12 23:38:14 -08001133 self = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
1135 IRDA_ASSERT(self != NULL, return 0;);
1136
1137 iobase = self->io.fir_base;
1138
1139 if (request_irq(self->io.irq, w83977af_interrupt, 0, dev->name,
1140 (void *) dev)) {
1141 return -EAGAIN;
1142 }
1143 /*
1144 * Always allocate the DMA channel after the IRQ,
1145 * and clean up on failure.
1146 */
1147 if (request_dma(self->io.dma, dev->name)) {
Julia Lawalla997cbb32012-03-11 11:49:02 +00001148 free_irq(self->io.irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 return -EAGAIN;
1150 }
1151
1152 /* Save current set */
1153 set = inb(iobase+SSR);
1154
1155 /* Enable some interrupts so we can receive frames again */
1156 switch_bank(iobase, SET0);
1157 if (self->io.speed > 115200) {
1158 outb(ICR_EFSFI, iobase+ICR);
1159 w83977af_dma_receive(self);
1160 } else
1161 outb(ICR_ERBRI, iobase+ICR);
1162
1163 /* Restore bank register */
1164 outb(set, iobase+SSR);
1165
1166 /* Ready to play! */
1167 netif_start_queue(dev);
1168
1169 /* Give self a hardware name */
1170 sprintf(hwname, "w83977af @ 0x%03x", self->io.fir_base);
1171
1172 /*
1173 * Open new IrLAP layer instance, now that everything should be
1174 * initialized properly
1175 */
1176 self->irlap = irlap_open(dev, &self->qos, hwname);
1177
1178 return 0;
1179}
1180
1181/*
1182 * Function w83977af_net_close (dev)
1183 *
1184 * Stop the device
1185 *
1186 */
1187static int w83977af_net_close(struct net_device *dev)
1188{
1189 struct w83977af_ir *self;
1190 int iobase;
1191 __u8 set;
1192
Harvey Harrisona97a6f12008-07-30 17:20:18 -07001193 IRDA_DEBUG(0, "%s()\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
1195 IRDA_ASSERT(dev != NULL, return -1;);
1196
Wang Chen4cf16532008-11-12 23:38:14 -08001197 self = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
1199 IRDA_ASSERT(self != NULL, return 0;);
1200
1201 iobase = self->io.fir_base;
1202
1203 /* Stop device */
1204 netif_stop_queue(dev);
1205
1206 /* Stop and remove instance of IrLAP */
1207 if (self->irlap)
1208 irlap_close(self->irlap);
1209 self->irlap = NULL;
1210
1211 disable_dma(self->io.dma);
1212
1213 /* Save current set */
1214 set = inb(iobase+SSR);
1215
1216 /* Disable interrupts */
1217 switch_bank(iobase, SET0);
1218 outb(0, iobase+ICR);
1219
1220 free_irq(self->io.irq, dev);
1221 free_dma(self->io.dma);
1222
1223 /* Restore bank register */
1224 outb(set, iobase+SSR);
1225
1226 return 0;
1227}
1228
1229/*
1230 * Function w83977af_net_ioctl (dev, rq, cmd)
1231 *
1232 * Process IOCTL commands for this device
1233 *
1234 */
1235static int w83977af_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1236{
1237 struct if_irda_req *irq = (struct if_irda_req *) rq;
1238 struct w83977af_ir *self;
1239 unsigned long flags;
1240 int ret = 0;
1241
1242 IRDA_ASSERT(dev != NULL, return -1;);
1243
Wang Chen4cf16532008-11-12 23:38:14 -08001244 self = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
1246 IRDA_ASSERT(self != NULL, return -1;);
1247
Harvey Harrisona97a6f12008-07-30 17:20:18 -07001248 IRDA_DEBUG(2, "%s(), %s, (cmd=0x%X)\n", __func__ , dev->name, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
1250 spin_lock_irqsave(&self->lock, flags);
1251
1252 switch (cmd) {
1253 case SIOCSBANDWIDTH: /* Set bandwidth */
1254 if (!capable(CAP_NET_ADMIN)) {
1255 ret = -EPERM;
1256 goto out;
1257 }
1258 w83977af_change_speed(self, irq->ifr_baudrate);
1259 break;
1260 case SIOCSMEDIABUSY: /* Set media busy */
1261 if (!capable(CAP_NET_ADMIN)) {
1262 ret = -EPERM;
1263 goto out;
1264 }
1265 irda_device_set_media_busy(self->netdev, TRUE);
1266 break;
1267 case SIOCGRECEIVING: /* Check if we are receiving right now */
1268 irq->ifr_receiving = w83977af_is_receiving(self);
1269 break;
1270 default:
1271 ret = -EOPNOTSUPP;
1272 }
1273out:
1274 spin_unlock_irqrestore(&self->lock, flags);
1275 return ret;
1276}
1277
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
1279MODULE_DESCRIPTION("Winbond W83977AF IrDA Device Driver");
1280MODULE_LICENSE("GPL");
1281
1282
1283module_param(qos_mtt_bits, int, 0);
1284MODULE_PARM_DESC(qos_mtt_bits, "Mimimum Turn Time");
1285module_param_array(io, int, NULL, 0);
1286MODULE_PARM_DESC(io, "Base I/O addresses");
1287module_param_array(irq, int, NULL, 0);
1288MODULE_PARM_DESC(irq, "IRQ lines");
1289
1290/*
1291 * Function init_module (void)
1292 *
1293 *
1294 *
1295 */
1296module_init(w83977af_init);
1297
1298/*
1299 * Function cleanup_module (void)
1300 *
1301 *
1302 *
1303 */
1304module_exit(w83977af_cleanup);