n_tty.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110
  1. /*
  2. * n_tty.c --- implements the N_TTY line discipline.
  3. *
  4. * This code used to be in tty_io.c, but things are getting hairy
  5. * enough that it made sense to split things off. (The N_TTY
  6. * processing has changed so much that it's hardly recognizable,
  7. * anyway...)
  8. *
  9. * Note that the open routine for N_TTY is guaranteed never to return
  10. * an error. This is because Linux will fall back to setting a line
  11. * to N_TTY if it can not switch to any other line discipline.
  12. *
  13. * Written by Theodore Ts'o, Copyright 1994.
  14. *
  15. * This file also contains code originally written by Linus Torvalds,
  16. * Copyright 1991, 1992, 1993, and by Julian Cowley, Copyright 1994.
  17. *
  18. * This file may be redistributed under the terms of the GNU General Public
  19. * License.
  20. *
  21. * Reduced memory usage for older ARM systems - Russell King.
  22. *
  23. * 2000/01/20 Fixed SMP locking on put_tty_queue using bits of
  24. * the patch by Andrew J. Kroll <ag784@freenet.buffalo.edu>
  25. * who actually finally proved there really was a race.
  26. *
  27. * 2002/03/18 Implemented n_tty_wakeup to send SIGIO POLL_OUTs to
  28. * waiting writing processes-Sapan Bhatia <sapan@corewars.org>.
  29. * Also fixed a bug in BLOCKING mode where n_tty_write returns
  30. * EAGAIN
  31. */
  32. #include <linux/types.h>
  33. #include <linux/major.h>
  34. #include <linux/errno.h>
  35. #include <linux/signal.h>
  36. #include <linux/fcntl.h>
  37. #include <linux/sched.h>
  38. #include <linux/interrupt.h>
  39. #include <linux/tty.h>
  40. #include <linux/timer.h>
  41. #include <linux/ctype.h>
  42. #include <linux/mm.h>
  43. #include <linux/string.h>
  44. #include <linux/slab.h>
  45. #include <linux/poll.h>
  46. #include <linux/bitops.h>
  47. #include <linux/audit.h>
  48. #include <linux/file.h>
  49. #include <linux/uaccess.h>
  50. #include <linux/module.h>
  51. #include <asm/system.h>
  52. /* number of characters left in xmit buffer before select has we have room */
  53. #define WAKEUP_CHARS 256
  54. /*
  55. * This defines the low- and high-watermarks for throttling and
  56. * unthrottling the TTY driver. These watermarks are used for
  57. * controlling the space in the read buffer.
  58. */
  59. #define TTY_THRESHOLD_THROTTLE 128 /* now based on remaining room */
  60. #define TTY_THRESHOLD_UNTHROTTLE 128
  61. /*
  62. * Special byte codes used in the echo buffer to represent operations
  63. * or special handling of characters. Bytes in the echo buffer that
  64. * are not part of such special blocks are treated as normal character
  65. * codes.
  66. */
  67. #define ECHO_OP_START 0xff
  68. #define ECHO_OP_MOVE_BACK_COL 0x80
  69. #define ECHO_OP_SET_CANON_COL 0x81
  70. #define ECHO_OP_ERASE_TAB 0x82
  71. static inline int tty_put_user(struct tty_struct *tty, unsigned char x,
  72. unsigned char __user *ptr)
  73. {
  74. tty_audit_add_data(tty, &x, 1);
  75. return put_user(x, ptr);
  76. }
  77. /**
  78. * n_tty_set__room - receive space
  79. * @tty: terminal
  80. *
  81. * Called by the driver to find out how much data it is
  82. * permitted to feed to the line discipline without any being lost
  83. * and thus to manage flow control. Not serialized. Answers for the
  84. * "instant".
  85. */
  86. static void n_tty_set_room(struct tty_struct *tty)
  87. {
  88. /* tty->read_cnt is not read locked ? */
  89. int left = N_TTY_BUF_SIZE - tty->read_cnt - 1;
  90. /*
  91. * If we are doing input canonicalization, and there are no
  92. * pending newlines, let characters through without limit, so
  93. * that erase characters will be handled. Other excess
  94. * characters will be beeped.
  95. */
  96. if (left <= 0)
  97. left = tty->icanon && !tty->canon_data;
  98. tty->receive_room = left;
  99. }
  100. static void put_tty_queue_nolock(unsigned char c, struct tty_struct *tty)
  101. {
  102. if (tty->read_cnt < N_TTY_BUF_SIZE) {
  103. tty->read_buf[tty->read_head] = c;
  104. tty->read_head = (tty->read_head + 1) & (N_TTY_BUF_SIZE-1);
  105. tty->read_cnt++;
  106. }
  107. }
  108. /**
  109. * put_tty_queue - add character to tty
  110. * @c: character
  111. * @tty: tty device
  112. *
  113. * Add a character to the tty read_buf queue. This is done under the
  114. * read_lock to serialize character addition and also to protect us
  115. * against parallel reads or flushes
  116. */
  117. static void put_tty_queue(unsigned char c, struct tty_struct *tty)
  118. {
  119. unsigned long flags;
  120. /*
  121. * The problem of stomping on the buffers ends here.
  122. * Why didn't anyone see this one coming? --AJK
  123. */
  124. spin_lock_irqsave(&tty->read_lock, flags);
  125. put_tty_queue_nolock(c, tty);
  126. spin_unlock_irqrestore(&tty->read_lock, flags);
  127. }
  128. /**
  129. * check_unthrottle - allow new receive data
  130. * @tty; tty device
  131. *
  132. * Check whether to call the driver unthrottle functions
  133. *
  134. * Can sleep, may be called under the atomic_read_lock mutex but
  135. * this is not guaranteed.
  136. */
  137. static void check_unthrottle(struct tty_struct *tty)
  138. {
  139. if (tty->count)
  140. tty_unthrottle(tty);
  141. }
  142. /**
  143. * reset_buffer_flags - reset buffer state
  144. * @tty: terminal to reset
  145. *
  146. * Reset the read buffer counters, clear the flags,
  147. * and make sure the driver is unthrottled. Called
  148. * from n_tty_open() and n_tty_flush_buffer().
  149. *
  150. * Locking: tty_read_lock for read fields.
  151. */
  152. static void reset_buffer_flags(struct tty_struct *tty)
  153. {
  154. unsigned long flags;
  155. spin_lock_irqsave(&tty->read_lock, flags);
  156. tty->read_head = tty->read_tail = tty->read_cnt = 0;
  157. spin_unlock_irqrestore(&tty->read_lock, flags);
  158. mutex_lock(&tty->echo_lock);
  159. tty->echo_pos = tty->echo_cnt = tty->echo_overrun = 0;
  160. mutex_unlock(&tty->echo_lock);
  161. tty->canon_head = tty->canon_data = tty->erasing = 0;
  162. memset(&tty->read_flags, 0, sizeof tty->read_flags);
  163. n_tty_set_room(tty);
  164. check_unthrottle(tty);
  165. }
  166. /**
  167. * n_tty_flush_buffer - clean input queue
  168. * @tty: terminal device
  169. *
  170. * Flush the input buffer. Called when the line discipline is
  171. * being closed, when the tty layer wants the buffer flushed (eg
  172. * at hangup) or when the N_TTY line discipline internally has to
  173. * clean the pending queue (for example some signals).
  174. *
  175. * Locking: ctrl_lock, read_lock.
  176. */
  177. static void n_tty_flush_buffer(struct tty_struct *tty)
  178. {
  179. unsigned long flags;
  180. /* clear everything and unthrottle the driver */
  181. reset_buffer_flags(tty);
  182. if (!tty->link)
  183. return;
  184. spin_lock_irqsave(&tty->ctrl_lock, flags);
  185. if (tty->link->packet) {
  186. tty->ctrl_status |= TIOCPKT_FLUSHREAD;
  187. wake_up_interruptible(&tty->link->read_wait);
  188. }
  189. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  190. }
  191. /**
  192. * n_tty_chars_in_buffer - report available bytes
  193. * @tty: tty device
  194. *
  195. * Report the number of characters buffered to be delivered to user
  196. * at this instant in time.
  197. *
  198. * Locking: read_lock
  199. */
  200. static ssize_t n_tty_chars_in_buffer(struct tty_struct *tty)
  201. {
  202. unsigned long flags;
  203. ssize_t n = 0;
  204. spin_lock_irqsave(&tty->read_lock, flags);
  205. if (!tty->icanon) {
  206. n = tty->read_cnt;
  207. } else if (tty->canon_data) {
  208. n = (tty->canon_head > tty->read_tail) ?
  209. tty->canon_head - tty->read_tail :
  210. tty->canon_head + (N_TTY_BUF_SIZE - tty->read_tail);
  211. }
  212. spin_unlock_irqrestore(&tty->read_lock, flags);
  213. return n;
  214. }
  215. /**
  216. * is_utf8_continuation - utf8 multibyte check
  217. * @c: byte to check
  218. *
  219. * Returns true if the utf8 character 'c' is a multibyte continuation
  220. * character. We use this to correctly compute the on screen size
  221. * of the character when printing
  222. */
  223. static inline int is_utf8_continuation(unsigned char c)
  224. {
  225. return (c & 0xc0) == 0x80;
  226. }
  227. /**
  228. * is_continuation - multibyte check
  229. * @c: byte to check
  230. *
  231. * Returns true if the utf8 character 'c' is a multibyte continuation
  232. * character and the terminal is in unicode mode.
  233. */
  234. static inline int is_continuation(unsigned char c, struct tty_struct *tty)
  235. {
  236. return I_IUTF8(tty) && is_utf8_continuation(c);
  237. }
  238. /**
  239. * do_output_char - output one character
  240. * @c: character (or partial unicode symbol)
  241. * @tty: terminal device
  242. * @space: space available in tty driver write buffer
  243. *
  244. * This is a helper function that handles one output character
  245. * (including special characters like TAB, CR, LF, etc.),
  246. * doing OPOST processing and putting the results in the
  247. * tty driver's write buffer.
  248. *
  249. * Note that Linux currently ignores TABDLY, CRDLY, VTDLY, FFDLY
  250. * and NLDLY. They simply aren't relevant in the world today.
  251. * If you ever need them, add them here.
  252. *
  253. * Returns the number of bytes of buffer space used or -1 if
  254. * no space left.
  255. *
  256. * Locking: should be called under the output_lock to protect
  257. * the column state and space left in the buffer
  258. */
  259. static int do_output_char(unsigned char c, struct tty_struct *tty, int space)
  260. {
  261. int spaces;
  262. if (!space)
  263. return -1;
  264. switch (c) {
  265. case '\n':
  266. if (O_ONLRET(tty))
  267. tty->column = 0;
  268. if (O_ONLCR(tty)) {
  269. if (space < 2)
  270. return -1;
  271. tty->canon_column = tty->column = 0;
  272. tty->ops->write(tty, "\r\n", 2);
  273. return 2;
  274. }
  275. tty->canon_column = tty->column;
  276. break;
  277. case '\r':
  278. if (O_ONOCR(tty) && tty->column == 0)
  279. return 0;
  280. if (O_OCRNL(tty)) {
  281. c = '\n';
  282. if (O_ONLRET(tty))
  283. tty->canon_column = tty->column = 0;
  284. break;
  285. }
  286. tty->canon_column = tty->column = 0;
  287. break;
  288. case '\t':
  289. spaces = 8 - (tty->column & 7);
  290. if (O_TABDLY(tty) == XTABS) {
  291. if (space < spaces)
  292. return -1;
  293. tty->column += spaces;
  294. tty->ops->write(tty, " ", spaces);
  295. return spaces;
  296. }
  297. tty->column += spaces;
  298. break;
  299. case '\b':
  300. if (tty->column > 0)
  301. tty->column--;
  302. break;
  303. default:
  304. if (!iscntrl(c)) {
  305. if (O_OLCUC(tty))
  306. c = toupper(c);
  307. if (!is_continuation(c, tty))
  308. tty->column++;
  309. }
  310. break;
  311. }
  312. tty_put_char(tty, c);
  313. return 1;
  314. }
  315. /**
  316. * process_output - output post processor
  317. * @c: character (or partial unicode symbol)
  318. * @tty: terminal device
  319. *
  320. * Output one character with OPOST processing.
  321. * Returns -1 when the output device is full and the character
  322. * must be retried.
  323. *
  324. * Locking: output_lock to protect column state and space left
  325. * (also, this is called from n_tty_write under the
  326. * tty layer write lock)
  327. */
  328. static int process_output(unsigned char c, struct tty_struct *tty)
  329. {
  330. int space, retval;
  331. mutex_lock(&tty->output_lock);
  332. space = tty_write_room(tty);
  333. retval = do_output_char(c, tty, space);
  334. mutex_unlock(&tty->output_lock);
  335. if (retval < 0)
  336. return -1;
  337. else
  338. return 0;
  339. }
  340. /**
  341. * process_output_block - block post processor
  342. * @tty: terminal device
  343. * @buf: character buffer
  344. * @nr: number of bytes to output
  345. *
  346. * Output a block of characters with OPOST processing.
  347. * Returns the number of characters output.
  348. *
  349. * This path is used to speed up block console writes, among other
  350. * things when processing blocks of output data. It handles only
  351. * the simple cases normally found and helps to generate blocks of
  352. * symbols for the console driver and thus improve performance.
  353. *
  354. * Locking: output_lock to protect column state and space left
  355. * (also, this is called from n_tty_write under the
  356. * tty layer write lock)
  357. */
  358. static ssize_t process_output_block(struct tty_struct *tty,
  359. const unsigned char *buf, unsigned int nr)
  360. {
  361. int space;
  362. int i;
  363. const unsigned char *cp;
  364. mutex_lock(&tty->output_lock);
  365. space = tty_write_room(tty);
  366. if (!space) {
  367. mutex_unlock(&tty->output_lock);
  368. return 0;
  369. }
  370. if (nr > space)
  371. nr = space;
  372. for (i = 0, cp = buf; i < nr; i++, cp++) {
  373. unsigned char c = *cp;
  374. switch (c) {
  375. case '\n':
  376. if (O_ONLRET(tty))
  377. tty->column = 0;
  378. if (O_ONLCR(tty))
  379. goto break_out;
  380. tty->canon_column = tty->column;
  381. break;
  382. case '\r':
  383. if (O_ONOCR(tty) && tty->column == 0)
  384. goto break_out;
  385. if (O_OCRNL(tty))
  386. goto break_out;
  387. tty->canon_column = tty->column = 0;
  388. break;
  389. case '\t':
  390. goto break_out;
  391. case '\b':
  392. if (tty->column > 0)
  393. tty->column--;
  394. break;
  395. default:
  396. if (!iscntrl(c)) {
  397. if (O_OLCUC(tty))
  398. goto break_out;
  399. if (!is_continuation(c, tty))
  400. tty->column++;
  401. }
  402. break;
  403. }
  404. }
  405. break_out:
  406. i = tty->ops->write(tty, buf, i);
  407. mutex_unlock(&tty->output_lock);
  408. return i;
  409. }
  410. /**
  411. * process_echoes - write pending echo characters
  412. * @tty: terminal device
  413. *
  414. * Write previously buffered echo (and other ldisc-generated)
  415. * characters to the tty.
  416. *
  417. * Characters generated by the ldisc (including echoes) need to
  418. * be buffered because the driver's write buffer can fill during
  419. * heavy program output. Echoing straight to the driver will
  420. * often fail under these conditions, causing lost characters and
  421. * resulting mismatches of ldisc state information.
  422. *
  423. * Since the ldisc state must represent the characters actually sent
  424. * to the driver at the time of the write, operations like certain
  425. * changes in column state are also saved in the buffer and executed
  426. * here.
  427. *
  428. * A circular fifo buffer is used so that the most recent characters
  429. * are prioritized. Also, when control characters are echoed with a
  430. * prefixed "^", the pair is treated atomically and thus not separated.
  431. *
  432. * Locking: output_lock to protect column state and space left,
  433. * echo_lock to protect the echo buffer
  434. */
  435. static void process_echoes(struct tty_struct *tty)
  436. {
  437. int space, nr;
  438. unsigned char c;
  439. unsigned char *cp, *buf_end;
  440. if (!tty->echo_cnt)
  441. return;
  442. mutex_lock(&tty->output_lock);
  443. mutex_lock(&tty->echo_lock);
  444. space = tty_write_room(tty);
  445. buf_end = tty->echo_buf + N_TTY_BUF_SIZE;
  446. cp = tty->echo_buf + tty->echo_pos;
  447. nr = tty->echo_cnt;
  448. while (nr > 0) {
  449. c = *cp;
  450. if (c == ECHO_OP_START) {
  451. unsigned char op;
  452. unsigned char *opp;
  453. int no_space_left = 0;
  454. /*
  455. * If the buffer byte is the start of a multi-byte
  456. * operation, get the next byte, which is either the
  457. * op code or a control character value.
  458. */
  459. opp = cp + 1;
  460. if (opp == buf_end)
  461. opp -= N_TTY_BUF_SIZE;
  462. op = *opp;
  463. switch (op) {
  464. unsigned int num_chars, num_bs;
  465. case ECHO_OP_ERASE_TAB:
  466. if (++opp == buf_end)
  467. opp -= N_TTY_BUF_SIZE;
  468. num_chars = *opp;
  469. /*
  470. * Determine how many columns to go back
  471. * in order to erase the tab.
  472. * This depends on the number of columns
  473. * used by other characters within the tab
  474. * area. If this (modulo 8) count is from
  475. * the start of input rather than from a
  476. * previous tab, we offset by canon column.
  477. * Otherwise, tab spacing is normal.
  478. */
  479. if (!(num_chars & 0x80))
  480. num_chars += tty->canon_column;
  481. num_bs = 8 - (num_chars & 7);
  482. if (num_bs > space) {
  483. no_space_left = 1;
  484. break;
  485. }
  486. space -= num_bs;
  487. while (num_bs--) {
  488. tty_put_char(tty, '\b');
  489. if (tty->column > 0)
  490. tty->column--;
  491. }
  492. cp += 3;
  493. nr -= 3;
  494. break;
  495. case ECHO_OP_SET_CANON_COL:
  496. tty->canon_column = tty->column;
  497. cp += 2;
  498. nr -= 2;
  499. break;
  500. case ECHO_OP_MOVE_BACK_COL:
  501. if (tty->column > 0)
  502. tty->column--;
  503. cp += 2;
  504. nr -= 2;
  505. break;
  506. case ECHO_OP_START:
  507. /* This is an escaped echo op start code */
  508. if (!space) {
  509. no_space_left = 1;
  510. break;
  511. }
  512. tty_put_char(tty, ECHO_OP_START);
  513. tty->column++;
  514. space--;
  515. cp += 2;
  516. nr -= 2;
  517. break;
  518. default:
  519. /*
  520. * If the op is not a special byte code,
  521. * it is a ctrl char tagged to be echoed
  522. * as "^X" (where X is the letter
  523. * representing the control char).
  524. * Note that we must ensure there is
  525. * enough space for the whole ctrl pair.
  526. *
  527. */
  528. if (space < 2) {
  529. no_space_left = 1;
  530. break;
  531. }
  532. tty_put_char(tty, '^');
  533. tty_put_char(tty, op ^ 0100);
  534. tty->column += 2;
  535. space -= 2;
  536. cp += 2;
  537. nr -= 2;
  538. }
  539. if (no_space_left)
  540. break;
  541. } else {
  542. if (O_OPOST(tty) &&
  543. !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
  544. int retval = do_output_char(c, tty, space);
  545. if (retval < 0)
  546. break;
  547. space -= retval;
  548. } else {
  549. if (!space)
  550. break;
  551. tty_put_char(tty, c);
  552. space -= 1;
  553. }
  554. cp += 1;
  555. nr -= 1;
  556. }
  557. /* When end of circular buffer reached, wrap around */
  558. if (cp >= buf_end)
  559. cp -= N_TTY_BUF_SIZE;
  560. }
  561. if (nr == 0) {
  562. tty->echo_pos = 0;
  563. tty->echo_cnt = 0;
  564. tty->echo_overrun = 0;
  565. } else {
  566. int num_processed = tty->echo_cnt - nr;
  567. tty->echo_pos += num_processed;
  568. tty->echo_pos &= N_TTY_BUF_SIZE - 1;
  569. tty->echo_cnt = nr;
  570. if (num_processed > 0)
  571. tty->echo_overrun = 0;
  572. }
  573. mutex_unlock(&tty->echo_lock);
  574. mutex_unlock(&tty->output_lock);
  575. if (tty->ops->flush_chars)
  576. tty->ops->flush_chars(tty);
  577. }
  578. /**
  579. * add_echo_byte - add a byte to the echo buffer
  580. * @c: unicode byte to echo
  581. * @tty: terminal device
  582. *
  583. * Add a character or operation byte to the echo buffer.
  584. *
  585. * Should be called under the echo lock to protect the echo buffer.
  586. */
  587. static void add_echo_byte(unsigned char c, struct tty_struct *tty)
  588. {
  589. int new_byte_pos;
  590. if (tty->echo_cnt == N_TTY_BUF_SIZE) {
  591. /* Circular buffer is already at capacity */
  592. new_byte_pos = tty->echo_pos;
  593. /*
  594. * Since the buffer start position needs to be advanced,
  595. * be sure to step by a whole operation byte group.
  596. */
  597. if (tty->echo_buf[tty->echo_pos] == ECHO_OP_START) {
  598. if (tty->echo_buf[(tty->echo_pos + 1) &
  599. (N_TTY_BUF_SIZE - 1)] ==
  600. ECHO_OP_ERASE_TAB) {
  601. tty->echo_pos += 3;
  602. tty->echo_cnt -= 2;
  603. } else {
  604. tty->echo_pos += 2;
  605. tty->echo_cnt -= 1;
  606. }
  607. } else {
  608. tty->echo_pos++;
  609. }
  610. tty->echo_pos &= N_TTY_BUF_SIZE - 1;
  611. tty->echo_overrun = 1;
  612. } else {
  613. new_byte_pos = tty->echo_pos + tty->echo_cnt;
  614. new_byte_pos &= N_TTY_BUF_SIZE - 1;
  615. tty->echo_cnt++;
  616. }
  617. tty->echo_buf[new_byte_pos] = c;
  618. }
  619. /**
  620. * echo_move_back_col - add operation to move back a column
  621. * @tty: terminal device
  622. *
  623. * Add an operation to the echo buffer to move back one column.
  624. *
  625. * Locking: echo_lock to protect the echo buffer
  626. */
  627. static void echo_move_back_col(struct tty_struct *tty)
  628. {
  629. mutex_lock(&tty->echo_lock);
  630. add_echo_byte(ECHO_OP_START, tty);
  631. add_echo_byte(ECHO_OP_MOVE_BACK_COL, tty);
  632. mutex_unlock(&tty->echo_lock);
  633. }
  634. /**
  635. * echo_set_canon_col - add operation to set the canon column
  636. * @tty: terminal device
  637. *
  638. * Add an operation to the echo buffer to set the canon column
  639. * to the current column.
  640. *
  641. * Locking: echo_lock to protect the echo buffer
  642. */
  643. static void echo_set_canon_col(struct tty_struct *tty)
  644. {
  645. mutex_lock(&tty->echo_lock);
  646. add_echo_byte(ECHO_OP_START, tty);
  647. add_echo_byte(ECHO_OP_SET_CANON_COL, tty);
  648. mutex_unlock(&tty->echo_lock);
  649. }
  650. /**
  651. * echo_erase_tab - add operation to erase a tab
  652. * @num_chars: number of character columns already used
  653. * @after_tab: true if num_chars starts after a previous tab
  654. * @tty: terminal device
  655. *
  656. * Add an operation to the echo buffer to erase a tab.
  657. *
  658. * Called by the eraser function, which knows how many character
  659. * columns have been used since either a previous tab or the start
  660. * of input. This information will be used later, along with
  661. * canon column (if applicable), to go back the correct number
  662. * of columns.
  663. *
  664. * Locking: echo_lock to protect the echo buffer
  665. */
  666. static void echo_erase_tab(unsigned int num_chars, int after_tab,
  667. struct tty_struct *tty)
  668. {
  669. mutex_lock(&tty->echo_lock);
  670. add_echo_byte(ECHO_OP_START, tty);
  671. add_echo_byte(ECHO_OP_ERASE_TAB, tty);
  672. /* We only need to know this modulo 8 (tab spacing) */
  673. num_chars &= 7;
  674. /* Set the high bit as a flag if num_chars is after a previous tab */
  675. if (after_tab)
  676. num_chars |= 0x80;
  677. add_echo_byte(num_chars, tty);
  678. mutex_unlock(&tty->echo_lock);
  679. }
  680. /**
  681. * echo_char_raw - echo a character raw
  682. * @c: unicode byte to echo
  683. * @tty: terminal device
  684. *
  685. * Echo user input back onto the screen. This must be called only when
  686. * L_ECHO(tty) is true. Called from the driver receive_buf path.
  687. *
  688. * This variant does not treat control characters specially.
  689. *
  690. * Locking: echo_lock to protect the echo buffer
  691. */
  692. static void echo_char_raw(unsigned char c, struct tty_struct *tty)
  693. {
  694. mutex_lock(&tty->echo_lock);
  695. if (c == ECHO_OP_START) {
  696. add_echo_byte(ECHO_OP_START, tty);
  697. add_echo_byte(ECHO_OP_START, tty);
  698. } else {
  699. add_echo_byte(c, tty);
  700. }
  701. mutex_unlock(&tty->echo_lock);
  702. }
  703. /**
  704. * echo_char - echo a character
  705. * @c: unicode byte to echo
  706. * @tty: terminal device
  707. *
  708. * Echo user input back onto the screen. This must be called only when
  709. * L_ECHO(tty) is true. Called from the driver receive_buf path.
  710. *
  711. * This variant tags control characters to be echoed as "^X"
  712. * (where X is the letter representing the control char).
  713. *
  714. * Locking: echo_lock to protect the echo buffer
  715. */
  716. static void echo_char(unsigned char c, struct tty_struct *tty)
  717. {
  718. mutex_lock(&tty->echo_lock);
  719. if (c == ECHO_OP_START) {
  720. add_echo_byte(ECHO_OP_START, tty);
  721. add_echo_byte(ECHO_OP_START, tty);
  722. } else {
  723. if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t')
  724. add_echo_byte(ECHO_OP_START, tty);
  725. add_echo_byte(c, tty);
  726. }
  727. mutex_unlock(&tty->echo_lock);
  728. }
  729. /**
  730. * finish_erasing - complete erase
  731. * @tty: tty doing the erase
  732. */
  733. static inline void finish_erasing(struct tty_struct *tty)
  734. {
  735. if (tty->erasing) {
  736. echo_char_raw('/', tty);
  737. tty->erasing = 0;
  738. }
  739. }
  740. /**
  741. * eraser - handle erase function
  742. * @c: character input
  743. * @tty: terminal device
  744. *
  745. * Perform erase and necessary output when an erase character is
  746. * present in the stream from the driver layer. Handles the complexities
  747. * of UTF-8 multibyte symbols.
  748. *
  749. * Locking: read_lock for tty buffers
  750. */
  751. static void eraser(unsigned char c, struct tty_struct *tty)
  752. {
  753. enum { ERASE, WERASE, KILL } kill_type;
  754. int head, seen_alnums, cnt;
  755. unsigned long flags;
  756. /* FIXME: locking needed ? */
  757. if (tty->read_head == tty->canon_head) {
  758. /* process_output('\a', tty); */ /* what do you think? */
  759. return;
  760. }
  761. if (c == ERASE_CHAR(tty))
  762. kill_type = ERASE;
  763. else if (c == WERASE_CHAR(tty))
  764. kill_type = WERASE;
  765. else {
  766. if (!L_ECHO(tty)) {
  767. spin_lock_irqsave(&tty->read_lock, flags);
  768. tty->read_cnt -= ((tty->read_head - tty->canon_head) &
  769. (N_TTY_BUF_SIZE - 1));
  770. tty->read_head = tty->canon_head;
  771. spin_unlock_irqrestore(&tty->read_lock, flags);
  772. return;
  773. }
  774. if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(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. finish_erasing(tty);
  781. echo_char(KILL_CHAR(tty), tty);
  782. /* Add a newline if ECHOK is on and ECHOKE is off. */
  783. if (L_ECHOK(tty))
  784. echo_char_raw('\n', tty);
  785. return;
  786. }
  787. kill_type = KILL;
  788. }
  789. seen_alnums = 0;
  790. /* FIXME: Locking ?? */
  791. while (tty->read_head != tty->canon_head) {
  792. head = tty->read_head;
  793. /* erase a single possibly multibyte character */
  794. do {
  795. head = (head - 1) & (N_TTY_BUF_SIZE-1);
  796. c = tty->read_buf[head];
  797. } while (is_continuation(c, tty) && head != tty->canon_head);
  798. /* do not partially erase */
  799. if (is_continuation(c, tty))
  800. break;
  801. if (kill_type == WERASE) {
  802. /* Equivalent to BSD's ALTWERASE. */
  803. if (isalnum(c) || c == '_')
  804. seen_alnums++;
  805. else if (seen_alnums)
  806. break;
  807. }
  808. cnt = (tty->read_head - head) & (N_TTY_BUF_SIZE-1);
  809. spin_lock_irqsave(&tty->read_lock, flags);
  810. tty->read_head = head;
  811. tty->read_cnt -= cnt;
  812. spin_unlock_irqrestore(&tty->read_lock, flags);
  813. if (L_ECHO(tty)) {
  814. if (L_ECHOPRT(tty)) {
  815. if (!tty->erasing) {
  816. echo_char_raw('\\', tty);
  817. tty->erasing = 1;
  818. }
  819. /* if cnt > 1, output a multi-byte character */
  820. echo_char(c, tty);
  821. while (--cnt > 0) {
  822. head = (head+1) & (N_TTY_BUF_SIZE-1);
  823. echo_char_raw(tty->read_buf[head], tty);
  824. echo_move_back_col(tty);
  825. }
  826. } else if (kill_type == ERASE && !L_ECHOE(tty)) {
  827. echo_char(ERASE_CHAR(tty), tty);
  828. } else if (c == '\t') {
  829. unsigned int num_chars = 0;
  830. int after_tab = 0;
  831. unsigned long tail = tty->read_head;
  832. /*
  833. * Count the columns used for characters
  834. * since the start of input or after a
  835. * previous tab.
  836. * This info is used to go back the correct
  837. * number of columns.
  838. */
  839. while (tail != tty->canon_head) {
  840. tail = (tail-1) & (N_TTY_BUF_SIZE-1);
  841. c = tty->read_buf[tail];
  842. if (c == '\t') {
  843. after_tab = 1;
  844. break;
  845. } else if (iscntrl(c)) {
  846. if (L_ECHOCTL(tty))
  847. num_chars += 2;
  848. } else if (!is_continuation(c, tty)) {
  849. num_chars++;
  850. }
  851. }
  852. echo_erase_tab(num_chars, after_tab, tty);
  853. } else {
  854. if (iscntrl(c) && L_ECHOCTL(tty)) {
  855. echo_char_raw('\b', tty);
  856. echo_char_raw(' ', tty);
  857. echo_char_raw('\b', tty);
  858. }
  859. if (!iscntrl(c) || L_ECHOCTL(tty)) {
  860. echo_char_raw('\b', tty);
  861. echo_char_raw(' ', tty);
  862. echo_char_raw('\b', tty);
  863. }
  864. }
  865. }
  866. if (kill_type == ERASE)
  867. break;
  868. }
  869. if (tty->read_head == tty->canon_head && L_ECHO(tty))
  870. finish_erasing(tty);
  871. }
  872. /**
  873. * isig - handle the ISIG optio
  874. * @sig: signal
  875. * @tty: terminal
  876. * @flush: force flush
  877. *
  878. * Called when a signal is being sent due to terminal input. This
  879. * may caus terminal flushing to take place according to the termios
  880. * settings and character used. Called from the driver receive_buf
  881. * path so serialized.
  882. *
  883. * Locking: ctrl_lock, read_lock (both via flush buffer)
  884. */
  885. static inline void isig(int sig, struct tty_struct *tty, int flush)
  886. {
  887. if (tty->pgrp)
  888. kill_pgrp(tty->pgrp, sig, 1);
  889. if (flush || !L_NOFLSH(tty)) {
  890. n_tty_flush_buffer(tty);
  891. tty_driver_flush_buffer(tty);
  892. }
  893. }
  894. /**
  895. * n_tty_receive_break - handle break
  896. * @tty: terminal
  897. *
  898. * An RS232 break event has been hit in the incoming bitstream. This
  899. * can cause a variety of events depending upon the termios settings.
  900. *
  901. * Called from the receive_buf path so single threaded.
  902. */
  903. static inline void n_tty_receive_break(struct tty_struct *tty)
  904. {
  905. if (I_IGNBRK(tty))
  906. return;
  907. if (I_BRKINT(tty)) {
  908. isig(SIGINT, tty, 1);
  909. return;
  910. }
  911. if (I_PARMRK(tty)) {
  912. put_tty_queue('\377', tty);
  913. put_tty_queue('\0', tty);
  914. }
  915. put_tty_queue('\0', tty);
  916. wake_up_interruptible(&tty->read_wait);
  917. }
  918. /**
  919. * n_tty_receive_overrun - handle overrun reporting
  920. * @tty: terminal
  921. *
  922. * Data arrived faster than we could process it. While the tty
  923. * driver has flagged this the bits that were missed are gone
  924. * forever.
  925. *
  926. * Called from the receive_buf path so single threaded. Does not
  927. * need locking as num_overrun and overrun_time are function
  928. * private.
  929. */
  930. static inline void n_tty_receive_overrun(struct tty_struct *tty)
  931. {
  932. char buf[64];
  933. tty->num_overrun++;
  934. if (time_before(tty->overrun_time, jiffies - HZ) ||
  935. time_after(tty->overrun_time, jiffies)) {
  936. printk(KERN_WARNING "%s: %d input overrun(s)\n",
  937. tty_name(tty, buf),
  938. tty->num_overrun);
  939. tty->overrun_time = jiffies;
  940. tty->num_overrun = 0;
  941. }
  942. }
  943. /**
  944. * n_tty_receive_parity_error - error notifier
  945. * @tty: terminal device
  946. * @c: character
  947. *
  948. * Process a parity error and queue the right data to indicate
  949. * the error case if necessary. Locking as per n_tty_receive_buf.
  950. */
  951. static inline void n_tty_receive_parity_error(struct tty_struct *tty,
  952. unsigned char c)
  953. {
  954. if (I_IGNPAR(tty))
  955. return;
  956. if (I_PARMRK(tty)) {
  957. put_tty_queue('\377', tty);
  958. put_tty_queue('\0', tty);
  959. put_tty_queue(c, tty);
  960. } else if (I_INPCK(tty))
  961. put_tty_queue('\0', tty);
  962. else
  963. put_tty_queue(c, tty);
  964. wake_up_interruptible(&tty->read_wait);
  965. }
  966. /**
  967. * n_tty_receive_char - perform processing
  968. * @tty: terminal device
  969. * @c: character
  970. *
  971. * Process an individual character of input received from the driver.
  972. * This is serialized with respect to itself by the rules for the
  973. * driver above.
  974. */
  975. static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
  976. {
  977. unsigned long flags;
  978. int parmrk;
  979. if (tty->raw) {
  980. put_tty_queue(c, tty);
  981. return;
  982. }
  983. if (I_ISTRIP(tty))
  984. c &= 0x7f;
  985. if (I_IUCLC(tty) && L_IEXTEN(tty))
  986. c = tolower(c);
  987. if (tty->stopped && !tty->flow_stopped && I_IXON(tty) &&
  988. I_IXANY(tty) && c != START_CHAR(tty) && c != STOP_CHAR(tty) &&
  989. c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) && c != SUSP_CHAR(tty)) {
  990. start_tty(tty);
  991. process_echoes(tty);
  992. }
  993. if (tty->closing) {
  994. if (I_IXON(tty)) {
  995. if (c == START_CHAR(tty)) {
  996. start_tty(tty);
  997. process_echoes(tty);
  998. } else if (c == STOP_CHAR(tty))
  999. stop_tty(tty);
  1000. }
  1001. return;
  1002. }
  1003. /*
  1004. * If the previous character was LNEXT, or we know that this
  1005. * character is not one of the characters that we'll have to
  1006. * handle specially, do shortcut processing to speed things
  1007. * up.
  1008. */
  1009. if (!test_bit(c, tty->process_char_map) || tty->lnext) {
  1010. tty->lnext = 0;
  1011. parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
  1012. if (tty->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
  1013. /* beep if no space */
  1014. if (L_ECHO(tty))
  1015. process_output('\a', tty);
  1016. return;
  1017. }
  1018. if (L_ECHO(tty)) {
  1019. finish_erasing(tty);
  1020. /* Record the column of first canon char. */
  1021. if (tty->canon_head == tty->read_head)
  1022. echo_set_canon_col(tty);
  1023. echo_char(c, tty);
  1024. process_echoes(tty);
  1025. }
  1026. if (parmrk)
  1027. put_tty_queue(c, tty);
  1028. put_tty_queue(c, tty);
  1029. return;
  1030. }
  1031. if (I_IXON(tty)) {
  1032. if (c == START_CHAR(tty)) {
  1033. start_tty(tty);
  1034. process_echoes(tty);
  1035. return;
  1036. }
  1037. if (c == STOP_CHAR(tty)) {
  1038. stop_tty(tty);
  1039. return;
  1040. }
  1041. }
  1042. if (L_ISIG(tty)) {
  1043. int signal;
  1044. signal = SIGINT;
  1045. if (c == INTR_CHAR(tty))
  1046. goto send_signal;
  1047. signal = SIGQUIT;
  1048. if (c == QUIT_CHAR(tty))
  1049. goto send_signal;
  1050. signal = SIGTSTP;
  1051. if (c == SUSP_CHAR(tty)) {
  1052. send_signal:
  1053. /*
  1054. * Note that we do not use isig() here because we want
  1055. * the order to be:
  1056. * 1) flush, 2) echo, 3) signal
  1057. */
  1058. if (!L_NOFLSH(tty)) {
  1059. n_tty_flush_buffer(tty);
  1060. tty_driver_flush_buffer(tty);
  1061. }
  1062. if (I_IXON(tty))
  1063. start_tty(tty);
  1064. if (L_ECHO(tty)) {
  1065. echo_char(c, tty);
  1066. process_echoes(tty);
  1067. }
  1068. if (tty->pgrp)
  1069. kill_pgrp(tty->pgrp, signal, 1);
  1070. return;
  1071. }
  1072. }
  1073. if (c == '\r') {
  1074. if (I_IGNCR(tty))
  1075. return;
  1076. if (I_ICRNL(tty))
  1077. c = '\n';
  1078. } else if (c == '\n' && I_INLCR(tty))
  1079. c = '\r';
  1080. if (tty->icanon) {
  1081. if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
  1082. (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
  1083. eraser(c, tty);
  1084. process_echoes(tty);
  1085. return;
  1086. }
  1087. if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
  1088. tty->lnext = 1;
  1089. if (L_ECHO(tty)) {
  1090. finish_erasing(tty);
  1091. if (L_ECHOCTL(tty)) {
  1092. echo_char_raw('^', tty);
  1093. echo_char_raw('\b', tty);
  1094. process_echoes(tty);
  1095. }
  1096. }
  1097. return;
  1098. }
  1099. if (c == REPRINT_CHAR(tty) && L_ECHO(tty) &&
  1100. L_IEXTEN(tty)) {
  1101. unsigned long tail = tty->canon_head;
  1102. finish_erasing(tty);
  1103. echo_char(c, tty);
  1104. echo_char_raw('\n', tty);
  1105. while (tail != tty->read_head) {
  1106. echo_char(tty->read_buf[tail], tty);
  1107. tail = (tail+1) & (N_TTY_BUF_SIZE-1);
  1108. }
  1109. process_echoes(tty);
  1110. return;
  1111. }
  1112. if (c == '\n') {
  1113. if (tty->read_cnt >= N_TTY_BUF_SIZE) {
  1114. if (L_ECHO(tty))
  1115. process_output('\a', tty);
  1116. return;
  1117. }
  1118. if (L_ECHO(tty) || L_ECHONL(tty)) {
  1119. echo_char_raw('\n', tty);
  1120. process_echoes(tty);
  1121. }
  1122. goto handle_newline;
  1123. }
  1124. if (c == EOF_CHAR(tty)) {
  1125. if (tty->read_cnt >= N_TTY_BUF_SIZE)
  1126. return;
  1127. if (tty->canon_head != tty->read_head)
  1128. set_bit(TTY_PUSH, &tty->flags);
  1129. c = __DISABLED_CHAR;
  1130. goto handle_newline;
  1131. }
  1132. if ((c == EOL_CHAR(tty)) ||
  1133. (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
  1134. parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty))
  1135. ? 1 : 0;
  1136. if (tty->read_cnt >= (N_TTY_BUF_SIZE - parmrk)) {
  1137. if (L_ECHO(tty))
  1138. process_output('\a', tty);
  1139. return;
  1140. }
  1141. /*
  1142. * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
  1143. */
  1144. if (L_ECHO(tty)) {
  1145. /* Record the column of first canon char. */
  1146. if (tty->canon_head == tty->read_head)
  1147. echo_set_canon_col(tty);
  1148. echo_char(c, tty);
  1149. process_echoes(tty);
  1150. }
  1151. /*
  1152. * XXX does PARMRK doubling happen for
  1153. * EOL_CHAR and EOL2_CHAR?
  1154. */
  1155. if (parmrk)
  1156. put_tty_queue(c, tty);
  1157. handle_newline:
  1158. spin_lock_irqsave(&tty->read_lock, flags);
  1159. set_bit(tty->read_head, tty->read_flags);
  1160. put_tty_queue_nolock(c, tty);
  1161. tty->canon_head = tty->read_head;
  1162. tty->canon_data++;
  1163. spin_unlock_irqrestore(&tty->read_lock, flags);
  1164. kill_fasync(&tty->fasync, SIGIO, POLL_IN);
  1165. if (waitqueue_active(&tty->read_wait))
  1166. wake_up_interruptible(&tty->read_wait);
  1167. return;
  1168. }
  1169. }
  1170. parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
  1171. if (tty->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
  1172. /* beep if no space */
  1173. if (L_ECHO(tty))
  1174. process_output('\a', tty);
  1175. return;
  1176. }
  1177. if (L_ECHO(tty)) {
  1178. finish_erasing(tty);
  1179. if (c == '\n')
  1180. echo_char_raw('\n', tty);
  1181. else {
  1182. /* Record the column of first canon char. */
  1183. if (tty->canon_head == tty->read_head)
  1184. echo_set_canon_col(tty);
  1185. echo_char(c, tty);
  1186. }
  1187. process_echoes(tty);
  1188. }
  1189. if (parmrk)
  1190. put_tty_queue(c, tty);
  1191. put_tty_queue(c, tty);
  1192. }
  1193. /**
  1194. * n_tty_write_wakeup - asynchronous I/O notifier
  1195. * @tty: tty device
  1196. *
  1197. * Required for the ptys, serial driver etc. since processes
  1198. * that attach themselves to the master and rely on ASYNC
  1199. * IO must be woken up
  1200. */
  1201. static void n_tty_write_wakeup(struct tty_struct *tty)
  1202. {
  1203. if (tty->fasync && test_and_clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags))
  1204. kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
  1205. }
  1206. /**
  1207. * n_tty_receive_buf - data receive
  1208. * @tty: terminal device
  1209. * @cp: buffer
  1210. * @fp: flag buffer
  1211. * @count: characters
  1212. *
  1213. * Called by the terminal driver when a block of characters has
  1214. * been received. This function must be called from soft contexts
  1215. * not from interrupt context. The driver is responsible for making
  1216. * calls one at a time and in order (or using flush_to_ldisc)
  1217. */
  1218. static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
  1219. char *fp, int count)
  1220. {
  1221. const unsigned char *p;
  1222. char *f, flags = TTY_NORMAL;
  1223. int i;
  1224. char buf[64];
  1225. unsigned long cpuflags;
  1226. if (!tty->read_buf)
  1227. return;
  1228. if (tty->real_raw) {
  1229. spin_lock_irqsave(&tty->read_lock, cpuflags);
  1230. i = min(N_TTY_BUF_SIZE - tty->read_cnt,
  1231. N_TTY_BUF_SIZE - tty->read_head);
  1232. i = min(count, i);
  1233. memcpy(tty->read_buf + tty->read_head, cp, i);
  1234. tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
  1235. tty->read_cnt += i;
  1236. cp += i;
  1237. count -= i;
  1238. i = min(N_TTY_BUF_SIZE - tty->read_cnt,
  1239. N_TTY_BUF_SIZE - tty->read_head);
  1240. i = min(count, i);
  1241. memcpy(tty->read_buf + tty->read_head, cp, i);
  1242. tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
  1243. tty->read_cnt += i;
  1244. spin_unlock_irqrestore(&tty->read_lock, cpuflags);
  1245. } else {
  1246. for (i = count, p = cp, f = fp; i; i--, p++) {
  1247. if (f)
  1248. flags = *f++;
  1249. switch (flags) {
  1250. case TTY_NORMAL:
  1251. n_tty_receive_char(tty, *p);
  1252. break;
  1253. case TTY_BREAK:
  1254. n_tty_receive_break(tty);
  1255. break;
  1256. case TTY_PARITY:
  1257. case TTY_FRAME:
  1258. n_tty_receive_parity_error(tty, *p);
  1259. break;
  1260. case TTY_OVERRUN:
  1261. n_tty_receive_overrun(tty);
  1262. break;
  1263. default:
  1264. printk(KERN_ERR "%s: unknown flag %d\n",
  1265. tty_name(tty, buf), flags);
  1266. break;
  1267. }
  1268. }
  1269. if (tty->ops->flush_chars)
  1270. tty->ops->flush_chars(tty);
  1271. }
  1272. n_tty_set_room(tty);
  1273. if (!tty->icanon && (tty->read_cnt >= tty->minimum_to_wake)) {
  1274. kill_fasync(&tty->fasync, SIGIO, POLL_IN);
  1275. if (waitqueue_active(&tty->read_wait))
  1276. wake_up_interruptible(&tty->read_wait);
  1277. }
  1278. /*
  1279. * Check the remaining room for the input canonicalization
  1280. * mode. We don't want to throttle the driver if we're in
  1281. * canonical mode and don't have a newline yet!
  1282. */
  1283. if (tty->receive_room < TTY_THRESHOLD_THROTTLE)
  1284. tty_throttle(tty);
  1285. }
  1286. int is_ignored(int sig)
  1287. {
  1288. return (sigismember(&current->blocked, sig) ||
  1289. current->sighand->action[sig-1].sa.sa_handler == SIG_IGN);
  1290. }
  1291. /**
  1292. * n_tty_set_termios - termios data changed
  1293. * @tty: terminal
  1294. * @old: previous data
  1295. *
  1296. * Called by the tty layer when the user changes termios flags so
  1297. * that the line discipline can plan ahead. This function cannot sleep
  1298. * and is protected from re-entry by the tty layer. The user is
  1299. * guaranteed that this function will not be re-entered or in progress
  1300. * when the ldisc is closed.
  1301. *
  1302. * Locking: Caller holds tty->termios_mutex
  1303. */
  1304. static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
  1305. {
  1306. int canon_change = 1;
  1307. BUG_ON(!tty);
  1308. if (old)
  1309. canon_change = (old->c_lflag ^ tty->termios->c_lflag) & ICANON;
  1310. if (canon_change) {
  1311. memset(&tty->read_flags, 0, sizeof tty->read_flags);
  1312. tty->canon_head = tty->read_tail;
  1313. tty->canon_data = 0;
  1314. tty->erasing = 0;
  1315. }
  1316. if (canon_change && !L_ICANON(tty) && tty->read_cnt)
  1317. wake_up_interruptible(&tty->read_wait);
  1318. tty->icanon = (L_ICANON(tty) != 0);
  1319. if (test_bit(TTY_HW_COOK_IN, &tty->flags)) {
  1320. tty->raw = 1;
  1321. tty->real_raw = 1;
  1322. n_tty_set_room(tty);
  1323. return;
  1324. }
  1325. if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
  1326. I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
  1327. I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
  1328. I_PARMRK(tty)) {
  1329. memset(tty->process_char_map, 0, 256/8);
  1330. if (I_IGNCR(tty) || I_ICRNL(tty))
  1331. set_bit('\r', tty->process_char_map);
  1332. if (I_INLCR(tty))
  1333. set_bit('\n', tty->process_char_map);
  1334. if (L_ICANON(tty)) {
  1335. set_bit(ERASE_CHAR(tty), tty->process_char_map);
  1336. set_bit(KILL_CHAR(tty), tty->process_char_map);
  1337. set_bit(EOF_CHAR(tty), tty->process_char_map);
  1338. set_bit('\n', tty->process_char_map);
  1339. set_bit(EOL_CHAR(tty), tty->process_char_map);
  1340. if (L_IEXTEN(tty)) {
  1341. set_bit(WERASE_CHAR(tty),
  1342. tty->process_char_map);
  1343. set_bit(LNEXT_CHAR(tty),
  1344. tty->process_char_map);
  1345. set_bit(EOL2_CHAR(tty),
  1346. tty->process_char_map);
  1347. if (L_ECHO(tty))
  1348. set_bit(REPRINT_CHAR(tty),
  1349. tty->process_char_map);
  1350. }
  1351. }
  1352. if (I_IXON(tty)) {
  1353. set_bit(START_CHAR(tty), tty->process_char_map);
  1354. set_bit(STOP_CHAR(tty), tty->process_char_map);
  1355. }
  1356. if (L_ISIG(tty)) {
  1357. set_bit(INTR_CHAR(tty), tty->process_char_map);
  1358. set_bit(QUIT_CHAR(tty), tty->process_char_map);
  1359. set_bit(SUSP_CHAR(tty), tty->process_char_map);
  1360. }
  1361. clear_bit(__DISABLED_CHAR, tty->process_char_map);
  1362. tty->raw = 0;
  1363. tty->real_raw = 0;
  1364. } else {
  1365. tty->raw = 1;
  1366. if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
  1367. (I_IGNPAR(tty) || !I_INPCK(tty)) &&
  1368. (tty->driver->flags & TTY_DRIVER_REAL_RAW))
  1369. tty->real_raw = 1;
  1370. else
  1371. tty->real_raw = 0;
  1372. }
  1373. n_tty_set_room(tty);
  1374. /* The termios change make the tty ready for I/O */
  1375. wake_up_interruptible(&tty->write_wait);
  1376. wake_up_interruptible(&tty->read_wait);
  1377. }
  1378. /**
  1379. * n_tty_close - close the ldisc for this tty
  1380. * @tty: device
  1381. *
  1382. * Called from the terminal layer when this line discipline is
  1383. * being shut down, either because of a close or becsuse of a
  1384. * discipline change. The function will not be called while other
  1385. * ldisc methods are in progress.
  1386. */
  1387. static void n_tty_close(struct tty_struct *tty)
  1388. {
  1389. n_tty_flush_buffer(tty);
  1390. if (tty->read_buf) {
  1391. kfree(tty->read_buf);
  1392. tty->read_buf = NULL;
  1393. }
  1394. if (tty->echo_buf) {
  1395. kfree(tty->echo_buf);
  1396. tty->echo_buf = NULL;
  1397. }
  1398. }
  1399. /**
  1400. * n_tty_open - open an ldisc
  1401. * @tty: terminal to open
  1402. *
  1403. * Called when this line discipline is being attached to the
  1404. * terminal device. Can sleep. Called serialized so that no
  1405. * other events will occur in parallel. No further open will occur
  1406. * until a close.
  1407. */
  1408. static int n_tty_open(struct tty_struct *tty)
  1409. {
  1410. if (!tty)
  1411. return -EINVAL;
  1412. /* These are ugly. Currently a malloc failure here can panic */
  1413. if (!tty->read_buf) {
  1414. tty->read_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
  1415. if (!tty->read_buf)
  1416. return -ENOMEM;
  1417. }
  1418. if (!tty->echo_buf) {
  1419. tty->echo_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
  1420. if (!tty->echo_buf)
  1421. return -ENOMEM;
  1422. }
  1423. reset_buffer_flags(tty);
  1424. tty->column = 0;
  1425. n_tty_set_termios(tty, NULL);
  1426. tty->minimum_to_wake = 1;
  1427. tty->closing = 0;
  1428. return 0;
  1429. }
  1430. static inline int input_available_p(struct tty_struct *tty, int amt)
  1431. {
  1432. tty_flush_to_ldisc(tty);
  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. if (b - buf != nr && tty->fasync)
  1808. set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  1809. return (b - buf) ? b - buf : retval;
  1810. }
  1811. /**
  1812. * n_tty_poll - poll method for N_TTY
  1813. * @tty: terminal device
  1814. * @file: file accessing it
  1815. * @wait: poll table
  1816. *
  1817. * Called when the line discipline is asked to poll() for data or
  1818. * for special events. This code is not serialized with respect to
  1819. * other events save open/close.
  1820. *
  1821. * This code must be sure never to sleep through a hangup.
  1822. * Called without the kernel lock held - fine
  1823. */
  1824. static unsigned int n_tty_poll(struct tty_struct *tty, struct file *file,
  1825. poll_table *wait)
  1826. {
  1827. unsigned int mask = 0;
  1828. poll_wait(file, &tty->read_wait, wait);
  1829. poll_wait(file, &tty->write_wait, wait);
  1830. if (input_available_p(tty, TIME_CHAR(tty) ? 0 : MIN_CHAR(tty)))
  1831. mask |= POLLIN | POLLRDNORM;
  1832. if (tty->packet && tty->link->ctrl_status)
  1833. mask |= POLLPRI | POLLIN | POLLRDNORM;
  1834. if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
  1835. mask |= POLLHUP;
  1836. if (tty_hung_up_p(file))
  1837. mask |= POLLHUP;
  1838. if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) {
  1839. if (MIN_CHAR(tty) && !TIME_CHAR(tty))
  1840. tty->minimum_to_wake = MIN_CHAR(tty);
  1841. else
  1842. tty->minimum_to_wake = 1;
  1843. }
  1844. if (tty->ops->write && !tty_is_writelocked(tty) &&
  1845. tty_chars_in_buffer(tty) < WAKEUP_CHARS &&
  1846. tty_write_room(tty) > 0)
  1847. mask |= POLLOUT | POLLWRNORM;
  1848. return mask;
  1849. }
  1850. static unsigned long inq_canon(struct tty_struct *tty)
  1851. {
  1852. int nr, head, tail;
  1853. if (!tty->canon_data)
  1854. return 0;
  1855. head = tty->canon_head;
  1856. tail = tty->read_tail;
  1857. nr = (head - tail) & (N_TTY_BUF_SIZE-1);
  1858. /* Skip EOF-chars.. */
  1859. while (head != tail) {
  1860. if (test_bit(tail, tty->read_flags) &&
  1861. tty->read_buf[tail] == __DISABLED_CHAR)
  1862. nr--;
  1863. tail = (tail+1) & (N_TTY_BUF_SIZE-1);
  1864. }
  1865. return nr;
  1866. }
  1867. static int n_tty_ioctl(struct tty_struct *tty, struct file *file,
  1868. unsigned int cmd, unsigned long arg)
  1869. {
  1870. int retval;
  1871. switch (cmd) {
  1872. case TIOCOUTQ:
  1873. return put_user(tty_chars_in_buffer(tty), (int __user *) arg);
  1874. case TIOCINQ:
  1875. /* FIXME: Locking */
  1876. retval = tty->read_cnt;
  1877. if (L_ICANON(tty))
  1878. retval = inq_canon(tty);
  1879. return put_user(retval, (unsigned int __user *) arg);
  1880. default:
  1881. return n_tty_ioctl_helper(tty, file, cmd, arg);
  1882. }
  1883. }
  1884. struct tty_ldisc_ops tty_ldisc_N_TTY = {
  1885. .magic = TTY_LDISC_MAGIC,
  1886. .name = "n_tty",
  1887. .open = n_tty_open,
  1888. .close = n_tty_close,
  1889. .flush_buffer = n_tty_flush_buffer,
  1890. .chars_in_buffer = n_tty_chars_in_buffer,
  1891. .read = n_tty_read,
  1892. .write = n_tty_write,
  1893. .ioctl = n_tty_ioctl,
  1894. .set_termios = n_tty_set_termios,
  1895. .poll = n_tty_poll,
  1896. .receive_buf = n_tty_receive_buf,
  1897. .write_wakeup = n_tty_write_wakeup
  1898. };
  1899. /**
  1900. * n_tty_inherit_ops - inherit N_TTY methods
  1901. * @ops: struct tty_ldisc_ops where to save N_TTY methods
  1902. *
  1903. * Used by a generic struct tty_ldisc_ops to easily inherit N_TTY
  1904. * methods.
  1905. */
  1906. void n_tty_inherit_ops(struct tty_ldisc_ops *ops)
  1907. {
  1908. *ops = tty_ldisc_N_TTY;
  1909. ops->owner = NULL;
  1910. ops->refcount = ops->flags = 0;
  1911. }
  1912. EXPORT_SYMBOL_GPL(n_tty_inherit_ops);