tty_ldisc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  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/console.h>
  14. #include <linux/timer.h>
  15. #include <linux/ctype.h>
  16. #include <linux/kd.h>
  17. #include <linux/mm.h>
  18. #include <linux/string.h>
  19. #include <linux/slab.h>
  20. #include <linux/poll.h>
  21. #include <linux/proc_fs.h>
  22. #include <linux/init.h>
  23. #include <linux/module.h>
  24. #include <linux/device.h>
  25. #include <linux/wait.h>
  26. #include <linux/bitops.h>
  27. #include <linux/delay.h>
  28. #include <linux/seq_file.h>
  29. #include <linux/uaccess.h>
  30. #include <asm/system.h>
  31. #include <linux/kbd_kern.h>
  32. #include <linux/vt_kern.h>
  33. #include <linux/selection.h>
  34. #include <linux/smp_lock.h> /* For the moment */
  35. #include <linux/kmod.h>
  36. #include <linux/nsproxy.h>
  37. /*
  38. * This guards the refcounted line discipline lists. The lock
  39. * must be taken with irqs off because there are hangup path
  40. * callers who will do ldisc lookups and cannot sleep.
  41. */
  42. static DEFINE_SPINLOCK(tty_ldisc_lock);
  43. static DECLARE_WAIT_QUEUE_HEAD(tty_ldisc_wait);
  44. /* Line disc dispatch table */
  45. static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS];
  46. static inline struct tty_ldisc *get_ldisc(struct tty_ldisc *ld)
  47. {
  48. if (ld)
  49. atomic_inc(&ld->users);
  50. return ld;
  51. }
  52. static void put_ldisc(struct tty_ldisc *ld)
  53. {
  54. unsigned long flags;
  55. if (WARN_ON_ONCE(!ld))
  56. return;
  57. /*
  58. * If this is the last user, free the ldisc, and
  59. * release the ldisc ops.
  60. *
  61. * We really want an "atomic_dec_and_lock_irqsave()",
  62. * but we don't have it, so this does it by hand.
  63. */
  64. local_irq_save(flags);
  65. if (atomic_dec_and_lock(&ld->users, &tty_ldisc_lock)) {
  66. struct tty_ldisc_ops *ldo = ld->ops;
  67. ldo->refcount--;
  68. module_put(ldo->owner);
  69. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  70. kfree(ld);
  71. return;
  72. }
  73. local_irq_restore(flags);
  74. }
  75. /**
  76. * tty_register_ldisc - install a line discipline
  77. * @disc: ldisc number
  78. * @new_ldisc: pointer to the ldisc object
  79. *
  80. * Installs a new line discipline into the kernel. The discipline
  81. * is set up as unreferenced and then made available to the kernel
  82. * from this point onwards.
  83. *
  84. * Locking:
  85. * takes tty_ldisc_lock to guard against ldisc races
  86. */
  87. int tty_register_ldisc(int disc, struct tty_ldisc_ops *new_ldisc)
  88. {
  89. unsigned long flags;
  90. int ret = 0;
  91. if (disc < N_TTY || disc >= NR_LDISCS)
  92. return -EINVAL;
  93. spin_lock_irqsave(&tty_ldisc_lock, flags);
  94. tty_ldiscs[disc] = new_ldisc;
  95. new_ldisc->num = disc;
  96. new_ldisc->refcount = 0;
  97. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  98. return ret;
  99. }
  100. EXPORT_SYMBOL(tty_register_ldisc);
  101. /**
  102. * tty_unregister_ldisc - unload a line discipline
  103. * @disc: ldisc number
  104. * @new_ldisc: pointer to the ldisc object
  105. *
  106. * Remove a line discipline from the kernel providing it is not
  107. * currently in use.
  108. *
  109. * Locking:
  110. * takes tty_ldisc_lock to guard against ldisc races
  111. */
  112. int tty_unregister_ldisc(int disc)
  113. {
  114. unsigned long flags;
  115. int ret = 0;
  116. if (disc < N_TTY || disc >= NR_LDISCS)
  117. return -EINVAL;
  118. spin_lock_irqsave(&tty_ldisc_lock, flags);
  119. if (tty_ldiscs[disc]->refcount)
  120. ret = -EBUSY;
  121. else
  122. tty_ldiscs[disc] = NULL;
  123. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  124. return ret;
  125. }
  126. EXPORT_SYMBOL(tty_unregister_ldisc);
  127. static struct tty_ldisc_ops *get_ldops(int disc)
  128. {
  129. unsigned long flags;
  130. struct tty_ldisc_ops *ldops, *ret;
  131. spin_lock_irqsave(&tty_ldisc_lock, flags);
  132. ret = ERR_PTR(-EINVAL);
  133. ldops = tty_ldiscs[disc];
  134. if (ldops) {
  135. ret = ERR_PTR(-EAGAIN);
  136. if (try_module_get(ldops->owner)) {
  137. ldops->refcount++;
  138. ret = ldops;
  139. }
  140. }
  141. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  142. return ret;
  143. }
  144. static void put_ldops(struct tty_ldisc_ops *ldops)
  145. {
  146. unsigned long flags;
  147. spin_lock_irqsave(&tty_ldisc_lock, flags);
  148. ldops->refcount--;
  149. module_put(ldops->owner);
  150. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  151. }
  152. /**
  153. * tty_ldisc_get - take a reference to an ldisc
  154. * @disc: ldisc number
  155. *
  156. * Takes a reference to a line discipline. Deals with refcounts and
  157. * module locking counts. Returns NULL if the discipline is not available.
  158. * Returns a pointer to the discipline and bumps the ref count if it is
  159. * available
  160. *
  161. * Locking:
  162. * takes tty_ldisc_lock to guard against ldisc races
  163. */
  164. static struct tty_ldisc *tty_ldisc_get(int disc)
  165. {
  166. struct tty_ldisc *ld;
  167. struct tty_ldisc_ops *ldops;
  168. if (disc < N_TTY || disc >= NR_LDISCS)
  169. return ERR_PTR(-EINVAL);
  170. /*
  171. * Get the ldisc ops - we may need to request them to be loaded
  172. * dynamically and try again.
  173. */
  174. ldops = get_ldops(disc);
  175. if (IS_ERR(ldops)) {
  176. request_module("tty-ldisc-%d", disc);
  177. ldops = get_ldops(disc);
  178. if (IS_ERR(ldops))
  179. return ERR_CAST(ldops);
  180. }
  181. ld = kmalloc(sizeof(struct tty_ldisc), GFP_KERNEL);
  182. if (ld == NULL) {
  183. put_ldops(ldops);
  184. return ERR_PTR(-ENOMEM);
  185. }
  186. ld->ops = ldops;
  187. atomic_set(&ld->users, 1);
  188. return ld;
  189. }
  190. static void *tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos)
  191. {
  192. return (*pos < NR_LDISCS) ? pos : NULL;
  193. }
  194. static void *tty_ldiscs_seq_next(struct seq_file *m, void *v, loff_t *pos)
  195. {
  196. (*pos)++;
  197. return (*pos < NR_LDISCS) ? pos : NULL;
  198. }
  199. static void tty_ldiscs_seq_stop(struct seq_file *m, void *v)
  200. {
  201. }
  202. static int tty_ldiscs_seq_show(struct seq_file *m, void *v)
  203. {
  204. int i = *(loff_t *)v;
  205. struct tty_ldisc_ops *ldops;
  206. ldops = get_ldops(i);
  207. if (IS_ERR(ldops))
  208. return 0;
  209. seq_printf(m, "%-10s %2d\n", ldops->name ? ldops->name : "???", i);
  210. put_ldops(ldops);
  211. return 0;
  212. }
  213. static const struct seq_operations tty_ldiscs_seq_ops = {
  214. .start = tty_ldiscs_seq_start,
  215. .next = tty_ldiscs_seq_next,
  216. .stop = tty_ldiscs_seq_stop,
  217. .show = tty_ldiscs_seq_show,
  218. };
  219. static int proc_tty_ldiscs_open(struct inode *inode, struct file *file)
  220. {
  221. return seq_open(file, &tty_ldiscs_seq_ops);
  222. }
  223. const struct file_operations tty_ldiscs_proc_fops = {
  224. .owner = THIS_MODULE,
  225. .open = proc_tty_ldiscs_open,
  226. .read = seq_read,
  227. .llseek = seq_lseek,
  228. .release = seq_release,
  229. };
  230. /**
  231. * tty_ldisc_assign - set ldisc on a tty
  232. * @tty: tty to assign
  233. * @ld: line discipline
  234. *
  235. * Install an instance of a line discipline into a tty structure. The
  236. * ldisc must have a reference count above zero to ensure it remains.
  237. * The tty instance refcount starts at zero.
  238. *
  239. * Locking:
  240. * Caller must hold references
  241. */
  242. static void tty_ldisc_assign(struct tty_struct *tty, struct tty_ldisc *ld)
  243. {
  244. tty->ldisc = ld;
  245. }
  246. /**
  247. * tty_ldisc_try - internal helper
  248. * @tty: the tty
  249. *
  250. * Make a single attempt to grab and bump the refcount on
  251. * the tty ldisc. Return 0 on failure or 1 on success. This is
  252. * used to implement both the waiting and non waiting versions
  253. * of tty_ldisc_ref
  254. *
  255. * Locking: takes tty_ldisc_lock
  256. */
  257. static struct tty_ldisc *tty_ldisc_try(struct tty_struct *tty)
  258. {
  259. unsigned long flags;
  260. struct tty_ldisc *ld;
  261. spin_lock_irqsave(&tty_ldisc_lock, flags);
  262. ld = NULL;
  263. if (test_bit(TTY_LDISC, &tty->flags))
  264. ld = get_ldisc(tty->ldisc);
  265. spin_unlock_irqrestore(&tty_ldisc_lock, flags);
  266. return ld;
  267. }
  268. /**
  269. * tty_ldisc_ref_wait - wait for the tty ldisc
  270. * @tty: tty device
  271. *
  272. * Dereference the line discipline for the terminal and take a
  273. * reference to it. If the line discipline is in flux then
  274. * wait patiently until it changes.
  275. *
  276. * Note: Must not be called from an IRQ/timer context. The caller
  277. * must also be careful not to hold other locks that will deadlock
  278. * against a discipline change, such as an existing ldisc reference
  279. * (which we check for)
  280. *
  281. * Locking: call functions take tty_ldisc_lock
  282. */
  283. struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty)
  284. {
  285. struct tty_ldisc *ld;
  286. /* wait_event is a macro */
  287. wait_event(tty_ldisc_wait, (ld = tty_ldisc_try(tty)) != NULL);
  288. return ld;
  289. }
  290. EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait);
  291. /**
  292. * tty_ldisc_ref - get the tty ldisc
  293. * @tty: tty device
  294. *
  295. * Dereference the line discipline for the terminal and take a
  296. * reference to it. If the line discipline is in flux then
  297. * return NULL. Can be called from IRQ and timer functions.
  298. *
  299. * Locking: called functions take tty_ldisc_lock
  300. */
  301. struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty)
  302. {
  303. return tty_ldisc_try(tty);
  304. }
  305. EXPORT_SYMBOL_GPL(tty_ldisc_ref);
  306. /**
  307. * tty_ldisc_deref - free a tty ldisc reference
  308. * @ld: reference to free up
  309. *
  310. * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May
  311. * be called in IRQ context.
  312. *
  313. * Locking: takes tty_ldisc_lock
  314. */
  315. void tty_ldisc_deref(struct tty_ldisc *ld)
  316. {
  317. put_ldisc(ld);
  318. }
  319. EXPORT_SYMBOL_GPL(tty_ldisc_deref);
  320. static inline void tty_ldisc_put(struct tty_ldisc *ld)
  321. {
  322. put_ldisc(ld);
  323. }
  324. /**
  325. * tty_ldisc_enable - allow ldisc use
  326. * @tty: terminal to activate ldisc on
  327. *
  328. * Set the TTY_LDISC flag when the line discipline can be called
  329. * again. Do necessary wakeups for existing sleepers. Clear the LDISC
  330. * changing flag to indicate any ldisc change is now over.
  331. *
  332. * Note: nobody should set the TTY_LDISC bit except via this function.
  333. * Clearing directly is allowed.
  334. */
  335. void tty_ldisc_enable(struct tty_struct *tty)
  336. {
  337. set_bit(TTY_LDISC, &tty->flags);
  338. clear_bit(TTY_LDISC_CHANGING, &tty->flags);
  339. wake_up(&tty_ldisc_wait);
  340. }
  341. /**
  342. * tty_ldisc_flush - flush line discipline queue
  343. * @tty: tty
  344. *
  345. * Flush the line discipline queue (if any) for this tty. If there
  346. * is no line discipline active this is a no-op.
  347. */
  348. void tty_ldisc_flush(struct tty_struct *tty)
  349. {
  350. struct tty_ldisc *ld = tty_ldisc_ref(tty);
  351. if (ld) {
  352. if (ld->ops->flush_buffer)
  353. ld->ops->flush_buffer(tty);
  354. tty_ldisc_deref(ld);
  355. }
  356. tty_buffer_flush(tty);
  357. }
  358. EXPORT_SYMBOL_GPL(tty_ldisc_flush);
  359. /**
  360. * tty_set_termios_ldisc - set ldisc field
  361. * @tty: tty structure
  362. * @num: line discipline number
  363. *
  364. * This is probably overkill for real world processors but
  365. * they are not on hot paths so a little discipline won't do
  366. * any harm.
  367. *
  368. * Locking: takes termios_mutex
  369. */
  370. static void tty_set_termios_ldisc(struct tty_struct *tty, int num)
  371. {
  372. mutex_lock(&tty->termios_mutex);
  373. tty->termios->c_line = num;
  374. mutex_unlock(&tty->termios_mutex);
  375. }
  376. /**
  377. * tty_ldisc_open - open a line discipline
  378. * @tty: tty we are opening the ldisc on
  379. * @ld: discipline to open
  380. *
  381. * A helper opening method. Also a convenient debugging and check
  382. * point.
  383. */
  384. static int tty_ldisc_open(struct tty_struct *tty, struct tty_ldisc *ld)
  385. {
  386. WARN_ON(test_and_set_bit(TTY_LDISC_OPEN, &tty->flags));
  387. if (ld->ops->open) {
  388. int ret;
  389. /* BKL here locks verus a hangup event */
  390. lock_kernel();
  391. ret = ld->ops->open(tty);
  392. unlock_kernel();
  393. return ret;
  394. }
  395. return 0;
  396. }
  397. /**
  398. * tty_ldisc_close - close a line discipline
  399. * @tty: tty we are opening the ldisc on
  400. * @ld: discipline to close
  401. *
  402. * A helper close method. Also a convenient debugging and check
  403. * point.
  404. */
  405. static void tty_ldisc_close(struct tty_struct *tty, struct tty_ldisc *ld)
  406. {
  407. WARN_ON(!test_bit(TTY_LDISC_OPEN, &tty->flags));
  408. clear_bit(TTY_LDISC_OPEN, &tty->flags);
  409. if (ld->ops->close)
  410. ld->ops->close(tty);
  411. }
  412. /**
  413. * tty_ldisc_restore - helper for tty ldisc change
  414. * @tty: tty to recover
  415. * @old: previous ldisc
  416. *
  417. * Restore the previous line discipline or N_TTY when a line discipline
  418. * change fails due to an open error
  419. */
  420. static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old)
  421. {
  422. char buf[64];
  423. struct tty_ldisc *new_ldisc;
  424. int r;
  425. /* There is an outstanding reference here so this is safe */
  426. old = tty_ldisc_get(old->ops->num);
  427. WARN_ON(IS_ERR(old));
  428. tty_ldisc_assign(tty, old);
  429. tty_set_termios_ldisc(tty, old->ops->num);
  430. if (tty_ldisc_open(tty, old) < 0) {
  431. tty_ldisc_put(old);
  432. /* This driver is always present */
  433. new_ldisc = tty_ldisc_get(N_TTY);
  434. if (IS_ERR(new_ldisc))
  435. panic("n_tty: get");
  436. tty_ldisc_assign(tty, new_ldisc);
  437. tty_set_termios_ldisc(tty, N_TTY);
  438. r = tty_ldisc_open(tty, new_ldisc);
  439. if (r < 0)
  440. panic("Couldn't open N_TTY ldisc for "
  441. "%s --- error %d.",
  442. tty_name(tty, buf), r);
  443. }
  444. }
  445. /**
  446. * tty_ldisc_halt - shut down the line discipline
  447. * @tty: tty device
  448. *
  449. * Shut down the line discipline and work queue for this tty device.
  450. * The TTY_LDISC flag being cleared ensures no further references can
  451. * be obtained while the delayed work queue halt ensures that no more
  452. * data is fed to the ldisc.
  453. *
  454. * You need to do a 'flush_scheduled_work()' (outside the ldisc_mutex)
  455. * in order to make sure any currently executing ldisc work is also
  456. * flushed.
  457. */
  458. static int tty_ldisc_halt(struct tty_struct *tty)
  459. {
  460. clear_bit(TTY_LDISC, &tty->flags);
  461. return cancel_delayed_work_sync(&tty->buf.work);
  462. }
  463. /**
  464. * tty_set_ldisc - set line discipline
  465. * @tty: the terminal to set
  466. * @ldisc: the line discipline
  467. *
  468. * Set the discipline of a tty line. Must be called from a process
  469. * context. The ldisc change logic has to protect itself against any
  470. * overlapping ldisc change (including on the other end of pty pairs),
  471. * the close of one side of a tty/pty pair, and eventually hangup.
  472. *
  473. * Locking: takes tty_ldisc_lock, termios_mutex
  474. */
  475. int tty_set_ldisc(struct tty_struct *tty, int ldisc)
  476. {
  477. int retval;
  478. struct tty_ldisc *o_ldisc, *new_ldisc;
  479. int work, o_work = 0;
  480. struct tty_struct *o_tty;
  481. new_ldisc = tty_ldisc_get(ldisc);
  482. if (IS_ERR(new_ldisc))
  483. return PTR_ERR(new_ldisc);
  484. lock_kernel();
  485. /*
  486. * We need to look at the tty locking here for pty/tty pairs
  487. * when both sides try to change in parallel.
  488. */
  489. o_tty = tty->link; /* o_tty is the pty side or NULL */
  490. /*
  491. * Check the no-op case
  492. */
  493. if (tty->ldisc->ops->num == ldisc) {
  494. unlock_kernel();
  495. tty_ldisc_put(new_ldisc);
  496. return 0;
  497. }
  498. unlock_kernel();
  499. /*
  500. * Problem: What do we do if this blocks ?
  501. * We could deadlock here
  502. */
  503. tty_wait_until_sent(tty, 0);
  504. mutex_lock(&tty->ldisc_mutex);
  505. /*
  506. * We could be midstream of another ldisc change which has
  507. * dropped the lock during processing. If so we need to wait.
  508. */
  509. while (test_bit(TTY_LDISC_CHANGING, &tty->flags)) {
  510. mutex_unlock(&tty->ldisc_mutex);
  511. wait_event(tty_ldisc_wait,
  512. test_bit(TTY_LDISC_CHANGING, &tty->flags) == 0);
  513. mutex_lock(&tty->ldisc_mutex);
  514. }
  515. lock_kernel();
  516. set_bit(TTY_LDISC_CHANGING, &tty->flags);
  517. /*
  518. * No more input please, we are switching. The new ldisc
  519. * will update this value in the ldisc open function
  520. */
  521. tty->receive_room = 0;
  522. o_ldisc = tty->ldisc;
  523. unlock_kernel();
  524. /*
  525. * Make sure we don't change while someone holds a
  526. * reference to the line discipline. The TTY_LDISC bit
  527. * prevents anyone taking a reference once it is clear.
  528. * We need the lock to avoid racing reference takers.
  529. *
  530. * We must clear the TTY_LDISC bit here to avoid a livelock
  531. * with a userspace app continually trying to use the tty in
  532. * parallel to the change and re-referencing the tty.
  533. */
  534. work = tty_ldisc_halt(tty);
  535. if (o_tty)
  536. o_work = tty_ldisc_halt(o_tty);
  537. /*
  538. * Wait for ->hangup_work and ->buf.work handlers to terminate.
  539. * We must drop the mutex here in case a hangup is also in process.
  540. */
  541. mutex_unlock(&tty->ldisc_mutex);
  542. flush_scheduled_work();
  543. mutex_lock(&tty->ldisc_mutex);
  544. lock_kernel();
  545. if (test_bit(TTY_HUPPED, &tty->flags)) {
  546. /* We were raced by the hangup method. It will have stomped
  547. the ldisc data and closed the ldisc down */
  548. clear_bit(TTY_LDISC_CHANGING, &tty->flags);
  549. mutex_unlock(&tty->ldisc_mutex);
  550. tty_ldisc_put(new_ldisc);
  551. unlock_kernel();
  552. return -EIO;
  553. }
  554. /* Shutdown the current discipline. */
  555. tty_ldisc_close(tty, o_ldisc);
  556. /* Now set up the new line discipline. */
  557. tty_ldisc_assign(tty, new_ldisc);
  558. tty_set_termios_ldisc(tty, ldisc);
  559. retval = tty_ldisc_open(tty, new_ldisc);
  560. if (retval < 0) {
  561. /* Back to the old one or N_TTY if we can't */
  562. tty_ldisc_put(new_ldisc);
  563. tty_ldisc_restore(tty, o_ldisc);
  564. }
  565. /* At this point we hold a reference to the new ldisc and a
  566. a reference to the old ldisc. If we ended up flipping back
  567. to the existing ldisc we have two references to it */
  568. if (tty->ldisc->ops->num != o_ldisc->ops->num && tty->ops->set_ldisc)
  569. tty->ops->set_ldisc(tty);
  570. tty_ldisc_put(o_ldisc);
  571. /*
  572. * Allow ldisc referencing to occur again
  573. */
  574. tty_ldisc_enable(tty);
  575. if (o_tty)
  576. tty_ldisc_enable(o_tty);
  577. /* Restart the work queue in case no characters kick it off. Safe if
  578. already running */
  579. if (work)
  580. schedule_delayed_work(&tty->buf.work, 1);
  581. if (o_work)
  582. schedule_delayed_work(&o_tty->buf.work, 1);
  583. mutex_unlock(&tty->ldisc_mutex);
  584. unlock_kernel();
  585. return retval;
  586. }
  587. /**
  588. * tty_reset_termios - reset terminal state
  589. * @tty: tty to reset
  590. *
  591. * Restore a terminal to the driver default state.
  592. */
  593. static void tty_reset_termios(struct tty_struct *tty)
  594. {
  595. mutex_lock(&tty->termios_mutex);
  596. *tty->termios = tty->driver->init_termios;
  597. tty->termios->c_ispeed = tty_termios_input_baud_rate(tty->termios);
  598. tty->termios->c_ospeed = tty_termios_baud_rate(tty->termios);
  599. mutex_unlock(&tty->termios_mutex);
  600. }
  601. /**
  602. * tty_ldisc_reinit - reinitialise the tty ldisc
  603. * @tty: tty to reinit
  604. *
  605. * Switch the tty back to N_TTY line discipline and leave the
  606. * ldisc state closed
  607. */
  608. static void tty_ldisc_reinit(struct tty_struct *tty)
  609. {
  610. struct tty_ldisc *ld;
  611. tty_ldisc_close(tty, tty->ldisc);
  612. tty_ldisc_put(tty->ldisc);
  613. tty->ldisc = NULL;
  614. /*
  615. * Switch the line discipline back
  616. */
  617. ld = tty_ldisc_get(N_TTY);
  618. BUG_ON(IS_ERR(ld));
  619. tty_ldisc_assign(tty, ld);
  620. tty_set_termios_ldisc(tty, N_TTY);
  621. }
  622. /**
  623. * tty_ldisc_hangup - hangup ldisc reset
  624. * @tty: tty being hung up
  625. *
  626. * Some tty devices reset their termios when they receive a hangup
  627. * event. In that situation we must also switch back to N_TTY properly
  628. * before we reset the termios data.
  629. *
  630. * Locking: We can take the ldisc mutex as the rest of the code is
  631. * careful to allow for this.
  632. *
  633. * In the pty pair case this occurs in the close() path of the
  634. * tty itself so we must be careful about locking rules.
  635. */
  636. void tty_ldisc_hangup(struct tty_struct *tty)
  637. {
  638. struct tty_ldisc *ld;
  639. /*
  640. * FIXME! What are the locking issues here? This may me overdoing
  641. * things... This question is especially important now that we've
  642. * removed the irqlock.
  643. */
  644. ld = tty_ldisc_ref(tty);
  645. if (ld != NULL) {
  646. /* We may have no line discipline at this point */
  647. if (ld->ops->flush_buffer)
  648. ld->ops->flush_buffer(tty);
  649. tty_driver_flush_buffer(tty);
  650. if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
  651. ld->ops->write_wakeup)
  652. ld->ops->write_wakeup(tty);
  653. if (ld->ops->hangup)
  654. ld->ops->hangup(tty);
  655. tty_ldisc_deref(ld);
  656. }
  657. /*
  658. * FIXME: Once we trust the LDISC code better we can wait here for
  659. * ldisc completion and fix the driver call race
  660. */
  661. wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
  662. wake_up_interruptible_poll(&tty->read_wait, POLLIN);
  663. /*
  664. * Shutdown the current line discipline, and reset it to
  665. * N_TTY.
  666. */
  667. if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) {
  668. /* Avoid racing set_ldisc or tty_ldisc_release */
  669. mutex_lock(&tty->ldisc_mutex);
  670. tty_ldisc_halt(tty);
  671. if (tty->ldisc) { /* Not yet closed */
  672. /* Switch back to N_TTY */
  673. tty_ldisc_reinit(tty);
  674. /* At this point we have a closed ldisc and we want to
  675. reopen it. We could defer this to the next open but
  676. it means auditing a lot of other paths so this is
  677. a FIXME */
  678. WARN_ON(tty_ldisc_open(tty, tty->ldisc));
  679. tty_ldisc_enable(tty);
  680. }
  681. mutex_unlock(&tty->ldisc_mutex);
  682. tty_reset_termios(tty);
  683. }
  684. }
  685. /**
  686. * tty_ldisc_setup - open line discipline
  687. * @tty: tty being shut down
  688. * @o_tty: pair tty for pty/tty pairs
  689. *
  690. * Called during the initial open of a tty/pty pair in order to set up the
  691. * line disciplines and bind them to the tty. This has no locking issues
  692. * as the device isn't yet active.
  693. */
  694. int tty_ldisc_setup(struct tty_struct *tty, struct tty_struct *o_tty)
  695. {
  696. struct tty_ldisc *ld = tty->ldisc;
  697. int retval;
  698. retval = tty_ldisc_open(tty, ld);
  699. if (retval)
  700. return retval;
  701. if (o_tty) {
  702. retval = tty_ldisc_open(o_tty, o_tty->ldisc);
  703. if (retval) {
  704. tty_ldisc_close(tty, ld);
  705. return retval;
  706. }
  707. tty_ldisc_enable(o_tty);
  708. }
  709. tty_ldisc_enable(tty);
  710. return 0;
  711. }
  712. /**
  713. * tty_ldisc_release - release line discipline
  714. * @tty: tty being shut down
  715. * @o_tty: pair tty for pty/tty pairs
  716. *
  717. * Called during the final close of a tty/pty pair in order to shut down
  718. * the line discpline layer. On exit the ldisc assigned is N_TTY and the
  719. * ldisc has not been opened.
  720. */
  721. void tty_ldisc_release(struct tty_struct *tty, struct tty_struct *o_tty)
  722. {
  723. /*
  724. * Prevent flush_to_ldisc() from rescheduling the work for later. Then
  725. * kill any delayed work. As this is the final close it does not
  726. * race with the set_ldisc code path.
  727. */
  728. tty_ldisc_halt(tty);
  729. flush_scheduled_work();
  730. mutex_lock(&tty->ldisc_mutex);
  731. /*
  732. * Now kill off the ldisc
  733. */
  734. tty_ldisc_close(tty, tty->ldisc);
  735. tty_ldisc_put(tty->ldisc);
  736. /* Force an oops if we mess this up */
  737. tty->ldisc = NULL;
  738. /* Ensure the next open requests the N_TTY ldisc */
  739. tty_set_termios_ldisc(tty, N_TTY);
  740. mutex_unlock(&tty->ldisc_mutex);
  741. /* This will need doing differently if we need to lock */
  742. if (o_tty)
  743. tty_ldisc_release(o_tty, NULL);
  744. /* And the memory resources remaining (buffers, termios) will be
  745. disposed of when the kref hits zero */
  746. }
  747. /**
  748. * tty_ldisc_init - ldisc setup for new tty
  749. * @tty: tty being allocated
  750. *
  751. * Set up the line discipline objects for a newly allocated tty. Note that
  752. * the tty structure is not completely set up when this call is made.
  753. */
  754. void tty_ldisc_init(struct tty_struct *tty)
  755. {
  756. struct tty_ldisc *ld = tty_ldisc_get(N_TTY);
  757. if (IS_ERR(ld))
  758. panic("n_tty: init_tty");
  759. tty_ldisc_assign(tty, ld);
  760. }
  761. void tty_ldisc_begin(void)
  762. {
  763. /* Setup the default TTY line discipline. */
  764. (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);
  765. }