162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * TTY core internal functions
462306a36Sopenharmony_ci */
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci#ifndef _TTY_INTERNAL_H
762306a36Sopenharmony_ci#define _TTY_INTERNAL_H
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci#define tty_msg(fn, tty, f, ...) \
1062306a36Sopenharmony_ci	fn("%s %s: " f, tty_driver_name(tty), tty_name(tty), ##__VA_ARGS__)
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci#define tty_debug(tty, f, ...)	tty_msg(pr_debug, tty, f, ##__VA_ARGS__)
1362306a36Sopenharmony_ci#define tty_notice(tty, f, ...)	tty_msg(pr_notice, tty, f, ##__VA_ARGS__)
1462306a36Sopenharmony_ci#define tty_warn(tty, f, ...)	tty_msg(pr_warn, tty, f, ##__VA_ARGS__)
1562306a36Sopenharmony_ci#define tty_err(tty, f, ...)	tty_msg(pr_err, tty, f, ##__VA_ARGS__)
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci#define tty_info_ratelimited(tty, f, ...) \
1862306a36Sopenharmony_ci		tty_msg(pr_info_ratelimited, tty, f, ##__VA_ARGS__)
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci/*
2162306a36Sopenharmony_ci * Lock subclasses for tty locks
2262306a36Sopenharmony_ci *
2362306a36Sopenharmony_ci * TTY_LOCK_NORMAL is for normal ttys and master ptys.
2462306a36Sopenharmony_ci * TTY_LOCK_SLAVE is for slave ptys only.
2562306a36Sopenharmony_ci *
2662306a36Sopenharmony_ci * Lock subclasses are necessary for handling nested locking with pty pairs.
2762306a36Sopenharmony_ci * tty locks which use nested locking:
2862306a36Sopenharmony_ci *
2962306a36Sopenharmony_ci * legacy_mutex - Nested tty locks are necessary for releasing pty pairs.
3062306a36Sopenharmony_ci *		  The stable lock order is master pty first, then slave pty.
3162306a36Sopenharmony_ci * termios_rwsem - The stable lock order is tty_buffer lock->termios_rwsem.
3262306a36Sopenharmony_ci *		   Subclassing this lock enables the slave pty to hold its
3362306a36Sopenharmony_ci *		   termios_rwsem when claiming the master tty_buffer lock.
3462306a36Sopenharmony_ci * tty_buffer lock - slave ptys can claim nested buffer lock when handling
3562306a36Sopenharmony_ci *		     signal chars. The stable lock order is slave pty, then
3662306a36Sopenharmony_ci *		     master.
3762306a36Sopenharmony_ci */
3862306a36Sopenharmony_cienum {
3962306a36Sopenharmony_ci	TTY_LOCK_NORMAL = 0,
4062306a36Sopenharmony_ci	TTY_LOCK_SLAVE,
4162306a36Sopenharmony_ci};
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_ci/* Values for tty->flow_change */
4462306a36Sopenharmony_ci#define TTY_THROTTLE_SAFE	1
4562306a36Sopenharmony_ci#define TTY_UNTHROTTLE_SAFE	2
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_cistatic inline void __tty_set_flow_change(struct tty_struct *tty, int val)
4862306a36Sopenharmony_ci{
4962306a36Sopenharmony_ci	tty->flow_change = val;
5062306a36Sopenharmony_ci}
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_cistatic inline void tty_set_flow_change(struct tty_struct *tty, int val)
5362306a36Sopenharmony_ci{
5462306a36Sopenharmony_ci	tty->flow_change = val;
5562306a36Sopenharmony_ci	smp_mb();
5662306a36Sopenharmony_ci}
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ciint tty_ldisc_lock(struct tty_struct *tty, unsigned long timeout);
5962306a36Sopenharmony_civoid tty_ldisc_unlock(struct tty_struct *tty);
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_ciint __tty_check_change(struct tty_struct *tty, int sig);
6262306a36Sopenharmony_ciint tty_check_change(struct tty_struct *tty);
6362306a36Sopenharmony_civoid __stop_tty(struct tty_struct *tty);
6462306a36Sopenharmony_civoid __start_tty(struct tty_struct *tty);
6562306a36Sopenharmony_civoid tty_write_unlock(struct tty_struct *tty);
6662306a36Sopenharmony_ciint tty_write_lock(struct tty_struct *tty, bool ndelay);
6762306a36Sopenharmony_civoid tty_vhangup_session(struct tty_struct *tty);
6862306a36Sopenharmony_civoid tty_open_proc_set_tty(struct file *filp, struct tty_struct *tty);
6962306a36Sopenharmony_ciint tty_signal_session_leader(struct tty_struct *tty, int exit_session);
7062306a36Sopenharmony_civoid session_clear_tty(struct pid *session);
7162306a36Sopenharmony_civoid tty_buffer_free_all(struct tty_port *port);
7262306a36Sopenharmony_civoid tty_buffer_flush(struct tty_struct *tty, struct tty_ldisc *ld);
7362306a36Sopenharmony_civoid tty_buffer_init(struct tty_port *port);
7462306a36Sopenharmony_civoid tty_buffer_set_lock_subclass(struct tty_port *port);
7562306a36Sopenharmony_cibool tty_buffer_restart_work(struct tty_port *port);
7662306a36Sopenharmony_cibool tty_buffer_cancel_work(struct tty_port *port);
7762306a36Sopenharmony_civoid tty_buffer_flush_work(struct tty_port *port);
7862306a36Sopenharmony_cispeed_t tty_termios_input_baud_rate(const struct ktermios *termios);
7962306a36Sopenharmony_civoid tty_ldisc_hangup(struct tty_struct *tty, bool reset);
8062306a36Sopenharmony_ciint tty_ldisc_reinit(struct tty_struct *tty, int disc);
8162306a36Sopenharmony_cilong tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
8262306a36Sopenharmony_cilong tty_jobctrl_ioctl(struct tty_struct *tty, struct tty_struct *real_tty,
8362306a36Sopenharmony_ci		       struct file *file, unsigned int cmd, unsigned long arg);
8462306a36Sopenharmony_civoid tty_default_fops(struct file_operations *fops);
8562306a36Sopenharmony_cistruct tty_struct *alloc_tty_struct(struct tty_driver *driver, int idx);
8662306a36Sopenharmony_ciint tty_alloc_file(struct file *file);
8762306a36Sopenharmony_civoid tty_add_file(struct tty_struct *tty, struct file *file);
8862306a36Sopenharmony_civoid tty_free_file(struct file *file);
8962306a36Sopenharmony_ciint tty_release(struct inode *inode, struct file *filp);
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_ci#define tty_is_writelocked(tty)  (mutex_is_locked(&tty->atomic_write_lock))
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_ciint tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty);
9462306a36Sopenharmony_civoid tty_ldisc_release(struct tty_struct *tty);
9562306a36Sopenharmony_ciint __must_check tty_ldisc_init(struct tty_struct *tty);
9662306a36Sopenharmony_civoid tty_ldisc_deinit(struct tty_struct *tty);
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_ciextern int tty_ldisc_autoload;
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_ci/* tty_audit.c */
10162306a36Sopenharmony_ci#ifdef CONFIG_AUDIT
10262306a36Sopenharmony_civoid tty_audit_add_data(const struct tty_struct *tty, const void *data,
10362306a36Sopenharmony_ci			size_t size);
10462306a36Sopenharmony_civoid tty_audit_tiocsti(const struct tty_struct *tty, u8 ch);
10562306a36Sopenharmony_ci#else
10662306a36Sopenharmony_cistatic inline void tty_audit_add_data(const struct tty_struct *tty,
10762306a36Sopenharmony_ci				      const void *data, size_t size)
10862306a36Sopenharmony_ci{
10962306a36Sopenharmony_ci}
11062306a36Sopenharmony_cistatic inline void tty_audit_tiocsti(const struct tty_struct *tty, u8 ch)
11162306a36Sopenharmony_ci{
11262306a36Sopenharmony_ci}
11362306a36Sopenharmony_ci#endif
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_cissize_t redirected_tty_write(struct kiocb *, struct iov_iter *);
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_ciint tty_insert_flip_string_and_push_buffer(struct tty_port *port,
11862306a36Sopenharmony_ci					   const u8 *chars, size_t cnt);
11962306a36Sopenharmony_ci
12062306a36Sopenharmony_ci#endif
121