Root/drivers/tty/n_tty.c

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

Archive Download this file



interactive