tty.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021.12.07 linzhenxing first version
  9. */
  10. #ifndef __TTY_H__
  11. #define __TTY_H__
  12. #include <rthw.h>
  13. #include <rtthread.h>
  14. #include <tty_ldisc.h>
  15. #ifdef RT_USING_SMART
  16. #include <lwp.h>
  17. #endif
  18. #if defined(RT_USING_POSIX_TERMIOS)
  19. #include <poll.h>
  20. #include <termios.h>
  21. #endif
  22. #ifndef ENOIOCTLCMD
  23. #define ENOIOCTLCMD (515) /* No ioctl command */
  24. #endif
  25. #define current lwp_self()
  26. #define __DISABLED_CHAR '\0'
  27. struct tty_node
  28. {
  29. struct rt_lwp *lwp;
  30. struct tty_node *next;
  31. };
  32. void tty_initstack(struct tty_node *node);
  33. int tty_push(struct tty_node **head, struct rt_lwp *lwp);
  34. struct rt_lwp *tty_pop(struct tty_node **head, struct rt_lwp *target_lwp);
  35. /*
  36. * When a break, frame error, or parity error happens, these codes are
  37. * stuffed into the flags buffer.
  38. */
  39. #define TTY_NORMAL 0
  40. #define TTY_BREAK 1
  41. #define TTY_FRAME 2
  42. #define TTY_PARITY 3
  43. #define TTY_OVERRUN 4
  44. #define INTR_CHAR(tty) ((tty)->init_termios.c_cc[VINTR])
  45. #define QUIT_CHAR(tty) ((tty)->init_termios.c_cc[VQUIT])
  46. #define ERASE_CHAR(tty) ((tty)->init_termios.c_cc[VERASE])
  47. #define KILL_CHAR(tty) ((tty)->init_termios.c_cc[VKILL])
  48. #define EOF_CHAR(tty) ((tty)->init_termios.c_cc[VEOF])
  49. #define TIME_CHAR(tty) ((tty)->init_termios.c_cc[VTIME])
  50. #define MIN_CHAR(tty) ((tty)->init_termios.c_cc[VMIN])
  51. #define SWTC_CHAR(tty) ((tty)->init_termios.c_cc[VSWTC])
  52. #define START_CHAR(tty) ((tty)->init_termios.c_cc[VSTART])
  53. #define STOP_CHAR(tty) ((tty)->init_termios.c_cc[VSTOP])
  54. #define SUSP_CHAR(tty) ((tty)->init_termios.c_cc[VSUSP])
  55. #define EOL_CHAR(tty) ((tty)->init_termios.c_cc[VEOL])
  56. #define REPRINT_CHAR(tty) ((tty)->init_termios.c_cc[VREPRINT])
  57. #define DISCARD_CHAR(tty) ((tty)->init_termios.c_cc[VDISCARD])
  58. #define WERASE_CHAR(tty) ((tty)->init_termios.c_cc[VWERASE])
  59. #define LNEXT_CHAR(tty) ((tty)->init_termios.c_cc[VLNEXT])
  60. #define EOL2_CHAR(tty) ((tty)->init_termios.c_cc[VEOL2])
  61. #define _I_FLAG(tty,f) ((tty)->init_termios.c_iflag & (f))
  62. #define _O_FLAG(tty,f) ((tty)->init_termios.c_oflag & (f))
  63. #define _C_FLAG(tty,f) ((tty)->init_termios.c_cflag & (f))
  64. #define _L_FLAG(tty,f) ((tty)->init_termios.c_lflag & (f))
  65. #define I_IGNBRK(tty) _I_FLAG((tty),IGNBRK)
  66. #define I_BRKINT(tty) _I_FLAG((tty),BRKINT)
  67. #define I_IGNPAR(tty) _I_FLAG((tty),IGNPAR)
  68. #define I_PARMRK(tty) _I_FLAG((tty),PARMRK)
  69. #define I_INPCK(tty) _I_FLAG((tty),INPCK)
  70. #define I_ISTRIP(tty) _I_FLAG((tty),ISTRIP)
  71. #define I_INLCR(tty) _I_FLAG((tty),INLCR)
  72. #define I_IGNCR(tty) _I_FLAG((tty),IGNCR)
  73. #define I_ICRNL(tty) _I_FLAG((tty),ICRNL)
  74. #define I_IUCLC(tty) _I_FLAG((tty),IUCLC)
  75. #define I_IXON(tty) _I_FLAG((tty),IXON)
  76. #define I_IXANY(tty) _I_FLAG((tty),IXANY)
  77. #define I_IXOFF(tty) _I_FLAG((tty),IXOFF)
  78. #define I_IMAXBEL(tty) _I_FLAG((tty),IMAXBEL)
  79. #define I_IUTF8(tty) _I_FLAG((tty), IUTF8)
  80. #define O_OPOST(tty) _O_FLAG((tty),OPOST)
  81. #define O_OLCUC(tty) _O_FLAG((tty),OLCUC)
  82. #define O_ONLCR(tty) _O_FLAG((tty),ONLCR)
  83. #define O_OCRNL(tty) _O_FLAG((tty),OCRNL)
  84. #define O_ONOCR(tty) _O_FLAG((tty),ONOCR)
  85. #define O_ONLRET(tty) _O_FLAG((tty),ONLRET)
  86. #define O_OFILL(tty) _O_FLAG((tty),OFILL)
  87. #define O_OFDEL(tty) _O_FLAG((tty),OFDEL)
  88. #define O_NLDLY(tty) _O_FLAG((tty),NLDLY)
  89. #define O_CRDLY(tty) _O_FLAG((tty),CRDLY)
  90. #define O_TABDLY(tty) _O_FLAG((tty),TABDLY)
  91. #define O_BSDLY(tty) _O_FLAG((tty),BSDLY)
  92. #define O_VTDLY(tty) _O_FLAG((tty),VTDLY)
  93. #define O_FFDLY(tty) _O_FLAG((tty),FFDLY)
  94. #define C_BAUD(tty) _C_FLAG((tty),CBAUD)
  95. #define C_CSIZE(tty) _C_FLAG((tty),CSIZE)
  96. #define C_CSTOPB(tty) _C_FLAG((tty),CSTOPB)
  97. #define C_CREAD(tty) _C_FLAG((tty),CREAD)
  98. #define C_PARENB(tty) _C_FLAG((tty),PARENB)
  99. #define C_PARODD(tty) _C_FLAG((tty),PARODD)
  100. #define C_HUPCL(tty) _C_FLAG((tty),HUPCL)
  101. #define C_CLOCAL(tty) _C_FLAG((tty),CLOCAL)
  102. #define C_CIBAUD(tty) _C_FLAG((tty),CIBAUD)
  103. #define C_CRTSCTS(tty) _C_FLAG((tty),CRTSCTS)
  104. #define L_ISIG(tty) _L_FLAG((tty),ISIG)
  105. #define L_ICANON(tty) _L_FLAG((tty),ICANON)
  106. #define L_XCASE(tty) _L_FLAG((tty),XCASE)
  107. #define L_ECHO(tty) _L_FLAG((tty),ECHO)
  108. #define L_ECHOE(tty) _L_FLAG((tty),ECHOE)
  109. #define L_ECHOK(tty) _L_FLAG((tty),ECHOK)
  110. #define L_ECHONL(tty) _L_FLAG((tty),ECHONL)
  111. #define L_NOFLSH(tty) _L_FLAG((tty),NOFLSH)
  112. #define L_TOSTOP(tty) _L_FLAG((tty),TOSTOP)
  113. #define L_ECHOCTL(tty) _L_FLAG((tty),ECHOCTL)
  114. #define L_ECHOPRT(tty) _L_FLAG((tty),ECHOPRT)
  115. #define L_ECHOKE(tty) _L_FLAG((tty),ECHOKE)
  116. #define L_FLUSHO(tty) _L_FLAG((tty),FLUSHO)
  117. #define L_PENDIN(tty) _L_FLAG((tty),PENDIN)
  118. #define L_IEXTEN(tty) _L_FLAG((tty),IEXTEN)
  119. #define L_EXTPROC(tty) _L_FLAG((tty), EXTPROC)
  120. /*
  121. * Where all of the state associated with a tty is kept while the tty
  122. * is open. Since the termios state should be kept even if the tty
  123. * has been closed --- for things like the baud rate, etc --- it is
  124. * not stored here, but rather a pointer to the real state is stored
  125. * here. Possible the winsize structure should have the same
  126. * treatment, but (1) the default 80x24 is usually right and (2) it's
  127. * most often used by a windowing system, which will set the correct
  128. * size each time the window is created or resized anyway.
  129. * - TYT, 9/14/92
  130. */
  131. struct tty_struct
  132. {
  133. struct rt_device parent;
  134. int type;
  135. int subtype;
  136. int init_flag;
  137. int index; //for pty
  138. int pts_lock; //for pty
  139. struct tty_struct *other_struct; //for pty
  140. struct termios init_termios;
  141. struct winsize winsize;
  142. struct rt_mutex lock;
  143. pid_t pgrp;
  144. pid_t session;
  145. struct rt_lwp *foreground;
  146. struct tty_node *head;
  147. struct tty_ldisc *ldisc;
  148. void *disc_data;
  149. struct rt_device *io_dev;
  150. struct rt_wqueue wait_queue;
  151. #define RT_TTY_BUF 1024
  152. rt_list_t tty_drivers;
  153. };
  154. enum
  155. {
  156. TTY_INIT_FLAG_NONE = 0,
  157. TTY_INIT_FLAG_ALLOCED,
  158. TTY_INIT_FLAG_REGED,
  159. TTY_INIT_FLAG_INITED,
  160. };
  161. #define TTY_DRIVER_TYPE_SYSTEM 0x0001
  162. #define TTY_DRIVER_TYPE_CONSOLE 0x0002
  163. #define TTY_DRIVER_TYPE_SERIAL 0x0003
  164. #define TTY_DRIVER_TYPE_PTY 0x0004
  165. #define TTY_DRIVER_TYPE_SCC 0x0005 /* scc driver */
  166. #define TTY_DRIVER_TYPE_SYSCONS 0x0006
  167. /* tty magic number */
  168. #define TTY_MAGIC 0x5401
  169. /*
  170. * These bits are used in the flags field of the tty structure.
  171. *
  172. * So that interrupts won't be able to mess up the queues,
  173. * copy_to_cooked must be atomic with respect to itself, as must
  174. * tty->write. Thus, you must use the inline functions set_bit() and
  175. * clear_bit() to make things atomic.
  176. */
  177. #define TTY_THROTTLED 0
  178. #define TTY_IO_ERROR 1
  179. #define TTY_OTHER_CLOSED 2
  180. #define TTY_EXCLUSIVE 3
  181. #define TTY_DEBUG 4
  182. #define TTY_DO_WRITE_WAKEUP 5
  183. #define TTY_PUSH 6
  184. #define TTY_CLOSING 7
  185. #define TTY_DONT_FLIP 8
  186. #define TTY_HW_COOK_OUT 14
  187. #define TTY_HW_COOK_IN 15
  188. #define TTY_PTY_LOCK 16
  189. #define TTY_NO_WRITE_SPLIT 17
  190. #define NR_LDISCS 30
  191. /* line disciplines */
  192. #define N_TTY 0
  193. #define N_SLIP 1
  194. #define N_MOUSE 2
  195. #define N_PPP 3
  196. #define N_STRIP 4
  197. #define N_AX25 5
  198. #define N_X25 6 /* X.25 async */
  199. #define N_6PACK 7
  200. #define N_MASC 8 /* Reserved for Mobitex module <kaz@cafe.net> */
  201. #define N_R3964 9 /* Reserved for Simatic R3964 module */
  202. #define N_PROFIBUS_FDL 10 /* Reserved for Profibus */
  203. #define N_IRDA 11 /* Linux IrDa - http://irda.sourceforge.net/ */
  204. #define N_SMSBLOCK 12 /* SMS block mode - for talking to GSM data */
  205. /* cards about SMS messages */
  206. #define N_HDLC 13 /* synchronous HDLC */
  207. #define N_SYNC_PPP 14 /* synchronous PPP */
  208. #define N_HCI 15 /* Bluetooth HCI UART */
  209. #define N_GIGASET_M101 16 /* Siemens Gigaset M101 serial DECT adapter */
  210. #define N_SLCAN 17 /* Serial / USB serial CAN Adaptors */
  211. #define N_PPS 18 /* Pulse per Second */
  212. #define N_V253 19 /* Codec control over voice modem */
  213. #define N_CAIF 20 /* CAIF protocol for talking to modems */
  214. #define N_GSM0710 21 /* GSM 0710 Mux */
  215. #define N_TI_WL 22 /* for TI's WL BT, FM, GPS combo chips */
  216. #define N_TRACESINK 23 /* Trace data routing for MIPI P1149.7 */
  217. #define N_TRACEROUTER 24 /* Trace data routing for MIPI P1149.7 */
  218. #define N_NCI 25 /* NFC NCI UART */
  219. /* Used for packet mode */
  220. #define TIOCPKT_DATA 0
  221. #define TIOCPKT_FLUSHREAD 1
  222. #define TIOCPKT_FLUSHWRITE 2
  223. #define TIOCPKT_STOP 4
  224. #define TIOCPKT_START 8
  225. #define TIOCPKT_NOSTOP 16
  226. #define TIOCPKT_DOSTOP 32
  227. /* pty subtypes */
  228. #define PTY_TYPE_MASTER 0x0001
  229. #define PTY_TYPE_SLAVE 0x0002
  230. /* serial subtype definitions */
  231. #define SERIAL_TYPE_NORMAL 1
  232. #define max(a, b) ({\
  233. typeof(a) _a = a;\
  234. typeof(b) _b = b;\
  235. _a > _b ? _a : _b; })
  236. #define min(a, b) ({\
  237. typeof(a) _a = a;\
  238. typeof(b) _b = b;\
  239. _a < _b ? _a : _b; })
  240. void mutex_lock(rt_mutex_t mutex);
  241. void mutex_unlock(rt_mutex_t mutex);
  242. int __tty_check_change(struct tty_struct *tty, int sig);
  243. int tty_check_change(struct tty_struct *tty);
  244. rt_inline struct rt_wqueue *wait_queue_get(struct rt_lwp *lwp, struct tty_struct *tty)
  245. {
  246. if (lwp == RT_NULL)
  247. {
  248. return &tty->wait_queue;
  249. }
  250. return &lwp->wait_queue;
  251. }
  252. rt_inline struct rt_wqueue *wait_queue_current_get(struct rt_lwp *lwp, struct tty_struct *tty)
  253. {
  254. return wait_queue_get(lwp, tty);
  255. }
  256. rt_inline void tty_wakeup_check(struct tty_struct *tty)
  257. {
  258. struct rt_wqueue *wq = NULL;
  259. wq = wait_queue_current_get(tty->foreground, tty);
  260. rt_wqueue_wakeup(wq, (void*)POLLIN);
  261. }
  262. int tty_init(struct tty_struct *tty, int type, int subtype, struct rt_device *iodev);
  263. const struct dfs_file_ops *tty_get_fops(void);
  264. int n_tty_ioctl_extend(struct tty_struct *tty, int cmd, void *arg);
  265. void console_ldata_init(struct tty_struct *tty);
  266. int n_tty_receive_buf(struct tty_struct *tty, char *cp, int count);
  267. #endif /*__TTY_H__*/