Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1#include <linux/types.h>
2#include <linux/errno.h>
3#include <linux/kmod.h>
4#include <linux/sched.h>
5#include <linux/interrupt.h>
6#include <linux/tty.h>
7#include <linux/tty_driver.h>
8#include <linux/file.h>
9#include <linux/mm.h>
10#include <linux/string.h>
11#include <linux/slab.h>
12#include <linux/poll.h>
13#include <linux/proc_fs.h>
14#include <linux/module.h>
15#include <linux/device.h>
16#include <linux/wait.h>
17#include <linux/bitops.h>
18#include <linux/seq_file.h>
19#include <linux/uaccess.h>
20#include <linux/ratelimit.h>
21
22#undef LDISC_DEBUG_HANGUP
23
24#ifdef LDISC_DEBUG_HANGUP
25#define tty_ldisc_debug(tty, f, args...) ({ \
26 printk(KERN_DEBUG "%s: %s: " f, __func__, tty_name(tty), ##args); \
27})
28#else
29#define tty_ldisc_debug(tty, f, args...)
30#endif
31
32/* lockdep nested classes for tty->ldisc_sem */
33enum {
34 LDISC_SEM_NORMAL,
35 LDISC_SEM_OTHER,
36};
37
38
39/*
40 * This guards the refcounted line discipline lists. The lock
41 * must be taken with irqs off because there are hangup path
42 * callers who will do ldisc lookups and cannot sleep.
43 */
44
45static DEFINE_RAW_SPINLOCK(tty_ldiscs_lock);
46/* Line disc dispatch table */
47static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS];
48
49/**
50 * tty_register_ldisc - install a line discipline
51 * @disc: ldisc number
52 * @new_ldisc: pointer to the ldisc object
53 *
54 * Installs a new line discipline into the kernel. The discipline
55 * is set up as unreferenced and then made available to the kernel
56 * from this point onwards.
57 *
58 * Locking:
59 * takes tty_ldiscs_lock to guard against ldisc races
60 */
61
62int tty_register_ldisc(int disc, struct tty_ldisc_ops *new_ldisc)
63{
64 unsigned long flags;
65 int ret = 0;
66
67 if (disc < N_TTY || disc >= NR_LDISCS)
68 return -EINVAL;
69
70 raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
71 tty_ldiscs[disc] = new_ldisc;
72 new_ldisc->num = disc;
73 new_ldisc->refcount = 0;
74 raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
75
76 return ret;
77}
78EXPORT_SYMBOL(tty_register_ldisc);
79
80/**
81 * tty_unregister_ldisc - unload a line discipline
82 * @disc: ldisc number
83 * @new_ldisc: pointer to the ldisc object
84 *
85 * Remove a line discipline from the kernel providing it is not
86 * currently in use.
87 *
88 * Locking:
89 * takes tty_ldiscs_lock to guard against ldisc races
90 */
91
92int tty_unregister_ldisc(int disc)
93{
94 unsigned long flags;
95 int ret = 0;
96
97 if (disc < N_TTY || disc >= NR_LDISCS)
98 return -EINVAL;
99
100 raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
101 if (tty_ldiscs[disc]->refcount)
102 ret = -EBUSY;
103 else
104 tty_ldiscs[disc] = NULL;
105 raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
106
107 return ret;
108}
109EXPORT_SYMBOL(tty_unregister_ldisc);
110
111static struct tty_ldisc_ops *get_ldops(int disc)
112{
113 unsigned long flags;
114 struct tty_ldisc_ops *ldops, *ret;
115
116 raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
117 ret = ERR_PTR(-EINVAL);
118 ldops = tty_ldiscs[disc];
119 if (ldops) {
120 ret = ERR_PTR(-EAGAIN);
121 if (try_module_get(ldops->owner)) {
122 ldops->refcount++;
123 ret = ldops;
124 }
125 }
126 raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
127 return ret;
128}
129
130static void put_ldops(struct tty_ldisc_ops *ldops)
131{
132 unsigned long flags;
133
134 raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
135 ldops->refcount--;
136 module_put(ldops->owner);
137 raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
138}
139
140/**
141 * tty_ldisc_get - take a reference to an ldisc
142 * @disc: ldisc number
143 *
144 * Takes a reference to a line discipline. Deals with refcounts and
145 * module locking counts. Returns NULL if the discipline is not available.
146 * Returns a pointer to the discipline and bumps the ref count if it is
147 * available
148 *
149 * Locking:
150 * takes tty_ldiscs_lock to guard against ldisc races
151 */
152
153static struct tty_ldisc *tty_ldisc_get(struct tty_struct *tty, int disc)
154{
155 struct tty_ldisc *ld;
156 struct tty_ldisc_ops *ldops;
157
158 if (disc < N_TTY || disc >= NR_LDISCS)
159 return ERR_PTR(-EINVAL);
160
161 /*
162 * Get the ldisc ops - we may need to request them to be loaded
163 * dynamically and try again.
164 */
165 ldops = get_ldops(disc);
166 if (IS_ERR(ldops)) {
167 request_module("tty-ldisc-%d", disc);
168 ldops = get_ldops(disc);
169 if (IS_ERR(ldops))
170 return ERR_CAST(ldops);
171 }
172
173 ld = kmalloc(sizeof(struct tty_ldisc), GFP_KERNEL);
174 if (ld == NULL) {
175 put_ldops(ldops);
176 return ERR_PTR(-ENOMEM);
177 }
178
179 ld->ops = ldops;
180 ld->tty = tty;
181
182 return ld;
183}
184
185/**
186 * tty_ldisc_put - release the ldisc
187 *
188 * Complement of tty_ldisc_get().
189 */
190static inline void tty_ldisc_put(struct tty_ldisc *ld)
191{
192 if (WARN_ON_ONCE(!ld))
193 return;
194
195 put_ldops(ld->ops);
196 kfree(ld);
197}
198
199static void *tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos)
200{
201 return (*pos < NR_LDISCS) ? pos : NULL;
202}
203
204static void *tty_ldiscs_seq_next(struct seq_file *m, void *v, loff_t *pos)
205{
206 (*pos)++;
207 return (*pos < NR_LDISCS) ? pos : NULL;
208}
209
210static void tty_ldiscs_seq_stop(struct seq_file *m, void *v)
211{
212}
213
214static int tty_ldiscs_seq_show(struct seq_file *m, void *v)
215{
216 int i = *(loff_t *)v;
217 struct tty_ldisc_ops *ldops;
218
219 ldops = get_ldops(i);
220 if (IS_ERR(ldops))
221 return 0;
222 seq_printf(m, "%-10s %2d\n", ldops->name ? ldops->name : "???", i);
223 put_ldops(ldops);
224 return 0;
225}
226
227static const struct seq_operations tty_ldiscs_seq_ops = {
228 .start = tty_ldiscs_seq_start,
229 .next = tty_ldiscs_seq_next,
230 .stop = tty_ldiscs_seq_stop,
231 .show = tty_ldiscs_seq_show,
232};
233
234static int proc_tty_ldiscs_open(struct inode *inode, struct file *file)
235{
236 return seq_open(file, &tty_ldiscs_seq_ops);
237}
238
239const struct file_operations tty_ldiscs_proc_fops = {
240 .owner = THIS_MODULE,
241 .open = proc_tty_ldiscs_open,
242 .read = seq_read,
243 .llseek = seq_lseek,
244 .release = seq_release,
245};
246
247/**
248 * tty_ldisc_ref_wait - wait for the tty ldisc
249 * @tty: tty device
250 *
251 * Dereference the line discipline for the terminal and take a
252 * reference to it. If the line discipline is in flux then
253 * wait patiently until it changes.
254 *
255 * Note: Must not be called from an IRQ/timer context. The caller
256 * must also be careful not to hold other locks that will deadlock
257 * against a discipline change, such as an existing ldisc reference
258 * (which we check for)
259 *
260 * Note: only callable from a file_operations routine (which
261 * guarantees tty->ldisc != NULL when the lock is acquired).
262 */
263
264struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty)
265{
266 ldsem_down_read(&tty->ldisc_sem, MAX_SCHEDULE_TIMEOUT);
267 WARN_ON(!tty->ldisc);
268 return tty->ldisc;
269}
270EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait);
271
272/**
273 * tty_ldisc_ref - get the tty ldisc
274 * @tty: tty device
275 *
276 * Dereference the line discipline for the terminal and take a
277 * reference to it. If the line discipline is in flux then
278 * return NULL. Can be called from IRQ and timer functions.
279 */
280
281struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty)
282{
283 struct tty_ldisc *ld = NULL;
284
285 if (ldsem_down_read_trylock(&tty->ldisc_sem)) {
286 ld = tty->ldisc;
287 if (!ld)
288 ldsem_up_read(&tty->ldisc_sem);
289 }
290 return ld;
291}
292EXPORT_SYMBOL_GPL(tty_ldisc_ref);
293
294/**
295 * tty_ldisc_deref - free a tty ldisc reference
296 * @ld: reference to free up
297 *
298 * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May
299 * be called in IRQ context.
300 */
301
302void tty_ldisc_deref(struct tty_ldisc *ld)
303{
304 ldsem_up_read(&ld->tty->ldisc_sem);
305}
306EXPORT_SYMBOL_GPL(tty_ldisc_deref);
307
308
309static inline int __lockfunc
310__tty_ldisc_lock(struct tty_struct *tty, unsigned long timeout)
311{
312 return ldsem_down_write(&tty->ldisc_sem, timeout);
313}
314
315static inline int __lockfunc
316__tty_ldisc_lock_nested(struct tty_struct *tty, unsigned long timeout)
317{
318 return ldsem_down_write_nested(&tty->ldisc_sem,
319 LDISC_SEM_OTHER, timeout);
320}
321
322static inline void __tty_ldisc_unlock(struct tty_struct *tty)
323{
324 return ldsem_up_write(&tty->ldisc_sem);
325}
326
327static int __lockfunc
328tty_ldisc_lock(struct tty_struct *tty, unsigned long timeout)
329{
330 int ret;
331
332 ret = __tty_ldisc_lock(tty, timeout);
333 if (!ret)
334 return -EBUSY;
335 set_bit(TTY_LDISC_HALTED, &tty->flags);
336 return 0;
337}
338
339static void tty_ldisc_unlock(struct tty_struct *tty)
340{
341 clear_bit(TTY_LDISC_HALTED, &tty->flags);
342 __tty_ldisc_unlock(tty);
343}
344
345static int __lockfunc
346tty_ldisc_lock_pair_timeout(struct tty_struct *tty, struct tty_struct *tty2,
347 unsigned long timeout)
348{
349 int ret;
350
351 if (tty < tty2) {
352 ret = __tty_ldisc_lock(tty, timeout);
353 if (ret) {
354 ret = __tty_ldisc_lock_nested(tty2, timeout);
355 if (!ret)
356 __tty_ldisc_unlock(tty);
357 }
358 } else {
359 /* if this is possible, it has lots of implications */
360 WARN_ON_ONCE(tty == tty2);
361 if (tty2 && tty != tty2) {
362 ret = __tty_ldisc_lock(tty2, timeout);
363 if (ret) {
364 ret = __tty_ldisc_lock_nested(tty, timeout);
365 if (!ret)
366 __tty_ldisc_unlock(tty2);
367 }
368 } else
369 ret = __tty_ldisc_lock(tty, timeout);
370 }
371
372 if (!ret)
373 return -EBUSY;
374
375 set_bit(TTY_LDISC_HALTED, &tty->flags);
376 if (tty2)
377 set_bit(TTY_LDISC_HALTED, &tty2->flags);
378 return 0;
379}
380
381static void __lockfunc
382tty_ldisc_lock_pair(struct tty_struct *tty, struct tty_struct *tty2)
383{
384 tty_ldisc_lock_pair_timeout(tty, tty2, MAX_SCHEDULE_TIMEOUT);
385}
386
387static void __lockfunc tty_ldisc_unlock_pair(struct tty_struct *tty,
388 struct tty_struct *tty2)
389{
390 __tty_ldisc_unlock(tty);
391 if (tty2)
392 __tty_ldisc_unlock(tty2);
393}
394
395/**
396 * tty_ldisc_flush - flush line discipline queue
397 * @tty: tty
398 *
399 * Flush the line discipline queue (if any) and the tty flip buffers
400 * for this tty.
401 */
402
403void tty_ldisc_flush(struct tty_struct *tty)
404{
405 struct tty_ldisc *ld = tty_ldisc_ref(tty);
406
407 tty_buffer_flush(tty, ld);
408 if (ld)
409 tty_ldisc_deref(ld);
410}
411EXPORT_SYMBOL_GPL(tty_ldisc_flush);
412
413/**
414 * tty_set_termios_ldisc - set ldisc field
415 * @tty: tty structure
416 * @num: line discipline number
417 *
418 * This is probably overkill for real world processors but
419 * they are not on hot paths so a little discipline won't do
420 * any harm.
421 *
422 * Locking: takes termios_rwsem
423 */
424
425static void tty_set_termios_ldisc(struct tty_struct *tty, int num)
426{
427 down_write(&tty->termios_rwsem);
428 tty->termios.c_line = num;
429 up_write(&tty->termios_rwsem);
430}
431
432/**
433 * tty_ldisc_open - open a line discipline
434 * @tty: tty we are opening the ldisc on
435 * @ld: discipline to open
436 *
437 * A helper opening method. Also a convenient debugging and check
438 * point.
439 *
440 * Locking: always called with BTM already held.
441 */
442
443static int tty_ldisc_open(struct tty_struct *tty, struct tty_ldisc *ld)
444{
445 WARN_ON(test_and_set_bit(TTY_LDISC_OPEN, &tty->flags));
446 if (ld->ops->open) {
447 int ret;
448 /* BTM here locks versus a hangup event */
449 ret = ld->ops->open(tty);
450 if (ret)
451 clear_bit(TTY_LDISC_OPEN, &tty->flags);
452 return ret;
453 }
454 return 0;
455}
456
457/**
458 * tty_ldisc_close - close a line discipline
459 * @tty: tty we are opening the ldisc on
460 * @ld: discipline to close
461 *
462 * A helper close method. Also a convenient debugging and check
463 * point.
464 */
465
466static void tty_ldisc_close(struct tty_struct *tty, struct tty_ldisc *ld)
467{
468 WARN_ON(!test_bit(TTY_LDISC_OPEN, &tty->flags));
469 clear_bit(TTY_LDISC_OPEN, &tty->flags);
470 if (ld->ops->close)
471 ld->ops->close(tty);
472}
473
474/**
475 * tty_ldisc_restore - helper for tty ldisc change
476 * @tty: tty to recover
477 * @old: previous ldisc
478 *
479 * Restore the previous line discipline or N_TTY when a line discipline
480 * change fails due to an open error
481 */
482
483static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old)
484{
485 struct tty_ldisc *new_ldisc;
486 int r;
487
488 /* There is an outstanding reference here so this is safe */
489 old = tty_ldisc_get(tty, old->ops->num);
490 WARN_ON(IS_ERR(old));
491 tty->ldisc = old;
492 tty_set_termios_ldisc(tty, old->ops->num);
493 if (tty_ldisc_open(tty, old) < 0) {
494 tty_ldisc_put(old);
495 /* This driver is always present */
496 new_ldisc = tty_ldisc_get(tty, N_TTY);
497 if (IS_ERR(new_ldisc))
498 panic("n_tty: get");
499 tty->ldisc = new_ldisc;
500 tty_set_termios_ldisc(tty, N_TTY);
501 r = tty_ldisc_open(tty, new_ldisc);
502 if (r < 0)
503 panic("Couldn't open N_TTY ldisc for "
504 "%s --- error %d.",
505 tty_name(tty), r);
506 }
507}
508
509/**
510 * tty_set_ldisc - set line discipline
511 * @tty: the terminal to set
512 * @ldisc: the line discipline
513 *
514 * Set the discipline of a tty line. Must be called from a process
515 * context. The ldisc change logic has to protect itself against any
516 * overlapping ldisc change (including on the other end of pty pairs),
517 * the close of one side of a tty/pty pair, and eventually hangup.
518 */
519
520int tty_set_ldisc(struct tty_struct *tty, int ldisc)
521{
522 int retval;
523 struct tty_ldisc *old_ldisc, *new_ldisc;
524
525 new_ldisc = tty_ldisc_get(tty, ldisc);
526 if (IS_ERR(new_ldisc))
527 return PTR_ERR(new_ldisc);
528
529 tty_lock(tty);
530 retval = tty_ldisc_lock(tty, 5 * HZ);
531 if (retval) {
532 tty_ldisc_put(new_ldisc);
533 tty_unlock(tty);
534 return retval;
535 }
536
537 /*
538 * Check the no-op case
539 */
540
541 if (tty->ldisc->ops->num == ldisc) {
542 tty_ldisc_unlock(tty);
543 tty_ldisc_put(new_ldisc);
544 tty_unlock(tty);
545 return 0;
546 }
547
548 old_ldisc = tty->ldisc;
549
550 if (test_bit(TTY_HUPPED, &tty->flags)) {
551 /* We were raced by the hangup method. It will have stomped
552 the ldisc data and closed the ldisc down */
553 tty_ldisc_unlock(tty);
554 tty_ldisc_put(new_ldisc);
555 tty_unlock(tty);
556 return -EIO;
557 }
558
559 /* Shutdown the old discipline. */
560 tty_ldisc_close(tty, old_ldisc);
561
562 /* Now set up the new line discipline. */
563 tty->ldisc = new_ldisc;
564 tty_set_termios_ldisc(tty, ldisc);
565
566 retval = tty_ldisc_open(tty, new_ldisc);
567 if (retval < 0) {
568 /* Back to the old one or N_TTY if we can't */
569 tty_ldisc_put(new_ldisc);
570 tty_ldisc_restore(tty, old_ldisc);
571 }
572
573 if (tty->ldisc->ops->num != old_ldisc->ops->num && tty->ops->set_ldisc) {
574 down_read(&tty->termios_rwsem);
575 tty->ops->set_ldisc(tty);
576 up_read(&tty->termios_rwsem);
577 }
578
579 /* At this point we hold a reference to the new ldisc and a
580 reference to the old ldisc, or we hold two references to
581 the old ldisc (if it was restored as part of error cleanup
582 above). In either case, releasing a single reference from
583 the old ldisc is correct. */
584
585 tty_ldisc_put(old_ldisc);
586
587 /*
588 * Allow ldisc referencing to occur again
589 */
590 tty_ldisc_unlock(tty);
591
592 /* Restart the work queue in case no characters kick it off. Safe if
593 already running */
594 schedule_work(&tty->port->buf.work);
595
596 tty_unlock(tty);
597 return retval;
598}
599
600/**
601 * tty_reset_termios - reset terminal state
602 * @tty: tty to reset
603 *
604 * Restore a terminal to the driver default state.
605 */
606
607static void tty_reset_termios(struct tty_struct *tty)
608{
609 down_write(&tty->termios_rwsem);
610 tty->termios = tty->driver->init_termios;
611 tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios);
612 tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios);
613 up_write(&tty->termios_rwsem);
614}
615
616
617/**
618 * tty_ldisc_reinit - reinitialise the tty ldisc
619 * @tty: tty to reinit
620 * @ldisc: line discipline to reinitialize
621 *
622 * Switch the tty to a line discipline and leave the ldisc
623 * state closed
624 */
625
626static int tty_ldisc_reinit(struct tty_struct *tty, int ldisc)
627{
628 struct tty_ldisc *ld = tty_ldisc_get(tty, ldisc);
629
630 if (IS_ERR(ld))
631 return -1;
632
633 tty_ldisc_close(tty, tty->ldisc);
634 tty_ldisc_put(tty->ldisc);
635 /*
636 * Switch the line discipline back
637 */
638 tty->ldisc = ld;
639 tty_set_termios_ldisc(tty, ldisc);
640
641 return 0;
642}
643
644/**
645 * tty_ldisc_hangup - hangup ldisc reset
646 * @tty: tty being hung up
647 *
648 * Some tty devices reset their termios when they receive a hangup
649 * event. In that situation we must also switch back to N_TTY properly
650 * before we reset the termios data.
651 *
652 * Locking: We can take the ldisc mutex as the rest of the code is
653 * careful to allow for this.
654 *
655 * In the pty pair case this occurs in the close() path of the
656 * tty itself so we must be careful about locking rules.
657 */
658
659void tty_ldisc_hangup(struct tty_struct *tty)
660{
661 struct tty_ldisc *ld;
662 int reset = tty->driver->flags & TTY_DRIVER_RESET_TERMIOS;
663 int err = 0;
664
665 tty_ldisc_debug(tty, "closing ldisc: %p\n", tty->ldisc);
666
667 ld = tty_ldisc_ref(tty);
668 if (ld != NULL) {
669 if (ld->ops->flush_buffer)
670 ld->ops->flush_buffer(tty);
671 tty_driver_flush_buffer(tty);
672 if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
673 ld->ops->write_wakeup)
674 ld->ops->write_wakeup(tty);
675 if (ld->ops->hangup)
676 ld->ops->hangup(tty);
677 tty_ldisc_deref(ld);
678 }
679
680 wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
681 wake_up_interruptible_poll(&tty->read_wait, POLLIN);
682
683 /*
684 * Shutdown the current line discipline, and reset it to
685 * N_TTY if need be.
686 *
687 * Avoid racing set_ldisc or tty_ldisc_release
688 */
689 tty_ldisc_lock(tty, MAX_SCHEDULE_TIMEOUT);
690
691 if (tty->ldisc) {
692
693 /* At this point we have a halted ldisc; we want to close it and
694 reopen a new ldisc. We could defer the reopen to the next
695 open but it means auditing a lot of other paths so this is
696 a FIXME */
697 if (reset == 0) {
698
699 if (!tty_ldisc_reinit(tty, tty->termios.c_line))
700 err = tty_ldisc_open(tty, tty->ldisc);
701 else
702 err = 1;
703 }
704 /* If the re-open fails or we reset then go to N_TTY. The
705 N_TTY open cannot fail */
706 if (reset || err) {
707 BUG_ON(tty_ldisc_reinit(tty, N_TTY));
708 WARN_ON(tty_ldisc_open(tty, tty->ldisc));
709 }
710 }
711 tty_ldisc_unlock(tty);
712 if (reset)
713 tty_reset_termios(tty);
714
715 tty_ldisc_debug(tty, "re-opened ldisc: %p\n", tty->ldisc);
716}
717
718/**
719 * tty_ldisc_setup - open line discipline
720 * @tty: tty being shut down
721 * @o_tty: pair tty for pty/tty pairs
722 *
723 * Called during the initial open of a tty/pty pair in order to set up the
724 * line disciplines and bind them to the tty. This has no locking issues
725 * as the device isn't yet active.
726 */
727
728int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty)
729{
730 struct tty_ldisc *ld = tty->ldisc;
731 int retval;
732
733 retval = tty_ldisc_open(tty, ld);
734 if (retval)
735 return retval;
736
737 if (o_tty) {
738 retval = tty_ldisc_open(o_tty, o_tty->ldisc);
739 if (retval) {
740 tty_ldisc_close(tty, ld);
741 return retval;
742 }
743 }
744 return 0;
745}
746
747static void tty_ldisc_kill(struct tty_struct *tty)
748{
749 /*
750 * Now kill off the ldisc
751 */
752 tty_ldisc_close(tty, tty->ldisc);
753 tty_ldisc_put(tty->ldisc);
754 /* Force an oops if we mess this up */
755 tty->ldisc = NULL;
756
757 /* Ensure the next open requests the N_TTY ldisc */
758 tty_set_termios_ldisc(tty, N_TTY);
759}
760
761/**
762 * tty_ldisc_release - release line discipline
763 * @tty: tty being shut down (or one end of pty pair)
764 *
765 * Called during the final close of a tty or a pty pair in order to shut
766 * down the line discpline layer. On exit, each ldisc assigned is N_TTY and
767 * each ldisc has not been opened.
768 */
769
770void tty_ldisc_release(struct tty_struct *tty)
771{
772 struct tty_struct *o_tty = tty->link;
773
774 /*
775 * Shutdown this line discipline. As this is the final close,
776 * it does not race with the set_ldisc code path.
777 */
778
779 tty_ldisc_debug(tty, "closing ldisc: %p\n", tty->ldisc);
780
781 tty_ldisc_lock_pair(tty, o_tty);
782 tty_ldisc_kill(tty);
783 if (o_tty)
784 tty_ldisc_kill(o_tty);
785 tty_ldisc_unlock_pair(tty, o_tty);
786
787 /* And the memory resources remaining (buffers, termios) will be
788 disposed of when the kref hits zero */
789
790 tty_ldisc_debug(tty, "ldisc closed\n");
791}
792
793/**
794 * tty_ldisc_init - ldisc setup for new tty
795 * @tty: tty being allocated
796 *
797 * Set up the line discipline objects for a newly allocated tty. Note that
798 * the tty structure is not completely set up when this call is made.
799 */
800
801void tty_ldisc_init(struct tty_struct *tty)
802{
803 struct tty_ldisc *ld = tty_ldisc_get(tty, N_TTY);
804 if (IS_ERR(ld))
805 panic("n_tty: init_tty");
806 tty->ldisc = ld;
807}
808
809/**
810 * tty_ldisc_init - ldisc cleanup for new tty
811 * @tty: tty that was allocated recently
812 *
813 * The tty structure must not becompletely set up (tty_ldisc_setup) when
814 * this call is made.
815 */
816void tty_ldisc_deinit(struct tty_struct *tty)
817{
818 tty_ldisc_put(tty->ldisc);
819 tty->ldisc = NULL;
820}
821
822void tty_ldisc_begin(void)
823{
824 /* Setup the default TTY line discipline. */
825 (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);
826}