Merge tag 'scsi-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi
[linux-2.6-block.git] / include / linux / tty.h
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/tty_port.h>
12 #include <linux/mutex.h>
13 #include <linux/tty_flags.h>
14 #include <uapi/linux/tty.h>
15 #include <linux/rwsem.h>
16 #include <linux/llist.h>
17
18
19 /*
20  * (Note: the *_driver.minor_start values 1, 64, 128, 192 are
21  * hardcoded at present.)
22  */
23 #define NR_UNIX98_PTY_DEFAULT   4096      /* Default maximum for Unix98 ptys */
24 #define NR_UNIX98_PTY_RESERVE   1024      /* Default reserve for main devpts */
25 #define NR_UNIX98_PTY_MAX       (1 << MINORBITS) /* Absolute limit */
26
27 /*
28  * This character is the same as _POSIX_VDISABLE: it cannot be used as
29  * a c_cc[] character, but indicates that a particular special character
30  * isn't in use (eg VINTR has no character etc)
31  */
32 #define __DISABLED_CHAR '\0'
33
34 #define INTR_CHAR(tty) ((tty)->termios.c_cc[VINTR])
35 #define QUIT_CHAR(tty) ((tty)->termios.c_cc[VQUIT])
36 #define ERASE_CHAR(tty) ((tty)->termios.c_cc[VERASE])
37 #define KILL_CHAR(tty) ((tty)->termios.c_cc[VKILL])
38 #define EOF_CHAR(tty) ((tty)->termios.c_cc[VEOF])
39 #define TIME_CHAR(tty) ((tty)->termios.c_cc[VTIME])
40 #define MIN_CHAR(tty) ((tty)->termios.c_cc[VMIN])
41 #define SWTC_CHAR(tty) ((tty)->termios.c_cc[VSWTC])
42 #define START_CHAR(tty) ((tty)->termios.c_cc[VSTART])
43 #define STOP_CHAR(tty) ((tty)->termios.c_cc[VSTOP])
44 #define SUSP_CHAR(tty) ((tty)->termios.c_cc[VSUSP])
45 #define EOL_CHAR(tty) ((tty)->termios.c_cc[VEOL])
46 #define REPRINT_CHAR(tty) ((tty)->termios.c_cc[VREPRINT])
47 #define DISCARD_CHAR(tty) ((tty)->termios.c_cc[VDISCARD])
48 #define WERASE_CHAR(tty) ((tty)->termios.c_cc[VWERASE])
49 #define LNEXT_CHAR(tty) ((tty)->termios.c_cc[VLNEXT])
50 #define EOL2_CHAR(tty) ((tty)->termios.c_cc[VEOL2])
51
52 #define _I_FLAG(tty, f) ((tty)->termios.c_iflag & (f))
53 #define _O_FLAG(tty, f) ((tty)->termios.c_oflag & (f))
54 #define _C_FLAG(tty, f) ((tty)->termios.c_cflag & (f))
55 #define _L_FLAG(tty, f) ((tty)->termios.c_lflag & (f))
56
57 #define I_IGNBRK(tty)   _I_FLAG((tty), IGNBRK)
58 #define I_BRKINT(tty)   _I_FLAG((tty), BRKINT)
59 #define I_IGNPAR(tty)   _I_FLAG((tty), IGNPAR)
60 #define I_PARMRK(tty)   _I_FLAG((tty), PARMRK)
61 #define I_INPCK(tty)    _I_FLAG((tty), INPCK)
62 #define I_ISTRIP(tty)   _I_FLAG((tty), ISTRIP)
63 #define I_INLCR(tty)    _I_FLAG((tty), INLCR)
64 #define I_IGNCR(tty)    _I_FLAG((tty), IGNCR)
65 #define I_ICRNL(tty)    _I_FLAG((tty), ICRNL)
66 #define I_IUCLC(tty)    _I_FLAG((tty), IUCLC)
67 #define I_IXON(tty)     _I_FLAG((tty), IXON)
68 #define I_IXANY(tty)    _I_FLAG((tty), IXANY)
69 #define I_IXOFF(tty)    _I_FLAG((tty), IXOFF)
70 #define I_IMAXBEL(tty)  _I_FLAG((tty), IMAXBEL)
71 #define I_IUTF8(tty)    _I_FLAG((tty), IUTF8)
72
73 #define O_OPOST(tty)    _O_FLAG((tty), OPOST)
74 #define O_OLCUC(tty)    _O_FLAG((tty), OLCUC)
75 #define O_ONLCR(tty)    _O_FLAG((tty), ONLCR)
76 #define O_OCRNL(tty)    _O_FLAG((tty), OCRNL)
77 #define O_ONOCR(tty)    _O_FLAG((tty), ONOCR)
78 #define O_ONLRET(tty)   _O_FLAG((tty), ONLRET)
79 #define O_OFILL(tty)    _O_FLAG((tty), OFILL)
80 #define O_OFDEL(tty)    _O_FLAG((tty), OFDEL)
81 #define O_NLDLY(tty)    _O_FLAG((tty), NLDLY)
82 #define O_CRDLY(tty)    _O_FLAG((tty), CRDLY)
83 #define O_TABDLY(tty)   _O_FLAG((tty), TABDLY)
84 #define O_BSDLY(tty)    _O_FLAG((tty), BSDLY)
85 #define O_VTDLY(tty)    _O_FLAG((tty), VTDLY)
86 #define O_FFDLY(tty)    _O_FLAG((tty), FFDLY)
87
88 #define C_BAUD(tty)     _C_FLAG((tty), CBAUD)
89 #define C_CSIZE(tty)    _C_FLAG((tty), CSIZE)
90 #define C_CSTOPB(tty)   _C_FLAG((tty), CSTOPB)
91 #define C_CREAD(tty)    _C_FLAG((tty), CREAD)
92 #define C_PARENB(tty)   _C_FLAG((tty), PARENB)
93 #define C_PARODD(tty)   _C_FLAG((tty), PARODD)
94 #define C_HUPCL(tty)    _C_FLAG((tty), HUPCL)
95 #define C_CLOCAL(tty)   _C_FLAG((tty), CLOCAL)
96 #define C_CIBAUD(tty)   _C_FLAG((tty), CIBAUD)
97 #define C_CRTSCTS(tty)  _C_FLAG((tty), CRTSCTS)
98 #define C_CMSPAR(tty)   _C_FLAG((tty), CMSPAR)
99
100 #define L_ISIG(tty)     _L_FLAG((tty), ISIG)
101 #define L_ICANON(tty)   _L_FLAG((tty), ICANON)
102 #define L_XCASE(tty)    _L_FLAG((tty), XCASE)
103 #define L_ECHO(tty)     _L_FLAG((tty), ECHO)
104 #define L_ECHOE(tty)    _L_FLAG((tty), ECHOE)
105 #define L_ECHOK(tty)    _L_FLAG((tty), ECHOK)
106 #define L_ECHONL(tty)   _L_FLAG((tty), ECHONL)
107 #define L_NOFLSH(tty)   _L_FLAG((tty), NOFLSH)
108 #define L_TOSTOP(tty)   _L_FLAG((tty), TOSTOP)
109 #define L_ECHOCTL(tty)  _L_FLAG((tty), ECHOCTL)
110 #define L_ECHOPRT(tty)  _L_FLAG((tty), ECHOPRT)
111 #define L_ECHOKE(tty)   _L_FLAG((tty), ECHOKE)
112 #define L_FLUSHO(tty)   _L_FLAG((tty), FLUSHO)
113 #define L_PENDIN(tty)   _L_FLAG((tty), PENDIN)
114 #define L_IEXTEN(tty)   _L_FLAG((tty), IEXTEN)
115 #define L_EXTPROC(tty)  _L_FLAG((tty), EXTPROC)
116
117 struct device;
118 struct signal_struct;
119 struct tty_operations;
120
121 /**
122  * struct tty_struct - state associated with a tty while open
123  *
124  * @kref: reference counting by tty_kref_get() and tty_kref_put(), reaching zero
125  *        frees the structure
126  * @dev: class device or %NULL (e.g. ptys, serdev)
127  * @driver: &struct tty_driver operating this tty
128  * @ops: &struct tty_operations of @driver for this tty (open, close, etc.)
129  * @index: index of this tty (e.g. to construct @name like tty12)
130  * @ldisc_sem: protects line discipline changes (@ldisc) -- lock tty not pty
131  * @ldisc: the current line discipline for this tty (n_tty by default)
132  * @atomic_write_lock: protects against concurrent writers, i.e. locks
133  *                     @write_cnt, @write_buf and similar
134  * @legacy_mutex: leftover from history (BKL -> BTM -> @legacy_mutex),
135  *                protecting several operations on this tty
136  * @throttle_mutex: protects against concurrent tty_throttle_safe() and
137  *                  tty_unthrottle_safe() (but not tty_unthrottle())
138  * @termios_rwsem: protects @termios and @termios_locked
139  * @winsize_mutex: protects @winsize
140  * @termios: termios for the current tty, copied from/to @driver.termios
141  * @termios_locked: locked termios (by %TIOCGLCKTRMIOS and %TIOCSLCKTRMIOS
142  *                  ioctls)
143  * @name: name of the tty constructed by tty_line_name() (e.g. ttyS3)
144  * @flags: bitwise OR of %TTY_THROTTLED, %TTY_IO_ERROR, ...
145  * @count: count of open processes, reaching zero cancels all the work for
146  *         this tty and drops a @kref too (but does not free this tty)
147  * @winsize: size of the terminal "window" (cf. @winsize_mutex)
148  * @flow: flow settings grouped together
149  * @flow.lock: lock for @flow members
150  * @flow.stopped: tty stopped/started by stop_tty()/start_tty()
151  * @flow.tco_stopped: tty stopped/started by %TCOOFF/%TCOON ioctls (it has
152  *                    precedence over @flow.stopped)
153  * @ctrl: control settings grouped together
154  * @ctrl.lock: lock for @ctrl members
155  * @ctrl.pgrp: process group of this tty (setpgrp(2))
156  * @ctrl.session: session of this tty (setsid(2)). Writes are protected by both
157  *                @ctrl.lock and @legacy_mutex, readers must use at least one of
158  *                them.
159  * @ctrl.pktstatus: packet mode status (bitwise OR of %TIOCPKT_ constants)
160  * @ctrl.packet: packet mode enabled
161  * @hw_stopped: not controlled by the tty layer, under @driver's control for CTS
162  *              handling
163  * @receive_room: bytes permitted to feed to @ldisc without any being lost
164  * @flow_change: controls behavior of throttling, see tty_throttle_safe() and
165  *               tty_unthrottle_safe()
166  * @link: link to another pty (master -> slave and vice versa)
167  * @fasync: state for %O_ASYNC (for %SIGIO); managed by fasync_helper()
168  * @write_wait: concurrent writers are waiting in this queue until they are
169  *              allowed to write
170  * @read_wait: readers wait for data in this queue
171  * @hangup_work: normally a work to perform a hangup (do_tty_hangup()); while
172  *               freeing the tty, (re)used to release_one_tty()
173  * @disc_data: pointer to @ldisc's private data (e.g. to &struct n_tty_data)
174  * @driver_data: pointer to @driver's private data (e.g. &struct uart_state)
175  * @files_lock: protects @tty_files list
176  * @tty_files: list of (re)openers of this tty (i.e. linked &struct
177  *             tty_file_private)
178  * @closing: when set during close, n_tty processes only START & STOP chars
179  * @write_buf: temporary buffer used during tty_write() to copy user data to
180  * @write_cnt: count of bytes written in tty_write() to @write_buf
181  * @SAK_work: if the tty has a pending do_SAK, it is queued here
182  * @port: persistent storage for this device (i.e. &struct tty_port)
183  *
184  * All of the state associated with a tty while the tty is open. Persistent
185  * storage for tty devices is referenced here as @port and is documented in
186  * &struct tty_port.
187  */
188 struct tty_struct {
189         struct kref kref;
190         int index;
191         struct device *dev;
192         struct tty_driver *driver;
193         struct tty_port *port;
194         const struct tty_operations *ops;
195
196         struct tty_ldisc *ldisc;
197         struct ld_semaphore ldisc_sem;
198
199         struct mutex atomic_write_lock;
200         struct mutex legacy_mutex;
201         struct mutex throttle_mutex;
202         struct rw_semaphore termios_rwsem;
203         struct mutex winsize_mutex;
204         struct ktermios termios, termios_locked;
205         char name[64];
206         unsigned long flags;
207         int count;
208         unsigned int receive_room;
209         struct winsize winsize;
210
211         struct {
212                 spinlock_t lock;
213                 bool stopped;
214                 bool tco_stopped;
215         } flow;
216
217         struct {
218                 struct pid *pgrp;
219                 struct pid *session;
220                 spinlock_t lock;
221                 unsigned char pktstatus;
222                 bool packet;
223         } ctrl;
224
225         bool hw_stopped;
226         bool closing;
227         int flow_change;
228
229         struct tty_struct *link;
230         struct fasync_struct *fasync;
231         wait_queue_head_t write_wait;
232         wait_queue_head_t read_wait;
233         struct work_struct hangup_work;
234         void *disc_data;
235         void *driver_data;
236         spinlock_t files_lock;
237         int write_cnt;
238         u8 *write_buf;
239
240         struct list_head tty_files;
241
242 #define N_TTY_BUF_SIZE 4096
243         struct work_struct SAK_work;
244 } __randomize_layout;
245
246 /* Each of a tty's open files has private_data pointing to tty_file_private */
247 struct tty_file_private {
248         struct tty_struct *tty;
249         struct file *file;
250         struct list_head list;
251 };
252
253 /**
254  * DOC: TTY Struct Flags
255  *
256  * These bits are used in the :c:member:`tty_struct.flags` field.
257  *
258  * So that interrupts won't be able to mess up the queues,
259  * copy_to_cooked must be atomic with respect to itself, as must
260  * tty->write.  Thus, you must use the inline functions set_bit() and
261  * clear_bit() to make things atomic.
262  *
263  * TTY_THROTTLED
264  *      Driver input is throttled. The ldisc should call
265  *      :c:member:`tty_driver.unthrottle()` in order to resume reception when
266  *      it is ready to process more data (at threshold min).
267  *
268  * TTY_IO_ERROR
269  *      If set, causes all subsequent userspace read/write calls on the tty to
270  *      fail, returning -%EIO. (May be no ldisc too.)
271  *
272  * TTY_OTHER_CLOSED
273  *      Device is a pty and the other side has closed.
274  *
275  * TTY_EXCLUSIVE
276  *      Exclusive open mode (a single opener).
277  *
278  * TTY_DO_WRITE_WAKEUP
279  *      If set, causes the driver to call the
280  *      :c:member:`tty_ldisc_ops.write_wakeup()` method in order to resume
281  *      transmission when it can accept more data to transmit.
282  *
283  * TTY_LDISC_OPEN
284  *      Indicates that a line discipline is open. For debugging purposes only.
285  *
286  * TTY_PTY_LOCK
287  *      A flag private to pty code to implement %TIOCSPTLCK/%TIOCGPTLCK logic.
288  *
289  * TTY_NO_WRITE_SPLIT
290  *      Prevent driver from splitting up writes into smaller chunks (preserve
291  *      write boundaries to driver).
292  *
293  * TTY_HUPPED
294  *      The TTY was hung up. This is set post :c:member:`tty_driver.hangup()`.
295  *
296  * TTY_HUPPING
297  *      The TTY is in the process of hanging up to abort potential readers.
298  *
299  * TTY_LDISC_CHANGING
300  *      Line discipline for this TTY is being changed. I/O should not block
301  *      when this is set. Use tty_io_nonblock() to check.
302  *
303  * TTY_LDISC_HALTED
304  *      Line discipline for this TTY was stopped. No work should be queued to
305  *      this ldisc.
306  */
307 #define TTY_THROTTLED           0
308 #define TTY_IO_ERROR            1
309 #define TTY_OTHER_CLOSED        2
310 #define TTY_EXCLUSIVE           3
311 #define TTY_DO_WRITE_WAKEUP     5
312 #define TTY_LDISC_OPEN          11
313 #define TTY_PTY_LOCK            16
314 #define TTY_NO_WRITE_SPLIT      17
315 #define TTY_HUPPED              18
316 #define TTY_HUPPING             19
317 #define TTY_LDISC_CHANGING      20
318 #define TTY_LDISC_HALTED        22
319
320 static inline bool tty_io_nonblock(struct tty_struct *tty, struct file *file)
321 {
322         return file->f_flags & O_NONBLOCK ||
323                 test_bit(TTY_LDISC_CHANGING, &tty->flags);
324 }
325
326 static inline bool tty_io_error(struct tty_struct *tty)
327 {
328         return test_bit(TTY_IO_ERROR, &tty->flags);
329 }
330
331 static inline bool tty_throttled(struct tty_struct *tty)
332 {
333         return test_bit(TTY_THROTTLED, &tty->flags);
334 }
335
336 #ifdef CONFIG_TTY
337 void tty_kref_put(struct tty_struct *tty);
338 struct pid *tty_get_pgrp(struct tty_struct *tty);
339 void tty_vhangup_self(void);
340 void disassociate_ctty(int priv);
341 dev_t tty_devnum(struct tty_struct *tty);
342 void proc_clear_tty(struct task_struct *p);
343 struct tty_struct *get_current_tty(void);
344 /* tty_io.c */
345 int __init tty_init(void);
346 const char *tty_name(const struct tty_struct *tty);
347 struct tty_struct *tty_kopen_exclusive(dev_t device);
348 struct tty_struct *tty_kopen_shared(dev_t device);
349 void tty_kclose(struct tty_struct *tty);
350 int tty_dev_name_to_number(const char *name, dev_t *number);
351 #else
352 static inline void tty_kref_put(struct tty_struct *tty)
353 { }
354 static inline struct pid *tty_get_pgrp(struct tty_struct *tty)
355 { return NULL; }
356 static inline void tty_vhangup_self(void)
357 { }
358 static inline void disassociate_ctty(int priv)
359 { }
360 static inline dev_t tty_devnum(struct tty_struct *tty)
361 { return 0; }
362 static inline void proc_clear_tty(struct task_struct *p)
363 { }
364 static inline struct tty_struct *get_current_tty(void)
365 { return NULL; }
366 /* tty_io.c */
367 static inline int __init tty_init(void)
368 { return 0; }
369 static inline const char *tty_name(const struct tty_struct *tty)
370 { return "(none)"; }
371 static inline struct tty_struct *tty_kopen_exclusive(dev_t device)
372 { return ERR_PTR(-ENODEV); }
373 static inline void tty_kclose(struct tty_struct *tty)
374 { }
375 static inline int tty_dev_name_to_number(const char *name, dev_t *number)
376 { return -ENOTSUPP; }
377 #endif
378
379 extern struct ktermios tty_std_termios;
380
381 int vcs_init(void);
382
383 extern const struct class tty_class;
384
385 /**
386  * tty_kref_get - get a tty reference
387  * @tty: tty device
388  *
389  * Returns: a new reference to a tty object
390  *
391  * Locking: The caller must hold sufficient locks/counts to ensure that their
392  * existing reference cannot go away.
393  */
394 static inline struct tty_struct *tty_kref_get(struct tty_struct *tty)
395 {
396         if (tty)
397                 kref_get(&tty->kref);
398         return tty;
399 }
400
401 const char *tty_driver_name(const struct tty_struct *tty);
402 void tty_wait_until_sent(struct tty_struct *tty, long timeout);
403 void stop_tty(struct tty_struct *tty);
404 void start_tty(struct tty_struct *tty);
405 void tty_write_message(struct tty_struct *tty, char *msg);
406 int tty_send_xchar(struct tty_struct *tty, u8 ch);
407 int tty_put_char(struct tty_struct *tty, u8 c);
408 unsigned int tty_chars_in_buffer(struct tty_struct *tty);
409 unsigned int tty_write_room(struct tty_struct *tty);
410 void tty_driver_flush_buffer(struct tty_struct *tty);
411 void tty_unthrottle(struct tty_struct *tty);
412 bool tty_throttle_safe(struct tty_struct *tty);
413 bool tty_unthrottle_safe(struct tty_struct *tty);
414 int tty_do_resize(struct tty_struct *tty, struct winsize *ws);
415 int tty_get_icount(struct tty_struct *tty,
416                 struct serial_icounter_struct *icount);
417 int tty_get_tiocm(struct tty_struct *tty);
418 int is_current_pgrp_orphaned(void);
419 void tty_hangup(struct tty_struct *tty);
420 void tty_vhangup(struct tty_struct *tty);
421 int tty_hung_up_p(struct file *filp);
422 void do_SAK(struct tty_struct *tty);
423 void __do_SAK(struct tty_struct *tty);
424 void no_tty(void);
425 speed_t tty_termios_baud_rate(const struct ktermios *termios);
426 void tty_termios_encode_baud_rate(struct ktermios *termios, speed_t ibaud,
427                 speed_t obaud);
428 void tty_encode_baud_rate(struct tty_struct *tty, speed_t ibaud,
429                 speed_t obaud);
430
431 /**
432  * tty_get_baud_rate - get tty bit rates
433  * @tty: tty to query
434  *
435  * Returns: the baud rate as an integer for this terminal
436  *
437  * Locking: The termios lock must be held by the caller.
438  */
439 static inline speed_t tty_get_baud_rate(const struct tty_struct *tty)
440 {
441         return tty_termios_baud_rate(&tty->termios);
442 }
443
444 unsigned char tty_get_char_size(unsigned int cflag);
445 unsigned char tty_get_frame_size(unsigned int cflag);
446
447 void tty_termios_copy_hw(struct ktermios *new, const struct ktermios *old);
448 bool tty_termios_hw_change(const struct ktermios *a, const struct ktermios *b);
449 int tty_set_termios(struct tty_struct *tty, struct ktermios *kt);
450
451 void tty_wakeup(struct tty_struct *tty);
452
453 int tty_mode_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg);
454 int tty_perform_flush(struct tty_struct *tty, unsigned long arg);
455 struct tty_struct *tty_init_dev(struct tty_driver *driver, int idx);
456 void tty_release_struct(struct tty_struct *tty, int idx);
457 void tty_init_termios(struct tty_struct *tty);
458 void tty_save_termios(struct tty_struct *tty);
459 int tty_standard_install(struct tty_driver *driver,
460                 struct tty_struct *tty);
461
462 extern struct mutex tty_mutex;
463
464 /* n_tty.c */
465 void n_tty_inherit_ops(struct tty_ldisc_ops *ops);
466 #ifdef CONFIG_TTY
467 void __init n_tty_init(void);
468 #else
469 static inline void n_tty_init(void) { }
470 #endif
471
472 /* tty_audit.c */
473 #ifdef CONFIG_AUDIT
474 void tty_audit_exit(void);
475 void tty_audit_fork(struct signal_struct *sig);
476 int tty_audit_push(void);
477 #else
478 static inline void tty_audit_exit(void)
479 {
480 }
481 static inline void tty_audit_fork(struct signal_struct *sig)
482 {
483 }
484 static inline int tty_audit_push(void)
485 {
486         return 0;
487 }
488 #endif
489
490 /* tty_ioctl.c */
491 int n_tty_ioctl_helper(struct tty_struct *tty, unsigned int cmd,
492                 unsigned long arg);
493
494 /* vt.c */
495
496 int vt_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg);
497
498 long vt_compat_ioctl(struct tty_struct *tty, unsigned int cmd,
499                 unsigned long arg);
500
501 /* tty_mutex.c */
502 /* functions for preparation of BKL removal */
503 void tty_lock(struct tty_struct *tty);
504 int  tty_lock_interruptible(struct tty_struct *tty);
505 void tty_unlock(struct tty_struct *tty);
506 void tty_lock_slave(struct tty_struct *tty);
507 void tty_unlock_slave(struct tty_struct *tty);
508 void tty_set_lock_subclass(struct tty_struct *tty);
509
510 #endif