n_tty.c 50 KB

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