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