n_tty.c 64 KB

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