initial commit with v2.6.9
[linux-2.6.9-moxart.git] / drivers / net / irda / irport.c
blob6ad80edb2efa824641a3a6ef9cba9acb4df90425
1 /*********************************************************************
2 *
3 * Filename: irport.c
4 * Version: 1.0
5 * Description: Half duplex serial port SIR driver for IrDA.
6 * Status: Experimental.
7 * Author: Dag Brattli <dagb@cs.uit.no>
8 * Created at: Sun Aug 3 13:49:59 1997
9 * Modified at: Fri Jan 28 20:22:38 2000
10 * Modified by: Dag Brattli <dagb@cs.uit.no>
11 * Sources: serial.c by Linus Torvalds
13 * Copyright (c) 1997, 1998, 1999-2000 Dag Brattli, All Rights Reserved.
14 * Copyright (c) 2000-2003 Jean Tourrilhes, All Rights Reserved.
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License as
18 * published by the Free Software Foundation; either version 2 of
19 * the License, or (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
29 * MA 02111-1307 USA
31 * This driver is ment to be a small half duplex serial driver to be
32 * used for IR-chipsets that has a UART (16550) compatibility mode.
33 * Eventually it will replace irtty, because of irtty has some
34 * problems that is hard to get around when we don't have control
35 * over the serial driver. This driver may also be used by FIR
36 * drivers to handle SIR mode for them.
38 ********************************************************************/
40 #include <linux/module.h>
42 #include <linux/kernel.h>
43 #include <linux/types.h>
44 #include <linux/ioport.h>
45 #include <linux/slab.h>
46 #include <linux/string.h>
47 #include <linux/skbuff.h>
48 #include <linux/serial_reg.h>
49 #include <linux/errno.h>
50 #include <linux/init.h>
51 #include <linux/spinlock.h>
52 #include <linux/delay.h>
53 #include <linux/rtnetlink.h>
55 #include <asm/system.h>
56 #include <asm/bitops.h>
57 #include <asm/io.h>
59 #include <net/irda/irda.h>
60 #include <net/irda/wrapper.h>
61 #include "irport.h"
63 #define IO_EXTENT 8
65 /*
66 * Currently you'll need to set these values using insmod like this:
67 * insmod irport io=0x3e8 irq=11
69 static unsigned int io[] = { ~0, ~0, ~0, ~0 };
70 static unsigned int irq[] = { 0, 0, 0, 0 };
72 static unsigned int qos_mtt_bits = 0x03;
74 static struct irport_cb *dev_self[] = { NULL, NULL, NULL, NULL};
75 static char *driver_name = "irport";
77 static inline void irport_write_wakeup(struct irport_cb *self);
78 static inline int irport_write(int iobase, int fifo_size, __u8 *buf, int len);
79 static inline void irport_receive(struct irport_cb *self);
81 static int irport_net_ioctl(struct net_device *dev, struct ifreq *rq,
82 int cmd);
83 static inline int irport_is_receiving(struct irport_cb *self);
84 static int irport_set_dtr_rts(struct net_device *dev, int dtr, int rts);
85 static int irport_raw_write(struct net_device *dev, __u8 *buf, int len);
86 static struct net_device_stats *irport_net_get_stats(struct net_device *dev);
87 static int irport_change_speed_complete(struct irda_task *task);
88 static void irport_timeout(struct net_device *dev);
90 EXPORT_SYMBOL(irport_open);
91 EXPORT_SYMBOL(irport_close);
92 EXPORT_SYMBOL(irport_start);
93 EXPORT_SYMBOL(irport_stop);
94 EXPORT_SYMBOL(irport_interrupt);
95 EXPORT_SYMBOL(irport_hard_xmit);
96 EXPORT_SYMBOL(irport_timeout);
97 EXPORT_SYMBOL(irport_change_speed);
98 EXPORT_SYMBOL(irport_net_open);
99 EXPORT_SYMBOL(irport_net_close);
101 static int __init irport_init(void)
103 int i;
105 for (i=0; (io[i] < 2000) && (i < 4); i++) {
106 if (irport_open(i, io[i], irq[i]) != NULL)
107 return 0;
110 * Maybe something failed, but we can still be usable for FIR drivers
112 return 0;
116 * Function irport_cleanup ()
118 * Close all configured ports
121 static void __exit irport_cleanup(void)
123 int i;
125 IRDA_DEBUG( 4, "%s()\n", __FUNCTION__);
127 for (i=0; i < 4; i++) {
128 if (dev_self[i])
129 irport_close(dev_self[i]);
133 struct irport_cb *
134 irport_open(int i, unsigned int iobase, unsigned int irq)
136 struct net_device *dev;
137 struct irport_cb *self;
139 IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
141 /* Lock the port that we need */
142 if (!request_region(iobase, IO_EXTENT, driver_name)) {
143 IRDA_DEBUG(0, "%s(), can't get iobase of 0x%03x\n",
144 __FUNCTION__, iobase);
145 goto err_out1;
149 * Allocate new instance of the driver
151 dev = alloc_irdadev(sizeof(struct irport_cb));
152 if (!dev) {
153 ERROR("%s(), can't allocate memory for "
154 "irda device!\n", __FUNCTION__);
155 goto err_out2;
158 self = dev->priv;
159 spin_lock_init(&self->lock);
161 /* Need to store self somewhere */
162 dev_self[i] = self;
163 self->priv = self;
164 self->index = i;
166 /* Initialize IO */
167 self->io.sir_base = iobase;
168 self->io.sir_ext = IO_EXTENT;
169 self->io.irq = irq;
170 self->io.fifo_size = 16; /* 16550A and compatible */
172 /* Initialize QoS for this device */
173 irda_init_max_qos_capabilies(&self->qos);
175 self->qos.baud_rate.bits = IR_9600|IR_19200|IR_38400|IR_57600|
176 IR_115200;
178 self->qos.min_turn_time.bits = qos_mtt_bits;
179 irda_qos_bits_to_value(&self->qos);
181 /* Bootstrap ZeroCopy Rx */
182 self->rx_buff.truesize = IRDA_SKB_MAX_MTU;
183 self->rx_buff.skb = __dev_alloc_skb(self->rx_buff.truesize,
184 GFP_KERNEL);
185 if (self->rx_buff.skb == NULL) {
186 ERROR("%s(), can't allocate memory for "
187 "receive buffer!\n", __FUNCTION__);
188 goto err_out3;
190 skb_reserve(self->rx_buff.skb, 1);
191 self->rx_buff.head = self->rx_buff.skb->data;
192 /* No need to memset the buffer, unless you are really pedantic */
194 /* Finish setup the Rx buffer descriptor */
195 self->rx_buff.in_frame = FALSE;
196 self->rx_buff.state = OUTSIDE_FRAME;
197 self->rx_buff.data = self->rx_buff.head;
199 /* Specify how much memory we want */
200 self->tx_buff.truesize = 4000;
202 /* Allocate memory if needed */
203 if (self->tx_buff.truesize > 0) {
204 self->tx_buff.head = (__u8 *) kmalloc(self->tx_buff.truesize,
205 GFP_KERNEL);
206 if (self->tx_buff.head == NULL) {
207 ERROR("%s(), can't allocate memory for "
208 "transmit buffer!\n", __FUNCTION__);
209 goto err_out4;
211 memset(self->tx_buff.head, 0, self->tx_buff.truesize);
213 self->tx_buff.data = self->tx_buff.head;
215 self->netdev = dev;
216 /* Keep track of module usage */
217 SET_MODULE_OWNER(dev);
219 /* May be overridden by piggyback drivers */
220 self->interrupt = irport_interrupt;
221 self->change_speed = irport_change_speed;
223 /* Override the network functions we need to use */
224 dev->hard_start_xmit = irport_hard_xmit;
225 dev->tx_timeout = irport_timeout;
226 dev->watchdog_timeo = HZ; /* Allow time enough for speed change */
227 dev->open = irport_net_open;
228 dev->stop = irport_net_close;
229 dev->get_stats = irport_net_get_stats;
230 dev->do_ioctl = irport_net_ioctl;
232 /* Make ifconfig display some details */
233 dev->base_addr = iobase;
234 dev->irq = irq;
236 if (register_netdev(dev)) {
237 ERROR("%s(), register_netdev() failed!\n", __FUNCTION__);
238 goto err_out5;
240 MESSAGE("IrDA: Registered device %s (irport io=0x%X irq=%d)\n",
241 dev->name, iobase, irq);
243 return self;
244 err_out5:
245 kfree(self->tx_buff.head);
246 err_out4:
247 kfree_skb(self->rx_buff.skb);
248 err_out3:
249 free_netdev(dev);
250 dev_self[i] = NULL;
251 err_out2:
252 release_region(iobase, IO_EXTENT);
253 err_out1:
254 return NULL;
257 int irport_close(struct irport_cb *self)
259 ASSERT(self != NULL, return -1;);
261 /* We are not using any dongle anymore! */
262 if (self->dongle)
263 irda_device_dongle_cleanup(self->dongle);
264 self->dongle = NULL;
266 /* Remove netdevice */
267 unregister_netdev(self->netdev);
269 /* Release the IO-port that this driver is using */
270 IRDA_DEBUG(0 , "%s(), Releasing Region %03x\n",
271 __FUNCTION__, self->io.sir_base);
272 release_region(self->io.sir_base, self->io.sir_ext);
274 if (self->tx_buff.head)
275 kfree(self->tx_buff.head);
277 if (self->rx_buff.skb)
278 kfree_skb(self->rx_buff.skb);
279 self->rx_buff.skb = NULL;
281 /* Remove ourselves */
282 dev_self[self->index] = NULL;
283 free_netdev(self->netdev);
285 return 0;
288 void irport_start(struct irport_cb *self)
290 int iobase;
292 iobase = self->io.sir_base;
294 irport_stop(self);
296 /* We can't lock, we may be called from a FIR driver - Jean II */
298 /* Initialize UART */
299 outb(UART_LCR_WLEN8, iobase+UART_LCR); /* Reset DLAB */
300 outb((UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2), iobase+UART_MCR);
302 /* Turn on interrups */
303 outb(UART_IER_RLSI | UART_IER_RDI |UART_IER_THRI, iobase+UART_IER);
306 void irport_stop(struct irport_cb *self)
308 int iobase;
310 iobase = self->io.sir_base;
312 /* We can't lock, we may be called from a FIR driver - Jean II */
314 /* We are not transmitting any more */
315 self->transmitting = 0;
317 /* Reset UART */
318 outb(0, iobase+UART_MCR);
320 /* Turn off interrupts */
321 outb(0, iobase+UART_IER);
325 * Function irport_probe (void)
327 * Start IO port
330 int irport_probe(int iobase)
332 IRDA_DEBUG(4, "%s(), iobase=%#x\n", __FUNCTION__, iobase);
334 return 0;
338 * Function irport_get_fcr (speed)
340 * Compute value of fcr
343 static inline unsigned int irport_get_fcr(__u32 speed)
345 unsigned int fcr; /* FIFO control reg */
347 /* Enable fifos */
348 fcr = UART_FCR_ENABLE_FIFO;
351 * Use trigger level 1 to avoid 3 ms. timeout delay at 9600 bps, and
352 * almost 1,7 ms at 19200 bps. At speeds above that we can just forget
353 * about this timeout since it will always be fast enough.
355 if (speed < 38400)
356 fcr |= UART_FCR_TRIGGER_1;
357 else
358 //fcr |= UART_FCR_TRIGGER_14;
359 fcr |= UART_FCR_TRIGGER_8;
361 return(fcr);
365 * Function irport_change_speed (self, speed)
367 * Set speed of IrDA port to specified baudrate
369 * This function should be called with irq off and spin-lock.
371 void irport_change_speed(void *priv, __u32 speed)
373 struct irport_cb *self = (struct irport_cb *) priv;
374 int iobase;
375 unsigned int fcr; /* FIFO control reg */
376 unsigned int lcr; /* Line control reg */
377 int divisor;
379 ASSERT(self != NULL, return;);
380 ASSERT(speed != 0, return;);
382 IRDA_DEBUG(1, "%s(), Setting speed to: %d - iobase=%#x\n",
383 __FUNCTION__, speed, self->io.sir_base);
385 /* We can't lock, we may be called from a FIR driver - Jean II */
387 iobase = self->io.sir_base;
389 /* Update accounting for new speed */
390 self->io.speed = speed;
392 /* Turn off interrupts */
393 outb(0, iobase+UART_IER);
395 divisor = SPEED_MAX/speed;
397 /* Get proper fifo configuration */
398 fcr = irport_get_fcr(speed);
400 /* IrDA ports use 8N1 */
401 lcr = UART_LCR_WLEN8;
403 outb(UART_LCR_DLAB | lcr, iobase+UART_LCR); /* Set DLAB */
404 outb(divisor & 0xff, iobase+UART_DLL); /* Set speed */
405 outb(divisor >> 8, iobase+UART_DLM);
406 outb(lcr, iobase+UART_LCR); /* Set 8N1 */
407 outb(fcr, iobase+UART_FCR); /* Enable FIFO's */
409 /* Turn on interrups */
410 /* This will generate a fatal interrupt storm.
411 * People calling us will do that properly - Jean II */
412 //outb(/*UART_IER_RLSI|*/UART_IER_RDI/*|UART_IER_THRI*/, iobase+UART_IER);
416 * Function __irport_change_speed (instance, state, param)
418 * State machine for changing speed of the device. We do it this way since
419 * we cannot use schedule_timeout() when we are in interrupt context
422 int __irport_change_speed(struct irda_task *task)
424 struct irport_cb *self;
425 __u32 speed = (__u32) task->param;
426 unsigned long flags = 0;
427 int wasunlocked = 0;
428 int ret = 0;
430 IRDA_DEBUG(2, "%s(), <%ld>\n", __FUNCTION__, jiffies);
432 self = (struct irport_cb *) task->instance;
434 ASSERT(self != NULL, return -1;);
436 /* Locking notes : this function may be called from irq context with
437 * spinlock, via irport_write_wakeup(), or from non-interrupt without
438 * spinlock (from the task timer). Yuck !
439 * This is ugly, and unsafe is the spinlock is not already aquired.
440 * This will be fixed when irda-task get rewritten.
441 * Jean II */
442 if (!spin_is_locked(&self->lock)) {
443 spin_lock_irqsave(&self->lock, flags);
444 wasunlocked = 1;
447 switch (task->state) {
448 case IRDA_TASK_INIT:
449 case IRDA_TASK_WAIT:
450 /* Are we ready to change speed yet? */
451 if (self->tx_buff.len > 0) {
452 task->state = IRDA_TASK_WAIT;
454 /* Try again later */
455 ret = msecs_to_jiffies(20);
456 break;
459 if (self->dongle)
460 irda_task_next_state(task, IRDA_TASK_CHILD_INIT);
461 else
462 irda_task_next_state(task, IRDA_TASK_CHILD_DONE);
463 break;
464 case IRDA_TASK_CHILD_INIT:
465 /* Go to default speed */
466 self->change_speed(self->priv, 9600);
468 /* Change speed of dongle */
469 if (irda_task_execute(self->dongle,
470 self->dongle->issue->change_speed,
471 NULL, task, (void *) speed))
473 /* Dongle need more time to change its speed */
474 irda_task_next_state(task, IRDA_TASK_CHILD_WAIT);
476 /* Give dongle 1 sec to finish */
477 ret = msecs_to_jiffies(1000);
478 } else
479 /* Child finished immediately */
480 irda_task_next_state(task, IRDA_TASK_CHILD_DONE);
481 break;
482 case IRDA_TASK_CHILD_WAIT:
483 WARNING("%s(), changing speed of dongle timed out!\n", __FUNCTION__);
484 ret = -1;
485 break;
486 case IRDA_TASK_CHILD_DONE:
487 /* Finally we are ready to change the speed */
488 self->change_speed(self->priv, speed);
490 irda_task_next_state(task, IRDA_TASK_DONE);
491 break;
492 default:
493 ERROR("%s(), unknown state %d\n", __FUNCTION__, task->state);
494 irda_task_next_state(task, IRDA_TASK_DONE);
495 ret = -1;
496 break;
498 /* Put stuff in the state we found them - Jean II */
499 if(wasunlocked) {
500 spin_unlock_irqrestore(&self->lock, flags);
503 return ret;
507 * Function irport_change_speed_complete (task)
509 * Called when the change speed operation completes
512 static int irport_change_speed_complete(struct irda_task *task)
514 struct irport_cb *self;
516 IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
518 self = (struct irport_cb *) task->instance;
520 ASSERT(self != NULL, return -1;);
521 ASSERT(self->netdev != NULL, return -1;);
523 /* Finished changing speed, so we are not busy any longer */
524 /* Signal network layer so it can try to send the frame */
526 netif_wake_queue(self->netdev);
528 return 0;
532 * Function irport_timeout (struct net_device *dev)
534 * The networking layer thinks we timed out.
538 static void irport_timeout(struct net_device *dev)
540 struct irport_cb *self;
541 int iobase;
542 int iir, lsr;
543 unsigned long flags;
545 self = (struct irport_cb *) dev->priv;
546 ASSERT(self != NULL, return;);
547 iobase = self->io.sir_base;
549 WARNING("%s: transmit timed out, jiffies = %ld, trans_start = %ld\n",
550 dev->name, jiffies, dev->trans_start);
551 spin_lock_irqsave(&self->lock, flags);
553 /* Debug what's happening... */
555 /* Get interrupt status */
556 lsr = inb(iobase+UART_LSR);
557 /* Read interrupt register */
558 iir = inb(iobase+UART_IIR);
559 IRDA_DEBUG(0, "%s(), iir=%02x, lsr=%02x, iobase=%#x\n",
560 __FUNCTION__, iir, lsr, iobase);
562 IRDA_DEBUG(0, "%s(), transmitting=%d, remain=%d, done=%d\n",
563 __FUNCTION__, self->transmitting, self->tx_buff.len,
564 self->tx_buff.data - self->tx_buff.head);
566 /* Now, restart the port */
567 irport_start(self);
568 self->change_speed(self->priv, self->io.speed);
569 /* This will re-enable irqs */
570 outb(/*UART_IER_RLSI|*/UART_IER_RDI/*|UART_IER_THRI*/, iobase+UART_IER);
571 dev->trans_start = jiffies;
572 spin_unlock_irqrestore(&self->lock, flags);
574 netif_wake_queue(dev);
578 * Function irport_wait_hw_transmitter_finish ()
580 * Wait for the real end of HW transmission
582 * The UART is a strict FIFO, and we get called only when we have finished
583 * pushing data to the FIFO, so the maximum amount of time we must wait
584 * is only for the FIFO to drain out.
586 * We use a simple calibrated loop. We may need to adjust the loop
587 * delay (udelay) to balance I/O traffic and latency. And we also need to
588 * adjust the maximum timeout.
589 * It would probably be better to wait for the proper interrupt,
590 * but it doesn't seem to be available.
592 * We can't use jiffies or kernel timers because :
593 * 1) We are called from the interrupt handler, which disable softirqs,
594 * so jiffies won't be increased
595 * 2) Jiffies granularity is usually very coarse (10ms), and we don't
596 * want to wait that long to detect stuck hardware.
597 * Jean II
600 static void irport_wait_hw_transmitter_finish(struct irport_cb *self)
602 int iobase;
603 int count = 1000; /* 1 ms */
605 iobase = self->io.sir_base;
607 /* Calibrated busy loop */
608 while((count-- > 0) && !(inb(iobase+UART_LSR) & UART_LSR_TEMT))
609 udelay(1);
611 if(count == 0)
612 IRDA_DEBUG(0, "%s(): stuck transmitter\n", __FUNCTION__);
616 * Function irport_hard_start_xmit (struct sk_buff *skb, struct net_device *dev)
618 * Transmits the current frame until FIFO is full, then
619 * waits until the next transmitt interrupt, and continues until the
620 * frame is transmitted.
622 int irport_hard_xmit(struct sk_buff *skb, struct net_device *dev)
624 struct irport_cb *self;
625 unsigned long flags;
626 int iobase;
627 s32 speed;
629 IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
631 ASSERT(dev != NULL, return 0;);
633 self = (struct irport_cb *) dev->priv;
634 ASSERT(self != NULL, return 0;);
636 iobase = self->io.sir_base;
638 netif_stop_queue(dev);
640 /* Make sure tests & speed change are atomic */
641 spin_lock_irqsave(&self->lock, flags);
643 /* Check if we need to change the speed */
644 speed = irda_get_next_speed(skb);
645 if ((speed != self->io.speed) && (speed != -1)) {
646 /* Check for empty frame */
647 if (!skb->len) {
649 * We send frames one by one in SIR mode (no
650 * pipelining), so at this point, if we were sending
651 * a previous frame, we just received the interrupt
652 * telling us it is finished (UART_IIR_THRI).
653 * Therefore, waiting for the transmitter to really
654 * finish draining the fifo won't take too long.
655 * And the interrupt handler is not expected to run.
656 * - Jean II */
657 irport_wait_hw_transmitter_finish(self);
658 /* Better go there already locked - Jean II */
659 irda_task_execute(self, __irport_change_speed,
660 irport_change_speed_complete,
661 NULL, (void *) speed);
662 dev->trans_start = jiffies;
663 spin_unlock_irqrestore(&self->lock, flags);
664 dev_kfree_skb(skb);
665 return 0;
666 } else
667 self->new_speed = speed;
670 /* Init tx buffer */
671 self->tx_buff.data = self->tx_buff.head;
673 /* Copy skb to tx_buff while wrapping, stuffing and making CRC */
674 self->tx_buff.len = async_wrap_skb(skb, self->tx_buff.data,
675 self->tx_buff.truesize);
677 self->stats.tx_bytes += self->tx_buff.len;
679 /* We are transmitting */
680 self->transmitting = 1;
682 /* Turn on transmit finished interrupt. Will fire immediately! */
683 outb(UART_IER_THRI, iobase+UART_IER);
685 dev->trans_start = jiffies;
686 spin_unlock_irqrestore(&self->lock, flags);
688 dev_kfree_skb(skb);
690 return 0;
694 * Function irport_write (driver)
696 * Fill Tx FIFO with transmit data
698 * Called only from irport_write_wakeup()
700 static inline int irport_write(int iobase, int fifo_size, __u8 *buf, int len)
702 int actual = 0;
704 /* Fill FIFO with current frame */
705 while ((actual < fifo_size) && (actual < len)) {
706 /* Transmit next byte */
707 outb(buf[actual], iobase+UART_TX);
709 actual++;
712 return actual;
716 * Function irport_write_wakeup (tty)
718 * Called by the driver when there's room for more data. If we have
719 * more packets to send, we send them here.
721 * Called only from irport_interrupt()
722 * Make sure this function is *not* called while we are receiving,
723 * otherwise we will reset fifo and loose data :-(
725 static inline void irport_write_wakeup(struct irport_cb *self)
727 int actual = 0;
728 int iobase;
729 unsigned int fcr;
731 ASSERT(self != NULL, return;);
733 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
735 iobase = self->io.sir_base;
737 /* Finished with frame? */
738 if (self->tx_buff.len > 0) {
739 /* Write data left in transmit buffer */
740 actual = irport_write(iobase, self->io.fifo_size,
741 self->tx_buff.data, self->tx_buff.len);
742 self->tx_buff.data += actual;
743 self->tx_buff.len -= actual;
744 } else {
746 * Now serial buffer is almost free & we can start
747 * transmission of another packet. But first we must check
748 * if we need to change the speed of the hardware
750 if (self->new_speed) {
751 irport_wait_hw_transmitter_finish(self);
752 irda_task_execute(self, __irport_change_speed,
753 irport_change_speed_complete,
754 NULL, (void *) self->new_speed);
755 self->new_speed = 0;
756 } else {
757 /* Tell network layer that we want more frames */
758 netif_wake_queue(self->netdev);
760 self->stats.tx_packets++;
763 * Reset Rx FIFO to make sure that all reflected transmit data
764 * is discarded. This is needed for half duplex operation
766 fcr = irport_get_fcr(self->io.speed);
767 fcr |= UART_FCR_CLEAR_RCVR;
768 outb(fcr, iobase+UART_FCR);
770 /* Finished transmitting */
771 self->transmitting = 0;
773 /* Turn on receive interrupts */
774 outb(UART_IER_RDI, iobase+UART_IER);
776 IRDA_DEBUG(1, "%s() : finished Tx\n", __FUNCTION__);
781 * Function irport_receive (self)
783 * Receive one frame from the infrared port
785 * Called only from irport_interrupt()
787 static inline void irport_receive(struct irport_cb *self)
789 int boguscount = 0;
790 int iobase;
792 ASSERT(self != NULL, return;);
794 iobase = self->io.sir_base;
797 * Receive all characters in Rx FIFO, unwrap and unstuff them.
798 * async_unwrap_char will deliver all found frames
800 do {
801 async_unwrap_char(self->netdev, &self->stats, &self->rx_buff,
802 inb(iobase+UART_RX));
804 /* Make sure we don't stay here too long */
805 if (boguscount++ > 32) {
806 IRDA_DEBUG(2,"%s(), breaking!\n", __FUNCTION__);
807 break;
809 } while (inb(iobase+UART_LSR) & UART_LSR_DR);
813 * Function irport_interrupt (irq, dev_id, regs)
815 * Interrupt handler
817 irqreturn_t irport_interrupt(int irq, void *dev_id, struct pt_regs *regs)
819 struct net_device *dev = (struct net_device *) dev_id;
820 struct irport_cb *self;
821 int boguscount = 0;
822 int iobase;
823 int iir, lsr;
824 int handled = 0;
826 if (!dev) {
827 WARNING("%s() irq %d for unknown device.\n", __FUNCTION__, irq);
828 return IRQ_NONE;
830 self = (struct irport_cb *) dev->priv;
832 spin_lock(&self->lock);
834 iobase = self->io.sir_base;
836 /* Cut'n'paste interrupt routine from serial.c
837 * This version try to minimise latency and I/O operations.
838 * Simplified and modified to enforce half duplex operation.
839 * - Jean II */
841 /* Check status even is iir reg is cleared, more robust and
842 * eliminate a read on the I/O bus - Jean II */
843 do {
844 /* Get interrupt status ; Clear interrupt */
845 lsr = inb(iobase+UART_LSR);
847 /* Are we receiving or transmitting ? */
848 if(!self->transmitting) {
849 /* Received something ? */
850 if (lsr & UART_LSR_DR)
851 irport_receive(self);
852 } else {
853 /* Room in Tx fifo ? */
854 if (lsr & (UART_LSR_THRE | UART_LSR_TEMT))
855 irport_write_wakeup(self);
858 /* A bit hackish, but working as expected... Jean II */
859 if(lsr & (UART_LSR_THRE | UART_LSR_TEMT | UART_LSR_DR))
860 handled = 1;
862 /* Make sure we don't stay here to long */
863 if (boguscount++ > 10) {
864 WARNING("%s() irq handler looping : lsr=%02x\n",
865 __FUNCTION__, lsr);
866 break;
869 /* Read interrupt register */
870 iir = inb(iobase+UART_IIR);
872 /* Enable this debug only when no other options and at low
873 * bit rates, otherwise it may cause Rx overruns (lsr=63).
874 * - Jean II */
875 IRDA_DEBUG(6, "%s(), iir=%02x, lsr=%02x, iobase=%#x\n",
876 __FUNCTION__, iir, lsr, iobase);
878 /* As long as interrupt pending... */
879 } while ((iir & UART_IIR_NO_INT) == 0);
881 spin_unlock(&self->lock);
882 return IRQ_RETVAL(handled);
886 * Function irport_net_open (dev)
888 * Network device is taken up. Usually this is done by "ifconfig irda0 up"
891 int irport_net_open(struct net_device *dev)
893 struct irport_cb *self;
894 int iobase;
895 char hwname[16];
896 unsigned long flags;
898 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
900 ASSERT(dev != NULL, return -1;);
901 self = (struct irport_cb *) dev->priv;
903 iobase = self->io.sir_base;
905 if (request_irq(self->io.irq, self->interrupt, 0, dev->name,
906 (void *) dev)) {
907 IRDA_DEBUG(0, "%s(), unable to allocate irq=%d\n",
908 __FUNCTION__, self->io.irq);
909 return -EAGAIN;
912 spin_lock_irqsave(&self->lock, flags);
913 /* Init uart */
914 irport_start(self);
915 /* Set 9600 bauds per default, including at the dongle */
916 irda_task_execute(self, __irport_change_speed,
917 irport_change_speed_complete,
918 NULL, (void *) 9600);
919 spin_unlock_irqrestore(&self->lock, flags);
922 /* Give self a hardware name */
923 sprintf(hwname, "SIR @ 0x%03x", self->io.sir_base);
926 * Open new IrLAP layer instance, now that everything should be
927 * initialized properly
929 self->irlap = irlap_open(dev, &self->qos, hwname);
931 /* Ready to play! */
933 netif_start_queue(dev);
935 return 0;
939 * Function irport_net_close (self)
941 * Network device is taken down. Usually this is done by
942 * "ifconfig irda0 down"
944 int irport_net_close(struct net_device *dev)
946 struct irport_cb *self;
947 int iobase;
948 unsigned long flags;
950 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
952 ASSERT(dev != NULL, return -1;);
953 self = (struct irport_cb *) dev->priv;
955 ASSERT(self != NULL, return -1;);
957 iobase = self->io.sir_base;
959 /* Stop device */
960 netif_stop_queue(dev);
962 /* Stop and remove instance of IrLAP */
963 if (self->irlap)
964 irlap_close(self->irlap);
965 self->irlap = NULL;
967 spin_lock_irqsave(&self->lock, flags);
968 irport_stop(self);
969 spin_unlock_irqrestore(&self->lock, flags);
971 free_irq(self->io.irq, dev);
973 return 0;
977 * Function irport_is_receiving (self)
979 * Returns true is we are currently receiving data
982 static inline int irport_is_receiving(struct irport_cb *self)
984 return (self->rx_buff.state != OUTSIDE_FRAME);
988 * Function irport_set_dtr_rts (tty, dtr, rts)
990 * This function can be used by dongles etc. to set or reset the status
991 * of the dtr and rts lines
993 static int irport_set_dtr_rts(struct net_device *dev, int dtr, int rts)
995 struct irport_cb *self = dev->priv;
996 int iobase;
998 ASSERT(self != NULL, return -1;);
1000 iobase = self->io.sir_base;
1002 if (dtr)
1003 dtr = UART_MCR_DTR;
1004 if (rts)
1005 rts = UART_MCR_RTS;
1007 outb(dtr|rts|UART_MCR_OUT2, iobase+UART_MCR);
1009 return 0;
1012 static int irport_raw_write(struct net_device *dev, __u8 *buf, int len)
1014 struct irport_cb *self = (struct irport_cb *) dev->priv;
1015 int actual = 0;
1016 int iobase;
1018 ASSERT(self != NULL, return -1;);
1020 iobase = self->io.sir_base;
1022 /* Tx FIFO should be empty! */
1023 if (!(inb(iobase+UART_LSR) & UART_LSR_THRE)) {
1024 IRDA_DEBUG( 0, "%s(), failed, fifo not empty!\n", __FUNCTION__);
1025 return -1;
1028 /* Fill FIFO with current frame */
1029 while (actual < len) {
1030 /* Transmit next byte */
1031 outb(buf[actual], iobase+UART_TX);
1032 actual++;
1035 return actual;
1039 * Function irport_net_ioctl (dev, rq, cmd)
1041 * Process IOCTL commands for this device
1044 static int irport_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1046 struct if_irda_req *irq = (struct if_irda_req *) rq;
1047 struct irport_cb *self;
1048 dongle_t *dongle;
1049 unsigned long flags;
1050 int ret = 0;
1052 ASSERT(dev != NULL, return -1;);
1054 self = dev->priv;
1056 ASSERT(self != NULL, return -1;);
1058 IRDA_DEBUG(2, "%s(), %s, (cmd=0x%X)\n", __FUNCTION__, dev->name, cmd);
1060 switch (cmd) {
1061 case SIOCSBANDWIDTH: /* Set bandwidth */
1062 if (!capable(CAP_NET_ADMIN))
1063 ret = -EPERM;
1064 else
1065 irda_task_execute(self, __irport_change_speed, NULL,
1066 NULL, (void *) irq->ifr_baudrate);
1067 break;
1068 case SIOCSDONGLE: /* Set dongle */
1069 if (!capable(CAP_NET_ADMIN)) {
1070 ret = -EPERM;
1071 break;
1074 /* Locking :
1075 * irda_device_dongle_init() can't be locked.
1076 * irda_task_execute() doesn't need to be locked.
1077 * Jean II
1080 /* Initialize dongle */
1081 dongle = irda_device_dongle_init(dev, irq->ifr_dongle);
1082 if (!dongle)
1083 break;
1085 dongle->set_mode = NULL;
1086 dongle->read = NULL;
1087 dongle->write = irport_raw_write;
1088 dongle->set_dtr_rts = irport_set_dtr_rts;
1090 /* Now initialize the dongle! */
1091 dongle->issue->open(dongle, &self->qos);
1093 /* Reset dongle */
1094 irda_task_execute(dongle, dongle->issue->reset, NULL, NULL,
1095 NULL);
1097 /* Make dongle available to driver only now to avoid
1098 * race conditions - Jean II */
1099 self->dongle = dongle;
1100 break;
1101 case SIOCSMEDIABUSY: /* Set media busy */
1102 if (!capable(CAP_NET_ADMIN)) {
1103 ret = -EPERM;
1104 break;
1107 irda_device_set_media_busy(self->netdev, TRUE);
1108 break;
1109 case SIOCGRECEIVING: /* Check if we are receiving right now */
1110 irq->ifr_receiving = irport_is_receiving(self);
1111 break;
1112 case SIOCSDTRRTS:
1113 if (!capable(CAP_NET_ADMIN)) {
1114 ret = -EPERM;
1115 break;
1118 /* No real need to lock... */
1119 spin_lock_irqsave(&self->lock, flags);
1120 irport_set_dtr_rts(dev, irq->ifr_dtr, irq->ifr_rts);
1121 spin_unlock_irqrestore(&self->lock, flags);
1122 break;
1123 default:
1124 ret = -EOPNOTSUPP;
1127 return ret;
1130 static struct net_device_stats *irport_net_get_stats(struct net_device *dev)
1132 struct irport_cb *self = (struct irport_cb *) dev->priv;
1134 return &self->stats;
1137 MODULE_PARM(io, "1-4i");
1138 MODULE_PARM_DESC(io, "Base I/O addresses");
1139 MODULE_PARM(irq, "1-4i");
1140 MODULE_PARM_DESC(irq, "IRQ lines");
1142 MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
1143 MODULE_DESCRIPTION("Half duplex serial driver for IrDA SIR mode");
1144 MODULE_LICENSE("GPL");
1146 module_init(irport_init);
1147 module_exit(irport_cleanup);