198602c01SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */ 298602c01SGreg Kroah-Hartman /* 398602c01SGreg Kroah-Hartman * TTY core internal functions 498602c01SGreg Kroah-Hartman */ 598602c01SGreg Kroah-Hartman 698602c01SGreg Kroah-Hartman #ifndef _TTY_INTERNAL_H 798602c01SGreg Kroah-Hartman #define _TTY_INTERNAL_H 898602c01SGreg Kroah-Hartman 998602c01SGreg Kroah-Hartman #define tty_msg(fn, tty, f, ...) \ 1098602c01SGreg Kroah-Hartman fn("%s %s: " f, tty_driver_name(tty), tty_name(tty), ##__VA_ARGS__) 1198602c01SGreg Kroah-Hartman 1298602c01SGreg Kroah-Hartman #define tty_debug(tty, f, ...) tty_msg(pr_debug, tty, f, ##__VA_ARGS__) 1398602c01SGreg Kroah-Hartman #define tty_notice(tty, f, ...) tty_msg(pr_notice, tty, f, ##__VA_ARGS__) 1498602c01SGreg Kroah-Hartman #define tty_warn(tty, f, ...) tty_msg(pr_warn, tty, f, ##__VA_ARGS__) 1598602c01SGreg Kroah-Hartman #define tty_err(tty, f, ...) tty_msg(pr_err, tty, f, ##__VA_ARGS__) 1698602c01SGreg Kroah-Hartman 1798602c01SGreg Kroah-Hartman #define tty_info_ratelimited(tty, f, ...) \ 1898602c01SGreg Kroah-Hartman tty_msg(pr_info_ratelimited, tty, f, ##__VA_ARGS__) 1998602c01SGreg Kroah-Hartman 206c80c0b9SGreg Kroah-Hartman /* 216c80c0b9SGreg Kroah-Hartman * Lock subclasses for tty locks 226c80c0b9SGreg Kroah-Hartman * 236c80c0b9SGreg Kroah-Hartman * TTY_LOCK_NORMAL is for normal ttys and master ptys. 246c80c0b9SGreg Kroah-Hartman * TTY_LOCK_SLAVE is for slave ptys only. 256c80c0b9SGreg Kroah-Hartman * 266c80c0b9SGreg Kroah-Hartman * Lock subclasses are necessary for handling nested locking with pty pairs. 276c80c0b9SGreg Kroah-Hartman * tty locks which use nested locking: 286c80c0b9SGreg Kroah-Hartman * 296c80c0b9SGreg Kroah-Hartman * legacy_mutex - Nested tty locks are necessary for releasing pty pairs. 306c80c0b9SGreg Kroah-Hartman * The stable lock order is master pty first, then slave pty. 316c80c0b9SGreg Kroah-Hartman * termios_rwsem - The stable lock order is tty_buffer lock->termios_rwsem. 326c80c0b9SGreg Kroah-Hartman * Subclassing this lock enables the slave pty to hold its 336c80c0b9SGreg Kroah-Hartman * termios_rwsem when claiming the master tty_buffer lock. 346c80c0b9SGreg Kroah-Hartman * tty_buffer lock - slave ptys can claim nested buffer lock when handling 356c80c0b9SGreg Kroah-Hartman * signal chars. The stable lock order is slave pty, then 366c80c0b9SGreg Kroah-Hartman * master. 376c80c0b9SGreg Kroah-Hartman */ 386c80c0b9SGreg Kroah-Hartman enum { 396c80c0b9SGreg Kroah-Hartman TTY_LOCK_NORMAL = 0, 406c80c0b9SGreg Kroah-Hartman TTY_LOCK_SLAVE, 416c80c0b9SGreg Kroah-Hartman }; 426c80c0b9SGreg Kroah-Hartman 439f72cab1SGreg Kroah-Hartman /* Values for tty->flow_change */ 449f72cab1SGreg Kroah-Hartman #define TTY_THROTTLE_SAFE 1 459f72cab1SGreg Kroah-Hartman #define TTY_UNTHROTTLE_SAFE 2 469f72cab1SGreg Kroah-Hartman 479f72cab1SGreg Kroah-Hartman static inline void __tty_set_flow_change(struct tty_struct *tty, int val) 489f72cab1SGreg Kroah-Hartman { 499f72cab1SGreg Kroah-Hartman tty->flow_change = val; 509f72cab1SGreg Kroah-Hartman } 519f72cab1SGreg Kroah-Hartman 529f72cab1SGreg Kroah-Hartman static inline void tty_set_flow_change(struct tty_struct *tty, int val) 539f72cab1SGreg Kroah-Hartman { 549f72cab1SGreg Kroah-Hartman tty->flow_change = val; 559f72cab1SGreg Kroah-Hartman smp_mb(); 569f72cab1SGreg Kroah-Hartman } 579f72cab1SGreg Kroah-Hartman 586c80c0b9SGreg Kroah-Hartman int tty_ldisc_lock(struct tty_struct *tty, unsigned long timeout); 596c80c0b9SGreg Kroah-Hartman void tty_ldisc_unlock(struct tty_struct *tty); 606c80c0b9SGreg Kroah-Hartman 615ffa6e34SGreg Kroah-Hartman int __tty_check_change(struct tty_struct *tty, int sig); 625ffa6e34SGreg Kroah-Hartman int tty_check_change(struct tty_struct *tty); 635ffa6e34SGreg Kroah-Hartman void __stop_tty(struct tty_struct *tty); 645ffa6e34SGreg Kroah-Hartman void __start_tty(struct tty_struct *tty); 655ffa6e34SGreg Kroah-Hartman void tty_vhangup_session(struct tty_struct *tty); 665ffa6e34SGreg Kroah-Hartman void tty_open_proc_set_tty(struct file *filp, struct tty_struct *tty); 675ffa6e34SGreg Kroah-Hartman int tty_signal_session_leader(struct tty_struct *tty, int exit_session); 685ffa6e34SGreg Kroah-Hartman void session_clear_tty(struct pid *session); 695ffa6e34SGreg Kroah-Hartman void tty_buffer_free_all(struct tty_port *port); 705ffa6e34SGreg Kroah-Hartman void tty_buffer_flush(struct tty_struct *tty, struct tty_ldisc *ld); 715ffa6e34SGreg Kroah-Hartman void tty_buffer_init(struct tty_port *port); 725ffa6e34SGreg Kroah-Hartman void tty_buffer_set_lock_subclass(struct tty_port *port); 735ffa6e34SGreg Kroah-Hartman bool tty_buffer_restart_work(struct tty_port *port); 745ffa6e34SGreg Kroah-Hartman bool tty_buffer_cancel_work(struct tty_port *port); 755ffa6e34SGreg Kroah-Hartman void tty_buffer_flush_work(struct tty_port *port); 76*87888fb9SIlpo Järvinen speed_t tty_termios_input_baud_rate(const struct ktermios *termios); 775ffa6e34SGreg Kroah-Hartman void tty_ldisc_hangup(struct tty_struct *tty, bool reset); 785ffa6e34SGreg Kroah-Hartman int tty_ldisc_reinit(struct tty_struct *tty, int disc); 795ffa6e34SGreg Kroah-Hartman long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg); 805ffa6e34SGreg Kroah-Hartman long tty_jobctrl_ioctl(struct tty_struct *tty, struct tty_struct *real_tty, 815ffa6e34SGreg Kroah-Hartman struct file *file, unsigned int cmd, unsigned long arg); 825ffa6e34SGreg Kroah-Hartman void tty_default_fops(struct file_operations *fops); 835ffa6e34SGreg Kroah-Hartman struct tty_struct *alloc_tty_struct(struct tty_driver *driver, int idx); 845ffa6e34SGreg Kroah-Hartman int tty_alloc_file(struct file *file); 855ffa6e34SGreg Kroah-Hartman void tty_add_file(struct tty_struct *tty, struct file *file); 865ffa6e34SGreg Kroah-Hartman void tty_free_file(struct file *file); 875ffa6e34SGreg Kroah-Hartman int tty_release(struct inode *inode, struct file *filp); 885ffa6e34SGreg Kroah-Hartman 895ffa6e34SGreg Kroah-Hartman #define tty_is_writelocked(tty) (mutex_is_locked(&tty->atomic_write_lock)) 905ffa6e34SGreg Kroah-Hartman 915ffa6e34SGreg Kroah-Hartman int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty); 925ffa6e34SGreg Kroah-Hartman void tty_ldisc_release(struct tty_struct *tty); 935ffa6e34SGreg Kroah-Hartman int __must_check tty_ldisc_init(struct tty_struct *tty); 945ffa6e34SGreg Kroah-Hartman void tty_ldisc_deinit(struct tty_struct *tty); 955ffa6e34SGreg Kroah-Hartman 965ffa6e34SGreg Kroah-Hartman void tty_sysctl_init(void); 975ffa6e34SGreg Kroah-Hartman 98da5d669eSGreg Kroah-Hartman /* tty_audit.c */ 99da5d669eSGreg Kroah-Hartman #ifdef CONFIG_AUDIT 100da5d669eSGreg Kroah-Hartman void tty_audit_add_data(struct tty_struct *tty, const void *data, size_t size); 101da5d669eSGreg Kroah-Hartman void tty_audit_tiocsti(struct tty_struct *tty, char ch); 102da5d669eSGreg Kroah-Hartman #else 103da5d669eSGreg Kroah-Hartman static inline void tty_audit_add_data(struct tty_struct *tty, const void *data, 104da5d669eSGreg Kroah-Hartman size_t size) 105da5d669eSGreg Kroah-Hartman { 106da5d669eSGreg Kroah-Hartman } 107da5d669eSGreg Kroah-Hartman static inline void tty_audit_tiocsti(struct tty_struct *tty, char ch) 108da5d669eSGreg Kroah-Hartman { 109da5d669eSGreg Kroah-Hartman } 110da5d669eSGreg Kroah-Hartman #endif 111da5d669eSGreg Kroah-Hartman 1129f72cab1SGreg Kroah-Hartman ssize_t redirected_tty_write(struct kiocb *, struct iov_iter *); 1139f72cab1SGreg Kroah-Hartman 114a501ab75SJiri Slaby int tty_insert_flip_string_and_push_buffer(struct tty_port *port, 115a501ab75SJiri Slaby const unsigned char *chars, size_t cnt); 116a501ab75SJiri Slaby 11798602c01SGreg Kroah-Hartman #endif 118