| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 1 | #include <linux/types.h> | 
 | 2 | #include <linux/major.h> | 
 | 3 | #include <linux/errno.h> | 
 | 4 | #include <linux/signal.h> | 
 | 5 | #include <linux/fcntl.h> | 
 | 6 | #include <linux/sched.h> | 
 | 7 | #include <linux/interrupt.h> | 
 | 8 | #include <linux/tty.h> | 
 | 9 | #include <linux/tty_driver.h> | 
 | 10 | #include <linux/tty_flip.h> | 
 | 11 | #include <linux/devpts_fs.h> | 
 | 12 | #include <linux/file.h> | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 13 | #include <linux/console.h> | 
 | 14 | #include <linux/timer.h> | 
 | 15 | #include <linux/ctype.h> | 
 | 16 | #include <linux/kd.h> | 
 | 17 | #include <linux/mm.h> | 
 | 18 | #include <linux/string.h> | 
 | 19 | #include <linux/slab.h> | 
 | 20 | #include <linux/poll.h> | 
 | 21 | #include <linux/proc_fs.h> | 
 | 22 | #include <linux/init.h> | 
 | 23 | #include <linux/module.h> | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 24 | #include <linux/device.h> | 
 | 25 | #include <linux/wait.h> | 
 | 26 | #include <linux/bitops.h> | 
 | 27 | #include <linux/delay.h> | 
 | 28 | #include <linux/seq_file.h> | 
 | 29 |  | 
 | 30 | #include <linux/uaccess.h> | 
 | 31 | #include <asm/system.h> | 
 | 32 |  | 
 | 33 | #include <linux/kbd_kern.h> | 
 | 34 | #include <linux/vt_kern.h> | 
 | 35 | #include <linux/selection.h> | 
 | 36 |  | 
 | 37 | #include <linux/kmod.h> | 
 | 38 | #include <linux/nsproxy.h> | 
 | 39 |  | 
 | 40 | /* | 
 | 41 |  *	This guards the refcounted line discipline lists. The lock | 
 | 42 |  *	must be taken with irqs off because there are hangup path | 
 | 43 |  *	callers who will do ldisc lookups and cannot sleep. | 
 | 44 |  */ | 
 | 45 |  | 
 | 46 | static DEFINE_SPINLOCK(tty_ldisc_lock); | 
 | 47 | static DECLARE_WAIT_QUEUE_HEAD(tty_ldisc_wait); | 
 | 48 | /* Line disc dispatch table */ | 
 | 49 | static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS]; | 
 | 50 |  | 
| Linus Torvalds | 65b7704 | 2009-08-03 11:11:19 -0700 | [diff] [blame] | 51 | static inline struct tty_ldisc *get_ldisc(struct tty_ldisc *ld) | 
 | 52 | { | 
 | 53 | 	if (ld) | 
 | 54 | 		atomic_inc(&ld->users); | 
 | 55 | 	return ld; | 
 | 56 | } | 
 | 57 |  | 
| Linus Torvalds | cbe9352 | 2009-08-03 14:54:56 -0700 | [diff] [blame] | 58 | static void put_ldisc(struct tty_ldisc *ld) | 
| Linus Torvalds | 65b7704 | 2009-08-03 11:11:19 -0700 | [diff] [blame] | 59 | { | 
| Linus Torvalds | cbe9352 | 2009-08-03 14:54:56 -0700 | [diff] [blame] | 60 | 	unsigned long flags; | 
 | 61 |  | 
| Linus Torvalds | 65b7704 | 2009-08-03 11:11:19 -0700 | [diff] [blame] | 62 | 	if (WARN_ON_ONCE(!ld)) | 
 | 63 | 		return; | 
 | 64 |  | 
 | 65 | 	/* | 
 | 66 | 	 * If this is the last user, free the ldisc, and | 
 | 67 | 	 * release the ldisc ops. | 
| Linus Torvalds | cbe9352 | 2009-08-03 14:54:56 -0700 | [diff] [blame] | 68 | 	 * | 
 | 69 | 	 * We really want an "atomic_dec_and_lock_irqsave()", | 
 | 70 | 	 * but we don't have it, so this does it by hand. | 
| Linus Torvalds | 65b7704 | 2009-08-03 11:11:19 -0700 | [diff] [blame] | 71 | 	 */ | 
| Linus Torvalds | cbe9352 | 2009-08-03 14:54:56 -0700 | [diff] [blame] | 72 | 	local_irq_save(flags); | 
 | 73 | 	if (atomic_dec_and_lock(&ld->users, &tty_ldisc_lock)) { | 
| Linus Torvalds | 65b7704 | 2009-08-03 11:11:19 -0700 | [diff] [blame] | 74 | 		struct tty_ldisc_ops *ldo = ld->ops; | 
 | 75 |  | 
| Linus Torvalds | 65b7704 | 2009-08-03 11:11:19 -0700 | [diff] [blame] | 76 | 		ldo->refcount--; | 
 | 77 | 		module_put(ldo->owner); | 
 | 78 | 		spin_unlock_irqrestore(&tty_ldisc_lock, flags); | 
| Linus Torvalds | cbe9352 | 2009-08-03 14:54:56 -0700 | [diff] [blame] | 79 |  | 
 | 80 | 		kfree(ld); | 
 | 81 | 		return; | 
| Linus Torvalds | 65b7704 | 2009-08-03 11:11:19 -0700 | [diff] [blame] | 82 | 	} | 
| Linus Torvalds | cbe9352 | 2009-08-03 14:54:56 -0700 | [diff] [blame] | 83 | 	local_irq_restore(flags); | 
| Linus Torvalds | 65b7704 | 2009-08-03 11:11:19 -0700 | [diff] [blame] | 84 | } | 
 | 85 |  | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 86 | /** | 
 | 87 |  *	tty_register_ldisc	-	install a line discipline | 
 | 88 |  *	@disc: ldisc number | 
 | 89 |  *	@new_ldisc: pointer to the ldisc object | 
 | 90 |  * | 
 | 91 |  *	Installs a new line discipline into the kernel. The discipline | 
 | 92 |  *	is set up as unreferenced and then made available to the kernel | 
 | 93 |  *	from this point onwards. | 
 | 94 |  * | 
 | 95 |  *	Locking: | 
 | 96 |  *		takes tty_ldisc_lock to guard against ldisc races | 
 | 97 |  */ | 
 | 98 |  | 
 | 99 | int tty_register_ldisc(int disc, struct tty_ldisc_ops *new_ldisc) | 
 | 100 | { | 
 | 101 | 	unsigned long flags; | 
 | 102 | 	int ret = 0; | 
 | 103 |  | 
 | 104 | 	if (disc < N_TTY || disc >= NR_LDISCS) | 
 | 105 | 		return -EINVAL; | 
 | 106 |  | 
 | 107 | 	spin_lock_irqsave(&tty_ldisc_lock, flags); | 
 | 108 | 	tty_ldiscs[disc] = new_ldisc; | 
 | 109 | 	new_ldisc->num = disc; | 
 | 110 | 	new_ldisc->refcount = 0; | 
 | 111 | 	spin_unlock_irqrestore(&tty_ldisc_lock, flags); | 
 | 112 |  | 
 | 113 | 	return ret; | 
 | 114 | } | 
 | 115 | EXPORT_SYMBOL(tty_register_ldisc); | 
 | 116 |  | 
 | 117 | /** | 
 | 118 |  *	tty_unregister_ldisc	-	unload a line discipline | 
 | 119 |  *	@disc: ldisc number | 
 | 120 |  *	@new_ldisc: pointer to the ldisc object | 
 | 121 |  * | 
 | 122 |  *	Remove a line discipline from the kernel providing it is not | 
 | 123 |  *	currently in use. | 
 | 124 |  * | 
 | 125 |  *	Locking: | 
 | 126 |  *		takes tty_ldisc_lock to guard against ldisc races | 
 | 127 |  */ | 
 | 128 |  | 
 | 129 | int tty_unregister_ldisc(int disc) | 
 | 130 | { | 
 | 131 | 	unsigned long flags; | 
 | 132 | 	int ret = 0; | 
 | 133 |  | 
 | 134 | 	if (disc < N_TTY || disc >= NR_LDISCS) | 
 | 135 | 		return -EINVAL; | 
 | 136 |  | 
 | 137 | 	spin_lock_irqsave(&tty_ldisc_lock, flags); | 
 | 138 | 	if (tty_ldiscs[disc]->refcount) | 
 | 139 | 		ret = -EBUSY; | 
 | 140 | 	else | 
 | 141 | 		tty_ldiscs[disc] = NULL; | 
 | 142 | 	spin_unlock_irqrestore(&tty_ldisc_lock, flags); | 
 | 143 |  | 
 | 144 | 	return ret; | 
 | 145 | } | 
 | 146 | EXPORT_SYMBOL(tty_unregister_ldisc); | 
 | 147 |  | 
 | 148 |  | 
 | 149 | /** | 
 | 150 |  *	tty_ldisc_try_get	-	try and reference an ldisc | 
 | 151 |  *	@disc: ldisc number | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 152 |  * | 
 | 153 |  *	Attempt to open and lock a line discipline into place. Return | 
| Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 154 |  *	the line discipline refcounted or an error. | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 155 |  */ | 
 | 156 |  | 
| Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 157 | static struct tty_ldisc *tty_ldisc_try_get(int disc) | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 158 | { | 
 | 159 | 	unsigned long flags; | 
| Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 160 | 	struct tty_ldisc *ld; | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 161 | 	struct tty_ldisc_ops *ldops; | 
 | 162 | 	int err = -EINVAL; | 
| Alan Cox | 852e99d | 2009-06-11 12:51:41 +0100 | [diff] [blame] | 163 |  | 
| Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 164 | 	ld = kmalloc(sizeof(struct tty_ldisc), GFP_KERNEL); | 
 | 165 | 	if (ld == NULL) | 
 | 166 | 		return ERR_PTR(-ENOMEM); | 
 | 167 |  | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 168 | 	spin_lock_irqsave(&tty_ldisc_lock, flags); | 
 | 169 | 	ld->ops = NULL; | 
 | 170 | 	ldops = tty_ldiscs[disc]; | 
 | 171 | 	/* Check the entry is defined */ | 
 | 172 | 	if (ldops) { | 
 | 173 | 		/* If the module is being unloaded we can't use it */ | 
 | 174 | 		if (!try_module_get(ldops->owner)) | 
 | 175 | 			err = -EAGAIN; | 
 | 176 | 		else { | 
 | 177 | 			/* lock it */ | 
 | 178 | 			ldops->refcount++; | 
 | 179 | 			ld->ops = ldops; | 
| Linus Torvalds | 65b7704 | 2009-08-03 11:11:19 -0700 | [diff] [blame] | 180 | 			atomic_set(&ld->users, 1); | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 181 | 			err = 0; | 
 | 182 | 		} | 
 | 183 | 	} | 
 | 184 | 	spin_unlock_irqrestore(&tty_ldisc_lock, flags); | 
| Alan Cox | 8d2ead7 | 2009-06-16 17:00:26 +0100 | [diff] [blame] | 185 | 	if (err) { | 
 | 186 | 		kfree(ld); | 
| Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 187 | 		return ERR_PTR(err); | 
| Alan Cox | 8d2ead7 | 2009-06-16 17:00:26 +0100 | [diff] [blame] | 188 | 	} | 
| Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 189 | 	return ld; | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 190 | } | 
 | 191 |  | 
 | 192 | /** | 
 | 193 |  *	tty_ldisc_get		-	take a reference to an ldisc | 
 | 194 |  *	@disc: ldisc number | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 195 |  * | 
 | 196 |  *	Takes a reference to a line discipline. Deals with refcounts and | 
 | 197 |  *	module locking counts. Returns NULL if the discipline is not available. | 
 | 198 |  *	Returns a pointer to the discipline and bumps the ref count if it is | 
 | 199 |  *	available | 
 | 200 |  * | 
 | 201 |  *	Locking: | 
 | 202 |  *		takes tty_ldisc_lock to guard against ldisc races | 
 | 203 |  */ | 
 | 204 |  | 
| Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 205 | static struct tty_ldisc *tty_ldisc_get(int disc) | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 206 | { | 
| Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 207 | 	struct tty_ldisc *ld; | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 208 |  | 
 | 209 | 	if (disc < N_TTY || disc >= NR_LDISCS) | 
| Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 210 | 		return ERR_PTR(-EINVAL); | 
 | 211 | 	ld = tty_ldisc_try_get(disc); | 
 | 212 | 	if (IS_ERR(ld)) { | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 213 | 		request_module("tty-ldisc-%d", disc); | 
| Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 214 | 		ld = tty_ldisc_try_get(disc); | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 215 | 	} | 
| Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 216 | 	return ld; | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 217 | } | 
 | 218 |  | 
| Alan Cox | 852e99d | 2009-06-11 12:51:41 +0100 | [diff] [blame] | 219 | static void *tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos) | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 220 | { | 
 | 221 | 	return (*pos < NR_LDISCS) ? pos : NULL; | 
 | 222 | } | 
 | 223 |  | 
| Alan Cox | 852e99d | 2009-06-11 12:51:41 +0100 | [diff] [blame] | 224 | static void *tty_ldiscs_seq_next(struct seq_file *m, void *v, loff_t *pos) | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 225 | { | 
 | 226 | 	(*pos)++; | 
 | 227 | 	return (*pos < NR_LDISCS) ? pos : NULL; | 
 | 228 | } | 
 | 229 |  | 
 | 230 | static void tty_ldiscs_seq_stop(struct seq_file *m, void *v) | 
 | 231 | { | 
 | 232 | } | 
 | 233 |  | 
 | 234 | static int tty_ldiscs_seq_show(struct seq_file *m, void *v) | 
 | 235 | { | 
 | 236 | 	int i = *(loff_t *)v; | 
| Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 237 | 	struct tty_ldisc *ld; | 
| Alan Cox | 852e99d | 2009-06-11 12:51:41 +0100 | [diff] [blame] | 238 |  | 
| Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 239 | 	ld = tty_ldisc_try_get(i); | 
 | 240 | 	if (IS_ERR(ld)) | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 241 | 		return 0; | 
| Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 242 | 	seq_printf(m, "%-10s %2d\n", ld->ops->name ? ld->ops->name : "???", i); | 
| Linus Torvalds | 65b7704 | 2009-08-03 11:11:19 -0700 | [diff] [blame] | 243 | 	put_ldisc(ld); | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 244 | 	return 0; | 
 | 245 | } | 
 | 246 |  | 
 | 247 | static const struct seq_operations tty_ldiscs_seq_ops = { | 
 | 248 | 	.start	= tty_ldiscs_seq_start, | 
 | 249 | 	.next	= tty_ldiscs_seq_next, | 
 | 250 | 	.stop	= tty_ldiscs_seq_stop, | 
 | 251 | 	.show	= tty_ldiscs_seq_show, | 
 | 252 | }; | 
 | 253 |  | 
 | 254 | static int proc_tty_ldiscs_open(struct inode *inode, struct file *file) | 
 | 255 | { | 
 | 256 | 	return seq_open(file, &tty_ldiscs_seq_ops); | 
 | 257 | } | 
 | 258 |  | 
 | 259 | const struct file_operations tty_ldiscs_proc_fops = { | 
 | 260 | 	.owner		= THIS_MODULE, | 
 | 261 | 	.open		= proc_tty_ldiscs_open, | 
 | 262 | 	.read		= seq_read, | 
 | 263 | 	.llseek		= seq_lseek, | 
 | 264 | 	.release	= seq_release, | 
 | 265 | }; | 
 | 266 |  | 
 | 267 | /** | 
 | 268 |  *	tty_ldisc_assign	-	set ldisc on a tty | 
 | 269 |  *	@tty: tty to assign | 
 | 270 |  *	@ld: line discipline | 
 | 271 |  * | 
 | 272 |  *	Install an instance of a line discipline into a tty structure. The | 
| Alan Cox | 8d2ead7 | 2009-06-16 17:00:26 +0100 | [diff] [blame] | 273 |  *	ldisc must have a reference count above zero to ensure it remains. | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 274 |  *	The tty instance refcount starts at zero. | 
 | 275 |  * | 
 | 276 |  *	Locking: | 
 | 277 |  *		Caller must hold references | 
 | 278 |  */ | 
 | 279 |  | 
 | 280 | static void tty_ldisc_assign(struct tty_struct *tty, struct tty_ldisc *ld) | 
 | 281 | { | 
| Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 282 | 	tty->ldisc = ld; | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 283 | } | 
 | 284 |  | 
 | 285 | /** | 
 | 286 |  *	tty_ldisc_try		-	internal helper | 
 | 287 |  *	@tty: the tty | 
 | 288 |  * | 
 | 289 |  *	Make a single attempt to grab and bump the refcount on | 
 | 290 |  *	the tty ldisc. Return 0 on failure or 1 on success. This is | 
 | 291 |  *	used to implement both the waiting and non waiting versions | 
 | 292 |  *	of tty_ldisc_ref | 
 | 293 |  * | 
 | 294 |  *	Locking: takes tty_ldisc_lock | 
 | 295 |  */ | 
 | 296 |  | 
| Linus Torvalds | 65b7704 | 2009-08-03 11:11:19 -0700 | [diff] [blame] | 297 | static struct tty_ldisc *tty_ldisc_try(struct tty_struct *tty) | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 298 | { | 
 | 299 | 	unsigned long flags; | 
 | 300 | 	struct tty_ldisc *ld; | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 301 |  | 
 | 302 | 	spin_lock_irqsave(&tty_ldisc_lock, flags); | 
| Linus Torvalds | 65b7704 | 2009-08-03 11:11:19 -0700 | [diff] [blame] | 303 | 	ld = NULL; | 
 | 304 | 	if (test_bit(TTY_LDISC, &tty->flags)) | 
 | 305 | 		ld = get_ldisc(tty->ldisc); | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 306 | 	spin_unlock_irqrestore(&tty_ldisc_lock, flags); | 
| Linus Torvalds | 65b7704 | 2009-08-03 11:11:19 -0700 | [diff] [blame] | 307 | 	return ld; | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 308 | } | 
 | 309 |  | 
 | 310 | /** | 
 | 311 |  *	tty_ldisc_ref_wait	-	wait for the tty ldisc | 
 | 312 |  *	@tty: tty device | 
 | 313 |  * | 
 | 314 |  *	Dereference the line discipline for the terminal and take a | 
 | 315 |  *	reference to it. If the line discipline is in flux then | 
 | 316 |  *	wait patiently until it changes. | 
 | 317 |  * | 
 | 318 |  *	Note: Must not be called from an IRQ/timer context. The caller | 
 | 319 |  *	must also be careful not to hold other locks that will deadlock | 
 | 320 |  *	against a discipline change, such as an existing ldisc reference | 
 | 321 |  *	(which we check for) | 
 | 322 |  * | 
 | 323 |  *	Locking: call functions take tty_ldisc_lock | 
 | 324 |  */ | 
 | 325 |  | 
 | 326 | struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty) | 
 | 327 | { | 
| Linus Torvalds | 65b7704 | 2009-08-03 11:11:19 -0700 | [diff] [blame] | 328 | 	struct tty_ldisc *ld; | 
 | 329 |  | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 330 | 	/* wait_event is a macro */ | 
| Linus Torvalds | 65b7704 | 2009-08-03 11:11:19 -0700 | [diff] [blame] | 331 | 	wait_event(tty_ldisc_wait, (ld = tty_ldisc_try(tty)) != NULL); | 
 | 332 | 	return ld; | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 333 | } | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 334 | EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait); | 
 | 335 |  | 
 | 336 | /** | 
 | 337 |  *	tty_ldisc_ref		-	get the tty ldisc | 
 | 338 |  *	@tty: tty device | 
 | 339 |  * | 
 | 340 |  *	Dereference the line discipline for the terminal and take a | 
 | 341 |  *	reference to it. If the line discipline is in flux then | 
 | 342 |  *	return NULL. Can be called from IRQ and timer functions. | 
 | 343 |  * | 
 | 344 |  *	Locking: called functions take tty_ldisc_lock | 
 | 345 |  */ | 
 | 346 |  | 
 | 347 | struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty) | 
 | 348 | { | 
| Linus Torvalds | 65b7704 | 2009-08-03 11:11:19 -0700 | [diff] [blame] | 349 | 	return tty_ldisc_try(tty); | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 350 | } | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 351 | EXPORT_SYMBOL_GPL(tty_ldisc_ref); | 
 | 352 |  | 
 | 353 | /** | 
 | 354 |  *	tty_ldisc_deref		-	free a tty ldisc reference | 
 | 355 |  *	@ld: reference to free up | 
 | 356 |  * | 
 | 357 |  *	Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May | 
 | 358 |  *	be called in IRQ context. | 
 | 359 |  * | 
 | 360 |  *	Locking: takes tty_ldisc_lock | 
 | 361 |  */ | 
 | 362 |  | 
 | 363 | void tty_ldisc_deref(struct tty_ldisc *ld) | 
 | 364 | { | 
| Linus Torvalds | 65b7704 | 2009-08-03 11:11:19 -0700 | [diff] [blame] | 365 | 	put_ldisc(ld); | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 366 | } | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 367 | EXPORT_SYMBOL_GPL(tty_ldisc_deref); | 
 | 368 |  | 
| Linus Torvalds | 65b7704 | 2009-08-03 11:11:19 -0700 | [diff] [blame] | 369 | static inline void tty_ldisc_put(struct tty_ldisc *ld) | 
 | 370 | { | 
 | 371 | 	put_ldisc(ld); | 
 | 372 | } | 
 | 373 |  | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 374 | /** | 
 | 375 |  *	tty_ldisc_enable	-	allow ldisc use | 
 | 376 |  *	@tty: terminal to activate ldisc on | 
 | 377 |  * | 
 | 378 |  *	Set the TTY_LDISC flag when the line discipline can be called | 
| Alan Cox | c9b3976 | 2009-01-02 13:44:56 +0000 | [diff] [blame] | 379 |  *	again. Do necessary wakeups for existing sleepers. Clear the LDISC | 
 | 380 |  *	changing flag to indicate any ldisc change is now over. | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 381 |  * | 
| Alan Cox | c9b3976 | 2009-01-02 13:44:56 +0000 | [diff] [blame] | 382 |  *	Note: nobody should set the TTY_LDISC bit except via this function. | 
 | 383 |  *	Clearing directly is allowed. | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 384 |  */ | 
 | 385 |  | 
 | 386 | void tty_ldisc_enable(struct tty_struct *tty) | 
 | 387 | { | 
 | 388 | 	set_bit(TTY_LDISC, &tty->flags); | 
| Alan Cox | c9b3976 | 2009-01-02 13:44:56 +0000 | [diff] [blame] | 389 | 	clear_bit(TTY_LDISC_CHANGING, &tty->flags); | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 390 | 	wake_up(&tty_ldisc_wait); | 
 | 391 | } | 
 | 392 |  | 
 | 393 | /** | 
| Alan Cox | f2c4c65 | 2009-06-11 12:50:58 +0100 | [diff] [blame] | 394 |  *	tty_ldisc_flush	-	flush line discipline queue | 
 | 395 |  *	@tty: tty | 
 | 396 |  * | 
 | 397 |  *	Flush the line discipline queue (if any) for this tty. If there | 
 | 398 |  *	is no line discipline active this is a no-op. | 
 | 399 |  */ | 
 | 400 |  | 
 | 401 | void tty_ldisc_flush(struct tty_struct *tty) | 
 | 402 | { | 
 | 403 | 	struct tty_ldisc *ld = tty_ldisc_ref(tty); | 
 | 404 | 	if (ld) { | 
 | 405 | 		if (ld->ops->flush_buffer) | 
 | 406 | 			ld->ops->flush_buffer(tty); | 
 | 407 | 		tty_ldisc_deref(ld); | 
 | 408 | 	} | 
 | 409 | 	tty_buffer_flush(tty); | 
 | 410 | } | 
| Alan Cox | f2c4c65 | 2009-06-11 12:50:58 +0100 | [diff] [blame] | 411 | EXPORT_SYMBOL_GPL(tty_ldisc_flush); | 
 | 412 |  | 
 | 413 | /** | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 414 |  *	tty_set_termios_ldisc		-	set ldisc field | 
 | 415 |  *	@tty: tty structure | 
 | 416 |  *	@num: line discipline number | 
 | 417 |  * | 
 | 418 |  *	This is probably overkill for real world processors but | 
 | 419 |  *	they are not on hot paths so a little discipline won't do | 
 | 420 |  *	any harm. | 
 | 421 |  * | 
 | 422 |  *	Locking: takes termios_mutex | 
 | 423 |  */ | 
 | 424 |  | 
 | 425 | static void tty_set_termios_ldisc(struct tty_struct *tty, int num) | 
 | 426 | { | 
 | 427 | 	mutex_lock(&tty->termios_mutex); | 
 | 428 | 	tty->termios->c_line = num; | 
 | 429 | 	mutex_unlock(&tty->termios_mutex); | 
 | 430 | } | 
 | 431 |  | 
| Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 432 | /** | 
 | 433 |  *	tty_ldisc_open		-	open a line discipline | 
 | 434 |  *	@tty: tty we are opening the ldisc on | 
 | 435 |  *	@ld: discipline to open | 
 | 436 |  * | 
 | 437 |  *	A helper opening method. Also a convenient debugging and check | 
 | 438 |  *	point. | 
 | 439 |  */ | 
 | 440 |  | 
 | 441 | static int tty_ldisc_open(struct tty_struct *tty, struct tty_ldisc *ld) | 
 | 442 | { | 
 | 443 | 	WARN_ON(test_and_set_bit(TTY_LDISC_OPEN, &tty->flags)); | 
 | 444 | 	if (ld->ops->open) | 
 | 445 | 		return ld->ops->open(tty); | 
 | 446 | 	return 0; | 
 | 447 | } | 
 | 448 |  | 
 | 449 | /** | 
 | 450 |  *	tty_ldisc_close		-	close a line discipline | 
 | 451 |  *	@tty: tty we are opening the ldisc on | 
 | 452 |  *	@ld: discipline to close | 
 | 453 |  * | 
 | 454 |  *	A helper close method. Also a convenient debugging and check | 
 | 455 |  *	point. | 
 | 456 |  */ | 
 | 457 |  | 
 | 458 | static void tty_ldisc_close(struct tty_struct *tty, struct tty_ldisc *ld) | 
 | 459 | { | 
 | 460 | 	WARN_ON(!test_bit(TTY_LDISC_OPEN, &tty->flags)); | 
 | 461 | 	clear_bit(TTY_LDISC_OPEN, &tty->flags); | 
 | 462 | 	if (ld->ops->close) | 
 | 463 | 		ld->ops->close(tty); | 
 | 464 | } | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 465 |  | 
 | 466 | /** | 
 | 467 |  *	tty_ldisc_restore	-	helper for tty ldisc change | 
 | 468 |  *	@tty: tty to recover | 
 | 469 |  *	@old: previous ldisc | 
 | 470 |  * | 
 | 471 |  *	Restore the previous line discipline or N_TTY when a line discipline | 
 | 472 |  *	change fails due to an open error | 
 | 473 |  */ | 
 | 474 |  | 
 | 475 | static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old) | 
 | 476 | { | 
 | 477 | 	char buf[64]; | 
| Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 478 | 	struct tty_ldisc *new_ldisc; | 
 | 479 | 	int r; | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 480 |  | 
 | 481 | 	/* There is an outstanding reference here so this is safe */ | 
| Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 482 | 	old = tty_ldisc_get(old->ops->num); | 
 | 483 | 	WARN_ON(IS_ERR(old)); | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 484 | 	tty_ldisc_assign(tty, old); | 
 | 485 | 	tty_set_termios_ldisc(tty, old->ops->num); | 
| Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 486 | 	if (tty_ldisc_open(tty, old) < 0) { | 
 | 487 | 		tty_ldisc_put(old); | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 488 | 		/* This driver is always present */ | 
| Alan Cox | 852e99d | 2009-06-11 12:51:41 +0100 | [diff] [blame] | 489 | 		new_ldisc = tty_ldisc_get(N_TTY); | 
| Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 490 | 		if (IS_ERR(new_ldisc)) | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 491 | 			panic("n_tty: get"); | 
| Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 492 | 		tty_ldisc_assign(tty, new_ldisc); | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 493 | 		tty_set_termios_ldisc(tty, N_TTY); | 
| Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 494 | 		r = tty_ldisc_open(tty, new_ldisc); | 
 | 495 | 		if (r < 0) | 
 | 496 | 			panic("Couldn't open N_TTY ldisc for " | 
 | 497 | 			      "%s --- error %d.", | 
 | 498 | 			      tty_name(tty, buf), r); | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 499 | 	} | 
 | 500 | } | 
 | 501 |  | 
 | 502 | /** | 
| Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 503 |  *	tty_ldisc_halt		-	shut down the line discipline | 
| Alan Cox | e8b70e7 | 2009-06-11 12:48:02 +0100 | [diff] [blame] | 504 |  *	@tty: tty device | 
 | 505 |  * | 
 | 506 |  *	Shut down the line discipline and work queue for this tty device. | 
 | 507 |  *	The TTY_LDISC flag being cleared ensures no further references can | 
 | 508 |  *	be obtained while the delayed work queue halt ensures that no more | 
 | 509 |  *	data is fed to the ldisc. | 
 | 510 |  * | 
| Linus Torvalds | 5c58cef | 2009-08-25 09:12:43 -0700 | [diff] [blame] | 511 |  *	You need to do a 'flush_scheduled_work()' (outside the ldisc_mutex) | 
 | 512 |  *	in order to make sure any currently executing ldisc work is also | 
 | 513 |  *	flushed. | 
| Alan Cox | e8b70e7 | 2009-06-11 12:48:02 +0100 | [diff] [blame] | 514 |  */ | 
 | 515 |  | 
| Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 516 | static int tty_ldisc_halt(struct tty_struct *tty) | 
| Alan Cox | e8b70e7 | 2009-06-11 12:48:02 +0100 | [diff] [blame] | 517 | { | 
 | 518 | 	clear_bit(TTY_LDISC, &tty->flags); | 
| Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 519 | 	return cancel_delayed_work(&tty->buf.work); | 
| Alan Cox | e8b70e7 | 2009-06-11 12:48:02 +0100 | [diff] [blame] | 520 | } | 
 | 521 |  | 
 | 522 | /** | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 523 |  *	tty_set_ldisc		-	set line discipline | 
 | 524 |  *	@tty: the terminal to set | 
 | 525 |  *	@ldisc: the line discipline | 
 | 526 |  * | 
 | 527 |  *	Set the discipline of a tty line. Must be called from a process | 
| Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 528 |  *	context. The ldisc change logic has to protect itself against any | 
 | 529 |  *	overlapping ldisc change (including on the other end of pty pairs), | 
 | 530 |  *	the close of one side of a tty/pty pair, and eventually hangup. | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 531 |  * | 
| Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 532 |  *	Locking: takes tty_ldisc_lock, termios_mutex | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 533 |  */ | 
 | 534 |  | 
 | 535 | int tty_set_ldisc(struct tty_struct *tty, int ldisc) | 
 | 536 | { | 
 | 537 | 	int retval; | 
| Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 538 | 	struct tty_ldisc *o_ldisc, *new_ldisc; | 
 | 539 | 	int work, o_work = 0; | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 540 | 	struct tty_struct *o_tty; | 
 | 541 |  | 
| Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 542 | 	new_ldisc = tty_ldisc_get(ldisc); | 
 | 543 | 	if (IS_ERR(new_ldisc)) | 
 | 544 | 		return PTR_ERR(new_ldisc); | 
 | 545 |  | 
 | 546 | 	/* | 
 | 547 | 	 *	We need to look at the tty locking here for pty/tty pairs | 
 | 548 | 	 *	when both sides try to change in parallel. | 
 | 549 | 	 */ | 
 | 550 |  | 
 | 551 | 	o_tty = tty->link;	/* o_tty is the pty side or NULL */ | 
 | 552 |  | 
 | 553 |  | 
 | 554 | 	/* | 
 | 555 | 	 *	Check the no-op case | 
 | 556 | 	 */ | 
 | 557 |  | 
 | 558 | 	if (tty->ldisc->ops->num == ldisc) { | 
 | 559 | 		tty_ldisc_put(new_ldisc); | 
 | 560 | 		return 0; | 
 | 561 | 	} | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 562 |  | 
 | 563 | 	/* | 
 | 564 | 	 *	Problem: What do we do if this blocks ? | 
| Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 565 | 	 *	We could deadlock here | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 566 | 	 */ | 
 | 567 |  | 
 | 568 | 	tty_wait_until_sent(tty, 0); | 
 | 569 |  | 
| Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 570 | 	mutex_lock(&tty->ldisc_mutex); | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 571 |  | 
 | 572 | 	/* | 
| Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 573 | 	 *	We could be midstream of another ldisc change which has | 
 | 574 | 	 *	dropped the lock during processing. If so we need to wait. | 
 | 575 | 	 */ | 
 | 576 |  | 
 | 577 | 	while (test_bit(TTY_LDISC_CHANGING, &tty->flags)) { | 
 | 578 | 		mutex_unlock(&tty->ldisc_mutex); | 
 | 579 | 		wait_event(tty_ldisc_wait, | 
 | 580 | 			test_bit(TTY_LDISC_CHANGING, &tty->flags) == 0); | 
 | 581 | 		mutex_lock(&tty->ldisc_mutex); | 
 | 582 | 	} | 
 | 583 | 	set_bit(TTY_LDISC_CHANGING, &tty->flags); | 
| Alan Cox | 852e99d | 2009-06-11 12:51:41 +0100 | [diff] [blame] | 584 |  | 
| Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 585 | 	/* | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 586 | 	 *	No more input please, we are switching. The new ldisc | 
 | 587 | 	 *	will update this value in the ldisc open function | 
 | 588 | 	 */ | 
 | 589 |  | 
 | 590 | 	tty->receive_room = 0; | 
 | 591 |  | 
 | 592 | 	o_ldisc = tty->ldisc; | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 593 | 	/* | 
 | 594 | 	 *	Make sure we don't change while someone holds a | 
 | 595 | 	 *	reference to the line discipline. The TTY_LDISC bit | 
 | 596 | 	 *	prevents anyone taking a reference once it is clear. | 
 | 597 | 	 *	We need the lock to avoid racing reference takers. | 
| Alan Cox | c9b3976 | 2009-01-02 13:44:56 +0000 | [diff] [blame] | 598 | 	 * | 
 | 599 | 	 *	We must clear the TTY_LDISC bit here to avoid a livelock | 
 | 600 | 	 *	with a userspace app continually trying to use the tty in | 
 | 601 | 	 *	parallel to the change and re-referencing the tty. | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 602 | 	 */ | 
 | 603 |  | 
| Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 604 | 	work = tty_ldisc_halt(tty); | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 605 | 	if (o_tty) | 
| Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 606 | 		o_work = tty_ldisc_halt(o_tty); | 
 | 607 |  | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 608 | 	/* | 
| Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 609 | 	 * Wait for ->hangup_work and ->buf.work handlers to terminate. | 
 | 610 | 	 * We must drop the mutex here in case a hangup is also in process. | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 611 | 	 */ | 
 | 612 |  | 
| Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 613 | 	mutex_unlock(&tty->ldisc_mutex); | 
 | 614 |  | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 615 | 	flush_scheduled_work(); | 
| Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 616 |  | 
| Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 617 | 	mutex_lock(&tty->ldisc_mutex); | 
 | 618 | 	if (test_bit(TTY_HUPPED, &tty->flags)) { | 
 | 619 | 		/* We were raced by the hangup method. It will have stomped | 
 | 620 | 		   the ldisc data and closed the ldisc down */ | 
 | 621 | 		clear_bit(TTY_LDISC_CHANGING, &tty->flags); | 
 | 622 | 		mutex_unlock(&tty->ldisc_mutex); | 
 | 623 | 		tty_ldisc_put(new_ldisc); | 
 | 624 | 		return -EIO; | 
 | 625 | 	} | 
 | 626 |  | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 627 | 	/* Shutdown the current discipline. */ | 
| Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 628 | 	tty_ldisc_close(tty, o_ldisc); | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 629 |  | 
 | 630 | 	/* Now set up the new line discipline. */ | 
| Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 631 | 	tty_ldisc_assign(tty, new_ldisc); | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 632 | 	tty_set_termios_ldisc(tty, ldisc); | 
| Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 633 |  | 
 | 634 | 	retval = tty_ldisc_open(tty, new_ldisc); | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 635 | 	if (retval < 0) { | 
| Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 636 | 		/* Back to the old one or N_TTY if we can't */ | 
 | 637 | 		tty_ldisc_put(new_ldisc); | 
 | 638 | 		tty_ldisc_restore(tty, o_ldisc); | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 639 | 	} | 
| Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 640 |  | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 641 | 	/* At this point we hold a reference to the new ldisc and a | 
 | 642 | 	   a reference to the old ldisc. If we ended up flipping back | 
 | 643 | 	   to the existing ldisc we have two references to it */ | 
 | 644 |  | 
| Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 645 | 	if (tty->ldisc->ops->num != o_ldisc->ops->num && tty->ops->set_ldisc) | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 646 | 		tty->ops->set_ldisc(tty); | 
 | 647 |  | 
| Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 648 | 	tty_ldisc_put(o_ldisc); | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 649 |  | 
 | 650 | 	/* | 
| Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 651 | 	 *	Allow ldisc referencing to occur again | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 652 | 	 */ | 
 | 653 |  | 
 | 654 | 	tty_ldisc_enable(tty); | 
 | 655 | 	if (o_tty) | 
 | 656 | 		tty_ldisc_enable(o_tty); | 
 | 657 |  | 
| Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 658 | 	/* Restart the work queue in case no characters kick it off. Safe if | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 659 | 	   already running */ | 
 | 660 | 	if (work) | 
 | 661 | 		schedule_delayed_work(&tty->buf.work, 1); | 
| Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 662 | 	if (o_work) | 
 | 663 | 		schedule_delayed_work(&o_tty->buf.work, 1); | 
 | 664 | 	mutex_unlock(&tty->ldisc_mutex); | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 665 | 	return retval; | 
 | 666 | } | 
 | 667 |  | 
| Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 668 | /** | 
 | 669 |  *	tty_reset_termios	-	reset terminal state | 
 | 670 |  *	@tty: tty to reset | 
 | 671 |  * | 
 | 672 |  *	Restore a terminal to the driver default state. | 
 | 673 |  */ | 
 | 674 |  | 
 | 675 | static void tty_reset_termios(struct tty_struct *tty) | 
 | 676 | { | 
 | 677 | 	mutex_lock(&tty->termios_mutex); | 
 | 678 | 	*tty->termios = tty->driver->init_termios; | 
 | 679 | 	tty->termios->c_ispeed = tty_termios_input_baud_rate(tty->termios); | 
 | 680 | 	tty->termios->c_ospeed = tty_termios_baud_rate(tty->termios); | 
 | 681 | 	mutex_unlock(&tty->termios_mutex); | 
 | 682 | } | 
 | 683 |  | 
 | 684 |  | 
 | 685 | /** | 
 | 686 |  *	tty_ldisc_reinit	-	reinitialise the tty ldisc | 
 | 687 |  *	@tty: tty to reinit | 
 | 688 |  * | 
 | 689 |  *	Switch the tty back to N_TTY line discipline and leave the | 
 | 690 |  *	ldisc state closed | 
 | 691 |  */ | 
 | 692 |  | 
 | 693 | static void tty_ldisc_reinit(struct tty_struct *tty) | 
 | 694 | { | 
 | 695 | 	struct tty_ldisc *ld; | 
 | 696 |  | 
 | 697 | 	tty_ldisc_close(tty, tty->ldisc); | 
 | 698 | 	tty_ldisc_put(tty->ldisc); | 
 | 699 | 	tty->ldisc = NULL; | 
 | 700 | 	/* | 
 | 701 | 	 *	Switch the line discipline back | 
 | 702 | 	 */ | 
 | 703 | 	ld = tty_ldisc_get(N_TTY); | 
 | 704 | 	BUG_ON(IS_ERR(ld)); | 
 | 705 | 	tty_ldisc_assign(tty, ld); | 
 | 706 | 	tty_set_termios_ldisc(tty, N_TTY); | 
 | 707 | } | 
 | 708 |  | 
 | 709 | /** | 
 | 710 |  *	tty_ldisc_hangup		-	hangup ldisc reset | 
 | 711 |  *	@tty: tty being hung up | 
 | 712 |  * | 
 | 713 |  *	Some tty devices reset their termios when they receive a hangup | 
 | 714 |  *	event. In that situation we must also switch back to N_TTY properly | 
 | 715 |  *	before we reset the termios data. | 
 | 716 |  * | 
 | 717 |  *	Locking: We can take the ldisc mutex as the rest of the code is | 
 | 718 |  *	careful to allow for this. | 
 | 719 |  * | 
 | 720 |  *	In the pty pair case this occurs in the close() path of the | 
 | 721 |  *	tty itself so we must be careful about locking rules. | 
 | 722 |  */ | 
 | 723 |  | 
 | 724 | void tty_ldisc_hangup(struct tty_struct *tty) | 
 | 725 | { | 
 | 726 | 	struct tty_ldisc *ld; | 
 | 727 |  | 
 | 728 | 	/* | 
 | 729 | 	 * FIXME! What are the locking issues here? This may me overdoing | 
 | 730 | 	 * things... This question is especially important now that we've | 
 | 731 | 	 * removed the irqlock. | 
 | 732 | 	 */ | 
 | 733 | 	ld = tty_ldisc_ref(tty); | 
 | 734 | 	if (ld != NULL) { | 
 | 735 | 		/* We may have no line discipline at this point */ | 
 | 736 | 		if (ld->ops->flush_buffer) | 
 | 737 | 			ld->ops->flush_buffer(tty); | 
 | 738 | 		tty_driver_flush_buffer(tty); | 
 | 739 | 		if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) && | 
 | 740 | 		    ld->ops->write_wakeup) | 
 | 741 | 			ld->ops->write_wakeup(tty); | 
 | 742 | 		if (ld->ops->hangup) | 
 | 743 | 			ld->ops->hangup(tty); | 
 | 744 | 		tty_ldisc_deref(ld); | 
 | 745 | 	} | 
 | 746 | 	/* | 
 | 747 | 	 * FIXME: Once we trust the LDISC code better we can wait here for | 
 | 748 | 	 * ldisc completion and fix the driver call race | 
 | 749 | 	 */ | 
 | 750 | 	wake_up_interruptible_poll(&tty->write_wait, POLLOUT); | 
 | 751 | 	wake_up_interruptible_poll(&tty->read_wait, POLLIN); | 
 | 752 | 	/* | 
 | 753 | 	 * Shutdown the current line discipline, and reset it to | 
 | 754 | 	 * N_TTY. | 
 | 755 | 	 */ | 
 | 756 | 	if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) { | 
| Linus Torvalds | 5c58cef | 2009-08-25 09:12:43 -0700 | [diff] [blame] | 757 | 		/* Make sure the old ldisc is quiescent */ | 
 | 758 | 		tty_ldisc_halt(tty); | 
 | 759 | 		flush_scheduled_work(); | 
 | 760 |  | 
| Alan Cox | c8d5004 | 2009-07-16 16:05:08 +0100 | [diff] [blame] | 761 | 		/* Avoid racing set_ldisc or tty_ldisc_release */ | 
| Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 762 | 		mutex_lock(&tty->ldisc_mutex); | 
| Alan Cox | c8d5004 | 2009-07-16 16:05:08 +0100 | [diff] [blame] | 763 | 		if (tty->ldisc) {	/* Not yet closed */ | 
 | 764 | 			/* Switch back to N_TTY */ | 
| Alan Cox | c8d5004 | 2009-07-16 16:05:08 +0100 | [diff] [blame] | 765 | 			tty_ldisc_reinit(tty); | 
 | 766 | 			/* At this point we have a closed ldisc and we want to | 
 | 767 | 			   reopen it. We could defer this to the next open but | 
 | 768 | 			   it means auditing a lot of other paths so this is | 
 | 769 | 			   a FIXME */ | 
 | 770 | 			WARN_ON(tty_ldisc_open(tty, tty->ldisc)); | 
 | 771 | 			tty_ldisc_enable(tty); | 
 | 772 | 		} | 
| Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 773 | 		mutex_unlock(&tty->ldisc_mutex); | 
 | 774 | 		tty_reset_termios(tty); | 
 | 775 | 	} | 
 | 776 | } | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 777 |  | 
 | 778 | /** | 
 | 779 |  *	tty_ldisc_setup			-	open line discipline | 
 | 780 |  *	@tty: tty being shut down | 
 | 781 |  *	@o_tty: pair tty for pty/tty pairs | 
 | 782 |  * | 
 | 783 |  *	Called during the initial open of a tty/pty pair in order to set up the | 
| Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 784 |  *	line disciplines and bind them to the tty. This has no locking issues | 
 | 785 |  *	as the device isn't yet active. | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 786 |  */ | 
 | 787 |  | 
 | 788 | int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty) | 
 | 789 | { | 
| Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 790 | 	struct tty_ldisc *ld = tty->ldisc; | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 791 | 	int retval; | 
 | 792 |  | 
| Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 793 | 	retval = tty_ldisc_open(tty, ld); | 
 | 794 | 	if (retval) | 
 | 795 | 		return retval; | 
 | 796 |  | 
 | 797 | 	if (o_tty) { | 
 | 798 | 		retval = tty_ldisc_open(o_tty, o_tty->ldisc); | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 799 | 		if (retval) { | 
| Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 800 | 			tty_ldisc_close(tty, ld); | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 801 | 			return retval; | 
 | 802 | 		} | 
 | 803 | 		tty_ldisc_enable(o_tty); | 
 | 804 | 	} | 
 | 805 | 	tty_ldisc_enable(tty); | 
 | 806 | 	return 0; | 
 | 807 | } | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 808 | /** | 
 | 809 |  *	tty_ldisc_release		-	release line discipline | 
 | 810 |  *	@tty: tty being shut down | 
 | 811 |  *	@o_tty: pair tty for pty/tty pairs | 
 | 812 |  * | 
| Alan Cox | 852e99d | 2009-06-11 12:51:41 +0100 | [diff] [blame] | 813 |  *	Called during the final close of a tty/pty pair in order to shut down | 
 | 814 |  *	the line discpline layer. On exit the ldisc assigned is N_TTY and the | 
| Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 815 |  *	ldisc has not been opened. | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 816 |  */ | 
 | 817 |  | 
 | 818 | void tty_ldisc_release(struct tty_struct *tty, struct tty_struct *o_tty) | 
 | 819 | { | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 820 | 	/* | 
 | 821 | 	 * Prevent flush_to_ldisc() from rescheduling the work for later.  Then | 
 | 822 | 	 * kill any delayed work. As this is the final close it does not | 
 | 823 | 	 * race with the set_ldisc code path. | 
 | 824 | 	 */ | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 825 |  | 
| Alan Cox | e8b70e7 | 2009-06-11 12:48:02 +0100 | [diff] [blame] | 826 | 	tty_ldisc_halt(tty); | 
| Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 827 | 	flush_scheduled_work(); | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 828 |  | 
| Alan Cox | c8d5004 | 2009-07-16 16:05:08 +0100 | [diff] [blame] | 829 | 	mutex_lock(&tty->ldisc_mutex); | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 830 | 	/* | 
| Alan Cox | aef29bc | 2009-06-29 15:21:47 +0100 | [diff] [blame] | 831 | 	 * Now kill off the ldisc | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 832 | 	 */ | 
| Alan Cox | aef29bc | 2009-06-29 15:21:47 +0100 | [diff] [blame] | 833 | 	tty_ldisc_close(tty, tty->ldisc); | 
 | 834 | 	tty_ldisc_put(tty->ldisc); | 
 | 835 | 	/* Force an oops if we mess this up */ | 
 | 836 | 	tty->ldisc = NULL; | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 837 |  | 
| Alan Cox | aef29bc | 2009-06-29 15:21:47 +0100 | [diff] [blame] | 838 | 	/* Ensure the next open requests the N_TTY ldisc */ | 
 | 839 | 	tty_set_termios_ldisc(tty, N_TTY); | 
| Alan Cox | c8d5004 | 2009-07-16 16:05:08 +0100 | [diff] [blame] | 840 | 	mutex_unlock(&tty->ldisc_mutex); | 
| Alan Cox | aef29bc | 2009-06-29 15:21:47 +0100 | [diff] [blame] | 841 |  | 
| Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 842 | 	/* This will need doing differently if we need to lock */ | 
 | 843 | 	if (o_tty) | 
 | 844 | 		tty_ldisc_release(o_tty, NULL); | 
| Alan Cox | aef29bc | 2009-06-29 15:21:47 +0100 | [diff] [blame] | 845 |  | 
 | 846 | 	/* And the memory resources remaining (buffers, termios) will be | 
 | 847 | 	   disposed of when the kref hits zero */ | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 848 | } | 
 | 849 |  | 
 | 850 | /** | 
 | 851 |  *	tty_ldisc_init		-	ldisc setup for new tty | 
 | 852 |  *	@tty: tty being allocated | 
 | 853 |  * | 
 | 854 |  *	Set up the line discipline objects for a newly allocated tty. Note that | 
 | 855 |  *	the tty structure is not completely set up when this call is made. | 
 | 856 |  */ | 
 | 857 |  | 
 | 858 | void tty_ldisc_init(struct tty_struct *tty) | 
 | 859 | { | 
| Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 860 | 	struct tty_ldisc *ld = tty_ldisc_get(N_TTY); | 
 | 861 | 	if (IS_ERR(ld)) | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 862 | 		panic("n_tty: init_tty"); | 
| Alan Cox | c65c9bc | 2009-06-11 12:50:12 +0100 | [diff] [blame] | 863 | 	tty_ldisc_assign(tty, ld); | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 864 | } | 
 | 865 |  | 
 | 866 | void tty_ldisc_begin(void) | 
 | 867 | { | 
 | 868 | 	/* Setup the default TTY line discipline. */ | 
 | 869 | 	(void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY); | 
 | 870 | } |