n_tty.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203
  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. if (n > buf_size)
  1284. {
  1285. rt_memcpy(b, from, buf_size);
  1286. b += buf_size;
  1287. n -= buf_size;
  1288. from = ldata->read_buf;
  1289. }
  1290. rt_memcpy(b, from, n);
  1291. if (found)
  1292. {
  1293. clear_bit(eol, (int *)ldata->read_flags);
  1294. }
  1295. ldata->read_tail = ldata->read_tail + c;
  1296. if (found)
  1297. {
  1298. if (!ldata->push)
  1299. {
  1300. ldata->line_start = ldata->read_tail;
  1301. }
  1302. else
  1303. {
  1304. ldata->push = 0;
  1305. }
  1306. }
  1307. return n;
  1308. }
  1309. static int n_tty_close(struct tty_struct *tty)
  1310. {
  1311. int ret = 0;
  1312. struct n_tty_data *ldata = RT_NULL;
  1313. struct tty_struct *o_tty = RT_NULL;
  1314. RT_ASSERT(tty != RT_NULL);
  1315. if (tty->type == TTY_DRIVER_TYPE_PTY && tty->subtype == PTY_TYPE_MASTER)
  1316. {
  1317. o_tty = tty->other_struct;
  1318. }
  1319. else
  1320. {
  1321. o_tty = tty;
  1322. }
  1323. ldata = o_tty->disc_data;
  1324. rt_free(ldata);
  1325. o_tty->disc_data = RT_NULL;
  1326. return ret;
  1327. }
  1328. static int n_tty_ioctl(struct dfs_file *fd, int cmd, void *args)
  1329. {
  1330. int ret = 0;
  1331. struct tty_struct *real_tty = RT_NULL;
  1332. struct tty_struct *tty = RT_NULL;
  1333. tty = (struct tty_struct *)fd->vnode->data;
  1334. RT_ASSERT(tty != RT_NULL);
  1335. if (tty->type == TTY_DRIVER_TYPE_PTY && tty->subtype == PTY_TYPE_MASTER)
  1336. {
  1337. real_tty = tty->other_struct;
  1338. }
  1339. else
  1340. {
  1341. real_tty = tty;
  1342. }
  1343. switch(cmd)
  1344. {
  1345. default:
  1346. ret = n_tty_ioctl_extend(real_tty, cmd, args);
  1347. if (ret != -ENOIOCTLCMD)
  1348. {
  1349. return ret;
  1350. }
  1351. }
  1352. ret = rt_device_control((rt_device_t)real_tty, cmd, args);
  1353. if (ret != -ENOIOCTLCMD)
  1354. {
  1355. return ret;
  1356. }
  1357. return ret;
  1358. }
  1359. static void
  1360. n_tty_receive_signal_char(struct tty_struct *tty, int signal, unsigned char c)
  1361. {
  1362. isig(signal, tty);
  1363. if (L_ECHO(tty))
  1364. {
  1365. echo_char(c, tty);
  1366. commit_echoes(tty);
  1367. }
  1368. else
  1369. {
  1370. process_echoes(tty);
  1371. }
  1372. return;
  1373. }
  1374. /**
  1375. * n_tty_receive_char - perform processing
  1376. * @tty: terminal device
  1377. * @c: character
  1378. *
  1379. * Process an individual character of input received from the driver.
  1380. * This is serialized with respect to itself by the rules for the
  1381. * driver above.
  1382. *
  1383. * n_tty_receive_buf()/producer path:
  1384. * caller holds non-exclusive termios_rwsem
  1385. * publishes canon_head if canonical mode is active
  1386. *
  1387. * Returns 1 if LNEXT was received, else returns 0
  1388. */
  1389. static int n_tty_receive_char_special(struct tty_struct *tty, unsigned char c)
  1390. {
  1391. struct n_tty_data *ldata = tty->disc_data;
  1392. if (I_IXON(tty))
  1393. {
  1394. if (c == START_CHAR(tty)) /*ctrl + p realize missing*/
  1395. {
  1396. process_echoes(tty);
  1397. return 0;
  1398. }
  1399. if (c == STOP_CHAR(tty)) /*ctrl + s realize missing*/
  1400. {
  1401. return 0;
  1402. }
  1403. }
  1404. if (L_ISIG(tty))
  1405. {
  1406. if (c == INTR_CHAR(tty)) /*ctrl + c realize missing*/
  1407. {
  1408. n_tty_receive_signal_char(tty, SIGINT, c);
  1409. return 0;
  1410. }
  1411. else if (c == QUIT_CHAR(tty)) /*ctrl + d realize missing*/
  1412. {
  1413. n_tty_receive_signal_char(tty, SIGQUIT, c);
  1414. return 0;
  1415. }
  1416. else if (c == SUSP_CHAR(tty)) /*ctrl + z realize missing*/
  1417. {
  1418. n_tty_receive_signal_char(tty, SIGTSTP, c);
  1419. return 0;
  1420. }
  1421. }
  1422. if (c == '\r')
  1423. {
  1424. if (I_IGNCR(tty))
  1425. {
  1426. return 0;
  1427. }
  1428. if (I_ICRNL(tty))
  1429. {
  1430. c = '\n';
  1431. }
  1432. }
  1433. else if (c == '\n' && I_INLCR(tty))
  1434. {
  1435. c = '\r';
  1436. }
  1437. if (ldata->icanon)
  1438. {
  1439. if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
  1440. (c == WERASE_CHAR(tty) && L_IEXTEN(tty)))
  1441. {
  1442. eraser(c, tty);
  1443. commit_echoes(tty);
  1444. return 0;
  1445. }
  1446. if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty))
  1447. {
  1448. ldata->lnext = 1;
  1449. if (L_ECHO(tty))
  1450. {
  1451. finish_erasing(ldata);
  1452. if (L_ECHOCTL(tty))
  1453. {
  1454. echo_char_raw('^', ldata);
  1455. echo_char_raw('\b', ldata);
  1456. commit_echoes(tty);
  1457. }
  1458. }
  1459. return 1;
  1460. }
  1461. if (c == REPRINT_CHAR(tty) && L_ECHO(tty) && L_IEXTEN(tty))
  1462. {
  1463. size_t tail = ldata->canon_head;
  1464. finish_erasing(ldata);
  1465. echo_char(c, tty);
  1466. echo_char_raw('\n', ldata);
  1467. while (tail != ldata->read_head)
  1468. {
  1469. echo_char(read_buf(ldata, tail), tty);
  1470. tail++;
  1471. }
  1472. commit_echoes(tty);
  1473. return 0;
  1474. }
  1475. if (c == '\n')
  1476. {
  1477. if (L_ECHO(tty) || L_ECHONL(tty))
  1478. {
  1479. echo_char_raw('\n', ldata);
  1480. commit_echoes(tty);
  1481. }
  1482. goto handle_newline;
  1483. }
  1484. if (c == EOF_CHAR(tty))
  1485. {
  1486. c = __DISABLED_CHAR;
  1487. goto handle_newline;
  1488. }
  1489. if ((c == EOL_CHAR(tty)) ||
  1490. (c == EOL2_CHAR(tty) && L_IEXTEN(tty)))
  1491. {
  1492. /*
  1493. * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
  1494. */
  1495. if (L_ECHO(tty))
  1496. {
  1497. /* Record the column of first canon char. */
  1498. if (ldata->canon_head == ldata->read_head)
  1499. {
  1500. echo_set_canon_col(ldata);
  1501. }
  1502. echo_char(c, tty);
  1503. commit_echoes(tty);
  1504. }
  1505. /*
  1506. * XXX does PARMRK doubling happen for
  1507. * EOL_CHAR and EOL2_CHAR?
  1508. */
  1509. if (c == (unsigned char) '\377' && I_PARMRK(tty))
  1510. {
  1511. put_tty_queue(c, ldata);
  1512. }
  1513. handle_newline:
  1514. set_bit(ldata->read_head & (RT_TTY_BUF - 1), (int *)ldata->read_flags);
  1515. put_tty_queue(c, ldata);
  1516. ldata->canon_head = ldata->read_head;
  1517. tty_wakeup_check(tty);
  1518. return 0;
  1519. }
  1520. }
  1521. if (L_ECHO(tty))
  1522. {
  1523. finish_erasing(ldata);
  1524. if (c == '\n')
  1525. {
  1526. echo_char_raw('\n', ldata);
  1527. }
  1528. else
  1529. {
  1530. /* Record the column of first canon char. */
  1531. if (ldata->canon_head == ldata->read_head)
  1532. {
  1533. echo_set_canon_col(ldata);
  1534. }
  1535. echo_char(c, tty);
  1536. }
  1537. commit_echoes(tty);
  1538. }
  1539. /* PARMRK doubling check */
  1540. if (c == (unsigned char) '\377' && I_PARMRK(tty))
  1541. {
  1542. put_tty_queue(c, ldata);
  1543. }
  1544. put_tty_queue(c, ldata);
  1545. return 0;
  1546. }
  1547. rt_inline void n_tty_receive_char_inline(struct tty_struct *tty, unsigned char c)
  1548. {
  1549. struct n_tty_data *ldata = tty->disc_data;
  1550. if (L_ECHO(tty))
  1551. {
  1552. finish_erasing(ldata);
  1553. /* Record the column of first canon char. */
  1554. if (ldata->canon_head == ldata->read_head)
  1555. {
  1556. echo_set_canon_col(ldata);
  1557. }
  1558. echo_char(c, tty);
  1559. commit_echoes(tty);
  1560. }
  1561. /* PARMRK doubling check */
  1562. if (c == (unsigned char) '\377' && I_PARMRK(tty))
  1563. {
  1564. put_tty_queue(c, ldata);
  1565. }
  1566. put_tty_queue(c, ldata);
  1567. }
  1568. static void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
  1569. {
  1570. n_tty_receive_char_inline(tty, c);
  1571. }
  1572. static void n_tty_receive_char_lnext(struct tty_struct *tty, unsigned char c, char flag)
  1573. {
  1574. struct n_tty_data *ldata = tty->disc_data;
  1575. ldata->lnext = 0;
  1576. if (flag == TTY_NORMAL)
  1577. {
  1578. if (I_ISTRIP(tty))
  1579. {
  1580. c &= 0x7f;
  1581. }
  1582. if (I_IUCLC(tty) && L_IEXTEN(tty))
  1583. {
  1584. c = tolower(c);
  1585. }
  1586. n_tty_receive_char(tty, c);
  1587. }
  1588. else
  1589. {
  1590. //n_tty_receive_char_flagged(tty, c, flag);
  1591. }
  1592. }
  1593. static void n_tty_receive_buf_real_raw(struct tty_struct *tty, char *cp, int count)
  1594. {
  1595. struct n_tty_data *ldata = tty->disc_data;
  1596. size_t n = 0, head = 0;
  1597. head = ldata->read_head & (RT_TTY_BUF - 1);
  1598. n = min(count, RT_TTY_BUF - head);
  1599. rt_memcpy(read_buf_addr(ldata, head), cp, n);
  1600. ldata->read_head += n;
  1601. cp += n;
  1602. count -= n;
  1603. head = ldata->read_head & (RT_TTY_BUF - 1);
  1604. n = min(count, RT_TTY_BUF - head);
  1605. rt_memcpy(read_buf_addr(ldata, head), cp, n);
  1606. ldata->read_head += n;
  1607. }
  1608. static void n_tty_receive_buf_raw(struct tty_struct *tty, char *cp, int count)
  1609. {
  1610. struct n_tty_data *ldata = tty->disc_data;
  1611. char flag = TTY_NORMAL;
  1612. while (count--)
  1613. {
  1614. if (flag == TTY_NORMAL)
  1615. {
  1616. put_tty_queue(*cp++, ldata);
  1617. }
  1618. }
  1619. }
  1620. static void n_tty_receive_buf_standard(struct tty_struct *tty, char *cp, int count)
  1621. {
  1622. struct n_tty_data *ldata = tty->disc_data;
  1623. char flag = TTY_NORMAL;
  1624. while (count--)
  1625. {
  1626. char c = *cp++;
  1627. if (I_ISTRIP(tty))
  1628. {
  1629. c &= 0x7f;
  1630. }
  1631. if (I_IUCLC(tty) && L_IEXTEN(tty))
  1632. {
  1633. c = tolower(c);
  1634. }
  1635. if (L_EXTPROC(tty))
  1636. {
  1637. put_tty_queue(c, ldata);
  1638. continue;
  1639. }
  1640. if (!test_bit(c, (int *)ldata->char_map))
  1641. {
  1642. n_tty_receive_char_inline(tty, c);
  1643. }
  1644. else if (n_tty_receive_char_special(tty, c) && count)
  1645. {
  1646. n_tty_receive_char_lnext(tty, *cp++, flag);
  1647. count--;
  1648. }
  1649. }
  1650. }
  1651. rt_inline void n_tty_receive_char_fast(struct tty_struct *tty, unsigned char c)
  1652. {
  1653. struct n_tty_data *ldata = tty->disc_data;
  1654. if (L_ECHO(tty))
  1655. {
  1656. finish_erasing(ldata);
  1657. /* Record the column of first canon char. */
  1658. if (ldata->canon_head == ldata->read_head)
  1659. {
  1660. echo_set_canon_col(ldata);
  1661. }
  1662. echo_char(c, tty);
  1663. commit_echoes(tty);
  1664. }
  1665. put_tty_queue(c, ldata);
  1666. }
  1667. static void n_tty_receive_buf_fast(struct tty_struct *tty, char *cp, int count)
  1668. {
  1669. struct n_tty_data *ldata = tty->disc_data;
  1670. char flag = TTY_NORMAL;
  1671. while (count--)
  1672. {
  1673. unsigned char c = *cp++;
  1674. if (!test_bit(c, (int *)ldata->char_map))
  1675. {
  1676. n_tty_receive_char_fast(tty, c);
  1677. }
  1678. else if (n_tty_receive_char_special(tty, c) && count)
  1679. {
  1680. n_tty_receive_char_lnext(tty, *cp++, flag);
  1681. count--;
  1682. }
  1683. }
  1684. }
  1685. static void __receive_buf(struct tty_struct *tty, char *cp, int count)
  1686. {
  1687. struct n_tty_data *ldata = tty->disc_data;
  1688. rt_bool_t preops = I_ISTRIP(tty) || (I_IUCLC(tty) && L_IEXTEN(tty));
  1689. if (ldata->real_raw)
  1690. {
  1691. n_tty_receive_buf_real_raw(tty, cp, count);
  1692. }
  1693. else if (ldata->raw || (L_EXTPROC(tty) && !preops))
  1694. {
  1695. n_tty_receive_buf_raw(tty, cp, count);
  1696. }
  1697. else
  1698. {
  1699. if (!preops && !I_PARMRK(tty))
  1700. {
  1701. n_tty_receive_buf_fast(tty, cp, count);
  1702. }
  1703. else
  1704. {
  1705. n_tty_receive_buf_standard(tty, cp, count);
  1706. }
  1707. flush_echoes(tty);
  1708. }
  1709. if (ldata->icanon && !L_EXTPROC(tty))
  1710. return;
  1711. /* publish read_head to consumer */
  1712. ldata->commit_head = ldata->read_head;
  1713. if (read_cnt(ldata))
  1714. {
  1715. tty_wakeup_check(tty);
  1716. }
  1717. }
  1718. int n_tty_receive_buf(struct tty_struct *tty,char *cp, int count)
  1719. {
  1720. int size = 0;
  1721. struct n_tty_data *ldata = tty->disc_data;
  1722. int room = 0, n = 0, rcvd = 0, overflow = 0;
  1723. size = count;
  1724. while(1)
  1725. {
  1726. size_t tail = ldata->read_tail;
  1727. room = RT_TTY_BUF - (ldata->read_head - tail);
  1728. if (I_PARMRK(tty))
  1729. {
  1730. room = (room +2)/3;
  1731. }
  1732. room--;
  1733. if (room <= 0)
  1734. {
  1735. overflow = ldata->icanon && ldata->canon_head == tail;
  1736. if (overflow && room < 0)
  1737. {
  1738. ldata->read_head--;
  1739. }
  1740. room = overflow;
  1741. }
  1742. else
  1743. {
  1744. overflow = 0;
  1745. }
  1746. n = min(size, room);
  1747. if (!n)
  1748. {
  1749. break;
  1750. }
  1751. if (!overflow)
  1752. {
  1753. __receive_buf(tty, cp, n);
  1754. }
  1755. cp += n;
  1756. size -= n;
  1757. rcvd += n;
  1758. }
  1759. return count - size;
  1760. }
  1761. /**
  1762. * job_control - check job control
  1763. * @tty: tty
  1764. * @file: file handle
  1765. *
  1766. * Perform job control management checks on this file/tty descriptor
  1767. * and if appropriate send any needed signals and return a negative
  1768. * error code if action should be taken.
  1769. *
  1770. * Locking: redirected write test is safe
  1771. * current->signal->tty check is safe
  1772. * ctrl_lock to safely reference tty->pgrp
  1773. */
  1774. static int job_control(struct tty_struct *tty)
  1775. {
  1776. return __tty_check_change(tty, SIGTTIN);
  1777. }
  1778. static int n_tty_read(struct dfs_file *fd, void *buf, size_t count)
  1779. {
  1780. int level = 0;
  1781. char *b = (char *)buf;
  1782. struct tty_struct *tty = RT_NULL;
  1783. struct rt_lwp *lwp = RT_NULL;
  1784. struct rt_wqueue *wq = RT_NULL;
  1785. int wait_ret = 0;
  1786. int retval = 0;
  1787. int c = 0;
  1788. level = rt_hw_interrupt_disable();
  1789. tty = (struct tty_struct *)fd->vnode->data;
  1790. RT_ASSERT(tty != RT_NULL);
  1791. c = job_control(tty);
  1792. if (c < 0)
  1793. {
  1794. rt_hw_interrupt_enable(level);
  1795. return c;
  1796. }
  1797. struct n_tty_data *ldata = tty->disc_data;
  1798. lwp = (struct rt_lwp *)(rt_thread_self()->lwp);
  1799. wq = wait_queue_get(lwp, tty);
  1800. while(count)
  1801. {
  1802. if ((!input_available_p(tty, 0)))
  1803. {
  1804. if (fd->flags & O_NONBLOCK)
  1805. {
  1806. retval = -EAGAIN;
  1807. break;
  1808. }
  1809. wait_ret = rt_wqueue_wait_interruptible(wq, 0, RT_WAITING_FOREVER);
  1810. if (wait_ret != 0)
  1811. {
  1812. break;
  1813. }
  1814. }
  1815. if (ldata->icanon && !L_EXTPROC(tty))
  1816. {
  1817. retval = canon_copy_from_read_buf(tty, b, count);
  1818. }
  1819. else
  1820. {
  1821. retval = copy_from_read_buf(tty, b, count);
  1822. }
  1823. if (retval >= 1)
  1824. {
  1825. break;
  1826. }
  1827. }
  1828. rt_hw_interrupt_enable(level);
  1829. return retval;
  1830. }
  1831. static int n_tty_write(struct dfs_file *fd, const void *buf, size_t count)
  1832. {
  1833. int retval = 0;
  1834. char *b = (char *)buf;
  1835. int c = 0;
  1836. struct tty_struct *tty = RT_NULL;
  1837. tty = (struct tty_struct *)fd->vnode->data;
  1838. RT_ASSERT(tty != RT_NULL);
  1839. retval = tty_check_change(tty);
  1840. if (retval)
  1841. {
  1842. return retval;
  1843. }
  1844. process_echoes(tty);
  1845. retval = count;
  1846. while(1)
  1847. {
  1848. if (O_OPOST(tty))
  1849. {
  1850. while (count > 0)
  1851. {
  1852. ssize_t num = process_output_block(tty, b, count);
  1853. if (num < 0)
  1854. {
  1855. if (num == -EAGAIN)
  1856. {
  1857. break;
  1858. }
  1859. retval = num;
  1860. goto break_out;
  1861. }
  1862. b += num;
  1863. count -= num;
  1864. if (count == 0)
  1865. {
  1866. break;
  1867. }
  1868. c = *b;
  1869. if (process_output(c, tty) < 0)
  1870. {
  1871. break;
  1872. }
  1873. b++;
  1874. count--;
  1875. }
  1876. retval -= count;
  1877. }
  1878. else
  1879. {
  1880. while (count > 0)
  1881. {
  1882. c = rt_device_write((rt_device_t)tty, -1, b, count);
  1883. if (c < 0)
  1884. {
  1885. retval = c;
  1886. goto break_out;
  1887. }
  1888. b += c;
  1889. count -= c;
  1890. }
  1891. retval -= count;
  1892. }
  1893. if (!count)
  1894. {
  1895. break;
  1896. }
  1897. if (fd->flags & O_NONBLOCK)
  1898. {
  1899. break;
  1900. }
  1901. }
  1902. break_out:
  1903. return retval;
  1904. }
  1905. static int n_tty_flush(struct dfs_file *fd)
  1906. {
  1907. return 0;
  1908. }
  1909. static int n_tty_lseek(struct dfs_file *fd, off_t offset)
  1910. {
  1911. return 0;
  1912. }
  1913. static int n_tty_getdents(struct dfs_file *fd, struct dirent *dirp, uint32_t count)
  1914. {
  1915. return 0;
  1916. }
  1917. static int n_tty_poll(struct dfs_file *fd, struct rt_pollreq *req)
  1918. {
  1919. rt_base_t level = 0;
  1920. int mask = POLLOUT;
  1921. struct tty_struct *tty = RT_NULL;
  1922. struct rt_wqueue *wq = RT_NULL;
  1923. struct rt_lwp *lwp = RT_NULL;
  1924. tty = (struct tty_struct *)fd->vnode->data;
  1925. RT_ASSERT(tty != RT_NULL);
  1926. RT_ASSERT(tty->init_flag == TTY_INIT_FLAG_INITED);
  1927. lwp = (struct rt_lwp *)(rt_thread_self()->lwp);
  1928. wq = wait_queue_get(lwp, tty);
  1929. rt_poll_add(wq, req);
  1930. level = rt_hw_interrupt_disable();
  1931. if (input_available_p(tty, 1))
  1932. {
  1933. mask |= POLLIN;
  1934. }
  1935. rt_hw_interrupt_enable(level);
  1936. return mask;
  1937. }
  1938. struct tty_ldisc_ops n_tty_ops = {
  1939. "n_tty",
  1940. 0,
  1941. n_tty_open,
  1942. n_tty_close,
  1943. n_tty_ioctl,
  1944. n_tty_read,
  1945. n_tty_write,
  1946. n_tty_flush,
  1947. n_tty_lseek,
  1948. n_tty_getdents,
  1949. n_tty_poll,
  1950. n_tty_set_termios,
  1951. n_tty_receive_buf,
  1952. 0,
  1953. };