n_tty.c 55 KB

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