n_tty.c 64 KB

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