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>
24 * This guards the refcounted line discipline lists. The lock
25 * must be taken with irqs off because there are hangup path
26 * callers who will do ldisc lookups and cannot sleep.
29 static DEFINE_RAW_SPINLOCK(tty_ldisc_lock);
30 static DECLARE_WAIT_QUEUE_HEAD(tty_ldisc_wait);
31 /* Line disc dispatch table */
32 static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS];
34 static inline struct tty_ldisc *get_ldisc(struct tty_ldisc *ld)
37 atomic_inc(&ld->users);
41 static void put_ldisc(struct tty_ldisc *ld)
45 if (WARN_ON_ONCE(!ld))
49 * If this is the last user, free the ldisc, and
50 * release the ldisc ops.
52 * We really want an "atomic_dec_and_raw_lock_irqsave()",
53 * but we don't have it, so this does it by hand.
55 raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
56 if (atomic_dec_and_test(&ld->users)) {
57 struct tty_ldisc_ops *ldo = ld->ops;
60 module_put(ldo->owner);
61 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
66 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
68 if (waitqueue_active(&ld->wq_idle))
69 wake_up(&ld->wq_idle);
73 * tty_register_ldisc - install a line discipline
75 * @new_ldisc: pointer to the ldisc object
77 * Installs a new line discipline into the kernel. The discipline
78 * is set up as unreferenced and then made available to the kernel
79 * from this point onwards.
82 * takes tty_ldisc_lock to guard against ldisc races
85 int tty_register_ldisc(int disc, struct tty_ldisc_ops *new_ldisc)
90 if (disc < N_TTY || disc >= NR_LDISCS)
93 raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
94 tty_ldiscs[disc] = new_ldisc;
95 new_ldisc->num = disc;
96 new_ldisc->refcount = 0;
97 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
101 EXPORT_SYMBOL(tty_register_ldisc);
104 * tty_unregister_ldisc - unload a line discipline
105 * @disc: ldisc number
106 * @new_ldisc: pointer to the ldisc object
108 * Remove a line discipline from the kernel providing it is not
112 * takes tty_ldisc_lock to guard against ldisc races
115 int tty_unregister_ldisc(int disc)
120 if (disc < N_TTY || disc >= NR_LDISCS)
123 raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
124 if (tty_ldiscs[disc]->refcount)
127 tty_ldiscs[disc] = NULL;
128 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
132 EXPORT_SYMBOL(tty_unregister_ldisc);
134 static struct tty_ldisc_ops *get_ldops(int disc)
137 struct tty_ldisc_ops *ldops, *ret;
139 raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
140 ret = ERR_PTR(-EINVAL);
141 ldops = tty_ldiscs[disc];
143 ret = ERR_PTR(-EAGAIN);
144 if (try_module_get(ldops->owner)) {
149 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
153 static void put_ldops(struct tty_ldisc_ops *ldops)
157 raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
159 module_put(ldops->owner);
160 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
164 * tty_ldisc_get - take a reference to an ldisc
165 * @disc: ldisc number
167 * Takes a reference to a line discipline. Deals with refcounts and
168 * module locking counts. Returns NULL if the discipline is not available.
169 * Returns a pointer to the discipline and bumps the ref count if it is
173 * takes tty_ldisc_lock to guard against ldisc races
176 static struct tty_ldisc *tty_ldisc_get(int disc)
178 struct tty_ldisc *ld;
179 struct tty_ldisc_ops *ldops;
181 if (disc < N_TTY || disc >= NR_LDISCS)
182 return ERR_PTR(-EINVAL);
185 * Get the ldisc ops - we may need to request them to be loaded
186 * dynamically and try again.
188 ldops = get_ldops(disc);
190 request_module("tty-ldisc-%d", disc);
191 ldops = get_ldops(disc);
193 return ERR_CAST(ldops);
196 ld = kmalloc(sizeof(struct tty_ldisc), GFP_KERNEL);
199 return ERR_PTR(-ENOMEM);
203 atomic_set(&ld->users, 1);
204 init_waitqueue_head(&ld->wq_idle);
209 static void *tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos)
211 return (*pos < NR_LDISCS) ? pos : NULL;
214 static void *tty_ldiscs_seq_next(struct seq_file *m, void *v, loff_t *pos)
217 return (*pos < NR_LDISCS) ? pos : NULL;
220 static void tty_ldiscs_seq_stop(struct seq_file *m, void *v)
224 static int tty_ldiscs_seq_show(struct seq_file *m, void *v)
226 int i = *(loff_t *)v;
227 struct tty_ldisc_ops *ldops;
229 ldops = get_ldops(i);
232 seq_printf(m, "%-10s %2d\n", ldops->name ? ldops->name : "???", i);
237 static const struct seq_operations tty_ldiscs_seq_ops = {
238 .start = tty_ldiscs_seq_start,
239 .next = tty_ldiscs_seq_next,
240 .stop = tty_ldiscs_seq_stop,
241 .show = tty_ldiscs_seq_show,
244 static int proc_tty_ldiscs_open(struct inode *inode, struct file *file)
246 return seq_open(file, &tty_ldiscs_seq_ops);
249 const struct file_operations tty_ldiscs_proc_fops = {
250 .owner = THIS_MODULE,
251 .open = proc_tty_ldiscs_open,
254 .release = seq_release,
258 * tty_ldisc_assign - set ldisc on a tty
259 * @tty: tty to assign
260 * @ld: line discipline
262 * Install an instance of a line discipline into a tty structure. The
263 * ldisc must have a reference count above zero to ensure it remains.
264 * The tty instance refcount starts at zero.
267 * Caller must hold references
270 static void tty_ldisc_assign(struct tty_struct *tty, struct tty_ldisc *ld)
276 * tty_ldisc_try - internal helper
279 * Make a single attempt to grab and bump the refcount on
280 * the tty ldisc. Return 0 on failure or 1 on success. This is
281 * used to implement both the waiting and non waiting versions
284 * Locking: takes tty_ldisc_lock
287 static struct tty_ldisc *tty_ldisc_try(struct tty_struct *tty)
290 struct tty_ldisc *ld;
292 raw_spin_lock_irqsave(&tty_ldisc_lock, flags);
294 if (test_bit(TTY_LDISC, &tty->flags))
295 ld = get_ldisc(tty->ldisc);
296 raw_spin_unlock_irqrestore(&tty_ldisc_lock, flags);
301 * tty_ldisc_ref_wait - wait for the tty ldisc
304 * Dereference the line discipline for the terminal and take a
305 * reference to it. If the line discipline is in flux then
306 * wait patiently until it changes.
308 * Note: Must not be called from an IRQ/timer context. The caller
309 * must also be careful not to hold other locks that will deadlock
310 * against a discipline change, such as an existing ldisc reference
311 * (which we check for)
313 * Locking: call functions take tty_ldisc_lock
316 struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty)
318 struct tty_ldisc *ld;
320 /* wait_event is a macro */
321 wait_event(tty_ldisc_wait, (ld = tty_ldisc_try(tty)) != NULL);
324 EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait);
327 * tty_ldisc_ref - get the tty ldisc
330 * Dereference the line discipline for the terminal and take a
331 * reference to it. If the line discipline is in flux then
332 * return NULL. Can be called from IRQ and timer functions.
334 * Locking: called functions take tty_ldisc_lock
337 struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty)
339 return tty_ldisc_try(tty);
341 EXPORT_SYMBOL_GPL(tty_ldisc_ref);
344 * tty_ldisc_deref - free a tty ldisc reference
345 * @ld: reference to free up
347 * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May
348 * be called in IRQ context.
350 * Locking: takes tty_ldisc_lock
353 void tty_ldisc_deref(struct tty_ldisc *ld)
357 EXPORT_SYMBOL_GPL(tty_ldisc_deref);
359 static inline void tty_ldisc_put(struct tty_ldisc *ld)
365 * tty_ldisc_enable - allow ldisc use
366 * @tty: terminal to activate ldisc on
368 * Set the TTY_LDISC flag when the line discipline can be called
369 * again. Do necessary wakeups for existing sleepers. Clear the LDISC
370 * changing flag to indicate any ldisc change is now over.
372 * Note: nobody should set the TTY_LDISC bit except via this function.
373 * Clearing directly is allowed.
376 static void tty_ldisc_enable(struct tty_struct *tty)
378 clear_bit(TTY_LDISC_HALTED, &tty->flags);
379 set_bit(TTY_LDISC, &tty->flags);
380 clear_bit(TTY_LDISC_CHANGING, &tty->flags);
381 wake_up(&tty_ldisc_wait);
385 * tty_ldisc_flush - flush line discipline queue
388 * Flush the line discipline queue (if any) for this tty. If there
389 * is no line discipline active this is a no-op.
392 void tty_ldisc_flush(struct tty_struct *tty)
394 struct tty_ldisc *ld = tty_ldisc_ref(tty);
396 if (ld->ops->flush_buffer)
397 ld->ops->flush_buffer(tty);
400 tty_buffer_flush(tty);
402 EXPORT_SYMBOL_GPL(tty_ldisc_flush);
405 * tty_set_termios_ldisc - set ldisc field
406 * @tty: tty structure
407 * @num: line discipline number
409 * This is probably overkill for real world processors but
410 * they are not on hot paths so a little discipline won't do
413 * Locking: takes termios_mutex
416 static void tty_set_termios_ldisc(struct tty_struct *tty, int num)
418 mutex_lock(&tty->termios_mutex);
419 tty->termios.c_line = num;
420 mutex_unlock(&tty->termios_mutex);
424 * tty_ldisc_open - open a line discipline
425 * @tty: tty we are opening the ldisc on
426 * @ld: discipline to open
428 * A helper opening method. Also a convenient debugging and check
431 * Locking: always called with BTM already held.
434 static int tty_ldisc_open(struct tty_struct *tty, struct tty_ldisc *ld)
436 WARN_ON(test_and_set_bit(TTY_LDISC_OPEN, &tty->flags));
439 /* BTM here locks versus a hangup event */
440 ret = ld->ops->open(tty);
442 clear_bit(TTY_LDISC_OPEN, &tty->flags);
449 * tty_ldisc_close - close a line discipline
450 * @tty: tty we are opening the ldisc on
451 * @ld: discipline to close
453 * A helper close method. Also a convenient debugging and check
457 static void tty_ldisc_close(struct tty_struct *tty, struct tty_ldisc *ld)
459 WARN_ON(!test_bit(TTY_LDISC_OPEN, &tty->flags));
460 clear_bit(TTY_LDISC_OPEN, &tty->flags);
466 * tty_ldisc_restore - helper for tty ldisc change
467 * @tty: tty to recover
468 * @old: previous ldisc
470 * Restore the previous line discipline or N_TTY when a line discipline
471 * change fails due to an open error
474 static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old)
477 struct tty_ldisc *new_ldisc;
480 /* There is an outstanding reference here so this is safe */
481 old = tty_ldisc_get(old->ops->num);
482 WARN_ON(IS_ERR(old));
483 tty_ldisc_assign(tty, old);
484 tty_set_termios_ldisc(tty, old->ops->num);
485 if (tty_ldisc_open(tty, old) < 0) {
487 /* This driver is always present */
488 new_ldisc = tty_ldisc_get(N_TTY);
489 if (IS_ERR(new_ldisc))
491 tty_ldisc_assign(tty, new_ldisc);
492 tty_set_termios_ldisc(tty, N_TTY);
493 r = tty_ldisc_open(tty, new_ldisc);
495 panic("Couldn't open N_TTY ldisc for "
497 tty_name(tty, buf), r);
502 * tty_ldisc_flush_works - flush all works of a tty
503 * @tty: tty device to flush works for
505 * Sync flush all works belonging to @tty.
507 static void tty_ldisc_flush_works(struct tty_struct *tty)
509 flush_work(&tty->SAK_work);
510 flush_work(&tty->hangup_work);
514 * tty_ldisc_wait_idle - wait for the ldisc to become idle
515 * @tty: tty to wait for
516 * @timeout: for how long to wait at most
518 * Wait for the line discipline to become idle. The discipline must
519 * have been halted for this to guarantee it remains idle.
521 static int tty_ldisc_wait_idle(struct tty_struct *tty, long timeout)
524 ret = wait_event_timeout(tty->ldisc->wq_idle,
525 atomic_read(&tty->ldisc->users) == 1, timeout);
526 return ret > 0 ? 0 : -EBUSY;
530 * tty_ldisc_halt - shut down the line discipline
532 * @o_tty: paired pty device (can be NULL)
533 * @timeout: # of jiffies to wait for ldisc refs to be released
535 * Shut down the line discipline and work queue for this tty device and
536 * its paired pty (if exists). Clearing the TTY_LDISC flag ensures
537 * no further references can be obtained, while waiting for existing
538 * references to be released ensures no more data is fed to the ldisc.
540 * You need to do a 'flush_scheduled_work()' (outside the ldisc_mutex)
541 * in order to make sure any currently executing ldisc work is also
545 static int tty_ldisc_halt(struct tty_struct *tty, struct tty_struct *o_tty,
550 clear_bit(TTY_LDISC, &tty->flags);
552 clear_bit(TTY_LDISC, &o_tty->flags);
554 retval = tty_ldisc_wait_idle(tty, timeout);
555 if (!retval && o_tty)
556 retval = tty_ldisc_wait_idle(o_tty, timeout);
560 set_bit(TTY_LDISC_HALTED, &tty->flags);
562 set_bit(TTY_LDISC_HALTED, &o_tty->flags);
568 * tty_ldisc_hangup_halt - halt the line discipline for hangup
569 * @tty: tty being hung up
571 * Shut down the line discipline and work queue for the tty device
572 * being hungup. Clear the TTY_LDISC flag to ensure no further
573 * references can be obtained and wait for remaining references to be
574 * released to ensure no more data is fed to this ldisc.
575 * Caller must hold legacy and ->ldisc_mutex.
577 * NB: tty_set_ldisc() is prevented from changing the ldisc concurrently
578 * with this function by checking the TTY_HUPPING flag.
580 static bool tty_ldisc_hangup_halt(struct tty_struct *tty)
582 char cur_n[TASK_COMM_LEN], tty_n[64];
583 long timeout = 3 * HZ;
585 clear_bit(TTY_LDISC, &tty->flags);
587 if (tty->ldisc) { /* Not yet closed */
590 while (tty_ldisc_wait_idle(tty, timeout) == -EBUSY) {
591 timeout = MAX_SCHEDULE_TIMEOUT;
592 printk_ratelimited(KERN_WARNING
593 "%s: waiting (%s) for %s took too long, but we keep waiting...\n",
594 __func__, get_task_comm(cur_n, current),
595 tty_name(tty, tty_n));
598 set_bit(TTY_LDISC_HALTED, &tty->flags);
600 /* must reacquire both locks and preserve lock order */
601 mutex_unlock(&tty->ldisc_mutex);
603 mutex_lock(&tty->ldisc_mutex);
609 * tty_set_ldisc - set line discipline
610 * @tty: the terminal to set
611 * @ldisc: the line discipline
613 * Set the discipline of a tty line. Must be called from a process
614 * context. The ldisc change logic has to protect itself against any
615 * overlapping ldisc change (including on the other end of pty pairs),
616 * the close of one side of a tty/pty pair, and eventually hangup.
618 * Locking: takes tty_ldisc_lock, termios_mutex
621 int tty_set_ldisc(struct tty_struct *tty, int ldisc)
624 struct tty_ldisc *o_ldisc, *new_ldisc;
625 struct tty_struct *o_tty;
627 new_ldisc = tty_ldisc_get(ldisc);
628 if (IS_ERR(new_ldisc))
629 return PTR_ERR(new_ldisc);
633 * We need to look at the tty locking here for pty/tty pairs
634 * when both sides try to change in parallel.
637 o_tty = tty->link; /* o_tty is the pty side or NULL */
641 * Check the no-op case
644 if (tty->ldisc->ops->num == ldisc) {
646 tty_ldisc_put(new_ldisc);
652 * Problem: What do we do if this blocks ?
653 * We could deadlock here
656 tty_wait_until_sent(tty, 0);
659 mutex_lock(&tty->ldisc_mutex);
662 * We could be midstream of another ldisc change which has
663 * dropped the lock during processing. If so we need to wait.
666 while (test_bit(TTY_LDISC_CHANGING, &tty->flags)) {
667 mutex_unlock(&tty->ldisc_mutex);
669 wait_event(tty_ldisc_wait,
670 test_bit(TTY_LDISC_CHANGING, &tty->flags) == 0);
672 mutex_lock(&tty->ldisc_mutex);
675 set_bit(TTY_LDISC_CHANGING, &tty->flags);
678 * No more input please, we are switching. The new ldisc
679 * will update this value in the ldisc open function
682 tty->receive_room = 0;
684 o_ldisc = tty->ldisc;
688 * Make sure we don't change while someone holds a
689 * reference to the line discipline. The TTY_LDISC bit
690 * prevents anyone taking a reference once it is clear.
691 * We need the lock to avoid racing reference takers.
693 * We must clear the TTY_LDISC bit here to avoid a livelock
694 * with a userspace app continually trying to use the tty in
695 * parallel to the change and re-referencing the tty.
698 retval = tty_ldisc_halt(tty, o_tty, 5 * HZ);
701 * Wait for ->hangup_work and ->buf.work handlers to terminate.
702 * We must drop the mutex here in case a hangup is also in process.
705 mutex_unlock(&tty->ldisc_mutex);
707 tty_ldisc_flush_works(tty);
710 mutex_lock(&tty->ldisc_mutex);
712 /* handle wait idle failure locked */
714 tty_ldisc_put(new_ldisc);
718 if (test_bit(TTY_HUPPING, &tty->flags)) {
719 /* We were raced by the hangup method. It will have stomped
720 the ldisc data and closed the ldisc down */
721 clear_bit(TTY_LDISC_CHANGING, &tty->flags);
722 mutex_unlock(&tty->ldisc_mutex);
723 tty_ldisc_put(new_ldisc);
728 /* Shutdown the current discipline. */
729 tty_ldisc_close(tty, o_ldisc);
731 /* Now set up the new line discipline. */
732 tty_ldisc_assign(tty, new_ldisc);
733 tty_set_termios_ldisc(tty, ldisc);
735 retval = tty_ldisc_open(tty, new_ldisc);
737 /* Back to the old one or N_TTY if we can't */
738 tty_ldisc_put(new_ldisc);
739 tty_ldisc_restore(tty, o_ldisc);
742 /* At this point we hold a reference to the new ldisc and a
743 a reference to the old ldisc. If we ended up flipping back
744 to the existing ldisc we have two references to it */
746 if (tty->ldisc->ops->num != o_ldisc->ops->num && tty->ops->set_ldisc)
747 tty->ops->set_ldisc(tty);
749 tty_ldisc_put(o_ldisc);
753 * Allow ldisc referencing to occur again
756 tty_ldisc_enable(tty);
758 tty_ldisc_enable(o_tty);
760 /* Restart the work queue in case no characters kick it off. Safe if
762 schedule_work(&tty->port->buf.work);
764 schedule_work(&o_tty->port->buf.work);
766 mutex_unlock(&tty->ldisc_mutex);
772 * tty_reset_termios - reset terminal state
775 * Restore a terminal to the driver default state.
778 static void tty_reset_termios(struct tty_struct *tty)
780 mutex_lock(&tty->termios_mutex);
781 tty->termios = tty->driver->init_termios;
782 tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios);
783 tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios);
784 mutex_unlock(&tty->termios_mutex);
789 * tty_ldisc_reinit - reinitialise the tty ldisc
790 * @tty: tty to reinit
791 * @ldisc: line discipline to reinitialize
793 * Switch the tty to a line discipline and leave the ldisc
797 static int tty_ldisc_reinit(struct tty_struct *tty, int ldisc)
799 struct tty_ldisc *ld = tty_ldisc_get(ldisc);
804 tty_ldisc_close(tty, tty->ldisc);
805 tty_ldisc_put(tty->ldisc);
808 * Switch the line discipline back
810 tty_ldisc_assign(tty, ld);
811 tty_set_termios_ldisc(tty, ldisc);
817 * tty_ldisc_hangup - hangup ldisc reset
818 * @tty: tty being hung up
820 * Some tty devices reset their termios when they receive a hangup
821 * event. In that situation we must also switch back to N_TTY properly
822 * before we reset the termios data.
824 * Locking: We can take the ldisc mutex as the rest of the code is
825 * careful to allow for this.
827 * In the pty pair case this occurs in the close() path of the
828 * tty itself so we must be careful about locking rules.
831 void tty_ldisc_hangup(struct tty_struct *tty)
833 struct tty_ldisc *ld;
834 int reset = tty->driver->flags & TTY_DRIVER_RESET_TERMIOS;
838 * FIXME! What are the locking issues here? This may me overdoing
839 * things... This question is especially important now that we've
840 * removed the irqlock.
842 ld = tty_ldisc_ref(tty);
844 /* We may have no line discipline at this point */
845 if (ld->ops->flush_buffer)
846 ld->ops->flush_buffer(tty);
847 tty_driver_flush_buffer(tty);
848 if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
849 ld->ops->write_wakeup)
850 ld->ops->write_wakeup(tty);
852 ld->ops->hangup(tty);
856 * FIXME: Once we trust the LDISC code better we can wait here for
857 * ldisc completion and fix the driver call race
859 wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
860 wake_up_interruptible_poll(&tty->read_wait, POLLIN);
862 * Shutdown the current line discipline, and reset it to
865 * Avoid racing set_ldisc or tty_ldisc_release
867 mutex_lock(&tty->ldisc_mutex);
869 /* At this point we have a closed ldisc and we want to
870 reopen it. We could defer this to the next open but
871 it means auditing a lot of other paths so this is
873 if (tty_ldisc_hangup_halt(tty)) {
876 if (!tty_ldisc_reinit(tty, tty->termios.c_line))
877 err = tty_ldisc_open(tty, tty->ldisc);
881 /* If the re-open fails or we reset then go to N_TTY. The
882 N_TTY open cannot fail */
884 BUG_ON(tty_ldisc_reinit(tty, N_TTY));
885 WARN_ON(tty_ldisc_open(tty, tty->ldisc));
887 tty_ldisc_enable(tty);
889 mutex_unlock(&tty->ldisc_mutex);
891 tty_reset_termios(tty);
895 * tty_ldisc_setup - open line discipline
896 * @tty: tty being shut down
897 * @o_tty: pair tty for pty/tty pairs
899 * Called during the initial open of a tty/pty pair in order to set up the
900 * line disciplines and bind them to the tty. This has no locking issues
901 * as the device isn't yet active.
904 int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty)
906 struct tty_ldisc *ld = tty->ldisc;
909 retval = tty_ldisc_open(tty, ld);
914 retval = tty_ldisc_open(o_tty, o_tty->ldisc);
916 tty_ldisc_close(tty, ld);
919 tty_ldisc_enable(o_tty);
921 tty_ldisc_enable(tty);
925 static void tty_ldisc_kill(struct tty_struct *tty)
927 mutex_lock(&tty->ldisc_mutex);
929 * Now kill off the ldisc
931 tty_ldisc_close(tty, tty->ldisc);
932 tty_ldisc_put(tty->ldisc);
933 /* Force an oops if we mess this up */
936 /* Ensure the next open requests the N_TTY ldisc */
937 tty_set_termios_ldisc(tty, N_TTY);
938 mutex_unlock(&tty->ldisc_mutex);
942 * tty_ldisc_release - release line discipline
943 * @tty: tty being shut down
944 * @o_tty: pair tty for pty/tty pairs
946 * Called during the final close of a tty/pty pair in order to shut down
947 * the line discpline layer. On exit the ldisc assigned is N_TTY and the
948 * ldisc has not been opened.
951 void tty_ldisc_release(struct tty_struct *tty, struct tty_struct *o_tty)
954 * Prevent flush_to_ldisc() from rescheduling the work for later. Then
955 * kill any delayed work. As this is the final close it does not
956 * race with the set_ldisc code path.
959 tty_ldisc_halt(tty, o_tty, MAX_SCHEDULE_TIMEOUT);
960 tty_ldisc_flush_works(tty);
962 tty_ldisc_flush_works(o_tty);
964 tty_lock_pair(tty, o_tty);
965 /* This will need doing differently if we need to lock */
968 tty_ldisc_kill(o_tty);
970 tty_unlock_pair(tty, o_tty);
971 /* And the memory resources remaining (buffers, termios) will be
972 disposed of when the kref hits zero */
976 * tty_ldisc_init - ldisc setup for new tty
977 * @tty: tty being allocated
979 * Set up the line discipline objects for a newly allocated tty. Note that
980 * the tty structure is not completely set up when this call is made.
983 void tty_ldisc_init(struct tty_struct *tty)
985 struct tty_ldisc *ld = tty_ldisc_get(N_TTY);
987 panic("n_tty: init_tty");
988 tty_ldisc_assign(tty, ld);
992 * tty_ldisc_init - ldisc cleanup for new tty
993 * @tty: tty that was allocated recently
995 * The tty structure must not becompletely set up (tty_ldisc_setup) when
998 void tty_ldisc_deinit(struct tty_struct *tty)
1000 put_ldisc(tty->ldisc);
1001 tty_ldisc_assign(tty, NULL);
1004 void tty_ldisc_begin(void)
1006 /* Setup the default TTY line discipline. */
1007 (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);