1
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_buffer.h>
10#include <linux/tty_driver.h>
11#include <linux/tty_ldisc.h>
12#include <linux/tty_port.h>
13#include <linux/mutex.h>
14#include <linux/tty_flags.h>
15#include <uapi/linux/tty.h>
16#include <linux/rwsem.h>
17#include <linux/llist.h>
18
19
20
21
22
23
24#define NR_UNIX98_PTY_DEFAULT 4096
25#define NR_UNIX98_PTY_RESERVE 1024
26#define NR_UNIX98_PTY_MAX (1 << MINORBITS)
27
28
29
30
31
32
33#define __DISABLED_CHAR '\0'
34
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])
52
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))
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)
99#define C_CMSPAR(tty) _C_FLAG((tty), CMSPAR)
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)
116#define L_EXTPROC(tty) _L_FLAG((tty), EXTPROC)
117
118struct device;
119struct signal_struct;
120struct tty_operations;
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195struct tty_struct {
196 int magic;
197 struct kref kref;
198 struct device *dev;
199 struct tty_driver *driver;
200 const struct tty_operations *ops;
201 int index;
202
203 struct ld_semaphore ldisc_sem;
204 struct tty_ldisc *ldisc;
205
206 struct mutex atomic_write_lock;
207 struct mutex legacy_mutex;
208 struct mutex throttle_mutex;
209 struct rw_semaphore termios_rwsem;
210 struct mutex winsize_mutex;
211 struct ktermios termios, termios_locked;
212 char name[64];
213 unsigned long flags;
214 int count;
215 struct winsize winsize;
216
217 struct {
218 spinlock_t lock;
219 bool stopped;
220 bool tco_stopped;
221 unsigned long unused[0];
222 } __aligned(sizeof(unsigned long)) flow;
223
224 struct {
225 spinlock_t lock;
226 struct pid *pgrp;
227 struct pid *session;
228 unsigned char pktstatus;
229 bool packet;
230 unsigned long unused[0];
231 } __aligned(sizeof(unsigned long)) ctrl;
232
233 int hw_stopped;
234 unsigned int receive_room;
235 int flow_change;
236
237 struct tty_struct *link;
238 struct fasync_struct *fasync;
239 wait_queue_head_t write_wait;
240 wait_queue_head_t read_wait;
241 struct work_struct hangup_work;
242 void *disc_data;
243 void *driver_data;
244 spinlock_t files_lock;
245 struct list_head tty_files;
246
247#define N_TTY_BUF_SIZE 4096
248
249 int closing;
250 unsigned char *write_buf;
251 int write_cnt;
252 struct work_struct SAK_work;
253 struct tty_port *port;
254} __randomize_layout;
255
256
257struct tty_file_private {
258 struct tty_struct *tty;
259 struct file *file;
260 struct list_head list;
261};
262
263
264#define TTY_MAGIC 0x5401
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320#define TTY_THROTTLED 0
321#define TTY_IO_ERROR 1
322#define TTY_OTHER_CLOSED 2
323#define TTY_EXCLUSIVE 3
324#define TTY_DO_WRITE_WAKEUP 5
325#define TTY_LDISC_OPEN 11
326#define TTY_PTY_LOCK 16
327#define TTY_NO_WRITE_SPLIT 17
328#define TTY_HUPPED 18
329#define TTY_HUPPING 19
330#define TTY_LDISC_CHANGING 20
331#define TTY_LDISC_HALTED 22
332
333static inline bool tty_io_nonblock(struct tty_struct *tty, struct file *file)
334{
335 return file->f_flags & O_NONBLOCK ||
336 test_bit(TTY_LDISC_CHANGING, &tty->flags);
337}
338
339static inline bool tty_io_error(struct tty_struct *tty)
340{
341 return test_bit(TTY_IO_ERROR, &tty->flags);
342}
343
344static inline bool tty_throttled(struct tty_struct *tty)
345{
346 return test_bit(TTY_THROTTLED, &tty->flags);
347}
348
349#ifdef CONFIG_TTY
350void tty_kref_put(struct tty_struct *tty);
351struct pid *tty_get_pgrp(struct tty_struct *tty);
352void tty_vhangup_self(void);
353void disassociate_ctty(int priv);
354dev_t tty_devnum(struct tty_struct *tty);
355void proc_clear_tty(struct task_struct *p);
356struct tty_struct *get_current_tty(void);
357
358int __init tty_init(void);
359const char *tty_name(const struct tty_struct *tty);
360struct tty_struct *tty_kopen_exclusive(dev_t device);
361struct tty_struct *tty_kopen_shared(dev_t device);
362void tty_kclose(struct tty_struct *tty);
363int tty_dev_name_to_number(const char *name, dev_t *number);
364#else
365static inline void tty_kref_put(struct tty_struct *tty)
366{ }
367static inline struct pid *tty_get_pgrp(struct tty_struct *tty)
368{ return NULL; }
369static inline void tty_vhangup_self(void)
370{ }
371static inline void disassociate_ctty(int priv)
372{ }
373static inline dev_t tty_devnum(struct tty_struct *tty)
374{ return 0; }
375static inline void proc_clear_tty(struct task_struct *p)
376{ }
377static inline struct tty_struct *get_current_tty(void)
378{ return NULL; }
379
380static inline int __init tty_init(void)
381{ return 0; }
382static inline const char *tty_name(const struct tty_struct *tty)
383{ return "(none)"; }
384static inline struct tty_struct *tty_kopen_exclusive(dev_t device)
385{ return ERR_PTR(-ENODEV); }
386static inline void tty_kclose(struct tty_struct *tty)
387{ }
388static inline int tty_dev_name_to_number(const char *name, dev_t *number)
389{ return -ENOTSUPP; }
390#endif
391
392extern struct ktermios tty_std_termios;
393
394int vcs_init(void);
395
396extern struct class *tty_class;
397
398
399
400
401
402
403
404
405
406
407static inline struct tty_struct *tty_kref_get(struct tty_struct *tty)
408{
409 if (tty)
410 kref_get(&tty->kref);
411 return tty;
412}
413
414const char *tty_driver_name(const struct tty_struct *tty);
415void tty_wait_until_sent(struct tty_struct *tty, long timeout);
416void stop_tty(struct tty_struct *tty);
417void start_tty(struct tty_struct *tty);
418void tty_write_message(struct tty_struct *tty, char *msg);
419int tty_send_xchar(struct tty_struct *tty, char ch);
420int tty_put_char(struct tty_struct *tty, unsigned char c);
421unsigned int tty_chars_in_buffer(struct tty_struct *tty);
422unsigned int tty_write_room(struct tty_struct *tty);
423void tty_driver_flush_buffer(struct tty_struct *tty);
424void tty_unthrottle(struct tty_struct *tty);
425int tty_throttle_safe(struct tty_struct *tty);
426int tty_unthrottle_safe(struct tty_struct *tty);
427int tty_do_resize(struct tty_struct *tty, struct winsize *ws);
428int tty_get_icount(struct tty_struct *tty,
429 struct serial_icounter_struct *icount);
430int is_current_pgrp_orphaned(void);
431void tty_hangup(struct tty_struct *tty);
432void tty_vhangup(struct tty_struct *tty);
433int tty_hung_up_p(struct file *filp);
434void do_SAK(struct tty_struct *tty);
435void __do_SAK(struct tty_struct *tty);
436void no_tty(void);
437speed_t tty_termios_baud_rate(struct ktermios *termios);
438void tty_termios_encode_baud_rate(struct ktermios *termios, speed_t ibaud,
439 speed_t obaud);
440void tty_encode_baud_rate(struct tty_struct *tty, speed_t ibaud,
441 speed_t obaud);
442
443
444
445
446
447
448
449
450
451
452
453static inline speed_t tty_get_baud_rate(struct tty_struct *tty)
454{
455 return tty_termios_baud_rate(&tty->termios);
456}
457
458unsigned char tty_get_char_size(unsigned int cflag);
459unsigned char tty_get_frame_size(unsigned int cflag);
460
461void tty_termios_copy_hw(struct ktermios *new, struct ktermios *old);
462int tty_termios_hw_change(const struct ktermios *a, const struct ktermios *b);
463int tty_set_termios(struct tty_struct *tty, struct ktermios *kt);
464
465void tty_wakeup(struct tty_struct *tty);
466
467int tty_mode_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg);
468int tty_perform_flush(struct tty_struct *tty, unsigned long arg);
469struct tty_struct *tty_init_dev(struct tty_driver *driver, int idx);
470void tty_release_struct(struct tty_struct *tty, int idx);
471void tty_init_termios(struct tty_struct *tty);
472void tty_save_termios(struct tty_struct *tty);
473int tty_standard_install(struct tty_driver *driver,
474 struct tty_struct *tty);
475
476extern struct mutex tty_mutex;
477
478
479void n_tty_inherit_ops(struct tty_ldisc_ops *ops);
480#ifdef CONFIG_TTY
481void __init n_tty_init(void);
482#else
483static inline void n_tty_init(void) { }
484#endif
485
486
487#ifdef CONFIG_AUDIT
488void tty_audit_exit(void);
489void tty_audit_fork(struct signal_struct *sig);
490int tty_audit_push(void);
491#else
492static inline void tty_audit_exit(void)
493{
494}
495static inline void tty_audit_fork(struct signal_struct *sig)
496{
497}
498static inline int tty_audit_push(void)
499{
500 return 0;
501}
502#endif
503
504
505int n_tty_ioctl_helper(struct tty_struct *tty, unsigned int cmd,
506 unsigned long arg);
507
508
509
510int vt_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg);
511
512long vt_compat_ioctl(struct tty_struct *tty, unsigned int cmd,
513 unsigned long arg);
514
515
516
517void tty_lock(struct tty_struct *tty);
518int tty_lock_interruptible(struct tty_struct *tty);
519void tty_unlock(struct tty_struct *tty);
520void tty_lock_slave(struct tty_struct *tty);
521void tty_unlock_slave(struct tty_struct *tty);
522void tty_set_lock_subclass(struct tty_struct *tty);
523
524#endif
525