blob: 81a4ccf54c574d450322a1d45f2b4d6450746891 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*********************************************************************
2 *
3 * Filename: nsc-ircc.c
4 * Version: 1.0
5 * Description: Driver for the NSC PC'108 and PC'338 IrDA chipsets
6 * Status: Stable.
7 * Author: Dag Brattli <dagb@cs.uit.no>
8 * Created at: Sat Nov 7 21:43:15 1998
9 * Modified at: Wed Mar 1 11:29:34 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 Lichen Wang, <lwang@actisys.com>
14 * Copyright (c) 1998 Actisys Corp., www.actisys.com
Jean Tourrilhesec4f32d2006-03-20 18:54:03 -080015 * Copyright (c) 2000-2004 Jean Tourrilhes <jt@hpl.hp.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 * All Rights Reserved
17 *
18 * This program is free software; you can redistribute it and/or
19 * modify it under the terms of the GNU General Public License as
20 * published by the Free Software Foundation; either version 2 of
21 * the License, or (at your option) any later version.
22 *
23 * Neither Dag Brattli nor University of Tromsø admit liability nor
24 * provide warranty for any of this software. This material is
25 * provided "AS-IS" and at no charge.
26 *
27 * Notice that all functions that needs to access the chip in _any_
28 * way, must save BSR register on entry, and restore it on exit.
29 * It is _very_ important to follow this policy!
30 *
31 * __u8 bank;
32 *
33 * bank = inb(iobase+BSR);
34 *
35 * do_your_stuff_here();
36 *
37 * outb(bank, iobase+BSR);
38 *
39 * If you find bugs in this file, its very likely that the same bug
40 * will also be in w83977af_ir.c since the implementations are quite
41 * similar.
42 *
43 ********************************************************************/
44
45#include <linux/module.h>
46
47#include <linux/kernel.h>
48#include <linux/types.h>
49#include <linux/skbuff.h>
50#include <linux/netdevice.h>
51#include <linux/ioport.h>
52#include <linux/delay.h>
53#include <linux/slab.h>
54#include <linux/init.h>
55#include <linux/rtnetlink.h>
56#include <linux/dma-mapping.h>
Jean Tourrilhesec4f32d2006-03-20 18:54:03 -080057#include <linux/pnp.h>
Dmitry Torokhov3b99b932006-03-20 18:59:05 -080058#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60#include <asm/io.h>
61#include <asm/dma.h>
62#include <asm/byteorder.h>
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#include <net/irda/wrapper.h>
65#include <net/irda/irda.h>
66#include <net/irda/irda_device.h>
67
68#include "nsc-ircc.h"
69
70#define CHIP_IO_EXTENT 8
71#define BROKEN_DONGLE_ID
72
73static char *driver_name = "nsc-ircc";
74
Dmitry Torokhov3b99b932006-03-20 18:59:05 -080075/* Power Management */
76#define NSC_IRCC_DRIVER_NAME "nsc-ircc"
77static int nsc_ircc_suspend(struct platform_device *dev, pm_message_t state);
78static int nsc_ircc_resume(struct platform_device *dev);
79
80static struct platform_driver nsc_ircc_driver = {
81 .suspend = nsc_ircc_suspend,
82 .resume = nsc_ircc_resume,
83 .driver = {
84 .name = NSC_IRCC_DRIVER_NAME,
85 },
86};
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088/* Module parameters */
89static int qos_mtt_bits = 0x07; /* 1 ms or more */
90static int dongle_id;
91
92/* Use BIOS settions by default, but user may supply module parameters */
93static unsigned int io[] = { ~0, ~0, ~0, ~0 };
Jean Tourrilhesec4f32d2006-03-20 18:54:03 -080094static unsigned int irq[] = { 0, 0, 0, 0 };
95static unsigned int dma[] = { 0, 0, 0, 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97static int nsc_ircc_probe_108(nsc_chip_t *chip, chipio_t *info);
98static int nsc_ircc_probe_338(nsc_chip_t *chip, chipio_t *info);
99static int nsc_ircc_probe_39x(nsc_chip_t *chip, chipio_t *info);
100static int nsc_ircc_init_108(nsc_chip_t *chip, chipio_t *info);
101static int nsc_ircc_init_338(nsc_chip_t *chip, chipio_t *info);
102static int nsc_ircc_init_39x(nsc_chip_t *chip, chipio_t *info);
Jean Tourrilhesec4f32d2006-03-20 18:54:03 -0800103static int nsc_ircc_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105/* These are the known NSC chips */
106static nsc_chip_t chips[] = {
107/* Name, {cfg registers}, chip id index reg, chip id expected value, revision mask */
108 { "PC87108", { 0x150, 0x398, 0xea }, 0x05, 0x10, 0xf0,
109 nsc_ircc_probe_108, nsc_ircc_init_108 },
110 { "PC87338", { 0x398, 0x15c, 0x2e }, 0x08, 0xb0, 0xf8,
111 nsc_ircc_probe_338, nsc_ircc_init_338 },
112 /* Contributed by Steffen Pingel - IBM X40 */
113 { "PC8738x", { 0x164e, 0x4e, 0x0 }, 0x20, 0xf4, 0xff,
114 nsc_ircc_probe_39x, nsc_ircc_init_39x },
115 /* Contributed by Jan Frey - IBM A30/A31 */
116 { "PC8739x", { 0x2e, 0x4e, 0x0 }, 0x20, 0xea, 0xff,
117 nsc_ircc_probe_39x, nsc_ircc_init_39x },
118 { NULL }
119};
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121static struct nsc_ircc_cb *dev_self[] = { NULL, NULL, NULL, NULL };
122
123static char *dongle_types[] = {
124 "Differential serial interface",
125 "Differential serial interface",
126 "Reserved",
127 "Reserved",
128 "Sharp RY5HD01",
129 "Reserved",
130 "Single-ended serial interface",
131 "Consumer-IR only",
132 "HP HSDL-2300, HP HSDL-3600/HSDL-3610",
133 "IBM31T1100 or Temic TFDS6000/TFDS6500",
134 "Reserved",
135 "Reserved",
136 "HP HSDL-1100/HSDL-2100",
137 "HP HSDL-1100/HSDL-2100",
138 "Supports SIR Mode only",
139 "No dongle connected",
140};
141
Jean Tourrilhesec4f32d2006-03-20 18:54:03 -0800142/* PNP probing */
143static chipio_t pnp_info;
144static const struct pnp_device_id nsc_ircc_pnp_table[] = {
145 { .id = "NSC6001", .driver_data = 0 },
146 { .id = "IBM0071", .driver_data = 0 },
147 { }
148};
149
150MODULE_DEVICE_TABLE(pnp, nsc_ircc_pnp_table);
151
152static struct pnp_driver nsc_ircc_pnp_driver = {
153 .name = "nsc-ircc",
154 .id_table = nsc_ircc_pnp_table,
155 .probe = nsc_ircc_pnp_probe,
156};
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158/* Some prototypes */
Jean Tourrilhesec4f32d2006-03-20 18:54:03 -0800159static int nsc_ircc_open(chipio_t *info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160static int nsc_ircc_close(struct nsc_ircc_cb *self);
161static int nsc_ircc_setup(chipio_t *info);
162static void nsc_ircc_pio_receive(struct nsc_ircc_cb *self);
163static int nsc_ircc_dma_receive(struct nsc_ircc_cb *self);
164static int nsc_ircc_dma_receive_complete(struct nsc_ircc_cb *self, int iobase);
165static int nsc_ircc_hard_xmit_sir(struct sk_buff *skb, struct net_device *dev);
166static int nsc_ircc_hard_xmit_fir(struct sk_buff *skb, struct net_device *dev);
167static int nsc_ircc_pio_write(int iobase, __u8 *buf, int len, int fifo_size);
168static void nsc_ircc_dma_xmit(struct nsc_ircc_cb *self, int iobase);
169static __u8 nsc_ircc_change_speed(struct nsc_ircc_cb *self, __u32 baud);
170static int nsc_ircc_is_receiving(struct nsc_ircc_cb *self);
171static int nsc_ircc_read_dongle_id (int iobase);
172static void nsc_ircc_init_dongle_interface (int iobase, int dongle_id);
173
174static int nsc_ircc_net_open(struct net_device *dev);
175static int nsc_ircc_net_close(struct net_device *dev);
176static int nsc_ircc_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
177static struct net_device_stats *nsc_ircc_net_get_stats(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Jean Tourrilhesec4f32d2006-03-20 18:54:03 -0800179/* Globals */
180static int pnp_registered;
181static int pnp_succeeded;
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183/*
184 * Function nsc_ircc_init ()
185 *
186 * Initialize chip. Just try to find out how many chips we are dealing with
187 * and where they are
188 */
189static int __init nsc_ircc_init(void)
190{
191 chipio_t info;
192 nsc_chip_t *chip;
Jean Tourrilhesec4f32d2006-03-20 18:54:03 -0800193 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 int cfg_base;
195 int cfg, id;
196 int reg;
197 int i = 0;
198
Dmitry Torokhov3b99b932006-03-20 18:59:05 -0800199 ret = platform_driver_register(&nsc_ircc_driver);
200 if (ret) {
201 IRDA_ERROR("%s, Can't register driver!\n", driver_name);
202 return ret;
203 }
204
Jean Tourrilhesec4f32d2006-03-20 18:54:03 -0800205 /* Register with PnP subsystem to detect disable ports */
206 ret = pnp_register_driver(&nsc_ircc_pnp_driver);
207
208 if (ret >= 0)
209 pnp_registered = 1;
210
211 ret = -ENODEV;
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 /* Probe for all the NSC chipsets we know about */
Jean Tourrilhesec4f32d2006-03-20 18:54:03 -0800214 for (chip = chips; chip->name ; chip++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 IRDA_DEBUG(2, "%s(), Probing for %s ...\n", __FUNCTION__,
216 chip->name);
217
218 /* Try all config registers for this chip */
Jean Tourrilhesec4f32d2006-03-20 18:54:03 -0800219 for (cfg = 0; cfg < ARRAY_SIZE(chip->cfg); cfg++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 cfg_base = chip->cfg[cfg];
221 if (!cfg_base)
222 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 /* Read index register */
225 reg = inb(cfg_base);
226 if (reg == 0xff) {
227 IRDA_DEBUG(2, "%s() no chip at 0x%03x\n", __FUNCTION__, cfg_base);
228 continue;
229 }
230
231 /* Read chip identification register */
232 outb(chip->cid_index, cfg_base);
233 id = inb(cfg_base+1);
234 if ((id & chip->cid_mask) == chip->cid_value) {
235 IRDA_DEBUG(2, "%s() Found %s chip, revision=%d\n",
236 __FUNCTION__, chip->name, id & ~chip->cid_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Jean Tourrilhesec4f32d2006-03-20 18:54:03 -0800238 /*
239 * If we found a correct PnP setting,
240 * we first try it.
241 */
242 if (pnp_succeeded) {
243 memset(&info, 0, sizeof(chipio_t));
244 info.cfg_base = cfg_base;
245 info.fir_base = pnp_info.fir_base;
246 info.dma = pnp_info.dma;
247 info.irq = pnp_info.irq;
248
249 if (info.fir_base < 0x2000) {
250 IRDA_MESSAGE("%s, chip->init\n", driver_name);
251 chip->init(chip, &info);
252 } else
253 chip->probe(chip, &info);
254
255 if (nsc_ircc_open(&info) >= 0)
256 ret = 0;
257 }
258
259 /*
260 * Opening based on PnP values failed.
261 * Let's fallback to user values, or probe
262 * the chip.
263 */
264 if (ret) {
265 IRDA_DEBUG(2, "%s, PnP init failed\n", driver_name);
266 memset(&info, 0, sizeof(chipio_t));
267 info.cfg_base = cfg_base;
268 info.fir_base = io[i];
269 info.dma = dma[i];
270 info.irq = irq[i];
271
272 /*
273 * If the user supplies the base address, then
274 * we init the chip, if not we probe the values
275 * set by the BIOS
276 */
277 if (io[i] < 0x2000) {
278 chip->init(chip, &info);
279 } else
280 chip->probe(chip, &info);
281
282 if (nsc_ircc_open(&info) >= 0)
283 ret = 0;
284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 i++;
286 } else {
287 IRDA_DEBUG(2, "%s(), Wrong chip id=0x%02x\n", __FUNCTION__, id);
288 }
289 }
Jean Tourrilhesec4f32d2006-03-20 18:54:03 -0800290 }
291
292 if (ret) {
Dmitry Torokhov3b99b932006-03-20 18:59:05 -0800293 platform_driver_unregister(&nsc_ircc_driver);
Jean Tourrilhesec4f32d2006-03-20 18:54:03 -0800294 pnp_unregister_driver(&nsc_ircc_pnp_driver);
295 pnp_registered = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 }
297
298 return ret;
299}
300
301/*
302 * Function nsc_ircc_cleanup ()
303 *
304 * Close all configured chips
305 *
306 */
307static void __exit nsc_ircc_cleanup(void)
308{
309 int i;
310
Jean Tourrilhesec4f32d2006-03-20 18:54:03 -0800311 for (i = 0; i < ARRAY_SIZE(dev_self); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 if (dev_self[i])
313 nsc_ircc_close(dev_self[i]);
314 }
Jean Tourrilhesec4f32d2006-03-20 18:54:03 -0800315
Dmitry Torokhov3b99b932006-03-20 18:59:05 -0800316 platform_driver_unregister(&nsc_ircc_driver);
317
Jean Tourrilhesec4f32d2006-03-20 18:54:03 -0800318 if (pnp_registered)
319 pnp_unregister_driver(&nsc_ircc_pnp_driver);
320
321 pnp_registered = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
323
324/*
325 * Function nsc_ircc_open (iobase, irq)
326 *
327 * Open driver instance
328 *
329 */
Jean Tourrilhesec4f32d2006-03-20 18:54:03 -0800330static int __init nsc_ircc_open(chipio_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
332 struct net_device *dev;
333 struct nsc_ircc_cb *self;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 void *ret;
Jean Tourrilhesec4f32d2006-03-20 18:54:03 -0800335 int err, chip_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
338
Jean Tourrilhesec4f32d2006-03-20 18:54:03 -0800339
340 for (chip_index = 0; chip_index < ARRAY_SIZE(dev_self); chip_index++) {
341 if (!dev_self[chip_index])
342 break;
343 }
344
345 if (chip_index == ARRAY_SIZE(dev_self)) {
346 IRDA_ERROR("%s(), maximum number of supported chips reached!\n", __FUNCTION__);
347 return -ENOMEM;
348 }
349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 IRDA_MESSAGE("%s, Found chip at base=0x%03x\n", driver_name,
351 info->cfg_base);
352
353 if ((nsc_ircc_setup(info)) == -1)
354 return -1;
355
356 IRDA_MESSAGE("%s, driver loaded (Dag Brattli)\n", driver_name);
357
358 dev = alloc_irdadev(sizeof(struct nsc_ircc_cb));
359 if (dev == NULL) {
360 IRDA_ERROR("%s(), can't allocate memory for "
361 "control block!\n", __FUNCTION__);
362 return -ENOMEM;
363 }
364
365 self = dev->priv;
366 self->netdev = dev;
367 spin_lock_init(&self->lock);
368
369 /* Need to store self somewhere */
Jean Tourrilhesec4f32d2006-03-20 18:54:03 -0800370 dev_self[chip_index] = self;
371 self->index = chip_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 /* Initialize IO */
374 self->io.cfg_base = info->cfg_base;
375 self->io.fir_base = info->fir_base;
376 self->io.irq = info->irq;
377 self->io.fir_ext = CHIP_IO_EXTENT;
378 self->io.dma = info->dma;
379 self->io.fifo_size = 32;
380
381 /* Reserve the ioports that we need */
382 ret = request_region(self->io.fir_base, self->io.fir_ext, driver_name);
383 if (!ret) {
384 IRDA_WARNING("%s(), can't get iobase of 0x%03x\n",
385 __FUNCTION__, self->io.fir_base);
386 err = -ENODEV;
387 goto out1;
388 }
389
390 /* Initialize QoS for this device */
391 irda_init_max_qos_capabilies(&self->qos);
392
393 /* The only value we must override it the baudrate */
394 self->qos.baud_rate.bits = IR_9600|IR_19200|IR_38400|IR_57600|
395 IR_115200|IR_576000|IR_1152000 |(IR_4000000 << 8);
396
397 self->qos.min_turn_time.bits = qos_mtt_bits;
398 irda_qos_bits_to_value(&self->qos);
399
400 /* Max DMA buffer size needed = (data_size + 6) * (window_size) + 6; */
401 self->rx_buff.truesize = 14384;
402 self->tx_buff.truesize = 14384;
403
404 /* Allocate memory if needed */
405 self->rx_buff.head =
406 dma_alloc_coherent(NULL, self->rx_buff.truesize,
407 &self->rx_buff_dma, GFP_KERNEL);
408 if (self->rx_buff.head == NULL) {
409 err = -ENOMEM;
410 goto out2;
411
412 }
413 memset(self->rx_buff.head, 0, self->rx_buff.truesize);
414
415 self->tx_buff.head =
416 dma_alloc_coherent(NULL, self->tx_buff.truesize,
417 &self->tx_buff_dma, GFP_KERNEL);
418 if (self->tx_buff.head == NULL) {
419 err = -ENOMEM;
420 goto out3;
421 }
422 memset(self->tx_buff.head, 0, self->tx_buff.truesize);
423
424 self->rx_buff.in_frame = FALSE;
425 self->rx_buff.state = OUTSIDE_FRAME;
426 self->tx_buff.data = self->tx_buff.head;
427 self->rx_buff.data = self->rx_buff.head;
428
429 /* Reset Tx queue info */
430 self->tx_fifo.len = self->tx_fifo.ptr = self->tx_fifo.free = 0;
431 self->tx_fifo.tail = self->tx_buff.head;
432
433 /* Override the network functions we need to use */
434 SET_MODULE_OWNER(dev);
435 dev->hard_start_xmit = nsc_ircc_hard_xmit_sir;
436 dev->open = nsc_ircc_net_open;
437 dev->stop = nsc_ircc_net_close;
438 dev->do_ioctl = nsc_ircc_net_ioctl;
439 dev->get_stats = nsc_ircc_net_get_stats;
440
441 err = register_netdev(dev);
442 if (err) {
443 IRDA_ERROR("%s(), register_netdev() failed!\n", __FUNCTION__);
444 goto out4;
445 }
446 IRDA_MESSAGE("IrDA: Registered device %s\n", dev->name);
447
448 /* Check if user has supplied a valid dongle id or not */
449 if ((dongle_id <= 0) ||
Jean Tourrilhesec4f32d2006-03-20 18:54:03 -0800450 (dongle_id >= ARRAY_SIZE(dongle_types))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 dongle_id = nsc_ircc_read_dongle_id(self->io.fir_base);
452
453 IRDA_MESSAGE("%s, Found dongle: %s\n", driver_name,
454 dongle_types[dongle_id]);
455 } else {
456 IRDA_MESSAGE("%s, Using dongle: %s\n", driver_name,
457 dongle_types[dongle_id]);
458 }
459
460 self->io.dongle_id = dongle_id;
461 nsc_ircc_init_dongle_interface(self->io.fir_base, dongle_id);
462
Dmitry Torokhov3b99b932006-03-20 18:59:05 -0800463 self->pldev = platform_device_register_simple(NSC_IRCC_DRIVER_NAME,
464 self->index, NULL, 0);
465 if (IS_ERR(self->pldev)) {
466 err = PTR_ERR(self->pldev);
467 goto out5;
468 }
469 platform_set_drvdata(self->pldev, self);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Jean Tourrilhesec4f32d2006-03-20 18:54:03 -0800471 return chip_index;
Dmitry Torokhov3b99b932006-03-20 18:59:05 -0800472
473 out5:
474 unregister_netdev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 out4:
476 dma_free_coherent(NULL, self->tx_buff.truesize,
477 self->tx_buff.head, self->tx_buff_dma);
478 out3:
479 dma_free_coherent(NULL, self->rx_buff.truesize,
480 self->rx_buff.head, self->rx_buff_dma);
481 out2:
482 release_region(self->io.fir_base, self->io.fir_ext);
483 out1:
484 free_netdev(dev);
Jean Tourrilhesec4f32d2006-03-20 18:54:03 -0800485 dev_self[chip_index] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 return err;
487}
488
489/*
490 * Function nsc_ircc_close (self)
491 *
492 * Close driver instance
493 *
494 */
495static int __exit nsc_ircc_close(struct nsc_ircc_cb *self)
496{
497 int iobase;
498
499 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
500
501 IRDA_ASSERT(self != NULL, return -1;);
502
503 iobase = self->io.fir_base;
504
Dmitry Torokhov3b99b932006-03-20 18:59:05 -0800505 platform_device_unregister(self->pldev);
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 /* Remove netdevice */
508 unregister_netdev(self->netdev);
509
510 /* Release the PORT that this driver is using */
511 IRDA_DEBUG(4, "%s(), Releasing Region %03x\n",
512 __FUNCTION__, self->io.fir_base);
513 release_region(self->io.fir_base, self->io.fir_ext);
514
515 if (self->tx_buff.head)
516 dma_free_coherent(NULL, self->tx_buff.truesize,
517 self->tx_buff.head, self->tx_buff_dma);
518
519 if (self->rx_buff.head)
520 dma_free_coherent(NULL, self->rx_buff.truesize,
521 self->rx_buff.head, self->rx_buff_dma);
522
523 dev_self[self->index] = NULL;
524 free_netdev(self->netdev);
525
526 return 0;
527}
528
529/*
530 * Function nsc_ircc_init_108 (iobase, cfg_base, irq, dma)
531 *
532 * Initialize the NSC '108 chip
533 *
534 */
535static int nsc_ircc_init_108(nsc_chip_t *chip, chipio_t *info)
536{
537 int cfg_base = info->cfg_base;
538 __u8 temp=0;
539
540 outb(2, cfg_base); /* Mode Control Register (MCTL) */
541 outb(0x00, cfg_base+1); /* Disable device */
542
543 /* Base Address and Interrupt Control Register (BAIC) */
544 outb(CFG_108_BAIC, cfg_base);
545 switch (info->fir_base) {
546 case 0x3e8: outb(0x14, cfg_base+1); break;
547 case 0x2e8: outb(0x15, cfg_base+1); break;
548 case 0x3f8: outb(0x16, cfg_base+1); break;
549 case 0x2f8: outb(0x17, cfg_base+1); break;
550 default: IRDA_ERROR("%s(), invalid base_address", __FUNCTION__);
551 }
552
553 /* Control Signal Routing Register (CSRT) */
554 switch (info->irq) {
555 case 3: temp = 0x01; break;
556 case 4: temp = 0x02; break;
557 case 5: temp = 0x03; break;
558 case 7: temp = 0x04; break;
559 case 9: temp = 0x05; break;
560 case 11: temp = 0x06; break;
561 case 15: temp = 0x07; break;
562 default: IRDA_ERROR("%s(), invalid irq", __FUNCTION__);
563 }
564 outb(CFG_108_CSRT, cfg_base);
565
566 switch (info->dma) {
567 case 0: outb(0x08+temp, cfg_base+1); break;
568 case 1: outb(0x10+temp, cfg_base+1); break;
569 case 3: outb(0x18+temp, cfg_base+1); break;
570 default: IRDA_ERROR("%s(), invalid dma", __FUNCTION__);
571 }
572
573 outb(CFG_108_MCTL, cfg_base); /* Mode Control Register (MCTL) */
574 outb(0x03, cfg_base+1); /* Enable device */
575
576 return 0;
577}
578
579/*
580 * Function nsc_ircc_probe_108 (chip, info)
581 *
582 *
583 *
584 */
585static int nsc_ircc_probe_108(nsc_chip_t *chip, chipio_t *info)
586{
587 int cfg_base = info->cfg_base;
588 int reg;
589
590 /* Read address and interrupt control register (BAIC) */
591 outb(CFG_108_BAIC, cfg_base);
592 reg = inb(cfg_base+1);
593
594 switch (reg & 0x03) {
595 case 0:
596 info->fir_base = 0x3e8;
597 break;
598 case 1:
599 info->fir_base = 0x2e8;
600 break;
601 case 2:
602 info->fir_base = 0x3f8;
603 break;
604 case 3:
605 info->fir_base = 0x2f8;
606 break;
607 }
608 info->sir_base = info->fir_base;
609 IRDA_DEBUG(2, "%s(), probing fir_base=0x%03x\n", __FUNCTION__,
610 info->fir_base);
611
612 /* Read control signals routing register (CSRT) */
613 outb(CFG_108_CSRT, cfg_base);
614 reg = inb(cfg_base+1);
615
616 switch (reg & 0x07) {
617 case 0:
618 info->irq = -1;
619 break;
620 case 1:
621 info->irq = 3;
622 break;
623 case 2:
624 info->irq = 4;
625 break;
626 case 3:
627 info->irq = 5;
628 break;
629 case 4:
630 info->irq = 7;
631 break;
632 case 5:
633 info->irq = 9;
634 break;
635 case 6:
636 info->irq = 11;
637 break;
638 case 7:
639 info->irq = 15;
640 break;
641 }
642 IRDA_DEBUG(2, "%s(), probing irq=%d\n", __FUNCTION__, info->irq);
643
644 /* Currently we only read Rx DMA but it will also be used for Tx */
645 switch ((reg >> 3) & 0x03) {
646 case 0:
647 info->dma = -1;
648 break;
649 case 1:
650 info->dma = 0;
651 break;
652 case 2:
653 info->dma = 1;
654 break;
655 case 3:
656 info->dma = 3;
657 break;
658 }
659 IRDA_DEBUG(2, "%s(), probing dma=%d\n", __FUNCTION__, info->dma);
660
661 /* Read mode control register (MCTL) */
662 outb(CFG_108_MCTL, cfg_base);
663 reg = inb(cfg_base+1);
664
665 info->enabled = reg & 0x01;
666 info->suspended = !((reg >> 1) & 0x01);
667
668 return 0;
669}
670
671/*
672 * Function nsc_ircc_init_338 (chip, info)
673 *
674 * Initialize the NSC '338 chip. Remember that the 87338 needs two
675 * consecutive writes to the data registers while CPU interrupts are
676 * disabled. The 97338 does not require this, but shouldn't be any
677 * harm if we do it anyway.
678 */
679static int nsc_ircc_init_338(nsc_chip_t *chip, chipio_t *info)
680{
681 /* No init yet */
682
683 return 0;
684}
685
686/*
687 * Function nsc_ircc_probe_338 (chip, info)
688 *
689 *
690 *
691 */
692static int nsc_ircc_probe_338(nsc_chip_t *chip, chipio_t *info)
693{
694 int cfg_base = info->cfg_base;
695 int reg, com = 0;
696 int pnp;
697
698 /* Read funtion enable register (FER) */
699 outb(CFG_338_FER, cfg_base);
700 reg = inb(cfg_base+1);
701
702 info->enabled = (reg >> 2) & 0x01;
703
704 /* Check if we are in Legacy or PnP mode */
705 outb(CFG_338_PNP0, cfg_base);
706 reg = inb(cfg_base+1);
707
708 pnp = (reg >> 3) & 0x01;
709 if (pnp) {
710 IRDA_DEBUG(2, "(), Chip is in PnP mode\n");
711 outb(0x46, cfg_base);
712 reg = (inb(cfg_base+1) & 0xfe) << 2;
713
714 outb(0x47, cfg_base);
715 reg |= ((inb(cfg_base+1) & 0xfc) << 8);
716
717 info->fir_base = reg;
718 } else {
719 /* Read function address register (FAR) */
720 outb(CFG_338_FAR, cfg_base);
721 reg = inb(cfg_base+1);
722
723 switch ((reg >> 4) & 0x03) {
724 case 0:
725 info->fir_base = 0x3f8;
726 break;
727 case 1:
728 info->fir_base = 0x2f8;
729 break;
730 case 2:
731 com = 3;
732 break;
733 case 3:
734 com = 4;
735 break;
736 }
737
738 if (com) {
739 switch ((reg >> 6) & 0x03) {
740 case 0:
741 if (com == 3)
742 info->fir_base = 0x3e8;
743 else
744 info->fir_base = 0x2e8;
745 break;
746 case 1:
747 if (com == 3)
748 info->fir_base = 0x338;
749 else
750 info->fir_base = 0x238;
751 break;
752 case 2:
753 if (com == 3)
754 info->fir_base = 0x2e8;
755 else
756 info->fir_base = 0x2e0;
757 break;
758 case 3:
759 if (com == 3)
760 info->fir_base = 0x220;
761 else
762 info->fir_base = 0x228;
763 break;
764 }
765 }
766 }
767 info->sir_base = info->fir_base;
768
769 /* Read PnP register 1 (PNP1) */
770 outb(CFG_338_PNP1, cfg_base);
771 reg = inb(cfg_base+1);
772
773 info->irq = reg >> 4;
774
775 /* Read PnP register 3 (PNP3) */
776 outb(CFG_338_PNP3, cfg_base);
777 reg = inb(cfg_base+1);
778
779 info->dma = (reg & 0x07) - 1;
780
781 /* Read power and test register (PTR) */
782 outb(CFG_338_PTR, cfg_base);
783 reg = inb(cfg_base+1);
784
785 info->suspended = reg & 0x01;
786
787 return 0;
788}
789
790
791/*
792 * Function nsc_ircc_init_39x (chip, info)
793 *
794 * Now that we know it's a '39x (see probe below), we need to
795 * configure it so we can use it.
796 *
797 * The NSC '338 chip is a Super I/O chip with a "bank" architecture,
798 * the configuration of the different functionality (serial, parallel,
799 * floppy...) are each in a different bank (Logical Device Number).
800 * The base address, irq and dma configuration registers are common
801 * to all functionalities (index 0x30 to 0x7F).
802 * There is only one configuration register specific to the
803 * serial port, CFG_39X_SPC.
804 * JeanII
805 *
806 * Note : this code was written by Jan Frey <janfrey@web.de>
807 */
808static int nsc_ircc_init_39x(nsc_chip_t *chip, chipio_t *info)
809{
810 int cfg_base = info->cfg_base;
811 int enabled;
812
813 /* User is shure about his config... accept it. */
814 IRDA_DEBUG(2, "%s(): nsc_ircc_init_39x (user settings): "
815 "io=0x%04x, irq=%d, dma=%d\n",
816 __FUNCTION__, info->fir_base, info->irq, info->dma);
817
818 /* Access bank for SP2 */
819 outb(CFG_39X_LDN, cfg_base);
820 outb(0x02, cfg_base+1);
821
822 /* Configure SP2 */
823
824 /* We want to enable the device if not enabled */
825 outb(CFG_39X_ACT, cfg_base);
826 enabled = inb(cfg_base+1) & 0x01;
827
828 if (!enabled) {
829 /* Enable the device */
830 outb(CFG_39X_SIOCF1, cfg_base);
831 outb(0x01, cfg_base+1);
832 /* May want to update info->enabled. Jean II */
833 }
834
835 /* Enable UART bank switching (bit 7) ; Sets the chip to normal
836 * power mode (wake up from sleep mode) (bit 1) */
837 outb(CFG_39X_SPC, cfg_base);
838 outb(0x82, cfg_base+1);
839
840 return 0;
841}
842
843/*
844 * Function nsc_ircc_probe_39x (chip, info)
845 *
846 * Test if we really have a '39x chip at the given address
847 *
848 * Note : this code was written by Jan Frey <janfrey@web.de>
849 */
850static int nsc_ircc_probe_39x(nsc_chip_t *chip, chipio_t *info)
851{
852 int cfg_base = info->cfg_base;
853 int reg1, reg2, irq, irqt, dma1, dma2;
854 int enabled, susp;
855
856 IRDA_DEBUG(2, "%s(), nsc_ircc_probe_39x, base=%d\n",
857 __FUNCTION__, cfg_base);
858
859 /* This function should be executed with irq off to avoid
860 * another driver messing with the Super I/O bank - Jean II */
861
862 /* Access bank for SP2 */
863 outb(CFG_39X_LDN, cfg_base);
864 outb(0x02, cfg_base+1);
865
866 /* Read infos about SP2 ; store in info struct */
867 outb(CFG_39X_BASEH, cfg_base);
868 reg1 = inb(cfg_base+1);
869 outb(CFG_39X_BASEL, cfg_base);
870 reg2 = inb(cfg_base+1);
871 info->fir_base = (reg1 << 8) | reg2;
872
873 outb(CFG_39X_IRQNUM, cfg_base);
874 irq = inb(cfg_base+1);
875 outb(CFG_39X_IRQSEL, cfg_base);
876 irqt = inb(cfg_base+1);
877 info->irq = irq;
878
879 outb(CFG_39X_DMA0, cfg_base);
880 dma1 = inb(cfg_base+1);
881 outb(CFG_39X_DMA1, cfg_base);
882 dma2 = inb(cfg_base+1);
883 info->dma = dma1 -1;
884
885 outb(CFG_39X_ACT, cfg_base);
886 info->enabled = enabled = inb(cfg_base+1) & 0x01;
887
888 outb(CFG_39X_SPC, cfg_base);
889 susp = 1 - ((inb(cfg_base+1) & 0x02) >> 1);
890
891 IRDA_DEBUG(2, "%s(): io=0x%02x%02x, irq=%d (type %d), rxdma=%d, txdma=%d, enabled=%d (suspended=%d)\n", __FUNCTION__, reg1,reg2,irq,irqt,dma1,dma2,enabled,susp);
892
893 /* Configure SP2 */
894
895 /* We want to enable the device if not enabled */
896 outb(CFG_39X_ACT, cfg_base);
897 enabled = inb(cfg_base+1) & 0x01;
898
899 if (!enabled) {
900 /* Enable the device */
901 outb(CFG_39X_SIOCF1, cfg_base);
902 outb(0x01, cfg_base+1);
903 /* May want to update info->enabled. Jean II */
904 }
905
906 /* Enable UART bank switching (bit 7) ; Sets the chip to normal
907 * power mode (wake up from sleep mode) (bit 1) */
908 outb(CFG_39X_SPC, cfg_base);
909 outb(0x82, cfg_base+1);
910
911 return 0;
912}
913
Jean Tourrilhesec4f32d2006-03-20 18:54:03 -0800914/* PNP probing */
915static int nsc_ircc_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *id)
916{
917 memset(&pnp_info, 0, sizeof(chipio_t));
918 pnp_info.irq = -1;
919 pnp_info.dma = -1;
920 pnp_succeeded = 1;
921
922 /* There don't seem to be any way to get the cfg_base.
923 * On my box, cfg_base is in the PnP descriptor of the
924 * motherboard. Oh well... Jean II */
925
926 if (pnp_port_valid(dev, 0) &&
927 !(pnp_port_flags(dev, 0) & IORESOURCE_DISABLED))
928 pnp_info.fir_base = pnp_port_start(dev, 0);
929
930 if (pnp_irq_valid(dev, 0) &&
931 !(pnp_irq_flags(dev, 0) & IORESOURCE_DISABLED))
932 pnp_info.irq = pnp_irq(dev, 0);
933
934 if (pnp_dma_valid(dev, 0) &&
935 !(pnp_dma_flags(dev, 0) & IORESOURCE_DISABLED))
936 pnp_info.dma = pnp_dma(dev, 0);
937
938 IRDA_DEBUG(0, "%s() : From PnP, found firbase 0x%03X ; irq %d ; dma %d.\n",
939 __FUNCTION__, pnp_info.fir_base, pnp_info.irq, pnp_info.dma);
940
941 if((pnp_info.fir_base == 0) ||
942 (pnp_info.irq == -1) || (pnp_info.dma == -1)) {
943 /* Returning an error will disable the device. Yuck ! */
944 //return -EINVAL;
945 pnp_succeeded = 0;
946 }
947
948 return 0;
949}
950
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951/*
952 * Function nsc_ircc_setup (info)
953 *
954 * Returns non-negative on success.
955 *
956 */
957static int nsc_ircc_setup(chipio_t *info)
958{
959 int version;
960 int iobase = info->fir_base;
961
962 /* Read the Module ID */
963 switch_bank(iobase, BANK3);
964 version = inb(iobase+MID);
965
966 IRDA_DEBUG(2, "%s() Driver %s Found chip version %02x\n",
967 __FUNCTION__, driver_name, version);
968
969 /* Should be 0x2? */
970 if (0x20 != (version & 0xf0)) {
971 IRDA_ERROR("%s, Wrong chip version %02x\n",
972 driver_name, version);
973 return -1;
974 }
975
976 /* Switch to advanced mode */
977 switch_bank(iobase, BANK2);
978 outb(ECR1_EXT_SL, iobase+ECR1);
979 switch_bank(iobase, BANK0);
980
981 /* Set FIFO threshold to TX17, RX16, reset and enable FIFO's */
982 switch_bank(iobase, BANK0);
983 outb(FCR_RXTH|FCR_TXTH|FCR_TXSR|FCR_RXSR|FCR_FIFO_EN, iobase+FCR);
984
985 outb(0x03, iobase+LCR); /* 8 bit word length */
986 outb(MCR_SIR, iobase+MCR); /* Start at SIR-mode, also clears LSR*/
987
988 /* Set FIFO size to 32 */
989 switch_bank(iobase, BANK2);
990 outb(EXCR2_RFSIZ|EXCR2_TFSIZ, iobase+EXCR2);
991
992 /* IRCR2: FEND_MD is not set */
993 switch_bank(iobase, BANK5);
994 outb(0x02, iobase+4);
995
996 /* Make sure that some defaults are OK */
997 switch_bank(iobase, BANK6);
998 outb(0x20, iobase+0); /* Set 32 bits FIR CRC */
999 outb(0x0a, iobase+1); /* Set MIR pulse width */
1000 outb(0x0d, iobase+2); /* Set SIR pulse width to 1.6us */
1001 outb(0x2a, iobase+4); /* Set beginning frag, and preamble length */
1002
1003 /* Enable receive interrupts */
1004 switch_bank(iobase, BANK0);
1005 outb(IER_RXHDL_IE, iobase+IER);
1006
1007 return 0;
1008}
1009
1010/*
1011 * Function nsc_ircc_read_dongle_id (void)
1012 *
1013 * Try to read dongle indentification. This procedure needs to be executed
1014 * once after power-on/reset. It also needs to be used whenever you suspect
1015 * that the user may have plugged/unplugged the IrDA Dongle.
1016 */
1017static int nsc_ircc_read_dongle_id (int iobase)
1018{
1019 int dongle_id;
1020 __u8 bank;
1021
1022 bank = inb(iobase+BSR);
1023
1024 /* Select Bank 7 */
1025 switch_bank(iobase, BANK7);
1026
1027 /* IRCFG4: IRSL0_DS and IRSL21_DS are cleared */
1028 outb(0x00, iobase+7);
1029
1030 /* ID0, 1, and 2 are pulled up/down very slowly */
1031 udelay(50);
1032
1033 /* IRCFG1: read the ID bits */
1034 dongle_id = inb(iobase+4) & 0x0f;
1035
1036#ifdef BROKEN_DONGLE_ID
1037 if (dongle_id == 0x0a)
1038 dongle_id = 0x09;
1039#endif
1040 /* Go back to bank 0 before returning */
1041 switch_bank(iobase, BANK0);
1042
1043 outb(bank, iobase+BSR);
1044
1045 return dongle_id;
1046}
1047
1048/*
1049 * Function nsc_ircc_init_dongle_interface (iobase, dongle_id)
1050 *
1051 * This function initializes the dongle for the transceiver that is
1052 * used. This procedure needs to be executed once after
1053 * power-on/reset. It also needs to be used whenever you suspect that
1054 * the dongle is changed.
1055 */
1056static void nsc_ircc_init_dongle_interface (int iobase, int dongle_id)
1057{
1058 int bank;
1059
1060 /* Save current bank */
1061 bank = inb(iobase+BSR);
1062
1063 /* Select Bank 7 */
1064 switch_bank(iobase, BANK7);
1065
1066 /* IRCFG4: set according to dongle_id */
1067 switch (dongle_id) {
1068 case 0x00: /* same as */
1069 case 0x01: /* Differential serial interface */
1070 IRDA_DEBUG(0, "%s(), %s not defined by irda yet\n",
1071 __FUNCTION__, dongle_types[dongle_id]);
1072 break;
1073 case 0x02: /* same as */
1074 case 0x03: /* Reserved */
1075 IRDA_DEBUG(0, "%s(), %s not defined by irda yet\n",
1076 __FUNCTION__, dongle_types[dongle_id]);
1077 break;
1078 case 0x04: /* Sharp RY5HD01 */
1079 break;
1080 case 0x05: /* Reserved, but this is what the Thinkpad reports */
1081 IRDA_DEBUG(0, "%s(), %s not defined by irda yet\n",
1082 __FUNCTION__, dongle_types[dongle_id]);
1083 break;
1084 case 0x06: /* Single-ended serial interface */
1085 IRDA_DEBUG(0, "%s(), %s not defined by irda yet\n",
1086 __FUNCTION__, dongle_types[dongle_id]);
1087 break;
1088 case 0x07: /* Consumer-IR only */
1089 IRDA_DEBUG(0, "%s(), %s is not for IrDA mode\n",
1090 __FUNCTION__, dongle_types[dongle_id]);
1091 break;
1092 case 0x08: /* HP HSDL-2300, HP HSDL-3600/HSDL-3610 */
1093 IRDA_DEBUG(0, "%s(), %s\n",
1094 __FUNCTION__, dongle_types[dongle_id]);
1095 break;
1096 case 0x09: /* IBM31T1100 or Temic TFDS6000/TFDS6500 */
1097 outb(0x28, iobase+7); /* Set irsl[0-2] as output */
1098 break;
1099 case 0x0A: /* same as */
1100 case 0x0B: /* Reserved */
1101 IRDA_DEBUG(0, "%s(), %s not defined by irda yet\n",
1102 __FUNCTION__, dongle_types[dongle_id]);
1103 break;
1104 case 0x0C: /* same as */
1105 case 0x0D: /* HP HSDL-1100/HSDL-2100 */
1106 /*
1107 * Set irsl0 as input, irsl[1-2] as output, and separate
1108 * inputs are used for SIR and MIR/FIR
1109 */
1110 outb(0x48, iobase+7);
1111 break;
1112 case 0x0E: /* Supports SIR Mode only */
1113 outb(0x28, iobase+7); /* Set irsl[0-2] as output */
1114 break;
1115 case 0x0F: /* No dongle connected */
1116 IRDA_DEBUG(0, "%s(), %s\n",
1117 __FUNCTION__, dongle_types[dongle_id]);
1118
1119 switch_bank(iobase, BANK0);
1120 outb(0x62, iobase+MCR);
1121 break;
1122 default:
1123 IRDA_DEBUG(0, "%s(), invalid dongle_id %#x",
1124 __FUNCTION__, dongle_id);
1125 }
1126
1127 /* IRCFG1: IRSL1 and 2 are set to IrDA mode */
1128 outb(0x00, iobase+4);
1129
1130 /* Restore bank register */
1131 outb(bank, iobase+BSR);
1132
1133} /* set_up_dongle_interface */
1134
1135/*
1136 * Function nsc_ircc_change_dongle_speed (iobase, speed, dongle_id)
1137 *
1138 * Change speed of the attach dongle
1139 *
1140 */
1141static void nsc_ircc_change_dongle_speed(int iobase, int speed, int dongle_id)
1142{
1143 __u8 bank;
1144
1145 /* Save current bank */
1146 bank = inb(iobase+BSR);
1147
1148 /* Select Bank 7 */
1149 switch_bank(iobase, BANK7);
1150
1151 /* IRCFG1: set according to dongle_id */
1152 switch (dongle_id) {
1153 case 0x00: /* same as */
1154 case 0x01: /* Differential serial interface */
1155 IRDA_DEBUG(0, "%s(), %s not defined by irda yet\n",
1156 __FUNCTION__, dongle_types[dongle_id]);
1157 break;
1158 case 0x02: /* same as */
1159 case 0x03: /* Reserved */
1160 IRDA_DEBUG(0, "%s(), %s not defined by irda yet\n",
1161 __FUNCTION__, dongle_types[dongle_id]);
1162 break;
1163 case 0x04: /* Sharp RY5HD01 */
1164 break;
1165 case 0x05: /* Reserved */
1166 IRDA_DEBUG(0, "%s(), %s not defined by irda yet\n",
1167 __FUNCTION__, dongle_types[dongle_id]);
1168 break;
1169 case 0x06: /* Single-ended serial interface */
1170 IRDA_DEBUG(0, "%s(), %s not defined by irda yet\n",
1171 __FUNCTION__, dongle_types[dongle_id]);
1172 break;
1173 case 0x07: /* Consumer-IR only */
1174 IRDA_DEBUG(0, "%s(), %s is not for IrDA mode\n",
1175 __FUNCTION__, dongle_types[dongle_id]);
1176 break;
1177 case 0x08: /* HP HSDL-2300, HP HSDL-3600/HSDL-3610 */
1178 IRDA_DEBUG(0, "%s(), %s\n",
1179 __FUNCTION__, dongle_types[dongle_id]);
1180 outb(0x00, iobase+4);
1181 if (speed > 115200)
1182 outb(0x01, iobase+4);
1183 break;
1184 case 0x09: /* IBM31T1100 or Temic TFDS6000/TFDS6500 */
1185 outb(0x01, iobase+4);
1186
1187 if (speed == 4000000) {
1188 /* There was a cli() there, but we now are already
1189 * under spin_lock_irqsave() - JeanII */
1190 outb(0x81, iobase+4);
1191 outb(0x80, iobase+4);
1192 } else
1193 outb(0x00, iobase+4);
1194 break;
1195 case 0x0A: /* same as */
1196 case 0x0B: /* Reserved */
1197 IRDA_DEBUG(0, "%s(), %s not defined by irda yet\n",
1198 __FUNCTION__, dongle_types[dongle_id]);
1199 break;
1200 case 0x0C: /* same as */
1201 case 0x0D: /* HP HSDL-1100/HSDL-2100 */
1202 break;
1203 case 0x0E: /* Supports SIR Mode only */
1204 break;
1205 case 0x0F: /* No dongle connected */
1206 IRDA_DEBUG(0, "%s(), %s is not for IrDA mode\n",
1207 __FUNCTION__, dongle_types[dongle_id]);
1208
1209 switch_bank(iobase, BANK0);
1210 outb(0x62, iobase+MCR);
1211 break;
1212 default:
1213 IRDA_DEBUG(0, "%s(), invalid data_rate\n", __FUNCTION__);
1214 }
1215 /* Restore bank register */
1216 outb(bank, iobase+BSR);
1217}
1218
1219/*
1220 * Function nsc_ircc_change_speed (self, baud)
1221 *
1222 * Change the speed of the device
1223 *
1224 * This function *must* be called with irq off and spin-lock.
1225 */
1226static __u8 nsc_ircc_change_speed(struct nsc_ircc_cb *self, __u32 speed)
1227{
1228 struct net_device *dev = self->netdev;
1229 __u8 mcr = MCR_SIR;
1230 int iobase;
1231 __u8 bank;
1232 __u8 ier; /* Interrupt enable register */
1233
1234 IRDA_DEBUG(2, "%s(), speed=%d\n", __FUNCTION__, speed);
1235
1236 IRDA_ASSERT(self != NULL, return 0;);
1237
1238 iobase = self->io.fir_base;
1239
1240 /* Update accounting for new speed */
1241 self->io.speed = speed;
1242
1243 /* Save current bank */
1244 bank = inb(iobase+BSR);
1245
1246 /* Disable interrupts */
1247 switch_bank(iobase, BANK0);
1248 outb(0, iobase+IER);
1249
1250 /* Select Bank 2 */
1251 switch_bank(iobase, BANK2);
1252
1253 outb(0x00, iobase+BGDH);
1254 switch (speed) {
1255 case 9600: outb(0x0c, iobase+BGDL); break;
1256 case 19200: outb(0x06, iobase+BGDL); break;
1257 case 38400: outb(0x03, iobase+BGDL); break;
1258 case 57600: outb(0x02, iobase+BGDL); break;
1259 case 115200: outb(0x01, iobase+BGDL); break;
1260 case 576000:
1261 switch_bank(iobase, BANK5);
1262
1263 /* IRCR2: MDRS is set */
1264 outb(inb(iobase+4) | 0x04, iobase+4);
1265
1266 mcr = MCR_MIR;
1267 IRDA_DEBUG(0, "%s(), handling baud of 576000\n", __FUNCTION__);
1268 break;
1269 case 1152000:
1270 mcr = MCR_MIR;
1271 IRDA_DEBUG(0, "%s(), handling baud of 1152000\n", __FUNCTION__);
1272 break;
1273 case 4000000:
1274 mcr = MCR_FIR;
1275 IRDA_DEBUG(0, "%s(), handling baud of 4000000\n", __FUNCTION__);
1276 break;
1277 default:
1278 mcr = MCR_FIR;
1279 IRDA_DEBUG(0, "%s(), unknown baud rate of %d\n",
1280 __FUNCTION__, speed);
1281 break;
1282 }
1283
1284 /* Set appropriate speed mode */
1285 switch_bank(iobase, BANK0);
1286 outb(mcr | MCR_TX_DFR, iobase+MCR);
1287
1288 /* Give some hits to the transceiver */
1289 nsc_ircc_change_dongle_speed(iobase, speed, self->io.dongle_id);
1290
1291 /* Set FIFO threshold to TX17, RX16 */
1292 switch_bank(iobase, BANK0);
1293 outb(0x00, iobase+FCR);
1294 outb(FCR_FIFO_EN, iobase+FCR);
1295 outb(FCR_RXTH| /* Set Rx FIFO threshold */
1296 FCR_TXTH| /* Set Tx FIFO threshold */
1297 FCR_TXSR| /* Reset Tx FIFO */
1298 FCR_RXSR| /* Reset Rx FIFO */
1299 FCR_FIFO_EN, /* Enable FIFOs */
1300 iobase+FCR);
1301
1302 /* Set FIFO size to 32 */
1303 switch_bank(iobase, BANK2);
1304 outb(EXCR2_RFSIZ|EXCR2_TFSIZ, iobase+EXCR2);
1305
1306 /* Enable some interrupts so we can receive frames */
1307 switch_bank(iobase, BANK0);
1308 if (speed > 115200) {
1309 /* Install FIR xmit handler */
1310 dev->hard_start_xmit = nsc_ircc_hard_xmit_fir;
1311 ier = IER_SFIF_IE;
1312 nsc_ircc_dma_receive(self);
1313 } else {
1314 /* Install SIR xmit handler */
1315 dev->hard_start_xmit = nsc_ircc_hard_xmit_sir;
1316 ier = IER_RXHDL_IE;
1317 }
1318 /* Set our current interrupt mask */
1319 outb(ier, iobase+IER);
1320
1321 /* Restore BSR */
1322 outb(bank, iobase+BSR);
1323
1324 /* Make sure interrupt handlers keep the proper interrupt mask */
1325 return(ier);
1326}
1327
1328/*
1329 * Function nsc_ircc_hard_xmit (skb, dev)
1330 *
1331 * Transmit the frame!
1332 *
1333 */
1334static int nsc_ircc_hard_xmit_sir(struct sk_buff *skb, struct net_device *dev)
1335{
1336 struct nsc_ircc_cb *self;
1337 unsigned long flags;
1338 int iobase;
1339 __s32 speed;
1340 __u8 bank;
1341
1342 self = (struct nsc_ircc_cb *) dev->priv;
1343
1344 IRDA_ASSERT(self != NULL, return 0;);
1345
1346 iobase = self->io.fir_base;
1347
1348 netif_stop_queue(dev);
1349
1350 /* Make sure tests *& speed change are atomic */
1351 spin_lock_irqsave(&self->lock, flags);
1352
1353 /* Check if we need to change the speed */
1354 speed = irda_get_next_speed(skb);
1355 if ((speed != self->io.speed) && (speed != -1)) {
1356 /* Check for empty frame. */
1357 if (!skb->len) {
1358 /* If we just sent a frame, we get called before
1359 * the last bytes get out (because of the SIR FIFO).
1360 * If this is the case, let interrupt handler change
1361 * the speed itself... Jean II */
1362 if (self->io.direction == IO_RECV) {
1363 nsc_ircc_change_speed(self, speed);
1364 /* TODO : For SIR->SIR, the next packet
1365 * may get corrupted - Jean II */
1366 netif_wake_queue(dev);
1367 } else {
1368 self->new_speed = speed;
1369 /* Queue will be restarted after speed change
1370 * to make sure packets gets through the
1371 * proper xmit handler - Jean II */
1372 }
1373 dev->trans_start = jiffies;
1374 spin_unlock_irqrestore(&self->lock, flags);
1375 dev_kfree_skb(skb);
1376 return 0;
1377 } else
1378 self->new_speed = speed;
1379 }
1380
1381 /* Save current bank */
1382 bank = inb(iobase+BSR);
1383
1384 self->tx_buff.data = self->tx_buff.head;
1385
1386 self->tx_buff.len = async_wrap_skb(skb, self->tx_buff.data,
1387 self->tx_buff.truesize);
1388
1389 self->stats.tx_bytes += self->tx_buff.len;
1390
1391 /* Add interrupt on tx low level (will fire immediately) */
1392 switch_bank(iobase, BANK0);
1393 outb(IER_TXLDL_IE, iobase+IER);
1394
1395 /* Restore bank register */
1396 outb(bank, iobase+BSR);
1397
1398 dev->trans_start = jiffies;
1399 spin_unlock_irqrestore(&self->lock, flags);
1400
1401 dev_kfree_skb(skb);
1402
1403 return 0;
1404}
1405
1406static int nsc_ircc_hard_xmit_fir(struct sk_buff *skb, struct net_device *dev)
1407{
1408 struct nsc_ircc_cb *self;
1409 unsigned long flags;
1410 int iobase;
1411 __s32 speed;
1412 __u8 bank;
1413 int mtt, diff;
1414
1415 self = (struct nsc_ircc_cb *) dev->priv;
1416 iobase = self->io.fir_base;
1417
1418 netif_stop_queue(dev);
1419
1420 /* Make sure tests *& speed change are atomic */
1421 spin_lock_irqsave(&self->lock, flags);
1422
1423 /* Check if we need to change the speed */
1424 speed = irda_get_next_speed(skb);
1425 if ((speed != self->io.speed) && (speed != -1)) {
1426 /* Check for empty frame. */
1427 if (!skb->len) {
1428 /* If we are currently transmitting, defer to
1429 * interrupt handler. - Jean II */
1430 if(self->tx_fifo.len == 0) {
1431 nsc_ircc_change_speed(self, speed);
1432 netif_wake_queue(dev);
1433 } else {
1434 self->new_speed = speed;
1435 /* Keep queue stopped :
1436 * the speed change operation may change the
1437 * xmit handler, and we want to make sure
1438 * the next packet get through the proper
1439 * Tx path, so block the Tx queue until
1440 * the speed change has been done.
1441 * Jean II */
1442 }
1443 dev->trans_start = jiffies;
1444 spin_unlock_irqrestore(&self->lock, flags);
1445 dev_kfree_skb(skb);
1446 return 0;
1447 } else {
1448 /* Change speed after current frame */
1449 self->new_speed = speed;
1450 }
1451 }
1452
1453 /* Save current bank */
1454 bank = inb(iobase+BSR);
1455
1456 /* Register and copy this frame to DMA memory */
1457 self->tx_fifo.queue[self->tx_fifo.free].start = self->tx_fifo.tail;
1458 self->tx_fifo.queue[self->tx_fifo.free].len = skb->len;
1459 self->tx_fifo.tail += skb->len;
1460
1461 self->stats.tx_bytes += skb->len;
1462
1463 memcpy(self->tx_fifo.queue[self->tx_fifo.free].start, skb->data,
1464 skb->len);
1465
1466 self->tx_fifo.len++;
1467 self->tx_fifo.free++;
1468
1469 /* Start transmit only if there is currently no transmit going on */
1470 if (self->tx_fifo.len == 1) {
1471 /* Check if we must wait the min turn time or not */
1472 mtt = irda_get_mtt(skb);
1473 if (mtt) {
1474 /* Check how much time we have used already */
1475 do_gettimeofday(&self->now);
1476 diff = self->now.tv_usec - self->stamp.tv_usec;
1477 if (diff < 0)
1478 diff += 1000000;
1479
1480 /* Check if the mtt is larger than the time we have
1481 * already used by all the protocol processing
1482 */
1483 if (mtt > diff) {
1484 mtt -= diff;
1485
1486 /*
1487 * Use timer if delay larger than 125 us, and
1488 * use udelay for smaller values which should
1489 * be acceptable
1490 */
1491 if (mtt > 125) {
1492 /* Adjust for timer resolution */
1493 mtt = mtt / 125;
1494
1495 /* Setup timer */
1496 switch_bank(iobase, BANK4);
1497 outb(mtt & 0xff, iobase+TMRL);
1498 outb((mtt >> 8) & 0x0f, iobase+TMRH);
1499
1500 /* Start timer */
1501 outb(IRCR1_TMR_EN, iobase+IRCR1);
1502 self->io.direction = IO_XMIT;
1503
1504 /* Enable timer interrupt */
1505 switch_bank(iobase, BANK0);
1506 outb(IER_TMR_IE, iobase+IER);
1507
1508 /* Timer will take care of the rest */
1509 goto out;
1510 } else
1511 udelay(mtt);
1512 }
1513 }
1514 /* Enable DMA interrupt */
1515 switch_bank(iobase, BANK0);
1516 outb(IER_DMA_IE, iobase+IER);
1517
1518 /* Transmit frame */
1519 nsc_ircc_dma_xmit(self, iobase);
1520 }
1521 out:
1522 /* Not busy transmitting anymore if window is not full,
1523 * and if we don't need to change speed */
1524 if ((self->tx_fifo.free < MAX_TX_WINDOW) && (self->new_speed == 0))
1525 netif_wake_queue(self->netdev);
1526
1527 /* Restore bank register */
1528 outb(bank, iobase+BSR);
1529
1530 dev->trans_start = jiffies;
1531 spin_unlock_irqrestore(&self->lock, flags);
1532 dev_kfree_skb(skb);
1533
1534 return 0;
1535}
1536
1537/*
1538 * Function nsc_ircc_dma_xmit (self, iobase)
1539 *
1540 * Transmit data using DMA
1541 *
1542 */
1543static void nsc_ircc_dma_xmit(struct nsc_ircc_cb *self, int iobase)
1544{
1545 int bsr;
1546
1547 /* Save current bank */
1548 bsr = inb(iobase+BSR);
1549
1550 /* Disable DMA */
1551 switch_bank(iobase, BANK0);
1552 outb(inb(iobase+MCR) & ~MCR_DMA_EN, iobase+MCR);
1553
1554 self->io.direction = IO_XMIT;
1555
1556 /* Choose transmit DMA channel */
1557 switch_bank(iobase, BANK2);
1558 outb(ECR1_DMASWP|ECR1_DMANF|ECR1_EXT_SL, iobase+ECR1);
1559
1560 irda_setup_dma(self->io.dma,
1561 ((u8 *)self->tx_fifo.queue[self->tx_fifo.ptr].start -
1562 self->tx_buff.head) + self->tx_buff_dma,
1563 self->tx_fifo.queue[self->tx_fifo.ptr].len,
1564 DMA_TX_MODE);
1565
1566 /* Enable DMA and SIR interaction pulse */
1567 switch_bank(iobase, BANK0);
1568 outb(inb(iobase+MCR)|MCR_TX_DFR|MCR_DMA_EN|MCR_IR_PLS, iobase+MCR);
1569
1570 /* Restore bank register */
1571 outb(bsr, iobase+BSR);
1572}
1573
1574/*
1575 * Function nsc_ircc_pio_xmit (self, iobase)
1576 *
1577 * Transmit data using PIO. Returns the number of bytes that actually
1578 * got transferred
1579 *
1580 */
1581static int nsc_ircc_pio_write(int iobase, __u8 *buf, int len, int fifo_size)
1582{
1583 int actual = 0;
1584 __u8 bank;
1585
1586 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
1587
1588 /* Save current bank */
1589 bank = inb(iobase+BSR);
1590
1591 switch_bank(iobase, BANK0);
1592 if (!(inb_p(iobase+LSR) & LSR_TXEMP)) {
1593 IRDA_DEBUG(4, "%s(), warning, FIFO not empty yet!\n",
1594 __FUNCTION__);
1595
1596 /* FIFO may still be filled to the Tx interrupt threshold */
1597 fifo_size -= 17;
1598 }
1599
1600 /* Fill FIFO with current frame */
1601 while ((fifo_size-- > 0) && (actual < len)) {
1602 /* Transmit next byte */
1603 outb(buf[actual++], iobase+TXD);
1604 }
1605
1606 IRDA_DEBUG(4, "%s(), fifo_size %d ; %d sent of %d\n",
1607 __FUNCTION__, fifo_size, actual, len);
1608
1609 /* Restore bank */
1610 outb(bank, iobase+BSR);
1611
1612 return actual;
1613}
1614
1615/*
1616 * Function nsc_ircc_dma_xmit_complete (self)
1617 *
1618 * The transfer of a frame in finished. This function will only be called
1619 * by the interrupt handler
1620 *
1621 */
1622static int nsc_ircc_dma_xmit_complete(struct nsc_ircc_cb *self)
1623{
1624 int iobase;
1625 __u8 bank;
1626 int ret = TRUE;
1627
1628 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
1629
1630 iobase = self->io.fir_base;
1631
1632 /* Save current bank */
1633 bank = inb(iobase+BSR);
1634
1635 /* Disable DMA */
1636 switch_bank(iobase, BANK0);
1637 outb(inb(iobase+MCR) & ~MCR_DMA_EN, iobase+MCR);
1638
1639 /* Check for underrrun! */
1640 if (inb(iobase+ASCR) & ASCR_TXUR) {
1641 self->stats.tx_errors++;
1642 self->stats.tx_fifo_errors++;
1643
1644 /* Clear bit, by writing 1 into it */
1645 outb(ASCR_TXUR, iobase+ASCR);
1646 } else {
1647 self->stats.tx_packets++;
1648 }
1649
1650 /* Finished with this frame, so prepare for next */
1651 self->tx_fifo.ptr++;
1652 self->tx_fifo.len--;
1653
1654 /* Any frames to be sent back-to-back? */
1655 if (self->tx_fifo.len) {
1656 nsc_ircc_dma_xmit(self, iobase);
1657
1658 /* Not finished yet! */
1659 ret = FALSE;
1660 } else {
1661 /* Reset Tx FIFO info */
1662 self->tx_fifo.len = self->tx_fifo.ptr = self->tx_fifo.free = 0;
1663 self->tx_fifo.tail = self->tx_buff.head;
1664 }
1665
1666 /* Make sure we have room for more frames and
1667 * that we don't need to change speed */
1668 if ((self->tx_fifo.free < MAX_TX_WINDOW) && (self->new_speed == 0)) {
1669 /* Not busy transmitting anymore */
1670 /* Tell the network layer, that we can accept more frames */
1671 netif_wake_queue(self->netdev);
1672 }
1673
1674 /* Restore bank */
1675 outb(bank, iobase+BSR);
1676
1677 return ret;
1678}
1679
1680/*
1681 * Function nsc_ircc_dma_receive (self)
1682 *
1683 * Get ready for receiving a frame. The device will initiate a DMA
1684 * if it starts to receive a frame.
1685 *
1686 */
1687static int nsc_ircc_dma_receive(struct nsc_ircc_cb *self)
1688{
1689 int iobase;
1690 __u8 bsr;
1691
1692 iobase = self->io.fir_base;
1693
1694 /* Reset Tx FIFO info */
1695 self->tx_fifo.len = self->tx_fifo.ptr = self->tx_fifo.free = 0;
1696 self->tx_fifo.tail = self->tx_buff.head;
1697
1698 /* Save current bank */
1699 bsr = inb(iobase+BSR);
1700
1701 /* Disable DMA */
1702 switch_bank(iobase, BANK0);
1703 outb(inb(iobase+MCR) & ~MCR_DMA_EN, iobase+MCR);
1704
1705 /* Choose DMA Rx, DMA Fairness, and Advanced mode */
1706 switch_bank(iobase, BANK2);
1707 outb(ECR1_DMANF|ECR1_EXT_SL, iobase+ECR1);
1708
1709 self->io.direction = IO_RECV;
1710 self->rx_buff.data = self->rx_buff.head;
1711
1712 /* Reset Rx FIFO. This will also flush the ST_FIFO */
1713 switch_bank(iobase, BANK0);
1714 outb(FCR_RXSR|FCR_FIFO_EN, iobase+FCR);
1715
1716 self->st_fifo.len = self->st_fifo.pending_bytes = 0;
1717 self->st_fifo.tail = self->st_fifo.head = 0;
1718
1719 irda_setup_dma(self->io.dma, self->rx_buff_dma, self->rx_buff.truesize,
1720 DMA_RX_MODE);
1721
1722 /* Enable DMA */
1723 switch_bank(iobase, BANK0);
1724 outb(inb(iobase+MCR)|MCR_DMA_EN, iobase+MCR);
1725
1726 /* Restore bank register */
1727 outb(bsr, iobase+BSR);
1728
1729 return 0;
1730}
1731
1732/*
1733 * Function nsc_ircc_dma_receive_complete (self)
1734 *
1735 * Finished with receiving frames
1736 *
1737 *
1738 */
1739static int nsc_ircc_dma_receive_complete(struct nsc_ircc_cb *self, int iobase)
1740{
1741 struct st_fifo *st_fifo;
1742 struct sk_buff *skb;
1743 __u8 status;
1744 __u8 bank;
1745 int len;
1746
1747 st_fifo = &self->st_fifo;
1748
1749 /* Save current bank */
1750 bank = inb(iobase+BSR);
1751
1752 /* Read all entries in status FIFO */
1753 switch_bank(iobase, BANK5);
1754 while ((status = inb(iobase+FRM_ST)) & FRM_ST_VLD) {
1755 /* We must empty the status FIFO no matter what */
1756 len = inb(iobase+RFLFL) | ((inb(iobase+RFLFH) & 0x1f) << 8);
1757
1758 if (st_fifo->tail >= MAX_RX_WINDOW) {
1759 IRDA_DEBUG(0, "%s(), window is full!\n", __FUNCTION__);
1760 continue;
1761 }
1762
1763 st_fifo->entries[st_fifo->tail].status = status;
1764 st_fifo->entries[st_fifo->tail].len = len;
1765 st_fifo->pending_bytes += len;
1766 st_fifo->tail++;
1767 st_fifo->len++;
1768 }
1769 /* Try to process all entries in status FIFO */
1770 while (st_fifo->len > 0) {
1771 /* Get first entry */
1772 status = st_fifo->entries[st_fifo->head].status;
1773 len = st_fifo->entries[st_fifo->head].len;
1774 st_fifo->pending_bytes -= len;
1775 st_fifo->head++;
1776 st_fifo->len--;
1777
1778 /* Check for errors */
1779 if (status & FRM_ST_ERR_MSK) {
1780 if (status & FRM_ST_LOST_FR) {
1781 /* Add number of lost frames to stats */
1782 self->stats.rx_errors += len;
1783 } else {
1784 /* Skip frame */
1785 self->stats.rx_errors++;
1786
1787 self->rx_buff.data += len;
1788
1789 if (status & FRM_ST_MAX_LEN)
1790 self->stats.rx_length_errors++;
1791
1792 if (status & FRM_ST_PHY_ERR)
1793 self->stats.rx_frame_errors++;
1794
1795 if (status & FRM_ST_BAD_CRC)
1796 self->stats.rx_crc_errors++;
1797 }
1798 /* The errors below can be reported in both cases */
1799 if (status & FRM_ST_OVR1)
1800 self->stats.rx_fifo_errors++;
1801
1802 if (status & FRM_ST_OVR2)
1803 self->stats.rx_fifo_errors++;
1804 } else {
1805 /*
1806 * First we must make sure that the frame we
1807 * want to deliver is all in main memory. If we
1808 * cannot tell, then we check if the Rx FIFO is
1809 * empty. If not then we will have to take a nap
1810 * and try again later.
1811 */
1812 if (st_fifo->pending_bytes < self->io.fifo_size) {
1813 switch_bank(iobase, BANK0);
1814 if (inb(iobase+LSR) & LSR_RXDA) {
1815 /* Put this entry back in fifo */
1816 st_fifo->head--;
1817 st_fifo->len++;
1818 st_fifo->pending_bytes += len;
1819 st_fifo->entries[st_fifo->head].status = status;
1820 st_fifo->entries[st_fifo->head].len = len;
1821 /*
1822 * DMA not finished yet, so try again
1823 * later, set timer value, resolution
1824 * 125 us
1825 */
1826 switch_bank(iobase, BANK4);
1827 outb(0x02, iobase+TMRL); /* x 125 us */
1828 outb(0x00, iobase+TMRH);
1829
1830 /* Start timer */
1831 outb(IRCR1_TMR_EN, iobase+IRCR1);
1832
1833 /* Restore bank register */
1834 outb(bank, iobase+BSR);
1835
1836 return FALSE; /* I'll be back! */
1837 }
1838 }
1839
1840 /*
1841 * Remember the time we received this frame, so we can
1842 * reduce the min turn time a bit since we will know
1843 * how much time we have used for protocol processing
1844 */
1845 do_gettimeofday(&self->stamp);
1846
1847 skb = dev_alloc_skb(len+1);
1848 if (skb == NULL) {
1849 IRDA_WARNING("%s(), memory squeeze, "
1850 "dropping frame.\n",
1851 __FUNCTION__);
1852 self->stats.rx_dropped++;
1853
1854 /* Restore bank register */
1855 outb(bank, iobase+BSR);
1856
1857 return FALSE;
1858 }
1859
1860 /* Make sure IP header gets aligned */
1861 skb_reserve(skb, 1);
1862
1863 /* Copy frame without CRC */
1864 if (self->io.speed < 4000000) {
1865 skb_put(skb, len-2);
1866 memcpy(skb->data, self->rx_buff.data, len-2);
1867 } else {
1868 skb_put(skb, len-4);
1869 memcpy(skb->data, self->rx_buff.data, len-4);
1870 }
1871
1872 /* Move to next frame */
1873 self->rx_buff.data += len;
1874 self->stats.rx_bytes += len;
1875 self->stats.rx_packets++;
1876
1877 skb->dev = self->netdev;
1878 skb->mac.raw = skb->data;
1879 skb->protocol = htons(ETH_P_IRDA);
1880 netif_rx(skb);
1881 self->netdev->last_rx = jiffies;
1882 }
1883 }
1884 /* Restore bank register */
1885 outb(bank, iobase+BSR);
1886
1887 return TRUE;
1888}
1889
1890/*
1891 * Function nsc_ircc_pio_receive (self)
1892 *
1893 * Receive all data in receiver FIFO
1894 *
1895 */
1896static void nsc_ircc_pio_receive(struct nsc_ircc_cb *self)
1897{
1898 __u8 byte;
1899 int iobase;
1900
1901 iobase = self->io.fir_base;
1902
1903 /* Receive all characters in Rx FIFO */
1904 do {
1905 byte = inb(iobase+RXD);
1906 async_unwrap_char(self->netdev, &self->stats, &self->rx_buff,
1907 byte);
1908 } while (inb(iobase+LSR) & LSR_RXDA); /* Data available */
1909}
1910
1911/*
1912 * Function nsc_ircc_sir_interrupt (self, eir)
1913 *
1914 * Handle SIR interrupt
1915 *
1916 */
1917static void nsc_ircc_sir_interrupt(struct nsc_ircc_cb *self, int eir)
1918{
1919 int actual;
1920
1921 /* Check if transmit FIFO is low on data */
1922 if (eir & EIR_TXLDL_EV) {
1923 /* Write data left in transmit buffer */
1924 actual = nsc_ircc_pio_write(self->io.fir_base,
1925 self->tx_buff.data,
1926 self->tx_buff.len,
1927 self->io.fifo_size);
1928 self->tx_buff.data += actual;
1929 self->tx_buff.len -= actual;
1930
1931 self->io.direction = IO_XMIT;
1932
1933 /* Check if finished */
1934 if (self->tx_buff.len > 0)
1935 self->ier = IER_TXLDL_IE;
1936 else {
1937
1938 self->stats.tx_packets++;
1939 netif_wake_queue(self->netdev);
1940 self->ier = IER_TXEMP_IE;
1941 }
1942
1943 }
1944 /* Check if transmission has completed */
1945 if (eir & EIR_TXEMP_EV) {
1946 /* Turn around and get ready to receive some data */
1947 self->io.direction = IO_RECV;
1948 self->ier = IER_RXHDL_IE;
1949 /* Check if we need to change the speed?
1950 * Need to be after self->io.direction to avoid race with
1951 * nsc_ircc_hard_xmit_sir() - Jean II */
1952 if (self->new_speed) {
1953 IRDA_DEBUG(2, "%s(), Changing speed!\n", __FUNCTION__);
1954 self->ier = nsc_ircc_change_speed(self,
1955 self->new_speed);
1956 self->new_speed = 0;
1957 netif_wake_queue(self->netdev);
1958
1959 /* Check if we are going to FIR */
1960 if (self->io.speed > 115200) {
1961 /* No need to do anymore SIR stuff */
1962 return;
1963 }
1964 }
1965 }
1966
1967 /* Rx FIFO threshold or timeout */
1968 if (eir & EIR_RXHDL_EV) {
1969 nsc_ircc_pio_receive(self);
1970
1971 /* Keep receiving */
1972 self->ier = IER_RXHDL_IE;
1973 }
1974}
1975
1976/*
1977 * Function nsc_ircc_fir_interrupt (self, eir)
1978 *
1979 * Handle MIR/FIR interrupt
1980 *
1981 */
1982static void nsc_ircc_fir_interrupt(struct nsc_ircc_cb *self, int iobase,
1983 int eir)
1984{
1985 __u8 bank;
1986
1987 bank = inb(iobase+BSR);
1988
1989 /* Status FIFO event*/
1990 if (eir & EIR_SFIF_EV) {
1991 /* Check if DMA has finished */
1992 if (nsc_ircc_dma_receive_complete(self, iobase)) {
1993 /* Wait for next status FIFO interrupt */
1994 self->ier = IER_SFIF_IE;
1995 } else {
1996 self->ier = IER_SFIF_IE | IER_TMR_IE;
1997 }
1998 } else if (eir & EIR_TMR_EV) { /* Timer finished */
1999 /* Disable timer */
2000 switch_bank(iobase, BANK4);
2001 outb(0, iobase+IRCR1);
2002
2003 /* Clear timer event */
2004 switch_bank(iobase, BANK0);
2005 outb(ASCR_CTE, iobase+ASCR);
2006
2007 /* Check if this is a Tx timer interrupt */
2008 if (self->io.direction == IO_XMIT) {
2009 nsc_ircc_dma_xmit(self, iobase);
2010
2011 /* Interrupt on DMA */
2012 self->ier = IER_DMA_IE;
2013 } else {
2014 /* Check (again) if DMA has finished */
2015 if (nsc_ircc_dma_receive_complete(self, iobase)) {
2016 self->ier = IER_SFIF_IE;
2017 } else {
2018 self->ier = IER_SFIF_IE | IER_TMR_IE;
2019 }
2020 }
2021 } else if (eir & EIR_DMA_EV) {
2022 /* Finished with all transmissions? */
2023 if (nsc_ircc_dma_xmit_complete(self)) {
2024 if(self->new_speed != 0) {
2025 /* As we stop the Tx queue, the speed change
2026 * need to be done when the Tx fifo is
2027 * empty. Ask for a Tx done interrupt */
2028 self->ier = IER_TXEMP_IE;
2029 } else {
2030 /* Check if there are more frames to be
2031 * transmitted */
2032 if (irda_device_txqueue_empty(self->netdev)) {
2033 /* Prepare for receive */
2034 nsc_ircc_dma_receive(self);
2035 self->ier = IER_SFIF_IE;
2036 } else
2037 IRDA_WARNING("%s(), potential "
2038 "Tx queue lockup !\n",
2039 __FUNCTION__);
2040 }
2041 } else {
2042 /* Not finished yet, so interrupt on DMA again */
2043 self->ier = IER_DMA_IE;
2044 }
2045 } else if (eir & EIR_TXEMP_EV) {
2046 /* The Tx FIFO has totally drained out, so now we can change
2047 * the speed... - Jean II */
2048 self->ier = nsc_ircc_change_speed(self, self->new_speed);
2049 self->new_speed = 0;
2050 netif_wake_queue(self->netdev);
2051 /* Note : nsc_ircc_change_speed() restarted Rx fifo */
2052 }
2053
2054 outb(bank, iobase+BSR);
2055}
2056
2057/*
2058 * Function nsc_ircc_interrupt (irq, dev_id, regs)
2059 *
2060 * An interrupt from the chip has arrived. Time to do some work
2061 *
2062 */
2063static irqreturn_t nsc_ircc_interrupt(int irq, void *dev_id,
2064 struct pt_regs *regs)
2065{
2066 struct net_device *dev = (struct net_device *) dev_id;
2067 struct nsc_ircc_cb *self;
2068 __u8 bsr, eir;
2069 int iobase;
2070
2071 if (!dev) {
2072 IRDA_WARNING("%s: irq %d for unknown device.\n",
2073 driver_name, irq);
2074 return IRQ_NONE;
2075 }
2076 self = (struct nsc_ircc_cb *) dev->priv;
2077
2078 spin_lock(&self->lock);
2079
2080 iobase = self->io.fir_base;
2081
2082 bsr = inb(iobase+BSR); /* Save current bank */
2083
2084 switch_bank(iobase, BANK0);
2085 self->ier = inb(iobase+IER);
2086 eir = inb(iobase+EIR) & self->ier; /* Mask out the interesting ones */
2087
2088 outb(0, iobase+IER); /* Disable interrupts */
2089
2090 if (eir) {
2091 /* Dispatch interrupt handler for the current speed */
2092 if (self->io.speed > 115200)
2093 nsc_ircc_fir_interrupt(self, iobase, eir);
2094 else
2095 nsc_ircc_sir_interrupt(self, eir);
2096 }
2097
2098 outb(self->ier, iobase+IER); /* Restore interrupts */
2099 outb(bsr, iobase+BSR); /* Restore bank register */
2100
2101 spin_unlock(&self->lock);
2102 return IRQ_RETVAL(eir);
2103}
2104
2105/*
2106 * Function nsc_ircc_is_receiving (self)
2107 *
2108 * Return TRUE is we are currently receiving a frame
2109 *
2110 */
2111static int nsc_ircc_is_receiving(struct nsc_ircc_cb *self)
2112{
2113 unsigned long flags;
2114 int status = FALSE;
2115 int iobase;
2116 __u8 bank;
2117
2118 IRDA_ASSERT(self != NULL, return FALSE;);
2119
2120 spin_lock_irqsave(&self->lock, flags);
2121
2122 if (self->io.speed > 115200) {
2123 iobase = self->io.fir_base;
2124
2125 /* Check if rx FIFO is not empty */
2126 bank = inb(iobase+BSR);
2127 switch_bank(iobase, BANK2);
2128 if ((inb(iobase+RXFLV) & 0x3f) != 0) {
2129 /* We are receiving something */
2130 status = TRUE;
2131 }
2132 outb(bank, iobase+BSR);
2133 } else
2134 status = (self->rx_buff.state != OUTSIDE_FRAME);
2135
2136 spin_unlock_irqrestore(&self->lock, flags);
2137
2138 return status;
2139}
2140
2141/*
2142 * Function nsc_ircc_net_open (dev)
2143 *
2144 * Start the device
2145 *
2146 */
2147static int nsc_ircc_net_open(struct net_device *dev)
2148{
2149 struct nsc_ircc_cb *self;
2150 int iobase;
2151 char hwname[32];
2152 __u8 bank;
2153
2154 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
2155
2156 IRDA_ASSERT(dev != NULL, return -1;);
2157 self = (struct nsc_ircc_cb *) dev->priv;
2158
2159 IRDA_ASSERT(self != NULL, return 0;);
2160
2161 iobase = self->io.fir_base;
2162
2163 if (request_irq(self->io.irq, nsc_ircc_interrupt, 0, dev->name, dev)) {
2164 IRDA_WARNING("%s, unable to allocate irq=%d\n",
2165 driver_name, self->io.irq);
2166 return -EAGAIN;
2167 }
2168 /*
2169 * Always allocate the DMA channel after the IRQ, and clean up on
2170 * failure.
2171 */
2172 if (request_dma(self->io.dma, dev->name)) {
2173 IRDA_WARNING("%s, unable to allocate dma=%d\n",
2174 driver_name, self->io.dma);
2175 free_irq(self->io.irq, dev);
2176 return -EAGAIN;
2177 }
2178
2179 /* Save current bank */
2180 bank = inb(iobase+BSR);
2181
2182 /* turn on interrupts */
2183 switch_bank(iobase, BANK0);
2184 outb(IER_LS_IE | IER_RXHDL_IE, iobase+IER);
2185
2186 /* Restore bank register */
2187 outb(bank, iobase+BSR);
2188
2189 /* Ready to play! */
2190 netif_start_queue(dev);
2191
2192 /* Give self a hardware name */
2193 sprintf(hwname, "NSC-FIR @ 0x%03x", self->io.fir_base);
2194
2195 /*
2196 * Open new IrLAP layer instance, now that everything should be
2197 * initialized properly
2198 */
2199 self->irlap = irlap_open(dev, &self->qos, hwname);
2200
2201 return 0;
2202}
2203
2204/*
2205 * Function nsc_ircc_net_close (dev)
2206 *
2207 * Stop the device
2208 *
2209 */
2210static int nsc_ircc_net_close(struct net_device *dev)
2211{
2212 struct nsc_ircc_cb *self;
2213 int iobase;
2214 __u8 bank;
2215
2216 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
2217
2218 IRDA_ASSERT(dev != NULL, return -1;);
2219
2220 self = (struct nsc_ircc_cb *) dev->priv;
2221 IRDA_ASSERT(self != NULL, return 0;);
2222
2223 /* Stop device */
2224 netif_stop_queue(dev);
2225
2226 /* Stop and remove instance of IrLAP */
2227 if (self->irlap)
2228 irlap_close(self->irlap);
2229 self->irlap = NULL;
2230
2231 iobase = self->io.fir_base;
2232
2233 disable_dma(self->io.dma);
2234
2235 /* Save current bank */
2236 bank = inb(iobase+BSR);
2237
2238 /* Disable interrupts */
2239 switch_bank(iobase, BANK0);
2240 outb(0, iobase+IER);
2241
2242 free_irq(self->io.irq, dev);
2243 free_dma(self->io.dma);
2244
2245 /* Restore bank register */
2246 outb(bank, iobase+BSR);
2247
2248 return 0;
2249}
2250
2251/*
2252 * Function nsc_ircc_net_ioctl (dev, rq, cmd)
2253 *
2254 * Process IOCTL commands for this device
2255 *
2256 */
2257static int nsc_ircc_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2258{
2259 struct if_irda_req *irq = (struct if_irda_req *) rq;
2260 struct nsc_ircc_cb *self;
2261 unsigned long flags;
2262 int ret = 0;
2263
2264 IRDA_ASSERT(dev != NULL, return -1;);
2265
2266 self = dev->priv;
2267
2268 IRDA_ASSERT(self != NULL, return -1;);
2269
2270 IRDA_DEBUG(2, "%s(), %s, (cmd=0x%X)\n", __FUNCTION__, dev->name, cmd);
2271
2272 switch (cmd) {
2273 case SIOCSBANDWIDTH: /* Set bandwidth */
2274 if (!capable(CAP_NET_ADMIN)) {
2275 ret = -EPERM;
2276 break;
2277 }
2278 spin_lock_irqsave(&self->lock, flags);
2279 nsc_ircc_change_speed(self, irq->ifr_baudrate);
2280 spin_unlock_irqrestore(&self->lock, flags);
2281 break;
2282 case SIOCSMEDIABUSY: /* Set media busy */
2283 if (!capable(CAP_NET_ADMIN)) {
2284 ret = -EPERM;
2285 break;
2286 }
2287 irda_device_set_media_busy(self->netdev, TRUE);
2288 break;
2289 case SIOCGRECEIVING: /* Check if we are receiving right now */
2290 /* This is already protected */
2291 irq->ifr_receiving = nsc_ircc_is_receiving(self);
2292 break;
2293 default:
2294 ret = -EOPNOTSUPP;
2295 }
2296 return ret;
2297}
2298
2299static struct net_device_stats *nsc_ircc_net_get_stats(struct net_device *dev)
2300{
2301 struct nsc_ircc_cb *self = (struct nsc_ircc_cb *) dev->priv;
2302
2303 return &self->stats;
2304}
2305
Dmitry Torokhov3b99b932006-03-20 18:59:05 -08002306static int nsc_ircc_suspend(struct platform_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307{
Dmitry Torokhov3b99b932006-03-20 18:59:05 -08002308 struct nsc_ircc_cb *self = platform_get_drvdata(dev);
2309 int bank;
2310 unsigned long flags;
2311 int iobase = self->io.fir_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
2313 if (self->io.suspended)
Dmitry Torokhov3b99b932006-03-20 18:59:05 -08002314 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
Dmitry Torokhov3b99b932006-03-20 18:59:05 -08002316 IRDA_DEBUG(1, "%s, Suspending\n", driver_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
Dmitry Torokhov3b99b932006-03-20 18:59:05 -08002318 rtnl_lock();
2319 if (netif_running(self->netdev)) {
2320 netif_device_detach(self->netdev);
2321 spin_lock_irqsave(&self->lock, flags);
2322 /* Save current bank */
2323 bank = inb(iobase+BSR);
2324
2325 /* Disable interrupts */
2326 switch_bank(iobase, BANK0);
2327 outb(0, iobase+IER);
2328
2329 /* Restore bank register */
2330 outb(bank, iobase+BSR);
2331
2332 spin_unlock_irqrestore(&self->lock, flags);
2333 free_irq(self->io.irq, self->netdev);
2334 disable_dma(self->io.dma);
2335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 self->io.suspended = 1;
Dmitry Torokhov3b99b932006-03-20 18:59:05 -08002337 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 return 0;
2340}
2341
Dmitry Torokhov3b99b932006-03-20 18:59:05 -08002342static int nsc_ircc_resume(struct platform_device *dev)
2343{
2344 struct nsc_ircc_cb *self = platform_get_drvdata(dev);
2345 unsigned long flags;
2346
2347 if (!self->io.suspended)
2348 return 0;
2349
2350 IRDA_DEBUG(1, "%s, Waking up\n", driver_name);
2351
2352 rtnl_lock();
2353 nsc_ircc_setup(&self->io);
2354 nsc_ircc_init_dongle_interface(self->io.fir_base, self->io.dongle_id);
2355
2356 if (netif_running(self->netdev)) {
2357 if (request_irq(self->io.irq, nsc_ircc_interrupt, 0,
2358 self->netdev->name, self->netdev)) {
2359 IRDA_WARNING("%s, unable to allocate irq=%d\n",
2360 driver_name, self->io.irq);
2361
2362 /*
2363 * Don't fail resume process, just kill this
2364 * network interface
2365 */
2366 unregister_netdevice(self->netdev);
2367 } else {
2368 spin_lock_irqsave(&self->lock, flags);
2369 nsc_ircc_change_speed(self, self->io.speed);
2370 spin_unlock_irqrestore(&self->lock, flags);
2371 netif_device_attach(self->netdev);
2372 }
2373
2374 } else {
2375 spin_lock_irqsave(&self->lock, flags);
2376 nsc_ircc_change_speed(self, 9600);
2377 spin_unlock_irqrestore(&self->lock, flags);
2378 }
2379 self->io.suspended = 0;
2380 rtnl_unlock();
2381
2382 return 0;
2383}
2384
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
2386MODULE_DESCRIPTION("NSC IrDA Device Driver");
2387MODULE_LICENSE("GPL");
2388
2389
2390module_param(qos_mtt_bits, int, 0);
2391MODULE_PARM_DESC(qos_mtt_bits, "Minimum Turn Time");
2392module_param_array(io, int, NULL, 0);
2393MODULE_PARM_DESC(io, "Base I/O addresses");
2394module_param_array(irq, int, NULL, 0);
2395MODULE_PARM_DESC(irq, "IRQ lines");
2396module_param_array(dma, int, NULL, 0);
2397MODULE_PARM_DESC(dma, "DMA channels");
2398module_param(dongle_id, int, 0);
2399MODULE_PARM_DESC(dongle_id, "Type-id of used dongle");
2400
2401module_init(nsc_ircc_init);
2402module_exit(nsc_ircc_cleanup);
2403