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> 2296fd7ce5SGreg Kroah-Hartman 23fc575ee6SPeter Hurley #undef LDISC_DEBUG_HANGUP 24fc575ee6SPeter Hurley 25fc575ee6SPeter Hurley #ifdef LDISC_DEBUG_HANGUP 260a6adc13SPeter Hurley #define tty_ldisc_debug(tty, f, args...) tty_debug(tty, f, ##args) 27fc575ee6SPeter Hurley #else 28fc575ee6SPeter Hurley #define tty_ldisc_debug(tty, f, args...) 29fc575ee6SPeter Hurley #endif 30fc575ee6SPeter Hurley 31d2c43890SPeter Hurley /* lockdep nested classes for tty->ldisc_sem */ 32d2c43890SPeter Hurley enum { 33d2c43890SPeter Hurley LDISC_SEM_NORMAL, 34d2c43890SPeter Hurley LDISC_SEM_OTHER, 35d2c43890SPeter Hurley }; 36d2c43890SPeter Hurley 37d2c43890SPeter Hurley 3896fd7ce5SGreg Kroah-Hartman /* 3996fd7ce5SGreg Kroah-Hartman * This guards the refcounted line discipline lists. The lock 4096fd7ce5SGreg Kroah-Hartman * must be taken with irqs off because there are hangup path 4196fd7ce5SGreg Kroah-Hartman * callers who will do ldisc lookups and cannot sleep. 4296fd7ce5SGreg Kroah-Hartman */ 4396fd7ce5SGreg Kroah-Hartman 44137084bbSPeter Hurley static DEFINE_RAW_SPINLOCK(tty_ldiscs_lock); 4596fd7ce5SGreg Kroah-Hartman /* Line disc dispatch table */ 4696fd7ce5SGreg Kroah-Hartman static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS]; 4796fd7ce5SGreg Kroah-Hartman 4896fd7ce5SGreg Kroah-Hartman /** 4996fd7ce5SGreg Kroah-Hartman * tty_register_ldisc - install a line discipline 5096fd7ce5SGreg Kroah-Hartman * @disc: ldisc number 5196fd7ce5SGreg Kroah-Hartman * @new_ldisc: pointer to the ldisc object 5296fd7ce5SGreg Kroah-Hartman * 5396fd7ce5SGreg Kroah-Hartman * Installs a new line discipline into the kernel. The discipline 5496fd7ce5SGreg Kroah-Hartman * is set up as unreferenced and then made available to the kernel 5596fd7ce5SGreg Kroah-Hartman * from this point onwards. 5696fd7ce5SGreg Kroah-Hartman * 5796fd7ce5SGreg Kroah-Hartman * Locking: 58137084bbSPeter Hurley * takes tty_ldiscs_lock to guard against ldisc races 5996fd7ce5SGreg Kroah-Hartman */ 6096fd7ce5SGreg Kroah-Hartman 6196fd7ce5SGreg Kroah-Hartman int tty_register_ldisc(int disc, struct tty_ldisc_ops *new_ldisc) 6296fd7ce5SGreg Kroah-Hartman { 6396fd7ce5SGreg Kroah-Hartman unsigned long flags; 6496fd7ce5SGreg Kroah-Hartman int ret = 0; 6596fd7ce5SGreg Kroah-Hartman 6696fd7ce5SGreg Kroah-Hartman if (disc < N_TTY || disc >= NR_LDISCS) 6796fd7ce5SGreg Kroah-Hartman return -EINVAL; 6896fd7ce5SGreg Kroah-Hartman 69137084bbSPeter Hurley raw_spin_lock_irqsave(&tty_ldiscs_lock, flags); 7096fd7ce5SGreg Kroah-Hartman tty_ldiscs[disc] = new_ldisc; 7196fd7ce5SGreg Kroah-Hartman new_ldisc->num = disc; 7296fd7ce5SGreg Kroah-Hartman new_ldisc->refcount = 0; 73137084bbSPeter Hurley raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags); 7496fd7ce5SGreg Kroah-Hartman 7596fd7ce5SGreg Kroah-Hartman return ret; 7696fd7ce5SGreg Kroah-Hartman } 7796fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_register_ldisc); 7896fd7ce5SGreg Kroah-Hartman 7996fd7ce5SGreg Kroah-Hartman /** 8096fd7ce5SGreg Kroah-Hartman * tty_unregister_ldisc - unload a line discipline 8196fd7ce5SGreg Kroah-Hartman * @disc: ldisc number 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 1388eddcca2SLee Jones static int tty_ldisc_autoload = IS_BUILTIN(CONFIG_LDISC_AUTOLOAD); 13996fd7ce5SGreg Kroah-Hartman /** 14096fd7ce5SGreg Kroah-Hartman * tty_ldisc_get - take a reference to an ldisc 1418a3bdec1SLee Jones * @tty: tty device 14296fd7ce5SGreg Kroah-Hartman * @disc: ldisc number 14396fd7ce5SGreg Kroah-Hartman * 14496fd7ce5SGreg Kroah-Hartman * Takes a reference to a line discipline. Deals with refcounts and 145c0cc1c5dSPeter Hurley * module locking counts. 146c0cc1c5dSPeter Hurley * 147c0cc1c5dSPeter Hurley * Returns: -EINVAL if the discipline index is not [N_TTY..NR_LDISCS] or 148c0cc1c5dSPeter Hurley * if the discipline is not registered 149c0cc1c5dSPeter Hurley * -EAGAIN if request_module() failed to load or register the 150c0cc1c5dSPeter Hurley * the discipline 151c0cc1c5dSPeter Hurley * -ENOMEM if allocation failure 152c0cc1c5dSPeter Hurley * 153c0cc1c5dSPeter Hurley * Otherwise, returns a pointer to the discipline and bumps the 154c0cc1c5dSPeter Hurley * ref count 15596fd7ce5SGreg Kroah-Hartman * 15696fd7ce5SGreg Kroah-Hartman * Locking: 157137084bbSPeter Hurley * takes tty_ldiscs_lock to guard against ldisc races 15896fd7ce5SGreg Kroah-Hartman */ 15996fd7ce5SGreg Kroah-Hartman 16036697529SPeter Hurley static struct tty_ldisc *tty_ldisc_get(struct tty_struct *tty, int disc) 16196fd7ce5SGreg Kroah-Hartman { 16296fd7ce5SGreg Kroah-Hartman struct tty_ldisc *ld; 16396fd7ce5SGreg Kroah-Hartman struct tty_ldisc_ops *ldops; 16496fd7ce5SGreg Kroah-Hartman 16596fd7ce5SGreg Kroah-Hartman if (disc < N_TTY || disc >= NR_LDISCS) 16696fd7ce5SGreg Kroah-Hartman return ERR_PTR(-EINVAL); 16796fd7ce5SGreg Kroah-Hartman 16896fd7ce5SGreg Kroah-Hartman /* 16996fd7ce5SGreg Kroah-Hartman * Get the ldisc ops - we may need to request them to be loaded 17096fd7ce5SGreg Kroah-Hartman * dynamically and try again. 17196fd7ce5SGreg Kroah-Hartman */ 17296fd7ce5SGreg Kroah-Hartman ldops = get_ldops(disc); 17396fd7ce5SGreg Kroah-Hartman if (IS_ERR(ldops)) { 1747c0cca7cSGreg Kroah-Hartman if (!capable(CAP_SYS_MODULE) && !tty_ldisc_autoload) 1757c0cca7cSGreg Kroah-Hartman return ERR_PTR(-EPERM); 17696fd7ce5SGreg Kroah-Hartman request_module("tty-ldisc-%d", disc); 17796fd7ce5SGreg Kroah-Hartman ldops = get_ldops(disc); 17896fd7ce5SGreg Kroah-Hartman if (IS_ERR(ldops)) 17996fd7ce5SGreg Kroah-Hartman return ERR_CAST(ldops); 18096fd7ce5SGreg Kroah-Hartman } 18196fd7ce5SGreg Kroah-Hartman 182bcdd0ca8STetsuo Handa /* 183bcdd0ca8STetsuo Handa * There is no way to handle allocation failure of only 16 bytes. 184bcdd0ca8STetsuo Handa * Let's simplify error handling and save more memory. 185bcdd0ca8STetsuo Handa */ 186bcdd0ca8STetsuo Handa ld = kmalloc(sizeof(struct tty_ldisc), GFP_KERNEL | __GFP_NOFAIL); 18796fd7ce5SGreg Kroah-Hartman ld->ops = ldops; 18836697529SPeter Hurley ld->tty = tty; 1891541f845SIvo Sieben 19096fd7ce5SGreg Kroah-Hartman return ld; 19196fd7ce5SGreg Kroah-Hartman } 19296fd7ce5SGreg Kroah-Hartman 1938eddcca2SLee Jones /* 194734de249SPeter Hurley * tty_ldisc_put - release the ldisc 195734de249SPeter Hurley * 196734de249SPeter Hurley * Complement of tty_ldisc_get(). 197734de249SPeter Hurley */ 198cb128f69SDenys Vlasenko static void tty_ldisc_put(struct tty_ldisc *ld) 199734de249SPeter Hurley { 200734de249SPeter Hurley if (WARN_ON_ONCE(!ld)) 201734de249SPeter Hurley return; 202734de249SPeter Hurley 20336697529SPeter Hurley put_ldops(ld->ops); 204734de249SPeter Hurley kfree(ld); 205734de249SPeter Hurley } 206734de249SPeter Hurley 20796fd7ce5SGreg Kroah-Hartman static void *tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos) 20896fd7ce5SGreg Kroah-Hartman { 20996fd7ce5SGreg Kroah-Hartman return (*pos < NR_LDISCS) ? pos : NULL; 21096fd7ce5SGreg Kroah-Hartman } 21196fd7ce5SGreg Kroah-Hartman 21296fd7ce5SGreg Kroah-Hartman static void *tty_ldiscs_seq_next(struct seq_file *m, void *v, loff_t *pos) 21396fd7ce5SGreg Kroah-Hartman { 21496fd7ce5SGreg Kroah-Hartman (*pos)++; 21596fd7ce5SGreg Kroah-Hartman return (*pos < NR_LDISCS) ? pos : NULL; 21696fd7ce5SGreg Kroah-Hartman } 21796fd7ce5SGreg Kroah-Hartman 21896fd7ce5SGreg Kroah-Hartman static void tty_ldiscs_seq_stop(struct seq_file *m, void *v) 21996fd7ce5SGreg Kroah-Hartman { 22096fd7ce5SGreg Kroah-Hartman } 22196fd7ce5SGreg Kroah-Hartman 22296fd7ce5SGreg Kroah-Hartman static int tty_ldiscs_seq_show(struct seq_file *m, void *v) 22396fd7ce5SGreg Kroah-Hartman { 22496fd7ce5SGreg Kroah-Hartman int i = *(loff_t *)v; 22596fd7ce5SGreg Kroah-Hartman struct tty_ldisc_ops *ldops; 22696fd7ce5SGreg Kroah-Hartman 22796fd7ce5SGreg Kroah-Hartman ldops = get_ldops(i); 22896fd7ce5SGreg Kroah-Hartman if (IS_ERR(ldops)) 22996fd7ce5SGreg Kroah-Hartman return 0; 23096fd7ce5SGreg Kroah-Hartman seq_printf(m, "%-10s %2d\n", ldops->name ? ldops->name : "???", i); 23196fd7ce5SGreg Kroah-Hartman put_ldops(ldops); 23296fd7ce5SGreg Kroah-Hartman return 0; 23396fd7ce5SGreg Kroah-Hartman } 23496fd7ce5SGreg Kroah-Hartman 235fddda2b7SChristoph Hellwig const struct seq_operations tty_ldiscs_seq_ops = { 23696fd7ce5SGreg Kroah-Hartman .start = tty_ldiscs_seq_start, 23796fd7ce5SGreg Kroah-Hartman .next = tty_ldiscs_seq_next, 23896fd7ce5SGreg Kroah-Hartman .stop = tty_ldiscs_seq_stop, 23996fd7ce5SGreg Kroah-Hartman .show = tty_ldiscs_seq_show, 24096fd7ce5SGreg Kroah-Hartman }; 24196fd7ce5SGreg Kroah-Hartman 24296fd7ce5SGreg Kroah-Hartman /** 24396fd7ce5SGreg Kroah-Hartman * tty_ldisc_ref_wait - wait for the tty ldisc 24496fd7ce5SGreg Kroah-Hartman * @tty: tty device 24596fd7ce5SGreg Kroah-Hartman * 24696fd7ce5SGreg Kroah-Hartman * Dereference the line discipline for the terminal and take a 24796fd7ce5SGreg Kroah-Hartman * reference to it. If the line discipline is in flux then 24896fd7ce5SGreg Kroah-Hartman * wait patiently until it changes. 24996fd7ce5SGreg Kroah-Hartman * 250892d1fa7SPeter Hurley * Returns: NULL if the tty has been hungup and not re-opened with 251892d1fa7SPeter Hurley * a new file descriptor, otherwise valid ldisc reference 252892d1fa7SPeter Hurley * 2538eddcca2SLee Jones * Note 1: Must not be called from an IRQ/timer context. The caller 25496fd7ce5SGreg Kroah-Hartman * must also be careful not to hold other locks that will deadlock 25596fd7ce5SGreg Kroah-Hartman * against a discipline change, such as an existing ldisc reference 25696fd7ce5SGreg Kroah-Hartman * (which we check for) 25796fd7ce5SGreg Kroah-Hartman * 2588eddcca2SLee Jones * Note 2: a file_operations routine (read/poll/write) should use this 259e55afd11SPeter Hurley * function to wait for any ldisc lifetime events to finish. 26096fd7ce5SGreg Kroah-Hartman */ 26196fd7ce5SGreg Kroah-Hartman 26296fd7ce5SGreg Kroah-Hartman struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty) 26396fd7ce5SGreg Kroah-Hartman { 264a4a3e061SDmitry Vyukov struct tty_ldisc *ld; 265a4a3e061SDmitry Vyukov 26636697529SPeter Hurley ldsem_down_read(&tty->ldisc_sem, MAX_SCHEDULE_TIMEOUT); 267a4a3e061SDmitry Vyukov ld = tty->ldisc; 268a4a3e061SDmitry Vyukov if (!ld) 269a570a49aSPeter Hurley ldsem_up_read(&tty->ldisc_sem); 270a4a3e061SDmitry Vyukov return ld; 27196fd7ce5SGreg Kroah-Hartman } 27296fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait); 27396fd7ce5SGreg Kroah-Hartman 27496fd7ce5SGreg Kroah-Hartman /** 27596fd7ce5SGreg Kroah-Hartman * tty_ldisc_ref - get the tty ldisc 27696fd7ce5SGreg Kroah-Hartman * @tty: tty device 27796fd7ce5SGreg Kroah-Hartman * 27896fd7ce5SGreg Kroah-Hartman * Dereference the line discipline for the terminal and take a 27996fd7ce5SGreg Kroah-Hartman * reference to it. If the line discipline is in flux then 28096fd7ce5SGreg Kroah-Hartman * return NULL. Can be called from IRQ and timer functions. 28196fd7ce5SGreg Kroah-Hartman */ 28296fd7ce5SGreg Kroah-Hartman 28396fd7ce5SGreg Kroah-Hartman struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty) 28496fd7ce5SGreg Kroah-Hartman { 28536697529SPeter Hurley struct tty_ldisc *ld = NULL; 28636697529SPeter Hurley 28736697529SPeter Hurley if (ldsem_down_read_trylock(&tty->ldisc_sem)) { 28836697529SPeter Hurley ld = tty->ldisc; 28936697529SPeter Hurley if (!ld) 29036697529SPeter Hurley ldsem_up_read(&tty->ldisc_sem); 29136697529SPeter Hurley } 29236697529SPeter Hurley return ld; 29396fd7ce5SGreg Kroah-Hartman } 29496fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(tty_ldisc_ref); 29596fd7ce5SGreg Kroah-Hartman 29696fd7ce5SGreg Kroah-Hartman /** 29796fd7ce5SGreg Kroah-Hartman * tty_ldisc_deref - free a tty ldisc reference 29896fd7ce5SGreg Kroah-Hartman * @ld: reference to free up 29996fd7ce5SGreg Kroah-Hartman * 30096fd7ce5SGreg Kroah-Hartman * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May 30196fd7ce5SGreg Kroah-Hartman * be called in IRQ context. 30296fd7ce5SGreg Kroah-Hartman */ 30396fd7ce5SGreg Kroah-Hartman 30496fd7ce5SGreg Kroah-Hartman void tty_ldisc_deref(struct tty_ldisc *ld) 30596fd7ce5SGreg Kroah-Hartman { 30636697529SPeter Hurley ldsem_up_read(&ld->tty->ldisc_sem); 30796fd7ce5SGreg Kroah-Hartman } 30896fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(tty_ldisc_deref); 30996fd7ce5SGreg Kroah-Hartman 310d2c43890SPeter Hurley 311c2bb524bSPeter Hurley static inline int 312e80a10eeSPeter Hurley __tty_ldisc_lock(struct tty_struct *tty, unsigned long timeout) 313d2c43890SPeter Hurley { 314d2c43890SPeter Hurley return ldsem_down_write(&tty->ldisc_sem, timeout); 315d2c43890SPeter Hurley } 316d2c43890SPeter Hurley 317c2bb524bSPeter Hurley static inline int 318e80a10eeSPeter Hurley __tty_ldisc_lock_nested(struct tty_struct *tty, unsigned long timeout) 319d2c43890SPeter Hurley { 320d2c43890SPeter Hurley return ldsem_down_write_nested(&tty->ldisc_sem, 321d2c43890SPeter Hurley LDISC_SEM_OTHER, timeout); 322d2c43890SPeter Hurley } 323d2c43890SPeter Hurley 324e80a10eeSPeter Hurley static inline void __tty_ldisc_unlock(struct tty_struct *tty) 325d2c43890SPeter Hurley { 32652772ea6SGuillaume Gomez ldsem_up_write(&tty->ldisc_sem); 327d2c43890SPeter Hurley } 328d2c43890SPeter Hurley 329b027e229SGaurav Kohli int tty_ldisc_lock(struct tty_struct *tty, unsigned long timeout) 330fae76e9aSPeter Hurley { 331fae76e9aSPeter Hurley int ret; 332fae76e9aSPeter Hurley 333c96cf923SDmitry Safonov /* Kindly asking blocked readers to release the read side */ 334c96cf923SDmitry Safonov set_bit(TTY_LDISC_CHANGING, &tty->flags); 335c96cf923SDmitry Safonov wake_up_interruptible_all(&tty->read_wait); 336c96cf923SDmitry Safonov wake_up_interruptible_all(&tty->write_wait); 337c96cf923SDmitry Safonov 338fae76e9aSPeter Hurley ret = __tty_ldisc_lock(tty, timeout); 339fae76e9aSPeter Hurley if (!ret) 340fae76e9aSPeter Hurley return -EBUSY; 341fae76e9aSPeter Hurley set_bit(TTY_LDISC_HALTED, &tty->flags); 342fae76e9aSPeter Hurley return 0; 343fae76e9aSPeter Hurley } 344fae76e9aSPeter Hurley 345b027e229SGaurav Kohli void tty_ldisc_unlock(struct tty_struct *tty) 346fae76e9aSPeter Hurley { 347fae76e9aSPeter Hurley clear_bit(TTY_LDISC_HALTED, &tty->flags); 348c96cf923SDmitry Safonov /* Can be cleared here - ldisc_unlock will wake up writers firstly */ 349c96cf923SDmitry Safonov clear_bit(TTY_LDISC_CHANGING, &tty->flags); 350fae76e9aSPeter Hurley __tty_ldisc_unlock(tty); 351fae76e9aSPeter Hurley } 352fae76e9aSPeter Hurley 353c2bb524bSPeter Hurley static int 354d2c43890SPeter Hurley tty_ldisc_lock_pair_timeout(struct tty_struct *tty, struct tty_struct *tty2, 355d2c43890SPeter Hurley unsigned long timeout) 356d2c43890SPeter Hurley { 357d2c43890SPeter Hurley int ret; 358d2c43890SPeter Hurley 359d2c43890SPeter Hurley if (tty < tty2) { 360e80a10eeSPeter Hurley ret = __tty_ldisc_lock(tty, timeout); 361d2c43890SPeter Hurley if (ret) { 362e80a10eeSPeter Hurley ret = __tty_ldisc_lock_nested(tty2, timeout); 363d2c43890SPeter Hurley if (!ret) 364e80a10eeSPeter Hurley __tty_ldisc_unlock(tty); 365d2c43890SPeter Hurley } 366d2c43890SPeter Hurley } else { 367d2c43890SPeter Hurley /* if this is possible, it has lots of implications */ 368d2c43890SPeter Hurley WARN_ON_ONCE(tty == tty2); 369d2c43890SPeter Hurley if (tty2 && tty != tty2) { 370e80a10eeSPeter Hurley ret = __tty_ldisc_lock(tty2, timeout); 371d2c43890SPeter Hurley if (ret) { 372e80a10eeSPeter Hurley ret = __tty_ldisc_lock_nested(tty, timeout); 373d2c43890SPeter Hurley if (!ret) 374e80a10eeSPeter Hurley __tty_ldisc_unlock(tty2); 375d2c43890SPeter Hurley } 376d2c43890SPeter Hurley } else 377e80a10eeSPeter Hurley ret = __tty_ldisc_lock(tty, timeout); 378d2c43890SPeter Hurley } 379d2c43890SPeter Hurley 380d2c43890SPeter Hurley if (!ret) 381d2c43890SPeter Hurley return -EBUSY; 382d2c43890SPeter Hurley 383d2c43890SPeter Hurley set_bit(TTY_LDISC_HALTED, &tty->flags); 384d2c43890SPeter Hurley if (tty2) 385d2c43890SPeter Hurley set_bit(TTY_LDISC_HALTED, &tty2->flags); 386d2c43890SPeter Hurley return 0; 387d2c43890SPeter Hurley } 388d2c43890SPeter Hurley 389c2bb524bSPeter Hurley static void tty_ldisc_lock_pair(struct tty_struct *tty, struct tty_struct *tty2) 390d2c43890SPeter Hurley { 391d2c43890SPeter Hurley tty_ldisc_lock_pair_timeout(tty, tty2, MAX_SCHEDULE_TIMEOUT); 392d2c43890SPeter Hurley } 393d2c43890SPeter Hurley 394c2bb524bSPeter Hurley static void tty_ldisc_unlock_pair(struct tty_struct *tty, 395d2c43890SPeter Hurley struct tty_struct *tty2) 396d2c43890SPeter Hurley { 397e80a10eeSPeter Hurley __tty_ldisc_unlock(tty); 398d2c43890SPeter Hurley if (tty2) 399e80a10eeSPeter Hurley __tty_ldisc_unlock(tty2); 400d2c43890SPeter Hurley } 401d2c43890SPeter Hurley 40296fd7ce5SGreg Kroah-Hartman /** 40396fd7ce5SGreg Kroah-Hartman * tty_ldisc_flush - flush line discipline queue 40496fd7ce5SGreg Kroah-Hartman * @tty: tty 40596fd7ce5SGreg Kroah-Hartman * 40686c80a8eSPeter Hurley * Flush the line discipline queue (if any) and the tty flip buffers 40786c80a8eSPeter Hurley * for this tty. 40896fd7ce5SGreg Kroah-Hartman */ 40996fd7ce5SGreg Kroah-Hartman 41096fd7ce5SGreg Kroah-Hartman void tty_ldisc_flush(struct tty_struct *tty) 41196fd7ce5SGreg Kroah-Hartman { 41296fd7ce5SGreg Kroah-Hartman struct tty_ldisc *ld = tty_ldisc_ref(tty); 41386c80a8eSPeter Hurley 41486c80a8eSPeter Hurley tty_buffer_flush(tty, ld); 41586c80a8eSPeter Hurley if (ld) 41696fd7ce5SGreg Kroah-Hartman tty_ldisc_deref(ld); 41796fd7ce5SGreg Kroah-Hartman } 41896fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(tty_ldisc_flush); 41996fd7ce5SGreg Kroah-Hartman 42096fd7ce5SGreg Kroah-Hartman /** 42196fd7ce5SGreg Kroah-Hartman * tty_set_termios_ldisc - set ldisc field 42296fd7ce5SGreg Kroah-Hartman * @tty: tty structure 423c12da96fSPeter Hurley * @disc: line discipline number 42496fd7ce5SGreg Kroah-Hartman * 42596fd7ce5SGreg Kroah-Hartman * This is probably overkill for real world processors but 42696fd7ce5SGreg Kroah-Hartman * they are not on hot paths so a little discipline won't do 42796fd7ce5SGreg Kroah-Hartman * any harm. 42896fd7ce5SGreg Kroah-Hartman * 429dd42bf11SPeter Hurley * The line discipline-related tty_struct fields are reset to 430dd42bf11SPeter Hurley * prevent the ldisc driver from re-using stale information for 431dd42bf11SPeter Hurley * the new ldisc instance. 432dd42bf11SPeter Hurley * 4336a1c0680SPeter Hurley * Locking: takes termios_rwsem 43496fd7ce5SGreg Kroah-Hartman */ 43596fd7ce5SGreg Kroah-Hartman 436c12da96fSPeter Hurley static void tty_set_termios_ldisc(struct tty_struct *tty, int disc) 43796fd7ce5SGreg Kroah-Hartman { 4386a1c0680SPeter Hurley down_write(&tty->termios_rwsem); 439c12da96fSPeter Hurley tty->termios.c_line = disc; 4406a1c0680SPeter Hurley up_write(&tty->termios_rwsem); 441dd42bf11SPeter Hurley 442dd42bf11SPeter Hurley tty->disc_data = NULL; 443dd42bf11SPeter Hurley tty->receive_room = 0; 44496fd7ce5SGreg Kroah-Hartman } 44596fd7ce5SGreg Kroah-Hartman 44696fd7ce5SGreg Kroah-Hartman /** 44796fd7ce5SGreg Kroah-Hartman * tty_ldisc_open - open a line discipline 44896fd7ce5SGreg Kroah-Hartman * @tty: tty we are opening the ldisc on 44996fd7ce5SGreg Kroah-Hartman * @ld: discipline to open 45096fd7ce5SGreg Kroah-Hartman * 45196fd7ce5SGreg Kroah-Hartman * A helper opening method. Also a convenient debugging and check 45296fd7ce5SGreg Kroah-Hartman * point. 45396fd7ce5SGreg Kroah-Hartman * 45496fd7ce5SGreg Kroah-Hartman * Locking: always called with BTM already held. 45596fd7ce5SGreg Kroah-Hartman */ 45696fd7ce5SGreg Kroah-Hartman 45796fd7ce5SGreg Kroah-Hartman static int tty_ldisc_open(struct tty_struct *tty, struct tty_ldisc *ld) 45896fd7ce5SGreg Kroah-Hartman { 45996fd7ce5SGreg Kroah-Hartman WARN_ON(test_and_set_bit(TTY_LDISC_OPEN, &tty->flags)); 46096fd7ce5SGreg Kroah-Hartman if (ld->ops->open) { 46196fd7ce5SGreg Kroah-Hartman int ret; 46296fd7ce5SGreg Kroah-Hartman /* BTM here locks versus a hangup event */ 46396fd7ce5SGreg Kroah-Hartman ret = ld->ops->open(tty); 4647f90cfc5SJiri Slaby if (ret) 4657f90cfc5SJiri Slaby clear_bit(TTY_LDISC_OPEN, &tty->flags); 466fb6edc91SPeter Hurley 467a570a49aSPeter Hurley tty_ldisc_debug(tty, "%p: opened\n", ld); 46896fd7ce5SGreg Kroah-Hartman return ret; 46996fd7ce5SGreg Kroah-Hartman } 47096fd7ce5SGreg Kroah-Hartman return 0; 47196fd7ce5SGreg Kroah-Hartman } 47296fd7ce5SGreg Kroah-Hartman 47396fd7ce5SGreg Kroah-Hartman /** 47496fd7ce5SGreg Kroah-Hartman * tty_ldisc_close - close a line discipline 47596fd7ce5SGreg Kroah-Hartman * @tty: tty we are opening the ldisc on 47696fd7ce5SGreg Kroah-Hartman * @ld: discipline to close 47796fd7ce5SGreg Kroah-Hartman * 47896fd7ce5SGreg Kroah-Hartman * A helper close method. Also a convenient debugging and check 47996fd7ce5SGreg Kroah-Hartman * point. 48096fd7ce5SGreg Kroah-Hartman */ 48196fd7ce5SGreg Kroah-Hartman 48296fd7ce5SGreg Kroah-Hartman static void tty_ldisc_close(struct tty_struct *tty, struct tty_ldisc *ld) 48396fd7ce5SGreg Kroah-Hartman { 4849ffbe8acSNikolay Borisov lockdep_assert_held_write(&tty->ldisc_sem); 48596fd7ce5SGreg Kroah-Hartman WARN_ON(!test_bit(TTY_LDISC_OPEN, &tty->flags)); 48696fd7ce5SGreg Kroah-Hartman clear_bit(TTY_LDISC_OPEN, &tty->flags); 48796fd7ce5SGreg Kroah-Hartman if (ld->ops->close) 48896fd7ce5SGreg Kroah-Hartman ld->ops->close(tty); 489a570a49aSPeter Hurley tty_ldisc_debug(tty, "%p: closed\n", ld); 49096fd7ce5SGreg Kroah-Hartman } 49196fd7ce5SGreg Kroah-Hartman 49296fd7ce5SGreg Kroah-Hartman /** 4938a8dabf2SAlan Cox * tty_ldisc_failto - helper for ldisc failback 4948a8dabf2SAlan Cox * @tty: tty to open the ldisc on 4958a8dabf2SAlan Cox * @ld: ldisc we are trying to fail back to 4968a8dabf2SAlan Cox * 4978a8dabf2SAlan Cox * Helper to try and recover a tty when switching back to the old 4988a8dabf2SAlan Cox * ldisc fails and we need something attached. 4998a8dabf2SAlan Cox */ 5008a8dabf2SAlan Cox 5018a8dabf2SAlan Cox static int tty_ldisc_failto(struct tty_struct *tty, int ld) 5028a8dabf2SAlan Cox { 5038a8dabf2SAlan Cox struct tty_ldisc *disc = tty_ldisc_get(tty, ld); 5048a8dabf2SAlan Cox int r; 5058a8dabf2SAlan Cox 5069ffbe8acSNikolay Borisov lockdep_assert_held_write(&tty->ldisc_sem); 5078a8dabf2SAlan Cox if (IS_ERR(disc)) 5088a8dabf2SAlan Cox return PTR_ERR(disc); 5098a8dabf2SAlan Cox tty->ldisc = disc; 5108a8dabf2SAlan Cox tty_set_termios_ldisc(tty, ld); 5118a8dabf2SAlan Cox if ((r = tty_ldisc_open(tty, disc)) < 0) 5128a8dabf2SAlan Cox tty_ldisc_put(disc); 5138a8dabf2SAlan Cox return r; 5148a8dabf2SAlan Cox } 5158a8dabf2SAlan Cox 5168a8dabf2SAlan Cox /** 517a8983d01SGreg Kroah-Hartman * tty_ldisc_restore - helper for tty ldisc change 518a8983d01SGreg Kroah-Hartman * @tty: tty to recover 519a8983d01SGreg Kroah-Hartman * @old: previous ldisc 520a8983d01SGreg Kroah-Hartman * 521a8983d01SGreg Kroah-Hartman * Restore the previous line discipline or N_TTY when a line discipline 522a8983d01SGreg Kroah-Hartman * change fails due to an open error 523a8983d01SGreg Kroah-Hartman */ 524a8983d01SGreg Kroah-Hartman 525a8983d01SGreg Kroah-Hartman static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old) 526a8983d01SGreg Kroah-Hartman { 527a8983d01SGreg Kroah-Hartman /* There is an outstanding reference here so this is safe */ 528598c2d41STetsuo Handa if (tty_ldisc_failto(tty, old->ops->num) < 0) { 529598c2d41STetsuo Handa const char *name = tty_name(tty); 530598c2d41STetsuo Handa 531598c2d41STetsuo Handa pr_warn("Falling back ldisc for %s.\n", name); 532*72a8dcd7SXiaofei Tan /* 533*72a8dcd7SXiaofei Tan * The traditional behaviour is to fall back to N_TTY, we 534*72a8dcd7SXiaofei Tan * want to avoid falling back to N_NULL unless we have no 535*72a8dcd7SXiaofei Tan * choice to avoid the risk of breaking anything 536*72a8dcd7SXiaofei Tan */ 5378a8dabf2SAlan Cox if (tty_ldisc_failto(tty, N_TTY) < 0 && 5388a8dabf2SAlan Cox tty_ldisc_failto(tty, N_NULL) < 0) 539598c2d41STetsuo Handa panic("Couldn't open N_NULL ldisc for %s.", name); 540a8983d01SGreg Kroah-Hartman } 541a8983d01SGreg Kroah-Hartman } 542a8983d01SGreg Kroah-Hartman 543a8983d01SGreg Kroah-Hartman /** 54496fd7ce5SGreg Kroah-Hartman * tty_set_ldisc - set line discipline 54596fd7ce5SGreg Kroah-Hartman * @tty: the terminal to set 546fa441954SJiri Slaby * @disc: the line discipline number 54796fd7ce5SGreg Kroah-Hartman * 54896fd7ce5SGreg Kroah-Hartman * Set the discipline of a tty line. Must be called from a process 54996fd7ce5SGreg Kroah-Hartman * context. The ldisc change logic has to protect itself against any 55096fd7ce5SGreg Kroah-Hartman * overlapping ldisc change (including on the other end of pty pairs), 55196fd7ce5SGreg Kroah-Hartman * the close of one side of a tty/pty pair, and eventually hangup. 55296fd7ce5SGreg Kroah-Hartman */ 55396fd7ce5SGreg Kroah-Hartman 554c12da96fSPeter Hurley int tty_set_ldisc(struct tty_struct *tty, int disc) 55596fd7ce5SGreg Kroah-Hartman { 556a8983d01SGreg Kroah-Hartman int retval; 557a8983d01SGreg Kroah-Hartman struct tty_ldisc *old_ldisc, *new_ldisc; 558a8983d01SGreg Kroah-Hartman 559a8983d01SGreg Kroah-Hartman new_ldisc = tty_ldisc_get(tty, disc); 560a8983d01SGreg Kroah-Hartman if (IS_ERR(new_ldisc)) 561a8983d01SGreg Kroah-Hartman return PTR_ERR(new_ldisc); 56296fd7ce5SGreg Kroah-Hartman 563c8483bc9SPeter Hurley tty_lock(tty); 564276a661aSPeter Hurley retval = tty_ldisc_lock(tty, 5 * HZ); 56563d8cb3fSPeter Hurley if (retval) 56663d8cb3fSPeter Hurley goto err; 56796fd7ce5SGreg Kroah-Hartman 568a570a49aSPeter Hurley if (!tty->ldisc) { 569a570a49aSPeter Hurley retval = -EIO; 570a570a49aSPeter Hurley goto out; 571a570a49aSPeter Hurley } 572a570a49aSPeter Hurley 57363d8cb3fSPeter Hurley /* Check the no-op case */ 574a8983d01SGreg Kroah-Hartman if (tty->ldisc->ops->num == disc) 57563d8cb3fSPeter Hurley goto out; 57696fd7ce5SGreg Kroah-Hartman 57763d8cb3fSPeter Hurley if (test_bit(TTY_HUPPED, &tty->flags)) { 57863d8cb3fSPeter Hurley /* We were raced by hangup */ 57963d8cb3fSPeter Hurley retval = -EIO; 58063d8cb3fSPeter Hurley goto out; 58196fd7ce5SGreg Kroah-Hartman } 58296fd7ce5SGreg Kroah-Hartman 583a8983d01SGreg Kroah-Hartman old_ldisc = tty->ldisc; 584a8983d01SGreg Kroah-Hartman 585a8983d01SGreg Kroah-Hartman /* Shutdown the old discipline. */ 586a8983d01SGreg Kroah-Hartman tty_ldisc_close(tty, old_ldisc); 587a8983d01SGreg Kroah-Hartman 588a8983d01SGreg Kroah-Hartman /* Now set up the new line discipline. */ 589a8983d01SGreg Kroah-Hartman tty->ldisc = new_ldisc; 590a8983d01SGreg Kroah-Hartman tty_set_termios_ldisc(tty, disc); 591a8983d01SGreg Kroah-Hartman 592a8983d01SGreg Kroah-Hartman retval = tty_ldisc_open(tty, new_ldisc); 59396fd7ce5SGreg Kroah-Hartman if (retval < 0) { 59496fd7ce5SGreg Kroah-Hartman /* Back to the old one or N_TTY if we can't */ 595a8983d01SGreg Kroah-Hartman tty_ldisc_put(new_ldisc); 596a8983d01SGreg Kroah-Hartman tty_ldisc_restore(tty, old_ldisc); 59796fd7ce5SGreg Kroah-Hartman } 59896fd7ce5SGreg Kroah-Hartman 599a8983d01SGreg Kroah-Hartman if (tty->ldisc->ops->num != old_ldisc->ops->num && tty->ops->set_ldisc) { 6009191aaaaSPeter Hurley down_read(&tty->termios_rwsem); 60196fd7ce5SGreg Kroah-Hartman tty->ops->set_ldisc(tty); 6029191aaaaSPeter Hurley up_read(&tty->termios_rwsem); 6039191aaaaSPeter Hurley } 60496fd7ce5SGreg Kroah-Hartman 605*72a8dcd7SXiaofei Tan /* 606*72a8dcd7SXiaofei Tan * At this point we hold a reference to the new ldisc and a 607*72a8dcd7SXiaofei Tan * reference to the old ldisc, or we hold two references to 608*72a8dcd7SXiaofei Tan * the old ldisc (if it was restored as part of error cleanup 609*72a8dcd7SXiaofei Tan * above). In either case, releasing a single reference from 610*72a8dcd7SXiaofei Tan * the old ldisc is correct. 611*72a8dcd7SXiaofei Tan */ 612a8983d01SGreg Kroah-Hartman new_ldisc = old_ldisc; 61363d8cb3fSPeter Hurley out: 614276a661aSPeter Hurley tty_ldisc_unlock(tty); 61596fd7ce5SGreg Kroah-Hartman 616*72a8dcd7SXiaofei Tan /* 617*72a8dcd7SXiaofei Tan * Restart the work queue in case no characters kick it off. Safe if 618*72a8dcd7SXiaofei Tan * already running 619*72a8dcd7SXiaofei Tan */ 62017a69219SPeter Hurley tty_buffer_restart_work(tty->port); 62163d8cb3fSPeter Hurley err: 622a8983d01SGreg Kroah-Hartman tty_ldisc_put(new_ldisc); /* drop the extra reference */ 62389c8d91eSAlan Cox tty_unlock(tty); 62496fd7ce5SGreg Kroah-Hartman return retval; 62596fd7ce5SGreg Kroah-Hartman } 6261ab92da3SOkash Khawaja EXPORT_SYMBOL_GPL(tty_set_ldisc); 62796fd7ce5SGreg Kroah-Hartman 62896fd7ce5SGreg Kroah-Hartman /** 6296ffeb4b2SPeter Hurley * tty_ldisc_kill - teardown ldisc 6306ffeb4b2SPeter Hurley * @tty: tty being released 6316ffeb4b2SPeter Hurley * 6326ffeb4b2SPeter Hurley * Perform final close of the ldisc and reset tty->ldisc 6336ffeb4b2SPeter Hurley */ 6346ffeb4b2SPeter Hurley static void tty_ldisc_kill(struct tty_struct *tty) 6356ffeb4b2SPeter Hurley { 6369ffbe8acSNikolay Borisov lockdep_assert_held_write(&tty->ldisc_sem); 6376ffeb4b2SPeter Hurley if (!tty->ldisc) 6386ffeb4b2SPeter Hurley return; 6396ffeb4b2SPeter Hurley /* 6406ffeb4b2SPeter Hurley * Now kill off the ldisc 6416ffeb4b2SPeter Hurley */ 6426ffeb4b2SPeter Hurley tty_ldisc_close(tty, tty->ldisc); 6436ffeb4b2SPeter Hurley tty_ldisc_put(tty->ldisc); 6446ffeb4b2SPeter Hurley /* Force an oops if we mess this up */ 6456ffeb4b2SPeter Hurley tty->ldisc = NULL; 6466ffeb4b2SPeter Hurley } 6476ffeb4b2SPeter Hurley 6486ffeb4b2SPeter Hurley /** 64996fd7ce5SGreg Kroah-Hartman * tty_reset_termios - reset terminal state 65096fd7ce5SGreg Kroah-Hartman * @tty: tty to reset 65196fd7ce5SGreg Kroah-Hartman * 65296fd7ce5SGreg Kroah-Hartman * Restore a terminal to the driver default state. 65396fd7ce5SGreg Kroah-Hartman */ 65496fd7ce5SGreg Kroah-Hartman 65596fd7ce5SGreg Kroah-Hartman static void tty_reset_termios(struct tty_struct *tty) 65696fd7ce5SGreg Kroah-Hartman { 6576a1c0680SPeter Hurley down_write(&tty->termios_rwsem); 658adc8d746SAlan Cox tty->termios = tty->driver->init_termios; 659adc8d746SAlan Cox tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios); 660adc8d746SAlan Cox tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios); 6616a1c0680SPeter Hurley up_write(&tty->termios_rwsem); 66296fd7ce5SGreg Kroah-Hartman } 66396fd7ce5SGreg Kroah-Hartman 66496fd7ce5SGreg Kroah-Hartman 66596fd7ce5SGreg Kroah-Hartman /** 66696fd7ce5SGreg Kroah-Hartman * tty_ldisc_reinit - reinitialise the tty ldisc 66796fd7ce5SGreg Kroah-Hartman * @tty: tty to reinit 668c12da96fSPeter Hurley * @disc: line discipline to reinitialize 66996fd7ce5SGreg Kroah-Hartman * 6707896f30dSPeter Hurley * Completely reinitialize the line discipline state, by closing the 671892d1fa7SPeter Hurley * current instance, if there is one, and opening a new instance. If 672892d1fa7SPeter Hurley * an error occurs opening the new non-N_TTY instance, the instance 673892d1fa7SPeter Hurley * is dropped and tty->ldisc reset to NULL. The caller can then retry 674892d1fa7SPeter Hurley * with N_TTY instead. 6757896f30dSPeter Hurley * 6767896f30dSPeter Hurley * Returns 0 if successful, otherwise error code < 0 67796fd7ce5SGreg Kroah-Hartman */ 67896fd7ce5SGreg Kroah-Hartman 679892d1fa7SPeter Hurley int tty_ldisc_reinit(struct tty_struct *tty, int disc) 68096fd7ce5SGreg Kroah-Hartman { 6817896f30dSPeter Hurley struct tty_ldisc *ld; 6827896f30dSPeter Hurley int retval; 6831c95ba1eSPhilippe Rétornaz 6849ffbe8acSNikolay Borisov lockdep_assert_held_write(&tty->ldisc_sem); 6857896f30dSPeter Hurley ld = tty_ldisc_get(tty, disc); 686a8983d01SGreg Kroah-Hartman if (IS_ERR(ld)) { 687a8983d01SGreg Kroah-Hartman BUG_ON(disc == N_TTY); 6887896f30dSPeter Hurley return PTR_ERR(ld); 689a8983d01SGreg Kroah-Hartman } 69096fd7ce5SGreg Kroah-Hartman 6917896f30dSPeter Hurley if (tty->ldisc) { 69296fd7ce5SGreg Kroah-Hartman tty_ldisc_close(tty, tty->ldisc); 69396fd7ce5SGreg Kroah-Hartman tty_ldisc_put(tty->ldisc); 6947896f30dSPeter Hurley } 6957896f30dSPeter Hurley 6967896f30dSPeter Hurley /* switch the line discipline */ 697f4807045SPeter Hurley tty->ldisc = ld; 698c12da96fSPeter Hurley tty_set_termios_ldisc(tty, disc); 6997896f30dSPeter Hurley retval = tty_ldisc_open(tty, tty->ldisc); 7007896f30dSPeter Hurley if (retval) { 7017896f30dSPeter Hurley tty_ldisc_put(tty->ldisc); 7027896f30dSPeter Hurley tty->ldisc = NULL; 7037896f30dSPeter Hurley } 7047896f30dSPeter Hurley return retval; 70596fd7ce5SGreg Kroah-Hartman } 70696fd7ce5SGreg Kroah-Hartman 70796fd7ce5SGreg Kroah-Hartman /** 70896fd7ce5SGreg Kroah-Hartman * tty_ldisc_hangup - hangup ldisc reset 70996fd7ce5SGreg Kroah-Hartman * @tty: tty being hung up 7108eddcca2SLee Jones * @reinit: whether to re-initialise the tty 71196fd7ce5SGreg Kroah-Hartman * 71296fd7ce5SGreg Kroah-Hartman * Some tty devices reset their termios when they receive a hangup 71396fd7ce5SGreg Kroah-Hartman * event. In that situation we must also switch back to N_TTY properly 71496fd7ce5SGreg Kroah-Hartman * before we reset the termios data. 71596fd7ce5SGreg Kroah-Hartman * 71696fd7ce5SGreg Kroah-Hartman * Locking: We can take the ldisc mutex as the rest of the code is 71796fd7ce5SGreg Kroah-Hartman * careful to allow for this. 71896fd7ce5SGreg Kroah-Hartman * 71996fd7ce5SGreg Kroah-Hartman * In the pty pair case this occurs in the close() path of the 72096fd7ce5SGreg Kroah-Hartman * tty itself so we must be careful about locking rules. 72196fd7ce5SGreg Kroah-Hartman */ 72296fd7ce5SGreg Kroah-Hartman 723892d1fa7SPeter Hurley void tty_ldisc_hangup(struct tty_struct *tty, bool reinit) 72496fd7ce5SGreg Kroah-Hartman { 72596fd7ce5SGreg Kroah-Hartman struct tty_ldisc *ld; 72696fd7ce5SGreg Kroah-Hartman 727a570a49aSPeter Hurley tty_ldisc_debug(tty, "%p: hangup\n", tty->ldisc); 728fc575ee6SPeter Hurley 72996fd7ce5SGreg Kroah-Hartman ld = tty_ldisc_ref(tty); 73096fd7ce5SGreg Kroah-Hartman if (ld != NULL) { 73196fd7ce5SGreg Kroah-Hartman if (ld->ops->flush_buffer) 73296fd7ce5SGreg Kroah-Hartman ld->ops->flush_buffer(tty); 73396fd7ce5SGreg Kroah-Hartman tty_driver_flush_buffer(tty); 73496fd7ce5SGreg Kroah-Hartman if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) && 73596fd7ce5SGreg Kroah-Hartman ld->ops->write_wakeup) 73696fd7ce5SGreg Kroah-Hartman ld->ops->write_wakeup(tty); 73796fd7ce5SGreg Kroah-Hartman if (ld->ops->hangup) 73896fd7ce5SGreg Kroah-Hartman ld->ops->hangup(tty); 73996fd7ce5SGreg Kroah-Hartman tty_ldisc_deref(ld); 74096fd7ce5SGreg Kroah-Hartman } 74136697529SPeter Hurley 742a9a08845SLinus Torvalds wake_up_interruptible_poll(&tty->write_wait, EPOLLOUT); 743a9a08845SLinus Torvalds wake_up_interruptible_poll(&tty->read_wait, EPOLLIN); 74436697529SPeter Hurley 74596fd7ce5SGreg Kroah-Hartman /* 74696fd7ce5SGreg Kroah-Hartman * Shutdown the current line discipline, and reset it to 74796fd7ce5SGreg Kroah-Hartman * N_TTY if need be. 74896fd7ce5SGreg Kroah-Hartman * 74996fd7ce5SGreg Kroah-Hartman * Avoid racing set_ldisc or tty_ldisc_release 75096fd7ce5SGreg Kroah-Hartman */ 751fae76e9aSPeter Hurley tty_ldisc_lock(tty, MAX_SCHEDULE_TIMEOUT); 75296fd7ce5SGreg Kroah-Hartman 753892d1fa7SPeter Hurley if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) 75496fd7ce5SGreg Kroah-Hartman tty_reset_termios(tty); 755fc575ee6SPeter Hurley 756892d1fa7SPeter Hurley if (tty->ldisc) { 757892d1fa7SPeter Hurley if (reinit) { 758e65c62b1SJohannes Weiner if (tty_ldisc_reinit(tty, tty->termios.c_line) < 0 && 759e65c62b1SJohannes Weiner tty_ldisc_reinit(tty, N_TTY) < 0) 760e65c62b1SJohannes Weiner WARN_ON(tty_ldisc_reinit(tty, N_NULL) < 0); 761892d1fa7SPeter Hurley } else 762892d1fa7SPeter Hurley tty_ldisc_kill(tty); 763892d1fa7SPeter Hurley } 764892d1fa7SPeter Hurley tty_ldisc_unlock(tty); 76596fd7ce5SGreg Kroah-Hartman } 76696fd7ce5SGreg Kroah-Hartman 76796fd7ce5SGreg Kroah-Hartman /** 76896fd7ce5SGreg Kroah-Hartman * tty_ldisc_setup - open line discipline 76996fd7ce5SGreg Kroah-Hartman * @tty: tty being shut down 77096fd7ce5SGreg Kroah-Hartman * @o_tty: pair tty for pty/tty pairs 77196fd7ce5SGreg Kroah-Hartman * 77296fd7ce5SGreg Kroah-Hartman * Called during the initial open of a tty/pty pair in order to set up the 77396fd7ce5SGreg Kroah-Hartman * line disciplines and bind them to the tty. This has no locking issues 77496fd7ce5SGreg Kroah-Hartman * as the device isn't yet active. 77596fd7ce5SGreg Kroah-Hartman */ 77696fd7ce5SGreg Kroah-Hartman 77796fd7ce5SGreg Kroah-Hartman int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty) 77896fd7ce5SGreg Kroah-Hartman { 7799de2a7ceSPeter Hurley int retval = tty_ldisc_open(tty, tty->ldisc); 780d7238359SXiaofei Tan 78196fd7ce5SGreg Kroah-Hartman if (retval) 78296fd7ce5SGreg Kroah-Hartman return retval; 78396fd7ce5SGreg Kroah-Hartman 78496fd7ce5SGreg Kroah-Hartman if (o_tty) { 785110b8928SDmitry Safonov /* 786110b8928SDmitry Safonov * Called without o_tty->ldisc_sem held, as o_tty has been 787110b8928SDmitry Safonov * just allocated and no one has a reference to it. 788110b8928SDmitry Safonov */ 78996fd7ce5SGreg Kroah-Hartman retval = tty_ldisc_open(o_tty, o_tty->ldisc); 79096fd7ce5SGreg Kroah-Hartman if (retval) { 7919de2a7ceSPeter Hurley tty_ldisc_close(tty, tty->ldisc); 79296fd7ce5SGreg Kroah-Hartman return retval; 79396fd7ce5SGreg Kroah-Hartman } 79496fd7ce5SGreg Kroah-Hartman } 79596fd7ce5SGreg Kroah-Hartman return 0; 79696fd7ce5SGreg Kroah-Hartman } 79789c8d91eSAlan Cox 79896fd7ce5SGreg Kroah-Hartman /** 79996fd7ce5SGreg Kroah-Hartman * tty_ldisc_release - release line discipline 80062462aefSPeter Hurley * @tty: tty being shut down (or one end of pty pair) 80196fd7ce5SGreg Kroah-Hartman * 80262462aefSPeter Hurley * Called during the final close of a tty or a pty pair in order to shut 8035b6e6832SPeter Hurley * down the line discpline layer. On exit, each tty's ldisc is NULL. 80496fd7ce5SGreg Kroah-Hartman */ 80596fd7ce5SGreg Kroah-Hartman 80662462aefSPeter Hurley void tty_ldisc_release(struct tty_struct *tty) 80796fd7ce5SGreg Kroah-Hartman { 80862462aefSPeter Hurley struct tty_struct *o_tty = tty->link; 80962462aefSPeter Hurley 81096fd7ce5SGreg Kroah-Hartman /* 811a2965b7bSPeter Hurley * Shutdown this line discipline. As this is the final close, 812a2965b7bSPeter Hurley * it does not race with the set_ldisc code path. 81396fd7ce5SGreg Kroah-Hartman */ 81496fd7ce5SGreg Kroah-Hartman 81536697529SPeter Hurley tty_ldisc_lock_pair(tty, o_tty); 81689c8d91eSAlan Cox tty_ldisc_kill(tty); 81789c8d91eSAlan Cox if (o_tty) 81889c8d91eSAlan Cox tty_ldisc_kill(o_tty); 81936697529SPeter Hurley tty_ldisc_unlock_pair(tty, o_tty); 82036697529SPeter Hurley 821*72a8dcd7SXiaofei Tan /* 822*72a8dcd7SXiaofei Tan * And the memory resources remaining (buffers, termios) will be 823*72a8dcd7SXiaofei Tan * disposed of when the kref hits zero 824*72a8dcd7SXiaofei Tan */ 825fc575ee6SPeter Hurley 826fb6edc91SPeter Hurley tty_ldisc_debug(tty, "released\n"); 82796fd7ce5SGreg Kroah-Hartman } 8281ab92da3SOkash Khawaja EXPORT_SYMBOL_GPL(tty_ldisc_release); 82996fd7ce5SGreg Kroah-Hartman 83096fd7ce5SGreg Kroah-Hartman /** 83196fd7ce5SGreg Kroah-Hartman * tty_ldisc_init - ldisc setup for new tty 83296fd7ce5SGreg Kroah-Hartman * @tty: tty being allocated 83396fd7ce5SGreg Kroah-Hartman * 83496fd7ce5SGreg Kroah-Hartman * Set up the line discipline objects for a newly allocated tty. Note that 83596fd7ce5SGreg Kroah-Hartman * the tty structure is not completely set up when this call is made. 83696fd7ce5SGreg Kroah-Hartman */ 83796fd7ce5SGreg Kroah-Hartman 838903f9db1STetsuo Handa int tty_ldisc_init(struct tty_struct *tty) 83996fd7ce5SGreg Kroah-Hartman { 84036697529SPeter Hurley struct tty_ldisc *ld = tty_ldisc_get(tty, N_TTY); 841d7238359SXiaofei Tan 84296fd7ce5SGreg Kroah-Hartman if (IS_ERR(ld)) 843903f9db1STetsuo Handa return PTR_ERR(ld); 844f4807045SPeter Hurley tty->ldisc = ld; 845903f9db1STetsuo Handa return 0; 84696fd7ce5SGreg Kroah-Hartman } 84796fd7ce5SGreg Kroah-Hartman 8486716671dSJiri Slaby /** 849c8b710b3SPeter Hurley * tty_ldisc_deinit - ldisc cleanup for new tty 8506716671dSJiri Slaby * @tty: tty that was allocated recently 8516716671dSJiri Slaby * 8526716671dSJiri Slaby * The tty structure must not becompletely set up (tty_ldisc_setup) when 8536716671dSJiri Slaby * this call is made. 8546716671dSJiri Slaby */ 8556716671dSJiri Slaby void tty_ldisc_deinit(struct tty_struct *tty) 8566716671dSJiri Slaby { 857110b8928SDmitry Safonov /* no ldisc_sem, tty is being destroyed */ 858c8b710b3SPeter Hurley if (tty->ldisc) 859ebc9baedSPeter Hurley tty_ldisc_put(tty->ldisc); 860f4807045SPeter Hurley tty->ldisc = NULL; 8616716671dSJiri Slaby } 8627c0cca7cSGreg Kroah-Hartman 8637c0cca7cSGreg Kroah-Hartman static struct ctl_table tty_table[] = { 8647c0cca7cSGreg Kroah-Hartman { 8657c0cca7cSGreg Kroah-Hartman .procname = "ldisc_autoload", 8667c0cca7cSGreg Kroah-Hartman .data = &tty_ldisc_autoload, 8677c0cca7cSGreg Kroah-Hartman .maxlen = sizeof(tty_ldisc_autoload), 8687c0cca7cSGreg Kroah-Hartman .mode = 0644, 8697c0cca7cSGreg Kroah-Hartman .proc_handler = proc_dointvec, 870eec4844fSMatteo Croce .extra1 = SYSCTL_ZERO, 871eec4844fSMatteo Croce .extra2 = SYSCTL_ONE, 8727c0cca7cSGreg Kroah-Hartman }, 8737c0cca7cSGreg Kroah-Hartman { } 8747c0cca7cSGreg Kroah-Hartman }; 8757c0cca7cSGreg Kroah-Hartman 8767c0cca7cSGreg Kroah-Hartman static struct ctl_table tty_dir_table[] = { 8777c0cca7cSGreg Kroah-Hartman { 8787c0cca7cSGreg Kroah-Hartman .procname = "tty", 8797c0cca7cSGreg Kroah-Hartman .mode = 0555, 8807c0cca7cSGreg Kroah-Hartman .child = tty_table, 8817c0cca7cSGreg Kroah-Hartman }, 8827c0cca7cSGreg Kroah-Hartman { } 8837c0cca7cSGreg Kroah-Hartman }; 8847c0cca7cSGreg Kroah-Hartman 8857c0cca7cSGreg Kroah-Hartman static struct ctl_table tty_root_table[] = { 8867c0cca7cSGreg Kroah-Hartman { 8877c0cca7cSGreg Kroah-Hartman .procname = "dev", 8887c0cca7cSGreg Kroah-Hartman .mode = 0555, 8897c0cca7cSGreg Kroah-Hartman .child = tty_dir_table, 8907c0cca7cSGreg Kroah-Hartman }, 8917c0cca7cSGreg Kroah-Hartman { } 8927c0cca7cSGreg Kroah-Hartman }; 8937c0cca7cSGreg Kroah-Hartman 8947c0cca7cSGreg Kroah-Hartman void tty_sysctl_init(void) 8957c0cca7cSGreg Kroah-Hartman { 8967c0cca7cSGreg Kroah-Hartman register_sysctl_table(tty_root_table); 8977c0cca7cSGreg Kroah-Hartman } 898