/*
* n_tty.c --- implements the N_TTY line discipline.
*
* 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
* processing has changed so much that it's hardly recognizable,
* anyway...)
*
* 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
* to N_TTY if it can not switch to any other line discipline.
*
* Written by Theodore Ts'o, Copyright 1994.
*
* This file also contains code originally written by Linus Torvalds,
* Copyright 1991, 1992, 1993, and by Julian Cowley, Copyright 1994.
*
* This file may be redistributed under the terms of the GNU Public
* License.
*
* Reduced memory usage for older ARM systems - Russell King.
*
* 2000/01/20 Fixed SMP locking on put_tty_queue using bits of
* the patch by Andrew J. Kroll <ag784@freenet.buffalo.edu>
* who actually finally proved there really was a race.
*/
#include <linux/types.h>
#include <linux/major.h>
#include <linux/errno.h>
#include <linux/signal.h>
#include <linux/fcntl.h>
#include <linux/sched.h>
#include <linux/interrupt.h>
#include <linux/tty.h>
#include <linux/timer.h>
#include <linux/ctype.h>
#include <linux/kd.h>
#include <linux/mm.h>
#include <linux/string.h>
#include <linux/malloc.h>
#include <linux/poll.h>
#include <asm/uaccess.h>
#include <asm/system.h>
#include <asm/bitops.h>
#define CONSOLE_DEV MKDEV(TTY_MAJOR,0)
#define SYSCONS_DEV MKDEV(TTYAUX_MAJOR,1)
#ifndef MIN
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#endif
/* number of characters left in xmit buffer before select has we have room */
#define WAKEUP_CHARS 256
/*
* This defines the low- and high-watermarks for throttling and
* unthrottling the TTY driver. These watermarks are used for
* controlling the space in the read buffer.
*/
#define TTY_THRESHOLD_THROTTLE 128 /* now based on remaining room */
#define TTY_THRESHOLD_UNTHROTTLE 128
66 static inline unsigned char *alloc_buf(void)
{
unsigned char *p;
int prio = in_interrupt() ? GFP_ATOMIC : GFP_KERNEL;
71 if (PAGE_SIZE != N_TTY_BUF_SIZE) {
p = kmalloc(N_TTY_BUF_SIZE, prio);
73 if (p)
memset(p, 0, N_TTY_BUF_SIZE);
75 } else
p = (unsigned char *)get_zeroed_page(prio);
78 return p;
}
81 static inline void free_buf(unsigned char *buf)
{
83 if (PAGE_SIZE != N_TTY_BUF_SIZE)
kfree(buf);
85 else
free_page((unsigned long) buf);
}
89 static inline void put_tty_queue(unsigned char c, struct tty_struct *tty)
{
unsigned long flags;
/*
* The problem of stomping on the buffers ends here.
* Why didn't anyone see this one comming? --AJK
*/
96 spin_lock_irqsave(&tty->read_lock, flags);
97 if (tty->read_cnt < N_TTY_BUF_SIZE) {
tty->read_buf[tty->read_head] = c;
tty->read_head = (tty->read_head + 1) & (N_TTY_BUF_SIZE-1);
tty->read_cnt++;
}
102 spin_unlock_irqrestore(&tty->read_lock, flags);
}
/*
* Check whether to call the driver.unthrottle function.
* We test the TTY_THROTTLED bit first so that it always
* indicates the current state.
*/
110 static void check_unthrottle(struct tty_struct * tty)
{
if (tty->count &&
test_and_clear_bit(TTY_THROTTLED, &tty->flags) &&
114 tty->driver.unthrottle)
tty->driver.unthrottle(tty);
}
/*
* Reset the read buffer counters, clear the flags,
* and make sure the driver is unthrottled. Called
* from n_tty_open() and n_tty_flush_buffer().
*/
123 static void reset_buffer_flags(struct tty_struct *tty)
{
unsigned long flags;
127 spin_lock_irqsave(&tty->read_lock, flags);
tty->read_head = tty->read_tail = tty->read_cnt = 0;
129 spin_unlock_irqrestore(&tty->read_lock, flags);
tty->canon_head = tty->canon_data = tty->erasing = 0;
memset(&tty->read_flags, 0, sizeof tty->read_flags);
check_unthrottle(tty);
}
/*
* Flush the input buffer
*/
138 void n_tty_flush_buffer(struct tty_struct * tty)
{
/* clear everything and unthrottle the driver */
reset_buffer_flags(tty);
143 if (!tty->link)
144 return;
146 if (tty->link->packet) {
tty->ctrl_status |= TIOCPKT_FLUSHREAD;
wake_up_interruptible(&tty->link->read_wait);
}
}
/*
* Return number of characters buffered to be delivered to user
*/
155 ssize_t n_tty_chars_in_buffer(struct tty_struct *tty)
{
unsigned long flags;
ssize_t n = 0;
160 spin_lock_irqsave(&tty->read_lock, flags);
161 if (!tty->icanon) {
n = tty->read_cnt;
163 } else if (tty->canon_data) {
n = (tty->canon_head > tty->read_tail) ?
tty->canon_head - tty->read_tail :
tty->canon_head + (N_TTY_BUF_SIZE - tty->read_tail);
}
168 spin_unlock_irqrestore(&tty->read_lock, flags);
169 return n;
}
/*
* Perform OPOST processing. Returns -1 when the output device is
* full and the character must be retried.
*/
176 static int opost(unsigned char c, struct tty_struct *tty)
{
int space, spaces;
space = tty->driver.write_room(tty);
181 if (!space)
182 return -1;
184 if (O_OPOST(tty)) {
185 switch (c) {
186 case '\n':
187 if (O_ONLRET(tty))
tty->column = 0;
189 if (O_ONLCR(tty)) {
190 if (space < 2)
191 return -1;
tty->driver.put_char(tty, '\r');
tty->column = 0;
}
tty->canon_column = tty->column;
196 break;
197 case '\r':
198 if (O_ONOCR(tty) && tty->column == 0)
199 return 0;
200 if (O_OCRNL(tty)) {
c = '\n';
202 if (O_ONLRET(tty))
tty->canon_column = tty->column = 0;
204 break;
}
tty->canon_column = tty->column = 0;
207 break;
208 case '\t':
spaces = 8 - (tty->column & 7);
210 if (O_TABDLY(tty) == XTABS) {
211 if (space < spaces)
212 return -1;
tty->column += spaces;
tty->driver.write(tty, 0, " ", spaces);
215 return 0;
}
tty->column += spaces;
218 break;
219 case '\b':
220 if (tty->column > 0)
tty->column--;
222 break;
223 default:
224 if (O_OLCUC(tty))
c = toupper(c);
226 if (!iscntrl(c))
tty->column++;
228 break;
}
}
tty->driver.put_char(tty, c);
232 return 0;
}
/*
* opost_block --- to speed up block console writes, among other
* things.
*/
239 static ssize_t opost_block(struct tty_struct * tty,
const unsigned char * inbuf, unsigned int nr)
{
char buf[80];
int space;
int i;
char *cp;
space = tty->driver.write_room(tty);
248 if (!space)
249 return 0;
250 if (nr > space)
nr = space;
252 if (nr > sizeof(buf))
nr = sizeof(buf);
255 if (copy_from_user(buf, inbuf, nr))
256 return -EFAULT;
258 for (i = 0, cp = buf; i < nr; i++, cp++) {
259 switch (*cp) {
260 case '\n':
261 if (O_ONLRET(tty))
tty->column = 0;
263 if (O_ONLCR(tty))
264 goto break_out;
tty->canon_column = tty->column;
266 break;
267 case '\r':
268 if (O_ONOCR(tty) && tty->column == 0)
269 goto break_out;
270 if (O_OCRNL(tty)) {
*cp = '\n';
272 if (O_ONLRET(tty))
tty->canon_column = tty->column = 0;
274 break;
}
tty->canon_column = tty->column = 0;
277 break;
278 case '\t':
279 goto break_out;
280 case '\b':
281 if (tty->column > 0)
tty->column--;
283 break;
284 default:
285 if (O_OLCUC(tty))
*cp = toupper(*cp);
287 if (!iscntrl(*cp))
tty->column++;
289 break;
}
}
break_out:
293 if (tty->driver.flush_chars)
tty->driver.flush_chars(tty);
i = tty->driver.write(tty, 0, buf, i);
296 return i;
}
301 static inline void put_char(unsigned char c, struct tty_struct *tty)
{
tty->driver.put_char(tty, c);
}
/* Must be called only when L_ECHO(tty) is true. */
308 static void echo_char(unsigned char c, struct tty_struct *tty)
{
310 if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t') {
put_char('^', tty);
put_char(c ^ 0100, tty);
tty->column += 2;
314 } else
opost(c, tty);
}
318 static inline void finish_erasing(struct tty_struct *tty)
{
320 if (tty->erasing) {
put_char('/', tty);
tty->column += 2;
tty->erasing = 0;
}
}
327 static void eraser(unsigned char c, struct tty_struct *tty)
{
enum { ERASE, WERASE, KILL } kill_type;
int head, seen_alnums;
unsigned long flags;
333 if (tty->read_head == tty->canon_head) {
/* opost('\a', tty); */ /* what do you think? */
335 return;
}
337 if (c == ERASE_CHAR(tty))
kill_type = ERASE;
339 else if (c == WERASE_CHAR(tty))
kill_type = WERASE;
341 else {
342 if (!L_ECHO(tty)) {
343 spin_lock_irqsave(&tty->read_lock, flags);
tty->read_cnt -= ((tty->read_head - tty->canon_head) &
(N_TTY_BUF_SIZE - 1));
tty->read_head = tty->canon_head;
347 spin_unlock_irqrestore(&tty->read_lock, flags);
348 return;
}
350 if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
351 spin_lock_irqsave(&tty->read_lock, flags);
tty->read_cnt -= ((tty->read_head - tty->canon_head) &
(N_TTY_BUF_SIZE - 1));
tty->read_head = tty->canon_head;
355 spin_unlock_irqrestore(&tty->read_lock, flags);
finish_erasing(tty);
echo_char(KILL_CHAR(tty), tty);
/* Add a newline if ECHOK is on and ECHOKE is off. */
359 if (L_ECHOK(tty))
opost('\n', tty);
361 return;
}
kill_type = KILL;
}
seen_alnums = 0;
367 while (tty->read_head != tty->canon_head) {
head = (tty->read_head - 1) & (N_TTY_BUF_SIZE-1);
c = tty->read_buf[head];
370 if (kill_type == WERASE) {
/* Equivalent to BSD's ALTWERASE. */
372 if (isalnum(c) || c == '_')
seen_alnums++;
374 else if (seen_alnums)
375 break;
}
377 spin_lock_irqsave(&tty->read_lock, flags);
tty->read_head = head;
tty->read_cnt--;
380 spin_unlock_irqrestore(&tty->read_lock, flags);
381 if (L_ECHO(tty)) {
382 if (L_ECHOPRT(tty)) {
383 if (!tty->erasing) {
put_char('\\', tty);
tty->column++;
tty->erasing = 1;
}
echo_char(c, tty);
389 } else if (kill_type == ERASE && !L_ECHOE(tty)) {
echo_char(ERASE_CHAR(tty), tty);
391 } else if (c == '\t') {
unsigned int col = tty->canon_column;
unsigned long tail = tty->canon_head;
/* Find the column of the last char. */
396 while (tail != tty->read_head) {
c = tty->read_buf[tail];
398 if (c == '\t')
col = (col | 7) + 1;
400 else if (iscntrl(c)) {
401 if (L_ECHOCTL(tty))
col += 2;
403 } else
col++;
tail = (tail+1) & (N_TTY_BUF_SIZE-1);
}
/* should never happen */
409 if (tty->column > 0x80000000)
tty->column = 0;
/* Now backup to that column. */
413 while (tty->column > col) {
/* Can't use opost here. */
put_char('\b', tty);
416 if (tty->column > 0)
tty->column--;
}
419 } else {
420 if (iscntrl(c) && L_ECHOCTL(tty)) {
put_char('\b', tty);
put_char(' ', tty);
put_char('\b', tty);
424 if (tty->column > 0)
tty->column--;
}
427 if (!iscntrl(c) || L_ECHOCTL(tty)) {
put_char('\b', tty);
put_char(' ', tty);
put_char('\b', tty);
431 if (tty->column > 0)
tty->column--;
}
}
}
436 if (kill_type == ERASE)
437 break;
}
439 if (tty->read_head == tty->canon_head)
finish_erasing(tty);
}
443 static inline void isig(int sig, struct tty_struct *tty, int flush)
{
445 if (tty->pgrp > 0)
kill_pg(tty->pgrp, sig, 1);
447 if (flush || !L_NOFLSH(tty)) {
n_tty_flush_buffer(tty);
449 if (tty->driver.flush_buffer)
tty->driver.flush_buffer(tty);
}
}
454 static inline void n_tty_receive_break(struct tty_struct *tty)
{
456 if (I_IGNBRK(tty))
457 return;
458 if (I_BRKINT(tty)) {
isig(SIGINT, tty, 1);
460 return;
}
462 if (I_PARMRK(tty)) {
put_tty_queue('\377', tty);
put_tty_queue('\0', tty);
}
put_tty_queue('\0', tty);
wake_up_interruptible(&tty->read_wait);
}
470 static inline void n_tty_receive_overrun(struct tty_struct *tty)
{
char buf[64];
tty->num_overrun++;
475 if (time_before(tty->overrun_time, jiffies - HZ)) {
printk("%s: %d input overrun(s)\n", tty_name(tty, buf),
tty->num_overrun);
tty->overrun_time = jiffies;
tty->num_overrun = 0;
}
}
483 static inline void n_tty_receive_parity_error(struct tty_struct *tty,
unsigned char c)
{
486 if (I_IGNPAR(tty)) {
487 return;
}
489 if (I_PARMRK(tty)) {
put_tty_queue('\377', tty);
put_tty_queue('\0', tty);
put_tty_queue(c, tty);
493 } else if (I_INPCK(tty))
put_tty_queue('\0', tty);
495 else
put_tty_queue(c, tty);
wake_up_interruptible(&tty->read_wait);
}
500 static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
{
502 if (tty->raw) {
put_tty_queue(c, tty);
504 return;
}
if (tty->stopped && !tty->flow_stopped &&
508 I_IXON(tty) && I_IXANY(tty)) {
start_tty(tty);
510 return;
}
513 if (I_ISTRIP(tty))
c &= 0x7f;
515 if (I_IUCLC(tty) && L_IEXTEN(tty))
c=tolower(c);
518 if (tty->closing) {
519 if (I_IXON(tty)) {
520 if (c == START_CHAR(tty))
start_tty(tty);
522 else if (c == STOP_CHAR(tty))
stop_tty(tty);
}
525 return;
}
/*
* If the previous character was LNEXT, or we know that this
* character is not one of the characters that we'll have to
* handle specially, do shortcut processing to speed things
* up.
*/
534 if (!test_bit(c, &tty->process_char_map) || tty->lnext) {
finish_erasing(tty);
tty->lnext = 0;
537 if (L_ECHO(tty)) {
538 if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
put_char('\a', tty); /* beep if no space */
540 return;
}
/* Record the column of first canon char. */
543 if (tty->canon_head == tty->read_head)
tty->canon_column = tty->column;
echo_char(c, tty);
}
547 if (I_PARMRK(tty) && c == (unsigned char) '\377')
put_tty_queue(c, tty);
put_tty_queue(c, tty);
550 return;
}
553 if (c == '\r') {
554 if (I_IGNCR(tty))
555 return;
556 if (I_ICRNL(tty))
c = '\n';
558 } else if (c == '\n' && I_INLCR(tty))
c = '\r';
560 if (I_IXON(tty)) {
561 if (c == START_CHAR(tty)) {
start_tty(tty);
563 return;
}
565 if (c == STOP_CHAR(tty)) {
stop_tty(tty);
567 return;
}
}
570 if (L_ISIG(tty)) {
int signal;
signal = SIGINT;
573 if (c == INTR_CHAR(tty))
574 goto send_signal;
signal = SIGQUIT;
576 if (c == QUIT_CHAR(tty))
577 goto send_signal;
signal = SIGTSTP;
579 if (c == SUSP_CHAR(tty)) {
send_signal:
isig(signal, tty, 0);
582 return;
}
}
585 if (tty->icanon) {
if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
587 (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
eraser(c, tty);
589 return;
}
591 if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
tty->lnext = 1;
593 if (L_ECHO(tty)) {
finish_erasing(tty);
595 if (L_ECHOCTL(tty)) {
put_char('^', tty);
put_char('\b', tty);
}
}
600 return;
}
if (c == REPRINT_CHAR(tty) && L_ECHO(tty) &&
603 L_IEXTEN(tty)) {
unsigned long tail = tty->canon_head;
finish_erasing(tty);
echo_char(c, tty);
opost('\n', tty);
609 while (tail != tty->read_head) {
echo_char(tty->read_buf[tail], tty);
tail = (tail+1) & (N_TTY_BUF_SIZE-1);
}
613 return;
}
615 if (c == '\n') {
616 if (L_ECHO(tty) || L_ECHONL(tty)) {
617 if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
put_char('\a', tty);
619 return;
}
opost('\n', tty);
}
623 goto handle_newline;
}
625 if (c == EOF_CHAR(tty)) {
626 if (tty->canon_head != tty->read_head)
set_bit(TTY_PUSH, &tty->flags);
c = __DISABLED_CHAR;
629 goto handle_newline;
}
if ((c == EOL_CHAR(tty)) ||
632 (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
/*
* XXX are EOL_CHAR and EOL2_CHAR echoed?!?
*/
636 if (L_ECHO(tty)) {
637 if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
put_char('\a', tty);
639 return;
}
/* Record the column of first canon char. */
642 if (tty->canon_head == tty->read_head)
tty->canon_column = tty->column;
echo_char(c, tty);
}
/*
* XXX does PARMRK doubling happen for
* EOL_CHAR and EOL2_CHAR?
*/
650 if (I_PARMRK(tty) && c == (unsigned char) '\377')
put_tty_queue(c, tty);
handle_newline:
set_bit(tty->read_head, &tty->read_flags);
put_tty_queue(c, tty);
tty->canon_head = tty->read_head;
tty->canon_data++;
kill_fasync(&tty->fasync, SIGIO, POLL_IN);
659 if (waitqueue_active(&tty->read_wait))
wake_up_interruptible(&tty->read_wait);
661 return;
}
}
finish_erasing(tty);
666 if (L_ECHO(tty)) {
667 if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
put_char('\a', tty); /* beep if no space */
669 return;
}
671 if (c == '\n')
opost('\n', tty);
673 else {
/* Record the column of first canon char. */
675 if (tty->canon_head == tty->read_head)
tty->canon_column = tty->column;
echo_char(c, tty);
}
}
681 if (I_PARMRK(tty) && c == (unsigned char) '\377')
put_tty_queue(c, tty);
put_tty_queue(c, tty);
}
687 static int n_tty_receive_room(struct tty_struct *tty)
{
int left = N_TTY_BUF_SIZE - tty->read_cnt - 1;
/*
* If we are doing input canonicalization, and there are no
* pending newlines, let characters through without limit, so
* that erase characters will be handled. Other excess
* characters will be beeped.
*/
697 if (tty->icanon && !tty->canon_data)
698 return N_TTY_BUF_SIZE;
700 if (left > 0)
701 return left;
702 return 0;
}
705 static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
char *fp, int count)
{
const unsigned char *p;
char *f, flags = TTY_NORMAL;
int i;
char buf[64];
unsigned long cpuflags;
714 if (!tty->read_buf)
715 return;
717 if (tty->real_raw) {
718 spin_lock_irqsave(&tty->read_lock, cpuflags);
i = MIN(count, MIN(N_TTY_BUF_SIZE - tty->read_cnt,
N_TTY_BUF_SIZE - tty->read_head));
memcpy(tty->read_buf + tty->read_head, cp, i);
tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
tty->read_cnt += i;
cp += i;
count -= i;
i = MIN(count, MIN(N_TTY_BUF_SIZE - tty->read_cnt,
N_TTY_BUF_SIZE - tty->read_head));
memcpy(tty->read_buf + tty->read_head, cp, i);
tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
tty->read_cnt += i;
732 spin_unlock_irqrestore(&tty->read_lock, cpuflags);
733 } else {
734 for (i=count, p = cp, f = fp; i; i--, p++) {
735 if (f)
flags = *f++;
737 switch (flags) {
738 case TTY_NORMAL:
n_tty_receive_char(tty, *p);
740 break;
741 case TTY_BREAK:
n_tty_receive_break(tty);
743 break;
744 case TTY_PARITY:
745 case TTY_FRAME:
n_tty_receive_parity_error(tty, *p);
747 break;
748 case TTY_OVERRUN:
n_tty_receive_overrun(tty);
750 break;
751 default:
printk("%s: unknown flag %d\n",
tty_name(tty, buf), flags);
754 break;
}
}
757 if (tty->driver.flush_chars)
tty->driver.flush_chars(tty);
}
761 if (!tty->icanon && (tty->read_cnt >= tty->minimum_to_wake)) {
kill_fasync(&tty->fasync, SIGIO, POLL_IN);
763 if (waitqueue_active(&tty->read_wait))
wake_up_interruptible(&tty->read_wait);
}
/*
* Check the remaining room for the input canonicalization
* mode. We don't want to throttle the driver if we're in
* canonical mode and don't have a newline yet!
*/
772 if (n_tty_receive_room(tty) < TTY_THRESHOLD_THROTTLE) {
/* check TTY_THROTTLED first so it indicates our state */
if (!test_and_set_bit(TTY_THROTTLED, &tty->flags) &&
775 tty->driver.throttle)
tty->driver.throttle(tty);
}
}
780 int is_ignored(int sig)
{
return (sigismember(¤t->blocked, sig) ||
783 current->sig->action[sig-1].sa.sa_handler == SIG_IGN);
}
786 static void n_tty_set_termios(struct tty_struct *tty, struct termios * old)
{
788 if (!tty)
789 return;
tty->icanon = (L_ICANON(tty) != 0);
792 if (test_bit(TTY_HW_COOK_IN, &tty->flags)) {
tty->raw = 1;
tty->real_raw = 1;
795 return;
}
if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
800 I_PARMRK(tty)) {
cli();
memset(tty->process_char_map, 0, 256/8);
804 if (I_IGNCR(tty) || I_ICRNL(tty))
set_bit('\r', &tty->process_char_map);
806 if (I_INLCR(tty))
set_bit('\n', &tty->process_char_map);
809 if (L_ICANON(tty)) {
set_bit(ERASE_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('\n', &tty->process_char_map);
set_bit(EOL_CHAR(tty), &tty->process_char_map);
815 if (L_IEXTEN(tty)) {
set_bit(WERASE_CHAR(tty),
&tty->process_char_map);
set_bit(LNEXT_CHAR(tty),
&tty->process_char_map);
set_bit(EOL2_CHAR(tty),
&tty->process_char_map);
822 if (L_ECHO(tty))
set_bit(REPRINT_CHAR(tty),
&tty->process_char_map);
}
}
827 if (I_IXON(tty)) {
set_bit(START_CHAR(tty), &tty->process_char_map);
set_bit(STOP_CHAR(tty), &tty->process_char_map);
}
831 if (L_ISIG(tty)) {
set_bit(INTR_CHAR(tty), &tty->process_char_map);
set_bit(QUIT_CHAR(tty), &tty->process_char_map);
set_bit(SUSP_CHAR(tty), &tty->process_char_map);
}
clear_bit(__DISABLED_CHAR, &tty->process_char_map);
sti();
tty->raw = 0;
tty->real_raw = 0;
840 } else {
tty->raw = 1;
if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
(I_IGNPAR(tty) || !I_INPCK(tty)) &&
844 (tty->driver.flags & TTY_DRIVER_REAL_RAW))
tty->real_raw = 1;
846 else
tty->real_raw = 0;
}
}
851 static void n_tty_close(struct tty_struct *tty)
{
n_tty_flush_buffer(tty);
854 if (tty->read_buf) {
free_buf(tty->read_buf);
tty->read_buf = 0;
}
}
860 static int n_tty_open(struct tty_struct *tty)
{
862 if (!tty)
863 return -EINVAL;
865 if (!tty->read_buf) {
tty->read_buf = alloc_buf();
867 if (!tty->read_buf)
868 return -ENOMEM;
}
memset(tty->read_buf, 0, N_TTY_BUF_SIZE);
reset_buffer_flags(tty);
tty->column = 0;
n_tty_set_termios(tty, 0);
tty->minimum_to_wake = 1;
tty->closing = 0;
876 return 0;
}
879 static inline int input_available_p(struct tty_struct *tty, int amt)
{
881 if (tty->icanon) {
882 if (tty->canon_data)
883 return 1;
884 } else if (tty->read_cnt >= (amt ? amt : 1))
885 return 1;
887 return 0;
}
/*
* Helper function to speed up read_chan. It is only called when
* ICANON is off; it copies characters straight from the tty queue 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
* buffer, and once to drain the space from the (physical) beginning of
* the buffer to head pointer.
*/
898 static inline int copy_from_read_buf(struct tty_struct *tty,
unsigned char **b,
size_t *nr)
{
int retval;
ssize_t n;
unsigned long flags;
retval = 0;
908 spin_lock_irqsave(&tty->read_lock, flags);
n = MIN(*nr, MIN(tty->read_cnt, N_TTY_BUF_SIZE - tty->read_tail));
910 spin_unlock_irqrestore(&tty->read_lock, flags);
911 if (n) {
mb();
retval = copy_to_user(*b, &tty->read_buf[tty->read_tail], n);
n -= retval;
915 spin_lock_irqsave(&tty->read_lock, flags);
tty->read_tail = (tty->read_tail + n) & (N_TTY_BUF_SIZE-1);
tty->read_cnt -= n;
918 spin_unlock_irqrestore(&tty->read_lock, flags);
*b += n;
*nr -= n;
}
922 return retval;
}
925 static ssize_t read_chan(struct tty_struct *tty, struct file *file,
unsigned char *buf, size_t nr)
{
unsigned char *b = buf;
DECLARE_WAITQUEUE(wait, current);
int c;
int minimum, time;
ssize_t retval = 0;
ssize_t size;
long timeout;
unsigned long flags;
do_it_again:
939 if (!tty->read_buf) {
printk("n_tty_read_chan: called with read_buf == NULL?!?\n");
941 return -EIO;
}
/* Job control check -- must be done at start and after
every sleep (POSIX.1 7.1.1.4). */
/* NOTE: not yet done after every sleep pending a thorough
check of the logic of this change. -- jlc */
/* don't stop on /dev/console */
if (file->f_dentry->d_inode->i_rdev != CONSOLE_DEV &&
file->f_dentry->d_inode->i_rdev != SYSCONS_DEV &&
951 current->tty == tty) {
952 if (tty->pgrp <= 0)
printk("read_chan: tty->pgrp <= 0!\n");
954 else if (current->pgrp != tty->pgrp) {
if (is_ignored(SIGTTIN) ||
956 is_orphaned_pgrp(current->pgrp))
957 return -EIO;
kill_pg(current->pgrp, SIGTTIN, 1);
959 return -ERESTARTSYS;
}
}
minimum = time = 0;
timeout = MAX_SCHEDULE_TIMEOUT;
965 if (!tty->icanon) {
time = (HZ / 10) * TIME_CHAR(tty);
minimum = MIN_CHAR(tty);
968 if (minimum) {
969 if (time)
tty->minimum_to_wake = 1;
else if (!waitqueue_active(&tty->read_wait) ||
972 (tty->minimum_to_wake > minimum))
tty->minimum_to_wake = minimum;
974 } else {
timeout = 0;
976 if (time) {
timeout = time;
time = 0;
}
tty->minimum_to_wake = minimum = 1;
}
}
984 if (file->f_flags & O_NONBLOCK) {
985 if (down_trylock(&tty->atomic_read))
986 return -EAGAIN;
}
988 else {
989 if (down_interruptible(&tty->atomic_read))
990 return -ERESTARTSYS;
}
add_wait_queue(&tty->read_wait, &wait);
set_bit(TTY_DONT_FLIP, &tty->flags);
995 while (nr) {
/* First test for status change. */
997 if (tty->packet && tty->link->ctrl_status) {
unsigned char cs;
999 if (b != buf)
1000 break;
cs = tty->link->ctrl_status;
tty->link->ctrl_status = 0;
put_user(cs, b++);
nr--;
1005 break;
}
/* This statement must be first before checking for input
so that any interrupt will set the state back to
TASK_RUNNING. */
1010 set_current_state(TASK_INTERRUPTIBLE);
if (((minimum - (b - buf)) < tty->minimum_to_wake) &&
1013 ((minimum - (b - buf)) >= 1))
tty->minimum_to_wake = (minimum - (b - buf));
1016 if (!input_available_p(tty, 0)) {
1017 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
retval = -EIO;
1019 break;
}
1021 if (tty_hung_up_p(file))
1022 break;
1023 if (!timeout)
1024 break;
1025 if (file->f_flags & O_NONBLOCK) {
retval = -EAGAIN;
1027 break;
}
1029 if (signal_pending(current)) {
retval = -ERESTARTSYS;
1031 break;
}
clear_bit(TTY_DONT_FLIP, &tty->flags);
timeout = schedule_timeout(timeout);
set_bit(TTY_DONT_FLIP, &tty->flags);
1036 continue;
}
current->state = TASK_RUNNING;
/* Deal with packet mode. */
1041 if (tty->packet && b == buf) {
put_user(TIOCPKT_DATA, b++);
nr--;
}
1046 if (tty->icanon) {
/* N.B. avoid overrun if nr == 0 */
1048 while (nr && tty->read_cnt) {
int eol;
eol = test_and_clear_bit(tty->read_tail,
&tty->read_flags);
c = tty->read_buf[tty->read_tail];
1054 spin_lock_irqsave(&tty->read_lock, flags);
tty->read_tail = ((tty->read_tail+1) &
(N_TTY_BUF_SIZE-1));
tty->read_cnt--;
1058 spin_unlock_irqrestore(&tty->read_lock, flags);
1060 if (!eol || (c != __DISABLED_CHAR)) {
put_user(c, b++);
nr--;
}
1064 if (eol) {
/* this test should be redundant:
* we shouldn't be reading data if
* canon_data is 0
*/
1069 if (--tty->canon_data < 0)
tty->canon_data = 0;
1071 break;
}
}
1074 } else {
int uncopied;
uncopied = copy_from_read_buf(tty, &b, &nr);
uncopied += copy_from_read_buf(tty, &b, &nr);
1078 if (uncopied) {
retval = -EFAULT;
1080 break;
}
}
/* If there is enough space in the read buffer now, let the
* low-level driver know. We use n_tty_chars_in_buffer() to
* check the buffer, as it now knows about canonical mode.
* Otherwise, if the driver is throttled and the line is
* longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
* we won't get any more characters.
*/
1091 if (n_tty_chars_in_buffer(tty) <= TTY_THRESHOLD_UNTHROTTLE)
check_unthrottle(tty);
1094 if (b - buf >= minimum)
1095 break;
1096 if (time)
timeout = time;
}
clear_bit(TTY_DONT_FLIP, &tty->flags);
up(&tty->atomic_read);
remove_wait_queue(&tty->read_wait, &wait);
1103 if (!waitqueue_active(&tty->read_wait))
tty->minimum_to_wake = minimum;
current->state = TASK_RUNNING;
size = b - buf;
1108 if (size) {
retval = size;
1110 if (nr)
clear_bit(TTY_PUSH, &tty->flags);
1112 } else if (test_and_clear_bit(TTY_PUSH, &tty->flags))
1113 goto do_it_again;
1115 return retval;
}
1118 static ssize_t write_chan(struct tty_struct * tty, struct file * file,
const unsigned char * buf, size_t nr)
{
const unsigned char *b = buf;
DECLARE_WAITQUEUE(wait, current);
int c;
ssize_t retval = 0;
/* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
if (L_TOSTOP(tty) &&
file->f_dentry->d_inode->i_rdev != CONSOLE_DEV &&
1129 file->f_dentry->d_inode->i_rdev != SYSCONS_DEV) {
retval = tty_check_change(tty);
1131 if (retval)
1132 return retval;
}
add_wait_queue(&tty->write_wait, &wait);
1136 while (1) {
1137 set_current_state(TASK_INTERRUPTIBLE);
1138 if (signal_pending(current)) {
retval = -ERESTARTSYS;
1140 break;
}
1142 if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
retval = -EIO;
1144 break;
}
1146 if (O_OPOST(tty) && !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
1147 while (nr > 0) {
ssize_t num = opost_block(tty, b, nr);
1149 if (num < 0) {
retval = num;
1151 goto break_out;
}
b += num;
nr -= num;
1155 if (nr == 0)
1156 break;
get_user(c, b);
1158 if (opost(c, tty) < 0)
1159 break;
b++; nr--;
}
1162 if (tty->driver.flush_chars)
tty->driver.flush_chars(tty);
1164 } else {
c = tty->driver.write(tty, 1, b, nr);
1166 if (c < 0) {
retval = c;
1168 goto break_out;
}
b += c;
nr -= c;
}
1173 if (!nr)
1174 break;
1175 if (file->f_flags & O_NONBLOCK) {
retval = -EAGAIN;
1177 break;
}
schedule();
}
break_out:
current->state = TASK_RUNNING;
remove_wait_queue(&tty->write_wait, &wait);
1184 return (b - buf) ? b - buf : retval;
}
/* Called without the kernel lock held - fine */
1188 static unsigned int normal_poll(struct tty_struct * tty, struct file * file, poll_table *wait)
{
unsigned int mask = 0;
poll_wait(file, &tty->read_wait, wait);
poll_wait(file, &tty->write_wait, wait);
1194 if (input_available_p(tty, TIME_CHAR(tty) ? 0 : MIN_CHAR(tty)))
mask |= POLLIN | POLLRDNORM;
1196 if (tty->packet && tty->link->ctrl_status)
mask |= POLLPRI | POLLIN | POLLRDNORM;
1198 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
mask |= POLLHUP;
1200 if (tty_hung_up_p(file))
mask |= POLLHUP;
1202 if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) {
1203 if (MIN_CHAR(tty) && !TIME_CHAR(tty))
tty->minimum_to_wake = MIN_CHAR(tty);
1205 else
tty->minimum_to_wake = 1;
}
1208 if (tty->driver.chars_in_buffer(tty) < WAKEUP_CHARS)
mask |= POLLOUT | POLLWRNORM;
1210 return mask;
}
struct tty_ldisc tty_ldisc_N_TTY = {
TTY_LDISC_MAGIC, /* magic */
"n_tty", /* name */
0, /* num */
0, /* flags */
n_tty_open, /* open */
n_tty_close, /* close */
n_tty_flush_buffer, /* flush_buffer */
n_tty_chars_in_buffer, /* chars_in_buffer */
read_chan, /* read */
write_chan, /* write */
n_tty_ioctl, /* ioctl */
n_tty_set_termios, /* set_termios */
normal_poll, /* poll */
n_tty_receive_buf, /* receive_buf */
n_tty_receive_room, /* receive_room */
0 /* write_wakeup */
};