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