n_tty.c 55 KB

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