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