n_tty.c 55 KB

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