Linux 2.3.7pre1
[davej-history.git] / drivers / char / n_tty.c
blobaf4daecfbf6a4bcb560d925af34648707026f45e
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 (tty->count &&
77 test_and_clear_bit(TTY_THROTTLED, &tty->flags) &&
78 tty->driver.unthrottle)
79 tty->driver.unthrottle(tty);
83 * Reset the read buffer counters, clear the flags,
84 * and make sure the driver is unthrottled. Called
85 * from n_tty_open() and n_tty_flush_buffer().
87 static void reset_buffer_flags(struct tty_struct *tty)
89 tty->read_head = tty->read_tail = tty->read_cnt = 0;
90 tty->canon_head = tty->canon_data = tty->erasing = 0;
91 memset(&tty->read_flags, 0, sizeof tty->read_flags);
92 check_unthrottle(tty);
96 * Flush the input buffer
98 void n_tty_flush_buffer(struct tty_struct * tty)
100 /* clear everything and unthrottle the driver */
101 reset_buffer_flags(tty);
103 if (!tty->link)
104 return;
106 if (tty->link->packet) {
107 tty->ctrl_status |= TIOCPKT_FLUSHREAD;
108 wake_up_interruptible(&tty->link->read_wait);
113 * Return number of characters buffered to be delivered to user
115 ssize_t n_tty_chars_in_buffer(struct tty_struct *tty)
117 if (tty->icanon) {
118 if (!tty->canon_data) return 0;
120 return (tty->canon_head > tty->read_tail) ?
121 tty->canon_head - tty->read_tail :
122 tty->canon_head + (N_TTY_BUF_SIZE - tty->read_tail);
124 return tty->read_cnt;
128 * Perform OPOST processing. Returns -1 when the output device is
129 * full and the character must be retried.
131 static int opost(unsigned char c, struct tty_struct *tty)
133 int space, spaces;
135 space = tty->driver.write_room(tty);
136 if (!space)
137 return -1;
139 if (O_OPOST(tty)) {
140 switch (c) {
141 case '\n':
142 if (O_ONLRET(tty))
143 tty->column = 0;
144 if (O_ONLCR(tty)) {
145 if (space < 2)
146 return -1;
147 tty->driver.put_char(tty, '\r');
148 tty->column = 0;
150 tty->canon_column = tty->column;
151 break;
152 case '\r':
153 if (O_ONOCR(tty) && tty->column == 0)
154 return 0;
155 if (O_OCRNL(tty)) {
156 c = '\n';
157 if (O_ONLRET(tty))
158 tty->canon_column = tty->column = 0;
159 break;
161 tty->canon_column = tty->column = 0;
162 break;
163 case '\t':
164 spaces = 8 - (tty->column & 7);
165 if (O_TABDLY(tty) == XTABS) {
166 if (space < spaces)
167 return -1;
168 tty->column += spaces;
169 tty->driver.write(tty, 0, " ", spaces);
170 return 0;
172 tty->column += spaces;
173 break;
174 case '\b':
175 if (tty->column > 0)
176 tty->column--;
177 break;
178 default:
179 if (O_OLCUC(tty))
180 c = toupper(c);
181 if (!iscntrl(c))
182 tty->column++;
183 break;
186 tty->driver.put_char(tty, c);
187 return 0;
191 * opost_block --- to speed up block console writes, among other
192 * things.
194 static ssize_t opost_block(struct tty_struct * tty,
195 const unsigned char * inbuf, unsigned int nr)
197 char buf[80];
198 int space;
199 int i;
200 char *cp;
202 space = tty->driver.write_room(tty);
203 if (!space)
204 return 0;
205 if (nr > space)
206 nr = space;
207 if (nr > sizeof(buf))
208 nr = sizeof(buf);
209 nr -= copy_from_user(buf, inbuf, nr);
210 if (!nr)
211 return 0;
213 for (i = 0, cp = buf; i < nr; i++, cp++) {
214 switch (*cp) {
215 case '\n':
216 if (O_ONLRET(tty))
217 tty->column = 0;
218 if (O_ONLCR(tty))
219 goto break_out;
220 tty->canon_column = tty->column;
221 break;
222 case '\r':
223 if (O_ONOCR(tty) && tty->column == 0)
224 goto break_out;
225 if (O_OCRNL(tty)) {
226 *cp = '\n';
227 if (O_ONLRET(tty))
228 tty->canon_column = tty->column = 0;
229 break;
231 tty->canon_column = tty->column = 0;
232 break;
233 case '\t':
234 goto break_out;
235 case '\b':
236 if (tty->column > 0)
237 tty->column--;
238 break;
239 default:
240 if (O_OLCUC(tty))
241 *cp = toupper(*cp);
242 if (!iscntrl(*cp))
243 tty->column++;
244 break;
247 break_out:
248 if (tty->driver.flush_chars)
249 tty->driver.flush_chars(tty);
250 i = tty->driver.write(tty, 0, buf, i);
251 return i;
256 static inline void put_char(unsigned char c, struct tty_struct *tty)
258 tty->driver.put_char(tty, c);
261 /* Must be called only when L_ECHO(tty) is true. */
263 static void echo_char(unsigned char c, struct tty_struct *tty)
265 if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t') {
266 put_char('^', tty);
267 put_char(c ^ 0100, tty);
268 tty->column += 2;
269 } else
270 opost(c, tty);
273 static inline void finish_erasing(struct tty_struct *tty)
275 if (tty->erasing) {
276 put_char('/', tty);
277 tty->column += 2;
278 tty->erasing = 0;
282 static void eraser(unsigned char c, struct tty_struct *tty)
284 enum { ERASE, WERASE, KILL } kill_type;
285 int head, seen_alnums;
287 if (tty->read_head == tty->canon_head) {
288 /* opost('\a', tty); */ /* what do you think? */
289 return;
291 if (c == ERASE_CHAR(tty))
292 kill_type = ERASE;
293 else if (c == WERASE_CHAR(tty))
294 kill_type = WERASE;
295 else {
296 if (!L_ECHO(tty)) {
297 tty->read_cnt -= ((tty->read_head - tty->canon_head) &
298 (N_TTY_BUF_SIZE - 1));
299 tty->read_head = tty->canon_head;
300 return;
302 if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
303 tty->read_cnt -= ((tty->read_head - tty->canon_head) &
304 (N_TTY_BUF_SIZE - 1));
305 tty->read_head = tty->canon_head;
306 finish_erasing(tty);
307 echo_char(KILL_CHAR(tty), tty);
308 /* Add a newline if ECHOK is on and ECHOKE is off. */
309 if (L_ECHOK(tty))
310 opost('\n', tty);
311 return;
313 kill_type = KILL;
316 seen_alnums = 0;
317 while (tty->read_head != tty->canon_head) {
318 head = (tty->read_head - 1) & (N_TTY_BUF_SIZE-1);
319 c = tty->read_buf[head];
320 if (kill_type == WERASE) {
321 /* Equivalent to BSD's ALTWERASE. */
322 if (isalnum(c) || c == '_')
323 seen_alnums++;
324 else if (seen_alnums)
325 break;
327 tty->read_head = head;
328 tty->read_cnt--;
329 if (L_ECHO(tty)) {
330 if (L_ECHOPRT(tty)) {
331 if (!tty->erasing) {
332 put_char('\\', tty);
333 tty->column++;
334 tty->erasing = 1;
336 echo_char(c, tty);
337 } else if (kill_type == ERASE && !L_ECHOE(tty)) {
338 echo_char(ERASE_CHAR(tty), tty);
339 } else if (c == '\t') {
340 unsigned int col = tty->canon_column;
341 unsigned long tail = tty->canon_head;
343 /* Find the column of the last char. */
344 while (tail != tty->read_head) {
345 c = tty->read_buf[tail];
346 if (c == '\t')
347 col = (col | 7) + 1;
348 else if (iscntrl(c)) {
349 if (L_ECHOCTL(tty))
350 col += 2;
351 } else
352 col++;
353 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
356 /* should never happen */
357 if (tty->column > 0x80000000)
358 tty->column = 0;
360 /* Now backup to that column. */
361 while (tty->column > col) {
362 /* Can't use opost here. */
363 put_char('\b', tty);
364 if (tty->column > 0)
365 tty->column--;
367 } else {
368 if (iscntrl(c) && L_ECHOCTL(tty)) {
369 put_char('\b', tty);
370 put_char(' ', tty);
371 put_char('\b', tty);
372 if (tty->column > 0)
373 tty->column--;
375 if (!iscntrl(c) || L_ECHOCTL(tty)) {
376 put_char('\b', tty);
377 put_char(' ', tty);
378 put_char('\b', tty);
379 if (tty->column > 0)
380 tty->column--;
384 if (kill_type == ERASE)
385 break;
387 if (tty->read_head == tty->canon_head)
388 finish_erasing(tty);
391 static inline void isig(int sig, struct tty_struct *tty, int flush)
393 if (tty->pgrp > 0)
394 kill_pg(tty->pgrp, sig, 1);
395 if (flush || !L_NOFLSH(tty)) {
396 n_tty_flush_buffer(tty);
397 if (tty->driver.flush_buffer)
398 tty->driver.flush_buffer(tty);
402 static inline void n_tty_receive_break(struct tty_struct *tty)
404 if (I_IGNBRK(tty))
405 return;
406 if (I_BRKINT(tty)) {
407 isig(SIGINT, tty, 1);
408 return;
410 if (I_PARMRK(tty)) {
411 put_tty_queue('\377', tty);
412 put_tty_queue('\0', tty);
414 put_tty_queue('\0', tty);
415 wake_up_interruptible(&tty->read_wait);
418 static inline void n_tty_receive_overrun(struct tty_struct *tty)
420 char buf[64];
422 tty->num_overrun++;
423 if (time_before(tty->overrun_time, jiffies - HZ)) {
424 printk("%s: %d input overrun(s)\n", tty_name(tty, buf),
425 tty->num_overrun);
426 tty->overrun_time = jiffies;
427 tty->num_overrun = 0;
431 static inline void n_tty_receive_parity_error(struct tty_struct *tty,
432 unsigned char c)
434 if (I_IGNPAR(tty)) {
435 return;
437 if (I_PARMRK(tty)) {
438 put_tty_queue('\377', tty);
439 put_tty_queue('\0', tty);
440 put_tty_queue(c, tty);
441 } else if (I_INPCK(tty))
442 put_tty_queue('\0', tty);
443 else
444 put_tty_queue(c, tty);
445 wake_up_interruptible(&tty->read_wait);
448 static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
450 if (tty->raw) {
451 put_tty_queue(c, tty);
452 return;
455 if (tty->stopped && !tty->flow_stopped &&
456 I_IXON(tty) && I_IXANY(tty)) {
457 start_tty(tty);
458 return;
461 if (I_ISTRIP(tty))
462 c &= 0x7f;
463 if (I_IUCLC(tty) && L_IEXTEN(tty))
464 c=tolower(c);
466 if (tty->closing) {
467 if (I_IXON(tty)) {
468 if (c == START_CHAR(tty))
469 start_tty(tty);
470 else if (c == STOP_CHAR(tty))
471 stop_tty(tty);
473 return;
477 * If the previous character was LNEXT, or we know that this
478 * character is not one of the characters that we'll have to
479 * handle specially, do shortcut processing to speed things
480 * up.
482 if (!test_bit(c, &tty->process_char_map) || tty->lnext) {
483 finish_erasing(tty);
484 tty->lnext = 0;
485 if (L_ECHO(tty)) {
486 if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
487 put_char('\a', tty); /* beep if no space */
488 return;
490 /* Record the column of first canon char. */
491 if (tty->canon_head == tty->read_head)
492 tty->canon_column = tty->column;
493 echo_char(c, tty);
495 if (I_PARMRK(tty) && c == (unsigned char) '\377')
496 put_tty_queue(c, tty);
497 put_tty_queue(c, tty);
498 return;
501 if (c == '\r') {
502 if (I_IGNCR(tty))
503 return;
504 if (I_ICRNL(tty))
505 c = '\n';
506 } else if (c == '\n' && I_INLCR(tty))
507 c = '\r';
508 if (I_IXON(tty)) {
509 if (c == START_CHAR(tty)) {
510 start_tty(tty);
511 return;
513 if (c == STOP_CHAR(tty)) {
514 stop_tty(tty);
515 return;
518 if (L_ISIG(tty)) {
519 int signal;
520 signal = SIGINT;
521 if (c == INTR_CHAR(tty))
522 goto send_signal;
523 signal = SIGQUIT;
524 if (c == QUIT_CHAR(tty))
525 goto send_signal;
526 signal = SIGTSTP;
527 if (c == SUSP_CHAR(tty)) {
528 send_signal:
529 isig(signal, tty, 0);
530 return;
533 if (tty->icanon) {
534 if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
535 (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
536 eraser(c, tty);
537 return;
539 if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
540 tty->lnext = 1;
541 if (L_ECHO(tty)) {
542 finish_erasing(tty);
543 if (L_ECHOCTL(tty)) {
544 put_char('^', tty);
545 put_char('\b', tty);
548 return;
550 if (c == REPRINT_CHAR(tty) && L_ECHO(tty) &&
551 L_IEXTEN(tty)) {
552 unsigned long tail = tty->canon_head;
554 finish_erasing(tty);
555 echo_char(c, tty);
556 opost('\n', tty);
557 while (tail != tty->read_head) {
558 echo_char(tty->read_buf[tail], tty);
559 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
561 return;
563 if (c == '\n') {
564 if (L_ECHO(tty) || L_ECHONL(tty)) {
565 if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
566 put_char('\a', tty);
567 return;
569 opost('\n', tty);
571 goto handle_newline;
573 if (c == EOF_CHAR(tty)) {
574 if (tty->canon_head != tty->read_head)
575 set_bit(TTY_PUSH, &tty->flags);
576 c = __DISABLED_CHAR;
577 goto handle_newline;
579 if ((c == EOL_CHAR(tty)) ||
580 (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
582 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
584 if (L_ECHO(tty)) {
585 if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
586 put_char('\a', tty);
587 return;
589 /* Record the column of first canon char. */
590 if (tty->canon_head == tty->read_head)
591 tty->canon_column = tty->column;
592 echo_char(c, tty);
595 * XXX does PARMRK doubling happen for
596 * EOL_CHAR and EOL2_CHAR?
598 if (I_PARMRK(tty) && c == (unsigned char) '\377')
599 put_tty_queue(c, tty);
601 handle_newline:
602 set_bit(tty->read_head, &tty->read_flags);
603 put_tty_queue(c, tty);
604 tty->canon_head = tty->read_head;
605 tty->canon_data++;
606 if (tty->fasync)
607 kill_fasync(tty->fasync, SIGIO);
608 if (waitqueue_active(&tty->read_wait))
609 wake_up_interruptible(&tty->read_wait);
610 return;
614 finish_erasing(tty);
615 if (L_ECHO(tty)) {
616 if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
617 put_char('\a', tty); /* beep if no space */
618 return;
620 if (c == '\n')
621 opost('\n', tty);
622 else {
623 /* Record the column of first canon char. */
624 if (tty->canon_head == tty->read_head)
625 tty->canon_column = tty->column;
626 echo_char(c, tty);
630 if (I_PARMRK(tty) && c == (unsigned char) '\377')
631 put_tty_queue(c, tty);
633 put_tty_queue(c, tty);
636 static int n_tty_receive_room(struct tty_struct *tty)
638 int left = N_TTY_BUF_SIZE - tty->read_cnt - 1;
641 * If we are doing input canonicalization, and there are no
642 * pending newlines, let characters through without limit, so
643 * that erase characters will be handled. Other excess
644 * characters will be beeped.
646 if (tty->icanon && !tty->canon_data)
647 return N_TTY_BUF_SIZE;
649 if (left > 0)
650 return left;
651 return 0;
654 static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
655 char *fp, int count)
657 const unsigned char *p;
658 char *f, flags = TTY_NORMAL;
659 int i;
660 char buf[64];
662 if (!tty->read_buf)
663 return;
665 if (tty->real_raw) {
666 i = MIN(count, MIN(N_TTY_BUF_SIZE - tty->read_cnt,
667 N_TTY_BUF_SIZE - tty->read_head));
668 memcpy(tty->read_buf + tty->read_head, cp, i);
669 tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
670 tty->read_cnt += i;
671 cp += i;
672 count -= i;
674 i = MIN(count, MIN(N_TTY_BUF_SIZE - tty->read_cnt,
675 N_TTY_BUF_SIZE - tty->read_head));
676 memcpy(tty->read_buf + tty->read_head, cp, i);
677 tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
678 tty->read_cnt += i;
679 } else {
680 for (i=count, p = cp, f = fp; i; i--, p++) {
681 if (f)
682 flags = *f++;
683 switch (flags) {
684 case TTY_NORMAL:
685 n_tty_receive_char(tty, *p);
686 break;
687 case TTY_BREAK:
688 n_tty_receive_break(tty);
689 break;
690 case TTY_PARITY:
691 case TTY_FRAME:
692 n_tty_receive_parity_error(tty, *p);
693 break;
694 case TTY_OVERRUN:
695 n_tty_receive_overrun(tty);
696 break;
697 default:
698 printk("%s: unknown flag %d\n",
699 tty_name(tty, buf), flags);
700 break;
703 if (tty->driver.flush_chars)
704 tty->driver.flush_chars(tty);
707 if (!tty->icanon && (tty->read_cnt >= tty->minimum_to_wake)) {
708 if (tty->fasync)
709 kill_fasync(tty->fasync, SIGIO);
710 if (waitqueue_active(&tty->read_wait))
711 wake_up_interruptible(&tty->read_wait);
715 * Check the remaining room for the input canonicalization
716 * mode. We don't want to throttle the driver if we're in
717 * canonical mode and don't have a newline yet!
719 if (n_tty_receive_room(tty) < TTY_THRESHOLD_THROTTLE) {
720 /* check TTY_THROTTLED first so it indicates our state */
721 if (!test_and_set_bit(TTY_THROTTLED, &tty->flags) &&
722 tty->driver.throttle)
723 tty->driver.throttle(tty);
727 int is_ignored(int sig)
729 return (sigismember(&current->blocked, sig) ||
730 current->sig->action[sig-1].sa.sa_handler == SIG_IGN);
733 static void n_tty_set_termios(struct tty_struct *tty, struct termios * old)
735 if (!tty)
736 return;
738 tty->icanon = (L_ICANON(tty) != 0);
739 if (test_bit(TTY_HW_COOK_IN, &tty->flags)) {
740 tty->raw = 1;
741 tty->real_raw = 1;
742 return;
744 if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
745 I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
746 I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
747 I_PARMRK(tty)) {
748 cli();
749 memset(tty->process_char_map, 0, 256/8);
751 if (I_IGNCR(tty) || I_ICRNL(tty))
752 set_bit('\r', &tty->process_char_map);
753 if (I_INLCR(tty))
754 set_bit('\n', &tty->process_char_map);
756 if (L_ICANON(tty)) {
757 set_bit(ERASE_CHAR(tty), &tty->process_char_map);
758 set_bit(KILL_CHAR(tty), &tty->process_char_map);
759 set_bit(EOF_CHAR(tty), &tty->process_char_map);
760 set_bit('\n', &tty->process_char_map);
761 set_bit(EOL_CHAR(tty), &tty->process_char_map);
762 if (L_IEXTEN(tty)) {
763 set_bit(WERASE_CHAR(tty),
764 &tty->process_char_map);
765 set_bit(LNEXT_CHAR(tty),
766 &tty->process_char_map);
767 set_bit(EOL2_CHAR(tty),
768 &tty->process_char_map);
769 if (L_ECHO(tty))
770 set_bit(REPRINT_CHAR(tty),
771 &tty->process_char_map);
774 if (I_IXON(tty)) {
775 set_bit(START_CHAR(tty), &tty->process_char_map);
776 set_bit(STOP_CHAR(tty), &tty->process_char_map);
778 if (L_ISIG(tty)) {
779 set_bit(INTR_CHAR(tty), &tty->process_char_map);
780 set_bit(QUIT_CHAR(tty), &tty->process_char_map);
781 set_bit(SUSP_CHAR(tty), &tty->process_char_map);
783 clear_bit(__DISABLED_CHAR, &tty->process_char_map);
784 sti();
785 tty->raw = 0;
786 tty->real_raw = 0;
787 } else {
788 tty->raw = 1;
789 if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
790 (I_IGNPAR(tty) || !I_INPCK(tty)) &&
791 (tty->driver.flags & TTY_DRIVER_REAL_RAW))
792 tty->real_raw = 1;
793 else
794 tty->real_raw = 0;
798 static void n_tty_close(struct tty_struct *tty)
800 n_tty_flush_buffer(tty);
801 if (tty->read_buf) {
802 free_page((unsigned long) tty->read_buf);
803 tty->read_buf = 0;
807 static int n_tty_open(struct tty_struct *tty)
809 if (!tty)
810 return -EINVAL;
812 if (!tty->read_buf) {
813 tty->read_buf = (unsigned char *)
814 get_free_page(in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
815 if (!tty->read_buf)
816 return -ENOMEM;
818 memset(tty->read_buf, 0, N_TTY_BUF_SIZE);
819 reset_buffer_flags(tty);
820 tty->column = 0;
821 n_tty_set_termios(tty, 0);
822 tty->minimum_to_wake = 1;
823 tty->closing = 0;
824 return 0;
827 static inline int input_available_p(struct tty_struct *tty, int amt)
829 if (tty->icanon) {
830 if (tty->canon_data)
831 return 1;
832 } else if (tty->read_cnt >= (amt ? amt : 1))
833 return 1;
835 return 0;
839 * Helper function to speed up read_chan. It is only called when
840 * ICANON is off; it copies characters straight from the tty queue to
841 * user space directly. It can be profitably called twice; once to
842 * drain the space from the tail pointer to the (physical) end of the
843 * buffer, and once to drain the space from the (physical) beginning of
844 * the buffer to head pointer.
846 static inline int copy_from_read_buf(struct tty_struct *tty,
847 unsigned char **b,
848 size_t *nr)
851 int retval;
852 ssize_t n;
854 retval = 0;
855 n = MIN(*nr, MIN(tty->read_cnt, N_TTY_BUF_SIZE - tty->read_tail));
856 if (n) {
857 retval = copy_to_user(*b, &tty->read_buf[tty->read_tail], n);
858 n -= retval;
859 tty->read_tail = (tty->read_tail + n) & (N_TTY_BUF_SIZE-1);
860 tty->read_cnt -= n;
861 *b += n;
862 *nr -= n;
864 return retval;
867 static ssize_t read_chan(struct tty_struct *tty, struct file *file,
868 unsigned char *buf, size_t nr)
870 unsigned char *b = buf;
871 DECLARE_WAITQUEUE(wait, current);
872 int c;
873 int minimum, time;
874 ssize_t retval = 0;
875 ssize_t size;
876 long timeout;
878 do_it_again:
880 if (!tty->read_buf) {
881 printk("n_tty_read_chan: called with read_buf == NULL?!?\n");
882 return -EIO;
885 /* Job control check -- must be done at start and after
886 every sleep (POSIX.1 7.1.1.4). */
887 /* NOTE: not yet done after every sleep pending a thorough
888 check of the logic of this change. -- jlc */
889 /* don't stop on /dev/console */
890 if (file->f_dentry->d_inode->i_rdev != CONSOLE_DEV &&
891 file->f_dentry->d_inode->i_rdev != SYSCONS_DEV &&
892 current->tty == tty) {
893 if (tty->pgrp <= 0)
894 printk("read_chan: tty->pgrp <= 0!\n");
895 else if (current->pgrp != tty->pgrp) {
896 if (is_ignored(SIGTTIN) ||
897 is_orphaned_pgrp(current->pgrp))
898 return -EIO;
899 kill_pg(current->pgrp, SIGTTIN, 1);
900 return -ERESTARTSYS;
904 minimum = time = 0;
905 timeout = MAX_SCHEDULE_TIMEOUT;
906 if (!tty->icanon) {
907 time = (HZ / 10) * TIME_CHAR(tty);
908 minimum = MIN_CHAR(tty);
909 if (minimum) {
910 if (time)
911 tty->minimum_to_wake = 1;
912 else if (!waitqueue_active(&tty->read_wait) ||
913 (tty->minimum_to_wake > minimum))
914 tty->minimum_to_wake = minimum;
915 } else {
916 timeout = 0;
917 if (time) {
918 timeout = time;
919 time = 0;
921 tty->minimum_to_wake = minimum = 1;
925 if (file->f_flags & O_NONBLOCK) {
926 if (down_trylock(&tty->atomic_read))
927 return -EAGAIN;
929 else {
930 if (down_interruptible(&tty->atomic_read))
931 return -ERESTARTSYS;
934 add_wait_queue(&tty->read_wait, &wait);
935 set_bit(TTY_DONT_FLIP, &tty->flags);
936 while (nr) {
937 /* First test for status change. */
938 if (tty->packet && tty->link->ctrl_status) {
939 unsigned char cs;
940 if (b != buf)
941 break;
942 cs = tty->link->ctrl_status;
943 tty->link->ctrl_status = 0;
944 put_user(cs, b++);
945 nr--;
946 break;
948 /* This statement must be first before checking for input
949 so that any interrupt will set the state back to
950 TASK_RUNNING. */
951 current->state = TASK_INTERRUPTIBLE;
953 if (((minimum - (b - buf)) < tty->minimum_to_wake) &&
954 ((minimum - (b - buf)) >= 1))
955 tty->minimum_to_wake = (minimum - (b - buf));
957 if (!input_available_p(tty, 0)) {
958 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
959 retval = -EIO;
960 break;
962 if (tty_hung_up_p(file))
963 break;
964 if (!timeout)
965 break;
966 if (file->f_flags & O_NONBLOCK) {
967 retval = -EAGAIN;
968 break;
970 if (signal_pending(current)) {
971 retval = -ERESTARTSYS;
972 break;
974 clear_bit(TTY_DONT_FLIP, &tty->flags);
975 timeout = schedule_timeout(timeout);
976 set_bit(TTY_DONT_FLIP, &tty->flags);
977 continue;
979 current->state = TASK_RUNNING;
981 /* Deal with packet mode. */
982 if (tty->packet && b == buf) {
983 put_user(TIOCPKT_DATA, b++);
984 nr--;
987 if (tty->icanon) {
988 /* N.B. avoid overrun if nr == 0 */
989 while (nr && tty->read_cnt) {
990 int eol;
992 eol = test_and_clear_bit(tty->read_tail,
993 &tty->read_flags);
994 c = tty->read_buf[tty->read_tail];
995 tty->read_tail = ((tty->read_tail+1) &
996 (N_TTY_BUF_SIZE-1));
997 tty->read_cnt--;
999 if (!eol || (c != __DISABLED_CHAR)) {
1000 put_user(c, b++);
1001 nr--;
1003 if (eol) {
1004 /* this test should be redundant:
1005 * we shouldn't be reading data if
1006 * canon_data is 0
1008 if (--tty->canon_data < 0)
1009 tty->canon_data = 0;
1010 break;
1013 } else {
1014 int uncopied;
1015 uncopied = copy_from_read_buf(tty, &b, &nr);
1016 uncopied += copy_from_read_buf(tty, &b, &nr);
1017 if (uncopied) {
1018 retval = -EFAULT;
1019 break;
1023 /* If there is enough space in the read buffer now, let the
1024 * low-level driver know. We use n_tty_chars_in_buffer() to
1025 * check the buffer, as it now knows about canonical mode.
1026 * Otherwise, if the driver is throttled and the line is
1027 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
1028 * we won't get any more characters.
1030 if (n_tty_chars_in_buffer(tty) <= TTY_THRESHOLD_UNTHROTTLE)
1031 check_unthrottle(tty);
1033 if (b - buf >= minimum)
1034 break;
1035 if (time)
1036 timeout = time;
1038 clear_bit(TTY_DONT_FLIP, &tty->flags);
1039 up(&tty->atomic_read);
1040 remove_wait_queue(&tty->read_wait, &wait);
1042 if (!waitqueue_active(&tty->read_wait))
1043 tty->minimum_to_wake = minimum;
1045 current->state = TASK_RUNNING;
1046 size = b - buf;
1047 if (size) {
1048 retval = size;
1049 if (nr)
1050 clear_bit(TTY_PUSH, &tty->flags);
1051 } else if (test_and_clear_bit(TTY_PUSH, &tty->flags))
1052 goto do_it_again;
1054 return retval;
1057 static ssize_t write_chan(struct tty_struct * tty, struct file * file,
1058 const unsigned char * buf, size_t nr)
1060 const unsigned char *b = buf;
1061 DECLARE_WAITQUEUE(wait, current);
1062 int c;
1063 ssize_t retval = 0;
1065 /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
1066 if (L_TOSTOP(tty) &&
1067 file->f_dentry->d_inode->i_rdev != CONSOLE_DEV &&
1068 file->f_dentry->d_inode->i_rdev != SYSCONS_DEV) {
1069 retval = tty_check_change(tty);
1070 if (retval)
1071 return retval;
1074 add_wait_queue(&tty->write_wait, &wait);
1075 while (1) {
1076 current->state = TASK_INTERRUPTIBLE;
1077 if (signal_pending(current)) {
1078 retval = -ERESTARTSYS;
1079 break;
1081 if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
1082 retval = -EIO;
1083 break;
1085 if (O_OPOST(tty) && !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
1086 while (nr > 0) {
1087 ssize_t num = opost_block(tty, b, nr);
1088 if (num < 0) {
1089 retval = num;
1090 goto break_out;
1092 b += num;
1093 nr -= num;
1094 if (nr == 0)
1095 break;
1096 get_user(c, b);
1097 if (opost(c, tty) < 0)
1098 break;
1099 b++; nr--;
1101 if (tty->driver.flush_chars)
1102 tty->driver.flush_chars(tty);
1103 } else {
1104 c = tty->driver.write(tty, 1, b, nr);
1105 if (c < 0) {
1106 retval = c;
1107 goto break_out;
1109 b += c;
1110 nr -= c;
1112 if (!nr)
1113 break;
1114 if (file->f_flags & O_NONBLOCK) {
1115 retval = -EAGAIN;
1116 break;
1118 schedule();
1120 break_out:
1121 current->state = TASK_RUNNING;
1122 remove_wait_queue(&tty->write_wait, &wait);
1123 return (b - buf) ? b - buf : retval;
1126 static unsigned int normal_poll(struct tty_struct * tty, struct file * file, poll_table *wait)
1128 unsigned int mask = 0;
1130 poll_wait(file, &tty->read_wait, wait);
1131 poll_wait(file, &tty->write_wait, wait);
1132 if (input_available_p(tty, TIME_CHAR(tty) ? 0 : MIN_CHAR(tty)))
1133 mask |= POLLIN | POLLRDNORM;
1134 if (tty->packet && tty->link->ctrl_status)
1135 mask |= POLLPRI | POLLIN | POLLRDNORM;
1136 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
1137 mask |= POLLHUP;
1138 if (tty_hung_up_p(file))
1139 mask |= POLLHUP;
1140 if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) {
1141 if (MIN_CHAR(tty) && !TIME_CHAR(tty))
1142 tty->minimum_to_wake = MIN_CHAR(tty);
1143 else
1144 tty->minimum_to_wake = 1;
1146 if (tty->driver.chars_in_buffer(tty) < WAKEUP_CHARS)
1147 mask |= POLLOUT | POLLWRNORM;
1148 return mask;
1151 struct tty_ldisc tty_ldisc_N_TTY = {
1152 TTY_LDISC_MAGIC, /* magic */
1153 "n_tty", /* name */
1154 0, /* num */
1155 0, /* flags */
1156 n_tty_open, /* open */
1157 n_tty_close, /* close */
1158 n_tty_flush_buffer, /* flush_buffer */
1159 n_tty_chars_in_buffer, /* chars_in_buffer */
1160 read_chan, /* read */
1161 write_chan, /* write */
1162 n_tty_ioctl, /* ioctl */
1163 n_tty_set_termios, /* set_termios */
1164 normal_poll, /* poll */
1165 n_tty_receive_buf, /* receive_buf */
1166 n_tty_receive_room, /* receive_room */
1167 0 /* write_wakeup */