import PULS_20160108
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / tty / n_tty.c
CommitLineData
1da177e4
LT
1/*
2 * n_tty.c --- implements the N_TTY line discipline.
4edf1827 3 *
1da177e4
LT
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
4edf1827 11 * to N_TTY if it can not switch to any other line discipline.
1da177e4
LT
12 *
13 * Written by Theodore Ts'o, Copyright 1994.
4edf1827 14 *
1da177e4
LT
15 * This file also contains code originally written by Linus Torvalds,
16 * Copyright 1991, 1992, 1993, and by Julian Cowley, Copyright 1994.
4edf1827 17 *
1da177e4
LT
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 *
4edf1827 23 * 2000/01/20 Fixed SMP locking on put_tty_queue using bits of
1da177e4
LT
24 * the patch by Andrew J. Kroll <ag784@freenet.buffalo.edu>
25 * who actually finally proved there really was a race.
26 *
27 * 2002/03/18 Implemented n_tty_wakeup to send SIGIO POLL_OUTs to
28 * waiting writing processes-Sapan Bhatia <sapan@corewars.org>.
11a96d18 29 * Also fixed a bug in BLOCKING mode where n_tty_write returns
1da177e4
LT
30 * EAGAIN
31 */
32
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>
522ed776
MT
48#include <linux/audit.h>
49#include <linux/file.h>
300a6204 50#include <linux/uaccess.h>
572b9adb 51#include <linux/module.h>
593fb1ae 52#include <linux/ratelimit.h>
1da177e4 53
1da177e4
LT
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 */
bbd20759 64#define TTY_THRESHOLD_UNTHROTTLE 128
1da177e4 65
a88a69c9
JP
66/*
67 * Special byte codes used in the echo buffer to represent operations
68 * or special handling of characters. Bytes in the echo buffer that
69 * are not part of such special blocks are treated as normal character
70 * codes.
71 */
72#define ECHO_OP_START 0xff
73#define ECHO_OP_MOVE_BACK_COL 0x80
74#define ECHO_OP_SET_CANON_COL 0x81
75#define ECHO_OP_ERASE_TAB 0x82
76
70ece7a7 77struct n_tty_data {
53c5ee2c
JS
78 unsigned int column;
79 unsigned long overrun_time;
80 int num_overrun;
81
82 unsigned char lnext:1, erasing:1, raw:1, real_raw:1, icanon:1;
83 unsigned char echo_overrun:1;
3fe780b3
JS
84
85 DECLARE_BITMAP(process_char_map, 256);
86 DECLARE_BITMAP(read_flags, N_TTY_BUF_SIZE);
ba2e68ac
JS
87
88 char *read_buf;
89 int read_head;
90 int read_tail;
91 int read_cnt;
92
93 unsigned char *echo_buf;
94 unsigned int echo_pos;
95 unsigned int echo_cnt;
96
97 int canon_data;
98 unsigned long canon_head;
99 unsigned int canon_column;
bddc7152
JS
100
101 struct mutex atomic_read_lock;
102 struct mutex output_lock;
103 struct mutex echo_lock;
98001214 104 raw_spinlock_t read_lock;
70ece7a7
JS
105};
106
522ed776
MT
107static inline int tty_put_user(struct tty_struct *tty, unsigned char x,
108 unsigned char __user *ptr)
109{
53c5ee2c
JS
110 struct n_tty_data *ldata = tty->disc_data;
111
112 tty_audit_add_data(tty, &x, 1, ldata->icanon);
522ed776
MT
113 return put_user(x, ptr);
114}
115
55db4c64 116/**
6fa3eb70 117 * n_tty_set_room - receive space
55db4c64
LT
118 * @tty: terminal
119 *
6fa3eb70
S
120 * Sets tty->receive_room to reflect the currently available space
121 * in the input buffer, and re-schedules the flip buffer work if space
122 * just became available.
123 *
124 * Locks: Concurrent update is protected with read_lock
55db4c64
LT
125 */
126
127static void n_tty_set_room(struct tty_struct *tty)
128{
53c5ee2c 129 struct n_tty_data *ldata = tty->disc_data;
090abf7b 130 int left;
55db4c64 131 int old_left;
6fa3eb70
S
132 unsigned long flags;
133
134 raw_spin_lock_irqsave(&ldata->read_lock, flags);
55db4c64 135
090abf7b
JA
136 if (I_PARMRK(tty)) {
137 /* Multiply read_cnt by 3, since each byte might take up to
138 * three times as many spaces when PARMRK is set (depending on
139 * its flags, e.g. parity error). */
ba2e68ac 140 left = N_TTY_BUF_SIZE - ldata->read_cnt * 3 - 1;
090abf7b 141 } else
ba2e68ac 142 left = N_TTY_BUF_SIZE - ldata->read_cnt - 1;
090abf7b 143
55db4c64
LT
144 /*
145 * If we are doing input canonicalization, and there are no
146 * pending newlines, let characters through without limit, so
147 * that erase characters will be handled. Other excess
148 * characters will be beeped.
149 */
150 if (left <= 0)
ba2e68ac 151 left = ldata->icanon && !ldata->canon_data;
55db4c64
LT
152 old_left = tty->receive_room;
153 tty->receive_room = left;
154
6fa3eb70
S
155 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
156
55db4c64 157 /* Did this open up the receive buffer? We may need to flip */
ecbbfd44
JS
158 if (left && !old_left) {
159 WARN_RATELIMIT(tty->port->itty == NULL,
cadf7486 160 "scheduling with invalid itty\n");
21622939
PH
161 /* see if ldisc has been killed - if so, this means that
162 * even though the ldisc has been halted and ->buf.work
163 * cancelled, ->buf.work is about to be rescheduled
164 */
165 WARN_RATELIMIT(test_bit(TTY_LDISC_HALTED, &tty->flags),
166 "scheduling buffer work for halted ldisc\n");
ecbbfd44
JS
167 schedule_work(&tty->port->buf.work);
168 }
55db4c64
LT
169}
170
57c94121 171static void put_tty_queue_nolock(unsigned char c, struct n_tty_data *ldata)
1da177e4 172{
ba2e68ac
JS
173 if (ldata->read_cnt < N_TTY_BUF_SIZE) {
174 ldata->read_buf[ldata->read_head] = c;
175 ldata->read_head = (ldata->read_head + 1) & (N_TTY_BUF_SIZE-1);
176 ldata->read_cnt++;
1da177e4
LT
177 }
178}
179
17b82060
AC
180/**
181 * put_tty_queue - add character to tty
182 * @c: character
57c94121 183 * @ldata: n_tty data
17b82060
AC
184 *
185 * Add a character to the tty read_buf queue. This is done under the
186 * read_lock to serialize character addition and also to protect us
187 * against parallel reads or flushes
188 */
189
57c94121 190static void put_tty_queue(unsigned char c, struct n_tty_data *ldata)
1da177e4
LT
191{
192 unsigned long flags;
193 /*
194 * The problem of stomping on the buffers ends here.
195 * Why didn't anyone see this one coming? --AJK
196 */
98001214 197 raw_spin_lock_irqsave(&ldata->read_lock, flags);
57c94121 198 put_tty_queue_nolock(c, ldata);
98001214 199 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
1da177e4
LT
200}
201
1da177e4
LT
202/**
203 * reset_buffer_flags - reset buffer state
204 * @tty: terminal to reset
205 *
25518c68
PH
206 * Reset the read buffer counters and clear the flags.
207 * Called from n_tty_open() and n_tty_flush_buffer().
17b82060
AC
208 *
209 * Locking: tty_read_lock for read fields.
1da177e4 210 */
a88a69c9 211
b66f4fa5 212static void reset_buffer_flags(struct n_tty_data *ldata)
1da177e4
LT
213{
214 unsigned long flags;
215
98001214 216 raw_spin_lock_irqsave(&ldata->read_lock, flags);
ba2e68ac 217 ldata->read_head = ldata->read_tail = ldata->read_cnt = 0;
98001214 218 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
a88a69c9 219
bddc7152 220 mutex_lock(&ldata->echo_lock);
ba2e68ac 221 ldata->echo_pos = ldata->echo_cnt = ldata->echo_overrun = 0;
bddc7152 222 mutex_unlock(&ldata->echo_lock);
a88a69c9 223
ba2e68ac 224 ldata->canon_head = ldata->canon_data = ldata->erasing = 0;
3fe780b3 225 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
1da177e4
LT
226}
227
a30737ab
PH
228static void n_tty_packet_mode_flush(struct tty_struct *tty)
229{
230 unsigned long flags;
231
232 spin_lock_irqsave(&tty->ctrl_lock, flags);
233 if (tty->link->packet) {
234 tty->ctrl_status |= TIOCPKT_FLUSHREAD;
235 wake_up_interruptible(&tty->link->read_wait);
236 }
237 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
238}
239
1da177e4
LT
240/**
241 * n_tty_flush_buffer - clean input queue
242 * @tty: terminal device
243 *
25518c68
PH
244 * Flush the input buffer. Called when the tty layer wants the
245 * buffer flushed (eg at hangup) or when the N_TTY line discipline
246 * internally has to clean the pending queue (for example some signals).
1da177e4 247 *
17b82060 248 * Locking: ctrl_lock, read_lock.
1da177e4 249 */
4edf1827
AC
250
251static void n_tty_flush_buffer(struct tty_struct *tty)
1da177e4 252{
b66f4fa5
PH
253 reset_buffer_flags(tty->disc_data);
254 n_tty_set_room(tty);
4edf1827 255
a30737ab
PH
256 if (tty->link)
257 n_tty_packet_mode_flush(tty);
1da177e4
LT
258}
259
260/**
261 * n_tty_chars_in_buffer - report available bytes
262 * @tty: tty device
263 *
264 * Report the number of characters buffered to be delivered to user
4edf1827 265 * at this instant in time.
17b82060
AC
266 *
267 * Locking: read_lock
1da177e4 268 */
4edf1827 269
1da177e4
LT
270static ssize_t n_tty_chars_in_buffer(struct tty_struct *tty)
271{
53c5ee2c 272 struct n_tty_data *ldata = tty->disc_data;
1da177e4
LT
273 unsigned long flags;
274 ssize_t n = 0;
275
98001214 276 raw_spin_lock_irqsave(&ldata->read_lock, flags);
53c5ee2c 277 if (!ldata->icanon) {
ba2e68ac
JS
278 n = ldata->read_cnt;
279 } else if (ldata->canon_data) {
280 n = (ldata->canon_head > ldata->read_tail) ?
281 ldata->canon_head - ldata->read_tail :
282 ldata->canon_head + (N_TTY_BUF_SIZE - ldata->read_tail);
1da177e4 283 }
98001214 284 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
1da177e4
LT
285 return n;
286}
287
288/**
289 * is_utf8_continuation - utf8 multibyte check
290 * @c: byte to check
291 *
292 * Returns true if the utf8 character 'c' is a multibyte continuation
293 * character. We use this to correctly compute the on screen size
294 * of the character when printing
295 */
4edf1827 296
1da177e4
LT
297static inline int is_utf8_continuation(unsigned char c)
298{
299 return (c & 0xc0) == 0x80;
300}
301
302/**
303 * is_continuation - multibyte check
304 * @c: byte to check
305 *
306 * Returns true if the utf8 character 'c' is a multibyte continuation
307 * character and the terminal is in unicode mode.
308 */
4edf1827 309
1da177e4
LT
310static inline int is_continuation(unsigned char c, struct tty_struct *tty)
311{
312 return I_IUTF8(tty) && is_utf8_continuation(c);
313}
314
315/**
a88a69c9 316 * do_output_char - output one character
1da177e4
LT
317 * @c: character (or partial unicode symbol)
318 * @tty: terminal device
a88a69c9 319 * @space: space available in tty driver write buffer
1da177e4 320 *
a88a69c9
JP
321 * This is a helper function that handles one output character
322 * (including special characters like TAB, CR, LF, etc.),
ee5aa7b8
JP
323 * doing OPOST processing and putting the results in the
324 * tty driver's write buffer.
a88a69c9
JP
325 *
326 * Note that Linux currently ignores TABDLY, CRDLY, VTDLY, FFDLY
327 * and NLDLY. They simply aren't relevant in the world today.
328 * If you ever need them, add them here.
1da177e4 329 *
a88a69c9
JP
330 * Returns the number of bytes of buffer space used or -1 if
331 * no space left.
332 *
333 * Locking: should be called under the output_lock to protect
334 * the column state and space left in the buffer
1da177e4 335 */
4edf1827 336
a88a69c9 337static int do_output_char(unsigned char c, struct tty_struct *tty, int space)
1da177e4 338{
53c5ee2c 339 struct n_tty_data *ldata = tty->disc_data;
a88a69c9 340 int spaces;
1da177e4 341
1da177e4
LT
342 if (!space)
343 return -1;
300a6204 344
a88a69c9
JP
345 switch (c) {
346 case '\n':
347 if (O_ONLRET(tty))
53c5ee2c 348 ldata->column = 0;
a88a69c9
JP
349 if (O_ONLCR(tty)) {
350 if (space < 2)
351 return -1;
ba2e68ac 352 ldata->canon_column = ldata->column = 0;
37f81fa1 353 tty->ops->write(tty, "\r\n", 2);
a88a69c9
JP
354 return 2;
355 }
ba2e68ac 356 ldata->canon_column = ldata->column;
a88a69c9
JP
357 break;
358 case '\r':
53c5ee2c 359 if (O_ONOCR(tty) && ldata->column == 0)
a88a69c9
JP
360 return 0;
361 if (O_OCRNL(tty)) {
362 c = '\n';
363 if (O_ONLRET(tty))
ba2e68ac 364 ldata->canon_column = ldata->column = 0;
1da177e4 365 break;
a88a69c9 366 }
ba2e68ac 367 ldata->canon_column = ldata->column = 0;
a88a69c9
JP
368 break;
369 case '\t':
53c5ee2c 370 spaces = 8 - (ldata->column & 7);
a88a69c9
JP
371 if (O_TABDLY(tty) == XTABS) {
372 if (space < spaces)
373 return -1;
53c5ee2c 374 ldata->column += spaces;
a88a69c9
JP
375 tty->ops->write(tty, " ", spaces);
376 return spaces;
1da177e4 377 }
53c5ee2c 378 ldata->column += spaces;
a88a69c9
JP
379 break;
380 case '\b':
53c5ee2c
JS
381 if (ldata->column > 0)
382 ldata->column--;
a88a69c9
JP
383 break;
384 default:
a59c0d6f
JP
385 if (!iscntrl(c)) {
386 if (O_OLCUC(tty))
387 c = toupper(c);
388 if (!is_continuation(c, tty))
53c5ee2c 389 ldata->column++;
a59c0d6f 390 }
a88a69c9 391 break;
1da177e4 392 }
a88a69c9 393
f34d7a5b 394 tty_put_char(tty, c);
a88a69c9
JP
395 return 1;
396}
397
398/**
399 * process_output - output post processor
400 * @c: character (or partial unicode symbol)
401 * @tty: terminal device
402 *
ee5aa7b8
JP
403 * Output one character with OPOST processing.
404 * Returns -1 when the output device is full and the character
405 * must be retried.
a88a69c9
JP
406 *
407 * Locking: output_lock to protect column state and space left
408 * (also, this is called from n_tty_write under the
409 * tty layer write lock)
410 */
411
412static int process_output(unsigned char c, struct tty_struct *tty)
413{
bddc7152 414 struct n_tty_data *ldata = tty->disc_data;
a88a69c9
JP
415 int space, retval;
416
bddc7152 417 mutex_lock(&ldata->output_lock);
a88a69c9
JP
418
419 space = tty_write_room(tty);
420 retval = do_output_char(c, tty, space);
421
bddc7152 422 mutex_unlock(&ldata->output_lock);
a88a69c9
JP
423 if (retval < 0)
424 return -1;
425 else
426 return 0;
1da177e4
LT
427}
428
429/**
a88a69c9 430 * process_output_block - block post processor
1da177e4 431 * @tty: terminal device
ee5aa7b8
JP
432 * @buf: character buffer
433 * @nr: number of bytes to output
434 *
435 * Output a block of characters with OPOST processing.
436 * Returns the number of characters output.
1da177e4
LT
437 *
438 * This path is used to speed up block console writes, among other
439 * things when processing blocks of output data. It handles only
440 * the simple cases normally found and helps to generate blocks of
441 * symbols for the console driver and thus improve performance.
442 *
a88a69c9
JP
443 * Locking: output_lock to protect column state and space left
444 * (also, this is called from n_tty_write under the
445 * tty layer write lock)
1da177e4 446 */
4edf1827 447
a88a69c9
JP
448static ssize_t process_output_block(struct tty_struct *tty,
449 const unsigned char *buf, unsigned int nr)
1da177e4 450{
53c5ee2c 451 struct n_tty_data *ldata = tty->disc_data;
1da177e4 452 int space;
bbd20759 453 int i;
1da177e4
LT
454 const unsigned char *cp;
455
bddc7152 456 mutex_lock(&ldata->output_lock);
a88a69c9 457
f34d7a5b 458 space = tty_write_room(tty);
300a6204 459 if (!space) {
bddc7152 460 mutex_unlock(&ldata->output_lock);
1da177e4 461 return 0;
a88a69c9 462 }
1da177e4
LT
463 if (nr > space)
464 nr = space;
465
466 for (i = 0, cp = buf; i < nr; i++, cp++) {
a59c0d6f
JP
467 unsigned char c = *cp;
468
469 switch (c) {
1da177e4
LT
470 case '\n':
471 if (O_ONLRET(tty))
53c5ee2c 472 ldata->column = 0;
1da177e4
LT
473 if (O_ONLCR(tty))
474 goto break_out;
ba2e68ac 475 ldata->canon_column = ldata->column;
1da177e4
LT
476 break;
477 case '\r':
53c5ee2c 478 if (O_ONOCR(tty) && ldata->column == 0)
1da177e4
LT
479 goto break_out;
480 if (O_OCRNL(tty))
481 goto break_out;
ba2e68ac 482 ldata->canon_column = ldata->column = 0;
1da177e4
LT
483 break;
484 case '\t':
485 goto break_out;
486 case '\b':
53c5ee2c
JS
487 if (ldata->column > 0)
488 ldata->column--;
1da177e4
LT
489 break;
490 default:
a59c0d6f
JP
491 if (!iscntrl(c)) {
492 if (O_OLCUC(tty))
493 goto break_out;
494 if (!is_continuation(c, tty))
53c5ee2c 495 ldata->column++;
a59c0d6f 496 }
1da177e4
LT
497 break;
498 }
499 }
500break_out:
f34d7a5b 501 i = tty->ops->write(tty, buf, i);
a88a69c9 502
bddc7152 503 mutex_unlock(&ldata->output_lock);
1da177e4
LT
504 return i;
505}
506
a88a69c9
JP
507/**
508 * process_echoes - write pending echo characters
509 * @tty: terminal device
510 *
511 * Write previously buffered echo (and other ldisc-generated)
512 * characters to the tty.
513 *
514 * Characters generated by the ldisc (including echoes) need to
515 * be buffered because the driver's write buffer can fill during
516 * heavy program output. Echoing straight to the driver will
517 * often fail under these conditions, causing lost characters and
518 * resulting mismatches of ldisc state information.
519 *
520 * Since the ldisc state must represent the characters actually sent
521 * to the driver at the time of the write, operations like certain
522 * changes in column state are also saved in the buffer and executed
523 * here.
524 *
525 * A circular fifo buffer is used so that the most recent characters
526 * are prioritized. Also, when control characters are echoed with a
527 * prefixed "^", the pair is treated atomically and thus not separated.
528 *
529 * Locking: output_lock to protect column state and space left,
530 * echo_lock to protect the echo buffer
531 */
532
533static void process_echoes(struct tty_struct *tty)
534{
53c5ee2c 535 struct n_tty_data *ldata = tty->disc_data;
a88a69c9
JP
536 int space, nr;
537 unsigned char c;
538 unsigned char *cp, *buf_end;
539
ba2e68ac 540 if (!ldata->echo_cnt)
a88a69c9
JP
541 return;
542
bddc7152
JS
543 mutex_lock(&ldata->output_lock);
544 mutex_lock(&ldata->echo_lock);
a88a69c9
JP
545
546 space = tty_write_room(tty);
547
ba2e68ac
JS
548 buf_end = ldata->echo_buf + N_TTY_BUF_SIZE;
549 cp = ldata->echo_buf + ldata->echo_pos;
550 nr = ldata->echo_cnt;
a88a69c9
JP
551 while (nr > 0) {
552 c = *cp;
553 if (c == ECHO_OP_START) {
554 unsigned char op;
555 unsigned char *opp;
556 int no_space_left = 0;
557
558 /*
559 * If the buffer byte is the start of a multi-byte
560 * operation, get the next byte, which is either the
561 * op code or a control character value.
562 */
563 opp = cp + 1;
564 if (opp == buf_end)
565 opp -= N_TTY_BUF_SIZE;
566 op = *opp;
300a6204 567
a88a69c9
JP
568 switch (op) {
569 unsigned int num_chars, num_bs;
570
571 case ECHO_OP_ERASE_TAB:
572 if (++opp == buf_end)
573 opp -= N_TTY_BUF_SIZE;
574 num_chars = *opp;
575
576 /*
577 * Determine how many columns to go back
578 * in order to erase the tab.
579 * This depends on the number of columns
580 * used by other characters within the tab
581 * area. If this (modulo 8) count is from
582 * the start of input rather than from a
583 * previous tab, we offset by canon column.
584 * Otherwise, tab spacing is normal.
585 */
586 if (!(num_chars & 0x80))
ba2e68ac 587 num_chars += ldata->canon_column;
a88a69c9
JP
588 num_bs = 8 - (num_chars & 7);
589
590 if (num_bs > space) {
591 no_space_left = 1;
592 break;
593 }
594 space -= num_bs;
595 while (num_bs--) {
596 tty_put_char(tty, '\b');
53c5ee2c
JS
597 if (ldata->column > 0)
598 ldata->column--;
a88a69c9
JP
599 }
600 cp += 3;
601 nr -= 3;
602 break;
603
604 case ECHO_OP_SET_CANON_COL:
ba2e68ac 605 ldata->canon_column = ldata->column;
a88a69c9
JP
606 cp += 2;
607 nr -= 2;
608 break;
609
610 case ECHO_OP_MOVE_BACK_COL:
53c5ee2c
JS
611 if (ldata->column > 0)
612 ldata->column--;
a88a69c9
JP
613 cp += 2;
614 nr -= 2;
615 break;
616
617 case ECHO_OP_START:
618 /* This is an escaped echo op start code */
619 if (!space) {
620 no_space_left = 1;
621 break;
622 }
623 tty_put_char(tty, ECHO_OP_START);
53c5ee2c 624 ldata->column++;
a88a69c9
JP
625 space--;
626 cp += 2;
627 nr -= 2;
628 break;
629
630 default:
a88a69c9 631 /*
62b26358
JP
632 * If the op is not a special byte code,
633 * it is a ctrl char tagged to be echoed
634 * as "^X" (where X is the letter
635 * representing the control char).
636 * Note that we must ensure there is
637 * enough space for the whole ctrl pair.
638 *
a88a69c9 639 */
62b26358
JP
640 if (space < 2) {
641 no_space_left = 1;
642 break;
643 }
644 tty_put_char(tty, '^');
645 tty_put_char(tty, op ^ 0100);
53c5ee2c 646 ldata->column += 2;
62b26358 647 space -= 2;
a88a69c9
JP
648 cp += 2;
649 nr -= 2;
650 }
651
652 if (no_space_left)
653 break;
654 } else {
ee5aa7b8
JP
655 if (O_OPOST(tty) &&
656 !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
657 int retval = do_output_char(c, tty, space);
658 if (retval < 0)
659 break;
660 space -= retval;
661 } else {
662 if (!space)
663 break;
664 tty_put_char(tty, c);
665 space -= 1;
666 }
a88a69c9
JP
667 cp += 1;
668 nr -= 1;
669 }
670
671 /* When end of circular buffer reached, wrap around */
672 if (cp >= buf_end)
673 cp -= N_TTY_BUF_SIZE;
674 }
675
676 if (nr == 0) {
ba2e68ac
JS
677 ldata->echo_pos = 0;
678 ldata->echo_cnt = 0;
53c5ee2c 679 ldata->echo_overrun = 0;
a88a69c9 680 } else {
ba2e68ac
JS
681 int num_processed = ldata->echo_cnt - nr;
682 ldata->echo_pos += num_processed;
683 ldata->echo_pos &= N_TTY_BUF_SIZE - 1;
684 ldata->echo_cnt = nr;
a88a69c9 685 if (num_processed > 0)
53c5ee2c 686 ldata->echo_overrun = 0;
a88a69c9
JP
687 }
688
bddc7152
JS
689 mutex_unlock(&ldata->echo_lock);
690 mutex_unlock(&ldata->output_lock);
a88a69c9
JP
691
692 if (tty->ops->flush_chars)
693 tty->ops->flush_chars(tty);
694}
695
696/**
697 * add_echo_byte - add a byte to the echo buffer
698 * @c: unicode byte to echo
57c94121 699 * @ldata: n_tty data
a88a69c9
JP
700 *
701 * Add a character or operation byte to the echo buffer.
702 *
703 * Should be called under the echo lock to protect the echo buffer.
704 */
705
57c94121 706static void add_echo_byte(unsigned char c, struct n_tty_data *ldata)
a88a69c9
JP
707{
708 int new_byte_pos;
709
ba2e68ac 710 if (ldata->echo_cnt == N_TTY_BUF_SIZE) {
a88a69c9 711 /* Circular buffer is already at capacity */
ba2e68ac 712 new_byte_pos = ldata->echo_pos;
a88a69c9
JP
713
714 /*
715 * Since the buffer start position needs to be advanced,
716 * be sure to step by a whole operation byte group.
717 */
ba2e68ac
JS
718 if (ldata->echo_buf[ldata->echo_pos] == ECHO_OP_START) {
719 if (ldata->echo_buf[(ldata->echo_pos + 1) &
a88a69c9
JP
720 (N_TTY_BUF_SIZE - 1)] ==
721 ECHO_OP_ERASE_TAB) {
ba2e68ac
JS
722 ldata->echo_pos += 3;
723 ldata->echo_cnt -= 2;
a88a69c9 724 } else {
ba2e68ac
JS
725 ldata->echo_pos += 2;
726 ldata->echo_cnt -= 1;
a88a69c9
JP
727 }
728 } else {
ba2e68ac 729 ldata->echo_pos++;
a88a69c9 730 }
ba2e68ac 731 ldata->echo_pos &= N_TTY_BUF_SIZE - 1;
a88a69c9 732
53c5ee2c 733 ldata->echo_overrun = 1;
a88a69c9 734 } else {
ba2e68ac 735 new_byte_pos = ldata->echo_pos + ldata->echo_cnt;
a88a69c9 736 new_byte_pos &= N_TTY_BUF_SIZE - 1;
ba2e68ac 737 ldata->echo_cnt++;
a88a69c9
JP
738 }
739
ba2e68ac 740 ldata->echo_buf[new_byte_pos] = c;
a88a69c9
JP
741}
742
743/**
744 * echo_move_back_col - add operation to move back a column
57c94121 745 * @ldata: n_tty data
a88a69c9
JP
746 *
747 * Add an operation to the echo buffer to move back one column.
748 *
749 * Locking: echo_lock to protect the echo buffer
750 */
751
57c94121 752static void echo_move_back_col(struct n_tty_data *ldata)
a88a69c9 753{
bddc7152 754 mutex_lock(&ldata->echo_lock);
57c94121
JS
755 add_echo_byte(ECHO_OP_START, ldata);
756 add_echo_byte(ECHO_OP_MOVE_BACK_COL, ldata);
bddc7152 757 mutex_unlock(&ldata->echo_lock);
a88a69c9
JP
758}
759
760/**
761 * echo_set_canon_col - add operation to set the canon column
57c94121 762 * @ldata: n_tty data
a88a69c9
JP
763 *
764 * Add an operation to the echo buffer to set the canon column
765 * to the current column.
766 *
767 * Locking: echo_lock to protect the echo buffer
768 */
769
57c94121 770static void echo_set_canon_col(struct n_tty_data *ldata)
a88a69c9 771{
bddc7152 772 mutex_lock(&ldata->echo_lock);
57c94121
JS
773 add_echo_byte(ECHO_OP_START, ldata);
774 add_echo_byte(ECHO_OP_SET_CANON_COL, ldata);
bddc7152 775 mutex_unlock(&ldata->echo_lock);
a88a69c9
JP
776}
777
778/**
779 * echo_erase_tab - add operation to erase a tab
780 * @num_chars: number of character columns already used
781 * @after_tab: true if num_chars starts after a previous tab
57c94121 782 * @ldata: n_tty data
a88a69c9
JP
783 *
784 * Add an operation to the echo buffer to erase a tab.
785 *
786 * Called by the eraser function, which knows how many character
787 * columns have been used since either a previous tab or the start
788 * of input. This information will be used later, along with
789 * canon column (if applicable), to go back the correct number
790 * of columns.
791 *
792 * Locking: echo_lock to protect the echo buffer
793 */
794
795static void echo_erase_tab(unsigned int num_chars, int after_tab,
57c94121 796 struct n_tty_data *ldata)
a88a69c9 797{
bddc7152 798 mutex_lock(&ldata->echo_lock);
a88a69c9 799
57c94121
JS
800 add_echo_byte(ECHO_OP_START, ldata);
801 add_echo_byte(ECHO_OP_ERASE_TAB, ldata);
a88a69c9
JP
802
803 /* We only need to know this modulo 8 (tab spacing) */
804 num_chars &= 7;
805
806 /* Set the high bit as a flag if num_chars is after a previous tab */
807 if (after_tab)
808 num_chars |= 0x80;
300a6204 809
57c94121 810 add_echo_byte(num_chars, ldata);
a88a69c9 811
bddc7152 812 mutex_unlock(&ldata->echo_lock);
a88a69c9
JP
813}
814
815/**
816 * echo_char_raw - echo a character raw
817 * @c: unicode byte to echo
818 * @tty: terminal device
819 *
820 * Echo user input back onto the screen. This must be called only when
821 * L_ECHO(tty) is true. Called from the driver receive_buf path.
822 *
823 * This variant does not treat control characters specially.
824 *
825 * Locking: echo_lock to protect the echo buffer
826 */
827
57c94121 828static void echo_char_raw(unsigned char c, struct n_tty_data *ldata)
a88a69c9 829{
bddc7152 830 mutex_lock(&ldata->echo_lock);
a88a69c9 831 if (c == ECHO_OP_START) {
57c94121
JS
832 add_echo_byte(ECHO_OP_START, ldata);
833 add_echo_byte(ECHO_OP_START, ldata);
a88a69c9 834 } else {
57c94121 835 add_echo_byte(c, ldata);
a88a69c9 836 }
bddc7152 837 mutex_unlock(&ldata->echo_lock);
a88a69c9 838}
1da177e4 839
1da177e4 840/**
a88a69c9 841 * echo_char - echo a character
1da177e4
LT
842 * @c: unicode byte to echo
843 * @tty: terminal device
844 *
4edf1827 845 * Echo user input back onto the screen. This must be called only when
1da177e4 846 * L_ECHO(tty) is true. Called from the driver receive_buf path.
17b82060 847 *
62b26358
JP
848 * This variant tags control characters to be echoed as "^X"
849 * (where X is the letter representing the control char).
a88a69c9
JP
850 *
851 * Locking: echo_lock to protect the echo buffer
1da177e4
LT
852 */
853
854static void echo_char(unsigned char c, struct tty_struct *tty)
855{
bddc7152
JS
856 struct n_tty_data *ldata = tty->disc_data;
857
858 mutex_lock(&ldata->echo_lock);
a88a69c9
JP
859
860 if (c == ECHO_OP_START) {
57c94121
JS
861 add_echo_byte(ECHO_OP_START, ldata);
862 add_echo_byte(ECHO_OP_START, ldata);
a88a69c9 863 } else {
62b26358 864 if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t')
57c94121
JS
865 add_echo_byte(ECHO_OP_START, ldata);
866 add_echo_byte(c, ldata);
a88a69c9
JP
867 }
868
bddc7152 869 mutex_unlock(&ldata->echo_lock);
1da177e4
LT
870}
871
17b82060 872/**
a88a69c9 873 * finish_erasing - complete erase
57c94121 874 * @ldata: n_tty data
17b82060 875 */
a88a69c9 876
57c94121 877static inline void finish_erasing(struct n_tty_data *ldata)
1da177e4 878{
53c5ee2c 879 if (ldata->erasing) {
57c94121 880 echo_char_raw('/', ldata);
53c5ee2c 881 ldata->erasing = 0;
1da177e4
LT
882 }
883}
884
885/**
886 * eraser - handle erase function
887 * @c: character input
888 * @tty: terminal device
889 *
3a4fa0a2 890 * Perform erase and necessary output when an erase character is
1da177e4
LT
891 * present in the stream from the driver layer. Handles the complexities
892 * of UTF-8 multibyte symbols.
17b82060 893 *
a88a69c9 894 * Locking: read_lock for tty buffers
1da177e4 895 */
4edf1827 896
1da177e4
LT
897static void eraser(unsigned char c, struct tty_struct *tty)
898{
53c5ee2c 899 struct n_tty_data *ldata = tty->disc_data;
1da177e4
LT
900 enum { ERASE, WERASE, KILL } kill_type;
901 int head, seen_alnums, cnt;
902 unsigned long flags;
903
17b82060 904 /* FIXME: locking needed ? */
ba2e68ac 905 if (ldata->read_head == ldata->canon_head) {
7e94b1d9 906 /* process_output('\a', tty); */ /* what do you think? */
1da177e4
LT
907 return;
908 }
909 if (c == ERASE_CHAR(tty))
910 kill_type = ERASE;
911 else if (c == WERASE_CHAR(tty))
912 kill_type = WERASE;
913 else {
914 if (!L_ECHO(tty)) {
98001214 915 raw_spin_lock_irqsave(&ldata->read_lock, flags);
ba2e68ac 916 ldata->read_cnt -= ((ldata->read_head - ldata->canon_head) &
1da177e4 917 (N_TTY_BUF_SIZE - 1));
ba2e68ac 918 ldata->read_head = ldata->canon_head;
98001214 919 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
1da177e4
LT
920 return;
921 }
922 if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
98001214 923 raw_spin_lock_irqsave(&ldata->read_lock, flags);
ba2e68ac 924 ldata->read_cnt -= ((ldata->read_head - ldata->canon_head) &
1da177e4 925 (N_TTY_BUF_SIZE - 1));
ba2e68ac 926 ldata->read_head = ldata->canon_head;
98001214 927 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
57c94121 928 finish_erasing(ldata);
1da177e4
LT
929 echo_char(KILL_CHAR(tty), tty);
930 /* Add a newline if ECHOK is on and ECHOKE is off. */
931 if (L_ECHOK(tty))
57c94121 932 echo_char_raw('\n', ldata);
1da177e4
LT
933 return;
934 }
935 kill_type = KILL;
936 }
937
938 seen_alnums = 0;
17b82060 939 /* FIXME: Locking ?? */
ba2e68ac
JS
940 while (ldata->read_head != ldata->canon_head) {
941 head = ldata->read_head;
1da177e4
LT
942
943 /* erase a single possibly multibyte character */
944 do {
945 head = (head - 1) & (N_TTY_BUF_SIZE-1);
ba2e68ac
JS
946 c = ldata->read_buf[head];
947 } while (is_continuation(c, tty) && head != ldata->canon_head);
1da177e4
LT
948
949 /* do not partially erase */
950 if (is_continuation(c, tty))
951 break;
952
953 if (kill_type == WERASE) {
954 /* Equivalent to BSD's ALTWERASE. */
955 if (isalnum(c) || c == '_')
956 seen_alnums++;
957 else if (seen_alnums)
958 break;
959 }
ba2e68ac 960 cnt = (ldata->read_head - head) & (N_TTY_BUF_SIZE-1);
98001214 961 raw_spin_lock_irqsave(&ldata->read_lock, flags);
ba2e68ac
JS
962 ldata->read_head = head;
963 ldata->read_cnt -= cnt;
98001214 964 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
1da177e4
LT
965 if (L_ECHO(tty)) {
966 if (L_ECHOPRT(tty)) {
53c5ee2c 967 if (!ldata->erasing) {
57c94121 968 echo_char_raw('\\', ldata);
53c5ee2c 969 ldata->erasing = 1;
1da177e4
LT
970 }
971 /* if cnt > 1, output a multi-byte character */
972 echo_char(c, tty);
973 while (--cnt > 0) {
974 head = (head+1) & (N_TTY_BUF_SIZE-1);
57c94121
JS
975 echo_char_raw(ldata->read_buf[head],
976 ldata);
977 echo_move_back_col(ldata);
1da177e4
LT
978 }
979 } else if (kill_type == ERASE && !L_ECHOE(tty)) {
980 echo_char(ERASE_CHAR(tty), tty);
981 } else if (c == '\t') {
a88a69c9
JP
982 unsigned int num_chars = 0;
983 int after_tab = 0;
ba2e68ac 984 unsigned long tail = ldata->read_head;
a88a69c9
JP
985
986 /*
987 * Count the columns used for characters
988 * since the start of input or after a
989 * previous tab.
990 * This info is used to go back the correct
991 * number of columns.
992 */
ba2e68ac 993 while (tail != ldata->canon_head) {
a88a69c9 994 tail = (tail-1) & (N_TTY_BUF_SIZE-1);
ba2e68ac 995 c = ldata->read_buf[tail];
a88a69c9
JP
996 if (c == '\t') {
997 after_tab = 1;
998 break;
300a6204 999 } else if (iscntrl(c)) {
1da177e4 1000 if (L_ECHOCTL(tty))
a88a69c9
JP
1001 num_chars += 2;
1002 } else if (!is_continuation(c, tty)) {
1003 num_chars++;
1004 }
1da177e4 1005 }
57c94121 1006 echo_erase_tab(num_chars, after_tab, ldata);
1da177e4
LT
1007 } else {
1008 if (iscntrl(c) && L_ECHOCTL(tty)) {
57c94121
JS
1009 echo_char_raw('\b', ldata);
1010 echo_char_raw(' ', ldata);
1011 echo_char_raw('\b', ldata);
1da177e4
LT
1012 }
1013 if (!iscntrl(c) || L_ECHOCTL(tty)) {
57c94121
JS
1014 echo_char_raw('\b', ldata);
1015 echo_char_raw(' ', ldata);
1016 echo_char_raw('\b', ldata);
1da177e4
LT
1017 }
1018 }
1019 }
1020 if (kill_type == ERASE)
1021 break;
1022 }
ba2e68ac 1023 if (ldata->read_head == ldata->canon_head && L_ECHO(tty))
57c94121 1024 finish_erasing(ldata);
1da177e4
LT
1025}
1026
1027/**
1028 * isig - handle the ISIG optio
1029 * @sig: signal
1030 * @tty: terminal
1da177e4 1031 *
8c985d18
PH
1032 * Called when a signal is being sent due to terminal input.
1033 * Called from the driver receive_buf path so serialized.
17b82060 1034 *
8c985d18 1035 * Locking: ctrl_lock
1da177e4 1036 */
4edf1827 1037
8c985d18 1038static inline void isig(int sig, struct tty_struct *tty)
1da177e4 1039{
8c985d18
PH
1040 struct pid *tty_pgrp = tty_get_pgrp(tty);
1041 if (tty_pgrp) {
1042 kill_pgrp(tty_pgrp, sig, 1);
1043 put_pid(tty_pgrp);
1da177e4
LT
1044 }
1045}
1046
1047/**
1048 * n_tty_receive_break - handle break
1049 * @tty: terminal
1050 *
1051 * An RS232 break event has been hit in the incoming bitstream. This
1052 * can cause a variety of events depending upon the termios settings.
1053 *
1054 * Called from the receive_buf path so single threaded.
1055 */
4edf1827 1056
1da177e4
LT
1057static inline void n_tty_receive_break(struct tty_struct *tty)
1058{
57c94121
JS
1059 struct n_tty_data *ldata = tty->disc_data;
1060
1da177e4
LT
1061 if (I_IGNBRK(tty))
1062 return;
1063 if (I_BRKINT(tty)) {
8c985d18
PH
1064 isig(SIGINT, tty);
1065 if (!L_NOFLSH(tty)) {
1066 n_tty_flush_buffer(tty);
1067 tty_driver_flush_buffer(tty);
1068 }
1da177e4
LT
1069 return;
1070 }
1071 if (I_PARMRK(tty)) {
57c94121
JS
1072 put_tty_queue('\377', ldata);
1073 put_tty_queue('\0', ldata);
1da177e4 1074 }
57c94121 1075 put_tty_queue('\0', ldata);
1da177e4
LT
1076 wake_up_interruptible(&tty->read_wait);
1077}
1078
1079/**
1080 * n_tty_receive_overrun - handle overrun reporting
1081 * @tty: terminal
1082 *
1083 * Data arrived faster than we could process it. While the tty
1084 * driver has flagged this the bits that were missed are gone
1085 * forever.
1086 *
1087 * Called from the receive_buf path so single threaded. Does not
1088 * need locking as num_overrun and overrun_time are function
1089 * private.
1090 */
4edf1827 1091
1da177e4
LT
1092static inline void n_tty_receive_overrun(struct tty_struct *tty)
1093{
53c5ee2c 1094 struct n_tty_data *ldata = tty->disc_data;
1da177e4
LT
1095 char buf[64];
1096
53c5ee2c
JS
1097 ldata->num_overrun++;
1098 if (time_after(jiffies, ldata->overrun_time + HZ) ||
1099 time_after(ldata->overrun_time, jiffies)) {
1da177e4
LT
1100 printk(KERN_WARNING "%s: %d input overrun(s)\n",
1101 tty_name(tty, buf),
53c5ee2c
JS
1102 ldata->num_overrun);
1103 ldata->overrun_time = jiffies;
1104 ldata->num_overrun = 0;
1da177e4
LT
1105 }
1106}
1107
1108/**
1109 * n_tty_receive_parity_error - error notifier
1110 * @tty: terminal device
1111 * @c: character
1112 *
1113 * Process a parity error and queue the right data to indicate
3a4fa0a2 1114 * the error case if necessary. Locking as per n_tty_receive_buf.
1da177e4
LT
1115 */
1116static inline void n_tty_receive_parity_error(struct tty_struct *tty,
1117 unsigned char c)
1118{
57c94121
JS
1119 struct n_tty_data *ldata = tty->disc_data;
1120
4edf1827 1121 if (I_IGNPAR(tty))
1da177e4 1122 return;
1da177e4 1123 if (I_PARMRK(tty)) {
57c94121
JS
1124 put_tty_queue('\377', ldata);
1125 put_tty_queue('\0', ldata);
1126 put_tty_queue(c, ldata);
1da177e4 1127 } else if (I_INPCK(tty))
57c94121 1128 put_tty_queue('\0', ldata);
1da177e4 1129 else
57c94121 1130 put_tty_queue(c, ldata);
1da177e4
LT
1131 wake_up_interruptible(&tty->read_wait);
1132}
1133
1134/**
1135 * n_tty_receive_char - perform processing
1136 * @tty: terminal device
1137 * @c: character
1138 *
1139 * Process an individual character of input received from the driver.
4edf1827 1140 * This is serialized with respect to itself by the rules for the
1da177e4
LT
1141 * driver above.
1142 */
1143
1144static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
1145{
53c5ee2c 1146 struct n_tty_data *ldata = tty->disc_data;
1da177e4 1147 unsigned long flags;
acc71bba 1148 int parmrk;
1da177e4 1149
53c5ee2c 1150 if (ldata->raw) {
57c94121 1151 put_tty_queue(c, ldata);
1da177e4
LT
1152 return;
1153 }
4edf1827 1154
1da177e4
LT
1155 if (I_ISTRIP(tty))
1156 c &= 0x7f;
1157 if (I_IUCLC(tty) && L_IEXTEN(tty))
300a6204 1158 c = tolower(c);
1da177e4 1159
26df6d13 1160 if (L_EXTPROC(tty)) {
57c94121 1161 put_tty_queue(c, ldata);
26df6d13 1162 return;
1163 }
1164
54d2a37e 1165 if (tty->stopped && !tty->flow_stopped && I_IXON(tty) &&
a88a69c9
JP
1166 I_IXANY(tty) && c != START_CHAR(tty) && c != STOP_CHAR(tty) &&
1167 c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) && c != SUSP_CHAR(tty)) {
54d2a37e 1168 start_tty(tty);
a88a69c9
JP
1169 process_echoes(tty);
1170 }
54d2a37e 1171
1da177e4
LT
1172 if (tty->closing) {
1173 if (I_IXON(tty)) {
a88a69c9 1174 if (c == START_CHAR(tty)) {
1da177e4 1175 start_tty(tty);
a88a69c9 1176 process_echoes(tty);
300a6204 1177 } else if (c == STOP_CHAR(tty))
1da177e4
LT
1178 stop_tty(tty);
1179 }
1180 return;
1181 }
1182
1183 /*
1184 * If the previous character was LNEXT, or we know that this
1185 * character is not one of the characters that we'll have to
1186 * handle specially, do shortcut processing to speed things
1187 * up.
1188 */
3fe780b3 1189 if (!test_bit(c, ldata->process_char_map) || ldata->lnext) {
53c5ee2c 1190 ldata->lnext = 0;
acc71bba 1191 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
ba2e68ac 1192 if (ldata->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
acc71bba 1193 /* beep if no space */
7e94b1d9
JP
1194 if (L_ECHO(tty))
1195 process_output('\a', tty);
acc71bba
JP
1196 return;
1197 }
1198 if (L_ECHO(tty)) {
57c94121 1199 finish_erasing(ldata);
1da177e4 1200 /* Record the column of first canon char. */
ba2e68ac 1201 if (ldata->canon_head == ldata->read_head)
57c94121 1202 echo_set_canon_col(ldata);
1da177e4 1203 echo_char(c, tty);
a88a69c9 1204 process_echoes(tty);
1da177e4 1205 }
acc71bba 1206 if (parmrk)
57c94121
JS
1207 put_tty_queue(c, ldata);
1208 put_tty_queue(c, ldata);
1da177e4
LT
1209 return;
1210 }
4edf1827 1211
1da177e4
LT
1212 if (I_IXON(tty)) {
1213 if (c == START_CHAR(tty)) {
1214 start_tty(tty);
a88a69c9 1215 process_echoes(tty);
1da177e4
LT
1216 return;
1217 }
1218 if (c == STOP_CHAR(tty)) {
1219 stop_tty(tty);
1220 return;
1221 }
1222 }
575537b3 1223
1da177e4
LT
1224 if (L_ISIG(tty)) {
1225 int signal;
1226 signal = SIGINT;
1227 if (c == INTR_CHAR(tty))
1228 goto send_signal;
1229 signal = SIGQUIT;
1230 if (c == QUIT_CHAR(tty))
1231 goto send_signal;
1232 signal = SIGTSTP;
1233 if (c == SUSP_CHAR(tty)) {
1234send_signal:
ec5b1157
JP
1235 if (!L_NOFLSH(tty)) {
1236 n_tty_flush_buffer(tty);
f34d7a5b 1237 tty_driver_flush_buffer(tty);
ec5b1157 1238 }
a88a69c9
JP
1239 if (I_IXON(tty))
1240 start_tty(tty);
1241 if (L_ECHO(tty)) {
ec5b1157 1242 echo_char(c, tty);
a88a69c9
JP
1243 process_echoes(tty);
1244 }
8c985d18 1245 isig(signal, tty);
1da177e4
LT
1246 return;
1247 }
1248 }
575537b3
JP
1249
1250 if (c == '\r') {
1251 if (I_IGNCR(tty))
1252 return;
1253 if (I_ICRNL(tty))
1254 c = '\n';
1255 } else if (c == '\n' && I_INLCR(tty))
1256 c = '\r';
1257
53c5ee2c 1258 if (ldata->icanon) {
1da177e4
LT
1259 if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
1260 (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
1261 eraser(c, tty);
a88a69c9 1262 process_echoes(tty);
1da177e4
LT
1263 return;
1264 }
1265 if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
53c5ee2c 1266 ldata->lnext = 1;
1da177e4 1267 if (L_ECHO(tty)) {
57c94121 1268 finish_erasing(ldata);
1da177e4 1269 if (L_ECHOCTL(tty)) {
57c94121
JS
1270 echo_char_raw('^', ldata);
1271 echo_char_raw('\b', ldata);
a88a69c9 1272 process_echoes(tty);
1da177e4
LT
1273 }
1274 }
1275 return;
1276 }
1277 if (c == REPRINT_CHAR(tty) && L_ECHO(tty) &&
1278 L_IEXTEN(tty)) {
ba2e68ac 1279 unsigned long tail = ldata->canon_head;
1da177e4 1280
57c94121 1281 finish_erasing(ldata);
1da177e4 1282 echo_char(c, tty);
57c94121 1283 echo_char_raw('\n', ldata);
ba2e68ac
JS
1284 while (tail != ldata->read_head) {
1285 echo_char(ldata->read_buf[tail], tty);
1da177e4
LT
1286 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
1287 }
a88a69c9 1288 process_echoes(tty);
1da177e4
LT
1289 return;
1290 }
1291 if (c == '\n') {
ba2e68ac 1292 if (ldata->read_cnt >= N_TTY_BUF_SIZE) {
7e94b1d9
JP
1293 if (L_ECHO(tty))
1294 process_output('\a', tty);
acc71bba
JP
1295 return;
1296 }
1297 if (L_ECHO(tty) || L_ECHONL(tty)) {
57c94121 1298 echo_char_raw('\n', ldata);
a88a69c9 1299 process_echoes(tty);
1da177e4
LT
1300 }
1301 goto handle_newline;
1302 }
1303 if (c == EOF_CHAR(tty)) {
ba2e68ac 1304 if (ldata->read_cnt >= N_TTY_BUF_SIZE)
acc71bba 1305 return;
ba2e68ac 1306 if (ldata->canon_head != ldata->read_head)
4edf1827 1307 set_bit(TTY_PUSH, &tty->flags);
1da177e4
LT
1308 c = __DISABLED_CHAR;
1309 goto handle_newline;
1310 }
1311 if ((c == EOL_CHAR(tty)) ||
1312 (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
acc71bba
JP
1313 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty))
1314 ? 1 : 0;
ba2e68ac 1315 if (ldata->read_cnt >= (N_TTY_BUF_SIZE - parmrk)) {
7e94b1d9
JP
1316 if (L_ECHO(tty))
1317 process_output('\a', tty);
acc71bba
JP
1318 return;
1319 }
1da177e4
LT
1320 /*
1321 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
1322 */
1323 if (L_ECHO(tty)) {
1da177e4 1324 /* Record the column of first canon char. */
ba2e68ac 1325 if (ldata->canon_head == ldata->read_head)
57c94121 1326 echo_set_canon_col(ldata);
1da177e4 1327 echo_char(c, tty);
a88a69c9 1328 process_echoes(tty);
1da177e4
LT
1329 }
1330 /*
1331 * XXX does PARMRK doubling happen for
1332 * EOL_CHAR and EOL2_CHAR?
1333 */
acc71bba 1334 if (parmrk)
57c94121 1335 put_tty_queue(c, ldata);
1da177e4 1336
4edf1827 1337handle_newline:
98001214 1338 raw_spin_lock_irqsave(&ldata->read_lock, flags);
ba2e68ac 1339 set_bit(ldata->read_head, ldata->read_flags);
57c94121 1340 put_tty_queue_nolock(c, ldata);
ba2e68ac
JS
1341 ldata->canon_head = ldata->read_head;
1342 ldata->canon_data++;
98001214 1343 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
1da177e4
LT
1344 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1345 if (waitqueue_active(&tty->read_wait))
1346 wake_up_interruptible(&tty->read_wait);
1347 return;
1348 }
1349 }
4edf1827 1350
acc71bba 1351 parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
ba2e68ac 1352 if (ldata->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
acc71bba 1353 /* beep if no space */
7e94b1d9
JP
1354 if (L_ECHO(tty))
1355 process_output('\a', tty);
acc71bba
JP
1356 return;
1357 }
1358 if (L_ECHO(tty)) {
57c94121 1359 finish_erasing(ldata);
1da177e4 1360 if (c == '\n')
57c94121 1361 echo_char_raw('\n', ldata);
1da177e4
LT
1362 else {
1363 /* Record the column of first canon char. */
ba2e68ac 1364 if (ldata->canon_head == ldata->read_head)
57c94121 1365 echo_set_canon_col(ldata);
1da177e4
LT
1366 echo_char(c, tty);
1367 }
a88a69c9 1368 process_echoes(tty);
1da177e4
LT
1369 }
1370
acc71bba 1371 if (parmrk)
57c94121 1372 put_tty_queue(c, ldata);
1da177e4 1373
57c94121 1374 put_tty_queue(c, ldata);
4edf1827 1375}
1da177e4 1376
1da177e4
LT
1377
1378/**
1379 * n_tty_write_wakeup - asynchronous I/O notifier
1380 * @tty: tty device
1381 *
1382 * Required for the ptys, serial driver etc. since processes
1383 * that attach themselves to the master and rely on ASYNC
1384 * IO must be woken up
1385 */
1386
1387static void n_tty_write_wakeup(struct tty_struct *tty)
1388{
ff8cb0fd 1389 if (tty->fasync && test_and_clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags))
1da177e4 1390 kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
1da177e4
LT
1391}
1392
1393/**
1394 * n_tty_receive_buf - data receive
1395 * @tty: terminal device
1396 * @cp: buffer
1397 * @fp: flag buffer
1398 * @count: characters
1399 *
1400 * Called by the terminal driver when a block of characters has
1401 * been received. This function must be called from soft contexts
1402 * not from interrupt context. The driver is responsible for making
1403 * calls one at a time and in order (or using flush_to_ldisc)
1404 */
4edf1827 1405
55db4c64
LT
1406static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
1407 char *fp, int count)
1da177e4 1408{
53c5ee2c 1409 struct n_tty_data *ldata = tty->disc_data;
1da177e4
LT
1410 const unsigned char *p;
1411 char *f, flags = TTY_NORMAL;
1412 int i;
1413 char buf[64];
1414 unsigned long cpuflags;
1415
53c5ee2c 1416 if (ldata->real_raw) {
98001214 1417 raw_spin_lock_irqsave(&ldata->read_lock, cpuflags);
ba2e68ac
JS
1418 i = min(N_TTY_BUF_SIZE - ldata->read_cnt,
1419 N_TTY_BUF_SIZE - ldata->read_head);
1da177e4 1420 i = min(count, i);
ba2e68ac
JS
1421 memcpy(ldata->read_buf + ldata->read_head, cp, i);
1422 ldata->read_head = (ldata->read_head + i) & (N_TTY_BUF_SIZE-1);
1423 ldata->read_cnt += i;
1da177e4
LT
1424 cp += i;
1425 count -= i;
1426
ba2e68ac
JS
1427 i = min(N_TTY_BUF_SIZE - ldata->read_cnt,
1428 N_TTY_BUF_SIZE - ldata->read_head);
1da177e4 1429 i = min(count, i);
ba2e68ac
JS
1430 memcpy(ldata->read_buf + ldata->read_head, cp, i);
1431 ldata->read_head = (ldata->read_head + i) & (N_TTY_BUF_SIZE-1);
1432 ldata->read_cnt += i;
98001214 1433 raw_spin_unlock_irqrestore(&ldata->read_lock, cpuflags);
1da177e4 1434 } else {
4edf1827 1435 for (i = count, p = cp, f = fp; i; i--, p++) {
1da177e4
LT
1436 if (f)
1437 flags = *f++;
1438 switch (flags) {
1439 case TTY_NORMAL:
1440 n_tty_receive_char(tty, *p);
1441 break;
1442 case TTY_BREAK:
1443 n_tty_receive_break(tty);
1444 break;
1445 case TTY_PARITY:
1446 case TTY_FRAME:
1447 n_tty_receive_parity_error(tty, *p);
1448 break;
1449 case TTY_OVERRUN:
1450 n_tty_receive_overrun(tty);
1451 break;
1452 default:
4edf1827 1453 printk(KERN_ERR "%s: unknown flag %d\n",
1da177e4
LT
1454 tty_name(tty, buf), flags);
1455 break;
1456 }
1457 }
f34d7a5b
AC
1458 if (tty->ops->flush_chars)
1459 tty->ops->flush_chars(tty);
1da177e4
LT
1460 }
1461
55db4c64
LT
1462 n_tty_set_room(tty);
1463
ba2e68ac 1464 if ((!ldata->icanon && (ldata->read_cnt >= tty->minimum_to_wake)) ||
26df6d13 1465 L_EXTPROC(tty)) {
1da177e4
LT
1466 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
1467 if (waitqueue_active(&tty->read_wait))
1468 wake_up_interruptible(&tty->read_wait);
1469 }
1470
1471 /*
1472 * Check the remaining room for the input canonicalization
1473 * mode. We don't want to throttle the driver if we're in
1474 * canonical mode and don't have a newline yet!
1475 */
e91e52e4
PH
1476 while (1) {
1477 tty_set_flow_change(tty, TTY_THROTTLE_SAFE);
1478 if (tty->receive_room >= TTY_THRESHOLD_THROTTLE)
1479 break;
1480 if (!tty_throttle_safe(tty))
1481 break;
1482 }
1483 __tty_set_flow_change(tty, 0);
1da177e4
LT
1484}
1485
1486int is_ignored(int sig)
1487{
1488 return (sigismember(&current->blocked, sig) ||
4edf1827 1489 current->sighand->action[sig-1].sa.sa_handler == SIG_IGN);
1da177e4
LT
1490}
1491
1492/**
1493 * n_tty_set_termios - termios data changed
1494 * @tty: terminal
1495 * @old: previous data
1496 *
1497 * Called by the tty layer when the user changes termios flags so
1498 * that the line discipline can plan ahead. This function cannot sleep
4edf1827 1499 * and is protected from re-entry by the tty layer. The user is
1da177e4
LT
1500 * guaranteed that this function will not be re-entered or in progress
1501 * when the ldisc is closed.
17b82060
AC
1502 *
1503 * Locking: Caller holds tty->termios_mutex
1da177e4 1504 */
4edf1827
AC
1505
1506static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
1da177e4 1507{
53c5ee2c 1508 struct n_tty_data *ldata = tty->disc_data;
47afa7a5 1509 int canon_change = 1;
47afa7a5
AC
1510
1511 if (old)
adc8d746 1512 canon_change = (old->c_lflag ^ tty->termios.c_lflag) & ICANON;
47afa7a5 1513 if (canon_change) {
3fe780b3 1514 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
ba2e68ac
JS
1515 ldata->canon_head = ldata->read_tail;
1516 ldata->canon_data = 0;
53c5ee2c 1517 ldata->erasing = 0;
47afa7a5
AC
1518 }
1519
ba2e68ac 1520 if (canon_change && !L_ICANON(tty) && ldata->read_cnt)
47afa7a5 1521 wake_up_interruptible(&tty->read_wait);
4edf1827 1522
53c5ee2c 1523 ldata->icanon = (L_ICANON(tty) != 0);
1da177e4 1524 if (test_bit(TTY_HW_COOK_IN, &tty->flags)) {
53c5ee2c
JS
1525 ldata->raw = 1;
1526 ldata->real_raw = 1;
55db4c64 1527 n_tty_set_room(tty);
1da177e4
LT
1528 return;
1529 }
1530 if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
1531 I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
1532 I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
1533 I_PARMRK(tty)) {
3fe780b3 1534 bitmap_zero(ldata->process_char_map, 256);
1da177e4
LT
1535
1536 if (I_IGNCR(tty) || I_ICRNL(tty))
3fe780b3 1537 set_bit('\r', ldata->process_char_map);
1da177e4 1538 if (I_INLCR(tty))
3fe780b3 1539 set_bit('\n', ldata->process_char_map);
1da177e4
LT
1540
1541 if (L_ICANON(tty)) {
3fe780b3
JS
1542 set_bit(ERASE_CHAR(tty), ldata->process_char_map);
1543 set_bit(KILL_CHAR(tty), ldata->process_char_map);
1544 set_bit(EOF_CHAR(tty), ldata->process_char_map);
1545 set_bit('\n', ldata->process_char_map);
1546 set_bit(EOL_CHAR(tty), ldata->process_char_map);
1da177e4
LT
1547 if (L_IEXTEN(tty)) {
1548 set_bit(WERASE_CHAR(tty),
3fe780b3 1549 ldata->process_char_map);
1da177e4 1550 set_bit(LNEXT_CHAR(tty),
3fe780b3 1551 ldata->process_char_map);
1da177e4 1552 set_bit(EOL2_CHAR(tty),
3fe780b3 1553 ldata->process_char_map);
1da177e4
LT
1554 if (L_ECHO(tty))
1555 set_bit(REPRINT_CHAR(tty),
3fe780b3 1556 ldata->process_char_map);
1da177e4
LT
1557 }
1558 }
1559 if (I_IXON(tty)) {
3fe780b3
JS
1560 set_bit(START_CHAR(tty), ldata->process_char_map);
1561 set_bit(STOP_CHAR(tty), ldata->process_char_map);
1da177e4
LT
1562 }
1563 if (L_ISIG(tty)) {
3fe780b3
JS
1564 set_bit(INTR_CHAR(tty), ldata->process_char_map);
1565 set_bit(QUIT_CHAR(tty), ldata->process_char_map);
1566 set_bit(SUSP_CHAR(tty), ldata->process_char_map);
1da177e4 1567 }
3fe780b3 1568 clear_bit(__DISABLED_CHAR, ldata->process_char_map);
53c5ee2c
JS
1569 ldata->raw = 0;
1570 ldata->real_raw = 0;
1da177e4 1571 } else {
53c5ee2c 1572 ldata->raw = 1;
1da177e4
LT
1573 if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
1574 (I_IGNPAR(tty) || !I_INPCK(tty)) &&
1575 (tty->driver->flags & TTY_DRIVER_REAL_RAW))
53c5ee2c 1576 ldata->real_raw = 1;
1da177e4 1577 else
53c5ee2c 1578 ldata->real_raw = 0;
1da177e4 1579 }
55db4c64 1580 n_tty_set_room(tty);
dab73b4e
WY
1581 /*
1582 * Fix tty hang when I_IXON(tty) is cleared, but the tty
1583 * been stopped by STOP_CHAR(tty) before it.
1584 */
1585 if (!I_IXON(tty) && old && (old->c_iflag & IXON) && !tty->flow_stopped) {
1586 start_tty(tty);
1587 }
1588
f34d7a5b
AC
1589 /* The termios change make the tty ready for I/O */
1590 wake_up_interruptible(&tty->write_wait);
1591 wake_up_interruptible(&tty->read_wait);
1da177e4
LT
1592}
1593
1594/**
1595 * n_tty_close - close the ldisc for this tty
1596 * @tty: device
1597 *
4edf1827
AC
1598 * Called from the terminal layer when this line discipline is
1599 * being shut down, either because of a close or becsuse of a
1da177e4
LT
1600 * discipline change. The function will not be called while other
1601 * ldisc methods are in progress.
1602 */
4edf1827 1603
1da177e4
LT
1604static void n_tty_close(struct tty_struct *tty)
1605{
70ece7a7
JS
1606 struct n_tty_data *ldata = tty->disc_data;
1607
79901317
PH
1608 if (tty->link)
1609 n_tty_packet_mode_flush(tty);
1610
ba2e68ac
JS
1611 kfree(ldata->read_buf);
1612 kfree(ldata->echo_buf);
70ece7a7 1613 kfree(ldata);
70ece7a7 1614 tty->disc_data = NULL;
1da177e4
LT
1615}
1616
1617/**
1618 * n_tty_open - open an ldisc
1619 * @tty: terminal to open
1620 *
4edf1827 1621 * Called when this line discipline is being attached to the
1da177e4
LT
1622 * terminal device. Can sleep. Called serialized so that no
1623 * other events will occur in parallel. No further open will occur
1624 * until a close.
1625 */
1626
1627static int n_tty_open(struct tty_struct *tty)
1628{
70ece7a7
JS
1629 struct n_tty_data *ldata;
1630
1631 ldata = kzalloc(sizeof(*ldata), GFP_KERNEL);
1632 if (!ldata)
1633 goto err;
1634
53c5ee2c 1635 ldata->overrun_time = jiffies;
bddc7152
JS
1636 mutex_init(&ldata->atomic_read_lock);
1637 mutex_init(&ldata->output_lock);
1638 mutex_init(&ldata->echo_lock);
98001214 1639 raw_spin_lock_init(&ldata->read_lock);
53c5ee2c 1640
a88a69c9 1641 /* These are ugly. Currently a malloc failure here can panic */
ba2e68ac
JS
1642 ldata->read_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
1643 ldata->echo_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
1644 if (!ldata->read_buf || !ldata->echo_buf)
b91939f5 1645 goto err_free_bufs;
0b4068a1 1646
70ece7a7 1647 tty->disc_data = ldata;
b66f4fa5 1648 reset_buffer_flags(tty->disc_data);
53c5ee2c 1649 ldata->column = 0;
1da177e4
LT
1650 tty->minimum_to_wake = 1;
1651 tty->closing = 0;
b66f4fa5
PH
1652 /* indicate buffer work may resume */
1653 clear_bit(TTY_LDISC_HALTED, &tty->flags);
1654 n_tty_set_termios(tty, NULL);
1655 tty_unthrottle(tty);
70ece7a7 1656
1da177e4 1657 return 0;
b91939f5 1658err_free_bufs:
ba2e68ac
JS
1659 kfree(ldata->read_buf);
1660 kfree(ldata->echo_buf);
70ece7a7
JS
1661 kfree(ldata);
1662err:
b91939f5 1663 return -ENOMEM;
1da177e4
LT
1664}
1665
1666static inline int input_available_p(struct tty_struct *tty, int amt)
1667{
53c5ee2c
JS
1668 struct n_tty_data *ldata = tty->disc_data;
1669
e043e42b 1670 tty_flush_to_ldisc(tty);
53c5ee2c 1671 if (ldata->icanon && !L_EXTPROC(tty)) {
ba2e68ac 1672 if (ldata->canon_data)
1da177e4 1673 return 1;
ba2e68ac 1674 } else if (ldata->read_cnt >= (amt ? amt : 1))
1da177e4
LT
1675 return 1;
1676
1677 return 0;
1678}
1679
1680/**
bbd20759 1681 * copy_from_read_buf - copy read data directly
1da177e4
LT
1682 * @tty: terminal device
1683 * @b: user data
1684 * @nr: size of data
1685 *
11a96d18 1686 * Helper function to speed up n_tty_read. It is only called when
1da177e4
LT
1687 * ICANON is off; it copies characters straight from the tty queue to
1688 * user space directly. It can be profitably called twice; once to
1689 * drain the space from the tail pointer to the (physical) end of the
1690 * buffer, and once to drain the space from the (physical) beginning of
1691 * the buffer to head pointer.
1692 *
bddc7152 1693 * Called under the ldata->atomic_read_lock sem
1da177e4
LT
1694 *
1695 */
4edf1827 1696
33f0f88f 1697static int copy_from_read_buf(struct tty_struct *tty,
1da177e4
LT
1698 unsigned char __user **b,
1699 size_t *nr)
1700
1701{
53c5ee2c 1702 struct n_tty_data *ldata = tty->disc_data;
1da177e4
LT
1703 int retval;
1704 size_t n;
1705 unsigned long flags;
3fa10cc8 1706 bool is_eof;
1da177e4
LT
1707
1708 retval = 0;
98001214 1709 raw_spin_lock_irqsave(&ldata->read_lock, flags);
ba2e68ac 1710 n = min(ldata->read_cnt, N_TTY_BUF_SIZE - ldata->read_tail);
1da177e4 1711 n = min(*nr, n);
98001214 1712 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
1da177e4 1713 if (n) {
ba2e68ac 1714 retval = copy_to_user(*b, &ldata->read_buf[ldata->read_tail], n);
1da177e4 1715 n -= retval;
3fa10cc8 1716 is_eof = n == 1 &&
ba2e68ac
JS
1717 ldata->read_buf[ldata->read_tail] == EOF_CHAR(tty);
1718 tty_audit_add_data(tty, &ldata->read_buf[ldata->read_tail], n,
53c5ee2c 1719 ldata->icanon);
98001214 1720 raw_spin_lock_irqsave(&ldata->read_lock, flags);
ba2e68ac
JS
1721 ldata->read_tail = (ldata->read_tail + n) & (N_TTY_BUF_SIZE-1);
1722 ldata->read_cnt -= n;
26df6d13 1723 /* Turn single EOF into zero-length read */
ba2e68ac 1724 if (L_EXTPROC(tty) && ldata->icanon && is_eof && !ldata->read_cnt)
3fa10cc8 1725 n = 0;
98001214 1726 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
1da177e4
LT
1727 *b += n;
1728 *nr -= n;
1729 }
1730 return retval;
1731}
1732
cc4191dc 1733extern ssize_t redirected_tty_write(struct file *, const char __user *,
4edf1827 1734 size_t, loff_t *);
1da177e4
LT
1735
1736/**
1737 * job_control - check job control
1738 * @tty: tty
1739 * @file: file handle
1740 *
1741 * Perform job control management checks on this file/tty descriptor
4edf1827 1742 * and if appropriate send any needed signals and return a negative
1da177e4 1743 * error code if action should be taken.
04f378b1 1744 *
01a5e440
PH
1745 * Locking: redirected write test is safe
1746 * current->signal->tty check is safe
1747 * ctrl_lock to safely reference tty->pgrp
1da177e4 1748 */
4edf1827 1749
1da177e4
LT
1750static int job_control(struct tty_struct *tty, struct file *file)
1751{
1752 /* Job control check -- must be done at start and after
1753 every sleep (POSIX.1 7.1.1.4). */
1754 /* NOTE: not yet done after every sleep pending a thorough
1755 check of the logic of this change. -- jlc */
1756 /* don't stop on /dev/console */
01a5e440
PH
1757 if (file->f_op->write == redirected_tty_write ||
1758 current->signal->tty != tty)
1759 return 0;
1760
1761 spin_lock_irq(&tty->ctrl_lock);
1762 if (!tty->pgrp)
1763 printk(KERN_ERR "n_tty_read: no tty->pgrp!\n");
1764 else if (task_pgrp(current) != tty->pgrp) {
1765 spin_unlock_irq(&tty->ctrl_lock);
1766 if (is_ignored(SIGTTIN) || is_current_pgrp_orphaned())
1767 return -EIO;
1768 kill_pgrp(task_pgrp(current), SIGTTIN, 1);
1769 set_thread_flag(TIF_SIGPENDING);
1770 return -ERESTARTSYS;
1da177e4 1771 }
01a5e440 1772 spin_unlock_irq(&tty->ctrl_lock);
1da177e4
LT
1773 return 0;
1774}
4edf1827 1775
1da177e4
LT
1776
1777/**
11a96d18 1778 * n_tty_read - read function for tty
1da177e4
LT
1779 * @tty: tty device
1780 * @file: file object
1781 * @buf: userspace buffer pointer
1782 * @nr: size of I/O
1783 *
1784 * Perform reads for the line discipline. We are guaranteed that the
1785 * line discipline will not be closed under us but we may get multiple
1786 * parallel readers and must handle this ourselves. We may also get
1787 * a hangup. Always called in user context, may sleep.
1788 *
1789 * This code must be sure never to sleep through a hangup.
1790 */
4edf1827 1791
11a96d18 1792static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
1da177e4
LT
1793 unsigned char __user *buf, size_t nr)
1794{
53c5ee2c 1795 struct n_tty_data *ldata = tty->disc_data;
1da177e4
LT
1796 unsigned char __user *b = buf;
1797 DECLARE_WAITQUEUE(wait, current);
1798 int c;
1799 int minimum, time;
1800 ssize_t retval = 0;
1801 ssize_t size;
1802 long timeout;
1803 unsigned long flags;
04f378b1 1804 int packet;
1da177e4
LT
1805
1806do_it_again:
1da177e4 1807 c = job_control(tty, file);
4edf1827 1808 if (c < 0)
1da177e4 1809 return c;
4edf1827 1810
1da177e4
LT
1811 minimum = time = 0;
1812 timeout = MAX_SCHEDULE_TIMEOUT;
53c5ee2c 1813 if (!ldata->icanon) {
1da177e4
LT
1814 time = (HZ / 10) * TIME_CHAR(tty);
1815 minimum = MIN_CHAR(tty);
1816 if (minimum) {
1817 if (time)
1818 tty->minimum_to_wake = 1;
1819 else if (!waitqueue_active(&tty->read_wait) ||
1820 (tty->minimum_to_wake > minimum))
1821 tty->minimum_to_wake = minimum;
1822 } else {
1823 timeout = 0;
1824 if (time) {
1825 timeout = time;
1826 time = 0;
1827 }
1828 tty->minimum_to_wake = minimum = 1;
1829 }
1830 }
1831
1832 /*
1833 * Internal serialization of reads.
1834 */
1835 if (file->f_flags & O_NONBLOCK) {
bddc7152 1836 if (!mutex_trylock(&ldata->atomic_read_lock))
1da177e4 1837 return -EAGAIN;
4edf1827 1838 } else {
bddc7152 1839 if (mutex_lock_interruptible(&ldata->atomic_read_lock))
1da177e4
LT
1840 return -ERESTARTSYS;
1841 }
04f378b1 1842 packet = tty->packet;
1da177e4
LT
1843
1844 add_wait_queue(&tty->read_wait, &wait);
1da177e4
LT
1845 while (nr) {
1846 /* First test for status change. */
04f378b1 1847 if (packet && tty->link->ctrl_status) {
1da177e4
LT
1848 unsigned char cs;
1849 if (b != buf)
1850 break;
04f378b1 1851 spin_lock_irqsave(&tty->link->ctrl_lock, flags);
1da177e4
LT
1852 cs = tty->link->ctrl_status;
1853 tty->link->ctrl_status = 0;
04f378b1 1854 spin_unlock_irqrestore(&tty->link->ctrl_lock, flags);
522ed776 1855 if (tty_put_user(tty, cs, b++)) {
1da177e4
LT
1856 retval = -EFAULT;
1857 b--;
1858 break;
1859 }
1860 nr--;
1861 break;
1862 }
1863 /* This statement must be first before checking for input
1864 so that any interrupt will set the state back to
1865 TASK_RUNNING. */
1866 set_current_state(TASK_INTERRUPTIBLE);
4edf1827 1867
1da177e4
LT
1868 if (((minimum - (b - buf)) < tty->minimum_to_wake) &&
1869 ((minimum - (b - buf)) >= 1))
1870 tty->minimum_to_wake = (minimum - (b - buf));
4edf1827 1871
1da177e4
LT
1872 if (!input_available_p(tty, 0)) {
1873 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
1874 retval = -EIO;
1875 break;
1876 }
1877 if (tty_hung_up_p(file))
1878 break;
1879 if (!timeout)
1880 break;
1881 if (file->f_flags & O_NONBLOCK) {
1882 retval = -EAGAIN;
1883 break;
1884 }
1885 if (signal_pending(current)) {
1886 retval = -ERESTARTSYS;
1887 break;
1888 }
55db4c64 1889 n_tty_set_room(tty);
1da177e4 1890 timeout = schedule_timeout(timeout);
1da177e4
LT
1891 continue;
1892 }
1893 __set_current_state(TASK_RUNNING);
1894
1895 /* Deal with packet mode. */
04f378b1 1896 if (packet && b == buf) {
522ed776 1897 if (tty_put_user(tty, TIOCPKT_DATA, b++)) {
1da177e4
LT
1898 retval = -EFAULT;
1899 b--;
1900 break;
1901 }
1902 nr--;
1903 }
1904
53c5ee2c 1905 if (ldata->icanon && !L_EXTPROC(tty)) {
1da177e4 1906 /* N.B. avoid overrun if nr == 0 */
98001214 1907 raw_spin_lock_irqsave(&ldata->read_lock, flags);
ba2e68ac 1908 while (nr && ldata->read_cnt) {
4edf1827 1909 int eol;
1da177e4 1910
ba2e68ac 1911 eol = test_and_clear_bit(ldata->read_tail,
3fe780b3 1912 ldata->read_flags);
ba2e68ac
JS
1913 c = ldata->read_buf[ldata->read_tail];
1914 ldata->read_tail = ((ldata->read_tail+1) &
1da177e4 1915 (N_TTY_BUF_SIZE-1));
ba2e68ac 1916 ldata->read_cnt--;
1da177e4
LT
1917 if (eol) {
1918 /* this test should be redundant:
1919 * we shouldn't be reading data if
1920 * canon_data is 0
1921 */
ba2e68ac
JS
1922 if (--ldata->canon_data < 0)
1923 ldata->canon_data = 0;
1da177e4 1924 }
98001214 1925 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
1da177e4
LT
1926
1927 if (!eol || (c != __DISABLED_CHAR)) {
522ed776 1928 if (tty_put_user(tty, c, b++)) {
1da177e4
LT
1929 retval = -EFAULT;
1930 b--;
98001214 1931 raw_spin_lock_irqsave(&ldata->read_lock, flags);
1da177e4
LT
1932 break;
1933 }
1934 nr--;
1935 }
522ed776
MT
1936 if (eol) {
1937 tty_audit_push(tty);
98001214 1938 raw_spin_lock_irqsave(&ldata->read_lock, flags);
1da177e4 1939 break;
522ed776 1940 }
98001214 1941 raw_spin_lock_irqsave(&ldata->read_lock, flags);
1da177e4 1942 }
98001214 1943 raw_spin_unlock_irqrestore(&ldata->read_lock, flags);
1da177e4
LT
1944 if (retval)
1945 break;
1946 } else {
1947 int uncopied;
04f378b1
AC
1948 /* The copy function takes the read lock and handles
1949 locking internally for this case */
1da177e4
LT
1950 uncopied = copy_from_read_buf(tty, &b, &nr);
1951 uncopied += copy_from_read_buf(tty, &b, &nr);
1952 if (uncopied) {
1953 retval = -EFAULT;
1954 break;
1955 }
1956 }
1957
1958 /* If there is enough space in the read buffer now, let the
1959 * low-level driver know. We use n_tty_chars_in_buffer() to
1960 * check the buffer, as it now knows about canonical mode.
1961 * Otherwise, if the driver is throttled and the line is
1962 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
1963 * we won't get any more characters.
1964 */
e91e52e4
PH
1965 while (1) {
1966 tty_set_flow_change(tty, TTY_UNTHROTTLE_SAFE);
1967 if (n_tty_chars_in_buffer(tty) > TTY_THRESHOLD_UNTHROTTLE)
1968 break;
1969 if (!tty->count)
1970 break;
55db4c64 1971 n_tty_set_room(tty);
e91e52e4
PH
1972 if (!tty_unthrottle_safe(tty))
1973 break;
55db4c64 1974 }
e91e52e4 1975 __tty_set_flow_change(tty, 0);
1da177e4
LT
1976
1977 if (b - buf >= minimum)
1978 break;
1979 if (time)
1980 timeout = time;
1981 }
bddc7152 1982 mutex_unlock(&ldata->atomic_read_lock);
1da177e4
LT
1983 remove_wait_queue(&tty->read_wait, &wait);
1984
1985 if (!waitqueue_active(&tty->read_wait))
1986 tty->minimum_to_wake = minimum;
1987
1988 __set_current_state(TASK_RUNNING);
1989 size = b - buf;
1990 if (size) {
1991 retval = size;
1992 if (nr)
4edf1827 1993 clear_bit(TTY_PUSH, &tty->flags);
1da177e4 1994 } else if (test_and_clear_bit(TTY_PUSH, &tty->flags))
bbd20759 1995 goto do_it_again;
1da177e4 1996
55db4c64 1997 n_tty_set_room(tty);
1da177e4
LT
1998 return retval;
1999}
2000
2001/**
11a96d18 2002 * n_tty_write - write function for tty
1da177e4
LT
2003 * @tty: tty device
2004 * @file: file object
2005 * @buf: userspace buffer pointer
2006 * @nr: size of I/O
2007 *
a88a69c9 2008 * Write function of the terminal device. This is serialized with
1da177e4 2009 * respect to other write callers but not to termios changes, reads
a88a69c9
JP
2010 * and other such events. Since the receive code will echo characters,
2011 * thus calling driver write methods, the output_lock is used in
2012 * the output processing functions called here as well as in the
2013 * echo processing function to protect the column state and space
2014 * left in the buffer.
1da177e4
LT
2015 *
2016 * This code must be sure never to sleep through a hangup.
a88a69c9
JP
2017 *
2018 * Locking: output_lock to protect column state and space left
2019 * (note that the process_output*() functions take this
2020 * lock themselves)
1da177e4 2021 */
4edf1827 2022
11a96d18 2023static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
a88a69c9 2024 const unsigned char *buf, size_t nr)
1da177e4
LT
2025{
2026 const unsigned char *b = buf;
2027 DECLARE_WAITQUEUE(wait, current);
2028 int c;
2029 ssize_t retval = 0;
2030
2031 /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
2032 if (L_TOSTOP(tty) && file->f_op->write != redirected_tty_write) {
2033 retval = tty_check_change(tty);
2034 if (retval)
2035 return retval;
2036 }
2037
a88a69c9
JP
2038 /* Write out any echoed characters that are still pending */
2039 process_echoes(tty);
300a6204 2040
1da177e4
LT
2041 add_wait_queue(&tty->write_wait, &wait);
2042 while (1) {
2043 set_current_state(TASK_INTERRUPTIBLE);
2044 if (signal_pending(current)) {
2045 retval = -ERESTARTSYS;
2046 break;
2047 }
2048 if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
2049 retval = -EIO;
2050 break;
2051 }
2052 if (O_OPOST(tty) && !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
2053 while (nr > 0) {
a88a69c9 2054 ssize_t num = process_output_block(tty, b, nr);
1da177e4
LT
2055 if (num < 0) {
2056 if (num == -EAGAIN)
2057 break;
2058 retval = num;
2059 goto break_out;
2060 }
2061 b += num;
2062 nr -= num;
2063 if (nr == 0)
2064 break;
2065 c = *b;
a88a69c9 2066 if (process_output(c, tty) < 0)
1da177e4
LT
2067 break;
2068 b++; nr--;
2069 }
f34d7a5b
AC
2070 if (tty->ops->flush_chars)
2071 tty->ops->flush_chars(tty);
1da177e4 2072 } else {
abb51007
PH
2073 struct n_tty_data *ldata = tty->disc_data;
2074
d6afe27b 2075 while (nr > 0) {
abb51007 2076 mutex_lock(&ldata->output_lock);
f34d7a5b 2077 c = tty->ops->write(tty, b, nr);
abb51007 2078 mutex_unlock(&ldata->output_lock);
d6afe27b
RZ
2079 if (c < 0) {
2080 retval = c;
2081 goto break_out;
2082 }
2083 if (!c)
2084 break;
2085 b += c;
2086 nr -= c;
1da177e4 2087 }
1da177e4
LT
2088 }
2089 if (!nr)
2090 break;
2091 if (file->f_flags & O_NONBLOCK) {
2092 retval = -EAGAIN;
2093 break;
2094 }
2095 schedule();
2096 }
2097break_out:
2098 __set_current_state(TASK_RUNNING);
2099 remove_wait_queue(&tty->write_wait, &wait);
ff8cb0fd
TP
2100 if (b - buf != nr && tty->fasync)
2101 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
1da177e4
LT
2102 return (b - buf) ? b - buf : retval;
2103}
2104
2105/**
11a96d18 2106 * n_tty_poll - poll method for N_TTY
1da177e4
LT
2107 * @tty: terminal device
2108 * @file: file accessing it
2109 * @wait: poll table
2110 *
2111 * Called when the line discipline is asked to poll() for data or
2112 * for special events. This code is not serialized with respect to
2113 * other events save open/close.
2114 *
2115 * This code must be sure never to sleep through a hangup.
2116 * Called without the kernel lock held - fine
1da177e4 2117 */
4edf1827 2118
11a96d18 2119static unsigned int n_tty_poll(struct tty_struct *tty, struct file *file,
4edf1827 2120 poll_table *wait)
1da177e4
LT
2121{
2122 unsigned int mask = 0;
2123
2124 poll_wait(file, &tty->read_wait, wait);
2125 poll_wait(file, &tty->write_wait, wait);
2126 if (input_available_p(tty, TIME_CHAR(tty) ? 0 : MIN_CHAR(tty)))
2127 mask |= POLLIN | POLLRDNORM;
2128 if (tty->packet && tty->link->ctrl_status)
2129 mask |= POLLPRI | POLLIN | POLLRDNORM;
2130 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
2131 mask |= POLLHUP;
2132 if (tty_hung_up_p(file))
2133 mask |= POLLHUP;
2134 if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) {
2135 if (MIN_CHAR(tty) && !TIME_CHAR(tty))
2136 tty->minimum_to_wake = MIN_CHAR(tty);
2137 else
2138 tty->minimum_to_wake = 1;
2139 }
f34d7a5b
AC
2140 if (tty->ops->write && !tty_is_writelocked(tty) &&
2141 tty_chars_in_buffer(tty) < WAKEUP_CHARS &&
2142 tty_write_room(tty) > 0)
1da177e4
LT
2143 mask |= POLLOUT | POLLWRNORM;
2144 return mask;
2145}
2146
57c94121 2147static unsigned long inq_canon(struct n_tty_data *ldata)
47afa7a5
AC
2148{
2149 int nr, head, tail;
2150
ba2e68ac 2151 if (!ldata->canon_data)
47afa7a5 2152 return 0;
ba2e68ac
JS
2153 head = ldata->canon_head;
2154 tail = ldata->read_tail;
47afa7a5
AC
2155 nr = (head - tail) & (N_TTY_BUF_SIZE-1);
2156 /* Skip EOF-chars.. */
2157 while (head != tail) {
3fe780b3 2158 if (test_bit(tail, ldata->read_flags) &&
ba2e68ac 2159 ldata->read_buf[tail] == __DISABLED_CHAR)
47afa7a5
AC
2160 nr--;
2161 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
2162 }
2163 return nr;
2164}
2165
2166static int n_tty_ioctl(struct tty_struct *tty, struct file *file,
2167 unsigned int cmd, unsigned long arg)
2168{
ba2e68ac 2169 struct n_tty_data *ldata = tty->disc_data;
47afa7a5
AC
2170 int retval;
2171
2172 switch (cmd) {
2173 case TIOCOUTQ:
2174 return put_user(tty_chars_in_buffer(tty), (int __user *) arg);
2175 case TIOCINQ:
17b82060 2176 /* FIXME: Locking */
ba2e68ac 2177 retval = ldata->read_cnt;
47afa7a5 2178 if (L_ICANON(tty))
57c94121 2179 retval = inq_canon(ldata);
47afa7a5
AC
2180 return put_user(retval, (unsigned int __user *) arg);
2181 default:
2182 return n_tty_ioctl_helper(tty, file, cmd, arg);
2183 }
2184}
2185
a352def2 2186struct tty_ldisc_ops tty_ldisc_N_TTY = {
e10cc1df
PF
2187 .magic = TTY_LDISC_MAGIC,
2188 .name = "n_tty",
2189 .open = n_tty_open,
2190 .close = n_tty_close,
2191 .flush_buffer = n_tty_flush_buffer,
2192 .chars_in_buffer = n_tty_chars_in_buffer,
11a96d18
AC
2193 .read = n_tty_read,
2194 .write = n_tty_write,
e10cc1df
PF
2195 .ioctl = n_tty_ioctl,
2196 .set_termios = n_tty_set_termios,
11a96d18 2197 .poll = n_tty_poll,
e10cc1df
PF
2198 .receive_buf = n_tty_receive_buf,
2199 .write_wakeup = n_tty_write_wakeup
1da177e4 2200};
572b9adb
RG
2201
2202/**
2203 * n_tty_inherit_ops - inherit N_TTY methods
2204 * @ops: struct tty_ldisc_ops where to save N_TTY methods
2205 *
593fb1ae 2206 * Enables a 'subclass' line discipline to 'inherit' N_TTY
572b9adb
RG
2207 * methods.
2208 */
2209
2210void n_tty_inherit_ops(struct tty_ldisc_ops *ops)
2211{
2212 *ops = tty_ldisc_N_TTY;
2213 ops->owner = NULL;
2214 ops->refcount = ops->flags = 0;
2215}
2216EXPORT_SYMBOL_GPL(n_tty_inherit_ops);