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