Import 2.1.33
[davej-history.git] / drivers / char / tty_io.c
blob86b441c4a07c6b976048bb4d6fb5f857755ed61e
1 /*
2 * linux/drivers/char/tty_io.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
7 /*
8 * 'tty_io.c' gives an orthogonal feeling to tty's, be they consoles
9 * or rs-channels. It also implements echoing, cooked mode etc.
11 * Kill-line thanks to John T Kohl, who also corrected VMIN = VTIME = 0.
13 * Modified by Theodore Ts'o, 9/14/92, to dynamically allocate the
14 * tty_struct and tty_queue structures. Previously there was an array
15 * of 256 tty_struct's which was statically allocated, and the
16 * tty_queue structures were allocated at boot time. Both are now
17 * dynamically allocated only when the tty is open.
19 * Also restructured routines so that there is more of a separation
20 * between the high-level tty routines (tty_io.c and tty_ioctl.c) and
21 * the low-level tty routines (serial.c, pty.c, console.c). This
22 * makes for cleaner and more compact code. -TYT, 9/17/92
24 * Modified by Fred N. van Kempen, 01/29/93, to add line disciplines
25 * which can be dynamically activated and de-activated by the line
26 * discipline handling modules (like SLIP).
28 * NOTE: pay no attention to the line discipline code (yet); its
29 * interface is still subject to change in this version...
30 * -- TYT, 1/31/92
32 * Added functionality to the OPOST tty handling. No delays, but all
33 * other bits should be there.
34 * -- Nick Holloway <alfie@dcs.warwick.ac.uk>, 27th May 1993.
36 * Rewrote canonical mode and added more termios flags.
37 * -- julian@uhunix.uhcc.hawaii.edu (J. Cowley), 13Jan94
39 * Reorganized FASYNC support so mouse code can share it.
40 * -- ctm@ardi.com, 9Sep95
42 * New TIOCLINUX variants added.
43 * -- mj@k332.feld.cvut.cz, 19-Nov-95
45 * Restrict vt switching via ioctl()
46 * -- grif@cs.ucr.edu, 5-Dec-95
48 * Move console and virtual terminal code to more apropriate files,
49 * implement CONFIG_VT and generalize console device interface.
50 * -- Marko Kohtala <Marko.Kohtala@hut.fi>, March 97
53 #include <linux/config.h>
54 #include <linux/types.h>
55 #include <linux/major.h>
56 #include <linux/errno.h>
57 #include <linux/signal.h>
58 #include <linux/fcntl.h>
59 #include <linux/sched.h>
60 #include <linux/interrupt.h>
61 #include <linux/tty.h>
62 #include <linux/tty_flip.h>
63 #include <linux/console.h>
64 #include <linux/timer.h>
65 #include <linux/ctype.h>
66 #include <linux/kd.h>
67 #include <linux/mm.h>
68 #include <linux/string.h>
69 #include <linux/malloc.h>
70 #include <linux/poll.h>
71 #ifdef CONFIG_PROC_FS
72 #include <linux/proc_fs.h>
73 #endif
75 #include <asm/uaccess.h>
76 #include <asm/system.h>
77 #include <asm/bitops.h>
79 #include "kbd_kern.h"
80 #include "vt_kern.h"
81 #include "selection.h"
83 #ifdef CONFIG_KERNELD
84 #include <linux/kerneld.h>
85 #endif
87 #define CONSOLE_DEV MKDEV(TTY_MAJOR,0)
88 #define TTY_DEV MKDEV(TTYAUX_MAJOR,0)
90 #undef TTY_DEBUG_HANGUP
92 #define TTY_PARANOIA_CHECK
93 #define CHECK_TTY_COUNT
95 struct termios tty_std_termios; /* for the benefit of tty drivers */
96 struct tty_driver *tty_drivers = NULL; /* linked list of tty drivers */
97 struct tty_ldisc ldiscs[NR_LDISCS]; /* line disc dispatch table */
100 * redirect is the pseudo-tty that console output
101 * is redirected to if asked by TIOCCONS.
103 struct tty_struct * redirect = NULL;
105 static void initialize_tty_struct(struct tty_struct *tty);
107 static long tty_read(struct inode *, struct file *, char *, unsigned long);
108 static long tty_write(struct inode *, struct file *, const char *, unsigned long);
109 static unsigned int tty_poll(struct file *, poll_table *);
110 static int tty_open(struct inode *, struct file *);
111 static int tty_release(struct inode *, struct file *);
112 static int tty_ioctl(struct inode * inode, struct file * file,
113 unsigned int cmd, unsigned long arg);
114 static int tty_fasync(struct inode * inode, struct file * filp, int on);
116 #ifndef MIN
117 #define MIN(a,b) ((a) < (b) ? (a) : (b))
118 #endif
121 * These two routines return the name of tty. tty_name() should NOT
122 * be used in interrupt drivers, since it's not re-entrant. Use
123 * _tty_name() instead.
125 char *_tty_name(struct tty_struct *tty, char *buf)
127 if (tty)
128 sprintf(buf, "%s%d", tty->driver.name,
129 MINOR(tty->device) - tty->driver.minor_start +
130 tty->driver.name_base);
131 else
132 strcpy(buf, "NULL tty");
133 return buf;
136 char *tty_name(struct tty_struct *tty)
138 static char buf[64];
140 return(_tty_name(tty, buf));
143 inline int tty_paranoia_check(struct tty_struct *tty, kdev_t device,
144 const char *routine)
146 #ifdef TTY_PARANOIA_CHECK
147 static const char *badmagic =
148 "Warning: bad magic number for tty struct (%s) in %s\n";
149 static const char *badtty =
150 "Warning: null TTY for (%s) in %s\n";
152 if (!tty) {
153 printk(badtty, kdevname(device), routine);
154 return 1;
156 if (tty->magic != TTY_MAGIC) {
157 printk(badmagic, kdevname(device), routine);
158 return 1;
160 #endif
161 return 0;
164 static int check_tty_count(struct tty_struct *tty, const char *routine)
166 #ifdef CHECK_TTY_COUNT
167 struct file *f;
168 int i, count = 0;
170 for (f = first_file, i=0; i<nr_files; i++, f = f->f_next) {
171 if (!f->f_count)
172 continue;
173 if (f->private_data == tty) {
174 count++;
177 if (tty->driver.type == TTY_DRIVER_TYPE_PTY &&
178 tty->driver.subtype == PTY_TYPE_SLAVE &&
179 tty->link && tty->link->count)
180 count++;
181 if (tty->count != count) {
182 printk("Warning: dev (%s) tty->count(%d) != #fd's(%d) in %s\n",
183 kdevname(tty->device), tty->count, count, routine);
184 return count;
186 #endif
187 return 0;
190 int tty_register_ldisc(int disc, struct tty_ldisc *new_ldisc)
192 if (disc < N_TTY || disc >= NR_LDISCS)
193 return -EINVAL;
195 if (new_ldisc) {
196 ldiscs[disc] = *new_ldisc;
197 ldiscs[disc].flags |= LDISC_FLAG_DEFINED;
198 ldiscs[disc].num = disc;
199 } else
200 memset(&ldiscs[disc], 0, sizeof(struct tty_ldisc));
202 return 0;
205 /* Set the discipline of a tty line. */
206 static int tty_set_ldisc(struct tty_struct *tty, int ldisc)
208 int retval = 0;
209 struct tty_ldisc o_ldisc;
211 if ((ldisc < N_TTY) || (ldisc >= NR_LDISCS))
212 return -EINVAL;
213 #ifdef CONFIG_KERNELD
214 /* Eduardo Blanco <ejbs@cs.cs.com.uy> */
215 if (!(ldiscs[ldisc].flags & LDISC_FLAG_DEFINED)) {
216 char modname [20];
217 sprintf(modname, "tty-ldisc-%d", ldisc);
218 request_module (modname);
220 #endif
221 if (!(ldiscs[ldisc].flags & LDISC_FLAG_DEFINED))
222 return -EINVAL;
224 if (tty->ldisc.num == ldisc)
225 return 0; /* We are already in the desired discipline */
226 o_ldisc = tty->ldisc;
228 tty_wait_until_sent(tty, 0);
230 /* Shutdown the current discipline. */
231 if (tty->ldisc.close)
232 (tty->ldisc.close)(tty);
234 /* Now set up the new line discipline. */
235 tty->ldisc = ldiscs[ldisc];
236 tty->termios->c_line = ldisc;
237 if (tty->ldisc.open)
238 retval = (tty->ldisc.open)(tty);
239 if (retval < 0) {
240 tty->ldisc = o_ldisc;
241 tty->termios->c_line = tty->ldisc.num;
242 if (tty->ldisc.open && (tty->ldisc.open(tty) < 0)) {
243 tty->ldisc = ldiscs[N_TTY];
244 tty->termios->c_line = N_TTY;
245 if (tty->ldisc.open) {
246 int r = tty->ldisc.open(tty);
248 if (r < 0)
249 panic("Couldn't open N_TTY ldisc for "
250 "%s --- error %d.",
251 tty_name(tty), r);
255 if (tty->ldisc.num != o_ldisc.num && tty->driver.set_ldisc)
256 tty->driver.set_ldisc(tty);
257 return retval;
261 * This routine returns a tty driver structure, given a device number
263 struct tty_driver *get_tty_driver(kdev_t device)
265 int major, minor;
266 struct tty_driver *p;
268 minor = MINOR(device);
269 major = MAJOR(device);
271 for (p = tty_drivers; p; p = p->next) {
272 if (p->major != major)
273 continue;
274 if (minor < p->minor_start)
275 continue;
276 if (minor >= p->minor_start + p->num)
277 continue;
278 return p;
280 return NULL;
284 * If we try to write to, or set the state of, a terminal and we're
285 * not in the foreground, send a SIGTTOU. If the signal is blocked or
286 * ignored, go ahead and perform the operation. (POSIX 7.2)
288 int tty_check_change(struct tty_struct * tty)
290 if (current->tty != tty)
291 return 0;
292 if (tty->pgrp <= 0) {
293 printk("tty_check_change: tty->pgrp <= 0!\n");
294 return 0;
296 if (current->pgrp == tty->pgrp)
297 return 0;
298 if (is_ignored(SIGTTOU))
299 return 0;
300 if (is_orphaned_pgrp(current->pgrp))
301 return -EIO;
302 (void) kill_pg(current->pgrp,SIGTTOU,1);
303 return -ERESTARTSYS;
306 static long hung_up_tty_read(struct inode * inode, struct file * file,
307 char * buf, unsigned long count)
309 return 0;
312 static long hung_up_tty_write(struct inode * inode,
313 struct file * file, const char * buf, unsigned long count)
315 return -EIO;
318 static unsigned int hung_up_tty_poll(struct file * filp, poll_table * wait)
320 return POLLIN | POLLOUT | POLLERR | POLLHUP | POLLRDNORM | POLLWRNORM;
323 static int hung_up_tty_ioctl(struct inode * inode, struct file * file,
324 unsigned int cmd, unsigned long arg)
326 return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
329 static long long tty_lseek(struct inode * inode, struct file * file,
330 long long offset, int orig)
332 return -ESPIPE;
335 static struct file_operations tty_fops = {
336 tty_lseek,
337 tty_read,
338 tty_write,
339 NULL, /* tty_readdir */
340 tty_poll,
341 tty_ioctl,
342 NULL, /* tty_mmap */
343 tty_open,
344 tty_release,
345 NULL, /* tty_fsync */
346 tty_fasync
349 static struct file_operations hung_up_tty_fops = {
350 tty_lseek,
351 hung_up_tty_read,
352 hung_up_tty_write,
353 NULL, /* hung_up_tty_readdir */
354 hung_up_tty_poll,
355 hung_up_tty_ioctl,
356 NULL, /* hung_up_tty_mmap */
357 NULL, /* hung_up_tty_open */
358 tty_release, /* hung_up_tty_release */
359 NULL, /* hung_up_tty_fsync */
360 NULL /* hung_up_tty_fasync */
363 void do_tty_hangup(struct tty_struct * tty, struct file_operations *fops)
365 int i;
366 struct file * filp;
367 struct task_struct *p;
369 if (!tty)
370 return;
371 check_tty_count(tty, "do_tty_hangup");
372 for (filp = first_file, i=0; i<nr_files; i++, filp = filp->f_next) {
373 if (!filp->f_count)
374 continue;
375 if (filp->private_data != tty)
376 continue;
377 if (!filp->f_inode)
378 continue;
379 if (filp->f_inode->i_rdev == CONSOLE_DEV)
380 continue;
381 if (filp->f_op != &tty_fops)
382 continue;
383 tty_fasync(filp->f_inode, filp, 0);
384 filp->f_op = fops;
387 if (tty->ldisc.flush_buffer)
388 tty->ldisc.flush_buffer(tty);
389 if (tty->driver.flush_buffer)
390 tty->driver.flush_buffer(tty);
391 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
392 tty->ldisc.write_wakeup)
393 (tty->ldisc.write_wakeup)(tty);
394 wake_up_interruptible(&tty->write_wait);
395 wake_up_interruptible(&tty->read_wait);
398 * Shutdown the current line discipline, and reset it to
399 * N_TTY.
401 if (tty->ldisc.num != ldiscs[N_TTY].num) {
402 if (tty->ldisc.close)
403 (tty->ldisc.close)(tty);
404 tty->ldisc = ldiscs[N_TTY];
405 tty->termios->c_line = N_TTY;
406 if (tty->ldisc.open) {
407 i = (tty->ldisc.open)(tty);
408 if (i < 0)
409 printk("do_tty_hangup: N_TTY open: error %d\n",
410 -i);
414 for_each_task(p) {
415 if ((tty->session > 0) && (p->session == tty->session) &&
416 p->leader) {
417 send_sig(SIGHUP,p,1);
418 send_sig(SIGCONT,p,1);
419 if (tty->pgrp > 0)
420 p->tty_old_pgrp = tty->pgrp;
422 if (p->tty == tty)
423 p->tty = NULL;
425 tty->flags = 0;
426 tty->session = 0;
427 tty->pgrp = -1;
428 tty->ctrl_status = 0;
429 if (tty->driver.flags & TTY_DRIVER_RESET_TERMIOS)
430 *tty->termios = tty->driver.init_termios;
431 if (tty->driver.hangup)
432 (tty->driver.hangup)(tty);
435 void tty_hangup(struct tty_struct * tty)
437 #ifdef TTY_DEBUG_HANGUP
438 printk("%s hangup...\n", tty_name(tty));
439 #endif
440 do_tty_hangup(tty, &hung_up_tty_fops);
443 void tty_vhangup(struct tty_struct * tty)
445 #ifdef TTY_DEBUG_HANGUP
446 printk("%s vhangup...\n", tty_name(tty));
447 #endif
448 do_tty_hangup(tty, &hung_up_tty_fops);
451 int tty_hung_up_p(struct file * filp)
453 return (filp->f_op == &hung_up_tty_fops);
457 * This function is typically called only by the session leader, when
458 * it wants to disassociate itself from its controlling tty.
460 * It performs the following functions:
461 * (1) Sends a SIGHUP and SIGCONT to the foreground process group
462 * (2) Clears the tty from being controlling the session
463 * (3) Clears the controlling tty for all processes in the
464 * session group.
466 * The argument on_exit is set to 1 if called when a process is
467 * exiting; it is 0 if called by the ioctl TIOCNOTTY.
469 void disassociate_ctty(int on_exit)
471 struct tty_struct *tty = current->tty;
472 struct task_struct *p;
473 int tty_pgrp = -1;
475 if (tty) {
476 tty_pgrp = tty->pgrp;
477 if (on_exit && tty->driver.type != TTY_DRIVER_TYPE_PTY)
478 tty_vhangup(tty);
479 } else {
480 if (current->tty_old_pgrp) {
481 kill_pg(current->tty_old_pgrp, SIGHUP, on_exit);
482 kill_pg(current->tty_old_pgrp, SIGCONT, on_exit);
484 return;
486 if (tty_pgrp > 0) {
487 kill_pg(tty_pgrp, SIGHUP, on_exit);
488 if (!on_exit)
489 kill_pg(tty_pgrp, SIGCONT, on_exit);
492 current->tty_old_pgrp = 0;
493 tty->session = 0;
494 tty->pgrp = -1;
496 for_each_task(p)
497 if (p->session == current->session)
498 p->tty = NULL;
501 void wait_for_keypress(void)
503 struct console *c = console_drivers;
504 while(c && !c->wait_key)
505 c = c->next;
506 if (c) c->wait_key();
509 void stop_tty(struct tty_struct *tty)
511 if (tty->stopped)
512 return;
513 tty->stopped = 1;
514 if (tty->link && tty->link->packet) {
515 tty->ctrl_status &= ~TIOCPKT_START;
516 tty->ctrl_status |= TIOCPKT_STOP;
517 wake_up_interruptible(&tty->link->read_wait);
519 if (tty->driver.stop)
520 (tty->driver.stop)(tty);
523 void start_tty(struct tty_struct *tty)
525 if (!tty->stopped || tty->flow_stopped)
526 return;
527 tty->stopped = 0;
528 if (tty->link && tty->link->packet) {
529 tty->ctrl_status &= ~TIOCPKT_STOP;
530 tty->ctrl_status |= TIOCPKT_START;
531 wake_up_interruptible(&tty->link->read_wait);
533 if (tty->driver.start)
534 (tty->driver.start)(tty);
535 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
536 tty->ldisc.write_wakeup)
537 (tty->ldisc.write_wakeup)(tty);
538 wake_up_interruptible(&tty->write_wait);
541 static long tty_read(struct inode * inode, struct file * file,
542 char * buf, unsigned long count)
544 int i;
545 struct tty_struct * tty;
547 tty = (struct tty_struct *)file->private_data;
548 if (tty_paranoia_check(tty, inode->i_rdev, "tty_read"))
549 return -EIO;
550 if (!tty || (tty->flags & (1 << TTY_IO_ERROR)))
551 return -EIO;
553 /* This check not only needs to be done before reading, but also
554 whenever read_chan() gets woken up after sleeping, so I've
555 moved it to there. This should only be done for the N_TTY
556 line discipline, anyway. Same goes for write_chan(). -- jlc. */
557 #if 0
558 if ((inode->i_rdev != CONSOLE_DEV) && /* don't stop on /dev/console */
559 (tty->pgrp > 0) &&
560 (current->tty == tty) &&
561 (tty->pgrp != current->pgrp))
562 if (is_ignored(SIGTTIN) || is_orphaned_pgrp(current->pgrp))
563 return -EIO;
564 else {
565 (void) kill_pg(current->pgrp, SIGTTIN, 1);
566 return -ERESTARTSYS;
568 #endif
569 if (tty->ldisc.read)
570 i = (tty->ldisc.read)(tty,file,buf,count);
571 else
572 i = -EIO;
573 if (i > 0)
574 inode->i_atime = CURRENT_TIME;
575 return i;
579 * Split writes up in sane blocksizes to avoid
580 * denial-of-service type attacks
582 static inline int do_tty_write(
583 int (*write)(struct tty_struct *, struct file *, const unsigned char *, unsigned int),
584 struct inode *inode,
585 struct tty_struct *tty,
586 struct file *file,
587 const unsigned char *buf,
588 unsigned int count)
590 int ret = 0, written = 0;
592 for (;;) {
593 unsigned long size = PAGE_SIZE*2;
594 if (size > count)
595 size = count;
596 ret = write(tty, file, buf, size);
597 if (ret <= 0)
598 break;
599 written += ret;
600 buf += ret;
601 count -= ret;
602 if (!count)
603 break;
604 ret = -ERESTARTSYS;
605 if (current->signal & ~current->blocked)
606 break;
607 if (need_resched)
608 schedule();
610 if (written) {
611 inode->i_mtime = CURRENT_TIME;
612 ret = written;
614 return ret;
618 static long tty_write(struct inode * inode, struct file * file,
619 const char * buf, unsigned long count)
621 int is_console;
622 struct tty_struct * tty;
624 is_console = (inode->i_rdev == CONSOLE_DEV);
626 if (is_console && redirect)
627 tty = redirect;
628 else
629 tty = (struct tty_struct *)file->private_data;
630 if (tty_paranoia_check(tty, inode->i_rdev, "tty_write"))
631 return -EIO;
632 if (!tty || !tty->driver.write || (tty->flags & (1 << TTY_IO_ERROR)))
633 return -EIO;
634 #if 0
635 if (!is_console && L_TOSTOP(tty) && (tty->pgrp > 0) &&
636 (current->tty == tty) && (tty->pgrp != current->pgrp)) {
637 if (is_orphaned_pgrp(current->pgrp))
638 return -EIO;
639 if (!is_ignored(SIGTTOU)) {
640 (void) kill_pg(current->pgrp, SIGTTOU, 1);
641 return -ERESTARTSYS;
644 #endif
645 if (!tty->ldisc.write)
646 return -EIO;
647 return do_tty_write(tty->ldisc.write,
648 inode, tty, file,
649 (const unsigned char *)buf,
650 (unsigned int)count);
654 * This is so ripe with races that you should *really* not touch this
655 * unless you know exactly what you are doing. All the changes have to be
656 * made atomically, or there may be incorrect pointers all over the place.
658 static int init_dev(kdev_t device, struct tty_struct **ret_tty)
660 struct tty_struct *tty, **tty_loc, *o_tty, **o_tty_loc;
661 struct termios *tp, **tp_loc, *o_tp, **o_tp_loc;
662 struct termios *ltp, **ltp_loc, *o_ltp, **o_ltp_loc;
663 struct tty_driver *driver;
664 int retval;
665 int idx;
667 driver = get_tty_driver(device);
668 if (!driver)
669 return -ENODEV;
671 idx = MINOR(device) - driver->minor_start;
672 tty = o_tty = NULL;
673 tp = o_tp = NULL;
674 ltp = o_ltp = NULL;
675 o_tty_loc = NULL;
676 o_tp_loc = o_ltp_loc = NULL;
678 tty_loc = &driver->table[idx];
679 tp_loc = &driver->termios[idx];
680 ltp_loc = &driver->termios_locked[idx];
682 repeat:
683 retval = -EIO;
684 if (driver->type == TTY_DRIVER_TYPE_PTY &&
685 driver->subtype == PTY_TYPE_MASTER &&
686 *tty_loc && (*tty_loc)->count)
687 goto end_init;
688 retval = -ENOMEM;
689 if (!*tty_loc && !tty) {
690 if (!(tty = (struct tty_struct*) get_free_page(GFP_KERNEL)))
691 goto end_init;
692 initialize_tty_struct(tty);
693 tty->device = device;
694 tty->driver = *driver;
695 goto repeat;
697 if (!*tp_loc && !tp) {
698 tp = (struct termios *) kmalloc(sizeof(struct termios),
699 GFP_KERNEL);
700 if (!tp)
701 goto end_init;
702 *tp = driver->init_termios;
703 goto repeat;
705 if (!*ltp_loc && !ltp) {
706 ltp = (struct termios *) kmalloc(sizeof(struct termios),
707 GFP_KERNEL);
708 if (!ltp)
709 goto end_init;
710 memset(ltp, 0, sizeof(struct termios));
711 goto repeat;
713 if (driver->type == TTY_DRIVER_TYPE_PTY) {
714 o_tty_loc = &driver->other->table[idx];
715 o_tp_loc = &driver->other->termios[idx];
716 o_ltp_loc = &driver->other->termios_locked[idx];
718 if (!*o_tty_loc && !o_tty) {
719 kdev_t o_device;
721 o_tty = (struct tty_struct *)
722 get_free_page(GFP_KERNEL);
723 if (!o_tty)
724 goto end_init;
725 o_device = MKDEV(driver->other->major,
726 driver->other->minor_start + idx);
727 initialize_tty_struct(o_tty);
728 o_tty->device = o_device;
729 o_tty->driver = *driver->other;
730 goto repeat;
732 if (!*o_tp_loc && !o_tp) {
733 o_tp = (struct termios *)
734 kmalloc(sizeof(struct termios), GFP_KERNEL);
735 if (!o_tp)
736 goto end_init;
737 *o_tp = driver->other->init_termios;
738 goto repeat;
740 if (!*o_ltp_loc && !o_ltp) {
741 o_ltp = (struct termios *)
742 kmalloc(sizeof(struct termios), GFP_KERNEL);
743 if (!o_ltp)
744 goto end_init;
745 memset(o_ltp, 0, sizeof(struct termios));
746 goto repeat;
750 /* Now we have allocated all the structures: update all the pointers.. */
751 if (!*tp_loc) {
752 *tp_loc = tp;
753 tp = NULL;
755 if (!*ltp_loc) {
756 *ltp_loc = ltp;
757 ltp = NULL;
759 if (!*tty_loc) {
760 tty->termios = *tp_loc;
761 tty->termios_locked = *ltp_loc;
762 *tty_loc = tty;
763 (*driver->refcount)++;
764 (*tty_loc)->count++;
765 if (tty->ldisc.open) {
766 retval = (tty->ldisc.open)(tty);
767 if (retval < 0) {
768 (*tty_loc)->count--;
769 tty = NULL;
770 goto end_init;
773 tty = NULL;
774 } else {
775 if ((*tty_loc)->flags & (1 << TTY_CLOSING)) {
776 printk("Attempt to open closing tty %s.\n",
777 tty_name(*tty_loc));
778 printk("Ack!!!! This should never happen!!\n");
779 return -EINVAL;
781 (*tty_loc)->count++;
783 if (driver->type == TTY_DRIVER_TYPE_PTY) {
784 if (!*o_tp_loc) {
785 *o_tp_loc = o_tp;
786 o_tp = NULL;
788 if (!*o_ltp_loc) {
789 *o_ltp_loc = o_ltp;
790 o_ltp = NULL;
792 if (!*o_tty_loc) {
793 o_tty->termios = *o_tp_loc;
794 o_tty->termios_locked = *o_ltp_loc;
795 *o_tty_loc = o_tty;
796 (*driver->other->refcount)++;
797 if (o_tty->ldisc.open) {
798 retval = (o_tty->ldisc.open)(o_tty);
799 if (retval < 0) {
800 (*tty_loc)->count--;
801 o_tty = NULL;
802 goto end_init;
805 o_tty = NULL;
807 (*tty_loc)->link = *o_tty_loc;
808 (*o_tty_loc)->link = *tty_loc;
809 if (driver->subtype == PTY_TYPE_MASTER)
810 (*o_tty_loc)->count++;
812 (*tty_loc)->driver = *driver;
813 *ret_tty = *tty_loc;
814 retval = 0;
815 end_init:
816 if (tty)
817 free_page((unsigned long) tty);
818 if (o_tty)
819 free_page((unsigned long) o_tty);
820 if (tp)
821 kfree_s(tp, sizeof(struct termios));
822 if (o_tp)
823 kfree_s(o_tp, sizeof(struct termios));
824 if (ltp)
825 kfree_s(ltp, sizeof(struct termios));
826 if (o_ltp)
827 kfree_s(o_ltp, sizeof(struct termios));
828 return retval;
832 * Even releasing the tty structures is a tricky business.. We have
833 * to be very careful that the structures are all released at the
834 * same time, as interrupts might otherwise get the wrong pointers.
836 static void release_dev(struct file * filp)
838 struct tty_struct *tty, *o_tty;
839 struct termios *tp, *o_tp, *ltp, *o_ltp;
840 struct task_struct **p;
841 int idx;
843 tty = (struct tty_struct *)filp->private_data;
844 if (tty_paranoia_check(tty, filp->f_inode->i_rdev, "release_dev"))
845 return;
847 check_tty_count(tty, "release_dev");
849 tty_fasync(filp->f_inode, filp, 0);
851 tp = tty->termios;
852 ltp = tty->termios_locked;
854 idx = MINOR(tty->device) - tty->driver.minor_start;
855 #ifdef TTY_PARANOIA_CHECK
856 if (idx < 0 || idx >= tty->driver.num) {
857 printk("release_dev: bad idx when trying to free (%s)\n",
858 kdevname(tty->device));
859 return;
861 if (tty != tty->driver.table[idx]) {
862 printk("release_dev: driver.table[%d] not tty for (%s)\n",
863 idx, kdevname(tty->device));
864 return;
866 if (tp != tty->driver.termios[idx]) {
867 printk("release_dev: driver.termios[%d] not termios for ("
868 "%s)\n",
869 idx, kdevname(tty->device));
870 return;
872 if (ltp != tty->driver.termios_locked[idx]) {
873 printk("release_dev: driver.termios_locked[%d] not termios_locked for ("
874 "%s)\n",
875 idx, kdevname(tty->device));
876 return;
878 #endif
880 #ifdef TTY_DEBUG_HANGUP
881 printk("release_dev of %s (tty count=%d)...", tty_name(tty),
882 tty->count);
883 #endif
885 o_tty = tty->link;
886 o_tp = (o_tty) ? o_tty->termios : NULL;
887 o_ltp = (o_tty) ? o_tty->termios_locked : NULL;
889 #ifdef TTY_PARANOIA_CHECK
890 if (tty->driver.other) {
891 if (o_tty != tty->driver.other->table[idx]) {
892 printk("release_dev: other->table[%d] not o_tty for ("
893 "%s)\n",
894 idx, kdevname(tty->device));
895 return;
897 if (o_tp != tty->driver.other->termios[idx]) {
898 printk("release_dev: other->termios[%d] not o_termios for ("
899 "%s)\n",
900 idx, kdevname(tty->device));
901 return;
903 if (o_ltp != tty->driver.other->termios_locked[idx]) {
904 printk("release_dev: other->termios_locked[%d] not o_termios_locked for ("
905 "%s)\n",
906 idx, kdevname(tty->device));
907 return;
910 if (o_tty->link != tty) {
911 printk("release_dev: bad pty pointers\n");
912 return;
915 #endif
917 if (tty->driver.close)
918 tty->driver.close(tty, filp);
919 if (tty->driver.type == TTY_DRIVER_TYPE_PTY &&
920 tty->driver.subtype == PTY_TYPE_MASTER) {
921 if (--tty->link->count < 0) {
922 printk("release_dev: bad pty slave count (%d) for %s\n",
923 tty->count, tty_name(tty));
924 tty->link->count = 0;
927 if (--tty->count < 0) {
928 printk("release_dev: bad tty->count (%d) for %s\n",
929 tty->count, tty_name(tty));
930 tty->count = 0;
932 if (tty->count)
933 return;
936 * Sanity check --- if tty->count is zero, there shouldn't be
937 * any waiters on tty->read_wait or tty->write_wait. But just
938 * in case....
940 while (1) {
941 if (waitqueue_active(&tty->read_wait)) {
942 printk("release_dev: %s: read_wait active?!?\n",
943 tty_name(tty));
944 wake_up(&tty->read_wait);
945 } else if (waitqueue_active(&tty->write_wait)) {
946 printk("release_dev: %s: write_wait active?!?\n",
947 tty_name(tty));
948 wake_up(&tty->write_wait);
949 } else
950 break;
951 schedule();
955 * We're committed; at this point, we must not block!
957 if (o_tty) {
958 if (o_tty->count)
959 return;
960 tty->driver.other->table[idx] = NULL;
961 tty->driver.other->termios[idx] = NULL;
962 kfree_s(o_tp, sizeof(struct termios));
965 #ifdef TTY_DEBUG_HANGUP
966 printk("freeing tty structure...");
967 #endif
968 tty->flags |= (1 << TTY_CLOSING);
971 * Make sure there aren't any processes that still think this
972 * tty is their controlling tty.
974 for (p = &LAST_TASK ; p > &FIRST_TASK ; --p) {
975 if (*p == 0)
976 continue;
977 if ((*p)->tty == tty)
978 (*p)->tty = NULL;
979 if (o_tty && (*p)->tty == o_tty)
980 (*p)->tty = NULL;
984 * Shutdown the current line discipline, and reset it to
985 * N_TTY.
987 if (tty->ldisc.close)
988 (tty->ldisc.close)(tty);
989 tty->ldisc = ldiscs[N_TTY];
990 tty->termios->c_line = N_TTY;
991 if (o_tty) {
992 if (o_tty->ldisc.close)
993 (o_tty->ldisc.close)(o_tty);
994 o_tty->ldisc = ldiscs[N_TTY];
997 tty->driver.table[idx] = NULL;
998 if (tty->driver.flags & TTY_DRIVER_RESET_TERMIOS) {
999 tty->driver.termios[idx] = NULL;
1000 kfree_s(tp, sizeof(struct termios));
1002 if (tty == redirect || o_tty == redirect)
1003 redirect = NULL;
1005 * Make sure that the tty's task queue isn't activated. If it
1006 * is, take it out of the linked list.
1008 spin_lock_irq(&tqueue_lock);
1009 if (tty->flip.tqueue.sync) {
1010 struct tq_struct *tq, *prev;
1012 for (tq=tq_timer, prev=0; tq; prev=tq, tq=tq->next) {
1013 if (tq == &tty->flip.tqueue) {
1014 if (prev)
1015 prev->next = tq->next;
1016 else
1017 tq_timer = tq->next;
1018 break;
1022 spin_unlock_irq(&tqueue_lock);
1023 tty->magic = 0;
1024 (*tty->driver.refcount)--;
1025 free_page((unsigned long) tty);
1026 filp->private_data = 0;
1027 if (o_tty) {
1028 o_tty->magic = 0;
1029 (*o_tty->driver.refcount)--;
1030 free_page((unsigned long) o_tty);
1035 * tty_open and tty_release keep up the tty count that contains the
1036 * number of opens done on a tty. We cannot use the inode-count, as
1037 * different inodes might point to the same tty.
1039 * Open-counting is needed for pty masters, as well as for keeping
1040 * track of serial lines: DTR is dropped when the last close happens.
1041 * (This is not done solely through tty->count, now. - Ted 1/27/92)
1043 * The termios state of a pty is reset on first open so that
1044 * settings don't persist across reuse.
1046 static int tty_open(struct inode * inode, struct file * filp)
1048 struct tty_struct *tty;
1049 int minor;
1050 int noctty, retval;
1051 kdev_t device;
1052 unsigned short saved_flags;
1054 saved_flags = filp->f_flags;
1055 retry_open:
1056 noctty = filp->f_flags & O_NOCTTY;
1057 device = inode->i_rdev;
1058 if (device == TTY_DEV) {
1059 if (!current->tty)
1060 return -ENXIO;
1061 device = current->tty->device;
1062 filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */
1063 /* noctty = 1; */
1065 if (device == CONSOLE_DEV) {
1066 struct console *c = console_drivers;
1067 while(c && !c->device)
1068 c = c->next;
1069 if (!c)
1070 return -ENODEV;
1071 device = c->device();
1072 noctty = 1;
1074 minor = MINOR(device);
1076 retval = init_dev(device, &tty);
1077 if (retval)
1078 return retval;
1079 filp->private_data = tty;
1080 check_tty_count(tty, "tty_open");
1081 if (tty->driver.type == TTY_DRIVER_TYPE_PTY &&
1082 tty->driver.subtype == PTY_TYPE_MASTER)
1083 noctty = 1;
1084 #ifdef TTY_DEBUG_HANGUP
1085 printk("opening %s...", tty_name(tty));
1086 #endif
1087 if (tty->driver.open)
1088 retval = tty->driver.open(tty, filp);
1089 else
1090 retval = -ENODEV;
1091 filp->f_flags = saved_flags;
1093 if (!retval && test_bit(TTY_EXCLUSIVE, &tty->flags) && !suser())
1094 retval = -EBUSY;
1096 if (retval) {
1097 #ifdef TTY_DEBUG_HANGUP
1098 printk("error %d in opening %s...", retval, tty_name(tty));
1099 #endif
1101 release_dev(filp);
1102 if (retval != -ERESTARTSYS)
1103 return retval;
1104 if (current->signal & ~current->blocked)
1105 return retval;
1106 schedule();
1108 * Need to reset f_op in case a hangup happened.
1110 filp->f_op = &tty_fops;
1111 goto retry_open;
1113 if (!noctty &&
1114 current->leader &&
1115 !current->tty &&
1116 tty->session == 0) {
1117 current->tty = tty;
1118 current->tty_old_pgrp = 0;
1119 tty->session = current->session;
1120 tty->pgrp = current->pgrp;
1122 return 0;
1126 * Note that releasing a pty master also releases the child, so
1127 * we have to make the redirection checks after that and on both
1128 * sides of a pty.
1130 static int tty_release(struct inode * inode, struct file * filp)
1132 release_dev(filp);
1133 return 0;
1136 static unsigned int tty_poll(struct file * filp, poll_table * wait)
1138 struct tty_struct * tty;
1140 tty = (struct tty_struct *)filp->private_data;
1141 if (tty_paranoia_check(tty, filp->f_inode->i_rdev, "tty_poll"))
1142 return 0;
1144 if (tty->ldisc.poll)
1145 return (tty->ldisc.poll)(tty, filp, wait);
1146 return 0;
1150 * fasync_helper() is used by some character device drivers (mainly mice)
1151 * to set up the fasync queue. It returns negative on error, 0 if it did
1152 * no changes and positive if it added/deleted the entry.
1154 int fasync_helper(struct inode * inode, struct file * filp, int on, struct fasync_struct **fapp)
1156 struct fasync_struct *fa, **fp;
1157 unsigned long flags;
1159 for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) {
1160 if (fa->fa_file == filp)
1161 break;
1164 if (on) {
1165 if (fa)
1166 return 0;
1167 fa = (struct fasync_struct *)kmalloc(sizeof(struct fasync_struct), GFP_KERNEL);
1168 if (!fa)
1169 return -ENOMEM;
1170 fa->magic = FASYNC_MAGIC;
1171 fa->fa_file = filp;
1172 save_flags(flags);
1173 cli();
1174 fa->fa_next = *fapp;
1175 *fapp = fa;
1176 restore_flags(flags);
1177 return 1;
1179 if (!fa)
1180 return 0;
1181 save_flags(flags);
1182 cli();
1183 *fp = fa->fa_next;
1184 restore_flags(flags);
1185 kfree(fa);
1186 return 1;
1189 static int tty_fasync(struct inode * inode, struct file * filp, int on)
1191 struct tty_struct * tty;
1192 int retval;
1194 tty = (struct tty_struct *)filp->private_data;
1195 if (tty_paranoia_check(tty, inode->i_rdev, "tty_fasync"))
1196 return 0;
1198 retval = fasync_helper(inode, filp, on, &tty->fasync);
1199 if (retval <= 0)
1200 return retval;
1202 if (on) {
1203 if (!waitqueue_active(&tty->read_wait))
1204 tty->minimum_to_wake = 1;
1205 if (filp->f_owner == 0) {
1206 if (tty->pgrp)
1207 filp->f_owner = -tty->pgrp;
1208 else
1209 filp->f_owner = current->pid;
1211 } else {
1212 if (!tty->fasync && !waitqueue_active(&tty->read_wait))
1213 tty->minimum_to_wake = N_TTY_BUF_SIZE;
1215 return 0;
1218 #if 0
1220 * XXX does anyone use this anymore?!?
1222 static int do_get_ps_info(unsigned long arg)
1224 struct tstruct {
1225 int flag;
1226 int present[NR_TASKS];
1227 struct task_struct tasks[NR_TASKS];
1229 struct tstruct *ts = (struct tstruct *)arg;
1230 struct task_struct **p;
1231 char *c, *d;
1232 int i, n = 0;
1234 i = verify_area(VERIFY_WRITE, (void *)arg, sizeof(struct tstruct));
1235 if (i)
1236 return i;
1237 for (p = &FIRST_TASK ; p <= &LAST_TASK ; p++, n++)
1238 if (*p)
1240 c = (char *)(*p);
1241 d = (char *)(ts->tasks+n);
1242 for (i=0 ; i<sizeof(struct task_struct) ; i++)
1243 put_user(*c++, d++);
1244 put_user(1, ts->present+n);
1246 else
1247 put_user(0, ts->present+n);
1248 return(0);
1250 #endif
1252 static int tiocsti(struct tty_struct *tty, char * arg)
1254 char ch, mbz = 0;
1256 if ((current->tty != tty) && !suser())
1257 return -EPERM;
1258 if (get_user(ch, arg))
1259 return -EFAULT;
1260 tty->ldisc.receive_buf(tty, &ch, &mbz, 1);
1261 return 0;
1264 static int tiocgwinsz(struct tty_struct *tty, struct winsize * arg)
1266 if (copy_to_user(arg, &tty->winsize, sizeof(*arg)))
1267 return -EFAULT;
1268 return 0;
1271 static int tiocswinsz(struct tty_struct *tty, struct tty_struct *real_tty,
1272 struct winsize * arg)
1274 struct winsize tmp_ws;
1276 if (copy_from_user(&tmp_ws, arg, sizeof(*arg)))
1277 return -EFAULT;
1278 if (!memcmp(&tmp_ws, &tty->winsize, sizeof(*arg)))
1279 return 0;
1280 if (tty->pgrp > 0)
1281 kill_pg(tty->pgrp, SIGWINCH, 1);
1282 if ((real_tty->pgrp != tty->pgrp) && (real_tty->pgrp > 0))
1283 kill_pg(real_tty->pgrp, SIGWINCH, 1);
1284 tty->winsize = tmp_ws;
1285 real_tty->winsize = tmp_ws;
1286 return 0;
1289 static int tioccons(struct tty_struct *tty, struct tty_struct *real_tty)
1291 if (tty->driver.type == TTY_DRIVER_TYPE_CONSOLE) {
1292 if (!suser())
1293 return -EPERM;
1294 redirect = NULL;
1295 return 0;
1297 if (redirect)
1298 return -EBUSY;
1299 redirect = real_tty;
1300 return 0;
1304 static int fionbio(struct file *file, int *arg)
1306 int nonblock;
1308 if (get_user(nonblock, arg))
1309 return -EFAULT;
1311 if (nonblock)
1312 file->f_flags |= O_NONBLOCK;
1313 else
1314 file->f_flags &= ~O_NONBLOCK;
1315 return 0;
1318 static int tiocsctty(struct tty_struct *tty, int arg)
1320 if (current->leader &&
1321 (current->session == tty->session))
1322 return 0;
1324 * The process must be a session leader and
1325 * not have a controlling tty already.
1327 if (!current->leader || current->tty)
1328 return -EPERM;
1329 if (tty->session > 0) {
1331 * This tty is already the controlling
1332 * tty for another session group!
1334 if ((arg == 1) && suser()) {
1336 * Steal it away
1338 struct task_struct *p;
1340 for_each_task(p)
1341 if (p->tty == tty)
1342 p->tty = NULL;
1343 } else
1344 return -EPERM;
1346 current->tty = tty;
1347 current->tty_old_pgrp = 0;
1348 tty->session = current->session;
1349 tty->pgrp = current->pgrp;
1350 return 0;
1353 static int tiocgpgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t *arg)
1356 * (tty == real_tty) is a cheap way of
1357 * testing if the tty is NOT a master pty.
1359 if (tty == real_tty && current->tty != real_tty)
1360 return -ENOTTY;
1361 return put_user(real_tty->pgrp, arg);
1364 static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t *arg)
1366 pid_t pgrp;
1367 int retval = tty_check_change(real_tty);
1369 if (retval == -EIO)
1370 return -ENOTTY;
1371 if (retval)
1372 return retval;
1373 if (!current->tty ||
1374 (current->tty != real_tty) ||
1375 (real_tty->session != current->session))
1376 return -ENOTTY;
1377 get_user(pgrp, (pid_t *) arg);
1378 if (pgrp < 0)
1379 return -EINVAL;
1380 if (session_of_pgrp(pgrp) != current->session)
1381 return -EPERM;
1382 real_tty->pgrp = pgrp;
1383 return 0;
1386 static int tiocttygstruct(struct tty_struct *tty, struct tty_struct *arg)
1388 if (copy_to_user(arg, tty, sizeof(*arg)))
1389 return -EFAULT;
1390 return 0;
1393 static int tiocsetd(struct tty_struct *tty, int *arg)
1395 int retval, ldisc;
1397 retval = tty_check_change(tty);
1398 if (retval)
1399 return retval;
1400 retval = get_user(ldisc, arg);
1401 if (retval)
1402 return retval;
1403 return tty_set_ldisc(tty, ldisc);
1407 * Split this up, as gcc can choke on it otherwise..
1409 static int tty_ioctl(struct inode * inode, struct file * file,
1410 unsigned int cmd, unsigned long arg)
1412 struct tty_struct *tty, *real_tty;
1414 tty = (struct tty_struct *)file->private_data;
1415 if (tty_paranoia_check(tty, inode->i_rdev, "tty_ioctl"))
1416 return -EINVAL;
1418 real_tty = tty;
1419 if (tty->driver.type == TTY_DRIVER_TYPE_PTY &&
1420 tty->driver.subtype == PTY_TYPE_MASTER)
1421 real_tty = tty->link;
1423 switch (cmd) {
1424 case TIOCSTI:
1425 return tiocsti(tty, (char *)arg);
1426 case TIOCGWINSZ:
1427 return tiocgwinsz(tty, (struct winsize *) arg);
1428 case TIOCSWINSZ:
1429 return tiocswinsz(tty, real_tty, (struct winsize *) arg);
1430 case TIOCCONS:
1431 return tioccons(tty, real_tty);
1432 case FIONBIO:
1433 return fionbio(file, (int *) arg);
1434 case TIOCEXCL:
1435 set_bit(TTY_EXCLUSIVE, &tty->flags);
1436 return 0;
1437 case TIOCNXCL:
1438 clear_bit(TTY_EXCLUSIVE, &tty->flags);
1439 return 0;
1440 case TIOCNOTTY:
1441 if (current->tty != tty)
1442 return -ENOTTY;
1443 if (current->leader)
1444 disassociate_ctty(0);
1445 current->tty = NULL;
1446 return 0;
1447 case TIOCSCTTY:
1448 return tiocsctty(tty, arg);
1449 case TIOCGPGRP:
1450 return tiocgpgrp(tty, real_tty, (pid_t *) arg);
1451 case TIOCSPGRP:
1452 return tiocspgrp(tty, real_tty, (pid_t *) arg);
1453 case TIOCGETD:
1454 return put_user(tty->ldisc.num, (int *) arg);
1455 case TIOCSETD:
1456 return tiocsetd(tty, (int *) arg);
1457 #ifdef CONFIG_VT
1458 case TIOCLINUX:
1459 return tioclinux(tty, arg);
1460 #endif
1461 case TIOCTTYGSTRUCT:
1462 return tiocttygstruct(tty, (struct tty_struct *) arg);
1464 if (tty->driver.ioctl) {
1465 int retval = (tty->driver.ioctl)(tty, file, cmd, arg);
1466 if (retval != -ENOIOCTLCMD)
1467 return retval;
1469 if (tty->ldisc.ioctl) {
1470 int retval = (tty->ldisc.ioctl)(tty, file, cmd, arg);
1471 if (retval != -ENOIOCTLCMD)
1472 return retval;
1474 return -EINVAL;
1479 * This implements the "Secure Attention Key" --- the idea is to
1480 * prevent trojan horses by killing all processes associated with this
1481 * tty when the user hits the "Secure Attention Key". Required for
1482 * super-paranoid applications --- see the Orange Book for more details.
1484 * This code could be nicer; ideally it should send a HUP, wait a few
1485 * seconds, then send a INT, and then a KILL signal. But you then
1486 * have to coordinate with the init process, since all processes associated
1487 * with the current tty must be dead before the new getty is allowed
1488 * to spawn.
1490 void do_SAK( struct tty_struct *tty)
1492 #ifdef TTY_SOFT_SAK
1493 tty_hangup(tty);
1494 #else
1495 struct task_struct **p;
1496 int session;
1497 int i;
1498 struct file *filp;
1500 if (!tty)
1501 return;
1502 session = tty->session;
1503 if (tty->ldisc.flush_buffer)
1504 tty->ldisc.flush_buffer(tty);
1505 if (tty->driver.flush_buffer)
1506 tty->driver.flush_buffer(tty);
1507 for (p = &LAST_TASK ; p > &FIRST_TASK ; --p) {
1508 if (!(*p))
1509 continue;
1510 if (((*p)->tty == tty) ||
1511 ((session > 0) && ((*p)->session == session)))
1512 send_sig(SIGKILL, *p, 1);
1513 else if ((*p)->files) {
1514 for (i=0; i < NR_OPEN; i++) {
1515 filp = (*p)->files->fd[i];
1516 if (filp && (filp->f_op == &tty_fops) &&
1517 (filp->private_data == tty)) {
1518 send_sig(SIGKILL, *p, 1);
1519 break;
1524 #endif
1528 * This routine is called out of the software interrupt to flush data
1529 * from the flip buffer to the line discipline.
1531 static void flush_to_ldisc(void *private_)
1533 struct tty_struct *tty = (struct tty_struct *) private_;
1534 unsigned char *cp;
1535 char *fp;
1536 int count;
1538 if (tty->flip.buf_num) {
1539 cp = tty->flip.char_buf + TTY_FLIPBUF_SIZE;
1540 fp = tty->flip.flag_buf + TTY_FLIPBUF_SIZE;
1541 tty->flip.buf_num = 0;
1543 cli();
1544 tty->flip.char_buf_ptr = tty->flip.char_buf;
1545 tty->flip.flag_buf_ptr = tty->flip.flag_buf;
1546 } else {
1547 cp = tty->flip.char_buf;
1548 fp = tty->flip.flag_buf;
1549 tty->flip.buf_num = 1;
1551 cli();
1552 tty->flip.char_buf_ptr = tty->flip.char_buf + TTY_FLIPBUF_SIZE;
1553 tty->flip.flag_buf_ptr = tty->flip.flag_buf + TTY_FLIPBUF_SIZE;
1555 count = tty->flip.count;
1556 tty->flip.count = 0;
1557 sti();
1559 #if 0
1560 if (count > tty->max_flip_cnt)
1561 tty->max_flip_cnt = count;
1562 #endif
1563 tty->ldisc.receive_buf(tty, cp, fp, count);
1567 * This subroutine initializes a tty structure.
1569 static void initialize_tty_struct(struct tty_struct *tty)
1571 memset(tty, 0, sizeof(struct tty_struct));
1572 tty->magic = TTY_MAGIC;
1573 tty->ldisc = ldiscs[N_TTY];
1574 tty->pgrp = -1;
1575 tty->flip.char_buf_ptr = tty->flip.char_buf;
1576 tty->flip.flag_buf_ptr = tty->flip.flag_buf;
1577 tty->flip.tqueue.routine = flush_to_ldisc;
1578 tty->flip.tqueue.data = tty;
1582 * The default put_char routine if the driver did not define one.
1584 void tty_default_put_char(struct tty_struct *tty, unsigned char ch)
1586 tty->driver.write(tty, 0, &ch, 1);
1590 * Called by a tty driver to register itself.
1592 int tty_register_driver(struct tty_driver *driver)
1594 int error;
1596 if (driver->flags & TTY_DRIVER_INSTALLED)
1597 return 0;
1599 error = register_chrdev(driver->major, driver->name, &tty_fops);
1600 if (error < 0)
1601 return error;
1602 else if(driver->major == 0)
1603 driver->major = error;
1605 if (!driver->put_char)
1606 driver->put_char = tty_default_put_char;
1608 driver->prev = 0;
1609 driver->next = tty_drivers;
1610 if (tty_drivers) tty_drivers->prev = driver;
1611 tty_drivers = driver;
1613 #ifdef CONFIG_PROC_FS
1614 proc_tty_register_driver(driver);
1615 #endif
1616 return error;
1620 * Called by a tty driver to unregister itself.
1622 int tty_unregister_driver(struct tty_driver *driver)
1624 int retval;
1625 struct tty_driver *p;
1626 int found = 0;
1627 const char *othername = NULL;
1629 if (*driver->refcount)
1630 return -EBUSY;
1632 for (p = tty_drivers; p; p = p->next) {
1633 if (p == driver)
1634 found++;
1635 else if (p->major == driver->major)
1636 othername = p->name;
1639 if (!found)
1640 return -ENOENT;
1642 if (othername == NULL) {
1643 retval = unregister_chrdev(driver->major, driver->name);
1644 if (retval)
1645 return retval;
1646 } else
1647 register_chrdev(driver->major, othername, &tty_fops);
1649 if (driver->prev)
1650 driver->prev->next = driver->next;
1651 else
1652 tty_drivers = driver->next;
1654 if (driver->next)
1655 driver->next->prev = driver->prev;
1657 #ifdef CONFIG_PROC_FS
1658 proc_tty_unregister_driver(driver);
1659 #endif
1660 return 0;
1665 * Initialize the console device. This is called *early*, so
1666 * we can't necessarily depend on lots of kernel help here.
1667 * Just do some early initializations, and do the complex setup
1668 * later.
1670 long console_init(long kmem_start, long kmem_end)
1672 /* Setup the default TTY line discipline. */
1673 memset(ldiscs, 0, sizeof(ldiscs));
1674 (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);
1677 * Set up the standard termios. Individual tty drivers may
1678 * deviate from this; this is used as a template.
1680 memset(&tty_std_termios, 0, sizeof(struct termios));
1681 memcpy(tty_std_termios.c_cc, INIT_C_CC, NCCS);
1682 tty_std_termios.c_iflag = ICRNL | IXON;
1683 tty_std_termios.c_oflag = OPOST | ONLCR;
1684 tty_std_termios.c_cflag = B38400 | CS8 | CREAD | HUPCL;
1685 tty_std_termios.c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK |
1686 ECHOCTL | ECHOKE | IEXTEN;
1689 * set up the console device so that later boot sequences can
1690 * inform about problems etc..
1692 #ifdef CONFIG_SERIAL_CONSOLE
1693 kmem_start = serial_console_init(kmem_start, kmem_end);
1694 #endif
1695 #ifdef CONFIG_VT
1696 kmem_start = con_init(kmem_start);
1697 #endif
1698 return kmem_start;
1701 static struct tty_driver dev_tty_driver, dev_console_driver;
1704 * Ok, now we can initialize the rest of the tty devices and can count
1705 * on memory allocations, interrupts etc..
1707 int tty_init(void)
1709 if (sizeof(struct tty_struct) > PAGE_SIZE)
1710 panic("size of tty structure > PAGE_SIZE!");
1713 * dev_tty_driver and dev_console_driver are actually magic
1714 * devices which get redirected at open time. Nevertheless,
1715 * we register them so that register_chrdev is called
1716 * appropriately.
1718 memset(&dev_tty_driver, 0, sizeof(struct tty_driver));
1719 dev_tty_driver.magic = TTY_DRIVER_MAGIC;
1720 dev_tty_driver.driver_name = "/dev/tty";
1721 dev_tty_driver.name = dev_tty_driver.driver_name + 5;
1722 dev_tty_driver.name_base = 0;
1723 dev_tty_driver.major = TTYAUX_MAJOR;
1724 dev_tty_driver.minor_start = 0;
1725 dev_tty_driver.num = 1;
1726 dev_tty_driver.type = TTY_DRIVER_TYPE_SYSTEM;
1727 dev_tty_driver.subtype = SYSTEM_TYPE_TTY;
1729 if (tty_register_driver(&dev_tty_driver))
1730 panic("Couldn't register /dev/tty driver\n");
1732 dev_console_driver = dev_tty_driver;
1733 dev_console_driver.driver_name = "/dev/console";
1734 dev_console_driver.name = dev_console_driver.driver_name + 5;
1735 dev_console_driver.major = TTY_MAJOR;
1736 dev_console_driver.type = TTY_DRIVER_TYPE_SYSTEM;
1737 dev_console_driver.subtype = SYSTEM_TYPE_CONSOLE;
1739 if (tty_register_driver(&dev_console_driver))
1740 panic("Couldn't register /dev/console driver\n");
1742 #ifdef CONFIG_VT
1743 kbd_init();
1744 #endif
1745 #ifdef CONFIG_ESPSERIAL /* init ESP before rs, so rs doesn't see the port */
1746 espserial_init();
1747 #endif
1748 #ifdef CONFIG_SERIAL
1749 rs_init();
1750 #endif
1751 #ifdef CONFIG_CYCLADES
1752 cy_init();
1753 #endif
1754 #ifdef CONFIG_STALLION
1755 stl_init();
1756 #endif
1757 #ifdef CONFIG_ISTALLION
1758 stli_init();
1759 #endif
1760 #ifdef CONFIG_DIGI
1761 pcxe_init();
1762 #endif
1763 #ifdef CONFIG_RISCOM8
1764 riscom8_init();
1765 #endif
1766 pty_init();
1767 #ifdef CONFIG_VT
1768 vcs_init();
1769 #endif
1770 return 0;