1
0

n_tty.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217
  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. #include <stddef.h>
  11. #include <ctype.h>
  12. #include <tty.h>
  13. #include <lwp.h>
  14. #if defined(RT_USING_POSIX_TERMIOS)
  15. #include <termios.h>
  16. #endif
  17. #include <sys/errno.h>
  18. #define DBG_TAG "N_TTY"
  19. #ifdef RT_TTY_DEBUG
  20. #define DBG_LVL DBG_LOG
  21. #else
  22. #define DBG_LVL DBG_INFO
  23. #endif /* RT_TTY_DEBUG */
  24. #include <rtdbg.h>
  25. /* number of characters left in xmit buffer before select has we have room */
  26. #define WAKEUP_CHARS 256
  27. /*
  28. * This defines the low- and high-watermarks for throttling and
  29. * unthrottling the TTY driver. These watermarks are used for
  30. * controlling the space in the read buffer.
  31. */
  32. #define TTY_THRESHOLD_THROTTLE 128 /* now based on remaining room */
  33. #define TTY_THRESHOLD_UNTHROTTLE 128
  34. /*
  35. * Special byte codes used in the echo buffer to represent operations
  36. * or special handling of characters. Bytes in the echo buffer that
  37. * are not part of such special blocks are treated as normal character
  38. * codes.
  39. */
  40. #define ECHO_OP_START 0xff
  41. #define ECHO_OP_MOVE_BACK_COL 0x80
  42. #define ECHO_OP_SET_CANON_COL 0x81
  43. #define ECHO_OP_ERASE_TAB 0x82
  44. #define ECHO_COMMIT_WATERMARK 256
  45. #define ECHO_BLOCK 256
  46. #define ECHO_DISCARD_WATERMARK RT_TTY_BUF - (ECHO_BLOCK + 32)
  47. struct n_tty_data
  48. {
  49. /* producer-published */
  50. size_t read_head;
  51. size_t commit_head;
  52. size_t canon_head;
  53. size_t echo_head;
  54. size_t echo_commit;
  55. size_t echo_mark;
  56. unsigned long char_map[256];
  57. /* non-atomic */
  58. rt_bool_t no_room;
  59. /* must hold exclusive termios_rwsem to reset these */
  60. unsigned char lnext:1, erasing:1, raw:1, real_raw:1, icanon:1;
  61. unsigned char push:1;
  62. /* shared by producer and consumer */
  63. char read_buf[RT_TTY_BUF];
  64. unsigned long read_flags[RT_TTY_BUF];
  65. unsigned char echo_buf[RT_TTY_BUF];
  66. /* consumer-published */
  67. size_t read_tail;
  68. size_t line_start;
  69. /* protected by output lock */
  70. unsigned int column;
  71. unsigned int canon_column;
  72. size_t echo_tail;
  73. rt_mutex_t atomic_read_lock;
  74. rt_mutex_t output_lock;
  75. };
  76. rt_inline int set_bit(int nr,int *addr)
  77. {
  78. int mask, retval, level;
  79. addr += nr >> 5;
  80. mask = 1 << (nr & 0x1f);
  81. level = rt_hw_interrupt_disable();
  82. retval = (mask & *addr) != 0;
  83. *addr |= mask;
  84. rt_hw_interrupt_enable(level);
  85. return retval;
  86. }
  87. rt_inline int clear_bit(int nr, int *addr)
  88. {
  89. int mask, retval, level;
  90. addr += nr >> 5;
  91. mask = 1 << (nr & 0x1f);
  92. level = rt_hw_interrupt_disable();
  93. retval = (mask & *addr) != 0;
  94. *addr &= ~mask;
  95. rt_hw_interrupt_enable(level);
  96. return retval;
  97. }
  98. rt_inline int test_bit(int nr, int *addr)
  99. {
  100. int mask;
  101. addr += nr >> 5;
  102. mask = 1 << (nr & 0x1f);
  103. return ((mask & *addr) != 0);
  104. }
  105. rt_inline int test_and_clear_bit(int nr, volatile void *addr)
  106. {
  107. int mask, retval, level;
  108. volatile unsigned int *a = addr;
  109. a += nr >> 5;
  110. mask = 1 << (nr & 0x1f);
  111. level = rt_hw_interrupt_disable();
  112. retval = (mask & *a) != 0;
  113. *a &= ~mask;
  114. rt_hw_interrupt_enable(level);
  115. return retval;
  116. }
  117. #ifdef __GNUC__
  118. rt_inline unsigned long __ffs(unsigned long word)
  119. {
  120. return __builtin_ffsl(word);
  121. }
  122. #else
  123. rt_inline unsigned long __ffs(unsigned long word)
  124. {
  125. int num = 0;
  126. #if BITS_PER_LONG == 64
  127. if ((word & 0xffffffff) == 0)
  128. {
  129. num += 32;
  130. word >>= 32;
  131. }
  132. #endif
  133. if ((word & 0xffff) == 0)
  134. {
  135. num += 16;
  136. word >>= 16;
  137. }
  138. if ((word & 0xff) == 0)
  139. {
  140. num += 8;
  141. word >>= 8;
  142. }
  143. if ((word & 0xf) == 0)
  144. {
  145. num += 4;
  146. word >>= 4;
  147. }
  148. if ((word & 0x3) == 0)
  149. {
  150. num += 2;
  151. word >>= 2;
  152. }
  153. if ((word & 0x1) == 0)
  154. {
  155. num += 1;
  156. }
  157. return num;
  158. }
  159. #endif
  160. #define BITS_PER_LONG 32
  161. #define BITOP_WORD(nr) ((nr) / BITS_PER_LONG)
  162. /*
  163. * Find the next set bit in a memory region.
  164. */
  165. rt_inline unsigned long find_next_bit(const unsigned long *addr, unsigned long size,
  166. unsigned long offset)
  167. {
  168. const unsigned long *p = addr + BITOP_WORD(offset);
  169. unsigned long result = offset & ~(BITS_PER_LONG-1);
  170. unsigned long tmp;
  171. if (offset >= size)
  172. {
  173. return size;
  174. }
  175. size -= result;
  176. offset %= BITS_PER_LONG;
  177. if (offset)
  178. {
  179. tmp = *(p++);
  180. tmp &= (~0UL << offset);
  181. if (size < BITS_PER_LONG)
  182. {
  183. goto found_first;
  184. }
  185. if (tmp)
  186. {
  187. goto found_middle;
  188. }
  189. size -= BITS_PER_LONG;
  190. result += BITS_PER_LONG;
  191. }
  192. while (size & ~(BITS_PER_LONG-1))
  193. {
  194. if ((tmp = *(p++)))
  195. {
  196. goto found_middle;
  197. }
  198. result += BITS_PER_LONG;
  199. size -= BITS_PER_LONG;
  200. }
  201. if (!size)
  202. {
  203. return result;
  204. }
  205. tmp = *p;
  206. found_first:
  207. tmp &= (~0UL >> (BITS_PER_LONG - size));
  208. if (tmp == 0UL) /* Are any bits set? */
  209. {
  210. return result + size; /* Nope. */
  211. }
  212. found_middle:
  213. return result + __ffs(tmp);
  214. }
  215. rt_inline void tty_sigaddset(lwp_sigset_t *set, int _sig)
  216. {
  217. unsigned long sig = _sig - 1;
  218. if (_LWP_NSIG_WORDS == 1)
  219. {
  220. set->sig[0] |= 1UL << sig;
  221. }
  222. else
  223. {
  224. set->sig[sig / _LWP_NSIG_BPW] |= 1UL << (sig % _LWP_NSIG_BPW);
  225. }
  226. }
  227. rt_inline size_t read_cnt(struct n_tty_data *ldata)
  228. {
  229. return ldata->read_head - ldata->read_tail;
  230. }
  231. rt_inline char read_buf(struct n_tty_data *ldata, size_t i)
  232. {
  233. return ldata->read_buf[i & (RT_TTY_BUF - 1)];
  234. }
  235. rt_inline char *read_buf_addr(struct n_tty_data *ldata, size_t i)
  236. {
  237. return &ldata->read_buf[i & (RT_TTY_BUF - 1)];
  238. }
  239. rt_inline unsigned char echo_buf(struct n_tty_data *ldata, size_t i)
  240. {
  241. return ldata->echo_buf[i & (RT_TTY_BUF - 1)];
  242. }
  243. rt_inline unsigned char *echo_buf_addr(struct n_tty_data *ldata, size_t i)
  244. {
  245. return &ldata->echo_buf[i & (RT_TTY_BUF - 1)];
  246. }
  247. /**
  248. * put_tty_queue - add character to tty
  249. * @c: character
  250. * @ldata: n_tty data
  251. *
  252. * Add a character to the tty read_buf queue.
  253. *
  254. * n_tty_receive_buf()/producer path:
  255. * caller holds non-exclusive termios_rwsem
  256. */
  257. rt_inline void put_tty_queue(unsigned char c, struct n_tty_data *ldata)
  258. {
  259. *read_buf_addr(ldata, ldata->read_head) = c;
  260. ldata->read_head++;
  261. }
  262. /**
  263. * reset_buffer_flags - reset buffer state
  264. * @tty: terminal to reset
  265. *
  266. * Reset the read buffer counters and clear the flags.
  267. * Called from n_tty_open() and n_tty_flush_buffer().
  268. *
  269. * Locking: caller holds exclusive termios_rwsem
  270. * (or locking is not required)
  271. */
  272. static void reset_buffer_flags(struct n_tty_data *ldata)
  273. {
  274. ldata->read_head = ldata->canon_head = ldata->read_tail = 0;
  275. ldata->echo_head = ldata->echo_tail = ldata->echo_commit = 0;
  276. ldata->commit_head = 0;
  277. ldata->echo_mark = 0;
  278. ldata->line_start = 0;
  279. ldata->erasing = 0;
  280. rt_memset(ldata->read_flags, 0, RT_TTY_BUF);
  281. ldata->push = 0;
  282. }
  283. /**
  284. * add_echo_byte - add a byte to the echo buffer
  285. * @c: unicode byte to echo
  286. * @ldata: n_tty data
  287. *
  288. * Add a character or operation byte to the echo buffer.
  289. */
  290. rt_inline void add_echo_byte(unsigned char c, struct n_tty_data *ldata)
  291. {
  292. *echo_buf_addr(ldata, ldata->echo_head++) = c;
  293. }
  294. /**
  295. * echo_move_back_col - add operation to move back a column
  296. * @ldata: n_tty data
  297. *
  298. * Add an operation to the echo buffer to move back one column.
  299. */
  300. static void echo_move_back_col(struct n_tty_data *ldata)
  301. {
  302. add_echo_byte(ECHO_OP_START, ldata);
  303. add_echo_byte(ECHO_OP_MOVE_BACK_COL, ldata);
  304. }
  305. /**
  306. * echo_set_canon_col - add operation to set the canon column
  307. * @ldata: n_tty data
  308. *
  309. * Add an operation to the echo buffer to set the canon column
  310. * to the current column.
  311. */
  312. static void echo_set_canon_col(struct n_tty_data *ldata)
  313. {
  314. add_echo_byte(ECHO_OP_START, ldata);
  315. add_echo_byte(ECHO_OP_SET_CANON_COL, ldata);
  316. }
  317. /**
  318. * echo_erase_tab - add operation to erase a tab
  319. * @num_chars: number of character columns already used
  320. * @after_tab: true if num_chars starts after a previous tab
  321. * @ldata: n_tty data
  322. *
  323. * Add an operation to the echo buffer to erase a tab.
  324. *
  325. * Called by the eraser function, which knows how many character
  326. * columns have been used since either a previous tab or the start
  327. * of input. This information will be used later, along with
  328. * canon column (if applicable), to go back the correct number
  329. * of columns.
  330. */
  331. static void echo_erase_tab(unsigned int num_chars, int after_tab,
  332. struct n_tty_data *ldata)
  333. {
  334. add_echo_byte(ECHO_OP_START, ldata);
  335. add_echo_byte(ECHO_OP_ERASE_TAB, ldata);
  336. /* We only need to know this modulo 8 (tab spacing) */
  337. num_chars &= 7;
  338. /* Set the high bit as a flag if num_chars is after a previous tab */
  339. if (after_tab)
  340. {
  341. num_chars |= 0x80;
  342. }
  343. add_echo_byte(num_chars, ldata);
  344. }
  345. /**
  346. * echo_char_raw - echo a character raw
  347. * @c: unicode byte to echo
  348. * @tty: terminal device
  349. *
  350. * Echo user input back onto the screen. This must be called only when
  351. * L_ECHO(tty) is true. Called from the driver receive_buf path.
  352. *
  353. * This variant does not treat control characters specially.
  354. */
  355. static void echo_char_raw(unsigned char c, struct n_tty_data *ldata)
  356. {
  357. if (c == ECHO_OP_START)
  358. {
  359. add_echo_byte(ECHO_OP_START, ldata);
  360. add_echo_byte(ECHO_OP_START, ldata);
  361. }
  362. else
  363. {
  364. add_echo_byte(c, ldata);
  365. }
  366. }
  367. /**
  368. * echo_char - echo a character
  369. * @c: unicode byte to echo
  370. * @tty: terminal device
  371. *
  372. * Echo user input back onto the screen. This must be called only when
  373. * L_ECHO(tty) is true. Called from the driver receive_buf path.
  374. *
  375. * This variant tags control characters to be echoed as "^X"
  376. * (where X is the letter representing the control char).
  377. */
  378. static void echo_char(unsigned char c, struct tty_struct *tty)
  379. {
  380. struct n_tty_data *ldata = tty->disc_data;
  381. if (c == ECHO_OP_START)
  382. {
  383. add_echo_byte(ECHO_OP_START, ldata);
  384. add_echo_byte(ECHO_OP_START, ldata);
  385. }
  386. else
  387. {
  388. if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t')
  389. {
  390. add_echo_byte(ECHO_OP_START, ldata);
  391. }
  392. add_echo_byte(c, ldata);
  393. }
  394. }
  395. /**
  396. * finish_erasing - complete erase
  397. * @ldata: n_tty data
  398. */
  399. rt_inline void finish_erasing(struct n_tty_data *ldata)
  400. {
  401. if (ldata->erasing)
  402. {
  403. echo_char_raw('/', ldata);
  404. ldata->erasing = 0;
  405. }
  406. }
  407. /**
  408. * is_utf8_continuation - utf8 multibyte check
  409. * @c: byte to check
  410. *
  411. * Returns true if the utf8 character 'c' is a multibyte continuation
  412. * character. We use this to correctly compute the on screen size
  413. * of the character when printing
  414. */
  415. rt_inline int is_utf8_continuation(unsigned char c)
  416. {
  417. return (c & 0xc0) == 0x80;
  418. }
  419. /**
  420. * is_continuation - multibyte check
  421. * @c: byte to check
  422. *
  423. * Returns true if the utf8 character 'c' is a multibyte continuation
  424. * character and the terminal is in unicode mode.
  425. */
  426. rt_inline int is_continuation(unsigned char c, struct tty_struct *tty)
  427. {
  428. return I_IUTF8(tty) && is_utf8_continuation(c);
  429. }
  430. /**
  431. * eraser - handle erase function
  432. * @c: character input
  433. * @tty: terminal device
  434. *
  435. * Perform erase and necessary output when an erase character is
  436. * present in the stream from the driver layer. Handles the complexities
  437. * of UTF-8 multibyte symbols.
  438. *
  439. * n_tty_receive_buf()/producer path:
  440. * caller holds non-exclusive termios_rwsem
  441. */
  442. static void eraser(unsigned char c, struct tty_struct *tty)
  443. {
  444. struct n_tty_data *ldata = tty->disc_data;
  445. enum { KERASE, WERASE, KILL } kill_type;
  446. size_t head = 0;
  447. size_t cnt = 0;
  448. int seen_alnums = 0;
  449. if (ldata->read_head == ldata->canon_head)
  450. {
  451. /* process_output('\a', tty); */ /* what do you think? */
  452. return;
  453. }
  454. if (c == ERASE_CHAR(tty))
  455. {
  456. kill_type = KERASE;
  457. }
  458. else if (c == WERASE_CHAR(tty))
  459. {
  460. kill_type = WERASE;
  461. }
  462. else
  463. {
  464. if (!L_ECHO(tty))
  465. {
  466. ldata->read_head = ldata->canon_head;
  467. return;
  468. }
  469. if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty))
  470. {
  471. ldata->read_head = ldata->canon_head;
  472. finish_erasing(ldata);
  473. echo_char(KILL_CHAR(tty), tty);
  474. /* Add a newline if ECHOK is on and ECHOKE is off. */
  475. if (L_ECHOK(tty))
  476. {
  477. echo_char_raw('\n', ldata);
  478. }
  479. return;
  480. }
  481. kill_type = KILL;
  482. }
  483. seen_alnums = 0;
  484. while (ldata->read_head != ldata->canon_head)
  485. {
  486. head = ldata->read_head;
  487. /* erase a single possibly multibyte character */
  488. do
  489. {
  490. head--;
  491. c = read_buf(ldata, head);
  492. } while (is_continuation(c, tty) && head != ldata->canon_head);
  493. /* do not partially erase */
  494. if (is_continuation(c, tty))
  495. {
  496. break;
  497. }
  498. if (kill_type == WERASE)
  499. {
  500. /* Equivalent to BSD's ALTWERASE. */
  501. if (isalnum(c) || c == '_')
  502. {
  503. seen_alnums++;
  504. }
  505. else if (seen_alnums)
  506. {
  507. break;
  508. }
  509. }
  510. cnt = ldata->read_head - head;
  511. ldata->read_head = head;
  512. if (L_ECHO(tty))
  513. {
  514. if (L_ECHOPRT(tty))
  515. {
  516. if (!ldata->erasing)
  517. {
  518. echo_char_raw('\\', ldata);
  519. ldata->erasing = 1;
  520. }
  521. /* if cnt > 1, output a multi-byte character */
  522. echo_char(c, tty);
  523. while (--cnt > 0)
  524. {
  525. head++;
  526. echo_char_raw(read_buf(ldata, head), ldata);
  527. echo_move_back_col(ldata);
  528. }
  529. }
  530. else if (kill_type == KERASE && !L_ECHOE(tty))
  531. {
  532. echo_char(ERASE_CHAR(tty), tty);
  533. }
  534. else if (c == '\t')
  535. {
  536. unsigned int num_chars = 0;
  537. int after_tab = 0;
  538. size_t tail = ldata->read_head;
  539. /*
  540. * Count the columns used for characters
  541. * since the start of input or after a
  542. * previous tab.
  543. * This info is used to go back the correct
  544. * number of columns.
  545. */
  546. while (tail != ldata->canon_head)
  547. {
  548. tail--;
  549. c = read_buf(ldata, tail);
  550. if (c == '\t')
  551. {
  552. after_tab = 1;
  553. break;
  554. }
  555. else if (iscntrl(c))
  556. {
  557. if (L_ECHOCTL(tty))
  558. {
  559. num_chars += 2;
  560. }
  561. }
  562. else if (!is_continuation(c, tty))
  563. {
  564. num_chars++;
  565. }
  566. }
  567. echo_erase_tab(num_chars, after_tab, ldata);
  568. }
  569. else
  570. {
  571. if (iscntrl(c) && L_ECHOCTL(tty))
  572. {
  573. echo_char_raw('\b', ldata);
  574. echo_char_raw(' ', ldata);
  575. echo_char_raw('\b', ldata);
  576. }
  577. if (!iscntrl(c) || L_ECHOCTL(tty))
  578. {
  579. echo_char_raw('\b', ldata);
  580. echo_char_raw(' ', ldata);
  581. echo_char_raw('\b', ldata);
  582. }
  583. }
  584. }
  585. if (kill_type == KERASE)
  586. {
  587. break;
  588. }
  589. }
  590. if (ldata->read_head == ldata->canon_head && L_ECHO(tty))
  591. {
  592. finish_erasing(ldata);
  593. }
  594. }
  595. /**
  596. * isig - handle the ISIG optio
  597. * @sig: signal
  598. * @tty: terminal
  599. *
  600. * Called when a signal is being sent due to terminal input.
  601. * Called from the driver receive_buf path so serialized.
  602. *
  603. * Performs input and output flush if !NOFLSH. In this context, the echo
  604. * buffer is 'output'. The signal is processed first to alert any current
  605. * readers or writers to discontinue and exit their i/o loops.
  606. *
  607. * Locking: ctrl_lock
  608. */
  609. static void __isig(int sig, struct tty_struct *tty)
  610. {
  611. struct rt_lwp *lwp = tty->foreground;
  612. struct tty_ldisc *ld = RT_NULL;
  613. struct termios old_termios;
  614. struct termios *new_termios = get_old_termios();
  615. if (lwp)
  616. {
  617. if (sig == SIGTSTP)
  618. {
  619. struct rt_lwp *old_lwp;
  620. rt_memcpy(&old_termios, &(tty->init_termios), sizeof(struct termios));
  621. tty->init_termios = *new_termios;
  622. ld = tty->ldisc;
  623. if (ld != RT_NULL)
  624. {
  625. if (ld->ops->set_termios)
  626. {
  627. ld->ops->set_termios(tty, &old_termios);
  628. }
  629. }
  630. lwp_signal_kill(lwp, SIGTTOU, SI_USER, 0);
  631. old_lwp = tty_pop(&tty->head, RT_NULL);
  632. tty->foreground = old_lwp;
  633. }
  634. else
  635. {
  636. lwp_signal_kill(lwp, sig, SI_USER, 0);
  637. }
  638. }
  639. }
  640. static void isig(int sig, struct tty_struct *tty)
  641. {
  642. struct n_tty_data *ldata = tty->disc_data;
  643. if (L_NOFLSH(tty))
  644. {
  645. /* signal only */
  646. __isig(sig, tty);
  647. }
  648. else
  649. { /* signal and flush */
  650. __isig(sig, tty);
  651. /* clear echo buffer */
  652. ldata->echo_head = ldata->echo_tail = 0;
  653. ldata->echo_mark = ldata->echo_commit = 0;
  654. /* clear input buffer */
  655. reset_buffer_flags(tty->disc_data);
  656. }
  657. }
  658. /**
  659. * do_output_char - output one character
  660. * @c: character (or partial unicode symbol)
  661. * @tty: terminal device
  662. * @space: space available in tty driver write buffer
  663. *
  664. * This is a helper function that handles one output character
  665. * (including special characters like TAB, CR, LF, etc.),
  666. * doing OPOST processing and putting the results in the
  667. * tty driver's write buffer.
  668. *
  669. * Note that Linux currently ignores TABDLY, CRDLY, VTDLY, FFDLY
  670. * and NLDLY. They simply aren't relevant in the world today.
  671. * If you ever need them, add them here.
  672. *
  673. * Returns the number of bytes of buffer space used or -1 if
  674. * no space left.
  675. *
  676. * Locking: should be called under the output_lock to protect
  677. * the column state and space left in the buffer
  678. */
  679. static int do_output_char(unsigned char c, struct tty_struct *tty)
  680. {
  681. struct n_tty_data *ldata = tty->disc_data;
  682. int spaces = 0;
  683. char *ch = RT_NULL;
  684. switch (c)
  685. {
  686. case '\n':
  687. if (O_ONLRET(tty))
  688. {
  689. ldata->column = 0;
  690. }
  691. if (O_ONLCR(tty))
  692. {
  693. ldata->canon_column = ldata->column = 0;
  694. ch = "\r\n";
  695. rt_device_write((rt_device_t)tty, -1, ch, 2);
  696. return 2;
  697. }
  698. ldata->canon_column = ldata->column;
  699. break;
  700. case '\r':
  701. if (O_ONOCR(tty) && ldata->column == 0)
  702. {
  703. return 0;
  704. }
  705. if (O_OCRNL(tty))
  706. {
  707. c = '\n';
  708. if (O_ONLRET(tty))
  709. {
  710. ldata->canon_column = ldata->column = 0;
  711. }
  712. break;
  713. }
  714. ldata->canon_column = ldata->column = 0;
  715. break;
  716. case '\t':
  717. spaces = 8 - (ldata->column & 7);
  718. if (O_TABDLY(tty) == XTABS)
  719. {
  720. ldata->column += spaces;
  721. ch = " ";
  722. rt_device_write((rt_device_t)tty, -1, &ch, spaces);
  723. return spaces;
  724. }
  725. ldata->column += spaces;
  726. break;
  727. case '\b':
  728. if (ldata->column > 0)
  729. {
  730. ldata->column--;
  731. }
  732. ch = "\b \b";
  733. rt_device_write((rt_device_t)tty, -1, ch, strlen(ch));
  734. return 1;
  735. default:
  736. if (!iscntrl(c))
  737. {
  738. if (O_OLCUC(tty))
  739. {
  740. c = toupper(c);
  741. }
  742. if (!is_continuation(c, tty))
  743. {
  744. ldata->column++;
  745. }
  746. }
  747. break;
  748. }
  749. rt_device_write((rt_device_t)tty, -1, &c, 1);
  750. return 1;
  751. }
  752. /**
  753. * process_output - output post processor
  754. * @c: character (or partial unicode symbol)
  755. * @tty: terminal device
  756. *
  757. * Output one character with OPOST processing.
  758. * Returns -1 when the output device is full and the character
  759. * must be retried.
  760. *
  761. * Locking: output_lock to protect column state and space left
  762. * (also, this is called from n_tty_write under the
  763. * tty layer write lock)
  764. */
  765. static int process_output(unsigned char c, struct tty_struct *tty)
  766. {
  767. int retval = 0;
  768. retval = do_output_char(c, tty);
  769. if (retval < 0)
  770. {
  771. return -1;
  772. }
  773. else
  774. {
  775. return 0;
  776. }
  777. }
  778. /**
  779. * process_output_block - block post processor
  780. * @tty: terminal device
  781. * @buf: character buffer
  782. * @nr: number of bytes to output
  783. *
  784. * Output a block of characters with OPOST processing.
  785. * Returns the number of characters output.
  786. *
  787. * This path is used to speed up block console writes, among other
  788. * things when processing blocks of output data. It handles only
  789. * the simple cases normally found and helps to generate blocks of
  790. * symbols for the console driver and thus improve performance.
  791. *
  792. * Locking: output_lock to protect column state and space left
  793. * (also, this is called from n_tty_write under the
  794. * tty layer write lock)
  795. */
  796. static ssize_t process_output_block(struct tty_struct *tty,
  797. const char *buf, unsigned int nr)
  798. {
  799. struct n_tty_data *ldata = tty->disc_data;
  800. int i = 0;
  801. ssize_t size = 0;
  802. const char *cp = RT_NULL;
  803. for (i = 0, cp = buf; i < nr; i++, cp++)
  804. {
  805. char c = *cp;
  806. switch (c)
  807. {
  808. case '\n':
  809. if (O_ONLRET(tty))
  810. {
  811. ldata->column = 0;
  812. }
  813. if (O_ONLCR(tty))
  814. {
  815. goto break_out;
  816. }
  817. ldata->canon_column = ldata->column;
  818. break;
  819. case '\r':
  820. if (O_ONOCR(tty) && ldata->column == 0)
  821. {
  822. goto break_out;
  823. }
  824. if (O_OCRNL(tty))
  825. {
  826. goto break_out;
  827. }
  828. ldata->canon_column = ldata->column = 0;
  829. break;
  830. case '\t':
  831. goto break_out;
  832. case '\b':
  833. if (ldata->column > 0)
  834. {
  835. ldata->column--;
  836. }
  837. break;
  838. default:
  839. if (!iscntrl(c))
  840. {
  841. if (O_OLCUC(tty))
  842. {
  843. goto break_out;
  844. }
  845. if (!is_continuation(c, tty))
  846. {
  847. ldata->column++;
  848. }
  849. }
  850. break;
  851. }
  852. }
  853. break_out:
  854. size = rt_device_write((rt_device_t)tty, -1, buf, i);
  855. return size;
  856. }
  857. static size_t __process_echoes(struct tty_struct *tty)
  858. {
  859. struct n_tty_data *ldata = tty->disc_data;
  860. size_t tail = 0;
  861. unsigned char c = 0;
  862. char ch = 0;
  863. unsigned char num_chars = 0, num_bs = 0;
  864. tail = ldata->echo_tail;
  865. while (ldata->echo_commit != tail)
  866. {
  867. c = echo_buf(ldata, tail);
  868. if (c == ECHO_OP_START)
  869. {
  870. unsigned char op = 0;
  871. /*
  872. * If the buffer byte is the start of a multi-byte
  873. * operation, get the next byte, which is either the
  874. * op code or a control character value.
  875. */
  876. op = echo_buf(ldata, tail + 1);
  877. switch (op)
  878. {
  879. case ECHO_OP_ERASE_TAB:
  880. num_chars = echo_buf(ldata, tail + 2);
  881. /*
  882. * Determine how many columns to go back
  883. * in order to erase the tab.
  884. * This depends on the number of columns
  885. * used by other characters within the tab
  886. * area. If this (modulo 8) count is from
  887. * the start of input rather than from a
  888. * previous tab, we offset by canon column.
  889. * Otherwise, tab spacing is normal.
  890. */
  891. if (!(num_chars & 0x80))
  892. {
  893. num_chars += ldata->canon_column;
  894. }
  895. num_bs = 8 - (num_chars & 7);
  896. while (num_bs--)
  897. {
  898. ch = '\b';
  899. rt_device_write((rt_device_t)tty, -1, &ch, 1);
  900. if (ldata->column > 0)
  901. {
  902. ldata->column--;
  903. }
  904. }
  905. tail += 3;
  906. break;
  907. case ECHO_OP_SET_CANON_COL:
  908. ldata->canon_column = ldata->column;
  909. tail += 2;
  910. break;
  911. case ECHO_OP_MOVE_BACK_COL:
  912. if (ldata->column > 0)
  913. {
  914. ldata->column--;
  915. }
  916. tail += 2;
  917. break;
  918. case ECHO_OP_START:
  919. ch = ECHO_OP_START;
  920. rt_device_write((rt_device_t)tty, -1, &ch, 1);
  921. ldata->column++;
  922. tail += 2;
  923. break;
  924. default:
  925. /*
  926. * If the op is not a special byte code,
  927. * it is a ctrl char tagged to be echoed
  928. * as "^X" (where X is the letter
  929. * representing the control char).
  930. * Note that we must ensure there is
  931. * enough space for the whole ctrl pair.
  932. *
  933. */
  934. ch = '^';
  935. rt_device_write((rt_device_t)tty, -1, &ch, 1);
  936. ch = op ^ 0100;
  937. rt_device_write((rt_device_t)tty, -1, &ch, 1);
  938. ldata->column += 2;
  939. tail += 2;
  940. }
  941. }
  942. else
  943. {
  944. if (O_OPOST(tty))
  945. {
  946. int retval = do_output_char(c, tty);
  947. if (retval < 0)
  948. {
  949. break;
  950. }
  951. }
  952. else
  953. {
  954. rt_device_write((rt_device_t)tty, -1, &c, 1);
  955. }
  956. tail += 1;
  957. }
  958. }
  959. /* If the echo buffer is nearly full (so that the possibility exists
  960. * of echo overrun before the next commit), then discard enough
  961. * data at the tail to prevent a subsequent overrun */
  962. while (ldata->echo_commit - tail >= ECHO_DISCARD_WATERMARK)
  963. {
  964. if (echo_buf(ldata, tail) == ECHO_OP_START)
  965. {
  966. if (echo_buf(ldata, tail + 1) == ECHO_OP_ERASE_TAB)
  967. {
  968. tail += 3;
  969. }
  970. else
  971. {
  972. tail += 2;
  973. }
  974. }
  975. else
  976. {
  977. tail++;
  978. }
  979. }
  980. ldata->echo_tail = tail;
  981. return 0;
  982. }
  983. static void commit_echoes(struct tty_struct *tty)
  984. {
  985. struct n_tty_data *ldata = tty->disc_data;
  986. size_t nr = 0, old = 0;
  987. size_t head = 0;
  988. head = ldata->echo_head;
  989. ldata->echo_mark = head;
  990. old = ldata->echo_commit - ldata->echo_tail;
  991. /* Process committed echoes if the accumulated # of bytes
  992. * is over the threshold (and try again each time another
  993. * block is accumulated) */
  994. nr = head - ldata->echo_tail;
  995. if (nr < ECHO_COMMIT_WATERMARK || (nr % ECHO_BLOCK > old % ECHO_BLOCK))
  996. {
  997. return;
  998. }
  999. ldata->echo_commit = head;
  1000. __process_echoes(tty);
  1001. }
  1002. static void process_echoes(struct tty_struct *tty)
  1003. {
  1004. struct n_tty_data *ldata = tty->disc_data;
  1005. if (ldata->echo_mark == ldata->echo_tail)
  1006. {
  1007. return;
  1008. }
  1009. ldata->echo_commit = ldata->echo_mark;
  1010. __process_echoes(tty);
  1011. }
  1012. /* NB: echo_mark and echo_head should be equivalent here */
  1013. static void flush_echoes(struct tty_struct *tty)
  1014. {
  1015. struct n_tty_data *ldata = tty->disc_data;
  1016. if ((!L_ECHO(tty) && !L_ECHONL(tty)) ||
  1017. ldata->echo_commit == ldata->echo_head)
  1018. {
  1019. return;
  1020. }
  1021. ldata->echo_commit = ldata->echo_head;
  1022. __process_echoes(tty);
  1023. }
  1024. /**
  1025. * n_tty_set_termios - termios data changed
  1026. * @tty: terminal
  1027. * @old: previous data
  1028. *
  1029. * Called by the tty layer when the user changes termios flags so
  1030. * that the line discipline can plan ahead. This function cannot sleep
  1031. * and is protected from re-entry by the tty layer. The user is
  1032. * guaranteed that this function will not be re-entered or in progress
  1033. * when the ldisc is closed.
  1034. *
  1035. * Locking: Caller holds tty->termios_rwsem
  1036. */
  1037. static void n_tty_set_termios(struct tty_struct *tty, struct termios *old)
  1038. {
  1039. struct n_tty_data *ldata = tty->disc_data;
  1040. if (!old || (old->c_lflag ^ tty->init_termios.c_lflag) & (ICANON | EXTPROC))
  1041. {
  1042. rt_memset(ldata->read_flags, 0, RT_TTY_BUF);
  1043. ldata->line_start = ldata->read_tail;
  1044. if (!L_ICANON(tty) || !read_cnt(ldata))
  1045. {
  1046. ldata->canon_head = ldata->read_tail;
  1047. ldata->push = 0;
  1048. }
  1049. else
  1050. {
  1051. set_bit((ldata->read_head - 1) & (RT_TTY_BUF - 1),(int *)ldata->read_flags);
  1052. ldata->canon_head = ldata->read_head;
  1053. ldata->push = 1;
  1054. }
  1055. ldata->commit_head = ldata->read_head;
  1056. ldata->erasing = 0;
  1057. ldata->lnext = 0;
  1058. }
  1059. ldata->icanon = (L_ICANON(tty) != 0);
  1060. if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
  1061. I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
  1062. I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
  1063. I_PARMRK(tty))
  1064. {
  1065. rt_memset(ldata->char_map, 0, 256);
  1066. if (I_IGNCR(tty) || I_ICRNL(tty))
  1067. {
  1068. set_bit('\r', (int *)ldata->char_map);
  1069. }
  1070. if (I_INLCR(tty))
  1071. {
  1072. set_bit('\n', (int *)ldata->char_map);
  1073. }
  1074. if (L_ICANON(tty))
  1075. {
  1076. set_bit(ERASE_CHAR(tty), (int *)ldata->char_map);
  1077. set_bit(KILL_CHAR(tty),(int *) ldata->char_map);
  1078. set_bit(EOF_CHAR(tty), (int *)ldata->char_map);
  1079. set_bit('\n',(int *) ldata->char_map);
  1080. set_bit(EOL_CHAR(tty),(int *) ldata->char_map);
  1081. if (L_IEXTEN(tty))
  1082. {
  1083. set_bit(WERASE_CHAR(tty), (int *)ldata->char_map);
  1084. set_bit(LNEXT_CHAR(tty), (int *)ldata->char_map);
  1085. set_bit(EOL2_CHAR(tty), (int *)ldata->char_map);
  1086. if (L_ECHO(tty))
  1087. {
  1088. set_bit(REPRINT_CHAR(tty),
  1089. (int *)ldata->char_map);
  1090. }
  1091. }
  1092. }
  1093. if (I_IXON(tty))
  1094. {
  1095. set_bit(START_CHAR(tty), (int *)ldata->char_map);
  1096. set_bit(STOP_CHAR(tty), (int *)ldata->char_map);
  1097. }
  1098. if (L_ISIG(tty))
  1099. {
  1100. set_bit(INTR_CHAR(tty), (int *)ldata->char_map);
  1101. set_bit(QUIT_CHAR(tty), (int *)ldata->char_map);
  1102. set_bit(SUSP_CHAR(tty), (int *)ldata->char_map);
  1103. }
  1104. clear_bit(__DISABLED_CHAR, (int *)ldata->char_map);
  1105. ldata->raw = 0;
  1106. ldata->real_raw = 0;
  1107. }
  1108. else
  1109. {
  1110. ldata->raw = 1;
  1111. if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
  1112. (I_IGNPAR(tty) || !I_INPCK(tty))/* &&
  1113. (tty->driver->flags & TTY_DRIVER_REAL_RAW)*/)
  1114. {
  1115. ldata->real_raw = 1;
  1116. }
  1117. else
  1118. {
  1119. ldata->real_raw = 0;
  1120. }
  1121. }
  1122. }
  1123. void console_ldata_init(struct tty_struct *tty)
  1124. {
  1125. struct n_tty_data *ldata = RT_NULL;
  1126. ldata = rt_malloc(sizeof(struct n_tty_data));
  1127. if (ldata == RT_NULL)
  1128. {
  1129. LOG_E("console_ldata_init ldata malloc fail");
  1130. return;
  1131. }
  1132. tty->disc_data = ldata;
  1133. reset_buffer_flags(ldata);
  1134. ldata->column = 0;
  1135. ldata->canon_column = 0;
  1136. ldata->no_room = 0;
  1137. ldata->lnext = 0;
  1138. n_tty_set_termios(tty, RT_NULL);
  1139. return;
  1140. }
  1141. static int n_tty_open(struct dfs_file *fd)
  1142. {
  1143. int ret = 0;
  1144. struct n_tty_data *ldata = RT_NULL;
  1145. struct tty_struct *tty = (struct tty_struct*)fd->vnode->data;
  1146. ldata = rt_malloc(sizeof(struct n_tty_data));
  1147. if (ldata == RT_NULL)
  1148. {
  1149. LOG_E("n_tty_open ldata malloc fail");
  1150. return -1;
  1151. }
  1152. ldata->atomic_read_lock = rt_mutex_create("atomic_read_lock",RT_IPC_FLAG_FIFO);
  1153. if(ldata->atomic_read_lock == RT_NULL)
  1154. {
  1155. LOG_E("n_tty_open atomic_read_lock create fail");
  1156. return -1;
  1157. }
  1158. ldata->output_lock = rt_mutex_create("output_lock",RT_IPC_FLAG_FIFO);
  1159. if(ldata->output_lock == RT_NULL)
  1160. {
  1161. LOG_E("n_tty_open output_lock create fail");
  1162. return -1;
  1163. }
  1164. tty->disc_data = ldata;
  1165. reset_buffer_flags(ldata);
  1166. ldata->column = 0;
  1167. ldata->canon_column = 0;
  1168. ldata->lnext = 0;
  1169. n_tty_set_termios(tty, RT_NULL);
  1170. return ret;
  1171. }
  1172. rt_inline int input_available_p(struct tty_struct *tty, int poll)
  1173. {
  1174. struct n_tty_data *ldata = tty->disc_data;
  1175. int amt = poll && !TIME_CHAR(tty) && MIN_CHAR(tty) ? MIN_CHAR(tty) : 1;
  1176. if (ldata->icanon && !L_EXTPROC(tty))
  1177. {
  1178. return ldata->canon_head != ldata->read_tail;
  1179. }
  1180. else
  1181. {
  1182. return ldata->commit_head - ldata->read_tail >= amt;
  1183. }
  1184. }
  1185. /**
  1186. * copy_from_read_buf - copy read data directly
  1187. * @tty: terminal device
  1188. * @b: user data
  1189. * @nr: size of data
  1190. *
  1191. * Helper function to speed up n_tty_read. It is only called when
  1192. * ICANON is off; it copies characters straight from the tty queue to
  1193. * user space directly. It can be profitably called twice; once to
  1194. * drain the space from the tail pointer to the (physical) end of the
  1195. * buffer, and once to drain the space from the (physical) beginning of
  1196. * the buffer to head pointer.
  1197. *
  1198. * Called under the ldata->atomic_read_lock sem
  1199. *
  1200. * n_tty_read()/consumer path:
  1201. * caller holds non-exclusive termios_rwsem
  1202. * read_tail published
  1203. */
  1204. static int copy_from_read_buf(struct tty_struct *tty,char *b,size_t nr)
  1205. {
  1206. struct n_tty_data *ldata = tty->disc_data;
  1207. size_t n = 0;
  1208. rt_bool_t is_eof = 0;
  1209. size_t head = ldata->commit_head;
  1210. size_t tail = ldata->read_tail & (RT_TTY_BUF - 1);
  1211. n = min(head - ldata->read_tail, RT_TTY_BUF - tail);
  1212. n = min(nr, n);
  1213. if (n)
  1214. {
  1215. const char *from = read_buf_addr(ldata, tail);
  1216. rt_memcpy(b, from, n);
  1217. is_eof = n == 1 && *from == EOF_CHAR(tty);
  1218. ldata->read_tail += n;
  1219. /* Turn single EOF into zero-length read */
  1220. if (L_EXTPROC(tty) && ldata->icanon && is_eof &&
  1221. (head == ldata->read_tail))
  1222. {
  1223. n = 0;
  1224. }
  1225. }
  1226. return n;
  1227. }
  1228. /**
  1229. * canon_copy_from_read_buf - copy read data in canonical mode
  1230. * @tty: terminal device
  1231. * @b: user data
  1232. * @nr: size of data
  1233. *
  1234. * Helper function for n_tty_read. It is only called when ICANON is on;
  1235. * it copies one line of input up to and including the line-delimiting
  1236. * character into the user-space buffer.
  1237. *
  1238. * NB: When termios is changed from non-canonical to canonical mode and
  1239. * the read buffer contains data, n_tty_set_termios() simulates an EOF
  1240. * push (as if C-d were input) _without_ the DISABLED_CHAR in the buffer.
  1241. * This causes data already processed as input to be immediately available
  1242. * as input although a newline has not been received.
  1243. *
  1244. * Called under the atomic_read_lock mutex
  1245. *
  1246. * n_tty_read()/consumer path:
  1247. * caller holds non-exclusive termios_rwsem
  1248. * read_tail published
  1249. */
  1250. static int canon_copy_from_read_buf(struct tty_struct *tty, char *b, size_t nr)
  1251. {
  1252. struct n_tty_data *ldata = tty->disc_data;
  1253. size_t n = 0, size = 0, more = 0, c = 0;
  1254. size_t eol = 0;
  1255. size_t tail = 0;
  1256. int found = 0;
  1257. /* N.B. avoid overrun if nr == 0 */
  1258. if (nr == 0)
  1259. {
  1260. return 0;
  1261. }
  1262. n = min(nr + 1, ldata->canon_head - ldata->read_tail);
  1263. tail = ldata->read_tail & (RT_TTY_BUF - 1);
  1264. size = min(tail + n, RT_TTY_BUF);
  1265. eol = find_next_bit(ldata->read_flags, size, tail);
  1266. more = n - (size - tail);
  1267. if (eol == RT_TTY_BUF && more)
  1268. {
  1269. /* scan wrapped without finding set bit */
  1270. eol = find_next_bit(ldata->read_flags, more, 0);
  1271. found = eol != more;
  1272. }
  1273. else
  1274. {
  1275. found = eol != size;
  1276. }
  1277. n = eol - tail;
  1278. if (n > RT_TTY_BUF)
  1279. {
  1280. n += RT_TTY_BUF;
  1281. }
  1282. c = n + found;
  1283. if (!found || read_buf(ldata, eol) != __DISABLED_CHAR)
  1284. {
  1285. c = min(nr, c);
  1286. n = c;
  1287. }
  1288. size_t buf_size = RT_TTY_BUF - tail;
  1289. const void *from = read_buf_addr(ldata, tail);
  1290. size_t temp_n = n;
  1291. if (n > buf_size)
  1292. {
  1293. rt_memcpy(b, from, buf_size);
  1294. b += buf_size;
  1295. temp_n -= buf_size;
  1296. from = ldata->read_buf;
  1297. }
  1298. rt_memcpy(b, from, temp_n);
  1299. if (found)
  1300. {
  1301. clear_bit(eol, (int *)ldata->read_flags);
  1302. }
  1303. ldata->read_tail = ldata->read_tail + c;
  1304. if (found)
  1305. {
  1306. if (!ldata->push)
  1307. {
  1308. ldata->line_start = ldata->read_tail;
  1309. }
  1310. else
  1311. {
  1312. ldata->push = 0;
  1313. }
  1314. }
  1315. return n;
  1316. }
  1317. static int n_tty_close(struct tty_struct *tty)
  1318. {
  1319. int ret = 0;
  1320. struct n_tty_data *ldata = RT_NULL;
  1321. struct tty_struct *o_tty = RT_NULL;
  1322. RT_ASSERT(tty != RT_NULL);
  1323. if (tty->type == TTY_DRIVER_TYPE_PTY && tty->subtype == PTY_TYPE_MASTER)
  1324. {
  1325. o_tty = tty->other_struct;
  1326. }
  1327. else
  1328. {
  1329. o_tty = tty;
  1330. }
  1331. ldata = o_tty->disc_data;
  1332. rt_free(ldata);
  1333. o_tty->disc_data = RT_NULL;
  1334. return ret;
  1335. }
  1336. static int n_tty_ioctl(struct dfs_file *fd, int cmd, void *args)
  1337. {
  1338. int ret = 0;
  1339. struct tty_struct *real_tty = RT_NULL;
  1340. struct tty_struct *tty = RT_NULL;
  1341. tty = (struct tty_struct *)fd->vnode->data;
  1342. RT_ASSERT(tty != RT_NULL);
  1343. if (tty->type == TTY_DRIVER_TYPE_PTY && tty->subtype == PTY_TYPE_MASTER)
  1344. {
  1345. real_tty = tty->other_struct;
  1346. }
  1347. else
  1348. {
  1349. real_tty = tty;
  1350. }
  1351. switch(cmd)
  1352. {
  1353. default:
  1354. ret = n_tty_ioctl_extend(real_tty, cmd, args);
  1355. if (ret != -ENOIOCTLCMD)
  1356. {
  1357. return ret;
  1358. }
  1359. }
  1360. ret = rt_device_control((rt_device_t)real_tty, cmd, args);
  1361. if (ret != -ENOIOCTLCMD)
  1362. {
  1363. return ret;
  1364. }
  1365. return ret;
  1366. }
  1367. static void
  1368. n_tty_receive_signal_char(struct tty_struct *tty, int signal, unsigned char c)
  1369. {
  1370. isig(signal, tty);
  1371. if (L_ECHO(tty))
  1372. {
  1373. echo_char(c, tty);
  1374. commit_echoes(tty);
  1375. }
  1376. else
  1377. {
  1378. process_echoes(tty);
  1379. }
  1380. return;
  1381. }
  1382. /**
  1383. * n_tty_receive_char - perform processing
  1384. * @tty: terminal device
  1385. * @c: character
  1386. *
  1387. * Process an individual character of input received from the driver.
  1388. * This is serialized with respect to itself by the rules for the
  1389. * driver above.
  1390. *
  1391. * n_tty_receive_buf()/producer path:
  1392. * caller holds non-exclusive termios_rwsem
  1393. * publishes canon_head if canonical mode is active
  1394. *
  1395. * Returns 1 if LNEXT was received, else returns 0
  1396. */
  1397. static int n_tty_receive_char_special(struct tty_struct *tty, unsigned char c)
  1398. {
  1399. struct n_tty_data *ldata = tty->disc_data;
  1400. if (I_IXON(tty))
  1401. {
  1402. if (c == START_CHAR(tty)) /*ctrl + p realize missing*/
  1403. {
  1404. process_echoes(tty);
  1405. return 0;
  1406. }
  1407. if (c == STOP_CHAR(tty)) /*ctrl + s realize missing*/
  1408. {
  1409. return 0;
  1410. }
  1411. }
  1412. if (L_ISIG(tty))
  1413. {
  1414. if (c == INTR_CHAR(tty)) /*ctrl + c realize missing*/
  1415. {
  1416. n_tty_receive_signal_char(tty, SIGINT, c);
  1417. return 0;
  1418. }
  1419. else if (c == QUIT_CHAR(tty)) /*ctrl + d realize missing*/
  1420. {
  1421. n_tty_receive_signal_char(tty, SIGQUIT, c);
  1422. return 0;
  1423. }
  1424. else if (c == SUSP_CHAR(tty)) /*ctrl + z realize missing*/
  1425. {
  1426. n_tty_receive_signal_char(tty, SIGTSTP, c);
  1427. return 0;
  1428. }
  1429. }
  1430. if (c == '\r')
  1431. {
  1432. if (I_IGNCR(tty))
  1433. {
  1434. return 0;
  1435. }
  1436. if (I_ICRNL(tty))
  1437. {
  1438. c = '\n';
  1439. }
  1440. }
  1441. else if (c == '\n' && I_INLCR(tty))
  1442. {
  1443. c = '\r';
  1444. }
  1445. if (ldata->icanon)
  1446. {
  1447. if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
  1448. (c == WERASE_CHAR(tty) && L_IEXTEN(tty)))
  1449. {
  1450. eraser(c, tty);
  1451. commit_echoes(tty);
  1452. return 0;
  1453. }
  1454. if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty))
  1455. {
  1456. ldata->lnext = 1;
  1457. if (L_ECHO(tty))
  1458. {
  1459. finish_erasing(ldata);
  1460. if (L_ECHOCTL(tty))
  1461. {
  1462. echo_char_raw('^', ldata);
  1463. echo_char_raw('\b', ldata);
  1464. commit_echoes(tty);
  1465. }
  1466. }
  1467. return 1;
  1468. }
  1469. if (c == REPRINT_CHAR(tty) && L_ECHO(tty) && L_IEXTEN(tty))
  1470. {
  1471. size_t tail = ldata->canon_head;
  1472. finish_erasing(ldata);
  1473. echo_char(c, tty);
  1474. echo_char_raw('\n', ldata);
  1475. while (tail != ldata->read_head)
  1476. {
  1477. echo_char(read_buf(ldata, tail), tty);
  1478. tail++;
  1479. }
  1480. commit_echoes(tty);
  1481. return 0;
  1482. }
  1483. if (c == '\n')
  1484. {
  1485. if (L_ECHO(tty) || L_ECHONL(tty))
  1486. {
  1487. echo_char_raw('\n', ldata);
  1488. commit_echoes(tty);
  1489. }
  1490. goto handle_newline;
  1491. }
  1492. if (c == EOF_CHAR(tty))
  1493. {
  1494. c = __DISABLED_CHAR;
  1495. goto handle_newline;
  1496. }
  1497. if ((c == EOL_CHAR(tty)) ||
  1498. (c == EOL2_CHAR(tty) && L_IEXTEN(tty)))
  1499. {
  1500. /*
  1501. * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
  1502. */
  1503. if (L_ECHO(tty))
  1504. {
  1505. /* Record the column of first canon char. */
  1506. if (ldata->canon_head == ldata->read_head)
  1507. {
  1508. echo_set_canon_col(ldata);
  1509. }
  1510. echo_char(c, tty);
  1511. commit_echoes(tty);
  1512. }
  1513. /*
  1514. * XXX does PARMRK doubling happen for
  1515. * EOL_CHAR and EOL2_CHAR?
  1516. */
  1517. if (c == (unsigned char) '\377' && I_PARMRK(tty))
  1518. {
  1519. put_tty_queue(c, ldata);
  1520. }
  1521. handle_newline:
  1522. set_bit(ldata->read_head & (RT_TTY_BUF - 1), (int *)ldata->read_flags);
  1523. put_tty_queue(c, ldata);
  1524. ldata->canon_head = ldata->read_head;
  1525. tty_wakeup_check(tty);
  1526. return 0;
  1527. }
  1528. }
  1529. if (L_ECHO(tty))
  1530. {
  1531. finish_erasing(ldata);
  1532. if (c == '\n')
  1533. {
  1534. echo_char_raw('\n', ldata);
  1535. }
  1536. else
  1537. {
  1538. /* Record the column of first canon char. */
  1539. if (ldata->canon_head == ldata->read_head)
  1540. {
  1541. echo_set_canon_col(ldata);
  1542. }
  1543. echo_char(c, tty);
  1544. }
  1545. commit_echoes(tty);
  1546. }
  1547. /* PARMRK doubling check */
  1548. if (c == (unsigned char) '\377' && I_PARMRK(tty))
  1549. {
  1550. put_tty_queue(c, ldata);
  1551. }
  1552. put_tty_queue(c, ldata);
  1553. return 0;
  1554. }
  1555. rt_inline void n_tty_receive_char_inline(struct tty_struct *tty, unsigned char c)
  1556. {
  1557. struct n_tty_data *ldata = tty->disc_data;
  1558. if (L_ECHO(tty))
  1559. {
  1560. finish_erasing(ldata);
  1561. /* Record the column of first canon char. */
  1562. if (ldata->canon_head == ldata->read_head)
  1563. {
  1564. echo_set_canon_col(ldata);
  1565. }
  1566. echo_char(c, tty);
  1567. commit_echoes(tty);
  1568. }
  1569. /* PARMRK doubling check */
  1570. if (c == (unsigned char) '\377' && I_PARMRK(tty))
  1571. {
  1572. put_tty_queue(c, ldata);
  1573. }
  1574. put_tty_queue(c, ldata);
  1575. }
  1576. static void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
  1577. {
  1578. n_tty_receive_char_inline(tty, c);
  1579. }
  1580. static void n_tty_receive_char_lnext(struct tty_struct *tty, unsigned char c, char flag)
  1581. {
  1582. struct n_tty_data *ldata = tty->disc_data;
  1583. ldata->lnext = 0;
  1584. if (flag == TTY_NORMAL)
  1585. {
  1586. if (I_ISTRIP(tty))
  1587. {
  1588. c &= 0x7f;
  1589. }
  1590. if (I_IUCLC(tty) && L_IEXTEN(tty))
  1591. {
  1592. c = tolower(c);
  1593. }
  1594. n_tty_receive_char(tty, c);
  1595. }
  1596. else
  1597. {
  1598. //n_tty_receive_char_flagged(tty, c, flag);
  1599. }
  1600. }
  1601. static void n_tty_receive_buf_real_raw(struct tty_struct *tty, char *cp, int count)
  1602. {
  1603. struct n_tty_data *ldata = tty->disc_data;
  1604. size_t n = 0, head = 0;
  1605. head = ldata->read_head & (RT_TTY_BUF - 1);
  1606. n = min(count, RT_TTY_BUF - head);
  1607. rt_memcpy(read_buf_addr(ldata, head), cp, n);
  1608. ldata->read_head += n;
  1609. cp += n;
  1610. count -= n;
  1611. head = ldata->read_head & (RT_TTY_BUF - 1);
  1612. n = min(count, RT_TTY_BUF - head);
  1613. rt_memcpy(read_buf_addr(ldata, head), cp, n);
  1614. ldata->read_head += n;
  1615. }
  1616. static void n_tty_receive_buf_raw(struct tty_struct *tty, char *cp, int count)
  1617. {
  1618. struct n_tty_data *ldata = tty->disc_data;
  1619. char flag = TTY_NORMAL;
  1620. while (count--)
  1621. {
  1622. if (flag == TTY_NORMAL)
  1623. {
  1624. put_tty_queue(*cp++, ldata);
  1625. }
  1626. }
  1627. }
  1628. static void n_tty_receive_buf_standard(struct tty_struct *tty, char *cp, int count)
  1629. {
  1630. struct n_tty_data *ldata = tty->disc_data;
  1631. char flag = TTY_NORMAL;
  1632. while (count--)
  1633. {
  1634. char c = *cp++;
  1635. if (I_ISTRIP(tty))
  1636. {
  1637. c &= 0x7f;
  1638. }
  1639. if (I_IUCLC(tty) && L_IEXTEN(tty))
  1640. {
  1641. c = tolower(c);
  1642. }
  1643. if (L_EXTPROC(tty))
  1644. {
  1645. put_tty_queue(c, ldata);
  1646. continue;
  1647. }
  1648. if (!test_bit(c, (int *)ldata->char_map))
  1649. {
  1650. n_tty_receive_char_inline(tty, c);
  1651. }
  1652. else if (n_tty_receive_char_special(tty, c) && count)
  1653. {
  1654. n_tty_receive_char_lnext(tty, *cp++, flag);
  1655. count--;
  1656. }
  1657. }
  1658. }
  1659. rt_inline void n_tty_receive_char_fast(struct tty_struct *tty, unsigned char c)
  1660. {
  1661. struct n_tty_data *ldata = tty->disc_data;
  1662. if (L_ECHO(tty))
  1663. {
  1664. finish_erasing(ldata);
  1665. /* Record the column of first canon char. */
  1666. if (ldata->canon_head == ldata->read_head)
  1667. {
  1668. echo_set_canon_col(ldata);
  1669. }
  1670. echo_char(c, tty);
  1671. commit_echoes(tty);
  1672. }
  1673. put_tty_queue(c, ldata);
  1674. }
  1675. static void n_tty_receive_buf_fast(struct tty_struct *tty, char *cp, int count)
  1676. {
  1677. struct n_tty_data *ldata = tty->disc_data;
  1678. char flag = TTY_NORMAL;
  1679. while (count--)
  1680. {
  1681. unsigned char c = *cp++;
  1682. if (!test_bit(c, (int *)ldata->char_map))
  1683. {
  1684. n_tty_receive_char_fast(tty, c);
  1685. }
  1686. else if (n_tty_receive_char_special(tty, c) && count)
  1687. {
  1688. n_tty_receive_char_lnext(tty, *cp++, flag);
  1689. count--;
  1690. }
  1691. }
  1692. }
  1693. static void __receive_buf(struct tty_struct *tty, char *cp, int count)
  1694. {
  1695. struct n_tty_data *ldata = tty->disc_data;
  1696. rt_bool_t preops = I_ISTRIP(tty) || (I_IUCLC(tty) && L_IEXTEN(tty));
  1697. if (ldata->real_raw)
  1698. {
  1699. n_tty_receive_buf_real_raw(tty, cp, count);
  1700. }
  1701. else if (ldata->raw || (L_EXTPROC(tty) && !preops))
  1702. {
  1703. n_tty_receive_buf_raw(tty, cp, count);
  1704. }
  1705. else
  1706. {
  1707. if (!preops && !I_PARMRK(tty))
  1708. {
  1709. n_tty_receive_buf_fast(tty, cp, count);
  1710. }
  1711. else
  1712. {
  1713. n_tty_receive_buf_standard(tty, cp, count);
  1714. }
  1715. flush_echoes(tty);
  1716. }
  1717. if (ldata->icanon && !L_EXTPROC(tty))
  1718. return;
  1719. /* publish read_head to consumer */
  1720. ldata->commit_head = ldata->read_head;
  1721. if (read_cnt(ldata))
  1722. {
  1723. tty_wakeup_check(tty);
  1724. }
  1725. }
  1726. int n_tty_receive_buf(struct tty_struct *tty,char *cp, int count)
  1727. {
  1728. int size = 0;
  1729. struct n_tty_data *ldata = tty->disc_data;
  1730. int room = 0, n = 0, rcvd = 0, overflow = 0;
  1731. size = count;
  1732. while(1)
  1733. {
  1734. size_t tail = ldata->read_tail;
  1735. room = RT_TTY_BUF - (ldata->read_head - tail);
  1736. if (I_PARMRK(tty))
  1737. {
  1738. room = (room +2)/3;
  1739. }
  1740. room--;
  1741. if (room <= 0)
  1742. {
  1743. overflow = ldata->icanon && ldata->canon_head == tail;
  1744. if (overflow && room < 0)
  1745. {
  1746. ldata->read_head--;
  1747. }
  1748. room = overflow;
  1749. }
  1750. else
  1751. {
  1752. overflow = 0;
  1753. }
  1754. n = min(size, room);
  1755. if (!n)
  1756. {
  1757. break;
  1758. }
  1759. if (!overflow)
  1760. {
  1761. __receive_buf(tty, cp, n);
  1762. }
  1763. cp += n;
  1764. size -= n;
  1765. rcvd += n;
  1766. }
  1767. return count - size;
  1768. }
  1769. /**
  1770. * job_control - check job control
  1771. * @tty: tty
  1772. * @file: file handle
  1773. *
  1774. * Perform job control management checks on this file/tty descriptor
  1775. * and if appropriate send any needed signals and return a negative
  1776. * error code if action should be taken.
  1777. *
  1778. * Locking: redirected write test is safe
  1779. * current->signal->tty check is safe
  1780. * ctrl_lock to safely reference tty->pgrp
  1781. */
  1782. static int job_control(struct tty_struct *tty)
  1783. {
  1784. return __tty_check_change(tty, SIGTTIN);
  1785. }
  1786. static struct rt_wqueue *_wait_queue_current_get(struct tty_struct *tty)
  1787. {
  1788. struct rt_lwp *lwp;
  1789. lwp = lwp_self();
  1790. if (!lwp || !lwp->tty)
  1791. lwp = RT_NULL;
  1792. return wait_queue_get(lwp, tty);
  1793. }
  1794. static int n_tty_read(struct dfs_file *fd, void *buf, size_t count)
  1795. {
  1796. int level = 0;
  1797. char *b = (char *)buf;
  1798. struct tty_struct *tty = RT_NULL;
  1799. struct rt_wqueue *wq = RT_NULL;
  1800. int wait_ret = 0;
  1801. int retval = 0;
  1802. int c = 0;
  1803. level = rt_hw_interrupt_disable();
  1804. tty = (struct tty_struct *)fd->vnode->data;
  1805. RT_ASSERT(tty != RT_NULL);
  1806. c = job_control(tty);
  1807. if (c < 0)
  1808. {
  1809. rt_hw_interrupt_enable(level);
  1810. return c;
  1811. }
  1812. struct n_tty_data *ldata = tty->disc_data;
  1813. wq = _wait_queue_current_get(tty);
  1814. while(count)
  1815. {
  1816. if ((!input_available_p(tty, 0)))
  1817. {
  1818. if (fd->flags & O_NONBLOCK)
  1819. {
  1820. retval = -EAGAIN;
  1821. break;
  1822. }
  1823. wait_ret = rt_wqueue_wait_interruptible(wq, 0, RT_WAITING_FOREVER);
  1824. if (wait_ret != 0)
  1825. {
  1826. break;
  1827. }
  1828. }
  1829. if (ldata->icanon && !L_EXTPROC(tty))
  1830. {
  1831. retval = canon_copy_from_read_buf(tty, b, count);
  1832. }
  1833. else
  1834. {
  1835. retval = copy_from_read_buf(tty, b, count);
  1836. }
  1837. if (retval >= 1)
  1838. {
  1839. break;
  1840. }
  1841. }
  1842. rt_hw_interrupt_enable(level);
  1843. return retval;
  1844. }
  1845. static int n_tty_write(struct dfs_file *fd, const void *buf, size_t count)
  1846. {
  1847. int retval = 0;
  1848. char *b = (char *)buf;
  1849. int c = 0;
  1850. struct tty_struct *tty = RT_NULL;
  1851. tty = (struct tty_struct *)fd->vnode->data;
  1852. RT_ASSERT(tty != RT_NULL);
  1853. retval = tty_check_change(tty);
  1854. if (retval)
  1855. {
  1856. return retval;
  1857. }
  1858. process_echoes(tty);
  1859. retval = count;
  1860. while(1)
  1861. {
  1862. if (O_OPOST(tty))
  1863. {
  1864. while (count > 0)
  1865. {
  1866. ssize_t num = process_output_block(tty, b, count);
  1867. if (num < 0)
  1868. {
  1869. if (num == -EAGAIN)
  1870. {
  1871. break;
  1872. }
  1873. retval = num;
  1874. goto break_out;
  1875. }
  1876. b += num;
  1877. count -= num;
  1878. if (count == 0)
  1879. {
  1880. break;
  1881. }
  1882. c = *b;
  1883. if (process_output(c, tty) < 0)
  1884. {
  1885. break;
  1886. }
  1887. b++;
  1888. count--;
  1889. }
  1890. retval -= count;
  1891. }
  1892. else
  1893. {
  1894. while (count > 0)
  1895. {
  1896. c = rt_device_write((rt_device_t)tty, -1, b, count);
  1897. if (c < 0)
  1898. {
  1899. retval = c;
  1900. goto break_out;
  1901. }
  1902. b += c;
  1903. count -= c;
  1904. }
  1905. retval -= count;
  1906. }
  1907. if (!count)
  1908. {
  1909. break;
  1910. }
  1911. if (fd->flags & O_NONBLOCK)
  1912. {
  1913. break;
  1914. }
  1915. }
  1916. break_out:
  1917. return retval;
  1918. }
  1919. static int n_tty_flush(struct dfs_file *fd)
  1920. {
  1921. return 0;
  1922. }
  1923. static int n_tty_lseek(struct dfs_file *fd, off_t offset)
  1924. {
  1925. return 0;
  1926. }
  1927. static int n_tty_getdents(struct dfs_file *fd, struct dirent *dirp, uint32_t count)
  1928. {
  1929. return 0;
  1930. }
  1931. static int n_tty_poll(struct dfs_file *fd, struct rt_pollreq *req)
  1932. {
  1933. rt_base_t level = 0;
  1934. int mask = POLLOUT;
  1935. struct tty_struct *tty = RT_NULL;
  1936. struct rt_wqueue *wq = RT_NULL;
  1937. tty = (struct tty_struct *)fd->vnode->data;
  1938. RT_ASSERT(tty != RT_NULL);
  1939. RT_ASSERT(tty->init_flag == TTY_INIT_FLAG_INITED);
  1940. wq = _wait_queue_current_get(tty);
  1941. rt_poll_add(wq, req);
  1942. level = rt_hw_interrupt_disable();
  1943. if (input_available_p(tty, 1))
  1944. {
  1945. mask |= POLLIN;
  1946. }
  1947. rt_hw_interrupt_enable(level);
  1948. return mask;
  1949. }
  1950. struct tty_ldisc_ops n_tty_ops = {
  1951. "n_tty",
  1952. 0,
  1953. n_tty_open,
  1954. n_tty_close,
  1955. n_tty_ioctl,
  1956. n_tty_read,
  1957. n_tty_write,
  1958. n_tty_flush,
  1959. n_tty_lseek,
  1960. n_tty_getdents,
  1961. n_tty_poll,
  1962. n_tty_set_termios,
  1963. n_tty_receive_buf,
  1964. 0,
  1965. };