tty.h 14 KB

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