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