dm thin metadata: fix __udivdi3 undefined on 32-bit
[linux/fpc-iii.git] / drivers / tty / n_tty.c
blobb1ec202099b2e6c5c52efe3f93140d5d2a3df2cd
1 /*
2 * n_tty.c --- implements the N_TTY line discipline.
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 n_tty_write 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/mm.h>
44 #include <linux/string.h>
45 #include <linux/slab.h>
46 #include <linux/poll.h>
47 #include <linux/bitops.h>
48 #include <linux/audit.h>
49 #include <linux/file.h>
50 #include <linux/uaccess.h>
51 #include <linux/module.h>
52 #include <linux/ratelimit.h>
53 #include <linux/vmalloc.h>
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
68 * Special byte codes used in the echo buffer to represent operations
69 * or special handling of characters. Bytes in the echo buffer that
70 * are not part of such special blocks are treated as normal character
71 * codes.
73 #define ECHO_OP_START 0xff
74 #define ECHO_OP_MOVE_BACK_COL 0x80
75 #define ECHO_OP_SET_CANON_COL 0x81
76 #define ECHO_OP_ERASE_TAB 0x82
78 #define ECHO_COMMIT_WATERMARK 256
79 #define ECHO_BLOCK 256
80 #define ECHO_DISCARD_WATERMARK N_TTY_BUF_SIZE - (ECHO_BLOCK + 32)
83 #undef N_TTY_TRACE
84 #ifdef N_TTY_TRACE
85 # define n_tty_trace(f, args...) trace_printk(f, ##args)
86 #else
87 # define n_tty_trace(f, args...)
88 #endif
90 struct n_tty_data {
91 /* producer-published */
92 size_t read_head;
93 size_t commit_head;
94 size_t canon_head;
95 size_t echo_head;
96 size_t echo_commit;
97 size_t echo_mark;
98 DECLARE_BITMAP(char_map, 256);
100 /* private to n_tty_receive_overrun (single-threaded) */
101 unsigned long overrun_time;
102 int num_overrun;
104 /* non-atomic */
105 bool no_room;
107 /* must hold exclusive termios_rwsem to reset these */
108 unsigned char lnext:1, erasing:1, raw:1, real_raw:1, icanon:1;
109 unsigned char push:1;
111 /* shared by producer and consumer */
112 char read_buf[N_TTY_BUF_SIZE];
113 DECLARE_BITMAP(read_flags, N_TTY_BUF_SIZE);
114 unsigned char echo_buf[N_TTY_BUF_SIZE];
116 int minimum_to_wake;
118 /* consumer-published */
119 size_t read_tail;
120 size_t line_start;
122 /* protected by output lock */
123 unsigned int column;
124 unsigned int canon_column;
125 size_t echo_tail;
127 struct mutex atomic_read_lock;
128 struct mutex output_lock;
131 #define MASK(x) ((x) & (N_TTY_BUF_SIZE - 1))
133 static inline size_t read_cnt(struct n_tty_data *ldata)
135 return ldata->read_head - ldata->read_tail;
138 static inline unsigned char read_buf(struct n_tty_data *ldata, size_t i)
140 return ldata->read_buf[i & (N_TTY_BUF_SIZE - 1)];
143 static inline unsigned char *read_buf_addr(struct n_tty_data *ldata, size_t i)
145 return &ldata->read_buf[i & (N_TTY_BUF_SIZE - 1)];
148 static inline unsigned char echo_buf(struct n_tty_data *ldata, size_t i)
150 smp_rmb(); /* Matches smp_wmb() in add_echo_byte(). */
151 return ldata->echo_buf[i & (N_TTY_BUF_SIZE - 1)];
154 static inline unsigned char *echo_buf_addr(struct n_tty_data *ldata, size_t i)
156 return &ldata->echo_buf[i & (N_TTY_BUF_SIZE - 1)];
159 static inline int tty_put_user(struct tty_struct *tty, unsigned char x,
160 unsigned char __user *ptr)
162 struct n_tty_data *ldata = tty->disc_data;
164 tty_audit_add_data(tty, &x, 1, ldata->icanon);
165 return put_user(x, ptr);
168 static inline int tty_copy_to_user(struct tty_struct *tty,
169 void __user *to,
170 const void *from,
171 unsigned long n)
173 struct n_tty_data *ldata = tty->disc_data;
175 tty_audit_add_data(tty, from, n, ldata->icanon);
176 return copy_to_user(to, from, n);
180 * n_tty_kick_worker - start input worker (if required)
181 * @tty: terminal
183 * Re-schedules the flip buffer work if it may have stopped
185 * Caller holds exclusive termios_rwsem
186 * or
187 * n_tty_read()/consumer path:
188 * holds non-exclusive termios_rwsem
191 static void n_tty_kick_worker(struct tty_struct *tty)
193 struct n_tty_data *ldata = tty->disc_data;
195 /* Did the input worker stop? Restart it */
196 if (unlikely(ldata->no_room)) {
197 ldata->no_room = 0;
199 WARN_RATELIMIT(tty->port->itty == NULL,
200 "scheduling with invalid itty\n");
201 /* see if ldisc has been killed - if so, this means that
202 * even though the ldisc has been halted and ->buf.work
203 * cancelled, ->buf.work is about to be rescheduled
205 WARN_RATELIMIT(test_bit(TTY_LDISC_HALTED, &tty->flags),
206 "scheduling buffer work for halted ldisc\n");
207 tty_buffer_restart_work(tty->port);
211 static ssize_t chars_in_buffer(struct tty_struct *tty)
213 struct n_tty_data *ldata = tty->disc_data;
214 ssize_t n = 0;
216 if (!ldata->icanon)
217 n = ldata->commit_head - ldata->read_tail;
218 else
219 n = ldata->canon_head - ldata->read_tail;
220 return n;
224 * n_tty_write_wakeup - asynchronous I/O notifier
225 * @tty: tty device
227 * Required for the ptys, serial driver etc. since processes
228 * that attach themselves to the master and rely on ASYNC
229 * IO must be woken up
232 static void n_tty_write_wakeup(struct tty_struct *tty)
234 if (tty->fasync && test_and_clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags))
235 kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
238 static void n_tty_check_throttle(struct tty_struct *tty)
240 struct n_tty_data *ldata = tty->disc_data;
243 * Check the remaining room for the input canonicalization
244 * mode. We don't want to throttle the driver if we're in
245 * canonical mode and don't have a newline yet!
247 if (ldata->icanon && ldata->canon_head == ldata->read_tail)
248 return;
250 while (1) {
251 int throttled;
252 tty_set_flow_change(tty, TTY_THROTTLE_SAFE);
253 if (N_TTY_BUF_SIZE - read_cnt(ldata) >= TTY_THRESHOLD_THROTTLE)
254 break;
255 throttled = tty_throttle_safe(tty);
256 if (!throttled)
257 break;
259 __tty_set_flow_change(tty, 0);
262 static void n_tty_check_unthrottle(struct tty_struct *tty)
264 if (tty->driver->type == TTY_DRIVER_TYPE_PTY) {
265 if (chars_in_buffer(tty) > TTY_THRESHOLD_UNTHROTTLE)
266 return;
267 if (!tty->count)
268 return;
269 n_tty_kick_worker(tty);
270 tty_wakeup(tty->link);
271 return;
274 /* If there is enough space in the read buffer now, let the
275 * low-level driver know. We use chars_in_buffer() to
276 * check the buffer, as it now knows about canonical mode.
277 * Otherwise, if the driver is throttled and the line is
278 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
279 * we won't get any more characters.
282 while (1) {
283 int unthrottled;
284 tty_set_flow_change(tty, TTY_UNTHROTTLE_SAFE);
285 if (chars_in_buffer(tty) > TTY_THRESHOLD_UNTHROTTLE)
286 break;
287 if (!tty->count)
288 break;
289 n_tty_kick_worker(tty);
290 unthrottled = tty_unthrottle_safe(tty);
291 if (!unthrottled)
292 break;
294 __tty_set_flow_change(tty, 0);
298 * put_tty_queue - add character to tty
299 * @c: character
300 * @ldata: n_tty data
302 * Add a character to the tty read_buf queue.
304 * n_tty_receive_buf()/producer path:
305 * caller holds non-exclusive termios_rwsem
308 static inline void put_tty_queue(unsigned char c, struct n_tty_data *ldata)
310 *read_buf_addr(ldata, ldata->read_head) = c;
311 ldata->read_head++;
315 * reset_buffer_flags - reset buffer state
316 * @tty: terminal to reset
318 * Reset the read buffer counters and clear the flags.
319 * Called from n_tty_open() and n_tty_flush_buffer().
321 * Locking: caller holds exclusive termios_rwsem
322 * (or locking is not required)
325 static void reset_buffer_flags(struct n_tty_data *ldata)
327 ldata->read_head = ldata->canon_head = ldata->read_tail = 0;
328 ldata->commit_head = 0;
329 ldata->line_start = 0;
331 ldata->erasing = 0;
332 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
333 ldata->push = 0;
336 static void n_tty_packet_mode_flush(struct tty_struct *tty)
338 unsigned long flags;
340 if (tty->link->packet) {
341 spin_lock_irqsave(&tty->ctrl_lock, flags);
342 tty->ctrl_status |= TIOCPKT_FLUSHREAD;
343 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
344 wake_up_interruptible(&tty->link->read_wait);
349 * n_tty_flush_buffer - clean input queue
350 * @tty: terminal device
352 * Flush the input buffer. Called when the tty layer wants the
353 * buffer flushed (eg at hangup) or when the N_TTY line discipline
354 * internally has to clean the pending queue (for example some signals).
356 * Holds termios_rwsem to exclude producer/consumer while
357 * buffer indices are reset.
359 * Locking: ctrl_lock, exclusive termios_rwsem
362 static void n_tty_flush_buffer(struct tty_struct *tty)
364 down_write(&tty->termios_rwsem);
365 reset_buffer_flags(tty->disc_data);
366 n_tty_kick_worker(tty);
368 if (tty->link)
369 n_tty_packet_mode_flush(tty);
370 up_write(&tty->termios_rwsem);
374 * n_tty_chars_in_buffer - report available bytes
375 * @tty: tty device
377 * Report the number of characters buffered to be delivered to user
378 * at this instant in time.
380 * Locking: exclusive termios_rwsem
383 static ssize_t n_tty_chars_in_buffer(struct tty_struct *tty)
385 ssize_t n;
387 WARN_ONCE(1, "%s is deprecated and scheduled for removal.", __func__);
389 down_write(&tty->termios_rwsem);
390 n = chars_in_buffer(tty);
391 up_write(&tty->termios_rwsem);
392 return n;
396 * is_utf8_continuation - utf8 multibyte check
397 * @c: byte to check
399 * Returns true if the utf8 character 'c' is a multibyte continuation
400 * character. We use this to correctly compute the on screen size
401 * of the character when printing
404 static inline int is_utf8_continuation(unsigned char c)
406 return (c & 0xc0) == 0x80;
410 * is_continuation - multibyte check
411 * @c: byte to check
413 * Returns true if the utf8 character 'c' is a multibyte continuation
414 * character and the terminal is in unicode mode.
417 static inline int is_continuation(unsigned char c, struct tty_struct *tty)
419 return I_IUTF8(tty) && is_utf8_continuation(c);
423 * do_output_char - output one character
424 * @c: character (or partial unicode symbol)
425 * @tty: terminal device
426 * @space: space available in tty driver write buffer
428 * This is a helper function that handles one output character
429 * (including special characters like TAB, CR, LF, etc.),
430 * doing OPOST processing and putting the results in the
431 * tty driver's write buffer.
433 * Note that Linux currently ignores TABDLY, CRDLY, VTDLY, FFDLY
434 * and NLDLY. They simply aren't relevant in the world today.
435 * If you ever need them, add them here.
437 * Returns the number of bytes of buffer space used or -1 if
438 * no space left.
440 * Locking: should be called under the output_lock to protect
441 * the column state and space left in the buffer
444 static int do_output_char(unsigned char c, struct tty_struct *tty, int space)
446 struct n_tty_data *ldata = tty->disc_data;
447 int spaces;
449 if (!space)
450 return -1;
452 switch (c) {
453 case '\n':
454 if (O_ONLRET(tty))
455 ldata->column = 0;
456 if (O_ONLCR(tty)) {
457 if (space < 2)
458 return -1;
459 ldata->canon_column = ldata->column = 0;
460 tty->ops->write(tty, "\r\n", 2);
461 return 2;
463 ldata->canon_column = ldata->column;
464 break;
465 case '\r':
466 if (O_ONOCR(tty) && ldata->column == 0)
467 return 0;
468 if (O_OCRNL(tty)) {
469 c = '\n';
470 if (O_ONLRET(tty))
471 ldata->canon_column = ldata->column = 0;
472 break;
474 ldata->canon_column = ldata->column = 0;
475 break;
476 case '\t':
477 spaces = 8 - (ldata->column & 7);
478 if (O_TABDLY(tty) == XTABS) {
479 if (space < spaces)
480 return -1;
481 ldata->column += spaces;
482 tty->ops->write(tty, " ", spaces);
483 return spaces;
485 ldata->column += spaces;
486 break;
487 case '\b':
488 if (ldata->column > 0)
489 ldata->column--;
490 break;
491 default:
492 if (!iscntrl(c)) {
493 if (O_OLCUC(tty))
494 c = toupper(c);
495 if (!is_continuation(c, tty))
496 ldata->column++;
498 break;
501 tty_put_char(tty, c);
502 return 1;
506 * process_output - output post processor
507 * @c: character (or partial unicode symbol)
508 * @tty: terminal device
510 * Output one character with OPOST processing.
511 * Returns -1 when the output device is full and the character
512 * must be retried.
514 * Locking: output_lock to protect column state and space left
515 * (also, this is called from n_tty_write under the
516 * tty layer write lock)
519 static int process_output(unsigned char c, struct tty_struct *tty)
521 struct n_tty_data *ldata = tty->disc_data;
522 int space, retval;
524 mutex_lock(&ldata->output_lock);
526 space = tty_write_room(tty);
527 retval = do_output_char(c, tty, space);
529 mutex_unlock(&ldata->output_lock);
530 if (retval < 0)
531 return -1;
532 else
533 return 0;
537 * process_output_block - block post processor
538 * @tty: terminal device
539 * @buf: character buffer
540 * @nr: number of bytes to output
542 * Output a block of characters with OPOST processing.
543 * Returns the number of characters output.
545 * This path is used to speed up block console writes, among other
546 * things when processing blocks of output data. It handles only
547 * the simple cases normally found and helps to generate blocks of
548 * symbols for the console driver and thus improve performance.
550 * Locking: output_lock to protect column state and space left
551 * (also, this is called from n_tty_write under the
552 * tty layer write lock)
555 static ssize_t process_output_block(struct tty_struct *tty,
556 const unsigned char *buf, unsigned int nr)
558 struct n_tty_data *ldata = tty->disc_data;
559 int space;
560 int i;
561 const unsigned char *cp;
563 mutex_lock(&ldata->output_lock);
565 space = tty_write_room(tty);
566 if (!space) {
567 mutex_unlock(&ldata->output_lock);
568 return 0;
570 if (nr > space)
571 nr = space;
573 for (i = 0, cp = buf; i < nr; i++, cp++) {
574 unsigned char c = *cp;
576 switch (c) {
577 case '\n':
578 if (O_ONLRET(tty))
579 ldata->column = 0;
580 if (O_ONLCR(tty))
581 goto break_out;
582 ldata->canon_column = ldata->column;
583 break;
584 case '\r':
585 if (O_ONOCR(tty) && ldata->column == 0)
586 goto break_out;
587 if (O_OCRNL(tty))
588 goto break_out;
589 ldata->canon_column = ldata->column = 0;
590 break;
591 case '\t':
592 goto break_out;
593 case '\b':
594 if (ldata->column > 0)
595 ldata->column--;
596 break;
597 default:
598 if (!iscntrl(c)) {
599 if (O_OLCUC(tty))
600 goto break_out;
601 if (!is_continuation(c, tty))
602 ldata->column++;
604 break;
607 break_out:
608 i = tty->ops->write(tty, buf, i);
610 mutex_unlock(&ldata->output_lock);
611 return i;
615 * process_echoes - write pending echo characters
616 * @tty: terminal device
618 * Write previously buffered echo (and other ldisc-generated)
619 * characters to the tty.
621 * Characters generated by the ldisc (including echoes) need to
622 * be buffered because the driver's write buffer can fill during
623 * heavy program output. Echoing straight to the driver will
624 * often fail under these conditions, causing lost characters and
625 * resulting mismatches of ldisc state information.
627 * Since the ldisc state must represent the characters actually sent
628 * to the driver at the time of the write, operations like certain
629 * changes in column state are also saved in the buffer and executed
630 * here.
632 * A circular fifo buffer is used so that the most recent characters
633 * are prioritized. Also, when control characters are echoed with a
634 * prefixed "^", the pair is treated atomically and thus not separated.
636 * Locking: callers must hold output_lock
639 static size_t __process_echoes(struct tty_struct *tty)
641 struct n_tty_data *ldata = tty->disc_data;
642 int space, old_space;
643 size_t tail;
644 unsigned char c;
646 old_space = space = tty_write_room(tty);
648 tail = ldata->echo_tail;
649 while (MASK(ldata->echo_commit) != MASK(tail)) {
650 c = echo_buf(ldata, tail);
651 if (c == ECHO_OP_START) {
652 unsigned char op;
653 int no_space_left = 0;
656 * Since add_echo_byte() is called without holding
657 * output_lock, we might see only portion of multi-byte
658 * operation.
660 if (MASK(ldata->echo_commit) == MASK(tail + 1))
661 goto not_yet_stored;
663 * If the buffer byte is the start of a multi-byte
664 * operation, get the next byte, which is either the
665 * op code or a control character value.
667 op = echo_buf(ldata, tail + 1);
669 switch (op) {
670 unsigned int num_chars, num_bs;
672 case ECHO_OP_ERASE_TAB:
673 if (MASK(ldata->echo_commit) == MASK(tail + 2))
674 goto not_yet_stored;
675 num_chars = echo_buf(ldata, tail + 2);
678 * Determine how many columns to go back
679 * in order to erase the tab.
680 * This depends on the number of columns
681 * used by other characters within the tab
682 * area. If this (modulo 8) count is from
683 * the start of input rather than from a
684 * previous tab, we offset by canon column.
685 * Otherwise, tab spacing is normal.
687 if (!(num_chars & 0x80))
688 num_chars += ldata->canon_column;
689 num_bs = 8 - (num_chars & 7);
691 if (num_bs > space) {
692 no_space_left = 1;
693 break;
695 space -= num_bs;
696 while (num_bs--) {
697 tty_put_char(tty, '\b');
698 if (ldata->column > 0)
699 ldata->column--;
701 tail += 3;
702 break;
704 case ECHO_OP_SET_CANON_COL:
705 ldata->canon_column = ldata->column;
706 tail += 2;
707 break;
709 case ECHO_OP_MOVE_BACK_COL:
710 if (ldata->column > 0)
711 ldata->column--;
712 tail += 2;
713 break;
715 case ECHO_OP_START:
716 /* This is an escaped echo op start code */
717 if (!space) {
718 no_space_left = 1;
719 break;
721 tty_put_char(tty, ECHO_OP_START);
722 ldata->column++;
723 space--;
724 tail += 2;
725 break;
727 default:
729 * If the op is not a special byte code,
730 * it is a ctrl char tagged to be echoed
731 * as "^X" (where X is the letter
732 * representing the control char).
733 * Note that we must ensure there is
734 * enough space for the whole ctrl pair.
737 if (space < 2) {
738 no_space_left = 1;
739 break;
741 tty_put_char(tty, '^');
742 tty_put_char(tty, op ^ 0100);
743 ldata->column += 2;
744 space -= 2;
745 tail += 2;
748 if (no_space_left)
749 break;
750 } else {
751 if (O_OPOST(tty)) {
752 int retval = do_output_char(c, tty, space);
753 if (retval < 0)
754 break;
755 space -= retval;
756 } else {
757 if (!space)
758 break;
759 tty_put_char(tty, c);
760 space -= 1;
762 tail += 1;
766 /* If the echo buffer is nearly full (so that the possibility exists
767 * of echo overrun before the next commit), then discard enough
768 * data at the tail to prevent a subsequent overrun */
769 while (ldata->echo_commit > tail &&
770 ldata->echo_commit - tail >= ECHO_DISCARD_WATERMARK) {
771 if (echo_buf(ldata, tail) == ECHO_OP_START) {
772 if (echo_buf(ldata, tail + 1) == ECHO_OP_ERASE_TAB)
773 tail += 3;
774 else
775 tail += 2;
776 } else
777 tail++;
780 not_yet_stored:
781 ldata->echo_tail = tail;
782 return old_space - space;
785 static void commit_echoes(struct tty_struct *tty)
787 struct n_tty_data *ldata = tty->disc_data;
788 size_t nr, old, echoed;
789 size_t head;
791 mutex_lock(&ldata->output_lock);
792 head = ldata->echo_head;
793 ldata->echo_mark = head;
794 old = ldata->echo_commit - ldata->echo_tail;
796 /* Process committed echoes if the accumulated # of bytes
797 * is over the threshold (and try again each time another
798 * block is accumulated) */
799 nr = head - ldata->echo_tail;
800 if (nr < ECHO_COMMIT_WATERMARK ||
801 (nr % ECHO_BLOCK > old % ECHO_BLOCK)) {
802 mutex_unlock(&ldata->output_lock);
803 return;
806 ldata->echo_commit = head;
807 echoed = __process_echoes(tty);
808 mutex_unlock(&ldata->output_lock);
810 if (echoed && tty->ops->flush_chars)
811 tty->ops->flush_chars(tty);
814 static void process_echoes(struct tty_struct *tty)
816 struct n_tty_data *ldata = tty->disc_data;
817 size_t echoed;
819 if (ldata->echo_mark == ldata->echo_tail)
820 return;
822 mutex_lock(&ldata->output_lock);
823 ldata->echo_commit = ldata->echo_mark;
824 echoed = __process_echoes(tty);
825 mutex_unlock(&ldata->output_lock);
827 if (echoed && tty->ops->flush_chars)
828 tty->ops->flush_chars(tty);
831 /* NB: echo_mark and echo_head should be equivalent here */
832 static void flush_echoes(struct tty_struct *tty)
834 struct n_tty_data *ldata = tty->disc_data;
836 if ((!L_ECHO(tty) && !L_ECHONL(tty)) ||
837 ldata->echo_commit == ldata->echo_head)
838 return;
840 mutex_lock(&ldata->output_lock);
841 ldata->echo_commit = ldata->echo_head;
842 __process_echoes(tty);
843 mutex_unlock(&ldata->output_lock);
847 * add_echo_byte - add a byte to the echo buffer
848 * @c: unicode byte to echo
849 * @ldata: n_tty data
851 * Add a character or operation byte to the echo buffer.
854 static inline void add_echo_byte(unsigned char c, struct n_tty_data *ldata)
856 *echo_buf_addr(ldata, ldata->echo_head) = c;
857 smp_wmb(); /* Matches smp_rmb() in echo_buf(). */
858 ldata->echo_head++;
862 * echo_move_back_col - add operation to move back a column
863 * @ldata: n_tty data
865 * Add an operation to the echo buffer to move back one column.
868 static void echo_move_back_col(struct n_tty_data *ldata)
870 add_echo_byte(ECHO_OP_START, ldata);
871 add_echo_byte(ECHO_OP_MOVE_BACK_COL, ldata);
875 * echo_set_canon_col - add operation to set the canon column
876 * @ldata: n_tty data
878 * Add an operation to the echo buffer to set the canon column
879 * to the current column.
882 static void echo_set_canon_col(struct n_tty_data *ldata)
884 add_echo_byte(ECHO_OP_START, ldata);
885 add_echo_byte(ECHO_OP_SET_CANON_COL, ldata);
889 * echo_erase_tab - add operation to erase a tab
890 * @num_chars: number of character columns already used
891 * @after_tab: true if num_chars starts after a previous tab
892 * @ldata: n_tty data
894 * Add an operation to the echo buffer to erase a tab.
896 * Called by the eraser function, which knows how many character
897 * columns have been used since either a previous tab or the start
898 * of input. This information will be used later, along with
899 * canon column (if applicable), to go back the correct number
900 * of columns.
903 static void echo_erase_tab(unsigned int num_chars, int after_tab,
904 struct n_tty_data *ldata)
906 add_echo_byte(ECHO_OP_START, ldata);
907 add_echo_byte(ECHO_OP_ERASE_TAB, ldata);
909 /* We only need to know this modulo 8 (tab spacing) */
910 num_chars &= 7;
912 /* Set the high bit as a flag if num_chars is after a previous tab */
913 if (after_tab)
914 num_chars |= 0x80;
916 add_echo_byte(num_chars, ldata);
920 * echo_char_raw - echo a character raw
921 * @c: unicode byte to echo
922 * @tty: terminal device
924 * Echo user input back onto the screen. This must be called only when
925 * L_ECHO(tty) is true. Called from the driver receive_buf path.
927 * This variant does not treat control characters specially.
930 static void echo_char_raw(unsigned char c, struct n_tty_data *ldata)
932 if (c == ECHO_OP_START) {
933 add_echo_byte(ECHO_OP_START, ldata);
934 add_echo_byte(ECHO_OP_START, ldata);
935 } else {
936 add_echo_byte(c, ldata);
941 * echo_char - echo a character
942 * @c: unicode byte to echo
943 * @tty: terminal device
945 * Echo user input back onto the screen. This must be called only when
946 * L_ECHO(tty) is true. Called from the driver receive_buf path.
948 * This variant tags control characters to be echoed as "^X"
949 * (where X is the letter representing the control char).
952 static void echo_char(unsigned char c, struct tty_struct *tty)
954 struct n_tty_data *ldata = tty->disc_data;
956 if (c == ECHO_OP_START) {
957 add_echo_byte(ECHO_OP_START, ldata);
958 add_echo_byte(ECHO_OP_START, ldata);
959 } else {
960 if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t')
961 add_echo_byte(ECHO_OP_START, ldata);
962 add_echo_byte(c, ldata);
967 * finish_erasing - complete erase
968 * @ldata: n_tty data
971 static inline void finish_erasing(struct n_tty_data *ldata)
973 if (ldata->erasing) {
974 echo_char_raw('/', ldata);
975 ldata->erasing = 0;
980 * eraser - handle erase function
981 * @c: character input
982 * @tty: terminal device
984 * Perform erase and necessary output when an erase character is
985 * present in the stream from the driver layer. Handles the complexities
986 * of UTF-8 multibyte symbols.
988 * n_tty_receive_buf()/producer path:
989 * caller holds non-exclusive termios_rwsem
992 static void eraser(unsigned char c, struct tty_struct *tty)
994 struct n_tty_data *ldata = tty->disc_data;
995 enum { ERASE, WERASE, KILL } kill_type;
996 size_t head;
997 size_t cnt;
998 int seen_alnums;
1000 if (ldata->read_head == ldata->canon_head) {
1001 /* process_output('\a', tty); */ /* what do you think? */
1002 return;
1004 if (c == ERASE_CHAR(tty))
1005 kill_type = ERASE;
1006 else if (c == WERASE_CHAR(tty))
1007 kill_type = WERASE;
1008 else {
1009 if (!L_ECHO(tty)) {
1010 ldata->read_head = ldata->canon_head;
1011 return;
1013 if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
1014 ldata->read_head = ldata->canon_head;
1015 finish_erasing(ldata);
1016 echo_char(KILL_CHAR(tty), tty);
1017 /* Add a newline if ECHOK is on and ECHOKE is off. */
1018 if (L_ECHOK(tty))
1019 echo_char_raw('\n', ldata);
1020 return;
1022 kill_type = KILL;
1025 seen_alnums = 0;
1026 while (MASK(ldata->read_head) != MASK(ldata->canon_head)) {
1027 head = ldata->read_head;
1029 /* erase a single possibly multibyte character */
1030 do {
1031 head--;
1032 c = read_buf(ldata, head);
1033 } while (is_continuation(c, tty) &&
1034 MASK(head) != MASK(ldata->canon_head));
1036 /* do not partially erase */
1037 if (is_continuation(c, tty))
1038 break;
1040 if (kill_type == WERASE) {
1041 /* Equivalent to BSD's ALTWERASE. */
1042 if (isalnum(c) || c == '_')
1043 seen_alnums++;
1044 else if (seen_alnums)
1045 break;
1047 cnt = ldata->read_head - head;
1048 ldata->read_head = head;
1049 if (L_ECHO(tty)) {
1050 if (L_ECHOPRT(tty)) {
1051 if (!ldata->erasing) {
1052 echo_char_raw('\\', ldata);
1053 ldata->erasing = 1;
1055 /* if cnt > 1, output a multi-byte character */
1056 echo_char(c, tty);
1057 while (--cnt > 0) {
1058 head++;
1059 echo_char_raw(read_buf(ldata, head), ldata);
1060 echo_move_back_col(ldata);
1062 } else if (kill_type == ERASE && !L_ECHOE(tty)) {
1063 echo_char(ERASE_CHAR(tty), tty);
1064 } else if (c == '\t') {
1065 unsigned int num_chars = 0;
1066 int after_tab = 0;
1067 size_t tail = ldata->read_head;
1070 * Count the columns used for characters
1071 * since the start of input or after a
1072 * previous tab.
1073 * This info is used to go back the correct
1074 * number of columns.
1076 while (MASK(tail) != MASK(ldata->canon_head)) {
1077 tail--;
1078 c = read_buf(ldata, tail);
1079 if (c == '\t') {
1080 after_tab = 1;
1081 break;
1082 } else if (iscntrl(c)) {
1083 if (L_ECHOCTL(tty))
1084 num_chars += 2;
1085 } else if (!is_continuation(c, tty)) {
1086 num_chars++;
1089 echo_erase_tab(num_chars, after_tab, ldata);
1090 } else {
1091 if (iscntrl(c) && L_ECHOCTL(tty)) {
1092 echo_char_raw('\b', ldata);
1093 echo_char_raw(' ', ldata);
1094 echo_char_raw('\b', ldata);
1096 if (!iscntrl(c) || L_ECHOCTL(tty)) {
1097 echo_char_raw('\b', ldata);
1098 echo_char_raw(' ', ldata);
1099 echo_char_raw('\b', ldata);
1103 if (kill_type == ERASE)
1104 break;
1106 if (ldata->read_head == ldata->canon_head && L_ECHO(tty))
1107 finish_erasing(ldata);
1111 * isig - handle the ISIG optio
1112 * @sig: signal
1113 * @tty: terminal
1115 * Called when a signal is being sent due to terminal input.
1116 * Called from the driver receive_buf path so serialized.
1118 * Performs input and output flush if !NOFLSH. In this context, the echo
1119 * buffer is 'output'. The signal is processed first to alert any current
1120 * readers or writers to discontinue and exit their i/o loops.
1122 * Locking: ctrl_lock
1125 static void __isig(int sig, struct tty_struct *tty)
1127 struct pid *tty_pgrp = tty_get_pgrp(tty);
1128 if (tty_pgrp) {
1129 kill_pgrp(tty_pgrp, sig, 1);
1130 put_pid(tty_pgrp);
1134 static void isig(int sig, struct tty_struct *tty)
1136 struct n_tty_data *ldata = tty->disc_data;
1138 if (L_NOFLSH(tty)) {
1139 /* signal only */
1140 __isig(sig, tty);
1142 } else { /* signal and flush */
1143 up_read(&tty->termios_rwsem);
1144 down_write(&tty->termios_rwsem);
1146 __isig(sig, tty);
1148 /* clear echo buffer */
1149 mutex_lock(&ldata->output_lock);
1150 ldata->echo_head = ldata->echo_tail = 0;
1151 ldata->echo_mark = ldata->echo_commit = 0;
1152 mutex_unlock(&ldata->output_lock);
1154 /* clear output buffer */
1155 tty_driver_flush_buffer(tty);
1157 /* clear input buffer */
1158 reset_buffer_flags(tty->disc_data);
1160 /* notify pty master of flush */
1161 if (tty->link)
1162 n_tty_packet_mode_flush(tty);
1164 up_write(&tty->termios_rwsem);
1165 down_read(&tty->termios_rwsem);
1170 * n_tty_receive_break - handle break
1171 * @tty: terminal
1173 * An RS232 break event has been hit in the incoming bitstream. This
1174 * can cause a variety of events depending upon the termios settings.
1176 * n_tty_receive_buf()/producer path:
1177 * caller holds non-exclusive termios_rwsem
1179 * Note: may get exclusive termios_rwsem if flushing input buffer
1182 static void n_tty_receive_break(struct tty_struct *tty)
1184 struct n_tty_data *ldata = tty->disc_data;
1186 if (I_IGNBRK(tty))
1187 return;
1188 if (I_BRKINT(tty)) {
1189 isig(SIGINT, tty);
1190 return;
1192 if (I_PARMRK(tty)) {
1193 put_tty_queue('\377', ldata);
1194 put_tty_queue('\0', ldata);
1196 put_tty_queue('\0', ldata);
1200 * n_tty_receive_overrun - handle overrun reporting
1201 * @tty: terminal
1203 * Data arrived faster than we could process it. While the tty
1204 * driver has flagged this the bits that were missed are gone
1205 * forever.
1207 * Called from the receive_buf path so single threaded. Does not
1208 * need locking as num_overrun and overrun_time are function
1209 * private.
1212 static void n_tty_receive_overrun(struct tty_struct *tty)
1214 struct n_tty_data *ldata = tty->disc_data;
1216 ldata->num_overrun++;
1217 if (time_after(jiffies, ldata->overrun_time + HZ) ||
1218 time_after(ldata->overrun_time, jiffies)) {
1219 printk(KERN_WARNING "%s: %d input overrun(s)\n",
1220 tty_name(tty),
1221 ldata->num_overrun);
1222 ldata->overrun_time = jiffies;
1223 ldata->num_overrun = 0;
1228 * n_tty_receive_parity_error - error notifier
1229 * @tty: terminal device
1230 * @c: character
1232 * Process a parity error and queue the right data to indicate
1233 * the error case if necessary.
1235 * n_tty_receive_buf()/producer path:
1236 * caller holds non-exclusive termios_rwsem
1238 static void n_tty_receive_parity_error(struct tty_struct *tty, unsigned char c)
1240 struct n_tty_data *ldata = tty->disc_data;
1242 if (I_INPCK(tty)) {
1243 if (I_IGNPAR(tty))
1244 return;
1245 if (I_PARMRK(tty)) {
1246 put_tty_queue('\377', ldata);
1247 put_tty_queue('\0', ldata);
1248 put_tty_queue(c, ldata);
1249 } else
1250 put_tty_queue('\0', ldata);
1251 } else
1252 put_tty_queue(c, ldata);
1255 static void
1256 n_tty_receive_signal_char(struct tty_struct *tty, int signal, unsigned char c)
1258 isig(signal, tty);
1259 if (I_IXON(tty))
1260 start_tty(tty);
1261 if (L_ECHO(tty)) {
1262 echo_char(c, tty);
1263 commit_echoes(tty);
1264 } else
1265 process_echoes(tty);
1266 return;
1270 * n_tty_receive_char - perform processing
1271 * @tty: terminal device
1272 * @c: character
1274 * Process an individual character of input received from the driver.
1275 * This is serialized with respect to itself by the rules for the
1276 * driver above.
1278 * n_tty_receive_buf()/producer path:
1279 * caller holds non-exclusive termios_rwsem
1280 * publishes canon_head if canonical mode is active
1282 * Returns 1 if LNEXT was received, else returns 0
1285 static int
1286 n_tty_receive_char_special(struct tty_struct *tty, unsigned char c)
1288 struct n_tty_data *ldata = tty->disc_data;
1290 if (I_IXON(tty)) {
1291 if (c == START_CHAR(tty)) {
1292 start_tty(tty);
1293 process_echoes(tty);
1294 return 0;
1296 if (c == STOP_CHAR(tty)) {
1297 stop_tty(tty);
1298 return 0;
1302 if (L_ISIG(tty)) {
1303 if (c == INTR_CHAR(tty)) {
1304 n_tty_receive_signal_char(tty, SIGINT, c);
1305 return 0;
1306 } else if (c == QUIT_CHAR(tty)) {
1307 n_tty_receive_signal_char(tty, SIGQUIT, c);
1308 return 0;
1309 } else if (c == SUSP_CHAR(tty)) {
1310 n_tty_receive_signal_char(tty, SIGTSTP, c);
1311 return 0;
1315 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) && I_IXANY(tty)) {
1316 start_tty(tty);
1317 process_echoes(tty);
1320 if (c == '\r') {
1321 if (I_IGNCR(tty))
1322 return 0;
1323 if (I_ICRNL(tty))
1324 c = '\n';
1325 } else if (c == '\n' && I_INLCR(tty))
1326 c = '\r';
1328 if (ldata->icanon) {
1329 if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
1330 (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
1331 eraser(c, tty);
1332 commit_echoes(tty);
1333 return 0;
1335 if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
1336 ldata->lnext = 1;
1337 if (L_ECHO(tty)) {
1338 finish_erasing(ldata);
1339 if (L_ECHOCTL(tty)) {
1340 echo_char_raw('^', ldata);
1341 echo_char_raw('\b', ldata);
1342 commit_echoes(tty);
1345 return 1;
1347 if (c == REPRINT_CHAR(tty) && L_ECHO(tty) && L_IEXTEN(tty)) {
1348 size_t tail = ldata->canon_head;
1350 finish_erasing(ldata);
1351 echo_char(c, tty);
1352 echo_char_raw('\n', ldata);
1353 while (MASK(tail) != MASK(ldata->read_head)) {
1354 echo_char(read_buf(ldata, tail), tty);
1355 tail++;
1357 commit_echoes(tty);
1358 return 0;
1360 if (c == '\n') {
1361 if (L_ECHO(tty) || L_ECHONL(tty)) {
1362 echo_char_raw('\n', ldata);
1363 commit_echoes(tty);
1365 goto handle_newline;
1367 if (c == EOF_CHAR(tty)) {
1368 c = __DISABLED_CHAR;
1369 goto handle_newline;
1371 if ((c == EOL_CHAR(tty)) ||
1372 (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
1374 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
1376 if (L_ECHO(tty)) {
1377 /* Record the column of first canon char. */
1378 if (ldata->canon_head == ldata->read_head)
1379 echo_set_canon_col(ldata);
1380 echo_char(c, tty);
1381 commit_echoes(tty);
1384 * XXX does PARMRK doubling happen for
1385 * EOL_CHAR and EOL2_CHAR?
1387 if (c == (unsigned char) '\377' && I_PARMRK(tty))
1388 put_tty_queue(c, ldata);
1390 handle_newline:
1391 set_bit(ldata->read_head & (N_TTY_BUF_SIZE - 1), ldata->read_flags);
1392 put_tty_queue(c, ldata);
1393 smp_store_release(&ldata->canon_head, ldata->read_head);
1394 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1395 wake_up_interruptible_poll(&tty->read_wait, POLLIN);
1396 return 0;
1400 if (L_ECHO(tty)) {
1401 finish_erasing(ldata);
1402 if (c == '\n')
1403 echo_char_raw('\n', ldata);
1404 else {
1405 /* Record the column of first canon char. */
1406 if (ldata->canon_head == ldata->read_head)
1407 echo_set_canon_col(ldata);
1408 echo_char(c, tty);
1410 commit_echoes(tty);
1413 /* PARMRK doubling check */
1414 if (c == (unsigned char) '\377' && I_PARMRK(tty))
1415 put_tty_queue(c, ldata);
1417 put_tty_queue(c, ldata);
1418 return 0;
1421 static inline void
1422 n_tty_receive_char_inline(struct tty_struct *tty, unsigned char c)
1424 struct n_tty_data *ldata = tty->disc_data;
1426 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) && I_IXANY(tty)) {
1427 start_tty(tty);
1428 process_echoes(tty);
1430 if (L_ECHO(tty)) {
1431 finish_erasing(ldata);
1432 /* Record the column of first canon char. */
1433 if (ldata->canon_head == ldata->read_head)
1434 echo_set_canon_col(ldata);
1435 echo_char(c, tty);
1436 commit_echoes(tty);
1438 /* PARMRK doubling check */
1439 if (c == (unsigned char) '\377' && I_PARMRK(tty))
1440 put_tty_queue(c, ldata);
1441 put_tty_queue(c, ldata);
1444 static void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
1446 n_tty_receive_char_inline(tty, c);
1449 static inline void
1450 n_tty_receive_char_fast(struct tty_struct *tty, unsigned char c)
1452 struct n_tty_data *ldata = tty->disc_data;
1454 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) && I_IXANY(tty)) {
1455 start_tty(tty);
1456 process_echoes(tty);
1458 if (L_ECHO(tty)) {
1459 finish_erasing(ldata);
1460 /* Record the column of first canon char. */
1461 if (ldata->canon_head == ldata->read_head)
1462 echo_set_canon_col(ldata);
1463 echo_char(c, tty);
1464 commit_echoes(tty);
1466 put_tty_queue(c, ldata);
1469 static void n_tty_receive_char_closing(struct tty_struct *tty, unsigned char c)
1471 if (I_ISTRIP(tty))
1472 c &= 0x7f;
1473 if (I_IUCLC(tty) && L_IEXTEN(tty))
1474 c = tolower(c);
1476 if (I_IXON(tty)) {
1477 if (c == STOP_CHAR(tty))
1478 stop_tty(tty);
1479 else if (c == START_CHAR(tty) ||
1480 (tty->stopped && !tty->flow_stopped && I_IXANY(tty) &&
1481 c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) &&
1482 c != SUSP_CHAR(tty))) {
1483 start_tty(tty);
1484 process_echoes(tty);
1489 static void
1490 n_tty_receive_char_flagged(struct tty_struct *tty, unsigned char c, char flag)
1492 switch (flag) {
1493 case TTY_BREAK:
1494 n_tty_receive_break(tty);
1495 break;
1496 case TTY_PARITY:
1497 case TTY_FRAME:
1498 n_tty_receive_parity_error(tty, c);
1499 break;
1500 case TTY_OVERRUN:
1501 n_tty_receive_overrun(tty);
1502 break;
1503 default:
1504 printk(KERN_ERR "%s: unknown flag %d\n",
1505 tty_name(tty), flag);
1506 break;
1510 static void
1511 n_tty_receive_char_lnext(struct tty_struct *tty, unsigned char c, char flag)
1513 struct n_tty_data *ldata = tty->disc_data;
1515 ldata->lnext = 0;
1516 if (likely(flag == TTY_NORMAL)) {
1517 if (I_ISTRIP(tty))
1518 c &= 0x7f;
1519 if (I_IUCLC(tty) && L_IEXTEN(tty))
1520 c = tolower(c);
1521 n_tty_receive_char(tty, c);
1522 } else
1523 n_tty_receive_char_flagged(tty, c, flag);
1526 static void
1527 n_tty_receive_buf_real_raw(struct tty_struct *tty, const unsigned char *cp,
1528 char *fp, int count)
1530 struct n_tty_data *ldata = tty->disc_data;
1531 size_t n, head;
1533 head = ldata->read_head & (N_TTY_BUF_SIZE - 1);
1534 n = min_t(size_t, count, N_TTY_BUF_SIZE - head);
1535 memcpy(read_buf_addr(ldata, head), cp, n);
1536 ldata->read_head += n;
1537 cp += n;
1538 count -= n;
1540 head = ldata->read_head & (N_TTY_BUF_SIZE - 1);
1541 n = min_t(size_t, count, N_TTY_BUF_SIZE - head);
1542 memcpy(read_buf_addr(ldata, head), cp, n);
1543 ldata->read_head += n;
1546 static void
1547 n_tty_receive_buf_raw(struct tty_struct *tty, const unsigned char *cp,
1548 char *fp, int count)
1550 struct n_tty_data *ldata = tty->disc_data;
1551 char flag = TTY_NORMAL;
1553 while (count--) {
1554 if (fp)
1555 flag = *fp++;
1556 if (likely(flag == TTY_NORMAL))
1557 put_tty_queue(*cp++, ldata);
1558 else
1559 n_tty_receive_char_flagged(tty, *cp++, flag);
1563 static void
1564 n_tty_receive_buf_closing(struct tty_struct *tty, const unsigned char *cp,
1565 char *fp, int count)
1567 char flag = TTY_NORMAL;
1569 while (count--) {
1570 if (fp)
1571 flag = *fp++;
1572 if (likely(flag == TTY_NORMAL))
1573 n_tty_receive_char_closing(tty, *cp++);
1574 else
1575 n_tty_receive_char_flagged(tty, *cp++, flag);
1579 static void
1580 n_tty_receive_buf_standard(struct tty_struct *tty, const unsigned char *cp,
1581 char *fp, int count)
1583 struct n_tty_data *ldata = tty->disc_data;
1584 char flag = TTY_NORMAL;
1586 while (count--) {
1587 if (fp)
1588 flag = *fp++;
1589 if (likely(flag == TTY_NORMAL)) {
1590 unsigned char c = *cp++;
1592 if (I_ISTRIP(tty))
1593 c &= 0x7f;
1594 if (I_IUCLC(tty) && L_IEXTEN(tty))
1595 c = tolower(c);
1596 if (L_EXTPROC(tty)) {
1597 put_tty_queue(c, ldata);
1598 continue;
1600 if (!test_bit(c, ldata->char_map))
1601 n_tty_receive_char_inline(tty, c);
1602 else if (n_tty_receive_char_special(tty, c) && count) {
1603 if (fp)
1604 flag = *fp++;
1605 n_tty_receive_char_lnext(tty, *cp++, flag);
1606 count--;
1608 } else
1609 n_tty_receive_char_flagged(tty, *cp++, flag);
1613 static void
1614 n_tty_receive_buf_fast(struct tty_struct *tty, const unsigned char *cp,
1615 char *fp, int count)
1617 struct n_tty_data *ldata = tty->disc_data;
1618 char flag = TTY_NORMAL;
1620 while (count--) {
1621 if (fp)
1622 flag = *fp++;
1623 if (likely(flag == TTY_NORMAL)) {
1624 unsigned char c = *cp++;
1626 if (!test_bit(c, ldata->char_map))
1627 n_tty_receive_char_fast(tty, c);
1628 else if (n_tty_receive_char_special(tty, c) && count) {
1629 if (fp)
1630 flag = *fp++;
1631 n_tty_receive_char_lnext(tty, *cp++, flag);
1632 count--;
1634 } else
1635 n_tty_receive_char_flagged(tty, *cp++, flag);
1639 static void __receive_buf(struct tty_struct *tty, const unsigned char *cp,
1640 char *fp, int count)
1642 struct n_tty_data *ldata = tty->disc_data;
1643 bool preops = I_ISTRIP(tty) || (I_IUCLC(tty) && L_IEXTEN(tty));
1645 if (ldata->real_raw)
1646 n_tty_receive_buf_real_raw(tty, cp, fp, count);
1647 else if (ldata->raw || (L_EXTPROC(tty) && !preops))
1648 n_tty_receive_buf_raw(tty, cp, fp, count);
1649 else if (tty->closing && !L_EXTPROC(tty))
1650 n_tty_receive_buf_closing(tty, cp, fp, count);
1651 else {
1652 if (ldata->lnext) {
1653 char flag = TTY_NORMAL;
1655 if (fp)
1656 flag = *fp++;
1657 n_tty_receive_char_lnext(tty, *cp++, flag);
1658 count--;
1661 if (!preops && !I_PARMRK(tty))
1662 n_tty_receive_buf_fast(tty, cp, fp, count);
1663 else
1664 n_tty_receive_buf_standard(tty, cp, fp, count);
1666 flush_echoes(tty);
1667 if (tty->ops->flush_chars)
1668 tty->ops->flush_chars(tty);
1671 if (ldata->icanon && !L_EXTPROC(tty))
1672 return;
1674 /* publish read_head to consumer */
1675 smp_store_release(&ldata->commit_head, ldata->read_head);
1677 if ((read_cnt(ldata) >= ldata->minimum_to_wake) || L_EXTPROC(tty)) {
1678 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1679 wake_up_interruptible_poll(&tty->read_wait, POLLIN);
1684 * n_tty_receive_buf_common - process input
1685 * @tty: device to receive input
1686 * @cp: input chars
1687 * @fp: flags for each char (if NULL, all chars are TTY_NORMAL)
1688 * @count: number of input chars in @cp
1690 * Called by the terminal driver when a block of characters has
1691 * been received. This function must be called from soft contexts
1692 * not from interrupt context. The driver is responsible for making
1693 * calls one at a time and in order (or using flush_to_ldisc)
1695 * Returns the # of input chars from @cp which were processed.
1697 * In canonical mode, the maximum line length is 4096 chars (including
1698 * the line termination char); lines longer than 4096 chars are
1699 * truncated. After 4095 chars, input data is still processed but
1700 * not stored. Overflow processing ensures the tty can always
1701 * receive more input until at least one line can be read.
1703 * In non-canonical mode, the read buffer will only accept 4095 chars;
1704 * this provides the necessary space for a newline char if the input
1705 * mode is switched to canonical.
1707 * Note it is possible for the read buffer to _contain_ 4096 chars
1708 * in non-canonical mode: the read buffer could already contain the
1709 * maximum canon line of 4096 chars when the mode is switched to
1710 * non-canonical.
1712 * n_tty_receive_buf()/producer path:
1713 * claims non-exclusive termios_rwsem
1714 * publishes commit_head or canon_head
1716 static int
1717 n_tty_receive_buf_common(struct tty_struct *tty, const unsigned char *cp,
1718 char *fp, int count, int flow)
1720 struct n_tty_data *ldata = tty->disc_data;
1721 int room, n, rcvd = 0, overflow;
1723 down_read(&tty->termios_rwsem);
1725 while (1) {
1727 * When PARMRK is set, each input char may take up to 3 chars
1728 * in the read buf; reduce the buffer space avail by 3x
1730 * If we are doing input canonicalization, and there are no
1731 * pending newlines, let characters through without limit, so
1732 * that erase characters will be handled. Other excess
1733 * characters will be beeped.
1735 * paired with store in *_copy_from_read_buf() -- guarantees
1736 * the consumer has loaded the data in read_buf up to the new
1737 * read_tail (so this producer will not overwrite unread data)
1739 size_t tail = smp_load_acquire(&ldata->read_tail);
1741 room = N_TTY_BUF_SIZE - (ldata->read_head - tail);
1742 if (I_PARMRK(tty))
1743 room = (room + 2) / 3;
1744 room--;
1745 if (room <= 0) {
1746 overflow = ldata->icanon && ldata->canon_head == tail;
1747 if (overflow && room < 0)
1748 ldata->read_head--;
1749 room = overflow;
1750 ldata->no_room = flow && !room;
1751 } else
1752 overflow = 0;
1754 n = min(count, room);
1755 if (!n)
1756 break;
1758 /* ignore parity errors if handling overflow */
1759 if (!overflow || !fp || *fp != TTY_PARITY)
1760 __receive_buf(tty, cp, fp, n);
1762 cp += n;
1763 if (fp)
1764 fp += n;
1765 count -= n;
1766 rcvd += n;
1769 tty->receive_room = room;
1771 /* Unthrottle if handling overflow on pty */
1772 if (tty->driver->type == TTY_DRIVER_TYPE_PTY) {
1773 if (overflow) {
1774 tty_set_flow_change(tty, TTY_UNTHROTTLE_SAFE);
1775 tty_unthrottle_safe(tty);
1776 __tty_set_flow_change(tty, 0);
1778 } else
1779 n_tty_check_throttle(tty);
1781 up_read(&tty->termios_rwsem);
1783 return rcvd;
1786 static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
1787 char *fp, int count)
1789 n_tty_receive_buf_common(tty, cp, fp, count, 0);
1792 static int n_tty_receive_buf2(struct tty_struct *tty, const unsigned char *cp,
1793 char *fp, int count)
1795 return n_tty_receive_buf_common(tty, cp, fp, count, 1);
1798 int is_ignored(int sig)
1800 return (sigismember(&current->blocked, sig) ||
1801 current->sighand->action[sig-1].sa.sa_handler == SIG_IGN);
1805 * n_tty_set_termios - termios data changed
1806 * @tty: terminal
1807 * @old: previous data
1809 * Called by the tty layer when the user changes termios flags so
1810 * that the line discipline can plan ahead. This function cannot sleep
1811 * and is protected from re-entry by the tty layer. The user is
1812 * guaranteed that this function will not be re-entered or in progress
1813 * when the ldisc is closed.
1815 * Locking: Caller holds tty->termios_rwsem
1818 static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
1820 struct n_tty_data *ldata = tty->disc_data;
1822 if (!old || (old->c_lflag ^ tty->termios.c_lflag) & (ICANON | EXTPROC)) {
1823 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
1824 ldata->line_start = ldata->read_tail;
1825 if (!L_ICANON(tty) || !read_cnt(ldata)) {
1826 ldata->canon_head = ldata->read_tail;
1827 ldata->push = 0;
1828 } else {
1829 set_bit((ldata->read_head - 1) & (N_TTY_BUF_SIZE - 1),
1830 ldata->read_flags);
1831 ldata->canon_head = ldata->read_head;
1832 ldata->push = 1;
1834 ldata->commit_head = ldata->read_head;
1835 ldata->erasing = 0;
1836 ldata->lnext = 0;
1839 ldata->icanon = (L_ICANON(tty) != 0);
1841 if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
1842 I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
1843 I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
1844 I_PARMRK(tty)) {
1845 bitmap_zero(ldata->char_map, 256);
1847 if (I_IGNCR(tty) || I_ICRNL(tty))
1848 set_bit('\r', ldata->char_map);
1849 if (I_INLCR(tty))
1850 set_bit('\n', ldata->char_map);
1852 if (L_ICANON(tty)) {
1853 set_bit(ERASE_CHAR(tty), ldata->char_map);
1854 set_bit(KILL_CHAR(tty), ldata->char_map);
1855 set_bit(EOF_CHAR(tty), ldata->char_map);
1856 set_bit('\n', ldata->char_map);
1857 set_bit(EOL_CHAR(tty), ldata->char_map);
1858 if (L_IEXTEN(tty)) {
1859 set_bit(WERASE_CHAR(tty), ldata->char_map);
1860 set_bit(LNEXT_CHAR(tty), ldata->char_map);
1861 set_bit(EOL2_CHAR(tty), ldata->char_map);
1862 if (L_ECHO(tty))
1863 set_bit(REPRINT_CHAR(tty),
1864 ldata->char_map);
1867 if (I_IXON(tty)) {
1868 set_bit(START_CHAR(tty), ldata->char_map);
1869 set_bit(STOP_CHAR(tty), ldata->char_map);
1871 if (L_ISIG(tty)) {
1872 set_bit(INTR_CHAR(tty), ldata->char_map);
1873 set_bit(QUIT_CHAR(tty), ldata->char_map);
1874 set_bit(SUSP_CHAR(tty), ldata->char_map);
1876 clear_bit(__DISABLED_CHAR, ldata->char_map);
1877 ldata->raw = 0;
1878 ldata->real_raw = 0;
1879 } else {
1880 ldata->raw = 1;
1881 if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
1882 (I_IGNPAR(tty) || !I_INPCK(tty)) &&
1883 (tty->driver->flags & TTY_DRIVER_REAL_RAW))
1884 ldata->real_raw = 1;
1885 else
1886 ldata->real_raw = 0;
1889 * Fix tty hang when I_IXON(tty) is cleared, but the tty
1890 * been stopped by STOP_CHAR(tty) before it.
1892 if (!I_IXON(tty) && old && (old->c_iflag & IXON) && !tty->flow_stopped) {
1893 start_tty(tty);
1894 process_echoes(tty);
1897 /* The termios change make the tty ready for I/O */
1898 wake_up_interruptible(&tty->write_wait);
1899 wake_up_interruptible(&tty->read_wait);
1903 * n_tty_close - close the ldisc for this tty
1904 * @tty: device
1906 * Called from the terminal layer when this line discipline is
1907 * being shut down, either because of a close or becsuse of a
1908 * discipline change. The function will not be called while other
1909 * ldisc methods are in progress.
1912 static void n_tty_close(struct tty_struct *tty)
1914 struct n_tty_data *ldata = tty->disc_data;
1916 if (tty->link)
1917 n_tty_packet_mode_flush(tty);
1919 vfree(ldata);
1920 tty->disc_data = NULL;
1924 * n_tty_open - open an ldisc
1925 * @tty: terminal to open
1927 * Called when this line discipline is being attached to the
1928 * terminal device. Can sleep. Called serialized so that no
1929 * other events will occur in parallel. No further open will occur
1930 * until a close.
1933 static int n_tty_open(struct tty_struct *tty)
1935 struct n_tty_data *ldata;
1937 /* Currently a malloc failure here can panic */
1938 ldata = vzalloc(sizeof(*ldata));
1939 if (!ldata)
1940 return -ENOMEM;
1942 ldata->overrun_time = jiffies;
1943 mutex_init(&ldata->atomic_read_lock);
1944 mutex_init(&ldata->output_lock);
1946 tty->disc_data = ldata;
1947 ldata->minimum_to_wake = 1;
1948 tty->closing = 0;
1949 /* indicate buffer work may resume */
1950 clear_bit(TTY_LDISC_HALTED, &tty->flags);
1951 n_tty_set_termios(tty, NULL);
1952 tty_unthrottle(tty);
1953 return 0;
1956 static inline int input_available_p(struct tty_struct *tty, int poll)
1958 struct n_tty_data *ldata = tty->disc_data;
1959 int amt = poll && !TIME_CHAR(tty) && MIN_CHAR(tty) ? MIN_CHAR(tty) : 1;
1961 if (ldata->icanon && !L_EXTPROC(tty))
1962 return ldata->canon_head != ldata->read_tail;
1963 else
1964 return ldata->commit_head - ldata->read_tail >= amt;
1968 * copy_from_read_buf - copy read data directly
1969 * @tty: terminal device
1970 * @b: user data
1971 * @nr: size of data
1973 * Helper function to speed up n_tty_read. It is only called when
1974 * ICANON is off; it copies characters straight from the tty queue to
1975 * user space directly. It can be profitably called twice; once to
1976 * drain the space from the tail pointer to the (physical) end of the
1977 * buffer, and once to drain the space from the (physical) beginning of
1978 * the buffer to head pointer.
1980 * Called under the ldata->atomic_read_lock sem
1982 * n_tty_read()/consumer path:
1983 * caller holds non-exclusive termios_rwsem
1984 * read_tail published
1987 static int copy_from_read_buf(struct tty_struct *tty,
1988 unsigned char __user **b,
1989 size_t *nr)
1992 struct n_tty_data *ldata = tty->disc_data;
1993 int retval;
1994 size_t n;
1995 bool is_eof;
1996 size_t head = smp_load_acquire(&ldata->commit_head);
1997 size_t tail = ldata->read_tail & (N_TTY_BUF_SIZE - 1);
1999 retval = 0;
2000 n = min(head - ldata->read_tail, N_TTY_BUF_SIZE - tail);
2001 n = min(*nr, n);
2002 if (n) {
2003 retval = copy_to_user(*b, read_buf_addr(ldata, tail), n);
2004 n -= retval;
2005 is_eof = n == 1 && read_buf(ldata, tail) == EOF_CHAR(tty);
2006 tty_audit_add_data(tty, read_buf_addr(ldata, tail), n,
2007 ldata->icanon);
2008 smp_store_release(&ldata->read_tail, ldata->read_tail + n);
2009 /* Turn single EOF into zero-length read */
2010 if (L_EXTPROC(tty) && ldata->icanon && is_eof &&
2011 (head == ldata->read_tail))
2012 n = 0;
2013 *b += n;
2014 *nr -= n;
2016 return retval;
2020 * canon_copy_from_read_buf - copy read data in canonical mode
2021 * @tty: terminal device
2022 * @b: user data
2023 * @nr: size of data
2025 * Helper function for n_tty_read. It is only called when ICANON is on;
2026 * it copies one line of input up to and including the line-delimiting
2027 * character into the user-space buffer.
2029 * NB: When termios is changed from non-canonical to canonical mode and
2030 * the read buffer contains data, n_tty_set_termios() simulates an EOF
2031 * push (as if C-d were input) _without_ the DISABLED_CHAR in the buffer.
2032 * This causes data already processed as input to be immediately available
2033 * as input although a newline has not been received.
2035 * Called under the atomic_read_lock mutex
2037 * n_tty_read()/consumer path:
2038 * caller holds non-exclusive termios_rwsem
2039 * read_tail published
2042 static int canon_copy_from_read_buf(struct tty_struct *tty,
2043 unsigned char __user **b,
2044 size_t *nr)
2046 struct n_tty_data *ldata = tty->disc_data;
2047 size_t n, size, more, c;
2048 size_t eol;
2049 size_t tail;
2050 int ret, found = 0;
2052 /* N.B. avoid overrun if nr == 0 */
2053 if (!*nr)
2054 return 0;
2056 n = min(*nr + 1, smp_load_acquire(&ldata->canon_head) - ldata->read_tail);
2058 tail = ldata->read_tail & (N_TTY_BUF_SIZE - 1);
2059 size = min_t(size_t, tail + n, N_TTY_BUF_SIZE);
2061 n_tty_trace("%s: nr:%zu tail:%zu n:%zu size:%zu\n",
2062 __func__, *nr, tail, n, size);
2064 eol = find_next_bit(ldata->read_flags, size, tail);
2065 more = n - (size - tail);
2066 if (eol == N_TTY_BUF_SIZE && more) {
2067 /* scan wrapped without finding set bit */
2068 eol = find_next_bit(ldata->read_flags, more, 0);
2069 if (eol != more)
2070 found = 1;
2071 } else if (eol != size)
2072 found = 1;
2074 size = N_TTY_BUF_SIZE - tail;
2075 n = eol - tail;
2076 if (n > N_TTY_BUF_SIZE)
2077 n += N_TTY_BUF_SIZE;
2078 c = n + found;
2080 if (!found || read_buf(ldata, eol) != __DISABLED_CHAR) {
2081 c = min(*nr, c);
2082 n = c;
2085 n_tty_trace("%s: eol:%zu found:%d n:%zu c:%zu size:%zu more:%zu\n",
2086 __func__, eol, found, n, c, size, more);
2088 if (n > size) {
2089 ret = tty_copy_to_user(tty, *b, read_buf_addr(ldata, tail), size);
2090 if (ret)
2091 return -EFAULT;
2092 ret = tty_copy_to_user(tty, *b + size, ldata->read_buf, n - size);
2093 } else
2094 ret = tty_copy_to_user(tty, *b, read_buf_addr(ldata, tail), n);
2096 if (ret)
2097 return -EFAULT;
2098 *b += n;
2099 *nr -= n;
2101 if (found)
2102 clear_bit(eol, ldata->read_flags);
2103 smp_store_release(&ldata->read_tail, ldata->read_tail + c);
2105 if (found) {
2106 if (!ldata->push)
2107 ldata->line_start = ldata->read_tail;
2108 else
2109 ldata->push = 0;
2110 tty_audit_push(tty);
2112 return 0;
2115 extern ssize_t redirected_tty_write(struct file *, const char __user *,
2116 size_t, loff_t *);
2119 * job_control - check job control
2120 * @tty: tty
2121 * @file: file handle
2123 * Perform job control management checks on this file/tty descriptor
2124 * and if appropriate send any needed signals and return a negative
2125 * error code if action should be taken.
2127 * Locking: redirected write test is safe
2128 * current->signal->tty check is safe
2129 * ctrl_lock to safely reference tty->pgrp
2132 static int job_control(struct tty_struct *tty, struct file *file)
2134 /* Job control check -- must be done at start and after
2135 every sleep (POSIX.1 7.1.1.4). */
2136 /* NOTE: not yet done after every sleep pending a thorough
2137 check of the logic of this change. -- jlc */
2138 /* don't stop on /dev/console */
2139 if (file->f_op->write == redirected_tty_write)
2140 return 0;
2142 return __tty_check_change(tty, SIGTTIN);
2147 * n_tty_read - read function for tty
2148 * @tty: tty device
2149 * @file: file object
2150 * @buf: userspace buffer pointer
2151 * @nr: size of I/O
2153 * Perform reads for the line discipline. We are guaranteed that the
2154 * line discipline will not be closed under us but we may get multiple
2155 * parallel readers and must handle this ourselves. We may also get
2156 * a hangup. Always called in user context, may sleep.
2158 * This code must be sure never to sleep through a hangup.
2160 * n_tty_read()/consumer path:
2161 * claims non-exclusive termios_rwsem
2162 * publishes read_tail
2165 static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
2166 unsigned char __user *buf, size_t nr)
2168 struct n_tty_data *ldata = tty->disc_data;
2169 unsigned char __user *b = buf;
2170 DEFINE_WAIT_FUNC(wait, woken_wake_function);
2171 int c;
2172 int minimum, time;
2173 ssize_t retval = 0;
2174 long timeout;
2175 int packet;
2176 size_t tail;
2178 c = job_control(tty, file);
2179 if (c < 0)
2180 return c;
2183 * Internal serialization of reads.
2185 if (file->f_flags & O_NONBLOCK) {
2186 if (!mutex_trylock(&ldata->atomic_read_lock))
2187 return -EAGAIN;
2188 } else {
2189 if (mutex_lock_interruptible(&ldata->atomic_read_lock))
2190 return -ERESTARTSYS;
2193 down_read(&tty->termios_rwsem);
2195 minimum = time = 0;
2196 timeout = MAX_SCHEDULE_TIMEOUT;
2197 if (!ldata->icanon) {
2198 minimum = MIN_CHAR(tty);
2199 if (minimum) {
2200 time = (HZ / 10) * TIME_CHAR(tty);
2201 if (time)
2202 ldata->minimum_to_wake = 1;
2203 else if (!waitqueue_active(&tty->read_wait) ||
2204 (ldata->minimum_to_wake > minimum))
2205 ldata->minimum_to_wake = minimum;
2206 } else {
2207 timeout = (HZ / 10) * TIME_CHAR(tty);
2208 ldata->minimum_to_wake = minimum = 1;
2212 packet = tty->packet;
2213 tail = ldata->read_tail;
2215 add_wait_queue(&tty->read_wait, &wait);
2216 while (nr) {
2217 /* First test for status change. */
2218 if (packet && tty->link->ctrl_status) {
2219 unsigned char cs;
2220 if (b != buf)
2221 break;
2222 spin_lock_irq(&tty->link->ctrl_lock);
2223 cs = tty->link->ctrl_status;
2224 tty->link->ctrl_status = 0;
2225 spin_unlock_irq(&tty->link->ctrl_lock);
2226 if (tty_put_user(tty, cs, b++)) {
2227 retval = -EFAULT;
2228 b--;
2229 break;
2231 nr--;
2232 break;
2235 if (((minimum - (b - buf)) < ldata->minimum_to_wake) &&
2236 ((minimum - (b - buf)) >= 1))
2237 ldata->minimum_to_wake = (minimum - (b - buf));
2239 if (!input_available_p(tty, 0)) {
2240 up_read(&tty->termios_rwsem);
2241 tty_buffer_flush_work(tty->port);
2242 down_read(&tty->termios_rwsem);
2243 if (!input_available_p(tty, 0)) {
2244 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
2245 retval = -EIO;
2246 break;
2248 if (tty_hung_up_p(file))
2249 break;
2251 * Abort readers for ttys which never actually
2252 * get hung up. See __tty_hangup().
2254 if (test_bit(TTY_HUPPING, &tty->flags))
2255 break;
2256 if (!timeout)
2257 break;
2258 if (file->f_flags & O_NONBLOCK) {
2259 retval = -EAGAIN;
2260 break;
2262 if (signal_pending(current)) {
2263 retval = -ERESTARTSYS;
2264 break;
2266 up_read(&tty->termios_rwsem);
2268 timeout = wait_woken(&wait, TASK_INTERRUPTIBLE,
2269 timeout);
2271 down_read(&tty->termios_rwsem);
2272 continue;
2276 if (ldata->icanon && !L_EXTPROC(tty)) {
2277 retval = canon_copy_from_read_buf(tty, &b, &nr);
2278 if (retval)
2279 break;
2280 } else {
2281 int uncopied;
2283 /* Deal with packet mode. */
2284 if (packet && b == buf) {
2285 if (tty_put_user(tty, TIOCPKT_DATA, b++)) {
2286 retval = -EFAULT;
2287 b--;
2288 break;
2290 nr--;
2293 uncopied = copy_from_read_buf(tty, &b, &nr);
2294 uncopied += copy_from_read_buf(tty, &b, &nr);
2295 if (uncopied) {
2296 retval = -EFAULT;
2297 break;
2301 n_tty_check_unthrottle(tty);
2303 if (b - buf >= minimum)
2304 break;
2305 if (time)
2306 timeout = time;
2308 if (tail != ldata->read_tail)
2309 n_tty_kick_worker(tty);
2310 up_read(&tty->termios_rwsem);
2312 remove_wait_queue(&tty->read_wait, &wait);
2313 if (!waitqueue_active(&tty->read_wait))
2314 ldata->minimum_to_wake = minimum;
2316 mutex_unlock(&ldata->atomic_read_lock);
2318 if (b - buf)
2319 retval = b - buf;
2321 return retval;
2325 * n_tty_write - write function for tty
2326 * @tty: tty device
2327 * @file: file object
2328 * @buf: userspace buffer pointer
2329 * @nr: size of I/O
2331 * Write function of the terminal device. This is serialized with
2332 * respect to other write callers but not to termios changes, reads
2333 * and other such events. Since the receive code will echo characters,
2334 * thus calling driver write methods, the output_lock is used in
2335 * the output processing functions called here as well as in the
2336 * echo processing function to protect the column state and space
2337 * left in the buffer.
2339 * This code must be sure never to sleep through a hangup.
2341 * Locking: output_lock to protect column state and space left
2342 * (note that the process_output*() functions take this
2343 * lock themselves)
2346 static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
2347 const unsigned char *buf, size_t nr)
2349 const unsigned char *b = buf;
2350 DEFINE_WAIT_FUNC(wait, woken_wake_function);
2351 int c;
2352 ssize_t retval = 0;
2354 /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
2355 if (L_TOSTOP(tty) && file->f_op->write != redirected_tty_write) {
2356 retval = tty_check_change(tty);
2357 if (retval)
2358 return retval;
2361 down_read(&tty->termios_rwsem);
2363 /* Write out any echoed characters that are still pending */
2364 process_echoes(tty);
2366 add_wait_queue(&tty->write_wait, &wait);
2367 while (1) {
2368 if (signal_pending(current)) {
2369 retval = -ERESTARTSYS;
2370 break;
2372 if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
2373 retval = -EIO;
2374 break;
2376 if (O_OPOST(tty)) {
2377 while (nr > 0) {
2378 ssize_t num = process_output_block(tty, b, nr);
2379 if (num < 0) {
2380 if (num == -EAGAIN)
2381 break;
2382 retval = num;
2383 goto break_out;
2385 b += num;
2386 nr -= num;
2387 if (nr == 0)
2388 break;
2389 c = *b;
2390 if (process_output(c, tty) < 0)
2391 break;
2392 b++; nr--;
2394 if (tty->ops->flush_chars)
2395 tty->ops->flush_chars(tty);
2396 } else {
2397 struct n_tty_data *ldata = tty->disc_data;
2399 while (nr > 0) {
2400 mutex_lock(&ldata->output_lock);
2401 c = tty->ops->write(tty, b, nr);
2402 mutex_unlock(&ldata->output_lock);
2403 if (c < 0) {
2404 retval = c;
2405 goto break_out;
2407 if (!c)
2408 break;
2409 b += c;
2410 nr -= c;
2413 if (!nr)
2414 break;
2415 if (file->f_flags & O_NONBLOCK) {
2416 retval = -EAGAIN;
2417 break;
2419 up_read(&tty->termios_rwsem);
2421 wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
2423 down_read(&tty->termios_rwsem);
2425 break_out:
2426 remove_wait_queue(&tty->write_wait, &wait);
2427 if (b - buf != nr && tty->fasync)
2428 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
2429 up_read(&tty->termios_rwsem);
2430 return (b - buf) ? b - buf : retval;
2434 * n_tty_poll - poll method for N_TTY
2435 * @tty: terminal device
2436 * @file: file accessing it
2437 * @wait: poll table
2439 * Called when the line discipline is asked to poll() for data or
2440 * for special events. This code is not serialized with respect to
2441 * other events save open/close.
2443 * This code must be sure never to sleep through a hangup.
2444 * Called without the kernel lock held - fine
2447 static unsigned int n_tty_poll(struct tty_struct *tty, struct file *file,
2448 poll_table *wait)
2450 struct n_tty_data *ldata = tty->disc_data;
2451 unsigned int mask = 0;
2453 poll_wait(file, &tty->read_wait, wait);
2454 poll_wait(file, &tty->write_wait, wait);
2455 if (input_available_p(tty, 1))
2456 mask |= POLLIN | POLLRDNORM;
2457 else {
2458 tty_buffer_flush_work(tty->port);
2459 if (input_available_p(tty, 1))
2460 mask |= POLLIN | POLLRDNORM;
2462 if (tty->packet && tty->link->ctrl_status)
2463 mask |= POLLPRI | POLLIN | POLLRDNORM;
2464 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
2465 mask |= POLLHUP;
2466 if (tty_hung_up_p(file))
2467 mask |= POLLHUP;
2468 if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) {
2469 if (MIN_CHAR(tty) && !TIME_CHAR(tty))
2470 ldata->minimum_to_wake = MIN_CHAR(tty);
2471 else
2472 ldata->minimum_to_wake = 1;
2474 if (tty->ops->write && !tty_is_writelocked(tty) &&
2475 tty_chars_in_buffer(tty) < WAKEUP_CHARS &&
2476 tty_write_room(tty) > 0)
2477 mask |= POLLOUT | POLLWRNORM;
2478 return mask;
2481 static unsigned long inq_canon(struct n_tty_data *ldata)
2483 size_t nr, head, tail;
2485 if (ldata->canon_head == ldata->read_tail)
2486 return 0;
2487 head = ldata->canon_head;
2488 tail = ldata->read_tail;
2489 nr = head - tail;
2490 /* Skip EOF-chars.. */
2491 while (MASK(head) != MASK(tail)) {
2492 if (test_bit(tail & (N_TTY_BUF_SIZE - 1), ldata->read_flags) &&
2493 read_buf(ldata, tail) == __DISABLED_CHAR)
2494 nr--;
2495 tail++;
2497 return nr;
2500 static int n_tty_ioctl(struct tty_struct *tty, struct file *file,
2501 unsigned int cmd, unsigned long arg)
2503 struct n_tty_data *ldata = tty->disc_data;
2504 int retval;
2506 switch (cmd) {
2507 case TIOCOUTQ:
2508 return put_user(tty_chars_in_buffer(tty), (int __user *) arg);
2509 case TIOCINQ:
2510 down_write(&tty->termios_rwsem);
2511 if (L_ICANON(tty) && !L_EXTPROC(tty))
2512 retval = inq_canon(ldata);
2513 else
2514 retval = read_cnt(ldata);
2515 up_write(&tty->termios_rwsem);
2516 return put_user(retval, (unsigned int __user *) arg);
2517 default:
2518 return n_tty_ioctl_helper(tty, file, cmd, arg);
2522 static void n_tty_fasync(struct tty_struct *tty, int on)
2524 struct n_tty_data *ldata = tty->disc_data;
2526 if (!waitqueue_active(&tty->read_wait)) {
2527 if (on)
2528 ldata->minimum_to_wake = 1;
2529 else if (!tty->fasync)
2530 ldata->minimum_to_wake = N_TTY_BUF_SIZE;
2534 struct tty_ldisc_ops tty_ldisc_N_TTY = {
2535 .magic = TTY_LDISC_MAGIC,
2536 .name = "n_tty",
2537 .open = n_tty_open,
2538 .close = n_tty_close,
2539 .flush_buffer = n_tty_flush_buffer,
2540 .chars_in_buffer = n_tty_chars_in_buffer,
2541 .read = n_tty_read,
2542 .write = n_tty_write,
2543 .ioctl = n_tty_ioctl,
2544 .set_termios = n_tty_set_termios,
2545 .poll = n_tty_poll,
2546 .receive_buf = n_tty_receive_buf,
2547 .write_wakeup = n_tty_write_wakeup,
2548 .fasync = n_tty_fasync,
2549 .receive_buf2 = n_tty_receive_buf2,
2553 * n_tty_inherit_ops - inherit N_TTY methods
2554 * @ops: struct tty_ldisc_ops where to save N_TTY methods
2556 * Enables a 'subclass' line discipline to 'inherit' N_TTY
2557 * methods.
2560 void n_tty_inherit_ops(struct tty_ldisc_ops *ops)
2562 *ops = tty_ldisc_N_TTY;
2563 ops->owner = NULL;
2564 ops->refcount = ops->flags = 0;
2566 EXPORT_SYMBOL_GPL(n_tty_inherit_ops);