| 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> | 
|  | 24 | #include <linux/smp_lock.h> | 
|  | 25 | #include <linux/device.h> | 
|  | 26 | #include <linux/wait.h> | 
|  | 27 | #include <linux/bitops.h> | 
|  | 28 | #include <linux/delay.h> | 
|  | 29 | #include <linux/seq_file.h> | 
|  | 30 |  | 
|  | 31 | #include <linux/uaccess.h> | 
|  | 32 | #include <asm/system.h> | 
|  | 33 |  | 
|  | 34 | #include <linux/kbd_kern.h> | 
|  | 35 | #include <linux/vt_kern.h> | 
|  | 36 | #include <linux/selection.h> | 
|  | 37 |  | 
|  | 38 | #include <linux/kmod.h> | 
|  | 39 | #include <linux/nsproxy.h> | 
|  | 40 |  | 
|  | 41 | /* | 
|  | 42 | *	This guards the refcounted line discipline lists. The lock | 
|  | 43 | *	must be taken with irqs off because there are hangup path | 
|  | 44 | *	callers who will do ldisc lookups and cannot sleep. | 
|  | 45 | */ | 
|  | 46 |  | 
|  | 47 | static DEFINE_SPINLOCK(tty_ldisc_lock); | 
|  | 48 | static DECLARE_WAIT_QUEUE_HEAD(tty_ldisc_wait); | 
|  | 49 | /* Line disc dispatch table */ | 
|  | 50 | static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS]; | 
|  | 51 |  | 
|  | 52 | /** | 
|  | 53 | *	tty_register_ldisc	-	install a line discipline | 
|  | 54 | *	@disc: ldisc number | 
|  | 55 | *	@new_ldisc: pointer to the ldisc object | 
|  | 56 | * | 
|  | 57 | *	Installs a new line discipline into the kernel. The discipline | 
|  | 58 | *	is set up as unreferenced and then made available to the kernel | 
|  | 59 | *	from this point onwards. | 
|  | 60 | * | 
|  | 61 | *	Locking: | 
|  | 62 | *		takes tty_ldisc_lock to guard against ldisc races | 
|  | 63 | */ | 
|  | 64 |  | 
|  | 65 | int tty_register_ldisc(int disc, struct tty_ldisc_ops *new_ldisc) | 
|  | 66 | { | 
|  | 67 | unsigned long flags; | 
|  | 68 | int ret = 0; | 
|  | 69 |  | 
|  | 70 | if (disc < N_TTY || disc >= NR_LDISCS) | 
|  | 71 | return -EINVAL; | 
|  | 72 |  | 
|  | 73 | spin_lock_irqsave(&tty_ldisc_lock, flags); | 
|  | 74 | tty_ldiscs[disc] = new_ldisc; | 
|  | 75 | new_ldisc->num = disc; | 
|  | 76 | new_ldisc->refcount = 0; | 
|  | 77 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); | 
|  | 78 |  | 
|  | 79 | return ret; | 
|  | 80 | } | 
|  | 81 | EXPORT_SYMBOL(tty_register_ldisc); | 
|  | 82 |  | 
|  | 83 | /** | 
|  | 84 | *	tty_unregister_ldisc	-	unload a line discipline | 
|  | 85 | *	@disc: ldisc number | 
|  | 86 | *	@new_ldisc: pointer to the ldisc object | 
|  | 87 | * | 
|  | 88 | *	Remove a line discipline from the kernel providing it is not | 
|  | 89 | *	currently in use. | 
|  | 90 | * | 
|  | 91 | *	Locking: | 
|  | 92 | *		takes tty_ldisc_lock to guard against ldisc races | 
|  | 93 | */ | 
|  | 94 |  | 
|  | 95 | int tty_unregister_ldisc(int disc) | 
|  | 96 | { | 
|  | 97 | unsigned long flags; | 
|  | 98 | int ret = 0; | 
|  | 99 |  | 
|  | 100 | if (disc < N_TTY || disc >= NR_LDISCS) | 
|  | 101 | return -EINVAL; | 
|  | 102 |  | 
|  | 103 | spin_lock_irqsave(&tty_ldisc_lock, flags); | 
|  | 104 | if (tty_ldiscs[disc]->refcount) | 
|  | 105 | ret = -EBUSY; | 
|  | 106 | else | 
|  | 107 | tty_ldiscs[disc] = NULL; | 
|  | 108 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); | 
|  | 109 |  | 
|  | 110 | return ret; | 
|  | 111 | } | 
|  | 112 | EXPORT_SYMBOL(tty_unregister_ldisc); | 
|  | 113 |  | 
|  | 114 |  | 
|  | 115 | /** | 
|  | 116 | *	tty_ldisc_try_get	-	try and reference an ldisc | 
|  | 117 | *	@disc: ldisc number | 
|  | 118 | *	@ld: tty ldisc structure to complete | 
|  | 119 | * | 
|  | 120 | *	Attempt to open and lock a line discipline into place. Return | 
|  | 121 | *	the line discipline refcounted and assigned in ld. On an error | 
|  | 122 | *	report the error code back | 
|  | 123 | */ | 
|  | 124 |  | 
|  | 125 | static int tty_ldisc_try_get(int disc, struct tty_ldisc *ld) | 
|  | 126 | { | 
|  | 127 | unsigned long flags; | 
|  | 128 | struct tty_ldisc_ops *ldops; | 
|  | 129 | int err = -EINVAL; | 
|  | 130 |  | 
|  | 131 | spin_lock_irqsave(&tty_ldisc_lock, flags); | 
|  | 132 | ld->ops = NULL; | 
|  | 133 | ldops = tty_ldiscs[disc]; | 
|  | 134 | /* Check the entry is defined */ | 
|  | 135 | if (ldops) { | 
|  | 136 | /* If the module is being unloaded we can't use it */ | 
|  | 137 | if (!try_module_get(ldops->owner)) | 
|  | 138 | err = -EAGAIN; | 
|  | 139 | else { | 
|  | 140 | /* lock it */ | 
|  | 141 | ldops->refcount++; | 
|  | 142 | ld->ops = ldops; | 
|  | 143 | err = 0; | 
|  | 144 | } | 
|  | 145 | } | 
|  | 146 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); | 
|  | 147 | return err; | 
|  | 148 | } | 
|  | 149 |  | 
|  | 150 | /** | 
|  | 151 | *	tty_ldisc_get		-	take a reference to an ldisc | 
|  | 152 | *	@disc: ldisc number | 
|  | 153 | *	@ld: tty line discipline structure to use | 
|  | 154 | * | 
|  | 155 | *	Takes a reference to a line discipline. Deals with refcounts and | 
|  | 156 | *	module locking counts. Returns NULL if the discipline is not available. | 
|  | 157 | *	Returns a pointer to the discipline and bumps the ref count if it is | 
|  | 158 | *	available | 
|  | 159 | * | 
|  | 160 | *	Locking: | 
|  | 161 | *		takes tty_ldisc_lock to guard against ldisc races | 
|  | 162 | */ | 
|  | 163 |  | 
|  | 164 | static int tty_ldisc_get(int disc, struct tty_ldisc *ld) | 
|  | 165 | { | 
|  | 166 | int err; | 
|  | 167 |  | 
|  | 168 | if (disc < N_TTY || disc >= NR_LDISCS) | 
|  | 169 | return -EINVAL; | 
|  | 170 | err = tty_ldisc_try_get(disc, ld); | 
| Eugeniy Meshcheryakov | 3ab36ab | 2008-07-31 10:03:19 +0100 | [diff] [blame] | 171 | if (err < 0) { | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 172 | request_module("tty-ldisc-%d", disc); | 
|  | 173 | err = tty_ldisc_try_get(disc, ld); | 
|  | 174 | } | 
|  | 175 | return err; | 
|  | 176 | } | 
|  | 177 |  | 
|  | 178 | /** | 
|  | 179 | *	tty_ldisc_put		-	drop ldisc reference | 
|  | 180 | *	@disc: ldisc number | 
|  | 181 | * | 
|  | 182 | *	Drop a reference to a line discipline. Manage refcounts and | 
|  | 183 | *	module usage counts | 
|  | 184 | * | 
|  | 185 | *	Locking: | 
|  | 186 | *		takes tty_ldisc_lock to guard against ldisc races | 
|  | 187 | */ | 
|  | 188 |  | 
|  | 189 | static void tty_ldisc_put(struct tty_ldisc_ops *ld) | 
|  | 190 | { | 
|  | 191 | unsigned long flags; | 
|  | 192 | int disc = ld->num; | 
|  | 193 |  | 
|  | 194 | BUG_ON(disc < N_TTY || disc >= NR_LDISCS); | 
|  | 195 |  | 
|  | 196 | spin_lock_irqsave(&tty_ldisc_lock, flags); | 
|  | 197 | ld = tty_ldiscs[disc]; | 
|  | 198 | BUG_ON(ld->refcount == 0); | 
|  | 199 | ld->refcount--; | 
|  | 200 | module_put(ld->owner); | 
|  | 201 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); | 
|  | 202 | } | 
|  | 203 |  | 
|  | 204 | static void * tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos) | 
|  | 205 | { | 
|  | 206 | return (*pos < NR_LDISCS) ? pos : NULL; | 
|  | 207 | } | 
|  | 208 |  | 
|  | 209 | static void * tty_ldiscs_seq_next(struct seq_file *m, void *v, loff_t *pos) | 
|  | 210 | { | 
|  | 211 | (*pos)++; | 
|  | 212 | return (*pos < NR_LDISCS) ? pos : NULL; | 
|  | 213 | } | 
|  | 214 |  | 
|  | 215 | static void tty_ldiscs_seq_stop(struct seq_file *m, void *v) | 
|  | 216 | { | 
|  | 217 | } | 
|  | 218 |  | 
|  | 219 | static int tty_ldiscs_seq_show(struct seq_file *m, void *v) | 
|  | 220 | { | 
|  | 221 | int i = *(loff_t *)v; | 
|  | 222 | struct tty_ldisc ld; | 
|  | 223 |  | 
|  | 224 | if (tty_ldisc_get(i, &ld) < 0) | 
|  | 225 | return 0; | 
|  | 226 | seq_printf(m, "%-10s %2d\n", ld.ops->name ? ld.ops->name : "???", i); | 
|  | 227 | tty_ldisc_put(ld.ops); | 
|  | 228 | return 0; | 
|  | 229 | } | 
|  | 230 |  | 
|  | 231 | static const struct seq_operations tty_ldiscs_seq_ops = { | 
|  | 232 | .start	= tty_ldiscs_seq_start, | 
|  | 233 | .next	= tty_ldiscs_seq_next, | 
|  | 234 | .stop	= tty_ldiscs_seq_stop, | 
|  | 235 | .show	= tty_ldiscs_seq_show, | 
|  | 236 | }; | 
|  | 237 |  | 
|  | 238 | static int proc_tty_ldiscs_open(struct inode *inode, struct file *file) | 
|  | 239 | { | 
|  | 240 | return seq_open(file, &tty_ldiscs_seq_ops); | 
|  | 241 | } | 
|  | 242 |  | 
|  | 243 | const struct file_operations tty_ldiscs_proc_fops = { | 
|  | 244 | .owner		= THIS_MODULE, | 
|  | 245 | .open		= proc_tty_ldiscs_open, | 
|  | 246 | .read		= seq_read, | 
|  | 247 | .llseek		= seq_lseek, | 
|  | 248 | .release	= seq_release, | 
|  | 249 | }; | 
|  | 250 |  | 
|  | 251 | /** | 
|  | 252 | *	tty_ldisc_assign	-	set ldisc on a tty | 
|  | 253 | *	@tty: tty to assign | 
|  | 254 | *	@ld: line discipline | 
|  | 255 | * | 
|  | 256 | *	Install an instance of a line discipline into a tty structure. The | 
|  | 257 | *	ldisc must have a reference count above zero to ensure it remains/ | 
|  | 258 | *	The tty instance refcount starts at zero. | 
|  | 259 | * | 
|  | 260 | *	Locking: | 
|  | 261 | *		Caller must hold references | 
|  | 262 | */ | 
|  | 263 |  | 
|  | 264 | static void tty_ldisc_assign(struct tty_struct *tty, struct tty_ldisc *ld) | 
|  | 265 | { | 
|  | 266 | ld->refcount = 0; | 
|  | 267 | tty->ldisc = *ld; | 
|  | 268 | } | 
|  | 269 |  | 
|  | 270 | /** | 
|  | 271 | *	tty_ldisc_try		-	internal helper | 
|  | 272 | *	@tty: the tty | 
|  | 273 | * | 
|  | 274 | *	Make a single attempt to grab and bump the refcount on | 
|  | 275 | *	the tty ldisc. Return 0 on failure or 1 on success. This is | 
|  | 276 | *	used to implement both the waiting and non waiting versions | 
|  | 277 | *	of tty_ldisc_ref | 
|  | 278 | * | 
|  | 279 | *	Locking: takes tty_ldisc_lock | 
|  | 280 | */ | 
|  | 281 |  | 
|  | 282 | static int tty_ldisc_try(struct tty_struct *tty) | 
|  | 283 | { | 
|  | 284 | unsigned long flags; | 
|  | 285 | struct tty_ldisc *ld; | 
|  | 286 | int ret = 0; | 
|  | 287 |  | 
|  | 288 | spin_lock_irqsave(&tty_ldisc_lock, flags); | 
|  | 289 | ld = &tty->ldisc; | 
|  | 290 | if (test_bit(TTY_LDISC, &tty->flags)) { | 
|  | 291 | ld->refcount++; | 
|  | 292 | ret = 1; | 
|  | 293 | } | 
|  | 294 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); | 
|  | 295 | return ret; | 
|  | 296 | } | 
|  | 297 |  | 
|  | 298 | /** | 
|  | 299 | *	tty_ldisc_ref_wait	-	wait for the tty ldisc | 
|  | 300 | *	@tty: tty device | 
|  | 301 | * | 
|  | 302 | *	Dereference the line discipline for the terminal and take a | 
|  | 303 | *	reference to it. If the line discipline is in flux then | 
|  | 304 | *	wait patiently until it changes. | 
|  | 305 | * | 
|  | 306 | *	Note: Must not be called from an IRQ/timer context. The caller | 
|  | 307 | *	must also be careful not to hold other locks that will deadlock | 
|  | 308 | *	against a discipline change, such as an existing ldisc reference | 
|  | 309 | *	(which we check for) | 
|  | 310 | * | 
|  | 311 | *	Locking: call functions take tty_ldisc_lock | 
|  | 312 | */ | 
|  | 313 |  | 
|  | 314 | struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty) | 
|  | 315 | { | 
|  | 316 | /* wait_event is a macro */ | 
|  | 317 | wait_event(tty_ldisc_wait, tty_ldisc_try(tty)); | 
| Alan Cox | c9b3976 | 2009-01-02 13:44:56 +0000 | [diff] [blame] | 318 | WARN_ON(tty->ldisc.refcount == 0); | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 319 | return &tty->ldisc; | 
|  | 320 | } | 
|  | 321 |  | 
|  | 322 | EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait); | 
|  | 323 |  | 
|  | 324 | /** | 
|  | 325 | *	tty_ldisc_ref		-	get the tty ldisc | 
|  | 326 | *	@tty: tty device | 
|  | 327 | * | 
|  | 328 | *	Dereference the line discipline for the terminal and take a | 
|  | 329 | *	reference to it. If the line discipline is in flux then | 
|  | 330 | *	return NULL. Can be called from IRQ and timer functions. | 
|  | 331 | * | 
|  | 332 | *	Locking: called functions take tty_ldisc_lock | 
|  | 333 | */ | 
|  | 334 |  | 
|  | 335 | struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty) | 
|  | 336 | { | 
|  | 337 | if (tty_ldisc_try(tty)) | 
|  | 338 | return &tty->ldisc; | 
|  | 339 | return NULL; | 
|  | 340 | } | 
|  | 341 |  | 
|  | 342 | EXPORT_SYMBOL_GPL(tty_ldisc_ref); | 
|  | 343 |  | 
|  | 344 | /** | 
|  | 345 | *	tty_ldisc_deref		-	free a tty ldisc reference | 
|  | 346 | *	@ld: reference to free up | 
|  | 347 | * | 
|  | 348 | *	Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May | 
|  | 349 | *	be called in IRQ context. | 
|  | 350 | * | 
|  | 351 | *	Locking: takes tty_ldisc_lock | 
|  | 352 | */ | 
|  | 353 |  | 
|  | 354 | void tty_ldisc_deref(struct tty_ldisc *ld) | 
|  | 355 | { | 
|  | 356 | unsigned long flags; | 
|  | 357 |  | 
|  | 358 | BUG_ON(ld == NULL); | 
|  | 359 |  | 
|  | 360 | spin_lock_irqsave(&tty_ldisc_lock, flags); | 
|  | 361 | if (ld->refcount == 0) | 
|  | 362 | printk(KERN_ERR "tty_ldisc_deref: no references.\n"); | 
|  | 363 | else | 
|  | 364 | ld->refcount--; | 
|  | 365 | if (ld->refcount == 0) | 
|  | 366 | wake_up(&tty_ldisc_wait); | 
|  | 367 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); | 
|  | 368 | } | 
|  | 369 |  | 
|  | 370 | EXPORT_SYMBOL_GPL(tty_ldisc_deref); | 
|  | 371 |  | 
|  | 372 | /** | 
|  | 373 | *	tty_ldisc_enable	-	allow ldisc use | 
|  | 374 | *	@tty: terminal to activate ldisc on | 
|  | 375 | * | 
|  | 376 | *	Set the TTY_LDISC flag when the line discipline can be called | 
| Alan Cox | c9b3976 | 2009-01-02 13:44:56 +0000 | [diff] [blame] | 377 | *	again. Do necessary wakeups for existing sleepers. Clear the LDISC | 
|  | 378 | *	changing flag to indicate any ldisc change is now over. | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 379 | * | 
| Alan Cox | c9b3976 | 2009-01-02 13:44:56 +0000 | [diff] [blame] | 380 | *	Note: nobody should set the TTY_LDISC bit except via this function. | 
|  | 381 | *	Clearing directly is allowed. | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 382 | */ | 
|  | 383 |  | 
|  | 384 | void tty_ldisc_enable(struct tty_struct *tty) | 
|  | 385 | { | 
|  | 386 | set_bit(TTY_LDISC, &tty->flags); | 
| Alan Cox | c9b3976 | 2009-01-02 13:44:56 +0000 | [diff] [blame] | 387 | clear_bit(TTY_LDISC_CHANGING, &tty->flags); | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 388 | wake_up(&tty_ldisc_wait); | 
|  | 389 | } | 
|  | 390 |  | 
|  | 391 | /** | 
|  | 392 | *	tty_set_termios_ldisc		-	set ldisc field | 
|  | 393 | *	@tty: tty structure | 
|  | 394 | *	@num: line discipline number | 
|  | 395 | * | 
|  | 396 | *	This is probably overkill for real world processors but | 
|  | 397 | *	they are not on hot paths so a little discipline won't do | 
|  | 398 | *	any harm. | 
|  | 399 | * | 
|  | 400 | *	Locking: takes termios_mutex | 
|  | 401 | */ | 
|  | 402 |  | 
|  | 403 | static void tty_set_termios_ldisc(struct tty_struct *tty, int num) | 
|  | 404 | { | 
|  | 405 | mutex_lock(&tty->termios_mutex); | 
|  | 406 | tty->termios->c_line = num; | 
|  | 407 | mutex_unlock(&tty->termios_mutex); | 
|  | 408 | } | 
|  | 409 |  | 
|  | 410 |  | 
|  | 411 | /** | 
|  | 412 | *	tty_ldisc_restore	-	helper for tty ldisc change | 
|  | 413 | *	@tty: tty to recover | 
|  | 414 | *	@old: previous ldisc | 
|  | 415 | * | 
|  | 416 | *	Restore the previous line discipline or N_TTY when a line discipline | 
|  | 417 | *	change fails due to an open error | 
|  | 418 | */ | 
|  | 419 |  | 
|  | 420 | static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old) | 
|  | 421 | { | 
|  | 422 | char buf[64]; | 
|  | 423 | struct tty_ldisc new_ldisc; | 
|  | 424 |  | 
|  | 425 | /* There is an outstanding reference here so this is safe */ | 
|  | 426 | tty_ldisc_get(old->ops->num, old); | 
|  | 427 | tty_ldisc_assign(tty, old); | 
|  | 428 | tty_set_termios_ldisc(tty, old->ops->num); | 
|  | 429 | if (old->ops->open && (old->ops->open(tty) < 0)) { | 
|  | 430 | tty_ldisc_put(old->ops); | 
|  | 431 | /* This driver is always present */ | 
|  | 432 | if (tty_ldisc_get(N_TTY, &new_ldisc) < 0) | 
|  | 433 | panic("n_tty: get"); | 
|  | 434 | tty_ldisc_assign(tty, &new_ldisc); | 
|  | 435 | tty_set_termios_ldisc(tty, N_TTY); | 
|  | 436 | if (new_ldisc.ops->open) { | 
|  | 437 | int r = new_ldisc.ops->open(tty); | 
|  | 438 | if (r < 0) | 
|  | 439 | panic("Couldn't open N_TTY ldisc for " | 
|  | 440 | "%s --- error %d.", | 
|  | 441 | tty_name(tty, buf), r); | 
|  | 442 | } | 
|  | 443 | } | 
|  | 444 | } | 
|  | 445 |  | 
|  | 446 | /** | 
|  | 447 | *	tty_set_ldisc		-	set line discipline | 
|  | 448 | *	@tty: the terminal to set | 
|  | 449 | *	@ldisc: the line discipline | 
|  | 450 | * | 
|  | 451 | *	Set the discipline of a tty line. Must be called from a process | 
|  | 452 | *	context. | 
|  | 453 | * | 
|  | 454 | *	Locking: takes tty_ldisc_lock. | 
|  | 455 | *		 called functions take termios_mutex | 
|  | 456 | */ | 
|  | 457 |  | 
|  | 458 | int tty_set_ldisc(struct tty_struct *tty, int ldisc) | 
|  | 459 | { | 
|  | 460 | int retval; | 
|  | 461 | struct tty_ldisc o_ldisc, new_ldisc; | 
|  | 462 | int work; | 
|  | 463 | unsigned long flags; | 
|  | 464 | struct tty_struct *o_tty; | 
|  | 465 |  | 
|  | 466 | restart: | 
|  | 467 | /* This is a bit ugly for now but means we can break the 'ldisc | 
|  | 468 | is part of the tty struct' assumption later */ | 
|  | 469 | retval = tty_ldisc_get(ldisc, &new_ldisc); | 
|  | 470 | if (retval) | 
|  | 471 | return retval; | 
|  | 472 |  | 
|  | 473 | /* | 
|  | 474 | *	Problem: What do we do if this blocks ? | 
|  | 475 | */ | 
|  | 476 |  | 
|  | 477 | tty_wait_until_sent(tty, 0); | 
|  | 478 |  | 
|  | 479 | if (tty->ldisc.ops->num == ldisc) { | 
|  | 480 | tty_ldisc_put(new_ldisc.ops); | 
|  | 481 | return 0; | 
|  | 482 | } | 
|  | 483 |  | 
|  | 484 | /* | 
|  | 485 | *	No more input please, we are switching. The new ldisc | 
|  | 486 | *	will update this value in the ldisc open function | 
|  | 487 | */ | 
|  | 488 |  | 
|  | 489 | tty->receive_room = 0; | 
|  | 490 |  | 
|  | 491 | o_ldisc = tty->ldisc; | 
|  | 492 | o_tty = tty->link; | 
|  | 493 |  | 
|  | 494 | /* | 
|  | 495 | *	Make sure we don't change while someone holds a | 
|  | 496 | *	reference to the line discipline. The TTY_LDISC bit | 
|  | 497 | *	prevents anyone taking a reference once it is clear. | 
|  | 498 | *	We need the lock to avoid racing reference takers. | 
| Alan Cox | c9b3976 | 2009-01-02 13:44:56 +0000 | [diff] [blame] | 499 | * | 
|  | 500 | *	We must clear the TTY_LDISC bit here to avoid a livelock | 
|  | 501 | *	with a userspace app continually trying to use the tty in | 
|  | 502 | *	parallel to the change and re-referencing the tty. | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 503 | */ | 
| Alan Cox | c9b3976 | 2009-01-02 13:44:56 +0000 | [diff] [blame] | 504 | clear_bit(TTY_LDISC, &tty->flags); | 
|  | 505 | if (o_tty) | 
|  | 506 | clear_bit(TTY_LDISC, &o_tty->flags); | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 507 |  | 
|  | 508 | spin_lock_irqsave(&tty_ldisc_lock, flags); | 
|  | 509 | if (tty->ldisc.refcount || (o_tty && o_tty->ldisc.refcount)) { | 
|  | 510 | if (tty->ldisc.refcount) { | 
|  | 511 | /* Free the new ldisc we grabbed. Must drop the lock | 
|  | 512 | first. */ | 
|  | 513 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); | 
|  | 514 | tty_ldisc_put(o_ldisc.ops); | 
|  | 515 | /* | 
|  | 516 | * There are several reasons we may be busy, including | 
|  | 517 | * random momentary I/O traffic. We must therefore | 
|  | 518 | * retry. We could distinguish between blocking ops | 
|  | 519 | * and retries if we made tty_ldisc_wait() smarter. | 
|  | 520 | * That is up for discussion. | 
|  | 521 | */ | 
|  | 522 | if (wait_event_interruptible(tty_ldisc_wait, tty->ldisc.refcount == 0) < 0) | 
|  | 523 | return -ERESTARTSYS; | 
|  | 524 | goto restart; | 
|  | 525 | } | 
|  | 526 | if (o_tty && o_tty->ldisc.refcount) { | 
|  | 527 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); | 
|  | 528 | tty_ldisc_put(o_tty->ldisc.ops); | 
|  | 529 | if (wait_event_interruptible(tty_ldisc_wait, o_tty->ldisc.refcount == 0) < 0) | 
|  | 530 | return -ERESTARTSYS; | 
|  | 531 | goto restart; | 
|  | 532 | } | 
|  | 533 | } | 
|  | 534 | /* | 
|  | 535 | *	If the TTY_LDISC bit is set, then we are racing against | 
|  | 536 | *	another ldisc change | 
|  | 537 | */ | 
| Alan Cox | c9b3976 | 2009-01-02 13:44:56 +0000 | [diff] [blame] | 538 | if (test_bit(TTY_LDISC_CHANGING, &tty->flags)) { | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 539 | struct tty_ldisc *ld; | 
|  | 540 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); | 
|  | 541 | tty_ldisc_put(new_ldisc.ops); | 
|  | 542 | ld = tty_ldisc_ref_wait(tty); | 
|  | 543 | tty_ldisc_deref(ld); | 
|  | 544 | goto restart; | 
|  | 545 | } | 
| Alan Cox | c9b3976 | 2009-01-02 13:44:56 +0000 | [diff] [blame] | 546 | /* | 
|  | 547 | *	This flag is used to avoid two parallel ldisc changes. Once | 
|  | 548 | *	open and close are fine grained locked this may work better | 
|  | 549 | *	as a mutex shared with the open/close/hup paths | 
|  | 550 | */ | 
|  | 551 | set_bit(TTY_LDISC_CHANGING, &tty->flags); | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 552 | if (o_tty) | 
| Alan Cox | c9b3976 | 2009-01-02 13:44:56 +0000 | [diff] [blame] | 553 | set_bit(TTY_LDISC_CHANGING, &o_tty->flags); | 
| Alan Cox | 01e1abb | 2008-07-22 11:16:55 +0100 | [diff] [blame] | 554 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); | 
|  | 555 |  | 
|  | 556 | /* | 
|  | 557 | *	From this point on we know nobody has an ldisc | 
|  | 558 | *	usage reference, nor can they obtain one until | 
|  | 559 | *	we say so later on. | 
|  | 560 | */ | 
|  | 561 |  | 
|  | 562 | work = cancel_delayed_work(&tty->buf.work); | 
|  | 563 | /* | 
|  | 564 | * Wait for ->hangup_work and ->buf.work handlers to terminate | 
|  | 565 | * MUST NOT hold locks here. | 
|  | 566 | */ | 
|  | 567 | flush_scheduled_work(); | 
|  | 568 | /* Shutdown the current discipline. */ | 
|  | 569 | if (o_ldisc.ops->close) | 
|  | 570 | (o_ldisc.ops->close)(tty); | 
|  | 571 |  | 
|  | 572 | /* Now set up the new line discipline. */ | 
|  | 573 | tty_ldisc_assign(tty, &new_ldisc); | 
|  | 574 | tty_set_termios_ldisc(tty, ldisc); | 
|  | 575 | if (new_ldisc.ops->open) | 
|  | 576 | retval = (new_ldisc.ops->open)(tty); | 
|  | 577 | if (retval < 0) { | 
|  | 578 | tty_ldisc_put(new_ldisc.ops); | 
|  | 579 | tty_ldisc_restore(tty, &o_ldisc); | 
|  | 580 | } | 
|  | 581 | /* At this point we hold a reference to the new ldisc and a | 
|  | 582 | a reference to the old ldisc. If we ended up flipping back | 
|  | 583 | to the existing ldisc we have two references to it */ | 
|  | 584 |  | 
|  | 585 | if (tty->ldisc.ops->num != o_ldisc.ops->num && tty->ops->set_ldisc) | 
|  | 586 | tty->ops->set_ldisc(tty); | 
|  | 587 |  | 
|  | 588 | tty_ldisc_put(o_ldisc.ops); | 
|  | 589 |  | 
|  | 590 | /* | 
|  | 591 | *	Allow ldisc referencing to occur as soon as the driver | 
|  | 592 | *	ldisc callback completes. | 
|  | 593 | */ | 
|  | 594 |  | 
|  | 595 | tty_ldisc_enable(tty); | 
|  | 596 | if (o_tty) | 
|  | 597 | tty_ldisc_enable(o_tty); | 
|  | 598 |  | 
|  | 599 | /* Restart it in case no characters kick it off. Safe if | 
|  | 600 | already running */ | 
|  | 601 | if (work) | 
|  | 602 | schedule_delayed_work(&tty->buf.work, 1); | 
|  | 603 | return retval; | 
|  | 604 | } | 
|  | 605 |  | 
|  | 606 |  | 
|  | 607 | /** | 
|  | 608 | *	tty_ldisc_setup			-	open line discipline | 
|  | 609 | *	@tty: tty being shut down | 
|  | 610 | *	@o_tty: pair tty for pty/tty pairs | 
|  | 611 | * | 
|  | 612 | *	Called during the initial open of a tty/pty pair in order to set up the | 
|  | 613 | *	line discplines and bind them to the tty. | 
|  | 614 | */ | 
|  | 615 |  | 
|  | 616 | int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty) | 
|  | 617 | { | 
|  | 618 | struct tty_ldisc *ld = &tty->ldisc; | 
|  | 619 | int retval; | 
|  | 620 |  | 
|  | 621 | if (ld->ops->open) { | 
|  | 622 | retval = (ld->ops->open)(tty); | 
|  | 623 | if (retval) | 
|  | 624 | return retval; | 
|  | 625 | } | 
|  | 626 | if (o_tty && o_tty->ldisc.ops->open) { | 
|  | 627 | retval = (o_tty->ldisc.ops->open)(o_tty); | 
|  | 628 | if (retval) { | 
|  | 629 | if (ld->ops->close) | 
|  | 630 | (ld->ops->close)(tty); | 
|  | 631 | return retval; | 
|  | 632 | } | 
|  | 633 | tty_ldisc_enable(o_tty); | 
|  | 634 | } | 
|  | 635 | tty_ldisc_enable(tty); | 
|  | 636 | return 0; | 
|  | 637 | } | 
|  | 638 |  | 
|  | 639 | /** | 
|  | 640 | *	tty_ldisc_release		-	release line discipline | 
|  | 641 | *	@tty: tty being shut down | 
|  | 642 | *	@o_tty: pair tty for pty/tty pairs | 
|  | 643 | * | 
|  | 644 | *	Called during the final close of a tty/pty pair in order to shut down the | 
|  | 645 | *	line discpline layer. | 
|  | 646 | */ | 
|  | 647 |  | 
|  | 648 | void tty_ldisc_release(struct tty_struct *tty, struct tty_struct *o_tty) | 
|  | 649 | { | 
|  | 650 | unsigned long flags; | 
|  | 651 | struct tty_ldisc ld; | 
|  | 652 | /* | 
|  | 653 | * Prevent flush_to_ldisc() from rescheduling the work for later.  Then | 
|  | 654 | * kill any delayed work. As this is the final close it does not | 
|  | 655 | * race with the set_ldisc code path. | 
|  | 656 | */ | 
|  | 657 | clear_bit(TTY_LDISC, &tty->flags); | 
|  | 658 | cancel_delayed_work(&tty->buf.work); | 
|  | 659 |  | 
|  | 660 | /* | 
|  | 661 | * Wait for ->hangup_work and ->buf.work handlers to terminate | 
|  | 662 | */ | 
|  | 663 |  | 
|  | 664 | flush_scheduled_work(); | 
|  | 665 |  | 
|  | 666 | /* | 
|  | 667 | * Wait for any short term users (we know they are just driver | 
|  | 668 | * side waiters as the file is closing so user count on the file | 
|  | 669 | * side is zero. | 
|  | 670 | */ | 
|  | 671 | spin_lock_irqsave(&tty_ldisc_lock, flags); | 
|  | 672 | while (tty->ldisc.refcount) { | 
|  | 673 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); | 
|  | 674 | wait_event(tty_ldisc_wait, tty->ldisc.refcount == 0); | 
|  | 675 | spin_lock_irqsave(&tty_ldisc_lock, flags); | 
|  | 676 | } | 
|  | 677 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); | 
|  | 678 | /* | 
|  | 679 | * Shutdown the current line discipline, and reset it to N_TTY. | 
|  | 680 | * | 
|  | 681 | * FIXME: this MUST get fixed for the new reflocking | 
|  | 682 | */ | 
|  | 683 | if (tty->ldisc.ops->close) | 
|  | 684 | (tty->ldisc.ops->close)(tty); | 
|  | 685 | tty_ldisc_put(tty->ldisc.ops); | 
|  | 686 |  | 
|  | 687 | /* | 
|  | 688 | *	Switch the line discipline back | 
|  | 689 | */ | 
|  | 690 | WARN_ON(tty_ldisc_get(N_TTY, &ld)); | 
|  | 691 | tty_ldisc_assign(tty, &ld); | 
|  | 692 | tty_set_termios_ldisc(tty, N_TTY); | 
|  | 693 | if (o_tty) { | 
|  | 694 | /* FIXME: could o_tty be in setldisc here ? */ | 
|  | 695 | clear_bit(TTY_LDISC, &o_tty->flags); | 
|  | 696 | if (o_tty->ldisc.ops->close) | 
|  | 697 | (o_tty->ldisc.ops->close)(o_tty); | 
|  | 698 | tty_ldisc_put(o_tty->ldisc.ops); | 
|  | 699 | WARN_ON(tty_ldisc_get(N_TTY, &ld)); | 
|  | 700 | tty_ldisc_assign(o_tty, &ld); | 
|  | 701 | tty_set_termios_ldisc(o_tty, N_TTY); | 
|  | 702 | } | 
|  | 703 | } | 
|  | 704 |  | 
|  | 705 | /** | 
|  | 706 | *	tty_ldisc_init		-	ldisc setup for new tty | 
|  | 707 | *	@tty: tty being allocated | 
|  | 708 | * | 
|  | 709 | *	Set up the line discipline objects for a newly allocated tty. Note that | 
|  | 710 | *	the tty structure is not completely set up when this call is made. | 
|  | 711 | */ | 
|  | 712 |  | 
|  | 713 | void tty_ldisc_init(struct tty_struct *tty) | 
|  | 714 | { | 
|  | 715 | struct tty_ldisc ld; | 
|  | 716 | if (tty_ldisc_get(N_TTY, &ld) < 0) | 
|  | 717 | panic("n_tty: init_tty"); | 
|  | 718 | tty_ldisc_assign(tty, &ld); | 
|  | 719 | } | 
|  | 720 |  | 
|  | 721 | void tty_ldisc_begin(void) | 
|  | 722 | { | 
|  | 723 | /* Setup the default TTY line discipline. */ | 
|  | 724 | (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY); | 
|  | 725 | } |