n_tty.c 50 KB

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