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