blob: be55dfcf59ac68d496fb54cdd9769601455af304 [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>
Alan Cox01e1abb2008-07-22 11:16:55 +010024#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
46static DEFINE_SPINLOCK(tty_ldisc_lock);
47static DECLARE_WAIT_QUEUE_HEAD(tty_ldisc_wait);
48/* Line disc dispatch table */
49static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS];
50
Linus Torvalds65b77042009-08-03 11:11:19 -070051static inline struct tty_ldisc *get_ldisc(struct tty_ldisc *ld)
52{
53 if (ld)
54 atomic_inc(&ld->users);
55 return ld;
56}
57
58static inline void put_ldisc(struct tty_ldisc *ld)
59{
60 if (WARN_ON_ONCE(!ld))
61 return;
62
63 /*
64 * If this is the last user, free the ldisc, and
65 * release the ldisc ops.
66 */
67 if (atomic_dec_and_test(&ld->users)) {
68 unsigned long flags;
69 struct tty_ldisc_ops *ldo = ld->ops;
70
71 kfree(ld);
72 spin_lock_irqsave(&tty_ldisc_lock, flags);
73 ldo->refcount--;
74 module_put(ldo->owner);
75 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
76 }
77}
78
Alan Cox01e1abb2008-07-22 11:16:55 +010079/**
80 * tty_register_ldisc - install a line discipline
81 * @disc: ldisc number
82 * @new_ldisc: pointer to the ldisc object
83 *
84 * Installs a new line discipline into the kernel. The discipline
85 * is set up as unreferenced and then made available to the kernel
86 * from this point onwards.
87 *
88 * Locking:
89 * takes tty_ldisc_lock to guard against ldisc races
90 */
91
92int tty_register_ldisc(int disc, struct tty_ldisc_ops *new_ldisc)
93{
94 unsigned long flags;
95 int ret = 0;
96
97 if (disc < N_TTY || disc >= NR_LDISCS)
98 return -EINVAL;
99
100 spin_lock_irqsave(&tty_ldisc_lock, flags);
101 tty_ldiscs[disc] = new_ldisc;
102 new_ldisc->num = disc;
103 new_ldisc->refcount = 0;
104 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
105
106 return ret;
107}
108EXPORT_SYMBOL(tty_register_ldisc);
109
110/**
111 * tty_unregister_ldisc - unload a line discipline
112 * @disc: ldisc number
113 * @new_ldisc: pointer to the ldisc object
114 *
115 * Remove a line discipline from the kernel providing it is not
116 * currently in use.
117 *
118 * Locking:
119 * takes tty_ldisc_lock to guard against ldisc races
120 */
121
122int tty_unregister_ldisc(int disc)
123{
124 unsigned long flags;
125 int ret = 0;
126
127 if (disc < N_TTY || disc >= NR_LDISCS)
128 return -EINVAL;
129
130 spin_lock_irqsave(&tty_ldisc_lock, flags);
131 if (tty_ldiscs[disc]->refcount)
132 ret = -EBUSY;
133 else
134 tty_ldiscs[disc] = NULL;
135 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
136
137 return ret;
138}
139EXPORT_SYMBOL(tty_unregister_ldisc);
140
141
142/**
143 * tty_ldisc_try_get - try and reference an ldisc
144 * @disc: ldisc number
Alan Cox01e1abb2008-07-22 11:16:55 +0100145 *
146 * Attempt to open and lock a line discipline into place. Return
Alan Coxc65c9bc2009-06-11 12:50:12 +0100147 * the line discipline refcounted or an error.
Alan Cox01e1abb2008-07-22 11:16:55 +0100148 */
149
Alan Coxc65c9bc2009-06-11 12:50:12 +0100150static struct tty_ldisc *tty_ldisc_try_get(int disc)
Alan Cox01e1abb2008-07-22 11:16:55 +0100151{
152 unsigned long flags;
Alan Coxc65c9bc2009-06-11 12:50:12 +0100153 struct tty_ldisc *ld;
Alan Cox01e1abb2008-07-22 11:16:55 +0100154 struct tty_ldisc_ops *ldops;
155 int err = -EINVAL;
Alan Cox852e99d2009-06-11 12:51:41 +0100156
Alan Coxc65c9bc2009-06-11 12:50:12 +0100157 ld = kmalloc(sizeof(struct tty_ldisc), GFP_KERNEL);
158 if (ld == NULL)
159 return ERR_PTR(-ENOMEM);
160
Alan Cox01e1abb2008-07-22 11:16:55 +0100161 spin_lock_irqsave(&tty_ldisc_lock, flags);
162 ld->ops = NULL;
163 ldops = tty_ldiscs[disc];
164 /* Check the entry is defined */
165 if (ldops) {
166 /* If the module is being unloaded we can't use it */
167 if (!try_module_get(ldops->owner))
168 err = -EAGAIN;
169 else {
170 /* lock it */
171 ldops->refcount++;
172 ld->ops = ldops;
Linus Torvalds65b77042009-08-03 11:11:19 -0700173 atomic_set(&ld->users, 1);
Alan Cox01e1abb2008-07-22 11:16:55 +0100174 err = 0;
175 }
176 }
177 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
Alan Cox8d2ead72009-06-16 17:00:26 +0100178 if (err) {
179 kfree(ld);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100180 return ERR_PTR(err);
Alan Cox8d2ead72009-06-16 17:00:26 +0100181 }
Alan Coxc65c9bc2009-06-11 12:50:12 +0100182 return ld;
Alan Cox01e1abb2008-07-22 11:16:55 +0100183}
184
185/**
186 * tty_ldisc_get - take a reference to an ldisc
187 * @disc: ldisc number
Alan Cox01e1abb2008-07-22 11:16:55 +0100188 *
189 * Takes a reference to a line discipline. Deals with refcounts and
190 * module locking counts. Returns NULL if the discipline is not available.
191 * Returns a pointer to the discipline and bumps the ref count if it is
192 * available
193 *
194 * Locking:
195 * takes tty_ldisc_lock to guard against ldisc races
196 */
197
Alan Coxc65c9bc2009-06-11 12:50:12 +0100198static struct tty_ldisc *tty_ldisc_get(int disc)
Alan Cox01e1abb2008-07-22 11:16:55 +0100199{
Alan Coxc65c9bc2009-06-11 12:50:12 +0100200 struct tty_ldisc *ld;
Alan Cox01e1abb2008-07-22 11:16:55 +0100201
202 if (disc < N_TTY || disc >= NR_LDISCS)
Alan Coxc65c9bc2009-06-11 12:50:12 +0100203 return ERR_PTR(-EINVAL);
204 ld = tty_ldisc_try_get(disc);
205 if (IS_ERR(ld)) {
Alan Cox01e1abb2008-07-22 11:16:55 +0100206 request_module("tty-ldisc-%d", disc);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100207 ld = tty_ldisc_try_get(disc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100208 }
Alan Coxc65c9bc2009-06-11 12:50:12 +0100209 return ld;
Alan Cox01e1abb2008-07-22 11:16:55 +0100210}
211
Alan Cox852e99d2009-06-11 12:51:41 +0100212static void *tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos)
Alan Cox01e1abb2008-07-22 11:16:55 +0100213{
214 return (*pos < NR_LDISCS) ? pos : NULL;
215}
216
Alan Cox852e99d2009-06-11 12:51:41 +0100217static void *tty_ldiscs_seq_next(struct seq_file *m, void *v, loff_t *pos)
Alan Cox01e1abb2008-07-22 11:16:55 +0100218{
219 (*pos)++;
220 return (*pos < NR_LDISCS) ? pos : NULL;
221}
222
223static void tty_ldiscs_seq_stop(struct seq_file *m, void *v)
224{
225}
226
227static int tty_ldiscs_seq_show(struct seq_file *m, void *v)
228{
229 int i = *(loff_t *)v;
Alan Coxc65c9bc2009-06-11 12:50:12 +0100230 struct tty_ldisc *ld;
Alan Cox852e99d2009-06-11 12:51:41 +0100231
Alan Coxc65c9bc2009-06-11 12:50:12 +0100232 ld = tty_ldisc_try_get(i);
233 if (IS_ERR(ld))
Alan Cox01e1abb2008-07-22 11:16:55 +0100234 return 0;
Alan Coxc65c9bc2009-06-11 12:50:12 +0100235 seq_printf(m, "%-10s %2d\n", ld->ops->name ? ld->ops->name : "???", i);
Linus Torvalds65b77042009-08-03 11:11:19 -0700236 put_ldisc(ld);
Alan Cox01e1abb2008-07-22 11:16:55 +0100237 return 0;
238}
239
240static const struct seq_operations tty_ldiscs_seq_ops = {
241 .start = tty_ldiscs_seq_start,
242 .next = tty_ldiscs_seq_next,
243 .stop = tty_ldiscs_seq_stop,
244 .show = tty_ldiscs_seq_show,
245};
246
247static int proc_tty_ldiscs_open(struct inode *inode, struct file *file)
248{
249 return seq_open(file, &tty_ldiscs_seq_ops);
250}
251
252const struct file_operations tty_ldiscs_proc_fops = {
253 .owner = THIS_MODULE,
254 .open = proc_tty_ldiscs_open,
255 .read = seq_read,
256 .llseek = seq_lseek,
257 .release = seq_release,
258};
259
260/**
261 * tty_ldisc_assign - set ldisc on a tty
262 * @tty: tty to assign
263 * @ld: line discipline
264 *
265 * Install an instance of a line discipline into a tty structure. The
Alan Cox8d2ead72009-06-16 17:00:26 +0100266 * ldisc must have a reference count above zero to ensure it remains.
Alan Cox01e1abb2008-07-22 11:16:55 +0100267 * The tty instance refcount starts at zero.
268 *
269 * Locking:
270 * Caller must hold references
271 */
272
273static void tty_ldisc_assign(struct tty_struct *tty, struct tty_ldisc *ld)
274{
Alan Coxc65c9bc2009-06-11 12:50:12 +0100275 tty->ldisc = ld;
Alan Cox01e1abb2008-07-22 11:16:55 +0100276}
277
278/**
279 * tty_ldisc_try - internal helper
280 * @tty: the tty
281 *
282 * Make a single attempt to grab and bump the refcount on
283 * the tty ldisc. Return 0 on failure or 1 on success. This is
284 * used to implement both the waiting and non waiting versions
285 * of tty_ldisc_ref
286 *
287 * Locking: takes tty_ldisc_lock
288 */
289
Linus Torvalds65b77042009-08-03 11:11:19 -0700290static struct tty_ldisc *tty_ldisc_try(struct tty_struct *tty)
Alan Cox01e1abb2008-07-22 11:16:55 +0100291{
292 unsigned long flags;
293 struct tty_ldisc *ld;
Alan Cox01e1abb2008-07-22 11:16:55 +0100294
295 spin_lock_irqsave(&tty_ldisc_lock, flags);
Linus Torvalds65b77042009-08-03 11:11:19 -0700296 ld = NULL;
297 if (test_bit(TTY_LDISC, &tty->flags))
298 ld = get_ldisc(tty->ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100299 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
Linus Torvalds65b77042009-08-03 11:11:19 -0700300 return ld;
Alan Cox01e1abb2008-07-22 11:16:55 +0100301}
302
303/**
304 * tty_ldisc_ref_wait - wait for the tty ldisc
305 * @tty: tty device
306 *
307 * Dereference the line discipline for the terminal and take a
308 * reference to it. If the line discipline is in flux then
309 * wait patiently until it changes.
310 *
311 * Note: Must not be called from an IRQ/timer context. The caller
312 * must also be careful not to hold other locks that will deadlock
313 * against a discipline change, such as an existing ldisc reference
314 * (which we check for)
315 *
316 * Locking: call functions take tty_ldisc_lock
317 */
318
319struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty)
320{
Linus Torvalds65b77042009-08-03 11:11:19 -0700321 struct tty_ldisc *ld;
322
Alan Cox01e1abb2008-07-22 11:16:55 +0100323 /* wait_event is a macro */
Linus Torvalds65b77042009-08-03 11:11:19 -0700324 wait_event(tty_ldisc_wait, (ld = tty_ldisc_try(tty)) != NULL);
325 return ld;
Alan Cox01e1abb2008-07-22 11:16:55 +0100326}
Alan Cox01e1abb2008-07-22 11:16:55 +0100327EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait);
328
329/**
330 * tty_ldisc_ref - get the tty ldisc
331 * @tty: tty device
332 *
333 * Dereference the line discipline for the terminal and take a
334 * reference to it. If the line discipline is in flux then
335 * return NULL. Can be called from IRQ and timer functions.
336 *
337 * Locking: called functions take tty_ldisc_lock
338 */
339
340struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty)
341{
Linus Torvalds65b77042009-08-03 11:11:19 -0700342 return tty_ldisc_try(tty);
Alan Cox01e1abb2008-07-22 11:16:55 +0100343}
Alan Cox01e1abb2008-07-22 11:16:55 +0100344EXPORT_SYMBOL_GPL(tty_ldisc_ref);
345
346/**
347 * tty_ldisc_deref - free a tty ldisc reference
348 * @ld: reference to free up
349 *
350 * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May
351 * be called in IRQ context.
352 *
353 * Locking: takes tty_ldisc_lock
354 */
355
356void tty_ldisc_deref(struct tty_ldisc *ld)
357{
Linus Torvalds65b77042009-08-03 11:11:19 -0700358 put_ldisc(ld);
Alan Cox01e1abb2008-07-22 11:16:55 +0100359}
Alan Cox01e1abb2008-07-22 11:16:55 +0100360EXPORT_SYMBOL_GPL(tty_ldisc_deref);
361
Linus Torvalds65b77042009-08-03 11:11:19 -0700362static inline void tty_ldisc_put(struct tty_ldisc *ld)
363{
364 put_ldisc(ld);
365}
366
Alan Cox01e1abb2008-07-22 11:16:55 +0100367/**
368 * tty_ldisc_enable - allow ldisc use
369 * @tty: terminal to activate ldisc on
370 *
371 * Set the TTY_LDISC flag when the line discipline can be called
Alan Coxc9b39762009-01-02 13:44:56 +0000372 * again. Do necessary wakeups for existing sleepers. Clear the LDISC
373 * changing flag to indicate any ldisc change is now over.
Alan Cox01e1abb2008-07-22 11:16:55 +0100374 *
Alan Coxc9b39762009-01-02 13:44:56 +0000375 * Note: nobody should set the TTY_LDISC bit except via this function.
376 * Clearing directly is allowed.
Alan Cox01e1abb2008-07-22 11:16:55 +0100377 */
378
379void tty_ldisc_enable(struct tty_struct *tty)
380{
381 set_bit(TTY_LDISC, &tty->flags);
Alan Coxc9b39762009-01-02 13:44:56 +0000382 clear_bit(TTY_LDISC_CHANGING, &tty->flags);
Alan Cox01e1abb2008-07-22 11:16:55 +0100383 wake_up(&tty_ldisc_wait);
384}
385
386/**
Alan Coxf2c4c652009-06-11 12:50:58 +0100387 * tty_ldisc_flush - flush line discipline queue
388 * @tty: tty
389 *
390 * Flush the line discipline queue (if any) for this tty. If there
391 * is no line discipline active this is a no-op.
392 */
393
394void tty_ldisc_flush(struct tty_struct *tty)
395{
396 struct tty_ldisc *ld = tty_ldisc_ref(tty);
397 if (ld) {
398 if (ld->ops->flush_buffer)
399 ld->ops->flush_buffer(tty);
400 tty_ldisc_deref(ld);
401 }
402 tty_buffer_flush(tty);
403}
Alan Coxf2c4c652009-06-11 12:50:58 +0100404EXPORT_SYMBOL_GPL(tty_ldisc_flush);
405
406/**
Alan Cox01e1abb2008-07-22 11:16:55 +0100407 * tty_set_termios_ldisc - set ldisc field
408 * @tty: tty structure
409 * @num: line discipline number
410 *
411 * This is probably overkill for real world processors but
412 * they are not on hot paths so a little discipline won't do
413 * any harm.
414 *
415 * Locking: takes termios_mutex
416 */
417
418static void tty_set_termios_ldisc(struct tty_struct *tty, int num)
419{
420 mutex_lock(&tty->termios_mutex);
421 tty->termios->c_line = num;
422 mutex_unlock(&tty->termios_mutex);
423}
424
Alan Coxc65c9bc2009-06-11 12:50:12 +0100425/**
426 * tty_ldisc_open - open a line discipline
427 * @tty: tty we are opening the ldisc on
428 * @ld: discipline to open
429 *
430 * A helper opening method. Also a convenient debugging and check
431 * point.
432 */
433
434static int tty_ldisc_open(struct tty_struct *tty, struct tty_ldisc *ld)
435{
436 WARN_ON(test_and_set_bit(TTY_LDISC_OPEN, &tty->flags));
437 if (ld->ops->open)
438 return ld->ops->open(tty);
439 return 0;
440}
441
442/**
443 * tty_ldisc_close - close a line discipline
444 * @tty: tty we are opening the ldisc on
445 * @ld: discipline to close
446 *
447 * A helper close method. Also a convenient debugging and check
448 * point.
449 */
450
451static void tty_ldisc_close(struct tty_struct *tty, struct tty_ldisc *ld)
452{
453 WARN_ON(!test_bit(TTY_LDISC_OPEN, &tty->flags));
454 clear_bit(TTY_LDISC_OPEN, &tty->flags);
455 if (ld->ops->close)
456 ld->ops->close(tty);
457}
Alan Cox01e1abb2008-07-22 11:16:55 +0100458
459/**
460 * tty_ldisc_restore - helper for tty ldisc change
461 * @tty: tty to recover
462 * @old: previous ldisc
463 *
464 * Restore the previous line discipline or N_TTY when a line discipline
465 * change fails due to an open error
466 */
467
468static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old)
469{
470 char buf[64];
Alan Coxc65c9bc2009-06-11 12:50:12 +0100471 struct tty_ldisc *new_ldisc;
472 int r;
Alan Cox01e1abb2008-07-22 11:16:55 +0100473
474 /* There is an outstanding reference here so this is safe */
Alan Coxc65c9bc2009-06-11 12:50:12 +0100475 old = tty_ldisc_get(old->ops->num);
476 WARN_ON(IS_ERR(old));
Alan Cox01e1abb2008-07-22 11:16:55 +0100477 tty_ldisc_assign(tty, old);
478 tty_set_termios_ldisc(tty, old->ops->num);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100479 if (tty_ldisc_open(tty, old) < 0) {
480 tty_ldisc_put(old);
Alan Cox01e1abb2008-07-22 11:16:55 +0100481 /* This driver is always present */
Alan Cox852e99d2009-06-11 12:51:41 +0100482 new_ldisc = tty_ldisc_get(N_TTY);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100483 if (IS_ERR(new_ldisc))
Alan Cox01e1abb2008-07-22 11:16:55 +0100484 panic("n_tty: get");
Alan Coxc65c9bc2009-06-11 12:50:12 +0100485 tty_ldisc_assign(tty, new_ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100486 tty_set_termios_ldisc(tty, N_TTY);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100487 r = tty_ldisc_open(tty, new_ldisc);
488 if (r < 0)
489 panic("Couldn't open N_TTY ldisc for "
490 "%s --- error %d.",
491 tty_name(tty, buf), r);
Alan Cox01e1abb2008-07-22 11:16:55 +0100492 }
493}
494
495/**
Alan Coxc65c9bc2009-06-11 12:50:12 +0100496 * tty_ldisc_halt - shut down the line discipline
Alan Coxe8b70e72009-06-11 12:48:02 +0100497 * @tty: tty device
498 *
499 * Shut down the line discipline and work queue for this tty device.
500 * The TTY_LDISC flag being cleared ensures no further references can
501 * be obtained while the delayed work queue halt ensures that no more
502 * data is fed to the ldisc.
503 *
Alan Cox852e99d2009-06-11 12:51:41 +0100504 * In order to wait for any existing references to complete see
Alan Coxe8b70e72009-06-11 12:48:02 +0100505 * tty_ldisc_wait_idle.
506 */
507
Alan Coxc65c9bc2009-06-11 12:50:12 +0100508static int tty_ldisc_halt(struct tty_struct *tty)
Alan Coxe8b70e72009-06-11 12:48:02 +0100509{
510 clear_bit(TTY_LDISC, &tty->flags);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100511 return cancel_delayed_work(&tty->buf.work);
Alan Coxe8b70e72009-06-11 12:48:02 +0100512}
513
514/**
Alan Cox01e1abb2008-07-22 11:16:55 +0100515 * tty_set_ldisc - set line discipline
516 * @tty: the terminal to set
517 * @ldisc: the line discipline
518 *
519 * Set the discipline of a tty line. Must be called from a process
Alan Coxc65c9bc2009-06-11 12:50:12 +0100520 * context. The ldisc change logic has to protect itself against any
521 * overlapping ldisc change (including on the other end of pty pairs),
522 * the close of one side of a tty/pty pair, and eventually hangup.
Alan Cox01e1abb2008-07-22 11:16:55 +0100523 *
Alan Coxc65c9bc2009-06-11 12:50:12 +0100524 * Locking: takes tty_ldisc_lock, termios_mutex
Alan Cox01e1abb2008-07-22 11:16:55 +0100525 */
526
527int tty_set_ldisc(struct tty_struct *tty, int ldisc)
528{
529 int retval;
Alan Coxc65c9bc2009-06-11 12:50:12 +0100530 struct tty_ldisc *o_ldisc, *new_ldisc;
531 int work, o_work = 0;
Alan Cox01e1abb2008-07-22 11:16:55 +0100532 struct tty_struct *o_tty;
533
Alan Coxc65c9bc2009-06-11 12:50:12 +0100534 new_ldisc = tty_ldisc_get(ldisc);
535 if (IS_ERR(new_ldisc))
536 return PTR_ERR(new_ldisc);
537
538 /*
539 * We need to look at the tty locking here for pty/tty pairs
540 * when both sides try to change in parallel.
541 */
542
543 o_tty = tty->link; /* o_tty is the pty side or NULL */
544
545
546 /*
547 * Check the no-op case
548 */
549
550 if (tty->ldisc->ops->num == ldisc) {
551 tty_ldisc_put(new_ldisc);
552 return 0;
553 }
Alan Cox01e1abb2008-07-22 11:16:55 +0100554
555 /*
556 * Problem: What do we do if this blocks ?
Alan Coxc65c9bc2009-06-11 12:50:12 +0100557 * We could deadlock here
Alan Cox01e1abb2008-07-22 11:16:55 +0100558 */
559
560 tty_wait_until_sent(tty, 0);
561
Alan Coxc65c9bc2009-06-11 12:50:12 +0100562 mutex_lock(&tty->ldisc_mutex);
Alan Cox01e1abb2008-07-22 11:16:55 +0100563
564 /*
Alan Coxc65c9bc2009-06-11 12:50:12 +0100565 * We could be midstream of another ldisc change which has
566 * dropped the lock during processing. If so we need to wait.
567 */
568
569 while (test_bit(TTY_LDISC_CHANGING, &tty->flags)) {
570 mutex_unlock(&tty->ldisc_mutex);
571 wait_event(tty_ldisc_wait,
572 test_bit(TTY_LDISC_CHANGING, &tty->flags) == 0);
573 mutex_lock(&tty->ldisc_mutex);
574 }
575 set_bit(TTY_LDISC_CHANGING, &tty->flags);
Alan Cox852e99d2009-06-11 12:51:41 +0100576
Alan Coxc65c9bc2009-06-11 12:50:12 +0100577 /*
Alan Cox01e1abb2008-07-22 11:16:55 +0100578 * No more input please, we are switching. The new ldisc
579 * will update this value in the ldisc open function
580 */
581
582 tty->receive_room = 0;
583
584 o_ldisc = tty->ldisc;
Alan Cox01e1abb2008-07-22 11:16:55 +0100585 /*
586 * Make sure we don't change while someone holds a
587 * reference to the line discipline. The TTY_LDISC bit
588 * prevents anyone taking a reference once it is clear.
589 * We need the lock to avoid racing reference takers.
Alan Coxc9b39762009-01-02 13:44:56 +0000590 *
591 * We must clear the TTY_LDISC bit here to avoid a livelock
592 * with a userspace app continually trying to use the tty in
593 * parallel to the change and re-referencing the tty.
Alan Cox01e1abb2008-07-22 11:16:55 +0100594 */
595
Alan Coxc65c9bc2009-06-11 12:50:12 +0100596 work = tty_ldisc_halt(tty);
Alan Cox01e1abb2008-07-22 11:16:55 +0100597 if (o_tty)
Alan Coxc65c9bc2009-06-11 12:50:12 +0100598 o_work = tty_ldisc_halt(o_tty);
599
Alan Cox01e1abb2008-07-22 11:16:55 +0100600 /*
Alan Coxc65c9bc2009-06-11 12:50:12 +0100601 * Wait for ->hangup_work and ->buf.work handlers to terminate.
602 * We must drop the mutex here in case a hangup is also in process.
Alan Cox01e1abb2008-07-22 11:16:55 +0100603 */
604
Alan Coxc65c9bc2009-06-11 12:50:12 +0100605 mutex_unlock(&tty->ldisc_mutex);
606
Alan Cox01e1abb2008-07-22 11:16:55 +0100607 flush_scheduled_work();
Alan Coxc65c9bc2009-06-11 12:50:12 +0100608
Alan Coxc65c9bc2009-06-11 12:50:12 +0100609 mutex_lock(&tty->ldisc_mutex);
610 if (test_bit(TTY_HUPPED, &tty->flags)) {
611 /* We were raced by the hangup method. It will have stomped
612 the ldisc data and closed the ldisc down */
613 clear_bit(TTY_LDISC_CHANGING, &tty->flags);
614 mutex_unlock(&tty->ldisc_mutex);
615 tty_ldisc_put(new_ldisc);
616 return -EIO;
617 }
618
Alan Cox01e1abb2008-07-22 11:16:55 +0100619 /* Shutdown the current discipline. */
Alan Coxc65c9bc2009-06-11 12:50:12 +0100620 tty_ldisc_close(tty, o_ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100621
622 /* Now set up the new line discipline. */
Alan Coxc65c9bc2009-06-11 12:50:12 +0100623 tty_ldisc_assign(tty, new_ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100624 tty_set_termios_ldisc(tty, ldisc);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100625
626 retval = tty_ldisc_open(tty, new_ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100627 if (retval < 0) {
Alan Coxc65c9bc2009-06-11 12:50:12 +0100628 /* Back to the old one or N_TTY if we can't */
629 tty_ldisc_put(new_ldisc);
630 tty_ldisc_restore(tty, o_ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100631 }
Alan Coxc65c9bc2009-06-11 12:50:12 +0100632
Alan Cox01e1abb2008-07-22 11:16:55 +0100633 /* At this point we hold a reference to the new ldisc and a
634 a reference to the old ldisc. If we ended up flipping back
635 to the existing ldisc we have two references to it */
636
Alan Coxc65c9bc2009-06-11 12:50:12 +0100637 if (tty->ldisc->ops->num != o_ldisc->ops->num && tty->ops->set_ldisc)
Alan Cox01e1abb2008-07-22 11:16:55 +0100638 tty->ops->set_ldisc(tty);
639
Alan Coxc65c9bc2009-06-11 12:50:12 +0100640 tty_ldisc_put(o_ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100641
642 /*
Alan Coxc65c9bc2009-06-11 12:50:12 +0100643 * Allow ldisc referencing to occur again
Alan Cox01e1abb2008-07-22 11:16:55 +0100644 */
645
646 tty_ldisc_enable(tty);
647 if (o_tty)
648 tty_ldisc_enable(o_tty);
649
Alan Coxc65c9bc2009-06-11 12:50:12 +0100650 /* Restart the work queue in case no characters kick it off. Safe if
Alan Cox01e1abb2008-07-22 11:16:55 +0100651 already running */
652 if (work)
653 schedule_delayed_work(&tty->buf.work, 1);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100654 if (o_work)
655 schedule_delayed_work(&o_tty->buf.work, 1);
656 mutex_unlock(&tty->ldisc_mutex);
Alan Cox01e1abb2008-07-22 11:16:55 +0100657 return retval;
658}
659
Alan Coxc65c9bc2009-06-11 12:50:12 +0100660/**
661 * tty_reset_termios - reset terminal state
662 * @tty: tty to reset
663 *
664 * Restore a terminal to the driver default state.
665 */
666
667static void tty_reset_termios(struct tty_struct *tty)
668{
669 mutex_lock(&tty->termios_mutex);
670 *tty->termios = tty->driver->init_termios;
671 tty->termios->c_ispeed = tty_termios_input_baud_rate(tty->termios);
672 tty->termios->c_ospeed = tty_termios_baud_rate(tty->termios);
673 mutex_unlock(&tty->termios_mutex);
674}
675
676
677/**
678 * tty_ldisc_reinit - reinitialise the tty ldisc
679 * @tty: tty to reinit
680 *
681 * Switch the tty back to N_TTY line discipline and leave the
682 * ldisc state closed
683 */
684
685static void tty_ldisc_reinit(struct tty_struct *tty)
686{
687 struct tty_ldisc *ld;
688
689 tty_ldisc_close(tty, tty->ldisc);
690 tty_ldisc_put(tty->ldisc);
691 tty->ldisc = NULL;
692 /*
693 * Switch the line discipline back
694 */
695 ld = tty_ldisc_get(N_TTY);
696 BUG_ON(IS_ERR(ld));
697 tty_ldisc_assign(tty, ld);
698 tty_set_termios_ldisc(tty, N_TTY);
699}
700
701/**
702 * tty_ldisc_hangup - hangup ldisc reset
703 * @tty: tty being hung up
704 *
705 * Some tty devices reset their termios when they receive a hangup
706 * event. In that situation we must also switch back to N_TTY properly
707 * before we reset the termios data.
708 *
709 * Locking: We can take the ldisc mutex as the rest of the code is
710 * careful to allow for this.
711 *
712 * In the pty pair case this occurs in the close() path of the
713 * tty itself so we must be careful about locking rules.
714 */
715
716void tty_ldisc_hangup(struct tty_struct *tty)
717{
718 struct tty_ldisc *ld;
719
720 /*
721 * FIXME! What are the locking issues here? This may me overdoing
722 * things... This question is especially important now that we've
723 * removed the irqlock.
724 */
725 ld = tty_ldisc_ref(tty);
726 if (ld != NULL) {
727 /* We may have no line discipline at this point */
728 if (ld->ops->flush_buffer)
729 ld->ops->flush_buffer(tty);
730 tty_driver_flush_buffer(tty);
731 if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
732 ld->ops->write_wakeup)
733 ld->ops->write_wakeup(tty);
734 if (ld->ops->hangup)
735 ld->ops->hangup(tty);
736 tty_ldisc_deref(ld);
737 }
738 /*
739 * FIXME: Once we trust the LDISC code better we can wait here for
740 * ldisc completion and fix the driver call race
741 */
742 wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
743 wake_up_interruptible_poll(&tty->read_wait, POLLIN);
744 /*
745 * Shutdown the current line discipline, and reset it to
746 * N_TTY.
747 */
748 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) {
Alan Coxc8d50042009-07-16 16:05:08 +0100749 /* Avoid racing set_ldisc or tty_ldisc_release */
Alan Coxc65c9bc2009-06-11 12:50:12 +0100750 mutex_lock(&tty->ldisc_mutex);
Alan Coxc8d50042009-07-16 16:05:08 +0100751 if (tty->ldisc) { /* Not yet closed */
752 /* Switch back to N_TTY */
753 tty_ldisc_halt(tty);
Alan Coxc8d50042009-07-16 16:05:08 +0100754 tty_ldisc_reinit(tty);
755 /* At this point we have a closed ldisc and we want to
756 reopen it. We could defer this to the next open but
757 it means auditing a lot of other paths so this is
758 a FIXME */
759 WARN_ON(tty_ldisc_open(tty, tty->ldisc));
760 tty_ldisc_enable(tty);
761 }
Alan Coxc65c9bc2009-06-11 12:50:12 +0100762 mutex_unlock(&tty->ldisc_mutex);
763 tty_reset_termios(tty);
764 }
765}
Alan Cox01e1abb2008-07-22 11:16:55 +0100766
767/**
768 * tty_ldisc_setup - open line discipline
769 * @tty: tty being shut down
770 * @o_tty: pair tty for pty/tty pairs
771 *
772 * Called during the initial open of a tty/pty pair in order to set up the
Alan Coxc65c9bc2009-06-11 12:50:12 +0100773 * line disciplines and bind them to the tty. This has no locking issues
774 * as the device isn't yet active.
Alan Cox01e1abb2008-07-22 11:16:55 +0100775 */
776
777int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty)
778{
Alan Coxc65c9bc2009-06-11 12:50:12 +0100779 struct tty_ldisc *ld = tty->ldisc;
Alan Cox01e1abb2008-07-22 11:16:55 +0100780 int retval;
781
Alan Coxc65c9bc2009-06-11 12:50:12 +0100782 retval = tty_ldisc_open(tty, ld);
783 if (retval)
784 return retval;
785
786 if (o_tty) {
787 retval = tty_ldisc_open(o_tty, o_tty->ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100788 if (retval) {
Alan Coxc65c9bc2009-06-11 12:50:12 +0100789 tty_ldisc_close(tty, ld);
Alan Cox01e1abb2008-07-22 11:16:55 +0100790 return retval;
791 }
792 tty_ldisc_enable(o_tty);
793 }
794 tty_ldisc_enable(tty);
795 return 0;
796}
Alan Cox01e1abb2008-07-22 11:16:55 +0100797/**
798 * tty_ldisc_release - release line discipline
799 * @tty: tty being shut down
800 * @o_tty: pair tty for pty/tty pairs
801 *
Alan Cox852e99d2009-06-11 12:51:41 +0100802 * Called during the final close of a tty/pty pair in order to shut down
803 * the line discpline layer. On exit the ldisc assigned is N_TTY and the
Alan Coxc65c9bc2009-06-11 12:50:12 +0100804 * ldisc has not been opened.
Alan Cox01e1abb2008-07-22 11:16:55 +0100805 */
806
807void tty_ldisc_release(struct tty_struct *tty, struct tty_struct *o_tty)
808{
Alan Cox01e1abb2008-07-22 11:16:55 +0100809 /*
810 * Prevent flush_to_ldisc() from rescheduling the work for later. Then
811 * kill any delayed work. As this is the final close it does not
812 * race with the set_ldisc code path.
813 */
Alan Cox01e1abb2008-07-22 11:16:55 +0100814
Alan Coxe8b70e72009-06-11 12:48:02 +0100815 tty_ldisc_halt(tty);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100816 flush_scheduled_work();
Alan Cox01e1abb2008-07-22 11:16:55 +0100817
Alan Coxc8d50042009-07-16 16:05:08 +0100818 mutex_lock(&tty->ldisc_mutex);
Alan Cox01e1abb2008-07-22 11:16:55 +0100819 /*
Alan Coxaef29bc2009-06-29 15:21:47 +0100820 * Now kill off the ldisc
Alan Cox01e1abb2008-07-22 11:16:55 +0100821 */
Alan Coxaef29bc2009-06-29 15:21:47 +0100822 tty_ldisc_close(tty, tty->ldisc);
823 tty_ldisc_put(tty->ldisc);
824 /* Force an oops if we mess this up */
825 tty->ldisc = NULL;
Alan Cox01e1abb2008-07-22 11:16:55 +0100826
Alan Coxaef29bc2009-06-29 15:21:47 +0100827 /* Ensure the next open requests the N_TTY ldisc */
828 tty_set_termios_ldisc(tty, N_TTY);
Alan Coxc8d50042009-07-16 16:05:08 +0100829 mutex_unlock(&tty->ldisc_mutex);
Alan Coxaef29bc2009-06-29 15:21:47 +0100830
Alan Coxc65c9bc2009-06-11 12:50:12 +0100831 /* This will need doing differently if we need to lock */
832 if (o_tty)
833 tty_ldisc_release(o_tty, NULL);
Alan Coxaef29bc2009-06-29 15:21:47 +0100834
835 /* And the memory resources remaining (buffers, termios) will be
836 disposed of when the kref hits zero */
Alan Cox01e1abb2008-07-22 11:16:55 +0100837}
838
839/**
840 * tty_ldisc_init - ldisc setup for new tty
841 * @tty: tty being allocated
842 *
843 * Set up the line discipline objects for a newly allocated tty. Note that
844 * the tty structure is not completely set up when this call is made.
845 */
846
847void tty_ldisc_init(struct tty_struct *tty)
848{
Alan Coxc65c9bc2009-06-11 12:50:12 +0100849 struct tty_ldisc *ld = tty_ldisc_get(N_TTY);
850 if (IS_ERR(ld))
Alan Cox01e1abb2008-07-22 11:16:55 +0100851 panic("n_tty: init_tty");
Alan Coxc65c9bc2009-06-11 12:50:12 +0100852 tty_ldisc_assign(tty, ld);
Alan Cox01e1abb2008-07-22 11:16:55 +0100853}
854
855void tty_ldisc_begin(void)
856{
857 /* Setup the default TTY line discipline. */
858 (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);
859}