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