n_tty.c 56 KB

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