n_tty.c 55 KB

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