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_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 *driver;
  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. /*
  191. * These bits are used in the flags field of the tty structure.
  192. *
  193. * So that interrupts won't be able to mess up the queues,
  194. * copy_to_cooked must be atomic with respect to itself, as must
  195. * tty->write. Thus, you must use the inline functions set_bit() and
  196. * clear_bit() to make things atomic.
  197. */
  198. #define TTY_THROTTLED 0
  199. #define TTY_IO_ERROR 1
  200. #define TTY_OTHER_CLOSED 2
  201. #define TTY_EXCLUSIVE 3
  202. #define TTY_DEBUG 4
  203. #define TTY_DO_WRITE_WAKEUP 5
  204. #define TTY_PUSH 6
  205. #define TTY_CLOSING 7
  206. #define TTY_DONT_FLIP 8
  207. #define TTY_HW_COOK_OUT 14
  208. #define TTY_HW_COOK_IN 15
  209. #define TTY_PTY_LOCK 16
  210. #define TTY_NO_WRITE_SPLIT 17
  211. #define NR_LDISCS 30
  212. /* line disciplines */
  213. #define N_TTY 0
  214. #define N_SLIP 1
  215. #define N_MOUSE 2
  216. #define N_PPP 3
  217. #define N_STRIP 4
  218. #define N_AX25 5
  219. #define N_X25 6 /* X.25 async */
  220. #define N_6PACK 7
  221. #define N_MASC 8 /* Reserved for Mobitex module <kaz@cafe.net> */
  222. #define N_R3964 9 /* Reserved for Simatic R3964 module */
  223. #define N_PROFIBUS_FDL 10 /* Reserved for Profibus */
  224. #define N_IRDA 11 /* Linux IrDa - http://irda.sourceforge.net/ */
  225. #define N_SMSBLOCK 12 /* SMS block mode - for talking to GSM data */
  226. /* cards about SMS messages */
  227. #define N_HDLC 13 /* synchronous HDLC */
  228. #define N_SYNC_PPP 14 /* synchronous PPP */
  229. #define N_HCI 15 /* Bluetooth HCI UART */
  230. #define N_GIGASET_M101 16 /* Siemens Gigaset M101 serial DECT adapter */
  231. #define N_SLCAN 17 /* Serial / USB serial CAN Adaptors */
  232. #define N_PPS 18 /* Pulse per Second */
  233. #define N_V253 19 /* Codec control over voice modem */
  234. #define N_CAIF 20 /* CAIF protocol for talking to modems */
  235. #define N_GSM0710 21 /* GSM 0710 Mux */
  236. #define N_TI_WL 22 /* for TI's WL BT, FM, GPS combo chips */
  237. #define N_TRACESINK 23 /* Trace data routing for MIPI P1149.7 */
  238. #define N_TRACEROUTER 24 /* Trace data routing for MIPI P1149.7 */
  239. #define N_NCI 25 /* NFC NCI UART */
  240. /* Used for packet mode */
  241. #define TIOCPKT_DATA 0
  242. #define TIOCPKT_FLUSHREAD 1
  243. #define TIOCPKT_FLUSHWRITE 2
  244. #define TIOCPKT_STOP 4
  245. #define TIOCPKT_START 8
  246. #define TIOCPKT_NOSTOP 16
  247. #define TIOCPKT_DOSTOP 32
  248. /* tty driver types */
  249. #define TTY_DRIVER_TYPE_SYSTEM 0x0001
  250. #define TTY_DRIVER_TYPE_CONSOLE 0x0002
  251. #define TTY_DRIVER_TYPE_SERIAL 0x0003
  252. #define TTY_DRIVER_TYPE_PTY 0x0004
  253. #define TTY_DRIVER_TYPE_SCC 0x0005 /* scc driver */
  254. #define TTY_DRIVER_TYPE_SYSCONS 0x0006
  255. /* pty subtypes */
  256. #define PTY_TYPE_MASTER 0x0001
  257. #define PTY_TYPE_SLAVE 0x0002
  258. /* serial subtype definitions */
  259. #define SERIAL_TYPE_NORMAL 1
  260. #define max(a, b) ({\
  261. typeof(a) _a = a;\
  262. typeof(b) _b = b;\
  263. _a > _b ? _a : _b; })
  264. #define min(a, b) ({\
  265. typeof(a) _a = a;\
  266. typeof(b) _b = b;\
  267. _a < _b ? _a : _b; })
  268. void tty_set_fops(struct dfs_file_ops *fops);
  269. void console_set_fops(struct dfs_file_ops *fops);
  270. void mutex_lock(rt_mutex_t mutex);
  271. void mutex_unlock(rt_mutex_t mutex);
  272. int __tty_check_change(struct tty_struct *tty, int sig);
  273. int tty_check_change(struct tty_struct *tty);
  274. rt_inline struct rt_wqueue *wait_queue_get(struct rt_lwp *lwp, struct tty_struct *tty)
  275. {
  276. if (lwp == RT_NULL)
  277. {
  278. return &tty->wait_queue;
  279. }
  280. return &lwp->wait_queue;
  281. }
  282. rt_inline struct rt_wqueue *wait_queue_current_get(struct rt_lwp *lwp, struct tty_struct *tty)
  283. {
  284. return wait_queue_get(lwp, tty);
  285. }
  286. rt_inline void tty_wakeup_check(struct tty_struct *tty)
  287. {
  288. struct rt_wqueue *wq = NULL;
  289. wq = wait_queue_current_get(tty->foreground, tty);
  290. rt_wqueue_wakeup(wq, (void*)POLLIN);
  291. }
  292. rt_inline int set_bit(int nr,int *addr)
  293. {
  294. int mask, retval, level;
  295. addr += nr >> 5;
  296. mask = 1 << (nr & 0x1f);
  297. level = rt_hw_interrupt_disable();
  298. retval = (mask & *addr) != 0;
  299. *addr |= mask;
  300. rt_hw_interrupt_enable(level);
  301. return retval;
  302. }
  303. rt_inline int clear_bit(int nr, int *addr)
  304. {
  305. int mask, retval, level;
  306. addr += nr >> 5;
  307. mask = 1 << (nr & 0x1f);
  308. level = rt_hw_interrupt_disable();
  309. retval = (mask & *addr) != 0;
  310. *addr &= ~mask;
  311. rt_hw_interrupt_enable(level);
  312. return retval;
  313. }
  314. rt_inline int test_bit(int nr, int *addr)
  315. {
  316. int mask;
  317. addr += nr >> 5;
  318. mask = 1 << (nr & 0x1f);
  319. return ((mask & *addr) != 0);
  320. }
  321. rt_inline int test_and_clear_bit(int nr, volatile void *addr)
  322. {
  323. int mask, retval, level;
  324. volatile unsigned int *a = addr;
  325. a += nr >> 5;
  326. mask = 1 << (nr & 0x1f);
  327. level = rt_hw_interrupt_disable();
  328. retval = (mask & *a) != 0;
  329. *a &= ~mask;
  330. rt_hw_interrupt_enable(level);
  331. return retval;
  332. }
  333. rt_inline unsigned long __ffs(unsigned long word)
  334. {
  335. int num = 0;
  336. #if BITS_PER_LONG == 64
  337. if ((word & 0xffffffff) == 0)
  338. {
  339. num += 32;
  340. word >>= 32;
  341. }
  342. #endif
  343. if ((word & 0xffff) == 0)
  344. {
  345. num += 16;
  346. word >>= 16;
  347. }
  348. if ((word & 0xff) == 0)
  349. {
  350. num += 8;
  351. word >>= 8;
  352. }
  353. if ((word & 0xf) == 0)
  354. {
  355. num += 4;
  356. word >>= 4;
  357. }
  358. if ((word & 0x3) == 0)
  359. {
  360. num += 2;
  361. word >>= 2;
  362. }
  363. if ((word & 0x1) == 0)
  364. {
  365. num += 1;
  366. }
  367. return num;
  368. }
  369. #define BITS_PER_LONG 32
  370. #define BITOP_WORD(nr) ((nr) / BITS_PER_LONG)
  371. /*
  372. * Find the next set bit in a memory region.
  373. */
  374. rt_inline unsigned long find_next_bit(const unsigned long *addr, unsigned long size,
  375. unsigned long offset)
  376. {
  377. const unsigned long *p = addr + BITOP_WORD(offset);
  378. unsigned long result = offset & ~(BITS_PER_LONG-1);
  379. unsigned long tmp;
  380. if (offset >= size)
  381. {
  382. return size;
  383. }
  384. size -= result;
  385. offset %= BITS_PER_LONG;
  386. if (offset)
  387. {
  388. tmp = *(p++);
  389. tmp &= (~0UL << offset);
  390. if (size < BITS_PER_LONG)
  391. {
  392. goto found_first;
  393. }
  394. if (tmp)
  395. {
  396. goto found_middle;
  397. }
  398. size -= BITS_PER_LONG;
  399. result += BITS_PER_LONG;
  400. }
  401. while (size & ~(BITS_PER_LONG-1))
  402. {
  403. if ((tmp = *(p++)))
  404. {
  405. goto found_middle;
  406. }
  407. result += BITS_PER_LONG;
  408. size -= BITS_PER_LONG;
  409. }
  410. if (!size)
  411. {
  412. return result;
  413. }
  414. tmp = *p;
  415. found_first:
  416. tmp &= (~0UL >> (BITS_PER_LONG - size));
  417. if (tmp == 0UL) /* Are any bits set? */
  418. {
  419. return result + size; /* Nope. */
  420. }
  421. found_middle:
  422. return result + __ffs(tmp);
  423. }
  424. /*create by tty_ioctl.c*/
  425. int n_tty_ioctl_extend(struct tty_struct *tty, int cmd, void *arg);
  426. /*create by n_tty.c*/
  427. void console_ldata_init(struct tty_struct *tty);
  428. int n_tty_receive_buf(struct tty_struct *tty, char *cp, int count);
  429. void n_tty_init(void);
  430. #endif /*__TTY_H__*/