2 * linux/drivers/char/tty_io.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
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...
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 devfs support.
56 * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 13-Jan-1998
58 * Added support for a Unix98-style ptmx device.
59 * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998
61 * Reduced memory usage for older ARM systems
62 * -- Russell King <rmk@arm.linux.org.uk>
64 * Move do_SAK() into process context. Less stack use in devfs functions.
65 * alloc_tty_struct() always uses kmalloc() -- Andrew Morton <andrewm@uow.edu.eu> 17Mar01
68 #include <linux/config.h>
69 #include <linux/types.h>
70 #include <linux/major.h>
71 #include <linux/errno.h>
72 #include <linux/signal.h>
73 #include <linux/fcntl.h>
74 #include <linux/sched.h>
75 #include <linux/interrupt.h>
76 #include <linux/tty.h>
77 #include <linux/tty_driver.h>
78 #include <linux/tty_flip.h>
79 #include <linux/devpts_fs.h>
80 #include <linux/file.h>
81 #include <linux/console.h>
82 #include <linux/timer.h>
83 #include <linux/ctype.h>
86 #include <linux/string.h>
87 #include <linux/slab.h>
88 #include <linux/poll.h>
89 #include <linux/proc_fs.h>
90 #include <linux/init.h>
91 #include <linux/module.h>
92 #include <linux/smp_lock.h>
93 #include <linux/device.h>
95 #include <asm/uaccess.h>
96 #include <asm/system.h>
97 #include <asm/bitops.h>
99 #include <linux/kbd_kern.h>
100 #include <linux/vt_kern.h>
101 #include <linux/selection.h>
102 #include <linux/devfs_fs_kernel.h>
104 #include <linux/kmod.h>
106 #define IS_CONSOLE_DEV(dev) (kdev_val(dev) == __mkdev(TTY_MAJOR,0))
107 #define IS_TTY_DEV(dev) (kdev_val(dev) == __mkdev(TTYAUX_MAJOR,0))
108 #define IS_SYSCONS_DEV(dev) (kdev_val(dev) == __mkdev(TTYAUX_MAJOR,1))
109 #define IS_PTMX_DEV(dev) (kdev_val(dev) == __mkdev(TTYAUX_MAJOR,2))
111 #undef TTY_DEBUG_HANGUP
113 #define TTY_PARANOIA_CHECK 1
114 #define CHECK_TTY_COUNT 1
116 struct termios tty_std_termios = { /* for the benefit of tty drivers */
117 .c_iflag = ICRNL | IXON,
118 .c_oflag = OPOST | ONLCR,
119 .c_cflag = B38400 | CS8 | CREAD | HUPCL,
120 .c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK |
121 ECHOCTL | ECHOKE | IEXTEN,
125 LIST_HEAD(tty_drivers); /* linked list of tty drivers */
126 struct tty_ldisc ldiscs[NR_LDISCS]; /* line disc dispatch table */
128 #ifdef CONFIG_UNIX98_PTYS
129 extern struct tty_driver *ptm_driver; /* Unix98 pty masters; for /dev/ptmx */
130 extern struct tty_driver *pts_driver; /* Unix98 pty slaves; for /dev/ptmx */
133 extern void disable_early_printk(void);
135 static void initialize_tty_struct(struct tty_struct *tty);
137 static ssize_t tty_read(struct file *, char *, size_t, loff_t *);
138 static ssize_t tty_write(struct file *, const char *, size_t, loff_t *);
139 static unsigned int tty_poll(struct file *, poll_table *);
140 static int tty_open(struct inode *, struct file *);
141 static int tty_release(struct inode *, struct file *);
142 int tty_ioctl(struct inode * inode, struct file * file,
143 unsigned int cmd, unsigned long arg);
144 static int tty_fasync(int fd, struct file * filp, int on);
145 extern int vme_scc_init (void);
146 extern int serial167_init(void);
147 extern int rs_8xx_init(void);
148 extern void tub3270_init(void);
149 extern void rs_360_init(void);
150 extern void tx3912_rs_init(void);
152 static struct tty_struct *alloc_tty_struct(void)
154 struct tty_struct *tty;
156 tty = kmalloc(sizeof(struct tty_struct), GFP_KERNEL);
158 memset(tty, 0, sizeof(struct tty_struct));
162 static inline void free_tty_struct(struct tty_struct *tty)
167 #define TTY_NUMBER(tty) ((tty)->index + (tty)->driver->name_base)
169 char *tty_name(struct tty_struct *tty, char *buf)
171 if (!tty) /* Hmm. NULL pointer. That's fun. */
172 strcpy(buf, "NULL tty");
174 strcpy(buf, tty->name);
178 EXPORT_SYMBOL(tty_name);
180 inline int tty_paranoia_check(struct tty_struct *tty, kdev_t device,
183 #ifdef TTY_PARANOIA_CHECK
184 static const char badmagic[] = KERN_WARNING
185 "Warning: bad magic number for tty struct (%s) in %s\n";
186 static const char badtty[] = KERN_WARNING
187 "Warning: null TTY for (%s) in %s\n";
190 printk(badtty, cdevname(device), routine);
193 if (tty->magic != TTY_MAGIC) {
194 printk(badmagic, cdevname(device), routine);
201 static int check_tty_count(struct tty_struct *tty, const char *routine)
203 #ifdef CHECK_TTY_COUNT
208 list_for_each(p, &tty->tty_files) {
212 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
213 tty->driver->subtype == PTY_TYPE_SLAVE &&
214 tty->link && tty->link->count)
216 if (tty->count != count) {
217 printk(KERN_WARNING "Warning: dev (%s) tty->count(%d) "
218 "!= #fd's(%d) in %s\n",
219 tty->name, tty->count, count, routine);
226 int tty_register_ldisc(int disc, struct tty_ldisc *new_ldisc)
228 if (disc < N_TTY || disc >= NR_LDISCS)
232 ldiscs[disc] = *new_ldisc;
233 ldiscs[disc].flags |= LDISC_FLAG_DEFINED;
234 ldiscs[disc].num = disc;
236 memset(&ldiscs[disc], 0, sizeof(struct tty_ldisc));
241 EXPORT_SYMBOL(tty_register_ldisc);
243 /* Set the discipline of a tty line. */
244 static int tty_set_ldisc(struct tty_struct *tty, int ldisc)
247 struct tty_ldisc o_ldisc;
250 if ((ldisc < N_TTY) || (ldisc >= NR_LDISCS))
252 /* Eduardo Blanco <ejbs@cs.cs.com.uy> */
253 /* Cyrus Durgin <cider@speakeasy.org> */
254 if (!(ldiscs[ldisc].flags & LDISC_FLAG_DEFINED)) {
255 request_module("tty-ldisc-%d", ldisc);
257 if (!(ldiscs[ldisc].flags & LDISC_FLAG_DEFINED))
260 if (tty->ldisc.num == ldisc)
261 return 0; /* We are already in the desired discipline */
263 if (!try_module_get(ldiscs[ldisc].owner))
266 o_ldisc = tty->ldisc;
268 tty_wait_until_sent(tty, 0);
270 /* Shutdown the current discipline. */
271 if (tty->ldisc.close)
272 (tty->ldisc.close)(tty);
274 /* Now set up the new line discipline. */
275 tty->ldisc = ldiscs[ldisc];
276 tty->termios->c_line = ldisc;
278 retval = (tty->ldisc.open)(tty);
280 tty->ldisc = o_ldisc;
281 tty->termios->c_line = tty->ldisc.num;
282 if (tty->ldisc.open && (tty->ldisc.open(tty) < 0)) {
283 tty->ldisc = ldiscs[N_TTY];
284 tty->termios->c_line = N_TTY;
285 if (tty->ldisc.open) {
286 int r = tty->ldisc.open(tty);
289 panic("Couldn't open N_TTY ldisc for "
291 tty_name(tty, buf), r);
295 module_put(o_ldisc.owner);
298 if (tty->ldisc.num != o_ldisc.num && tty->driver->set_ldisc)
299 tty->driver->set_ldisc(tty);
304 * This routine returns a tty driver structure, given a device number
306 struct tty_driver *get_tty_driver(dev_t device, int *index)
308 struct tty_driver *p;
310 list_for_each_entry(p, &tty_drivers, tty_drivers) {
311 dev_t base = MKDEV(p->major, p->minor_start);
312 if (device < base || device >= base + p->num)
314 *index = device - base;
321 * If we try to write to, or set the state of, a terminal and we're
322 * not in the foreground, send a SIGTTOU. If the signal is blocked or
323 * ignored, go ahead and perform the operation. (POSIX 7.2)
325 int tty_check_change(struct tty_struct * tty)
327 if (current->tty != tty)
329 if (tty->pgrp <= 0) {
330 printk(KERN_WARNING "tty_check_change: tty->pgrp <= 0!\n");
333 if (current->pgrp == tty->pgrp)
335 if (is_ignored(SIGTTOU))
337 if (is_orphaned_pgrp(current->pgrp))
339 (void) kill_pg(current->pgrp,SIGTTOU,1);
343 EXPORT_SYMBOL(tty_check_change);
345 static ssize_t hung_up_tty_read(struct file * file, char * buf,
346 size_t count, loff_t *ppos)
348 /* Can't seek (pread) on ttys. */
349 if (ppos != &file->f_pos)
354 static ssize_t hung_up_tty_write(struct file * file, const char * buf,
355 size_t count, loff_t *ppos)
357 /* Can't seek (pwrite) on ttys. */
358 if (ppos != &file->f_pos)
363 /* No kernel lock held - none needed ;) */
364 static unsigned int hung_up_tty_poll(struct file * filp, poll_table * wait)
366 return POLLIN | POLLOUT | POLLERR | POLLHUP | POLLRDNORM | POLLWRNORM;
369 static int hung_up_tty_ioctl(struct inode * inode, struct file * file,
370 unsigned int cmd, unsigned long arg)
372 return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
375 static struct file_operations tty_fops = {
382 .release = tty_release,
383 .fasync = tty_fasync,
386 static struct file_operations hung_up_tty_fops = {
388 .read = hung_up_tty_read,
389 .write = hung_up_tty_write,
390 .poll = hung_up_tty_poll,
391 .ioctl = hung_up_tty_ioctl,
392 .release = tty_release,
395 static spinlock_t redirect_lock = SPIN_LOCK_UNLOCKED;
396 static struct file *redirect;
398 * This can be called by the "eventd" kernel thread. That is process synchronous,
399 * but doesn't hold any locks, so we need to make sure we have the appropriate
400 * locks for what we're doing..
402 void do_tty_hangup(void *data)
404 struct tty_struct *tty = (struct tty_struct *) data;
405 struct file * cons_filp = NULL;
406 struct file *filp, *f = NULL;
407 struct task_struct *p;
409 int closecount = 0, n;
414 /* inuse_filps is protected by the single kernel lock */
417 spin_lock(&redirect_lock);
418 if (redirect && redirect->private_data == tty) {
422 spin_unlock(&redirect_lock);
426 check_tty_count(tty, "do_tty_hangup");
428 list_for_each_entry(filp, &tty->tty_files, f_list) {
429 if (IS_CONSOLE_DEV(filp->f_dentry->d_inode->i_rdev) ||
430 IS_SYSCONS_DEV(filp->f_dentry->d_inode->i_rdev)) {
434 if (filp->f_op != &tty_fops)
437 tty_fasync(-1, filp, 0); /* can't block */
438 filp->f_op = &hung_up_tty_fops;
442 /* FIXME! What are the locking issues here? This may me overdoing things..
443 * this question is especially important now that we've removed the irqlock. */
447 local_irq_save(flags); // FIXME: is this safe?
448 if (tty->ldisc.flush_buffer)
449 tty->ldisc.flush_buffer(tty);
450 if (tty->driver->flush_buffer)
451 tty->driver->flush_buffer(tty);
452 if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
453 tty->ldisc.write_wakeup)
454 (tty->ldisc.write_wakeup)(tty);
455 local_irq_restore(flags); // FIXME: is this safe?
458 wake_up_interruptible(&tty->write_wait);
459 wake_up_interruptible(&tty->read_wait);
462 * Shutdown the current line discipline, and reset it to
465 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
466 *tty->termios = tty->driver->init_termios;
467 if (tty->ldisc.num != ldiscs[N_TTY].num) {
468 if (tty->ldisc.close)
469 (tty->ldisc.close)(tty);
470 module_put(tty->ldisc.owner);
472 tty->ldisc = ldiscs[N_TTY];
473 tty->termios->c_line = N_TTY;
474 if (tty->ldisc.open) {
475 int i = (tty->ldisc.open)(tty);
477 printk(KERN_ERR "do_tty_hangup: N_TTY open: "
482 read_lock(&tasklist_lock);
483 if (tty->session > 0) {
485 for_each_task_pid(tty->session, PIDTYPE_SID, p, l, pid) {
490 send_group_sig_info(SIGHUP, SEND_SIG_PRIV, p);
491 send_group_sig_info(SIGCONT, SEND_SIG_PRIV, p);
493 p->tty_old_pgrp = tty->pgrp;
496 read_unlock(&tasklist_lock);
501 tty->ctrl_status = 0;
503 * If one of the devices matches a console pointer, we
504 * cannot just call hangup() because that will cause
505 * tty->count and state->count to go out of sync.
506 * So we just call close() the right number of times.
509 if (tty->driver->close)
510 for (n = 0; n < closecount; n++)
511 tty->driver->close(tty, cons_filp);
512 } else if (tty->driver->hangup)
513 (tty->driver->hangup)(tty);
517 void tty_hangup(struct tty_struct * tty)
519 #ifdef TTY_DEBUG_HANGUP
522 printk(KERN_DEBUG "%s hangup...\n", tty_name(tty, buf));
524 schedule_work(&tty->hangup_work);
527 EXPORT_SYMBOL(tty_hangup);
529 void tty_vhangup(struct tty_struct * tty)
531 #ifdef TTY_DEBUG_HANGUP
534 printk(KERN_DEBUG "%s vhangup...\n", tty_name(tty, buf));
536 do_tty_hangup((void *) tty);
538 EXPORT_SYMBOL(tty_vhangup);
540 int tty_hung_up_p(struct file * filp)
542 return (filp->f_op == &hung_up_tty_fops);
545 EXPORT_SYMBOL(tty_hung_up_p);
548 * This function is typically called only by the session leader, when
549 * it wants to disassociate itself from its controlling tty.
551 * It performs the following functions:
552 * (1) Sends a SIGHUP and SIGCONT to the foreground process group
553 * (2) Clears the tty from being controlling the session
554 * (3) Clears the controlling tty for all processes in the
557 * The argument on_exit is set to 1 if called when a process is
558 * exiting; it is 0 if called by the ioctl TIOCNOTTY.
560 void disassociate_ctty(int on_exit)
562 struct tty_struct *tty;
563 struct task_struct *p;
572 tty_pgrp = tty->pgrp;
573 if (on_exit && tty->driver->type != TTY_DRIVER_TYPE_PTY)
576 if (current->tty_old_pgrp) {
577 kill_pg(current->tty_old_pgrp, SIGHUP, on_exit);
578 kill_pg(current->tty_old_pgrp, SIGCONT, on_exit);
584 kill_pg(tty_pgrp, SIGHUP, on_exit);
586 kill_pg(tty_pgrp, SIGCONT, on_exit);
589 current->tty_old_pgrp = 0;
593 read_lock(&tasklist_lock);
594 for_each_task_pid(current->session, PIDTYPE_SID, p, l, pid)
596 read_unlock(&tasklist_lock);
600 void stop_tty(struct tty_struct *tty)
605 if (tty->link && tty->link->packet) {
606 tty->ctrl_status &= ~TIOCPKT_START;
607 tty->ctrl_status |= TIOCPKT_STOP;
608 wake_up_interruptible(&tty->link->read_wait);
610 if (tty->driver->stop)
611 (tty->driver->stop)(tty);
614 EXPORT_SYMBOL(stop_tty);
616 void start_tty(struct tty_struct *tty)
618 if (!tty->stopped || tty->flow_stopped)
621 if (tty->link && tty->link->packet) {
622 tty->ctrl_status &= ~TIOCPKT_STOP;
623 tty->ctrl_status |= TIOCPKT_START;
624 wake_up_interruptible(&tty->link->read_wait);
626 if (tty->driver->start)
627 (tty->driver->start)(tty);
628 if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
629 tty->ldisc.write_wakeup)
630 (tty->ldisc.write_wakeup)(tty);
631 wake_up_interruptible(&tty->write_wait);
634 EXPORT_SYMBOL(start_tty);
636 static ssize_t tty_read(struct file * file, char * buf, size_t count,
640 struct tty_struct * tty;
643 /* Can't seek (pread) on ttys. */
644 if (ppos != &file->f_pos)
647 tty = (struct tty_struct *)file->private_data;
648 inode = file->f_dentry->d_inode;
649 if (tty_paranoia_check(tty, inode->i_rdev, "tty_read"))
651 if (!tty || (test_bit(TTY_IO_ERROR, &tty->flags)))
654 /* This check not only needs to be done before reading, but also
655 whenever read_chan() gets woken up after sleeping, so I've
656 moved it to there. This should only be done for the N_TTY
657 line discipline, anyway. Same goes for write_chan(). -- jlc. */
659 if (!IS_CONSOLE_DEV(inode->i_rdev) && /* don't stop on /dev/console */
661 (current->tty == tty) &&
662 (tty->pgrp != current->pgrp))
663 if (is_ignored(SIGTTIN) || is_orphaned_pgrp(current->pgrp))
666 (void) kill_pg(current->pgrp, SIGTTIN, 1);
672 i = (tty->ldisc.read)(tty,file,buf,count);
677 inode->i_atime = CURRENT_TIME;
682 * Split writes up in sane blocksizes to avoid
683 * denial-of-service type attacks
685 static inline ssize_t do_tty_write(
686 ssize_t (*write)(struct tty_struct *, struct file *, const unsigned char *, size_t),
687 struct tty_struct *tty,
689 const unsigned char *buf,
692 ssize_t ret = 0, written = 0;
694 if (down_interruptible(&tty->atomic_write)) {
697 if ( test_bit(TTY_NO_WRITE_SPLIT, &tty->flags) ) {
699 written = write(tty, file, buf, count);
703 unsigned long size = max((unsigned long)PAGE_SIZE*2, 16384UL);
707 ret = write(tty, file, buf, size);
717 if (signal_pending(current))
723 file->f_dentry->d_inode->i_mtime = CURRENT_TIME;
726 up(&tty->atomic_write);
731 static ssize_t tty_write(struct file * file, const char * buf, size_t count,
735 struct tty_struct * tty;
736 struct inode *inode = file->f_dentry->d_inode;
738 * For now, we redirect writes from /dev/console as
741 is_console = IS_SYSCONS_DEV(inode->i_rdev) ||
742 IS_CONSOLE_DEV(inode->i_rdev);
744 /* Can't seek (pwrite) on ttys. */
745 if (ppos != &file->f_pos)
749 struct file *p = NULL;
751 spin_lock(&redirect_lock);
756 spin_unlock(&redirect_lock);
759 ssize_t res = vfs_write(p, buf, count, &p->f_pos);
765 tty = (struct tty_struct *)file->private_data;
766 if (tty_paranoia_check(tty, inode->i_rdev, "tty_write"))
768 if (!tty || !tty->driver->write || (test_bit(TTY_IO_ERROR, &tty->flags)))
770 if (!tty->ldisc.write)
772 return do_tty_write(tty->ldisc.write, tty, file,
773 (const unsigned char *)buf, count);
776 /* Semaphore to protect creating and releasing a tty */
777 static DECLARE_MUTEX(tty_sem);
779 static void down_tty_sem(int index)
784 static void up_tty_sem(int index)
789 static void release_mem(struct tty_struct *tty, int idx);
791 static inline void tty_line_name(struct tty_driver *driver, int index, char *p)
793 sprintf(p, "%s%d", driver->name, index + driver->name_base);
797 * WSH 06/09/97: Rewritten to remove races and properly clean up after a
798 * failed open. The new code protects the open with a semaphore, so it's
799 * really quite straightforward. The semaphore locking can probably be
800 * relaxed for the (most common) case of reopening a tty.
802 static int init_dev(struct tty_driver *driver, int idx,
803 struct tty_struct **ret_tty)
805 struct tty_struct *tty, *o_tty;
806 struct termios *tp, **tp_loc, *o_tp, **o_tp_loc;
807 struct termios *ltp, **ltp_loc, *o_ltp, **o_ltp_loc;
811 * Check whether we need to acquire the tty semaphore to avoid
812 * race conditions. For now, play it safe.
816 /* check whether we're reopening an existing tty */
817 tty = driver->ttys[idx];
818 if (tty) goto fast_track;
821 * First time open is complex, especially for PTY devices.
822 * This code guarantees that either everything succeeds and the
823 * TTY is ready for operation, or else the table slots are vacated
824 * and the allocated memory released. (Except that the termios
825 * and locked termios may be retained.)
828 if (!try_module_get(driver->owner)) {
837 tty = alloc_tty_struct();
840 initialize_tty_struct(tty);
841 tty->device = MKDEV(driver->major, driver->minor_start) + idx;
842 tty->driver = driver;
844 tty_line_name(driver, idx, tty->name);
846 tp_loc = &driver->termios[idx];
848 tp = (struct termios *) kmalloc(sizeof(struct termios),
852 *tp = driver->init_termios;
855 ltp_loc = &driver->termios_locked[idx];
857 ltp = (struct termios *) kmalloc(sizeof(struct termios),
861 memset(ltp, 0, sizeof(struct termios));
864 if (driver->type == TTY_DRIVER_TYPE_PTY) {
865 o_tty = alloc_tty_struct();
868 initialize_tty_struct(o_tty);
869 o_tty->device = MKDEV(driver->other->major,
870 driver->other->minor_start) + idx;
871 o_tty->driver = driver->other;
873 tty_line_name(driver->other, idx, o_tty->name);
875 o_tp_loc = &driver->other->termios[idx];
877 o_tp = (struct termios *)
878 kmalloc(sizeof(struct termios), GFP_KERNEL);
881 *o_tp = driver->other->init_termios;
884 o_ltp_loc = &driver->other->termios_locked[idx];
886 o_ltp = (struct termios *)
887 kmalloc(sizeof(struct termios), GFP_KERNEL);
890 memset(o_ltp, 0, sizeof(struct termios));
894 * Everything allocated ... set up the o_tty structure.
896 driver->other->ttys[idx] = o_tty;
901 o_tty->termios = *o_tp_loc;
902 o_tty->termios_locked = *o_ltp_loc;
903 driver->other->refcount++;
904 if (driver->subtype == PTY_TYPE_MASTER)
907 /* Establish the links in both directions */
913 * All structures have been allocated, so now we install them.
914 * Failures after this point use release_mem to clean up, so
915 * there's no need to null out the local pointers.
917 driver->ttys[idx] = tty;
923 tty->termios = *tp_loc;
924 tty->termios_locked = *ltp_loc;
929 * Structures all installed ... call the ldisc open routines.
930 * If we fail here just call release_mem to clean up. No need
931 * to decrement the use counts, as release_mem doesn't care.
933 if (tty->ldisc.open) {
934 retval = (tty->ldisc.open)(tty);
936 goto release_mem_out;
938 if (o_tty && o_tty->ldisc.open) {
939 retval = (o_tty->ldisc.open)(o_tty);
941 if (tty->ldisc.close)
942 (tty->ldisc.close)(tty);
943 goto release_mem_out;
949 * This fast open can be used if the tty is already open.
950 * No memory is allocated, and the only failures are from
951 * attempting to open a closing tty or attempting multiple
952 * opens on a pty master.
955 if (test_bit(TTY_CLOSING, &tty->flags)) {
959 if (driver->type == TTY_DRIVER_TYPE_PTY &&
960 driver->subtype == PTY_TYPE_MASTER) {
962 * special case for PTY masters: only one open permitted,
963 * and the slave side open count is incremented as well.
972 tty->driver = driver; /* N.B. why do this every time?? */
977 /* All paths come through here to release the semaphore */
982 /* Release locally allocated memory ... nothing placed in slots */
987 free_tty_struct(o_tty);
992 free_tty_struct(tty);
995 module_put(driver->owner);
999 /* call the tty release_mem routine to clean out this slot */
1001 printk(KERN_INFO "init_dev: ldisc open failed, "
1002 "clearing slot %d\n", idx);
1003 release_mem(tty, idx);
1008 * Releases memory associated with a tty structure, and clears out the
1009 * driver table slots.
1011 static void release_mem(struct tty_struct *tty, int idx)
1013 struct tty_struct *o_tty;
1016 if ((o_tty = tty->link) != NULL) {
1017 o_tty->driver->ttys[idx] = NULL;
1018 if (o_tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) {
1019 tp = o_tty->driver->termios[idx];
1020 o_tty->driver->termios[idx] = NULL;
1024 o_tty->driver->refcount--;
1026 list_del(&o_tty->tty_files);
1028 free_tty_struct(o_tty);
1031 tty->driver->ttys[idx] = NULL;
1032 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) {
1033 tp = tty->driver->termios[idx];
1034 tty->driver->termios[idx] = NULL;
1038 tty->driver->refcount--;
1040 list_del(&tty->tty_files);
1042 module_put(tty->driver->owner);
1043 free_tty_struct(tty);
1047 * Even releasing the tty structures is a tricky business.. We have
1048 * to be very careful that the structures are all released at the
1049 * same time, as interrupts might otherwise get the wrong pointers.
1051 * WSH 09/09/97: rewritten to avoid some nasty race conditions that could
1052 * lead to double frees or releasing memory still in use.
1054 static void release_dev(struct file * filp)
1056 struct tty_struct *tty, *o_tty;
1057 int pty_master, tty_closing, o_tty_closing, do_sleep;
1061 tty = (struct tty_struct *)filp->private_data;
1062 if (tty_paranoia_check(tty, filp->f_dentry->d_inode->i_rdev, "release_dev"))
1065 check_tty_count(tty, "release_dev");
1067 tty_fasync(-1, filp, 0);
1070 pty_master = (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
1071 tty->driver->subtype == PTY_TYPE_MASTER);
1074 #ifdef TTY_PARANOIA_CHECK
1075 if (idx < 0 || idx >= tty->driver->num) {
1076 printk(KERN_DEBUG "release_dev: bad idx when trying to "
1077 "free (%s)\n", tty->name);
1080 if (tty != tty->driver->ttys[idx]) {
1081 printk(KERN_DEBUG "release_dev: driver.table[%d] not tty "
1082 "for (%s)\n", idx, tty->name);
1085 if (tty->termios != tty->driver->termios[idx]) {
1086 printk(KERN_DEBUG "release_dev: driver.termios[%d] not termios "
1091 if (tty->termios_locked != tty->driver->termios_locked[idx]) {
1092 printk(KERN_DEBUG "release_dev: driver.termios_locked[%d] not "
1093 "termios_locked for (%s)\n",
1099 #ifdef TTY_DEBUG_HANGUP
1100 printk(KERN_DEBUG "release_dev of %s (tty count=%d)...",
1101 tty_name(tty, buf), tty->count);
1104 #ifdef TTY_PARANOIA_CHECK
1105 if (tty->driver->other) {
1106 if (o_tty != tty->driver->other->ttys[idx]) {
1107 printk(KERN_DEBUG "release_dev: other->table[%d] "
1108 "not o_tty for (%s)\n",
1112 if (o_tty->termios != tty->driver->other->termios[idx]) {
1113 printk(KERN_DEBUG "release_dev: other->termios[%d] "
1114 "not o_termios for (%s)\n",
1118 if (o_tty->termios_locked !=
1119 tty->driver->other->termios_locked[idx]) {
1120 printk(KERN_DEBUG "release_dev: other->termios_locked["
1121 "%d] not o_termios_locked for (%s)\n",
1125 if (o_tty->link != tty) {
1126 printk(KERN_DEBUG "release_dev: bad pty pointers\n");
1132 if (tty->driver->close)
1133 tty->driver->close(tty, filp);
1136 * Sanity check: if tty->count is going to zero, there shouldn't be
1137 * any waiters on tty->read_wait or tty->write_wait. We test the
1138 * wait queues and kick everyone out _before_ actually starting to
1139 * close. This ensures that we won't block while releasing the tty
1142 * The test for the o_tty closing is necessary, since the master and
1143 * slave sides may close in any order. If the slave side closes out
1144 * first, its count will be one, since the master side holds an open.
1145 * Thus this test wouldn't be triggered at the time the slave closes,
1148 * Note that it's possible for the tty to be opened again while we're
1149 * flushing out waiters. By recalculating the closing flags before
1150 * each iteration we avoid any problems.
1153 tty_closing = tty->count <= 1;
1154 o_tty_closing = o_tty &&
1155 (o_tty->count <= (pty_master ? 1 : 0));
1159 if (waitqueue_active(&tty->read_wait)) {
1160 wake_up(&tty->read_wait);
1163 if (waitqueue_active(&tty->write_wait)) {
1164 wake_up(&tty->write_wait);
1168 if (o_tty_closing) {
1169 if (waitqueue_active(&o_tty->read_wait)) {
1170 wake_up(&o_tty->read_wait);
1173 if (waitqueue_active(&o_tty->write_wait)) {
1174 wake_up(&o_tty->write_wait);
1181 printk(KERN_WARNING "release_dev: %s: read/write wait queue "
1182 "active!\n", tty_name(tty, buf));
1187 * The closing flags are now consistent with the open counts on
1188 * both sides, and we've completed the last operation that could
1189 * block, so it's safe to proceed with closing.
1192 if (--o_tty->count < 0) {
1193 printk(KERN_WARNING "release_dev: bad pty slave count "
1195 o_tty->count, tty_name(o_tty, buf));
1199 if (--tty->count < 0) {
1200 printk(KERN_WARNING "release_dev: bad tty->count (%d) for %s\n",
1201 tty->count, tty_name(tty, buf));
1206 * We've decremented tty->count, so we need to remove this file
1207 * descriptor off the tty->tty_files list; this serves two
1209 * - check_tty_count sees the correct number of file descriptors
1210 * associated with this tty.
1211 * - do_tty_hangup no longer sees this file descriptor as
1212 * something that needs to be handled for hangups.
1215 filp->private_data = NULL;
1218 * Perform some housekeeping before deciding whether to return.
1220 * Set the TTY_CLOSING flag if this was the last open. In the
1221 * case of a pty we may have to wait around for the other side
1222 * to close, and TTY_CLOSING makes sure we can't be reopened.
1225 set_bit(TTY_CLOSING, &tty->flags);
1227 set_bit(TTY_CLOSING, &o_tty->flags);
1230 * If _either_ side is closing, make sure there aren't any
1231 * processes that still think tty or o_tty is their controlling
1234 if (tty_closing || o_tty_closing) {
1235 struct task_struct *p;
1236 struct list_head *l;
1239 read_lock(&tasklist_lock);
1240 for_each_task_pid(tty->session, PIDTYPE_SID, p, l, pid)
1243 for_each_task_pid(o_tty->session, PIDTYPE_SID, p,l, pid)
1245 read_unlock(&tasklist_lock);
1248 /* check whether both sides are closing ... */
1249 if (!tty_closing || (o_tty && !o_tty_closing))
1252 #ifdef TTY_DEBUG_HANGUP
1253 printk(KERN_DEBUG "freeing tty structure...");
1257 * Shutdown the current line discipline, and reset it to N_TTY.
1258 * N.B. why reset ldisc when we're releasing the memory??
1260 if (tty->ldisc.close)
1261 (tty->ldisc.close)(tty);
1262 module_put(tty->ldisc.owner);
1264 tty->ldisc = ldiscs[N_TTY];
1265 tty->termios->c_line = N_TTY;
1267 if (o_tty->ldisc.close)
1268 (o_tty->ldisc.close)(o_tty);
1269 module_put(o_tty->ldisc.owner);
1270 o_tty->ldisc = ldiscs[N_TTY];
1274 * Prevent flush_to_ldisc() from rescheduling the work for later. Then
1275 * kill any delayed work.
1277 clear_bit(TTY_DONT_FLIP, &tty->flags);
1278 cancel_delayed_work(&tty->flip.work);
1281 * Wait for ->hangup_work and ->flip.work handlers to terminate
1283 flush_scheduled_work();
1286 * The release_mem function takes care of the details of clearing
1287 * the slots and preserving the termios structure.
1289 release_mem(tty, idx);
1293 * tty_open and tty_release keep up the tty count that contains the
1294 * number of opens done on a tty. We cannot use the inode-count, as
1295 * different inodes might point to the same tty.
1297 * Open-counting is needed for pty masters, as well as for keeping
1298 * track of serial lines: DTR is dropped when the last close happens.
1299 * (This is not done solely through tty->count, now. - Ted 1/27/92)
1301 * The termios state of a pty is reset on first open so that
1302 * settings don't persist across reuse.
1304 static int tty_open(struct inode * inode, struct file * filp)
1306 struct tty_struct *tty;
1308 struct tty_driver *driver;
1311 unsigned short saved_flags;
1313 saved_flags = filp->f_flags;
1315 noctty = filp->f_flags & O_NOCTTY;
1316 device = inode->i_rdev;
1317 if (IS_TTY_DEV(device)) {
1320 driver = current->tty->driver;
1321 index = current->tty->index;
1322 filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */
1327 if (IS_CONSOLE_DEV(device)) {
1328 extern int fg_console;
1329 extern struct tty_driver *console_driver;
1330 driver = console_driver;
1336 if (IS_SYSCONS_DEV(device)) {
1337 struct console *c = console_drivers;
1338 for (c = console_drivers; c; c = c->next) {
1341 driver = c->device(c, &index);
1344 /* Don't let /dev/console block */
1345 filp->f_flags |= O_NONBLOCK;
1352 if (IS_PTMX_DEV(device)) {
1353 #ifdef CONFIG_UNIX98_PTYS
1354 /* find a device that is not in use. */
1356 driver = ptm_driver;
1357 for (index = 0; index < driver->num ; index++)
1358 if (!init_dev(driver, index, &tty))
1359 goto ptmx_found; /* ok! */
1360 return -EIO; /* no free ptys */
1362 set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
1363 devpts_pty_new(index, MKDEV(pts_driver->major, pts_driver->minor_start) + index);
1367 #endif /* CONFIG_UNIX_98_PTYS */
1369 driver = get_tty_driver(kdev_t_to_nr(device), &index);
1373 retval = init_dev(driver, index, &tty);
1378 filp->private_data = tty;
1379 file_move(filp, &tty->tty_files);
1380 check_tty_count(tty, "tty_open");
1381 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
1382 tty->driver->subtype == PTY_TYPE_MASTER)
1384 #ifdef TTY_DEBUG_HANGUP
1385 printk(KERN_DEBUG "opening %s...", tty->name);
1387 if (tty->driver->open)
1388 retval = tty->driver->open(tty, filp);
1391 filp->f_flags = saved_flags;
1393 if (!retval && test_bit(TTY_EXCLUSIVE, &tty->flags) && !capable(CAP_SYS_ADMIN))
1397 #ifdef TTY_DEBUG_HANGUP
1398 printk(KERN_DEBUG "error %d in opening %s...", retval,
1403 if (retval != -ERESTARTSYS)
1405 if (signal_pending(current))
1409 * Need to reset f_op in case a hangup happened.
1411 filp->f_op = &tty_fops;
1417 tty->session == 0) {
1420 task_unlock(current);
1421 current->tty_old_pgrp = 0;
1422 tty->session = current->session;
1423 tty->pgrp = current->pgrp;
1428 static int tty_release(struct inode * inode, struct file * filp)
1436 /* No kernel lock held - fine */
1437 static unsigned int tty_poll(struct file * filp, poll_table * wait)
1439 struct tty_struct * tty;
1441 tty = (struct tty_struct *)filp->private_data;
1442 if (tty_paranoia_check(tty, filp->f_dentry->d_inode->i_rdev, "tty_poll"))
1445 if (tty->ldisc.poll)
1446 return (tty->ldisc.poll)(tty, filp, wait);
1450 static int tty_fasync(int fd, struct file * filp, int on)
1452 struct tty_struct * tty;
1455 tty = (struct tty_struct *)filp->private_data;
1456 if (tty_paranoia_check(tty, filp->f_dentry->d_inode->i_rdev, "tty_fasync"))
1459 retval = fasync_helper(fd, filp, on, &tty->fasync);
1464 if (!waitqueue_active(&tty->read_wait))
1465 tty->minimum_to_wake = 1;
1466 retval = f_setown(filp, (-tty->pgrp) ? : current->pid, 0);
1470 if (!tty->fasync && !waitqueue_active(&tty->read_wait))
1471 tty->minimum_to_wake = N_TTY_BUF_SIZE;
1476 static int tiocsti(struct tty_struct *tty, char * arg)
1480 if ((current->tty != tty) && !capable(CAP_SYS_ADMIN))
1482 if (get_user(ch, arg))
1484 tty->ldisc.receive_buf(tty, &ch, &mbz, 1);
1488 static int tiocgwinsz(struct tty_struct *tty, struct winsize * arg)
1490 if (copy_to_user(arg, &tty->winsize, sizeof(*arg)))
1495 static int tiocswinsz(struct tty_struct *tty, struct tty_struct *real_tty,
1496 struct winsize * arg)
1498 struct winsize tmp_ws;
1500 if (copy_from_user(&tmp_ws, arg, sizeof(*arg)))
1502 if (!memcmp(&tmp_ws, &tty->winsize, sizeof(*arg)))
1505 if (tty->driver->type == TTY_DRIVER_TYPE_CONSOLE) {
1506 unsigned int currcons = tty->index;
1507 if (vc_resize(currcons, tmp_ws.ws_col, tmp_ws.ws_row))
1512 kill_pg(tty->pgrp, SIGWINCH, 1);
1513 if ((real_tty->pgrp != tty->pgrp) && (real_tty->pgrp > 0))
1514 kill_pg(real_tty->pgrp, SIGWINCH, 1);
1515 tty->winsize = tmp_ws;
1516 real_tty->winsize = tmp_ws;
1520 static int tioccons(struct inode *inode, struct file *file)
1522 if (IS_SYSCONS_DEV(inode->i_rdev) ||
1523 IS_CONSOLE_DEV(inode->i_rdev)) {
1525 if (!capable(CAP_SYS_ADMIN))
1527 spin_lock(&redirect_lock);
1530 spin_unlock(&redirect_lock);
1535 spin_lock(&redirect_lock);
1537 spin_unlock(&redirect_lock);
1542 spin_unlock(&redirect_lock);
1547 static int fionbio(struct file *file, int *arg)
1551 if (get_user(nonblock, arg))
1555 file->f_flags |= O_NONBLOCK;
1557 file->f_flags &= ~O_NONBLOCK;
1561 static int tiocsctty(struct tty_struct *tty, int arg)
1563 struct list_head *l;
1567 if (current->leader &&
1568 (current->session == tty->session))
1571 * The process must be a session leader and
1572 * not have a controlling tty already.
1574 if (!current->leader || current->tty)
1576 if (tty->session > 0) {
1578 * This tty is already the controlling
1579 * tty for another session group!
1581 if ((arg == 1) && capable(CAP_SYS_ADMIN)) {
1586 read_lock(&tasklist_lock);
1587 for_each_task_pid(tty->session, PIDTYPE_SID, p, l, pid)
1589 read_unlock(&tasklist_lock);
1595 task_unlock(current);
1596 current->tty_old_pgrp = 0;
1597 tty->session = current->session;
1598 tty->pgrp = current->pgrp;
1602 static int tiocgpgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t *arg)
1605 * (tty == real_tty) is a cheap way of
1606 * testing if the tty is NOT a master pty.
1608 if (tty == real_tty && current->tty != real_tty)
1610 return put_user(real_tty->pgrp, arg);
1613 static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t *arg)
1616 int retval = tty_check_change(real_tty);
1622 if (!current->tty ||
1623 (current->tty != real_tty) ||
1624 (real_tty->session != current->session))
1626 if (get_user(pgrp, (pid_t *) arg))
1630 if (session_of_pgrp(pgrp) != current->session)
1632 real_tty->pgrp = pgrp;
1636 static int tiocgsid(struct tty_struct *tty, struct tty_struct *real_tty, pid_t *arg)
1639 * (tty == real_tty) is a cheap way of
1640 * testing if the tty is NOT a master pty.
1642 if (tty == real_tty && current->tty != real_tty)
1644 if (real_tty->session <= 0)
1646 return put_user(real_tty->session, arg);
1649 static int tiocsetd(struct tty_struct *tty, int *arg)
1653 if (get_user(ldisc, arg))
1655 return tty_set_ldisc(tty, ldisc);
1658 static int send_break(struct tty_struct *tty, int duration)
1660 set_current_state(TASK_INTERRUPTIBLE);
1662 tty->driver->break_ctl(tty, -1);
1663 if (!signal_pending(current))
1664 schedule_timeout(duration);
1665 tty->driver->break_ctl(tty, 0);
1666 if (signal_pending(current))
1672 tty_tiocmget(struct tty_struct *tty, struct file *file, unsigned long arg)
1674 int retval = -EINVAL;
1676 if (tty->driver->tiocmget) {
1677 retval = tty->driver->tiocmget(tty, file);
1680 retval = put_user(retval, (int *)arg);
1686 tty_tiocmset(struct tty_struct *tty, struct file *file, unsigned int cmd,
1689 int retval = -EINVAL;
1691 if (tty->driver->tiocmset) {
1692 unsigned int set, clear, val;
1694 retval = get_user(val, (unsigned int *)arg);
1712 set &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
1713 clear &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
1715 retval = tty->driver->tiocmset(tty, file, set, clear);
1721 * Split this up, as gcc can choke on it otherwise..
1723 int tty_ioctl(struct inode * inode, struct file * file,
1724 unsigned int cmd, unsigned long arg)
1726 struct tty_struct *tty, *real_tty;
1729 tty = (struct tty_struct *)file->private_data;
1730 if (tty_paranoia_check(tty, inode->i_rdev, "tty_ioctl"))
1734 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
1735 tty->driver->subtype == PTY_TYPE_MASTER)
1736 real_tty = tty->link;
1739 * Break handling by driver
1741 if (!tty->driver->break_ctl) {
1745 if (tty->driver->ioctl)
1746 return tty->driver->ioctl(tty, file, cmd, arg);
1749 /* These two ioctl's always return success; even if */
1750 /* the driver doesn't support them. */
1753 if (!tty->driver->ioctl)
1755 retval = tty->driver->ioctl(tty, file, cmd, arg);
1756 if (retval == -ENOIOCTLCMD)
1763 * Factor out some common prep work
1771 retval = tty_check_change(tty);
1774 if (cmd != TIOCCBRK) {
1775 tty_wait_until_sent(tty, 0);
1776 if (signal_pending(current))
1784 return tiocsti(tty, (char *)arg);
1786 return tiocgwinsz(tty, (struct winsize *) arg);
1788 return tiocswinsz(tty, real_tty, (struct winsize *) arg);
1790 return real_tty!=tty ? -EINVAL : tioccons(inode, file);
1792 return fionbio(file, (int *) arg);
1794 set_bit(TTY_EXCLUSIVE, &tty->flags);
1797 clear_bit(TTY_EXCLUSIVE, &tty->flags);
1800 if (current->tty != tty)
1802 if (current->leader)
1803 disassociate_ctty(0);
1805 current->tty = NULL;
1806 task_unlock(current);
1809 return tiocsctty(tty, arg);
1811 return tiocgpgrp(tty, real_tty, (pid_t *) arg);
1813 return tiocspgrp(tty, real_tty, (pid_t *) arg);
1815 return tiocgsid(tty, real_tty, (pid_t *) arg);
1817 return put_user(tty->ldisc.num, (int *) arg);
1819 return tiocsetd(tty, (int *) arg);
1822 return tioclinux(tty, arg);
1827 case TIOCSBRK: /* Turn break on, unconditionally */
1828 tty->driver->break_ctl(tty, -1);
1831 case TIOCCBRK: /* Turn break off, unconditionally */
1832 tty->driver->break_ctl(tty, 0);
1834 case TCSBRK: /* SVID version: non-zero arg --> no break */
1836 * XXX is the above comment correct, or the
1837 * code below correct? Is this ioctl used at
1841 return send_break(tty, HZ/4);
1843 case TCSBRKP: /* support for POSIX tcsendbreak() */
1844 return send_break(tty, arg ? arg*(HZ/10) : HZ/4);
1847 return tty_tiocmget(tty, file, arg);
1852 return tty_tiocmset(tty, file, cmd, arg);
1854 if (tty->driver->ioctl) {
1855 int retval = (tty->driver->ioctl)(tty, file, cmd, arg);
1856 if (retval != -ENOIOCTLCMD)
1859 if (tty->ldisc.ioctl) {
1860 int retval = (tty->ldisc.ioctl)(tty, file, cmd, arg);
1861 if (retval != -ENOIOCTLCMD)
1869 * This implements the "Secure Attention Key" --- the idea is to
1870 * prevent trojan horses by killing all processes associated with this
1871 * tty when the user hits the "Secure Attention Key". Required for
1872 * super-paranoid applications --- see the Orange Book for more details.
1874 * This code could be nicer; ideally it should send a HUP, wait a few
1875 * seconds, then send a INT, and then a KILL signal. But you then
1876 * have to coordinate with the init process, since all processes associated
1877 * with the current tty must be dead before the new getty is allowed
1880 * Now, if it would be correct ;-/ The current code has a nasty hole -
1881 * it doesn't catch files in flight. We may send the descriptor to ourselves
1882 * via AF_UNIX socket, close it and later fetch from socket. FIXME.
1884 * Nasty bug: do_SAK is being called in interrupt context. This can
1885 * deadlock. We punt it up to process context. AKPM - 16Mar2001
1887 static void __do_SAK(void *arg)
1892 struct tty_struct *tty = arg;
1893 struct task_struct *p;
1894 struct list_head *l;
1902 session = tty->session;
1903 if (tty->ldisc.flush_buffer)
1904 tty->ldisc.flush_buffer(tty);
1905 if (tty->driver->flush_buffer)
1906 tty->driver->flush_buffer(tty);
1907 read_lock(&tasklist_lock);
1908 for_each_task_pid(session, PIDTYPE_SID, p, l, pid) {
1909 if (p->tty == tty || session > 0) {
1910 printk(KERN_NOTICE "SAK: killed process %d"
1911 " (%s): p->session==tty->session\n",
1913 send_sig(SIGKILL, p, 1);
1918 spin_lock(&p->files->file_lock);
1919 for (i=0; i < p->files->max_fds; i++) {
1920 filp = fcheck_files(p->files, i);
1921 if (filp && (filp->f_op == &tty_fops) &&
1922 (filp->private_data == tty)) {
1923 printk(KERN_NOTICE "SAK: killed process %d"
1924 " (%s): fd#%d opened to the tty\n",
1925 p->pid, p->comm, i);
1926 send_sig(SIGKILL, p, 1);
1930 spin_unlock(&p->files->file_lock);
1934 read_unlock(&tasklist_lock);
1939 * The tq handling here is a little racy - tty->SAK_work may already be queued.
1940 * Fortunately we don't need to worry, because if ->SAK_work is already queued,
1941 * the values which we write to it will be identical to the values which it
1942 * already has. --akpm
1944 void do_SAK(struct tty_struct *tty)
1948 PREPARE_WORK(&tty->SAK_work, __do_SAK, tty);
1949 schedule_work(&tty->SAK_work);
1952 EXPORT_SYMBOL(do_SAK);
1955 * This routine is called out of the software interrupt to flush data
1956 * from the flip buffer to the line discipline.
1958 static void flush_to_ldisc(void *private_)
1960 struct tty_struct *tty = (struct tty_struct *) private_;
1964 unsigned long flags;
1966 if (test_bit(TTY_DONT_FLIP, &tty->flags)) {
1968 * Do it after the next timer tick:
1970 schedule_delayed_work(&tty->flip.work, 1);
1974 spin_lock_irqsave(&tty->read_lock, flags);
1975 if (tty->flip.buf_num) {
1976 cp = tty->flip.char_buf + TTY_FLIPBUF_SIZE;
1977 fp = tty->flip.flag_buf + TTY_FLIPBUF_SIZE;
1978 tty->flip.buf_num = 0;
1979 tty->flip.char_buf_ptr = tty->flip.char_buf;
1980 tty->flip.flag_buf_ptr = tty->flip.flag_buf;
1982 cp = tty->flip.char_buf;
1983 fp = tty->flip.flag_buf;
1984 tty->flip.buf_num = 1;
1985 tty->flip.char_buf_ptr = tty->flip.char_buf + TTY_FLIPBUF_SIZE;
1986 tty->flip.flag_buf_ptr = tty->flip.flag_buf + TTY_FLIPBUF_SIZE;
1988 count = tty->flip.count;
1989 tty->flip.count = 0;
1990 spin_unlock_irqrestore(&tty->read_lock, flags);
1992 tty->ldisc.receive_buf(tty, cp, fp, count);
1996 * Routine which returns the baud rate of the tty
1998 * Note that the baud_table needs to be kept in sync with the
1999 * include/asm/termbits.h file.
2001 static int baud_table[] = {
2002 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
2003 9600, 19200, 38400, 57600, 115200, 230400, 460800,
2005 76800, 153600, 307200, 614400, 921600
2007 500000, 576000, 921600, 1000000, 1152000, 1500000, 2000000,
2008 2500000, 3000000, 3500000, 4000000
2012 static int n_baud_table = ARRAY_SIZE(baud_table);
2014 int tty_termios_baud_rate(struct termios *termios)
2016 unsigned int cbaud = termios->c_cflag & CBAUD;
2018 if (cbaud & CBAUDEX) {
2021 if (cbaud < 1 || cbaud + 15 > n_baud_table)
2022 termios->c_cflag &= ~CBAUDEX;
2027 return baud_table[cbaud];
2030 EXPORT_SYMBOL(tty_termios_baud_rate);
2032 int tty_get_baud_rate(struct tty_struct *tty)
2034 int baud = tty_termios_baud_rate(tty->termios);
2036 if (baud == 38400 && tty->alt_speed) {
2038 printk(KERN_WARNING "Use of setserial/setrocket to "
2039 "set SPD_* flags is deprecated\n");
2042 baud = tty->alt_speed;
2048 EXPORT_SYMBOL(tty_get_baud_rate);
2050 void tty_flip_buffer_push(struct tty_struct *tty)
2052 if (tty->low_latency)
2053 flush_to_ldisc((void *) tty);
2055 schedule_delayed_work(&tty->flip.work, 1);
2059 * This subroutine initializes a tty structure.
2061 static void initialize_tty_struct(struct tty_struct *tty)
2063 memset(tty, 0, sizeof(struct tty_struct));
2064 tty->magic = TTY_MAGIC;
2065 tty->ldisc = ldiscs[N_TTY];
2067 tty->flip.char_buf_ptr = tty->flip.char_buf;
2068 tty->flip.flag_buf_ptr = tty->flip.flag_buf;
2069 INIT_WORK(&tty->flip.work, flush_to_ldisc, tty);
2070 init_MUTEX(&tty->flip.pty_sem);
2071 init_waitqueue_head(&tty->write_wait);
2072 init_waitqueue_head(&tty->read_wait);
2073 INIT_WORK(&tty->hangup_work, do_tty_hangup, tty);
2074 sema_init(&tty->atomic_read, 1);
2075 sema_init(&tty->atomic_write, 1);
2076 spin_lock_init(&tty->read_lock);
2077 INIT_LIST_HEAD(&tty->tty_files);
2078 INIT_WORK(&tty->SAK_work, NULL, NULL);
2082 * The default put_char routine if the driver did not define one.
2084 static void tty_default_put_char(struct tty_struct *tty, unsigned char ch)
2086 tty->driver->write(tty, 0, &ch, 1);
2090 struct list_head node;
2092 struct class_device class_dev;
2094 #define to_tty_dev(d) container_of(d, struct tty_dev, class_dev)
2096 static void release_tty_dev(struct class_device *class_dev)
2098 struct tty_dev *tty_dev = to_tty_dev(class_dev);
2102 static struct class tty_class = {
2104 .release = &release_tty_dev,
2107 static LIST_HEAD(tty_dev_list);
2108 static spinlock_t tty_dev_list_lock = SPIN_LOCK_UNLOCKED;
2110 static ssize_t show_dev(struct class_device *class_dev, char *buf)
2112 struct tty_dev *tty_dev = to_tty_dev(class_dev);
2113 return print_dev_t(buf, tty_dev->dev);
2115 static CLASS_DEVICE_ATTR(dev, S_IRUGO, show_dev, NULL);
2117 static void tty_add_class_device(char *name, dev_t dev, struct device *device)
2119 struct tty_dev *tty_dev = NULL;
2122 tty_dev = kmalloc(sizeof(*tty_dev), GFP_KERNEL);
2125 memset(tty_dev, 0x00, sizeof(*tty_dev));
2127 tty_dev->class_dev.dev = device;
2128 tty_dev->class_dev.class = &tty_class;
2129 snprintf(tty_dev->class_dev.class_id, BUS_ID_SIZE, "%s", name);
2130 retval = class_device_register(&tty_dev->class_dev);
2133 class_device_create_file (&tty_dev->class_dev, &class_device_attr_dev);
2135 spin_lock(&tty_dev_list_lock);
2136 list_add(&tty_dev->node, &tty_dev_list);
2137 spin_unlock(&tty_dev_list_lock);
2143 void tty_remove_class_device(dev_t dev)
2145 struct tty_dev *tty_dev = NULL;
2146 struct list_head *tmp;
2149 spin_lock(&tty_dev_list_lock);
2150 list_for_each (tmp, &tty_dev_list) {
2151 tty_dev = list_entry(tmp, struct tty_dev, node);
2152 if ((MAJOR(tty_dev->dev) == MAJOR(dev)) &&
2153 (MINOR(tty_dev->dev) == MINOR(dev))) {
2159 list_del(&tty_dev->node);
2160 spin_unlock(&tty_dev_list_lock);
2161 class_device_unregister(&tty_dev->class_dev);
2163 spin_unlock(&tty_dev_list_lock);
2168 * tty_register_device - register a tty device
2169 * @driver: the tty driver that describes the tty device
2170 * @index: the index in the tty driver for this tty device
2171 * @device: a struct device that is associated with this tty device.
2172 * This field is optional, if there is no known struct device for this
2173 * tty device it can be set to NULL safely.
2175 * This call is required to be made to register an individual tty device if
2176 * the tty driver's flags have the TTY_DRIVER_NO_DEVFS bit set. If that
2177 * bit is not set, this function should not be called.
2179 void tty_register_device(struct tty_driver *driver, unsigned index,
2180 struct device *device)
2182 dev_t dev = MKDEV(driver->major, driver->minor_start) + index;
2184 if (index >= driver->num) {
2185 printk(KERN_ERR "Attempt to register invalid tty line number "
2190 devfs_mk_cdev(dev, S_IFCHR | S_IRUSR | S_IWUSR,
2191 "%s%d", driver->devfs_name, index + driver->name_base);
2193 /* we don't care about the ptys */
2194 /* how nice to hide this behind some crappy interface.. */
2195 if (driver->type != TTY_DRIVER_TYPE_PTY) {
2197 tty_line_name(driver, index, name);
2198 tty_add_class_device(name, dev, device);
2203 * tty_unregister_device - unregister a tty device
2204 * @driver: the tty driver that describes the tty device
2205 * @index: the index in the tty driver for this tty device
2207 * If a tty device is registered with a call to tty_register_device() then
2208 * this function must be made when the tty device is gone.
2210 void tty_unregister_device(struct tty_driver *driver, unsigned index)
2212 devfs_remove("%s%d", driver->devfs_name, index + driver->name_base);
2213 tty_remove_class_device(MKDEV(driver->major, driver->minor_start) + index);
2216 EXPORT_SYMBOL(tty_register_device);
2217 EXPORT_SYMBOL(tty_unregister_device);
2219 static struct kobject tty_kobj = {.name = "tty"};
2221 struct tty_driver *alloc_tty_driver(int lines)
2223 struct tty_driver *driver;
2225 driver = kmalloc(sizeof(struct tty_driver), GFP_KERNEL);
2227 memset(driver, 0, sizeof(struct tty_driver));
2228 driver->magic = TTY_DRIVER_MAGIC;
2229 driver->num = lines;
2230 /* later we'll move allocation of tables here */
2235 void put_tty_driver(struct tty_driver *driver)
2240 void tty_set_operations(struct tty_driver *driver, struct tty_operations *op)
2242 driver->open = op->open;
2243 driver->close = op->close;
2244 driver->write = op->write;
2245 driver->put_char = op->put_char;
2246 driver->flush_chars = op->flush_chars;
2247 driver->write_room = op->write_room;
2248 driver->chars_in_buffer = op->chars_in_buffer;
2249 driver->ioctl = op->ioctl;
2250 driver->set_termios = op->set_termios;
2251 driver->throttle = op->throttle;
2252 driver->unthrottle = op->unthrottle;
2253 driver->stop = op->stop;
2254 driver->start = op->start;
2255 driver->hangup = op->hangup;
2256 driver->break_ctl = op->break_ctl;
2257 driver->flush_buffer = op->flush_buffer;
2258 driver->set_ldisc = op->set_ldisc;
2259 driver->wait_until_sent = op->wait_until_sent;
2260 driver->send_xchar = op->send_xchar;
2261 driver->read_proc = op->read_proc;
2262 driver->write_proc = op->write_proc;
2263 driver->tiocmget = op->tiocmget;
2264 driver->tiocmset = op->tiocmset;
2268 EXPORT_SYMBOL(alloc_tty_driver);
2269 EXPORT_SYMBOL(put_tty_driver);
2270 EXPORT_SYMBOL(tty_set_operations);
2273 * Called by a tty driver to register itself.
2275 int tty_register_driver(struct tty_driver *driver)
2283 if (driver->flags & TTY_DRIVER_INSTALLED)
2286 p = kmalloc(driver->num * 3 * sizeof(void *), GFP_KERNEL);
2289 memset(p, 0, driver->num * 3 * sizeof(void *));
2291 if (!driver->major) {
2292 error = alloc_chrdev_region(&dev, driver->minor_start, driver->num,
2293 (char*)driver->name);
2295 driver->major = MAJOR(dev);
2296 driver->minor_start = MINOR(dev);
2299 dev = MKDEV(driver->major, driver->minor_start);
2300 error = register_chrdev_region(dev, driver->num,
2301 (char*)driver->name);
2308 driver->ttys = (struct tty_struct **)p;
2309 driver->termios = (struct termios **)(p + driver->num);
2310 driver->termios_locked = (struct termios **)(p + driver->num * 2);
2312 driver->cdev.kobj.parent = &tty_kobj;
2313 strcpy(driver->cdev.kobj.name, driver->name);
2314 for (s = strchr(driver->cdev.kobj.name, '/'); s; s = strchr(s, '/'))
2316 cdev_init(&driver->cdev, &tty_fops);
2317 driver->cdev.owner = driver->owner;
2318 error = cdev_add(&driver->cdev, dev, driver->num);
2320 kobject_del(&driver->cdev.kobj);
2321 unregister_chrdev_region(dev, driver->num);
2322 driver->ttys = NULL;
2323 driver->termios = driver->termios_locked = NULL;
2328 if (!driver->put_char)
2329 driver->put_char = tty_default_put_char;
2331 list_add(&driver->tty_drivers, &tty_drivers);
2333 if ( !(driver->flags & TTY_DRIVER_NO_DEVFS) ) {
2334 for(i = 0; i < driver->num; i++)
2335 tty_register_device(driver, i, NULL);
2337 proc_tty_register_driver(driver);
2342 * Called by a tty driver to unregister itself.
2344 int tty_unregister_driver(struct tty_driver *driver)
2350 if (driver->refcount)
2353 cdev_unmap(MKDEV(driver->major, driver->minor_start), driver->num);
2354 unregister_chrdev_region(MKDEV(driver->major, driver->minor_start),
2357 list_del(&driver->tty_drivers);
2360 * Free the termios and termios_locked structures because
2361 * we don't want to get memory leaks when modular tty
2362 * drivers are removed from the kernel.
2364 for (i = 0; i < driver->num; i++) {
2365 tp = driver->termios[i];
2367 driver->termios[i] = NULL;
2370 tp = driver->termios_locked[i];
2372 driver->termios_locked[i] = NULL;
2375 if (!(driver->flags & TTY_DRIVER_NO_DEVFS))
2376 tty_unregister_device(driver, i);
2379 proc_tty_unregister_driver(driver);
2380 driver->ttys = NULL;
2381 driver->termios = driver->termios_locked = NULL;
2383 cdev_del(&driver->cdev);
2389 * Initialize the console device. This is called *early*, so
2390 * we can't necessarily depend on lots of kernel help here.
2391 * Just do some early initializations, and do the complex setup
2394 void __init console_init(void)
2398 /* Setup the default TTY line discipline. */
2399 (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);
2402 * set up the console device so that later boot sequences can
2403 * inform about problems etc..
2405 #ifdef CONFIG_EARLY_PRINTK
2406 disable_early_printk();
2408 #ifdef CONFIG_SERIAL_68360
2409 /* This is not a console initcall. I know not what it's doing here.
2410 So I haven't moved it. dwmw2 */
2413 call = &__con_initcall_start;
2414 while (call < &__con_initcall_end) {
2421 extern int vty_init(void);
2424 static int __init tty_class_init(void)
2426 return class_register(&tty_class);
2429 postcore_initcall(tty_class_init);
2431 static struct cdev tty_cdev, console_cdev;
2432 #ifdef CONFIG_UNIX98_PTYS
2433 static struct cdev ptmx_cdev;
2436 static struct cdev vc0_cdev;
2440 * Ok, now we can initialize the rest of the tty devices and can count
2441 * on memory allocations, interrupts etc..
2443 void __init tty_init(void)
2445 strcpy(tty_cdev.kobj.name, "dev.tty");
2446 cdev_init(&tty_cdev, &tty_fops);
2447 if (cdev_add(&tty_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) ||
2448 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0)
2449 panic("Couldn't register /dev/tty driver\n");
2450 devfs_mk_cdev(MKDEV(TTYAUX_MAJOR, 0), S_IFCHR|S_IRUGO|S_IWUGO, "tty");
2451 tty_add_class_device ("tty", MKDEV(TTYAUX_MAJOR, 0), NULL);
2453 strcpy(console_cdev.kobj.name, "dev.console");
2454 cdev_init(&console_cdev, &tty_fops);
2455 if (cdev_add(&console_cdev, MKDEV(TTYAUX_MAJOR, 1), 1) ||
2456 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 1), 1, "/dev/console") < 0)
2457 panic("Couldn't register /dev/console driver\n");
2458 devfs_mk_cdev(MKDEV(TTYAUX_MAJOR, 1), S_IFCHR|S_IRUSR|S_IWUSR, "console");
2459 tty_add_class_device ("console", MKDEV(TTYAUX_MAJOR, 1), NULL);
2461 tty_kobj.kset = tty_cdev.kobj.kset;
2462 kobject_register(&tty_kobj);
2464 #ifdef CONFIG_UNIX98_PTYS
2465 strcpy(ptmx_cdev.kobj.name, "dev.ptmx");
2466 cdev_init(&ptmx_cdev, &tty_fops);
2467 if (cdev_add(&ptmx_cdev, MKDEV(TTYAUX_MAJOR, 2), 1) ||
2468 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 2), 1, "/dev/ptmx") < 0)
2469 panic("Couldn't register /dev/ptmx driver\n");
2470 devfs_mk_cdev(MKDEV(TTYAUX_MAJOR, 2), S_IFCHR|S_IRUGO|S_IWUGO, "ptmx");
2471 tty_add_class_device ("ptmx", MKDEV(TTYAUX_MAJOR, 2), NULL);
2475 strcpy(vc0_cdev.kobj.name, "dev.vc0");
2476 cdev_init(&vc0_cdev, &tty_fops);
2477 if (cdev_add(&vc0_cdev, MKDEV(TTY_MAJOR, 0), 1) ||
2478 register_chrdev_region(MKDEV(TTY_MAJOR, 0), 1, "/dev/vc/0") < 0)
2479 panic("Couldn't register /dev/tty0 driver\n");
2480 devfs_mk_cdev(MKDEV(TTY_MAJOR, 0), S_IFCHR|S_IRUSR|S_IWUSR, "vc/0");
2481 tty_add_class_device ("tty0", MKDEV(TTY_MAJOR, 0), NULL);
2486 #ifdef CONFIG_ESPSERIAL /* init ESP before rs, so rs doesn't see the port */
2489 #if defined(CONFIG_MVME162_SCC) || defined(CONFIG_BVME6000_SCC) || defined(CONFIG_MVME147_SCC)
2492 #ifdef CONFIG_SERIAL_TX3912
2495 #ifdef CONFIG_ROCKETPORT
2498 #ifdef CONFIG_SERIAL167
2501 #ifdef CONFIG_CYCLADES
2504 #ifdef CONFIG_STALLION
2507 #ifdef CONFIG_ISTALLION
2513 #ifdef CONFIG_DIGIEPCA
2516 #ifdef CONFIG_SPECIALIX
2519 #if (defined(CONFIG_8xx) || defined(CONFIG_8260))
2521 #endif /* CONFIG_8xx */
2523 #ifdef CONFIG_MOXA_INTELLIO
2526 #ifdef CONFIG_TN3270