]> Git Repo - linux.git/blame - drivers/tty/n_tty.c
tty: audit: unify to u8
[linux.git] / drivers / tty / n_tty.c
CommitLineData
e3b3d0f5 1// SPDX-License-Identifier: GPL-1.0+
1da177e4
LT
2/*
3 * n_tty.c --- implements the N_TTY line discipline.
4edf1827 4 *
1da177e4
LT
5 * This code used to be in tty_io.c, but things are getting hairy
6 * enough that it made sense to split things off. (The N_TTY
7 * processing has changed so much that it's hardly recognizable,
8 * anyway...)
9 *
10 * Note that the open routine for N_TTY is guaranteed never to return
11 * an error. This is because Linux will fall back to setting a line
4edf1827 12 * to N_TTY if it can not switch to any other line discipline.
1da177e4
LT
13 *
14 * Written by Theodore Ts'o, Copyright 1994.
4edf1827 15 *
1da177e4
LT
16 * This file also contains code originally written by Linus Torvalds,
17 * Copyright 1991, 1992, 1993, and by Julian Cowley, Copyright 1994.
4edf1827 18 *
1da177e4
LT
19 * Reduced memory usage for older ARM systems - Russell King.
20 *
4edf1827 21 * 2000/01/20 Fixed SMP locking on put_tty_queue using bits of
1da177e4
LT
22 * the patch by Andrew J. Kroll <[email protected]>
23 * who actually finally proved there really was a race.
24 *
25 * 2002/03/18 Implemented n_tty_wakeup to send SIGIO POLL_OUTs to
26 * waiting writing processes-Sapan Bhatia <[email protected]>.
11a96d18 27 * Also fixed a bug in BLOCKING mode where n_tty_write returns
1da177e4
LT
28 * EAGAIN
29 */
30
8ed012d1 31#include <linux/bitmap.h>
9db1be84
IJ
32#include <linux/bitops.h>
33#include <linux/ctype.h>
1da177e4 34#include <linux/errno.h>
8ed012d1 35#include <linux/export.h>
1da177e4 36#include <linux/fcntl.h>
9db1be84 37#include <linux/file.h>
8ed012d1 38#include <linux/jiffies.h>
7e26c84d 39#include <linux/math.h>
9db1be84 40#include <linux/poll.h>
593fb1ae 41#include <linux/ratelimit.h>
9db1be84
IJ
42#include <linux/sched.h>
43#include <linux/signal.h>
44#include <linux/slab.h>
45#include <linux/string.h>
9db1be84
IJ
46#include <linux/tty.h>
47#include <linux/types.h>
48#include <linux/uaccess.h>
86e35aea 49#include <linux/vmalloc.h>
9db1be84 50
98602c01 51#include "tty.h"
1da177e4 52
a5db4826
VV
53/*
54 * Until this number of characters is queued in the xmit buffer, select will
55 * return "we have room for writes".
56 */
1da177e4
LT
57#define WAKEUP_CHARS 256
58
59/*
60 * This defines the low- and high-watermarks for throttling and
61 * unthrottling the TTY driver. These watermarks are used for
62 * controlling the space in the read buffer.
63 */
64#define TTY_THRESHOLD_THROTTLE 128 /* now based on remaining room */
bbd20759 65#define TTY_THRESHOLD_UNTHROTTLE 128
1da177e4 66
a88a69c9
JP
67/*
68 * Special byte codes used in the echo buffer to represent operations
69 * or special handling of characters. Bytes in the echo buffer that
70 * are not part of such special blocks are treated as normal character
71 * codes.
72 */
73#define ECHO_OP_START 0xff
74#define ECHO_OP_MOVE_BACK_COL 0x80
75#define ECHO_OP_SET_CANON_COL 0x81
76#define ECHO_OP_ERASE_TAB 0x82
77
cbfd0340
PH
78#define ECHO_COMMIT_WATERMARK 256
79#define ECHO_BLOCK 256
80#define ECHO_DISCARD_WATERMARK N_TTY_BUF_SIZE - (ECHO_BLOCK + 32)
81
82
32f13521
PH
83#undef N_TTY_TRACE
84#ifdef N_TTY_TRACE
85# define n_tty_trace(f, args...) trace_printk(f, ##args)
86#else
a287885f 87# define n_tty_trace(f, args...) no_printk(f, ##args)
32f13521
PH
88#endif
89
70ece7a7 90struct n_tty_data {
fb7aa03d
PH
91 /* producer-published */
92 size_t read_head;
70aca71f 93 size_t commit_head;
fb7aa03d 94 size_t canon_head;
9dfd16dd
PH
95 size_t echo_head;
96 size_t echo_commit;
1075a6e2 97 size_t echo_mark;
1bb9d562 98 DECLARE_BITMAP(char_map, 256);
fb7aa03d
PH
99
100 /* private to n_tty_receive_overrun (single-threaded) */
53c5ee2c
JS
101 unsigned long overrun_time;
102 int num_overrun;
103
24a89d1c
PH
104 /* non-atomic */
105 bool no_room;
106
fb7aa03d 107 /* must hold exclusive termios_rwsem to reset these */
53c5ee2c 108 unsigned char lnext:1, erasing:1, raw:1, real_raw:1, icanon:1;
4d0ed182 109 unsigned char push:1;
3fe780b3 110
fb7aa03d 111 /* shared by producer and consumer */
20bafb3d 112 char read_buf[N_TTY_BUF_SIZE];
3fe780b3 113 DECLARE_BITMAP(read_flags, N_TTY_BUF_SIZE);
20bafb3d 114 unsigned char echo_buf[N_TTY_BUF_SIZE];
ba2e68ac 115
fb7aa03d
PH
116 /* consumer-published */
117 size_t read_tail;
40d5e090 118 size_t line_start;
fb7aa03d 119
6bb6fa69
IJ
120 /* # of chars looked ahead (to find software flow control chars) */
121 size_t lookahead_count;
122
fb7aa03d
PH
123 /* protected by output lock */
124 unsigned int column;
ba2e68ac 125 unsigned int canon_column;
9dfd16dd 126 size_t echo_tail;
bddc7152
JS
127
128 struct mutex atomic_read_lock;
129 struct mutex output_lock;
70ece7a7
JS
130};
131
3d63b7e4
TH
132#define MASK(x) ((x) & (N_TTY_BUF_SIZE - 1))
133
ce74117a
PH
134static inline size_t read_cnt(struct n_tty_data *ldata)
135{
a2f73be8 136 return ldata->read_head - ldata->read_tail;
ce74117a
PH
137}
138
bc5a5e3f
PH
139static inline unsigned char read_buf(struct n_tty_data *ldata, size_t i)
140{
141 return ldata->read_buf[i & (N_TTY_BUF_SIZE - 1)];
142}
143
144static inline unsigned char *read_buf_addr(struct n_tty_data *ldata, size_t i)
145{
146 return &ldata->read_buf[i & (N_TTY_BUF_SIZE - 1)];
147}
148
addaebcc
PH
149static inline unsigned char echo_buf(struct n_tty_data *ldata, size_t i)
150{
ebec3f8f 151 smp_rmb(); /* Matches smp_wmb() in add_echo_byte(). */
addaebcc
PH
152 return ldata->echo_buf[i & (N_TTY_BUF_SIZE - 1)];
153}
154
155static inline unsigned char *echo_buf_addr(struct n_tty_data *ldata, size_t i)
156{
157 return &ldata->echo_buf[i & (N_TTY_BUF_SIZE - 1)];
158}
159
b97b3d9f 160/* If we are not echoing the data, perhaps this is a secret so erase it */
f6f847ff 161static void zero_buffer(const struct tty_struct *tty, u8 *buffer, size_t size)
b97b3d9f 162{
f6f847ff
JS
163 if (L_ICANON(tty) && !L_ECHO(tty))
164 memset(buffer, 0, size);
b97b3d9f
GKH
165}
166
5bedcf70
JS
167static void tty_copy(const struct tty_struct *tty, void *to, size_t tail,
168 size_t n)
72586c60
LA
169{
170 struct n_tty_data *ldata = tty->disc_data;
679e7c29 171 size_t size = N_TTY_BUF_SIZE - tail;
b97b3d9f 172 void *from = read_buf_addr(ldata, tail);
679e7c29
PH
173
174 if (n > size) {
309426ae 175 tty_audit_add_data(tty, from, size);
3b830a9c
LT
176 memcpy(to, from, size);
177 zero_buffer(tty, from, size);
679e7c29
PH
178 to += size;
179 n -= size;
180 from = ldata->read_buf;
181 }
72586c60 182
309426ae 183 tty_audit_add_data(tty, from, n);
3b830a9c
LT
184 memcpy(to, from, n);
185 zero_buffer(tty, from, n);
72586c60
LA
186}
187
24a89d1c 188/**
98629663
JS
189 * n_tty_kick_worker - start input worker (if required)
190 * @tty: terminal
24a89d1c 191 *
98629663 192 * Re-schedules the flip buffer work if it may have stopped.
24a89d1c 193 *
98629663
JS
194 * Locking:
195 * * Caller holds exclusive %termios_rwsem, or
196 * * n_tty_read()/consumer path:
197 * holds non-exclusive %termios_rwsem
24a89d1c 198 */
5bedcf70 199static void n_tty_kick_worker(const struct tty_struct *tty)
7879a9f9 200{
24a89d1c
PH
201 struct n_tty_data *ldata = tty->disc_data;
202
2c5dc464 203 /* Did the input worker stop? Restart it */
4903fde8
HL
204 if (unlikely(READ_ONCE(ldata->no_room))) {
205 WRITE_ONCE(ldata->no_room, 0);
24a89d1c 206
ecbbfd44 207 WARN_RATELIMIT(tty->port->itty == NULL,
cadf7486 208 "scheduling with invalid itty\n");
21622939
PH
209 /* see if ldisc has been killed - if so, this means that
210 * even though the ldisc has been halted and ->buf.work
211 * cancelled, ->buf.work is about to be rescheduled
212 */
213 WARN_RATELIMIT(test_bit(TTY_LDISC_HALTED, &tty->flags),
214 "scheduling buffer work for halted ldisc\n");
e176058f 215 tty_buffer_restart_work(tty->port);
ecbbfd44 216 }
55db4c64
LT
217}
218
5bedcf70 219static ssize_t chars_in_buffer(const struct tty_struct *tty)
9a4aec2d 220{
5bedcf70 221 const struct n_tty_data *ldata = tty->disc_data;
9a4aec2d
PH
222 ssize_t n = 0;
223
224 if (!ldata->icanon)
70aca71f 225 n = ldata->commit_head - ldata->read_tail;
9a4aec2d
PH
226 else
227 n = ldata->canon_head - ldata->read_tail;
228 return n;
229}
230
ee0bab83 231/**
98629663
JS
232 * n_tty_write_wakeup - asynchronous I/O notifier
233 * @tty: tty device
ee0bab83 234 *
98629663
JS
235 * Required for the ptys, serial driver etc. since processes that attach
236 * themselves to the master and rely on ASYNC IO must be woken up.
ee0bab83 237 */
ee0bab83
PH
238static void n_tty_write_wakeup(struct tty_struct *tty)
239{
7bccc365
PH
240 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
241 kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
ee0bab83
PH
242}
243
4a23a4df 244static void n_tty_check_throttle(struct tty_struct *tty)
6367ca72 245{
a342846f
PH
246 struct n_tty_data *ldata = tty->disc_data;
247
6367ca72
PH
248 /*
249 * Check the remaining room for the input canonicalization
250 * mode. We don't want to throttle the driver if we're in
251 * canonical mode and don't have a newline yet!
252 */
a342846f
PH
253 if (ldata->icanon && ldata->canon_head == ldata->read_tail)
254 return;
255
6367ca72
PH
256 while (1) {
257 int throttled;
258 tty_set_flow_change(tty, TTY_THROTTLE_SAFE);
5e28cca1 259 if (N_TTY_BUF_SIZE - read_cnt(ldata) >= TTY_THRESHOLD_THROTTLE)
6367ca72
PH
260 break;
261 throttled = tty_throttle_safe(tty);
262 if (!throttled)
263 break;
264 }
265 __tty_set_flow_change(tty, 0);
266}
267
4b293492 268static void n_tty_check_unthrottle(struct tty_struct *tty)
6367ca72 269{
6d27a63c 270 if (tty->driver->type == TTY_DRIVER_TYPE_PTY) {
3afb1b39
PH
271 if (chars_in_buffer(tty) > TTY_THRESHOLD_UNTHROTTLE)
272 return;
2c5dc464 273 n_tty_kick_worker(tty);
6d27a63c 274 tty_wakeup(tty->link);
3afb1b39
PH
275 return;
276 }
277
6367ca72
PH
278 /* If there is enough space in the read buffer now, let the
279 * low-level driver know. We use chars_in_buffer() to
280 * check the buffer, as it now knows about canonical mode.
281 * Otherwise, if the driver is throttled and the line is
282 * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
283 * we won't get any more characters.
284 */
285
286 while (1) {
287 int unthrottled;
288 tty_set_flow_change(tty, TTY_UNTHROTTLE_SAFE);
289 if (chars_in_buffer(tty) > TTY_THRESHOLD_UNTHROTTLE)
290 break;
2c5dc464 291 n_tty_kick_worker(tty);
6367ca72
PH
292 unthrottled = tty_unthrottle_safe(tty);
293 if (!unthrottled)
294 break;
295 }
296 __tty_set_flow_change(tty, 0);
297}
298
17b82060 299/**
98629663
JS
300 * put_tty_queue - add character to tty
301 * @c: character
302 * @ldata: n_tty data
17b82060 303 *
98629663 304 * Add a character to the tty read_buf queue.
6d76bd26 305 *
98629663
JS
306 * Locking:
307 * * n_tty_receive_buf()/producer path:
308 * caller holds non-exclusive %termios_rwsem
17b82060 309 */
19e2ad6a 310static inline void put_tty_queue(unsigned char c, struct n_tty_data *ldata)
1da177e4 311{
8bfbe2de
CR
312 *read_buf_addr(ldata, ldata->read_head) = c;
313 ldata->read_head++;
1da177e4
LT
314}
315
1da177e4 316/**
98629663
JS
317 * reset_buffer_flags - reset buffer state
318 * @ldata: line disc data to reset
1da177e4 319 *
98629663
JS
320 * Reset the read buffer counters and clear the flags. Called from
321 * n_tty_open() and n_tty_flush_buffer().
17b82060 322 *
98629663
JS
323 * Locking:
324 * * caller holds exclusive %termios_rwsem, or
325 * * (locking is not required)
1da177e4 326 */
b66f4fa5 327static void reset_buffer_flags(struct n_tty_data *ldata)
1da177e4 328{
a73d3d69 329 ldata->read_head = ldata->canon_head = ldata->read_tail = 0;
70aca71f 330 ldata->commit_head = 0;
40d5e090 331 ldata->line_start = 0;
a88a69c9 332
a73d3d69 333 ldata->erasing = 0;
3fe780b3 334 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
4d0ed182 335 ldata->push = 0;
6bb6fa69
IJ
336
337 ldata->lookahead_count = 0;
1da177e4
LT
338}
339
a30737ab
PH
340static void n_tty_packet_mode_flush(struct tty_struct *tty)
341{
342 unsigned long flags;
343
64d608db
JS
344 if (tty->link->ctrl.packet) {
345 spin_lock_irqsave(&tty->ctrl.lock, flags);
346 tty->ctrl.pktstatus |= TIOCPKT_FLUSHREAD;
347 spin_unlock_irqrestore(&tty->ctrl.lock, flags);
e81107d4 348 wake_up_interruptible(&tty->link->read_wait);
a30737ab 349 }
a30737ab
PH
350}
351
1da177e4 352/**
98629663
JS
353 * n_tty_flush_buffer - clean input queue
354 * @tty: terminal device
1da177e4 355 *
98629663
JS
356 * Flush the input buffer. Called when the tty layer wants the buffer flushed
357 * (eg at hangup) or when the %N_TTY line discipline internally has to clean
358 * the pending queue (for example some signals).
1da177e4 359 *
98629663
JS
360 * Holds %termios_rwsem to exclude producer/consumer while buffer indices are
361 * reset.
6d76bd26 362 *
98629663 363 * Locking: %ctrl.lock, exclusive %termios_rwsem
1da177e4 364 */
4edf1827 365static void n_tty_flush_buffer(struct tty_struct *tty)
1da177e4 366{
6d76bd26 367 down_write(&tty->termios_rwsem);
b66f4fa5 368 reset_buffer_flags(tty->disc_data);
2c5dc464 369 n_tty_kick_worker(tty);
4edf1827 370
a30737ab
PH
371 if (tty->link)
372 n_tty_packet_mode_flush(tty);
6d76bd26 373 up_write(&tty->termios_rwsem);
1da177e4
LT
374}
375
1da177e4 376/**
98629663
JS
377 * is_utf8_continuation - utf8 multibyte check
378 * @c: byte to check
1da177e4 379 *
98629663
JS
380 * Returns: true if the utf8 character @c is a multibyte continuation
381 * character. We use this to correctly compute the on-screen size of the
382 * character when printing.
1da177e4 383 */
1da177e4
LT
384static inline int is_utf8_continuation(unsigned char c)
385{
386 return (c & 0xc0) == 0x80;
387}
388
389/**
98629663
JS
390 * is_continuation - multibyte check
391 * @c: byte to check
392 * @tty: terminal device
1da177e4 393 *
98629663
JS
394 * Returns: true if the utf8 character @c is a multibyte continuation character
395 * and the terminal is in unicode mode.
1da177e4 396 */
5bedcf70 397static inline int is_continuation(unsigned char c, const struct tty_struct *tty)
1da177e4
LT
398{
399 return I_IUTF8(tty) && is_utf8_continuation(c);
400}
401
402/**
98629663
JS
403 * do_output_char - output one character
404 * @c: character (or partial unicode symbol)
405 * @tty: terminal device
406 * @space: space available in tty driver write buffer
1da177e4 407 *
98629663
JS
408 * This is a helper function that handles one output character (including
409 * special characters like TAB, CR, LF, etc.), doing OPOST processing and
410 * putting the results in the tty driver's write buffer.
a88a69c9 411 *
98629663
JS
412 * Note that Linux currently ignores TABDLY, CRDLY, VTDLY, FFDLY and NLDLY.
413 * They simply aren't relevant in the world today. If you ever need them, add
414 * them here.
1da177e4 415 *
98629663 416 * Returns: the number of bytes of buffer space used or -1 if no space left.
a88a69c9 417 *
98629663
JS
418 * Locking: should be called under the %output_lock to protect the column state
419 * and space left in the buffer.
1da177e4 420 */
a88a69c9 421static int do_output_char(unsigned char c, struct tty_struct *tty, int space)
1da177e4 422{
53c5ee2c 423 struct n_tty_data *ldata = tty->disc_data;
a88a69c9 424 int spaces;
1da177e4 425
1da177e4
LT
426 if (!space)
427 return -1;
300a6204 428
a88a69c9
JP
429 switch (c) {
430 case '\n':
431 if (O_ONLRET(tty))
53c5ee2c 432 ldata->column = 0;
a88a69c9
JP
433 if (O_ONLCR(tty)) {
434 if (space < 2)
435 return -1;
ba2e68ac 436 ldata->canon_column = ldata->column = 0;
37f81fa1 437 tty->ops->write(tty, "\r\n", 2);
a88a69c9
JP
438 return 2;
439 }
ba2e68ac 440 ldata->canon_column = ldata->column;
a88a69c9
JP
441 break;
442 case '\r':
53c5ee2c 443 if (O_ONOCR(tty) && ldata->column == 0)
a88a69c9
JP
444 return 0;
445 if (O_OCRNL(tty)) {
446 c = '\n';
447 if (O_ONLRET(tty))
ba2e68ac 448 ldata->canon_column = ldata->column = 0;
1da177e4 449 break;
a88a69c9 450 }
ba2e68ac 451 ldata->canon_column = ldata->column = 0;
a88a69c9
JP
452 break;
453 case '\t':
53c5ee2c 454 spaces = 8 - (ldata->column & 7);
a88a69c9
JP
455 if (O_TABDLY(tty) == XTABS) {
456 if (space < spaces)
457 return -1;
53c5ee2c 458 ldata->column += spaces;
a88a69c9
JP
459 tty->ops->write(tty, " ", spaces);
460 return spaces;
1da177e4 461 }
53c5ee2c 462 ldata->column += spaces;
a88a69c9
JP
463 break;
464 case '\b':
53c5ee2c
JS
465 if (ldata->column > 0)
466 ldata->column--;
a88a69c9
JP
467 break;
468 default:
a59c0d6f
JP
469 if (!iscntrl(c)) {
470 if (O_OLCUC(tty))
471 c = toupper(c);
472 if (!is_continuation(c, tty))
53c5ee2c 473 ldata->column++;
a59c0d6f 474 }
a88a69c9 475 break;
1da177e4 476 }
a88a69c9 477
f34d7a5b 478 tty_put_char(tty, c);
a88a69c9
JP
479 return 1;
480}
481
482/**
98629663
JS
483 * process_output - output post processor
484 * @c: character (or partial unicode symbol)
485 * @tty: terminal device
486 *
487 * Output one character with OPOST processing.
a88a69c9 488 *
98629663
JS
489 * Returns: -1 when the output device is full and the character must be
490 * retried.
a88a69c9 491 *
98629663
JS
492 * Locking: %output_lock to protect column state and space left (also, this is
493 *called from n_tty_write() under the tty layer write lock).
a88a69c9 494 */
a88a69c9
JP
495static int process_output(unsigned char c, struct tty_struct *tty)
496{
bddc7152 497 struct n_tty_data *ldata = tty->disc_data;
a88a69c9
JP
498 int space, retval;
499
bddc7152 500 mutex_lock(&ldata->output_lock);
a88a69c9
JP
501
502 space = tty_write_room(tty);
503 retval = do_output_char(c, tty, space);
504
bddc7152 505 mutex_unlock(&ldata->output_lock);
a88a69c9
JP
506 if (retval < 0)
507 return -1;
508 else
509 return 0;
1da177e4
LT
510}
511
512/**
98629663
JS
513 * process_output_block - block post processor
514 * @tty: terminal device
515 * @buf: character buffer
516 * @nr: number of bytes to output
517 *
518 * Output a block of characters with OPOST processing.
519 *
520 * This path is used to speed up block console writes, among other things when
521 * processing blocks of output data. It handles only the simple cases normally
522 * found and helps to generate blocks of symbols for the console driver and
523 * thus improve performance.
524 *
525 * Returns: the number of characters output.
526 *
527 * Locking: %output_lock to protect column state and space left (also, this is
528 * called from n_tty_write() under the tty layer write lock).
1da177e4 529 */
a88a69c9
JP
530static ssize_t process_output_block(struct tty_struct *tty,
531 const unsigned char *buf, unsigned int nr)
1da177e4 532{
53c5ee2c 533 struct n_tty_data *ldata = tty->disc_data;
1da177e4 534 int space;
bbd20759 535 int i;
1da177e4
LT
536 const unsigned char *cp;
537
bddc7152 538 mutex_lock(&ldata->output_lock);
a88a69c9 539
f34d7a5b 540 space = tty_write_room(tty);
9ef8927f 541 if (space <= 0) {
bddc7152 542 mutex_unlock(&ldata->output_lock);
9ef8927f 543 return space;
a88a69c9 544 }
1da177e4
LT
545 if (nr > space)
546 nr = space;
547
548 for (i = 0, cp = buf; i < nr; i++, cp++) {
a59c0d6f
JP
549 unsigned char c = *cp;
550
551 switch (c) {
1da177e4
LT
552 case '\n':
553 if (O_ONLRET(tty))
53c5ee2c 554 ldata->column = 0;
1da177e4
LT
555 if (O_ONLCR(tty))
556 goto break_out;
ba2e68ac 557 ldata->canon_column = ldata->column;
1da177e4
LT
558 break;
559 case '\r':
53c5ee2c 560 if (O_ONOCR(tty) && ldata->column == 0)
1da177e4
LT
561 goto break_out;
562 if (O_OCRNL(tty))
563 goto break_out;
ba2e68ac 564 ldata->canon_column = ldata->column = 0;
1da177e4
LT
565 break;
566 case '\t':
567 goto break_out;
568 case '\b':
53c5ee2c
JS
569 if (ldata->column > 0)
570 ldata->column--;
1da177e4
LT
571 break;
572 default:
a59c0d6f
JP
573 if (!iscntrl(c)) {
574 if (O_OLCUC(tty))
575 goto break_out;
576 if (!is_continuation(c, tty))
53c5ee2c 577 ldata->column++;
a59c0d6f 578 }
1da177e4
LT
579 break;
580 }
581 }
582break_out:
f34d7a5b 583 i = tty->ops->write(tty, buf, i);
a88a69c9 584
bddc7152 585 mutex_unlock(&ldata->output_lock);
1da177e4
LT
586 return i;
587}
588
a88a69c9 589/**
98629663
JS
590 * __process_echoes - write pending echo characters
591 * @tty: terminal device
a88a69c9 592 *
98629663
JS
593 * Write previously buffered echo (and other ldisc-generated) characters to the
594 * tty.
a88a69c9 595 *
98629663
JS
596 * Characters generated by the ldisc (including echoes) need to be buffered
597 * because the driver's write buffer can fill during heavy program output.
598 * Echoing straight to the driver will often fail under these conditions,
599 * causing lost characters and resulting mismatches of ldisc state information.
a88a69c9 600 *
98629663
JS
601 * Since the ldisc state must represent the characters actually sent to the
602 * driver at the time of the write, operations like certain changes in column
603 * state are also saved in the buffer and executed here.
a88a69c9 604 *
98629663
JS
605 * A circular fifo buffer is used so that the most recent characters are
606 * prioritized. Also, when control characters are echoed with a prefixed "^",
607 * the pair is treated atomically and thus not separated.
a88a69c9 608 *
98629663 609 * Locking: callers must hold %output_lock.
a88a69c9 610 */
bc5b1ec5 611static size_t __process_echoes(struct tty_struct *tty)
a88a69c9 612{
53c5ee2c 613 struct n_tty_data *ldata = tty->disc_data;
bc5b1ec5 614 int space, old_space;
addaebcc 615 size_t tail;
a88a69c9 616 unsigned char c;
a88a69c9 617
bc5b1ec5 618 old_space = space = tty_write_room(tty);
a88a69c9 619
addaebcc 620 tail = ldata->echo_tail;
ebec3f8f 621 while (MASK(ldata->echo_commit) != MASK(tail)) {
addaebcc 622 c = echo_buf(ldata, tail);
a88a69c9
JP
623 if (c == ECHO_OP_START) {
624 unsigned char op;
b8abba0e 625 bool space_left = true;
a88a69c9 626
ebec3f8f
TH
627 /*
628 * Since add_echo_byte() is called without holding
629 * output_lock, we might see only portion of multi-byte
630 * operation.
631 */
632 if (MASK(ldata->echo_commit) == MASK(tail + 1))
633 goto not_yet_stored;
a88a69c9
JP
634 /*
635 * If the buffer byte is the start of a multi-byte
636 * operation, get the next byte, which is either the
637 * op code or a control character value.
638 */
addaebcc 639 op = echo_buf(ldata, tail + 1);
300a6204 640
a88a69c9 641 switch (op) {
e24cd4e6 642 case ECHO_OP_ERASE_TAB: {
a88a69c9
JP
643 unsigned int num_chars, num_bs;
644
ebec3f8f
TH
645 if (MASK(ldata->echo_commit) == MASK(tail + 2))
646 goto not_yet_stored;
addaebcc 647 num_chars = echo_buf(ldata, tail + 2);
a88a69c9
JP
648
649 /*
650 * Determine how many columns to go back
651 * in order to erase the tab.
652 * This depends on the number of columns
653 * used by other characters within the tab
654 * area. If this (modulo 8) count is from
655 * the start of input rather than from a
656 * previous tab, we offset by canon column.
657 * Otherwise, tab spacing is normal.
658 */
659 if (!(num_chars & 0x80))
ba2e68ac 660 num_chars += ldata->canon_column;
a88a69c9
JP
661 num_bs = 8 - (num_chars & 7);
662
663 if (num_bs > space) {
b8abba0e 664 space_left = false;
a88a69c9
JP
665 break;
666 }
667 space -= num_bs;
668 while (num_bs--) {
669 tty_put_char(tty, '\b');
53c5ee2c
JS
670 if (ldata->column > 0)
671 ldata->column--;
a88a69c9 672 }
addaebcc 673 tail += 3;
a88a69c9 674 break;
e24cd4e6 675 }
a88a69c9 676 case ECHO_OP_SET_CANON_COL:
ba2e68ac 677 ldata->canon_column = ldata->column;
addaebcc 678 tail += 2;
a88a69c9
JP
679 break;
680
681 case ECHO_OP_MOVE_BACK_COL:
53c5ee2c
JS
682 if (ldata->column > 0)
683 ldata->column--;
addaebcc 684 tail += 2;
a88a69c9
JP
685 break;
686
687 case ECHO_OP_START:
688 /* This is an escaped echo op start code */
689 if (!space) {
b8abba0e 690 space_left = false;
a88a69c9
JP
691 break;
692 }
693 tty_put_char(tty, ECHO_OP_START);
53c5ee2c 694 ldata->column++;
a88a69c9 695 space--;
addaebcc 696 tail += 2;
a88a69c9
JP
697 break;
698
699 default:
a88a69c9 700 /*
62b26358
JP
701 * If the op is not a special byte code,
702 * it is a ctrl char tagged to be echoed
703 * as "^X" (where X is the letter
704 * representing the control char).
705 * Note that we must ensure there is
706 * enough space for the whole ctrl pair.
707 *
a88a69c9 708 */
62b26358 709 if (space < 2) {
b8abba0e 710 space_left = false;
62b26358
JP
711 break;
712 }
713 tty_put_char(tty, '^');
714 tty_put_char(tty, op ^ 0100);
53c5ee2c 715 ldata->column += 2;
62b26358 716 space -= 2;
addaebcc 717 tail += 2;
a88a69c9
JP
718 }
719
b8abba0e 720 if (!space_left)
a88a69c9
JP
721 break;
722 } else {
582f5590 723 if (O_OPOST(tty)) {
ee5aa7b8
JP
724 int retval = do_output_char(c, tty, space);
725 if (retval < 0)
726 break;
727 space -= retval;
728 } else {
729 if (!space)
730 break;
731 tty_put_char(tty, c);
732 space -= 1;
733 }
addaebcc 734 tail += 1;
a88a69c9 735 }
a88a69c9
JP
736 }
737
cbfd0340
PH
738 /* If the echo buffer is nearly full (so that the possibility exists
739 * of echo overrun before the next commit), then discard enough
740 * data at the tail to prevent a subsequent overrun */
ebec3f8f
TH
741 while (ldata->echo_commit > tail &&
742 ldata->echo_commit - tail >= ECHO_DISCARD_WATERMARK) {
c476f658 743 if (echo_buf(ldata, tail) == ECHO_OP_START) {
6f222536 744 if (echo_buf(ldata, tail + 1) == ECHO_OP_ERASE_TAB)
cbfd0340
PH
745 tail += 3;
746 else
747 tail += 2;
748 } else
749 tail++;
750 }
751
ebec3f8f 752 not_yet_stored:
addaebcc 753 ldata->echo_tail = tail;
bc5b1ec5 754 return old_space - space;
019ebdf9
PH
755}
756
757static void commit_echoes(struct tty_struct *tty)
758{
759 struct n_tty_data *ldata = tty->disc_data;
bc5b1ec5 760 size_t nr, old, echoed;
cbfd0340
PH
761 size_t head;
762
ebec3f8f 763 mutex_lock(&ldata->output_lock);
cbfd0340 764 head = ldata->echo_head;
1075a6e2 765 ldata->echo_mark = head;
cbfd0340
PH
766 old = ldata->echo_commit - ldata->echo_tail;
767
768 /* Process committed echoes if the accumulated # of bytes
769 * is over the threshold (and try again each time another
770 * block is accumulated) */
771 nr = head - ldata->echo_tail;
ebec3f8f
TH
772 if (nr < ECHO_COMMIT_WATERMARK ||
773 (nr % ECHO_BLOCK > old % ECHO_BLOCK)) {
774 mutex_unlock(&ldata->output_lock);
cbfd0340 775 return;
ebec3f8f 776 }
a88a69c9 777
cbfd0340 778 ldata->echo_commit = head;
bc5b1ec5 779 echoed = __process_echoes(tty);
bddc7152 780 mutex_unlock(&ldata->output_lock);
a88a69c9 781
bc5b1ec5 782 if (echoed && tty->ops->flush_chars)
a88a69c9
JP
783 tty->ops->flush_chars(tty);
784}
785
019ebdf9 786static void process_echoes(struct tty_struct *tty)
17bd7907
PH
787{
788 struct n_tty_data *ldata = tty->disc_data;
bc5b1ec5 789 size_t echoed;
17bd7907 790
e2613be5 791 if (ldata->echo_mark == ldata->echo_tail)
019ebdf9
PH
792 return;
793
794 mutex_lock(&ldata->output_lock);
1075a6e2 795 ldata->echo_commit = ldata->echo_mark;
bc5b1ec5 796 echoed = __process_echoes(tty);
019ebdf9
PH
797 mutex_unlock(&ldata->output_lock);
798
bc5b1ec5 799 if (echoed && tty->ops->flush_chars)
019ebdf9 800 tty->ops->flush_chars(tty);
17bd7907
PH
801}
802
1075a6e2 803/* NB: echo_mark and echo_head should be equivalent here */
cbfd0340
PH
804static void flush_echoes(struct tty_struct *tty)
805{
806 struct n_tty_data *ldata = tty->disc_data;
807
39434abd
PH
808 if ((!L_ECHO(tty) && !L_ECHONL(tty)) ||
809 ldata->echo_commit == ldata->echo_head)
cbfd0340
PH
810 return;
811
812 mutex_lock(&ldata->output_lock);
813 ldata->echo_commit = ldata->echo_head;
814 __process_echoes(tty);
815 mutex_unlock(&ldata->output_lock);
816}
817
a88a69c9 818/**
98629663
JS
819 * add_echo_byte - add a byte to the echo buffer
820 * @c: unicode byte to echo
821 * @ldata: n_tty data
a88a69c9 822 *
98629663 823 * Add a character or operation byte to the echo buffer.
a88a69c9 824 */
cbfd0340 825static inline void add_echo_byte(unsigned char c, struct n_tty_data *ldata)
a88a69c9 826{
ebec3f8f
TH
827 *echo_buf_addr(ldata, ldata->echo_head) = c;
828 smp_wmb(); /* Matches smp_rmb() in echo_buf(). */
829 ldata->echo_head++;
a88a69c9
JP
830}
831
832/**
98629663
JS
833 * echo_move_back_col - add operation to move back a column
834 * @ldata: n_tty data
a88a69c9 835 *
98629663 836 * Add an operation to the echo buffer to move back one column.
a88a69c9 837 */
57c94121 838static void echo_move_back_col(struct n_tty_data *ldata)
a88a69c9 839{
57c94121
JS
840 add_echo_byte(ECHO_OP_START, ldata);
841 add_echo_byte(ECHO_OP_MOVE_BACK_COL, ldata);
a88a69c9
JP
842}
843
844/**
98629663
JS
845 * echo_set_canon_col - add operation to set the canon column
846 * @ldata: n_tty data
a88a69c9 847 *
98629663
JS
848 * Add an operation to the echo buffer to set the canon column to the current
849 * column.
a88a69c9 850 */
57c94121 851static void echo_set_canon_col(struct n_tty_data *ldata)
a88a69c9 852{
57c94121
JS
853 add_echo_byte(ECHO_OP_START, ldata);
854 add_echo_byte(ECHO_OP_SET_CANON_COL, ldata);
a88a69c9
JP
855}
856
857/**
98629663
JS
858 * echo_erase_tab - add operation to erase a tab
859 * @num_chars: number of character columns already used
860 * @after_tab: true if num_chars starts after a previous tab
861 * @ldata: n_tty data
862 *
863 * Add an operation to the echo buffer to erase a tab.
864 *
865 * Called by the eraser function, which knows how many character columns have
866 * been used since either a previous tab or the start of input. This
867 * information will be used later, along with canon column (if applicable), to
868 * go back the correct number of columns.
a88a69c9 869 */
a88a69c9 870static void echo_erase_tab(unsigned int num_chars, int after_tab,
57c94121 871 struct n_tty_data *ldata)
a88a69c9 872{
57c94121
JS
873 add_echo_byte(ECHO_OP_START, ldata);
874 add_echo_byte(ECHO_OP_ERASE_TAB, ldata);
a88a69c9
JP
875
876 /* We only need to know this modulo 8 (tab spacing) */
877 num_chars &= 7;
878
879 /* Set the high bit as a flag if num_chars is after a previous tab */
880 if (after_tab)
881 num_chars |= 0x80;
300a6204 882
57c94121 883 add_echo_byte(num_chars, ldata);
a88a69c9
JP
884}
885
886/**
98629663
JS
887 * echo_char_raw - echo a character raw
888 * @c: unicode byte to echo
889 * @ldata: line disc data
a88a69c9 890 *
98629663
JS
891 * Echo user input back onto the screen. This must be called only when
892 * L_ECHO(tty) is true. Called from the &tty_driver.receive_buf() path.
a88a69c9 893 *
98629663 894 * This variant does not treat control characters specially.
a88a69c9 895 */
57c94121 896static void echo_char_raw(unsigned char c, struct n_tty_data *ldata)
a88a69c9 897{
a88a69c9 898 if (c == ECHO_OP_START) {
57c94121
JS
899 add_echo_byte(ECHO_OP_START, ldata);
900 add_echo_byte(ECHO_OP_START, ldata);
a88a69c9 901 } else {
57c94121 902 add_echo_byte(c, ldata);
a88a69c9 903 }
a88a69c9 904}
1da177e4 905
1da177e4 906/**
98629663
JS
907 * echo_char - echo a character
908 * @c: unicode byte to echo
909 * @tty: terminal device
1da177e4 910 *
98629663
JS
911 * Echo user input back onto the screen. This must be called only when
912 * L_ECHO(tty) is true. Called from the &tty_driver.receive_buf() path.
17b82060 913 *
98629663
JS
914 * This variant tags control characters to be echoed as "^X" (where X is the
915 * letter representing the control char).
1da177e4 916 */
5bedcf70 917static void echo_char(unsigned char c, const struct tty_struct *tty)
1da177e4 918{
bddc7152
JS
919 struct n_tty_data *ldata = tty->disc_data;
920
a88a69c9 921 if (c == ECHO_OP_START) {
57c94121
JS
922 add_echo_byte(ECHO_OP_START, ldata);
923 add_echo_byte(ECHO_OP_START, ldata);
a88a69c9 924 } else {
62b26358 925 if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t')
57c94121
JS
926 add_echo_byte(ECHO_OP_START, ldata);
927 add_echo_byte(c, ldata);
a88a69c9 928 }
1da177e4
LT
929}
930
17b82060 931/**
98629663
JS
932 * finish_erasing - complete erase
933 * @ldata: n_tty data
17b82060 934 */
57c94121 935static inline void finish_erasing(struct n_tty_data *ldata)
1da177e4 936{
53c5ee2c 937 if (ldata->erasing) {
57c94121 938 echo_char_raw('/', ldata);
53c5ee2c 939 ldata->erasing = 0;
1da177e4
LT
940 }
941}
942
943/**
98629663
JS
944 * eraser - handle erase function
945 * @c: character input
946 * @tty: terminal device
1da177e4 947 *
98629663
JS
948 * Perform erase and necessary output when an erase character is present in the
949 * stream from the driver layer. Handles the complexities of UTF-8 multibyte
950 * symbols.
17b82060 951 *
98629663
JS
952 * Locking: n_tty_receive_buf()/producer path:
953 * caller holds non-exclusive %termios_rwsem
1da177e4 954 */
5bedcf70 955static void eraser(unsigned char c, const struct tty_struct *tty)
1da177e4 956{
53c5ee2c 957 struct n_tty_data *ldata = tty->disc_data;
1da177e4 958 enum { ERASE, WERASE, KILL } kill_type;
bc5a5e3f
PH
959 size_t head;
960 size_t cnt;
961 int seen_alnums;
1da177e4 962
ba2e68ac 963 if (ldata->read_head == ldata->canon_head) {
7e94b1d9 964 /* process_output('\a', tty); */ /* what do you think? */
1da177e4
LT
965 return;
966 }
967 if (c == ERASE_CHAR(tty))
968 kill_type = ERASE;
969 else if (c == WERASE_CHAR(tty))
970 kill_type = WERASE;
971 else {
972 if (!L_ECHO(tty)) {
ba2e68ac 973 ldata->read_head = ldata->canon_head;
1da177e4
LT
974 return;
975 }
976 if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
ba2e68ac 977 ldata->read_head = ldata->canon_head;
57c94121 978 finish_erasing(ldata);
1da177e4
LT
979 echo_char(KILL_CHAR(tty), tty);
980 /* Add a newline if ECHOK is on and ECHOKE is off. */
981 if (L_ECHOK(tty))
57c94121 982 echo_char_raw('\n', ldata);
1da177e4
LT
983 return;
984 }
985 kill_type = KILL;
986 }
987
988 seen_alnums = 0;
3d63b7e4 989 while (MASK(ldata->read_head) != MASK(ldata->canon_head)) {
ba2e68ac 990 head = ldata->read_head;
1da177e4
LT
991
992 /* erase a single possibly multibyte character */
993 do {
bc5a5e3f
PH
994 head--;
995 c = read_buf(ldata, head);
3d63b7e4
TH
996 } while (is_continuation(c, tty) &&
997 MASK(head) != MASK(ldata->canon_head));
1da177e4
LT
998
999 /* do not partially erase */
1000 if (is_continuation(c, tty))
1001 break;
1002
1003 if (kill_type == WERASE) {
1004 /* Equivalent to BSD's ALTWERASE. */
1005 if (isalnum(c) || c == '_')
1006 seen_alnums++;
1007 else if (seen_alnums)
1008 break;
1009 }
bc5a5e3f 1010 cnt = ldata->read_head - head;
ba2e68ac 1011 ldata->read_head = head;
1da177e4
LT
1012 if (L_ECHO(tty)) {
1013 if (L_ECHOPRT(tty)) {
53c5ee2c 1014 if (!ldata->erasing) {
57c94121 1015 echo_char_raw('\\', ldata);
53c5ee2c 1016 ldata->erasing = 1;
1da177e4
LT
1017 }
1018 /* if cnt > 1, output a multi-byte character */
1019 echo_char(c, tty);
1020 while (--cnt > 0) {
bc5a5e3f
PH
1021 head++;
1022 echo_char_raw(read_buf(ldata, head), ldata);
57c94121 1023 echo_move_back_col(ldata);
1da177e4
LT
1024 }
1025 } else if (kill_type == ERASE && !L_ECHOE(tty)) {
1026 echo_char(ERASE_CHAR(tty), tty);
1027 } else if (c == '\t') {
a88a69c9
JP
1028 unsigned int num_chars = 0;
1029 int after_tab = 0;
bc5a5e3f 1030 size_t tail = ldata->read_head;
a88a69c9
JP
1031
1032 /*
1033 * Count the columns used for characters
1034 * since the start of input or after a
1035 * previous tab.
1036 * This info is used to go back the correct
1037 * number of columns.
1038 */
3d63b7e4 1039 while (MASK(tail) != MASK(ldata->canon_head)) {
bc5a5e3f
PH
1040 tail--;
1041 c = read_buf(ldata, tail);
a88a69c9
JP
1042 if (c == '\t') {
1043 after_tab = 1;
1044 break;
300a6204 1045 } else if (iscntrl(c)) {
1da177e4 1046 if (L_ECHOCTL(tty))
a88a69c9
JP
1047 num_chars += 2;
1048 } else if (!is_continuation(c, tty)) {
1049 num_chars++;
1050 }
1da177e4 1051 }
57c94121 1052 echo_erase_tab(num_chars, after_tab, ldata);
1da177e4
LT
1053 } else {
1054 if (iscntrl(c) && L_ECHOCTL(tty)) {
57c94121
JS
1055 echo_char_raw('\b', ldata);
1056 echo_char_raw(' ', ldata);
1057 echo_char_raw('\b', ldata);
1da177e4
LT
1058 }
1059 if (!iscntrl(c) || L_ECHOCTL(tty)) {
57c94121
JS
1060 echo_char_raw('\b', ldata);
1061 echo_char_raw(' ', ldata);
1062 echo_char_raw('\b', ldata);
1da177e4
LT
1063 }
1064 }
1065 }
1066 if (kill_type == ERASE)
1067 break;
1068 }
ba2e68ac 1069 if (ldata->read_head == ldata->canon_head && L_ECHO(tty))
57c94121 1070 finish_erasing(ldata);
1da177e4
LT
1071}
1072
c66453ce
JS
1073
1074static void __isig(int sig, struct tty_struct *tty)
1075{
1076 struct pid *tty_pgrp = tty_get_pgrp(tty);
1077 if (tty_pgrp) {
1078 kill_pgrp(tty_pgrp, sig, 1);
1079 put_pid(tty_pgrp);
1080 }
1081}
1082
1da177e4 1083/**
98629663
JS
1084 * isig - handle the ISIG optio
1085 * @sig: signal
1086 * @tty: terminal
1da177e4 1087 *
98629663
JS
1088 * Called when a signal is being sent due to terminal input. Called from the
1089 * &tty_driver.receive_buf() path, so serialized.
17b82060 1090 *
98629663
JS
1091 * Performs input and output flush if !NOFLSH. In this context, the echo
1092 * buffer is 'output'. The signal is processed first to alert any current
1093 * readers or writers to discontinue and exit their i/o loops.
d2b6f447 1094 *
98629663 1095 * Locking: %ctrl.lock
1da177e4 1096 */
3b19e032
PH
1097static void isig(int sig, struct tty_struct *tty)
1098{
1099 struct n_tty_data *ldata = tty->disc_data;
1100
1101 if (L_NOFLSH(tty)) {
1102 /* signal only */
1103 __isig(sig, tty);
1104
1105 } else { /* signal and flush */
d2b6f447
PH
1106 up_read(&tty->termios_rwsem);
1107 down_write(&tty->termios_rwsem);
1108
3b19e032
PH
1109 __isig(sig, tty);
1110
d2b6f447
PH
1111 /* clear echo buffer */
1112 mutex_lock(&ldata->output_lock);
1113 ldata->echo_head = ldata->echo_tail = 0;
1114 ldata->echo_mark = ldata->echo_commit = 0;
1115 mutex_unlock(&ldata->output_lock);
1116
1117 /* clear output buffer */
1118 tty_driver_flush_buffer(tty);
1119
1120 /* clear input buffer */
1121 reset_buffer_flags(tty->disc_data);
1122
1123 /* notify pty master of flush */
1124 if (tty->link)
1125 n_tty_packet_mode_flush(tty);
1126
1127 up_write(&tty->termios_rwsem);
1128 down_read(&tty->termios_rwsem);
1129 }
1da177e4
LT
1130}
1131
1132/**
98629663
JS
1133 * n_tty_receive_break - handle break
1134 * @tty: terminal
1da177e4 1135 *
98629663
JS
1136 * An RS232 break event has been hit in the incoming bitstream. This can cause
1137 * a variety of events depending upon the termios settings.
1da177e4 1138 *
98629663
JS
1139 * Locking: n_tty_receive_buf()/producer path:
1140 * caller holds non-exclusive termios_rwsem
6d76bd26 1141 *
98629663 1142 * Note: may get exclusive %termios_rwsem if flushing input buffer
1da177e4 1143 */
4b293492 1144static void n_tty_receive_break(struct tty_struct *tty)
1da177e4 1145{
57c94121
JS
1146 struct n_tty_data *ldata = tty->disc_data;
1147
1da177e4
LT
1148 if (I_IGNBRK(tty))
1149 return;
1150 if (I_BRKINT(tty)) {
8c985d18 1151 isig(SIGINT, tty);
1da177e4
LT
1152 return;
1153 }
1154 if (I_PARMRK(tty)) {
57c94121
JS
1155 put_tty_queue('\377', ldata);
1156 put_tty_queue('\0', ldata);
1da177e4 1157 }
57c94121 1158 put_tty_queue('\0', ldata);
1da177e4
LT
1159}
1160
1161/**
98629663
JS
1162 * n_tty_receive_overrun - handle overrun reporting
1163 * @tty: terminal
1da177e4 1164 *
98629663
JS
1165 * Data arrived faster than we could process it. While the tty driver has
1166 * flagged this the bits that were missed are gone forever.
1da177e4 1167 *
98629663
JS
1168 * Called from the receive_buf path so single threaded. Does not need locking
1169 * as num_overrun and overrun_time are function private.
1da177e4 1170 */
5bedcf70 1171static void n_tty_receive_overrun(const struct tty_struct *tty)
1da177e4 1172{
53c5ee2c 1173 struct n_tty_data *ldata = tty->disc_data;
1da177e4 1174
53c5ee2c
JS
1175 ldata->num_overrun++;
1176 if (time_after(jiffies, ldata->overrun_time + HZ) ||
37e8b08a 1177 time_after(ldata->overrun_time, jiffies)) {
339f36ba 1178 tty_warn(tty, "%d input overrun(s)\n", ldata->num_overrun);
53c5ee2c
JS
1179 ldata->overrun_time = jiffies;
1180 ldata->num_overrun = 0;
1da177e4
LT
1181 }
1182}
1183
1184/**
98629663
JS
1185 * n_tty_receive_parity_error - error notifier
1186 * @tty: terminal device
1187 * @c: character
1da177e4 1188 *
98629663
JS
1189 * Process a parity error and queue the right data to indicate the error case
1190 * if necessary.
6d76bd26 1191 *
98629663
JS
1192 * Locking: n_tty_receive_buf()/producer path:
1193 * caller holds non-exclusive %termios_rwsem
1da177e4 1194 */
5bedcf70
JS
1195static void n_tty_receive_parity_error(const struct tty_struct *tty,
1196 unsigned char c)
1da177e4 1197{
57c94121
JS
1198 struct n_tty_data *ldata = tty->disc_data;
1199
66528f90
PH
1200 if (I_INPCK(tty)) {
1201 if (I_IGNPAR(tty))
1202 return;
1203 if (I_PARMRK(tty)) {
1204 put_tty_queue('\377', ldata);
1205 put_tty_queue('\0', ldata);
1206 put_tty_queue(c, ldata);
1207 } else
1208 put_tty_queue('\0', ldata);
1209 } else
57c94121 1210 put_tty_queue(c, ldata);
1da177e4
LT
1211}
1212
b0ac50be
PH
1213static void
1214n_tty_receive_signal_char(struct tty_struct *tty, int signal, unsigned char c)
1215{
d2b6f447 1216 isig(signal, tty);
b0ac50be
PH
1217 if (I_IXON(tty))
1218 start_tty(tty);
1219 if (L_ECHO(tty)) {
1220 echo_char(c, tty);
1221 commit_echoes(tty);
e2613be5
PH
1222 } else
1223 process_echoes(tty);
b0ac50be
PH
1224}
1225
25e02ba6
IJ
1226static bool n_tty_is_char_flow_ctrl(struct tty_struct *tty, unsigned char c)
1227{
1228 return c == START_CHAR(tty) || c == STOP_CHAR(tty);
1229}
1230
6bb6fa69
IJ
1231/**
1232 * n_tty_receive_char_flow_ctrl - receive flow control chars
1233 * @tty: terminal device
1234 * @c: character
1235 * @lookahead_done: lookahead has processed this character already
1236 *
1237 * Receive and process flow control character actions.
1238 *
1239 * In case lookahead for flow control chars already handled the character in
1240 * advance to the normal receive, the actions are skipped during normal
1241 * receive.
1242 *
1243 * Returns true if @c is consumed as flow-control character, the character
1244 * must not be treated as normal character.
1245 */
1246static bool n_tty_receive_char_flow_ctrl(struct tty_struct *tty, unsigned char c,
1247 bool lookahead_done)
ec66b8cf 1248{
25e02ba6
IJ
1249 if (!n_tty_is_char_flow_ctrl(tty, c))
1250 return false;
1251
6bb6fa69
IJ
1252 if (lookahead_done)
1253 return true;
1254
ec66b8cf
IJ
1255 if (c == START_CHAR(tty)) {
1256 start_tty(tty);
1257 process_echoes(tty);
1258 return true;
1259 }
ec66b8cf 1260
25e02ba6
IJ
1261 /* STOP_CHAR */
1262 stop_tty(tty);
1263 return true;
ec66b8cf
IJ
1264}
1265
6bb6fa69
IJ
1266static void n_tty_receive_char_special(struct tty_struct *tty, unsigned char c,
1267 bool lookahead_done)
1da177e4 1268{
53c5ee2c 1269 struct n_tty_data *ldata = tty->disc_data;
1da177e4 1270
6bb6fa69 1271 if (I_IXON(tty) && n_tty_receive_char_flow_ctrl(tty, c, lookahead_done))
ec66b8cf 1272 return;
575537b3 1273
1da177e4 1274 if (L_ISIG(tty)) {
b0ac50be
PH
1275 if (c == INTR_CHAR(tty)) {
1276 n_tty_receive_signal_char(tty, SIGINT, c);
16765365 1277 return;
b0ac50be
PH
1278 } else if (c == QUIT_CHAR(tty)) {
1279 n_tty_receive_signal_char(tty, SIGQUIT, c);
16765365 1280 return;
b0ac50be
PH
1281 } else if (c == SUSP_CHAR(tty)) {
1282 n_tty_receive_signal_char(tty, SIGTSTP, c);
16765365 1283 return;
1da177e4
LT
1284 }
1285 }
575537b3 1286
6e94dbc7 1287 if (tty->flow.stopped && !tty->flow.tco_stopped && I_IXON(tty) && I_IXANY(tty)) {
855df3c0
PH
1288 start_tty(tty);
1289 process_echoes(tty);
1290 }
1291
575537b3
JP
1292 if (c == '\r') {
1293 if (I_IGNCR(tty))
16765365 1294 return;
575537b3
JP
1295 if (I_ICRNL(tty))
1296 c = '\n';
1297 } else if (c == '\n' && I_INLCR(tty))
1298 c = '\r';
1299
53c5ee2c 1300 if (ldata->icanon) {
1da177e4
LT
1301 if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
1302 (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
1303 eraser(c, tty);
17bd7907 1304 commit_echoes(tty);
16765365 1305 return;
1da177e4
LT
1306 }
1307 if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
53c5ee2c 1308 ldata->lnext = 1;
1da177e4 1309 if (L_ECHO(tty)) {
57c94121 1310 finish_erasing(ldata);
1da177e4 1311 if (L_ECHOCTL(tty)) {
57c94121
JS
1312 echo_char_raw('^', ldata);
1313 echo_char_raw('\b', ldata);
17bd7907 1314 commit_echoes(tty);
1da177e4
LT
1315 }
1316 }
16765365 1317 return;
1da177e4 1318 }
e60d27c4 1319 if (c == REPRINT_CHAR(tty) && L_ECHO(tty) && L_IEXTEN(tty)) {
bc5a5e3f 1320 size_t tail = ldata->canon_head;
1da177e4 1321
57c94121 1322 finish_erasing(ldata);
1da177e4 1323 echo_char(c, tty);
57c94121 1324 echo_char_raw('\n', ldata);
3d63b7e4 1325 while (MASK(tail) != MASK(ldata->read_head)) {
bc5a5e3f
PH
1326 echo_char(read_buf(ldata, tail), tty);
1327 tail++;
1da177e4 1328 }
17bd7907 1329 commit_echoes(tty);
16765365 1330 return;
1da177e4
LT
1331 }
1332 if (c == '\n') {
acc71bba 1333 if (L_ECHO(tty) || L_ECHONL(tty)) {
57c94121 1334 echo_char_raw('\n', ldata);
17bd7907 1335 commit_echoes(tty);
1da177e4
LT
1336 }
1337 goto handle_newline;
1338 }
1339 if (c == EOF_CHAR(tty)) {
1da177e4
LT
1340 c = __DISABLED_CHAR;
1341 goto handle_newline;
1342 }
1343 if ((c == EOL_CHAR(tty)) ||
1344 (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
1345 /*
1346 * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
1347 */
1348 if (L_ECHO(tty)) {
1da177e4 1349 /* Record the column of first canon char. */
ba2e68ac 1350 if (ldata->canon_head == ldata->read_head)
57c94121 1351 echo_set_canon_col(ldata);
1da177e4 1352 echo_char(c, tty);
17bd7907 1353 commit_echoes(tty);
1da177e4
LT
1354 }
1355 /*
1356 * XXX does PARMRK doubling happen for
1357 * EOL_CHAR and EOL2_CHAR?
1358 */
001ba923 1359 if (c == (unsigned char) '\377' && I_PARMRK(tty))
57c94121 1360 put_tty_queue(c, ldata);
1da177e4 1361
4edf1827 1362handle_newline:
bc5a5e3f 1363 set_bit(ldata->read_head & (N_TTY_BUF_SIZE - 1), ldata->read_flags);
6d76bd26 1364 put_tty_queue(c, ldata);
70aca71f 1365 smp_store_release(&ldata->canon_head, ldata->read_head);
1da177e4 1366 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
c816b2e6 1367 wake_up_interruptible_poll(&tty->read_wait, EPOLLIN | EPOLLRDNORM);
16765365 1368 return;
1da177e4
LT
1369 }
1370 }
4edf1827 1371
acc71bba 1372 if (L_ECHO(tty)) {
57c94121 1373 finish_erasing(ldata);
1da177e4 1374 if (c == '\n')
57c94121 1375 echo_char_raw('\n', ldata);
1da177e4
LT
1376 else {
1377 /* Record the column of first canon char. */
ba2e68ac 1378 if (ldata->canon_head == ldata->read_head)
57c94121 1379 echo_set_canon_col(ldata);
1da177e4
LT
1380 echo_char(c, tty);
1381 }
17bd7907 1382 commit_echoes(tty);
1da177e4
LT
1383 }
1384
001ba923
PH
1385 /* PARMRK doubling check */
1386 if (c == (unsigned char) '\377' && I_PARMRK(tty))
57c94121 1387 put_tty_queue(c, ldata);
1da177e4 1388
57c94121 1389 put_tty_queue(c, ldata);
4edf1827 1390}
1da177e4 1391
c66453ce 1392/**
98629663
JS
1393 * n_tty_receive_char - perform processing
1394 * @tty: terminal device
1395 * @c: character
c66453ce 1396 *
98629663
JS
1397 * Process an individual character of input received from the driver. This is
1398 * serialized with respect to itself by the rules for the driver above.
c66453ce 1399 *
98629663
JS
1400 * Locking: n_tty_receive_buf()/producer path:
1401 * caller holds non-exclusive %termios_rwsem
1402 * publishes canon_head if canonical mode is active
c66453ce 1403 */
95aafe32 1404static void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
4b1f79c2
PH
1405{
1406 struct n_tty_data *ldata = tty->disc_data;
4b1f79c2 1407
6e94dbc7 1408 if (tty->flow.stopped && !tty->flow.tco_stopped && I_IXON(tty) && I_IXANY(tty)) {
e60d27c4
PH
1409 start_tty(tty);
1410 process_echoes(tty);
1411 }
1412 if (L_ECHO(tty)) {
1413 finish_erasing(ldata);
1414 /* Record the column of first canon char. */
1415 if (ldata->canon_head == ldata->read_head)
1416 echo_set_canon_col(ldata);
1417 echo_char(c, tty);
1418 commit_echoes(tty);
4b1f79c2 1419 }
001ba923 1420 /* PARMRK doubling check */
95aafe32 1421 if (c == (unsigned char) '\377' && I_PARMRK(tty))
e60d27c4
PH
1422 put_tty_queue(c, ldata);
1423 put_tty_queue(c, ldata);
1424}
4b1f79c2 1425
6bb6fa69
IJ
1426static void n_tty_receive_char_closing(struct tty_struct *tty, unsigned char c,
1427 bool lookahead_done)
ad0cc7ba
PH
1428{
1429 if (I_ISTRIP(tty))
1430 c &= 0x7f;
1431 if (I_IUCLC(tty) && L_IEXTEN(tty))
1432 c = tolower(c);
1433
1434 if (I_IXON(tty)) {
65534736
IJ
1435 if (!n_tty_receive_char_flow_ctrl(tty, c, lookahead_done) &&
1436 tty->flow.stopped && !tty->flow.tco_stopped && I_IXANY(tty) &&
1437 c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) &&
1438 c != SUSP_CHAR(tty)) {
ad0cc7ba
PH
1439 start_tty(tty);
1440 process_echoes(tty);
1441 }
1442 }
1443}
1444
d2f8d7ab
PH
1445static void
1446n_tty_receive_char_flagged(struct tty_struct *tty, unsigned char c, char flag)
1447{
d2f8d7ab
PH
1448 switch (flag) {
1449 case TTY_BREAK:
1450 n_tty_receive_break(tty);
1451 break;
1452 case TTY_PARITY:
1453 case TTY_FRAME:
1454 n_tty_receive_parity_error(tty, c);
1455 break;
1456 case TTY_OVERRUN:
1457 n_tty_receive_overrun(tty);
1458 break;
1459 default:
339f36ba 1460 tty_err(tty, "unknown flag %d\n", flag);
d2f8d7ab
PH
1461 break;
1462 }
1463}
1464
e60d27c4
PH
1465static void
1466n_tty_receive_char_lnext(struct tty_struct *tty, unsigned char c, char flag)
1467{
1468 struct n_tty_data *ldata = tty->disc_data;
1469
1470 ldata->lnext = 0;
1471 if (likely(flag == TTY_NORMAL)) {
1472 if (I_ISTRIP(tty))
1473 c &= 0x7f;
1474 if (I_IUCLC(tty) && L_IEXTEN(tty))
1475 c = tolower(c);
95aafe32 1476 n_tty_receive_char(tty, c);
e60d27c4
PH
1477 } else
1478 n_tty_receive_char_flagged(tty, c, flag);
1479}
1480
6bb6fa69 1481/* Caller must ensure count > 0 */
a8d9cd23 1482static void n_tty_lookahead_flow_ctrl(struct tty_struct *tty, const u8 *cp,
892bc209 1483 const u8 *fp, size_t count)
6bb6fa69
IJ
1484{
1485 struct n_tty_data *ldata = tty->disc_data;
1486 unsigned char flag = TTY_NORMAL;
1487
1488 ldata->lookahead_count += count;
1489
1490 if (!I_IXON(tty))
1491 return;
1492
1493 while (count--) {
1494 if (fp)
1495 flag = *fp++;
1496 if (likely(flag == TTY_NORMAL))
1497 n_tty_receive_char_flow_ctrl(tty, *cp, false);
1498 cp++;
1499 }
1500}
1501
4a23a4df 1502static void
a8d9cd23
JSS
1503n_tty_receive_buf_real_raw(const struct tty_struct *tty, const u8 *cp,
1504 int count)
4a23a4df
PH
1505{
1506 struct n_tty_data *ldata = tty->disc_data;
1507 size_t n, head;
1508
1509 head = ldata->read_head & (N_TTY_BUF_SIZE - 1);
70aca71f 1510 n = min_t(size_t, count, N_TTY_BUF_SIZE - head);
4a23a4df
PH
1511 memcpy(read_buf_addr(ldata, head), cp, n);
1512 ldata->read_head += n;
1513 cp += n;
1514 count -= n;
1515
1516 head = ldata->read_head & (N_TTY_BUF_SIZE - 1);
70aca71f 1517 n = min_t(size_t, count, N_TTY_BUF_SIZE - head);
4a23a4df
PH
1518 memcpy(read_buf_addr(ldata, head), cp, n);
1519 ldata->read_head += n;
1520}
1521
554117bd 1522static void
892bc209
JSS
1523n_tty_receive_buf_raw(struct tty_struct *tty, const u8 *cp, const u8 *fp,
1524 int count)
554117bd
PH
1525{
1526 struct n_tty_data *ldata = tty->disc_data;
892bc209 1527 u8 flag = TTY_NORMAL;
554117bd
PH
1528
1529 while (count--) {
1530 if (fp)
1531 flag = *fp++;
1532 if (likely(flag == TTY_NORMAL))
1533 put_tty_queue(*cp++, ldata);
1534 else
1535 n_tty_receive_char_flagged(tty, *cp++, flag);
1536 }
1537}
1538
ad0cc7ba 1539static void
892bc209
JSS
1540n_tty_receive_buf_closing(struct tty_struct *tty, const u8 *cp, const u8 *fp,
1541 int count, bool lookahead_done)
ad0cc7ba
PH
1542{
1543 char flag = TTY_NORMAL;
1544
1545 while (count--) {
1546 if (fp)
1547 flag = *fp++;
1548 if (likely(flag == TTY_NORMAL))
6bb6fa69 1549 n_tty_receive_char_closing(tty, *cp++, lookahead_done);
ad0cc7ba
PH
1550 }
1551}
1552
a8d9cd23 1553static void n_tty_receive_buf_standard(struct tty_struct *tty, const u8 *cp,
892bc209 1554 const u8 *fp, int count,
a8d9cd23 1555 bool lookahead_done)
6baad008
PH
1556{
1557 struct n_tty_data *ldata = tty->disc_data;
892bc209 1558 u8 flag = TTY_NORMAL;
6baad008
PH
1559
1560 while (count--) {
a8d9cd23 1561 u8 c = *cp++;
3a7d530a 1562
6baad008
PH
1563 if (fp)
1564 flag = *fp++;
67a620d5
JS
1565
1566 if (ldata->lnext) {
3a7d530a 1567 n_tty_receive_char_lnext(tty, c, flag);
67a620d5
JS
1568 continue;
1569 }
1570
e8f2a139 1571 if (unlikely(flag != TTY_NORMAL)) {
3a7d530a 1572 n_tty_receive_char_flagged(tty, c, flag);
e8f2a139
JS
1573 continue;
1574 }
1575
1576 if (I_ISTRIP(tty))
1577 c &= 0x7f;
1578 if (I_IUCLC(tty) && L_IEXTEN(tty))
1579 c = tolower(c);
1580 if (L_EXTPROC(tty)) {
1581 put_tty_queue(c, ldata);
1582 continue;
1583 }
1584
1585 if (test_bit(c, ldata->char_map))
6bb6fa69 1586 n_tty_receive_char_special(tty, c, lookahead_done);
e8f2a139
JS
1587 else
1588 n_tty_receive_char(tty, c);
6baad008
PH
1589 }
1590}
1591
892bc209
JSS
1592static void __receive_buf(struct tty_struct *tty, const u8 *cp, const u8 *fp,
1593 int count)
1da177e4 1594{
53c5ee2c 1595 struct n_tty_data *ldata = tty->disc_data;
a1dd30e9 1596 bool preops = I_ISTRIP(tty) || (I_IUCLC(tty) && L_IEXTEN(tty));
6bb6fa69 1597 size_t la_count = min_t(size_t, ldata->lookahead_count, count);
1da177e4 1598
4a23a4df 1599 if (ldata->real_raw)
b30a3d39 1600 n_tty_receive_buf_real_raw(tty, cp, count);
a1dd30e9 1601 else if (ldata->raw || (L_EXTPROC(tty) && !preops))
554117bd 1602 n_tty_receive_buf_raw(tty, cp, fp, count);
6bb6fa69
IJ
1603 else if (tty->closing && !L_EXTPROC(tty)) {
1604 if (la_count > 0)
1605 n_tty_receive_buf_closing(tty, cp, fp, la_count, true);
1606 if (count > la_count)
1607 n_tty_receive_buf_closing(tty, cp, fp, count - la_count, false);
1608 } else {
1609 if (la_count > 0)
1610 n_tty_receive_buf_standard(tty, cp, fp, la_count, true);
1611 if (count > la_count)
1612 n_tty_receive_buf_standard(tty, cp, fp, count - la_count, false);
cbfd0340
PH
1613
1614 flush_echoes(tty);
f34d7a5b
AC
1615 if (tty->ops->flush_chars)
1616 tty->ops->flush_chars(tty);
1da177e4
LT
1617 }
1618
6bb6fa69
IJ
1619 ldata->lookahead_count -= la_count;
1620
70aca71f
PH
1621 if (ldata->icanon && !L_EXTPROC(tty))
1622 return;
1623
1624 /* publish read_head to consumer */
1625 smp_store_release(&ldata->commit_head, ldata->read_head);
1626
33d71363 1627 if (read_cnt(ldata)) {
1da177e4 1628 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
c816b2e6 1629 wake_up_interruptible_poll(&tty->read_wait, EPOLLIN | EPOLLRDNORM);
1da177e4 1630 }
1da177e4
LT
1631}
1632
fb5ef9e7 1633/**
98629663
JS
1634 * n_tty_receive_buf_common - process input
1635 * @tty: device to receive input
1636 * @cp: input chars
1637 * @fp: flags for each char (if %NULL, all chars are %TTY_NORMAL)
1638 * @count: number of input chars in @cp
1639 * @flow: enable flow control
1640 *
1641 * Called by the terminal driver when a block of characters has been received.
1642 * This function must be called from soft contexts not from interrupt context.
1643 * The driver is responsible for making calls one at a time and in order (or
1644 * using flush_to_ldisc()).
1645 *
1646 * Returns: the # of input chars from @cp which were processed.
1647 *
1648 * In canonical mode, the maximum line length is 4096 chars (including the line
1649 * termination char); lines longer than 4096 chars are truncated. After 4095
1650 * chars, input data is still processed but not stored. Overflow processing
1651 * ensures the tty can always receive more input until at least one line can be
1652 * read.
1653 *
1654 * In non-canonical mode, the read buffer will only accept 4095 chars; this
1655 * provides the necessary space for a newline char if the input mode is
1656 * switched to canonical.
1657 *
1658 * Note it is possible for the read buffer to _contain_ 4096 chars in
1659 * non-canonical mode: the read buffer could already contain the maximum canon
1660 * line of 4096 chars when the mode is switched to non-canonical.
1661 *
1662 * Locking: n_tty_receive_buf()/producer path:
1663 * claims non-exclusive %termios_rwsem
1664 * publishes commit_head or canon_head
fb5ef9e7 1665 */
e8161447 1666static size_t
892bc209
JSS
1667n_tty_receive_buf_common(struct tty_struct *tty, const u8 *cp, const u8 *fp,
1668 int count, int flow)
24a89d1c
PH
1669{
1670 struct n_tty_data *ldata = tty->disc_data;
e8161447
JSS
1671 size_t rcvd = 0;
1672 int room, n, overflow;
24a89d1c 1673
9356b535
PH
1674 down_read(&tty->termios_rwsem);
1675
c96cf923 1676 do {
70aca71f 1677 /*
06c49f9f
PH
1678 * When PARMRK is set, each input char may take up to 3 chars
1679 * in the read buf; reduce the buffer space avail by 3x
70aca71f
PH
1680 *
1681 * If we are doing input canonicalization, and there are no
1682 * pending newlines, let characters through without limit, so
1683 * that erase characters will be handled. Other excess
1684 * characters will be beeped.
1685 *
1686 * paired with store in *_copy_from_read_buf() -- guarantees
1687 * the consumer has loaded the data in read_buf up to the new
1688 * read_tail (so this producer will not overwrite unread data)
1689 */
1690 size_t tail = smp_load_acquire(&ldata->read_tail);
70aca71f 1691
fb5ef9e7 1692 room = N_TTY_BUF_SIZE - (ldata->read_head - tail);
70aca71f 1693 if (I_PARMRK(tty))
7e26c84d 1694 room = DIV_ROUND_UP(room, 3);
fb5ef9e7
PH
1695 room--;
1696 if (room <= 0) {
1697 overflow = ldata->icanon && ldata->canon_head == tail;
1698 if (overflow && room < 0)
1699 ldata->read_head--;
1700 room = overflow;
4903fde8 1701 WRITE_ONCE(ldata->no_room, flow && !room);
fb5ef9e7
PH
1702 } else
1703 overflow = 0;
70aca71f 1704
19e2ad6a 1705 n = min(count, room);
fb5ef9e7 1706 if (!n)
19e2ad6a 1707 break;
fb5ef9e7
PH
1708
1709 /* ignore parity errors if handling overflow */
1710 if (!overflow || !fp || *fp != TTY_PARITY)
1711 __receive_buf(tty, cp, fp, n);
1712
19e2ad6a
PH
1713 cp += n;
1714 if (fp)
1715 fp += n;
1716 count -= n;
1717 rcvd += n;
c96cf923 1718 } while (!test_bit(TTY_LDISC_CHANGING, &tty->flags));
24a89d1c 1719
19e2ad6a 1720 tty->receive_room = room;
fb5ef9e7
PH
1721
1722 /* Unthrottle if handling overflow on pty */
1723 if (tty->driver->type == TTY_DRIVER_TYPE_PTY) {
1724 if (overflow) {
1725 tty_set_flow_change(tty, TTY_UNTHROTTLE_SAFE);
1726 tty_unthrottle_safe(tty);
1727 __tty_set_flow_change(tty, 0);
1728 }
1729 } else
1730 n_tty_check_throttle(tty);
1731
4903fde8
HL
1732 if (unlikely(ldata->no_room)) {
1733 /*
1734 * Barrier here is to ensure to read the latest read_tail in
1735 * chars_in_buffer() and to make sure that read_tail is not loaded
1736 * before ldata->no_room is set.
1737 */
1738 smp_mb();
1739 if (!chars_in_buffer(tty))
1740 n_tty_kick_worker(tty);
1741 }
1742
9356b535
PH
1743 up_read(&tty->termios_rwsem);
1744
19e2ad6a 1745 return rcvd;
24a89d1c
PH
1746}
1747
a8d9cd23 1748static void n_tty_receive_buf(struct tty_struct *tty, const u8 *cp,
892bc209 1749 const u8 *fp, size_t count)
5c32d123
PH
1750{
1751 n_tty_receive_buf_common(tty, cp, fp, count, 0);
1752}
1753
a8d9cd23 1754static size_t n_tty_receive_buf2(struct tty_struct *tty, const u8 *cp,
892bc209 1755 const u8 *fp, size_t count)
5c32d123
PH
1756{
1757 return n_tty_receive_buf_common(tty, cp, fp, count, 1);
1758}
1759
1da177e4 1760/**
98629663
JS
1761 * n_tty_set_termios - termios data changed
1762 * @tty: terminal
1763 * @old: previous data
1da177e4 1764 *
98629663
JS
1765 * Called by the tty layer when the user changes termios flags so that the line
1766 * discipline can plan ahead. This function cannot sleep and is protected from
1767 * re-entry by the tty layer. The user is guaranteed that this function will
1768 * not be re-entered or in progress when the ldisc is closed.
17b82060 1769 *
98629663 1770 * Locking: Caller holds @tty->termios_rwsem
1da177e4 1771 */
8b7d2d95 1772static void n_tty_set_termios(struct tty_struct *tty, const struct ktermios *old)
1da177e4 1773{
53c5ee2c 1774 struct n_tty_data *ldata = tty->disc_data;
47afa7a5 1775
966031f3 1776 if (!old || (old->c_lflag ^ tty->termios.c_lflag) & (ICANON | EXTPROC)) {
3fe780b3 1777 bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
4d0ed182
PH
1778 ldata->line_start = ldata->read_tail;
1779 if (!L_ICANON(tty) || !read_cnt(ldata)) {
1780 ldata->canon_head = ldata->read_tail;
1781 ldata->push = 0;
1782 } else {
1783 set_bit((ldata->read_head - 1) & (N_TTY_BUF_SIZE - 1),
1784 ldata->read_flags);
1785 ldata->canon_head = ldata->read_head;
1786 ldata->push = 1;
1787 }
70aca71f 1788 ldata->commit_head = ldata->read_head;
53c5ee2c 1789 ldata->erasing = 0;
6f9b028a 1790 ldata->lnext = 0;
47afa7a5
AC
1791 }
1792
53c5ee2c 1793 ldata->icanon = (L_ICANON(tty) != 0);
582f5590 1794
1da177e4
LT
1795 if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
1796 I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
1797 I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
1798 I_PARMRK(tty)) {
1bb9d562 1799 bitmap_zero(ldata->char_map, 256);
1da177e4
LT
1800
1801 if (I_IGNCR(tty) || I_ICRNL(tty))
1bb9d562 1802 set_bit('\r', ldata->char_map);
1da177e4 1803 if (I_INLCR(tty))
1bb9d562 1804 set_bit('\n', ldata->char_map);
1da177e4
LT
1805
1806 if (L_ICANON(tty)) {
1bb9d562
PH
1807 set_bit(ERASE_CHAR(tty), ldata->char_map);
1808 set_bit(KILL_CHAR(tty), ldata->char_map);
1809 set_bit(EOF_CHAR(tty), ldata->char_map);
1810 set_bit('\n', ldata->char_map);
1811 set_bit(EOL_CHAR(tty), ldata->char_map);
1da177e4 1812 if (L_IEXTEN(tty)) {
1bb9d562
PH
1813 set_bit(WERASE_CHAR(tty), ldata->char_map);
1814 set_bit(LNEXT_CHAR(tty), ldata->char_map);
1815 set_bit(EOL2_CHAR(tty), ldata->char_map);
1da177e4
LT
1816 if (L_ECHO(tty))
1817 set_bit(REPRINT_CHAR(tty),
1bb9d562 1818 ldata->char_map);
1da177e4
LT
1819 }
1820 }
1821 if (I_IXON(tty)) {
1bb9d562
PH
1822 set_bit(START_CHAR(tty), ldata->char_map);
1823 set_bit(STOP_CHAR(tty), ldata->char_map);
1da177e4
LT
1824 }
1825 if (L_ISIG(tty)) {
1bb9d562
PH
1826 set_bit(INTR_CHAR(tty), ldata->char_map);
1827 set_bit(QUIT_CHAR(tty), ldata->char_map);
1828 set_bit(SUSP_CHAR(tty), ldata->char_map);
1da177e4 1829 }
1bb9d562 1830 clear_bit(__DISABLED_CHAR, ldata->char_map);
53c5ee2c
JS
1831 ldata->raw = 0;
1832 ldata->real_raw = 0;
1da177e4 1833 } else {
53c5ee2c 1834 ldata->raw = 1;
1da177e4
LT
1835 if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
1836 (I_IGNPAR(tty) || !I_INPCK(tty)) &&
1837 (tty->driver->flags & TTY_DRIVER_REAL_RAW))
53c5ee2c 1838 ldata->real_raw = 1;
1da177e4 1839 else
53c5ee2c 1840 ldata->real_raw = 0;
1da177e4 1841 }
dab73b4e
WY
1842 /*
1843 * Fix tty hang when I_IXON(tty) is cleared, but the tty
1844 * been stopped by STOP_CHAR(tty) before it.
1845 */
6e94dbc7 1846 if (!I_IXON(tty) && old && (old->c_iflag & IXON) && !tty->flow.tco_stopped) {
dab73b4e 1847 start_tty(tty);
e2613be5
PH
1848 process_echoes(tty);
1849 }
dab73b4e 1850
f34d7a5b 1851 /* The termios change make the tty ready for I/O */
e81107d4
KT
1852 wake_up_interruptible(&tty->write_wait);
1853 wake_up_interruptible(&tty->read_wait);
1da177e4
LT
1854}
1855
1856/**
98629663
JS
1857 * n_tty_close - close the ldisc for this tty
1858 * @tty: device
1da177e4 1859 *
98629663
JS
1860 * Called from the terminal layer when this line discipline is being shut down,
1861 * either because of a close or becsuse of a discipline change. The function
1862 * will not be called while other ldisc methods are in progress.
1da177e4 1863 */
1da177e4
LT
1864static void n_tty_close(struct tty_struct *tty)
1865{
70ece7a7
JS
1866 struct n_tty_data *ldata = tty->disc_data;
1867
79901317
PH
1868 if (tty->link)
1869 n_tty_packet_mode_flush(tty);
1870
c9cd57bf 1871 down_write(&tty->termios_rwsem);
20bafb3d 1872 vfree(ldata);
70ece7a7 1873 tty->disc_data = NULL;
c9cd57bf 1874 up_write(&tty->termios_rwsem);
1da177e4
LT
1875}
1876
1877/**
98629663
JS
1878 * n_tty_open - open an ldisc
1879 * @tty: terminal to open
1da177e4 1880 *
98629663
JS
1881 * Called when this line discipline is being attached to the terminal device.
1882 * Can sleep. Called serialized so that no other events will occur in parallel.
1883 * No further open will occur until a close.
1da177e4 1884 */
1da177e4
LT
1885static int n_tty_open(struct tty_struct *tty)
1886{
70ece7a7
JS
1887 struct n_tty_data *ldata;
1888
20bafb3d 1889 /* Currently a malloc failure here can panic */
ebec3f8f 1890 ldata = vzalloc(sizeof(*ldata));
70ece7a7 1891 if (!ldata)
ebec3f8f 1892 return -ENOMEM;
70ece7a7 1893
53c5ee2c 1894 ldata->overrun_time = jiffies;
bddc7152
JS
1895 mutex_init(&ldata->atomic_read_lock);
1896 mutex_init(&ldata->output_lock);
53c5ee2c 1897
70ece7a7 1898 tty->disc_data = ldata;
1da177e4 1899 tty->closing = 0;
b66f4fa5
PH
1900 /* indicate buffer work may resume */
1901 clear_bit(TTY_LDISC_HALTED, &tty->flags);
1902 n_tty_set_termios(tty, NULL);
1903 tty_unthrottle(tty);
1da177e4
LT
1904 return 0;
1905}
1906
5bedcf70 1907static inline int input_available_p(const struct tty_struct *tty, int poll)
1da177e4 1908{
5bedcf70 1909 const struct n_tty_data *ldata = tty->disc_data;
a5934804 1910 int amt = poll && !TIME_CHAR(tty) && MIN_CHAR(tty) ? MIN_CHAR(tty) : 1;
53c5ee2c 1911
25e8d0ed
PH
1912 if (ldata->icanon && !L_EXTPROC(tty))
1913 return ldata->canon_head != ldata->read_tail;
1914 else
70aca71f 1915 return ldata->commit_head - ldata->read_tail >= amt;
1da177e4
LT
1916}
1917
1918/**
98629663
JS
1919 * copy_from_read_buf - copy read data directly
1920 * @tty: terminal device
1921 * @kbp: data
1922 * @nr: size of data
1da177e4 1923 *
98629663
JS
1924 * Helper function to speed up n_tty_read(). It is only called when %ICANON is
1925 * off; it copies characters straight from the tty queue.
1da177e4 1926 *
98629663
JS
1927 * Returns: true if it successfully copied data, but there is still more data
1928 * to be had.
15ea8ae8 1929 *
98629663
JS
1930 * Locking:
1931 * * called under the @ldata->atomic_read_lock sem
1932 * * n_tty_read()/consumer path:
1933 * caller holds non-exclusive %termios_rwsem;
6d76bd26 1934 * read_tail published
1da177e4 1935 */
5bedcf70 1936static bool copy_from_read_buf(const struct tty_struct *tty,
3b830a9c 1937 unsigned char **kbp,
1da177e4
LT
1938 size_t *nr)
1939
1940{
53c5ee2c 1941 struct n_tty_data *ldata = tty->disc_data;
1da177e4 1942 size_t n;
3fa10cc8 1943 bool is_eof;
70aca71f 1944 size_t head = smp_load_acquire(&ldata->commit_head);
bc5a5e3f 1945 size_t tail = ldata->read_tail & (N_TTY_BUF_SIZE - 1);
1da177e4 1946
70aca71f 1947 n = min(head - ldata->read_tail, N_TTY_BUF_SIZE - tail);
1da177e4 1948 n = min(*nr, n);
1da177e4 1949 if (n) {
b97b3d9f 1950 unsigned char *from = read_buf_addr(ldata, tail);
3b830a9c 1951 memcpy(*kbp, from, n);
e661cf70 1952 is_eof = n == 1 && *from == EOF_CHAR(tty);
309426ae 1953 tty_audit_add_data(tty, from, n);
b97b3d9f 1954 zero_buffer(tty, from, n);
70aca71f 1955 smp_store_release(&ldata->read_tail, ldata->read_tail + n);
26df6d13 1956 /* Turn single EOF into zero-length read */
70aca71f
PH
1957 if (L_EXTPROC(tty) && ldata->icanon && is_eof &&
1958 (head == ldata->read_tail))
15ea8ae8 1959 return false;
3b830a9c 1960 *kbp += n;
1da177e4 1961 *nr -= n;
15ea8ae8
LT
1962
1963 /* If we have more to copy, let the caller know */
1964 return head != ldata->read_tail;
1da177e4 1965 }
15ea8ae8 1966 return false;
1da177e4
LT
1967}
1968
88bb0de3 1969/**
98629663
JS
1970 * canon_copy_from_read_buf - copy read data in canonical mode
1971 * @tty: terminal device
1972 * @kbp: data
1973 * @nr: size of data
1974 *
1975 * Helper function for n_tty_read(). It is only called when %ICANON is on; it
1976 * copies one line of input up to and including the line-delimiting character
1977 * into the result buffer.
1978 *
1979 * Note: When termios is changed from non-canonical to canonical mode and the
1980 * read buffer contains data, n_tty_set_termios() simulates an EOF push (as if
1981 * C-d were input) _without_ the %DISABLED_CHAR in the buffer. This causes data
1982 * already processed as input to be immediately available as input although a
1983 * newline has not been received.
1984 *
1985 * Locking:
1986 * * called under the %atomic_read_lock mutex
1987 * * n_tty_read()/consumer path:
1988 * caller holds non-exclusive %termios_rwsem;
1989 * read_tail published
88bb0de3 1990 */
5bedcf70 1991static bool canon_copy_from_read_buf(const struct tty_struct *tty,
64a69892
LT
1992 unsigned char **kbp,
1993 size_t *nr)
88bb0de3
PH
1994{
1995 struct n_tty_data *ldata = tty->disc_data;
32f13521 1996 size_t n, size, more, c;
bc5a5e3f 1997 size_t eol;
d7fe75cb 1998 size_t tail, canon_head;
3b830a9c 1999 int found = 0;
88bb0de3
PH
2000
2001 /* N.B. avoid overrun if nr == 0 */
ac8f3bf8 2002 if (!*nr)
d7fe75cb 2003 return false;
88bb0de3 2004
d7fe75cb 2005 canon_head = smp_load_acquire(&ldata->canon_head);
35930307 2006 n = min(*nr, canon_head - ldata->read_tail);
ac8f3bf8 2007
bc5a5e3f 2008 tail = ldata->read_tail & (N_TTY_BUF_SIZE - 1);
32f13521
PH
2009 size = min_t(size_t, tail + n, N_TTY_BUF_SIZE);
2010
bc5a5e3f 2011 n_tty_trace("%s: nr:%zu tail:%zu n:%zu size:%zu\n",
32f13521
PH
2012 __func__, *nr, tail, n, size);
2013
2014 eol = find_next_bit(ldata->read_flags, size, tail);
2015 more = n - (size - tail);
2016 if (eol == N_TTY_BUF_SIZE && more) {
2017 /* scan wrapped without finding set bit */
b5c7e7ec 2018 eol = find_first_bit(ldata->read_flags, more);
b985e9e3
PH
2019 found = eol != more;
2020 } else
2021 found = eol != size;
32f13521 2022
c77569d2 2023 n = eol - tail;
da555db6
MT
2024 if (n > N_TTY_BUF_SIZE)
2025 n += N_TTY_BUF_SIZE;
ac8f3bf8 2026 c = n + found;
32f13521 2027
35930307 2028 if (!found || read_buf(ldata, eol) != __DISABLED_CHAR)
ac8f3bf8 2029 n = c;
32f13521 2030
679e7c29
PH
2031 n_tty_trace("%s: eol:%zu found:%d n:%zu c:%zu tail:%zu more:%zu\n",
2032 __func__, eol, found, n, c, tail, more);
32f13521 2033
3b830a9c
LT
2034 tty_copy(tty, *kbp, tail, n);
2035 *kbp += n;
32f13521
PH
2036 *nr -= n;
2037
a73d3d69 2038 if (found)
6d76bd26 2039 clear_bit(eol, ldata->read_flags);
70aca71f 2040 smp_store_release(&ldata->read_tail, ldata->read_tail + c);
88bb0de3 2041
40d5e090 2042 if (found) {
4d0ed182
PH
2043 if (!ldata->push)
2044 ldata->line_start = ldata->read_tail;
2045 else
2046 ldata->push = 0;
b50819f4 2047 tty_audit_push();
d7fe75cb 2048 return false;
40d5e090 2049 }
d7fe75cb
LT
2050
2051 /* No EOL found - do a continuation retry if there is more data */
2052 return ldata->read_tail != canon_head;
88bb0de3
PH
2053}
2054
65a8b287
DG
2055/*
2056 * If we finished a read at the exact location of an
2057 * EOF (special EOL character that's a __DISABLED_CHAR)
2058 * in the stream, silently eat the EOF.
2059 */
32042446 2060static void canon_skip_eof(struct n_tty_data *ldata)
65a8b287 2061{
65a8b287
DG
2062 size_t tail, canon_head;
2063
2064 canon_head = smp_load_acquire(&ldata->canon_head);
2065 tail = ldata->read_tail;
2066
2067 // No data?
2068 if (tail == canon_head)
2069 return;
2070
2071 // See if the tail position is EOF in the circular buffer
2072 tail &= (N_TTY_BUF_SIZE - 1);
2073 if (!test_bit(tail, ldata->read_flags))
2074 return;
2075 if (read_buf(ldata, tail) != __DISABLED_CHAR)
2076 return;
2077
2078 // Clear the EOL bit, skip the EOF char.
2079 clear_bit(tail, ldata->read_flags);
2080 smp_store_release(&ldata->read_tail, ldata->read_tail + 1);
2081}
2082
1da177e4 2083/**
98629663
JS
2084 * job_control - check job control
2085 * @tty: tty
2086 * @file: file handle
2087 *
2088 * Perform job control management checks on this @file/@tty descriptor and if
2089 * appropriate send any needed signals and return a negative error code if
2090 * action should be taken.
2091 *
2092 * Locking:
2093 * * redirected write test is safe
2094 * * current->signal->tty check is safe
2095 * * ctrl.lock to safely reference @tty->ctrl.pgrp
1da177e4 2096 */
1da177e4
LT
2097static int job_control(struct tty_struct *tty, struct file *file)
2098{
2099 /* Job control check -- must be done at start and after
2100 every sleep (POSIX.1 7.1.1.4). */
2101 /* NOTE: not yet done after every sleep pending a thorough
2102 check of the logic of this change. -- jlc */
2103 /* don't stop on /dev/console */
9f12e37c 2104 if (file->f_op->write_iter == redirected_tty_write)
01a5e440
PH
2105 return 0;
2106
2812d9e9 2107 return __tty_check_change(tty, SIGTTIN);
1da177e4 2108}
4edf1827 2109
1da177e4
LT
2110
2111/**
98629663
JS
2112 * n_tty_read - read function for tty
2113 * @tty: tty device
2114 * @file: file object
2115 * @kbuf: kernelspace buffer pointer
2116 * @nr: size of I/O
2117 * @cookie: if non-%NULL, this is a continuation read
2118 * @offset: where to continue reading from (unused in n_tty)
2119 *
2120 * Perform reads for the line discipline. We are guaranteed that the line
2121 * discipline will not be closed under us but we may get multiple parallel
2122 * readers and must handle this ourselves. We may also get a hangup. Always
2123 * called in user context, may sleep.
2124 *
2125 * This code must be sure never to sleep through a hangup.
2126 *
2127 * Locking: n_tty_read()/consumer path:
2128 * claims non-exclusive termios_rwsem;
2129 * publishes read_tail
1da177e4 2130 */
11a96d18 2131static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
3b830a9c
LT
2132 unsigned char *kbuf, size_t nr,
2133 void **cookie, unsigned long offset)
1da177e4 2134{
53c5ee2c 2135 struct n_tty_data *ldata = tty->disc_data;
3b830a9c 2136 unsigned char *kb = kbuf;
97d9e28d 2137 DEFINE_WAIT_FUNC(wait, woken_wake_function);
0f40fbbc 2138 int c;
1da177e4
LT
2139 int minimum, time;
2140 ssize_t retval = 0;
1da177e4 2141 long timeout;
64d608db 2142 bool packet;
947d66b6 2143 size_t old_tail;
1da177e4 2144
15ea8ae8
LT
2145 /*
2146 * Is this a continuation of a read started earler?
2147 *
2148 * If so, we still hold the atomic_read_lock and the
2149 * termios_rwsem, and can just continue to copy data.
2150 */
2151 if (*cookie) {
d7fe75cb 2152 if (ldata->icanon && !L_EXTPROC(tty)) {
65a8b287
DG
2153 /*
2154 * If we have filled the user buffer, see
2155 * if we should skip an EOF character before
2156 * releasing the lock and returning done.
2157 */
2158 if (!nr)
32042446 2159 canon_skip_eof(ldata);
65a8b287 2160 else if (canon_copy_from_read_buf(tty, &kb, &nr))
d7fe75cb
LT
2161 return kb - kbuf;
2162 } else {
2163 if (copy_from_read_buf(tty, &kb, &nr))
2164 return kb - kbuf;
2165 }
15ea8ae8
LT
2166
2167 /* No more data - release locks and stop retries */
2168 n_tty_kick_worker(tty);
2169 n_tty_check_unthrottle(tty);
2170 up_read(&tty->termios_rwsem);
2171 mutex_unlock(&ldata->atomic_read_lock);
2172 *cookie = NULL;
2173 return kb - kbuf;
2174 }
2175
1da177e4 2176 c = job_control(tty, file);
4edf1827 2177 if (c < 0)
1da177e4 2178 return c;
4edf1827 2179
aefceaf4
PH
2180 /*
2181 * Internal serialization of reads.
2182 */
2183 if (file->f_flags & O_NONBLOCK) {
2184 if (!mutex_trylock(&ldata->atomic_read_lock))
2185 return -EAGAIN;
2186 } else {
2187 if (mutex_lock_interruptible(&ldata->atomic_read_lock))
2188 return -ERESTARTSYS;
2189 }
2190
9356b535
PH
2191 down_read(&tty->termios_rwsem);
2192
1da177e4
LT
2193 minimum = time = 0;
2194 timeout = MAX_SCHEDULE_TIMEOUT;
53c5ee2c 2195 if (!ldata->icanon) {
1da177e4
LT
2196 minimum = MIN_CHAR(tty);
2197 if (minimum) {
a6e54319 2198 time = (HZ / 10) * TIME_CHAR(tty);
1da177e4 2199 } else {
a6e54319 2200 timeout = (HZ / 10) * TIME_CHAR(tty);
33d71363 2201 minimum = 1;
1da177e4
LT
2202 }
2203 }
2204
64d608db 2205 packet = tty->ctrl.packet;
947d66b6 2206 old_tail = ldata->read_tail;
1da177e4
LT
2207
2208 add_wait_queue(&tty->read_wait, &wait);
1da177e4
LT
2209 while (nr) {
2210 /* First test for status change. */
64d608db 2211 if (packet && tty->link->ctrl.pktstatus) {
1da177e4 2212 unsigned char cs;
3b830a9c 2213 if (kb != kbuf)
1da177e4 2214 break;
64d608db
JS
2215 spin_lock_irq(&tty->link->ctrl.lock);
2216 cs = tty->link->ctrl.pktstatus;
2217 tty->link->ctrl.pktstatus = 0;
2218 spin_unlock_irq(&tty->link->ctrl.lock);
3b830a9c 2219 *kb++ = cs;
1da177e4
LT
2220 nr--;
2221 break;
2222 }
4edf1827 2223
1da177e4 2224 if (!input_available_p(tty, 0)) {
52bce7f8 2225 up_read(&tty->termios_rwsem);
0f40fbbc
BB
2226 tty_buffer_flush_work(tty->port);
2227 down_read(&tty->termios_rwsem);
2228 if (!input_available_p(tty, 0)) {
2229 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
2230 retval = -EIO;
2231 break;
2232 }
2233 if (tty_hung_up_p(file))
2234 break;
28b0f8a6
TH
2235 /*
2236 * Abort readers for ttys which never actually
2237 * get hung up. See __tty_hangup().
2238 */
2239 if (test_bit(TTY_HUPPING, &tty->flags))
2240 break;
0f40fbbc
BB
2241 if (!timeout)
2242 break;
c96cf923 2243 if (tty_io_nonblock(tty, file)) {
0f40fbbc
BB
2244 retval = -EAGAIN;
2245 break;
2246 }
2247 if (signal_pending(current)) {
2248 retval = -ERESTARTSYS;
2249 break;
2250 }
2251 up_read(&tty->termios_rwsem);
9356b535 2252
0f40fbbc
BB
2253 timeout = wait_woken(&wait, TASK_INTERRUPTIBLE,
2254 timeout);
9356b535 2255
0f40fbbc
BB
2256 down_read(&tty->termios_rwsem);
2257 continue;
2258 }
1da177e4
LT
2259 }
2260
53c5ee2c 2261 if (ldata->icanon && !L_EXTPROC(tty)) {
d7fe75cb
LT
2262 if (canon_copy_from_read_buf(tty, &kb, &nr))
2263 goto more_to_be_read;
1da177e4 2264 } else {
95ea90db 2265 /* Deal with packet mode. */
3b830a9c
LT
2266 if (packet && kb == kbuf) {
2267 *kb++ = TIOCPKT_DATA;
95ea90db
PH
2268 nr--;
2269 }
2270
15ea8ae8
LT
2271 /*
2272 * Copy data, and if there is more to be had
2273 * and we have nothing more to wait for, then
2274 * let's mark us for retries.
2275 *
2276 * NOTE! We return here with both the termios_sem
2277 * and atomic_read_lock still held, the retries
2278 * will release them when done.
2279 */
2280 if (copy_from_read_buf(tty, &kb, &nr) && kb - kbuf >= minimum) {
d7fe75cb 2281more_to_be_read:
15ea8ae8
LT
2282 remove_wait_queue(&tty->read_wait, &wait);
2283 *cookie = cookie;
2284 return kb - kbuf;
1da177e4
LT
2285 }
2286 }
2287
6367ca72 2288 n_tty_check_unthrottle(tty);
1da177e4 2289
3b830a9c 2290 if (kb - kbuf >= minimum)
1da177e4
LT
2291 break;
2292 if (time)
2293 timeout = time;
2294 }
4903fde8
HL
2295 if (old_tail != ldata->read_tail) {
2296 /*
2297 * Make sure no_room is not read in n_tty_kick_worker()
2298 * before setting ldata->read_tail in copy_from_read_buf().
2299 */
2300 smp_mb();
2c5dc464 2301 n_tty_kick_worker(tty);
4903fde8 2302 }
42458f41
PH
2303 up_read(&tty->termios_rwsem);
2304
1da177e4 2305 remove_wait_queue(&tty->read_wait, &wait);
aebf0453
PH
2306 mutex_unlock(&ldata->atomic_read_lock);
2307
3b830a9c
LT
2308 if (kb - kbuf)
2309 retval = kb - kbuf;
1da177e4
LT
2310
2311 return retval;
2312}
2313
2314/**
98629663
JS
2315 * n_tty_write - write function for tty
2316 * @tty: tty device
2317 * @file: file object
2318 * @buf: userspace buffer pointer
2319 * @nr: size of I/O
2320 *
2321 * Write function of the terminal device. This is serialized with respect to
2322 * other write callers but not to termios changes, reads and other such events.
2323 * Since the receive code will echo characters, thus calling driver write
2324 * methods, the %output_lock is used in the output processing functions called
2325 * here as well as in the echo processing function to protect the column state
2326 * and space left in the buffer.
2327 *
2328 * This code must be sure never to sleep through a hangup.
2329 *
2330 * Locking: output_lock to protect column state and space left
2331 * (note that the process_output*() functions take this lock themselves)
1da177e4 2332 */
4edf1827 2333
11a96d18 2334static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
a88a69c9 2335 const unsigned char *buf, size_t nr)
1da177e4
LT
2336{
2337 const unsigned char *b = buf;
97d9e28d 2338 DEFINE_WAIT_FUNC(wait, woken_wake_function);
1da177e4
LT
2339 int c;
2340 ssize_t retval = 0;
2341
2342 /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
9f12e37c 2343 if (L_TOSTOP(tty) && file->f_op->write_iter != redirected_tty_write) {
1da177e4
LT
2344 retval = tty_check_change(tty);
2345 if (retval)
2346 return retval;
2347 }
2348
9356b535
PH
2349 down_read(&tty->termios_rwsem);
2350
a88a69c9
JP
2351 /* Write out any echoed characters that are still pending */
2352 process_echoes(tty);
300a6204 2353
1da177e4
LT
2354 add_wait_queue(&tty->write_wait, &wait);
2355 while (1) {
1da177e4
LT
2356 if (signal_pending(current)) {
2357 retval = -ERESTARTSYS;
2358 break;
2359 }
2360 if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
2361 retval = -EIO;
2362 break;
2363 }
582f5590 2364 if (O_OPOST(tty)) {
1da177e4 2365 while (nr > 0) {
a88a69c9 2366 ssize_t num = process_output_block(tty, b, nr);
1da177e4
LT
2367 if (num < 0) {
2368 if (num == -EAGAIN)
2369 break;
2370 retval = num;
2371 goto break_out;
2372 }
2373 b += num;
2374 nr -= num;
2375 if (nr == 0)
2376 break;
2377 c = *b;
a88a69c9 2378 if (process_output(c, tty) < 0)
1da177e4
LT
2379 break;
2380 b++; nr--;
2381 }
f34d7a5b
AC
2382 if (tty->ops->flush_chars)
2383 tty->ops->flush_chars(tty);
1da177e4 2384 } else {
4291086b
PH
2385 struct n_tty_data *ldata = tty->disc_data;
2386
d6afe27b 2387 while (nr > 0) {
4291086b 2388 mutex_lock(&ldata->output_lock);
f34d7a5b 2389 c = tty->ops->write(tty, b, nr);
4291086b 2390 mutex_unlock(&ldata->output_lock);
d6afe27b
RZ
2391 if (c < 0) {
2392 retval = c;
2393 goto break_out;
2394 }
2395 if (!c)
2396 break;
2397 b += c;
2398 nr -= c;
1da177e4 2399 }
1da177e4
LT
2400 }
2401 if (!nr)
2402 break;
c96cf923 2403 if (tty_io_nonblock(tty, file)) {
1da177e4
LT
2404 retval = -EAGAIN;
2405 break;
2406 }
9356b535
PH
2407 up_read(&tty->termios_rwsem);
2408
97d9e28d 2409 wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
9356b535
PH
2410
2411 down_read(&tty->termios_rwsem);
1da177e4
LT
2412 }
2413break_out:
1da177e4 2414 remove_wait_queue(&tty->write_wait, &wait);
87108bc9 2415 if (nr && tty->fasync)
ff8cb0fd 2416 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
9356b535 2417 up_read(&tty->termios_rwsem);
1da177e4
LT
2418 return (b - buf) ? b - buf : retval;
2419}
2420
2421/**
98629663
JS
2422 * n_tty_poll - poll method for N_TTY
2423 * @tty: terminal device
2424 * @file: file accessing it
2425 * @wait: poll table
1da177e4 2426 *
98629663
JS
2427 * Called when the line discipline is asked to poll() for data or for special
2428 * events. This code is not serialized with respect to other events save
2429 * open/close.
1da177e4 2430 *
98629663
JS
2431 * This code must be sure never to sleep through a hangup.
2432 *
2433 * Locking: called without the kernel lock held -- fine.
1da177e4 2434 */
afc9a42b 2435static __poll_t n_tty_poll(struct tty_struct *tty, struct file *file,
4edf1827 2436 poll_table *wait)
1da177e4 2437{
afc9a42b 2438 __poll_t mask = 0;
1da177e4
LT
2439
2440 poll_wait(file, &tty->read_wait, wait);
2441 poll_wait(file, &tty->write_wait, wait);
eafbe67f 2442 if (input_available_p(tty, 1))
a9a08845 2443 mask |= EPOLLIN | EPOLLRDNORM;
0f40fbbc
BB
2444 else {
2445 tty_buffer_flush_work(tty->port);
2446 if (input_available_p(tty, 1))
a9a08845 2447 mask |= EPOLLIN | EPOLLRDNORM;
0f40fbbc 2448 }
64d608db 2449 if (tty->ctrl.packet && tty->link->ctrl.pktstatus)
a9a08845 2450 mask |= EPOLLPRI | EPOLLIN | EPOLLRDNORM;
0f40fbbc 2451 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
a9a08845 2452 mask |= EPOLLHUP;
1da177e4 2453 if (tty_hung_up_p(file))
a9a08845 2454 mask |= EPOLLHUP;
f34d7a5b
AC
2455 if (tty->ops->write && !tty_is_writelocked(tty) &&
2456 tty_chars_in_buffer(tty) < WAKEUP_CHARS &&
2457 tty_write_room(tty) > 0)
a9a08845 2458 mask |= EPOLLOUT | EPOLLWRNORM;
1da177e4
LT
2459 return mask;
2460}
2461
57c94121 2462static unsigned long inq_canon(struct n_tty_data *ldata)
47afa7a5 2463{
bc5a5e3f 2464 size_t nr, head, tail;
47afa7a5 2465
a73d3d69 2466 if (ldata->canon_head == ldata->read_tail)
47afa7a5 2467 return 0;
ba2e68ac
JS
2468 head = ldata->canon_head;
2469 tail = ldata->read_tail;
bc5a5e3f 2470 nr = head - tail;
47afa7a5 2471 /* Skip EOF-chars.. */
3d63b7e4 2472 while (MASK(head) != MASK(tail)) {
bc5a5e3f
PH
2473 if (test_bit(tail & (N_TTY_BUF_SIZE - 1), ldata->read_flags) &&
2474 read_buf(ldata, tail) == __DISABLED_CHAR)
47afa7a5 2475 nr--;
bc5a5e3f 2476 tail++;
47afa7a5
AC
2477 }
2478 return nr;
2479}
2480
d78328bc
JS
2481static int n_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
2482 unsigned long arg)
47afa7a5 2483{
ba2e68ac 2484 struct n_tty_data *ldata = tty->disc_data;
47afa7a5
AC
2485 int retval;
2486
2487 switch (cmd) {
2488 case TIOCOUTQ:
2489 return put_user(tty_chars_in_buffer(tty), (int __user *) arg);
2490 case TIOCINQ:
6d76bd26 2491 down_write(&tty->termios_rwsem);
966031f3 2492 if (L_ICANON(tty) && !L_EXTPROC(tty))
57c94121 2493 retval = inq_canon(ldata);
6d76bd26
PH
2494 else
2495 retval = read_cnt(ldata);
2496 up_write(&tty->termios_rwsem);
47afa7a5
AC
2497 return put_user(retval, (unsigned int __user *) arg);
2498 default:
7c783601 2499 return n_tty_ioctl_helper(tty, cmd, arg);
47afa7a5
AC
2500 }
2501}
2502
27228732 2503static struct tty_ldisc_ops n_tty_ops = {
5e30d3bf 2504 .owner = THIS_MODULE,
fbadf70a 2505 .num = N_TTY,
e10cc1df
PF
2506 .name = "n_tty",
2507 .open = n_tty_open,
2508 .close = n_tty_close,
2509 .flush_buffer = n_tty_flush_buffer,
11a96d18
AC
2510 .read = n_tty_read,
2511 .write = n_tty_write,
e10cc1df
PF
2512 .ioctl = n_tty_ioctl,
2513 .set_termios = n_tty_set_termios,
11a96d18 2514 .poll = n_tty_poll,
e10cc1df 2515 .receive_buf = n_tty_receive_buf,
f6c8dbe6 2516 .write_wakeup = n_tty_write_wakeup,
24a89d1c 2517 .receive_buf2 = n_tty_receive_buf2,
6bb6fa69 2518 .lookahead_buf = n_tty_lookahead_flow_ctrl,
1da177e4 2519};
572b9adb
RG
2520
2521/**
2522 * n_tty_inherit_ops - inherit N_TTY methods
2523 * @ops: struct tty_ldisc_ops where to save N_TTY methods
2524 *
27228732 2525 * Enables a 'subclass' line discipline to 'inherit' N_TTY methods.
572b9adb
RG
2526 */
2527
2528void n_tty_inherit_ops(struct tty_ldisc_ops *ops)
2529{
27228732 2530 *ops = n_tty_ops;
572b9adb 2531 ops->owner = NULL;
572b9adb
RG
2532}
2533EXPORT_SYMBOL_GPL(n_tty_inherit_ops);
27228732
PH
2534
2535void __init n_tty_init(void)
2536{
fbadf70a 2537 tty_register_ldisc(&n_tty_ops);
27228732 2538}
This page took 2.116248 seconds and 4 git commands to generate.