/*
|
/*
|
* n_tty.c --- implements the N_TTY line discipline.
|
* n_tty.c --- implements the N_TTY line discipline.
|
*
|
*
|
* This code used to be in tty_io.c, but things are getting hairy
|
* This code used to be in tty_io.c, but things are getting hairy
|
* enough that it made sense to split things off. (The N_TTY
|
* enough that it made sense to split things off. (The N_TTY
|
* processing has changed so much that it's hardly recognizable,
|
* processing has changed so much that it's hardly recognizable,
|
* anyway...)
|
* anyway...)
|
*
|
*
|
* Note that the open routine for N_TTY is guaranteed never to return
|
* Note that the open routine for N_TTY is guaranteed never to return
|
* an error. This is because Linux will fall back to setting a line
|
* an error. This is because Linux will fall back to setting a line
|
* to N_TTY if it can not switch to any other line discipline.
|
* to N_TTY if it can not switch to any other line discipline.
|
*
|
*
|
* Written by Theodore Ts'o, Copyright 1994.
|
* Written by Theodore Ts'o, Copyright 1994.
|
*
|
*
|
* This file also contains code originally written by Linus Torvalds,
|
* This file also contains code originally written by Linus Torvalds,
|
* Copyright 1991, 1992, 1993, and by Julian Cowley, Copyright 1994.
|
* Copyright 1991, 1992, 1993, and by Julian Cowley, Copyright 1994.
|
*
|
*
|
* This file may be redistributed under the terms of the GNU Public
|
* This file may be redistributed under the terms of the GNU Public
|
* License.
|
* License.
|
*
|
*
|
* Altered for ARM to reduce memory usage.
|
* Altered for ARM to reduce memory usage.
|
*/
|
*/
|
|
|
#include <linux/types.h>
|
#include <linux/types.h>
|
#include <linux/major.h>
|
#include <linux/major.h>
|
#include <linux/errno.h>
|
#include <linux/errno.h>
|
#include <linux/signal.h>
|
#include <linux/signal.h>
|
#include <linux/fcntl.h>
|
#include <linux/fcntl.h>
|
#include <linux/sched.h>
|
#include <linux/sched.h>
|
#include <linux/interrupt.h>
|
#include <linux/interrupt.h>
|
#include <linux/tty.h>
|
#include <linux/tty.h>
|
#include <linux/timer.h>
|
#include <linux/timer.h>
|
#include <linux/ctype.h>
|
#include <linux/ctype.h>
|
#include <linux/kd.h>
|
#include <linux/kd.h>
|
#include <linux/mm.h>
|
#include <linux/mm.h>
|
#include <linux/string.h>
|
#include <linux/string.h>
|
#include <linux/malloc.h>
|
#include <linux/malloc.h>
|
|
|
#include <asm/segment.h>
|
#include <asm/segment.h>
|
#include <asm/system.h>
|
#include <asm/system.h>
|
#include <asm/bitops.h>
|
#include <asm/bitops.h>
|
|
|
#define CONSOLE_DEV MKDEV(TTY_MAJOR,0)
|
#define CONSOLE_DEV MKDEV(TTY_MAJOR,0)
|
|
|
#ifndef MIN
|
#ifndef MIN
|
#define MIN(a,b) ((a) < (b) ? (a) : (b))
|
#define MIN(a,b) ((a) < (b) ? (a) : (b))
|
#endif
|
#endif
|
|
|
/* number of characters left in xmit buffer before select has we have room */
|
/* number of characters left in xmit buffer before select has we have room */
|
#define WAKEUP_CHARS 256
|
#define WAKEUP_CHARS 256
|
|
|
/*
|
/*
|
* This defines the low- and high-watermarks for throttling and
|
* This defines the low- and high-watermarks for throttling and
|
* unthrottling the TTY driver. These watermarks are used for
|
* unthrottling the TTY driver. These watermarks are used for
|
* controlling the space in the read buffer.
|
* controlling the space in the read buffer.
|
*/
|
*/
|
#define TTY_THRESHOLD_THROTTLE (N_TTY_BUF_SIZE - 128)
|
#define TTY_THRESHOLD_THROTTLE (N_TTY_BUF_SIZE - 128)
|
#define TTY_THRESHOLD_UNTHROTTLE 128
|
#define TTY_THRESHOLD_UNTHROTTLE 128
|
|
|
static inline void put_tty_queue(unsigned char c, struct tty_struct *tty)
|
static inline void put_tty_queue(unsigned char c, struct tty_struct *tty)
|
{
|
{
|
if (tty->read_cnt < N_TTY_BUF_SIZE) {
|
if (tty->read_cnt < N_TTY_BUF_SIZE) {
|
tty->read_buf[tty->read_head] = c;
|
tty->read_buf[tty->read_head] = c;
|
tty->read_head = (tty->read_head + 1) & (N_TTY_BUF_SIZE-1);
|
tty->read_head = (tty->read_head + 1) & (N_TTY_BUF_SIZE-1);
|
tty->read_cnt++;
|
tty->read_cnt++;
|
}
|
}
|
}
|
}
|
|
|
/*
|
/*
|
* Flush the input buffer
|
* Flush the input buffer
|
*/
|
*/
|
void n_tty_flush_buffer(struct tty_struct * tty)
|
void n_tty_flush_buffer(struct tty_struct * tty)
|
{
|
{
|
tty->read_head = tty->read_tail = tty->read_cnt = 0;
|
tty->read_head = tty->read_tail = tty->read_cnt = 0;
|
tty->canon_head = tty->canon_data = tty->erasing = 0;
|
tty->canon_head = tty->canon_data = tty->erasing = 0;
|
memset(&tty->read_flags, 0, sizeof tty->read_flags);
|
memset(&tty->read_flags, 0, sizeof tty->read_flags);
|
|
|
if (!tty->link)
|
if (!tty->link)
|
return;
|
return;
|
|
|
if (tty->driver.unthrottle)
|
if (tty->driver.unthrottle)
|
(tty->driver.unthrottle)(tty);
|
(tty->driver.unthrottle)(tty);
|
if (tty->link->packet) {
|
if (tty->link->packet) {
|
tty->ctrl_status |= TIOCPKT_FLUSHREAD;
|
tty->ctrl_status |= TIOCPKT_FLUSHREAD;
|
wake_up_interruptible(&tty->link->read_wait);
|
wake_up_interruptible(&tty->link->read_wait);
|
}
|
}
|
}
|
}
|
|
|
/*
|
/*
|
* Return number of characters buffered to be delivered to user
|
* Return number of characters buffered to be delivered to user
|
*/
|
*/
|
int n_tty_chars_in_buffer(struct tty_struct *tty)
|
int n_tty_chars_in_buffer(struct tty_struct *tty)
|
{
|
{
|
if (tty->icanon) {
|
if (tty->icanon) {
|
if (!tty->canon_data) return 0;
|
if (!tty->canon_data) return 0;
|
|
|
return (tty->canon_head > tty->read_tail) ?
|
return (tty->canon_head > tty->read_tail) ?
|
tty->canon_head - tty->read_tail :
|
tty->canon_head - tty->read_tail :
|
tty->canon_head + (N_TTY_BUF_SIZE - tty->read_tail);
|
tty->canon_head + (N_TTY_BUF_SIZE - tty->read_tail);
|
}
|
}
|
return tty->read_cnt;
|
return tty->read_cnt;
|
}
|
}
|
|
|
/*
|
/*
|
* Perform OPOST processing. Returns -1 when the output device is
|
* Perform OPOST processing. Returns -1 when the output device is
|
* full and the character must be retried.
|
* full and the character must be retried.
|
*/
|
*/
|
static int opost(unsigned char c, struct tty_struct *tty)
|
static int opost(unsigned char c, struct tty_struct *tty)
|
{
|
{
|
int space, spaces;
|
int space, spaces;
|
|
|
space = tty->driver.write_room(tty);
|
space = tty->driver.write_room(tty);
|
if (!space)
|
if (!space)
|
return -1;
|
return -1;
|
|
|
if (O_OPOST(tty)) {
|
if (O_OPOST(tty)) {
|
switch (c) {
|
switch (c) {
|
case '\n':
|
case '\n':
|
if (O_ONLRET(tty))
|
if (O_ONLRET(tty))
|
tty->column = 0;
|
tty->column = 0;
|
if (O_ONLCR(tty)) {
|
if (O_ONLCR(tty)) {
|
if (space < 2)
|
if (space < 2)
|
return -1;
|
return -1;
|
tty->driver.put_char(tty, '\r');
|
tty->driver.put_char(tty, '\r');
|
tty->column = 0;
|
tty->column = 0;
|
}
|
}
|
tty->canon_column = tty->column;
|
tty->canon_column = tty->column;
|
break;
|
break;
|
case '\r':
|
case '\r':
|
if (O_ONOCR(tty) && tty->column == 0)
|
if (O_ONOCR(tty) && tty->column == 0)
|
return 0;
|
return 0;
|
if (O_OCRNL(tty)) {
|
if (O_OCRNL(tty)) {
|
c = '\n';
|
c = '\n';
|
if (O_ONLRET(tty))
|
if (O_ONLRET(tty))
|
tty->canon_column = tty->column = 0;
|
tty->canon_column = tty->column = 0;
|
break;
|
break;
|
}
|
}
|
tty->canon_column = tty->column = 0;
|
tty->canon_column = tty->column = 0;
|
break;
|
break;
|
case '\t':
|
case '\t':
|
spaces = 8 - (tty->column & 7);
|
spaces = 8 - (tty->column & 7);
|
if (O_TABDLY(tty) == XTABS) {
|
if (O_TABDLY(tty) == XTABS) {
|
if (space < spaces)
|
if (space < spaces)
|
return -1;
|
return -1;
|
tty->column += spaces;
|
tty->column += spaces;
|
tty->driver.write(tty, 0, " ", spaces);
|
tty->driver.write(tty, 0, " ", spaces);
|
return 0;
|
return 0;
|
}
|
}
|
tty->column += spaces;
|
tty->column += spaces;
|
break;
|
break;
|
case '\b':
|
case '\b':
|
if (tty->column > 0)
|
if (tty->column > 0)
|
tty->column--;
|
tty->column--;
|
break;
|
break;
|
default:
|
default:
|
if (O_OLCUC(tty))
|
if (O_OLCUC(tty))
|
c = toupper(c);
|
c = toupper(c);
|
if (!iscntrl(c))
|
if (!iscntrl(c))
|
tty->column++;
|
tty->column++;
|
break;
|
break;
|
}
|
}
|
}
|
}
|
tty->driver.put_char(tty, c);
|
tty->driver.put_char(tty, c);
|
return 0;
|
return 0;
|
}
|
}
|
|
|
static inline void put_char(unsigned char c, struct tty_struct *tty)
|
static inline void put_char(unsigned char c, struct tty_struct *tty)
|
{
|
{
|
tty->driver.put_char(tty, c);
|
tty->driver.put_char(tty, c);
|
}
|
}
|
|
|
/* Must be called only when L_ECHO(tty) is true. */
|
/* Must be called only when L_ECHO(tty) is true. */
|
|
|
static void echo_char(unsigned char c, struct tty_struct *tty)
|
static void echo_char(unsigned char c, struct tty_struct *tty)
|
{
|
{
|
if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t') {
|
if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t') {
|
put_char('^', tty);
|
put_char('^', tty);
|
put_char(c ^ 0100, tty);
|
put_char(c ^ 0100, tty);
|
tty->column += 2;
|
tty->column += 2;
|
} else
|
} else
|
opost(c, tty);
|
opost(c, tty);
|
}
|
}
|
|
|
static inline void finish_erasing(struct tty_struct *tty)
|
static inline void finish_erasing(struct tty_struct *tty)
|
{
|
{
|
if (tty->erasing) {
|
if (tty->erasing) {
|
put_char('/', tty);
|
put_char('/', tty);
|
tty->column += 2;
|
tty->column += 2;
|
tty->erasing = 0;
|
tty->erasing = 0;
|
}
|
}
|
}
|
}
|
|
|
static void eraser(unsigned char c, struct tty_struct *tty)
|
static void eraser(unsigned char c, struct tty_struct *tty)
|
{
|
{
|
enum { ERASE, WERASE, KILL } kill_type;
|
enum { ERASE, WERASE, KILL } kill_type;
|
int head, seen_alnums;
|
int head, seen_alnums;
|
|
|
if (tty->read_head == tty->canon_head) {
|
if (tty->read_head == tty->canon_head) {
|
/* opost('\a', tty); */ /* what do you think? */
|
/* opost('\a', tty); */ /* what do you think? */
|
return;
|
return;
|
}
|
}
|
if (c == ERASE_CHAR(tty))
|
if (c == ERASE_CHAR(tty))
|
kill_type = ERASE;
|
kill_type = ERASE;
|
else if (c == WERASE_CHAR(tty))
|
else if (c == WERASE_CHAR(tty))
|
kill_type = WERASE;
|
kill_type = WERASE;
|
else {
|
else {
|
if (!L_ECHO(tty)) {
|
if (!L_ECHO(tty)) {
|
tty->read_cnt -= ((tty->read_head - tty->canon_head) &
|
tty->read_cnt -= ((tty->read_head - tty->canon_head) &
|
(N_TTY_BUF_SIZE - 1));
|
(N_TTY_BUF_SIZE - 1));
|
tty->read_head = tty->canon_head;
|
tty->read_head = tty->canon_head;
|
return;
|
return;
|
}
|
}
|
if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
|
if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
|
tty->read_cnt -= ((tty->read_head - tty->canon_head) &
|
tty->read_cnt -= ((tty->read_head - tty->canon_head) &
|
(N_TTY_BUF_SIZE - 1));
|
(N_TTY_BUF_SIZE - 1));
|
tty->read_head = tty->canon_head;
|
tty->read_head = tty->canon_head;
|
finish_erasing(tty);
|
finish_erasing(tty);
|
echo_char(KILL_CHAR(tty), tty);
|
echo_char(KILL_CHAR(tty), tty);
|
/* Add a newline if ECHOK is on and ECHOKE is off. */
|
/* Add a newline if ECHOK is on and ECHOKE is off. */
|
if (L_ECHOK(tty))
|
if (L_ECHOK(tty))
|
opost('\n', tty);
|
opost('\n', tty);
|
return;
|
return;
|
}
|
}
|
kill_type = KILL;
|
kill_type = KILL;
|
}
|
}
|
|
|
seen_alnums = 0;
|
seen_alnums = 0;
|
while (tty->read_head != tty->canon_head) {
|
while (tty->read_head != tty->canon_head) {
|
head = (tty->read_head - 1) & (N_TTY_BUF_SIZE-1);
|
head = (tty->read_head - 1) & (N_TTY_BUF_SIZE-1);
|
c = tty->read_buf[head];
|
c = tty->read_buf[head];
|
if (kill_type == WERASE) {
|
if (kill_type == WERASE) {
|
/* Equivalent to BSD's ALTWERASE. */
|
/* Equivalent to BSD's ALTWERASE. */
|
if (isalnum(c) || c == '_')
|
if (isalnum(c) || c == '_')
|
seen_alnums++;
|
seen_alnums++;
|
else if (seen_alnums)
|
else if (seen_alnums)
|
break;
|
break;
|
}
|
}
|
tty->read_head = head;
|
tty->read_head = head;
|
tty->read_cnt--;
|
tty->read_cnt--;
|
if (L_ECHO(tty)) {
|
if (L_ECHO(tty)) {
|
if (L_ECHOPRT(tty)) {
|
if (L_ECHOPRT(tty)) {
|
if (!tty->erasing) {
|
if (!tty->erasing) {
|
put_char('\\', tty);
|
put_char('\\', tty);
|
tty->column++;
|
tty->column++;
|
tty->erasing = 1;
|
tty->erasing = 1;
|
}
|
}
|
echo_char(c, tty);
|
echo_char(c, tty);
|
} else if (kill_type == ERASE && !L_ECHOE(tty)) {
|
} else if (kill_type == ERASE && !L_ECHOE(tty)) {
|
echo_char(ERASE_CHAR(tty), tty);
|
echo_char(ERASE_CHAR(tty), tty);
|
} else if (c == '\t') {
|
} else if (c == '\t') {
|
unsigned int col = tty->canon_column;
|
unsigned int col = tty->canon_column;
|
unsigned long tail = tty->canon_head;
|
unsigned long tail = tty->canon_head;
|
|
|
/* Find the column of the last char. */
|
/* Find the column of the last char. */
|
while (tail != tty->read_head) {
|
while (tail != tty->read_head) {
|
c = tty->read_buf[tail];
|
c = tty->read_buf[tail];
|
if (c == '\t')
|
if (c == '\t')
|
col = (col | 7) + 1;
|
col = (col | 7) + 1;
|
else if (iscntrl(c)) {
|
else if (iscntrl(c)) {
|
if (L_ECHOCTL(tty))
|
if (L_ECHOCTL(tty))
|
col += 2;
|
col += 2;
|
} else
|
} else
|
col++;
|
col++;
|
tail = (tail+1) & (N_TTY_BUF_SIZE-1);
|
tail = (tail+1) & (N_TTY_BUF_SIZE-1);
|
}
|
}
|
|
|
/* should never happen */
|
/* should never happen */
|
if (tty->column > 0x80000000)
|
if (tty->column > 0x80000000)
|
tty->column = 0;
|
tty->column = 0;
|
|
|
/* Now backup to that column. */
|
/* Now backup to that column. */
|
while (tty->column > col) {
|
while (tty->column > col) {
|
/* Can't use opost here. */
|
/* Can't use opost here. */
|
put_char('\b', tty);
|
put_char('\b', tty);
|
if (tty->column > 0)
|
if (tty->column > 0)
|
tty->column--;
|
tty->column--;
|
}
|
}
|
} else {
|
} else {
|
if (iscntrl(c) && L_ECHOCTL(tty)) {
|
if (iscntrl(c) && L_ECHOCTL(tty)) {
|
put_char('\b', tty);
|
put_char('\b', tty);
|
put_char(' ', tty);
|
put_char(' ', tty);
|
put_char('\b', tty);
|
put_char('\b', tty);
|
if (tty->column > 0)
|
if (tty->column > 0)
|
tty->column--;
|
tty->column--;
|
}
|
}
|
if (!iscntrl(c) || L_ECHOCTL(tty)) {
|
if (!iscntrl(c) || L_ECHOCTL(tty)) {
|
put_char('\b', tty);
|
put_char('\b', tty);
|
put_char(' ', tty);
|
put_char(' ', tty);
|
put_char('\b', tty);
|
put_char('\b', tty);
|
if (tty->column > 0)
|
if (tty->column > 0)
|
tty->column--;
|
tty->column--;
|
}
|
}
|
}
|
}
|
}
|
}
|
if (kill_type == ERASE)
|
if (kill_type == ERASE)
|
break;
|
break;
|
}
|
}
|
if (tty->read_head == tty->canon_head)
|
if (tty->read_head == tty->canon_head)
|
finish_erasing(tty);
|
finish_erasing(tty);
|
}
|
}
|
|
|
static inline void isig(int sig, struct tty_struct *tty, int flush)
|
static inline void isig(int sig, struct tty_struct *tty, int flush)
|
{
|
{
|
if (tty->pgrp > 0)
|
if (tty->pgrp > 0)
|
kill_pg(tty->pgrp, sig, 1);
|
kill_pg(tty->pgrp, sig, 1);
|
if (flush || !L_NOFLSH(tty)) {
|
if (flush || !L_NOFLSH(tty)) {
|
n_tty_flush_buffer(tty);
|
n_tty_flush_buffer(tty);
|
if (tty->driver.flush_buffer)
|
if (tty->driver.flush_buffer)
|
tty->driver.flush_buffer(tty);
|
tty->driver.flush_buffer(tty);
|
}
|
}
|
}
|
}
|
|
|
static inline void n_tty_receive_break(struct tty_struct *tty)
|
static inline void n_tty_receive_break(struct tty_struct *tty)
|
{
|
{
|
if (I_IGNBRK(tty))
|
if (I_IGNBRK(tty))
|
return;
|
return;
|
if (I_BRKINT(tty)) {
|
if (I_BRKINT(tty)) {
|
isig(SIGINT, tty, 1);
|
isig(SIGINT, tty, 1);
|
return;
|
return;
|
}
|
}
|
if (I_PARMRK(tty)) {
|
if (I_PARMRK(tty)) {
|
put_tty_queue('\377', tty);
|
put_tty_queue('\377', tty);
|
put_tty_queue('\0', tty);
|
put_tty_queue('\0', tty);
|
}
|
}
|
put_tty_queue('\0', tty);
|
put_tty_queue('\0', tty);
|
wake_up_interruptible(&tty->read_wait);
|
wake_up_interruptible(&tty->read_wait);
|
}
|
}
|
|
|
static inline void n_tty_receive_overrun(struct tty_struct *tty)
|
static inline void n_tty_receive_overrun(struct tty_struct *tty)
|
{
|
{
|
char buf[64];
|
char buf[64];
|
|
|
tty->num_overrun++;
|
tty->num_overrun++;
|
if (tty->overrun_time < (jiffies - HZ)) {
|
if (tty->overrun_time < (jiffies - HZ)) {
|
printk("%s: %d input overrun(s)\n", _tty_name(tty, buf),
|
printk("%s: %d input overrun(s)\n", _tty_name(tty, buf),
|
tty->num_overrun);
|
tty->num_overrun);
|
tty->overrun_time = jiffies;
|
tty->overrun_time = jiffies;
|
tty->num_overrun = 0;
|
tty->num_overrun = 0;
|
}
|
}
|
}
|
}
|
|
|
static inline void n_tty_receive_parity_error(struct tty_struct *tty,
|
static inline void n_tty_receive_parity_error(struct tty_struct *tty,
|
unsigned char c)
|
unsigned char c)
|
{
|
{
|
if (I_IGNPAR(tty)) {
|
if (I_IGNPAR(tty)) {
|
return;
|
return;
|
}
|
}
|
if (I_PARMRK(tty)) {
|
if (I_PARMRK(tty)) {
|
put_tty_queue('\377', tty);
|
put_tty_queue('\377', tty);
|
put_tty_queue('\0', tty);
|
put_tty_queue('\0', tty);
|
put_tty_queue(c, tty);
|
put_tty_queue(c, tty);
|
} else if (I_INPCK(tty))
|
} else if (I_INPCK(tty))
|
put_tty_queue('\0', tty);
|
put_tty_queue('\0', tty);
|
else
|
else
|
put_tty_queue(c, tty);
|
put_tty_queue(c, tty);
|
wake_up_interruptible(&tty->read_wait);
|
wake_up_interruptible(&tty->read_wait);
|
}
|
}
|
|
|
static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
|
static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
|
{
|
{
|
if (tty->raw) {
|
if (tty->raw) {
|
put_tty_queue(c, tty);
|
put_tty_queue(c, tty);
|
return;
|
return;
|
}
|
}
|
|
|
if (tty->stopped && I_IXON(tty) && I_IXANY(tty)) {
|
if (tty->stopped && I_IXON(tty) && I_IXANY(tty)) {
|
start_tty(tty);
|
start_tty(tty);
|
return;
|
return;
|
}
|
}
|
|
|
if (I_ISTRIP(tty))
|
if (I_ISTRIP(tty))
|
c &= 0x7f;
|
c &= 0x7f;
|
if (I_IUCLC(tty) && L_IEXTEN(tty))
|
if (I_IUCLC(tty) && L_IEXTEN(tty))
|
c=tolower(c);
|
c=tolower(c);
|
|
|
if (tty->closing) {
|
if (tty->closing) {
|
if (I_IXON(tty)) {
|
if (I_IXON(tty)) {
|
if (c == START_CHAR(tty))
|
if (c == START_CHAR(tty))
|
start_tty(tty);
|
start_tty(tty);
|
else if (c == STOP_CHAR(tty))
|
else if (c == STOP_CHAR(tty))
|
stop_tty(tty);
|
stop_tty(tty);
|
}
|
}
|
return;
|
return;
|
}
|
}
|
|
|
/*
|
/*
|
* If the previous character was LNEXT, or we know that this
|
* If the previous character was LNEXT, or we know that this
|
* character is not one of the characters that we'll have to
|
* character is not one of the characters that we'll have to
|
* handle specially, do shortcut processing to speed things
|
* handle specially, do shortcut processing to speed things
|
* up.
|
* up.
|
*/
|
*/
|
if (!test_bit(c, &tty->process_char_map) || tty->lnext) {
|
if (!test_bit(c, &tty->process_char_map) || tty->lnext) {
|
finish_erasing(tty);
|
finish_erasing(tty);
|
tty->lnext = 0;
|
tty->lnext = 0;
|
if (L_ECHO(tty)) {
|
if (L_ECHO(tty)) {
|
if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
|
if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
|
put_char('\a', tty); /* beep if no space */
|
put_char('\a', tty); /* beep if no space */
|
return;
|
return;
|
}
|
}
|
/* Record the column of first canon char. */
|
/* Record the column of first canon char. */
|
if (tty->canon_head == tty->read_head)
|
if (tty->canon_head == tty->read_head)
|
tty->canon_column = tty->column;
|
tty->canon_column = tty->column;
|
echo_char(c, tty);
|
echo_char(c, tty);
|
}
|
}
|
if (I_PARMRK(tty) && c == (unsigned char) '\377')
|
if (I_PARMRK(tty) && c == (unsigned char) '\377')
|
put_tty_queue(c, tty);
|
put_tty_queue(c, tty);
|
put_tty_queue(c, tty);
|
put_tty_queue(c, tty);
|
return;
|
return;
|
}
|
}
|
|
|
if (c == '\r') {
|
if (c == '\r') {
|
if (I_IGNCR(tty))
|
if (I_IGNCR(tty))
|
return;
|
return;
|
if (I_ICRNL(tty))
|
if (I_ICRNL(tty))
|
c = '\n';
|
c = '\n';
|
} else if (c == '\n' && I_INLCR(tty))
|
} else if (c == '\n' && I_INLCR(tty))
|
c = '\r';
|
c = '\r';
|
if (I_IXON(tty)) {
|
if (I_IXON(tty)) {
|
if (c == START_CHAR(tty)) {
|
if (c == START_CHAR(tty)) {
|
start_tty(tty);
|
start_tty(tty);
|
return;
|
return;
|
}
|
}
|
if (c == STOP_CHAR(tty)) {
|
if (c == STOP_CHAR(tty)) {
|
stop_tty(tty);
|
stop_tty(tty);
|
return;
|
return;
|
}
|
}
|
}
|
}
|
if (L_ISIG(tty)) {
|
if (L_ISIG(tty)) {
|
int signal;
|
int signal;
|
signal = SIGINT;
|
signal = SIGINT;
|
if (c == INTR_CHAR(tty))
|
if (c == INTR_CHAR(tty))
|
goto send_signal;
|
goto send_signal;
|
signal = SIGQUIT;
|
signal = SIGQUIT;
|
if (c == QUIT_CHAR(tty))
|
if (c == QUIT_CHAR(tty))
|
goto send_signal;
|
goto send_signal;
|
signal = SIGTSTP;
|
signal = SIGTSTP;
|
if (c == SUSP_CHAR(tty)) {
|
if (c == SUSP_CHAR(tty)) {
|
send_signal:
|
send_signal:
|
isig(signal, tty, 0);
|
isig(signal, tty, 0);
|
return;
|
return;
|
}
|
}
|
}
|
}
|
if (L_ICANON(tty)) {
|
if (L_ICANON(tty)) {
|
if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
|
if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
|
(c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
|
(c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
|
eraser(c, tty);
|
eraser(c, tty);
|
return;
|
return;
|
}
|
}
|
if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
|
if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
|
tty->lnext = 1;
|
tty->lnext = 1;
|
if (L_ECHO(tty)) {
|
if (L_ECHO(tty)) {
|
finish_erasing(tty);
|
finish_erasing(tty);
|
if (L_ECHOCTL(tty)) {
|
if (L_ECHOCTL(tty)) {
|
put_char('^', tty);
|
put_char('^', tty);
|
put_char('\b', tty);
|
put_char('\b', tty);
|
}
|
}
|
}
|
}
|
return;
|
return;
|
}
|
}
|
if (c == REPRINT_CHAR(tty) && L_ECHO(tty) &&
|
if (c == REPRINT_CHAR(tty) && L_ECHO(tty) &&
|
L_IEXTEN(tty)) {
|
L_IEXTEN(tty)) {
|
unsigned long tail = tty->canon_head;
|
unsigned long tail = tty->canon_head;
|
|
|
finish_erasing(tty);
|
finish_erasing(tty);
|
echo_char(c, tty);
|
echo_char(c, tty);
|
opost('\n', tty);
|
opost('\n', tty);
|
while (tail != tty->read_head) {
|
while (tail != tty->read_head) {
|
echo_char(tty->read_buf[tail], tty);
|
echo_char(tty->read_buf[tail], tty);
|
tail = (tail+1) & (N_TTY_BUF_SIZE-1);
|
tail = (tail+1) & (N_TTY_BUF_SIZE-1);
|
}
|
}
|
return;
|
return;
|
}
|
}
|
if (c == '\n') {
|
if (c == '\n') {
|
if (L_ECHO(tty) || L_ECHONL(tty)) {
|
if (L_ECHO(tty) || L_ECHONL(tty)) {
|
if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
|
if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
|
put_char('\a', tty);
|
put_char('\a', tty);
|
return;
|
return;
|
}
|
}
|
opost('\n', tty);
|
opost('\n', tty);
|
}
|
}
|
goto handle_newline;
|
goto handle_newline;
|
}
|
}
|
if (c == EOF_CHAR(tty)) {
|
if (c == EOF_CHAR(tty)) {
|
if (tty->canon_head != tty->read_head)
|
if (tty->canon_head != tty->read_head)
|
set_bit(TTY_PUSH, &tty->flags);
|
set_bit(TTY_PUSH, &tty->flags);
|
c = __DISABLED_CHAR;
|
c = __DISABLED_CHAR;
|
goto handle_newline;
|
goto handle_newline;
|
}
|
}
|
if ((c == EOL_CHAR(tty)) ||
|
if ((c == EOL_CHAR(tty)) ||
|
(c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
|
(c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
|
/*
|
/*
|
* XXX are EOL_CHAR and EOL2_CHAR echoed?!?
|
* XXX are EOL_CHAR and EOL2_CHAR echoed?!?
|
*/
|
*/
|
if (L_ECHO(tty)) {
|
if (L_ECHO(tty)) {
|
if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
|
if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
|
put_char('\a', tty);
|
put_char('\a', tty);
|
return;
|
return;
|
}
|
}
|
/* Record the column of first canon char. */
|
/* Record the column of first canon char. */
|
if (tty->canon_head == tty->read_head)
|
if (tty->canon_head == tty->read_head)
|
tty->canon_column = tty->column;
|
tty->canon_column = tty->column;
|
echo_char(c, tty);
|
echo_char(c, tty);
|
}
|
}
|
/*
|
/*
|
* XXX does PARMRK doubling happen for
|
* XXX does PARMRK doubling happen for
|
* EOL_CHAR and EOL2_CHAR?
|
* EOL_CHAR and EOL2_CHAR?
|
*/
|
*/
|
if (I_PARMRK(tty) && c == (unsigned char) '\377')
|
if (I_PARMRK(tty) && c == (unsigned char) '\377')
|
put_tty_queue(c, tty);
|
put_tty_queue(c, tty);
|
|
|
handle_newline:
|
handle_newline:
|
set_bit(tty->read_head, &tty->read_flags);
|
set_bit(tty->read_head, &tty->read_flags);
|
put_tty_queue(c, tty);
|
put_tty_queue(c, tty);
|
tty->canon_head = tty->read_head;
|
tty->canon_head = tty->read_head;
|
tty->canon_data++;
|
tty->canon_data++;
|
if (tty->fasync)
|
if (tty->fasync)
|
kill_fasync(tty->fasync, SIGIO);
|
kill_fasync(tty->fasync, SIGIO);
|
if (tty->read_wait)
|
if (tty->read_wait)
|
wake_up_interruptible(&tty->read_wait);
|
wake_up_interruptible(&tty->read_wait);
|
return;
|
return;
|
}
|
}
|
}
|
}
|
|
|
finish_erasing(tty);
|
finish_erasing(tty);
|
if (L_ECHO(tty)) {
|
if (L_ECHO(tty)) {
|
if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
|
if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
|
put_char('\a', tty); /* beep if no space */
|
put_char('\a', tty); /* beep if no space */
|
return;
|
return;
|
}
|
}
|
if (c == '\n')
|
if (c == '\n')
|
opost('\n', tty);
|
opost('\n', tty);
|
else {
|
else {
|
/* Record the column of first canon char. */
|
/* Record the column of first canon char. */
|
if (tty->canon_head == tty->read_head)
|
if (tty->canon_head == tty->read_head)
|
tty->canon_column = tty->column;
|
tty->canon_column = tty->column;
|
echo_char(c, tty);
|
echo_char(c, tty);
|
}
|
}
|
}
|
}
|
|
|
if (I_PARMRK(tty) && c == (unsigned char) '\377')
|
if (I_PARMRK(tty) && c == (unsigned char) '\377')
|
put_tty_queue(c, tty);
|
put_tty_queue(c, tty);
|
|
|
put_tty_queue(c, tty);
|
put_tty_queue(c, tty);
|
}
|
}
|
|
|
static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
|
static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
|
char *fp, int count)
|
char *fp, int count)
|
{
|
{
|
const unsigned char *p;
|
const unsigned char *p;
|
char *f, flags = 0;
|
char *f, flags = 0;
|
int i;
|
int i;
|
|
|
if (!tty->read_buf)
|
if (!tty->read_buf)
|
return;
|
return;
|
|
|
if (tty->real_raw) {
|
if (tty->real_raw) {
|
i = MIN(count, MIN(N_TTY_BUF_SIZE - tty->read_cnt,
|
i = MIN(count, MIN(N_TTY_BUF_SIZE - tty->read_cnt,
|
N_TTY_BUF_SIZE - tty->read_head));
|
N_TTY_BUF_SIZE - tty->read_head));
|
memcpy(tty->read_buf + tty->read_head, cp, i);
|
memcpy(tty->read_buf + tty->read_head, cp, i);
|
tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
|
tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
|
tty->read_cnt += i;
|
tty->read_cnt += i;
|
cp += i;
|
cp += i;
|
count -= i;
|
count -= i;
|
|
|
i = MIN(count, MIN(N_TTY_BUF_SIZE - tty->read_cnt,
|
i = MIN(count, MIN(N_TTY_BUF_SIZE - tty->read_cnt,
|
N_TTY_BUF_SIZE - tty->read_head));
|
N_TTY_BUF_SIZE - tty->read_head));
|
memcpy(tty->read_buf + tty->read_head, cp, i);
|
memcpy(tty->read_buf + tty->read_head, cp, i);
|
tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
|
tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
|
tty->read_cnt += i;
|
tty->read_cnt += i;
|
} else {
|
} else {
|
for (i=count, p = cp, f = fp; i; i--, p++) {
|
for (i=count, p = cp, f = fp; i; i--, p++) {
|
if (f)
|
if (f)
|
flags = *f++;
|
flags = *f++;
|
switch (flags) {
|
switch (flags) {
|
case TTY_NORMAL:
|
case TTY_NORMAL:
|
n_tty_receive_char(tty, *p);
|
n_tty_receive_char(tty, *p);
|
break;
|
break;
|
case TTY_BREAK:
|
case TTY_BREAK:
|
n_tty_receive_break(tty);
|
n_tty_receive_break(tty);
|
break;
|
break;
|
case TTY_PARITY:
|
case TTY_PARITY:
|
case TTY_FRAME:
|
case TTY_FRAME:
|
n_tty_receive_parity_error(tty, *p);
|
n_tty_receive_parity_error(tty, *p);
|
break;
|
break;
|
case TTY_OVERRUN:
|
case TTY_OVERRUN:
|
n_tty_receive_overrun(tty);
|
n_tty_receive_overrun(tty);
|
break;
|
break;
|
default:
|
default:
|
printk("%s: unknown flag %d\n", tty_name(tty),
|
printk("%s: unknown flag %d\n", tty_name(tty),
|
flags);
|
flags);
|
break;
|
break;
|
}
|
}
|
}
|
}
|
if (tty->driver.flush_chars)
|
if (tty->driver.flush_chars)
|
tty->driver.flush_chars(tty);
|
tty->driver.flush_chars(tty);
|
}
|
}
|
|
|
if (!tty->icanon && (tty->read_cnt >= tty->minimum_to_wake)) {
|
if (!tty->icanon && (tty->read_cnt >= tty->minimum_to_wake)) {
|
if (tty->fasync)
|
if (tty->fasync)
|
kill_fasync(tty->fasync, SIGIO);
|
kill_fasync(tty->fasync, SIGIO);
|
if (tty->read_wait)
|
if (tty->read_wait)
|
wake_up_interruptible(&tty->read_wait);
|
wake_up_interruptible(&tty->read_wait);
|
}
|
}
|
|
|
if ((tty->read_cnt >= TTY_THRESHOLD_THROTTLE) &&
|
if ((tty->read_cnt >= TTY_THRESHOLD_THROTTLE) &&
|
tty->driver.throttle &&
|
tty->driver.throttle &&
|
!set_bit(TTY_THROTTLED, &tty->flags))
|
!set_bit(TTY_THROTTLED, &tty->flags))
|
tty->driver.throttle(tty);
|
tty->driver.throttle(tty);
|
}
|
}
|
|
|
static int n_tty_receive_room(struct tty_struct *tty)
|
static int n_tty_receive_room(struct tty_struct *tty)
|
{
|
{
|
int left = N_TTY_BUF_SIZE - tty->read_cnt - 1;
|
int left = N_TTY_BUF_SIZE - tty->read_cnt - 1;
|
|
|
/*
|
/*
|
* If we are doing input canonicalization, and there are no
|
* If we are doing input canonicalization, and there are no
|
* pending newlines, let characters through without limit, so
|
* pending newlines, let characters through without limit, so
|
* that erase characters will be handled. Other excess
|
* that erase characters will be handled. Other excess
|
* characters will be beeped.
|
* characters will be beeped.
|
*/
|
*/
|
if (tty->icanon && !tty->canon_data)
|
if (tty->icanon && !tty->canon_data)
|
return N_TTY_BUF_SIZE;
|
return N_TTY_BUF_SIZE;
|
|
|
if (left > 0)
|
if (left > 0)
|
return left;
|
return left;
|
return 0;
|
return 0;
|
}
|
}
|
|
|
int is_ignored(int sig)
|
int is_ignored(int sig)
|
{
|
{
|
return ((current->blocked & (1<<(sig-1))) ||
|
return ((current->blocked & (1<<(sig-1))) ||
|
(current->sig->action[sig-1].sa_handler == SIG_IGN));
|
(current->sig->action[sig-1].sa_handler == SIG_IGN));
|
}
|
}
|
|
|
static void n_tty_set_termios(struct tty_struct *tty, struct termios * old)
|
static void n_tty_set_termios(struct tty_struct *tty, struct termios * old)
|
{
|
{
|
if (!tty)
|
if (!tty)
|
return;
|
return;
|
|
|
tty->icanon = (L_ICANON(tty) != 0);
|
tty->icanon = (L_ICANON(tty) != 0);
|
if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
|
if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
|
I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
|
I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
|
I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
|
I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
|
I_PARMRK(tty)) {
|
I_PARMRK(tty)) {
|
cli();
|
cli();
|
memset(tty->process_char_map, 0, 256/8);
|
memset(tty->process_char_map, 0, 256/8);
|
|
|
if (I_IGNCR(tty) || I_ICRNL(tty))
|
if (I_IGNCR(tty) || I_ICRNL(tty))
|
set_bit('\r', &tty->process_char_map);
|
set_bit('\r', &tty->process_char_map);
|
if (I_INLCR(tty))
|
if (I_INLCR(tty))
|
set_bit('\n', &tty->process_char_map);
|
set_bit('\n', &tty->process_char_map);
|
|
|
if (L_ICANON(tty)) {
|
if (L_ICANON(tty)) {
|
set_bit(ERASE_CHAR(tty), &tty->process_char_map);
|
set_bit(ERASE_CHAR(tty), &tty->process_char_map);
|
set_bit(KILL_CHAR(tty), &tty->process_char_map);
|
set_bit(KILL_CHAR(tty), &tty->process_char_map);
|
set_bit(EOF_CHAR(tty), &tty->process_char_map);
|
set_bit(EOF_CHAR(tty), &tty->process_char_map);
|
set_bit('\n', &tty->process_char_map);
|
set_bit('\n', &tty->process_char_map);
|
set_bit(EOL_CHAR(tty), &tty->process_char_map);
|
set_bit(EOL_CHAR(tty), &tty->process_char_map);
|
if (L_IEXTEN(tty)) {
|
if (L_IEXTEN(tty)) {
|
set_bit(WERASE_CHAR(tty),
|
set_bit(WERASE_CHAR(tty),
|
&tty->process_char_map);
|
&tty->process_char_map);
|
set_bit(LNEXT_CHAR(tty),
|
set_bit(LNEXT_CHAR(tty),
|
&tty->process_char_map);
|
&tty->process_char_map);
|
set_bit(EOL2_CHAR(tty),
|
set_bit(EOL2_CHAR(tty),
|
&tty->process_char_map);
|
&tty->process_char_map);
|
if (L_ECHO(tty))
|
if (L_ECHO(tty))
|
set_bit(REPRINT_CHAR(tty),
|
set_bit(REPRINT_CHAR(tty),
|
&tty->process_char_map);
|
&tty->process_char_map);
|
}
|
}
|
}
|
}
|
if (I_IXON(tty)) {
|
if (I_IXON(tty)) {
|
set_bit(START_CHAR(tty), &tty->process_char_map);
|
set_bit(START_CHAR(tty), &tty->process_char_map);
|
set_bit(STOP_CHAR(tty), &tty->process_char_map);
|
set_bit(STOP_CHAR(tty), &tty->process_char_map);
|
}
|
}
|
if (L_ISIG(tty)) {
|
if (L_ISIG(tty)) {
|
set_bit(INTR_CHAR(tty), &tty->process_char_map);
|
set_bit(INTR_CHAR(tty), &tty->process_char_map);
|
set_bit(QUIT_CHAR(tty), &tty->process_char_map);
|
set_bit(QUIT_CHAR(tty), &tty->process_char_map);
|
set_bit(SUSP_CHAR(tty), &tty->process_char_map);
|
set_bit(SUSP_CHAR(tty), &tty->process_char_map);
|
}
|
}
|
clear_bit(__DISABLED_CHAR, &tty->process_char_map);
|
clear_bit(__DISABLED_CHAR, &tty->process_char_map);
|
sti();
|
sti();
|
tty->raw = 0;
|
tty->raw = 0;
|
tty->real_raw = 0;
|
tty->real_raw = 0;
|
} else {
|
} else {
|
tty->raw = 1;
|
tty->raw = 1;
|
if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
|
if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
|
(I_IGNPAR(tty) || !I_INPCK(tty)) &&
|
(I_IGNPAR(tty) || !I_INPCK(tty)) &&
|
(tty->driver.flags & TTY_DRIVER_REAL_RAW))
|
(tty->driver.flags & TTY_DRIVER_REAL_RAW))
|
tty->real_raw = 1;
|
tty->real_raw = 1;
|
else
|
else
|
tty->real_raw = 0;
|
tty->real_raw = 0;
|
}
|
}
|
}
|
}
|
|
|
static void n_tty_close(struct tty_struct *tty)
|
static void n_tty_close(struct tty_struct *tty)
|
{
|
{
|
n_tty_flush_buffer(tty);
|
n_tty_flush_buffer(tty);
|
if (tty->read_buf) {
|
if (tty->read_buf) {
|
kfree_s (tty->read_buf, N_TTY_BUF_SIZE);
|
kfree_s (tty->read_buf, N_TTY_BUF_SIZE);
|
tty->read_buf = 0;
|
tty->read_buf = 0;
|
}
|
}
|
}
|
}
|
|
|
static int n_tty_open(struct tty_struct *tty)
|
static int n_tty_open(struct tty_struct *tty)
|
{
|
{
|
if (!tty)
|
if (!tty)
|
return -EINVAL;
|
return -EINVAL;
|
|
|
if (!tty->read_buf) {
|
if (!tty->read_buf) {
|
tty->read_buf = (unsigned char *)
|
tty->read_buf = (unsigned char *)
|
kmalloc (N_TTY_BUF_SIZE, intr_count ? GFP_ATOMIC : GFP_KERNEL);
|
kmalloc (N_TTY_BUF_SIZE, intr_count ? GFP_ATOMIC : GFP_KERNEL);
|
if (!tty->read_buf)
|
if (!tty->read_buf)
|
return -ENOMEM;
|
return -ENOMEM;
|
}
|
}
|
memset(tty->read_buf, 0, N_TTY_BUF_SIZE);
|
memset(tty->read_buf, 0, N_TTY_BUF_SIZE);
|
tty->read_head = tty->read_tail = tty->read_cnt = 0;
|
tty->read_head = tty->read_tail = tty->read_cnt = 0;
|
tty->canon_head = tty->canon_data = tty->erasing = 0;
|
tty->canon_head = tty->canon_data = tty->erasing = 0;
|
tty->column = 0;
|
tty->column = 0;
|
memset(tty->read_flags, 0, sizeof(tty->read_flags));
|
memset(tty->read_flags, 0, sizeof(tty->read_flags));
|
n_tty_set_termios(tty, 0);
|
n_tty_set_termios(tty, 0);
|
tty->minimum_to_wake = 1;
|
tty->minimum_to_wake = 1;
|
tty->closing = 0;
|
tty->closing = 0;
|
return 0;
|
return 0;
|
}
|
}
|
|
|
static inline int input_available_p(struct tty_struct *tty, int amt)
|
static inline int input_available_p(struct tty_struct *tty, int amt)
|
{
|
{
|
if (L_ICANON(tty)) {
|
if (L_ICANON(tty)) {
|
if (tty->canon_data)
|
if (tty->canon_data)
|
return 1;
|
return 1;
|
} else if (tty->read_cnt >= (amt ? amt : 1))
|
} else if (tty->read_cnt >= (amt ? amt : 1))
|
return 1;
|
return 1;
|
|
|
return 0;
|
return 0;
|
}
|
}
|
|
|
/*
|
/*
|
* Helper function to speed up read_chan. It is only called when
|
* Helper function to speed up read_chan. It is only called when
|
* ICANON is off; it copies characters straight from the tty queue to
|
* ICANON is off; it copies characters straight from the tty queue to
|
* user space directly. It can be profitably called twice; once to
|
* user space directly. It can be profitably called twice; once to
|
* drain the space from the tail pointer to the (physical) end of the
|
* drain the space from the tail pointer to the (physical) end of the
|
* buffer, and once to drain the space from the (physical) beginning of
|
* buffer, and once to drain the space from the (physical) beginning of
|
* the buffer to head pointer.
|
* the buffer to head pointer.
|
*/
|
*/
|
static inline void copy_from_read_buf(struct tty_struct *tty,
|
static inline void copy_from_read_buf(struct tty_struct *tty,
|
unsigned char **b,
|
unsigned char **b,
|
unsigned int *nr)
|
unsigned int *nr)
|
|
|
{
|
{
|
int n;
|
int n;
|
|
|
n = MIN(*nr, MIN(tty->read_cnt, N_TTY_BUF_SIZE - tty->read_tail));
|
n = MIN(*nr, MIN(tty->read_cnt, N_TTY_BUF_SIZE - tty->read_tail));
|
if (!n)
|
if (!n)
|
return;
|
return;
|
memcpy_tofs(*b, &tty->read_buf[tty->read_tail], n);
|
memcpy_tofs(*b, &tty->read_buf[tty->read_tail], n);
|
tty->read_tail = (tty->read_tail + n) & (N_TTY_BUF_SIZE-1);
|
tty->read_tail = (tty->read_tail + n) & (N_TTY_BUF_SIZE-1);
|
tty->read_cnt -= n;
|
tty->read_cnt -= n;
|
*b += n;
|
*b += n;
|
*nr -= n;
|
*nr -= n;
|
}
|
}
|
|
|
static int read_chan(struct tty_struct *tty, struct file *file,
|
static int read_chan(struct tty_struct *tty, struct file *file,
|
unsigned char *buf, unsigned int nr)
|
unsigned char *buf, unsigned int nr)
|
{
|
{
|
struct wait_queue wait = { current, NULL };
|
struct wait_queue wait = { current, NULL };
|
int c;
|
int c;
|
unsigned char *b = buf;
|
unsigned char *b = buf;
|
int minimum, time;
|
int minimum, time;
|
int retval = 0;
|
int retval = 0;
|
int size;
|
int size;
|
|
|
do_it_again:
|
do_it_again:
|
|
|
if (!tty->read_buf) {
|
if (!tty->read_buf) {
|
printk("n_tty_read_chan: called with read_buf == NULL?!?\n");
|
printk("n_tty_read_chan: called with read_buf == NULL?!?\n");
|
return -EIO;
|
return -EIO;
|
}
|
}
|
|
|
/* Job control check -- must be done at start and after
|
/* Job control check -- must be done at start and after
|
every sleep (POSIX.1 7.1.1.4). */
|
every sleep (POSIX.1 7.1.1.4). */
|
/* NOTE: not yet done after every sleep pending a thorough
|
/* NOTE: not yet done after every sleep pending a thorough
|
check of the logic of this change. -- jlc */
|
check of the logic of this change. -- jlc */
|
/* don't stop on /dev/console */
|
/* don't stop on /dev/console */
|
if (file->f_inode->i_rdev != CONSOLE_DEV &&
|
if (file->f_inode->i_rdev != CONSOLE_DEV &&
|
current->tty == tty) {
|
current->tty == tty) {
|
if (tty->pgrp <= 0)
|
if (tty->pgrp <= 0)
|
printk("read_chan: tty->pgrp <= 0!\n");
|
printk("read_chan: tty->pgrp <= 0!\n");
|
else if (current->pgrp != tty->pgrp) {
|
else if (current->pgrp != tty->pgrp) {
|
if (is_ignored(SIGTTIN) ||
|
if (is_ignored(SIGTTIN) ||
|
is_orphaned_pgrp(current->pgrp))
|
is_orphaned_pgrp(current->pgrp))
|
return -EIO;
|
return -EIO;
|
kill_pg(current->pgrp, SIGTTIN, 1);
|
kill_pg(current->pgrp, SIGTTIN, 1);
|
return -ERESTARTSYS;
|
return -ERESTARTSYS;
|
}
|
}
|
}
|
}
|
|
|
if (L_ICANON(tty)) {
|
if (L_ICANON(tty)) {
|
minimum = time = 0;
|
minimum = time = 0;
|
current->timeout = (unsigned long) -1;
|
current->timeout = (unsigned long) -1;
|
} else {
|
} else {
|
time = (HZ / 10) * TIME_CHAR(tty);
|
time = (HZ / 10) * TIME_CHAR(tty);
|
minimum = MIN_CHAR(tty);
|
minimum = MIN_CHAR(tty);
|
if (minimum) {
|
if (minimum) {
|
current->timeout = (unsigned long) -1;
|
current->timeout = (unsigned long) -1;
|
if (time)
|
if (time)
|
tty->minimum_to_wake = 1;
|
tty->minimum_to_wake = 1;
|
else if (!waitqueue_active(&tty->read_wait) ||
|
else if (!waitqueue_active(&tty->read_wait) ||
|
(tty->minimum_to_wake > minimum))
|
(tty->minimum_to_wake > minimum))
|
tty->minimum_to_wake = minimum;
|
tty->minimum_to_wake = minimum;
|
} else {
|
} else {
|
if (time) {
|
if (time) {
|
current->timeout = time + jiffies;
|
current->timeout = time + jiffies;
|
time = 0;
|
time = 0;
|
} else
|
} else
|
current->timeout = 0;
|
current->timeout = 0;
|
tty->minimum_to_wake = minimum = 1;
|
tty->minimum_to_wake = minimum = 1;
|
}
|
}
|
}
|
}
|
|
|
add_wait_queue(&tty->read_wait, &wait);
|
add_wait_queue(&tty->read_wait, &wait);
|
while (1) {
|
while (1) {
|
/* First test for status change. */
|
/* First test for status change. */
|
if (tty->packet && tty->link->ctrl_status) {
|
if (tty->packet && tty->link->ctrl_status) {
|
if (b != buf)
|
if (b != buf)
|
break;
|
break;
|
put_user(tty->link->ctrl_status, b++);
|
put_user(tty->link->ctrl_status, b++);
|
tty->link->ctrl_status = 0;
|
tty->link->ctrl_status = 0;
|
break;
|
break;
|
}
|
}
|
/* This statement must be first before checking for input
|
/* This statement must be first before checking for input
|
so that any interrupt will set the state back to
|
so that any interrupt will set the state back to
|
TASK_RUNNING. */
|
TASK_RUNNING. */
|
current->state = TASK_INTERRUPTIBLE;
|
current->state = TASK_INTERRUPTIBLE;
|
|
|
if (((minimum - (b - buf)) < tty->minimum_to_wake) &&
|
if (((minimum - (b - buf)) < tty->minimum_to_wake) &&
|
((minimum - (b - buf)) >= 1))
|
((minimum - (b - buf)) >= 1))
|
tty->minimum_to_wake = (minimum - (b - buf));
|
tty->minimum_to_wake = (minimum - (b - buf));
|
|
|
if (!input_available_p(tty, 0)) {
|
if (!input_available_p(tty, 0)) {
|
if (tty->flags & (1 << TTY_OTHER_CLOSED)) {
|
if (tty->flags & (1 << TTY_OTHER_CLOSED)) {
|
retval = -EIO;
|
retval = -EIO;
|
break;
|
break;
|
}
|
}
|
if (tty_hung_up_p(file))
|
if (tty_hung_up_p(file))
|
break;
|
break;
|
if (!current->timeout)
|
if (!current->timeout)
|
break;
|
break;
|
if (file->f_flags & O_NONBLOCK) {
|
if (file->f_flags & O_NONBLOCK) {
|
retval = -EAGAIN;
|
retval = -EAGAIN;
|
break;
|
break;
|
}
|
}
|
if (current->signal & ~current->blocked) {
|
if (current->signal & ~current->blocked) {
|
retval = -ERESTARTSYS;
|
retval = -ERESTARTSYS;
|
break;
|
break;
|
}
|
}
|
schedule();
|
schedule();
|
continue;
|
continue;
|
}
|
}
|
current->state = TASK_RUNNING;
|
current->state = TASK_RUNNING;
|
|
|
/* Deal with packet mode. */
|
/* Deal with packet mode. */
|
if (tty->packet && b == buf) {
|
if (tty->packet && b == buf) {
|
put_user(TIOCPKT_DATA, b++);
|
put_user(TIOCPKT_DATA, b++);
|
nr--;
|
nr--;
|
}
|
}
|
|
|
if (L_ICANON(tty)) {
|
if (L_ICANON(tty)) {
|
while (1) {
|
while (1) {
|
int eol;
|
int eol;
|
|
|
disable_bh(TQUEUE_BH);
|
disable_bh(TQUEUE_BH);
|
if (!tty->read_cnt) {
|
if (!tty->read_cnt) {
|
enable_bh(TQUEUE_BH);
|
enable_bh(TQUEUE_BH);
|
break;
|
break;
|
}
|
}
|
eol = clear_bit(tty->read_tail,
|
eol = clear_bit(tty->read_tail,
|
&tty->read_flags);
|
&tty->read_flags);
|
c = tty->read_buf[tty->read_tail];
|
c = tty->read_buf[tty->read_tail];
|
tty->read_tail = ((tty->read_tail+1) &
|
tty->read_tail = ((tty->read_tail+1) &
|
(N_TTY_BUF_SIZE-1));
|
(N_TTY_BUF_SIZE-1));
|
tty->read_cnt--;
|
tty->read_cnt--;
|
enable_bh(TQUEUE_BH);
|
enable_bh(TQUEUE_BH);
|
if (!eol) {
|
if (!eol) {
|
put_user(c, b++);
|
put_user(c, b++);
|
if (--nr)
|
if (--nr)
|
continue;
|
continue;
|
break;
|
break;
|
}
|
}
|
if (--tty->canon_data < 0) {
|
if (--tty->canon_data < 0) {
|
tty->canon_data = 0;
|
tty->canon_data = 0;
|
}
|
}
|
if (c != __DISABLED_CHAR) {
|
if (c != __DISABLED_CHAR) {
|
put_user(c, b++);
|
put_user(c, b++);
|
nr--;
|
nr--;
|
}
|
}
|
break;
|
break;
|
}
|
}
|
} else {
|
} else {
|
disable_bh(TQUEUE_BH);
|
disable_bh(TQUEUE_BH);
|
copy_from_read_buf(tty, &b, &nr);
|
copy_from_read_buf(tty, &b, &nr);
|
copy_from_read_buf(tty, &b, &nr);
|
copy_from_read_buf(tty, &b, &nr);
|
enable_bh(TQUEUE_BH);
|
enable_bh(TQUEUE_BH);
|
}
|
}
|
|
|
/* If there is enough space in the read buffer now, let the
|
/* If there is enough space in the read buffer now, let the
|
low-level driver know. */
|
low-level driver know. */
|
if (tty->driver.unthrottle &&
|
if (tty->driver.unthrottle &&
|
(tty->read_cnt <= TTY_THRESHOLD_UNTHROTTLE)
|
(tty->read_cnt <= TTY_THRESHOLD_UNTHROTTLE)
|
&& clear_bit(TTY_THROTTLED, &tty->flags))
|
&& clear_bit(TTY_THROTTLED, &tty->flags))
|
tty->driver.unthrottle(tty);
|
tty->driver.unthrottle(tty);
|
|
|
if (b - buf >= minimum || !nr)
|
if (b - buf >= minimum || !nr)
|
break;
|
break;
|
if (time)
|
if (time)
|
current->timeout = time + jiffies;
|
current->timeout = time + jiffies;
|
}
|
}
|
remove_wait_queue(&tty->read_wait, &wait);
|
remove_wait_queue(&tty->read_wait, &wait);
|
|
|
if (!waitqueue_active(&tty->read_wait))
|
if (!waitqueue_active(&tty->read_wait))
|
tty->minimum_to_wake = minimum;
|
tty->minimum_to_wake = minimum;
|
|
|
current->state = TASK_RUNNING;
|
current->state = TASK_RUNNING;
|
current->timeout = 0;
|
current->timeout = 0;
|
size = b - buf;
|
size = b - buf;
|
if (size && nr)
|
if (size && nr)
|
clear_bit(TTY_PUSH, &tty->flags);
|
clear_bit(TTY_PUSH, &tty->flags);
|
if (!size && clear_bit(TTY_PUSH, &tty->flags))
|
if (!size && clear_bit(TTY_PUSH, &tty->flags))
|
goto do_it_again;
|
goto do_it_again;
|
if (!size && !retval)
|
if (!size && !retval)
|
clear_bit(TTY_PUSH, &tty->flags);
|
clear_bit(TTY_PUSH, &tty->flags);
|
return (size ? size : retval);
|
return (size ? size : retval);
|
}
|
}
|
|
|
static int write_chan(struct tty_struct * tty, struct file * file,
|
static int write_chan(struct tty_struct * tty, struct file * file,
|
const unsigned char * buf, unsigned int nr)
|
const unsigned char * buf, unsigned int nr)
|
{
|
{
|
struct wait_queue wait = { current, NULL };
|
struct wait_queue wait = { current, NULL };
|
int c;
|
int c;
|
const unsigned char *b = buf;
|
const unsigned char *b = buf;
|
int retval = 0;
|
int retval = 0;
|
|
|
/* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
|
/* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
|
if (L_TOSTOP(tty) && file->f_inode->i_rdev != CONSOLE_DEV) {
|
if (L_TOSTOP(tty) && file->f_inode->i_rdev != CONSOLE_DEV) {
|
retval = tty_check_change(tty);
|
retval = tty_check_change(tty);
|
if (retval)
|
if (retval)
|
return retval;
|
return retval;
|
}
|
}
|
|
|
add_wait_queue(&tty->write_wait, &wait);
|
add_wait_queue(&tty->write_wait, &wait);
|
while (1) {
|
while (1) {
|
current->state = TASK_INTERRUPTIBLE;
|
current->state = TASK_INTERRUPTIBLE;
|
if (current->signal & ~current->blocked) {
|
if (current->signal & ~current->blocked) {
|
retval = -ERESTARTSYS;
|
retval = -ERESTARTSYS;
|
break;
|
break;
|
}
|
}
|
if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
|
if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
|
retval = -EIO;
|
retval = -EIO;
|
break;
|
break;
|
}
|
}
|
if (O_OPOST(tty)) {
|
if (O_OPOST(tty)) {
|
while (nr > 0) {
|
while (nr > 0) {
|
c = get_user(b);
|
c = get_user(b);
|
if (opost(c, tty) < 0)
|
if (opost(c, tty) < 0)
|
break;
|
break;
|
b++; nr--;
|
b++; nr--;
|
}
|
}
|
if (tty->driver.flush_chars)
|
if (tty->driver.flush_chars)
|
tty->driver.flush_chars(tty);
|
tty->driver.flush_chars(tty);
|
} else {
|
} else {
|
c = tty->driver.write(tty, 1, b, nr);
|
c = tty->driver.write(tty, 1, b, nr);
|
b += c;
|
b += c;
|
nr -= c;
|
nr -= c;
|
}
|
}
|
if (!nr)
|
if (!nr)
|
break;
|
break;
|
if (file->f_flags & O_NONBLOCK) {
|
if (file->f_flags & O_NONBLOCK) {
|
retval = -EAGAIN;
|
retval = -EAGAIN;
|
break;
|
break;
|
}
|
}
|
schedule();
|
schedule();
|
}
|
}
|
current->state = TASK_RUNNING;
|
current->state = TASK_RUNNING;
|
remove_wait_queue(&tty->write_wait, &wait);
|
remove_wait_queue(&tty->write_wait, &wait);
|
return (b - buf) ? b - buf : retval;
|
return (b - buf) ? b - buf : retval;
|
}
|
}
|
|
|
static int normal_select(struct tty_struct * tty, struct inode * inode,
|
static int normal_select(struct tty_struct * tty, struct inode * inode,
|
struct file * file, int sel_type, select_table *wait)
|
struct file * file, int sel_type, select_table *wait)
|
{
|
{
|
switch (sel_type) {
|
switch (sel_type) {
|
case SEL_IN:
|
case SEL_IN:
|
if (input_available_p(tty, TIME_CHAR(tty) ? 0 :
|
if (input_available_p(tty, TIME_CHAR(tty) ? 0 :
|
MIN_CHAR(tty)))
|
MIN_CHAR(tty)))
|
return 1;
|
return 1;
|
/* fall through */
|
/* fall through */
|
case SEL_EX:
|
case SEL_EX:
|
if (tty->packet && tty->link->ctrl_status)
|
if (tty->packet && tty->link->ctrl_status)
|
return 1;
|
return 1;
|
if (tty->flags & (1 << TTY_OTHER_CLOSED))
|
if (tty->flags & (1 << TTY_OTHER_CLOSED))
|
return 1;
|
return 1;
|
if (tty_hung_up_p(file))
|
if (tty_hung_up_p(file))
|
return 1;
|
return 1;
|
if (!waitqueue_active(&tty->read_wait)) {
|
if (!waitqueue_active(&tty->read_wait)) {
|
if (MIN_CHAR(tty) && !TIME_CHAR(tty))
|
if (MIN_CHAR(tty) && !TIME_CHAR(tty))
|
tty->minimum_to_wake = MIN_CHAR(tty);
|
tty->minimum_to_wake = MIN_CHAR(tty);
|
else
|
else
|
tty->minimum_to_wake = 1;
|
tty->minimum_to_wake = 1;
|
}
|
}
|
select_wait(&tty->read_wait, wait);
|
select_wait(&tty->read_wait, wait);
|
return 0;
|
return 0;
|
case SEL_OUT:
|
case SEL_OUT:
|
if (tty->driver.chars_in_buffer(tty) < WAKEUP_CHARS)
|
if (tty->driver.chars_in_buffer(tty) < WAKEUP_CHARS)
|
return 1;
|
return 1;
|
select_wait(&tty->write_wait, wait);
|
select_wait(&tty->write_wait, wait);
|
return 0;
|
return 0;
|
}
|
}
|
return 0;
|
return 0;
|
}
|
}
|
|
|
struct tty_ldisc tty_ldisc_N_TTY = {
|
struct tty_ldisc tty_ldisc_N_TTY = {
|
TTY_LDISC_MAGIC, /* magic */
|
TTY_LDISC_MAGIC, /* magic */
|
0, /* num */
|
0, /* num */
|
0, /* flags */
|
0, /* flags */
|
n_tty_open, /* open */
|
n_tty_open, /* open */
|
n_tty_close, /* close */
|
n_tty_close, /* close */
|
n_tty_flush_buffer, /* flush_buffer */
|
n_tty_flush_buffer, /* flush_buffer */
|
n_tty_chars_in_buffer, /* chars_in_buffer */
|
n_tty_chars_in_buffer, /* chars_in_buffer */
|
read_chan, /* read */
|
read_chan, /* read */
|
write_chan, /* write */
|
write_chan, /* write */
|
n_tty_ioctl, /* ioctl */
|
n_tty_ioctl, /* ioctl */
|
n_tty_set_termios, /* set_termios */
|
n_tty_set_termios, /* set_termios */
|
normal_select, /* select */
|
normal_select, /* select */
|
n_tty_receive_buf, /* receive_buf */
|
n_tty_receive_buf, /* receive_buf */
|
n_tty_receive_room, /* receive_room */
|
n_tty_receive_room, /* receive_room */
|
0 /* write_wakeup */
|
0 /* write_wakeup */
|
};
|
};
|
|
|
|
|