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>
7 #include <linux/tty_driver.h>
8 #include <linux/file.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/init.h>
15 #include <linux/module.h>
16 #include <linux/device.h>
17 #include <linux/wait.h>
18 #include <linux/bitops.h>
19 #include <linux/seq_file.h>
20 #include <linux/uaccess.h>
21 #include <linux/ratelimit.h>
23 #undef LDISC_DEBUG_HANGUP
25 #ifdef LDISC_DEBUG_HANGUP
26 #define tty_ldisc_debug(tty, f, args...) ({ \
28 printk(KERN_DEBUG "%s: %s: " f, __func__, tty_name(tty, __b), ##args); \
31 #define tty_ldisc_debug(tty, f, args...)
35 * This guards the refcounted line discipline lists. The lock
36 * must be taken with irqs off because there are hangup path
37 * callers who will do ldisc lookups and cannot sleep.
40 static DEFINE_RAW_SPINLOCK(tty_ldisc_lock);
41 static DECLARE_WAIT_QUEUE_HEAD(tty_ldisc_wait);
42 /* Line disc dispatch table */
43 static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS];
46 * tty_register_ldisc - install a line discipline
48 * @new_ldisc: pointer to the ldisc object
50 * Installs a new line discipline into the kernel. The discipline
51 * is set up as unreferenced and then made available to the kernel
52 * from this point onwards.
55 * takes tty_ldisc_lock to guard against ldisc races
58 int tty_register_ldisc(int disc, struct tty_ldisc_ops *new_ldisc)
63 if (disc < N_TTY || disc >= NR_LDISCS)
66 raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
67 tty_ldiscs[disc] = new_ldisc;
68 new_ldisc->num = disc;
69 new_ldisc->refcount = 0;
70 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
74 EXPORT_SYMBOL(tty_register_ldisc);
77 * tty_unregister_ldisc - unload a line discipline
79 * @new_ldisc: pointer to the ldisc object
81 * Remove a line discipline from the kernel providing it is not
85 * takes tty_ldisc_lock to guard against ldisc races
88 int tty_unregister_ldisc(int disc)
93 if (disc < N_TTY || disc >= NR_LDISCS)
96 raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
97 if (tty_ldiscs[disc]->refcount)
100 tty_ldiscs[disc] = NULL;
101 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
105 EXPORT_SYMBOL(tty_unregister_ldisc);
107 static struct tty_ldisc_ops *get_ldops(int disc)
110 struct tty_ldisc_ops *ldops, *ret;
112 raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
113 ret = ERR_PTR(-EINVAL);
114 ldops = tty_ldiscs[disc];
116 ret = ERR_PTR(-EAGAIN);
117 if (try_module_get(ldops->owner)) {
122 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
126 static void put_ldops(struct tty_ldisc_ops *ldops)
130 raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
132 module_put(ldops->owner);
133 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
137 * tty_ldisc_get - take a reference to an ldisc
138 * @disc: ldisc number
140 * Takes a reference to a line discipline. Deals with refcounts and
141 * module locking counts. Returns NULL if the discipline is not available.
142 * Returns a pointer to the discipline and bumps the ref count if it is
146 * takes tty_ldisc_lock to guard against ldisc races
149 static struct tty_ldisc *tty_ldisc_get(int disc)
151 struct tty_ldisc *ld;
152 struct tty_ldisc_ops *ldops;
154 if (disc < N_TTY || disc >= NR_LDISCS)
155 return ERR_PTR(-EINVAL);
158 * Get the ldisc ops - we may need to request them to be loaded
159 * dynamically and try again.
161 ldops = get_ldops(disc);
163 request_module("tty-ldisc-%d", disc);
164 ldops = get_ldops(disc);
166 return ERR_CAST(ldops);
169 ld = kmalloc(sizeof(struct tty_ldisc), GFP_KERNEL);
172 return ERR_PTR(-ENOMEM);
176 atomic_set(&ld->users, 1);
177 init_waitqueue_head(&ld->wq_idle);
182 static void *tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos)
184 return (*pos < NR_LDISCS) ? pos : NULL;
187 static void *tty_ldiscs_seq_next(struct seq_file *m, void *v, loff_t *pos)
190 return (*pos < NR_LDISCS) ? pos : NULL;
193 static void tty_ldiscs_seq_stop(struct seq_file *m, void *v)
197 static int tty_ldiscs_seq_show(struct seq_file *m, void *v)
199 int i = *(loff_t *)v;
200 struct tty_ldisc_ops *ldops;
202 ldops = get_ldops(i);
205 seq_printf(m, "%-10s %2d\n", ldops->name ? ldops->name : "???", i);
210 static const struct seq_operations tty_ldiscs_seq_ops = {
211 .start = tty_ldiscs_seq_start,
212 .next = tty_ldiscs_seq_next,
213 .stop = tty_ldiscs_seq_stop,
214 .show = tty_ldiscs_seq_show,
217 static int proc_tty_ldiscs_open(struct inode *inode, struct file *file)
219 return seq_open(file, &tty_ldiscs_seq_ops);
222 const struct file_operations tty_ldiscs_proc_fops = {
223 .owner = THIS_MODULE,
224 .open = proc_tty_ldiscs_open,
227 .release = seq_release,
231 * tty_ldisc_assign - set ldisc on a tty
232 * @tty: tty to assign
233 * @ld: line discipline
235 * Install an instance of a line discipline into a tty structure. The
236 * ldisc must have a reference count above zero to ensure it remains.
237 * The tty instance refcount starts at zero.
240 * Caller must hold references
243 static void tty_ldisc_assign(struct tty_struct *tty, struct tty_ldisc *ld)
249 * tty_ldisc_try - internal helper
252 * Make a single attempt to grab and bump the refcount on
253 * the tty ldisc. Return 0 on failure or 1 on success. This is
254 * used to implement both the waiting and non waiting versions
257 * Locking: takes tty_ldisc_lock
260 static struct tty_ldisc *tty_ldisc_try(struct tty_struct *tty)
263 struct tty_ldisc *ld;
265 /* FIXME: this allows reference acquire after TTY_LDISC is cleared */
266 raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
268 if (test_bit(TTY_LDISC, &tty->flags) && tty->ldisc) {
270 atomic_inc(&ld->users);
272 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
277 * tty_ldisc_ref_wait - wait for the tty ldisc
280 * Dereference the line discipline for the terminal and take a
281 * reference to it. If the line discipline is in flux then
282 * wait patiently until it changes.
284 * Note: Must not be called from an IRQ/timer context. The caller
285 * must also be careful not to hold other locks that will deadlock
286 * against a discipline change, such as an existing ldisc reference
287 * (which we check for)
289 * Locking: call functions take tty_ldisc_lock
292 struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty)
294 struct tty_ldisc *ld;
296 /* wait_event is a macro */
297 wait_event(tty_ldisc_wait, (ld = tty_ldisc_try(tty)) != NULL);
300 EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait);
303 * tty_ldisc_ref - get the tty ldisc
306 * Dereference the line discipline for the terminal and take a
307 * reference to it. If the line discipline is in flux then
308 * return NULL. Can be called from IRQ and timer functions.
310 * Locking: called functions take tty_ldisc_lock
313 struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty)
315 return tty_ldisc_try(tty);
317 EXPORT_SYMBOL_GPL(tty_ldisc_ref);
320 * tty_ldisc_deref - free a tty ldisc reference
321 * @ld: reference to free up
323 * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May
324 * be called in IRQ context.
326 * Locking: takes tty_ldisc_lock
329 void tty_ldisc_deref(struct tty_ldisc *ld)
333 if (WARN_ON_ONCE(!ld))
336 raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
338 * WARNs if one-too-many reader references were released
339 * - the last reference must be released with tty_ldisc_put
341 WARN_ON(atomic_dec_and_test(&ld->users));
342 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
344 if (waitqueue_active(&ld->wq_idle))
345 wake_up(&ld->wq_idle);
347 EXPORT_SYMBOL_GPL(tty_ldisc_deref);
350 * tty_ldisc_put - release the ldisc
352 * Complement of tty_ldisc_get().
354 static inline void tty_ldisc_put(struct tty_ldisc *ld)
358 if (WARN_ON_ONCE(!ld))
361 raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
363 /* unreleased reader reference(s) will cause this WARN */
364 WARN_ON(!atomic_dec_and_test(&ld->users));
367 module_put(ld->ops->owner);
369 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
373 * tty_ldisc_enable - allow ldisc use
374 * @tty: terminal to activate ldisc on
376 * Set the TTY_LDISC flag when the line discipline can be called
377 * again. Do necessary wakeups for existing sleepers. Clear the LDISC
378 * changing flag to indicate any ldisc change is now over.
380 * Note: nobody should set the TTY_LDISC bit except via this function.
381 * Clearing directly is allowed.
384 static void tty_ldisc_enable(struct tty_struct *tty)
386 clear_bit(TTY_LDISC_HALTED, &tty->flags);
387 set_bit(TTY_LDISC, &tty->flags);
388 clear_bit(TTY_LDISC_CHANGING, &tty->flags);
389 wake_up(&tty_ldisc_wait);
393 * tty_ldisc_flush - flush line discipline queue
396 * Flush the line discipline queue (if any) for this tty. If there
397 * is no line discipline active this is a no-op.
400 void tty_ldisc_flush(struct tty_struct *tty)
402 struct tty_ldisc *ld = tty_ldisc_ref(tty);
404 if (ld->ops->flush_buffer)
405 ld->ops->flush_buffer(tty);
408 tty_buffer_flush(tty);
410 EXPORT_SYMBOL_GPL(tty_ldisc_flush);
413 * tty_set_termios_ldisc - set ldisc field
414 * @tty: tty structure
415 * @num: line discipline number
417 * This is probably overkill for real world processors but
418 * they are not on hot paths so a little discipline won't do
421 * Locking: takes termios_mutex
424 static void tty_set_termios_ldisc(struct tty_struct *tty, int num)
426 mutex_lock(&tty->termios_mutex);
427 tty->termios.c_line = num;
428 mutex_unlock(&tty->termios_mutex);
432 * tty_ldisc_open - open a line discipline
433 * @tty: tty we are opening the ldisc on
434 * @ld: discipline to open
436 * A helper opening method. Also a convenient debugging and check
439 * Locking: always called with BTM already held.
442 static int tty_ldisc_open(struct tty_struct *tty, struct tty_ldisc *ld)
444 WARN_ON(test_and_set_bit(TTY_LDISC_OPEN, &tty->flags));
447 /* BTM here locks versus a hangup event */
448 ret = ld->ops->open(tty);
450 clear_bit(TTY_LDISC_OPEN, &tty->flags);
457 * tty_ldisc_close - close a line discipline
458 * @tty: tty we are opening the ldisc on
459 * @ld: discipline to close
461 * A helper close method. Also a convenient debugging and check
465 static void tty_ldisc_close(struct tty_struct *tty, struct tty_ldisc *ld)
467 WARN_ON(!test_bit(TTY_LDISC_OPEN, &tty->flags));
468 clear_bit(TTY_LDISC_OPEN, &tty->flags);
474 * tty_ldisc_restore - helper for tty ldisc change
475 * @tty: tty to recover
476 * @old: previous ldisc
478 * Restore the previous line discipline or N_TTY when a line discipline
479 * change fails due to an open error
482 static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old)
485 struct tty_ldisc *new_ldisc;
488 /* There is an outstanding reference here so this is safe */
489 old = tty_ldisc_get(old->ops->num);
490 WARN_ON(IS_ERR(old));
491 tty_ldisc_assign(tty, old);
492 tty_set_termios_ldisc(tty, old->ops->num);
493 if (tty_ldisc_open(tty, old) < 0) {
495 /* This driver is always present */
496 new_ldisc = tty_ldisc_get(N_TTY);
497 if (IS_ERR(new_ldisc))
499 tty_ldisc_assign(tty, new_ldisc);
500 tty_set_termios_ldisc(tty, N_TTY);
501 r = tty_ldisc_open(tty, new_ldisc);
503 panic("Couldn't open N_TTY ldisc for "
505 tty_name(tty, buf), r);
510 * tty_ldisc_wait_idle - wait for the ldisc to become idle
511 * @tty: tty to wait for
512 * @timeout: for how long to wait at most
514 * Wait for the line discipline to become idle. The discipline must
515 * have been halted for this to guarantee it remains idle.
517 static int tty_ldisc_wait_idle(struct tty_struct *tty, long timeout)
520 ret = wait_event_timeout(tty->ldisc->wq_idle,
521 atomic_read(&tty->ldisc->users) == 1, timeout);
522 return ret > 0 ? 0 : -EBUSY;
526 * tty_ldisc_halt - shut down the line discipline
528 * @o_tty: paired pty device (can be NULL)
529 * @timeout: # of jiffies to wait for ldisc refs to be released
531 * Shut down the line discipline and work queue for this tty device and
532 * its paired pty (if exists). Clearing the TTY_LDISC flag ensures
533 * no further references can be obtained, while waiting for existing
534 * references to be released ensures no more data is fed to the ldisc.
536 * You need to do a 'flush_scheduled_work()' (outside the ldisc_mutex)
537 * in order to make sure any currently executing ldisc work is also
541 static int tty_ldisc_halt(struct tty_struct *tty, struct tty_struct *o_tty,
546 clear_bit(TTY_LDISC, &tty->flags);
548 clear_bit(TTY_LDISC, &o_tty->flags);
550 retval = tty_ldisc_wait_idle(tty, timeout);
551 if (!retval && o_tty)
552 retval = tty_ldisc_wait_idle(o_tty, timeout);
556 set_bit(TTY_LDISC_HALTED, &tty->flags);
558 set_bit(TTY_LDISC_HALTED, &o_tty->flags);
564 * tty_ldisc_hangup_halt - halt the line discipline for hangup
565 * @tty: tty being hung up
567 * Shut down the line discipline and work queue for the tty device
568 * being hungup. Clear the TTY_LDISC flag to ensure no further
569 * references can be obtained and wait for remaining references to be
570 * released to ensure no more data is fed to this ldisc.
571 * Caller must hold legacy and ->ldisc_mutex.
573 * NB: tty_set_ldisc() is prevented from changing the ldisc concurrently
574 * with this function by checking the TTY_HUPPING flag.
576 static bool tty_ldisc_hangup_halt(struct tty_struct *tty)
578 char cur_n[TASK_COMM_LEN], tty_n[64];
579 long timeout = 3 * HZ;
581 clear_bit(TTY_LDISC, &tty->flags);
583 if (tty->ldisc) { /* Not yet closed */
586 while (tty_ldisc_wait_idle(tty, timeout) == -EBUSY) {
587 timeout = MAX_SCHEDULE_TIMEOUT;
588 printk_ratelimited(KERN_WARNING
589 "%s: waiting (%s) for %s took too long, but we keep waiting...\n",
590 __func__, get_task_comm(cur_n, current),
591 tty_name(tty, tty_n));
594 set_bit(TTY_LDISC_HALTED, &tty->flags);
596 /* must reacquire both locks and preserve lock order */
597 mutex_unlock(&tty->ldisc_mutex);
599 mutex_lock(&tty->ldisc_mutex);
605 * tty_set_ldisc - set line discipline
606 * @tty: the terminal to set
607 * @ldisc: the line discipline
609 * Set the discipline of a tty line. Must be called from a process
610 * context. The ldisc change logic has to protect itself against any
611 * overlapping ldisc change (including on the other end of pty pairs),
612 * the close of one side of a tty/pty pair, and eventually hangup.
614 * Locking: takes tty_ldisc_lock, termios_mutex
617 int tty_set_ldisc(struct tty_struct *tty, int ldisc)
620 struct tty_ldisc *o_ldisc, *new_ldisc;
621 struct tty_struct *o_tty;
623 new_ldisc = tty_ldisc_get(ldisc);
624 if (IS_ERR(new_ldisc))
625 return PTR_ERR(new_ldisc);
629 * We need to look at the tty locking here for pty/tty pairs
630 * when both sides try to change in parallel.
633 o_tty = tty->link; /* o_tty is the pty side or NULL */
637 * Check the no-op case
640 if (tty->ldisc->ops->num == ldisc) {
642 tty_ldisc_put(new_ldisc);
648 * Problem: What do we do if this blocks ?
649 * We could deadlock here
652 tty_wait_until_sent(tty, 0);
655 mutex_lock(&tty->ldisc_mutex);
658 * We could be midstream of another ldisc change which has
659 * dropped the lock during processing. If so we need to wait.
662 while (test_bit(TTY_LDISC_CHANGING, &tty->flags)) {
663 mutex_unlock(&tty->ldisc_mutex);
665 wait_event(tty_ldisc_wait,
666 test_bit(TTY_LDISC_CHANGING, &tty->flags) == 0);
668 mutex_lock(&tty->ldisc_mutex);
671 set_bit(TTY_LDISC_CHANGING, &tty->flags);
674 * No more input please, we are switching. The new ldisc
675 * will update this value in the ldisc open function
678 tty->receive_room = 0;
680 o_ldisc = tty->ldisc;
684 * Make sure we don't change while someone holds a
685 * reference to the line discipline. The TTY_LDISC bit
686 * prevents anyone taking a reference once it is clear.
687 * We need the lock to avoid racing reference takers.
689 * We must clear the TTY_LDISC bit here to avoid a livelock
690 * with a userspace app continually trying to use the tty in
691 * parallel to the change and re-referencing the tty.
694 retval = tty_ldisc_halt(tty, o_tty, 5 * HZ);
697 * Wait for hangup to complete, if pending.
698 * We must drop the mutex here in case a hangup is also in process.
701 mutex_unlock(&tty->ldisc_mutex);
703 flush_work(&tty->hangup_work);
706 mutex_lock(&tty->ldisc_mutex);
708 /* handle wait idle failure locked */
710 tty_ldisc_put(new_ldisc);
714 if (test_bit(TTY_HUPPING, &tty->flags)) {
715 /* We were raced by the hangup method. It will have stomped
716 the ldisc data and closed the ldisc down */
717 clear_bit(TTY_LDISC_CHANGING, &tty->flags);
718 mutex_unlock(&tty->ldisc_mutex);
719 tty_ldisc_put(new_ldisc);
724 /* Shutdown the current discipline. */
725 tty_ldisc_close(tty, o_ldisc);
727 /* Now set up the new line discipline. */
728 tty_ldisc_assign(tty, new_ldisc);
729 tty_set_termios_ldisc(tty, ldisc);
731 retval = tty_ldisc_open(tty, new_ldisc);
733 /* Back to the old one or N_TTY if we can't */
734 tty_ldisc_put(new_ldisc);
735 tty_ldisc_restore(tty, o_ldisc);
738 /* At this point we hold a reference to the new ldisc and a
739 a reference to the old ldisc. If we ended up flipping back
740 to the existing ldisc we have two references to it */
742 if (tty->ldisc->ops->num != o_ldisc->ops->num && tty->ops->set_ldisc)
743 tty->ops->set_ldisc(tty);
745 tty_ldisc_put(o_ldisc);
749 * Allow ldisc referencing to occur again
752 tty_ldisc_enable(tty);
754 tty_ldisc_enable(o_tty);
756 /* Restart the work queue in case no characters kick it off. Safe if
758 schedule_work(&tty->port->buf.work);
760 schedule_work(&o_tty->port->buf.work);
762 mutex_unlock(&tty->ldisc_mutex);
768 * tty_reset_termios - reset terminal state
771 * Restore a terminal to the driver default state.
774 static void tty_reset_termios(struct tty_struct *tty)
776 mutex_lock(&tty->termios_mutex);
777 tty->termios = tty->driver->init_termios;
778 tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios);
779 tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios);
780 mutex_unlock(&tty->termios_mutex);
785 * tty_ldisc_reinit - reinitialise the tty ldisc
786 * @tty: tty to reinit
787 * @ldisc: line discipline to reinitialize
789 * Switch the tty to a line discipline and leave the ldisc
793 static int tty_ldisc_reinit(struct tty_struct *tty, int ldisc)
795 struct tty_ldisc *ld = tty_ldisc_get(ldisc);
800 tty_ldisc_close(tty, tty->ldisc);
801 tty_ldisc_put(tty->ldisc);
804 * Switch the line discipline back
806 tty_ldisc_assign(tty, ld);
807 tty_set_termios_ldisc(tty, ldisc);
813 * tty_ldisc_hangup - hangup ldisc reset
814 * @tty: tty being hung up
816 * Some tty devices reset their termios when they receive a hangup
817 * event. In that situation we must also switch back to N_TTY properly
818 * before we reset the termios data.
820 * Locking: We can take the ldisc mutex as the rest of the code is
821 * careful to allow for this.
823 * In the pty pair case this occurs in the close() path of the
824 * tty itself so we must be careful about locking rules.
827 void tty_ldisc_hangup(struct tty_struct *tty)
829 struct tty_ldisc *ld;
830 int reset = tty->driver->flags & TTY_DRIVER_RESET_TERMIOS;
833 tty_ldisc_debug(tty, "closing ldisc: %p\n", tty->ldisc);
836 * FIXME! What are the locking issues here? This may me overdoing
837 * things... This question is especially important now that we've
838 * removed the irqlock.
840 ld = tty_ldisc_ref(tty);
842 /* We may have no line discipline at this point */
843 if (ld->ops->flush_buffer)
844 ld->ops->flush_buffer(tty);
845 tty_driver_flush_buffer(tty);
846 if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
847 ld->ops->write_wakeup)
848 ld->ops->write_wakeup(tty);
850 ld->ops->hangup(tty);
854 * FIXME: Once we trust the LDISC code better we can wait here for
855 * ldisc completion and fix the driver call race
857 wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
858 wake_up_interruptible_poll(&tty->read_wait, POLLIN);
860 * Shutdown the current line discipline, and reset it to
863 * Avoid racing set_ldisc or tty_ldisc_release
865 mutex_lock(&tty->ldisc_mutex);
867 if (tty_ldisc_hangup_halt(tty)) {
869 /* At this point we have a halted ldisc; we want to close it and
870 reopen a new ldisc. We could defer the reopen to the next
871 open but it means auditing a lot of other paths so this is
875 if (!tty_ldisc_reinit(tty, tty->termios.c_line))
876 err = tty_ldisc_open(tty, tty->ldisc);
880 /* If the re-open fails or we reset then go to N_TTY. The
881 N_TTY open cannot fail */
883 BUG_ON(tty_ldisc_reinit(tty, N_TTY));
884 WARN_ON(tty_ldisc_open(tty, tty->ldisc));
886 tty_ldisc_enable(tty);
888 mutex_unlock(&tty->ldisc_mutex);
890 tty_reset_termios(tty);
892 tty_ldisc_debug(tty, "re-opened ldisc: %p\n", tty->ldisc);
896 * tty_ldisc_setup - open line discipline
897 * @tty: tty being shut down
898 * @o_tty: pair tty for pty/tty pairs
900 * Called during the initial open of a tty/pty pair in order to set up the
901 * line disciplines and bind them to the tty. This has no locking issues
902 * as the device isn't yet active.
905 int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty)
907 struct tty_ldisc *ld = tty->ldisc;
910 retval = tty_ldisc_open(tty, ld);
915 retval = tty_ldisc_open(o_tty, o_tty->ldisc);
917 tty_ldisc_close(tty, ld);
920 tty_ldisc_enable(o_tty);
922 tty_ldisc_enable(tty);
926 static void tty_ldisc_kill(struct tty_struct *tty)
928 mutex_lock(&tty->ldisc_mutex);
930 * Now kill off the ldisc
932 tty_ldisc_close(tty, tty->ldisc);
933 tty_ldisc_put(tty->ldisc);
934 /* Force an oops if we mess this up */
937 /* Ensure the next open requests the N_TTY ldisc */
938 tty_set_termios_ldisc(tty, N_TTY);
939 mutex_unlock(&tty->ldisc_mutex);
943 * tty_ldisc_release - release line discipline
944 * @tty: tty being shut down
945 * @o_tty: pair tty for pty/tty pairs
947 * Called during the final close of a tty/pty pair in order to shut down
948 * the line discpline layer. On exit the ldisc assigned is N_TTY and the
949 * ldisc has not been opened.
952 void tty_ldisc_release(struct tty_struct *tty, struct tty_struct *o_tty)
955 * Shutdown this line discipline. As this is the final close,
956 * it does not race with the set_ldisc code path.
959 tty_ldisc_debug(tty, "closing ldisc: %p\n", tty->ldisc);
961 tty_ldisc_halt(tty, o_tty, MAX_SCHEDULE_TIMEOUT);
963 tty_lock_pair(tty, o_tty);
964 /* This will need doing differently if we need to lock */
967 tty_ldisc_kill(o_tty);
969 tty_unlock_pair(tty, o_tty);
970 /* And the memory resources remaining (buffers, termios) will be
971 disposed of when the kref hits zero */
973 tty_ldisc_debug(tty, "ldisc closed\n");
977 * tty_ldisc_init - ldisc setup for new tty
978 * @tty: tty being allocated
980 * Set up the line discipline objects for a newly allocated tty. Note that
981 * the tty structure is not completely set up when this call is made.
984 void tty_ldisc_init(struct tty_struct *tty)
986 struct tty_ldisc *ld = tty_ldisc_get(N_TTY);
988 panic("n_tty: init_tty");
989 tty_ldisc_assign(tty, ld);
993 * tty_ldisc_init - ldisc cleanup for new tty
994 * @tty: tty that was allocated recently
996 * The tty structure must not becompletely set up (tty_ldisc_setup) when
999 void tty_ldisc_deinit(struct tty_struct *tty)
1001 tty_ldisc_put(tty->ldisc);
1002 tty_ldisc_assign(tty, NULL);
1005 void tty_ldisc_begin(void)
1007 /* Setup the default TTY line discipline. */
1008 (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);