Linux 2.2.0pre4
[davej-history.git] / drivers / char / tty_io.c
blob007dd9048907b28599f9207f36070f017932e083
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 appropriate files,
49 * implement CONFIG_VT and generalize console device interface.
50 * -- Marko Kohtala <Marko.Kohtala@hut.fi>, March 97
52 * Rewrote init_dev and release_dev to eliminate races.
53 * -- Bill Hawes <whawes@star.net>, June 97
55 * Added support for a Unix98-style ptmx device.
56 * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998
59 #include <linux/config.h>
60 #include <linux/types.h>
61 #include <linux/major.h>
62 #include <linux/errno.h>
63 #include <linux/signal.h>
64 #include <linux/fcntl.h>
65 #include <linux/sched.h>
66 #include <linux/interrupt.h>
67 #include <linux/tty.h>
68 #include <linux/tty_driver.h>
69 #include <linux/tty_flip.h>
70 #include <linux/devpts_fs.h>
71 #include <linux/file.h>
72 #include <linux/console.h>
73 #include <linux/timer.h>
74 #include <linux/ctype.h>
75 #include <linux/kd.h>
76 #include <linux/mm.h>
77 #include <linux/string.h>
78 #include <linux/malloc.h>
79 #include <linux/poll.h>
80 #include <linux/proc_fs.h>
81 #include <linux/init.h>
82 #include <linux/smp_lock.h>
84 #include <asm/uaccess.h>
85 #include <asm/system.h>
86 #include <asm/bitops.h>
88 #include <linux/kbd_kern.h>
89 #include <linux/vt_kern.h>
90 #include <linux/selection.h>
92 #ifdef CONFIG_KMOD
93 #include <linux/kmod.h>
94 #endif
96 #define CONSOLE_DEV MKDEV(TTY_MAJOR,0)
97 #define TTY_DEV MKDEV(TTYAUX_MAJOR,0)
98 #define SYSCONS_DEV MKDEV(TTYAUX_MAJOR,1)
99 #define PTMX_DEV MKDEV(TTYAUX_MAJOR,2)
101 #undef TTY_DEBUG_HANGUP
103 #define TTY_PARANOIA_CHECK 1
104 #define CHECK_TTY_COUNT 1
106 struct termios tty_std_termios; /* for the benefit of tty drivers */
107 struct tty_driver *tty_drivers = NULL; /* linked list of tty drivers */
108 struct tty_ldisc ldiscs[NR_LDISCS]; /* line disc dispatch table */
110 #ifdef CONFIG_UNIX98_PTYS
111 extern struct tty_driver ptm_driver[]; /* Unix98 pty masters; for /dev/ptmx */
112 #endif
115 * redirect is the pseudo-tty that console output
116 * is redirected to if asked by TIOCCONS.
118 struct tty_struct * redirect = NULL;
120 static void initialize_tty_struct(struct tty_struct *tty);
122 static ssize_t tty_read(struct file *, char *, size_t, loff_t *);
123 static ssize_t tty_write(struct file *, const char *, size_t, loff_t *);
124 static unsigned int tty_poll(struct file *, poll_table *);
125 static int tty_open(struct inode *, struct file *);
126 static int tty_release(struct inode *, struct file *);
127 int tty_ioctl(struct inode * inode, struct file * file,
128 unsigned int cmd, unsigned long arg);
129 static int tty_fasync(int fd, struct file * filp, int on);
130 #ifdef CONFIG_8xx
131 extern long console_8xx_init(long, long);
132 extern int rs_8xx_init(void);
133 #endif /* CONFIG_8xx */
135 #ifndef MIN
136 #define MIN(a,b) ((a) < (b) ? (a) : (b))
137 #endif
140 * This routine returns the name of tty.
142 #define TTY_NUMBER(tty) (MINOR((tty)->device) - (tty)->driver.minor_start + \
143 (tty)->driver.name_base)
145 char *tty_name(struct tty_struct *tty, char *buf)
147 if (tty)
148 sprintf(buf, "%s%d", tty->driver.name, TTY_NUMBER(tty));
149 else
150 strcpy(buf, "NULL tty");
151 return buf;
154 inline int tty_paranoia_check(struct tty_struct *tty, kdev_t device,
155 const char *routine)
157 #ifdef TTY_PARANOIA_CHECK
158 static const char *badmagic =
159 "Warning: bad magic number for tty struct (%s) in %s\n";
160 static const char *badtty =
161 "Warning: null TTY for (%s) in %s\n";
163 if (!tty) {
164 printk(badtty, kdevname(device), routine);
165 return 1;
167 if (tty->magic != TTY_MAGIC) {
168 printk(badmagic, kdevname(device), routine);
169 return 1;
171 #endif
172 return 0;
175 static int check_tty_count(struct tty_struct *tty, const char *routine)
177 #ifdef CHECK_TTY_COUNT
178 struct file *f;
179 int count = 0;
181 for(f = inuse_filps; f; f = f->f_next) {
182 if(f->private_data == tty)
183 count++;
185 if (tty->driver.type == TTY_DRIVER_TYPE_PTY &&
186 tty->driver.subtype == PTY_TYPE_SLAVE &&
187 tty->link && tty->link->count)
188 count++;
189 if (tty->count != count) {
190 printk("Warning: dev (%s) tty->count(%d) != #fd's(%d) in %s\n",
191 kdevname(tty->device), tty->count, count, routine);
192 return count;
194 #endif
195 return 0;
198 int tty_register_ldisc(int disc, struct tty_ldisc *new_ldisc)
200 if (disc < N_TTY || disc >= NR_LDISCS)
201 return -EINVAL;
203 if (new_ldisc) {
204 ldiscs[disc] = *new_ldisc;
205 ldiscs[disc].flags |= LDISC_FLAG_DEFINED;
206 ldiscs[disc].num = disc;
207 } else
208 memset(&ldiscs[disc], 0, sizeof(struct tty_ldisc));
210 return 0;
213 /* Set the discipline of a tty line. */
214 static int tty_set_ldisc(struct tty_struct *tty, int ldisc)
216 int retval = 0;
217 struct tty_ldisc o_ldisc;
218 char buf[64];
220 if ((ldisc < N_TTY) || (ldisc >= NR_LDISCS))
221 return -EINVAL;
222 #ifdef CONFIG_KMOD
223 /* Eduardo Blanco <ejbs@cs.cs.com.uy> */
224 /* Cyrus Durgin <cider@speakeasy.org> */
225 if (!(ldiscs[ldisc].flags & LDISC_FLAG_DEFINED)) {
226 char modname [20];
227 sprintf(modname, "tty-ldisc-%d", ldisc);
228 request_module (modname);
230 #endif
231 if (!(ldiscs[ldisc].flags & LDISC_FLAG_DEFINED))
232 return -EINVAL;
234 if (tty->ldisc.num == ldisc)
235 return 0; /* We are already in the desired discipline */
236 o_ldisc = tty->ldisc;
238 tty_wait_until_sent(tty, 0);
240 /* Shutdown the current discipline. */
241 if (tty->ldisc.close)
242 (tty->ldisc.close)(tty);
244 /* Now set up the new line discipline. */
245 tty->ldisc = ldiscs[ldisc];
246 tty->termios->c_line = ldisc;
247 if (tty->ldisc.open)
248 retval = (tty->ldisc.open)(tty);
249 if (retval < 0) {
250 tty->ldisc = o_ldisc;
251 tty->termios->c_line = tty->ldisc.num;
252 if (tty->ldisc.open && (tty->ldisc.open(tty) < 0)) {
253 tty->ldisc = ldiscs[N_TTY];
254 tty->termios->c_line = N_TTY;
255 if (tty->ldisc.open) {
256 int r = tty->ldisc.open(tty);
258 if (r < 0)
259 panic("Couldn't open N_TTY ldisc for "
260 "%s --- error %d.",
261 tty_name(tty, buf), r);
265 if (tty->ldisc.num != o_ldisc.num && tty->driver.set_ldisc)
266 tty->driver.set_ldisc(tty);
267 return retval;
271 * This routine returns a tty driver structure, given a device number
273 struct tty_driver *get_tty_driver(kdev_t device)
275 int major, minor;
276 struct tty_driver *p;
278 minor = MINOR(device);
279 major = MAJOR(device);
281 for (p = tty_drivers; p; p = p->next) {
282 if (p->major != major)
283 continue;
284 if (minor < p->minor_start)
285 continue;
286 if (minor >= p->minor_start + p->num)
287 continue;
288 return p;
290 return NULL;
294 * If we try to write to, or set the state of, a terminal and we're
295 * not in the foreground, send a SIGTTOU. If the signal is blocked or
296 * ignored, go ahead and perform the operation. (POSIX 7.2)
298 int tty_check_change(struct tty_struct * tty)
300 if (current->tty != tty)
301 return 0;
302 if (tty->pgrp <= 0) {
303 printk("tty_check_change: tty->pgrp <= 0!\n");
304 return 0;
306 if (current->pgrp == tty->pgrp)
307 return 0;
308 if (is_ignored(SIGTTOU))
309 return 0;
310 if (is_orphaned_pgrp(current->pgrp))
311 return -EIO;
312 (void) kill_pg(current->pgrp,SIGTTOU,1);
313 return -ERESTARTSYS;
316 static ssize_t hung_up_tty_read(struct file * file, char * buf,
317 size_t count, loff_t *ppos)
319 /* Can't seek (pread) on ttys. */
320 if (ppos != &file->f_pos)
321 return -ESPIPE;
322 return 0;
325 static ssize_t hung_up_tty_write(struct file * file, const char * buf,
326 size_t count, loff_t *ppos)
328 /* Can't seek (pwrite) on ttys. */
329 if (ppos != &file->f_pos)
330 return -ESPIPE;
331 return -EIO;
334 static unsigned int hung_up_tty_poll(struct file * filp, poll_table * wait)
336 return POLLIN | POLLOUT | POLLERR | POLLHUP | POLLRDNORM | POLLWRNORM;
339 static int hung_up_tty_ioctl(struct inode * inode, struct file * file,
340 unsigned int cmd, unsigned long arg)
342 return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
345 static long long tty_lseek(struct file * file, long long offset, int orig)
347 return -ESPIPE;
350 static struct file_operations tty_fops = {
351 tty_lseek,
352 tty_read,
353 tty_write,
354 NULL, /* tty_readdir */
355 tty_poll,
356 tty_ioctl,
357 NULL, /* tty_mmap */
358 tty_open,
359 NULL, /* flush */
360 tty_release,
361 NULL, /* tty_fsync */
362 tty_fasync
365 static struct file_operations hung_up_tty_fops = {
366 tty_lseek,
367 hung_up_tty_read,
368 hung_up_tty_write,
369 NULL, /* hung_up_tty_readdir */
370 hung_up_tty_poll,
371 hung_up_tty_ioctl,
372 NULL, /* hung_up_tty_mmap */
373 NULL, /* hung_up_tty_open */
374 NULL, /* flush */
375 tty_release, /* hung_up_tty_release */
376 NULL, /* hung_up_tty_fsync */
377 NULL /* hung_up_tty_fasync */
381 * This can be called through the "tq_scheduler"
382 * task-list. That is process synchronous, but
383 * doesn't hold any locks, so we need to make
384 * sure we have the appropriate locks for what
385 * we're doing..
387 void do_tty_hangup(void *data)
389 struct tty_struct *tty = (struct tty_struct *) data;
390 struct file * filp;
391 struct file * cons_filp = NULL;
392 struct task_struct *p;
393 int closecount = 0, n;
395 if (!tty)
396 return;
398 /* inuse_filps is protected by the single kernel lock */
399 lock_kernel();
401 check_tty_count(tty, "do_tty_hangup");
402 for (filp = inuse_filps; filp; filp = filp->f_next) {
403 if (filp->private_data != tty)
404 continue;
405 if (!filp->f_dentry)
406 continue;
407 if (!filp->f_dentry->d_inode)
408 continue;
409 if (filp->f_dentry->d_inode->i_rdev == CONSOLE_DEV ||
410 filp->f_dentry->d_inode->i_rdev == SYSCONS_DEV) {
411 cons_filp = filp;
412 continue;
414 if (filp->f_op != &tty_fops)
415 continue;
416 closecount++;
417 tty_fasync(-1, filp, 0);
418 filp->f_op = &hung_up_tty_fops;
421 /* FIXME! What are the locking issues here? This may me overdoing things.. */
423 unsigned long flags;
425 save_flags(flags); cli();
426 if (tty->ldisc.flush_buffer)
427 tty->ldisc.flush_buffer(tty);
428 if (tty->driver.flush_buffer)
429 tty->driver.flush_buffer(tty);
430 if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
431 tty->ldisc.write_wakeup)
432 (tty->ldisc.write_wakeup)(tty);
433 restore_flags(flags);
436 wake_up_interruptible(&tty->write_wait);
437 wake_up_interruptible(&tty->read_wait);
440 * Shutdown the current line discipline, and reset it to
441 * N_TTY.
443 if (tty->driver.flags & TTY_DRIVER_RESET_TERMIOS)
444 *tty->termios = tty->driver.init_termios;
445 if (tty->ldisc.num != ldiscs[N_TTY].num) {
446 if (tty->ldisc.close)
447 (tty->ldisc.close)(tty);
448 tty->ldisc = ldiscs[N_TTY];
449 tty->termios->c_line = N_TTY;
450 if (tty->ldisc.open) {
451 int i = (tty->ldisc.open)(tty);
452 if (i < 0)
453 printk("do_tty_hangup: N_TTY open: error %d\n",
454 -i);
458 read_lock(&tasklist_lock);
459 for_each_task(p) {
460 if ((tty->session > 0) && (p->session == tty->session) &&
461 p->leader) {
462 send_sig(SIGHUP,p,1);
463 send_sig(SIGCONT,p,1);
464 if (tty->pgrp > 0)
465 p->tty_old_pgrp = tty->pgrp;
467 if (p->tty == tty)
468 p->tty = NULL;
470 read_unlock(&tasklist_lock);
472 tty->flags = 0;
473 tty->session = 0;
474 tty->pgrp = -1;
475 tty->ctrl_status = 0;
477 * If one of the devices matches a console pointer, we
478 * cannot just call hangup() because that will cause
479 * tty->count and state->count to go out of sync.
480 * So we just call close() the right number of times.
482 if (cons_filp) {
483 if (tty->driver.close)
484 for (n = 0; n < closecount; n++)
485 tty->driver.close(tty, cons_filp);
486 } else if (tty->driver.hangup)
487 (tty->driver.hangup)(tty);
488 unlock_kernel();
491 void tty_hangup(struct tty_struct * tty)
493 #ifdef TTY_DEBUG_HANGUP
494 char buf[64];
496 printk("%s hangup...\n", tty_name(tty, buf));
497 #endif
498 queue_task(&tty->tq_hangup, &tq_scheduler);
501 void tty_vhangup(struct tty_struct * tty)
503 #ifdef TTY_DEBUG_HANGUP
504 char buf[64];
506 printk("%s vhangup...\n", tty_name(tty, buf));
507 #endif
508 do_tty_hangup((void *) tty);
511 int tty_hung_up_p(struct file * filp)
513 return (filp->f_op == &hung_up_tty_fops);
517 * This function is typically called only by the session leader, when
518 * it wants to disassociate itself from its controlling tty.
520 * It performs the following functions:
521 * (1) Sends a SIGHUP and SIGCONT to the foreground process group
522 * (2) Clears the tty from being controlling the session
523 * (3) Clears the controlling tty for all processes in the
524 * session group.
526 * The argument on_exit is set to 1 if called when a process is
527 * exiting; it is 0 if called by the ioctl TIOCNOTTY.
529 void disassociate_ctty(int on_exit)
531 struct tty_struct *tty = current->tty;
532 struct task_struct *p;
533 int tty_pgrp = -1;
535 if (tty) {
536 tty_pgrp = tty->pgrp;
537 if (on_exit && tty->driver.type != TTY_DRIVER_TYPE_PTY)
538 tty_vhangup(tty);
539 } else {
540 if (current->tty_old_pgrp) {
541 kill_pg(current->tty_old_pgrp, SIGHUP, on_exit);
542 kill_pg(current->tty_old_pgrp, SIGCONT, on_exit);
544 return;
546 if (tty_pgrp > 0) {
547 kill_pg(tty_pgrp, SIGHUP, on_exit);
548 if (!on_exit)
549 kill_pg(tty_pgrp, SIGCONT, on_exit);
552 current->tty_old_pgrp = 0;
553 tty->session = 0;
554 tty->pgrp = -1;
556 read_lock(&tasklist_lock);
557 for_each_task(p)
558 if (p->session == current->session)
559 p->tty = NULL;
560 read_unlock(&tasklist_lock);
563 void wait_for_keypress(void)
565 struct console *c = console_drivers;
566 if (c) c->wait_key(c);
569 void stop_tty(struct tty_struct *tty)
571 if (tty->stopped)
572 return;
573 tty->stopped = 1;
574 if (tty->link && tty->link->packet) {
575 tty->ctrl_status &= ~TIOCPKT_START;
576 tty->ctrl_status |= TIOCPKT_STOP;
577 wake_up_interruptible(&tty->link->read_wait);
579 if (tty->driver.stop)
580 (tty->driver.stop)(tty);
583 void start_tty(struct tty_struct *tty)
585 if (!tty->stopped || tty->flow_stopped)
586 return;
587 tty->stopped = 0;
588 if (tty->link && tty->link->packet) {
589 tty->ctrl_status &= ~TIOCPKT_STOP;
590 tty->ctrl_status |= TIOCPKT_START;
591 wake_up_interruptible(&tty->link->read_wait);
593 if (tty->driver.start)
594 (tty->driver.start)(tty);
595 if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
596 tty->ldisc.write_wakeup)
597 (tty->ldisc.write_wakeup)(tty);
598 wake_up_interruptible(&tty->write_wait);
601 static ssize_t tty_read(struct file * file, char * buf, size_t count,
602 loff_t *ppos)
604 int i;
605 struct tty_struct * tty;
606 struct inode *inode;
608 /* Can't seek (pread) on ttys. */
609 if (ppos != &file->f_pos)
610 return -ESPIPE;
612 tty = (struct tty_struct *)file->private_data;
613 inode = file->f_dentry->d_inode;
614 if (tty_paranoia_check(tty, inode->i_rdev, "tty_read"))
615 return -EIO;
616 if (!tty || (test_bit(TTY_IO_ERROR, &tty->flags)))
617 return -EIO;
619 /* This check not only needs to be done before reading, but also
620 whenever read_chan() gets woken up after sleeping, so I've
621 moved it to there. This should only be done for the N_TTY
622 line discipline, anyway. Same goes for write_chan(). -- jlc. */
623 #if 0
624 if ((inode->i_rdev != CONSOLE_DEV) && /* don't stop on /dev/console */
625 (tty->pgrp > 0) &&
626 (current->tty == tty) &&
627 (tty->pgrp != current->pgrp))
628 if (is_ignored(SIGTTIN) || is_orphaned_pgrp(current->pgrp))
629 return -EIO;
630 else {
631 (void) kill_pg(current->pgrp, SIGTTIN, 1);
632 return -ERESTARTSYS;
634 #endif
635 if (tty->ldisc.read)
636 i = (tty->ldisc.read)(tty,file,buf,count);
637 else
638 i = -EIO;
639 if (i > 0)
640 inode->i_atime = CURRENT_TIME;
641 return i;
645 * Split writes up in sane blocksizes to avoid
646 * denial-of-service type attacks
648 static inline ssize_t do_tty_write(
649 ssize_t (*write)(struct tty_struct *, struct file *, const unsigned char *, size_t),
650 struct tty_struct *tty,
651 struct file *file,
652 const unsigned char *buf,
653 size_t count)
655 ssize_t ret = 0, written = 0;
656 struct inode *inode = file->f_dentry->d_inode;
658 up(&inode->i_sem);
659 if (down_interruptible(&inode->i_atomic_write)) {
660 down(&inode->i_sem);
661 return -ERESTARTSYS;
663 for (;;) {
664 unsigned long size = PAGE_SIZE*2;
665 if (size > count)
666 size = count;
667 ret = write(tty, file, buf, size);
668 if (ret <= 0)
669 break;
670 written += ret;
671 buf += ret;
672 count -= ret;
673 if (!count)
674 break;
675 ret = -ERESTARTSYS;
676 if (signal_pending(current))
677 break;
678 if (current->need_resched)
679 schedule();
681 if (written) {
682 file->f_dentry->d_inode->i_mtime = CURRENT_TIME;
683 ret = written;
685 up(&inode->i_atomic_write);
686 down(&inode->i_sem);
687 return ret;
691 static ssize_t tty_write(struct file * file, const char * buf, size_t count,
692 loff_t *ppos)
694 int is_console;
695 struct tty_struct * tty;
696 struct inode *inode;
698 /* Can't seek (pwrite) on ttys. */
699 if (ppos != &file->f_pos)
700 return -ESPIPE;
703 * For now, we redirect writes from /dev/console as
704 * well as /dev/tty0.
706 inode = file->f_dentry->d_inode;
707 is_console = (inode->i_rdev == SYSCONS_DEV ||
708 inode->i_rdev == CONSOLE_DEV);
710 if (is_console && redirect)
711 tty = redirect;
712 else
713 tty = (struct tty_struct *)file->private_data;
714 if (tty_paranoia_check(tty, inode->i_rdev, "tty_write"))
715 return -EIO;
716 if (!tty || !tty->driver.write || (test_bit(TTY_IO_ERROR, &tty->flags)))
717 return -EIO;
718 #if 0
719 if (!is_console && L_TOSTOP(tty) && (tty->pgrp > 0) &&
720 (current->tty == tty) && (tty->pgrp != current->pgrp)) {
721 if (is_orphaned_pgrp(current->pgrp))
722 return -EIO;
723 if (!is_ignored(SIGTTOU)) {
724 (void) kill_pg(current->pgrp, SIGTTOU, 1);
725 return -ERESTARTSYS;
728 #endif
729 if (!tty->ldisc.write)
730 return -EIO;
731 return do_tty_write(tty->ldisc.write, tty, file,
732 (const unsigned char *)buf, count);
735 /* Semaphore to protect creating and releasing a tty */
736 static struct semaphore tty_sem = MUTEX;
738 static void down_tty_sem(int index)
740 down(&tty_sem);
743 static void up_tty_sem(int index)
745 up(&tty_sem);
748 static void release_mem(struct tty_struct *tty, int idx);
751 * WSH 06/09/97: Rewritten to remove races and properly clean up after a
752 * failed open. The new code protects the open with a semaphore, so it's
753 * really quite straightforward. The semaphore locking can probably be
754 * relaxed for the (most common) case of reopening a tty.
756 static int init_dev(kdev_t device, struct tty_struct **ret_tty)
758 struct tty_struct *tty, *o_tty;
759 struct termios *tp, **tp_loc, *o_tp, **o_tp_loc;
760 struct termios *ltp, **ltp_loc, *o_ltp, **o_ltp_loc;
761 struct tty_driver *driver;
762 int retval=0;
763 int idx;
765 driver = get_tty_driver(device);
766 if (!driver)
767 return -ENODEV;
769 idx = MINOR(device) - driver->minor_start;
772 * Check whether we need to acquire the tty semaphore to avoid
773 * race conditions. For now, play it safe.
775 down_tty_sem(idx);
777 /* check whether we're reopening an existing tty */
778 tty = driver->table[idx];
779 if (tty) goto fast_track;
782 * First time open is complex, especially for PTY devices.
783 * This code guarantees that either everything succeeds and the
784 * TTY is ready for operation, or else the table slots are vacated
785 * and the allocated memory released. (Except that the termios
786 * and locked termios may be retained.)
789 o_tty = NULL;
790 tp = o_tp = NULL;
791 ltp = o_ltp = NULL;
793 tty = (struct tty_struct*) get_free_page(GFP_KERNEL);
794 if(!tty)
795 goto fail_no_mem;
796 initialize_tty_struct(tty);
797 tty->device = device;
798 tty->driver = *driver;
800 tp_loc = &driver->termios[idx];
801 if (!*tp_loc) {
802 tp = (struct termios *) kmalloc(sizeof(struct termios),
803 GFP_KERNEL);
804 if (!tp)
805 goto free_mem_out;
806 *tp = driver->init_termios;
809 ltp_loc = &driver->termios_locked[idx];
810 if (!*ltp_loc) {
811 ltp = (struct termios *) kmalloc(sizeof(struct termios),
812 GFP_KERNEL);
813 if (!ltp)
814 goto free_mem_out;
815 memset(ltp, 0, sizeof(struct termios));
818 if (driver->type == TTY_DRIVER_TYPE_PTY) {
819 o_tty = (struct tty_struct *) get_free_page(GFP_KERNEL);
820 if (!o_tty)
821 goto free_mem_out;
822 initialize_tty_struct(o_tty);
823 o_tty->device = (kdev_t) MKDEV(driver->other->major,
824 driver->other->minor_start + idx);
825 o_tty->driver = *driver->other;
827 o_tp_loc = &driver->other->termios[idx];
828 if (!*o_tp_loc) {
829 o_tp = (struct termios *)
830 kmalloc(sizeof(struct termios), GFP_KERNEL);
831 if (!o_tp)
832 goto free_mem_out;
833 *o_tp = driver->other->init_termios;
836 o_ltp_loc = &driver->other->termios_locked[idx];
837 if (!*o_ltp_loc) {
838 o_ltp = (struct termios *)
839 kmalloc(sizeof(struct termios), GFP_KERNEL);
840 if (!o_ltp)
841 goto free_mem_out;
842 memset(o_ltp, 0, sizeof(struct termios));
846 * Everything allocated ... set up the o_tty structure.
848 driver->other->table[idx] = o_tty;
849 if (!*o_tp_loc)
850 *o_tp_loc = o_tp;
851 if (!*o_ltp_loc)
852 *o_ltp_loc = o_ltp;
853 o_tty->termios = *o_tp_loc;
854 o_tty->termios_locked = *o_ltp_loc;
855 (*driver->other->refcount)++;
856 if (driver->subtype == PTY_TYPE_MASTER)
857 o_tty->count++;
859 /* Establish the links in both directions */
860 tty->link = o_tty;
861 o_tty->link = tty;
865 * All structures have been allocated, so now we install them.
866 * Failures after this point use release_mem to clean up, so
867 * there's no need to null out the local pointers.
869 driver->table[idx] = tty; /* FIXME: this is broken and
870 probably causes ^D bug. tty->private_date does not (yet) point
871 to a console, if keypress comes now, await armagedon.
873 also, driver->table is accessed from interrupt for vt case,
874 and this does not look like atomic access at all. */
876 if (!*tp_loc)
877 *tp_loc = tp;
878 if (!*ltp_loc)
879 *ltp_loc = ltp;
880 tty->termios = *tp_loc;
881 tty->termios_locked = *ltp_loc;
882 (*driver->refcount)++;
883 tty->count++;
886 * Structures all installed ... call the ldisc open routines.
887 * If we fail here just call release_mem to clean up. No need
888 * to decrement the use counts, as release_mem doesn't care.
890 if (tty->ldisc.open) {
891 retval = (tty->ldisc.open)(tty);
892 if (retval)
893 goto release_mem_out;
895 if (o_tty && o_tty->ldisc.open) {
896 retval = (o_tty->ldisc.open)(o_tty);
897 if (retval) {
898 if (tty->ldisc.close)
899 (tty->ldisc.close)(tty);
900 goto release_mem_out;
903 goto success;
906 * This fast open can be used if the tty is already open.
907 * No memory is allocated, and the only failures are from
908 * attempting to open a closing tty or attempting multiple
909 * opens on a pty master.
911 fast_track:
912 if (test_bit(TTY_CLOSING, &tty->flags)) {
913 retval = -EIO;
914 goto end_init;
916 if (driver->type == TTY_DRIVER_TYPE_PTY &&
917 driver->subtype == PTY_TYPE_MASTER) {
919 * special case for PTY masters: only one open permitted,
920 * and the slave side open count is incremented as well.
922 if (tty->count) {
923 retval = -EIO;
924 goto end_init;
926 tty->link->count++;
928 tty->count++;
929 tty->driver = *driver; /* N.B. why do this every time?? */
931 success:
932 *ret_tty = tty;
934 /* All paths come through here to release the semaphore */
935 end_init:
936 up_tty_sem(idx);
937 return retval;
939 /* Release locally allocated memory ... nothing placed in slots */
940 free_mem_out:
941 if (o_tp)
942 kfree_s(o_tp, sizeof(struct termios));
943 if (o_tty)
944 free_page((unsigned long) o_tty);
945 if (ltp)
946 kfree_s(ltp, sizeof(struct termios));
947 if (tp)
948 kfree_s(tp, sizeof(struct termios));
949 free_page((unsigned long) tty);
951 fail_no_mem:
952 retval = -ENOMEM;
953 goto end_init;
955 /* call the tty release_mem routine to clean out this slot */
956 release_mem_out:
957 printk("init_dev: ldisc open failed, clearing slot %d\n", idx);
958 release_mem(tty, idx);
959 goto end_init;
963 * Releases memory associated with a tty structure, and clears out the
964 * driver table slots.
966 static void release_mem(struct tty_struct *tty, int idx)
968 struct tty_struct *o_tty;
969 struct termios *tp;
971 if ((o_tty = tty->link) != NULL) {
972 o_tty->driver.table[idx] = NULL;
973 if (o_tty->driver.flags & TTY_DRIVER_RESET_TERMIOS) {
974 tp = o_tty->driver.termios[idx];
975 o_tty->driver.termios[idx] = NULL;
976 kfree_s(tp, sizeof(struct termios));
978 o_tty->magic = 0;
979 (*o_tty->driver.refcount)--;
980 free_page((unsigned long) o_tty);
983 tty->driver.table[idx] = NULL;
984 if (tty->driver.flags & TTY_DRIVER_RESET_TERMIOS) {
985 tp = tty->driver.termios[idx];
986 tty->driver.termios[idx] = NULL;
987 kfree_s(tp, sizeof(struct termios));
989 tty->magic = 0;
990 (*tty->driver.refcount)--;
991 free_page((unsigned long) tty);
995 * Even releasing the tty structures is a tricky business.. We have
996 * to be very careful that the structures are all released at the
997 * same time, as interrupts might otherwise get the wrong pointers.
999 * WSH 09/09/97: rewritten to avoid some nasty race conditions that could
1000 * lead to double frees or releasing memory still in use.
1002 static void release_dev(struct file * filp)
1004 struct tty_struct *tty, *o_tty;
1005 int pty_master, tty_closing, o_tty_closing, do_sleep;
1006 int idx;
1007 char buf[64];
1009 tty = (struct tty_struct *)filp->private_data;
1010 if (tty_paranoia_check(tty, filp->f_dentry->d_inode->i_rdev, "release_dev"))
1011 return;
1013 check_tty_count(tty, "release_dev");
1015 tty_fasync(-1, filp, 0);
1017 idx = MINOR(tty->device) - tty->driver.minor_start;
1018 pty_master = (tty->driver.type == TTY_DRIVER_TYPE_PTY &&
1019 tty->driver.subtype == PTY_TYPE_MASTER);
1020 o_tty = tty->link;
1022 #ifdef TTY_PARANOIA_CHECK
1023 if (idx < 0 || idx >= tty->driver.num) {
1024 printk("release_dev: bad idx when trying to free (%s)\n",
1025 kdevname(tty->device));
1026 return;
1028 if (tty != tty->driver.table[idx]) {
1029 printk("release_dev: driver.table[%d] not tty for (%s)\n",
1030 idx, kdevname(tty->device));
1031 return;
1033 if (tty->termios != tty->driver.termios[idx]) {
1034 printk("release_dev: driver.termios[%d] not termios "
1035 "for (%s)\n",
1036 idx, kdevname(tty->device));
1037 return;
1039 if (tty->termios_locked != tty->driver.termios_locked[idx]) {
1040 printk("release_dev: driver.termios_locked[%d] not "
1041 "termios_locked for (%s)\n",
1042 idx, kdevname(tty->device));
1043 return;
1045 #endif
1047 #ifdef TTY_DEBUG_HANGUP
1048 printk("release_dev of %s (tty count=%d)...", tty_name(tty, buf),
1049 tty->count);
1050 #endif
1052 #ifdef TTY_PARANOIA_CHECK
1053 if (tty->driver.other) {
1054 if (o_tty != tty->driver.other->table[idx]) {
1055 printk("release_dev: other->table[%d] not o_tty for ("
1056 "%s)\n",
1057 idx, kdevname(tty->device));
1058 return;
1060 if (o_tty->termios != tty->driver.other->termios[idx]) {
1061 printk("release_dev: other->termios[%d] not o_termios "
1062 "for (%s)\n",
1063 idx, kdevname(tty->device));
1064 return;
1066 if (o_tty->termios_locked !=
1067 tty->driver.other->termios_locked[idx]) {
1068 printk("release_dev: other->termios_locked[%d] not "
1069 "o_termios_locked for (%s)\n",
1070 idx, kdevname(tty->device));
1071 return;
1073 if (o_tty->link != tty) {
1074 printk("release_dev: bad pty pointers\n");
1075 return;
1078 #endif
1080 if (tty->driver.close)
1081 tty->driver.close(tty, filp);
1084 * Sanity check: if tty->count is going to zero, there shouldn't be
1085 * any waiters on tty->read_wait or tty->write_wait. We test the
1086 * wait queues and kick everyone out _before_ actually starting to
1087 * close. This ensures that we won't block while releasing the tty
1088 * structure.
1090 * The test for the o_tty closing is necessary, since the master and
1091 * slave sides may close in any order. If the slave side closes out
1092 * first, its count will be one, since the master side holds an open.
1093 * Thus this test wouldn't be triggered at the time the slave closes,
1094 * so we do it now.
1096 * Note that it's possible for the tty to be opened again while we're
1097 * flushing out waiters. By recalculating the closing flags before
1098 * each iteration we avoid any problems.
1100 while (1) {
1101 tty_closing = tty->count <= 1;
1102 o_tty_closing = o_tty &&
1103 (o_tty->count <= (pty_master ? 1 : 0));
1104 do_sleep = 0;
1106 if (tty_closing) {
1107 if (waitqueue_active(&tty->read_wait)) {
1108 wake_up(&tty->read_wait);
1109 do_sleep++;
1111 if (waitqueue_active(&tty->write_wait)) {
1112 wake_up(&tty->write_wait);
1113 do_sleep++;
1116 if (o_tty_closing) {
1117 if (waitqueue_active(&o_tty->read_wait)) {
1118 wake_up(&o_tty->read_wait);
1119 do_sleep++;
1121 if (waitqueue_active(&o_tty->write_wait)) {
1122 wake_up(&o_tty->write_wait);
1123 do_sleep++;
1126 if (!do_sleep)
1127 break;
1129 printk("release_dev: %s: read/write wait queue active!\n",
1130 tty_name(tty, buf));
1131 schedule();
1135 * The closing flags are now consistent with the open counts on
1136 * both sides, and we've completed the last operation that could
1137 * block, so it's safe to proceed with closing.
1139 if (pty_master) {
1140 if (--o_tty->count < 0) {
1141 printk("release_dev: bad pty slave count (%d) for %s\n",
1142 o_tty->count, tty_name(o_tty, buf));
1143 o_tty->count = 0;
1146 if (--tty->count < 0) {
1147 printk("release_dev: bad tty->count (%d) for %s\n",
1148 tty->count, tty_name(tty, buf));
1149 tty->count = 0;
1153 * We've decremented tty->count, so we should zero out
1154 * filp->private_data, to break the link between the tty and
1155 * the file descriptor. Otherwise if close_fp() blocks before
1156 * the the file descriptor is removed from the inuse_filp
1157 * list, check_tty_count() could observe a discrepancy and
1158 * printk a warning message to the user.
1160 filp->private_data = 0;
1163 * Perform some housekeeping before deciding whether to return.
1165 * Set the TTY_CLOSING flag if this was the last open. In the
1166 * case of a pty we may have to wait around for the other side
1167 * to close, and TTY_CLOSING makes sure we can't be reopened.
1169 if(tty_closing)
1170 set_bit(TTY_CLOSING, &tty->flags);
1171 if(o_tty_closing)
1172 set_bit(TTY_CLOSING, &o_tty->flags);
1175 * If _either_ side is closing, make sure there aren't any
1176 * processes that still think tty or o_tty is their controlling
1177 * tty. Also, clear redirect if it points to either tty.
1179 if (tty_closing || o_tty_closing) {
1180 struct task_struct *p;
1182 read_lock(&tasklist_lock);
1183 for_each_task(p) {
1184 if (p->tty == tty || (o_tty && p->tty == o_tty))
1185 p->tty = NULL;
1187 read_unlock(&tasklist_lock);
1189 if (redirect == tty || (o_tty && redirect == o_tty))
1190 redirect = NULL;
1193 /* check whether both sides are closing ... */
1194 if (!tty_closing || (o_tty && !o_tty_closing))
1195 return;
1197 #ifdef TTY_DEBUG_HANGUP
1198 printk("freeing tty structure...");
1199 #endif
1202 * Shutdown the current line discipline, and reset it to N_TTY.
1203 * N.B. why reset ldisc when we're releasing the memory??
1205 if (tty->ldisc.close)
1206 (tty->ldisc.close)(tty);
1207 tty->ldisc = ldiscs[N_TTY];
1208 tty->termios->c_line = N_TTY;
1209 if (o_tty) {
1210 if (o_tty->ldisc.close)
1211 (o_tty->ldisc.close)(o_tty);
1212 o_tty->ldisc = ldiscs[N_TTY];
1216 * Make sure that the tty's task queue isn't activated.
1218 run_task_queue(&tq_timer);
1219 run_task_queue(&tq_scheduler);
1222 * The release_mem function takes care of the details of clearing
1223 * the slots and preserving the termios structure.
1225 release_mem(tty, idx);
1229 * tty_open and tty_release keep up the tty count that contains the
1230 * number of opens done on a tty. We cannot use the inode-count, as
1231 * different inodes might point to the same tty.
1233 * Open-counting is needed for pty masters, as well as for keeping
1234 * track of serial lines: DTR is dropped when the last close happens.
1235 * (This is not done solely through tty->count, now. - Ted 1/27/92)
1237 * The termios state of a pty is reset on first open so that
1238 * settings don't persist across reuse.
1240 static int tty_open(struct inode * inode, struct file * filp)
1242 struct tty_struct *tty;
1243 int noctty, retval;
1244 kdev_t device;
1245 unsigned short saved_flags;
1246 char buf[64];
1248 saved_flags = filp->f_flags;
1249 retry_open:
1250 noctty = filp->f_flags & O_NOCTTY;
1251 device = inode->i_rdev;
1252 if (device == TTY_DEV) {
1253 if (!current->tty)
1254 return -ENXIO;
1255 device = current->tty->device;
1256 filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */
1257 /* noctty = 1; */
1259 #ifdef CONFIG_VT
1260 if (device == CONSOLE_DEV) {
1261 extern int fg_console;
1262 device = MKDEV(TTY_MAJOR, fg_console + 1);
1263 noctty = 1;
1265 #endif
1266 if (device == SYSCONS_DEV) {
1267 struct console *c = console_drivers;
1268 while(c && !c->device)
1269 c = c->next;
1270 if (!c)
1271 return -ENODEV;
1272 device = c->device(c);
1273 filp->f_flags |= O_NONBLOCK; /* Don't let /dev/console block */
1274 noctty = 1;
1277 if (device == PTMX_DEV) {
1278 #ifdef CONFIG_UNIX98_PTYS
1280 /* find a free pty. */
1281 int major, minor;
1282 struct tty_driver *driver;
1284 /* find a device that is not in use. */
1285 retval = -1;
1286 for ( major = 0 ; major < UNIX98_NR_MAJORS ; major++ ) {
1287 driver = &ptm_driver[major];
1288 for (minor = driver->minor_start ;
1289 minor < driver->minor_start + driver->num ;
1290 minor++) {
1291 device = MKDEV(driver->major, minor);
1292 if (!init_dev(device, &tty)) goto ptmx_found; /* ok! */
1295 return -EIO; /* no free ptys */
1296 ptmx_found:
1297 set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
1298 minor -= driver->minor_start;
1299 devpts_pty_new(driver->other->name_base + minor, MKDEV(driver->other->major, minor + driver->other->minor_start));
1300 noctty = 1;
1301 goto init_dev_done;
1303 #else /* CONFIG_UNIX_98_PTYS */
1305 return -ENODEV;
1307 #endif /* CONFIG_UNIX_98_PTYS */
1310 retval = init_dev(device, &tty);
1311 if (retval)
1312 return retval;
1314 #ifdef CONFIG_UNIX98_PTYS
1315 init_dev_done:
1316 #endif
1317 filp->private_data = tty;
1318 check_tty_count(tty, "tty_open");
1319 if (tty->driver.type == TTY_DRIVER_TYPE_PTY &&
1320 tty->driver.subtype == PTY_TYPE_MASTER)
1321 noctty = 1;
1322 #ifdef TTY_DEBUG_HANGUP
1323 printk("opening %s...", tty_name(tty, buf));
1324 #endif
1325 if (tty->driver.open)
1326 retval = tty->driver.open(tty, filp);
1327 else
1328 retval = -ENODEV;
1329 filp->f_flags = saved_flags;
1331 if (!retval && test_bit(TTY_EXCLUSIVE, &tty->flags) && !suser())
1332 retval = -EBUSY;
1334 if (retval) {
1335 #ifdef TTY_DEBUG_HANGUP
1336 printk("error %d in opening %s...", retval,
1337 tty_name(tty, buf));
1338 #endif
1340 release_dev(filp);
1341 if (retval != -ERESTARTSYS)
1342 return retval;
1343 if (signal_pending(current))
1344 return retval;
1345 schedule();
1347 * Need to reset f_op in case a hangup happened.
1349 filp->f_op = &tty_fops;
1350 goto retry_open;
1352 if (!noctty &&
1353 current->leader &&
1354 !current->tty &&
1355 tty->session == 0) {
1356 current->tty = tty;
1357 current->tty_old_pgrp = 0;
1358 tty->session = current->session;
1359 tty->pgrp = current->pgrp;
1361 if ((tty->driver.type == TTY_DRIVER_TYPE_SERIAL) &&
1362 (tty->driver.subtype == SERIAL_TYPE_CALLOUT) &&
1363 (tty->count == 1)) {
1364 static int nr_warns = 0;
1365 if (nr_warns < 5) {
1366 printk(KERN_WARNING "tty_io.c: "
1367 "process %d (%s) used obsolete /dev/%s - "
1368 "update software to use /dev/ttyS%d\n",
1369 current->pid, current->comm,
1370 tty_name(tty, buf), TTY_NUMBER(tty));
1371 nr_warns++;
1374 return 0;
1377 static int tty_release(struct inode * inode, struct file * filp)
1379 release_dev(filp);
1380 return 0;
1383 static unsigned int tty_poll(struct file * filp, poll_table * wait)
1385 struct tty_struct * tty;
1387 tty = (struct tty_struct *)filp->private_data;
1388 if (tty_paranoia_check(tty, filp->f_dentry->d_inode->i_rdev, "tty_poll"))
1389 return 0;
1391 if (tty->ldisc.poll)
1392 return (tty->ldisc.poll)(tty, filp, wait);
1393 return 0;
1397 * fasync_helper() is used by some character device drivers (mainly mice)
1398 * to set up the fasync queue. It returns negative on error, 0 if it did
1399 * no changes and positive if it added/deleted the entry.
1401 int fasync_helper(int fd, struct file * filp, int on, struct fasync_struct **fapp)
1403 struct fasync_struct *fa, **fp;
1404 unsigned long flags;
1406 for (fp = fapp; (fa = *fp) != NULL; fp = &fa->fa_next) {
1407 if (fa->fa_file == filp)
1408 break;
1411 if (on) {
1412 if (fa) {
1413 fa->fa_fd = fd;
1414 return 0;
1416 fa = (struct fasync_struct *)kmalloc(sizeof(struct fasync_struct), GFP_KERNEL);
1417 if (!fa)
1418 return -ENOMEM;
1419 fa->magic = FASYNC_MAGIC;
1420 fa->fa_file = filp;
1421 fa->fa_fd = fd;
1422 save_flags(flags);
1423 cli();
1424 fa->fa_next = *fapp;
1425 *fapp = fa;
1426 restore_flags(flags);
1427 return 1;
1429 if (!fa)
1430 return 0;
1431 save_flags(flags);
1432 cli();
1433 *fp = fa->fa_next;
1434 restore_flags(flags);
1435 kfree(fa);
1436 return 1;
1439 static int tty_fasync(int fd, struct file * filp, int on)
1441 struct tty_struct * tty;
1442 int retval;
1444 tty = (struct tty_struct *)filp->private_data;
1445 if (tty_paranoia_check(tty, filp->f_dentry->d_inode->i_rdev, "tty_fasync"))
1446 return 0;
1448 retval = fasync_helper(fd, filp, on, &tty->fasync);
1449 if (retval <= 0)
1450 return retval;
1452 if (on) {
1453 if (!waitqueue_active(&tty->read_wait))
1454 tty->minimum_to_wake = 1;
1455 if (filp->f_owner.pid == 0) {
1456 filp->f_owner.pid = (-tty->pgrp) ? : current->pid;
1457 filp->f_owner.uid = current->uid;
1458 filp->f_owner.euid = current->euid;
1460 } else {
1461 if (!tty->fasync && !waitqueue_active(&tty->read_wait))
1462 tty->minimum_to_wake = N_TTY_BUF_SIZE;
1464 return 0;
1467 static int tiocsti(struct tty_struct *tty, char * arg)
1469 char ch, mbz = 0;
1471 if ((current->tty != tty) && !suser())
1472 return -EPERM;
1473 if (get_user(ch, arg))
1474 return -EFAULT;
1475 tty->ldisc.receive_buf(tty, &ch, &mbz, 1);
1476 return 0;
1479 static int tiocgwinsz(struct tty_struct *tty, struct winsize * arg)
1481 if (copy_to_user(arg, &tty->winsize, sizeof(*arg)))
1482 return -EFAULT;
1483 return 0;
1486 static int tiocswinsz(struct tty_struct *tty, struct tty_struct *real_tty,
1487 struct winsize * arg)
1489 struct winsize tmp_ws;
1491 if (copy_from_user(&tmp_ws, arg, sizeof(*arg)))
1492 return -EFAULT;
1493 if (!memcmp(&tmp_ws, &tty->winsize, sizeof(*arg)))
1494 return 0;
1495 if (tty->pgrp > 0)
1496 kill_pg(tty->pgrp, SIGWINCH, 1);
1497 if ((real_tty->pgrp != tty->pgrp) && (real_tty->pgrp > 0))
1498 kill_pg(real_tty->pgrp, SIGWINCH, 1);
1499 tty->winsize = tmp_ws;
1500 real_tty->winsize = tmp_ws;
1501 return 0;
1504 static int tioccons(struct tty_struct *tty, struct tty_struct *real_tty)
1506 if (tty->driver.type == TTY_DRIVER_TYPE_CONSOLE ||
1507 tty->driver.type == TTY_DRIVER_TYPE_SYSCONS) {
1508 if (!suser())
1509 return -EPERM;
1510 redirect = NULL;
1511 return 0;
1513 if (redirect)
1514 return -EBUSY;
1515 redirect = real_tty;
1516 return 0;
1520 static int fionbio(struct file *file, int *arg)
1522 int nonblock;
1524 if (get_user(nonblock, arg))
1525 return -EFAULT;
1527 if (nonblock)
1528 file->f_flags |= O_NONBLOCK;
1529 else
1530 file->f_flags &= ~O_NONBLOCK;
1531 return 0;
1534 static int tiocsctty(struct tty_struct *tty, int arg)
1536 if (current->leader &&
1537 (current->session == tty->session))
1538 return 0;
1540 * The process must be a session leader and
1541 * not have a controlling tty already.
1543 if (!current->leader || current->tty)
1544 return -EPERM;
1545 if (tty->session > 0) {
1547 * This tty is already the controlling
1548 * tty for another session group!
1550 if ((arg == 1) && suser()) {
1552 * Steal it away
1554 struct task_struct *p;
1556 read_lock(&tasklist_lock);
1557 for_each_task(p)
1558 if (p->tty == tty)
1559 p->tty = NULL;
1560 read_unlock(&tasklist_lock);
1561 } else
1562 return -EPERM;
1564 current->tty = tty;
1565 current->tty_old_pgrp = 0;
1566 tty->session = current->session;
1567 tty->pgrp = current->pgrp;
1568 return 0;
1571 static int tiocgpgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t *arg)
1574 * (tty == real_tty) is a cheap way of
1575 * testing if the tty is NOT a master pty.
1577 if (tty == real_tty && current->tty != real_tty)
1578 return -ENOTTY;
1579 return put_user(real_tty->pgrp, arg);
1582 static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t *arg)
1584 pid_t pgrp;
1585 int retval = tty_check_change(real_tty);
1587 if (retval == -EIO)
1588 return -ENOTTY;
1589 if (retval)
1590 return retval;
1591 if (!current->tty ||
1592 (current->tty != real_tty) ||
1593 (real_tty->session != current->session))
1594 return -ENOTTY;
1595 get_user(pgrp, (pid_t *) arg);
1596 if (pgrp < 0)
1597 return -EINVAL;
1598 if (session_of_pgrp(pgrp) != current->session)
1599 return -EPERM;
1600 real_tty->pgrp = pgrp;
1601 return 0;
1604 static int tiocgsid(struct tty_struct *tty, struct tty_struct *real_tty, pid_t *arg)
1607 * (tty == real_tty) is a cheap way of
1608 * testing if the tty is NOT a master pty.
1610 if (tty == real_tty && current->tty != real_tty)
1611 return -ENOTTY;
1612 if (real_tty->session <= 0)
1613 return -ENOTTY;
1614 return put_user(real_tty->session, arg);
1617 static int tiocttygstruct(struct tty_struct *tty, struct tty_struct *arg)
1619 if (copy_to_user(arg, tty, sizeof(*arg)))
1620 return -EFAULT;
1621 return 0;
1624 static int tiocsetd(struct tty_struct *tty, int *arg)
1626 int retval, ldisc;
1628 retval = get_user(ldisc, arg);
1629 if (retval)
1630 return retval;
1631 return tty_set_ldisc(tty, ldisc);
1634 static int send_break(struct tty_struct *tty, int duration)
1636 current->state = TASK_INTERRUPTIBLE;
1638 tty->driver.break_ctl(tty, -1);
1639 if (!signal_pending(current))
1640 schedule_timeout(duration);
1641 tty->driver.break_ctl(tty, 0);
1642 if (signal_pending(current))
1643 return -EINTR;
1644 return 0;
1648 * Split this up, as gcc can choke on it otherwise..
1650 int tty_ioctl(struct inode * inode, struct file * file,
1651 unsigned int cmd, unsigned long arg)
1653 struct tty_struct *tty, *real_tty;
1654 int retval;
1656 tty = (struct tty_struct *)file->private_data;
1657 if (tty_paranoia_check(tty, inode->i_rdev, "tty_ioctl"))
1658 return -EINVAL;
1660 real_tty = tty;
1661 if (tty->driver.type == TTY_DRIVER_TYPE_PTY &&
1662 tty->driver.subtype == PTY_TYPE_MASTER)
1663 real_tty = tty->link;
1666 * Break handling by driver
1668 if (!tty->driver.break_ctl) {
1669 switch(cmd) {
1670 case TIOCSBRK:
1671 case TIOCCBRK:
1672 if (tty->driver.ioctl)
1673 return tty->driver.ioctl(tty, file, cmd, arg);
1674 return -EINVAL;
1676 /* These two ioctl's always return success; even if */
1677 /* the driver doesn't support them. */
1678 case TCSBRK:
1679 case TCSBRKP:
1680 if (!tty->driver.ioctl)
1681 return 0;
1682 retval = tty->driver.ioctl(tty, file, cmd, arg);
1683 if (retval == -ENOIOCTLCMD)
1684 retval = 0;
1685 return retval;
1690 * Factor out some common prep work
1692 switch (cmd) {
1693 case TIOCSETD:
1694 case TIOCSBRK:
1695 case TIOCCBRK:
1696 case TCSBRK:
1697 case TCSBRKP:
1698 retval = tty_check_change(tty);
1699 if (retval)
1700 return retval;
1701 if (cmd != TIOCCBRK) {
1702 tty_wait_until_sent(tty, 0);
1703 if (signal_pending(current))
1704 return -EINTR;
1706 break;
1709 switch (cmd) {
1710 case TIOCSTI:
1711 return tiocsti(tty, (char *)arg);
1712 case TIOCGWINSZ:
1713 return tiocgwinsz(tty, (struct winsize *) arg);
1714 case TIOCSWINSZ:
1715 return tiocswinsz(tty, real_tty, (struct winsize *) arg);
1716 case TIOCCONS:
1717 return tioccons(tty, real_tty);
1718 case FIONBIO:
1719 return fionbio(file, (int *) arg);
1720 case TIOCEXCL:
1721 set_bit(TTY_EXCLUSIVE, &tty->flags);
1722 return 0;
1723 case TIOCNXCL:
1724 clear_bit(TTY_EXCLUSIVE, &tty->flags);
1725 return 0;
1726 case TIOCNOTTY:
1727 if (current->tty != tty)
1728 return -ENOTTY;
1729 if (current->leader)
1730 disassociate_ctty(0);
1731 current->tty = NULL;
1732 return 0;
1733 case TIOCSCTTY:
1734 return tiocsctty(tty, arg);
1735 case TIOCGPGRP:
1736 return tiocgpgrp(tty, real_tty, (pid_t *) arg);
1737 case TIOCSPGRP:
1738 return tiocspgrp(tty, real_tty, (pid_t *) arg);
1739 case TIOCGSID:
1740 return tiocgsid(tty, real_tty, (pid_t *) arg);
1741 case TIOCGETD:
1742 return put_user(tty->ldisc.num, (int *) arg);
1743 case TIOCSETD:
1744 return tiocsetd(tty, (int *) arg);
1745 #ifdef CONFIG_VT
1746 case TIOCLINUX:
1747 return tioclinux(tty, arg);
1748 #endif
1749 case TIOCTTYGSTRUCT:
1750 return tiocttygstruct(tty, (struct tty_struct *) arg);
1753 * Break handling
1755 case TIOCSBRK: /* Turn break on, unconditionally */
1756 tty->driver.break_ctl(tty, -1);
1757 return 0;
1759 case TIOCCBRK: /* Turn break off, unconditionally */
1760 tty->driver.break_ctl(tty, 0);
1761 return 0;
1762 case TCSBRK: /* SVID version: non-zero arg --> no break */
1764 * XXX is the above comment correct, or the
1765 * code below correct? Is this ioctl used at
1766 * all by anyone?
1768 if (!arg)
1769 return send_break(tty, HZ/4);
1770 return 0;
1771 case TCSBRKP: /* support for POSIX tcsendbreak() */
1772 return send_break(tty, arg ? arg*(HZ/10) : HZ/4);
1774 if (tty->driver.ioctl) {
1775 int retval = (tty->driver.ioctl)(tty, file, cmd, arg);
1776 if (retval != -ENOIOCTLCMD)
1777 return retval;
1779 if (tty->ldisc.ioctl) {
1780 int retval = (tty->ldisc.ioctl)(tty, file, cmd, arg);
1781 if (retval != -ENOIOCTLCMD)
1782 return retval;
1784 return -EINVAL;
1789 * This implements the "Secure Attention Key" --- the idea is to
1790 * prevent trojan horses by killing all processes associated with this
1791 * tty when the user hits the "Secure Attention Key". Required for
1792 * super-paranoid applications --- see the Orange Book for more details.
1794 * This code could be nicer; ideally it should send a HUP, wait a few
1795 * seconds, then send a INT, and then a KILL signal. But you then
1796 * have to coordinate with the init process, since all processes associated
1797 * with the current tty must be dead before the new getty is allowed
1798 * to spawn.
1800 void do_SAK( struct tty_struct *tty)
1802 #ifdef TTY_SOFT_SAK
1803 tty_hangup(tty);
1804 #else
1805 struct task_struct *p;
1806 int session;
1807 int i;
1808 struct file *filp;
1810 if (!tty)
1811 return;
1812 session = tty->session;
1813 if (tty->ldisc.flush_buffer)
1814 tty->ldisc.flush_buffer(tty);
1815 if (tty->driver.flush_buffer)
1816 tty->driver.flush_buffer(tty);
1817 read_lock(&tasklist_lock);
1818 for_each_task(p) {
1819 if ((p->tty == tty) ||
1820 ((session > 0) && (p->session == session)))
1821 send_sig(SIGKILL, p, 1);
1822 else if (p->files) {
1823 for (i=0; i < p->files->max_fds; i++) {
1824 filp = fcheck_task(p, i);
1825 if (filp && (filp->f_op == &tty_fops) &&
1826 (filp->private_data == tty)) {
1827 send_sig(SIGKILL, p, 1);
1828 break;
1833 read_unlock(&tasklist_lock);
1834 #endif
1838 * This routine is called out of the software interrupt to flush data
1839 * from the flip buffer to the line discipline.
1841 static void flush_to_ldisc(void *private_)
1843 struct tty_struct *tty = (struct tty_struct *) private_;
1844 unsigned char *cp;
1845 char *fp;
1846 int count;
1847 unsigned long flags;
1849 if (tty->flip.buf_num) {
1850 cp = tty->flip.char_buf + TTY_FLIPBUF_SIZE;
1851 fp = tty->flip.flag_buf + TTY_FLIPBUF_SIZE;
1852 tty->flip.buf_num = 0;
1854 save_flags(flags); cli();
1855 tty->flip.char_buf_ptr = tty->flip.char_buf;
1856 tty->flip.flag_buf_ptr = tty->flip.flag_buf;
1857 } else {
1858 cp = tty->flip.char_buf;
1859 fp = tty->flip.flag_buf;
1860 tty->flip.buf_num = 1;
1862 save_flags(flags); cli();
1863 tty->flip.char_buf_ptr = tty->flip.char_buf + TTY_FLIPBUF_SIZE;
1864 tty->flip.flag_buf_ptr = tty->flip.flag_buf + TTY_FLIPBUF_SIZE;
1866 count = tty->flip.count;
1867 tty->flip.count = 0;
1868 restore_flags(flags);
1870 tty->ldisc.receive_buf(tty, cp, fp, count);
1874 * Routine which returns the baud rate of the tty
1878 * This is used to figure out the divisor speeds and the timeouts
1880 static int baud_table[] = {
1881 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
1882 9600, 19200, 38400, 57600, 115200, 230400, 460800, 0 };
1884 int tty_get_baud_rate(struct tty_struct *tty)
1886 unsigned int cflag, i;
1888 cflag = tty->termios->c_cflag;
1890 i = cflag & CBAUD;
1891 if (i & CBAUDEX) {
1892 i &= ~CBAUDEX;
1893 if (i < 1 || i > 4)
1894 tty->termios->c_cflag &= ~CBAUDEX;
1895 else
1896 i += 15;
1898 if (i==15 && tty->alt_speed) {
1899 if (!tty->warned) {
1900 printk("Use of setserial/setrocket to set SPD_* flags is deprecated\n");
1901 tty->warned = 1;
1903 return(tty->alt_speed);
1906 return baud_table[i];
1909 void tty_flip_buffer_push(struct tty_struct *tty)
1911 if (tty->low_latency)
1912 flush_to_ldisc((void *) tty);
1913 else
1914 queue_task(&tty->flip.tqueue, &tq_timer);
1918 * This subroutine initializes a tty structure.
1920 static void initialize_tty_struct(struct tty_struct *tty)
1922 memset(tty, 0, sizeof(struct tty_struct));
1923 tty->magic = TTY_MAGIC;
1924 tty->ldisc = ldiscs[N_TTY];
1925 tty->pgrp = -1;
1926 tty->flip.char_buf_ptr = tty->flip.char_buf;
1927 tty->flip.flag_buf_ptr = tty->flip.flag_buf;
1928 tty->flip.tqueue.routine = flush_to_ldisc;
1929 tty->flip.tqueue.data = tty;
1930 tty->flip.pty_sem = MUTEX;
1931 tty->tq_hangup.routine = do_tty_hangup;
1932 tty->tq_hangup.data = tty;
1936 * The default put_char routine if the driver did not define one.
1938 void tty_default_put_char(struct tty_struct *tty, unsigned char ch)
1940 tty->driver.write(tty, 0, &ch, 1);
1944 * Called by a tty driver to register itself.
1946 int tty_register_driver(struct tty_driver *driver)
1948 int error;
1950 if (driver->flags & TTY_DRIVER_INSTALLED)
1951 return 0;
1953 error = register_chrdev(driver->major, driver->name, &tty_fops);
1954 if (error < 0)
1955 return error;
1956 else if(driver->major == 0)
1957 driver->major = error;
1959 if (!driver->put_char)
1960 driver->put_char = tty_default_put_char;
1962 driver->prev = 0;
1963 driver->next = tty_drivers;
1964 if (tty_drivers) tty_drivers->prev = driver;
1965 tty_drivers = driver;
1967 proc_tty_register_driver(driver);
1968 return error;
1972 * Called by a tty driver to unregister itself.
1974 int tty_unregister_driver(struct tty_driver *driver)
1976 int retval;
1977 struct tty_driver *p;
1978 int found = 0;
1979 const char *othername = NULL;
1981 if (*driver->refcount)
1982 return -EBUSY;
1984 for (p = tty_drivers; p; p = p->next) {
1985 if (p == driver)
1986 found++;
1987 else if (p->major == driver->major)
1988 othername = p->name;
1991 if (!found)
1992 return -ENOENT;
1994 if (othername == NULL) {
1995 retval = unregister_chrdev(driver->major, driver->name);
1996 if (retval)
1997 return retval;
1998 } else
1999 register_chrdev(driver->major, othername, &tty_fops);
2001 if (driver->prev)
2002 driver->prev->next = driver->next;
2003 else
2004 tty_drivers = driver->next;
2006 if (driver->next)
2007 driver->next->prev = driver->prev;
2009 proc_tty_unregister_driver(driver);
2010 return 0;
2015 * Initialize the console device. This is called *early*, so
2016 * we can't necessarily depend on lots of kernel help here.
2017 * Just do some early initializations, and do the complex setup
2018 * later.
2020 long __init console_init(long kmem_start, long kmem_end)
2022 /* Setup the default TTY line discipline. */
2023 memset(ldiscs, 0, sizeof(ldiscs));
2024 (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);
2027 * Set up the standard termios. Individual tty drivers may
2028 * deviate from this; this is used as a template.
2030 memset(&tty_std_termios, 0, sizeof(struct termios));
2031 memcpy(tty_std_termios.c_cc, INIT_C_CC, NCCS);
2032 tty_std_termios.c_iflag = ICRNL | IXON;
2033 tty_std_termios.c_oflag = OPOST | ONLCR;
2034 tty_std_termios.c_cflag = B38400 | CS8 | CREAD | HUPCL;
2035 tty_std_termios.c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK |
2036 ECHOCTL | ECHOKE | IEXTEN;
2039 * set up the console device so that later boot sequences can
2040 * inform about problems etc..
2042 #ifdef CONFIG_VT
2043 kmem_start = con_init(kmem_start);
2044 #endif
2045 #ifdef CONFIG_SERIAL_CONSOLE
2046 #ifdef CONFIG_8xx
2047 kmem_start = console_8xx_init(kmem_start, kmem_end);
2048 #else
2049 kmem_start = serial_console_init(kmem_start, kmem_end);
2050 #endif /* CONFIG_8xx */
2051 #endif
2052 return kmem_start;
2055 static struct tty_driver dev_tty_driver, dev_syscons_driver;
2056 #ifdef CONFIG_UNIX98_PTYS
2057 static struct tty_driver dev_ptmx_driver;
2058 #endif
2059 #ifdef CONFIG_VT
2060 static struct tty_driver dev_console_driver;
2061 #endif
2064 * Ok, now we can initialize the rest of the tty devices and can count
2065 * on memory allocations, interrupts etc..
2067 __initfunc(int tty_init(void))
2069 if (sizeof(struct tty_struct) > PAGE_SIZE)
2070 panic("size of tty structure > PAGE_SIZE!");
2073 * dev_tty_driver and dev_console_driver are actually magic
2074 * devices which get redirected at open time. Nevertheless,
2075 * we register them so that register_chrdev is called
2076 * appropriately.
2078 memset(&dev_tty_driver, 0, sizeof(struct tty_driver));
2079 dev_tty_driver.magic = TTY_DRIVER_MAGIC;
2080 dev_tty_driver.driver_name = "/dev/tty";
2081 dev_tty_driver.name = dev_tty_driver.driver_name + 5;
2082 dev_tty_driver.name_base = 0;
2083 dev_tty_driver.major = TTYAUX_MAJOR;
2084 dev_tty_driver.minor_start = 0;
2085 dev_tty_driver.num = 1;
2086 dev_tty_driver.type = TTY_DRIVER_TYPE_SYSTEM;
2087 dev_tty_driver.subtype = SYSTEM_TYPE_TTY;
2089 if (tty_register_driver(&dev_tty_driver))
2090 panic("Couldn't register /dev/tty driver\n");
2092 dev_syscons_driver = dev_tty_driver;
2093 dev_syscons_driver.driver_name = "/dev/console";
2094 dev_syscons_driver.name = dev_syscons_driver.driver_name + 5;
2095 dev_syscons_driver.major = TTYAUX_MAJOR;
2096 dev_syscons_driver.minor_start = 1;
2097 dev_syscons_driver.type = TTY_DRIVER_TYPE_SYSTEM;
2098 dev_syscons_driver.subtype = SYSTEM_TYPE_SYSCONS;
2100 if (tty_register_driver(&dev_syscons_driver))
2101 panic("Couldn't register /dev/console driver\n");
2103 #ifdef CONFIG_UNIX98_PTYS
2104 dev_ptmx_driver = dev_tty_driver;
2105 dev_ptmx_driver.driver_name = "/dev/ptmx";
2106 dev_ptmx_driver.name = dev_ptmx_driver.driver_name + 5;
2107 dev_ptmx_driver.major= MAJOR(PTMX_DEV);
2108 dev_ptmx_driver.minor_start = MINOR(PTMX_DEV);
2109 dev_ptmx_driver.type = TTY_DRIVER_TYPE_SYSTEM;
2110 dev_ptmx_driver.subtype = SYSTEM_TYPE_SYSPTMX;
2112 if (tty_register_driver(&dev_ptmx_driver))
2113 panic("Couldn't register /dev/ptmx driver\n");
2114 #endif
2116 #ifdef CONFIG_VT
2117 dev_console_driver = dev_tty_driver;
2118 dev_console_driver.driver_name = "/dev/tty0";
2119 dev_console_driver.name = dev_console_driver.driver_name + 5;
2120 dev_console_driver.major = TTY_MAJOR;
2121 dev_console_driver.type = TTY_DRIVER_TYPE_SYSTEM;
2122 dev_console_driver.subtype = SYSTEM_TYPE_CONSOLE;
2124 if (tty_register_driver(&dev_console_driver))
2125 panic("Couldn't register /dev/tty0 driver\n");
2127 kbd_init();
2128 #endif
2129 #ifdef CONFIG_ESPSERIAL /* init ESP before rs, so rs doesn't see the port */
2130 espserial_init();
2131 #endif
2132 #ifdef CONFIG_SERIAL
2133 rs_init();
2134 #endif
2135 #ifdef CONFIG_MAC_SERIAL
2136 macserial_init();
2137 #endif
2138 #ifdef CONFIG_ROCKETPORT
2139 rp_init();
2140 #endif
2141 #ifdef CONFIG_MVME16x
2142 serial167_init();
2143 #endif
2144 #ifdef CONFIG_CYCLADES
2145 cy_init();
2146 #endif
2147 #ifdef CONFIG_STALLION
2148 stl_init();
2149 #endif
2150 #ifdef CONFIG_ISTALLION
2151 stli_init();
2152 #endif
2153 #ifdef CONFIG_DIGI
2154 pcxe_init();
2155 #endif
2156 #ifdef CONFIG_DIGIEPCA
2157 pc_init();
2158 #endif
2159 #ifdef CONFIG_RISCOM8
2160 riscom8_init();
2161 #endif
2162 #ifdef CONFIG_SPECIALIX
2163 specialix_init();
2164 #endif
2165 #ifdef CONFIG_8xx
2166 rs_8xx_init();
2167 #endif /* CONFIG_8xx */
2168 pty_init();
2169 #ifdef CONFIG_VT
2170 vcs_init();
2171 #endif
2172 return 0;