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