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