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