n_tty.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561
  1. // SPDX-License-Identifier: GPL-1.0+
  2. /*
  3. * n_tty.c --- implements the N_TTY line discipline.
  4. *
  5. * This code used to be in tty_io.c, but things are getting hairy
  6. * enough that it made sense to split things off. (The N_TTY
  7. * processing has changed so much that it's hardly recognizable,
  8. * anyway...)
  9. *
  10. * Note that the open routine for N_TTY is guaranteed never to return
  11. * an error. This is because Linux will fall back to setting a line
  12. * to N_TTY if it can not switch to any other line discipline.
  13. *
  14. * Written by Theodore Ts'o, Copyright 1994.
  15. *
  16. * This file also contains code originally written by Linus Torvalds,
  17. * Copyright 1991, 1992, 1993, and by Julian Cowley, Copyright 1994.
  18. *
  19. * Reduced memory usage for older ARM systems - Russell King.
  20. *
  21. * 2000/01/20 Fixed SMP locking on put_tty_queue using bits of
  22. * the patch by Andrew J. Kroll <ag784@freenet.buffalo.edu>
  23. * who actually finally proved there really was a race.
  24. *
  25. * 2002/03/18 Implemented n_tty_wakeup to send SIGIO POLL_OUTs to
  26. * waiting writing processes-Sapan Bhatia <sapan@corewars.org>.
  27. * Also fixed a bug in BLOCKING mode where n_tty_write returns
  28. * EAGAIN
  29. */
  30. #include <linux/bitmap.h>
  31. #include <linux/bitops.h>
  32. #include <linux/ctype.h>
  33. #include <linux/errno.h>
  34. #include <linux/export.h>
  35. #include <linux/fcntl.h>
  36. #include <linux/file.h>
  37. #include <linux/jiffies.h>
  38. #include <linux/math.h>
  39. #include <linux/poll.h>
  40. #include <linux/ratelimit.h>
  41. #include <linux/sched.h>
  42. #include <linux/signal.h>
  43. #include <linux/slab.h>
  44. #include <linux/string.h>
  45. #include <linux/tty.h>
  46. #include <linux/types.h>
  47. #include <linux/uaccess.h>
  48. #include <linux/vmalloc.h>
  49. #include "tty.h"
  50. /*
  51. * Until this number of characters is queued in the xmit buffer, select will
  52. * return "we have room for writes".
  53. */
  54. #define WAKEUP_CHARS 256
  55. /*
  56. * This defines the low- and high-watermarks for throttling and
  57. * unthrottling the TTY driver. These watermarks are used for
  58. * controlling the space in the read buffer.
  59. */
  60. #define TTY_THRESHOLD_THROTTLE 128 /* now based on remaining room */
  61. #define TTY_THRESHOLD_UNTHROTTLE 128
  62. /*
  63. * Special byte codes used in the echo buffer to represent operations
  64. * or special handling of characters. Bytes in the echo buffer that
  65. * are not part of such special blocks are treated as normal character
  66. * codes.
  67. */
  68. #define ECHO_OP_START 0xff
  69. #define ECHO_OP_MOVE_BACK_COL 0x80
  70. #define ECHO_OP_SET_CANON_COL 0x81
  71. #define ECHO_OP_ERASE_TAB 0x82
  72. #define ECHO_COMMIT_WATERMARK 256
  73. #define ECHO_BLOCK 256
  74. #define ECHO_DISCARD_WATERMARK N_TTY_BUF_SIZE - (ECHO_BLOCK + 32)
  75. #undef N_TTY_TRACE
  76. #ifdef N_TTY_TRACE
  77. # define n_tty_trace(f, args...) trace_printk(f, ##args)
  78. #else
  79. # define n_tty_trace(f, args...) no_printk(f, ##args)
  80. #endif
  81. struct n_tty_data {
  82. /* producer-published */
  83. size_t read_head;
  84. size_t commit_head;
  85. size_t canon_head;
  86. size_t echo_head;
  87. size_t echo_commit;
  88. size_t echo_mark;
  89. DECLARE_BITMAP(char_map, 256);
  90. /* private to n_tty_receive_overrun (single-threaded) */
  91. unsigned long overrun_time;
  92. unsigned int num_overrun;
  93. /* non-atomic */
  94. bool no_room;
  95. /* must hold exclusive termios_rwsem to reset these */
  96. unsigned char lnext:1, erasing:1, raw:1, real_raw:1, icanon:1;
  97. unsigned char push:1;
  98. /* shared by producer and consumer */
  99. u8 read_buf[N_TTY_BUF_SIZE];
  100. DECLARE_BITMAP(read_flags, N_TTY_BUF_SIZE);
  101. u8 echo_buf[N_TTY_BUF_SIZE];
  102. /* consumer-published */
  103. size_t read_tail;
  104. size_t line_start;
  105. /* # of chars looked ahead (to find software flow control chars) */
  106. size_t lookahead_count;
  107. /* protected by output lock */
  108. unsigned int column;
  109. unsigned int canon_column;
  110. size_t echo_tail;
  111. struct mutex atomic_read_lock;
  112. struct mutex output_lock;
  113. };
  114. #define MASK(x) ((x) & (N_TTY_BUF_SIZE - 1))
  115. static inline size_t read_cnt(struct n_tty_data *ldata)
  116. {
  117. return ldata->read_head - ldata->read_tail;
  118. }
  119. static inline u8 read_buf(struct n_tty_data *ldata, size_t i)
  120. {
  121. return ldata->read_buf[MASK(i)];
  122. }
  123. static inline u8 *read_buf_addr(struct n_tty_data *ldata, size_t i)
  124. {
  125. return &ldata->read_buf[MASK(i)];
  126. }
  127. static inline u8 echo_buf(struct n_tty_data *ldata, size_t i)
  128. {
  129. smp_rmb(); /* Matches smp_wmb() in add_echo_byte(). */
  130. return ldata->echo_buf[MASK(i)];
  131. }
  132. static inline u8 *echo_buf_addr(struct n_tty_data *ldata, size_t i)
  133. {
  134. return &ldata->echo_buf[MASK(i)];
  135. }
  136. /* If we are not echoing the data, perhaps this is a secret so erase it */
  137. static void zero_buffer(const struct tty_struct *tty, u8 *buffer, size_t size)
  138. {
  139. if (L_ICANON(tty) && !L_ECHO(tty))
  140. memset(buffer, 0, size);
  141. }
  142. static void tty_copy(const struct tty_struct *tty, void *to, size_t tail,
  143. size_t n)
  144. {
  145. struct n_tty_data *ldata = tty->disc_data;
  146. size_t size = N_TTY_BUF_SIZE - tail;
  147. void *from = read_buf_addr(ldata, tail);
  148. if (n > size) {
  149. tty_audit_add_data(tty, from, size);
  150. memcpy(to, from, size);
  151. zero_buffer(tty, from, size);
  152. to += size;
  153. n -= size;
  154. from = ldata->read_buf;
  155. }
  156. tty_audit_add_data(tty, from, n);
  157. memcpy(to, from, n);
  158. zero_buffer(tty, from, n);
  159. }
  160. /**
  161. * n_tty_kick_worker - start input worker (if required)
  162. * @tty: terminal
  163. *
  164. * Re-schedules the flip buffer work if it may have stopped.
  165. *
  166. * Locking:
  167. * * Caller holds exclusive %termios_rwsem, or
  168. * * n_tty_read()/consumer path:
  169. * holds non-exclusive %termios_rwsem
  170. */
  171. static void n_tty_kick_worker(const struct tty_struct *tty)
  172. {
  173. struct n_tty_data *ldata = tty->disc_data;
  174. /* Did the input worker stop? Restart it */
  175. if (unlikely(READ_ONCE(ldata->no_room))) {
  176. WRITE_ONCE(ldata->no_room, 0);
  177. WARN_RATELIMIT(tty->port->itty == NULL,
  178. "scheduling with invalid itty\n");
  179. /* see if ldisc has been killed - if so, this means that
  180. * even though the ldisc has been halted and ->buf.work
  181. * cancelled, ->buf.work is about to be rescheduled
  182. */
  183. WARN_RATELIMIT(test_bit(TTY_LDISC_HALTED, &tty->flags),
  184. "scheduling buffer work for halted ldisc\n");
  185. tty_buffer_restart_work(tty->port);
  186. }
  187. }
  188. static ssize_t chars_in_buffer(const struct tty_struct *tty)
  189. {
  190. const struct n_tty_data *ldata = tty->disc_data;
  191. size_t head = ldata->icanon ? ldata->canon_head : ldata->commit_head;
  192. return head - ldata->read_tail;
  193. }
  194. /**
  195. * n_tty_write_wakeup - asynchronous I/O notifier
  196. * @tty: tty device
  197. *
  198. * Required for the ptys, serial driver etc. since processes that attach
  199. * themselves to the master and rely on ASYNC IO must be woken up.
  200. */
  201. static void n_tty_write_wakeup(struct tty_struct *tty)
  202. {
  203. clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  204. kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
  205. }
  206. static void n_tty_check_throttle(struct tty_struct *tty)
  207. {
  208. struct n_tty_data *ldata = tty->disc_data;
  209. /*
  210. * Check the remaining room for the input canonicalization
  211. * mode. We don't want to throttle the driver if we're in
  212. * canonical mode and don't have a newline yet!
  213. */
  214. if (ldata->icanon && ldata->canon_head == ldata->read_tail)
  215. return;
  216. do {
  217. tty_set_flow_change(tty, TTY_THROTTLE_SAFE);
  218. if (N_TTY_BUF_SIZE - read_cnt(ldata) >= TTY_THRESHOLD_THROTTLE)
  219. break;
  220. } while (!tty_throttle_safe(tty));
  221. __tty_set_flow_change(tty, 0);
  222. }
  223. static void n_tty_check_unthrottle(struct tty_struct *tty)
  224. {
  225. if (tty->driver->type == TTY_DRIVER_TYPE_PTY) {
  226. if (chars_in_buffer(tty) > TTY_THRESHOLD_UNTHROTTLE)
  227. return;
  228. n_tty_kick_worker(tty);
  229. tty_wakeup(tty->link);
  230. return;
  231. }
  232. /* If there is enough space in the read buffer now, let the
  233. * low-level driver know. We use chars_in_buffer() to
  234. * check the buffer, as it now knows about canonical mode.
  235. * Otherwise, if the driver is throttled and the line is
  236. * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
  237. * we won't get any more characters.
  238. */
  239. do {
  240. tty_set_flow_change(tty, TTY_UNTHROTTLE_SAFE);
  241. if (chars_in_buffer(tty) > TTY_THRESHOLD_UNTHROTTLE)
  242. break;
  243. n_tty_kick_worker(tty);
  244. } while (!tty_unthrottle_safe(tty));
  245. __tty_set_flow_change(tty, 0);
  246. }
  247. /**
  248. * put_tty_queue - add character to tty
  249. * @c: character
  250. * @ldata: n_tty data
  251. *
  252. * Add a character to the tty read_buf queue.
  253. *
  254. * Locking:
  255. * * n_tty_receive_buf()/producer path:
  256. * caller holds non-exclusive %termios_rwsem
  257. */
  258. static inline void put_tty_queue(u8 c, struct n_tty_data *ldata)
  259. {
  260. *read_buf_addr(ldata, ldata->read_head) = c;
  261. ldata->read_head++;
  262. }
  263. /**
  264. * reset_buffer_flags - reset buffer state
  265. * @ldata: line disc data to reset
  266. *
  267. * Reset the read buffer counters and clear the flags. Called from
  268. * n_tty_open() and n_tty_flush_buffer().
  269. *
  270. * Locking:
  271. * * caller holds exclusive %termios_rwsem, or
  272. * * (locking is not required)
  273. */
  274. static void reset_buffer_flags(struct n_tty_data *ldata)
  275. {
  276. ldata->read_head = ldata->canon_head = ldata->read_tail = 0;
  277. ldata->commit_head = 0;
  278. ldata->line_start = 0;
  279. ldata->erasing = 0;
  280. bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
  281. ldata->push = 0;
  282. ldata->lookahead_count = 0;
  283. }
  284. static void n_tty_packet_mode_flush(struct tty_struct *tty)
  285. {
  286. unsigned long flags;
  287. if (tty->link->ctrl.packet) {
  288. spin_lock_irqsave(&tty->ctrl.lock, flags);
  289. tty->ctrl.pktstatus |= TIOCPKT_FLUSHREAD;
  290. spin_unlock_irqrestore(&tty->ctrl.lock, flags);
  291. wake_up_interruptible(&tty->link->read_wait);
  292. }
  293. }
  294. /**
  295. * n_tty_flush_buffer - clean input queue
  296. * @tty: terminal device
  297. *
  298. * Flush the input buffer. Called when the tty layer wants the buffer flushed
  299. * (eg at hangup) or when the %N_TTY line discipline internally has to clean
  300. * the pending queue (for example some signals).
  301. *
  302. * Holds %termios_rwsem to exclude producer/consumer while buffer indices are
  303. * reset.
  304. *
  305. * Locking: %ctrl.lock, exclusive %termios_rwsem
  306. */
  307. static void n_tty_flush_buffer(struct tty_struct *tty)
  308. {
  309. down_write(&tty->termios_rwsem);
  310. reset_buffer_flags(tty->disc_data);
  311. n_tty_kick_worker(tty);
  312. if (tty->link)
  313. n_tty_packet_mode_flush(tty);
  314. up_write(&tty->termios_rwsem);
  315. }
  316. /**
  317. * is_utf8_continuation - utf8 multibyte check
  318. * @c: byte to check
  319. *
  320. * Returns: true if the utf8 character @c is a multibyte continuation
  321. * character. We use this to correctly compute the on-screen size of the
  322. * character when printing.
  323. */
  324. static inline int is_utf8_continuation(u8 c)
  325. {
  326. return (c & 0xc0) == 0x80;
  327. }
  328. /**
  329. * is_continuation - multibyte check
  330. * @c: byte to check
  331. * @tty: terminal device
  332. *
  333. * Returns: true if the utf8 character @c is a multibyte continuation character
  334. * and the terminal is in unicode mode.
  335. */
  336. static inline int is_continuation(u8 c, const struct tty_struct *tty)
  337. {
  338. return I_IUTF8(tty) && is_utf8_continuation(c);
  339. }
  340. /**
  341. * do_output_char - output one character
  342. * @c: character (or partial unicode symbol)
  343. * @tty: terminal device
  344. * @space: space available in tty driver write buffer
  345. *
  346. * This is a helper function that handles one output character (including
  347. * special characters like TAB, CR, LF, etc.), doing OPOST processing and
  348. * putting the results in the tty driver's write buffer.
  349. *
  350. * Note that Linux currently ignores TABDLY, CRDLY, VTDLY, FFDLY and NLDLY.
  351. * They simply aren't relevant in the world today. If you ever need them, add
  352. * them here.
  353. *
  354. * Returns: the number of bytes of buffer space used or -1 if no space left.
  355. *
  356. * Locking: should be called under the %output_lock to protect the column state
  357. * and space left in the buffer.
  358. */
  359. static int do_output_char(u8 c, struct tty_struct *tty, int space)
  360. {
  361. struct n_tty_data *ldata = tty->disc_data;
  362. int spaces;
  363. if (!space)
  364. return -1;
  365. switch (c) {
  366. case '\n':
  367. if (O_ONLRET(tty))
  368. ldata->column = 0;
  369. if (O_ONLCR(tty)) {
  370. if (space < 2)
  371. return -1;
  372. ldata->canon_column = ldata->column = 0;
  373. tty->ops->write(tty, "\r\n", 2);
  374. return 2;
  375. }
  376. ldata->canon_column = ldata->column;
  377. break;
  378. case '\r':
  379. if (O_ONOCR(tty) && ldata->column == 0)
  380. return 0;
  381. if (O_OCRNL(tty)) {
  382. c = '\n';
  383. if (O_ONLRET(tty))
  384. ldata->canon_column = ldata->column = 0;
  385. break;
  386. }
  387. ldata->canon_column = ldata->column = 0;
  388. break;
  389. case '\t':
  390. spaces = 8 - (ldata->column & 7);
  391. if (O_TABDLY(tty) == XTABS) {
  392. if (space < spaces)
  393. return -1;
  394. ldata->column += spaces;
  395. tty->ops->write(tty, " ", spaces);
  396. return spaces;
  397. }
  398. ldata->column += spaces;
  399. break;
  400. case '\b':
  401. if (ldata->column > 0)
  402. ldata->column--;
  403. break;
  404. default:
  405. if (!iscntrl(c)) {
  406. if (O_OLCUC(tty))
  407. c = toupper(c);
  408. if (!is_continuation(c, tty))
  409. ldata->column++;
  410. }
  411. break;
  412. }
  413. tty_put_char(tty, c);
  414. return 1;
  415. }
  416. /**
  417. * process_output - output post processor
  418. * @c: character (or partial unicode symbol)
  419. * @tty: terminal device
  420. *
  421. * Output one character with OPOST processing.
  422. *
  423. * Returns: -1 when the output device is full and the character must be
  424. * retried.
  425. *
  426. * Locking: %output_lock to protect column state and space left (also, this is
  427. *called from n_tty_write() under the tty layer write lock).
  428. */
  429. static int process_output(u8 c, struct tty_struct *tty)
  430. {
  431. struct n_tty_data *ldata = tty->disc_data;
  432. unsigned int space;
  433. int retval;
  434. mutex_lock(&ldata->output_lock);
  435. space = tty_write_room(tty);
  436. retval = do_output_char(c, tty, space);
  437. mutex_unlock(&ldata->output_lock);
  438. if (retval < 0)
  439. return -1;
  440. else
  441. return 0;
  442. }
  443. /**
  444. * process_output_block - block post processor
  445. * @tty: terminal device
  446. * @buf: character buffer
  447. * @nr: number of bytes to output
  448. *
  449. * Output a block of characters with OPOST processing.
  450. *
  451. * This path is used to speed up block console writes, among other things when
  452. * processing blocks of output data. It handles only the simple cases normally
  453. * found and helps to generate blocks of symbols for the console driver and
  454. * thus improve performance.
  455. *
  456. * Returns: the number of characters output.
  457. *
  458. * Locking: %output_lock to protect column state and space left (also, this is
  459. * called from n_tty_write() under the tty layer write lock).
  460. */
  461. static ssize_t process_output_block(struct tty_struct *tty,
  462. const u8 *buf, unsigned int nr)
  463. {
  464. struct n_tty_data *ldata = tty->disc_data;
  465. unsigned int space;
  466. int i;
  467. const u8 *cp;
  468. mutex_lock(&ldata->output_lock);
  469. space = tty_write_room(tty);
  470. if (space == 0) {
  471. mutex_unlock(&ldata->output_lock);
  472. return 0;
  473. }
  474. if (nr > space)
  475. nr = space;
  476. for (i = 0, cp = buf; i < nr; i++, cp++) {
  477. u8 c = *cp;
  478. switch (c) {
  479. case '\n':
  480. if (O_ONLRET(tty))
  481. ldata->column = 0;
  482. if (O_ONLCR(tty))
  483. goto break_out;
  484. ldata->canon_column = ldata->column;
  485. break;
  486. case '\r':
  487. if (O_ONOCR(tty) && ldata->column == 0)
  488. goto break_out;
  489. if (O_OCRNL(tty))
  490. goto break_out;
  491. ldata->canon_column = ldata->column = 0;
  492. break;
  493. case '\t':
  494. goto break_out;
  495. case '\b':
  496. if (ldata->column > 0)
  497. ldata->column--;
  498. break;
  499. default:
  500. if (!iscntrl(c)) {
  501. if (O_OLCUC(tty))
  502. goto break_out;
  503. if (!is_continuation(c, tty))
  504. ldata->column++;
  505. }
  506. break;
  507. }
  508. }
  509. break_out:
  510. i = tty->ops->write(tty, buf, i);
  511. mutex_unlock(&ldata->output_lock);
  512. return i;
  513. }
  514. static int n_tty_process_echo_ops(struct tty_struct *tty, size_t *tail,
  515. int space)
  516. {
  517. struct n_tty_data *ldata = tty->disc_data;
  518. u8 op;
  519. /*
  520. * Since add_echo_byte() is called without holding output_lock, we
  521. * might see only portion of multi-byte operation.
  522. */
  523. if (MASK(ldata->echo_commit) == MASK(*tail + 1))
  524. return -ENODATA;
  525. /*
  526. * If the buffer byte is the start of a multi-byte operation, get the
  527. * next byte, which is either the op code or a control character value.
  528. */
  529. op = echo_buf(ldata, *tail + 1);
  530. switch (op) {
  531. case ECHO_OP_ERASE_TAB: {
  532. unsigned int num_chars, num_bs;
  533. if (MASK(ldata->echo_commit) == MASK(*tail + 2))
  534. return -ENODATA;
  535. num_chars = echo_buf(ldata, *tail + 2);
  536. /*
  537. * Determine how many columns to go back in order to erase the
  538. * tab. This depends on the number of columns used by other
  539. * characters within the tab area. If this (modulo 8) count is
  540. * from the start of input rather than from a previous tab, we
  541. * offset by canon column. Otherwise, tab spacing is normal.
  542. */
  543. if (!(num_chars & 0x80))
  544. num_chars += ldata->canon_column;
  545. num_bs = 8 - (num_chars & 7);
  546. if (num_bs > space)
  547. return -ENOSPC;
  548. space -= num_bs;
  549. while (num_bs--) {
  550. tty_put_char(tty, '\b');
  551. if (ldata->column > 0)
  552. ldata->column--;
  553. }
  554. *tail += 3;
  555. break;
  556. }
  557. case ECHO_OP_SET_CANON_COL:
  558. ldata->canon_column = ldata->column;
  559. *tail += 2;
  560. break;
  561. case ECHO_OP_MOVE_BACK_COL:
  562. if (ldata->column > 0)
  563. ldata->column--;
  564. *tail += 2;
  565. break;
  566. case ECHO_OP_START:
  567. /* This is an escaped echo op start code */
  568. if (!space)
  569. return -ENOSPC;
  570. tty_put_char(tty, ECHO_OP_START);
  571. ldata->column++;
  572. space--;
  573. *tail += 2;
  574. break;
  575. default:
  576. /*
  577. * If the op is not a special byte code, it is a ctrl char
  578. * tagged to be echoed as "^X" (where X is the letter
  579. * representing the control char). Note that we must ensure
  580. * there is enough space for the whole ctrl pair.
  581. */
  582. if (space < 2)
  583. return -ENOSPC;
  584. tty_put_char(tty, '^');
  585. tty_put_char(tty, op ^ 0100);
  586. ldata->column += 2;
  587. space -= 2;
  588. *tail += 2;
  589. break;
  590. }
  591. return space;
  592. }
  593. /**
  594. * __process_echoes - write pending echo characters
  595. * @tty: terminal device
  596. *
  597. * Write previously buffered echo (and other ldisc-generated) characters to the
  598. * tty.
  599. *
  600. * Characters generated by the ldisc (including echoes) need to be buffered
  601. * because the driver's write buffer can fill during heavy program output.
  602. * Echoing straight to the driver will often fail under these conditions,
  603. * causing lost characters and resulting mismatches of ldisc state information.
  604. *
  605. * Since the ldisc state must represent the characters actually sent to the
  606. * driver at the time of the write, operations like certain changes in column
  607. * state are also saved in the buffer and executed here.
  608. *
  609. * A circular fifo buffer is used so that the most recent characters are
  610. * prioritized. Also, when control characters are echoed with a prefixed "^",
  611. * the pair is treated atomically and thus not separated.
  612. *
  613. * Locking: callers must hold %output_lock.
  614. */
  615. static size_t __process_echoes(struct tty_struct *tty)
  616. {
  617. struct n_tty_data *ldata = tty->disc_data;
  618. unsigned int space, old_space;
  619. size_t tail;
  620. u8 c;
  621. old_space = space = tty_write_room(tty);
  622. tail = ldata->echo_tail;
  623. while (MASK(ldata->echo_commit) != MASK(tail)) {
  624. c = echo_buf(ldata, tail);
  625. if (c == ECHO_OP_START) {
  626. int ret = n_tty_process_echo_ops(tty, &tail, space);
  627. if (ret == -ENODATA)
  628. goto not_yet_stored;
  629. if (ret < 0)
  630. break;
  631. space = ret;
  632. } else {
  633. if (O_OPOST(tty)) {
  634. int retval = do_output_char(c, tty, space);
  635. if (retval < 0)
  636. break;
  637. space -= retval;
  638. } else {
  639. if (!space)
  640. break;
  641. tty_put_char(tty, c);
  642. space -= 1;
  643. }
  644. tail += 1;
  645. }
  646. }
  647. /* If the echo buffer is nearly full (so that the possibility exists
  648. * of echo overrun before the next commit), then discard enough
  649. * data at the tail to prevent a subsequent overrun */
  650. while (ldata->echo_commit > tail &&
  651. ldata->echo_commit - tail >= ECHO_DISCARD_WATERMARK) {
  652. if (echo_buf(ldata, tail) == ECHO_OP_START) {
  653. if (echo_buf(ldata, tail + 1) == ECHO_OP_ERASE_TAB)
  654. tail += 3;
  655. else
  656. tail += 2;
  657. } else
  658. tail++;
  659. }
  660. not_yet_stored:
  661. ldata->echo_tail = tail;
  662. return old_space - space;
  663. }
  664. static void commit_echoes(struct tty_struct *tty)
  665. {
  666. struct n_tty_data *ldata = tty->disc_data;
  667. size_t nr, old, echoed;
  668. size_t head;
  669. mutex_lock(&ldata->output_lock);
  670. head = ldata->echo_head;
  671. ldata->echo_mark = head;
  672. old = ldata->echo_commit - ldata->echo_tail;
  673. /* Process committed echoes if the accumulated # of bytes
  674. * is over the threshold (and try again each time another
  675. * block is accumulated) */
  676. nr = head - ldata->echo_tail;
  677. if (nr < ECHO_COMMIT_WATERMARK ||
  678. (nr % ECHO_BLOCK > old % ECHO_BLOCK)) {
  679. mutex_unlock(&ldata->output_lock);
  680. return;
  681. }
  682. ldata->echo_commit = head;
  683. echoed = __process_echoes(tty);
  684. mutex_unlock(&ldata->output_lock);
  685. if (echoed && tty->ops->flush_chars)
  686. tty->ops->flush_chars(tty);
  687. }
  688. static void process_echoes(struct tty_struct *tty)
  689. {
  690. struct n_tty_data *ldata = tty->disc_data;
  691. size_t echoed;
  692. if (ldata->echo_mark == ldata->echo_tail)
  693. return;
  694. mutex_lock(&ldata->output_lock);
  695. ldata->echo_commit = ldata->echo_mark;
  696. echoed = __process_echoes(tty);
  697. mutex_unlock(&ldata->output_lock);
  698. if (echoed && tty->ops->flush_chars)
  699. tty->ops->flush_chars(tty);
  700. }
  701. /* NB: echo_mark and echo_head should be equivalent here */
  702. static void flush_echoes(struct tty_struct *tty)
  703. {
  704. struct n_tty_data *ldata = tty->disc_data;
  705. if ((!L_ECHO(tty) && !L_ECHONL(tty)) ||
  706. ldata->echo_commit == ldata->echo_head)
  707. return;
  708. mutex_lock(&ldata->output_lock);
  709. ldata->echo_commit = ldata->echo_head;
  710. __process_echoes(tty);
  711. mutex_unlock(&ldata->output_lock);
  712. }
  713. /**
  714. * add_echo_byte - add a byte to the echo buffer
  715. * @c: unicode byte to echo
  716. * @ldata: n_tty data
  717. *
  718. * Add a character or operation byte to the echo buffer.
  719. */
  720. static inline void add_echo_byte(u8 c, struct n_tty_data *ldata)
  721. {
  722. *echo_buf_addr(ldata, ldata->echo_head) = c;
  723. smp_wmb(); /* Matches smp_rmb() in echo_buf(). */
  724. ldata->echo_head++;
  725. }
  726. /**
  727. * echo_move_back_col - add operation to move back a column
  728. * @ldata: n_tty data
  729. *
  730. * Add an operation to the echo buffer to move back one column.
  731. */
  732. static void echo_move_back_col(struct n_tty_data *ldata)
  733. {
  734. add_echo_byte(ECHO_OP_START, ldata);
  735. add_echo_byte(ECHO_OP_MOVE_BACK_COL, ldata);
  736. }
  737. /**
  738. * echo_set_canon_col - add operation to set the canon column
  739. * @ldata: n_tty data
  740. *
  741. * Add an operation to the echo buffer to set the canon column to the current
  742. * column.
  743. */
  744. static void echo_set_canon_col(struct n_tty_data *ldata)
  745. {
  746. add_echo_byte(ECHO_OP_START, ldata);
  747. add_echo_byte(ECHO_OP_SET_CANON_COL, ldata);
  748. }
  749. /**
  750. * echo_erase_tab - add operation to erase a tab
  751. * @num_chars: number of character columns already used
  752. * @after_tab: true if num_chars starts after a previous tab
  753. * @ldata: n_tty data
  754. *
  755. * Add an operation to the echo buffer to erase a tab.
  756. *
  757. * Called by the eraser function, which knows how many character columns have
  758. * been used since either a previous tab or the start of input. This
  759. * information will be used later, along with canon column (if applicable), to
  760. * go back the correct number of columns.
  761. */
  762. static void echo_erase_tab(unsigned int num_chars, int after_tab,
  763. struct n_tty_data *ldata)
  764. {
  765. add_echo_byte(ECHO_OP_START, ldata);
  766. add_echo_byte(ECHO_OP_ERASE_TAB, ldata);
  767. /* We only need to know this modulo 8 (tab spacing) */
  768. num_chars &= 7;
  769. /* Set the high bit as a flag if num_chars is after a previous tab */
  770. if (after_tab)
  771. num_chars |= 0x80;
  772. add_echo_byte(num_chars, ldata);
  773. }
  774. /**
  775. * echo_char_raw - echo a character raw
  776. * @c: unicode byte to echo
  777. * @ldata: line disc data
  778. *
  779. * Echo user input back onto the screen. This must be called only when
  780. * L_ECHO(tty) is true. Called from the &tty_driver.receive_buf() path.
  781. *
  782. * This variant does not treat control characters specially.
  783. */
  784. static void echo_char_raw(u8 c, struct n_tty_data *ldata)
  785. {
  786. if (c == ECHO_OP_START) {
  787. add_echo_byte(ECHO_OP_START, ldata);
  788. add_echo_byte(ECHO_OP_START, ldata);
  789. } else {
  790. add_echo_byte(c, ldata);
  791. }
  792. }
  793. /**
  794. * echo_char - echo a character
  795. * @c: unicode byte to echo
  796. * @tty: terminal device
  797. *
  798. * Echo user input back onto the screen. This must be called only when
  799. * L_ECHO(tty) is true. Called from the &tty_driver.receive_buf() path.
  800. *
  801. * This variant tags control characters to be echoed as "^X" (where X is the
  802. * letter representing the control char).
  803. */
  804. static void echo_char(u8 c, const struct tty_struct *tty)
  805. {
  806. struct n_tty_data *ldata = tty->disc_data;
  807. if (c == ECHO_OP_START) {
  808. add_echo_byte(ECHO_OP_START, ldata);
  809. add_echo_byte(ECHO_OP_START, ldata);
  810. } else {
  811. if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t')
  812. add_echo_byte(ECHO_OP_START, ldata);
  813. add_echo_byte(c, ldata);
  814. }
  815. }
  816. /**
  817. * finish_erasing - complete erase
  818. * @ldata: n_tty data
  819. */
  820. static inline void finish_erasing(struct n_tty_data *ldata)
  821. {
  822. if (ldata->erasing) {
  823. echo_char_raw('/', ldata);
  824. ldata->erasing = 0;
  825. }
  826. }
  827. /**
  828. * eraser - handle erase function
  829. * @c: character input
  830. * @tty: terminal device
  831. *
  832. * Perform erase and necessary output when an erase character is present in the
  833. * stream from the driver layer. Handles the complexities of UTF-8 multibyte
  834. * symbols.
  835. *
  836. * Locking: n_tty_receive_buf()/producer path:
  837. * caller holds non-exclusive %termios_rwsem
  838. */
  839. static void eraser(u8 c, const struct tty_struct *tty)
  840. {
  841. struct n_tty_data *ldata = tty->disc_data;
  842. enum { ERASE, WERASE, KILL } kill_type;
  843. size_t head;
  844. size_t cnt;
  845. int seen_alnums;
  846. if (ldata->read_head == ldata->canon_head) {
  847. /* process_output('\a', tty); */ /* what do you think? */
  848. return;
  849. }
  850. if (c == ERASE_CHAR(tty))
  851. kill_type = ERASE;
  852. else if (c == WERASE_CHAR(tty))
  853. kill_type = WERASE;
  854. else {
  855. if (!L_ECHO(tty)) {
  856. ldata->read_head = ldata->canon_head;
  857. return;
  858. }
  859. if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
  860. ldata->read_head = ldata->canon_head;
  861. finish_erasing(ldata);
  862. echo_char(KILL_CHAR(tty), tty);
  863. /* Add a newline if ECHOK is on and ECHOKE is off. */
  864. if (L_ECHOK(tty))
  865. echo_char_raw('\n', ldata);
  866. return;
  867. }
  868. kill_type = KILL;
  869. }
  870. seen_alnums = 0;
  871. while (MASK(ldata->read_head) != MASK(ldata->canon_head)) {
  872. head = ldata->read_head;
  873. /* erase a single possibly multibyte character */
  874. do {
  875. head--;
  876. c = read_buf(ldata, head);
  877. } while (is_continuation(c, tty) &&
  878. MASK(head) != MASK(ldata->canon_head));
  879. /* do not partially erase */
  880. if (is_continuation(c, tty))
  881. break;
  882. if (kill_type == WERASE) {
  883. /* Equivalent to BSD's ALTWERASE. */
  884. if (isalnum(c) || c == '_')
  885. seen_alnums++;
  886. else if (seen_alnums)
  887. break;
  888. }
  889. cnt = ldata->read_head - head;
  890. ldata->read_head = head;
  891. if (L_ECHO(tty)) {
  892. if (L_ECHOPRT(tty)) {
  893. if (!ldata->erasing) {
  894. echo_char_raw('\\', ldata);
  895. ldata->erasing = 1;
  896. }
  897. /* if cnt > 1, output a multi-byte character */
  898. echo_char(c, tty);
  899. while (--cnt > 0) {
  900. head++;
  901. echo_char_raw(read_buf(ldata, head), ldata);
  902. echo_move_back_col(ldata);
  903. }
  904. } else if (kill_type == ERASE && !L_ECHOE(tty)) {
  905. echo_char(ERASE_CHAR(tty), tty);
  906. } else if (c == '\t') {
  907. unsigned int num_chars = 0;
  908. int after_tab = 0;
  909. size_t tail = ldata->read_head;
  910. /*
  911. * Count the columns used for characters
  912. * since the start of input or after a
  913. * previous tab.
  914. * This info is used to go back the correct
  915. * number of columns.
  916. */
  917. while (MASK(tail) != MASK(ldata->canon_head)) {
  918. tail--;
  919. c = read_buf(ldata, tail);
  920. if (c == '\t') {
  921. after_tab = 1;
  922. break;
  923. } else if (iscntrl(c)) {
  924. if (L_ECHOCTL(tty))
  925. num_chars += 2;
  926. } else if (!is_continuation(c, tty)) {
  927. num_chars++;
  928. }
  929. }
  930. echo_erase_tab(num_chars, after_tab, ldata);
  931. } else {
  932. if (iscntrl(c) && L_ECHOCTL(tty)) {
  933. echo_char_raw('\b', ldata);
  934. echo_char_raw(' ', ldata);
  935. echo_char_raw('\b', ldata);
  936. }
  937. if (!iscntrl(c) || L_ECHOCTL(tty)) {
  938. echo_char_raw('\b', ldata);
  939. echo_char_raw(' ', ldata);
  940. echo_char_raw('\b', ldata);
  941. }
  942. }
  943. }
  944. if (kill_type == ERASE)
  945. break;
  946. }
  947. if (ldata->read_head == ldata->canon_head && L_ECHO(tty))
  948. finish_erasing(ldata);
  949. }
  950. static void __isig(int sig, struct tty_struct *tty)
  951. {
  952. struct pid *tty_pgrp = tty_get_pgrp(tty);
  953. if (tty_pgrp) {
  954. kill_pgrp(tty_pgrp, sig, 1);
  955. put_pid(tty_pgrp);
  956. }
  957. }
  958. /**
  959. * isig - handle the ISIG optio
  960. * @sig: signal
  961. * @tty: terminal
  962. *
  963. * Called when a signal is being sent due to terminal input. Called from the
  964. * &tty_driver.receive_buf() path, so serialized.
  965. *
  966. * Performs input and output flush if !NOFLSH. In this context, the echo
  967. * buffer is 'output'. The signal is processed first to alert any current
  968. * readers or writers to discontinue and exit their i/o loops.
  969. *
  970. * Locking: %ctrl.lock
  971. */
  972. static void isig(int sig, struct tty_struct *tty)
  973. {
  974. struct n_tty_data *ldata = tty->disc_data;
  975. if (L_NOFLSH(tty)) {
  976. /* signal only */
  977. __isig(sig, tty);
  978. } else { /* signal and flush */
  979. up_read(&tty->termios_rwsem);
  980. down_write(&tty->termios_rwsem);
  981. __isig(sig, tty);
  982. /* clear echo buffer */
  983. mutex_lock(&ldata->output_lock);
  984. ldata->echo_head = ldata->echo_tail = 0;
  985. ldata->echo_mark = ldata->echo_commit = 0;
  986. mutex_unlock(&ldata->output_lock);
  987. /* clear output buffer */
  988. tty_driver_flush_buffer(tty);
  989. /* clear input buffer */
  990. reset_buffer_flags(tty->disc_data);
  991. /* notify pty master of flush */
  992. if (tty->link)
  993. n_tty_packet_mode_flush(tty);
  994. up_write(&tty->termios_rwsem);
  995. down_read(&tty->termios_rwsem);
  996. }
  997. }
  998. /**
  999. * n_tty_receive_break - handle break
  1000. * @tty: terminal
  1001. *
  1002. * An RS232 break event has been hit in the incoming bitstream. This can cause
  1003. * a variety of events depending upon the termios settings.
  1004. *
  1005. * Locking: n_tty_receive_buf()/producer path:
  1006. * caller holds non-exclusive termios_rwsem
  1007. *
  1008. * Note: may get exclusive %termios_rwsem if flushing input buffer
  1009. */
  1010. static void n_tty_receive_break(struct tty_struct *tty)
  1011. {
  1012. struct n_tty_data *ldata = tty->disc_data;
  1013. if (I_IGNBRK(tty))
  1014. return;
  1015. if (I_BRKINT(tty)) {
  1016. isig(SIGINT, tty);
  1017. return;
  1018. }
  1019. if (I_PARMRK(tty)) {
  1020. put_tty_queue('\377', ldata);
  1021. put_tty_queue('\0', ldata);
  1022. }
  1023. put_tty_queue('\0', ldata);
  1024. }
  1025. /**
  1026. * n_tty_receive_overrun - handle overrun reporting
  1027. * @tty: terminal
  1028. *
  1029. * Data arrived faster than we could process it. While the tty driver has
  1030. * flagged this the bits that were missed are gone forever.
  1031. *
  1032. * Called from the receive_buf path so single threaded. Does not need locking
  1033. * as num_overrun and overrun_time are function private.
  1034. */
  1035. static void n_tty_receive_overrun(const struct tty_struct *tty)
  1036. {
  1037. struct n_tty_data *ldata = tty->disc_data;
  1038. ldata->num_overrun++;
  1039. if (time_is_before_jiffies(ldata->overrun_time + HZ)) {
  1040. tty_warn(tty, "%u input overrun(s)\n", ldata->num_overrun);
  1041. ldata->overrun_time = jiffies;
  1042. ldata->num_overrun = 0;
  1043. }
  1044. }
  1045. /**
  1046. * n_tty_receive_parity_error - error notifier
  1047. * @tty: terminal device
  1048. * @c: character
  1049. *
  1050. * Process a parity error and queue the right data to indicate the error case
  1051. * if necessary.
  1052. *
  1053. * Locking: n_tty_receive_buf()/producer path:
  1054. * caller holds non-exclusive %termios_rwsem
  1055. */
  1056. static void n_tty_receive_parity_error(const struct tty_struct *tty,
  1057. u8 c)
  1058. {
  1059. struct n_tty_data *ldata = tty->disc_data;
  1060. if (I_INPCK(tty)) {
  1061. if (I_IGNPAR(tty))
  1062. return;
  1063. if (I_PARMRK(tty)) {
  1064. put_tty_queue('\377', ldata);
  1065. put_tty_queue('\0', ldata);
  1066. put_tty_queue(c, ldata);
  1067. } else
  1068. put_tty_queue('\0', ldata);
  1069. } else
  1070. put_tty_queue(c, ldata);
  1071. }
  1072. static void
  1073. n_tty_receive_signal_char(struct tty_struct *tty, int signal, u8 c)
  1074. {
  1075. isig(signal, tty);
  1076. if (I_IXON(tty))
  1077. start_tty(tty);
  1078. if (L_ECHO(tty)) {
  1079. echo_char(c, tty);
  1080. commit_echoes(tty);
  1081. } else
  1082. process_echoes(tty);
  1083. }
  1084. static bool n_tty_is_char_flow_ctrl(struct tty_struct *tty, u8 c)
  1085. {
  1086. return c == START_CHAR(tty) || c == STOP_CHAR(tty);
  1087. }
  1088. /**
  1089. * n_tty_receive_char_flow_ctrl - receive flow control chars
  1090. * @tty: terminal device
  1091. * @c: character
  1092. * @lookahead_done: lookahead has processed this character already
  1093. *
  1094. * Receive and process flow control character actions.
  1095. *
  1096. * In case lookahead for flow control chars already handled the character in
  1097. * advance to the normal receive, the actions are skipped during normal
  1098. * receive.
  1099. *
  1100. * Returns true if @c is consumed as flow-control character, the character
  1101. * must not be treated as normal character.
  1102. */
  1103. static bool n_tty_receive_char_flow_ctrl(struct tty_struct *tty, u8 c,
  1104. bool lookahead_done)
  1105. {
  1106. if (!n_tty_is_char_flow_ctrl(tty, c))
  1107. return false;
  1108. if (lookahead_done)
  1109. return true;
  1110. if (c == START_CHAR(tty)) {
  1111. start_tty(tty);
  1112. process_echoes(tty);
  1113. return true;
  1114. }
  1115. /* STOP_CHAR */
  1116. stop_tty(tty);
  1117. return true;
  1118. }
  1119. static void n_tty_receive_handle_newline(struct tty_struct *tty, u8 c)
  1120. {
  1121. struct n_tty_data *ldata = tty->disc_data;
  1122. set_bit(MASK(ldata->read_head), ldata->read_flags);
  1123. put_tty_queue(c, ldata);
  1124. smp_store_release(&ldata->canon_head, ldata->read_head);
  1125. kill_fasync(&tty->fasync, SIGIO, POLL_IN);
  1126. wake_up_interruptible_poll(&tty->read_wait, EPOLLIN | EPOLLRDNORM);
  1127. }
  1128. static bool n_tty_receive_char_canon(struct tty_struct *tty, u8 c)
  1129. {
  1130. struct n_tty_data *ldata = tty->disc_data;
  1131. if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
  1132. (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
  1133. eraser(c, tty);
  1134. commit_echoes(tty);
  1135. return true;
  1136. }
  1137. if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
  1138. ldata->lnext = 1;
  1139. if (L_ECHO(tty)) {
  1140. finish_erasing(ldata);
  1141. if (L_ECHOCTL(tty)) {
  1142. echo_char_raw('^', ldata);
  1143. echo_char_raw('\b', ldata);
  1144. commit_echoes(tty);
  1145. }
  1146. }
  1147. return true;
  1148. }
  1149. if (c == REPRINT_CHAR(tty) && L_ECHO(tty) && L_IEXTEN(tty)) {
  1150. size_t tail = ldata->canon_head;
  1151. finish_erasing(ldata);
  1152. echo_char(c, tty);
  1153. echo_char_raw('\n', ldata);
  1154. while (MASK(tail) != MASK(ldata->read_head)) {
  1155. echo_char(read_buf(ldata, tail), tty);
  1156. tail++;
  1157. }
  1158. commit_echoes(tty);
  1159. return true;
  1160. }
  1161. if (c == '\n') {
  1162. if (L_ECHO(tty) || L_ECHONL(tty)) {
  1163. echo_char_raw('\n', ldata);
  1164. commit_echoes(tty);
  1165. }
  1166. n_tty_receive_handle_newline(tty, c);
  1167. return true;
  1168. }
  1169. if (c == EOF_CHAR(tty)) {
  1170. c = __DISABLED_CHAR;
  1171. n_tty_receive_handle_newline(tty, c);
  1172. return true;
  1173. }
  1174. if ((c == EOL_CHAR(tty)) ||
  1175. (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
  1176. /*
  1177. * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
  1178. */
  1179. if (L_ECHO(tty)) {
  1180. /* Record the column of first canon char. */
  1181. if (ldata->canon_head == ldata->read_head)
  1182. echo_set_canon_col(ldata);
  1183. echo_char(c, tty);
  1184. commit_echoes(tty);
  1185. }
  1186. /*
  1187. * XXX does PARMRK doubling happen for
  1188. * EOL_CHAR and EOL2_CHAR?
  1189. */
  1190. if (c == '\377' && I_PARMRK(tty))
  1191. put_tty_queue(c, ldata);
  1192. n_tty_receive_handle_newline(tty, c);
  1193. return true;
  1194. }
  1195. return false;
  1196. }
  1197. static void n_tty_receive_char_special(struct tty_struct *tty, u8 c,
  1198. bool lookahead_done)
  1199. {
  1200. struct n_tty_data *ldata = tty->disc_data;
  1201. if (I_IXON(tty) && n_tty_receive_char_flow_ctrl(tty, c, lookahead_done))
  1202. return;
  1203. if (L_ISIG(tty)) {
  1204. if (c == INTR_CHAR(tty)) {
  1205. n_tty_receive_signal_char(tty, SIGINT, c);
  1206. return;
  1207. } else if (c == QUIT_CHAR(tty)) {
  1208. n_tty_receive_signal_char(tty, SIGQUIT, c);
  1209. return;
  1210. } else if (c == SUSP_CHAR(tty)) {
  1211. n_tty_receive_signal_char(tty, SIGTSTP, c);
  1212. return;
  1213. }
  1214. }
  1215. if (tty->flow.stopped && !tty->flow.tco_stopped && I_IXON(tty) && I_IXANY(tty)) {
  1216. start_tty(tty);
  1217. process_echoes(tty);
  1218. }
  1219. if (c == '\r') {
  1220. if (I_IGNCR(tty))
  1221. return;
  1222. if (I_ICRNL(tty))
  1223. c = '\n';
  1224. } else if (c == '\n' && I_INLCR(tty))
  1225. c = '\r';
  1226. if (ldata->icanon && n_tty_receive_char_canon(tty, c))
  1227. return;
  1228. if (L_ECHO(tty)) {
  1229. finish_erasing(ldata);
  1230. if (c == '\n')
  1231. echo_char_raw('\n', ldata);
  1232. else {
  1233. /* Record the column of first canon char. */
  1234. if (ldata->canon_head == ldata->read_head)
  1235. echo_set_canon_col(ldata);
  1236. echo_char(c, tty);
  1237. }
  1238. commit_echoes(tty);
  1239. }
  1240. /* PARMRK doubling check */
  1241. if (c == '\377' && I_PARMRK(tty))
  1242. put_tty_queue(c, ldata);
  1243. put_tty_queue(c, ldata);
  1244. }
  1245. /**
  1246. * n_tty_receive_char - perform processing
  1247. * @tty: terminal device
  1248. * @c: character
  1249. *
  1250. * Process an individual character of input received from the driver. This is
  1251. * serialized with respect to itself by the rules for the driver above.
  1252. *
  1253. * Locking: n_tty_receive_buf()/producer path:
  1254. * caller holds non-exclusive %termios_rwsem
  1255. * publishes canon_head if canonical mode is active
  1256. */
  1257. static void n_tty_receive_char(struct tty_struct *tty, u8 c)
  1258. {
  1259. struct n_tty_data *ldata = tty->disc_data;
  1260. if (tty->flow.stopped && !tty->flow.tco_stopped && I_IXON(tty) && I_IXANY(tty)) {
  1261. start_tty(tty);
  1262. process_echoes(tty);
  1263. }
  1264. if (L_ECHO(tty)) {
  1265. finish_erasing(ldata);
  1266. /* Record the column of first canon char. */
  1267. if (ldata->canon_head == ldata->read_head)
  1268. echo_set_canon_col(ldata);
  1269. echo_char(c, tty);
  1270. commit_echoes(tty);
  1271. }
  1272. /* PARMRK doubling check */
  1273. if (c == '\377' && I_PARMRK(tty))
  1274. put_tty_queue(c, ldata);
  1275. put_tty_queue(c, ldata);
  1276. }
  1277. static void n_tty_receive_char_closing(struct tty_struct *tty, u8 c,
  1278. bool lookahead_done)
  1279. {
  1280. if (I_ISTRIP(tty))
  1281. c &= 0x7f;
  1282. if (I_IUCLC(tty) && L_IEXTEN(tty))
  1283. c = tolower(c);
  1284. if (I_IXON(tty)) {
  1285. if (!n_tty_receive_char_flow_ctrl(tty, c, lookahead_done) &&
  1286. tty->flow.stopped && !tty->flow.tco_stopped && I_IXANY(tty) &&
  1287. c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) &&
  1288. c != SUSP_CHAR(tty)) {
  1289. start_tty(tty);
  1290. process_echoes(tty);
  1291. }
  1292. }
  1293. }
  1294. static void
  1295. n_tty_receive_char_flagged(struct tty_struct *tty, u8 c, u8 flag)
  1296. {
  1297. switch (flag) {
  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, c);
  1304. break;
  1305. case TTY_OVERRUN:
  1306. n_tty_receive_overrun(tty);
  1307. break;
  1308. default:
  1309. tty_err(tty, "unknown flag %u\n", flag);
  1310. break;
  1311. }
  1312. }
  1313. static void
  1314. n_tty_receive_char_lnext(struct tty_struct *tty, u8 c, u8 flag)
  1315. {
  1316. struct n_tty_data *ldata = tty->disc_data;
  1317. ldata->lnext = 0;
  1318. if (likely(flag == TTY_NORMAL)) {
  1319. if (I_ISTRIP(tty))
  1320. c &= 0x7f;
  1321. if (I_IUCLC(tty) && L_IEXTEN(tty))
  1322. c = tolower(c);
  1323. n_tty_receive_char(tty, c);
  1324. } else
  1325. n_tty_receive_char_flagged(tty, c, flag);
  1326. }
  1327. /* Caller must ensure count > 0 */
  1328. static void n_tty_lookahead_flow_ctrl(struct tty_struct *tty, const u8 *cp,
  1329. const u8 *fp, size_t count)
  1330. {
  1331. struct n_tty_data *ldata = tty->disc_data;
  1332. u8 flag = TTY_NORMAL;
  1333. ldata->lookahead_count += count;
  1334. if (!I_IXON(tty))
  1335. return;
  1336. while (count--) {
  1337. if (fp)
  1338. flag = *fp++;
  1339. if (likely(flag == TTY_NORMAL))
  1340. n_tty_receive_char_flow_ctrl(tty, *cp, false);
  1341. cp++;
  1342. }
  1343. }
  1344. static void
  1345. n_tty_receive_buf_real_raw(const struct tty_struct *tty, const u8 *cp,
  1346. size_t count)
  1347. {
  1348. struct n_tty_data *ldata = tty->disc_data;
  1349. /* handle buffer wrap-around by a loop */
  1350. for (unsigned int i = 0; i < 2; i++) {
  1351. size_t head = MASK(ldata->read_head);
  1352. size_t n = min(count, N_TTY_BUF_SIZE - head);
  1353. memcpy(read_buf_addr(ldata, head), cp, n);
  1354. ldata->read_head += n;
  1355. cp += n;
  1356. count -= n;
  1357. }
  1358. }
  1359. static void
  1360. n_tty_receive_buf_raw(struct tty_struct *tty, const u8 *cp, const u8 *fp,
  1361. size_t count)
  1362. {
  1363. struct n_tty_data *ldata = tty->disc_data;
  1364. u8 flag = TTY_NORMAL;
  1365. while (count--) {
  1366. if (fp)
  1367. flag = *fp++;
  1368. if (likely(flag == TTY_NORMAL))
  1369. put_tty_queue(*cp++, ldata);
  1370. else
  1371. n_tty_receive_char_flagged(tty, *cp++, flag);
  1372. }
  1373. }
  1374. static void
  1375. n_tty_receive_buf_closing(struct tty_struct *tty, const u8 *cp, const u8 *fp,
  1376. size_t count, bool lookahead_done)
  1377. {
  1378. u8 flag = TTY_NORMAL;
  1379. while (count--) {
  1380. if (fp)
  1381. flag = *fp++;
  1382. if (likely(flag == TTY_NORMAL))
  1383. n_tty_receive_char_closing(tty, *cp++, lookahead_done);
  1384. }
  1385. }
  1386. static void n_tty_receive_buf_standard(struct tty_struct *tty, const u8 *cp,
  1387. const u8 *fp, size_t count,
  1388. bool lookahead_done)
  1389. {
  1390. struct n_tty_data *ldata = tty->disc_data;
  1391. u8 flag = TTY_NORMAL;
  1392. while (count--) {
  1393. u8 c = *cp++;
  1394. if (fp)
  1395. flag = *fp++;
  1396. if (ldata->lnext) {
  1397. n_tty_receive_char_lnext(tty, c, flag);
  1398. continue;
  1399. }
  1400. if (unlikely(flag != TTY_NORMAL)) {
  1401. n_tty_receive_char_flagged(tty, c, flag);
  1402. continue;
  1403. }
  1404. if (I_ISTRIP(tty))
  1405. c &= 0x7f;
  1406. if (I_IUCLC(tty) && L_IEXTEN(tty))
  1407. c = tolower(c);
  1408. if (L_EXTPROC(tty)) {
  1409. put_tty_queue(c, ldata);
  1410. continue;
  1411. }
  1412. if (test_bit(c, ldata->char_map))
  1413. n_tty_receive_char_special(tty, c, lookahead_done);
  1414. else
  1415. n_tty_receive_char(tty, c);
  1416. }
  1417. }
  1418. static void __receive_buf(struct tty_struct *tty, const u8 *cp, const u8 *fp,
  1419. size_t count)
  1420. {
  1421. struct n_tty_data *ldata = tty->disc_data;
  1422. bool preops = I_ISTRIP(tty) || (I_IUCLC(tty) && L_IEXTEN(tty));
  1423. size_t la_count = min(ldata->lookahead_count, count);
  1424. if (ldata->real_raw)
  1425. n_tty_receive_buf_real_raw(tty, cp, count);
  1426. else if (ldata->raw || (L_EXTPROC(tty) && !preops))
  1427. n_tty_receive_buf_raw(tty, cp, fp, count);
  1428. else if (tty->closing && !L_EXTPROC(tty)) {
  1429. if (la_count > 0) {
  1430. n_tty_receive_buf_closing(tty, cp, fp, la_count, true);
  1431. cp += la_count;
  1432. if (fp)
  1433. fp += la_count;
  1434. count -= la_count;
  1435. }
  1436. if (count > 0)
  1437. n_tty_receive_buf_closing(tty, cp, fp, count, false);
  1438. } else {
  1439. if (la_count > 0) {
  1440. n_tty_receive_buf_standard(tty, cp, fp, la_count, true);
  1441. cp += la_count;
  1442. if (fp)
  1443. fp += la_count;
  1444. count -= la_count;
  1445. }
  1446. if (count > 0)
  1447. n_tty_receive_buf_standard(tty, cp, fp, count, false);
  1448. flush_echoes(tty);
  1449. if (tty->ops->flush_chars)
  1450. tty->ops->flush_chars(tty);
  1451. }
  1452. ldata->lookahead_count -= la_count;
  1453. if (ldata->icanon && !L_EXTPROC(tty))
  1454. return;
  1455. /* publish read_head to consumer */
  1456. smp_store_release(&ldata->commit_head, ldata->read_head);
  1457. if (read_cnt(ldata)) {
  1458. kill_fasync(&tty->fasync, SIGIO, POLL_IN);
  1459. wake_up_interruptible_poll(&tty->read_wait, EPOLLIN | EPOLLRDNORM);
  1460. }
  1461. }
  1462. /**
  1463. * n_tty_receive_buf_common - process input
  1464. * @tty: device to receive input
  1465. * @cp: input chars
  1466. * @fp: flags for each char (if %NULL, all chars are %TTY_NORMAL)
  1467. * @count: number of input chars in @cp
  1468. * @flow: enable flow control
  1469. *
  1470. * Called by the terminal driver when a block of characters has been received.
  1471. * This function must be called from soft contexts not from interrupt context.
  1472. * The driver is responsible for making calls one at a time and in order (or
  1473. * using flush_to_ldisc()).
  1474. *
  1475. * Returns: the # of input chars from @cp which were processed.
  1476. *
  1477. * In canonical mode, the maximum line length is 4096 chars (including the line
  1478. * termination char); lines longer than 4096 chars are truncated. After 4095
  1479. * chars, input data is still processed but not stored. Overflow processing
  1480. * ensures the tty can always receive more input until at least one line can be
  1481. * read.
  1482. *
  1483. * In non-canonical mode, the read buffer will only accept 4095 chars; this
  1484. * provides the necessary space for a newline char if the input mode is
  1485. * switched to canonical.
  1486. *
  1487. * Note it is possible for the read buffer to _contain_ 4096 chars in
  1488. * non-canonical mode: the read buffer could already contain the maximum canon
  1489. * line of 4096 chars when the mode is switched to non-canonical.
  1490. *
  1491. * Locking: n_tty_receive_buf()/producer path:
  1492. * claims non-exclusive %termios_rwsem
  1493. * publishes commit_head or canon_head
  1494. */
  1495. static size_t
  1496. n_tty_receive_buf_common(struct tty_struct *tty, const u8 *cp, const u8 *fp,
  1497. size_t count, bool flow)
  1498. {
  1499. struct n_tty_data *ldata = tty->disc_data;
  1500. size_t n, rcvd = 0;
  1501. int room, overflow;
  1502. down_read(&tty->termios_rwsem);
  1503. do {
  1504. /*
  1505. * When PARMRK is set, each input char may take up to 3 chars
  1506. * in the read buf; reduce the buffer space avail by 3x
  1507. *
  1508. * If we are doing input canonicalization, and there are no
  1509. * pending newlines, let characters through without limit, so
  1510. * that erase characters will be handled. Other excess
  1511. * characters will be beeped.
  1512. *
  1513. * paired with store in *_copy_from_read_buf() -- guarantees
  1514. * the consumer has loaded the data in read_buf up to the new
  1515. * read_tail (so this producer will not overwrite unread data)
  1516. */
  1517. size_t tail = smp_load_acquire(&ldata->read_tail);
  1518. room = N_TTY_BUF_SIZE - (ldata->read_head - tail);
  1519. if (I_PARMRK(tty))
  1520. room = DIV_ROUND_UP(room, 3);
  1521. room--;
  1522. if (room <= 0) {
  1523. overflow = ldata->icanon && ldata->canon_head == tail;
  1524. if (overflow && room < 0)
  1525. ldata->read_head--;
  1526. room = overflow;
  1527. WRITE_ONCE(ldata->no_room, flow && !room);
  1528. } else
  1529. overflow = 0;
  1530. n = min_t(size_t, count, room);
  1531. if (!n)
  1532. break;
  1533. /* ignore parity errors if handling overflow */
  1534. if (!overflow || !fp || *fp != TTY_PARITY)
  1535. __receive_buf(tty, cp, fp, n);
  1536. cp += n;
  1537. if (fp)
  1538. fp += n;
  1539. count -= n;
  1540. rcvd += n;
  1541. } while (!test_bit(TTY_LDISC_CHANGING, &tty->flags));
  1542. tty->receive_room = room;
  1543. /* Unthrottle if handling overflow on pty */
  1544. if (tty->driver->type == TTY_DRIVER_TYPE_PTY) {
  1545. if (overflow) {
  1546. tty_set_flow_change(tty, TTY_UNTHROTTLE_SAFE);
  1547. tty_unthrottle_safe(tty);
  1548. __tty_set_flow_change(tty, 0);
  1549. }
  1550. } else
  1551. n_tty_check_throttle(tty);
  1552. if (unlikely(ldata->no_room)) {
  1553. /*
  1554. * Barrier here is to ensure to read the latest read_tail in
  1555. * chars_in_buffer() and to make sure that read_tail is not loaded
  1556. * before ldata->no_room is set.
  1557. */
  1558. smp_mb();
  1559. if (!chars_in_buffer(tty))
  1560. n_tty_kick_worker(tty);
  1561. }
  1562. up_read(&tty->termios_rwsem);
  1563. return rcvd;
  1564. }
  1565. static void n_tty_receive_buf(struct tty_struct *tty, const u8 *cp,
  1566. const u8 *fp, size_t count)
  1567. {
  1568. n_tty_receive_buf_common(tty, cp, fp, count, false);
  1569. }
  1570. static size_t n_tty_receive_buf2(struct tty_struct *tty, const u8 *cp,
  1571. const u8 *fp, size_t count)
  1572. {
  1573. return n_tty_receive_buf_common(tty, cp, fp, count, true);
  1574. }
  1575. /**
  1576. * n_tty_set_termios - termios data changed
  1577. * @tty: terminal
  1578. * @old: previous data
  1579. *
  1580. * Called by the tty layer when the user changes termios flags so that the line
  1581. * discipline can plan ahead. This function cannot sleep and is protected from
  1582. * re-entry by the tty layer. The user is guaranteed that this function will
  1583. * not be re-entered or in progress when the ldisc is closed.
  1584. *
  1585. * Locking: Caller holds @tty->termios_rwsem
  1586. */
  1587. static void n_tty_set_termios(struct tty_struct *tty, const struct ktermios *old)
  1588. {
  1589. struct n_tty_data *ldata = tty->disc_data;
  1590. if (!old || (old->c_lflag ^ tty->termios.c_lflag) & (ICANON | EXTPROC)) {
  1591. bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
  1592. ldata->line_start = ldata->read_tail;
  1593. if (!L_ICANON(tty) || !read_cnt(ldata)) {
  1594. ldata->canon_head = ldata->read_tail;
  1595. ldata->push = 0;
  1596. } else {
  1597. set_bit(MASK(ldata->read_head - 1), ldata->read_flags);
  1598. ldata->canon_head = ldata->read_head;
  1599. ldata->push = 1;
  1600. }
  1601. ldata->commit_head = ldata->read_head;
  1602. ldata->erasing = 0;
  1603. ldata->lnext = 0;
  1604. }
  1605. ldata->icanon = (L_ICANON(tty) != 0);
  1606. if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
  1607. I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
  1608. I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
  1609. I_PARMRK(tty)) {
  1610. bitmap_zero(ldata->char_map, 256);
  1611. if (I_IGNCR(tty) || I_ICRNL(tty))
  1612. set_bit('\r', ldata->char_map);
  1613. if (I_INLCR(tty))
  1614. set_bit('\n', ldata->char_map);
  1615. if (L_ICANON(tty)) {
  1616. set_bit(ERASE_CHAR(tty), ldata->char_map);
  1617. set_bit(KILL_CHAR(tty), ldata->char_map);
  1618. set_bit(EOF_CHAR(tty), ldata->char_map);
  1619. set_bit('\n', ldata->char_map);
  1620. set_bit(EOL_CHAR(tty), ldata->char_map);
  1621. if (L_IEXTEN(tty)) {
  1622. set_bit(WERASE_CHAR(tty), ldata->char_map);
  1623. set_bit(LNEXT_CHAR(tty), ldata->char_map);
  1624. set_bit(EOL2_CHAR(tty), ldata->char_map);
  1625. if (L_ECHO(tty))
  1626. set_bit(REPRINT_CHAR(tty),
  1627. ldata->char_map);
  1628. }
  1629. }
  1630. if (I_IXON(tty)) {
  1631. set_bit(START_CHAR(tty), ldata->char_map);
  1632. set_bit(STOP_CHAR(tty), ldata->char_map);
  1633. }
  1634. if (L_ISIG(tty)) {
  1635. set_bit(INTR_CHAR(tty), ldata->char_map);
  1636. set_bit(QUIT_CHAR(tty), ldata->char_map);
  1637. set_bit(SUSP_CHAR(tty), ldata->char_map);
  1638. }
  1639. clear_bit(__DISABLED_CHAR, ldata->char_map);
  1640. ldata->raw = 0;
  1641. ldata->real_raw = 0;
  1642. } else {
  1643. ldata->raw = 1;
  1644. if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
  1645. (I_IGNPAR(tty) || !I_INPCK(tty)) &&
  1646. (tty->driver->flags & TTY_DRIVER_REAL_RAW))
  1647. ldata->real_raw = 1;
  1648. else
  1649. ldata->real_raw = 0;
  1650. }
  1651. /*
  1652. * Fix tty hang when I_IXON(tty) is cleared, but the tty
  1653. * been stopped by STOP_CHAR(tty) before it.
  1654. */
  1655. if (!I_IXON(tty) && old && (old->c_iflag & IXON) && !tty->flow.tco_stopped) {
  1656. start_tty(tty);
  1657. process_echoes(tty);
  1658. }
  1659. /* The termios change make the tty ready for I/O */
  1660. wake_up_interruptible(&tty->write_wait);
  1661. wake_up_interruptible(&tty->read_wait);
  1662. }
  1663. /**
  1664. * n_tty_close - close the ldisc for this tty
  1665. * @tty: device
  1666. *
  1667. * Called from the terminal layer when this line discipline is being shut down,
  1668. * either because of a close or becsuse of a discipline change. The function
  1669. * will not be called while other ldisc methods are in progress.
  1670. */
  1671. static void n_tty_close(struct tty_struct *tty)
  1672. {
  1673. struct n_tty_data *ldata = tty->disc_data;
  1674. if (tty->link)
  1675. n_tty_packet_mode_flush(tty);
  1676. down_write(&tty->termios_rwsem);
  1677. vfree(ldata);
  1678. tty->disc_data = NULL;
  1679. up_write(&tty->termios_rwsem);
  1680. }
  1681. /**
  1682. * n_tty_open - open an ldisc
  1683. * @tty: terminal to open
  1684. *
  1685. * Called when this line discipline is being attached to the terminal device.
  1686. * Can sleep. Called serialized so that no other events will occur in parallel.
  1687. * No further open will occur until a close.
  1688. */
  1689. static int n_tty_open(struct tty_struct *tty)
  1690. {
  1691. struct n_tty_data *ldata;
  1692. /* Currently a malloc failure here can panic */
  1693. ldata = vzalloc(sizeof(*ldata));
  1694. if (!ldata)
  1695. return -ENOMEM;
  1696. ldata->overrun_time = jiffies;
  1697. mutex_init(&ldata->atomic_read_lock);
  1698. mutex_init(&ldata->output_lock);
  1699. tty->disc_data = ldata;
  1700. tty->closing = 0;
  1701. /* indicate buffer work may resume */
  1702. clear_bit(TTY_LDISC_HALTED, &tty->flags);
  1703. n_tty_set_termios(tty, NULL);
  1704. tty_unthrottle(tty);
  1705. return 0;
  1706. }
  1707. static inline int input_available_p(const struct tty_struct *tty, int poll)
  1708. {
  1709. const struct n_tty_data *ldata = tty->disc_data;
  1710. int amt = poll && !TIME_CHAR(tty) && MIN_CHAR(tty) ? MIN_CHAR(tty) : 1;
  1711. if (ldata->icanon && !L_EXTPROC(tty))
  1712. return ldata->canon_head != ldata->read_tail;
  1713. else
  1714. return ldata->commit_head - ldata->read_tail >= amt;
  1715. }
  1716. /**
  1717. * copy_from_read_buf - copy read data directly
  1718. * @tty: terminal device
  1719. * @kbp: data
  1720. * @nr: size of data
  1721. *
  1722. * Helper function to speed up n_tty_read(). It is only called when %ICANON is
  1723. * off; it copies characters straight from the tty queue.
  1724. *
  1725. * Returns: true if it successfully copied data, but there is still more data
  1726. * to be had.
  1727. *
  1728. * Locking:
  1729. * * called under the @ldata->atomic_read_lock sem
  1730. * * n_tty_read()/consumer path:
  1731. * caller holds non-exclusive %termios_rwsem;
  1732. * read_tail published
  1733. */
  1734. static bool copy_from_read_buf(const struct tty_struct *tty, u8 **kbp,
  1735. size_t *nr)
  1736. {
  1737. struct n_tty_data *ldata = tty->disc_data;
  1738. size_t n;
  1739. bool is_eof;
  1740. size_t head = smp_load_acquire(&ldata->commit_head);
  1741. size_t tail = MASK(ldata->read_tail);
  1742. n = min3(head - ldata->read_tail, N_TTY_BUF_SIZE - tail, *nr);
  1743. if (!n)
  1744. return false;
  1745. u8 *from = read_buf_addr(ldata, tail);
  1746. memcpy(*kbp, from, n);
  1747. is_eof = n == 1 && *from == EOF_CHAR(tty);
  1748. tty_audit_add_data(tty, from, n);
  1749. zero_buffer(tty, from, n);
  1750. smp_store_release(&ldata->read_tail, ldata->read_tail + n);
  1751. /* Turn single EOF into zero-length read */
  1752. if (L_EXTPROC(tty) && ldata->icanon && is_eof &&
  1753. head == ldata->read_tail)
  1754. return false;
  1755. *kbp += n;
  1756. *nr -= n;
  1757. /* If we have more to copy, let the caller know */
  1758. return head != ldata->read_tail;
  1759. }
  1760. /**
  1761. * canon_copy_from_read_buf - copy read data in canonical mode
  1762. * @tty: terminal device
  1763. * @kbp: data
  1764. * @nr: size of data
  1765. *
  1766. * Helper function for n_tty_read(). It is only called when %ICANON is on; it
  1767. * copies one line of input up to and including the line-delimiting character
  1768. * into the result buffer.
  1769. *
  1770. * Note: When termios is changed from non-canonical to canonical mode and the
  1771. * read buffer contains data, n_tty_set_termios() simulates an EOF push (as if
  1772. * C-d were input) _without_ the %DISABLED_CHAR in the buffer. This causes data
  1773. * already processed as input to be immediately available as input although a
  1774. * newline has not been received.
  1775. *
  1776. * Locking:
  1777. * * called under the %atomic_read_lock mutex
  1778. * * n_tty_read()/consumer path:
  1779. * caller holds non-exclusive %termios_rwsem;
  1780. * read_tail published
  1781. */
  1782. static bool canon_copy_from_read_buf(const struct tty_struct *tty, u8 **kbp,
  1783. size_t *nr)
  1784. {
  1785. struct n_tty_data *ldata = tty->disc_data;
  1786. size_t n, size, more, c;
  1787. size_t eol;
  1788. size_t tail, canon_head;
  1789. int found = 0;
  1790. /* N.B. avoid overrun if nr == 0 */
  1791. if (!*nr)
  1792. return false;
  1793. canon_head = smp_load_acquire(&ldata->canon_head);
  1794. n = min(*nr, canon_head - ldata->read_tail);
  1795. tail = MASK(ldata->read_tail);
  1796. size = min_t(size_t, tail + n, N_TTY_BUF_SIZE);
  1797. n_tty_trace("%s: nr:%zu tail:%zu n:%zu size:%zu\n",
  1798. __func__, *nr, tail, n, size);
  1799. eol = find_next_bit(ldata->read_flags, size, tail);
  1800. more = n - (size - tail);
  1801. if (eol == N_TTY_BUF_SIZE && more) {
  1802. /* scan wrapped without finding set bit */
  1803. eol = find_first_bit(ldata->read_flags, more);
  1804. found = eol != more;
  1805. } else
  1806. found = eol != size;
  1807. n = eol - tail;
  1808. if (n > N_TTY_BUF_SIZE)
  1809. n += N_TTY_BUF_SIZE;
  1810. c = n + found;
  1811. if (!found || read_buf(ldata, eol) != __DISABLED_CHAR)
  1812. n = c;
  1813. n_tty_trace("%s: eol:%zu found:%d n:%zu c:%zu tail:%zu more:%zu\n",
  1814. __func__, eol, found, n, c, tail, more);
  1815. tty_copy(tty, *kbp, tail, n);
  1816. *kbp += n;
  1817. *nr -= n;
  1818. if (found)
  1819. clear_bit(eol, ldata->read_flags);
  1820. smp_store_release(&ldata->read_tail, ldata->read_tail + c);
  1821. if (found) {
  1822. if (!ldata->push)
  1823. ldata->line_start = ldata->read_tail;
  1824. else
  1825. ldata->push = 0;
  1826. tty_audit_push();
  1827. return false;
  1828. }
  1829. /* No EOL found - do a continuation retry if there is more data */
  1830. return ldata->read_tail != canon_head;
  1831. }
  1832. /*
  1833. * If we finished a read at the exact location of an
  1834. * EOF (special EOL character that's a __DISABLED_CHAR)
  1835. * in the stream, silently eat the EOF.
  1836. */
  1837. static void canon_skip_eof(struct n_tty_data *ldata)
  1838. {
  1839. size_t tail, canon_head;
  1840. canon_head = smp_load_acquire(&ldata->canon_head);
  1841. tail = ldata->read_tail;
  1842. // No data?
  1843. if (tail == canon_head)
  1844. return;
  1845. // See if the tail position is EOF in the circular buffer
  1846. tail &= (N_TTY_BUF_SIZE - 1);
  1847. if (!test_bit(tail, ldata->read_flags))
  1848. return;
  1849. if (read_buf(ldata, tail) != __DISABLED_CHAR)
  1850. return;
  1851. // Clear the EOL bit, skip the EOF char.
  1852. clear_bit(tail, ldata->read_flags);
  1853. smp_store_release(&ldata->read_tail, ldata->read_tail + 1);
  1854. }
  1855. /**
  1856. * job_control - check job control
  1857. * @tty: tty
  1858. * @file: file handle
  1859. *
  1860. * Perform job control management checks on this @file/@tty descriptor and if
  1861. * appropriate send any needed signals and return a negative error code if
  1862. * action should be taken.
  1863. *
  1864. * Locking:
  1865. * * redirected write test is safe
  1866. * * current->signal->tty check is safe
  1867. * * ctrl.lock to safely reference @tty->ctrl.pgrp
  1868. */
  1869. static int job_control(struct tty_struct *tty, struct file *file)
  1870. {
  1871. /* Job control check -- must be done at start and after
  1872. every sleep (POSIX.1 7.1.1.4). */
  1873. /* NOTE: not yet done after every sleep pending a thorough
  1874. check of the logic of this change. -- jlc */
  1875. /* don't stop on /dev/console */
  1876. if (file->f_op->write_iter == redirected_tty_write)
  1877. return 0;
  1878. return __tty_check_change(tty, SIGTTIN);
  1879. }
  1880. /**
  1881. * n_tty_read - read function for tty
  1882. * @tty: tty device
  1883. * @file: file object
  1884. * @kbuf: kernelspace buffer pointer
  1885. * @nr: size of I/O
  1886. * @cookie: if non-%NULL, this is a continuation read
  1887. * @offset: where to continue reading from (unused in n_tty)
  1888. *
  1889. * Perform reads for the line discipline. We are guaranteed that the line
  1890. * discipline will not be closed under us but we may get multiple parallel
  1891. * readers and must handle this ourselves. We may also get a hangup. Always
  1892. * called in user context, may sleep.
  1893. *
  1894. * This code must be sure never to sleep through a hangup.
  1895. *
  1896. * Locking: n_tty_read()/consumer path:
  1897. * claims non-exclusive termios_rwsem;
  1898. * publishes read_tail
  1899. */
  1900. static ssize_t n_tty_read(struct tty_struct *tty, struct file *file, u8 *kbuf,
  1901. size_t nr, void **cookie, unsigned long offset)
  1902. {
  1903. struct n_tty_data *ldata = tty->disc_data;
  1904. u8 *kb = kbuf;
  1905. DEFINE_WAIT_FUNC(wait, woken_wake_function);
  1906. int minimum, time;
  1907. ssize_t retval;
  1908. long timeout;
  1909. bool packet;
  1910. size_t old_tail;
  1911. /*
  1912. * Is this a continuation of a read started earler?
  1913. *
  1914. * If so, we still hold the atomic_read_lock and the
  1915. * termios_rwsem, and can just continue to copy data.
  1916. */
  1917. if (*cookie) {
  1918. if (ldata->icanon && !L_EXTPROC(tty)) {
  1919. /*
  1920. * If we have filled the user buffer, see
  1921. * if we should skip an EOF character before
  1922. * releasing the lock and returning done.
  1923. */
  1924. if (!nr)
  1925. canon_skip_eof(ldata);
  1926. else if (canon_copy_from_read_buf(tty, &kb, &nr))
  1927. return kb - kbuf;
  1928. } else {
  1929. if (copy_from_read_buf(tty, &kb, &nr))
  1930. return kb - kbuf;
  1931. }
  1932. /* No more data - release locks and stop retries */
  1933. n_tty_kick_worker(tty);
  1934. n_tty_check_unthrottle(tty);
  1935. up_read(&tty->termios_rwsem);
  1936. mutex_unlock(&ldata->atomic_read_lock);
  1937. *cookie = NULL;
  1938. return kb - kbuf;
  1939. }
  1940. retval = job_control(tty, file);
  1941. if (retval < 0)
  1942. return retval;
  1943. /*
  1944. * Internal serialization of reads.
  1945. */
  1946. if (file->f_flags & O_NONBLOCK) {
  1947. if (!mutex_trylock(&ldata->atomic_read_lock))
  1948. return -EAGAIN;
  1949. } else {
  1950. if (mutex_lock_interruptible(&ldata->atomic_read_lock))
  1951. return -ERESTARTSYS;
  1952. }
  1953. down_read(&tty->termios_rwsem);
  1954. minimum = time = 0;
  1955. timeout = MAX_SCHEDULE_TIMEOUT;
  1956. if (!ldata->icanon) {
  1957. minimum = MIN_CHAR(tty);
  1958. if (minimum) {
  1959. time = (HZ / 10) * TIME_CHAR(tty);
  1960. } else {
  1961. timeout = (HZ / 10) * TIME_CHAR(tty);
  1962. minimum = 1;
  1963. }
  1964. }
  1965. packet = tty->ctrl.packet;
  1966. old_tail = ldata->read_tail;
  1967. add_wait_queue(&tty->read_wait, &wait);
  1968. while (nr) {
  1969. /* First test for status change. */
  1970. if (packet && tty->link->ctrl.pktstatus) {
  1971. u8 cs;
  1972. if (kb != kbuf)
  1973. break;
  1974. spin_lock_irq(&tty->link->ctrl.lock);
  1975. cs = tty->link->ctrl.pktstatus;
  1976. tty->link->ctrl.pktstatus = 0;
  1977. spin_unlock_irq(&tty->link->ctrl.lock);
  1978. *kb++ = cs;
  1979. nr--;
  1980. break;
  1981. }
  1982. if (!input_available_p(tty, 0)) {
  1983. up_read(&tty->termios_rwsem);
  1984. tty_buffer_flush_work(tty->port);
  1985. down_read(&tty->termios_rwsem);
  1986. if (!input_available_p(tty, 0)) {
  1987. if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
  1988. retval = -EIO;
  1989. break;
  1990. }
  1991. if (tty_hung_up_p(file))
  1992. break;
  1993. /*
  1994. * Abort readers for ttys which never actually
  1995. * get hung up. See __tty_hangup().
  1996. */
  1997. if (test_bit(TTY_HUPPING, &tty->flags))
  1998. break;
  1999. if (!timeout)
  2000. break;
  2001. if (tty_io_nonblock(tty, file)) {
  2002. retval = -EAGAIN;
  2003. break;
  2004. }
  2005. if (signal_pending(current)) {
  2006. retval = -ERESTARTSYS;
  2007. break;
  2008. }
  2009. up_read(&tty->termios_rwsem);
  2010. timeout = wait_woken(&wait, TASK_INTERRUPTIBLE,
  2011. timeout);
  2012. down_read(&tty->termios_rwsem);
  2013. continue;
  2014. }
  2015. }
  2016. if (ldata->icanon && !L_EXTPROC(tty)) {
  2017. if (canon_copy_from_read_buf(tty, &kb, &nr))
  2018. goto more_to_be_read;
  2019. } else {
  2020. /* Deal with packet mode. */
  2021. if (packet && kb == kbuf) {
  2022. *kb++ = TIOCPKT_DATA;
  2023. nr--;
  2024. }
  2025. /*
  2026. * Copy data, and if there is more to be had
  2027. * and we have nothing more to wait for, then
  2028. * let's mark us for retries.
  2029. *
  2030. * NOTE! We return here with both the termios_sem
  2031. * and atomic_read_lock still held, the retries
  2032. * will release them when done.
  2033. */
  2034. if (copy_from_read_buf(tty, &kb, &nr) && kb - kbuf >= minimum) {
  2035. more_to_be_read:
  2036. remove_wait_queue(&tty->read_wait, &wait);
  2037. *cookie = cookie;
  2038. return kb - kbuf;
  2039. }
  2040. }
  2041. n_tty_check_unthrottle(tty);
  2042. if (kb - kbuf >= minimum)
  2043. break;
  2044. if (time)
  2045. timeout = time;
  2046. }
  2047. if (old_tail != ldata->read_tail) {
  2048. /*
  2049. * Make sure no_room is not read in n_tty_kick_worker()
  2050. * before setting ldata->read_tail in copy_from_read_buf().
  2051. */
  2052. smp_mb();
  2053. n_tty_kick_worker(tty);
  2054. }
  2055. up_read(&tty->termios_rwsem);
  2056. remove_wait_queue(&tty->read_wait, &wait);
  2057. mutex_unlock(&ldata->atomic_read_lock);
  2058. if (kb - kbuf)
  2059. retval = kb - kbuf;
  2060. return retval;
  2061. }
  2062. /**
  2063. * n_tty_write - write function for tty
  2064. * @tty: tty device
  2065. * @file: file object
  2066. * @buf: userspace buffer pointer
  2067. * @nr: size of I/O
  2068. *
  2069. * Write function of the terminal device. This is serialized with respect to
  2070. * other write callers but not to termios changes, reads and other such events.
  2071. * Since the receive code will echo characters, thus calling driver write
  2072. * methods, the %output_lock is used in the output processing functions called
  2073. * here as well as in the echo processing function to protect the column state
  2074. * and space left in the buffer.
  2075. *
  2076. * This code must be sure never to sleep through a hangup.
  2077. *
  2078. * Locking: output_lock to protect column state and space left
  2079. * (note that the process_output*() functions take this lock themselves)
  2080. */
  2081. static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
  2082. const u8 *buf, size_t nr)
  2083. {
  2084. const u8 *b = buf;
  2085. DEFINE_WAIT_FUNC(wait, woken_wake_function);
  2086. ssize_t num, retval = 0;
  2087. /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
  2088. if (L_TOSTOP(tty) && file->f_op->write_iter != redirected_tty_write) {
  2089. retval = tty_check_change(tty);
  2090. if (retval)
  2091. return retval;
  2092. }
  2093. down_read(&tty->termios_rwsem);
  2094. /* Write out any echoed characters that are still pending */
  2095. process_echoes(tty);
  2096. add_wait_queue(&tty->write_wait, &wait);
  2097. while (1) {
  2098. if (signal_pending(current)) {
  2099. retval = -ERESTARTSYS;
  2100. break;
  2101. }
  2102. if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
  2103. retval = -EIO;
  2104. break;
  2105. }
  2106. if (O_OPOST(tty)) {
  2107. while (nr > 0) {
  2108. num = process_output_block(tty, b, nr);
  2109. if (num < 0) {
  2110. if (num == -EAGAIN)
  2111. break;
  2112. retval = num;
  2113. goto break_out;
  2114. }
  2115. b += num;
  2116. nr -= num;
  2117. if (nr == 0)
  2118. break;
  2119. if (process_output(*b, tty) < 0)
  2120. break;
  2121. b++; nr--;
  2122. }
  2123. if (tty->ops->flush_chars)
  2124. tty->ops->flush_chars(tty);
  2125. } else {
  2126. struct n_tty_data *ldata = tty->disc_data;
  2127. while (nr > 0) {
  2128. mutex_lock(&ldata->output_lock);
  2129. num = tty->ops->write(tty, b, nr);
  2130. mutex_unlock(&ldata->output_lock);
  2131. if (num < 0) {
  2132. retval = num;
  2133. goto break_out;
  2134. }
  2135. if (!num)
  2136. break;
  2137. b += num;
  2138. nr -= num;
  2139. }
  2140. }
  2141. if (!nr)
  2142. break;
  2143. if (tty_io_nonblock(tty, file)) {
  2144. retval = -EAGAIN;
  2145. break;
  2146. }
  2147. up_read(&tty->termios_rwsem);
  2148. wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
  2149. down_read(&tty->termios_rwsem);
  2150. }
  2151. break_out:
  2152. remove_wait_queue(&tty->write_wait, &wait);
  2153. if (nr && tty->fasync)
  2154. set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  2155. up_read(&tty->termios_rwsem);
  2156. return (b - buf) ? b - buf : retval;
  2157. }
  2158. /**
  2159. * n_tty_poll - poll method for N_TTY
  2160. * @tty: terminal device
  2161. * @file: file accessing it
  2162. * @wait: poll table
  2163. *
  2164. * Called when the line discipline is asked to poll() for data or for special
  2165. * events. This code is not serialized with respect to other events save
  2166. * open/close.
  2167. *
  2168. * This code must be sure never to sleep through a hangup.
  2169. *
  2170. * Locking: called without the kernel lock held -- fine.
  2171. */
  2172. static __poll_t n_tty_poll(struct tty_struct *tty, struct file *file,
  2173. poll_table *wait)
  2174. {
  2175. __poll_t mask = 0;
  2176. poll_wait(file, &tty->read_wait, wait);
  2177. poll_wait(file, &tty->write_wait, wait);
  2178. if (input_available_p(tty, 1))
  2179. mask |= EPOLLIN | EPOLLRDNORM;
  2180. else {
  2181. tty_buffer_flush_work(tty->port);
  2182. if (input_available_p(tty, 1))
  2183. mask |= EPOLLIN | EPOLLRDNORM;
  2184. }
  2185. if (tty->ctrl.packet && tty->link->ctrl.pktstatus)
  2186. mask |= EPOLLPRI | EPOLLIN | EPOLLRDNORM;
  2187. if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
  2188. mask |= EPOLLHUP;
  2189. if (tty_hung_up_p(file))
  2190. mask |= EPOLLHUP;
  2191. if (tty->ops->write && !tty_is_writelocked(tty) &&
  2192. tty_chars_in_buffer(tty) < WAKEUP_CHARS &&
  2193. tty_write_room(tty) > 0)
  2194. mask |= EPOLLOUT | EPOLLWRNORM;
  2195. return mask;
  2196. }
  2197. static unsigned long inq_canon(struct n_tty_data *ldata)
  2198. {
  2199. size_t nr, head, tail;
  2200. if (ldata->canon_head == ldata->read_tail)
  2201. return 0;
  2202. head = ldata->canon_head;
  2203. tail = ldata->read_tail;
  2204. nr = head - tail;
  2205. /* Skip EOF-chars.. */
  2206. while (MASK(head) != MASK(tail)) {
  2207. if (test_bit(MASK(tail), ldata->read_flags) &&
  2208. read_buf(ldata, tail) == __DISABLED_CHAR)
  2209. nr--;
  2210. tail++;
  2211. }
  2212. return nr;
  2213. }
  2214. static int n_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
  2215. unsigned long arg)
  2216. {
  2217. struct n_tty_data *ldata = tty->disc_data;
  2218. unsigned int num;
  2219. switch (cmd) {
  2220. case TIOCOUTQ:
  2221. return put_user(tty_chars_in_buffer(tty), (int __user *) arg);
  2222. case TIOCINQ:
  2223. down_write(&tty->termios_rwsem);
  2224. if (L_ICANON(tty) && !L_EXTPROC(tty))
  2225. num = inq_canon(ldata);
  2226. else
  2227. num = read_cnt(ldata);
  2228. up_write(&tty->termios_rwsem);
  2229. return put_user(num, (unsigned int __user *) arg);
  2230. default:
  2231. return n_tty_ioctl_helper(tty, cmd, arg);
  2232. }
  2233. }
  2234. static struct tty_ldisc_ops n_tty_ops = {
  2235. .owner = THIS_MODULE,
  2236. .num = N_TTY,
  2237. .name = "n_tty",
  2238. .open = n_tty_open,
  2239. .close = n_tty_close,
  2240. .flush_buffer = n_tty_flush_buffer,
  2241. .read = n_tty_read,
  2242. .write = n_tty_write,
  2243. .ioctl = n_tty_ioctl,
  2244. .set_termios = n_tty_set_termios,
  2245. .poll = n_tty_poll,
  2246. .receive_buf = n_tty_receive_buf,
  2247. .write_wakeup = n_tty_write_wakeup,
  2248. .receive_buf2 = n_tty_receive_buf2,
  2249. .lookahead_buf = n_tty_lookahead_flow_ctrl,
  2250. };
  2251. /**
  2252. * n_tty_inherit_ops - inherit N_TTY methods
  2253. * @ops: struct tty_ldisc_ops where to save N_TTY methods
  2254. *
  2255. * Enables a 'subclass' line discipline to 'inherit' N_TTY methods.
  2256. */
  2257. void n_tty_inherit_ops(struct tty_ldisc_ops *ops)
  2258. {
  2259. *ops = n_tty_ops;
  2260. ops->owner = NULL;
  2261. }
  2262. EXPORT_SYMBOL_GPL(n_tty_inherit_ops);
  2263. void __init n_tty_init(void)
  2264. {
  2265. tty_register_ldisc(&n_tty_ops);
  2266. }