blob: cbfacc0bbea520764bdc176dab20f47e0f90a3e2 [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
Linus Torvaldscbe93522009-08-03 14:54:56 -070058static void put_ldisc(struct tty_ldisc *ld)
Linus Torvalds65b77042009-08-03 11:11:19 -070059{
Linus Torvaldscbe93522009-08-03 14:54:56 -070060 unsigned long flags;
61
Linus Torvalds65b77042009-08-03 11:11:19 -070062 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 Torvaldscbe93522009-08-03 14:54:56 -070068 *
69 * We really want an "atomic_dec_and_lock_irqsave()",
70 * but we don't have it, so this does it by hand.
Linus Torvalds65b77042009-08-03 11:11:19 -070071 */
Linus Torvaldscbe93522009-08-03 14:54:56 -070072 local_irq_save(flags);
73 if (atomic_dec_and_lock(&ld->users, &tty_ldisc_lock)) {
Linus Torvalds65b77042009-08-03 11:11:19 -070074 struct tty_ldisc_ops *ldo = ld->ops;
75
Linus Torvalds65b77042009-08-03 11:11:19 -070076 ldo->refcount--;
77 module_put(ldo->owner);
78 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
Linus Torvaldscbe93522009-08-03 14:54:56 -070079
80 kfree(ld);
81 return;
Linus Torvalds65b77042009-08-03 11:11:19 -070082 }
Linus Torvaldscbe93522009-08-03 14:54:56 -070083 local_irq_restore(flags);
Linus Torvalds65b77042009-08-03 11:11:19 -070084}
85
Alan Cox01e1abb2008-07-22 11:16:55 +010086/**
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
99int 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}
115EXPORT_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
129int 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}
146EXPORT_SYMBOL(tty_unregister_ldisc);
147
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700148static 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
167static 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 Cox01e1abb2008-07-22 11:16:55 +0100176
177/**
178 * tty_ldisc_try_get - try and reference an ldisc
179 * @disc: ldisc number
Alan Cox01e1abb2008-07-22 11:16:55 +0100180 *
181 * Attempt to open and lock a line discipline into place. Return
Alan Coxc65c9bc2009-06-11 12:50:12 +0100182 * the line discipline refcounted or an error.
Alan Cox01e1abb2008-07-22 11:16:55 +0100183 */
184
Alan Coxc65c9bc2009-06-11 12:50:12 +0100185static struct tty_ldisc *tty_ldisc_try_get(int disc)
Alan Cox01e1abb2008-07-22 11:16:55 +0100186{
Alan Coxc65c9bc2009-06-11 12:50:12 +0100187 struct tty_ldisc *ld;
Alan Cox01e1abb2008-07-22 11:16:55 +0100188 struct tty_ldisc_ops *ldops;
Alan Cox852e99d2009-06-11 12:51:41 +0100189
Alan Coxc65c9bc2009-06-11 12:50:12 +0100190 ld = kmalloc(sizeof(struct tty_ldisc), GFP_KERNEL);
191 if (ld == NULL)
192 return ERR_PTR(-ENOMEM);
193
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700194 ldops = get_ldops(disc);
195 if (IS_ERR(ldops)) {
Alan Cox8d2ead72009-06-16 17:00:26 +0100196 kfree(ld);
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700197 return ERR_CAST(ldops);
Alan Cox8d2ead72009-06-16 17:00:26 +0100198 }
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700199
200 ld->ops = ldops;
201 atomic_set(&ld->users, 1);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100202 return ld;
Alan Cox01e1abb2008-07-22 11:16:55 +0100203}
204
205/**
206 * tty_ldisc_get - take a reference to an ldisc
207 * @disc: ldisc number
Alan Cox01e1abb2008-07-22 11:16:55 +0100208 *
209 * Takes a reference to a line discipline. Deals with refcounts and
210 * module locking counts. Returns NULL if the discipline is not available.
211 * Returns a pointer to the discipline and bumps the ref count if it is
212 * available
213 *
214 * Locking:
215 * takes tty_ldisc_lock to guard against ldisc races
216 */
217
Alan Coxc65c9bc2009-06-11 12:50:12 +0100218static struct tty_ldisc *tty_ldisc_get(int disc)
Alan Cox01e1abb2008-07-22 11:16:55 +0100219{
Alan Coxc65c9bc2009-06-11 12:50:12 +0100220 struct tty_ldisc *ld;
Alan Cox01e1abb2008-07-22 11:16:55 +0100221
222 if (disc < N_TTY || disc >= NR_LDISCS)
Alan Coxc65c9bc2009-06-11 12:50:12 +0100223 return ERR_PTR(-EINVAL);
224 ld = tty_ldisc_try_get(disc);
225 if (IS_ERR(ld)) {
Alan Cox01e1abb2008-07-22 11:16:55 +0100226 request_module("tty-ldisc-%d", disc);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100227 ld = tty_ldisc_try_get(disc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100228 }
Alan Coxc65c9bc2009-06-11 12:50:12 +0100229 return ld;
Alan Cox01e1abb2008-07-22 11:16:55 +0100230}
231
Alan Cox852e99d2009-06-11 12:51:41 +0100232static void *tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos)
Alan Cox01e1abb2008-07-22 11:16:55 +0100233{
234 return (*pos < NR_LDISCS) ? pos : NULL;
235}
236
Alan Cox852e99d2009-06-11 12:51:41 +0100237static void *tty_ldiscs_seq_next(struct seq_file *m, void *v, loff_t *pos)
Alan Cox01e1abb2008-07-22 11:16:55 +0100238{
239 (*pos)++;
240 return (*pos < NR_LDISCS) ? pos : NULL;
241}
242
243static void tty_ldiscs_seq_stop(struct seq_file *m, void *v)
244{
245}
246
247static int tty_ldiscs_seq_show(struct seq_file *m, void *v)
248{
249 int i = *(loff_t *)v;
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700250 struct tty_ldisc_ops *ldops;
Alan Cox852e99d2009-06-11 12:51:41 +0100251
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700252 ldops = get_ldops(i);
253 if (IS_ERR(ldops))
Alan Cox01e1abb2008-07-22 11:16:55 +0100254 return 0;
Linus Torvaldsf0de0e82009-08-03 16:00:15 -0700255 seq_printf(m, "%-10s %2d\n", ldops->name ? ldops->name : "???", i);
256 put_ldops(ldops);
Alan Cox01e1abb2008-07-22 11:16:55 +0100257 return 0;
258}
259
260static const struct seq_operations tty_ldiscs_seq_ops = {
261 .start = tty_ldiscs_seq_start,
262 .next = tty_ldiscs_seq_next,
263 .stop = tty_ldiscs_seq_stop,
264 .show = tty_ldiscs_seq_show,
265};
266
267static int proc_tty_ldiscs_open(struct inode *inode, struct file *file)
268{
269 return seq_open(file, &tty_ldiscs_seq_ops);
270}
271
272const struct file_operations tty_ldiscs_proc_fops = {
273 .owner = THIS_MODULE,
274 .open = proc_tty_ldiscs_open,
275 .read = seq_read,
276 .llseek = seq_lseek,
277 .release = seq_release,
278};
279
280/**
281 * tty_ldisc_assign - set ldisc on a tty
282 * @tty: tty to assign
283 * @ld: line discipline
284 *
285 * Install an instance of a line discipline into a tty structure. The
Alan Cox8d2ead72009-06-16 17:00:26 +0100286 * ldisc must have a reference count above zero to ensure it remains.
Alan Cox01e1abb2008-07-22 11:16:55 +0100287 * The tty instance refcount starts at zero.
288 *
289 * Locking:
290 * Caller must hold references
291 */
292
293static void tty_ldisc_assign(struct tty_struct *tty, struct tty_ldisc *ld)
294{
Alan Coxc65c9bc2009-06-11 12:50:12 +0100295 tty->ldisc = ld;
Alan Cox01e1abb2008-07-22 11:16:55 +0100296}
297
298/**
299 * tty_ldisc_try - internal helper
300 * @tty: the tty
301 *
302 * Make a single attempt to grab and bump the refcount on
303 * the tty ldisc. Return 0 on failure or 1 on success. This is
304 * used to implement both the waiting and non waiting versions
305 * of tty_ldisc_ref
306 *
307 * Locking: takes tty_ldisc_lock
308 */
309
Linus Torvalds65b77042009-08-03 11:11:19 -0700310static struct tty_ldisc *tty_ldisc_try(struct tty_struct *tty)
Alan Cox01e1abb2008-07-22 11:16:55 +0100311{
312 unsigned long flags;
313 struct tty_ldisc *ld;
Alan Cox01e1abb2008-07-22 11:16:55 +0100314
315 spin_lock_irqsave(&tty_ldisc_lock, flags);
Linus Torvalds65b77042009-08-03 11:11:19 -0700316 ld = NULL;
317 if (test_bit(TTY_LDISC, &tty->flags))
318 ld = get_ldisc(tty->ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100319 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
Linus Torvalds65b77042009-08-03 11:11:19 -0700320 return ld;
Alan Cox01e1abb2008-07-22 11:16:55 +0100321}
322
323/**
324 * tty_ldisc_ref_wait - wait for the tty ldisc
325 * @tty: tty device
326 *
327 * Dereference the line discipline for the terminal and take a
328 * reference to it. If the line discipline is in flux then
329 * wait patiently until it changes.
330 *
331 * Note: Must not be called from an IRQ/timer context. The caller
332 * must also be careful not to hold other locks that will deadlock
333 * against a discipline change, such as an existing ldisc reference
334 * (which we check for)
335 *
336 * Locking: call functions take tty_ldisc_lock
337 */
338
339struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty)
340{
Linus Torvalds65b77042009-08-03 11:11:19 -0700341 struct tty_ldisc *ld;
342
Alan Cox01e1abb2008-07-22 11:16:55 +0100343 /* wait_event is a macro */
Linus Torvalds65b77042009-08-03 11:11:19 -0700344 wait_event(tty_ldisc_wait, (ld = tty_ldisc_try(tty)) != NULL);
345 return ld;
Alan Cox01e1abb2008-07-22 11:16:55 +0100346}
Alan Cox01e1abb2008-07-22 11:16:55 +0100347EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait);
348
349/**
350 * tty_ldisc_ref - get the tty ldisc
351 * @tty: tty device
352 *
353 * Dereference the line discipline for the terminal and take a
354 * reference to it. If the line discipline is in flux then
355 * return NULL. Can be called from IRQ and timer functions.
356 *
357 * Locking: called functions take tty_ldisc_lock
358 */
359
360struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty)
361{
Linus Torvalds65b77042009-08-03 11:11:19 -0700362 return tty_ldisc_try(tty);
Alan Cox01e1abb2008-07-22 11:16:55 +0100363}
Alan Cox01e1abb2008-07-22 11:16:55 +0100364EXPORT_SYMBOL_GPL(tty_ldisc_ref);
365
366/**
367 * tty_ldisc_deref - free a tty ldisc reference
368 * @ld: reference to free up
369 *
370 * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May
371 * be called in IRQ context.
372 *
373 * Locking: takes tty_ldisc_lock
374 */
375
376void tty_ldisc_deref(struct tty_ldisc *ld)
377{
Linus Torvalds65b77042009-08-03 11:11:19 -0700378 put_ldisc(ld);
Alan Cox01e1abb2008-07-22 11:16:55 +0100379}
Alan Cox01e1abb2008-07-22 11:16:55 +0100380EXPORT_SYMBOL_GPL(tty_ldisc_deref);
381
Linus Torvalds65b77042009-08-03 11:11:19 -0700382static inline void tty_ldisc_put(struct tty_ldisc *ld)
383{
384 put_ldisc(ld);
385}
386
Alan Cox01e1abb2008-07-22 11:16:55 +0100387/**
388 * tty_ldisc_enable - allow ldisc use
389 * @tty: terminal to activate ldisc on
390 *
391 * Set the TTY_LDISC flag when the line discipline can be called
Alan Coxc9b39762009-01-02 13:44:56 +0000392 * again. Do necessary wakeups for existing sleepers. Clear the LDISC
393 * changing flag to indicate any ldisc change is now over.
Alan Cox01e1abb2008-07-22 11:16:55 +0100394 *
Alan Coxc9b39762009-01-02 13:44:56 +0000395 * Note: nobody should set the TTY_LDISC bit except via this function.
396 * Clearing directly is allowed.
Alan Cox01e1abb2008-07-22 11:16:55 +0100397 */
398
399void tty_ldisc_enable(struct tty_struct *tty)
400{
401 set_bit(TTY_LDISC, &tty->flags);
Alan Coxc9b39762009-01-02 13:44:56 +0000402 clear_bit(TTY_LDISC_CHANGING, &tty->flags);
Alan Cox01e1abb2008-07-22 11:16:55 +0100403 wake_up(&tty_ldisc_wait);
404}
405
406/**
Alan Coxf2c4c652009-06-11 12:50:58 +0100407 * tty_ldisc_flush - flush line discipline queue
408 * @tty: tty
409 *
410 * Flush the line discipline queue (if any) for this tty. If there
411 * is no line discipline active this is a no-op.
412 */
413
414void tty_ldisc_flush(struct tty_struct *tty)
415{
416 struct tty_ldisc *ld = tty_ldisc_ref(tty);
417 if (ld) {
418 if (ld->ops->flush_buffer)
419 ld->ops->flush_buffer(tty);
420 tty_ldisc_deref(ld);
421 }
422 tty_buffer_flush(tty);
423}
Alan Coxf2c4c652009-06-11 12:50:58 +0100424EXPORT_SYMBOL_GPL(tty_ldisc_flush);
425
426/**
Alan Cox01e1abb2008-07-22 11:16:55 +0100427 * tty_set_termios_ldisc - set ldisc field
428 * @tty: tty structure
429 * @num: line discipline number
430 *
431 * This is probably overkill for real world processors but
432 * they are not on hot paths so a little discipline won't do
433 * any harm.
434 *
435 * Locking: takes termios_mutex
436 */
437
438static void tty_set_termios_ldisc(struct tty_struct *tty, int num)
439{
440 mutex_lock(&tty->termios_mutex);
441 tty->termios->c_line = num;
442 mutex_unlock(&tty->termios_mutex);
443}
444
Alan Coxc65c9bc2009-06-11 12:50:12 +0100445/**
446 * tty_ldisc_open - open a line discipline
447 * @tty: tty we are opening the ldisc on
448 * @ld: discipline to open
449 *
450 * A helper opening method. Also a convenient debugging and check
451 * point.
452 */
453
454static int tty_ldisc_open(struct tty_struct *tty, struct tty_ldisc *ld)
455{
456 WARN_ON(test_and_set_bit(TTY_LDISC_OPEN, &tty->flags));
457 if (ld->ops->open)
458 return ld->ops->open(tty);
459 return 0;
460}
461
462/**
463 * tty_ldisc_close - close a line discipline
464 * @tty: tty we are opening the ldisc on
465 * @ld: discipline to close
466 *
467 * A helper close method. Also a convenient debugging and check
468 * point.
469 */
470
471static void tty_ldisc_close(struct tty_struct *tty, struct tty_ldisc *ld)
472{
473 WARN_ON(!test_bit(TTY_LDISC_OPEN, &tty->flags));
474 clear_bit(TTY_LDISC_OPEN, &tty->flags);
475 if (ld->ops->close)
476 ld->ops->close(tty);
477}
Alan Cox01e1abb2008-07-22 11:16:55 +0100478
479/**
480 * tty_ldisc_restore - helper for tty ldisc change
481 * @tty: tty to recover
482 * @old: previous ldisc
483 *
484 * Restore the previous line discipline or N_TTY when a line discipline
485 * change fails due to an open error
486 */
487
488static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old)
489{
490 char buf[64];
Alan Coxc65c9bc2009-06-11 12:50:12 +0100491 struct tty_ldisc *new_ldisc;
492 int r;
Alan Cox01e1abb2008-07-22 11:16:55 +0100493
494 /* There is an outstanding reference here so this is safe */
Alan Coxc65c9bc2009-06-11 12:50:12 +0100495 old = tty_ldisc_get(old->ops->num);
496 WARN_ON(IS_ERR(old));
Alan Cox01e1abb2008-07-22 11:16:55 +0100497 tty_ldisc_assign(tty, old);
498 tty_set_termios_ldisc(tty, old->ops->num);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100499 if (tty_ldisc_open(tty, old) < 0) {
500 tty_ldisc_put(old);
Alan Cox01e1abb2008-07-22 11:16:55 +0100501 /* This driver is always present */
Alan Cox852e99d2009-06-11 12:51:41 +0100502 new_ldisc = tty_ldisc_get(N_TTY);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100503 if (IS_ERR(new_ldisc))
Alan Cox01e1abb2008-07-22 11:16:55 +0100504 panic("n_tty: get");
Alan Coxc65c9bc2009-06-11 12:50:12 +0100505 tty_ldisc_assign(tty, new_ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100506 tty_set_termios_ldisc(tty, N_TTY);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100507 r = tty_ldisc_open(tty, new_ldisc);
508 if (r < 0)
509 panic("Couldn't open N_TTY ldisc for "
510 "%s --- error %d.",
511 tty_name(tty, buf), r);
Alan Cox01e1abb2008-07-22 11:16:55 +0100512 }
513}
514
515/**
Alan Coxc65c9bc2009-06-11 12:50:12 +0100516 * tty_ldisc_halt - shut down the line discipline
Alan Coxe8b70e72009-06-11 12:48:02 +0100517 * @tty: tty device
518 *
519 * Shut down the line discipline and work queue for this tty device.
520 * The TTY_LDISC flag being cleared ensures no further references can
521 * be obtained while the delayed work queue halt ensures that no more
522 * data is fed to the ldisc.
523 *
Linus Torvalds5c58cef2009-08-25 09:12:43 -0700524 * You need to do a 'flush_scheduled_work()' (outside the ldisc_mutex)
525 * in order to make sure any currently executing ldisc work is also
526 * flushed.
Alan Coxe8b70e72009-06-11 12:48:02 +0100527 */
528
Alan Coxc65c9bc2009-06-11 12:50:12 +0100529static int tty_ldisc_halt(struct tty_struct *tty)
Alan Coxe8b70e72009-06-11 12:48:02 +0100530{
531 clear_bit(TTY_LDISC, &tty->flags);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100532 return cancel_delayed_work(&tty->buf.work);
Alan Coxe8b70e72009-06-11 12:48:02 +0100533}
534
535/**
Alan Cox01e1abb2008-07-22 11:16:55 +0100536 * tty_set_ldisc - set line discipline
537 * @tty: the terminal to set
538 * @ldisc: the line discipline
539 *
540 * Set the discipline of a tty line. Must be called from a process
Alan Coxc65c9bc2009-06-11 12:50:12 +0100541 * context. The ldisc change logic has to protect itself against any
542 * overlapping ldisc change (including on the other end of pty pairs),
543 * the close of one side of a tty/pty pair, and eventually hangup.
Alan Cox01e1abb2008-07-22 11:16:55 +0100544 *
Alan Coxc65c9bc2009-06-11 12:50:12 +0100545 * Locking: takes tty_ldisc_lock, termios_mutex
Alan Cox01e1abb2008-07-22 11:16:55 +0100546 */
547
548int tty_set_ldisc(struct tty_struct *tty, int ldisc)
549{
550 int retval;
Alan Coxc65c9bc2009-06-11 12:50:12 +0100551 struct tty_ldisc *o_ldisc, *new_ldisc;
552 int work, o_work = 0;
Alan Cox01e1abb2008-07-22 11:16:55 +0100553 struct tty_struct *o_tty;
554
Alan Coxc65c9bc2009-06-11 12:50:12 +0100555 new_ldisc = tty_ldisc_get(ldisc);
556 if (IS_ERR(new_ldisc))
557 return PTR_ERR(new_ldisc);
558
559 /*
560 * We need to look at the tty locking here for pty/tty pairs
561 * when both sides try to change in parallel.
562 */
563
564 o_tty = tty->link; /* o_tty is the pty side or NULL */
565
566
567 /*
568 * Check the no-op case
569 */
570
571 if (tty->ldisc->ops->num == ldisc) {
572 tty_ldisc_put(new_ldisc);
573 return 0;
574 }
Alan Cox01e1abb2008-07-22 11:16:55 +0100575
576 /*
577 * Problem: What do we do if this blocks ?
Alan Coxc65c9bc2009-06-11 12:50:12 +0100578 * We could deadlock here
Alan Cox01e1abb2008-07-22 11:16:55 +0100579 */
580
581 tty_wait_until_sent(tty, 0);
582
Alan Coxc65c9bc2009-06-11 12:50:12 +0100583 mutex_lock(&tty->ldisc_mutex);
Alan Cox01e1abb2008-07-22 11:16:55 +0100584
585 /*
Alan Coxc65c9bc2009-06-11 12:50:12 +0100586 * We could be midstream of another ldisc change which has
587 * dropped the lock during processing. If so we need to wait.
588 */
589
590 while (test_bit(TTY_LDISC_CHANGING, &tty->flags)) {
591 mutex_unlock(&tty->ldisc_mutex);
592 wait_event(tty_ldisc_wait,
593 test_bit(TTY_LDISC_CHANGING, &tty->flags) == 0);
594 mutex_lock(&tty->ldisc_mutex);
595 }
596 set_bit(TTY_LDISC_CHANGING, &tty->flags);
Alan Cox852e99d2009-06-11 12:51:41 +0100597
Alan Coxc65c9bc2009-06-11 12:50:12 +0100598 /*
Alan Cox01e1abb2008-07-22 11:16:55 +0100599 * No more input please, we are switching. The new ldisc
600 * will update this value in the ldisc open function
601 */
602
603 tty->receive_room = 0;
604
605 o_ldisc = tty->ldisc;
Alan Cox01e1abb2008-07-22 11:16:55 +0100606 /*
607 * Make sure we don't change while someone holds a
608 * reference to the line discipline. The TTY_LDISC bit
609 * prevents anyone taking a reference once it is clear.
610 * We need the lock to avoid racing reference takers.
Alan Coxc9b39762009-01-02 13:44:56 +0000611 *
612 * We must clear the TTY_LDISC bit here to avoid a livelock
613 * with a userspace app continually trying to use the tty in
614 * parallel to the change and re-referencing the tty.
Alan Cox01e1abb2008-07-22 11:16:55 +0100615 */
616
Alan Coxc65c9bc2009-06-11 12:50:12 +0100617 work = tty_ldisc_halt(tty);
Alan Cox01e1abb2008-07-22 11:16:55 +0100618 if (o_tty)
Alan Coxc65c9bc2009-06-11 12:50:12 +0100619 o_work = tty_ldisc_halt(o_tty);
620
Alan Cox01e1abb2008-07-22 11:16:55 +0100621 /*
Alan Coxc65c9bc2009-06-11 12:50:12 +0100622 * Wait for ->hangup_work and ->buf.work handlers to terminate.
623 * We must drop the mutex here in case a hangup is also in process.
Alan Cox01e1abb2008-07-22 11:16:55 +0100624 */
625
Alan Coxc65c9bc2009-06-11 12:50:12 +0100626 mutex_unlock(&tty->ldisc_mutex);
627
Alan Cox01e1abb2008-07-22 11:16:55 +0100628 flush_scheduled_work();
Alan Coxc65c9bc2009-06-11 12:50:12 +0100629
Alan Coxc65c9bc2009-06-11 12:50:12 +0100630 mutex_lock(&tty->ldisc_mutex);
631 if (test_bit(TTY_HUPPED, &tty->flags)) {
632 /* We were raced by the hangup method. It will have stomped
633 the ldisc data and closed the ldisc down */
634 clear_bit(TTY_LDISC_CHANGING, &tty->flags);
635 mutex_unlock(&tty->ldisc_mutex);
636 tty_ldisc_put(new_ldisc);
637 return -EIO;
638 }
639
Alan Cox01e1abb2008-07-22 11:16:55 +0100640 /* Shutdown the current discipline. */
Alan Coxc65c9bc2009-06-11 12:50:12 +0100641 tty_ldisc_close(tty, o_ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100642
643 /* Now set up the new line discipline. */
Alan Coxc65c9bc2009-06-11 12:50:12 +0100644 tty_ldisc_assign(tty, new_ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100645 tty_set_termios_ldisc(tty, ldisc);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100646
647 retval = tty_ldisc_open(tty, new_ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100648 if (retval < 0) {
Alan Coxc65c9bc2009-06-11 12:50:12 +0100649 /* Back to the old one or N_TTY if we can't */
650 tty_ldisc_put(new_ldisc);
651 tty_ldisc_restore(tty, o_ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100652 }
Alan Coxc65c9bc2009-06-11 12:50:12 +0100653
Alan Cox01e1abb2008-07-22 11:16:55 +0100654 /* At this point we hold a reference to the new ldisc and a
655 a reference to the old ldisc. If we ended up flipping back
656 to the existing ldisc we have two references to it */
657
Alan Coxc65c9bc2009-06-11 12:50:12 +0100658 if (tty->ldisc->ops->num != o_ldisc->ops->num && tty->ops->set_ldisc)
Alan Cox01e1abb2008-07-22 11:16:55 +0100659 tty->ops->set_ldisc(tty);
660
Alan Coxc65c9bc2009-06-11 12:50:12 +0100661 tty_ldisc_put(o_ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100662
663 /*
Alan Coxc65c9bc2009-06-11 12:50:12 +0100664 * Allow ldisc referencing to occur again
Alan Cox01e1abb2008-07-22 11:16:55 +0100665 */
666
667 tty_ldisc_enable(tty);
668 if (o_tty)
669 tty_ldisc_enable(o_tty);
670
Alan Coxc65c9bc2009-06-11 12:50:12 +0100671 /* Restart the work queue in case no characters kick it off. Safe if
Alan Cox01e1abb2008-07-22 11:16:55 +0100672 already running */
673 if (work)
674 schedule_delayed_work(&tty->buf.work, 1);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100675 if (o_work)
676 schedule_delayed_work(&o_tty->buf.work, 1);
677 mutex_unlock(&tty->ldisc_mutex);
Alan Cox01e1abb2008-07-22 11:16:55 +0100678 return retval;
679}
680
Alan Coxc65c9bc2009-06-11 12:50:12 +0100681/**
682 * tty_reset_termios - reset terminal state
683 * @tty: tty to reset
684 *
685 * Restore a terminal to the driver default state.
686 */
687
688static void tty_reset_termios(struct tty_struct *tty)
689{
690 mutex_lock(&tty->termios_mutex);
691 *tty->termios = tty->driver->init_termios;
692 tty->termios->c_ispeed = tty_termios_input_baud_rate(tty->termios);
693 tty->termios->c_ospeed = tty_termios_baud_rate(tty->termios);
694 mutex_unlock(&tty->termios_mutex);
695}
696
697
698/**
699 * tty_ldisc_reinit - reinitialise the tty ldisc
700 * @tty: tty to reinit
701 *
702 * Switch the tty back to N_TTY line discipline and leave the
703 * ldisc state closed
704 */
705
706static void tty_ldisc_reinit(struct tty_struct *tty)
707{
708 struct tty_ldisc *ld;
709
710 tty_ldisc_close(tty, tty->ldisc);
711 tty_ldisc_put(tty->ldisc);
712 tty->ldisc = NULL;
713 /*
714 * Switch the line discipline back
715 */
716 ld = tty_ldisc_get(N_TTY);
717 BUG_ON(IS_ERR(ld));
718 tty_ldisc_assign(tty, ld);
719 tty_set_termios_ldisc(tty, N_TTY);
720}
721
722/**
723 * tty_ldisc_hangup - hangup ldisc reset
724 * @tty: tty being hung up
725 *
726 * Some tty devices reset their termios when they receive a hangup
727 * event. In that situation we must also switch back to N_TTY properly
728 * before we reset the termios data.
729 *
730 * Locking: We can take the ldisc mutex as the rest of the code is
731 * careful to allow for this.
732 *
733 * In the pty pair case this occurs in the close() path of the
734 * tty itself so we must be careful about locking rules.
735 */
736
737void tty_ldisc_hangup(struct tty_struct *tty)
738{
739 struct tty_ldisc *ld;
740
741 /*
742 * FIXME! What are the locking issues here? This may me overdoing
743 * things... This question is especially important now that we've
744 * removed the irqlock.
745 */
746 ld = tty_ldisc_ref(tty);
747 if (ld != NULL) {
748 /* We may have no line discipline at this point */
749 if (ld->ops->flush_buffer)
750 ld->ops->flush_buffer(tty);
751 tty_driver_flush_buffer(tty);
752 if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
753 ld->ops->write_wakeup)
754 ld->ops->write_wakeup(tty);
755 if (ld->ops->hangup)
756 ld->ops->hangup(tty);
757 tty_ldisc_deref(ld);
758 }
759 /*
760 * FIXME: Once we trust the LDISC code better we can wait here for
761 * ldisc completion and fix the driver call race
762 */
763 wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
764 wake_up_interruptible_poll(&tty->read_wait, POLLIN);
765 /*
766 * Shutdown the current line discipline, and reset it to
767 * N_TTY.
768 */
769 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) {
Linus Torvalds5c58cef2009-08-25 09:12:43 -0700770 /* Make sure the old ldisc is quiescent */
771 tty_ldisc_halt(tty);
772 flush_scheduled_work();
773
Alan Coxc8d50042009-07-16 16:05:08 +0100774 /* Avoid racing set_ldisc or tty_ldisc_release */
Alan Coxc65c9bc2009-06-11 12:50:12 +0100775 mutex_lock(&tty->ldisc_mutex);
Alan Coxc8d50042009-07-16 16:05:08 +0100776 if (tty->ldisc) { /* Not yet closed */
777 /* Switch back to N_TTY */
Alan Coxc8d50042009-07-16 16:05:08 +0100778 tty_ldisc_reinit(tty);
779 /* At this point we have a closed ldisc and we want to
780 reopen it. We could defer this to the next open but
781 it means auditing a lot of other paths so this is
782 a FIXME */
783 WARN_ON(tty_ldisc_open(tty, tty->ldisc));
784 tty_ldisc_enable(tty);
785 }
Alan Coxc65c9bc2009-06-11 12:50:12 +0100786 mutex_unlock(&tty->ldisc_mutex);
787 tty_reset_termios(tty);
788 }
789}
Alan Cox01e1abb2008-07-22 11:16:55 +0100790
791/**
792 * tty_ldisc_setup - open line discipline
793 * @tty: tty being shut down
794 * @o_tty: pair tty for pty/tty pairs
795 *
796 * Called during the initial open of a tty/pty pair in order to set up the
Alan Coxc65c9bc2009-06-11 12:50:12 +0100797 * line disciplines and bind them to the tty. This has no locking issues
798 * as the device isn't yet active.
Alan Cox01e1abb2008-07-22 11:16:55 +0100799 */
800
801int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty)
802{
Alan Coxc65c9bc2009-06-11 12:50:12 +0100803 struct tty_ldisc *ld = tty->ldisc;
Alan Cox01e1abb2008-07-22 11:16:55 +0100804 int retval;
805
Alan Coxc65c9bc2009-06-11 12:50:12 +0100806 retval = tty_ldisc_open(tty, ld);
807 if (retval)
808 return retval;
809
810 if (o_tty) {
811 retval = tty_ldisc_open(o_tty, o_tty->ldisc);
Alan Cox01e1abb2008-07-22 11:16:55 +0100812 if (retval) {
Alan Coxc65c9bc2009-06-11 12:50:12 +0100813 tty_ldisc_close(tty, ld);
Alan Cox01e1abb2008-07-22 11:16:55 +0100814 return retval;
815 }
816 tty_ldisc_enable(o_tty);
817 }
818 tty_ldisc_enable(tty);
819 return 0;
820}
Alan Cox01e1abb2008-07-22 11:16:55 +0100821/**
822 * tty_ldisc_release - release line discipline
823 * @tty: tty being shut down
824 * @o_tty: pair tty for pty/tty pairs
825 *
Alan Cox852e99d2009-06-11 12:51:41 +0100826 * Called during the final close of a tty/pty pair in order to shut down
827 * the line discpline layer. On exit the ldisc assigned is N_TTY and the
Alan Coxc65c9bc2009-06-11 12:50:12 +0100828 * ldisc has not been opened.
Alan Cox01e1abb2008-07-22 11:16:55 +0100829 */
830
831void tty_ldisc_release(struct tty_struct *tty, struct tty_struct *o_tty)
832{
Alan Cox01e1abb2008-07-22 11:16:55 +0100833 /*
834 * Prevent flush_to_ldisc() from rescheduling the work for later. Then
835 * kill any delayed work. As this is the final close it does not
836 * race with the set_ldisc code path.
837 */
Alan Cox01e1abb2008-07-22 11:16:55 +0100838
Alan Coxe8b70e72009-06-11 12:48:02 +0100839 tty_ldisc_halt(tty);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100840 flush_scheduled_work();
Alan Cox01e1abb2008-07-22 11:16:55 +0100841
Alan Coxc8d50042009-07-16 16:05:08 +0100842 mutex_lock(&tty->ldisc_mutex);
Alan Cox01e1abb2008-07-22 11:16:55 +0100843 /*
Alan Coxaef29bc2009-06-29 15:21:47 +0100844 * Now kill off the ldisc
Alan Cox01e1abb2008-07-22 11:16:55 +0100845 */
Alan Coxaef29bc2009-06-29 15:21:47 +0100846 tty_ldisc_close(tty, tty->ldisc);
847 tty_ldisc_put(tty->ldisc);
848 /* Force an oops if we mess this up */
849 tty->ldisc = NULL;
Alan Cox01e1abb2008-07-22 11:16:55 +0100850
Alan Coxaef29bc2009-06-29 15:21:47 +0100851 /* Ensure the next open requests the N_TTY ldisc */
852 tty_set_termios_ldisc(tty, N_TTY);
Alan Coxc8d50042009-07-16 16:05:08 +0100853 mutex_unlock(&tty->ldisc_mutex);
Alan Coxaef29bc2009-06-29 15:21:47 +0100854
Alan Coxc65c9bc2009-06-11 12:50:12 +0100855 /* This will need doing differently if we need to lock */
856 if (o_tty)
857 tty_ldisc_release(o_tty, NULL);
Alan Coxaef29bc2009-06-29 15:21:47 +0100858
859 /* And the memory resources remaining (buffers, termios) will be
860 disposed of when the kref hits zero */
Alan Cox01e1abb2008-07-22 11:16:55 +0100861}
862
863/**
864 * tty_ldisc_init - ldisc setup for new tty
865 * @tty: tty being allocated
866 *
867 * Set up the line discipline objects for a newly allocated tty. Note that
868 * the tty structure is not completely set up when this call is made.
869 */
870
871void tty_ldisc_init(struct tty_struct *tty)
872{
Alan Coxc65c9bc2009-06-11 12:50:12 +0100873 struct tty_ldisc *ld = tty_ldisc_get(N_TTY);
874 if (IS_ERR(ld))
Alan Cox01e1abb2008-07-22 11:16:55 +0100875 panic("n_tty: init_tty");
Alan Coxc65c9bc2009-06-11 12:50:12 +0100876 tty_ldisc_assign(tty, ld);
Alan Cox01e1abb2008-07-22 11:16:55 +0100877}
878
879void tty_ldisc_begin(void)
880{
881 /* Setup the default TTY line discipline. */
882 (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);
883}