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/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>
22 #undef LDISC_DEBUG_HANGUP
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); \
29 #define tty_ldisc_debug(tty, f, args...)
32 /* lockdep nested classes for tty->ldisc_sem */
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.
45 static DEFINE_RAW_SPINLOCK(tty_ldiscs_lock);
46 /* Line disc dispatch table */
47 static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS];
50 * tty_register_ldisc - install a line discipline
52 * @new_ldisc: pointer to the ldisc object
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.
59 * takes tty_ldiscs_lock to guard against ldisc races
62 int tty_register_ldisc(int disc, struct tty_ldisc_ops *new_ldisc)
67 if (disc < N_TTY || disc >= NR_LDISCS)
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);
78 EXPORT_SYMBOL(tty_register_ldisc);
81 * tty_unregister_ldisc - unload a line discipline
83 * @new_ldisc: pointer to the ldisc object
85 * Remove a line discipline from the kernel providing it is not
89 * takes tty_ldiscs_lock to guard against ldisc races
92 int tty_unregister_ldisc(int disc)
97 if (disc < N_TTY || disc >= NR_LDISCS)
100 raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
101 if (tty_ldiscs[disc]->refcount)
104 tty_ldiscs[disc] = NULL;
105 raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
109 EXPORT_SYMBOL(tty_unregister_ldisc);
111 static struct tty_ldisc_ops *get_ldops(int disc)
114 struct tty_ldisc_ops *ldops, *ret;
116 raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
117 ret = ERR_PTR(-EINVAL);
118 ldops = tty_ldiscs[disc];
120 ret = ERR_PTR(-EAGAIN);
121 if (try_module_get(ldops->owner)) {
126 raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
130 static void put_ldops(struct tty_ldisc_ops *ldops)
134 raw_spin_lock_irqsave(&tty_ldiscs_lock, flags);
136 module_put(ldops->owner);
137 raw_spin_unlock_irqrestore(&tty_ldiscs_lock, flags);
141 * tty_ldisc_get - take a reference to an ldisc
142 * @disc: ldisc number
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
150 * takes tty_ldiscs_lock to guard against ldisc races
153 static struct tty_ldisc *tty_ldisc_get(struct tty_struct *tty, int disc)
155 struct tty_ldisc *ld;
156 struct tty_ldisc_ops *ldops;
158 if (disc < N_TTY || disc >= NR_LDISCS)
159 return ERR_PTR(-EINVAL);
162 * Get the ldisc ops - we may need to request them to be loaded
163 * dynamically and try again.
165 ldops = get_ldops(disc);
167 request_module("tty-ldisc-%d", disc);
168 ldops = get_ldops(disc);
170 return ERR_CAST(ldops);
173 ld = kmalloc(sizeof(struct tty_ldisc), GFP_KERNEL);
176 return ERR_PTR(-ENOMEM);
186 * tty_ldisc_put - release the ldisc
188 * Complement of tty_ldisc_get().
190 static inline void tty_ldisc_put(struct tty_ldisc *ld)
192 if (WARN_ON_ONCE(!ld))
199 static void *tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos)
201 return (*pos < NR_LDISCS) ? pos : NULL;
204 static void *tty_ldiscs_seq_next(struct seq_file *m, void *v, loff_t *pos)
207 return (*pos < NR_LDISCS) ? pos : NULL;
210 static void tty_ldiscs_seq_stop(struct seq_file *m, void *v)
214 static int tty_ldiscs_seq_show(struct seq_file *m, void *v)
216 int i = *(loff_t *)v;
217 struct tty_ldisc_ops *ldops;
219 ldops = get_ldops(i);
222 seq_printf(m, "%-10s %2d\n", ldops->name ? ldops->name : "???", i);
227 static 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,
234 static int proc_tty_ldiscs_open(struct inode *inode, struct file *file)
236 return seq_open(file, &tty_ldiscs_seq_ops);
239 const struct file_operations tty_ldiscs_proc_fops = {
240 .owner = THIS_MODULE,
241 .open = proc_tty_ldiscs_open,
244 .release = seq_release,
248 * tty_ldisc_ref_wait - wait for the tty ldisc
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.
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)
260 * Note: only callable from a file_operations routine (which
261 * guarantees tty->ldisc != NULL when the lock is acquired).
264 struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty)
266 ldsem_down_read(&tty->ldisc_sem, MAX_SCHEDULE_TIMEOUT);
267 WARN_ON(!tty->ldisc);
270 EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait);
273 * tty_ldisc_ref - get the tty ldisc
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.
281 struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty)
283 struct tty_ldisc *ld = NULL;
285 if (ldsem_down_read_trylock(&tty->ldisc_sem)) {
288 ldsem_up_read(&tty->ldisc_sem);
292 EXPORT_SYMBOL_GPL(tty_ldisc_ref);
295 * tty_ldisc_deref - free a tty ldisc reference
296 * @ld: reference to free up
298 * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May
299 * be called in IRQ context.
302 void tty_ldisc_deref(struct tty_ldisc *ld)
304 ldsem_up_read(&ld->tty->ldisc_sem);
306 EXPORT_SYMBOL_GPL(tty_ldisc_deref);
309 static inline int __lockfunc
310 __tty_ldisc_lock(struct tty_struct *tty, unsigned long timeout)
312 return ldsem_down_write(&tty->ldisc_sem, timeout);
315 static inline int __lockfunc
316 __tty_ldisc_lock_nested(struct tty_struct *tty, unsigned long timeout)
318 return ldsem_down_write_nested(&tty->ldisc_sem,
319 LDISC_SEM_OTHER, timeout);
322 static inline void __tty_ldisc_unlock(struct tty_struct *tty)
324 return ldsem_up_write(&tty->ldisc_sem);
327 static int __lockfunc
328 tty_ldisc_lock(struct tty_struct *tty, unsigned long timeout)
332 ret = __tty_ldisc_lock(tty, timeout);
335 set_bit(TTY_LDISC_HALTED, &tty->flags);
339 static void tty_ldisc_unlock(struct tty_struct *tty)
341 clear_bit(TTY_LDISC_HALTED, &tty->flags);
342 __tty_ldisc_unlock(tty);
345 static int __lockfunc
346 tty_ldisc_lock_pair_timeout(struct tty_struct *tty, struct tty_struct *tty2,
347 unsigned long timeout)
352 ret = __tty_ldisc_lock(tty, timeout);
354 ret = __tty_ldisc_lock_nested(tty2, timeout);
356 __tty_ldisc_unlock(tty);
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);
364 ret = __tty_ldisc_lock_nested(tty, timeout);
366 __tty_ldisc_unlock(tty2);
369 ret = __tty_ldisc_lock(tty, timeout);
375 set_bit(TTY_LDISC_HALTED, &tty->flags);
377 set_bit(TTY_LDISC_HALTED, &tty2->flags);
381 static void __lockfunc
382 tty_ldisc_lock_pair(struct tty_struct *tty, struct tty_struct *tty2)
384 tty_ldisc_lock_pair_timeout(tty, tty2, MAX_SCHEDULE_TIMEOUT);
387 static void __lockfunc tty_ldisc_unlock_pair(struct tty_struct *tty,
388 struct tty_struct *tty2)
390 __tty_ldisc_unlock(tty);
392 __tty_ldisc_unlock(tty2);
396 * tty_ldisc_flush - flush line discipline queue
399 * Flush the line discipline queue (if any) and the tty flip buffers
403 void tty_ldisc_flush(struct tty_struct *tty)
405 struct tty_ldisc *ld = tty_ldisc_ref(tty);
407 tty_buffer_flush(tty, ld);
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
422 * Locking: takes termios_rwsem
425 static void tty_set_termios_ldisc(struct tty_struct *tty, int num)
427 down_write(&tty->termios_rwsem);
428 tty->termios.c_line = num;
429 up_write(&tty->termios_rwsem);
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
440 * Locking: always called with BTM already held.
443 static int tty_ldisc_open(struct tty_struct *tty, struct tty_ldisc *ld)
445 WARN_ON(test_and_set_bit(TTY_LDISC_OPEN, &tty->flags));
448 /* BTM here locks versus a hangup event */
449 ret = ld->ops->open(tty);
451 clear_bit(TTY_LDISC_OPEN, &tty->flags);
458 * tty_ldisc_close - close a line discipline
459 * @tty: tty we are opening the ldisc on
460 * @ld: discipline to close
462 * A helper close method. Also a convenient debugging and check
466 static void tty_ldisc_close(struct tty_struct *tty, struct tty_ldisc *ld)
468 WARN_ON(!test_bit(TTY_LDISC_OPEN, &tty->flags));
469 clear_bit(TTY_LDISC_OPEN, &tty->flags);
475 * tty_ldisc_restore - helper for tty ldisc change
476 * @tty: tty to recover
477 * @old: previous ldisc
479 * Restore the previous line discipline or N_TTY when a line discipline
480 * change fails due to an open error
483 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(tty, old->ops->num);
490 WARN_ON(IS_ERR(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(tty, N_TTY);
497 if (IS_ERR(new_ldisc))
499 tty->ldisc = 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 "
510 * tty_set_ldisc - set line discipline
511 * @tty: the terminal to set
512 * @ldisc: the line discipline
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.
520 int tty_set_ldisc(struct tty_struct *tty, int ldisc)
523 struct tty_ldisc *old_ldisc, *new_ldisc;
525 new_ldisc = tty_ldisc_get(tty, ldisc);
526 if (IS_ERR(new_ldisc))
527 return PTR_ERR(new_ldisc);
530 retval = tty_ldisc_lock(tty, 5 * HZ);
532 tty_ldisc_put(new_ldisc);
538 * Check the no-op case
541 if (tty->ldisc->ops->num == ldisc) {
542 tty_ldisc_unlock(tty);
543 tty_ldisc_put(new_ldisc);
548 old_ldisc = tty->ldisc;
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);
559 /* Shutdown the old discipline. */
560 tty_ldisc_close(tty, old_ldisc);
562 /* Now set up the new line discipline. */
563 tty->ldisc = new_ldisc;
564 tty_set_termios_ldisc(tty, ldisc);
566 retval = tty_ldisc_open(tty, new_ldisc);
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);
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);
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. */
585 tty_ldisc_put(old_ldisc);
588 * Allow ldisc referencing to occur again
590 tty_ldisc_unlock(tty);
592 /* Restart the work queue in case no characters kick it off. Safe if
594 schedule_work(&tty->port->buf.work);
601 * tty_reset_termios - reset terminal state
604 * Restore a terminal to the driver default state.
607 static void tty_reset_termios(struct tty_struct *tty)
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);
618 * tty_ldisc_reinit - reinitialise the tty ldisc
619 * @tty: tty to reinit
620 * @ldisc: line discipline to reinitialize
622 * Switch the tty to a line discipline and leave the ldisc
626 static int tty_ldisc_reinit(struct tty_struct *tty, int ldisc)
628 struct tty_ldisc *ld = tty_ldisc_get(tty, ldisc);
633 tty_ldisc_close(tty, tty->ldisc);
634 tty_ldisc_put(tty->ldisc);
636 * Switch the line discipline back
639 tty_set_termios_ldisc(tty, ldisc);
645 * tty_ldisc_hangup - hangup ldisc reset
646 * @tty: tty being hung up
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.
652 * Locking: We can take the ldisc mutex as the rest of the code is
653 * careful to allow for this.
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.
659 void tty_ldisc_hangup(struct tty_struct *tty)
661 struct tty_ldisc *ld;
662 int reset = tty->driver->flags & TTY_DRIVER_RESET_TERMIOS;
665 tty_ldisc_debug(tty, "closing ldisc: %p\n", tty->ldisc);
667 ld = tty_ldisc_ref(tty);
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);
676 ld->ops->hangup(tty);
680 wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
681 wake_up_interruptible_poll(&tty->read_wait, POLLIN);
684 * Shutdown the current line discipline, and reset it to
687 * Avoid racing set_ldisc or tty_ldisc_release
689 tty_ldisc_lock(tty, MAX_SCHEDULE_TIMEOUT);
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
699 if (!tty_ldisc_reinit(tty, tty->termios.c_line))
700 err = tty_ldisc_open(tty, tty->ldisc);
704 /* If the re-open fails or we reset then go to N_TTY. The
705 N_TTY open cannot fail */
707 BUG_ON(tty_ldisc_reinit(tty, N_TTY));
708 WARN_ON(tty_ldisc_open(tty, tty->ldisc));
711 tty_ldisc_unlock(tty);
713 tty_reset_termios(tty);
715 tty_ldisc_debug(tty, "re-opened ldisc: %p\n", tty->ldisc);
719 * tty_ldisc_setup - open line discipline
720 * @tty: tty being shut down
721 * @o_tty: pair tty for pty/tty pairs
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.
728 int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty)
730 struct tty_ldisc *ld = tty->ldisc;
733 retval = tty_ldisc_open(tty, ld);
738 retval = tty_ldisc_open(o_tty, o_tty->ldisc);
740 tty_ldisc_close(tty, ld);
747 static void tty_ldisc_kill(struct tty_struct *tty)
750 * Now kill off the ldisc
752 tty_ldisc_close(tty, tty->ldisc);
753 tty_ldisc_put(tty->ldisc);
754 /* Force an oops if we mess this up */
757 /* Ensure the next open requests the N_TTY ldisc */
758 tty_set_termios_ldisc(tty, N_TTY);
762 * tty_ldisc_release - release line discipline
763 * @tty: tty being shut down (or one end of pty pair)
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.
770 void tty_ldisc_release(struct tty_struct *tty)
772 struct tty_struct *o_tty = tty->link;
775 * Shutdown this line discipline. As this is the final close,
776 * it does not race with the set_ldisc code path.
779 tty_ldisc_debug(tty, "closing ldisc: %p\n", tty->ldisc);
781 tty_ldisc_lock_pair(tty, o_tty);
784 tty_ldisc_kill(o_tty);
785 tty_ldisc_unlock_pair(tty, o_tty);
787 /* And the memory resources remaining (buffers, termios) will be
788 disposed of when the kref hits zero */
790 tty_ldisc_debug(tty, "ldisc closed\n");
794 * tty_ldisc_init - ldisc setup for new tty
795 * @tty: tty being allocated
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.
801 void tty_ldisc_init(struct tty_struct *tty)
803 struct tty_ldisc *ld = tty_ldisc_get(tty, N_TTY);
805 panic("n_tty: init_tty");
810 * tty_ldisc_init - ldisc cleanup for new tty
811 * @tty: tty that was allocated recently
813 * The tty structure must not becompletely set up (tty_ldisc_setup) when
816 void tty_ldisc_deinit(struct tty_struct *tty)
818 tty_ldisc_put(tty->ldisc);
822 void tty_ldisc_begin(void)
824 /* Setup the default TTY line discipline. */
825 (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);