ia64/kvm: compilation fix. export account_system_vtime.
[pv_ops_mirror.git] / drivers / net / irda / nsc-ircc.c
bloba7714da7c283c84d48082b34540fbb05f86f85d2
1 /*********************************************************************
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>
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
15 * Copyright (c) 2000-2004 Jean Tourrilhes <jt@hpl.hp.com>
16 * All Rights Reserved
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.
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.
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!
31 * __u8 bank;
33 * bank = inb(iobase+BSR);
35 * do_your_stuff_here();
37 * outb(bank, iobase+BSR);
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.
43 ********************************************************************/
45 #include <linux/module.h>
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>
57 #include <linux/pnp.h>
58 #include <linux/platform_device.h>
60 #include <asm/io.h>
61 #include <asm/dma.h>
62 #include <asm/byteorder.h>
64 #include <net/irda/wrapper.h>
65 #include <net/irda/irda.h>
66 #include <net/irda/irda_device.h>
68 #include "nsc-ircc.h"
70 #define CHIP_IO_EXTENT 8
71 #define BROKEN_DONGLE_ID
73 static char *driver_name = "nsc-ircc";
75 /* Power Management */
76 #define NSC_IRCC_DRIVER_NAME "nsc-ircc"
77 static int nsc_ircc_suspend(struct platform_device *dev, pm_message_t state);
78 static int nsc_ircc_resume(struct platform_device *dev);
80 static struct platform_driver nsc_ircc_driver = {
81 .suspend = nsc_ircc_suspend,
82 .resume = nsc_ircc_resume,
83 .driver = {
84 .name = NSC_IRCC_DRIVER_NAME,
88 /* Module parameters */
89 static int qos_mtt_bits = 0x07; /* 1 ms or more */
90 static int dongle_id;
92 /* Use BIOS settions by default, but user may supply module parameters */
93 static unsigned int io[] = { ~0, ~0, ~0, ~0, ~0 };
94 static unsigned int irq[] = { 0, 0, 0, 0, 0 };
95 static unsigned int dma[] = { 0, 0, 0, 0, 0 };
97 static int nsc_ircc_probe_108(nsc_chip_t *chip, chipio_t *info);
98 static int nsc_ircc_probe_338(nsc_chip_t *chip, chipio_t *info);
99 static int nsc_ircc_probe_39x(nsc_chip_t *chip, chipio_t *info);
100 static int nsc_ircc_init_108(nsc_chip_t *chip, chipio_t *info);
101 static int nsc_ircc_init_338(nsc_chip_t *chip, chipio_t *info);
102 static int nsc_ircc_init_39x(nsc_chip_t *chip, chipio_t *info);
103 #ifdef CONFIG_PNP
104 static int nsc_ircc_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *id);
105 #endif
107 /* These are the known NSC chips */
108 static nsc_chip_t chips[] = {
109 /* Name, {cfg registers}, chip id index reg, chip id expected value, revision mask */
110 { "PC87108", { 0x150, 0x398, 0xea }, 0x05, 0x10, 0xf0,
111 nsc_ircc_probe_108, nsc_ircc_init_108 },
112 { "PC87338", { 0x398, 0x15c, 0x2e }, 0x08, 0xb0, 0xf8,
113 nsc_ircc_probe_338, nsc_ircc_init_338 },
114 /* Contributed by Steffen Pingel - IBM X40 */
115 { "PC8738x", { 0x164e, 0x4e, 0x2e }, 0x20, 0xf4, 0xff,
116 nsc_ircc_probe_39x, nsc_ircc_init_39x },
117 /* Contributed by Jan Frey - IBM A30/A31 */
118 { "PC8739x", { 0x2e, 0x4e, 0x0 }, 0x20, 0xea, 0xff,
119 nsc_ircc_probe_39x, nsc_ircc_init_39x },
120 /* IBM ThinkPads using PC8738x (T60/X60/Z60) */
121 { "IBM-PC8738x", { 0x2e, 0x4e, 0x0 }, 0x20, 0xf4, 0xff,
122 nsc_ircc_probe_39x, nsc_ircc_init_39x },
123 /* IBM ThinkPads using PC8394T (T43/R52/?) */
124 { "IBM-PC8394T", { 0x2e, 0x4e, 0x0 }, 0x20, 0xf9, 0xff,
125 nsc_ircc_probe_39x, nsc_ircc_init_39x },
126 { NULL }
129 static struct nsc_ircc_cb *dev_self[] = { NULL, NULL, NULL, NULL, NULL };
131 static char *dongle_types[] = {
132 "Differential serial interface",
133 "Differential serial interface",
134 "Reserved",
135 "Reserved",
136 "Sharp RY5HD01",
137 "Reserved",
138 "Single-ended serial interface",
139 "Consumer-IR only",
140 "HP HSDL-2300, HP HSDL-3600/HSDL-3610",
141 "IBM31T1100 or Temic TFDS6000/TFDS6500",
142 "Reserved",
143 "Reserved",
144 "HP HSDL-1100/HSDL-2100",
145 "HP HSDL-1100/HSDL-2100",
146 "Supports SIR Mode only",
147 "No dongle connected",
150 /* PNP probing */
151 static chipio_t pnp_info;
152 static const struct pnp_device_id nsc_ircc_pnp_table[] = {
153 { .id = "NSC6001", .driver_data = 0 },
154 { .id = "IBM0071", .driver_data = 0 },
158 MODULE_DEVICE_TABLE(pnp, nsc_ircc_pnp_table);
160 static struct pnp_driver nsc_ircc_pnp_driver = {
161 #ifdef CONFIG_PNP
162 .name = "nsc-ircc",
163 .id_table = nsc_ircc_pnp_table,
164 .probe = nsc_ircc_pnp_probe,
165 #endif
168 /* Some prototypes */
169 static int nsc_ircc_open(chipio_t *info);
170 static int nsc_ircc_close(struct nsc_ircc_cb *self);
171 static int nsc_ircc_setup(chipio_t *info);
172 static void nsc_ircc_pio_receive(struct nsc_ircc_cb *self);
173 static int nsc_ircc_dma_receive(struct nsc_ircc_cb *self);
174 static int nsc_ircc_dma_receive_complete(struct nsc_ircc_cb *self, int iobase);
175 static int nsc_ircc_hard_xmit_sir(struct sk_buff *skb, struct net_device *dev);
176 static int nsc_ircc_hard_xmit_fir(struct sk_buff *skb, struct net_device *dev);
177 static int nsc_ircc_pio_write(int iobase, __u8 *buf, int len, int fifo_size);
178 static void nsc_ircc_dma_xmit(struct nsc_ircc_cb *self, int iobase);
179 static __u8 nsc_ircc_change_speed(struct nsc_ircc_cb *self, __u32 baud);
180 static int nsc_ircc_is_receiving(struct nsc_ircc_cb *self);
181 static int nsc_ircc_read_dongle_id (int iobase);
182 static void nsc_ircc_init_dongle_interface (int iobase, int dongle_id);
184 static int nsc_ircc_net_open(struct net_device *dev);
185 static int nsc_ircc_net_close(struct net_device *dev);
186 static int nsc_ircc_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
187 static struct net_device_stats *nsc_ircc_net_get_stats(struct net_device *dev);
189 /* Globals */
190 static int pnp_registered;
191 static int pnp_succeeded;
194 * Function nsc_ircc_init ()
196 * Initialize chip. Just try to find out how many chips we are dealing with
197 * and where they are
199 static int __init nsc_ircc_init(void)
201 chipio_t info;
202 nsc_chip_t *chip;
203 int ret;
204 int cfg_base;
205 int cfg, id;
206 int reg;
207 int i = 0;
209 ret = platform_driver_register(&nsc_ircc_driver);
210 if (ret) {
211 IRDA_ERROR("%s, Can't register driver!\n", driver_name);
212 return ret;
215 /* Register with PnP subsystem to detect disable ports */
216 ret = pnp_register_driver(&nsc_ircc_pnp_driver);
218 if (!ret)
219 pnp_registered = 1;
221 ret = -ENODEV;
223 /* Probe for all the NSC chipsets we know about */
224 for (chip = chips; chip->name ; chip++) {
225 IRDA_DEBUG(2, "%s(), Probing for %s ...\n", __FUNCTION__,
226 chip->name);
228 /* Try all config registers for this chip */
229 for (cfg = 0; cfg < ARRAY_SIZE(chip->cfg); cfg++) {
230 cfg_base = chip->cfg[cfg];
231 if (!cfg_base)
232 continue;
234 /* Read index register */
235 reg = inb(cfg_base);
236 if (reg == 0xff) {
237 IRDA_DEBUG(2, "%s() no chip at 0x%03x\n", __FUNCTION__, cfg_base);
238 continue;
241 /* Read chip identification register */
242 outb(chip->cid_index, cfg_base);
243 id = inb(cfg_base+1);
244 if ((id & chip->cid_mask) == chip->cid_value) {
245 IRDA_DEBUG(2, "%s() Found %s chip, revision=%d\n",
246 __FUNCTION__, chip->name, id & ~chip->cid_mask);
249 * If we found a correct PnP setting,
250 * we first try it.
252 if (pnp_succeeded) {
253 memset(&info, 0, sizeof(chipio_t));
254 info.cfg_base = cfg_base;
255 info.fir_base = pnp_info.fir_base;
256 info.dma = pnp_info.dma;
257 info.irq = pnp_info.irq;
259 if (info.fir_base < 0x2000) {
260 IRDA_MESSAGE("%s, chip->init\n", driver_name);
261 chip->init(chip, &info);
262 } else
263 chip->probe(chip, &info);
265 if (nsc_ircc_open(&info) >= 0)
266 ret = 0;
270 * Opening based on PnP values failed.
271 * Let's fallback to user values, or probe
272 * the chip.
274 if (ret) {
275 IRDA_DEBUG(2, "%s, PnP init failed\n", driver_name);
276 memset(&info, 0, sizeof(chipio_t));
277 info.cfg_base = cfg_base;
278 info.fir_base = io[i];
279 info.dma = dma[i];
280 info.irq = irq[i];
283 * If the user supplies the base address, then
284 * we init the chip, if not we probe the values
285 * set by the BIOS
287 if (io[i] < 0x2000) {
288 chip->init(chip, &info);
289 } else
290 chip->probe(chip, &info);
292 if (nsc_ircc_open(&info) >= 0)
293 ret = 0;
295 i++;
296 } else {
297 IRDA_DEBUG(2, "%s(), Wrong chip id=0x%02x\n", __FUNCTION__, id);
302 if (ret) {
303 platform_driver_unregister(&nsc_ircc_driver);
304 pnp_unregister_driver(&nsc_ircc_pnp_driver);
305 pnp_registered = 0;
308 return ret;
312 * Function nsc_ircc_cleanup ()
314 * Close all configured chips
317 static void __exit nsc_ircc_cleanup(void)
319 int i;
321 for (i = 0; i < ARRAY_SIZE(dev_self); i++) {
322 if (dev_self[i])
323 nsc_ircc_close(dev_self[i]);
326 platform_driver_unregister(&nsc_ircc_driver);
328 if (pnp_registered)
329 pnp_unregister_driver(&nsc_ircc_pnp_driver);
331 pnp_registered = 0;
335 * Function nsc_ircc_open (iobase, irq)
337 * Open driver instance
340 static int __init nsc_ircc_open(chipio_t *info)
342 struct net_device *dev;
343 struct nsc_ircc_cb *self;
344 void *ret;
345 int err, chip_index;
347 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
350 for (chip_index = 0; chip_index < ARRAY_SIZE(dev_self); chip_index++) {
351 if (!dev_self[chip_index])
352 break;
355 if (chip_index == ARRAY_SIZE(dev_self)) {
356 IRDA_ERROR("%s(), maximum number of supported chips reached!\n", __FUNCTION__);
357 return -ENOMEM;
360 IRDA_MESSAGE("%s, Found chip at base=0x%03x\n", driver_name,
361 info->cfg_base);
363 if ((nsc_ircc_setup(info)) == -1)
364 return -1;
366 IRDA_MESSAGE("%s, driver loaded (Dag Brattli)\n", driver_name);
368 dev = alloc_irdadev(sizeof(struct nsc_ircc_cb));
369 if (dev == NULL) {
370 IRDA_ERROR("%s(), can't allocate memory for "
371 "control block!\n", __FUNCTION__);
372 return -ENOMEM;
375 self = dev->priv;
376 self->netdev = dev;
377 spin_lock_init(&self->lock);
379 /* Need to store self somewhere */
380 dev_self[chip_index] = self;
381 self->index = chip_index;
383 /* Initialize IO */
384 self->io.cfg_base = info->cfg_base;
385 self->io.fir_base = info->fir_base;
386 self->io.irq = info->irq;
387 self->io.fir_ext = CHIP_IO_EXTENT;
388 self->io.dma = info->dma;
389 self->io.fifo_size = 32;
391 /* Reserve the ioports that we need */
392 ret = request_region(self->io.fir_base, self->io.fir_ext, driver_name);
393 if (!ret) {
394 IRDA_WARNING("%s(), can't get iobase of 0x%03x\n",
395 __FUNCTION__, self->io.fir_base);
396 err = -ENODEV;
397 goto out1;
400 /* Initialize QoS for this device */
401 irda_init_max_qos_capabilies(&self->qos);
403 /* The only value we must override it the baudrate */
404 self->qos.baud_rate.bits = IR_9600|IR_19200|IR_38400|IR_57600|
405 IR_115200|IR_576000|IR_1152000 |(IR_4000000 << 8);
407 self->qos.min_turn_time.bits = qos_mtt_bits;
408 irda_qos_bits_to_value(&self->qos);
410 /* Max DMA buffer size needed = (data_size + 6) * (window_size) + 6; */
411 self->rx_buff.truesize = 14384;
412 self->tx_buff.truesize = 14384;
414 /* Allocate memory if needed */
415 self->rx_buff.head =
416 dma_alloc_coherent(NULL, self->rx_buff.truesize,
417 &self->rx_buff_dma, GFP_KERNEL);
418 if (self->rx_buff.head == NULL) {
419 err = -ENOMEM;
420 goto out2;
423 memset(self->rx_buff.head, 0, self->rx_buff.truesize);
425 self->tx_buff.head =
426 dma_alloc_coherent(NULL, self->tx_buff.truesize,
427 &self->tx_buff_dma, GFP_KERNEL);
428 if (self->tx_buff.head == NULL) {
429 err = -ENOMEM;
430 goto out3;
432 memset(self->tx_buff.head, 0, self->tx_buff.truesize);
434 self->rx_buff.in_frame = FALSE;
435 self->rx_buff.state = OUTSIDE_FRAME;
436 self->tx_buff.data = self->tx_buff.head;
437 self->rx_buff.data = self->rx_buff.head;
439 /* Reset Tx queue info */
440 self->tx_fifo.len = self->tx_fifo.ptr = self->tx_fifo.free = 0;
441 self->tx_fifo.tail = self->tx_buff.head;
443 /* Override the network functions we need to use */
444 dev->hard_start_xmit = nsc_ircc_hard_xmit_sir;
445 dev->open = nsc_ircc_net_open;
446 dev->stop = nsc_ircc_net_close;
447 dev->do_ioctl = nsc_ircc_net_ioctl;
448 dev->get_stats = nsc_ircc_net_get_stats;
450 err = register_netdev(dev);
451 if (err) {
452 IRDA_ERROR("%s(), register_netdev() failed!\n", __FUNCTION__);
453 goto out4;
455 IRDA_MESSAGE("IrDA: Registered device %s\n", dev->name);
457 /* Check if user has supplied a valid dongle id or not */
458 if ((dongle_id <= 0) ||
459 (dongle_id >= ARRAY_SIZE(dongle_types))) {
460 dongle_id = nsc_ircc_read_dongle_id(self->io.fir_base);
462 IRDA_MESSAGE("%s, Found dongle: %s\n", driver_name,
463 dongle_types[dongle_id]);
464 } else {
465 IRDA_MESSAGE("%s, Using dongle: %s\n", driver_name,
466 dongle_types[dongle_id]);
469 self->io.dongle_id = dongle_id;
470 nsc_ircc_init_dongle_interface(self->io.fir_base, dongle_id);
472 self->pldev = platform_device_register_simple(NSC_IRCC_DRIVER_NAME,
473 self->index, NULL, 0);
474 if (IS_ERR(self->pldev)) {
475 err = PTR_ERR(self->pldev);
476 goto out5;
478 platform_set_drvdata(self->pldev, self);
480 return chip_index;
482 out5:
483 unregister_netdev(dev);
484 out4:
485 dma_free_coherent(NULL, self->tx_buff.truesize,
486 self->tx_buff.head, self->tx_buff_dma);
487 out3:
488 dma_free_coherent(NULL, self->rx_buff.truesize,
489 self->rx_buff.head, self->rx_buff_dma);
490 out2:
491 release_region(self->io.fir_base, self->io.fir_ext);
492 out1:
493 free_netdev(dev);
494 dev_self[chip_index] = NULL;
495 return err;
499 * Function nsc_ircc_close (self)
501 * Close driver instance
504 static int __exit nsc_ircc_close(struct nsc_ircc_cb *self)
506 int iobase;
508 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
510 IRDA_ASSERT(self != NULL, return -1;);
512 iobase = self->io.fir_base;
514 platform_device_unregister(self->pldev);
516 /* Remove netdevice */
517 unregister_netdev(self->netdev);
519 /* Release the PORT that this driver is using */
520 IRDA_DEBUG(4, "%s(), Releasing Region %03x\n",
521 __FUNCTION__, self->io.fir_base);
522 release_region(self->io.fir_base, self->io.fir_ext);
524 if (self->tx_buff.head)
525 dma_free_coherent(NULL, self->tx_buff.truesize,
526 self->tx_buff.head, self->tx_buff_dma);
528 if (self->rx_buff.head)
529 dma_free_coherent(NULL, self->rx_buff.truesize,
530 self->rx_buff.head, self->rx_buff_dma);
532 dev_self[self->index] = NULL;
533 free_netdev(self->netdev);
535 return 0;
539 * Function nsc_ircc_init_108 (iobase, cfg_base, irq, dma)
541 * Initialize the NSC '108 chip
544 static int nsc_ircc_init_108(nsc_chip_t *chip, chipio_t *info)
546 int cfg_base = info->cfg_base;
547 __u8 temp=0;
549 outb(2, cfg_base); /* Mode Control Register (MCTL) */
550 outb(0x00, cfg_base+1); /* Disable device */
552 /* Base Address and Interrupt Control Register (BAIC) */
553 outb(CFG_108_BAIC, cfg_base);
554 switch (info->fir_base) {
555 case 0x3e8: outb(0x14, cfg_base+1); break;
556 case 0x2e8: outb(0x15, cfg_base+1); break;
557 case 0x3f8: outb(0x16, cfg_base+1); break;
558 case 0x2f8: outb(0x17, cfg_base+1); break;
559 default: IRDA_ERROR("%s(), invalid base_address", __FUNCTION__);
562 /* Control Signal Routing Register (CSRT) */
563 switch (info->irq) {
564 case 3: temp = 0x01; break;
565 case 4: temp = 0x02; break;
566 case 5: temp = 0x03; break;
567 case 7: temp = 0x04; break;
568 case 9: temp = 0x05; break;
569 case 11: temp = 0x06; break;
570 case 15: temp = 0x07; break;
571 default: IRDA_ERROR("%s(), invalid irq", __FUNCTION__);
573 outb(CFG_108_CSRT, cfg_base);
575 switch (info->dma) {
576 case 0: outb(0x08+temp, cfg_base+1); break;
577 case 1: outb(0x10+temp, cfg_base+1); break;
578 case 3: outb(0x18+temp, cfg_base+1); break;
579 default: IRDA_ERROR("%s(), invalid dma", __FUNCTION__);
582 outb(CFG_108_MCTL, cfg_base); /* Mode Control Register (MCTL) */
583 outb(0x03, cfg_base+1); /* Enable device */
585 return 0;
589 * Function nsc_ircc_probe_108 (chip, info)
594 static int nsc_ircc_probe_108(nsc_chip_t *chip, chipio_t *info)
596 int cfg_base = info->cfg_base;
597 int reg;
599 /* Read address and interrupt control register (BAIC) */
600 outb(CFG_108_BAIC, cfg_base);
601 reg = inb(cfg_base+1);
603 switch (reg & 0x03) {
604 case 0:
605 info->fir_base = 0x3e8;
606 break;
607 case 1:
608 info->fir_base = 0x2e8;
609 break;
610 case 2:
611 info->fir_base = 0x3f8;
612 break;
613 case 3:
614 info->fir_base = 0x2f8;
615 break;
617 info->sir_base = info->fir_base;
618 IRDA_DEBUG(2, "%s(), probing fir_base=0x%03x\n", __FUNCTION__,
619 info->fir_base);
621 /* Read control signals routing register (CSRT) */
622 outb(CFG_108_CSRT, cfg_base);
623 reg = inb(cfg_base+1);
625 switch (reg & 0x07) {
626 case 0:
627 info->irq = -1;
628 break;
629 case 1:
630 info->irq = 3;
631 break;
632 case 2:
633 info->irq = 4;
634 break;
635 case 3:
636 info->irq = 5;
637 break;
638 case 4:
639 info->irq = 7;
640 break;
641 case 5:
642 info->irq = 9;
643 break;
644 case 6:
645 info->irq = 11;
646 break;
647 case 7:
648 info->irq = 15;
649 break;
651 IRDA_DEBUG(2, "%s(), probing irq=%d\n", __FUNCTION__, info->irq);
653 /* Currently we only read Rx DMA but it will also be used for Tx */
654 switch ((reg >> 3) & 0x03) {
655 case 0:
656 info->dma = -1;
657 break;
658 case 1:
659 info->dma = 0;
660 break;
661 case 2:
662 info->dma = 1;
663 break;
664 case 3:
665 info->dma = 3;
666 break;
668 IRDA_DEBUG(2, "%s(), probing dma=%d\n", __FUNCTION__, info->dma);
670 /* Read mode control register (MCTL) */
671 outb(CFG_108_MCTL, cfg_base);
672 reg = inb(cfg_base+1);
674 info->enabled = reg & 0x01;
675 info->suspended = !((reg >> 1) & 0x01);
677 return 0;
681 * Function nsc_ircc_init_338 (chip, info)
683 * Initialize the NSC '338 chip. Remember that the 87338 needs two
684 * consecutive writes to the data registers while CPU interrupts are
685 * disabled. The 97338 does not require this, but shouldn't be any
686 * harm if we do it anyway.
688 static int nsc_ircc_init_338(nsc_chip_t *chip, chipio_t *info)
690 /* No init yet */
692 return 0;
696 * Function nsc_ircc_probe_338 (chip, info)
701 static int nsc_ircc_probe_338(nsc_chip_t *chip, chipio_t *info)
703 int cfg_base = info->cfg_base;
704 int reg, com = 0;
705 int pnp;
707 /* Read funtion enable register (FER) */
708 outb(CFG_338_FER, cfg_base);
709 reg = inb(cfg_base+1);
711 info->enabled = (reg >> 2) & 0x01;
713 /* Check if we are in Legacy or PnP mode */
714 outb(CFG_338_PNP0, cfg_base);
715 reg = inb(cfg_base+1);
717 pnp = (reg >> 3) & 0x01;
718 if (pnp) {
719 IRDA_DEBUG(2, "(), Chip is in PnP mode\n");
720 outb(0x46, cfg_base);
721 reg = (inb(cfg_base+1) & 0xfe) << 2;
723 outb(0x47, cfg_base);
724 reg |= ((inb(cfg_base+1) & 0xfc) << 8);
726 info->fir_base = reg;
727 } else {
728 /* Read function address register (FAR) */
729 outb(CFG_338_FAR, cfg_base);
730 reg = inb(cfg_base+1);
732 switch ((reg >> 4) & 0x03) {
733 case 0:
734 info->fir_base = 0x3f8;
735 break;
736 case 1:
737 info->fir_base = 0x2f8;
738 break;
739 case 2:
740 com = 3;
741 break;
742 case 3:
743 com = 4;
744 break;
747 if (com) {
748 switch ((reg >> 6) & 0x03) {
749 case 0:
750 if (com == 3)
751 info->fir_base = 0x3e8;
752 else
753 info->fir_base = 0x2e8;
754 break;
755 case 1:
756 if (com == 3)
757 info->fir_base = 0x338;
758 else
759 info->fir_base = 0x238;
760 break;
761 case 2:
762 if (com == 3)
763 info->fir_base = 0x2e8;
764 else
765 info->fir_base = 0x2e0;
766 break;
767 case 3:
768 if (com == 3)
769 info->fir_base = 0x220;
770 else
771 info->fir_base = 0x228;
772 break;
776 info->sir_base = info->fir_base;
778 /* Read PnP register 1 (PNP1) */
779 outb(CFG_338_PNP1, cfg_base);
780 reg = inb(cfg_base+1);
782 info->irq = reg >> 4;
784 /* Read PnP register 3 (PNP3) */
785 outb(CFG_338_PNP3, cfg_base);
786 reg = inb(cfg_base+1);
788 info->dma = (reg & 0x07) - 1;
790 /* Read power and test register (PTR) */
791 outb(CFG_338_PTR, cfg_base);
792 reg = inb(cfg_base+1);
794 info->suspended = reg & 0x01;
796 return 0;
801 * Function nsc_ircc_init_39x (chip, info)
803 * Now that we know it's a '39x (see probe below), we need to
804 * configure it so we can use it.
806 * The NSC '338 chip is a Super I/O chip with a "bank" architecture,
807 * the configuration of the different functionality (serial, parallel,
808 * floppy...) are each in a different bank (Logical Device Number).
809 * The base address, irq and dma configuration registers are common
810 * to all functionalities (index 0x30 to 0x7F).
811 * There is only one configuration register specific to the
812 * serial port, CFG_39X_SPC.
813 * JeanII
815 * Note : this code was written by Jan Frey <janfrey@web.de>
817 static int nsc_ircc_init_39x(nsc_chip_t *chip, chipio_t *info)
819 int cfg_base = info->cfg_base;
820 int enabled;
822 /* User is sure about his config... accept it. */
823 IRDA_DEBUG(2, "%s(): nsc_ircc_init_39x (user settings): "
824 "io=0x%04x, irq=%d, dma=%d\n",
825 __FUNCTION__, info->fir_base, info->irq, info->dma);
827 /* Access bank for SP2 */
828 outb(CFG_39X_LDN, cfg_base);
829 outb(0x02, cfg_base+1);
831 /* Configure SP2 */
833 /* We want to enable the device if not enabled */
834 outb(CFG_39X_ACT, cfg_base);
835 enabled = inb(cfg_base+1) & 0x01;
837 if (!enabled) {
838 /* Enable the device */
839 outb(CFG_39X_SIOCF1, cfg_base);
840 outb(0x01, cfg_base+1);
841 /* May want to update info->enabled. Jean II */
844 /* Enable UART bank switching (bit 7) ; Sets the chip to normal
845 * power mode (wake up from sleep mode) (bit 1) */
846 outb(CFG_39X_SPC, cfg_base);
847 outb(0x82, cfg_base+1);
849 return 0;
853 * Function nsc_ircc_probe_39x (chip, info)
855 * Test if we really have a '39x chip at the given address
857 * Note : this code was written by Jan Frey <janfrey@web.de>
859 static int nsc_ircc_probe_39x(nsc_chip_t *chip, chipio_t *info)
861 int cfg_base = info->cfg_base;
862 int reg1, reg2, irq, irqt, dma1, dma2;
863 int enabled, susp;
865 IRDA_DEBUG(2, "%s(), nsc_ircc_probe_39x, base=%d\n",
866 __FUNCTION__, cfg_base);
868 /* This function should be executed with irq off to avoid
869 * another driver messing with the Super I/O bank - Jean II */
871 /* Access bank for SP2 */
872 outb(CFG_39X_LDN, cfg_base);
873 outb(0x02, cfg_base+1);
875 /* Read infos about SP2 ; store in info struct */
876 outb(CFG_39X_BASEH, cfg_base);
877 reg1 = inb(cfg_base+1);
878 outb(CFG_39X_BASEL, cfg_base);
879 reg2 = inb(cfg_base+1);
880 info->fir_base = (reg1 << 8) | reg2;
882 outb(CFG_39X_IRQNUM, cfg_base);
883 irq = inb(cfg_base+1);
884 outb(CFG_39X_IRQSEL, cfg_base);
885 irqt = inb(cfg_base+1);
886 info->irq = irq;
888 outb(CFG_39X_DMA0, cfg_base);
889 dma1 = inb(cfg_base+1);
890 outb(CFG_39X_DMA1, cfg_base);
891 dma2 = inb(cfg_base+1);
892 info->dma = dma1 -1;
894 outb(CFG_39X_ACT, cfg_base);
895 info->enabled = enabled = inb(cfg_base+1) & 0x01;
897 outb(CFG_39X_SPC, cfg_base);
898 susp = 1 - ((inb(cfg_base+1) & 0x02) >> 1);
900 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);
902 /* Configure SP2 */
904 /* We want to enable the device if not enabled */
905 outb(CFG_39X_ACT, cfg_base);
906 enabled = inb(cfg_base+1) & 0x01;
908 if (!enabled) {
909 /* Enable the device */
910 outb(CFG_39X_SIOCF1, cfg_base);
911 outb(0x01, cfg_base+1);
912 /* May want to update info->enabled. Jean II */
915 /* Enable UART bank switching (bit 7) ; Sets the chip to normal
916 * power mode (wake up from sleep mode) (bit 1) */
917 outb(CFG_39X_SPC, cfg_base);
918 outb(0x82, cfg_base+1);
920 return 0;
923 #ifdef CONFIG_PNP
924 /* PNP probing */
925 static int nsc_ircc_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *id)
927 memset(&pnp_info, 0, sizeof(chipio_t));
928 pnp_info.irq = -1;
929 pnp_info.dma = -1;
930 pnp_succeeded = 1;
932 /* There don't seem to be any way to get the cfg_base.
933 * On my box, cfg_base is in the PnP descriptor of the
934 * motherboard. Oh well... Jean II */
936 if (pnp_port_valid(dev, 0) &&
937 !(pnp_port_flags(dev, 0) & IORESOURCE_DISABLED))
938 pnp_info.fir_base = pnp_port_start(dev, 0);
940 if (pnp_irq_valid(dev, 0) &&
941 !(pnp_irq_flags(dev, 0) & IORESOURCE_DISABLED))
942 pnp_info.irq = pnp_irq(dev, 0);
944 if (pnp_dma_valid(dev, 0) &&
945 !(pnp_dma_flags(dev, 0) & IORESOURCE_DISABLED))
946 pnp_info.dma = pnp_dma(dev, 0);
948 IRDA_DEBUG(0, "%s() : From PnP, found firbase 0x%03X ; irq %d ; dma %d.\n",
949 __FUNCTION__, pnp_info.fir_base, pnp_info.irq, pnp_info.dma);
951 if((pnp_info.fir_base == 0) ||
952 (pnp_info.irq == -1) || (pnp_info.dma == -1)) {
953 /* Returning an error will disable the device. Yuck ! */
954 //return -EINVAL;
955 pnp_succeeded = 0;
958 return 0;
960 #endif
963 * Function nsc_ircc_setup (info)
965 * Returns non-negative on success.
968 static int nsc_ircc_setup(chipio_t *info)
970 int version;
971 int iobase = info->fir_base;
973 /* Read the Module ID */
974 switch_bank(iobase, BANK3);
975 version = inb(iobase+MID);
977 IRDA_DEBUG(2, "%s() Driver %s Found chip version %02x\n",
978 __FUNCTION__, driver_name, version);
980 /* Should be 0x2? */
981 if (0x20 != (version & 0xf0)) {
982 IRDA_ERROR("%s, Wrong chip version %02x\n",
983 driver_name, version);
984 return -1;
987 /* Switch to advanced mode */
988 switch_bank(iobase, BANK2);
989 outb(ECR1_EXT_SL, iobase+ECR1);
990 switch_bank(iobase, BANK0);
992 /* Set FIFO threshold to TX17, RX16, reset and enable FIFO's */
993 switch_bank(iobase, BANK0);
994 outb(FCR_RXTH|FCR_TXTH|FCR_TXSR|FCR_RXSR|FCR_FIFO_EN, iobase+FCR);
996 outb(0x03, iobase+LCR); /* 8 bit word length */
997 outb(MCR_SIR, iobase+MCR); /* Start at SIR-mode, also clears LSR*/
999 /* Set FIFO size to 32 */
1000 switch_bank(iobase, BANK2);
1001 outb(EXCR2_RFSIZ|EXCR2_TFSIZ, iobase+EXCR2);
1003 /* IRCR2: FEND_MD is not set */
1004 switch_bank(iobase, BANK5);
1005 outb(0x02, iobase+4);
1007 /* Make sure that some defaults are OK */
1008 switch_bank(iobase, BANK6);
1009 outb(0x20, iobase+0); /* Set 32 bits FIR CRC */
1010 outb(0x0a, iobase+1); /* Set MIR pulse width */
1011 outb(0x0d, iobase+2); /* Set SIR pulse width to 1.6us */
1012 outb(0x2a, iobase+4); /* Set beginning frag, and preamble length */
1014 /* Enable receive interrupts */
1015 switch_bank(iobase, BANK0);
1016 outb(IER_RXHDL_IE, iobase+IER);
1018 return 0;
1022 * Function nsc_ircc_read_dongle_id (void)
1024 * Try to read dongle indentification. This procedure needs to be executed
1025 * once after power-on/reset. It also needs to be used whenever you suspect
1026 * that the user may have plugged/unplugged the IrDA Dongle.
1028 static int nsc_ircc_read_dongle_id (int iobase)
1030 int dongle_id;
1031 __u8 bank;
1033 bank = inb(iobase+BSR);
1035 /* Select Bank 7 */
1036 switch_bank(iobase, BANK7);
1038 /* IRCFG4: IRSL0_DS and IRSL21_DS are cleared */
1039 outb(0x00, iobase+7);
1041 /* ID0, 1, and 2 are pulled up/down very slowly */
1042 udelay(50);
1044 /* IRCFG1: read the ID bits */
1045 dongle_id = inb(iobase+4) & 0x0f;
1047 #ifdef BROKEN_DONGLE_ID
1048 if (dongle_id == 0x0a)
1049 dongle_id = 0x09;
1050 #endif
1051 /* Go back to bank 0 before returning */
1052 switch_bank(iobase, BANK0);
1054 outb(bank, iobase+BSR);
1056 return dongle_id;
1060 * Function nsc_ircc_init_dongle_interface (iobase, dongle_id)
1062 * This function initializes the dongle for the transceiver that is
1063 * used. This procedure needs to be executed once after
1064 * power-on/reset. It also needs to be used whenever you suspect that
1065 * the dongle is changed.
1067 static void nsc_ircc_init_dongle_interface (int iobase, int dongle_id)
1069 int bank;
1071 /* Save current bank */
1072 bank = inb(iobase+BSR);
1074 /* Select Bank 7 */
1075 switch_bank(iobase, BANK7);
1077 /* IRCFG4: set according to dongle_id */
1078 switch (dongle_id) {
1079 case 0x00: /* same as */
1080 case 0x01: /* Differential serial interface */
1081 IRDA_DEBUG(0, "%s(), %s not defined by irda yet\n",
1082 __FUNCTION__, dongle_types[dongle_id]);
1083 break;
1084 case 0x02: /* same as */
1085 case 0x03: /* Reserved */
1086 IRDA_DEBUG(0, "%s(), %s not defined by irda yet\n",
1087 __FUNCTION__, dongle_types[dongle_id]);
1088 break;
1089 case 0x04: /* Sharp RY5HD01 */
1090 break;
1091 case 0x05: /* Reserved, but this is what the Thinkpad reports */
1092 IRDA_DEBUG(0, "%s(), %s not defined by irda yet\n",
1093 __FUNCTION__, dongle_types[dongle_id]);
1094 break;
1095 case 0x06: /* Single-ended serial interface */
1096 IRDA_DEBUG(0, "%s(), %s not defined by irda yet\n",
1097 __FUNCTION__, dongle_types[dongle_id]);
1098 break;
1099 case 0x07: /* Consumer-IR only */
1100 IRDA_DEBUG(0, "%s(), %s is not for IrDA mode\n",
1101 __FUNCTION__, dongle_types[dongle_id]);
1102 break;
1103 case 0x08: /* HP HSDL-2300, HP HSDL-3600/HSDL-3610 */
1104 IRDA_DEBUG(0, "%s(), %s\n",
1105 __FUNCTION__, dongle_types[dongle_id]);
1106 break;
1107 case 0x09: /* IBM31T1100 or Temic TFDS6000/TFDS6500 */
1108 outb(0x28, iobase+7); /* Set irsl[0-2] as output */
1109 break;
1110 case 0x0A: /* same as */
1111 case 0x0B: /* Reserved */
1112 IRDA_DEBUG(0, "%s(), %s not defined by irda yet\n",
1113 __FUNCTION__, dongle_types[dongle_id]);
1114 break;
1115 case 0x0C: /* same as */
1116 case 0x0D: /* HP HSDL-1100/HSDL-2100 */
1118 * Set irsl0 as input, irsl[1-2] as output, and separate
1119 * inputs are used for SIR and MIR/FIR
1121 outb(0x48, iobase+7);
1122 break;
1123 case 0x0E: /* Supports SIR Mode only */
1124 outb(0x28, iobase+7); /* Set irsl[0-2] as output */
1125 break;
1126 case 0x0F: /* No dongle connected */
1127 IRDA_DEBUG(0, "%s(), %s\n",
1128 __FUNCTION__, dongle_types[dongle_id]);
1130 switch_bank(iobase, BANK0);
1131 outb(0x62, iobase+MCR);
1132 break;
1133 default:
1134 IRDA_DEBUG(0, "%s(), invalid dongle_id %#x",
1135 __FUNCTION__, dongle_id);
1138 /* IRCFG1: IRSL1 and 2 are set to IrDA mode */
1139 outb(0x00, iobase+4);
1141 /* Restore bank register */
1142 outb(bank, iobase+BSR);
1144 } /* set_up_dongle_interface */
1147 * Function nsc_ircc_change_dongle_speed (iobase, speed, dongle_id)
1149 * Change speed of the attach dongle
1152 static void nsc_ircc_change_dongle_speed(int iobase, int speed, int dongle_id)
1154 __u8 bank;
1156 /* Save current bank */
1157 bank = inb(iobase+BSR);
1159 /* Select Bank 7 */
1160 switch_bank(iobase, BANK7);
1162 /* IRCFG1: set according to dongle_id */
1163 switch (dongle_id) {
1164 case 0x00: /* same as */
1165 case 0x01: /* Differential serial interface */
1166 IRDA_DEBUG(0, "%s(), %s not defined by irda yet\n",
1167 __FUNCTION__, dongle_types[dongle_id]);
1168 break;
1169 case 0x02: /* same as */
1170 case 0x03: /* Reserved */
1171 IRDA_DEBUG(0, "%s(), %s not defined by irda yet\n",
1172 __FUNCTION__, dongle_types[dongle_id]);
1173 break;
1174 case 0x04: /* Sharp RY5HD01 */
1175 break;
1176 case 0x05: /* Reserved */
1177 IRDA_DEBUG(0, "%s(), %s not defined by irda yet\n",
1178 __FUNCTION__, dongle_types[dongle_id]);
1179 break;
1180 case 0x06: /* Single-ended serial interface */
1181 IRDA_DEBUG(0, "%s(), %s not defined by irda yet\n",
1182 __FUNCTION__, dongle_types[dongle_id]);
1183 break;
1184 case 0x07: /* Consumer-IR only */
1185 IRDA_DEBUG(0, "%s(), %s is not for IrDA mode\n",
1186 __FUNCTION__, dongle_types[dongle_id]);
1187 break;
1188 case 0x08: /* HP HSDL-2300, HP HSDL-3600/HSDL-3610 */
1189 IRDA_DEBUG(0, "%s(), %s\n",
1190 __FUNCTION__, dongle_types[dongle_id]);
1191 outb(0x00, iobase+4);
1192 if (speed > 115200)
1193 outb(0x01, iobase+4);
1194 break;
1195 case 0x09: /* IBM31T1100 or Temic TFDS6000/TFDS6500 */
1196 outb(0x01, iobase+4);
1198 if (speed == 4000000) {
1199 /* There was a cli() there, but we now are already
1200 * under spin_lock_irqsave() - JeanII */
1201 outb(0x81, iobase+4);
1202 outb(0x80, iobase+4);
1203 } else
1204 outb(0x00, iobase+4);
1205 break;
1206 case 0x0A: /* same as */
1207 case 0x0B: /* Reserved */
1208 IRDA_DEBUG(0, "%s(), %s not defined by irda yet\n",
1209 __FUNCTION__, dongle_types[dongle_id]);
1210 break;
1211 case 0x0C: /* same as */
1212 case 0x0D: /* HP HSDL-1100/HSDL-2100 */
1213 break;
1214 case 0x0E: /* Supports SIR Mode only */
1215 break;
1216 case 0x0F: /* No dongle connected */
1217 IRDA_DEBUG(0, "%s(), %s is not for IrDA mode\n",
1218 __FUNCTION__, dongle_types[dongle_id]);
1220 switch_bank(iobase, BANK0);
1221 outb(0x62, iobase+MCR);
1222 break;
1223 default:
1224 IRDA_DEBUG(0, "%s(), invalid data_rate\n", __FUNCTION__);
1226 /* Restore bank register */
1227 outb(bank, iobase+BSR);
1231 * Function nsc_ircc_change_speed (self, baud)
1233 * Change the speed of the device
1235 * This function *must* be called with irq off and spin-lock.
1237 static __u8 nsc_ircc_change_speed(struct nsc_ircc_cb *self, __u32 speed)
1239 struct net_device *dev = self->netdev;
1240 __u8 mcr = MCR_SIR;
1241 int iobase;
1242 __u8 bank;
1243 __u8 ier; /* Interrupt enable register */
1245 IRDA_DEBUG(2, "%s(), speed=%d\n", __FUNCTION__, speed);
1247 IRDA_ASSERT(self != NULL, return 0;);
1249 iobase = self->io.fir_base;
1251 /* Update accounting for new speed */
1252 self->io.speed = speed;
1254 /* Save current bank */
1255 bank = inb(iobase+BSR);
1257 /* Disable interrupts */
1258 switch_bank(iobase, BANK0);
1259 outb(0, iobase+IER);
1261 /* Select Bank 2 */
1262 switch_bank(iobase, BANK2);
1264 outb(0x00, iobase+BGDH);
1265 switch (speed) {
1266 case 9600: outb(0x0c, iobase+BGDL); break;
1267 case 19200: outb(0x06, iobase+BGDL); break;
1268 case 38400: outb(0x03, iobase+BGDL); break;
1269 case 57600: outb(0x02, iobase+BGDL); break;
1270 case 115200: outb(0x01, iobase+BGDL); break;
1271 case 576000:
1272 switch_bank(iobase, BANK5);
1274 /* IRCR2: MDRS is set */
1275 outb(inb(iobase+4) | 0x04, iobase+4);
1277 mcr = MCR_MIR;
1278 IRDA_DEBUG(0, "%s(), handling baud of 576000\n", __FUNCTION__);
1279 break;
1280 case 1152000:
1281 mcr = MCR_MIR;
1282 IRDA_DEBUG(0, "%s(), handling baud of 1152000\n", __FUNCTION__);
1283 break;
1284 case 4000000:
1285 mcr = MCR_FIR;
1286 IRDA_DEBUG(0, "%s(), handling baud of 4000000\n", __FUNCTION__);
1287 break;
1288 default:
1289 mcr = MCR_FIR;
1290 IRDA_DEBUG(0, "%s(), unknown baud rate of %d\n",
1291 __FUNCTION__, speed);
1292 break;
1295 /* Set appropriate speed mode */
1296 switch_bank(iobase, BANK0);
1297 outb(mcr | MCR_TX_DFR, iobase+MCR);
1299 /* Give some hits to the transceiver */
1300 nsc_ircc_change_dongle_speed(iobase, speed, self->io.dongle_id);
1302 /* Set FIFO threshold to TX17, RX16 */
1303 switch_bank(iobase, BANK0);
1304 outb(0x00, iobase+FCR);
1305 outb(FCR_FIFO_EN, iobase+FCR);
1306 outb(FCR_RXTH| /* Set Rx FIFO threshold */
1307 FCR_TXTH| /* Set Tx FIFO threshold */
1308 FCR_TXSR| /* Reset Tx FIFO */
1309 FCR_RXSR| /* Reset Rx FIFO */
1310 FCR_FIFO_EN, /* Enable FIFOs */
1311 iobase+FCR);
1313 /* Set FIFO size to 32 */
1314 switch_bank(iobase, BANK2);
1315 outb(EXCR2_RFSIZ|EXCR2_TFSIZ, iobase+EXCR2);
1317 /* Enable some interrupts so we can receive frames */
1318 switch_bank(iobase, BANK0);
1319 if (speed > 115200) {
1320 /* Install FIR xmit handler */
1321 dev->hard_start_xmit = nsc_ircc_hard_xmit_fir;
1322 ier = IER_SFIF_IE;
1323 nsc_ircc_dma_receive(self);
1324 } else {
1325 /* Install SIR xmit handler */
1326 dev->hard_start_xmit = nsc_ircc_hard_xmit_sir;
1327 ier = IER_RXHDL_IE;
1329 /* Set our current interrupt mask */
1330 outb(ier, iobase+IER);
1332 /* Restore BSR */
1333 outb(bank, iobase+BSR);
1335 /* Make sure interrupt handlers keep the proper interrupt mask */
1336 return(ier);
1340 * Function nsc_ircc_hard_xmit (skb, dev)
1342 * Transmit the frame!
1345 static int nsc_ircc_hard_xmit_sir(struct sk_buff *skb, struct net_device *dev)
1347 struct nsc_ircc_cb *self;
1348 unsigned long flags;
1349 int iobase;
1350 __s32 speed;
1351 __u8 bank;
1353 self = (struct nsc_ircc_cb *) dev->priv;
1355 IRDA_ASSERT(self != NULL, return 0;);
1357 iobase = self->io.fir_base;
1359 netif_stop_queue(dev);
1361 /* Make sure tests *& speed change are atomic */
1362 spin_lock_irqsave(&self->lock, flags);
1364 /* Check if we need to change the speed */
1365 speed = irda_get_next_speed(skb);
1366 if ((speed != self->io.speed) && (speed != -1)) {
1367 /* Check for empty frame. */
1368 if (!skb->len) {
1369 /* If we just sent a frame, we get called before
1370 * the last bytes get out (because of the SIR FIFO).
1371 * If this is the case, let interrupt handler change
1372 * the speed itself... Jean II */
1373 if (self->io.direction == IO_RECV) {
1374 nsc_ircc_change_speed(self, speed);
1375 /* TODO : For SIR->SIR, the next packet
1376 * may get corrupted - Jean II */
1377 netif_wake_queue(dev);
1378 } else {
1379 self->new_speed = speed;
1380 /* Queue will be restarted after speed change
1381 * to make sure packets gets through the
1382 * proper xmit handler - Jean II */
1384 dev->trans_start = jiffies;
1385 spin_unlock_irqrestore(&self->lock, flags);
1386 dev_kfree_skb(skb);
1387 return 0;
1388 } else
1389 self->new_speed = speed;
1392 /* Save current bank */
1393 bank = inb(iobase+BSR);
1395 self->tx_buff.data = self->tx_buff.head;
1397 self->tx_buff.len = async_wrap_skb(skb, self->tx_buff.data,
1398 self->tx_buff.truesize);
1400 self->stats.tx_bytes += self->tx_buff.len;
1402 /* Add interrupt on tx low level (will fire immediately) */
1403 switch_bank(iobase, BANK0);
1404 outb(IER_TXLDL_IE, iobase+IER);
1406 /* Restore bank register */
1407 outb(bank, iobase+BSR);
1409 dev->trans_start = jiffies;
1410 spin_unlock_irqrestore(&self->lock, flags);
1412 dev_kfree_skb(skb);
1414 return 0;
1417 static int nsc_ircc_hard_xmit_fir(struct sk_buff *skb, struct net_device *dev)
1419 struct nsc_ircc_cb *self;
1420 unsigned long flags;
1421 int iobase;
1422 __s32 speed;
1423 __u8 bank;
1424 int mtt, diff;
1426 self = (struct nsc_ircc_cb *) dev->priv;
1427 iobase = self->io.fir_base;
1429 netif_stop_queue(dev);
1431 /* Make sure tests *& speed change are atomic */
1432 spin_lock_irqsave(&self->lock, flags);
1434 /* Check if we need to change the speed */
1435 speed = irda_get_next_speed(skb);
1436 if ((speed != self->io.speed) && (speed != -1)) {
1437 /* Check for empty frame. */
1438 if (!skb->len) {
1439 /* If we are currently transmitting, defer to
1440 * interrupt handler. - Jean II */
1441 if(self->tx_fifo.len == 0) {
1442 nsc_ircc_change_speed(self, speed);
1443 netif_wake_queue(dev);
1444 } else {
1445 self->new_speed = speed;
1446 /* Keep queue stopped :
1447 * the speed change operation may change the
1448 * xmit handler, and we want to make sure
1449 * the next packet get through the proper
1450 * Tx path, so block the Tx queue until
1451 * the speed change has been done.
1452 * Jean II */
1454 dev->trans_start = jiffies;
1455 spin_unlock_irqrestore(&self->lock, flags);
1456 dev_kfree_skb(skb);
1457 return 0;
1458 } else {
1459 /* Change speed after current frame */
1460 self->new_speed = speed;
1464 /* Save current bank */
1465 bank = inb(iobase+BSR);
1467 /* Register and copy this frame to DMA memory */
1468 self->tx_fifo.queue[self->tx_fifo.free].start = self->tx_fifo.tail;
1469 self->tx_fifo.queue[self->tx_fifo.free].len = skb->len;
1470 self->tx_fifo.tail += skb->len;
1472 self->stats.tx_bytes += skb->len;
1474 skb_copy_from_linear_data(skb, self->tx_fifo.queue[self->tx_fifo.free].start,
1475 skb->len);
1476 self->tx_fifo.len++;
1477 self->tx_fifo.free++;
1479 /* Start transmit only if there is currently no transmit going on */
1480 if (self->tx_fifo.len == 1) {
1481 /* Check if we must wait the min turn time or not */
1482 mtt = irda_get_mtt(skb);
1483 if (mtt) {
1484 /* Check how much time we have used already */
1485 do_gettimeofday(&self->now);
1486 diff = self->now.tv_usec - self->stamp.tv_usec;
1487 if (diff < 0)
1488 diff += 1000000;
1490 /* Check if the mtt is larger than the time we have
1491 * already used by all the protocol processing
1493 if (mtt > diff) {
1494 mtt -= diff;
1497 * Use timer if delay larger than 125 us, and
1498 * use udelay for smaller values which should
1499 * be acceptable
1501 if (mtt > 125) {
1502 /* Adjust for timer resolution */
1503 mtt = mtt / 125;
1505 /* Setup timer */
1506 switch_bank(iobase, BANK4);
1507 outb(mtt & 0xff, iobase+TMRL);
1508 outb((mtt >> 8) & 0x0f, iobase+TMRH);
1510 /* Start timer */
1511 outb(IRCR1_TMR_EN, iobase+IRCR1);
1512 self->io.direction = IO_XMIT;
1514 /* Enable timer interrupt */
1515 switch_bank(iobase, BANK0);
1516 outb(IER_TMR_IE, iobase+IER);
1518 /* Timer will take care of the rest */
1519 goto out;
1520 } else
1521 udelay(mtt);
1524 /* Enable DMA interrupt */
1525 switch_bank(iobase, BANK0);
1526 outb(IER_DMA_IE, iobase+IER);
1528 /* Transmit frame */
1529 nsc_ircc_dma_xmit(self, iobase);
1531 out:
1532 /* Not busy transmitting anymore if window is not full,
1533 * and if we don't need to change speed */
1534 if ((self->tx_fifo.free < MAX_TX_WINDOW) && (self->new_speed == 0))
1535 netif_wake_queue(self->netdev);
1537 /* Restore bank register */
1538 outb(bank, iobase+BSR);
1540 dev->trans_start = jiffies;
1541 spin_unlock_irqrestore(&self->lock, flags);
1542 dev_kfree_skb(skb);
1544 return 0;
1548 * Function nsc_ircc_dma_xmit (self, iobase)
1550 * Transmit data using DMA
1553 static void nsc_ircc_dma_xmit(struct nsc_ircc_cb *self, int iobase)
1555 int bsr;
1557 /* Save current bank */
1558 bsr = inb(iobase+BSR);
1560 /* Disable DMA */
1561 switch_bank(iobase, BANK0);
1562 outb(inb(iobase+MCR) & ~MCR_DMA_EN, iobase+MCR);
1564 self->io.direction = IO_XMIT;
1566 /* Choose transmit DMA channel */
1567 switch_bank(iobase, BANK2);
1568 outb(ECR1_DMASWP|ECR1_DMANF|ECR1_EXT_SL, iobase+ECR1);
1570 irda_setup_dma(self->io.dma,
1571 ((u8 *)self->tx_fifo.queue[self->tx_fifo.ptr].start -
1572 self->tx_buff.head) + self->tx_buff_dma,
1573 self->tx_fifo.queue[self->tx_fifo.ptr].len,
1574 DMA_TX_MODE);
1576 /* Enable DMA and SIR interaction pulse */
1577 switch_bank(iobase, BANK0);
1578 outb(inb(iobase+MCR)|MCR_TX_DFR|MCR_DMA_EN|MCR_IR_PLS, iobase+MCR);
1580 /* Restore bank register */
1581 outb(bsr, iobase+BSR);
1585 * Function nsc_ircc_pio_xmit (self, iobase)
1587 * Transmit data using PIO. Returns the number of bytes that actually
1588 * got transferred
1591 static int nsc_ircc_pio_write(int iobase, __u8 *buf, int len, int fifo_size)
1593 int actual = 0;
1594 __u8 bank;
1596 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
1598 /* Save current bank */
1599 bank = inb(iobase+BSR);
1601 switch_bank(iobase, BANK0);
1602 if (!(inb_p(iobase+LSR) & LSR_TXEMP)) {
1603 IRDA_DEBUG(4, "%s(), warning, FIFO not empty yet!\n",
1604 __FUNCTION__);
1606 /* FIFO may still be filled to the Tx interrupt threshold */
1607 fifo_size -= 17;
1610 /* Fill FIFO with current frame */
1611 while ((fifo_size-- > 0) && (actual < len)) {
1612 /* Transmit next byte */
1613 outb(buf[actual++], iobase+TXD);
1616 IRDA_DEBUG(4, "%s(), fifo_size %d ; %d sent of %d\n",
1617 __FUNCTION__, fifo_size, actual, len);
1619 /* Restore bank */
1620 outb(bank, iobase+BSR);
1622 return actual;
1626 * Function nsc_ircc_dma_xmit_complete (self)
1628 * The transfer of a frame in finished. This function will only be called
1629 * by the interrupt handler
1632 static int nsc_ircc_dma_xmit_complete(struct nsc_ircc_cb *self)
1634 int iobase;
1635 __u8 bank;
1636 int ret = TRUE;
1638 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
1640 iobase = self->io.fir_base;
1642 /* Save current bank */
1643 bank = inb(iobase+BSR);
1645 /* Disable DMA */
1646 switch_bank(iobase, BANK0);
1647 outb(inb(iobase+MCR) & ~MCR_DMA_EN, iobase+MCR);
1649 /* Check for underrrun! */
1650 if (inb(iobase+ASCR) & ASCR_TXUR) {
1651 self->stats.tx_errors++;
1652 self->stats.tx_fifo_errors++;
1654 /* Clear bit, by writing 1 into it */
1655 outb(ASCR_TXUR, iobase+ASCR);
1656 } else {
1657 self->stats.tx_packets++;
1660 /* Finished with this frame, so prepare for next */
1661 self->tx_fifo.ptr++;
1662 self->tx_fifo.len--;
1664 /* Any frames to be sent back-to-back? */
1665 if (self->tx_fifo.len) {
1666 nsc_ircc_dma_xmit(self, iobase);
1668 /* Not finished yet! */
1669 ret = FALSE;
1670 } else {
1671 /* Reset Tx FIFO info */
1672 self->tx_fifo.len = self->tx_fifo.ptr = self->tx_fifo.free = 0;
1673 self->tx_fifo.tail = self->tx_buff.head;
1676 /* Make sure we have room for more frames and
1677 * that we don't need to change speed */
1678 if ((self->tx_fifo.free < MAX_TX_WINDOW) && (self->new_speed == 0)) {
1679 /* Not busy transmitting anymore */
1680 /* Tell the network layer, that we can accept more frames */
1681 netif_wake_queue(self->netdev);
1684 /* Restore bank */
1685 outb(bank, iobase+BSR);
1687 return ret;
1691 * Function nsc_ircc_dma_receive (self)
1693 * Get ready for receiving a frame. The device will initiate a DMA
1694 * if it starts to receive a frame.
1697 static int nsc_ircc_dma_receive(struct nsc_ircc_cb *self)
1699 int iobase;
1700 __u8 bsr;
1702 iobase = self->io.fir_base;
1704 /* Reset Tx FIFO info */
1705 self->tx_fifo.len = self->tx_fifo.ptr = self->tx_fifo.free = 0;
1706 self->tx_fifo.tail = self->tx_buff.head;
1708 /* Save current bank */
1709 bsr = inb(iobase+BSR);
1711 /* Disable DMA */
1712 switch_bank(iobase, BANK0);
1713 outb(inb(iobase+MCR) & ~MCR_DMA_EN, iobase+MCR);
1715 /* Choose DMA Rx, DMA Fairness, and Advanced mode */
1716 switch_bank(iobase, BANK2);
1717 outb(ECR1_DMANF|ECR1_EXT_SL, iobase+ECR1);
1719 self->io.direction = IO_RECV;
1720 self->rx_buff.data = self->rx_buff.head;
1722 /* Reset Rx FIFO. This will also flush the ST_FIFO */
1723 switch_bank(iobase, BANK0);
1724 outb(FCR_RXSR|FCR_FIFO_EN, iobase+FCR);
1726 self->st_fifo.len = self->st_fifo.pending_bytes = 0;
1727 self->st_fifo.tail = self->st_fifo.head = 0;
1729 irda_setup_dma(self->io.dma, self->rx_buff_dma, self->rx_buff.truesize,
1730 DMA_RX_MODE);
1732 /* Enable DMA */
1733 switch_bank(iobase, BANK0);
1734 outb(inb(iobase+MCR)|MCR_DMA_EN, iobase+MCR);
1736 /* Restore bank register */
1737 outb(bsr, iobase+BSR);
1739 return 0;
1743 * Function nsc_ircc_dma_receive_complete (self)
1745 * Finished with receiving frames
1749 static int nsc_ircc_dma_receive_complete(struct nsc_ircc_cb *self, int iobase)
1751 struct st_fifo *st_fifo;
1752 struct sk_buff *skb;
1753 __u8 status;
1754 __u8 bank;
1755 int len;
1757 st_fifo = &self->st_fifo;
1759 /* Save current bank */
1760 bank = inb(iobase+BSR);
1762 /* Read all entries in status FIFO */
1763 switch_bank(iobase, BANK5);
1764 while ((status = inb(iobase+FRM_ST)) & FRM_ST_VLD) {
1765 /* We must empty the status FIFO no matter what */
1766 len = inb(iobase+RFLFL) | ((inb(iobase+RFLFH) & 0x1f) << 8);
1768 if (st_fifo->tail >= MAX_RX_WINDOW) {
1769 IRDA_DEBUG(0, "%s(), window is full!\n", __FUNCTION__);
1770 continue;
1773 st_fifo->entries[st_fifo->tail].status = status;
1774 st_fifo->entries[st_fifo->tail].len = len;
1775 st_fifo->pending_bytes += len;
1776 st_fifo->tail++;
1777 st_fifo->len++;
1779 /* Try to process all entries in status FIFO */
1780 while (st_fifo->len > 0) {
1781 /* Get first entry */
1782 status = st_fifo->entries[st_fifo->head].status;
1783 len = st_fifo->entries[st_fifo->head].len;
1784 st_fifo->pending_bytes -= len;
1785 st_fifo->head++;
1786 st_fifo->len--;
1788 /* Check for errors */
1789 if (status & FRM_ST_ERR_MSK) {
1790 if (status & FRM_ST_LOST_FR) {
1791 /* Add number of lost frames to stats */
1792 self->stats.rx_errors += len;
1793 } else {
1794 /* Skip frame */
1795 self->stats.rx_errors++;
1797 self->rx_buff.data += len;
1799 if (status & FRM_ST_MAX_LEN)
1800 self->stats.rx_length_errors++;
1802 if (status & FRM_ST_PHY_ERR)
1803 self->stats.rx_frame_errors++;
1805 if (status & FRM_ST_BAD_CRC)
1806 self->stats.rx_crc_errors++;
1808 /* The errors below can be reported in both cases */
1809 if (status & FRM_ST_OVR1)
1810 self->stats.rx_fifo_errors++;
1812 if (status & FRM_ST_OVR2)
1813 self->stats.rx_fifo_errors++;
1814 } else {
1816 * First we must make sure that the frame we
1817 * want to deliver is all in main memory. If we
1818 * cannot tell, then we check if the Rx FIFO is
1819 * empty. If not then we will have to take a nap
1820 * and try again later.
1822 if (st_fifo->pending_bytes < self->io.fifo_size) {
1823 switch_bank(iobase, BANK0);
1824 if (inb(iobase+LSR) & LSR_RXDA) {
1825 /* Put this entry back in fifo */
1826 st_fifo->head--;
1827 st_fifo->len++;
1828 st_fifo->pending_bytes += len;
1829 st_fifo->entries[st_fifo->head].status = status;
1830 st_fifo->entries[st_fifo->head].len = len;
1832 * DMA not finished yet, so try again
1833 * later, set timer value, resolution
1834 * 125 us
1836 switch_bank(iobase, BANK4);
1837 outb(0x02, iobase+TMRL); /* x 125 us */
1838 outb(0x00, iobase+TMRH);
1840 /* Start timer */
1841 outb(IRCR1_TMR_EN, iobase+IRCR1);
1843 /* Restore bank register */
1844 outb(bank, iobase+BSR);
1846 return FALSE; /* I'll be back! */
1851 * Remember the time we received this frame, so we can
1852 * reduce the min turn time a bit since we will know
1853 * how much time we have used for protocol processing
1855 do_gettimeofday(&self->stamp);
1857 skb = dev_alloc_skb(len+1);
1858 if (skb == NULL) {
1859 IRDA_WARNING("%s(), memory squeeze, "
1860 "dropping frame.\n",
1861 __FUNCTION__);
1862 self->stats.rx_dropped++;
1864 /* Restore bank register */
1865 outb(bank, iobase+BSR);
1867 return FALSE;
1870 /* Make sure IP header gets aligned */
1871 skb_reserve(skb, 1);
1873 /* Copy frame without CRC */
1874 if (self->io.speed < 4000000) {
1875 skb_put(skb, len-2);
1876 skb_copy_to_linear_data(skb,
1877 self->rx_buff.data,
1878 len - 2);
1879 } else {
1880 skb_put(skb, len-4);
1881 skb_copy_to_linear_data(skb,
1882 self->rx_buff.data,
1883 len - 4);
1886 /* Move to next frame */
1887 self->rx_buff.data += len;
1888 self->stats.rx_bytes += len;
1889 self->stats.rx_packets++;
1891 skb->dev = self->netdev;
1892 skb_reset_mac_header(skb);
1893 skb->protocol = htons(ETH_P_IRDA);
1894 netif_rx(skb);
1895 self->netdev->last_rx = jiffies;
1898 /* Restore bank register */
1899 outb(bank, iobase+BSR);
1901 return TRUE;
1905 * Function nsc_ircc_pio_receive (self)
1907 * Receive all data in receiver FIFO
1910 static void nsc_ircc_pio_receive(struct nsc_ircc_cb *self)
1912 __u8 byte;
1913 int iobase;
1915 iobase = self->io.fir_base;
1917 /* Receive all characters in Rx FIFO */
1918 do {
1919 byte = inb(iobase+RXD);
1920 async_unwrap_char(self->netdev, &self->stats, &self->rx_buff,
1921 byte);
1922 } while (inb(iobase+LSR) & LSR_RXDA); /* Data available */
1926 * Function nsc_ircc_sir_interrupt (self, eir)
1928 * Handle SIR interrupt
1931 static void nsc_ircc_sir_interrupt(struct nsc_ircc_cb *self, int eir)
1933 int actual;
1935 /* Check if transmit FIFO is low on data */
1936 if (eir & EIR_TXLDL_EV) {
1937 /* Write data left in transmit buffer */
1938 actual = nsc_ircc_pio_write(self->io.fir_base,
1939 self->tx_buff.data,
1940 self->tx_buff.len,
1941 self->io.fifo_size);
1942 self->tx_buff.data += actual;
1943 self->tx_buff.len -= actual;
1945 self->io.direction = IO_XMIT;
1947 /* Check if finished */
1948 if (self->tx_buff.len > 0)
1949 self->ier = IER_TXLDL_IE;
1950 else {
1952 self->stats.tx_packets++;
1953 netif_wake_queue(self->netdev);
1954 self->ier = IER_TXEMP_IE;
1958 /* Check if transmission has completed */
1959 if (eir & EIR_TXEMP_EV) {
1960 /* Turn around and get ready to receive some data */
1961 self->io.direction = IO_RECV;
1962 self->ier = IER_RXHDL_IE;
1963 /* Check if we need to change the speed?
1964 * Need to be after self->io.direction to avoid race with
1965 * nsc_ircc_hard_xmit_sir() - Jean II */
1966 if (self->new_speed) {
1967 IRDA_DEBUG(2, "%s(), Changing speed!\n", __FUNCTION__);
1968 self->ier = nsc_ircc_change_speed(self,
1969 self->new_speed);
1970 self->new_speed = 0;
1971 netif_wake_queue(self->netdev);
1973 /* Check if we are going to FIR */
1974 if (self->io.speed > 115200) {
1975 /* No need to do anymore SIR stuff */
1976 return;
1981 /* Rx FIFO threshold or timeout */
1982 if (eir & EIR_RXHDL_EV) {
1983 nsc_ircc_pio_receive(self);
1985 /* Keep receiving */
1986 self->ier = IER_RXHDL_IE;
1991 * Function nsc_ircc_fir_interrupt (self, eir)
1993 * Handle MIR/FIR interrupt
1996 static void nsc_ircc_fir_interrupt(struct nsc_ircc_cb *self, int iobase,
1997 int eir)
1999 __u8 bank;
2001 bank = inb(iobase+BSR);
2003 /* Status FIFO event*/
2004 if (eir & EIR_SFIF_EV) {
2005 /* Check if DMA has finished */
2006 if (nsc_ircc_dma_receive_complete(self, iobase)) {
2007 /* Wait for next status FIFO interrupt */
2008 self->ier = IER_SFIF_IE;
2009 } else {
2010 self->ier = IER_SFIF_IE | IER_TMR_IE;
2012 } else if (eir & EIR_TMR_EV) { /* Timer finished */
2013 /* Disable timer */
2014 switch_bank(iobase, BANK4);
2015 outb(0, iobase+IRCR1);
2017 /* Clear timer event */
2018 switch_bank(iobase, BANK0);
2019 outb(ASCR_CTE, iobase+ASCR);
2021 /* Check if this is a Tx timer interrupt */
2022 if (self->io.direction == IO_XMIT) {
2023 nsc_ircc_dma_xmit(self, iobase);
2025 /* Interrupt on DMA */
2026 self->ier = IER_DMA_IE;
2027 } else {
2028 /* Check (again) if DMA has finished */
2029 if (nsc_ircc_dma_receive_complete(self, iobase)) {
2030 self->ier = IER_SFIF_IE;
2031 } else {
2032 self->ier = IER_SFIF_IE | IER_TMR_IE;
2035 } else if (eir & EIR_DMA_EV) {
2036 /* Finished with all transmissions? */
2037 if (nsc_ircc_dma_xmit_complete(self)) {
2038 if(self->new_speed != 0) {
2039 /* As we stop the Tx queue, the speed change
2040 * need to be done when the Tx fifo is
2041 * empty. Ask for a Tx done interrupt */
2042 self->ier = IER_TXEMP_IE;
2043 } else {
2044 /* Check if there are more frames to be
2045 * transmitted */
2046 if (irda_device_txqueue_empty(self->netdev)) {
2047 /* Prepare for receive */
2048 nsc_ircc_dma_receive(self);
2049 self->ier = IER_SFIF_IE;
2050 } else
2051 IRDA_WARNING("%s(), potential "
2052 "Tx queue lockup !\n",
2053 __FUNCTION__);
2055 } else {
2056 /* Not finished yet, so interrupt on DMA again */
2057 self->ier = IER_DMA_IE;
2059 } else if (eir & EIR_TXEMP_EV) {
2060 /* The Tx FIFO has totally drained out, so now we can change
2061 * the speed... - Jean II */
2062 self->ier = nsc_ircc_change_speed(self, self->new_speed);
2063 self->new_speed = 0;
2064 netif_wake_queue(self->netdev);
2065 /* Note : nsc_ircc_change_speed() restarted Rx fifo */
2068 outb(bank, iobase+BSR);
2072 * Function nsc_ircc_interrupt (irq, dev_id, regs)
2074 * An interrupt from the chip has arrived. Time to do some work
2077 static irqreturn_t nsc_ircc_interrupt(int irq, void *dev_id)
2079 struct net_device *dev = dev_id;
2080 struct nsc_ircc_cb *self;
2081 __u8 bsr, eir;
2082 int iobase;
2084 self = dev->priv;
2086 spin_lock(&self->lock);
2088 iobase = self->io.fir_base;
2090 bsr = inb(iobase+BSR); /* Save current bank */
2092 switch_bank(iobase, BANK0);
2093 self->ier = inb(iobase+IER);
2094 eir = inb(iobase+EIR) & self->ier; /* Mask out the interesting ones */
2096 outb(0, iobase+IER); /* Disable interrupts */
2098 if (eir) {
2099 /* Dispatch interrupt handler for the current speed */
2100 if (self->io.speed > 115200)
2101 nsc_ircc_fir_interrupt(self, iobase, eir);
2102 else
2103 nsc_ircc_sir_interrupt(self, eir);
2106 outb(self->ier, iobase+IER); /* Restore interrupts */
2107 outb(bsr, iobase+BSR); /* Restore bank register */
2109 spin_unlock(&self->lock);
2110 return IRQ_RETVAL(eir);
2114 * Function nsc_ircc_is_receiving (self)
2116 * Return TRUE is we are currently receiving a frame
2119 static int nsc_ircc_is_receiving(struct nsc_ircc_cb *self)
2121 unsigned long flags;
2122 int status = FALSE;
2123 int iobase;
2124 __u8 bank;
2126 IRDA_ASSERT(self != NULL, return FALSE;);
2128 spin_lock_irqsave(&self->lock, flags);
2130 if (self->io.speed > 115200) {
2131 iobase = self->io.fir_base;
2133 /* Check if rx FIFO is not empty */
2134 bank = inb(iobase+BSR);
2135 switch_bank(iobase, BANK2);
2136 if ((inb(iobase+RXFLV) & 0x3f) != 0) {
2137 /* We are receiving something */
2138 status = TRUE;
2140 outb(bank, iobase+BSR);
2141 } else
2142 status = (self->rx_buff.state != OUTSIDE_FRAME);
2144 spin_unlock_irqrestore(&self->lock, flags);
2146 return status;
2150 * Function nsc_ircc_net_open (dev)
2152 * Start the device
2155 static int nsc_ircc_net_open(struct net_device *dev)
2157 struct nsc_ircc_cb *self;
2158 int iobase;
2159 char hwname[32];
2160 __u8 bank;
2162 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
2164 IRDA_ASSERT(dev != NULL, return -1;);
2165 self = (struct nsc_ircc_cb *) dev->priv;
2167 IRDA_ASSERT(self != NULL, return 0;);
2169 iobase = self->io.fir_base;
2171 if (request_irq(self->io.irq, nsc_ircc_interrupt, 0, dev->name, dev)) {
2172 IRDA_WARNING("%s, unable to allocate irq=%d\n",
2173 driver_name, self->io.irq);
2174 return -EAGAIN;
2177 * Always allocate the DMA channel after the IRQ, and clean up on
2178 * failure.
2180 if (request_dma(self->io.dma, dev->name)) {
2181 IRDA_WARNING("%s, unable to allocate dma=%d\n",
2182 driver_name, self->io.dma);
2183 free_irq(self->io.irq, dev);
2184 return -EAGAIN;
2187 /* Save current bank */
2188 bank = inb(iobase+BSR);
2190 /* turn on interrupts */
2191 switch_bank(iobase, BANK0);
2192 outb(IER_LS_IE | IER_RXHDL_IE, iobase+IER);
2194 /* Restore bank register */
2195 outb(bank, iobase+BSR);
2197 /* Ready to play! */
2198 netif_start_queue(dev);
2200 /* Give self a hardware name */
2201 sprintf(hwname, "NSC-FIR @ 0x%03x", self->io.fir_base);
2204 * Open new IrLAP layer instance, now that everything should be
2205 * initialized properly
2207 self->irlap = irlap_open(dev, &self->qos, hwname);
2209 return 0;
2213 * Function nsc_ircc_net_close (dev)
2215 * Stop the device
2218 static int nsc_ircc_net_close(struct net_device *dev)
2220 struct nsc_ircc_cb *self;
2221 int iobase;
2222 __u8 bank;
2224 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
2226 IRDA_ASSERT(dev != NULL, return -1;);
2228 self = (struct nsc_ircc_cb *) dev->priv;
2229 IRDA_ASSERT(self != NULL, return 0;);
2231 /* Stop device */
2232 netif_stop_queue(dev);
2234 /* Stop and remove instance of IrLAP */
2235 if (self->irlap)
2236 irlap_close(self->irlap);
2237 self->irlap = NULL;
2239 iobase = self->io.fir_base;
2241 disable_dma(self->io.dma);
2243 /* Save current bank */
2244 bank = inb(iobase+BSR);
2246 /* Disable interrupts */
2247 switch_bank(iobase, BANK0);
2248 outb(0, iobase+IER);
2250 free_irq(self->io.irq, dev);
2251 free_dma(self->io.dma);
2253 /* Restore bank register */
2254 outb(bank, iobase+BSR);
2256 return 0;
2260 * Function nsc_ircc_net_ioctl (dev, rq, cmd)
2262 * Process IOCTL commands for this device
2265 static int nsc_ircc_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2267 struct if_irda_req *irq = (struct if_irda_req *) rq;
2268 struct nsc_ircc_cb *self;
2269 unsigned long flags;
2270 int ret = 0;
2272 IRDA_ASSERT(dev != NULL, return -1;);
2274 self = dev->priv;
2276 IRDA_ASSERT(self != NULL, return -1;);
2278 IRDA_DEBUG(2, "%s(), %s, (cmd=0x%X)\n", __FUNCTION__, dev->name, cmd);
2280 switch (cmd) {
2281 case SIOCSBANDWIDTH: /* Set bandwidth */
2282 if (!capable(CAP_NET_ADMIN)) {
2283 ret = -EPERM;
2284 break;
2286 spin_lock_irqsave(&self->lock, flags);
2287 nsc_ircc_change_speed(self, irq->ifr_baudrate);
2288 spin_unlock_irqrestore(&self->lock, flags);
2289 break;
2290 case SIOCSMEDIABUSY: /* Set media busy */
2291 if (!capable(CAP_NET_ADMIN)) {
2292 ret = -EPERM;
2293 break;
2295 irda_device_set_media_busy(self->netdev, TRUE);
2296 break;
2297 case SIOCGRECEIVING: /* Check if we are receiving right now */
2298 /* This is already protected */
2299 irq->ifr_receiving = nsc_ircc_is_receiving(self);
2300 break;
2301 default:
2302 ret = -EOPNOTSUPP;
2304 return ret;
2307 static struct net_device_stats *nsc_ircc_net_get_stats(struct net_device *dev)
2309 struct nsc_ircc_cb *self = (struct nsc_ircc_cb *) dev->priv;
2311 return &self->stats;
2314 static int nsc_ircc_suspend(struct platform_device *dev, pm_message_t state)
2316 struct nsc_ircc_cb *self = platform_get_drvdata(dev);
2317 int bank;
2318 unsigned long flags;
2319 int iobase = self->io.fir_base;
2321 if (self->io.suspended)
2322 return 0;
2324 IRDA_DEBUG(1, "%s, Suspending\n", driver_name);
2326 rtnl_lock();
2327 if (netif_running(self->netdev)) {
2328 netif_device_detach(self->netdev);
2329 spin_lock_irqsave(&self->lock, flags);
2330 /* Save current bank */
2331 bank = inb(iobase+BSR);
2333 /* Disable interrupts */
2334 switch_bank(iobase, BANK0);
2335 outb(0, iobase+IER);
2337 /* Restore bank register */
2338 outb(bank, iobase+BSR);
2340 spin_unlock_irqrestore(&self->lock, flags);
2341 free_irq(self->io.irq, self->netdev);
2342 disable_dma(self->io.dma);
2344 self->io.suspended = 1;
2345 rtnl_unlock();
2347 return 0;
2350 static int nsc_ircc_resume(struct platform_device *dev)
2352 struct nsc_ircc_cb *self = platform_get_drvdata(dev);
2353 unsigned long flags;
2355 if (!self->io.suspended)
2356 return 0;
2358 IRDA_DEBUG(1, "%s, Waking up\n", driver_name);
2360 rtnl_lock();
2361 nsc_ircc_setup(&self->io);
2362 nsc_ircc_init_dongle_interface(self->io.fir_base, self->io.dongle_id);
2364 if (netif_running(self->netdev)) {
2365 if (request_irq(self->io.irq, nsc_ircc_interrupt, 0,
2366 self->netdev->name, self->netdev)) {
2367 IRDA_WARNING("%s, unable to allocate irq=%d\n",
2368 driver_name, self->io.irq);
2371 * Don't fail resume process, just kill this
2372 * network interface
2374 unregister_netdevice(self->netdev);
2375 } else {
2376 spin_lock_irqsave(&self->lock, flags);
2377 nsc_ircc_change_speed(self, self->io.speed);
2378 spin_unlock_irqrestore(&self->lock, flags);
2379 netif_device_attach(self->netdev);
2382 } else {
2383 spin_lock_irqsave(&self->lock, flags);
2384 nsc_ircc_change_speed(self, 9600);
2385 spin_unlock_irqrestore(&self->lock, flags);
2387 self->io.suspended = 0;
2388 rtnl_unlock();
2390 return 0;
2393 MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
2394 MODULE_DESCRIPTION("NSC IrDA Device Driver");
2395 MODULE_LICENSE("GPL");
2398 module_param(qos_mtt_bits, int, 0);
2399 MODULE_PARM_DESC(qos_mtt_bits, "Minimum Turn Time");
2400 module_param_array(io, int, NULL, 0);
2401 MODULE_PARM_DESC(io, "Base I/O addresses");
2402 module_param_array(irq, int, NULL, 0);
2403 MODULE_PARM_DESC(irq, "IRQ lines");
2404 module_param_array(dma, int, NULL, 0);
2405 MODULE_PARM_DESC(dma, "DMA channels");
2406 module_param(dongle_id, int, 0);
2407 MODULE_PARM_DESC(dongle_id, "Type-id of used dongle");
2409 module_init(nsc_ircc_init);
2410 module_exit(nsc_ircc_cleanup);