tty_ldisc.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  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. WARN_ON(tty->ldisc.refcount == 0);
  279. return &tty->ldisc;
  280. }
  281. EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait);
  282. /**
  283. * tty_ldisc_ref - get the tty ldisc
  284. * @tty: tty device
  285. *
  286. * Dereference the line discipline for the terminal and take a
  287. * reference to it. If the line discipline is in flux then
  288. * return NULL. Can be called from IRQ and timer functions.
  289. *
  290. * Locking: called functions take tty_ldisc_lock
  291. */
  292. struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty)
  293. {
  294. if (tty_ldisc_try(tty))
  295. return &tty->ldisc;
  296. return NULL;
  297. }
  298. EXPORT_SYMBOL_GPL(tty_ldisc_ref);
  299. /**
  300. * tty_ldisc_deref - free a tty ldisc reference
  301. * @ld: reference to free up
  302. *
  303. * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May
  304. * be called in IRQ context.
  305. *
  306. * Locking: takes tty_ldisc_lock
  307. */
  308. void tty_ldisc_deref(struct tty_ldisc *ld)
  309. {
  310. unsigned long flags;
  311. BUG_ON(ld == NULL);
  312. spin_lock_irqsave(&tty_ldisc_lock, flags);
  313. if (ld->refcount == 0)
  314. printk(KERN_ERR "tty_ldisc_deref: no references.\n");
  315. else
  316. ld->refcount--;
  317. if (ld->refcount == 0)
  318. wake_up(&tty_ldisc_wait);
  319. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  320. }
  321. EXPORT_SYMBOL_GPL(tty_ldisc_deref);
  322. /**
  323. * tty_ldisc_enable - allow ldisc use
  324. * @tty: terminal to activate ldisc on
  325. *
  326. * Set the TTY_LDISC flag when the line discipline can be called
  327. * again. Do necessary wakeups for existing sleepers. Clear the LDISC
  328. * changing flag to indicate any ldisc change is now over.
  329. *
  330. * Note: nobody should set the TTY_LDISC bit except via this function.
  331. * Clearing directly is allowed.
  332. */
  333. void tty_ldisc_enable(struct tty_struct *tty)
  334. {
  335. set_bit(TTY_LDISC, &tty->flags);
  336. clear_bit(TTY_LDISC_CHANGING, &tty->flags);
  337. wake_up(&tty_ldisc_wait);
  338. }
  339. /**
  340. * tty_set_termios_ldisc - set ldisc field
  341. * @tty: tty structure
  342. * @num: line discipline number
  343. *
  344. * This is probably overkill for real world processors but
  345. * they are not on hot paths so a little discipline won't do
  346. * any harm.
  347. *
  348. * Locking: takes termios_mutex
  349. */
  350. static void tty_set_termios_ldisc(struct tty_struct *tty, int num)
  351. {
  352. mutex_lock(&tty->termios_mutex);
  353. tty->termios->c_line = num;
  354. mutex_unlock(&tty->termios_mutex);
  355. }
  356. /**
  357. * tty_ldisc_restore - helper for tty ldisc change
  358. * @tty: tty to recover
  359. * @old: previous ldisc
  360. *
  361. * Restore the previous line discipline or N_TTY when a line discipline
  362. * change fails due to an open error
  363. */
  364. static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old)
  365. {
  366. char buf[64];
  367. struct tty_ldisc new_ldisc;
  368. /* There is an outstanding reference here so this is safe */
  369. tty_ldisc_get(old->ops->num, old);
  370. tty_ldisc_assign(tty, old);
  371. tty_set_termios_ldisc(tty, old->ops->num);
  372. if (old->ops->open && (old->ops->open(tty) < 0)) {
  373. tty_ldisc_put(old->ops);
  374. /* This driver is always present */
  375. if (tty_ldisc_get(N_TTY, &new_ldisc) < 0)
  376. panic("n_tty: get");
  377. tty_ldisc_assign(tty, &new_ldisc);
  378. tty_set_termios_ldisc(tty, N_TTY);
  379. if (new_ldisc.ops->open) {
  380. int r = new_ldisc.ops->open(tty);
  381. if (r < 0)
  382. panic("Couldn't open N_TTY ldisc for "
  383. "%s --- error %d.",
  384. tty_name(tty, buf), r);
  385. }
  386. }
  387. }
  388. /**
  389. * tty_set_ldisc - set line discipline
  390. * @tty: the terminal to set
  391. * @ldisc: the line discipline
  392. *
  393. * Set the discipline of a tty line. Must be called from a process
  394. * context.
  395. *
  396. * Locking: takes tty_ldisc_lock.
  397. * called functions take termios_mutex
  398. */
  399. int tty_set_ldisc(struct tty_struct *tty, int ldisc)
  400. {
  401. int retval;
  402. struct tty_ldisc o_ldisc, new_ldisc;
  403. int work;
  404. unsigned long flags;
  405. struct tty_struct *o_tty;
  406. restart:
  407. /* This is a bit ugly for now but means we can break the 'ldisc
  408. is part of the tty struct' assumption later */
  409. retval = tty_ldisc_get(ldisc, &new_ldisc);
  410. if (retval)
  411. return retval;
  412. /*
  413. * Problem: What do we do if this blocks ?
  414. */
  415. tty_wait_until_sent(tty, 0);
  416. if (tty->ldisc.ops->num == ldisc) {
  417. tty_ldisc_put(new_ldisc.ops);
  418. return 0;
  419. }
  420. /*
  421. * No more input please, we are switching. The new ldisc
  422. * will update this value in the ldisc open function
  423. */
  424. tty->receive_room = 0;
  425. o_ldisc = tty->ldisc;
  426. o_tty = tty->link;
  427. /*
  428. * Make sure we don't change while someone holds a
  429. * reference to the line discipline. The TTY_LDISC bit
  430. * prevents anyone taking a reference once it is clear.
  431. * We need the lock to avoid racing reference takers.
  432. *
  433. * We must clear the TTY_LDISC bit here to avoid a livelock
  434. * with a userspace app continually trying to use the tty in
  435. * parallel to the change and re-referencing the tty.
  436. */
  437. clear_bit(TTY_LDISC, &tty->flags);
  438. if (o_tty)
  439. clear_bit(TTY_LDISC, &o_tty->flags);
  440. spin_lock_irqsave(&tty_ldisc_lock, flags);
  441. if (tty->ldisc.refcount || (o_tty && o_tty->ldisc.refcount)) {
  442. if (tty->ldisc.refcount) {
  443. /* Free the new ldisc we grabbed. Must drop the lock
  444. first. */
  445. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  446. tty_ldisc_put(o_ldisc.ops);
  447. /*
  448. * There are several reasons we may be busy, including
  449. * random momentary I/O traffic. We must therefore
  450. * retry. We could distinguish between blocking ops
  451. * and retries if we made tty_ldisc_wait() smarter.
  452. * That is up for discussion.
  453. */
  454. if (wait_event_interruptible(tty_ldisc_wait, tty->ldisc.refcount == 0) < 0)
  455. return -ERESTARTSYS;
  456. goto restart;
  457. }
  458. if (o_tty && o_tty->ldisc.refcount) {
  459. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  460. tty_ldisc_put(o_tty->ldisc.ops);
  461. if (wait_event_interruptible(tty_ldisc_wait, o_tty->ldisc.refcount == 0) < 0)
  462. return -ERESTARTSYS;
  463. goto restart;
  464. }
  465. }
  466. /*
  467. * If the TTY_LDISC bit is set, then we are racing against
  468. * another ldisc change
  469. */
  470. if (test_bit(TTY_LDISC_CHANGING, &tty->flags)) {
  471. struct tty_ldisc *ld;
  472. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  473. tty_ldisc_put(new_ldisc.ops);
  474. ld = tty_ldisc_ref_wait(tty);
  475. tty_ldisc_deref(ld);
  476. goto restart;
  477. }
  478. /*
  479. * This flag is used to avoid two parallel ldisc changes. Once
  480. * open and close are fine grained locked this may work better
  481. * as a mutex shared with the open/close/hup paths
  482. */
  483. set_bit(TTY_LDISC_CHANGING, &tty->flags);
  484. if (o_tty)
  485. set_bit(TTY_LDISC_CHANGING, &o_tty->flags);
  486. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  487. /*
  488. * From this point on we know nobody has an ldisc
  489. * usage reference, nor can they obtain one until
  490. * we say so later on.
  491. */
  492. work = cancel_delayed_work(&tty->buf.work);
  493. /*
  494. * Wait for ->hangup_work and ->buf.work handlers to terminate
  495. * MUST NOT hold locks here.
  496. */
  497. flush_scheduled_work();
  498. /* Shutdown the current discipline. */
  499. if (o_ldisc.ops->close)
  500. (o_ldisc.ops->close)(tty);
  501. /* Now set up the new line discipline. */
  502. tty_ldisc_assign(tty, &new_ldisc);
  503. tty_set_termios_ldisc(tty, ldisc);
  504. if (new_ldisc.ops->open)
  505. retval = (new_ldisc.ops->open)(tty);
  506. if (retval < 0) {
  507. tty_ldisc_put(new_ldisc.ops);
  508. tty_ldisc_restore(tty, &o_ldisc);
  509. }
  510. /* At this point we hold a reference to the new ldisc and a
  511. a reference to the old ldisc. If we ended up flipping back
  512. to the existing ldisc we have two references to it */
  513. if (tty->ldisc.ops->num != o_ldisc.ops->num && tty->ops->set_ldisc)
  514. tty->ops->set_ldisc(tty);
  515. tty_ldisc_put(o_ldisc.ops);
  516. /*
  517. * Allow ldisc referencing to occur as soon as the driver
  518. * ldisc callback completes.
  519. */
  520. tty_ldisc_enable(tty);
  521. if (o_tty)
  522. tty_ldisc_enable(o_tty);
  523. /* Restart it in case no characters kick it off. Safe if
  524. already running */
  525. if (work)
  526. schedule_delayed_work(&tty->buf.work, 1);
  527. return retval;
  528. }
  529. /**
  530. * tty_ldisc_setup - open line discipline
  531. * @tty: tty being shut down
  532. * @o_tty: pair tty for pty/tty pairs
  533. *
  534. * Called during the initial open of a tty/pty pair in order to set up the
  535. * line discplines and bind them to the tty.
  536. */
  537. int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty)
  538. {
  539. struct tty_ldisc *ld = &tty->ldisc;
  540. int retval;
  541. if (ld->ops->open) {
  542. retval = (ld->ops->open)(tty);
  543. if (retval)
  544. return retval;
  545. }
  546. if (o_tty && o_tty->ldisc.ops->open) {
  547. retval = (o_tty->ldisc.ops->open)(o_tty);
  548. if (retval) {
  549. if (ld->ops->close)
  550. (ld->ops->close)(tty);
  551. return retval;
  552. }
  553. tty_ldisc_enable(o_tty);
  554. }
  555. tty_ldisc_enable(tty);
  556. return 0;
  557. }
  558. /**
  559. * tty_ldisc_release - release line discipline
  560. * @tty: tty being shut down
  561. * @o_tty: pair tty for pty/tty pairs
  562. *
  563. * Called during the final close of a tty/pty pair in order to shut down the
  564. * line discpline layer.
  565. */
  566. void tty_ldisc_release(struct tty_struct *tty, struct tty_struct *o_tty)
  567. {
  568. unsigned long flags;
  569. struct tty_ldisc ld;
  570. /*
  571. * Prevent flush_to_ldisc() from rescheduling the work for later. Then
  572. * kill any delayed work. As this is the final close it does not
  573. * race with the set_ldisc code path.
  574. */
  575. clear_bit(TTY_LDISC, &tty->flags);
  576. cancel_delayed_work(&tty->buf.work);
  577. /*
  578. * Wait for ->hangup_work and ->buf.work handlers to terminate
  579. */
  580. flush_scheduled_work();
  581. /*
  582. * Wait for any short term users (we know they are just driver
  583. * side waiters as the file is closing so user count on the file
  584. * side is zero.
  585. */
  586. spin_lock_irqsave(&tty_ldisc_lock, flags);
  587. while (tty->ldisc.refcount) {
  588. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  589. wait_event(tty_ldisc_wait, tty->ldisc.refcount == 0);
  590. spin_lock_irqsave(&tty_ldisc_lock, flags);
  591. }
  592. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  593. /*
  594. * Shutdown the current line discipline, and reset it to N_TTY.
  595. *
  596. * FIXME: this MUST get fixed for the new reflocking
  597. */
  598. if (tty->ldisc.ops->close)
  599. (tty->ldisc.ops->close)(tty);
  600. tty_ldisc_put(tty->ldisc.ops);
  601. /*
  602. * Switch the line discipline back
  603. */
  604. WARN_ON(tty_ldisc_get(N_TTY, &ld));
  605. tty_ldisc_assign(tty, &ld);
  606. tty_set_termios_ldisc(tty, N_TTY);
  607. if (o_tty) {
  608. /* FIXME: could o_tty be in setldisc here ? */
  609. clear_bit(TTY_LDISC, &o_tty->flags);
  610. if (o_tty->ldisc.ops->close)
  611. (o_tty->ldisc.ops->close)(o_tty);
  612. tty_ldisc_put(o_tty->ldisc.ops);
  613. WARN_ON(tty_ldisc_get(N_TTY, &ld));
  614. tty_ldisc_assign(o_tty, &ld);
  615. tty_set_termios_ldisc(o_tty, N_TTY);
  616. }
  617. }
  618. /**
  619. * tty_ldisc_init - ldisc setup for new tty
  620. * @tty: tty being allocated
  621. *
  622. * Set up the line discipline objects for a newly allocated tty. Note that
  623. * the tty structure is not completely set up when this call is made.
  624. */
  625. void tty_ldisc_init(struct tty_struct *tty)
  626. {
  627. struct tty_ldisc ld;
  628. if (tty_ldisc_get(N_TTY, &ld) < 0)
  629. panic("n_tty: init_tty");
  630. tty_ldisc_assign(tty, &ld);
  631. }
  632. void tty_ldisc_begin(void)
  633. {
  634. /* Setup the default TTY line discipline. */
  635. (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);
  636. }