tty.h 28 KB

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