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