tty-ldisc: make /proc/tty/ldiscs use ldisc_ops instead of ldiscs
[linux-2.6/mini2440.git] / drivers / char / tty_ldisc.c
blobcbfacc0bbea520764bdc176dab20f47e0f90a3e2
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>
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>
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>
30 #include <linux/uaccess.h>
31 #include <asm/system.h>
33 #include <linux/kbd_kern.h>
34 #include <linux/vt_kern.h>
35 #include <linux/selection.h>
37 #include <linux/kmod.h>
38 #include <linux/nsproxy.h>
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.
46 static DEFINE_SPINLOCK(tty_ldisc_lock);
47 static DECLARE_WAIT_QUEUE_HEAD(tty_ldisc_wait);
48 /* Line disc dispatch table */
49 static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS];
51 static inline struct tty_ldisc *get_ldisc(struct tty_ldisc *ld)
53 if (ld)
54 atomic_inc(&ld->users);
55 return ld;
58 static void put_ldisc(struct tty_ldisc *ld)
60 unsigned long flags;
62 if (WARN_ON_ONCE(!ld))
63 return;
66 * If this is the last user, free the ldisc, and
67 * release the ldisc ops.
69 * We really want an "atomic_dec_and_lock_irqsave()",
70 * but we don't have it, so this does it by hand.
72 local_irq_save(flags);
73 if (atomic_dec_and_lock(&ld->users, &tty_ldisc_lock)) {
74 struct tty_ldisc_ops *ldo = ld->ops;
76 ldo->refcount--;
77 module_put(ldo->owner);
78 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
80 kfree(ld);
81 return;
83 local_irq_restore(flags);
86 /**
87 * tty_register_ldisc - install a line discipline
88 * @disc: ldisc number
89 * @new_ldisc: pointer to the ldisc object
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.
95 * Locking:
96 * takes tty_ldisc_lock to guard against ldisc races
99 int tty_register_ldisc(int disc, struct tty_ldisc_ops *new_ldisc)
101 unsigned long flags;
102 int ret = 0;
104 if (disc < N_TTY || disc >= NR_LDISCS)
105 return -EINVAL;
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);
113 return ret;
115 EXPORT_SYMBOL(tty_register_ldisc);
118 * tty_unregister_ldisc - unload a line discipline
119 * @disc: ldisc number
120 * @new_ldisc: pointer to the ldisc object
122 * Remove a line discipline from the kernel providing it is not
123 * currently in use.
125 * Locking:
126 * takes tty_ldisc_lock to guard against ldisc races
129 int tty_unregister_ldisc(int disc)
131 unsigned long flags;
132 int ret = 0;
134 if (disc < N_TTY || disc >= NR_LDISCS)
135 return -EINVAL;
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);
144 return ret;
146 EXPORT_SYMBOL(tty_unregister_ldisc);
148 static struct tty_ldisc_ops *get_ldops(int disc)
150 unsigned long flags;
151 struct tty_ldisc_ops *ldops, *ret;
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;
163 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
164 return ret;
167 static void put_ldops(struct tty_ldisc_ops *ldops)
169 unsigned long flags;
171 spin_lock_irqsave(&tty_ldisc_lock, flags);
172 ldops->refcount--;
173 module_put(ldops->owner);
174 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
178 * tty_ldisc_try_get - try and reference an ldisc
179 * @disc: ldisc number
181 * Attempt to open and lock a line discipline into place. Return
182 * the line discipline refcounted or an error.
185 static struct tty_ldisc *tty_ldisc_try_get(int disc)
187 struct tty_ldisc *ld;
188 struct tty_ldisc_ops *ldops;
190 ld = kmalloc(sizeof(struct tty_ldisc), GFP_KERNEL);
191 if (ld == NULL)
192 return ERR_PTR(-ENOMEM);
194 ldops = get_ldops(disc);
195 if (IS_ERR(ldops)) {
196 kfree(ld);
197 return ERR_CAST(ldops);
200 ld->ops = ldops;
201 atomic_set(&ld->users, 1);
202 return ld;
206 * tty_ldisc_get - take a reference to an ldisc
207 * @disc: ldisc number
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
214 * Locking:
215 * takes tty_ldisc_lock to guard against ldisc races
218 static struct tty_ldisc *tty_ldisc_get(int disc)
220 struct tty_ldisc *ld;
222 if (disc < N_TTY || disc >= NR_LDISCS)
223 return ERR_PTR(-EINVAL);
224 ld = tty_ldisc_try_get(disc);
225 if (IS_ERR(ld)) {
226 request_module("tty-ldisc-%d", disc);
227 ld = tty_ldisc_try_get(disc);
229 return ld;
232 static void *tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos)
234 return (*pos < NR_LDISCS) ? pos : NULL;
237 static void *tty_ldiscs_seq_next(struct seq_file *m, void *v, loff_t *pos)
239 (*pos)++;
240 return (*pos < NR_LDISCS) ? pos : NULL;
243 static void tty_ldiscs_seq_stop(struct seq_file *m, void *v)
247 static int tty_ldiscs_seq_show(struct seq_file *m, void *v)
249 int i = *(loff_t *)v;
250 struct tty_ldisc_ops *ldops;
252 ldops = get_ldops(i);
253 if (IS_ERR(ldops))
254 return 0;
255 seq_printf(m, "%-10s %2d\n", ldops->name ? ldops->name : "???", i);
256 put_ldops(ldops);
257 return 0;
260 static 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,
267 static int proc_tty_ldiscs_open(struct inode *inode, struct file *file)
269 return seq_open(file, &tty_ldiscs_seq_ops);
272 const 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,
281 * tty_ldisc_assign - set ldisc on a tty
282 * @tty: tty to assign
283 * @ld: line discipline
285 * Install an instance of a line discipline into a tty structure. The
286 * ldisc must have a reference count above zero to ensure it remains.
287 * The tty instance refcount starts at zero.
289 * Locking:
290 * Caller must hold references
293 static void tty_ldisc_assign(struct tty_struct *tty, struct tty_ldisc *ld)
295 tty->ldisc = ld;
299 * tty_ldisc_try - internal helper
300 * @tty: the tty
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
307 * Locking: takes tty_ldisc_lock
310 static struct tty_ldisc *tty_ldisc_try(struct tty_struct *tty)
312 unsigned long flags;
313 struct tty_ldisc *ld;
315 spin_lock_irqsave(&tty_ldisc_lock, flags);
316 ld = NULL;
317 if (test_bit(TTY_LDISC, &tty->flags))
318 ld = get_ldisc(tty->ldisc);
319 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
320 return ld;
324 * tty_ldisc_ref_wait - wait for the tty ldisc
325 * @tty: tty device
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.
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)
336 * Locking: call functions take tty_ldisc_lock
339 struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty)
341 struct tty_ldisc *ld;
343 /* wait_event is a macro */
344 wait_event(tty_ldisc_wait, (ld = tty_ldisc_try(tty)) != NULL);
345 return ld;
347 EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait);
350 * tty_ldisc_ref - get the tty ldisc
351 * @tty: tty device
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.
357 * Locking: called functions take tty_ldisc_lock
360 struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty)
362 return tty_ldisc_try(tty);
364 EXPORT_SYMBOL_GPL(tty_ldisc_ref);
367 * tty_ldisc_deref - free a tty ldisc reference
368 * @ld: reference to free up
370 * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May
371 * be called in IRQ context.
373 * Locking: takes tty_ldisc_lock
376 void tty_ldisc_deref(struct tty_ldisc *ld)
378 put_ldisc(ld);
380 EXPORT_SYMBOL_GPL(tty_ldisc_deref);
382 static inline void tty_ldisc_put(struct tty_ldisc *ld)
384 put_ldisc(ld);
388 * tty_ldisc_enable - allow ldisc use
389 * @tty: terminal to activate ldisc on
391 * Set the TTY_LDISC flag when the line discipline can be called
392 * again. Do necessary wakeups for existing sleepers. Clear the LDISC
393 * changing flag to indicate any ldisc change is now over.
395 * Note: nobody should set the TTY_LDISC bit except via this function.
396 * Clearing directly is allowed.
399 void tty_ldisc_enable(struct tty_struct *tty)
401 set_bit(TTY_LDISC, &tty->flags);
402 clear_bit(TTY_LDISC_CHANGING, &tty->flags);
403 wake_up(&tty_ldisc_wait);
407 * tty_ldisc_flush - flush line discipline queue
408 * @tty: tty
410 * Flush the line discipline queue (if any) for this tty. If there
411 * is no line discipline active this is a no-op.
414 void tty_ldisc_flush(struct tty_struct *tty)
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);
422 tty_buffer_flush(tty);
424 EXPORT_SYMBOL_GPL(tty_ldisc_flush);
427 * tty_set_termios_ldisc - set ldisc field
428 * @tty: tty structure
429 * @num: line discipline number
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.
435 * Locking: takes termios_mutex
438 static void tty_set_termios_ldisc(struct tty_struct *tty, int num)
440 mutex_lock(&tty->termios_mutex);
441 tty->termios->c_line = num;
442 mutex_unlock(&tty->termios_mutex);
446 * tty_ldisc_open - open a line discipline
447 * @tty: tty we are opening the ldisc on
448 * @ld: discipline to open
450 * A helper opening method. Also a convenient debugging and check
451 * point.
454 static int tty_ldisc_open(struct tty_struct *tty, struct tty_ldisc *ld)
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;
463 * tty_ldisc_close - close a line discipline
464 * @tty: tty we are opening the ldisc on
465 * @ld: discipline to close
467 * A helper close method. Also a convenient debugging and check
468 * point.
471 static void tty_ldisc_close(struct tty_struct *tty, struct tty_ldisc *ld)
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);
480 * tty_ldisc_restore - helper for tty ldisc change
481 * @tty: tty to recover
482 * @old: previous ldisc
484 * Restore the previous line discipline or N_TTY when a line discipline
485 * change fails due to an open error
488 static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old)
490 char buf[64];
491 struct tty_ldisc *new_ldisc;
492 int r;
494 /* There is an outstanding reference here so this is safe */
495 old = tty_ldisc_get(old->ops->num);
496 WARN_ON(IS_ERR(old));
497 tty_ldisc_assign(tty, old);
498 tty_set_termios_ldisc(tty, old->ops->num);
499 if (tty_ldisc_open(tty, old) < 0) {
500 tty_ldisc_put(old);
501 /* This driver is always present */
502 new_ldisc = tty_ldisc_get(N_TTY);
503 if (IS_ERR(new_ldisc))
504 panic("n_tty: get");
505 tty_ldisc_assign(tty, new_ldisc);
506 tty_set_termios_ldisc(tty, N_TTY);
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);
516 * tty_ldisc_halt - shut down the line discipline
517 * @tty: tty device
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.
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.
529 static int tty_ldisc_halt(struct tty_struct *tty)
531 clear_bit(TTY_LDISC, &tty->flags);
532 return cancel_delayed_work(&tty->buf.work);
536 * tty_set_ldisc - set line discipline
537 * @tty: the terminal to set
538 * @ldisc: the line discipline
540 * Set the discipline of a tty line. Must be called from a process
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.
545 * Locking: takes tty_ldisc_lock, termios_mutex
548 int tty_set_ldisc(struct tty_struct *tty, int ldisc)
550 int retval;
551 struct tty_ldisc *o_ldisc, *new_ldisc;
552 int work, o_work = 0;
553 struct tty_struct *o_tty;
555 new_ldisc = tty_ldisc_get(ldisc);
556 if (IS_ERR(new_ldisc))
557 return PTR_ERR(new_ldisc);
560 * We need to look at the tty locking here for pty/tty pairs
561 * when both sides try to change in parallel.
564 o_tty = tty->link; /* o_tty is the pty side or NULL */
568 * Check the no-op case
571 if (tty->ldisc->ops->num == ldisc) {
572 tty_ldisc_put(new_ldisc);
573 return 0;
577 * Problem: What do we do if this blocks ?
578 * We could deadlock here
581 tty_wait_until_sent(tty, 0);
583 mutex_lock(&tty->ldisc_mutex);
586 * We could be midstream of another ldisc change which has
587 * dropped the lock during processing. If so we need to wait.
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);
596 set_bit(TTY_LDISC_CHANGING, &tty->flags);
599 * No more input please, we are switching. The new ldisc
600 * will update this value in the ldisc open function
603 tty->receive_room = 0;
605 o_ldisc = tty->ldisc;
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.
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.
617 work = tty_ldisc_halt(tty);
618 if (o_tty)
619 o_work = tty_ldisc_halt(o_tty);
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.
626 mutex_unlock(&tty->ldisc_mutex);
628 flush_scheduled_work();
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;
640 /* Shutdown the current discipline. */
641 tty_ldisc_close(tty, o_ldisc);
643 /* Now set up the new line discipline. */
644 tty_ldisc_assign(tty, new_ldisc);
645 tty_set_termios_ldisc(tty, ldisc);
647 retval = tty_ldisc_open(tty, new_ldisc);
648 if (retval < 0) {
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);
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 */
658 if (tty->ldisc->ops->num != o_ldisc->ops->num && tty->ops->set_ldisc)
659 tty->ops->set_ldisc(tty);
661 tty_ldisc_put(o_ldisc);
664 * Allow ldisc referencing to occur again
667 tty_ldisc_enable(tty);
668 if (o_tty)
669 tty_ldisc_enable(o_tty);
671 /* Restart the work queue in case no characters kick it off. Safe if
672 already running */
673 if (work)
674 schedule_delayed_work(&tty->buf.work, 1);
675 if (o_work)
676 schedule_delayed_work(&o_tty->buf.work, 1);
677 mutex_unlock(&tty->ldisc_mutex);
678 return retval;
682 * tty_reset_termios - reset terminal state
683 * @tty: tty to reset
685 * Restore a terminal to the driver default state.
688 static void tty_reset_termios(struct tty_struct *tty)
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);
699 * tty_ldisc_reinit - reinitialise the tty ldisc
700 * @tty: tty to reinit
702 * Switch the tty back to N_TTY line discipline and leave the
703 * ldisc state closed
706 static void tty_ldisc_reinit(struct tty_struct *tty)
708 struct tty_ldisc *ld;
710 tty_ldisc_close(tty, tty->ldisc);
711 tty_ldisc_put(tty->ldisc);
712 tty->ldisc = NULL;
714 * Switch the line discipline back
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);
723 * tty_ldisc_hangup - hangup ldisc reset
724 * @tty: tty being hung up
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.
730 * Locking: We can take the ldisc mutex as the rest of the code is
731 * careful to allow for this.
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.
737 void tty_ldisc_hangup(struct tty_struct *tty)
739 struct tty_ldisc *ld;
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.
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);
760 * FIXME: Once we trust the LDISC code better we can wait here for
761 * ldisc completion and fix the driver call race
763 wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
764 wake_up_interruptible_poll(&tty->read_wait, POLLIN);
766 * Shutdown the current line discipline, and reset it to
767 * N_TTY.
769 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) {
770 /* Make sure the old ldisc is quiescent */
771 tty_ldisc_halt(tty);
772 flush_scheduled_work();
774 /* Avoid racing set_ldisc or tty_ldisc_release */
775 mutex_lock(&tty->ldisc_mutex);
776 if (tty->ldisc) { /* Not yet closed */
777 /* Switch back to N_TTY */
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);
786 mutex_unlock(&tty->ldisc_mutex);
787 tty_reset_termios(tty);
792 * tty_ldisc_setup - open line discipline
793 * @tty: tty being shut down
794 * @o_tty: pair tty for pty/tty pairs
796 * Called during the initial open of a tty/pty pair in order to set up the
797 * line disciplines and bind them to the tty. This has no locking issues
798 * as the device isn't yet active.
801 int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty)
803 struct tty_ldisc *ld = tty->ldisc;
804 int retval;
806 retval = tty_ldisc_open(tty, ld);
807 if (retval)
808 return retval;
810 if (o_tty) {
811 retval = tty_ldisc_open(o_tty, o_tty->ldisc);
812 if (retval) {
813 tty_ldisc_close(tty, ld);
814 return retval;
816 tty_ldisc_enable(o_tty);
818 tty_ldisc_enable(tty);
819 return 0;
822 * tty_ldisc_release - release line discipline
823 * @tty: tty being shut down
824 * @o_tty: pair tty for pty/tty pairs
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
828 * ldisc has not been opened.
831 void tty_ldisc_release(struct tty_struct *tty, struct tty_struct *o_tty)
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.
839 tty_ldisc_halt(tty);
840 flush_scheduled_work();
842 mutex_lock(&tty->ldisc_mutex);
844 * Now kill off the ldisc
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;
851 /* Ensure the next open requests the N_TTY ldisc */
852 tty_set_termios_ldisc(tty, N_TTY);
853 mutex_unlock(&tty->ldisc_mutex);
855 /* This will need doing differently if we need to lock */
856 if (o_tty)
857 tty_ldisc_release(o_tty, NULL);
859 /* And the memory resources remaining (buffers, termios) will be
860 disposed of when the kref hits zero */
864 * tty_ldisc_init - ldisc setup for new tty
865 * @tty: tty being allocated
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.
871 void tty_ldisc_init(struct tty_struct *tty)
873 struct tty_ldisc *ld = tty_ldisc_get(N_TTY);
874 if (IS_ERR(ld))
875 panic("n_tty: init_tty");
876 tty_ldisc_assign(tty, ld);
879 void tty_ldisc_begin(void)
881 /* Setup the default TTY line discipline. */
882 (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);