esp.c
来自「Linux Kernel 2.6.9 for OMAP1710」· C语言 代码 · 共 2,491 行 · 第 1/5 页
C
2,491 行
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; /* * 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) { 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); /* * 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) { set_current_state(TASK_INTERRUPTIBLE); schedule_timeout(info->close_delay); } wake_up_interruptible(&info->open_wait); } info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING); wake_up_interruptible(&info->close_wait);out: restore_flags(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; save_flags(flags); cli(); 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)) { set_current_state(TASK_INTERRUPTIBLE); schedule_timeout(char_time); if (signal_pending(current)) break; if (timeout && time_after(jiffies, orig_jiffies + timeout)) break; serial_out(info, UART_ESI_CMD1, ESI_NO_COMMAND); serial_out(info, UART_ESI_CMD1, ESI_GET_TX_AVAIL); } restore_flags(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 save_flags(flags); cli(); if (!tty_hung_up_p(filp)) info->count--; restore_flags(flags); info->blocked_open++; while (1) { save_flags(flags); cli(); 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); } restore_flags(flags); 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 schedule(); } set_current_state(TASK_RUNNING); remove_wait_queue(&info->open_wait, &wait); if (!tty_hung_up_p(filp)) info->count++; info->blocked_open--;#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; 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 info->count++; tty->driver_data = info; info->tty = tty; if (!tmp_buf) { tmp_buf = (unsigned char *) get_zeroed_page(GFP_KERNEL); if (!tmp_buf) return -ENOMEM; } /* * 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 *region_start){ int port_detected = 0; unsigned long flags; save_flags(flags); cli(); /* * Check for ESP card */ if (!check_region(info->port, 8) && 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; } if (ports && (ports->port == (info->port - 8))) { release_region(*region_start, info->port - *region_start); } else *region_start = info->port; if (!request_region(*region_start, info->port - *region_start + 8, "esp serial")) { restore_flags(flags); return -EIO; } /* 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); } } restore_flags(flags); return (port_detected);}static 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! */int __init espserial_init(void){ int i, offset; int region_start; 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 >= flow_off) flow_on = flow_off - 1; show_serial_version(); /* Initialize the tty_driver structure */ esp_driver->owner = THIS_MODULE; esp_driver->name = "ttyP"; esp_driver->devfs_name = "tts/P"; esp_driver->major = ESP_IN_MAJOR; esp_driver->minor_start = 0; esp_driver->type = TTY_DRIVER_TYPE_SERIAL; esp_driver->subtype = SERIAL_TYPE_NORMAL; esp_driver->init_termios = tty_std_termios; esp_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL; esp_driver->flags = TTY_DRIVER_REAL_RAW; tty_set_operations(esp_driver, &esp_ops); if (tty_register_driver(esp_driver)) { printk(KERN_ERR "Couldn't register esp
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?