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...)
34 /* lockdep nested classes for tty->ldisc_sem */
42 * This guards the refcounted line discipline lists. The lock
43 * must be taken with irqs off because there are hangup path
44 * callers who will do ldisc lookups and cannot sleep.
47 static DEFINE_RAW_SPINLOCK(tty_ldiscs_lock
);
48 /* Line disc dispatch table */
49 static struct tty_ldisc_ops
*tty_ldiscs
[NR_LDISCS
];
52 * tty_register_ldisc - install a line discipline
54 * @new_ldisc: pointer to the ldisc object
56 * Installs a new line discipline into the kernel. The discipline
57 * is set up as unreferenced and then made available to the kernel
58 * from this point onwards.
61 * takes tty_ldiscs_lock to guard against ldisc races
64 int tty_register_ldisc(int disc
, struct tty_ldisc_ops
*new_ldisc
)
69 if (disc
< N_TTY
|| disc
>= NR_LDISCS
)
72 raw_spin_lock_irqsave(&tty_ldiscs_lock
, flags
);
73 tty_ldiscs
[disc
] = new_ldisc
;
74 new_ldisc
->num
= disc
;
75 new_ldisc
->refcount
= 0;
76 raw_spin_unlock_irqrestore(&tty_ldiscs_lock
, flags
);
80 EXPORT_SYMBOL(tty_register_ldisc
);
83 * tty_unregister_ldisc - unload a line discipline
85 * @new_ldisc: pointer to the ldisc object
87 * Remove a line discipline from the kernel providing it is not
91 * takes tty_ldiscs_lock to guard against ldisc races
94 int tty_unregister_ldisc(int disc
)
99 if (disc
< N_TTY
|| disc
>= NR_LDISCS
)
102 raw_spin_lock_irqsave(&tty_ldiscs_lock
, flags
);
103 if (tty_ldiscs
[disc
]->refcount
)
106 tty_ldiscs
[disc
] = NULL
;
107 raw_spin_unlock_irqrestore(&tty_ldiscs_lock
, flags
);
111 EXPORT_SYMBOL(tty_unregister_ldisc
);
113 static struct tty_ldisc_ops
*get_ldops(int disc
)
116 struct tty_ldisc_ops
*ldops
, *ret
;
118 raw_spin_lock_irqsave(&tty_ldiscs_lock
, flags
);
119 ret
= ERR_PTR(-EINVAL
);
120 ldops
= tty_ldiscs
[disc
];
122 ret
= ERR_PTR(-EAGAIN
);
123 if (try_module_get(ldops
->owner
)) {
128 raw_spin_unlock_irqrestore(&tty_ldiscs_lock
, flags
);
132 static void put_ldops(struct tty_ldisc_ops
*ldops
)
136 raw_spin_lock_irqsave(&tty_ldiscs_lock
, flags
);
138 module_put(ldops
->owner
);
139 raw_spin_unlock_irqrestore(&tty_ldiscs_lock
, flags
);
143 * tty_ldisc_get - take a reference to an ldisc
144 * @disc: ldisc number
146 * Takes a reference to a line discipline. Deals with refcounts and
147 * module locking counts. Returns NULL if the discipline is not available.
148 * Returns a pointer to the discipline and bumps the ref count if it is
152 * takes tty_ldiscs_lock to guard against ldisc races
155 static struct tty_ldisc
*tty_ldisc_get(struct tty_struct
*tty
, int disc
)
157 struct tty_ldisc
*ld
;
158 struct tty_ldisc_ops
*ldops
;
160 if (disc
< N_TTY
|| disc
>= NR_LDISCS
)
161 return ERR_PTR(-EINVAL
);
164 * Get the ldisc ops - we may need to request them to be loaded
165 * dynamically and try again.
167 ldops
= get_ldops(disc
);
169 request_module("tty-ldisc-%d", disc
);
170 ldops
= get_ldops(disc
);
172 return ERR_CAST(ldops
);
175 ld
= kmalloc(sizeof(struct tty_ldisc
), GFP_KERNEL
);
178 return ERR_PTR(-ENOMEM
);
188 * tty_ldisc_put - release the ldisc
190 * Complement of tty_ldisc_get().
192 static inline void tty_ldisc_put(struct tty_ldisc
*ld
)
194 if (WARN_ON_ONCE(!ld
))
201 static void *tty_ldiscs_seq_start(struct seq_file
*m
, loff_t
*pos
)
203 return (*pos
< NR_LDISCS
) ? pos
: NULL
;
206 static void *tty_ldiscs_seq_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
209 return (*pos
< NR_LDISCS
) ? pos
: NULL
;
212 static void tty_ldiscs_seq_stop(struct seq_file
*m
, void *v
)
216 static int tty_ldiscs_seq_show(struct seq_file
*m
, void *v
)
218 int i
= *(loff_t
*)v
;
219 struct tty_ldisc_ops
*ldops
;
221 ldops
= get_ldops(i
);
224 seq_printf(m
, "%-10s %2d\n", ldops
->name
? ldops
->name
: "???", i
);
229 static const struct seq_operations tty_ldiscs_seq_ops
= {
230 .start
= tty_ldiscs_seq_start
,
231 .next
= tty_ldiscs_seq_next
,
232 .stop
= tty_ldiscs_seq_stop
,
233 .show
= tty_ldiscs_seq_show
,
236 static int proc_tty_ldiscs_open(struct inode
*inode
, struct file
*file
)
238 return seq_open(file
, &tty_ldiscs_seq_ops
);
241 const struct file_operations tty_ldiscs_proc_fops
= {
242 .owner
= THIS_MODULE
,
243 .open
= proc_tty_ldiscs_open
,
246 .release
= seq_release
,
250 * tty_ldisc_ref_wait - wait for the tty ldisc
253 * Dereference the line discipline for the terminal and take a
254 * reference to it. If the line discipline is in flux then
255 * wait patiently until it changes.
257 * Note: Must not be called from an IRQ/timer context. The caller
258 * must also be careful not to hold other locks that will deadlock
259 * against a discipline change, such as an existing ldisc reference
260 * (which we check for)
262 * Note: only callable from a file_operations routine (which
263 * guarantees tty->ldisc != NULL when the lock is acquired).
266 struct tty_ldisc
*tty_ldisc_ref_wait(struct tty_struct
*tty
)
268 ldsem_down_read(&tty
->ldisc_sem
, MAX_SCHEDULE_TIMEOUT
);
269 WARN_ON(!tty
->ldisc
);
272 EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait
);
275 * tty_ldisc_ref - get the tty ldisc
278 * Dereference the line discipline for the terminal and take a
279 * reference to it. If the line discipline is in flux then
280 * return NULL. Can be called from IRQ and timer functions.
283 struct tty_ldisc
*tty_ldisc_ref(struct tty_struct
*tty
)
285 struct tty_ldisc
*ld
= NULL
;
287 if (ldsem_down_read_trylock(&tty
->ldisc_sem
)) {
290 ldsem_up_read(&tty
->ldisc_sem
);
294 EXPORT_SYMBOL_GPL(tty_ldisc_ref
);
297 * tty_ldisc_deref - free a tty ldisc reference
298 * @ld: reference to free up
300 * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May
301 * be called in IRQ context.
304 void tty_ldisc_deref(struct tty_ldisc
*ld
)
306 ldsem_up_read(&ld
->tty
->ldisc_sem
);
308 EXPORT_SYMBOL_GPL(tty_ldisc_deref
);
311 static inline int __lockfunc
312 tty_ldisc_lock(struct tty_struct
*tty
, unsigned long timeout
)
314 return ldsem_down_write(&tty
->ldisc_sem
, timeout
);
317 static inline int __lockfunc
318 tty_ldisc_lock_nested(struct tty_struct
*tty
, unsigned long timeout
)
320 return ldsem_down_write_nested(&tty
->ldisc_sem
,
321 LDISC_SEM_OTHER
, timeout
);
324 static inline void tty_ldisc_unlock(struct tty_struct
*tty
)
326 return ldsem_up_write(&tty
->ldisc_sem
);
329 static int __lockfunc
330 tty_ldisc_lock_pair_timeout(struct tty_struct
*tty
, struct tty_struct
*tty2
,
331 unsigned long timeout
)
336 ret
= tty_ldisc_lock(tty
, timeout
);
338 ret
= tty_ldisc_lock_nested(tty2
, timeout
);
340 tty_ldisc_unlock(tty
);
343 /* if this is possible, it has lots of implications */
344 WARN_ON_ONCE(tty
== tty2
);
345 if (tty2
&& tty
!= tty2
) {
346 ret
= tty_ldisc_lock(tty2
, timeout
);
348 ret
= tty_ldisc_lock_nested(tty
, timeout
);
350 tty_ldisc_unlock(tty2
);
353 ret
= tty_ldisc_lock(tty
, timeout
);
359 set_bit(TTY_LDISC_HALTED
, &tty
->flags
);
361 set_bit(TTY_LDISC_HALTED
, &tty2
->flags
);
365 static void __lockfunc
366 tty_ldisc_lock_pair(struct tty_struct
*tty
, struct tty_struct
*tty2
)
368 tty_ldisc_lock_pair_timeout(tty
, tty2
, MAX_SCHEDULE_TIMEOUT
);
371 static void __lockfunc
tty_ldisc_unlock_pair(struct tty_struct
*tty
,
372 struct tty_struct
*tty2
)
374 tty_ldisc_unlock(tty
);
376 tty_ldisc_unlock(tty2
);
379 static void __lockfunc
tty_ldisc_enable_pair(struct tty_struct
*tty
,
380 struct tty_struct
*tty2
)
382 clear_bit(TTY_LDISC_HALTED
, &tty
->flags
);
384 clear_bit(TTY_LDISC_HALTED
, &tty2
->flags
);
386 tty_ldisc_unlock_pair(tty
, tty2
);
390 * tty_ldisc_flush - flush line discipline queue
393 * Flush the line discipline queue (if any) for this tty. If there
394 * is no line discipline active this is a no-op.
397 void tty_ldisc_flush(struct tty_struct
*tty
)
399 struct tty_ldisc
*ld
= tty_ldisc_ref(tty
);
401 if (ld
->ops
->flush_buffer
)
402 ld
->ops
->flush_buffer(tty
);
405 tty_buffer_flush(tty
);
407 EXPORT_SYMBOL_GPL(tty_ldisc_flush
);
410 * tty_set_termios_ldisc - set ldisc field
411 * @tty: tty structure
412 * @num: line discipline number
414 * This is probably overkill for real world processors but
415 * they are not on hot paths so a little discipline won't do
418 * Locking: takes termios_rwsem
421 static void tty_set_termios_ldisc(struct tty_struct
*tty
, int num
)
423 down_write(&tty
->termios_rwsem
);
424 tty
->termios
.c_line
= num
;
425 up_write(&tty
->termios_rwsem
);
429 * tty_ldisc_open - open a line discipline
430 * @tty: tty we are opening the ldisc on
431 * @ld: discipline to open
433 * A helper opening method. Also a convenient debugging and check
436 * Locking: always called with BTM already held.
439 static int tty_ldisc_open(struct tty_struct
*tty
, struct tty_ldisc
*ld
)
441 WARN_ON(test_and_set_bit(TTY_LDISC_OPEN
, &tty
->flags
));
444 /* BTM here locks versus a hangup event */
445 ret
= ld
->ops
->open(tty
);
447 clear_bit(TTY_LDISC_OPEN
, &tty
->flags
);
454 * tty_ldisc_close - close a line discipline
455 * @tty: tty we are opening the ldisc on
456 * @ld: discipline to close
458 * A helper close method. Also a convenient debugging and check
462 static void tty_ldisc_close(struct tty_struct
*tty
, struct tty_ldisc
*ld
)
464 WARN_ON(!test_bit(TTY_LDISC_OPEN
, &tty
->flags
));
465 clear_bit(TTY_LDISC_OPEN
, &tty
->flags
);
471 * tty_ldisc_restore - helper for tty ldisc change
472 * @tty: tty to recover
473 * @old: previous ldisc
475 * Restore the previous line discipline or N_TTY when a line discipline
476 * change fails due to an open error
479 static void tty_ldisc_restore(struct tty_struct
*tty
, struct tty_ldisc
*old
)
482 struct tty_ldisc
*new_ldisc
;
485 /* There is an outstanding reference here so this is safe */
486 old
= tty_ldisc_get(tty
, old
->ops
->num
);
487 WARN_ON(IS_ERR(old
));
489 tty_set_termios_ldisc(tty
, old
->ops
->num
);
490 if (tty_ldisc_open(tty
, old
) < 0) {
492 /* This driver is always present */
493 new_ldisc
= tty_ldisc_get(tty
, N_TTY
);
494 if (IS_ERR(new_ldisc
))
496 tty
->ldisc
= new_ldisc
;
497 tty_set_termios_ldisc(tty
, N_TTY
);
498 r
= tty_ldisc_open(tty
, new_ldisc
);
500 panic("Couldn't open N_TTY ldisc for "
502 tty_name(tty
, buf
), r
);
507 * tty_set_ldisc - set line discipline
508 * @tty: the terminal to set
509 * @ldisc: the line discipline
511 * Set the discipline of a tty line. Must be called from a process
512 * context. The ldisc change logic has to protect itself against any
513 * overlapping ldisc change (including on the other end of pty pairs),
514 * the close of one side of a tty/pty pair, and eventually hangup.
517 int tty_set_ldisc(struct tty_struct
*tty
, int ldisc
)
520 struct tty_ldisc
*old_ldisc
, *new_ldisc
;
521 struct tty_struct
*o_tty
= tty
->link
;
523 new_ldisc
= tty_ldisc_get(tty
, ldisc
);
524 if (IS_ERR(new_ldisc
))
525 return PTR_ERR(new_ldisc
);
527 retval
= tty_ldisc_lock_pair_timeout(tty
, o_tty
, 5 * HZ
);
529 tty_ldisc_put(new_ldisc
);
534 * Check the no-op case
537 if (tty
->ldisc
->ops
->num
== ldisc
) {
538 tty_ldisc_enable_pair(tty
, o_tty
);
539 tty_ldisc_put(new_ldisc
);
543 old_ldisc
= tty
->ldisc
;
546 if (test_bit(TTY_HUPPING
, &tty
->flags
) ||
547 test_bit(TTY_HUPPED
, &tty
->flags
)) {
548 /* We were raced by the hangup method. It will have stomped
549 the ldisc data and closed the ldisc down */
550 tty_ldisc_enable_pair(tty
, o_tty
);
551 tty_ldisc_put(new_ldisc
);
556 /* Shutdown the old discipline. */
557 tty_ldisc_close(tty
, old_ldisc
);
559 /* Now set up the new line discipline. */
560 tty
->ldisc
= new_ldisc
;
561 tty_set_termios_ldisc(tty
, ldisc
);
563 retval
= tty_ldisc_open(tty
, new_ldisc
);
565 /* Back to the old one or N_TTY if we can't */
566 tty_ldisc_put(new_ldisc
);
567 tty_ldisc_restore(tty
, old_ldisc
);
570 if (tty
->ldisc
->ops
->num
!= old_ldisc
->ops
->num
&& tty
->ops
->set_ldisc
)
571 tty
->ops
->set_ldisc(tty
);
573 /* At this point we hold a reference to the new ldisc and a
574 reference to the old ldisc, or we hold two references to
575 the old ldisc (if it was restored as part of error cleanup
576 above). In either case, releasing a single reference from
577 the old ldisc is correct. */
579 tty_ldisc_put(old_ldisc
);
582 * Allow ldisc referencing to occur again
584 tty_ldisc_enable_pair(tty
, o_tty
);
586 /* Restart the work queue in case no characters kick it off. Safe if
588 schedule_work(&tty
->port
->buf
.work
);
590 schedule_work(&o_tty
->port
->buf
.work
);
597 * tty_reset_termios - reset terminal state
600 * Restore a terminal to the driver default state.
603 static void tty_reset_termios(struct tty_struct
*tty
)
605 down_write(&tty
->termios_rwsem
);
606 tty
->termios
= tty
->driver
->init_termios
;
607 tty
->termios
.c_ispeed
= tty_termios_input_baud_rate(&tty
->termios
);
608 tty
->termios
.c_ospeed
= tty_termios_baud_rate(&tty
->termios
);
609 up_write(&tty
->termios_rwsem
);
614 * tty_ldisc_reinit - reinitialise the tty ldisc
615 * @tty: tty to reinit
616 * @ldisc: line discipline to reinitialize
618 * Switch the tty to a line discipline and leave the ldisc
622 static int tty_ldisc_reinit(struct tty_struct
*tty
, int ldisc
)
624 struct tty_ldisc
*ld
= tty_ldisc_get(tty
, ldisc
);
629 tty_ldisc_close(tty
, tty
->ldisc
);
630 tty_ldisc_put(tty
->ldisc
);
632 * Switch the line discipline back
635 tty_set_termios_ldisc(tty
, ldisc
);
641 * tty_ldisc_hangup - hangup ldisc reset
642 * @tty: tty being hung up
644 * Some tty devices reset their termios when they receive a hangup
645 * event. In that situation we must also switch back to N_TTY properly
646 * before we reset the termios data.
648 * Locking: We can take the ldisc mutex as the rest of the code is
649 * careful to allow for this.
651 * In the pty pair case this occurs in the close() path of the
652 * tty itself so we must be careful about locking rules.
655 void tty_ldisc_hangup(struct tty_struct
*tty
)
657 struct tty_ldisc
*ld
;
658 int reset
= tty
->driver
->flags
& TTY_DRIVER_RESET_TERMIOS
;
661 tty_ldisc_debug(tty
, "closing ldisc: %p\n", tty
->ldisc
);
663 ld
= tty_ldisc_ref(tty
);
665 if (ld
->ops
->flush_buffer
)
666 ld
->ops
->flush_buffer(tty
);
667 tty_driver_flush_buffer(tty
);
668 if ((test_bit(TTY_DO_WRITE_WAKEUP
, &tty
->flags
)) &&
669 ld
->ops
->write_wakeup
)
670 ld
->ops
->write_wakeup(tty
);
672 ld
->ops
->hangup(tty
);
676 wake_up_interruptible_poll(&tty
->write_wait
, POLLOUT
);
677 wake_up_interruptible_poll(&tty
->read_wait
, POLLIN
);
682 * Shutdown the current line discipline, and reset it to
685 * Avoid racing set_ldisc or tty_ldisc_release
687 tty_ldisc_lock_pair(tty
, tty
->link
);
692 /* At this point we have a halted ldisc; we want to close it and
693 reopen a new ldisc. We could defer the reopen to the next
694 open but it means auditing a lot of other paths so this is
698 if (!tty_ldisc_reinit(tty
, tty
->termios
.c_line
))
699 err
= tty_ldisc_open(tty
, tty
->ldisc
);
703 /* If the re-open fails or we reset then go to N_TTY. The
704 N_TTY open cannot fail */
706 BUG_ON(tty_ldisc_reinit(tty
, N_TTY
));
707 WARN_ON(tty_ldisc_open(tty
, tty
->ldisc
));
710 tty_ldisc_enable_pair(tty
, tty
->link
);
712 tty_reset_termios(tty
);
714 tty_ldisc_debug(tty
, "re-opened ldisc: %p\n", tty
->ldisc
);
718 * tty_ldisc_setup - open line discipline
719 * @tty: tty being shut down
720 * @o_tty: pair tty for pty/tty pairs
722 * Called during the initial open of a tty/pty pair in order to set up the
723 * line disciplines and bind them to the tty. This has no locking issues
724 * as the device isn't yet active.
727 int tty_ldisc_setup(struct tty_struct
*tty
, struct tty_struct
*o_tty
)
729 struct tty_ldisc
*ld
= tty
->ldisc
;
732 retval
= tty_ldisc_open(tty
, ld
);
737 retval
= tty_ldisc_open(o_tty
, o_tty
->ldisc
);
739 tty_ldisc_close(tty
, ld
);
746 static void tty_ldisc_kill(struct tty_struct
*tty
)
749 * Now kill off the ldisc
751 tty_ldisc_close(tty
, tty
->ldisc
);
752 tty_ldisc_put(tty
->ldisc
);
753 /* Force an oops if we mess this up */
756 /* Ensure the next open requests the N_TTY ldisc */
757 tty_set_termios_ldisc(tty
, N_TTY
);
761 * tty_ldisc_release - release line discipline
762 * @tty: tty being shut down
763 * @o_tty: pair tty for pty/tty pairs
765 * Called during the final close of a tty/pty pair in order to shut down
766 * the line discpline layer. On exit the ldisc assigned is N_TTY and the
767 * ldisc has not been opened.
770 void tty_ldisc_release(struct tty_struct
*tty
, struct tty_struct
*o_tty
)
773 * Shutdown this line discipline. As this is the final close,
774 * it does not race with the set_ldisc code path.
777 tty_ldisc_debug(tty
, "closing ldisc: %p\n", tty
->ldisc
);
779 tty_ldisc_lock_pair(tty
, o_tty
);
780 tty_lock_pair(tty
, o_tty
);
784 tty_ldisc_kill(o_tty
);
786 tty_unlock_pair(tty
, o_tty
);
787 tty_ldisc_unlock_pair(tty
, o_tty
);
789 /* And the memory resources remaining (buffers, termios) will be
790 disposed of when the kref hits zero */
792 tty_ldisc_debug(tty
, "ldisc closed\n");
796 * tty_ldisc_init - ldisc setup for new tty
797 * @tty: tty being allocated
799 * Set up the line discipline objects for a newly allocated tty. Note that
800 * the tty structure is not completely set up when this call is made.
803 void tty_ldisc_init(struct tty_struct
*tty
)
805 struct tty_ldisc
*ld
= tty_ldisc_get(tty
, N_TTY
);
807 panic("n_tty: init_tty");
812 * tty_ldisc_init - ldisc cleanup for new tty
813 * @tty: tty that was allocated recently
815 * The tty structure must not becompletely set up (tty_ldisc_setup) when
818 void tty_ldisc_deinit(struct tty_struct
*tty
)
820 tty_ldisc_put(tty
->ldisc
);
824 void tty_ldisc_begin(void)
826 /* Setup the default TTY line discipline. */
827 (void) tty_register_ldisc(N_TTY
, &tty_ldisc_N_TTY
);