n_tty.c 55 KB

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