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