n_tty.c 54 KB

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