n_tty.c 51 KB

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