n_tty.c 53 KB

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