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