n_tty.c 55 KB

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