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