tty_ldisc.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. #include <linux/types.h>
  2. #include <linux/major.h>
  3. #include <linux/errno.h>
  4. #include <linux/signal.h>
  5. #include <linux/fcntl.h>
  6. #include <linux/sched.h>
  7. #include <linux/interrupt.h>
  8. #include <linux/tty.h>
  9. #include <linux/tty_driver.h>
  10. #include <linux/tty_flip.h>
  11. #include <linux/devpts_fs.h>
  12. #include <linux/file.h>
  13. #include <linux/fdtable.h>
  14. #include <linux/console.h>
  15. #include <linux/timer.h>
  16. #include <linux/ctype.h>
  17. #include <linux/kd.h>
  18. #include <linux/mm.h>
  19. #include <linux/string.h>
  20. #include <linux/slab.h>
  21. #include <linux/poll.h>
  22. #include <linux/proc_fs.h>
  23. #include <linux/init.h>
  24. #include <linux/module.h>
  25. #include <linux/smp_lock.h>
  26. #include <linux/device.h>
  27. #include <linux/wait.h>
  28. #include <linux/bitops.h>
  29. #include <linux/delay.h>
  30. #include <linux/seq_file.h>
  31. #include <linux/uaccess.h>
  32. #include <asm/system.h>
  33. #include <linux/kbd_kern.h>
  34. #include <linux/vt_kern.h>
  35. #include <linux/selection.h>
  36. #include <linux/kmod.h>
  37. #include <linux/nsproxy.h>
  38. /*
  39. * This guards the refcounted line discipline lists. The lock
  40. * must be taken with irqs off because there are hangup path
  41. * callers who will do ldisc lookups and cannot sleep.
  42. */
  43. static DEFINE_SPINLOCK(tty_ldisc_lock);
  44. static DECLARE_WAIT_QUEUE_HEAD(tty_ldisc_wait);
  45. /* Line disc dispatch table */
  46. static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS];
  47. /**
  48. * tty_register_ldisc - install a line discipline
  49. * @disc: ldisc number
  50. * @new_ldisc: pointer to the ldisc object
  51. *
  52. * Installs a new line discipline into the kernel. The discipline
  53. * is set up as unreferenced and then made available to the kernel
  54. * from this point onwards.
  55. *
  56. * Locking:
  57. * takes tty_ldisc_lock to guard against ldisc races
  58. */
  59. int tty_register_ldisc(int disc, struct tty_ldisc_ops *new_ldisc)
  60. {
  61. unsigned long flags;
  62. int ret = 0;
  63. if (disc < N_TTY || disc >= NR_LDISCS)
  64. return -EINVAL;
  65. spin_lock_irqsave(&tty_ldisc_lock, flags);
  66. tty_ldiscs[disc] = new_ldisc;
  67. new_ldisc->num = disc;
  68. new_ldisc->refcount = 0;
  69. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  70. return ret;
  71. }
  72. EXPORT_SYMBOL(tty_register_ldisc);
  73. /**
  74. * tty_unregister_ldisc - unload a line discipline
  75. * @disc: ldisc number
  76. * @new_ldisc: pointer to the ldisc object
  77. *
  78. * Remove a line discipline from the kernel providing it is not
  79. * currently in use.
  80. *
  81. * Locking:
  82. * takes tty_ldisc_lock to guard against ldisc races
  83. */
  84. int tty_unregister_ldisc(int disc)
  85. {
  86. unsigned long flags;
  87. int ret = 0;
  88. if (disc < N_TTY || disc >= NR_LDISCS)
  89. return -EINVAL;
  90. spin_lock_irqsave(&tty_ldisc_lock, flags);
  91. if (tty_ldiscs[disc]->refcount)
  92. ret = -EBUSY;
  93. else
  94. tty_ldiscs[disc] = NULL;
  95. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  96. return ret;
  97. }
  98. EXPORT_SYMBOL(tty_unregister_ldisc);
  99. /**
  100. * tty_ldisc_try_get - try and reference an ldisc
  101. * @disc: ldisc number
  102. * @ld: tty ldisc structure to complete
  103. *
  104. * Attempt to open and lock a line discipline into place. Return
  105. * the line discipline refcounted and assigned in ld. On an error
  106. * report the error code back
  107. */
  108. static int tty_ldisc_try_get(int disc, struct tty_ldisc *ld)
  109. {
  110. unsigned long flags;
  111. struct tty_ldisc_ops *ldops;
  112. int err = -EINVAL;
  113. spin_lock_irqsave(&tty_ldisc_lock, flags);
  114. ld->ops = NULL;
  115. ldops = tty_ldiscs[disc];
  116. /* Check the entry is defined */
  117. if (ldops) {
  118. /* If the module is being unloaded we can't use it */
  119. if (!try_module_get(ldops->owner))
  120. err = -EAGAIN;
  121. else {
  122. /* lock it */
  123. ldops->refcount++;
  124. ld->ops = ldops;
  125. err = 0;
  126. }
  127. }
  128. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  129. return err;
  130. }
  131. /**
  132. * tty_ldisc_get - take a reference to an ldisc
  133. * @disc: ldisc number
  134. * @ld: tty line discipline structure to use
  135. *
  136. * Takes a reference to a line discipline. Deals with refcounts and
  137. * module locking counts. Returns NULL if the discipline is not available.
  138. * Returns a pointer to the discipline and bumps the ref count if it is
  139. * available
  140. *
  141. * Locking:
  142. * takes tty_ldisc_lock to guard against ldisc races
  143. */
  144. static int tty_ldisc_get(int disc, struct tty_ldisc *ld)
  145. {
  146. int err;
  147. if (disc < N_TTY || disc >= NR_LDISCS)
  148. return -EINVAL;
  149. err = tty_ldisc_try_get(disc, ld);
  150. if (err < 0) {
  151. request_module("tty-ldisc-%d", disc);
  152. err = tty_ldisc_try_get(disc, ld);
  153. }
  154. return err;
  155. }
  156. /**
  157. * tty_ldisc_put - drop ldisc reference
  158. * @disc: ldisc number
  159. *
  160. * Drop a reference to a line discipline. Manage refcounts and
  161. * module usage counts
  162. *
  163. * Locking:
  164. * takes tty_ldisc_lock to guard against ldisc races
  165. */
  166. static void tty_ldisc_put(struct tty_ldisc_ops *ld)
  167. {
  168. unsigned long flags;
  169. int disc = ld->num;
  170. BUG_ON(disc < N_TTY || disc >= NR_LDISCS);
  171. spin_lock_irqsave(&tty_ldisc_lock, flags);
  172. ld = tty_ldiscs[disc];
  173. BUG_ON(ld->refcount == 0);
  174. ld->refcount--;
  175. module_put(ld->owner);
  176. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  177. }
  178. static void * tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos)
  179. {
  180. return (*pos < NR_LDISCS) ? pos : NULL;
  181. }
  182. static void * tty_ldiscs_seq_next(struct seq_file *m, void *v, loff_t *pos)
  183. {
  184. (*pos)++;
  185. return (*pos < NR_LDISCS) ? pos : NULL;
  186. }
  187. static void tty_ldiscs_seq_stop(struct seq_file *m, void *v)
  188. {
  189. }
  190. static int tty_ldiscs_seq_show(struct seq_file *m, void *v)
  191. {
  192. int i = *(loff_t *)v;
  193. struct tty_ldisc ld;
  194. if (tty_ldisc_get(i, &ld) < 0)
  195. return 0;
  196. seq_printf(m, "%-10s %2d\n", ld.ops->name ? ld.ops->name : "???", i);
  197. tty_ldisc_put(ld.ops);
  198. return 0;
  199. }
  200. static const struct seq_operations tty_ldiscs_seq_ops = {
  201. .start = tty_ldiscs_seq_start,
  202. .next = tty_ldiscs_seq_next,
  203. .stop = tty_ldiscs_seq_stop,
  204. .show = tty_ldiscs_seq_show,
  205. };
  206. static int proc_tty_ldiscs_open(struct inode *inode, struct file *file)
  207. {
  208. return seq_open(file, &tty_ldiscs_seq_ops);
  209. }
  210. const struct file_operations tty_ldiscs_proc_fops = {
  211. .owner = THIS_MODULE,
  212. .open = proc_tty_ldiscs_open,
  213. .read = seq_read,
  214. .llseek = seq_lseek,
  215. .release = seq_release,
  216. };
  217. /**
  218. * tty_ldisc_assign - set ldisc on a tty
  219. * @tty: tty to assign
  220. * @ld: line discipline
  221. *
  222. * Install an instance of a line discipline into a tty structure. The
  223. * ldisc must have a reference count above zero to ensure it remains/
  224. * The tty instance refcount starts at zero.
  225. *
  226. * Locking:
  227. * Caller must hold references
  228. */
  229. static void tty_ldisc_assign(struct tty_struct *tty, struct tty_ldisc *ld)
  230. {
  231. ld->refcount = 0;
  232. tty->ldisc = *ld;
  233. }
  234. /**
  235. * tty_ldisc_try - internal helper
  236. * @tty: the tty
  237. *
  238. * Make a single attempt to grab and bump the refcount on
  239. * the tty ldisc. Return 0 on failure or 1 on success. This is
  240. * used to implement both the waiting and non waiting versions
  241. * of tty_ldisc_ref
  242. *
  243. * Locking: takes tty_ldisc_lock
  244. */
  245. static int tty_ldisc_try(struct tty_struct *tty)
  246. {
  247. unsigned long flags;
  248. struct tty_ldisc *ld;
  249. int ret = 0;
  250. spin_lock_irqsave(&tty_ldisc_lock, flags);
  251. ld = &tty->ldisc;
  252. if (test_bit(TTY_LDISC, &tty->flags)) {
  253. ld->refcount++;
  254. ret = 1;
  255. }
  256. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  257. return ret;
  258. }
  259. /**
  260. * tty_ldisc_ref_wait - wait for the tty ldisc
  261. * @tty: tty device
  262. *
  263. * Dereference the line discipline for the terminal and take a
  264. * reference to it. If the line discipline is in flux then
  265. * wait patiently until it changes.
  266. *
  267. * Note: Must not be called from an IRQ/timer context. The caller
  268. * must also be careful not to hold other locks that will deadlock
  269. * against a discipline change, such as an existing ldisc reference
  270. * (which we check for)
  271. *
  272. * Locking: call functions take tty_ldisc_lock
  273. */
  274. struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty)
  275. {
  276. /* wait_event is a macro */
  277. wait_event(tty_ldisc_wait, tty_ldisc_try(tty));
  278. if (tty->ldisc.refcount == 0)
  279. printk(KERN_ERR "tty_ldisc_ref_wait\n");
  280. return &tty->ldisc;
  281. }
  282. EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait);
  283. /**
  284. * tty_ldisc_ref - get the tty ldisc
  285. * @tty: tty device
  286. *
  287. * Dereference the line discipline for the terminal and take a
  288. * reference to it. If the line discipline is in flux then
  289. * return NULL. Can be called from IRQ and timer functions.
  290. *
  291. * Locking: called functions take tty_ldisc_lock
  292. */
  293. struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty)
  294. {
  295. if (tty_ldisc_try(tty))
  296. return &tty->ldisc;
  297. return NULL;
  298. }
  299. EXPORT_SYMBOL_GPL(tty_ldisc_ref);
  300. /**
  301. * tty_ldisc_deref - free a tty ldisc reference
  302. * @ld: reference to free up
  303. *
  304. * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May
  305. * be called in IRQ context.
  306. *
  307. * Locking: takes tty_ldisc_lock
  308. */
  309. void tty_ldisc_deref(struct tty_ldisc *ld)
  310. {
  311. unsigned long flags;
  312. BUG_ON(ld == NULL);
  313. spin_lock_irqsave(&tty_ldisc_lock, flags);
  314. if (ld->refcount == 0)
  315. printk(KERN_ERR "tty_ldisc_deref: no references.\n");
  316. else
  317. ld->refcount--;
  318. if (ld->refcount == 0)
  319. wake_up(&tty_ldisc_wait);
  320. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  321. }
  322. EXPORT_SYMBOL_GPL(tty_ldisc_deref);
  323. /**
  324. * tty_ldisc_enable - allow ldisc use
  325. * @tty: terminal to activate ldisc on
  326. *
  327. * Set the TTY_LDISC flag when the line discipline can be called
  328. * again. Do necessary wakeups for existing sleepers.
  329. *
  330. * Note: nobody should set this bit except via this function. Clearing
  331. * directly is allowed.
  332. */
  333. void tty_ldisc_enable(struct tty_struct *tty)
  334. {
  335. set_bit(TTY_LDISC, &tty->flags);
  336. wake_up(&tty_ldisc_wait);
  337. }
  338. /**
  339. * tty_set_termios_ldisc - set ldisc field
  340. * @tty: tty structure
  341. * @num: line discipline number
  342. *
  343. * This is probably overkill for real world processors but
  344. * they are not on hot paths so a little discipline won't do
  345. * any harm.
  346. *
  347. * Locking: takes termios_mutex
  348. */
  349. static void tty_set_termios_ldisc(struct tty_struct *tty, int num)
  350. {
  351. mutex_lock(&tty->termios_mutex);
  352. tty->termios->c_line = num;
  353. mutex_unlock(&tty->termios_mutex);
  354. }
  355. /**
  356. * tty_ldisc_restore - helper for tty ldisc change
  357. * @tty: tty to recover
  358. * @old: previous ldisc
  359. *
  360. * Restore the previous line discipline or N_TTY when a line discipline
  361. * change fails due to an open error
  362. */
  363. static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old)
  364. {
  365. char buf[64];
  366. struct tty_ldisc new_ldisc;
  367. /* There is an outstanding reference here so this is safe */
  368. tty_ldisc_get(old->ops->num, old);
  369. tty_ldisc_assign(tty, old);
  370. tty_set_termios_ldisc(tty, old->ops->num);
  371. if (old->ops->open && (old->ops->open(tty) < 0)) {
  372. tty_ldisc_put(old->ops);
  373. /* This driver is always present */
  374. if (tty_ldisc_get(N_TTY, &new_ldisc) < 0)
  375. panic("n_tty: get");
  376. tty_ldisc_assign(tty, &new_ldisc);
  377. tty_set_termios_ldisc(tty, N_TTY);
  378. if (new_ldisc.ops->open) {
  379. int r = new_ldisc.ops->open(tty);
  380. if (r < 0)
  381. panic("Couldn't open N_TTY ldisc for "
  382. "%s --- error %d.",
  383. tty_name(tty, buf), r);
  384. }
  385. }
  386. }
  387. /**
  388. * tty_set_ldisc - set line discipline
  389. * @tty: the terminal to set
  390. * @ldisc: the line discipline
  391. *
  392. * Set the discipline of a tty line. Must be called from a process
  393. * context.
  394. *
  395. * Locking: takes tty_ldisc_lock.
  396. * called functions take termios_mutex
  397. */
  398. int tty_set_ldisc(struct tty_struct *tty, int ldisc)
  399. {
  400. int retval;
  401. struct tty_ldisc o_ldisc, new_ldisc;
  402. int work;
  403. unsigned long flags;
  404. struct tty_struct *o_tty;
  405. restart:
  406. /* This is a bit ugly for now but means we can break the 'ldisc
  407. is part of the tty struct' assumption later */
  408. retval = tty_ldisc_get(ldisc, &new_ldisc);
  409. if (retval)
  410. return retval;
  411. /*
  412. * Problem: What do we do if this blocks ?
  413. */
  414. tty_wait_until_sent(tty, 0);
  415. if (tty->ldisc.ops->num == ldisc) {
  416. tty_ldisc_put(new_ldisc.ops);
  417. return 0;
  418. }
  419. /*
  420. * No more input please, we are switching. The new ldisc
  421. * will update this value in the ldisc open function
  422. */
  423. tty->receive_room = 0;
  424. o_ldisc = tty->ldisc;
  425. o_tty = tty->link;
  426. /*
  427. * Make sure we don't change while someone holds a
  428. * reference to the line discipline. The TTY_LDISC bit
  429. * prevents anyone taking a reference once it is clear.
  430. * We need the lock to avoid racing reference takers.
  431. */
  432. spin_lock_irqsave(&tty_ldisc_lock, flags);
  433. if (tty->ldisc.refcount || (o_tty && o_tty->ldisc.refcount)) {
  434. if (tty->ldisc.refcount) {
  435. /* Free the new ldisc we grabbed. Must drop the lock
  436. first. */
  437. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  438. tty_ldisc_put(o_ldisc.ops);
  439. /*
  440. * There are several reasons we may be busy, including
  441. * random momentary I/O traffic. We must therefore
  442. * retry. We could distinguish between blocking ops
  443. * and retries if we made tty_ldisc_wait() smarter.
  444. * That is up for discussion.
  445. */
  446. if (wait_event_interruptible(tty_ldisc_wait, tty->ldisc.refcount == 0) < 0)
  447. return -ERESTARTSYS;
  448. goto restart;
  449. }
  450. if (o_tty && o_tty->ldisc.refcount) {
  451. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  452. tty_ldisc_put(o_tty->ldisc.ops);
  453. if (wait_event_interruptible(tty_ldisc_wait, o_tty->ldisc.refcount == 0) < 0)
  454. return -ERESTARTSYS;
  455. goto restart;
  456. }
  457. }
  458. /*
  459. * If the TTY_LDISC bit is set, then we are racing against
  460. * another ldisc change
  461. */
  462. if (!test_bit(TTY_LDISC, &tty->flags)) {
  463. struct tty_ldisc *ld;
  464. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  465. tty_ldisc_put(new_ldisc.ops);
  466. ld = tty_ldisc_ref_wait(tty);
  467. tty_ldisc_deref(ld);
  468. goto restart;
  469. }
  470. clear_bit(TTY_LDISC, &tty->flags);
  471. if (o_tty)
  472. clear_bit(TTY_LDISC, &o_tty->flags);
  473. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  474. /*
  475. * From this point on we know nobody has an ldisc
  476. * usage reference, nor can they obtain one until
  477. * we say so later on.
  478. */
  479. work = cancel_delayed_work(&tty->buf.work);
  480. /*
  481. * Wait for ->hangup_work and ->buf.work handlers to terminate
  482. * MUST NOT hold locks here.
  483. */
  484. flush_scheduled_work();
  485. /* Shutdown the current discipline. */
  486. if (o_ldisc.ops->close)
  487. (o_ldisc.ops->close)(tty);
  488. /* Now set up the new line discipline. */
  489. tty_ldisc_assign(tty, &new_ldisc);
  490. tty_set_termios_ldisc(tty, ldisc);
  491. if (new_ldisc.ops->open)
  492. retval = (new_ldisc.ops->open)(tty);
  493. if (retval < 0) {
  494. tty_ldisc_put(new_ldisc.ops);
  495. tty_ldisc_restore(tty, &o_ldisc);
  496. }
  497. /* At this point we hold a reference to the new ldisc and a
  498. a reference to the old ldisc. If we ended up flipping back
  499. to the existing ldisc we have two references to it */
  500. if (tty->ldisc.ops->num != o_ldisc.ops->num && tty->ops->set_ldisc)
  501. tty->ops->set_ldisc(tty);
  502. tty_ldisc_put(o_ldisc.ops);
  503. /*
  504. * Allow ldisc referencing to occur as soon as the driver
  505. * ldisc callback completes.
  506. */
  507. tty_ldisc_enable(tty);
  508. if (o_tty)
  509. tty_ldisc_enable(o_tty);
  510. /* Restart it in case no characters kick it off. Safe if
  511. already running */
  512. if (work)
  513. schedule_delayed_work(&tty->buf.work, 1);
  514. return retval;
  515. }
  516. /**
  517. * tty_ldisc_setup - open line discipline
  518. * @tty: tty being shut down
  519. * @o_tty: pair tty for pty/tty pairs
  520. *
  521. * Called during the initial open of a tty/pty pair in order to set up the
  522. * line discplines and bind them to the tty.
  523. */
  524. int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty)
  525. {
  526. struct tty_ldisc *ld = &tty->ldisc;
  527. int retval;
  528. if (ld->ops->open) {
  529. retval = (ld->ops->open)(tty);
  530. if (retval)
  531. return retval;
  532. }
  533. if (o_tty && o_tty->ldisc.ops->open) {
  534. retval = (o_tty->ldisc.ops->open)(o_tty);
  535. if (retval) {
  536. if (ld->ops->close)
  537. (ld->ops->close)(tty);
  538. return retval;
  539. }
  540. tty_ldisc_enable(o_tty);
  541. }
  542. tty_ldisc_enable(tty);
  543. return 0;
  544. }
  545. /**
  546. * tty_ldisc_release - release line discipline
  547. * @tty: tty being shut down
  548. * @o_tty: pair tty for pty/tty pairs
  549. *
  550. * Called during the final close of a tty/pty pair in order to shut down the
  551. * line discpline layer.
  552. */
  553. void tty_ldisc_release(struct tty_struct *tty, struct tty_struct *o_tty)
  554. {
  555. unsigned long flags;
  556. struct tty_ldisc ld;
  557. /*
  558. * Prevent flush_to_ldisc() from rescheduling the work for later. Then
  559. * kill any delayed work. As this is the final close it does not
  560. * race with the set_ldisc code path.
  561. */
  562. clear_bit(TTY_LDISC, &tty->flags);
  563. cancel_delayed_work(&tty->buf.work);
  564. /*
  565. * Wait for ->hangup_work and ->buf.work handlers to terminate
  566. */
  567. flush_scheduled_work();
  568. /*
  569. * Wait for any short term users (we know they are just driver
  570. * side waiters as the file is closing so user count on the file
  571. * side is zero.
  572. */
  573. spin_lock_irqsave(&tty_ldisc_lock, flags);
  574. while (tty->ldisc.refcount) {
  575. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  576. wait_event(tty_ldisc_wait, tty->ldisc.refcount == 0);
  577. spin_lock_irqsave(&tty_ldisc_lock, flags);
  578. }
  579. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  580. /*
  581. * Shutdown the current line discipline, and reset it to N_TTY.
  582. *
  583. * FIXME: this MUST get fixed for the new reflocking
  584. */
  585. if (tty->ldisc.ops->close)
  586. (tty->ldisc.ops->close)(tty);
  587. tty_ldisc_put(tty->ldisc.ops);
  588. /*
  589. * Switch the line discipline back
  590. */
  591. WARN_ON(tty_ldisc_get(N_TTY, &ld));
  592. tty_ldisc_assign(tty, &ld);
  593. tty_set_termios_ldisc(tty, N_TTY);
  594. if (o_tty) {
  595. /* FIXME: could o_tty be in setldisc here ? */
  596. clear_bit(TTY_LDISC, &o_tty->flags);
  597. if (o_tty->ldisc.ops->close)
  598. (o_tty->ldisc.ops->close)(o_tty);
  599. tty_ldisc_put(o_tty->ldisc.ops);
  600. WARN_ON(tty_ldisc_get(N_TTY, &ld));
  601. tty_ldisc_assign(o_tty, &ld);
  602. tty_set_termios_ldisc(o_tty, N_TTY);
  603. }
  604. }
  605. /**
  606. * tty_ldisc_init - ldisc setup for new tty
  607. * @tty: tty being allocated
  608. *
  609. * Set up the line discipline objects for a newly allocated tty. Note that
  610. * the tty structure is not completely set up when this call is made.
  611. */
  612. void tty_ldisc_init(struct tty_struct *tty)
  613. {
  614. struct tty_ldisc ld;
  615. if (tty_ldisc_get(N_TTY, &ld) < 0)
  616. panic("n_tty: init_tty");
  617. tty_ldisc_assign(tty, &ld);
  618. }
  619. void tty_ldisc_begin(void)
  620. {
  621. /* Setup the default TTY line discipline. */
  622. (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);
  623. }