xref: /openbmc/linux/drivers/tty/tty_ldisc.c (revision e65c62b1)
196fd7ce5SGreg Kroah-Hartman #include <linux/types.h>
296fd7ce5SGreg Kroah-Hartman #include <linux/errno.h>
38b3ffa17SJiri Slaby #include <linux/kmod.h>
496fd7ce5SGreg Kroah-Hartman #include <linux/sched.h>
596fd7ce5SGreg Kroah-Hartman #include <linux/interrupt.h>
696fd7ce5SGreg Kroah-Hartman #include <linux/tty.h>
796fd7ce5SGreg Kroah-Hartman #include <linux/tty_driver.h>
896fd7ce5SGreg Kroah-Hartman #include <linux/file.h>
996fd7ce5SGreg Kroah-Hartman #include <linux/mm.h>
1096fd7ce5SGreg Kroah-Hartman #include <linux/string.h>
1196fd7ce5SGreg Kroah-Hartman #include <linux/slab.h>
1296fd7ce5SGreg Kroah-Hartman #include <linux/poll.h>
1396fd7ce5SGreg Kroah-Hartman #include <linux/proc_fs.h>
1496fd7ce5SGreg Kroah-Hartman #include <linux/module.h>
1596fd7ce5SGreg Kroah-Hartman #include <linux/device.h>
1696fd7ce5SGreg Kroah-Hartman #include <linux/wait.h>
1796fd7ce5SGreg Kroah-Hartman #include <linux/bitops.h>
1896fd7ce5SGreg Kroah-Hartman #include <linux/seq_file.h>
1996fd7ce5SGreg Kroah-Hartman #include <linux/uaccess.h>
200c73c08eSJiri Slaby #include <linux/ratelimit.h>
2196fd7ce5SGreg Kroah-Hartman 
22fc575ee6SPeter Hurley #undef LDISC_DEBUG_HANGUP
23fc575ee6SPeter Hurley 
24fc575ee6SPeter Hurley #ifdef LDISC_DEBUG_HANGUP
250a6adc13SPeter Hurley #define tty_ldisc_debug(tty, f, args...)	tty_debug(tty, f, ##args)
26fc575ee6SPeter Hurley #else
27fc575ee6SPeter Hurley #define tty_ldisc_debug(tty, f, args...)
28fc575ee6SPeter Hurley #endif
29fc575ee6SPeter Hurley 
30d2c43890SPeter Hurley /* lockdep nested classes for tty->ldisc_sem */
31d2c43890SPeter Hurley enum {
32d2c43890SPeter Hurley 	LDISC_SEM_NORMAL,
33d2c43890SPeter Hurley 	LDISC_SEM_OTHER,
34d2c43890SPeter Hurley };
35d2c43890SPeter Hurley 
36d2c43890SPeter Hurley 
3796fd7ce5SGreg Kroah-Hartman /*
3896fd7ce5SGreg Kroah-Hartman  *	This guards the refcounted line discipline lists. The lock
3996fd7ce5SGreg Kroah-Hartman  *	must be taken with irqs off because there are hangup path
4096fd7ce5SGreg Kroah-Hartman  *	callers who will do ldisc lookups and cannot sleep.
4196fd7ce5SGreg Kroah-Hartman  */
4296fd7ce5SGreg Kroah-Hartman 
43137084bbSPeter Hurley static DEFINE_RAW_SPINLOCK(tty_ldiscs_lock);
4496fd7ce5SGreg Kroah-Hartman /* Line disc dispatch table */
4596fd7ce5SGreg Kroah-Hartman static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS];
4696fd7ce5SGreg Kroah-Hartman 
4796fd7ce5SGreg Kroah-Hartman /**
4896fd7ce5SGreg Kroah-Hartman  *	tty_register_ldisc	-	install a line discipline
4996fd7ce5SGreg Kroah-Hartman  *	@disc: ldisc number
5096fd7ce5SGreg Kroah-Hartman  *	@new_ldisc: pointer to the ldisc object
5196fd7ce5SGreg Kroah-Hartman  *
5296fd7ce5SGreg Kroah-Hartman  *	Installs a new line discipline into the kernel. The discipline
5396fd7ce5SGreg Kroah-Hartman  *	is set up as unreferenced and then made available to the kernel
5496fd7ce5SGreg Kroah-Hartman  *	from this point onwards.
5596fd7ce5SGreg Kroah-Hartman  *
5696fd7ce5SGreg Kroah-Hartman  *	Locking:
57137084bbSPeter Hurley  *		takes tty_ldiscs_lock to guard against ldisc races
5896fd7ce5SGreg Kroah-Hartman  */
5996fd7ce5SGreg Kroah-Hartman 
6096fd7ce5SGreg Kroah-Hartman int tty_register_ldisc(int disc, struct tty_ldisc_ops *new_ldisc)
6196fd7ce5SGreg Kroah-Hartman {
6296fd7ce5SGreg Kroah-Hartman 	unsigned long flags;
6396fd7ce5SGreg Kroah-Hartman 	int ret = 0;
6496fd7ce5SGreg Kroah-Hartman 
6596fd7ce5SGreg Kroah-Hartman 	if (disc < N_TTY || disc >= NR_LDISCS)
6696fd7ce5SGreg Kroah-Hartman 		return -EINVAL;
6796fd7ce5SGreg Kroah-Hartman 
68137084bbSPeter Hurley 	raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
6996fd7ce5SGreg Kroah-Hartman 	tty_ldiscs[disc] = new_ldisc;
7096fd7ce5SGreg Kroah-Hartman 	new_ldisc->num = disc;
7196fd7ce5SGreg Kroah-Hartman 	new_ldisc->refcount = 0;
72137084bbSPeter Hurley 	raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
7396fd7ce5SGreg Kroah-Hartman 
7496fd7ce5SGreg Kroah-Hartman 	return ret;
7596fd7ce5SGreg Kroah-Hartman }
7696fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_register_ldisc);
7796fd7ce5SGreg Kroah-Hartman 
7896fd7ce5SGreg Kroah-Hartman /**
7996fd7ce5SGreg Kroah-Hartman  *	tty_unregister_ldisc	-	unload a line discipline
8096fd7ce5SGreg Kroah-Hartman  *	@disc: ldisc number
8196fd7ce5SGreg Kroah-Hartman  *	@new_ldisc: pointer to the ldisc object
8296fd7ce5SGreg Kroah-Hartman  *
8396fd7ce5SGreg Kroah-Hartman  *	Remove a line discipline from the kernel providing it is not
8496fd7ce5SGreg Kroah-Hartman  *	currently in use.
8596fd7ce5SGreg Kroah-Hartman  *
8696fd7ce5SGreg Kroah-Hartman  *	Locking:
87137084bbSPeter Hurley  *		takes tty_ldiscs_lock to guard against ldisc races
8896fd7ce5SGreg Kroah-Hartman  */
8996fd7ce5SGreg Kroah-Hartman 
9096fd7ce5SGreg Kroah-Hartman int tty_unregister_ldisc(int disc)
9196fd7ce5SGreg Kroah-Hartman {
9296fd7ce5SGreg Kroah-Hartman 	unsigned long flags;
9396fd7ce5SGreg Kroah-Hartman 	int ret = 0;
9496fd7ce5SGreg Kroah-Hartman 
9596fd7ce5SGreg Kroah-Hartman 	if (disc < N_TTY || disc >= NR_LDISCS)
9696fd7ce5SGreg Kroah-Hartman 		return -EINVAL;
9796fd7ce5SGreg Kroah-Hartman 
98137084bbSPeter Hurley 	raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
9996fd7ce5SGreg Kroah-Hartman 	if (tty_ldiscs[disc]->refcount)
10096fd7ce5SGreg Kroah-Hartman 		ret = -EBUSY;
10196fd7ce5SGreg Kroah-Hartman 	else
10296fd7ce5SGreg Kroah-Hartman 		tty_ldiscs[disc] = NULL;
103137084bbSPeter Hurley 	raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
10496fd7ce5SGreg Kroah-Hartman 
10596fd7ce5SGreg Kroah-Hartman 	return ret;
10696fd7ce5SGreg Kroah-Hartman }
10796fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_unregister_ldisc);
10896fd7ce5SGreg Kroah-Hartman 
10996fd7ce5SGreg Kroah-Hartman static struct tty_ldisc_ops *get_ldops(int disc)
11096fd7ce5SGreg Kroah-Hartman {
11196fd7ce5SGreg Kroah-Hartman 	unsigned long flags;
11296fd7ce5SGreg Kroah-Hartman 	struct tty_ldisc_ops *ldops, *ret;
11396fd7ce5SGreg Kroah-Hartman 
114137084bbSPeter Hurley 	raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
11596fd7ce5SGreg Kroah-Hartman 	ret = ERR_PTR(-EINVAL);
11696fd7ce5SGreg Kroah-Hartman 	ldops = tty_ldiscs[disc];
11796fd7ce5SGreg Kroah-Hartman 	if (ldops) {
11896fd7ce5SGreg Kroah-Hartman 		ret = ERR_PTR(-EAGAIN);
11996fd7ce5SGreg Kroah-Hartman 		if (try_module_get(ldops->owner)) {
12096fd7ce5SGreg Kroah-Hartman 			ldops->refcount++;
12196fd7ce5SGreg Kroah-Hartman 			ret = ldops;
12296fd7ce5SGreg Kroah-Hartman 		}
12396fd7ce5SGreg Kroah-Hartman 	}
124137084bbSPeter Hurley 	raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
12596fd7ce5SGreg Kroah-Hartman 	return ret;
12696fd7ce5SGreg Kroah-Hartman }
12796fd7ce5SGreg Kroah-Hartman 
12896fd7ce5SGreg Kroah-Hartman static void put_ldops(struct tty_ldisc_ops *ldops)
12996fd7ce5SGreg Kroah-Hartman {
13096fd7ce5SGreg Kroah-Hartman 	unsigned long flags;
13196fd7ce5SGreg Kroah-Hartman 
132137084bbSPeter Hurley 	raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
13396fd7ce5SGreg Kroah-Hartman 	ldops->refcount--;
13496fd7ce5SGreg Kroah-Hartman 	module_put(ldops->owner);
135137084bbSPeter Hurley 	raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
13696fd7ce5SGreg Kroah-Hartman }
13796fd7ce5SGreg Kroah-Hartman 
13896fd7ce5SGreg Kroah-Hartman /**
13996fd7ce5SGreg Kroah-Hartman  *	tty_ldisc_get		-	take a reference to an ldisc
14096fd7ce5SGreg Kroah-Hartman  *	@disc: ldisc number
14196fd7ce5SGreg Kroah-Hartman  *
14296fd7ce5SGreg Kroah-Hartman  *	Takes a reference to a line discipline. Deals with refcounts and
143c0cc1c5dSPeter Hurley  *	module locking counts.
144c0cc1c5dSPeter Hurley  *
145c0cc1c5dSPeter Hurley  *	Returns: -EINVAL if the discipline index is not [N_TTY..NR_LDISCS] or
146c0cc1c5dSPeter Hurley  *			 if the discipline is not registered
147c0cc1c5dSPeter Hurley  *		 -EAGAIN if request_module() failed to load or register the
148c0cc1c5dSPeter Hurley  *			 the discipline
149c0cc1c5dSPeter Hurley  *		 -ENOMEM if allocation failure
150c0cc1c5dSPeter Hurley  *
151c0cc1c5dSPeter Hurley  *		 Otherwise, returns a pointer to the discipline and bumps the
152c0cc1c5dSPeter Hurley  *		 ref count
15396fd7ce5SGreg Kroah-Hartman  *
15496fd7ce5SGreg Kroah-Hartman  *	Locking:
155137084bbSPeter Hurley  *		takes tty_ldiscs_lock to guard against ldisc races
15696fd7ce5SGreg Kroah-Hartman  */
15796fd7ce5SGreg Kroah-Hartman 
15836697529SPeter Hurley static struct tty_ldisc *tty_ldisc_get(struct tty_struct *tty, int disc)
15996fd7ce5SGreg Kroah-Hartman {
16096fd7ce5SGreg Kroah-Hartman 	struct tty_ldisc *ld;
16196fd7ce5SGreg Kroah-Hartman 	struct tty_ldisc_ops *ldops;
16296fd7ce5SGreg Kroah-Hartman 
16396fd7ce5SGreg Kroah-Hartman 	if (disc < N_TTY || disc >= NR_LDISCS)
16496fd7ce5SGreg Kroah-Hartman 		return ERR_PTR(-EINVAL);
16596fd7ce5SGreg Kroah-Hartman 
16696fd7ce5SGreg Kroah-Hartman 	/*
16796fd7ce5SGreg Kroah-Hartman 	 * Get the ldisc ops - we may need to request them to be loaded
16896fd7ce5SGreg Kroah-Hartman 	 * dynamically and try again.
16996fd7ce5SGreg Kroah-Hartman 	 */
17096fd7ce5SGreg Kroah-Hartman 	ldops = get_ldops(disc);
17196fd7ce5SGreg Kroah-Hartman 	if (IS_ERR(ldops)) {
17296fd7ce5SGreg Kroah-Hartman 		request_module("tty-ldisc-%d", disc);
17396fd7ce5SGreg Kroah-Hartman 		ldops = get_ldops(disc);
17496fd7ce5SGreg Kroah-Hartman 		if (IS_ERR(ldops))
17596fd7ce5SGreg Kroah-Hartman 			return ERR_CAST(ldops);
17696fd7ce5SGreg Kroah-Hartman 	}
17796fd7ce5SGreg Kroah-Hartman 
17896fd7ce5SGreg Kroah-Hartman 	ld = kmalloc(sizeof(struct tty_ldisc), GFP_KERNEL);
17996fd7ce5SGreg Kroah-Hartman 	if (ld == NULL) {
18096fd7ce5SGreg Kroah-Hartman 		put_ldops(ldops);
18196fd7ce5SGreg Kroah-Hartman 		return ERR_PTR(-ENOMEM);
18296fd7ce5SGreg Kroah-Hartman 	}
18396fd7ce5SGreg Kroah-Hartman 
18496fd7ce5SGreg Kroah-Hartman 	ld->ops = ldops;
18536697529SPeter Hurley 	ld->tty = tty;
1861541f845SIvo Sieben 
18796fd7ce5SGreg Kroah-Hartman 	return ld;
18896fd7ce5SGreg Kroah-Hartman }
18996fd7ce5SGreg Kroah-Hartman 
190734de249SPeter Hurley /**
191734de249SPeter Hurley  *	tty_ldisc_put		-	release the ldisc
192734de249SPeter Hurley  *
193734de249SPeter Hurley  *	Complement of tty_ldisc_get().
194734de249SPeter Hurley  */
195cb128f69SDenys Vlasenko static void tty_ldisc_put(struct tty_ldisc *ld)
196734de249SPeter Hurley {
197734de249SPeter Hurley 	if (WARN_ON_ONCE(!ld))
198734de249SPeter Hurley 		return;
199734de249SPeter Hurley 
20036697529SPeter Hurley 	put_ldops(ld->ops);
201734de249SPeter Hurley 	kfree(ld);
202734de249SPeter Hurley }
203734de249SPeter Hurley 
20496fd7ce5SGreg Kroah-Hartman static void *tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos)
20596fd7ce5SGreg Kroah-Hartman {
20696fd7ce5SGreg Kroah-Hartman 	return (*pos < NR_LDISCS) ? pos : NULL;
20796fd7ce5SGreg Kroah-Hartman }
20896fd7ce5SGreg Kroah-Hartman 
20996fd7ce5SGreg Kroah-Hartman static void *tty_ldiscs_seq_next(struct seq_file *m, void *v, loff_t *pos)
21096fd7ce5SGreg Kroah-Hartman {
21196fd7ce5SGreg Kroah-Hartman 	(*pos)++;
21296fd7ce5SGreg Kroah-Hartman 	return (*pos < NR_LDISCS) ? pos : NULL;
21396fd7ce5SGreg Kroah-Hartman }
21496fd7ce5SGreg Kroah-Hartman 
21596fd7ce5SGreg Kroah-Hartman static void tty_ldiscs_seq_stop(struct seq_file *m, void *v)
21696fd7ce5SGreg Kroah-Hartman {
21796fd7ce5SGreg Kroah-Hartman }
21896fd7ce5SGreg Kroah-Hartman 
21996fd7ce5SGreg Kroah-Hartman static int tty_ldiscs_seq_show(struct seq_file *m, void *v)
22096fd7ce5SGreg Kroah-Hartman {
22196fd7ce5SGreg Kroah-Hartman 	int i = *(loff_t *)v;
22296fd7ce5SGreg Kroah-Hartman 	struct tty_ldisc_ops *ldops;
22396fd7ce5SGreg Kroah-Hartman 
22496fd7ce5SGreg Kroah-Hartman 	ldops = get_ldops(i);
22596fd7ce5SGreg Kroah-Hartman 	if (IS_ERR(ldops))
22696fd7ce5SGreg Kroah-Hartman 		return 0;
22796fd7ce5SGreg Kroah-Hartman 	seq_printf(m, "%-10s %2d\n", ldops->name ? ldops->name : "???", i);
22896fd7ce5SGreg Kroah-Hartman 	put_ldops(ldops);
22996fd7ce5SGreg Kroah-Hartman 	return 0;
23096fd7ce5SGreg Kroah-Hartman }
23196fd7ce5SGreg Kroah-Hartman 
23296fd7ce5SGreg Kroah-Hartman static const struct seq_operations tty_ldiscs_seq_ops = {
23396fd7ce5SGreg Kroah-Hartman 	.start	= tty_ldiscs_seq_start,
23496fd7ce5SGreg Kroah-Hartman 	.next	= tty_ldiscs_seq_next,
23596fd7ce5SGreg Kroah-Hartman 	.stop	= tty_ldiscs_seq_stop,
23696fd7ce5SGreg Kroah-Hartman 	.show	= tty_ldiscs_seq_show,
23796fd7ce5SGreg Kroah-Hartman };
23896fd7ce5SGreg Kroah-Hartman 
23996fd7ce5SGreg Kroah-Hartman static int proc_tty_ldiscs_open(struct inode *inode, struct file *file)
24096fd7ce5SGreg Kroah-Hartman {
24196fd7ce5SGreg Kroah-Hartman 	return seq_open(file, &tty_ldiscs_seq_ops);
24296fd7ce5SGreg Kroah-Hartman }
24396fd7ce5SGreg Kroah-Hartman 
24496fd7ce5SGreg Kroah-Hartman const struct file_operations tty_ldiscs_proc_fops = {
24596fd7ce5SGreg Kroah-Hartman 	.owner		= THIS_MODULE,
24696fd7ce5SGreg Kroah-Hartman 	.open		= proc_tty_ldiscs_open,
24796fd7ce5SGreg Kroah-Hartman 	.read		= seq_read,
24896fd7ce5SGreg Kroah-Hartman 	.llseek		= seq_lseek,
24996fd7ce5SGreg Kroah-Hartman 	.release	= seq_release,
25096fd7ce5SGreg Kroah-Hartman };
25196fd7ce5SGreg Kroah-Hartman 
25296fd7ce5SGreg Kroah-Hartman /**
25396fd7ce5SGreg Kroah-Hartman  *	tty_ldisc_ref_wait	-	wait for the tty ldisc
25496fd7ce5SGreg Kroah-Hartman  *	@tty: tty device
25596fd7ce5SGreg Kroah-Hartman  *
25696fd7ce5SGreg Kroah-Hartman  *	Dereference the line discipline for the terminal and take a
25796fd7ce5SGreg Kroah-Hartman  *	reference to it. If the line discipline is in flux then
25896fd7ce5SGreg Kroah-Hartman  *	wait patiently until it changes.
25996fd7ce5SGreg Kroah-Hartman  *
260892d1fa7SPeter Hurley  *	Returns: NULL if the tty has been hungup and not re-opened with
261892d1fa7SPeter Hurley  *		 a new file descriptor, otherwise valid ldisc reference
262892d1fa7SPeter Hurley  *
26396fd7ce5SGreg Kroah-Hartman  *	Note: Must not be called from an IRQ/timer context. The caller
26496fd7ce5SGreg Kroah-Hartman  *	must also be careful not to hold other locks that will deadlock
26596fd7ce5SGreg Kroah-Hartman  *	against a discipline change, such as an existing ldisc reference
26696fd7ce5SGreg Kroah-Hartman  *	(which we check for)
26796fd7ce5SGreg Kroah-Hartman  *
268e55afd11SPeter Hurley  *	Note: a file_operations routine (read/poll/write) should use this
269e55afd11SPeter Hurley  *	function to wait for any ldisc lifetime events to finish.
27096fd7ce5SGreg Kroah-Hartman  */
27196fd7ce5SGreg Kroah-Hartman 
27296fd7ce5SGreg Kroah-Hartman struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty)
27396fd7ce5SGreg Kroah-Hartman {
274a4a3e061SDmitry Vyukov 	struct tty_ldisc *ld;
275a4a3e061SDmitry Vyukov 
27636697529SPeter Hurley 	ldsem_down_read(&tty->ldisc_sem, MAX_SCHEDULE_TIMEOUT);
277a4a3e061SDmitry Vyukov 	ld = tty->ldisc;
278a4a3e061SDmitry Vyukov 	if (!ld)
279a570a49aSPeter Hurley 		ldsem_up_read(&tty->ldisc_sem);
280a4a3e061SDmitry Vyukov 	return ld;
28196fd7ce5SGreg Kroah-Hartman }
28296fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait);
28396fd7ce5SGreg Kroah-Hartman 
28496fd7ce5SGreg Kroah-Hartman /**
28596fd7ce5SGreg Kroah-Hartman  *	tty_ldisc_ref		-	get the tty ldisc
28696fd7ce5SGreg Kroah-Hartman  *	@tty: tty device
28796fd7ce5SGreg Kroah-Hartman  *
28896fd7ce5SGreg Kroah-Hartman  *	Dereference the line discipline for the terminal and take a
28996fd7ce5SGreg Kroah-Hartman  *	reference to it. If the line discipline is in flux then
29096fd7ce5SGreg Kroah-Hartman  *	return NULL. Can be called from IRQ and timer functions.
29196fd7ce5SGreg Kroah-Hartman  */
29296fd7ce5SGreg Kroah-Hartman 
29396fd7ce5SGreg Kroah-Hartman struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty)
29496fd7ce5SGreg Kroah-Hartman {
29536697529SPeter Hurley 	struct tty_ldisc *ld = NULL;
29636697529SPeter Hurley 
29736697529SPeter Hurley 	if (ldsem_down_read_trylock(&tty->ldisc_sem)) {
29836697529SPeter Hurley 		ld = tty->ldisc;
29936697529SPeter Hurley 		if (!ld)
30036697529SPeter Hurley 			ldsem_up_read(&tty->ldisc_sem);
30136697529SPeter Hurley 	}
30236697529SPeter Hurley 	return ld;
30396fd7ce5SGreg Kroah-Hartman }
30496fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(tty_ldisc_ref);
30596fd7ce5SGreg Kroah-Hartman 
30696fd7ce5SGreg Kroah-Hartman /**
30796fd7ce5SGreg Kroah-Hartman  *	tty_ldisc_deref		-	free a tty ldisc reference
30896fd7ce5SGreg Kroah-Hartman  *	@ld: reference to free up
30996fd7ce5SGreg Kroah-Hartman  *
31096fd7ce5SGreg Kroah-Hartman  *	Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May
31196fd7ce5SGreg Kroah-Hartman  *	be called in IRQ context.
31296fd7ce5SGreg Kroah-Hartman  */
31396fd7ce5SGreg Kroah-Hartman 
31496fd7ce5SGreg Kroah-Hartman void tty_ldisc_deref(struct tty_ldisc *ld)
31596fd7ce5SGreg Kroah-Hartman {
31636697529SPeter Hurley 	ldsem_up_read(&ld->tty->ldisc_sem);
31796fd7ce5SGreg Kroah-Hartman }
31896fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(tty_ldisc_deref);
31996fd7ce5SGreg Kroah-Hartman 
320d2c43890SPeter Hurley 
321c2bb524bSPeter Hurley static inline int
322e80a10eeSPeter Hurley __tty_ldisc_lock(struct tty_struct *tty, unsigned long timeout)
323d2c43890SPeter Hurley {
324d2c43890SPeter Hurley 	return ldsem_down_write(&tty->ldisc_sem, timeout);
325d2c43890SPeter Hurley }
326d2c43890SPeter Hurley 
327c2bb524bSPeter Hurley static inline int
328e80a10eeSPeter Hurley __tty_ldisc_lock_nested(struct tty_struct *tty, unsigned long timeout)
329d2c43890SPeter Hurley {
330d2c43890SPeter Hurley 	return ldsem_down_write_nested(&tty->ldisc_sem,
331d2c43890SPeter Hurley 				       LDISC_SEM_OTHER, timeout);
332d2c43890SPeter Hurley }
333d2c43890SPeter Hurley 
334e80a10eeSPeter Hurley static inline void __tty_ldisc_unlock(struct tty_struct *tty)
335d2c43890SPeter Hurley {
33652772ea6SGuillaume Gomez 	ldsem_up_write(&tty->ldisc_sem);
337d2c43890SPeter Hurley }
338d2c43890SPeter Hurley 
339c2bb524bSPeter Hurley static int tty_ldisc_lock(struct tty_struct *tty, unsigned long timeout)
340fae76e9aSPeter Hurley {
341fae76e9aSPeter Hurley 	int ret;
342fae76e9aSPeter Hurley 
343fae76e9aSPeter Hurley 	ret = __tty_ldisc_lock(tty, timeout);
344fae76e9aSPeter Hurley 	if (!ret)
345fae76e9aSPeter Hurley 		return -EBUSY;
346fae76e9aSPeter Hurley 	set_bit(TTY_LDISC_HALTED, &tty->flags);
347fae76e9aSPeter Hurley 	return 0;
348fae76e9aSPeter Hurley }
349fae76e9aSPeter Hurley 
350fae76e9aSPeter Hurley static void tty_ldisc_unlock(struct tty_struct *tty)
351fae76e9aSPeter Hurley {
352fae76e9aSPeter Hurley 	clear_bit(TTY_LDISC_HALTED, &tty->flags);
353fae76e9aSPeter Hurley 	__tty_ldisc_unlock(tty);
354fae76e9aSPeter Hurley }
355fae76e9aSPeter Hurley 
356c2bb524bSPeter Hurley static int
357d2c43890SPeter Hurley tty_ldisc_lock_pair_timeout(struct tty_struct *tty, struct tty_struct *tty2,
358d2c43890SPeter Hurley 			    unsigned long timeout)
359d2c43890SPeter Hurley {
360d2c43890SPeter Hurley 	int ret;
361d2c43890SPeter Hurley 
362d2c43890SPeter Hurley 	if (tty < tty2) {
363e80a10eeSPeter Hurley 		ret = __tty_ldisc_lock(tty, timeout);
364d2c43890SPeter Hurley 		if (ret) {
365e80a10eeSPeter Hurley 			ret = __tty_ldisc_lock_nested(tty2, timeout);
366d2c43890SPeter Hurley 			if (!ret)
367e80a10eeSPeter Hurley 				__tty_ldisc_unlock(tty);
368d2c43890SPeter Hurley 		}
369d2c43890SPeter Hurley 	} else {
370d2c43890SPeter Hurley 		/* if this is possible, it has lots of implications */
371d2c43890SPeter Hurley 		WARN_ON_ONCE(tty == tty2);
372d2c43890SPeter Hurley 		if (tty2 && tty != tty2) {
373e80a10eeSPeter Hurley 			ret = __tty_ldisc_lock(tty2, timeout);
374d2c43890SPeter Hurley 			if (ret) {
375e80a10eeSPeter Hurley 				ret = __tty_ldisc_lock_nested(tty, timeout);
376d2c43890SPeter Hurley 				if (!ret)
377e80a10eeSPeter Hurley 					__tty_ldisc_unlock(tty2);
378d2c43890SPeter Hurley 			}
379d2c43890SPeter Hurley 		} else
380e80a10eeSPeter Hurley 			ret = __tty_ldisc_lock(tty, timeout);
381d2c43890SPeter Hurley 	}
382d2c43890SPeter Hurley 
383d2c43890SPeter Hurley 	if (!ret)
384d2c43890SPeter Hurley 		return -EBUSY;
385d2c43890SPeter Hurley 
386d2c43890SPeter Hurley 	set_bit(TTY_LDISC_HALTED, &tty->flags);
387d2c43890SPeter Hurley 	if (tty2)
388d2c43890SPeter Hurley 		set_bit(TTY_LDISC_HALTED, &tty2->flags);
389d2c43890SPeter Hurley 	return 0;
390d2c43890SPeter Hurley }
391d2c43890SPeter Hurley 
392c2bb524bSPeter Hurley static void tty_ldisc_lock_pair(struct tty_struct *tty, struct tty_struct *tty2)
393d2c43890SPeter Hurley {
394d2c43890SPeter Hurley 	tty_ldisc_lock_pair_timeout(tty, tty2, MAX_SCHEDULE_TIMEOUT);
395d2c43890SPeter Hurley }
396d2c43890SPeter Hurley 
397c2bb524bSPeter Hurley static void tty_ldisc_unlock_pair(struct tty_struct *tty,
398d2c43890SPeter Hurley 				  struct tty_struct *tty2)
399d2c43890SPeter Hurley {
400e80a10eeSPeter Hurley 	__tty_ldisc_unlock(tty);
401d2c43890SPeter Hurley 	if (tty2)
402e80a10eeSPeter Hurley 		__tty_ldisc_unlock(tty2);
403d2c43890SPeter Hurley }
404d2c43890SPeter Hurley 
40596fd7ce5SGreg Kroah-Hartman /**
40696fd7ce5SGreg Kroah-Hartman  *	tty_ldisc_flush	-	flush line discipline queue
40796fd7ce5SGreg Kroah-Hartman  *	@tty: tty
40896fd7ce5SGreg Kroah-Hartman  *
40986c80a8eSPeter Hurley  *	Flush the line discipline queue (if any) and the tty flip buffers
41086c80a8eSPeter Hurley  *	for this tty.
41196fd7ce5SGreg Kroah-Hartman  */
41296fd7ce5SGreg Kroah-Hartman 
41396fd7ce5SGreg Kroah-Hartman void tty_ldisc_flush(struct tty_struct *tty)
41496fd7ce5SGreg Kroah-Hartman {
41596fd7ce5SGreg Kroah-Hartman 	struct tty_ldisc *ld = tty_ldisc_ref(tty);
41686c80a8eSPeter Hurley 
41786c80a8eSPeter Hurley 	tty_buffer_flush(tty, ld);
41886c80a8eSPeter Hurley 	if (ld)
41996fd7ce5SGreg Kroah-Hartman 		tty_ldisc_deref(ld);
42096fd7ce5SGreg Kroah-Hartman }
42196fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(tty_ldisc_flush);
42296fd7ce5SGreg Kroah-Hartman 
42396fd7ce5SGreg Kroah-Hartman /**
42496fd7ce5SGreg Kroah-Hartman  *	tty_set_termios_ldisc		-	set ldisc field
42596fd7ce5SGreg Kroah-Hartman  *	@tty: tty structure
426c12da96fSPeter Hurley  *	@disc: line discipline number
42796fd7ce5SGreg Kroah-Hartman  *
42896fd7ce5SGreg Kroah-Hartman  *	This is probably overkill for real world processors but
42996fd7ce5SGreg Kroah-Hartman  *	they are not on hot paths so a little discipline won't do
43096fd7ce5SGreg Kroah-Hartman  *	any harm.
43196fd7ce5SGreg Kroah-Hartman  *
432dd42bf11SPeter Hurley  *	The line discipline-related tty_struct fields are reset to
433dd42bf11SPeter Hurley  *	prevent the ldisc driver from re-using stale information for
434dd42bf11SPeter Hurley  *	the new ldisc instance.
435dd42bf11SPeter Hurley  *
4366a1c0680SPeter Hurley  *	Locking: takes termios_rwsem
43796fd7ce5SGreg Kroah-Hartman  */
43896fd7ce5SGreg Kroah-Hartman 
439c12da96fSPeter Hurley static void tty_set_termios_ldisc(struct tty_struct *tty, int disc)
44096fd7ce5SGreg Kroah-Hartman {
4416a1c0680SPeter Hurley 	down_write(&tty->termios_rwsem);
442c12da96fSPeter Hurley 	tty->termios.c_line = disc;
4436a1c0680SPeter Hurley 	up_write(&tty->termios_rwsem);
444dd42bf11SPeter Hurley 
445dd42bf11SPeter Hurley 	tty->disc_data = NULL;
446dd42bf11SPeter Hurley 	tty->receive_room = 0;
44796fd7ce5SGreg Kroah-Hartman }
44896fd7ce5SGreg Kroah-Hartman 
44996fd7ce5SGreg Kroah-Hartman /**
45096fd7ce5SGreg Kroah-Hartman  *	tty_ldisc_open		-	open a line discipline
45196fd7ce5SGreg Kroah-Hartman  *	@tty: tty we are opening the ldisc on
45296fd7ce5SGreg Kroah-Hartman  *	@ld: discipline to open
45396fd7ce5SGreg Kroah-Hartman  *
45496fd7ce5SGreg Kroah-Hartman  *	A helper opening method. Also a convenient debugging and check
45596fd7ce5SGreg Kroah-Hartman  *	point.
45696fd7ce5SGreg Kroah-Hartman  *
45796fd7ce5SGreg Kroah-Hartman  *	Locking: always called with BTM already held.
45896fd7ce5SGreg Kroah-Hartman  */
45996fd7ce5SGreg Kroah-Hartman 
46096fd7ce5SGreg Kroah-Hartman static int tty_ldisc_open(struct tty_struct *tty, struct tty_ldisc *ld)
46196fd7ce5SGreg Kroah-Hartman {
46296fd7ce5SGreg Kroah-Hartman 	WARN_ON(test_and_set_bit(TTY_LDISC_OPEN, &tty->flags));
46396fd7ce5SGreg Kroah-Hartman 	if (ld->ops->open) {
46496fd7ce5SGreg Kroah-Hartman 		int ret;
46596fd7ce5SGreg Kroah-Hartman                 /* BTM here locks versus a hangup event */
46696fd7ce5SGreg Kroah-Hartman 		ret = ld->ops->open(tty);
4677f90cfc5SJiri Slaby 		if (ret)
4687f90cfc5SJiri Slaby 			clear_bit(TTY_LDISC_OPEN, &tty->flags);
469fb6edc91SPeter Hurley 
470a570a49aSPeter Hurley 		tty_ldisc_debug(tty, "%p: opened\n", ld);
47196fd7ce5SGreg Kroah-Hartman 		return ret;
47296fd7ce5SGreg Kroah-Hartman 	}
47396fd7ce5SGreg Kroah-Hartman 	return 0;
47496fd7ce5SGreg Kroah-Hartman }
47596fd7ce5SGreg Kroah-Hartman 
47696fd7ce5SGreg Kroah-Hartman /**
47796fd7ce5SGreg Kroah-Hartman  *	tty_ldisc_close		-	close a line discipline
47896fd7ce5SGreg Kroah-Hartman  *	@tty: tty we are opening the ldisc on
47996fd7ce5SGreg Kroah-Hartman  *	@ld: discipline to close
48096fd7ce5SGreg Kroah-Hartman  *
48196fd7ce5SGreg Kroah-Hartman  *	A helper close method. Also a convenient debugging and check
48296fd7ce5SGreg Kroah-Hartman  *	point.
48396fd7ce5SGreg Kroah-Hartman  */
48496fd7ce5SGreg Kroah-Hartman 
48596fd7ce5SGreg Kroah-Hartman static void tty_ldisc_close(struct tty_struct *tty, struct tty_ldisc *ld)
48696fd7ce5SGreg Kroah-Hartman {
48796fd7ce5SGreg Kroah-Hartman 	WARN_ON(!test_bit(TTY_LDISC_OPEN, &tty->flags));
48896fd7ce5SGreg Kroah-Hartman 	clear_bit(TTY_LDISC_OPEN, &tty->flags);
48996fd7ce5SGreg Kroah-Hartman 	if (ld->ops->close)
49096fd7ce5SGreg Kroah-Hartman 		ld->ops->close(tty);
491a570a49aSPeter Hurley 	tty_ldisc_debug(tty, "%p: closed\n", ld);
49296fd7ce5SGreg Kroah-Hartman }
49396fd7ce5SGreg Kroah-Hartman 
49496fd7ce5SGreg Kroah-Hartman /**
4958a8dabf2SAlan Cox  *	tty_ldisc_failto	-	helper for ldisc failback
4968a8dabf2SAlan Cox  *	@tty: tty to open the ldisc on
4978a8dabf2SAlan Cox  *	@ld: ldisc we are trying to fail back to
4988a8dabf2SAlan Cox  *
4998a8dabf2SAlan Cox  *	Helper to try and recover a tty when switching back to the old
5008a8dabf2SAlan Cox  *	ldisc fails and we need something attached.
5018a8dabf2SAlan Cox  */
5028a8dabf2SAlan Cox 
5038a8dabf2SAlan Cox static int tty_ldisc_failto(struct tty_struct *tty, int ld)
5048a8dabf2SAlan Cox {
5058a8dabf2SAlan Cox 	struct tty_ldisc *disc = tty_ldisc_get(tty, ld);
5068a8dabf2SAlan Cox 	int r;
5078a8dabf2SAlan Cox 
5088a8dabf2SAlan Cox 	if (IS_ERR(disc))
5098a8dabf2SAlan Cox 		return PTR_ERR(disc);
5108a8dabf2SAlan Cox 	tty->ldisc = disc;
5118a8dabf2SAlan Cox 	tty_set_termios_ldisc(tty, ld);
5128a8dabf2SAlan Cox 	if ((r = tty_ldisc_open(tty, disc)) < 0)
5138a8dabf2SAlan Cox 		tty_ldisc_put(disc);
5148a8dabf2SAlan Cox 	return r;
5158a8dabf2SAlan Cox }
5168a8dabf2SAlan Cox 
5178a8dabf2SAlan Cox /**
518a8983d01SGreg Kroah-Hartman  *	tty_ldisc_restore	-	helper for tty ldisc change
519a8983d01SGreg Kroah-Hartman  *	@tty: tty to recover
520a8983d01SGreg Kroah-Hartman  *	@old: previous ldisc
521a8983d01SGreg Kroah-Hartman  *
522a8983d01SGreg Kroah-Hartman  *	Restore the previous line discipline or N_TTY when a line discipline
523a8983d01SGreg Kroah-Hartman  *	change fails due to an open error
524a8983d01SGreg Kroah-Hartman  */
525a8983d01SGreg Kroah-Hartman 
526a8983d01SGreg Kroah-Hartman static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old)
527a8983d01SGreg Kroah-Hartman {
528a8983d01SGreg Kroah-Hartman 	/* There is an outstanding reference here so this is safe */
529a8983d01SGreg Kroah-Hartman 	old = tty_ldisc_get(tty, old->ops->num);
530a8983d01SGreg Kroah-Hartman 	WARN_ON(IS_ERR(old));
531a8983d01SGreg Kroah-Hartman 	tty->ldisc = old;
532a8983d01SGreg Kroah-Hartman 	tty_set_termios_ldisc(tty, old->ops->num);
533a8983d01SGreg Kroah-Hartman 	if (tty_ldisc_open(tty, old) < 0) {
534a8983d01SGreg Kroah-Hartman 		tty_ldisc_put(old);
5358a8dabf2SAlan Cox 		/* The traditional behaviour is to fall back to N_TTY, we
5368a8dabf2SAlan Cox 		   want to avoid falling back to N_NULL unless we have no
5378a8dabf2SAlan Cox 		   choice to avoid the risk of breaking anything */
5388a8dabf2SAlan Cox 		if (tty_ldisc_failto(tty, N_TTY) < 0 &&
5398a8dabf2SAlan Cox 		    tty_ldisc_failto(tty, N_NULL) < 0)
5408a8dabf2SAlan Cox 			panic("Couldn't open N_NULL ldisc for %s.",
5418a8dabf2SAlan Cox 			      tty_name(tty));
542a8983d01SGreg Kroah-Hartman 	}
543a8983d01SGreg Kroah-Hartman }
544a8983d01SGreg Kroah-Hartman 
545a8983d01SGreg Kroah-Hartman /**
54696fd7ce5SGreg Kroah-Hartman  *	tty_set_ldisc		-	set line discipline
54796fd7ce5SGreg Kroah-Hartman  *	@tty: the terminal to set
54896fd7ce5SGreg Kroah-Hartman  *	@ldisc: the line discipline
54996fd7ce5SGreg Kroah-Hartman  *
55096fd7ce5SGreg Kroah-Hartman  *	Set the discipline of a tty line. Must be called from a process
55196fd7ce5SGreg Kroah-Hartman  *	context. The ldisc change logic has to protect itself against any
55296fd7ce5SGreg Kroah-Hartman  *	overlapping ldisc change (including on the other end of pty pairs),
55396fd7ce5SGreg Kroah-Hartman  *	the close of one side of a tty/pty pair, and eventually hangup.
55496fd7ce5SGreg Kroah-Hartman  */
55596fd7ce5SGreg Kroah-Hartman 
556c12da96fSPeter Hurley int tty_set_ldisc(struct tty_struct *tty, int disc)
55796fd7ce5SGreg Kroah-Hartman {
558a8983d01SGreg Kroah-Hartman 	int retval;
559a8983d01SGreg Kroah-Hartman 	struct tty_ldisc *old_ldisc, *new_ldisc;
560a8983d01SGreg Kroah-Hartman 
561a8983d01SGreg Kroah-Hartman 	new_ldisc = tty_ldisc_get(tty, disc);
562a8983d01SGreg Kroah-Hartman 	if (IS_ERR(new_ldisc))
563a8983d01SGreg Kroah-Hartman 		return PTR_ERR(new_ldisc);
56496fd7ce5SGreg Kroah-Hartman 
565c8483bc9SPeter Hurley 	tty_lock(tty);
566276a661aSPeter Hurley 	retval = tty_ldisc_lock(tty, 5 * HZ);
56763d8cb3fSPeter Hurley 	if (retval)
56863d8cb3fSPeter Hurley 		goto err;
56996fd7ce5SGreg Kroah-Hartman 
570a570a49aSPeter Hurley 	if (!tty->ldisc) {
571a570a49aSPeter Hurley 		retval = -EIO;
572a570a49aSPeter Hurley 		goto out;
573a570a49aSPeter Hurley 	}
574a570a49aSPeter Hurley 
57563d8cb3fSPeter Hurley 	/* Check the no-op case */
576a8983d01SGreg Kroah-Hartman 	if (tty->ldisc->ops->num == disc)
57763d8cb3fSPeter Hurley 		goto out;
57896fd7ce5SGreg Kroah-Hartman 
57963d8cb3fSPeter Hurley 	if (test_bit(TTY_HUPPED, &tty->flags)) {
58063d8cb3fSPeter Hurley 		/* We were raced by hangup */
58163d8cb3fSPeter Hurley 		retval = -EIO;
58263d8cb3fSPeter Hurley 		goto out;
58396fd7ce5SGreg Kroah-Hartman 	}
58496fd7ce5SGreg Kroah-Hartman 
585a8983d01SGreg Kroah-Hartman 	old_ldisc = tty->ldisc;
586a8983d01SGreg Kroah-Hartman 
587a8983d01SGreg Kroah-Hartman 	/* Shutdown the old discipline. */
588a8983d01SGreg Kroah-Hartman 	tty_ldisc_close(tty, old_ldisc);
589a8983d01SGreg Kroah-Hartman 
590a8983d01SGreg Kroah-Hartman 	/* Now set up the new line discipline. */
591a8983d01SGreg Kroah-Hartman 	tty->ldisc = new_ldisc;
592a8983d01SGreg Kroah-Hartman 	tty_set_termios_ldisc(tty, disc);
593a8983d01SGreg Kroah-Hartman 
594a8983d01SGreg Kroah-Hartman 	retval = tty_ldisc_open(tty, new_ldisc);
59596fd7ce5SGreg Kroah-Hartman 	if (retval < 0) {
59696fd7ce5SGreg Kroah-Hartman 		/* Back to the old one or N_TTY if we can't */
597a8983d01SGreg Kroah-Hartman 		tty_ldisc_put(new_ldisc);
598a8983d01SGreg Kroah-Hartman 		tty_ldisc_restore(tty, old_ldisc);
59996fd7ce5SGreg Kroah-Hartman 	}
60096fd7ce5SGreg Kroah-Hartman 
601a8983d01SGreg Kroah-Hartman 	if (tty->ldisc->ops->num != old_ldisc->ops->num && tty->ops->set_ldisc) {
6029191aaaaSPeter Hurley 		down_read(&tty->termios_rwsem);
60396fd7ce5SGreg Kroah-Hartman 		tty->ops->set_ldisc(tty);
6049191aaaaSPeter Hurley 		up_read(&tty->termios_rwsem);
6059191aaaaSPeter Hurley 	}
60696fd7ce5SGreg Kroah-Hartman 
607a8983d01SGreg Kroah-Hartman 	/* At this point we hold a reference to the new ldisc and a
608a8983d01SGreg Kroah-Hartman 	   reference to the old ldisc, or we hold two references to
609a8983d01SGreg Kroah-Hartman 	   the old ldisc (if it was restored as part of error cleanup
610a8983d01SGreg Kroah-Hartman 	   above). In either case, releasing a single reference from
611a8983d01SGreg Kroah-Hartman 	   the old ldisc is correct. */
612a8983d01SGreg Kroah-Hartman 	new_ldisc = old_ldisc;
61363d8cb3fSPeter Hurley out:
614276a661aSPeter Hurley 	tty_ldisc_unlock(tty);
61596fd7ce5SGreg Kroah-Hartman 
61696fd7ce5SGreg Kroah-Hartman 	/* Restart the work queue in case no characters kick it off. Safe if
61796fd7ce5SGreg Kroah-Hartman 	   already running */
61817a69219SPeter Hurley 	tty_buffer_restart_work(tty->port);
61963d8cb3fSPeter Hurley err:
620a8983d01SGreg Kroah-Hartman 	tty_ldisc_put(new_ldisc);	/* drop the extra reference */
62189c8d91eSAlan Cox 	tty_unlock(tty);
62296fd7ce5SGreg Kroah-Hartman 	return retval;
62396fd7ce5SGreg Kroah-Hartman }
6241ab92da3SOkash Khawaja EXPORT_SYMBOL_GPL(tty_set_ldisc);
62596fd7ce5SGreg Kroah-Hartman 
62696fd7ce5SGreg Kroah-Hartman /**
6276ffeb4b2SPeter Hurley  *	tty_ldisc_kill	-	teardown ldisc
6286ffeb4b2SPeter Hurley  *	@tty: tty being released
6296ffeb4b2SPeter Hurley  *
6306ffeb4b2SPeter Hurley  *	Perform final close of the ldisc and reset tty->ldisc
6316ffeb4b2SPeter Hurley  */
6326ffeb4b2SPeter Hurley static void tty_ldisc_kill(struct tty_struct *tty)
6336ffeb4b2SPeter Hurley {
6346ffeb4b2SPeter Hurley 	if (!tty->ldisc)
6356ffeb4b2SPeter Hurley 		return;
6366ffeb4b2SPeter Hurley 	/*
6376ffeb4b2SPeter Hurley 	 * Now kill off the ldisc
6386ffeb4b2SPeter Hurley 	 */
6396ffeb4b2SPeter Hurley 	tty_ldisc_close(tty, tty->ldisc);
6406ffeb4b2SPeter Hurley 	tty_ldisc_put(tty->ldisc);
6416ffeb4b2SPeter Hurley 	/* Force an oops if we mess this up */
6426ffeb4b2SPeter Hurley 	tty->ldisc = NULL;
6436ffeb4b2SPeter Hurley }
6446ffeb4b2SPeter Hurley 
6456ffeb4b2SPeter Hurley /**
64696fd7ce5SGreg Kroah-Hartman  *	tty_reset_termios	-	reset terminal state
64796fd7ce5SGreg Kroah-Hartman  *	@tty: tty to reset
64896fd7ce5SGreg Kroah-Hartman  *
64996fd7ce5SGreg Kroah-Hartman  *	Restore a terminal to the driver default state.
65096fd7ce5SGreg Kroah-Hartman  */
65196fd7ce5SGreg Kroah-Hartman 
65296fd7ce5SGreg Kroah-Hartman static void tty_reset_termios(struct tty_struct *tty)
65396fd7ce5SGreg Kroah-Hartman {
6546a1c0680SPeter Hurley 	down_write(&tty->termios_rwsem);
655adc8d746SAlan Cox 	tty->termios = tty->driver->init_termios;
656adc8d746SAlan Cox 	tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios);
657adc8d746SAlan Cox 	tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios);
6586a1c0680SPeter Hurley 	up_write(&tty->termios_rwsem);
65996fd7ce5SGreg Kroah-Hartman }
66096fd7ce5SGreg Kroah-Hartman 
66196fd7ce5SGreg Kroah-Hartman 
66296fd7ce5SGreg Kroah-Hartman /**
66396fd7ce5SGreg Kroah-Hartman  *	tty_ldisc_reinit	-	reinitialise the tty ldisc
66496fd7ce5SGreg Kroah-Hartman  *	@tty: tty to reinit
665c12da96fSPeter Hurley  *	@disc: line discipline to reinitialize
66696fd7ce5SGreg Kroah-Hartman  *
6677896f30dSPeter Hurley  *	Completely reinitialize the line discipline state, by closing the
668892d1fa7SPeter Hurley  *	current instance, if there is one, and opening a new instance. If
669892d1fa7SPeter Hurley  *	an error occurs opening the new non-N_TTY instance, the instance
670892d1fa7SPeter Hurley  *	is dropped and tty->ldisc reset to NULL. The caller can then retry
671892d1fa7SPeter Hurley  *	with N_TTY instead.
6727896f30dSPeter Hurley  *
6737896f30dSPeter Hurley  *	Returns 0 if successful, otherwise error code < 0
67496fd7ce5SGreg Kroah-Hartman  */
67596fd7ce5SGreg Kroah-Hartman 
676892d1fa7SPeter Hurley int tty_ldisc_reinit(struct tty_struct *tty, int disc)
67796fd7ce5SGreg Kroah-Hartman {
6787896f30dSPeter Hurley 	struct tty_ldisc *ld;
6797896f30dSPeter Hurley 	int retval;
6801c95ba1eSPhilippe Rétornaz 
6817896f30dSPeter Hurley 	ld = tty_ldisc_get(tty, disc);
682a8983d01SGreg Kroah-Hartman 	if (IS_ERR(ld)) {
683a8983d01SGreg Kroah-Hartman 		BUG_ON(disc == N_TTY);
6847896f30dSPeter Hurley 		return PTR_ERR(ld);
685a8983d01SGreg Kroah-Hartman 	}
68696fd7ce5SGreg Kroah-Hartman 
6877896f30dSPeter Hurley 	if (tty->ldisc) {
68896fd7ce5SGreg Kroah-Hartman 		tty_ldisc_close(tty, tty->ldisc);
68996fd7ce5SGreg Kroah-Hartman 		tty_ldisc_put(tty->ldisc);
6907896f30dSPeter Hurley 	}
6917896f30dSPeter Hurley 
6927896f30dSPeter Hurley 	/* switch the line discipline */
693f4807045SPeter Hurley 	tty->ldisc = ld;
694c12da96fSPeter Hurley 	tty_set_termios_ldisc(tty, disc);
6957896f30dSPeter Hurley 	retval = tty_ldisc_open(tty, tty->ldisc);
6967896f30dSPeter Hurley 	if (retval) {
6977896f30dSPeter Hurley 		tty_ldisc_put(tty->ldisc);
6987896f30dSPeter Hurley 		tty->ldisc = NULL;
6997896f30dSPeter Hurley 	}
7007896f30dSPeter Hurley 	return retval;
70196fd7ce5SGreg Kroah-Hartman }
70296fd7ce5SGreg Kroah-Hartman 
70396fd7ce5SGreg Kroah-Hartman /**
70496fd7ce5SGreg Kroah-Hartman  *	tty_ldisc_hangup		-	hangup ldisc reset
70596fd7ce5SGreg Kroah-Hartman  *	@tty: tty being hung up
70696fd7ce5SGreg Kroah-Hartman  *
70796fd7ce5SGreg Kroah-Hartman  *	Some tty devices reset their termios when they receive a hangup
70896fd7ce5SGreg Kroah-Hartman  *	event. In that situation we must also switch back to N_TTY properly
70996fd7ce5SGreg Kroah-Hartman  *	before we reset the termios data.
71096fd7ce5SGreg Kroah-Hartman  *
71196fd7ce5SGreg Kroah-Hartman  *	Locking: We can take the ldisc mutex as the rest of the code is
71296fd7ce5SGreg Kroah-Hartman  *	careful to allow for this.
71396fd7ce5SGreg Kroah-Hartman  *
71496fd7ce5SGreg Kroah-Hartman  *	In the pty pair case this occurs in the close() path of the
71596fd7ce5SGreg Kroah-Hartman  *	tty itself so we must be careful about locking rules.
71696fd7ce5SGreg Kroah-Hartman  */
71796fd7ce5SGreg Kroah-Hartman 
718892d1fa7SPeter Hurley void tty_ldisc_hangup(struct tty_struct *tty, bool reinit)
71996fd7ce5SGreg Kroah-Hartman {
72096fd7ce5SGreg Kroah-Hartman 	struct tty_ldisc *ld;
72196fd7ce5SGreg Kroah-Hartman 
722a570a49aSPeter Hurley 	tty_ldisc_debug(tty, "%p: hangup\n", tty->ldisc);
723fc575ee6SPeter Hurley 
72496fd7ce5SGreg Kroah-Hartman 	ld = tty_ldisc_ref(tty);
72596fd7ce5SGreg Kroah-Hartman 	if (ld != NULL) {
72696fd7ce5SGreg Kroah-Hartman 		if (ld->ops->flush_buffer)
72796fd7ce5SGreg Kroah-Hartman 			ld->ops->flush_buffer(tty);
72896fd7ce5SGreg Kroah-Hartman 		tty_driver_flush_buffer(tty);
72996fd7ce5SGreg Kroah-Hartman 		if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
73096fd7ce5SGreg Kroah-Hartman 		    ld->ops->write_wakeup)
73196fd7ce5SGreg Kroah-Hartman 			ld->ops->write_wakeup(tty);
73296fd7ce5SGreg Kroah-Hartman 		if (ld->ops->hangup)
73396fd7ce5SGreg Kroah-Hartman 			ld->ops->hangup(tty);
73496fd7ce5SGreg Kroah-Hartman 		tty_ldisc_deref(ld);
73596fd7ce5SGreg Kroah-Hartman 	}
73636697529SPeter Hurley 
73796fd7ce5SGreg Kroah-Hartman 	wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
73896fd7ce5SGreg Kroah-Hartman 	wake_up_interruptible_poll(&tty->read_wait, POLLIN);
73936697529SPeter Hurley 
74096fd7ce5SGreg Kroah-Hartman 	/*
74196fd7ce5SGreg Kroah-Hartman 	 * Shutdown the current line discipline, and reset it to
74296fd7ce5SGreg Kroah-Hartman 	 * N_TTY if need be.
74396fd7ce5SGreg Kroah-Hartman 	 *
74496fd7ce5SGreg Kroah-Hartman 	 * Avoid racing set_ldisc or tty_ldisc_release
74596fd7ce5SGreg Kroah-Hartman 	 */
746fae76e9aSPeter Hurley 	tty_ldisc_lock(tty, MAX_SCHEDULE_TIMEOUT);
74796fd7ce5SGreg Kroah-Hartman 
748892d1fa7SPeter Hurley 	if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
74996fd7ce5SGreg Kroah-Hartman 		tty_reset_termios(tty);
750fc575ee6SPeter Hurley 
751892d1fa7SPeter Hurley 	if (tty->ldisc) {
752892d1fa7SPeter Hurley 		if (reinit) {
753e65c62b1SJohannes Weiner 			if (tty_ldisc_reinit(tty, tty->termios.c_line) < 0 &&
754e65c62b1SJohannes Weiner 			    tty_ldisc_reinit(tty, N_TTY) < 0)
755e65c62b1SJohannes Weiner 				WARN_ON(tty_ldisc_reinit(tty, N_NULL) < 0);
756892d1fa7SPeter Hurley 		} else
757892d1fa7SPeter Hurley 			tty_ldisc_kill(tty);
758892d1fa7SPeter Hurley 	}
759892d1fa7SPeter Hurley 	tty_ldisc_unlock(tty);
76096fd7ce5SGreg Kroah-Hartman }
76196fd7ce5SGreg Kroah-Hartman 
76296fd7ce5SGreg Kroah-Hartman /**
76396fd7ce5SGreg Kroah-Hartman  *	tty_ldisc_setup			-	open line discipline
76496fd7ce5SGreg Kroah-Hartman  *	@tty: tty being shut down
76596fd7ce5SGreg Kroah-Hartman  *	@o_tty: pair tty for pty/tty pairs
76696fd7ce5SGreg Kroah-Hartman  *
76796fd7ce5SGreg Kroah-Hartman  *	Called during the initial open of a tty/pty pair in order to set up the
76896fd7ce5SGreg Kroah-Hartman  *	line disciplines and bind them to the tty. This has no locking issues
76996fd7ce5SGreg Kroah-Hartman  *	as the device isn't yet active.
77096fd7ce5SGreg Kroah-Hartman  */
77196fd7ce5SGreg Kroah-Hartman 
77296fd7ce5SGreg Kroah-Hartman int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty)
77396fd7ce5SGreg Kroah-Hartman {
7749de2a7ceSPeter Hurley 	int retval = tty_ldisc_open(tty, tty->ldisc);
77596fd7ce5SGreg Kroah-Hartman 	if (retval)
77696fd7ce5SGreg Kroah-Hartman 		return retval;
77796fd7ce5SGreg Kroah-Hartman 
77896fd7ce5SGreg Kroah-Hartman 	if (o_tty) {
77996fd7ce5SGreg Kroah-Hartman 		retval = tty_ldisc_open(o_tty, o_tty->ldisc);
78096fd7ce5SGreg Kroah-Hartman 		if (retval) {
7819de2a7ceSPeter Hurley 			tty_ldisc_close(tty, tty->ldisc);
78296fd7ce5SGreg Kroah-Hartman 			return retval;
78396fd7ce5SGreg Kroah-Hartman 		}
78496fd7ce5SGreg Kroah-Hartman 	}
78596fd7ce5SGreg Kroah-Hartman 	return 0;
78696fd7ce5SGreg Kroah-Hartman }
78789c8d91eSAlan Cox 
78896fd7ce5SGreg Kroah-Hartman /**
78996fd7ce5SGreg Kroah-Hartman  *	tty_ldisc_release		-	release line discipline
79062462aefSPeter Hurley  *	@tty: tty being shut down (or one end of pty pair)
79196fd7ce5SGreg Kroah-Hartman  *
79262462aefSPeter Hurley  *	Called during the final close of a tty or a pty pair in order to shut
7935b6e6832SPeter Hurley  *	down the line discpline layer. On exit, each tty's ldisc is NULL.
79496fd7ce5SGreg Kroah-Hartman  */
79596fd7ce5SGreg Kroah-Hartman 
79662462aefSPeter Hurley void tty_ldisc_release(struct tty_struct *tty)
79796fd7ce5SGreg Kroah-Hartman {
79862462aefSPeter Hurley 	struct tty_struct *o_tty = tty->link;
79962462aefSPeter Hurley 
80096fd7ce5SGreg Kroah-Hartman 	/*
801a2965b7bSPeter Hurley 	 * Shutdown this line discipline. As this is the final close,
802a2965b7bSPeter Hurley 	 * it does not race with the set_ldisc code path.
80396fd7ce5SGreg Kroah-Hartman 	 */
80496fd7ce5SGreg Kroah-Hartman 
80536697529SPeter Hurley 	tty_ldisc_lock_pair(tty, o_tty);
80689c8d91eSAlan Cox 	tty_ldisc_kill(tty);
80789c8d91eSAlan Cox 	if (o_tty)
80889c8d91eSAlan Cox 		tty_ldisc_kill(o_tty);
80936697529SPeter Hurley 	tty_ldisc_unlock_pair(tty, o_tty);
81036697529SPeter Hurley 
81196fd7ce5SGreg Kroah-Hartman 	/* And the memory resources remaining (buffers, termios) will be
81296fd7ce5SGreg Kroah-Hartman 	   disposed of when the kref hits zero */
813fc575ee6SPeter Hurley 
814fb6edc91SPeter Hurley 	tty_ldisc_debug(tty, "released\n");
81596fd7ce5SGreg Kroah-Hartman }
8161ab92da3SOkash Khawaja EXPORT_SYMBOL_GPL(tty_ldisc_release);
81796fd7ce5SGreg Kroah-Hartman 
81896fd7ce5SGreg Kroah-Hartman /**
81996fd7ce5SGreg Kroah-Hartman  *	tty_ldisc_init		-	ldisc setup for new tty
82096fd7ce5SGreg Kroah-Hartman  *	@tty: tty being allocated
82196fd7ce5SGreg Kroah-Hartman  *
82296fd7ce5SGreg Kroah-Hartman  *	Set up the line discipline objects for a newly allocated tty. Note that
82396fd7ce5SGreg Kroah-Hartman  *	the tty structure is not completely set up when this call is made.
82496fd7ce5SGreg Kroah-Hartman  */
82596fd7ce5SGreg Kroah-Hartman 
82696fd7ce5SGreg Kroah-Hartman void tty_ldisc_init(struct tty_struct *tty)
82796fd7ce5SGreg Kroah-Hartman {
82836697529SPeter Hurley 	struct tty_ldisc *ld = tty_ldisc_get(tty, N_TTY);
82996fd7ce5SGreg Kroah-Hartman 	if (IS_ERR(ld))
83096fd7ce5SGreg Kroah-Hartman 		panic("n_tty: init_tty");
831f4807045SPeter Hurley 	tty->ldisc = ld;
83296fd7ce5SGreg Kroah-Hartman }
83396fd7ce5SGreg Kroah-Hartman 
8346716671dSJiri Slaby /**
835c8b710b3SPeter Hurley  *	tty_ldisc_deinit	-	ldisc cleanup for new tty
8366716671dSJiri Slaby  *	@tty: tty that was allocated recently
8376716671dSJiri Slaby  *
8386716671dSJiri Slaby  *	The tty structure must not becompletely set up (tty_ldisc_setup) when
8396716671dSJiri Slaby  *      this call is made.
8406716671dSJiri Slaby  */
8416716671dSJiri Slaby void tty_ldisc_deinit(struct tty_struct *tty)
8426716671dSJiri Slaby {
843c8b710b3SPeter Hurley 	if (tty->ldisc)
844ebc9baedSPeter Hurley 		tty_ldisc_put(tty->ldisc);
845f4807045SPeter Hurley 	tty->ldisc = NULL;
8466716671dSJiri Slaby }
847