n_tty.c 64 KB

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