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