📄 serial_s3c44b0x.c
字号:
cli(); while (1) { c = MIN(count, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1, SERIAL_XMIT_SIZE - info->xmit_head)); if (c <= 0) break; memcpy(info->xmit_buf + info->xmit_head, buf, c); info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE - 1); info->xmit_cnt += c; buf += c; count -= c; total += c; } restore_flags(flags); } if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) { if (!info->use_ints) { while (info->xmit_cnt) { /* Send char */ xmit_char(info, info->xmit_buf[info->xmit_tail++]); info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE - 1); info->xmit_cnt--; } } else { /* send one char */ tx_start(info); xmit_char(info, info->xmit_buf[info->xmit_tail++]); info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE - 1); info->xmit_cnt--; } } restore_flags(flags); return total;}static int rs_write_room(struct tty_struct *tty){ struct s3c44b0x_serial *info = (struct s3c44b0x_serial *) tty->driver_data; int ret; if (serial_paranoia_check(info, tty->device, "rs_write_room")) return 0; ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1; if (ret < 0) ret = 0; return ret;}static int rs_chars_in_buffer(struct tty_struct *tty){ struct s3c44b0x_serial *info = (struct s3c44b0x_serial *) tty->driver_data; if (serial_paranoia_check(info, tty->device, "rs_chars_in_buffer")) return 0; return info->xmit_cnt;}static void rs_flush_buffer(struct tty_struct *tty){ unsigned long flags; struct s3c44b0x_serial *info = (struct s3c44b0x_serial *) tty->driver_data; if (serial_paranoia_check(info, tty->device, "rs_flush_buffer")) return; save_flags(flags); cli(); info->xmit_cnt = info->xmit_head = info->xmit_tail = 0; restore_flags(flags); wake_up_interruptible(&tty->write_wait);}/* * ------------------------------------------------------------ * rs_throttle() * * This routine is called by the upper-layer tty layer to signal that * incoming characters should be throttled. * ------------------------------------------------------------ */static void rs_throttle(struct tty_struct *tty){ struct s3c44b0x_serial *info = (struct s3c44b0x_serial *) tty->driver_data; unsigned int flags; if (serial_paranoia_check(info, tty->device, "rs_throttle")) return; if (I_IXOFF(tty)) info->x_char = STOP_CHAR(tty); /* Turn off RTS line (do this atomically) */ if (tty->termios->c_cflag & CRTSCTS) { save_flags(flags); cli(); set_rts (info, 0); restore_flags(flags); }}static void rs_unthrottle(struct tty_struct *tty){ struct s3c44b0x_serial *info = (struct s3c44b0x_serial *) tty->driver_data; unsigned int flags; if (serial_paranoia_check(info, tty->device, "rs_unthrottle")) return; if (I_IXOFF(tty)) { if (info->x_char) info->x_char = 0; else info->x_char = START_CHAR(tty); } /* Assert RTS line (do this atomically) */ if (tty->termios->c_cflag & CRTSCTS) { save_flags(flags); cli(); set_rts (info, 1); restore_flags(flags); }}/* * ------------------------------------------------------------ * rs_ioctl() and friends * ------------------------------------------------------------ */static int get_serial_info(struct s3c44b0x_serial *info, struct serial_struct *retinfo){ struct serial_struct tmp; if (!retinfo) return -EFAULT; memset(&tmp, 0, sizeof(tmp)); tmp.type = info->type; tmp.line = info->line; tmp.irq = info->irq; tmp.port = info->port; tmp.flags = info->flags; tmp.baud_base = info->baud_base; tmp.close_delay = info->close_delay; tmp.closing_wait = info->closing_wait; tmp.custom_divisor = info->custom_divisor; memcpy_tofs(retinfo, &tmp, sizeof(*retinfo)); return 0;}static int set_serial_info(struct s3c44b0x_serial *info, struct serial_struct *new_info){ struct serial_struct new_serial; struct s3c44b0x_serial old_info; int retval = 0; if (!new_info) return -EFAULT; memcpy_fromfs(&new_serial, new_info, sizeof(new_serial)); old_info = *info; if (!suser()) { if ((new_serial.baud_base != info->baud_base) || (new_serial.type != info->type) || (new_serial.close_delay != info->close_delay) || ((new_serial.flags & ~S_USR_MASK) != (info->flags & ~S_USR_MASK))) return -EPERM; info->flags = ((info->flags & ~S_USR_MASK) | (new_serial.flags & S_USR_MASK)); info->custom_divisor = new_serial.custom_divisor; goto check_and_exit; } if (info->count > 1) return -EBUSY; /* * OK, past this point, all the error checking has been done. * At this point, we start making changes..... */ info->baud_base = new_serial.baud_base; info->flags = ((info->flags & ~S_FLAGS) | (new_serial.flags & S_FLAGS)); info->type = new_serial.type; info->close_delay = new_serial.close_delay; info->closing_wait = new_serial.closing_wait;check_and_exit: retval = startup(info); return retval;}/* * get_lsr_info - get line status register info * * Purpose: Let user call ioctl() to get info when the UART physically * is emptied. On bus types like RS485, the transmitter must * release the bus after transmitting. This must be done when * the transmit shift register is empty, not be done when the * transmit holding register is empty. This functionality * allows an RS485 driver to be written in user space. */static int get_lsr_info(struct s3c44b0x_serial *info, unsigned int *value){ unsigned char status; cli();/* status = (info->usart->csr & U_CTS) ? 1 : 0; */ status = 0x00; sti(); put_user(status, value); return 0;}/* * This routine sends a break character out the serial port. */static void send_break(struct s3c44b0x_serial *info, int duration){ u_int8_t ucon; current->state = TASK_INTERRUPTIBLE; cli(); ucon = inb(S3C44B0X_UCON0 + info->uart_offset); ucon |= S3C44B0X_UCON_SEND_BREAK; outb(ucon, S3C44B0X_UCON0 + info->uart_offset); schedule_timeout(info->close_delay); ucon &= ~S3C44B0X_UCON_SEND_BREAK; outb(ucon, S3C44B0X_UCON0 + info->uart_offset); sti();}static int rs_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg){ int error; struct s3c44b0x_serial *info = (struct s3c44b0x_serial *) tty->driver_data; unsigned int val; int retval; if (serial_paranoia_check(info, tty->device, "rs_ioctl")) return -ENODEV; if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) && (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) && (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) { if (tty->flags & (1 << TTY_IO_ERROR)) return -EIO; } switch (cmd) { case TCSBRK: /* SVID version: non-zero arg --> no break */ retval = tty_check_change(tty); if (retval) return retval; tty_wait_until_sent(tty, 0); if (!arg) send_break(info, HZ / 4); /* 1/4 second */ return 0; case TCSBRKP: /* support for POSIX tcsendbreak() */ retval = tty_check_change(tty); if (retval) return retval; tty_wait_until_sent(tty, 0); send_break(info, arg ? arg * (HZ / 10) : HZ / 4); return 0; case TIOCGSOFTCAR: error = verify_area(VERIFY_WRITE, (void *) arg, sizeof(long)); if (error) return error; put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long *) arg); return 0; case TIOCSSOFTCAR: arg = get_user(arg, (unsigned long *) arg); tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) | (arg ? CLOCAL : 0)); return 0; case TIOCGSERIAL: error = verify_area(VERIFY_WRITE, (void *) arg, sizeof(struct serial_struct)); if (error) return error; return get_serial_info(info, (struct serial_struct *) arg); case TIOCSSERIAL: return set_serial_info(info, (struct serial_struct *) arg); case TIOCSERGETLSR: /* Get line status register */ error = verify_area(VERIFY_WRITE, (void *) arg, sizeof(unsigned int)); if (error) return error; else return get_lsr_info(info, (unsigned int *) arg); case TIOCSERGSTRUCT: error = verify_area(VERIFY_WRITE, (void *) arg, sizeof(struct s3c44b0x_serial)); if (error) return error; memcpy_tofs((struct s3c44b0x_serial *) arg, info, sizeof(struct s3c44b0x_serial)); return 0; case TIOCMGET: /* get all modem bits */ if ((error = verify_area(VERIFY_WRITE, (void *) arg, sizeof(unsigned int)))) return(error); val = get_status(info); put_user(val, (unsigned int *) arg); break; case TIOCMBIS: /* modem bits set */ if ((error = verify_area(VERIFY_WRITE, (void *) arg, sizeof(unsigned int)))) return(error); get_user(val, (unsigned int *) arg); if (val & TIOCM_RTS) set_rts(info, 1); if (val & TIOCM_DTR) set_dtr(info, 1); break; case TIOCMBIC: /* modem bits clear */ if ((error = verify_area(VERIFY_WRITE, (void *) arg, sizeof(unsigned int)))) return(error); get_user(val, (unsigned int *) arg); if (val & TIOCM_RTS) set_rts(info, 0); if (val & TIOCM_DTR) set_dtr(info, 0); break; case TIOCMSET: /* set all modem bits */ if ((error = verify_area(VERIFY_WRITE, (void *) arg, sizeof(unsigned int)))) return(error); get_user(val, (unsigned int *) arg); set_rts (info, ((val & TIOCM_RTS) ? 1 : 0) ); set_dtr (info, ((val & TIOCM_DTR) ? 1 : 0) ); break; case TCSETS: /* * take care if the speed change, leave rest to termios by falling through */ handle_termios_tcsets((struct termios *)arg, info); break; case TIOSERMODE: { /* set the serial mode */ serial_mode_t orig_ser_mode = info->ser_mode; info->ser_mode = (serial_mode_t)arg; switch ((serial_mode_t)arg) { case SERIAL_MODE_NONE: tx_disable(info); rx_disable(info); break; case SERIAL_MODE_RS422: tx_enable(info); rx_enable(info); break; case SERIAL_MODE_RS422_LISTEN: tx_enable(info); rx_enable(info); break; case SERIAL_MODE_RS485_ECHO: tx_disable(info); rx_enable(info); break; case SERIAL_MODE_RS485_NO_ECHO: tx_disable(info); rx_enable(info); break; default: info->ser_mode = orig_ser_mode; return 0; } break; } default: return -ENOIOCTLCMD; } return 0;}static void handle_termios_tcsets( struct termios *ptermios, struct s3c44b0x_serial *pinfo){ if (pinfo->tty->termios->c_cflag != ptermios->c_cflag) { pinfo->tty->termios->c_cflag = ptermios->c_cflag; } change_speed(pinfo);} static void rs_set_termios( struct tty_struct *tty, struct termios *old_termios){ struct s3c44b0x_serial *info = (struct s3c44b0x_serial *) tty->driver_data; if (tty->termios->c_cflag == old_termios->c_cflag) return; change_speed(info); /* Handle turning off CRTSCTS */ if ((old_termios->c_cflag & CRTSCTS) && !(tty->termios->c_cflag & CRTSCTS)) { tty->hw_stopped = 0; rs_start(tty); }}/* * ------------------------------------------------------------ * 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 * S 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 s3c44b0x_serial *info = (struct s3c44b0x_serial *) tty->driver_data; unsigned long flags; if (!info || serial_paranoia_check(info, tty->device, "rs_close")) return; save_flags(flags); cli(); if (tty_hung_up_p(filp)) { restore_flags(flags); return; }#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; } /* wait until data is sent */ wait_tx_empty(info, 1); if (info->count) { restore_flags(flags); return; } /* closing port so disable interrupts */ set_ints_mode(info, 0); info->flags |= S_CLOSING; /* * Save the termios structure, since this port may have * separate termios for callout and dialin. */ if (info->flags & S_NORMAL_ACTIVE) info->normal_termios = *tty->termios; if (info->flags & S_CALLOUT_ACTIVE) info->callout_termios = *tty->termios; /* * 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 != S_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. */ shutdown(info); if (tty->driver.flush_buffer) tty->driver.flush_buffer(tty); tty->closing = 0; info->event = 0; info->tty = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -