xref: /openbmc/linux/drivers/tty/tty_io.c (revision 6e9430ac)
196fd7ce5SGreg Kroah-Hartman /*
296fd7ce5SGreg Kroah-Hartman  *  Copyright (C) 1991, 1992  Linus Torvalds
396fd7ce5SGreg Kroah-Hartman  */
496fd7ce5SGreg Kroah-Hartman 
596fd7ce5SGreg Kroah-Hartman /*
696fd7ce5SGreg Kroah-Hartman  * 'tty_io.c' gives an orthogonal feeling to tty's, be they consoles
796fd7ce5SGreg Kroah-Hartman  * or rs-channels. It also implements echoing, cooked mode etc.
896fd7ce5SGreg Kroah-Hartman  *
996fd7ce5SGreg Kroah-Hartman  * Kill-line thanks to John T Kohl, who also corrected VMIN = VTIME = 0.
1096fd7ce5SGreg Kroah-Hartman  *
1196fd7ce5SGreg Kroah-Hartman  * Modified by Theodore Ts'o, 9/14/92, to dynamically allocate the
1296fd7ce5SGreg Kroah-Hartman  * tty_struct and tty_queue structures.  Previously there was an array
1396fd7ce5SGreg Kroah-Hartman  * of 256 tty_struct's which was statically allocated, and the
1496fd7ce5SGreg Kroah-Hartman  * tty_queue structures were allocated at boot time.  Both are now
1596fd7ce5SGreg Kroah-Hartman  * dynamically allocated only when the tty is open.
1696fd7ce5SGreg Kroah-Hartman  *
1796fd7ce5SGreg Kroah-Hartman  * Also restructured routines so that there is more of a separation
1896fd7ce5SGreg Kroah-Hartman  * between the high-level tty routines (tty_io.c and tty_ioctl.c) and
1996fd7ce5SGreg Kroah-Hartman  * the low-level tty routines (serial.c, pty.c, console.c).  This
2096fd7ce5SGreg Kroah-Hartman  * makes for cleaner and more compact code.  -TYT, 9/17/92
2196fd7ce5SGreg Kroah-Hartman  *
2296fd7ce5SGreg Kroah-Hartman  * Modified by Fred N. van Kempen, 01/29/93, to add line disciplines
2396fd7ce5SGreg Kroah-Hartman  * which can be dynamically activated and de-activated by the line
2496fd7ce5SGreg Kroah-Hartman  * discipline handling modules (like SLIP).
2596fd7ce5SGreg Kroah-Hartman  *
2696fd7ce5SGreg Kroah-Hartman  * NOTE: pay no attention to the line discipline code (yet); its
2796fd7ce5SGreg Kroah-Hartman  * interface is still subject to change in this version...
2896fd7ce5SGreg Kroah-Hartman  * -- TYT, 1/31/92
2996fd7ce5SGreg Kroah-Hartman  *
3096fd7ce5SGreg Kroah-Hartman  * Added functionality to the OPOST tty handling.  No delays, but all
3196fd7ce5SGreg Kroah-Hartman  * other bits should be there.
3296fd7ce5SGreg Kroah-Hartman  *	-- Nick Holloway <alfie@dcs.warwick.ac.uk>, 27th May 1993.
3396fd7ce5SGreg Kroah-Hartman  *
3496fd7ce5SGreg Kroah-Hartman  * Rewrote canonical mode and added more termios flags.
3596fd7ce5SGreg Kroah-Hartman  * 	-- julian@uhunix.uhcc.hawaii.edu (J. Cowley), 13Jan94
3696fd7ce5SGreg Kroah-Hartman  *
3796fd7ce5SGreg Kroah-Hartman  * Reorganized FASYNC support so mouse code can share it.
3896fd7ce5SGreg Kroah-Hartman  *	-- ctm@ardi.com, 9Sep95
3996fd7ce5SGreg Kroah-Hartman  *
4096fd7ce5SGreg Kroah-Hartman  * New TIOCLINUX variants added.
4196fd7ce5SGreg Kroah-Hartman  *	-- mj@k332.feld.cvut.cz, 19-Nov-95
4296fd7ce5SGreg Kroah-Hartman  *
4396fd7ce5SGreg Kroah-Hartman  * Restrict vt switching via ioctl()
4496fd7ce5SGreg Kroah-Hartman  *      -- grif@cs.ucr.edu, 5-Dec-95
4596fd7ce5SGreg Kroah-Hartman  *
4696fd7ce5SGreg Kroah-Hartman  * Move console and virtual terminal code to more appropriate files,
4796fd7ce5SGreg Kroah-Hartman  * implement CONFIG_VT and generalize console device interface.
4896fd7ce5SGreg Kroah-Hartman  *	-- Marko Kohtala <Marko.Kohtala@hut.fi>, March 97
4996fd7ce5SGreg Kroah-Hartman  *
5096fd7ce5SGreg Kroah-Hartman  * Rewrote tty_init_dev and tty_release_dev to eliminate races.
5196fd7ce5SGreg Kroah-Hartman  *	-- Bill Hawes <whawes@star.net>, June 97
5296fd7ce5SGreg Kroah-Hartman  *
5396fd7ce5SGreg Kroah-Hartman  * Added devfs support.
5496fd7ce5SGreg Kroah-Hartman  *      -- C. Scott Ananian <cananian@alumni.princeton.edu>, 13-Jan-1998
5596fd7ce5SGreg Kroah-Hartman  *
5696fd7ce5SGreg Kroah-Hartman  * Added support for a Unix98-style ptmx device.
5796fd7ce5SGreg Kroah-Hartman  *      -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998
5896fd7ce5SGreg Kroah-Hartman  *
5996fd7ce5SGreg Kroah-Hartman  * Reduced memory usage for older ARM systems
6096fd7ce5SGreg Kroah-Hartman  *      -- Russell King <rmk@arm.linux.org.uk>
6196fd7ce5SGreg Kroah-Hartman  *
6296fd7ce5SGreg Kroah-Hartman  * Move do_SAK() into process context.  Less stack use in devfs functions.
6396fd7ce5SGreg Kroah-Hartman  * alloc_tty_struct() always uses kmalloc()
6496fd7ce5SGreg Kroah-Hartman  *			 -- Andrew Morton <andrewm@uow.edu.eu> 17Mar01
6596fd7ce5SGreg Kroah-Hartman  */
6696fd7ce5SGreg Kroah-Hartman 
6796fd7ce5SGreg Kroah-Hartman #include <linux/types.h>
6896fd7ce5SGreg Kroah-Hartman #include <linux/major.h>
6996fd7ce5SGreg Kroah-Hartman #include <linux/errno.h>
7096fd7ce5SGreg Kroah-Hartman #include <linux/signal.h>
7196fd7ce5SGreg Kroah-Hartman #include <linux/fcntl.h>
7296fd7ce5SGreg Kroah-Hartman #include <linux/sched.h>
7396fd7ce5SGreg Kroah-Hartman #include <linux/interrupt.h>
7496fd7ce5SGreg Kroah-Hartman #include <linux/tty.h>
7596fd7ce5SGreg Kroah-Hartman #include <linux/tty_driver.h>
7696fd7ce5SGreg Kroah-Hartman #include <linux/tty_flip.h>
7796fd7ce5SGreg Kroah-Hartman #include <linux/devpts_fs.h>
7896fd7ce5SGreg Kroah-Hartman #include <linux/file.h>
7996fd7ce5SGreg Kroah-Hartman #include <linux/fdtable.h>
8096fd7ce5SGreg Kroah-Hartman #include <linux/console.h>
8196fd7ce5SGreg Kroah-Hartman #include <linux/timer.h>
8296fd7ce5SGreg Kroah-Hartman #include <linux/ctype.h>
8396fd7ce5SGreg Kroah-Hartman #include <linux/kd.h>
8496fd7ce5SGreg Kroah-Hartman #include <linux/mm.h>
8596fd7ce5SGreg Kroah-Hartman #include <linux/string.h>
8696fd7ce5SGreg Kroah-Hartman #include <linux/slab.h>
8796fd7ce5SGreg Kroah-Hartman #include <linux/poll.h>
8896fd7ce5SGreg Kroah-Hartman #include <linux/proc_fs.h>
8996fd7ce5SGreg Kroah-Hartman #include <linux/init.h>
9096fd7ce5SGreg Kroah-Hartman #include <linux/module.h>
9196fd7ce5SGreg Kroah-Hartman #include <linux/device.h>
9296fd7ce5SGreg Kroah-Hartman #include <linux/wait.h>
9396fd7ce5SGreg Kroah-Hartman #include <linux/bitops.h>
9496fd7ce5SGreg Kroah-Hartman #include <linux/delay.h>
9596fd7ce5SGreg Kroah-Hartman #include <linux/seq_file.h>
9696fd7ce5SGreg Kroah-Hartman #include <linux/serial.h>
975a3c6b25SManuel Zerpies #include <linux/ratelimit.h>
9896fd7ce5SGreg Kroah-Hartman 
9996fd7ce5SGreg Kroah-Hartman #include <linux/uaccess.h>
10096fd7ce5SGreg Kroah-Hartman 
10196fd7ce5SGreg Kroah-Hartman #include <linux/kbd_kern.h>
10296fd7ce5SGreg Kroah-Hartman #include <linux/vt_kern.h>
10396fd7ce5SGreg Kroah-Hartman #include <linux/selection.h>
10496fd7ce5SGreg Kroah-Hartman 
10596fd7ce5SGreg Kroah-Hartman #include <linux/kmod.h>
10696fd7ce5SGreg Kroah-Hartman #include <linux/nsproxy.h>
10796fd7ce5SGreg Kroah-Hartman 
10896fd7ce5SGreg Kroah-Hartman #undef TTY_DEBUG_HANGUP
10996fd7ce5SGreg Kroah-Hartman 
11096fd7ce5SGreg Kroah-Hartman #define TTY_PARANOIA_CHECK 1
11196fd7ce5SGreg Kroah-Hartman #define CHECK_TTY_COUNT 1
11296fd7ce5SGreg Kroah-Hartman 
11396fd7ce5SGreg Kroah-Hartman struct ktermios tty_std_termios = {	/* for the benefit of tty drivers  */
11496fd7ce5SGreg Kroah-Hartman 	.c_iflag = ICRNL | IXON,
11596fd7ce5SGreg Kroah-Hartman 	.c_oflag = OPOST | ONLCR,
11696fd7ce5SGreg Kroah-Hartman 	.c_cflag = B38400 | CS8 | CREAD | HUPCL,
11796fd7ce5SGreg Kroah-Hartman 	.c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK |
11896fd7ce5SGreg Kroah-Hartman 		   ECHOCTL | ECHOKE | IEXTEN,
11996fd7ce5SGreg Kroah-Hartman 	.c_cc = INIT_C_CC,
12096fd7ce5SGreg Kroah-Hartman 	.c_ispeed = 38400,
12196fd7ce5SGreg Kroah-Hartman 	.c_ospeed = 38400
12296fd7ce5SGreg Kroah-Hartman };
12396fd7ce5SGreg Kroah-Hartman 
12496fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_std_termios);
12596fd7ce5SGreg Kroah-Hartman 
12696fd7ce5SGreg Kroah-Hartman /* This list gets poked at by procfs and various bits of boot up code. This
12796fd7ce5SGreg Kroah-Hartman    could do with some rationalisation such as pulling the tty proc function
12896fd7ce5SGreg Kroah-Hartman    into this file */
12996fd7ce5SGreg Kroah-Hartman 
13096fd7ce5SGreg Kroah-Hartman LIST_HEAD(tty_drivers);			/* linked list of tty drivers */
13196fd7ce5SGreg Kroah-Hartman 
13296fd7ce5SGreg Kroah-Hartman /* Mutex to protect creating and releasing a tty. This is shared with
13396fd7ce5SGreg Kroah-Hartman    vt.c for deeply disgusting hack reasons */
13496fd7ce5SGreg Kroah-Hartman DEFINE_MUTEX(tty_mutex);
13596fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_mutex);
13696fd7ce5SGreg Kroah-Hartman 
13796fd7ce5SGreg Kroah-Hartman /* Spinlock to protect the tty->tty_files list */
13896fd7ce5SGreg Kroah-Hartman DEFINE_SPINLOCK(tty_files_lock);
13996fd7ce5SGreg Kroah-Hartman 
14096fd7ce5SGreg Kroah-Hartman static ssize_t tty_read(struct file *, char __user *, size_t, loff_t *);
14196fd7ce5SGreg Kroah-Hartman static ssize_t tty_write(struct file *, const char __user *, size_t, loff_t *);
14296fd7ce5SGreg Kroah-Hartman ssize_t redirected_tty_write(struct file *, const char __user *,
14396fd7ce5SGreg Kroah-Hartman 							size_t, loff_t *);
14496fd7ce5SGreg Kroah-Hartman static unsigned int tty_poll(struct file *, poll_table *);
14596fd7ce5SGreg Kroah-Hartman static int tty_open(struct inode *, struct file *);
14696fd7ce5SGreg Kroah-Hartman long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
14796fd7ce5SGreg Kroah-Hartman #ifdef CONFIG_COMPAT
14896fd7ce5SGreg Kroah-Hartman static long tty_compat_ioctl(struct file *file, unsigned int cmd,
14996fd7ce5SGreg Kroah-Hartman 				unsigned long arg);
15096fd7ce5SGreg Kroah-Hartman #else
15196fd7ce5SGreg Kroah-Hartman #define tty_compat_ioctl NULL
15296fd7ce5SGreg Kroah-Hartman #endif
15396fd7ce5SGreg Kroah-Hartman static int __tty_fasync(int fd, struct file *filp, int on);
15496fd7ce5SGreg Kroah-Hartman static int tty_fasync(int fd, struct file *filp, int on);
15596fd7ce5SGreg Kroah-Hartman static void release_tty(struct tty_struct *tty, int idx);
15696fd7ce5SGreg Kroah-Hartman static void __proc_set_tty(struct task_struct *tsk, struct tty_struct *tty);
15796fd7ce5SGreg Kroah-Hartman static void proc_set_tty(struct task_struct *tsk, struct tty_struct *tty);
15896fd7ce5SGreg Kroah-Hartman 
15996fd7ce5SGreg Kroah-Hartman /**
16096fd7ce5SGreg Kroah-Hartman  *	alloc_tty_struct	-	allocate a tty object
16196fd7ce5SGreg Kroah-Hartman  *
16296fd7ce5SGreg Kroah-Hartman  *	Return a new empty tty structure. The data fields have not
16396fd7ce5SGreg Kroah-Hartman  *	been initialized in any way but has been zeroed
16496fd7ce5SGreg Kroah-Hartman  *
16596fd7ce5SGreg Kroah-Hartman  *	Locking: none
16696fd7ce5SGreg Kroah-Hartman  */
16796fd7ce5SGreg Kroah-Hartman 
16896fd7ce5SGreg Kroah-Hartman struct tty_struct *alloc_tty_struct(void)
16996fd7ce5SGreg Kroah-Hartman {
17096fd7ce5SGreg Kroah-Hartman 	return kzalloc(sizeof(struct tty_struct), GFP_KERNEL);
17196fd7ce5SGreg Kroah-Hartman }
17296fd7ce5SGreg Kroah-Hartman 
17396fd7ce5SGreg Kroah-Hartman /**
17496fd7ce5SGreg Kroah-Hartman  *	free_tty_struct		-	free a disused tty
17596fd7ce5SGreg Kroah-Hartman  *	@tty: tty struct to free
17696fd7ce5SGreg Kroah-Hartman  *
17796fd7ce5SGreg Kroah-Hartman  *	Free the write buffers, tty queue and tty memory itself.
17896fd7ce5SGreg Kroah-Hartman  *
17996fd7ce5SGreg Kroah-Hartman  *	Locking: none. Must be called after tty is definitely unused
18096fd7ce5SGreg Kroah-Hartman  */
18196fd7ce5SGreg Kroah-Hartman 
18296fd7ce5SGreg Kroah-Hartman void free_tty_struct(struct tty_struct *tty)
18396fd7ce5SGreg Kroah-Hartman {
184dc6802a7SDan Carpenter 	if (!tty)
185dc6802a7SDan Carpenter 		return;
18696fd7ce5SGreg Kroah-Hartman 	if (tty->dev)
18796fd7ce5SGreg Kroah-Hartman 		put_device(tty->dev);
18896fd7ce5SGreg Kroah-Hartman 	kfree(tty->write_buf);
18989c8d91eSAlan Cox 	tty->magic = 0xDEADDEAD;
19096fd7ce5SGreg Kroah-Hartman 	kfree(tty);
19196fd7ce5SGreg Kroah-Hartman }
19296fd7ce5SGreg Kroah-Hartman 
19396fd7ce5SGreg Kroah-Hartman static inline struct tty_struct *file_tty(struct file *file)
19496fd7ce5SGreg Kroah-Hartman {
19596fd7ce5SGreg Kroah-Hartman 	return ((struct tty_file_private *)file->private_data)->tty;
19696fd7ce5SGreg Kroah-Hartman }
19796fd7ce5SGreg Kroah-Hartman 
198fa90e1c9SJiri Slaby int tty_alloc_file(struct file *file)
19996fd7ce5SGreg Kroah-Hartman {
20096fd7ce5SGreg Kroah-Hartman 	struct tty_file_private *priv;
20196fd7ce5SGreg Kroah-Hartman 
20296fd7ce5SGreg Kroah-Hartman 	priv = kmalloc(sizeof(*priv), GFP_KERNEL);
20396fd7ce5SGreg Kroah-Hartman 	if (!priv)
20496fd7ce5SGreg Kroah-Hartman 		return -ENOMEM;
20596fd7ce5SGreg Kroah-Hartman 
206fa90e1c9SJiri Slaby 	file->private_data = priv;
207fa90e1c9SJiri Slaby 
208fa90e1c9SJiri Slaby 	return 0;
209fa90e1c9SJiri Slaby }
210fa90e1c9SJiri Slaby 
211fa90e1c9SJiri Slaby /* Associate a new file with the tty structure */
212fa90e1c9SJiri Slaby void tty_add_file(struct tty_struct *tty, struct file *file)
213fa90e1c9SJiri Slaby {
214fa90e1c9SJiri Slaby 	struct tty_file_private *priv = file->private_data;
215fa90e1c9SJiri Slaby 
21696fd7ce5SGreg Kroah-Hartman 	priv->tty = tty;
21796fd7ce5SGreg Kroah-Hartman 	priv->file = file;
21896fd7ce5SGreg Kroah-Hartman 
21996fd7ce5SGreg Kroah-Hartman 	spin_lock(&tty_files_lock);
22096fd7ce5SGreg Kroah-Hartman 	list_add(&priv->list, &tty->tty_files);
22196fd7ce5SGreg Kroah-Hartman 	spin_unlock(&tty_files_lock);
222fa90e1c9SJiri Slaby }
22396fd7ce5SGreg Kroah-Hartman 
224fa90e1c9SJiri Slaby /**
225fa90e1c9SJiri Slaby  * tty_free_file - free file->private_data
226fa90e1c9SJiri Slaby  *
227fa90e1c9SJiri Slaby  * This shall be used only for fail path handling when tty_add_file was not
228fa90e1c9SJiri Slaby  * called yet.
229fa90e1c9SJiri Slaby  */
230fa90e1c9SJiri Slaby void tty_free_file(struct file *file)
231fa90e1c9SJiri Slaby {
232fa90e1c9SJiri Slaby 	struct tty_file_private *priv = file->private_data;
233fa90e1c9SJiri Slaby 
234fa90e1c9SJiri Slaby 	file->private_data = NULL;
235fa90e1c9SJiri Slaby 	kfree(priv);
23696fd7ce5SGreg Kroah-Hartman }
23796fd7ce5SGreg Kroah-Hartman 
23896fd7ce5SGreg Kroah-Hartman /* Delete file from its tty */
2392520e274SJosh Triplett static void tty_del_file(struct file *file)
24096fd7ce5SGreg Kroah-Hartman {
24196fd7ce5SGreg Kroah-Hartman 	struct tty_file_private *priv = file->private_data;
24296fd7ce5SGreg Kroah-Hartman 
24396fd7ce5SGreg Kroah-Hartman 	spin_lock(&tty_files_lock);
24496fd7ce5SGreg Kroah-Hartman 	list_del(&priv->list);
24596fd7ce5SGreg Kroah-Hartman 	spin_unlock(&tty_files_lock);
246fa90e1c9SJiri Slaby 	tty_free_file(file);
24796fd7ce5SGreg Kroah-Hartman }
24896fd7ce5SGreg Kroah-Hartman 
24996fd7ce5SGreg Kroah-Hartman 
25096fd7ce5SGreg Kroah-Hartman #define TTY_NUMBER(tty) ((tty)->index + (tty)->driver->name_base)
25196fd7ce5SGreg Kroah-Hartman 
25296fd7ce5SGreg Kroah-Hartman /**
25396fd7ce5SGreg Kroah-Hartman  *	tty_name	-	return tty naming
25496fd7ce5SGreg Kroah-Hartman  *	@tty: tty structure
25596fd7ce5SGreg Kroah-Hartman  *	@buf: buffer for output
25696fd7ce5SGreg Kroah-Hartman  *
25796fd7ce5SGreg Kroah-Hartman  *	Convert a tty structure into a name. The name reflects the kernel
25896fd7ce5SGreg Kroah-Hartman  *	naming policy and if udev is in use may not reflect user space
25996fd7ce5SGreg Kroah-Hartman  *
26096fd7ce5SGreg Kroah-Hartman  *	Locking: none
26196fd7ce5SGreg Kroah-Hartman  */
26296fd7ce5SGreg Kroah-Hartman 
26396fd7ce5SGreg Kroah-Hartman char *tty_name(struct tty_struct *tty, char *buf)
26496fd7ce5SGreg Kroah-Hartman {
26596fd7ce5SGreg Kroah-Hartman 	if (!tty) /* Hmm.  NULL pointer.  That's fun. */
26696fd7ce5SGreg Kroah-Hartman 		strcpy(buf, "NULL tty");
26796fd7ce5SGreg Kroah-Hartman 	else
26896fd7ce5SGreg Kroah-Hartman 		strcpy(buf, tty->name);
26996fd7ce5SGreg Kroah-Hartman 	return buf;
27096fd7ce5SGreg Kroah-Hartman }
27196fd7ce5SGreg Kroah-Hartman 
27296fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_name);
27396fd7ce5SGreg Kroah-Hartman 
27496fd7ce5SGreg Kroah-Hartman int tty_paranoia_check(struct tty_struct *tty, struct inode *inode,
27596fd7ce5SGreg Kroah-Hartman 			      const char *routine)
27696fd7ce5SGreg Kroah-Hartman {
27796fd7ce5SGreg Kroah-Hartman #ifdef TTY_PARANOIA_CHECK
27896fd7ce5SGreg Kroah-Hartman 	if (!tty) {
27996fd7ce5SGreg Kroah-Hartman 		printk(KERN_WARNING
28096fd7ce5SGreg Kroah-Hartman 			"null TTY for (%d:%d) in %s\n",
28196fd7ce5SGreg Kroah-Hartman 			imajor(inode), iminor(inode), routine);
28296fd7ce5SGreg Kroah-Hartman 		return 1;
28396fd7ce5SGreg Kroah-Hartman 	}
28496fd7ce5SGreg Kroah-Hartman 	if (tty->magic != TTY_MAGIC) {
28596fd7ce5SGreg Kroah-Hartman 		printk(KERN_WARNING
28696fd7ce5SGreg Kroah-Hartman 			"bad magic number for tty struct (%d:%d) in %s\n",
28796fd7ce5SGreg Kroah-Hartman 			imajor(inode), iminor(inode), routine);
28896fd7ce5SGreg Kroah-Hartman 		return 1;
28996fd7ce5SGreg Kroah-Hartman 	}
29096fd7ce5SGreg Kroah-Hartman #endif
29196fd7ce5SGreg Kroah-Hartman 	return 0;
29296fd7ce5SGreg Kroah-Hartman }
29396fd7ce5SGreg Kroah-Hartman 
29496fd7ce5SGreg Kroah-Hartman static int check_tty_count(struct tty_struct *tty, const char *routine)
29596fd7ce5SGreg Kroah-Hartman {
29696fd7ce5SGreg Kroah-Hartman #ifdef CHECK_TTY_COUNT
29796fd7ce5SGreg Kroah-Hartman 	struct list_head *p;
29896fd7ce5SGreg Kroah-Hartman 	int count = 0;
29996fd7ce5SGreg Kroah-Hartman 
30096fd7ce5SGreg Kroah-Hartman 	spin_lock(&tty_files_lock);
30196fd7ce5SGreg Kroah-Hartman 	list_for_each(p, &tty->tty_files) {
30296fd7ce5SGreg Kroah-Hartman 		count++;
30396fd7ce5SGreg Kroah-Hartman 	}
30496fd7ce5SGreg Kroah-Hartman 	spin_unlock(&tty_files_lock);
30596fd7ce5SGreg Kroah-Hartman 	if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
30696fd7ce5SGreg Kroah-Hartman 	    tty->driver->subtype == PTY_TYPE_SLAVE &&
30796fd7ce5SGreg Kroah-Hartman 	    tty->link && tty->link->count)
30896fd7ce5SGreg Kroah-Hartman 		count++;
30996fd7ce5SGreg Kroah-Hartman 	if (tty->count != count) {
31096fd7ce5SGreg Kroah-Hartman 		printk(KERN_WARNING "Warning: dev (%s) tty->count(%d) "
31196fd7ce5SGreg Kroah-Hartman 				    "!= #fd's(%d) in %s\n",
31296fd7ce5SGreg Kroah-Hartman 		       tty->name, tty->count, count, routine);
31396fd7ce5SGreg Kroah-Hartman 		return count;
31496fd7ce5SGreg Kroah-Hartman 	}
31596fd7ce5SGreg Kroah-Hartman #endif
31696fd7ce5SGreg Kroah-Hartman 	return 0;
31796fd7ce5SGreg Kroah-Hartman }
31896fd7ce5SGreg Kroah-Hartman 
31996fd7ce5SGreg Kroah-Hartman /**
32096fd7ce5SGreg Kroah-Hartman  *	get_tty_driver		-	find device of a tty
32196fd7ce5SGreg Kroah-Hartman  *	@dev_t: device identifier
32296fd7ce5SGreg Kroah-Hartman  *	@index: returns the index of the tty
32396fd7ce5SGreg Kroah-Hartman  *
32496fd7ce5SGreg Kroah-Hartman  *	This routine returns a tty driver structure, given a device number
32596fd7ce5SGreg Kroah-Hartman  *	and also passes back the index number.
32696fd7ce5SGreg Kroah-Hartman  *
32796fd7ce5SGreg Kroah-Hartman  *	Locking: caller must hold tty_mutex
32896fd7ce5SGreg Kroah-Hartman  */
32996fd7ce5SGreg Kroah-Hartman 
33096fd7ce5SGreg Kroah-Hartman static struct tty_driver *get_tty_driver(dev_t device, int *index)
33196fd7ce5SGreg Kroah-Hartman {
33296fd7ce5SGreg Kroah-Hartman 	struct tty_driver *p;
33396fd7ce5SGreg Kroah-Hartman 
33496fd7ce5SGreg Kroah-Hartman 	list_for_each_entry(p, &tty_drivers, tty_drivers) {
33596fd7ce5SGreg Kroah-Hartman 		dev_t base = MKDEV(p->major, p->minor_start);
33696fd7ce5SGreg Kroah-Hartman 		if (device < base || device >= base + p->num)
33796fd7ce5SGreg Kroah-Hartman 			continue;
33896fd7ce5SGreg Kroah-Hartman 		*index = device - base;
33996fd7ce5SGreg Kroah-Hartman 		return tty_driver_kref_get(p);
34096fd7ce5SGreg Kroah-Hartman 	}
34196fd7ce5SGreg Kroah-Hartman 	return NULL;
34296fd7ce5SGreg Kroah-Hartman }
34396fd7ce5SGreg Kroah-Hartman 
34496fd7ce5SGreg Kroah-Hartman #ifdef CONFIG_CONSOLE_POLL
34596fd7ce5SGreg Kroah-Hartman 
34696fd7ce5SGreg Kroah-Hartman /**
34796fd7ce5SGreg Kroah-Hartman  *	tty_find_polling_driver	-	find device of a polled tty
34896fd7ce5SGreg Kroah-Hartman  *	@name: name string to match
34996fd7ce5SGreg Kroah-Hartman  *	@line: pointer to resulting tty line nr
35096fd7ce5SGreg Kroah-Hartman  *
35196fd7ce5SGreg Kroah-Hartman  *	This routine returns a tty driver structure, given a name
35296fd7ce5SGreg Kroah-Hartman  *	and the condition that the tty driver is capable of polled
35396fd7ce5SGreg Kroah-Hartman  *	operation.
35496fd7ce5SGreg Kroah-Hartman  */
35596fd7ce5SGreg Kroah-Hartman struct tty_driver *tty_find_polling_driver(char *name, int *line)
35696fd7ce5SGreg Kroah-Hartman {
35796fd7ce5SGreg Kroah-Hartman 	struct tty_driver *p, *res = NULL;
35896fd7ce5SGreg Kroah-Hartman 	int tty_line = 0;
35996fd7ce5SGreg Kroah-Hartman 	int len;
36096fd7ce5SGreg Kroah-Hartman 	char *str, *stp;
36196fd7ce5SGreg Kroah-Hartman 
36296fd7ce5SGreg Kroah-Hartman 	for (str = name; *str; str++)
36396fd7ce5SGreg Kroah-Hartman 		if ((*str >= '0' && *str <= '9') || *str == ',')
36496fd7ce5SGreg Kroah-Hartman 			break;
36596fd7ce5SGreg Kroah-Hartman 	if (!*str)
36696fd7ce5SGreg Kroah-Hartman 		return NULL;
36796fd7ce5SGreg Kroah-Hartman 
36896fd7ce5SGreg Kroah-Hartman 	len = str - name;
36996fd7ce5SGreg Kroah-Hartman 	tty_line = simple_strtoul(str, &str, 10);
37096fd7ce5SGreg Kroah-Hartman 
37196fd7ce5SGreg Kroah-Hartman 	mutex_lock(&tty_mutex);
37296fd7ce5SGreg Kroah-Hartman 	/* Search through the tty devices to look for a match */
37396fd7ce5SGreg Kroah-Hartman 	list_for_each_entry(p, &tty_drivers, tty_drivers) {
37496fd7ce5SGreg Kroah-Hartman 		if (strncmp(name, p->name, len) != 0)
37596fd7ce5SGreg Kroah-Hartman 			continue;
37696fd7ce5SGreg Kroah-Hartman 		stp = str;
37796fd7ce5SGreg Kroah-Hartman 		if (*stp == ',')
37896fd7ce5SGreg Kroah-Hartman 			stp++;
37996fd7ce5SGreg Kroah-Hartman 		if (*stp == '\0')
38096fd7ce5SGreg Kroah-Hartman 			stp = NULL;
38196fd7ce5SGreg Kroah-Hartman 
38296fd7ce5SGreg Kroah-Hartman 		if (tty_line >= 0 && tty_line < p->num && p->ops &&
38396fd7ce5SGreg Kroah-Hartman 		    p->ops->poll_init && !p->ops->poll_init(p, tty_line, stp)) {
38496fd7ce5SGreg Kroah-Hartman 			res = tty_driver_kref_get(p);
38596fd7ce5SGreg Kroah-Hartman 			*line = tty_line;
38696fd7ce5SGreg Kroah-Hartman 			break;
38796fd7ce5SGreg Kroah-Hartman 		}
38896fd7ce5SGreg Kroah-Hartman 	}
38996fd7ce5SGreg Kroah-Hartman 	mutex_unlock(&tty_mutex);
39096fd7ce5SGreg Kroah-Hartman 
39196fd7ce5SGreg Kroah-Hartman 	return res;
39296fd7ce5SGreg Kroah-Hartman }
39396fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(tty_find_polling_driver);
39496fd7ce5SGreg Kroah-Hartman #endif
39596fd7ce5SGreg Kroah-Hartman 
39696fd7ce5SGreg Kroah-Hartman /**
39796fd7ce5SGreg Kroah-Hartman  *	tty_check_change	-	check for POSIX terminal changes
39896fd7ce5SGreg Kroah-Hartman  *	@tty: tty to check
39996fd7ce5SGreg Kroah-Hartman  *
40096fd7ce5SGreg Kroah-Hartman  *	If we try to write to, or set the state of, a terminal and we're
40196fd7ce5SGreg Kroah-Hartman  *	not in the foreground, send a SIGTTOU.  If the signal is blocked or
40296fd7ce5SGreg Kroah-Hartman  *	ignored, go ahead and perform the operation.  (POSIX 7.2)
40396fd7ce5SGreg Kroah-Hartman  *
40496fd7ce5SGreg Kroah-Hartman  *	Locking: ctrl_lock
40596fd7ce5SGreg Kroah-Hartman  */
40696fd7ce5SGreg Kroah-Hartman 
40796fd7ce5SGreg Kroah-Hartman int tty_check_change(struct tty_struct *tty)
40896fd7ce5SGreg Kroah-Hartman {
40996fd7ce5SGreg Kroah-Hartman 	unsigned long flags;
41096fd7ce5SGreg Kroah-Hartman 	int ret = 0;
41196fd7ce5SGreg Kroah-Hartman 
41296fd7ce5SGreg Kroah-Hartman 	if (current->signal->tty != tty)
41396fd7ce5SGreg Kroah-Hartman 		return 0;
41496fd7ce5SGreg Kroah-Hartman 
41596fd7ce5SGreg Kroah-Hartman 	spin_lock_irqsave(&tty->ctrl_lock, flags);
41696fd7ce5SGreg Kroah-Hartman 
41796fd7ce5SGreg Kroah-Hartman 	if (!tty->pgrp) {
41896fd7ce5SGreg Kroah-Hartman 		printk(KERN_WARNING "tty_check_change: tty->pgrp == NULL!\n");
41996fd7ce5SGreg Kroah-Hartman 		goto out_unlock;
42096fd7ce5SGreg Kroah-Hartman 	}
42196fd7ce5SGreg Kroah-Hartman 	if (task_pgrp(current) == tty->pgrp)
42296fd7ce5SGreg Kroah-Hartman 		goto out_unlock;
42396fd7ce5SGreg Kroah-Hartman 	spin_unlock_irqrestore(&tty->ctrl_lock, flags);
42496fd7ce5SGreg Kroah-Hartman 	if (is_ignored(SIGTTOU))
42596fd7ce5SGreg Kroah-Hartman 		goto out;
42696fd7ce5SGreg Kroah-Hartman 	if (is_current_pgrp_orphaned()) {
42796fd7ce5SGreg Kroah-Hartman 		ret = -EIO;
42896fd7ce5SGreg Kroah-Hartman 		goto out;
42996fd7ce5SGreg Kroah-Hartman 	}
43096fd7ce5SGreg Kroah-Hartman 	kill_pgrp(task_pgrp(current), SIGTTOU, 1);
43196fd7ce5SGreg Kroah-Hartman 	set_thread_flag(TIF_SIGPENDING);
43296fd7ce5SGreg Kroah-Hartman 	ret = -ERESTARTSYS;
43396fd7ce5SGreg Kroah-Hartman out:
43496fd7ce5SGreg Kroah-Hartman 	return ret;
43596fd7ce5SGreg Kroah-Hartman out_unlock:
43696fd7ce5SGreg Kroah-Hartman 	spin_unlock_irqrestore(&tty->ctrl_lock, flags);
43796fd7ce5SGreg Kroah-Hartman 	return ret;
43896fd7ce5SGreg Kroah-Hartman }
43996fd7ce5SGreg Kroah-Hartman 
44096fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_check_change);
44196fd7ce5SGreg Kroah-Hartman 
44296fd7ce5SGreg Kroah-Hartman static ssize_t hung_up_tty_read(struct file *file, char __user *buf,
44396fd7ce5SGreg Kroah-Hartman 				size_t count, loff_t *ppos)
44496fd7ce5SGreg Kroah-Hartman {
44596fd7ce5SGreg Kroah-Hartman 	return 0;
44696fd7ce5SGreg Kroah-Hartman }
44796fd7ce5SGreg Kroah-Hartman 
44896fd7ce5SGreg Kroah-Hartman static ssize_t hung_up_tty_write(struct file *file, const char __user *buf,
44996fd7ce5SGreg Kroah-Hartman 				 size_t count, loff_t *ppos)
45096fd7ce5SGreg Kroah-Hartman {
45196fd7ce5SGreg Kroah-Hartman 	return -EIO;
45296fd7ce5SGreg Kroah-Hartman }
45396fd7ce5SGreg Kroah-Hartman 
45496fd7ce5SGreg Kroah-Hartman /* No kernel lock held - none needed ;) */
45596fd7ce5SGreg Kroah-Hartman static unsigned int hung_up_tty_poll(struct file *filp, poll_table *wait)
45696fd7ce5SGreg Kroah-Hartman {
45796fd7ce5SGreg Kroah-Hartman 	return POLLIN | POLLOUT | POLLERR | POLLHUP | POLLRDNORM | POLLWRNORM;
45896fd7ce5SGreg Kroah-Hartman }
45996fd7ce5SGreg Kroah-Hartman 
46096fd7ce5SGreg Kroah-Hartman static long hung_up_tty_ioctl(struct file *file, unsigned int cmd,
46196fd7ce5SGreg Kroah-Hartman 		unsigned long arg)
46296fd7ce5SGreg Kroah-Hartman {
46396fd7ce5SGreg Kroah-Hartman 	return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
46496fd7ce5SGreg Kroah-Hartman }
46596fd7ce5SGreg Kroah-Hartman 
46696fd7ce5SGreg Kroah-Hartman static long hung_up_tty_compat_ioctl(struct file *file,
46796fd7ce5SGreg Kroah-Hartman 				     unsigned int cmd, unsigned long arg)
46896fd7ce5SGreg Kroah-Hartman {
46996fd7ce5SGreg Kroah-Hartman 	return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
47096fd7ce5SGreg Kroah-Hartman }
47196fd7ce5SGreg Kroah-Hartman 
47296fd7ce5SGreg Kroah-Hartman static const struct file_operations tty_fops = {
47396fd7ce5SGreg Kroah-Hartman 	.llseek		= no_llseek,
47496fd7ce5SGreg Kroah-Hartman 	.read		= tty_read,
47596fd7ce5SGreg Kroah-Hartman 	.write		= tty_write,
47696fd7ce5SGreg Kroah-Hartman 	.poll		= tty_poll,
47796fd7ce5SGreg Kroah-Hartman 	.unlocked_ioctl	= tty_ioctl,
47896fd7ce5SGreg Kroah-Hartman 	.compat_ioctl	= tty_compat_ioctl,
47996fd7ce5SGreg Kroah-Hartman 	.open		= tty_open,
48096fd7ce5SGreg Kroah-Hartman 	.release	= tty_release,
48196fd7ce5SGreg Kroah-Hartman 	.fasync		= tty_fasync,
48296fd7ce5SGreg Kroah-Hartman };
48396fd7ce5SGreg Kroah-Hartman 
48496fd7ce5SGreg Kroah-Hartman static const struct file_operations console_fops = {
48596fd7ce5SGreg Kroah-Hartman 	.llseek		= no_llseek,
48696fd7ce5SGreg Kroah-Hartman 	.read		= tty_read,
48796fd7ce5SGreg Kroah-Hartman 	.write		= redirected_tty_write,
48896fd7ce5SGreg Kroah-Hartman 	.poll		= tty_poll,
48996fd7ce5SGreg Kroah-Hartman 	.unlocked_ioctl	= tty_ioctl,
49096fd7ce5SGreg Kroah-Hartman 	.compat_ioctl	= tty_compat_ioctl,
49196fd7ce5SGreg Kroah-Hartman 	.open		= tty_open,
49296fd7ce5SGreg Kroah-Hartman 	.release	= tty_release,
49396fd7ce5SGreg Kroah-Hartman 	.fasync		= tty_fasync,
49496fd7ce5SGreg Kroah-Hartman };
49596fd7ce5SGreg Kroah-Hartman 
49696fd7ce5SGreg Kroah-Hartman static const struct file_operations hung_up_tty_fops = {
49796fd7ce5SGreg Kroah-Hartman 	.llseek		= no_llseek,
49896fd7ce5SGreg Kroah-Hartman 	.read		= hung_up_tty_read,
49996fd7ce5SGreg Kroah-Hartman 	.write		= hung_up_tty_write,
50096fd7ce5SGreg Kroah-Hartman 	.poll		= hung_up_tty_poll,
50196fd7ce5SGreg Kroah-Hartman 	.unlocked_ioctl	= hung_up_tty_ioctl,
50296fd7ce5SGreg Kroah-Hartman 	.compat_ioctl	= hung_up_tty_compat_ioctl,
50396fd7ce5SGreg Kroah-Hartman 	.release	= tty_release,
50496fd7ce5SGreg Kroah-Hartman };
50596fd7ce5SGreg Kroah-Hartman 
50696fd7ce5SGreg Kroah-Hartman static DEFINE_SPINLOCK(redirect_lock);
50796fd7ce5SGreg Kroah-Hartman static struct file *redirect;
50896fd7ce5SGreg Kroah-Hartman 
50996fd7ce5SGreg Kroah-Hartman /**
51096fd7ce5SGreg Kroah-Hartman  *	tty_wakeup	-	request more data
51196fd7ce5SGreg Kroah-Hartman  *	@tty: terminal
51296fd7ce5SGreg Kroah-Hartman  *
51396fd7ce5SGreg Kroah-Hartman  *	Internal and external helper for wakeups of tty. This function
51496fd7ce5SGreg Kroah-Hartman  *	informs the line discipline if present that the driver is ready
51596fd7ce5SGreg Kroah-Hartman  *	to receive more output data.
51696fd7ce5SGreg Kroah-Hartman  */
51796fd7ce5SGreg Kroah-Hartman 
51896fd7ce5SGreg Kroah-Hartman void tty_wakeup(struct tty_struct *tty)
51996fd7ce5SGreg Kroah-Hartman {
52096fd7ce5SGreg Kroah-Hartman 	struct tty_ldisc *ld;
52196fd7ce5SGreg Kroah-Hartman 
52296fd7ce5SGreg Kroah-Hartman 	if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) {
52396fd7ce5SGreg Kroah-Hartman 		ld = tty_ldisc_ref(tty);
52496fd7ce5SGreg Kroah-Hartman 		if (ld) {
52596fd7ce5SGreg Kroah-Hartman 			if (ld->ops->write_wakeup)
52696fd7ce5SGreg Kroah-Hartman 				ld->ops->write_wakeup(tty);
52796fd7ce5SGreg Kroah-Hartman 			tty_ldisc_deref(ld);
52896fd7ce5SGreg Kroah-Hartman 		}
52996fd7ce5SGreg Kroah-Hartman 	}
53096fd7ce5SGreg Kroah-Hartman 	wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
53196fd7ce5SGreg Kroah-Hartman }
53296fd7ce5SGreg Kroah-Hartman 
53396fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(tty_wakeup);
53496fd7ce5SGreg Kroah-Hartman 
53596fd7ce5SGreg Kroah-Hartman /**
53696fd7ce5SGreg Kroah-Hartman  *	__tty_hangup		-	actual handler for hangup events
53796fd7ce5SGreg Kroah-Hartman  *	@work: tty device
53896fd7ce5SGreg Kroah-Hartman  *
53996fd7ce5SGreg Kroah-Hartman  *	This can be called by the "eventd" kernel thread.  That is process
54096fd7ce5SGreg Kroah-Hartman  *	synchronous but doesn't hold any locks, so we need to make sure we
54196fd7ce5SGreg Kroah-Hartman  *	have the appropriate locks for what we're doing.
54296fd7ce5SGreg Kroah-Hartman  *
54396fd7ce5SGreg Kroah-Hartman  *	The hangup event clears any pending redirections onto the hung up
54496fd7ce5SGreg Kroah-Hartman  *	device. It ensures future writes will error and it does the needed
54596fd7ce5SGreg Kroah-Hartman  *	line discipline hangup and signal delivery. The tty object itself
54696fd7ce5SGreg Kroah-Hartman  *	remains intact.
54796fd7ce5SGreg Kroah-Hartman  *
54896fd7ce5SGreg Kroah-Hartman  *	Locking:
54996fd7ce5SGreg Kroah-Hartman  *		BTM
55096fd7ce5SGreg Kroah-Hartman  *		  redirect lock for undoing redirection
55196fd7ce5SGreg Kroah-Hartman  *		  file list lock for manipulating list of ttys
55296fd7ce5SGreg Kroah-Hartman  *		  tty_ldisc_lock from called functions
55396fd7ce5SGreg Kroah-Hartman  *		  termios_mutex resetting termios data
55496fd7ce5SGreg Kroah-Hartman  *		  tasklist_lock to walk task list for hangup event
55596fd7ce5SGreg Kroah-Hartman  *		    ->siglock to protect ->signal/->sighand
55696fd7ce5SGreg Kroah-Hartman  */
5572520e274SJosh Triplett static void __tty_hangup(struct tty_struct *tty)
55896fd7ce5SGreg Kroah-Hartman {
55996fd7ce5SGreg Kroah-Hartman 	struct file *cons_filp = NULL;
56096fd7ce5SGreg Kroah-Hartman 	struct file *filp, *f = NULL;
56196fd7ce5SGreg Kroah-Hartman 	struct task_struct *p;
56296fd7ce5SGreg Kroah-Hartman 	struct tty_file_private *priv;
56396fd7ce5SGreg Kroah-Hartman 	int    closecount = 0, n;
56496fd7ce5SGreg Kroah-Hartman 	unsigned long flags;
56596fd7ce5SGreg Kroah-Hartman 	int refs = 0;
56696fd7ce5SGreg Kroah-Hartman 
56796fd7ce5SGreg Kroah-Hartman 	if (!tty)
56896fd7ce5SGreg Kroah-Hartman 		return;
56996fd7ce5SGreg Kroah-Hartman 
57096fd7ce5SGreg Kroah-Hartman 
57196fd7ce5SGreg Kroah-Hartman 	spin_lock(&redirect_lock);
57296fd7ce5SGreg Kroah-Hartman 	if (redirect && file_tty(redirect) == tty) {
57396fd7ce5SGreg Kroah-Hartman 		f = redirect;
57496fd7ce5SGreg Kroah-Hartman 		redirect = NULL;
57596fd7ce5SGreg Kroah-Hartman 	}
57696fd7ce5SGreg Kroah-Hartman 	spin_unlock(&redirect_lock);
57796fd7ce5SGreg Kroah-Hartman 
57889c8d91eSAlan Cox 	tty_lock(tty);
57996fd7ce5SGreg Kroah-Hartman 
580acfa747bSJiri Slaby 	/* some functions below drop BTM, so we need this bit */
581acfa747bSJiri Slaby 	set_bit(TTY_HUPPING, &tty->flags);
582acfa747bSJiri Slaby 
58396fd7ce5SGreg Kroah-Hartman 	/* inuse_filps is protected by the single tty lock,
58496fd7ce5SGreg Kroah-Hartman 	   this really needs to change if we want to flush the
58596fd7ce5SGreg Kroah-Hartman 	   workqueue with the lock held */
58696fd7ce5SGreg Kroah-Hartman 	check_tty_count(tty, "tty_hangup");
58796fd7ce5SGreg Kroah-Hartman 
58896fd7ce5SGreg Kroah-Hartman 	spin_lock(&tty_files_lock);
58996fd7ce5SGreg Kroah-Hartman 	/* This breaks for file handles being sent over AF_UNIX sockets ? */
59096fd7ce5SGreg Kroah-Hartman 	list_for_each_entry(priv, &tty->tty_files, list) {
59196fd7ce5SGreg Kroah-Hartman 		filp = priv->file;
59296fd7ce5SGreg Kroah-Hartman 		if (filp->f_op->write == redirected_tty_write)
59396fd7ce5SGreg Kroah-Hartman 			cons_filp = filp;
59496fd7ce5SGreg Kroah-Hartman 		if (filp->f_op->write != tty_write)
59596fd7ce5SGreg Kroah-Hartman 			continue;
59696fd7ce5SGreg Kroah-Hartman 		closecount++;
59796fd7ce5SGreg Kroah-Hartman 		__tty_fasync(-1, filp, 0);	/* can't block */
59896fd7ce5SGreg Kroah-Hartman 		filp->f_op = &hung_up_tty_fops;
59996fd7ce5SGreg Kroah-Hartman 	}
60096fd7ce5SGreg Kroah-Hartman 	spin_unlock(&tty_files_lock);
60196fd7ce5SGreg Kroah-Hartman 
602acfa747bSJiri Slaby 	/*
603acfa747bSJiri Slaby 	 * it drops BTM and thus races with reopen
604acfa747bSJiri Slaby 	 * we protect the race by TTY_HUPPING
605acfa747bSJiri Slaby 	 */
60696fd7ce5SGreg Kroah-Hartman 	tty_ldisc_hangup(tty);
60796fd7ce5SGreg Kroah-Hartman 
60896fd7ce5SGreg Kroah-Hartman 	read_lock(&tasklist_lock);
60996fd7ce5SGreg Kroah-Hartman 	if (tty->session) {
61096fd7ce5SGreg Kroah-Hartman 		do_each_pid_task(tty->session, PIDTYPE_SID, p) {
61196fd7ce5SGreg Kroah-Hartman 			spin_lock_irq(&p->sighand->siglock);
61296fd7ce5SGreg Kroah-Hartman 			if (p->signal->tty == tty) {
61396fd7ce5SGreg Kroah-Hartman 				p->signal->tty = NULL;
61496fd7ce5SGreg Kroah-Hartman 				/* We defer the dereferences outside fo
61596fd7ce5SGreg Kroah-Hartman 				   the tasklist lock */
61696fd7ce5SGreg Kroah-Hartman 				refs++;
61796fd7ce5SGreg Kroah-Hartman 			}
61896fd7ce5SGreg Kroah-Hartman 			if (!p->signal->leader) {
61996fd7ce5SGreg Kroah-Hartman 				spin_unlock_irq(&p->sighand->siglock);
62096fd7ce5SGreg Kroah-Hartman 				continue;
62196fd7ce5SGreg Kroah-Hartman 			}
62296fd7ce5SGreg Kroah-Hartman 			__group_send_sig_info(SIGHUP, SEND_SIG_PRIV, p);
62396fd7ce5SGreg Kroah-Hartman 			__group_send_sig_info(SIGCONT, SEND_SIG_PRIV, p);
62496fd7ce5SGreg Kroah-Hartman 			put_pid(p->signal->tty_old_pgrp);  /* A noop */
62596fd7ce5SGreg Kroah-Hartman 			spin_lock_irqsave(&tty->ctrl_lock, flags);
62696fd7ce5SGreg Kroah-Hartman 			if (tty->pgrp)
62796fd7ce5SGreg Kroah-Hartman 				p->signal->tty_old_pgrp = get_pid(tty->pgrp);
62896fd7ce5SGreg Kroah-Hartman 			spin_unlock_irqrestore(&tty->ctrl_lock, flags);
62996fd7ce5SGreg Kroah-Hartman 			spin_unlock_irq(&p->sighand->siglock);
63096fd7ce5SGreg Kroah-Hartman 		} while_each_pid_task(tty->session, PIDTYPE_SID, p);
63196fd7ce5SGreg Kroah-Hartman 	}
63296fd7ce5SGreg Kroah-Hartman 	read_unlock(&tasklist_lock);
63396fd7ce5SGreg Kroah-Hartman 
63496fd7ce5SGreg Kroah-Hartman 	spin_lock_irqsave(&tty->ctrl_lock, flags);
63596fd7ce5SGreg Kroah-Hartman 	clear_bit(TTY_THROTTLED, &tty->flags);
63696fd7ce5SGreg Kroah-Hartman 	clear_bit(TTY_PUSH, &tty->flags);
63796fd7ce5SGreg Kroah-Hartman 	clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
63896fd7ce5SGreg Kroah-Hartman 	put_pid(tty->session);
63996fd7ce5SGreg Kroah-Hartman 	put_pid(tty->pgrp);
64096fd7ce5SGreg Kroah-Hartman 	tty->session = NULL;
64196fd7ce5SGreg Kroah-Hartman 	tty->pgrp = NULL;
64296fd7ce5SGreg Kroah-Hartman 	tty->ctrl_status = 0;
64396fd7ce5SGreg Kroah-Hartman 	spin_unlock_irqrestore(&tty->ctrl_lock, flags);
64496fd7ce5SGreg Kroah-Hartman 
64596fd7ce5SGreg Kroah-Hartman 	/* Account for the p->signal references we killed */
64696fd7ce5SGreg Kroah-Hartman 	while (refs--)
64796fd7ce5SGreg Kroah-Hartman 		tty_kref_put(tty);
64896fd7ce5SGreg Kroah-Hartman 
64996fd7ce5SGreg Kroah-Hartman 	/*
65096fd7ce5SGreg Kroah-Hartman 	 * If one of the devices matches a console pointer, we
65196fd7ce5SGreg Kroah-Hartman 	 * cannot just call hangup() because that will cause
65296fd7ce5SGreg Kroah-Hartman 	 * tty->count and state->count to go out of sync.
65396fd7ce5SGreg Kroah-Hartman 	 * So we just call close() the right number of times.
65496fd7ce5SGreg Kroah-Hartman 	 */
65596fd7ce5SGreg Kroah-Hartman 	if (cons_filp) {
65696fd7ce5SGreg Kroah-Hartman 		if (tty->ops->close)
65796fd7ce5SGreg Kroah-Hartman 			for (n = 0; n < closecount; n++)
65896fd7ce5SGreg Kroah-Hartman 				tty->ops->close(tty, cons_filp);
65996fd7ce5SGreg Kroah-Hartman 	} else if (tty->ops->hangup)
66096fd7ce5SGreg Kroah-Hartman 		(tty->ops->hangup)(tty);
66196fd7ce5SGreg Kroah-Hartman 	/*
66296fd7ce5SGreg Kroah-Hartman 	 * We don't want to have driver/ldisc interactions beyond
66396fd7ce5SGreg Kroah-Hartman 	 * the ones we did here. The driver layer expects no
66496fd7ce5SGreg Kroah-Hartman 	 * calls after ->hangup() from the ldisc side. However we
66596fd7ce5SGreg Kroah-Hartman 	 * can't yet guarantee all that.
66696fd7ce5SGreg Kroah-Hartman 	 */
66796fd7ce5SGreg Kroah-Hartman 	set_bit(TTY_HUPPED, &tty->flags);
668acfa747bSJiri Slaby 	clear_bit(TTY_HUPPING, &tty->flags);
66996fd7ce5SGreg Kroah-Hartman 	tty_ldisc_enable(tty);
67096fd7ce5SGreg Kroah-Hartman 
67189c8d91eSAlan Cox 	tty_unlock(tty);
67296fd7ce5SGreg Kroah-Hartman 
67396fd7ce5SGreg Kroah-Hartman 	if (f)
67496fd7ce5SGreg Kroah-Hartman 		fput(f);
67596fd7ce5SGreg Kroah-Hartman }
67696fd7ce5SGreg Kroah-Hartman 
67796fd7ce5SGreg Kroah-Hartman static void do_tty_hangup(struct work_struct *work)
67896fd7ce5SGreg Kroah-Hartman {
67996fd7ce5SGreg Kroah-Hartman 	struct tty_struct *tty =
68096fd7ce5SGreg Kroah-Hartman 		container_of(work, struct tty_struct, hangup_work);
68196fd7ce5SGreg Kroah-Hartman 
68296fd7ce5SGreg Kroah-Hartman 	__tty_hangup(tty);
68396fd7ce5SGreg Kroah-Hartman }
68496fd7ce5SGreg Kroah-Hartman 
68596fd7ce5SGreg Kroah-Hartman /**
68696fd7ce5SGreg Kroah-Hartman  *	tty_hangup		-	trigger a hangup event
68796fd7ce5SGreg Kroah-Hartman  *	@tty: tty to hangup
68896fd7ce5SGreg Kroah-Hartman  *
68996fd7ce5SGreg Kroah-Hartman  *	A carrier loss (virtual or otherwise) has occurred on this like
69096fd7ce5SGreg Kroah-Hartman  *	schedule a hangup sequence to run after this event.
69196fd7ce5SGreg Kroah-Hartman  */
69296fd7ce5SGreg Kroah-Hartman 
69396fd7ce5SGreg Kroah-Hartman void tty_hangup(struct tty_struct *tty)
69496fd7ce5SGreg Kroah-Hartman {
69596fd7ce5SGreg Kroah-Hartman #ifdef TTY_DEBUG_HANGUP
69696fd7ce5SGreg Kroah-Hartman 	char	buf[64];
69796fd7ce5SGreg Kroah-Hartman 	printk(KERN_DEBUG "%s hangup...\n", tty_name(tty, buf));
69896fd7ce5SGreg Kroah-Hartman #endif
69996fd7ce5SGreg Kroah-Hartman 	schedule_work(&tty->hangup_work);
70096fd7ce5SGreg Kroah-Hartman }
70196fd7ce5SGreg Kroah-Hartman 
70296fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_hangup);
70396fd7ce5SGreg Kroah-Hartman 
70496fd7ce5SGreg Kroah-Hartman /**
70596fd7ce5SGreg Kroah-Hartman  *	tty_vhangup		-	process vhangup
70696fd7ce5SGreg Kroah-Hartman  *	@tty: tty to hangup
70796fd7ce5SGreg Kroah-Hartman  *
70896fd7ce5SGreg Kroah-Hartman  *	The user has asked via system call for the terminal to be hung up.
70996fd7ce5SGreg Kroah-Hartman  *	We do this synchronously so that when the syscall returns the process
71096fd7ce5SGreg Kroah-Hartman  *	is complete. That guarantee is necessary for security reasons.
71196fd7ce5SGreg Kroah-Hartman  */
71296fd7ce5SGreg Kroah-Hartman 
71396fd7ce5SGreg Kroah-Hartman void tty_vhangup(struct tty_struct *tty)
71496fd7ce5SGreg Kroah-Hartman {
71596fd7ce5SGreg Kroah-Hartman #ifdef TTY_DEBUG_HANGUP
71696fd7ce5SGreg Kroah-Hartman 	char	buf[64];
71796fd7ce5SGreg Kroah-Hartman 
71896fd7ce5SGreg Kroah-Hartman 	printk(KERN_DEBUG "%s vhangup...\n", tty_name(tty, buf));
71996fd7ce5SGreg Kroah-Hartman #endif
72096fd7ce5SGreg Kroah-Hartman 	__tty_hangup(tty);
72196fd7ce5SGreg Kroah-Hartman }
72296fd7ce5SGreg Kroah-Hartman 
72396fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_vhangup);
72496fd7ce5SGreg Kroah-Hartman 
72596fd7ce5SGreg Kroah-Hartman 
72696fd7ce5SGreg Kroah-Hartman /**
72796fd7ce5SGreg Kroah-Hartman  *	tty_vhangup_self	-	process vhangup for own ctty
72896fd7ce5SGreg Kroah-Hartman  *
72996fd7ce5SGreg Kroah-Hartman  *	Perform a vhangup on the current controlling tty
73096fd7ce5SGreg Kroah-Hartman  */
73196fd7ce5SGreg Kroah-Hartman 
73296fd7ce5SGreg Kroah-Hartman void tty_vhangup_self(void)
73396fd7ce5SGreg Kroah-Hartman {
73496fd7ce5SGreg Kroah-Hartman 	struct tty_struct *tty;
73596fd7ce5SGreg Kroah-Hartman 
73696fd7ce5SGreg Kroah-Hartman 	tty = get_current_tty();
73796fd7ce5SGreg Kroah-Hartman 	if (tty) {
73896fd7ce5SGreg Kroah-Hartman 		tty_vhangup(tty);
73996fd7ce5SGreg Kroah-Hartman 		tty_kref_put(tty);
74096fd7ce5SGreg Kroah-Hartman 	}
74196fd7ce5SGreg Kroah-Hartman }
74296fd7ce5SGreg Kroah-Hartman 
74396fd7ce5SGreg Kroah-Hartman /**
74496fd7ce5SGreg Kroah-Hartman  *	tty_hung_up_p		-	was tty hung up
74596fd7ce5SGreg Kroah-Hartman  *	@filp: file pointer of tty
74696fd7ce5SGreg Kroah-Hartman  *
74796fd7ce5SGreg Kroah-Hartman  *	Return true if the tty has been subject to a vhangup or a carrier
74896fd7ce5SGreg Kroah-Hartman  *	loss
74996fd7ce5SGreg Kroah-Hartman  */
75096fd7ce5SGreg Kroah-Hartman 
75196fd7ce5SGreg Kroah-Hartman int tty_hung_up_p(struct file *filp)
75296fd7ce5SGreg Kroah-Hartman {
75396fd7ce5SGreg Kroah-Hartman 	return (filp->f_op == &hung_up_tty_fops);
75496fd7ce5SGreg Kroah-Hartman }
75596fd7ce5SGreg Kroah-Hartman 
75696fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_hung_up_p);
75796fd7ce5SGreg Kroah-Hartman 
75896fd7ce5SGreg Kroah-Hartman static void session_clear_tty(struct pid *session)
75996fd7ce5SGreg Kroah-Hartman {
76096fd7ce5SGreg Kroah-Hartman 	struct task_struct *p;
76196fd7ce5SGreg Kroah-Hartman 	do_each_pid_task(session, PIDTYPE_SID, p) {
76296fd7ce5SGreg Kroah-Hartman 		proc_clear_tty(p);
76396fd7ce5SGreg Kroah-Hartman 	} while_each_pid_task(session, PIDTYPE_SID, p);
76496fd7ce5SGreg Kroah-Hartman }
76596fd7ce5SGreg Kroah-Hartman 
76696fd7ce5SGreg Kroah-Hartman /**
76796fd7ce5SGreg Kroah-Hartman  *	disassociate_ctty	-	disconnect controlling tty
76896fd7ce5SGreg Kroah-Hartman  *	@on_exit: true if exiting so need to "hang up" the session
76996fd7ce5SGreg Kroah-Hartman  *
77096fd7ce5SGreg Kroah-Hartman  *	This function is typically called only by the session leader, when
77196fd7ce5SGreg Kroah-Hartman  *	it wants to disassociate itself from its controlling tty.
77296fd7ce5SGreg Kroah-Hartman  *
77396fd7ce5SGreg Kroah-Hartman  *	It performs the following functions:
77496fd7ce5SGreg Kroah-Hartman  * 	(1)  Sends a SIGHUP and SIGCONT to the foreground process group
77596fd7ce5SGreg Kroah-Hartman  * 	(2)  Clears the tty from being controlling the session
77696fd7ce5SGreg Kroah-Hartman  * 	(3)  Clears the controlling tty for all processes in the
77796fd7ce5SGreg Kroah-Hartman  * 		session group.
77896fd7ce5SGreg Kroah-Hartman  *
77996fd7ce5SGreg Kroah-Hartman  *	The argument on_exit is set to 1 if called when a process is
78096fd7ce5SGreg Kroah-Hartman  *	exiting; it is 0 if called by the ioctl TIOCNOTTY.
78196fd7ce5SGreg Kroah-Hartman  *
78296fd7ce5SGreg Kroah-Hartman  *	Locking:
78396fd7ce5SGreg Kroah-Hartman  *		BTM is taken for hysterical raisins, and held when
78496fd7ce5SGreg Kroah-Hartman  *		  called from no_tty().
78596fd7ce5SGreg Kroah-Hartman  *		  tty_mutex is taken to protect tty
78696fd7ce5SGreg Kroah-Hartman  *		  ->siglock is taken to protect ->signal/->sighand
78796fd7ce5SGreg Kroah-Hartman  *		  tasklist_lock is taken to walk process list for sessions
78896fd7ce5SGreg Kroah-Hartman  *		    ->siglock is taken to protect ->signal/->sighand
78996fd7ce5SGreg Kroah-Hartman  */
79096fd7ce5SGreg Kroah-Hartman 
79196fd7ce5SGreg Kroah-Hartman void disassociate_ctty(int on_exit)
79296fd7ce5SGreg Kroah-Hartman {
79396fd7ce5SGreg Kroah-Hartman 	struct tty_struct *tty;
79496fd7ce5SGreg Kroah-Hartman 
79596fd7ce5SGreg Kroah-Hartman 	if (!current->signal->leader)
79696fd7ce5SGreg Kroah-Hartman 		return;
79796fd7ce5SGreg Kroah-Hartman 
79896fd7ce5SGreg Kroah-Hartman 	tty = get_current_tty();
79996fd7ce5SGreg Kroah-Hartman 	if (tty) {
8001411dc4aSJiri Slaby 		struct pid *tty_pgrp = get_pid(tty->pgrp);
80196fd7ce5SGreg Kroah-Hartman 		if (on_exit) {
80296fd7ce5SGreg Kroah-Hartman 			if (tty->driver->type != TTY_DRIVER_TYPE_PTY)
80396fd7ce5SGreg Kroah-Hartman 				tty_vhangup(tty);
80496fd7ce5SGreg Kroah-Hartman 		}
80596fd7ce5SGreg Kroah-Hartman 		tty_kref_put(tty);
8061411dc4aSJiri Slaby 		if (tty_pgrp) {
8071411dc4aSJiri Slaby 			kill_pgrp(tty_pgrp, SIGHUP, on_exit);
8081411dc4aSJiri Slaby 			if (!on_exit)
8091411dc4aSJiri Slaby 				kill_pgrp(tty_pgrp, SIGCONT, on_exit);
8101411dc4aSJiri Slaby 			put_pid(tty_pgrp);
8111411dc4aSJiri Slaby 		}
81296fd7ce5SGreg Kroah-Hartman 	} else if (on_exit) {
81396fd7ce5SGreg Kroah-Hartman 		struct pid *old_pgrp;
81496fd7ce5SGreg Kroah-Hartman 		spin_lock_irq(&current->sighand->siglock);
81596fd7ce5SGreg Kroah-Hartman 		old_pgrp = current->signal->tty_old_pgrp;
81696fd7ce5SGreg Kroah-Hartman 		current->signal->tty_old_pgrp = NULL;
81796fd7ce5SGreg Kroah-Hartman 		spin_unlock_irq(&current->sighand->siglock);
81896fd7ce5SGreg Kroah-Hartman 		if (old_pgrp) {
81996fd7ce5SGreg Kroah-Hartman 			kill_pgrp(old_pgrp, SIGHUP, on_exit);
82096fd7ce5SGreg Kroah-Hartman 			kill_pgrp(old_pgrp, SIGCONT, on_exit);
82196fd7ce5SGreg Kroah-Hartman 			put_pid(old_pgrp);
82296fd7ce5SGreg Kroah-Hartman 		}
82396fd7ce5SGreg Kroah-Hartman 		return;
82496fd7ce5SGreg Kroah-Hartman 	}
82596fd7ce5SGreg Kroah-Hartman 
82696fd7ce5SGreg Kroah-Hartman 	spin_lock_irq(&current->sighand->siglock);
82796fd7ce5SGreg Kroah-Hartman 	put_pid(current->signal->tty_old_pgrp);
82896fd7ce5SGreg Kroah-Hartman 	current->signal->tty_old_pgrp = NULL;
82996fd7ce5SGreg Kroah-Hartman 	spin_unlock_irq(&current->sighand->siglock);
83096fd7ce5SGreg Kroah-Hartman 
83196fd7ce5SGreg Kroah-Hartman 	tty = get_current_tty();
83296fd7ce5SGreg Kroah-Hartman 	if (tty) {
83396fd7ce5SGreg Kroah-Hartman 		unsigned long flags;
83496fd7ce5SGreg Kroah-Hartman 		spin_lock_irqsave(&tty->ctrl_lock, flags);
83596fd7ce5SGreg Kroah-Hartman 		put_pid(tty->session);
83696fd7ce5SGreg Kroah-Hartman 		put_pid(tty->pgrp);
83796fd7ce5SGreg Kroah-Hartman 		tty->session = NULL;
83896fd7ce5SGreg Kroah-Hartman 		tty->pgrp = NULL;
83996fd7ce5SGreg Kroah-Hartman 		spin_unlock_irqrestore(&tty->ctrl_lock, flags);
84096fd7ce5SGreg Kroah-Hartman 		tty_kref_put(tty);
84196fd7ce5SGreg Kroah-Hartman 	} else {
84296fd7ce5SGreg Kroah-Hartman #ifdef TTY_DEBUG_HANGUP
84396fd7ce5SGreg Kroah-Hartman 		printk(KERN_DEBUG "error attempted to write to tty [0x%p]"
84496fd7ce5SGreg Kroah-Hartman 		       " = NULL", tty);
84596fd7ce5SGreg Kroah-Hartman #endif
84696fd7ce5SGreg Kroah-Hartman 	}
84796fd7ce5SGreg Kroah-Hartman 
84896fd7ce5SGreg Kroah-Hartman 	/* Now clear signal->tty under the lock */
84996fd7ce5SGreg Kroah-Hartman 	read_lock(&tasklist_lock);
85096fd7ce5SGreg Kroah-Hartman 	session_clear_tty(task_session(current));
85196fd7ce5SGreg Kroah-Hartman 	read_unlock(&tasklist_lock);
85296fd7ce5SGreg Kroah-Hartman }
85396fd7ce5SGreg Kroah-Hartman 
85496fd7ce5SGreg Kroah-Hartman /**
85596fd7ce5SGreg Kroah-Hartman  *
85696fd7ce5SGreg Kroah-Hartman  *	no_tty	- Ensure the current process does not have a controlling tty
85796fd7ce5SGreg Kroah-Hartman  */
85896fd7ce5SGreg Kroah-Hartman void no_tty(void)
85996fd7ce5SGreg Kroah-Hartman {
8603af502b9SAlan Cox 	/* FIXME: Review locking here. The tty_lock never covered any race
8613af502b9SAlan Cox 	   between a new association and proc_clear_tty but possible we need
8623af502b9SAlan Cox 	   to protect against this anyway */
86396fd7ce5SGreg Kroah-Hartman 	struct task_struct *tsk = current;
86496fd7ce5SGreg Kroah-Hartman 	disassociate_ctty(0);
86596fd7ce5SGreg Kroah-Hartman 	proc_clear_tty(tsk);
86696fd7ce5SGreg Kroah-Hartman }
86796fd7ce5SGreg Kroah-Hartman 
86896fd7ce5SGreg Kroah-Hartman 
86996fd7ce5SGreg Kroah-Hartman /**
87096fd7ce5SGreg Kroah-Hartman  *	stop_tty	-	propagate flow control
87196fd7ce5SGreg Kroah-Hartman  *	@tty: tty to stop
87296fd7ce5SGreg Kroah-Hartman  *
87396fd7ce5SGreg Kroah-Hartman  *	Perform flow control to the driver. For PTY/TTY pairs we
87496fd7ce5SGreg Kroah-Hartman  *	must also propagate the TIOCKPKT status. May be called
87596fd7ce5SGreg Kroah-Hartman  *	on an already stopped device and will not re-call the driver
87696fd7ce5SGreg Kroah-Hartman  *	method.
87796fd7ce5SGreg Kroah-Hartman  *
87896fd7ce5SGreg Kroah-Hartman  *	This functionality is used by both the line disciplines for
87996fd7ce5SGreg Kroah-Hartman  *	halting incoming flow and by the driver. It may therefore be
88096fd7ce5SGreg Kroah-Hartman  *	called from any context, may be under the tty atomic_write_lock
88196fd7ce5SGreg Kroah-Hartman  *	but not always.
88296fd7ce5SGreg Kroah-Hartman  *
88396fd7ce5SGreg Kroah-Hartman  *	Locking:
88496fd7ce5SGreg Kroah-Hartman  *		Uses the tty control lock internally
88596fd7ce5SGreg Kroah-Hartman  */
88696fd7ce5SGreg Kroah-Hartman 
88796fd7ce5SGreg Kroah-Hartman void stop_tty(struct tty_struct *tty)
88896fd7ce5SGreg Kroah-Hartman {
88996fd7ce5SGreg Kroah-Hartman 	unsigned long flags;
89096fd7ce5SGreg Kroah-Hartman 	spin_lock_irqsave(&tty->ctrl_lock, flags);
89196fd7ce5SGreg Kroah-Hartman 	if (tty->stopped) {
89296fd7ce5SGreg Kroah-Hartman 		spin_unlock_irqrestore(&tty->ctrl_lock, flags);
89396fd7ce5SGreg Kroah-Hartman 		return;
89496fd7ce5SGreg Kroah-Hartman 	}
89596fd7ce5SGreg Kroah-Hartman 	tty->stopped = 1;
89696fd7ce5SGreg Kroah-Hartman 	if (tty->link && tty->link->packet) {
89796fd7ce5SGreg Kroah-Hartman 		tty->ctrl_status &= ~TIOCPKT_START;
89896fd7ce5SGreg Kroah-Hartman 		tty->ctrl_status |= TIOCPKT_STOP;
89996fd7ce5SGreg Kroah-Hartman 		wake_up_interruptible_poll(&tty->link->read_wait, POLLIN);
90096fd7ce5SGreg Kroah-Hartman 	}
90196fd7ce5SGreg Kroah-Hartman 	spin_unlock_irqrestore(&tty->ctrl_lock, flags);
90296fd7ce5SGreg Kroah-Hartman 	if (tty->ops->stop)
90396fd7ce5SGreg Kroah-Hartman 		(tty->ops->stop)(tty);
90496fd7ce5SGreg Kroah-Hartman }
90596fd7ce5SGreg Kroah-Hartman 
90696fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(stop_tty);
90796fd7ce5SGreg Kroah-Hartman 
90896fd7ce5SGreg Kroah-Hartman /**
90996fd7ce5SGreg Kroah-Hartman  *	start_tty	-	propagate flow control
91096fd7ce5SGreg Kroah-Hartman  *	@tty: tty to start
91196fd7ce5SGreg Kroah-Hartman  *
91296fd7ce5SGreg Kroah-Hartman  *	Start a tty that has been stopped if at all possible. Perform
91396fd7ce5SGreg Kroah-Hartman  *	any necessary wakeups and propagate the TIOCPKT status. If this
91496fd7ce5SGreg Kroah-Hartman  *	is the tty was previous stopped and is being started then the
91596fd7ce5SGreg Kroah-Hartman  *	driver start method is invoked and the line discipline woken.
91696fd7ce5SGreg Kroah-Hartman  *
91796fd7ce5SGreg Kroah-Hartman  *	Locking:
91896fd7ce5SGreg Kroah-Hartman  *		ctrl_lock
91996fd7ce5SGreg Kroah-Hartman  */
92096fd7ce5SGreg Kroah-Hartman 
92196fd7ce5SGreg Kroah-Hartman void start_tty(struct tty_struct *tty)
92296fd7ce5SGreg Kroah-Hartman {
92396fd7ce5SGreg Kroah-Hartman 	unsigned long flags;
92496fd7ce5SGreg Kroah-Hartman 	spin_lock_irqsave(&tty->ctrl_lock, flags);
92596fd7ce5SGreg Kroah-Hartman 	if (!tty->stopped || tty->flow_stopped) {
92696fd7ce5SGreg Kroah-Hartman 		spin_unlock_irqrestore(&tty->ctrl_lock, flags);
92796fd7ce5SGreg Kroah-Hartman 		return;
92896fd7ce5SGreg Kroah-Hartman 	}
92996fd7ce5SGreg Kroah-Hartman 	tty->stopped = 0;
93096fd7ce5SGreg Kroah-Hartman 	if (tty->link && tty->link->packet) {
93196fd7ce5SGreg Kroah-Hartman 		tty->ctrl_status &= ~TIOCPKT_STOP;
93296fd7ce5SGreg Kroah-Hartman 		tty->ctrl_status |= TIOCPKT_START;
93396fd7ce5SGreg Kroah-Hartman 		wake_up_interruptible_poll(&tty->link->read_wait, POLLIN);
93496fd7ce5SGreg Kroah-Hartman 	}
93596fd7ce5SGreg Kroah-Hartman 	spin_unlock_irqrestore(&tty->ctrl_lock, flags);
93696fd7ce5SGreg Kroah-Hartman 	if (tty->ops->start)
93796fd7ce5SGreg Kroah-Hartman 		(tty->ops->start)(tty);
93896fd7ce5SGreg Kroah-Hartman 	/* If we have a running line discipline it may need kicking */
93996fd7ce5SGreg Kroah-Hartman 	tty_wakeup(tty);
94096fd7ce5SGreg Kroah-Hartman }
94196fd7ce5SGreg Kroah-Hartman 
94296fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(start_tty);
94396fd7ce5SGreg Kroah-Hartman 
94496fd7ce5SGreg Kroah-Hartman /**
94596fd7ce5SGreg Kroah-Hartman  *	tty_read	-	read method for tty device files
94696fd7ce5SGreg Kroah-Hartman  *	@file: pointer to tty file
94796fd7ce5SGreg Kroah-Hartman  *	@buf: user buffer
94896fd7ce5SGreg Kroah-Hartman  *	@count: size of user buffer
94996fd7ce5SGreg Kroah-Hartman  *	@ppos: unused
95096fd7ce5SGreg Kroah-Hartman  *
95196fd7ce5SGreg Kroah-Hartman  *	Perform the read system call function on this terminal device. Checks
95296fd7ce5SGreg Kroah-Hartman  *	for hung up devices before calling the line discipline method.
95396fd7ce5SGreg Kroah-Hartman  *
95496fd7ce5SGreg Kroah-Hartman  *	Locking:
95596fd7ce5SGreg Kroah-Hartman  *		Locks the line discipline internally while needed. Multiple
95696fd7ce5SGreg Kroah-Hartman  *	read calls may be outstanding in parallel.
95796fd7ce5SGreg Kroah-Hartman  */
95896fd7ce5SGreg Kroah-Hartman 
95996fd7ce5SGreg Kroah-Hartman static ssize_t tty_read(struct file *file, char __user *buf, size_t count,
96096fd7ce5SGreg Kroah-Hartman 			loff_t *ppos)
96196fd7ce5SGreg Kroah-Hartman {
96296fd7ce5SGreg Kroah-Hartman 	int i;
96396fd7ce5SGreg Kroah-Hartman 	struct inode *inode = file->f_path.dentry->d_inode;
96496fd7ce5SGreg Kroah-Hartman 	struct tty_struct *tty = file_tty(file);
96596fd7ce5SGreg Kroah-Hartman 	struct tty_ldisc *ld;
96696fd7ce5SGreg Kroah-Hartman 
96796fd7ce5SGreg Kroah-Hartman 	if (tty_paranoia_check(tty, inode, "tty_read"))
96896fd7ce5SGreg Kroah-Hartman 		return -EIO;
96996fd7ce5SGreg Kroah-Hartman 	if (!tty || (test_bit(TTY_IO_ERROR, &tty->flags)))
97096fd7ce5SGreg Kroah-Hartman 		return -EIO;
97196fd7ce5SGreg Kroah-Hartman 
97296fd7ce5SGreg Kroah-Hartman 	/* We want to wait for the line discipline to sort out in this
97396fd7ce5SGreg Kroah-Hartman 	   situation */
97496fd7ce5SGreg Kroah-Hartman 	ld = tty_ldisc_ref_wait(tty);
97596fd7ce5SGreg Kroah-Hartman 	if (ld->ops->read)
97696fd7ce5SGreg Kroah-Hartman 		i = (ld->ops->read)(tty, file, buf, count);
97796fd7ce5SGreg Kroah-Hartman 	else
97896fd7ce5SGreg Kroah-Hartman 		i = -EIO;
97996fd7ce5SGreg Kroah-Hartman 	tty_ldisc_deref(ld);
98096fd7ce5SGreg Kroah-Hartman 	if (i > 0)
98196fd7ce5SGreg Kroah-Hartman 		inode->i_atime = current_fs_time(inode->i_sb);
98296fd7ce5SGreg Kroah-Hartman 	return i;
98396fd7ce5SGreg Kroah-Hartman }
98496fd7ce5SGreg Kroah-Hartman 
98596fd7ce5SGreg Kroah-Hartman void tty_write_unlock(struct tty_struct *tty)
98683c67571SJiri Slaby 	__releases(&tty->atomic_write_lock)
98796fd7ce5SGreg Kroah-Hartman {
98896fd7ce5SGreg Kroah-Hartman 	mutex_unlock(&tty->atomic_write_lock);
98996fd7ce5SGreg Kroah-Hartman 	wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
99096fd7ce5SGreg Kroah-Hartman }
99196fd7ce5SGreg Kroah-Hartman 
99296fd7ce5SGreg Kroah-Hartman int tty_write_lock(struct tty_struct *tty, int ndelay)
99383c67571SJiri Slaby 	__acquires(&tty->atomic_write_lock)
99496fd7ce5SGreg Kroah-Hartman {
99596fd7ce5SGreg Kroah-Hartman 	if (!mutex_trylock(&tty->atomic_write_lock)) {
99696fd7ce5SGreg Kroah-Hartman 		if (ndelay)
99796fd7ce5SGreg Kroah-Hartman 			return -EAGAIN;
99896fd7ce5SGreg Kroah-Hartman 		if (mutex_lock_interruptible(&tty->atomic_write_lock))
99996fd7ce5SGreg Kroah-Hartman 			return -ERESTARTSYS;
100096fd7ce5SGreg Kroah-Hartman 	}
100196fd7ce5SGreg Kroah-Hartman 	return 0;
100296fd7ce5SGreg Kroah-Hartman }
100396fd7ce5SGreg Kroah-Hartman 
100496fd7ce5SGreg Kroah-Hartman /*
100596fd7ce5SGreg Kroah-Hartman  * Split writes up in sane blocksizes to avoid
100696fd7ce5SGreg Kroah-Hartman  * denial-of-service type attacks
100796fd7ce5SGreg Kroah-Hartman  */
100896fd7ce5SGreg Kroah-Hartman static inline ssize_t do_tty_write(
100996fd7ce5SGreg Kroah-Hartman 	ssize_t (*write)(struct tty_struct *, struct file *, const unsigned char *, size_t),
101096fd7ce5SGreg Kroah-Hartman 	struct tty_struct *tty,
101196fd7ce5SGreg Kroah-Hartman 	struct file *file,
101296fd7ce5SGreg Kroah-Hartman 	const char __user *buf,
101396fd7ce5SGreg Kroah-Hartman 	size_t count)
101496fd7ce5SGreg Kroah-Hartman {
101596fd7ce5SGreg Kroah-Hartman 	ssize_t ret, written = 0;
101696fd7ce5SGreg Kroah-Hartman 	unsigned int chunk;
101796fd7ce5SGreg Kroah-Hartman 
101896fd7ce5SGreg Kroah-Hartman 	ret = tty_write_lock(tty, file->f_flags & O_NDELAY);
101996fd7ce5SGreg Kroah-Hartman 	if (ret < 0)
102096fd7ce5SGreg Kroah-Hartman 		return ret;
102196fd7ce5SGreg Kroah-Hartman 
102296fd7ce5SGreg Kroah-Hartman 	/*
102396fd7ce5SGreg Kroah-Hartman 	 * We chunk up writes into a temporary buffer. This
102496fd7ce5SGreg Kroah-Hartman 	 * simplifies low-level drivers immensely, since they
102596fd7ce5SGreg Kroah-Hartman 	 * don't have locking issues and user mode accesses.
102696fd7ce5SGreg Kroah-Hartman 	 *
102796fd7ce5SGreg Kroah-Hartman 	 * But if TTY_NO_WRITE_SPLIT is set, we should use a
102896fd7ce5SGreg Kroah-Hartman 	 * big chunk-size..
102996fd7ce5SGreg Kroah-Hartman 	 *
103096fd7ce5SGreg Kroah-Hartman 	 * The default chunk-size is 2kB, because the NTTY
103196fd7ce5SGreg Kroah-Hartman 	 * layer has problems with bigger chunks. It will
103296fd7ce5SGreg Kroah-Hartman 	 * claim to be able to handle more characters than
103396fd7ce5SGreg Kroah-Hartman 	 * it actually does.
103496fd7ce5SGreg Kroah-Hartman 	 *
103596fd7ce5SGreg Kroah-Hartman 	 * FIXME: This can probably go away now except that 64K chunks
103696fd7ce5SGreg Kroah-Hartman 	 * are too likely to fail unless switched to vmalloc...
103796fd7ce5SGreg Kroah-Hartman 	 */
103896fd7ce5SGreg Kroah-Hartman 	chunk = 2048;
103996fd7ce5SGreg Kroah-Hartman 	if (test_bit(TTY_NO_WRITE_SPLIT, &tty->flags))
104096fd7ce5SGreg Kroah-Hartman 		chunk = 65536;
104196fd7ce5SGreg Kroah-Hartman 	if (count < chunk)
104296fd7ce5SGreg Kroah-Hartman 		chunk = count;
104396fd7ce5SGreg Kroah-Hartman 
104496fd7ce5SGreg Kroah-Hartman 	/* write_buf/write_cnt is protected by the atomic_write_lock mutex */
104596fd7ce5SGreg Kroah-Hartman 	if (tty->write_cnt < chunk) {
104696fd7ce5SGreg Kroah-Hartman 		unsigned char *buf_chunk;
104796fd7ce5SGreg Kroah-Hartman 
104896fd7ce5SGreg Kroah-Hartman 		if (chunk < 1024)
104996fd7ce5SGreg Kroah-Hartman 			chunk = 1024;
105096fd7ce5SGreg Kroah-Hartman 
105196fd7ce5SGreg Kroah-Hartman 		buf_chunk = kmalloc(chunk, GFP_KERNEL);
105296fd7ce5SGreg Kroah-Hartman 		if (!buf_chunk) {
105396fd7ce5SGreg Kroah-Hartman 			ret = -ENOMEM;
105496fd7ce5SGreg Kroah-Hartman 			goto out;
105596fd7ce5SGreg Kroah-Hartman 		}
105696fd7ce5SGreg Kroah-Hartman 		kfree(tty->write_buf);
105796fd7ce5SGreg Kroah-Hartman 		tty->write_cnt = chunk;
105896fd7ce5SGreg Kroah-Hartman 		tty->write_buf = buf_chunk;
105996fd7ce5SGreg Kroah-Hartman 	}
106096fd7ce5SGreg Kroah-Hartman 
106196fd7ce5SGreg Kroah-Hartman 	/* Do the write .. */
106296fd7ce5SGreg Kroah-Hartman 	for (;;) {
106396fd7ce5SGreg Kroah-Hartman 		size_t size = count;
106496fd7ce5SGreg Kroah-Hartman 		if (size > chunk)
106596fd7ce5SGreg Kroah-Hartman 			size = chunk;
106696fd7ce5SGreg Kroah-Hartman 		ret = -EFAULT;
106796fd7ce5SGreg Kroah-Hartman 		if (copy_from_user(tty->write_buf, buf, size))
106896fd7ce5SGreg Kroah-Hartman 			break;
106996fd7ce5SGreg Kroah-Hartman 		ret = write(tty, file, tty->write_buf, size);
107096fd7ce5SGreg Kroah-Hartman 		if (ret <= 0)
107196fd7ce5SGreg Kroah-Hartman 			break;
107296fd7ce5SGreg Kroah-Hartman 		written += ret;
107396fd7ce5SGreg Kroah-Hartman 		buf += ret;
107496fd7ce5SGreg Kroah-Hartman 		count -= ret;
107596fd7ce5SGreg Kroah-Hartman 		if (!count)
107696fd7ce5SGreg Kroah-Hartman 			break;
107796fd7ce5SGreg Kroah-Hartman 		ret = -ERESTARTSYS;
107896fd7ce5SGreg Kroah-Hartman 		if (signal_pending(current))
107996fd7ce5SGreg Kroah-Hartman 			break;
108096fd7ce5SGreg Kroah-Hartman 		cond_resched();
108196fd7ce5SGreg Kroah-Hartman 	}
108296fd7ce5SGreg Kroah-Hartman 	if (written) {
108396fd7ce5SGreg Kroah-Hartman 		struct inode *inode = file->f_path.dentry->d_inode;
108496fd7ce5SGreg Kroah-Hartman 		inode->i_mtime = current_fs_time(inode->i_sb);
108596fd7ce5SGreg Kroah-Hartman 		ret = written;
108696fd7ce5SGreg Kroah-Hartman 	}
108796fd7ce5SGreg Kroah-Hartman out:
108896fd7ce5SGreg Kroah-Hartman 	tty_write_unlock(tty);
108996fd7ce5SGreg Kroah-Hartman 	return ret;
109096fd7ce5SGreg Kroah-Hartman }
109196fd7ce5SGreg Kroah-Hartman 
109296fd7ce5SGreg Kroah-Hartman /**
109396fd7ce5SGreg Kroah-Hartman  * tty_write_message - write a message to a certain tty, not just the console.
109496fd7ce5SGreg Kroah-Hartman  * @tty: the destination tty_struct
109596fd7ce5SGreg Kroah-Hartman  * @msg: the message to write
109696fd7ce5SGreg Kroah-Hartman  *
109796fd7ce5SGreg Kroah-Hartman  * This is used for messages that need to be redirected to a specific tty.
109896fd7ce5SGreg Kroah-Hartman  * We don't put it into the syslog queue right now maybe in the future if
109996fd7ce5SGreg Kroah-Hartman  * really needed.
110096fd7ce5SGreg Kroah-Hartman  *
110196fd7ce5SGreg Kroah-Hartman  * We must still hold the BTM and test the CLOSING flag for the moment.
110296fd7ce5SGreg Kroah-Hartman  */
110396fd7ce5SGreg Kroah-Hartman 
110496fd7ce5SGreg Kroah-Hartman void tty_write_message(struct tty_struct *tty, char *msg)
110596fd7ce5SGreg Kroah-Hartman {
110696fd7ce5SGreg Kroah-Hartman 	if (tty) {
110796fd7ce5SGreg Kroah-Hartman 		mutex_lock(&tty->atomic_write_lock);
110889c8d91eSAlan Cox 		tty_lock(tty);
110996fd7ce5SGreg Kroah-Hartman 		if (tty->ops->write && !test_bit(TTY_CLOSING, &tty->flags)) {
111089c8d91eSAlan Cox 			tty_unlock(tty);
111196fd7ce5SGreg Kroah-Hartman 			tty->ops->write(tty, msg, strlen(msg));
111296fd7ce5SGreg Kroah-Hartman 		} else
111389c8d91eSAlan Cox 			tty_unlock(tty);
111496fd7ce5SGreg Kroah-Hartman 		tty_write_unlock(tty);
111596fd7ce5SGreg Kroah-Hartman 	}
111696fd7ce5SGreg Kroah-Hartman 	return;
111796fd7ce5SGreg Kroah-Hartman }
111896fd7ce5SGreg Kroah-Hartman 
111996fd7ce5SGreg Kroah-Hartman 
112096fd7ce5SGreg Kroah-Hartman /**
112196fd7ce5SGreg Kroah-Hartman  *	tty_write		-	write method for tty device file
112296fd7ce5SGreg Kroah-Hartman  *	@file: tty file pointer
112396fd7ce5SGreg Kroah-Hartman  *	@buf: user data to write
112496fd7ce5SGreg Kroah-Hartman  *	@count: bytes to write
112596fd7ce5SGreg Kroah-Hartman  *	@ppos: unused
112696fd7ce5SGreg Kroah-Hartman  *
112796fd7ce5SGreg Kroah-Hartman  *	Write data to a tty device via the line discipline.
112896fd7ce5SGreg Kroah-Hartman  *
112996fd7ce5SGreg Kroah-Hartman  *	Locking:
113096fd7ce5SGreg Kroah-Hartman  *		Locks the line discipline as required
113196fd7ce5SGreg Kroah-Hartman  *		Writes to the tty driver are serialized by the atomic_write_lock
113296fd7ce5SGreg Kroah-Hartman  *	and are then processed in chunks to the device. The line discipline
113396fd7ce5SGreg Kroah-Hartman  *	write method will not be invoked in parallel for each device.
113496fd7ce5SGreg Kroah-Hartman  */
113596fd7ce5SGreg Kroah-Hartman 
113696fd7ce5SGreg Kroah-Hartman static ssize_t tty_write(struct file *file, const char __user *buf,
113796fd7ce5SGreg Kroah-Hartman 						size_t count, loff_t *ppos)
113896fd7ce5SGreg Kroah-Hartman {
113996fd7ce5SGreg Kroah-Hartman 	struct inode *inode = file->f_path.dentry->d_inode;
114096fd7ce5SGreg Kroah-Hartman 	struct tty_struct *tty = file_tty(file);
114196fd7ce5SGreg Kroah-Hartman  	struct tty_ldisc *ld;
114296fd7ce5SGreg Kroah-Hartman 	ssize_t ret;
114396fd7ce5SGreg Kroah-Hartman 
114496fd7ce5SGreg Kroah-Hartman 	if (tty_paranoia_check(tty, inode, "tty_write"))
114596fd7ce5SGreg Kroah-Hartman 		return -EIO;
114696fd7ce5SGreg Kroah-Hartman 	if (!tty || !tty->ops->write ||
114796fd7ce5SGreg Kroah-Hartman 		(test_bit(TTY_IO_ERROR, &tty->flags)))
114896fd7ce5SGreg Kroah-Hartman 			return -EIO;
114996fd7ce5SGreg Kroah-Hartman 	/* Short term debug to catch buggy drivers */
115096fd7ce5SGreg Kroah-Hartman 	if (tty->ops->write_room == NULL)
115196fd7ce5SGreg Kroah-Hartman 		printk(KERN_ERR "tty driver %s lacks a write_room method.\n",
115296fd7ce5SGreg Kroah-Hartman 			tty->driver->name);
115396fd7ce5SGreg Kroah-Hartman 	ld = tty_ldisc_ref_wait(tty);
115496fd7ce5SGreg Kroah-Hartman 	if (!ld->ops->write)
115596fd7ce5SGreg Kroah-Hartman 		ret = -EIO;
115696fd7ce5SGreg Kroah-Hartman 	else
115796fd7ce5SGreg Kroah-Hartman 		ret = do_tty_write(ld->ops->write, tty, file, buf, count);
115896fd7ce5SGreg Kroah-Hartman 	tty_ldisc_deref(ld);
115996fd7ce5SGreg Kroah-Hartman 	return ret;
116096fd7ce5SGreg Kroah-Hartman }
116196fd7ce5SGreg Kroah-Hartman 
116296fd7ce5SGreg Kroah-Hartman ssize_t redirected_tty_write(struct file *file, const char __user *buf,
116396fd7ce5SGreg Kroah-Hartman 						size_t count, loff_t *ppos)
116496fd7ce5SGreg Kroah-Hartman {
116596fd7ce5SGreg Kroah-Hartman 	struct file *p = NULL;
116696fd7ce5SGreg Kroah-Hartman 
116796fd7ce5SGreg Kroah-Hartman 	spin_lock(&redirect_lock);
1168cb0942b8SAl Viro 	if (redirect)
1169cb0942b8SAl Viro 		p = get_file(redirect);
117096fd7ce5SGreg Kroah-Hartman 	spin_unlock(&redirect_lock);
117196fd7ce5SGreg Kroah-Hartman 
117296fd7ce5SGreg Kroah-Hartman 	if (p) {
117396fd7ce5SGreg Kroah-Hartman 		ssize_t res;
117496fd7ce5SGreg Kroah-Hartman 		res = vfs_write(p, buf, count, &p->f_pos);
117596fd7ce5SGreg Kroah-Hartman 		fput(p);
117696fd7ce5SGreg Kroah-Hartman 		return res;
117796fd7ce5SGreg Kroah-Hartman 	}
117896fd7ce5SGreg Kroah-Hartman 	return tty_write(file, buf, count, ppos);
117996fd7ce5SGreg Kroah-Hartman }
118096fd7ce5SGreg Kroah-Hartman 
118196fd7ce5SGreg Kroah-Hartman static char ptychar[] = "pqrstuvwxyzabcde";
118296fd7ce5SGreg Kroah-Hartman 
118396fd7ce5SGreg Kroah-Hartman /**
118496fd7ce5SGreg Kroah-Hartman  *	pty_line_name	-	generate name for a pty
118596fd7ce5SGreg Kroah-Hartman  *	@driver: the tty driver in use
118696fd7ce5SGreg Kroah-Hartman  *	@index: the minor number
118796fd7ce5SGreg Kroah-Hartman  *	@p: output buffer of at least 6 bytes
118896fd7ce5SGreg Kroah-Hartman  *
118996fd7ce5SGreg Kroah-Hartman  *	Generate a name from a driver reference and write it to the output
119096fd7ce5SGreg Kroah-Hartman  *	buffer.
119196fd7ce5SGreg Kroah-Hartman  *
119296fd7ce5SGreg Kroah-Hartman  *	Locking: None
119396fd7ce5SGreg Kroah-Hartman  */
119496fd7ce5SGreg Kroah-Hartman static void pty_line_name(struct tty_driver *driver, int index, char *p)
119596fd7ce5SGreg Kroah-Hartman {
119696fd7ce5SGreg Kroah-Hartman 	int i = index + driver->name_base;
119796fd7ce5SGreg Kroah-Hartman 	/* ->name is initialized to "ttyp", but "tty" is expected */
119896fd7ce5SGreg Kroah-Hartman 	sprintf(p, "%s%c%x",
119996fd7ce5SGreg Kroah-Hartman 		driver->subtype == PTY_TYPE_SLAVE ? "tty" : driver->name,
120096fd7ce5SGreg Kroah-Hartman 		ptychar[i >> 4 & 0xf], i & 0xf);
120196fd7ce5SGreg Kroah-Hartman }
120296fd7ce5SGreg Kroah-Hartman 
120396fd7ce5SGreg Kroah-Hartman /**
120496fd7ce5SGreg Kroah-Hartman  *	tty_line_name	-	generate name for a tty
120596fd7ce5SGreg Kroah-Hartman  *	@driver: the tty driver in use
120696fd7ce5SGreg Kroah-Hartman  *	@index: the minor number
120796fd7ce5SGreg Kroah-Hartman  *	@p: output buffer of at least 7 bytes
120896fd7ce5SGreg Kroah-Hartman  *
120996fd7ce5SGreg Kroah-Hartman  *	Generate a name from a driver reference and write it to the output
121096fd7ce5SGreg Kroah-Hartman  *	buffer.
121196fd7ce5SGreg Kroah-Hartman  *
121296fd7ce5SGreg Kroah-Hartman  *	Locking: None
121396fd7ce5SGreg Kroah-Hartman  */
121496fd7ce5SGreg Kroah-Hartman static void tty_line_name(struct tty_driver *driver, int index, char *p)
121596fd7ce5SGreg Kroah-Hartman {
12160019b408SJiri Slaby 	if (driver->flags & TTY_DRIVER_UNNUMBERED_NODE)
12170019b408SJiri Slaby 		strcpy(p, driver->name);
12180019b408SJiri Slaby 	else
121996fd7ce5SGreg Kroah-Hartman 		sprintf(p, "%s%d", driver->name, index + driver->name_base);
122096fd7ce5SGreg Kroah-Hartman }
122196fd7ce5SGreg Kroah-Hartman 
122296fd7ce5SGreg Kroah-Hartman /**
122396fd7ce5SGreg Kroah-Hartman  *	tty_driver_lookup_tty() - find an existing tty, if any
122496fd7ce5SGreg Kroah-Hartman  *	@driver: the driver for the tty
122596fd7ce5SGreg Kroah-Hartman  *	@idx:	 the minor number
122696fd7ce5SGreg Kroah-Hartman  *
122796fd7ce5SGreg Kroah-Hartman  *	Return the tty, if found or ERR_PTR() otherwise.
122896fd7ce5SGreg Kroah-Hartman  *
122996fd7ce5SGreg Kroah-Hartman  *	Locking: tty_mutex must be held. If tty is found, the mutex must
123096fd7ce5SGreg Kroah-Hartman  *	be held until the 'fast-open' is also done. Will change once we
123196fd7ce5SGreg Kroah-Hartman  *	have refcounting in the driver and per driver locking
123296fd7ce5SGreg Kroah-Hartman  */
123396fd7ce5SGreg Kroah-Hartman static struct tty_struct *tty_driver_lookup_tty(struct tty_driver *driver,
123496fd7ce5SGreg Kroah-Hartman 		struct inode *inode, int idx)
123596fd7ce5SGreg Kroah-Hartman {
123696fd7ce5SGreg Kroah-Hartman 	if (driver->ops->lookup)
123796fd7ce5SGreg Kroah-Hartman 		return driver->ops->lookup(driver, inode, idx);
123896fd7ce5SGreg Kroah-Hartman 
1239d4834267SJiri Slaby 	return driver->ttys[idx];
124096fd7ce5SGreg Kroah-Hartman }
124196fd7ce5SGreg Kroah-Hartman 
124296fd7ce5SGreg Kroah-Hartman /**
124396fd7ce5SGreg Kroah-Hartman  *	tty_init_termios	-  helper for termios setup
124496fd7ce5SGreg Kroah-Hartman  *	@tty: the tty to set up
124596fd7ce5SGreg Kroah-Hartman  *
124696fd7ce5SGreg Kroah-Hartman  *	Initialise the termios structures for this tty. Thus runs under
124796fd7ce5SGreg Kroah-Hartman  *	the tty_mutex currently so we can be relaxed about ordering.
124896fd7ce5SGreg Kroah-Hartman  */
124996fd7ce5SGreg Kroah-Hartman 
125096fd7ce5SGreg Kroah-Hartman int tty_init_termios(struct tty_struct *tty)
125196fd7ce5SGreg Kroah-Hartman {
125296fd7ce5SGreg Kroah-Hartman 	struct ktermios *tp;
125396fd7ce5SGreg Kroah-Hartman 	int idx = tty->index;
125496fd7ce5SGreg Kroah-Hartman 
125536b3c070SAlan Cox 	if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
125636b3c070SAlan Cox 		tty->termios = tty->driver->init_termios;
125736b3c070SAlan Cox 	else {
125836b3c070SAlan Cox 		/* Check for lazy saved data */
125996fd7ce5SGreg Kroah-Hartman 		tp = tty->driver->termios[idx];
126036b3c070SAlan Cox 		if (tp != NULL)
1261adc8d746SAlan Cox 			tty->termios = *tp;
126236b3c070SAlan Cox 		else
126336b3c070SAlan Cox 			tty->termios = tty->driver->init_termios;
126436b3c070SAlan Cox 	}
126596fd7ce5SGreg Kroah-Hartman 	/* Compatibility until drivers always set this */
1266adc8d746SAlan Cox 	tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios);
1267adc8d746SAlan Cox 	tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios);
126896fd7ce5SGreg Kroah-Hartman 	return 0;
126996fd7ce5SGreg Kroah-Hartman }
127096fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(tty_init_termios);
127196fd7ce5SGreg Kroah-Hartman 
127266d450e8SJiri Slaby int tty_standard_install(struct tty_driver *driver, struct tty_struct *tty)
127366d450e8SJiri Slaby {
127466d450e8SJiri Slaby 	int ret = tty_init_termios(tty);
127566d450e8SJiri Slaby 	if (ret)
127666d450e8SJiri Slaby 		return ret;
127766d450e8SJiri Slaby 
127866d450e8SJiri Slaby 	tty_driver_kref_get(driver);
127966d450e8SJiri Slaby 	tty->count++;
128066d450e8SJiri Slaby 	driver->ttys[tty->index] = tty;
128166d450e8SJiri Slaby 	return 0;
128266d450e8SJiri Slaby }
128366d450e8SJiri Slaby EXPORT_SYMBOL_GPL(tty_standard_install);
128466d450e8SJiri Slaby 
128596fd7ce5SGreg Kroah-Hartman /**
128696fd7ce5SGreg Kroah-Hartman  *	tty_driver_install_tty() - install a tty entry in the driver
128796fd7ce5SGreg Kroah-Hartman  *	@driver: the driver for the tty
128896fd7ce5SGreg Kroah-Hartman  *	@tty: the tty
128996fd7ce5SGreg Kroah-Hartman  *
129096fd7ce5SGreg Kroah-Hartman  *	Install a tty object into the driver tables. The tty->index field
129196fd7ce5SGreg Kroah-Hartman  *	will be set by the time this is called. This method is responsible
129296fd7ce5SGreg Kroah-Hartman  *	for ensuring any need additional structures are allocated and
129396fd7ce5SGreg Kroah-Hartman  *	configured.
129496fd7ce5SGreg Kroah-Hartman  *
129596fd7ce5SGreg Kroah-Hartman  *	Locking: tty_mutex for now
129696fd7ce5SGreg Kroah-Hartman  */
129796fd7ce5SGreg Kroah-Hartman static int tty_driver_install_tty(struct tty_driver *driver,
129896fd7ce5SGreg Kroah-Hartman 						struct tty_struct *tty)
129996fd7ce5SGreg Kroah-Hartman {
130066d450e8SJiri Slaby 	return driver->ops->install ? driver->ops->install(driver, tty) :
130166d450e8SJiri Slaby 		tty_standard_install(driver, tty);
130296fd7ce5SGreg Kroah-Hartman }
130396fd7ce5SGreg Kroah-Hartman 
130496fd7ce5SGreg Kroah-Hartman /**
130596fd7ce5SGreg Kroah-Hartman  *	tty_driver_remove_tty() - remove a tty from the driver tables
130696fd7ce5SGreg Kroah-Hartman  *	@driver: the driver for the tty
130796fd7ce5SGreg Kroah-Hartman  *	@idx:	 the minor number
130896fd7ce5SGreg Kroah-Hartman  *
130996fd7ce5SGreg Kroah-Hartman  *	Remvoe a tty object from the driver tables. The tty->index field
131096fd7ce5SGreg Kroah-Hartman  *	will be set by the time this is called.
131196fd7ce5SGreg Kroah-Hartman  *
131296fd7ce5SGreg Kroah-Hartman  *	Locking: tty_mutex for now
131396fd7ce5SGreg Kroah-Hartman  */
131424d406a6SJiri Slaby void tty_driver_remove_tty(struct tty_driver *driver, struct tty_struct *tty)
131596fd7ce5SGreg Kroah-Hartman {
131696fd7ce5SGreg Kroah-Hartman 	if (driver->ops->remove)
131796fd7ce5SGreg Kroah-Hartman 		driver->ops->remove(driver, tty);
131896fd7ce5SGreg Kroah-Hartman 	else
131996fd7ce5SGreg Kroah-Hartman 		driver->ttys[tty->index] = NULL;
132096fd7ce5SGreg Kroah-Hartman }
132196fd7ce5SGreg Kroah-Hartman 
132296fd7ce5SGreg Kroah-Hartman /*
132396fd7ce5SGreg Kroah-Hartman  * 	tty_reopen()	- fast re-open of an open tty
132496fd7ce5SGreg Kroah-Hartman  * 	@tty	- the tty to open
132596fd7ce5SGreg Kroah-Hartman  *
132696fd7ce5SGreg Kroah-Hartman  *	Return 0 on success, -errno on error.
132796fd7ce5SGreg Kroah-Hartman  *
132896fd7ce5SGreg Kroah-Hartman  *	Locking: tty_mutex must be held from the time the tty was found
132996fd7ce5SGreg Kroah-Hartman  *		 till this open completes.
133096fd7ce5SGreg Kroah-Hartman  */
133196fd7ce5SGreg Kroah-Hartman static int tty_reopen(struct tty_struct *tty)
133296fd7ce5SGreg Kroah-Hartman {
133396fd7ce5SGreg Kroah-Hartman 	struct tty_driver *driver = tty->driver;
133496fd7ce5SGreg Kroah-Hartman 
1335e2efafbfSJiri Slaby 	if (test_bit(TTY_CLOSING, &tty->flags) ||
1336acfa747bSJiri Slaby 			test_bit(TTY_HUPPING, &tty->flags) ||
1337e2efafbfSJiri Slaby 			test_bit(TTY_LDISC_CHANGING, &tty->flags))
133896fd7ce5SGreg Kroah-Hartman 		return -EIO;
133996fd7ce5SGreg Kroah-Hartman 
134096fd7ce5SGreg Kroah-Hartman 	if (driver->type == TTY_DRIVER_TYPE_PTY &&
134196fd7ce5SGreg Kroah-Hartman 	    driver->subtype == PTY_TYPE_MASTER) {
134296fd7ce5SGreg Kroah-Hartman 		/*
134396fd7ce5SGreg Kroah-Hartman 		 * special case for PTY masters: only one open permitted,
134496fd7ce5SGreg Kroah-Hartman 		 * and the slave side open count is incremented as well.
134596fd7ce5SGreg Kroah-Hartman 		 */
134696fd7ce5SGreg Kroah-Hartman 		if (tty->count)
134796fd7ce5SGreg Kroah-Hartman 			return -EIO;
134896fd7ce5SGreg Kroah-Hartman 
134996fd7ce5SGreg Kroah-Hartman 		tty->link->count++;
135096fd7ce5SGreg Kroah-Hartman 	}
135196fd7ce5SGreg Kroah-Hartman 	tty->count++;
135296fd7ce5SGreg Kroah-Hartman 
135396fd7ce5SGreg Kroah-Hartman 	mutex_lock(&tty->ldisc_mutex);
135496fd7ce5SGreg Kroah-Hartman 	WARN_ON(!test_bit(TTY_LDISC, &tty->flags));
135596fd7ce5SGreg Kroah-Hartman 	mutex_unlock(&tty->ldisc_mutex);
135696fd7ce5SGreg Kroah-Hartman 
135796fd7ce5SGreg Kroah-Hartman 	return 0;
135896fd7ce5SGreg Kroah-Hartman }
135996fd7ce5SGreg Kroah-Hartman 
136096fd7ce5SGreg Kroah-Hartman /**
136196fd7ce5SGreg Kroah-Hartman  *	tty_init_dev		-	initialise a tty device
136296fd7ce5SGreg Kroah-Hartman  *	@driver: tty driver we are opening a device on
136396fd7ce5SGreg Kroah-Hartman  *	@idx: device index
136496fd7ce5SGreg Kroah-Hartman  *	@ret_tty: returned tty structure
136596fd7ce5SGreg Kroah-Hartman  *
136696fd7ce5SGreg Kroah-Hartman  *	Prepare a tty device. This may not be a "new" clean device but
136796fd7ce5SGreg Kroah-Hartman  *	could also be an active device. The pty drivers require special
136896fd7ce5SGreg Kroah-Hartman  *	handling because of this.
136996fd7ce5SGreg Kroah-Hartman  *
137096fd7ce5SGreg Kroah-Hartman  *	Locking:
137196fd7ce5SGreg Kroah-Hartman  *		The function is called under the tty_mutex, which
137296fd7ce5SGreg Kroah-Hartman  *	protects us from the tty struct or driver itself going away.
137396fd7ce5SGreg Kroah-Hartman  *
137496fd7ce5SGreg Kroah-Hartman  *	On exit the tty device has the line discipline attached and
137596fd7ce5SGreg Kroah-Hartman  *	a reference count of 1. If a pair was created for pty/tty use
137696fd7ce5SGreg Kroah-Hartman  *	and the other was a pty master then it too has a reference count of 1.
137796fd7ce5SGreg Kroah-Hartman  *
137896fd7ce5SGreg Kroah-Hartman  * WSH 06/09/97: Rewritten to remove races and properly clean up after a
137996fd7ce5SGreg Kroah-Hartman  * failed open.  The new code protects the open with a mutex, so it's
138096fd7ce5SGreg Kroah-Hartman  * really quite straightforward.  The mutex locking can probably be
138196fd7ce5SGreg Kroah-Hartman  * relaxed for the (most common) case of reopening a tty.
138296fd7ce5SGreg Kroah-Hartman  */
138396fd7ce5SGreg Kroah-Hartman 
1384593a27c4SKonstantin Khlebnikov struct tty_struct *tty_init_dev(struct tty_driver *driver, int idx)
138596fd7ce5SGreg Kroah-Hartman {
138696fd7ce5SGreg Kroah-Hartman 	struct tty_struct *tty;
138796fd7ce5SGreg Kroah-Hartman 	int retval;
138896fd7ce5SGreg Kroah-Hartman 
138996fd7ce5SGreg Kroah-Hartman 	/*
139096fd7ce5SGreg Kroah-Hartman 	 * First time open is complex, especially for PTY devices.
139196fd7ce5SGreg Kroah-Hartman 	 * This code guarantees that either everything succeeds and the
139296fd7ce5SGreg Kroah-Hartman 	 * TTY is ready for operation, or else the table slots are vacated
139396fd7ce5SGreg Kroah-Hartman 	 * and the allocated memory released.  (Except that the termios
139496fd7ce5SGreg Kroah-Hartman 	 * and locked termios may be retained.)
139596fd7ce5SGreg Kroah-Hartman 	 */
139696fd7ce5SGreg Kroah-Hartman 
139796fd7ce5SGreg Kroah-Hartman 	if (!try_module_get(driver->owner))
139896fd7ce5SGreg Kroah-Hartman 		return ERR_PTR(-ENODEV);
139996fd7ce5SGreg Kroah-Hartman 
140096fd7ce5SGreg Kroah-Hartman 	tty = alloc_tty_struct();
1401d5543503SJiri Slaby 	if (!tty) {
1402d5543503SJiri Slaby 		retval = -ENOMEM;
1403d5543503SJiri Slaby 		goto err_module_put;
1404d5543503SJiri Slaby 	}
140596fd7ce5SGreg Kroah-Hartman 	initialize_tty_struct(tty, driver, idx);
140696fd7ce5SGreg Kroah-Hartman 
140789c8d91eSAlan Cox 	tty_lock(tty);
140896fd7ce5SGreg Kroah-Hartman 	retval = tty_driver_install_tty(driver, tty);
1409d5543503SJiri Slaby 	if (retval < 0)
1410a9dccddbSJiri Slaby 		goto err_deinit_tty;
141196fd7ce5SGreg Kroah-Hartman 
141204831dc1SJiri Slaby 	if (!tty->port)
141304831dc1SJiri Slaby 		tty->port = driver->ports[idx];
141404831dc1SJiri Slaby 
14155d4121c0SJiri Slaby 	WARN_RATELIMIT(!tty->port,
14165d4121c0SJiri Slaby 			"%s: %s driver does not set tty->port. This will crash the kernel later. Fix the driver!\n",
14175d4121c0SJiri Slaby 			__func__, tty->driver->name);
14185d4121c0SJiri Slaby 
1419967fab69SJiri Slaby 	tty->port->itty = tty;
1420967fab69SJiri Slaby 
142196fd7ce5SGreg Kroah-Hartman 	/*
142296fd7ce5SGreg Kroah-Hartman 	 * Structures all installed ... call the ldisc open routines.
142396fd7ce5SGreg Kroah-Hartman 	 * If we fail here just call release_tty to clean up.  No need
142496fd7ce5SGreg Kroah-Hartman 	 * to decrement the use counts, as release_tty doesn't care.
142596fd7ce5SGreg Kroah-Hartman 	 */
142696fd7ce5SGreg Kroah-Hartman 	retval = tty_ldisc_setup(tty, tty->link);
142796fd7ce5SGreg Kroah-Hartman 	if (retval)
1428d5543503SJiri Slaby 		goto err_release_tty;
142989c8d91eSAlan Cox 	/* Return the tty locked so that it cannot vanish under the caller */
143096fd7ce5SGreg Kroah-Hartman 	return tty;
143196fd7ce5SGreg Kroah-Hartman 
1432a9dccddbSJiri Slaby err_deinit_tty:
143389c8d91eSAlan Cox 	tty_unlock(tty);
1434a9dccddbSJiri Slaby 	deinitialize_tty_struct(tty);
1435d5543503SJiri Slaby 	free_tty_struct(tty);
1436d5543503SJiri Slaby err_module_put:
143796fd7ce5SGreg Kroah-Hartman 	module_put(driver->owner);
1438d5543503SJiri Slaby 	return ERR_PTR(retval);
143996fd7ce5SGreg Kroah-Hartman 
144096fd7ce5SGreg Kroah-Hartman 	/* call the tty release_tty routine to clean out this slot */
1441d5543503SJiri Slaby err_release_tty:
144289c8d91eSAlan Cox 	tty_unlock(tty);
14435a3c6b25SManuel Zerpies 	printk_ratelimited(KERN_INFO "tty_init_dev: ldisc open failed, "
144496fd7ce5SGreg Kroah-Hartman 				 "clearing slot %d\n", idx);
144596fd7ce5SGreg Kroah-Hartman 	release_tty(tty, idx);
144696fd7ce5SGreg Kroah-Hartman 	return ERR_PTR(retval);
144796fd7ce5SGreg Kroah-Hartman }
144896fd7ce5SGreg Kroah-Hartman 
144996fd7ce5SGreg Kroah-Hartman void tty_free_termios(struct tty_struct *tty)
145096fd7ce5SGreg Kroah-Hartman {
145196fd7ce5SGreg Kroah-Hartman 	struct ktermios *tp;
145296fd7ce5SGreg Kroah-Hartman 	int idx = tty->index;
145336b3c070SAlan Cox 
145436b3c070SAlan Cox 	/* If the port is going to reset then it has no termios to save */
145536b3c070SAlan Cox 	if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
145636b3c070SAlan Cox 		return;
145736b3c070SAlan Cox 
145836b3c070SAlan Cox 	/* Stash the termios data */
1459adc8d746SAlan Cox 	tp = tty->driver->termios[idx];
146036b3c070SAlan Cox 	if (tp == NULL) {
146136b3c070SAlan Cox 		tp = kmalloc(sizeof(struct ktermios), GFP_KERNEL);
146236b3c070SAlan Cox 		if (tp == NULL) {
146336b3c070SAlan Cox 			pr_warn("tty: no memory to save termios state.\n");
146436b3c070SAlan Cox 			return;
146596fd7ce5SGreg Kroah-Hartman 		}
14664ac5d705SDan Carpenter 		tty->driver->termios[idx] = tp;
146736b3c070SAlan Cox 	}
146836b3c070SAlan Cox 	*tp = tty->termios;
146996fd7ce5SGreg Kroah-Hartman }
147096fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_free_termios);
147196fd7ce5SGreg Kroah-Hartman 
147296fd7ce5SGreg Kroah-Hartman 
147396fd7ce5SGreg Kroah-Hartman /**
147496fd7ce5SGreg Kroah-Hartman  *	release_one_tty		-	release tty structure memory
147596fd7ce5SGreg Kroah-Hartman  *	@kref: kref of tty we are obliterating
147696fd7ce5SGreg Kroah-Hartman  *
147796fd7ce5SGreg Kroah-Hartman  *	Releases memory associated with a tty structure, and clears out the
147896fd7ce5SGreg Kroah-Hartman  *	driver table slots. This function is called when a device is no longer
147996fd7ce5SGreg Kroah-Hartman  *	in use. It also gets called when setup of a device fails.
148096fd7ce5SGreg Kroah-Hartman  *
148196fd7ce5SGreg Kroah-Hartman  *	Locking:
148296fd7ce5SGreg Kroah-Hartman  *		takes the file list lock internally when working on the list
148396fd7ce5SGreg Kroah-Hartman  *	of ttys that the driver keeps.
148496fd7ce5SGreg Kroah-Hartman  *
148596fd7ce5SGreg Kroah-Hartman  *	This method gets called from a work queue so that the driver private
148696fd7ce5SGreg Kroah-Hartman  *	cleanup ops can sleep (needed for USB at least)
148796fd7ce5SGreg Kroah-Hartman  */
148896fd7ce5SGreg Kroah-Hartman static void release_one_tty(struct work_struct *work)
148996fd7ce5SGreg Kroah-Hartman {
149096fd7ce5SGreg Kroah-Hartman 	struct tty_struct *tty =
149196fd7ce5SGreg Kroah-Hartman 		container_of(work, struct tty_struct, hangup_work);
149296fd7ce5SGreg Kroah-Hartman 	struct tty_driver *driver = tty->driver;
149396fd7ce5SGreg Kroah-Hartman 
149496fd7ce5SGreg Kroah-Hartman 	if (tty->ops->cleanup)
149596fd7ce5SGreg Kroah-Hartman 		tty->ops->cleanup(tty);
149696fd7ce5SGreg Kroah-Hartman 
149796fd7ce5SGreg Kroah-Hartman 	tty->magic = 0;
149896fd7ce5SGreg Kroah-Hartman 	tty_driver_kref_put(driver);
149996fd7ce5SGreg Kroah-Hartman 	module_put(driver->owner);
150096fd7ce5SGreg Kroah-Hartman 
150196fd7ce5SGreg Kroah-Hartman 	spin_lock(&tty_files_lock);
150296fd7ce5SGreg Kroah-Hartman 	list_del_init(&tty->tty_files);
150396fd7ce5SGreg Kroah-Hartman 	spin_unlock(&tty_files_lock);
150496fd7ce5SGreg Kroah-Hartman 
150596fd7ce5SGreg Kroah-Hartman 	put_pid(tty->pgrp);
150696fd7ce5SGreg Kroah-Hartman 	put_pid(tty->session);
150796fd7ce5SGreg Kroah-Hartman 	free_tty_struct(tty);
150896fd7ce5SGreg Kroah-Hartman }
150996fd7ce5SGreg Kroah-Hartman 
151096fd7ce5SGreg Kroah-Hartman static void queue_release_one_tty(struct kref *kref)
151196fd7ce5SGreg Kroah-Hartman {
151296fd7ce5SGreg Kroah-Hartman 	struct tty_struct *tty = container_of(kref, struct tty_struct, kref);
151396fd7ce5SGreg Kroah-Hartman 
151496fd7ce5SGreg Kroah-Hartman 	/* The hangup queue is now free so we can reuse it rather than
151596fd7ce5SGreg Kroah-Hartman 	   waste a chunk of memory for each port */
151696fd7ce5SGreg Kroah-Hartman 	INIT_WORK(&tty->hangup_work, release_one_tty);
151796fd7ce5SGreg Kroah-Hartman 	schedule_work(&tty->hangup_work);
151896fd7ce5SGreg Kroah-Hartman }
151996fd7ce5SGreg Kroah-Hartman 
152096fd7ce5SGreg Kroah-Hartman /**
152196fd7ce5SGreg Kroah-Hartman  *	tty_kref_put		-	release a tty kref
152296fd7ce5SGreg Kroah-Hartman  *	@tty: tty device
152396fd7ce5SGreg Kroah-Hartman  *
152496fd7ce5SGreg Kroah-Hartman  *	Release a reference to a tty device and if need be let the kref
152596fd7ce5SGreg Kroah-Hartman  *	layer destruct the object for us
152696fd7ce5SGreg Kroah-Hartman  */
152796fd7ce5SGreg Kroah-Hartman 
152896fd7ce5SGreg Kroah-Hartman void tty_kref_put(struct tty_struct *tty)
152996fd7ce5SGreg Kroah-Hartman {
153096fd7ce5SGreg Kroah-Hartman 	if (tty)
153196fd7ce5SGreg Kroah-Hartman 		kref_put(&tty->kref, queue_release_one_tty);
153296fd7ce5SGreg Kroah-Hartman }
153396fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_kref_put);
153496fd7ce5SGreg Kroah-Hartman 
153596fd7ce5SGreg Kroah-Hartman /**
153696fd7ce5SGreg Kroah-Hartman  *	release_tty		-	release tty structure memory
153796fd7ce5SGreg Kroah-Hartman  *
153896fd7ce5SGreg Kroah-Hartman  *	Release both @tty and a possible linked partner (think pty pair),
153996fd7ce5SGreg Kroah-Hartman  *	and decrement the refcount of the backing module.
154096fd7ce5SGreg Kroah-Hartman  *
154196fd7ce5SGreg Kroah-Hartman  *	Locking:
1542d155255aSAlan Cox  *		tty_mutex
154396fd7ce5SGreg Kroah-Hartman  *		takes the file list lock internally when working on the list
154496fd7ce5SGreg Kroah-Hartman  *	of ttys that the driver keeps.
154596fd7ce5SGreg Kroah-Hartman  *
154696fd7ce5SGreg Kroah-Hartman  */
154796fd7ce5SGreg Kroah-Hartman static void release_tty(struct tty_struct *tty, int idx)
154896fd7ce5SGreg Kroah-Hartman {
154996fd7ce5SGreg Kroah-Hartman 	/* This should always be true but check for the moment */
155096fd7ce5SGreg Kroah-Hartman 	WARN_ON(tty->index != idx);
1551d155255aSAlan Cox 	WARN_ON(!mutex_is_locked(&tty_mutex));
155236b3c070SAlan Cox 	if (tty->ops->shutdown)
155336b3c070SAlan Cox 		tty->ops->shutdown(tty);
155436b3c070SAlan Cox 	tty_free_termios(tty);
155536b3c070SAlan Cox 	tty_driver_remove_tty(tty->driver, tty);
1556967fab69SJiri Slaby 	tty->port->itty = NULL;
155736b3c070SAlan Cox 
155896fd7ce5SGreg Kroah-Hartman 	if (tty->link)
155996fd7ce5SGreg Kroah-Hartman 		tty_kref_put(tty->link);
156096fd7ce5SGreg Kroah-Hartman 	tty_kref_put(tty);
156196fd7ce5SGreg Kroah-Hartman }
156296fd7ce5SGreg Kroah-Hartman 
156396fd7ce5SGreg Kroah-Hartman /**
1564955787caSJiri Slaby  *	tty_release_checks - check a tty before real release
1565955787caSJiri Slaby  *	@tty: tty to check
1566955787caSJiri Slaby  *	@o_tty: link of @tty (if any)
1567955787caSJiri Slaby  *	@idx: index of the tty
1568955787caSJiri Slaby  *
1569955787caSJiri Slaby  *	Performs some paranoid checking before true release of the @tty.
1570955787caSJiri Slaby  *	This is a no-op unless TTY_PARANOIA_CHECK is defined.
1571955787caSJiri Slaby  */
1572955787caSJiri Slaby static int tty_release_checks(struct tty_struct *tty, struct tty_struct *o_tty,
1573955787caSJiri Slaby 		int idx)
1574955787caSJiri Slaby {
1575955787caSJiri Slaby #ifdef TTY_PARANOIA_CHECK
1576955787caSJiri Slaby 	if (idx < 0 || idx >= tty->driver->num) {
15779de44bd6SJiri Slaby 		printk(KERN_DEBUG "%s: bad idx when trying to free (%s)\n",
15789de44bd6SJiri Slaby 				__func__, tty->name);
1579955787caSJiri Slaby 		return -1;
1580955787caSJiri Slaby 	}
1581955787caSJiri Slaby 
1582955787caSJiri Slaby 	/* not much to check for devpts */
1583955787caSJiri Slaby 	if (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)
1584955787caSJiri Slaby 		return 0;
1585955787caSJiri Slaby 
1586955787caSJiri Slaby 	if (tty != tty->driver->ttys[idx]) {
15879de44bd6SJiri Slaby 		printk(KERN_DEBUG "%s: driver.table[%d] not tty for (%s)\n",
15889de44bd6SJiri Slaby 				__func__, idx, tty->name);
1589955787caSJiri Slaby 		return -1;
1590955787caSJiri Slaby 	}
1591955787caSJiri Slaby 	if (tty->driver->other) {
1592955787caSJiri Slaby 		if (o_tty != tty->driver->other->ttys[idx]) {
15939de44bd6SJiri Slaby 			printk(KERN_DEBUG "%s: other->table[%d] not o_tty for (%s)\n",
15949de44bd6SJiri Slaby 					__func__, idx, tty->name);
1595955787caSJiri Slaby 			return -1;
1596955787caSJiri Slaby 		}
1597955787caSJiri Slaby 		if (o_tty->link != tty) {
15989de44bd6SJiri Slaby 			printk(KERN_DEBUG "%s: bad pty pointers\n", __func__);
1599955787caSJiri Slaby 			return -1;
1600955787caSJiri Slaby 		}
1601955787caSJiri Slaby 	}
1602955787caSJiri Slaby #endif
1603955787caSJiri Slaby 	return 0;
1604955787caSJiri Slaby }
1605955787caSJiri Slaby 
1606955787caSJiri Slaby /**
160796fd7ce5SGreg Kroah-Hartman  *	tty_release		-	vfs callback for close
160896fd7ce5SGreg Kroah-Hartman  *	@inode: inode of tty
160996fd7ce5SGreg Kroah-Hartman  *	@filp: file pointer for handle to tty
161096fd7ce5SGreg Kroah-Hartman  *
161196fd7ce5SGreg Kroah-Hartman  *	Called the last time each file handle is closed that references
161296fd7ce5SGreg Kroah-Hartman  *	this tty. There may however be several such references.
161396fd7ce5SGreg Kroah-Hartman  *
161496fd7ce5SGreg Kroah-Hartman  *	Locking:
161596fd7ce5SGreg Kroah-Hartman  *		Takes bkl. See tty_release_dev
161696fd7ce5SGreg Kroah-Hartman  *
161796fd7ce5SGreg Kroah-Hartman  * Even releasing the tty structures is a tricky business.. We have
161896fd7ce5SGreg Kroah-Hartman  * to be very careful that the structures are all released at the
161996fd7ce5SGreg Kroah-Hartman  * same time, as interrupts might otherwise get the wrong pointers.
162096fd7ce5SGreg Kroah-Hartman  *
162196fd7ce5SGreg Kroah-Hartman  * WSH 09/09/97: rewritten to avoid some nasty race conditions that could
162296fd7ce5SGreg Kroah-Hartman  * lead to double frees or releasing memory still in use.
162396fd7ce5SGreg Kroah-Hartman  */
162496fd7ce5SGreg Kroah-Hartman 
162596fd7ce5SGreg Kroah-Hartman int tty_release(struct inode *inode, struct file *filp)
162696fd7ce5SGreg Kroah-Hartman {
162796fd7ce5SGreg Kroah-Hartman 	struct tty_struct *tty = file_tty(filp);
162896fd7ce5SGreg Kroah-Hartman 	struct tty_struct *o_tty;
162996fd7ce5SGreg Kroah-Hartman 	int	pty_master, tty_closing, o_tty_closing, do_sleep;
163096fd7ce5SGreg Kroah-Hartman 	int	idx;
163196fd7ce5SGreg Kroah-Hartman 	char	buf[64];
163296fd7ce5SGreg Kroah-Hartman 
16339de44bd6SJiri Slaby 	if (tty_paranoia_check(tty, inode, __func__))
163496fd7ce5SGreg Kroah-Hartman 		return 0;
163596fd7ce5SGreg Kroah-Hartman 
163689c8d91eSAlan Cox 	tty_lock(tty);
16379de44bd6SJiri Slaby 	check_tty_count(tty, __func__);
163896fd7ce5SGreg Kroah-Hartman 
163996fd7ce5SGreg Kroah-Hartman 	__tty_fasync(-1, filp, 0);
164096fd7ce5SGreg Kroah-Hartman 
164196fd7ce5SGreg Kroah-Hartman 	idx = tty->index;
164296fd7ce5SGreg Kroah-Hartman 	pty_master = (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
164396fd7ce5SGreg Kroah-Hartman 		      tty->driver->subtype == PTY_TYPE_MASTER);
164489c8d91eSAlan Cox 	/* Review: parallel close */
164596fd7ce5SGreg Kroah-Hartman 	o_tty = tty->link;
164696fd7ce5SGreg Kroah-Hartman 
1647955787caSJiri Slaby 	if (tty_release_checks(tty, o_tty, idx)) {
164889c8d91eSAlan Cox 		tty_unlock(tty);
164996fd7ce5SGreg Kroah-Hartman 		return 0;
165096fd7ce5SGreg Kroah-Hartman 	}
165196fd7ce5SGreg Kroah-Hartman 
165296fd7ce5SGreg Kroah-Hartman #ifdef TTY_DEBUG_HANGUP
16539de44bd6SJiri Slaby 	printk(KERN_DEBUG "%s: %s (tty count=%d)...\n", __func__,
165496fd7ce5SGreg Kroah-Hartman 			tty_name(tty, buf), tty->count);
165596fd7ce5SGreg Kroah-Hartman #endif
165696fd7ce5SGreg Kroah-Hartman 
165796fd7ce5SGreg Kroah-Hartman 	if (tty->ops->close)
165896fd7ce5SGreg Kroah-Hartman 		tty->ops->close(tty, filp);
165996fd7ce5SGreg Kroah-Hartman 
166089c8d91eSAlan Cox 	tty_unlock(tty);
166196fd7ce5SGreg Kroah-Hartman 	/*
166296fd7ce5SGreg Kroah-Hartman 	 * Sanity check: if tty->count is going to zero, there shouldn't be
166396fd7ce5SGreg Kroah-Hartman 	 * any waiters on tty->read_wait or tty->write_wait.  We test the
166496fd7ce5SGreg Kroah-Hartman 	 * wait queues and kick everyone out _before_ actually starting to
166596fd7ce5SGreg Kroah-Hartman 	 * close.  This ensures that we won't block while releasing the tty
166696fd7ce5SGreg Kroah-Hartman 	 * structure.
166796fd7ce5SGreg Kroah-Hartman 	 *
166896fd7ce5SGreg Kroah-Hartman 	 * The test for the o_tty closing is necessary, since the master and
166996fd7ce5SGreg Kroah-Hartman 	 * slave sides may close in any order.  If the slave side closes out
167096fd7ce5SGreg Kroah-Hartman 	 * first, its count will be one, since the master side holds an open.
167196fd7ce5SGreg Kroah-Hartman 	 * Thus this test wouldn't be triggered at the time the slave closes,
167296fd7ce5SGreg Kroah-Hartman 	 * so we do it now.
167396fd7ce5SGreg Kroah-Hartman 	 *
167496fd7ce5SGreg Kroah-Hartman 	 * Note that it's possible for the tty to be opened again while we're
167596fd7ce5SGreg Kroah-Hartman 	 * flushing out waiters.  By recalculating the closing flags before
167696fd7ce5SGreg Kroah-Hartman 	 * each iteration we avoid any problems.
167796fd7ce5SGreg Kroah-Hartman 	 */
167896fd7ce5SGreg Kroah-Hartman 	while (1) {
167996fd7ce5SGreg Kroah-Hartman 		/* Guard against races with tty->count changes elsewhere and
168096fd7ce5SGreg Kroah-Hartman 		   opens on /dev/tty */
168196fd7ce5SGreg Kroah-Hartman 
168296fd7ce5SGreg Kroah-Hartman 		mutex_lock(&tty_mutex);
168389c8d91eSAlan Cox 		tty_lock_pair(tty, o_tty);
168496fd7ce5SGreg Kroah-Hartman 		tty_closing = tty->count <= 1;
168596fd7ce5SGreg Kroah-Hartman 		o_tty_closing = o_tty &&
168696fd7ce5SGreg Kroah-Hartman 			(o_tty->count <= (pty_master ? 1 : 0));
168796fd7ce5SGreg Kroah-Hartman 		do_sleep = 0;
168896fd7ce5SGreg Kroah-Hartman 
168996fd7ce5SGreg Kroah-Hartman 		if (tty_closing) {
169096fd7ce5SGreg Kroah-Hartman 			if (waitqueue_active(&tty->read_wait)) {
169196fd7ce5SGreg Kroah-Hartman 				wake_up_poll(&tty->read_wait, POLLIN);
169296fd7ce5SGreg Kroah-Hartman 				do_sleep++;
169396fd7ce5SGreg Kroah-Hartman 			}
169496fd7ce5SGreg Kroah-Hartman 			if (waitqueue_active(&tty->write_wait)) {
169596fd7ce5SGreg Kroah-Hartman 				wake_up_poll(&tty->write_wait, POLLOUT);
169696fd7ce5SGreg Kroah-Hartman 				do_sleep++;
169796fd7ce5SGreg Kroah-Hartman 			}
169896fd7ce5SGreg Kroah-Hartman 		}
169996fd7ce5SGreg Kroah-Hartman 		if (o_tty_closing) {
170096fd7ce5SGreg Kroah-Hartman 			if (waitqueue_active(&o_tty->read_wait)) {
170196fd7ce5SGreg Kroah-Hartman 				wake_up_poll(&o_tty->read_wait, POLLIN);
170296fd7ce5SGreg Kroah-Hartman 				do_sleep++;
170396fd7ce5SGreg Kroah-Hartman 			}
170496fd7ce5SGreg Kroah-Hartman 			if (waitqueue_active(&o_tty->write_wait)) {
170596fd7ce5SGreg Kroah-Hartman 				wake_up_poll(&o_tty->write_wait, POLLOUT);
170696fd7ce5SGreg Kroah-Hartman 				do_sleep++;
170796fd7ce5SGreg Kroah-Hartman 			}
170896fd7ce5SGreg Kroah-Hartman 		}
170996fd7ce5SGreg Kroah-Hartman 		if (!do_sleep)
171096fd7ce5SGreg Kroah-Hartman 			break;
171196fd7ce5SGreg Kroah-Hartman 
17129de44bd6SJiri Slaby 		printk(KERN_WARNING "%s: %s: read/write wait queue active!\n",
17139de44bd6SJiri Slaby 				__func__, tty_name(tty, buf));
171489c8d91eSAlan Cox 		tty_unlock_pair(tty, o_tty);
171596fd7ce5SGreg Kroah-Hartman 		mutex_unlock(&tty_mutex);
171696fd7ce5SGreg Kroah-Hartman 		schedule();
171796fd7ce5SGreg Kroah-Hartman 	}
171896fd7ce5SGreg Kroah-Hartman 
171996fd7ce5SGreg Kroah-Hartman 	/*
172096fd7ce5SGreg Kroah-Hartman 	 * The closing flags are now consistent with the open counts on
172196fd7ce5SGreg Kroah-Hartman 	 * both sides, and we've completed the last operation that could
172296fd7ce5SGreg Kroah-Hartman 	 * block, so it's safe to proceed with closing.
1723d155255aSAlan Cox 	 *
1724d155255aSAlan Cox 	 * We must *not* drop the tty_mutex until we ensure that a further
1725d155255aSAlan Cox 	 * entry into tty_open can not pick up this tty.
172696fd7ce5SGreg Kroah-Hartman 	 */
172796fd7ce5SGreg Kroah-Hartman 	if (pty_master) {
172896fd7ce5SGreg Kroah-Hartman 		if (--o_tty->count < 0) {
17299de44bd6SJiri Slaby 			printk(KERN_WARNING "%s: bad pty slave count (%d) for %s\n",
17309de44bd6SJiri Slaby 				__func__, o_tty->count, tty_name(o_tty, buf));
173196fd7ce5SGreg Kroah-Hartman 			o_tty->count = 0;
173296fd7ce5SGreg Kroah-Hartman 		}
173396fd7ce5SGreg Kroah-Hartman 	}
173496fd7ce5SGreg Kroah-Hartman 	if (--tty->count < 0) {
17359de44bd6SJiri Slaby 		printk(KERN_WARNING "%s: bad tty->count (%d) for %s\n",
17369de44bd6SJiri Slaby 				__func__, tty->count, tty_name(tty, buf));
173796fd7ce5SGreg Kroah-Hartman 		tty->count = 0;
173896fd7ce5SGreg Kroah-Hartman 	}
173996fd7ce5SGreg Kroah-Hartman 
174096fd7ce5SGreg Kroah-Hartman 	/*
174196fd7ce5SGreg Kroah-Hartman 	 * We've decremented tty->count, so we need to remove this file
174296fd7ce5SGreg Kroah-Hartman 	 * descriptor off the tty->tty_files list; this serves two
174396fd7ce5SGreg Kroah-Hartman 	 * purposes:
174496fd7ce5SGreg Kroah-Hartman 	 *  - check_tty_count sees the correct number of file descriptors
174596fd7ce5SGreg Kroah-Hartman 	 *    associated with this tty.
174696fd7ce5SGreg Kroah-Hartman 	 *  - do_tty_hangup no longer sees this file descriptor as
174796fd7ce5SGreg Kroah-Hartman 	 *    something that needs to be handled for hangups.
174896fd7ce5SGreg Kroah-Hartman 	 */
174996fd7ce5SGreg Kroah-Hartman 	tty_del_file(filp);
175096fd7ce5SGreg Kroah-Hartman 
175196fd7ce5SGreg Kroah-Hartman 	/*
175296fd7ce5SGreg Kroah-Hartman 	 * Perform some housekeeping before deciding whether to return.
175396fd7ce5SGreg Kroah-Hartman 	 *
175496fd7ce5SGreg Kroah-Hartman 	 * Set the TTY_CLOSING flag if this was the last open.  In the
175596fd7ce5SGreg Kroah-Hartman 	 * case of a pty we may have to wait around for the other side
175696fd7ce5SGreg Kroah-Hartman 	 * to close, and TTY_CLOSING makes sure we can't be reopened.
175796fd7ce5SGreg Kroah-Hartman 	 */
175896fd7ce5SGreg Kroah-Hartman 	if (tty_closing)
175996fd7ce5SGreg Kroah-Hartman 		set_bit(TTY_CLOSING, &tty->flags);
176096fd7ce5SGreg Kroah-Hartman 	if (o_tty_closing)
176196fd7ce5SGreg Kroah-Hartman 		set_bit(TTY_CLOSING, &o_tty->flags);
176296fd7ce5SGreg Kroah-Hartman 
176396fd7ce5SGreg Kroah-Hartman 	/*
176496fd7ce5SGreg Kroah-Hartman 	 * If _either_ side is closing, make sure there aren't any
176596fd7ce5SGreg Kroah-Hartman 	 * processes that still think tty or o_tty is their controlling
176696fd7ce5SGreg Kroah-Hartman 	 * tty.
176796fd7ce5SGreg Kroah-Hartman 	 */
176896fd7ce5SGreg Kroah-Hartman 	if (tty_closing || o_tty_closing) {
176996fd7ce5SGreg Kroah-Hartman 		read_lock(&tasklist_lock);
177096fd7ce5SGreg Kroah-Hartman 		session_clear_tty(tty->session);
177196fd7ce5SGreg Kroah-Hartman 		if (o_tty)
177296fd7ce5SGreg Kroah-Hartman 			session_clear_tty(o_tty->session);
177396fd7ce5SGreg Kroah-Hartman 		read_unlock(&tasklist_lock);
177496fd7ce5SGreg Kroah-Hartman 	}
177596fd7ce5SGreg Kroah-Hartman 
177696fd7ce5SGreg Kroah-Hartman 	mutex_unlock(&tty_mutex);
177789c8d91eSAlan Cox 	tty_unlock_pair(tty, o_tty);
1778d155255aSAlan Cox 	/* At this point the TTY_CLOSING flag should ensure a dead tty
1779d155255aSAlan Cox 	   cannot be re-opened by a racing opener */
178096fd7ce5SGreg Kroah-Hartman 
178196fd7ce5SGreg Kroah-Hartman 	/* check whether both sides are closing ... */
1782d155255aSAlan Cox 	if (!tty_closing || (o_tty && !o_tty_closing))
178396fd7ce5SGreg Kroah-Hartman 		return 0;
178496fd7ce5SGreg Kroah-Hartman 
178596fd7ce5SGreg Kroah-Hartman #ifdef TTY_DEBUG_HANGUP
17869de44bd6SJiri Slaby 	printk(KERN_DEBUG "%s: freeing tty structure...\n", __func__);
178796fd7ce5SGreg Kroah-Hartman #endif
178896fd7ce5SGreg Kroah-Hartman 	/*
178996fd7ce5SGreg Kroah-Hartman 	 * Ask the line discipline code to release its structures
179096fd7ce5SGreg Kroah-Hartman 	 */
179196fd7ce5SGreg Kroah-Hartman 	tty_ldisc_release(tty, o_tty);
179296fd7ce5SGreg Kroah-Hartman 	/*
179396fd7ce5SGreg Kroah-Hartman 	 * The release_tty function takes care of the details of clearing
179489c8d91eSAlan Cox 	 * the slots and preserving the termios structure. The tty_unlock_pair
179589c8d91eSAlan Cox 	 * should be safe as we keep a kref while the tty is locked (so the
179689c8d91eSAlan Cox 	 * unlock never unlocks a freed tty).
179796fd7ce5SGreg Kroah-Hartman 	 */
1798d155255aSAlan Cox 	mutex_lock(&tty_mutex);
179996fd7ce5SGreg Kroah-Hartman 	release_tty(tty, idx);
1800d155255aSAlan Cox 	mutex_unlock(&tty_mutex);
180196fd7ce5SGreg Kroah-Hartman 
180296fd7ce5SGreg Kroah-Hartman 	return 0;
180396fd7ce5SGreg Kroah-Hartman }
180496fd7ce5SGreg Kroah-Hartman 
180596fd7ce5SGreg Kroah-Hartman /**
1806b82154acSJiri Slaby  *	tty_open_current_tty - get tty of current task for open
1807b82154acSJiri Slaby  *	@device: device number
1808b82154acSJiri Slaby  *	@filp: file pointer to tty
1809b82154acSJiri Slaby  *	@return: tty of the current task iff @device is /dev/tty
1810b82154acSJiri Slaby  *
1811b82154acSJiri Slaby  *	We cannot return driver and index like for the other nodes because
1812b82154acSJiri Slaby  *	devpts will not work then. It expects inodes to be from devpts FS.
18133af502b9SAlan Cox  *
18143af502b9SAlan Cox  *	We need to move to returning a refcounted object from all the lookup
18153af502b9SAlan Cox  *	paths including this one.
1816b82154acSJiri Slaby  */
1817b82154acSJiri Slaby static struct tty_struct *tty_open_current_tty(dev_t device, struct file *filp)
1818b82154acSJiri Slaby {
1819b82154acSJiri Slaby 	struct tty_struct *tty;
1820b82154acSJiri Slaby 
1821b82154acSJiri Slaby 	if (device != MKDEV(TTYAUX_MAJOR, 0))
1822b82154acSJiri Slaby 		return NULL;
1823b82154acSJiri Slaby 
1824b82154acSJiri Slaby 	tty = get_current_tty();
1825b82154acSJiri Slaby 	if (!tty)
1826b82154acSJiri Slaby 		return ERR_PTR(-ENXIO);
1827b82154acSJiri Slaby 
1828b82154acSJiri Slaby 	filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */
1829b82154acSJiri Slaby 	/* noctty = 1; */
1830b82154acSJiri Slaby 	tty_kref_put(tty);
1831b82154acSJiri Slaby 	/* FIXME: we put a reference and return a TTY! */
18323af502b9SAlan Cox 	/* This is only safe because the caller holds tty_mutex */
1833b82154acSJiri Slaby 	return tty;
1834b82154acSJiri Slaby }
1835b82154acSJiri Slaby 
1836b82154acSJiri Slaby /**
18375b5e7040SJiri Slaby  *	tty_lookup_driver - lookup a tty driver for a given device file
18385b5e7040SJiri Slaby  *	@device: device number
18395b5e7040SJiri Slaby  *	@filp: file pointer to tty
18405b5e7040SJiri Slaby  *	@noctty: set if the device should not become a controlling tty
18415b5e7040SJiri Slaby  *	@index: index for the device in the @return driver
18425b5e7040SJiri Slaby  *	@return: driver for this inode (with increased refcount)
18435b5e7040SJiri Slaby  *
18445b5e7040SJiri Slaby  * 	If @return is not erroneous, the caller is responsible to decrement the
18455b5e7040SJiri Slaby  * 	refcount by tty_driver_kref_put.
18465b5e7040SJiri Slaby  *
18475b5e7040SJiri Slaby  *	Locking: tty_mutex protects get_tty_driver
18485b5e7040SJiri Slaby  */
18495b5e7040SJiri Slaby static struct tty_driver *tty_lookup_driver(dev_t device, struct file *filp,
18505b5e7040SJiri Slaby 		int *noctty, int *index)
18515b5e7040SJiri Slaby {
18525b5e7040SJiri Slaby 	struct tty_driver *driver;
18535b5e7040SJiri Slaby 
18542cd0050cSJiri Slaby 	switch (device) {
18555b5e7040SJiri Slaby #ifdef CONFIG_VT
18562cd0050cSJiri Slaby 	case MKDEV(TTY_MAJOR, 0): {
18575b5e7040SJiri Slaby 		extern struct tty_driver *console_driver;
18585b5e7040SJiri Slaby 		driver = tty_driver_kref_get(console_driver);
18595b5e7040SJiri Slaby 		*index = fg_console;
18605b5e7040SJiri Slaby 		*noctty = 1;
18612cd0050cSJiri Slaby 		break;
18625b5e7040SJiri Slaby 	}
18635b5e7040SJiri Slaby #endif
18642cd0050cSJiri Slaby 	case MKDEV(TTYAUX_MAJOR, 1): {
18655b5e7040SJiri Slaby 		struct tty_driver *console_driver = console_device(index);
18665b5e7040SJiri Slaby 		if (console_driver) {
18675b5e7040SJiri Slaby 			driver = tty_driver_kref_get(console_driver);
18685b5e7040SJiri Slaby 			if (driver) {
18695b5e7040SJiri Slaby 				/* Don't let /dev/console block */
18705b5e7040SJiri Slaby 				filp->f_flags |= O_NONBLOCK;
18715b5e7040SJiri Slaby 				*noctty = 1;
18722cd0050cSJiri Slaby 				break;
18735b5e7040SJiri Slaby 			}
18745b5e7040SJiri Slaby 		}
18755b5e7040SJiri Slaby 		return ERR_PTR(-ENODEV);
18765b5e7040SJiri Slaby 	}
18772cd0050cSJiri Slaby 	default:
18785b5e7040SJiri Slaby 		driver = get_tty_driver(device, index);
18795b5e7040SJiri Slaby 		if (!driver)
18805b5e7040SJiri Slaby 			return ERR_PTR(-ENODEV);
18812cd0050cSJiri Slaby 		break;
18822cd0050cSJiri Slaby 	}
18835b5e7040SJiri Slaby 	return driver;
18845b5e7040SJiri Slaby }
18855b5e7040SJiri Slaby 
18865b5e7040SJiri Slaby /**
188796fd7ce5SGreg Kroah-Hartman  *	tty_open		-	open a tty device
188896fd7ce5SGreg Kroah-Hartman  *	@inode: inode of device file
188996fd7ce5SGreg Kroah-Hartman  *	@filp: file pointer to tty
189096fd7ce5SGreg Kroah-Hartman  *
189196fd7ce5SGreg Kroah-Hartman  *	tty_open and tty_release keep up the tty count that contains the
189296fd7ce5SGreg Kroah-Hartman  *	number of opens done on a tty. We cannot use the inode-count, as
189396fd7ce5SGreg Kroah-Hartman  *	different inodes might point to the same tty.
189496fd7ce5SGreg Kroah-Hartman  *
189596fd7ce5SGreg Kroah-Hartman  *	Open-counting is needed for pty masters, as well as for keeping
189696fd7ce5SGreg Kroah-Hartman  *	track of serial lines: DTR is dropped when the last close happens.
189796fd7ce5SGreg Kroah-Hartman  *	(This is not done solely through tty->count, now.  - Ted 1/27/92)
189896fd7ce5SGreg Kroah-Hartman  *
189996fd7ce5SGreg Kroah-Hartman  *	The termios state of a pty is reset on first open so that
190096fd7ce5SGreg Kroah-Hartman  *	settings don't persist across reuse.
190196fd7ce5SGreg Kroah-Hartman  *
19025b5e7040SJiri Slaby  *	Locking: tty_mutex protects tty, tty_lookup_driver and tty_init_dev.
190396fd7ce5SGreg Kroah-Hartman  *		 tty->count should protect the rest.
190496fd7ce5SGreg Kroah-Hartman  *		 ->siglock protects ->signal/->sighand
190589c8d91eSAlan Cox  *
190689c8d91eSAlan Cox  *	Note: the tty_unlock/lock cases without a ref are only safe due to
190789c8d91eSAlan Cox  *	tty_mutex
190896fd7ce5SGreg Kroah-Hartman  */
190996fd7ce5SGreg Kroah-Hartman 
191096fd7ce5SGreg Kroah-Hartman static int tty_open(struct inode *inode, struct file *filp)
191196fd7ce5SGreg Kroah-Hartman {
1912b82154acSJiri Slaby 	struct tty_struct *tty;
191396fd7ce5SGreg Kroah-Hartman 	int noctty, retval;
1914b82154acSJiri Slaby 	struct tty_driver *driver = NULL;
191596fd7ce5SGreg Kroah-Hartman 	int index;
191696fd7ce5SGreg Kroah-Hartman 	dev_t device = inode->i_rdev;
191796fd7ce5SGreg Kroah-Hartman 	unsigned saved_flags = filp->f_flags;
191896fd7ce5SGreg Kroah-Hartman 
191996fd7ce5SGreg Kroah-Hartman 	nonseekable_open(inode, filp);
192096fd7ce5SGreg Kroah-Hartman 
192196fd7ce5SGreg Kroah-Hartman retry_open:
1922fa90e1c9SJiri Slaby 	retval = tty_alloc_file(filp);
1923fa90e1c9SJiri Slaby 	if (retval)
1924fa90e1c9SJiri Slaby 		return -ENOMEM;
1925fa90e1c9SJiri Slaby 
192696fd7ce5SGreg Kroah-Hartman 	noctty = filp->f_flags & O_NOCTTY;
192796fd7ce5SGreg Kroah-Hartman 	index  = -1;
192896fd7ce5SGreg Kroah-Hartman 	retval = 0;
192996fd7ce5SGreg Kroah-Hartman 
193096fd7ce5SGreg Kroah-Hartman 	mutex_lock(&tty_mutex);
193189c8d91eSAlan Cox 	/* This is protected by the tty_mutex */
1932b82154acSJiri Slaby 	tty = tty_open_current_tty(device, filp);
1933b82154acSJiri Slaby 	if (IS_ERR(tty)) {
1934ba5db448SJiri Slaby 		retval = PTR_ERR(tty);
1935ba5db448SJiri Slaby 		goto err_unlock;
19365b5e7040SJiri Slaby 	} else if (!tty) {
19375b5e7040SJiri Slaby 		driver = tty_lookup_driver(device, filp, &noctty, &index);
19385b5e7040SJiri Slaby 		if (IS_ERR(driver)) {
1939ba5db448SJiri Slaby 			retval = PTR_ERR(driver);
1940ba5db448SJiri Slaby 			goto err_unlock;
194196fd7ce5SGreg Kroah-Hartman 		}
194296fd7ce5SGreg Kroah-Hartman 
194396fd7ce5SGreg Kroah-Hartman 		/* check whether we're reopening an existing tty */
194496fd7ce5SGreg Kroah-Hartman 		tty = tty_driver_lookup_tty(driver, inode, index);
194596fd7ce5SGreg Kroah-Hartman 		if (IS_ERR(tty)) {
1946ba5db448SJiri Slaby 			retval = PTR_ERR(tty);
1947ba5db448SJiri Slaby 			goto err_unlock;
194896fd7ce5SGreg Kroah-Hartman 		}
194996fd7ce5SGreg Kroah-Hartman 	}
195096fd7ce5SGreg Kroah-Hartman 
195196fd7ce5SGreg Kroah-Hartman 	if (tty) {
195289c8d91eSAlan Cox 		tty_lock(tty);
195396fd7ce5SGreg Kroah-Hartman 		retval = tty_reopen(tty);
195489c8d91eSAlan Cox 		if (retval < 0) {
195589c8d91eSAlan Cox 			tty_unlock(tty);
195696fd7ce5SGreg Kroah-Hartman 			tty = ERR_PTR(retval);
195789c8d91eSAlan Cox 		}
195889c8d91eSAlan Cox 	} else	/* Returns with the tty_lock held for now */
1959593a27c4SKonstantin Khlebnikov 		tty = tty_init_dev(driver, index);
196096fd7ce5SGreg Kroah-Hartman 
196196fd7ce5SGreg Kroah-Hartman 	mutex_unlock(&tty_mutex);
1962b82154acSJiri Slaby 	if (driver)
196396fd7ce5SGreg Kroah-Hartman 		tty_driver_kref_put(driver);
196496fd7ce5SGreg Kroah-Hartman 	if (IS_ERR(tty)) {
1965ba5db448SJiri Slaby 		retval = PTR_ERR(tty);
1966ba5db448SJiri Slaby 		goto err_file;
196796fd7ce5SGreg Kroah-Hartman 	}
196896fd7ce5SGreg Kroah-Hartman 
1969fa90e1c9SJiri Slaby 	tty_add_file(tty, filp);
197096fd7ce5SGreg Kroah-Hartman 
19719de44bd6SJiri Slaby 	check_tty_count(tty, __func__);
197296fd7ce5SGreg Kroah-Hartman 	if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
197396fd7ce5SGreg Kroah-Hartman 	    tty->driver->subtype == PTY_TYPE_MASTER)
197496fd7ce5SGreg Kroah-Hartman 		noctty = 1;
197596fd7ce5SGreg Kroah-Hartman #ifdef TTY_DEBUG_HANGUP
19769de44bd6SJiri Slaby 	printk(KERN_DEBUG "%s: opening %s...\n", __func__, tty->name);
197796fd7ce5SGreg Kroah-Hartman #endif
197896fd7ce5SGreg Kroah-Hartman 	if (tty->ops->open)
197996fd7ce5SGreg Kroah-Hartman 		retval = tty->ops->open(tty, filp);
198096fd7ce5SGreg Kroah-Hartman 	else
198196fd7ce5SGreg Kroah-Hartman 		retval = -ENODEV;
198296fd7ce5SGreg Kroah-Hartman 	filp->f_flags = saved_flags;
198396fd7ce5SGreg Kroah-Hartman 
198496fd7ce5SGreg Kroah-Hartman 	if (!retval && test_bit(TTY_EXCLUSIVE, &tty->flags) &&
198596fd7ce5SGreg Kroah-Hartman 						!capable(CAP_SYS_ADMIN))
198696fd7ce5SGreg Kroah-Hartman 		retval = -EBUSY;
198796fd7ce5SGreg Kroah-Hartman 
198896fd7ce5SGreg Kroah-Hartman 	if (retval) {
198996fd7ce5SGreg Kroah-Hartman #ifdef TTY_DEBUG_HANGUP
19909de44bd6SJiri Slaby 		printk(KERN_DEBUG "%s: error %d in opening %s...\n", __func__,
19919de44bd6SJiri Slaby 				retval, tty->name);
199296fd7ce5SGreg Kroah-Hartman #endif
199389c8d91eSAlan Cox 		tty_unlock(tty); /* need to call tty_release without BTM */
199496fd7ce5SGreg Kroah-Hartman 		tty_release(inode, filp);
199596fd7ce5SGreg Kroah-Hartman 		if (retval != -ERESTARTSYS)
199696fd7ce5SGreg Kroah-Hartman 			return retval;
199796fd7ce5SGreg Kroah-Hartman 
199896fd7ce5SGreg Kroah-Hartman 		if (signal_pending(current))
199996fd7ce5SGreg Kroah-Hartman 			return retval;
200096fd7ce5SGreg Kroah-Hartman 
200196fd7ce5SGreg Kroah-Hartman 		schedule();
200296fd7ce5SGreg Kroah-Hartman 		/*
200396fd7ce5SGreg Kroah-Hartman 		 * Need to reset f_op in case a hangup happened.
200496fd7ce5SGreg Kroah-Hartman 		 */
200596fd7ce5SGreg Kroah-Hartman 		if (filp->f_op == &hung_up_tty_fops)
200696fd7ce5SGreg Kroah-Hartman 			filp->f_op = &tty_fops;
200796fd7ce5SGreg Kroah-Hartman 		goto retry_open;
200896fd7ce5SGreg Kroah-Hartman 	}
200989c8d91eSAlan Cox 	tty_unlock(tty);
201096fd7ce5SGreg Kroah-Hartman 
201196fd7ce5SGreg Kroah-Hartman 
201296fd7ce5SGreg Kroah-Hartman 	mutex_lock(&tty_mutex);
201389c8d91eSAlan Cox 	tty_lock(tty);
201496fd7ce5SGreg Kroah-Hartman 	spin_lock_irq(&current->sighand->siglock);
201596fd7ce5SGreg Kroah-Hartman 	if (!noctty &&
201696fd7ce5SGreg Kroah-Hartman 	    current->signal->leader &&
201796fd7ce5SGreg Kroah-Hartman 	    !current->signal->tty &&
201896fd7ce5SGreg Kroah-Hartman 	    tty->session == NULL)
201996fd7ce5SGreg Kroah-Hartman 		__proc_set_tty(current, tty);
202096fd7ce5SGreg Kroah-Hartman 	spin_unlock_irq(&current->sighand->siglock);
202189c8d91eSAlan Cox 	tty_unlock(tty);
202296fd7ce5SGreg Kroah-Hartman 	mutex_unlock(&tty_mutex);
202396fd7ce5SGreg Kroah-Hartman 	return 0;
2024ba5db448SJiri Slaby err_unlock:
2025ba5db448SJiri Slaby 	mutex_unlock(&tty_mutex);
2026ba5db448SJiri Slaby 	/* after locks to avoid deadlock */
2027ba5db448SJiri Slaby 	if (!IS_ERR_OR_NULL(driver))
2028ba5db448SJiri Slaby 		tty_driver_kref_put(driver);
2029ba5db448SJiri Slaby err_file:
2030ba5db448SJiri Slaby 	tty_free_file(filp);
2031ba5db448SJiri Slaby 	return retval;
203296fd7ce5SGreg Kroah-Hartman }
203396fd7ce5SGreg Kroah-Hartman 
203496fd7ce5SGreg Kroah-Hartman 
203596fd7ce5SGreg Kroah-Hartman 
203696fd7ce5SGreg Kroah-Hartman /**
203796fd7ce5SGreg Kroah-Hartman  *	tty_poll	-	check tty status
203896fd7ce5SGreg Kroah-Hartman  *	@filp: file being polled
203996fd7ce5SGreg Kroah-Hartman  *	@wait: poll wait structures to update
204096fd7ce5SGreg Kroah-Hartman  *
204196fd7ce5SGreg Kroah-Hartman  *	Call the line discipline polling method to obtain the poll
204296fd7ce5SGreg Kroah-Hartman  *	status of the device.
204396fd7ce5SGreg Kroah-Hartman  *
204496fd7ce5SGreg Kroah-Hartman  *	Locking: locks called line discipline but ldisc poll method
204596fd7ce5SGreg Kroah-Hartman  *	may be re-entered freely by other callers.
204696fd7ce5SGreg Kroah-Hartman  */
204796fd7ce5SGreg Kroah-Hartman 
204896fd7ce5SGreg Kroah-Hartman static unsigned int tty_poll(struct file *filp, poll_table *wait)
204996fd7ce5SGreg Kroah-Hartman {
205096fd7ce5SGreg Kroah-Hartman 	struct tty_struct *tty = file_tty(filp);
205196fd7ce5SGreg Kroah-Hartman 	struct tty_ldisc *ld;
205296fd7ce5SGreg Kroah-Hartman 	int ret = 0;
205396fd7ce5SGreg Kroah-Hartman 
205496fd7ce5SGreg Kroah-Hartman 	if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode, "tty_poll"))
205596fd7ce5SGreg Kroah-Hartman 		return 0;
205696fd7ce5SGreg Kroah-Hartman 
205796fd7ce5SGreg Kroah-Hartman 	ld = tty_ldisc_ref_wait(tty);
205896fd7ce5SGreg Kroah-Hartman 	if (ld->ops->poll)
205996fd7ce5SGreg Kroah-Hartman 		ret = (ld->ops->poll)(tty, filp, wait);
206096fd7ce5SGreg Kroah-Hartman 	tty_ldisc_deref(ld);
206196fd7ce5SGreg Kroah-Hartman 	return ret;
206296fd7ce5SGreg Kroah-Hartman }
206396fd7ce5SGreg Kroah-Hartman 
206496fd7ce5SGreg Kroah-Hartman static int __tty_fasync(int fd, struct file *filp, int on)
206596fd7ce5SGreg Kroah-Hartman {
206696fd7ce5SGreg Kroah-Hartman 	struct tty_struct *tty = file_tty(filp);
206796fd7ce5SGreg Kroah-Hartman 	unsigned long flags;
206896fd7ce5SGreg Kroah-Hartman 	int retval = 0;
206996fd7ce5SGreg Kroah-Hartman 
207096fd7ce5SGreg Kroah-Hartman 	if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode, "tty_fasync"))
207196fd7ce5SGreg Kroah-Hartman 		goto out;
207296fd7ce5SGreg Kroah-Hartman 
207396fd7ce5SGreg Kroah-Hartman 	retval = fasync_helper(fd, filp, on, &tty->fasync);
207496fd7ce5SGreg Kroah-Hartman 	if (retval <= 0)
207596fd7ce5SGreg Kroah-Hartman 		goto out;
207696fd7ce5SGreg Kroah-Hartman 
207796fd7ce5SGreg Kroah-Hartman 	if (on) {
207896fd7ce5SGreg Kroah-Hartman 		enum pid_type type;
207996fd7ce5SGreg Kroah-Hartman 		struct pid *pid;
208096fd7ce5SGreg Kroah-Hartman 		if (!waitqueue_active(&tty->read_wait))
208196fd7ce5SGreg Kroah-Hartman 			tty->minimum_to_wake = 1;
208296fd7ce5SGreg Kroah-Hartman 		spin_lock_irqsave(&tty->ctrl_lock, flags);
208396fd7ce5SGreg Kroah-Hartman 		if (tty->pgrp) {
208496fd7ce5SGreg Kroah-Hartman 			pid = tty->pgrp;
208596fd7ce5SGreg Kroah-Hartman 			type = PIDTYPE_PGID;
208696fd7ce5SGreg Kroah-Hartman 		} else {
208796fd7ce5SGreg Kroah-Hartman 			pid = task_pid(current);
208896fd7ce5SGreg Kroah-Hartman 			type = PIDTYPE_PID;
208996fd7ce5SGreg Kroah-Hartman 		}
209096fd7ce5SGreg Kroah-Hartman 		get_pid(pid);
209196fd7ce5SGreg Kroah-Hartman 		spin_unlock_irqrestore(&tty->ctrl_lock, flags);
209296fd7ce5SGreg Kroah-Hartman 		retval = __f_setown(filp, pid, type, 0);
209396fd7ce5SGreg Kroah-Hartman 		put_pid(pid);
209496fd7ce5SGreg Kroah-Hartman 		if (retval)
209596fd7ce5SGreg Kroah-Hartman 			goto out;
209696fd7ce5SGreg Kroah-Hartman 	} else {
209796fd7ce5SGreg Kroah-Hartman 		if (!tty->fasync && !waitqueue_active(&tty->read_wait))
209896fd7ce5SGreg Kroah-Hartman 			tty->minimum_to_wake = N_TTY_BUF_SIZE;
209996fd7ce5SGreg Kroah-Hartman 	}
210096fd7ce5SGreg Kroah-Hartman 	retval = 0;
210196fd7ce5SGreg Kroah-Hartman out:
210296fd7ce5SGreg Kroah-Hartman 	return retval;
210396fd7ce5SGreg Kroah-Hartman }
210496fd7ce5SGreg Kroah-Hartman 
210596fd7ce5SGreg Kroah-Hartman static int tty_fasync(int fd, struct file *filp, int on)
210696fd7ce5SGreg Kroah-Hartman {
210789c8d91eSAlan Cox 	struct tty_struct *tty = file_tty(filp);
210896fd7ce5SGreg Kroah-Hartman 	int retval;
210989c8d91eSAlan Cox 
211089c8d91eSAlan Cox 	tty_lock(tty);
211196fd7ce5SGreg Kroah-Hartman 	retval = __tty_fasync(fd, filp, on);
211289c8d91eSAlan Cox 	tty_unlock(tty);
211389c8d91eSAlan Cox 
211496fd7ce5SGreg Kroah-Hartman 	return retval;
211596fd7ce5SGreg Kroah-Hartman }
211696fd7ce5SGreg Kroah-Hartman 
211796fd7ce5SGreg Kroah-Hartman /**
211896fd7ce5SGreg Kroah-Hartman  *	tiocsti			-	fake input character
211996fd7ce5SGreg Kroah-Hartman  *	@tty: tty to fake input into
212096fd7ce5SGreg Kroah-Hartman  *	@p: pointer to character
212196fd7ce5SGreg Kroah-Hartman  *
212296fd7ce5SGreg Kroah-Hartman  *	Fake input to a tty device. Does the necessary locking and
212396fd7ce5SGreg Kroah-Hartman  *	input management.
212496fd7ce5SGreg Kroah-Hartman  *
212596fd7ce5SGreg Kroah-Hartman  *	FIXME: does not honour flow control ??
212696fd7ce5SGreg Kroah-Hartman  *
212796fd7ce5SGreg Kroah-Hartman  *	Locking:
212896fd7ce5SGreg Kroah-Hartman  *		Called functions take tty_ldisc_lock
212996fd7ce5SGreg Kroah-Hartman  *		current->signal->tty check is safe without locks
213096fd7ce5SGreg Kroah-Hartman  *
213196fd7ce5SGreg Kroah-Hartman  *	FIXME: may race normal receive processing
213296fd7ce5SGreg Kroah-Hartman  */
213396fd7ce5SGreg Kroah-Hartman 
213496fd7ce5SGreg Kroah-Hartman static int tiocsti(struct tty_struct *tty, char __user *p)
213596fd7ce5SGreg Kroah-Hartman {
213696fd7ce5SGreg Kroah-Hartman 	char ch, mbz = 0;
213796fd7ce5SGreg Kroah-Hartman 	struct tty_ldisc *ld;
213896fd7ce5SGreg Kroah-Hartman 
213996fd7ce5SGreg Kroah-Hartman 	if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN))
214096fd7ce5SGreg Kroah-Hartman 		return -EPERM;
214196fd7ce5SGreg Kroah-Hartman 	if (get_user(ch, p))
214296fd7ce5SGreg Kroah-Hartman 		return -EFAULT;
214396fd7ce5SGreg Kroah-Hartman 	tty_audit_tiocsti(tty, ch);
214496fd7ce5SGreg Kroah-Hartman 	ld = tty_ldisc_ref_wait(tty);
214596fd7ce5SGreg Kroah-Hartman 	ld->ops->receive_buf(tty, &ch, &mbz, 1);
214696fd7ce5SGreg Kroah-Hartman 	tty_ldisc_deref(ld);
214796fd7ce5SGreg Kroah-Hartman 	return 0;
214896fd7ce5SGreg Kroah-Hartman }
214996fd7ce5SGreg Kroah-Hartman 
215096fd7ce5SGreg Kroah-Hartman /**
215196fd7ce5SGreg Kroah-Hartman  *	tiocgwinsz		-	implement window query ioctl
215296fd7ce5SGreg Kroah-Hartman  *	@tty; tty
215396fd7ce5SGreg Kroah-Hartman  *	@arg: user buffer for result
215496fd7ce5SGreg Kroah-Hartman  *
215596fd7ce5SGreg Kroah-Hartman  *	Copies the kernel idea of the window size into the user buffer.
215696fd7ce5SGreg Kroah-Hartman  *
215796fd7ce5SGreg Kroah-Hartman  *	Locking: tty->termios_mutex is taken to ensure the winsize data
215896fd7ce5SGreg Kroah-Hartman  *		is consistent.
215996fd7ce5SGreg Kroah-Hartman  */
216096fd7ce5SGreg Kroah-Hartman 
216196fd7ce5SGreg Kroah-Hartman static int tiocgwinsz(struct tty_struct *tty, struct winsize __user *arg)
216296fd7ce5SGreg Kroah-Hartman {
216396fd7ce5SGreg Kroah-Hartman 	int err;
216496fd7ce5SGreg Kroah-Hartman 
216596fd7ce5SGreg Kroah-Hartman 	mutex_lock(&tty->termios_mutex);
216696fd7ce5SGreg Kroah-Hartman 	err = copy_to_user(arg, &tty->winsize, sizeof(*arg));
216796fd7ce5SGreg Kroah-Hartman 	mutex_unlock(&tty->termios_mutex);
216896fd7ce5SGreg Kroah-Hartman 
216996fd7ce5SGreg Kroah-Hartman 	return err ? -EFAULT: 0;
217096fd7ce5SGreg Kroah-Hartman }
217196fd7ce5SGreg Kroah-Hartman 
217296fd7ce5SGreg Kroah-Hartman /**
217396fd7ce5SGreg Kroah-Hartman  *	tty_do_resize		-	resize event
217496fd7ce5SGreg Kroah-Hartman  *	@tty: tty being resized
217596fd7ce5SGreg Kroah-Hartman  *	@rows: rows (character)
217696fd7ce5SGreg Kroah-Hartman  *	@cols: cols (character)
217796fd7ce5SGreg Kroah-Hartman  *
217896fd7ce5SGreg Kroah-Hartman  *	Update the termios variables and send the necessary signals to
217996fd7ce5SGreg Kroah-Hartman  *	peform a terminal resize correctly
218096fd7ce5SGreg Kroah-Hartman  */
218196fd7ce5SGreg Kroah-Hartman 
218296fd7ce5SGreg Kroah-Hartman int tty_do_resize(struct tty_struct *tty, struct winsize *ws)
218396fd7ce5SGreg Kroah-Hartman {
218496fd7ce5SGreg Kroah-Hartman 	struct pid *pgrp;
218596fd7ce5SGreg Kroah-Hartman 	unsigned long flags;
218696fd7ce5SGreg Kroah-Hartman 
218796fd7ce5SGreg Kroah-Hartman 	/* Lock the tty */
218896fd7ce5SGreg Kroah-Hartman 	mutex_lock(&tty->termios_mutex);
218996fd7ce5SGreg Kroah-Hartman 	if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
219096fd7ce5SGreg Kroah-Hartman 		goto done;
219196fd7ce5SGreg Kroah-Hartman 	/* Get the PID values and reference them so we can
219296fd7ce5SGreg Kroah-Hartman 	   avoid holding the tty ctrl lock while sending signals */
219396fd7ce5SGreg Kroah-Hartman 	spin_lock_irqsave(&tty->ctrl_lock, flags);
219496fd7ce5SGreg Kroah-Hartman 	pgrp = get_pid(tty->pgrp);
219596fd7ce5SGreg Kroah-Hartman 	spin_unlock_irqrestore(&tty->ctrl_lock, flags);
219696fd7ce5SGreg Kroah-Hartman 
219796fd7ce5SGreg Kroah-Hartman 	if (pgrp)
219896fd7ce5SGreg Kroah-Hartman 		kill_pgrp(pgrp, SIGWINCH, 1);
219996fd7ce5SGreg Kroah-Hartman 	put_pid(pgrp);
220096fd7ce5SGreg Kroah-Hartman 
220196fd7ce5SGreg Kroah-Hartman 	tty->winsize = *ws;
220296fd7ce5SGreg Kroah-Hartman done:
220396fd7ce5SGreg Kroah-Hartman 	mutex_unlock(&tty->termios_mutex);
220496fd7ce5SGreg Kroah-Hartman 	return 0;
220596fd7ce5SGreg Kroah-Hartman }
220696fd7ce5SGreg Kroah-Hartman 
220796fd7ce5SGreg Kroah-Hartman /**
220896fd7ce5SGreg Kroah-Hartman  *	tiocswinsz		-	implement window size set ioctl
220996fd7ce5SGreg Kroah-Hartman  *	@tty; tty side of tty
221096fd7ce5SGreg Kroah-Hartman  *	@arg: user buffer for result
221196fd7ce5SGreg Kroah-Hartman  *
221296fd7ce5SGreg Kroah-Hartman  *	Copies the user idea of the window size to the kernel. Traditionally
221396fd7ce5SGreg Kroah-Hartman  *	this is just advisory information but for the Linux console it
221496fd7ce5SGreg Kroah-Hartman  *	actually has driver level meaning and triggers a VC resize.
221596fd7ce5SGreg Kroah-Hartman  *
221696fd7ce5SGreg Kroah-Hartman  *	Locking:
221725985edcSLucas De Marchi  *		Driver dependent. The default do_resize method takes the
221896fd7ce5SGreg Kroah-Hartman  *	tty termios mutex and ctrl_lock. The console takes its own lock
221996fd7ce5SGreg Kroah-Hartman  *	then calls into the default method.
222096fd7ce5SGreg Kroah-Hartman  */
222196fd7ce5SGreg Kroah-Hartman 
222296fd7ce5SGreg Kroah-Hartman static int tiocswinsz(struct tty_struct *tty, struct winsize __user *arg)
222396fd7ce5SGreg Kroah-Hartman {
222496fd7ce5SGreg Kroah-Hartman 	struct winsize tmp_ws;
222596fd7ce5SGreg Kroah-Hartman 	if (copy_from_user(&tmp_ws, arg, sizeof(*arg)))
222696fd7ce5SGreg Kroah-Hartman 		return -EFAULT;
222796fd7ce5SGreg Kroah-Hartman 
222896fd7ce5SGreg Kroah-Hartman 	if (tty->ops->resize)
222996fd7ce5SGreg Kroah-Hartman 		return tty->ops->resize(tty, &tmp_ws);
223096fd7ce5SGreg Kroah-Hartman 	else
223196fd7ce5SGreg Kroah-Hartman 		return tty_do_resize(tty, &tmp_ws);
223296fd7ce5SGreg Kroah-Hartman }
223396fd7ce5SGreg Kroah-Hartman 
223496fd7ce5SGreg Kroah-Hartman /**
223596fd7ce5SGreg Kroah-Hartman  *	tioccons	-	allow admin to move logical console
223696fd7ce5SGreg Kroah-Hartman  *	@file: the file to become console
223796fd7ce5SGreg Kroah-Hartman  *
223825985edcSLucas De Marchi  *	Allow the administrator to move the redirected console device
223996fd7ce5SGreg Kroah-Hartman  *
224096fd7ce5SGreg Kroah-Hartman  *	Locking: uses redirect_lock to guard the redirect information
224196fd7ce5SGreg Kroah-Hartman  */
224296fd7ce5SGreg Kroah-Hartman 
224396fd7ce5SGreg Kroah-Hartman static int tioccons(struct file *file)
224496fd7ce5SGreg Kroah-Hartman {
224596fd7ce5SGreg Kroah-Hartman 	if (!capable(CAP_SYS_ADMIN))
224696fd7ce5SGreg Kroah-Hartman 		return -EPERM;
224796fd7ce5SGreg Kroah-Hartman 	if (file->f_op->write == redirected_tty_write) {
224896fd7ce5SGreg Kroah-Hartman 		struct file *f;
224996fd7ce5SGreg Kroah-Hartman 		spin_lock(&redirect_lock);
225096fd7ce5SGreg Kroah-Hartman 		f = redirect;
225196fd7ce5SGreg Kroah-Hartman 		redirect = NULL;
225296fd7ce5SGreg Kroah-Hartman 		spin_unlock(&redirect_lock);
225396fd7ce5SGreg Kroah-Hartman 		if (f)
225496fd7ce5SGreg Kroah-Hartman 			fput(f);
225596fd7ce5SGreg Kroah-Hartman 		return 0;
225696fd7ce5SGreg Kroah-Hartman 	}
225796fd7ce5SGreg Kroah-Hartman 	spin_lock(&redirect_lock);
225896fd7ce5SGreg Kroah-Hartman 	if (redirect) {
225996fd7ce5SGreg Kroah-Hartman 		spin_unlock(&redirect_lock);
226096fd7ce5SGreg Kroah-Hartman 		return -EBUSY;
226196fd7ce5SGreg Kroah-Hartman 	}
2262cb0942b8SAl Viro 	redirect = get_file(file);
226396fd7ce5SGreg Kroah-Hartman 	spin_unlock(&redirect_lock);
226496fd7ce5SGreg Kroah-Hartman 	return 0;
226596fd7ce5SGreg Kroah-Hartman }
226696fd7ce5SGreg Kroah-Hartman 
226796fd7ce5SGreg Kroah-Hartman /**
226896fd7ce5SGreg Kroah-Hartman  *	fionbio		-	non blocking ioctl
226996fd7ce5SGreg Kroah-Hartman  *	@file: file to set blocking value
227096fd7ce5SGreg Kroah-Hartman  *	@p: user parameter
227196fd7ce5SGreg Kroah-Hartman  *
227296fd7ce5SGreg Kroah-Hartman  *	Historical tty interfaces had a blocking control ioctl before
227396fd7ce5SGreg Kroah-Hartman  *	the generic functionality existed. This piece of history is preserved
227496fd7ce5SGreg Kroah-Hartman  *	in the expected tty API of posix OS's.
227596fd7ce5SGreg Kroah-Hartman  *
227696fd7ce5SGreg Kroah-Hartman  *	Locking: none, the open file handle ensures it won't go away.
227796fd7ce5SGreg Kroah-Hartman  */
227896fd7ce5SGreg Kroah-Hartman 
227996fd7ce5SGreg Kroah-Hartman static int fionbio(struct file *file, int __user *p)
228096fd7ce5SGreg Kroah-Hartman {
228196fd7ce5SGreg Kroah-Hartman 	int nonblock;
228296fd7ce5SGreg Kroah-Hartman 
228396fd7ce5SGreg Kroah-Hartman 	if (get_user(nonblock, p))
228496fd7ce5SGreg Kroah-Hartman 		return -EFAULT;
228596fd7ce5SGreg Kroah-Hartman 
228696fd7ce5SGreg Kroah-Hartman 	spin_lock(&file->f_lock);
228796fd7ce5SGreg Kroah-Hartman 	if (nonblock)
228896fd7ce5SGreg Kroah-Hartman 		file->f_flags |= O_NONBLOCK;
228996fd7ce5SGreg Kroah-Hartman 	else
229096fd7ce5SGreg Kroah-Hartman 		file->f_flags &= ~O_NONBLOCK;
229196fd7ce5SGreg Kroah-Hartman 	spin_unlock(&file->f_lock);
229296fd7ce5SGreg Kroah-Hartman 	return 0;
229396fd7ce5SGreg Kroah-Hartman }
229496fd7ce5SGreg Kroah-Hartman 
229596fd7ce5SGreg Kroah-Hartman /**
229696fd7ce5SGreg Kroah-Hartman  *	tiocsctty	-	set controlling tty
229796fd7ce5SGreg Kroah-Hartman  *	@tty: tty structure
229896fd7ce5SGreg Kroah-Hartman  *	@arg: user argument
229996fd7ce5SGreg Kroah-Hartman  *
230096fd7ce5SGreg Kroah-Hartman  *	This ioctl is used to manage job control. It permits a session
230196fd7ce5SGreg Kroah-Hartman  *	leader to set this tty as the controlling tty for the session.
230296fd7ce5SGreg Kroah-Hartman  *
230396fd7ce5SGreg Kroah-Hartman  *	Locking:
230496fd7ce5SGreg Kroah-Hartman  *		Takes tty_mutex() to protect tty instance
230596fd7ce5SGreg Kroah-Hartman  *		Takes tasklist_lock internally to walk sessions
230696fd7ce5SGreg Kroah-Hartman  *		Takes ->siglock() when updating signal->tty
230796fd7ce5SGreg Kroah-Hartman  */
230896fd7ce5SGreg Kroah-Hartman 
230996fd7ce5SGreg Kroah-Hartman static int tiocsctty(struct tty_struct *tty, int arg)
231096fd7ce5SGreg Kroah-Hartman {
231196fd7ce5SGreg Kroah-Hartman 	int ret = 0;
231296fd7ce5SGreg Kroah-Hartman 	if (current->signal->leader && (task_session(current) == tty->session))
231396fd7ce5SGreg Kroah-Hartman 		return ret;
231496fd7ce5SGreg Kroah-Hartman 
231596fd7ce5SGreg Kroah-Hartman 	mutex_lock(&tty_mutex);
231696fd7ce5SGreg Kroah-Hartman 	/*
231796fd7ce5SGreg Kroah-Hartman 	 * The process must be a session leader and
231896fd7ce5SGreg Kroah-Hartman 	 * not have a controlling tty already.
231996fd7ce5SGreg Kroah-Hartman 	 */
232096fd7ce5SGreg Kroah-Hartman 	if (!current->signal->leader || current->signal->tty) {
232196fd7ce5SGreg Kroah-Hartman 		ret = -EPERM;
232296fd7ce5SGreg Kroah-Hartman 		goto unlock;
232396fd7ce5SGreg Kroah-Hartman 	}
232496fd7ce5SGreg Kroah-Hartman 
232596fd7ce5SGreg Kroah-Hartman 	if (tty->session) {
232696fd7ce5SGreg Kroah-Hartman 		/*
232796fd7ce5SGreg Kroah-Hartman 		 * This tty is already the controlling
232896fd7ce5SGreg Kroah-Hartman 		 * tty for another session group!
232996fd7ce5SGreg Kroah-Hartman 		 */
233096fd7ce5SGreg Kroah-Hartman 		if (arg == 1 && capable(CAP_SYS_ADMIN)) {
233196fd7ce5SGreg Kroah-Hartman 			/*
233296fd7ce5SGreg Kroah-Hartman 			 * Steal it away
233396fd7ce5SGreg Kroah-Hartman 			 */
233496fd7ce5SGreg Kroah-Hartman 			read_lock(&tasklist_lock);
233596fd7ce5SGreg Kroah-Hartman 			session_clear_tty(tty->session);
233696fd7ce5SGreg Kroah-Hartman 			read_unlock(&tasklist_lock);
233796fd7ce5SGreg Kroah-Hartman 		} else {
233896fd7ce5SGreg Kroah-Hartman 			ret = -EPERM;
233996fd7ce5SGreg Kroah-Hartman 			goto unlock;
234096fd7ce5SGreg Kroah-Hartman 		}
234196fd7ce5SGreg Kroah-Hartman 	}
234296fd7ce5SGreg Kroah-Hartman 	proc_set_tty(current, tty);
234396fd7ce5SGreg Kroah-Hartman unlock:
234496fd7ce5SGreg Kroah-Hartman 	mutex_unlock(&tty_mutex);
234596fd7ce5SGreg Kroah-Hartman 	return ret;
234696fd7ce5SGreg Kroah-Hartman }
234796fd7ce5SGreg Kroah-Hartman 
234896fd7ce5SGreg Kroah-Hartman /**
234996fd7ce5SGreg Kroah-Hartman  *	tty_get_pgrp	-	return a ref counted pgrp pid
235096fd7ce5SGreg Kroah-Hartman  *	@tty: tty to read
235196fd7ce5SGreg Kroah-Hartman  *
235296fd7ce5SGreg Kroah-Hartman  *	Returns a refcounted instance of the pid struct for the process
235396fd7ce5SGreg Kroah-Hartman  *	group controlling the tty.
235496fd7ce5SGreg Kroah-Hartman  */
235596fd7ce5SGreg Kroah-Hartman 
235696fd7ce5SGreg Kroah-Hartman struct pid *tty_get_pgrp(struct tty_struct *tty)
235796fd7ce5SGreg Kroah-Hartman {
235896fd7ce5SGreg Kroah-Hartman 	unsigned long flags;
235996fd7ce5SGreg Kroah-Hartman 	struct pid *pgrp;
236096fd7ce5SGreg Kroah-Hartman 
236196fd7ce5SGreg Kroah-Hartman 	spin_lock_irqsave(&tty->ctrl_lock, flags);
236296fd7ce5SGreg Kroah-Hartman 	pgrp = get_pid(tty->pgrp);
236396fd7ce5SGreg Kroah-Hartman 	spin_unlock_irqrestore(&tty->ctrl_lock, flags);
236496fd7ce5SGreg Kroah-Hartman 
236596fd7ce5SGreg Kroah-Hartman 	return pgrp;
236696fd7ce5SGreg Kroah-Hartman }
236796fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(tty_get_pgrp);
236896fd7ce5SGreg Kroah-Hartman 
236996fd7ce5SGreg Kroah-Hartman /**
237096fd7ce5SGreg Kroah-Hartman  *	tiocgpgrp		-	get process group
237196fd7ce5SGreg Kroah-Hartman  *	@tty: tty passed by user
237225985edcSLucas De Marchi  *	@real_tty: tty side of the tty passed by the user if a pty else the tty
237396fd7ce5SGreg Kroah-Hartman  *	@p: returned pid
237496fd7ce5SGreg Kroah-Hartman  *
237596fd7ce5SGreg Kroah-Hartman  *	Obtain the process group of the tty. If there is no process group
237696fd7ce5SGreg Kroah-Hartman  *	return an error.
237796fd7ce5SGreg Kroah-Hartman  *
237896fd7ce5SGreg Kroah-Hartman  *	Locking: none. Reference to current->signal->tty is safe.
237996fd7ce5SGreg Kroah-Hartman  */
238096fd7ce5SGreg Kroah-Hartman 
238196fd7ce5SGreg Kroah-Hartman static int tiocgpgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
238296fd7ce5SGreg Kroah-Hartman {
238396fd7ce5SGreg Kroah-Hartman 	struct pid *pid;
238496fd7ce5SGreg Kroah-Hartman 	int ret;
238596fd7ce5SGreg Kroah-Hartman 	/*
238696fd7ce5SGreg Kroah-Hartman 	 * (tty == real_tty) is a cheap way of
238796fd7ce5SGreg Kroah-Hartman 	 * testing if the tty is NOT a master pty.
238896fd7ce5SGreg Kroah-Hartman 	 */
238996fd7ce5SGreg Kroah-Hartman 	if (tty == real_tty && current->signal->tty != real_tty)
239096fd7ce5SGreg Kroah-Hartman 		return -ENOTTY;
239196fd7ce5SGreg Kroah-Hartman 	pid = tty_get_pgrp(real_tty);
239296fd7ce5SGreg Kroah-Hartman 	ret =  put_user(pid_vnr(pid), p);
239396fd7ce5SGreg Kroah-Hartman 	put_pid(pid);
239496fd7ce5SGreg Kroah-Hartman 	return ret;
239596fd7ce5SGreg Kroah-Hartman }
239696fd7ce5SGreg Kroah-Hartman 
239796fd7ce5SGreg Kroah-Hartman /**
239896fd7ce5SGreg Kroah-Hartman  *	tiocspgrp		-	attempt to set process group
239996fd7ce5SGreg Kroah-Hartman  *	@tty: tty passed by user
240096fd7ce5SGreg Kroah-Hartman  *	@real_tty: tty side device matching tty passed by user
240196fd7ce5SGreg Kroah-Hartman  *	@p: pid pointer
240296fd7ce5SGreg Kroah-Hartman  *
240396fd7ce5SGreg Kroah-Hartman  *	Set the process group of the tty to the session passed. Only
240496fd7ce5SGreg Kroah-Hartman  *	permitted where the tty session is our session.
240596fd7ce5SGreg Kroah-Hartman  *
240696fd7ce5SGreg Kroah-Hartman  *	Locking: RCU, ctrl lock
240796fd7ce5SGreg Kroah-Hartman  */
240896fd7ce5SGreg Kroah-Hartman 
240996fd7ce5SGreg Kroah-Hartman static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
241096fd7ce5SGreg Kroah-Hartman {
241196fd7ce5SGreg Kroah-Hartman 	struct pid *pgrp;
241296fd7ce5SGreg Kroah-Hartman 	pid_t pgrp_nr;
241396fd7ce5SGreg Kroah-Hartman 	int retval = tty_check_change(real_tty);
241496fd7ce5SGreg Kroah-Hartman 	unsigned long flags;
241596fd7ce5SGreg Kroah-Hartman 
241696fd7ce5SGreg Kroah-Hartman 	if (retval == -EIO)
241796fd7ce5SGreg Kroah-Hartman 		return -ENOTTY;
241896fd7ce5SGreg Kroah-Hartman 	if (retval)
241996fd7ce5SGreg Kroah-Hartman 		return retval;
242096fd7ce5SGreg Kroah-Hartman 	if (!current->signal->tty ||
242196fd7ce5SGreg Kroah-Hartman 	    (current->signal->tty != real_tty) ||
242296fd7ce5SGreg Kroah-Hartman 	    (real_tty->session != task_session(current)))
242396fd7ce5SGreg Kroah-Hartman 		return -ENOTTY;
242496fd7ce5SGreg Kroah-Hartman 	if (get_user(pgrp_nr, p))
242596fd7ce5SGreg Kroah-Hartman 		return -EFAULT;
242696fd7ce5SGreg Kroah-Hartman 	if (pgrp_nr < 0)
242796fd7ce5SGreg Kroah-Hartman 		return -EINVAL;
242896fd7ce5SGreg Kroah-Hartman 	rcu_read_lock();
242996fd7ce5SGreg Kroah-Hartman 	pgrp = find_vpid(pgrp_nr);
243096fd7ce5SGreg Kroah-Hartman 	retval = -ESRCH;
243196fd7ce5SGreg Kroah-Hartman 	if (!pgrp)
243296fd7ce5SGreg Kroah-Hartman 		goto out_unlock;
243396fd7ce5SGreg Kroah-Hartman 	retval = -EPERM;
243496fd7ce5SGreg Kroah-Hartman 	if (session_of_pgrp(pgrp) != task_session(current))
243596fd7ce5SGreg Kroah-Hartman 		goto out_unlock;
243696fd7ce5SGreg Kroah-Hartman 	retval = 0;
243796fd7ce5SGreg Kroah-Hartman 	spin_lock_irqsave(&tty->ctrl_lock, flags);
243896fd7ce5SGreg Kroah-Hartman 	put_pid(real_tty->pgrp);
243996fd7ce5SGreg Kroah-Hartman 	real_tty->pgrp = get_pid(pgrp);
244096fd7ce5SGreg Kroah-Hartman 	spin_unlock_irqrestore(&tty->ctrl_lock, flags);
244196fd7ce5SGreg Kroah-Hartman out_unlock:
244296fd7ce5SGreg Kroah-Hartman 	rcu_read_unlock();
244396fd7ce5SGreg Kroah-Hartman 	return retval;
244496fd7ce5SGreg Kroah-Hartman }
244596fd7ce5SGreg Kroah-Hartman 
244696fd7ce5SGreg Kroah-Hartman /**
244796fd7ce5SGreg Kroah-Hartman  *	tiocgsid		-	get session id
244896fd7ce5SGreg Kroah-Hartman  *	@tty: tty passed by user
244925985edcSLucas De Marchi  *	@real_tty: tty side of the tty passed by the user if a pty else the tty
245096fd7ce5SGreg Kroah-Hartman  *	@p: pointer to returned session id
245196fd7ce5SGreg Kroah-Hartman  *
245296fd7ce5SGreg Kroah-Hartman  *	Obtain the session id of the tty. If there is no session
245396fd7ce5SGreg Kroah-Hartman  *	return an error.
245496fd7ce5SGreg Kroah-Hartman  *
245596fd7ce5SGreg Kroah-Hartman  *	Locking: none. Reference to current->signal->tty is safe.
245696fd7ce5SGreg Kroah-Hartman  */
245796fd7ce5SGreg Kroah-Hartman 
245896fd7ce5SGreg Kroah-Hartman static int tiocgsid(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
245996fd7ce5SGreg Kroah-Hartman {
246096fd7ce5SGreg Kroah-Hartman 	/*
246196fd7ce5SGreg Kroah-Hartman 	 * (tty == real_tty) is a cheap way of
246296fd7ce5SGreg Kroah-Hartman 	 * testing if the tty is NOT a master pty.
246396fd7ce5SGreg Kroah-Hartman 	*/
246496fd7ce5SGreg Kroah-Hartman 	if (tty == real_tty && current->signal->tty != real_tty)
246596fd7ce5SGreg Kroah-Hartman 		return -ENOTTY;
246696fd7ce5SGreg Kroah-Hartman 	if (!real_tty->session)
246796fd7ce5SGreg Kroah-Hartman 		return -ENOTTY;
246896fd7ce5SGreg Kroah-Hartman 	return put_user(pid_vnr(real_tty->session), p);
246996fd7ce5SGreg Kroah-Hartman }
247096fd7ce5SGreg Kroah-Hartman 
247196fd7ce5SGreg Kroah-Hartman /**
247296fd7ce5SGreg Kroah-Hartman  *	tiocsetd	-	set line discipline
247396fd7ce5SGreg Kroah-Hartman  *	@tty: tty device
247496fd7ce5SGreg Kroah-Hartman  *	@p: pointer to user data
247596fd7ce5SGreg Kroah-Hartman  *
247696fd7ce5SGreg Kroah-Hartman  *	Set the line discipline according to user request.
247796fd7ce5SGreg Kroah-Hartman  *
247896fd7ce5SGreg Kroah-Hartman  *	Locking: see tty_set_ldisc, this function is just a helper
247996fd7ce5SGreg Kroah-Hartman  */
248096fd7ce5SGreg Kroah-Hartman 
248196fd7ce5SGreg Kroah-Hartman static int tiocsetd(struct tty_struct *tty, int __user *p)
248296fd7ce5SGreg Kroah-Hartman {
248396fd7ce5SGreg Kroah-Hartman 	int ldisc;
248496fd7ce5SGreg Kroah-Hartman 	int ret;
248596fd7ce5SGreg Kroah-Hartman 
248696fd7ce5SGreg Kroah-Hartman 	if (get_user(ldisc, p))
248796fd7ce5SGreg Kroah-Hartman 		return -EFAULT;
248896fd7ce5SGreg Kroah-Hartman 
248996fd7ce5SGreg Kroah-Hartman 	ret = tty_set_ldisc(tty, ldisc);
249096fd7ce5SGreg Kroah-Hartman 
249196fd7ce5SGreg Kroah-Hartman 	return ret;
249296fd7ce5SGreg Kroah-Hartman }
249396fd7ce5SGreg Kroah-Hartman 
249496fd7ce5SGreg Kroah-Hartman /**
249596fd7ce5SGreg Kroah-Hartman  *	send_break	-	performed time break
249696fd7ce5SGreg Kroah-Hartman  *	@tty: device to break on
249796fd7ce5SGreg Kroah-Hartman  *	@duration: timeout in mS
249896fd7ce5SGreg Kroah-Hartman  *
249996fd7ce5SGreg Kroah-Hartman  *	Perform a timed break on hardware that lacks its own driver level
250096fd7ce5SGreg Kroah-Hartman  *	timed break functionality.
250196fd7ce5SGreg Kroah-Hartman  *
250296fd7ce5SGreg Kroah-Hartman  *	Locking:
250396fd7ce5SGreg Kroah-Hartman  *		atomic_write_lock serializes
250496fd7ce5SGreg Kroah-Hartman  *
250596fd7ce5SGreg Kroah-Hartman  */
250696fd7ce5SGreg Kroah-Hartman 
250796fd7ce5SGreg Kroah-Hartman static int send_break(struct tty_struct *tty, unsigned int duration)
250896fd7ce5SGreg Kroah-Hartman {
250996fd7ce5SGreg Kroah-Hartman 	int retval;
251096fd7ce5SGreg Kroah-Hartman 
251196fd7ce5SGreg Kroah-Hartman 	if (tty->ops->break_ctl == NULL)
251296fd7ce5SGreg Kroah-Hartman 		return 0;
251396fd7ce5SGreg Kroah-Hartman 
251496fd7ce5SGreg Kroah-Hartman 	if (tty->driver->flags & TTY_DRIVER_HARDWARE_BREAK)
251596fd7ce5SGreg Kroah-Hartman 		retval = tty->ops->break_ctl(tty, duration);
251696fd7ce5SGreg Kroah-Hartman 	else {
251796fd7ce5SGreg Kroah-Hartman 		/* Do the work ourselves */
251896fd7ce5SGreg Kroah-Hartman 		if (tty_write_lock(tty, 0) < 0)
251996fd7ce5SGreg Kroah-Hartman 			return -EINTR;
252096fd7ce5SGreg Kroah-Hartman 		retval = tty->ops->break_ctl(tty, -1);
252196fd7ce5SGreg Kroah-Hartman 		if (retval)
252296fd7ce5SGreg Kroah-Hartman 			goto out;
252396fd7ce5SGreg Kroah-Hartman 		if (!signal_pending(current))
252496fd7ce5SGreg Kroah-Hartman 			msleep_interruptible(duration);
252596fd7ce5SGreg Kroah-Hartman 		retval = tty->ops->break_ctl(tty, 0);
252696fd7ce5SGreg Kroah-Hartman out:
252796fd7ce5SGreg Kroah-Hartman 		tty_write_unlock(tty);
252896fd7ce5SGreg Kroah-Hartman 		if (signal_pending(current))
252996fd7ce5SGreg Kroah-Hartman 			retval = -EINTR;
253096fd7ce5SGreg Kroah-Hartman 	}
253196fd7ce5SGreg Kroah-Hartman 	return retval;
253296fd7ce5SGreg Kroah-Hartman }
253396fd7ce5SGreg Kroah-Hartman 
253496fd7ce5SGreg Kroah-Hartman /**
253596fd7ce5SGreg Kroah-Hartman  *	tty_tiocmget		-	get modem status
253696fd7ce5SGreg Kroah-Hartman  *	@tty: tty device
253796fd7ce5SGreg Kroah-Hartman  *	@file: user file pointer
253896fd7ce5SGreg Kroah-Hartman  *	@p: pointer to result
253996fd7ce5SGreg Kroah-Hartman  *
254096fd7ce5SGreg Kroah-Hartman  *	Obtain the modem status bits from the tty driver if the feature
254196fd7ce5SGreg Kroah-Hartman  *	is supported. Return -EINVAL if it is not available.
254296fd7ce5SGreg Kroah-Hartman  *
254396fd7ce5SGreg Kroah-Hartman  *	Locking: none (up to the driver)
254496fd7ce5SGreg Kroah-Hartman  */
254596fd7ce5SGreg Kroah-Hartman 
254660b33c13SAlan Cox static int tty_tiocmget(struct tty_struct *tty, int __user *p)
254796fd7ce5SGreg Kroah-Hartman {
254896fd7ce5SGreg Kroah-Hartman 	int retval = -EINVAL;
254996fd7ce5SGreg Kroah-Hartman 
255096fd7ce5SGreg Kroah-Hartman 	if (tty->ops->tiocmget) {
255160b33c13SAlan Cox 		retval = tty->ops->tiocmget(tty);
255296fd7ce5SGreg Kroah-Hartman 
255396fd7ce5SGreg Kroah-Hartman 		if (retval >= 0)
255496fd7ce5SGreg Kroah-Hartman 			retval = put_user(retval, p);
255596fd7ce5SGreg Kroah-Hartman 	}
255696fd7ce5SGreg Kroah-Hartman 	return retval;
255796fd7ce5SGreg Kroah-Hartman }
255896fd7ce5SGreg Kroah-Hartman 
255996fd7ce5SGreg Kroah-Hartman /**
256096fd7ce5SGreg Kroah-Hartman  *	tty_tiocmset		-	set modem status
256196fd7ce5SGreg Kroah-Hartman  *	@tty: tty device
256296fd7ce5SGreg Kroah-Hartman  *	@cmd: command - clear bits, set bits or set all
256396fd7ce5SGreg Kroah-Hartman  *	@p: pointer to desired bits
256496fd7ce5SGreg Kroah-Hartman  *
256596fd7ce5SGreg Kroah-Hartman  *	Set the modem status bits from the tty driver if the feature
256696fd7ce5SGreg Kroah-Hartman  *	is supported. Return -EINVAL if it is not available.
256796fd7ce5SGreg Kroah-Hartman  *
256896fd7ce5SGreg Kroah-Hartman  *	Locking: none (up to the driver)
256996fd7ce5SGreg Kroah-Hartman  */
257096fd7ce5SGreg Kroah-Hartman 
257120b9d177SAlan Cox static int tty_tiocmset(struct tty_struct *tty, unsigned int cmd,
257296fd7ce5SGreg Kroah-Hartman 	     unsigned __user *p)
257396fd7ce5SGreg Kroah-Hartman {
257496fd7ce5SGreg Kroah-Hartman 	int retval;
257596fd7ce5SGreg Kroah-Hartman 	unsigned int set, clear, val;
257696fd7ce5SGreg Kroah-Hartman 
257796fd7ce5SGreg Kroah-Hartman 	if (tty->ops->tiocmset == NULL)
257896fd7ce5SGreg Kroah-Hartman 		return -EINVAL;
257996fd7ce5SGreg Kroah-Hartman 
258096fd7ce5SGreg Kroah-Hartman 	retval = get_user(val, p);
258196fd7ce5SGreg Kroah-Hartman 	if (retval)
258296fd7ce5SGreg Kroah-Hartman 		return retval;
258396fd7ce5SGreg Kroah-Hartman 	set = clear = 0;
258496fd7ce5SGreg Kroah-Hartman 	switch (cmd) {
258596fd7ce5SGreg Kroah-Hartman 	case TIOCMBIS:
258696fd7ce5SGreg Kroah-Hartman 		set = val;
258796fd7ce5SGreg Kroah-Hartman 		break;
258896fd7ce5SGreg Kroah-Hartman 	case TIOCMBIC:
258996fd7ce5SGreg Kroah-Hartman 		clear = val;
259096fd7ce5SGreg Kroah-Hartman 		break;
259196fd7ce5SGreg Kroah-Hartman 	case TIOCMSET:
259296fd7ce5SGreg Kroah-Hartman 		set = val;
259396fd7ce5SGreg Kroah-Hartman 		clear = ~val;
259496fd7ce5SGreg Kroah-Hartman 		break;
259596fd7ce5SGreg Kroah-Hartman 	}
259696fd7ce5SGreg Kroah-Hartman 	set &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
259796fd7ce5SGreg Kroah-Hartman 	clear &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
259820b9d177SAlan Cox 	return tty->ops->tiocmset(tty, set, clear);
259996fd7ce5SGreg Kroah-Hartman }
260096fd7ce5SGreg Kroah-Hartman 
260196fd7ce5SGreg Kroah-Hartman static int tty_tiocgicount(struct tty_struct *tty, void __user *arg)
260296fd7ce5SGreg Kroah-Hartman {
260396fd7ce5SGreg Kroah-Hartman 	int retval = -EINVAL;
260496fd7ce5SGreg Kroah-Hartman 	struct serial_icounter_struct icount;
260596fd7ce5SGreg Kroah-Hartman 	memset(&icount, 0, sizeof(icount));
260696fd7ce5SGreg Kroah-Hartman 	if (tty->ops->get_icount)
260796fd7ce5SGreg Kroah-Hartman 		retval = tty->ops->get_icount(tty, &icount);
260896fd7ce5SGreg Kroah-Hartman 	if (retval != 0)
260996fd7ce5SGreg Kroah-Hartman 		return retval;
261096fd7ce5SGreg Kroah-Hartman 	if (copy_to_user(arg, &icount, sizeof(icount)))
261196fd7ce5SGreg Kroah-Hartman 		return -EFAULT;
261296fd7ce5SGreg Kroah-Hartman 	return 0;
261396fd7ce5SGreg Kroah-Hartman }
261496fd7ce5SGreg Kroah-Hartman 
261596fd7ce5SGreg Kroah-Hartman struct tty_struct *tty_pair_get_tty(struct tty_struct *tty)
261696fd7ce5SGreg Kroah-Hartman {
261796fd7ce5SGreg Kroah-Hartman 	if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
261896fd7ce5SGreg Kroah-Hartman 	    tty->driver->subtype == PTY_TYPE_MASTER)
261996fd7ce5SGreg Kroah-Hartman 		tty = tty->link;
262096fd7ce5SGreg Kroah-Hartman 	return tty;
262196fd7ce5SGreg Kroah-Hartman }
262296fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_pair_get_tty);
262396fd7ce5SGreg Kroah-Hartman 
262496fd7ce5SGreg Kroah-Hartman struct tty_struct *tty_pair_get_pty(struct tty_struct *tty)
262596fd7ce5SGreg Kroah-Hartman {
262696fd7ce5SGreg Kroah-Hartman 	if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
262796fd7ce5SGreg Kroah-Hartman 	    tty->driver->subtype == PTY_TYPE_MASTER)
262896fd7ce5SGreg Kroah-Hartman 	    return tty;
262996fd7ce5SGreg Kroah-Hartman 	return tty->link;
263096fd7ce5SGreg Kroah-Hartman }
263196fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_pair_get_pty);
263296fd7ce5SGreg Kroah-Hartman 
263396fd7ce5SGreg Kroah-Hartman /*
263496fd7ce5SGreg Kroah-Hartman  * Split this up, as gcc can choke on it otherwise..
263596fd7ce5SGreg Kroah-Hartman  */
263696fd7ce5SGreg Kroah-Hartman long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
263796fd7ce5SGreg Kroah-Hartman {
263896fd7ce5SGreg Kroah-Hartman 	struct tty_struct *tty = file_tty(file);
263996fd7ce5SGreg Kroah-Hartman 	struct tty_struct *real_tty;
264096fd7ce5SGreg Kroah-Hartman 	void __user *p = (void __user *)arg;
264196fd7ce5SGreg Kroah-Hartman 	int retval;
264296fd7ce5SGreg Kroah-Hartman 	struct tty_ldisc *ld;
264396fd7ce5SGreg Kroah-Hartman 	struct inode *inode = file->f_dentry->d_inode;
264496fd7ce5SGreg Kroah-Hartman 
264596fd7ce5SGreg Kroah-Hartman 	if (tty_paranoia_check(tty, inode, "tty_ioctl"))
264696fd7ce5SGreg Kroah-Hartman 		return -EINVAL;
264796fd7ce5SGreg Kroah-Hartman 
264896fd7ce5SGreg Kroah-Hartman 	real_tty = tty_pair_get_tty(tty);
264996fd7ce5SGreg Kroah-Hartman 
265096fd7ce5SGreg Kroah-Hartman 	/*
265196fd7ce5SGreg Kroah-Hartman 	 * Factor out some common prep work
265296fd7ce5SGreg Kroah-Hartman 	 */
265396fd7ce5SGreg Kroah-Hartman 	switch (cmd) {
265496fd7ce5SGreg Kroah-Hartman 	case TIOCSETD:
265596fd7ce5SGreg Kroah-Hartman 	case TIOCSBRK:
265696fd7ce5SGreg Kroah-Hartman 	case TIOCCBRK:
265796fd7ce5SGreg Kroah-Hartman 	case TCSBRK:
265896fd7ce5SGreg Kroah-Hartman 	case TCSBRKP:
265996fd7ce5SGreg Kroah-Hartman 		retval = tty_check_change(tty);
266096fd7ce5SGreg Kroah-Hartman 		if (retval)
266196fd7ce5SGreg Kroah-Hartman 			return retval;
266296fd7ce5SGreg Kroah-Hartman 		if (cmd != TIOCCBRK) {
266396fd7ce5SGreg Kroah-Hartman 			tty_wait_until_sent(tty, 0);
266496fd7ce5SGreg Kroah-Hartman 			if (signal_pending(current))
266596fd7ce5SGreg Kroah-Hartman 				return -EINTR;
266696fd7ce5SGreg Kroah-Hartman 		}
266796fd7ce5SGreg Kroah-Hartman 		break;
266896fd7ce5SGreg Kroah-Hartman 	}
266996fd7ce5SGreg Kroah-Hartman 
267096fd7ce5SGreg Kroah-Hartman 	/*
267196fd7ce5SGreg Kroah-Hartman 	 *	Now do the stuff.
267296fd7ce5SGreg Kroah-Hartman 	 */
267396fd7ce5SGreg Kroah-Hartman 	switch (cmd) {
267496fd7ce5SGreg Kroah-Hartman 	case TIOCSTI:
267596fd7ce5SGreg Kroah-Hartman 		return tiocsti(tty, p);
267696fd7ce5SGreg Kroah-Hartman 	case TIOCGWINSZ:
267796fd7ce5SGreg Kroah-Hartman 		return tiocgwinsz(real_tty, p);
267896fd7ce5SGreg Kroah-Hartman 	case TIOCSWINSZ:
267996fd7ce5SGreg Kroah-Hartman 		return tiocswinsz(real_tty, p);
268096fd7ce5SGreg Kroah-Hartman 	case TIOCCONS:
268196fd7ce5SGreg Kroah-Hartman 		return real_tty != tty ? -EINVAL : tioccons(file);
268296fd7ce5SGreg Kroah-Hartman 	case FIONBIO:
268396fd7ce5SGreg Kroah-Hartman 		return fionbio(file, p);
268496fd7ce5SGreg Kroah-Hartman 	case TIOCEXCL:
268596fd7ce5SGreg Kroah-Hartman 		set_bit(TTY_EXCLUSIVE, &tty->flags);
268696fd7ce5SGreg Kroah-Hartman 		return 0;
268796fd7ce5SGreg Kroah-Hartman 	case TIOCNXCL:
268896fd7ce5SGreg Kroah-Hartman 		clear_bit(TTY_EXCLUSIVE, &tty->flags);
268996fd7ce5SGreg Kroah-Hartman 		return 0;
269084fd7bdfSCyrill Gorcunov 	case TIOCGEXCL:
269184fd7bdfSCyrill Gorcunov 	{
269284fd7bdfSCyrill Gorcunov 		int excl = test_bit(TTY_EXCLUSIVE, &tty->flags);
269384fd7bdfSCyrill Gorcunov 		return put_user(excl, (int __user *)p);
269484fd7bdfSCyrill Gorcunov 	}
269596fd7ce5SGreg Kroah-Hartman 	case TIOCNOTTY:
269696fd7ce5SGreg Kroah-Hartman 		if (current->signal->tty != tty)
269796fd7ce5SGreg Kroah-Hartman 			return -ENOTTY;
269896fd7ce5SGreg Kroah-Hartman 		no_tty();
269996fd7ce5SGreg Kroah-Hartman 		return 0;
270096fd7ce5SGreg Kroah-Hartman 	case TIOCSCTTY:
270196fd7ce5SGreg Kroah-Hartman 		return tiocsctty(tty, arg);
270296fd7ce5SGreg Kroah-Hartman 	case TIOCGPGRP:
270396fd7ce5SGreg Kroah-Hartman 		return tiocgpgrp(tty, real_tty, p);
270496fd7ce5SGreg Kroah-Hartman 	case TIOCSPGRP:
270596fd7ce5SGreg Kroah-Hartman 		return tiocspgrp(tty, real_tty, p);
270696fd7ce5SGreg Kroah-Hartman 	case TIOCGSID:
270796fd7ce5SGreg Kroah-Hartman 		return tiocgsid(tty, real_tty, p);
270896fd7ce5SGreg Kroah-Hartman 	case TIOCGETD:
270996fd7ce5SGreg Kroah-Hartman 		return put_user(tty->ldisc->ops->num, (int __user *)p);
271096fd7ce5SGreg Kroah-Hartman 	case TIOCSETD:
271196fd7ce5SGreg Kroah-Hartman 		return tiocsetd(tty, p);
27123c95c985SKay Sievers 	case TIOCVHANGUP:
27133c95c985SKay Sievers 		if (!capable(CAP_SYS_ADMIN))
27143c95c985SKay Sievers 			return -EPERM;
27153c95c985SKay Sievers 		tty_vhangup(tty);
27163c95c985SKay Sievers 		return 0;
2717b7b8de08SWerner Fink 	case TIOCGDEV:
2718b7b8de08SWerner Fink 	{
2719b7b8de08SWerner Fink 		unsigned int ret = new_encode_dev(tty_devnum(real_tty));
2720b7b8de08SWerner Fink 		return put_user(ret, (unsigned int __user *)p);
2721b7b8de08SWerner Fink 	}
272296fd7ce5SGreg Kroah-Hartman 	/*
272396fd7ce5SGreg Kroah-Hartman 	 * Break handling
272496fd7ce5SGreg Kroah-Hartman 	 */
272596fd7ce5SGreg Kroah-Hartman 	case TIOCSBRK:	/* Turn break on, unconditionally */
272696fd7ce5SGreg Kroah-Hartman 		if (tty->ops->break_ctl)
272796fd7ce5SGreg Kroah-Hartman 			return tty->ops->break_ctl(tty, -1);
272896fd7ce5SGreg Kroah-Hartman 		return 0;
272996fd7ce5SGreg Kroah-Hartman 	case TIOCCBRK:	/* Turn break off, unconditionally */
273096fd7ce5SGreg Kroah-Hartman 		if (tty->ops->break_ctl)
273196fd7ce5SGreg Kroah-Hartman 			return tty->ops->break_ctl(tty, 0);
273296fd7ce5SGreg Kroah-Hartman 		return 0;
273396fd7ce5SGreg Kroah-Hartman 	case TCSBRK:   /* SVID version: non-zero arg --> no break */
273496fd7ce5SGreg Kroah-Hartman 		/* non-zero arg means wait for all output data
273596fd7ce5SGreg Kroah-Hartman 		 * to be sent (performed above) but don't send break.
273696fd7ce5SGreg Kroah-Hartman 		 * This is used by the tcdrain() termios function.
273796fd7ce5SGreg Kroah-Hartman 		 */
273896fd7ce5SGreg Kroah-Hartman 		if (!arg)
273996fd7ce5SGreg Kroah-Hartman 			return send_break(tty, 250);
274096fd7ce5SGreg Kroah-Hartman 		return 0;
274196fd7ce5SGreg Kroah-Hartman 	case TCSBRKP:	/* support for POSIX tcsendbreak() */
274296fd7ce5SGreg Kroah-Hartman 		return send_break(tty, arg ? arg*100 : 250);
274396fd7ce5SGreg Kroah-Hartman 
274496fd7ce5SGreg Kroah-Hartman 	case TIOCMGET:
274560b33c13SAlan Cox 		return tty_tiocmget(tty, p);
274696fd7ce5SGreg Kroah-Hartman 	case TIOCMSET:
274796fd7ce5SGreg Kroah-Hartman 	case TIOCMBIC:
274896fd7ce5SGreg Kroah-Hartman 	case TIOCMBIS:
274920b9d177SAlan Cox 		return tty_tiocmset(tty, cmd, p);
275096fd7ce5SGreg Kroah-Hartman 	case TIOCGICOUNT:
275196fd7ce5SGreg Kroah-Hartman 		retval = tty_tiocgicount(tty, p);
275296fd7ce5SGreg Kroah-Hartman 		/* For the moment allow fall through to the old method */
275396fd7ce5SGreg Kroah-Hartman         	if (retval != -EINVAL)
275496fd7ce5SGreg Kroah-Hartman 			return retval;
275596fd7ce5SGreg Kroah-Hartman 		break;
275696fd7ce5SGreg Kroah-Hartman 	case TCFLSH:
275796fd7ce5SGreg Kroah-Hartman 		switch (arg) {
275896fd7ce5SGreg Kroah-Hartman 		case TCIFLUSH:
275996fd7ce5SGreg Kroah-Hartman 		case TCIOFLUSH:
276096fd7ce5SGreg Kroah-Hartman 		/* flush tty buffer and allow ldisc to process ioctl */
276196fd7ce5SGreg Kroah-Hartman 			tty_buffer_flush(tty);
276296fd7ce5SGreg Kroah-Hartman 			break;
276396fd7ce5SGreg Kroah-Hartman 		}
276496fd7ce5SGreg Kroah-Hartman 		break;
276596fd7ce5SGreg Kroah-Hartman 	}
276696fd7ce5SGreg Kroah-Hartman 	if (tty->ops->ioctl) {
27676caa76b7SAlan Cox 		retval = (tty->ops->ioctl)(tty, cmd, arg);
276896fd7ce5SGreg Kroah-Hartman 		if (retval != -ENOIOCTLCMD)
276996fd7ce5SGreg Kroah-Hartman 			return retval;
277096fd7ce5SGreg Kroah-Hartman 	}
277196fd7ce5SGreg Kroah-Hartman 	ld = tty_ldisc_ref_wait(tty);
277296fd7ce5SGreg Kroah-Hartman 	retval = -EINVAL;
277396fd7ce5SGreg Kroah-Hartman 	if (ld->ops->ioctl) {
277496fd7ce5SGreg Kroah-Hartman 		retval = ld->ops->ioctl(tty, file, cmd, arg);
277596fd7ce5SGreg Kroah-Hartman 		if (retval == -ENOIOCTLCMD)
2776bbb63c51SWanlong Gao 			retval = -ENOTTY;
277796fd7ce5SGreg Kroah-Hartman 	}
277896fd7ce5SGreg Kroah-Hartman 	tty_ldisc_deref(ld);
277996fd7ce5SGreg Kroah-Hartman 	return retval;
278096fd7ce5SGreg Kroah-Hartman }
278196fd7ce5SGreg Kroah-Hartman 
278296fd7ce5SGreg Kroah-Hartman #ifdef CONFIG_COMPAT
278396fd7ce5SGreg Kroah-Hartman static long tty_compat_ioctl(struct file *file, unsigned int cmd,
278496fd7ce5SGreg Kroah-Hartman 				unsigned long arg)
278596fd7ce5SGreg Kroah-Hartman {
278696fd7ce5SGreg Kroah-Hartman 	struct inode *inode = file->f_dentry->d_inode;
278796fd7ce5SGreg Kroah-Hartman 	struct tty_struct *tty = file_tty(file);
278896fd7ce5SGreg Kroah-Hartman 	struct tty_ldisc *ld;
278996fd7ce5SGreg Kroah-Hartman 	int retval = -ENOIOCTLCMD;
279096fd7ce5SGreg Kroah-Hartman 
279196fd7ce5SGreg Kroah-Hartman 	if (tty_paranoia_check(tty, inode, "tty_ioctl"))
279296fd7ce5SGreg Kroah-Hartman 		return -EINVAL;
279396fd7ce5SGreg Kroah-Hartman 
279496fd7ce5SGreg Kroah-Hartman 	if (tty->ops->compat_ioctl) {
27956caa76b7SAlan Cox 		retval = (tty->ops->compat_ioctl)(tty, cmd, arg);
279696fd7ce5SGreg Kroah-Hartman 		if (retval != -ENOIOCTLCMD)
279796fd7ce5SGreg Kroah-Hartman 			return retval;
279896fd7ce5SGreg Kroah-Hartman 	}
279996fd7ce5SGreg Kroah-Hartman 
280096fd7ce5SGreg Kroah-Hartman 	ld = tty_ldisc_ref_wait(tty);
280196fd7ce5SGreg Kroah-Hartman 	if (ld->ops->compat_ioctl)
280296fd7ce5SGreg Kroah-Hartman 		retval = ld->ops->compat_ioctl(tty, file, cmd, arg);
28038193c429SThomas Meyer 	else
28048193c429SThomas Meyer 		retval = n_tty_compat_ioctl_helper(tty, file, cmd, arg);
280596fd7ce5SGreg Kroah-Hartman 	tty_ldisc_deref(ld);
280696fd7ce5SGreg Kroah-Hartman 
280796fd7ce5SGreg Kroah-Hartman 	return retval;
280896fd7ce5SGreg Kroah-Hartman }
280996fd7ce5SGreg Kroah-Hartman #endif
281096fd7ce5SGreg Kroah-Hartman 
2811c3c073f8SAl Viro static int this_tty(const void *t, struct file *file, unsigned fd)
2812c3c073f8SAl Viro {
2813c3c073f8SAl Viro 	if (likely(file->f_op->read != tty_read))
2814c3c073f8SAl Viro 		return 0;
2815c3c073f8SAl Viro 	return file_tty(file) != t ? 0 : fd + 1;
2816c3c073f8SAl Viro }
2817c3c073f8SAl Viro 
281896fd7ce5SGreg Kroah-Hartman /*
281996fd7ce5SGreg Kroah-Hartman  * This implements the "Secure Attention Key" ---  the idea is to
282096fd7ce5SGreg Kroah-Hartman  * prevent trojan horses by killing all processes associated with this
282196fd7ce5SGreg Kroah-Hartman  * tty when the user hits the "Secure Attention Key".  Required for
282296fd7ce5SGreg Kroah-Hartman  * super-paranoid applications --- see the Orange Book for more details.
282396fd7ce5SGreg Kroah-Hartman  *
282496fd7ce5SGreg Kroah-Hartman  * This code could be nicer; ideally it should send a HUP, wait a few
282596fd7ce5SGreg Kroah-Hartman  * seconds, then send a INT, and then a KILL signal.  But you then
282696fd7ce5SGreg Kroah-Hartman  * have to coordinate with the init process, since all processes associated
282796fd7ce5SGreg Kroah-Hartman  * with the current tty must be dead before the new getty is allowed
282896fd7ce5SGreg Kroah-Hartman  * to spawn.
282996fd7ce5SGreg Kroah-Hartman  *
283096fd7ce5SGreg Kroah-Hartman  * Now, if it would be correct ;-/ The current code has a nasty hole -
283196fd7ce5SGreg Kroah-Hartman  * it doesn't catch files in flight. We may send the descriptor to ourselves
283296fd7ce5SGreg Kroah-Hartman  * via AF_UNIX socket, close it and later fetch from socket. FIXME.
283396fd7ce5SGreg Kroah-Hartman  *
283496fd7ce5SGreg Kroah-Hartman  * Nasty bug: do_SAK is being called in interrupt context.  This can
283596fd7ce5SGreg Kroah-Hartman  * deadlock.  We punt it up to process context.  AKPM - 16Mar2001
283696fd7ce5SGreg Kroah-Hartman  */
283796fd7ce5SGreg Kroah-Hartman void __do_SAK(struct tty_struct *tty)
283896fd7ce5SGreg Kroah-Hartman {
283996fd7ce5SGreg Kroah-Hartman #ifdef TTY_SOFT_SAK
284096fd7ce5SGreg Kroah-Hartman 	tty_hangup(tty);
284196fd7ce5SGreg Kroah-Hartman #else
284296fd7ce5SGreg Kroah-Hartman 	struct task_struct *g, *p;
284396fd7ce5SGreg Kroah-Hartman 	struct pid *session;
284496fd7ce5SGreg Kroah-Hartman 	int		i;
284596fd7ce5SGreg Kroah-Hartman 
284696fd7ce5SGreg Kroah-Hartman 	if (!tty)
284796fd7ce5SGreg Kroah-Hartman 		return;
284896fd7ce5SGreg Kroah-Hartman 	session = tty->session;
284996fd7ce5SGreg Kroah-Hartman 
285096fd7ce5SGreg Kroah-Hartman 	tty_ldisc_flush(tty);
285196fd7ce5SGreg Kroah-Hartman 
285296fd7ce5SGreg Kroah-Hartman 	tty_driver_flush_buffer(tty);
285396fd7ce5SGreg Kroah-Hartman 
285496fd7ce5SGreg Kroah-Hartman 	read_lock(&tasklist_lock);
285596fd7ce5SGreg Kroah-Hartman 	/* Kill the entire session */
285696fd7ce5SGreg Kroah-Hartman 	do_each_pid_task(session, PIDTYPE_SID, p) {
285796fd7ce5SGreg Kroah-Hartman 		printk(KERN_NOTICE "SAK: killed process %d"
285896fd7ce5SGreg Kroah-Hartman 			" (%s): task_session(p)==tty->session\n",
285996fd7ce5SGreg Kroah-Hartman 			task_pid_nr(p), p->comm);
286096fd7ce5SGreg Kroah-Hartman 		send_sig(SIGKILL, p, 1);
286196fd7ce5SGreg Kroah-Hartman 	} while_each_pid_task(session, PIDTYPE_SID, p);
286296fd7ce5SGreg Kroah-Hartman 	/* Now kill any processes that happen to have the
286396fd7ce5SGreg Kroah-Hartman 	 * tty open.
286496fd7ce5SGreg Kroah-Hartman 	 */
286596fd7ce5SGreg Kroah-Hartman 	do_each_thread(g, p) {
286696fd7ce5SGreg Kroah-Hartman 		if (p->signal->tty == tty) {
286796fd7ce5SGreg Kroah-Hartman 			printk(KERN_NOTICE "SAK: killed process %d"
286896fd7ce5SGreg Kroah-Hartman 			    " (%s): task_session(p)==tty->session\n",
286996fd7ce5SGreg Kroah-Hartman 			    task_pid_nr(p), p->comm);
287096fd7ce5SGreg Kroah-Hartman 			send_sig(SIGKILL, p, 1);
287196fd7ce5SGreg Kroah-Hartman 			continue;
287296fd7ce5SGreg Kroah-Hartman 		}
287396fd7ce5SGreg Kroah-Hartman 		task_lock(p);
2874c3c073f8SAl Viro 		i = iterate_fd(p->files, 0, this_tty, tty);
2875c3c073f8SAl Viro 		if (i != 0) {
287696fd7ce5SGreg Kroah-Hartman 			printk(KERN_NOTICE "SAK: killed process %d"
287796fd7ce5SGreg Kroah-Hartman 			    " (%s): fd#%d opened to the tty\n",
2878c3c073f8SAl Viro 				    task_pid_nr(p), p->comm, i - 1);
287996fd7ce5SGreg Kroah-Hartman 			force_sig(SIGKILL, p);
288096fd7ce5SGreg Kroah-Hartman 		}
288196fd7ce5SGreg Kroah-Hartman 		task_unlock(p);
288296fd7ce5SGreg Kroah-Hartman 	} while_each_thread(g, p);
288396fd7ce5SGreg Kroah-Hartman 	read_unlock(&tasklist_lock);
288496fd7ce5SGreg Kroah-Hartman #endif
288596fd7ce5SGreg Kroah-Hartman }
288696fd7ce5SGreg Kroah-Hartman 
288796fd7ce5SGreg Kroah-Hartman static void do_SAK_work(struct work_struct *work)
288896fd7ce5SGreg Kroah-Hartman {
288996fd7ce5SGreg Kroah-Hartman 	struct tty_struct *tty =
289096fd7ce5SGreg Kroah-Hartman 		container_of(work, struct tty_struct, SAK_work);
289196fd7ce5SGreg Kroah-Hartman 	__do_SAK(tty);
289296fd7ce5SGreg Kroah-Hartman }
289396fd7ce5SGreg Kroah-Hartman 
289496fd7ce5SGreg Kroah-Hartman /*
289596fd7ce5SGreg Kroah-Hartman  * The tq handling here is a little racy - tty->SAK_work may already be queued.
289696fd7ce5SGreg Kroah-Hartman  * Fortunately we don't need to worry, because if ->SAK_work is already queued,
289796fd7ce5SGreg Kroah-Hartman  * the values which we write to it will be identical to the values which it
289896fd7ce5SGreg Kroah-Hartman  * already has. --akpm
289996fd7ce5SGreg Kroah-Hartman  */
290096fd7ce5SGreg Kroah-Hartman void do_SAK(struct tty_struct *tty)
290196fd7ce5SGreg Kroah-Hartman {
290296fd7ce5SGreg Kroah-Hartman 	if (!tty)
290396fd7ce5SGreg Kroah-Hartman 		return;
290496fd7ce5SGreg Kroah-Hartman 	schedule_work(&tty->SAK_work);
290596fd7ce5SGreg Kroah-Hartman }
290696fd7ce5SGreg Kroah-Hartman 
290796fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(do_SAK);
290896fd7ce5SGreg Kroah-Hartman 
29096e9430acSGreg Kroah-Hartman static int dev_match_devt(struct device *dev, const void *data)
291096fd7ce5SGreg Kroah-Hartman {
29116e9430acSGreg Kroah-Hartman 	const dev_t *devt = data;
291296fd7ce5SGreg Kroah-Hartman 	return dev->devt == *devt;
291396fd7ce5SGreg Kroah-Hartman }
291496fd7ce5SGreg Kroah-Hartman 
291596fd7ce5SGreg Kroah-Hartman /* Must put_device() after it's unused! */
291696fd7ce5SGreg Kroah-Hartman static struct device *tty_get_device(struct tty_struct *tty)
291796fd7ce5SGreg Kroah-Hartman {
291896fd7ce5SGreg Kroah-Hartman 	dev_t devt = tty_devnum(tty);
291996fd7ce5SGreg Kroah-Hartman 	return class_find_device(tty_class, NULL, &devt, dev_match_devt);
292096fd7ce5SGreg Kroah-Hartman }
292196fd7ce5SGreg Kroah-Hartman 
292296fd7ce5SGreg Kroah-Hartman 
292396fd7ce5SGreg Kroah-Hartman /**
292496fd7ce5SGreg Kroah-Hartman  *	initialize_tty_struct
292596fd7ce5SGreg Kroah-Hartman  *	@tty: tty to initialize
292696fd7ce5SGreg Kroah-Hartman  *
292796fd7ce5SGreg Kroah-Hartman  *	This subroutine initializes a tty structure that has been newly
292896fd7ce5SGreg Kroah-Hartman  *	allocated.
292996fd7ce5SGreg Kroah-Hartman  *
293096fd7ce5SGreg Kroah-Hartman  *	Locking: none - tty in question must not be exposed at this point
293196fd7ce5SGreg Kroah-Hartman  */
293296fd7ce5SGreg Kroah-Hartman 
293396fd7ce5SGreg Kroah-Hartman void initialize_tty_struct(struct tty_struct *tty,
293496fd7ce5SGreg Kroah-Hartman 		struct tty_driver *driver, int idx)
293596fd7ce5SGreg Kroah-Hartman {
293696fd7ce5SGreg Kroah-Hartman 	memset(tty, 0, sizeof(struct tty_struct));
293796fd7ce5SGreg Kroah-Hartman 	kref_init(&tty->kref);
293896fd7ce5SGreg Kroah-Hartman 	tty->magic = TTY_MAGIC;
293996fd7ce5SGreg Kroah-Hartman 	tty_ldisc_init(tty);
294096fd7ce5SGreg Kroah-Hartman 	tty->session = NULL;
294196fd7ce5SGreg Kroah-Hartman 	tty->pgrp = NULL;
294289c8d91eSAlan Cox 	mutex_init(&tty->legacy_mutex);
294396fd7ce5SGreg Kroah-Hartman 	mutex_init(&tty->termios_mutex);
294496fd7ce5SGreg Kroah-Hartman 	mutex_init(&tty->ldisc_mutex);
294596fd7ce5SGreg Kroah-Hartman 	init_waitqueue_head(&tty->write_wait);
294696fd7ce5SGreg Kroah-Hartman 	init_waitqueue_head(&tty->read_wait);
294796fd7ce5SGreg Kroah-Hartman 	INIT_WORK(&tty->hangup_work, do_tty_hangup);
294896fd7ce5SGreg Kroah-Hartman 	mutex_init(&tty->atomic_write_lock);
294996fd7ce5SGreg Kroah-Hartman 	spin_lock_init(&tty->ctrl_lock);
295096fd7ce5SGreg Kroah-Hartman 	INIT_LIST_HEAD(&tty->tty_files);
295196fd7ce5SGreg Kroah-Hartman 	INIT_WORK(&tty->SAK_work, do_SAK_work);
295296fd7ce5SGreg Kroah-Hartman 
295396fd7ce5SGreg Kroah-Hartman 	tty->driver = driver;
295496fd7ce5SGreg Kroah-Hartman 	tty->ops = driver->ops;
295596fd7ce5SGreg Kroah-Hartman 	tty->index = idx;
295696fd7ce5SGreg Kroah-Hartman 	tty_line_name(driver, idx, tty->name);
295796fd7ce5SGreg Kroah-Hartman 	tty->dev = tty_get_device(tty);
295896fd7ce5SGreg Kroah-Hartman }
295996fd7ce5SGreg Kroah-Hartman 
296096fd7ce5SGreg Kroah-Hartman /**
29616716671dSJiri Slaby  *	deinitialize_tty_struct
29626716671dSJiri Slaby  *	@tty: tty to deinitialize
29636716671dSJiri Slaby  *
29646716671dSJiri Slaby  *	This subroutine deinitializes a tty structure that has been newly
29656716671dSJiri Slaby  *	allocated but tty_release cannot be called on that yet.
29666716671dSJiri Slaby  *
29676716671dSJiri Slaby  *	Locking: none - tty in question must not be exposed at this point
29686716671dSJiri Slaby  */
29696716671dSJiri Slaby void deinitialize_tty_struct(struct tty_struct *tty)
29706716671dSJiri Slaby {
29716716671dSJiri Slaby 	tty_ldisc_deinit(tty);
29726716671dSJiri Slaby }
29736716671dSJiri Slaby 
29746716671dSJiri Slaby /**
297596fd7ce5SGreg Kroah-Hartman  *	tty_put_char	-	write one character to a tty
297696fd7ce5SGreg Kroah-Hartman  *	@tty: tty
297796fd7ce5SGreg Kroah-Hartman  *	@ch: character
297896fd7ce5SGreg Kroah-Hartman  *
297996fd7ce5SGreg Kroah-Hartman  *	Write one byte to the tty using the provided put_char method
298096fd7ce5SGreg Kroah-Hartman  *	if present. Returns the number of characters successfully output.
298196fd7ce5SGreg Kroah-Hartman  *
298296fd7ce5SGreg Kroah-Hartman  *	Note: the specific put_char operation in the driver layer may go
298396fd7ce5SGreg Kroah-Hartman  *	away soon. Don't call it directly, use this method
298496fd7ce5SGreg Kroah-Hartman  */
298596fd7ce5SGreg Kroah-Hartman 
298696fd7ce5SGreg Kroah-Hartman int tty_put_char(struct tty_struct *tty, unsigned char ch)
298796fd7ce5SGreg Kroah-Hartman {
298896fd7ce5SGreg Kroah-Hartman 	if (tty->ops->put_char)
298996fd7ce5SGreg Kroah-Hartman 		return tty->ops->put_char(tty, ch);
299096fd7ce5SGreg Kroah-Hartman 	return tty->ops->write(tty, &ch, 1);
299196fd7ce5SGreg Kroah-Hartman }
299296fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(tty_put_char);
299396fd7ce5SGreg Kroah-Hartman 
299496fd7ce5SGreg Kroah-Hartman struct class *tty_class;
299596fd7ce5SGreg Kroah-Hartman 
29967e73eca6SJiri Slaby static int tty_cdev_add(struct tty_driver *driver, dev_t dev,
29977e73eca6SJiri Slaby 		unsigned int index, unsigned int count)
29987e73eca6SJiri Slaby {
29997e73eca6SJiri Slaby 	/* init here, since reused cdevs cause crashes */
30007e73eca6SJiri Slaby 	cdev_init(&driver->cdevs[index], &tty_fops);
30017e73eca6SJiri Slaby 	driver->cdevs[index].owner = driver->owner;
30027e73eca6SJiri Slaby 	return cdev_add(&driver->cdevs[index], dev, count);
30037e73eca6SJiri Slaby }
30047e73eca6SJiri Slaby 
300596fd7ce5SGreg Kroah-Hartman /**
300696fd7ce5SGreg Kroah-Hartman  *	tty_register_device - register a tty device
300796fd7ce5SGreg Kroah-Hartman  *	@driver: the tty driver that describes the tty device
300896fd7ce5SGreg Kroah-Hartman  *	@index: the index in the tty driver for this tty device
300996fd7ce5SGreg Kroah-Hartman  *	@device: a struct device that is associated with this tty device.
301096fd7ce5SGreg Kroah-Hartman  *		This field is optional, if there is no known struct device
301196fd7ce5SGreg Kroah-Hartman  *		for this tty device it can be set to NULL safely.
301296fd7ce5SGreg Kroah-Hartman  *
301396fd7ce5SGreg Kroah-Hartman  *	Returns a pointer to the struct device for this tty device
301496fd7ce5SGreg Kroah-Hartman  *	(or ERR_PTR(-EFOO) on error).
301596fd7ce5SGreg Kroah-Hartman  *
301696fd7ce5SGreg Kroah-Hartman  *	This call is required to be made to register an individual tty device
301796fd7ce5SGreg Kroah-Hartman  *	if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set.  If
301896fd7ce5SGreg Kroah-Hartman  *	that bit is not set, this function should not be called by a tty
301996fd7ce5SGreg Kroah-Hartman  *	driver.
302096fd7ce5SGreg Kroah-Hartman  *
302196fd7ce5SGreg Kroah-Hartman  *	Locking: ??
302296fd7ce5SGreg Kroah-Hartman  */
302396fd7ce5SGreg Kroah-Hartman 
302496fd7ce5SGreg Kroah-Hartman struct device *tty_register_device(struct tty_driver *driver, unsigned index,
302596fd7ce5SGreg Kroah-Hartman 				   struct device *device)
302696fd7ce5SGreg Kroah-Hartman {
30276915c0e4STomas Hlavacek 	return tty_register_device_attr(driver, index, device, NULL, NULL);
30286915c0e4STomas Hlavacek }
30296915c0e4STomas Hlavacek EXPORT_SYMBOL(tty_register_device);
30306915c0e4STomas Hlavacek 
3031b1b79916STomas Hlavacek static void tty_device_create_release(struct device *dev)
3032b1b79916STomas Hlavacek {
3033b1b79916STomas Hlavacek 	pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
3034b1b79916STomas Hlavacek 	kfree(dev);
3035b1b79916STomas Hlavacek }
3036b1b79916STomas Hlavacek 
30376915c0e4STomas Hlavacek /**
30386915c0e4STomas Hlavacek  *	tty_register_device_attr - register a tty device
30396915c0e4STomas Hlavacek  *	@driver: the tty driver that describes the tty device
30406915c0e4STomas Hlavacek  *	@index: the index in the tty driver for this tty device
30416915c0e4STomas Hlavacek  *	@device: a struct device that is associated with this tty device.
30426915c0e4STomas Hlavacek  *		This field is optional, if there is no known struct device
30436915c0e4STomas Hlavacek  *		for this tty device it can be set to NULL safely.
30446915c0e4STomas Hlavacek  *	@drvdata: Driver data to be set to device.
30456915c0e4STomas Hlavacek  *	@attr_grp: Attribute group to be set on device.
30466915c0e4STomas Hlavacek  *
30476915c0e4STomas Hlavacek  *	Returns a pointer to the struct device for this tty device
30486915c0e4STomas Hlavacek  *	(or ERR_PTR(-EFOO) on error).
30496915c0e4STomas Hlavacek  *
30506915c0e4STomas Hlavacek  *	This call is required to be made to register an individual tty device
30516915c0e4STomas Hlavacek  *	if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set.  If
30526915c0e4STomas Hlavacek  *	that bit is not set, this function should not be called by a tty
30536915c0e4STomas Hlavacek  *	driver.
30546915c0e4STomas Hlavacek  *
30556915c0e4STomas Hlavacek  *	Locking: ??
30566915c0e4STomas Hlavacek  */
30576915c0e4STomas Hlavacek struct device *tty_register_device_attr(struct tty_driver *driver,
30586915c0e4STomas Hlavacek 				   unsigned index, struct device *device,
30596915c0e4STomas Hlavacek 				   void *drvdata,
30606915c0e4STomas Hlavacek 				   const struct attribute_group **attr_grp)
30616915c0e4STomas Hlavacek {
306296fd7ce5SGreg Kroah-Hartman 	char name[64];
30636915c0e4STomas Hlavacek 	dev_t devt = MKDEV(driver->major, driver->minor_start) + index;
30646915c0e4STomas Hlavacek 	struct device *dev = NULL;
30656915c0e4STomas Hlavacek 	int retval = -ENODEV;
30667e73eca6SJiri Slaby 	bool cdev = false;
306796fd7ce5SGreg Kroah-Hartman 
306896fd7ce5SGreg Kroah-Hartman 	if (index >= driver->num) {
306996fd7ce5SGreg Kroah-Hartman 		printk(KERN_ERR "Attempt to register invalid tty line number "
307096fd7ce5SGreg Kroah-Hartman 		       " (%d).\n", index);
307196fd7ce5SGreg Kroah-Hartman 		return ERR_PTR(-EINVAL);
307296fd7ce5SGreg Kroah-Hartman 	}
307396fd7ce5SGreg Kroah-Hartman 
307496fd7ce5SGreg Kroah-Hartman 	if (driver->type == TTY_DRIVER_TYPE_PTY)
307596fd7ce5SGreg Kroah-Hartman 		pty_line_name(driver, index, name);
307696fd7ce5SGreg Kroah-Hartman 	else
307796fd7ce5SGreg Kroah-Hartman 		tty_line_name(driver, index, name);
307896fd7ce5SGreg Kroah-Hartman 
30797e73eca6SJiri Slaby 	if (!(driver->flags & TTY_DRIVER_DYNAMIC_ALLOC)) {
30806915c0e4STomas Hlavacek 		retval = tty_cdev_add(driver, devt, index, 1);
30816915c0e4STomas Hlavacek 		if (retval)
30826915c0e4STomas Hlavacek 			goto error;
30837e73eca6SJiri Slaby 		cdev = true;
30847e73eca6SJiri Slaby 	}
30857e73eca6SJiri Slaby 
30866915c0e4STomas Hlavacek 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
30876915c0e4STomas Hlavacek 	if (!dev) {
30886915c0e4STomas Hlavacek 		retval = -ENOMEM;
30896915c0e4STomas Hlavacek 		goto error;
309096fd7ce5SGreg Kroah-Hartman 	}
30916915c0e4STomas Hlavacek 
30926915c0e4STomas Hlavacek 	dev->devt = devt;
30936915c0e4STomas Hlavacek 	dev->class = tty_class;
30946915c0e4STomas Hlavacek 	dev->parent = device;
3095b1b79916STomas Hlavacek 	dev->release = tty_device_create_release;
30966915c0e4STomas Hlavacek 	dev_set_name(dev, "%s", name);
30976915c0e4STomas Hlavacek 	dev->groups = attr_grp;
30986915c0e4STomas Hlavacek 	dev_set_drvdata(dev, drvdata);
30996915c0e4STomas Hlavacek 
31006915c0e4STomas Hlavacek 	retval = device_register(dev);
31016915c0e4STomas Hlavacek 	if (retval)
31026915c0e4STomas Hlavacek 		goto error;
31036915c0e4STomas Hlavacek 
31046915c0e4STomas Hlavacek 	return dev;
31056915c0e4STomas Hlavacek 
31066915c0e4STomas Hlavacek error:
31076915c0e4STomas Hlavacek 	put_device(dev);
31086915c0e4STomas Hlavacek 	if (cdev)
31096915c0e4STomas Hlavacek 		cdev_del(&driver->cdevs[index]);
31106915c0e4STomas Hlavacek 	return ERR_PTR(retval);
31116915c0e4STomas Hlavacek }
31126915c0e4STomas Hlavacek EXPORT_SYMBOL_GPL(tty_register_device_attr);
311396fd7ce5SGreg Kroah-Hartman 
311496fd7ce5SGreg Kroah-Hartman /**
311596fd7ce5SGreg Kroah-Hartman  * 	tty_unregister_device - unregister a tty device
311696fd7ce5SGreg Kroah-Hartman  * 	@driver: the tty driver that describes the tty device
311796fd7ce5SGreg Kroah-Hartman  * 	@index: the index in the tty driver for this tty device
311896fd7ce5SGreg Kroah-Hartman  *
311996fd7ce5SGreg Kroah-Hartman  * 	If a tty device is registered with a call to tty_register_device() then
312096fd7ce5SGreg Kroah-Hartman  *	this function must be called when the tty device is gone.
312196fd7ce5SGreg Kroah-Hartman  *
312296fd7ce5SGreg Kroah-Hartman  *	Locking: ??
312396fd7ce5SGreg Kroah-Hartman  */
312496fd7ce5SGreg Kroah-Hartman 
312596fd7ce5SGreg Kroah-Hartman void tty_unregister_device(struct tty_driver *driver, unsigned index)
312696fd7ce5SGreg Kroah-Hartman {
312796fd7ce5SGreg Kroah-Hartman 	device_destroy(tty_class,
312896fd7ce5SGreg Kroah-Hartman 		MKDEV(driver->major, driver->minor_start) + index);
31297e73eca6SJiri Slaby 	if (!(driver->flags & TTY_DRIVER_DYNAMIC_ALLOC))
31307e73eca6SJiri Slaby 		cdev_del(&driver->cdevs[index]);
313196fd7ce5SGreg Kroah-Hartman }
313296fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_unregister_device);
313396fd7ce5SGreg Kroah-Hartman 
31347f0bc6a6SJiri Slaby /**
31357f0bc6a6SJiri Slaby  * __tty_alloc_driver -- allocate tty driver
31367f0bc6a6SJiri Slaby  * @lines: count of lines this driver can handle at most
31377f0bc6a6SJiri Slaby  * @owner: module which is repsonsible for this driver
31387f0bc6a6SJiri Slaby  * @flags: some of TTY_DRIVER_* flags, will be set in driver->flags
31397f0bc6a6SJiri Slaby  *
31407f0bc6a6SJiri Slaby  * This should not be called directly, some of the provided macros should be
31417f0bc6a6SJiri Slaby  * used instead. Use IS_ERR and friends on @retval.
31427f0bc6a6SJiri Slaby  */
31437f0bc6a6SJiri Slaby struct tty_driver *__tty_alloc_driver(unsigned int lines, struct module *owner,
31447f0bc6a6SJiri Slaby 		unsigned long flags)
314596fd7ce5SGreg Kroah-Hartman {
314696fd7ce5SGreg Kroah-Hartman 	struct tty_driver *driver;
31477e73eca6SJiri Slaby 	unsigned int cdevs = 1;
314816a02081SJiri Slaby 	int err;
314996fd7ce5SGreg Kroah-Hartman 
31500019b408SJiri Slaby 	if (!lines || (flags & TTY_DRIVER_UNNUMBERED_NODE && lines > 1))
31517f0bc6a6SJiri Slaby 		return ERR_PTR(-EINVAL);
31527f0bc6a6SJiri Slaby 
315396fd7ce5SGreg Kroah-Hartman 	driver = kzalloc(sizeof(struct tty_driver), GFP_KERNEL);
31547f0bc6a6SJiri Slaby 	if (!driver)
31557f0bc6a6SJiri Slaby 		return ERR_PTR(-ENOMEM);
31567f0bc6a6SJiri Slaby 
315796fd7ce5SGreg Kroah-Hartman 	kref_init(&driver->kref);
315896fd7ce5SGreg Kroah-Hartman 	driver->magic = TTY_DRIVER_MAGIC;
315996fd7ce5SGreg Kroah-Hartman 	driver->num = lines;
31601a54a76dSJiri Slaby 	driver->owner = owner;
31617f0bc6a6SJiri Slaby 	driver->flags = flags;
316216a02081SJiri Slaby 
316316a02081SJiri Slaby 	if (!(flags & TTY_DRIVER_DEVPTS_MEM)) {
316416a02081SJiri Slaby 		driver->ttys = kcalloc(lines, sizeof(*driver->ttys),
316516a02081SJiri Slaby 				GFP_KERNEL);
316616a02081SJiri Slaby 		driver->termios = kcalloc(lines, sizeof(*driver->termios),
316716a02081SJiri Slaby 				GFP_KERNEL);
316816a02081SJiri Slaby 		if (!driver->ttys || !driver->termios) {
316916a02081SJiri Slaby 			err = -ENOMEM;
317016a02081SJiri Slaby 			goto err_free_all;
317116a02081SJiri Slaby 		}
317216a02081SJiri Slaby 	}
317316a02081SJiri Slaby 
317416a02081SJiri Slaby 	if (!(flags & TTY_DRIVER_DYNAMIC_ALLOC)) {
317516a02081SJiri Slaby 		driver->ports = kcalloc(lines, sizeof(*driver->ports),
317616a02081SJiri Slaby 				GFP_KERNEL);
317716a02081SJiri Slaby 		if (!driver->ports) {
317816a02081SJiri Slaby 			err = -ENOMEM;
317916a02081SJiri Slaby 			goto err_free_all;
318016a02081SJiri Slaby 		}
31817e73eca6SJiri Slaby 		cdevs = lines;
31827e73eca6SJiri Slaby 	}
31837e73eca6SJiri Slaby 
31847e73eca6SJiri Slaby 	driver->cdevs = kcalloc(cdevs, sizeof(*driver->cdevs), GFP_KERNEL);
31857e73eca6SJiri Slaby 	if (!driver->cdevs) {
31867e73eca6SJiri Slaby 		err = -ENOMEM;
31877e73eca6SJiri Slaby 		goto err_free_all;
318816a02081SJiri Slaby 	}
31897f0bc6a6SJiri Slaby 
319096fd7ce5SGreg Kroah-Hartman 	return driver;
319116a02081SJiri Slaby err_free_all:
319216a02081SJiri Slaby 	kfree(driver->ports);
319316a02081SJiri Slaby 	kfree(driver->ttys);
319416a02081SJiri Slaby 	kfree(driver->termios);
319516a02081SJiri Slaby 	kfree(driver);
319616a02081SJiri Slaby 	return ERR_PTR(err);
319796fd7ce5SGreg Kroah-Hartman }
31987f0bc6a6SJiri Slaby EXPORT_SYMBOL(__tty_alloc_driver);
319996fd7ce5SGreg Kroah-Hartman 
320096fd7ce5SGreg Kroah-Hartman static void destruct_tty_driver(struct kref *kref)
320196fd7ce5SGreg Kroah-Hartman {
320296fd7ce5SGreg Kroah-Hartman 	struct tty_driver *driver = container_of(kref, struct tty_driver, kref);
320396fd7ce5SGreg Kroah-Hartman 	int i;
320496fd7ce5SGreg Kroah-Hartman 	struct ktermios *tp;
320596fd7ce5SGreg Kroah-Hartman 
320696fd7ce5SGreg Kroah-Hartman 	if (driver->flags & TTY_DRIVER_INSTALLED) {
320796fd7ce5SGreg Kroah-Hartman 		/*
320896fd7ce5SGreg Kroah-Hartman 		 * Free the termios and termios_locked structures because
320996fd7ce5SGreg Kroah-Hartman 		 * we don't want to get memory leaks when modular tty
321096fd7ce5SGreg Kroah-Hartman 		 * drivers are removed from the kernel.
321196fd7ce5SGreg Kroah-Hartman 		 */
321296fd7ce5SGreg Kroah-Hartman 		for (i = 0; i < driver->num; i++) {
321396fd7ce5SGreg Kroah-Hartman 			tp = driver->termios[i];
321496fd7ce5SGreg Kroah-Hartman 			if (tp) {
321596fd7ce5SGreg Kroah-Hartman 				driver->termios[i] = NULL;
321696fd7ce5SGreg Kroah-Hartman 				kfree(tp);
321796fd7ce5SGreg Kroah-Hartman 			}
321896fd7ce5SGreg Kroah-Hartman 			if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV))
321996fd7ce5SGreg Kroah-Hartman 				tty_unregister_device(driver, i);
322096fd7ce5SGreg Kroah-Hartman 		}
322196fd7ce5SGreg Kroah-Hartman 		proc_tty_unregister_driver(driver);
32227e73eca6SJiri Slaby 		if (driver->flags & TTY_DRIVER_DYNAMIC_ALLOC)
32237e73eca6SJiri Slaby 			cdev_del(&driver->cdevs[0]);
322496fd7ce5SGreg Kroah-Hartman 	}
32257e73eca6SJiri Slaby 	kfree(driver->cdevs);
322604831dc1SJiri Slaby 	kfree(driver->ports);
322716a02081SJiri Slaby 	kfree(driver->termios);
322816a02081SJiri Slaby 	kfree(driver->ttys);
322996fd7ce5SGreg Kroah-Hartman 	kfree(driver);
323096fd7ce5SGreg Kroah-Hartman }
323196fd7ce5SGreg Kroah-Hartman 
323296fd7ce5SGreg Kroah-Hartman void tty_driver_kref_put(struct tty_driver *driver)
323396fd7ce5SGreg Kroah-Hartman {
323496fd7ce5SGreg Kroah-Hartman 	kref_put(&driver->kref, destruct_tty_driver);
323596fd7ce5SGreg Kroah-Hartman }
323696fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_driver_kref_put);
323796fd7ce5SGreg Kroah-Hartman 
323896fd7ce5SGreg Kroah-Hartman void tty_set_operations(struct tty_driver *driver,
323996fd7ce5SGreg Kroah-Hartman 			const struct tty_operations *op)
324096fd7ce5SGreg Kroah-Hartman {
324196fd7ce5SGreg Kroah-Hartman 	driver->ops = op;
324296fd7ce5SGreg Kroah-Hartman };
324396fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_set_operations);
324496fd7ce5SGreg Kroah-Hartman 
324596fd7ce5SGreg Kroah-Hartman void put_tty_driver(struct tty_driver *d)
324696fd7ce5SGreg Kroah-Hartman {
324796fd7ce5SGreg Kroah-Hartman 	tty_driver_kref_put(d);
324896fd7ce5SGreg Kroah-Hartman }
324996fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(put_tty_driver);
325096fd7ce5SGreg Kroah-Hartman 
325196fd7ce5SGreg Kroah-Hartman /*
325296fd7ce5SGreg Kroah-Hartman  * Called by a tty driver to register itself.
325396fd7ce5SGreg Kroah-Hartman  */
325496fd7ce5SGreg Kroah-Hartman int tty_register_driver(struct tty_driver *driver)
325596fd7ce5SGreg Kroah-Hartman {
325696fd7ce5SGreg Kroah-Hartman 	int error;
325796fd7ce5SGreg Kroah-Hartman 	int i;
325896fd7ce5SGreg Kroah-Hartman 	dev_t dev;
325996fd7ce5SGreg Kroah-Hartman 	struct device *d;
326096fd7ce5SGreg Kroah-Hartman 
326196fd7ce5SGreg Kroah-Hartman 	if (!driver->major) {
326296fd7ce5SGreg Kroah-Hartman 		error = alloc_chrdev_region(&dev, driver->minor_start,
326396fd7ce5SGreg Kroah-Hartman 						driver->num, driver->name);
326496fd7ce5SGreg Kroah-Hartman 		if (!error) {
326596fd7ce5SGreg Kroah-Hartman 			driver->major = MAJOR(dev);
326696fd7ce5SGreg Kroah-Hartman 			driver->minor_start = MINOR(dev);
326796fd7ce5SGreg Kroah-Hartman 		}
326896fd7ce5SGreg Kroah-Hartman 	} else {
326996fd7ce5SGreg Kroah-Hartman 		dev = MKDEV(driver->major, driver->minor_start);
327096fd7ce5SGreg Kroah-Hartman 		error = register_chrdev_region(dev, driver->num, driver->name);
327196fd7ce5SGreg Kroah-Hartman 	}
32729bb8a3d4SJiri Slaby 	if (error < 0)
327316a02081SJiri Slaby 		goto err;
327496fd7ce5SGreg Kroah-Hartman 
32757e73eca6SJiri Slaby 	if (driver->flags & TTY_DRIVER_DYNAMIC_ALLOC) {
32767e73eca6SJiri Slaby 		error = tty_cdev_add(driver, dev, 0, driver->num);
32779bb8a3d4SJiri Slaby 		if (error)
32789bb8a3d4SJiri Slaby 			goto err_unreg_char;
32797e73eca6SJiri Slaby 	}
328096fd7ce5SGreg Kroah-Hartman 
328196fd7ce5SGreg Kroah-Hartman 	mutex_lock(&tty_mutex);
328296fd7ce5SGreg Kroah-Hartman 	list_add(&driver->tty_drivers, &tty_drivers);
328396fd7ce5SGreg Kroah-Hartman 	mutex_unlock(&tty_mutex);
328496fd7ce5SGreg Kroah-Hartman 
328596fd7ce5SGreg Kroah-Hartman 	if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV)) {
328696fd7ce5SGreg Kroah-Hartman 		for (i = 0; i < driver->num; i++) {
328796fd7ce5SGreg Kroah-Hartman 			d = tty_register_device(driver, i, NULL);
328896fd7ce5SGreg Kroah-Hartman 			if (IS_ERR(d)) {
328996fd7ce5SGreg Kroah-Hartman 				error = PTR_ERR(d);
329016a02081SJiri Slaby 				goto err_unreg_devs;
329196fd7ce5SGreg Kroah-Hartman 			}
329296fd7ce5SGreg Kroah-Hartman 		}
329396fd7ce5SGreg Kroah-Hartman 	}
329496fd7ce5SGreg Kroah-Hartman 	proc_tty_register_driver(driver);
329596fd7ce5SGreg Kroah-Hartman 	driver->flags |= TTY_DRIVER_INSTALLED;
329696fd7ce5SGreg Kroah-Hartman 	return 0;
329796fd7ce5SGreg Kroah-Hartman 
329816a02081SJiri Slaby err_unreg_devs:
329996fd7ce5SGreg Kroah-Hartman 	for (i--; i >= 0; i--)
330096fd7ce5SGreg Kroah-Hartman 		tty_unregister_device(driver, i);
330196fd7ce5SGreg Kroah-Hartman 
330296fd7ce5SGreg Kroah-Hartman 	mutex_lock(&tty_mutex);
330396fd7ce5SGreg Kroah-Hartman 	list_del(&driver->tty_drivers);
330496fd7ce5SGreg Kroah-Hartman 	mutex_unlock(&tty_mutex);
330596fd7ce5SGreg Kroah-Hartman 
33069bb8a3d4SJiri Slaby err_unreg_char:
330796fd7ce5SGreg Kroah-Hartman 	unregister_chrdev_region(dev, driver->num);
330816a02081SJiri Slaby err:
330996fd7ce5SGreg Kroah-Hartman 	return error;
331096fd7ce5SGreg Kroah-Hartman }
331196fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_register_driver);
331296fd7ce5SGreg Kroah-Hartman 
331396fd7ce5SGreg Kroah-Hartman /*
331496fd7ce5SGreg Kroah-Hartman  * Called by a tty driver to unregister itself.
331596fd7ce5SGreg Kroah-Hartman  */
331696fd7ce5SGreg Kroah-Hartman int tty_unregister_driver(struct tty_driver *driver)
331796fd7ce5SGreg Kroah-Hartman {
331896fd7ce5SGreg Kroah-Hartman #if 0
331996fd7ce5SGreg Kroah-Hartman 	/* FIXME */
332096fd7ce5SGreg Kroah-Hartman 	if (driver->refcount)
332196fd7ce5SGreg Kroah-Hartman 		return -EBUSY;
332296fd7ce5SGreg Kroah-Hartman #endif
332396fd7ce5SGreg Kroah-Hartman 	unregister_chrdev_region(MKDEV(driver->major, driver->minor_start),
332496fd7ce5SGreg Kroah-Hartman 				driver->num);
332596fd7ce5SGreg Kroah-Hartman 	mutex_lock(&tty_mutex);
332696fd7ce5SGreg Kroah-Hartman 	list_del(&driver->tty_drivers);
332796fd7ce5SGreg Kroah-Hartman 	mutex_unlock(&tty_mutex);
332896fd7ce5SGreg Kroah-Hartman 	return 0;
332996fd7ce5SGreg Kroah-Hartman }
333096fd7ce5SGreg Kroah-Hartman 
333196fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_unregister_driver);
333296fd7ce5SGreg Kroah-Hartman 
333396fd7ce5SGreg Kroah-Hartman dev_t tty_devnum(struct tty_struct *tty)
333496fd7ce5SGreg Kroah-Hartman {
333596fd7ce5SGreg Kroah-Hartman 	return MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index;
333696fd7ce5SGreg Kroah-Hartman }
333796fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_devnum);
333896fd7ce5SGreg Kroah-Hartman 
333996fd7ce5SGreg Kroah-Hartman void proc_clear_tty(struct task_struct *p)
334096fd7ce5SGreg Kroah-Hartman {
334196fd7ce5SGreg Kroah-Hartman 	unsigned long flags;
334296fd7ce5SGreg Kroah-Hartman 	struct tty_struct *tty;
334396fd7ce5SGreg Kroah-Hartman 	spin_lock_irqsave(&p->sighand->siglock, flags);
334496fd7ce5SGreg Kroah-Hartman 	tty = p->signal->tty;
334596fd7ce5SGreg Kroah-Hartman 	p->signal->tty = NULL;
334696fd7ce5SGreg Kroah-Hartman 	spin_unlock_irqrestore(&p->sighand->siglock, flags);
334796fd7ce5SGreg Kroah-Hartman 	tty_kref_put(tty);
334896fd7ce5SGreg Kroah-Hartman }
334996fd7ce5SGreg Kroah-Hartman 
335096fd7ce5SGreg Kroah-Hartman /* Called under the sighand lock */
335196fd7ce5SGreg Kroah-Hartman 
335296fd7ce5SGreg Kroah-Hartman static void __proc_set_tty(struct task_struct *tsk, struct tty_struct *tty)
335396fd7ce5SGreg Kroah-Hartman {
335496fd7ce5SGreg Kroah-Hartman 	if (tty) {
335596fd7ce5SGreg Kroah-Hartman 		unsigned long flags;
335696fd7ce5SGreg Kroah-Hartman 		/* We should not have a session or pgrp to put here but.... */
335796fd7ce5SGreg Kroah-Hartman 		spin_lock_irqsave(&tty->ctrl_lock, flags);
335896fd7ce5SGreg Kroah-Hartman 		put_pid(tty->session);
335996fd7ce5SGreg Kroah-Hartman 		put_pid(tty->pgrp);
336096fd7ce5SGreg Kroah-Hartman 		tty->pgrp = get_pid(task_pgrp(tsk));
336196fd7ce5SGreg Kroah-Hartman 		spin_unlock_irqrestore(&tty->ctrl_lock, flags);
336296fd7ce5SGreg Kroah-Hartman 		tty->session = get_pid(task_session(tsk));
336396fd7ce5SGreg Kroah-Hartman 		if (tsk->signal->tty) {
336496fd7ce5SGreg Kroah-Hartman 			printk(KERN_DEBUG "tty not NULL!!\n");
336596fd7ce5SGreg Kroah-Hartman 			tty_kref_put(tsk->signal->tty);
336696fd7ce5SGreg Kroah-Hartman 		}
336796fd7ce5SGreg Kroah-Hartman 	}
336896fd7ce5SGreg Kroah-Hartman 	put_pid(tsk->signal->tty_old_pgrp);
336996fd7ce5SGreg Kroah-Hartman 	tsk->signal->tty = tty_kref_get(tty);
337096fd7ce5SGreg Kroah-Hartman 	tsk->signal->tty_old_pgrp = NULL;
337196fd7ce5SGreg Kroah-Hartman }
337296fd7ce5SGreg Kroah-Hartman 
337396fd7ce5SGreg Kroah-Hartman static void proc_set_tty(struct task_struct *tsk, struct tty_struct *tty)
337496fd7ce5SGreg Kroah-Hartman {
337596fd7ce5SGreg Kroah-Hartman 	spin_lock_irq(&tsk->sighand->siglock);
337696fd7ce5SGreg Kroah-Hartman 	__proc_set_tty(tsk, tty);
337796fd7ce5SGreg Kroah-Hartman 	spin_unlock_irq(&tsk->sighand->siglock);
337896fd7ce5SGreg Kroah-Hartman }
337996fd7ce5SGreg Kroah-Hartman 
338096fd7ce5SGreg Kroah-Hartman struct tty_struct *get_current_tty(void)
338196fd7ce5SGreg Kroah-Hartman {
338296fd7ce5SGreg Kroah-Hartman 	struct tty_struct *tty;
338396fd7ce5SGreg Kroah-Hartman 	unsigned long flags;
338496fd7ce5SGreg Kroah-Hartman 
338596fd7ce5SGreg Kroah-Hartman 	spin_lock_irqsave(&current->sighand->siglock, flags);
338696fd7ce5SGreg Kroah-Hartman 	tty = tty_kref_get(current->signal->tty);
338796fd7ce5SGreg Kroah-Hartman 	spin_unlock_irqrestore(&current->sighand->siglock, flags);
338896fd7ce5SGreg Kroah-Hartman 	return tty;
338996fd7ce5SGreg Kroah-Hartman }
339096fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(get_current_tty);
339196fd7ce5SGreg Kroah-Hartman 
339296fd7ce5SGreg Kroah-Hartman void tty_default_fops(struct file_operations *fops)
339396fd7ce5SGreg Kroah-Hartman {
339496fd7ce5SGreg Kroah-Hartman 	*fops = tty_fops;
339596fd7ce5SGreg Kroah-Hartman }
339696fd7ce5SGreg Kroah-Hartman 
339796fd7ce5SGreg Kroah-Hartman /*
339896fd7ce5SGreg Kroah-Hartman  * Initialize the console device. This is called *early*, so
339996fd7ce5SGreg Kroah-Hartman  * we can't necessarily depend on lots of kernel help here.
340096fd7ce5SGreg Kroah-Hartman  * Just do some early initializations, and do the complex setup
340196fd7ce5SGreg Kroah-Hartman  * later.
340296fd7ce5SGreg Kroah-Hartman  */
340396fd7ce5SGreg Kroah-Hartman void __init console_init(void)
340496fd7ce5SGreg Kroah-Hartman {
340596fd7ce5SGreg Kroah-Hartman 	initcall_t *call;
340696fd7ce5SGreg Kroah-Hartman 
340796fd7ce5SGreg Kroah-Hartman 	/* Setup the default TTY line discipline. */
340896fd7ce5SGreg Kroah-Hartman 	tty_ldisc_begin();
340996fd7ce5SGreg Kroah-Hartman 
341096fd7ce5SGreg Kroah-Hartman 	/*
341196fd7ce5SGreg Kroah-Hartman 	 * set up the console device so that later boot sequences can
341296fd7ce5SGreg Kroah-Hartman 	 * inform about problems etc..
341396fd7ce5SGreg Kroah-Hartman 	 */
341496fd7ce5SGreg Kroah-Hartman 	call = __con_initcall_start;
341596fd7ce5SGreg Kroah-Hartman 	while (call < __con_initcall_end) {
341696fd7ce5SGreg Kroah-Hartman 		(*call)();
341796fd7ce5SGreg Kroah-Hartman 		call++;
341896fd7ce5SGreg Kroah-Hartman 	}
341996fd7ce5SGreg Kroah-Hartman }
342096fd7ce5SGreg Kroah-Hartman 
34212c9ede55SAl Viro static char *tty_devnode(struct device *dev, umode_t *mode)
342296fd7ce5SGreg Kroah-Hartman {
342396fd7ce5SGreg Kroah-Hartman 	if (!mode)
342496fd7ce5SGreg Kroah-Hartman 		return NULL;
342596fd7ce5SGreg Kroah-Hartman 	if (dev->devt == MKDEV(TTYAUX_MAJOR, 0) ||
342696fd7ce5SGreg Kroah-Hartman 	    dev->devt == MKDEV(TTYAUX_MAJOR, 2))
342796fd7ce5SGreg Kroah-Hartman 		*mode = 0666;
342896fd7ce5SGreg Kroah-Hartman 	return NULL;
342996fd7ce5SGreg Kroah-Hartman }
343096fd7ce5SGreg Kroah-Hartman 
343196fd7ce5SGreg Kroah-Hartman static int __init tty_class_init(void)
343296fd7ce5SGreg Kroah-Hartman {
343396fd7ce5SGreg Kroah-Hartman 	tty_class = class_create(THIS_MODULE, "tty");
343496fd7ce5SGreg Kroah-Hartman 	if (IS_ERR(tty_class))
343596fd7ce5SGreg Kroah-Hartman 		return PTR_ERR(tty_class);
343696fd7ce5SGreg Kroah-Hartman 	tty_class->devnode = tty_devnode;
343796fd7ce5SGreg Kroah-Hartman 	return 0;
343896fd7ce5SGreg Kroah-Hartman }
343996fd7ce5SGreg Kroah-Hartman 
344096fd7ce5SGreg Kroah-Hartman postcore_initcall(tty_class_init);
344196fd7ce5SGreg Kroah-Hartman 
344296fd7ce5SGreg Kroah-Hartman /* 3/2004 jmc: why do these devices exist? */
344396fd7ce5SGreg Kroah-Hartman static struct cdev tty_cdev, console_cdev;
344496fd7ce5SGreg Kroah-Hartman 
3445fbc92a34SKay Sievers static ssize_t show_cons_active(struct device *dev,
3446fbc92a34SKay Sievers 				struct device_attribute *attr, char *buf)
3447fbc92a34SKay Sievers {
3448fbc92a34SKay Sievers 	struct console *cs[16];
3449fbc92a34SKay Sievers 	int i = 0;
3450fbc92a34SKay Sievers 	struct console *c;
3451fbc92a34SKay Sievers 	ssize_t count = 0;
3452fbc92a34SKay Sievers 
3453ac751efaSTorben Hohn 	console_lock();
3454a2a6a822SKay Sievers 	for_each_console(c) {
3455fbc92a34SKay Sievers 		if (!c->device)
3456fbc92a34SKay Sievers 			continue;
3457fbc92a34SKay Sievers 		if (!c->write)
3458fbc92a34SKay Sievers 			continue;
3459fbc92a34SKay Sievers 		if ((c->flags & CON_ENABLED) == 0)
3460fbc92a34SKay Sievers 			continue;
3461fbc92a34SKay Sievers 		cs[i++] = c;
3462fbc92a34SKay Sievers 		if (i >= ARRAY_SIZE(cs))
3463fbc92a34SKay Sievers 			break;
3464fbc92a34SKay Sievers 	}
3465fbc92a34SKay Sievers 	while (i--)
3466fbc92a34SKay Sievers 		count += sprintf(buf + count, "%s%d%c",
3467fbc92a34SKay Sievers 				 cs[i]->name, cs[i]->index, i ? ' ':'\n');
3468ac751efaSTorben Hohn 	console_unlock();
3469fbc92a34SKay Sievers 
3470fbc92a34SKay Sievers 	return count;
3471fbc92a34SKay Sievers }
3472fbc92a34SKay Sievers static DEVICE_ATTR(active, S_IRUGO, show_cons_active, NULL);
3473fbc92a34SKay Sievers 
3474fbc92a34SKay Sievers static struct device *consdev;
3475fbc92a34SKay Sievers 
3476fbc92a34SKay Sievers void console_sysfs_notify(void)
3477fbc92a34SKay Sievers {
3478fbc92a34SKay Sievers 	if (consdev)
3479fbc92a34SKay Sievers 		sysfs_notify(&consdev->kobj, NULL, "active");
3480fbc92a34SKay Sievers }
3481fbc92a34SKay Sievers 
348296fd7ce5SGreg Kroah-Hartman /*
348396fd7ce5SGreg Kroah-Hartman  * Ok, now we can initialize the rest of the tty devices and can count
348496fd7ce5SGreg Kroah-Hartman  * on memory allocations, interrupts etc..
348596fd7ce5SGreg Kroah-Hartman  */
348696fd7ce5SGreg Kroah-Hartman int __init tty_init(void)
348796fd7ce5SGreg Kroah-Hartman {
348896fd7ce5SGreg Kroah-Hartman 	cdev_init(&tty_cdev, &tty_fops);
348996fd7ce5SGreg Kroah-Hartman 	if (cdev_add(&tty_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) ||
349096fd7ce5SGreg Kroah-Hartman 	    register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0)
349196fd7ce5SGreg Kroah-Hartman 		panic("Couldn't register /dev/tty driver\n");
3492fbc92a34SKay Sievers 	device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 0), NULL, "tty");
349396fd7ce5SGreg Kroah-Hartman 
349496fd7ce5SGreg Kroah-Hartman 	cdev_init(&console_cdev, &console_fops);
349596fd7ce5SGreg Kroah-Hartman 	if (cdev_add(&console_cdev, MKDEV(TTYAUX_MAJOR, 1), 1) ||
349696fd7ce5SGreg Kroah-Hartman 	    register_chrdev_region(MKDEV(TTYAUX_MAJOR, 1), 1, "/dev/console") < 0)
349796fd7ce5SGreg Kroah-Hartman 		panic("Couldn't register /dev/console driver\n");
3498fbc92a34SKay Sievers 	consdev = device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 1), NULL,
349996fd7ce5SGreg Kroah-Hartman 			      "console");
3500fbc92a34SKay Sievers 	if (IS_ERR(consdev))
3501fbc92a34SKay Sievers 		consdev = NULL;
3502fbc92a34SKay Sievers 	else
3503a2a6a822SKay Sievers 		WARN_ON(device_create_file(consdev, &dev_attr_active) < 0);
350496fd7ce5SGreg Kroah-Hartman 
350596fd7ce5SGreg Kroah-Hartman #ifdef CONFIG_VT
350696fd7ce5SGreg Kroah-Hartman 	vty_init(&console_fops);
350796fd7ce5SGreg Kroah-Hartman #endif
350896fd7ce5SGreg Kroah-Hartman 	return 0;
350996fd7ce5SGreg Kroah-Hartman }
351096fd7ce5SGreg Kroah-Hartman 
3511