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 117 struct device; 118 struct signal_struct; 119 struct 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 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 * @ctrl: control settings grouped together 154 * @ctrl.lock: lock for @ctrl members 155 * @ctrl.pgrp: process group of this tty (setpgrp(2)) 156 * @ctrl.session: session of this tty (setsid(2)). Writes are protected by both 157 * @ctrl.lock and @legacy_mutex, readers must use at least one of 158 * them. 159 * @ctrl.pktstatus: packet mode status (bitwise OR of %TIOCPKT_ constants) 160 * @ctrl.packet: packet mode enabled 161 * @hw_stopped: not controlled by the tty layer, under @driver's control for CTS 162 * handling 163 * @receive_room: bytes permitted to feed to @ldisc without any being lost 164 * @flow_change: controls behavior of throttling, see tty_throttle_safe() and 165 * tty_unthrottle_safe() 166 * @link: link to another pty (master -> slave and vice versa) 167 * @fasync: state for %O_ASYNC (for %SIGIO); managed by fasync_helper() 168 * @write_wait: concurrent writers are waiting in this queue until they are 169 * allowed to write 170 * @read_wait: readers wait for data in this queue 171 * @hangup_work: normally a work to perform a hangup (do_tty_hangup()); while 172 * freeing the tty, (re)used to release_one_tty() 173 * @disc_data: pointer to @ldisc's private data (e.g. to &struct n_tty_data) 174 * @driver_data: pointer to @driver's private data (e.g. &struct uart_state) 175 * @files_lock: protects @tty_files list 176 * @tty_files: list of (re)openers of this tty (i.e. linked &struct 177 * tty_file_private) 178 * @closing: when set during close, n_tty processes only START & STOP chars 179 * @write_buf: temporary buffer used during tty_write() to copy user data to 180 * @write_cnt: count of bytes written in tty_write() to @write_buf 181 * @SAK_work: if the tty has a pending do_SAK, it is queued here 182 * @port: persistent storage for this device (i.e. &struct tty_port) 183 * 184 * All of the state associated with a tty while the tty is open. Persistent 185 * storage for tty devices is referenced here as @port and is documented in 186 * &struct tty_port. 187 */ 188 struct tty_struct { 189 struct kref kref; 190 int index; 191 struct device *dev; 192 struct tty_driver *driver; 193 struct tty_port *port; 194 const struct tty_operations *ops; 195 196 struct tty_ldisc *ldisc; 197 struct ld_semaphore ldisc_sem; 198 199 struct mutex atomic_write_lock; 200 struct mutex legacy_mutex; 201 struct mutex throttle_mutex; 202 struct rw_semaphore termios_rwsem; 203 struct mutex winsize_mutex; 204 struct ktermios termios, termios_locked; 205 char name[64]; 206 unsigned long flags; 207 int count; 208 unsigned int receive_room; 209 struct winsize winsize; 210 211 struct { 212 spinlock_t lock; 213 bool stopped; 214 bool tco_stopped; 215 } flow; 216 217 struct { 218 struct pid *pgrp; 219 struct pid *session; 220 spinlock_t lock; 221 unsigned char pktstatus; 222 bool packet; 223 } ctrl; 224 225 bool hw_stopped; 226 bool closing; 227 int flow_change; 228 229 struct tty_struct *link; 230 struct fasync_struct *fasync; 231 wait_queue_head_t write_wait; 232 wait_queue_head_t read_wait; 233 struct work_struct hangup_work; 234 void *disc_data; 235 void *driver_data; 236 spinlock_t files_lock; 237 int write_cnt; 238 u8 *write_buf; 239 240 struct list_head tty_files; 241 242 struct work_struct SAK_work; 243 } __randomize_layout; 244 245 /* Each of a tty's open files has private_data pointing to tty_file_private */ 246 struct tty_file_private { 247 struct tty_struct *tty; 248 struct file *file; 249 struct list_head list; 250 }; 251 252 /** 253 * enum tty_struct_flags - TTY Struct Flags 254 * 255 * These bits are used in the :c:member:`tty_struct.flags` field. 256 * 257 * So that interrupts won't be able to mess up the queues, 258 * copy_to_cooked must be atomic with respect to itself, as must 259 * tty->write. Thus, you must use the inline functions set_bit() and 260 * clear_bit() to make things atomic. 261 * 262 * @TTY_THROTTLED: 263 * Driver input is throttled. The ldisc should call 264 * :c:member:`tty_driver.unthrottle()` in order to resume reception when 265 * it is ready to process more data (at threshold min). 266 * 267 * @TTY_IO_ERROR: 268 * If set, causes all subsequent userspace read/write calls on the tty to 269 * fail, returning -%EIO. (May be no ldisc too.) 270 * 271 * @TTY_OTHER_CLOSED: 272 * Device is a pty and the other side has closed. 273 * 274 * @TTY_EXCLUSIVE: 275 * Exclusive open mode (a single opener). 276 * 277 * @TTY_DO_WRITE_WAKEUP: 278 * If set, causes the driver to call the 279 * :c:member:`tty_ldisc_ops.write_wakeup()` method in order to resume 280 * transmission when it can accept more data to transmit. 281 * 282 * @TTY_LDISC_OPEN: 283 * Indicates that a line discipline is open. For debugging purposes only. 284 * 285 * @TTY_PTY_LOCK: 286 * A flag private to pty code to implement %TIOCSPTLCK/%TIOCGPTLCK logic. 287 * 288 * @TTY_NO_WRITE_SPLIT: 289 * Prevent driver from splitting up writes into smaller chunks (preserve 290 * write boundaries to driver). 291 * 292 * @TTY_HUPPED: 293 * The TTY was hung up. This is set post :c:member:`tty_driver.hangup()`. 294 * 295 * @TTY_HUPPING: 296 * The TTY is in the process of hanging up to abort potential readers. 297 * 298 * @TTY_LDISC_CHANGING: 299 * Line discipline for this TTY is being changed. I/O should not block 300 * when this is set. Use tty_io_nonblock() to check. 301 * 302 * @TTY_LDISC_HALTED: 303 * Line discipline for this TTY was stopped. No work should be queued to 304 * this ldisc. 305 */ 306 enum tty_struct_flags { 307 TTY_THROTTLED, 308 TTY_IO_ERROR, 309 TTY_OTHER_CLOSED, 310 TTY_EXCLUSIVE, 311 TTY_DO_WRITE_WAKEUP, 312 TTY_LDISC_OPEN, 313 TTY_PTY_LOCK, 314 TTY_NO_WRITE_SPLIT, 315 TTY_HUPPED, 316 TTY_HUPPING, 317 TTY_LDISC_CHANGING, 318 TTY_LDISC_HALTED, 319 }; 320 321 static inline bool tty_io_nonblock(struct tty_struct *tty, struct file *file) 322 { 323 return file->f_flags & O_NONBLOCK || 324 test_bit(TTY_LDISC_CHANGING, &tty->flags); 325 } 326 327 static inline bool tty_io_error(struct tty_struct *tty) 328 { 329 return test_bit(TTY_IO_ERROR, &tty->flags); 330 } 331 332 static inline bool tty_throttled(struct tty_struct *tty) 333 { 334 return test_bit(TTY_THROTTLED, &tty->flags); 335 } 336 337 #ifdef CONFIG_TTY 338 void tty_kref_put(struct tty_struct *tty); 339 struct pid *tty_get_pgrp(struct tty_struct *tty); 340 void tty_vhangup_self(void); 341 void disassociate_ctty(int priv); 342 dev_t tty_devnum(struct tty_struct *tty); 343 void proc_clear_tty(struct task_struct *p); 344 struct tty_struct *get_current_tty(void); 345 /* tty_io.c */ 346 int __init tty_init(void); 347 const char *tty_name(const struct tty_struct *tty); 348 struct tty_struct *tty_kopen_exclusive(dev_t device); 349 struct tty_struct *tty_kopen_shared(dev_t device); 350 void tty_kclose(struct tty_struct *tty); 351 int tty_dev_name_to_number(const char *name, dev_t *number); 352 #else 353 static inline void tty_kref_put(struct tty_struct *tty) 354 { } 355 static inline struct pid *tty_get_pgrp(struct tty_struct *tty) 356 { return NULL; } 357 static inline void tty_vhangup_self(void) 358 { } 359 static inline void disassociate_ctty(int priv) 360 { } 361 static inline dev_t tty_devnum(struct tty_struct *tty) 362 { return 0; } 363 static inline void proc_clear_tty(struct task_struct *p) 364 { } 365 static inline struct tty_struct *get_current_tty(void) 366 { return NULL; } 367 /* tty_io.c */ 368 static inline int __init tty_init(void) 369 { return 0; } 370 static inline const char *tty_name(const struct tty_struct *tty) 371 { return "(none)"; } 372 static inline struct tty_struct *tty_kopen_exclusive(dev_t device) 373 { return ERR_PTR(-ENODEV); } 374 static inline void tty_kclose(struct tty_struct *tty) 375 { } 376 static inline int tty_dev_name_to_number(const char *name, dev_t *number) 377 { return -ENOTSUPP; } 378 #endif 379 380 extern struct ktermios tty_std_termios; 381 382 int vcs_init(void); 383 384 extern const struct class tty_class; 385 386 /** 387 * tty_kref_get - get a tty reference 388 * @tty: tty device 389 * 390 * Returns: a new reference to a tty object 391 * 392 * Locking: The caller must hold sufficient locks/counts to ensure that their 393 * existing reference cannot go away. 394 */ 395 static inline struct tty_struct *tty_kref_get(struct tty_struct *tty) 396 { 397 if (tty) 398 kref_get(&tty->kref); 399 return tty; 400 } 401 402 const char *tty_driver_name(const struct tty_struct *tty); 403 void tty_wait_until_sent(struct tty_struct *tty, long timeout); 404 void stop_tty(struct tty_struct *tty); 405 void start_tty(struct tty_struct *tty); 406 void tty_write_message(struct tty_struct *tty, char *msg); 407 int tty_send_xchar(struct tty_struct *tty, u8 ch); 408 int tty_put_char(struct tty_struct *tty, u8 c); 409 unsigned int tty_chars_in_buffer(struct tty_struct *tty); 410 unsigned int tty_write_room(struct tty_struct *tty); 411 void tty_driver_flush_buffer(struct tty_struct *tty); 412 void tty_unthrottle(struct tty_struct *tty); 413 bool tty_throttle_safe(struct tty_struct *tty); 414 bool tty_unthrottle_safe(struct tty_struct *tty); 415 int tty_do_resize(struct tty_struct *tty, struct winsize *ws); 416 int tty_get_icount(struct tty_struct *tty, 417 struct serial_icounter_struct *icount); 418 int tty_get_tiocm(struct tty_struct *tty); 419 int is_current_pgrp_orphaned(void); 420 void tty_hangup(struct tty_struct *tty); 421 void tty_vhangup(struct tty_struct *tty); 422 int tty_hung_up_p(struct file *filp); 423 void do_SAK(struct tty_struct *tty); 424 void __do_SAK(struct tty_struct *tty); 425 void no_tty(void); 426 speed_t tty_termios_baud_rate(const struct ktermios *termios); 427 void tty_termios_encode_baud_rate(struct ktermios *termios, speed_t ibaud, 428 speed_t obaud); 429 void tty_encode_baud_rate(struct tty_struct *tty, speed_t ibaud, 430 speed_t obaud); 431 432 /** 433 * tty_get_baud_rate - get tty bit rates 434 * @tty: tty to query 435 * 436 * Returns: the baud rate as an integer for this terminal 437 * 438 * Locking: The termios lock must be held by the caller. 439 */ 440 static inline speed_t tty_get_baud_rate(const struct tty_struct *tty) 441 { 442 return tty_termios_baud_rate(&tty->termios); 443 } 444 445 unsigned char tty_get_char_size(unsigned int cflag); 446 unsigned char tty_get_frame_size(unsigned int cflag); 447 448 void tty_termios_copy_hw(struct ktermios *new, const struct ktermios *old); 449 bool tty_termios_hw_change(const struct ktermios *a, const struct ktermios *b); 450 int tty_set_termios(struct tty_struct *tty, struct ktermios *kt); 451 452 void tty_wakeup(struct tty_struct *tty); 453 454 int tty_mode_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg); 455 int tty_perform_flush(struct tty_struct *tty, unsigned long arg); 456 struct tty_struct *tty_init_dev(struct tty_driver *driver, int idx); 457 void tty_release_struct(struct tty_struct *tty, int idx); 458 void tty_init_termios(struct tty_struct *tty); 459 void tty_save_termios(struct tty_struct *tty); 460 int tty_standard_install(struct tty_driver *driver, 461 struct tty_struct *tty); 462 463 extern struct mutex tty_mutex; 464 465 /* n_tty.c */ 466 void n_tty_inherit_ops(struct tty_ldisc_ops *ops); 467 #ifdef CONFIG_TTY 468 void __init n_tty_init(void); 469 #else 470 static inline void n_tty_init(void) { } 471 #endif 472 473 /* tty_audit.c */ 474 #ifdef CONFIG_AUDIT 475 void tty_audit_exit(void); 476 void tty_audit_fork(struct signal_struct *sig); 477 int tty_audit_push(void); 478 #else 479 static inline void tty_audit_exit(void) 480 { 481 } 482 static inline void tty_audit_fork(struct signal_struct *sig) 483 { 484 } 485 static inline int tty_audit_push(void) 486 { 487 return 0; 488 } 489 #endif 490 491 /* tty_ioctl.c */ 492 int n_tty_ioctl_helper(struct tty_struct *tty, unsigned int cmd, 493 unsigned long arg); 494 495 /* vt.c */ 496 497 int vt_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg); 498 499 long vt_compat_ioctl(struct tty_struct *tty, unsigned int cmd, 500 unsigned long arg); 501 502 /* tty_mutex.c */ 503 /* functions for preparation of BKL removal */ 504 void tty_lock(struct tty_struct *tty); 505 int tty_lock_interruptible(struct tty_struct *tty); 506 void tty_unlock(struct tty_struct *tty); 507 void tty_lock_slave(struct tty_struct *tty); 508 void tty_unlock_slave(struct tty_struct *tty); 509 void tty_set_lock_subclass(struct tty_struct *tty); 510 511 #endif 512