nilfs2: fix oopses with doubly mounted snapshots
[linux-2.6/mini2440.git] / drivers / char / tty_ldisc.c
blob1733d3439ad2fd31e2cac8d083c5de499c4a685d
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);
150 * tty_ldisc_try_get - try and reference an ldisc
151 * @disc: ldisc number
153 * Attempt to open and lock a line discipline into place. Return
154 * the line discipline refcounted or an error.
157 static struct tty_ldisc *tty_ldisc_try_get(int disc)
159 unsigned long flags;
160 struct tty_ldisc *ld;
161 struct tty_ldisc_ops *ldops;
162 int err = -EINVAL;
164 ld = kmalloc(sizeof(struct tty_ldisc), GFP_KERNEL);
165 if (ld == NULL)
166 return ERR_PTR(-ENOMEM);
168 spin_lock_irqsave(&tty_ldisc_lock, flags);
169 ld->ops = NULL;
170 ldops = tty_ldiscs[disc];
171 /* Check the entry is defined */
172 if (ldops) {
173 /* If the module is being unloaded we can't use it */
174 if (!try_module_get(ldops->owner))
175 err = -EAGAIN;
176 else {
177 /* lock it */
178 ldops->refcount++;
179 ld->ops = ldops;
180 atomic_set(&ld->users, 1);
181 err = 0;
184 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
185 if (err) {
186 kfree(ld);
187 return ERR_PTR(err);
189 return ld;
193 * tty_ldisc_get - take a reference to an ldisc
194 * @disc: ldisc number
196 * Takes a reference to a line discipline. Deals with refcounts and
197 * module locking counts. Returns NULL if the discipline is not available.
198 * Returns a pointer to the discipline and bumps the ref count if it is
199 * available
201 * Locking:
202 * takes tty_ldisc_lock to guard against ldisc races
205 static struct tty_ldisc *tty_ldisc_get(int disc)
207 struct tty_ldisc *ld;
209 if (disc < N_TTY || disc >= NR_LDISCS)
210 return ERR_PTR(-EINVAL);
211 ld = tty_ldisc_try_get(disc);
212 if (IS_ERR(ld)) {
213 request_module("tty-ldisc-%d", disc);
214 ld = tty_ldisc_try_get(disc);
216 return ld;
219 static void *tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos)
221 return (*pos < NR_LDISCS) ? pos : NULL;
224 static void *tty_ldiscs_seq_next(struct seq_file *m, void *v, loff_t *pos)
226 (*pos)++;
227 return (*pos < NR_LDISCS) ? pos : NULL;
230 static void tty_ldiscs_seq_stop(struct seq_file *m, void *v)
234 static int tty_ldiscs_seq_show(struct seq_file *m, void *v)
236 int i = *(loff_t *)v;
237 struct tty_ldisc *ld;
239 ld = tty_ldisc_try_get(i);
240 if (IS_ERR(ld))
241 return 0;
242 seq_printf(m, "%-10s %2d\n", ld->ops->name ? ld->ops->name : "???", i);
243 put_ldisc(ld);
244 return 0;
247 static const struct seq_operations tty_ldiscs_seq_ops = {
248 .start = tty_ldiscs_seq_start,
249 .next = tty_ldiscs_seq_next,
250 .stop = tty_ldiscs_seq_stop,
251 .show = tty_ldiscs_seq_show,
254 static int proc_tty_ldiscs_open(struct inode *inode, struct file *file)
256 return seq_open(file, &tty_ldiscs_seq_ops);
259 const struct file_operations tty_ldiscs_proc_fops = {
260 .owner = THIS_MODULE,
261 .open = proc_tty_ldiscs_open,
262 .read = seq_read,
263 .llseek = seq_lseek,
264 .release = seq_release,
268 * tty_ldisc_assign - set ldisc on a tty
269 * @tty: tty to assign
270 * @ld: line discipline
272 * Install an instance of a line discipline into a tty structure. The
273 * ldisc must have a reference count above zero to ensure it remains.
274 * The tty instance refcount starts at zero.
276 * Locking:
277 * Caller must hold references
280 static void tty_ldisc_assign(struct tty_struct *tty, struct tty_ldisc *ld)
282 tty->ldisc = ld;
286 * tty_ldisc_try - internal helper
287 * @tty: the tty
289 * Make a single attempt to grab and bump the refcount on
290 * the tty ldisc. Return 0 on failure or 1 on success. This is
291 * used to implement both the waiting and non waiting versions
292 * of tty_ldisc_ref
294 * Locking: takes tty_ldisc_lock
297 static struct tty_ldisc *tty_ldisc_try(struct tty_struct *tty)
299 unsigned long flags;
300 struct tty_ldisc *ld;
302 spin_lock_irqsave(&tty_ldisc_lock, flags);
303 ld = NULL;
304 if (test_bit(TTY_LDISC, &tty->flags))
305 ld = get_ldisc(tty->ldisc);
306 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
307 return ld;
311 * tty_ldisc_ref_wait - wait for the tty ldisc
312 * @tty: tty device
314 * Dereference the line discipline for the terminal and take a
315 * reference to it. If the line discipline is in flux then
316 * wait patiently until it changes.
318 * Note: Must not be called from an IRQ/timer context. The caller
319 * must also be careful not to hold other locks that will deadlock
320 * against a discipline change, such as an existing ldisc reference
321 * (which we check for)
323 * Locking: call functions take tty_ldisc_lock
326 struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty)
328 struct tty_ldisc *ld;
330 /* wait_event is a macro */
331 wait_event(tty_ldisc_wait, (ld = tty_ldisc_try(tty)) != NULL);
332 return ld;
334 EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait);
337 * tty_ldisc_ref - get the tty ldisc
338 * @tty: tty device
340 * Dereference the line discipline for the terminal and take a
341 * reference to it. If the line discipline is in flux then
342 * return NULL. Can be called from IRQ and timer functions.
344 * Locking: called functions take tty_ldisc_lock
347 struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty)
349 return tty_ldisc_try(tty);
351 EXPORT_SYMBOL_GPL(tty_ldisc_ref);
354 * tty_ldisc_deref - free a tty ldisc reference
355 * @ld: reference to free up
357 * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May
358 * be called in IRQ context.
360 * Locking: takes tty_ldisc_lock
363 void tty_ldisc_deref(struct tty_ldisc *ld)
365 put_ldisc(ld);
367 EXPORT_SYMBOL_GPL(tty_ldisc_deref);
369 static inline void tty_ldisc_put(struct tty_ldisc *ld)
371 put_ldisc(ld);
375 * tty_ldisc_enable - allow ldisc use
376 * @tty: terminal to activate ldisc on
378 * Set the TTY_LDISC flag when the line discipline can be called
379 * again. Do necessary wakeups for existing sleepers. Clear the LDISC
380 * changing flag to indicate any ldisc change is now over.
382 * Note: nobody should set the TTY_LDISC bit except via this function.
383 * Clearing directly is allowed.
386 void tty_ldisc_enable(struct tty_struct *tty)
388 set_bit(TTY_LDISC, &tty->flags);
389 clear_bit(TTY_LDISC_CHANGING, &tty->flags);
390 wake_up(&tty_ldisc_wait);
394 * tty_ldisc_flush - flush line discipline queue
395 * @tty: tty
397 * Flush the line discipline queue (if any) for this tty. If there
398 * is no line discipline active this is a no-op.
401 void tty_ldisc_flush(struct tty_struct *tty)
403 struct tty_ldisc *ld = tty_ldisc_ref(tty);
404 if (ld) {
405 if (ld->ops->flush_buffer)
406 ld->ops->flush_buffer(tty);
407 tty_ldisc_deref(ld);
409 tty_buffer_flush(tty);
411 EXPORT_SYMBOL_GPL(tty_ldisc_flush);
414 * tty_set_termios_ldisc - set ldisc field
415 * @tty: tty structure
416 * @num: line discipline number
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.
422 * Locking: takes termios_mutex
425 static void tty_set_termios_ldisc(struct tty_struct *tty, int num)
427 mutex_lock(&tty->termios_mutex);
428 tty->termios->c_line = num;
429 mutex_unlock(&tty->termios_mutex);
433 * tty_ldisc_open - open a line discipline
434 * @tty: tty we are opening the ldisc on
435 * @ld: discipline to open
437 * A helper opening method. Also a convenient debugging and check
438 * point.
441 static int tty_ldisc_open(struct tty_struct *tty, struct tty_ldisc *ld)
443 WARN_ON(test_and_set_bit(TTY_LDISC_OPEN, &tty->flags));
444 if (ld->ops->open)
445 return ld->ops->open(tty);
446 return 0;
450 * tty_ldisc_close - close a line discipline
451 * @tty: tty we are opening the ldisc on
452 * @ld: discipline to close
454 * A helper close method. Also a convenient debugging and check
455 * point.
458 static void tty_ldisc_close(struct tty_struct *tty, struct tty_ldisc *ld)
460 WARN_ON(!test_bit(TTY_LDISC_OPEN, &tty->flags));
461 clear_bit(TTY_LDISC_OPEN, &tty->flags);
462 if (ld->ops->close)
463 ld->ops->close(tty);
467 * tty_ldisc_restore - helper for tty ldisc change
468 * @tty: tty to recover
469 * @old: previous ldisc
471 * Restore the previous line discipline or N_TTY when a line discipline
472 * change fails due to an open error
475 static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old)
477 char buf[64];
478 struct tty_ldisc *new_ldisc;
479 int r;
481 /* There is an outstanding reference here so this is safe */
482 old = tty_ldisc_get(old->ops->num);
483 WARN_ON(IS_ERR(old));
484 tty_ldisc_assign(tty, old);
485 tty_set_termios_ldisc(tty, old->ops->num);
486 if (tty_ldisc_open(tty, old) < 0) {
487 tty_ldisc_put(old);
488 /* This driver is always present */
489 new_ldisc = tty_ldisc_get(N_TTY);
490 if (IS_ERR(new_ldisc))
491 panic("n_tty: get");
492 tty_ldisc_assign(tty, new_ldisc);
493 tty_set_termios_ldisc(tty, N_TTY);
494 r = tty_ldisc_open(tty, new_ldisc);
495 if (r < 0)
496 panic("Couldn't open N_TTY ldisc for "
497 "%s --- error %d.",
498 tty_name(tty, buf), r);
503 * tty_ldisc_halt - shut down the line discipline
504 * @tty: tty device
506 * Shut down the line discipline and work queue for this tty device.
507 * The TTY_LDISC flag being cleared ensures no further references can
508 * be obtained while the delayed work queue halt ensures that no more
509 * data is fed to the ldisc.
511 * In order to wait for any existing references to complete see
512 * tty_ldisc_wait_idle.
515 static int tty_ldisc_halt(struct tty_struct *tty)
517 clear_bit(TTY_LDISC, &tty->flags);
518 return cancel_delayed_work(&tty->buf.work);
522 * tty_set_ldisc - set line discipline
523 * @tty: the terminal to set
524 * @ldisc: the line discipline
526 * Set the discipline of a tty line. Must be called from a process
527 * context. The ldisc change logic has to protect itself against any
528 * overlapping ldisc change (including on the other end of pty pairs),
529 * the close of one side of a tty/pty pair, and eventually hangup.
531 * Locking: takes tty_ldisc_lock, termios_mutex
534 int tty_set_ldisc(struct tty_struct *tty, int ldisc)
536 int retval;
537 struct tty_ldisc *o_ldisc, *new_ldisc;
538 int work, o_work = 0;
539 struct tty_struct *o_tty;
541 new_ldisc = tty_ldisc_get(ldisc);
542 if (IS_ERR(new_ldisc))
543 return PTR_ERR(new_ldisc);
546 * We need to look at the tty locking here for pty/tty pairs
547 * when both sides try to change in parallel.
550 o_tty = tty->link; /* o_tty is the pty side or NULL */
554 * Check the no-op case
557 if (tty->ldisc->ops->num == ldisc) {
558 tty_ldisc_put(new_ldisc);
559 return 0;
563 * Problem: What do we do if this blocks ?
564 * We could deadlock here
567 tty_wait_until_sent(tty, 0);
569 mutex_lock(&tty->ldisc_mutex);
572 * We could be midstream of another ldisc change which has
573 * dropped the lock during processing. If so we need to wait.
576 while (test_bit(TTY_LDISC_CHANGING, &tty->flags)) {
577 mutex_unlock(&tty->ldisc_mutex);
578 wait_event(tty_ldisc_wait,
579 test_bit(TTY_LDISC_CHANGING, &tty->flags) == 0);
580 mutex_lock(&tty->ldisc_mutex);
582 set_bit(TTY_LDISC_CHANGING, &tty->flags);
585 * No more input please, we are switching. The new ldisc
586 * will update this value in the ldisc open function
589 tty->receive_room = 0;
591 o_ldisc = tty->ldisc;
593 * Make sure we don't change while someone holds a
594 * reference to the line discipline. The TTY_LDISC bit
595 * prevents anyone taking a reference once it is clear.
596 * We need the lock to avoid racing reference takers.
598 * We must clear the TTY_LDISC bit here to avoid a livelock
599 * with a userspace app continually trying to use the tty in
600 * parallel to the change and re-referencing the tty.
603 work = tty_ldisc_halt(tty);
604 if (o_tty)
605 o_work = tty_ldisc_halt(o_tty);
608 * Wait for ->hangup_work and ->buf.work handlers to terminate.
609 * We must drop the mutex here in case a hangup is also in process.
612 mutex_unlock(&tty->ldisc_mutex);
614 flush_scheduled_work();
616 mutex_lock(&tty->ldisc_mutex);
617 if (test_bit(TTY_HUPPED, &tty->flags)) {
618 /* We were raced by the hangup method. It will have stomped
619 the ldisc data and closed the ldisc down */
620 clear_bit(TTY_LDISC_CHANGING, &tty->flags);
621 mutex_unlock(&tty->ldisc_mutex);
622 tty_ldisc_put(new_ldisc);
623 return -EIO;
626 /* Shutdown the current discipline. */
627 tty_ldisc_close(tty, o_ldisc);
629 /* Now set up the new line discipline. */
630 tty_ldisc_assign(tty, new_ldisc);
631 tty_set_termios_ldisc(tty, ldisc);
633 retval = tty_ldisc_open(tty, new_ldisc);
634 if (retval < 0) {
635 /* Back to the old one or N_TTY if we can't */
636 tty_ldisc_put(new_ldisc);
637 tty_ldisc_restore(tty, o_ldisc);
640 /* At this point we hold a reference to the new ldisc and a
641 a reference to the old ldisc. If we ended up flipping back
642 to the existing ldisc we have two references to it */
644 if (tty->ldisc->ops->num != o_ldisc->ops->num && tty->ops->set_ldisc)
645 tty->ops->set_ldisc(tty);
647 tty_ldisc_put(o_ldisc);
650 * Allow ldisc referencing to occur again
653 tty_ldisc_enable(tty);
654 if (o_tty)
655 tty_ldisc_enable(o_tty);
657 /* Restart the work queue in case no characters kick it off. Safe if
658 already running */
659 if (work)
660 schedule_delayed_work(&tty->buf.work, 1);
661 if (o_work)
662 schedule_delayed_work(&o_tty->buf.work, 1);
663 mutex_unlock(&tty->ldisc_mutex);
664 return retval;
668 * tty_reset_termios - reset terminal state
669 * @tty: tty to reset
671 * Restore a terminal to the driver default state.
674 static void tty_reset_termios(struct tty_struct *tty)
676 mutex_lock(&tty->termios_mutex);
677 *tty->termios = tty->driver->init_termios;
678 tty->termios->c_ispeed = tty_termios_input_baud_rate(tty->termios);
679 tty->termios->c_ospeed = tty_termios_baud_rate(tty->termios);
680 mutex_unlock(&tty->termios_mutex);
685 * tty_ldisc_reinit - reinitialise the tty ldisc
686 * @tty: tty to reinit
688 * Switch the tty back to N_TTY line discipline and leave the
689 * ldisc state closed
692 static void tty_ldisc_reinit(struct tty_struct *tty)
694 struct tty_ldisc *ld;
696 tty_ldisc_close(tty, tty->ldisc);
697 tty_ldisc_put(tty->ldisc);
698 tty->ldisc = NULL;
700 * Switch the line discipline back
702 ld = tty_ldisc_get(N_TTY);
703 BUG_ON(IS_ERR(ld));
704 tty_ldisc_assign(tty, ld);
705 tty_set_termios_ldisc(tty, N_TTY);
709 * tty_ldisc_hangup - hangup ldisc reset
710 * @tty: tty being hung up
712 * Some tty devices reset their termios when they receive a hangup
713 * event. In that situation we must also switch back to N_TTY properly
714 * before we reset the termios data.
716 * Locking: We can take the ldisc mutex as the rest of the code is
717 * careful to allow for this.
719 * In the pty pair case this occurs in the close() path of the
720 * tty itself so we must be careful about locking rules.
723 void tty_ldisc_hangup(struct tty_struct *tty)
725 struct tty_ldisc *ld;
728 * FIXME! What are the locking issues here? This may me overdoing
729 * things... This question is especially important now that we've
730 * removed the irqlock.
732 ld = tty_ldisc_ref(tty);
733 if (ld != NULL) {
734 /* We may have no line discipline at this point */
735 if (ld->ops->flush_buffer)
736 ld->ops->flush_buffer(tty);
737 tty_driver_flush_buffer(tty);
738 if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
739 ld->ops->write_wakeup)
740 ld->ops->write_wakeup(tty);
741 if (ld->ops->hangup)
742 ld->ops->hangup(tty);
743 tty_ldisc_deref(ld);
746 * FIXME: Once we trust the LDISC code better we can wait here for
747 * ldisc completion and fix the driver call race
749 wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
750 wake_up_interruptible_poll(&tty->read_wait, POLLIN);
752 * Shutdown the current line discipline, and reset it to
753 * N_TTY.
755 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) {
756 /* Avoid racing set_ldisc or tty_ldisc_release */
757 mutex_lock(&tty->ldisc_mutex);
758 if (tty->ldisc) { /* Not yet closed */
759 /* Switch back to N_TTY */
760 tty_ldisc_halt(tty);
761 tty_ldisc_reinit(tty);
762 /* At this point we have a closed ldisc and we want to
763 reopen it. We could defer this to the next open but
764 it means auditing a lot of other paths so this is
765 a FIXME */
766 WARN_ON(tty_ldisc_open(tty, tty->ldisc));
767 tty_ldisc_enable(tty);
769 mutex_unlock(&tty->ldisc_mutex);
770 tty_reset_termios(tty);
775 * tty_ldisc_setup - open line discipline
776 * @tty: tty being shut down
777 * @o_tty: pair tty for pty/tty pairs
779 * Called during the initial open of a tty/pty pair in order to set up the
780 * line disciplines and bind them to the tty. This has no locking issues
781 * as the device isn't yet active.
784 int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty)
786 struct tty_ldisc *ld = tty->ldisc;
787 int retval;
789 retval = tty_ldisc_open(tty, ld);
790 if (retval)
791 return retval;
793 if (o_tty) {
794 retval = tty_ldisc_open(o_tty, o_tty->ldisc);
795 if (retval) {
796 tty_ldisc_close(tty, ld);
797 return retval;
799 tty_ldisc_enable(o_tty);
801 tty_ldisc_enable(tty);
802 return 0;
805 * tty_ldisc_release - release line discipline
806 * @tty: tty being shut down
807 * @o_tty: pair tty for pty/tty pairs
809 * Called during the final close of a tty/pty pair in order to shut down
810 * the line discpline layer. On exit the ldisc assigned is N_TTY and the
811 * ldisc has not been opened.
814 void tty_ldisc_release(struct tty_struct *tty, struct tty_struct *o_tty)
817 * Prevent flush_to_ldisc() from rescheduling the work for later. Then
818 * kill any delayed work. As this is the final close it does not
819 * race with the set_ldisc code path.
822 tty_ldisc_halt(tty);
823 flush_scheduled_work();
825 mutex_lock(&tty->ldisc_mutex);
827 * Now kill off the ldisc
829 tty_ldisc_close(tty, tty->ldisc);
830 tty_ldisc_put(tty->ldisc);
831 /* Force an oops if we mess this up */
832 tty->ldisc = NULL;
834 /* Ensure the next open requests the N_TTY ldisc */
835 tty_set_termios_ldisc(tty, N_TTY);
836 mutex_unlock(&tty->ldisc_mutex);
838 /* This will need doing differently if we need to lock */
839 if (o_tty)
840 tty_ldisc_release(o_tty, NULL);
842 /* And the memory resources remaining (buffers, termios) will be
843 disposed of when the kref hits zero */
847 * tty_ldisc_init - ldisc setup for new tty
848 * @tty: tty being allocated
850 * Set up the line discipline objects for a newly allocated tty. Note that
851 * the tty structure is not completely set up when this call is made.
854 void tty_ldisc_init(struct tty_struct *tty)
856 struct tty_ldisc *ld = tty_ldisc_get(N_TTY);
857 if (IS_ERR(ld))
858 panic("n_tty: init_tty");
859 tty_ldisc_assign(tty, ld);
862 void tty_ldisc_begin(void)
864 /* Setup the default TTY line discipline. */
865 (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);