xref: /openbmc/linux/drivers/tty/tty_ldisc.c (revision 100eeae2)
196fd7ce5SGreg Kroah-Hartman #include <linux/types.h>
296fd7ce5SGreg Kroah-Hartman #include <linux/major.h>
396fd7ce5SGreg Kroah-Hartman #include <linux/errno.h>
496fd7ce5SGreg Kroah-Hartman #include <linux/signal.h>
596fd7ce5SGreg Kroah-Hartman #include <linux/fcntl.h>
696fd7ce5SGreg Kroah-Hartman #include <linux/sched.h>
796fd7ce5SGreg Kroah-Hartman #include <linux/interrupt.h>
896fd7ce5SGreg Kroah-Hartman #include <linux/tty.h>
996fd7ce5SGreg Kroah-Hartman #include <linux/tty_driver.h>
1096fd7ce5SGreg Kroah-Hartman #include <linux/tty_flip.h>
1196fd7ce5SGreg Kroah-Hartman #include <linux/devpts_fs.h>
1296fd7ce5SGreg Kroah-Hartman #include <linux/file.h>
1396fd7ce5SGreg Kroah-Hartman #include <linux/console.h>
1496fd7ce5SGreg Kroah-Hartman #include <linux/timer.h>
1596fd7ce5SGreg Kroah-Hartman #include <linux/ctype.h>
1696fd7ce5SGreg Kroah-Hartman #include <linux/kd.h>
1796fd7ce5SGreg Kroah-Hartman #include <linux/mm.h>
1896fd7ce5SGreg Kroah-Hartman #include <linux/string.h>
1996fd7ce5SGreg Kroah-Hartman #include <linux/slab.h>
2096fd7ce5SGreg Kroah-Hartman #include <linux/poll.h>
2196fd7ce5SGreg Kroah-Hartman #include <linux/proc_fs.h>
2296fd7ce5SGreg Kroah-Hartman #include <linux/init.h>
2396fd7ce5SGreg Kroah-Hartman #include <linux/module.h>
2496fd7ce5SGreg Kroah-Hartman #include <linux/device.h>
2596fd7ce5SGreg Kroah-Hartman #include <linux/wait.h>
2696fd7ce5SGreg Kroah-Hartman #include <linux/bitops.h>
2796fd7ce5SGreg Kroah-Hartman #include <linux/delay.h>
2896fd7ce5SGreg Kroah-Hartman #include <linux/seq_file.h>
2996fd7ce5SGreg Kroah-Hartman 
3096fd7ce5SGreg Kroah-Hartman #include <linux/uaccess.h>
3196fd7ce5SGreg Kroah-Hartman #include <asm/system.h>
3296fd7ce5SGreg Kroah-Hartman 
3396fd7ce5SGreg Kroah-Hartman #include <linux/kbd_kern.h>
3496fd7ce5SGreg Kroah-Hartman #include <linux/vt_kern.h>
3596fd7ce5SGreg Kroah-Hartman #include <linux/selection.h>
3696fd7ce5SGreg Kroah-Hartman 
3796fd7ce5SGreg Kroah-Hartman #include <linux/smp_lock.h>	/* For the moment */
3896fd7ce5SGreg Kroah-Hartman 
3996fd7ce5SGreg Kroah-Hartman #include <linux/kmod.h>
4096fd7ce5SGreg Kroah-Hartman #include <linux/nsproxy.h>
4196fd7ce5SGreg Kroah-Hartman 
4296fd7ce5SGreg Kroah-Hartman /*
4396fd7ce5SGreg Kroah-Hartman  *	This guards the refcounted line discipline lists. The lock
4496fd7ce5SGreg Kroah-Hartman  *	must be taken with irqs off because there are hangup path
4596fd7ce5SGreg Kroah-Hartman  *	callers who will do ldisc lookups and cannot sleep.
4696fd7ce5SGreg Kroah-Hartman  */
4796fd7ce5SGreg Kroah-Hartman 
4896fd7ce5SGreg Kroah-Hartman static DEFINE_SPINLOCK(tty_ldisc_lock);
4996fd7ce5SGreg Kroah-Hartman static DECLARE_WAIT_QUEUE_HEAD(tty_ldisc_wait);
50100eeae2SJiri Slaby static DECLARE_WAIT_QUEUE_HEAD(tty_ldisc_idle);
5196fd7ce5SGreg Kroah-Hartman /* Line disc dispatch table */
5296fd7ce5SGreg Kroah-Hartman static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS];
5396fd7ce5SGreg Kroah-Hartman 
5496fd7ce5SGreg Kroah-Hartman static inline struct tty_ldisc *get_ldisc(struct tty_ldisc *ld)
5596fd7ce5SGreg Kroah-Hartman {
5696fd7ce5SGreg Kroah-Hartman 	if (ld)
5796fd7ce5SGreg Kroah-Hartman 		atomic_inc(&ld->users);
5896fd7ce5SGreg Kroah-Hartman 	return ld;
5996fd7ce5SGreg Kroah-Hartman }
6096fd7ce5SGreg Kroah-Hartman 
6196fd7ce5SGreg Kroah-Hartman static void put_ldisc(struct tty_ldisc *ld)
6296fd7ce5SGreg Kroah-Hartman {
6396fd7ce5SGreg Kroah-Hartman 	unsigned long flags;
6496fd7ce5SGreg Kroah-Hartman 
6596fd7ce5SGreg Kroah-Hartman 	if (WARN_ON_ONCE(!ld))
6696fd7ce5SGreg Kroah-Hartman 		return;
6796fd7ce5SGreg Kroah-Hartman 
6896fd7ce5SGreg Kroah-Hartman 	/*
6996fd7ce5SGreg Kroah-Hartman 	 * If this is the last user, free the ldisc, and
7096fd7ce5SGreg Kroah-Hartman 	 * release the ldisc ops.
7196fd7ce5SGreg Kroah-Hartman 	 *
7296fd7ce5SGreg Kroah-Hartman 	 * We really want an "atomic_dec_and_lock_irqsave()",
7396fd7ce5SGreg Kroah-Hartman 	 * but we don't have it, so this does it by hand.
7496fd7ce5SGreg Kroah-Hartman 	 */
7596fd7ce5SGreg Kroah-Hartman 	local_irq_save(flags);
7696fd7ce5SGreg Kroah-Hartman 	if (atomic_dec_and_lock(&ld->users, &tty_ldisc_lock)) {
7796fd7ce5SGreg Kroah-Hartman 		struct tty_ldisc_ops *ldo = ld->ops;
7896fd7ce5SGreg Kroah-Hartman 
7996fd7ce5SGreg Kroah-Hartman 		ldo->refcount--;
8096fd7ce5SGreg Kroah-Hartman 		module_put(ldo->owner);
8196fd7ce5SGreg Kroah-Hartman 		spin_unlock_irqrestore(&tty_ldisc_lock, flags);
8296fd7ce5SGreg Kroah-Hartman 
8396fd7ce5SGreg Kroah-Hartman 		kfree(ld);
8496fd7ce5SGreg Kroah-Hartman 		return;
8596fd7ce5SGreg Kroah-Hartman 	}
8696fd7ce5SGreg Kroah-Hartman 	local_irq_restore(flags);
87100eeae2SJiri Slaby 	wake_up(&tty_ldisc_idle);
8896fd7ce5SGreg Kroah-Hartman }
8996fd7ce5SGreg Kroah-Hartman 
9096fd7ce5SGreg Kroah-Hartman /**
9196fd7ce5SGreg Kroah-Hartman  *	tty_register_ldisc	-	install a line discipline
9296fd7ce5SGreg Kroah-Hartman  *	@disc: ldisc number
9396fd7ce5SGreg Kroah-Hartman  *	@new_ldisc: pointer to the ldisc object
9496fd7ce5SGreg Kroah-Hartman  *
9596fd7ce5SGreg Kroah-Hartman  *	Installs a new line discipline into the kernel. The discipline
9696fd7ce5SGreg Kroah-Hartman  *	is set up as unreferenced and then made available to the kernel
9796fd7ce5SGreg Kroah-Hartman  *	from this point onwards.
9896fd7ce5SGreg Kroah-Hartman  *
9996fd7ce5SGreg Kroah-Hartman  *	Locking:
10096fd7ce5SGreg Kroah-Hartman  *		takes tty_ldisc_lock to guard against ldisc races
10196fd7ce5SGreg Kroah-Hartman  */
10296fd7ce5SGreg Kroah-Hartman 
10396fd7ce5SGreg Kroah-Hartman int tty_register_ldisc(int disc, struct tty_ldisc_ops *new_ldisc)
10496fd7ce5SGreg Kroah-Hartman {
10596fd7ce5SGreg Kroah-Hartman 	unsigned long flags;
10696fd7ce5SGreg Kroah-Hartman 	int ret = 0;
10796fd7ce5SGreg Kroah-Hartman 
10896fd7ce5SGreg Kroah-Hartman 	if (disc < N_TTY || disc >= NR_LDISCS)
10996fd7ce5SGreg Kroah-Hartman 		return -EINVAL;
11096fd7ce5SGreg Kroah-Hartman 
11196fd7ce5SGreg Kroah-Hartman 	spin_lock_irqsave(&tty_ldisc_lock, flags);
11296fd7ce5SGreg Kroah-Hartman 	tty_ldiscs[disc] = new_ldisc;
11396fd7ce5SGreg Kroah-Hartman 	new_ldisc->num = disc;
11496fd7ce5SGreg Kroah-Hartman 	new_ldisc->refcount = 0;
11596fd7ce5SGreg Kroah-Hartman 	spin_unlock_irqrestore(&tty_ldisc_lock, flags);
11696fd7ce5SGreg Kroah-Hartman 
11796fd7ce5SGreg Kroah-Hartman 	return ret;
11896fd7ce5SGreg Kroah-Hartman }
11996fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_register_ldisc);
12096fd7ce5SGreg Kroah-Hartman 
12196fd7ce5SGreg Kroah-Hartman /**
12296fd7ce5SGreg Kroah-Hartman  *	tty_unregister_ldisc	-	unload a line discipline
12396fd7ce5SGreg Kroah-Hartman  *	@disc: ldisc number
12496fd7ce5SGreg Kroah-Hartman  *	@new_ldisc: pointer to the ldisc object
12596fd7ce5SGreg Kroah-Hartman  *
12696fd7ce5SGreg Kroah-Hartman  *	Remove a line discipline from the kernel providing it is not
12796fd7ce5SGreg Kroah-Hartman  *	currently in use.
12896fd7ce5SGreg Kroah-Hartman  *
12996fd7ce5SGreg Kroah-Hartman  *	Locking:
13096fd7ce5SGreg Kroah-Hartman  *		takes tty_ldisc_lock to guard against ldisc races
13196fd7ce5SGreg Kroah-Hartman  */
13296fd7ce5SGreg Kroah-Hartman 
13396fd7ce5SGreg Kroah-Hartman int tty_unregister_ldisc(int disc)
13496fd7ce5SGreg Kroah-Hartman {
13596fd7ce5SGreg Kroah-Hartman 	unsigned long flags;
13696fd7ce5SGreg Kroah-Hartman 	int ret = 0;
13796fd7ce5SGreg Kroah-Hartman 
13896fd7ce5SGreg Kroah-Hartman 	if (disc < N_TTY || disc >= NR_LDISCS)
13996fd7ce5SGreg Kroah-Hartman 		return -EINVAL;
14096fd7ce5SGreg Kroah-Hartman 
14196fd7ce5SGreg Kroah-Hartman 	spin_lock_irqsave(&tty_ldisc_lock, flags);
14296fd7ce5SGreg Kroah-Hartman 	if (tty_ldiscs[disc]->refcount)
14396fd7ce5SGreg Kroah-Hartman 		ret = -EBUSY;
14496fd7ce5SGreg Kroah-Hartman 	else
14596fd7ce5SGreg Kroah-Hartman 		tty_ldiscs[disc] = NULL;
14696fd7ce5SGreg Kroah-Hartman 	spin_unlock_irqrestore(&tty_ldisc_lock, flags);
14796fd7ce5SGreg Kroah-Hartman 
14896fd7ce5SGreg Kroah-Hartman 	return ret;
14996fd7ce5SGreg Kroah-Hartman }
15096fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_unregister_ldisc);
15196fd7ce5SGreg Kroah-Hartman 
15296fd7ce5SGreg Kroah-Hartman static struct tty_ldisc_ops *get_ldops(int disc)
15396fd7ce5SGreg Kroah-Hartman {
15496fd7ce5SGreg Kroah-Hartman 	unsigned long flags;
15596fd7ce5SGreg Kroah-Hartman 	struct tty_ldisc_ops *ldops, *ret;
15696fd7ce5SGreg Kroah-Hartman 
15796fd7ce5SGreg Kroah-Hartman 	spin_lock_irqsave(&tty_ldisc_lock, flags);
15896fd7ce5SGreg Kroah-Hartman 	ret = ERR_PTR(-EINVAL);
15996fd7ce5SGreg Kroah-Hartman 	ldops = tty_ldiscs[disc];
16096fd7ce5SGreg Kroah-Hartman 	if (ldops) {
16196fd7ce5SGreg Kroah-Hartman 		ret = ERR_PTR(-EAGAIN);
16296fd7ce5SGreg Kroah-Hartman 		if (try_module_get(ldops->owner)) {
16396fd7ce5SGreg Kroah-Hartman 			ldops->refcount++;
16496fd7ce5SGreg Kroah-Hartman 			ret = ldops;
16596fd7ce5SGreg Kroah-Hartman 		}
16696fd7ce5SGreg Kroah-Hartman 	}
16796fd7ce5SGreg Kroah-Hartman 	spin_unlock_irqrestore(&tty_ldisc_lock, flags);
16896fd7ce5SGreg Kroah-Hartman 	return ret;
16996fd7ce5SGreg Kroah-Hartman }
17096fd7ce5SGreg Kroah-Hartman 
17196fd7ce5SGreg Kroah-Hartman static void put_ldops(struct tty_ldisc_ops *ldops)
17296fd7ce5SGreg Kroah-Hartman {
17396fd7ce5SGreg Kroah-Hartman 	unsigned long flags;
17496fd7ce5SGreg Kroah-Hartman 
17596fd7ce5SGreg Kroah-Hartman 	spin_lock_irqsave(&tty_ldisc_lock, flags);
17696fd7ce5SGreg Kroah-Hartman 	ldops->refcount--;
17796fd7ce5SGreg Kroah-Hartman 	module_put(ldops->owner);
17896fd7ce5SGreg Kroah-Hartman 	spin_unlock_irqrestore(&tty_ldisc_lock, flags);
17996fd7ce5SGreg Kroah-Hartman }
18096fd7ce5SGreg Kroah-Hartman 
18196fd7ce5SGreg Kroah-Hartman /**
18296fd7ce5SGreg Kroah-Hartman  *	tty_ldisc_get		-	take a reference to an ldisc
18396fd7ce5SGreg Kroah-Hartman  *	@disc: ldisc number
18496fd7ce5SGreg Kroah-Hartman  *
18596fd7ce5SGreg Kroah-Hartman  *	Takes a reference to a line discipline. Deals with refcounts and
18696fd7ce5SGreg Kroah-Hartman  *	module locking counts. Returns NULL if the discipline is not available.
18796fd7ce5SGreg Kroah-Hartman  *	Returns a pointer to the discipline and bumps the ref count if it is
18896fd7ce5SGreg Kroah-Hartman  *	available
18996fd7ce5SGreg Kroah-Hartman  *
19096fd7ce5SGreg Kroah-Hartman  *	Locking:
19196fd7ce5SGreg Kroah-Hartman  *		takes tty_ldisc_lock to guard against ldisc races
19296fd7ce5SGreg Kroah-Hartman  */
19396fd7ce5SGreg Kroah-Hartman 
19496fd7ce5SGreg Kroah-Hartman static struct tty_ldisc *tty_ldisc_get(int disc)
19596fd7ce5SGreg Kroah-Hartman {
19696fd7ce5SGreg Kroah-Hartman 	struct tty_ldisc *ld;
19796fd7ce5SGreg Kroah-Hartman 	struct tty_ldisc_ops *ldops;
19896fd7ce5SGreg Kroah-Hartman 
19996fd7ce5SGreg Kroah-Hartman 	if (disc < N_TTY || disc >= NR_LDISCS)
20096fd7ce5SGreg Kroah-Hartman 		return ERR_PTR(-EINVAL);
20196fd7ce5SGreg Kroah-Hartman 
20296fd7ce5SGreg Kroah-Hartman 	/*
20396fd7ce5SGreg Kroah-Hartman 	 * Get the ldisc ops - we may need to request them to be loaded
20496fd7ce5SGreg Kroah-Hartman 	 * dynamically and try again.
20596fd7ce5SGreg Kroah-Hartman 	 */
20696fd7ce5SGreg Kroah-Hartman 	ldops = get_ldops(disc);
20796fd7ce5SGreg Kroah-Hartman 	if (IS_ERR(ldops)) {
20896fd7ce5SGreg Kroah-Hartman 		request_module("tty-ldisc-%d", disc);
20996fd7ce5SGreg Kroah-Hartman 		ldops = get_ldops(disc);
21096fd7ce5SGreg Kroah-Hartman 		if (IS_ERR(ldops))
21196fd7ce5SGreg Kroah-Hartman 			return ERR_CAST(ldops);
21296fd7ce5SGreg Kroah-Hartman 	}
21396fd7ce5SGreg Kroah-Hartman 
21496fd7ce5SGreg Kroah-Hartman 	ld = kmalloc(sizeof(struct tty_ldisc), GFP_KERNEL);
21596fd7ce5SGreg Kroah-Hartman 	if (ld == NULL) {
21696fd7ce5SGreg Kroah-Hartman 		put_ldops(ldops);
21796fd7ce5SGreg Kroah-Hartman 		return ERR_PTR(-ENOMEM);
21896fd7ce5SGreg Kroah-Hartman 	}
21996fd7ce5SGreg Kroah-Hartman 
22096fd7ce5SGreg Kroah-Hartman 	ld->ops = ldops;
22196fd7ce5SGreg Kroah-Hartman 	atomic_set(&ld->users, 1);
22296fd7ce5SGreg Kroah-Hartman 	return ld;
22396fd7ce5SGreg Kroah-Hartman }
22496fd7ce5SGreg Kroah-Hartman 
22596fd7ce5SGreg Kroah-Hartman static void *tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos)
22696fd7ce5SGreg Kroah-Hartman {
22796fd7ce5SGreg Kroah-Hartman 	return (*pos < NR_LDISCS) ? pos : NULL;
22896fd7ce5SGreg Kroah-Hartman }
22996fd7ce5SGreg Kroah-Hartman 
23096fd7ce5SGreg Kroah-Hartman static void *tty_ldiscs_seq_next(struct seq_file *m, void *v, loff_t *pos)
23196fd7ce5SGreg Kroah-Hartman {
23296fd7ce5SGreg Kroah-Hartman 	(*pos)++;
23396fd7ce5SGreg Kroah-Hartman 	return (*pos < NR_LDISCS) ? pos : NULL;
23496fd7ce5SGreg Kroah-Hartman }
23596fd7ce5SGreg Kroah-Hartman 
23696fd7ce5SGreg Kroah-Hartman static void tty_ldiscs_seq_stop(struct seq_file *m, void *v)
23796fd7ce5SGreg Kroah-Hartman {
23896fd7ce5SGreg Kroah-Hartman }
23996fd7ce5SGreg Kroah-Hartman 
24096fd7ce5SGreg Kroah-Hartman static int tty_ldiscs_seq_show(struct seq_file *m, void *v)
24196fd7ce5SGreg Kroah-Hartman {
24296fd7ce5SGreg Kroah-Hartman 	int i = *(loff_t *)v;
24396fd7ce5SGreg Kroah-Hartman 	struct tty_ldisc_ops *ldops;
24496fd7ce5SGreg Kroah-Hartman 
24596fd7ce5SGreg Kroah-Hartman 	ldops = get_ldops(i);
24696fd7ce5SGreg Kroah-Hartman 	if (IS_ERR(ldops))
24796fd7ce5SGreg Kroah-Hartman 		return 0;
24896fd7ce5SGreg Kroah-Hartman 	seq_printf(m, "%-10s %2d\n", ldops->name ? ldops->name : "???", i);
24996fd7ce5SGreg Kroah-Hartman 	put_ldops(ldops);
25096fd7ce5SGreg Kroah-Hartman 	return 0;
25196fd7ce5SGreg Kroah-Hartman }
25296fd7ce5SGreg Kroah-Hartman 
25396fd7ce5SGreg Kroah-Hartman static const struct seq_operations tty_ldiscs_seq_ops = {
25496fd7ce5SGreg Kroah-Hartman 	.start	= tty_ldiscs_seq_start,
25596fd7ce5SGreg Kroah-Hartman 	.next	= tty_ldiscs_seq_next,
25696fd7ce5SGreg Kroah-Hartman 	.stop	= tty_ldiscs_seq_stop,
25796fd7ce5SGreg Kroah-Hartman 	.show	= tty_ldiscs_seq_show,
25896fd7ce5SGreg Kroah-Hartman };
25996fd7ce5SGreg Kroah-Hartman 
26096fd7ce5SGreg Kroah-Hartman static int proc_tty_ldiscs_open(struct inode *inode, struct file *file)
26196fd7ce5SGreg Kroah-Hartman {
26296fd7ce5SGreg Kroah-Hartman 	return seq_open(file, &tty_ldiscs_seq_ops);
26396fd7ce5SGreg Kroah-Hartman }
26496fd7ce5SGreg Kroah-Hartman 
26596fd7ce5SGreg Kroah-Hartman const struct file_operations tty_ldiscs_proc_fops = {
26696fd7ce5SGreg Kroah-Hartman 	.owner		= THIS_MODULE,
26796fd7ce5SGreg Kroah-Hartman 	.open		= proc_tty_ldiscs_open,
26896fd7ce5SGreg Kroah-Hartman 	.read		= seq_read,
26996fd7ce5SGreg Kroah-Hartman 	.llseek		= seq_lseek,
27096fd7ce5SGreg Kroah-Hartman 	.release	= seq_release,
27196fd7ce5SGreg Kroah-Hartman };
27296fd7ce5SGreg Kroah-Hartman 
27396fd7ce5SGreg Kroah-Hartman /**
27496fd7ce5SGreg Kroah-Hartman  *	tty_ldisc_assign	-	set ldisc on a tty
27596fd7ce5SGreg Kroah-Hartman  *	@tty: tty to assign
27696fd7ce5SGreg Kroah-Hartman  *	@ld: line discipline
27796fd7ce5SGreg Kroah-Hartman  *
27896fd7ce5SGreg Kroah-Hartman  *	Install an instance of a line discipline into a tty structure. The
27996fd7ce5SGreg Kroah-Hartman  *	ldisc must have a reference count above zero to ensure it remains.
28096fd7ce5SGreg Kroah-Hartman  *	The tty instance refcount starts at zero.
28196fd7ce5SGreg Kroah-Hartman  *
28296fd7ce5SGreg Kroah-Hartman  *	Locking:
28396fd7ce5SGreg Kroah-Hartman  *		Caller must hold references
28496fd7ce5SGreg Kroah-Hartman  */
28596fd7ce5SGreg Kroah-Hartman 
28696fd7ce5SGreg Kroah-Hartman static void tty_ldisc_assign(struct tty_struct *tty, struct tty_ldisc *ld)
28796fd7ce5SGreg Kroah-Hartman {
28896fd7ce5SGreg Kroah-Hartman 	tty->ldisc = ld;
28996fd7ce5SGreg Kroah-Hartman }
29096fd7ce5SGreg Kroah-Hartman 
29196fd7ce5SGreg Kroah-Hartman /**
29296fd7ce5SGreg Kroah-Hartman  *	tty_ldisc_try		-	internal helper
29396fd7ce5SGreg Kroah-Hartman  *	@tty: the tty
29496fd7ce5SGreg Kroah-Hartman  *
29596fd7ce5SGreg Kroah-Hartman  *	Make a single attempt to grab and bump the refcount on
29696fd7ce5SGreg Kroah-Hartman  *	the tty ldisc. Return 0 on failure or 1 on success. This is
29796fd7ce5SGreg Kroah-Hartman  *	used to implement both the waiting and non waiting versions
29896fd7ce5SGreg Kroah-Hartman  *	of tty_ldisc_ref
29996fd7ce5SGreg Kroah-Hartman  *
30096fd7ce5SGreg Kroah-Hartman  *	Locking: takes tty_ldisc_lock
30196fd7ce5SGreg Kroah-Hartman  */
30296fd7ce5SGreg Kroah-Hartman 
30396fd7ce5SGreg Kroah-Hartman static struct tty_ldisc *tty_ldisc_try(struct tty_struct *tty)
30496fd7ce5SGreg Kroah-Hartman {
30596fd7ce5SGreg Kroah-Hartman 	unsigned long flags;
30696fd7ce5SGreg Kroah-Hartman 	struct tty_ldisc *ld;
30796fd7ce5SGreg Kroah-Hartman 
30896fd7ce5SGreg Kroah-Hartman 	spin_lock_irqsave(&tty_ldisc_lock, flags);
30996fd7ce5SGreg Kroah-Hartman 	ld = NULL;
31096fd7ce5SGreg Kroah-Hartman 	if (test_bit(TTY_LDISC, &tty->flags))
31196fd7ce5SGreg Kroah-Hartman 		ld = get_ldisc(tty->ldisc);
31296fd7ce5SGreg Kroah-Hartman 	spin_unlock_irqrestore(&tty_ldisc_lock, flags);
31396fd7ce5SGreg Kroah-Hartman 	return ld;
31496fd7ce5SGreg Kroah-Hartman }
31596fd7ce5SGreg Kroah-Hartman 
31696fd7ce5SGreg Kroah-Hartman /**
31796fd7ce5SGreg Kroah-Hartman  *	tty_ldisc_ref_wait	-	wait for the tty ldisc
31896fd7ce5SGreg Kroah-Hartman  *	@tty: tty device
31996fd7ce5SGreg Kroah-Hartman  *
32096fd7ce5SGreg Kroah-Hartman  *	Dereference the line discipline for the terminal and take a
32196fd7ce5SGreg Kroah-Hartman  *	reference to it. If the line discipline is in flux then
32296fd7ce5SGreg Kroah-Hartman  *	wait patiently until it changes.
32396fd7ce5SGreg Kroah-Hartman  *
32496fd7ce5SGreg Kroah-Hartman  *	Note: Must not be called from an IRQ/timer context. The caller
32596fd7ce5SGreg Kroah-Hartman  *	must also be careful not to hold other locks that will deadlock
32696fd7ce5SGreg Kroah-Hartman  *	against a discipline change, such as an existing ldisc reference
32796fd7ce5SGreg Kroah-Hartman  *	(which we check for)
32896fd7ce5SGreg Kroah-Hartman  *
32996fd7ce5SGreg Kroah-Hartman  *	Locking: call functions take tty_ldisc_lock
33096fd7ce5SGreg Kroah-Hartman  */
33196fd7ce5SGreg Kroah-Hartman 
33296fd7ce5SGreg Kroah-Hartman struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty)
33396fd7ce5SGreg Kroah-Hartman {
33496fd7ce5SGreg Kroah-Hartman 	struct tty_ldisc *ld;
33596fd7ce5SGreg Kroah-Hartman 
33696fd7ce5SGreg Kroah-Hartman 	/* wait_event is a macro */
33796fd7ce5SGreg Kroah-Hartman 	wait_event(tty_ldisc_wait, (ld = tty_ldisc_try(tty)) != NULL);
33896fd7ce5SGreg Kroah-Hartman 	return ld;
33996fd7ce5SGreg Kroah-Hartman }
34096fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait);
34196fd7ce5SGreg Kroah-Hartman 
34296fd7ce5SGreg Kroah-Hartman /**
34396fd7ce5SGreg Kroah-Hartman  *	tty_ldisc_ref		-	get the tty ldisc
34496fd7ce5SGreg Kroah-Hartman  *	@tty: tty device
34596fd7ce5SGreg Kroah-Hartman  *
34696fd7ce5SGreg Kroah-Hartman  *	Dereference the line discipline for the terminal and take a
34796fd7ce5SGreg Kroah-Hartman  *	reference to it. If the line discipline is in flux then
34896fd7ce5SGreg Kroah-Hartman  *	return NULL. Can be called from IRQ and timer functions.
34996fd7ce5SGreg Kroah-Hartman  *
35096fd7ce5SGreg Kroah-Hartman  *	Locking: called functions take tty_ldisc_lock
35196fd7ce5SGreg Kroah-Hartman  */
35296fd7ce5SGreg Kroah-Hartman 
35396fd7ce5SGreg Kroah-Hartman struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty)
35496fd7ce5SGreg Kroah-Hartman {
35596fd7ce5SGreg Kroah-Hartman 	return tty_ldisc_try(tty);
35696fd7ce5SGreg Kroah-Hartman }
35796fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(tty_ldisc_ref);
35896fd7ce5SGreg Kroah-Hartman 
35996fd7ce5SGreg Kroah-Hartman /**
36096fd7ce5SGreg Kroah-Hartman  *	tty_ldisc_deref		-	free a tty ldisc reference
36196fd7ce5SGreg Kroah-Hartman  *	@ld: reference to free up
36296fd7ce5SGreg Kroah-Hartman  *
36396fd7ce5SGreg Kroah-Hartman  *	Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May
36496fd7ce5SGreg Kroah-Hartman  *	be called in IRQ context.
36596fd7ce5SGreg Kroah-Hartman  *
36696fd7ce5SGreg Kroah-Hartman  *	Locking: takes tty_ldisc_lock
36796fd7ce5SGreg Kroah-Hartman  */
36896fd7ce5SGreg Kroah-Hartman 
36996fd7ce5SGreg Kroah-Hartman void tty_ldisc_deref(struct tty_ldisc *ld)
37096fd7ce5SGreg Kroah-Hartman {
37196fd7ce5SGreg Kroah-Hartman 	put_ldisc(ld);
37296fd7ce5SGreg Kroah-Hartman }
37396fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(tty_ldisc_deref);
37496fd7ce5SGreg Kroah-Hartman 
37596fd7ce5SGreg Kroah-Hartman static inline void tty_ldisc_put(struct tty_ldisc *ld)
37696fd7ce5SGreg Kroah-Hartman {
37796fd7ce5SGreg Kroah-Hartman 	put_ldisc(ld);
37896fd7ce5SGreg Kroah-Hartman }
37996fd7ce5SGreg Kroah-Hartman 
38096fd7ce5SGreg Kroah-Hartman /**
38196fd7ce5SGreg Kroah-Hartman  *	tty_ldisc_enable	-	allow ldisc use
38296fd7ce5SGreg Kroah-Hartman  *	@tty: terminal to activate ldisc on
38396fd7ce5SGreg Kroah-Hartman  *
38496fd7ce5SGreg Kroah-Hartman  *	Set the TTY_LDISC flag when the line discipline can be called
38596fd7ce5SGreg Kroah-Hartman  *	again. Do necessary wakeups for existing sleepers. Clear the LDISC
38696fd7ce5SGreg Kroah-Hartman  *	changing flag to indicate any ldisc change is now over.
38796fd7ce5SGreg Kroah-Hartman  *
38896fd7ce5SGreg Kroah-Hartman  *	Note: nobody should set the TTY_LDISC bit except via this function.
38996fd7ce5SGreg Kroah-Hartman  *	Clearing directly is allowed.
39096fd7ce5SGreg Kroah-Hartman  */
39196fd7ce5SGreg Kroah-Hartman 
39296fd7ce5SGreg Kroah-Hartman void tty_ldisc_enable(struct tty_struct *tty)
39396fd7ce5SGreg Kroah-Hartman {
39496fd7ce5SGreg Kroah-Hartman 	set_bit(TTY_LDISC, &tty->flags);
39596fd7ce5SGreg Kroah-Hartman 	clear_bit(TTY_LDISC_CHANGING, &tty->flags);
39696fd7ce5SGreg Kroah-Hartman 	wake_up(&tty_ldisc_wait);
39796fd7ce5SGreg Kroah-Hartman }
39896fd7ce5SGreg Kroah-Hartman 
39996fd7ce5SGreg Kroah-Hartman /**
40096fd7ce5SGreg Kroah-Hartman  *	tty_ldisc_flush	-	flush line discipline queue
40196fd7ce5SGreg Kroah-Hartman  *	@tty: tty
40296fd7ce5SGreg Kroah-Hartman  *
40396fd7ce5SGreg Kroah-Hartman  *	Flush the line discipline queue (if any) for this tty. If there
40496fd7ce5SGreg Kroah-Hartman  *	is no line discipline active this is a no-op.
40596fd7ce5SGreg Kroah-Hartman  */
40696fd7ce5SGreg Kroah-Hartman 
40796fd7ce5SGreg Kroah-Hartman void tty_ldisc_flush(struct tty_struct *tty)
40896fd7ce5SGreg Kroah-Hartman {
40996fd7ce5SGreg Kroah-Hartman 	struct tty_ldisc *ld = tty_ldisc_ref(tty);
41096fd7ce5SGreg Kroah-Hartman 	if (ld) {
41196fd7ce5SGreg Kroah-Hartman 		if (ld->ops->flush_buffer)
41296fd7ce5SGreg Kroah-Hartman 			ld->ops->flush_buffer(tty);
41396fd7ce5SGreg Kroah-Hartman 		tty_ldisc_deref(ld);
41496fd7ce5SGreg Kroah-Hartman 	}
41596fd7ce5SGreg Kroah-Hartman 	tty_buffer_flush(tty);
41696fd7ce5SGreg Kroah-Hartman }
41796fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(tty_ldisc_flush);
41896fd7ce5SGreg Kroah-Hartman 
41996fd7ce5SGreg Kroah-Hartman /**
42096fd7ce5SGreg Kroah-Hartman  *	tty_set_termios_ldisc		-	set ldisc field
42196fd7ce5SGreg Kroah-Hartman  *	@tty: tty structure
42296fd7ce5SGreg Kroah-Hartman  *	@num: line discipline number
42396fd7ce5SGreg Kroah-Hartman  *
42496fd7ce5SGreg Kroah-Hartman  *	This is probably overkill for real world processors but
42596fd7ce5SGreg Kroah-Hartman  *	they are not on hot paths so a little discipline won't do
42696fd7ce5SGreg Kroah-Hartman  *	any harm.
42796fd7ce5SGreg Kroah-Hartman  *
42896fd7ce5SGreg Kroah-Hartman  *	Locking: takes termios_mutex
42996fd7ce5SGreg Kroah-Hartman  */
43096fd7ce5SGreg Kroah-Hartman 
43196fd7ce5SGreg Kroah-Hartman static void tty_set_termios_ldisc(struct tty_struct *tty, int num)
43296fd7ce5SGreg Kroah-Hartman {
43396fd7ce5SGreg Kroah-Hartman 	mutex_lock(&tty->termios_mutex);
43496fd7ce5SGreg Kroah-Hartman 	tty->termios->c_line = num;
43596fd7ce5SGreg Kroah-Hartman 	mutex_unlock(&tty->termios_mutex);
43696fd7ce5SGreg Kroah-Hartman }
43796fd7ce5SGreg Kroah-Hartman 
43896fd7ce5SGreg Kroah-Hartman /**
43996fd7ce5SGreg Kroah-Hartman  *	tty_ldisc_open		-	open a line discipline
44096fd7ce5SGreg Kroah-Hartman  *	@tty: tty we are opening the ldisc on
44196fd7ce5SGreg Kroah-Hartman  *	@ld: discipline to open
44296fd7ce5SGreg Kroah-Hartman  *
44396fd7ce5SGreg Kroah-Hartman  *	A helper opening method. Also a convenient debugging and check
44496fd7ce5SGreg Kroah-Hartman  *	point.
44596fd7ce5SGreg Kroah-Hartman  *
44696fd7ce5SGreg Kroah-Hartman  *	Locking: always called with BTM already held.
44796fd7ce5SGreg Kroah-Hartman  */
44896fd7ce5SGreg Kroah-Hartman 
44996fd7ce5SGreg Kroah-Hartman static int tty_ldisc_open(struct tty_struct *tty, struct tty_ldisc *ld)
45096fd7ce5SGreg Kroah-Hartman {
45196fd7ce5SGreg Kroah-Hartman 	WARN_ON(test_and_set_bit(TTY_LDISC_OPEN, &tty->flags));
45296fd7ce5SGreg Kroah-Hartman 	if (ld->ops->open) {
45396fd7ce5SGreg Kroah-Hartman 		int ret;
45496fd7ce5SGreg Kroah-Hartman                 /* BTM here locks versus a hangup event */
45596fd7ce5SGreg Kroah-Hartman 		WARN_ON(!tty_locked());
45696fd7ce5SGreg Kroah-Hartman 		ret = ld->ops->open(tty);
45796fd7ce5SGreg Kroah-Hartman 		return ret;
45896fd7ce5SGreg Kroah-Hartman 	}
45996fd7ce5SGreg Kroah-Hartman 	return 0;
46096fd7ce5SGreg Kroah-Hartman }
46196fd7ce5SGreg Kroah-Hartman 
46296fd7ce5SGreg Kroah-Hartman /**
46396fd7ce5SGreg Kroah-Hartman  *	tty_ldisc_close		-	close a line discipline
46496fd7ce5SGreg Kroah-Hartman  *	@tty: tty we are opening the ldisc on
46596fd7ce5SGreg Kroah-Hartman  *	@ld: discipline to close
46696fd7ce5SGreg Kroah-Hartman  *
46796fd7ce5SGreg Kroah-Hartman  *	A helper close method. Also a convenient debugging and check
46896fd7ce5SGreg Kroah-Hartman  *	point.
46996fd7ce5SGreg Kroah-Hartman  */
47096fd7ce5SGreg Kroah-Hartman 
47196fd7ce5SGreg Kroah-Hartman static void tty_ldisc_close(struct tty_struct *tty, struct tty_ldisc *ld)
47296fd7ce5SGreg Kroah-Hartman {
47396fd7ce5SGreg Kroah-Hartman 	WARN_ON(!test_bit(TTY_LDISC_OPEN, &tty->flags));
47496fd7ce5SGreg Kroah-Hartman 	clear_bit(TTY_LDISC_OPEN, &tty->flags);
47596fd7ce5SGreg Kroah-Hartman 	if (ld->ops->close)
47696fd7ce5SGreg Kroah-Hartman 		ld->ops->close(tty);
47796fd7ce5SGreg Kroah-Hartman }
47896fd7ce5SGreg Kroah-Hartman 
47996fd7ce5SGreg Kroah-Hartman /**
48096fd7ce5SGreg Kroah-Hartman  *	tty_ldisc_restore	-	helper for tty ldisc change
48196fd7ce5SGreg Kroah-Hartman  *	@tty: tty to recover
48296fd7ce5SGreg Kroah-Hartman  *	@old: previous ldisc
48396fd7ce5SGreg Kroah-Hartman  *
48496fd7ce5SGreg Kroah-Hartman  *	Restore the previous line discipline or N_TTY when a line discipline
48596fd7ce5SGreg Kroah-Hartman  *	change fails due to an open error
48696fd7ce5SGreg Kroah-Hartman  */
48796fd7ce5SGreg Kroah-Hartman 
48896fd7ce5SGreg Kroah-Hartman static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old)
48996fd7ce5SGreg Kroah-Hartman {
49096fd7ce5SGreg Kroah-Hartman 	char buf[64];
49196fd7ce5SGreg Kroah-Hartman 	struct tty_ldisc *new_ldisc;
49296fd7ce5SGreg Kroah-Hartman 	int r;
49396fd7ce5SGreg Kroah-Hartman 
49496fd7ce5SGreg Kroah-Hartman 	/* There is an outstanding reference here so this is safe */
49596fd7ce5SGreg Kroah-Hartman 	old = tty_ldisc_get(old->ops->num);
49696fd7ce5SGreg Kroah-Hartman 	WARN_ON(IS_ERR(old));
49796fd7ce5SGreg Kroah-Hartman 	tty_ldisc_assign(tty, old);
49896fd7ce5SGreg Kroah-Hartman 	tty_set_termios_ldisc(tty, old->ops->num);
49996fd7ce5SGreg Kroah-Hartman 	if (tty_ldisc_open(tty, old) < 0) {
50096fd7ce5SGreg Kroah-Hartman 		tty_ldisc_put(old);
50196fd7ce5SGreg Kroah-Hartman 		/* This driver is always present */
50296fd7ce5SGreg Kroah-Hartman 		new_ldisc = tty_ldisc_get(N_TTY);
50396fd7ce5SGreg Kroah-Hartman 		if (IS_ERR(new_ldisc))
50496fd7ce5SGreg Kroah-Hartman 			panic("n_tty: get");
50596fd7ce5SGreg Kroah-Hartman 		tty_ldisc_assign(tty, new_ldisc);
50696fd7ce5SGreg Kroah-Hartman 		tty_set_termios_ldisc(tty, N_TTY);
50796fd7ce5SGreg Kroah-Hartman 		r = tty_ldisc_open(tty, new_ldisc);
50896fd7ce5SGreg Kroah-Hartman 		if (r < 0)
50996fd7ce5SGreg Kroah-Hartman 			panic("Couldn't open N_TTY ldisc for "
51096fd7ce5SGreg Kroah-Hartman 			      "%s --- error %d.",
51196fd7ce5SGreg Kroah-Hartman 			      tty_name(tty, buf), r);
51296fd7ce5SGreg Kroah-Hartman 	}
51396fd7ce5SGreg Kroah-Hartman }
51496fd7ce5SGreg Kroah-Hartman 
51596fd7ce5SGreg Kroah-Hartman /**
51696fd7ce5SGreg Kroah-Hartman  *	tty_ldisc_halt		-	shut down the line discipline
51796fd7ce5SGreg Kroah-Hartman  *	@tty: tty device
51896fd7ce5SGreg Kroah-Hartman  *
51996fd7ce5SGreg Kroah-Hartman  *	Shut down the line discipline and work queue for this tty device.
52096fd7ce5SGreg Kroah-Hartman  *	The TTY_LDISC flag being cleared ensures no further references can
52196fd7ce5SGreg Kroah-Hartman  *	be obtained while the delayed work queue halt ensures that no more
52296fd7ce5SGreg Kroah-Hartman  *	data is fed to the ldisc.
52396fd7ce5SGreg Kroah-Hartman  *
52496fd7ce5SGreg Kroah-Hartman  *	You need to do a 'flush_scheduled_work()' (outside the ldisc_mutex)
52596fd7ce5SGreg Kroah-Hartman  *	in order to make sure any currently executing ldisc work is also
52696fd7ce5SGreg Kroah-Hartman  *	flushed.
52796fd7ce5SGreg Kroah-Hartman  */
52896fd7ce5SGreg Kroah-Hartman 
52996fd7ce5SGreg Kroah-Hartman static int tty_ldisc_halt(struct tty_struct *tty)
53096fd7ce5SGreg Kroah-Hartman {
53196fd7ce5SGreg Kroah-Hartman 	clear_bit(TTY_LDISC, &tty->flags);
53296fd7ce5SGreg Kroah-Hartman 	return cancel_delayed_work_sync(&tty->buf.work);
53396fd7ce5SGreg Kroah-Hartman }
53496fd7ce5SGreg Kroah-Hartman 
53596fd7ce5SGreg Kroah-Hartman /**
536100eeae2SJiri Slaby  *	tty_ldisc_wait_idle	-	wait for the ldisc to become idle
537100eeae2SJiri Slaby  *	@tty: tty to wait for
538100eeae2SJiri Slaby  *
539100eeae2SJiri Slaby  *	Wait for the line discipline to become idle. The discipline must
540100eeae2SJiri Slaby  *	have been halted for this to guarantee it remains idle.
541100eeae2SJiri Slaby  */
542100eeae2SJiri Slaby static int tty_ldisc_wait_idle(struct tty_struct *tty)
543100eeae2SJiri Slaby {
544100eeae2SJiri Slaby 	int ret;
545100eeae2SJiri Slaby 	ret = wait_event_interruptible_timeout(tty_ldisc_idle,
546100eeae2SJiri Slaby 			atomic_read(&tty->ldisc->users) == 1, 5 * HZ);
547100eeae2SJiri Slaby 	if (ret < 0)
548100eeae2SJiri Slaby 		return ret;
549100eeae2SJiri Slaby 	return ret > 0 ? 0 : -EBUSY;
550100eeae2SJiri Slaby }
551100eeae2SJiri Slaby 
552100eeae2SJiri Slaby /**
55396fd7ce5SGreg Kroah-Hartman  *	tty_set_ldisc		-	set line discipline
55496fd7ce5SGreg Kroah-Hartman  *	@tty: the terminal to set
55596fd7ce5SGreg Kroah-Hartman  *	@ldisc: the line discipline
55696fd7ce5SGreg Kroah-Hartman  *
55796fd7ce5SGreg Kroah-Hartman  *	Set the discipline of a tty line. Must be called from a process
55896fd7ce5SGreg Kroah-Hartman  *	context. The ldisc change logic has to protect itself against any
55996fd7ce5SGreg Kroah-Hartman  *	overlapping ldisc change (including on the other end of pty pairs),
56096fd7ce5SGreg Kroah-Hartman  *	the close of one side of a tty/pty pair, and eventually hangup.
56196fd7ce5SGreg Kroah-Hartman  *
56296fd7ce5SGreg Kroah-Hartman  *	Locking: takes tty_ldisc_lock, termios_mutex
56396fd7ce5SGreg Kroah-Hartman  */
56496fd7ce5SGreg Kroah-Hartman 
56596fd7ce5SGreg Kroah-Hartman int tty_set_ldisc(struct tty_struct *tty, int ldisc)
56696fd7ce5SGreg Kroah-Hartman {
56796fd7ce5SGreg Kroah-Hartman 	int retval;
56896fd7ce5SGreg Kroah-Hartman 	struct tty_ldisc *o_ldisc, *new_ldisc;
56996fd7ce5SGreg Kroah-Hartman 	int work, o_work = 0;
57096fd7ce5SGreg Kroah-Hartman 	struct tty_struct *o_tty;
57196fd7ce5SGreg Kroah-Hartman 
57296fd7ce5SGreg Kroah-Hartman 	new_ldisc = tty_ldisc_get(ldisc);
57396fd7ce5SGreg Kroah-Hartman 	if (IS_ERR(new_ldisc))
57496fd7ce5SGreg Kroah-Hartman 		return PTR_ERR(new_ldisc);
57596fd7ce5SGreg Kroah-Hartman 
57696fd7ce5SGreg Kroah-Hartman 	tty_lock();
57796fd7ce5SGreg Kroah-Hartman 	/*
57896fd7ce5SGreg Kroah-Hartman 	 *	We need to look at the tty locking here for pty/tty pairs
57996fd7ce5SGreg Kroah-Hartman 	 *	when both sides try to change in parallel.
58096fd7ce5SGreg Kroah-Hartman 	 */
58196fd7ce5SGreg Kroah-Hartman 
58296fd7ce5SGreg Kroah-Hartman 	o_tty = tty->link;	/* o_tty is the pty side or NULL */
58396fd7ce5SGreg Kroah-Hartman 
58496fd7ce5SGreg Kroah-Hartman 
58596fd7ce5SGreg Kroah-Hartman 	/*
58696fd7ce5SGreg Kroah-Hartman 	 *	Check the no-op case
58796fd7ce5SGreg Kroah-Hartman 	 */
58896fd7ce5SGreg Kroah-Hartman 
58996fd7ce5SGreg Kroah-Hartman 	if (tty->ldisc->ops->num == ldisc) {
59096fd7ce5SGreg Kroah-Hartman 		tty_unlock();
59196fd7ce5SGreg Kroah-Hartman 		tty_ldisc_put(new_ldisc);
59296fd7ce5SGreg Kroah-Hartman 		return 0;
59396fd7ce5SGreg Kroah-Hartman 	}
59496fd7ce5SGreg Kroah-Hartman 
59596fd7ce5SGreg Kroah-Hartman 	tty_unlock();
59696fd7ce5SGreg Kroah-Hartman 	/*
59796fd7ce5SGreg Kroah-Hartman 	 *	Problem: What do we do if this blocks ?
59896fd7ce5SGreg Kroah-Hartman 	 *	We could deadlock here
59996fd7ce5SGreg Kroah-Hartman 	 */
60096fd7ce5SGreg Kroah-Hartman 
60196fd7ce5SGreg Kroah-Hartman 	tty_wait_until_sent(tty, 0);
60296fd7ce5SGreg Kroah-Hartman 
60396fd7ce5SGreg Kroah-Hartman 	tty_lock();
60496fd7ce5SGreg Kroah-Hartman 	mutex_lock(&tty->ldisc_mutex);
60596fd7ce5SGreg Kroah-Hartman 
60696fd7ce5SGreg Kroah-Hartman 	/*
60796fd7ce5SGreg Kroah-Hartman 	 *	We could be midstream of another ldisc change which has
60896fd7ce5SGreg Kroah-Hartman 	 *	dropped the lock during processing. If so we need to wait.
60996fd7ce5SGreg Kroah-Hartman 	 */
61096fd7ce5SGreg Kroah-Hartman 
61196fd7ce5SGreg Kroah-Hartman 	while (test_bit(TTY_LDISC_CHANGING, &tty->flags)) {
61296fd7ce5SGreg Kroah-Hartman 		mutex_unlock(&tty->ldisc_mutex);
61396fd7ce5SGreg Kroah-Hartman 		tty_unlock();
61496fd7ce5SGreg Kroah-Hartman 		wait_event(tty_ldisc_wait,
61596fd7ce5SGreg Kroah-Hartman 			test_bit(TTY_LDISC_CHANGING, &tty->flags) == 0);
61696fd7ce5SGreg Kroah-Hartman 		tty_lock();
61796fd7ce5SGreg Kroah-Hartman 		mutex_lock(&tty->ldisc_mutex);
61896fd7ce5SGreg Kroah-Hartman 	}
61996fd7ce5SGreg Kroah-Hartman 
62096fd7ce5SGreg Kroah-Hartman 	set_bit(TTY_LDISC_CHANGING, &tty->flags);
62196fd7ce5SGreg Kroah-Hartman 
62296fd7ce5SGreg Kroah-Hartman 	/*
62396fd7ce5SGreg Kroah-Hartman 	 *	No more input please, we are switching. The new ldisc
62496fd7ce5SGreg Kroah-Hartman 	 *	will update this value in the ldisc open function
62596fd7ce5SGreg Kroah-Hartman 	 */
62696fd7ce5SGreg Kroah-Hartman 
62796fd7ce5SGreg Kroah-Hartman 	tty->receive_room = 0;
62896fd7ce5SGreg Kroah-Hartman 
62996fd7ce5SGreg Kroah-Hartman 	o_ldisc = tty->ldisc;
63096fd7ce5SGreg Kroah-Hartman 
63196fd7ce5SGreg Kroah-Hartman 	tty_unlock();
63296fd7ce5SGreg Kroah-Hartman 	/*
63396fd7ce5SGreg Kroah-Hartman 	 *	Make sure we don't change while someone holds a
63496fd7ce5SGreg Kroah-Hartman 	 *	reference to the line discipline. The TTY_LDISC bit
63596fd7ce5SGreg Kroah-Hartman 	 *	prevents anyone taking a reference once it is clear.
63696fd7ce5SGreg Kroah-Hartman 	 *	We need the lock to avoid racing reference takers.
63796fd7ce5SGreg Kroah-Hartman 	 *
63896fd7ce5SGreg Kroah-Hartman 	 *	We must clear the TTY_LDISC bit here to avoid a livelock
63996fd7ce5SGreg Kroah-Hartman 	 *	with a userspace app continually trying to use the tty in
64096fd7ce5SGreg Kroah-Hartman 	 *	parallel to the change and re-referencing the tty.
64196fd7ce5SGreg Kroah-Hartman 	 */
64296fd7ce5SGreg Kroah-Hartman 
64396fd7ce5SGreg Kroah-Hartman 	work = tty_ldisc_halt(tty);
64496fd7ce5SGreg Kroah-Hartman 	if (o_tty)
64596fd7ce5SGreg Kroah-Hartman 		o_work = tty_ldisc_halt(o_tty);
64696fd7ce5SGreg Kroah-Hartman 
64796fd7ce5SGreg Kroah-Hartman 	/*
64896fd7ce5SGreg Kroah-Hartman 	 * Wait for ->hangup_work and ->buf.work handlers to terminate.
64996fd7ce5SGreg Kroah-Hartman 	 * We must drop the mutex here in case a hangup is also in process.
65096fd7ce5SGreg Kroah-Hartman 	 */
65196fd7ce5SGreg Kroah-Hartman 
65296fd7ce5SGreg Kroah-Hartman 	mutex_unlock(&tty->ldisc_mutex);
65396fd7ce5SGreg Kroah-Hartman 
65496fd7ce5SGreg Kroah-Hartman 	flush_scheduled_work();
65596fd7ce5SGreg Kroah-Hartman 
656100eeae2SJiri Slaby 	retval = tty_ldisc_wait_idle(tty);
657100eeae2SJiri Slaby 
65896fd7ce5SGreg Kroah-Hartman 	tty_lock();
65996fd7ce5SGreg Kroah-Hartman 	mutex_lock(&tty->ldisc_mutex);
660100eeae2SJiri Slaby 
661100eeae2SJiri Slaby 	/* handle wait idle failure locked */
662100eeae2SJiri Slaby 	if (retval) {
663100eeae2SJiri Slaby 		tty_ldisc_put(new_ldisc);
664100eeae2SJiri Slaby 		goto enable;
665100eeae2SJiri Slaby 	}
666100eeae2SJiri Slaby 
66796fd7ce5SGreg Kroah-Hartman 	if (test_bit(TTY_HUPPED, &tty->flags)) {
66896fd7ce5SGreg Kroah-Hartman 		/* We were raced by the hangup method. It will have stomped
66996fd7ce5SGreg Kroah-Hartman 		   the ldisc data and closed the ldisc down */
67096fd7ce5SGreg Kroah-Hartman 		clear_bit(TTY_LDISC_CHANGING, &tty->flags);
67196fd7ce5SGreg Kroah-Hartman 		mutex_unlock(&tty->ldisc_mutex);
67296fd7ce5SGreg Kroah-Hartman 		tty_ldisc_put(new_ldisc);
67396fd7ce5SGreg Kroah-Hartman 		tty_unlock();
67496fd7ce5SGreg Kroah-Hartman 		return -EIO;
67596fd7ce5SGreg Kroah-Hartman 	}
67696fd7ce5SGreg Kroah-Hartman 
67796fd7ce5SGreg Kroah-Hartman 	/* Shutdown the current discipline. */
67896fd7ce5SGreg Kroah-Hartman 	tty_ldisc_close(tty, o_ldisc);
67996fd7ce5SGreg Kroah-Hartman 
68096fd7ce5SGreg Kroah-Hartman 	/* Now set up the new line discipline. */
68196fd7ce5SGreg Kroah-Hartman 	tty_ldisc_assign(tty, new_ldisc);
68296fd7ce5SGreg Kroah-Hartman 	tty_set_termios_ldisc(tty, ldisc);
68396fd7ce5SGreg Kroah-Hartman 
68496fd7ce5SGreg Kroah-Hartman 	retval = tty_ldisc_open(tty, new_ldisc);
68596fd7ce5SGreg Kroah-Hartman 	if (retval < 0) {
68696fd7ce5SGreg Kroah-Hartman 		/* Back to the old one or N_TTY if we can't */
68796fd7ce5SGreg Kroah-Hartman 		tty_ldisc_put(new_ldisc);
68896fd7ce5SGreg Kroah-Hartman 		tty_ldisc_restore(tty, o_ldisc);
68996fd7ce5SGreg Kroah-Hartman 	}
69096fd7ce5SGreg Kroah-Hartman 
69196fd7ce5SGreg Kroah-Hartman 	/* At this point we hold a reference to the new ldisc and a
69296fd7ce5SGreg Kroah-Hartman 	   a reference to the old ldisc. If we ended up flipping back
69396fd7ce5SGreg Kroah-Hartman 	   to the existing ldisc we have two references to it */
69496fd7ce5SGreg Kroah-Hartman 
69596fd7ce5SGreg Kroah-Hartman 	if (tty->ldisc->ops->num != o_ldisc->ops->num && tty->ops->set_ldisc)
69696fd7ce5SGreg Kroah-Hartman 		tty->ops->set_ldisc(tty);
69796fd7ce5SGreg Kroah-Hartman 
69896fd7ce5SGreg Kroah-Hartman 	tty_ldisc_put(o_ldisc);
69996fd7ce5SGreg Kroah-Hartman 
700100eeae2SJiri Slaby enable:
70196fd7ce5SGreg Kroah-Hartman 	/*
70296fd7ce5SGreg Kroah-Hartman 	 *	Allow ldisc referencing to occur again
70396fd7ce5SGreg Kroah-Hartman 	 */
70496fd7ce5SGreg Kroah-Hartman 
70596fd7ce5SGreg Kroah-Hartman 	tty_ldisc_enable(tty);
70696fd7ce5SGreg Kroah-Hartman 	if (o_tty)
70796fd7ce5SGreg Kroah-Hartman 		tty_ldisc_enable(o_tty);
70896fd7ce5SGreg Kroah-Hartman 
70996fd7ce5SGreg Kroah-Hartman 	/* Restart the work queue in case no characters kick it off. Safe if
71096fd7ce5SGreg Kroah-Hartman 	   already running */
71196fd7ce5SGreg Kroah-Hartman 	if (work)
71296fd7ce5SGreg Kroah-Hartman 		schedule_delayed_work(&tty->buf.work, 1);
71396fd7ce5SGreg Kroah-Hartman 	if (o_work)
71496fd7ce5SGreg Kroah-Hartman 		schedule_delayed_work(&o_tty->buf.work, 1);
71596fd7ce5SGreg Kroah-Hartman 	mutex_unlock(&tty->ldisc_mutex);
71696fd7ce5SGreg Kroah-Hartman 	tty_unlock();
71796fd7ce5SGreg Kroah-Hartman 	return retval;
71896fd7ce5SGreg Kroah-Hartman }
71996fd7ce5SGreg Kroah-Hartman 
72096fd7ce5SGreg Kroah-Hartman /**
72196fd7ce5SGreg Kroah-Hartman  *	tty_reset_termios	-	reset terminal state
72296fd7ce5SGreg Kroah-Hartman  *	@tty: tty to reset
72396fd7ce5SGreg Kroah-Hartman  *
72496fd7ce5SGreg Kroah-Hartman  *	Restore a terminal to the driver default state.
72596fd7ce5SGreg Kroah-Hartman  */
72696fd7ce5SGreg Kroah-Hartman 
72796fd7ce5SGreg Kroah-Hartman static void tty_reset_termios(struct tty_struct *tty)
72896fd7ce5SGreg Kroah-Hartman {
72996fd7ce5SGreg Kroah-Hartman 	mutex_lock(&tty->termios_mutex);
73096fd7ce5SGreg Kroah-Hartman 	*tty->termios = tty->driver->init_termios;
73196fd7ce5SGreg Kroah-Hartman 	tty->termios->c_ispeed = tty_termios_input_baud_rate(tty->termios);
73296fd7ce5SGreg Kroah-Hartman 	tty->termios->c_ospeed = tty_termios_baud_rate(tty->termios);
73396fd7ce5SGreg Kroah-Hartman 	mutex_unlock(&tty->termios_mutex);
73496fd7ce5SGreg Kroah-Hartman }
73596fd7ce5SGreg Kroah-Hartman 
73696fd7ce5SGreg Kroah-Hartman 
73796fd7ce5SGreg Kroah-Hartman /**
73896fd7ce5SGreg Kroah-Hartman  *	tty_ldisc_reinit	-	reinitialise the tty ldisc
73996fd7ce5SGreg Kroah-Hartman  *	@tty: tty to reinit
74096fd7ce5SGreg Kroah-Hartman  *	@ldisc: line discipline to reinitialize
74196fd7ce5SGreg Kroah-Hartman  *
74296fd7ce5SGreg Kroah-Hartman  *	Switch the tty to a line discipline and leave the ldisc
74396fd7ce5SGreg Kroah-Hartman  *	state closed
74496fd7ce5SGreg Kroah-Hartman  */
74596fd7ce5SGreg Kroah-Hartman 
74696fd7ce5SGreg Kroah-Hartman static void tty_ldisc_reinit(struct tty_struct *tty, int ldisc)
74796fd7ce5SGreg Kroah-Hartman {
74896fd7ce5SGreg Kroah-Hartman 	struct tty_ldisc *ld;
74996fd7ce5SGreg Kroah-Hartman 
75096fd7ce5SGreg Kroah-Hartman 	tty_ldisc_close(tty, tty->ldisc);
75196fd7ce5SGreg Kroah-Hartman 	tty_ldisc_put(tty->ldisc);
75296fd7ce5SGreg Kroah-Hartman 	tty->ldisc = NULL;
75396fd7ce5SGreg Kroah-Hartman 	/*
75496fd7ce5SGreg Kroah-Hartman 	 *	Switch the line discipline back
75596fd7ce5SGreg Kroah-Hartman 	 */
75696fd7ce5SGreg Kroah-Hartman 	ld = tty_ldisc_get(ldisc);
75796fd7ce5SGreg Kroah-Hartman 	BUG_ON(IS_ERR(ld));
75896fd7ce5SGreg Kroah-Hartman 	tty_ldisc_assign(tty, ld);
75996fd7ce5SGreg Kroah-Hartman 	tty_set_termios_ldisc(tty, ldisc);
76096fd7ce5SGreg Kroah-Hartman }
76196fd7ce5SGreg Kroah-Hartman 
76296fd7ce5SGreg Kroah-Hartman /**
76396fd7ce5SGreg Kroah-Hartman  *	tty_ldisc_hangup		-	hangup ldisc reset
76496fd7ce5SGreg Kroah-Hartman  *	@tty: tty being hung up
76596fd7ce5SGreg Kroah-Hartman  *
76696fd7ce5SGreg Kroah-Hartman  *	Some tty devices reset their termios when they receive a hangup
76796fd7ce5SGreg Kroah-Hartman  *	event. In that situation we must also switch back to N_TTY properly
76896fd7ce5SGreg Kroah-Hartman  *	before we reset the termios data.
76996fd7ce5SGreg Kroah-Hartman  *
77096fd7ce5SGreg Kroah-Hartman  *	Locking: We can take the ldisc mutex as the rest of the code is
77196fd7ce5SGreg Kroah-Hartman  *	careful to allow for this.
77296fd7ce5SGreg Kroah-Hartman  *
77396fd7ce5SGreg Kroah-Hartman  *	In the pty pair case this occurs in the close() path of the
77496fd7ce5SGreg Kroah-Hartman  *	tty itself so we must be careful about locking rules.
77596fd7ce5SGreg Kroah-Hartman  */
77696fd7ce5SGreg Kroah-Hartman 
77796fd7ce5SGreg Kroah-Hartman void tty_ldisc_hangup(struct tty_struct *tty)
77896fd7ce5SGreg Kroah-Hartman {
77996fd7ce5SGreg Kroah-Hartman 	struct tty_ldisc *ld;
78096fd7ce5SGreg Kroah-Hartman 	int reset = tty->driver->flags & TTY_DRIVER_RESET_TERMIOS;
78196fd7ce5SGreg Kroah-Hartman 	int err = 0;
78296fd7ce5SGreg Kroah-Hartman 
78396fd7ce5SGreg Kroah-Hartman 	/*
78496fd7ce5SGreg Kroah-Hartman 	 * FIXME! What are the locking issues here? This may me overdoing
78596fd7ce5SGreg Kroah-Hartman 	 * things... This question is especially important now that we've
78696fd7ce5SGreg Kroah-Hartman 	 * removed the irqlock.
78796fd7ce5SGreg Kroah-Hartman 	 */
78896fd7ce5SGreg Kroah-Hartman 	ld = tty_ldisc_ref(tty);
78996fd7ce5SGreg Kroah-Hartman 	if (ld != NULL) {
79096fd7ce5SGreg Kroah-Hartman 		/* We may have no line discipline at this point */
79196fd7ce5SGreg Kroah-Hartman 		if (ld->ops->flush_buffer)
79296fd7ce5SGreg Kroah-Hartman 			ld->ops->flush_buffer(tty);
79396fd7ce5SGreg Kroah-Hartman 		tty_driver_flush_buffer(tty);
79496fd7ce5SGreg Kroah-Hartman 		if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
79596fd7ce5SGreg Kroah-Hartman 		    ld->ops->write_wakeup)
79696fd7ce5SGreg Kroah-Hartman 			ld->ops->write_wakeup(tty);
79796fd7ce5SGreg Kroah-Hartman 		if (ld->ops->hangup)
79896fd7ce5SGreg Kroah-Hartman 			ld->ops->hangup(tty);
79996fd7ce5SGreg Kroah-Hartman 		tty_ldisc_deref(ld);
80096fd7ce5SGreg Kroah-Hartman 	}
80196fd7ce5SGreg Kroah-Hartman 	/*
80296fd7ce5SGreg Kroah-Hartman 	 * FIXME: Once we trust the LDISC code better we can wait here for
80396fd7ce5SGreg Kroah-Hartman 	 * ldisc completion and fix the driver call race
80496fd7ce5SGreg Kroah-Hartman 	 */
80596fd7ce5SGreg Kroah-Hartman 	wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
80696fd7ce5SGreg Kroah-Hartman 	wake_up_interruptible_poll(&tty->read_wait, POLLIN);
80796fd7ce5SGreg Kroah-Hartman 	/*
80896fd7ce5SGreg Kroah-Hartman 	 * Shutdown the current line discipline, and reset it to
80996fd7ce5SGreg Kroah-Hartman 	 * N_TTY if need be.
81096fd7ce5SGreg Kroah-Hartman 	 *
81196fd7ce5SGreg Kroah-Hartman 	 * Avoid racing set_ldisc or tty_ldisc_release
81296fd7ce5SGreg Kroah-Hartman 	 */
81396fd7ce5SGreg Kroah-Hartman 	mutex_lock(&tty->ldisc_mutex);
81496fd7ce5SGreg Kroah-Hartman 
81596fd7ce5SGreg Kroah-Hartman 	/*
81696fd7ce5SGreg Kroah-Hartman 	 * this is like tty_ldisc_halt, but we need to give up
81796fd7ce5SGreg Kroah-Hartman 	 * the BTM before calling cancel_delayed_work_sync,
81896fd7ce5SGreg Kroah-Hartman 	 * which may need to wait for another function taking the BTM
81996fd7ce5SGreg Kroah-Hartman 	 */
82096fd7ce5SGreg Kroah-Hartman 	clear_bit(TTY_LDISC, &tty->flags);
82196fd7ce5SGreg Kroah-Hartman 	tty_unlock();
82296fd7ce5SGreg Kroah-Hartman 	cancel_delayed_work_sync(&tty->buf.work);
82396fd7ce5SGreg Kroah-Hartman 	mutex_unlock(&tty->ldisc_mutex);
82496fd7ce5SGreg Kroah-Hartman 
82596fd7ce5SGreg Kroah-Hartman 	tty_lock();
82696fd7ce5SGreg Kroah-Hartman 	mutex_lock(&tty->ldisc_mutex);
82796fd7ce5SGreg Kroah-Hartman 
82896fd7ce5SGreg Kroah-Hartman 	/* At this point we have a closed ldisc and we want to
82996fd7ce5SGreg Kroah-Hartman 	   reopen it. We could defer this to the next open but
83096fd7ce5SGreg Kroah-Hartman 	   it means auditing a lot of other paths so this is
83196fd7ce5SGreg Kroah-Hartman 	   a FIXME */
83296fd7ce5SGreg Kroah-Hartman 	if (tty->ldisc) {	/* Not yet closed */
83396fd7ce5SGreg Kroah-Hartman 		if (reset == 0) {
83496fd7ce5SGreg Kroah-Hartman 			tty_ldisc_reinit(tty, tty->termios->c_line);
83596fd7ce5SGreg Kroah-Hartman 			err = tty_ldisc_open(tty, tty->ldisc);
83696fd7ce5SGreg Kroah-Hartman 		}
83796fd7ce5SGreg Kroah-Hartman 		/* If the re-open fails or we reset then go to N_TTY. The
83896fd7ce5SGreg Kroah-Hartman 		   N_TTY open cannot fail */
83996fd7ce5SGreg Kroah-Hartman 		if (reset || err) {
84096fd7ce5SGreg Kroah-Hartman 			tty_ldisc_reinit(tty, N_TTY);
84196fd7ce5SGreg Kroah-Hartman 			WARN_ON(tty_ldisc_open(tty, tty->ldisc));
84296fd7ce5SGreg Kroah-Hartman 		}
84396fd7ce5SGreg Kroah-Hartman 		tty_ldisc_enable(tty);
84496fd7ce5SGreg Kroah-Hartman 	}
84596fd7ce5SGreg Kroah-Hartman 	mutex_unlock(&tty->ldisc_mutex);
84696fd7ce5SGreg Kroah-Hartman 	if (reset)
84796fd7ce5SGreg Kroah-Hartman 		tty_reset_termios(tty);
84896fd7ce5SGreg Kroah-Hartman }
84996fd7ce5SGreg Kroah-Hartman 
85096fd7ce5SGreg Kroah-Hartman /**
85196fd7ce5SGreg Kroah-Hartman  *	tty_ldisc_setup			-	open line discipline
85296fd7ce5SGreg Kroah-Hartman  *	@tty: tty being shut down
85396fd7ce5SGreg Kroah-Hartman  *	@o_tty: pair tty for pty/tty pairs
85496fd7ce5SGreg Kroah-Hartman  *
85596fd7ce5SGreg Kroah-Hartman  *	Called during the initial open of a tty/pty pair in order to set up the
85696fd7ce5SGreg Kroah-Hartman  *	line disciplines and bind them to the tty. This has no locking issues
85796fd7ce5SGreg Kroah-Hartman  *	as the device isn't yet active.
85896fd7ce5SGreg Kroah-Hartman  */
85996fd7ce5SGreg Kroah-Hartman 
86096fd7ce5SGreg Kroah-Hartman int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty)
86196fd7ce5SGreg Kroah-Hartman {
86296fd7ce5SGreg Kroah-Hartman 	struct tty_ldisc *ld = tty->ldisc;
86396fd7ce5SGreg Kroah-Hartman 	int retval;
86496fd7ce5SGreg Kroah-Hartman 
86596fd7ce5SGreg Kroah-Hartman 	retval = tty_ldisc_open(tty, ld);
86696fd7ce5SGreg Kroah-Hartman 	if (retval)
86796fd7ce5SGreg Kroah-Hartman 		return retval;
86896fd7ce5SGreg Kroah-Hartman 
86996fd7ce5SGreg Kroah-Hartman 	if (o_tty) {
87096fd7ce5SGreg Kroah-Hartman 		retval = tty_ldisc_open(o_tty, o_tty->ldisc);
87196fd7ce5SGreg Kroah-Hartman 		if (retval) {
87296fd7ce5SGreg Kroah-Hartman 			tty_ldisc_close(tty, ld);
87396fd7ce5SGreg Kroah-Hartman 			return retval;
87496fd7ce5SGreg Kroah-Hartman 		}
87596fd7ce5SGreg Kroah-Hartman 		tty_ldisc_enable(o_tty);
87696fd7ce5SGreg Kroah-Hartman 	}
87796fd7ce5SGreg Kroah-Hartman 	tty_ldisc_enable(tty);
87896fd7ce5SGreg Kroah-Hartman 	return 0;
87996fd7ce5SGreg Kroah-Hartman }
88096fd7ce5SGreg Kroah-Hartman /**
88196fd7ce5SGreg Kroah-Hartman  *	tty_ldisc_release		-	release line discipline
88296fd7ce5SGreg Kroah-Hartman  *	@tty: tty being shut down
88396fd7ce5SGreg Kroah-Hartman  *	@o_tty: pair tty for pty/tty pairs
88496fd7ce5SGreg Kroah-Hartman  *
88596fd7ce5SGreg Kroah-Hartman  *	Called during the final close of a tty/pty pair in order to shut down
88696fd7ce5SGreg Kroah-Hartman  *	the line discpline layer. On exit the ldisc assigned is N_TTY and the
88796fd7ce5SGreg Kroah-Hartman  *	ldisc has not been opened.
88896fd7ce5SGreg Kroah-Hartman  */
88996fd7ce5SGreg Kroah-Hartman 
89096fd7ce5SGreg Kroah-Hartman void tty_ldisc_release(struct tty_struct *tty, struct tty_struct *o_tty)
89196fd7ce5SGreg Kroah-Hartman {
89296fd7ce5SGreg Kroah-Hartman 	/*
89396fd7ce5SGreg Kroah-Hartman 	 * Prevent flush_to_ldisc() from rescheduling the work for later.  Then
89496fd7ce5SGreg Kroah-Hartman 	 * kill any delayed work. As this is the final close it does not
89596fd7ce5SGreg Kroah-Hartman 	 * race with the set_ldisc code path.
89696fd7ce5SGreg Kroah-Hartman 	 */
89796fd7ce5SGreg Kroah-Hartman 
89896fd7ce5SGreg Kroah-Hartman 	tty_unlock();
89996fd7ce5SGreg Kroah-Hartman 	tty_ldisc_halt(tty);
90096fd7ce5SGreg Kroah-Hartman 	flush_scheduled_work();
90196fd7ce5SGreg Kroah-Hartman 	tty_lock();
90296fd7ce5SGreg Kroah-Hartman 
90396fd7ce5SGreg Kroah-Hartman 	mutex_lock(&tty->ldisc_mutex);
90496fd7ce5SGreg Kroah-Hartman 	/*
90596fd7ce5SGreg Kroah-Hartman 	 * Now kill off the ldisc
90696fd7ce5SGreg Kroah-Hartman 	 */
90796fd7ce5SGreg Kroah-Hartman 	tty_ldisc_close(tty, tty->ldisc);
90896fd7ce5SGreg Kroah-Hartman 	tty_ldisc_put(tty->ldisc);
90996fd7ce5SGreg Kroah-Hartman 	/* Force an oops if we mess this up */
91096fd7ce5SGreg Kroah-Hartman 	tty->ldisc = NULL;
91196fd7ce5SGreg Kroah-Hartman 
91296fd7ce5SGreg Kroah-Hartman 	/* Ensure the next open requests the N_TTY ldisc */
91396fd7ce5SGreg Kroah-Hartman 	tty_set_termios_ldisc(tty, N_TTY);
91496fd7ce5SGreg Kroah-Hartman 	mutex_unlock(&tty->ldisc_mutex);
91596fd7ce5SGreg Kroah-Hartman 
91696fd7ce5SGreg Kroah-Hartman 	/* This will need doing differently if we need to lock */
91796fd7ce5SGreg Kroah-Hartman 	if (o_tty)
91896fd7ce5SGreg Kroah-Hartman 		tty_ldisc_release(o_tty, NULL);
91996fd7ce5SGreg Kroah-Hartman 
92096fd7ce5SGreg Kroah-Hartman 	/* And the memory resources remaining (buffers, termios) will be
92196fd7ce5SGreg Kroah-Hartman 	   disposed of when the kref hits zero */
92296fd7ce5SGreg Kroah-Hartman }
92396fd7ce5SGreg Kroah-Hartman 
92496fd7ce5SGreg Kroah-Hartman /**
92596fd7ce5SGreg Kroah-Hartman  *	tty_ldisc_init		-	ldisc setup for new tty
92696fd7ce5SGreg Kroah-Hartman  *	@tty: tty being allocated
92796fd7ce5SGreg Kroah-Hartman  *
92896fd7ce5SGreg Kroah-Hartman  *	Set up the line discipline objects for a newly allocated tty. Note that
92996fd7ce5SGreg Kroah-Hartman  *	the tty structure is not completely set up when this call is made.
93096fd7ce5SGreg Kroah-Hartman  */
93196fd7ce5SGreg Kroah-Hartman 
93296fd7ce5SGreg Kroah-Hartman void tty_ldisc_init(struct tty_struct *tty)
93396fd7ce5SGreg Kroah-Hartman {
93496fd7ce5SGreg Kroah-Hartman 	struct tty_ldisc *ld = tty_ldisc_get(N_TTY);
93596fd7ce5SGreg Kroah-Hartman 	if (IS_ERR(ld))
93696fd7ce5SGreg Kroah-Hartman 		panic("n_tty: init_tty");
93796fd7ce5SGreg Kroah-Hartman 	tty_ldisc_assign(tty, ld);
93896fd7ce5SGreg Kroah-Hartman }
93996fd7ce5SGreg Kroah-Hartman 
94096fd7ce5SGreg Kroah-Hartman void tty_ldisc_begin(void)
94196fd7ce5SGreg Kroah-Hartman {
94296fd7ce5SGreg Kroah-Hartman 	/* Setup the default TTY line discipline. */
94396fd7ce5SGreg Kroah-Hartman 	(void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);
94496fd7ce5SGreg Kroah-Hartman }
945