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