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->ops->write(tty, "\r\n", 2);
  271. return 2;
  272. }
  273. tty->canon_column = tty->column;
  274. break;
  275. case '\r':
  276. if (O_ONOCR(tty) && tty->column == 0)
  277. return 0;
  278. if (O_OCRNL(tty)) {
  279. c = '\n';
  280. if (O_ONLRET(tty))
  281. tty->canon_column = tty->column = 0;
  282. break;
  283. }
  284. tty->canon_column = tty->column = 0;
  285. break;
  286. case '\t':
  287. spaces = 8 - (tty->column & 7);
  288. if (O_TABDLY(tty) == XTABS) {
  289. if (space < spaces)
  290. return -1;
  291. tty->column += spaces;
  292. tty->ops->write(tty, " ", spaces);
  293. return spaces;
  294. }
  295. tty->column += spaces;
  296. break;
  297. case '\b':
  298. if (tty->column > 0)
  299. tty->column--;
  300. break;
  301. default:
  302. if (!iscntrl(c)) {
  303. if (O_OLCUC(tty))
  304. c = toupper(c);
  305. if (!is_continuation(c, tty))
  306. tty->column++;
  307. }
  308. break;
  309. }
  310. tty_put_char(tty, c);
  311. return 1;
  312. }
  313. /**
  314. * process_output - output post processor
  315. * @c: character (or partial unicode symbol)
  316. * @tty: terminal device
  317. *
  318. * Perform OPOST processing. Returns -1 when the output device is
  319. * full and the character must be retried.
  320. *
  321. * Locking: output_lock to protect column state and space left
  322. * (also, this is called from n_tty_write under the
  323. * tty layer write lock)
  324. */
  325. static int process_output(unsigned char c, struct tty_struct *tty)
  326. {
  327. int space, retval;
  328. mutex_lock(&tty->output_lock);
  329. space = tty_write_room(tty);
  330. retval = do_output_char(c, tty, space);
  331. mutex_unlock(&tty->output_lock);
  332. if (retval < 0)
  333. return -1;
  334. else
  335. return 0;
  336. }
  337. /**
  338. * process_output_block - block post processor
  339. * @tty: terminal device
  340. * @inbuf: user buffer
  341. * @nr: number of bytes
  342. *
  343. * This path is used to speed up block console writes, among other
  344. * things when processing blocks of output data. It handles only
  345. * the simple cases normally found and helps to generate blocks of
  346. * symbols for the console driver and thus improve performance.
  347. *
  348. * Locking: output_lock to protect column state and space left
  349. * (also, this is called from n_tty_write under the
  350. * tty layer write lock)
  351. */
  352. static ssize_t process_output_block(struct tty_struct *tty,
  353. const unsigned char *buf, unsigned int nr)
  354. {
  355. int space;
  356. int i;
  357. const unsigned char *cp;
  358. mutex_lock(&tty->output_lock);
  359. space = tty_write_room(tty);
  360. if (!space) {
  361. mutex_unlock(&tty->output_lock);
  362. return 0;
  363. }
  364. if (nr > space)
  365. nr = space;
  366. for (i = 0, cp = buf; i < nr; i++, cp++) {
  367. unsigned char c = *cp;
  368. switch (c) {
  369. case '\n':
  370. if (O_ONLRET(tty))
  371. tty->column = 0;
  372. if (O_ONLCR(tty))
  373. goto break_out;
  374. tty->canon_column = tty->column;
  375. break;
  376. case '\r':
  377. if (O_ONOCR(tty) && tty->column == 0)
  378. goto break_out;
  379. if (O_OCRNL(tty))
  380. goto break_out;
  381. tty->canon_column = tty->column = 0;
  382. break;
  383. case '\t':
  384. goto break_out;
  385. case '\b':
  386. if (tty->column > 0)
  387. tty->column--;
  388. break;
  389. default:
  390. if (!iscntrl(c)) {
  391. if (O_OLCUC(tty))
  392. goto break_out;
  393. if (!is_continuation(c, tty))
  394. tty->column++;
  395. }
  396. break;
  397. }
  398. }
  399. break_out:
  400. i = tty->ops->write(tty, buf, i);
  401. mutex_unlock(&tty->output_lock);
  402. return i;
  403. }
  404. /**
  405. * process_echoes - write pending echo characters
  406. * @tty: terminal device
  407. *
  408. * Write previously buffered echo (and other ldisc-generated)
  409. * characters to the tty.
  410. *
  411. * Characters generated by the ldisc (including echoes) need to
  412. * be buffered because the driver's write buffer can fill during
  413. * heavy program output. Echoing straight to the driver will
  414. * often fail under these conditions, causing lost characters and
  415. * resulting mismatches of ldisc state information.
  416. *
  417. * Since the ldisc state must represent the characters actually sent
  418. * to the driver at the time of the write, operations like certain
  419. * changes in column state are also saved in the buffer and executed
  420. * here.
  421. *
  422. * A circular fifo buffer is used so that the most recent characters
  423. * are prioritized. Also, when control characters are echoed with a
  424. * prefixed "^", the pair is treated atomically and thus not separated.
  425. *
  426. * Locking: output_lock to protect column state and space left,
  427. * echo_lock to protect the echo buffer
  428. */
  429. static void process_echoes(struct tty_struct *tty)
  430. {
  431. int space, nr;
  432. unsigned char c;
  433. unsigned char *cp, *buf_end;
  434. if (!tty->echo_cnt)
  435. return;
  436. mutex_lock(&tty->output_lock);
  437. mutex_lock(&tty->echo_lock);
  438. space = tty_write_room(tty);
  439. buf_end = tty->echo_buf + N_TTY_BUF_SIZE;
  440. cp = tty->echo_buf + tty->echo_pos;
  441. nr = tty->echo_cnt;
  442. while (nr > 0) {
  443. c = *cp;
  444. if (c == ECHO_OP_START) {
  445. unsigned char op;
  446. unsigned char *opp;
  447. int no_space_left = 0;
  448. /*
  449. * If the buffer byte is the start of a multi-byte
  450. * operation, get the next byte, which is either the
  451. * op code or a control character value.
  452. */
  453. opp = cp + 1;
  454. if (opp == buf_end)
  455. opp -= N_TTY_BUF_SIZE;
  456. op = *opp;
  457. switch (op) {
  458. unsigned int num_chars, num_bs;
  459. case ECHO_OP_ERASE_TAB:
  460. if (++opp == buf_end)
  461. opp -= N_TTY_BUF_SIZE;
  462. num_chars = *opp;
  463. /*
  464. * Determine how many columns to go back
  465. * in order to erase the tab.
  466. * This depends on the number of columns
  467. * used by other characters within the tab
  468. * area. If this (modulo 8) count is from
  469. * the start of input rather than from a
  470. * previous tab, we offset by canon column.
  471. * Otherwise, tab spacing is normal.
  472. */
  473. if (!(num_chars & 0x80))
  474. num_chars += tty->canon_column;
  475. num_bs = 8 - (num_chars & 7);
  476. if (num_bs > space) {
  477. no_space_left = 1;
  478. break;
  479. }
  480. space -= num_bs;
  481. while (num_bs--) {
  482. tty_put_char(tty, '\b');
  483. if (tty->column > 0)
  484. tty->column--;
  485. }
  486. cp += 3;
  487. nr -= 3;
  488. break;
  489. case ECHO_OP_SET_CANON_COL:
  490. tty->canon_column = tty->column;
  491. cp += 2;
  492. nr -= 2;
  493. break;
  494. case ECHO_OP_MOVE_BACK_COL:
  495. if (tty->column > 0)
  496. tty->column--;
  497. cp += 2;
  498. nr -= 2;
  499. break;
  500. case ECHO_OP_START:
  501. /* This is an escaped echo op start code */
  502. if (!space) {
  503. no_space_left = 1;
  504. break;
  505. }
  506. tty_put_char(tty, ECHO_OP_START);
  507. tty->column++;
  508. space--;
  509. cp += 2;
  510. nr -= 2;
  511. break;
  512. default:
  513. if (iscntrl(op)) {
  514. if (L_ECHOCTL(tty)) {
  515. /*
  516. * Ensure there is enough space
  517. * for the whole ctrl pair.
  518. */
  519. if (space < 2) {
  520. no_space_left = 1;
  521. break;
  522. }
  523. tty_put_char(tty, '^');
  524. tty_put_char(tty, op ^ 0100);
  525. tty->column += 2;
  526. space -= 2;
  527. } else {
  528. if (!space) {
  529. no_space_left = 1;
  530. break;
  531. }
  532. tty_put_char(tty, op);
  533. space--;
  534. }
  535. }
  536. /*
  537. * If above falls through, this was an
  538. * undefined op.
  539. */
  540. cp += 2;
  541. nr -= 2;
  542. }
  543. if (no_space_left)
  544. break;
  545. } else {
  546. int retval;
  547. retval = do_output_char(c, tty, space);
  548. if (retval < 0)
  549. break;
  550. space -= retval;
  551. cp += 1;
  552. nr -= 1;
  553. }
  554. /* When end of circular buffer reached, wrap around */
  555. if (cp >= buf_end)
  556. cp -= N_TTY_BUF_SIZE;
  557. }
  558. if (nr == 0) {
  559. tty->echo_pos = 0;
  560. tty->echo_cnt = 0;
  561. tty->echo_overrun = 0;
  562. } else {
  563. int num_processed = tty->echo_cnt - nr;
  564. tty->echo_pos += num_processed;
  565. tty->echo_pos &= N_TTY_BUF_SIZE - 1;
  566. tty->echo_cnt = nr;
  567. if (num_processed > 0)
  568. tty->echo_overrun = 0;
  569. }
  570. mutex_unlock(&tty->echo_lock);
  571. mutex_unlock(&tty->output_lock);
  572. if (tty->ops->flush_chars)
  573. tty->ops->flush_chars(tty);
  574. }
  575. /**
  576. * add_echo_byte - add a byte to the echo buffer
  577. * @c: unicode byte to echo
  578. * @tty: terminal device
  579. *
  580. * Add a character or operation byte to the echo buffer.
  581. *
  582. * Should be called under the echo lock to protect the echo buffer.
  583. */
  584. static void add_echo_byte(unsigned char c, struct tty_struct *tty)
  585. {
  586. int new_byte_pos;
  587. if (tty->echo_cnt == N_TTY_BUF_SIZE) {
  588. /* Circular buffer is already at capacity */
  589. new_byte_pos = tty->echo_pos;
  590. /*
  591. * Since the buffer start position needs to be advanced,
  592. * be sure to step by a whole operation byte group.
  593. */
  594. if (tty->echo_buf[tty->echo_pos] == ECHO_OP_START) {
  595. if (tty->echo_buf[(tty->echo_pos + 1) &
  596. (N_TTY_BUF_SIZE - 1)] ==
  597. ECHO_OP_ERASE_TAB) {
  598. tty->echo_pos += 3;
  599. tty->echo_cnt -= 2;
  600. } else {
  601. tty->echo_pos += 2;
  602. tty->echo_cnt -= 1;
  603. }
  604. } else {
  605. tty->echo_pos++;
  606. }
  607. tty->echo_pos &= N_TTY_BUF_SIZE - 1;
  608. tty->echo_overrun = 1;
  609. } else {
  610. new_byte_pos = tty->echo_pos + tty->echo_cnt;
  611. new_byte_pos &= N_TTY_BUF_SIZE - 1;
  612. tty->echo_cnt++;
  613. }
  614. tty->echo_buf[new_byte_pos] = c;
  615. }
  616. /**
  617. * echo_move_back_col - add operation to move back a column
  618. * @tty: terminal device
  619. *
  620. * Add an operation to the echo buffer to move back one column.
  621. *
  622. * Locking: echo_lock to protect the echo buffer
  623. */
  624. static void echo_move_back_col(struct tty_struct *tty)
  625. {
  626. mutex_lock(&tty->echo_lock);
  627. add_echo_byte(ECHO_OP_START, tty);
  628. add_echo_byte(ECHO_OP_MOVE_BACK_COL, tty);
  629. mutex_unlock(&tty->echo_lock);
  630. }
  631. /**
  632. * echo_set_canon_col - add operation to set the canon column
  633. * @tty: terminal device
  634. *
  635. * Add an operation to the echo buffer to set the canon column
  636. * to the current column.
  637. *
  638. * Locking: echo_lock to protect the echo buffer
  639. */
  640. static void echo_set_canon_col(struct tty_struct *tty)
  641. {
  642. mutex_lock(&tty->echo_lock);
  643. add_echo_byte(ECHO_OP_START, tty);
  644. add_echo_byte(ECHO_OP_SET_CANON_COL, tty);
  645. mutex_unlock(&tty->echo_lock);
  646. }
  647. /**
  648. * echo_erase_tab - add operation to erase a tab
  649. * @num_chars: number of character columns already used
  650. * @after_tab: true if num_chars starts after a previous tab
  651. * @tty: terminal device
  652. *
  653. * Add an operation to the echo buffer to erase a tab.
  654. *
  655. * Called by the eraser function, which knows how many character
  656. * columns have been used since either a previous tab or the start
  657. * of input. This information will be used later, along with
  658. * canon column (if applicable), to go back the correct number
  659. * of columns.
  660. *
  661. * Locking: echo_lock to protect the echo buffer
  662. */
  663. static void echo_erase_tab(unsigned int num_chars, int after_tab,
  664. struct tty_struct *tty)
  665. {
  666. mutex_lock(&tty->echo_lock);
  667. add_echo_byte(ECHO_OP_START, tty);
  668. add_echo_byte(ECHO_OP_ERASE_TAB, tty);
  669. /* We only need to know this modulo 8 (tab spacing) */
  670. num_chars &= 7;
  671. /* Set the high bit as a flag if num_chars is after a previous tab */
  672. if (after_tab)
  673. num_chars |= 0x80;
  674. add_echo_byte(num_chars, tty);
  675. mutex_unlock(&tty->echo_lock);
  676. }
  677. /**
  678. * echo_char_raw - echo a character raw
  679. * @c: unicode byte to echo
  680. * @tty: terminal device
  681. *
  682. * Echo user input back onto the screen. This must be called only when
  683. * L_ECHO(tty) is true. Called from the driver receive_buf path.
  684. *
  685. * This variant does not treat control characters specially.
  686. *
  687. * Locking: echo_lock to protect the echo buffer
  688. */
  689. static void echo_char_raw(unsigned char c, struct tty_struct *tty)
  690. {
  691. mutex_lock(&tty->echo_lock);
  692. if (c == ECHO_OP_START) {
  693. add_echo_byte(ECHO_OP_START, tty);
  694. add_echo_byte(ECHO_OP_START, tty);
  695. } else {
  696. add_echo_byte(c, tty);
  697. }
  698. mutex_unlock(&tty->echo_lock);
  699. }
  700. /**
  701. * echo_char - echo a character
  702. * @c: unicode byte to echo
  703. * @tty: terminal device
  704. *
  705. * Echo user input back onto the screen. This must be called only when
  706. * L_ECHO(tty) is true. Called from the driver receive_buf path.
  707. *
  708. * This variant tags control characters to be possibly echoed as
  709. * as "^X" (where X is the letter representing the control char).
  710. *
  711. * Locking: echo_lock to protect the echo buffer
  712. */
  713. static void echo_char(unsigned char c, struct tty_struct *tty)
  714. {
  715. mutex_lock(&tty->echo_lock);
  716. if (c == ECHO_OP_START) {
  717. add_echo_byte(ECHO_OP_START, tty);
  718. add_echo_byte(ECHO_OP_START, tty);
  719. } else {
  720. if (iscntrl(c) && c != '\t')
  721. add_echo_byte(ECHO_OP_START, tty);
  722. add_echo_byte(c, tty);
  723. }
  724. mutex_unlock(&tty->echo_lock);
  725. }
  726. /**
  727. * finish_erasing - complete erase
  728. * @tty: tty doing the erase
  729. */
  730. static inline void finish_erasing(struct tty_struct *tty)
  731. {
  732. if (tty->erasing) {
  733. echo_char_raw('/', tty);
  734. tty->erasing = 0;
  735. }
  736. }
  737. /**
  738. * eraser - handle erase function
  739. * @c: character input
  740. * @tty: terminal device
  741. *
  742. * Perform erase and necessary output when an erase character is
  743. * present in the stream from the driver layer. Handles the complexities
  744. * of UTF-8 multibyte symbols.
  745. *
  746. * Locking: read_lock for tty buffers
  747. */
  748. static void eraser(unsigned char c, struct tty_struct *tty)
  749. {
  750. enum { ERASE, WERASE, KILL } kill_type;
  751. int head, seen_alnums, cnt;
  752. unsigned long flags;
  753. /* FIXME: locking needed ? */
  754. if (tty->read_head == tty->canon_head) {
  755. /* process_output('\a', tty); */ /* what do you think? */
  756. return;
  757. }
  758. if (c == ERASE_CHAR(tty))
  759. kill_type = ERASE;
  760. else if (c == WERASE_CHAR(tty))
  761. kill_type = WERASE;
  762. else {
  763. if (!L_ECHO(tty)) {
  764. spin_lock_irqsave(&tty->read_lock, flags);
  765. tty->read_cnt -= ((tty->read_head - tty->canon_head) &
  766. (N_TTY_BUF_SIZE - 1));
  767. tty->read_head = tty->canon_head;
  768. spin_unlock_irqrestore(&tty->read_lock, flags);
  769. return;
  770. }
  771. if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
  772. spin_lock_irqsave(&tty->read_lock, flags);
  773. tty->read_cnt -= ((tty->read_head - tty->canon_head) &
  774. (N_TTY_BUF_SIZE - 1));
  775. tty->read_head = tty->canon_head;
  776. spin_unlock_irqrestore(&tty->read_lock, flags);
  777. finish_erasing(tty);
  778. echo_char(KILL_CHAR(tty), tty);
  779. /* Add a newline if ECHOK is on and ECHOKE is off. */
  780. if (L_ECHOK(tty))
  781. echo_char_raw('\n', tty);
  782. return;
  783. }
  784. kill_type = KILL;
  785. }
  786. seen_alnums = 0;
  787. /* FIXME: Locking ?? */
  788. while (tty->read_head != tty->canon_head) {
  789. head = tty->read_head;
  790. /* erase a single possibly multibyte character */
  791. do {
  792. head = (head - 1) & (N_TTY_BUF_SIZE-1);
  793. c = tty->read_buf[head];
  794. } while (is_continuation(c, tty) && head != tty->canon_head);
  795. /* do not partially erase */
  796. if (is_continuation(c, tty))
  797. break;
  798. if (kill_type == WERASE) {
  799. /* Equivalent to BSD's ALTWERASE. */
  800. if (isalnum(c) || c == '_')
  801. seen_alnums++;
  802. else if (seen_alnums)
  803. break;
  804. }
  805. cnt = (tty->read_head - head) & (N_TTY_BUF_SIZE-1);
  806. spin_lock_irqsave(&tty->read_lock, flags);
  807. tty->read_head = head;
  808. tty->read_cnt -= cnt;
  809. spin_unlock_irqrestore(&tty->read_lock, flags);
  810. if (L_ECHO(tty)) {
  811. if (L_ECHOPRT(tty)) {
  812. if (!tty->erasing) {
  813. echo_char_raw('\\', tty);
  814. tty->erasing = 1;
  815. }
  816. /* if cnt > 1, output a multi-byte character */
  817. echo_char(c, tty);
  818. while (--cnt > 0) {
  819. head = (head+1) & (N_TTY_BUF_SIZE-1);
  820. echo_char_raw(tty->read_buf[head], tty);
  821. echo_move_back_col(tty);
  822. }
  823. } else if (kill_type == ERASE && !L_ECHOE(tty)) {
  824. echo_char(ERASE_CHAR(tty), tty);
  825. } else if (c == '\t') {
  826. unsigned int num_chars = 0;
  827. int after_tab = 0;
  828. unsigned long tail = tty->read_head;
  829. /*
  830. * Count the columns used for characters
  831. * since the start of input or after a
  832. * previous tab.
  833. * This info is used to go back the correct
  834. * number of columns.
  835. */
  836. while (tail != tty->canon_head) {
  837. tail = (tail-1) & (N_TTY_BUF_SIZE-1);
  838. c = tty->read_buf[tail];
  839. if (c == '\t') {
  840. after_tab = 1;
  841. break;
  842. } else if (iscntrl(c)) {
  843. if (L_ECHOCTL(tty))
  844. num_chars += 2;
  845. } else if (!is_continuation(c, tty)) {
  846. num_chars++;
  847. }
  848. }
  849. echo_erase_tab(num_chars, after_tab, tty);
  850. } else {
  851. if (iscntrl(c) && L_ECHOCTL(tty)) {
  852. echo_char_raw('\b', tty);
  853. echo_char_raw(' ', tty);
  854. echo_char_raw('\b', tty);
  855. }
  856. if (!iscntrl(c) || L_ECHOCTL(tty)) {
  857. echo_char_raw('\b', tty);
  858. echo_char_raw(' ', tty);
  859. echo_char_raw('\b', tty);
  860. }
  861. }
  862. }
  863. if (kill_type == ERASE)
  864. break;
  865. }
  866. if (tty->read_head == tty->canon_head && L_ECHO(tty))
  867. finish_erasing(tty);
  868. }
  869. /**
  870. * isig - handle the ISIG optio
  871. * @sig: signal
  872. * @tty: terminal
  873. * @flush: force flush
  874. *
  875. * Called when a signal is being sent due to terminal input. This
  876. * may caus terminal flushing to take place according to the termios
  877. * settings and character used. Called from the driver receive_buf
  878. * path so serialized.
  879. *
  880. * Locking: ctrl_lock, read_lock (both via flush buffer)
  881. */
  882. static inline void isig(int sig, struct tty_struct *tty, int flush)
  883. {
  884. if (tty->pgrp)
  885. kill_pgrp(tty->pgrp, sig, 1);
  886. if (flush || !L_NOFLSH(tty)) {
  887. n_tty_flush_buffer(tty);
  888. tty_driver_flush_buffer(tty);
  889. }
  890. }
  891. /**
  892. * n_tty_receive_break - handle break
  893. * @tty: terminal
  894. *
  895. * An RS232 break event has been hit in the incoming bitstream. This
  896. * can cause a variety of events depending upon the termios settings.
  897. *
  898. * Called from the receive_buf path so single threaded.
  899. */
  900. static inline void n_tty_receive_break(struct tty_struct *tty)
  901. {
  902. if (I_IGNBRK(tty))
  903. return;
  904. if (I_BRKINT(tty)) {
  905. isig(SIGINT, tty, 1);
  906. return;
  907. }
  908. if (I_PARMRK(tty)) {
  909. put_tty_queue('\377', tty);
  910. put_tty_queue('\0', tty);
  911. }
  912. put_tty_queue('\0', tty);
  913. wake_up_interruptible(&tty->read_wait);
  914. }
  915. /**
  916. * n_tty_receive_overrun - handle overrun reporting
  917. * @tty: terminal
  918. *
  919. * Data arrived faster than we could process it. While the tty
  920. * driver has flagged this the bits that were missed are gone
  921. * forever.
  922. *
  923. * Called from the receive_buf path so single threaded. Does not
  924. * need locking as num_overrun and overrun_time are function
  925. * private.
  926. */
  927. static inline void n_tty_receive_overrun(struct tty_struct *tty)
  928. {
  929. char buf[64];
  930. tty->num_overrun++;
  931. if (time_before(tty->overrun_time, jiffies - HZ) ||
  932. time_after(tty->overrun_time, jiffies)) {
  933. printk(KERN_WARNING "%s: %d input overrun(s)\n",
  934. tty_name(tty, buf),
  935. tty->num_overrun);
  936. tty->overrun_time = jiffies;
  937. tty->num_overrun = 0;
  938. }
  939. }
  940. /**
  941. * n_tty_receive_parity_error - error notifier
  942. * @tty: terminal device
  943. * @c: character
  944. *
  945. * Process a parity error and queue the right data to indicate
  946. * the error case if necessary. Locking as per n_tty_receive_buf.
  947. */
  948. static inline void n_tty_receive_parity_error(struct tty_struct *tty,
  949. unsigned char c)
  950. {
  951. if (I_IGNPAR(tty))
  952. return;
  953. if (I_PARMRK(tty)) {
  954. put_tty_queue('\377', tty);
  955. put_tty_queue('\0', tty);
  956. put_tty_queue(c, tty);
  957. } else if (I_INPCK(tty))
  958. put_tty_queue('\0', tty);
  959. else
  960. put_tty_queue(c, tty);
  961. wake_up_interruptible(&tty->read_wait);
  962. }
  963. /**
  964. * n_tty_receive_char - perform processing
  965. * @tty: terminal device
  966. * @c: character
  967. *
  968. * Process an individual character of input received from the driver.
  969. * This is serialized with respect to itself by the rules for the
  970. * driver above.
  971. */
  972. static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
  973. {
  974. unsigned long flags;
  975. int parmrk;
  976. if (tty->raw) {
  977. put_tty_queue(c, tty);
  978. return;
  979. }
  980. if (I_ISTRIP(tty))
  981. c &= 0x7f;
  982. if (I_IUCLC(tty) && L_IEXTEN(tty))
  983. c = tolower(c);
  984. if (tty->stopped && !tty->flow_stopped && I_IXON(tty) &&
  985. I_IXANY(tty) && c != START_CHAR(tty) && c != STOP_CHAR(tty) &&
  986. c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) && c != SUSP_CHAR(tty)) {
  987. start_tty(tty);
  988. process_echoes(tty);
  989. }
  990. if (tty->closing) {
  991. if (I_IXON(tty)) {
  992. if (c == START_CHAR(tty)) {
  993. start_tty(tty);
  994. process_echoes(tty);
  995. } else if (c == STOP_CHAR(tty))
  996. stop_tty(tty);
  997. }
  998. return;
  999. }
  1000. /*
  1001. * If the previous character was LNEXT, or we know that this
  1002. * character is not one of the characters that we'll have to
  1003. * handle specially, do shortcut processing to speed things
  1004. * up.
  1005. */
  1006. if (!test_bit(c, tty->process_char_map) || tty->lnext) {
  1007. tty->lnext = 0;
  1008. parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
  1009. if (tty->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
  1010. /* beep if no space */
  1011. if (L_ECHO(tty))
  1012. process_output('\a', tty);
  1013. return;
  1014. }
  1015. if (L_ECHO(tty)) {
  1016. finish_erasing(tty);
  1017. /* Record the column of first canon char. */
  1018. if (tty->canon_head == tty->read_head)
  1019. echo_set_canon_col(tty);
  1020. echo_char(c, tty);
  1021. process_echoes(tty);
  1022. }
  1023. if (parmrk)
  1024. put_tty_queue(c, tty);
  1025. put_tty_queue(c, tty);
  1026. return;
  1027. }
  1028. if (I_IXON(tty)) {
  1029. if (c == START_CHAR(tty)) {
  1030. start_tty(tty);
  1031. process_echoes(tty);
  1032. return;
  1033. }
  1034. if (c == STOP_CHAR(tty)) {
  1035. stop_tty(tty);
  1036. return;
  1037. }
  1038. }
  1039. if (L_ISIG(tty)) {
  1040. int signal;
  1041. signal = SIGINT;
  1042. if (c == INTR_CHAR(tty))
  1043. goto send_signal;
  1044. signal = SIGQUIT;
  1045. if (c == QUIT_CHAR(tty))
  1046. goto send_signal;
  1047. signal = SIGTSTP;
  1048. if (c == SUSP_CHAR(tty)) {
  1049. send_signal:
  1050. /*
  1051. * Note that we do not use isig() here because we want
  1052. * the order to be:
  1053. * 1) flush, 2) echo, 3) signal
  1054. */
  1055. if (!L_NOFLSH(tty)) {
  1056. n_tty_flush_buffer(tty);
  1057. tty_driver_flush_buffer(tty);
  1058. }
  1059. if (I_IXON(tty))
  1060. start_tty(tty);
  1061. if (L_ECHO(tty)) {
  1062. echo_char(c, tty);
  1063. process_echoes(tty);
  1064. }
  1065. if (tty->pgrp)
  1066. kill_pgrp(tty->pgrp, signal, 1);
  1067. return;
  1068. }
  1069. }
  1070. if (c == '\r') {
  1071. if (I_IGNCR(tty))
  1072. return;
  1073. if (I_ICRNL(tty))
  1074. c = '\n';
  1075. } else if (c == '\n' && I_INLCR(tty))
  1076. c = '\r';
  1077. if (tty->icanon) {
  1078. if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
  1079. (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
  1080. eraser(c, tty);
  1081. process_echoes(tty);
  1082. return;
  1083. }
  1084. if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
  1085. tty->lnext = 1;
  1086. if (L_ECHO(tty)) {
  1087. finish_erasing(tty);
  1088. if (L_ECHOCTL(tty)) {
  1089. echo_char_raw('^', tty);
  1090. echo_char_raw('\b', tty);
  1091. process_echoes(tty);
  1092. }
  1093. }
  1094. return;
  1095. }
  1096. if (c == REPRINT_CHAR(tty) && L_ECHO(tty) &&
  1097. L_IEXTEN(tty)) {
  1098. unsigned long tail = tty->canon_head;
  1099. finish_erasing(tty);
  1100. echo_char(c, tty);
  1101. echo_char_raw('\n', tty);
  1102. while (tail != tty->read_head) {
  1103. echo_char(tty->read_buf[tail], tty);
  1104. tail = (tail+1) & (N_TTY_BUF_SIZE-1);
  1105. }
  1106. process_echoes(tty);
  1107. return;
  1108. }
  1109. if (c == '\n') {
  1110. if (tty->read_cnt >= N_TTY_BUF_SIZE) {
  1111. if (L_ECHO(tty))
  1112. process_output('\a', tty);
  1113. return;
  1114. }
  1115. if (L_ECHO(tty) || L_ECHONL(tty)) {
  1116. echo_char_raw('\n', tty);
  1117. process_echoes(tty);
  1118. }
  1119. goto handle_newline;
  1120. }
  1121. if (c == EOF_CHAR(tty)) {
  1122. if (tty->read_cnt >= N_TTY_BUF_SIZE)
  1123. return;
  1124. if (tty->canon_head != tty->read_head)
  1125. set_bit(TTY_PUSH, &tty->flags);
  1126. c = __DISABLED_CHAR;
  1127. goto handle_newline;
  1128. }
  1129. if ((c == EOL_CHAR(tty)) ||
  1130. (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
  1131. parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty))
  1132. ? 1 : 0;
  1133. if (tty->read_cnt >= (N_TTY_BUF_SIZE - parmrk)) {
  1134. if (L_ECHO(tty))
  1135. process_output('\a', tty);
  1136. return;
  1137. }
  1138. /*
  1139. * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
  1140. */
  1141. if (L_ECHO(tty)) {
  1142. /* Record the column of first canon char. */
  1143. if (tty->canon_head == tty->read_head)
  1144. echo_set_canon_col(tty);
  1145. echo_char(c, tty);
  1146. process_echoes(tty);
  1147. }
  1148. /*
  1149. * XXX does PARMRK doubling happen for
  1150. * EOL_CHAR and EOL2_CHAR?
  1151. */
  1152. if (parmrk)
  1153. put_tty_queue(c, tty);
  1154. handle_newline:
  1155. spin_lock_irqsave(&tty->read_lock, flags);
  1156. set_bit(tty->read_head, tty->read_flags);
  1157. put_tty_queue_nolock(c, tty);
  1158. tty->canon_head = tty->read_head;
  1159. tty->canon_data++;
  1160. spin_unlock_irqrestore(&tty->read_lock, flags);
  1161. kill_fasync(&tty->fasync, SIGIO, POLL_IN);
  1162. if (waitqueue_active(&tty->read_wait))
  1163. wake_up_interruptible(&tty->read_wait);
  1164. return;
  1165. }
  1166. }
  1167. parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
  1168. if (tty->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
  1169. /* beep if no space */
  1170. if (L_ECHO(tty))
  1171. process_output('\a', tty);
  1172. return;
  1173. }
  1174. if (L_ECHO(tty)) {
  1175. finish_erasing(tty);
  1176. if (c == '\n')
  1177. echo_char_raw('\n', tty);
  1178. else {
  1179. /* Record the column of first canon char. */
  1180. if (tty->canon_head == tty->read_head)
  1181. echo_set_canon_col(tty);
  1182. echo_char(c, tty);
  1183. }
  1184. process_echoes(tty);
  1185. }
  1186. if (parmrk)
  1187. put_tty_queue(c, tty);
  1188. put_tty_queue(c, tty);
  1189. }
  1190. /**
  1191. * n_tty_write_wakeup - asynchronous I/O notifier
  1192. * @tty: tty device
  1193. *
  1194. * Required for the ptys, serial driver etc. since processes
  1195. * that attach themselves to the master and rely on ASYNC
  1196. * IO must be woken up
  1197. */
  1198. static void n_tty_write_wakeup(struct tty_struct *tty)
  1199. {
  1200. if (tty->fasync && test_and_clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags))
  1201. kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
  1202. }
  1203. /**
  1204. * n_tty_receive_buf - data receive
  1205. * @tty: terminal device
  1206. * @cp: buffer
  1207. * @fp: flag buffer
  1208. * @count: characters
  1209. *
  1210. * Called by the terminal driver when a block of characters has
  1211. * been received. This function must be called from soft contexts
  1212. * not from interrupt context. The driver is responsible for making
  1213. * calls one at a time and in order (or using flush_to_ldisc)
  1214. */
  1215. static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
  1216. char *fp, int count)
  1217. {
  1218. const unsigned char *p;
  1219. char *f, flags = TTY_NORMAL;
  1220. int i;
  1221. char buf[64];
  1222. unsigned long cpuflags;
  1223. if (!tty->read_buf)
  1224. return;
  1225. if (tty->real_raw) {
  1226. spin_lock_irqsave(&tty->read_lock, cpuflags);
  1227. i = min(N_TTY_BUF_SIZE - tty->read_cnt,
  1228. N_TTY_BUF_SIZE - tty->read_head);
  1229. i = min(count, i);
  1230. memcpy(tty->read_buf + tty->read_head, cp, i);
  1231. tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
  1232. tty->read_cnt += i;
  1233. cp += i;
  1234. count -= i;
  1235. i = min(N_TTY_BUF_SIZE - tty->read_cnt,
  1236. N_TTY_BUF_SIZE - tty->read_head);
  1237. i = min(count, i);
  1238. memcpy(tty->read_buf + tty->read_head, cp, i);
  1239. tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
  1240. tty->read_cnt += i;
  1241. spin_unlock_irqrestore(&tty->read_lock, cpuflags);
  1242. } else {
  1243. for (i = count, p = cp, f = fp; i; i--, p++) {
  1244. if (f)
  1245. flags = *f++;
  1246. switch (flags) {
  1247. case TTY_NORMAL:
  1248. n_tty_receive_char(tty, *p);
  1249. break;
  1250. case TTY_BREAK:
  1251. n_tty_receive_break(tty);
  1252. break;
  1253. case TTY_PARITY:
  1254. case TTY_FRAME:
  1255. n_tty_receive_parity_error(tty, *p);
  1256. break;
  1257. case TTY_OVERRUN:
  1258. n_tty_receive_overrun(tty);
  1259. break;
  1260. default:
  1261. printk(KERN_ERR "%s: unknown flag %d\n",
  1262. tty_name(tty, buf), flags);
  1263. break;
  1264. }
  1265. }
  1266. if (tty->ops->flush_chars)
  1267. tty->ops->flush_chars(tty);
  1268. }
  1269. n_tty_set_room(tty);
  1270. if (!tty->icanon && (tty->read_cnt >= tty->minimum_to_wake)) {
  1271. kill_fasync(&tty->fasync, SIGIO, POLL_IN);
  1272. if (waitqueue_active(&tty->read_wait))
  1273. wake_up_interruptible(&tty->read_wait);
  1274. }
  1275. /*
  1276. * Check the remaining room for the input canonicalization
  1277. * mode. We don't want to throttle the driver if we're in
  1278. * canonical mode and don't have a newline yet!
  1279. */
  1280. if (tty->receive_room < TTY_THRESHOLD_THROTTLE)
  1281. tty_throttle(tty);
  1282. }
  1283. int is_ignored(int sig)
  1284. {
  1285. return (sigismember(&current->blocked, sig) ||
  1286. current->sighand->action[sig-1].sa.sa_handler == SIG_IGN);
  1287. }
  1288. /**
  1289. * n_tty_set_termios - termios data changed
  1290. * @tty: terminal
  1291. * @old: previous data
  1292. *
  1293. * Called by the tty layer when the user changes termios flags so
  1294. * that the line discipline can plan ahead. This function cannot sleep
  1295. * and is protected from re-entry by the tty layer. The user is
  1296. * guaranteed that this function will not be re-entered or in progress
  1297. * when the ldisc is closed.
  1298. *
  1299. * Locking: Caller holds tty->termios_mutex
  1300. */
  1301. static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
  1302. {
  1303. int canon_change = 1;
  1304. BUG_ON(!tty);
  1305. if (old)
  1306. canon_change = (old->c_lflag ^ tty->termios->c_lflag) & ICANON;
  1307. if (canon_change) {
  1308. memset(&tty->read_flags, 0, sizeof tty->read_flags);
  1309. tty->canon_head = tty->read_tail;
  1310. tty->canon_data = 0;
  1311. tty->erasing = 0;
  1312. }
  1313. if (canon_change && !L_ICANON(tty) && tty->read_cnt)
  1314. wake_up_interruptible(&tty->read_wait);
  1315. tty->icanon = (L_ICANON(tty) != 0);
  1316. if (test_bit(TTY_HW_COOK_IN, &tty->flags)) {
  1317. tty->raw = 1;
  1318. tty->real_raw = 1;
  1319. n_tty_set_room(tty);
  1320. return;
  1321. }
  1322. if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
  1323. I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
  1324. I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
  1325. I_PARMRK(tty)) {
  1326. memset(tty->process_char_map, 0, 256/8);
  1327. if (I_IGNCR(tty) || I_ICRNL(tty))
  1328. set_bit('\r', tty->process_char_map);
  1329. if (I_INLCR(tty))
  1330. set_bit('\n', tty->process_char_map);
  1331. if (L_ICANON(tty)) {
  1332. set_bit(ERASE_CHAR(tty), tty->process_char_map);
  1333. set_bit(KILL_CHAR(tty), tty->process_char_map);
  1334. set_bit(EOF_CHAR(tty), tty->process_char_map);
  1335. set_bit('\n', tty->process_char_map);
  1336. set_bit(EOL_CHAR(tty), tty->process_char_map);
  1337. if (L_IEXTEN(tty)) {
  1338. set_bit(WERASE_CHAR(tty),
  1339. tty->process_char_map);
  1340. set_bit(LNEXT_CHAR(tty),
  1341. tty->process_char_map);
  1342. set_bit(EOL2_CHAR(tty),
  1343. tty->process_char_map);
  1344. if (L_ECHO(tty))
  1345. set_bit(REPRINT_CHAR(tty),
  1346. tty->process_char_map);
  1347. }
  1348. }
  1349. if (I_IXON(tty)) {
  1350. set_bit(START_CHAR(tty), tty->process_char_map);
  1351. set_bit(STOP_CHAR(tty), tty->process_char_map);
  1352. }
  1353. if (L_ISIG(tty)) {
  1354. set_bit(INTR_CHAR(tty), tty->process_char_map);
  1355. set_bit(QUIT_CHAR(tty), tty->process_char_map);
  1356. set_bit(SUSP_CHAR(tty), tty->process_char_map);
  1357. }
  1358. clear_bit(__DISABLED_CHAR, tty->process_char_map);
  1359. tty->raw = 0;
  1360. tty->real_raw = 0;
  1361. } else {
  1362. tty->raw = 1;
  1363. if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
  1364. (I_IGNPAR(tty) || !I_INPCK(tty)) &&
  1365. (tty->driver->flags & TTY_DRIVER_REAL_RAW))
  1366. tty->real_raw = 1;
  1367. else
  1368. tty->real_raw = 0;
  1369. }
  1370. n_tty_set_room(tty);
  1371. /* The termios change make the tty ready for I/O */
  1372. wake_up_interruptible(&tty->write_wait);
  1373. wake_up_interruptible(&tty->read_wait);
  1374. }
  1375. /**
  1376. * n_tty_close - close the ldisc for this tty
  1377. * @tty: device
  1378. *
  1379. * Called from the terminal layer when this line discipline is
  1380. * being shut down, either because of a close or becsuse of a
  1381. * discipline change. The function will not be called while other
  1382. * ldisc methods are in progress.
  1383. */
  1384. static void n_tty_close(struct tty_struct *tty)
  1385. {
  1386. n_tty_flush_buffer(tty);
  1387. if (tty->read_buf) {
  1388. kfree(tty->read_buf);
  1389. tty->read_buf = NULL;
  1390. }
  1391. if (tty->echo_buf) {
  1392. kfree(tty->echo_buf);
  1393. tty->echo_buf = NULL;
  1394. }
  1395. }
  1396. /**
  1397. * n_tty_open - open an ldisc
  1398. * @tty: terminal to open
  1399. *
  1400. * Called when this line discipline is being attached to the
  1401. * terminal device. Can sleep. Called serialized so that no
  1402. * other events will occur in parallel. No further open will occur
  1403. * until a close.
  1404. */
  1405. static int n_tty_open(struct tty_struct *tty)
  1406. {
  1407. if (!tty)
  1408. return -EINVAL;
  1409. /* These are ugly. Currently a malloc failure here can panic */
  1410. if (!tty->read_buf) {
  1411. tty->read_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
  1412. if (!tty->read_buf)
  1413. return -ENOMEM;
  1414. }
  1415. if (!tty->echo_buf) {
  1416. tty->echo_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
  1417. if (!tty->echo_buf)
  1418. return -ENOMEM;
  1419. }
  1420. reset_buffer_flags(tty);
  1421. tty->column = 0;
  1422. n_tty_set_termios(tty, NULL);
  1423. tty->minimum_to_wake = 1;
  1424. tty->closing = 0;
  1425. return 0;
  1426. }
  1427. static inline int input_available_p(struct tty_struct *tty, int amt)
  1428. {
  1429. tty_flush_to_ldisc(tty);
  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. };