Fix compile error. DEC guys, nobody of you compiling this stuff or
[linux-2.6/linux-mips.git] / drivers / char / n_tty.c
blobf488d9124528ae32f708d596f0404a8de333e6a6
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.
21 * 2000/01/20 Fixed SMP locking on put_tty_queue using bits of
22 * the patch by Andrew J. Kroll <ag784@freenet.buffalo.edu>
23 * who actually finally proved there really was a race.
26 #include <linux/types.h>
27 #include <linux/major.h>
28 #include <linux/errno.h>
29 #include <linux/signal.h>
30 #include <linux/fcntl.h>
31 #include <linux/sched.h>
32 #include <linux/interrupt.h>
33 #include <linux/tty.h>
34 #include <linux/timer.h>
35 #include <linux/ctype.h>
36 #include <linux/kd.h>
37 #include <linux/mm.h>
38 #include <linux/string.h>
39 #include <linux/malloc.h>
40 #include <linux/poll.h>
42 #include <asm/uaccess.h>
43 #include <asm/system.h>
44 #include <asm/bitops.h>
46 #define CONSOLE_DEV MKDEV(TTY_MAJOR,0)
47 #define SYSCONS_DEV MKDEV(TTYAUX_MAJOR,1)
49 #ifndef MIN
50 #define MIN(a,b) ((a) < (b) ? (a) : (b))
51 #endif
53 /* number of characters left in xmit buffer before select has we have room */
54 #define WAKEUP_CHARS 256
57 * This defines the low- and high-watermarks for throttling and
58 * unthrottling the TTY driver. These watermarks are used for
59 * controlling the space in the read buffer.
61 #define TTY_THRESHOLD_THROTTLE 128 /* now based on remaining room */
62 #define TTY_THRESHOLD_UNTHROTTLE 128
64 static inline void put_tty_queue(unsigned char c, struct tty_struct *tty)
66 unsigned long flags;
68 * The problem of stomping on the buffers ends here.
69 * Why didn't anyone see this one comming? --AJK
71 spin_lock_irqsave(&tty->read_lock, flags);
72 if (tty->read_cnt < N_TTY_BUF_SIZE) {
73 tty->read_buf[tty->read_head] = c;
74 tty->read_head = (tty->read_head + 1) & (N_TTY_BUF_SIZE-1);
75 tty->read_cnt++;
77 spin_unlock_irqrestore(&tty->read_lock, flags);
80 /*
81 * Check whether to call the driver.unthrottle function.
82 * We test the TTY_THROTTLED bit first so that it always
83 * indicates the current state.
85 static void check_unthrottle(struct tty_struct * tty)
87 if (tty->count &&
88 test_and_clear_bit(TTY_THROTTLED, &tty->flags) &&
89 tty->driver.unthrottle)
90 tty->driver.unthrottle(tty);
94 * Reset the read buffer counters, clear the flags,
95 * and make sure the driver is unthrottled. Called
96 * from n_tty_open() and n_tty_flush_buffer().
98 static void reset_buffer_flags(struct tty_struct *tty)
100 unsigned long flags;
102 spin_lock_irqsave(&tty->read_lock, flags);
103 tty->read_head = tty->read_tail = tty->read_cnt = 0;
104 spin_unlock_irqrestore(&tty->read_lock, flags);
105 tty->canon_head = tty->canon_data = tty->erasing = 0;
106 memset(&tty->read_flags, 0, sizeof tty->read_flags);
107 check_unthrottle(tty);
111 * Flush the input buffer
113 void n_tty_flush_buffer(struct tty_struct * tty)
115 /* clear everything and unthrottle the driver */
116 reset_buffer_flags(tty);
118 if (!tty->link)
119 return;
121 if (tty->link->packet) {
122 tty->ctrl_status |= TIOCPKT_FLUSHREAD;
123 wake_up_interruptible(&tty->link->read_wait);
128 * Return number of characters buffered to be delivered to user
130 ssize_t n_tty_chars_in_buffer(struct tty_struct *tty)
132 unsigned long flags;
133 ssize_t n = 0;
135 spin_lock_irqsave(&tty->read_lock, flags);
136 if (!tty->icanon) {
137 n = tty->read_cnt;
138 } else if (tty->canon_data) {
139 n = (tty->canon_head > tty->read_tail) ?
140 tty->canon_head - tty->read_tail :
141 tty->canon_head + (N_TTY_BUF_SIZE - tty->read_tail);
143 spin_unlock_irqrestore(&tty->read_lock, flags);
144 return n;
148 * Perform OPOST processing. Returns -1 when the output device is
149 * full and the character must be retried.
151 static int opost(unsigned char c, struct tty_struct *tty)
153 int space, spaces;
155 space = tty->driver.write_room(tty);
156 if (!space)
157 return -1;
159 if (O_OPOST(tty)) {
160 switch (c) {
161 case '\n':
162 if (O_ONLRET(tty))
163 tty->column = 0;
164 if (O_ONLCR(tty)) {
165 if (space < 2)
166 return -1;
167 tty->driver.put_char(tty, '\r');
168 tty->column = 0;
170 tty->canon_column = tty->column;
171 break;
172 case '\r':
173 if (O_ONOCR(tty) && tty->column == 0)
174 return 0;
175 if (O_OCRNL(tty)) {
176 c = '\n';
177 if (O_ONLRET(tty))
178 tty->canon_column = tty->column = 0;
179 break;
181 tty->canon_column = tty->column = 0;
182 break;
183 case '\t':
184 spaces = 8 - (tty->column & 7);
185 if (O_TABDLY(tty) == XTABS) {
186 if (space < spaces)
187 return -1;
188 tty->column += spaces;
189 tty->driver.write(tty, 0, " ", spaces);
190 return 0;
192 tty->column += spaces;
193 break;
194 case '\b':
195 if (tty->column > 0)
196 tty->column--;
197 break;
198 default:
199 if (O_OLCUC(tty))
200 c = toupper(c);
201 if (!iscntrl(c))
202 tty->column++;
203 break;
206 tty->driver.put_char(tty, c);
207 return 0;
211 * opost_block --- to speed up block console writes, among other
212 * things.
214 static ssize_t opost_block(struct tty_struct * tty,
215 const unsigned char * inbuf, unsigned int nr)
217 char buf[80];
218 int space;
219 int i;
220 char *cp;
222 space = tty->driver.write_room(tty);
223 if (!space)
224 return 0;
225 if (nr > space)
226 nr = space;
227 if (nr > sizeof(buf))
228 nr = sizeof(buf);
230 if (copy_from_user(buf, inbuf, nr))
231 return -EFAULT;
233 for (i = 0, cp = buf; i < nr; i++, cp++) {
234 switch (*cp) {
235 case '\n':
236 if (O_ONLRET(tty))
237 tty->column = 0;
238 if (O_ONLCR(tty))
239 goto break_out;
240 tty->canon_column = tty->column;
241 break;
242 case '\r':
243 if (O_ONOCR(tty) && tty->column == 0)
244 goto break_out;
245 if (O_OCRNL(tty)) {
246 *cp = '\n';
247 if (O_ONLRET(tty))
248 tty->canon_column = tty->column = 0;
249 break;
251 tty->canon_column = tty->column = 0;
252 break;
253 case '\t':
254 goto break_out;
255 case '\b':
256 if (tty->column > 0)
257 tty->column--;
258 break;
259 default:
260 if (O_OLCUC(tty))
261 *cp = toupper(*cp);
262 if (!iscntrl(*cp))
263 tty->column++;
264 break;
267 break_out:
268 if (tty->driver.flush_chars)
269 tty->driver.flush_chars(tty);
270 i = tty->driver.write(tty, 0, buf, i);
271 return i;
276 static inline void put_char(unsigned char c, struct tty_struct *tty)
278 tty->driver.put_char(tty, c);
281 /* Must be called only when L_ECHO(tty) is true. */
283 static void echo_char(unsigned char c, struct tty_struct *tty)
285 if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t') {
286 put_char('^', tty);
287 put_char(c ^ 0100, tty);
288 tty->column += 2;
289 } else
290 opost(c, tty);
293 static inline void finish_erasing(struct tty_struct *tty)
295 if (tty->erasing) {
296 put_char('/', tty);
297 tty->column += 2;
298 tty->erasing = 0;
302 static void eraser(unsigned char c, struct tty_struct *tty)
304 enum { ERASE, WERASE, KILL } kill_type;
305 int head, seen_alnums;
306 unsigned long flags;
308 if (tty->read_head == tty->canon_head) {
309 /* opost('\a', tty); */ /* what do you think? */
310 return;
312 if (c == ERASE_CHAR(tty))
313 kill_type = ERASE;
314 else if (c == WERASE_CHAR(tty))
315 kill_type = WERASE;
316 else {
317 if (!L_ECHO(tty)) {
318 spin_lock_irqsave(&tty->read_lock, flags);
319 tty->read_cnt -= ((tty->read_head - tty->canon_head) &
320 (N_TTY_BUF_SIZE - 1));
321 tty->read_head = tty->canon_head;
322 spin_unlock_irqrestore(&tty->read_lock, flags);
323 return;
325 if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
326 spin_lock_irqsave(&tty->read_lock, flags);
327 tty->read_cnt -= ((tty->read_head - tty->canon_head) &
328 (N_TTY_BUF_SIZE - 1));
329 tty->read_head = tty->canon_head;
330 spin_unlock_irqrestore(&tty->read_lock, flags);
331 finish_erasing(tty);
332 echo_char(KILL_CHAR(tty), tty);
333 /* Add a newline if ECHOK is on and ECHOKE is off. */
334 if (L_ECHOK(tty))
335 opost('\n', tty);
336 return;
338 kill_type = KILL;
341 seen_alnums = 0;
342 while (tty->read_head != tty->canon_head) {
343 head = (tty->read_head - 1) & (N_TTY_BUF_SIZE-1);
344 c = tty->read_buf[head];
345 if (kill_type == WERASE) {
346 /* Equivalent to BSD's ALTWERASE. */
347 if (isalnum(c) || c == '_')
348 seen_alnums++;
349 else if (seen_alnums)
350 break;
352 spin_lock_irqsave(&tty->read_lock, flags);
353 tty->read_head = head;
354 tty->read_cnt--;
355 spin_unlock_irqrestore(&tty->read_lock, flags);
356 if (L_ECHO(tty)) {
357 if (L_ECHOPRT(tty)) {
358 if (!tty->erasing) {
359 put_char('\\', tty);
360 tty->column++;
361 tty->erasing = 1;
363 echo_char(c, tty);
364 } else if (kill_type == ERASE && !L_ECHOE(tty)) {
365 echo_char(ERASE_CHAR(tty), tty);
366 } else if (c == '\t') {
367 unsigned int col = tty->canon_column;
368 unsigned long tail = tty->canon_head;
370 /* Find the column of the last char. */
371 while (tail != tty->read_head) {
372 c = tty->read_buf[tail];
373 if (c == '\t')
374 col = (col | 7) + 1;
375 else if (iscntrl(c)) {
376 if (L_ECHOCTL(tty))
377 col += 2;
378 } else
379 col++;
380 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
383 /* should never happen */
384 if (tty->column > 0x80000000)
385 tty->column = 0;
387 /* Now backup to that column. */
388 while (tty->column > col) {
389 /* Can't use opost here. */
390 put_char('\b', tty);
391 if (tty->column > 0)
392 tty->column--;
394 } else {
395 if (iscntrl(c) && L_ECHOCTL(tty)) {
396 put_char('\b', tty);
397 put_char(' ', tty);
398 put_char('\b', tty);
399 if (tty->column > 0)
400 tty->column--;
402 if (!iscntrl(c) || L_ECHOCTL(tty)) {
403 put_char('\b', tty);
404 put_char(' ', tty);
405 put_char('\b', tty);
406 if (tty->column > 0)
407 tty->column--;
411 if (kill_type == ERASE)
412 break;
414 if (tty->read_head == tty->canon_head)
415 finish_erasing(tty);
418 static inline void isig(int sig, struct tty_struct *tty, int flush)
420 if (tty->pgrp > 0)
421 kill_pg(tty->pgrp, sig, 1);
422 if (flush || !L_NOFLSH(tty)) {
423 n_tty_flush_buffer(tty);
424 if (tty->driver.flush_buffer)
425 tty->driver.flush_buffer(tty);
429 static inline void n_tty_receive_break(struct tty_struct *tty)
431 if (I_IGNBRK(tty))
432 return;
433 if (I_BRKINT(tty)) {
434 isig(SIGINT, tty, 1);
435 return;
437 if (I_PARMRK(tty)) {
438 put_tty_queue('\377', tty);
439 put_tty_queue('\0', tty);
441 put_tty_queue('\0', tty);
442 wake_up_interruptible(&tty->read_wait);
445 static inline void n_tty_receive_overrun(struct tty_struct *tty)
447 char buf[64];
449 tty->num_overrun++;
450 if (time_before(tty->overrun_time, jiffies - HZ)) {
451 printk("%s: %d input overrun(s)\n", tty_name(tty, buf),
452 tty->num_overrun);
453 tty->overrun_time = jiffies;
454 tty->num_overrun = 0;
458 static inline void n_tty_receive_parity_error(struct tty_struct *tty,
459 unsigned char c)
461 if (I_IGNPAR(tty)) {
462 return;
464 if (I_PARMRK(tty)) {
465 put_tty_queue('\377', tty);
466 put_tty_queue('\0', tty);
467 put_tty_queue(c, tty);
468 } else if (I_INPCK(tty))
469 put_tty_queue('\0', tty);
470 else
471 put_tty_queue(c, tty);
472 wake_up_interruptible(&tty->read_wait);
475 static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
477 if (tty->raw) {
478 put_tty_queue(c, tty);
479 return;
482 if (tty->stopped && !tty->flow_stopped &&
483 I_IXON(tty) && I_IXANY(tty)) {
484 start_tty(tty);
485 return;
488 if (I_ISTRIP(tty))
489 c &= 0x7f;
490 if (I_IUCLC(tty) && L_IEXTEN(tty))
491 c=tolower(c);
493 if (tty->closing) {
494 if (I_IXON(tty)) {
495 if (c == START_CHAR(tty))
496 start_tty(tty);
497 else if (c == STOP_CHAR(tty))
498 stop_tty(tty);
500 return;
504 * If the previous character was LNEXT, or we know that this
505 * character is not one of the characters that we'll have to
506 * handle specially, do shortcut processing to speed things
507 * up.
509 if (!test_bit(c, &tty->process_char_map) || tty->lnext) {
510 finish_erasing(tty);
511 tty->lnext = 0;
512 if (L_ECHO(tty)) {
513 if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
514 put_char('\a', tty); /* beep if no space */
515 return;
517 /* Record the column of first canon char. */
518 if (tty->canon_head == tty->read_head)
519 tty->canon_column = tty->column;
520 echo_char(c, tty);
522 if (I_PARMRK(tty) && c == (unsigned char) '\377')
523 put_tty_queue(c, tty);
524 put_tty_queue(c, tty);
525 return;
528 if (c == '\r') {
529 if (I_IGNCR(tty))
530 return;
531 if (I_ICRNL(tty))
532 c = '\n';
533 } else if (c == '\n' && I_INLCR(tty))
534 c = '\r';
535 if (I_IXON(tty)) {
536 if (c == START_CHAR(tty)) {
537 start_tty(tty);
538 return;
540 if (c == STOP_CHAR(tty)) {
541 stop_tty(tty);
542 return;
545 if (L_ISIG(tty)) {
546 int signal;
547 signal = SIGINT;
548 if (c == INTR_CHAR(tty))
549 goto send_signal;
550 signal = SIGQUIT;
551 if (c == QUIT_CHAR(tty))
552 goto send_signal;
553 signal = SIGTSTP;
554 if (c == SUSP_CHAR(tty)) {
555 send_signal:
556 isig(signal, tty, 0);
557 return;
560 if (tty->icanon) {
561 if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
562 (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
563 eraser(c, tty);
564 return;
566 if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
567 tty->lnext = 1;
568 if (L_ECHO(tty)) {
569 finish_erasing(tty);
570 if (L_ECHOCTL(tty)) {
571 put_char('^', tty);
572 put_char('\b', tty);
575 return;
577 if (c == REPRINT_CHAR(tty) && L_ECHO(tty) &&
578 L_IEXTEN(tty)) {
579 unsigned long tail = tty->canon_head;
581 finish_erasing(tty);
582 echo_char(c, tty);
583 opost('\n', tty);
584 while (tail != tty->read_head) {
585 echo_char(tty->read_buf[tail], tty);
586 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
588 return;
590 if (c == '\n') {
591 if (L_ECHO(tty) || L_ECHONL(tty)) {
592 if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
593 put_char('\a', tty);
594 return;
596 opost('\n', tty);
598 goto handle_newline;
600 if (c == EOF_CHAR(tty)) {
601 if (tty->canon_head != tty->read_head)
602 set_bit(TTY_PUSH, &tty->flags);
603 c = __DISABLED_CHAR;
604 goto handle_newline;
606 if ((c == EOL_CHAR(tty)) ||
607 (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
609 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
611 if (L_ECHO(tty)) {
612 if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
613 put_char('\a', tty);
614 return;
616 /* Record the column of first canon char. */
617 if (tty->canon_head == tty->read_head)
618 tty->canon_column = tty->column;
619 echo_char(c, tty);
622 * XXX does PARMRK doubling happen for
623 * EOL_CHAR and EOL2_CHAR?
625 if (I_PARMRK(tty) && c == (unsigned char) '\377')
626 put_tty_queue(c, tty);
628 handle_newline:
629 set_bit(tty->read_head, &tty->read_flags);
630 put_tty_queue(c, tty);
631 tty->canon_head = tty->read_head;
632 tty->canon_data++;
633 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
634 if (waitqueue_active(&tty->read_wait))
635 wake_up_interruptible(&tty->read_wait);
636 return;
640 finish_erasing(tty);
641 if (L_ECHO(tty)) {
642 if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
643 put_char('\a', tty); /* beep if no space */
644 return;
646 if (c == '\n')
647 opost('\n', tty);
648 else {
649 /* Record the column of first canon char. */
650 if (tty->canon_head == tty->read_head)
651 tty->canon_column = tty->column;
652 echo_char(c, tty);
656 if (I_PARMRK(tty) && c == (unsigned char) '\377')
657 put_tty_queue(c, tty);
659 put_tty_queue(c, tty);
662 static int n_tty_receive_room(struct tty_struct *tty)
664 int left = N_TTY_BUF_SIZE - tty->read_cnt - 1;
667 * If we are doing input canonicalization, and there are no
668 * pending newlines, let characters through without limit, so
669 * that erase characters will be handled. Other excess
670 * characters will be beeped.
672 if (tty->icanon && !tty->canon_data)
673 return N_TTY_BUF_SIZE;
675 if (left > 0)
676 return left;
677 return 0;
680 static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
681 char *fp, int count)
683 const unsigned char *p;
684 char *f, flags = TTY_NORMAL;
685 int i;
686 char buf[64];
687 unsigned long cpuflags;
689 if (!tty->read_buf)
690 return;
692 if (tty->real_raw) {
693 spin_lock_irqsave(&tty->read_lock, cpuflags);
694 i = MIN(count, MIN(N_TTY_BUF_SIZE - tty->read_cnt,
695 N_TTY_BUF_SIZE - tty->read_head));
696 memcpy(tty->read_buf + tty->read_head, cp, i);
697 tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
698 tty->read_cnt += i;
699 cp += i;
700 count -= i;
702 i = MIN(count, MIN(N_TTY_BUF_SIZE - tty->read_cnt,
703 N_TTY_BUF_SIZE - tty->read_head));
704 memcpy(tty->read_buf + tty->read_head, cp, i);
705 tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
706 tty->read_cnt += i;
707 spin_unlock_irqrestore(&tty->read_lock, cpuflags);
708 } else {
709 for (i=count, p = cp, f = fp; i; i--, p++) {
710 if (f)
711 flags = *f++;
712 switch (flags) {
713 case TTY_NORMAL:
714 n_tty_receive_char(tty, *p);
715 break;
716 case TTY_BREAK:
717 n_tty_receive_break(tty);
718 break;
719 case TTY_PARITY:
720 case TTY_FRAME:
721 n_tty_receive_parity_error(tty, *p);
722 break;
723 case TTY_OVERRUN:
724 n_tty_receive_overrun(tty);
725 break;
726 default:
727 printk("%s: unknown flag %d\n",
728 tty_name(tty, buf), flags);
729 break;
732 if (tty->driver.flush_chars)
733 tty->driver.flush_chars(tty);
736 if (!tty->icanon && (tty->read_cnt >= tty->minimum_to_wake)) {
737 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
738 if (waitqueue_active(&tty->read_wait))
739 wake_up_interruptible(&tty->read_wait);
743 * Check the remaining room for the input canonicalization
744 * mode. We don't want to throttle the driver if we're in
745 * canonical mode and don't have a newline yet!
747 if (n_tty_receive_room(tty) < TTY_THRESHOLD_THROTTLE) {
748 /* check TTY_THROTTLED first so it indicates our state */
749 if (!test_and_set_bit(TTY_THROTTLED, &tty->flags) &&
750 tty->driver.throttle)
751 tty->driver.throttle(tty);
755 int is_ignored(int sig)
757 return (sigismember(&current->blocked, sig) ||
758 current->sig->action[sig-1].sa.sa_handler == SIG_IGN);
761 static void n_tty_set_termios(struct tty_struct *tty, struct termios * old)
763 if (!tty)
764 return;
766 tty->icanon = (L_ICANON(tty) != 0);
767 if (test_bit(TTY_HW_COOK_IN, &tty->flags)) {
768 tty->raw = 1;
769 tty->real_raw = 1;
770 return;
772 if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
773 I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
774 I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
775 I_PARMRK(tty)) {
776 cli();
777 memset(tty->process_char_map, 0, 256/8);
779 if (I_IGNCR(tty) || I_ICRNL(tty))
780 set_bit('\r', &tty->process_char_map);
781 if (I_INLCR(tty))
782 set_bit('\n', &tty->process_char_map);
784 if (L_ICANON(tty)) {
785 set_bit(ERASE_CHAR(tty), &tty->process_char_map);
786 set_bit(KILL_CHAR(tty), &tty->process_char_map);
787 set_bit(EOF_CHAR(tty), &tty->process_char_map);
788 set_bit('\n', &tty->process_char_map);
789 set_bit(EOL_CHAR(tty), &tty->process_char_map);
790 if (L_IEXTEN(tty)) {
791 set_bit(WERASE_CHAR(tty),
792 &tty->process_char_map);
793 set_bit(LNEXT_CHAR(tty),
794 &tty->process_char_map);
795 set_bit(EOL2_CHAR(tty),
796 &tty->process_char_map);
797 if (L_ECHO(tty))
798 set_bit(REPRINT_CHAR(tty),
799 &tty->process_char_map);
802 if (I_IXON(tty)) {
803 set_bit(START_CHAR(tty), &tty->process_char_map);
804 set_bit(STOP_CHAR(tty), &tty->process_char_map);
806 if (L_ISIG(tty)) {
807 set_bit(INTR_CHAR(tty), &tty->process_char_map);
808 set_bit(QUIT_CHAR(tty), &tty->process_char_map);
809 set_bit(SUSP_CHAR(tty), &tty->process_char_map);
811 clear_bit(__DISABLED_CHAR, &tty->process_char_map);
812 sti();
813 tty->raw = 0;
814 tty->real_raw = 0;
815 } else {
816 tty->raw = 1;
817 if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
818 (I_IGNPAR(tty) || !I_INPCK(tty)) &&
819 (tty->driver.flags & TTY_DRIVER_REAL_RAW))
820 tty->real_raw = 1;
821 else
822 tty->real_raw = 0;
826 static void n_tty_close(struct tty_struct *tty)
828 n_tty_flush_buffer(tty);
829 if (tty->read_buf) {
830 free_page((unsigned long) tty->read_buf);
831 tty->read_buf = 0;
835 static int n_tty_open(struct tty_struct *tty)
837 if (!tty)
838 return -EINVAL;
840 if (!tty->read_buf) {
841 tty->read_buf = (unsigned char *)
842 get_zeroed_page(in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
843 if (!tty->read_buf)
844 return -ENOMEM;
846 memset(tty->read_buf, 0, N_TTY_BUF_SIZE);
847 reset_buffer_flags(tty);
848 tty->column = 0;
849 n_tty_set_termios(tty, 0);
850 tty->minimum_to_wake = 1;
851 tty->closing = 0;
852 return 0;
855 static inline int input_available_p(struct tty_struct *tty, int amt)
857 if (tty->icanon) {
858 if (tty->canon_data)
859 return 1;
860 } else if (tty->read_cnt >= (amt ? amt : 1))
861 return 1;
863 return 0;
867 * Helper function to speed up read_chan. It is only called when
868 * ICANON is off; it copies characters straight from the tty queue to
869 * user space directly. It can be profitably called twice; once to
870 * drain the space from the tail pointer to the (physical) end of the
871 * buffer, and once to drain the space from the (physical) beginning of
872 * the buffer to head pointer.
874 static inline int copy_from_read_buf(struct tty_struct *tty,
875 unsigned char **b,
876 size_t *nr)
879 int retval;
880 ssize_t n;
881 unsigned long flags;
883 retval = 0;
884 spin_lock_irqsave(&tty->read_lock, flags);
885 n = MIN(*nr, MIN(tty->read_cnt, N_TTY_BUF_SIZE - tty->read_tail));
886 spin_unlock_irqrestore(&tty->read_lock, flags);
887 if (n) {
888 mb();
889 retval = copy_to_user(*b, &tty->read_buf[tty->read_tail], n);
890 n -= retval;
891 spin_lock_irqsave(&tty->read_lock, flags);
892 tty->read_tail = (tty->read_tail + n) & (N_TTY_BUF_SIZE-1);
893 tty->read_cnt -= n;
894 spin_unlock_irqrestore(&tty->read_lock, flags);
895 *b += n;
896 *nr -= n;
898 return retval;
901 static ssize_t read_chan(struct tty_struct *tty, struct file *file,
902 unsigned char *buf, size_t nr)
904 unsigned char *b = buf;
905 DECLARE_WAITQUEUE(wait, current);
906 int c;
907 int minimum, time;
908 ssize_t retval = 0;
909 ssize_t size;
910 long timeout;
911 unsigned long flags;
913 do_it_again:
915 if (!tty->read_buf) {
916 printk("n_tty_read_chan: called with read_buf == NULL?!?\n");
917 return -EIO;
920 /* Job control check -- must be done at start and after
921 every sleep (POSIX.1 7.1.1.4). */
922 /* NOTE: not yet done after every sleep pending a thorough
923 check of the logic of this change. -- jlc */
924 /* don't stop on /dev/console */
925 if (file->f_dentry->d_inode->i_rdev != CONSOLE_DEV &&
926 file->f_dentry->d_inode->i_rdev != SYSCONS_DEV &&
927 current->tty == tty) {
928 if (tty->pgrp <= 0)
929 printk("read_chan: tty->pgrp <= 0!\n");
930 else if (current->pgrp != tty->pgrp) {
931 if (is_ignored(SIGTTIN) ||
932 is_orphaned_pgrp(current->pgrp))
933 return -EIO;
934 kill_pg(current->pgrp, SIGTTIN, 1);
935 return -ERESTARTSYS;
939 minimum = time = 0;
940 timeout = MAX_SCHEDULE_TIMEOUT;
941 if (!tty->icanon) {
942 time = (HZ / 10) * TIME_CHAR(tty);
943 minimum = MIN_CHAR(tty);
944 if (minimum) {
945 if (time)
946 tty->minimum_to_wake = 1;
947 else if (!waitqueue_active(&tty->read_wait) ||
948 (tty->minimum_to_wake > minimum))
949 tty->minimum_to_wake = minimum;
950 } else {
951 timeout = 0;
952 if (time) {
953 timeout = time;
954 time = 0;
956 tty->minimum_to_wake = minimum = 1;
960 if (file->f_flags & O_NONBLOCK) {
961 if (down_trylock(&tty->atomic_read))
962 return -EAGAIN;
964 else {
965 if (down_interruptible(&tty->atomic_read))
966 return -ERESTARTSYS;
969 add_wait_queue(&tty->read_wait, &wait);
970 set_bit(TTY_DONT_FLIP, &tty->flags);
971 while (nr) {
972 /* First test for status change. */
973 if (tty->packet && tty->link->ctrl_status) {
974 unsigned char cs;
975 if (b != buf)
976 break;
977 cs = tty->link->ctrl_status;
978 tty->link->ctrl_status = 0;
979 put_user(cs, b++);
980 nr--;
981 break;
983 /* This statement must be first before checking for input
984 so that any interrupt will set the state back to
985 TASK_RUNNING. */
986 set_current_state(TASK_INTERRUPTIBLE);
988 if (((minimum - (b - buf)) < tty->minimum_to_wake) &&
989 ((minimum - (b - buf)) >= 1))
990 tty->minimum_to_wake = (minimum - (b - buf));
992 if (!input_available_p(tty, 0)) {
993 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
994 retval = -EIO;
995 break;
997 if (tty_hung_up_p(file))
998 break;
999 if (!timeout)
1000 break;
1001 if (file->f_flags & O_NONBLOCK) {
1002 retval = -EAGAIN;
1003 break;
1005 if (signal_pending(current)) {
1006 retval = -ERESTARTSYS;
1007 break;
1009 clear_bit(TTY_DONT_FLIP, &tty->flags);
1010 timeout = schedule_timeout(timeout);
1011 set_bit(TTY_DONT_FLIP, &tty->flags);
1012 continue;
1014 current->state = TASK_RUNNING;
1016 /* Deal with packet mode. */
1017 if (tty->packet && b == buf) {
1018 put_user(TIOCPKT_DATA, b++);
1019 nr--;
1022 if (tty->icanon) {
1023 /* N.B. avoid overrun if nr == 0 */
1024 while (nr && tty->read_cnt) {
1025 int eol;
1027 eol = test_and_clear_bit(tty->read_tail,
1028 &tty->read_flags);
1029 c = tty->read_buf[tty->read_tail];
1030 spin_lock_irqsave(&tty->read_lock, flags);
1031 tty->read_tail = ((tty->read_tail+1) &
1032 (N_TTY_BUF_SIZE-1));
1033 tty->read_cnt--;
1034 spin_unlock_irqrestore(&tty->read_lock, flags);
1036 if (!eol || (c != __DISABLED_CHAR)) {
1037 put_user(c, b++);
1038 nr--;
1040 if (eol) {
1041 /* this test should be redundant:
1042 * we shouldn't be reading data if
1043 * canon_data is 0
1045 if (--tty->canon_data < 0)
1046 tty->canon_data = 0;
1047 break;
1050 } else {
1051 int uncopied;
1052 uncopied = copy_from_read_buf(tty, &b, &nr);
1053 uncopied += copy_from_read_buf(tty, &b, &nr);
1054 if (uncopied) {
1055 retval = -EFAULT;
1056 break;
1060 /* If there is enough space in the read buffer now, let the
1061 * low-level driver know. We use n_tty_chars_in_buffer() to
1062 * check the buffer, as it now knows about canonical mode.
1063 * Otherwise, if the driver is throttled and the line is
1064 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
1065 * we won't get any more characters.
1067 if (n_tty_chars_in_buffer(tty) <= TTY_THRESHOLD_UNTHROTTLE)
1068 check_unthrottle(tty);
1070 if (b - buf >= minimum)
1071 break;
1072 if (time)
1073 timeout = time;
1075 clear_bit(TTY_DONT_FLIP, &tty->flags);
1076 up(&tty->atomic_read);
1077 remove_wait_queue(&tty->read_wait, &wait);
1079 if (!waitqueue_active(&tty->read_wait))
1080 tty->minimum_to_wake = minimum;
1082 current->state = TASK_RUNNING;
1083 size = b - buf;
1084 if (size) {
1085 retval = size;
1086 if (nr)
1087 clear_bit(TTY_PUSH, &tty->flags);
1088 } else if (test_and_clear_bit(TTY_PUSH, &tty->flags))
1089 goto do_it_again;
1091 return retval;
1094 static ssize_t write_chan(struct tty_struct * tty, struct file * file,
1095 const unsigned char * buf, size_t nr)
1097 const unsigned char *b = buf;
1098 DECLARE_WAITQUEUE(wait, current);
1099 int c;
1100 ssize_t retval = 0;
1102 /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
1103 if (L_TOSTOP(tty) &&
1104 file->f_dentry->d_inode->i_rdev != CONSOLE_DEV &&
1105 file->f_dentry->d_inode->i_rdev != SYSCONS_DEV) {
1106 retval = tty_check_change(tty);
1107 if (retval)
1108 return retval;
1111 add_wait_queue(&tty->write_wait, &wait);
1112 while (1) {
1113 set_current_state(TASK_INTERRUPTIBLE);
1114 if (signal_pending(current)) {
1115 retval = -ERESTARTSYS;
1116 break;
1118 if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
1119 retval = -EIO;
1120 break;
1122 if (O_OPOST(tty) && !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
1123 while (nr > 0) {
1124 ssize_t num = opost_block(tty, b, nr);
1125 if (num < 0) {
1126 retval = num;
1127 goto break_out;
1129 b += num;
1130 nr -= num;
1131 if (nr == 0)
1132 break;
1133 current->state = TASK_RUNNING;
1134 get_user(c, b);
1135 current->state = TASK_INTERRUPTIBLE;
1136 if (opost(c, tty) < 0)
1137 break;
1138 b++; nr--;
1140 if (tty->driver.flush_chars)
1141 tty->driver.flush_chars(tty);
1142 } else {
1143 current->state = TASK_RUNNING;
1144 c = tty->driver.write(tty, 1, b, nr);
1145 current->state = TASK_INTERRUPTIBLE;
1146 if (c < 0) {
1147 retval = c;
1148 goto break_out;
1150 b += c;
1151 nr -= c;
1153 if (!nr)
1154 break;
1155 if (file->f_flags & O_NONBLOCK) {
1156 retval = -EAGAIN;
1157 break;
1159 schedule();
1161 break_out:
1162 current->state = TASK_RUNNING;
1163 remove_wait_queue(&tty->write_wait, &wait);
1164 return (b - buf) ? b - buf : retval;
1167 /* Called without the kernel lock held - fine */
1168 static unsigned int normal_poll(struct tty_struct * tty, struct file * file, poll_table *wait)
1170 unsigned int mask = 0;
1172 poll_wait(file, &tty->read_wait, wait);
1173 poll_wait(file, &tty->write_wait, wait);
1174 if (input_available_p(tty, TIME_CHAR(tty) ? 0 : MIN_CHAR(tty)))
1175 mask |= POLLIN | POLLRDNORM;
1176 if (tty->packet && tty->link->ctrl_status)
1177 mask |= POLLPRI | POLLIN | POLLRDNORM;
1178 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
1179 mask |= POLLHUP;
1180 if (tty_hung_up_p(file))
1181 mask |= POLLHUP;
1182 if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) {
1183 if (MIN_CHAR(tty) && !TIME_CHAR(tty))
1184 tty->minimum_to_wake = MIN_CHAR(tty);
1185 else
1186 tty->minimum_to_wake = 1;
1188 if (tty->driver.chars_in_buffer(tty) < WAKEUP_CHARS)
1189 mask |= POLLOUT | POLLWRNORM;
1190 return mask;
1193 struct tty_ldisc tty_ldisc_N_TTY = {
1194 TTY_LDISC_MAGIC, /* magic */
1195 "n_tty", /* name */
1196 0, /* num */
1197 0, /* flags */
1198 n_tty_open, /* open */
1199 n_tty_close, /* close */
1200 n_tty_flush_buffer, /* flush_buffer */
1201 n_tty_chars_in_buffer, /* chars_in_buffer */
1202 read_chan, /* read */
1203 write_chan, /* write */
1204 n_tty_ioctl, /* ioctl */
1205 n_tty_set_termios, /* set_termios */
1206 normal_poll, /* poll */
1207 n_tty_receive_buf, /* receive_buf */
1208 n_tty_receive_room, /* receive_room */
1209 0 /* write_wakeup */