n_tty.c 51 KB

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