📄 irport.c
字号:
if(count == 0) IRDA_DEBUG(0, "%s(): stuck transmitter\n", __FUNCTION__);}/* * Function irport_hard_start_xmit (struct sk_buff *skb, struct net_device *dev) * * Transmits the current frame until FIFO is full, then * waits until the next transmitt interrupt, and continues until the * frame is transmitted. */static int irport_hard_xmit(struct sk_buff *skb, struct net_device *dev){ struct irport_cb *self; unsigned long flags; int iobase; s32 speed; IRDA_DEBUG(1, "%s()\n", __FUNCTION__); IRDA_ASSERT(dev != NULL, return 0;); self = (struct irport_cb *) dev->priv; IRDA_ASSERT(self != NULL, return 0;); iobase = self->io.sir_base; netif_stop_queue(dev); /* Make sure tests & speed change are atomic */ spin_lock_irqsave(&self->lock, flags); /* Check if we need to change the speed */ speed = irda_get_next_speed(skb); if ((speed != self->io.speed) && (speed != -1)) { /* Check for empty frame */ if (!skb->len) { /* * We send frames one by one in SIR mode (no * pipelining), so at this point, if we were sending * a previous frame, we just received the interrupt * telling us it is finished (UART_IIR_THRI). * Therefore, waiting for the transmitter to really * finish draining the fifo won't take too long. * And the interrupt handler is not expected to run. * - Jean II */ irport_wait_hw_transmitter_finish(self); /* Better go there already locked - Jean II */ irda_task_execute(self, __irport_change_speed, irport_change_speed_complete, NULL, (void *) speed); dev->trans_start = jiffies; spin_unlock_irqrestore(&self->lock, flags); dev_kfree_skb(skb); return 0; } else self->new_speed = speed; } /* Init tx buffer */ self->tx_buff.data = self->tx_buff.head; /* Copy skb to tx_buff while wrapping, stuffing and making CRC */ self->tx_buff.len = async_wrap_skb(skb, self->tx_buff.data, self->tx_buff.truesize); self->stats.tx_bytes += self->tx_buff.len; /* We are transmitting */ self->transmitting = 1; /* Turn on transmit finished interrupt. Will fire immediately! */ outb(UART_IER_THRI, iobase+UART_IER); dev->trans_start = jiffies; spin_unlock_irqrestore(&self->lock, flags); dev_kfree_skb(skb); return 0;} /* * Function irport_write (driver) * * Fill Tx FIFO with transmit data * * Called only from irport_write_wakeup() */static inline int irport_write(int iobase, int fifo_size, __u8 *buf, int len){ int actual = 0; /* Fill FIFO with current frame */ while ((actual < fifo_size) && (actual < len)) { /* Transmit next byte */ outb(buf[actual], iobase+UART_TX); actual++; } return actual;}/* * Function irport_write_wakeup (tty) * * Called by the driver when there's room for more data. If we have * more packets to send, we send them here. * * Called only from irport_interrupt() * Make sure this function is *not* called while we are receiving, * otherwise we will reset fifo and loose data :-( */static inline void irport_write_wakeup(struct irport_cb *self){ int actual = 0; int iobase; unsigned int fcr; IRDA_ASSERT(self != NULL, return;); IRDA_DEBUG(4, "%s()\n", __FUNCTION__); iobase = self->io.sir_base; /* Finished with frame? */ if (self->tx_buff.len > 0) { /* Write data left in transmit buffer */ actual = irport_write(iobase, self->io.fifo_size, self->tx_buff.data, self->tx_buff.len); self->tx_buff.data += actual; self->tx_buff.len -= actual; } else { /* * Now serial buffer is almost free & we can start * transmission of another packet. But first we must check * if we need to change the speed of the hardware */ if (self->new_speed) { irport_wait_hw_transmitter_finish(self); irda_task_execute(self, __irport_change_speed, irport_change_speed_complete, NULL, (void *) self->new_speed); self->new_speed = 0; } else { /* Tell network layer that we want more frames */ netif_wake_queue(self->netdev); } self->stats.tx_packets++; /* * Reset Rx FIFO to make sure that all reflected transmit data * is discarded. This is needed for half duplex operation */ fcr = irport_get_fcr(self->io.speed); fcr |= UART_FCR_CLEAR_RCVR; outb(fcr, iobase+UART_FCR); /* Finished transmitting */ self->transmitting = 0; /* Turn on receive interrupts */ outb(UART_IER_RDI, iobase+UART_IER); IRDA_DEBUG(1, "%s() : finished Tx\n", __FUNCTION__); }}/* * Function irport_receive (self) * * Receive one frame from the infrared port * * Called only from irport_interrupt() */static inline void irport_receive(struct irport_cb *self) { int boguscount = 0; int iobase; IRDA_ASSERT(self != NULL, return;); iobase = self->io.sir_base; /* * Receive all characters in Rx FIFO, unwrap and unstuff them. * async_unwrap_char will deliver all found frames */ do { async_unwrap_char(self->netdev, &self->stats, &self->rx_buff, inb(iobase+UART_RX)); /* Make sure we don't stay here too long */ if (boguscount++ > 32) { IRDA_DEBUG(2,"%s(), breaking!\n", __FUNCTION__); break; } } while (inb(iobase+UART_LSR) & UART_LSR_DR); }/* * Function irport_interrupt (irq, dev_id, regs) * * Interrupt handler */static irqreturn_t irport_interrupt(int irq, void *dev_id, struct pt_regs *regs) { struct net_device *dev = (struct net_device *) dev_id; struct irport_cb *self; int boguscount = 0; int iobase; int iir, lsr; int handled = 0; if (!dev) { IRDA_WARNING("%s() irq %d for unknown device.\n", __FUNCTION__, irq); return IRQ_NONE; } self = (struct irport_cb *) dev->priv; spin_lock(&self->lock); iobase = self->io.sir_base; /* Cut'n'paste interrupt routine from serial.c * This version try to minimise latency and I/O operations. * Simplified and modified to enforce half duplex operation. * - Jean II */ /* Check status even is iir reg is cleared, more robust and * eliminate a read on the I/O bus - Jean II */ do { /* Get interrupt status ; Clear interrupt */ lsr = inb(iobase+UART_LSR); /* Are we receiving or transmitting ? */ if(!self->transmitting) { /* Received something ? */ if (lsr & UART_LSR_DR) irport_receive(self); } else { /* Room in Tx fifo ? */ if (lsr & (UART_LSR_THRE | UART_LSR_TEMT)) irport_write_wakeup(self); } /* A bit hackish, but working as expected... Jean II */ if(lsr & (UART_LSR_THRE | UART_LSR_TEMT | UART_LSR_DR)) handled = 1; /* Make sure we don't stay here to long */ if (boguscount++ > 10) { IRDA_WARNING("%s() irq handler looping : lsr=%02x\n", __FUNCTION__, lsr); break; } /* Read interrupt register */ iir = inb(iobase+UART_IIR); /* Enable this debug only when no other options and at low * bit rates, otherwise it may cause Rx overruns (lsr=63). * - Jean II */ IRDA_DEBUG(6, "%s(), iir=%02x, lsr=%02x, iobase=%#x\n", __FUNCTION__, iir, lsr, iobase); /* As long as interrupt pending... */ } while ((iir & UART_IIR_NO_INT) == 0); spin_unlock(&self->lock); return IRQ_RETVAL(handled);}/* * Function irport_net_open (dev) * * Network device is taken up. Usually this is done by "ifconfig irda0 up" * */static int irport_net_open(struct net_device *dev){ struct irport_cb *self; int iobase; char hwname[16]; unsigned long flags; IRDA_DEBUG(2, "%s()\n", __FUNCTION__); IRDA_ASSERT(dev != NULL, return -1;); self = (struct irport_cb *) dev->priv; iobase = self->io.sir_base; if (request_irq(self->io.irq, self->interrupt, 0, dev->name, (void *) dev)) { IRDA_DEBUG(0, "%s(), unable to allocate irq=%d\n", __FUNCTION__, self->io.irq); return -EAGAIN; } spin_lock_irqsave(&self->lock, flags); /* Init uart */ irport_start(self); /* Set 9600 bauds per default, including at the dongle */ irda_task_execute(self, __irport_change_speed, irport_change_speed_complete, NULL, (void *) 9600); spin_unlock_irqrestore(&self->lock, flags); /* Give self a hardware name */ sprintf(hwname, "SIR @ 0x%03x", self->io.sir_base); /* * Open new IrLAP layer instance, now that everything should be * initialized properly */ self->irlap = irlap_open(dev, &self->qos, hwname); /* Ready to play! */ netif_start_queue(dev); return 0;}/* * Function irport_net_close (self) * * Network device is taken down. Usually this is done by * "ifconfig irda0 down" */static int irport_net_close(struct net_device *dev){ struct irport_cb *self; int iobase; unsigned long flags; IRDA_DEBUG(4, "%s()\n", __FUNCTION__); IRDA_ASSERT(dev != NULL, return -1;); self = (struct irport_cb *) dev->priv; IRDA_ASSERT(self != NULL, return -1;); iobase = self->io.sir_base; /* Stop device */ netif_stop_queue(dev); /* Stop and remove instance of IrLAP */ if (self->irlap) irlap_close(self->irlap); self->irlap = NULL; spin_lock_irqsave(&self->lock, flags); irport_stop(self); spin_unlock_irqrestore(&self->lock, flags); free_irq(self->io.irq, dev); return 0;}/* * Function irport_is_receiving (self) * * Returns true is we are currently receiving data * */static inline int irport_is_receiving(struct irport_cb *self){ return (self->rx_buff.state != OUTSIDE_FRAME);}/* * Function irport_set_dtr_rts (tty, dtr, rts) * * This function can be used by dongles etc. to set or reset the status * of the dtr and rts lines */static int irport_set_dtr_rts(struct net_device *dev, int dtr, int rts){ struct irport_cb *self = dev->priv; int iobase; IRDA_ASSERT(self != NULL, return -1;); iobase = self->io.sir_base; if (dtr) dtr = UART_MCR_DTR; if (rts) rts = UART_MCR_RTS; outb(dtr|rts|UART_MCR_OUT2, iobase+UART_MCR); return 0;}static int irport_raw_write(struct net_device *dev, __u8 *buf, int len){ struct irport_cb *self = (struct irport_cb *) dev->priv; int actual = 0; int iobase; IRDA_ASSERT(self != NULL, return -1;); iobase = self->io.sir_base; /* Tx FIFO should be empty! */ if (!(inb(iobase+UART_LSR) & UART_LSR_THRE)) { IRDA_DEBUG( 0, "%s(), failed, fifo not empty!\n", __FUNCTION__); return -1; } /* Fill FIFO with current frame */ while (actual < len) { /* Transmit next byte */ outb(buf[actual], iobase+UART_TX); actual++; } return actual;}/* * Function irport_net_ioctl (dev, rq, cmd) * * Process IOCTL commands for this device * */static int irport_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd){ struct if_irda_req *irq = (struct if_irda_req *) rq; struct irport_cb *self; dongle_t *dongle; unsigned long flags; int ret = 0; IRDA_ASSERT(dev != NULL, return -1;); self = dev->priv; IRDA_ASSERT(self != NULL, return -1;); IRDA_DEBUG(2, "%s(), %s, (cmd=0x%X)\n", __FUNCTION__, dev->name, cmd); switch (cmd) { case SIOCSBANDWIDTH: /* Set bandwidth */ if (!capable(CAP_NET_ADMIN)) ret = -EPERM; else irda_task_execute(self, __irport_change_speed, NULL, NULL, (void *) irq->ifr_baudrate); break; case SIOCSDONGLE: /* Set dongle */ if (!capable(CAP_NET_ADMIN)) { ret = -EPERM; break; } /* Locking : * irda_device_dongle_init() can't be locked. * irda_task_execute() doesn't need to be locked. * Jean II */ /* Initialize dongle */ dongle = irda_device_dongle_init(dev, irq->ifr_dongle); if (!dongle) break; dongle->set_mode = NULL; dongle->read = NULL; dongle->write = irport_raw_write; dongle->set_dtr_rts = irport_set_dtr_rts; /* Now initialize the dongle! */ dongle->issue->open(dongle, &self->qos); /* Reset dongle */ irda_task_execute(dongle, dongle->issue->reset, NULL, NULL, NULL); /* Make dongle available to driver only now to avoid * race conditions - Jean II */ self->dongle = dongle; break; case SIOCSMEDIABUSY: /* Set media busy */ if (!capable(CAP_NET_ADMIN)) { ret = -EPERM; break; } irda_device_set_media_busy(self->netdev, TRUE); break; case SIOCGRECEIVING: /* Check if we are receiving right now */ irq->ifr_receiving = irport_is_receiving(self); break; case SIOCSDTRRTS: if (!capable(CAP_NET_ADMIN)) { ret = -EPERM; break; } /* No real need to lock... */ spin_lock_irqsave(&self->lock, flags); irport_set_dtr_rts(dev, irq->ifr_dtr, irq->ifr_rts); spin_unlock_irqrestore(&self->lock, flags); break; default: ret = -EOPNOTSUPP; } return ret;}static struct net_device_stats *irport_net_get_stats(struct net_device *dev){ struct irport_cb *self = (struct irport_cb *) dev->priv; return &self->stats;}static int __init irport_init(void){ int i; for (i=0; (io[i] < 2000) && (i < 4); i++) { if (irport_open(i, io[i], irq[i]) != NULL) return 0; } /* * Maybe something failed, but we can still be usable for FIR drivers */ return 0;}/* * Function irport_cleanup () * * Close all configured ports * */static void __exit irport_cleanup(void){ int i; IRDA_DEBUG( 4, "%s()\n", __FUNCTION__); for (i=0; i < 4; i++) { if (dev_self[i]) irport_close(dev_self[i]); }}MODULE_PARM(io, "1-4i");MODULE_PARM_DESC(io, "Base I/O addresses");MODULE_PARM(irq, "1-4i");MODULE_PARM_DESC(irq, "IRQ lines");MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");MODULE_DESCRIPTION("Half duplex serial driver for IrDA SIR mode");MODULE_LICENSE("GPL");module_init(irport_init);module_exit(irport_cleanup);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -