n_tty.c 55 KB

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