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                                     struct pt_regs *regs);
 
  92 static int irport_hard_xmit(struct sk_buff *skb, struct net_device *dev);
 
  93 static void irport_change_speed(void *priv, __u32 speed);
 
  94 static int irport_net_open(struct net_device *dev);
 
  95 static int irport_net_close(struct net_device *dev);
 
  97 static struct irport_cb *
 
  98 irport_open(int i, unsigned int iobase, unsigned int irq)
 
 100         struct net_device *dev;
 
 101         struct irport_cb *self;
 
 103         IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
 
 105         /* Lock the port that we need */
 
 106         if (!request_region(iobase, IO_EXTENT, driver_name)) {
 
 107                 IRDA_DEBUG(0, "%s(), can't get iobase of 0x%03x\n",
 
 108                            __FUNCTION__, iobase);
 
 113          *  Allocate new instance of the driver
 
 115         dev = alloc_irdadev(sizeof(struct irport_cb));
 
 117                 IRDA_ERROR("%s(), can't allocate memory for "
 
 118                            "irda device!\n", __FUNCTION__);
 
 123         spin_lock_init(&self->lock);
 
 125         /* Need to store self somewhere */
 
 131         self->io.sir_base  = iobase;
 
 132         self->io.sir_ext   = IO_EXTENT;
 
 134         self->io.fifo_size = 16;                /* 16550A and compatible */
 
 136         /* Initialize QoS for this device */
 
 137         irda_init_max_qos_capabilies(&self->qos);
 
 139         self->qos.baud_rate.bits = IR_9600|IR_19200|IR_38400|IR_57600|
 
 142         self->qos.min_turn_time.bits = qos_mtt_bits;
 
 143         irda_qos_bits_to_value(&self->qos);
 
 145         /* Bootstrap ZeroCopy Rx */
 
 146         self->rx_buff.truesize = IRDA_SKB_MAX_MTU;
 
 147         self->rx_buff.skb = __dev_alloc_skb(self->rx_buff.truesize,
 
 149         if (self->rx_buff.skb == NULL) {
 
 150                 IRDA_ERROR("%s(), can't allocate memory for "
 
 151                            "receive buffer!\n", __FUNCTION__);
 
 154         skb_reserve(self->rx_buff.skb, 1);
 
 155         self->rx_buff.head = self->rx_buff.skb->data;
 
 156         /* No need to memset the buffer, unless you are really pedantic */
 
 158         /* Finish setup the Rx buffer descriptor */
 
 159         self->rx_buff.in_frame = FALSE;
 
 160         self->rx_buff.state = OUTSIDE_FRAME;
 
 161         self->rx_buff.data = self->rx_buff.head;
 
 163         /* Specify how much memory we want */
 
 164         self->tx_buff.truesize = 4000;
 
 166         /* Allocate memory if needed */
 
 167         if (self->tx_buff.truesize > 0) {
 
 168                 self->tx_buff.head = (__u8 *) kmalloc(self->tx_buff.truesize, 
 
 170                 if (self->tx_buff.head == NULL) {
 
 171                         IRDA_ERROR("%s(), can't allocate memory for "
 
 172                                    "transmit buffer!\n", __FUNCTION__);
 
 175                 memset(self->tx_buff.head, 0, self->tx_buff.truesize);
 
 177         self->tx_buff.data = self->tx_buff.head;
 
 180         /* Keep track of module usage */
 
 181         SET_MODULE_OWNER(dev);
 
 183         /* May be overridden by piggyback drivers */
 
 184         self->interrupt    = irport_interrupt;
 
 185         self->change_speed = irport_change_speed;
 
 187         /* Override the network functions we need to use */
 
 188         dev->hard_start_xmit = irport_hard_xmit;
 
 189         dev->tx_timeout      = irport_timeout;
 
 190         dev->watchdog_timeo  = HZ;  /* Allow time enough for speed change */
 
 191         dev->open            = irport_net_open;
 
 192         dev->stop            = irport_net_close;
 
 193         dev->get_stats       = irport_net_get_stats;
 
 194         dev->do_ioctl        = irport_net_ioctl;
 
 196         /* Make ifconfig display some details */
 
 197         dev->base_addr = iobase;
 
 200         if (register_netdev(dev)) {
 
 201                 IRDA_ERROR("%s(), register_netdev() failed!\n", __FUNCTION__);
 
 204         IRDA_MESSAGE("IrDA: Registered device %s (irport io=0x%X irq=%d)\n",
 
 205                 dev->name, iobase, irq);
 
 209         kfree(self->tx_buff.head);
 
 211         kfree_skb(self->rx_buff.skb);
 
 216         release_region(iobase, IO_EXTENT);
 
 221 static int irport_close(struct irport_cb *self)
 
 223         IRDA_ASSERT(self != NULL, return -1;);
 
 225         /* We are not using any dongle anymore! */
 
 227                 irda_device_dongle_cleanup(self->dongle);
 
 230         /* Remove netdevice */
 
 231         unregister_netdev(self->netdev);
 
 233         /* Release the IO-port that this driver is using */
 
 234         IRDA_DEBUG(0 , "%s(), Releasing Region %03x\n", 
 
 235                    __FUNCTION__, self->io.sir_base);
 
 236         release_region(self->io.sir_base, self->io.sir_ext);
 
 238         kfree(self->tx_buff.head);
 
 240         if (self->rx_buff.skb)
 
 241                 kfree_skb(self->rx_buff.skb);
 
 242         self->rx_buff.skb = NULL;
 
 244         /* Remove ourselves */
 
 245         dev_self[self->index] = NULL;
 
 246         free_netdev(self->netdev);
 
 251 static void irport_stop(struct irport_cb *self)
 
 255         iobase = self->io.sir_base;
 
 257         /* We can't lock, we may be called from a FIR driver - Jean II */
 
 259         /* We are not transmitting any more */
 
 260         self->transmitting = 0;
 
 263         outb(0, iobase+UART_MCR);
 
 265         /* Turn off interrupts */
 
 266         outb(0, iobase+UART_IER);
 
 269 static void irport_start(struct irport_cb *self)
 
 273         iobase = self->io.sir_base;
 
 277         /* We can't lock, we may be called from a FIR driver - Jean II */
 
 279         /* Initialize UART */
 
 280         outb(UART_LCR_WLEN8, iobase+UART_LCR);  /* Reset DLAB */
 
 281         outb((UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2), iobase+UART_MCR);
 
 283         /* Turn on interrups */
 
 284         outb(UART_IER_RLSI | UART_IER_RDI |UART_IER_THRI, iobase+UART_IER);
 
 288  * Function irport_get_fcr (speed)
 
 290  *    Compute value of fcr
 
 293 static inline unsigned int irport_get_fcr(__u32 speed)
 
 295         unsigned int fcr;    /* FIFO control reg */
 
 298         fcr = UART_FCR_ENABLE_FIFO;
 
 301          * Use trigger level 1 to avoid 3 ms. timeout delay at 9600 bps, and
 
 302          * almost 1,7 ms at 19200 bps. At speeds above that we can just forget
 
 303          * about this timeout since it will always be fast enough. 
 
 306                 fcr |= UART_FCR_TRIGGER_1;
 
 308                 //fcr |= UART_FCR_TRIGGER_14;
 
 309                 fcr |= UART_FCR_TRIGGER_8;
 
 315  * Function irport_change_speed (self, speed)
 
 317  *    Set speed of IrDA port to specified baudrate
 
 319  * This function should be called with irq off and spin-lock.
 
 321 static void irport_change_speed(void *priv, __u32 speed)
 
 323         struct irport_cb *self = (struct irport_cb *) priv;
 
 325         unsigned int fcr;    /* FIFO control reg */
 
 326         unsigned int lcr;    /* Line control reg */
 
 329         IRDA_ASSERT(self != NULL, return;);
 
 330         IRDA_ASSERT(speed != 0, return;);
 
 332         IRDA_DEBUG(1, "%s(), Setting speed to: %d - iobase=%#x\n",
 
 333                     __FUNCTION__, speed, self->io.sir_base);
 
 335         /* We can't lock, we may be called from a FIR driver - Jean II */
 
 337         iobase = self->io.sir_base;
 
 339         /* Update accounting for new speed */
 
 340         self->io.speed = speed;
 
 342         /* Turn off interrupts */
 
 343         outb(0, iobase+UART_IER); 
 
 345         divisor = SPEED_MAX/speed;
 
 347         /* Get proper fifo configuration */
 
 348         fcr = irport_get_fcr(speed);
 
 350         /* IrDA ports use 8N1 */
 
 351         lcr = UART_LCR_WLEN8;
 
 353         outb(UART_LCR_DLAB | lcr, iobase+UART_LCR); /* Set DLAB */
 
 354         outb(divisor & 0xff,      iobase+UART_DLL); /* Set speed */
 
 355         outb(divisor >> 8,        iobase+UART_DLM);
 
 356         outb(lcr,                 iobase+UART_LCR); /* Set 8N1  */
 
 357         outb(fcr,                 iobase+UART_FCR); /* Enable FIFO's */
 
 359         /* Turn on interrups */
 
 360         /* This will generate a fatal interrupt storm.
 
 361          * People calling us will do that properly - Jean II */
 
 362         //outb(/*UART_IER_RLSI|*/UART_IER_RDI/*|UART_IER_THRI*/, iobase+UART_IER);
 
 366  * Function __irport_change_speed (instance, state, param)
 
 368  *    State machine for changing speed of the device. We do it this way since
 
 369  *    we cannot use schedule_timeout() when we are in interrupt context
 
 372 static int __irport_change_speed(struct irda_task *task)
 
 374         struct irport_cb *self;
 
 375         __u32 speed = (__u32) task->param;
 
 376         unsigned long flags = 0;
 
 380         IRDA_DEBUG(2, "%s(), <%ld>\n", __FUNCTION__, jiffies); 
 
 382         self = (struct irport_cb *) task->instance;
 
 384         IRDA_ASSERT(self != NULL, return -1;);
 
 386         /* Locking notes : this function may be called from irq context with
 
 387          * spinlock, via irport_write_wakeup(), or from non-interrupt without
 
 388          * spinlock (from the task timer). Yuck !
 
 389          * This is ugly, and unsafe is the spinlock is not already aquired.
 
 390          * This will be fixed when irda-task get rewritten.
 
 392         if (!spin_is_locked(&self->lock)) {
 
 393                 spin_lock_irqsave(&self->lock, flags);
 
 397         switch (task->state) {
 
 400                 /* Are we ready to change speed yet? */
 
 401                 if (self->tx_buff.len > 0) {
 
 402                         task->state = IRDA_TASK_WAIT;
 
 404                         /* Try again later */
 
 405                         ret = msecs_to_jiffies(20);
 
 410                         irda_task_next_state(task, IRDA_TASK_CHILD_INIT);
 
 412                         irda_task_next_state(task, IRDA_TASK_CHILD_DONE);
 
 414         case IRDA_TASK_CHILD_INIT:
 
 415                 /* Go to default speed */
 
 416                 self->change_speed(self->priv, 9600);
 
 418                 /* Change speed of dongle */
 
 419                 if (irda_task_execute(self->dongle,
 
 420                                       self->dongle->issue->change_speed, 
 
 421                                       NULL, task, (void *) speed))
 
 423                         /* Dongle need more time to change its speed */
 
 424                         irda_task_next_state(task, IRDA_TASK_CHILD_WAIT);
 
 426                         /* Give dongle 1 sec to finish */
 
 427                         ret = msecs_to_jiffies(1000);
 
 429                         /* Child finished immediately */
 
 430                         irda_task_next_state(task, IRDA_TASK_CHILD_DONE);
 
 432         case IRDA_TASK_CHILD_WAIT:
 
 433                 IRDA_WARNING("%s(), changing speed of dongle timed out!\n", __FUNCTION__);
 
 436         case IRDA_TASK_CHILD_DONE:
 
 437                 /* Finally we are ready to change the speed */
 
 438                 self->change_speed(self->priv, speed);
 
 440                 irda_task_next_state(task, IRDA_TASK_DONE);
 
 443                 IRDA_ERROR("%s(), unknown state %d\n",
 
 444                            __FUNCTION__, task->state);
 
 445                 irda_task_next_state(task, IRDA_TASK_DONE);
 
 449         /* Put stuff in the state we found them - Jean II */
 
 451                 spin_unlock_irqrestore(&self->lock, flags);
 
 458  * Function irport_change_speed_complete (task)
 
 460  *    Called when the change speed operation completes
 
 463 static int irport_change_speed_complete(struct irda_task *task)
 
 465         struct irport_cb *self;
 
 467         IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
 
 469         self = (struct irport_cb *) task->instance;
 
 471         IRDA_ASSERT(self != NULL, return -1;);
 
 472         IRDA_ASSERT(self->netdev != NULL, return -1;);
 
 474         /* Finished changing speed, so we are not busy any longer */
 
 475         /* Signal network layer so it can try to send the frame */
 
 477         netif_wake_queue(self->netdev);
 
 483  * Function irport_timeout (struct net_device *dev)
 
 485  *    The networking layer thinks we timed out.
 
 489 static void irport_timeout(struct net_device *dev)
 
 491         struct irport_cb *self;
 
 496         self = (struct irport_cb *) dev->priv;
 
 497         IRDA_ASSERT(self != NULL, return;);
 
 498         iobase = self->io.sir_base;
 
 500         IRDA_WARNING("%s: transmit timed out, jiffies = %ld, trans_start = %ld\n",
 
 501                 dev->name, jiffies, dev->trans_start);
 
 502         spin_lock_irqsave(&self->lock, flags);
 
 504         /* Debug what's happening... */
 
 506         /* Get interrupt status */
 
 507         lsr = inb(iobase+UART_LSR);
 
 508         /* Read interrupt register */
 
 509         iir = inb(iobase+UART_IIR);
 
 510         IRDA_DEBUG(0, "%s(), iir=%02x, lsr=%02x, iobase=%#x\n", 
 
 511                    __FUNCTION__, iir, lsr, iobase);
 
 513         IRDA_DEBUG(0, "%s(), transmitting=%d, remain=%d, done=%d\n", 
 
 514                    __FUNCTION__, self->transmitting, self->tx_buff.len,
 
 515                    self->tx_buff.data - self->tx_buff.head);
 
 517         /* Now, restart the port */
 
 519         self->change_speed(self->priv, self->io.speed);
 
 520         /* This will re-enable irqs */
 
 521         outb(/*UART_IER_RLSI|*/UART_IER_RDI/*|UART_IER_THRI*/, iobase+UART_IER);
 
 522         dev->trans_start = jiffies;
 
 523         spin_unlock_irqrestore(&self->lock, flags);
 
 525         netif_wake_queue(dev);
 
 529  * Function irport_wait_hw_transmitter_finish ()
 
 531  *    Wait for the real end of HW transmission
 
 533  * The UART is a strict FIFO, and we get called only when we have finished
 
 534  * pushing data to the FIFO, so the maximum amount of time we must wait
 
 535  * is only for the FIFO to drain out.
 
 537  * We use a simple calibrated loop. We may need to adjust the loop
 
 538  * delay (udelay) to balance I/O traffic and latency. And we also need to
 
 539  * adjust the maximum timeout.
 
 540  * It would probably be better to wait for the proper interrupt,
 
 541  * but it doesn't seem to be available.
 
 543  * We can't use jiffies or kernel timers because :
 
 544  * 1) We are called from the interrupt handler, which disable softirqs,
 
 545  * so jiffies won't be increased
 
 546  * 2) Jiffies granularity is usually very coarse (10ms), and we don't
 
 547  * want to wait that long to detect stuck hardware.
 
 551 static void irport_wait_hw_transmitter_finish(struct irport_cb *self)
 
 554         int count = 1000;       /* 1 ms */
 
 556         iobase = self->io.sir_base;
 
 558         /* Calibrated busy loop */
 
 559         while((count-- > 0) && !(inb(iobase+UART_LSR) & UART_LSR_TEMT))
 
 563                 IRDA_DEBUG(0, "%s(): stuck transmitter\n", __FUNCTION__);
 
 567  * Function irport_hard_start_xmit (struct sk_buff *skb, struct net_device *dev)
 
 569  *    Transmits the current frame until FIFO is full, then
 
 570  *    waits until the next transmitt interrupt, and continues until the
 
 571  *    frame is transmitted.
 
 573 static int irport_hard_xmit(struct sk_buff *skb, struct net_device *dev)
 
 575         struct irport_cb *self;
 
 580         IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
 
 582         IRDA_ASSERT(dev != NULL, return 0;);
 
 584         self = (struct irport_cb *) dev->priv;
 
 585         IRDA_ASSERT(self != NULL, return 0;);
 
 587         iobase = self->io.sir_base;
 
 589         netif_stop_queue(dev);
 
 591         /* Make sure tests & speed change are atomic */
 
 592         spin_lock_irqsave(&self->lock, flags);
 
 594         /* Check if we need to change the speed */
 
 595         speed = irda_get_next_speed(skb);
 
 596         if ((speed != self->io.speed) && (speed != -1)) {
 
 597                 /* Check for empty frame */
 
 600                          * We send frames one by one in SIR mode (no
 
 601                          * pipelining), so at this point, if we were sending
 
 602                          * a previous frame, we just received the interrupt
 
 603                          * telling us it is finished (UART_IIR_THRI).
 
 604                          * Therefore, waiting for the transmitter to really
 
 605                          * finish draining the fifo won't take too long.
 
 606                          * And the interrupt handler is not expected to run.
 
 608                         irport_wait_hw_transmitter_finish(self);
 
 609                         /* Better go there already locked - Jean II */
 
 610                         irda_task_execute(self, __irport_change_speed, 
 
 611                                           irport_change_speed_complete, 
 
 612                                           NULL, (void *) speed);
 
 613                         dev->trans_start = jiffies;
 
 614                         spin_unlock_irqrestore(&self->lock, flags);
 
 618                         self->new_speed = speed;
 
 622         self->tx_buff.data = self->tx_buff.head;
 
 624         /* Copy skb to tx_buff while wrapping, stuffing and making CRC */
 
 625         self->tx_buff.len = async_wrap_skb(skb, self->tx_buff.data, 
 
 626                                            self->tx_buff.truesize);
 
 628         self->stats.tx_bytes += self->tx_buff.len;
 
 630         /* We are transmitting */
 
 631         self->transmitting = 1;
 
 633         /* Turn on transmit finished interrupt. Will fire immediately!  */
 
 634         outb(UART_IER_THRI, iobase+UART_IER); 
 
 636         dev->trans_start = jiffies;
 
 637         spin_unlock_irqrestore(&self->lock, flags);
 
 645  * Function irport_write (driver)
 
 647  *    Fill Tx FIFO with transmit data
 
 649  * Called only from irport_write_wakeup()
 
 651 static inline int irport_write(int iobase, int fifo_size, __u8 *buf, int len)
 
 655         /* Fill FIFO with current frame */
 
 656         while ((actual < fifo_size) && (actual < len)) {
 
 657                 /* Transmit next byte */
 
 658                 outb(buf[actual], iobase+UART_TX);
 
 667  * Function irport_write_wakeup (tty)
 
 669  *    Called by the driver when there's room for more data.  If we have
 
 670  *    more packets to send, we send them here.
 
 672  * Called only from irport_interrupt()
 
 673  * Make sure this function is *not* called while we are receiving,
 
 674  * otherwise we will reset fifo and loose data :-(
 
 676 static inline void irport_write_wakeup(struct irport_cb *self)
 
 682         IRDA_ASSERT(self != NULL, return;);
 
 684         IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
 
 686         iobase = self->io.sir_base;
 
 688         /* Finished with frame?  */
 
 689         if (self->tx_buff.len > 0)  {
 
 690                 /* Write data left in transmit buffer */
 
 691                 actual = irport_write(iobase, self->io.fifo_size, 
 
 692                                       self->tx_buff.data, self->tx_buff.len);
 
 693                 self->tx_buff.data += actual;
 
 694                 self->tx_buff.len  -= actual;
 
 697                  *  Now serial buffer is almost free & we can start 
 
 698                  *  transmission of another packet. But first we must check
 
 699                  *  if we need to change the speed of the hardware
 
 701                 if (self->new_speed) {
 
 702                         irport_wait_hw_transmitter_finish(self);
 
 703                         irda_task_execute(self, __irport_change_speed, 
 
 704                                           irport_change_speed_complete, 
 
 705                                           NULL, (void *) self->new_speed);
 
 708                         /* Tell network layer that we want more frames */
 
 709                         netif_wake_queue(self->netdev);
 
 711                 self->stats.tx_packets++;
 
 714                  * Reset Rx FIFO to make sure that all reflected transmit data
 
 715                  * is discarded. This is needed for half duplex operation
 
 717                 fcr = irport_get_fcr(self->io.speed);
 
 718                 fcr |= UART_FCR_CLEAR_RCVR;
 
 719                 outb(fcr, iobase+UART_FCR);
 
 721                 /* Finished transmitting */
 
 722                 self->transmitting = 0;
 
 724                 /* Turn on receive interrupts */
 
 725                 outb(UART_IER_RDI, iobase+UART_IER);
 
 727                 IRDA_DEBUG(1, "%s() : finished Tx\n", __FUNCTION__);
 
 732  * Function irport_receive (self)
 
 734  *    Receive one frame from the infrared port
 
 736  * Called only from irport_interrupt()
 
 738 static inline void irport_receive(struct irport_cb *self) 
 
 743         IRDA_ASSERT(self != NULL, return;);
 
 745         iobase = self->io.sir_base;
 
 748          * Receive all characters in Rx FIFO, unwrap and unstuff them. 
 
 749          * async_unwrap_char will deliver all found frames  
 
 752                 async_unwrap_char(self->netdev, &self->stats, &self->rx_buff, 
 
 753                                   inb(iobase+UART_RX));
 
 755                 /* Make sure we don't stay here too long */
 
 756                 if (boguscount++ > 32) {
 
 757                         IRDA_DEBUG(2,"%s(), breaking!\n", __FUNCTION__);
 
 760         } while (inb(iobase+UART_LSR) & UART_LSR_DR);   
 
 764  * Function irport_interrupt (irq, dev_id, regs)
 
 768 static irqreturn_t irport_interrupt(int irq, void *dev_id,
 
 769                                     struct pt_regs *regs) 
 
 771         struct net_device *dev = (struct net_device *) dev_id;
 
 772         struct irport_cb *self;
 
 779                 IRDA_WARNING("%s() irq %d for unknown device.\n", __FUNCTION__, irq);
 
 782         self = (struct irport_cb *) dev->priv;
 
 784         spin_lock(&self->lock);
 
 786         iobase = self->io.sir_base;
 
 788         /* Cut'n'paste interrupt routine from serial.c
 
 789          * This version try to minimise latency and I/O operations.
 
 790          * Simplified and modified to enforce half duplex operation.
 
 793         /* Check status even is iir reg is cleared, more robust and
 
 794          * eliminate a read on the I/O bus - Jean II */
 
 796                 /* Get interrupt status ; Clear interrupt */
 
 797                 lsr = inb(iobase+UART_LSR);
 
 799                 /* Are we receiving or transmitting ? */
 
 800                 if(!self->transmitting) {
 
 801                         /* Received something ? */
 
 802                         if (lsr & UART_LSR_DR)
 
 803                                 irport_receive(self);
 
 805                         /* Room in Tx fifo ? */
 
 806                         if (lsr & (UART_LSR_THRE | UART_LSR_TEMT))
 
 807                                 irport_write_wakeup(self);
 
 810                 /* A bit hackish, but working as expected... Jean II */
 
 811                 if(lsr & (UART_LSR_THRE | UART_LSR_TEMT | UART_LSR_DR))
 
 814                 /* Make sure we don't stay here to long */
 
 815                 if (boguscount++ > 10) {
 
 816                         IRDA_WARNING("%s() irq handler looping : lsr=%02x\n",
 
 821                 /* Read interrupt register */
 
 822                 iir = inb(iobase+UART_IIR);
 
 824                 /* Enable this debug only when no other options and at low
 
 825                  * bit rates, otherwise it may cause Rx overruns (lsr=63).
 
 827                 IRDA_DEBUG(6, "%s(), iir=%02x, lsr=%02x, iobase=%#x\n", 
 
 828                             __FUNCTION__, iir, lsr, iobase);
 
 830                 /* As long as interrupt pending... */
 
 831         } while ((iir & UART_IIR_NO_INT) == 0);
 
 833         spin_unlock(&self->lock);
 
 834         return IRQ_RETVAL(handled);
 
 838  * Function irport_net_open (dev)
 
 840  *    Network device is taken up. Usually this is done by "ifconfig irda0 up" 
 
 843 static int irport_net_open(struct net_device *dev)
 
 845         struct irport_cb *self;
 
 850         IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
 
 852         IRDA_ASSERT(dev != NULL, return -1;);
 
 853         self = (struct irport_cb *) dev->priv;
 
 855         iobase = self->io.sir_base;
 
 857         if (request_irq(self->io.irq, self->interrupt, 0, dev->name, 
 
 859                 IRDA_DEBUG(0, "%s(), unable to allocate irq=%d\n",
 
 860                            __FUNCTION__, self->io.irq);
 
 864         spin_lock_irqsave(&self->lock, flags);
 
 867         /* Set 9600 bauds per default, including at the dongle */
 
 868         irda_task_execute(self, __irport_change_speed, 
 
 869                           irport_change_speed_complete, 
 
 870                           NULL, (void *) 9600);
 
 871         spin_unlock_irqrestore(&self->lock, flags);
 
 874         /* Give self a hardware name */
 
 875         sprintf(hwname, "SIR @ 0x%03x", self->io.sir_base);
 
 878          * Open new IrLAP layer instance, now that everything should be
 
 879          * initialized properly 
 
 881         self->irlap = irlap_open(dev, &self->qos, hwname);
 
 885         netif_start_queue(dev);
 
 891  * Function irport_net_close (self)
 
 893  *    Network device is taken down. Usually this is done by 
 
 894  *    "ifconfig irda0 down" 
 
 896 static int irport_net_close(struct net_device *dev)
 
 898         struct irport_cb *self;
 
 902         IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
 
 904         IRDA_ASSERT(dev != NULL, return -1;);
 
 905         self = (struct irport_cb *) dev->priv;
 
 907         IRDA_ASSERT(self != NULL, return -1;);
 
 909         iobase = self->io.sir_base;
 
 912         netif_stop_queue(dev);
 
 914         /* Stop and remove instance of IrLAP */
 
 916                 irlap_close(self->irlap);
 
 919         spin_lock_irqsave(&self->lock, flags);
 
 921         spin_unlock_irqrestore(&self->lock, flags);
 
 923         free_irq(self->io.irq, dev);
 
 929  * Function irport_is_receiving (self)
 
 931  *    Returns true is we are currently receiving data
 
 934 static inline int irport_is_receiving(struct irport_cb *self)
 
 936         return (self->rx_buff.state != OUTSIDE_FRAME);
 
 940  * Function irport_set_dtr_rts (tty, dtr, rts)
 
 942  *    This function can be used by dongles etc. to set or reset the status
 
 943  *    of the dtr and rts lines
 
 945 static int irport_set_dtr_rts(struct net_device *dev, int dtr, int rts)
 
 947         struct irport_cb *self = dev->priv;
 
 950         IRDA_ASSERT(self != NULL, return -1;);
 
 952         iobase = self->io.sir_base;
 
 959         outb(dtr|rts|UART_MCR_OUT2, iobase+UART_MCR);
 
 964 static int irport_raw_write(struct net_device *dev, __u8 *buf, int len)
 
 966         struct irport_cb *self = (struct irport_cb *) dev->priv;
 
 970         IRDA_ASSERT(self != NULL, return -1;);
 
 972         iobase = self->io.sir_base;
 
 974         /* Tx FIFO should be empty! */
 
 975         if (!(inb(iobase+UART_LSR) & UART_LSR_THRE)) {
 
 976                 IRDA_DEBUG( 0, "%s(), failed, fifo not empty!\n", __FUNCTION__);
 
 980         /* Fill FIFO with current frame */
 
 981         while (actual < len) {
 
 982                 /* Transmit next byte */
 
 983                 outb(buf[actual], iobase+UART_TX);
 
 991  * Function irport_net_ioctl (dev, rq, cmd)
 
 993  *    Process IOCTL commands for this device
 
 996 static int irport_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
 
 998         struct if_irda_req *irq = (struct if_irda_req *) rq;
 
 999         struct irport_cb *self;
 
1001         unsigned long flags;
 
1004         IRDA_ASSERT(dev != NULL, return -1;);
 
1008         IRDA_ASSERT(self != NULL, return -1;);
 
1010         IRDA_DEBUG(2, "%s(), %s, (cmd=0x%X)\n", __FUNCTION__, dev->name, cmd);
 
1013         case SIOCSBANDWIDTH: /* Set bandwidth */
 
1014                 if (!capable(CAP_NET_ADMIN))
 
1017                         irda_task_execute(self, __irport_change_speed, NULL, 
 
1018                                           NULL, (void *) irq->ifr_baudrate);
 
1020         case SIOCSDONGLE: /* Set dongle */
 
1021                 if (!capable(CAP_NET_ADMIN)) {
 
1027                  * irda_device_dongle_init() can't be locked.
 
1028                  * irda_task_execute() doesn't need to be locked.
 
1032                 /* Initialize dongle */
 
1033                 dongle = irda_device_dongle_init(dev, irq->ifr_dongle);
 
1037                 dongle->set_mode    = NULL;
 
1038                 dongle->read        = NULL;
 
1039                 dongle->write       = irport_raw_write;
 
1040                 dongle->set_dtr_rts = irport_set_dtr_rts;
 
1042                 /* Now initialize the dongle!  */
 
1043                 dongle->issue->open(dongle, &self->qos);
 
1046                 irda_task_execute(dongle, dongle->issue->reset, NULL, NULL, 
 
1049                 /* Make dongle available to driver only now to avoid
 
1050                  * race conditions - Jean II */
 
1051                 self->dongle = dongle;
 
1053         case SIOCSMEDIABUSY: /* Set media busy */
 
1054                 if (!capable(CAP_NET_ADMIN)) {
 
1059                 irda_device_set_media_busy(self->netdev, TRUE);
 
1061         case SIOCGRECEIVING: /* Check if we are receiving right now */
 
1062                 irq->ifr_receiving = irport_is_receiving(self);
 
1065                 if (!capable(CAP_NET_ADMIN)) {
 
1070                 /* No real need to lock... */
 
1071                 spin_lock_irqsave(&self->lock, flags);
 
1072                 irport_set_dtr_rts(dev, irq->ifr_dtr, irq->ifr_rts);
 
1073                 spin_unlock_irqrestore(&self->lock, flags);
 
1082 static struct net_device_stats *irport_net_get_stats(struct net_device *dev)
 
1084         struct irport_cb *self = (struct irport_cb *) dev->priv;
 
1086         return &self->stats;
 
1089 static int __init irport_init(void)
 
1093         for (i=0; (io[i] < 2000) && (i < 4); i++) {
 
1094                 if (irport_open(i, io[i], irq[i]) != NULL)
 
1098          * Maybe something failed, but we can still be usable for FIR drivers 
 
1104  * Function irport_cleanup ()
 
1106  *    Close all configured ports
 
1109 static void __exit irport_cleanup(void)
 
1113         IRDA_DEBUG( 4, "%s()\n", __FUNCTION__);
 
1115         for (i=0; i < 4; i++) {
 
1117                         irport_close(dev_self[i]);
 
1121 module_param_array(io, int, NULL, 0);
 
1122 MODULE_PARM_DESC(io, "Base I/O addresses");
 
1123 module_param_array(irq, int, NULL, 0);
 
1124 MODULE_PARM_DESC(irq, "IRQ lines");
 
1126 MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
 
1127 MODULE_DESCRIPTION("Half duplex serial driver for IrDA SIR mode");
 
1128 MODULE_LICENSE("GPL");
 
1130 module_init(irport_init);
 
1131 module_exit(irport_cleanup);