📄 serial_atmel.c
字号:
memcpy(info->xmit_buf + info->xmit_head, buf, c); } info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE - 1); info->xmit_cnt += c; restore_flags(flags); buf += c; count -= c; total += c; } if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) { /* Enable transmitter */ cli(); /*printk("Enabling transmitter\n"); */ if (!info->use_ints) { while (info->xmit_cnt) { wait_EOT(info->usart); /* Send char */ xmit_char(info, info->xmit_buf[info->xmit_tail++]); wait_EOT(info->usart); info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE - 1); info->xmit_cnt--; } } else { if (info->xmit_cnt) { /* Send char */ wait_EOT(info->usart); if (info->xmit_tail + info->xmit_cnt < SERIAL_XMIT_SIZE) { xmit_string(info, info->xmit_buf + info->xmit_tail, info->xmit_cnt); info->xmit_tail = (info->xmit_tail + info->xmit_cnt) & (SERIAL_XMIT_SIZE - 1); info->xmit_cnt = 0; } else { coucou2(); xmit_string(info, info->xmit_buf + info->xmit_tail, SERIAL_XMIT_SIZE - info->xmit_tail); //xmit_string(info, info->xmit_buf, info->xmit_tail + info->xmit_cnt - SERIAL_XMIT_SIZE); info->xmit_cnt = info->xmit_cnt - (SERIAL_XMIT_SIZE - info->xmit_tail); info->xmit_tail = 0; } } } } else { /*printk("Skipping transmit\n"); */ }#if 0 printk("Enabling stuff anyhow\n"); tx_start(0); if (SCC_EOT(0, 0)) { printk("TX FIFO empty.\n"); /* Send char */ atmel_xmit_char(info->usart, info->xmit_buf[info->xmit_tail++]); info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE - 1); info->xmit_cnt--; }#endif restore_flags(flags); return total;}static int rs_write_room(struct tty_struct *tty){ struct atmel_serial *info = (struct atmel_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 atmel_serial *info = (struct atmel_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 atmel_serial *info = (struct atmel_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); if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) && tty->ldisc.write_wakeup) (tty->ldisc.write_wakeup) (tty);}/* * ------------------------------------------------------------ * 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 atmel_serial *info = (struct atmel_serial *) tty->driver_data;#ifdef SERIAL_DEBUG_THROTTLE char buf[64]; printk("throttle %s: %d....\n", _tty_name(tty, buf), tty->ldisc.chars_in_buffer(tty));#endif 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 atomic) */}static void rs_unthrottle(struct tty_struct *tty){ struct atmel_serial *info = (struct atmel_serial *) tty->driver_data;#ifdef SERIAL_DEBUG_THROTTLE char buf[64]; printk("unthrottle %s: %d....\n", _tty_name(tty, buf), tty->ldisc.chars_in_buffer(tty));#endif 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 atomic) */}/* * ------------------------------------------------------------ * rs_ioctl() and friends * ------------------------------------------------------------ */static int get_serial_info(struct atmel_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; copy_to_user(retinfo, &tmp, sizeof(*retinfo)); return 0;}static int set_serial_info(struct atmel_serial *info, struct serial_struct *new_info){ struct serial_struct new_serial; struct atmel_serial old_info; int retval = 0; if (!new_info) return -EFAULT; copy_from_user(&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); change_speed(info); retval = 0; 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 atmel_serial *info, unsigned int *value){ unsigned char status; cli(); status = info->usart->csr; status &= US_TXEMPTY; sti(); put_user(status, value); return 0;}/* * This routine sends a break character out the serial port. */static void send_break(struct atmel_serial *info, int duration){ unsigned long flags; if (!info->port) return; current->state = TASK_INTERRUPTIBLE; save_flags(flags); cli(); info->usart->cr = US_STTBRK; if (!info->use_ints) { while (US_TXRDY != (info->usart->csr & US_TXRDY)) { ; // this takes max 2ms at 9600 } info->usart->cr = US_STPBRK; } restore_flags(flags);}static int rs_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg){ int error; struct atmel_serial *info = (struct atmel_serial *) tty->driver_data; 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 atmel_serial)); if (error) return error; copy_to_user((struct atmel_serial *) arg, info, sizeof(struct atmel_serial)); return 0; case TCSETS: handle_termios_tcsets((struct termios *)arg, info); // return set_serial_info(info, (struct serial_struct *) arg); break; default: return -ENOIOCTLCMD; } return 0;}static void handle_termios_tcsets(struct termios * ptermios, struct atmel_serial * pinfo ){ /* * hmmmm.... */ 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 atmel_serial *info = (struct atmel_serial *) tty->driver_data; if (tty->termios->c_cflag == old_termios->c_cflag) return; change_speed(info); 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 atmel_serial *info = (struct atmel_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; } if (info->count) { restore_flags(flags); return; } // closing port so disable interrupts set_ints_mode(0, info); 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); if (tty->ldisc.flush_buffer) tty->ldisc.flush_buffer(tty); tty->closing = 0; info->event = 0; info->tty = 0; if (tty->ldisc.num != ldiscs[N_TTY].num) { if (tty->ldisc.close) (tty->ldisc.close) (tty); tty->ldisc = ldiscs[N_TTY]; tty->termios->c_line = N_TTY; if (tty->ldisc.open) (tty->ldisc.open) (tty); } if (info->blocked_open) { if (info->close_delay) { current->state = TASK_INTERRUPTIBLE; schedule_timeout(info->close_delay); } wake_up_interruptible(&info->open_wait); } info->flags &= ~(S_NORMAL_ACTIVE | S_CALLOUT_ACTIVE | S_CLOSING); wake_up_interruptible(&info->close_wait); restore_flags(flags);}/* * rs_hangup() --- called by tty_hangup() when a hangup is signaled. */static void rs_hangup(struct tty_struct *tty){ struct atmel_serial *info = (struct atmel_serial *) tty->driver_data; if (serial_paranoia_check(info, tty->device, "rs_hangup")) return; rs_flush_buffer(tty); shutdown(info); info->event = 0; info->count = 0; info->flags &= ~(S_NORMAL_ACTIVE | S_CALLOUT_ACTIVE); info->tty = 0; wake_up_interruptible(&info->open_wait);}/* * ------------------------------------------------------------ * rs_open() and friends * ------------------------------------------------------------ */static int block_til_ready(struct tty_struct *tty, struct file *filp, struct atmel_serial *info){ DECLARE_WAITQUEUE(wait, current); int retval; int do_clocal = 0; /* * If the device is in the middle of being closed, then block * until it's done, and then try again. */ if (info->flags & S_CLOSING) { interruptible_sleep_on(&info->close_wait);#ifdef SERIAL_DO_RESTART if (info->flags & S_HUP_NOTIFY) return -EAGAIN; else return -ERESTARTSYS;#else return -EAGAIN;#endif } /* * If this is a callout device, then just make sure the normal * device isn't being used. */ if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -