n_tty.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094
  1. /*
  2. * n_tty.c --- implements the N_TTY line discipline.
  3. *
  4. * This code used to be in tty_io.c, but things are getting hairy
  5. * enough that it made sense to split things off. (The N_TTY
  6. * processing has changed so much that it's hardly recognizable,
  7. * anyway...)
  8. *
  9. * Note that the open routine for N_TTY is guaranteed never to return
  10. * an error. This is because Linux will fall back to setting a line
  11. * to N_TTY if it can not switch to any other line discipline.
  12. *
  13. * Written by Theodore Ts'o, Copyright 1994.
  14. *
  15. * This file also contains code originally written by Linus Torvalds,
  16. * Copyright 1991, 1992, 1993, and by Julian Cowley, Copyright 1994.
  17. *
  18. * This file may be redistributed under the terms of the GNU General Public
  19. * License.
  20. *
  21. * Reduced memory usage for older ARM systems - Russell King.
  22. *
  23. * 2000/01/20 Fixed SMP locking on put_tty_queue using bits of
  24. * the patch by Andrew J. Kroll <ag784@freenet.buffalo.edu>
  25. * who actually finally proved there really was a race.
  26. *
  27. * 2002/03/18 Implemented n_tty_wakeup to send SIGIO POLL_OUTs to
  28. * waiting writing processes-Sapan Bhatia <sapan@corewars.org>.
  29. * Also fixed a bug in BLOCKING mode where n_tty_write returns
  30. * EAGAIN
  31. */
  32. #include <linux/types.h>
  33. #include <linux/major.h>
  34. #include <linux/errno.h>
  35. #include <linux/signal.h>
  36. #include <linux/fcntl.h>
  37. #include <linux/sched.h>
  38. #include <linux/interrupt.h>
  39. #include <linux/tty.h>
  40. #include <linux/timer.h>
  41. #include <linux/ctype.h>
  42. #include <linux/mm.h>
  43. #include <linux/string.h>
  44. #include <linux/slab.h>
  45. #include <linux/poll.h>
  46. #include <linux/bitops.h>
  47. #include <linux/audit.h>
  48. #include <linux/file.h>
  49. #include <linux/uaccess.h>
  50. #include <linux/module.h>
  51. #include <asm/system.h>
  52. /* number of characters left in xmit buffer before select has we have room */
  53. #define WAKEUP_CHARS 256
  54. /*
  55. * This defines the low- and high-watermarks for throttling and
  56. * unthrottling the TTY driver. These watermarks are used for
  57. * controlling the space in the read buffer.
  58. */
  59. #define TTY_THRESHOLD_THROTTLE 128 /* now based on remaining room */
  60. #define TTY_THRESHOLD_UNTHROTTLE 128
  61. /*
  62. * Special byte codes used in the echo buffer to represent operations
  63. * or special handling of characters. Bytes in the echo buffer that
  64. * are not part of such special blocks are treated as normal character
  65. * codes.
  66. */
  67. #define ECHO_OP_START 0xff
  68. #define ECHO_OP_MOVE_BACK_COL 0x80
  69. #define ECHO_OP_SET_CANON_COL 0x81
  70. #define ECHO_OP_ERASE_TAB 0x82
  71. static inline int tty_put_user(struct tty_struct *tty, unsigned char x,
  72. unsigned char __user *ptr)
  73. {
  74. tty_audit_add_data(tty, &x, 1);
  75. return put_user(x, ptr);
  76. }
  77. static void put_tty_queue_nolock(unsigned char c, struct tty_struct *tty)
  78. {
  79. if (tty->read_cnt < N_TTY_BUF_SIZE) {
  80. tty->read_buf[tty->read_head] = c;
  81. tty->read_head = (tty->read_head + 1) & (N_TTY_BUF_SIZE-1);
  82. tty->read_cnt++;
  83. }
  84. }
  85. /**
  86. * put_tty_queue - add character to tty
  87. * @c: character
  88. * @tty: tty device
  89. *
  90. * Add a character to the tty read_buf queue. This is done under the
  91. * read_lock to serialize character addition and also to protect us
  92. * against parallel reads or flushes
  93. */
  94. static void put_tty_queue(unsigned char c, struct tty_struct *tty)
  95. {
  96. unsigned long flags;
  97. /*
  98. * The problem of stomping on the buffers ends here.
  99. * Why didn't anyone see this one coming? --AJK
  100. */
  101. spin_lock_irqsave(&tty->read_lock, flags);
  102. put_tty_queue_nolock(c, tty);
  103. spin_unlock_irqrestore(&tty->read_lock, flags);
  104. }
  105. /**
  106. * check_unthrottle - allow new receive data
  107. * @tty; tty device
  108. *
  109. * Check whether to call the driver unthrottle functions
  110. *
  111. * Can sleep, may be called under the atomic_read_lock mutex but
  112. * this is not guaranteed.
  113. */
  114. static void check_unthrottle(struct tty_struct *tty)
  115. {
  116. if (tty->count)
  117. tty_unthrottle(tty);
  118. }
  119. /**
  120. * reset_buffer_flags - reset buffer state
  121. * @tty: terminal to reset
  122. *
  123. * Reset the read buffer counters, clear the flags,
  124. * and make sure the driver is unthrottled. Called
  125. * from n_tty_open() and n_tty_flush_buffer().
  126. *
  127. * Locking: tty_read_lock for read fields.
  128. */
  129. static void reset_buffer_flags(struct tty_struct *tty)
  130. {
  131. unsigned long flags;
  132. spin_lock_irqsave(&tty->read_lock, flags);
  133. tty->read_head = tty->read_tail = tty->read_cnt = 0;
  134. spin_unlock_irqrestore(&tty->read_lock, flags);
  135. mutex_lock(&tty->echo_lock);
  136. tty->echo_pos = tty->echo_cnt = tty->echo_overrun = 0;
  137. mutex_unlock(&tty->echo_lock);
  138. tty->canon_head = tty->canon_data = tty->erasing = 0;
  139. memset(&tty->read_flags, 0, sizeof tty->read_flags);
  140. check_unthrottle(tty);
  141. }
  142. /**
  143. * n_tty_flush_buffer - clean input queue
  144. * @tty: terminal device
  145. *
  146. * Flush the input buffer. Called when the line discipline is
  147. * being closed, when the tty layer wants the buffer flushed (eg
  148. * at hangup) or when the N_TTY line discipline internally has to
  149. * clean the pending queue (for example some signals).
  150. *
  151. * Locking: ctrl_lock, read_lock.
  152. */
  153. static void n_tty_flush_buffer(struct tty_struct *tty)
  154. {
  155. unsigned long flags;
  156. /* clear everything and unthrottle the driver */
  157. reset_buffer_flags(tty);
  158. if (!tty->link)
  159. return;
  160. spin_lock_irqsave(&tty->ctrl_lock, flags);
  161. if (tty->link->packet) {
  162. tty->ctrl_status |= TIOCPKT_FLUSHREAD;
  163. wake_up_interruptible(&tty->link->read_wait);
  164. }
  165. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  166. }
  167. /**
  168. * n_tty_chars_in_buffer - report available bytes
  169. * @tty: tty device
  170. *
  171. * Report the number of characters buffered to be delivered to user
  172. * at this instant in time.
  173. *
  174. * Locking: read_lock
  175. */
  176. static ssize_t n_tty_chars_in_buffer(struct tty_struct *tty)
  177. {
  178. unsigned long flags;
  179. ssize_t n = 0;
  180. spin_lock_irqsave(&tty->read_lock, flags);
  181. if (!tty->icanon) {
  182. n = tty->read_cnt;
  183. } else if (tty->canon_data) {
  184. n = (tty->canon_head > tty->read_tail) ?
  185. tty->canon_head - tty->read_tail :
  186. tty->canon_head + (N_TTY_BUF_SIZE - tty->read_tail);
  187. }
  188. spin_unlock_irqrestore(&tty->read_lock, flags);
  189. return n;
  190. }
  191. /**
  192. * is_utf8_continuation - utf8 multibyte check
  193. * @c: byte to check
  194. *
  195. * Returns true if the utf8 character 'c' is a multibyte continuation
  196. * character. We use this to correctly compute the on screen size
  197. * of the character when printing
  198. */
  199. static inline int is_utf8_continuation(unsigned char c)
  200. {
  201. return (c & 0xc0) == 0x80;
  202. }
  203. /**
  204. * is_continuation - multibyte check
  205. * @c: byte to check
  206. *
  207. * Returns true if the utf8 character 'c' is a multibyte continuation
  208. * character and the terminal is in unicode mode.
  209. */
  210. static inline int is_continuation(unsigned char c, struct tty_struct *tty)
  211. {
  212. return I_IUTF8(tty) && is_utf8_continuation(c);
  213. }
  214. /**
  215. * do_output_char - output one character
  216. * @c: character (or partial unicode symbol)
  217. * @tty: terminal device
  218. * @space: space available in tty driver write buffer
  219. *
  220. * This is a helper function that handles one output character
  221. * (including special characters like TAB, CR, LF, etc.),
  222. * doing OPOST processing and putting the results in the
  223. * tty driver's write buffer.
  224. *
  225. * Note that Linux currently ignores TABDLY, CRDLY, VTDLY, FFDLY
  226. * and NLDLY. They simply aren't relevant in the world today.
  227. * If you ever need them, add them here.
  228. *
  229. * Returns the number of bytes of buffer space used or -1 if
  230. * no space left.
  231. *
  232. * Locking: should be called under the output_lock to protect
  233. * the column state and space left in the buffer
  234. */
  235. static int do_output_char(unsigned char c, struct tty_struct *tty, int space)
  236. {
  237. int spaces;
  238. if (!space)
  239. return -1;
  240. switch (c) {
  241. case '\n':
  242. if (O_ONLRET(tty))
  243. tty->column = 0;
  244. if (O_ONLCR(tty)) {
  245. if (space < 2)
  246. return -1;
  247. tty->canon_column = tty->column = 0;
  248. tty->ops->write(tty, "\r\n", 2);
  249. return 2;
  250. }
  251. tty->canon_column = tty->column;
  252. break;
  253. case '\r':
  254. if (O_ONOCR(tty) && tty->column == 0)
  255. return 0;
  256. if (O_OCRNL(tty)) {
  257. c = '\n';
  258. if (O_ONLRET(tty))
  259. tty->canon_column = tty->column = 0;
  260. break;
  261. }
  262. tty->canon_column = tty->column = 0;
  263. break;
  264. case '\t':
  265. spaces = 8 - (tty->column & 7);
  266. if (O_TABDLY(tty) == XTABS) {
  267. if (space < spaces)
  268. return -1;
  269. tty->column += spaces;
  270. tty->ops->write(tty, " ", spaces);
  271. return spaces;
  272. }
  273. tty->column += spaces;
  274. break;
  275. case '\b':
  276. if (tty->column > 0)
  277. tty->column--;
  278. break;
  279. default:
  280. if (!iscntrl(c)) {
  281. if (O_OLCUC(tty))
  282. c = toupper(c);
  283. if (!is_continuation(c, tty))
  284. tty->column++;
  285. }
  286. break;
  287. }
  288. tty_put_char(tty, c);
  289. return 1;
  290. }
  291. /**
  292. * process_output - output post processor
  293. * @c: character (or partial unicode symbol)
  294. * @tty: terminal device
  295. *
  296. * Output one character with OPOST processing.
  297. * Returns -1 when the output device is full and the character
  298. * must be retried.
  299. *
  300. * Locking: output_lock to protect column state and space left
  301. * (also, this is called from n_tty_write under the
  302. * tty layer write lock)
  303. */
  304. static int process_output(unsigned char c, struct tty_struct *tty)
  305. {
  306. int space, retval;
  307. mutex_lock(&tty->output_lock);
  308. space = tty_write_room(tty);
  309. retval = do_output_char(c, tty, space);
  310. mutex_unlock(&tty->output_lock);
  311. if (retval < 0)
  312. return -1;
  313. else
  314. return 0;
  315. }
  316. /**
  317. * process_output_block - block post processor
  318. * @tty: terminal device
  319. * @buf: character buffer
  320. * @nr: number of bytes to output
  321. *
  322. * Output a block of characters with OPOST processing.
  323. * Returns the number of characters output.
  324. *
  325. * This path is used to speed up block console writes, among other
  326. * things when processing blocks of output data. It handles only
  327. * the simple cases normally found and helps to generate blocks of
  328. * symbols for the console driver and thus improve performance.
  329. *
  330. * Locking: output_lock to protect column state and space left
  331. * (also, this is called from n_tty_write under the
  332. * tty layer write lock)
  333. */
  334. static ssize_t process_output_block(struct tty_struct *tty,
  335. const unsigned char *buf, unsigned int nr)
  336. {
  337. int space;
  338. int i;
  339. const unsigned char *cp;
  340. mutex_lock(&tty->output_lock);
  341. space = tty_write_room(tty);
  342. if (!space) {
  343. mutex_unlock(&tty->output_lock);
  344. return 0;
  345. }
  346. if (nr > space)
  347. nr = space;
  348. for (i = 0, cp = buf; i < nr; i++, cp++) {
  349. unsigned char c = *cp;
  350. switch (c) {
  351. case '\n':
  352. if (O_ONLRET(tty))
  353. tty->column = 0;
  354. if (O_ONLCR(tty))
  355. goto break_out;
  356. tty->canon_column = tty->column;
  357. break;
  358. case '\r':
  359. if (O_ONOCR(tty) && tty->column == 0)
  360. goto break_out;
  361. if (O_OCRNL(tty))
  362. goto break_out;
  363. tty->canon_column = tty->column = 0;
  364. break;
  365. case '\t':
  366. goto break_out;
  367. case '\b':
  368. if (tty->column > 0)
  369. tty->column--;
  370. break;
  371. default:
  372. if (!iscntrl(c)) {
  373. if (O_OLCUC(tty))
  374. goto break_out;
  375. if (!is_continuation(c, tty))
  376. tty->column++;
  377. }
  378. break;
  379. }
  380. }
  381. break_out:
  382. i = tty->ops->write(tty, buf, i);
  383. mutex_unlock(&tty->output_lock);
  384. return i;
  385. }
  386. /**
  387. * process_echoes - write pending echo characters
  388. * @tty: terminal device
  389. *
  390. * Write previously buffered echo (and other ldisc-generated)
  391. * characters to the tty.
  392. *
  393. * Characters generated by the ldisc (including echoes) need to
  394. * be buffered because the driver's write buffer can fill during
  395. * heavy program output. Echoing straight to the driver will
  396. * often fail under these conditions, causing lost characters and
  397. * resulting mismatches of ldisc state information.
  398. *
  399. * Since the ldisc state must represent the characters actually sent
  400. * to the driver at the time of the write, operations like certain
  401. * changes in column state are also saved in the buffer and executed
  402. * here.
  403. *
  404. * A circular fifo buffer is used so that the most recent characters
  405. * are prioritized. Also, when control characters are echoed with a
  406. * prefixed "^", the pair is treated atomically and thus not separated.
  407. *
  408. * Locking: output_lock to protect column state and space left,
  409. * echo_lock to protect the echo buffer
  410. */
  411. static void process_echoes(struct tty_struct *tty)
  412. {
  413. int space, nr;
  414. unsigned char c;
  415. unsigned char *cp, *buf_end;
  416. if (!tty->echo_cnt)
  417. return;
  418. mutex_lock(&tty->output_lock);
  419. mutex_lock(&tty->echo_lock);
  420. space = tty_write_room(tty);
  421. buf_end = tty->echo_buf + N_TTY_BUF_SIZE;
  422. cp = tty->echo_buf + tty->echo_pos;
  423. nr = tty->echo_cnt;
  424. while (nr > 0) {
  425. c = *cp;
  426. if (c == ECHO_OP_START) {
  427. unsigned char op;
  428. unsigned char *opp;
  429. int no_space_left = 0;
  430. /*
  431. * If the buffer byte is the start of a multi-byte
  432. * operation, get the next byte, which is either the
  433. * op code or a control character value.
  434. */
  435. opp = cp + 1;
  436. if (opp == buf_end)
  437. opp -= N_TTY_BUF_SIZE;
  438. op = *opp;
  439. switch (op) {
  440. unsigned int num_chars, num_bs;
  441. case ECHO_OP_ERASE_TAB:
  442. if (++opp == buf_end)
  443. opp -= N_TTY_BUF_SIZE;
  444. num_chars = *opp;
  445. /*
  446. * Determine how many columns to go back
  447. * in order to erase the tab.
  448. * This depends on the number of columns
  449. * used by other characters within the tab
  450. * area. If this (modulo 8) count is from
  451. * the start of input rather than from a
  452. * previous tab, we offset by canon column.
  453. * Otherwise, tab spacing is normal.
  454. */
  455. if (!(num_chars & 0x80))
  456. num_chars += tty->canon_column;
  457. num_bs = 8 - (num_chars & 7);
  458. if (num_bs > space) {
  459. no_space_left = 1;
  460. break;
  461. }
  462. space -= num_bs;
  463. while (num_bs--) {
  464. tty_put_char(tty, '\b');
  465. if (tty->column > 0)
  466. tty->column--;
  467. }
  468. cp += 3;
  469. nr -= 3;
  470. break;
  471. case ECHO_OP_SET_CANON_COL:
  472. tty->canon_column = tty->column;
  473. cp += 2;
  474. nr -= 2;
  475. break;
  476. case ECHO_OP_MOVE_BACK_COL:
  477. if (tty->column > 0)
  478. tty->column--;
  479. cp += 2;
  480. nr -= 2;
  481. break;
  482. case ECHO_OP_START:
  483. /* This is an escaped echo op start code */
  484. if (!space) {
  485. no_space_left = 1;
  486. break;
  487. }
  488. tty_put_char(tty, ECHO_OP_START);
  489. tty->column++;
  490. space--;
  491. cp += 2;
  492. nr -= 2;
  493. break;
  494. default:
  495. /*
  496. * If the op is not a special byte code,
  497. * it is a ctrl char tagged to be echoed
  498. * as "^X" (where X is the letter
  499. * representing the control char).
  500. * Note that we must ensure there is
  501. * enough space for the whole ctrl pair.
  502. *
  503. */
  504. if (space < 2) {
  505. no_space_left = 1;
  506. break;
  507. }
  508. tty_put_char(tty, '^');
  509. tty_put_char(tty, op ^ 0100);
  510. tty->column += 2;
  511. space -= 2;
  512. cp += 2;
  513. nr -= 2;
  514. }
  515. if (no_space_left)
  516. break;
  517. } else {
  518. if (O_OPOST(tty) &&
  519. !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
  520. int retval = do_output_char(c, tty, space);
  521. if (retval < 0)
  522. break;
  523. space -= retval;
  524. } else {
  525. if (!space)
  526. break;
  527. tty_put_char(tty, c);
  528. space -= 1;
  529. }
  530. cp += 1;
  531. nr -= 1;
  532. }
  533. /* When end of circular buffer reached, wrap around */
  534. if (cp >= buf_end)
  535. cp -= N_TTY_BUF_SIZE;
  536. }
  537. if (nr == 0) {
  538. tty->echo_pos = 0;
  539. tty->echo_cnt = 0;
  540. tty->echo_overrun = 0;
  541. } else {
  542. int num_processed = tty->echo_cnt - nr;
  543. tty->echo_pos += num_processed;
  544. tty->echo_pos &= N_TTY_BUF_SIZE - 1;
  545. tty->echo_cnt = nr;
  546. if (num_processed > 0)
  547. tty->echo_overrun = 0;
  548. }
  549. mutex_unlock(&tty->echo_lock);
  550. mutex_unlock(&tty->output_lock);
  551. if (tty->ops->flush_chars)
  552. tty->ops->flush_chars(tty);
  553. }
  554. /**
  555. * add_echo_byte - add a byte to the echo buffer
  556. * @c: unicode byte to echo
  557. * @tty: terminal device
  558. *
  559. * Add a character or operation byte to the echo buffer.
  560. *
  561. * Should be called under the echo lock to protect the echo buffer.
  562. */
  563. static void add_echo_byte(unsigned char c, struct tty_struct *tty)
  564. {
  565. int new_byte_pos;
  566. if (tty->echo_cnt == N_TTY_BUF_SIZE) {
  567. /* Circular buffer is already at capacity */
  568. new_byte_pos = tty->echo_pos;
  569. /*
  570. * Since the buffer start position needs to be advanced,
  571. * be sure to step by a whole operation byte group.
  572. */
  573. if (tty->echo_buf[tty->echo_pos] == ECHO_OP_START) {
  574. if (tty->echo_buf[(tty->echo_pos + 1) &
  575. (N_TTY_BUF_SIZE - 1)] ==
  576. ECHO_OP_ERASE_TAB) {
  577. tty->echo_pos += 3;
  578. tty->echo_cnt -= 2;
  579. } else {
  580. tty->echo_pos += 2;
  581. tty->echo_cnt -= 1;
  582. }
  583. } else {
  584. tty->echo_pos++;
  585. }
  586. tty->echo_pos &= N_TTY_BUF_SIZE - 1;
  587. tty->echo_overrun = 1;
  588. } else {
  589. new_byte_pos = tty->echo_pos + tty->echo_cnt;
  590. new_byte_pos &= N_TTY_BUF_SIZE - 1;
  591. tty->echo_cnt++;
  592. }
  593. tty->echo_buf[new_byte_pos] = c;
  594. }
  595. /**
  596. * echo_move_back_col - add operation to move back a column
  597. * @tty: terminal device
  598. *
  599. * Add an operation to the echo buffer to move back one column.
  600. *
  601. * Locking: echo_lock to protect the echo buffer
  602. */
  603. static void echo_move_back_col(struct tty_struct *tty)
  604. {
  605. mutex_lock(&tty->echo_lock);
  606. add_echo_byte(ECHO_OP_START, tty);
  607. add_echo_byte(ECHO_OP_MOVE_BACK_COL, tty);
  608. mutex_unlock(&tty->echo_lock);
  609. }
  610. /**
  611. * echo_set_canon_col - add operation to set the canon column
  612. * @tty: terminal device
  613. *
  614. * Add an operation to the echo buffer to set the canon column
  615. * to the current column.
  616. *
  617. * Locking: echo_lock to protect the echo buffer
  618. */
  619. static void echo_set_canon_col(struct tty_struct *tty)
  620. {
  621. mutex_lock(&tty->echo_lock);
  622. add_echo_byte(ECHO_OP_START, tty);
  623. add_echo_byte(ECHO_OP_SET_CANON_COL, tty);
  624. mutex_unlock(&tty->echo_lock);
  625. }
  626. /**
  627. * echo_erase_tab - add operation to erase a tab
  628. * @num_chars: number of character columns already used
  629. * @after_tab: true if num_chars starts after a previous tab
  630. * @tty: terminal device
  631. *
  632. * Add an operation to the echo buffer to erase a tab.
  633. *
  634. * Called by the eraser function, which knows how many character
  635. * columns have been used since either a previous tab or the start
  636. * of input. This information will be used later, along with
  637. * canon column (if applicable), to go back the correct number
  638. * of columns.
  639. *
  640. * Locking: echo_lock to protect the echo buffer
  641. */
  642. static void echo_erase_tab(unsigned int num_chars, int after_tab,
  643. struct tty_struct *tty)
  644. {
  645. mutex_lock(&tty->echo_lock);
  646. add_echo_byte(ECHO_OP_START, tty);
  647. add_echo_byte(ECHO_OP_ERASE_TAB, tty);
  648. /* We only need to know this modulo 8 (tab spacing) */
  649. num_chars &= 7;
  650. /* Set the high bit as a flag if num_chars is after a previous tab */
  651. if (after_tab)
  652. num_chars |= 0x80;
  653. add_echo_byte(num_chars, tty);
  654. mutex_unlock(&tty->echo_lock);
  655. }
  656. /**
  657. * echo_char_raw - echo a character raw
  658. * @c: unicode byte to echo
  659. * @tty: terminal device
  660. *
  661. * Echo user input back onto the screen. This must be called only when
  662. * L_ECHO(tty) is true. Called from the driver receive_buf path.
  663. *
  664. * This variant does not treat control characters specially.
  665. *
  666. * Locking: echo_lock to protect the echo buffer
  667. */
  668. static void echo_char_raw(unsigned char c, struct tty_struct *tty)
  669. {
  670. mutex_lock(&tty->echo_lock);
  671. if (c == ECHO_OP_START) {
  672. add_echo_byte(ECHO_OP_START, tty);
  673. add_echo_byte(ECHO_OP_START, tty);
  674. } else {
  675. add_echo_byte(c, tty);
  676. }
  677. mutex_unlock(&tty->echo_lock);
  678. }
  679. /**
  680. * echo_char - echo a character
  681. * @c: unicode byte to echo
  682. * @tty: terminal device
  683. *
  684. * Echo user input back onto the screen. This must be called only when
  685. * L_ECHO(tty) is true. Called from the driver receive_buf path.
  686. *
  687. * This variant tags control characters to be echoed as "^X"
  688. * (where X is the letter representing the control char).
  689. *
  690. * Locking: echo_lock to protect the echo buffer
  691. */
  692. static void echo_char(unsigned char c, struct tty_struct *tty)
  693. {
  694. mutex_lock(&tty->echo_lock);
  695. if (c == ECHO_OP_START) {
  696. add_echo_byte(ECHO_OP_START, tty);
  697. add_echo_byte(ECHO_OP_START, tty);
  698. } else {
  699. if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t')
  700. add_echo_byte(ECHO_OP_START, tty);
  701. add_echo_byte(c, tty);
  702. }
  703. mutex_unlock(&tty->echo_lock);
  704. }
  705. /**
  706. * finish_erasing - complete erase
  707. * @tty: tty doing the erase
  708. */
  709. static inline void finish_erasing(struct tty_struct *tty)
  710. {
  711. if (tty->erasing) {
  712. echo_char_raw('/', tty);
  713. tty->erasing = 0;
  714. }
  715. }
  716. /**
  717. * eraser - handle erase function
  718. * @c: character input
  719. * @tty: terminal device
  720. *
  721. * Perform erase and necessary output when an erase character is
  722. * present in the stream from the driver layer. Handles the complexities
  723. * of UTF-8 multibyte symbols.
  724. *
  725. * Locking: read_lock for tty buffers
  726. */
  727. static void eraser(unsigned char c, struct tty_struct *tty)
  728. {
  729. enum { ERASE, WERASE, KILL } kill_type;
  730. int head, seen_alnums, cnt;
  731. unsigned long flags;
  732. /* FIXME: locking needed ? */
  733. if (tty->read_head == tty->canon_head) {
  734. /* process_output('\a', tty); */ /* what do you think? */
  735. return;
  736. }
  737. if (c == ERASE_CHAR(tty))
  738. kill_type = ERASE;
  739. else if (c == WERASE_CHAR(tty))
  740. kill_type = WERASE;
  741. else {
  742. if (!L_ECHO(tty)) {
  743. spin_lock_irqsave(&tty->read_lock, flags);
  744. tty->read_cnt -= ((tty->read_head - tty->canon_head) &
  745. (N_TTY_BUF_SIZE - 1));
  746. tty->read_head = tty->canon_head;
  747. spin_unlock_irqrestore(&tty->read_lock, flags);
  748. return;
  749. }
  750. if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
  751. spin_lock_irqsave(&tty->read_lock, flags);
  752. tty->read_cnt -= ((tty->read_head - tty->canon_head) &
  753. (N_TTY_BUF_SIZE - 1));
  754. tty->read_head = tty->canon_head;
  755. spin_unlock_irqrestore(&tty->read_lock, flags);
  756. finish_erasing(tty);
  757. echo_char(KILL_CHAR(tty), tty);
  758. /* Add a newline if ECHOK is on and ECHOKE is off. */
  759. if (L_ECHOK(tty))
  760. echo_char_raw('\n', tty);
  761. return;
  762. }
  763. kill_type = KILL;
  764. }
  765. seen_alnums = 0;
  766. /* FIXME: Locking ?? */
  767. while (tty->read_head != tty->canon_head) {
  768. head = tty->read_head;
  769. /* erase a single possibly multibyte character */
  770. do {
  771. head = (head - 1) & (N_TTY_BUF_SIZE-1);
  772. c = tty->read_buf[head];
  773. } while (is_continuation(c, tty) && head != tty->canon_head);
  774. /* do not partially erase */
  775. if (is_continuation(c, tty))
  776. break;
  777. if (kill_type == WERASE) {
  778. /* Equivalent to BSD's ALTWERASE. */
  779. if (isalnum(c) || c == '_')
  780. seen_alnums++;
  781. else if (seen_alnums)
  782. break;
  783. }
  784. cnt = (tty->read_head - head) & (N_TTY_BUF_SIZE-1);
  785. spin_lock_irqsave(&tty->read_lock, flags);
  786. tty->read_head = head;
  787. tty->read_cnt -= cnt;
  788. spin_unlock_irqrestore(&tty->read_lock, flags);
  789. if (L_ECHO(tty)) {
  790. if (L_ECHOPRT(tty)) {
  791. if (!tty->erasing) {
  792. echo_char_raw('\\', tty);
  793. tty->erasing = 1;
  794. }
  795. /* if cnt > 1, output a multi-byte character */
  796. echo_char(c, tty);
  797. while (--cnt > 0) {
  798. head = (head+1) & (N_TTY_BUF_SIZE-1);
  799. echo_char_raw(tty->read_buf[head], tty);
  800. echo_move_back_col(tty);
  801. }
  802. } else if (kill_type == ERASE && !L_ECHOE(tty)) {
  803. echo_char(ERASE_CHAR(tty), tty);
  804. } else if (c == '\t') {
  805. unsigned int num_chars = 0;
  806. int after_tab = 0;
  807. unsigned long tail = tty->read_head;
  808. /*
  809. * Count the columns used for characters
  810. * since the start of input or after a
  811. * previous tab.
  812. * This info is used to go back the correct
  813. * number of columns.
  814. */
  815. while (tail != tty->canon_head) {
  816. tail = (tail-1) & (N_TTY_BUF_SIZE-1);
  817. c = tty->read_buf[tail];
  818. if (c == '\t') {
  819. after_tab = 1;
  820. break;
  821. } else if (iscntrl(c)) {
  822. if (L_ECHOCTL(tty))
  823. num_chars += 2;
  824. } else if (!is_continuation(c, tty)) {
  825. num_chars++;
  826. }
  827. }
  828. echo_erase_tab(num_chars, after_tab, tty);
  829. } else {
  830. if (iscntrl(c) && L_ECHOCTL(tty)) {
  831. echo_char_raw('\b', tty);
  832. echo_char_raw(' ', tty);
  833. echo_char_raw('\b', tty);
  834. }
  835. if (!iscntrl(c) || L_ECHOCTL(tty)) {
  836. echo_char_raw('\b', tty);
  837. echo_char_raw(' ', tty);
  838. echo_char_raw('\b', tty);
  839. }
  840. }
  841. }
  842. if (kill_type == ERASE)
  843. break;
  844. }
  845. if (tty->read_head == tty->canon_head && L_ECHO(tty))
  846. finish_erasing(tty);
  847. }
  848. /**
  849. * isig - handle the ISIG optio
  850. * @sig: signal
  851. * @tty: terminal
  852. * @flush: force flush
  853. *
  854. * Called when a signal is being sent due to terminal input. This
  855. * may caus terminal flushing to take place according to the termios
  856. * settings and character used. Called from the driver receive_buf
  857. * path so serialized.
  858. *
  859. * Locking: ctrl_lock, read_lock (both via flush buffer)
  860. */
  861. static inline void isig(int sig, struct tty_struct *tty, int flush)
  862. {
  863. if (tty->pgrp)
  864. kill_pgrp(tty->pgrp, sig, 1);
  865. if (flush || !L_NOFLSH(tty)) {
  866. n_tty_flush_buffer(tty);
  867. tty_driver_flush_buffer(tty);
  868. }
  869. }
  870. /**
  871. * n_tty_receive_break - handle break
  872. * @tty: terminal
  873. *
  874. * An RS232 break event has been hit in the incoming bitstream. This
  875. * can cause a variety of events depending upon the termios settings.
  876. *
  877. * Called from the receive_buf path so single threaded.
  878. */
  879. static inline void n_tty_receive_break(struct tty_struct *tty)
  880. {
  881. if (I_IGNBRK(tty))
  882. return;
  883. if (I_BRKINT(tty)) {
  884. isig(SIGINT, tty, 1);
  885. return;
  886. }
  887. if (I_PARMRK(tty)) {
  888. put_tty_queue('\377', tty);
  889. put_tty_queue('\0', tty);
  890. }
  891. put_tty_queue('\0', tty);
  892. wake_up_interruptible(&tty->read_wait);
  893. }
  894. /**
  895. * n_tty_receive_overrun - handle overrun reporting
  896. * @tty: terminal
  897. *
  898. * Data arrived faster than we could process it. While the tty
  899. * driver has flagged this the bits that were missed are gone
  900. * forever.
  901. *
  902. * Called from the receive_buf path so single threaded. Does not
  903. * need locking as num_overrun and overrun_time are function
  904. * private.
  905. */
  906. static inline void n_tty_receive_overrun(struct tty_struct *tty)
  907. {
  908. char buf[64];
  909. tty->num_overrun++;
  910. if (time_before(tty->overrun_time, jiffies - HZ) ||
  911. time_after(tty->overrun_time, jiffies)) {
  912. printk(KERN_WARNING "%s: %d input overrun(s)\n",
  913. tty_name(tty, buf),
  914. tty->num_overrun);
  915. tty->overrun_time = jiffies;
  916. tty->num_overrun = 0;
  917. }
  918. }
  919. /**
  920. * n_tty_receive_parity_error - error notifier
  921. * @tty: terminal device
  922. * @c: character
  923. *
  924. * Process a parity error and queue the right data to indicate
  925. * the error case if necessary. Locking as per n_tty_receive_buf.
  926. */
  927. static inline void n_tty_receive_parity_error(struct tty_struct *tty,
  928. unsigned char c)
  929. {
  930. if (I_IGNPAR(tty))
  931. return;
  932. if (I_PARMRK(tty)) {
  933. put_tty_queue('\377', tty);
  934. put_tty_queue('\0', tty);
  935. put_tty_queue(c, tty);
  936. } else if (I_INPCK(tty))
  937. put_tty_queue('\0', tty);
  938. else
  939. put_tty_queue(c, tty);
  940. wake_up_interruptible(&tty->read_wait);
  941. }
  942. /**
  943. * n_tty_receive_char - perform processing
  944. * @tty: terminal device
  945. * @c: character
  946. *
  947. * Process an individual character of input received from the driver.
  948. * This is serialized with respect to itself by the rules for the
  949. * driver above.
  950. */
  951. static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
  952. {
  953. unsigned long flags;
  954. int parmrk;
  955. if (tty->raw) {
  956. put_tty_queue(c, tty);
  957. return;
  958. }
  959. if (I_ISTRIP(tty))
  960. c &= 0x7f;
  961. if (I_IUCLC(tty) && L_IEXTEN(tty))
  962. c = tolower(c);
  963. if (L_EXTPROC(tty)) {
  964. put_tty_queue(c, tty);
  965. return;
  966. }
  967. if (tty->stopped && !tty->flow_stopped && I_IXON(tty) &&
  968. I_IXANY(tty) && c != START_CHAR(tty) && c != STOP_CHAR(tty) &&
  969. c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) && c != SUSP_CHAR(tty)) {
  970. start_tty(tty);
  971. process_echoes(tty);
  972. }
  973. if (tty->closing) {
  974. if (I_IXON(tty)) {
  975. if (c == START_CHAR(tty)) {
  976. start_tty(tty);
  977. process_echoes(tty);
  978. } else if (c == STOP_CHAR(tty))
  979. stop_tty(tty);
  980. }
  981. return;
  982. }
  983. /*
  984. * If the previous character was LNEXT, or we know that this
  985. * character is not one of the characters that we'll have to
  986. * handle specially, do shortcut processing to speed things
  987. * up.
  988. */
  989. if (!test_bit(c, tty->process_char_map) || tty->lnext) {
  990. tty->lnext = 0;
  991. parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
  992. if (tty->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
  993. /* beep if no space */
  994. if (L_ECHO(tty))
  995. process_output('\a', tty);
  996. return;
  997. }
  998. if (L_ECHO(tty)) {
  999. finish_erasing(tty);
  1000. /* Record the column of first canon char. */
  1001. if (tty->canon_head == tty->read_head)
  1002. echo_set_canon_col(tty);
  1003. echo_char(c, tty);
  1004. process_echoes(tty);
  1005. }
  1006. if (parmrk)
  1007. put_tty_queue(c, tty);
  1008. put_tty_queue(c, tty);
  1009. return;
  1010. }
  1011. if (I_IXON(tty)) {
  1012. if (c == START_CHAR(tty)) {
  1013. start_tty(tty);
  1014. process_echoes(tty);
  1015. return;
  1016. }
  1017. if (c == STOP_CHAR(tty)) {
  1018. stop_tty(tty);
  1019. return;
  1020. }
  1021. }
  1022. if (L_ISIG(tty)) {
  1023. int signal;
  1024. signal = SIGINT;
  1025. if (c == INTR_CHAR(tty))
  1026. goto send_signal;
  1027. signal = SIGQUIT;
  1028. if (c == QUIT_CHAR(tty))
  1029. goto send_signal;
  1030. signal = SIGTSTP;
  1031. if (c == SUSP_CHAR(tty)) {
  1032. send_signal:
  1033. /*
  1034. * Note that we do not use isig() here because we want
  1035. * the order to be:
  1036. * 1) flush, 2) echo, 3) signal
  1037. */
  1038. if (!L_NOFLSH(tty)) {
  1039. n_tty_flush_buffer(tty);
  1040. tty_driver_flush_buffer(tty);
  1041. }
  1042. if (I_IXON(tty))
  1043. start_tty(tty);
  1044. if (L_ECHO(tty)) {
  1045. echo_char(c, tty);
  1046. process_echoes(tty);
  1047. }
  1048. if (tty->pgrp)
  1049. kill_pgrp(tty->pgrp, signal, 1);
  1050. return;
  1051. }
  1052. }
  1053. if (c == '\r') {
  1054. if (I_IGNCR(tty))
  1055. return;
  1056. if (I_ICRNL(tty))
  1057. c = '\n';
  1058. } else if (c == '\n' && I_INLCR(tty))
  1059. c = '\r';
  1060. if (tty->icanon) {
  1061. if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
  1062. (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
  1063. eraser(c, tty);
  1064. process_echoes(tty);
  1065. return;
  1066. }
  1067. if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
  1068. tty->lnext = 1;
  1069. if (L_ECHO(tty)) {
  1070. finish_erasing(tty);
  1071. if (L_ECHOCTL(tty)) {
  1072. echo_char_raw('^', tty);
  1073. echo_char_raw('\b', tty);
  1074. process_echoes(tty);
  1075. }
  1076. }
  1077. return;
  1078. }
  1079. if (c == REPRINT_CHAR(tty) && L_ECHO(tty) &&
  1080. L_IEXTEN(tty)) {
  1081. unsigned long tail = tty->canon_head;
  1082. finish_erasing(tty);
  1083. echo_char(c, tty);
  1084. echo_char_raw('\n', tty);
  1085. while (tail != tty->read_head) {
  1086. echo_char(tty->read_buf[tail], tty);
  1087. tail = (tail+1) & (N_TTY_BUF_SIZE-1);
  1088. }
  1089. process_echoes(tty);
  1090. return;
  1091. }
  1092. if (c == '\n') {
  1093. if (tty->read_cnt >= N_TTY_BUF_SIZE) {
  1094. if (L_ECHO(tty))
  1095. process_output('\a', tty);
  1096. return;
  1097. }
  1098. if (L_ECHO(tty) || L_ECHONL(tty)) {
  1099. echo_char_raw('\n', tty);
  1100. process_echoes(tty);
  1101. }
  1102. goto handle_newline;
  1103. }
  1104. if (c == EOF_CHAR(tty)) {
  1105. if (tty->read_cnt >= N_TTY_BUF_SIZE)
  1106. return;
  1107. if (tty->canon_head != tty->read_head)
  1108. set_bit(TTY_PUSH, &tty->flags);
  1109. c = __DISABLED_CHAR;
  1110. goto handle_newline;
  1111. }
  1112. if ((c == EOL_CHAR(tty)) ||
  1113. (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
  1114. parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty))
  1115. ? 1 : 0;
  1116. if (tty->read_cnt >= (N_TTY_BUF_SIZE - parmrk)) {
  1117. if (L_ECHO(tty))
  1118. process_output('\a', tty);
  1119. return;
  1120. }
  1121. /*
  1122. * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
  1123. */
  1124. if (L_ECHO(tty)) {
  1125. /* Record the column of first canon char. */
  1126. if (tty->canon_head == tty->read_head)
  1127. echo_set_canon_col(tty);
  1128. echo_char(c, tty);
  1129. process_echoes(tty);
  1130. }
  1131. /*
  1132. * XXX does PARMRK doubling happen for
  1133. * EOL_CHAR and EOL2_CHAR?
  1134. */
  1135. if (parmrk)
  1136. put_tty_queue(c, tty);
  1137. handle_newline:
  1138. spin_lock_irqsave(&tty->read_lock, flags);
  1139. set_bit(tty->read_head, tty->read_flags);
  1140. put_tty_queue_nolock(c, tty);
  1141. tty->canon_head = tty->read_head;
  1142. tty->canon_data++;
  1143. spin_unlock_irqrestore(&tty->read_lock, flags);
  1144. kill_fasync(&tty->fasync, SIGIO, POLL_IN);
  1145. if (waitqueue_active(&tty->read_wait))
  1146. wake_up_interruptible(&tty->read_wait);
  1147. return;
  1148. }
  1149. }
  1150. parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
  1151. if (tty->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
  1152. /* beep if no space */
  1153. if (L_ECHO(tty))
  1154. process_output('\a', tty);
  1155. return;
  1156. }
  1157. if (L_ECHO(tty)) {
  1158. finish_erasing(tty);
  1159. if (c == '\n')
  1160. echo_char_raw('\n', tty);
  1161. else {
  1162. /* Record the column of first canon char. */
  1163. if (tty->canon_head == tty->read_head)
  1164. echo_set_canon_col(tty);
  1165. echo_char(c, tty);
  1166. }
  1167. process_echoes(tty);
  1168. }
  1169. if (parmrk)
  1170. put_tty_queue(c, tty);
  1171. put_tty_queue(c, tty);
  1172. }
  1173. /**
  1174. * n_tty_write_wakeup - asynchronous I/O notifier
  1175. * @tty: tty device
  1176. *
  1177. * Required for the ptys, serial driver etc. since processes
  1178. * that attach themselves to the master and rely on ASYNC
  1179. * IO must be woken up
  1180. */
  1181. static void n_tty_write_wakeup(struct tty_struct *tty)
  1182. {
  1183. if (tty->fasync && test_and_clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags))
  1184. kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
  1185. }
  1186. /**
  1187. * n_tty_receive_buf - data receive
  1188. * @tty: terminal device
  1189. * @cp: buffer
  1190. * @fp: flag buffer
  1191. * @count: characters
  1192. *
  1193. * Called by the terminal driver when a block of characters has
  1194. * been received. This function must be called from soft contexts
  1195. * not from interrupt context. The driver is responsible for making
  1196. * calls one at a time and in order (or using flush_to_ldisc)
  1197. */
  1198. static unsigned int n_tty_receive_buf(struct tty_struct *tty,
  1199. const unsigned char *cp, char *fp, int count)
  1200. {
  1201. const unsigned char *p;
  1202. char *f, flags = TTY_NORMAL;
  1203. int i;
  1204. char buf[64];
  1205. unsigned long cpuflags;
  1206. int left;
  1207. int ret = 0;
  1208. if (!tty->read_buf)
  1209. return 0;
  1210. if (tty->real_raw) {
  1211. spin_lock_irqsave(&tty->read_lock, cpuflags);
  1212. i = min(N_TTY_BUF_SIZE - tty->read_cnt,
  1213. N_TTY_BUF_SIZE - tty->read_head);
  1214. i = min(count, i);
  1215. memcpy(tty->read_buf + tty->read_head, cp, i);
  1216. tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
  1217. tty->read_cnt += i;
  1218. ret += i;
  1219. cp += i;
  1220. count -= i;
  1221. i = min(N_TTY_BUF_SIZE - tty->read_cnt,
  1222. N_TTY_BUF_SIZE - tty->read_head);
  1223. i = min(count, i);
  1224. memcpy(tty->read_buf + tty->read_head, cp, i);
  1225. tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
  1226. tty->read_cnt += i;
  1227. ret += i;
  1228. spin_unlock_irqrestore(&tty->read_lock, cpuflags);
  1229. } else {
  1230. ret = count;
  1231. for (i = count, p = cp, f = fp; i; i--, p++) {
  1232. if (f)
  1233. flags = *f++;
  1234. switch (flags) {
  1235. case TTY_NORMAL:
  1236. n_tty_receive_char(tty, *p);
  1237. break;
  1238. case TTY_BREAK:
  1239. n_tty_receive_break(tty);
  1240. break;
  1241. case TTY_PARITY:
  1242. case TTY_FRAME:
  1243. n_tty_receive_parity_error(tty, *p);
  1244. break;
  1245. case TTY_OVERRUN:
  1246. n_tty_receive_overrun(tty);
  1247. break;
  1248. default:
  1249. printk(KERN_ERR "%s: unknown flag %d\n",
  1250. tty_name(tty, buf), flags);
  1251. break;
  1252. }
  1253. }
  1254. if (tty->ops->flush_chars)
  1255. tty->ops->flush_chars(tty);
  1256. }
  1257. if ((!tty->icanon && (tty->read_cnt >= tty->minimum_to_wake)) ||
  1258. L_EXTPROC(tty)) {
  1259. kill_fasync(&tty->fasync, SIGIO, POLL_IN);
  1260. if (waitqueue_active(&tty->read_wait))
  1261. wake_up_interruptible(&tty->read_wait);
  1262. }
  1263. /*
  1264. * Check the remaining room for the input canonicalization
  1265. * mode. We don't want to throttle the driver if we're in
  1266. * canonical mode and don't have a newline yet!
  1267. */
  1268. left = N_TTY_BUF_SIZE - tty->read_cnt - 1;
  1269. if (left < TTY_THRESHOLD_THROTTLE)
  1270. tty_throttle(tty);
  1271. return ret;
  1272. }
  1273. int is_ignored(int sig)
  1274. {
  1275. return (sigismember(&current->blocked, sig) ||
  1276. current->sighand->action[sig-1].sa.sa_handler == SIG_IGN);
  1277. }
  1278. /**
  1279. * n_tty_set_termios - termios data changed
  1280. * @tty: terminal
  1281. * @old: previous data
  1282. *
  1283. * Called by the tty layer when the user changes termios flags so
  1284. * that the line discipline can plan ahead. This function cannot sleep
  1285. * and is protected from re-entry by the tty layer. The user is
  1286. * guaranteed that this function will not be re-entered or in progress
  1287. * when the ldisc is closed.
  1288. *
  1289. * Locking: Caller holds tty->termios_mutex
  1290. */
  1291. static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
  1292. {
  1293. int canon_change = 1;
  1294. BUG_ON(!tty);
  1295. if (old)
  1296. canon_change = (old->c_lflag ^ tty->termios->c_lflag) & ICANON;
  1297. if (canon_change) {
  1298. memset(&tty->read_flags, 0, sizeof tty->read_flags);
  1299. tty->canon_head = tty->read_tail;
  1300. tty->canon_data = 0;
  1301. tty->erasing = 0;
  1302. }
  1303. if (canon_change && !L_ICANON(tty) && tty->read_cnt)
  1304. wake_up_interruptible(&tty->read_wait);
  1305. tty->icanon = (L_ICANON(tty) != 0);
  1306. if (test_bit(TTY_HW_COOK_IN, &tty->flags)) {
  1307. tty->raw = 1;
  1308. tty->real_raw = 1;
  1309. return;
  1310. }
  1311. if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
  1312. I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
  1313. I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
  1314. I_PARMRK(tty)) {
  1315. memset(tty->process_char_map, 0, 256/8);
  1316. if (I_IGNCR(tty) || I_ICRNL(tty))
  1317. set_bit('\r', tty->process_char_map);
  1318. if (I_INLCR(tty))
  1319. set_bit('\n', tty->process_char_map);
  1320. if (L_ICANON(tty)) {
  1321. set_bit(ERASE_CHAR(tty), tty->process_char_map);
  1322. set_bit(KILL_CHAR(tty), tty->process_char_map);
  1323. set_bit(EOF_CHAR(tty), tty->process_char_map);
  1324. set_bit('\n', tty->process_char_map);
  1325. set_bit(EOL_CHAR(tty), tty->process_char_map);
  1326. if (L_IEXTEN(tty)) {
  1327. set_bit(WERASE_CHAR(tty),
  1328. tty->process_char_map);
  1329. set_bit(LNEXT_CHAR(tty),
  1330. tty->process_char_map);
  1331. set_bit(EOL2_CHAR(tty),
  1332. tty->process_char_map);
  1333. if (L_ECHO(tty))
  1334. set_bit(REPRINT_CHAR(tty),
  1335. tty->process_char_map);
  1336. }
  1337. }
  1338. if (I_IXON(tty)) {
  1339. set_bit(START_CHAR(tty), tty->process_char_map);
  1340. set_bit(STOP_CHAR(tty), tty->process_char_map);
  1341. }
  1342. if (L_ISIG(tty)) {
  1343. set_bit(INTR_CHAR(tty), tty->process_char_map);
  1344. set_bit(QUIT_CHAR(tty), tty->process_char_map);
  1345. set_bit(SUSP_CHAR(tty), tty->process_char_map);
  1346. }
  1347. clear_bit(__DISABLED_CHAR, tty->process_char_map);
  1348. tty->raw = 0;
  1349. tty->real_raw = 0;
  1350. } else {
  1351. tty->raw = 1;
  1352. if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
  1353. (I_IGNPAR(tty) || !I_INPCK(tty)) &&
  1354. (tty->driver->flags & TTY_DRIVER_REAL_RAW))
  1355. tty->real_raw = 1;
  1356. else
  1357. tty->real_raw = 0;
  1358. }
  1359. /* The termios change make the tty ready for I/O */
  1360. wake_up_interruptible(&tty->write_wait);
  1361. wake_up_interruptible(&tty->read_wait);
  1362. }
  1363. /**
  1364. * n_tty_close - close the ldisc for this tty
  1365. * @tty: device
  1366. *
  1367. * Called from the terminal layer when this line discipline is
  1368. * being shut down, either because of a close or becsuse of a
  1369. * discipline change. The function will not be called while other
  1370. * ldisc methods are in progress.
  1371. */
  1372. static void n_tty_close(struct tty_struct *tty)
  1373. {
  1374. n_tty_flush_buffer(tty);
  1375. if (tty->read_buf) {
  1376. kfree(tty->read_buf);
  1377. tty->read_buf = NULL;
  1378. }
  1379. if (tty->echo_buf) {
  1380. kfree(tty->echo_buf);
  1381. tty->echo_buf = NULL;
  1382. }
  1383. }
  1384. /**
  1385. * n_tty_open - open an ldisc
  1386. * @tty: terminal to open
  1387. *
  1388. * Called when this line discipline is being attached to the
  1389. * terminal device. Can sleep. Called serialized so that no
  1390. * other events will occur in parallel. No further open will occur
  1391. * until a close.
  1392. */
  1393. static int n_tty_open(struct tty_struct *tty)
  1394. {
  1395. if (!tty)
  1396. return -EINVAL;
  1397. /* These are ugly. Currently a malloc failure here can panic */
  1398. if (!tty->read_buf) {
  1399. tty->read_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
  1400. if (!tty->read_buf)
  1401. return -ENOMEM;
  1402. }
  1403. if (!tty->echo_buf) {
  1404. tty->echo_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
  1405. if (!tty->echo_buf)
  1406. return -ENOMEM;
  1407. }
  1408. reset_buffer_flags(tty);
  1409. tty->column = 0;
  1410. n_tty_set_termios(tty, NULL);
  1411. tty->minimum_to_wake = 1;
  1412. tty->closing = 0;
  1413. return 0;
  1414. }
  1415. static inline int input_available_p(struct tty_struct *tty, int amt)
  1416. {
  1417. tty_flush_to_ldisc(tty);
  1418. if (tty->icanon && !L_EXTPROC(tty)) {
  1419. if (tty->canon_data)
  1420. return 1;
  1421. } else if (tty->read_cnt >= (amt ? amt : 1))
  1422. return 1;
  1423. return 0;
  1424. }
  1425. /**
  1426. * copy_from_read_buf - copy read data directly
  1427. * @tty: terminal device
  1428. * @b: user data
  1429. * @nr: size of data
  1430. *
  1431. * Helper function to speed up n_tty_read. It is only called when
  1432. * ICANON is off; it copies characters straight from the tty queue to
  1433. * user space directly. It can be profitably called twice; once to
  1434. * drain the space from the tail pointer to the (physical) end of the
  1435. * buffer, and once to drain the space from the (physical) beginning of
  1436. * the buffer to head pointer.
  1437. *
  1438. * Called under the tty->atomic_read_lock sem
  1439. *
  1440. */
  1441. static int copy_from_read_buf(struct tty_struct *tty,
  1442. unsigned char __user **b,
  1443. size_t *nr)
  1444. {
  1445. int retval;
  1446. size_t n;
  1447. unsigned long flags;
  1448. retval = 0;
  1449. spin_lock_irqsave(&tty->read_lock, flags);
  1450. n = min(tty->read_cnt, N_TTY_BUF_SIZE - tty->read_tail);
  1451. n = min(*nr, n);
  1452. spin_unlock_irqrestore(&tty->read_lock, flags);
  1453. if (n) {
  1454. retval = copy_to_user(*b, &tty->read_buf[tty->read_tail], n);
  1455. n -= retval;
  1456. tty_audit_add_data(tty, &tty->read_buf[tty->read_tail], n);
  1457. spin_lock_irqsave(&tty->read_lock, flags);
  1458. tty->read_tail = (tty->read_tail + n) & (N_TTY_BUF_SIZE-1);
  1459. tty->read_cnt -= n;
  1460. /* Turn single EOF into zero-length read */
  1461. if (L_EXTPROC(tty) && tty->icanon && n == 1) {
  1462. if (!tty->read_cnt && (*b)[n-1] == EOF_CHAR(tty))
  1463. n--;
  1464. }
  1465. spin_unlock_irqrestore(&tty->read_lock, flags);
  1466. *b += n;
  1467. *nr -= n;
  1468. }
  1469. return retval;
  1470. }
  1471. extern ssize_t redirected_tty_write(struct file *, const char __user *,
  1472. size_t, loff_t *);
  1473. /**
  1474. * job_control - check job control
  1475. * @tty: tty
  1476. * @file: file handle
  1477. *
  1478. * Perform job control management checks on this file/tty descriptor
  1479. * and if appropriate send any needed signals and return a negative
  1480. * error code if action should be taken.
  1481. *
  1482. * FIXME:
  1483. * Locking: None - redirected write test is safe, testing
  1484. * current->signal should possibly lock current->sighand
  1485. * pgrp locking ?
  1486. */
  1487. static int job_control(struct tty_struct *tty, struct file *file)
  1488. {
  1489. /* Job control check -- must be done at start and after
  1490. every sleep (POSIX.1 7.1.1.4). */
  1491. /* NOTE: not yet done after every sleep pending a thorough
  1492. check of the logic of this change. -- jlc */
  1493. /* don't stop on /dev/console */
  1494. if (file->f_op->write != redirected_tty_write &&
  1495. current->signal->tty == tty) {
  1496. if (!tty->pgrp)
  1497. printk(KERN_ERR "n_tty_read: no tty->pgrp!\n");
  1498. else if (task_pgrp(current) != tty->pgrp) {
  1499. if (is_ignored(SIGTTIN) ||
  1500. is_current_pgrp_orphaned())
  1501. return -EIO;
  1502. kill_pgrp(task_pgrp(current), SIGTTIN, 1);
  1503. set_thread_flag(TIF_SIGPENDING);
  1504. return -ERESTARTSYS;
  1505. }
  1506. }
  1507. return 0;
  1508. }
  1509. /**
  1510. * n_tty_read - read function for tty
  1511. * @tty: tty device
  1512. * @file: file object
  1513. * @buf: userspace buffer pointer
  1514. * @nr: size of I/O
  1515. *
  1516. * Perform reads for the line discipline. We are guaranteed that the
  1517. * line discipline will not be closed under us but we may get multiple
  1518. * parallel readers and must handle this ourselves. We may also get
  1519. * a hangup. Always called in user context, may sleep.
  1520. *
  1521. * This code must be sure never to sleep through a hangup.
  1522. */
  1523. static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
  1524. unsigned char __user *buf, size_t nr)
  1525. {
  1526. unsigned char __user *b = buf;
  1527. DECLARE_WAITQUEUE(wait, current);
  1528. int c;
  1529. int minimum, time;
  1530. ssize_t retval = 0;
  1531. ssize_t size;
  1532. long timeout;
  1533. unsigned long flags;
  1534. int packet;
  1535. do_it_again:
  1536. BUG_ON(!tty->read_buf);
  1537. c = job_control(tty, file);
  1538. if (c < 0)
  1539. return c;
  1540. minimum = time = 0;
  1541. timeout = MAX_SCHEDULE_TIMEOUT;
  1542. if (!tty->icanon) {
  1543. time = (HZ / 10) * TIME_CHAR(tty);
  1544. minimum = MIN_CHAR(tty);
  1545. if (minimum) {
  1546. if (time)
  1547. tty->minimum_to_wake = 1;
  1548. else if (!waitqueue_active(&tty->read_wait) ||
  1549. (tty->minimum_to_wake > minimum))
  1550. tty->minimum_to_wake = minimum;
  1551. } else {
  1552. timeout = 0;
  1553. if (time) {
  1554. timeout = time;
  1555. time = 0;
  1556. }
  1557. tty->minimum_to_wake = minimum = 1;
  1558. }
  1559. }
  1560. /*
  1561. * Internal serialization of reads.
  1562. */
  1563. if (file->f_flags & O_NONBLOCK) {
  1564. if (!mutex_trylock(&tty->atomic_read_lock))
  1565. return -EAGAIN;
  1566. } else {
  1567. if (mutex_lock_interruptible(&tty->atomic_read_lock))
  1568. return -ERESTARTSYS;
  1569. }
  1570. packet = tty->packet;
  1571. add_wait_queue(&tty->read_wait, &wait);
  1572. while (nr) {
  1573. /* First test for status change. */
  1574. if (packet && tty->link->ctrl_status) {
  1575. unsigned char cs;
  1576. if (b != buf)
  1577. break;
  1578. spin_lock_irqsave(&tty->link->ctrl_lock, flags);
  1579. cs = tty->link->ctrl_status;
  1580. tty->link->ctrl_status = 0;
  1581. spin_unlock_irqrestore(&tty->link->ctrl_lock, flags);
  1582. if (tty_put_user(tty, cs, b++)) {
  1583. retval = -EFAULT;
  1584. b--;
  1585. break;
  1586. }
  1587. nr--;
  1588. break;
  1589. }
  1590. /* This statement must be first before checking for input
  1591. so that any interrupt will set the state back to
  1592. TASK_RUNNING. */
  1593. set_current_state(TASK_INTERRUPTIBLE);
  1594. if (((minimum - (b - buf)) < tty->minimum_to_wake) &&
  1595. ((minimum - (b - buf)) >= 1))
  1596. tty->minimum_to_wake = (minimum - (b - buf));
  1597. if (!input_available_p(tty, 0)) {
  1598. if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
  1599. retval = -EIO;
  1600. break;
  1601. }
  1602. if (tty_hung_up_p(file))
  1603. break;
  1604. if (!timeout)
  1605. break;
  1606. if (file->f_flags & O_NONBLOCK) {
  1607. retval = -EAGAIN;
  1608. break;
  1609. }
  1610. if (signal_pending(current)) {
  1611. retval = -ERESTARTSYS;
  1612. break;
  1613. }
  1614. timeout = schedule_timeout(timeout);
  1615. continue;
  1616. }
  1617. __set_current_state(TASK_RUNNING);
  1618. /* Deal with packet mode. */
  1619. if (packet && b == buf) {
  1620. if (tty_put_user(tty, TIOCPKT_DATA, b++)) {
  1621. retval = -EFAULT;
  1622. b--;
  1623. break;
  1624. }
  1625. nr--;
  1626. }
  1627. if (tty->icanon && !L_EXTPROC(tty)) {
  1628. /* N.B. avoid overrun if nr == 0 */
  1629. while (nr && tty->read_cnt) {
  1630. int eol;
  1631. eol = test_and_clear_bit(tty->read_tail,
  1632. tty->read_flags);
  1633. c = tty->read_buf[tty->read_tail];
  1634. spin_lock_irqsave(&tty->read_lock, flags);
  1635. tty->read_tail = ((tty->read_tail+1) &
  1636. (N_TTY_BUF_SIZE-1));
  1637. tty->read_cnt--;
  1638. if (eol) {
  1639. /* this test should be redundant:
  1640. * we shouldn't be reading data if
  1641. * canon_data is 0
  1642. */
  1643. if (--tty->canon_data < 0)
  1644. tty->canon_data = 0;
  1645. }
  1646. spin_unlock_irqrestore(&tty->read_lock, flags);
  1647. if (!eol || (c != __DISABLED_CHAR)) {
  1648. if (tty_put_user(tty, c, b++)) {
  1649. retval = -EFAULT;
  1650. b--;
  1651. break;
  1652. }
  1653. nr--;
  1654. }
  1655. if (eol) {
  1656. tty_audit_push(tty);
  1657. break;
  1658. }
  1659. }
  1660. if (retval)
  1661. break;
  1662. } else {
  1663. int uncopied;
  1664. /* The copy function takes the read lock and handles
  1665. locking internally for this case */
  1666. uncopied = copy_from_read_buf(tty, &b, &nr);
  1667. uncopied += copy_from_read_buf(tty, &b, &nr);
  1668. if (uncopied) {
  1669. retval = -EFAULT;
  1670. break;
  1671. }
  1672. }
  1673. /* If there is enough space in the read buffer now, let the
  1674. * low-level driver know. We use n_tty_chars_in_buffer() to
  1675. * check the buffer, as it now knows about canonical mode.
  1676. * Otherwise, if the driver is throttled and the line is
  1677. * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
  1678. * we won't get any more characters.
  1679. */
  1680. if (n_tty_chars_in_buffer(tty) <= TTY_THRESHOLD_UNTHROTTLE)
  1681. check_unthrottle(tty);
  1682. if (b - buf >= minimum)
  1683. break;
  1684. if (time)
  1685. timeout = time;
  1686. }
  1687. mutex_unlock(&tty->atomic_read_lock);
  1688. remove_wait_queue(&tty->read_wait, &wait);
  1689. if (!waitqueue_active(&tty->read_wait))
  1690. tty->minimum_to_wake = minimum;
  1691. __set_current_state(TASK_RUNNING);
  1692. size = b - buf;
  1693. if (size) {
  1694. retval = size;
  1695. if (nr)
  1696. clear_bit(TTY_PUSH, &tty->flags);
  1697. } else if (test_and_clear_bit(TTY_PUSH, &tty->flags))
  1698. goto do_it_again;
  1699. return retval;
  1700. }
  1701. /**
  1702. * n_tty_write - write function for tty
  1703. * @tty: tty device
  1704. * @file: file object
  1705. * @buf: userspace buffer pointer
  1706. * @nr: size of I/O
  1707. *
  1708. * Write function of the terminal device. This is serialized with
  1709. * respect to other write callers but not to termios changes, reads
  1710. * and other such events. Since the receive code will echo characters,
  1711. * thus calling driver write methods, the output_lock is used in
  1712. * the output processing functions called here as well as in the
  1713. * echo processing function to protect the column state and space
  1714. * left in the buffer.
  1715. *
  1716. * This code must be sure never to sleep through a hangup.
  1717. *
  1718. * Locking: output_lock to protect column state and space left
  1719. * (note that the process_output*() functions take this
  1720. * lock themselves)
  1721. */
  1722. static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
  1723. const unsigned char *buf, size_t nr)
  1724. {
  1725. const unsigned char *b = buf;
  1726. DECLARE_WAITQUEUE(wait, current);
  1727. int c;
  1728. ssize_t retval = 0;
  1729. /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
  1730. if (L_TOSTOP(tty) && file->f_op->write != redirected_tty_write) {
  1731. retval = tty_check_change(tty);
  1732. if (retval)
  1733. return retval;
  1734. }
  1735. /* Write out any echoed characters that are still pending */
  1736. process_echoes(tty);
  1737. add_wait_queue(&tty->write_wait, &wait);
  1738. while (1) {
  1739. set_current_state(TASK_INTERRUPTIBLE);
  1740. if (signal_pending(current)) {
  1741. retval = -ERESTARTSYS;
  1742. break;
  1743. }
  1744. if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
  1745. retval = -EIO;
  1746. break;
  1747. }
  1748. if (O_OPOST(tty) && !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
  1749. while (nr > 0) {
  1750. ssize_t num = process_output_block(tty, b, nr);
  1751. if (num < 0) {
  1752. if (num == -EAGAIN)
  1753. break;
  1754. retval = num;
  1755. goto break_out;
  1756. }
  1757. b += num;
  1758. nr -= num;
  1759. if (nr == 0)
  1760. break;
  1761. c = *b;
  1762. if (process_output(c, tty) < 0)
  1763. break;
  1764. b++; nr--;
  1765. }
  1766. if (tty->ops->flush_chars)
  1767. tty->ops->flush_chars(tty);
  1768. } else {
  1769. while (nr > 0) {
  1770. c = tty->ops->write(tty, b, nr);
  1771. if (c < 0) {
  1772. retval = c;
  1773. goto break_out;
  1774. }
  1775. if (!c)
  1776. break;
  1777. b += c;
  1778. nr -= c;
  1779. }
  1780. }
  1781. if (!nr)
  1782. break;
  1783. if (file->f_flags & O_NONBLOCK) {
  1784. retval = -EAGAIN;
  1785. break;
  1786. }
  1787. schedule();
  1788. }
  1789. break_out:
  1790. __set_current_state(TASK_RUNNING);
  1791. remove_wait_queue(&tty->write_wait, &wait);
  1792. if (b - buf != nr && tty->fasync)
  1793. set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  1794. return (b - buf) ? b - buf : retval;
  1795. }
  1796. /**
  1797. * n_tty_poll - poll method for N_TTY
  1798. * @tty: terminal device
  1799. * @file: file accessing it
  1800. * @wait: poll table
  1801. *
  1802. * Called when the line discipline is asked to poll() for data or
  1803. * for special events. This code is not serialized with respect to
  1804. * other events save open/close.
  1805. *
  1806. * This code must be sure never to sleep through a hangup.
  1807. * Called without the kernel lock held - fine
  1808. */
  1809. static unsigned int n_tty_poll(struct tty_struct *tty, struct file *file,
  1810. poll_table *wait)
  1811. {
  1812. unsigned int mask = 0;
  1813. poll_wait(file, &tty->read_wait, wait);
  1814. poll_wait(file, &tty->write_wait, wait);
  1815. if (input_available_p(tty, TIME_CHAR(tty) ? 0 : MIN_CHAR(tty)))
  1816. mask |= POLLIN | POLLRDNORM;
  1817. if (tty->packet && tty->link->ctrl_status)
  1818. mask |= POLLPRI | POLLIN | POLLRDNORM;
  1819. if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
  1820. mask |= POLLHUP;
  1821. if (tty_hung_up_p(file))
  1822. mask |= POLLHUP;
  1823. if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) {
  1824. if (MIN_CHAR(tty) && !TIME_CHAR(tty))
  1825. tty->minimum_to_wake = MIN_CHAR(tty);
  1826. else
  1827. tty->minimum_to_wake = 1;
  1828. }
  1829. if (tty->ops->write && !tty_is_writelocked(tty) &&
  1830. tty_chars_in_buffer(tty) < WAKEUP_CHARS &&
  1831. tty_write_room(tty) > 0)
  1832. mask |= POLLOUT | POLLWRNORM;
  1833. return mask;
  1834. }
  1835. static unsigned long inq_canon(struct tty_struct *tty)
  1836. {
  1837. int nr, head, tail;
  1838. if (!tty->canon_data)
  1839. return 0;
  1840. head = tty->canon_head;
  1841. tail = tty->read_tail;
  1842. nr = (head - tail) & (N_TTY_BUF_SIZE-1);
  1843. /* Skip EOF-chars.. */
  1844. while (head != tail) {
  1845. if (test_bit(tail, tty->read_flags) &&
  1846. tty->read_buf[tail] == __DISABLED_CHAR)
  1847. nr--;
  1848. tail = (tail+1) & (N_TTY_BUF_SIZE-1);
  1849. }
  1850. return nr;
  1851. }
  1852. static int n_tty_ioctl(struct tty_struct *tty, struct file *file,
  1853. unsigned int cmd, unsigned long arg)
  1854. {
  1855. int retval;
  1856. switch (cmd) {
  1857. case TIOCOUTQ:
  1858. return put_user(tty_chars_in_buffer(tty), (int __user *) arg);
  1859. case TIOCINQ:
  1860. /* FIXME: Locking */
  1861. retval = tty->read_cnt;
  1862. if (L_ICANON(tty))
  1863. retval = inq_canon(tty);
  1864. return put_user(retval, (unsigned int __user *) arg);
  1865. default:
  1866. return n_tty_ioctl_helper(tty, file, cmd, arg);
  1867. }
  1868. }
  1869. struct tty_ldisc_ops tty_ldisc_N_TTY = {
  1870. .magic = TTY_LDISC_MAGIC,
  1871. .name = "n_tty",
  1872. .open = n_tty_open,
  1873. .close = n_tty_close,
  1874. .flush_buffer = n_tty_flush_buffer,
  1875. .chars_in_buffer = n_tty_chars_in_buffer,
  1876. .read = n_tty_read,
  1877. .write = n_tty_write,
  1878. .ioctl = n_tty_ioctl,
  1879. .set_termios = n_tty_set_termios,
  1880. .poll = n_tty_poll,
  1881. .receive_buf = n_tty_receive_buf,
  1882. .write_wakeup = n_tty_write_wakeup
  1883. };
  1884. /**
  1885. * n_tty_inherit_ops - inherit N_TTY methods
  1886. * @ops: struct tty_ldisc_ops where to save N_TTY methods
  1887. *
  1888. * Used by a generic struct tty_ldisc_ops to easily inherit N_TTY
  1889. * methods.
  1890. */
  1891. void n_tty_inherit_ops(struct tty_ldisc_ops *ops)
  1892. {
  1893. *ops = tty_ldisc_N_TTY;
  1894. ops->owner = NULL;
  1895. ops->refcount = ops->flags = 0;
  1896. }
  1897. EXPORT_SYMBOL_GPL(n_tty_inherit_ops);