n_tty.c 53 KB

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