n_tty.c 50 KB

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