This source file includes following definitions.
- put_tty_queue
- n_tty_flush_buffer
- n_tty_chars_in_buffer
- opost
- put_char
- echo_char
- finish_erasing
- eraser
- isig
- n_tty_receive_break
- n_tty_receive_overrun
- n_tty_receive_parity_error
- n_tty_receive_char
- n_tty_receive_buf
- n_tty_receive_room
- is_ignored
- n_tty_set_termios
- n_tty_close
- n_tty_open
- input_available_p
- copy_from_read_buf
- gobble_eof
- read_chan
- write_chan
- normal_select
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 #include <linux/types.h>
23 #include <linux/major.h>
24 #include <linux/errno.h>
25 #include <linux/signal.h>
26 #include <linux/fcntl.h>
27 #include <linux/sched.h>
28 #include <linux/interrupt.h>
29 #include <linux/tty.h>
30 #include <linux/timer.h>
31 #include <linux/ctype.h>
32 #include <linux/kd.h>
33 #include <linux/mm.h>
34 #include <linux/string.h>
35 #include <linux/malloc.h>
36
37 #include <asm/segment.h>
38 #include <asm/system.h>
39 #include <asm/bitops.h>
40
41 #define CONSOLE_DEV MKDEV(TTY_MAJOR,0)
42
43 #ifndef MIN
44 #define MIN(a,b) ((a) < (b) ? (a) : (b))
45 #endif
46
47
48 #define WAKEUP_CHARS 256
49
50
51
52
53
54
55 #define TTY_THRESHOLD_THROTTLE (N_TTY_BUF_SIZE - 128)
56 #define TTY_THRESHOLD_UNTHROTTLE 128
57
58 static inline void put_tty_queue(unsigned char c, struct tty_struct *tty)
59 {
60 if (tty->read_cnt < N_TTY_BUF_SIZE) {
61 tty->read_buf[tty->read_head] = c;
62 tty->read_head = (tty->read_head + 1) & (N_TTY_BUF_SIZE-1);
63 tty->read_cnt++;
64 }
65 }
66
67
68
69
70 void n_tty_flush_buffer(struct tty_struct * tty)
71 {
72 tty->read_head = tty->read_tail = tty->read_cnt = 0;
73 tty->canon_head = tty->canon_data = tty->erasing = 0;
74 memset(&tty->read_flags, 0, sizeof tty->read_flags);
75
76 if (!tty->link)
77 return;
78
79 if (tty->driver.unthrottle)
80 (tty->driver.unthrottle)(tty);
81 if (tty->link->packet) {
82 tty->ctrl_status |= TIOCPKT_FLUSHREAD;
83 wake_up_interruptible(&tty->link->read_wait);
84 }
85 }
86
87
88
89
90 int n_tty_chars_in_buffer(struct tty_struct *tty)
91 {
92 return tty->read_cnt;
93 }
94
95
96
97
98
99 static int opost(unsigned char c, struct tty_struct *tty)
100 {
101 int space, spaces;
102
103 space = tty->driver.write_room(tty);
104 if (!space)
105 return -1;
106
107 if (O_OPOST(tty)) {
108 switch (c) {
109 case '\n':
110 if (O_ONLRET(tty))
111 tty->column = 0;
112 if (O_ONLCR(tty)) {
113 if (space < 2)
114 return -1;
115 tty->driver.put_char(tty, '\r');
116 tty->column = 0;
117 }
118 tty->canon_column = tty->column;
119 break;
120 case '\r':
121 if (O_ONOCR(tty) && tty->column == 0)
122 return 0;
123 if (O_OCRNL(tty)) {
124 c = '\n';
125 if (O_ONLRET(tty))
126 tty->canon_column = tty->column = 0;
127 break;
128 }
129 tty->canon_column = tty->column = 0;
130 break;
131 case '\t':
132 spaces = 8 - (tty->column & 7);
133 if (O_TABDLY(tty) == XTABS) {
134 if (space < spaces)
135 return -1;
136 tty->column += spaces;
137 tty->driver.write(tty, 0, " ", spaces);
138 return 0;
139 }
140 tty->column += spaces;
141 break;
142 case '\b':
143 if (tty->column > 0)
144 tty->column--;
145 break;
146 default:
147 if (O_OLCUC(tty))
148 c = toupper(c);
149 if (!iscntrl(c))
150 tty->column++;
151 break;
152 }
153 }
154 tty->driver.put_char(tty, c);
155 return 0;
156 }
157
158 static inline void put_char(unsigned char c, struct tty_struct *tty)
159 {
160 tty->driver.put_char(tty, c);
161 }
162
163
164
165 static void echo_char(unsigned char c, struct tty_struct *tty)
166 {
167 if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t') {
168 put_char('^', tty);
169 put_char(c ^ 0100, tty);
170 tty->column += 2;
171 } else
172 opost(c, tty);
173 }
174
175 static inline void finish_erasing(struct tty_struct *tty)
176 {
177 if (tty->erasing) {
178 put_char('/', tty);
179 tty->column += 2;
180 tty->erasing = 0;
181 }
182 }
183
184 static void eraser(unsigned char c, struct tty_struct *tty)
185 {
186 enum { ERASE, WERASE, KILL } kill_type;
187 int head, seen_alnums;
188
189 if (tty->read_head == tty->canon_head) {
190
191 return;
192 }
193 if (c == ERASE_CHAR(tty))
194 kill_type = ERASE;
195 else if (c == WERASE_CHAR(tty))
196 kill_type = WERASE;
197 else {
198 if (!L_ECHO(tty)) {
199 tty->read_cnt -= ((tty->read_head - tty->canon_head) &
200 (N_TTY_BUF_SIZE - 1));
201 tty->read_head = tty->canon_head;
202 return;
203 }
204 if (!L_ECHOK(tty) || !L_ECHOKE(tty)) {
205 tty->read_cnt -= ((tty->read_head - tty->canon_head) &
206 (N_TTY_BUF_SIZE - 1));
207 tty->read_head = tty->canon_head;
208 finish_erasing(tty);
209 echo_char(KILL_CHAR(tty), tty);
210
211 if (L_ECHOK(tty))
212 opost('\n', tty);
213 return;
214 }
215 kill_type = KILL;
216 }
217
218 seen_alnums = 0;
219 while (tty->read_head != tty->canon_head) {
220 head = (tty->read_head - 1) & (N_TTY_BUF_SIZE-1);
221 c = tty->read_buf[head];
222 if (kill_type == WERASE) {
223
224 if (isalnum(c) || c == '_')
225 seen_alnums++;
226 else if (seen_alnums)
227 break;
228 }
229 tty->read_head = head;
230 tty->read_cnt--;
231 if (L_ECHO(tty)) {
232 if (L_ECHOPRT(tty)) {
233 if (!tty->erasing) {
234 put_char('\\', tty);
235 tty->column++;
236 tty->erasing = 1;
237 }
238 echo_char(c, tty);
239 } else if (!L_ECHOE(tty)) {
240 echo_char(ERASE_CHAR(tty), tty);
241 } else if (c == '\t') {
242 unsigned int col = tty->canon_column;
243 unsigned long tail = tty->canon_head;
244
245
246 while (tail != tty->read_head) {
247 c = tty->read_buf[tail];
248 if (c == '\t')
249 col = (col | 7) + 1;
250 else if (iscntrl(c)) {
251 if (L_ECHOCTL(tty))
252 col += 2;
253 } else
254 col++;
255 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
256 }
257
258
259 while (tty->column > col) {
260
261 put_char('\b', tty);
262 tty->column--;
263 }
264 } else {
265 if (iscntrl(c) && L_ECHOCTL(tty)) {
266 put_char('\b', tty);
267 put_char(' ', tty);
268 put_char('\b', tty);
269 tty->column--;
270 }
271 if (!iscntrl(c) || L_ECHOCTL(tty)) {
272 put_char('\b', tty);
273 put_char(' ', tty);
274 put_char('\b', tty);
275 tty->column--;
276 }
277 }
278 }
279 if (kill_type == ERASE)
280 break;
281 }
282 if (tty->read_head == tty->canon_head)
283 finish_erasing(tty);
284 }
285
286 static void isig(int sig, struct tty_struct *tty)
287 {
288 if (tty->pgrp > 0)
289 kill_pg(tty->pgrp, sig, 1);
290 if (!L_NOFLSH(tty)) {
291 n_tty_flush_buffer(tty);
292 if (tty->driver.flush_buffer)
293 tty->driver.flush_buffer(tty);
294 }
295 }
296
297 static inline void n_tty_receive_break(struct tty_struct *tty)
298 {
299 if (I_IGNBRK(tty))
300 return;
301 if (I_BRKINT(tty)) {
302 isig(SIGINT, tty);
303 return;
304 }
305 if (I_PARMRK(tty)) {
306 put_tty_queue('\377', tty);
307 put_tty_queue('\0', tty);
308 }
309 put_tty_queue('\0', tty);
310 wake_up_interruptible(&tty->read_wait);
311 }
312
313 static inline void n_tty_receive_overrun(struct tty_struct *tty)
314 {
315 char buf[64];
316
317 tty->num_overrun++;
318 if (tty->overrun_time < (jiffies - HZ)) {
319 printk("%s: %d input overrun(s)\n", _tty_name(tty, buf),
320 tty->num_overrun);
321 tty->overrun_time = jiffies;
322 tty->num_overrun = 0;
323 }
324 }
325
326 static inline void n_tty_receive_parity_error(struct tty_struct *tty,
327 unsigned char c)
328 {
329 if (I_IGNPAR(tty)) {
330 return;
331 }
332 if (I_PARMRK(tty)) {
333 put_tty_queue('\377', tty);
334 put_tty_queue('\0', tty);
335 put_tty_queue(c, tty);
336 } else
337 put_tty_queue('\0', tty);
338 wake_up_interruptible(&tty->read_wait);
339 }
340
341 static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
342 {
343 if (tty->raw) {
344 put_tty_queue(c, tty);
345 return;
346 }
347
348 if (tty->stopped && I_IXON(tty) && I_IXANY(tty) && L_IEXTEN(tty)) {
349 start_tty(tty);
350 return;
351 }
352
353 if (I_ISTRIP(tty))
354 c &= 0x7f;
355 if (I_IUCLC(tty) && L_IEXTEN(tty))
356 c=tolower(c);
357
358
359
360
361
362
363
364 if (!test_bit(c, &tty->process_char_map) || tty->lnext) {
365 finish_erasing(tty);
366 tty->lnext = 0;
367 if (L_ECHO(tty)) {
368 if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
369 put_char('\a', tty);
370 return;
371 }
372
373 if (tty->canon_head == tty->read_head)
374 tty->canon_column = tty->column;
375 echo_char(c, tty);
376 }
377 if (I_PARMRK(tty) && c == (unsigned char) '\377')
378 put_tty_queue(c, tty);
379 put_tty_queue(c, tty);
380 return;
381 }
382
383 if (c == '\r') {
384 if (I_IGNCR(tty))
385 return;
386 if (I_ICRNL(tty))
387 c = '\n';
388 } else if (c == '\n' && I_INLCR(tty))
389 c = '\r';
390 if (I_IXON(tty)) {
391 if (c == START_CHAR(tty)) {
392 start_tty(tty);
393 return;
394 }
395 if (c == STOP_CHAR(tty)) {
396 stop_tty(tty);
397 return;
398 }
399 }
400 if (L_ISIG(tty)) {
401 if (c == INTR_CHAR(tty)) {
402 isig(SIGINT, tty);
403 return;
404 }
405 if (c == QUIT_CHAR(tty)) {
406 isig(SIGQUIT, tty);
407 return;
408 }
409 if (c == SUSP_CHAR(tty)) {
410 if (!is_orphaned_pgrp(tty->pgrp))
411 isig(SIGTSTP, tty);
412 return;
413 }
414 }
415 if (L_ICANON(tty)) {
416 if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
417 (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
418 eraser(c, tty);
419 return;
420 }
421 if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
422 tty->lnext = 1;
423 if (L_ECHO(tty)) {
424 finish_erasing(tty);
425 if (L_ECHOCTL(tty)) {
426 put_char('^', tty);
427 put_char('\b', tty);
428 }
429 }
430 return;
431 }
432 if (c == REPRINT_CHAR(tty) && L_ECHO(tty) &&
433 L_IEXTEN(tty)) {
434 unsigned long tail = tty->canon_head;
435
436 finish_erasing(tty);
437 echo_char(c, tty);
438 opost('\n', tty);
439 while (tail != tty->read_head) {
440 echo_char(tty->read_buf[tail], tty);
441 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
442 }
443 return;
444 }
445 if (c == '\n') {
446 if (L_ECHO(tty) || L_ECHONL(tty)) {
447 if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
448 put_char('\a', tty);
449 return;
450 }
451 opost('\n', tty);
452 }
453 goto handle_newline;
454 }
455 if (c == EOF_CHAR(tty)) {
456 c = __DISABLED_CHAR;
457 goto handle_newline;
458 }
459 if ((c == EOL_CHAR(tty)) ||
460 (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
461
462
463
464 if (L_ECHO(tty)) {
465 if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
466 put_char('\a', tty);
467 return;
468 }
469
470 if (tty->canon_head == tty->read_head)
471 tty->canon_column = tty->column;
472 echo_char(c, tty);
473 }
474
475
476
477
478 if (I_PARMRK(tty) && c == (unsigned char) '\377')
479 put_tty_queue(c, tty);
480
481 handle_newline:
482 set_bit(tty->read_head, &tty->read_flags);
483 put_tty_queue(c, tty);
484 tty->canon_head = tty->read_head;
485 tty->canon_data++;
486 if (tty->fasync)
487 kill_fasync(tty->fasync, SIGIO);
488 if (tty->read_wait)
489 wake_up_interruptible(&tty->read_wait);
490 return;
491 }
492 }
493
494 finish_erasing(tty);
495 if (L_ECHO(tty)) {
496 if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
497 put_char('\a', tty);
498 return;
499 }
500 if (c == '\n')
501 opost('\n', tty);
502 else {
503
504 if (tty->canon_head == tty->read_head)
505 tty->canon_column = tty->column;
506 echo_char(c, tty);
507 }
508 }
509
510 if (I_PARMRK(tty) && c == (unsigned char) '\377')
511 put_tty_queue(c, tty);
512
513 put_tty_queue(c, tty);
514 }
515
516 static void n_tty_receive_buf(struct tty_struct *tty, unsigned char *cp,
517 char *fp, int count)
518 {
519 unsigned char *p;
520 char *f, flags = 0;
521 int i;
522
523 if (!tty->read_buf)
524 return;
525
526 if (tty->real_raw) {
527 i = MIN(count, MIN(N_TTY_BUF_SIZE - tty->read_cnt,
528 N_TTY_BUF_SIZE - tty->read_head));
529 memcpy(tty->read_buf + tty->read_head, cp, i);
530 tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
531 tty->read_cnt += i;
532 cp += i;
533 count -= i;
534
535 i = MIN(count, MIN(N_TTY_BUF_SIZE - tty->read_cnt,
536 N_TTY_BUF_SIZE - tty->read_head));
537 memcpy(tty->read_buf + tty->read_head, cp, i);
538 tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
539 tty->read_cnt += i;
540 } else {
541 for (i=count, p = cp, f = fp; i; i--, p++) {
542 if (f)
543 flags = *f++;
544 switch (flags) {
545 case TTY_NORMAL:
546 n_tty_receive_char(tty, *p);
547 break;
548 case TTY_BREAK:
549 n_tty_receive_break(tty);
550 break;
551 case TTY_PARITY:
552 case TTY_FRAME:
553 n_tty_receive_parity_error(tty, *p);
554 break;
555 case TTY_OVERRUN:
556 n_tty_receive_overrun(tty);
557 break;
558 default:
559 printk("%s: unknown flag %d\n", tty_name(tty),
560 flags);
561 break;
562 }
563 }
564 if (tty->driver.flush_chars)
565 tty->driver.flush_chars(tty);
566 }
567
568 if (!tty->icanon && (tty->read_cnt >= tty->minimum_to_wake)) {
569 if (tty->fasync)
570 kill_fasync(tty->fasync, SIGIO);
571 if (tty->read_wait)
572 wake_up_interruptible(&tty->read_wait);
573 }
574
575 if ((tty->read_cnt >= TTY_THRESHOLD_THROTTLE) &&
576 tty->driver.throttle &&
577 !set_bit(TTY_THROTTLED, &tty->flags))
578 tty->driver.throttle(tty);
579 }
580
581 static int n_tty_receive_room(struct tty_struct *tty)
582 {
583 int left = N_TTY_BUF_SIZE - tty->read_cnt - 1;
584
585 if (left > 0)
586 return left;
587 return 0;
588 }
589
590 int is_ignored(int sig)
591 {
592 return ((current->blocked & (1<<(sig-1))) ||
593 (current->sigaction[sig-1].sa_handler == SIG_IGN));
594 }
595
596 static void n_tty_set_termios(struct tty_struct *tty, struct termios * old)
597 {
598 if (!tty)
599 return;
600
601 tty->icanon = (L_ICANON(tty) != 0);
602 if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
603 I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
604 I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
605 I_PARMRK(tty)) {
606 cli();
607 memset(tty->process_char_map, 0, 256/32);
608
609 if (I_IGNCR(tty) || I_ICRNL(tty))
610 set_bit('\r', &tty->process_char_map);
611 if (I_INLCR(tty))
612 set_bit('\n', &tty->process_char_map);
613
614 if (L_ICANON(tty)) {
615 set_bit(ERASE_CHAR(tty), &tty->process_char_map);
616 set_bit(KILL_CHAR(tty), &tty->process_char_map);
617 set_bit(EOF_CHAR(tty), &tty->process_char_map);
618 set_bit('\n', &tty->process_char_map);
619 set_bit(EOL_CHAR(tty), &tty->process_char_map);
620 if (L_IEXTEN(tty)) {
621 set_bit(WERASE_CHAR(tty),
622 &tty->process_char_map);
623 set_bit(LNEXT_CHAR(tty),
624 &tty->process_char_map);
625 set_bit(EOL2_CHAR(tty),
626 &tty->process_char_map);
627 if (L_ECHO(tty))
628 set_bit(REPRINT_CHAR(tty),
629 &tty->process_char_map);
630 }
631 }
632 if (I_IXON(tty)) {
633 set_bit(START_CHAR(tty), &tty->process_char_map);
634 set_bit(STOP_CHAR(tty), &tty->process_char_map);
635 }
636 if (L_ISIG(tty)) {
637 set_bit(INTR_CHAR(tty), &tty->process_char_map);
638 set_bit(QUIT_CHAR(tty), &tty->process_char_map);
639 set_bit(SUSP_CHAR(tty), &tty->process_char_map);
640 }
641 clear_bit(__DISABLED_CHAR, &tty->process_char_map);
642 sti();
643 tty->raw = 0;
644 tty->real_raw = 0;
645 } else {
646 tty->raw = 1;
647 if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
648 (I_IGNPAR(tty) || !I_INPCK(tty)) &&
649 (tty->driver.flags & TTY_DRIVER_REAL_RAW))
650 tty->real_raw = 1;
651 else
652 tty->real_raw = 0;
653 }
654 }
655
656 static void n_tty_close(struct tty_struct *tty)
657 {
658 wait_until_sent(tty, 0);
659 n_tty_flush_buffer(tty);
660 if (tty->read_buf) {
661 free_page((unsigned long) tty->read_buf);
662 tty->read_buf = 0;
663 }
664 }
665
666 static int n_tty_open(struct tty_struct *tty)
667 {
668 if (!tty)
669 return -EINVAL;
670
671 if (!tty->read_buf) {
672 tty->read_buf = (unsigned char *)
673 get_free_page(intr_count ? GFP_ATOMIC : GFP_KERNEL);
674 if (!tty->read_buf)
675 return -ENOMEM;
676 }
677 memset(tty->read_buf, 0, N_TTY_BUF_SIZE);
678 tty->read_head = tty->read_tail = tty->read_cnt = 0;
679 memset(tty->read_flags, 0, sizeof(tty->read_flags));
680 n_tty_set_termios(tty, 0);
681 tty->minimum_to_wake = 1;
682 return 0;
683 }
684
685 static inline int input_available_p(struct tty_struct *tty, int amt)
686 {
687 if (L_ICANON(tty)) {
688 if (tty->canon_data)
689 return 1;
690 } else if (tty->read_cnt >= (amt ? amt : 1))
691 return 1;
692
693 return 0;
694 }
695
696
697
698
699
700
701
702
703
704 static inline void copy_from_read_buf(struct tty_struct *tty,
705 unsigned char **b,
706 unsigned int *nr)
707
708 {
709 int n;
710
711 n = MIN(*nr, MIN(tty->read_cnt, N_TTY_BUF_SIZE - tty->read_tail));
712 if (!n)
713 return;
714 memcpy_tofs(*b, &tty->read_buf[tty->read_tail], n);
715 tty->read_tail = (tty->read_tail + n) & (N_TTY_BUF_SIZE-1);
716 tty->read_cnt -= n;
717 *b += n;
718 *nr -= n;
719 }
720
721
722
723
724
725
726
727 static inline void gobble_eof(struct tty_struct *tty)
728 {
729 cli();
730 if ((tty->read_cnt) &&
731 (tty->read_buf[tty->read_tail] == __DISABLED_CHAR) &&
732 clear_bit(tty->read_tail, &tty->read_flags)) {
733 tty->read_tail = (tty->read_tail+1) & (N_TTY_BUF_SIZE-1);
734 tty->read_cnt--;
735 }
736 sti();
737 }
738
739 static int read_chan(struct tty_struct *tty, struct file *file,
740 unsigned char *buf, unsigned int nr)
741 {
742 struct wait_queue wait = { current, NULL };
743 int c;
744 unsigned char *b = buf;
745 int minimum, time;
746 int retval = 0;
747
748 if (!tty->read_buf) {
749 printk("n_tty_read_chan: called with read_buf == NULL?!?\n");
750 return -EIO;
751 }
752
753
754
755
756
757
758 if (file->f_inode->i_rdev != CONSOLE_DEV &&
759 current->tty == tty) {
760 if (tty->pgrp <= 0)
761 printk("read_chan: tty->pgrp <= 0!\n");
762 else if (current->pgrp != tty->pgrp) {
763 if (is_ignored(SIGTTIN) ||
764 is_orphaned_pgrp(current->pgrp))
765 return -EIO;
766 kill_pg(current->pgrp, SIGTTIN, 1);
767 return -ERESTARTSYS;
768 }
769 }
770
771 if (L_ICANON(tty)) {
772 minimum = time = 0;
773 current->timeout = (unsigned long) -1;
774 } else {
775 time = (HZ / 10) * TIME_CHAR(tty);
776 minimum = MIN_CHAR(tty);
777 if (minimum) {
778 current->timeout = (unsigned long) -1;
779 if (time)
780 tty->minimum_to_wake = 1;
781 else if (!tty->read_wait ||
782 (tty->minimum_to_wake > minimum))
783 tty->minimum_to_wake = minimum;
784 } else {
785 if (time) {
786 current->timeout = time + jiffies;
787 time = 0;
788 } else
789 current->timeout = 0;
790 tty->minimum_to_wake = minimum = 1;
791 }
792 }
793
794 add_wait_queue(&tty->read_wait, &wait);
795 while (1) {
796
797 if (tty->packet && tty->link->ctrl_status) {
798 if (b != buf)
799 break;
800 put_fs_byte(tty->link->ctrl_status, b++);
801 tty->link->ctrl_status = 0;
802 break;
803 }
804
805
806
807 current->state = TASK_INTERRUPTIBLE;
808
809 if (((minimum - (b - buf)) < tty->minimum_to_wake) &&
810 ((minimum - (b - buf)) >= 1))
811 tty->minimum_to_wake = (minimum - (b - buf));
812
813 if (!input_available_p(tty, 0)) {
814 if (tty->flags & (1 << TTY_SLAVE_CLOSED)) {
815 retval = -EIO;
816 break;
817 }
818 if (tty_hung_up_p(file))
819 break;
820 if (!current->timeout)
821 break;
822 if (file->f_flags & O_NONBLOCK) {
823 retval = -EAGAIN;
824 break;
825 }
826 if (current->signal & ~current->blocked) {
827 retval = -ERESTARTSYS;
828 break;
829 }
830 schedule();
831 continue;
832 }
833 current->state = TASK_RUNNING;
834
835
836 if (tty->packet && b == buf) {
837 put_fs_byte(TIOCPKT_DATA, b++);
838 nr--;
839 }
840
841 if (L_ICANON(tty)) {
842 while (1) {
843 int eol;
844
845 disable_bh(TQUEUE_BH);
846 if (!tty->read_cnt) {
847 enable_bh(TQUEUE_BH);
848 break;
849 }
850 eol = clear_bit(tty->read_tail,
851 &tty->read_flags);
852 c = tty->read_buf[tty->read_tail];
853 tty->read_tail = ((tty->read_tail+1) &
854 (N_TTY_BUF_SIZE-1));
855 tty->read_cnt--;
856 enable_bh(TQUEUE_BH);
857 if (!eol) {
858 put_fs_byte(c, b++);
859 if (--nr)
860 continue;
861 gobble_eof(tty);
862 break;
863 }
864 if (--tty->canon_data < 0) {
865 tty->canon_data = 0;
866 }
867 if (c != __DISABLED_CHAR) {
868 put_fs_byte(c, b++);
869 nr--;
870 }
871 break;
872 }
873 } else {
874 disable_bh(TQUEUE_BH);
875 copy_from_read_buf(tty, &b, &nr);
876 copy_from_read_buf(tty, &b, &nr);
877 enable_bh(TQUEUE_BH);
878 }
879
880
881
882 if (tty->driver.unthrottle &&
883 (tty->read_cnt <= TTY_THRESHOLD_UNTHROTTLE)
884 && clear_bit(TTY_THROTTLED, &tty->flags))
885 tty->driver.unthrottle(tty);
886
887 if (b - buf >= minimum || !nr)
888 break;
889 if (time)
890 current->timeout = time + jiffies;
891 }
892 remove_wait_queue(&tty->read_wait, &wait);
893
894 if (!tty->read_wait)
895 tty->minimum_to_wake = minimum;
896
897 current->state = TASK_RUNNING;
898 current->timeout = 0;
899 return (b - buf) ? b - buf : retval;
900 }
901
902 static int write_chan(struct tty_struct * tty, struct file * file,
903 unsigned char * buf, unsigned int nr)
904 {
905 struct wait_queue wait = { current, NULL };
906 int c;
907 unsigned char *b = buf;
908 int retval = 0;
909
910
911 if (L_TOSTOP(tty) && file->f_inode->i_rdev != CONSOLE_DEV) {
912 retval = tty_check_change(tty);
913 if (retval)
914 return retval;
915 }
916
917 add_wait_queue(&tty->write_wait, &wait);
918 while (1) {
919 current->state = TASK_INTERRUPTIBLE;
920 if (current->signal & ~current->blocked) {
921 retval = -ERESTARTSYS;
922 break;
923 }
924 if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
925 retval = -EIO;
926 break;
927 }
928 if (O_OPOST(tty)) {
929 while (nr > 0) {
930 c = get_fs_byte(b);
931 if (opost(c, tty) < 0)
932 break;
933 b++; nr--;
934 if (tty->driver.flush_chars)
935 tty->driver.flush_chars(tty);
936 }
937 } else {
938 c = tty->driver.write(tty, 1, b, nr);
939 b += c;
940 nr -= c;
941 }
942 if (!nr)
943 break;
944 if (file->f_flags & O_NONBLOCK) {
945 retval = -EAGAIN;
946 break;
947 }
948 schedule();
949 }
950 current->state = TASK_RUNNING;
951 remove_wait_queue(&tty->write_wait, &wait);
952 return (b - buf) ? b - buf : retval;
953 }
954
955 static int normal_select(struct tty_struct * tty, struct inode * inode,
956 struct file * file, int sel_type, select_table *wait)
957 {
958 switch (sel_type) {
959 case SEL_IN:
960 if (input_available_p(tty, TIME_CHAR(tty) ? 0 :
961 MIN_CHAR(tty)))
962 return 1;
963
964 case SEL_EX:
965 if (tty->packet && tty->link->ctrl_status)
966 return 1;
967 if (tty->flags & (1 << TTY_SLAVE_CLOSED))
968 return 1;
969 if (tty_hung_up_p(file))
970 return 1;
971 if (!tty->read_wait) {
972 if (MIN_CHAR(tty) && !TIME_CHAR(tty))
973 tty->minimum_to_wake = MIN_CHAR(tty);
974 else
975 tty->minimum_to_wake = 1;
976 }
977 select_wait(&tty->read_wait, wait);
978 return 0;
979 case SEL_OUT:
980 if (tty->driver.chars_in_buffer(tty) < WAKEUP_CHARS)
981 return 1;
982 select_wait(&tty->write_wait, wait);
983 return 0;
984 }
985 return 0;
986 }
987
988 struct tty_ldisc tty_ldisc_N_TTY = {
989 TTY_LDISC_MAGIC,
990 0,
991 0,
992 n_tty_open,
993 n_tty_close,
994 n_tty_flush_buffer,
995 n_tty_chars_in_buffer,
996 read_chan,
997 write_chan,
998 n_tty_ioctl,
999 n_tty_set_termios,
1000 normal_select,
1001 n_tty_receive_buf,
1002 n_tty_receive_room,
1003 0
1004 };
1005