📄 esp.c
字号:
* ------------------------------------------------------------ * rs_close() * * This routine is called when the serial port gets closed. First, we * wait for the last remaining data to be sent. Then, we unlink its * async structure from the interrupt chain if necessary, and we free * that IRQ if nothing is left in the chain. * ------------------------------------------------------------ */static void rs_close(struct tty_struct *tty, struct file * filp){ struct esp_struct * info = (struct esp_struct *)tty->driver_data; unsigned long flags; if (!info || serial_paranoia_check(info, tty->name, "rs_close")) return; spin_lock_irqsave(&info->lock, flags); if (tty_hung_up_p(filp)) { DBG_CNT("before DEC-hung"); goto out; } #ifdef SERIAL_DEBUG_OPEN printk("rs_close ttys%d, count = %d\n", info->line, info->count);#endif if ((tty->count == 1) && (info->count != 1)) { /* * Uh, oh. tty->count is 1, which means that the tty * structure will be freed. Info->count should always * be one in these conditions. If it's greater than * one, we've got real problems, since it means the * serial port won't be shutdown. */ printk("rs_close: bad serial port count; tty->count is 1, " "info->count is %d\n", info->count); info->count = 1; } if (--info->count < 0) { printk("rs_close: bad serial port count for ttys%d: %d\n", info->line, info->count); info->count = 0; } if (info->count) { DBG_CNT("before DEC-2"); goto out; } info->flags |= ASYNC_CLOSING; spin_unlock_irqrestore(&info->lock, flags); /* * Now we wait for the transmit buffer to clear; and we notify * the line discipline to only process XON/XOFF characters. */ tty->closing = 1; if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE) tty_wait_until_sent(tty, info->closing_wait); /* * At this point we stop accepting input. To do this, we * disable the receive line status interrupts, and tell the * interrupt driver to stop checking the data ready bit in the * line status register. */ /* info->IER &= ~UART_IER_RLSI; */ info->IER &= ~UART_IER_RDI; info->read_status_mask &= ~UART_LSR_DR; if (info->flags & ASYNC_INITIALIZED) { spin_lock_irqsave(&info->lock, flags); serial_out(info, UART_ESI_CMD1, ESI_SET_SRV_MASK); serial_out(info, UART_ESI_CMD2, info->IER); /* disable receive timeout */ serial_out(info, UART_ESI_CMD1, ESI_SET_RX_TIMEOUT); serial_out(info, UART_ESI_CMD2, 0x00); spin_unlock_irqrestore(&info->lock, flags); /* * Before we drop DTR, make sure the UART transmitter * has completely drained; this is especially * important if there is a transmit FIFO! */ rs_wait_until_sent(tty, info->timeout); } shutdown(info); if (tty->driver->flush_buffer) tty->driver->flush_buffer(tty); tty_ldisc_flush(tty); tty->closing = 0; info->event = 0; info->tty = NULL; if (info->blocked_open) { if (info->close_delay) { msleep_interruptible(jiffies_to_msecs(info->close_delay)); } wake_up_interruptible(&info->open_wait); } info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING); wake_up_interruptible(&info->close_wait); return;out: spin_unlock_irqrestore(&info->lock, flags);}static void rs_wait_until_sent(struct tty_struct *tty, int timeout){ struct esp_struct *info = (struct esp_struct *)tty->driver_data; unsigned long orig_jiffies, char_time; unsigned long flags; if (serial_paranoia_check(info, tty->name, "rs_wait_until_sent")) return; orig_jiffies = jiffies; char_time = ((info->timeout - HZ / 50) / 1024) / 5; if (!char_time) char_time = 1; spin_lock_irqsave(&info->lock, flags); serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND); serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL); while ((serial_in(info, UART_ESI_STAT1) != 0x03) || (serial_in(info, UART_ESI_STAT2) != 0xff)) { spin_unlock_irqrestore(&info->lock, flags); msleep_interruptible(jiffies_to_msecs(char_time)); if (signal_pending(current)) break; if (timeout && time_after(jiffies, orig_jiffies + timeout)) break; spin_lock_irqsave(&info->lock, flags); serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND); serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL); } spin_unlock_irqrestore(&info->lock, flags); set_current_state(TASK_RUNNING);}/* * esp_hangup() --- called by tty_hangup() when a hangup is signaled. */static void esp_hangup(struct tty_struct *tty){ struct esp_struct * info = (struct esp_struct *)tty->driver_data; if (serial_paranoia_check(info, tty->name, "esp_hangup")) return; rs_flush_buffer(tty); shutdown(info); info->event = 0; info->count = 0; info->flags &= ~ASYNC_NORMAL_ACTIVE; info->tty = NULL; wake_up_interruptible(&info->open_wait);}/* * ------------------------------------------------------------ * esp_open() and friends * ------------------------------------------------------------ */static int block_til_ready(struct tty_struct *tty, struct file * filp, struct esp_struct *info){ DECLARE_WAITQUEUE(wait, current); int retval; int do_clocal = 0; unsigned long flags; /* * If the device is in the middle of being closed, then block * until it's done, and then try again. */ if (tty_hung_up_p(filp) || (info->flags & ASYNC_CLOSING)) { if (info->flags & ASYNC_CLOSING) interruptible_sleep_on(&info->close_wait);#ifdef SERIAL_DO_RESTART if (info->flags & ASYNC_HUP_NOTIFY) return -EAGAIN; else return -ERESTARTSYS;#else return -EAGAIN;#endif } /* * If non-blocking mode is set, or the port is not enabled, * then make the check up front and then exit. */ if ((filp->f_flags & O_NONBLOCK) || (tty->flags & (1 << TTY_IO_ERROR))) { info->flags |= ASYNC_NORMAL_ACTIVE; return 0; } if (tty->termios->c_cflag & CLOCAL) do_clocal = 1; /* * Block waiting for the carrier detect and the line to become * free (i.e., not in use by the callout). While we are in * this loop, info->count is dropped by one, so that * rs_close() knows when to free things. We restore it upon * exit, either normal or abnormal. */ retval = 0; add_wait_queue(&info->open_wait, &wait);#ifdef SERIAL_DEBUG_OPEN printk("block_til_ready before block: ttys%d, count = %d\n", info->line, info->count);#endif spin_lock_irqsave(&info->lock, flags); if (!tty_hung_up_p(filp)) info->count--; info->blocked_open++; while (1) { if ((tty->termios->c_cflag & CBAUD)) { unsigned int scratch; serial_out(info, UART_ESI_CMD1, ESI_READ_UART); serial_out(info, UART_ESI_CMD2, UART_MCR); scratch = serial_in(info, UART_ESI_STAT1); serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART); serial_out(info, UART_ESI_CMD2, UART_MCR); serial_out(info, UART_ESI_CMD2, scratch | UART_MCR_DTR | UART_MCR_RTS); } set_current_state(TASK_INTERRUPTIBLE); if (tty_hung_up_p(filp) || !(info->flags & ASYNC_INITIALIZED)) {#ifdef SERIAL_DO_RESTART if (info->flags & ASYNC_HUP_NOTIFY) retval = -EAGAIN; else retval = -ERESTARTSYS; #else retval = -EAGAIN;#endif break; } serial_out(info, UART_ESI_CMD1, ESI_GET_UART_STAT); if (serial_in(info, UART_ESI_STAT2) & UART_MSR_DCD) do_clocal = 1; if (!(info->flags & ASYNC_CLOSING) && (do_clocal)) break; if (signal_pending(current)) { retval = -ERESTARTSYS; break; }#ifdef SERIAL_DEBUG_OPEN printk("block_til_ready blocking: ttys%d, count = %d\n", info->line, info->count);#endif spin_unlock_irqrestore(&info->lock, flags); schedule(); spin_lock_irqsave(&info->lock, flags); } set_current_state(TASK_RUNNING); remove_wait_queue(&info->open_wait, &wait); if (!tty_hung_up_p(filp)) info->count++; info->blocked_open--; spin_unlock_irqrestore(&info->lock, flags);#ifdef SERIAL_DEBUG_OPEN printk("block_til_ready after blocking: ttys%d, count = %d\n", info->line, info->count);#endif if (retval) return retval; info->flags |= ASYNC_NORMAL_ACTIVE; return 0;} /* * This routine is called whenever a serial port is opened. It * enables interrupts for a serial port, linking in its async structure into * the IRQ chain. It also performs the serial-specific * initialization for the tty structure. */static int esp_open(struct tty_struct *tty, struct file * filp){ struct esp_struct *info; int retval, line; unsigned long flags; line = tty->index; if ((line < 0) || (line >= NR_PORTS)) return -ENODEV; /* find the port in the chain */ info = ports; while (info && (info->line != line)) info = info->next_port; if (!info) { serial_paranoia_check(info, tty->name, "esp_open"); return -ENODEV; }#ifdef SERIAL_DEBUG_OPEN printk("esp_open %s, count = %d\n", tty->name, info->count);#endif spin_lock_irqsave(&info->lock, flags); info->count++; tty->driver_data = info; info->tty = tty; spin_unlock_irqrestore(&info->lock, flags); /* * Start up serial port */ retval = startup(info); if (retval) return retval; retval = block_til_ready(tty, filp, info); if (retval) {#ifdef SERIAL_DEBUG_OPEN printk("esp_open returning after block_til_ready with %d\n", retval);#endif return retval; }#ifdef SERIAL_DEBUG_OPEN printk("esp_open %s successful...", tty->name);#endif return 0;}/* * --------------------------------------------------------------------- * espserial_init() and friends * * espserial_init() is called at boot-time to initialize the serial driver. * --------------------------------------------------------------------- *//* * This routine prints out the appropriate serial driver version * number, and identifies which options were configured into this * driver. */ static inline void show_serial_version(void){ printk(KERN_INFO "%s version %s (DMA %u)\n", serial_name, serial_version, dma);}/* * This routine is called by espserial_init() to initialize a specific serial * port. */static inline int autoconfig(struct esp_struct * info){ int port_detected = 0; unsigned long flags; if (!request_region(info->port, REGION_SIZE, "esp serial")) return -EIO; spin_lock_irqsave(&info->lock, flags); /* * Check for ESP card */ if (serial_in(info, UART_ESI_BASE) == 0xf3) { serial_out(info, UART_ESI_CMD1, 0x00); serial_out(info, UART_ESI_CMD1, 0x01); if ((serial_in(info, UART_ESI_STAT2) & 0x70) == 0x20) { port_detected = 1; if (!(info->irq)) { serial_out(info, UART_ESI_CMD1, 0x02); if (serial_in(info, UART_ESI_STAT1) & 0x01) info->irq = 3; else info->irq = 4; } /* put card in enhanced mode */ /* this prevents access through */ /* the "old" IO ports */ esp_basic_init(info); /* clear out MCR */ serial_out(info, UART_ESI_CMD1, ESI_WRITE_UART); serial_out(info, UART_ESI_CMD2, UART_MCR); serial_out(info, UART_ESI_CMD2, 0x00); } } if (!port_detected) release_region(info->port, REGION_SIZE); spin_unlock_irqrestore(&info->lock, flags); return (port_detected);}static const struct tty_operations esp_ops = { .open = esp_open, .close = rs_close, .write = rs_write, .put_char = rs_put_char, .flush_chars = rs_flush_chars, .write_room = rs_write_room, .chars_in_buffer = rs_chars_in_buffer, .flush_buffer = rs_flush_buffer, .ioctl = rs_ioctl, .throttle = rs_throttle, .unthrottle = rs_unthrottle, .set_termios = rs_set_termios, .stop = rs_stop, .start = rs_start, .hangup = esp_hangup, .break_ctl = esp_break, .wait_until_sent = rs_wait_until_sent, .tiocmget = esp_tiocmget, .tiocmset = esp_tiocmset,};/* * The serial driver boot-time initialization code! */static int __init espserial_init(void){ int i, offset; struct esp_struct * info; struct esp_struct *last_primary = NULL; int esp[] = {0x100,0x140,0x180,0x200,0x240,0x280,0x300,0x380}; esp_driver = alloc_tty_driver(NR_PORTS); if (!esp_driver) return -ENOMEM; for (i = 0; i < NR_PRIMARY; i++) { if (irq[i] != 0) { if ((irq[i] < 2) || (irq[i] > 15) || (irq[i] == 6) || (irq[i] == 8) || (irq[i] == 13)) irq[i] = 0; else if (irq[i] == 2) irq[i] = 9; } } if ((dma != 1) && (dma != 3)) dma = 0; if ((rx_trigger < 1) || (rx_trigger > 1023)) rx_trigger = 768; if ((tx_trigger < 1) || (tx_trigger > 1023)) tx_trigger = 768; if ((flow_off < 1) || (flow_off > 1023)) flow_off = 1016; if ((flow_on < 1) || (flow_on > 1023)) flow_on = 944; if ((rx_timeout < 0) || (rx_timeout > 255)) rx_timeout = 128; if (flow_on >
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -