xref: /OK3568_Linux_fs/kernel/include/linux/tty.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun #ifndef _LINUX_TTY_H
3*4882a593Smuzhiyun #define _LINUX_TTY_H
4*4882a593Smuzhiyun 
5*4882a593Smuzhiyun #include <linux/fs.h>
6*4882a593Smuzhiyun #include <linux/major.h>
7*4882a593Smuzhiyun #include <linux/termios.h>
8*4882a593Smuzhiyun #include <linux/workqueue.h>
9*4882a593Smuzhiyun #include <linux/tty_driver.h>
10*4882a593Smuzhiyun #include <linux/tty_ldisc.h>
11*4882a593Smuzhiyun #include <linux/mutex.h>
12*4882a593Smuzhiyun #include <linux/tty_flags.h>
13*4882a593Smuzhiyun #include <linux/seq_file.h>
14*4882a593Smuzhiyun #include <uapi/linux/tty.h>
15*4882a593Smuzhiyun #include <linux/rwsem.h>
16*4882a593Smuzhiyun #include <linux/llist.h>
17*4882a593Smuzhiyun #include <linux/android_kabi.h>
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun /*
21*4882a593Smuzhiyun  * Lock subclasses for tty locks
22*4882a593Smuzhiyun  *
23*4882a593Smuzhiyun  * TTY_LOCK_NORMAL is for normal ttys and master ptys.
24*4882a593Smuzhiyun  * TTY_LOCK_SLAVE is for slave ptys only.
25*4882a593Smuzhiyun  *
26*4882a593Smuzhiyun  * Lock subclasses are necessary for handling nested locking with pty pairs.
27*4882a593Smuzhiyun  * tty locks which use nested locking:
28*4882a593Smuzhiyun  *
29*4882a593Smuzhiyun  * legacy_mutex - Nested tty locks are necessary for releasing pty pairs.
30*4882a593Smuzhiyun  *		  The stable lock order is master pty first, then slave pty.
31*4882a593Smuzhiyun  * termios_rwsem - The stable lock order is tty_buffer lock->termios_rwsem.
32*4882a593Smuzhiyun  *		   Subclassing this lock enables the slave pty to hold its
33*4882a593Smuzhiyun  *		   termios_rwsem when claiming the master tty_buffer lock.
34*4882a593Smuzhiyun  * tty_buffer lock - slave ptys can claim nested buffer lock when handling
35*4882a593Smuzhiyun  *		     signal chars. The stable lock order is slave pty, then
36*4882a593Smuzhiyun  *		     master.
37*4882a593Smuzhiyun  */
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun enum {
40*4882a593Smuzhiyun 	TTY_LOCK_NORMAL = 0,
41*4882a593Smuzhiyun 	TTY_LOCK_SLAVE,
42*4882a593Smuzhiyun };
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun /*
45*4882a593Smuzhiyun  * (Note: the *_driver.minor_start values 1, 64, 128, 192 are
46*4882a593Smuzhiyun  * hardcoded at present.)
47*4882a593Smuzhiyun  */
48*4882a593Smuzhiyun #define NR_UNIX98_PTY_DEFAULT	4096      /* Default maximum for Unix98 ptys */
49*4882a593Smuzhiyun #define NR_UNIX98_PTY_RESERVE	1024	  /* Default reserve for main devpts */
50*4882a593Smuzhiyun #define NR_UNIX98_PTY_MAX	(1 << MINORBITS) /* Absolute limit */
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun /*
53*4882a593Smuzhiyun  * This character is the same as _POSIX_VDISABLE: it cannot be used as
54*4882a593Smuzhiyun  * a c_cc[] character, but indicates that a particular special character
55*4882a593Smuzhiyun  * isn't in use (eg VINTR has no character etc)
56*4882a593Smuzhiyun  */
57*4882a593Smuzhiyun #define __DISABLED_CHAR '\0'
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun struct tty_buffer {
60*4882a593Smuzhiyun 	union {
61*4882a593Smuzhiyun 		struct tty_buffer *next;
62*4882a593Smuzhiyun 		struct llist_node free;
63*4882a593Smuzhiyun 	};
64*4882a593Smuzhiyun 	int used;
65*4882a593Smuzhiyun 	int size;
66*4882a593Smuzhiyun 	int commit;
67*4882a593Smuzhiyun 	int read;
68*4882a593Smuzhiyun 	int flags;
69*4882a593Smuzhiyun 	/* Data points here */
70*4882a593Smuzhiyun 	unsigned long data[];
71*4882a593Smuzhiyun };
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun /* Values for .flags field of tty_buffer */
74*4882a593Smuzhiyun #define TTYB_NORMAL	1	/* buffer has no flags buffer */
75*4882a593Smuzhiyun 
char_buf_ptr(struct tty_buffer * b,int ofs)76*4882a593Smuzhiyun static inline unsigned char *char_buf_ptr(struct tty_buffer *b, int ofs)
77*4882a593Smuzhiyun {
78*4882a593Smuzhiyun 	return ((unsigned char *)b->data) + ofs;
79*4882a593Smuzhiyun }
80*4882a593Smuzhiyun 
flag_buf_ptr(struct tty_buffer * b,int ofs)81*4882a593Smuzhiyun static inline char *flag_buf_ptr(struct tty_buffer *b, int ofs)
82*4882a593Smuzhiyun {
83*4882a593Smuzhiyun 	return (char *)char_buf_ptr(b, ofs) + b->size;
84*4882a593Smuzhiyun }
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun struct tty_bufhead {
87*4882a593Smuzhiyun 	struct tty_buffer *head;	/* Queue head */
88*4882a593Smuzhiyun 	struct work_struct work;
89*4882a593Smuzhiyun 	struct mutex	   lock;
90*4882a593Smuzhiyun 	atomic_t	   priority;
91*4882a593Smuzhiyun 	struct tty_buffer sentinel;
92*4882a593Smuzhiyun 	struct llist_head free;		/* Free queue head */
93*4882a593Smuzhiyun 	atomic_t	   mem_used;    /* In-use buffers excluding free list */
94*4882a593Smuzhiyun 	int		   mem_limit;
95*4882a593Smuzhiyun 	struct tty_buffer *tail;	/* Active buffer */
96*4882a593Smuzhiyun };
97*4882a593Smuzhiyun /*
98*4882a593Smuzhiyun  * When a break, frame error, or parity error happens, these codes are
99*4882a593Smuzhiyun  * stuffed into the flags buffer.
100*4882a593Smuzhiyun  */
101*4882a593Smuzhiyun #define TTY_NORMAL	0
102*4882a593Smuzhiyun #define TTY_BREAK	1
103*4882a593Smuzhiyun #define TTY_FRAME	2
104*4882a593Smuzhiyun #define TTY_PARITY	3
105*4882a593Smuzhiyun #define TTY_OVERRUN	4
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun #define INTR_CHAR(tty) ((tty)->termios.c_cc[VINTR])
108*4882a593Smuzhiyun #define QUIT_CHAR(tty) ((tty)->termios.c_cc[VQUIT])
109*4882a593Smuzhiyun #define ERASE_CHAR(tty) ((tty)->termios.c_cc[VERASE])
110*4882a593Smuzhiyun #define KILL_CHAR(tty) ((tty)->termios.c_cc[VKILL])
111*4882a593Smuzhiyun #define EOF_CHAR(tty) ((tty)->termios.c_cc[VEOF])
112*4882a593Smuzhiyun #define TIME_CHAR(tty) ((tty)->termios.c_cc[VTIME])
113*4882a593Smuzhiyun #define MIN_CHAR(tty) ((tty)->termios.c_cc[VMIN])
114*4882a593Smuzhiyun #define SWTC_CHAR(tty) ((tty)->termios.c_cc[VSWTC])
115*4882a593Smuzhiyun #define START_CHAR(tty) ((tty)->termios.c_cc[VSTART])
116*4882a593Smuzhiyun #define STOP_CHAR(tty) ((tty)->termios.c_cc[VSTOP])
117*4882a593Smuzhiyun #define SUSP_CHAR(tty) ((tty)->termios.c_cc[VSUSP])
118*4882a593Smuzhiyun #define EOL_CHAR(tty) ((tty)->termios.c_cc[VEOL])
119*4882a593Smuzhiyun #define REPRINT_CHAR(tty) ((tty)->termios.c_cc[VREPRINT])
120*4882a593Smuzhiyun #define DISCARD_CHAR(tty) ((tty)->termios.c_cc[VDISCARD])
121*4882a593Smuzhiyun #define WERASE_CHAR(tty) ((tty)->termios.c_cc[VWERASE])
122*4882a593Smuzhiyun #define LNEXT_CHAR(tty)	((tty)->termios.c_cc[VLNEXT])
123*4882a593Smuzhiyun #define EOL2_CHAR(tty) ((tty)->termios.c_cc[VEOL2])
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun #define _I_FLAG(tty, f)	((tty)->termios.c_iflag & (f))
126*4882a593Smuzhiyun #define _O_FLAG(tty, f)	((tty)->termios.c_oflag & (f))
127*4882a593Smuzhiyun #define _C_FLAG(tty, f)	((tty)->termios.c_cflag & (f))
128*4882a593Smuzhiyun #define _L_FLAG(tty, f)	((tty)->termios.c_lflag & (f))
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun #define I_IGNBRK(tty)	_I_FLAG((tty), IGNBRK)
131*4882a593Smuzhiyun #define I_BRKINT(tty)	_I_FLAG((tty), BRKINT)
132*4882a593Smuzhiyun #define I_IGNPAR(tty)	_I_FLAG((tty), IGNPAR)
133*4882a593Smuzhiyun #define I_PARMRK(tty)	_I_FLAG((tty), PARMRK)
134*4882a593Smuzhiyun #define I_INPCK(tty)	_I_FLAG((tty), INPCK)
135*4882a593Smuzhiyun #define I_ISTRIP(tty)	_I_FLAG((tty), ISTRIP)
136*4882a593Smuzhiyun #define I_INLCR(tty)	_I_FLAG((tty), INLCR)
137*4882a593Smuzhiyun #define I_IGNCR(tty)	_I_FLAG((tty), IGNCR)
138*4882a593Smuzhiyun #define I_ICRNL(tty)	_I_FLAG((tty), ICRNL)
139*4882a593Smuzhiyun #define I_IUCLC(tty)	_I_FLAG((tty), IUCLC)
140*4882a593Smuzhiyun #define I_IXON(tty)	_I_FLAG((tty), IXON)
141*4882a593Smuzhiyun #define I_IXANY(tty)	_I_FLAG((tty), IXANY)
142*4882a593Smuzhiyun #define I_IXOFF(tty)	_I_FLAG((tty), IXOFF)
143*4882a593Smuzhiyun #define I_IMAXBEL(tty)	_I_FLAG((tty), IMAXBEL)
144*4882a593Smuzhiyun #define I_IUTF8(tty)	_I_FLAG((tty), IUTF8)
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun #define O_OPOST(tty)	_O_FLAG((tty), OPOST)
147*4882a593Smuzhiyun #define O_OLCUC(tty)	_O_FLAG((tty), OLCUC)
148*4882a593Smuzhiyun #define O_ONLCR(tty)	_O_FLAG((tty), ONLCR)
149*4882a593Smuzhiyun #define O_OCRNL(tty)	_O_FLAG((tty), OCRNL)
150*4882a593Smuzhiyun #define O_ONOCR(tty)	_O_FLAG((tty), ONOCR)
151*4882a593Smuzhiyun #define O_ONLRET(tty)	_O_FLAG((tty), ONLRET)
152*4882a593Smuzhiyun #define O_OFILL(tty)	_O_FLAG((tty), OFILL)
153*4882a593Smuzhiyun #define O_OFDEL(tty)	_O_FLAG((tty), OFDEL)
154*4882a593Smuzhiyun #define O_NLDLY(tty)	_O_FLAG((tty), NLDLY)
155*4882a593Smuzhiyun #define O_CRDLY(tty)	_O_FLAG((tty), CRDLY)
156*4882a593Smuzhiyun #define O_TABDLY(tty)	_O_FLAG((tty), TABDLY)
157*4882a593Smuzhiyun #define O_BSDLY(tty)	_O_FLAG((tty), BSDLY)
158*4882a593Smuzhiyun #define O_VTDLY(tty)	_O_FLAG((tty), VTDLY)
159*4882a593Smuzhiyun #define O_FFDLY(tty)	_O_FLAG((tty), FFDLY)
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun #define C_BAUD(tty)	_C_FLAG((tty), CBAUD)
162*4882a593Smuzhiyun #define C_CSIZE(tty)	_C_FLAG((tty), CSIZE)
163*4882a593Smuzhiyun #define C_CSTOPB(tty)	_C_FLAG((tty), CSTOPB)
164*4882a593Smuzhiyun #define C_CREAD(tty)	_C_FLAG((tty), CREAD)
165*4882a593Smuzhiyun #define C_PARENB(tty)	_C_FLAG((tty), PARENB)
166*4882a593Smuzhiyun #define C_PARODD(tty)	_C_FLAG((tty), PARODD)
167*4882a593Smuzhiyun #define C_HUPCL(tty)	_C_FLAG((tty), HUPCL)
168*4882a593Smuzhiyun #define C_CLOCAL(tty)	_C_FLAG((tty), CLOCAL)
169*4882a593Smuzhiyun #define C_CIBAUD(tty)	_C_FLAG((tty), CIBAUD)
170*4882a593Smuzhiyun #define C_CRTSCTS(tty)	_C_FLAG((tty), CRTSCTS)
171*4882a593Smuzhiyun #define C_CMSPAR(tty)	_C_FLAG((tty), CMSPAR)
172*4882a593Smuzhiyun 
173*4882a593Smuzhiyun #define L_ISIG(tty)	_L_FLAG((tty), ISIG)
174*4882a593Smuzhiyun #define L_ICANON(tty)	_L_FLAG((tty), ICANON)
175*4882a593Smuzhiyun #define L_XCASE(tty)	_L_FLAG((tty), XCASE)
176*4882a593Smuzhiyun #define L_ECHO(tty)	_L_FLAG((tty), ECHO)
177*4882a593Smuzhiyun #define L_ECHOE(tty)	_L_FLAG((tty), ECHOE)
178*4882a593Smuzhiyun #define L_ECHOK(tty)	_L_FLAG((tty), ECHOK)
179*4882a593Smuzhiyun #define L_ECHONL(tty)	_L_FLAG((tty), ECHONL)
180*4882a593Smuzhiyun #define L_NOFLSH(tty)	_L_FLAG((tty), NOFLSH)
181*4882a593Smuzhiyun #define L_TOSTOP(tty)	_L_FLAG((tty), TOSTOP)
182*4882a593Smuzhiyun #define L_ECHOCTL(tty)	_L_FLAG((tty), ECHOCTL)
183*4882a593Smuzhiyun #define L_ECHOPRT(tty)	_L_FLAG((tty), ECHOPRT)
184*4882a593Smuzhiyun #define L_ECHOKE(tty)	_L_FLAG((tty), ECHOKE)
185*4882a593Smuzhiyun #define L_FLUSHO(tty)	_L_FLAG((tty), FLUSHO)
186*4882a593Smuzhiyun #define L_PENDIN(tty)	_L_FLAG((tty), PENDIN)
187*4882a593Smuzhiyun #define L_IEXTEN(tty)	_L_FLAG((tty), IEXTEN)
188*4882a593Smuzhiyun #define L_EXTPROC(tty)	_L_FLAG((tty), EXTPROC)
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun struct device;
191*4882a593Smuzhiyun struct signal_struct;
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun /*
194*4882a593Smuzhiyun  * Port level information. Each device keeps its own port level information
195*4882a593Smuzhiyun  * so provide a common structure for those ports wanting to use common support
196*4882a593Smuzhiyun  * routines.
197*4882a593Smuzhiyun  *
198*4882a593Smuzhiyun  * The tty port has a different lifetime to the tty so must be kept apart.
199*4882a593Smuzhiyun  * In addition be careful as tty -> port mappings are valid for the life
200*4882a593Smuzhiyun  * of the tty object but in many cases port -> tty mappings are valid only
201*4882a593Smuzhiyun  * until a hangup so don't use the wrong path.
202*4882a593Smuzhiyun  */
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun struct tty_port;
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun struct tty_port_operations {
207*4882a593Smuzhiyun 	/* Return 1 if the carrier is raised */
208*4882a593Smuzhiyun 	int (*carrier_raised)(struct tty_port *port);
209*4882a593Smuzhiyun 	/* Control the DTR line */
210*4882a593Smuzhiyun 	void (*dtr_rts)(struct tty_port *port, int raise);
211*4882a593Smuzhiyun 	/* Called when the last close completes or a hangup finishes
212*4882a593Smuzhiyun 	   IFF the port was initialized. Do not use to free resources. Called
213*4882a593Smuzhiyun 	   under the port mutex to serialize against activate/shutdowns */
214*4882a593Smuzhiyun 	void (*shutdown)(struct tty_port *port);
215*4882a593Smuzhiyun 	/* Called under the port mutex from tty_port_open, serialized using
216*4882a593Smuzhiyun 	   the port mutex */
217*4882a593Smuzhiyun         /* FIXME: long term getting the tty argument *out* of this would be
218*4882a593Smuzhiyun            good for consoles */
219*4882a593Smuzhiyun 	int (*activate)(struct tty_port *port, struct tty_struct *tty);
220*4882a593Smuzhiyun 	/* Called on the final put of a port */
221*4882a593Smuzhiyun 	void (*destruct)(struct tty_port *port);
222*4882a593Smuzhiyun 
223*4882a593Smuzhiyun 	ANDROID_KABI_RESERVE(1);
224*4882a593Smuzhiyun };
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun struct tty_port_client_operations {
227*4882a593Smuzhiyun 	int (*receive_buf)(struct tty_port *port, const unsigned char *, const unsigned char *, size_t);
228*4882a593Smuzhiyun 	void (*write_wakeup)(struct tty_port *port);
229*4882a593Smuzhiyun };
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun extern const struct tty_port_client_operations tty_port_default_client_ops;
232*4882a593Smuzhiyun 
233*4882a593Smuzhiyun struct tty_port {
234*4882a593Smuzhiyun 	struct tty_bufhead	buf;		/* Locked internally */
235*4882a593Smuzhiyun 	struct tty_struct	*tty;		/* Back pointer */
236*4882a593Smuzhiyun 	struct tty_struct	*itty;		/* internal back ptr */
237*4882a593Smuzhiyun 	const struct tty_port_operations *ops;	/* Port operations */
238*4882a593Smuzhiyun 	const struct tty_port_client_operations *client_ops; /* Port client operations */
239*4882a593Smuzhiyun 	spinlock_t		lock;		/* Lock protecting tty field */
240*4882a593Smuzhiyun 	int			blocked_open;	/* Waiting to open */
241*4882a593Smuzhiyun 	int			count;		/* Usage count */
242*4882a593Smuzhiyun 	wait_queue_head_t	open_wait;	/* Open waiters */
243*4882a593Smuzhiyun 	wait_queue_head_t	delta_msr_wait;	/* Modem status change */
244*4882a593Smuzhiyun 	unsigned long		flags;		/* User TTY flags ASYNC_ */
245*4882a593Smuzhiyun 	unsigned long		iflags;		/* Internal flags TTY_PORT_ */
246*4882a593Smuzhiyun 	unsigned char		console:1,	/* port is a console */
247*4882a593Smuzhiyun 				low_latency:1;	/* optional: tune for latency */
248*4882a593Smuzhiyun 	struct mutex		mutex;		/* Locking */
249*4882a593Smuzhiyun 	struct mutex		buf_mutex;	/* Buffer alloc lock */
250*4882a593Smuzhiyun 	unsigned char		*xmit_buf;	/* Optional buffer */
251*4882a593Smuzhiyun 	unsigned int		close_delay;	/* Close port delay */
252*4882a593Smuzhiyun 	unsigned int		closing_wait;	/* Delay for output */
253*4882a593Smuzhiyun 	int			drain_delay;	/* Set to zero if no pure time
254*4882a593Smuzhiyun 						   based drain is needed else
255*4882a593Smuzhiyun 						   set to size of fifo */
256*4882a593Smuzhiyun 	struct kref		kref;		/* Ref counter */
257*4882a593Smuzhiyun 	void 			*client_data;
258*4882a593Smuzhiyun 
259*4882a593Smuzhiyun 	ANDROID_KABI_RESERVE(1);
260*4882a593Smuzhiyun };
261*4882a593Smuzhiyun 
262*4882a593Smuzhiyun /* tty_port::iflags bits -- use atomic bit ops */
263*4882a593Smuzhiyun #define TTY_PORT_INITIALIZED	0	/* device is initialized */
264*4882a593Smuzhiyun #define TTY_PORT_SUSPENDED	1	/* device is suspended */
265*4882a593Smuzhiyun #define TTY_PORT_ACTIVE		2	/* device is open */
266*4882a593Smuzhiyun 
267*4882a593Smuzhiyun /*
268*4882a593Smuzhiyun  * uart drivers: use the uart_port::status field and the UPSTAT_* defines
269*4882a593Smuzhiyun  * for s/w-based flow control steering and carrier detection status
270*4882a593Smuzhiyun  */
271*4882a593Smuzhiyun #define TTY_PORT_CTS_FLOW	3	/* h/w flow control enabled */
272*4882a593Smuzhiyun #define TTY_PORT_CHECK_CD	4	/* carrier detect enabled */
273*4882a593Smuzhiyun #define TTY_PORT_KOPENED	5	/* device exclusively opened by
274*4882a593Smuzhiyun 					   kernel */
275*4882a593Smuzhiyun 
276*4882a593Smuzhiyun /*
277*4882a593Smuzhiyun  * Where all of the state associated with a tty is kept while the tty
278*4882a593Smuzhiyun  * is open.  Since the termios state should be kept even if the tty
279*4882a593Smuzhiyun  * has been closed --- for things like the baud rate, etc --- it is
280*4882a593Smuzhiyun  * not stored here, but rather a pointer to the real state is stored
281*4882a593Smuzhiyun  * here.  Possible the winsize structure should have the same
282*4882a593Smuzhiyun  * treatment, but (1) the default 80x24 is usually right and (2) it's
283*4882a593Smuzhiyun  * most often used by a windowing system, which will set the correct
284*4882a593Smuzhiyun  * size each time the window is created or resized anyway.
285*4882a593Smuzhiyun  * 						- TYT, 9/14/92
286*4882a593Smuzhiyun  */
287*4882a593Smuzhiyun 
288*4882a593Smuzhiyun struct tty_operations;
289*4882a593Smuzhiyun 
290*4882a593Smuzhiyun struct tty_struct {
291*4882a593Smuzhiyun 	int	magic;
292*4882a593Smuzhiyun 	struct kref kref;
293*4882a593Smuzhiyun 	struct device *dev;
294*4882a593Smuzhiyun 	struct tty_driver *driver;
295*4882a593Smuzhiyun 	const struct tty_operations *ops;
296*4882a593Smuzhiyun 	int index;
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun 	/* Protects ldisc changes: Lock tty not pty */
299*4882a593Smuzhiyun 	struct ld_semaphore ldisc_sem;
300*4882a593Smuzhiyun 	struct tty_ldisc *ldisc;
301*4882a593Smuzhiyun 
302*4882a593Smuzhiyun 	struct mutex atomic_write_lock;
303*4882a593Smuzhiyun 	struct mutex legacy_mutex;
304*4882a593Smuzhiyun 	struct mutex throttle_mutex;
305*4882a593Smuzhiyun 	struct rw_semaphore termios_rwsem;
306*4882a593Smuzhiyun 	struct mutex winsize_mutex;
307*4882a593Smuzhiyun 	spinlock_t ctrl_lock;
308*4882a593Smuzhiyun 	spinlock_t flow_lock;
309*4882a593Smuzhiyun 	/* Termios values are protected by the termios rwsem */
310*4882a593Smuzhiyun 	struct ktermios termios, termios_locked;
311*4882a593Smuzhiyun 
312*4882a593Smuzhiyun 	/* termiox is estored only for ABI preservation, do not use */
313*4882a593Smuzhiyun 	struct termiox *termiox;
314*4882a593Smuzhiyun 
315*4882a593Smuzhiyun 	char name[64];
316*4882a593Smuzhiyun 	struct pid *pgrp;		/* Protected by ctrl lock */
317*4882a593Smuzhiyun 	/*
318*4882a593Smuzhiyun 	 * Writes protected by both ctrl lock and legacy mutex, readers must use
319*4882a593Smuzhiyun 	 * at least one of them.
320*4882a593Smuzhiyun 	 */
321*4882a593Smuzhiyun 	struct pid *session;
322*4882a593Smuzhiyun 	unsigned long flags;
323*4882a593Smuzhiyun 	int count;
324*4882a593Smuzhiyun 	struct winsize winsize;		/* winsize_mutex */
325*4882a593Smuzhiyun 	unsigned long stopped:1,	/* flow_lock */
326*4882a593Smuzhiyun 		      flow_stopped:1,
327*4882a593Smuzhiyun 		      unused:BITS_PER_LONG - 2;
328*4882a593Smuzhiyun 	int hw_stopped;
329*4882a593Smuzhiyun 	unsigned long ctrl_status:8,	/* ctrl_lock */
330*4882a593Smuzhiyun 		      packet:1,
331*4882a593Smuzhiyun 		      unused_ctrl:BITS_PER_LONG - 9;
332*4882a593Smuzhiyun 	unsigned int receive_room;	/* Bytes free for queue */
333*4882a593Smuzhiyun 	int flow_change;
334*4882a593Smuzhiyun 
335*4882a593Smuzhiyun 	struct tty_struct *link;
336*4882a593Smuzhiyun 	struct fasync_struct *fasync;
337*4882a593Smuzhiyun 	wait_queue_head_t write_wait;
338*4882a593Smuzhiyun 	wait_queue_head_t read_wait;
339*4882a593Smuzhiyun 	struct work_struct hangup_work;
340*4882a593Smuzhiyun 	void *disc_data;
341*4882a593Smuzhiyun 	void *driver_data;
342*4882a593Smuzhiyun 	spinlock_t files_lock;		/* protects tty_files list */
343*4882a593Smuzhiyun 	struct list_head tty_files;
344*4882a593Smuzhiyun 
345*4882a593Smuzhiyun #define N_TTY_BUF_SIZE 4096
346*4882a593Smuzhiyun 
347*4882a593Smuzhiyun 	int closing;
348*4882a593Smuzhiyun 	unsigned char *write_buf;
349*4882a593Smuzhiyun 	int write_cnt;
350*4882a593Smuzhiyun 	/* If the tty has a pending do_SAK, queue it here - akpm */
351*4882a593Smuzhiyun 	struct work_struct SAK_work;
352*4882a593Smuzhiyun 	struct tty_port *port;
353*4882a593Smuzhiyun 
354*4882a593Smuzhiyun 	ANDROID_KABI_RESERVE(1);
355*4882a593Smuzhiyun 	ANDROID_KABI_RESERVE(2);
356*4882a593Smuzhiyun } __randomize_layout;
357*4882a593Smuzhiyun 
358*4882a593Smuzhiyun /* Each of a tty's open files has private_data pointing to tty_file_private */
359*4882a593Smuzhiyun struct tty_file_private {
360*4882a593Smuzhiyun 	struct tty_struct *tty;
361*4882a593Smuzhiyun 	struct file *file;
362*4882a593Smuzhiyun 	struct list_head list;
363*4882a593Smuzhiyun };
364*4882a593Smuzhiyun 
365*4882a593Smuzhiyun /* tty magic number */
366*4882a593Smuzhiyun #define TTY_MAGIC		0x5401
367*4882a593Smuzhiyun 
368*4882a593Smuzhiyun /*
369*4882a593Smuzhiyun  * These bits are used in the flags field of the tty structure.
370*4882a593Smuzhiyun  *
371*4882a593Smuzhiyun  * So that interrupts won't be able to mess up the queues,
372*4882a593Smuzhiyun  * copy_to_cooked must be atomic with respect to itself, as must
373*4882a593Smuzhiyun  * tty->write.  Thus, you must use the inline functions set_bit() and
374*4882a593Smuzhiyun  * clear_bit() to make things atomic.
375*4882a593Smuzhiyun  */
376*4882a593Smuzhiyun #define TTY_THROTTLED 		0	/* Call unthrottle() at threshold min */
377*4882a593Smuzhiyun #define TTY_IO_ERROR 		1	/* Cause an I/O error (may be no ldisc too) */
378*4882a593Smuzhiyun #define TTY_OTHER_CLOSED 	2	/* Other side (if any) has closed */
379*4882a593Smuzhiyun #define TTY_EXCLUSIVE 		3	/* Exclusive open mode */
380*4882a593Smuzhiyun #define TTY_DO_WRITE_WAKEUP 	5	/* Call write_wakeup after queuing new */
381*4882a593Smuzhiyun #define TTY_LDISC_OPEN	 	11	/* Line discipline is open */
382*4882a593Smuzhiyun #define TTY_PTY_LOCK 		16	/* pty private */
383*4882a593Smuzhiyun #define TTY_NO_WRITE_SPLIT 	17	/* Preserve write boundaries to driver */
384*4882a593Smuzhiyun #define TTY_HUPPED 		18	/* Post driver->hangup() */
385*4882a593Smuzhiyun #define TTY_HUPPING		19	/* Hangup in progress */
386*4882a593Smuzhiyun #define TTY_LDISC_CHANGING	20	/* Change pending - non-block IO */
387*4882a593Smuzhiyun #define TTY_LDISC_HALTED	22	/* Line discipline is halted */
388*4882a593Smuzhiyun 
389*4882a593Smuzhiyun /* Values for tty->flow_change */
390*4882a593Smuzhiyun #define TTY_THROTTLE_SAFE 1
391*4882a593Smuzhiyun #define TTY_UNTHROTTLE_SAFE 2
392*4882a593Smuzhiyun 
__tty_set_flow_change(struct tty_struct * tty,int val)393*4882a593Smuzhiyun static inline void __tty_set_flow_change(struct tty_struct *tty, int val)
394*4882a593Smuzhiyun {
395*4882a593Smuzhiyun 	tty->flow_change = val;
396*4882a593Smuzhiyun }
397*4882a593Smuzhiyun 
tty_set_flow_change(struct tty_struct * tty,int val)398*4882a593Smuzhiyun static inline void tty_set_flow_change(struct tty_struct *tty, int val)
399*4882a593Smuzhiyun {
400*4882a593Smuzhiyun 	tty->flow_change = val;
401*4882a593Smuzhiyun 	smp_mb();
402*4882a593Smuzhiyun }
403*4882a593Smuzhiyun 
tty_io_nonblock(struct tty_struct * tty,struct file * file)404*4882a593Smuzhiyun static inline bool tty_io_nonblock(struct tty_struct *tty, struct file *file)
405*4882a593Smuzhiyun {
406*4882a593Smuzhiyun 	return file->f_flags & O_NONBLOCK ||
407*4882a593Smuzhiyun 		test_bit(TTY_LDISC_CHANGING, &tty->flags);
408*4882a593Smuzhiyun }
409*4882a593Smuzhiyun 
tty_io_error(struct tty_struct * tty)410*4882a593Smuzhiyun static inline bool tty_io_error(struct tty_struct *tty)
411*4882a593Smuzhiyun {
412*4882a593Smuzhiyun 	return test_bit(TTY_IO_ERROR, &tty->flags);
413*4882a593Smuzhiyun }
414*4882a593Smuzhiyun 
tty_throttled(struct tty_struct * tty)415*4882a593Smuzhiyun static inline bool tty_throttled(struct tty_struct *tty)
416*4882a593Smuzhiyun {
417*4882a593Smuzhiyun 	return test_bit(TTY_THROTTLED, &tty->flags);
418*4882a593Smuzhiyun }
419*4882a593Smuzhiyun 
420*4882a593Smuzhiyun #ifdef CONFIG_TTY
421*4882a593Smuzhiyun extern void tty_kref_put(struct tty_struct *tty);
422*4882a593Smuzhiyun extern struct pid *tty_get_pgrp(struct tty_struct *tty);
423*4882a593Smuzhiyun extern void tty_vhangup_self(void);
424*4882a593Smuzhiyun extern void disassociate_ctty(int priv);
425*4882a593Smuzhiyun extern dev_t tty_devnum(struct tty_struct *tty);
426*4882a593Smuzhiyun extern void proc_clear_tty(struct task_struct *p);
427*4882a593Smuzhiyun extern struct tty_struct *get_current_tty(void);
428*4882a593Smuzhiyun /* tty_io.c */
429*4882a593Smuzhiyun extern int __init tty_init(void);
430*4882a593Smuzhiyun extern const char *tty_name(const struct tty_struct *tty);
431*4882a593Smuzhiyun extern struct tty_struct *tty_kopen(dev_t device);
432*4882a593Smuzhiyun extern void tty_kclose(struct tty_struct *tty);
433*4882a593Smuzhiyun extern int tty_dev_name_to_number(const char *name, dev_t *number);
434*4882a593Smuzhiyun extern int tty_ldisc_lock(struct tty_struct *tty, unsigned long timeout);
435*4882a593Smuzhiyun extern void tty_ldisc_unlock(struct tty_struct *tty);
436*4882a593Smuzhiyun extern ssize_t redirected_tty_write(struct kiocb *, struct iov_iter *);
437*4882a593Smuzhiyun #else
tty_kref_put(struct tty_struct * tty)438*4882a593Smuzhiyun static inline void tty_kref_put(struct tty_struct *tty)
439*4882a593Smuzhiyun { }
tty_get_pgrp(struct tty_struct * tty)440*4882a593Smuzhiyun static inline struct pid *tty_get_pgrp(struct tty_struct *tty)
441*4882a593Smuzhiyun { return NULL; }
tty_vhangup_self(void)442*4882a593Smuzhiyun static inline void tty_vhangup_self(void)
443*4882a593Smuzhiyun { }
disassociate_ctty(int priv)444*4882a593Smuzhiyun static inline void disassociate_ctty(int priv)
445*4882a593Smuzhiyun { }
tty_devnum(struct tty_struct * tty)446*4882a593Smuzhiyun static inline dev_t tty_devnum(struct tty_struct *tty)
447*4882a593Smuzhiyun { return 0; }
proc_clear_tty(struct task_struct * p)448*4882a593Smuzhiyun static inline void proc_clear_tty(struct task_struct *p)
449*4882a593Smuzhiyun { }
get_current_tty(void)450*4882a593Smuzhiyun static inline struct tty_struct *get_current_tty(void)
451*4882a593Smuzhiyun { return NULL; }
452*4882a593Smuzhiyun /* tty_io.c */
tty_init(void)453*4882a593Smuzhiyun static inline int __init tty_init(void)
454*4882a593Smuzhiyun { return 0; }
tty_name(const struct tty_struct * tty)455*4882a593Smuzhiyun static inline const char *tty_name(const struct tty_struct *tty)
456*4882a593Smuzhiyun { return "(none)"; }
tty_kopen(dev_t device)457*4882a593Smuzhiyun static inline struct tty_struct *tty_kopen(dev_t device)
458*4882a593Smuzhiyun { return ERR_PTR(-ENODEV); }
tty_kclose(struct tty_struct * tty)459*4882a593Smuzhiyun static inline void tty_kclose(struct tty_struct *tty)
460*4882a593Smuzhiyun { }
tty_dev_name_to_number(const char * name,dev_t * number)461*4882a593Smuzhiyun static inline int tty_dev_name_to_number(const char *name, dev_t *number)
462*4882a593Smuzhiyun { return -ENOTSUPP; }
463*4882a593Smuzhiyun #endif
464*4882a593Smuzhiyun 
465*4882a593Smuzhiyun extern struct ktermios tty_std_termios;
466*4882a593Smuzhiyun 
467*4882a593Smuzhiyun extern int vcs_init(void);
468*4882a593Smuzhiyun 
469*4882a593Smuzhiyun extern struct class *tty_class;
470*4882a593Smuzhiyun 
471*4882a593Smuzhiyun /**
472*4882a593Smuzhiyun  *	tty_kref_get		-	get a tty reference
473*4882a593Smuzhiyun  *	@tty: tty device
474*4882a593Smuzhiyun  *
475*4882a593Smuzhiyun  *	Return a new reference to a tty object. The caller must hold
476*4882a593Smuzhiyun  *	sufficient locks/counts to ensure that their existing reference cannot
477*4882a593Smuzhiyun  *	go away
478*4882a593Smuzhiyun  */
479*4882a593Smuzhiyun 
tty_kref_get(struct tty_struct * tty)480*4882a593Smuzhiyun static inline struct tty_struct *tty_kref_get(struct tty_struct *tty)
481*4882a593Smuzhiyun {
482*4882a593Smuzhiyun 	if (tty)
483*4882a593Smuzhiyun 		kref_get(&tty->kref);
484*4882a593Smuzhiyun 	return tty;
485*4882a593Smuzhiyun }
486*4882a593Smuzhiyun 
487*4882a593Smuzhiyun extern const char *tty_driver_name(const struct tty_struct *tty);
488*4882a593Smuzhiyun extern void tty_wait_until_sent(struct tty_struct *tty, long timeout);
489*4882a593Smuzhiyun extern int __tty_check_change(struct tty_struct *tty, int sig);
490*4882a593Smuzhiyun extern int tty_check_change(struct tty_struct *tty);
491*4882a593Smuzhiyun extern void __stop_tty(struct tty_struct *tty);
492*4882a593Smuzhiyun extern void stop_tty(struct tty_struct *tty);
493*4882a593Smuzhiyun extern void __start_tty(struct tty_struct *tty);
494*4882a593Smuzhiyun extern void start_tty(struct tty_struct *tty);
495*4882a593Smuzhiyun extern int tty_register_driver(struct tty_driver *driver);
496*4882a593Smuzhiyun extern int tty_unregister_driver(struct tty_driver *driver);
497*4882a593Smuzhiyun extern struct device *tty_register_device(struct tty_driver *driver,
498*4882a593Smuzhiyun 					  unsigned index, struct device *dev);
499*4882a593Smuzhiyun extern struct device *tty_register_device_attr(struct tty_driver *driver,
500*4882a593Smuzhiyun 				unsigned index, struct device *device,
501*4882a593Smuzhiyun 				void *drvdata,
502*4882a593Smuzhiyun 				const struct attribute_group **attr_grp);
503*4882a593Smuzhiyun extern void tty_unregister_device(struct tty_driver *driver, unsigned index);
504*4882a593Smuzhiyun extern void tty_write_message(struct tty_struct *tty, char *msg);
505*4882a593Smuzhiyun extern int tty_send_xchar(struct tty_struct *tty, char ch);
506*4882a593Smuzhiyun extern int tty_put_char(struct tty_struct *tty, unsigned char c);
507*4882a593Smuzhiyun extern int tty_chars_in_buffer(struct tty_struct *tty);
508*4882a593Smuzhiyun extern int tty_write_room(struct tty_struct *tty);
509*4882a593Smuzhiyun extern void tty_driver_flush_buffer(struct tty_struct *tty);
510*4882a593Smuzhiyun extern void tty_throttle(struct tty_struct *tty);
511*4882a593Smuzhiyun extern void tty_unthrottle(struct tty_struct *tty);
512*4882a593Smuzhiyun extern int tty_throttle_safe(struct tty_struct *tty);
513*4882a593Smuzhiyun extern int tty_unthrottle_safe(struct tty_struct *tty);
514*4882a593Smuzhiyun extern int tty_do_resize(struct tty_struct *tty, struct winsize *ws);
515*4882a593Smuzhiyun extern int is_current_pgrp_orphaned(void);
516*4882a593Smuzhiyun extern void tty_hangup(struct tty_struct *tty);
517*4882a593Smuzhiyun extern void tty_vhangup(struct tty_struct *tty);
518*4882a593Smuzhiyun extern void tty_vhangup_session(struct tty_struct *tty);
519*4882a593Smuzhiyun extern int tty_hung_up_p(struct file *filp);
520*4882a593Smuzhiyun extern void do_SAK(struct tty_struct *tty);
521*4882a593Smuzhiyun extern void __do_SAK(struct tty_struct *tty);
522*4882a593Smuzhiyun extern void tty_open_proc_set_tty(struct file *filp, struct tty_struct *tty);
523*4882a593Smuzhiyun extern int tty_signal_session_leader(struct tty_struct *tty, int exit_session);
524*4882a593Smuzhiyun extern void session_clear_tty(struct pid *session);
525*4882a593Smuzhiyun extern void no_tty(void);
526*4882a593Smuzhiyun extern void tty_buffer_free_all(struct tty_port *port);
527*4882a593Smuzhiyun extern void tty_buffer_flush(struct tty_struct *tty, struct tty_ldisc *ld);
528*4882a593Smuzhiyun extern void tty_buffer_init(struct tty_port *port);
529*4882a593Smuzhiyun extern void tty_buffer_set_lock_subclass(struct tty_port *port);
530*4882a593Smuzhiyun extern bool tty_buffer_restart_work(struct tty_port *port);
531*4882a593Smuzhiyun extern bool tty_buffer_cancel_work(struct tty_port *port);
532*4882a593Smuzhiyun extern void tty_buffer_flush_work(struct tty_port *port);
533*4882a593Smuzhiyun extern speed_t tty_termios_baud_rate(struct ktermios *termios);
534*4882a593Smuzhiyun extern speed_t tty_termios_input_baud_rate(struct ktermios *termios);
535*4882a593Smuzhiyun extern void tty_termios_encode_baud_rate(struct ktermios *termios,
536*4882a593Smuzhiyun 						speed_t ibaud, speed_t obaud);
537*4882a593Smuzhiyun extern void tty_encode_baud_rate(struct tty_struct *tty,
538*4882a593Smuzhiyun 						speed_t ibaud, speed_t obaud);
539*4882a593Smuzhiyun 
540*4882a593Smuzhiyun /**
541*4882a593Smuzhiyun  *	tty_get_baud_rate	-	get tty bit rates
542*4882a593Smuzhiyun  *	@tty: tty to query
543*4882a593Smuzhiyun  *
544*4882a593Smuzhiyun  *	Returns the baud rate as an integer for this terminal. The
545*4882a593Smuzhiyun  *	termios lock must be held by the caller and the terminal bit
546*4882a593Smuzhiyun  *	flags may be updated.
547*4882a593Smuzhiyun  *
548*4882a593Smuzhiyun  *	Locking: none
549*4882a593Smuzhiyun  */
tty_get_baud_rate(struct tty_struct * tty)550*4882a593Smuzhiyun static inline speed_t tty_get_baud_rate(struct tty_struct *tty)
551*4882a593Smuzhiyun {
552*4882a593Smuzhiyun 	return tty_termios_baud_rate(&tty->termios);
553*4882a593Smuzhiyun }
554*4882a593Smuzhiyun 
555*4882a593Smuzhiyun extern void tty_termios_copy_hw(struct ktermios *new, struct ktermios *old);
556*4882a593Smuzhiyun extern int tty_termios_hw_change(const struct ktermios *a, const struct ktermios *b);
557*4882a593Smuzhiyun extern int tty_set_termios(struct tty_struct *tty, struct ktermios *kt);
558*4882a593Smuzhiyun 
559*4882a593Smuzhiyun extern struct tty_ldisc *tty_ldisc_ref(struct tty_struct *);
560*4882a593Smuzhiyun extern void tty_ldisc_deref(struct tty_ldisc *);
561*4882a593Smuzhiyun extern struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *);
562*4882a593Smuzhiyun extern void tty_ldisc_hangup(struct tty_struct *tty, bool reset);
563*4882a593Smuzhiyun extern int tty_ldisc_reinit(struct tty_struct *tty, int disc);
564*4882a593Smuzhiyun extern const struct seq_operations tty_ldiscs_seq_ops;
565*4882a593Smuzhiyun 
566*4882a593Smuzhiyun extern void tty_wakeup(struct tty_struct *tty);
567*4882a593Smuzhiyun extern void tty_ldisc_flush(struct tty_struct *tty);
568*4882a593Smuzhiyun 
569*4882a593Smuzhiyun extern long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
570*4882a593Smuzhiyun extern int tty_mode_ioctl(struct tty_struct *tty, struct file *file,
571*4882a593Smuzhiyun 			unsigned int cmd, unsigned long arg);
572*4882a593Smuzhiyun extern long tty_jobctrl_ioctl(struct tty_struct *tty, struct tty_struct *real_tty,
573*4882a593Smuzhiyun 			      struct file *file, unsigned int cmd, unsigned long arg);
574*4882a593Smuzhiyun extern int tty_perform_flush(struct tty_struct *tty, unsigned long arg);
575*4882a593Smuzhiyun extern void tty_default_fops(struct file_operations *fops);
576*4882a593Smuzhiyun extern struct tty_struct *alloc_tty_struct(struct tty_driver *driver, int idx);
577*4882a593Smuzhiyun extern int tty_alloc_file(struct file *file);
578*4882a593Smuzhiyun extern void tty_add_file(struct tty_struct *tty, struct file *file);
579*4882a593Smuzhiyun extern void tty_free_file(struct file *file);
580*4882a593Smuzhiyun extern struct tty_struct *tty_init_dev(struct tty_driver *driver, int idx);
581*4882a593Smuzhiyun extern void tty_release_struct(struct tty_struct *tty, int idx);
582*4882a593Smuzhiyun extern int tty_release(struct inode *inode, struct file *filp);
583*4882a593Smuzhiyun extern void tty_init_termios(struct tty_struct *tty);
584*4882a593Smuzhiyun extern void tty_save_termios(struct tty_struct *tty);
585*4882a593Smuzhiyun extern int tty_standard_install(struct tty_driver *driver,
586*4882a593Smuzhiyun 		struct tty_struct *tty);
587*4882a593Smuzhiyun 
588*4882a593Smuzhiyun extern struct mutex tty_mutex;
589*4882a593Smuzhiyun 
590*4882a593Smuzhiyun #define tty_is_writelocked(tty)  (mutex_is_locked(&tty->atomic_write_lock))
591*4882a593Smuzhiyun 
592*4882a593Smuzhiyun extern void tty_port_init(struct tty_port *port);
593*4882a593Smuzhiyun extern void tty_port_link_device(struct tty_port *port,
594*4882a593Smuzhiyun 		struct tty_driver *driver, unsigned index);
595*4882a593Smuzhiyun extern struct device *tty_port_register_device(struct tty_port *port,
596*4882a593Smuzhiyun 		struct tty_driver *driver, unsigned index,
597*4882a593Smuzhiyun 		struct device *device);
598*4882a593Smuzhiyun extern struct device *tty_port_register_device_attr(struct tty_port *port,
599*4882a593Smuzhiyun 		struct tty_driver *driver, unsigned index,
600*4882a593Smuzhiyun 		struct device *device, void *drvdata,
601*4882a593Smuzhiyun 		const struct attribute_group **attr_grp);
602*4882a593Smuzhiyun extern struct device *tty_port_register_device_serdev(struct tty_port *port,
603*4882a593Smuzhiyun 		struct tty_driver *driver, unsigned index,
604*4882a593Smuzhiyun 		struct device *device);
605*4882a593Smuzhiyun extern struct device *tty_port_register_device_attr_serdev(struct tty_port *port,
606*4882a593Smuzhiyun 		struct tty_driver *driver, unsigned index,
607*4882a593Smuzhiyun 		struct device *device, void *drvdata,
608*4882a593Smuzhiyun 		const struct attribute_group **attr_grp);
609*4882a593Smuzhiyun extern void tty_port_unregister_device(struct tty_port *port,
610*4882a593Smuzhiyun 		struct tty_driver *driver, unsigned index);
611*4882a593Smuzhiyun extern int tty_port_alloc_xmit_buf(struct tty_port *port);
612*4882a593Smuzhiyun extern void tty_port_free_xmit_buf(struct tty_port *port);
613*4882a593Smuzhiyun extern void tty_port_destroy(struct tty_port *port);
614*4882a593Smuzhiyun extern void tty_port_put(struct tty_port *port);
615*4882a593Smuzhiyun 
tty_port_get(struct tty_port * port)616*4882a593Smuzhiyun static inline struct tty_port *tty_port_get(struct tty_port *port)
617*4882a593Smuzhiyun {
618*4882a593Smuzhiyun 	if (port && kref_get_unless_zero(&port->kref))
619*4882a593Smuzhiyun 		return port;
620*4882a593Smuzhiyun 	return NULL;
621*4882a593Smuzhiyun }
622*4882a593Smuzhiyun 
623*4882a593Smuzhiyun /* If the cts flow control is enabled, return true. */
tty_port_cts_enabled(struct tty_port * port)624*4882a593Smuzhiyun static inline bool tty_port_cts_enabled(struct tty_port *port)
625*4882a593Smuzhiyun {
626*4882a593Smuzhiyun 	return test_bit(TTY_PORT_CTS_FLOW, &port->iflags);
627*4882a593Smuzhiyun }
628*4882a593Smuzhiyun 
tty_port_set_cts_flow(struct tty_port * port,bool val)629*4882a593Smuzhiyun static inline void tty_port_set_cts_flow(struct tty_port *port, bool val)
630*4882a593Smuzhiyun {
631*4882a593Smuzhiyun 	if (val)
632*4882a593Smuzhiyun 		set_bit(TTY_PORT_CTS_FLOW, &port->iflags);
633*4882a593Smuzhiyun 	else
634*4882a593Smuzhiyun 		clear_bit(TTY_PORT_CTS_FLOW, &port->iflags);
635*4882a593Smuzhiyun }
636*4882a593Smuzhiyun 
tty_port_active(struct tty_port * port)637*4882a593Smuzhiyun static inline bool tty_port_active(struct tty_port *port)
638*4882a593Smuzhiyun {
639*4882a593Smuzhiyun 	return test_bit(TTY_PORT_ACTIVE, &port->iflags);
640*4882a593Smuzhiyun }
641*4882a593Smuzhiyun 
tty_port_set_active(struct tty_port * port,bool val)642*4882a593Smuzhiyun static inline void tty_port_set_active(struct tty_port *port, bool val)
643*4882a593Smuzhiyun {
644*4882a593Smuzhiyun 	if (val)
645*4882a593Smuzhiyun 		set_bit(TTY_PORT_ACTIVE, &port->iflags);
646*4882a593Smuzhiyun 	else
647*4882a593Smuzhiyun 		clear_bit(TTY_PORT_ACTIVE, &port->iflags);
648*4882a593Smuzhiyun }
649*4882a593Smuzhiyun 
tty_port_check_carrier(struct tty_port * port)650*4882a593Smuzhiyun static inline bool tty_port_check_carrier(struct tty_port *port)
651*4882a593Smuzhiyun {
652*4882a593Smuzhiyun 	return test_bit(TTY_PORT_CHECK_CD, &port->iflags);
653*4882a593Smuzhiyun }
654*4882a593Smuzhiyun 
tty_port_set_check_carrier(struct tty_port * port,bool val)655*4882a593Smuzhiyun static inline void tty_port_set_check_carrier(struct tty_port *port, bool val)
656*4882a593Smuzhiyun {
657*4882a593Smuzhiyun 	if (val)
658*4882a593Smuzhiyun 		set_bit(TTY_PORT_CHECK_CD, &port->iflags);
659*4882a593Smuzhiyun 	else
660*4882a593Smuzhiyun 		clear_bit(TTY_PORT_CHECK_CD, &port->iflags);
661*4882a593Smuzhiyun }
662*4882a593Smuzhiyun 
tty_port_suspended(struct tty_port * port)663*4882a593Smuzhiyun static inline bool tty_port_suspended(struct tty_port *port)
664*4882a593Smuzhiyun {
665*4882a593Smuzhiyun 	return test_bit(TTY_PORT_SUSPENDED, &port->iflags);
666*4882a593Smuzhiyun }
667*4882a593Smuzhiyun 
tty_port_set_suspended(struct tty_port * port,bool val)668*4882a593Smuzhiyun static inline void tty_port_set_suspended(struct tty_port *port, bool val)
669*4882a593Smuzhiyun {
670*4882a593Smuzhiyun 	if (val)
671*4882a593Smuzhiyun 		set_bit(TTY_PORT_SUSPENDED, &port->iflags);
672*4882a593Smuzhiyun 	else
673*4882a593Smuzhiyun 		clear_bit(TTY_PORT_SUSPENDED, &port->iflags);
674*4882a593Smuzhiyun }
675*4882a593Smuzhiyun 
tty_port_initialized(struct tty_port * port)676*4882a593Smuzhiyun static inline bool tty_port_initialized(struct tty_port *port)
677*4882a593Smuzhiyun {
678*4882a593Smuzhiyun 	return test_bit(TTY_PORT_INITIALIZED, &port->iflags);
679*4882a593Smuzhiyun }
680*4882a593Smuzhiyun 
tty_port_set_initialized(struct tty_port * port,bool val)681*4882a593Smuzhiyun static inline void tty_port_set_initialized(struct tty_port *port, bool val)
682*4882a593Smuzhiyun {
683*4882a593Smuzhiyun 	if (val)
684*4882a593Smuzhiyun 		set_bit(TTY_PORT_INITIALIZED, &port->iflags);
685*4882a593Smuzhiyun 	else
686*4882a593Smuzhiyun 		clear_bit(TTY_PORT_INITIALIZED, &port->iflags);
687*4882a593Smuzhiyun }
688*4882a593Smuzhiyun 
tty_port_kopened(struct tty_port * port)689*4882a593Smuzhiyun static inline bool tty_port_kopened(struct tty_port *port)
690*4882a593Smuzhiyun {
691*4882a593Smuzhiyun 	return test_bit(TTY_PORT_KOPENED, &port->iflags);
692*4882a593Smuzhiyun }
693*4882a593Smuzhiyun 
tty_port_set_kopened(struct tty_port * port,bool val)694*4882a593Smuzhiyun static inline void tty_port_set_kopened(struct tty_port *port, bool val)
695*4882a593Smuzhiyun {
696*4882a593Smuzhiyun 	if (val)
697*4882a593Smuzhiyun 		set_bit(TTY_PORT_KOPENED, &port->iflags);
698*4882a593Smuzhiyun 	else
699*4882a593Smuzhiyun 		clear_bit(TTY_PORT_KOPENED, &port->iflags);
700*4882a593Smuzhiyun }
701*4882a593Smuzhiyun 
702*4882a593Smuzhiyun extern struct tty_struct *tty_port_tty_get(struct tty_port *port);
703*4882a593Smuzhiyun extern void tty_port_tty_set(struct tty_port *port, struct tty_struct *tty);
704*4882a593Smuzhiyun extern int tty_port_carrier_raised(struct tty_port *port);
705*4882a593Smuzhiyun extern void tty_port_raise_dtr_rts(struct tty_port *port);
706*4882a593Smuzhiyun extern void tty_port_lower_dtr_rts(struct tty_port *port);
707*4882a593Smuzhiyun extern void tty_port_hangup(struct tty_port *port);
708*4882a593Smuzhiyun extern void tty_port_tty_hangup(struct tty_port *port, bool check_clocal);
709*4882a593Smuzhiyun extern void tty_port_tty_wakeup(struct tty_port *port);
710*4882a593Smuzhiyun extern int tty_port_block_til_ready(struct tty_port *port,
711*4882a593Smuzhiyun 				struct tty_struct *tty, struct file *filp);
712*4882a593Smuzhiyun extern int tty_port_close_start(struct tty_port *port,
713*4882a593Smuzhiyun 				struct tty_struct *tty, struct file *filp);
714*4882a593Smuzhiyun extern void tty_port_close_end(struct tty_port *port, struct tty_struct *tty);
715*4882a593Smuzhiyun extern void tty_port_close(struct tty_port *port,
716*4882a593Smuzhiyun 				struct tty_struct *tty, struct file *filp);
717*4882a593Smuzhiyun extern int tty_port_install(struct tty_port *port, struct tty_driver *driver,
718*4882a593Smuzhiyun 				struct tty_struct *tty);
719*4882a593Smuzhiyun extern int tty_port_open(struct tty_port *port,
720*4882a593Smuzhiyun 				struct tty_struct *tty, struct file *filp);
tty_port_users(struct tty_port * port)721*4882a593Smuzhiyun static inline int tty_port_users(struct tty_port *port)
722*4882a593Smuzhiyun {
723*4882a593Smuzhiyun 	return port->count + port->blocked_open;
724*4882a593Smuzhiyun }
725*4882a593Smuzhiyun 
726*4882a593Smuzhiyun extern int tty_register_ldisc(int disc, struct tty_ldisc_ops *new_ldisc);
727*4882a593Smuzhiyun extern int tty_unregister_ldisc(int disc);
728*4882a593Smuzhiyun extern int tty_set_ldisc(struct tty_struct *tty, int disc);
729*4882a593Smuzhiyun extern int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty);
730*4882a593Smuzhiyun extern void tty_ldisc_release(struct tty_struct *tty);
731*4882a593Smuzhiyun extern int __must_check tty_ldisc_init(struct tty_struct *tty);
732*4882a593Smuzhiyun extern void tty_ldisc_deinit(struct tty_struct *tty);
733*4882a593Smuzhiyun extern int tty_ldisc_receive_buf(struct tty_ldisc *ld, const unsigned char *p,
734*4882a593Smuzhiyun 				 char *f, int count);
735*4882a593Smuzhiyun 
736*4882a593Smuzhiyun /* n_tty.c */
737*4882a593Smuzhiyun extern void n_tty_inherit_ops(struct tty_ldisc_ops *ops);
738*4882a593Smuzhiyun #ifdef CONFIG_TTY
739*4882a593Smuzhiyun extern void __init n_tty_init(void);
740*4882a593Smuzhiyun #else
n_tty_init(void)741*4882a593Smuzhiyun static inline void n_tty_init(void) { }
742*4882a593Smuzhiyun #endif
743*4882a593Smuzhiyun 
744*4882a593Smuzhiyun /* tty_audit.c */
745*4882a593Smuzhiyun #ifdef CONFIG_AUDIT
746*4882a593Smuzhiyun extern void tty_audit_add_data(struct tty_struct *tty, const void *data,
747*4882a593Smuzhiyun 			       size_t size);
748*4882a593Smuzhiyun extern void tty_audit_exit(void);
749*4882a593Smuzhiyun extern void tty_audit_fork(struct signal_struct *sig);
750*4882a593Smuzhiyun extern void tty_audit_tiocsti(struct tty_struct *tty, char ch);
751*4882a593Smuzhiyun extern int tty_audit_push(void);
752*4882a593Smuzhiyun #else
tty_audit_add_data(struct tty_struct * tty,const void * data,size_t size)753*4882a593Smuzhiyun static inline void tty_audit_add_data(struct tty_struct *tty, const void *data,
754*4882a593Smuzhiyun 				      size_t size)
755*4882a593Smuzhiyun {
756*4882a593Smuzhiyun }
tty_audit_tiocsti(struct tty_struct * tty,char ch)757*4882a593Smuzhiyun static inline void tty_audit_tiocsti(struct tty_struct *tty, char ch)
758*4882a593Smuzhiyun {
759*4882a593Smuzhiyun }
tty_audit_exit(void)760*4882a593Smuzhiyun static inline void tty_audit_exit(void)
761*4882a593Smuzhiyun {
762*4882a593Smuzhiyun }
tty_audit_fork(struct signal_struct * sig)763*4882a593Smuzhiyun static inline void tty_audit_fork(struct signal_struct *sig)
764*4882a593Smuzhiyun {
765*4882a593Smuzhiyun }
tty_audit_push(void)766*4882a593Smuzhiyun static inline int tty_audit_push(void)
767*4882a593Smuzhiyun {
768*4882a593Smuzhiyun 	return 0;
769*4882a593Smuzhiyun }
770*4882a593Smuzhiyun #endif
771*4882a593Smuzhiyun 
772*4882a593Smuzhiyun /* tty_ioctl.c */
773*4882a593Smuzhiyun extern int n_tty_ioctl_helper(struct tty_struct *tty, struct file *file,
774*4882a593Smuzhiyun 		       unsigned int cmd, unsigned long arg);
775*4882a593Smuzhiyun 
776*4882a593Smuzhiyun /* vt.c */
777*4882a593Smuzhiyun 
778*4882a593Smuzhiyun extern int vt_ioctl(struct tty_struct *tty,
779*4882a593Smuzhiyun 		    unsigned int cmd, unsigned long arg);
780*4882a593Smuzhiyun 
781*4882a593Smuzhiyun extern long vt_compat_ioctl(struct tty_struct *tty,
782*4882a593Smuzhiyun 		     unsigned int cmd, unsigned long arg);
783*4882a593Smuzhiyun 
784*4882a593Smuzhiyun /* tty_mutex.c */
785*4882a593Smuzhiyun /* functions for preparation of BKL removal */
786*4882a593Smuzhiyun extern void tty_lock(struct tty_struct *tty);
787*4882a593Smuzhiyun extern int  tty_lock_interruptible(struct tty_struct *tty);
788*4882a593Smuzhiyun extern void tty_unlock(struct tty_struct *tty);
789*4882a593Smuzhiyun extern void tty_lock_slave(struct tty_struct *tty);
790*4882a593Smuzhiyun extern void tty_unlock_slave(struct tty_struct *tty);
791*4882a593Smuzhiyun extern void tty_set_lock_subclass(struct tty_struct *tty);
792*4882a593Smuzhiyun 
793*4882a593Smuzhiyun #ifdef CONFIG_PROC_FS
794*4882a593Smuzhiyun extern void proc_tty_register_driver(struct tty_driver *);
795*4882a593Smuzhiyun extern void proc_tty_unregister_driver(struct tty_driver *);
796*4882a593Smuzhiyun #else
proc_tty_register_driver(struct tty_driver * d)797*4882a593Smuzhiyun static inline void proc_tty_register_driver(struct tty_driver *d) {}
proc_tty_unregister_driver(struct tty_driver * d)798*4882a593Smuzhiyun static inline void proc_tty_unregister_driver(struct tty_driver *d) {}
799*4882a593Smuzhiyun #endif
800*4882a593Smuzhiyun 
801*4882a593Smuzhiyun #define tty_msg(fn, tty, f, ...) \
802*4882a593Smuzhiyun 	fn("%s %s: " f, tty_driver_name(tty), tty_name(tty), ##__VA_ARGS__)
803*4882a593Smuzhiyun 
804*4882a593Smuzhiyun #define tty_debug(tty, f, ...)	tty_msg(pr_debug, tty, f, ##__VA_ARGS__)
805*4882a593Smuzhiyun #define tty_info(tty, f, ...)	tty_msg(pr_info, tty, f, ##__VA_ARGS__)
806*4882a593Smuzhiyun #define tty_notice(tty, f, ...)	tty_msg(pr_notice, tty, f, ##__VA_ARGS__)
807*4882a593Smuzhiyun #define tty_warn(tty, f, ...)	tty_msg(pr_warn, tty, f, ##__VA_ARGS__)
808*4882a593Smuzhiyun #define tty_err(tty, f, ...)	tty_msg(pr_err, tty, f, ##__VA_ARGS__)
809*4882a593Smuzhiyun 
810*4882a593Smuzhiyun #define tty_info_ratelimited(tty, f, ...) \
811*4882a593Smuzhiyun 		tty_msg(pr_info_ratelimited, tty, f, ##__VA_ARGS__)
812*4882a593Smuzhiyun 
813*4882a593Smuzhiyun #endif
814