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