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/init.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 2396fd7ce5SGreg Kroah-Hartman /* 2496fd7ce5SGreg Kroah-Hartman * This guards the refcounted line discipline lists. The lock 2596fd7ce5SGreg Kroah-Hartman * must be taken with irqs off because there are hangup path 2696fd7ce5SGreg Kroah-Hartman * callers who will do ldisc lookups and cannot sleep. 2796fd7ce5SGreg Kroah-Hartman */ 2896fd7ce5SGreg Kroah-Hartman 29c9739941SIvo Sieben static DEFINE_RAW_SPINLOCK(tty_ldisc_lock); 3096fd7ce5SGreg Kroah-Hartman static DECLARE_WAIT_QUEUE_HEAD(tty_ldisc_wait); 3196fd7ce5SGreg Kroah-Hartman /* Line disc dispatch table */ 3296fd7ce5SGreg Kroah-Hartman static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS]; 3396fd7ce5SGreg Kroah-Hartman 3496fd7ce5SGreg Kroah-Hartman static inline struct tty_ldisc *get_ldisc(struct tty_ldisc *ld) 3596fd7ce5SGreg Kroah-Hartman { 3696fd7ce5SGreg Kroah-Hartman if (ld) 3796fd7ce5SGreg Kroah-Hartman atomic_inc(&ld->users); 3896fd7ce5SGreg Kroah-Hartman return ld; 3996fd7ce5SGreg Kroah-Hartman } 4096fd7ce5SGreg Kroah-Hartman 4196fd7ce5SGreg Kroah-Hartman static void put_ldisc(struct tty_ldisc *ld) 4296fd7ce5SGreg Kroah-Hartman { 4396fd7ce5SGreg Kroah-Hartman unsigned long flags; 4496fd7ce5SGreg Kroah-Hartman 4596fd7ce5SGreg Kroah-Hartman if (WARN_ON_ONCE(!ld)) 4696fd7ce5SGreg Kroah-Hartman return; 4796fd7ce5SGreg Kroah-Hartman 4896fd7ce5SGreg Kroah-Hartman /* 4996fd7ce5SGreg Kroah-Hartman * If this is the last user, free the ldisc, and 5096fd7ce5SGreg Kroah-Hartman * release the ldisc ops. 5196fd7ce5SGreg Kroah-Hartman * 52c9739941SIvo Sieben * We really want an "atomic_dec_and_raw_lock_irqsave()", 5396fd7ce5SGreg Kroah-Hartman * but we don't have it, so this does it by hand. 5496fd7ce5SGreg Kroah-Hartman */ 55c9739941SIvo Sieben raw_spin_lock_irqsave(&tty_ldisc_lock, flags); 56c9739941SIvo Sieben if (atomic_dec_and_test(&ld->users)) { 5796fd7ce5SGreg Kroah-Hartman struct tty_ldisc_ops *ldo = ld->ops; 5896fd7ce5SGreg Kroah-Hartman 5996fd7ce5SGreg Kroah-Hartman ldo->refcount--; 6096fd7ce5SGreg Kroah-Hartman module_put(ldo->owner); 61c9739941SIvo Sieben raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags); 6296fd7ce5SGreg Kroah-Hartman 6396fd7ce5SGreg Kroah-Hartman kfree(ld); 6496fd7ce5SGreg Kroah-Hartman return; 6596fd7ce5SGreg Kroah-Hartman } 66c9739941SIvo Sieben raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags); 67bd5d7ce9SIvo Sieben 68bd5d7ce9SIvo Sieben if (waitqueue_active(&ld->wq_idle)) 691541f845SIvo Sieben wake_up(&ld->wq_idle); 7096fd7ce5SGreg Kroah-Hartman } 7196fd7ce5SGreg Kroah-Hartman 7296fd7ce5SGreg Kroah-Hartman /** 7396fd7ce5SGreg Kroah-Hartman * tty_register_ldisc - install a line discipline 7496fd7ce5SGreg Kroah-Hartman * @disc: ldisc number 7596fd7ce5SGreg Kroah-Hartman * @new_ldisc: pointer to the ldisc object 7696fd7ce5SGreg Kroah-Hartman * 7796fd7ce5SGreg Kroah-Hartman * Installs a new line discipline into the kernel. The discipline 7896fd7ce5SGreg Kroah-Hartman * is set up as unreferenced and then made available to the kernel 7996fd7ce5SGreg Kroah-Hartman * from this point onwards. 8096fd7ce5SGreg Kroah-Hartman * 8196fd7ce5SGreg Kroah-Hartman * Locking: 8296fd7ce5SGreg Kroah-Hartman * takes tty_ldisc_lock to guard against ldisc races 8396fd7ce5SGreg Kroah-Hartman */ 8496fd7ce5SGreg Kroah-Hartman 8596fd7ce5SGreg Kroah-Hartman int tty_register_ldisc(int disc, struct tty_ldisc_ops *new_ldisc) 8696fd7ce5SGreg Kroah-Hartman { 8796fd7ce5SGreg Kroah-Hartman unsigned long flags; 8896fd7ce5SGreg Kroah-Hartman int ret = 0; 8996fd7ce5SGreg Kroah-Hartman 9096fd7ce5SGreg Kroah-Hartman if (disc < N_TTY || disc >= NR_LDISCS) 9196fd7ce5SGreg Kroah-Hartman return -EINVAL; 9296fd7ce5SGreg Kroah-Hartman 93c9739941SIvo Sieben raw_spin_lock_irqsave(&tty_ldisc_lock, flags); 9496fd7ce5SGreg Kroah-Hartman tty_ldiscs[disc] = new_ldisc; 9596fd7ce5SGreg Kroah-Hartman new_ldisc->num = disc; 9696fd7ce5SGreg Kroah-Hartman new_ldisc->refcount = 0; 97c9739941SIvo Sieben raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags); 9896fd7ce5SGreg Kroah-Hartman 9996fd7ce5SGreg Kroah-Hartman return ret; 10096fd7ce5SGreg Kroah-Hartman } 10196fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_register_ldisc); 10296fd7ce5SGreg Kroah-Hartman 10396fd7ce5SGreg Kroah-Hartman /** 10496fd7ce5SGreg Kroah-Hartman * tty_unregister_ldisc - unload a line discipline 10596fd7ce5SGreg Kroah-Hartman * @disc: ldisc number 10696fd7ce5SGreg Kroah-Hartman * @new_ldisc: pointer to the ldisc object 10796fd7ce5SGreg Kroah-Hartman * 10896fd7ce5SGreg Kroah-Hartman * Remove a line discipline from the kernel providing it is not 10996fd7ce5SGreg Kroah-Hartman * currently in use. 11096fd7ce5SGreg Kroah-Hartman * 11196fd7ce5SGreg Kroah-Hartman * Locking: 11296fd7ce5SGreg Kroah-Hartman * takes tty_ldisc_lock to guard against ldisc races 11396fd7ce5SGreg Kroah-Hartman */ 11496fd7ce5SGreg Kroah-Hartman 11596fd7ce5SGreg Kroah-Hartman int tty_unregister_ldisc(int disc) 11696fd7ce5SGreg Kroah-Hartman { 11796fd7ce5SGreg Kroah-Hartman unsigned long flags; 11896fd7ce5SGreg Kroah-Hartman int ret = 0; 11996fd7ce5SGreg Kroah-Hartman 12096fd7ce5SGreg Kroah-Hartman if (disc < N_TTY || disc >= NR_LDISCS) 12196fd7ce5SGreg Kroah-Hartman return -EINVAL; 12296fd7ce5SGreg Kroah-Hartman 123c9739941SIvo Sieben raw_spin_lock_irqsave(&tty_ldisc_lock, flags); 12496fd7ce5SGreg Kroah-Hartman if (tty_ldiscs[disc]->refcount) 12596fd7ce5SGreg Kroah-Hartman ret = -EBUSY; 12696fd7ce5SGreg Kroah-Hartman else 12796fd7ce5SGreg Kroah-Hartman tty_ldiscs[disc] = NULL; 128c9739941SIvo Sieben raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags); 12996fd7ce5SGreg Kroah-Hartman 13096fd7ce5SGreg Kroah-Hartman return ret; 13196fd7ce5SGreg Kroah-Hartman } 13296fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL(tty_unregister_ldisc); 13396fd7ce5SGreg Kroah-Hartman 13496fd7ce5SGreg Kroah-Hartman static struct tty_ldisc_ops *get_ldops(int disc) 13596fd7ce5SGreg Kroah-Hartman { 13696fd7ce5SGreg Kroah-Hartman unsigned long flags; 13796fd7ce5SGreg Kroah-Hartman struct tty_ldisc_ops *ldops, *ret; 13896fd7ce5SGreg Kroah-Hartman 139c9739941SIvo Sieben raw_spin_lock_irqsave(&tty_ldisc_lock, flags); 14096fd7ce5SGreg Kroah-Hartman ret = ERR_PTR(-EINVAL); 14196fd7ce5SGreg Kroah-Hartman ldops = tty_ldiscs[disc]; 14296fd7ce5SGreg Kroah-Hartman if (ldops) { 14396fd7ce5SGreg Kroah-Hartman ret = ERR_PTR(-EAGAIN); 14496fd7ce5SGreg Kroah-Hartman if (try_module_get(ldops->owner)) { 14596fd7ce5SGreg Kroah-Hartman ldops->refcount++; 14696fd7ce5SGreg Kroah-Hartman ret = ldops; 14796fd7ce5SGreg Kroah-Hartman } 14896fd7ce5SGreg Kroah-Hartman } 149c9739941SIvo Sieben raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags); 15096fd7ce5SGreg Kroah-Hartman return ret; 15196fd7ce5SGreg Kroah-Hartman } 15296fd7ce5SGreg Kroah-Hartman 15396fd7ce5SGreg Kroah-Hartman static void put_ldops(struct tty_ldisc_ops *ldops) 15496fd7ce5SGreg Kroah-Hartman { 15596fd7ce5SGreg Kroah-Hartman unsigned long flags; 15696fd7ce5SGreg Kroah-Hartman 157c9739941SIvo Sieben raw_spin_lock_irqsave(&tty_ldisc_lock, flags); 15896fd7ce5SGreg Kroah-Hartman ldops->refcount--; 15996fd7ce5SGreg Kroah-Hartman module_put(ldops->owner); 160c9739941SIvo Sieben raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags); 16196fd7ce5SGreg Kroah-Hartman } 16296fd7ce5SGreg Kroah-Hartman 16396fd7ce5SGreg Kroah-Hartman /** 16496fd7ce5SGreg Kroah-Hartman * tty_ldisc_get - take a reference to an ldisc 16596fd7ce5SGreg Kroah-Hartman * @disc: ldisc number 16696fd7ce5SGreg Kroah-Hartman * 16796fd7ce5SGreg Kroah-Hartman * Takes a reference to a line discipline. Deals with refcounts and 16896fd7ce5SGreg Kroah-Hartman * module locking counts. Returns NULL if the discipline is not available. 16996fd7ce5SGreg Kroah-Hartman * Returns a pointer to the discipline and bumps the ref count if it is 17096fd7ce5SGreg Kroah-Hartman * available 17196fd7ce5SGreg Kroah-Hartman * 17296fd7ce5SGreg Kroah-Hartman * Locking: 17396fd7ce5SGreg Kroah-Hartman * takes tty_ldisc_lock to guard against ldisc races 17496fd7ce5SGreg Kroah-Hartman */ 17596fd7ce5SGreg Kroah-Hartman 17696fd7ce5SGreg Kroah-Hartman static struct tty_ldisc *tty_ldisc_get(int disc) 17796fd7ce5SGreg Kroah-Hartman { 17896fd7ce5SGreg Kroah-Hartman struct tty_ldisc *ld; 17996fd7ce5SGreg Kroah-Hartman struct tty_ldisc_ops *ldops; 18096fd7ce5SGreg Kroah-Hartman 18196fd7ce5SGreg Kroah-Hartman if (disc < N_TTY || disc >= NR_LDISCS) 18296fd7ce5SGreg Kroah-Hartman return ERR_PTR(-EINVAL); 18396fd7ce5SGreg Kroah-Hartman 18496fd7ce5SGreg Kroah-Hartman /* 18596fd7ce5SGreg Kroah-Hartman * Get the ldisc ops - we may need to request them to be loaded 18696fd7ce5SGreg Kroah-Hartman * dynamically and try again. 18796fd7ce5SGreg Kroah-Hartman */ 18896fd7ce5SGreg Kroah-Hartman ldops = get_ldops(disc); 18996fd7ce5SGreg Kroah-Hartman if (IS_ERR(ldops)) { 19096fd7ce5SGreg Kroah-Hartman request_module("tty-ldisc-%d", disc); 19196fd7ce5SGreg Kroah-Hartman ldops = get_ldops(disc); 19296fd7ce5SGreg Kroah-Hartman if (IS_ERR(ldops)) 19396fd7ce5SGreg Kroah-Hartman return ERR_CAST(ldops); 19496fd7ce5SGreg Kroah-Hartman } 19596fd7ce5SGreg Kroah-Hartman 19696fd7ce5SGreg Kroah-Hartman ld = kmalloc(sizeof(struct tty_ldisc), GFP_KERNEL); 19796fd7ce5SGreg Kroah-Hartman if (ld == NULL) { 19896fd7ce5SGreg Kroah-Hartman put_ldops(ldops); 19996fd7ce5SGreg Kroah-Hartman return ERR_PTR(-ENOMEM); 20096fd7ce5SGreg Kroah-Hartman } 20196fd7ce5SGreg Kroah-Hartman 20296fd7ce5SGreg Kroah-Hartman ld->ops = ldops; 20396fd7ce5SGreg Kroah-Hartman atomic_set(&ld->users, 1); 2041541f845SIvo Sieben init_waitqueue_head(&ld->wq_idle); 2051541f845SIvo Sieben 20696fd7ce5SGreg Kroah-Hartman return ld; 20796fd7ce5SGreg Kroah-Hartman } 20896fd7ce5SGreg Kroah-Hartman 20996fd7ce5SGreg Kroah-Hartman static void *tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos) 21096fd7ce5SGreg Kroah-Hartman { 21196fd7ce5SGreg Kroah-Hartman return (*pos < NR_LDISCS) ? pos : NULL; 21296fd7ce5SGreg Kroah-Hartman } 21396fd7ce5SGreg Kroah-Hartman 21496fd7ce5SGreg Kroah-Hartman static void *tty_ldiscs_seq_next(struct seq_file *m, void *v, loff_t *pos) 21596fd7ce5SGreg Kroah-Hartman { 21696fd7ce5SGreg Kroah-Hartman (*pos)++; 21796fd7ce5SGreg Kroah-Hartman return (*pos < NR_LDISCS) ? pos : NULL; 21896fd7ce5SGreg Kroah-Hartman } 21996fd7ce5SGreg Kroah-Hartman 22096fd7ce5SGreg Kroah-Hartman static void tty_ldiscs_seq_stop(struct seq_file *m, void *v) 22196fd7ce5SGreg Kroah-Hartman { 22296fd7ce5SGreg Kroah-Hartman } 22396fd7ce5SGreg Kroah-Hartman 22496fd7ce5SGreg Kroah-Hartman static int tty_ldiscs_seq_show(struct seq_file *m, void *v) 22596fd7ce5SGreg Kroah-Hartman { 22696fd7ce5SGreg Kroah-Hartman int i = *(loff_t *)v; 22796fd7ce5SGreg Kroah-Hartman struct tty_ldisc_ops *ldops; 22896fd7ce5SGreg Kroah-Hartman 22996fd7ce5SGreg Kroah-Hartman ldops = get_ldops(i); 23096fd7ce5SGreg Kroah-Hartman if (IS_ERR(ldops)) 23196fd7ce5SGreg Kroah-Hartman return 0; 23296fd7ce5SGreg Kroah-Hartman seq_printf(m, "%-10s %2d\n", ldops->name ? ldops->name : "???", i); 23396fd7ce5SGreg Kroah-Hartman put_ldops(ldops); 23496fd7ce5SGreg Kroah-Hartman return 0; 23596fd7ce5SGreg Kroah-Hartman } 23696fd7ce5SGreg Kroah-Hartman 23796fd7ce5SGreg Kroah-Hartman static const struct seq_operations tty_ldiscs_seq_ops = { 23896fd7ce5SGreg Kroah-Hartman .start = tty_ldiscs_seq_start, 23996fd7ce5SGreg Kroah-Hartman .next = tty_ldiscs_seq_next, 24096fd7ce5SGreg Kroah-Hartman .stop = tty_ldiscs_seq_stop, 24196fd7ce5SGreg Kroah-Hartman .show = tty_ldiscs_seq_show, 24296fd7ce5SGreg Kroah-Hartman }; 24396fd7ce5SGreg Kroah-Hartman 24496fd7ce5SGreg Kroah-Hartman static int proc_tty_ldiscs_open(struct inode *inode, struct file *file) 24596fd7ce5SGreg Kroah-Hartman { 24696fd7ce5SGreg Kroah-Hartman return seq_open(file, &tty_ldiscs_seq_ops); 24796fd7ce5SGreg Kroah-Hartman } 24896fd7ce5SGreg Kroah-Hartman 24996fd7ce5SGreg Kroah-Hartman const struct file_operations tty_ldiscs_proc_fops = { 25096fd7ce5SGreg Kroah-Hartman .owner = THIS_MODULE, 25196fd7ce5SGreg Kroah-Hartman .open = proc_tty_ldiscs_open, 25296fd7ce5SGreg Kroah-Hartman .read = seq_read, 25396fd7ce5SGreg Kroah-Hartman .llseek = seq_lseek, 25496fd7ce5SGreg Kroah-Hartman .release = seq_release, 25596fd7ce5SGreg Kroah-Hartman }; 25696fd7ce5SGreg Kroah-Hartman 25796fd7ce5SGreg Kroah-Hartman /** 25896fd7ce5SGreg Kroah-Hartman * tty_ldisc_assign - set ldisc on a tty 25996fd7ce5SGreg Kroah-Hartman * @tty: tty to assign 26096fd7ce5SGreg Kroah-Hartman * @ld: line discipline 26196fd7ce5SGreg Kroah-Hartman * 26296fd7ce5SGreg Kroah-Hartman * Install an instance of a line discipline into a tty structure. The 26396fd7ce5SGreg Kroah-Hartman * ldisc must have a reference count above zero to ensure it remains. 26496fd7ce5SGreg Kroah-Hartman * The tty instance refcount starts at zero. 26596fd7ce5SGreg Kroah-Hartman * 26696fd7ce5SGreg Kroah-Hartman * Locking: 26796fd7ce5SGreg Kroah-Hartman * Caller must hold references 26896fd7ce5SGreg Kroah-Hartman */ 26996fd7ce5SGreg Kroah-Hartman 27096fd7ce5SGreg Kroah-Hartman static void tty_ldisc_assign(struct tty_struct *tty, struct tty_ldisc *ld) 27196fd7ce5SGreg Kroah-Hartman { 27296fd7ce5SGreg Kroah-Hartman tty->ldisc = ld; 27396fd7ce5SGreg Kroah-Hartman } 27496fd7ce5SGreg Kroah-Hartman 27596fd7ce5SGreg Kroah-Hartman /** 27696fd7ce5SGreg Kroah-Hartman * tty_ldisc_try - internal helper 27796fd7ce5SGreg Kroah-Hartman * @tty: the tty 27896fd7ce5SGreg Kroah-Hartman * 27996fd7ce5SGreg Kroah-Hartman * Make a single attempt to grab and bump the refcount on 28096fd7ce5SGreg Kroah-Hartman * the tty ldisc. Return 0 on failure or 1 on success. This is 28196fd7ce5SGreg Kroah-Hartman * used to implement both the waiting and non waiting versions 28296fd7ce5SGreg Kroah-Hartman * of tty_ldisc_ref 28396fd7ce5SGreg Kroah-Hartman * 28496fd7ce5SGreg Kroah-Hartman * Locking: takes tty_ldisc_lock 28596fd7ce5SGreg Kroah-Hartman */ 28696fd7ce5SGreg Kroah-Hartman 28796fd7ce5SGreg Kroah-Hartman static struct tty_ldisc *tty_ldisc_try(struct tty_struct *tty) 28896fd7ce5SGreg Kroah-Hartman { 28996fd7ce5SGreg Kroah-Hartman unsigned long flags; 29096fd7ce5SGreg Kroah-Hartman struct tty_ldisc *ld; 29196fd7ce5SGreg Kroah-Hartman 292c9739941SIvo Sieben raw_spin_lock_irqsave(&tty_ldisc_lock, flags); 29396fd7ce5SGreg Kroah-Hartman ld = NULL; 29496fd7ce5SGreg Kroah-Hartman if (test_bit(TTY_LDISC, &tty->flags)) 29596fd7ce5SGreg Kroah-Hartman ld = get_ldisc(tty->ldisc); 296c9739941SIvo Sieben raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags); 29796fd7ce5SGreg Kroah-Hartman return ld; 29896fd7ce5SGreg Kroah-Hartman } 29996fd7ce5SGreg Kroah-Hartman 30096fd7ce5SGreg Kroah-Hartman /** 30196fd7ce5SGreg Kroah-Hartman * tty_ldisc_ref_wait - wait for the tty ldisc 30296fd7ce5SGreg Kroah-Hartman * @tty: tty device 30396fd7ce5SGreg Kroah-Hartman * 30496fd7ce5SGreg Kroah-Hartman * Dereference the line discipline for the terminal and take a 30596fd7ce5SGreg Kroah-Hartman * reference to it. If the line discipline is in flux then 30696fd7ce5SGreg Kroah-Hartman * wait patiently until it changes. 30796fd7ce5SGreg Kroah-Hartman * 30896fd7ce5SGreg Kroah-Hartman * Note: Must not be called from an IRQ/timer context. The caller 30996fd7ce5SGreg Kroah-Hartman * must also be careful not to hold other locks that will deadlock 31096fd7ce5SGreg Kroah-Hartman * against a discipline change, such as an existing ldisc reference 31196fd7ce5SGreg Kroah-Hartman * (which we check for) 31296fd7ce5SGreg Kroah-Hartman * 31396fd7ce5SGreg Kroah-Hartman * Locking: call functions take tty_ldisc_lock 31496fd7ce5SGreg Kroah-Hartman */ 31596fd7ce5SGreg Kroah-Hartman 31696fd7ce5SGreg Kroah-Hartman struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty) 31796fd7ce5SGreg Kroah-Hartman { 31896fd7ce5SGreg Kroah-Hartman struct tty_ldisc *ld; 31996fd7ce5SGreg Kroah-Hartman 32096fd7ce5SGreg Kroah-Hartman /* wait_event is a macro */ 32196fd7ce5SGreg Kroah-Hartman wait_event(tty_ldisc_wait, (ld = tty_ldisc_try(tty)) != NULL); 32296fd7ce5SGreg Kroah-Hartman return ld; 32396fd7ce5SGreg Kroah-Hartman } 32496fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait); 32596fd7ce5SGreg Kroah-Hartman 32696fd7ce5SGreg Kroah-Hartman /** 32796fd7ce5SGreg Kroah-Hartman * tty_ldisc_ref - get the tty ldisc 32896fd7ce5SGreg Kroah-Hartman * @tty: tty device 32996fd7ce5SGreg Kroah-Hartman * 33096fd7ce5SGreg Kroah-Hartman * Dereference the line discipline for the terminal and take a 33196fd7ce5SGreg Kroah-Hartman * reference to it. If the line discipline is in flux then 33296fd7ce5SGreg Kroah-Hartman * return NULL. Can be called from IRQ and timer functions. 33396fd7ce5SGreg Kroah-Hartman * 33496fd7ce5SGreg Kroah-Hartman * Locking: called functions take tty_ldisc_lock 33596fd7ce5SGreg Kroah-Hartman */ 33696fd7ce5SGreg Kroah-Hartman 33796fd7ce5SGreg Kroah-Hartman struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty) 33896fd7ce5SGreg Kroah-Hartman { 33996fd7ce5SGreg Kroah-Hartman return tty_ldisc_try(tty); 34096fd7ce5SGreg Kroah-Hartman } 34196fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(tty_ldisc_ref); 34296fd7ce5SGreg Kroah-Hartman 34396fd7ce5SGreg Kroah-Hartman /** 34496fd7ce5SGreg Kroah-Hartman * tty_ldisc_deref - free a tty ldisc reference 34596fd7ce5SGreg Kroah-Hartman * @ld: reference to free up 34696fd7ce5SGreg Kroah-Hartman * 34796fd7ce5SGreg Kroah-Hartman * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May 34896fd7ce5SGreg Kroah-Hartman * be called in IRQ context. 34996fd7ce5SGreg Kroah-Hartman * 35096fd7ce5SGreg Kroah-Hartman * Locking: takes tty_ldisc_lock 35196fd7ce5SGreg Kroah-Hartman */ 35296fd7ce5SGreg Kroah-Hartman 35396fd7ce5SGreg Kroah-Hartman void tty_ldisc_deref(struct tty_ldisc *ld) 35496fd7ce5SGreg Kroah-Hartman { 35596fd7ce5SGreg Kroah-Hartman put_ldisc(ld); 35696fd7ce5SGreg Kroah-Hartman } 35796fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(tty_ldisc_deref); 35896fd7ce5SGreg Kroah-Hartman 35996fd7ce5SGreg Kroah-Hartman static inline void tty_ldisc_put(struct tty_ldisc *ld) 36096fd7ce5SGreg Kroah-Hartman { 36196fd7ce5SGreg Kroah-Hartman put_ldisc(ld); 36296fd7ce5SGreg Kroah-Hartman } 36396fd7ce5SGreg Kroah-Hartman 36496fd7ce5SGreg Kroah-Hartman /** 36596fd7ce5SGreg Kroah-Hartman * tty_ldisc_enable - allow ldisc use 36696fd7ce5SGreg Kroah-Hartman * @tty: terminal to activate ldisc on 36796fd7ce5SGreg Kroah-Hartman * 36896fd7ce5SGreg Kroah-Hartman * Set the TTY_LDISC flag when the line discipline can be called 36996fd7ce5SGreg Kroah-Hartman * again. Do necessary wakeups for existing sleepers. Clear the LDISC 37096fd7ce5SGreg Kroah-Hartman * changing flag to indicate any ldisc change is now over. 37196fd7ce5SGreg Kroah-Hartman * 37296fd7ce5SGreg Kroah-Hartman * Note: nobody should set the TTY_LDISC bit except via this function. 37396fd7ce5SGreg Kroah-Hartman * Clearing directly is allowed. 37496fd7ce5SGreg Kroah-Hartman */ 37596fd7ce5SGreg Kroah-Hartman 37696fd7ce5SGreg Kroah-Hartman void tty_ldisc_enable(struct tty_struct *tty) 37796fd7ce5SGreg Kroah-Hartman { 37821622939SPeter Hurley clear_bit(TTY_LDISC_HALTED, &tty->flags); 37996fd7ce5SGreg Kroah-Hartman set_bit(TTY_LDISC, &tty->flags); 38096fd7ce5SGreg Kroah-Hartman clear_bit(TTY_LDISC_CHANGING, &tty->flags); 38196fd7ce5SGreg Kroah-Hartman wake_up(&tty_ldisc_wait); 38296fd7ce5SGreg Kroah-Hartman } 38396fd7ce5SGreg Kroah-Hartman 38496fd7ce5SGreg Kroah-Hartman /** 38596fd7ce5SGreg Kroah-Hartman * tty_ldisc_flush - flush line discipline queue 38696fd7ce5SGreg Kroah-Hartman * @tty: tty 38796fd7ce5SGreg Kroah-Hartman * 38896fd7ce5SGreg Kroah-Hartman * Flush the line discipline queue (if any) for this tty. If there 38996fd7ce5SGreg Kroah-Hartman * is no line discipline active this is a no-op. 39096fd7ce5SGreg Kroah-Hartman */ 39196fd7ce5SGreg Kroah-Hartman 39296fd7ce5SGreg Kroah-Hartman void tty_ldisc_flush(struct tty_struct *tty) 39396fd7ce5SGreg Kroah-Hartman { 39496fd7ce5SGreg Kroah-Hartman struct tty_ldisc *ld = tty_ldisc_ref(tty); 39596fd7ce5SGreg Kroah-Hartman if (ld) { 39696fd7ce5SGreg Kroah-Hartman if (ld->ops->flush_buffer) 39796fd7ce5SGreg Kroah-Hartman ld->ops->flush_buffer(tty); 39896fd7ce5SGreg Kroah-Hartman tty_ldisc_deref(ld); 39996fd7ce5SGreg Kroah-Hartman } 40096fd7ce5SGreg Kroah-Hartman tty_buffer_flush(tty); 40196fd7ce5SGreg Kroah-Hartman } 40296fd7ce5SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(tty_ldisc_flush); 40396fd7ce5SGreg Kroah-Hartman 40496fd7ce5SGreg Kroah-Hartman /** 40596fd7ce5SGreg Kroah-Hartman * tty_set_termios_ldisc - set ldisc field 40696fd7ce5SGreg Kroah-Hartman * @tty: tty structure 40796fd7ce5SGreg Kroah-Hartman * @num: line discipline number 40896fd7ce5SGreg Kroah-Hartman * 40996fd7ce5SGreg Kroah-Hartman * This is probably overkill for real world processors but 41096fd7ce5SGreg Kroah-Hartman * they are not on hot paths so a little discipline won't do 41196fd7ce5SGreg Kroah-Hartman * any harm. 41296fd7ce5SGreg Kroah-Hartman * 41396fd7ce5SGreg Kroah-Hartman * Locking: takes termios_mutex 41496fd7ce5SGreg Kroah-Hartman */ 41596fd7ce5SGreg Kroah-Hartman 41696fd7ce5SGreg Kroah-Hartman static void tty_set_termios_ldisc(struct tty_struct *tty, int num) 41796fd7ce5SGreg Kroah-Hartman { 41896fd7ce5SGreg Kroah-Hartman mutex_lock(&tty->termios_mutex); 419adc8d746SAlan Cox tty->termios.c_line = num; 42096fd7ce5SGreg Kroah-Hartman mutex_unlock(&tty->termios_mutex); 42196fd7ce5SGreg Kroah-Hartman } 42296fd7ce5SGreg Kroah-Hartman 42396fd7ce5SGreg Kroah-Hartman /** 42496fd7ce5SGreg Kroah-Hartman * tty_ldisc_open - open a line discipline 42596fd7ce5SGreg Kroah-Hartman * @tty: tty we are opening the ldisc on 42696fd7ce5SGreg Kroah-Hartman * @ld: discipline to open 42796fd7ce5SGreg Kroah-Hartman * 42896fd7ce5SGreg Kroah-Hartman * A helper opening method. Also a convenient debugging and check 42996fd7ce5SGreg Kroah-Hartman * point. 43096fd7ce5SGreg Kroah-Hartman * 43196fd7ce5SGreg Kroah-Hartman * Locking: always called with BTM already held. 43296fd7ce5SGreg Kroah-Hartman */ 43396fd7ce5SGreg Kroah-Hartman 43496fd7ce5SGreg Kroah-Hartman static int tty_ldisc_open(struct tty_struct *tty, struct tty_ldisc *ld) 43596fd7ce5SGreg Kroah-Hartman { 43696fd7ce5SGreg Kroah-Hartman WARN_ON(test_and_set_bit(TTY_LDISC_OPEN, &tty->flags)); 43796fd7ce5SGreg Kroah-Hartman if (ld->ops->open) { 43896fd7ce5SGreg Kroah-Hartman int ret; 43996fd7ce5SGreg Kroah-Hartman /* BTM here locks versus a hangup event */ 44096fd7ce5SGreg Kroah-Hartman ret = ld->ops->open(tty); 4417f90cfc5SJiri Slaby if (ret) 4427f90cfc5SJiri Slaby clear_bit(TTY_LDISC_OPEN, &tty->flags); 44396fd7ce5SGreg Kroah-Hartman return ret; 44496fd7ce5SGreg Kroah-Hartman } 44596fd7ce5SGreg Kroah-Hartman return 0; 44696fd7ce5SGreg Kroah-Hartman } 44796fd7ce5SGreg Kroah-Hartman 44896fd7ce5SGreg Kroah-Hartman /** 44996fd7ce5SGreg Kroah-Hartman * tty_ldisc_close - close a line discipline 45096fd7ce5SGreg Kroah-Hartman * @tty: tty we are opening the ldisc on 45196fd7ce5SGreg Kroah-Hartman * @ld: discipline to close 45296fd7ce5SGreg Kroah-Hartman * 45396fd7ce5SGreg Kroah-Hartman * A helper close method. Also a convenient debugging and check 45496fd7ce5SGreg Kroah-Hartman * point. 45596fd7ce5SGreg Kroah-Hartman */ 45696fd7ce5SGreg Kroah-Hartman 45796fd7ce5SGreg Kroah-Hartman static void tty_ldisc_close(struct tty_struct *tty, struct tty_ldisc *ld) 45896fd7ce5SGreg Kroah-Hartman { 45996fd7ce5SGreg Kroah-Hartman WARN_ON(!test_bit(TTY_LDISC_OPEN, &tty->flags)); 46096fd7ce5SGreg Kroah-Hartman clear_bit(TTY_LDISC_OPEN, &tty->flags); 46196fd7ce5SGreg Kroah-Hartman if (ld->ops->close) 46296fd7ce5SGreg Kroah-Hartman ld->ops->close(tty); 46396fd7ce5SGreg Kroah-Hartman } 46496fd7ce5SGreg Kroah-Hartman 46596fd7ce5SGreg Kroah-Hartman /** 46696fd7ce5SGreg Kroah-Hartman * tty_ldisc_restore - helper for tty ldisc change 46796fd7ce5SGreg Kroah-Hartman * @tty: tty to recover 46896fd7ce5SGreg Kroah-Hartman * @old: previous ldisc 46996fd7ce5SGreg Kroah-Hartman * 47096fd7ce5SGreg Kroah-Hartman * Restore the previous line discipline or N_TTY when a line discipline 47196fd7ce5SGreg Kroah-Hartman * change fails due to an open error 47296fd7ce5SGreg Kroah-Hartman */ 47396fd7ce5SGreg Kroah-Hartman 47496fd7ce5SGreg Kroah-Hartman static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old) 47596fd7ce5SGreg Kroah-Hartman { 47696fd7ce5SGreg Kroah-Hartman char buf[64]; 47796fd7ce5SGreg Kroah-Hartman struct tty_ldisc *new_ldisc; 47896fd7ce5SGreg Kroah-Hartman int r; 47996fd7ce5SGreg Kroah-Hartman 48096fd7ce5SGreg Kroah-Hartman /* There is an outstanding reference here so this is safe */ 48196fd7ce5SGreg Kroah-Hartman old = tty_ldisc_get(old->ops->num); 48296fd7ce5SGreg Kroah-Hartman WARN_ON(IS_ERR(old)); 48396fd7ce5SGreg Kroah-Hartman tty_ldisc_assign(tty, old); 48496fd7ce5SGreg Kroah-Hartman tty_set_termios_ldisc(tty, old->ops->num); 48596fd7ce5SGreg Kroah-Hartman if (tty_ldisc_open(tty, old) < 0) { 48696fd7ce5SGreg Kroah-Hartman tty_ldisc_put(old); 48796fd7ce5SGreg Kroah-Hartman /* This driver is always present */ 48896fd7ce5SGreg Kroah-Hartman new_ldisc = tty_ldisc_get(N_TTY); 48996fd7ce5SGreg Kroah-Hartman if (IS_ERR(new_ldisc)) 49096fd7ce5SGreg Kroah-Hartman panic("n_tty: get"); 49196fd7ce5SGreg Kroah-Hartman tty_ldisc_assign(tty, new_ldisc); 49296fd7ce5SGreg Kroah-Hartman tty_set_termios_ldisc(tty, N_TTY); 49396fd7ce5SGreg Kroah-Hartman r = tty_ldisc_open(tty, new_ldisc); 49496fd7ce5SGreg Kroah-Hartman if (r < 0) 49596fd7ce5SGreg Kroah-Hartman panic("Couldn't open N_TTY ldisc for " 49696fd7ce5SGreg Kroah-Hartman "%s --- error %d.", 49796fd7ce5SGreg Kroah-Hartman tty_name(tty, buf), r); 49896fd7ce5SGreg Kroah-Hartman } 49996fd7ce5SGreg Kroah-Hartman } 50096fd7ce5SGreg Kroah-Hartman 50196fd7ce5SGreg Kroah-Hartman /** 5020a1f1a0bSTejun Heo * tty_ldisc_flush_works - flush all works of a tty 5030a1f1a0bSTejun Heo * @tty: tty device to flush works for 5040a1f1a0bSTejun Heo * 5050a1f1a0bSTejun Heo * Sync flush all works belonging to @tty. 5060a1f1a0bSTejun Heo */ 5070a1f1a0bSTejun Heo static void tty_ldisc_flush_works(struct tty_struct *tty) 5080a1f1a0bSTejun Heo { 50943829731STejun Heo flush_work(&tty->hangup_work); 51043829731STejun Heo flush_work(&tty->SAK_work); 511ecbbfd44SJiri Slaby flush_work(&tty->port->buf.work); 5120a1f1a0bSTejun Heo } 5130a1f1a0bSTejun Heo 5140a1f1a0bSTejun Heo /** 515100eeae2SJiri Slaby * tty_ldisc_wait_idle - wait for the ldisc to become idle 516100eeae2SJiri Slaby * @tty: tty to wait for 517df92d056SJiri Slaby * @timeout: for how long to wait at most 518100eeae2SJiri Slaby * 519100eeae2SJiri Slaby * Wait for the line discipline to become idle. The discipline must 520100eeae2SJiri Slaby * have been halted for this to guarantee it remains idle. 521100eeae2SJiri Slaby */ 522df92d056SJiri Slaby static int tty_ldisc_wait_idle(struct tty_struct *tty, long timeout) 523100eeae2SJiri Slaby { 524df92d056SJiri Slaby long ret; 5251541f845SIvo Sieben ret = wait_event_timeout(tty->ldisc->wq_idle, 526df92d056SJiri Slaby atomic_read(&tty->ldisc->users) == 1, timeout); 527100eeae2SJiri Slaby return ret > 0 ? 0 : -EBUSY; 528100eeae2SJiri Slaby } 529100eeae2SJiri Slaby 530100eeae2SJiri Slaby /** 53111cf48eaSPeter Hurley * tty_ldisc_halt - shut down the line discipline 53211cf48eaSPeter Hurley * @tty: tty device 53311cf48eaSPeter Hurley * 53411cf48eaSPeter Hurley * Shut down the line discipline and work queue for this tty device. 53511cf48eaSPeter Hurley * The TTY_LDISC flag being cleared ensures no further references can 53611cf48eaSPeter Hurley * be obtained while the delayed work queue halt ensures that no more 53711cf48eaSPeter Hurley * data is fed to the ldisc. 53811cf48eaSPeter Hurley * 53911cf48eaSPeter Hurley * You need to do a 'flush_scheduled_work()' (outside the ldisc_mutex) 54011cf48eaSPeter Hurley * in order to make sure any currently executing ldisc work is also 54111cf48eaSPeter Hurley * flushed. 54211cf48eaSPeter Hurley */ 54311cf48eaSPeter Hurley 54411cf48eaSPeter Hurley static int tty_ldisc_halt(struct tty_struct *tty) 54511cf48eaSPeter Hurley { 54611cf48eaSPeter Hurley int scheduled; 54711cf48eaSPeter Hurley clear_bit(TTY_LDISC, &tty->flags); 54811cf48eaSPeter Hurley scheduled = cancel_work_sync(&tty->port->buf.work); 54911cf48eaSPeter Hurley set_bit(TTY_LDISC_HALTED, &tty->flags); 55011cf48eaSPeter Hurley return scheduled; 55111cf48eaSPeter Hurley } 55211cf48eaSPeter Hurley 55311cf48eaSPeter Hurley /** 55476bc35e7SPeter Hurley * tty_ldisc_hangup_halt - halt the line discipline for hangup 55576bc35e7SPeter Hurley * @tty: tty being hung up 556168942c9SPeter Hurley * 55776bc35e7SPeter Hurley * Shut down the line discipline and work queue for the tty device 55876bc35e7SPeter Hurley * being hungup. Clear the TTY_LDISC flag to ensure no further 55976bc35e7SPeter Hurley * references can be obtained, wait for remaining references to be 56076bc35e7SPeter Hurley * released, and cancel pending buffer work to ensure no more 56176bc35e7SPeter Hurley * data is fed to this ldisc. 562168942c9SPeter Hurley * Caller must hold legacy and ->ldisc_mutex. 5632276ad97SPeter Hurley * 5642276ad97SPeter Hurley * NB: tty_set_ldisc() is prevented from changing the ldisc concurrently 5652276ad97SPeter Hurley * with this function by checking the TTY_HUPPING flag. 56676bc35e7SPeter Hurley * 56776bc35e7SPeter Hurley * NB: if tty->ldisc is NULL then buffer work does not need to be 56876bc35e7SPeter Hurley * cancelled because it must already have done as a precondition 56976bc35e7SPeter Hurley * of closing the ldisc and setting tty->ldisc to NULL 570168942c9SPeter Hurley */ 57176bc35e7SPeter Hurley static bool tty_ldisc_hangup_halt(struct tty_struct *tty) 572168942c9SPeter Hurley { 573168942c9SPeter Hurley char cur_n[TASK_COMM_LEN], tty_n[64]; 574168942c9SPeter Hurley long timeout = 3 * HZ; 5752276ad97SPeter Hurley 57676bc35e7SPeter Hurley clear_bit(TTY_LDISC, &tty->flags); 57776bc35e7SPeter Hurley 5782276ad97SPeter Hurley if (tty->ldisc) { /* Not yet closed */ 579168942c9SPeter Hurley tty_unlock(tty); 580168942c9SPeter Hurley 581168942c9SPeter Hurley while (tty_ldisc_wait_idle(tty, timeout) == -EBUSY) { 582168942c9SPeter Hurley timeout = MAX_SCHEDULE_TIMEOUT; 583168942c9SPeter Hurley printk_ratelimited(KERN_WARNING 584168942c9SPeter Hurley "%s: waiting (%s) for %s took too long, but we keep waiting...\n", 585168942c9SPeter Hurley __func__, get_task_comm(cur_n, current), 586168942c9SPeter Hurley tty_name(tty, tty_n)); 587168942c9SPeter Hurley } 58876bc35e7SPeter Hurley 58976bc35e7SPeter Hurley cancel_work_sync(&tty->port->buf.work); 59076bc35e7SPeter Hurley set_bit(TTY_LDISC_HALTED, &tty->flags); 59176bc35e7SPeter Hurley 592168942c9SPeter Hurley /* must reacquire both locks and preserve lock order */ 593168942c9SPeter Hurley mutex_unlock(&tty->ldisc_mutex); 594168942c9SPeter Hurley tty_lock(tty); 595168942c9SPeter Hurley mutex_lock(&tty->ldisc_mutex); 596168942c9SPeter Hurley } 597168942c9SPeter Hurley return !!tty->ldisc; 598168942c9SPeter Hurley } 599168942c9SPeter Hurley 600168942c9SPeter Hurley /** 60196fd7ce5SGreg Kroah-Hartman * tty_set_ldisc - set line discipline 60296fd7ce5SGreg Kroah-Hartman * @tty: the terminal to set 60396fd7ce5SGreg Kroah-Hartman * @ldisc: the line discipline 60496fd7ce5SGreg Kroah-Hartman * 60596fd7ce5SGreg Kroah-Hartman * Set the discipline of a tty line. Must be called from a process 60696fd7ce5SGreg Kroah-Hartman * context. The ldisc change logic has to protect itself against any 60796fd7ce5SGreg Kroah-Hartman * overlapping ldisc change (including on the other end of pty pairs), 60896fd7ce5SGreg Kroah-Hartman * the close of one side of a tty/pty pair, and eventually hangup. 60996fd7ce5SGreg Kroah-Hartman * 61096fd7ce5SGreg Kroah-Hartman * Locking: takes tty_ldisc_lock, termios_mutex 61196fd7ce5SGreg Kroah-Hartman */ 61296fd7ce5SGreg Kroah-Hartman 61396fd7ce5SGreg Kroah-Hartman int tty_set_ldisc(struct tty_struct *tty, int ldisc) 61496fd7ce5SGreg Kroah-Hartman { 61596fd7ce5SGreg Kroah-Hartman int retval; 61696fd7ce5SGreg Kroah-Hartman struct tty_ldisc *o_ldisc, *new_ldisc; 61796fd7ce5SGreg Kroah-Hartman int work, o_work = 0; 61896fd7ce5SGreg Kroah-Hartman struct tty_struct *o_tty; 61996fd7ce5SGreg Kroah-Hartman 62096fd7ce5SGreg Kroah-Hartman new_ldisc = tty_ldisc_get(ldisc); 62196fd7ce5SGreg Kroah-Hartman if (IS_ERR(new_ldisc)) 62296fd7ce5SGreg Kroah-Hartman return PTR_ERR(new_ldisc); 62396fd7ce5SGreg Kroah-Hartman 62489c8d91eSAlan Cox tty_lock(tty); 62596fd7ce5SGreg Kroah-Hartman /* 62696fd7ce5SGreg Kroah-Hartman * We need to look at the tty locking here for pty/tty pairs 62796fd7ce5SGreg Kroah-Hartman * when both sides try to change in parallel. 62896fd7ce5SGreg Kroah-Hartman */ 62996fd7ce5SGreg Kroah-Hartman 63096fd7ce5SGreg Kroah-Hartman o_tty = tty->link; /* o_tty is the pty side or NULL */ 63196fd7ce5SGreg Kroah-Hartman 63296fd7ce5SGreg Kroah-Hartman 63396fd7ce5SGreg Kroah-Hartman /* 63496fd7ce5SGreg Kroah-Hartman * Check the no-op case 63596fd7ce5SGreg Kroah-Hartman */ 63696fd7ce5SGreg Kroah-Hartman 63796fd7ce5SGreg Kroah-Hartman if (tty->ldisc->ops->num == ldisc) { 63889c8d91eSAlan Cox tty_unlock(tty); 63996fd7ce5SGreg Kroah-Hartman tty_ldisc_put(new_ldisc); 64096fd7ce5SGreg Kroah-Hartman return 0; 64196fd7ce5SGreg Kroah-Hartman } 64296fd7ce5SGreg Kroah-Hartman 64389c8d91eSAlan Cox tty_unlock(tty); 64496fd7ce5SGreg Kroah-Hartman /* 64596fd7ce5SGreg Kroah-Hartman * Problem: What do we do if this blocks ? 64696fd7ce5SGreg Kroah-Hartman * We could deadlock here 64796fd7ce5SGreg Kroah-Hartman */ 64896fd7ce5SGreg Kroah-Hartman 64996fd7ce5SGreg Kroah-Hartman tty_wait_until_sent(tty, 0); 65096fd7ce5SGreg Kroah-Hartman 65189c8d91eSAlan Cox tty_lock(tty); 65296fd7ce5SGreg Kroah-Hartman mutex_lock(&tty->ldisc_mutex); 65396fd7ce5SGreg Kroah-Hartman 65496fd7ce5SGreg Kroah-Hartman /* 65596fd7ce5SGreg Kroah-Hartman * We could be midstream of another ldisc change which has 65696fd7ce5SGreg Kroah-Hartman * dropped the lock during processing. If so we need to wait. 65796fd7ce5SGreg Kroah-Hartman */ 65896fd7ce5SGreg Kroah-Hartman 65996fd7ce5SGreg Kroah-Hartman while (test_bit(TTY_LDISC_CHANGING, &tty->flags)) { 66096fd7ce5SGreg Kroah-Hartman mutex_unlock(&tty->ldisc_mutex); 66189c8d91eSAlan Cox tty_unlock(tty); 66296fd7ce5SGreg Kroah-Hartman wait_event(tty_ldisc_wait, 66396fd7ce5SGreg Kroah-Hartman test_bit(TTY_LDISC_CHANGING, &tty->flags) == 0); 66489c8d91eSAlan Cox tty_lock(tty); 66596fd7ce5SGreg Kroah-Hartman mutex_lock(&tty->ldisc_mutex); 66696fd7ce5SGreg Kroah-Hartman } 66796fd7ce5SGreg Kroah-Hartman 66896fd7ce5SGreg Kroah-Hartman set_bit(TTY_LDISC_CHANGING, &tty->flags); 66996fd7ce5SGreg Kroah-Hartman 67096fd7ce5SGreg Kroah-Hartman /* 67196fd7ce5SGreg Kroah-Hartman * No more input please, we are switching. The new ldisc 67296fd7ce5SGreg Kroah-Hartman * will update this value in the ldisc open function 67396fd7ce5SGreg Kroah-Hartman */ 67496fd7ce5SGreg Kroah-Hartman 67596fd7ce5SGreg Kroah-Hartman tty->receive_room = 0; 67696fd7ce5SGreg Kroah-Hartman 67796fd7ce5SGreg Kroah-Hartman o_ldisc = tty->ldisc; 67896fd7ce5SGreg Kroah-Hartman 67989c8d91eSAlan Cox tty_unlock(tty); 68096fd7ce5SGreg Kroah-Hartman /* 68196fd7ce5SGreg Kroah-Hartman * Make sure we don't change while someone holds a 68296fd7ce5SGreg Kroah-Hartman * reference to the line discipline. The TTY_LDISC bit 68396fd7ce5SGreg Kroah-Hartman * prevents anyone taking a reference once it is clear. 68496fd7ce5SGreg Kroah-Hartman * We need the lock to avoid racing reference takers. 68596fd7ce5SGreg Kroah-Hartman * 68696fd7ce5SGreg Kroah-Hartman * We must clear the TTY_LDISC bit here to avoid a livelock 68796fd7ce5SGreg Kroah-Hartman * with a userspace app continually trying to use the tty in 68896fd7ce5SGreg Kroah-Hartman * parallel to the change and re-referencing the tty. 68996fd7ce5SGreg Kroah-Hartman */ 69096fd7ce5SGreg Kroah-Hartman 69196fd7ce5SGreg Kroah-Hartman work = tty_ldisc_halt(tty); 69296fd7ce5SGreg Kroah-Hartman if (o_tty) 69396fd7ce5SGreg Kroah-Hartman o_work = tty_ldisc_halt(o_tty); 69496fd7ce5SGreg Kroah-Hartman 69596fd7ce5SGreg Kroah-Hartman /* 69696fd7ce5SGreg Kroah-Hartman * Wait for ->hangup_work and ->buf.work handlers to terminate. 69796fd7ce5SGreg Kroah-Hartman * We must drop the mutex here in case a hangup is also in process. 69896fd7ce5SGreg Kroah-Hartman */ 69996fd7ce5SGreg Kroah-Hartman 70096fd7ce5SGreg Kroah-Hartman mutex_unlock(&tty->ldisc_mutex); 70196fd7ce5SGreg Kroah-Hartman 7020a1f1a0bSTejun Heo tty_ldisc_flush_works(tty); 70396fd7ce5SGreg Kroah-Hartman 704df92d056SJiri Slaby retval = tty_ldisc_wait_idle(tty, 5 * HZ); 705100eeae2SJiri Slaby 70689c8d91eSAlan Cox tty_lock(tty); 70796fd7ce5SGreg Kroah-Hartman mutex_lock(&tty->ldisc_mutex); 708100eeae2SJiri Slaby 709100eeae2SJiri Slaby /* handle wait idle failure locked */ 710100eeae2SJiri Slaby if (retval) { 711100eeae2SJiri Slaby tty_ldisc_put(new_ldisc); 712100eeae2SJiri Slaby goto enable; 713100eeae2SJiri Slaby } 714100eeae2SJiri Slaby 71540c9f61eSShachar Shemesh if (test_bit(TTY_HUPPING, &tty->flags)) { 71696fd7ce5SGreg Kroah-Hartman /* We were raced by the hangup method. It will have stomped 71796fd7ce5SGreg Kroah-Hartman the ldisc data and closed the ldisc down */ 71896fd7ce5SGreg Kroah-Hartman clear_bit(TTY_LDISC_CHANGING, &tty->flags); 71996fd7ce5SGreg Kroah-Hartman mutex_unlock(&tty->ldisc_mutex); 72096fd7ce5SGreg Kroah-Hartman tty_ldisc_put(new_ldisc); 72189c8d91eSAlan Cox tty_unlock(tty); 72296fd7ce5SGreg Kroah-Hartman return -EIO; 72396fd7ce5SGreg Kroah-Hartman } 72496fd7ce5SGreg Kroah-Hartman 72596fd7ce5SGreg Kroah-Hartman /* Shutdown the current discipline. */ 72696fd7ce5SGreg Kroah-Hartman tty_ldisc_close(tty, o_ldisc); 72796fd7ce5SGreg Kroah-Hartman 72896fd7ce5SGreg Kroah-Hartman /* Now set up the new line discipline. */ 72996fd7ce5SGreg Kroah-Hartman tty_ldisc_assign(tty, new_ldisc); 73096fd7ce5SGreg Kroah-Hartman tty_set_termios_ldisc(tty, ldisc); 73196fd7ce5SGreg Kroah-Hartman 73296fd7ce5SGreg Kroah-Hartman retval = tty_ldisc_open(tty, new_ldisc); 73396fd7ce5SGreg Kroah-Hartman if (retval < 0) { 73496fd7ce5SGreg Kroah-Hartman /* Back to the old one or N_TTY if we can't */ 73596fd7ce5SGreg Kroah-Hartman tty_ldisc_put(new_ldisc); 73696fd7ce5SGreg Kroah-Hartman tty_ldisc_restore(tty, o_ldisc); 73796fd7ce5SGreg Kroah-Hartman } 73896fd7ce5SGreg Kroah-Hartman 73996fd7ce5SGreg Kroah-Hartman /* At this point we hold a reference to the new ldisc and a 74096fd7ce5SGreg Kroah-Hartman a reference to the old ldisc. If we ended up flipping back 74196fd7ce5SGreg Kroah-Hartman to the existing ldisc we have two references to it */ 74296fd7ce5SGreg Kroah-Hartman 74396fd7ce5SGreg Kroah-Hartman if (tty->ldisc->ops->num != o_ldisc->ops->num && tty->ops->set_ldisc) 74496fd7ce5SGreg Kroah-Hartman tty->ops->set_ldisc(tty); 74596fd7ce5SGreg Kroah-Hartman 74696fd7ce5SGreg Kroah-Hartman tty_ldisc_put(o_ldisc); 74796fd7ce5SGreg Kroah-Hartman 748100eeae2SJiri Slaby enable: 74996fd7ce5SGreg Kroah-Hartman /* 75096fd7ce5SGreg Kroah-Hartman * Allow ldisc referencing to occur again 75196fd7ce5SGreg Kroah-Hartman */ 75296fd7ce5SGreg Kroah-Hartman 75396fd7ce5SGreg Kroah-Hartman tty_ldisc_enable(tty); 75496fd7ce5SGreg Kroah-Hartman if (o_tty) 75596fd7ce5SGreg Kroah-Hartman tty_ldisc_enable(o_tty); 75696fd7ce5SGreg Kroah-Hartman 75796fd7ce5SGreg Kroah-Hartman /* Restart the work queue in case no characters kick it off. Safe if 75896fd7ce5SGreg Kroah-Hartman already running */ 75996fd7ce5SGreg Kroah-Hartman if (work) 760ecbbfd44SJiri Slaby schedule_work(&tty->port->buf.work); 76196fd7ce5SGreg Kroah-Hartman if (o_work) 762ecbbfd44SJiri Slaby schedule_work(&o_tty->port->buf.work); 76396fd7ce5SGreg Kroah-Hartman mutex_unlock(&tty->ldisc_mutex); 76489c8d91eSAlan Cox tty_unlock(tty); 76596fd7ce5SGreg Kroah-Hartman return retval; 76696fd7ce5SGreg Kroah-Hartman } 76796fd7ce5SGreg Kroah-Hartman 76896fd7ce5SGreg Kroah-Hartman /** 76996fd7ce5SGreg Kroah-Hartman * tty_reset_termios - reset terminal state 77096fd7ce5SGreg Kroah-Hartman * @tty: tty to reset 77196fd7ce5SGreg Kroah-Hartman * 77296fd7ce5SGreg Kroah-Hartman * Restore a terminal to the driver default state. 77396fd7ce5SGreg Kroah-Hartman */ 77496fd7ce5SGreg Kroah-Hartman 77596fd7ce5SGreg Kroah-Hartman static void tty_reset_termios(struct tty_struct *tty) 77696fd7ce5SGreg Kroah-Hartman { 77796fd7ce5SGreg Kroah-Hartman mutex_lock(&tty->termios_mutex); 778adc8d746SAlan Cox tty->termios = tty->driver->init_termios; 779adc8d746SAlan Cox tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios); 780adc8d746SAlan Cox tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios); 78196fd7ce5SGreg Kroah-Hartman mutex_unlock(&tty->termios_mutex); 78296fd7ce5SGreg Kroah-Hartman } 78396fd7ce5SGreg Kroah-Hartman 78496fd7ce5SGreg Kroah-Hartman 78596fd7ce5SGreg Kroah-Hartman /** 78696fd7ce5SGreg Kroah-Hartman * tty_ldisc_reinit - reinitialise the tty ldisc 78796fd7ce5SGreg Kroah-Hartman * @tty: tty to reinit 78896fd7ce5SGreg Kroah-Hartman * @ldisc: line discipline to reinitialize 78996fd7ce5SGreg Kroah-Hartman * 79096fd7ce5SGreg Kroah-Hartman * Switch the tty to a line discipline and leave the ldisc 79196fd7ce5SGreg Kroah-Hartman * state closed 79296fd7ce5SGreg Kroah-Hartman */ 79396fd7ce5SGreg Kroah-Hartman 7941c95ba1eSPhilippe Rétornaz static int tty_ldisc_reinit(struct tty_struct *tty, int ldisc) 79596fd7ce5SGreg Kroah-Hartman { 7961c95ba1eSPhilippe Rétornaz struct tty_ldisc *ld = tty_ldisc_get(ldisc); 7971c95ba1eSPhilippe Rétornaz 7981c95ba1eSPhilippe Rétornaz if (IS_ERR(ld)) 7991c95ba1eSPhilippe Rétornaz return -1; 80096fd7ce5SGreg Kroah-Hartman 80196fd7ce5SGreg Kroah-Hartman tty_ldisc_close(tty, tty->ldisc); 80296fd7ce5SGreg Kroah-Hartman tty_ldisc_put(tty->ldisc); 80396fd7ce5SGreg Kroah-Hartman tty->ldisc = NULL; 80496fd7ce5SGreg Kroah-Hartman /* 80596fd7ce5SGreg Kroah-Hartman * Switch the line discipline back 80696fd7ce5SGreg Kroah-Hartman */ 80796fd7ce5SGreg Kroah-Hartman tty_ldisc_assign(tty, ld); 80896fd7ce5SGreg Kroah-Hartman tty_set_termios_ldisc(tty, ldisc); 8091c95ba1eSPhilippe Rétornaz 8101c95ba1eSPhilippe Rétornaz return 0; 81196fd7ce5SGreg Kroah-Hartman } 81296fd7ce5SGreg Kroah-Hartman 81396fd7ce5SGreg Kroah-Hartman /** 81496fd7ce5SGreg Kroah-Hartman * tty_ldisc_hangup - hangup ldisc reset 81596fd7ce5SGreg Kroah-Hartman * @tty: tty being hung up 81696fd7ce5SGreg Kroah-Hartman * 81796fd7ce5SGreg Kroah-Hartman * Some tty devices reset their termios when they receive a hangup 81896fd7ce5SGreg Kroah-Hartman * event. In that situation we must also switch back to N_TTY properly 81996fd7ce5SGreg Kroah-Hartman * before we reset the termios data. 82096fd7ce5SGreg Kroah-Hartman * 82196fd7ce5SGreg Kroah-Hartman * Locking: We can take the ldisc mutex as the rest of the code is 82296fd7ce5SGreg Kroah-Hartman * careful to allow for this. 82396fd7ce5SGreg Kroah-Hartman * 82496fd7ce5SGreg Kroah-Hartman * In the pty pair case this occurs in the close() path of the 82596fd7ce5SGreg Kroah-Hartman * tty itself so we must be careful about locking rules. 82696fd7ce5SGreg Kroah-Hartman */ 82796fd7ce5SGreg Kroah-Hartman 82896fd7ce5SGreg Kroah-Hartman void tty_ldisc_hangup(struct tty_struct *tty) 82996fd7ce5SGreg Kroah-Hartman { 83096fd7ce5SGreg Kroah-Hartman struct tty_ldisc *ld; 83196fd7ce5SGreg Kroah-Hartman int reset = tty->driver->flags & TTY_DRIVER_RESET_TERMIOS; 83296fd7ce5SGreg Kroah-Hartman int err = 0; 83396fd7ce5SGreg Kroah-Hartman 83496fd7ce5SGreg Kroah-Hartman /* 83596fd7ce5SGreg Kroah-Hartman * FIXME! What are the locking issues here? This may me overdoing 83696fd7ce5SGreg Kroah-Hartman * things... This question is especially important now that we've 83796fd7ce5SGreg Kroah-Hartman * removed the irqlock. 83896fd7ce5SGreg Kroah-Hartman */ 83996fd7ce5SGreg Kroah-Hartman ld = tty_ldisc_ref(tty); 84096fd7ce5SGreg Kroah-Hartman if (ld != NULL) { 84196fd7ce5SGreg Kroah-Hartman /* We may have no line discipline at this point */ 84296fd7ce5SGreg Kroah-Hartman if (ld->ops->flush_buffer) 84396fd7ce5SGreg Kroah-Hartman ld->ops->flush_buffer(tty); 84496fd7ce5SGreg Kroah-Hartman tty_driver_flush_buffer(tty); 84596fd7ce5SGreg Kroah-Hartman if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) && 84696fd7ce5SGreg Kroah-Hartman ld->ops->write_wakeup) 84796fd7ce5SGreg Kroah-Hartman ld->ops->write_wakeup(tty); 84896fd7ce5SGreg Kroah-Hartman if (ld->ops->hangup) 84996fd7ce5SGreg Kroah-Hartman ld->ops->hangup(tty); 85096fd7ce5SGreg Kroah-Hartman tty_ldisc_deref(ld); 85196fd7ce5SGreg Kroah-Hartman } 85296fd7ce5SGreg Kroah-Hartman /* 85396fd7ce5SGreg Kroah-Hartman * FIXME: Once we trust the LDISC code better we can wait here for 85496fd7ce5SGreg Kroah-Hartman * ldisc completion and fix the driver call race 85596fd7ce5SGreg Kroah-Hartman */ 85696fd7ce5SGreg Kroah-Hartman wake_up_interruptible_poll(&tty->write_wait, POLLOUT); 85796fd7ce5SGreg Kroah-Hartman wake_up_interruptible_poll(&tty->read_wait, POLLIN); 85896fd7ce5SGreg Kroah-Hartman /* 85996fd7ce5SGreg Kroah-Hartman * Shutdown the current line discipline, and reset it to 86096fd7ce5SGreg Kroah-Hartman * N_TTY if need be. 86196fd7ce5SGreg Kroah-Hartman * 86296fd7ce5SGreg Kroah-Hartman * Avoid racing set_ldisc or tty_ldisc_release 86396fd7ce5SGreg Kroah-Hartman */ 86496fd7ce5SGreg Kroah-Hartman mutex_lock(&tty->ldisc_mutex); 86596fd7ce5SGreg Kroah-Hartman 86696fd7ce5SGreg Kroah-Hartman /* At this point we have a closed ldisc and we want to 86796fd7ce5SGreg Kroah-Hartman reopen it. We could defer this to the next open but 86896fd7ce5SGreg Kroah-Hartman it means auditing a lot of other paths so this is 86996fd7ce5SGreg Kroah-Hartman a FIXME */ 87076bc35e7SPeter Hurley if (tty_ldisc_hangup_halt(tty)) { 87196fd7ce5SGreg Kroah-Hartman if (reset == 0) { 8721c95ba1eSPhilippe Rétornaz 873adc8d746SAlan Cox if (!tty_ldisc_reinit(tty, tty->termios.c_line)) 87496fd7ce5SGreg Kroah-Hartman err = tty_ldisc_open(tty, tty->ldisc); 8751c95ba1eSPhilippe Rétornaz else 8761c95ba1eSPhilippe Rétornaz err = 1; 87796fd7ce5SGreg Kroah-Hartman } 87896fd7ce5SGreg Kroah-Hartman /* If the re-open fails or we reset then go to N_TTY. The 87996fd7ce5SGreg Kroah-Hartman N_TTY open cannot fail */ 88096fd7ce5SGreg Kroah-Hartman if (reset || err) { 8811c95ba1eSPhilippe Rétornaz BUG_ON(tty_ldisc_reinit(tty, N_TTY)); 88296fd7ce5SGreg Kroah-Hartman WARN_ON(tty_ldisc_open(tty, tty->ldisc)); 88396fd7ce5SGreg Kroah-Hartman } 88496fd7ce5SGreg Kroah-Hartman tty_ldisc_enable(tty); 88596fd7ce5SGreg Kroah-Hartman } 88696fd7ce5SGreg Kroah-Hartman mutex_unlock(&tty->ldisc_mutex); 88796fd7ce5SGreg Kroah-Hartman if (reset) 88896fd7ce5SGreg Kroah-Hartman tty_reset_termios(tty); 88996fd7ce5SGreg Kroah-Hartman } 89096fd7ce5SGreg Kroah-Hartman 89196fd7ce5SGreg Kroah-Hartman /** 89296fd7ce5SGreg Kroah-Hartman * tty_ldisc_setup - open line discipline 89396fd7ce5SGreg Kroah-Hartman * @tty: tty being shut down 89496fd7ce5SGreg Kroah-Hartman * @o_tty: pair tty for pty/tty pairs 89596fd7ce5SGreg Kroah-Hartman * 89696fd7ce5SGreg Kroah-Hartman * Called during the initial open of a tty/pty pair in order to set up the 89796fd7ce5SGreg Kroah-Hartman * line disciplines and bind them to the tty. This has no locking issues 89896fd7ce5SGreg Kroah-Hartman * as the device isn't yet active. 89996fd7ce5SGreg Kroah-Hartman */ 90096fd7ce5SGreg Kroah-Hartman 90196fd7ce5SGreg Kroah-Hartman int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty) 90296fd7ce5SGreg Kroah-Hartman { 90396fd7ce5SGreg Kroah-Hartman struct tty_ldisc *ld = tty->ldisc; 90496fd7ce5SGreg Kroah-Hartman int retval; 90596fd7ce5SGreg Kroah-Hartman 90696fd7ce5SGreg Kroah-Hartman retval = tty_ldisc_open(tty, ld); 90796fd7ce5SGreg Kroah-Hartman if (retval) 90896fd7ce5SGreg Kroah-Hartman return retval; 90996fd7ce5SGreg Kroah-Hartman 91096fd7ce5SGreg Kroah-Hartman if (o_tty) { 91196fd7ce5SGreg Kroah-Hartman retval = tty_ldisc_open(o_tty, o_tty->ldisc); 91296fd7ce5SGreg Kroah-Hartman if (retval) { 91396fd7ce5SGreg Kroah-Hartman tty_ldisc_close(tty, ld); 91496fd7ce5SGreg Kroah-Hartman return retval; 91596fd7ce5SGreg Kroah-Hartman } 91696fd7ce5SGreg Kroah-Hartman tty_ldisc_enable(o_tty); 91796fd7ce5SGreg Kroah-Hartman } 91896fd7ce5SGreg Kroah-Hartman tty_ldisc_enable(tty); 91996fd7ce5SGreg Kroah-Hartman return 0; 92096fd7ce5SGreg Kroah-Hartman } 92189c8d91eSAlan Cox 92289c8d91eSAlan Cox static void tty_ldisc_kill(struct tty_struct *tty) 92389c8d91eSAlan Cox { 92431e12128SJiri Slaby /* There cannot be users from userspace now. But there still might be 92531e12128SJiri Slaby * drivers holding a reference via tty_ldisc_ref. Do not steal them the 92631e12128SJiri Slaby * ldisc until they are done. */ 92731e12128SJiri Slaby tty_ldisc_wait_idle(tty, MAX_SCHEDULE_TIMEOUT); 92831e12128SJiri Slaby 92989c8d91eSAlan Cox mutex_lock(&tty->ldisc_mutex); 93089c8d91eSAlan Cox /* 93189c8d91eSAlan Cox * Now kill off the ldisc 93289c8d91eSAlan Cox */ 93389c8d91eSAlan Cox tty_ldisc_close(tty, tty->ldisc); 93489c8d91eSAlan Cox tty_ldisc_put(tty->ldisc); 93589c8d91eSAlan Cox /* Force an oops if we mess this up */ 93689c8d91eSAlan Cox tty->ldisc = NULL; 93789c8d91eSAlan Cox 93889c8d91eSAlan Cox /* Ensure the next open requests the N_TTY ldisc */ 93989c8d91eSAlan Cox tty_set_termios_ldisc(tty, N_TTY); 94089c8d91eSAlan Cox mutex_unlock(&tty->ldisc_mutex); 94189c8d91eSAlan Cox } 94289c8d91eSAlan Cox 94396fd7ce5SGreg Kroah-Hartman /** 94496fd7ce5SGreg Kroah-Hartman * tty_ldisc_release - release line discipline 94596fd7ce5SGreg Kroah-Hartman * @tty: tty being shut down 94696fd7ce5SGreg Kroah-Hartman * @o_tty: pair tty for pty/tty pairs 94796fd7ce5SGreg Kroah-Hartman * 94896fd7ce5SGreg Kroah-Hartman * Called during the final close of a tty/pty pair in order to shut down 94996fd7ce5SGreg Kroah-Hartman * the line discpline layer. On exit the ldisc assigned is N_TTY and the 95096fd7ce5SGreg Kroah-Hartman * ldisc has not been opened. 95196fd7ce5SGreg Kroah-Hartman */ 95296fd7ce5SGreg Kroah-Hartman 95396fd7ce5SGreg Kroah-Hartman void tty_ldisc_release(struct tty_struct *tty, struct tty_struct *o_tty) 95496fd7ce5SGreg Kroah-Hartman { 95596fd7ce5SGreg Kroah-Hartman /* 95696fd7ce5SGreg Kroah-Hartman * Prevent flush_to_ldisc() from rescheduling the work for later. Then 95796fd7ce5SGreg Kroah-Hartman * kill any delayed work. As this is the final close it does not 95896fd7ce5SGreg Kroah-Hartman * race with the set_ldisc code path. 95996fd7ce5SGreg Kroah-Hartman */ 96096fd7ce5SGreg Kroah-Hartman 96196fd7ce5SGreg Kroah-Hartman tty_ldisc_halt(tty); 962852e4a81SSebastian Andrzej Siewior if (o_tty) 96389c8d91eSAlan Cox tty_ldisc_halt(o_tty); 964d155255aSAlan Cox 965852e4a81SSebastian Andrzej Siewior tty_ldisc_flush_works(tty); 966852e4a81SSebastian Andrzej Siewior if (o_tty) 967852e4a81SSebastian Andrzej Siewior tty_ldisc_flush_works(o_tty); 968852e4a81SSebastian Andrzej Siewior 969852e4a81SSebastian Andrzej Siewior tty_lock_pair(tty, o_tty); 9706d31a88cSAlan Cox /* This will need doing differently if we need to lock */ 97189c8d91eSAlan Cox tty_ldisc_kill(tty); 97289c8d91eSAlan Cox if (o_tty) 97389c8d91eSAlan Cox tty_ldisc_kill(o_tty); 97489c8d91eSAlan Cox 97589c8d91eSAlan Cox tty_unlock_pair(tty, o_tty); 97696fd7ce5SGreg Kroah-Hartman /* And the memory resources remaining (buffers, termios) will be 97796fd7ce5SGreg Kroah-Hartman disposed of when the kref hits zero */ 97896fd7ce5SGreg Kroah-Hartman } 97996fd7ce5SGreg Kroah-Hartman 98096fd7ce5SGreg Kroah-Hartman /** 98196fd7ce5SGreg Kroah-Hartman * tty_ldisc_init - ldisc setup for new tty 98296fd7ce5SGreg Kroah-Hartman * @tty: tty being allocated 98396fd7ce5SGreg Kroah-Hartman * 98496fd7ce5SGreg Kroah-Hartman * Set up the line discipline objects for a newly allocated tty. Note that 98596fd7ce5SGreg Kroah-Hartman * the tty structure is not completely set up when this call is made. 98696fd7ce5SGreg Kroah-Hartman */ 98796fd7ce5SGreg Kroah-Hartman 98896fd7ce5SGreg Kroah-Hartman void tty_ldisc_init(struct tty_struct *tty) 98996fd7ce5SGreg Kroah-Hartman { 99096fd7ce5SGreg Kroah-Hartman struct tty_ldisc *ld = tty_ldisc_get(N_TTY); 99196fd7ce5SGreg Kroah-Hartman if (IS_ERR(ld)) 99296fd7ce5SGreg Kroah-Hartman panic("n_tty: init_tty"); 99396fd7ce5SGreg Kroah-Hartman tty_ldisc_assign(tty, ld); 99496fd7ce5SGreg Kroah-Hartman } 99596fd7ce5SGreg Kroah-Hartman 9966716671dSJiri Slaby /** 9976716671dSJiri Slaby * tty_ldisc_init - ldisc cleanup for new tty 9986716671dSJiri Slaby * @tty: tty that was allocated recently 9996716671dSJiri Slaby * 10006716671dSJiri Slaby * The tty structure must not becompletely set up (tty_ldisc_setup) when 10016716671dSJiri Slaby * this call is made. 10026716671dSJiri Slaby */ 10036716671dSJiri Slaby void tty_ldisc_deinit(struct tty_struct *tty) 10046716671dSJiri Slaby { 10056716671dSJiri Slaby put_ldisc(tty->ldisc); 10066716671dSJiri Slaby tty_ldisc_assign(tty, NULL); 10076716671dSJiri Slaby } 10086716671dSJiri Slaby 100996fd7ce5SGreg Kroah-Hartman void tty_ldisc_begin(void) 101096fd7ce5SGreg Kroah-Hartman { 101196fd7ce5SGreg Kroah-Hartman /* Setup the default TTY line discipline. */ 101296fd7ce5SGreg Kroah-Hartman (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY); 101396fd7ce5SGreg Kroah-Hartman } 1014