1e3b3d0f5SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
296fd7ce5SGreg Kroah-Hartman #include <linux/types.h>
396fd7ce5SGreg Kroah-Hartman #include <linux/errno.h>
48b3ffa17SJiri Slaby #include <linux/kmod.h>
596fd7ce5SGreg Kroah-Hartman #include <linux/sched.h>
696fd7ce5SGreg Kroah-Hartman #include <linux/interrupt.h>
796fd7ce5SGreg Kroah-Hartman #include <linux/tty.h>
896fd7ce5SGreg Kroah-Hartman #include <linux/tty_driver.h>
996fd7ce5SGreg Kroah-Hartman #include <linux/file.h>
1096fd7ce5SGreg Kroah-Hartman #include <linux/mm.h>
1196fd7ce5SGreg Kroah-Hartman #include <linux/string.h>
1296fd7ce5SGreg Kroah-Hartman #include <linux/slab.h>
1396fd7ce5SGreg Kroah-Hartman #include <linux/poll.h>
1496fd7ce5SGreg Kroah-Hartman #include <linux/proc_fs.h>
1596fd7ce5SGreg Kroah-Hartman #include <linux/module.h>
1696fd7ce5SGreg Kroah-Hartman #include <linux/device.h>
1796fd7ce5SGreg Kroah-Hartman #include <linux/wait.h>
1896fd7ce5SGreg Kroah-Hartman #include <linux/bitops.h>
1996fd7ce5SGreg Kroah-Hartman #include <linux/seq_file.h>
2096fd7ce5SGreg Kroah-Hartman #include <linux/uaccess.h>
210c73c08eSJiri Slaby #include <linux/ratelimit.h>
2298602c01SGreg Kroah-Hartman #include "tty.h"
2396fd7ce5SGreg Kroah-Hartman
24fc575ee6SPeter Hurley #undef LDISC_DEBUG_HANGUP
25fc575ee6SPeter Hurley
26fc575ee6SPeter Hurley #ifdef LDISC_DEBUG_HANGUP
270a6adc13SPeter Hurley #define tty_ldisc_debug(tty, f, args...) tty_debug(tty, f, ##args)
28fc575ee6SPeter Hurley #else
29fc575ee6SPeter Hurley #define tty_ldisc_debug(tty, f, args...)
30fc575ee6SPeter Hurley #endif
31fc575ee6SPeter Hurley
32d2c43890SPeter Hurley /* lockdep nested classes for tty->ldisc_sem */
33d2c43890SPeter Hurley enum {
34d2c43890SPeter Hurley LDISC_SEM_NORMAL,
35d2c43890SPeter Hurley LDISC_SEM_OTHER,
36d2c43890SPeter Hurley };
37d2c43890SPeter Hurley
38d2c43890SPeter Hurley
3996fd7ce5SGreg Kroah-Hartman /*
4096fd7ce5SGreg Kroah-Hartman * This guards the refcounted line discipline lists. The lock
4196fd7ce5SGreg Kroah-Hartman * must be taken with irqs off because there are hangup path
4296fd7ce5SGreg Kroah-Hartman * callers who will do ldisc lookups and cannot sleep.
4396fd7ce5SGreg Kroah-Hartman */
4496fd7ce5SGreg Kroah-Hartman
45137084bbSPeter Hurley static DEFINE_RAW_SPINLOCK(tty_ldiscs_lock);
4696fd7ce5SGreg Kroah-Hartman /* Line disc dispatch table */
4796fd7ce5SGreg Kroah-Hartman static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS];
4896fd7ce5SGreg Kroah-Hartman
4996fd7ce5SGreg Kroah-Hartman /**
5096fd7ce5SGreg Kroah-Hartman * tty_register_ldisc - install a line discipline
5196fd7ce5SGreg Kroah-Hartman * @new_ldisc: pointer to the ldisc object
5296fd7ce5SGreg Kroah-Hartman *
53cbb68f91SJiri Slaby * Installs a new line discipline into the kernel. The discipline is set up as
54cbb68f91SJiri Slaby * unreferenced and then made available to the kernel from this point onwards.
5596fd7ce5SGreg Kroah-Hartman *
56cbb68f91SJiri Slaby * Locking: takes %tty_ldiscs_lock to guard against ldisc races
5796fd7ce5SGreg Kroah-Hartman */
tty_register_ldisc(struct tty_ldisc_ops * new_ldisc)58fbadf70aSJiri Slaby int tty_register_ldisc(struct tty_ldisc_ops *new_ldisc)
5996fd7ce5SGreg Kroah-Hartman {
6096fd7ce5SGreg Kroah-Hartman unsigned long flags;
6196fd7ce5SGreg Kroah-Hartman
62fbadf70aSJiri Slaby if (new_ldisc->num < N_TTY || new_ldisc->num >= NR_LDISCS)
6396fd7ce5SGreg Kroah-Hartman return -EINVAL;
6496fd7ce5SGreg Kroah-Hartman
65137084bbSPeter Hurley raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
66fbadf70aSJiri Slaby tty_ldiscs[new_ldisc->num] = new_ldisc;
67137084bbSPeter Hurley raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
6896fd7ce5SGreg Kroah-Hartman
69728648c7SLi zeming return 0;
7096fd7ce5SGreg Kroah-Hartman }
7196fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_register_ldisc);
7296fd7ce5SGreg Kroah-Hartman
7396fd7ce5SGreg Kroah-Hartman /**
7496fd7ce5SGreg Kroah-Hartman * tty_unregister_ldisc - unload a line discipline
75e2129550SBaokun Li * @ldisc: ldisc number
7696fd7ce5SGreg Kroah-Hartman *
77cbb68f91SJiri Slaby * Remove a line discipline from the kernel providing it is not currently in
78cbb68f91SJiri Slaby * use.
7996fd7ce5SGreg Kroah-Hartman *
80cbb68f91SJiri Slaby * Locking: takes %tty_ldiscs_lock to guard against ldisc races
8196fd7ce5SGreg Kroah-Hartman */
8296fd7ce5SGreg Kroah-Hartman
tty_unregister_ldisc(struct tty_ldisc_ops * ldisc)83f6f19595SJiri Slaby void tty_unregister_ldisc(struct tty_ldisc_ops *ldisc)
8496fd7ce5SGreg Kroah-Hartman {
8596fd7ce5SGreg Kroah-Hartman unsigned long flags;
8696fd7ce5SGreg Kroah-Hartman
87137084bbSPeter Hurley raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
88f81ee8b8SJiri Slaby tty_ldiscs[ldisc->num] = NULL;
89137084bbSPeter Hurley raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
9096fd7ce5SGreg Kroah-Hartman }
9196fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_unregister_ldisc);
9296fd7ce5SGreg Kroah-Hartman
get_ldops(int disc)9396fd7ce5SGreg Kroah-Hartman static struct tty_ldisc_ops *get_ldops(int disc)
9496fd7ce5SGreg Kroah-Hartman {
9596fd7ce5SGreg Kroah-Hartman unsigned long flags;
9696fd7ce5SGreg Kroah-Hartman struct tty_ldisc_ops *ldops, *ret;
9796fd7ce5SGreg Kroah-Hartman
98137084bbSPeter Hurley raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
9996fd7ce5SGreg Kroah-Hartman ret = ERR_PTR(-EINVAL);
10096fd7ce5SGreg Kroah-Hartman ldops = tty_ldiscs[disc];
10196fd7ce5SGreg Kroah-Hartman if (ldops) {
10296fd7ce5SGreg Kroah-Hartman ret = ERR_PTR(-EAGAIN);
10319475209SJiri Slaby if (try_module_get(ldops->owner))
10496fd7ce5SGreg Kroah-Hartman ret = ldops;
10596fd7ce5SGreg Kroah-Hartman }
106137084bbSPeter Hurley raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
10796fd7ce5SGreg Kroah-Hartman return ret;
10896fd7ce5SGreg Kroah-Hartman }
10996fd7ce5SGreg Kroah-Hartman
put_ldops(struct tty_ldisc_ops * ldops)11096fd7ce5SGreg Kroah-Hartman static void put_ldops(struct tty_ldisc_ops *ldops)
11196fd7ce5SGreg Kroah-Hartman {
11296fd7ce5SGreg Kroah-Hartman unsigned long flags;
11396fd7ce5SGreg Kroah-Hartman
114137084bbSPeter Hurley raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
11596fd7ce5SGreg Kroah-Hartman module_put(ldops->owner);
116137084bbSPeter Hurley raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
11796fd7ce5SGreg Kroah-Hartman }
11896fd7ce5SGreg Kroah-Hartman
1195fd8c2d3SKees Cook int tty_ldisc_autoload = IS_BUILTIN(CONFIG_LDISC_AUTOLOAD);
120cbb68f91SJiri Slaby
12196fd7ce5SGreg Kroah-Hartman /**
12296fd7ce5SGreg Kroah-Hartman * tty_ldisc_get - take a reference to an ldisc
1238a3bdec1SLee Jones * @tty: tty device
12496fd7ce5SGreg Kroah-Hartman * @disc: ldisc number
12596fd7ce5SGreg Kroah-Hartman *
126cbb68f91SJiri Slaby * Takes a reference to a line discipline. Deals with refcounts and module
127cbb68f91SJiri Slaby * locking counts. If the discipline is not available, its module loaded, if
128cbb68f91SJiri Slaby * possible.
129c0cc1c5dSPeter Hurley *
130cbb68f91SJiri Slaby * Returns:
131cbb68f91SJiri Slaby * * -%EINVAL if the discipline index is not [%N_TTY .. %NR_LDISCS] or if the
132cbb68f91SJiri Slaby * discipline is not registered
133cbb68f91SJiri Slaby * * -%EAGAIN if request_module() failed to load or register the discipline
134cbb68f91SJiri Slaby * * -%ENOMEM if allocation failure
135cbb68f91SJiri Slaby * * Otherwise, returns a pointer to the discipline and bumps the ref count
136c0cc1c5dSPeter Hurley *
137cbb68f91SJiri Slaby * Locking: takes %tty_ldiscs_lock to guard against ldisc races
13896fd7ce5SGreg Kroah-Hartman */
tty_ldisc_get(struct tty_struct * tty,int disc)13936697529SPeter Hurley static struct tty_ldisc *tty_ldisc_get(struct tty_struct *tty, int disc)
14096fd7ce5SGreg Kroah-Hartman {
14196fd7ce5SGreg Kroah-Hartman struct tty_ldisc *ld;
14296fd7ce5SGreg Kroah-Hartman struct tty_ldisc_ops *ldops;
14396fd7ce5SGreg Kroah-Hartman
14496fd7ce5SGreg Kroah-Hartman if (disc < N_TTY || disc >= NR_LDISCS)
14596fd7ce5SGreg Kroah-Hartman return ERR_PTR(-EINVAL);
14696fd7ce5SGreg Kroah-Hartman
14796fd7ce5SGreg Kroah-Hartman /*
14896fd7ce5SGreg Kroah-Hartman * Get the ldisc ops - we may need to request them to be loaded
14996fd7ce5SGreg Kroah-Hartman * dynamically and try again.
15096fd7ce5SGreg Kroah-Hartman */
15196fd7ce5SGreg Kroah-Hartman ldops = get_ldops(disc);
15296fd7ce5SGreg Kroah-Hartman if (IS_ERR(ldops)) {
1537c0cca7cSGreg Kroah-Hartman if (!capable(CAP_SYS_MODULE) && !tty_ldisc_autoload)
1547c0cca7cSGreg Kroah-Hartman return ERR_PTR(-EPERM);
15596fd7ce5SGreg Kroah-Hartman request_module("tty-ldisc-%d", disc);
15696fd7ce5SGreg Kroah-Hartman ldops = get_ldops(disc);
15796fd7ce5SGreg Kroah-Hartman if (IS_ERR(ldops))
15896fd7ce5SGreg Kroah-Hartman return ERR_CAST(ldops);
15996fd7ce5SGreg Kroah-Hartman }
16096fd7ce5SGreg Kroah-Hartman
161bcdd0ca8STetsuo Handa /*
162bcdd0ca8STetsuo Handa * There is no way to handle allocation failure of only 16 bytes.
163bcdd0ca8STetsuo Handa * Let's simplify error handling and save more memory.
164bcdd0ca8STetsuo Handa */
165bcdd0ca8STetsuo Handa ld = kmalloc(sizeof(struct tty_ldisc), GFP_KERNEL | __GFP_NOFAIL);
16696fd7ce5SGreg Kroah-Hartman ld->ops = ldops;
16736697529SPeter Hurley ld->tty = tty;
1681541f845SIvo Sieben
16996fd7ce5SGreg Kroah-Hartman return ld;
17096fd7ce5SGreg Kroah-Hartman }
17196fd7ce5SGreg Kroah-Hartman
172cbb68f91SJiri Slaby /**
173734de249SPeter Hurley * tty_ldisc_put - release the ldisc
174cbb68f91SJiri Slaby * @ld: lisdsc to release
175734de249SPeter Hurley *
176734de249SPeter Hurley * Complement of tty_ldisc_get().
177734de249SPeter Hurley */
tty_ldisc_put(struct tty_ldisc * ld)178cb128f69SDenys Vlasenko static void tty_ldisc_put(struct tty_ldisc *ld)
179734de249SPeter Hurley {
180734de249SPeter Hurley if (WARN_ON_ONCE(!ld))
181734de249SPeter Hurley return;
182734de249SPeter Hurley
18336697529SPeter Hurley put_ldops(ld->ops);
184734de249SPeter Hurley kfree(ld);
185734de249SPeter Hurley }
186734de249SPeter Hurley
tty_ldiscs_seq_start(struct seq_file * m,loff_t * pos)18796fd7ce5SGreg Kroah-Hartman static void *tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos)
18896fd7ce5SGreg Kroah-Hartman {
18996fd7ce5SGreg Kroah-Hartman return (*pos < NR_LDISCS) ? pos : NULL;
19096fd7ce5SGreg Kroah-Hartman }
19196fd7ce5SGreg Kroah-Hartman
tty_ldiscs_seq_next(struct seq_file * m,void * v,loff_t * pos)19296fd7ce5SGreg Kroah-Hartman static void *tty_ldiscs_seq_next(struct seq_file *m, void *v, loff_t *pos)
19396fd7ce5SGreg Kroah-Hartman {
19496fd7ce5SGreg Kroah-Hartman (*pos)++;
19596fd7ce5SGreg Kroah-Hartman return (*pos < NR_LDISCS) ? pos : NULL;
19696fd7ce5SGreg Kroah-Hartman }
19796fd7ce5SGreg Kroah-Hartman
tty_ldiscs_seq_stop(struct seq_file * m,void * v)19896fd7ce5SGreg Kroah-Hartman static void tty_ldiscs_seq_stop(struct seq_file *m, void *v)
19996fd7ce5SGreg Kroah-Hartman {
20096fd7ce5SGreg Kroah-Hartman }
20196fd7ce5SGreg Kroah-Hartman
tty_ldiscs_seq_show(struct seq_file * m,void * v)20296fd7ce5SGreg Kroah-Hartman static int tty_ldiscs_seq_show(struct seq_file *m, void *v)
20396fd7ce5SGreg Kroah-Hartman {
20496fd7ce5SGreg Kroah-Hartman int i = *(loff_t *)v;
20596fd7ce5SGreg Kroah-Hartman struct tty_ldisc_ops *ldops;
20696fd7ce5SGreg Kroah-Hartman
20796fd7ce5SGreg Kroah-Hartman ldops = get_ldops(i);
20896fd7ce5SGreg Kroah-Hartman if (IS_ERR(ldops))
20996fd7ce5SGreg Kroah-Hartman return 0;
21096fd7ce5SGreg Kroah-Hartman seq_printf(m, "%-10s %2d\n", ldops->name ? ldops->name : "???", i);
21196fd7ce5SGreg Kroah-Hartman put_ldops(ldops);
21296fd7ce5SGreg Kroah-Hartman return 0;
21396fd7ce5SGreg Kroah-Hartman }
21496fd7ce5SGreg Kroah-Hartman
215fddda2b7SChristoph Hellwig const struct seq_operations tty_ldiscs_seq_ops = {
21696fd7ce5SGreg Kroah-Hartman .start = tty_ldiscs_seq_start,
21796fd7ce5SGreg Kroah-Hartman .next = tty_ldiscs_seq_next,
21896fd7ce5SGreg Kroah-Hartman .stop = tty_ldiscs_seq_stop,
21996fd7ce5SGreg Kroah-Hartman .show = tty_ldiscs_seq_show,
22096fd7ce5SGreg Kroah-Hartman };
22196fd7ce5SGreg Kroah-Hartman
22296fd7ce5SGreg Kroah-Hartman /**
22396fd7ce5SGreg Kroah-Hartman * tty_ldisc_ref_wait - wait for the tty ldisc
22496fd7ce5SGreg Kroah-Hartman * @tty: tty device
22596fd7ce5SGreg Kroah-Hartman *
226cbb68f91SJiri Slaby * Dereference the line discipline for the terminal and take a reference to it.
227cbb68f91SJiri Slaby * If the line discipline is in flux then wait patiently until it changes.
22896fd7ce5SGreg Kroah-Hartman *
229cbb68f91SJiri Slaby * Returns: %NULL if the tty has been hungup and not re-opened with a new file
230cbb68f91SJiri Slaby * descriptor, otherwise valid ldisc reference
231892d1fa7SPeter Hurley *
232cbb68f91SJiri Slaby * Note 1: Must not be called from an IRQ/timer context. The caller must also
233cbb68f91SJiri Slaby * be careful not to hold other locks that will deadlock against a discipline
234cbb68f91SJiri Slaby * change, such as an existing ldisc reference (which we check for).
23596fd7ce5SGreg Kroah-Hartman *
236cbb68f91SJiri Slaby * Note 2: a file_operations routine (read/poll/write) should use this function
237cbb68f91SJiri Slaby * to wait for any ldisc lifetime events to finish.
23896fd7ce5SGreg Kroah-Hartman */
tty_ldisc_ref_wait(struct tty_struct * tty)23996fd7ce5SGreg Kroah-Hartman struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty)
24096fd7ce5SGreg Kroah-Hartman {
241a4a3e061SDmitry Vyukov struct tty_ldisc *ld;
242a4a3e061SDmitry Vyukov
24336697529SPeter Hurley ldsem_down_read(&tty->ldisc_sem, MAX_SCHEDULE_TIMEOUT);
244a4a3e061SDmitry Vyukov ld = tty->ldisc;
245a4a3e061SDmitry Vyukov if (!ld)
246a570a49aSPeter Hurley ldsem_up_read(&tty->ldisc_sem);
247a4a3e061SDmitry Vyukov return ld;
24896fd7ce5SGreg Kroah-Hartman }
24996fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait);
25096fd7ce5SGreg Kroah-Hartman
25196fd7ce5SGreg Kroah-Hartman /**
25296fd7ce5SGreg Kroah-Hartman * tty_ldisc_ref - get the tty ldisc
25396fd7ce5SGreg Kroah-Hartman * @tty: tty device
25496fd7ce5SGreg Kroah-Hartman *
255cbb68f91SJiri Slaby * Dereference the line discipline for the terminal and take a reference to it.
256cbb68f91SJiri Slaby * If the line discipline is in flux then return %NULL. Can be called from IRQ
257cbb68f91SJiri Slaby * and timer functions.
25896fd7ce5SGreg Kroah-Hartman */
tty_ldisc_ref(struct tty_struct * tty)25996fd7ce5SGreg Kroah-Hartman struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty)
26096fd7ce5SGreg Kroah-Hartman {
26136697529SPeter Hurley struct tty_ldisc *ld = NULL;
26236697529SPeter Hurley
26336697529SPeter Hurley if (ldsem_down_read_trylock(&tty->ldisc_sem)) {
26436697529SPeter Hurley ld = tty->ldisc;
26536697529SPeter Hurley if (!ld)
26636697529SPeter Hurley ldsem_up_read(&tty->ldisc_sem);
26736697529SPeter Hurley }
26836697529SPeter Hurley return ld;
26996fd7ce5SGreg Kroah-Hartman }
27096fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(tty_ldisc_ref);
27196fd7ce5SGreg Kroah-Hartman
27296fd7ce5SGreg Kroah-Hartman /**
27396fd7ce5SGreg Kroah-Hartman * tty_ldisc_deref - free a tty ldisc reference
27496fd7ce5SGreg Kroah-Hartman * @ld: reference to free up
27596fd7ce5SGreg Kroah-Hartman *
276cbb68f91SJiri Slaby * Undoes the effect of tty_ldisc_ref() or tty_ldisc_ref_wait(). May be called
277cbb68f91SJiri Slaby * in IRQ context.
27896fd7ce5SGreg Kroah-Hartman */
tty_ldisc_deref(struct tty_ldisc * ld)27996fd7ce5SGreg Kroah-Hartman void tty_ldisc_deref(struct tty_ldisc *ld)
28096fd7ce5SGreg Kroah-Hartman {
28136697529SPeter Hurley ldsem_up_read(&ld->tty->ldisc_sem);
28296fd7ce5SGreg Kroah-Hartman }
28396fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(tty_ldisc_deref);
28496fd7ce5SGreg Kroah-Hartman
285d2c43890SPeter Hurley
286c2bb524bSPeter Hurley static inline int
__tty_ldisc_lock(struct tty_struct * tty,unsigned long timeout)287e80a10eeSPeter Hurley __tty_ldisc_lock(struct tty_struct *tty, unsigned long timeout)
288d2c43890SPeter Hurley {
289d2c43890SPeter Hurley return ldsem_down_write(&tty->ldisc_sem, timeout);
290d2c43890SPeter Hurley }
291d2c43890SPeter Hurley
292c2bb524bSPeter Hurley static inline int
__tty_ldisc_lock_nested(struct tty_struct * tty,unsigned long timeout)293e80a10eeSPeter Hurley __tty_ldisc_lock_nested(struct tty_struct *tty, unsigned long timeout)
294d2c43890SPeter Hurley {
295d2c43890SPeter Hurley return ldsem_down_write_nested(&tty->ldisc_sem,
296d2c43890SPeter Hurley LDISC_SEM_OTHER, timeout);
297d2c43890SPeter Hurley }
298d2c43890SPeter Hurley
__tty_ldisc_unlock(struct tty_struct * tty)299e80a10eeSPeter Hurley static inline void __tty_ldisc_unlock(struct tty_struct *tty)
300d2c43890SPeter Hurley {
30152772ea6SGuillaume Gomez ldsem_up_write(&tty->ldisc_sem);
302d2c43890SPeter Hurley }
303d2c43890SPeter Hurley
tty_ldisc_lock(struct tty_struct * tty,unsigned long timeout)304b027e229SGaurav Kohli int tty_ldisc_lock(struct tty_struct *tty, unsigned long timeout)
305fae76e9aSPeter Hurley {
306fae76e9aSPeter Hurley int ret;
307fae76e9aSPeter Hurley
308c96cf923SDmitry Safonov /* Kindly asking blocked readers to release the read side */
309c96cf923SDmitry Safonov set_bit(TTY_LDISC_CHANGING, &tty->flags);
310c96cf923SDmitry Safonov wake_up_interruptible_all(&tty->read_wait);
311c96cf923SDmitry Safonov wake_up_interruptible_all(&tty->write_wait);
312c96cf923SDmitry Safonov
313fae76e9aSPeter Hurley ret = __tty_ldisc_lock(tty, timeout);
314fae76e9aSPeter Hurley if (!ret)
315fae76e9aSPeter Hurley return -EBUSY;
316fae76e9aSPeter Hurley set_bit(TTY_LDISC_HALTED, &tty->flags);
317fae76e9aSPeter Hurley return 0;
318fae76e9aSPeter Hurley }
319fae76e9aSPeter Hurley
tty_ldisc_unlock(struct tty_struct * tty)320b027e229SGaurav Kohli void tty_ldisc_unlock(struct tty_struct *tty)
321fae76e9aSPeter Hurley {
322fae76e9aSPeter Hurley clear_bit(TTY_LDISC_HALTED, &tty->flags);
323c96cf923SDmitry Safonov /* Can be cleared here - ldisc_unlock will wake up writers firstly */
324c96cf923SDmitry Safonov clear_bit(TTY_LDISC_CHANGING, &tty->flags);
325fae76e9aSPeter Hurley __tty_ldisc_unlock(tty);
326fae76e9aSPeter Hurley }
327fae76e9aSPeter Hurley
328c2bb524bSPeter Hurley static int
tty_ldisc_lock_pair_timeout(struct tty_struct * tty,struct tty_struct * tty2,unsigned long timeout)329d2c43890SPeter Hurley tty_ldisc_lock_pair_timeout(struct tty_struct *tty, struct tty_struct *tty2,
330d2c43890SPeter Hurley unsigned long timeout)
331d2c43890SPeter Hurley {
332d2c43890SPeter Hurley int ret;
333d2c43890SPeter Hurley
334d2c43890SPeter Hurley if (tty < tty2) {
335e80a10eeSPeter Hurley ret = __tty_ldisc_lock(tty, timeout);
336d2c43890SPeter Hurley if (ret) {
337e80a10eeSPeter Hurley ret = __tty_ldisc_lock_nested(tty2, timeout);
338d2c43890SPeter Hurley if (!ret)
339e80a10eeSPeter Hurley __tty_ldisc_unlock(tty);
340d2c43890SPeter Hurley }
341d2c43890SPeter Hurley } else {
342d2c43890SPeter Hurley /* if this is possible, it has lots of implications */
343d2c43890SPeter Hurley WARN_ON_ONCE(tty == tty2);
344d2c43890SPeter Hurley if (tty2 && tty != tty2) {
345e80a10eeSPeter Hurley ret = __tty_ldisc_lock(tty2, timeout);
346d2c43890SPeter Hurley if (ret) {
347e80a10eeSPeter Hurley ret = __tty_ldisc_lock_nested(tty, timeout);
348d2c43890SPeter Hurley if (!ret)
349e80a10eeSPeter Hurley __tty_ldisc_unlock(tty2);
350d2c43890SPeter Hurley }
351d2c43890SPeter Hurley } else
352e80a10eeSPeter Hurley ret = __tty_ldisc_lock(tty, timeout);
353d2c43890SPeter Hurley }
354d2c43890SPeter Hurley
355d2c43890SPeter Hurley if (!ret)
356d2c43890SPeter Hurley return -EBUSY;
357d2c43890SPeter Hurley
358d2c43890SPeter Hurley set_bit(TTY_LDISC_HALTED, &tty->flags);
359d2c43890SPeter Hurley if (tty2)
360d2c43890SPeter Hurley set_bit(TTY_LDISC_HALTED, &tty2->flags);
361d2c43890SPeter Hurley return 0;
362d2c43890SPeter Hurley }
363d2c43890SPeter Hurley
tty_ldisc_lock_pair(struct tty_struct * tty,struct tty_struct * tty2)364c2bb524bSPeter Hurley static void tty_ldisc_lock_pair(struct tty_struct *tty, struct tty_struct *tty2)
365d2c43890SPeter Hurley {
366d2c43890SPeter Hurley tty_ldisc_lock_pair_timeout(tty, tty2, MAX_SCHEDULE_TIMEOUT);
367d2c43890SPeter Hurley }
368d2c43890SPeter Hurley
tty_ldisc_unlock_pair(struct tty_struct * tty,struct tty_struct * tty2)369c2bb524bSPeter Hurley static void tty_ldisc_unlock_pair(struct tty_struct *tty,
370d2c43890SPeter Hurley struct tty_struct *tty2)
371d2c43890SPeter Hurley {
372e80a10eeSPeter Hurley __tty_ldisc_unlock(tty);
373d2c43890SPeter Hurley if (tty2)
374e80a10eeSPeter Hurley __tty_ldisc_unlock(tty2);
375d2c43890SPeter Hurley }
376d2c43890SPeter Hurley
37796fd7ce5SGreg Kroah-Hartman /**
37896fd7ce5SGreg Kroah-Hartman * tty_ldisc_flush - flush line discipline queue
379cbb68f91SJiri Slaby * @tty: tty to flush ldisc for
38096fd7ce5SGreg Kroah-Hartman *
381cbb68f91SJiri Slaby * Flush the line discipline queue (if any) and the tty flip buffers for this
382cbb68f91SJiri Slaby * @tty.
38396fd7ce5SGreg Kroah-Hartman */
tty_ldisc_flush(struct tty_struct * tty)38496fd7ce5SGreg Kroah-Hartman void tty_ldisc_flush(struct tty_struct *tty)
38596fd7ce5SGreg Kroah-Hartman {
38696fd7ce5SGreg Kroah-Hartman struct tty_ldisc *ld = tty_ldisc_ref(tty);
38786c80a8eSPeter Hurley
38886c80a8eSPeter Hurley tty_buffer_flush(tty, ld);
38986c80a8eSPeter Hurley if (ld)
39096fd7ce5SGreg Kroah-Hartman tty_ldisc_deref(ld);
39196fd7ce5SGreg Kroah-Hartman }
39296fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(tty_ldisc_flush);
39396fd7ce5SGreg Kroah-Hartman
39496fd7ce5SGreg Kroah-Hartman /**
39596fd7ce5SGreg Kroah-Hartman * tty_set_termios_ldisc - set ldisc field
39696fd7ce5SGreg Kroah-Hartman * @tty: tty structure
397c12da96fSPeter Hurley * @disc: line discipline number
39896fd7ce5SGreg Kroah-Hartman *
399cbb68f91SJiri Slaby * This is probably overkill for real world processors but they are not on hot
400cbb68f91SJiri Slaby * paths so a little discipline won't do any harm.
40196fd7ce5SGreg Kroah-Hartman *
402cbb68f91SJiri Slaby * The line discipline-related tty_struct fields are reset to prevent the ldisc
403cbb68f91SJiri Slaby * driver from re-using stale information for the new ldisc instance.
404dd42bf11SPeter Hurley *
4056a1c0680SPeter Hurley * Locking: takes termios_rwsem
40696fd7ce5SGreg Kroah-Hartman */
tty_set_termios_ldisc(struct tty_struct * tty,int disc)407c12da96fSPeter Hurley static void tty_set_termios_ldisc(struct tty_struct *tty, int disc)
40896fd7ce5SGreg Kroah-Hartman {
4096a1c0680SPeter Hurley down_write(&tty->termios_rwsem);
410c12da96fSPeter Hurley tty->termios.c_line = disc;
4116a1c0680SPeter Hurley up_write(&tty->termios_rwsem);
412dd42bf11SPeter Hurley
413dd42bf11SPeter Hurley tty->disc_data = NULL;
414dd42bf11SPeter Hurley tty->receive_room = 0;
41596fd7ce5SGreg Kroah-Hartman }
41696fd7ce5SGreg Kroah-Hartman
41796fd7ce5SGreg Kroah-Hartman /**
41896fd7ce5SGreg Kroah-Hartman * tty_ldisc_open - open a line discipline
41996fd7ce5SGreg Kroah-Hartman * @tty: tty we are opening the ldisc on
42096fd7ce5SGreg Kroah-Hartman * @ld: discipline to open
42196fd7ce5SGreg Kroah-Hartman *
422cbb68f91SJiri Slaby * A helper opening method. Also a convenient debugging and check point.
42396fd7ce5SGreg Kroah-Hartman *
42496fd7ce5SGreg Kroah-Hartman * Locking: always called with BTM already held.
42596fd7ce5SGreg Kroah-Hartman */
tty_ldisc_open(struct tty_struct * tty,struct tty_ldisc * ld)42696fd7ce5SGreg Kroah-Hartman static int tty_ldisc_open(struct tty_struct *tty, struct tty_ldisc *ld)
42796fd7ce5SGreg Kroah-Hartman {
42896fd7ce5SGreg Kroah-Hartman WARN_ON(test_and_set_bit(TTY_LDISC_OPEN, &tty->flags));
42996fd7ce5SGreg Kroah-Hartman if (ld->ops->open) {
43096fd7ce5SGreg Kroah-Hartman int ret;
43196fd7ce5SGreg Kroah-Hartman /* BTM here locks versus a hangup event */
43296fd7ce5SGreg Kroah-Hartman ret = ld->ops->open(tty);
4337f90cfc5SJiri Slaby if (ret)
4347f90cfc5SJiri Slaby clear_bit(TTY_LDISC_OPEN, &tty->flags);
435fb6edc91SPeter Hurley
436a570a49aSPeter Hurley tty_ldisc_debug(tty, "%p: opened\n", ld);
43796fd7ce5SGreg Kroah-Hartman return ret;
43896fd7ce5SGreg Kroah-Hartman }
43996fd7ce5SGreg Kroah-Hartman return 0;
44096fd7ce5SGreg Kroah-Hartman }
44196fd7ce5SGreg Kroah-Hartman
44296fd7ce5SGreg Kroah-Hartman /**
44396fd7ce5SGreg Kroah-Hartman * tty_ldisc_close - close a line discipline
44496fd7ce5SGreg Kroah-Hartman * @tty: tty we are opening the ldisc on
44596fd7ce5SGreg Kroah-Hartman * @ld: discipline to close
44696fd7ce5SGreg Kroah-Hartman *
447cbb68f91SJiri Slaby * A helper close method. Also a convenient debugging and check point.
44896fd7ce5SGreg Kroah-Hartman */
tty_ldisc_close(struct tty_struct * tty,struct tty_ldisc * ld)44996fd7ce5SGreg Kroah-Hartman static void tty_ldisc_close(struct tty_struct *tty, struct tty_ldisc *ld)
45096fd7ce5SGreg Kroah-Hartman {
4519ffbe8acSNikolay Borisov lockdep_assert_held_write(&tty->ldisc_sem);
45296fd7ce5SGreg Kroah-Hartman WARN_ON(!test_bit(TTY_LDISC_OPEN, &tty->flags));
45396fd7ce5SGreg Kroah-Hartman clear_bit(TTY_LDISC_OPEN, &tty->flags);
45496fd7ce5SGreg Kroah-Hartman if (ld->ops->close)
45596fd7ce5SGreg Kroah-Hartman ld->ops->close(tty);
456a570a49aSPeter Hurley tty_ldisc_debug(tty, "%p: closed\n", ld);
45796fd7ce5SGreg Kroah-Hartman }
45896fd7ce5SGreg Kroah-Hartman
45996fd7ce5SGreg Kroah-Hartman /**
4608a8dabf2SAlan Cox * tty_ldisc_failto - helper for ldisc failback
4618a8dabf2SAlan Cox * @tty: tty to open the ldisc on
4628a8dabf2SAlan Cox * @ld: ldisc we are trying to fail back to
4638a8dabf2SAlan Cox *
464cbb68f91SJiri Slaby * Helper to try and recover a tty when switching back to the old ldisc fails
465cbb68f91SJiri Slaby * and we need something attached.
4668a8dabf2SAlan Cox */
tty_ldisc_failto(struct tty_struct * tty,int ld)4678a8dabf2SAlan Cox static int tty_ldisc_failto(struct tty_struct *tty, int ld)
4688a8dabf2SAlan Cox {
4698a8dabf2SAlan Cox struct tty_ldisc *disc = tty_ldisc_get(tty, ld);
4708a8dabf2SAlan Cox int r;
4718a8dabf2SAlan Cox
4729ffbe8acSNikolay Borisov lockdep_assert_held_write(&tty->ldisc_sem);
4738a8dabf2SAlan Cox if (IS_ERR(disc))
4748a8dabf2SAlan Cox return PTR_ERR(disc);
4758a8dabf2SAlan Cox tty->ldisc = disc;
4768a8dabf2SAlan Cox tty_set_termios_ldisc(tty, ld);
477408795b0SXiaofei Tan r = tty_ldisc_open(tty, disc);
478408795b0SXiaofei Tan if (r < 0)
4798a8dabf2SAlan Cox tty_ldisc_put(disc);
4808a8dabf2SAlan Cox return r;
4818a8dabf2SAlan Cox }
4828a8dabf2SAlan Cox
4838a8dabf2SAlan Cox /**
484a8983d01SGreg Kroah-Hartman * tty_ldisc_restore - helper for tty ldisc change
485a8983d01SGreg Kroah-Hartman * @tty: tty to recover
486a8983d01SGreg Kroah-Hartman * @old: previous ldisc
487a8983d01SGreg Kroah-Hartman *
488cbb68f91SJiri Slaby * Restore the previous line discipline or %N_TTY when a line discipline change
489cbb68f91SJiri Slaby * fails due to an open error
490a8983d01SGreg Kroah-Hartman */
tty_ldisc_restore(struct tty_struct * tty,struct tty_ldisc * old)491a8983d01SGreg Kroah-Hartman static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old)
492a8983d01SGreg Kroah-Hartman {
493a8983d01SGreg Kroah-Hartman /* There is an outstanding reference here so this is safe */
494598c2d41STetsuo Handa if (tty_ldisc_failto(tty, old->ops->num) < 0) {
495598c2d41STetsuo Handa const char *name = tty_name(tty);
496598c2d41STetsuo Handa
497598c2d41STetsuo Handa pr_warn("Falling back ldisc for %s.\n", name);
49872a8dcd7SXiaofei Tan /*
49972a8dcd7SXiaofei Tan * The traditional behaviour is to fall back to N_TTY, we
50072a8dcd7SXiaofei Tan * want to avoid falling back to N_NULL unless we have no
50172a8dcd7SXiaofei Tan * choice to avoid the risk of breaking anything
50272a8dcd7SXiaofei Tan */
5038a8dabf2SAlan Cox if (tty_ldisc_failto(tty, N_TTY) < 0 &&
5048a8dabf2SAlan Cox tty_ldisc_failto(tty, N_NULL) < 0)
505598c2d41STetsuo Handa panic("Couldn't open N_NULL ldisc for %s.", name);
506a8983d01SGreg Kroah-Hartman }
507a8983d01SGreg Kroah-Hartman }
508a8983d01SGreg Kroah-Hartman
509a8983d01SGreg Kroah-Hartman /**
51096fd7ce5SGreg Kroah-Hartman * tty_set_ldisc - set line discipline
51196fd7ce5SGreg Kroah-Hartman * @tty: the terminal to set
512fa441954SJiri Slaby * @disc: the line discipline number
51396fd7ce5SGreg Kroah-Hartman *
514cbb68f91SJiri Slaby * Set the discipline of a tty line. Must be called from a process context. The
515cbb68f91SJiri Slaby * ldisc change logic has to protect itself against any overlapping ldisc
516cbb68f91SJiri Slaby * change (including on the other end of pty pairs), the close of one side of a
517cbb68f91SJiri Slaby * tty/pty pair, and eventually hangup.
51896fd7ce5SGreg Kroah-Hartman */
tty_set_ldisc(struct tty_struct * tty,int disc)519c12da96fSPeter Hurley int tty_set_ldisc(struct tty_struct *tty, int disc)
52096fd7ce5SGreg Kroah-Hartman {
521a8983d01SGreg Kroah-Hartman int retval;
522a8983d01SGreg Kroah-Hartman struct tty_ldisc *old_ldisc, *new_ldisc;
523a8983d01SGreg Kroah-Hartman
524a8983d01SGreg Kroah-Hartman new_ldisc = tty_ldisc_get(tty, disc);
525a8983d01SGreg Kroah-Hartman if (IS_ERR(new_ldisc))
526a8983d01SGreg Kroah-Hartman return PTR_ERR(new_ldisc);
52796fd7ce5SGreg Kroah-Hartman
528c8483bc9SPeter Hurley tty_lock(tty);
529276a661aSPeter Hurley retval = tty_ldisc_lock(tty, 5 * HZ);
53063d8cb3fSPeter Hurley if (retval)
53163d8cb3fSPeter Hurley goto err;
53296fd7ce5SGreg Kroah-Hartman
533a570a49aSPeter Hurley if (!tty->ldisc) {
534a570a49aSPeter Hurley retval = -EIO;
535a570a49aSPeter Hurley goto out;
536a570a49aSPeter Hurley }
537a570a49aSPeter Hurley
53863d8cb3fSPeter Hurley /* Check the no-op case */
539a8983d01SGreg Kroah-Hartman if (tty->ldisc->ops->num == disc)
54063d8cb3fSPeter Hurley goto out;
54196fd7ce5SGreg Kroah-Hartman
54263d8cb3fSPeter Hurley if (test_bit(TTY_HUPPED, &tty->flags)) {
54363d8cb3fSPeter Hurley /* We were raced by hangup */
54463d8cb3fSPeter Hurley retval = -EIO;
54563d8cb3fSPeter Hurley goto out;
54696fd7ce5SGreg Kroah-Hartman }
54796fd7ce5SGreg Kroah-Hartman
548*287b569aSLinus Torvalds if (tty->ops->ldisc_ok) {
549*287b569aSLinus Torvalds retval = tty->ops->ldisc_ok(tty, disc);
550*287b569aSLinus Torvalds if (retval)
551*287b569aSLinus Torvalds goto out;
552*287b569aSLinus Torvalds }
553*287b569aSLinus Torvalds
554a8983d01SGreg Kroah-Hartman old_ldisc = tty->ldisc;
555a8983d01SGreg Kroah-Hartman
556a8983d01SGreg Kroah-Hartman /* Shutdown the old discipline. */
557a8983d01SGreg Kroah-Hartman tty_ldisc_close(tty, old_ldisc);
558a8983d01SGreg Kroah-Hartman
559a8983d01SGreg Kroah-Hartman /* Now set up the new line discipline. */
560a8983d01SGreg Kroah-Hartman tty->ldisc = new_ldisc;
561a8983d01SGreg Kroah-Hartman tty_set_termios_ldisc(tty, disc);
562a8983d01SGreg Kroah-Hartman
563a8983d01SGreg Kroah-Hartman retval = tty_ldisc_open(tty, new_ldisc);
56496fd7ce5SGreg Kroah-Hartman if (retval < 0) {
56596fd7ce5SGreg Kroah-Hartman /* Back to the old one or N_TTY if we can't */
566a8983d01SGreg Kroah-Hartman tty_ldisc_put(new_ldisc);
567a8983d01SGreg Kroah-Hartman tty_ldisc_restore(tty, old_ldisc);
56896fd7ce5SGreg Kroah-Hartman }
56996fd7ce5SGreg Kroah-Hartman
570a8983d01SGreg Kroah-Hartman if (tty->ldisc->ops->num != old_ldisc->ops->num && tty->ops->set_ldisc) {
5719191aaaaSPeter Hurley down_read(&tty->termios_rwsem);
57296fd7ce5SGreg Kroah-Hartman tty->ops->set_ldisc(tty);
5739191aaaaSPeter Hurley up_read(&tty->termios_rwsem);
5749191aaaaSPeter Hurley }
57596fd7ce5SGreg Kroah-Hartman
57672a8dcd7SXiaofei Tan /*
57772a8dcd7SXiaofei Tan * At this point we hold a reference to the new ldisc and a
57872a8dcd7SXiaofei Tan * reference to the old ldisc, or we hold two references to
57972a8dcd7SXiaofei Tan * the old ldisc (if it was restored as part of error cleanup
58072a8dcd7SXiaofei Tan * above). In either case, releasing a single reference from
58172a8dcd7SXiaofei Tan * the old ldisc is correct.
58272a8dcd7SXiaofei Tan */
583a8983d01SGreg Kroah-Hartman new_ldisc = old_ldisc;
58463d8cb3fSPeter Hurley out:
585276a661aSPeter Hurley tty_ldisc_unlock(tty);
58696fd7ce5SGreg Kroah-Hartman
58772a8dcd7SXiaofei Tan /*
58872a8dcd7SXiaofei Tan * Restart the work queue in case no characters kick it off. Safe if
58972a8dcd7SXiaofei Tan * already running
59072a8dcd7SXiaofei Tan */
59117a69219SPeter Hurley tty_buffer_restart_work(tty->port);
59263d8cb3fSPeter Hurley err:
593a8983d01SGreg Kroah-Hartman tty_ldisc_put(new_ldisc); /* drop the extra reference */
59489c8d91eSAlan Cox tty_unlock(tty);
59596fd7ce5SGreg Kroah-Hartman return retval;
59696fd7ce5SGreg Kroah-Hartman }
5971ab92da3SOkash Khawaja EXPORT_SYMBOL_GPL(tty_set_ldisc);
59896fd7ce5SGreg Kroah-Hartman
59996fd7ce5SGreg Kroah-Hartman /**
6006ffeb4b2SPeter Hurley * tty_ldisc_kill - teardown ldisc
6016ffeb4b2SPeter Hurley * @tty: tty being released
6026ffeb4b2SPeter Hurley *
603cbb68f91SJiri Slaby * Perform final close of the ldisc and reset @tty->ldisc
6046ffeb4b2SPeter Hurley */
tty_ldisc_kill(struct tty_struct * tty)6056ffeb4b2SPeter Hurley static void tty_ldisc_kill(struct tty_struct *tty)
6066ffeb4b2SPeter Hurley {
6079ffbe8acSNikolay Borisov lockdep_assert_held_write(&tty->ldisc_sem);
6086ffeb4b2SPeter Hurley if (!tty->ldisc)
6096ffeb4b2SPeter Hurley return;
6106ffeb4b2SPeter Hurley /*
6116ffeb4b2SPeter Hurley * Now kill off the ldisc
6126ffeb4b2SPeter Hurley */
6136ffeb4b2SPeter Hurley tty_ldisc_close(tty, tty->ldisc);
6146ffeb4b2SPeter Hurley tty_ldisc_put(tty->ldisc);
6156ffeb4b2SPeter Hurley /* Force an oops if we mess this up */
6166ffeb4b2SPeter Hurley tty->ldisc = NULL;
6176ffeb4b2SPeter Hurley }
6186ffeb4b2SPeter Hurley
6196ffeb4b2SPeter Hurley /**
62096fd7ce5SGreg Kroah-Hartman * tty_reset_termios - reset terminal state
62196fd7ce5SGreg Kroah-Hartman * @tty: tty to reset
62296fd7ce5SGreg Kroah-Hartman *
62396fd7ce5SGreg Kroah-Hartman * Restore a terminal to the driver default state.
62496fd7ce5SGreg Kroah-Hartman */
tty_reset_termios(struct tty_struct * tty)62596fd7ce5SGreg Kroah-Hartman static void tty_reset_termios(struct tty_struct *tty)
62696fd7ce5SGreg Kroah-Hartman {
6276a1c0680SPeter Hurley down_write(&tty->termios_rwsem);
628adc8d746SAlan Cox tty->termios = tty->driver->init_termios;
629adc8d746SAlan Cox tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios);
630adc8d746SAlan Cox tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios);
6316a1c0680SPeter Hurley up_write(&tty->termios_rwsem);
63296fd7ce5SGreg Kroah-Hartman }
63396fd7ce5SGreg Kroah-Hartman
63496fd7ce5SGreg Kroah-Hartman
63596fd7ce5SGreg Kroah-Hartman /**
63696fd7ce5SGreg Kroah-Hartman * tty_ldisc_reinit - reinitialise the tty ldisc
63796fd7ce5SGreg Kroah-Hartman * @tty: tty to reinit
638c12da96fSPeter Hurley * @disc: line discipline to reinitialize
63996fd7ce5SGreg Kroah-Hartman *
640cbb68f91SJiri Slaby * Completely reinitialize the line discipline state, by closing the current
641cbb68f91SJiri Slaby * instance, if there is one, and opening a new instance. If an error occurs
642cbb68f91SJiri Slaby * opening the new non-%N_TTY instance, the instance is dropped and @tty->ldisc
643cbb68f91SJiri Slaby * reset to %NULL. The caller can then retry with %N_TTY instead.
6447896f30dSPeter Hurley *
645cbb68f91SJiri Slaby * Returns: 0 if successful, otherwise error code < 0
64696fd7ce5SGreg Kroah-Hartman */
tty_ldisc_reinit(struct tty_struct * tty,int disc)647892d1fa7SPeter Hurley int tty_ldisc_reinit(struct tty_struct *tty, int disc)
64896fd7ce5SGreg Kroah-Hartman {
6497896f30dSPeter Hurley struct tty_ldisc *ld;
6507896f30dSPeter Hurley int retval;
6511c95ba1eSPhilippe Rétornaz
6529ffbe8acSNikolay Borisov lockdep_assert_held_write(&tty->ldisc_sem);
6537896f30dSPeter Hurley ld = tty_ldisc_get(tty, disc);
654a8983d01SGreg Kroah-Hartman if (IS_ERR(ld)) {
655a8983d01SGreg Kroah-Hartman BUG_ON(disc == N_TTY);
6567896f30dSPeter Hurley return PTR_ERR(ld);
657a8983d01SGreg Kroah-Hartman }
65896fd7ce5SGreg Kroah-Hartman
6597896f30dSPeter Hurley if (tty->ldisc) {
66096fd7ce5SGreg Kroah-Hartman tty_ldisc_close(tty, tty->ldisc);
66196fd7ce5SGreg Kroah-Hartman tty_ldisc_put(tty->ldisc);
6627896f30dSPeter Hurley }
6637896f30dSPeter Hurley
6647896f30dSPeter Hurley /* switch the line discipline */
665f4807045SPeter Hurley tty->ldisc = ld;
666c12da96fSPeter Hurley tty_set_termios_ldisc(tty, disc);
6677896f30dSPeter Hurley retval = tty_ldisc_open(tty, tty->ldisc);
6687896f30dSPeter Hurley if (retval) {
6697896f30dSPeter Hurley tty_ldisc_put(tty->ldisc);
6707896f30dSPeter Hurley tty->ldisc = NULL;
6717896f30dSPeter Hurley }
6727896f30dSPeter Hurley return retval;
67396fd7ce5SGreg Kroah-Hartman }
67496fd7ce5SGreg Kroah-Hartman
67596fd7ce5SGreg Kroah-Hartman /**
67696fd7ce5SGreg Kroah-Hartman * tty_ldisc_hangup - hangup ldisc reset
67796fd7ce5SGreg Kroah-Hartman * @tty: tty being hung up
6788eddcca2SLee Jones * @reinit: whether to re-initialise the tty
67996fd7ce5SGreg Kroah-Hartman *
680cbb68f91SJiri Slaby * Some tty devices reset their termios when they receive a hangup event. In
681cbb68f91SJiri Slaby * that situation we must also switch back to %N_TTY properly before we reset
682cbb68f91SJiri Slaby * the termios data.
68396fd7ce5SGreg Kroah-Hartman *
684cbb68f91SJiri Slaby * Locking: We can take the ldisc mutex as the rest of the code is careful to
685cbb68f91SJiri Slaby * allow for this.
68696fd7ce5SGreg Kroah-Hartman *
687cbb68f91SJiri Slaby * In the pty pair case this occurs in the close() path of the tty itself so we
688cbb68f91SJiri Slaby * must be careful about locking rules.
68996fd7ce5SGreg Kroah-Hartman */
tty_ldisc_hangup(struct tty_struct * tty,bool reinit)690892d1fa7SPeter Hurley void tty_ldisc_hangup(struct tty_struct *tty, bool reinit)
69196fd7ce5SGreg Kroah-Hartman {
69296fd7ce5SGreg Kroah-Hartman struct tty_ldisc *ld;
69396fd7ce5SGreg Kroah-Hartman
694a570a49aSPeter Hurley tty_ldisc_debug(tty, "%p: hangup\n", tty->ldisc);
695fc575ee6SPeter Hurley
69696fd7ce5SGreg Kroah-Hartman ld = tty_ldisc_ref(tty);
69796fd7ce5SGreg Kroah-Hartman if (ld != NULL) {
69896fd7ce5SGreg Kroah-Hartman if (ld->ops->flush_buffer)
69996fd7ce5SGreg Kroah-Hartman ld->ops->flush_buffer(tty);
70096fd7ce5SGreg Kroah-Hartman tty_driver_flush_buffer(tty);
70196fd7ce5SGreg Kroah-Hartman if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
70296fd7ce5SGreg Kroah-Hartman ld->ops->write_wakeup)
70396fd7ce5SGreg Kroah-Hartman ld->ops->write_wakeup(tty);
70496fd7ce5SGreg Kroah-Hartman if (ld->ops->hangup)
70596fd7ce5SGreg Kroah-Hartman ld->ops->hangup(tty);
70696fd7ce5SGreg Kroah-Hartman tty_ldisc_deref(ld);
70796fd7ce5SGreg Kroah-Hartman }
70836697529SPeter Hurley
709a9a08845SLinus Torvalds wake_up_interruptible_poll(&tty->write_wait, EPOLLOUT);
710a9a08845SLinus Torvalds wake_up_interruptible_poll(&tty->read_wait, EPOLLIN);
71136697529SPeter Hurley
71296fd7ce5SGreg Kroah-Hartman /*
71396fd7ce5SGreg Kroah-Hartman * Shutdown the current line discipline, and reset it to
71496fd7ce5SGreg Kroah-Hartman * N_TTY if need be.
71596fd7ce5SGreg Kroah-Hartman *
71696fd7ce5SGreg Kroah-Hartman * Avoid racing set_ldisc or tty_ldisc_release
71796fd7ce5SGreg Kroah-Hartman */
718fae76e9aSPeter Hurley tty_ldisc_lock(tty, MAX_SCHEDULE_TIMEOUT);
71996fd7ce5SGreg Kroah-Hartman
720892d1fa7SPeter Hurley if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
72196fd7ce5SGreg Kroah-Hartman tty_reset_termios(tty);
722fc575ee6SPeter Hurley
723892d1fa7SPeter Hurley if (tty->ldisc) {
724892d1fa7SPeter Hurley if (reinit) {
725e65c62b1SJohannes Weiner if (tty_ldisc_reinit(tty, tty->termios.c_line) < 0 &&
726e65c62b1SJohannes Weiner tty_ldisc_reinit(tty, N_TTY) < 0)
727e65c62b1SJohannes Weiner WARN_ON(tty_ldisc_reinit(tty, N_NULL) < 0);
728892d1fa7SPeter Hurley } else
729892d1fa7SPeter Hurley tty_ldisc_kill(tty);
730892d1fa7SPeter Hurley }
731892d1fa7SPeter Hurley tty_ldisc_unlock(tty);
73296fd7ce5SGreg Kroah-Hartman }
73396fd7ce5SGreg Kroah-Hartman
73496fd7ce5SGreg Kroah-Hartman /**
73596fd7ce5SGreg Kroah-Hartman * tty_ldisc_setup - open line discipline
73696fd7ce5SGreg Kroah-Hartman * @tty: tty being shut down
73796fd7ce5SGreg Kroah-Hartman * @o_tty: pair tty for pty/tty pairs
73896fd7ce5SGreg Kroah-Hartman *
739cbb68f91SJiri Slaby * Called during the initial open of a tty/pty pair in order to set up the line
740cbb68f91SJiri Slaby * disciplines and bind them to the @tty. This has no locking issues as the
741cbb68f91SJiri Slaby * device isn't yet active.
74296fd7ce5SGreg Kroah-Hartman */
tty_ldisc_setup(struct tty_struct * tty,struct tty_struct * o_tty)74396fd7ce5SGreg Kroah-Hartman int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty)
74496fd7ce5SGreg Kroah-Hartman {
7459de2a7ceSPeter Hurley int retval = tty_ldisc_open(tty, tty->ldisc);
746d7238359SXiaofei Tan
74796fd7ce5SGreg Kroah-Hartman if (retval)
74896fd7ce5SGreg Kroah-Hartman return retval;
74996fd7ce5SGreg Kroah-Hartman
75096fd7ce5SGreg Kroah-Hartman if (o_tty) {
751110b8928SDmitry Safonov /*
752110b8928SDmitry Safonov * Called without o_tty->ldisc_sem held, as o_tty has been
753110b8928SDmitry Safonov * just allocated and no one has a reference to it.
754110b8928SDmitry Safonov */
75596fd7ce5SGreg Kroah-Hartman retval = tty_ldisc_open(o_tty, o_tty->ldisc);
75696fd7ce5SGreg Kroah-Hartman if (retval) {
7579de2a7ceSPeter Hurley tty_ldisc_close(tty, tty->ldisc);
75896fd7ce5SGreg Kroah-Hartman return retval;
75996fd7ce5SGreg Kroah-Hartman }
76096fd7ce5SGreg Kroah-Hartman }
76196fd7ce5SGreg Kroah-Hartman return 0;
76296fd7ce5SGreg Kroah-Hartman }
76389c8d91eSAlan Cox
76496fd7ce5SGreg Kroah-Hartman /**
76596fd7ce5SGreg Kroah-Hartman * tty_ldisc_release - release line discipline
76662462aefSPeter Hurley * @tty: tty being shut down (or one end of pty pair)
76796fd7ce5SGreg Kroah-Hartman *
768cbb68f91SJiri Slaby * Called during the final close of a tty or a pty pair in order to shut down
769cbb68f91SJiri Slaby * the line discpline layer. On exit, each tty's ldisc is %NULL.
77096fd7ce5SGreg Kroah-Hartman */
tty_ldisc_release(struct tty_struct * tty)77162462aefSPeter Hurley void tty_ldisc_release(struct tty_struct *tty)
77296fd7ce5SGreg Kroah-Hartman {
77362462aefSPeter Hurley struct tty_struct *o_tty = tty->link;
77462462aefSPeter Hurley
77596fd7ce5SGreg Kroah-Hartman /*
776a2965b7bSPeter Hurley * Shutdown this line discipline. As this is the final close,
777a2965b7bSPeter Hurley * it does not race with the set_ldisc code path.
77896fd7ce5SGreg Kroah-Hartman */
77996fd7ce5SGreg Kroah-Hartman
78036697529SPeter Hurley tty_ldisc_lock_pair(tty, o_tty);
78189c8d91eSAlan Cox tty_ldisc_kill(tty);
78289c8d91eSAlan Cox if (o_tty)
78389c8d91eSAlan Cox tty_ldisc_kill(o_tty);
78436697529SPeter Hurley tty_ldisc_unlock_pair(tty, o_tty);
78536697529SPeter Hurley
78672a8dcd7SXiaofei Tan /*
78772a8dcd7SXiaofei Tan * And the memory resources remaining (buffers, termios) will be
78872a8dcd7SXiaofei Tan * disposed of when the kref hits zero
78972a8dcd7SXiaofei Tan */
790fc575ee6SPeter Hurley
791fb6edc91SPeter Hurley tty_ldisc_debug(tty, "released\n");
79296fd7ce5SGreg Kroah-Hartman }
79396fd7ce5SGreg Kroah-Hartman
79496fd7ce5SGreg Kroah-Hartman /**
79596fd7ce5SGreg Kroah-Hartman * tty_ldisc_init - ldisc setup for new tty
79696fd7ce5SGreg Kroah-Hartman * @tty: tty being allocated
79796fd7ce5SGreg Kroah-Hartman *
798cbb68f91SJiri Slaby * Set up the line discipline objects for a newly allocated tty. Note that the
799cbb68f91SJiri Slaby * tty structure is not completely set up when this call is made.
80096fd7ce5SGreg Kroah-Hartman */
tty_ldisc_init(struct tty_struct * tty)801903f9db1STetsuo Handa int tty_ldisc_init(struct tty_struct *tty)
80296fd7ce5SGreg Kroah-Hartman {
80336697529SPeter Hurley struct tty_ldisc *ld = tty_ldisc_get(tty, N_TTY);
804d7238359SXiaofei Tan
80596fd7ce5SGreg Kroah-Hartman if (IS_ERR(ld))
806903f9db1STetsuo Handa return PTR_ERR(ld);
807f4807045SPeter Hurley tty->ldisc = ld;
808903f9db1STetsuo Handa return 0;
80996fd7ce5SGreg Kroah-Hartman }
81096fd7ce5SGreg Kroah-Hartman
8116716671dSJiri Slaby /**
812c8b710b3SPeter Hurley * tty_ldisc_deinit - ldisc cleanup for new tty
8136716671dSJiri Slaby * @tty: tty that was allocated recently
8146716671dSJiri Slaby *
815cbb68f91SJiri Slaby * The tty structure must not be completely set up (tty_ldisc_setup()) when
8166716671dSJiri Slaby * this call is made.
8176716671dSJiri Slaby */
tty_ldisc_deinit(struct tty_struct * tty)8186716671dSJiri Slaby void tty_ldisc_deinit(struct tty_struct *tty)
8196716671dSJiri Slaby {
820110b8928SDmitry Safonov /* no ldisc_sem, tty is being destroyed */
821c8b710b3SPeter Hurley if (tty->ldisc)
822ebc9baedSPeter Hurley tty_ldisc_put(tty->ldisc);
823f4807045SPeter Hurley tty->ldisc = NULL;
8246716671dSJiri Slaby }
825