tty.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /*
  2. * Copyright (c) 2006-2021, 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_LWP
  16. #include <lwp.h>
  17. #endif
  18. #if defined(RT_USING_POSIX)
  19. #include <dfs_poll.h>
  20. #include <posix_termios.h>
  21. #endif
  22. #define current lwp_self()
  23. #define __DISABLED_CHAR '\0'
  24. /*
  25. * When a break, frame error, or parity error happens, these codes are
  26. * stuffed into the flags buffer.
  27. */
  28. #define TTY_NORMAL 0
  29. #define TTY_BREAK 1
  30. #define TTY_FRAME 2
  31. #define TTY_PARITY 3
  32. #define TTY_OVERRUN 4
  33. #define INTR_CHAR(tty) ((tty)->init_termios.c_cc[VINTR])
  34. #define QUIT_CHAR(tty) ((tty)->init_termios.c_cc[VQUIT])
  35. #define ERASE_CHAR(tty) ((tty)->init_termios.c_cc[VERASE])
  36. #define KILL_CHAR(tty) ((tty)->init_termios.c_cc[VKILL])
  37. #define EOF_CHAR(tty) ((tty)->init_termios.c_cc[VEOF])
  38. #define TIME_CHAR(tty) ((tty)->init_termios.c_cc[VTIME])
  39. #define MIN_CHAR(tty) ((tty)->init_termios.c_cc[VMIN])
  40. #define SWTC_CHAR(tty) ((tty)->init_termios.c_cc[VSWTC])
  41. #define START_CHAR(tty) ((tty)->init_termios.c_cc[VSTART])
  42. #define STOP_CHAR(tty) ((tty)->init_termios.c_cc[VSTOP])
  43. #define SUSP_CHAR(tty) ((tty)->init_termios.c_cc[VSUSP])
  44. #define EOL_CHAR(tty) ((tty)->init_termios.c_cc[VEOL])
  45. #define REPRINT_CHAR(tty) ((tty)->init_termios.c_cc[VREPRINT])
  46. #define DISCARD_CHAR(tty) ((tty)->init_termios.c_cc[VDISCARD])
  47. #define WERASE_CHAR(tty) ((tty)->init_termios.c_cc[VWERASE])
  48. #define LNEXT_CHAR(tty) ((tty)->init_termios.c_cc[VLNEXT])
  49. #define EOL2_CHAR(tty) ((tty)->init_termios.c_cc[VEOL2])
  50. #define _I_FLAG(tty,f) ((tty)->init_termios.c_iflag & (f))
  51. #define _O_FLAG(tty,f) ((tty)->init_termios.c_oflag & (f))
  52. #define _C_FLAG(tty,f) ((tty)->init_termios.c_cflag & (f))
  53. #define _L_FLAG(tty,f) ((tty)->init_termios.c_lflag & (f))
  54. #define I_IGNBRK(tty) _I_FLAG((tty),IGNBRK)
  55. #define I_BRKINT(tty) _I_FLAG((tty),BRKINT)
  56. #define I_IGNPAR(tty) _I_FLAG((tty),IGNPAR)
  57. #define I_PARMRK(tty) _I_FLAG((tty),PARMRK)
  58. #define I_INPCK(tty) _I_FLAG((tty),INPCK)
  59. #define I_ISTRIP(tty) _I_FLAG((tty),ISTRIP)
  60. #define I_INLCR(tty) _I_FLAG((tty),INLCR)
  61. #define I_IGNCR(tty) _I_FLAG((tty),IGNCR)
  62. #define I_ICRNL(tty) _I_FLAG((tty),ICRNL)
  63. #define I_IUCLC(tty) _I_FLAG((tty),IUCLC)
  64. #define I_IXON(tty) _I_FLAG((tty),IXON)
  65. #define I_IXANY(tty) _I_FLAG((tty),IXANY)
  66. #define I_IXOFF(tty) _I_FLAG((tty),IXOFF)
  67. #define I_IMAXBEL(tty) _I_FLAG((tty),IMAXBEL)
  68. #define I_IUTF8(tty) _I_FLAG((tty), IUTF8)
  69. #define O_OPOST(tty) _O_FLAG((tty),OPOST)
  70. #define O_OLCUC(tty) _O_FLAG((tty),OLCUC)
  71. #define O_ONLCR(tty) _O_FLAG((tty),ONLCR)
  72. #define O_OCRNL(tty) _O_FLAG((tty),OCRNL)
  73. #define O_ONOCR(tty) _O_FLAG((tty),ONOCR)
  74. #define O_ONLRET(tty) _O_FLAG((tty),ONLRET)
  75. #define O_OFILL(tty) _O_FLAG((tty),OFILL)
  76. #define O_OFDEL(tty) _O_FLAG((tty),OFDEL)
  77. #define O_NLDLY(tty) _O_FLAG((tty),NLDLY)
  78. #define O_CRDLY(tty) _O_FLAG((tty),CRDLY)
  79. #define O_TABDLY(tty) _O_FLAG((tty),TABDLY)
  80. #define O_BSDLY(tty) _O_FLAG((tty),BSDLY)
  81. #define O_VTDLY(tty) _O_FLAG((tty),VTDLY)
  82. #define O_FFDLY(tty) _O_FLAG((tty),FFDLY)
  83. #define C_BAUD(tty) _C_FLAG((tty),CBAUD)
  84. #define C_CSIZE(tty) _C_FLAG((tty),CSIZE)
  85. #define C_CSTOPB(tty) _C_FLAG((tty),CSTOPB)
  86. #define C_CREAD(tty) _C_FLAG((tty),CREAD)
  87. #define C_PARENB(tty) _C_FLAG((tty),PARENB)
  88. #define C_PARODD(tty) _C_FLAG((tty),PARODD)
  89. #define C_HUPCL(tty) _C_FLAG((tty),HUPCL)
  90. #define C_CLOCAL(tty) _C_FLAG((tty),CLOCAL)
  91. #define C_CIBAUD(tty) _C_FLAG((tty),CIBAUD)
  92. #define C_CRTSCTS(tty) _C_FLAG((tty),CRTSCTS)
  93. #define L_ISIG(tty) _L_FLAG((tty),ISIG)
  94. #define L_ICANON(tty) _L_FLAG((tty),ICANON)
  95. #define L_XCASE(tty) _L_FLAG((tty),XCASE)
  96. #define L_ECHO(tty) _L_FLAG((tty),ECHO)
  97. #define L_ECHOE(tty) _L_FLAG((tty),ECHOE)
  98. #define L_ECHOK(tty) _L_FLAG((tty),ECHOK)
  99. #define L_ECHONL(tty) _L_FLAG((tty),ECHONL)
  100. #define L_NOFLSH(tty) _L_FLAG((tty),NOFLSH)
  101. #define L_TOSTOP(tty) _L_FLAG((tty),TOSTOP)
  102. #define L_ECHOCTL(tty) _L_FLAG((tty),ECHOCTL)
  103. #define L_ECHOPRT(tty) _L_FLAG((tty),ECHOPRT)
  104. #define L_ECHOKE(tty) _L_FLAG((tty),ECHOKE)
  105. #define L_FLUSHO(tty) _L_FLAG((tty),FLUSHO)
  106. #define L_PENDIN(tty) _L_FLAG((tty),PENDIN)
  107. #define L_IEXTEN(tty) _L_FLAG((tty),IEXTEN)
  108. #define L_EXTPROC(tty) _L_FLAG((tty), EXTPROC)
  109. /*
  110. * Where all of the state associated with a tty is kept while the tty
  111. * is open. Since the termios state should be kept even if the tty
  112. * has been closed --- for things like the baud rate, etc --- it is
  113. * not stored here, but rather a pointer to the real state is stored
  114. * here. Possible the winsize structure should have the same
  115. * treatment, but (1) the default 80x24 is usually right and (2) it's
  116. * most often used by a windowing system, which will set the correct
  117. * size each time the window is created or resized anyway.
  118. * - TYT, 9/14/92
  119. */
  120. struct tty_struct
  121. {
  122. struct rt_device parent;
  123. int type;
  124. int subtype;
  125. int init_flag;
  126. int index; //for pty
  127. int pts_lock; //for pty
  128. struct tty_struct *other_struct; //for pty
  129. struct winsize winsize;
  130. struct termios init_termios;
  131. struct rt_mutex mutex;
  132. pid_t pgrp;
  133. pid_t session;
  134. struct rt_lwp *foreground;
  135. struct tty_ldisc *ldisc;
  136. void *disc_data;
  137. struct rt_device *driver;
  138. struct rt_wqueue wait_queue;
  139. #define RT_TTY_BUF 1024
  140. rt_list_t tty_drivers;
  141. };
  142. enum
  143. {
  144. TTY_INIT_FLAG_NONE = 0,
  145. TTY_INIT_FLAG_ALLOCED,
  146. TTY_INIT_FLAG_REGED,
  147. TTY_INIT_FLAG_INITED,
  148. };
  149. #define TTY_DRIVER_TYPE_SYSTEM 0x0001
  150. #define TTY_DRIVER_TYPE_CONSOLE 0x0002
  151. #define TTY_DRIVER_TYPE_SERIAL 0x0003
  152. #define TTY_DRIVER_TYPE_PTY 0x0004
  153. #define TTY_DRIVER_TYPE_SCC 0x0005 /* scc driver */
  154. #define TTY_DRIVER_TYPE_SYSCONS 0x0006
  155. /* tty magic number */
  156. #define TTY_MAGIC 0x5401
  157. /*
  158. * These bits are used in the flags field of the tty structure.
  159. *
  160. * So that interrupts won't be able to mess up the queues,
  161. * copy_to_cooked must be atomic with respect to itself, as must
  162. * tty->write. Thus, you must use the inline functions set_bit() and
  163. * clear_bit() to make things atomic.
  164. */
  165. #define TTY_THROTTLED 0
  166. #define TTY_IO_ERROR 1
  167. #define TTY_OTHER_CLOSED 2
  168. #define TTY_EXCLUSIVE 3
  169. #define TTY_DEBUG 4
  170. #define TTY_DO_WRITE_WAKEUP 5
  171. #define TTY_PUSH 6
  172. #define TTY_CLOSING 7
  173. #define TTY_DONT_FLIP 8
  174. #define TTY_HW_COOK_OUT 14
  175. #define TTY_HW_COOK_IN 15
  176. #define TTY_PTY_LOCK 16
  177. #define TTY_NO_WRITE_SPLIT 17
  178. /*
  179. * These bits are used in the flags field of the tty structure.
  180. *
  181. * So that interrupts won't be able to mess up the queues,
  182. * copy_to_cooked must be atomic with respect to itself, as must
  183. * tty->write. Thus, you must use the inline functions set_bit() and
  184. * clear_bit() to make things atomic.
  185. */
  186. #define TTY_THROTTLED 0
  187. #define TTY_IO_ERROR 1
  188. #define TTY_OTHER_CLOSED 2
  189. #define TTY_EXCLUSIVE 3
  190. #define TTY_DEBUG 4
  191. #define TTY_DO_WRITE_WAKEUP 5
  192. #define TTY_PUSH 6
  193. #define TTY_CLOSING 7
  194. #define TTY_DONT_FLIP 8
  195. #define TTY_HW_COOK_OUT 14
  196. #define TTY_HW_COOK_IN 15
  197. #define TTY_PTY_LOCK 16
  198. #define TTY_NO_WRITE_SPLIT 17
  199. #define NR_LDISCS 30
  200. /* line disciplines */
  201. #define N_TTY 0
  202. #define N_SLIP 1
  203. #define N_MOUSE 2
  204. #define N_PPP 3
  205. #define N_STRIP 4
  206. #define N_AX25 5
  207. #define N_X25 6 /* X.25 async */
  208. #define N_6PACK 7
  209. #define N_MASC 8 /* Reserved for Mobitex module <kaz@cafe.net> */
  210. #define N_R3964 9 /* Reserved for Simatic R3964 module */
  211. #define N_PROFIBUS_FDL 10 /* Reserved for Profibus */
  212. #define N_IRDA 11 /* Linux IrDa - http://irda.sourceforge.net/ */
  213. #define N_SMSBLOCK 12 /* SMS block mode - for talking to GSM data */
  214. /* cards about SMS messages */
  215. #define N_HDLC 13 /* synchronous HDLC */
  216. #define N_SYNC_PPP 14 /* synchronous PPP */
  217. #define N_HCI 15 /* Bluetooth HCI UART */
  218. #define N_GIGASET_M101 16 /* Siemens Gigaset M101 serial DECT adapter */
  219. #define N_SLCAN 17 /* Serial / USB serial CAN Adaptors */
  220. #define N_PPS 18 /* Pulse per Second */
  221. #define N_V253 19 /* Codec control over voice modem */
  222. #define N_CAIF 20 /* CAIF protocol for talking to modems */
  223. #define N_GSM0710 21 /* GSM 0710 Mux */
  224. #define N_TI_WL 22 /* for TI's WL BT, FM, GPS combo chips */
  225. #define N_TRACESINK 23 /* Trace data routing for MIPI P1149.7 */
  226. #define N_TRACEROUTER 24 /* Trace data routing for MIPI P1149.7 */
  227. #define N_NCI 25 /* NFC NCI UART */
  228. /* Used for packet mode */
  229. #define TIOCPKT_DATA 0
  230. #define TIOCPKT_FLUSHREAD 1
  231. #define TIOCPKT_FLUSHWRITE 2
  232. #define TIOCPKT_STOP 4
  233. #define TIOCPKT_START 8
  234. #define TIOCPKT_NOSTOP 16
  235. #define TIOCPKT_DOSTOP 32
  236. /* tty driver types */
  237. #define TTY_DRIVER_TYPE_SYSTEM 0x0001
  238. #define TTY_DRIVER_TYPE_CONSOLE 0x0002
  239. #define TTY_DRIVER_TYPE_SERIAL 0x0003
  240. #define TTY_DRIVER_TYPE_PTY 0x0004
  241. #define TTY_DRIVER_TYPE_SCC 0x0005 /* scc driver */
  242. #define TTY_DRIVER_TYPE_SYSCONS 0x0006
  243. /* pty subtypes */
  244. #define PTY_TYPE_MASTER 0x0001
  245. #define PTY_TYPE_SLAVE 0x0002
  246. /* serial subtype definitions */
  247. #define SERIAL_TYPE_NORMAL 1
  248. #define max(a, b) ({\
  249. typeof(a) _a = a;\
  250. typeof(b) _b = b;\
  251. _a > _b ? _a : _b; })
  252. #define min(a, b) ({\
  253. typeof(a) _a = a;\
  254. typeof(b) _b = b;\
  255. _a < _b ? _a : _b; })
  256. void tty_set_fops(struct dfs_file_ops *fops);
  257. void console_set_fops(struct dfs_file_ops *fops);
  258. void mutex_lock(rt_mutex_t mutex);
  259. void mutex_unlock(rt_mutex_t mutex);
  260. int __tty_check_change(struct tty_struct *tty, int sig);
  261. int tty_check_change(struct tty_struct *tty);
  262. rt_inline struct rt_wqueue *wait_queue_get(struct rt_lwp *lwp, struct tty_struct *tty)
  263. {
  264. if (lwp == RT_NULL)
  265. {
  266. return &tty->wait_queue;
  267. }
  268. return &lwp->wait_queue;
  269. }
  270. rt_inline struct rt_wqueue *wait_queue_current_get(struct rt_lwp *lwp, struct tty_struct *tty)
  271. {
  272. return wait_queue_get(lwp, tty);
  273. }
  274. rt_inline void tty_wakeup_check(struct tty_struct *tty)
  275. {
  276. struct rt_wqueue *wq = NULL;
  277. wq = wait_queue_current_get(tty->foreground, tty);
  278. rt_wqueue_wakeup(wq, (void*)POLLIN);
  279. }
  280. rt_inline int set_bit(int nr,int *addr)
  281. {
  282. int mask, retval, level;
  283. addr += nr >> 5;
  284. mask = 1 << (nr & 0x1f);
  285. level = rt_hw_interrupt_disable();
  286. retval = (mask & *addr) != 0;
  287. *addr |= mask;
  288. rt_hw_interrupt_enable(level);
  289. return retval;
  290. }
  291. rt_inline int clear_bit(int nr, int *addr)
  292. {
  293. int mask, retval, level;
  294. addr += nr >> 5;
  295. mask = 1 << (nr & 0x1f);
  296. level = rt_hw_interrupt_disable();
  297. retval = (mask & *addr) != 0;
  298. *addr &= ~mask;
  299. rt_hw_interrupt_enable(level);
  300. return retval;
  301. }
  302. rt_inline int test_bit(int nr, int *addr)
  303. {
  304. int mask;
  305. addr += nr >> 5;
  306. mask = 1 << (nr & 0x1f);
  307. return ((mask & *addr) != 0);
  308. }
  309. rt_inline int test_and_clear_bit(int nr, volatile void *addr)
  310. {
  311. int mask, retval, level;
  312. volatile unsigned int *a = addr;
  313. a += nr >> 5;
  314. mask = 1 << (nr & 0x1f);
  315. level = rt_hw_interrupt_disable();
  316. retval = (mask & *a) != 0;
  317. *a &= ~mask;
  318. rt_hw_interrupt_enable(level);
  319. return retval;
  320. }
  321. rt_inline unsigned long __ffs(unsigned long word)
  322. {
  323. int num = 0;
  324. #if BITS_PER_LONG == 64
  325. if ((word & 0xffffffff) == 0)
  326. {
  327. num += 32;
  328. word >>= 32;
  329. }
  330. #endif
  331. if ((word & 0xffff) == 0)
  332. {
  333. num += 16;
  334. word >>= 16;
  335. }
  336. if ((word & 0xff) == 0)
  337. {
  338. num += 8;
  339. word >>= 8;
  340. }
  341. if ((word & 0xf) == 0)
  342. {
  343. num += 4;
  344. word >>= 4;
  345. }
  346. if ((word & 0x3) == 0)
  347. {
  348. num += 2;
  349. word >>= 2;
  350. }
  351. if ((word & 0x1) == 0)
  352. {
  353. num += 1;
  354. }
  355. return num;
  356. }
  357. #define BITS_PER_LONG 32
  358. #define BITOP_WORD(nr) ((nr) / BITS_PER_LONG)
  359. /*
  360. * Find the next set bit in a memory region.
  361. */
  362. rt_inline unsigned long find_next_bit(const unsigned long *addr, unsigned long size,
  363. unsigned long offset)
  364. {
  365. const unsigned long *p = addr + BITOP_WORD(offset);
  366. unsigned long result = offset & ~(BITS_PER_LONG-1);
  367. unsigned long tmp;
  368. if (offset >= size)
  369. {
  370. return size;
  371. }
  372. size -= result;
  373. offset %= BITS_PER_LONG;
  374. if (offset)
  375. {
  376. tmp = *(p++);
  377. tmp &= (~0UL << offset);
  378. if (size < BITS_PER_LONG)
  379. {
  380. goto found_first;
  381. }
  382. if (tmp)
  383. {
  384. goto found_middle;
  385. }
  386. size -= BITS_PER_LONG;
  387. result += BITS_PER_LONG;
  388. }
  389. while (size & ~(BITS_PER_LONG-1))
  390. {
  391. if ((tmp = *(p++)))
  392. {
  393. goto found_middle;
  394. }
  395. result += BITS_PER_LONG;
  396. size -= BITS_PER_LONG;
  397. }
  398. if (!size)
  399. {
  400. return result;
  401. }
  402. tmp = *p;
  403. found_first:
  404. tmp &= (~0UL >> (BITS_PER_LONG - size));
  405. if (tmp == 0UL) /* Are any bits set? */
  406. {
  407. return result + size; /* Nope. */
  408. }
  409. found_middle:
  410. return result + __ffs(tmp);
  411. }
  412. /*create by tty_ioctl.c*/
  413. int n_tty_ioctl_extend(struct tty_struct *tty, int cmd, void *arg);
  414. /*create by n_tty.c*/
  415. void console_ldata_init(struct tty_struct *tty);
  416. int n_tty_receive_buf(struct tty_struct *tty, char *cp, int count);
  417. void n_tty_init(void);
  418. #endif /*__TTY_H__*/