File: /usr/src/linux/drivers/char/n_tty.c
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...)
8 *
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.
12 *
13 * Written by Theodore Ts'o, Copyright 1994.
14 *
15 * This file also contains code originally written by Linus Torvalds,
16 * Copyright 1991, 1992, 1993, and by Julian Cowley, Copyright 1994.
17 *
18 * This file may be redistributed under the terms of the GNU General Public
19 * License.
20 *
21 * Reduced memory usage for older ARM systems - Russell King.
22 *
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.
26 */
27
28 #include <linux/types.h>
29 #include <linux/major.h>
30 #include <linux/errno.h>
31 #include <linux/signal.h>
32 #include <linux/fcntl.h>
33 #include <linux/sched.h>
34 #include <linux/interrupt.h>
35 #include <linux/tty.h>
36 #include <linux/timer.h>
37 #include <linux/ctype.h>
38 #include <linux/kd.h>
39 #include <linux/mm.h>
40 #include <linux/string.h>
41 #include <linux/slab.h>
42 #include <linux/poll.h>
43
44 #include <asm/uaccess.h>
45 #include <asm/system.h>
46 #include <asm/bitops.h>
47
48 #define CONSOLE_DEV MKDEV(TTY_MAJOR,0)
49 #define SYSCONS_DEV MKDEV(TTYAUX_MAJOR,1)
50
51 #ifndef MIN
52 #define MIN(a,b) ((a) < (b) ? (a) : (b))
53 #endif
54
55 /* number of characters left in xmit buffer before select has we have room */
56 #define WAKEUP_CHARS 256
57
58 /*
59 * This defines the low- and high-watermarks for throttling and
60 * unthrottling the TTY driver. These watermarks are used for
61 * controlling the space in the read buffer.
62 */
63 #define TTY_THRESHOLD_THROTTLE 128 /* now based on remaining room */
64 #define TTY_THRESHOLD_UNTHROTTLE 128
65
66 static inline unsigned char *alloc_buf(void)
67 {
68 unsigned char *p;
69 int prio = in_interrupt() ? GFP_ATOMIC : GFP_KERNEL;
70
71 if (PAGE_SIZE != N_TTY_BUF_SIZE) {
72 p = kmalloc(N_TTY_BUF_SIZE, prio);
73 if (p)
74 memset(p, 0, N_TTY_BUF_SIZE);
75 } else
76 p = (unsigned char *)get_zeroed_page(prio);
77
78 return p;
79 }
80
81 static inline void free_buf(unsigned char *buf)
82 {
83 if (PAGE_SIZE != N_TTY_BUF_SIZE)
84 kfree(buf);
85 else
86 free_page((unsigned long) buf);
87 }
88
89 static inline void put_tty_queue_nolock(unsigned char c, struct tty_struct *tty)
90 {
91 if (tty->read_cnt < N_TTY_BUF_SIZE) {
92 tty->read_buf[tty->read_head] = c;
93 tty->read_head = (tty->read_head + 1) & (N_TTY_BUF_SIZE-1);
94 tty->read_cnt++;
95 }
96 }
97
98 static inline void put_tty_queue(unsigned char c, struct tty_struct *tty)
99 {
100 unsigned long flags;
101 /*
102 * The problem of stomping on the buffers ends here.
103 * Why didn't anyone see this one coming? --AJK
104 */
105 spin_lock_irqsave(&tty->read_lock, flags);
106 put_tty_queue_nolock(c, tty);
107 spin_unlock_irqrestore(&tty->read_lock, flags);
108 }
109
110 /*
111 * Check whether to call the driver.unthrottle function.
112 * We test the TTY_THROTTLED bit first so that it always
113 * indicates the current state.
114 */
115 static void check_unthrottle(struct tty_struct * tty)
116 {
117 if (tty->count &&
118 test_and_clear_bit(TTY_THROTTLED, &tty->flags) &&
119 tty->driver.unthrottle)
120 tty->driver.unthrottle(tty);
121 }
122
123 /*
124 * Reset the read buffer counters, clear the flags,
125 * and make sure the driver is unthrottled. Called
126 * from n_tty_open() and n_tty_flush_buffer().
127 */
128 static void reset_buffer_flags(struct tty_struct *tty)
129 {
130 unsigned long flags;
131
132 spin_lock_irqsave(&tty->read_lock, flags);
133 tty->read_head = tty->read_tail = tty->read_cnt = 0;
134 spin_unlock_irqrestore(&tty->read_lock, flags);
135 tty->canon_head = tty->canon_data = tty->erasing = 0;
136 memset(&tty->read_flags, 0, sizeof tty->read_flags);
137 check_unthrottle(tty);
138 }
139
140 /*
141 * Flush the input buffer
142 */
143 void n_tty_flush_buffer(struct tty_struct * tty)
144 {
145 /* clear everything and unthrottle the driver */
146 reset_buffer_flags(tty);
147
148 if (!tty->link)
149 return;
150
151 if (tty->link->packet) {
152 tty->ctrl_status |= TIOCPKT_FLUSHREAD;
153 wake_up_interruptible(&tty->link->read_wait);
154 }
155 }
156
157 /*
158 * Return number of characters buffered to be delivered to user
159 */
160 ssize_t n_tty_chars_in_buffer(struct tty_struct *tty)
161 {
162 unsigned long flags;
163 ssize_t n = 0;
164
165 spin_lock_irqsave(&tty->read_lock, flags);
166 if (!tty->icanon) {
167 n = tty->read_cnt;
168 } else if (tty->canon_data) {
169 n = (tty->canon_head > tty->read_tail) ?
170 tty->canon_head - tty->read_tail :
171 tty->canon_head + (N_TTY_BUF_SIZE - tty->read_tail);
172 }
173 spin_unlock_irqrestore(&tty->read_lock, flags);
174 return n;
175 }
176
177 /*
178 * Perform OPOST processing. Returns -1 when the output device is
179 * full and the character must be retried.
180 */
181 static int opost(unsigned char c, struct tty_struct *tty)
182 {
183 int space, spaces;
184
185 space = tty->driver.write_room(tty);
186 if (!space)
187 return -1;
188
189 if (O_OPOST(tty)) {
190 switch (c) {
191 case '\n':
192 if (O_ONLRET(tty))
193 tty->column = 0;
194 if (O_ONLCR(tty)) {
195 if (space < 2)
196 return -1;
197 tty->driver.put_char(tty, '\r');
198 tty->column = 0;
199 }
200 tty->canon_column = tty->column;
201 break;
202 case '\r':
203 if (O_ONOCR(tty) && tty->column == 0)
204 return 0;
205 if (O_OCRNL(tty)) {
206 c = '\n';
207 if (O_ONLRET(tty))
208 tty->canon_column = tty->column = 0;
209 break;
210 }
211 tty->canon_column = tty->column = 0;
212 break;
213 case '\t':
214 spaces = 8 - (tty->column & 7);
215 if (O_TABDLY(tty) == XTABS) {
216 if (space < spaces)
217 return -1;
218 tty->column += spaces;
219 tty->driver.write(tty, 0, " ", spaces);
220 return 0;
221 }
222 tty->column += spaces;
223 break;
224 case '\b':
225 if (tty->column > 0)
226 tty->column--;
227 break;
228 default:
229 if (O_OLCUC(tty))
230 c = toupper(c);
231 if (!iscntrl(c))
232 tty->column++;
233 break;
234 }
235 }
236 tty->driver.put_char(tty, c);
237 return 0;
238 }
239
240 /*
241 * opost_block --- to speed up block console writes, among other
242 * things.
243 */
244 static ssize_t opost_block(struct tty_struct * tty,
245 const unsigned char * inbuf, unsigned int nr)
246 {
247 char buf[80];
248 int space;
249 int i;
250 char *cp;
251
252 space = tty->driver.write_room(tty);
253 if (!space)
254 return 0;
255 if (nr > space)
256 nr = space;
257 if (nr > sizeof(buf))
258 nr = sizeof(buf);
259
260 if (copy_from_user(buf, inbuf, nr))
261 return -EFAULT;
262
263 for (i = 0, cp = buf; i < nr; i++, cp++) {
264 switch (*cp) {
265 case '\n':
266 if (O_ONLRET(tty))
267 tty->column = 0;
268 if (O_ONLCR(tty))
269 goto break_out;
270 tty->canon_column = tty->column;
271 break;
272 case '\r':
273 if (O_ONOCR(tty) && tty->column == 0)
274 goto break_out;
275 if (O_OCRNL(tty)) {
276 *cp = '\n';
277 if (O_ONLRET(tty))
278 tty->canon_column = tty->column = 0;
279 break;
280 }
281 tty->canon_column = tty->column = 0;
282 break;
283 case '\t':
284 goto break_out;
285 case '\b':
286 if (tty->column > 0)
287 tty->column--;
288 break;
289 default:
290 if (O_OLCUC(tty))
291 *cp = toupper(*cp);
292 if (!iscntrl(*cp))
293 tty->column++;
294 break;
295 }
296 }
297 break_out:
298 if (tty->driver.flush_chars)
299 tty->driver.flush_chars(tty);
300 i = tty->driver.write(tty, 0, buf, i);
301 return i;
302 }
303
304
305
306 static inline void put_char(unsigned char c, struct tty_struct *tty)
307 {
308 tty->driver.put_char(tty, c);
309 }
310
311 /* Must be called only when L_ECHO(tty) is true. */
312
313 static void echo_char(unsigned char c, struct tty_struct *tty)
314 {
315 if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t') {
316 put_char('^', tty);
317 put_char(c ^ 0100, tty);
318 tty->column += 2;
319 } else
320 opost(c, tty);
321 }
322
323 static inline void finish_erasing(struct tty_struct *tty)
324 {
325 if (tty->erasing) {
326 put_char('/', tty);
327 tty->column += 2;
328 tty->erasing = 0;
329 }
330 }
331
332 static void eraser(unsigned char c, struct tty_struct *tty)
333 {
334 enum { ERASE, WERASE, KILL } kill_type;
335 int head, seen_alnums;
336 unsigned long flags;
337
338 if (tty->read_head == tty->canon_head) {
339 /* opost('\a', tty); */ /* what do you think? */
340 return;
341 }
342 if (c == ERASE_CHAR(tty))
343 kill_type = ERASE;
344 else if (c == WERASE_CHAR(tty))
345 kill_type = WERASE;
346 else {
347 if (!L_ECHO(tty)) {
348 spin_lock_irqsave(&tty->read_lock, flags);
349 tty->read_cnt -= ((tty->read_head - tty->canon_head) &
350 (N_TTY_BUF_SIZE - 1));
351 tty->read_head = tty->canon_head;
352 spin_unlock_irqrestore(&tty->read_lock, flags);
353 return;
354 }
355 if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
356 spin_lock_irqsave(&tty->read_lock, flags);
357 tty->read_cnt -= ((tty->read_head - tty->canon_head) &
358 (N_TTY_BUF_SIZE - 1));
359 tty->read_head = tty->canon_head;
360 spin_unlock_irqrestore(&tty->read_lock, flags);
361 finish_erasing(tty);
362 echo_char(KILL_CHAR(tty), tty);
363 /* Add a newline if ECHOK is on and ECHOKE is off. */
364 if (L_ECHOK(tty))
365 opost('\n', tty);
366 return;
367 }
368 kill_type = KILL;
369 }
370
371 seen_alnums = 0;
372 while (tty->read_head != tty->canon_head) {
373 head = (tty->read_head - 1) & (N_TTY_BUF_SIZE-1);
374 c = tty->read_buf[head];
375 if (kill_type == WERASE) {
376 /* Equivalent to BSD's ALTWERASE. */
377 if (isalnum(c) || c == '_')
378 seen_alnums++;
379 else if (seen_alnums)
380 break;
381 }
382 spin_lock_irqsave(&tty->read_lock, flags);
383 tty->read_head = head;
384 tty->read_cnt--;
385 spin_unlock_irqrestore(&tty->read_lock, flags);
386 if (L_ECHO(tty)) {
387 if (L_ECHOPRT(tty)) {
388 if (!tty->erasing) {
389 put_char('\\', tty);
390 tty->column++;
391 tty->erasing = 1;
392 }
393 echo_char(c, tty);
394 } else if (kill_type == ERASE && !L_ECHOE(tty)) {
395 echo_char(ERASE_CHAR(tty), tty);
396 } else if (c == '\t') {
397 unsigned int col = tty->canon_column;
398 unsigned long tail = tty->canon_head;
399
400 /* Find the column of the last char. */
401 while (tail != tty->read_head) {
402 c = tty->read_buf[tail];
403 if (c == '\t')
404 col = (col | 7) + 1;
405 else if (iscntrl(c)) {
406 if (L_ECHOCTL(tty))
407 col += 2;
408 } else
409 col++;
410 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
411 }
412
413 /* should never happen */
414 if (tty->column > 0x80000000)
415 tty->column = 0;
416
417 /* Now backup to that column. */
418 while (tty->column > col) {
419 /* Can't use opost here. */
420 put_char('\b', tty);
421 if (tty->column > 0)
422 tty->column--;
423 }
424 } else {
425 if (iscntrl(c) && L_ECHOCTL(tty)) {
426 put_char('\b', tty);
427 put_char(' ', tty);
428 put_char('\b', tty);
429 if (tty->column > 0)
430 tty->column--;
431 }
432 if (!iscntrl(c) || L_ECHOCTL(tty)) {
433 put_char('\b', tty);
434 put_char(' ', tty);
435 put_char('\b', tty);
436 if (tty->column > 0)
437 tty->column--;
438 }
439 }
440 }
441 if (kill_type == ERASE)
442 break;
443 }
444 if (tty->read_head == tty->canon_head)
445 finish_erasing(tty);
446 }
447
448 static inline void isig(int sig, struct tty_struct *tty, int flush)
449 {
450 if (tty->pgrp > 0)
451 kill_pg(tty->pgrp, sig, 1);
452 if (flush || !L_NOFLSH(tty)) {
453 n_tty_flush_buffer(tty);
454 if (tty->driver.flush_buffer)
455 tty->driver.flush_buffer(tty);
456 }
457 }
458
459 static inline void n_tty_receive_break(struct tty_struct *tty)
460 {
461 if (I_IGNBRK(tty))
462 return;
463 if (I_BRKINT(tty)) {
464 isig(SIGINT, tty, 1);
465 return;
466 }
467 if (I_PARMRK(tty)) {
468 put_tty_queue('\377', tty);
469 put_tty_queue('\0', tty);
470 }
471 put_tty_queue('\0', tty);
472 wake_up_interruptible(&tty->read_wait);
473 }
474
475 static inline void n_tty_receive_overrun(struct tty_struct *tty)
476 {
477 char buf[64];
478
479 tty->num_overrun++;
480 if (time_before(tty->overrun_time, jiffies - HZ)) {
481 printk("%s: %d input overrun(s)\n", tty_name(tty, buf),
482 tty->num_overrun);
483 tty->overrun_time = jiffies;
484 tty->num_overrun = 0;
485 }
486 }
487
488 static inline void n_tty_receive_parity_error(struct tty_struct *tty,
489 unsigned char c)
490 {
491 if (I_IGNPAR(tty)) {
492 return;
493 }
494 if (I_PARMRK(tty)) {
495 put_tty_queue('\377', tty);
496 put_tty_queue('\0', tty);
497 put_tty_queue(c, tty);
498 } else if (I_INPCK(tty))
499 put_tty_queue('\0', tty);
500 else
501 put_tty_queue(c, tty);
502 wake_up_interruptible(&tty->read_wait);
503 }
504
505 static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
506 {
507 unsigned long flags;
508
509 if (tty->raw) {
510 put_tty_queue(c, tty);
511 return;
512 }
513
514 if (tty->stopped && !tty->flow_stopped &&
515 I_IXON(tty) && I_IXANY(tty)) {
516 start_tty(tty);
517 return;
518 }
519
520 if (I_ISTRIP(tty))
521 c &= 0x7f;
522 if (I_IUCLC(tty) && L_IEXTEN(tty))
523 c=tolower(c);
524
525 if (tty->closing) {
526 if (I_IXON(tty)) {
527 if (c == START_CHAR(tty))
528 start_tty(tty);
529 else if (c == STOP_CHAR(tty))
530 stop_tty(tty);
531 }
532 return;
533 }
534
535 /*
536 * If the previous character was LNEXT, or we know that this
537 * character is not one of the characters that we'll have to
538 * handle specially, do shortcut processing to speed things
539 * up.
540 */
541 if (!test_bit(c, &tty->process_char_map) || tty->lnext) {
542 finish_erasing(tty);
543 tty->lnext = 0;
544 if (L_ECHO(tty)) {
545 if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
546 put_char('\a', tty); /* beep if no space */
547 return;
548 }
549 /* Record the column of first canon char. */
550 if (tty->canon_head == tty->read_head)
551 tty->canon_column = tty->column;
552 echo_char(c, tty);
553 }
554 if (I_PARMRK(tty) && c == (unsigned char) '\377')
555 put_tty_queue(c, tty);
556 put_tty_queue(c, tty);
557 return;
558 }
559
560 if (c == '\r') {
561 if (I_IGNCR(tty))
562 return;
563 if (I_ICRNL(tty))
564 c = '\n';
565 } else if (c == '\n' && I_INLCR(tty))
566 c = '\r';
567 if (I_IXON(tty)) {
568 if (c == START_CHAR(tty)) {
569 start_tty(tty);
570 return;
571 }
572 if (c == STOP_CHAR(tty)) {
573 stop_tty(tty);
574 return;
575 }
576 }
577 if (L_ISIG(tty)) {
578 int signal;
579 signal = SIGINT;
580 if (c == INTR_CHAR(tty))
581 goto send_signal;
582 signal = SIGQUIT;
583 if (c == QUIT_CHAR(tty))
584 goto send_signal;
585 signal = SIGTSTP;
586 if (c == SUSP_CHAR(tty)) {
587 send_signal:
588 isig(signal, tty, 0);
589 return;
590 }
591 }
592 if (tty->icanon) {
593 if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
594 (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
595 eraser(c, tty);
596 return;
597 }
598 if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
599 tty->lnext = 1;
600 if (L_ECHO(tty)) {
601 finish_erasing(tty);
602 if (L_ECHOCTL(tty)) {
603 put_char('^', tty);
604 put_char('\b', tty);
605 }
606 }
607 return;
608 }
609 if (c == REPRINT_CHAR(tty) && L_ECHO(tty) &&
610 L_IEXTEN(tty)) {
611 unsigned long tail = tty->canon_head;
612
613 finish_erasing(tty);
614 echo_char(c, tty);
615 opost('\n', tty);
616 while (tail != tty->read_head) {
617 echo_char(tty->read_buf[tail], tty);
618 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
619 }
620 return;
621 }
622 if (c == '\n') {
623 if (L_ECHO(tty) || L_ECHONL(tty)) {
624 if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
625 put_char('\a', tty);
626 return;
627 }
628 opost('\n', tty);
629 }
630 goto handle_newline;
631 }
632 if (c == EOF_CHAR(tty)) {
633 if (tty->canon_head != tty->read_head)
634 set_bit(TTY_PUSH, &tty->flags);
635 c = __DISABLED_CHAR;
636 goto handle_newline;
637 }
638 if ((c == EOL_CHAR(tty)) ||
639 (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
640 /*
641 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
642 */
643 if (L_ECHO(tty)) {
644 if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
645 put_char('\a', tty);
646 return;
647 }
648 /* Record the column of first canon char. */
649 if (tty->canon_head == tty->read_head)
650 tty->canon_column = tty->column;
651 echo_char(c, tty);
652 }
653 /*
654 * XXX does PARMRK doubling happen for
655 * EOL_CHAR and EOL2_CHAR?
656 */
657 if (I_PARMRK(tty) && c == (unsigned char) '\377')
658 put_tty_queue(c, tty);
659
660 handle_newline:
661 spin_lock_irqsave(&tty->read_lock, flags);
662 set_bit(tty->read_head, &tty->read_flags);
663 put_tty_queue_nolock(c, tty);
664 tty->canon_head = tty->read_head;
665 tty->canon_data++;
666 spin_unlock_irqrestore(&tty->read_lock, flags);
667 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
668 if (waitqueue_active(&tty->read_wait))
669 wake_up_interruptible(&tty->read_wait);
670 return;
671 }
672 }
673
674 finish_erasing(tty);
675 if (L_ECHO(tty)) {
676 if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
677 put_char('\a', tty); /* beep if no space */
678 return;
679 }
680 if (c == '\n')
681 opost('\n', tty);
682 else {
683 /* Record the column of first canon char. */
684 if (tty->canon_head == tty->read_head)
685 tty->canon_column = tty->column;
686 echo_char(c, tty);
687 }
688 }
689
690 if (I_PARMRK(tty) && c == (unsigned char) '\377')
691 put_tty_queue(c, tty);
692
693 put_tty_queue(c, tty);
694 }
695
696 static int n_tty_receive_room(struct tty_struct *tty)
697 {
698 int left = N_TTY_BUF_SIZE - tty->read_cnt - 1;
699
700 /*
701 * If we are doing input canonicalization, and there are no
702 * pending newlines, let characters through without limit, so
703 * that erase characters will be handled. Other excess
704 * characters will be beeped.
705 */
706 if (tty->icanon && !tty->canon_data)
707 return N_TTY_BUF_SIZE;
708
709 if (left > 0)
710 return left;
711 return 0;
712 }
713
714 static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
715 char *fp, int count)
716 {
717 const unsigned char *p;
718 char *f, flags = TTY_NORMAL;
719 int i;
720 char buf[64];
721 unsigned long cpuflags;
722
723 if (!tty->read_buf)
724 return;
725
726 if (tty->real_raw) {
727 spin_lock_irqsave(&tty->read_lock, cpuflags);
728 i = MIN(count, MIN(N_TTY_BUF_SIZE - tty->read_cnt,
729 N_TTY_BUF_SIZE - tty->read_head));
730 memcpy(tty->read_buf + tty->read_head, cp, i);
731 tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
732 tty->read_cnt += i;
733 cp += i;
734 count -= i;
735
736 i = MIN(count, MIN(N_TTY_BUF_SIZE - tty->read_cnt,
737 N_TTY_BUF_SIZE - tty->read_head));
738 memcpy(tty->read_buf + tty->read_head, cp, i);
739 tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
740 tty->read_cnt += i;
741 spin_unlock_irqrestore(&tty->read_lock, cpuflags);
742 } else {
743 for (i=count, p = cp, f = fp; i; i--, p++) {
744 if (f)
745 flags = *f++;
746 switch (flags) {
747 case TTY_NORMAL:
748 n_tty_receive_char(tty, *p);
749 break;
750 case TTY_BREAK:
751 n_tty_receive_break(tty);
752 break;
753 case TTY_PARITY:
754 case TTY_FRAME:
755 n_tty_receive_parity_error(tty, *p);
756 break;
757 case TTY_OVERRUN:
758 n_tty_receive_overrun(tty);
759 break;
760 default:
761 printk("%s: unknown flag %d\n",
762 tty_name(tty, buf), flags);
763 break;
764 }
765 }
766 if (tty->driver.flush_chars)
767 tty->driver.flush_chars(tty);
768 }
769
770 if (!tty->icanon && (tty->read_cnt >= tty->minimum_to_wake)) {
771 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
772 if (waitqueue_active(&tty->read_wait))
773 wake_up_interruptible(&tty->read_wait);
774 }
775
776 /*
777 * Check the remaining room for the input canonicalization
778 * mode. We don't want to throttle the driver if we're in
779 * canonical mode and don't have a newline yet!
780 */
781 if (n_tty_receive_room(tty) < TTY_THRESHOLD_THROTTLE) {
782 /* check TTY_THROTTLED first so it indicates our state */
783 if (!test_and_set_bit(TTY_THROTTLED, &tty->flags) &&
784 tty->driver.throttle)
785 tty->driver.throttle(tty);
786 }
787 }
788
789 int is_ignored(int sig)
790 {
791 return (sigismember(¤t->blocked, sig) ||
792 current->sig->action[sig-1].sa.sa_handler == SIG_IGN);
793 }
794
795 static void n_tty_set_termios(struct tty_struct *tty, struct termios * old)
796 {
797 if (!tty)
798 return;
799
800 tty->icanon = (L_ICANON(tty) != 0);
801 if (test_bit(TTY_HW_COOK_IN, &tty->flags)) {
802 tty->raw = 1;
803 tty->real_raw = 1;
804 return;
805 }
806 if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
807 I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
808 I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
809 I_PARMRK(tty)) {
810 cli();
811 memset(tty->process_char_map, 0, 256/8);
812
813 if (I_IGNCR(tty) || I_ICRNL(tty))
814 set_bit('\r', &tty->process_char_map);
815 if (I_INLCR(tty))
816 set_bit('\n', &tty->process_char_map);
817
818 if (L_ICANON(tty)) {
819 set_bit(ERASE_CHAR(tty), &tty->process_char_map);
820 set_bit(KILL_CHAR(tty), &tty->process_char_map);
821 set_bit(EOF_CHAR(tty), &tty->process_char_map);
822 set_bit('\n', &tty->process_char_map);
823 set_bit(EOL_CHAR(tty), &tty->process_char_map);
824 if (L_IEXTEN(tty)) {
825 set_bit(WERASE_CHAR(tty),
826 &tty->process_char_map);
827 set_bit(LNEXT_CHAR(tty),
828 &tty->process_char_map);
829 set_bit(EOL2_CHAR(tty),
830 &tty->process_char_map);
831 if (L_ECHO(tty))
832 set_bit(REPRINT_CHAR(tty),
833 &tty->process_char_map);
834 }
835 }
836 if (I_IXON(tty)) {
837 set_bit(START_CHAR(tty), &tty->process_char_map);
838 set_bit(STOP_CHAR(tty), &tty->process_char_map);
839 }
840 if (L_ISIG(tty)) {
841 set_bit(INTR_CHAR(tty), &tty->process_char_map);
842 set_bit(QUIT_CHAR(tty), &tty->process_char_map);
843 set_bit(SUSP_CHAR(tty), &tty->process_char_map);
844 }
845 clear_bit(__DISABLED_CHAR, &tty->process_char_map);
846 sti();
847 tty->raw = 0;
848 tty->real_raw = 0;
849 } else {
850 tty->raw = 1;
851 if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
852 (I_IGNPAR(tty) || !I_INPCK(tty)) &&
853 (tty->driver.flags & TTY_DRIVER_REAL_RAW))
854 tty->real_raw = 1;
855 else
856 tty->real_raw = 0;
857 }
858 }
859
860 static void n_tty_close(struct tty_struct *tty)
861 {
862 n_tty_flush_buffer(tty);
863 if (tty->read_buf) {
864 free_buf(tty->read_buf);
865 tty->read_buf = 0;
866 }
867 }
868
869 static int n_tty_open(struct tty_struct *tty)
870 {
871 if (!tty)
872 return -EINVAL;
873
874 if (!tty->read_buf) {
875 tty->read_buf = alloc_buf();
876 if (!tty->read_buf)
877 return -ENOMEM;
878 }
879 memset(tty->read_buf, 0, N_TTY_BUF_SIZE);
880 reset_buffer_flags(tty);
881 tty->column = 0;
882 n_tty_set_termios(tty, 0);
883 tty->minimum_to_wake = 1;
884 tty->closing = 0;
885 return 0;
886 }
887
888 static inline int input_available_p(struct tty_struct *tty, int amt)
889 {
890 if (tty->icanon) {
891 if (tty->canon_data)
892 return 1;
893 } else if (tty->read_cnt >= (amt ? amt : 1))
894 return 1;
895
896 return 0;
897 }
898
899 /*
900 * Helper function to speed up read_chan. It is only called when
901 * ICANON is off; it copies characters straight from the tty queue to
902 * user space directly. It can be profitably called twice; once to
903 * drain the space from the tail pointer to the (physical) end of the
904 * buffer, and once to drain the space from the (physical) beginning of
905 * the buffer to head pointer.
906 */
907 static inline int copy_from_read_buf(struct tty_struct *tty,
908 unsigned char **b,
909 size_t *nr)
910
911 {
912 int retval;
913 ssize_t n;
914 unsigned long flags;
915
916 retval = 0;
917 spin_lock_irqsave(&tty->read_lock, flags);
918 n = MIN(*nr, MIN(tty->read_cnt, N_TTY_BUF_SIZE - tty->read_tail));
919 spin_unlock_irqrestore(&tty->read_lock, flags);
920 if (n) {
921 mb();
922 retval = copy_to_user(*b, &tty->read_buf[tty->read_tail], n);
923 n -= retval;
924 spin_lock_irqsave(&tty->read_lock, flags);
925 tty->read_tail = (tty->read_tail + n) & (N_TTY_BUF_SIZE-1);
926 tty->read_cnt -= n;
927 spin_unlock_irqrestore(&tty->read_lock, flags);
928 *b += n;
929 *nr -= n;
930 }
931 return retval;
932 }
933
934 static ssize_t read_chan(struct tty_struct *tty, struct file *file,
935 unsigned char *buf, size_t nr)
936 {
937 unsigned char *b = buf;
938 DECLARE_WAITQUEUE(wait, current);
939 int c;
940 int minimum, time;
941 ssize_t retval = 0;
942 ssize_t size;
943 long timeout;
944 unsigned long flags;
945
946 do_it_again:
947
948 if (!tty->read_buf) {
949 printk("n_tty_read_chan: called with read_buf == NULL?!?\n");
950 return -EIO;
951 }
952
953 /* Job control check -- must be done at start and after
954 every sleep (POSIX.1 7.1.1.4). */
955 /* NOTE: not yet done after every sleep pending a thorough
956 check of the logic of this change. -- jlc */
957 /* don't stop on /dev/console */
958 if (file->f_dentry->d_inode->i_rdev != CONSOLE_DEV &&
959 file->f_dentry->d_inode->i_rdev != SYSCONS_DEV &&
960 current->tty == tty) {
961 if (tty->pgrp <= 0)
962 printk("read_chan: tty->pgrp <= 0!\n");
963 else if (current->pgrp != tty->pgrp) {
964 if (is_ignored(SIGTTIN) ||
965 is_orphaned_pgrp(current->pgrp))
966 return -EIO;
967 kill_pg(current->pgrp, SIGTTIN, 1);
968 return -ERESTARTSYS;
969 }
970 }
971
972 minimum = time = 0;
973 timeout = MAX_SCHEDULE_TIMEOUT;
974 if (!tty->icanon) {
975 time = (HZ / 10) * TIME_CHAR(tty);
976 minimum = MIN_CHAR(tty);
977 if (minimum) {
978 if (time)
979 tty->minimum_to_wake = 1;
980 else if (!waitqueue_active(&tty->read_wait) ||
981 (tty->minimum_to_wake > minimum))
982 tty->minimum_to_wake = minimum;
983 } else {
984 timeout = 0;
985 if (time) {
986 timeout = time;
987 time = 0;
988 }
989 tty->minimum_to_wake = minimum = 1;
990 }
991 }
992
993 if (file->f_flags & O_NONBLOCK) {
994 if (down_trylock(&tty->atomic_read))
995 return -EAGAIN;
996 }
997 else {
998 if (down_interruptible(&tty->atomic_read))
999 return -ERESTARTSYS;
1000 }
1001
1002 add_wait_queue(&tty->read_wait, &wait);
1003 set_bit(TTY_DONT_FLIP, &tty->flags);
1004 while (nr) {
1005 /* First test for status change. */
1006 if (tty->packet && tty->link->ctrl_status) {
1007 unsigned char cs;
1008 if (b != buf)
1009 break;
1010 cs = tty->link->ctrl_status;
1011 tty->link->ctrl_status = 0;
1012 put_user(cs, b++);
1013 nr--;
1014 break;
1015 }
1016 /* This statement must be first before checking for input
1017 so that any interrupt will set the state back to
1018 TASK_RUNNING. */
1019 set_current_state(TASK_INTERRUPTIBLE);
1020
1021 if (((minimum - (b - buf)) < tty->minimum_to_wake) &&
1022 ((minimum - (b - buf)) >= 1))
1023 tty->minimum_to_wake = (minimum - (b - buf));
1024
1025 if (!input_available_p(tty, 0)) {
1026 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
1027 retval = -EIO;
1028 break;
1029 }
1030 if (tty_hung_up_p(file))
1031 break;
1032 if (!timeout)
1033 break;
1034 if (file->f_flags & O_NONBLOCK) {
1035 retval = -EAGAIN;
1036 break;
1037 }
1038 if (signal_pending(current)) {
1039 retval = -ERESTARTSYS;
1040 break;
1041 }
1042 clear_bit(TTY_DONT_FLIP, &tty->flags);
1043 timeout = schedule_timeout(timeout);
1044 set_bit(TTY_DONT_FLIP, &tty->flags);
1045 continue;
1046 }
1047 current->state = TASK_RUNNING;
1048
1049 /* Deal with packet mode. */
1050 if (tty->packet && b == buf) {
1051 put_user(TIOCPKT_DATA, b++);
1052 nr--;
1053 }
1054
1055 if (tty->icanon) {
1056 /* N.B. avoid overrun if nr == 0 */
1057 while (nr && tty->read_cnt) {
1058 int eol;
1059
1060 eol = test_and_clear_bit(tty->read_tail,
1061 &tty->read_flags);
1062 c = tty->read_buf[tty->read_tail];
1063 spin_lock_irqsave(&tty->read_lock, flags);
1064 tty->read_tail = ((tty->read_tail+1) &
1065 (N_TTY_BUF_SIZE-1));
1066 tty->read_cnt--;
1067 if (eol) {
1068 /* this test should be redundant:
1069 * we shouldn't be reading data if
1070 * canon_data is 0
1071 */
1072 if (--tty->canon_data < 0)
1073 tty->canon_data = 0;
1074 }
1075 spin_unlock_irqrestore(&tty->read_lock, flags);
1076
1077 if (!eol || (c != __DISABLED_CHAR)) {
1078 put_user(c, b++);
1079 nr--;
1080 }
1081 if (eol)
1082 break;
1083 }
1084 } else {
1085 int uncopied;
1086 uncopied = copy_from_read_buf(tty, &b, &nr);
1087 uncopied += copy_from_read_buf(tty, &b, &nr);
1088 if (uncopied) {
1089 retval = -EFAULT;
1090 break;
1091 }
1092 }
1093
1094 /* If there is enough space in the read buffer now, let the
1095 * low-level driver know. We use n_tty_chars_in_buffer() to
1096 * check the buffer, as it now knows about canonical mode.
1097 * Otherwise, if the driver is throttled and the line is
1098 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
1099 * we won't get any more characters.
1100 */
1101 if (n_tty_chars_in_buffer(tty) <= TTY_THRESHOLD_UNTHROTTLE)
1102 check_unthrottle(tty);
1103
1104 if (b - buf >= minimum)
1105 break;
1106 if (time)
1107 timeout = time;
1108 }
1109 clear_bit(TTY_DONT_FLIP, &tty->flags);
1110 up(&tty->atomic_read);
1111 remove_wait_queue(&tty->read_wait, &wait);
1112
1113 if (!waitqueue_active(&tty->read_wait))
1114 tty->minimum_to_wake = minimum;
1115
1116 current->state = TASK_RUNNING;
1117 size = b - buf;
1118 if (size) {
1119 retval = size;
1120 if (nr)
1121 clear_bit(TTY_PUSH, &tty->flags);
1122 } else if (test_and_clear_bit(TTY_PUSH, &tty->flags))
1123 goto do_it_again;
1124
1125 return retval;
1126 }
1127
1128 static ssize_t write_chan(struct tty_struct * tty, struct file * file,
1129 const unsigned char * buf, size_t nr)
1130 {
1131 const unsigned char *b = buf;
1132 DECLARE_WAITQUEUE(wait, current);
1133 int c;
1134 ssize_t retval = 0;
1135
1136 /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
1137 if (L_TOSTOP(tty) &&
1138 file->f_dentry->d_inode->i_rdev != CONSOLE_DEV &&
1139 file->f_dentry->d_inode->i_rdev != SYSCONS_DEV) {
1140 retval = tty_check_change(tty);
1141 if (retval)
1142 return retval;
1143 }
1144
1145 add_wait_queue(&tty->write_wait, &wait);
1146 while (1) {
1147 set_current_state(TASK_INTERRUPTIBLE);
1148 if (signal_pending(current)) {
1149 retval = -ERESTARTSYS;
1150 break;
1151 }
1152 if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
1153 retval = -EIO;
1154 break;
1155 }
1156 if (O_OPOST(tty) && !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
1157 while (nr > 0) {
1158 ssize_t num = opost_block(tty, b, nr);
1159 if (num < 0) {
1160 retval = num;
1161 goto break_out;
1162 }
1163 b += num;
1164 nr -= num;
1165 if (nr == 0)
1166 break;
1167 get_user(c, b);
1168 if (opost(c, tty) < 0)
1169 break;
1170 b++; nr--;
1171 }
1172 if (tty->driver.flush_chars)
1173 tty->driver.flush_chars(tty);
1174 } else {
1175 c = tty->driver.write(tty, 1, b, nr);
1176 if (c < 0) {
1177 retval = c;
1178 goto break_out;
1179 }
1180 b += c;
1181 nr -= c;
1182 }
1183 if (!nr)
1184 break;
1185 if (file->f_flags & O_NONBLOCK) {
1186 retval = -EAGAIN;
1187 break;
1188 }
1189 schedule();
1190 }
1191 break_out:
1192 current->state = TASK_RUNNING;
1193 remove_wait_queue(&tty->write_wait, &wait);
1194 return (b - buf) ? b - buf : retval;
1195 }
1196
1197 /* Called without the kernel lock held - fine */
1198 static unsigned int normal_poll(struct tty_struct * tty, struct file * file, poll_table *wait)
1199 {
1200 unsigned int mask = 0;
1201
1202 poll_wait(file, &tty->read_wait, wait);
1203 poll_wait(file, &tty->write_wait, wait);
1204 if (input_available_p(tty, TIME_CHAR(tty) ? 0 : MIN_CHAR(tty)))
1205 mask |= POLLIN | POLLRDNORM;
1206 if (tty->packet && tty->link->ctrl_status)
1207 mask |= POLLPRI | POLLIN | POLLRDNORM;
1208 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
1209 mask |= POLLHUP;
1210 if (tty_hung_up_p(file))
1211 mask |= POLLHUP;
1212 if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) {
1213 if (MIN_CHAR(tty) && !TIME_CHAR(tty))
1214 tty->minimum_to_wake = MIN_CHAR(tty);
1215 else
1216 tty->minimum_to_wake = 1;
1217 }
1218 if (tty->driver.chars_in_buffer(tty) < WAKEUP_CHARS)
1219 mask |= POLLOUT | POLLWRNORM;
1220 return mask;
1221 }
1222
1223 struct tty_ldisc tty_ldisc_N_TTY = {
1224 TTY_LDISC_MAGIC, /* magic */
1225 "n_tty", /* name */
1226 0, /* num */
1227 0, /* flags */
1228 n_tty_open, /* open */
1229 n_tty_close, /* close */
1230 n_tty_flush_buffer, /* flush_buffer */
1231 n_tty_chars_in_buffer, /* chars_in_buffer */
1232 read_chan, /* read */
1233 write_chan, /* write */
1234 n_tty_ioctl, /* ioctl */
1235 n_tty_set_termios, /* set_termios */
1236 normal_poll, /* poll */
1237 n_tty_receive_buf, /* receive_buf */
1238 n_tty_receive_room, /* receive_room */
1239 0 /* write_wakeup */
1240 };
1241
1242