[PATCH] Fix up 'linux-dvb' maintainers entry
[linux-2.6/history.git] / drivers / char / n_tty.c
blob44d551ba1091df66e7c4956a3fafcb9809942f2f
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 General Public
19 * License.
21 * Reduced memory usage for older ARM systems - Russell King.
23 * 2000/01/20 Fixed SMP locking on put_tty_queue using bits of
24 * the patch by Andrew J. Kroll <ag784@freenet.buffalo.edu>
25 * who actually finally proved there really was a race.
27 * 2002/03/18 Implemented n_tty_wakeup to send SIGIO POLL_OUTs to
28 * waiting writing processes-Sapan Bhatia <sapan@corewars.org>.
29 * Also fixed a bug in BLOCKING mode where write_chan returns
30 * EAGAIN
33 #include <linux/types.h>
34 #include <linux/major.h>
35 #include <linux/errno.h>
36 #include <linux/signal.h>
37 #include <linux/fcntl.h>
38 #include <linux/sched.h>
39 #include <linux/interrupt.h>
40 #include <linux/tty.h>
41 #include <linux/timer.h>
42 #include <linux/ctype.h>
43 #include <linux/kd.h>
44 #include <linux/mm.h>
45 #include <linux/string.h>
46 #include <linux/slab.h>
47 #include <linux/poll.h>
49 #include <asm/uaccess.h>
50 #include <asm/system.h>
51 #include <asm/bitops.h>
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 unsigned char *alloc_buf(void)
66 unsigned char *p;
67 int prio = in_interrupt() ? GFP_ATOMIC : GFP_KERNEL;
69 if (PAGE_SIZE != N_TTY_BUF_SIZE) {
70 p = kmalloc(N_TTY_BUF_SIZE, prio);
71 if (p)
72 memset(p, 0, N_TTY_BUF_SIZE);
73 } else
74 p = (unsigned char *)get_zeroed_page(prio);
76 return p;
79 static inline void free_buf(unsigned char *buf)
81 if (PAGE_SIZE != N_TTY_BUF_SIZE)
82 kfree(buf);
83 else
84 free_page((unsigned long) buf);
87 static inline void put_tty_queue_nolock(unsigned char c, struct tty_struct *tty)
89 if (tty->read_cnt < N_TTY_BUF_SIZE) {
90 tty->read_buf[tty->read_head] = c;
91 tty->read_head = (tty->read_head + 1) & (N_TTY_BUF_SIZE-1);
92 tty->read_cnt++;
96 static inline void put_tty_queue(unsigned char c, struct tty_struct *tty)
98 unsigned long flags;
100 * The problem of stomping on the buffers ends here.
101 * Why didn't anyone see this one coming? --AJK
103 spin_lock_irqsave(&tty->read_lock, flags);
104 put_tty_queue_nolock(c, tty);
105 spin_unlock_irqrestore(&tty->read_lock, flags);
109 * Check whether to call the driver.unthrottle function.
110 * We test the TTY_THROTTLED bit first so that it always
111 * indicates the current state.
113 static void check_unthrottle(struct tty_struct * tty)
115 if (tty->count &&
116 test_and_clear_bit(TTY_THROTTLED, &tty->flags) &&
117 tty->driver->unthrottle)
118 tty->driver->unthrottle(tty);
122 * Reset the read buffer counters, clear the flags,
123 * and make sure the driver is unthrottled. Called
124 * from n_tty_open() and n_tty_flush_buffer().
126 static void reset_buffer_flags(struct tty_struct *tty)
128 unsigned long flags;
130 spin_lock_irqsave(&tty->read_lock, flags);
131 tty->read_head = tty->read_tail = tty->read_cnt = 0;
132 spin_unlock_irqrestore(&tty->read_lock, flags);
133 tty->canon_head = tty->canon_data = tty->erasing = 0;
134 memset(&tty->read_flags, 0, sizeof tty->read_flags);
135 check_unthrottle(tty);
139 * Flush the input buffer
141 void n_tty_flush_buffer(struct tty_struct * tty)
143 /* clear everything and unthrottle the driver */
144 reset_buffer_flags(tty);
146 if (!tty->link)
147 return;
149 if (tty->link->packet) {
150 tty->ctrl_status |= TIOCPKT_FLUSHREAD;
151 wake_up_interruptible(&tty->link->read_wait);
156 * Return number of characters buffered to be delivered to user
158 ssize_t n_tty_chars_in_buffer(struct tty_struct *tty)
160 unsigned long flags;
161 ssize_t n = 0;
163 spin_lock_irqsave(&tty->read_lock, flags);
164 if (!tty->icanon) {
165 n = tty->read_cnt;
166 } else if (tty->canon_data) {
167 n = (tty->canon_head > tty->read_tail) ?
168 tty->canon_head - tty->read_tail :
169 tty->canon_head + (N_TTY_BUF_SIZE - tty->read_tail);
171 spin_unlock_irqrestore(&tty->read_lock, flags);
172 return n;
176 * Perform OPOST processing. Returns -1 when the output device is
177 * full and the character must be retried.
179 static int opost(unsigned char c, struct tty_struct *tty)
181 int space, spaces;
183 space = tty->driver->write_room(tty);
184 if (!space)
185 return -1;
187 if (O_OPOST(tty)) {
188 switch (c) {
189 case '\n':
190 if (O_ONLRET(tty))
191 tty->column = 0;
192 if (O_ONLCR(tty)) {
193 if (space < 2)
194 return -1;
195 tty->driver->put_char(tty, '\r');
196 tty->column = 0;
198 tty->canon_column = tty->column;
199 break;
200 case '\r':
201 if (O_ONOCR(tty) && tty->column == 0)
202 return 0;
203 if (O_OCRNL(tty)) {
204 c = '\n';
205 if (O_ONLRET(tty))
206 tty->canon_column = tty->column = 0;
207 break;
209 tty->canon_column = tty->column = 0;
210 break;
211 case '\t':
212 spaces = 8 - (tty->column & 7);
213 if (O_TABDLY(tty) == XTABS) {
214 if (space < spaces)
215 return -1;
216 tty->column += spaces;
217 tty->driver->write(tty, 0, " ", spaces);
218 return 0;
220 tty->column += spaces;
221 break;
222 case '\b':
223 if (tty->column > 0)
224 tty->column--;
225 break;
226 default:
227 if (O_OLCUC(tty))
228 c = toupper(c);
229 if (!iscntrl(c))
230 tty->column++;
231 break;
234 tty->driver->put_char(tty, c);
235 return 0;
239 * opost_block --- to speed up block console writes, among other
240 * things.
242 static ssize_t opost_block(struct tty_struct * tty,
243 const unsigned char * inbuf, unsigned int nr)
245 char buf[80];
246 int space;
247 int i;
248 char *cp;
250 space = tty->driver->write_room(tty);
251 if (!space)
252 return 0;
253 if (nr > space)
254 nr = space;
255 if (nr > sizeof(buf))
256 nr = sizeof(buf);
258 if (copy_from_user(buf, inbuf, nr))
259 return -EFAULT;
261 for (i = 0, cp = buf; i < nr; i++, cp++) {
262 switch (*cp) {
263 case '\n':
264 if (O_ONLRET(tty))
265 tty->column = 0;
266 if (O_ONLCR(tty))
267 goto break_out;
268 tty->canon_column = tty->column;
269 break;
270 case '\r':
271 if (O_ONOCR(tty) && tty->column == 0)
272 goto break_out;
273 if (O_OCRNL(tty)) {
274 *cp = '\n';
275 if (O_ONLRET(tty))
276 tty->canon_column = tty->column = 0;
277 break;
279 tty->canon_column = tty->column = 0;
280 break;
281 case '\t':
282 goto break_out;
283 case '\b':
284 if (tty->column > 0)
285 tty->column--;
286 break;
287 default:
288 if (O_OLCUC(tty))
289 *cp = toupper(*cp);
290 if (!iscntrl(*cp))
291 tty->column++;
292 break;
295 break_out:
296 if (tty->driver->flush_chars)
297 tty->driver->flush_chars(tty);
298 i = tty->driver->write(tty, 0, buf, i);
299 return i;
304 static inline void put_char(unsigned char c, struct tty_struct *tty)
306 tty->driver->put_char(tty, c);
309 /* Must be called only when L_ECHO(tty) is true. */
311 static void echo_char(unsigned char c, struct tty_struct *tty)
313 if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t') {
314 put_char('^', tty);
315 put_char(c ^ 0100, tty);
316 tty->column += 2;
317 } else
318 opost(c, tty);
321 static inline void finish_erasing(struct tty_struct *tty)
323 if (tty->erasing) {
324 put_char('/', tty);
325 tty->column++;
326 tty->erasing = 0;
330 static void eraser(unsigned char c, struct tty_struct *tty)
332 enum { ERASE, WERASE, KILL } kill_type;
333 int head, seen_alnums;
334 unsigned long flags;
336 if (tty->read_head == tty->canon_head) {
337 /* opost('\a', tty); */ /* what do you think? */
338 return;
340 if (c == ERASE_CHAR(tty))
341 kill_type = ERASE;
342 else if (c == WERASE_CHAR(tty))
343 kill_type = WERASE;
344 else {
345 if (!L_ECHO(tty)) {
346 spin_lock_irqsave(&tty->read_lock, flags);
347 tty->read_cnt -= ((tty->read_head - tty->canon_head) &
348 (N_TTY_BUF_SIZE - 1));
349 tty->read_head = tty->canon_head;
350 spin_unlock_irqrestore(&tty->read_lock, flags);
351 return;
353 if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
354 spin_lock_irqsave(&tty->read_lock, flags);
355 tty->read_cnt -= ((tty->read_head - tty->canon_head) &
356 (N_TTY_BUF_SIZE - 1));
357 tty->read_head = tty->canon_head;
358 spin_unlock_irqrestore(&tty->read_lock, flags);
359 finish_erasing(tty);
360 echo_char(KILL_CHAR(tty), tty);
361 /* Add a newline if ECHOK is on and ECHOKE is off. */
362 if (L_ECHOK(tty))
363 opost('\n', tty);
364 return;
366 kill_type = KILL;
369 seen_alnums = 0;
370 while (tty->read_head != tty->canon_head) {
371 head = (tty->read_head - 1) & (N_TTY_BUF_SIZE-1);
372 c = tty->read_buf[head];
373 if (kill_type == WERASE) {
374 /* Equivalent to BSD's ALTWERASE. */
375 if (isalnum(c) || c == '_')
376 seen_alnums++;
377 else if (seen_alnums)
378 break;
380 spin_lock_irqsave(&tty->read_lock, flags);
381 tty->read_head = head;
382 tty->read_cnt--;
383 spin_unlock_irqrestore(&tty->read_lock, flags);
384 if (L_ECHO(tty)) {
385 if (L_ECHOPRT(tty)) {
386 if (!tty->erasing) {
387 put_char('\\', tty);
388 tty->column++;
389 tty->erasing = 1;
391 echo_char(c, tty);
392 } else if (kill_type == ERASE && !L_ECHOE(tty)) {
393 echo_char(ERASE_CHAR(tty), tty);
394 } else if (c == '\t') {
395 unsigned int col = tty->canon_column;
396 unsigned long tail = tty->canon_head;
398 /* Find the column of the last char. */
399 while (tail != tty->read_head) {
400 c = tty->read_buf[tail];
401 if (c == '\t')
402 col = (col | 7) + 1;
403 else if (iscntrl(c)) {
404 if (L_ECHOCTL(tty))
405 col += 2;
406 } else
407 col++;
408 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
411 /* should never happen */
412 if (tty->column > 0x80000000)
413 tty->column = 0;
415 /* Now backup to that column. */
416 while (tty->column > col) {
417 /* Can't use opost here. */
418 put_char('\b', tty);
419 if (tty->column > 0)
420 tty->column--;
422 } else {
423 if (iscntrl(c) && L_ECHOCTL(tty)) {
424 put_char('\b', tty);
425 put_char(' ', tty);
426 put_char('\b', tty);
427 if (tty->column > 0)
428 tty->column--;
430 if (!iscntrl(c) || L_ECHOCTL(tty)) {
431 put_char('\b', tty);
432 put_char(' ', tty);
433 put_char('\b', tty);
434 if (tty->column > 0)
435 tty->column--;
439 if (kill_type == ERASE)
440 break;
442 if (tty->read_head == tty->canon_head)
443 finish_erasing(tty);
446 static inline void isig(int sig, struct tty_struct *tty, int flush)
448 if (tty->pgrp > 0)
449 kill_pg(tty->pgrp, sig, 1);
450 if (flush || !L_NOFLSH(tty)) {
451 n_tty_flush_buffer(tty);
452 if (tty->driver->flush_buffer)
453 tty->driver->flush_buffer(tty);
457 static inline void n_tty_receive_break(struct tty_struct *tty)
459 if (I_IGNBRK(tty))
460 return;
461 if (I_BRKINT(tty)) {
462 isig(SIGINT, tty, 1);
463 return;
465 if (I_PARMRK(tty)) {
466 put_tty_queue('\377', tty);
467 put_tty_queue('\0', tty);
469 put_tty_queue('\0', tty);
470 wake_up_interruptible(&tty->read_wait);
473 static inline void n_tty_receive_overrun(struct tty_struct *tty)
475 char buf[64];
477 tty->num_overrun++;
478 if (time_before(tty->overrun_time, jiffies - HZ)) {
479 printk("%s: %d input overrun(s)\n", tty_name(tty, buf),
480 tty->num_overrun);
481 tty->overrun_time = jiffies;
482 tty->num_overrun = 0;
486 static inline void n_tty_receive_parity_error(struct tty_struct *tty,
487 unsigned char c)
489 if (I_IGNPAR(tty)) {
490 return;
492 if (I_PARMRK(tty)) {
493 put_tty_queue('\377', tty);
494 put_tty_queue('\0', tty);
495 put_tty_queue(c, tty);
496 } else if (I_INPCK(tty))
497 put_tty_queue('\0', tty);
498 else
499 put_tty_queue(c, tty);
500 wake_up_interruptible(&tty->read_wait);
503 static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
505 unsigned long flags;
507 if (tty->raw) {
508 put_tty_queue(c, tty);
509 return;
512 if (tty->stopped && !tty->flow_stopped &&
513 I_IXON(tty) && I_IXANY(tty)) {
514 start_tty(tty);
515 return;
518 if (I_ISTRIP(tty))
519 c &= 0x7f;
520 if (I_IUCLC(tty) && L_IEXTEN(tty))
521 c=tolower(c);
523 if (tty->closing) {
524 if (I_IXON(tty)) {
525 if (c == START_CHAR(tty))
526 start_tty(tty);
527 else if (c == STOP_CHAR(tty))
528 stop_tty(tty);
530 return;
534 * If the previous character was LNEXT, or we know that this
535 * character is not one of the characters that we'll have to
536 * handle specially, do shortcut processing to speed things
537 * up.
539 if (!test_bit(c, tty->process_char_map) || tty->lnext) {
540 finish_erasing(tty);
541 tty->lnext = 0;
542 if (L_ECHO(tty)) {
543 if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
544 put_char('\a', tty); /* beep if no space */
545 return;
547 /* Record the column of first canon char. */
548 if (tty->canon_head == tty->read_head)
549 tty->canon_column = tty->column;
550 echo_char(c, tty);
552 if (I_PARMRK(tty) && c == (unsigned char) '\377')
553 put_tty_queue(c, tty);
554 put_tty_queue(c, tty);
555 return;
558 if (c == '\r') {
559 if (I_IGNCR(tty))
560 return;
561 if (I_ICRNL(tty))
562 c = '\n';
563 } else if (c == '\n' && I_INLCR(tty))
564 c = '\r';
565 if (I_IXON(tty)) {
566 if (c == START_CHAR(tty)) {
567 start_tty(tty);
568 return;
570 if (c == STOP_CHAR(tty)) {
571 stop_tty(tty);
572 return;
575 if (L_ISIG(tty)) {
576 int signal;
577 signal = SIGINT;
578 if (c == INTR_CHAR(tty))
579 goto send_signal;
580 signal = SIGQUIT;
581 if (c == QUIT_CHAR(tty))
582 goto send_signal;
583 signal = SIGTSTP;
584 if (c == SUSP_CHAR(tty)) {
585 send_signal:
586 isig(signal, tty, 0);
587 return;
590 if (tty->icanon) {
591 if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
592 (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
593 eraser(c, tty);
594 return;
596 if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
597 tty->lnext = 1;
598 if (L_ECHO(tty)) {
599 finish_erasing(tty);
600 if (L_ECHOCTL(tty)) {
601 put_char('^', tty);
602 put_char('\b', tty);
605 return;
607 if (c == REPRINT_CHAR(tty) && L_ECHO(tty) &&
608 L_IEXTEN(tty)) {
609 unsigned long tail = tty->canon_head;
611 finish_erasing(tty);
612 echo_char(c, tty);
613 opost('\n', tty);
614 while (tail != tty->read_head) {
615 echo_char(tty->read_buf[tail], tty);
616 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
618 return;
620 if (c == '\n') {
621 if (L_ECHO(tty) || L_ECHONL(tty)) {
622 if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
623 put_char('\a', tty);
624 return;
626 opost('\n', tty);
628 goto handle_newline;
630 if (c == EOF_CHAR(tty)) {
631 if (tty->canon_head != tty->read_head)
632 set_bit(TTY_PUSH, &tty->flags);
633 c = __DISABLED_CHAR;
634 goto handle_newline;
636 if ((c == EOL_CHAR(tty)) ||
637 (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
639 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
641 if (L_ECHO(tty)) {
642 if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
643 put_char('\a', tty);
644 return;
646 /* Record the column of first canon char. */
647 if (tty->canon_head == tty->read_head)
648 tty->canon_column = tty->column;
649 echo_char(c, tty);
652 * XXX does PARMRK doubling happen for
653 * EOL_CHAR and EOL2_CHAR?
655 if (I_PARMRK(tty) && c == (unsigned char) '\377')
656 put_tty_queue(c, tty);
658 handle_newline:
659 spin_lock_irqsave(&tty->read_lock, flags);
660 set_bit(tty->read_head, tty->read_flags);
661 put_tty_queue_nolock(c, tty);
662 tty->canon_head = tty->read_head;
663 tty->canon_data++;
664 spin_unlock_irqrestore(&tty->read_lock, flags);
665 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
666 if (waitqueue_active(&tty->read_wait))
667 wake_up_interruptible(&tty->read_wait);
668 return;
672 finish_erasing(tty);
673 if (L_ECHO(tty)) {
674 if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
675 put_char('\a', tty); /* beep if no space */
676 return;
678 if (c == '\n')
679 opost('\n', tty);
680 else {
681 /* Record the column of first canon char. */
682 if (tty->canon_head == tty->read_head)
683 tty->canon_column = tty->column;
684 echo_char(c, tty);
688 if (I_PARMRK(tty) && c == (unsigned char) '\377')
689 put_tty_queue(c, tty);
691 put_tty_queue(c, tty);
694 static int n_tty_receive_room(struct tty_struct *tty)
696 int left = N_TTY_BUF_SIZE - tty->read_cnt - 1;
699 * If we are doing input canonicalization, and there are no
700 * pending newlines, let characters through without limit, so
701 * that erase characters will be handled. Other excess
702 * characters will be beeped.
704 if (tty->icanon && !tty->canon_data)
705 return N_TTY_BUF_SIZE;
707 if (left > 0)
708 return left;
709 return 0;
713 * Required for the ptys, serial driver etc. since processes
714 * that attach themselves to the master and rely on ASYNC
715 * IO must be woken up
718 static void n_tty_write_wakeup(struct tty_struct *tty)
720 if (tty->fasync)
722 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
723 kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
725 return;
728 static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
729 char *fp, int count)
731 const unsigned char *p;
732 char *f, flags = TTY_NORMAL;
733 int i;
734 char buf[64];
735 unsigned long cpuflags;
737 if (!tty->read_buf)
738 return;
740 if (tty->real_raw) {
741 spin_lock_irqsave(&tty->read_lock, cpuflags);
742 i = min(N_TTY_BUF_SIZE - tty->read_cnt,
743 N_TTY_BUF_SIZE - tty->read_head);
744 i = min(count, i);
745 memcpy(tty->read_buf + tty->read_head, cp, i);
746 tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
747 tty->read_cnt += i;
748 cp += i;
749 count -= i;
751 i = min(N_TTY_BUF_SIZE - tty->read_cnt,
752 N_TTY_BUF_SIZE - tty->read_head);
753 i = min(count, i);
754 memcpy(tty->read_buf + tty->read_head, cp, i);
755 tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
756 tty->read_cnt += i;
757 spin_unlock_irqrestore(&tty->read_lock, cpuflags);
758 } else {
759 for (i=count, p = cp, f = fp; i; i--, p++) {
760 if (f)
761 flags = *f++;
762 switch (flags) {
763 case TTY_NORMAL:
764 n_tty_receive_char(tty, *p);
765 break;
766 case TTY_BREAK:
767 n_tty_receive_break(tty);
768 break;
769 case TTY_PARITY:
770 case TTY_FRAME:
771 n_tty_receive_parity_error(tty, *p);
772 break;
773 case TTY_OVERRUN:
774 n_tty_receive_overrun(tty);
775 break;
776 default:
777 printk("%s: unknown flag %d\n",
778 tty_name(tty, buf), flags);
779 break;
782 if (tty->driver->flush_chars)
783 tty->driver->flush_chars(tty);
786 if (!tty->icanon && (tty->read_cnt >= tty->minimum_to_wake)) {
787 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
788 if (waitqueue_active(&tty->read_wait))
789 wake_up_interruptible(&tty->read_wait);
793 * Check the remaining room for the input canonicalization
794 * mode. We don't want to throttle the driver if we're in
795 * canonical mode and don't have a newline yet!
797 if (n_tty_receive_room(tty) < TTY_THRESHOLD_THROTTLE) {
798 /* check TTY_THROTTLED first so it indicates our state */
799 if (!test_and_set_bit(TTY_THROTTLED, &tty->flags) &&
800 tty->driver->throttle)
801 tty->driver->throttle(tty);
805 int is_ignored(int sig)
807 return (sigismember(&current->blocked, sig) ||
808 current->sighand->action[sig-1].sa.sa_handler == SIG_IGN);
811 static void n_tty_set_termios(struct tty_struct *tty, struct termios * old)
813 if (!tty)
814 return;
816 tty->icanon = (L_ICANON(tty) != 0);
817 if (test_bit(TTY_HW_COOK_IN, &tty->flags)) {
818 tty->raw = 1;
819 tty->real_raw = 1;
820 return;
822 if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
823 I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
824 I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
825 I_PARMRK(tty)) {
826 local_irq_disable(); // FIXME: is this safe?
827 memset(tty->process_char_map, 0, 256/8);
829 if (I_IGNCR(tty) || I_ICRNL(tty))
830 set_bit('\r', tty->process_char_map);
831 if (I_INLCR(tty))
832 set_bit('\n', tty->process_char_map);
834 if (L_ICANON(tty)) {
835 set_bit(ERASE_CHAR(tty), tty->process_char_map);
836 set_bit(KILL_CHAR(tty), tty->process_char_map);
837 set_bit(EOF_CHAR(tty), tty->process_char_map);
838 set_bit('\n', tty->process_char_map);
839 set_bit(EOL_CHAR(tty), tty->process_char_map);
840 if (L_IEXTEN(tty)) {
841 set_bit(WERASE_CHAR(tty),
842 tty->process_char_map);
843 set_bit(LNEXT_CHAR(tty),
844 tty->process_char_map);
845 set_bit(EOL2_CHAR(tty),
846 tty->process_char_map);
847 if (L_ECHO(tty))
848 set_bit(REPRINT_CHAR(tty),
849 tty->process_char_map);
852 if (I_IXON(tty)) {
853 set_bit(START_CHAR(tty), tty->process_char_map);
854 set_bit(STOP_CHAR(tty), tty->process_char_map);
856 if (L_ISIG(tty)) {
857 set_bit(INTR_CHAR(tty), tty->process_char_map);
858 set_bit(QUIT_CHAR(tty), tty->process_char_map);
859 set_bit(SUSP_CHAR(tty), tty->process_char_map);
861 clear_bit(__DISABLED_CHAR, tty->process_char_map);
862 local_irq_enable(); // FIXME: is this safe?
863 tty->raw = 0;
864 tty->real_raw = 0;
865 } else {
866 tty->raw = 1;
867 if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
868 (I_IGNPAR(tty) || !I_INPCK(tty)) &&
869 (tty->driver->flags & TTY_DRIVER_REAL_RAW))
870 tty->real_raw = 1;
871 else
872 tty->real_raw = 0;
876 static void n_tty_close(struct tty_struct *tty)
878 n_tty_flush_buffer(tty);
879 if (tty->read_buf) {
880 free_buf(tty->read_buf);
881 tty->read_buf = 0;
885 static int n_tty_open(struct tty_struct *tty)
887 if (!tty)
888 return -EINVAL;
890 if (!tty->read_buf) {
891 tty->read_buf = alloc_buf();
892 if (!tty->read_buf)
893 return -ENOMEM;
895 memset(tty->read_buf, 0, N_TTY_BUF_SIZE);
896 reset_buffer_flags(tty);
897 tty->column = 0;
898 n_tty_set_termios(tty, 0);
899 tty->minimum_to_wake = 1;
900 tty->closing = 0;
901 return 0;
904 static inline int input_available_p(struct tty_struct *tty, int amt)
906 if (tty->icanon) {
907 if (tty->canon_data)
908 return 1;
909 } else if (tty->read_cnt >= (amt ? amt : 1))
910 return 1;
912 return 0;
916 * Helper function to speed up read_chan. It is only called when
917 * ICANON is off; it copies characters straight from the tty queue to
918 * user space directly. It can be profitably called twice; once to
919 * drain the space from the tail pointer to the (physical) end of the
920 * buffer, and once to drain the space from the (physical) beginning of
921 * the buffer to head pointer.
923 static inline int copy_from_read_buf(struct tty_struct *tty,
924 unsigned char **b,
925 size_t *nr)
928 int retval;
929 ssize_t n;
930 unsigned long flags;
932 retval = 0;
933 spin_lock_irqsave(&tty->read_lock, flags);
934 n = min(tty->read_cnt, N_TTY_BUF_SIZE - tty->read_tail);
935 n = min((ssize_t)*nr, n);
936 spin_unlock_irqrestore(&tty->read_lock, flags);
937 if (n) {
938 mb();
939 retval = copy_to_user(*b, &tty->read_buf[tty->read_tail], n);
940 n -= retval;
941 spin_lock_irqsave(&tty->read_lock, flags);
942 tty->read_tail = (tty->read_tail + n) & (N_TTY_BUF_SIZE-1);
943 tty->read_cnt -= n;
944 spin_unlock_irqrestore(&tty->read_lock, flags);
945 *b += n;
946 *nr -= n;
948 return retval;
951 extern ssize_t redirected_tty_write(struct file *,const char *,size_t,loff_t *);
953 static ssize_t read_chan(struct tty_struct *tty, struct file *file,
954 unsigned char *buf, size_t nr)
956 unsigned char *b = buf;
957 DECLARE_WAITQUEUE(wait, current);
958 int c;
959 int minimum, time;
960 ssize_t retval = 0;
961 ssize_t size;
962 long timeout;
963 unsigned long flags;
965 do_it_again:
967 if (!tty->read_buf) {
968 printk("n_tty_read_chan: called with read_buf == NULL?!?\n");
969 return -EIO;
972 /* Job control check -- must be done at start and after
973 every sleep (POSIX.1 7.1.1.4). */
974 /* NOTE: not yet done after every sleep pending a thorough
975 check of the logic of this change. -- jlc */
976 /* don't stop on /dev/console */
977 if (file->f_op->write != redirected_tty_write && current->tty == tty) {
978 if (tty->pgrp <= 0)
979 printk("read_chan: tty->pgrp <= 0!\n");
980 else if (process_group(current) != tty->pgrp) {
981 if (is_ignored(SIGTTIN) ||
982 is_orphaned_pgrp(process_group(current)))
983 return -EIO;
984 kill_pg(process_group(current), SIGTTIN, 1);
985 return -ERESTARTSYS;
989 minimum = time = 0;
990 timeout = MAX_SCHEDULE_TIMEOUT;
991 if (!tty->icanon) {
992 time = (HZ / 10) * TIME_CHAR(tty);
993 minimum = MIN_CHAR(tty);
994 if (minimum) {
995 if (time)
996 tty->minimum_to_wake = 1;
997 else if (!waitqueue_active(&tty->read_wait) ||
998 (tty->minimum_to_wake > minimum))
999 tty->minimum_to_wake = minimum;
1000 } else {
1001 timeout = 0;
1002 if (time) {
1003 timeout = time;
1004 time = 0;
1006 tty->minimum_to_wake = minimum = 1;
1010 if (file->f_flags & O_NONBLOCK) {
1011 if (down_trylock(&tty->atomic_read))
1012 return -EAGAIN;
1014 else {
1015 if (down_interruptible(&tty->atomic_read))
1016 return -ERESTARTSYS;
1019 add_wait_queue(&tty->read_wait, &wait);
1020 set_bit(TTY_DONT_FLIP, &tty->flags);
1021 while (nr) {
1022 /* First test for status change. */
1023 if (tty->packet && tty->link->ctrl_status) {
1024 unsigned char cs;
1025 if (b != buf)
1026 break;
1027 cs = tty->link->ctrl_status;
1028 tty->link->ctrl_status = 0;
1029 if (put_user(cs, b++)) {
1030 retval = -EFAULT;
1031 b--;
1032 break;
1034 nr--;
1035 break;
1037 /* This statement must be first before checking for input
1038 so that any interrupt will set the state back to
1039 TASK_RUNNING. */
1040 set_current_state(TASK_INTERRUPTIBLE);
1042 if (((minimum - (b - buf)) < tty->minimum_to_wake) &&
1043 ((minimum - (b - buf)) >= 1))
1044 tty->minimum_to_wake = (minimum - (b - buf));
1046 if (!input_available_p(tty, 0)) {
1047 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
1048 retval = -EIO;
1049 break;
1051 if (tty_hung_up_p(file))
1052 break;
1053 if (!timeout)
1054 break;
1055 if (file->f_flags & O_NONBLOCK) {
1056 retval = -EAGAIN;
1057 break;
1059 if (signal_pending(current)) {
1060 retval = -ERESTARTSYS;
1061 break;
1063 clear_bit(TTY_DONT_FLIP, &tty->flags);
1064 timeout = schedule_timeout(timeout);
1065 set_bit(TTY_DONT_FLIP, &tty->flags);
1066 continue;
1068 current->state = TASK_RUNNING;
1070 /* Deal with packet mode. */
1071 if (tty->packet && b == buf) {
1072 if (put_user(TIOCPKT_DATA, b++)) {
1073 retval = -EFAULT;
1074 b--;
1075 break;
1077 nr--;
1080 if (tty->icanon) {
1081 /* N.B. avoid overrun if nr == 0 */
1082 while (nr && tty->read_cnt) {
1083 int eol;
1085 eol = test_and_clear_bit(tty->read_tail,
1086 tty->read_flags);
1087 c = tty->read_buf[tty->read_tail];
1088 spin_lock_irqsave(&tty->read_lock, flags);
1089 tty->read_tail = ((tty->read_tail+1) &
1090 (N_TTY_BUF_SIZE-1));
1091 tty->read_cnt--;
1092 if (eol) {
1093 /* this test should be redundant:
1094 * we shouldn't be reading data if
1095 * canon_data is 0
1097 if (--tty->canon_data < 0)
1098 tty->canon_data = 0;
1100 spin_unlock_irqrestore(&tty->read_lock, flags);
1102 if (!eol || (c != __DISABLED_CHAR)) {
1103 if (put_user(c, b++)) {
1104 retval = -EFAULT;
1105 b--;
1106 break;
1108 nr--;
1110 if (eol)
1111 break;
1113 if (retval)
1114 break;
1115 } else {
1116 int uncopied;
1117 uncopied = copy_from_read_buf(tty, &b, &nr);
1118 uncopied += copy_from_read_buf(tty, &b, &nr);
1119 if (uncopied) {
1120 retval = -EFAULT;
1121 break;
1125 /* If there is enough space in the read buffer now, let the
1126 * low-level driver know. We use n_tty_chars_in_buffer() to
1127 * check the buffer, as it now knows about canonical mode.
1128 * Otherwise, if the driver is throttled and the line is
1129 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
1130 * we won't get any more characters.
1132 if (n_tty_chars_in_buffer(tty) <= TTY_THRESHOLD_UNTHROTTLE)
1133 check_unthrottle(tty);
1135 if (b - buf >= minimum)
1136 break;
1137 if (time)
1138 timeout = time;
1140 clear_bit(TTY_DONT_FLIP, &tty->flags);
1141 up(&tty->atomic_read);
1142 remove_wait_queue(&tty->read_wait, &wait);
1144 if (!waitqueue_active(&tty->read_wait))
1145 tty->minimum_to_wake = minimum;
1147 current->state = TASK_RUNNING;
1148 size = b - buf;
1149 if (size) {
1150 retval = size;
1151 if (nr)
1152 clear_bit(TTY_PUSH, &tty->flags);
1153 } else if (test_and_clear_bit(TTY_PUSH, &tty->flags))
1154 goto do_it_again;
1156 return retval;
1159 static ssize_t write_chan(struct tty_struct * tty, struct file * file,
1160 const unsigned char * buf, size_t nr)
1162 const unsigned char *b = buf;
1163 DECLARE_WAITQUEUE(wait, current);
1164 int c;
1165 ssize_t retval = 0;
1167 /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
1168 if (L_TOSTOP(tty) && file->f_op->write != redirected_tty_write) {
1169 retval = tty_check_change(tty);
1170 if (retval)
1171 return retval;
1174 add_wait_queue(&tty->write_wait, &wait);
1175 while (1) {
1176 set_current_state(TASK_INTERRUPTIBLE);
1177 if (signal_pending(current)) {
1178 retval = -ERESTARTSYS;
1179 break;
1181 if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
1182 retval = -EIO;
1183 break;
1185 if (O_OPOST(tty) && !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
1186 while (nr > 0) {
1187 ssize_t num = opost_block(tty, b, nr);
1188 if (num < 0) {
1189 if (num == -EAGAIN)
1190 break;
1191 retval = num;
1192 goto break_out;
1194 b += num;
1195 nr -= num;
1196 if (nr == 0)
1197 break;
1198 get_user(c, b);
1199 if (opost(c, tty) < 0)
1200 break;
1201 b++; nr--;
1203 if (tty->driver->flush_chars)
1204 tty->driver->flush_chars(tty);
1205 } else {
1206 c = tty->driver->write(tty, 1, b, nr);
1207 if (c < 0) {
1208 retval = c;
1209 goto break_out;
1211 b += c;
1212 nr -= c;
1214 if (!nr)
1215 break;
1216 if (file->f_flags & O_NONBLOCK) {
1217 retval = -EAGAIN;
1218 break;
1220 schedule();
1222 break_out:
1223 current->state = TASK_RUNNING;
1224 remove_wait_queue(&tty->write_wait, &wait);
1225 return (b - buf) ? b - buf : retval;
1228 /* Called without the kernel lock held - fine */
1229 static unsigned int normal_poll(struct tty_struct * tty, struct file * file, poll_table *wait)
1231 unsigned int mask = 0;
1233 poll_wait(file, &tty->read_wait, wait);
1234 poll_wait(file, &tty->write_wait, wait);
1235 if (input_available_p(tty, TIME_CHAR(tty) ? 0 : MIN_CHAR(tty)))
1236 mask |= POLLIN | POLLRDNORM;
1237 if (tty->packet && tty->link->ctrl_status)
1238 mask |= POLLPRI | POLLIN | POLLRDNORM;
1239 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
1240 mask |= POLLHUP;
1241 if (tty_hung_up_p(file))
1242 mask |= POLLHUP;
1243 if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) {
1244 if (MIN_CHAR(tty) && !TIME_CHAR(tty))
1245 tty->minimum_to_wake = MIN_CHAR(tty);
1246 else
1247 tty->minimum_to_wake = 1;
1249 if (tty->driver->chars_in_buffer(tty) < WAKEUP_CHARS &&
1250 tty->driver->write_room(tty) > 0)
1251 mask |= POLLOUT | POLLWRNORM;
1252 return mask;
1255 struct tty_ldisc tty_ldisc_N_TTY = {
1256 TTY_LDISC_MAGIC, /* magic */
1257 "n_tty", /* name */
1258 0, /* num */
1259 0, /* flags */
1260 n_tty_open, /* open */
1261 n_tty_close, /* close */
1262 n_tty_flush_buffer, /* flush_buffer */
1263 n_tty_chars_in_buffer, /* chars_in_buffer */
1264 read_chan, /* read */
1265 write_chan, /* write */
1266 n_tty_ioctl, /* ioctl */
1267 n_tty_set_termios, /* set_termios */
1268 normal_poll, /* poll */
1269 n_tty_receive_buf, /* receive_buf */
1270 n_tty_receive_room, /* receive_room */
1271 n_tty_write_wakeup /* write_wakeup */