1 /*********************************************************************
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,
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>
59 #include <net/irda/irda.h>
60 #include <net/irda/wrapper.h>
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,
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);
112 * Allocate new instance of the driver
114 dev = alloc_irdadev(sizeof(struct irport_cb));
116 IRDA_ERROR("%s(), can't allocate memory for "
117 "irda device!\n", __FUNCTION__);
122 spin_lock_init(&self->lock);
124 /* Need to store self somewhere */
130 self->io.sir_base = iobase;
131 self->io.sir_ext = IO_EXTENT;
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|
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,
148 if (self->rx_buff.skb == NULL) {
149 IRDA_ERROR("%s(), can't allocate memory for "
150 "receive buffer!\n", __FUNCTION__);
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,
169 if (self->tx_buff.head == NULL) {
170 IRDA_ERROR("%s(), can't allocate memory for "
171 "transmit buffer!\n", __FUNCTION__);
175 self->tx_buff.data = self->tx_buff.head;
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;
198 if (register_netdev(dev)) {
199 IRDA_ERROR("%s(), register_netdev() failed!\n", __FUNCTION__);
202 IRDA_MESSAGE("IrDA: Registered device %s (irport io=0x%X irq=%d)\n",
203 dev->name, iobase, irq);
207 kfree(self->tx_buff.head);
209 kfree_skb(self->rx_buff.skb);
214 release_region(iobase, IO_EXTENT);
219 static int irport_close(struct irport_cb *self)
221 IRDA_ASSERT(self != NULL, return -1;);
223 /* We are not using any dongle anymore! */
225 irda_device_dongle_cleanup(self->dongle);
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);
249 static void irport_stop(struct irport_cb *self)
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;
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)
271 iobase = self->io.sir_base;
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 */
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.
304 fcr |= UART_FCR_TRIGGER_1;
306 //fcr |= UART_FCR_TRIGGER_14;
307 fcr |= UART_FCR_TRIGGER_8;
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;
323 unsigned int fcr; /* FIFO control reg */
324 unsigned int lcr; /* Line control reg */
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;
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.
390 if (!spin_is_locked(&self->lock)) {
391 spin_lock_irqsave(&self->lock, flags);
395 switch (task->state) {
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);
408 irda_task_next_state(task, IRDA_TASK_CHILD_INIT);
410 irda_task_next_state(task, IRDA_TASK_CHILD_DONE);
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);
427 /* Child finished immediately */
428 irda_task_next_state(task, IRDA_TASK_CHILD_DONE);
430 case IRDA_TASK_CHILD_WAIT:
431 IRDA_WARNING("%s(), changing speed of dongle timed out!\n", __FUNCTION__);
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);
441 IRDA_ERROR("%s(), unknown state %d\n",
442 __FUNCTION__, task->state);
443 irda_task_next_state(task, IRDA_TASK_DONE);
447 /* Put stuff in the state we found them - Jean II */
449 spin_unlock_irqrestore(&self->lock, flags);
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);
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;
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 */
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.
549 static void irport_wait_hw_transmitter_finish(struct irport_cb *self)
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))
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;
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 */
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.
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);
616 self->new_speed = speed;
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);
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)
653 /* Fill FIFO with current frame */
654 while ((actual < fifo_size) && (actual < len)) {
655 /* Transmit next byte */
656 outb(buf[actual], iobase+UART_TX);
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)
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;
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);
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)
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
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__);
758 } while (inb(iobase+UART_LSR) & UART_LSR_DR);
762 * Function irport_interrupt (irq, dev_id)
766 static irqreturn_t irport_interrupt(int irq, void *dev_id)
768 struct net_device *dev = dev_id;
769 struct irport_cb *self;
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.
786 /* Check status even is iir reg is cleared, more robust and
787 * eliminate a read on the I/O bus - Jean II */
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);
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))
807 /* Make sure we don't stay here to long */
808 if (boguscount++ > 10) {
809 IRDA_WARNING("%s() irq handler looping : lsr=%02x\n",
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).
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;
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,
852 IRDA_DEBUG(0, "%s(), unable to allocate irq=%d\n",
853 __FUNCTION__, self->io.irq);
857 spin_lock_irqsave(&self->lock, flags);
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);
878 netif_start_queue(dev);
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;
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;
905 netif_stop_queue(dev);
907 /* Stop and remove instance of IrLAP */
909 irlap_close(self->irlap);
912 spin_lock_irqsave(&self->lock, flags);
914 spin_unlock_irqrestore(&self->lock, flags);
916 free_irq(self->io.irq, dev);
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;
943 IRDA_ASSERT(self != NULL, return -1;);
945 iobase = self->io.sir_base;
952 outb(dtr|rts|UART_MCR_OUT2, iobase+UART_MCR);
957 static int irport_raw_write(struct net_device *dev, __u8 *buf, int len)
959 struct irport_cb *self = (struct irport_cb *) dev->priv;
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__);
973 /* Fill FIFO with current frame */
974 while (actual < len) {
975 /* Transmit next byte */
976 outb(buf[actual], iobase+UART_TX);
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;
997 IRDA_ASSERT(dev != NULL, return -1;);
1001 IRDA_ASSERT(self != NULL, return -1;);
1003 IRDA_DEBUG(2, "%s(), %s, (cmd=0x%X)\n", __FUNCTION__, dev->name, cmd);
1006 case SIOCSBANDWIDTH: /* Set bandwidth */
1007 if (!capable(CAP_NET_ADMIN))
1010 irda_task_execute(self, __irport_change_speed, NULL,
1011 NULL, (void *) irq->ifr_baudrate);
1013 case SIOCSDONGLE: /* Set dongle */
1014 if (!capable(CAP_NET_ADMIN)) {
1020 * irda_device_dongle_init() can't be locked.
1021 * irda_task_execute() doesn't need to be locked.
1025 /* Initialize dongle */
1026 dongle = irda_device_dongle_init(dev, irq->ifr_dongle);
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);
1039 irda_task_execute(dongle, dongle->issue->reset, NULL, NULL,
1042 /* Make dongle available to driver only now to avoid
1043 * race conditions - Jean II */
1044 self->dongle = dongle;
1046 case SIOCSMEDIABUSY: /* Set media busy */
1047 if (!capable(CAP_NET_ADMIN)) {
1052 irda_device_set_media_busy(self->netdev, TRUE);
1054 case SIOCGRECEIVING: /* Check if we are receiving right now */
1055 irq->ifr_receiving = irport_is_receiving(self);
1058 if (!capable(CAP_NET_ADMIN)) {
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);
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)
1086 for (i=0; (io[i] < 2000) && (i < ARRAY_SIZE(dev_self)); i++) {
1087 if (irport_open(i, io[i], irq[i]) != NULL)
1091 * Maybe something failed, but we can still be usable for FIR drivers
1097 * Function irport_cleanup ()
1099 * Close all configured ports
1102 static void __exit irport_cleanup(void)
1106 IRDA_DEBUG( 4, "%s()\n", __FUNCTION__);
1108 for (i=0; i < ARRAY_SIZE(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);