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