VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[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 whether to call the driver.unthrottle function.
104  * We test the TTY_THROTTLED bit first so that it always
105  * indicates the current state.
106  */
107 static void check_unthrottle(struct tty_struct * tty)
108 {
109         if (tty->count &&
110             test_and_clear_bit(TTY_THROTTLED, &tty->flags) && 
111             tty->driver->unthrottle)
112                 tty->driver->unthrottle(tty);
113 }
114
115 /*
116  * Reset the read buffer counters, clear the flags, 
117  * and make sure the driver is unthrottled. Called
118  * from n_tty_open() and n_tty_flush_buffer().
119  */
120 static void reset_buffer_flags(struct tty_struct *tty)
121 {
122         unsigned long flags;
123
124         spin_lock_irqsave(&tty->read_lock, flags);
125         tty->read_head = tty->read_tail = tty->read_cnt = 0;
126         spin_unlock_irqrestore(&tty->read_lock, flags);
127         tty->canon_head = tty->canon_data = tty->erasing = 0;
128         memset(&tty->read_flags, 0, sizeof tty->read_flags);
129         check_unthrottle(tty);
130 }
131
132 /*
133  * Flush the input buffer
134  */
135 void n_tty_flush_buffer(struct tty_struct * tty)
136 {
137         /* clear everything and unthrottle the driver */
138         reset_buffer_flags(tty);
139         
140         if (!tty->link)
141                 return;
142
143         if (tty->link->packet) {
144                 tty->ctrl_status |= TIOCPKT_FLUSHREAD;
145                 wake_up_interruptible(&tty->link->read_wait);
146         }
147 }
148
149 /*
150  * Return number of characters buffered to be delivered to user
151  */
152 ssize_t n_tty_chars_in_buffer(struct tty_struct *tty)
153 {
154         unsigned long flags;
155         ssize_t n = 0;
156
157         spin_lock_irqsave(&tty->read_lock, flags);
158         if (!tty->icanon) {
159                 n = tty->read_cnt;
160         } else if (tty->canon_data) {
161                 n = (tty->canon_head > tty->read_tail) ?
162                         tty->canon_head - tty->read_tail :
163                         tty->canon_head + (N_TTY_BUF_SIZE - tty->read_tail);
164         }
165         spin_unlock_irqrestore(&tty->read_lock, flags);
166         return n;
167 }
168
169 static inline int is_utf8_continuation(unsigned char c)
170 {
171         return (c & 0xc0) == 0x80;
172 }
173
174 static inline int is_continuation(unsigned char c, struct tty_struct *tty)
175 {
176         return I_IUTF8(tty) && is_utf8_continuation(c);
177 }
178
179 /*
180  * Perform OPOST processing.  Returns -1 when the output device is
181  * full and the character must be retried.
182  */
183 static int opost(unsigned char c, struct tty_struct *tty)
184 {
185         int     space, spaces;
186
187         space = tty->driver->write_room(tty);
188         if (!space)
189                 return -1;
190
191         if (O_OPOST(tty)) {
192                 switch (c) {
193                 case '\n':
194                         if (O_ONLRET(tty))
195                                 tty->column = 0;
196                         if (O_ONLCR(tty)) {
197                                 if (space < 2)
198                                         return -1;
199                                 tty->driver->put_char(tty, '\r');
200                                 tty->column = 0;
201                         }
202                         tty->canon_column = tty->column;
203                         break;
204                 case '\r':
205                         if (O_ONOCR(tty) && tty->column == 0)
206                                 return 0;
207                         if (O_OCRNL(tty)) {
208                                 c = '\n';
209                                 if (O_ONLRET(tty))
210                                         tty->canon_column = tty->column = 0;
211                                 break;
212                         }
213                         tty->canon_column = tty->column = 0;
214                         break;
215                 case '\t':
216                         spaces = 8 - (tty->column & 7);
217                         if (O_TABDLY(tty) == XTABS) {
218                                 if (space < spaces)
219                                         return -1;
220                                 tty->column += spaces;
221                                 tty->driver->write(tty, 0, "        ", spaces);
222                                 return 0;
223                         }
224                         tty->column += spaces;
225                         break;
226                 case '\b':
227                         if (tty->column > 0)
228                                 tty->column--;
229                         break;
230                 default:
231                         if (O_OLCUC(tty))
232                                 c = toupper(c);
233                         if (!iscntrl(c) && !is_continuation(c, tty))
234                                 tty->column++;
235                         break;
236                 }
237         }
238         tty->driver->put_char(tty, c);
239         return 0;
240 }
241
242 /*
243  * opost_block --- to speed up block console writes, among other
244  * things.
245  */
246 static ssize_t opost_block(struct tty_struct * tty,
247                        const unsigned char __user * inbuf, unsigned int nr)
248 {
249         char    buf[80];
250         int     space;
251         int     i;
252         char    *cp;
253
254         space = tty->driver->write_room(tty);
255         if (!space)
256                 return 0;
257         if (nr > space)
258                 nr = space;
259         if (nr > sizeof(buf))
260             nr = sizeof(buf);
261
262         if (copy_from_user(buf, inbuf, nr))
263                 return -EFAULT;
264
265         for (i = 0, cp = buf; i < nr; i++, cp++) {
266                 switch (*cp) {
267                 case '\n':
268                         if (O_ONLRET(tty))
269                                 tty->column = 0;
270                         if (O_ONLCR(tty))
271                                 goto break_out;
272                         tty->canon_column = tty->column;
273                         break;
274                 case '\r':
275                         if (O_ONOCR(tty) && tty->column == 0)
276                                 goto break_out;
277                         if (O_OCRNL(tty)) {
278                                 *cp = '\n';
279                                 if (O_ONLRET(tty))
280                                         tty->canon_column = tty->column = 0;
281                                 break;
282                         }
283                         tty->canon_column = tty->column = 0;
284                         break;
285                 case '\t':
286                         goto break_out;
287                 case '\b':
288                         if (tty->column > 0)
289                                 tty->column--;
290                         break;
291                 default:
292                         if (O_OLCUC(tty))
293                                 *cp = toupper(*cp);
294                         if (!iscntrl(*cp))
295                                 tty->column++;
296                         break;
297                 }
298         }
299 break_out:
300         if (tty->driver->flush_chars)
301                 tty->driver->flush_chars(tty);
302         i = tty->driver->write(tty, 0, buf, i); 
303         return i;
304 }
305
306
307
308 static inline void put_char(unsigned char c, struct tty_struct *tty)
309 {
310         tty->driver->put_char(tty, c);
311 }
312
313 /* Must be called only when L_ECHO(tty) is true. */
314
315 static void echo_char(unsigned char c, struct tty_struct *tty)
316 {
317         if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t') {
318                 put_char('^', tty);
319                 put_char(c ^ 0100, tty);
320                 tty->column += 2;
321         } else
322                 opost(c, tty);
323 }
324
325 static inline void finish_erasing(struct tty_struct *tty)
326 {
327         if (tty->erasing) {
328                 put_char('/', tty);
329                 tty->column++;
330                 tty->erasing = 0;
331         }
332 }
333
334 static void eraser(unsigned char c, struct tty_struct *tty)
335 {
336         enum { ERASE, WERASE, KILL } kill_type;
337         int head, seen_alnums, cnt;
338         unsigned long flags;
339
340         if (tty->read_head == tty->canon_head) {
341                 /* opost('\a', tty); */         /* what do you think? */
342                 return;
343         }
344         if (c == ERASE_CHAR(tty))
345                 kill_type = ERASE;
346         else if (c == WERASE_CHAR(tty))
347                 kill_type = WERASE;
348         else {
349                 if (!L_ECHO(tty)) {
350                         spin_lock_irqsave(&tty->read_lock, flags);
351                         tty->read_cnt -= ((tty->read_head - tty->canon_head) &
352                                           (N_TTY_BUF_SIZE - 1));
353                         tty->read_head = tty->canon_head;
354                         spin_unlock_irqrestore(&tty->read_lock, flags);
355                         return;
356                 }
357                 if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
358                         spin_lock_irqsave(&tty->read_lock, flags);
359                         tty->read_cnt -= ((tty->read_head - tty->canon_head) &
360                                           (N_TTY_BUF_SIZE - 1));
361                         tty->read_head = tty->canon_head;
362                         spin_unlock_irqrestore(&tty->read_lock, flags);
363                         finish_erasing(tty);
364                         echo_char(KILL_CHAR(tty), tty);
365                         /* Add a newline if ECHOK is on and ECHOKE is off. */
366                         if (L_ECHOK(tty))
367                                 opost('\n', tty);
368                         return;
369                 }
370                 kill_type = KILL;
371         }
372
373         seen_alnums = 0;
374         while (tty->read_head != tty->canon_head) {
375                 head = tty->read_head;
376
377                 /* erase a single possibly multibyte character */
378                 do {
379                         head = (head - 1) & (N_TTY_BUF_SIZE-1);
380                         c = tty->read_buf[head];
381                 } while (is_continuation(c, tty) && head != tty->canon_head);
382
383                 /* do not partially erase */
384                 if (is_continuation(c, tty))
385                         break;
386
387                 if (kill_type == WERASE) {
388                         /* Equivalent to BSD's ALTWERASE. */
389                         if (isalnum(c) || c == '_')
390                                 seen_alnums++;
391                         else if (seen_alnums)
392                                 break;
393                 }
394                 cnt = (tty->read_head - head) & (N_TTY_BUF_SIZE-1);
395                 spin_lock_irqsave(&tty->read_lock, flags);
396                 tty->read_head = head;
397                 tty->read_cnt -= cnt;
398                 spin_unlock_irqrestore(&tty->read_lock, flags);
399                 if (L_ECHO(tty)) {
400                         if (L_ECHOPRT(tty)) {
401                                 if (!tty->erasing) {
402                                         put_char('\\', tty);
403                                         tty->column++;
404                                         tty->erasing = 1;
405                                 }
406                                 /* if cnt > 1, output a multi-byte character */
407                                 echo_char(c, tty);
408                                 while (--cnt > 0) {
409                                         head = (head+1) & (N_TTY_BUF_SIZE-1);
410                                         put_char(tty->read_buf[head], tty);
411                                 }
412                         } else if (kill_type == ERASE && !L_ECHOE(tty)) {
413                                 echo_char(ERASE_CHAR(tty), tty);
414                         } else if (c == '\t') {
415                                 unsigned int col = tty->canon_column;
416                                 unsigned long tail = tty->canon_head;
417
418                                 /* Find the column of the last char. */
419                                 while (tail != tty->read_head) {
420                                         c = tty->read_buf[tail];
421                                         if (c == '\t')
422                                                 col = (col | 7) + 1;
423                                         else if (iscntrl(c)) {
424                                                 if (L_ECHOCTL(tty))
425                                                         col += 2;
426                                         } else if (!is_continuation(c, tty))
427                                                 col++;
428                                         tail = (tail+1) & (N_TTY_BUF_SIZE-1);
429                                 }
430
431                                 /* should never happen */
432                                 if (tty->column > 0x80000000)
433                                         tty->column = 0; 
434
435                                 /* Now backup to that column. */
436                                 while (tty->column > col) {
437                                         /* Can't use opost here. */
438                                         put_char('\b', tty);
439                                         if (tty->column > 0)
440                                                 tty->column--;
441                                 }
442                         } else {
443                                 if (iscntrl(c) && L_ECHOCTL(tty)) {
444                                         put_char('\b', tty);
445                                         put_char(' ', tty);
446                                         put_char('\b', tty);
447                                         if (tty->column > 0)
448                                                 tty->column--;
449                                 }
450                                 if (!iscntrl(c) || L_ECHOCTL(tty)) {
451                                         put_char('\b', tty);
452                                         put_char(' ', tty);
453                                         put_char('\b', tty);
454                                         if (tty->column > 0)
455                                                 tty->column--;
456                                 }
457                         }
458                 }
459                 if (kill_type == ERASE)
460                         break;
461         }
462         if (tty->read_head == tty->canon_head)
463                 finish_erasing(tty);
464 }
465
466 static inline void isig(int sig, struct tty_struct *tty, int flush)
467 {
468         if (tty->pgrp > 0)
469                 kill_pg(tty->pgrp, sig, 1);
470         if (flush || !L_NOFLSH(tty)) {
471                 n_tty_flush_buffer(tty);
472                 if (tty->driver->flush_buffer)
473                         tty->driver->flush_buffer(tty);
474         }
475 }
476
477 static inline void n_tty_receive_break(struct tty_struct *tty)
478 {
479         if (I_IGNBRK(tty))
480                 return;
481         if (I_BRKINT(tty)) {
482                 isig(SIGINT, tty, 1);
483                 return;
484         }
485         if (I_PARMRK(tty)) {
486                 put_tty_queue('\377', tty);
487                 put_tty_queue('\0', tty);
488         }
489         put_tty_queue('\0', tty);
490         wake_up_interruptible(&tty->read_wait);
491 }
492
493 static inline void n_tty_receive_overrun(struct tty_struct *tty)
494 {
495         char buf[64];
496
497         tty->num_overrun++;
498         if (time_before(tty->overrun_time, jiffies - HZ)) {
499                 printk("%s: %d input overrun(s)\n", tty_name(tty, buf),
500                        tty->num_overrun);
501                 tty->overrun_time = jiffies;
502                 tty->num_overrun = 0;
503         }
504 }
505
506 static inline void n_tty_receive_parity_error(struct tty_struct *tty,
507                                               unsigned char c)
508 {
509         if (I_IGNPAR(tty)) {
510                 return;
511         }
512         if (I_PARMRK(tty)) {
513                 put_tty_queue('\377', tty);
514                 put_tty_queue('\0', tty);
515                 put_tty_queue(c, tty);
516         } else  if (I_INPCK(tty))
517                 put_tty_queue('\0', tty);
518         else
519                 put_tty_queue(c, tty);
520         wake_up_interruptible(&tty->read_wait);
521 }
522
523 static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
524 {
525         unsigned long flags;
526
527         if (tty->raw) {
528                 put_tty_queue(c, tty);
529                 return;
530         }
531         
532         if (tty->stopped && !tty->flow_stopped &&
533             I_IXON(tty) && I_IXANY(tty)) {
534                 start_tty(tty);
535                 return;
536         }
537         
538         if (I_ISTRIP(tty))
539                 c &= 0x7f;
540         if (I_IUCLC(tty) && L_IEXTEN(tty))
541                 c=tolower(c);
542
543         if (tty->closing) {
544                 if (I_IXON(tty)) {
545                         if (c == START_CHAR(tty))
546                                 start_tty(tty);
547                         else if (c == STOP_CHAR(tty))
548                                 stop_tty(tty);
549                 }
550                 return;
551         }
552
553         /*
554          * If the previous character was LNEXT, or we know that this
555          * character is not one of the characters that we'll have to
556          * handle specially, do shortcut processing to speed things
557          * up.
558          */
559         if (!test_bit(c, tty->process_char_map) || tty->lnext) {
560                 finish_erasing(tty);
561                 tty->lnext = 0;
562                 if (L_ECHO(tty)) {
563                         if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
564                                 put_char('\a', tty); /* beep if no space */
565                                 return;
566                         }
567                         /* Record the column of first canon char. */
568                         if (tty->canon_head == tty->read_head)
569                                 tty->canon_column = tty->column;
570                         echo_char(c, tty);
571                 }
572                 if (I_PARMRK(tty) && c == (unsigned char) '\377')
573                         put_tty_queue(c, tty);
574                 put_tty_queue(c, tty);
575                 return;
576         }
577                 
578         if (c == '\r') {
579                 if (I_IGNCR(tty))
580                         return;
581                 if (I_ICRNL(tty))
582                         c = '\n';
583         } else if (c == '\n' && I_INLCR(tty))
584                 c = '\r';
585         if (I_IXON(tty)) {
586                 if (c == START_CHAR(tty)) {
587                         start_tty(tty);
588                         return;
589                 }
590                 if (c == STOP_CHAR(tty)) {
591                         stop_tty(tty);
592                         return;
593                 }
594         }
595         if (L_ISIG(tty)) {
596                 int signal;
597                 signal = SIGINT;
598                 if (c == INTR_CHAR(tty))
599                         goto send_signal;
600                 signal = SIGQUIT;
601                 if (c == QUIT_CHAR(tty))
602                         goto send_signal;
603                 signal = SIGTSTP;
604                 if (c == SUSP_CHAR(tty)) {
605 send_signal:
606                         isig(signal, tty, 0);
607                         return;
608                 }
609         }
610         if (tty->icanon) {
611                 if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
612                     (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
613                         eraser(c, tty);
614                         return;
615                 }
616                 if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
617                         tty->lnext = 1;
618                         if (L_ECHO(tty)) {
619                                 finish_erasing(tty);
620                                 if (L_ECHOCTL(tty)) {
621                                         put_char('^', tty);
622                                         put_char('\b', tty);
623                                 }
624                         }
625                         return;
626                 }
627                 if (c == REPRINT_CHAR(tty) && L_ECHO(tty) &&
628                     L_IEXTEN(tty)) {
629                         unsigned long tail = tty->canon_head;
630
631                         finish_erasing(tty);
632                         echo_char(c, tty);
633                         opost('\n', tty);
634                         while (tail != tty->read_head) {
635                                 echo_char(tty->read_buf[tail], tty);
636                                 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
637                         }
638                         return;
639                 }
640                 if (c == '\n') {
641                         if (L_ECHO(tty) || L_ECHONL(tty)) {
642                                 if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
643                                         put_char('\a', tty);
644                                         return;
645                                 }
646                                 opost('\n', tty);
647                         }
648                         goto handle_newline;
649                 }
650                 if (c == EOF_CHAR(tty)) {
651                         if (tty->canon_head != tty->read_head)
652                                 set_bit(TTY_PUSH, &tty->flags);
653                         c = __DISABLED_CHAR;
654                         goto handle_newline;
655                 }
656                 if ((c == EOL_CHAR(tty)) ||
657                     (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
658                         /*
659                          * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
660                          */
661                         if (L_ECHO(tty)) {
662                                 if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
663                                         put_char('\a', tty);
664                                         return;
665                                 }
666                                 /* Record the column of first canon char. */
667                                 if (tty->canon_head == tty->read_head)
668                                         tty->canon_column = tty->column;
669                                 echo_char(c, tty);
670                         }
671                         /*
672                          * XXX does PARMRK doubling happen for
673                          * EOL_CHAR and EOL2_CHAR?
674                          */
675                         if (I_PARMRK(tty) && c == (unsigned char) '\377')
676                                 put_tty_queue(c, tty);
677
678                 handle_newline:
679                         spin_lock_irqsave(&tty->read_lock, flags);
680                         set_bit(tty->read_head, tty->read_flags);
681                         put_tty_queue_nolock(c, tty);
682                         tty->canon_head = tty->read_head;
683                         tty->canon_data++;
684                         spin_unlock_irqrestore(&tty->read_lock, flags);
685                         kill_fasync(&tty->fasync, SIGIO, POLL_IN);
686                         if (waitqueue_active(&tty->read_wait))
687                                 wake_up_interruptible(&tty->read_wait);
688                         return;
689                 }
690         }
691         
692         finish_erasing(tty);
693         if (L_ECHO(tty)) {
694                 if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
695                         put_char('\a', tty); /* beep if no space */
696                         return;
697                 }
698                 if (c == '\n')
699                         opost('\n', tty);
700                 else {
701                         /* Record the column of first canon char. */
702                         if (tty->canon_head == tty->read_head)
703                                 tty->canon_column = tty->column;
704                         echo_char(c, tty);
705                 }
706         }
707
708         if (I_PARMRK(tty) && c == (unsigned char) '\377')
709                 put_tty_queue(c, tty);
710
711         put_tty_queue(c, tty);
712 }       
713
714 static int n_tty_receive_room(struct tty_struct *tty)
715 {
716         int     left = N_TTY_BUF_SIZE - tty->read_cnt - 1;
717
718         /*
719          * If we are doing input canonicalization, and there are no
720          * pending newlines, let characters through without limit, so
721          * that erase characters will be handled.  Other excess
722          * characters will be beeped.
723          */
724         if (tty->icanon && !tty->canon_data)
725                 return N_TTY_BUF_SIZE;
726
727         if (left > 0)
728                 return left;
729         return 0;
730 }
731
732 /*
733  * Required for the ptys, serial driver etc. since processes
734  * that attach themselves to the master and rely on ASYNC
735  * IO must be woken up
736  */
737
738 static void n_tty_write_wakeup(struct tty_struct *tty)
739 {
740         if (tty->fasync)
741         {
742                 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
743                 kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
744         }
745         return;
746 }
747
748 static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
749                               char *fp, int count)
750 {
751         const unsigned char *p;
752         char *f, flags = TTY_NORMAL;
753         int     i;
754         char    buf[64];
755         unsigned long cpuflags;
756
757         if (!tty->read_buf)
758                 return;
759
760         if (tty->real_raw) {
761                 spin_lock_irqsave(&tty->read_lock, cpuflags);
762                 i = min(N_TTY_BUF_SIZE - tty->read_cnt,
763                         N_TTY_BUF_SIZE - tty->read_head);
764                 i = min(count, i);
765                 memcpy(tty->read_buf + tty->read_head, cp, i);
766                 tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
767                 tty->read_cnt += i;
768                 cp += i;
769                 count -= i;
770
771                 i = min(N_TTY_BUF_SIZE - tty->read_cnt,
772                         N_TTY_BUF_SIZE - tty->read_head);
773                 i = min(count, i);
774                 memcpy(tty->read_buf + tty->read_head, cp, i);
775                 tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
776                 tty->read_cnt += i;
777                 spin_unlock_irqrestore(&tty->read_lock, cpuflags);
778         } else {
779                 for (i=count, p = cp, f = fp; i; i--, p++) {
780                         if (f)
781                                 flags = *f++;
782                         switch (flags) {
783                         case TTY_NORMAL:
784                                 n_tty_receive_char(tty, *p);
785                                 break;
786                         case TTY_BREAK:
787                                 n_tty_receive_break(tty);
788                                 break;
789                         case TTY_PARITY:
790                         case TTY_FRAME:
791                                 n_tty_receive_parity_error(tty, *p);
792                                 break;
793                         case TTY_OVERRUN:
794                                 n_tty_receive_overrun(tty);
795                                 break;
796                         default:
797                                 printk("%s: unknown flag %d\n",
798                                        tty_name(tty, buf), flags);
799                                 break;
800                         }
801                 }
802                 if (tty->driver->flush_chars)
803                         tty->driver->flush_chars(tty);
804         }
805
806         if (!tty->icanon && (tty->read_cnt >= tty->minimum_to_wake)) {
807                 kill_fasync(&tty->fasync, SIGIO, POLL_IN);
808                 if (waitqueue_active(&tty->read_wait))
809                         wake_up_interruptible(&tty->read_wait);
810         }
811
812         /*
813          * Check the remaining room for the input canonicalization
814          * mode.  We don't want to throttle the driver if we're in
815          * canonical mode and don't have a newline yet!
816          */
817         if (n_tty_receive_room(tty) < TTY_THRESHOLD_THROTTLE) {
818                 /* check TTY_THROTTLED first so it indicates our state */
819                 if (!test_and_set_bit(TTY_THROTTLED, &tty->flags) &&
820                     tty->driver->throttle)
821                         tty->driver->throttle(tty);
822         }
823 }
824
825 int is_ignored(int sig)
826 {
827         return (sigismember(&current->blocked, sig) ||
828                 current->sighand->action[sig-1].sa.sa_handler == SIG_IGN);
829 }
830
831 static void n_tty_set_termios(struct tty_struct *tty, struct termios * old)
832 {
833         if (!tty)
834                 return;
835         
836         tty->icanon = (L_ICANON(tty) != 0);
837         if (test_bit(TTY_HW_COOK_IN, &tty->flags)) {
838                 tty->raw = 1;
839                 tty->real_raw = 1;
840                 return;
841         }
842         if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
843             I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
844             I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
845             I_PARMRK(tty)) {
846                 local_irq_disable(); // FIXME: is this safe?
847                 memset(tty->process_char_map, 0, 256/8);
848
849                 if (I_IGNCR(tty) || I_ICRNL(tty))
850                         set_bit('\r', tty->process_char_map);
851                 if (I_INLCR(tty))
852                         set_bit('\n', tty->process_char_map);
853
854                 if (L_ICANON(tty)) {
855                         set_bit(ERASE_CHAR(tty), tty->process_char_map);
856                         set_bit(KILL_CHAR(tty), tty->process_char_map);
857                         set_bit(EOF_CHAR(tty), tty->process_char_map);
858                         set_bit('\n', tty->process_char_map);
859                         set_bit(EOL_CHAR(tty), tty->process_char_map);
860                         if (L_IEXTEN(tty)) {
861                                 set_bit(WERASE_CHAR(tty),
862                                         tty->process_char_map);
863                                 set_bit(LNEXT_CHAR(tty),
864                                         tty->process_char_map);
865                                 set_bit(EOL2_CHAR(tty),
866                                         tty->process_char_map);
867                                 if (L_ECHO(tty))
868                                         set_bit(REPRINT_CHAR(tty),
869                                                 tty->process_char_map);
870                         }
871                 }
872                 if (I_IXON(tty)) {
873                         set_bit(START_CHAR(tty), tty->process_char_map);
874                         set_bit(STOP_CHAR(tty), tty->process_char_map);
875                 }
876                 if (L_ISIG(tty)) {
877                         set_bit(INTR_CHAR(tty), tty->process_char_map);
878                         set_bit(QUIT_CHAR(tty), tty->process_char_map);
879                         set_bit(SUSP_CHAR(tty), tty->process_char_map);
880                 }
881                 clear_bit(__DISABLED_CHAR, tty->process_char_map);
882                 local_irq_enable(); // FIXME: is this safe?
883                 tty->raw = 0;
884                 tty->real_raw = 0;
885         } else {
886                 tty->raw = 1;
887                 if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
888                     (I_IGNPAR(tty) || !I_INPCK(tty)) &&
889                     (tty->driver->flags & TTY_DRIVER_REAL_RAW))
890                         tty->real_raw = 1;
891                 else
892                         tty->real_raw = 0;
893         }
894 }
895
896 static void n_tty_close(struct tty_struct *tty)
897 {
898         n_tty_flush_buffer(tty);
899         if (tty->read_buf) {
900                 free_buf(tty->read_buf);
901                 tty->read_buf = NULL;
902         }
903 }
904
905 static int n_tty_open(struct tty_struct *tty)
906 {
907         if (!tty)
908                 return -EINVAL;
909
910         if (!tty->read_buf) {
911                 tty->read_buf = alloc_buf();
912                 if (!tty->read_buf)
913                         return -ENOMEM;
914         }
915         memset(tty->read_buf, 0, N_TTY_BUF_SIZE);
916         reset_buffer_flags(tty);
917         tty->column = 0;
918         n_tty_set_termios(tty, NULL);
919         tty->minimum_to_wake = 1;
920         tty->closing = 0;
921         return 0;
922 }
923
924 static inline int input_available_p(struct tty_struct *tty, int amt)
925 {
926         if (tty->icanon) {
927                 if (tty->canon_data)
928                         return 1;
929         } else if (tty->read_cnt >= (amt ? amt : 1))
930                 return 1;
931
932         return 0;
933 }
934
935 /*
936  * Helper function to speed up read_chan.  It is only called when
937  * ICANON is off; it copies characters straight from the tty queue to
938  * user space directly.  It can be profitably called twice; once to
939  * drain the space from the tail pointer to the (physical) end of the
940  * buffer, and once to drain the space from the (physical) beginning of
941  * the buffer to head pointer.
942  */
943 static inline int copy_from_read_buf(struct tty_struct *tty,
944                                       unsigned char __user **b,
945                                       size_t *nr)
946
947 {
948         int retval;
949         ssize_t n;
950         unsigned long flags;
951
952         retval = 0;
953         spin_lock_irqsave(&tty->read_lock, flags);
954         n = min(tty->read_cnt, N_TTY_BUF_SIZE - tty->read_tail);
955         n = min((ssize_t)*nr, n);
956         spin_unlock_irqrestore(&tty->read_lock, flags);
957         if (n) {
958                 mb();
959                 retval = copy_to_user(*b, &tty->read_buf[tty->read_tail], n);
960                 n -= retval;
961                 spin_lock_irqsave(&tty->read_lock, flags);
962                 tty->read_tail = (tty->read_tail + n) & (N_TTY_BUF_SIZE-1);
963                 tty->read_cnt -= n;
964                 spin_unlock_irqrestore(&tty->read_lock, flags);
965                 *b += n;
966                 *nr -= n;
967         }
968         return retval;
969 }
970
971 extern ssize_t redirected_tty_write(struct file *,const char *,size_t,loff_t *);
972
973 static ssize_t read_chan(struct tty_struct *tty, struct file *file,
974                          unsigned char __user *buf, size_t nr)
975 {
976         unsigned char __user *b = buf;
977         DECLARE_WAITQUEUE(wait, current);
978         int c;
979         int minimum, time;
980         ssize_t retval = 0;
981         ssize_t size;
982         long timeout;
983         unsigned long flags;
984
985 do_it_again:
986
987         if (!tty->read_buf) {
988                 printk("n_tty_read_chan: called with read_buf == NULL?!?\n");
989                 return -EIO;
990         }
991
992         /* Job control check -- must be done at start and after
993            every sleep (POSIX.1 7.1.1.4). */
994         /* NOTE: not yet done after every sleep pending a thorough
995            check of the logic of this change. -- jlc */
996         /* don't stop on /dev/console */
997         if (file->f_op->write != redirected_tty_write &&
998             current->signal->tty == tty) {
999                 if (tty->pgrp <= 0)
1000                         printk("read_chan: tty->pgrp <= 0!\n");
1001                 else if (process_group(current) != tty->pgrp) {
1002                         if (is_ignored(SIGTTIN) ||
1003                             is_orphaned_pgrp(process_group(current)))
1004                                 return -EIO;
1005                         kill_pg(process_group(current), SIGTTIN, 1);
1006                         return -ERESTARTSYS;
1007                 }
1008         }
1009
1010         minimum = time = 0;
1011         timeout = MAX_SCHEDULE_TIMEOUT;
1012         if (!tty->icanon) {
1013                 time = (HZ / 10) * TIME_CHAR(tty);
1014                 minimum = MIN_CHAR(tty);
1015                 if (minimum) {
1016                         if (time)
1017                                 tty->minimum_to_wake = 1;
1018                         else if (!waitqueue_active(&tty->read_wait) ||
1019                                  (tty->minimum_to_wake > minimum))
1020                                 tty->minimum_to_wake = minimum;
1021                 } else {
1022                         timeout = 0;
1023                         if (time) {
1024                                 timeout = time;
1025                                 time = 0;
1026                         }
1027                         tty->minimum_to_wake = minimum = 1;
1028                 }
1029         }
1030
1031         if (file->f_flags & O_NONBLOCK) {
1032                 if (down_trylock(&tty->atomic_read))
1033                         return -EAGAIN;
1034         }
1035         else {
1036                 if (down_interruptible(&tty->atomic_read))
1037                         return -ERESTARTSYS;
1038         }
1039
1040         add_wait_queue(&tty->read_wait, &wait);
1041         set_bit(TTY_DONT_FLIP, &tty->flags);
1042         while (nr) {
1043                 /* First test for status change. */
1044                 if (tty->packet && tty->link->ctrl_status) {
1045                         unsigned char cs;
1046                         if (b != buf)
1047                                 break;
1048                         cs = tty->link->ctrl_status;
1049                         tty->link->ctrl_status = 0;
1050                         if (put_user(cs, b++)) {
1051                                 retval = -EFAULT;
1052                                 b--;
1053                                 break;
1054                         }
1055                         nr--;
1056                         break;
1057                 }
1058                 /* This statement must be first before checking for input
1059                    so that any interrupt will set the state back to
1060                    TASK_RUNNING. */
1061                 set_current_state(TASK_INTERRUPTIBLE);
1062                 
1063                 if (((minimum - (b - buf)) < tty->minimum_to_wake) &&
1064                     ((minimum - (b - buf)) >= 1))
1065                         tty->minimum_to_wake = (minimum - (b - buf));
1066                 
1067                 if (!input_available_p(tty, 0)) {
1068                         if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
1069                                 retval = -EIO;
1070                                 break;
1071                         }
1072                         if (tty_hung_up_p(file))
1073                                 break;
1074                         if (!timeout)
1075                                 break;
1076                         if (file->f_flags & O_NONBLOCK) {
1077                                 retval = -EAGAIN;
1078                                 break;
1079                         }
1080                         if (signal_pending(current)) {
1081                                 retval = -ERESTARTSYS;
1082                                 break;
1083                         }
1084                         clear_bit(TTY_DONT_FLIP, &tty->flags);
1085                         timeout = schedule_timeout(timeout);
1086                         set_bit(TTY_DONT_FLIP, &tty->flags);
1087                         continue;
1088                 }
1089                 __set_current_state(TASK_RUNNING);
1090
1091                 /* Deal with packet mode. */
1092                 if (tty->packet && b == buf) {
1093                         if (put_user(TIOCPKT_DATA, b++)) {
1094                                 retval = -EFAULT;
1095                                 b--;
1096                                 break;
1097                         }
1098                         nr--;
1099                 }
1100
1101                 if (tty->icanon) {
1102                         /* N.B. avoid overrun if nr == 0 */
1103                         while (nr && tty->read_cnt) {
1104                                 int eol;
1105
1106                                 eol = test_and_clear_bit(tty->read_tail,
1107                                                 tty->read_flags);
1108                                 c = tty->read_buf[tty->read_tail];
1109                                 spin_lock_irqsave(&tty->read_lock, flags);
1110                                 tty->read_tail = ((tty->read_tail+1) &
1111                                                   (N_TTY_BUF_SIZE-1));
1112                                 tty->read_cnt--;
1113                                 if (eol) {
1114                                         /* this test should be redundant:
1115                                          * we shouldn't be reading data if
1116                                          * canon_data is 0
1117                                          */
1118                                         if (--tty->canon_data < 0)
1119                                                 tty->canon_data = 0;
1120                                 }
1121                                 spin_unlock_irqrestore(&tty->read_lock, flags);
1122
1123                                 if (!eol || (c != __DISABLED_CHAR)) {
1124                                         if (put_user(c, b++)) {
1125                                                 retval = -EFAULT;
1126                                                 b--;
1127                                                 break;
1128                                         }
1129                                         nr--;
1130                                 }
1131                                 if (eol)
1132                                         break;
1133                         }
1134                         if (retval)
1135                                 break;
1136                 } else {
1137                         int uncopied;
1138                         uncopied = copy_from_read_buf(tty, &b, &nr);
1139                         uncopied += copy_from_read_buf(tty, &b, &nr);
1140                         if (uncopied) {
1141                                 retval = -EFAULT;
1142                                 break;
1143                         }
1144                 }
1145
1146                 /* If there is enough space in the read buffer now, let the
1147                  * low-level driver know. We use n_tty_chars_in_buffer() to
1148                  * check the buffer, as it now knows about canonical mode.
1149                  * Otherwise, if the driver is throttled and the line is
1150                  * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
1151                  * we won't get any more characters.
1152                  */
1153                 if (n_tty_chars_in_buffer(tty) <= TTY_THRESHOLD_UNTHROTTLE)
1154                         check_unthrottle(tty);
1155
1156                 if (b - buf >= minimum)
1157                         break;
1158                 if (time)
1159                         timeout = time;
1160         }
1161         clear_bit(TTY_DONT_FLIP, &tty->flags);
1162         up(&tty->atomic_read);
1163         remove_wait_queue(&tty->read_wait, &wait);
1164
1165         if (!waitqueue_active(&tty->read_wait))
1166                 tty->minimum_to_wake = minimum;
1167
1168         __set_current_state(TASK_RUNNING);
1169         size = b - buf;
1170         if (size) {
1171                 retval = size;
1172                 if (nr)
1173                         clear_bit(TTY_PUSH, &tty->flags);
1174         } else if (test_and_clear_bit(TTY_PUSH, &tty->flags))
1175                  goto do_it_again;
1176
1177         return retval;
1178 }
1179
1180 static ssize_t write_chan(struct tty_struct * tty, struct file * file,
1181                           const unsigned char __user * buf, size_t nr)
1182 {
1183         const unsigned char __user *b = buf;
1184         DECLARE_WAITQUEUE(wait, current);
1185         int c;
1186         ssize_t retval = 0;
1187
1188         /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
1189         if (L_TOSTOP(tty) && file->f_op->write != redirected_tty_write) {
1190                 retval = tty_check_change(tty);
1191                 if (retval)
1192                         return retval;
1193         }
1194
1195         add_wait_queue(&tty->write_wait, &wait);
1196         while (1) {
1197                 set_current_state(TASK_INTERRUPTIBLE);
1198                 if (signal_pending(current)) {
1199                         retval = -ERESTARTSYS;
1200                         break;
1201                 }
1202                 if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
1203                         retval = -EIO;
1204                         break;
1205                 }
1206                 if (O_OPOST(tty) && !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
1207                         while (nr > 0) {
1208                                 ssize_t num = opost_block(tty, b, nr);
1209                                 if (num < 0) {
1210                                         if (num == -EAGAIN)
1211                                                 break;
1212                                         retval = num;
1213                                         goto break_out;
1214                                 }
1215                                 b += num;
1216                                 nr -= num;
1217                                 if (nr == 0)
1218                                         break;
1219                                 get_user(c, b);
1220                                 if (opost(c, tty) < 0)
1221                                         break;
1222                                 b++; nr--;
1223                         }
1224                         if (tty->driver->flush_chars)
1225                                 tty->driver->flush_chars(tty);
1226                 } else {
1227                         c = tty->driver->write(tty, 1, b, nr);
1228                         if (c < 0) {
1229                                 retval = c;
1230                                 goto break_out;
1231                         }
1232                         b += c;
1233                         nr -= c;
1234                 }
1235                 if (!nr)
1236                         break;
1237                 if (file->f_flags & O_NONBLOCK) {
1238                         retval = -EAGAIN;
1239                         break;
1240                 }
1241                 schedule();
1242         }
1243 break_out:
1244         __set_current_state(TASK_RUNNING);
1245         remove_wait_queue(&tty->write_wait, &wait);
1246         return (b - buf) ? b - buf : retval;
1247 }
1248
1249 /* Called without the kernel lock held - fine */
1250 static unsigned int normal_poll(struct tty_struct * tty, struct file * file, poll_table *wait)
1251 {
1252         unsigned int mask = 0;
1253
1254         poll_wait(file, &tty->read_wait, wait);
1255         poll_wait(file, &tty->write_wait, wait);
1256         if (input_available_p(tty, TIME_CHAR(tty) ? 0 : MIN_CHAR(tty)))
1257                 mask |= POLLIN | POLLRDNORM;
1258         if (tty->packet && tty->link->ctrl_status)
1259                 mask |= POLLPRI | POLLIN | POLLRDNORM;
1260         if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
1261                 mask |= POLLHUP;
1262         if (tty_hung_up_p(file))
1263                 mask |= POLLHUP;
1264         if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) {
1265                 if (MIN_CHAR(tty) && !TIME_CHAR(tty))
1266                         tty->minimum_to_wake = MIN_CHAR(tty);
1267                 else
1268                         tty->minimum_to_wake = 1;
1269         }
1270         if (tty->driver->chars_in_buffer(tty) < WAKEUP_CHARS &&
1271                         tty->driver->write_room(tty) > 0)
1272                 mask |= POLLOUT | POLLWRNORM;
1273         return mask;
1274 }
1275
1276 struct tty_ldisc tty_ldisc_N_TTY = {
1277         TTY_LDISC_MAGIC,        /* magic */
1278         "n_tty",                /* name */
1279         0,                      /* num */
1280         0,                      /* flags */
1281         n_tty_open,             /* open */
1282         n_tty_close,            /* close */
1283         n_tty_flush_buffer,     /* flush_buffer */
1284         n_tty_chars_in_buffer,  /* chars_in_buffer */
1285         read_chan,              /* read */
1286         write_chan,             /* write */
1287         n_tty_ioctl,            /* ioctl */
1288         n_tty_set_termios,      /* set_termios */
1289         normal_poll,            /* poll */
1290         n_tty_receive_buf,      /* receive_buf */
1291         n_tty_receive_room,     /* receive_room */
1292         n_tty_write_wakeup      /* write_wakeup */
1293 };
1294