n_tty.c 55 KB

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