remove unused TIF_NOTIFY_RESUME flag
[pv_ops_mirror.git] / drivers / net / irda / irport.c
blob20732458f5ac20cb41cea01cfe14e5ae5d2ce2c8
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>
54 #include <linux/bitops.h>
56 #include <asm/system.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 static irqreturn_t irport_interrupt(int irq, void *dev_id);
91 static int irport_hard_xmit(struct sk_buff *skb, struct net_device *dev);
92 static void irport_change_speed(void *priv, __u32 speed);
93 static int irport_net_open(struct net_device *dev);
94 static int irport_net_close(struct net_device *dev);
96 static struct irport_cb *
97 irport_open(int i, unsigned int iobase, unsigned int irq)
99 struct net_device *dev;
100 struct irport_cb *self;
102 IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
104 /* Lock the port that we need */
105 if (!request_region(iobase, IO_EXTENT, driver_name)) {
106 IRDA_DEBUG(0, "%s(), can't get iobase of 0x%03x\n",
107 __FUNCTION__, iobase);
108 goto err_out1;
112 * Allocate new instance of the driver
114 dev = alloc_irdadev(sizeof(struct irport_cb));
115 if (!dev) {
116 IRDA_ERROR("%s(), can't allocate memory for "
117 "irda device!\n", __FUNCTION__);
118 goto err_out2;
121 self = dev->priv;
122 spin_lock_init(&self->lock);
124 /* Need to store self somewhere */
125 dev_self[i] = self;
126 self->priv = self;
127 self->index = i;
129 /* Initialize IO */
130 self->io.sir_base = iobase;
131 self->io.sir_ext = IO_EXTENT;
132 self->io.irq = irq;
133 self->io.fifo_size = 16; /* 16550A and compatible */
135 /* Initialize QoS for this device */
136 irda_init_max_qos_capabilies(&self->qos);
138 self->qos.baud_rate.bits = IR_9600|IR_19200|IR_38400|IR_57600|
139 IR_115200;
141 self->qos.min_turn_time.bits = qos_mtt_bits;
142 irda_qos_bits_to_value(&self->qos);
144 /* Bootstrap ZeroCopy Rx */
145 self->rx_buff.truesize = IRDA_SKB_MAX_MTU;
146 self->rx_buff.skb = __dev_alloc_skb(self->rx_buff.truesize,
147 GFP_KERNEL);
148 if (self->rx_buff.skb == NULL) {
149 IRDA_ERROR("%s(), can't allocate memory for "
150 "receive buffer!\n", __FUNCTION__);
151 goto err_out3;
153 skb_reserve(self->rx_buff.skb, 1);
154 self->rx_buff.head = self->rx_buff.skb->data;
155 /* No need to memset the buffer, unless you are really pedantic */
157 /* Finish setup the Rx buffer descriptor */
158 self->rx_buff.in_frame = FALSE;
159 self->rx_buff.state = OUTSIDE_FRAME;
160 self->rx_buff.data = self->rx_buff.head;
162 /* Specify how much memory we want */
163 self->tx_buff.truesize = 4000;
165 /* Allocate memory if needed */
166 if (self->tx_buff.truesize > 0) {
167 self->tx_buff.head = kzalloc(self->tx_buff.truesize,
168 GFP_KERNEL);
169 if (self->tx_buff.head == NULL) {
170 IRDA_ERROR("%s(), can't allocate memory for "
171 "transmit buffer!\n", __FUNCTION__);
172 goto err_out4;
175 self->tx_buff.data = self->tx_buff.head;
177 self->netdev = dev;
178 /* Keep track of module usage */
179 SET_MODULE_OWNER(dev);
181 /* May be overridden by piggyback drivers */
182 self->interrupt = irport_interrupt;
183 self->change_speed = irport_change_speed;
185 /* Override the network functions we need to use */
186 dev->hard_start_xmit = irport_hard_xmit;
187 dev->tx_timeout = irport_timeout;
188 dev->watchdog_timeo = HZ; /* Allow time enough for speed change */
189 dev->open = irport_net_open;
190 dev->stop = irport_net_close;
191 dev->get_stats = irport_net_get_stats;
192 dev->do_ioctl = irport_net_ioctl;
194 /* Make ifconfig display some details */
195 dev->base_addr = iobase;
196 dev->irq = irq;
198 if (register_netdev(dev)) {
199 IRDA_ERROR("%s(), register_netdev() failed!\n", __FUNCTION__);
200 goto err_out5;
202 IRDA_MESSAGE("IrDA: Registered device %s (irport io=0x%X irq=%d)\n",
203 dev->name, iobase, irq);
205 return self;
206 err_out5:
207 kfree(self->tx_buff.head);
208 err_out4:
209 kfree_skb(self->rx_buff.skb);
210 err_out3:
211 free_netdev(dev);
212 dev_self[i] = NULL;
213 err_out2:
214 release_region(iobase, IO_EXTENT);
215 err_out1:
216 return NULL;
219 static int irport_close(struct irport_cb *self)
221 IRDA_ASSERT(self != NULL, return -1;);
223 /* We are not using any dongle anymore! */
224 if (self->dongle)
225 irda_device_dongle_cleanup(self->dongle);
226 self->dongle = NULL;
228 /* Remove netdevice */
229 unregister_netdev(self->netdev);
231 /* Release the IO-port that this driver is using */
232 IRDA_DEBUG(0 , "%s(), Releasing Region %03x\n",
233 __FUNCTION__, self->io.sir_base);
234 release_region(self->io.sir_base, self->io.sir_ext);
236 kfree(self->tx_buff.head);
238 if (self->rx_buff.skb)
239 kfree_skb(self->rx_buff.skb);
240 self->rx_buff.skb = NULL;
242 /* Remove ourselves */
243 dev_self[self->index] = NULL;
244 free_netdev(self->netdev);
246 return 0;
249 static void irport_stop(struct irport_cb *self)
251 int iobase;
253 iobase = self->io.sir_base;
255 /* We can't lock, we may be called from a FIR driver - Jean II */
257 /* We are not transmitting any more */
258 self->transmitting = 0;
260 /* Reset UART */
261 outb(0, iobase+UART_MCR);
263 /* Turn off interrupts */
264 outb(0, iobase+UART_IER);
267 static void irport_start(struct irport_cb *self)
269 int iobase;
271 iobase = self->io.sir_base;
273 irport_stop(self);
275 /* We can't lock, we may be called from a FIR driver - Jean II */
277 /* Initialize UART */
278 outb(UART_LCR_WLEN8, iobase+UART_LCR); /* Reset DLAB */
279 outb((UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2), iobase+UART_MCR);
281 /* Turn on interrups */
282 outb(UART_IER_RLSI | UART_IER_RDI |UART_IER_THRI, iobase+UART_IER);
286 * Function irport_get_fcr (speed)
288 * Compute value of fcr
291 static inline unsigned int irport_get_fcr(__u32 speed)
293 unsigned int fcr; /* FIFO control reg */
295 /* Enable fifos */
296 fcr = UART_FCR_ENABLE_FIFO;
299 * Use trigger level 1 to avoid 3 ms. timeout delay at 9600 bps, and
300 * almost 1,7 ms at 19200 bps. At speeds above that we can just forget
301 * about this timeout since it will always be fast enough.
303 if (speed < 38400)
304 fcr |= UART_FCR_TRIGGER_1;
305 else
306 //fcr |= UART_FCR_TRIGGER_14;
307 fcr |= UART_FCR_TRIGGER_8;
309 return(fcr);
313 * Function irport_change_speed (self, speed)
315 * Set speed of IrDA port to specified baudrate
317 * This function should be called with irq off and spin-lock.
319 static void irport_change_speed(void *priv, __u32 speed)
321 struct irport_cb *self = (struct irport_cb *) priv;
322 int iobase;
323 unsigned int fcr; /* FIFO control reg */
324 unsigned int lcr; /* Line control reg */
325 int divisor;
327 IRDA_ASSERT(self != NULL, return;);
328 IRDA_ASSERT(speed != 0, return;);
330 IRDA_DEBUG(1, "%s(), Setting speed to: %d - iobase=%#x\n",
331 __FUNCTION__, speed, self->io.sir_base);
333 /* We can't lock, we may be called from a FIR driver - Jean II */
335 iobase = self->io.sir_base;
337 /* Update accounting for new speed */
338 self->io.speed = speed;
340 /* Turn off interrupts */
341 outb(0, iobase+UART_IER);
343 divisor = SPEED_MAX/speed;
345 /* Get proper fifo configuration */
346 fcr = irport_get_fcr(speed);
348 /* IrDA ports use 8N1 */
349 lcr = UART_LCR_WLEN8;
351 outb(UART_LCR_DLAB | lcr, iobase+UART_LCR); /* Set DLAB */
352 outb(divisor & 0xff, iobase+UART_DLL); /* Set speed */
353 outb(divisor >> 8, iobase+UART_DLM);
354 outb(lcr, iobase+UART_LCR); /* Set 8N1 */
355 outb(fcr, iobase+UART_FCR); /* Enable FIFO's */
357 /* Turn on interrups */
358 /* This will generate a fatal interrupt storm.
359 * People calling us will do that properly - Jean II */
360 //outb(/*UART_IER_RLSI|*/UART_IER_RDI/*|UART_IER_THRI*/, iobase+UART_IER);
364 * Function __irport_change_speed (instance, state, param)
366 * State machine for changing speed of the device. We do it this way since
367 * we cannot use schedule_timeout() when we are in interrupt context
370 static int __irport_change_speed(struct irda_task *task)
372 struct irport_cb *self;
373 __u32 speed = (__u32) task->param;
374 unsigned long flags = 0;
375 int wasunlocked = 0;
376 int ret = 0;
378 IRDA_DEBUG(2, "%s(), <%ld>\n", __FUNCTION__, jiffies);
380 self = (struct irport_cb *) task->instance;
382 IRDA_ASSERT(self != NULL, return -1;);
384 /* Locking notes : this function may be called from irq context with
385 * spinlock, via irport_write_wakeup(), or from non-interrupt without
386 * spinlock (from the task timer). Yuck !
387 * This is ugly, and unsafe is the spinlock is not already acquired.
388 * This will be fixed when irda-task get rewritten.
389 * Jean II */
390 if (!spin_is_locked(&self->lock)) {
391 spin_lock_irqsave(&self->lock, flags);
392 wasunlocked = 1;
395 switch (task->state) {
396 case IRDA_TASK_INIT:
397 case IRDA_TASK_WAIT:
398 /* Are we ready to change speed yet? */
399 if (self->tx_buff.len > 0) {
400 task->state = IRDA_TASK_WAIT;
402 /* Try again later */
403 ret = msecs_to_jiffies(20);
404 break;
407 if (self->dongle)
408 irda_task_next_state(task, IRDA_TASK_CHILD_INIT);
409 else
410 irda_task_next_state(task, IRDA_TASK_CHILD_DONE);
411 break;
412 case IRDA_TASK_CHILD_INIT:
413 /* Go to default speed */
414 self->change_speed(self->priv, 9600);
416 /* Change speed of dongle */
417 if (irda_task_execute(self->dongle,
418 self->dongle->issue->change_speed,
419 NULL, task, (void *) speed))
421 /* Dongle need more time to change its speed */
422 irda_task_next_state(task, IRDA_TASK_CHILD_WAIT);
424 /* Give dongle 1 sec to finish */
425 ret = msecs_to_jiffies(1000);
426 } else
427 /* Child finished immediately */
428 irda_task_next_state(task, IRDA_TASK_CHILD_DONE);
429 break;
430 case IRDA_TASK_CHILD_WAIT:
431 IRDA_WARNING("%s(), changing speed of dongle timed out!\n", __FUNCTION__);
432 ret = -1;
433 break;
434 case IRDA_TASK_CHILD_DONE:
435 /* Finally we are ready to change the speed */
436 self->change_speed(self->priv, speed);
438 irda_task_next_state(task, IRDA_TASK_DONE);
439 break;
440 default:
441 IRDA_ERROR("%s(), unknown state %d\n",
442 __FUNCTION__, task->state);
443 irda_task_next_state(task, IRDA_TASK_DONE);
444 ret = -1;
445 break;
447 /* Put stuff in the state we found them - Jean II */
448 if(wasunlocked) {
449 spin_unlock_irqrestore(&self->lock, flags);
452 return ret;
456 * Function irport_change_speed_complete (task)
458 * Called when the change speed operation completes
461 static int irport_change_speed_complete(struct irda_task *task)
463 struct irport_cb *self;
465 IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
467 self = (struct irport_cb *) task->instance;
469 IRDA_ASSERT(self != NULL, return -1;);
470 IRDA_ASSERT(self->netdev != NULL, return -1;);
472 /* Finished changing speed, so we are not busy any longer */
473 /* Signal network layer so it can try to send the frame */
475 netif_wake_queue(self->netdev);
477 return 0;
481 * Function irport_timeout (struct net_device *dev)
483 * The networking layer thinks we timed out.
487 static void irport_timeout(struct net_device *dev)
489 struct irport_cb *self;
490 int iobase;
491 int iir, lsr;
492 unsigned long flags;
494 self = (struct irport_cb *) dev->priv;
495 IRDA_ASSERT(self != NULL, return;);
496 iobase = self->io.sir_base;
498 IRDA_WARNING("%s: transmit timed out, jiffies = %ld, trans_start = %ld\n",
499 dev->name, jiffies, dev->trans_start);
500 spin_lock_irqsave(&self->lock, flags);
502 /* Debug what's happening... */
504 /* Get interrupt status */
505 lsr = inb(iobase+UART_LSR);
506 /* Read interrupt register */
507 iir = inb(iobase+UART_IIR);
508 IRDA_DEBUG(0, "%s(), iir=%02x, lsr=%02x, iobase=%#x\n",
509 __FUNCTION__, iir, lsr, iobase);
511 IRDA_DEBUG(0, "%s(), transmitting=%d, remain=%d, done=%td\n",
512 __FUNCTION__, self->transmitting, self->tx_buff.len,
513 self->tx_buff.data - self->tx_buff.head);
515 /* Now, restart the port */
516 irport_start(self);
517 self->change_speed(self->priv, self->io.speed);
518 /* This will re-enable irqs */
519 outb(/*UART_IER_RLSI|*/UART_IER_RDI/*|UART_IER_THRI*/, iobase+UART_IER);
520 dev->trans_start = jiffies;
521 spin_unlock_irqrestore(&self->lock, flags);
523 netif_wake_queue(dev);
527 * Function irport_wait_hw_transmitter_finish ()
529 * Wait for the real end of HW transmission
531 * The UART is a strict FIFO, and we get called only when we have finished
532 * pushing data to the FIFO, so the maximum amount of time we must wait
533 * is only for the FIFO to drain out.
535 * We use a simple calibrated loop. We may need to adjust the loop
536 * delay (udelay) to balance I/O traffic and latency. And we also need to
537 * adjust the maximum timeout.
538 * It would probably be better to wait for the proper interrupt,
539 * but it doesn't seem to be available.
541 * We can't use jiffies or kernel timers because :
542 * 1) We are called from the interrupt handler, which disable softirqs,
543 * so jiffies won't be increased
544 * 2) Jiffies granularity is usually very coarse (10ms), and we don't
545 * want to wait that long to detect stuck hardware.
546 * Jean II
549 static void irport_wait_hw_transmitter_finish(struct irport_cb *self)
551 int iobase;
552 int count = 1000; /* 1 ms */
554 iobase = self->io.sir_base;
556 /* Calibrated busy loop */
557 while((count-- > 0) && !(inb(iobase+UART_LSR) & UART_LSR_TEMT))
558 udelay(1);
560 if(count == 0)
561 IRDA_DEBUG(0, "%s(): stuck transmitter\n", __FUNCTION__);
565 * Function irport_hard_start_xmit (struct sk_buff *skb, struct net_device *dev)
567 * Transmits the current frame until FIFO is full, then
568 * waits until the next transmitt interrupt, and continues until the
569 * frame is transmitted.
571 static int irport_hard_xmit(struct sk_buff *skb, struct net_device *dev)
573 struct irport_cb *self;
574 unsigned long flags;
575 int iobase;
576 s32 speed;
578 IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
580 IRDA_ASSERT(dev != NULL, return 0;);
582 self = (struct irport_cb *) dev->priv;
583 IRDA_ASSERT(self != NULL, return 0;);
585 iobase = self->io.sir_base;
587 netif_stop_queue(dev);
589 /* Make sure tests & speed change are atomic */
590 spin_lock_irqsave(&self->lock, flags);
592 /* Check if we need to change the speed */
593 speed = irda_get_next_speed(skb);
594 if ((speed != self->io.speed) && (speed != -1)) {
595 /* Check for empty frame */
596 if (!skb->len) {
598 * We send frames one by one in SIR mode (no
599 * pipelining), so at this point, if we were sending
600 * a previous frame, we just received the interrupt
601 * telling us it is finished (UART_IIR_THRI).
602 * Therefore, waiting for the transmitter to really
603 * finish draining the fifo won't take too long.
604 * And the interrupt handler is not expected to run.
605 * - Jean II */
606 irport_wait_hw_transmitter_finish(self);
607 /* Better go there already locked - Jean II */
608 irda_task_execute(self, __irport_change_speed,
609 irport_change_speed_complete,
610 NULL, (void *) speed);
611 dev->trans_start = jiffies;
612 spin_unlock_irqrestore(&self->lock, flags);
613 dev_kfree_skb(skb);
614 return 0;
615 } else
616 self->new_speed = speed;
619 /* Init tx buffer */
620 self->tx_buff.data = self->tx_buff.head;
622 /* Copy skb to tx_buff while wrapping, stuffing and making CRC */
623 self->tx_buff.len = async_wrap_skb(skb, self->tx_buff.data,
624 self->tx_buff.truesize);
626 self->stats.tx_bytes += self->tx_buff.len;
628 /* We are transmitting */
629 self->transmitting = 1;
631 /* Turn on transmit finished interrupt. Will fire immediately! */
632 outb(UART_IER_THRI, iobase+UART_IER);
634 dev->trans_start = jiffies;
635 spin_unlock_irqrestore(&self->lock, flags);
637 dev_kfree_skb(skb);
639 return 0;
643 * Function irport_write (driver)
645 * Fill Tx FIFO with transmit data
647 * Called only from irport_write_wakeup()
649 static inline int irport_write(int iobase, int fifo_size, __u8 *buf, int len)
651 int actual = 0;
653 /* Fill FIFO with current frame */
654 while ((actual < fifo_size) && (actual < len)) {
655 /* Transmit next byte */
656 outb(buf[actual], iobase+UART_TX);
658 actual++;
661 return actual;
665 * Function irport_write_wakeup (tty)
667 * Called by the driver when there's room for more data. If we have
668 * more packets to send, we send them here.
670 * Called only from irport_interrupt()
671 * Make sure this function is *not* called while we are receiving,
672 * otherwise we will reset fifo and loose data :-(
674 static inline void irport_write_wakeup(struct irport_cb *self)
676 int actual = 0;
677 int iobase;
678 unsigned int fcr;
680 IRDA_ASSERT(self != NULL, return;);
682 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
684 iobase = self->io.sir_base;
686 /* Finished with frame? */
687 if (self->tx_buff.len > 0) {
688 /* Write data left in transmit buffer */
689 actual = irport_write(iobase, self->io.fifo_size,
690 self->tx_buff.data, self->tx_buff.len);
691 self->tx_buff.data += actual;
692 self->tx_buff.len -= actual;
693 } else {
695 * Now serial buffer is almost free & we can start
696 * transmission of another packet. But first we must check
697 * if we need to change the speed of the hardware
699 if (self->new_speed) {
700 irport_wait_hw_transmitter_finish(self);
701 irda_task_execute(self, __irport_change_speed,
702 irport_change_speed_complete,
703 NULL, (void *) self->new_speed);
704 self->new_speed = 0;
705 } else {
706 /* Tell network layer that we want more frames */
707 netif_wake_queue(self->netdev);
709 self->stats.tx_packets++;
712 * Reset Rx FIFO to make sure that all reflected transmit data
713 * is discarded. This is needed for half duplex operation
715 fcr = irport_get_fcr(self->io.speed);
716 fcr |= UART_FCR_CLEAR_RCVR;
717 outb(fcr, iobase+UART_FCR);
719 /* Finished transmitting */
720 self->transmitting = 0;
722 /* Turn on receive interrupts */
723 outb(UART_IER_RDI, iobase+UART_IER);
725 IRDA_DEBUG(1, "%s() : finished Tx\n", __FUNCTION__);
730 * Function irport_receive (self)
732 * Receive one frame from the infrared port
734 * Called only from irport_interrupt()
736 static inline void irport_receive(struct irport_cb *self)
738 int boguscount = 0;
739 int iobase;
741 IRDA_ASSERT(self != NULL, return;);
743 iobase = self->io.sir_base;
746 * Receive all characters in Rx FIFO, unwrap and unstuff them.
747 * async_unwrap_char will deliver all found frames
749 do {
750 async_unwrap_char(self->netdev, &self->stats, &self->rx_buff,
751 inb(iobase+UART_RX));
753 /* Make sure we don't stay here too long */
754 if (boguscount++ > 32) {
755 IRDA_DEBUG(2,"%s(), breaking!\n", __FUNCTION__);
756 break;
758 } while (inb(iobase+UART_LSR) & UART_LSR_DR);
762 * Function irport_interrupt (irq, dev_id)
764 * Interrupt handler
766 static irqreturn_t irport_interrupt(int irq, void *dev_id)
768 struct net_device *dev = dev_id;
769 struct irport_cb *self;
770 int boguscount = 0;
771 int iobase;
772 int iir, lsr;
773 int handled = 0;
775 self = dev->priv;
777 spin_lock(&self->lock);
779 iobase = self->io.sir_base;
781 /* Cut'n'paste interrupt routine from serial.c
782 * This version try to minimise latency and I/O operations.
783 * Simplified and modified to enforce half duplex operation.
784 * - Jean II */
786 /* Check status even is iir reg is cleared, more robust and
787 * eliminate a read on the I/O bus - Jean II */
788 do {
789 /* Get interrupt status ; Clear interrupt */
790 lsr = inb(iobase+UART_LSR);
792 /* Are we receiving or transmitting ? */
793 if(!self->transmitting) {
794 /* Received something ? */
795 if (lsr & UART_LSR_DR)
796 irport_receive(self);
797 } else {
798 /* Room in Tx fifo ? */
799 if (lsr & (UART_LSR_THRE | UART_LSR_TEMT))
800 irport_write_wakeup(self);
803 /* A bit hackish, but working as expected... Jean II */
804 if(lsr & (UART_LSR_THRE | UART_LSR_TEMT | UART_LSR_DR))
805 handled = 1;
807 /* Make sure we don't stay here to long */
808 if (boguscount++ > 10) {
809 IRDA_WARNING("%s() irq handler looping : lsr=%02x\n",
810 __FUNCTION__, lsr);
811 break;
814 /* Read interrupt register */
815 iir = inb(iobase+UART_IIR);
817 /* Enable this debug only when no other options and at low
818 * bit rates, otherwise it may cause Rx overruns (lsr=63).
819 * - Jean II */
820 IRDA_DEBUG(6, "%s(), iir=%02x, lsr=%02x, iobase=%#x\n",
821 __FUNCTION__, iir, lsr, iobase);
823 /* As long as interrupt pending... */
824 } while ((iir & UART_IIR_NO_INT) == 0);
826 spin_unlock(&self->lock);
827 return IRQ_RETVAL(handled);
831 * Function irport_net_open (dev)
833 * Network device is taken up. Usually this is done by "ifconfig irda0 up"
836 static int irport_net_open(struct net_device *dev)
838 struct irport_cb *self;
839 int iobase;
840 char hwname[16];
841 unsigned long flags;
843 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
845 IRDA_ASSERT(dev != NULL, return -1;);
846 self = (struct irport_cb *) dev->priv;
848 iobase = self->io.sir_base;
850 if (request_irq(self->io.irq, self->interrupt, 0, dev->name,
851 (void *) dev)) {
852 IRDA_DEBUG(0, "%s(), unable to allocate irq=%d\n",
853 __FUNCTION__, self->io.irq);
854 return -EAGAIN;
857 spin_lock_irqsave(&self->lock, flags);
858 /* Init uart */
859 irport_start(self);
860 /* Set 9600 bauds per default, including at the dongle */
861 irda_task_execute(self, __irport_change_speed,
862 irport_change_speed_complete,
863 NULL, (void *) 9600);
864 spin_unlock_irqrestore(&self->lock, flags);
867 /* Give self a hardware name */
868 sprintf(hwname, "SIR @ 0x%03x", self->io.sir_base);
871 * Open new IrLAP layer instance, now that everything should be
872 * initialized properly
874 self->irlap = irlap_open(dev, &self->qos, hwname);
876 /* Ready to play! */
878 netif_start_queue(dev);
880 return 0;
884 * Function irport_net_close (self)
886 * Network device is taken down. Usually this is done by
887 * "ifconfig irda0 down"
889 static int irport_net_close(struct net_device *dev)
891 struct irport_cb *self;
892 int iobase;
893 unsigned long flags;
895 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
897 IRDA_ASSERT(dev != NULL, return -1;);
898 self = (struct irport_cb *) dev->priv;
900 IRDA_ASSERT(self != NULL, return -1;);
902 iobase = self->io.sir_base;
904 /* Stop device */
905 netif_stop_queue(dev);
907 /* Stop and remove instance of IrLAP */
908 if (self->irlap)
909 irlap_close(self->irlap);
910 self->irlap = NULL;
912 spin_lock_irqsave(&self->lock, flags);
913 irport_stop(self);
914 spin_unlock_irqrestore(&self->lock, flags);
916 free_irq(self->io.irq, dev);
918 return 0;
922 * Function irport_is_receiving (self)
924 * Returns true is we are currently receiving data
927 static inline int irport_is_receiving(struct irport_cb *self)
929 return (self->rx_buff.state != OUTSIDE_FRAME);
933 * Function irport_set_dtr_rts (tty, dtr, rts)
935 * This function can be used by dongles etc. to set or reset the status
936 * of the dtr and rts lines
938 static int irport_set_dtr_rts(struct net_device *dev, int dtr, int rts)
940 struct irport_cb *self = dev->priv;
941 int iobase;
943 IRDA_ASSERT(self != NULL, return -1;);
945 iobase = self->io.sir_base;
947 if (dtr)
948 dtr = UART_MCR_DTR;
949 if (rts)
950 rts = UART_MCR_RTS;
952 outb(dtr|rts|UART_MCR_OUT2, iobase+UART_MCR);
954 return 0;
957 static int irport_raw_write(struct net_device *dev, __u8 *buf, int len)
959 struct irport_cb *self = (struct irport_cb *) dev->priv;
960 int actual = 0;
961 int iobase;
963 IRDA_ASSERT(self != NULL, return -1;);
965 iobase = self->io.sir_base;
967 /* Tx FIFO should be empty! */
968 if (!(inb(iobase+UART_LSR) & UART_LSR_THRE)) {
969 IRDA_DEBUG( 0, "%s(), failed, fifo not empty!\n", __FUNCTION__);
970 return -1;
973 /* Fill FIFO with current frame */
974 while (actual < len) {
975 /* Transmit next byte */
976 outb(buf[actual], iobase+UART_TX);
977 actual++;
980 return actual;
984 * Function irport_net_ioctl (dev, rq, cmd)
986 * Process IOCTL commands for this device
989 static int irport_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
991 struct if_irda_req *irq = (struct if_irda_req *) rq;
992 struct irport_cb *self;
993 dongle_t *dongle;
994 unsigned long flags;
995 int ret = 0;
997 IRDA_ASSERT(dev != NULL, return -1;);
999 self = dev->priv;
1001 IRDA_ASSERT(self != NULL, return -1;);
1003 IRDA_DEBUG(2, "%s(), %s, (cmd=0x%X)\n", __FUNCTION__, dev->name, cmd);
1005 switch (cmd) {
1006 case SIOCSBANDWIDTH: /* Set bandwidth */
1007 if (!capable(CAP_NET_ADMIN))
1008 ret = -EPERM;
1009 else
1010 irda_task_execute(self, __irport_change_speed, NULL,
1011 NULL, (void *) irq->ifr_baudrate);
1012 break;
1013 case SIOCSDONGLE: /* Set dongle */
1014 if (!capable(CAP_NET_ADMIN)) {
1015 ret = -EPERM;
1016 break;
1019 /* Locking :
1020 * irda_device_dongle_init() can't be locked.
1021 * irda_task_execute() doesn't need to be locked.
1022 * Jean II
1025 /* Initialize dongle */
1026 dongle = irda_device_dongle_init(dev, irq->ifr_dongle);
1027 if (!dongle)
1028 break;
1030 dongle->set_mode = NULL;
1031 dongle->read = NULL;
1032 dongle->write = irport_raw_write;
1033 dongle->set_dtr_rts = irport_set_dtr_rts;
1035 /* Now initialize the dongle! */
1036 dongle->issue->open(dongle, &self->qos);
1038 /* Reset dongle */
1039 irda_task_execute(dongle, dongle->issue->reset, NULL, NULL,
1040 NULL);
1042 /* Make dongle available to driver only now to avoid
1043 * race conditions - Jean II */
1044 self->dongle = dongle;
1045 break;
1046 case SIOCSMEDIABUSY: /* Set media busy */
1047 if (!capable(CAP_NET_ADMIN)) {
1048 ret = -EPERM;
1049 break;
1052 irda_device_set_media_busy(self->netdev, TRUE);
1053 break;
1054 case SIOCGRECEIVING: /* Check if we are receiving right now */
1055 irq->ifr_receiving = irport_is_receiving(self);
1056 break;
1057 case SIOCSDTRRTS:
1058 if (!capable(CAP_NET_ADMIN)) {
1059 ret = -EPERM;
1060 break;
1063 /* No real need to lock... */
1064 spin_lock_irqsave(&self->lock, flags);
1065 irport_set_dtr_rts(dev, irq->ifr_dtr, irq->ifr_rts);
1066 spin_unlock_irqrestore(&self->lock, flags);
1067 break;
1068 default:
1069 ret = -EOPNOTSUPP;
1072 return ret;
1075 static struct net_device_stats *irport_net_get_stats(struct net_device *dev)
1077 struct irport_cb *self = (struct irport_cb *) dev->priv;
1079 return &self->stats;
1082 static int __init irport_init(void)
1084 int i;
1086 for (i=0; (io[i] < 2000) && (i < ARRAY_SIZE(dev_self)); i++) {
1087 if (irport_open(i, io[i], irq[i]) != NULL)
1088 return 0;
1091 * Maybe something failed, but we can still be usable for FIR drivers
1093 return 0;
1097 * Function irport_cleanup ()
1099 * Close all configured ports
1102 static void __exit irport_cleanup(void)
1104 int i;
1106 IRDA_DEBUG( 4, "%s()\n", __FUNCTION__);
1108 for (i=0; i < ARRAY_SIZE(dev_self); i++) {
1109 if (dev_self[i])
1110 irport_close(dev_self[i]);
1114 module_param_array(io, int, NULL, 0);
1115 MODULE_PARM_DESC(io, "Base I/O addresses");
1116 module_param_array(irq, int, NULL, 0);
1117 MODULE_PARM_DESC(irq, "IRQ lines");
1119 MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
1120 MODULE_DESCRIPTION("Half duplex serial driver for IrDA SIR mode");
1121 MODULE_LICENSE("GPL");
1123 module_init(irport_init);
1124 module_exit(irport_cleanup);