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