n_tty.c 50 KB

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