n_tty.c 64 KB

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