Coarsly sort out 32-bit-only, 64-bit-only and ``portable'' MIPS lib/
[linux-2.6/linux-mips.git] / drivers / char / n_tty.c
blob83ef3bee6d76bcaf81f20d5f835b6ab20ea9ec52
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 #define IS_CONSOLE_DEV(dev) (kdev_val(dev) == __mkdev(TTY_MAJOR,0))
54 #define IS_SYSCONS_DEV(dev) (kdev_val(dev) == __mkdev(TTYAUX_MAJOR,1))
56 /* number of characters left in xmit buffer before select has we have room */
57 #define WAKEUP_CHARS 256
60 * This defines the low- and high-watermarks for throttling and
61 * unthrottling the TTY driver. These watermarks are used for
62 * controlling the space in the read buffer.
64 #define TTY_THRESHOLD_THROTTLE 128 /* now based on remaining room */
65 #define TTY_THRESHOLD_UNTHROTTLE 128
67 static inline unsigned char *alloc_buf(void)
69 unsigned char *p;
70 int prio = in_interrupt() ? GFP_ATOMIC : GFP_KERNEL;
72 if (PAGE_SIZE != N_TTY_BUF_SIZE) {
73 p = kmalloc(N_TTY_BUF_SIZE, prio);
74 if (p)
75 memset(p, 0, N_TTY_BUF_SIZE);
76 } else
77 p = (unsigned char *)get_zeroed_page(prio);
79 return p;
82 static inline void free_buf(unsigned char *buf)
84 if (PAGE_SIZE != N_TTY_BUF_SIZE)
85 kfree(buf);
86 else
87 free_page((unsigned long) buf);
90 static inline void put_tty_queue_nolock(unsigned char c, struct tty_struct *tty)
92 if (tty->read_cnt < N_TTY_BUF_SIZE) {
93 tty->read_buf[tty->read_head] = c;
94 tty->read_head = (tty->read_head + 1) & (N_TTY_BUF_SIZE-1);
95 tty->read_cnt++;
99 static inline void put_tty_queue(unsigned char c, struct tty_struct *tty)
101 unsigned long flags;
103 * The problem of stomping on the buffers ends here.
104 * Why didn't anyone see this one coming? --AJK
106 spin_lock_irqsave(&tty->read_lock, flags);
107 put_tty_queue_nolock(c, tty);
108 spin_unlock_irqrestore(&tty->read_lock, flags);
112 * Check whether to call the driver.unthrottle function.
113 * We test the TTY_THROTTLED bit first so that it always
114 * indicates the current state.
116 static void check_unthrottle(struct tty_struct * tty)
118 if (tty->count &&
119 test_and_clear_bit(TTY_THROTTLED, &tty->flags) &&
120 tty->driver->unthrottle)
121 tty->driver->unthrottle(tty);
125 * Reset the read buffer counters, clear the flags,
126 * and make sure the driver is unthrottled. Called
127 * from n_tty_open() and n_tty_flush_buffer().
129 static void reset_buffer_flags(struct tty_struct *tty)
131 unsigned long flags;
133 spin_lock_irqsave(&tty->read_lock, flags);
134 tty->read_head = tty->read_tail = tty->read_cnt = 0;
135 spin_unlock_irqrestore(&tty->read_lock, flags);
136 tty->canon_head = tty->canon_data = tty->erasing = 0;
137 memset(&tty->read_flags, 0, sizeof tty->read_flags);
138 check_unthrottle(tty);
142 * Flush the input buffer
144 void n_tty_flush_buffer(struct tty_struct * tty)
146 /* clear everything and unthrottle the driver */
147 reset_buffer_flags(tty);
149 if (!tty->link)
150 return;
152 if (tty->link->packet) {
153 tty->ctrl_status |= TIOCPKT_FLUSHREAD;
154 wake_up_interruptible(&tty->link->read_wait);
159 * Return number of characters buffered to be delivered to user
161 ssize_t n_tty_chars_in_buffer(struct tty_struct *tty)
163 unsigned long flags;
164 ssize_t n = 0;
166 spin_lock_irqsave(&tty->read_lock, flags);
167 if (!tty->icanon) {
168 n = tty->read_cnt;
169 } else if (tty->canon_data) {
170 n = (tty->canon_head > tty->read_tail) ?
171 tty->canon_head - tty->read_tail :
172 tty->canon_head + (N_TTY_BUF_SIZE - tty->read_tail);
174 spin_unlock_irqrestore(&tty->read_lock, flags);
175 return n;
179 * Perform OPOST processing. Returns -1 when the output device is
180 * full and the character must be retried.
182 static int opost(unsigned char c, struct tty_struct *tty)
184 int space, spaces;
186 space = tty->driver->write_room(tty);
187 if (!space)
188 return -1;
190 if (O_OPOST(tty)) {
191 switch (c) {
192 case '\n':
193 if (O_ONLRET(tty))
194 tty->column = 0;
195 if (O_ONLCR(tty)) {
196 if (space < 2)
197 return -1;
198 tty->driver->put_char(tty, '\r');
199 tty->column = 0;
201 tty->canon_column = tty->column;
202 break;
203 case '\r':
204 if (O_ONOCR(tty) && tty->column == 0)
205 return 0;
206 if (O_OCRNL(tty)) {
207 c = '\n';
208 if (O_ONLRET(tty))
209 tty->canon_column = tty->column = 0;
210 break;
212 tty->canon_column = tty->column = 0;
213 break;
214 case '\t':
215 spaces = 8 - (tty->column & 7);
216 if (O_TABDLY(tty) == XTABS) {
217 if (space < spaces)
218 return -1;
219 tty->column += spaces;
220 tty->driver->write(tty, 0, " ", spaces);
221 return 0;
223 tty->column += spaces;
224 break;
225 case '\b':
226 if (tty->column > 0)
227 tty->column--;
228 break;
229 default:
230 if (O_OLCUC(tty))
231 c = toupper(c);
232 if (!iscntrl(c))
233 tty->column++;
234 break;
237 tty->driver->put_char(tty, c);
238 return 0;
242 * opost_block --- to speed up block console writes, among other
243 * things.
245 static ssize_t opost_block(struct tty_struct * tty,
246 const unsigned char * inbuf, unsigned int nr)
248 char buf[80];
249 int space;
250 int i;
251 char *cp;
253 space = tty->driver->write_room(tty);
254 if (!space)
255 return 0;
256 if (nr > space)
257 nr = space;
258 if (nr > sizeof(buf))
259 nr = sizeof(buf);
261 if (copy_from_user(buf, inbuf, nr))
262 return -EFAULT;
264 for (i = 0, cp = buf; i < nr; i++, cp++) {
265 switch (*cp) {
266 case '\n':
267 if (O_ONLRET(tty))
268 tty->column = 0;
269 if (O_ONLCR(tty))
270 goto break_out;
271 tty->canon_column = tty->column;
272 break;
273 case '\r':
274 if (O_ONOCR(tty) && tty->column == 0)
275 goto break_out;
276 if (O_OCRNL(tty)) {
277 *cp = '\n';
278 if (O_ONLRET(tty))
279 tty->canon_column = tty->column = 0;
280 break;
282 tty->canon_column = tty->column = 0;
283 break;
284 case '\t':
285 goto break_out;
286 case '\b':
287 if (tty->column > 0)
288 tty->column--;
289 break;
290 default:
291 if (O_OLCUC(tty))
292 *cp = toupper(*cp);
293 if (!iscntrl(*cp))
294 tty->column++;
295 break;
298 break_out:
299 if (tty->driver->flush_chars)
300 tty->driver->flush_chars(tty);
301 i = tty->driver->write(tty, 0, buf, i);
302 return i;
307 static inline void put_char(unsigned char c, struct tty_struct *tty)
309 tty->driver->put_char(tty, c);
312 /* Must be called only when L_ECHO(tty) is true. */
314 static void echo_char(unsigned char c, struct tty_struct *tty)
316 if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t') {
317 put_char('^', tty);
318 put_char(c ^ 0100, tty);
319 tty->column += 2;
320 } else
321 opost(c, tty);
324 static inline void finish_erasing(struct tty_struct *tty)
326 if (tty->erasing) {
327 put_char('/', tty);
328 tty->column++;
329 tty->erasing = 0;
333 static void eraser(unsigned char c, struct tty_struct *tty)
335 enum { ERASE, WERASE, KILL } kill_type;
336 int head, seen_alnums;
337 unsigned long flags;
339 if (tty->read_head == tty->canon_head) {
340 /* opost('\a', tty); */ /* what do you think? */
341 return;
343 if (c == ERASE_CHAR(tty))
344 kill_type = ERASE;
345 else if (c == WERASE_CHAR(tty))
346 kill_type = WERASE;
347 else {
348 if (!L_ECHO(tty)) {
349 spin_lock_irqsave(&tty->read_lock, flags);
350 tty->read_cnt -= ((tty->read_head - tty->canon_head) &
351 (N_TTY_BUF_SIZE - 1));
352 tty->read_head = tty->canon_head;
353 spin_unlock_irqrestore(&tty->read_lock, flags);
354 return;
356 if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
357 spin_lock_irqsave(&tty->read_lock, flags);
358 tty->read_cnt -= ((tty->read_head - tty->canon_head) &
359 (N_TTY_BUF_SIZE - 1));
360 tty->read_head = tty->canon_head;
361 spin_unlock_irqrestore(&tty->read_lock, flags);
362 finish_erasing(tty);
363 echo_char(KILL_CHAR(tty), tty);
364 /* Add a newline if ECHOK is on and ECHOKE is off. */
365 if (L_ECHOK(tty))
366 opost('\n', tty);
367 return;
369 kill_type = KILL;
372 seen_alnums = 0;
373 while (tty->read_head != tty->canon_head) {
374 head = (tty->read_head - 1) & (N_TTY_BUF_SIZE-1);
375 c = tty->read_buf[head];
376 if (kill_type == WERASE) {
377 /* Equivalent to BSD's ALTWERASE. */
378 if (isalnum(c) || c == '_')
379 seen_alnums++;
380 else if (seen_alnums)
381 break;
383 spin_lock_irqsave(&tty->read_lock, flags);
384 tty->read_head = head;
385 tty->read_cnt--;
386 spin_unlock_irqrestore(&tty->read_lock, flags);
387 if (L_ECHO(tty)) {
388 if (L_ECHOPRT(tty)) {
389 if (!tty->erasing) {
390 put_char('\\', tty);
391 tty->column++;
392 tty->erasing = 1;
394 echo_char(c, tty);
395 } else if (kill_type == ERASE && !L_ECHOE(tty)) {
396 echo_char(ERASE_CHAR(tty), tty);
397 } else if (c == '\t') {
398 unsigned int col = tty->canon_column;
399 unsigned long tail = tty->canon_head;
401 /* Find the column of the last char. */
402 while (tail != tty->read_head) {
403 c = tty->read_buf[tail];
404 if (c == '\t')
405 col = (col | 7) + 1;
406 else if (iscntrl(c)) {
407 if (L_ECHOCTL(tty))
408 col += 2;
409 } else
410 col++;
411 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
414 /* should never happen */
415 if (tty->column > 0x80000000)
416 tty->column = 0;
418 /* Now backup to that column. */
419 while (tty->column > col) {
420 /* Can't use opost here. */
421 put_char('\b', tty);
422 if (tty->column > 0)
423 tty->column--;
425 } else {
426 if (iscntrl(c) && L_ECHOCTL(tty)) {
427 put_char('\b', tty);
428 put_char(' ', tty);
429 put_char('\b', tty);
430 if (tty->column > 0)
431 tty->column--;
433 if (!iscntrl(c) || L_ECHOCTL(tty)) {
434 put_char('\b', tty);
435 put_char(' ', tty);
436 put_char('\b', tty);
437 if (tty->column > 0)
438 tty->column--;
442 if (kill_type == ERASE)
443 break;
445 if (tty->read_head == tty->canon_head)
446 finish_erasing(tty);
449 static inline void isig(int sig, struct tty_struct *tty, int flush)
451 if (tty->pgrp > 0)
452 kill_pg(tty->pgrp, sig, 1);
453 if (flush || !L_NOFLSH(tty)) {
454 n_tty_flush_buffer(tty);
455 if (tty->driver->flush_buffer)
456 tty->driver->flush_buffer(tty);
460 static inline void n_tty_receive_break(struct tty_struct *tty)
462 if (I_IGNBRK(tty))
463 return;
464 if (I_BRKINT(tty)) {
465 isig(SIGINT, tty, 1);
466 return;
468 if (I_PARMRK(tty)) {
469 put_tty_queue('\377', tty);
470 put_tty_queue('\0', tty);
472 put_tty_queue('\0', tty);
473 wake_up_interruptible(&tty->read_wait);
476 static inline void n_tty_receive_overrun(struct tty_struct *tty)
478 char buf[64];
480 tty->num_overrun++;
481 if (time_before(tty->overrun_time, jiffies - HZ)) {
482 printk("%s: %d input overrun(s)\n", tty_name(tty, buf),
483 tty->num_overrun);
484 tty->overrun_time = jiffies;
485 tty->num_overrun = 0;
489 static inline void n_tty_receive_parity_error(struct tty_struct *tty,
490 unsigned char c)
492 if (I_IGNPAR(tty)) {
493 return;
495 if (I_PARMRK(tty)) {
496 put_tty_queue('\377', tty);
497 put_tty_queue('\0', tty);
498 put_tty_queue(c, tty);
499 } else if (I_INPCK(tty))
500 put_tty_queue('\0', tty);
501 else
502 put_tty_queue(c, tty);
503 wake_up_interruptible(&tty->read_wait);
506 static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
508 unsigned long flags;
510 if (tty->raw) {
511 put_tty_queue(c, tty);
512 return;
515 if (tty->stopped && !tty->flow_stopped &&
516 I_IXON(tty) && I_IXANY(tty)) {
517 start_tty(tty);
518 return;
521 if (I_ISTRIP(tty))
522 c &= 0x7f;
523 if (I_IUCLC(tty) && L_IEXTEN(tty))
524 c=tolower(c);
526 if (tty->closing) {
527 if (I_IXON(tty)) {
528 if (c == START_CHAR(tty))
529 start_tty(tty);
530 else if (c == STOP_CHAR(tty))
531 stop_tty(tty);
533 return;
537 * If the previous character was LNEXT, or we know that this
538 * character is not one of the characters that we'll have to
539 * handle specially, do shortcut processing to speed things
540 * up.
542 if (!test_bit(c, tty->process_char_map) || tty->lnext) {
543 finish_erasing(tty);
544 tty->lnext = 0;
545 if (L_ECHO(tty)) {
546 if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
547 put_char('\a', tty); /* beep if no space */
548 return;
550 /* Record the column of first canon char. */
551 if (tty->canon_head == tty->read_head)
552 tty->canon_column = tty->column;
553 echo_char(c, tty);
555 if (I_PARMRK(tty) && c == (unsigned char) '\377')
556 put_tty_queue(c, tty);
557 put_tty_queue(c, tty);
558 return;
561 if (c == '\r') {
562 if (I_IGNCR(tty))
563 return;
564 if (I_ICRNL(tty))
565 c = '\n';
566 } else if (c == '\n' && I_INLCR(tty))
567 c = '\r';
568 if (I_IXON(tty)) {
569 if (c == START_CHAR(tty)) {
570 start_tty(tty);
571 return;
573 if (c == STOP_CHAR(tty)) {
574 stop_tty(tty);
575 return;
578 if (L_ISIG(tty)) {
579 int signal;
580 signal = SIGINT;
581 if (c == INTR_CHAR(tty))
582 goto send_signal;
583 signal = SIGQUIT;
584 if (c == QUIT_CHAR(tty))
585 goto send_signal;
586 signal = SIGTSTP;
587 if (c == SUSP_CHAR(tty)) {
588 send_signal:
589 isig(signal, tty, 0);
590 return;
593 if (tty->icanon) {
594 if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
595 (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
596 eraser(c, tty);
597 return;
599 if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
600 tty->lnext = 1;
601 if (L_ECHO(tty)) {
602 finish_erasing(tty);
603 if (L_ECHOCTL(tty)) {
604 put_char('^', tty);
605 put_char('\b', tty);
608 return;
610 if (c == REPRINT_CHAR(tty) && L_ECHO(tty) &&
611 L_IEXTEN(tty)) {
612 unsigned long tail = tty->canon_head;
614 finish_erasing(tty);
615 echo_char(c, tty);
616 opost('\n', tty);
617 while (tail != tty->read_head) {
618 echo_char(tty->read_buf[tail], tty);
619 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
621 return;
623 if (c == '\n') {
624 if (L_ECHO(tty) || L_ECHONL(tty)) {
625 if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
626 put_char('\a', tty);
627 return;
629 opost('\n', tty);
631 goto handle_newline;
633 if (c == EOF_CHAR(tty)) {
634 if (tty->canon_head != tty->read_head)
635 set_bit(TTY_PUSH, &tty->flags);
636 c = __DISABLED_CHAR;
637 goto handle_newline;
639 if ((c == EOL_CHAR(tty)) ||
640 (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
642 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
644 if (L_ECHO(tty)) {
645 if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
646 put_char('\a', tty);
647 return;
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);
655 * XXX does PARMRK doubling happen for
656 * EOL_CHAR and EOL2_CHAR?
658 if (I_PARMRK(tty) && c == (unsigned char) '\377')
659 put_tty_queue(c, tty);
661 handle_newline:
662 spin_lock_irqsave(&tty->read_lock, flags);
663 set_bit(tty->read_head, tty->read_flags);
664 put_tty_queue_nolock(c, tty);
665 tty->canon_head = tty->read_head;
666 tty->canon_data++;
667 spin_unlock_irqrestore(&tty->read_lock, flags);
668 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
669 if (waitqueue_active(&tty->read_wait))
670 wake_up_interruptible(&tty->read_wait);
671 return;
675 finish_erasing(tty);
676 if (L_ECHO(tty)) {
677 if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
678 put_char('\a', tty); /* beep if no space */
679 return;
681 if (c == '\n')
682 opost('\n', tty);
683 else {
684 /* Record the column of first canon char. */
685 if (tty->canon_head == tty->read_head)
686 tty->canon_column = tty->column;
687 echo_char(c, tty);
691 if (I_PARMRK(tty) && c == (unsigned char) '\377')
692 put_tty_queue(c, tty);
694 put_tty_queue(c, tty);
697 static int n_tty_receive_room(struct tty_struct *tty)
699 int left = N_TTY_BUF_SIZE - tty->read_cnt - 1;
702 * If we are doing input canonicalization, and there are no
703 * pending newlines, let characters through without limit, so
704 * that erase characters will be handled. Other excess
705 * characters will be beeped.
707 if (tty->icanon && !tty->canon_data)
708 return N_TTY_BUF_SIZE;
710 if (left > 0)
711 return left;
712 return 0;
716 * Required for the ptys, serial driver etc. since processes
717 * that attach themselves to the master and rely on ASYNC
718 * IO must be woken up
721 static void n_tty_write_wakeup(struct tty_struct *tty)
723 if (tty->fasync)
725 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
726 kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
728 return;
731 static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
732 char *fp, int count)
734 const unsigned char *p;
735 char *f, flags = TTY_NORMAL;
736 int i;
737 char buf[64];
738 unsigned long cpuflags;
740 if (!tty->read_buf)
741 return;
743 if (tty->real_raw) {
744 spin_lock_irqsave(&tty->read_lock, cpuflags);
745 i = min(N_TTY_BUF_SIZE - tty->read_cnt,
746 N_TTY_BUF_SIZE - tty->read_head);
747 i = min(count, i);
748 memcpy(tty->read_buf + tty->read_head, cp, i);
749 tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
750 tty->read_cnt += i;
751 cp += i;
752 count -= i;
754 i = min(N_TTY_BUF_SIZE - tty->read_cnt,
755 N_TTY_BUF_SIZE - tty->read_head);
756 i = min(count, i);
757 memcpy(tty->read_buf + tty->read_head, cp, i);
758 tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
759 tty->read_cnt += i;
760 spin_unlock_irqrestore(&tty->read_lock, cpuflags);
761 } else {
762 for (i=count, p = cp, f = fp; i; i--, p++) {
763 if (f)
764 flags = *f++;
765 switch (flags) {
766 case TTY_NORMAL:
767 n_tty_receive_char(tty, *p);
768 break;
769 case TTY_BREAK:
770 n_tty_receive_break(tty);
771 break;
772 case TTY_PARITY:
773 case TTY_FRAME:
774 n_tty_receive_parity_error(tty, *p);
775 break;
776 case TTY_OVERRUN:
777 n_tty_receive_overrun(tty);
778 break;
779 default:
780 printk("%s: unknown flag %d\n",
781 tty_name(tty, buf), flags);
782 break;
785 if (tty->driver->flush_chars)
786 tty->driver->flush_chars(tty);
789 if (!tty->icanon && (tty->read_cnt >= tty->minimum_to_wake)) {
790 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
791 if (waitqueue_active(&tty->read_wait))
792 wake_up_interruptible(&tty->read_wait);
796 * Check the remaining room for the input canonicalization
797 * mode. We don't want to throttle the driver if we're in
798 * canonical mode and don't have a newline yet!
800 if (n_tty_receive_room(tty) < TTY_THRESHOLD_THROTTLE) {
801 /* check TTY_THROTTLED first so it indicates our state */
802 if (!test_and_set_bit(TTY_THROTTLED, &tty->flags) &&
803 tty->driver->throttle)
804 tty->driver->throttle(tty);
808 int is_ignored(int sig)
810 return (sigismember(&current->blocked, sig) ||
811 current->sighand->action[sig-1].sa.sa_handler == SIG_IGN);
814 static void n_tty_set_termios(struct tty_struct *tty, struct termios * old)
816 if (!tty)
817 return;
819 tty->icanon = (L_ICANON(tty) != 0);
820 if (test_bit(TTY_HW_COOK_IN, &tty->flags)) {
821 tty->raw = 1;
822 tty->real_raw = 1;
823 return;
825 if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
826 I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
827 I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
828 I_PARMRK(tty)) {
829 local_irq_disable(); // FIXME: is this safe?
830 memset(tty->process_char_map, 0, 256/8);
832 if (I_IGNCR(tty) || I_ICRNL(tty))
833 set_bit('\r', tty->process_char_map);
834 if (I_INLCR(tty))
835 set_bit('\n', tty->process_char_map);
837 if (L_ICANON(tty)) {
838 set_bit(ERASE_CHAR(tty), tty->process_char_map);
839 set_bit(KILL_CHAR(tty), tty->process_char_map);
840 set_bit(EOF_CHAR(tty), tty->process_char_map);
841 set_bit('\n', tty->process_char_map);
842 set_bit(EOL_CHAR(tty), tty->process_char_map);
843 if (L_IEXTEN(tty)) {
844 set_bit(WERASE_CHAR(tty),
845 tty->process_char_map);
846 set_bit(LNEXT_CHAR(tty),
847 tty->process_char_map);
848 set_bit(EOL2_CHAR(tty),
849 tty->process_char_map);
850 if (L_ECHO(tty))
851 set_bit(REPRINT_CHAR(tty),
852 tty->process_char_map);
855 if (I_IXON(tty)) {
856 set_bit(START_CHAR(tty), tty->process_char_map);
857 set_bit(STOP_CHAR(tty), tty->process_char_map);
859 if (L_ISIG(tty)) {
860 set_bit(INTR_CHAR(tty), tty->process_char_map);
861 set_bit(QUIT_CHAR(tty), tty->process_char_map);
862 set_bit(SUSP_CHAR(tty), tty->process_char_map);
864 clear_bit(__DISABLED_CHAR, tty->process_char_map);
865 local_irq_enable(); // FIXME: is this safe?
866 tty->raw = 0;
867 tty->real_raw = 0;
868 } else {
869 tty->raw = 1;
870 if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
871 (I_IGNPAR(tty) || !I_INPCK(tty)) &&
872 (tty->driver->flags & TTY_DRIVER_REAL_RAW))
873 tty->real_raw = 1;
874 else
875 tty->real_raw = 0;
879 static void n_tty_close(struct tty_struct *tty)
881 n_tty_flush_buffer(tty);
882 if (tty->read_buf) {
883 free_buf(tty->read_buf);
884 tty->read_buf = 0;
888 static int n_tty_open(struct tty_struct *tty)
890 if (!tty)
891 return -EINVAL;
893 if (!tty->read_buf) {
894 tty->read_buf = alloc_buf();
895 if (!tty->read_buf)
896 return -ENOMEM;
898 memset(tty->read_buf, 0, N_TTY_BUF_SIZE);
899 reset_buffer_flags(tty);
900 tty->column = 0;
901 n_tty_set_termios(tty, 0);
902 tty->minimum_to_wake = 1;
903 tty->closing = 0;
904 return 0;
907 static inline int input_available_p(struct tty_struct *tty, int amt)
909 if (tty->icanon) {
910 if (tty->canon_data)
911 return 1;
912 } else if (tty->read_cnt >= (amt ? amt : 1))
913 return 1;
915 return 0;
919 * Helper function to speed up read_chan. It is only called when
920 * ICANON is off; it copies characters straight from the tty queue to
921 * user space directly. It can be profitably called twice; once to
922 * drain the space from the tail pointer to the (physical) end of the
923 * buffer, and once to drain the space from the (physical) beginning of
924 * the buffer to head pointer.
926 static inline int copy_from_read_buf(struct tty_struct *tty,
927 unsigned char **b,
928 size_t *nr)
931 int retval;
932 ssize_t n;
933 unsigned long flags;
935 retval = 0;
936 spin_lock_irqsave(&tty->read_lock, flags);
937 n = min(tty->read_cnt, N_TTY_BUF_SIZE - tty->read_tail);
938 n = min((ssize_t)*nr, n);
939 spin_unlock_irqrestore(&tty->read_lock, flags);
940 if (n) {
941 mb();
942 retval = copy_to_user(*b, &tty->read_buf[tty->read_tail], n);
943 n -= retval;
944 spin_lock_irqsave(&tty->read_lock, flags);
945 tty->read_tail = (tty->read_tail + n) & (N_TTY_BUF_SIZE-1);
946 tty->read_cnt -= n;
947 spin_unlock_irqrestore(&tty->read_lock, flags);
948 *b += n;
949 *nr -= n;
951 return retval;
954 static ssize_t read_chan(struct tty_struct *tty, struct file *file,
955 unsigned char *buf, size_t nr)
957 unsigned char *b = buf;
958 DECLARE_WAITQUEUE(wait, current);
959 int c;
960 int minimum, time;
961 ssize_t retval = 0;
962 ssize_t size;
963 long timeout;
964 unsigned long flags;
966 do_it_again:
968 if (!tty->read_buf) {
969 printk("n_tty_read_chan: called with read_buf == NULL?!?\n");
970 return -EIO;
973 /* Job control check -- must be done at start and after
974 every sleep (POSIX.1 7.1.1.4). */
975 /* NOTE: not yet done after every sleep pending a thorough
976 check of the logic of this change. -- jlc */
977 /* don't stop on /dev/console */
978 if (!IS_CONSOLE_DEV(file->f_dentry->d_inode->i_rdev) &&
979 !IS_SYSCONS_DEV(file->f_dentry->d_inode->i_rdev) &&
980 current->tty == tty) {
981 if (tty->pgrp <= 0)
982 printk("read_chan: tty->pgrp <= 0!\n");
983 else if (current->pgrp != tty->pgrp) {
984 if (is_ignored(SIGTTIN) ||
985 is_orphaned_pgrp(current->pgrp))
986 return -EIO;
987 kill_pg(current->pgrp, SIGTTIN, 1);
988 return -ERESTARTSYS;
992 minimum = time = 0;
993 timeout = MAX_SCHEDULE_TIMEOUT;
994 if (!tty->icanon) {
995 time = (HZ / 10) * TIME_CHAR(tty);
996 minimum = MIN_CHAR(tty);
997 if (minimum) {
998 if (time)
999 tty->minimum_to_wake = 1;
1000 else if (!waitqueue_active(&tty->read_wait) ||
1001 (tty->minimum_to_wake > minimum))
1002 tty->minimum_to_wake = minimum;
1003 } else {
1004 timeout = 0;
1005 if (time) {
1006 timeout = time;
1007 time = 0;
1009 tty->minimum_to_wake = minimum = 1;
1013 if (file->f_flags & O_NONBLOCK) {
1014 if (down_trylock(&tty->atomic_read))
1015 return -EAGAIN;
1017 else {
1018 if (down_interruptible(&tty->atomic_read))
1019 return -ERESTARTSYS;
1022 add_wait_queue(&tty->read_wait, &wait);
1023 set_bit(TTY_DONT_FLIP, &tty->flags);
1024 while (nr) {
1025 /* First test for status change. */
1026 if (tty->packet && tty->link->ctrl_status) {
1027 unsigned char cs;
1028 if (b != buf)
1029 break;
1030 cs = tty->link->ctrl_status;
1031 tty->link->ctrl_status = 0;
1032 if (put_user(cs, b++)) {
1033 retval = -EFAULT;
1034 b--;
1035 break;
1037 nr--;
1038 break;
1040 /* This statement must be first before checking for input
1041 so that any interrupt will set the state back to
1042 TASK_RUNNING. */
1043 set_current_state(TASK_INTERRUPTIBLE);
1045 if (((minimum - (b - buf)) < tty->minimum_to_wake) &&
1046 ((minimum - (b - buf)) >= 1))
1047 tty->minimum_to_wake = (minimum - (b - buf));
1049 if (!input_available_p(tty, 0)) {
1050 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
1051 retval = -EIO;
1052 break;
1054 if (tty_hung_up_p(file))
1055 break;
1056 if (!timeout)
1057 break;
1058 if (file->f_flags & O_NONBLOCK) {
1059 retval = -EAGAIN;
1060 break;
1062 if (signal_pending(current)) {
1063 retval = -ERESTARTSYS;
1064 break;
1066 clear_bit(TTY_DONT_FLIP, &tty->flags);
1067 timeout = schedule_timeout(timeout);
1068 set_bit(TTY_DONT_FLIP, &tty->flags);
1069 continue;
1071 current->state = TASK_RUNNING;
1073 /* Deal with packet mode. */
1074 if (tty->packet && b == buf) {
1075 if (put_user(TIOCPKT_DATA, b++)) {
1076 retval = -EFAULT;
1077 b--;
1078 break;
1080 nr--;
1083 if (tty->icanon) {
1084 /* N.B. avoid overrun if nr == 0 */
1085 while (nr && tty->read_cnt) {
1086 int eol;
1088 eol = test_and_clear_bit(tty->read_tail,
1089 tty->read_flags);
1090 c = tty->read_buf[tty->read_tail];
1091 spin_lock_irqsave(&tty->read_lock, flags);
1092 tty->read_tail = ((tty->read_tail+1) &
1093 (N_TTY_BUF_SIZE-1));
1094 tty->read_cnt--;
1095 if (eol) {
1096 /* this test should be redundant:
1097 * we shouldn't be reading data if
1098 * canon_data is 0
1100 if (--tty->canon_data < 0)
1101 tty->canon_data = 0;
1103 spin_unlock_irqrestore(&tty->read_lock, flags);
1105 if (!eol || (c != __DISABLED_CHAR)) {
1106 if (put_user(c, b++)) {
1107 retval = -EFAULT;
1108 b--;
1109 break;
1111 nr--;
1113 if (eol)
1114 break;
1116 if (retval)
1117 break;
1118 } else {
1119 int uncopied;
1120 uncopied = copy_from_read_buf(tty, &b, &nr);
1121 uncopied += copy_from_read_buf(tty, &b, &nr);
1122 if (uncopied) {
1123 retval = -EFAULT;
1124 break;
1128 /* If there is enough space in the read buffer now, let the
1129 * low-level driver know. We use n_tty_chars_in_buffer() to
1130 * check the buffer, as it now knows about canonical mode.
1131 * Otherwise, if the driver is throttled and the line is
1132 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
1133 * we won't get any more characters.
1135 if (n_tty_chars_in_buffer(tty) <= TTY_THRESHOLD_UNTHROTTLE)
1136 check_unthrottle(tty);
1138 if (b - buf >= minimum)
1139 break;
1140 if (time)
1141 timeout = time;
1143 clear_bit(TTY_DONT_FLIP, &tty->flags);
1144 up(&tty->atomic_read);
1145 remove_wait_queue(&tty->read_wait, &wait);
1147 if (!waitqueue_active(&tty->read_wait))
1148 tty->minimum_to_wake = minimum;
1150 current->state = TASK_RUNNING;
1151 size = b - buf;
1152 if (size) {
1153 retval = size;
1154 if (nr)
1155 clear_bit(TTY_PUSH, &tty->flags);
1156 } else if (test_and_clear_bit(TTY_PUSH, &tty->flags))
1157 goto do_it_again;
1159 return retval;
1162 static ssize_t write_chan(struct tty_struct * tty, struct file * file,
1163 const unsigned char * buf, size_t nr)
1165 const unsigned char *b = buf;
1166 DECLARE_WAITQUEUE(wait, current);
1167 int c;
1168 ssize_t retval = 0;
1170 /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
1171 if (L_TOSTOP(tty) &&
1172 !IS_CONSOLE_DEV(file->f_dentry->d_inode->i_rdev) &&
1173 !IS_SYSCONS_DEV(file->f_dentry->d_inode->i_rdev)) {
1174 retval = tty_check_change(tty);
1175 if (retval)
1176 return retval;
1179 add_wait_queue(&tty->write_wait, &wait);
1180 while (1) {
1181 set_current_state(TASK_INTERRUPTIBLE);
1182 if (signal_pending(current)) {
1183 retval = -ERESTARTSYS;
1184 break;
1186 if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
1187 retval = -EIO;
1188 break;
1190 if (O_OPOST(tty) && !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
1191 while (nr > 0) {
1192 ssize_t num = opost_block(tty, b, nr);
1193 if (num < 0) {
1194 if (num == -EAGAIN)
1195 break;
1196 retval = num;
1197 goto break_out;
1199 b += num;
1200 nr -= num;
1201 if (nr == 0)
1202 break;
1203 get_user(c, b);
1204 if (opost(c, tty) < 0)
1205 break;
1206 b++; nr--;
1208 if (tty->driver->flush_chars)
1209 tty->driver->flush_chars(tty);
1210 } else {
1211 c = tty->driver->write(tty, 1, b, nr);
1212 if (c < 0) {
1213 retval = c;
1214 goto break_out;
1216 b += c;
1217 nr -= c;
1219 if (!nr)
1220 break;
1221 if (file->f_flags & O_NONBLOCK) {
1222 retval = -EAGAIN;
1223 break;
1225 schedule();
1227 break_out:
1228 current->state = TASK_RUNNING;
1229 remove_wait_queue(&tty->write_wait, &wait);
1230 return (b - buf) ? b - buf : retval;
1233 /* Called without the kernel lock held - fine */
1234 static unsigned int normal_poll(struct tty_struct * tty, struct file * file, poll_table *wait)
1236 unsigned int mask = 0;
1238 poll_wait(file, &tty->read_wait, wait);
1239 poll_wait(file, &tty->write_wait, wait);
1240 if (input_available_p(tty, TIME_CHAR(tty) ? 0 : MIN_CHAR(tty)))
1241 mask |= POLLIN | POLLRDNORM;
1242 if (tty->packet && tty->link->ctrl_status)
1243 mask |= POLLPRI | POLLIN | POLLRDNORM;
1244 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
1245 mask |= POLLHUP;
1246 if (tty_hung_up_p(file))
1247 mask |= POLLHUP;
1248 if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) {
1249 if (MIN_CHAR(tty) && !TIME_CHAR(tty))
1250 tty->minimum_to_wake = MIN_CHAR(tty);
1251 else
1252 tty->minimum_to_wake = 1;
1254 if (tty->driver->chars_in_buffer(tty) < WAKEUP_CHARS)
1255 mask |= POLLOUT | POLLWRNORM;
1256 return mask;
1259 struct tty_ldisc tty_ldisc_N_TTY = {
1260 TTY_LDISC_MAGIC, /* magic */
1261 "n_tty", /* name */
1262 0, /* num */
1263 0, /* flags */
1264 n_tty_open, /* open */
1265 n_tty_close, /* close */
1266 n_tty_flush_buffer, /* flush_buffer */
1267 n_tty_chars_in_buffer, /* chars_in_buffer */
1268 read_chan, /* read */
1269 write_chan, /* write */
1270 n_tty_ioctl, /* ioctl */
1271 n_tty_set_termios, /* set_termios */
1272 normal_poll, /* poll */
1273 n_tty_receive_buf, /* receive_buf */
1274 n_tty_receive_room, /* receive_room */
1275 n_tty_write_wakeup /* write_wakeup */