• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_buffer.h>
10  #include <linux/tty_driver.h>
11  #include <linux/tty_ldisc.h>
12  #include <linux/tty_port.h>
13  #include <linux/mutex.h>
14  #include <linux/tty_flags.h>
15  #include <uapi/linux/tty.h>
16  #include <linux/rwsem.h>
17  #include <linux/llist.h>
18  #include <linux/android_kabi.h>
19  
20  
21  /*
22   * (Note: the *_driver.minor_start values 1, 64, 128, 192 are
23   * hardcoded at present.)
24   */
25  #define NR_UNIX98_PTY_DEFAULT	4096      /* Default maximum for Unix98 ptys */
26  #define NR_UNIX98_PTY_RESERVE	1024	  /* Default reserve for main devpts */
27  #define NR_UNIX98_PTY_MAX	(1 << MINORBITS) /* Absolute limit */
28  
29  /*
30   * This character is the same as _POSIX_VDISABLE: it cannot be used as
31   * a c_cc[] character, but indicates that a particular special character
32   * isn't in use (eg VINTR has no character etc)
33   */
34  #define __DISABLED_CHAR '\0'
35  
36  #define INTR_CHAR(tty) ((tty)->termios.c_cc[VINTR])
37  #define QUIT_CHAR(tty) ((tty)->termios.c_cc[VQUIT])
38  #define ERASE_CHAR(tty) ((tty)->termios.c_cc[VERASE])
39  #define KILL_CHAR(tty) ((tty)->termios.c_cc[VKILL])
40  #define EOF_CHAR(tty) ((tty)->termios.c_cc[VEOF])
41  #define TIME_CHAR(tty) ((tty)->termios.c_cc[VTIME])
42  #define MIN_CHAR(tty) ((tty)->termios.c_cc[VMIN])
43  #define SWTC_CHAR(tty) ((tty)->termios.c_cc[VSWTC])
44  #define START_CHAR(tty) ((tty)->termios.c_cc[VSTART])
45  #define STOP_CHAR(tty) ((tty)->termios.c_cc[VSTOP])
46  #define SUSP_CHAR(tty) ((tty)->termios.c_cc[VSUSP])
47  #define EOL_CHAR(tty) ((tty)->termios.c_cc[VEOL])
48  #define REPRINT_CHAR(tty) ((tty)->termios.c_cc[VREPRINT])
49  #define DISCARD_CHAR(tty) ((tty)->termios.c_cc[VDISCARD])
50  #define WERASE_CHAR(tty) ((tty)->termios.c_cc[VWERASE])
51  #define LNEXT_CHAR(tty)	((tty)->termios.c_cc[VLNEXT])
52  #define EOL2_CHAR(tty) ((tty)->termios.c_cc[VEOL2])
53  
54  #define _I_FLAG(tty, f)	((tty)->termios.c_iflag & (f))
55  #define _O_FLAG(tty, f)	((tty)->termios.c_oflag & (f))
56  #define _C_FLAG(tty, f)	((tty)->termios.c_cflag & (f))
57  #define _L_FLAG(tty, f)	((tty)->termios.c_lflag & (f))
58  
59  #define I_IGNBRK(tty)	_I_FLAG((tty), IGNBRK)
60  #define I_BRKINT(tty)	_I_FLAG((tty), BRKINT)
61  #define I_IGNPAR(tty)	_I_FLAG((tty), IGNPAR)
62  #define I_PARMRK(tty)	_I_FLAG((tty), PARMRK)
63  #define I_INPCK(tty)	_I_FLAG((tty), INPCK)
64  #define I_ISTRIP(tty)	_I_FLAG((tty), ISTRIP)
65  #define I_INLCR(tty)	_I_FLAG((tty), INLCR)
66  #define I_IGNCR(tty)	_I_FLAG((tty), IGNCR)
67  #define I_ICRNL(tty)	_I_FLAG((tty), ICRNL)
68  #define I_IUCLC(tty)	_I_FLAG((tty), IUCLC)
69  #define I_IXON(tty)	_I_FLAG((tty), IXON)
70  #define I_IXANY(tty)	_I_FLAG((tty), IXANY)
71  #define I_IXOFF(tty)	_I_FLAG((tty), IXOFF)
72  #define I_IMAXBEL(tty)	_I_FLAG((tty), IMAXBEL)
73  #define I_IUTF8(tty)	_I_FLAG((tty), IUTF8)
74  
75  #define O_OPOST(tty)	_O_FLAG((tty), OPOST)
76  #define O_OLCUC(tty)	_O_FLAG((tty), OLCUC)
77  #define O_ONLCR(tty)	_O_FLAG((tty), ONLCR)
78  #define O_OCRNL(tty)	_O_FLAG((tty), OCRNL)
79  #define O_ONOCR(tty)	_O_FLAG((tty), ONOCR)
80  #define O_ONLRET(tty)	_O_FLAG((tty), ONLRET)
81  #define O_OFILL(tty)	_O_FLAG((tty), OFILL)
82  #define O_OFDEL(tty)	_O_FLAG((tty), OFDEL)
83  #define O_NLDLY(tty)	_O_FLAG((tty), NLDLY)
84  #define O_CRDLY(tty)	_O_FLAG((tty), CRDLY)
85  #define O_TABDLY(tty)	_O_FLAG((tty), TABDLY)
86  #define O_BSDLY(tty)	_O_FLAG((tty), BSDLY)
87  #define O_VTDLY(tty)	_O_FLAG((tty), VTDLY)
88  #define O_FFDLY(tty)	_O_FLAG((tty), FFDLY)
89  
90  #define C_BAUD(tty)	_C_FLAG((tty), CBAUD)
91  #define C_CSIZE(tty)	_C_FLAG((tty), CSIZE)
92  #define C_CSTOPB(tty)	_C_FLAG((tty), CSTOPB)
93  #define C_CREAD(tty)	_C_FLAG((tty), CREAD)
94  #define C_PARENB(tty)	_C_FLAG((tty), PARENB)
95  #define C_PARODD(tty)	_C_FLAG((tty), PARODD)
96  #define C_HUPCL(tty)	_C_FLAG((tty), HUPCL)
97  #define C_CLOCAL(tty)	_C_FLAG((tty), CLOCAL)
98  #define C_CIBAUD(tty)	_C_FLAG((tty), CIBAUD)
99  #define C_CRTSCTS(tty)	_C_FLAG((tty), CRTSCTS)
100  #define C_CMSPAR(tty)	_C_FLAG((tty), CMSPAR)
101  
102  #define L_ISIG(tty)	_L_FLAG((tty), ISIG)
103  #define L_ICANON(tty)	_L_FLAG((tty), ICANON)
104  #define L_XCASE(tty)	_L_FLAG((tty), XCASE)
105  #define L_ECHO(tty)	_L_FLAG((tty), ECHO)
106  #define L_ECHOE(tty)	_L_FLAG((tty), ECHOE)
107  #define L_ECHOK(tty)	_L_FLAG((tty), ECHOK)
108  #define L_ECHONL(tty)	_L_FLAG((tty), ECHONL)
109  #define L_NOFLSH(tty)	_L_FLAG((tty), NOFLSH)
110  #define L_TOSTOP(tty)	_L_FLAG((tty), TOSTOP)
111  #define L_ECHOCTL(tty)	_L_FLAG((tty), ECHOCTL)
112  #define L_ECHOPRT(tty)	_L_FLAG((tty), ECHOPRT)
113  #define L_ECHOKE(tty)	_L_FLAG((tty), ECHOKE)
114  #define L_FLUSHO(tty)	_L_FLAG((tty), FLUSHO)
115  #define L_PENDIN(tty)	_L_FLAG((tty), PENDIN)
116  #define L_IEXTEN(tty)	_L_FLAG((tty), IEXTEN)
117  #define L_EXTPROC(tty)	_L_FLAG((tty), EXTPROC)
118  
119  struct device;
120  struct signal_struct;
121  struct tty_operations;
122  
123  /**
124   * struct tty_struct - state associated with a tty while open
125   *
126   * @flow.lock: lock for flow members
127   * @flow.stopped: tty stopped/started by tty_stop/tty_start
128   * @flow.tco_stopped: tty stopped/started by TCOOFF/TCOON ioctls (it has
129   *		      precedense over @flow.stopped)
130   * @flow.unused: alignment for Alpha, so that no members other than @flow.* are
131   *		 modified by the same 64b word store. The @flow's __aligned is
132   *		 there for the very same reason.
133   * @ctrl.lock: lock for ctrl members
134   * @ctrl.pgrp: process group of this tty (setpgrp(2))
135   * @ctrl.session: session of this tty (setsid(2)). Writes are protected by both
136   *		  @ctrl.lock and legacy mutex, readers must use at least one of
137   *		  them.
138   * @ctrl.pktstatus: packet mode status (bitwise OR of TIOCPKT_* constants)
139   * @ctrl.packet: packet mode enabled
140   *
141   * All of the state associated with a tty while the tty is open. Persistent
142   * storage for tty devices is referenced here as @port in struct tty_port.
143   */
144  struct tty_struct {
145  	int	magic;
146  	struct kref kref;
147  	struct device *dev;	/* class device or NULL (e.g. ptys, serdev) */
148  	struct tty_driver *driver;
149  	const struct tty_operations *ops;
150  	int index;
151  
152  	/* Protects ldisc changes: Lock tty not pty */
153  	struct ld_semaphore ldisc_sem;
154  	struct tty_ldisc *ldisc;
155  
156  	struct mutex atomic_write_lock;
157  	struct mutex legacy_mutex;
158  	struct mutex throttle_mutex;
159  	struct rw_semaphore termios_rwsem;
160  	struct mutex winsize_mutex;
161  	/* Termios values are protected by the termios rwsem */
162  	struct ktermios termios, termios_locked;
163  	char name[64];
164  	unsigned long flags;
165  	int count;
166  	struct winsize winsize;		/* winsize_mutex */
167  
168  	struct {
169  		spinlock_t lock;
170  		bool stopped;
171  		bool tco_stopped;
172  		unsigned long unused[0];
173  	} __aligned(sizeof(unsigned long)) flow;
174  
175  	struct {
176  		spinlock_t lock;
177  		struct pid *pgrp;
178  		struct pid *session;
179  		unsigned char pktstatus;
180  		bool packet;
181  		unsigned long unused[0];
182  	} __aligned(sizeof(unsigned long)) ctrl;
183  
184  	int hw_stopped;
185  	unsigned int receive_room;	/* Bytes free for queue */
186  	int flow_change;
187  
188  	struct tty_struct *link;
189  	struct fasync_struct *fasync;
190  	wait_queue_head_t write_wait;
191  	wait_queue_head_t read_wait;
192  	struct work_struct hangup_work;
193  	void *disc_data;
194  	void *driver_data;
195  	spinlock_t files_lock;		/* protects tty_files list */
196  	struct list_head tty_files;
197  
198  #define N_TTY_BUF_SIZE 4096
199  
200  	int closing;
201  	unsigned char *write_buf;
202  	int write_cnt;
203  	/* If the tty has a pending do_SAK, queue it here - akpm */
204  	struct work_struct SAK_work;
205  	struct tty_port *port;
206  
207  	ANDROID_KABI_RESERVE(1);
208  	ANDROID_KABI_RESERVE(2);
209  } __randomize_layout;
210  
211  /* Each of a tty's open files has private_data pointing to tty_file_private */
212  struct tty_file_private {
213  	struct tty_struct *tty;
214  	struct file *file;
215  	struct list_head list;
216  };
217  
218  /* tty magic number */
219  #define TTY_MAGIC		0x5401
220  
221  /*
222   * These bits are used in the flags field of the tty structure.
223   *
224   * So that interrupts won't be able to mess up the queues,
225   * copy_to_cooked must be atomic with respect to itself, as must
226   * tty->write.  Thus, you must use the inline functions set_bit() and
227   * clear_bit() to make things atomic.
228   */
229  #define TTY_THROTTLED 		0	/* Call unthrottle() at threshold min */
230  #define TTY_IO_ERROR 		1	/* Cause an I/O error (may be no ldisc too) */
231  #define TTY_OTHER_CLOSED 	2	/* Other side (if any) has closed */
232  #define TTY_EXCLUSIVE 		3	/* Exclusive open mode */
233  #define TTY_DO_WRITE_WAKEUP 	5	/* Call write_wakeup after queuing new */
234  #define TTY_LDISC_OPEN	 	11	/* Line discipline is open */
235  #define TTY_PTY_LOCK 		16	/* pty private */
236  #define TTY_NO_WRITE_SPLIT 	17	/* Preserve write boundaries to driver */
237  #define TTY_HUPPED 		18	/* Post driver->hangup() */
238  #define TTY_HUPPING		19	/* Hangup in progress */
239  #define TTY_LDISC_CHANGING	20	/* Change pending - non-block IO */
240  #define TTY_LDISC_HALTED	22	/* Line discipline is halted */
241  
tty_io_nonblock(struct tty_struct * tty,struct file * file)242  static inline bool tty_io_nonblock(struct tty_struct *tty, struct file *file)
243  {
244  	return file->f_flags & O_NONBLOCK ||
245  		test_bit(TTY_LDISC_CHANGING, &tty->flags);
246  }
247  
tty_io_error(struct tty_struct * tty)248  static inline bool tty_io_error(struct tty_struct *tty)
249  {
250  	return test_bit(TTY_IO_ERROR, &tty->flags);
251  }
252  
tty_throttled(struct tty_struct * tty)253  static inline bool tty_throttled(struct tty_struct *tty)
254  {
255  	return test_bit(TTY_THROTTLED, &tty->flags);
256  }
257  
258  #ifdef CONFIG_TTY
259  extern void tty_kref_put(struct tty_struct *tty);
260  extern struct pid *tty_get_pgrp(struct tty_struct *tty);
261  extern void tty_vhangup_self(void);
262  extern void disassociate_ctty(int priv);
263  extern dev_t tty_devnum(struct tty_struct *tty);
264  extern void proc_clear_tty(struct task_struct *p);
265  extern struct tty_struct *get_current_tty(void);
266  /* tty_io.c */
267  extern int __init tty_init(void);
268  extern const char *tty_name(const struct tty_struct *tty);
269  extern struct tty_struct *tty_kopen_exclusive(dev_t device);
270  extern struct tty_struct *tty_kopen_shared(dev_t device);
271  extern void tty_kclose(struct tty_struct *tty);
272  extern int tty_dev_name_to_number(const char *name, dev_t *number);
273  #else
tty_kref_put(struct tty_struct * tty)274  static inline void tty_kref_put(struct tty_struct *tty)
275  { }
tty_get_pgrp(struct tty_struct * tty)276  static inline struct pid *tty_get_pgrp(struct tty_struct *tty)
277  { return NULL; }
tty_vhangup_self(void)278  static inline void tty_vhangup_self(void)
279  { }
disassociate_ctty(int priv)280  static inline void disassociate_ctty(int priv)
281  { }
tty_devnum(struct tty_struct * tty)282  static inline dev_t tty_devnum(struct tty_struct *tty)
283  { return 0; }
proc_clear_tty(struct task_struct * p)284  static inline void proc_clear_tty(struct task_struct *p)
285  { }
get_current_tty(void)286  static inline struct tty_struct *get_current_tty(void)
287  { return NULL; }
288  /* tty_io.c */
tty_init(void)289  static inline int __init tty_init(void)
290  { return 0; }
tty_name(const struct tty_struct * tty)291  static inline const char *tty_name(const struct tty_struct *tty)
292  { return "(none)"; }
tty_kopen_exclusive(dev_t device)293  static inline struct tty_struct *tty_kopen_exclusive(dev_t device)
294  { return ERR_PTR(-ENODEV); }
tty_kclose(struct tty_struct * tty)295  static inline void tty_kclose(struct tty_struct *tty)
296  { }
tty_dev_name_to_number(const char * name,dev_t * number)297  static inline int tty_dev_name_to_number(const char *name, dev_t *number)
298  { return -ENOTSUPP; }
299  #endif
300  
301  extern struct ktermios tty_std_termios;
302  
303  extern int vcs_init(void);
304  
305  extern struct class *tty_class;
306  
307  /**
308   *	tty_kref_get		-	get a tty reference
309   *	@tty: tty device
310   *
311   *	Return a new reference to a tty object. The caller must hold
312   *	sufficient locks/counts to ensure that their existing reference cannot
313   *	go away
314   */
315  
tty_kref_get(struct tty_struct * tty)316  static inline struct tty_struct *tty_kref_get(struct tty_struct *tty)
317  {
318  	if (tty)
319  		kref_get(&tty->kref);
320  	return tty;
321  }
322  
323  extern const char *tty_driver_name(const struct tty_struct *tty);
324  extern void tty_wait_until_sent(struct tty_struct *tty, long timeout);
325  extern void stop_tty(struct tty_struct *tty);
326  extern void start_tty(struct tty_struct *tty);
327  extern void tty_write_message(struct tty_struct *tty, char *msg);
328  extern int tty_send_xchar(struct tty_struct *tty, char ch);
329  extern int tty_put_char(struct tty_struct *tty, unsigned char c);
330  extern unsigned int tty_chars_in_buffer(struct tty_struct *tty);
331  extern unsigned int tty_write_room(struct tty_struct *tty);
332  extern void tty_driver_flush_buffer(struct tty_struct *tty);
333  extern void tty_unthrottle(struct tty_struct *tty);
334  extern int tty_throttle_safe(struct tty_struct *tty);
335  extern int tty_unthrottle_safe(struct tty_struct *tty);
336  extern int tty_do_resize(struct tty_struct *tty, struct winsize *ws);
337  extern int tty_get_icount(struct tty_struct *tty,
338  			  struct serial_icounter_struct *icount);
339  extern int is_current_pgrp_orphaned(void);
340  extern void tty_hangup(struct tty_struct *tty);
341  extern void tty_vhangup(struct tty_struct *tty);
342  extern int tty_hung_up_p(struct file *filp);
343  extern void do_SAK(struct tty_struct *tty);
344  extern void __do_SAK(struct tty_struct *tty);
345  extern void no_tty(void);
346  extern speed_t tty_termios_baud_rate(struct ktermios *termios);
347  extern void tty_termios_encode_baud_rate(struct ktermios *termios,
348  						speed_t ibaud, speed_t obaud);
349  extern void tty_encode_baud_rate(struct tty_struct *tty,
350  						speed_t ibaud, speed_t obaud);
351  
352  /**
353   *	tty_get_baud_rate	-	get tty bit rates
354   *	@tty: tty to query
355   *
356   *	Returns the baud rate as an integer for this terminal. The
357   *	termios lock must be held by the caller and the terminal bit
358   *	flags may be updated.
359   *
360   *	Locking: none
361   */
tty_get_baud_rate(struct tty_struct * tty)362  static inline speed_t tty_get_baud_rate(struct tty_struct *tty)
363  {
364  	return tty_termios_baud_rate(&tty->termios);
365  }
366  
367  unsigned char tty_get_char_size(unsigned int cflag);
368  unsigned char tty_get_frame_size(unsigned int cflag);
369  
370  extern void tty_termios_copy_hw(struct ktermios *new, struct ktermios *old);
371  extern int tty_termios_hw_change(const struct ktermios *a, const struct ktermios *b);
372  extern int tty_set_termios(struct tty_struct *tty, struct ktermios *kt);
373  
374  extern void tty_wakeup(struct tty_struct *tty);
375  
376  extern int tty_mode_ioctl(struct tty_struct *tty, struct file *file,
377  			unsigned int cmd, unsigned long arg);
378  extern int tty_perform_flush(struct tty_struct *tty, unsigned long arg);
379  extern struct tty_struct *tty_init_dev(struct tty_driver *driver, int idx);
380  extern void tty_release_struct(struct tty_struct *tty, int idx);
381  extern void tty_init_termios(struct tty_struct *tty);
382  extern void tty_save_termios(struct tty_struct *tty);
383  extern int tty_standard_install(struct tty_driver *driver,
384  		struct tty_struct *tty);
385  
386  extern struct mutex tty_mutex;
387  
388  /* n_tty.c */
389  extern void n_tty_inherit_ops(struct tty_ldisc_ops *ops);
390  #ifdef CONFIG_TTY
391  extern void __init n_tty_init(void);
392  #else
n_tty_init(void)393  static inline void n_tty_init(void) { }
394  #endif
395  
396  /* tty_audit.c */
397  #ifdef CONFIG_AUDIT
398  extern void tty_audit_exit(void);
399  extern void tty_audit_fork(struct signal_struct *sig);
400  extern int tty_audit_push(void);
401  #else
tty_audit_exit(void)402  static inline void tty_audit_exit(void)
403  {
404  }
tty_audit_fork(struct signal_struct * sig)405  static inline void tty_audit_fork(struct signal_struct *sig)
406  {
407  }
tty_audit_push(void)408  static inline int tty_audit_push(void)
409  {
410  	return 0;
411  }
412  #endif
413  
414  /* tty_ioctl.c */
415  extern int n_tty_ioctl_helper(struct tty_struct *tty, struct file *file,
416  		       unsigned int cmd, unsigned long arg);
417  
418  /* vt.c */
419  
420  extern int vt_ioctl(struct tty_struct *tty,
421  		    unsigned int cmd, unsigned long arg);
422  
423  extern long vt_compat_ioctl(struct tty_struct *tty,
424  		     unsigned int cmd, unsigned long arg);
425  
426  /* tty_mutex.c */
427  /* functions for preparation of BKL removal */
428  extern void tty_lock(struct tty_struct *tty);
429  extern int  tty_lock_interruptible(struct tty_struct *tty);
430  extern void tty_unlock(struct tty_struct *tty);
431  extern void tty_lock_slave(struct tty_struct *tty);
432  extern void tty_unlock_slave(struct tty_struct *tty);
433  extern void tty_set_lock_subclass(struct tty_struct *tty);
434  
435  #endif
436