blob: a58a19a6a5e102b52c60ea1000086b90640ed21d [file] [log] [blame]
Alan Cox01e1abb2008-07-22 11:16:55 +01001#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 Cox01e1abb2008-07-22 11:16:55 +010013#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
47static DEFINE_SPINLOCK(tty_ldisc_lock);
48static DECLARE_WAIT_QUEUE_HEAD(tty_ldisc_wait);
49/* Line disc dispatch table */
50static 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
65int 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}
81EXPORT_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
95int 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}
112EXPORT_SYMBOL(tty_unregister_ldisc);
113
114
115/**
116 * tty_ldisc_try_get - try and reference an ldisc
117 * @disc: ldisc number
Alan Cox01e1abb2008-07-22 11:16:55 +0100118 *
119 * Attempt to open and lock a line discipline into place. Return
Alan Coxc65c9bc2009-06-11 12:50:12 +0100120 * the line discipline refcounted or an error.
Alan Cox01e1abb2008-07-22 11:16:55 +0100121 */
122
Alan Coxc65c9bc2009-06-11 12:50:12 +0100123static struct tty_ldisc *tty_ldisc_try_get(int disc)
Alan Cox01e1abb2008-07-22 11:16:55 +0100124{
125 unsigned long flags;
Alan Coxc65c9bc2009-06-11 12:50:12 +0100126 struct tty_ldisc *ld;
Alan Cox01e1abb2008-07-22 11:16:55 +0100127 struct tty_ldisc_ops *ldops;
128 int err = -EINVAL;
129
Alan Coxc65c9bc2009-06-11 12:50:12 +0100130 ld = kmalloc(sizeof(struct tty_ldisc), GFP_KERNEL);
131 if (ld == NULL)
132 return ERR_PTR(-ENOMEM);
133
Alan Cox01e1abb2008-07-22 11:16:55 +0100134 spin_lock_irqsave(&tty_ldisc_lock, flags);
135 ld->ops = NULL;
136 ldops = tty_ldiscs[disc];
137 /* Check the entry is defined */
138 if (ldops) {
139 /* If the module is being unloaded we can't use it */
140 if (!try_module_get(ldops->owner))
141 err = -EAGAIN;
142 else {
143 /* lock it */
144 ldops->refcount++;
145 ld->ops = ldops;
Alan Coxc65c9bc2009-06-11 12:50:12 +0100146 ld->refcount = 0;
Alan Cox01e1abb2008-07-22 11:16:55 +0100147 err = 0;
148 }
149 }
150 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100151 if (err)
152 return ERR_PTR(err);
153 return ld;
Alan Cox01e1abb2008-07-22 11:16:55 +0100154}
155
156/**
157 * tty_ldisc_get - take a reference to an ldisc
158 * @disc: ldisc number
Alan Cox01e1abb2008-07-22 11:16:55 +0100159 *
160 * Takes a reference to a line discipline. Deals with refcounts and
161 * module locking counts. Returns NULL if the discipline is not available.
162 * Returns a pointer to the discipline and bumps the ref count if it is
163 * available
164 *
165 * Locking:
166 * takes tty_ldisc_lock to guard against ldisc races
167 */
168
Alan Coxc65c9bc2009-06-11 12:50:12 +0100169static struct tty_ldisc *tty_ldisc_get(int disc)
Alan Cox01e1abb2008-07-22 11:16:55 +0100170{
Alan Coxc65c9bc2009-06-11 12:50:12 +0100171 struct tty_ldisc *ld;
Alan Cox01e1abb2008-07-22 11:16:55 +0100172
173 if (disc < N_TTY || disc >= NR_LDISCS)
Alan Coxc65c9bc2009-06-11 12:50:12 +0100174 return ERR_PTR(-EINVAL);
175 ld = tty_ldisc_try_get(disc);
176 if (IS_ERR(ld)) {
Alan Cox01e1abb2008-07-22 11:16:55 +0100177 request_module("tty-ldisc-%d", disc);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100178 ld = tty_ldisc_try_get(disc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100179 }
Alan Coxc65c9bc2009-06-11 12:50:12 +0100180 return ld;
Alan Cox01e1abb2008-07-22 11:16:55 +0100181}
182
183/**
184 * tty_ldisc_put - drop ldisc reference
Alan Coxc65c9bc2009-06-11 12:50:12 +0100185 * @ld: ldisc
Alan Cox01e1abb2008-07-22 11:16:55 +0100186 *
187 * Drop a reference to a line discipline. Manage refcounts and
Alan Coxc65c9bc2009-06-11 12:50:12 +0100188 * module usage counts. Free the ldisc once the recount hits zero.
Alan Cox01e1abb2008-07-22 11:16:55 +0100189 *
190 * Locking:
191 * takes tty_ldisc_lock to guard against ldisc races
192 */
193
Alan Coxc65c9bc2009-06-11 12:50:12 +0100194static void tty_ldisc_put(struct tty_ldisc *ld)
Alan Cox01e1abb2008-07-22 11:16:55 +0100195{
196 unsigned long flags;
Alan Coxc65c9bc2009-06-11 12:50:12 +0100197 int disc = ld->ops->num;
198 struct tty_ldisc_ops *ldo;
Alan Cox01e1abb2008-07-22 11:16:55 +0100199
200 BUG_ON(disc < N_TTY || disc >= NR_LDISCS);
201
202 spin_lock_irqsave(&tty_ldisc_lock, flags);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100203 ldo = tty_ldiscs[disc];
204 BUG_ON(ldo->refcount == 0);
205 ldo->refcount--;
206 module_put(ldo->owner);
Alan Cox01e1abb2008-07-22 11:16:55 +0100207 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100208 kfree(ld);
Alan Cox01e1abb2008-07-22 11:16:55 +0100209}
210
211static void * tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos)
212{
213 return (*pos < NR_LDISCS) ? pos : NULL;
214}
215
216static void * tty_ldiscs_seq_next(struct seq_file *m, void *v, loff_t *pos)
217{
218 (*pos)++;
219 return (*pos < NR_LDISCS) ? pos : NULL;
220}
221
222static void tty_ldiscs_seq_stop(struct seq_file *m, void *v)
223{
224}
225
226static int tty_ldiscs_seq_show(struct seq_file *m, void *v)
227{
228 int i = *(loff_t *)v;
Alan Coxc65c9bc2009-06-11 12:50:12 +0100229 struct tty_ldisc *ld;
Alan Cox01e1abb2008-07-22 11:16:55 +0100230
Alan Coxc65c9bc2009-06-11 12:50:12 +0100231 ld = tty_ldisc_try_get(i);
232 if (IS_ERR(ld))
Alan Cox01e1abb2008-07-22 11:16:55 +0100233 return 0;
Alan Coxc65c9bc2009-06-11 12:50:12 +0100234 seq_printf(m, "%-10s %2d\n", ld->ops->name ? ld->ops->name : "???", i);
235 tty_ldisc_put(ld);
Alan Cox01e1abb2008-07-22 11:16:55 +0100236 return 0;
237}
238
239static const struct seq_operations tty_ldiscs_seq_ops = {
240 .start = tty_ldiscs_seq_start,
241 .next = tty_ldiscs_seq_next,
242 .stop = tty_ldiscs_seq_stop,
243 .show = tty_ldiscs_seq_show,
244};
245
246static int proc_tty_ldiscs_open(struct inode *inode, struct file *file)
247{
248 return seq_open(file, &tty_ldiscs_seq_ops);
249}
250
251const struct file_operations tty_ldiscs_proc_fops = {
252 .owner = THIS_MODULE,
253 .open = proc_tty_ldiscs_open,
254 .read = seq_read,
255 .llseek = seq_lseek,
256 .release = seq_release,
257};
258
259/**
260 * tty_ldisc_assign - set ldisc on a tty
261 * @tty: tty to assign
262 * @ld: line discipline
263 *
264 * Install an instance of a line discipline into a tty structure. The
265 * ldisc must have a reference count above zero to ensure it remains/
266 * The tty instance refcount starts at zero.
267 *
268 * Locking:
269 * Caller must hold references
270 */
271
272static void tty_ldisc_assign(struct tty_struct *tty, struct tty_ldisc *ld)
273{
Alan Coxc65c9bc2009-06-11 12:50:12 +0100274 tty->ldisc = ld;
Alan Cox01e1abb2008-07-22 11:16:55 +0100275}
276
277/**
278 * tty_ldisc_try - internal helper
279 * @tty: the tty
280 *
281 * Make a single attempt to grab and bump the refcount on
282 * the tty ldisc. Return 0 on failure or 1 on success. This is
283 * used to implement both the waiting and non waiting versions
284 * of tty_ldisc_ref
285 *
286 * Locking: takes tty_ldisc_lock
287 */
288
289static int tty_ldisc_try(struct tty_struct *tty)
290{
291 unsigned long flags;
292 struct tty_ldisc *ld;
293 int ret = 0;
294
295 spin_lock_irqsave(&tty_ldisc_lock, flags);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100296 ld = tty->ldisc;
Alan Cox01e1abb2008-07-22 11:16:55 +0100297 if (test_bit(TTY_LDISC, &tty->flags)) {
298 ld->refcount++;
299 ret = 1;
300 }
301 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
302 return ret;
303}
304
305/**
306 * tty_ldisc_ref_wait - wait for the tty ldisc
307 * @tty: tty device
308 *
309 * Dereference the line discipline for the terminal and take a
310 * reference to it. If the line discipline is in flux then
311 * wait patiently until it changes.
312 *
313 * Note: Must not be called from an IRQ/timer context. The caller
314 * must also be careful not to hold other locks that will deadlock
315 * against a discipline change, such as an existing ldisc reference
316 * (which we check for)
317 *
318 * Locking: call functions take tty_ldisc_lock
319 */
320
321struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty)
322{
323 /* wait_event is a macro */
324 wait_event(tty_ldisc_wait, tty_ldisc_try(tty));
Alan Coxc65c9bc2009-06-11 12:50:12 +0100325 WARN_ON(tty->ldisc->refcount == 0);
326 return tty->ldisc;
Alan Cox01e1abb2008-07-22 11:16:55 +0100327}
328
329EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait);
330
331/**
332 * tty_ldisc_ref - get the tty ldisc
333 * @tty: tty device
334 *
335 * Dereference the line discipline for the terminal and take a
336 * reference to it. If the line discipline is in flux then
337 * return NULL. Can be called from IRQ and timer functions.
338 *
339 * Locking: called functions take tty_ldisc_lock
340 */
341
342struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty)
343{
344 if (tty_ldisc_try(tty))
Alan Coxc65c9bc2009-06-11 12:50:12 +0100345 return tty->ldisc;
Alan Cox01e1abb2008-07-22 11:16:55 +0100346 return NULL;
347}
348
349EXPORT_SYMBOL_GPL(tty_ldisc_ref);
350
351/**
352 * tty_ldisc_deref - free a tty ldisc reference
353 * @ld: reference to free up
354 *
355 * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May
356 * be called in IRQ context.
357 *
358 * Locking: takes tty_ldisc_lock
359 */
360
361void tty_ldisc_deref(struct tty_ldisc *ld)
362{
363 unsigned long flags;
364
365 BUG_ON(ld == NULL);
366
367 spin_lock_irqsave(&tty_ldisc_lock, flags);
368 if (ld->refcount == 0)
369 printk(KERN_ERR "tty_ldisc_deref: no references.\n");
370 else
371 ld->refcount--;
372 if (ld->refcount == 0)
373 wake_up(&tty_ldisc_wait);
374 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
375}
376
377EXPORT_SYMBOL_GPL(tty_ldisc_deref);
378
379/**
380 * tty_ldisc_enable - allow ldisc use
381 * @tty: terminal to activate ldisc on
382 *
383 * Set the TTY_LDISC flag when the line discipline can be called
Alan Coxc9b39762009-01-02 13:44:56 +0000384 * again. Do necessary wakeups for existing sleepers. Clear the LDISC
385 * changing flag to indicate any ldisc change is now over.
Alan Cox01e1abb2008-07-22 11:16:55 +0100386 *
Alan Coxc9b39762009-01-02 13:44:56 +0000387 * Note: nobody should set the TTY_LDISC bit except via this function.
388 * Clearing directly is allowed.
Alan Cox01e1abb2008-07-22 11:16:55 +0100389 */
390
391void tty_ldisc_enable(struct tty_struct *tty)
392{
393 set_bit(TTY_LDISC, &tty->flags);
Alan Coxc9b39762009-01-02 13:44:56 +0000394 clear_bit(TTY_LDISC_CHANGING, &tty->flags);
Alan Cox01e1abb2008-07-22 11:16:55 +0100395 wake_up(&tty_ldisc_wait);
396}
397
398/**
399 * tty_set_termios_ldisc - set ldisc field
400 * @tty: tty structure
401 * @num: line discipline number
402 *
403 * This is probably overkill for real world processors but
404 * they are not on hot paths so a little discipline won't do
405 * any harm.
406 *
407 * Locking: takes termios_mutex
408 */
409
410static void tty_set_termios_ldisc(struct tty_struct *tty, int num)
411{
412 mutex_lock(&tty->termios_mutex);
413 tty->termios->c_line = num;
414 mutex_unlock(&tty->termios_mutex);
415}
416
Alan Coxc65c9bc2009-06-11 12:50:12 +0100417/**
418 * tty_ldisc_open - open a line discipline
419 * @tty: tty we are opening the ldisc on
420 * @ld: discipline to open
421 *
422 * A helper opening method. Also a convenient debugging and check
423 * point.
424 */
425
426static int tty_ldisc_open(struct tty_struct *tty, struct tty_ldisc *ld)
427{
428 WARN_ON(test_and_set_bit(TTY_LDISC_OPEN, &tty->flags));
429 if (ld->ops->open)
430 return ld->ops->open(tty);
431 return 0;
432}
433
434/**
435 * tty_ldisc_close - close a line discipline
436 * @tty: tty we are opening the ldisc on
437 * @ld: discipline to close
438 *
439 * A helper close method. Also a convenient debugging and check
440 * point.
441 */
442
443static void tty_ldisc_close(struct tty_struct *tty, struct tty_ldisc *ld)
444{
445 WARN_ON(!test_bit(TTY_LDISC_OPEN, &tty->flags));
446 clear_bit(TTY_LDISC_OPEN, &tty->flags);
447 if (ld->ops->close)
448 ld->ops->close(tty);
449}
Alan Cox01e1abb2008-07-22 11:16:55 +0100450
451/**
452 * tty_ldisc_restore - helper for tty ldisc change
453 * @tty: tty to recover
454 * @old: previous ldisc
455 *
456 * Restore the previous line discipline or N_TTY when a line discipline
457 * change fails due to an open error
458 */
459
460static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old)
461{
462 char buf[64];
Alan Coxc65c9bc2009-06-11 12:50:12 +0100463 struct tty_ldisc *new_ldisc;
464 int r;
Alan Cox01e1abb2008-07-22 11:16:55 +0100465
466 /* There is an outstanding reference here so this is safe */
Alan Coxc65c9bc2009-06-11 12:50:12 +0100467 old = tty_ldisc_get(old->ops->num);
468 WARN_ON(IS_ERR(old));
Alan Cox01e1abb2008-07-22 11:16:55 +0100469 tty_ldisc_assign(tty, old);
470 tty_set_termios_ldisc(tty, old->ops->num);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100471 if (tty_ldisc_open(tty, old) < 0) {
472 tty_ldisc_put(old);
Alan Cox01e1abb2008-07-22 11:16:55 +0100473 /* This driver is always present */
Alan Coxc65c9bc2009-06-11 12:50:12 +0100474 new_ldisc =tty_ldisc_get(N_TTY);
475 if (IS_ERR(new_ldisc))
Alan Cox01e1abb2008-07-22 11:16:55 +0100476 panic("n_tty: get");
Alan Coxc65c9bc2009-06-11 12:50:12 +0100477 tty_ldisc_assign(tty, new_ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100478 tty_set_termios_ldisc(tty, N_TTY);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100479 r = tty_ldisc_open(tty, new_ldisc);
480 if (r < 0)
481 panic("Couldn't open N_TTY ldisc for "
482 "%s --- error %d.",
483 tty_name(tty, buf), r);
Alan Cox01e1abb2008-07-22 11:16:55 +0100484 }
485}
486
487/**
Alan Coxc65c9bc2009-06-11 12:50:12 +0100488 * tty_ldisc_halt - shut down the line discipline
Alan Coxe8b70e72009-06-11 12:48:02 +0100489 * @tty: tty device
490 *
491 * Shut down the line discipline and work queue for this tty device.
492 * The TTY_LDISC flag being cleared ensures no further references can
493 * be obtained while the delayed work queue halt ensures that no more
494 * data is fed to the ldisc.
495 *
496 * In order to wait for any existing references to complete see
497 * tty_ldisc_wait_idle.
498 */
499
Alan Coxc65c9bc2009-06-11 12:50:12 +0100500static int tty_ldisc_halt(struct tty_struct *tty)
Alan Coxe8b70e72009-06-11 12:48:02 +0100501{
502 clear_bit(TTY_LDISC, &tty->flags);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100503 return cancel_delayed_work(&tty->buf.work);
Alan Coxe8b70e72009-06-11 12:48:02 +0100504}
505
506/**
507 * tty_ldisc_wait_idle - wait for the ldisc to become idle
508 * @tty: tty to wait for
509 *
510 * Wait for the line discipline to become idle. The discipline must
511 * have been halted for this to guarantee it remains idle.
512 *
Alan Coxc65c9bc2009-06-11 12:50:12 +0100513 * tty_ldisc_lock protects the ref counts currently.
Alan Coxe8b70e72009-06-11 12:48:02 +0100514 */
515
Alan Coxc65c9bc2009-06-11 12:50:12 +0100516static int tty_ldisc_wait_idle(struct tty_struct *tty)
Alan Coxe8b70e72009-06-11 12:48:02 +0100517{
518 unsigned long flags;
519 spin_lock_irqsave(&tty_ldisc_lock, flags);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100520 while (tty->ldisc->refcount) {
Alan Coxe8b70e72009-06-11 12:48:02 +0100521 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100522 if (wait_event_timeout(tty_ldisc_wait,
523 tty->ldisc->refcount == 0, 5 * HZ) == 0)
524 return -EBUSY;
Alan Coxe8b70e72009-06-11 12:48:02 +0100525 spin_lock_irqsave(&tty_ldisc_lock, flags);
526 }
527 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100528 return 0;
Alan Coxe8b70e72009-06-11 12:48:02 +0100529}
530
531/**
Alan Cox01e1abb2008-07-22 11:16:55 +0100532 * tty_set_ldisc - set line discipline
533 * @tty: the terminal to set
534 * @ldisc: the line discipline
535 *
536 * Set the discipline of a tty line. Must be called from a process
Alan Coxc65c9bc2009-06-11 12:50:12 +0100537 * context. The ldisc change logic has to protect itself against any
538 * overlapping ldisc change (including on the other end of pty pairs),
539 * the close of one side of a tty/pty pair, and eventually hangup.
Alan Cox01e1abb2008-07-22 11:16:55 +0100540 *
Alan Coxc65c9bc2009-06-11 12:50:12 +0100541 * Locking: takes tty_ldisc_lock, termios_mutex
Alan Cox01e1abb2008-07-22 11:16:55 +0100542 */
543
544int tty_set_ldisc(struct tty_struct *tty, int ldisc)
545{
546 int retval;
Alan Coxc65c9bc2009-06-11 12:50:12 +0100547 struct tty_ldisc *o_ldisc, *new_ldisc;
548 int work, o_work = 0;
Alan Cox01e1abb2008-07-22 11:16:55 +0100549 struct tty_struct *o_tty;
550
Alan Coxc65c9bc2009-06-11 12:50:12 +0100551 new_ldisc = tty_ldisc_get(ldisc);
552 if (IS_ERR(new_ldisc))
553 return PTR_ERR(new_ldisc);
554
555 /*
556 * We need to look at the tty locking here for pty/tty pairs
557 * when both sides try to change in parallel.
558 */
559
560 o_tty = tty->link; /* o_tty is the pty side or NULL */
561
562
563 /*
564 * Check the no-op case
565 */
566
567 if (tty->ldisc->ops->num == ldisc) {
568 tty_ldisc_put(new_ldisc);
569 return 0;
570 }
Alan Cox01e1abb2008-07-22 11:16:55 +0100571
572 /*
573 * Problem: What do we do if this blocks ?
Alan Coxc65c9bc2009-06-11 12:50:12 +0100574 * We could deadlock here
Alan Cox01e1abb2008-07-22 11:16:55 +0100575 */
576
577 tty_wait_until_sent(tty, 0);
578
Alan Coxc65c9bc2009-06-11 12:50:12 +0100579 mutex_lock(&tty->ldisc_mutex);
Alan Cox01e1abb2008-07-22 11:16:55 +0100580
581 /*
Alan Coxc65c9bc2009-06-11 12:50:12 +0100582 * We could be midstream of another ldisc change which has
583 * dropped the lock during processing. If so we need to wait.
584 */
585
586 while (test_bit(TTY_LDISC_CHANGING, &tty->flags)) {
587 mutex_unlock(&tty->ldisc_mutex);
588 wait_event(tty_ldisc_wait,
589 test_bit(TTY_LDISC_CHANGING, &tty->flags) == 0);
590 mutex_lock(&tty->ldisc_mutex);
591 }
592 set_bit(TTY_LDISC_CHANGING, &tty->flags);
593
594 /*
Alan Cox01e1abb2008-07-22 11:16:55 +0100595 * No more input please, we are switching. The new ldisc
596 * will update this value in the ldisc open function
597 */
598
599 tty->receive_room = 0;
600
601 o_ldisc = tty->ldisc;
Alan Cox01e1abb2008-07-22 11:16:55 +0100602 /*
603 * Make sure we don't change while someone holds a
604 * reference to the line discipline. The TTY_LDISC bit
605 * prevents anyone taking a reference once it is clear.
606 * We need the lock to avoid racing reference takers.
Alan Coxc9b39762009-01-02 13:44:56 +0000607 *
608 * We must clear the TTY_LDISC bit here to avoid a livelock
609 * with a userspace app continually trying to use the tty in
610 * parallel to the change and re-referencing the tty.
Alan Cox01e1abb2008-07-22 11:16:55 +0100611 */
612
Alan Coxc65c9bc2009-06-11 12:50:12 +0100613 work = tty_ldisc_halt(tty);
Alan Cox01e1abb2008-07-22 11:16:55 +0100614 if (o_tty)
Alan Coxc65c9bc2009-06-11 12:50:12 +0100615 o_work = tty_ldisc_halt(o_tty);
616
Alan Cox01e1abb2008-07-22 11:16:55 +0100617 /*
Alan Coxc65c9bc2009-06-11 12:50:12 +0100618 * Wait for ->hangup_work and ->buf.work handlers to terminate.
619 * We must drop the mutex here in case a hangup is also in process.
Alan Cox01e1abb2008-07-22 11:16:55 +0100620 */
621
Alan Coxc65c9bc2009-06-11 12:50:12 +0100622 mutex_unlock(&tty->ldisc_mutex);
623
Alan Cox01e1abb2008-07-22 11:16:55 +0100624 flush_scheduled_work();
Alan Coxc65c9bc2009-06-11 12:50:12 +0100625
626 /* Let any existing reference holders finish */
627 retval = tty_ldisc_wait_idle(tty);
628 if (retval < 0) {
629 clear_bit(TTY_LDISC_CHANGING, &tty->flags);
630 tty_ldisc_put(new_ldisc);
631 return retval;
632 }
633
634 mutex_lock(&tty->ldisc_mutex);
635 if (test_bit(TTY_HUPPED, &tty->flags)) {
636 /* We were raced by the hangup method. It will have stomped
637 the ldisc data and closed the ldisc down */
638 clear_bit(TTY_LDISC_CHANGING, &tty->flags);
639 mutex_unlock(&tty->ldisc_mutex);
640 tty_ldisc_put(new_ldisc);
641 return -EIO;
642 }
643
Alan Cox01e1abb2008-07-22 11:16:55 +0100644 /* Shutdown the current discipline. */
Alan Coxc65c9bc2009-06-11 12:50:12 +0100645 tty_ldisc_close(tty, o_ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100646
647 /* Now set up the new line discipline. */
Alan Coxc65c9bc2009-06-11 12:50:12 +0100648 tty_ldisc_assign(tty, new_ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100649 tty_set_termios_ldisc(tty, ldisc);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100650
651 retval = tty_ldisc_open(tty, new_ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100652 if (retval < 0) {
Alan Coxc65c9bc2009-06-11 12:50:12 +0100653 /* Back to the old one or N_TTY if we can't */
654 tty_ldisc_put(new_ldisc);
655 tty_ldisc_restore(tty, o_ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100656 }
Alan Coxc65c9bc2009-06-11 12:50:12 +0100657
Alan Cox01e1abb2008-07-22 11:16:55 +0100658 /* At this point we hold a reference to the new ldisc and a
659 a reference to the old ldisc. If we ended up flipping back
660 to the existing ldisc we have two references to it */
661
Alan Coxc65c9bc2009-06-11 12:50:12 +0100662 if (tty->ldisc->ops->num != o_ldisc->ops->num && tty->ops->set_ldisc)
Alan Cox01e1abb2008-07-22 11:16:55 +0100663 tty->ops->set_ldisc(tty);
664
Alan Coxc65c9bc2009-06-11 12:50:12 +0100665 tty_ldisc_put(o_ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100666
667 /*
Alan Coxc65c9bc2009-06-11 12:50:12 +0100668 * Allow ldisc referencing to occur again
Alan Cox01e1abb2008-07-22 11:16:55 +0100669 */
670
671 tty_ldisc_enable(tty);
672 if (o_tty)
673 tty_ldisc_enable(o_tty);
674
Alan Coxc65c9bc2009-06-11 12:50:12 +0100675 /* Restart the work queue in case no characters kick it off. Safe if
Alan Cox01e1abb2008-07-22 11:16:55 +0100676 already running */
677 if (work)
678 schedule_delayed_work(&tty->buf.work, 1);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100679 if (o_work)
680 schedule_delayed_work(&o_tty->buf.work, 1);
681 mutex_unlock(&tty->ldisc_mutex);
Alan Cox01e1abb2008-07-22 11:16:55 +0100682 return retval;
683}
684
Alan Coxc65c9bc2009-06-11 12:50:12 +0100685/**
686 * tty_reset_termios - reset terminal state
687 * @tty: tty to reset
688 *
689 * Restore a terminal to the driver default state.
690 */
691
692static void tty_reset_termios(struct tty_struct *tty)
693{
694 mutex_lock(&tty->termios_mutex);
695 *tty->termios = tty->driver->init_termios;
696 tty->termios->c_ispeed = tty_termios_input_baud_rate(tty->termios);
697 tty->termios->c_ospeed = tty_termios_baud_rate(tty->termios);
698 mutex_unlock(&tty->termios_mutex);
699}
700
701
702/**
703 * tty_ldisc_reinit - reinitialise the tty ldisc
704 * @tty: tty to reinit
705 *
706 * Switch the tty back to N_TTY line discipline and leave the
707 * ldisc state closed
708 */
709
710static void tty_ldisc_reinit(struct tty_struct *tty)
711{
712 struct tty_ldisc *ld;
713
714 tty_ldisc_close(tty, tty->ldisc);
715 tty_ldisc_put(tty->ldisc);
716 tty->ldisc = NULL;
717 /*
718 * Switch the line discipline back
719 */
720 ld = tty_ldisc_get(N_TTY);
721 BUG_ON(IS_ERR(ld));
722 tty_ldisc_assign(tty, ld);
723 tty_set_termios_ldisc(tty, N_TTY);
724}
725
726/**
727 * tty_ldisc_hangup - hangup ldisc reset
728 * @tty: tty being hung up
729 *
730 * Some tty devices reset their termios when they receive a hangup
731 * event. In that situation we must also switch back to N_TTY properly
732 * before we reset the termios data.
733 *
734 * Locking: We can take the ldisc mutex as the rest of the code is
735 * careful to allow for this.
736 *
737 * In the pty pair case this occurs in the close() path of the
738 * tty itself so we must be careful about locking rules.
739 */
740
741void tty_ldisc_hangup(struct tty_struct *tty)
742{
743 struct tty_ldisc *ld;
744
745 /*
746 * FIXME! What are the locking issues here? This may me overdoing
747 * things... This question is especially important now that we've
748 * removed the irqlock.
749 */
750 ld = tty_ldisc_ref(tty);
751 if (ld != NULL) {
752 /* We may have no line discipline at this point */
753 if (ld->ops->flush_buffer)
754 ld->ops->flush_buffer(tty);
755 tty_driver_flush_buffer(tty);
756 if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
757 ld->ops->write_wakeup)
758 ld->ops->write_wakeup(tty);
759 if (ld->ops->hangup)
760 ld->ops->hangup(tty);
761 tty_ldisc_deref(ld);
762 }
763 /*
764 * FIXME: Once we trust the LDISC code better we can wait here for
765 * ldisc completion and fix the driver call race
766 */
767 wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
768 wake_up_interruptible_poll(&tty->read_wait, POLLIN);
769 /*
770 * Shutdown the current line discipline, and reset it to
771 * N_TTY.
772 */
773 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) {
774 /* Avoid racing set_ldisc */
775 mutex_lock(&tty->ldisc_mutex);
776 /* Switch back to N_TTY */
777 tty_ldisc_reinit(tty);
778 /* At this point we have a closed ldisc and we want to
779 reopen it. We could defer this to the next open but
780 it means auditing a lot of other paths so this is a FIXME */
781 WARN_ON(tty_ldisc_open(tty, tty->ldisc));
782 tty_ldisc_enable(tty);
783 mutex_unlock(&tty->ldisc_mutex);
784 tty_reset_termios(tty);
785 }
786}
Alan Cox01e1abb2008-07-22 11:16:55 +0100787
788/**
789 * tty_ldisc_setup - open line discipline
790 * @tty: tty being shut down
791 * @o_tty: pair tty for pty/tty pairs
792 *
793 * Called during the initial open of a tty/pty pair in order to set up the
Alan Coxc65c9bc2009-06-11 12:50:12 +0100794 * line disciplines and bind them to the tty. This has no locking issues
795 * as the device isn't yet active.
Alan Cox01e1abb2008-07-22 11:16:55 +0100796 */
797
798int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty)
799{
Alan Coxc65c9bc2009-06-11 12:50:12 +0100800 struct tty_ldisc *ld = tty->ldisc;
Alan Cox01e1abb2008-07-22 11:16:55 +0100801 int retval;
802
Alan Coxc65c9bc2009-06-11 12:50:12 +0100803 retval = tty_ldisc_open(tty, ld);
804 if (retval)
805 return retval;
806
807 if (o_tty) {
808 retval = tty_ldisc_open(o_tty, o_tty->ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100809 if (retval) {
Alan Coxc65c9bc2009-06-11 12:50:12 +0100810 tty_ldisc_close(tty, ld);
Alan Cox01e1abb2008-07-22 11:16:55 +0100811 return retval;
812 }
813 tty_ldisc_enable(o_tty);
814 }
815 tty_ldisc_enable(tty);
816 return 0;
817}
Alan Cox01e1abb2008-07-22 11:16:55 +0100818/**
819 * tty_ldisc_release - release line discipline
820 * @tty: tty being shut down
821 * @o_tty: pair tty for pty/tty pairs
822 *
823 * Called during the final close of a tty/pty pair in order to shut down the
Alan Coxc65c9bc2009-06-11 12:50:12 +0100824 * line discpline layer. On exit the ldisc assigned is N_TTY and the
825 * ldisc has not been opened.
Alan Cox01e1abb2008-07-22 11:16:55 +0100826 */
827
828void tty_ldisc_release(struct tty_struct *tty, struct tty_struct *o_tty)
829{
Alan Cox01e1abb2008-07-22 11:16:55 +0100830 /*
831 * Prevent flush_to_ldisc() from rescheduling the work for later. Then
832 * kill any delayed work. As this is the final close it does not
833 * race with the set_ldisc code path.
834 */
Alan Cox01e1abb2008-07-22 11:16:55 +0100835
Alan Coxe8b70e72009-06-11 12:48:02 +0100836 tty_ldisc_halt(tty);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100837 flush_scheduled_work();
Alan Cox01e1abb2008-07-22 11:16:55 +0100838
839 /*
840 * Wait for any short term users (we know they are just driver
841 * side waiters as the file is closing so user count on the file
842 * side is zero.
843 */
Alan Coxe8b70e72009-06-11 12:48:02 +0100844
845 tty_ldisc_wait_idle(tty);
846
Alan Cox01e1abb2008-07-22 11:16:55 +0100847 /*
848 * Shutdown the current line discipline, and reset it to N_TTY.
849 *
850 * FIXME: this MUST get fixed for the new reflocking
851 */
Alan Cox01e1abb2008-07-22 11:16:55 +0100852
Alan Coxe8b70e72009-06-11 12:48:02 +0100853 tty_ldisc_reinit(tty);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100854 /* This will need doing differently if we need to lock */
855 if (o_tty)
856 tty_ldisc_release(o_tty, NULL);
Alan Cox01e1abb2008-07-22 11:16:55 +0100857}
858
859/**
860 * tty_ldisc_init - ldisc setup for new tty
861 * @tty: tty being allocated
862 *
863 * Set up the line discipline objects for a newly allocated tty. Note that
864 * the tty structure is not completely set up when this call is made.
865 */
866
867void tty_ldisc_init(struct tty_struct *tty)
868{
Alan Coxc65c9bc2009-06-11 12:50:12 +0100869 struct tty_ldisc *ld = tty_ldisc_get(N_TTY);
870 if (IS_ERR(ld))
Alan Cox01e1abb2008-07-22 11:16:55 +0100871 panic("n_tty: init_tty");
Alan Coxc65c9bc2009-06-11 12:50:12 +0100872 tty_ldisc_assign(tty, ld);
Alan Cox01e1abb2008-07-22 11:16:55 +0100873}
874
875void tty_ldisc_begin(void)
876{
877 /* Setup the default TTY line discipline. */
878 (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);
879}