n_tty.c 64 KB

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