n_tty.c 55 KB

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