📄 macserial.c
字号:
SERIAL_XMIT_SIZE - info->xmit_head)); memcpy(info->xmit_buf + info->xmit_head, tmp_buf, c); info->xmit_head = ((info->xmit_head + c) & (SERIAL_XMIT_SIZE-1)); info->xmit_cnt += c; restore_flags(flags); buf += c; count -= c; ret += c; } up(&tmp_buf_sem); } else { while (1) { save_flags(flags); cli(); c = MIN(count, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1, SERIAL_XMIT_SIZE - info->xmit_head)); if (c <= 0) { restore_flags(flags); 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; restore_flags(flags); buf += c; count -= c; ret += c; } } if (info->xmit_cnt && !tty->stopped && !info->tx_stopped && !info->tx_active) transmit_chars(info); return ret;}static int rs_write_room(struct tty_struct *tty){ struct mac_serial *info = (struct mac_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 mac_serial *info = (struct mac_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){ struct mac_serial *info = (struct mac_serial *)tty->driver_data; unsigned long flags; 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 mac_serial *info = (struct mac_serial *)tty->driver_data; unsigned long flags;#ifdef SERIAL_DEBUG_THROTTLE printk("throttle %ld....\n",tty->ldisc.chars_in_buffer(tty));#endif if (serial_paranoia_check(info, tty->device, "rs_throttle")) return; if (I_IXOFF(tty)) { save_flags(flags); cli(); info->x_char = STOP_CHAR(tty); if (!info->tx_active) transmit_chars(info); restore_flags(flags); } if (C_CRTSCTS(tty)) { /* * Here we want to turn off the RTS line. On Macintoshes, * we only get the DTR line, which goes to both DTR and * RTS on the modem. RTS doesn't go out to the serial * port socket. So you should make sure your modem is * set to ignore DTR if you're using CRTSCTS. */ save_flags(flags); cli(); info->curregs[5] &= ~(DTR | RTS); info->pendregs[5] &= ~(DTR | RTS); write_zsreg(info->zs_channel, 5, info->curregs[5]); restore_flags(flags); }}static void rs_unthrottle(struct tty_struct * tty){ struct mac_serial *info = (struct mac_serial *)tty->driver_data; unsigned long flags;#ifdef SERIAL_DEBUG_THROTTLE printk("unthrottle %s: %d....\n",tty->ldisc.chars_in_buffer(tty));#endif if (serial_paranoia_check(info, tty->device, "rs_unthrottle")) return; if (I_IXOFF(tty)) { save_flags(flags); cli(); if (info->x_char) info->x_char = 0; else { info->x_char = START_CHAR(tty); if (!info->tx_active) transmit_chars(info); } restore_flags(flags); } if (C_CRTSCTS(tty)) { /* Assert RTS and DTR lines */ save_flags(flags); cli(); info->curregs[5] |= DTR | RTS; info->pendregs[5] |= DTR | RTS; write_zsreg(info->zs_channel, 5, info->curregs[5]); restore_flags(flags); }}/* * ------------------------------------------------------------ * rs_ioctl() and friends * ------------------------------------------------------------ */static int get_serial_info(struct mac_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.port = info->port; tmp.irq = info->irq; 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; if (copy_to_user(retinfo,&tmp,sizeof(*retinfo))) return -EFAULT; return 0;}static int set_serial_info(struct mac_serial * info, struct serial_struct * new_info){ struct serial_struct new_serial; struct mac_serial old_info; int retval = 0; if (copy_from_user(&new_serial,new_info,sizeof(new_serial))) return -EFAULT; old_info = *info; if (!capable(CAP_SYS_ADMIN)) { if ((new_serial.baud_base != info->baud_base) || (new_serial.type != info->type) || (new_serial.close_delay != info->close_delay) || ((new_serial.flags & ~ZILOG_USR_MASK) != (info->flags & ~ZILOG_USR_MASK))) return -EPERM; info->flags = ((info->flags & ~ZILOG_USR_MASK) | (new_serial.flags & ZILOG_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 & ~ZILOG_FLAGS) | (new_serial.flags & ZILOG_FLAGS)); info->type = new_serial.type; info->close_delay = new_serial.close_delay; info->closing_wait = new_serial.closing_wait;check_and_exit: if (info->flags & ZILOG_INITIALIZED) retval = setup_scc(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 mac_serial * info, unsigned int *value){ unsigned char status; unsigned long flags; save_flags(flags); cli(); status = read_zsreg(info->zs_channel, 0); restore_flags(flags); status = (status & Tx_BUF_EMP)? TIOCSER_TEMT: 0; return put_user(status,value);}static int get_modem_info(struct mac_serial *info, unsigned int *value){ unsigned char control, status; unsigned int result; unsigned long flags; save_flags(flags); cli(); control = info->curregs[5]; status = read_zsreg(info->zs_channel, 0); restore_flags(flags); result = ((control & RTS) ? TIOCM_RTS: 0) | ((control & DTR) ? TIOCM_DTR: 0) | ((status & DCD) ? TIOCM_CAR: 0) | ((status & CTS) ? 0: TIOCM_CTS); return put_user(result,value);}static int set_modem_info(struct mac_serial *info, unsigned int cmd, unsigned int *value){ int error; unsigned int arg, bits; unsigned long flags; error = get_user(arg, value); if (error) return error; bits = (arg & TIOCM_RTS? RTS: 0) + (arg & TIOCM_DTR? DTR: 0); save_flags(flags); cli(); switch (cmd) { case TIOCMBIS: info->curregs[5] |= bits; break; case TIOCMBIC: info->curregs[5] &= ~bits; break; case TIOCMSET: info->curregs[5] = (info->curregs[5] & ~(DTR | RTS)) | bits; break; default: restore_flags(flags); return -EINVAL; } info->pendregs[5] = info->curregs[5]; write_zsreg(info->zs_channel, 5, info->curregs[5]); restore_flags(flags); return 0;}/* * rs_break - turn transmit break condition on/off */static void rs_break(struct tty_struct *tty, int break_state){ struct mac_serial *info = (struct mac_serial *) tty->driver_data; unsigned long flags; if (serial_paranoia_check(info, tty->device, "rs_break")) return; save_flags(flags); cli(); if (break_state == -1) info->curregs[5] |= SND_BRK; else info->curregs[5] &= ~SND_BRK; write_zsreg(info->zs_channel, 5, info->curregs[5]); restore_flags(flags);}static int rs_ioctl(struct tty_struct *tty, struct file * file, unsigned int cmd, unsigned long arg){ struct mac_serial * info = (struct mac_serial *)tty->driver_data;#ifdef CONFIG_KGDB if (info->kgdb_channel) return -ENODEV;#endif if (serial_paranoia_check(info, tty->device, "rs_ioctl")) return -ENODEV; if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) && (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT)) { if (tty->flags & (1 << TTY_IO_ERROR)) return -EIO; } switch (cmd) { case TIOCMGET: return get_modem_info(info, (unsigned int *) arg); case TIOCMBIS: case TIOCMBIC: case TIOCMSET: return set_modem_info(info, cmd, (unsigned int *) arg); case TIOCGSERIAL: 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 */ return get_lsr_info(info, (unsigned int *) arg); case TIOCSERGSTRUCT: if (copy_to_user((struct mac_serial *) arg, info, sizeof(struct mac_serial))) return -EFAULT; return 0; default: return -ENOIOCTLCMD; } return 0;}static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios){ struct mac_serial *info = (struct mac_serial *)tty->driver_data; int was_stopped; if (tty->termios->c_cflag == old_termios->c_cflag) return; was_stopped = info->tx_stopped; change_speed(info, old_termios); if (was_stopped && !info->tx_stopped) { tty->hw_stopped = 0; rs_start(tty); }}/* * ------------------------------------------------------------ * rs_close() * * This routine is called when the serial port gets closed. * Wait for the last remaining data to be sent. * ------------------------------------------------------------ */static void rs_close(struct tty_struct *tty, struct file * filp){ struct mac_serial * info = (struct mac_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)) { MOD_DEC_USE_COUNT; restore_flags(flags); return; } OPNDBG("rs_close ttys%d, count = %d\n", info->line, info->count); 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) { MOD_DEC_USE_COUNT; restore_flags(flags); return; } info->flags |= ZILOG_CLOSING; /* * Save the termios structure, since this port may have * separate termios for callout and dialin. */ if (info->flags & ZILOG_NORMAL_ACTIVE) info->normal_termios = *tty->termios; if (info->flags & ZILOG_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. */ OPNDBG("waiting end of Tx... (timeout:%d)\n", info->closing_wait); tty->closing = 1; if (info->closing_wait != ZILOG_CLOSING_WAIT_NONE) { restore_flags(flags); tty_wait_until_sent(tty, info->closing_wait); save_flags(flags); cli(); } /* * At this point we stop accepting input. To do this, we * disable the receiver and receive interrupts. */ info->curregs[3] &= ~RxENABLE; info->pendregs[3] = info->curregs[3]; write_zsreg(info->zs_channel, 3, info->curregs[3]); info->curregs[1] &= ~(0x18); /* disable any rx ints */ info->pendregs[1] = info->curregs[1]; write_zsreg(info->zs_channel, 1, info->curregs[1]); ZS_CLEARFIFO(info->zs_channel); if (info->flags & ZILOG_INITIALIZED) { /* * Before we drop DTR, make sure the SCC transmitter * has completely drained. */ OPNDBG("waiting end of Rx...\n"); restore_flags(flags); rs_wait_until_sent(tty, info->timeout); save_flags(flags); cli(); } shutdown(info); /* restore flags now since shutdown() will have disabled this port's specific irqs */ restore_flags(flags); 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 (info->blocked_open) { if (info->close_delay) { current->state = TASK_INTERRUPTIBLE; schedule_timeout(info->close_delay); } wake_up_interruptible(&info->open_wait); } info->flags &= ~(ZILOG_NORMAL_ACTIVE|ZILOG_CALLOUT_ACTIVE| ZILOG_CLOSING); wake_up_interruptible(&info->close_wait); MOD_DEC_USE_COUNT;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -