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