n_tty.c 51 KB

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