]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/char/n_tty.c
tty: The big operations rework
[linux-2.6-omap-h63xx.git] / drivers / char / 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 write_chan 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
51 #include <asm/uaccess.h>
52 #include <asm/system.h>
53
54 /* number of characters left in xmit buffer before select has we have room */
55 #define WAKEUP_CHARS 256
56
57 /*
58  * This defines the low- and high-watermarks for throttling and
59  * unthrottling the TTY driver.  These watermarks are used for
60  * controlling the space in the read buffer.
61  */
62 #define TTY_THRESHOLD_THROTTLE          128 /* now based on remaining room */
63 #define TTY_THRESHOLD_UNTHROTTLE        128
64
65 static inline unsigned char *alloc_buf(void)
66 {
67         gfp_t prio = in_interrupt() ? GFP_ATOMIC : GFP_KERNEL;
68
69         if (PAGE_SIZE != N_TTY_BUF_SIZE)
70                 return kmalloc(N_TTY_BUF_SIZE, prio);
71         else
72                 return (unsigned char *)__get_free_page(prio);
73 }
74
75 static inline void free_buf(unsigned char *buf)
76 {
77         if (PAGE_SIZE != N_TTY_BUF_SIZE)
78                 kfree(buf);
79         else
80                 free_page((unsigned long) buf);
81 }
82
83 static inline int tty_put_user(struct tty_struct *tty, unsigned char x,
84                                unsigned char __user *ptr)
85 {
86         tty_audit_add_data(tty, &x, 1);
87         return put_user(x, ptr);
88 }
89
90 /**
91  *      n_tty_set__room -       receive space
92  *      @tty: terminal
93  *
94  *      Called by the driver to find out how much data it is
95  *      permitted to feed to the line discipline without any being lost
96  *      and thus to manage flow control. Not serialized. Answers for the
97  *      "instant".
98  */
99
100 static void n_tty_set_room(struct tty_struct *tty)
101 {
102         int     left = N_TTY_BUF_SIZE - tty->read_cnt - 1;
103
104         /*
105          * If we are doing input canonicalization, and there are no
106          * pending newlines, let characters through without limit, so
107          * that erase characters will be handled.  Other excess
108          * characters will be beeped.
109          */
110         if (left <= 0)
111                 left = tty->icanon && !tty->canon_data;
112         tty->receive_room = left;
113 }
114
115 static void put_tty_queue_nolock(unsigned char c, struct tty_struct *tty)
116 {
117         if (tty->read_cnt < N_TTY_BUF_SIZE) {
118                 tty->read_buf[tty->read_head] = c;
119                 tty->read_head = (tty->read_head + 1) & (N_TTY_BUF_SIZE-1);
120                 tty->read_cnt++;
121         }
122 }
123
124 static void put_tty_queue(unsigned char c, struct tty_struct *tty)
125 {
126         unsigned long flags;
127         /*
128          *      The problem of stomping on the buffers ends here.
129          *      Why didn't anyone see this one coming? --AJK
130         */
131         spin_lock_irqsave(&tty->read_lock, flags);
132         put_tty_queue_nolock(c, tty);
133         spin_unlock_irqrestore(&tty->read_lock, flags);
134 }
135
136 /**
137  *      check_unthrottle        -       allow new receive data
138  *      @tty; tty device
139  *
140  *      Check whether to call the driver.unthrottle function.
141  *      We test the TTY_THROTTLED bit first so that it always
142  *      indicates the current state. The decision about whether
143  *      it is worth allowing more input has been taken by the caller.
144  *      Can sleep, may be called under the atomic_read_lock mutex but
145  *      this is not guaranteed.
146  */
147
148 static void check_unthrottle(struct tty_struct *tty)
149 {
150         if (tty->count &&
151             test_and_clear_bit(TTY_THROTTLED, &tty->flags) &&
152             tty->ops->unthrottle)
153                 tty->ops->unthrottle(tty);
154 }
155
156 /**
157  *      reset_buffer_flags      -       reset buffer state
158  *      @tty: terminal to reset
159  *
160  *      Reset the read buffer counters, clear the flags,
161  *      and make sure the driver is unthrottled. Called
162  *      from n_tty_open() and n_tty_flush_buffer().
163  */
164 static void reset_buffer_flags(struct tty_struct *tty)
165 {
166         unsigned long flags;
167
168         spin_lock_irqsave(&tty->read_lock, flags);
169         tty->read_head = tty->read_tail = tty->read_cnt = 0;
170         spin_unlock_irqrestore(&tty->read_lock, flags);
171         tty->canon_head = tty->canon_data = tty->erasing = 0;
172         memset(&tty->read_flags, 0, sizeof tty->read_flags);
173         n_tty_set_room(tty);
174         check_unthrottle(tty);
175 }
176
177 /**
178  *      n_tty_flush_buffer      -       clean input queue
179  *      @tty:   terminal device
180  *
181  *      Flush the input buffer. Called when the line discipline is
182  *      being closed, when the tty layer wants the buffer flushed (eg
183  *      at hangup) or when the N_TTY line discipline internally has to
184  *      clean the pending queue (for example some signals).
185  *
186  *      Locking: ctrl_lock
187  */
188
189 static void n_tty_flush_buffer(struct tty_struct *tty)
190 {
191         unsigned long flags;
192         /* clear everything and unthrottle the driver */
193         reset_buffer_flags(tty);
194
195         if (!tty->link)
196                 return;
197
198         spin_lock_irqsave(&tty->ctrl_lock, flags);
199         if (tty->link->packet) {
200                 tty->ctrl_status |= TIOCPKT_FLUSHREAD;
201                 wake_up_interruptible(&tty->link->read_wait);
202         }
203         spin_unlock_irqrestore(&tty->ctrl_lock, flags);
204 }
205
206 /**
207  *      n_tty_chars_in_buffer   -       report available bytes
208  *      @tty: tty device
209  *
210  *      Report the number of characters buffered to be delivered to user
211  *      at this instant in time.
212  */
213
214 static ssize_t n_tty_chars_in_buffer(struct tty_struct *tty)
215 {
216         unsigned long flags;
217         ssize_t n = 0;
218
219         spin_lock_irqsave(&tty->read_lock, flags);
220         if (!tty->icanon) {
221                 n = tty->read_cnt;
222         } else if (tty->canon_data) {
223                 n = (tty->canon_head > tty->read_tail) ?
224                         tty->canon_head - tty->read_tail :
225                         tty->canon_head + (N_TTY_BUF_SIZE - tty->read_tail);
226         }
227         spin_unlock_irqrestore(&tty->read_lock, flags);
228         return n;
229 }
230
231 /**
232  *      is_utf8_continuation    -       utf8 multibyte check
233  *      @c: byte to check
234  *
235  *      Returns true if the utf8 character 'c' is a multibyte continuation
236  *      character. We use this to correctly compute the on screen size
237  *      of the character when printing
238  */
239
240 static inline int is_utf8_continuation(unsigned char c)
241 {
242         return (c & 0xc0) == 0x80;
243 }
244
245 /**
246  *      is_continuation         -       multibyte check
247  *      @c: byte to check
248  *
249  *      Returns true if the utf8 character 'c' is a multibyte continuation
250  *      character and the terminal is in unicode mode.
251  */
252
253 static inline int is_continuation(unsigned char c, struct tty_struct *tty)
254 {
255         return I_IUTF8(tty) && is_utf8_continuation(c);
256 }
257
258 /**
259  *      opost                   -       output post processor
260  *      @c: character (or partial unicode symbol)
261  *      @tty: terminal device
262  *
263  *      Perform OPOST processing.  Returns -1 when the output device is
264  *      full and the character must be retried. Note that Linux currently
265  *      ignores TABDLY, CRDLY, VTDLY, FFDLY and NLDLY. They simply aren't
266  *      relevant in the world today. If you ever need them, add them here.
267  *
268  *      Called from both the receive and transmit sides and can be called
269  *      re-entrantly. Relies on lock_kernel() for tty->column state.
270  */
271
272 static int opost(unsigned char c, struct tty_struct *tty)
273 {
274         int     space, spaces;
275
276         space = tty_write_room(tty);
277         if (!space)
278                 return -1;
279
280         lock_kernel();
281         if (O_OPOST(tty)) {
282                 switch (c) {
283                 case '\n':
284                         if (O_ONLRET(tty))
285                                 tty->column = 0;
286                         if (O_ONLCR(tty)) {
287                                 if (space < 2)
288                                         return -1;
289                                 tty_put_char(tty, '\r');
290                                 tty->column = 0;
291                         }
292                         tty->canon_column = tty->column;
293                         break;
294                 case '\r':
295                         if (O_ONOCR(tty) && tty->column == 0)
296                                 return 0;
297                         if (O_OCRNL(tty)) {
298                                 c = '\n';
299                                 if (O_ONLRET(tty))
300                                         tty->canon_column = tty->column = 0;
301                                 break;
302                         }
303                         tty->canon_column = tty->column = 0;
304                         break;
305                 case '\t':
306                         spaces = 8 - (tty->column & 7);
307                         if (O_TABDLY(tty) == XTABS) {
308                                 if (space < spaces)
309                                         return -1;
310                                 tty->column += spaces;
311                                 tty->ops->write(tty, "        ", spaces);
312                                 return 0;
313                         }
314                         tty->column += spaces;
315                         break;
316                 case '\b':
317                         if (tty->column > 0)
318                                 tty->column--;
319                         break;
320                 default:
321                         if (O_OLCUC(tty))
322                                 c = toupper(c);
323                         if (!iscntrl(c) && !is_continuation(c, tty))
324                                 tty->column++;
325                         break;
326                 }
327         }
328         tty_put_char(tty, c);
329         unlock_kernel();
330         return 0;
331 }
332
333 /**
334  *      opost_block             -       block postprocess
335  *      @tty: terminal device
336  *      @inbuf: user buffer
337  *      @nr: number of bytes
338  *
339  *      This path is used to speed up block console writes, among other
340  *      things when processing blocks of output data. It handles only
341  *      the simple cases normally found and helps to generate blocks of
342  *      symbols for the console driver and thus improve performance.
343  *
344  *      Called from write_chan under the tty layer write lock. Relies
345  *      on lock_kernel for the tty->column state.
346  */
347
348 static ssize_t opost_block(struct tty_struct *tty,
349                        const unsigned char *buf, unsigned int nr)
350 {
351         int     space;
352         int     i;
353         const unsigned char *cp;
354
355         space = tty_write_room(tty);
356         if (!space)
357                 return 0;
358         if (nr > space)
359                 nr = space;
360
361         lock_kernel();
362         for (i = 0, cp = buf; i < nr; i++, cp++) {
363                 switch (*cp) {
364                 case '\n':
365                         if (O_ONLRET(tty))
366                                 tty->column = 0;
367                         if (O_ONLCR(tty))
368                                 goto break_out;
369                         tty->canon_column = tty->column;
370                         break;
371                 case '\r':
372                         if (O_ONOCR(tty) && tty->column == 0)
373                                 goto break_out;
374                         if (O_OCRNL(tty))
375                                 goto break_out;
376                         tty->canon_column = tty->column = 0;
377                         break;
378                 case '\t':
379                         goto break_out;
380                 case '\b':
381                         if (tty->column > 0)
382                                 tty->column--;
383                         break;
384                 default:
385                         if (O_OLCUC(tty))
386                                 goto break_out;
387                         if (!iscntrl(*cp))
388                                 tty->column++;
389                         break;
390                 }
391         }
392 break_out:
393         if (tty->ops->flush_chars)
394                 tty->ops->flush_chars(tty);
395         i = tty->ops->write(tty, buf, i);
396         unlock_kernel();
397         return i;
398 }
399
400
401 /**
402  *      echo_char       -       echo characters
403  *      @c: unicode byte to echo
404  *      @tty: terminal device
405  *
406  *      Echo user input back onto the screen. This must be called only when
407  *      L_ECHO(tty) is true. Called from the driver receive_buf path.
408  */
409
410 static void echo_char(unsigned char c, struct tty_struct *tty)
411 {
412         if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t') {
413                 tty_put_char(tty, '^');
414                 tty_put_char(tty, c ^ 0100);
415                 tty->column += 2;
416         } else
417                 opost(c, tty);
418 }
419
420 static inline void finish_erasing(struct tty_struct *tty)
421 {
422         if (tty->erasing) {
423                 tty_put_char(tty, '/');
424                 tty->column++;
425                 tty->erasing = 0;
426         }
427 }
428
429 /**
430  *      eraser          -       handle erase function
431  *      @c: character input
432  *      @tty: terminal device
433  *
434  *      Perform erase and necessary output when an erase character is
435  *      present in the stream from the driver layer. Handles the complexities
436  *      of UTF-8 multibyte symbols.
437  */
438
439 static void eraser(unsigned char c, struct tty_struct *tty)
440 {
441         enum { ERASE, WERASE, KILL } kill_type;
442         int head, seen_alnums, cnt;
443         unsigned long flags;
444
445         if (tty->read_head == tty->canon_head) {
446                 /* opost('\a', tty); */         /* what do you think? */
447                 return;
448         }
449         if (c == ERASE_CHAR(tty))
450                 kill_type = ERASE;
451         else if (c == WERASE_CHAR(tty))
452                 kill_type = WERASE;
453         else {
454                 if (!L_ECHO(tty)) {
455                         spin_lock_irqsave(&tty->read_lock, flags);
456                         tty->read_cnt -= ((tty->read_head - tty->canon_head) &
457                                           (N_TTY_BUF_SIZE - 1));
458                         tty->read_head = tty->canon_head;
459                         spin_unlock_irqrestore(&tty->read_lock, flags);
460                         return;
461                 }
462                 if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
463                         spin_lock_irqsave(&tty->read_lock, flags);
464                         tty->read_cnt -= ((tty->read_head - tty->canon_head) &
465                                           (N_TTY_BUF_SIZE - 1));
466                         tty->read_head = tty->canon_head;
467                         spin_unlock_irqrestore(&tty->read_lock, flags);
468                         finish_erasing(tty);
469                         echo_char(KILL_CHAR(tty), tty);
470                         /* Add a newline if ECHOK is on and ECHOKE is off. */
471                         if (L_ECHOK(tty))
472                                 opost('\n', tty);
473                         return;
474                 }
475                 kill_type = KILL;
476         }
477
478         seen_alnums = 0;
479         while (tty->read_head != tty->canon_head) {
480                 head = tty->read_head;
481
482                 /* erase a single possibly multibyte character */
483                 do {
484                         head = (head - 1) & (N_TTY_BUF_SIZE-1);
485                         c = tty->read_buf[head];
486                 } while (is_continuation(c, tty) && head != tty->canon_head);
487
488                 /* do not partially erase */
489                 if (is_continuation(c, tty))
490                         break;
491
492                 if (kill_type == WERASE) {
493                         /* Equivalent to BSD's ALTWERASE. */
494                         if (isalnum(c) || c == '_')
495                                 seen_alnums++;
496                         else if (seen_alnums)
497                                 break;
498                 }
499                 cnt = (tty->read_head - head) & (N_TTY_BUF_SIZE-1);
500                 spin_lock_irqsave(&tty->read_lock, flags);
501                 tty->read_head = head;
502                 tty->read_cnt -= cnt;
503                 spin_unlock_irqrestore(&tty->read_lock, flags);
504                 if (L_ECHO(tty)) {
505                         if (L_ECHOPRT(tty)) {
506                                 if (!tty->erasing) {
507                                         tty_put_char(tty, '\\');
508                                         tty->column++;
509                                         tty->erasing = 1;
510                                 }
511                                 /* if cnt > 1, output a multi-byte character */
512                                 echo_char(c, tty);
513                                 while (--cnt > 0) {
514                                         head = (head+1) & (N_TTY_BUF_SIZE-1);
515                                         tty_put_char(tty, tty->read_buf[head]);
516                                 }
517                         } else if (kill_type == ERASE && !L_ECHOE(tty)) {
518                                 echo_char(ERASE_CHAR(tty), tty);
519                         } else if (c == '\t') {
520                                 unsigned int col = tty->canon_column;
521                                 unsigned long tail = tty->canon_head;
522
523                                 /* Find the column of the last char. */
524                                 while (tail != tty->read_head) {
525                                         c = tty->read_buf[tail];
526                                         if (c == '\t')
527                                                 col = (col | 7) + 1;
528                                         else if (iscntrl(c)) {
529                                                 if (L_ECHOCTL(tty))
530                                                         col += 2;
531                                         } else if (!is_continuation(c, tty))
532                                                 col++;
533                                         tail = (tail+1) & (N_TTY_BUF_SIZE-1);
534                                 }
535
536                                 /* should never happen */
537                                 if (tty->column > 0x80000000)
538                                         tty->column = 0;
539
540                                 /* Now backup to that column. */
541                                 while (tty->column > col) {
542                                         /* Can't use opost here. */
543                                         tty_put_char(tty, '\b');
544                                         if (tty->column > 0)
545                                                 tty->column--;
546                                 }
547                         } else {
548                                 if (iscntrl(c) && L_ECHOCTL(tty)) {
549                                         tty_put_char(tty, '\b');
550                                         tty_put_char(tty, ' ');
551                                         tty_put_char(tty, '\b');
552                                         if (tty->column > 0)
553                                                 tty->column--;
554                                 }
555                                 if (!iscntrl(c) || L_ECHOCTL(tty)) {
556                                         tty_put_char(tty, '\b');
557                                         tty_put_char(tty, ' ');
558                                         tty_put_char(tty, '\b');
559                                         if (tty->column > 0)
560                                                 tty->column--;
561                                 }
562                         }
563                 }
564                 if (kill_type == ERASE)
565                         break;
566         }
567         if (tty->read_head == tty->canon_head)
568                 finish_erasing(tty);
569 }
570
571 /**
572  *      isig            -       handle the ISIG optio
573  *      @sig: signal
574  *      @tty: terminal
575  *      @flush: force flush
576  *
577  *      Called when a signal is being sent due to terminal input. This
578  *      may caus terminal flushing to take place according to the termios
579  *      settings and character used. Called from the driver receive_buf
580  *      path so serialized.
581  */
582
583 static inline void isig(int sig, struct tty_struct *tty, int flush)
584 {
585         if (tty->pgrp)
586                 kill_pgrp(tty->pgrp, sig, 1);
587         if (flush || !L_NOFLSH(tty)) {
588                 n_tty_flush_buffer(tty);
589                 tty_driver_flush_buffer(tty);
590         }
591 }
592
593 /**
594  *      n_tty_receive_break     -       handle break
595  *      @tty: terminal
596  *
597  *      An RS232 break event has been hit in the incoming bitstream. This
598  *      can cause a variety of events depending upon the termios settings.
599  *
600  *      Called from the receive_buf path so single threaded.
601  */
602
603 static inline void n_tty_receive_break(struct tty_struct *tty)
604 {
605         if (I_IGNBRK(tty))
606                 return;
607         if (I_BRKINT(tty)) {
608                 isig(SIGINT, tty, 1);
609                 return;
610         }
611         if (I_PARMRK(tty)) {
612                 put_tty_queue('\377', tty);
613                 put_tty_queue('\0', tty);
614         }
615         put_tty_queue('\0', tty);
616         wake_up_interruptible(&tty->read_wait);
617 }
618
619 /**
620  *      n_tty_receive_overrun   -       handle overrun reporting
621  *      @tty: terminal
622  *
623  *      Data arrived faster than we could process it. While the tty
624  *      driver has flagged this the bits that were missed are gone
625  *      forever.
626  *
627  *      Called from the receive_buf path so single threaded. Does not
628  *      need locking as num_overrun and overrun_time are function
629  *      private.
630  */
631
632 static inline void n_tty_receive_overrun(struct tty_struct *tty)
633 {
634         char buf[64];
635
636         tty->num_overrun++;
637         if (time_before(tty->overrun_time, jiffies - HZ) ||
638                         time_after(tty->overrun_time, jiffies)) {
639                 printk(KERN_WARNING "%s: %d input overrun(s)\n",
640                         tty_name(tty, buf),
641                         tty->num_overrun);
642                 tty->overrun_time = jiffies;
643                 tty->num_overrun = 0;
644         }
645 }
646
647 /**
648  *      n_tty_receive_parity_error      -       error notifier
649  *      @tty: terminal device
650  *      @c: character
651  *
652  *      Process a parity error and queue the right data to indicate
653  *      the error case if necessary. Locking as per n_tty_receive_buf.
654  */
655 static inline void n_tty_receive_parity_error(struct tty_struct *tty,
656                                               unsigned char c)
657 {
658         if (I_IGNPAR(tty))
659                 return;
660         if (I_PARMRK(tty)) {
661                 put_tty_queue('\377', tty);
662                 put_tty_queue('\0', tty);
663                 put_tty_queue(c, tty);
664         } else  if (I_INPCK(tty))
665                 put_tty_queue('\0', tty);
666         else
667                 put_tty_queue(c, tty);
668         wake_up_interruptible(&tty->read_wait);
669 }
670
671 /**
672  *      n_tty_receive_char      -       perform processing
673  *      @tty: terminal device
674  *      @c: character
675  *
676  *      Process an individual character of input received from the driver.
677  *      This is serialized with respect to itself by the rules for the
678  *      driver above.
679  */
680
681 static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
682 {
683         unsigned long flags;
684
685         if (tty->raw) {
686                 put_tty_queue(c, tty);
687                 return;
688         }
689
690         if (I_ISTRIP(tty))
691                 c &= 0x7f;
692         if (I_IUCLC(tty) && L_IEXTEN(tty))
693                 c=tolower(c);
694
695         if (tty->stopped && !tty->flow_stopped && I_IXON(tty) &&
696             ((I_IXANY(tty) && c != START_CHAR(tty) && c != STOP_CHAR(tty)) ||
697              c == INTR_CHAR(tty) || c == QUIT_CHAR(tty) || c == SUSP_CHAR(tty)))
698                 start_tty(tty);
699
700         if (tty->closing) {
701                 if (I_IXON(tty)) {
702                         if (c == START_CHAR(tty))
703                                 start_tty(tty);
704                         else if (c == STOP_CHAR(tty))
705                                 stop_tty(tty);
706                 }
707                 return;
708         }
709
710         /*
711          * If the previous character was LNEXT, or we know that this
712          * character is not one of the characters that we'll have to
713          * handle specially, do shortcut processing to speed things
714          * up.
715          */
716         if (!test_bit(c, tty->process_char_map) || tty->lnext) {
717                 finish_erasing(tty);
718                 tty->lnext = 0;
719                 if (L_ECHO(tty)) {
720                         if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
721                                 tty_put_char(tty, '\a'); /* beep if no space */
722                                 return;
723                         }
724                         /* Record the column of first canon char. */
725                         if (tty->canon_head == tty->read_head)
726                                 tty->canon_column = tty->column;
727                         echo_char(c, tty);
728                 }
729                 if (I_PARMRK(tty) && c == (unsigned char) '\377')
730                         put_tty_queue(c, tty);
731                 put_tty_queue(c, tty);
732                 return;
733         }
734
735         if (I_IXON(tty)) {
736                 if (c == START_CHAR(tty)) {
737                         start_tty(tty);
738                         return;
739                 }
740                 if (c == STOP_CHAR(tty)) {
741                         stop_tty(tty);
742                         return;
743                 }
744         }
745
746         if (L_ISIG(tty)) {
747                 int signal;
748                 signal = SIGINT;
749                 if (c == INTR_CHAR(tty))
750                         goto send_signal;
751                 signal = SIGQUIT;
752                 if (c == QUIT_CHAR(tty))
753                         goto send_signal;
754                 signal = SIGTSTP;
755                 if (c == SUSP_CHAR(tty)) {
756 send_signal:
757                         /*
758                          * Echo character, and then send the signal.
759                          * Note that we do not use isig() here because we want
760                          * the order to be:
761                          * 1) flush, 2) echo, 3) signal
762                          */
763                         if (!L_NOFLSH(tty)) {
764                                 n_tty_flush_buffer(tty);
765                                 tty_driver_flush_buffer(tty);
766                         }
767                         if (L_ECHO(tty))
768                                 echo_char(c, tty);
769                         if (tty->pgrp)
770                                 kill_pgrp(tty->pgrp, signal, 1);
771                         return;
772                 }
773         }
774
775         if (c == '\r') {
776                 if (I_IGNCR(tty))
777                         return;
778                 if (I_ICRNL(tty))
779                         c = '\n';
780         } else if (c == '\n' && I_INLCR(tty))
781                 c = '\r';
782
783         if (tty->icanon) {
784                 if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
785                     (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
786                         eraser(c, tty);
787                         return;
788                 }
789                 if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
790                         tty->lnext = 1;
791                         if (L_ECHO(tty)) {
792                                 finish_erasing(tty);
793                                 if (L_ECHOCTL(tty)) {
794                                         tty_put_char(tty, '^');
795                                         tty_put_char(tty, '\b');
796                                 }
797                         }
798                         return;
799                 }
800                 if (c == REPRINT_CHAR(tty) && L_ECHO(tty) &&
801                     L_IEXTEN(tty)) {
802                         unsigned long tail = tty->canon_head;
803
804                         finish_erasing(tty);
805                         echo_char(c, tty);
806                         opost('\n', tty);
807                         while (tail != tty->read_head) {
808                                 echo_char(tty->read_buf[tail], tty);
809                                 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
810                         }
811                         return;
812                 }
813                 if (c == '\n') {
814                         if (L_ECHO(tty) || L_ECHONL(tty)) {
815                                 if (tty->read_cnt >= N_TTY_BUF_SIZE-1)
816                                         tty_put_char(tty, '\a');
817                                 opost('\n', tty);
818                         }
819                         goto handle_newline;
820                 }
821                 if (c == EOF_CHAR(tty)) {
822                         if (tty->canon_head != tty->read_head)
823                                 set_bit(TTY_PUSH, &tty->flags);
824                         c = __DISABLED_CHAR;
825                         goto handle_newline;
826                 }
827                 if ((c == EOL_CHAR(tty)) ||
828                     (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
829                         /*
830                          * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
831                          */
832                         if (L_ECHO(tty)) {
833                                 if (tty->read_cnt >= N_TTY_BUF_SIZE-1)
834                                         tty_put_char(tty, '\a');
835                                 /* Record the column of first canon char. */
836                                 if (tty->canon_head == tty->read_head)
837                                         tty->canon_column = tty->column;
838                                 echo_char(c, tty);
839                         }
840                         /*
841                          * XXX does PARMRK doubling happen for
842                          * EOL_CHAR and EOL2_CHAR?
843                          */
844                         if (I_PARMRK(tty) && c == (unsigned char) '\377')
845                                 put_tty_queue(c, tty);
846
847 handle_newline:
848                         spin_lock_irqsave(&tty->read_lock, flags);
849                         set_bit(tty->read_head, tty->read_flags);
850                         put_tty_queue_nolock(c, tty);
851                         tty->canon_head = tty->read_head;
852                         tty->canon_data++;
853                         spin_unlock_irqrestore(&tty->read_lock, flags);
854                         kill_fasync(&tty->fasync, SIGIO, POLL_IN);
855                         if (waitqueue_active(&tty->read_wait))
856                                 wake_up_interruptible(&tty->read_wait);
857                         return;
858                 }
859         }
860
861         finish_erasing(tty);
862         if (L_ECHO(tty)) {
863                 if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
864                         tty_put_char(tty, '\a'); /* beep if no space */
865                         return;
866                 }
867                 if (c == '\n')
868                         opost('\n', tty);
869                 else {
870                         /* Record the column of first canon char. */
871                         if (tty->canon_head == tty->read_head)
872                                 tty->canon_column = tty->column;
873                         echo_char(c, tty);
874                 }
875         }
876
877         if (I_PARMRK(tty) && c == (unsigned char) '\377')
878                 put_tty_queue(c, tty);
879
880         put_tty_queue(c, tty);
881 }
882
883
884 /**
885  *      n_tty_write_wakeup      -       asynchronous I/O notifier
886  *      @tty: tty device
887  *
888  *      Required for the ptys, serial driver etc. since processes
889  *      that attach themselves to the master and rely on ASYNC
890  *      IO must be woken up
891  */
892
893 static void n_tty_write_wakeup(struct tty_struct *tty)
894 {
895         if (tty->fasync) {
896                 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
897                 kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
898         }
899 }
900
901 /**
902  *      n_tty_receive_buf       -       data receive
903  *      @tty: terminal device
904  *      @cp: buffer
905  *      @fp: flag buffer
906  *      @count: characters
907  *
908  *      Called by the terminal driver when a block of characters has
909  *      been received. This function must be called from soft contexts
910  *      not from interrupt context. The driver is responsible for making
911  *      calls one at a time and in order (or using flush_to_ldisc)
912  */
913
914 static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
915                               char *fp, int count)
916 {
917         const unsigned char *p;
918         char *f, flags = TTY_NORMAL;
919         int     i;
920         char    buf[64];
921         unsigned long cpuflags;
922
923         if (!tty->read_buf)
924                 return;
925
926         if (tty->real_raw) {
927                 spin_lock_irqsave(&tty->read_lock, cpuflags);
928                 i = min(N_TTY_BUF_SIZE - tty->read_cnt,
929                         N_TTY_BUF_SIZE - tty->read_head);
930                 i = min(count, i);
931                 memcpy(tty->read_buf + tty->read_head, cp, i);
932                 tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
933                 tty->read_cnt += i;
934                 cp += i;
935                 count -= i;
936
937                 i = min(N_TTY_BUF_SIZE - tty->read_cnt,
938                         N_TTY_BUF_SIZE - tty->read_head);
939                 i = min(count, i);
940                 memcpy(tty->read_buf + tty->read_head, cp, i);
941                 tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
942                 tty->read_cnt += i;
943                 spin_unlock_irqrestore(&tty->read_lock, cpuflags);
944         } else {
945                 for (i = count, p = cp, f = fp; i; i--, p++) {
946                         if (f)
947                                 flags = *f++;
948                         switch (flags) {
949                         case TTY_NORMAL:
950                                 n_tty_receive_char(tty, *p);
951                                 break;
952                         case TTY_BREAK:
953                                 n_tty_receive_break(tty);
954                                 break;
955                         case TTY_PARITY:
956                         case TTY_FRAME:
957                                 n_tty_receive_parity_error(tty, *p);
958                                 break;
959                         case TTY_OVERRUN:
960                                 n_tty_receive_overrun(tty);
961                                 break;
962                         default:
963                                 printk(KERN_ERR "%s: unknown flag %d\n",
964                                        tty_name(tty, buf), flags);
965                                 break;
966                         }
967                 }
968                 if (tty->ops->flush_chars)
969                         tty->ops->flush_chars(tty);
970         }
971
972         n_tty_set_room(tty);
973
974         if (!tty->icanon && (tty->read_cnt >= tty->minimum_to_wake)) {
975                 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
976                 if (waitqueue_active(&tty->read_wait))
977                         wake_up_interruptible(&tty->read_wait);
978         }
979
980         /*
981          * Check the remaining room for the input canonicalization
982          * mode.  We don't want to throttle the driver if we're in
983          * canonical mode and don't have a newline yet!
984          */
985         if (tty->receive_room < TTY_THRESHOLD_THROTTLE) {
986                 /* check TTY_THROTTLED first so it indicates our state */
987                 if (!test_and_set_bit(TTY_THROTTLED, &tty->flags) &&
988                     tty->ops->throttle)
989                         tty->ops->throttle(tty);
990         }
991 }
992
993 int is_ignored(int sig)
994 {
995         return (sigismember(&current->blocked, sig) ||
996                 current->sighand->action[sig-1].sa.sa_handler == SIG_IGN);
997 }
998
999 /**
1000  *      n_tty_set_termios       -       termios data changed
1001  *      @tty: terminal
1002  *      @old: previous data
1003  *
1004  *      Called by the tty layer when the user changes termios flags so
1005  *      that the line discipline can plan ahead. This function cannot sleep
1006  *      and is protected from re-entry by the tty layer. The user is
1007  *      guaranteed that this function will not be re-entered or in progress
1008  *      when the ldisc is closed.
1009  */
1010
1011 static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
1012 {
1013         if (!tty)
1014                 return;
1015
1016         tty->icanon = (L_ICANON(tty) != 0);
1017         if (test_bit(TTY_HW_COOK_IN, &tty->flags)) {
1018                 tty->raw = 1;
1019                 tty->real_raw = 1;
1020                 n_tty_set_room(tty);
1021                 return;
1022         }
1023         if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
1024             I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
1025             I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
1026             I_PARMRK(tty)) {
1027                 memset(tty->process_char_map, 0, 256/8);
1028
1029                 if (I_IGNCR(tty) || I_ICRNL(tty))
1030                         set_bit('\r', tty->process_char_map);
1031                 if (I_INLCR(tty))
1032                         set_bit('\n', tty->process_char_map);
1033
1034                 if (L_ICANON(tty)) {
1035                         set_bit(ERASE_CHAR(tty), tty->process_char_map);
1036                         set_bit(KILL_CHAR(tty), tty->process_char_map);
1037                         set_bit(EOF_CHAR(tty), tty->process_char_map);
1038                         set_bit('\n', tty->process_char_map);
1039                         set_bit(EOL_CHAR(tty), tty->process_char_map);
1040                         if (L_IEXTEN(tty)) {
1041                                 set_bit(WERASE_CHAR(tty),
1042                                         tty->process_char_map);
1043                                 set_bit(LNEXT_CHAR(tty),
1044                                         tty->process_char_map);
1045                                 set_bit(EOL2_CHAR(tty),
1046                                         tty->process_char_map);
1047                                 if (L_ECHO(tty))
1048                                         set_bit(REPRINT_CHAR(tty),
1049                                                 tty->process_char_map);
1050                         }
1051                 }
1052                 if (I_IXON(tty)) {
1053                         set_bit(START_CHAR(tty), tty->process_char_map);
1054                         set_bit(STOP_CHAR(tty), tty->process_char_map);
1055                 }
1056                 if (L_ISIG(tty)) {
1057                         set_bit(INTR_CHAR(tty), tty->process_char_map);
1058                         set_bit(QUIT_CHAR(tty), tty->process_char_map);
1059                         set_bit(SUSP_CHAR(tty), tty->process_char_map);
1060                 }
1061                 clear_bit(__DISABLED_CHAR, tty->process_char_map);
1062                 tty->raw = 0;
1063                 tty->real_raw = 0;
1064         } else {
1065                 tty->raw = 1;
1066                 if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
1067                     (I_IGNPAR(tty) || !I_INPCK(tty)) &&
1068                     (tty->driver->flags & TTY_DRIVER_REAL_RAW))
1069                         tty->real_raw = 1;
1070                 else
1071                         tty->real_raw = 0;
1072         }
1073         n_tty_set_room(tty);
1074         /* The termios change make the tty ready for I/O */
1075         wake_up_interruptible(&tty->write_wait);
1076         wake_up_interruptible(&tty->read_wait);
1077 }
1078
1079 /**
1080  *      n_tty_close             -       close the ldisc for this tty
1081  *      @tty: device
1082  *
1083  *      Called from the terminal layer when this line discipline is
1084  *      being shut down, either because of a close or becsuse of a
1085  *      discipline change. The function will not be called while other
1086  *      ldisc methods are in progress.
1087  */
1088
1089 static void n_tty_close(struct tty_struct *tty)
1090 {
1091         n_tty_flush_buffer(tty);
1092         if (tty->read_buf) {
1093                 free_buf(tty->read_buf);
1094                 tty->read_buf = NULL;
1095         }
1096 }
1097
1098 /**
1099  *      n_tty_open              -       open an ldisc
1100  *      @tty: terminal to open
1101  *
1102  *      Called when this line discipline is being attached to the
1103  *      terminal device. Can sleep. Called serialized so that no
1104  *      other events will occur in parallel. No further open will occur
1105  *      until a close.
1106  */
1107
1108 static int n_tty_open(struct tty_struct *tty)
1109 {
1110         if (!tty)
1111                 return -EINVAL;
1112
1113         /* This one is ugly. Currently a malloc failure here can panic */
1114         if (!tty->read_buf) {
1115                 tty->read_buf = alloc_buf();
1116                 if (!tty->read_buf)
1117                         return -ENOMEM;
1118         }
1119         memset(tty->read_buf, 0, N_TTY_BUF_SIZE);
1120         reset_buffer_flags(tty);
1121         tty->column = 0;
1122         n_tty_set_termios(tty, NULL);
1123         tty->minimum_to_wake = 1;
1124         tty->closing = 0;
1125         return 0;
1126 }
1127
1128 static inline int input_available_p(struct tty_struct *tty, int amt)
1129 {
1130         if (tty->icanon) {
1131                 if (tty->canon_data)
1132                         return 1;
1133         } else if (tty->read_cnt >= (amt ? amt : 1))
1134                 return 1;
1135
1136         return 0;
1137 }
1138
1139 /**
1140  *      copy_from_read_buf      -       copy read data directly
1141  *      @tty: terminal device
1142  *      @b: user data
1143  *      @nr: size of data
1144  *
1145  *      Helper function to speed up read_chan.  It is only called when
1146  *      ICANON is off; it copies characters straight from the tty queue to
1147  *      user space directly.  It can be profitably called twice; once to
1148  *      drain the space from the tail pointer to the (physical) end of the
1149  *      buffer, and once to drain the space from the (physical) beginning of
1150  *      the buffer to head pointer.
1151  *
1152  *      Called under the tty->atomic_read_lock sem
1153  *
1154  */
1155
1156 static int copy_from_read_buf(struct tty_struct *tty,
1157                                       unsigned char __user **b,
1158                                       size_t *nr)
1159
1160 {
1161         int retval;
1162         size_t n;
1163         unsigned long flags;
1164
1165         retval = 0;
1166         spin_lock_irqsave(&tty->read_lock, flags);
1167         n = min(tty->read_cnt, N_TTY_BUF_SIZE - tty->read_tail);
1168         n = min(*nr, n);
1169         spin_unlock_irqrestore(&tty->read_lock, flags);
1170         if (n) {
1171                 retval = copy_to_user(*b, &tty->read_buf[tty->read_tail], n);
1172                 n -= retval;
1173                 tty_audit_add_data(tty, &tty->read_buf[tty->read_tail], n);
1174                 spin_lock_irqsave(&tty->read_lock, flags);
1175                 tty->read_tail = (tty->read_tail + n) & (N_TTY_BUF_SIZE-1);
1176                 tty->read_cnt -= n;
1177                 spin_unlock_irqrestore(&tty->read_lock, flags);
1178                 *b += n;
1179                 *nr -= n;
1180         }
1181         return retval;
1182 }
1183
1184 extern ssize_t redirected_tty_write(struct file *, const char __user *,
1185                                                         size_t, loff_t *);
1186
1187 /**
1188  *      job_control             -       check job control
1189  *      @tty: tty
1190  *      @file: file handle
1191  *
1192  *      Perform job control management checks on this file/tty descriptor
1193  *      and if appropriate send any needed signals and return a negative
1194  *      error code if action should be taken.
1195  *
1196  *      FIXME:
1197  *      Locking: None - redirected write test is safe, testing
1198  *      current->signal should possibly lock current->sighand
1199  *      pgrp locking ?
1200  */
1201
1202 static int job_control(struct tty_struct *tty, struct file *file)
1203 {
1204         /* Job control check -- must be done at start and after
1205            every sleep (POSIX.1 7.1.1.4). */
1206         /* NOTE: not yet done after every sleep pending a thorough
1207            check of the logic of this change. -- jlc */
1208         /* don't stop on /dev/console */
1209         if (file->f_op->write != redirected_tty_write &&
1210             current->signal->tty == tty) {
1211                 if (!tty->pgrp)
1212                         printk(KERN_ERR "read_chan: no tty->pgrp!\n");
1213                 else if (task_pgrp(current) != tty->pgrp) {
1214                         if (is_ignored(SIGTTIN) ||
1215                             is_current_pgrp_orphaned())
1216                                 return -EIO;
1217                         kill_pgrp(task_pgrp(current), SIGTTIN, 1);
1218                         set_thread_flag(TIF_SIGPENDING);
1219                         return -ERESTARTSYS;
1220                 }
1221         }
1222         return 0;
1223 }
1224
1225
1226 /**
1227  *      read_chan               -       read function for tty
1228  *      @tty: tty device
1229  *      @file: file object
1230  *      @buf: userspace buffer pointer
1231  *      @nr: size of I/O
1232  *
1233  *      Perform reads for the line discipline. We are guaranteed that the
1234  *      line discipline will not be closed under us but we may get multiple
1235  *      parallel readers and must handle this ourselves. We may also get
1236  *      a hangup. Always called in user context, may sleep.
1237  *
1238  *      This code must be sure never to sleep through a hangup.
1239  */
1240
1241 static ssize_t read_chan(struct tty_struct *tty, struct file *file,
1242                          unsigned char __user *buf, size_t nr)
1243 {
1244         unsigned char __user *b = buf;
1245         DECLARE_WAITQUEUE(wait, current);
1246         int c;
1247         int minimum, time;
1248         ssize_t retval = 0;
1249         ssize_t size;
1250         long timeout;
1251         unsigned long flags;
1252         int packet;
1253
1254 do_it_again:
1255
1256         if (!tty->read_buf) {
1257                 printk(KERN_ERR "n_tty_read_chan: read_buf == NULL?!?\n");
1258                 return -EIO;
1259         }
1260
1261         c = job_control(tty, file);
1262         if (c < 0)
1263                 return c;
1264
1265         minimum = time = 0;
1266         timeout = MAX_SCHEDULE_TIMEOUT;
1267         if (!tty->icanon) {
1268                 time = (HZ / 10) * TIME_CHAR(tty);
1269                 minimum = MIN_CHAR(tty);
1270                 if (minimum) {
1271                         if (time)
1272                                 tty->minimum_to_wake = 1;
1273                         else if (!waitqueue_active(&tty->read_wait) ||
1274                                  (tty->minimum_to_wake > minimum))
1275                                 tty->minimum_to_wake = minimum;
1276                 } else {
1277                         timeout = 0;
1278                         if (time) {
1279                                 timeout = time;
1280                                 time = 0;
1281                         }
1282                         tty->minimum_to_wake = minimum = 1;
1283                 }
1284         }
1285
1286         /*
1287          *      Internal serialization of reads.
1288          */
1289         if (file->f_flags & O_NONBLOCK) {
1290                 if (!mutex_trylock(&tty->atomic_read_lock))
1291                         return -EAGAIN;
1292         } else {
1293                 if (mutex_lock_interruptible(&tty->atomic_read_lock))
1294                         return -ERESTARTSYS;
1295         }
1296         packet = tty->packet;
1297
1298         add_wait_queue(&tty->read_wait, &wait);
1299         while (nr) {
1300                 /* First test for status change. */
1301                 if (packet && tty->link->ctrl_status) {
1302                         unsigned char cs;
1303                         if (b != buf)
1304                                 break;
1305                         spin_lock_irqsave(&tty->link->ctrl_lock, flags);
1306                         cs = tty->link->ctrl_status;
1307                         tty->link->ctrl_status = 0;
1308                         spin_unlock_irqrestore(&tty->link->ctrl_lock, flags);
1309                         if (tty_put_user(tty, cs, b++)) {
1310                                 retval = -EFAULT;
1311                                 b--;
1312                                 break;
1313                         }
1314                         nr--;
1315                         break;
1316                 }
1317                 /* This statement must be first before checking for input
1318                    so that any interrupt will set the state back to
1319                    TASK_RUNNING. */
1320                 set_current_state(TASK_INTERRUPTIBLE);
1321
1322                 if (((minimum - (b - buf)) < tty->minimum_to_wake) &&
1323                     ((minimum - (b - buf)) >= 1))
1324                         tty->minimum_to_wake = (minimum - (b - buf));
1325
1326                 if (!input_available_p(tty, 0)) {
1327                         if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
1328                                 retval = -EIO;
1329                                 break;
1330                         }
1331                         if (tty_hung_up_p(file))
1332                                 break;
1333                         if (!timeout)
1334                                 break;
1335                         if (file->f_flags & O_NONBLOCK) {
1336                                 retval = -EAGAIN;
1337                                 break;
1338                         }
1339                         if (signal_pending(current)) {
1340                                 retval = -ERESTARTSYS;
1341                                 break;
1342                         }
1343                         /* FIXME: does n_tty_set_room need locking ? */
1344                         n_tty_set_room(tty);
1345                         timeout = schedule_timeout(timeout);
1346                         continue;
1347                 }
1348                 __set_current_state(TASK_RUNNING);
1349
1350                 /* Deal with packet mode. */
1351                 if (packet && b == buf) {
1352                         if (tty_put_user(tty, TIOCPKT_DATA, b++)) {
1353                                 retval = -EFAULT;
1354                                 b--;
1355                                 break;
1356                         }
1357                         nr--;
1358                 }
1359
1360                 if (tty->icanon) {
1361                         /* N.B. avoid overrun if nr == 0 */
1362                         while (nr && tty->read_cnt) {
1363                                 int eol;
1364
1365                                 eol = test_and_clear_bit(tty->read_tail,
1366                                                 tty->read_flags);
1367                                 c = tty->read_buf[tty->read_tail];
1368                                 spin_lock_irqsave(&tty->read_lock, flags);
1369                                 tty->read_tail = ((tty->read_tail+1) &
1370                                                   (N_TTY_BUF_SIZE-1));
1371                                 tty->read_cnt--;
1372                                 if (eol) {
1373                                         /* this test should be redundant:
1374                                          * we shouldn't be reading data if
1375                                          * canon_data is 0
1376                                          */
1377                                         if (--tty->canon_data < 0)
1378                                                 tty->canon_data = 0;
1379                                 }
1380                                 spin_unlock_irqrestore(&tty->read_lock, flags);
1381
1382                                 if (!eol || (c != __DISABLED_CHAR)) {
1383                                         if (tty_put_user(tty, c, b++)) {
1384                                                 retval = -EFAULT;
1385                                                 b--;
1386                                                 break;
1387                                         }
1388                                         nr--;
1389                                 }
1390                                 if (eol) {
1391                                         tty_audit_push(tty);
1392                                         break;
1393                                 }
1394                         }
1395                         if (retval)
1396                                 break;
1397                 } else {
1398                         int uncopied;
1399                         /* The copy function takes the read lock and handles
1400                            locking internally for this case */
1401                         uncopied = copy_from_read_buf(tty, &b, &nr);
1402                         uncopied += copy_from_read_buf(tty, &b, &nr);
1403                         if (uncopied) {
1404                                 retval = -EFAULT;
1405                                 break;
1406                         }
1407                 }
1408
1409                 /* If there is enough space in the read buffer now, let the
1410                  * low-level driver know. We use n_tty_chars_in_buffer() to
1411                  * check the buffer, as it now knows about canonical mode.
1412                  * Otherwise, if the driver is throttled and the line is
1413                  * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
1414                  * we won't get any more characters.
1415                  */
1416                 if (n_tty_chars_in_buffer(tty) <= TTY_THRESHOLD_UNTHROTTLE) {
1417                         n_tty_set_room(tty);
1418                         check_unthrottle(tty);
1419                 }
1420
1421                 if (b - buf >= minimum)
1422                         break;
1423                 if (time)
1424                         timeout = time;
1425         }
1426         mutex_unlock(&tty->atomic_read_lock);
1427         remove_wait_queue(&tty->read_wait, &wait);
1428
1429         if (!waitqueue_active(&tty->read_wait))
1430                 tty->minimum_to_wake = minimum;
1431
1432         __set_current_state(TASK_RUNNING);
1433         size = b - buf;
1434         if (size) {
1435                 retval = size;
1436                 if (nr)
1437                         clear_bit(TTY_PUSH, &tty->flags);
1438         } else if (test_and_clear_bit(TTY_PUSH, &tty->flags))
1439                  goto do_it_again;
1440
1441         n_tty_set_room(tty);
1442         return retval;
1443 }
1444
1445 /**
1446  *      write_chan              -       write function for tty
1447  *      @tty: tty device
1448  *      @file: file object
1449  *      @buf: userspace buffer pointer
1450  *      @nr: size of I/O
1451  *
1452  *      Write function of the terminal device. This is serialized with
1453  *      respect to other write callers but not to termios changes, reads
1454  *      and other such events. We must be careful with N_TTY as the receive
1455  *      code will echo characters, thus calling driver write methods.
1456  *
1457  *      This code must be sure never to sleep through a hangup.
1458  */
1459
1460 static ssize_t write_chan(struct tty_struct *tty, struct file *file,
1461                           const unsigned char *buf, size_t nr)
1462 {
1463         const unsigned char *b = buf;
1464         DECLARE_WAITQUEUE(wait, current);
1465         int c;
1466         ssize_t retval = 0;
1467
1468         /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
1469         if (L_TOSTOP(tty) && file->f_op->write != redirected_tty_write) {
1470                 retval = tty_check_change(tty);
1471                 if (retval)
1472                         return retval;
1473         }
1474
1475         add_wait_queue(&tty->write_wait, &wait);
1476         while (1) {
1477                 set_current_state(TASK_INTERRUPTIBLE);
1478                 if (signal_pending(current)) {
1479                         retval = -ERESTARTSYS;
1480                         break;
1481                 }
1482                 if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
1483                         retval = -EIO;
1484                         break;
1485                 }
1486                 if (O_OPOST(tty) && !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
1487                         while (nr > 0) {
1488                                 ssize_t num = opost_block(tty, b, nr);
1489                                 if (num < 0) {
1490                                         if (num == -EAGAIN)
1491                                                 break;
1492                                         retval = num;
1493                                         goto break_out;
1494                                 }
1495                                 b += num;
1496                                 nr -= num;
1497                                 if (nr == 0)
1498                                         break;
1499                                 c = *b;
1500                                 if (opost(c, tty) < 0)
1501                                         break;
1502                                 b++; nr--;
1503                         }
1504                         if (tty->ops->flush_chars)
1505                                 tty->ops->flush_chars(tty);
1506                 } else {
1507                         while (nr > 0) {
1508                                 c = tty->ops->write(tty, b, nr);
1509                                 if (c < 0) {
1510                                         retval = c;
1511                                         goto break_out;
1512                                 }
1513                                 if (!c)
1514                                         break;
1515                                 b += c;
1516                                 nr -= c;
1517                         }
1518                 }
1519                 if (!nr)
1520                         break;
1521                 if (file->f_flags & O_NONBLOCK) {
1522                         retval = -EAGAIN;
1523                         break;
1524                 }
1525                 schedule();
1526         }
1527 break_out:
1528         __set_current_state(TASK_RUNNING);
1529         remove_wait_queue(&tty->write_wait, &wait);
1530         return (b - buf) ? b - buf : retval;
1531 }
1532
1533 /**
1534  *      normal_poll             -       poll method for N_TTY
1535  *      @tty: terminal device
1536  *      @file: file accessing it
1537  *      @wait: poll table
1538  *
1539  *      Called when the line discipline is asked to poll() for data or
1540  *      for special events. This code is not serialized with respect to
1541  *      other events save open/close.
1542  *
1543  *      This code must be sure never to sleep through a hangup.
1544  *      Called without the kernel lock held - fine
1545  */
1546
1547 static unsigned int normal_poll(struct tty_struct *tty, struct file *file,
1548                                                         poll_table *wait)
1549 {
1550         unsigned int mask = 0;
1551
1552         poll_wait(file, &tty->read_wait, wait);
1553         poll_wait(file, &tty->write_wait, wait);
1554         if (input_available_p(tty, TIME_CHAR(tty) ? 0 : MIN_CHAR(tty)))
1555                 mask |= POLLIN | POLLRDNORM;
1556         if (tty->packet && tty->link->ctrl_status)
1557                 mask |= POLLPRI | POLLIN | POLLRDNORM;
1558         if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
1559                 mask |= POLLHUP;
1560         if (tty_hung_up_p(file))
1561                 mask |= POLLHUP;
1562         if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) {
1563                 if (MIN_CHAR(tty) && !TIME_CHAR(tty))
1564                         tty->minimum_to_wake = MIN_CHAR(tty);
1565                 else
1566                         tty->minimum_to_wake = 1;
1567         }
1568         if (tty->ops->write && !tty_is_writelocked(tty) &&
1569                         tty_chars_in_buffer(tty) < WAKEUP_CHARS &&
1570                         tty_write_room(tty) > 0)
1571                 mask |= POLLOUT | POLLWRNORM;
1572         return mask;
1573 }
1574
1575 struct tty_ldisc tty_ldisc_N_TTY = {
1576         .magic           = TTY_LDISC_MAGIC,
1577         .name            = "n_tty",
1578         .open            = n_tty_open,
1579         .close           = n_tty_close,
1580         .flush_buffer    = n_tty_flush_buffer,
1581         .chars_in_buffer = n_tty_chars_in_buffer,
1582         .read            = read_chan,
1583         .write           = write_chan,
1584         .ioctl           = n_tty_ioctl,
1585         .set_termios     = n_tty_set_termios,
1586         .poll            = normal_poll,
1587         .receive_buf     = n_tty_receive_buf,
1588         .write_wakeup    = n_tty_write_wakeup
1589 };
1590