1/* SPDX-License-Identifier: GPL-2.0 */ 2#ifndef _LINUX_TTY_H 3#define _LINUX_TTY_H 4 5#include <linux/fs.h> 6#include <linux/major.h> 7#include <linux/termios.h> 8#include <linux/workqueue.h> 9#include <linux/tty_driver.h> 10#include <linux/tty_ldisc.h> 11#include <linux/tty_port.h> 12#include <linux/mutex.h> 13#include <linux/tty_flags.h> 14#include <uapi/linux/tty.h> 15#include <linux/rwsem.h> 16#include <linux/llist.h> 17 18 19/* 20 * (Note: the *_driver.minor_start values 1, 64, 128, 192 are 21 * hardcoded at present.) 22 */ 23#define NR_UNIX98_PTY_DEFAULT 4096 /* Default maximum for Unix98 ptys */ 24#define NR_UNIX98_PTY_RESERVE 1024 /* Default reserve for main devpts */ 25#define NR_UNIX98_PTY_MAX (1 << MINORBITS) /* Absolute limit */ 26 27/* 28 * This character is the same as _POSIX_VDISABLE: it cannot be used as 29 * a c_cc[] character, but indicates that a particular special character 30 * isn't in use (eg VINTR has no character etc) 31 */ 32#define __DISABLED_CHAR '\0' 33 34#define INTR_CHAR(tty) ((tty)->termios.c_cc[VINTR]) 35#define QUIT_CHAR(tty) ((tty)->termios.c_cc[VQUIT]) 36#define ERASE_CHAR(tty) ((tty)->termios.c_cc[VERASE]) 37#define KILL_CHAR(tty) ((tty)->termios.c_cc[VKILL]) 38#define EOF_CHAR(tty) ((tty)->termios.c_cc[VEOF]) 39#define TIME_CHAR(tty) ((tty)->termios.c_cc[VTIME]) 40#define MIN_CHAR(tty) ((tty)->termios.c_cc[VMIN]) 41#define SWTC_CHAR(tty) ((tty)->termios.c_cc[VSWTC]) 42#define START_CHAR(tty) ((tty)->termios.c_cc[VSTART]) 43#define STOP_CHAR(tty) ((tty)->termios.c_cc[VSTOP]) 44#define SUSP_CHAR(tty) ((tty)->termios.c_cc[VSUSP]) 45#define EOL_CHAR(tty) ((tty)->termios.c_cc[VEOL]) 46#define REPRINT_CHAR(tty) ((tty)->termios.c_cc[VREPRINT]) 47#define DISCARD_CHAR(tty) ((tty)->termios.c_cc[VDISCARD]) 48#define WERASE_CHAR(tty) ((tty)->termios.c_cc[VWERASE]) 49#define LNEXT_CHAR(tty) ((tty)->termios.c_cc[VLNEXT]) 50#define EOL2_CHAR(tty) ((tty)->termios.c_cc[VEOL2]) 51 52#define _I_FLAG(tty, f) ((tty)->termios.c_iflag & (f)) 53#define _O_FLAG(tty, f) ((tty)->termios.c_oflag & (f)) 54#define _C_FLAG(tty, f) ((tty)->termios.c_cflag & (f)) 55#define _L_FLAG(tty, f) ((tty)->termios.c_lflag & (f)) 56 57#define I_IGNBRK(tty) _I_FLAG((tty), IGNBRK) 58#define I_BRKINT(tty) _I_FLAG((tty), BRKINT) 59#define I_IGNPAR(tty) _I_FLAG((tty), IGNPAR) 60#define I_PARMRK(tty) _I_FLAG((tty), PARMRK) 61#define I_INPCK(tty) _I_FLAG((tty), INPCK) 62#define I_ISTRIP(tty) _I_FLAG((tty), ISTRIP) 63#define I_INLCR(tty) _I_FLAG((tty), INLCR) 64#define I_IGNCR(tty) _I_FLAG((tty), IGNCR) 65#define I_ICRNL(tty) _I_FLAG((tty), ICRNL) 66#define I_IUCLC(tty) _I_FLAG((tty), IUCLC) 67#define I_IXON(tty) _I_FLAG((tty), IXON) 68#define I_IXANY(tty) _I_FLAG((tty), IXANY) 69#define I_IXOFF(tty) _I_FLAG((tty), IXOFF) 70#define I_IMAXBEL(tty) _I_FLAG((tty), IMAXBEL) 71#define I_IUTF8(tty) _I_FLAG((tty), IUTF8) 72 73#define O_OPOST(tty) _O_FLAG((tty), OPOST) 74#define O_OLCUC(tty) _O_FLAG((tty), OLCUC) 75#define O_ONLCR(tty) _O_FLAG((tty), ONLCR) 76#define O_OCRNL(tty) _O_FLAG((tty), OCRNL) 77#define O_ONOCR(tty) _O_FLAG((tty), ONOCR) 78#define O_ONLRET(tty) _O_FLAG((tty), ONLRET) 79#define O_OFILL(tty) _O_FLAG((tty), OFILL) 80#define O_OFDEL(tty) _O_FLAG((tty), OFDEL) 81#define O_NLDLY(tty) _O_FLAG((tty), NLDLY) 82#define O_CRDLY(tty) _O_FLAG((tty), CRDLY) 83#define O_TABDLY(tty) _O_FLAG((tty), TABDLY) 84#define O_BSDLY(tty) _O_FLAG((tty), BSDLY) 85#define O_VTDLY(tty) _O_FLAG((tty), VTDLY) 86#define O_FFDLY(tty) _O_FLAG((tty), FFDLY) 87 88#define C_BAUD(tty) _C_FLAG((tty), CBAUD) 89#define C_CSIZE(tty) _C_FLAG((tty), CSIZE) 90#define C_CSTOPB(tty) _C_FLAG((tty), CSTOPB) 91#define C_CREAD(tty) _C_FLAG((tty), CREAD) 92#define C_PARENB(tty) _C_FLAG((tty), PARENB) 93#define C_PARODD(tty) _C_FLAG((tty), PARODD) 94#define C_HUPCL(tty) _C_FLAG((tty), HUPCL) 95#define C_CLOCAL(tty) _C_FLAG((tty), CLOCAL) 96#define C_CIBAUD(tty) _C_FLAG((tty), CIBAUD) 97#define C_CRTSCTS(tty) _C_FLAG((tty), CRTSCTS) 98#define C_CMSPAR(tty) _C_FLAG((tty), CMSPAR) 99 100#define L_ISIG(tty) _L_FLAG((tty), ISIG) 101#define L_ICANON(tty) _L_FLAG((tty), ICANON) 102#define L_XCASE(tty) _L_FLAG((tty), XCASE) 103#define L_ECHO(tty) _L_FLAG((tty), ECHO) 104#define L_ECHOE(tty) _L_FLAG((tty), ECHOE) 105#define L_ECHOK(tty) _L_FLAG((tty), ECHOK) 106#define L_ECHONL(tty) _L_FLAG((tty), ECHONL) 107#define L_NOFLSH(tty) _L_FLAG((tty), NOFLSH) 108#define L_TOSTOP(tty) _L_FLAG((tty), TOSTOP) 109#define L_ECHOCTL(tty) _L_FLAG((tty), ECHOCTL) 110#define L_ECHOPRT(tty) _L_FLAG((tty), ECHOPRT) 111#define L_ECHOKE(tty) _L_FLAG((tty), ECHOKE) 112#define L_FLUSHO(tty) _L_FLAG((tty), FLUSHO) 113#define L_PENDIN(tty) _L_FLAG((tty), PENDIN) 114#define L_IEXTEN(tty) _L_FLAG((tty), IEXTEN) 115#define L_EXTPROC(tty) _L_FLAG((tty), EXTPROC) 116 117struct device; 118struct signal_struct; 119struct tty_operations; 120 121/** 122 * struct tty_struct - state associated with a tty while open 123 * 124 * @kref: reference counting by tty_kref_get() and tty_kref_put(), reaching zero 125 * frees the structure 126 * @dev: class device or %NULL (e.g. ptys, serdev) 127 * @driver: &struct tty_driver operating this tty 128 * @ops: &struct tty_operations of @driver for this tty (open, close, etc.) 129 * @index: index of this tty (e.g. to construct @name like tty12) 130 * @ldisc_sem: protects line discipline changes (@ldisc) -- lock tty not pty 131 * @ldisc: the current line discipline for this tty (n_tty by default) 132 * @atomic_write_lock: protects against concurrent writers, i.e. locks 133 * @write_cnt, @write_buf and similar 134 * @legacy_mutex: leftover from history (BKL -> BTM -> @legacy_mutex), 135 * protecting several operations on this tty 136 * @throttle_mutex: protects against concurrent tty_throttle_safe() and 137 * tty_unthrottle_safe() (but not tty_unthrottle()) 138 * @termios_rwsem: protects @termios and @termios_locked 139 * @winsize_mutex: protects @winsize 140 * @termios: termios for the current tty, copied from/to @driver.termios 141 * @termios_locked: locked termios (by %TIOCGLCKTRMIOS and %TIOCSLCKTRMIOS 142 * ioctls) 143 * @name: name of the tty constructed by tty_line_name() (e.g. ttyS3) 144 * @flags: bitwise OR of %TTY_THROTTLED, %TTY_IO_ERROR, ... 145 * @count: count of open processes, reaching zero cancels all the work for 146 * this tty and drops a @kref too (but does not free this tty) 147 * @winsize: size of the terminal "window" (cf. @winsize_mutex) 148 * @flow: flow settings grouped together, see also @flow.unused 149 * @flow.lock: lock for @flow members 150 * @flow.stopped: tty stopped/started by stop_tty()/start_tty() 151 * @flow.tco_stopped: tty stopped/started by %TCOOFF/%TCOON ioctls (it has 152 * precedence over @flow.stopped) 153 * @flow.unused: alignment for Alpha, so that no members other than @flow.* are 154 * modified by the same 64b word store. The @flow's __aligned is 155 * there for the very same reason. 156 * @ctrl: control settings grouped together, see also @ctrl.unused 157 * @ctrl.lock: lock for @ctrl members 158 * @ctrl.pgrp: process group of this tty (setpgrp(2)) 159 * @ctrl.session: session of this tty (setsid(2)). Writes are protected by both 160 * @ctrl.lock and @legacy_mutex, readers must use at least one of 161 * them. 162 * @ctrl.pktstatus: packet mode status (bitwise OR of %TIOCPKT_ constants) 163 * @ctrl.packet: packet mode enabled 164 * @ctrl.unused: alignment for Alpha, see @flow.unused for explanation 165 * @hw_stopped: not controlled by the tty layer, under @driver's control for CTS 166 * handling 167 * @receive_room: bytes permitted to feed to @ldisc without any being lost 168 * @flow_change: controls behavior of throttling, see tty_throttle_safe() and 169 * tty_unthrottle_safe() 170 * @link: link to another pty (master -> slave and vice versa) 171 * @fasync: state for %O_ASYNC (for %SIGIO); managed by fasync_helper() 172 * @write_wait: concurrent writers are waiting in this queue until they are 173 * allowed to write 174 * @read_wait: readers wait for data in this queue 175 * @hangup_work: normally a work to perform a hangup (do_tty_hangup()); while 176 * freeing the tty, (re)used to release_one_tty() 177 * @disc_data: pointer to @ldisc's private data (e.g. to &struct n_tty_data) 178 * @driver_data: pointer to @driver's private data (e.g. &struct uart_state) 179 * @files_lock: protects @tty_files list 180 * @tty_files: list of (re)openers of this tty (i.e. linked &struct 181 * tty_file_private) 182 * @closing: when set during close, n_tty processes only START & STOP chars 183 * @write_buf: temporary buffer used during tty_write() to copy user data to 184 * @write_cnt: count of bytes written in tty_write() to @write_buf 185 * @SAK_work: if the tty has a pending do_SAK, it is queued here 186 * @port: persistent storage for this device (i.e. &struct tty_port) 187 * 188 * All of the state associated with a tty while the tty is open. Persistent 189 * storage for tty devices is referenced here as @port and is documented in 190 * &struct tty_port. 191 */ 192struct tty_struct { 193 struct kref kref; 194 int index; 195 struct device *dev; 196 struct tty_driver *driver; 197 struct tty_port *port; 198 const struct tty_operations *ops; 199 200 struct tty_ldisc *ldisc; 201 struct ld_semaphore ldisc_sem; 202 203 struct mutex atomic_write_lock; 204 struct mutex legacy_mutex; 205 struct mutex throttle_mutex; 206 struct rw_semaphore termios_rwsem; 207 struct mutex winsize_mutex; 208 struct ktermios termios, termios_locked; 209 char name[64]; 210 unsigned long flags; 211 int count; 212 unsigned int receive_room; 213 struct winsize winsize; 214 215 struct { 216 spinlock_t lock; 217 bool stopped; 218 bool tco_stopped; 219 unsigned long unused[0]; 220 } __aligned(sizeof(unsigned long)) flow; 221 222 struct { 223 struct pid *pgrp; 224 struct pid *session; 225 spinlock_t lock; 226 unsigned char pktstatus; 227 bool packet; 228 unsigned long unused[0]; 229 } __aligned(sizeof(unsigned long)) ctrl; 230 231 bool hw_stopped; 232 bool closing; 233 int flow_change; 234 235 struct tty_struct *link; 236 struct fasync_struct *fasync; 237 wait_queue_head_t write_wait; 238 wait_queue_head_t read_wait; 239 struct work_struct hangup_work; 240 void *disc_data; 241 void *driver_data; 242 spinlock_t files_lock; 243 int write_cnt; 244 u8 *write_buf; 245 246 struct list_head tty_files; 247 248#define N_TTY_BUF_SIZE 4096 249 struct work_struct SAK_work; 250} __randomize_layout; 251 252/* Each of a tty's open files has private_data pointing to tty_file_private */ 253struct tty_file_private { 254 struct tty_struct *tty; 255 struct file *file; 256 struct list_head list; 257}; 258 259/** 260 * DOC: TTY Struct Flags 261 * 262 * These bits are used in the :c:member:`tty_struct.flags` field. 263 * 264 * So that interrupts won't be able to mess up the queues, 265 * copy_to_cooked must be atomic with respect to itself, as must 266 * tty->write. Thus, you must use the inline functions set_bit() and 267 * clear_bit() to make things atomic. 268 * 269 * TTY_THROTTLED 270 * Driver input is throttled. The ldisc should call 271 * :c:member:`tty_driver.unthrottle()` in order to resume reception when 272 * it is ready to process more data (at threshold min). 273 * 274 * TTY_IO_ERROR 275 * If set, causes all subsequent userspace read/write calls on the tty to 276 * fail, returning -%EIO. (May be no ldisc too.) 277 * 278 * TTY_OTHER_CLOSED 279 * Device is a pty and the other side has closed. 280 * 281 * TTY_EXCLUSIVE 282 * Exclusive open mode (a single opener). 283 * 284 * TTY_DO_WRITE_WAKEUP 285 * If set, causes the driver to call the 286 * :c:member:`tty_ldisc_ops.write_wakeup()` method in order to resume 287 * transmission when it can accept more data to transmit. 288 * 289 * TTY_LDISC_OPEN 290 * Indicates that a line discipline is open. For debugging purposes only. 291 * 292 * TTY_PTY_LOCK 293 * A flag private to pty code to implement %TIOCSPTLCK/%TIOCGPTLCK logic. 294 * 295 * TTY_NO_WRITE_SPLIT 296 * Prevent driver from splitting up writes into smaller chunks (preserve 297 * write boundaries to driver). 298 * 299 * TTY_HUPPED 300 * The TTY was hung up. This is set post :c:member:`tty_driver.hangup()`. 301 * 302 * TTY_HUPPING 303 * The TTY is in the process of hanging up to abort potential readers. 304 * 305 * TTY_LDISC_CHANGING 306 * Line discipline for this TTY is being changed. I/O should not block 307 * when this is set. Use tty_io_nonblock() to check. 308 * 309 * TTY_LDISC_HALTED 310 * Line discipline for this TTY was stopped. No work should be queued to 311 * this ldisc. 312 */ 313#define TTY_THROTTLED 0 314#define TTY_IO_ERROR 1 315#define TTY_OTHER_CLOSED 2 316#define TTY_EXCLUSIVE 3 317#define TTY_DO_WRITE_WAKEUP 5 318#define TTY_LDISC_OPEN 11 319#define TTY_PTY_LOCK 16 320#define TTY_NO_WRITE_SPLIT 17 321#define TTY_HUPPED 18 322#define TTY_HUPPING 19 323#define TTY_LDISC_CHANGING 20 324#define TTY_LDISC_HALTED 22 325 326static inline bool tty_io_nonblock(struct tty_struct *tty, struct file *file) 327{ 328 return file->f_flags & O_NONBLOCK || 329 test_bit(TTY_LDISC_CHANGING, &tty->flags); 330} 331 332static inline bool tty_io_error(struct tty_struct *tty) 333{ 334 return test_bit(TTY_IO_ERROR, &tty->flags); 335} 336 337static inline bool tty_throttled(struct tty_struct *tty) 338{ 339 return test_bit(TTY_THROTTLED, &tty->flags); 340} 341 342#ifdef CONFIG_TTY 343void tty_kref_put(struct tty_struct *tty); 344struct pid *tty_get_pgrp(struct tty_struct *tty); 345void tty_vhangup_self(void); 346void disassociate_ctty(int priv); 347dev_t tty_devnum(struct tty_struct *tty); 348void proc_clear_tty(struct task_struct *p); 349struct tty_struct *get_current_tty(void); 350/* tty_io.c */ 351int __init tty_init(void); 352const char *tty_name(const struct tty_struct *tty); 353struct tty_struct *tty_kopen_exclusive(dev_t device); 354struct tty_struct *tty_kopen_shared(dev_t device); 355void tty_kclose(struct tty_struct *tty); 356int tty_dev_name_to_number(const char *name, dev_t *number); 357#else 358static inline void tty_kref_put(struct tty_struct *tty) 359{ } 360static inline struct pid *tty_get_pgrp(struct tty_struct *tty) 361{ return NULL; } 362static inline void tty_vhangup_self(void) 363{ } 364static inline void disassociate_ctty(int priv) 365{ } 366static inline dev_t tty_devnum(struct tty_struct *tty) 367{ return 0; } 368static inline void proc_clear_tty(struct task_struct *p) 369{ } 370static inline struct tty_struct *get_current_tty(void) 371{ return NULL; } 372/* tty_io.c */ 373static inline int __init tty_init(void) 374{ return 0; } 375static inline const char *tty_name(const struct tty_struct *tty) 376{ return "(none)"; } 377static inline struct tty_struct *tty_kopen_exclusive(dev_t device) 378{ return ERR_PTR(-ENODEV); } 379static inline void tty_kclose(struct tty_struct *tty) 380{ } 381static inline int tty_dev_name_to_number(const char *name, dev_t *number) 382{ return -ENOTSUPP; } 383#endif 384 385extern struct ktermios tty_std_termios; 386 387int vcs_init(void); 388 389extern const struct class tty_class; 390 391/** 392 * tty_kref_get - get a tty reference 393 * @tty: tty device 394 * 395 * Returns: a new reference to a tty object 396 * 397 * Locking: The caller must hold sufficient locks/counts to ensure that their 398 * existing reference cannot go away. 399 */ 400static inline struct tty_struct *tty_kref_get(struct tty_struct *tty) 401{ 402 if (tty) 403 kref_get(&tty->kref); 404 return tty; 405} 406 407const char *tty_driver_name(const struct tty_struct *tty); 408void tty_wait_until_sent(struct tty_struct *tty, long timeout); 409void stop_tty(struct tty_struct *tty); 410void start_tty(struct tty_struct *tty); 411void tty_write_message(struct tty_struct *tty, char *msg); 412int tty_send_xchar(struct tty_struct *tty, u8 ch); 413int tty_put_char(struct tty_struct *tty, u8 c); 414unsigned int tty_chars_in_buffer(struct tty_struct *tty); 415unsigned int tty_write_room(struct tty_struct *tty); 416void tty_driver_flush_buffer(struct tty_struct *tty); 417void tty_unthrottle(struct tty_struct *tty); 418bool tty_throttle_safe(struct tty_struct *tty); 419bool tty_unthrottle_safe(struct tty_struct *tty); 420int tty_do_resize(struct tty_struct *tty, struct winsize *ws); 421int tty_get_icount(struct tty_struct *tty, 422 struct serial_icounter_struct *icount); 423int tty_get_tiocm(struct tty_struct *tty); 424int is_current_pgrp_orphaned(void); 425void tty_hangup(struct tty_struct *tty); 426void tty_vhangup(struct tty_struct *tty); 427int tty_hung_up_p(struct file *filp); 428void do_SAK(struct tty_struct *tty); 429void __do_SAK(struct tty_struct *tty); 430void no_tty(void); 431speed_t tty_termios_baud_rate(const struct ktermios *termios); 432void tty_termios_encode_baud_rate(struct ktermios *termios, speed_t ibaud, 433 speed_t obaud); 434void tty_encode_baud_rate(struct tty_struct *tty, speed_t ibaud, 435 speed_t obaud); 436 437/** 438 * tty_get_baud_rate - get tty bit rates 439 * @tty: tty to query 440 * 441 * Returns: the baud rate as an integer for this terminal 442 * 443 * Locking: The termios lock must be held by the caller. 444 */ 445static inline speed_t tty_get_baud_rate(const struct tty_struct *tty) 446{ 447 return tty_termios_baud_rate(&tty->termios); 448} 449 450unsigned char tty_get_char_size(unsigned int cflag); 451unsigned char tty_get_frame_size(unsigned int cflag); 452 453void tty_termios_copy_hw(struct ktermios *new, const struct ktermios *old); 454bool tty_termios_hw_change(const struct ktermios *a, const struct ktermios *b); 455int tty_set_termios(struct tty_struct *tty, struct ktermios *kt); 456 457void tty_wakeup(struct tty_struct *tty); 458 459int tty_mode_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg); 460int tty_perform_flush(struct tty_struct *tty, unsigned long arg); 461struct tty_struct *tty_init_dev(struct tty_driver *driver, int idx); 462void tty_release_struct(struct tty_struct *tty, int idx); 463void tty_init_termios(struct tty_struct *tty); 464void tty_save_termios(struct tty_struct *tty); 465int tty_standard_install(struct tty_driver *driver, 466 struct tty_struct *tty); 467 468extern struct mutex tty_mutex; 469 470/* n_tty.c */ 471void n_tty_inherit_ops(struct tty_ldisc_ops *ops); 472#ifdef CONFIG_TTY 473void __init n_tty_init(void); 474#else 475static inline void n_tty_init(void) { } 476#endif 477 478/* tty_audit.c */ 479#ifdef CONFIG_AUDIT 480void tty_audit_exit(void); 481void tty_audit_fork(struct signal_struct *sig); 482int tty_audit_push(void); 483#else 484static inline void tty_audit_exit(void) 485{ 486} 487static inline void tty_audit_fork(struct signal_struct *sig) 488{ 489} 490static inline int tty_audit_push(void) 491{ 492 return 0; 493} 494#endif 495 496/* tty_ioctl.c */ 497int n_tty_ioctl_helper(struct tty_struct *tty, unsigned int cmd, 498 unsigned long arg); 499 500/* vt.c */ 501 502int vt_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg); 503 504long vt_compat_ioctl(struct tty_struct *tty, unsigned int cmd, 505 unsigned long arg); 506 507/* tty_mutex.c */ 508/* functions for preparation of BKL removal */ 509void tty_lock(struct tty_struct *tty); 510int tty_lock_interruptible(struct tty_struct *tty); 511void tty_unlock(struct tty_struct *tty); 512void tty_lock_slave(struct tty_struct *tty); 513void tty_unlock_slave(struct tty_struct *tty); 514void tty_set_lock_subclass(struct tty_struct *tty); 515 516#endif 517