1
0

n_tty.c 53 KB

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