This source file includes following definitions.
- tty_register_ldisc
- put_tty_queue
- get_tty_queue
- tty_read_raw_data
- tty_write_flush
- tty_read_flush
- hung_up_tty_read
- hung_up_tty_write
- hung_up_tty_select
- hung_up_tty_ioctl
- tty_lseek
- do_tty_hangup
- tty_hangup
- tty_vhangup
- tty_hung_up_p
- disassociate_ctty
- vt_waitactive
- complete_change_console
- change_console
- wait_for_keypress
- stop_tty
- start_tty
- opost
- echo_char
- eraser
- isig
- copy_to_cooked
- is_ignored
- input_available_p
- read_chan
- write_chan
- tty_read
- tty_write
- init_dev
- release_dev
- tty_open
- tty_release
- tty_select
- normal_select
- do_SAK
- tty_write_data
- tty_bh_routine
- initialize_tty_struct
- initialize_termios
- tty_init
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40 #include <linux/types.h>
41 #include <linux/major.h>
42 #include <linux/errno.h>
43 #include <linux/signal.h>
44 #include <linux/fcntl.h>
45 #include <linux/sched.h>
46 #include <linux/tty.h>
47 #include <linux/timer.h>
48 #include <linux/ctype.h>
49 #include <linux/kd.h>
50 #include <linux/mm.h>
51 #include <linux/string.h>
52 #include <linux/malloc.h>
53
54 #include <asm/segment.h>
55 #include <asm/system.h>
56 #include <asm/bitops.h>
57
58 #include "kbd_kern.h"
59 #include "vt_kern.h"
60
61 #define CONSOLE_DEV MKDEV(TTY_MAJOR,0)
62
63 #define MAX_TTYS 256
64
65 struct tty_struct *tty_table[MAX_TTYS];
66 struct termios *tty_termios[MAX_TTYS];
67
68 struct termios *termios_locked[MAX_TTYS];
69 struct tty_ldisc ldiscs[NR_LDISCS];
70 int tty_check_write[MAX_TTYS/32];
71
72
73
74
75
76
77 int fg_console = 0;
78 struct tty_struct * redirect = NULL;
79 struct wait_queue * keypress_wait = NULL;
80
81 static void initialize_tty_struct(int line, struct tty_struct *tty);
82 static void initialize_termios(int line, struct termios *tp);
83
84 static int tty_read(struct inode *, struct file *, char *, int);
85 static int tty_write(struct inode *, struct file *, char *, int);
86 static int tty_select(struct inode *, struct file *, int, select_table *);
87 static int tty_open(struct inode *, struct file *);
88 static void tty_release(struct inode *, struct file *);
89
90 int tty_register_ldisc(int disc, struct tty_ldisc *new_ldisc)
91 {
92 if (disc < N_TTY || disc >= NR_LDISCS)
93 return -EINVAL;
94
95 if (new_ldisc) {
96 ldiscs[disc] = *new_ldisc;
97 ldiscs[disc].flags |= LDISC_FLAG_DEFINED;
98 } else
99 memset(&ldiscs[disc], 0, sizeof(struct tty_ldisc));
100
101 return 0;
102 }
103
104 void put_tty_queue(unsigned char c, struct tty_queue * queue)
105 {
106 int head;
107 unsigned long flags;
108
109 save_flags(flags);
110 cli();
111 head = (queue->head + 1) & (TTY_BUF_SIZE-1);
112 if (head != queue->tail) {
113 queue->buf[queue->head] = c;
114 queue->head = head;
115 }
116 restore_flags(flags);
117 }
118
119 int get_tty_queue(struct tty_queue * queue)
120 {
121 int result = -1;
122 unsigned long flags;
123
124 save_flags(flags);
125 cli();
126 if (queue->tail != queue->head) {
127 result = queue->buf[queue->tail];
128 INC(queue->tail);
129 }
130 restore_flags(flags);
131 return result;
132 }
133
134
135
136
137
138
139
140
141
142 int tty_read_raw_data(struct tty_struct *tty, unsigned char *bufp, int buflen)
143 {
144 int result = 0;
145 unsigned char *p = bufp;
146 unsigned long flags;
147 int head, tail;
148 int ok = 1;
149
150 save_flags(flags);
151 cli();
152 tail = tty->read_q.tail;
153 head = tty->read_q.head;
154 while ((result < buflen) && (tail!=head) && ok) {
155 ok = !clear_bit (tail, &tty->readq_flags);
156 *p++ = tty->read_q.buf[tail++];
157 tail &= TTY_BUF_SIZE-1;
158 result++;
159 }
160 tty->read_q.tail = tail;
161 restore_flags(flags);
162 return (ok) ? result : -result;
163 }
164
165
166 void tty_write_flush(struct tty_struct * tty)
167 {
168 if (!tty->write || EMPTY(&tty->write_q))
169 return;
170 if (set_bit(TTY_WRITE_BUSY,&tty->flags))
171 return;
172 tty->write(tty);
173 if (!clear_bit(TTY_WRITE_BUSY,&tty->flags))
174 printk("tty_write_flush: bit already cleared\n");
175 }
176
177 void tty_read_flush(struct tty_struct * tty)
178 {
179 if (!tty || EMPTY(&tty->read_q))
180 return;
181 if (set_bit(TTY_READ_BUSY, &tty->flags))
182 return;
183 ldiscs[tty->disc].handler(tty);
184 if (!clear_bit(TTY_READ_BUSY, &tty->flags))
185 printk("tty_read_flush: bit already cleared\n");
186 }
187
188 static int hung_up_tty_read(struct inode * inode, struct file * file, char * buf, int count)
189 {
190 return 0;
191 }
192
193 static int hung_up_tty_write(struct inode * inode, struct file * file, char * buf, int count)
194 {
195 return -EIO;
196 }
197
198 static int hung_up_tty_select(struct inode * inode, struct file * filp, int sel_type, select_table * wait)
199 {
200 return 1;
201 }
202
203 static int hung_up_tty_ioctl(struct inode * inode, struct file * file,
204 unsigned int cmd, unsigned long arg)
205 {
206 return -EIO;
207 }
208
209 static int tty_lseek(struct inode * inode, struct file * file, off_t offset, int orig)
210 {
211 return -ESPIPE;
212 }
213
214 static struct file_operations tty_fops = {
215 tty_lseek,
216 tty_read,
217 tty_write,
218 NULL,
219 tty_select,
220 tty_ioctl,
221 NULL,
222 tty_open,
223 tty_release
224 };
225
226 static struct file_operations hung_up_tty_fops = {
227 tty_lseek,
228 hung_up_tty_read,
229 hung_up_tty_write,
230 NULL,
231 hung_up_tty_select,
232 hung_up_tty_ioctl,
233 NULL,
234 NULL,
235 tty_release
236 };
237
238 void do_tty_hangup(struct tty_struct * tty, struct file_operations *fops)
239 {
240 int i;
241 struct file * filp;
242 struct task_struct *p;
243 int dev;
244
245 if (!tty)
246 return;
247 dev = MKDEV(TTY_MAJOR,tty->line);
248 for (filp = first_file, i=0; i<nr_files; i++, filp = filp->f_next) {
249 if (!filp->f_count)
250 continue;
251 if (filp->f_rdev != dev)
252 continue;
253 if (filp->f_inode && filp->f_inode->i_rdev == CONSOLE_DEV)
254 continue;
255 if (filp->f_op != &tty_fops)
256 continue;
257 filp->f_op = fops;
258 }
259 flush_input(tty);
260 flush_output(tty);
261 wake_up_interruptible(&tty->secondary.proc_list);
262 if (tty->session > 0) {
263 kill_sl(tty->session,SIGHUP,1);
264 kill_sl(tty->session,SIGCONT,1);
265 }
266 tty->session = 0;
267 tty->pgrp = -1;
268 for_each_task(p) {
269 if (p->tty == tty->line)
270 p->tty = -1;
271 }
272 if (tty->hangup)
273 (tty->hangup)(tty);
274 }
275
276 void tty_hangup(struct tty_struct * tty)
277 {
278 #ifdef TTY_DEBUG_HANGUP
279 printk("tty%d hangup...\n", tty->line);
280 #endif
281 do_tty_hangup(tty, &hung_up_tty_fops);
282 }
283
284 void tty_vhangup(struct tty_struct * tty)
285 {
286 #ifdef TTY_DEBUG_HANGUP
287 printk("tty%d vhangup...\n", tty->line);
288 #endif
289 do_tty_hangup(tty, &hung_up_tty_fops);
290 }
291
292 int tty_hung_up_p(struct file * filp)
293 {
294 return (filp->f_op == &hung_up_tty_fops);
295 }
296
297
298
299
300
301
302
303
304
305
306
307 void disassociate_ctty(int priv)
308 {
309 struct tty_struct *tty;
310 struct task_struct *p;
311
312 if (current->tty >= 0) {
313 tty = tty_table[current->tty];
314 if (tty) {
315 if (tty->pgrp > 0) {
316 kill_pg(tty->pgrp, SIGHUP, priv);
317 kill_pg(tty->pgrp, SIGCONT, priv);
318 }
319 tty->session = 0;
320 tty->pgrp = -1;
321 } else
322 printk("disassociate_ctty: ctty is NULL?!?");
323 }
324
325 for_each_task(p)
326 if (p->session == current->session)
327 p->tty = -1;
328 }
329
330
331
332
333
334
335
336
337 static struct wait_queue *vt_activate_queue = NULL;
338
339
340
341
342
343 int vt_waitactive(void)
344 {
345 interruptible_sleep_on(&vt_activate_queue);
346 return (current->signal & ~current->blocked) ? -1 : 0;
347 }
348
349 #define vt_wake_waitactive() wake_up(&vt_activate_queue)
350
351 extern int kill_proc(int pid, int sig, int priv);
352
353
354
355
356 void complete_change_console(unsigned int new_console)
357 {
358 unsigned char old_vc_mode;
359
360 if (new_console == fg_console || new_console >= NR_CONSOLES)
361 return;
362
363
364
365
366
367
368 old_vc_mode = vt_cons[fg_console].vc_mode;
369 update_screen(new_console);
370
371
372
373
374
375
376 if (vt_cons[new_console].vt_mode.mode == VT_PROCESS)
377 {
378
379
380
381
382
383 if (kill_proc(vt_cons[new_console].vt_pid,
384 vt_cons[new_console].vt_mode.acqsig,
385 1) != 0)
386 {
387
388
389
390
391
392
393
394
395
396 vt_cons[new_console].vc_mode = KD_TEXT;
397 clr_vc_kbd_mode(kbd_table + new_console, VC_RAW);
398 clr_vc_kbd_mode(kbd_table + new_console, VC_MEDIUMRAW);
399 vt_cons[new_console].vt_mode.mode = VT_AUTO;
400 vt_cons[new_console].vt_mode.waitv = 0;
401 vt_cons[new_console].vt_mode.relsig = 0;
402 vt_cons[new_console].vt_mode.acqsig = 0;
403 vt_cons[new_console].vt_mode.frsig = 0;
404 vt_cons[new_console].vt_pid = -1;
405 vt_cons[new_console].vt_newvt = -1;
406 }
407 }
408
409
410
411
412
413 if (old_vc_mode != vt_cons[new_console].vc_mode)
414 {
415 if (vt_cons[new_console].vc_mode == KD_TEXT)
416 unblank_screen();
417 else {
418 timer_active &= ~(1<<BLANK_TIMER);
419 blank_screen();
420 }
421 }
422
423
424
425
426 vt_wake_waitactive();
427 return;
428 }
429
430
431
432
433 void change_console(unsigned int new_console)
434 {
435 if (new_console == fg_console || new_console >= NR_CONSOLES)
436 return;
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453 if (vt_cons[fg_console].vt_mode.mode == VT_PROCESS)
454 {
455
456
457
458
459
460 if (kill_proc(vt_cons[fg_console].vt_pid,
461 vt_cons[fg_console].vt_mode.relsig,
462 1) == 0)
463 {
464
465
466
467
468
469 vt_cons[fg_console].vt_newvt = new_console;
470 return;
471 }
472
473
474
475
476
477
478
479
480
481
482 vt_cons[fg_console].vc_mode = KD_TEXT;
483 clr_vc_kbd_mode(kbd_table + fg_console, VC_RAW);
484 clr_vc_kbd_mode(kbd_table + fg_console, VC_MEDIUMRAW);
485 vt_cons[fg_console].vt_mode.mode = VT_AUTO;
486 vt_cons[fg_console].vt_mode.waitv = 0;
487 vt_cons[fg_console].vt_mode.relsig = 0;
488 vt_cons[fg_console].vt_mode.acqsig = 0;
489 vt_cons[fg_console].vt_mode.frsig = 0;
490 vt_cons[fg_console].vt_pid = -1;
491 vt_cons[fg_console].vt_newvt = -1;
492
493
494
495 }
496
497
498
499
500 if (vt_cons[fg_console].vc_mode == KD_GRAPHICS)
501 return;
502
503 complete_change_console(new_console);
504 }
505
506 void wait_for_keypress(void)
507 {
508 sleep_on(&keypress_wait);
509 }
510
511 void stop_tty(struct tty_struct *tty)
512 {
513 if (tty->stopped)
514 return;
515 tty->stopped = 1;
516 if (tty->link && tty->link->packet) {
517 tty->ctrl_status &= ~TIOCPKT_START;
518 tty->ctrl_status |= TIOCPKT_STOP;
519 wake_up_interruptible(&tty->link->secondary.proc_list);
520 }
521 if (tty->stop)
522 (tty->stop)(tty);
523 if (IS_A_CONSOLE(tty->line)) {
524 set_vc_kbd_led(kbd_table + fg_console, VC_SCROLLOCK);
525 set_leds();
526 }
527 }
528
529 void start_tty(struct tty_struct *tty)
530 {
531 if (!tty->stopped)
532 return;
533 tty->stopped = 0;
534 if (tty->link && tty->link->packet) {
535 tty->ctrl_status &= ~TIOCPKT_STOP;
536 tty->ctrl_status |= TIOCPKT_START;
537 wake_up_interruptible(&tty->link->secondary.proc_list);
538 }
539 if (tty->start)
540 (tty->start)(tty);
541 TTY_WRITE_FLUSH(tty);
542 if (IS_A_CONSOLE(tty->line)) {
543 clr_vc_kbd_led(kbd_table + fg_console, VC_SCROLLOCK);
544 set_leds();
545 }
546 }
547
548
549
550
551 static int opost(unsigned char c, struct tty_struct *tty)
552 {
553 if (FULL(&tty->write_q))
554 return -1;
555 if (O_OPOST(tty)) {
556 switch (c) {
557 case '\n':
558 if (O_ONLRET(tty))
559 tty->column = 0;
560 if (O_ONLCR(tty)) {
561 if (LEFT(&tty->write_q) < 2)
562 return -1;
563 put_tty_queue('\r', &tty->write_q);
564 tty->column = 0;
565 }
566 tty->canon_column = tty->column;
567 break;
568 case '\r':
569 if (O_ONOCR(tty) && tty->column == 0)
570 return 0;
571 if (O_OCRNL(tty)) {
572 c = '\n';
573 if (O_ONLRET(tty))
574 tty->canon_column = tty->column = 0;
575 break;
576 }
577 tty->canon_column = tty->column = 0;
578 break;
579 case '\t':
580 if (O_TABDLY(tty) == XTABS) {
581 if (LEFT(&tty->write_q) < 8)
582 return -1;
583 do
584 put_tty_queue(' ', &tty->write_q);
585 while (++tty->column % 8);
586 return 0;
587 }
588 tty->column = (tty->column | 7) + 1;
589 break;
590 case '\b':
591 if (tty->column > 0)
592 tty->column--;
593 break;
594 default:
595 if (O_OLCUC(tty))
596 c = toupper(c);
597 if (!iscntrl(c))
598 tty->column++;
599 break;
600 }
601 }
602 put_tty_queue(c, &tty->write_q);
603 return 0;
604 }
605
606
607
608 static void echo_char(unsigned char c, struct tty_struct *tty)
609 {
610 if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t') {
611 opost('^', tty);
612 opost(c ^ 0100, tty);
613 } else
614 opost(c, tty);
615 }
616
617 static void eraser(unsigned char c, struct tty_struct *tty)
618 {
619 enum { ERASE, WERASE, KILL } kill_type;
620 int seen_alnums;
621
622 if (tty->secondary.head == tty->canon_head) {
623
624 return;
625 }
626 if (c == ERASE_CHAR(tty))
627 kill_type = ERASE;
628 else if (c == WERASE_CHAR(tty))
629 kill_type = WERASE;
630 else {
631 if (!L_ECHO(tty)) {
632 tty->secondary.head = tty->canon_head;
633 return;
634 }
635 if (!L_ECHOK(tty) || !L_ECHOKE(tty)) {
636 tty->secondary.head = tty->canon_head;
637 if (tty->erasing) {
638 opost('/', tty);
639 tty->erasing = 0;
640 }
641 echo_char(KILL_CHAR(tty), tty);
642
643 if (L_ECHOK(tty))
644 opost('\n', tty);
645 return;
646 }
647 kill_type = KILL;
648 }
649
650 seen_alnums = 0;
651 while (tty->secondary.head != tty->canon_head) {
652 c = LAST(&tty->secondary);
653 if (kill_type == WERASE) {
654
655 if (isalnum(c) || c == '_')
656 seen_alnums++;
657 else if (seen_alnums)
658 break;
659 }
660 DEC(tty->secondary.head);
661 if (L_ECHO(tty)) {
662 if (L_ECHOPRT(tty)) {
663 if (!tty->erasing) {
664 opost('\\', tty);
665 tty->erasing = 1;
666 }
667 echo_char(c, tty);
668 } else if (!L_ECHOE(tty)) {
669 echo_char(ERASE_CHAR(tty), tty);
670 } else if (c == '\t') {
671 unsigned int col = tty->canon_column;
672 unsigned long tail = tty->canon_head;
673
674
675 while (tail != tty->secondary.head) {
676 c = tty->secondary.buf[tail];
677 if (c == '\t')
678 col = (col | 7) + 1;
679 else if (iscntrl(c)) {
680 if (L_ECHOCTL(tty))
681 col += 2;
682 } else
683 col++;
684 INC(tail);
685 }
686
687
688 while (tty->column > col) {
689
690 put_tty_queue('\b', &tty->write_q);
691 tty->column--;
692 }
693 } else {
694 if (iscntrl(c) && L_ECHOCTL(tty)) {
695 opost('\b', tty);
696 opost(' ', tty);
697 opost('\b', tty);
698 }
699 if (!iscntrl(c) || L_ECHOCTL(tty)) {
700 opost('\b', tty);
701 opost(' ', tty);
702 opost('\b', tty);
703 }
704 }
705 }
706 if (kill_type == ERASE)
707 break;
708 }
709 if (tty->erasing && tty->secondary.head == tty->canon_head) {
710 opost('/', tty);
711 tty->erasing = 0;
712 }
713 }
714
715 static void isig(int sig, struct tty_struct *tty)
716 {
717 kill_pg(tty->pgrp, sig, 1);
718 if (!L_NOFLSH(tty)) {
719 flush_input(tty);
720 flush_output(tty);
721 }
722 }
723
724 static void copy_to_cooked(struct tty_struct * tty)
725 {
726 int c, special_flag;
727 unsigned long flags;
728
729 if (!tty) {
730 printk("copy_to_cooked: called with NULL tty\n");
731 return;
732 }
733 if (!tty->write) {
734 printk("copy_to_cooked: tty %d has null write routine\n",
735 tty->line);
736 }
737 while (1) {
738
739
740
741
742
743 c = LEFT(&tty->secondary);
744 if (tty->throttle && (c < SQ_THRESHOLD_LW)
745 && !set_bit(TTY_SQ_THROTTLED, &tty->flags))
746 tty->throttle(tty, TTY_THROTTLE_SQ_FULL);
747 if (c == 0)
748 break;
749 save_flags(flags); cli();
750 if (!EMPTY(&tty->read_q)) {
751 c = tty->read_q.buf[tty->read_q.tail];
752 special_flag = clear_bit(tty->read_q.tail,
753 &tty->readq_flags);
754 INC(tty->read_q.tail);
755 restore_flags(flags);
756 } else {
757 restore_flags(flags);
758 break;
759 }
760 if (special_flag) {
761 tty->char_error = c;
762 continue;
763 }
764 if (tty->char_error) {
765 if (tty->char_error == TTY_BREAK) {
766 tty->char_error = 0;
767 if (I_IGNBRK(tty))
768 continue;
769
770 if (I_BRKINT(tty))
771 continue;
772 if (I_PARMRK(tty)) {
773 put_tty_queue('\377', &tty->secondary);
774 put_tty_queue('\0', &tty->secondary);
775 }
776 put_tty_queue('\0', &tty->secondary);
777 continue;
778 }
779 if (tty->char_error == TTY_OVERRUN) {
780 tty->char_error = 0;
781 printk("tty%d: input overrun\n", tty->line);
782 continue;
783 }
784
785 tty->char_error = 0;
786 if (I_IGNPAR(tty)) {
787 continue;
788 }
789 if (I_PARMRK(tty)) {
790 put_tty_queue('\377', &tty->secondary);
791 put_tty_queue('\0', &tty->secondary);
792 put_tty_queue(c, &tty->secondary);
793 } else
794 put_tty_queue('\0', &tty->secondary);
795 continue;
796 }
797 if (I_ISTRIP(tty))
798 c &= 0x7f;
799 if (!tty->lnext) {
800 if (c == '\r') {
801 if (I_IGNCR(tty))
802 continue;
803 if (I_ICRNL(tty))
804 c = '\n';
805 } else if (c == '\n' && I_INLCR(tty))
806 c = '\r';
807 }
808 if (I_IUCLC(tty) && L_IEXTEN(tty))
809 c=tolower(c);
810 if (c == __DISABLED_CHAR)
811 tty->lnext = 1;
812 if (L_ICANON(tty) && !tty->lnext) {
813 if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
814 (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
815 eraser(c, tty);
816 continue;
817 }
818 if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
819 tty->lnext = 1;
820 if (L_ECHO(tty)) {
821 if (tty->erasing) {
822 opost('/', tty);
823 tty->erasing = 0;
824 }
825 if (L_ECHOCTL(tty)) {
826 opost('^', tty);
827 opost('\b', tty);
828 }
829 }
830 continue;
831 }
832 if (c == REPRINT_CHAR(tty) && L_ECHO(tty) &&
833 L_IEXTEN(tty)) {
834 unsigned long tail = tty->canon_head;
835
836 if (tty->erasing) {
837 opost('/', tty);
838 tty->erasing = 0;
839 }
840 echo_char(c, tty);
841 opost('\n', tty);
842 while (tail != tty->secondary.head) {
843 echo_char(tty->secondary.buf[tail],
844 tty);
845 INC(tail);
846 }
847 continue;
848 }
849 }
850 if (I_IXON(tty) && !tty->lnext) {
851 if ((tty->stopped && I_IXANY(tty) && L_IEXTEN(tty)) ||
852 c == START_CHAR(tty)) {
853 start_tty(tty);
854 continue;
855 }
856 if (c == STOP_CHAR(tty)) {
857 stop_tty(tty);
858 continue;
859 }
860 }
861 if (L_ISIG(tty) && !tty->lnext) {
862 if (c == INTR_CHAR(tty)) {
863 isig(SIGINT, tty);
864 continue;
865 }
866 if (c == QUIT_CHAR(tty)) {
867 isig(SIGQUIT, tty);
868 continue;
869 }
870 if (c == SUSP_CHAR(tty)) {
871 if (!is_orphaned_pgrp(tty->pgrp))
872 isig(SIGTSTP, tty);
873 continue;
874 }
875 }
876
877 if (tty->erasing) {
878 opost('/', tty);
879 tty->erasing = 0;
880 }
881 if (c == '\n' && !tty->lnext) {
882 if (L_ECHO(tty) || (L_ICANON(tty) && L_ECHONL(tty)))
883 opost('\n', tty);
884 } else if (L_ECHO(tty)) {
885
886
887
888 if (c != EOF_CHAR(tty) || !L_ICANON(tty) ||
889 tty->lnext) {
890
891 if (tty->canon_head == tty->secondary.head)
892 tty->canon_column = tty->column;
893 echo_char(c, tty);
894 }
895 }
896
897 if (I_PARMRK(tty) && c == (unsigned char) '\377' &&
898 (c != EOF_CHAR(tty) || !L_ICANON(tty) || tty->lnext))
899 put_tty_queue(c, &tty->secondary);
900
901 if (L_ICANON(tty) && !tty->lnext &&
902 (c == '\n' || c == EOF_CHAR(tty) || c == EOL_CHAR(tty) ||
903 (c == EOL2_CHAR(tty) && L_IEXTEN(tty)))) {
904 if (c == EOF_CHAR(tty))
905 c = __DISABLED_CHAR;
906 set_bit(tty->secondary.head, &tty->secondary_flags);
907 put_tty_queue(c, &tty->secondary);
908 tty->canon_head = tty->secondary.head;
909 tty->canon_data++;
910 } else
911 put_tty_queue(c, &tty->secondary);
912 tty->lnext = 0;
913 }
914 if (!EMPTY(&tty->write_q))
915 TTY_WRITE_FLUSH(tty);
916 if (L_ICANON(tty) ? tty->canon_data : !EMPTY(&tty->secondary))
917 wake_up_interruptible(&tty->secondary.proc_list);
918
919 if (tty->throttle && (LEFT(&tty->read_q) >= RQ_THRESHOLD_HW)
920 && clear_bit(TTY_RQ_THROTTLED, &tty->flags))
921 tty->throttle(tty, TTY_THROTTLE_RQ_AVAIL);
922 }
923
924 int is_ignored(int sig)
925 {
926 return ((current->blocked & (1<<(sig-1))) ||
927 (current->sigaction[sig-1].sa_handler == SIG_IGN));
928 }
929
930 static inline int input_available_p(struct tty_struct *tty)
931 {
932
933 if (L_ICANON(tty)) {
934 if (tty->canon_data || FULL(&tty->read_q))
935 return 1;
936 } else if (!EMPTY(&tty->secondary))
937 return 1;
938
939
940 TTY_READ_FLUSH(tty);
941 if (tty->link)
942 TTY_WRITE_FLUSH(tty->link);
943
944 if (L_ICANON(tty)) {
945 if (tty->canon_data || FULL(&tty->read_q))
946 return 1;
947 } else if (!EMPTY(&tty->secondary))
948 return 1;
949 return 0;
950 }
951
952 static int read_chan(struct tty_struct *tty, struct file *file,
953 unsigned char *buf, unsigned int nr)
954 {
955 struct wait_queue wait = { current, NULL };
956 int c;
957 unsigned char *b = buf;
958 int minimum, time;
959 int retval = 0;
960
961
962
963
964
965
966 if (file->f_inode->i_rdev != CONSOLE_DEV &&
967 current->tty == tty->line) {
968 if (tty->pgrp <= 0)
969 printk("read_chan: tty->pgrp <= 0!\n");
970 else if (current->pgrp != tty->pgrp) {
971 if (is_ignored(SIGTTIN) ||
972 is_orphaned_pgrp(current->pgrp))
973 return -EIO;
974 kill_pg(current->pgrp, SIGTTIN, 1);
975 return -ERESTARTSYS;
976 }
977 }
978
979 if (L_ICANON(tty)) {
980 minimum = time = 0;
981 current->timeout = (unsigned long) -1;
982 } else {
983 time = (HZ / 10) * TIME_CHAR(tty);
984 minimum = MIN_CHAR(tty);
985 if (minimum)
986 current->timeout = (unsigned long) -1;
987 else {
988 if (time) {
989 current->timeout = time + jiffies;
990 time = 0;
991 } else
992 current->timeout = 0;
993 minimum = 1;
994 }
995 }
996
997 add_wait_queue(&tty->secondary.proc_list, &wait);
998 while (1) {
999
1000 if (tty->packet && tty->link->ctrl_status) {
1001 if (b != buf)
1002 break;
1003 put_fs_byte(tty->link->ctrl_status, b++);
1004 tty->link->ctrl_status = 0;
1005 break;
1006 }
1007
1008
1009
1010 current->state = TASK_INTERRUPTIBLE;
1011 if (!input_available_p(tty)) {
1012 if (tty->flags & (1 << TTY_SLAVE_CLOSED)) {
1013 retval = -EIO;
1014 break;
1015 }
1016 if (tty_hung_up_p(file))
1017 break;
1018 if (!current->timeout)
1019 break;
1020 if (file->f_flags & O_NONBLOCK) {
1021 retval = -EAGAIN;
1022 break;
1023 }
1024 if (current->signal & ~current->blocked) {
1025 retval = -ERESTARTSYS;
1026 break;
1027 }
1028 schedule();
1029 continue;
1030 }
1031 current->state = TASK_RUNNING;
1032
1033
1034 if (tty->packet && b == buf) {
1035 put_fs_byte(TIOCPKT_DATA, b++);
1036 nr--;
1037 }
1038
1039 while (nr > 0) {
1040 int eol;
1041
1042 cli();
1043 if (EMPTY(&tty->secondary)) {
1044 sti();
1045 break;
1046 }
1047 eol = clear_bit(tty->secondary.tail,
1048 &tty->secondary_flags);
1049 c = tty->secondary.buf[tty->secondary.tail];
1050 INC(tty->secondary.tail);
1051 sti();
1052 if (eol) {
1053 if (--tty->canon_data < 0) {
1054 printk("read_chan: canon_data < 0!\n");
1055 tty->canon_data = 0;
1056 }
1057 if (c == __DISABLED_CHAR)
1058 break;
1059 put_fs_byte(c, b++);
1060 nr--;
1061 break;
1062 }
1063 put_fs_byte(c, b++);
1064 nr--;
1065 }
1066
1067
1068
1069 if (tty->throttle && (LEFT(&tty->secondary) >= SQ_THRESHOLD_HW)
1070 && !clear_bit(TTY_SQ_THROTTLED, &tty->flags))
1071 tty->throttle(tty, TTY_THROTTLE_SQ_AVAIL);
1072
1073
1074 if (b - buf >= minimum || !nr)
1075 break;
1076 if (time)
1077 current->timeout = time + jiffies;
1078 }
1079 remove_wait_queue(&tty->secondary.proc_list, &wait);
1080 current->state = TASK_RUNNING;
1081 current->timeout = 0;
1082 return (b - buf) ? b - buf : retval;
1083 }
1084
1085 static int write_chan(struct tty_struct * tty, struct file * file,
1086 unsigned char * buf, unsigned int nr)
1087 {
1088 struct wait_queue wait = { current, NULL };
1089 int c;
1090 unsigned char *b = buf;
1091 int retval = 0;
1092
1093
1094 if (L_TOSTOP(tty) && file->f_inode->i_rdev != CONSOLE_DEV) {
1095 retval = check_change(tty, tty->line);
1096 if (retval)
1097 return retval;
1098 }
1099
1100 add_wait_queue(&tty->write_q.proc_list, &wait);
1101 while (1) {
1102 current->state = TASK_INTERRUPTIBLE;
1103 if (current->signal & ~current->blocked) {
1104 retval = -ERESTARTSYS;
1105 break;
1106 }
1107 if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
1108 retval = -EIO;
1109 break;
1110 }
1111 while (nr > 0) {
1112 c = get_fs_byte(b);
1113
1114
1115 if (opost(c, tty) < 0)
1116 break;
1117 b++; nr--;
1118 }
1119 TTY_WRITE_FLUSH(tty);
1120 if (!nr)
1121 break;
1122 if (EMPTY(&tty->write_q) && !need_resched)
1123 continue;
1124 schedule();
1125 }
1126 current->state = TASK_RUNNING;
1127 remove_wait_queue(&tty->write_q.proc_list, &wait);
1128 return (b - buf) ? b - buf : retval;
1129 }
1130
1131 static int tty_read(struct inode * inode, struct file * file, char * buf, int count)
1132 {
1133 int i, dev;
1134 struct tty_struct * tty;
1135
1136 dev = file->f_rdev;
1137 if (MAJOR(dev) != TTY_MAJOR) {
1138 printk("tty_read: bad pseudo-major nr #%d\n", MAJOR(dev));
1139 return -EINVAL;
1140 }
1141 dev = MINOR(dev);
1142 tty = TTY_TABLE(dev);
1143 if (!tty || (tty->flags & (1 << TTY_IO_ERROR)))
1144 return -EIO;
1145
1146
1147
1148
1149
1150 #if 0
1151 if ((inode->i_rdev != CONSOLE_DEV) &&
1152 (tty->pgrp > 0) &&
1153 (current->tty == dev) &&
1154 (tty->pgrp != current->pgrp))
1155 if (is_ignored(SIGTTIN) || is_orphaned_pgrp(current->pgrp))
1156 return -EIO;
1157 else {
1158 (void) kill_pg(current->pgrp, SIGTTIN, 1);
1159 return -ERESTARTSYS;
1160 }
1161 #endif
1162 if (ldiscs[tty->disc].read)
1163
1164 i = (ldiscs[tty->disc].read)(tty,file,(unsigned char *)buf,(unsigned int)count);
1165 else
1166 i = -EIO;
1167 if (i > 0)
1168 inode->i_atime = CURRENT_TIME;
1169 return i;
1170 }
1171
1172 static int tty_write(struct inode * inode, struct file * file, char * buf, int count)
1173 {
1174 int dev, i, is_console;
1175 struct tty_struct * tty;
1176
1177 dev = file->f_rdev;
1178 is_console = (inode->i_rdev == CONSOLE_DEV);
1179 if (MAJOR(dev) != TTY_MAJOR) {
1180 printk("tty_write: pseudo-major != TTY_MAJOR\n");
1181 return -EINVAL;
1182 }
1183 dev = MINOR(dev);
1184 if (is_console && redirect)
1185 tty = redirect;
1186 else
1187 tty = TTY_TABLE(dev);
1188 if (!tty || !tty->write || (tty->flags & (1 << TTY_IO_ERROR)))
1189 return -EIO;
1190 #if 0
1191 if (!is_console && L_TOSTOP(tty) && (tty->pgrp > 0) &&
1192 (current->tty == dev) && (tty->pgrp != current->pgrp)) {
1193 if (is_orphaned_pgrp(current->pgrp))
1194 return -EIO;
1195 if (!is_ignored(SIGTTOU)) {
1196 (void) kill_pg(current->pgrp, SIGTTOU, 1);
1197 return -ERESTARTSYS;
1198 }
1199 }
1200 #endif
1201 if (ldiscs[tty->disc].write)
1202
1203 i = (ldiscs[tty->disc].write)(tty,file,(unsigned char *)buf,(unsigned int)count);
1204 else
1205 i = -EIO;
1206 if (i > 0)
1207 inode->i_mtime = CURRENT_TIME;
1208 return i;
1209 }
1210
1211
1212
1213
1214
1215
1216 static int init_dev(int dev)
1217 {
1218 struct tty_struct *tty, *o_tty;
1219 struct termios *tp, *o_tp, *ltp, *o_ltp;
1220 int retval;
1221 int o_dev;
1222
1223 o_dev = PTY_OTHER(dev);
1224 tty = o_tty = NULL;
1225 tp = o_tp = NULL;
1226 ltp = o_ltp = NULL;
1227 repeat:
1228 retval = -EAGAIN;
1229 if (IS_A_PTY_MASTER(dev) && tty_table[dev] && tty_table[dev]->count)
1230 goto end_init;
1231 retval = -ENOMEM;
1232 if (!tty_table[dev] && !tty) {
1233 if (!(tty = (struct tty_struct*) get_free_page(GFP_KERNEL)))
1234 goto end_init;
1235 initialize_tty_struct(dev, tty);
1236 goto repeat;
1237 }
1238 if (!tty_termios[dev] && !tp) {
1239 tp = (struct termios *) kmalloc(sizeof(struct termios),
1240 GFP_KERNEL);
1241 if (!tp)
1242 goto end_init;
1243 initialize_termios(dev, tp);
1244 goto repeat;
1245 }
1246 if (!termios_locked[dev] && !ltp) {
1247 ltp = (struct termios *) kmalloc(sizeof(struct termios),
1248 GFP_KERNEL);
1249 if (!ltp)
1250 goto end_init;
1251 memset(ltp, 0, sizeof(struct termios));
1252 goto repeat;
1253 }
1254 if (IS_A_PTY(dev)) {
1255 if (!tty_table[o_dev] && !o_tty) {
1256 o_tty = (struct tty_struct *)
1257 get_free_page(GFP_KERNEL);
1258 if (!o_tty)
1259 goto end_init;
1260 initialize_tty_struct(o_dev, o_tty);
1261 goto repeat;
1262 }
1263 if (!tty_termios[o_dev] && !o_tp) {
1264 o_tp = (struct termios *)
1265 kmalloc(sizeof(struct termios), GFP_KERNEL);
1266 if (!o_tp)
1267 goto end_init;
1268 initialize_termios(o_dev, o_tp);
1269 goto repeat;
1270 }
1271 if (!termios_locked[o_dev] && !o_ltp) {
1272 o_ltp = (struct termios *)
1273 kmalloc(sizeof(struct termios), GFP_KERNEL);
1274 if (!o_ltp)
1275 goto end_init;
1276 memset(o_ltp, 0, sizeof(struct termios));
1277 goto repeat;
1278 }
1279
1280 }
1281
1282 if (!tty_termios[dev]) {
1283 tty_termios[dev] = tp;
1284 tp = NULL;
1285 }
1286 if (!tty_table[dev]) {
1287 tty->termios = tty_termios[dev];
1288 tty_table[dev] = tty;
1289 tty = NULL;
1290 }
1291 if (!termios_locked[dev]) {
1292 termios_locked[dev] = ltp;
1293 ltp = NULL;
1294 }
1295 if (IS_A_PTY(dev)) {
1296 if (!tty_termios[o_dev]) {
1297 tty_termios[o_dev] = o_tp;
1298 o_tp = NULL;
1299 }
1300 if (!termios_locked[o_dev]) {
1301 termios_locked[o_dev] = o_ltp;
1302 o_ltp = NULL;
1303 }
1304 if (!tty_table[o_dev]) {
1305 o_tty->termios = tty_termios[o_dev];
1306 tty_table[o_dev] = o_tty;
1307 o_tty = NULL;
1308 }
1309 tty_table[dev]->link = tty_table[o_dev];
1310 tty_table[o_dev]->link = tty_table[dev];
1311 }
1312 tty_table[dev]->count++;
1313 if (IS_A_PTY_MASTER(dev))
1314 tty_table[o_dev]->count++;
1315 retval = 0;
1316 end_init:
1317 if (tty)
1318 free_page((unsigned long) tty);
1319 if (o_tty)
1320 free_page((unsigned long) o_tty);
1321 if (tp)
1322 kfree_s(tp, sizeof(struct termios));
1323 if (o_tp)
1324 kfree_s(o_tp, sizeof(struct termios));
1325 if (ltp)
1326 kfree_s(ltp, sizeof(struct termios));
1327 if (o_ltp)
1328 kfree_s(o_ltp, sizeof(struct termios));
1329 return retval;
1330 }
1331
1332
1333
1334
1335
1336
1337 static void release_dev(int dev, struct file * filp)
1338 {
1339 struct tty_struct *tty, *o_tty;
1340 struct termios *tp, *o_tp;
1341 struct task_struct **p;
1342
1343 tty = tty_table[dev];
1344 tp = tty_termios[dev];
1345 o_tty = NULL;
1346 o_tp = NULL;
1347 if (!tty) {
1348 printk("release_dev: tty_table[%d] was NULL\n", dev);
1349 return;
1350 }
1351 if (!tp) {
1352 printk("release_dev: tty_termios[%d] was NULL\n", dev);
1353 return;
1354 }
1355 #ifdef TTY_DEBUG_HANGUP
1356 printk("release_dev of tty%d (tty count=%d)...", dev, tty->count);
1357 #endif
1358 if (IS_A_PTY(dev)) {
1359 o_tty = tty_table[PTY_OTHER(dev)];
1360 o_tp = tty_termios[PTY_OTHER(dev)];
1361 if (!o_tty) {
1362 printk("release_dev: pty pair(%d) was NULL\n", dev);
1363 return;
1364 }
1365 if (!o_tp) {
1366 printk("release_dev: pty pair(%d) termios was NULL\n", dev);
1367 return;
1368 }
1369 if (tty->link != o_tty || o_tty->link != tty) {
1370 printk("release_dev: bad pty pointers\n");
1371 return;
1372 }
1373 }
1374 tty->write_data_cnt = 0;
1375 if (tty->close)
1376 tty->close(tty, filp);
1377 if (IS_A_PTY_MASTER(dev)) {
1378 if (--tty->link->count < 0) {
1379 printk("release_dev: bad tty slave count (dev = %d): %d\n",
1380 dev, tty->count);
1381 tty->link->count = 0;
1382 }
1383 }
1384 if (--tty->count < 0) {
1385 printk("release_dev: bad tty_table[%d]->count: %d\n",
1386 dev, tty->count);
1387 tty->count = 0;
1388 }
1389 if (tty->count)
1390 return;
1391
1392 #ifdef TTY_DEBUG_HANGUP
1393 printk("freeing tty structure...");
1394 #endif
1395
1396
1397
1398
1399
1400 for (p = &LAST_TASK ; p > &FIRST_TASK ; --p) {
1401 if ((*p) && (*p)->tty == tty->line)
1402 (*p)->tty = -1;
1403 }
1404
1405
1406
1407
1408
1409 if (ldiscs[tty->disc].close != NULL)
1410 ldiscs[tty->disc].close(tty);
1411 tty->disc = N_TTY;
1412 tty->termios->c_line = N_TTY;
1413
1414 if (o_tty) {
1415 if (o_tty->count)
1416 return;
1417 else {
1418 tty_table[PTY_OTHER(dev)] = NULL;
1419 tty_termios[PTY_OTHER(dev)] = NULL;
1420 }
1421 }
1422 tty_table[dev] = NULL;
1423 if (IS_A_PTY(dev)) {
1424 tty_termios[dev] = NULL;
1425 kfree_s(tp, sizeof(struct termios));
1426 }
1427 if (tty == redirect || o_tty == redirect)
1428 redirect = NULL;
1429 free_page((unsigned long) tty);
1430 if (o_tty)
1431 free_page((unsigned long) o_tty);
1432 if (o_tp)
1433 kfree_s(o_tp, sizeof(struct termios));
1434 }
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448 static int tty_open(struct inode * inode, struct file * filp)
1449 {
1450 struct tty_struct *tty;
1451 int major, minor;
1452 int noctty, retval;
1453
1454 retry_open:
1455 minor = MINOR(inode->i_rdev);
1456 major = MAJOR(inode->i_rdev);
1457 noctty = filp->f_flags & O_NOCTTY;
1458 if (major == TTYAUX_MAJOR) {
1459 if (!minor) {
1460 major = TTY_MAJOR;
1461 minor = current->tty;
1462 }
1463
1464 } else if (major == TTY_MAJOR) {
1465 if (!minor) {
1466 minor = fg_console + 1;
1467 noctty = 1;
1468 }
1469 } else {
1470 printk("Bad major #%d in tty_open\n", MAJOR(inode->i_rdev));
1471 return -ENODEV;
1472 }
1473 if (minor <= 0)
1474 return -ENXIO;
1475 if (IS_A_PTY_MASTER(minor))
1476 noctty = 1;
1477 filp->f_rdev = (major << 8) | minor;
1478 retval = init_dev(minor);
1479 if (retval)
1480 return retval;
1481 tty = tty_table[minor];
1482 #ifdef TTY_DEBUG_HANGUP
1483 printk("opening tty%d...", tty->line);
1484 #endif
1485 if (test_bit(TTY_EXCLUSIVE, &tty->flags) && !suser())
1486 return -EBUSY;
1487
1488 #if 0
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501 tty->ctrl_status = 0;
1502 tty->packet = 0;
1503 #endif
1504
1505 if (tty->open) {
1506 retval = tty->open(tty, filp);
1507 } else {
1508 retval = -ENODEV;
1509 }
1510 if (retval) {
1511 #ifdef TTY_DEBUG_HANGUP
1512 printk("error %d in opening tty%d...", retval, tty->line);
1513 #endif
1514
1515 release_dev(minor, filp);
1516 if (retval != -ERESTARTSYS)
1517 return retval;
1518 if (current->signal & ~current->blocked)
1519 return retval;
1520 schedule();
1521 goto retry_open;
1522 }
1523 if (!noctty &&
1524 current->leader &&
1525 current->tty<0 &&
1526 tty->session==0) {
1527 current->tty = minor;
1528 tty->session = current->session;
1529 tty->pgrp = current->pgrp;
1530 }
1531 filp->f_rdev = MKDEV(TTY_MAJOR,minor);
1532 return 0;
1533 }
1534
1535
1536
1537
1538
1539
1540 static void tty_release(struct inode * inode, struct file * filp)
1541 {
1542 int dev;
1543
1544 dev = filp->f_rdev;
1545 if (MAJOR(dev) != TTY_MAJOR) {
1546 printk("tty_release: tty pseudo-major != TTY_MAJOR\n");
1547 return;
1548 }
1549 dev = MINOR(filp->f_rdev);
1550 if (!dev) {
1551 printk("tty_release: bad f_rdev\n");
1552 return;
1553 }
1554 release_dev(dev, filp);
1555 }
1556
1557 static int tty_select(struct inode * inode, struct file * filp, int sel_type, select_table * wait)
1558 {
1559 int dev;
1560 struct tty_struct * tty;
1561
1562 dev = filp->f_rdev;
1563 if (MAJOR(dev) != TTY_MAJOR) {
1564 printk("tty_select: tty pseudo-major != TTY_MAJOR\n");
1565 return 0;
1566 }
1567 dev = MINOR(filp->f_rdev);
1568 tty = TTY_TABLE(dev);
1569 if (!tty) {
1570 printk("tty_select: tty struct for dev %d was NULL\n", dev);
1571 return 0;
1572 }
1573 if (ldiscs[tty->disc].select)
1574 return (ldiscs[tty->disc].select)(tty, inode, filp,
1575 sel_type, wait);
1576 return 0;
1577 }
1578
1579 static int normal_select(struct tty_struct * tty, struct inode * inode,
1580 struct file * file, int sel_type, select_table *wait)
1581 {
1582 switch (sel_type) {
1583 case SEL_IN:
1584 if (input_available_p(tty))
1585 return 1;
1586
1587 case SEL_EX:
1588 if (tty->packet && tty->link->ctrl_status)
1589 return 1;
1590 if (tty->flags & (1 << TTY_SLAVE_CLOSED))
1591 return 1;
1592 if (tty_hung_up_p(file))
1593 return 1;
1594 select_wait(&tty->secondary.proc_list, wait);
1595 return 0;
1596 case SEL_OUT:
1597 if (!FULL(&tty->write_q))
1598 return 1;
1599 select_wait(&tty->write_q.proc_list, wait);
1600 return 0;
1601 }
1602 return 0;
1603 }
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617 void do_SAK( struct tty_struct *tty)
1618 {
1619 #ifdef TTY_SOFT_SAK
1620 tty_hangup(tty);
1621 #else
1622 struct task_struct **p;
1623 int line = tty->line;
1624 int session = tty->session;
1625 int i;
1626 struct file *filp;
1627
1628 flush_input(tty);
1629 flush_output(tty);
1630 for (p = &LAST_TASK ; p > &FIRST_TASK ; --p) {
1631 if (!(*p))
1632 continue;
1633 if (((*p)->tty == line) ||
1634 ((session > 0) && ((*p)->session == session)))
1635 send_sig(SIGKILL, *p, 1);
1636 else {
1637 for (i=0; i < NR_OPEN; i++) {
1638 filp = (*p)->filp[i];
1639 if (filp && (filp->f_op == &tty_fops) &&
1640 (MINOR(filp->f_rdev) == line)) {
1641 send_sig(SIGKILL, *p, 1);
1642 break;
1643 }
1644 }
1645 }
1646 }
1647 #endif
1648 }
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665 int tty_write_data(struct tty_struct *tty, char *bufp, int buflen,
1666 void (*callback)(void * data), void * callarg)
1667 {
1668 int head, tail, count;
1669 unsigned long flags;
1670 char *p;
1671
1672 #define VLEFT ((tail-head-1)&(TTY_BUF_SIZE-1))
1673
1674 save_flags(flags);
1675 cli();
1676 if (tty->write_data_cnt) {
1677 restore_flags(flags);
1678 return -EBUSY;
1679 }
1680
1681 head = tty->write_q.head;
1682 tail = tty->write_q.tail;
1683 count = buflen;
1684 p = bufp;
1685
1686 while (count && VLEFT > 0) {
1687 tty->write_q.buf[head++] = *p++;
1688 head &= TTY_BUF_SIZE-1;
1689 count--;
1690 }
1691 tty->write_q.head = head;
1692 if (count) {
1693 tty->write_data_cnt = count;
1694 tty->write_data_ptr = (unsigned char *) p;
1695 tty->write_data_callback = callback;
1696 tty->write_data_arg = callarg;
1697 }
1698 restore_flags(flags);
1699 tty->write(tty);
1700 return count;
1701 }
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711 void tty_bh_routine(void * unused)
1712 {
1713 int i, j, line, mask;
1714 int head, tail, count;
1715 unsigned char * p;
1716 struct tty_struct * tty;
1717
1718 for (i = 0, line = 0; i < MAX_TTYS / 32; i++) {
1719 if (!tty_check_write[i]) {
1720 line += 32;
1721 continue;
1722 }
1723 for (j=0, mask=0; j < 32; j++, line++, mask <<= 1) {
1724 if (clear_bit(j, &tty_check_write[i])) {
1725 tty = tty_table[line];
1726 if (!tty || !tty->write_data_cnt)
1727 continue;
1728 cli();
1729 head = tty->write_q.head;
1730 tail = tty->write_q.tail;
1731 count = tty->write_data_cnt;
1732 p = tty->write_data_ptr;
1733
1734 while (count && VLEFT > 0) {
1735 tty->write_q.buf[head++] = *p++;
1736 head &= TTY_BUF_SIZE-1;
1737 count--;
1738 }
1739 tty->write_q.head = head;
1740 tty->write_data_ptr = p;
1741 tty->write_data_cnt = count;
1742 sti();
1743 if (!count)
1744 (tty->write_data_callback)
1745 (tty->write_data_arg);
1746 }
1747 }
1748 }
1749
1750 }
1751
1752
1753
1754
1755
1756 static void initialize_tty_struct(int line, struct tty_struct *tty)
1757 {
1758 memset(tty, 0, sizeof(struct tty_struct));
1759 tty->line = line;
1760 tty->disc = N_TTY;
1761 tty->pgrp = -1;
1762 if (IS_A_CONSOLE(line)) {
1763 tty->open = con_open;
1764 tty->winsize.ws_row = video_num_lines;
1765 tty->winsize.ws_col = video_num_columns;
1766 } else if IS_A_SERIAL(line) {
1767 tty->open = rs_open;
1768 } else if IS_A_PTY(line) {
1769 tty->open = pty_open;
1770 }
1771 }
1772
1773 static void initialize_termios(int line, struct termios * tp)
1774 {
1775 memset(tp, 0, sizeof(struct termios));
1776 memcpy(tp->c_cc, INIT_C_CC, NCCS);
1777 if (IS_A_CONSOLE(line) || IS_A_PTY_SLAVE(line)) {
1778 tp->c_iflag = ICRNL | IXON;
1779 tp->c_oflag = OPOST | ONLCR;
1780 tp->c_cflag = B38400 | CS8 | CREAD;
1781 tp->c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK |
1782 ECHOCTL | ECHOKE | IEXTEN;
1783 } else if (IS_A_SERIAL(line)) {
1784 tp->c_iflag = ICRNL | IXON;
1785 tp->c_oflag = OPOST | ONLCR | XTABS;
1786 tp->c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1787 tp->c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK |
1788 ECHOCTL | ECHOKE | IEXTEN;
1789 } else if (IS_A_PTY_MASTER(line))
1790 tp->c_cflag = B9600 | CS8 | CREAD;
1791 }
1792
1793 static struct tty_ldisc tty_ldisc_N_TTY = {
1794 0,
1795 NULL,
1796 NULL,
1797 read_chan,
1798 write_chan,
1799 NULL,
1800 normal_select,
1801 copy_to_cooked
1802 };
1803
1804
1805 long tty_init(long kmem_start)
1806 {
1807 int i;
1808
1809 if (sizeof(struct tty_struct) > PAGE_SIZE)
1810 panic("size of tty structure > PAGE_SIZE!");
1811 if (register_chrdev(TTY_MAJOR,"tty",&tty_fops))
1812 panic("unable to get major %d for tty device", TTY_MAJOR);
1813 if (register_chrdev(TTYAUX_MAJOR,"tty",&tty_fops))
1814 panic("unable to get major %d for tty device", TTYAUX_MAJOR);
1815 for (i=0 ; i< MAX_TTYS ; i++) {
1816 tty_table[i] = 0;
1817 tty_termios[i] = 0;
1818 }
1819 memset(tty_check_write, 0, sizeof(tty_check_write));
1820 bh_base[TTY_BH].routine = tty_bh_routine;
1821
1822
1823 memset(ldiscs, 0, sizeof(ldiscs));
1824 (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);
1825
1826 kmem_start = kbd_init(kmem_start);
1827 kmem_start = con_init(kmem_start);
1828 kmem_start = rs_init(kmem_start);
1829 return kmem_start;
1830 }