Import 2.1.118
[davej-history.git] / drivers / char / n_tty.c
blob07272723d90a3c6acde3fbd35e7bdb521cfa5da0
1 /*
2 * n_tty.c --- implements the N_TTY line discipline.
3 *
4 * This code used to be in tty_io.c, but things are getting hairy
5 * enough that it made sense to split things off. (The N_TTY
6 * processing has changed so much that it's hardly recognizable,
7 * anyway...)
9 * Note that the open routine for N_TTY is guaranteed never to return
10 * an error. This is because Linux will fall back to setting a line
11 * to N_TTY if it can not switch to any other line discipline.
13 * Written by Theodore Ts'o, Copyright 1994.
15 * This file also contains code originally written by Linus Torvalds,
16 * Copyright 1991, 1992, 1993, and by Julian Cowley, Copyright 1994.
18 * This file may be redistributed under the terms of the GNU Public
19 * License.
22 #include <linux/types.h>
23 #include <linux/major.h>
24 #include <linux/errno.h>
25 #include <linux/signal.h>
26 #include <linux/fcntl.h>
27 #include <linux/sched.h>
28 #include <linux/interrupt.h>
29 #include <linux/tty.h>
30 #include <linux/timer.h>
31 #include <linux/ctype.h>
32 #include <linux/kd.h>
33 #include <linux/mm.h>
34 #include <linux/string.h>
35 #include <linux/malloc.h>
36 #include <linux/poll.h>
38 #include <asm/uaccess.h>
39 #include <asm/system.h>
40 #include <asm/bitops.h>
42 #define CONSOLE_DEV MKDEV(TTY_MAJOR,0)
43 #define SYSCONS_DEV MKDEV(TTYAUX_MAJOR,1)
45 #ifndef MIN
46 #define MIN(a,b) ((a) < (b) ? (a) : (b))
47 #endif
49 /* number of characters left in xmit buffer before select has we have room */
50 #define WAKEUP_CHARS 256
53 * This defines the low- and high-watermarks for throttling and
54 * unthrottling the TTY driver. These watermarks are used for
55 * controlling the space in the read buffer.
57 #define TTY_THRESHOLD_THROTTLE 128 /* now based on remaining room */
58 #define TTY_THRESHOLD_UNTHROTTLE 128
60 static inline void put_tty_queue(unsigned char c, struct tty_struct *tty)
62 if (tty->read_cnt < N_TTY_BUF_SIZE) {
63 tty->read_buf[tty->read_head] = c;
64 tty->read_head = (tty->read_head + 1) & (N_TTY_BUF_SIZE-1);
65 tty->read_cnt++;
69 /*
70 * Check whether to call the driver.unthrottle function.
71 * We test the TTY_THROTTLED bit first so that it always
72 * indicates the current state.
74 static void check_unthrottle(struct tty_struct * tty)
76 if (test_and_clear_bit(TTY_THROTTLED, &tty->flags) &&
77 tty->driver.unthrottle)
78 tty->driver.unthrottle(tty);
82 * Reset the read buffer counters, clear the flags,
83 * and make sure the driver is unthrottled. Called
84 * from n_tty_open() and n_tty_flush_buffer().
86 static void reset_buffer_flags(struct tty_struct *tty)
88 tty->read_head = tty->read_tail = tty->read_cnt = 0;
89 tty->canon_head = tty->canon_data = tty->erasing = 0;
90 memset(&tty->read_flags, 0, sizeof tty->read_flags);
91 check_unthrottle(tty);
95 * Flush the input buffer
97 void n_tty_flush_buffer(struct tty_struct * tty)
99 /* clear everything and unthrottle the driver */
100 reset_buffer_flags(tty);
102 if (!tty->link)
103 return;
105 if (tty->link->packet) {
106 tty->ctrl_status |= TIOCPKT_FLUSHREAD;
107 wake_up_interruptible(&tty->link->read_wait);
112 * Return number of characters buffered to be delivered to user
114 ssize_t n_tty_chars_in_buffer(struct tty_struct *tty)
116 if (tty->icanon) {
117 if (!tty->canon_data) return 0;
119 return (tty->canon_head > tty->read_tail) ?
120 tty->canon_head - tty->read_tail :
121 tty->canon_head + (N_TTY_BUF_SIZE - tty->read_tail);
123 return tty->read_cnt;
127 * Perform OPOST processing. Returns -1 when the output device is
128 * full and the character must be retried.
130 static int opost(unsigned char c, struct tty_struct *tty)
132 int space, spaces;
134 space = tty->driver.write_room(tty);
135 if (!space)
136 return -1;
138 if (O_OPOST(tty)) {
139 switch (c) {
140 case '\n':
141 if (O_ONLRET(tty))
142 tty->column = 0;
143 if (O_ONLCR(tty)) {
144 if (space < 2)
145 return -1;
146 tty->driver.put_char(tty, '\r');
147 tty->column = 0;
149 tty->canon_column = tty->column;
150 break;
151 case '\r':
152 if (O_ONOCR(tty) && tty->column == 0)
153 return 0;
154 if (O_OCRNL(tty)) {
155 c = '\n';
156 if (O_ONLRET(tty))
157 tty->canon_column = tty->column = 0;
158 break;
160 tty->canon_column = tty->column = 0;
161 break;
162 case '\t':
163 spaces = 8 - (tty->column & 7);
164 if (O_TABDLY(tty) == XTABS) {
165 if (space < spaces)
166 return -1;
167 tty->column += spaces;
168 tty->driver.write(tty, 0, " ", spaces);
169 return 0;
171 tty->column += spaces;
172 break;
173 case '\b':
174 if (tty->column > 0)
175 tty->column--;
176 break;
177 default:
178 if (O_OLCUC(tty))
179 c = toupper(c);
180 if (!iscntrl(c))
181 tty->column++;
182 break;
185 tty->driver.put_char(tty, c);
186 return 0;
190 * opost_block --- to speed up block console writes, among other
191 * things.
193 static ssize_t opost_block(struct tty_struct * tty,
194 const unsigned char * inbuf, unsigned int nr)
196 char buf[80];
197 int space;
198 int i;
199 char *cp;
201 space = tty->driver.write_room(tty);
202 if (!space)
203 return 0;
204 if (nr > space)
205 nr = space;
206 if (nr > sizeof(buf))
207 nr = sizeof(buf);
208 nr -= copy_from_user(buf, inbuf, nr);
209 if (!nr)
210 return 0;
212 for (i = 0, cp = buf; i < nr; i++, cp++) {
213 switch (*cp) {
214 case '\n':
215 if (O_ONLRET(tty))
216 tty->column = 0;
217 if (O_ONLCR(tty))
218 goto break_out;
219 tty->canon_column = tty->column;
220 break;
221 case '\r':
222 if (O_ONOCR(tty) && tty->column == 0)
223 goto break_out;
224 if (O_OCRNL(tty)) {
225 *cp = '\n';
226 if (O_ONLRET(tty))
227 tty->canon_column = tty->column = 0;
228 break;
230 tty->canon_column = tty->column = 0;
231 break;
232 case '\t':
233 goto break_out;
234 case '\b':
235 if (tty->column > 0)
236 tty->column--;
237 break;
238 default:
239 if (O_OLCUC(tty))
240 *cp = toupper(*cp);
241 if (!iscntrl(*cp))
242 tty->column++;
243 break;
246 break_out:
247 if (tty->driver.flush_chars)
248 tty->driver.flush_chars(tty);
249 i = tty->driver.write(tty, 0, buf, i);
250 return i;
255 static inline void put_char(unsigned char c, struct tty_struct *tty)
257 tty->driver.put_char(tty, c);
260 /* Must be called only when L_ECHO(tty) is true. */
262 static void echo_char(unsigned char c, struct tty_struct *tty)
264 if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t') {
265 put_char('^', tty);
266 put_char(c ^ 0100, tty);
267 tty->column += 2;
268 } else
269 opost(c, tty);
272 static inline void finish_erasing(struct tty_struct *tty)
274 if (tty->erasing) {
275 put_char('/', tty);
276 tty->column += 2;
277 tty->erasing = 0;
281 static void eraser(unsigned char c, struct tty_struct *tty)
283 enum { ERASE, WERASE, KILL } kill_type;
284 int head, seen_alnums;
286 if (tty->read_head == tty->canon_head) {
287 /* opost('\a', tty); */ /* what do you think? */
288 return;
290 if (c == ERASE_CHAR(tty))
291 kill_type = ERASE;
292 else if (c == WERASE_CHAR(tty))
293 kill_type = WERASE;
294 else {
295 if (!L_ECHO(tty)) {
296 tty->read_cnt -= ((tty->read_head - tty->canon_head) &
297 (N_TTY_BUF_SIZE - 1));
298 tty->read_head = tty->canon_head;
299 return;
301 if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
302 tty->read_cnt -= ((tty->read_head - tty->canon_head) &
303 (N_TTY_BUF_SIZE - 1));
304 tty->read_head = tty->canon_head;
305 finish_erasing(tty);
306 echo_char(KILL_CHAR(tty), tty);
307 /* Add a newline if ECHOK is on and ECHOKE is off. */
308 if (L_ECHOK(tty))
309 opost('\n', tty);
310 return;
312 kill_type = KILL;
315 seen_alnums = 0;
316 while (tty->read_head != tty->canon_head) {
317 head = (tty->read_head - 1) & (N_TTY_BUF_SIZE-1);
318 c = tty->read_buf[head];
319 if (kill_type == WERASE) {
320 /* Equivalent to BSD's ALTWERASE. */
321 if (isalnum(c) || c == '_')
322 seen_alnums++;
323 else if (seen_alnums)
324 break;
326 tty->read_head = head;
327 tty->read_cnt--;
328 if (L_ECHO(tty)) {
329 if (L_ECHOPRT(tty)) {
330 if (!tty->erasing) {
331 put_char('\\', tty);
332 tty->column++;
333 tty->erasing = 1;
335 echo_char(c, tty);
336 } else if (kill_type == ERASE && !L_ECHOE(tty)) {
337 echo_char(ERASE_CHAR(tty), tty);
338 } else if (c == '\t') {
339 unsigned int col = tty->canon_column;
340 unsigned long tail = tty->canon_head;
342 /* Find the column of the last char. */
343 while (tail != tty->read_head) {
344 c = tty->read_buf[tail];
345 if (c == '\t')
346 col = (col | 7) + 1;
347 else if (iscntrl(c)) {
348 if (L_ECHOCTL(tty))
349 col += 2;
350 } else
351 col++;
352 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
355 /* should never happen */
356 if (tty->column > 0x80000000)
357 tty->column = 0;
359 /* Now backup to that column. */
360 while (tty->column > col) {
361 /* Can't use opost here. */
362 put_char('\b', tty);
363 if (tty->column > 0)
364 tty->column--;
366 } else {
367 if (iscntrl(c) && L_ECHOCTL(tty)) {
368 put_char('\b', tty);
369 put_char(' ', tty);
370 put_char('\b', tty);
371 if (tty->column > 0)
372 tty->column--;
374 if (!iscntrl(c) || L_ECHOCTL(tty)) {
375 put_char('\b', tty);
376 put_char(' ', tty);
377 put_char('\b', tty);
378 if (tty->column > 0)
379 tty->column--;
383 if (kill_type == ERASE)
384 break;
386 if (tty->read_head == tty->canon_head)
387 finish_erasing(tty);
390 static inline void isig(int sig, struct tty_struct *tty, int flush)
392 if (tty->pgrp > 0)
393 kill_pg(tty->pgrp, sig, 1);
394 if (flush || !L_NOFLSH(tty)) {
395 n_tty_flush_buffer(tty);
396 if (tty->driver.flush_buffer)
397 tty->driver.flush_buffer(tty);
401 static inline void n_tty_receive_break(struct tty_struct *tty)
403 if (I_IGNBRK(tty))
404 return;
405 if (I_BRKINT(tty)) {
406 isig(SIGINT, tty, 1);
407 return;
409 if (I_PARMRK(tty)) {
410 put_tty_queue('\377', tty);
411 put_tty_queue('\0', tty);
413 put_tty_queue('\0', tty);
414 wake_up_interruptible(&tty->read_wait);
417 static inline void n_tty_receive_overrun(struct tty_struct *tty)
419 char buf[64];
421 tty->num_overrun++;
422 if (tty->overrun_time < (jiffies - HZ)) {
423 printk("%s: %d input overrun(s)\n", tty_name(tty, buf),
424 tty->num_overrun);
425 tty->overrun_time = jiffies;
426 tty->num_overrun = 0;
430 static inline void n_tty_receive_parity_error(struct tty_struct *tty,
431 unsigned char c)
433 if (I_IGNPAR(tty)) {
434 return;
436 if (I_PARMRK(tty)) {
437 put_tty_queue('\377', tty);
438 put_tty_queue('\0', tty);
439 put_tty_queue(c, tty);
440 } else if (I_INPCK(tty))
441 put_tty_queue('\0', tty);
442 else
443 put_tty_queue(c, tty);
444 wake_up_interruptible(&tty->read_wait);
447 static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
449 if (tty->raw) {
450 put_tty_queue(c, tty);
451 return;
454 if (tty->stopped && !tty->flow_stopped &&
455 I_IXON(tty) && I_IXANY(tty)) {
456 start_tty(tty);
457 return;
460 if (I_ISTRIP(tty))
461 c &= 0x7f;
462 if (I_IUCLC(tty) && L_IEXTEN(tty))
463 c=tolower(c);
465 if (tty->closing) {
466 if (I_IXON(tty)) {
467 if (c == START_CHAR(tty))
468 start_tty(tty);
469 else if (c == STOP_CHAR(tty))
470 stop_tty(tty);
472 return;
476 * If the previous character was LNEXT, or we know that this
477 * character is not one of the characters that we'll have to
478 * handle specially, do shortcut processing to speed things
479 * up.
481 if (!test_bit(c, &tty->process_char_map) || tty->lnext) {
482 finish_erasing(tty);
483 tty->lnext = 0;
484 if (L_ECHO(tty)) {
485 if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
486 put_char('\a', tty); /* beep if no space */
487 return;
489 /* Record the column of first canon char. */
490 if (tty->canon_head == tty->read_head)
491 tty->canon_column = tty->column;
492 echo_char(c, tty);
494 if (I_PARMRK(tty) && c == (unsigned char) '\377')
495 put_tty_queue(c, tty);
496 put_tty_queue(c, tty);
497 return;
500 if (c == '\r') {
501 if (I_IGNCR(tty))
502 return;
503 if (I_ICRNL(tty))
504 c = '\n';
505 } else if (c == '\n' && I_INLCR(tty))
506 c = '\r';
507 if (I_IXON(tty)) {
508 if (c == START_CHAR(tty)) {
509 start_tty(tty);
510 return;
512 if (c == STOP_CHAR(tty)) {
513 stop_tty(tty);
514 return;
517 if (L_ISIG(tty)) {
518 int signal;
519 signal = SIGINT;
520 if (c == INTR_CHAR(tty))
521 goto send_signal;
522 signal = SIGQUIT;
523 if (c == QUIT_CHAR(tty))
524 goto send_signal;
525 signal = SIGTSTP;
526 if (c == SUSP_CHAR(tty)) {
527 send_signal:
528 isig(signal, tty, 0);
529 return;
532 if (tty->icanon) {
533 if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
534 (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
535 eraser(c, tty);
536 return;
538 if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
539 tty->lnext = 1;
540 if (L_ECHO(tty)) {
541 finish_erasing(tty);
542 if (L_ECHOCTL(tty)) {
543 put_char('^', tty);
544 put_char('\b', tty);
547 return;
549 if (c == REPRINT_CHAR(tty) && L_ECHO(tty) &&
550 L_IEXTEN(tty)) {
551 unsigned long tail = tty->canon_head;
553 finish_erasing(tty);
554 echo_char(c, tty);
555 opost('\n', tty);
556 while (tail != tty->read_head) {
557 echo_char(tty->read_buf[tail], tty);
558 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
560 return;
562 if (c == '\n') {
563 if (L_ECHO(tty) || L_ECHONL(tty)) {
564 if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
565 put_char('\a', tty);
566 return;
568 opost('\n', tty);
570 goto handle_newline;
572 if (c == EOF_CHAR(tty)) {
573 if (tty->canon_head != tty->read_head)
574 set_bit(TTY_PUSH, &tty->flags);
575 c = __DISABLED_CHAR;
576 goto handle_newline;
578 if ((c == EOL_CHAR(tty)) ||
579 (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
581 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
583 if (L_ECHO(tty)) {
584 if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
585 put_char('\a', tty);
586 return;
588 /* Record the column of first canon char. */
589 if (tty->canon_head == tty->read_head)
590 tty->canon_column = tty->column;
591 echo_char(c, tty);
594 * XXX does PARMRK doubling happen for
595 * EOL_CHAR and EOL2_CHAR?
597 if (I_PARMRK(tty) && c == (unsigned char) '\377')
598 put_tty_queue(c, tty);
600 handle_newline:
601 set_bit(tty->read_head, &tty->read_flags);
602 put_tty_queue(c, tty);
603 tty->canon_head = tty->read_head;
604 tty->canon_data++;
605 if (tty->fasync)
606 kill_fasync(tty->fasync, SIGIO);
607 if (tty->read_wait)
608 wake_up_interruptible(&tty->read_wait);
609 return;
613 finish_erasing(tty);
614 if (L_ECHO(tty)) {
615 if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
616 put_char('\a', tty); /* beep if no space */
617 return;
619 if (c == '\n')
620 opost('\n', tty);
621 else {
622 /* Record the column of first canon char. */
623 if (tty->canon_head == tty->read_head)
624 tty->canon_column = tty->column;
625 echo_char(c, tty);
629 if (I_PARMRK(tty) && c == (unsigned char) '\377')
630 put_tty_queue(c, tty);
632 put_tty_queue(c, tty);
635 static int n_tty_receive_room(struct tty_struct *tty)
637 int left = N_TTY_BUF_SIZE - tty->read_cnt - 1;
640 * If we are doing input canonicalization, and there are no
641 * pending newlines, let characters through without limit, so
642 * that erase characters will be handled. Other excess
643 * characters will be beeped.
645 if (tty->icanon && !tty->canon_data)
646 return N_TTY_BUF_SIZE;
648 if (left > 0)
649 return left;
650 return 0;
653 static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
654 char *fp, int count)
656 const unsigned char *p;
657 char *f, flags = TTY_NORMAL;
658 int i;
659 char buf[64];
661 if (!tty->read_buf)
662 return;
664 if (tty->real_raw) {
665 i = MIN(count, MIN(N_TTY_BUF_SIZE - tty->read_cnt,
666 N_TTY_BUF_SIZE - tty->read_head));
667 memcpy(tty->read_buf + tty->read_head, cp, i);
668 tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
669 tty->read_cnt += i;
670 cp += i;
671 count -= i;
673 i = MIN(count, MIN(N_TTY_BUF_SIZE - tty->read_cnt,
674 N_TTY_BUF_SIZE - tty->read_head));
675 memcpy(tty->read_buf + tty->read_head, cp, i);
676 tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
677 tty->read_cnt += i;
678 } else {
679 for (i=count, p = cp, f = fp; i; i--, p++) {
680 if (f)
681 flags = *f++;
682 switch (flags) {
683 case TTY_NORMAL:
684 n_tty_receive_char(tty, *p);
685 break;
686 case TTY_BREAK:
687 n_tty_receive_break(tty);
688 break;
689 case TTY_PARITY:
690 case TTY_FRAME:
691 n_tty_receive_parity_error(tty, *p);
692 break;
693 case TTY_OVERRUN:
694 n_tty_receive_overrun(tty);
695 break;
696 default:
697 printk("%s: unknown flag %d\n",
698 tty_name(tty, buf), flags);
699 break;
702 if (tty->driver.flush_chars)
703 tty->driver.flush_chars(tty);
706 if (!tty->icanon && (tty->read_cnt >= tty->minimum_to_wake)) {
707 if (tty->fasync)
708 kill_fasync(tty->fasync, SIGIO);
709 if (tty->read_wait)
710 wake_up_interruptible(&tty->read_wait);
714 * Check the remaining room for the input canonicalization
715 * mode. We don't want to throttle the driver if we're in
716 * canonical mode and don't have a newline yet!
718 if (n_tty_receive_room(tty) < TTY_THRESHOLD_THROTTLE) {
719 /* check TTY_THROTTLED first so it indicates our state */
720 if (!test_and_set_bit(TTY_THROTTLED, &tty->flags) &&
721 tty->driver.throttle)
722 tty->driver.throttle(tty);
726 int is_ignored(int sig)
728 return (sigismember(&current->blocked, sig) ||
729 current->sig->action[sig-1].sa.sa_handler == SIG_IGN);
732 static void n_tty_set_termios(struct tty_struct *tty, struct termios * old)
734 if (!tty)
735 return;
737 tty->icanon = (L_ICANON(tty) != 0);
738 if (test_bit(TTY_HW_COOK_IN, &tty->flags)) {
739 tty->raw = 1;
740 tty->real_raw = 1;
741 return;
743 if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
744 I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
745 I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
746 I_PARMRK(tty)) {
747 cli();
748 memset(tty->process_char_map, 0, 256/8);
750 if (I_IGNCR(tty) || I_ICRNL(tty))
751 set_bit('\r', &tty->process_char_map);
752 if (I_INLCR(tty))
753 set_bit('\n', &tty->process_char_map);
755 if (L_ICANON(tty)) {
756 set_bit(ERASE_CHAR(tty), &tty->process_char_map);
757 set_bit(KILL_CHAR(tty), &tty->process_char_map);
758 set_bit(EOF_CHAR(tty), &tty->process_char_map);
759 set_bit('\n', &tty->process_char_map);
760 set_bit(EOL_CHAR(tty), &tty->process_char_map);
761 if (L_IEXTEN(tty)) {
762 set_bit(WERASE_CHAR(tty),
763 &tty->process_char_map);
764 set_bit(LNEXT_CHAR(tty),
765 &tty->process_char_map);
766 set_bit(EOL2_CHAR(tty),
767 &tty->process_char_map);
768 if (L_ECHO(tty))
769 set_bit(REPRINT_CHAR(tty),
770 &tty->process_char_map);
773 if (I_IXON(tty)) {
774 set_bit(START_CHAR(tty), &tty->process_char_map);
775 set_bit(STOP_CHAR(tty), &tty->process_char_map);
777 if (L_ISIG(tty)) {
778 set_bit(INTR_CHAR(tty), &tty->process_char_map);
779 set_bit(QUIT_CHAR(tty), &tty->process_char_map);
780 set_bit(SUSP_CHAR(tty), &tty->process_char_map);
782 clear_bit(__DISABLED_CHAR, &tty->process_char_map);
783 sti();
784 tty->raw = 0;
785 tty->real_raw = 0;
786 } else {
787 tty->raw = 1;
788 if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
789 (I_IGNPAR(tty) || !I_INPCK(tty)) &&
790 (tty->driver.flags & TTY_DRIVER_REAL_RAW))
791 tty->real_raw = 1;
792 else
793 tty->real_raw = 0;
797 static void n_tty_close(struct tty_struct *tty)
799 n_tty_flush_buffer(tty);
800 if (tty->read_buf) {
801 free_page((unsigned long) tty->read_buf);
802 tty->read_buf = 0;
806 static int n_tty_open(struct tty_struct *tty)
808 if (!tty)
809 return -EINVAL;
811 if (!tty->read_buf) {
812 tty->read_buf = (unsigned char *)
813 get_free_page(in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
814 if (!tty->read_buf)
815 return -ENOMEM;
817 memset(tty->read_buf, 0, N_TTY_BUF_SIZE);
818 reset_buffer_flags(tty);
819 tty->column = 0;
820 n_tty_set_termios(tty, 0);
821 tty->minimum_to_wake = 1;
822 tty->closing = 0;
823 return 0;
826 static inline int input_available_p(struct tty_struct *tty, int amt)
828 if (tty->icanon) {
829 if (tty->canon_data)
830 return 1;
831 } else if (tty->read_cnt >= (amt ? amt : 1))
832 return 1;
834 return 0;
838 * Helper function to speed up read_chan. It is only called when
839 * ICANON is off; it copies characters straight from the tty queue to
840 * user space directly. It can be profitably called twice; once to
841 * drain the space from the tail pointer to the (physical) end of the
842 * buffer, and once to drain the space from the (physical) beginning of
843 * the buffer to head pointer.
845 static inline int copy_from_read_buf(struct tty_struct *tty,
846 unsigned char **b,
847 size_t *nr)
850 int retval;
851 ssize_t n;
853 retval = 0;
854 n = MIN(*nr, MIN(tty->read_cnt, N_TTY_BUF_SIZE - tty->read_tail));
855 if (n) {
856 retval = copy_to_user(*b, &tty->read_buf[tty->read_tail], n);
857 n -= retval;
858 tty->read_tail = (tty->read_tail + n) & (N_TTY_BUF_SIZE-1);
859 tty->read_cnt -= n;
860 *b += n;
861 *nr -= n;
863 return retval;
866 static ssize_t read_chan(struct tty_struct *tty, struct file *file,
867 unsigned char *buf, size_t nr)
869 unsigned char *b = buf;
870 struct wait_queue wait = { current, NULL };
871 int c;
872 int minimum, time;
873 ssize_t retval = 0;
874 ssize_t size;
876 do_it_again:
878 if (!tty->read_buf) {
879 printk("n_tty_read_chan: called with read_buf == NULL?!?\n");
880 return -EIO;
883 /* Job control check -- must be done at start and after
884 every sleep (POSIX.1 7.1.1.4). */
885 /* NOTE: not yet done after every sleep pending a thorough
886 check of the logic of this change. -- jlc */
887 /* don't stop on /dev/console */
888 if (file->f_dentry->d_inode->i_rdev != CONSOLE_DEV &&
889 file->f_dentry->d_inode->i_rdev != SYSCONS_DEV &&
890 current->tty == tty) {
891 if (tty->pgrp <= 0)
892 printk("read_chan: tty->pgrp <= 0!\n");
893 else if (current->pgrp != tty->pgrp) {
894 if (is_ignored(SIGTTIN) ||
895 is_orphaned_pgrp(current->pgrp))
896 return -EIO;
897 kill_pg(current->pgrp, SIGTTIN, 1);
898 return -ERESTARTSYS;
902 minimum = time = 0;
903 current->timeout = (unsigned long) -1;
904 if (!tty->icanon) {
905 time = (HZ / 10) * TIME_CHAR(tty);
906 minimum = MIN_CHAR(tty);
907 if (minimum) {
908 if (time)
909 tty->minimum_to_wake = 1;
910 else if (!waitqueue_active(&tty->read_wait) ||
911 (tty->minimum_to_wake > minimum))
912 tty->minimum_to_wake = minimum;
913 } else {
914 current->timeout = 0;
915 if (time) {
916 current->timeout = time + jiffies;
917 time = 0;
919 tty->minimum_to_wake = minimum = 1;
923 add_wait_queue(&tty->read_wait, &wait);
925 disable_bh(TQUEUE_BH);
926 while (nr) {
927 /* First test for status change. */
928 if (tty->packet && tty->link->ctrl_status) {
929 if (b != buf)
930 break;
931 put_user(tty->link->ctrl_status, b++);
932 nr--;
933 tty->link->ctrl_status = 0;
934 break;
936 /* This statement must be first before checking for input
937 so that any interrupt will set the state back to
938 TASK_RUNNING. */
939 current->state = TASK_INTERRUPTIBLE;
941 if (((minimum - (b - buf)) < tty->minimum_to_wake) &&
942 ((minimum - (b - buf)) >= 1))
943 tty->minimum_to_wake = (minimum - (b - buf));
945 if (!input_available_p(tty, 0)) {
946 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
947 retval = -EIO;
948 break;
950 if (tty_hung_up_p(file))
951 break;
952 if (!current->timeout)
953 break;
954 if (file->f_flags & O_NONBLOCK) {
955 retval = -EAGAIN;
956 break;
958 if (signal_pending(current)) {
959 retval = -ERESTARTSYS;
960 break;
962 enable_bh(TQUEUE_BH);
963 schedule();
964 disable_bh(TQUEUE_BH);
965 continue;
967 current->state = TASK_RUNNING;
969 /* Deal with packet mode. */
970 if (tty->packet && b == buf) {
971 put_user(TIOCPKT_DATA, b++);
972 nr--;
975 if (tty->icanon) {
976 /* N.B. avoid overrun if nr == 0 */
977 while (nr && tty->read_cnt) {
978 int eol;
980 eol = test_and_clear_bit(tty->read_tail,
981 &tty->read_flags);
982 c = tty->read_buf[tty->read_tail];
983 tty->read_tail = ((tty->read_tail+1) &
984 (N_TTY_BUF_SIZE-1));
985 tty->read_cnt--;
987 if (!eol || (c != __DISABLED_CHAR)) {
988 put_user(c, b++);
989 nr--;
991 if (eol) {
992 /* this test should be redundant:
993 * we shouldn't be reading data if
994 * canon_data is 0
996 if (--tty->canon_data < 0)
997 tty->canon_data = 0;
998 break;
1001 } else {
1002 int uncopied;
1003 uncopied = copy_from_read_buf(tty, &b, &nr);
1004 uncopied += copy_from_read_buf(tty, &b, &nr);
1005 if (uncopied) {
1006 retval = -EFAULT;
1007 break;
1011 /* If there is enough space in the read buffer now, let the
1012 * low-level driver know. We use n_tty_chars_in_buffer() to
1013 * check the buffer, as it now knows about canonical mode.
1014 * Otherwise, if the driver is throttled and the line is
1015 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
1016 * we won't get any more characters.
1018 if (n_tty_chars_in_buffer(tty) <= TTY_THRESHOLD_UNTHROTTLE)
1019 check_unthrottle(tty);
1021 if (b - buf >= minimum)
1022 break;
1023 if (time)
1024 current->timeout = time + jiffies;
1026 enable_bh(TQUEUE_BH);
1027 remove_wait_queue(&tty->read_wait, &wait);
1029 if (!waitqueue_active(&tty->read_wait))
1030 tty->minimum_to_wake = minimum;
1032 current->state = TASK_RUNNING;
1033 current->timeout = 0;
1034 size = b - buf;
1035 if (size) {
1036 retval = size;
1037 if (nr)
1038 clear_bit(TTY_PUSH, &tty->flags);
1039 } else if (test_and_clear_bit(TTY_PUSH, &tty->flags))
1040 goto do_it_again;
1042 return retval;
1045 static ssize_t write_chan(struct tty_struct * tty, struct file * file,
1046 const unsigned char * buf, size_t nr)
1048 const unsigned char *b = buf;
1049 struct wait_queue wait = { current, NULL };
1050 int c;
1051 ssize_t retval = 0;
1053 /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
1054 if (L_TOSTOP(tty) &&
1055 file->f_dentry->d_inode->i_rdev != CONSOLE_DEV &&
1056 file->f_dentry->d_inode->i_rdev != SYSCONS_DEV) {
1057 retval = tty_check_change(tty);
1058 if (retval)
1059 return retval;
1062 add_wait_queue(&tty->write_wait, &wait);
1063 while (1) {
1064 current->state = TASK_INTERRUPTIBLE;
1065 if (signal_pending(current)) {
1066 retval = -ERESTARTSYS;
1067 break;
1069 if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
1070 retval = -EIO;
1071 break;
1073 if (O_OPOST(tty) && !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
1074 while (nr > 0) {
1075 ssize_t num = opost_block(tty, b, nr);
1076 if (num < 0) {
1077 retval = num;
1078 goto break_out;
1080 b += num;
1081 nr -= num;
1082 if (nr == 0)
1083 break;
1084 get_user(c, b);
1085 if (opost(c, tty) < 0)
1086 break;
1087 b++; nr--;
1089 if (tty->driver.flush_chars)
1090 tty->driver.flush_chars(tty);
1091 } else {
1092 c = tty->driver.write(tty, 1, b, nr);
1093 if (c < 0) {
1094 retval = c;
1095 goto break_out;
1097 b += c;
1098 nr -= c;
1100 if (!nr)
1101 break;
1102 if (file->f_flags & O_NONBLOCK) {
1103 retval = -EAGAIN;
1104 break;
1106 schedule();
1108 break_out:
1109 current->state = TASK_RUNNING;
1110 remove_wait_queue(&tty->write_wait, &wait);
1111 return (b - buf) ? b - buf : retval;
1114 static unsigned int normal_poll(struct tty_struct * tty, struct file * file, poll_table *wait)
1116 unsigned int mask = 0;
1118 poll_wait(file, &tty->read_wait, wait);
1119 poll_wait(file, &tty->write_wait, wait);
1120 if (input_available_p(tty, TIME_CHAR(tty) ? 0 : MIN_CHAR(tty)))
1121 mask |= POLLIN | POLLRDNORM;
1122 if (tty->packet && tty->link->ctrl_status)
1123 mask |= POLLPRI | POLLIN | POLLRDNORM;
1124 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
1125 mask |= POLLHUP;
1126 if (tty_hung_up_p(file))
1127 mask |= POLLHUP;
1128 if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) {
1129 if (MIN_CHAR(tty) && !TIME_CHAR(tty))
1130 tty->minimum_to_wake = MIN_CHAR(tty);
1131 else
1132 tty->minimum_to_wake = 1;
1134 if (tty->driver.chars_in_buffer(tty) < WAKEUP_CHARS)
1135 mask |= POLLOUT | POLLWRNORM;
1136 return mask;
1139 struct tty_ldisc tty_ldisc_N_TTY = {
1140 TTY_LDISC_MAGIC, /* magic */
1141 "n_tty", /* name */
1142 0, /* num */
1143 0, /* flags */
1144 n_tty_open, /* open */
1145 n_tty_close, /* close */
1146 n_tty_flush_buffer, /* flush_buffer */
1147 n_tty_chars_in_buffer, /* chars_in_buffer */
1148 read_chan, /* read */
1149 write_chan, /* write */
1150 n_tty_ioctl, /* ioctl */
1151 n_tty_set_termios, /* set_termios */
1152 normal_poll, /* poll */
1153 n_tty_receive_buf, /* receive_buf */
1154 n_tty_receive_room, /* receive_room */
1155 0 /* write_wakeup */