n_tty.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593
  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 pid *tty_pgrp = tty_get_pgrp(tty);
  983. if (tty_pgrp) {
  984. kill_pgrp(tty_pgrp, sig, 1);
  985. put_pid(tty_pgrp);
  986. }
  987. }
  988. static void isig(int sig, struct tty_struct *tty)
  989. {
  990. struct n_tty_data *ldata = tty->disc_data;
  991. if (L_NOFLSH(tty)) {
  992. /* signal only */
  993. __isig(sig, tty);
  994. } else { /* signal and flush */
  995. up_read(&tty->termios_rwsem);
  996. down_write(&tty->termios_rwsem);
  997. __isig(sig, tty);
  998. /* clear echo buffer */
  999. mutex_lock(&ldata->output_lock);
  1000. ldata->echo_head = ldata->echo_tail = 0;
  1001. ldata->echo_mark = ldata->echo_commit = 0;
  1002. mutex_unlock(&ldata->output_lock);
  1003. /* clear output buffer */
  1004. tty_driver_flush_buffer(tty);
  1005. /* clear input buffer */
  1006. reset_buffer_flags(tty->disc_data);
  1007. /* notify pty master of flush */
  1008. if (tty->link)
  1009. n_tty_packet_mode_flush(tty);
  1010. up_write(&tty->termios_rwsem);
  1011. down_read(&tty->termios_rwsem);
  1012. }
  1013. }
  1014. /**
  1015. * n_tty_receive_break - handle break
  1016. * @tty: terminal
  1017. *
  1018. * An RS232 break event has been hit in the incoming bitstream. This
  1019. * can cause a variety of events depending upon the termios settings.
  1020. *
  1021. * n_tty_receive_buf()/producer path:
  1022. * caller holds non-exclusive termios_rwsem
  1023. *
  1024. * Note: may get exclusive termios_rwsem if flushing input buffer
  1025. */
  1026. static void n_tty_receive_break(struct tty_struct *tty)
  1027. {
  1028. struct n_tty_data *ldata = tty->disc_data;
  1029. if (I_IGNBRK(tty))
  1030. return;
  1031. if (I_BRKINT(tty)) {
  1032. isig(SIGINT, tty);
  1033. return;
  1034. }
  1035. if (I_PARMRK(tty)) {
  1036. put_tty_queue('\377', ldata);
  1037. put_tty_queue('\0', ldata);
  1038. }
  1039. put_tty_queue('\0', ldata);
  1040. if (waitqueue_active(&tty->read_wait))
  1041. wake_up_interruptible_poll(&tty->read_wait, POLLIN);
  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
  1048. * driver has flagged this the bits that were missed are gone
  1049. * forever.
  1050. *
  1051. * Called from the receive_buf path so single threaded. Does not
  1052. * need locking as num_overrun and overrun_time are function
  1053. * private.
  1054. */
  1055. static void n_tty_receive_overrun(struct tty_struct *tty)
  1056. {
  1057. struct n_tty_data *ldata = tty->disc_data;
  1058. ldata->num_overrun++;
  1059. if (time_after(jiffies, ldata->overrun_time + HZ) ||
  1060. time_after(ldata->overrun_time, jiffies)) {
  1061. printk(KERN_WARNING "%s: %d input overrun(s)\n",
  1062. tty_name(tty),
  1063. ldata->num_overrun);
  1064. ldata->overrun_time = jiffies;
  1065. ldata->num_overrun = 0;
  1066. }
  1067. }
  1068. /**
  1069. * n_tty_receive_parity_error - error notifier
  1070. * @tty: terminal device
  1071. * @c: character
  1072. *
  1073. * Process a parity error and queue the right data to indicate
  1074. * the error case if necessary.
  1075. *
  1076. * n_tty_receive_buf()/producer path:
  1077. * caller holds non-exclusive termios_rwsem
  1078. */
  1079. static void n_tty_receive_parity_error(struct tty_struct *tty, unsigned char c)
  1080. {
  1081. struct n_tty_data *ldata = tty->disc_data;
  1082. if (I_INPCK(tty)) {
  1083. if (I_IGNPAR(tty))
  1084. return;
  1085. if (I_PARMRK(tty)) {
  1086. put_tty_queue('\377', ldata);
  1087. put_tty_queue('\0', ldata);
  1088. put_tty_queue(c, ldata);
  1089. } else
  1090. put_tty_queue('\0', ldata);
  1091. } else
  1092. put_tty_queue(c, ldata);
  1093. if (waitqueue_active(&tty->read_wait))
  1094. wake_up_interruptible_poll(&tty->read_wait, POLLIN);
  1095. }
  1096. static void
  1097. n_tty_receive_signal_char(struct tty_struct *tty, int signal, unsigned char c)
  1098. {
  1099. isig(signal, tty);
  1100. if (I_IXON(tty))
  1101. start_tty(tty);
  1102. if (L_ECHO(tty)) {
  1103. echo_char(c, tty);
  1104. commit_echoes(tty);
  1105. } else
  1106. process_echoes(tty);
  1107. return;
  1108. }
  1109. /**
  1110. * n_tty_receive_char - perform processing
  1111. * @tty: terminal device
  1112. * @c: character
  1113. *
  1114. * Process an individual character of input received from the driver.
  1115. * This is serialized with respect to itself by the rules for the
  1116. * driver above.
  1117. *
  1118. * n_tty_receive_buf()/producer path:
  1119. * caller holds non-exclusive termios_rwsem
  1120. * publishes canon_head if canonical mode is active
  1121. *
  1122. * Returns 1 if LNEXT was received, else returns 0
  1123. */
  1124. static int
  1125. n_tty_receive_char_special(struct tty_struct *tty, unsigned char c)
  1126. {
  1127. struct n_tty_data *ldata = tty->disc_data;
  1128. if (I_IXON(tty)) {
  1129. if (c == START_CHAR(tty)) {
  1130. start_tty(tty);
  1131. process_echoes(tty);
  1132. return 0;
  1133. }
  1134. if (c == STOP_CHAR(tty)) {
  1135. stop_tty(tty);
  1136. return 0;
  1137. }
  1138. }
  1139. if (L_ISIG(tty)) {
  1140. if (c == INTR_CHAR(tty)) {
  1141. n_tty_receive_signal_char(tty, SIGINT, c);
  1142. return 0;
  1143. } else if (c == QUIT_CHAR(tty)) {
  1144. n_tty_receive_signal_char(tty, SIGQUIT, c);
  1145. return 0;
  1146. } else if (c == SUSP_CHAR(tty)) {
  1147. n_tty_receive_signal_char(tty, SIGTSTP, c);
  1148. return 0;
  1149. }
  1150. }
  1151. if (tty->stopped && !tty->flow_stopped && I_IXON(tty) && I_IXANY(tty)) {
  1152. start_tty(tty);
  1153. process_echoes(tty);
  1154. }
  1155. if (c == '\r') {
  1156. if (I_IGNCR(tty))
  1157. return 0;
  1158. if (I_ICRNL(tty))
  1159. c = '\n';
  1160. } else if (c == '\n' && I_INLCR(tty))
  1161. c = '\r';
  1162. if (ldata->icanon) {
  1163. if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
  1164. (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
  1165. eraser(c, tty);
  1166. commit_echoes(tty);
  1167. return 0;
  1168. }
  1169. if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
  1170. ldata->lnext = 1;
  1171. if (L_ECHO(tty)) {
  1172. finish_erasing(ldata);
  1173. if (L_ECHOCTL(tty)) {
  1174. echo_char_raw('^', ldata);
  1175. echo_char_raw('\b', ldata);
  1176. commit_echoes(tty);
  1177. }
  1178. }
  1179. return 1;
  1180. }
  1181. if (c == REPRINT_CHAR(tty) && L_ECHO(tty) && L_IEXTEN(tty)) {
  1182. size_t tail = ldata->canon_head;
  1183. finish_erasing(ldata);
  1184. echo_char(c, tty);
  1185. echo_char_raw('\n', ldata);
  1186. while (tail != ldata->read_head) {
  1187. echo_char(read_buf(ldata, tail), tty);
  1188. tail++;
  1189. }
  1190. commit_echoes(tty);
  1191. return 0;
  1192. }
  1193. if (c == '\n') {
  1194. if (L_ECHO(tty) || L_ECHONL(tty)) {
  1195. echo_char_raw('\n', ldata);
  1196. commit_echoes(tty);
  1197. }
  1198. goto handle_newline;
  1199. }
  1200. if (c == EOF_CHAR(tty)) {
  1201. c = __DISABLED_CHAR;
  1202. goto handle_newline;
  1203. }
  1204. if ((c == EOL_CHAR(tty)) ||
  1205. (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
  1206. /*
  1207. * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
  1208. */
  1209. if (L_ECHO(tty)) {
  1210. /* Record the column of first canon char. */
  1211. if (ldata->canon_head == ldata->read_head)
  1212. echo_set_canon_col(ldata);
  1213. echo_char(c, tty);
  1214. commit_echoes(tty);
  1215. }
  1216. /*
  1217. * XXX does PARMRK doubling happen for
  1218. * EOL_CHAR and EOL2_CHAR?
  1219. */
  1220. if (c == (unsigned char) '\377' && I_PARMRK(tty))
  1221. put_tty_queue(c, ldata);
  1222. handle_newline:
  1223. set_bit(ldata->read_head & (N_TTY_BUF_SIZE - 1), ldata->read_flags);
  1224. put_tty_queue(c, ldata);
  1225. smp_store_release(&ldata->canon_head, ldata->read_head);
  1226. kill_fasync(&tty->fasync, SIGIO, POLL_IN);
  1227. if (waitqueue_active(&tty->read_wait))
  1228. wake_up_interruptible_poll(&tty->read_wait, POLLIN);
  1229. return 0;
  1230. }
  1231. }
  1232. if (L_ECHO(tty)) {
  1233. finish_erasing(ldata);
  1234. if (c == '\n')
  1235. echo_char_raw('\n', ldata);
  1236. else {
  1237. /* Record the column of first canon char. */
  1238. if (ldata->canon_head == ldata->read_head)
  1239. echo_set_canon_col(ldata);
  1240. echo_char(c, tty);
  1241. }
  1242. commit_echoes(tty);
  1243. }
  1244. /* PARMRK doubling check */
  1245. if (c == (unsigned char) '\377' && I_PARMRK(tty))
  1246. put_tty_queue(c, ldata);
  1247. put_tty_queue(c, ldata);
  1248. return 0;
  1249. }
  1250. static inline void
  1251. n_tty_receive_char_inline(struct tty_struct *tty, unsigned char c)
  1252. {
  1253. struct n_tty_data *ldata = tty->disc_data;
  1254. if (tty->stopped && !tty->flow_stopped && I_IXON(tty) && I_IXANY(tty)) {
  1255. start_tty(tty);
  1256. process_echoes(tty);
  1257. }
  1258. if (L_ECHO(tty)) {
  1259. finish_erasing(ldata);
  1260. /* Record the column of first canon char. */
  1261. if (ldata->canon_head == ldata->read_head)
  1262. echo_set_canon_col(ldata);
  1263. echo_char(c, tty);
  1264. commit_echoes(tty);
  1265. }
  1266. /* PARMRK doubling check */
  1267. if (c == (unsigned char) '\377' && I_PARMRK(tty))
  1268. put_tty_queue(c, ldata);
  1269. put_tty_queue(c, ldata);
  1270. }
  1271. static void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
  1272. {
  1273. n_tty_receive_char_inline(tty, c);
  1274. }
  1275. static inline void
  1276. n_tty_receive_char_fast(struct tty_struct *tty, unsigned char c)
  1277. {
  1278. struct n_tty_data *ldata = tty->disc_data;
  1279. if (tty->stopped && !tty->flow_stopped && I_IXON(tty) && I_IXANY(tty)) {
  1280. start_tty(tty);
  1281. process_echoes(tty);
  1282. }
  1283. if (L_ECHO(tty)) {
  1284. finish_erasing(ldata);
  1285. /* Record the column of first canon char. */
  1286. if (ldata->canon_head == ldata->read_head)
  1287. echo_set_canon_col(ldata);
  1288. echo_char(c, tty);
  1289. commit_echoes(tty);
  1290. }
  1291. put_tty_queue(c, ldata);
  1292. }
  1293. static void n_tty_receive_char_closing(struct tty_struct *tty, unsigned char c)
  1294. {
  1295. if (I_ISTRIP(tty))
  1296. c &= 0x7f;
  1297. if (I_IUCLC(tty) && L_IEXTEN(tty))
  1298. c = tolower(c);
  1299. if (I_IXON(tty)) {
  1300. if (c == STOP_CHAR(tty))
  1301. stop_tty(tty);
  1302. else if (c == START_CHAR(tty) ||
  1303. (tty->stopped && !tty->flow_stopped && I_IXANY(tty) &&
  1304. c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) &&
  1305. c != SUSP_CHAR(tty))) {
  1306. start_tty(tty);
  1307. process_echoes(tty);
  1308. }
  1309. }
  1310. }
  1311. static void
  1312. n_tty_receive_char_flagged(struct tty_struct *tty, unsigned char c, char flag)
  1313. {
  1314. switch (flag) {
  1315. case TTY_BREAK:
  1316. n_tty_receive_break(tty);
  1317. break;
  1318. case TTY_PARITY:
  1319. case TTY_FRAME:
  1320. n_tty_receive_parity_error(tty, c);
  1321. break;
  1322. case TTY_OVERRUN:
  1323. n_tty_receive_overrun(tty);
  1324. break;
  1325. default:
  1326. printk(KERN_ERR "%s: unknown flag %d\n",
  1327. tty_name(tty), flag);
  1328. break;
  1329. }
  1330. }
  1331. static void
  1332. n_tty_receive_char_lnext(struct tty_struct *tty, unsigned char c, char flag)
  1333. {
  1334. struct n_tty_data *ldata = tty->disc_data;
  1335. ldata->lnext = 0;
  1336. if (likely(flag == TTY_NORMAL)) {
  1337. if (I_ISTRIP(tty))
  1338. c &= 0x7f;
  1339. if (I_IUCLC(tty) && L_IEXTEN(tty))
  1340. c = tolower(c);
  1341. n_tty_receive_char(tty, c);
  1342. } else
  1343. n_tty_receive_char_flagged(tty, c, flag);
  1344. }
  1345. static void
  1346. n_tty_receive_buf_real_raw(struct tty_struct *tty, const unsigned char *cp,
  1347. char *fp, int count)
  1348. {
  1349. struct n_tty_data *ldata = tty->disc_data;
  1350. size_t n, head;
  1351. head = ldata->read_head & (N_TTY_BUF_SIZE - 1);
  1352. n = min_t(size_t, count, N_TTY_BUF_SIZE - head);
  1353. memcpy(read_buf_addr(ldata, head), cp, n);
  1354. ldata->read_head += n;
  1355. cp += n;
  1356. count -= n;
  1357. 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. }
  1362. static void
  1363. n_tty_receive_buf_raw(struct tty_struct *tty, const unsigned char *cp,
  1364. char *fp, int count)
  1365. {
  1366. struct n_tty_data *ldata = tty->disc_data;
  1367. char flag = TTY_NORMAL;
  1368. while (count--) {
  1369. if (fp)
  1370. flag = *fp++;
  1371. if (likely(flag == TTY_NORMAL))
  1372. put_tty_queue(*cp++, ldata);
  1373. else
  1374. n_tty_receive_char_flagged(tty, *cp++, flag);
  1375. }
  1376. }
  1377. static void
  1378. n_tty_receive_buf_closing(struct tty_struct *tty, const unsigned char *cp,
  1379. char *fp, int count)
  1380. {
  1381. char flag = TTY_NORMAL;
  1382. while (count--) {
  1383. if (fp)
  1384. flag = *fp++;
  1385. if (likely(flag == TTY_NORMAL))
  1386. n_tty_receive_char_closing(tty, *cp++);
  1387. else
  1388. n_tty_receive_char_flagged(tty, *cp++, flag);
  1389. }
  1390. }
  1391. static void
  1392. n_tty_receive_buf_standard(struct tty_struct *tty, const unsigned char *cp,
  1393. char *fp, int count)
  1394. {
  1395. struct n_tty_data *ldata = tty->disc_data;
  1396. char flag = TTY_NORMAL;
  1397. while (count--) {
  1398. if (fp)
  1399. flag = *fp++;
  1400. if (likely(flag == TTY_NORMAL)) {
  1401. unsigned char c = *cp++;
  1402. if (I_ISTRIP(tty))
  1403. c &= 0x7f;
  1404. if (I_IUCLC(tty) && L_IEXTEN(tty))
  1405. c = tolower(c);
  1406. if (L_EXTPROC(tty)) {
  1407. put_tty_queue(c, ldata);
  1408. continue;
  1409. }
  1410. if (!test_bit(c, ldata->char_map))
  1411. n_tty_receive_char_inline(tty, c);
  1412. else if (n_tty_receive_char_special(tty, c) && count) {
  1413. if (fp)
  1414. flag = *fp++;
  1415. n_tty_receive_char_lnext(tty, *cp++, flag);
  1416. count--;
  1417. }
  1418. } else
  1419. n_tty_receive_char_flagged(tty, *cp++, flag);
  1420. }
  1421. }
  1422. static void
  1423. n_tty_receive_buf_fast(struct tty_struct *tty, const unsigned char *cp,
  1424. char *fp, int count)
  1425. {
  1426. struct n_tty_data *ldata = tty->disc_data;
  1427. char flag = TTY_NORMAL;
  1428. while (count--) {
  1429. if (fp)
  1430. flag = *fp++;
  1431. if (likely(flag == TTY_NORMAL)) {
  1432. unsigned char c = *cp++;
  1433. if (!test_bit(c, ldata->char_map))
  1434. n_tty_receive_char_fast(tty, c);
  1435. else if (n_tty_receive_char_special(tty, c) && count) {
  1436. if (fp)
  1437. flag = *fp++;
  1438. n_tty_receive_char_lnext(tty, *cp++, flag);
  1439. count--;
  1440. }
  1441. } else
  1442. n_tty_receive_char_flagged(tty, *cp++, flag);
  1443. }
  1444. }
  1445. static void __receive_buf(struct tty_struct *tty, const unsigned char *cp,
  1446. char *fp, int count)
  1447. {
  1448. struct n_tty_data *ldata = tty->disc_data;
  1449. bool preops = I_ISTRIP(tty) || (I_IUCLC(tty) && L_IEXTEN(tty));
  1450. if (ldata->real_raw)
  1451. n_tty_receive_buf_real_raw(tty, cp, fp, count);
  1452. else if (ldata->raw || (L_EXTPROC(tty) && !preops))
  1453. n_tty_receive_buf_raw(tty, cp, fp, count);
  1454. else if (tty->closing && !L_EXTPROC(tty))
  1455. n_tty_receive_buf_closing(tty, cp, fp, count);
  1456. else {
  1457. if (ldata->lnext) {
  1458. char flag = TTY_NORMAL;
  1459. if (fp)
  1460. flag = *fp++;
  1461. n_tty_receive_char_lnext(tty, *cp++, flag);
  1462. count--;
  1463. }
  1464. if (!preops && !I_PARMRK(tty))
  1465. n_tty_receive_buf_fast(tty, cp, fp, count);
  1466. else
  1467. n_tty_receive_buf_standard(tty, cp, fp, count);
  1468. flush_echoes(tty);
  1469. if (tty->ops->flush_chars)
  1470. tty->ops->flush_chars(tty);
  1471. }
  1472. if (ldata->icanon && !L_EXTPROC(tty))
  1473. return;
  1474. /* publish read_head to consumer */
  1475. smp_store_release(&ldata->commit_head, ldata->read_head);
  1476. if ((read_cnt(ldata) >= ldata->minimum_to_wake) || L_EXTPROC(tty)) {
  1477. kill_fasync(&tty->fasync, SIGIO, POLL_IN);
  1478. if (waitqueue_active(&tty->read_wait))
  1479. wake_up_interruptible_poll(&tty->read_wait, POLLIN);
  1480. }
  1481. }
  1482. /**
  1483. * n_tty_receive_buf_common - process input
  1484. * @tty: device to receive input
  1485. * @cp: input chars
  1486. * @fp: flags for each char (if NULL, all chars are TTY_NORMAL)
  1487. * @count: number of input chars in @cp
  1488. *
  1489. * Called by the terminal driver when a block of characters has
  1490. * been received. This function must be called from soft contexts
  1491. * not from interrupt context. The driver is responsible for making
  1492. * calls one at a time and in order (or using flush_to_ldisc)
  1493. *
  1494. * Returns the # of input chars from @cp which were processed.
  1495. *
  1496. * In canonical mode, the maximum line length is 4096 chars (including
  1497. * the line termination char); lines longer than 4096 chars are
  1498. * truncated. After 4095 chars, input data is still processed but
  1499. * not stored. Overflow processing ensures the tty can always
  1500. * receive more input until at least one line can be read.
  1501. *
  1502. * In non-canonical mode, the read buffer will only accept 4095 chars;
  1503. * this provides the necessary space for a newline char if the input
  1504. * mode is switched to canonical.
  1505. *
  1506. * Note it is possible for the read buffer to _contain_ 4096 chars
  1507. * in non-canonical mode: the read buffer could already contain the
  1508. * maximum canon line of 4096 chars when the mode is switched to
  1509. * non-canonical.
  1510. *
  1511. * n_tty_receive_buf()/producer path:
  1512. * claims non-exclusive termios_rwsem
  1513. * publishes commit_head or canon_head
  1514. */
  1515. static int
  1516. n_tty_receive_buf_common(struct tty_struct *tty, const unsigned char *cp,
  1517. char *fp, int count, int flow)
  1518. {
  1519. struct n_tty_data *ldata = tty->disc_data;
  1520. int room, n, rcvd = 0, overflow;
  1521. down_read(&tty->termios_rwsem);
  1522. while (1) {
  1523. /*
  1524. * When PARMRK is set, each input char may take up to 3 chars
  1525. * in the read buf; reduce the buffer space avail by 3x
  1526. *
  1527. * If we are doing input canonicalization, and there are no
  1528. * pending newlines, let characters through without limit, so
  1529. * that erase characters will be handled. Other excess
  1530. * characters will be beeped.
  1531. *
  1532. * paired with store in *_copy_from_read_buf() -- guarantees
  1533. * the consumer has loaded the data in read_buf up to the new
  1534. * read_tail (so this producer will not overwrite unread data)
  1535. */
  1536. size_t tail = smp_load_acquire(&ldata->read_tail);
  1537. room = N_TTY_BUF_SIZE - (ldata->read_head - tail);
  1538. if (I_PARMRK(tty))
  1539. room = (room + 2) / 3;
  1540. room--;
  1541. if (room <= 0) {
  1542. overflow = ldata->icanon && ldata->canon_head == tail;
  1543. if (overflow && room < 0)
  1544. ldata->read_head--;
  1545. room = overflow;
  1546. ldata->no_room = flow && !room;
  1547. } else
  1548. overflow = 0;
  1549. n = min(count, room);
  1550. if (!n)
  1551. break;
  1552. /* ignore parity errors if handling overflow */
  1553. if (!overflow || !fp || *fp != TTY_PARITY)
  1554. __receive_buf(tty, cp, fp, n);
  1555. cp += n;
  1556. if (fp)
  1557. fp += n;
  1558. count -= n;
  1559. rcvd += n;
  1560. }
  1561. tty->receive_room = room;
  1562. /* Unthrottle if handling overflow on pty */
  1563. if (tty->driver->type == TTY_DRIVER_TYPE_PTY) {
  1564. if (overflow) {
  1565. tty_set_flow_change(tty, TTY_UNTHROTTLE_SAFE);
  1566. tty_unthrottle_safe(tty);
  1567. __tty_set_flow_change(tty, 0);
  1568. }
  1569. } else
  1570. n_tty_check_throttle(tty);
  1571. up_read(&tty->termios_rwsem);
  1572. return rcvd;
  1573. }
  1574. static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
  1575. char *fp, int count)
  1576. {
  1577. n_tty_receive_buf_common(tty, cp, fp, count, 0);
  1578. }
  1579. static int n_tty_receive_buf2(struct tty_struct *tty, const unsigned char *cp,
  1580. char *fp, int count)
  1581. {
  1582. return n_tty_receive_buf_common(tty, cp, fp, count, 1);
  1583. }
  1584. int is_ignored(int sig)
  1585. {
  1586. return (sigismember(&current->blocked, sig) ||
  1587. current->sighand->action[sig-1].sa.sa_handler == SIG_IGN);
  1588. }
  1589. /**
  1590. * n_tty_set_termios - termios data changed
  1591. * @tty: terminal
  1592. * @old: previous data
  1593. *
  1594. * Called by the tty layer when the user changes termios flags so
  1595. * that the line discipline can plan ahead. This function cannot sleep
  1596. * and is protected from re-entry by the tty layer. The user is
  1597. * guaranteed that this function will not be re-entered or in progress
  1598. * when the ldisc is closed.
  1599. *
  1600. * Locking: Caller holds tty->termios_rwsem
  1601. */
  1602. static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
  1603. {
  1604. struct n_tty_data *ldata = tty->disc_data;
  1605. if (!old || (old->c_lflag ^ tty->termios.c_lflag) & ICANON) {
  1606. bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
  1607. ldata->line_start = ldata->read_tail;
  1608. if (!L_ICANON(tty) || !read_cnt(ldata)) {
  1609. ldata->canon_head = ldata->read_tail;
  1610. ldata->push = 0;
  1611. } else {
  1612. set_bit((ldata->read_head - 1) & (N_TTY_BUF_SIZE - 1),
  1613. ldata->read_flags);
  1614. ldata->canon_head = ldata->read_head;
  1615. ldata->push = 1;
  1616. }
  1617. ldata->commit_head = ldata->read_head;
  1618. ldata->erasing = 0;
  1619. ldata->lnext = 0;
  1620. }
  1621. ldata->icanon = (L_ICANON(tty) != 0);
  1622. if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
  1623. I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
  1624. I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
  1625. I_PARMRK(tty)) {
  1626. bitmap_zero(ldata->char_map, 256);
  1627. if (I_IGNCR(tty) || I_ICRNL(tty))
  1628. set_bit('\r', ldata->char_map);
  1629. if (I_INLCR(tty))
  1630. set_bit('\n', ldata->char_map);
  1631. if (L_ICANON(tty)) {
  1632. set_bit(ERASE_CHAR(tty), ldata->char_map);
  1633. set_bit(KILL_CHAR(tty), ldata->char_map);
  1634. set_bit(EOF_CHAR(tty), ldata->char_map);
  1635. set_bit('\n', ldata->char_map);
  1636. set_bit(EOL_CHAR(tty), ldata->char_map);
  1637. if (L_IEXTEN(tty)) {
  1638. set_bit(WERASE_CHAR(tty), ldata->char_map);
  1639. set_bit(LNEXT_CHAR(tty), ldata->char_map);
  1640. set_bit(EOL2_CHAR(tty), ldata->char_map);
  1641. if (L_ECHO(tty))
  1642. set_bit(REPRINT_CHAR(tty),
  1643. ldata->char_map);
  1644. }
  1645. }
  1646. if (I_IXON(tty)) {
  1647. set_bit(START_CHAR(tty), ldata->char_map);
  1648. set_bit(STOP_CHAR(tty), ldata->char_map);
  1649. }
  1650. if (L_ISIG(tty)) {
  1651. set_bit(INTR_CHAR(tty), ldata->char_map);
  1652. set_bit(QUIT_CHAR(tty), ldata->char_map);
  1653. set_bit(SUSP_CHAR(tty), ldata->char_map);
  1654. }
  1655. clear_bit(__DISABLED_CHAR, ldata->char_map);
  1656. ldata->raw = 0;
  1657. ldata->real_raw = 0;
  1658. } else {
  1659. ldata->raw = 1;
  1660. if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
  1661. (I_IGNPAR(tty) || !I_INPCK(tty)) &&
  1662. (tty->driver->flags & TTY_DRIVER_REAL_RAW))
  1663. ldata->real_raw = 1;
  1664. else
  1665. ldata->real_raw = 0;
  1666. }
  1667. /*
  1668. * Fix tty hang when I_IXON(tty) is cleared, but the tty
  1669. * been stopped by STOP_CHAR(tty) before it.
  1670. */
  1671. if (!I_IXON(tty) && old && (old->c_iflag & IXON) && !tty->flow_stopped) {
  1672. start_tty(tty);
  1673. process_echoes(tty);
  1674. }
  1675. /* The termios change make the tty ready for I/O */
  1676. if (waitqueue_active(&tty->write_wait))
  1677. wake_up_interruptible(&tty->write_wait);
  1678. if (waitqueue_active(&tty->read_wait))
  1679. wake_up_interruptible(&tty->read_wait);
  1680. }
  1681. /**
  1682. * n_tty_close - close the ldisc for this tty
  1683. * @tty: device
  1684. *
  1685. * Called from the terminal layer when this line discipline is
  1686. * being shut down, either because of a close or becsuse of a
  1687. * discipline change. The function will not be called while other
  1688. * ldisc methods are in progress.
  1689. */
  1690. static void n_tty_close(struct tty_struct *tty)
  1691. {
  1692. struct n_tty_data *ldata = tty->disc_data;
  1693. if (tty->link)
  1694. n_tty_packet_mode_flush(tty);
  1695. vfree(ldata);
  1696. tty->disc_data = NULL;
  1697. }
  1698. /**
  1699. * n_tty_open - open an ldisc
  1700. * @tty: terminal to open
  1701. *
  1702. * Called when this line discipline is being attached to the
  1703. * terminal device. Can sleep. Called serialized so that no
  1704. * other events will occur in parallel. No further open will occur
  1705. * until a close.
  1706. */
  1707. static int n_tty_open(struct tty_struct *tty)
  1708. {
  1709. struct n_tty_data *ldata;
  1710. /* Currently a malloc failure here can panic */
  1711. ldata = vmalloc(sizeof(*ldata));
  1712. if (!ldata)
  1713. goto err;
  1714. ldata->overrun_time = jiffies;
  1715. mutex_init(&ldata->atomic_read_lock);
  1716. mutex_init(&ldata->output_lock);
  1717. tty->disc_data = ldata;
  1718. reset_buffer_flags(tty->disc_data);
  1719. ldata->column = 0;
  1720. ldata->canon_column = 0;
  1721. ldata->minimum_to_wake = 1;
  1722. ldata->num_overrun = 0;
  1723. ldata->no_room = 0;
  1724. ldata->lnext = 0;
  1725. tty->closing = 0;
  1726. /* indicate buffer work may resume */
  1727. clear_bit(TTY_LDISC_HALTED, &tty->flags);
  1728. n_tty_set_termios(tty, NULL);
  1729. tty_unthrottle(tty);
  1730. return 0;
  1731. err:
  1732. return -ENOMEM;
  1733. }
  1734. static inline int input_available_p(struct tty_struct *tty, int poll)
  1735. {
  1736. struct n_tty_data *ldata = tty->disc_data;
  1737. int amt = poll && !TIME_CHAR(tty) && MIN_CHAR(tty) ? MIN_CHAR(tty) : 1;
  1738. if (ldata->icanon && !L_EXTPROC(tty))
  1739. return ldata->canon_head != ldata->read_tail;
  1740. else
  1741. return ldata->commit_head - ldata->read_tail >= amt;
  1742. }
  1743. static inline int check_other_done(struct tty_struct *tty)
  1744. {
  1745. int done = test_bit(TTY_OTHER_DONE, &tty->flags);
  1746. if (done) {
  1747. /* paired with cmpxchg() in check_other_closed(); ensures
  1748. * read buffer head index is not stale
  1749. */
  1750. smp_mb__after_atomic();
  1751. }
  1752. return done;
  1753. }
  1754. /**
  1755. * copy_from_read_buf - copy read data directly
  1756. * @tty: terminal device
  1757. * @b: user data
  1758. * @nr: size of data
  1759. *
  1760. * Helper function to speed up n_tty_read. It is only called when
  1761. * ICANON is off; it copies characters straight from the tty queue to
  1762. * user space directly. It can be profitably called twice; once to
  1763. * drain the space from the tail pointer to the (physical) end of the
  1764. * buffer, and once to drain the space from the (physical) beginning of
  1765. * the buffer to head pointer.
  1766. *
  1767. * Called under the ldata->atomic_read_lock sem
  1768. *
  1769. * n_tty_read()/consumer path:
  1770. * caller holds non-exclusive termios_rwsem
  1771. * read_tail published
  1772. */
  1773. static int copy_from_read_buf(struct tty_struct *tty,
  1774. unsigned char __user **b,
  1775. size_t *nr)
  1776. {
  1777. struct n_tty_data *ldata = tty->disc_data;
  1778. int retval;
  1779. size_t n;
  1780. bool is_eof;
  1781. size_t head = smp_load_acquire(&ldata->commit_head);
  1782. size_t tail = ldata->read_tail & (N_TTY_BUF_SIZE - 1);
  1783. retval = 0;
  1784. n = min(head - ldata->read_tail, N_TTY_BUF_SIZE - tail);
  1785. n = min(*nr, n);
  1786. if (n) {
  1787. retval = copy_to_user(*b, read_buf_addr(ldata, tail), n);
  1788. n -= retval;
  1789. is_eof = n == 1 && read_buf(ldata, tail) == EOF_CHAR(tty);
  1790. tty_audit_add_data(tty, read_buf_addr(ldata, tail), n,
  1791. ldata->icanon);
  1792. smp_store_release(&ldata->read_tail, ldata->read_tail + n);
  1793. /* Turn single EOF into zero-length read */
  1794. if (L_EXTPROC(tty) && ldata->icanon && is_eof &&
  1795. (head == ldata->read_tail))
  1796. n = 0;
  1797. *b += n;
  1798. *nr -= n;
  1799. }
  1800. return retval;
  1801. }
  1802. /**
  1803. * canon_copy_from_read_buf - copy read data in canonical mode
  1804. * @tty: terminal device
  1805. * @b: user data
  1806. * @nr: size of data
  1807. *
  1808. * Helper function for n_tty_read. It is only called when ICANON is on;
  1809. * it copies one line of input up to and including the line-delimiting
  1810. * character into the user-space buffer.
  1811. *
  1812. * NB: When termios is changed from non-canonical to canonical mode and
  1813. * the read buffer contains data, n_tty_set_termios() simulates an EOF
  1814. * push (as if C-d were input) _without_ the DISABLED_CHAR in the buffer.
  1815. * This causes data already processed as input to be immediately available
  1816. * as input although a newline has not been received.
  1817. *
  1818. * Called under the atomic_read_lock mutex
  1819. *
  1820. * n_tty_read()/consumer path:
  1821. * caller holds non-exclusive termios_rwsem
  1822. * read_tail published
  1823. */
  1824. static int canon_copy_from_read_buf(struct tty_struct *tty,
  1825. unsigned char __user **b,
  1826. size_t *nr)
  1827. {
  1828. struct n_tty_data *ldata = tty->disc_data;
  1829. size_t n, size, more, c;
  1830. size_t eol;
  1831. size_t tail;
  1832. int ret, found = 0;
  1833. bool eof_push = 0;
  1834. /* N.B. avoid overrun if nr == 0 */
  1835. n = min(*nr, smp_load_acquire(&ldata->canon_head) - ldata->read_tail);
  1836. if (!n)
  1837. return 0;
  1838. tail = ldata->read_tail & (N_TTY_BUF_SIZE - 1);
  1839. size = min_t(size_t, tail + n, N_TTY_BUF_SIZE);
  1840. n_tty_trace("%s: nr:%zu tail:%zu n:%zu size:%zu\n",
  1841. __func__, *nr, tail, n, size);
  1842. eol = find_next_bit(ldata->read_flags, size, tail);
  1843. more = n - (size - tail);
  1844. if (eol == N_TTY_BUF_SIZE && more) {
  1845. /* scan wrapped without finding set bit */
  1846. eol = find_next_bit(ldata->read_flags, more, 0);
  1847. if (eol != more)
  1848. found = 1;
  1849. } else if (eol != size)
  1850. found = 1;
  1851. size = N_TTY_BUF_SIZE - tail;
  1852. n = eol - tail;
  1853. if (n > N_TTY_BUF_SIZE)
  1854. n += N_TTY_BUF_SIZE;
  1855. n += found;
  1856. c = n;
  1857. if (found && !ldata->push && read_buf(ldata, eol) == __DISABLED_CHAR) {
  1858. n--;
  1859. eof_push = !n && ldata->read_tail != ldata->line_start;
  1860. }
  1861. n_tty_trace("%s: eol:%zu found:%d n:%zu c:%zu size:%zu more:%zu\n",
  1862. __func__, eol, found, n, c, size, more);
  1863. if (n > size) {
  1864. ret = tty_copy_to_user(tty, *b, read_buf_addr(ldata, tail), size);
  1865. if (ret)
  1866. return -EFAULT;
  1867. ret = tty_copy_to_user(tty, *b + size, ldata->read_buf, n - size);
  1868. } else
  1869. ret = tty_copy_to_user(tty, *b, read_buf_addr(ldata, tail), n);
  1870. if (ret)
  1871. return -EFAULT;
  1872. *b += n;
  1873. *nr -= n;
  1874. if (found)
  1875. clear_bit(eol, ldata->read_flags);
  1876. smp_store_release(&ldata->read_tail, ldata->read_tail + c);
  1877. if (found) {
  1878. if (!ldata->push)
  1879. ldata->line_start = ldata->read_tail;
  1880. else
  1881. ldata->push = 0;
  1882. tty_audit_push(tty);
  1883. }
  1884. return eof_push ? -EAGAIN : 0;
  1885. }
  1886. extern ssize_t redirected_tty_write(struct file *, const char __user *,
  1887. size_t, loff_t *);
  1888. /**
  1889. * job_control - check job control
  1890. * @tty: tty
  1891. * @file: file handle
  1892. *
  1893. * Perform job control management checks on this file/tty descriptor
  1894. * and if appropriate send any needed signals and return a negative
  1895. * error code if action should be taken.
  1896. *
  1897. * Locking: redirected write test is safe
  1898. * current->signal->tty check is safe
  1899. * ctrl_lock to safely reference tty->pgrp
  1900. */
  1901. static int job_control(struct tty_struct *tty, struct file *file)
  1902. {
  1903. struct pid *pgrp;
  1904. /* Job control check -- must be done at start and after
  1905. every sleep (POSIX.1 7.1.1.4). */
  1906. /* NOTE: not yet done after every sleep pending a thorough
  1907. check of the logic of this change. -- jlc */
  1908. /* don't stop on /dev/console */
  1909. if (file->f_op->write == redirected_tty_write ||
  1910. current->signal->tty != tty)
  1911. return 0;
  1912. rcu_read_lock();
  1913. pgrp = task_pgrp(current);
  1914. spin_lock_irq(&tty->ctrl_lock);
  1915. if (!tty->pgrp)
  1916. printk(KERN_ERR "n_tty_read: no tty->pgrp!\n");
  1917. else if (pgrp != tty->pgrp) {
  1918. spin_unlock_irq(&tty->ctrl_lock);
  1919. if (is_ignored(SIGTTIN) || is_current_pgrp_orphaned()) {
  1920. rcu_read_unlock();
  1921. return -EIO;
  1922. }
  1923. kill_pgrp(pgrp, SIGTTIN, 1);
  1924. rcu_read_unlock();
  1925. set_thread_flag(TIF_SIGPENDING);
  1926. return -ERESTARTSYS;
  1927. }
  1928. spin_unlock_irq(&tty->ctrl_lock);
  1929. rcu_read_unlock();
  1930. return 0;
  1931. }
  1932. /**
  1933. * n_tty_read - read function for tty
  1934. * @tty: tty device
  1935. * @file: file object
  1936. * @buf: userspace buffer pointer
  1937. * @nr: size of I/O
  1938. *
  1939. * Perform reads for the line discipline. We are guaranteed that the
  1940. * line discipline will not be closed under us but we may get multiple
  1941. * parallel readers and must handle this ourselves. We may also get
  1942. * a hangup. Always called in user context, may sleep.
  1943. *
  1944. * This code must be sure never to sleep through a hangup.
  1945. *
  1946. * n_tty_read()/consumer path:
  1947. * claims non-exclusive termios_rwsem
  1948. * publishes read_tail
  1949. */
  1950. static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
  1951. unsigned char __user *buf, size_t nr)
  1952. {
  1953. struct n_tty_data *ldata = tty->disc_data;
  1954. unsigned char __user *b = buf;
  1955. DEFINE_WAIT_FUNC(wait, woken_wake_function);
  1956. int c, done;
  1957. int minimum, time;
  1958. ssize_t retval = 0;
  1959. long timeout;
  1960. int packet;
  1961. size_t tail;
  1962. c = job_control(tty, file);
  1963. if (c < 0)
  1964. return c;
  1965. /*
  1966. * Internal serialization of reads.
  1967. */
  1968. if (file->f_flags & O_NONBLOCK) {
  1969. if (!mutex_trylock(&ldata->atomic_read_lock))
  1970. return -EAGAIN;
  1971. } else {
  1972. if (mutex_lock_interruptible(&ldata->atomic_read_lock))
  1973. return -ERESTARTSYS;
  1974. }
  1975. down_read(&tty->termios_rwsem);
  1976. minimum = time = 0;
  1977. timeout = MAX_SCHEDULE_TIMEOUT;
  1978. if (!ldata->icanon) {
  1979. minimum = MIN_CHAR(tty);
  1980. if (minimum) {
  1981. time = (HZ / 10) * TIME_CHAR(tty);
  1982. if (time)
  1983. ldata->minimum_to_wake = 1;
  1984. else if (!waitqueue_active(&tty->read_wait) ||
  1985. (ldata->minimum_to_wake > minimum))
  1986. ldata->minimum_to_wake = minimum;
  1987. } else {
  1988. timeout = (HZ / 10) * TIME_CHAR(tty);
  1989. ldata->minimum_to_wake = minimum = 1;
  1990. }
  1991. }
  1992. packet = tty->packet;
  1993. tail = ldata->read_tail;
  1994. add_wait_queue(&tty->read_wait, &wait);
  1995. while (nr) {
  1996. /* First test for status change. */
  1997. if (packet && tty->link->ctrl_status) {
  1998. unsigned char cs;
  1999. if (b != buf)
  2000. break;
  2001. spin_lock_irq(&tty->link->ctrl_lock);
  2002. cs = tty->link->ctrl_status;
  2003. tty->link->ctrl_status = 0;
  2004. spin_unlock_irq(&tty->link->ctrl_lock);
  2005. if (tty_put_user(tty, cs, b++)) {
  2006. retval = -EFAULT;
  2007. b--;
  2008. break;
  2009. }
  2010. nr--;
  2011. break;
  2012. }
  2013. if (((minimum - (b - buf)) < ldata->minimum_to_wake) &&
  2014. ((minimum - (b - buf)) >= 1))
  2015. ldata->minimum_to_wake = (minimum - (b - buf));
  2016. done = check_other_done(tty);
  2017. if (!input_available_p(tty, 0)) {
  2018. if (done) {
  2019. retval = -EIO;
  2020. break;
  2021. }
  2022. if (tty_hung_up_p(file))
  2023. break;
  2024. if (!timeout)
  2025. break;
  2026. if (file->f_flags & O_NONBLOCK) {
  2027. retval = -EAGAIN;
  2028. break;
  2029. }
  2030. if (signal_pending(current)) {
  2031. retval = -ERESTARTSYS;
  2032. break;
  2033. }
  2034. up_read(&tty->termios_rwsem);
  2035. timeout = wait_woken(&wait, TASK_INTERRUPTIBLE,
  2036. timeout);
  2037. down_read(&tty->termios_rwsem);
  2038. continue;
  2039. }
  2040. if (ldata->icanon && !L_EXTPROC(tty)) {
  2041. retval = canon_copy_from_read_buf(tty, &b, &nr);
  2042. if (retval == -EAGAIN) {
  2043. retval = 0;
  2044. continue;
  2045. } else if (retval)
  2046. break;
  2047. } else {
  2048. int uncopied;
  2049. /* Deal with packet mode. */
  2050. if (packet && b == buf) {
  2051. if (tty_put_user(tty, TIOCPKT_DATA, b++)) {
  2052. retval = -EFAULT;
  2053. b--;
  2054. break;
  2055. }
  2056. nr--;
  2057. }
  2058. uncopied = copy_from_read_buf(tty, &b, &nr);
  2059. uncopied += copy_from_read_buf(tty, &b, &nr);
  2060. if (uncopied) {
  2061. retval = -EFAULT;
  2062. break;
  2063. }
  2064. }
  2065. n_tty_check_unthrottle(tty);
  2066. if (b - buf >= minimum)
  2067. break;
  2068. if (time)
  2069. timeout = time;
  2070. }
  2071. if (tail != ldata->read_tail)
  2072. n_tty_kick_worker(tty);
  2073. up_read(&tty->termios_rwsem);
  2074. remove_wait_queue(&tty->read_wait, &wait);
  2075. if (!waitqueue_active(&tty->read_wait))
  2076. ldata->minimum_to_wake = minimum;
  2077. mutex_unlock(&ldata->atomic_read_lock);
  2078. if (b - buf)
  2079. retval = b - buf;
  2080. return retval;
  2081. }
  2082. /**
  2083. * n_tty_write - write function for tty
  2084. * @tty: tty device
  2085. * @file: file object
  2086. * @buf: userspace buffer pointer
  2087. * @nr: size of I/O
  2088. *
  2089. * Write function of the terminal device. This is serialized with
  2090. * respect to other write callers but not to termios changes, reads
  2091. * and other such events. Since the receive code will echo characters,
  2092. * thus calling driver write methods, the output_lock is used in
  2093. * the output processing functions called here as well as in the
  2094. * echo processing function to protect the column state and space
  2095. * left in the buffer.
  2096. *
  2097. * This code must be sure never to sleep through a hangup.
  2098. *
  2099. * Locking: output_lock to protect column state and space left
  2100. * (note that the process_output*() functions take this
  2101. * lock themselves)
  2102. */
  2103. static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
  2104. const unsigned char *buf, size_t nr)
  2105. {
  2106. const unsigned char *b = buf;
  2107. DEFINE_WAIT_FUNC(wait, woken_wake_function);
  2108. int c;
  2109. ssize_t retval = 0;
  2110. /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
  2111. if (L_TOSTOP(tty) && file->f_op->write != redirected_tty_write) {
  2112. retval = tty_check_change(tty);
  2113. if (retval)
  2114. return retval;
  2115. }
  2116. down_read(&tty->termios_rwsem);
  2117. /* Write out any echoed characters that are still pending */
  2118. process_echoes(tty);
  2119. add_wait_queue(&tty->write_wait, &wait);
  2120. while (1) {
  2121. if (signal_pending(current)) {
  2122. retval = -ERESTARTSYS;
  2123. break;
  2124. }
  2125. if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
  2126. retval = -EIO;
  2127. break;
  2128. }
  2129. if (O_OPOST(tty)) {
  2130. while (nr > 0) {
  2131. ssize_t num = process_output_block(tty, b, nr);
  2132. if (num < 0) {
  2133. if (num == -EAGAIN)
  2134. break;
  2135. retval = num;
  2136. goto break_out;
  2137. }
  2138. b += num;
  2139. nr -= num;
  2140. if (nr == 0)
  2141. break;
  2142. c = *b;
  2143. if (process_output(c, tty) < 0)
  2144. break;
  2145. b++; nr--;
  2146. }
  2147. if (tty->ops->flush_chars)
  2148. tty->ops->flush_chars(tty);
  2149. } else {
  2150. struct n_tty_data *ldata = tty->disc_data;
  2151. while (nr > 0) {
  2152. mutex_lock(&ldata->output_lock);
  2153. c = tty->ops->write(tty, b, nr);
  2154. mutex_unlock(&ldata->output_lock);
  2155. if (c < 0) {
  2156. retval = c;
  2157. goto break_out;
  2158. }
  2159. if (!c)
  2160. break;
  2161. b += c;
  2162. nr -= c;
  2163. }
  2164. }
  2165. if (!nr)
  2166. break;
  2167. if (file->f_flags & O_NONBLOCK) {
  2168. retval = -EAGAIN;
  2169. break;
  2170. }
  2171. up_read(&tty->termios_rwsem);
  2172. wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
  2173. down_read(&tty->termios_rwsem);
  2174. }
  2175. break_out:
  2176. remove_wait_queue(&tty->write_wait, &wait);
  2177. if (b - buf != nr && tty->fasync)
  2178. set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  2179. up_read(&tty->termios_rwsem);
  2180. return (b - buf) ? b - buf : retval;
  2181. }
  2182. /**
  2183. * n_tty_poll - poll method for N_TTY
  2184. * @tty: terminal device
  2185. * @file: file accessing it
  2186. * @wait: poll table
  2187. *
  2188. * Called when the line discipline is asked to poll() for data or
  2189. * for special events. This code is not serialized with respect to
  2190. * other events save open/close.
  2191. *
  2192. * This code must be sure never to sleep through a hangup.
  2193. * Called without the kernel lock held - fine
  2194. */
  2195. static unsigned int n_tty_poll(struct tty_struct *tty, struct file *file,
  2196. poll_table *wait)
  2197. {
  2198. struct n_tty_data *ldata = tty->disc_data;
  2199. unsigned int mask = 0;
  2200. poll_wait(file, &tty->read_wait, wait);
  2201. poll_wait(file, &tty->write_wait, wait);
  2202. if (check_other_done(tty))
  2203. mask |= POLLHUP;
  2204. if (input_available_p(tty, 1))
  2205. mask |= POLLIN | POLLRDNORM;
  2206. if (tty->packet && tty->link->ctrl_status)
  2207. mask |= POLLPRI | POLLIN | POLLRDNORM;
  2208. if (tty_hung_up_p(file))
  2209. mask |= POLLHUP;
  2210. if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) {
  2211. if (MIN_CHAR(tty) && !TIME_CHAR(tty))
  2212. ldata->minimum_to_wake = MIN_CHAR(tty);
  2213. else
  2214. ldata->minimum_to_wake = 1;
  2215. }
  2216. if (tty->ops->write && !tty_is_writelocked(tty) &&
  2217. tty_chars_in_buffer(tty) < WAKEUP_CHARS &&
  2218. tty_write_room(tty) > 0)
  2219. mask |= POLLOUT | POLLWRNORM;
  2220. return mask;
  2221. }
  2222. static unsigned long inq_canon(struct n_tty_data *ldata)
  2223. {
  2224. size_t nr, head, tail;
  2225. if (ldata->canon_head == ldata->read_tail)
  2226. return 0;
  2227. head = ldata->canon_head;
  2228. tail = ldata->read_tail;
  2229. nr = head - tail;
  2230. /* Skip EOF-chars.. */
  2231. while (head != tail) {
  2232. if (test_bit(tail & (N_TTY_BUF_SIZE - 1), ldata->read_flags) &&
  2233. read_buf(ldata, tail) == __DISABLED_CHAR)
  2234. nr--;
  2235. tail++;
  2236. }
  2237. return nr;
  2238. }
  2239. static int n_tty_ioctl(struct tty_struct *tty, struct file *file,
  2240. unsigned int cmd, unsigned long arg)
  2241. {
  2242. struct n_tty_data *ldata = tty->disc_data;
  2243. int retval;
  2244. switch (cmd) {
  2245. case TIOCOUTQ:
  2246. return put_user(tty_chars_in_buffer(tty), (int __user *) arg);
  2247. case TIOCINQ:
  2248. down_write(&tty->termios_rwsem);
  2249. if (L_ICANON(tty))
  2250. retval = inq_canon(ldata);
  2251. else
  2252. retval = read_cnt(ldata);
  2253. up_write(&tty->termios_rwsem);
  2254. return put_user(retval, (unsigned int __user *) arg);
  2255. default:
  2256. return n_tty_ioctl_helper(tty, file, cmd, arg);
  2257. }
  2258. }
  2259. static void n_tty_fasync(struct tty_struct *tty, int on)
  2260. {
  2261. struct n_tty_data *ldata = tty->disc_data;
  2262. if (!waitqueue_active(&tty->read_wait)) {
  2263. if (on)
  2264. ldata->minimum_to_wake = 1;
  2265. else if (!tty->fasync)
  2266. ldata->minimum_to_wake = N_TTY_BUF_SIZE;
  2267. }
  2268. }
  2269. struct tty_ldisc_ops tty_ldisc_N_TTY = {
  2270. .magic = TTY_LDISC_MAGIC,
  2271. .name = "n_tty",
  2272. .open = n_tty_open,
  2273. .close = n_tty_close,
  2274. .flush_buffer = n_tty_flush_buffer,
  2275. .chars_in_buffer = n_tty_chars_in_buffer,
  2276. .read = n_tty_read,
  2277. .write = n_tty_write,
  2278. .ioctl = n_tty_ioctl,
  2279. .set_termios = n_tty_set_termios,
  2280. .poll = n_tty_poll,
  2281. .receive_buf = n_tty_receive_buf,
  2282. .write_wakeup = n_tty_write_wakeup,
  2283. .fasync = n_tty_fasync,
  2284. .receive_buf2 = n_tty_receive_buf2,
  2285. };
  2286. /**
  2287. * n_tty_inherit_ops - inherit N_TTY methods
  2288. * @ops: struct tty_ldisc_ops where to save N_TTY methods
  2289. *
  2290. * Enables a 'subclass' line discipline to 'inherit' N_TTY
  2291. * methods.
  2292. */
  2293. void n_tty_inherit_ops(struct tty_ldisc_ops *ops)
  2294. {
  2295. *ops = tty_ldisc_N_TTY;
  2296. ops->owner = NULL;
  2297. ops->refcount = ops->flags = 0;
  2298. }
  2299. EXPORT_SYMBOL_GPL(n_tty_inherit_ops);