]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/char/tty_ldisc.c
tty-ldisc: make /proc/tty/ldiscs use ldisc_ops instead of ldiscs
[mirror_ubuntu-zesty-kernel.git] / drivers / char / tty_ldisc.c
CommitLineData
01e1abb2
AC
1#include <linux/types.h>
2#include <linux/major.h>
3#include <linux/errno.h>
4#include <linux/signal.h>
5#include <linux/fcntl.h>
6#include <linux/sched.h>
7#include <linux/interrupt.h>
8#include <linux/tty.h>
9#include <linux/tty_driver.h>
10#include <linux/tty_flip.h>
11#include <linux/devpts_fs.h>
12#include <linux/file.h>
01e1abb2
AC
13#include <linux/console.h>
14#include <linux/timer.h>
15#include <linux/ctype.h>
16#include <linux/kd.h>
17#include <linux/mm.h>
18#include <linux/string.h>
19#include <linux/slab.h>
20#include <linux/poll.h>
21#include <linux/proc_fs.h>
22#include <linux/init.h>
23#include <linux/module.h>
01e1abb2
AC
24#include <linux/device.h>
25#include <linux/wait.h>
26#include <linux/bitops.h>
27#include <linux/delay.h>
28#include <linux/seq_file.h>
29
30#include <linux/uaccess.h>
31#include <asm/system.h>
32
33#include <linux/kbd_kern.h>
34#include <linux/vt_kern.h>
35#include <linux/selection.h>
36
37#include <linux/kmod.h>
38#include <linux/nsproxy.h>
39
40/*
41 * This guards the refcounted line discipline lists. The lock
42 * must be taken with irqs off because there are hangup path
43 * callers who will do ldisc lookups and cannot sleep.
44 */
45
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
65b77046
LT
51static inline struct tty_ldisc *get_ldisc(struct tty_ldisc *ld)
52{
53 if (ld)
54 atomic_inc(&ld->users);
55 return ld;
56}
57
cbe9352f 58static void put_ldisc(struct tty_ldisc *ld)
65b77046 59{
cbe9352f
LT
60 unsigned long flags;
61
65b77046
LT
62 if (WARN_ON_ONCE(!ld))
63 return;
64
65 /*
66 * If this is the last user, free the ldisc, and
67 * release the ldisc ops.
cbe9352f
LT
68 *
69 * We really want an "atomic_dec_and_lock_irqsave()",
70 * but we don't have it, so this does it by hand.
65b77046 71 */
cbe9352f
LT
72 local_irq_save(flags);
73 if (atomic_dec_and_lock(&ld->users, &tty_ldisc_lock)) {
65b77046
LT
74 struct tty_ldisc_ops *ldo = ld->ops;
75
65b77046
LT
76 ldo->refcount--;
77 module_put(ldo->owner);
78 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
cbe9352f
LT
79
80 kfree(ld);
81 return;
65b77046 82 }
cbe9352f 83 local_irq_restore(flags);
65b77046
LT
84}
85
01e1abb2
AC
86/**
87 * tty_register_ldisc - install a line discipline
88 * @disc: ldisc number
89 * @new_ldisc: pointer to the ldisc object
90 *
91 * Installs a new line discipline into the kernel. The discipline
92 * is set up as unreferenced and then made available to the kernel
93 * from this point onwards.
94 *
95 * Locking:
96 * takes tty_ldisc_lock to guard against ldisc races
97 */
98
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
f0de0e8d
LT
148static 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}
01e1abb2
AC
176
177/**
178 * tty_ldisc_try_get - try and reference an ldisc
179 * @disc: ldisc number
01e1abb2
AC
180 *
181 * Attempt to open and lock a line discipline into place. Return
c65c9bc3 182 * the line discipline refcounted or an error.
01e1abb2
AC
183 */
184
c65c9bc3 185static struct tty_ldisc *tty_ldisc_try_get(int disc)
01e1abb2 186{
c65c9bc3 187 struct tty_ldisc *ld;
01e1abb2 188 struct tty_ldisc_ops *ldops;
852e99d2 189
c65c9bc3
AC
190 ld = kmalloc(sizeof(struct tty_ldisc), GFP_KERNEL);
191 if (ld == NULL)
192 return ERR_PTR(-ENOMEM);
193
f0de0e8d
LT
194 ldops = get_ldops(disc);
195 if (IS_ERR(ldops)) {
8d2ead74 196 kfree(ld);
f0de0e8d 197 return ERR_CAST(ldops);
8d2ead74 198 }
f0de0e8d
LT
199
200 ld->ops = ldops;
201 atomic_set(&ld->users, 1);
c65c9bc3 202 return ld;
01e1abb2
AC
203}
204
205/**
206 * tty_ldisc_get - take a reference to an ldisc
207 * @disc: ldisc number
01e1abb2
AC
208 *
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
c65c9bc3 218static struct tty_ldisc *tty_ldisc_get(int disc)
01e1abb2 219{
c65c9bc3 220 struct tty_ldisc *ld;
01e1abb2
AC
221
222 if (disc < N_TTY || disc >= NR_LDISCS)
c65c9bc3
AC
223 return ERR_PTR(-EINVAL);
224 ld = tty_ldisc_try_get(disc);
225 if (IS_ERR(ld)) {
01e1abb2 226 request_module("tty-ldisc-%d", disc);
c65c9bc3 227 ld = tty_ldisc_try_get(disc);
01e1abb2 228 }
c65c9bc3 229 return ld;
01e1abb2
AC
230}
231
852e99d2 232static void *tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos)
01e1abb2
AC
233{
234 return (*pos < NR_LDISCS) ? pos : NULL;
235}
236
852e99d2 237static void *tty_ldiscs_seq_next(struct seq_file *m, void *v, loff_t *pos)
01e1abb2
AC
238{
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;
f0de0e8d 250 struct tty_ldisc_ops *ldops;
852e99d2 251
f0de0e8d
LT
252 ldops = get_ldops(i);
253 if (IS_ERR(ldops))
01e1abb2 254 return 0;
f0de0e8d
LT
255 seq_printf(m, "%-10s %2d\n", ldops->name ? ldops->name : "???", i);
256 put_ldops(ldops);
01e1abb2
AC
257 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
8d2ead74 286 * ldisc must have a reference count above zero to ensure it remains.
01e1abb2
AC
287 * 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{
c65c9bc3 295 tty->ldisc = ld;
01e1abb2
AC
296}
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
65b77046 310static struct tty_ldisc *tty_ldisc_try(struct tty_struct *tty)
01e1abb2
AC
311{
312 unsigned long flags;
313 struct tty_ldisc *ld;
01e1abb2
AC
314
315 spin_lock_irqsave(&tty_ldisc_lock, flags);
65b77046
LT
316 ld = NULL;
317 if (test_bit(TTY_LDISC, &tty->flags))
318 ld = get_ldisc(tty->ldisc);
01e1abb2 319 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
65b77046 320 return ld;
01e1abb2
AC
321}
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{
65b77046
LT
341 struct tty_ldisc *ld;
342
01e1abb2 343 /* wait_event is a macro */
65b77046
LT
344 wait_event(tty_ldisc_wait, (ld = tty_ldisc_try(tty)) != NULL);
345 return ld;
01e1abb2 346}
01e1abb2
AC
347EXPORT_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{
65b77046 362 return tty_ldisc_try(tty);
01e1abb2 363}
01e1abb2
AC
364EXPORT_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{
65b77046 378 put_ldisc(ld);
01e1abb2 379}
01e1abb2
AC
380EXPORT_SYMBOL_GPL(tty_ldisc_deref);
381
65b77046
LT
382static inline void tty_ldisc_put(struct tty_ldisc *ld)
383{
384 put_ldisc(ld);
385}
386
01e1abb2
AC
387/**
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
c9b3976e
AC
392 * again. Do necessary wakeups for existing sleepers. Clear the LDISC
393 * changing flag to indicate any ldisc change is now over.
01e1abb2 394 *
c9b3976e
AC
395 * Note: nobody should set the TTY_LDISC bit except via this function.
396 * Clearing directly is allowed.
01e1abb2
AC
397 */
398
399void tty_ldisc_enable(struct tty_struct *tty)
400{
401 set_bit(TTY_LDISC, &tty->flags);
c9b3976e 402 clear_bit(TTY_LDISC_CHANGING, &tty->flags);
01e1abb2
AC
403 wake_up(&tty_ldisc_wait);
404}
405
f2c4c65c
AC
406/**
407 * 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}
f2c4c65c
AC
424EXPORT_SYMBOL_GPL(tty_ldisc_flush);
425
01e1abb2
AC
426/**
427 * 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
c65c9bc3
AC
445/**
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}
01e1abb2
AC
478
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];
c65c9bc3
AC
491 struct tty_ldisc *new_ldisc;
492 int r;
01e1abb2
AC
493
494 /* There is an outstanding reference here so this is safe */
c65c9bc3
AC
495 old = tty_ldisc_get(old->ops->num);
496 WARN_ON(IS_ERR(old));
01e1abb2
AC
497 tty_ldisc_assign(tty, old);
498 tty_set_termios_ldisc(tty, old->ops->num);
c65c9bc3
AC
499 if (tty_ldisc_open(tty, old) < 0) {
500 tty_ldisc_put(old);
01e1abb2 501 /* This driver is always present */
852e99d2 502 new_ldisc = tty_ldisc_get(N_TTY);
c65c9bc3 503 if (IS_ERR(new_ldisc))
01e1abb2 504 panic("n_tty: get");
c65c9bc3 505 tty_ldisc_assign(tty, new_ldisc);
01e1abb2 506 tty_set_termios_ldisc(tty, N_TTY);
c65c9bc3
AC
507 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);
01e1abb2
AC
512 }
513}
514
e8b70e7d 515/**
c65c9bc3 516 * tty_ldisc_halt - shut down the line discipline
e8b70e7d
AC
517 * @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 *
5c58ceff
LT
524 * 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.
e8b70e7d
AC
527 */
528
c65c9bc3 529static int tty_ldisc_halt(struct tty_struct *tty)
e8b70e7d
AC
530{
531 clear_bit(TTY_LDISC, &tty->flags);
c65c9bc3 532 return cancel_delayed_work(&tty->buf.work);
e8b70e7d
AC
533}
534
01e1abb2
AC
535/**
536 * 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
c65c9bc3
AC
541 * 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.
01e1abb2 544 *
c65c9bc3 545 * Locking: takes tty_ldisc_lock, termios_mutex
01e1abb2
AC
546 */
547
548int tty_set_ldisc(struct tty_struct *tty, int ldisc)
549{
550 int retval;
c65c9bc3
AC
551 struct tty_ldisc *o_ldisc, *new_ldisc;
552 int work, o_work = 0;
01e1abb2
AC
553 struct tty_struct *o_tty;
554
c65c9bc3
AC
555 new_ldisc = tty_ldisc_get(ldisc);
556 if (IS_ERR(new_ldisc))
557 return PTR_ERR(new_ldisc);
01e1abb2
AC
558
559 /*
c65c9bc3
AC
560 * We need to look at the tty locking here for pty/tty pairs
561 * when both sides try to change in parallel.
01e1abb2
AC
562 */
563
c65c9bc3
AC
564 o_tty = tty->link; /* o_tty is the pty side or NULL */
565
01e1abb2 566
c65c9bc3
AC
567 /*
568 * Check the no-op case
569 */
570
571 if (tty->ldisc->ops->num == ldisc) {
572 tty_ldisc_put(new_ldisc);
01e1abb2
AC
573 return 0;
574 }
575
c65c9bc3
AC
576 /*
577 * Problem: What do we do if this blocks ?
578 * We could deadlock here
579 */
580
581 tty_wait_until_sent(tty, 0);
582
583 mutex_lock(&tty->ldisc_mutex);
584
585 /*
586 * 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);
852e99d2 597
01e1abb2
AC
598 /*
599 * 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;
01e1abb2
AC
606 /*
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.
c9b3976e
AC
611 *
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.
01e1abb2
AC
615 */
616
c65c9bc3 617 work = tty_ldisc_halt(tty);
01e1abb2 618 if (o_tty)
c65c9bc3 619 o_work = tty_ldisc_halt(o_tty);
01e1abb2 620
01e1abb2 621 /*
c65c9bc3
AC
622 * 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.
01e1abb2 624 */
c65c9bc3
AC
625
626 mutex_unlock(&tty->ldisc_mutex);
627
01e1abb2 628 flush_scheduled_work();
c65c9bc3 629
c65c9bc3
AC
630 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
01e1abb2 640 /* Shutdown the current discipline. */
c65c9bc3 641 tty_ldisc_close(tty, o_ldisc);
01e1abb2
AC
642
643 /* Now set up the new line discipline. */
c65c9bc3 644 tty_ldisc_assign(tty, new_ldisc);
01e1abb2 645 tty_set_termios_ldisc(tty, ldisc);
c65c9bc3
AC
646
647 retval = tty_ldisc_open(tty, new_ldisc);
01e1abb2 648 if (retval < 0) {
c65c9bc3
AC
649 /* 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);
01e1abb2 652 }
c65c9bc3 653
01e1abb2
AC
654 /* 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
c65c9bc3 658 if (tty->ldisc->ops->num != o_ldisc->ops->num && tty->ops->set_ldisc)
01e1abb2
AC
659 tty->ops->set_ldisc(tty);
660
c65c9bc3 661 tty_ldisc_put(o_ldisc);
01e1abb2
AC
662
663 /*
c65c9bc3 664 * Allow ldisc referencing to occur again
01e1abb2
AC
665 */
666
667 tty_ldisc_enable(tty);
668 if (o_tty)
669 tty_ldisc_enable(o_tty);
670
c65c9bc3 671 /* Restart the work queue in case no characters kick it off. Safe if
01e1abb2
AC
672 already running */
673 if (work)
674 schedule_delayed_work(&tty->buf.work, 1);
c65c9bc3
AC
675 if (o_work)
676 schedule_delayed_work(&o_tty->buf.work, 1);
677 mutex_unlock(&tty->ldisc_mutex);
01e1abb2
AC
678 return retval;
679}
680
c65c9bc3
AC
681/**
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) {
5c58ceff
LT
770 /* Make sure the old ldisc is quiescent */
771 tty_ldisc_halt(tty);
772 flush_scheduled_work();
773
c8d50041 774 /* Avoid racing set_ldisc or tty_ldisc_release */
c65c9bc3 775 mutex_lock(&tty->ldisc_mutex);
c8d50041
AC
776 if (tty->ldisc) { /* Not yet closed */
777 /* Switch back to N_TTY */
c8d50041
AC
778 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 }
c65c9bc3
AC
786 mutex_unlock(&tty->ldisc_mutex);
787 tty_reset_termios(tty);
788 }
789}
01e1abb2
AC
790
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
c65c9bc3
AC
797 * line disciplines and bind them to the tty. This has no locking issues
798 * as the device isn't yet active.
01e1abb2
AC
799 */
800
801int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty)
802{
c65c9bc3 803 struct tty_ldisc *ld = tty->ldisc;
01e1abb2
AC
804 int retval;
805
c65c9bc3
AC
806 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);
01e1abb2 812 if (retval) {
c65c9bc3 813 tty_ldisc_close(tty, ld);
01e1abb2
AC
814 return retval;
815 }
816 tty_ldisc_enable(o_tty);
817 }
818 tty_ldisc_enable(tty);
819 return 0;
820}
01e1abb2
AC
821/**
822 * tty_ldisc_release - release line discipline
823 * @tty: tty being shut down
824 * @o_tty: pair tty for pty/tty pairs
825 *
852e99d2
AC
826 * 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
c65c9bc3 828 * ldisc has not been opened.
01e1abb2
AC
829 */
830
831void tty_ldisc_release(struct tty_struct *tty, struct tty_struct *o_tty)
832{
01e1abb2
AC
833 /*
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 */
01e1abb2 838
e8b70e7d 839 tty_ldisc_halt(tty);
c65c9bc3 840 flush_scheduled_work();
01e1abb2 841
c8d50041 842 mutex_lock(&tty->ldisc_mutex);
01e1abb2 843 /*
aef29bc2 844 * Now kill off the ldisc
01e1abb2 845 */
aef29bc2
AC
846 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;
850
851 /* Ensure the next open requests the N_TTY ldisc */
852 tty_set_termios_ldisc(tty, N_TTY);
c8d50041 853 mutex_unlock(&tty->ldisc_mutex);
01e1abb2 854
c65c9bc3
AC
855 /* This will need doing differently if we need to lock */
856 if (o_tty)
857 tty_ldisc_release(o_tty, NULL);
aef29bc2
AC
858
859 /* And the memory resources remaining (buffers, termios) will be
860 disposed of when the kref hits zero */
01e1abb2
AC
861}
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{
c65c9bc3
AC
873 struct tty_ldisc *ld = tty_ldisc_get(N_TTY);
874 if (IS_ERR(ld))
01e1abb2 875 panic("n_tty: init_tty");
c65c9bc3 876 tty_ldisc_assign(tty, ld);
01e1abb2
AC
877}
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}